api documentation for ec_parse
[protos/libecoli.git] / src / ecoli_node_cmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <sys/queue.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <stdarg.h>
11 #include <errno.h>
12 #include <limits.h>
13
14 #include <ecoli_init.h>
15 #include <ecoli_malloc.h>
16 #include <ecoli_log.h>
17 #include <ecoli_test.h>
18 #include <ecoli_strvec.h>
19 #include <ecoli_node.h>
20 #include <ecoli_config.h>
21 #include <ecoli_parse.h>
22 #include <ecoli_complete.h>
23 #include <ecoli_node_helper.h>
24 #include <ecoli_node_expr.h>
25 #include <ecoli_node_str.h>
26 #include <ecoli_node_or.h>
27 #include <ecoli_node_subset.h>
28 #include <ecoli_node_int.h>
29 #include <ecoli_node_many.h>
30 #include <ecoli_node_seq.h>
31 #include <ecoli_node_option.h>
32 #include <ecoli_node_re.h>
33 #include <ecoli_node_re_lex.h>
34 #include <ecoli_node_cmd.h>
35
36 EC_LOG_TYPE_REGISTER(node_cmd);
37
38 static struct ec_node *ec_node_cmd_parser; /* the expression parser. */
39 static struct ec_node *ec_node_cmd_expr;   /* the expr parser without lexer. */
40
41 struct ec_node_cmd {
42         char *cmd_str;           /* the command string. */
43         struct ec_node *cmd;     /* the command node. */
44         struct ec_node **table;  /* table of node referenced in command. */
45         unsigned int len;        /* len of the table. */
46 };
47
48 /* passed as user context to expression parser */
49 struct ec_node_cmd_ctx {
50         struct ec_node **table;
51         unsigned int len;
52 };
53
54 static int
55 ec_node_cmd_eval_var(void **result, void *userctx,
56         const struct ec_pnode *var)
57 {
58         const struct ec_strvec *vec;
59         struct ec_node_cmd_ctx *ctx = userctx;
60         struct ec_node *eval = NULL;
61         const char *str, *id;
62         unsigned int i;
63
64         /* get parsed string vector, it should contain only one str */
65         vec = ec_pnode_get_strvec(var);
66         if (ec_strvec_len(vec) != 1) {
67                 errno = EINVAL;
68                 return -1;
69         }
70         str = ec_strvec_val(vec, 0);
71
72         for (i = 0; i < ctx->len; i++) {
73                 id = ec_node_id(ctx->table[i]);
74                 if (id == NULL)
75                         continue;
76                 if (strcmp(str, id))
77                         continue;
78                 /* if id matches, use a node provided by the user... */
79                 eval = ec_node_clone(ctx->table[i]);
80                 if (eval == NULL)
81                         return -1;
82                 break;
83         }
84
85         /* ...or create a string node */
86         if (eval == NULL) {
87                 eval = ec_node_str(EC_NO_ID, str);
88                 if (eval == NULL)
89                         return -1;
90         }
91
92         *result = eval;
93
94         return 0;
95 }
96
97 static int
98 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
99         const struct ec_pnode *operator)
100 {
101         (void)result;
102         (void)userctx;
103         (void)operand;
104         (void)operator;
105
106         errno = EINVAL;
107         return -1;
108 }
109
110 static int
111 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
112         const struct ec_pnode *operator)
113 {
114         const struct ec_strvec *vec;
115         struct ec_node *in = operand;;
116         struct ec_node *out = NULL;;
117
118         (void)userctx;
119
120         /* get parsed string vector, it should contain only one str */
121         vec = ec_pnode_get_strvec(operator);
122         if (ec_strvec_len(vec) != 1) {
123                 errno = EINVAL;
124                 return -1;
125         }
126
127         if (!strcmp(ec_strvec_val(vec, 0), "*")) {
128                 out = ec_node_many(EC_NO_ID,
129                                 ec_node_clone(in), 0, 0);
130                 if (out == NULL)
131                         return -1;
132                 ec_node_free(in);
133                 *result = out;
134         } else {
135                 errno = EINVAL;
136                 return -1;
137         }
138
139         return 0;
140 }
141
142 static int
143 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
144         const struct ec_pnode *operator, void *operand2)
145
146 {
147         const struct ec_strvec *vec;
148         struct ec_node *out = NULL;
149         struct ec_node *in1 = operand1;
150         struct ec_node *in2 = operand2;
151
152         (void)userctx;
153
154         /* get parsed string vector, it should contain only one str */
155         vec = ec_pnode_get_strvec(operator);
156         if (ec_strvec_len(vec) > 1) {
157                 errno = EINVAL;
158                 return -1;
159         }
160
161         if (ec_strvec_len(vec) == 0) {
162                 if (!strcmp(ec_node_get_type_name(in1), "seq")) {
163                         if (ec_node_seq_add(in1, ec_node_clone(in2)) < 0)
164                                 return -1;
165                         ec_node_free(in2);
166                         *result = in1;
167                 } else {
168                         out = EC_NODE_SEQ(EC_NO_ID, ec_node_clone(in1),
169                                         ec_node_clone(in2));
170                         if (out == NULL)
171                                 return -1;
172                         ec_node_free(in1);
173                         ec_node_free(in2);
174                         *result = out;
175                 }
176         } else if (!strcmp(ec_strvec_val(vec, 0), "|")) {
177                 if (!strcmp(ec_node_get_type_name(in2), "or")) {
178                         if (ec_node_or_add(in2, ec_node_clone(in1)) < 0)
179                                 return -1;
180                         ec_node_free(in1);
181                         *result = in2;
182                 } else if (!strcmp(ec_node_get_type_name(in1), "or")) {
183                         if (ec_node_or_add(in1, ec_node_clone(in2)) < 0)
184                                 return -1;
185                         ec_node_free(in2);
186                         *result = in1;
187                 } else {
188                         out = EC_NODE_OR(EC_NO_ID, ec_node_clone(in1),
189                                         ec_node_clone(in2));
190                         if (out == NULL)
191                                 return -1;
192                         ec_node_free(in1);
193                         ec_node_free(in2);
194                         *result = out;
195                 }
196         } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
197                 if (!strcmp(ec_node_get_type_name(in2), "subset")) {
198                         if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
199                                 return -1;
200                         ec_node_free(in1);
201                         *result = in2;
202                 } else if (!strcmp(ec_node_get_type_name(in1), "subset")) {
203                         if (ec_node_subset_add(in1, ec_node_clone(in2)) < 0)
204                                 return -1;
205                         ec_node_free(in2);
206                         *result = in1;
207                 } else {
208                         out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
209                                         ec_node_clone(in2));
210                         if (out == NULL)
211                                 return -1;
212                         ec_node_free(in1);
213                         ec_node_free(in2);
214                         *result = out;
215                 }
216         } else {
217                 errno = EINVAL;
218                 return -1;
219         }
220
221         return 0;
222 }
223
224 static int
225 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
226         const struct ec_pnode *open_paren,
227         const struct ec_pnode *close_paren,
228         void *value)
229 {
230         const struct ec_strvec *vec;
231         struct ec_node *in = value;;
232         struct ec_node *out = NULL;;
233
234         (void)userctx;
235         (void)close_paren;
236
237         /* get parsed string vector, it should contain only one str */
238         vec = ec_pnode_get_strvec(open_paren);
239         if (ec_strvec_len(vec) != 1) {
240                 errno = EINVAL;
241                 return -1;
242         }
243
244         if (!strcmp(ec_strvec_val(vec, 0), "[")) {
245                 out = ec_node_option(EC_NO_ID, ec_node_clone(in));
246                 if (out == NULL)
247                         return -1;
248                 ec_node_free(in);
249         } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
250                 out = in;
251         } else {
252                 errno = EINVAL;
253                 return -1;
254         }
255
256         *result = out;
257
258         return 0;
259 }
260
261 static void
262 ec_node_cmd_eval_free(void *result, void *userctx)
263 {
264         (void)userctx;
265         ec_free(result);
266 }
267
268 static const struct ec_node_expr_eval_ops expr_ops = {
269         .eval_var = ec_node_cmd_eval_var,
270         .eval_pre_op = ec_node_cmd_eval_pre_op,
271         .eval_post_op = ec_node_cmd_eval_post_op,
272         .eval_bin_op = ec_node_cmd_eval_bin_op,
273         .eval_parenthesis = ec_node_cmd_eval_parenthesis,
274         .eval_free = ec_node_cmd_eval_free,
275 };
276
277 static struct ec_node *
278 ec_node_cmd_build_expr(void)
279 {
280         struct ec_node *expr = NULL;
281         int ret;
282
283         /* build the expression parser */
284         expr = ec_node("expr", "expr");
285         if (expr == NULL)
286                 goto fail;
287         ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID,
288                                         "[a-zA-Z0-9]+"));
289         if (ret < 0)
290                 goto fail;
291         ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, ","));
292         if (ret < 0)
293                 goto fail;
294         ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, "|"));
295         if (ret < 0)
296                 goto fail;
297         ret = ec_node_expr_add_bin_op(expr, ec_node("empty", EC_NO_ID));
298         if (ret < 0)
299                 goto fail;
300         ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "+"));
301         if (ret < 0)
302                 goto fail;
303         ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "*"));
304         if (ret < 0)
305                 goto fail;
306         ret = ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "["),
307                 ec_node_str(EC_NO_ID, "]"));
308         if (ret < 0)
309                 goto fail;
310         ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "("),
311                 ec_node_str(EC_NO_ID, ")"));
312         if (ret < 0)
313                 goto fail;
314
315         return expr;
316
317 fail:
318         ec_node_free(expr);
319         return NULL;
320 }
321
322 static struct ec_node *
323 ec_node_cmd_build_parser(struct ec_node *expr)
324 {
325         struct ec_node *lex = NULL;
326         int ret;
327
328         /* prepend a lexer to the expression node */
329         lex = ec_node_re_lex(EC_NO_ID, ec_node_clone(expr));
330         if (lex == NULL)
331                 goto fail;
332
333         ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1, NULL);
334         if (ret < 0)
335                 goto fail;
336         ret = ec_node_re_lex_add(lex, "[*|,()]", 1, NULL);
337         if (ret < 0)
338                 goto fail;
339         ret = ec_node_re_lex_add(lex, "\\[", 1, NULL);
340         if (ret < 0)
341                 goto fail;
342         ret = ec_node_re_lex_add(lex, "\\]", 1, NULL);
343         if (ret < 0)
344                 goto fail;
345         ret = ec_node_re_lex_add(lex, "[         ]+", 0, NULL);
346         if (ret < 0)
347                 goto fail;
348
349         return lex;
350
351 fail:
352         ec_node_free(lex);
353
354         return NULL;
355 }
356
357 static struct ec_node *
358 ec_node_cmd_build(const char *cmd_str, struct ec_node **table, size_t len)
359 {
360         struct ec_node_cmd_ctx ctx = { table, len };
361         struct ec_pnode *p = NULL;
362         void *result;
363         int ret;
364
365         /* parse the command expression */
366         p = ec_parse(ec_node_cmd_parser, cmd_str);
367         if (p == NULL)
368                 goto fail;
369
370         if (!ec_pnode_matches(p)) {
371                 errno = EINVAL;
372                 goto fail;
373         }
374
375         ret = ec_node_expr_eval(&result, ec_node_cmd_expr,
376                                 ec_pnode_get_first_child(p),
377                                 &expr_ops, &ctx);
378         if (ret < 0)
379                 goto fail;
380
381         ec_pnode_free(p);
382         return result;
383
384 fail:
385         ec_pnode_free(p);
386         return NULL;
387 }
388
389 static int
390 ec_node_cmd_parse(const struct ec_node *node, struct ec_pnode *pstate,
391                 const struct ec_strvec *strvec)
392 {
393         struct ec_node_cmd *priv = ec_node_priv(node);
394
395         return ec_parse_child(priv->cmd, pstate, strvec);
396 }
397
398 static int
399 ec_node_cmd_complete(const struct ec_node *node,
400                 struct ec_comp *comp,
401                 const struct ec_strvec *strvec)
402 {
403         struct ec_node_cmd *priv = ec_node_priv(node);
404
405         return ec_complete_child(priv->cmd, comp, strvec);
406 }
407
408 static void ec_node_cmd_free_priv(struct ec_node *node)
409 {
410         struct ec_node_cmd *priv = ec_node_priv(node);
411         size_t i;
412
413         ec_free(priv->cmd_str);
414         priv->cmd_str = NULL;
415         ec_node_free(priv->cmd);
416         priv->cmd = NULL;
417         for (i = 0; i < priv->len; i++)
418                 ec_node_free(priv->table[i]);
419         ec_free(priv->table);
420         priv->table = NULL;
421         priv->len = 0;
422 }
423
424 static const struct ec_config_schema ec_node_cmd_subschema[] = {
425         {
426                 .desc = "A child node whose id is referenced in the expression.",
427                 .type = EC_CONFIG_TYPE_NODE,
428         },
429         {
430                 .type = EC_CONFIG_TYPE_NONE,
431         },
432 };
433
434 static const struct ec_config_schema ec_node_cmd_schema[] = {
435         {
436                 .key = "expr",
437                 .desc = "The expression to match. Supported operators "
438                 "are or '|', list ',', many '+', many-or-zero '*', "
439                 "option '[]', group '()'. An identifier (alphanumeric) can "
440                 "reference a node whose node_id matches. Else it is "
441                 "interpreted as ec_node_str() matching this string. "
442                 "Example: command [option] (subset1, subset2) x|y",
443                 .type = EC_CONFIG_TYPE_STRING,
444         },
445         {
446                 .key = "children",
447                 .desc = "The list of children nodes.",
448                 .type = EC_CONFIG_TYPE_LIST,
449                 .subschema = ec_node_cmd_subschema,
450         },
451         {
452                 .type = EC_CONFIG_TYPE_NONE,
453         },
454 };
455
456 static int ec_node_cmd_set_config(struct ec_node *node,
457                                 const struct ec_config *config)
458 {
459         struct ec_node_cmd *priv = ec_node_priv(node);
460         const struct ec_config *expr = NULL;
461         struct ec_node *cmd = NULL;
462         struct ec_node **table = NULL;
463         char *cmd_str = NULL;
464         size_t len = 0, i;
465
466         /* retrieve config locally */
467         expr = ec_config_dict_get(config, "expr");
468         if (expr == NULL) {
469                 errno = EINVAL;
470                 goto fail;
471         }
472
473         table = ec_node_config_node_list_to_table(
474                 ec_config_dict_get(config, "children"), &len);
475         if (table == NULL)
476                 goto fail;
477
478         cmd_str = ec_strdup(expr->string);
479         if (cmd_str == NULL)
480                 goto fail;
481
482         /* parse expression to build the cmd child node */
483         cmd = ec_node_cmd_build(cmd_str, table, len);
484         if (cmd == NULL)
485                 goto fail;
486
487         /* ok, store the config */
488         ec_node_free(priv->cmd);
489         priv->cmd = cmd;
490         ec_free(priv->cmd_str);
491         priv->cmd_str = cmd_str;
492         for (i = 0; i < priv->len; i++)
493                 ec_node_free(priv->table[i]);
494         ec_free(priv->table);
495         priv->table = table;
496         priv->len = len;
497
498         return 0;
499
500 fail:
501         for (i = 0; i < len; i++)
502                 ec_node_free(table[i]);
503         ec_free(table);
504         ec_free(cmd_str);
505         ec_node_free(cmd);
506         return -1;
507 }
508
509 static size_t
510 ec_node_cmd_get_children_count(const struct ec_node *node)
511 {
512         struct ec_node_cmd *priv = ec_node_priv(node);
513
514         if (priv->cmd == NULL)
515                 return 0;
516         return 1;
517 }
518
519 static int
520 ec_node_cmd_get_child(const struct ec_node *node, size_t i,
521                 struct ec_node **child, unsigned int *refs)
522 {
523         struct ec_node_cmd *priv = ec_node_priv(node);
524
525         if (i > 0)
526                 return -1;
527
528         *child = priv->cmd;
529         *refs = 1;
530         return 0;
531 }
532
533 static struct ec_node_type ec_node_cmd_type = {
534         .name = "cmd",
535         .schema = ec_node_cmd_schema,
536         .set_config = ec_node_cmd_set_config,
537         .parse = ec_node_cmd_parse,
538         .complete = ec_node_cmd_complete,
539         .size = sizeof(struct ec_node_cmd),
540         .free_priv = ec_node_cmd_free_priv,
541         .get_children_count = ec_node_cmd_get_children_count,
542         .get_child = ec_node_cmd_get_child,
543 };
544
545 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
546
547 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
548 {
549         struct ec_config *config = NULL, *children = NULL;
550         struct ec_node *node = NULL;
551         va_list ap;
552         int ret;
553
554         /* this block must stay first, it frees the nodes on error */
555         va_start(ap, cmd);
556         children = ec_node_config_node_list_from_vargs(ap);
557         va_end(ap);
558         if (children == NULL)
559                 goto fail;
560
561         node = ec_node_from_type(&ec_node_cmd_type, id);
562         if (node == NULL)
563                 goto fail;
564
565         config = ec_config_dict();
566         if (config == NULL)
567                 goto fail;
568
569         if (ec_config_dict_set(config, "expr", ec_config_string(cmd)) < 0)
570                 goto fail;
571
572         if (ec_config_dict_set(config, "children", children) < 0) {
573                 children = NULL; /* freed */
574                 goto fail;
575         }
576         children = NULL;
577
578         ret = ec_node_set_config(node, config);
579         config = NULL; /* freed */
580         if (ret < 0)
581                 goto fail;
582
583         return node;
584
585 fail:
586         ec_node_free(node); /* will also free added children */
587         ec_config_free(children);
588         ec_config_free(config);
589
590         return NULL;
591 }
592
593 static int ec_node_cmd_init_func(void)
594 {
595         ec_node_cmd_expr = ec_node_cmd_build_expr();
596         if (ec_node_cmd_expr == NULL)
597                 goto fail;
598
599         ec_node_cmd_parser = ec_node_cmd_build_parser(ec_node_cmd_expr);
600         if (ec_node_cmd_parser == NULL)
601                 goto fail;
602
603         return 0;
604
605 fail:
606         EC_LOG(EC_LOG_ERR, "Failed to initialize command parser\n");
607         ec_node_free(ec_node_cmd_expr);
608         ec_node_cmd_expr = NULL;
609         ec_node_free(ec_node_cmd_parser);
610         ec_node_cmd_parser = NULL;
611         return -1;
612 }
613
614 static void ec_node_cmd_exit_func(void)
615 {
616         ec_node_free(ec_node_cmd_expr);
617         ec_node_cmd_expr = NULL;
618         ec_node_free(ec_node_cmd_parser);
619         ec_node_cmd_parser = NULL;
620 }
621
622 static struct ec_init ec_node_cmd_init = {
623         .init = ec_node_cmd_init_func,
624         .exit = ec_node_cmd_exit_func,
625         .priority = 75,
626 };
627
628 EC_INIT_REGISTER(ec_node_cmd_init);
629
630 /* LCOV_EXCL_START */
631 static int ec_node_cmd_testcase(void)
632 {
633         struct ec_node *node;
634         int testres = 0;
635
636         node = EC_NODE_CMD(EC_NO_ID,
637                 "command [option] (subset1, subset2, subset3, subset4) x|y z*",
638                 ec_node_int("x", 0, 10, 10),
639                 ec_node_int("y", 20, 30, 10)
640         );
641         if (node == NULL) {
642                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
643                 return -1;
644         }
645         testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
646         testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "subset1", "1");
647         testres |= EC_TEST_CHECK_PARSE(node, 4, "command", "subset3", "subset2",
648                                 "1");
649         testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "subset2", "subset3",
650                                 "subset1", "1");
651         testres |= EC_TEST_CHECK_PARSE(node, 6, "command", "subset3", "subset1",
652                                 "subset4", "subset2", "4");
653         testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
654         testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
655         testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
656                                 "z", "z");
657         testres |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
658         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
659         ec_node_free(node);
660
661         node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
662                         ec_node_int("count", 0, 10, 10));
663         if (node == NULL) {
664                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
665                 return -1;
666         }
667         testres |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
668
669         testres |= EC_TEST_CHECK_COMPLETE(node,
670                 "", EC_VA_END,
671                 "good", EC_VA_END);
672         testres |= EC_TEST_CHECK_COMPLETE(node,
673                 "g", EC_VA_END,
674                 "good", EC_VA_END);
675         testres |= EC_TEST_CHECK_COMPLETE(node,
676                 "good", "morning", "", EC_VA_END,
677                 "bob", "bobby", "michael", EC_VA_END);
678
679         ec_node_free(node);
680
681         node = EC_NODE_CMD(EC_NO_ID, "[foo [bar]]");
682         if (node == NULL) {
683                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
684                 return -1;
685         }
686         testres |= EC_TEST_CHECK_PARSE(node, 0);
687         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
688         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
689         testres |= EC_TEST_CHECK_PARSE(node, 0, "x");
690         ec_node_free(node);
691
692         return testres;
693 }
694 /* LCOV_EXCL_STOP */
695
696 static struct ec_test ec_node_cmd_test = {
697         .name = "node_cmd",
698         .test = ec_node_cmd_testcase,
699 };
700
701 EC_TEST_REGISTER(ec_node_cmd_test);