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