829c8966ad583dccee8b54cbc40f7d43bfa10975
[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_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_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_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_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         if (!ec_pnode_has_child(p)) {
375                 errno = EINVAL;
376                 goto fail;
377         }
378
379         ret = ec_node_expr_eval(&result, ec_node_cmd_expr,
380                                 ec_pnode_get_first_child(p),
381                                 &expr_ops, &ctx);
382         if (ret < 0)
383                 goto fail;
384
385         ec_pnode_free(p);
386         return result;
387
388 fail:
389         ec_pnode_free(p);
390         return NULL;
391 }
392
393 static int
394 ec_node_cmd_parse(const struct ec_node *node, struct ec_pnode *state,
395                 const struct ec_strvec *strvec)
396 {
397         struct ec_node_cmd *priv = ec_node_priv(node);
398
399         return ec_parse_child(priv->cmd, state, strvec);
400 }
401
402 static int
403 ec_node_cmd_complete(const struct ec_node *node,
404                 struct ec_comp *comp,
405                 const struct ec_strvec *strvec)
406 {
407         struct ec_node_cmd *priv = ec_node_priv(node);
408
409         return ec_complete_child(priv->cmd, comp, strvec);
410 }
411
412 static void ec_node_cmd_free_priv(struct ec_node *node)
413 {
414         struct ec_node_cmd *priv = ec_node_priv(node);
415         size_t i;
416
417         ec_free(priv->cmd_str);
418         priv->cmd_str = NULL;
419         ec_node_free(priv->cmd);
420         priv->cmd = NULL;
421         for (i = 0; i < priv->len; i++)
422                 ec_node_free(priv->table[i]);
423         ec_free(priv->table);
424         priv->table = NULL;
425         priv->len = 0;
426 }
427
428 static const struct ec_config_schema ec_node_cmd_subschema[] = {
429         {
430                 .desc = "A child node whose id is referenced in the expression.",
431                 .type = EC_CONFIG_TYPE_NODE,
432         },
433         {
434                 .type = EC_CONFIG_TYPE_NONE,
435         },
436 };
437
438 static const struct ec_config_schema ec_node_cmd_schema[] = {
439         {
440                 .key = "expr",
441                 .desc = "The expression to match. Supported operators "
442                 "are or '|', list ',', many '+', many-or-zero '*', "
443                 "option '[]', group '()'. An identifier (alphanumeric) can "
444                 "reference a node whose node_id matches. Else it is "
445                 "interpreted as ec_node_str() matching this string. "
446                 "Example: command [option] (subset1, subset2) x|y",
447                 .type = EC_CONFIG_TYPE_STRING,
448         },
449         {
450                 .key = "children",
451                 .desc = "The list of children nodes.",
452                 .type = EC_CONFIG_TYPE_LIST,
453                 .subschema = ec_node_cmd_subschema,
454         },
455         {
456                 .type = EC_CONFIG_TYPE_NONE,
457         },
458 };
459
460 static int ec_node_cmd_set_config(struct ec_node *node,
461                                 const struct ec_config *config)
462 {
463         struct ec_node_cmd *priv = ec_node_priv(node);
464         const struct ec_config *expr = NULL;
465         struct ec_node *cmd = NULL;
466         struct ec_node **table = NULL;
467         char *cmd_str = NULL;
468         size_t len = 0, i;
469
470         /* retrieve config locally */
471         expr = ec_config_dict_get(config, "expr");
472         if (expr == NULL) {
473                 errno = EINVAL;
474                 goto fail;
475         }
476
477         table = ec_node_config_node_list_to_table(
478                 ec_config_dict_get(config, "children"), &len);
479         if (table == NULL)
480                 goto fail;
481
482         cmd_str = ec_strdup(expr->string);
483         if (cmd_str == NULL)
484                 goto fail;
485
486         /* parse expression to build the cmd child node */
487         cmd = ec_node_cmd_build(cmd_str, table, len);
488         if (cmd == NULL)
489                 goto fail;
490
491         /* ok, store the config */
492         ec_node_free(priv->cmd);
493         priv->cmd = cmd;
494         ec_free(priv->cmd_str);
495         priv->cmd_str = cmd_str;
496         for (i = 0; i < priv->len; i++)
497                 ec_node_free(priv->table[i]);
498         ec_free(priv->table);
499         priv->table = table;
500         priv->len = len;
501
502         return 0;
503
504 fail:
505         for (i = 0; i < len; i++)
506                 ec_node_free(table[i]);
507         ec_free(table);
508         ec_free(cmd_str);
509         ec_node_free(cmd);
510         return -1;
511 }
512
513 static size_t
514 ec_node_cmd_get_children_count(const struct ec_node *node)
515 {
516         struct ec_node_cmd *priv = ec_node_priv(node);
517
518         if (priv->cmd == NULL)
519                 return 0;
520         return 1;
521 }
522
523 static int
524 ec_node_cmd_get_child(const struct ec_node *node, size_t i,
525                 struct ec_node **child, unsigned int *refs)
526 {
527         struct ec_node_cmd *priv = ec_node_priv(node);
528
529         if (i > 0)
530                 return -1;
531
532         *child = priv->cmd;
533         *refs = 1;
534         return 0;
535 }
536
537 static struct ec_node_type ec_node_cmd_type = {
538         .name = "cmd",
539         .schema = ec_node_cmd_schema,
540         .set_config = ec_node_cmd_set_config,
541         .parse = ec_node_cmd_parse,
542         .complete = ec_node_cmd_complete,
543         .size = sizeof(struct ec_node_cmd),
544         .free_priv = ec_node_cmd_free_priv,
545         .get_children_count = ec_node_cmd_get_children_count,
546         .get_child = ec_node_cmd_get_child,
547 };
548
549 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
550
551 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
552 {
553         struct ec_config *config = NULL, *children = NULL;
554         struct ec_node *node = NULL;
555         va_list ap;
556         int ret;
557
558         /* this block must stay first, it frees the nodes on error */
559         va_start(ap, cmd);
560         children = ec_node_config_node_list_from_vargs(ap);
561         va_end(ap);
562         if (children == NULL)
563                 goto fail;
564
565         node = ec_node_from_type(&ec_node_cmd_type, id);
566         if (node == NULL)
567                 goto fail;
568
569         config = ec_config_dict();
570         if (config == NULL)
571                 goto fail;
572
573         if (ec_config_dict_set(config, "expr", ec_config_string(cmd)) < 0)
574                 goto fail;
575
576         if (ec_config_dict_set(config, "children", children) < 0) {
577                 children = NULL; /* freed */
578                 goto fail;
579         }
580         children = NULL;
581
582         ret = ec_node_set_config(node, config);
583         config = NULL; /* freed */
584         if (ret < 0)
585                 goto fail;
586
587         return node;
588
589 fail:
590         ec_node_free(node); /* will also free added children */
591         ec_config_free(children);
592         ec_config_free(config);
593
594         return NULL;
595 }
596
597 static int ec_node_cmd_init_func(void)
598 {
599         ec_node_cmd_expr = ec_node_cmd_build_expr();
600         if (ec_node_cmd_expr == NULL)
601                 goto fail;
602
603         ec_node_cmd_parser = ec_node_cmd_build_parser(ec_node_cmd_expr);
604         if (ec_node_cmd_parser == NULL)
605                 goto fail;
606
607         return 0;
608
609 fail:
610         EC_LOG(EC_LOG_ERR, "Failed to initialize command parser\n");
611         ec_node_free(ec_node_cmd_expr);
612         ec_node_cmd_expr = NULL;
613         ec_node_free(ec_node_cmd_parser);
614         ec_node_cmd_parser = NULL;
615         return -1;
616 }
617
618 static void ec_node_cmd_exit_func(void)
619 {
620         ec_node_free(ec_node_cmd_expr);
621         ec_node_cmd_expr = NULL;
622         ec_node_free(ec_node_cmd_parser);
623         ec_node_cmd_parser = NULL;
624 }
625
626 static struct ec_init ec_node_cmd_init = {
627         .init = ec_node_cmd_init_func,
628         .exit = ec_node_cmd_exit_func,
629         .priority = 75,
630 };
631
632 EC_INIT_REGISTER(ec_node_cmd_init);
633
634 /* LCOV_EXCL_START */
635 static int ec_node_cmd_testcase(void)
636 {
637         struct ec_node *node;
638         int testres = 0;
639
640         node = EC_NODE_CMD(EC_NO_ID,
641                 "command [option] (subset1, subset2, subset3, subset4) x|y z*",
642                 ec_node_int("x", 0, 10, 10),
643                 ec_node_int("y", 20, 30, 10)
644         );
645         if (node == NULL) {
646                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
647                 return -1;
648         }
649         testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
650         testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "subset1", "1");
651         testres |= EC_TEST_CHECK_PARSE(node, 4, "command", "subset3", "subset2",
652                                 "1");
653         testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "subset2", "subset3",
654                                 "subset1", "1");
655         testres |= EC_TEST_CHECK_PARSE(node, 6, "command", "subset3", "subset1",
656                                 "subset4", "subset2", "4");
657         testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
658         testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
659         testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
660                                 "z", "z");
661         testres |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
662         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
663         ec_node_free(node);
664
665         node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
666                         ec_node_int("count", 0, 10, 10));
667         if (node == NULL) {
668                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
669                 return -1;
670         }
671         testres |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
672
673         testres |= EC_TEST_CHECK_COMPLETE(node,
674                 "", EC_VA_END,
675                 "good", EC_VA_END);
676         testres |= EC_TEST_CHECK_COMPLETE(node,
677                 "g", EC_VA_END,
678                 "good", EC_VA_END);
679         testres |= EC_TEST_CHECK_COMPLETE(node,
680                 "good", "morning", "", EC_VA_END,
681                 "bob", "bobby", "michael", EC_VA_END);
682
683         ec_node_free(node);
684
685         node = EC_NODE_CMD(EC_NO_ID, "[foo [bar]]");
686         if (node == NULL) {
687                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
688                 return -1;
689         }
690         testres |= EC_TEST_CHECK_PARSE(node, 0);
691         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
692         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
693         testres |= EC_TEST_CHECK_PARSE(node, 0, "x");
694         ec_node_free(node);
695
696         return testres;
697 }
698 /* LCOV_EXCL_STOP */
699
700 static struct ec_test ec_node_cmd_test = {
701         .name = "node_cmd",
702         .test = ec_node_cmd_testcase,
703 };
704
705 EC_TEST_REGISTER(ec_node_cmd_test);