parsed -> parse
[protos/libecoli.git] / lib / 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_parse.h>
20 #include <ecoli_complete.h>
21 #include <ecoli_node_expr.h>
22 #include <ecoli_node_str.h>
23 #include <ecoli_node_or.h>
24 #include <ecoli_node_subset.h>
25 #include <ecoli_node_int.h>
26 #include <ecoli_node_many.h>
27 #include <ecoli_node_seq.h>
28 #include <ecoli_node_option.h>
29 #include <ecoli_node_re.h>
30 #include <ecoli_node_re_lex.h>
31 #include <ecoli_node_cmd.h>
32
33 EC_LOG_TYPE_REGISTER(node_cmd);
34
35 struct ec_node_cmd {
36         struct ec_node gen;
37         char *cmd_str;           /* the command string. */
38         struct ec_node *cmd;       /* the command node. */
39         struct ec_node *lex;       /* the lexer node. */
40         struct ec_node *expr;      /* the expression parser. */
41         struct ec_node **table;    /* table of node referenced in command. */
42         unsigned int len;        /* len of the table. */
43 };
44
45 static int
46 ec_node_cmd_eval_var(void **result, void *userctx,
47         const struct ec_parse *var)
48 {
49         const struct ec_strvec *vec;
50         struct ec_node_cmd *node = userctx;
51         struct ec_node *eval = NULL;
52         const char *str, *id;
53         unsigned int i;
54
55         (void)userctx;
56
57         /* get parsed string vector, it should contain only one str */
58         vec = ec_parse_strvec(var);
59         if (ec_strvec_len(vec) != 1)
60                 return -EINVAL;
61         str = ec_strvec_val(vec, 0);
62
63         for (i = 0; i < node->len; i++) {
64                 id = ec_node_id(node->table[i]);
65                 if (id == NULL)
66                         continue;
67                 if (strcmp(str, id))
68                         continue;
69                 /* if id matches, use a node provided by the user... */
70                 eval = ec_node_clone(node->table[i]);
71                 if (eval == NULL)
72                         return -ENOMEM;
73                 break;
74         }
75
76         /* ...or create a string node */
77         if (eval == NULL) {
78                 eval = ec_node_str(EC_NO_ID, str);
79                 if (eval == NULL)
80                         return -ENOMEM;
81         }
82
83         *result = eval;
84
85         return 0;
86 }
87
88 static int
89 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
90         const struct ec_parse *operator)
91 {
92         (void)result;
93         (void)userctx;
94         (void)operand;
95         (void)operator;
96
97         return -EINVAL;
98 }
99
100 static int
101 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
102         const struct ec_parse *operator)
103 {
104         const struct ec_strvec *vec;
105         struct ec_node *in = operand;;
106         struct ec_node *out = NULL;;
107
108         (void)userctx;
109
110         /* get parsed string vector, it should contain only one str */
111         vec = ec_parse_strvec(operator);
112         if (ec_strvec_len(vec) != 1)
113                 return -EINVAL;
114
115         if (!strcmp(ec_strvec_val(vec, 0), "*")) {
116                 out = ec_node_many(EC_NO_ID,
117                                 ec_node_clone(in), 0, 0);
118                 if (out == NULL)
119                         return -EINVAL;
120                 ec_node_free(in);
121                 *result = out;
122         } else {
123                 return -EINVAL;
124         }
125
126         return 0;
127 }
128
129 static int
130 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
131         const struct ec_parse *operator, void *operand2)
132
133 {
134         const struct ec_strvec *vec;
135         struct ec_node *out = NULL;
136         struct ec_node *in1 = operand1;
137         struct ec_node *in2 = operand2;
138
139         (void)userctx;
140
141         /* get parsed string vector, it should contain only one str */
142         vec = ec_parse_strvec(operator);
143         if (ec_strvec_len(vec) > 1)
144                 return -EINVAL;
145
146         if (ec_strvec_len(vec) == 0) {
147                 if (!strcmp(in1->type->name, "seq")) {
148                         if (ec_node_seq_add(in1, ec_node_clone(in2)) < 0)
149                                 return -EINVAL;
150                         ec_node_free(in2);
151                         *result = in1;
152                 } else {
153                         out = EC_NODE_SEQ(EC_NO_ID, ec_node_clone(in1),
154                                         ec_node_clone(in2));
155                         if (out == NULL)
156                                 return -EINVAL;
157                         ec_node_free(in1);
158                         ec_node_free(in2);
159                         *result = out;
160                 }
161         } else if (!strcmp(ec_strvec_val(vec, 0), "|")) {
162                 if (!strcmp(in2->type->name, "or")) {
163                         if (ec_node_or_add(in2, ec_node_clone(in1)) < 0)
164                                 return -EINVAL;
165                         ec_node_free(in1);
166                         *result = in2;
167                 } else if (!strcmp(in1->type->name, "or")) {
168                         if (ec_node_or_add(in1, ec_node_clone(in2)) < 0)
169                                 return -EINVAL;
170                         ec_node_free(in2);
171                         *result = in1;
172                 } else {
173                         out = EC_NODE_OR(EC_NO_ID, ec_node_clone(in1),
174                                         ec_node_clone(in2));
175                         if (out == NULL)
176                                 return -EINVAL;
177                         ec_node_free(in1);
178                         ec_node_free(in2);
179                         *result = out;
180                 }
181         } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
182                 if (!strcmp(in2->type->name, "subset")) {
183                         if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
184                                 return -EINVAL;
185                         ec_node_free(in1);
186                         *result = in2;
187                 } else if (!strcmp(in1->type->name, "subset")) {
188                         if (ec_node_subset_add(in1, ec_node_clone(in2)) < 0)
189                                 return -EINVAL;
190                         ec_node_free(in2);
191                         *result = in1;
192                 } else {
193                         out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
194                                         ec_node_clone(in2));
195                         if (out == NULL)
196                                 return -EINVAL;
197                         ec_node_free(in1);
198                         ec_node_free(in2);
199                         *result = out;
200                 }
201         } else {
202                 return -EINVAL;
203         }
204
205         return 0;
206 }
207
208 static int
209 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
210         const struct ec_parse *open_paren,
211         const struct ec_parse *close_paren,
212         void *value)
213 {
214         const struct ec_strvec *vec;
215         struct ec_node *in = value;;
216         struct ec_node *out = NULL;;
217
218         (void)userctx;
219         (void)close_paren;
220
221         /* get parsed string vector, it should contain only one str */
222         vec = ec_parse_strvec(open_paren);
223         if (ec_strvec_len(vec) != 1)
224                 return -EINVAL;
225
226         if (!strcmp(ec_strvec_val(vec, 0), "[")) {
227                 out = ec_node_option(EC_NO_ID, ec_node_clone(in));
228                 if (out == NULL)
229                         return -EINVAL;
230                 ec_node_free(in);
231         } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
232                 out = in;
233         } else {
234                 return -EINVAL;
235         }
236
237         *result = out;
238
239         return 0;
240 }
241
242 static void
243 ec_node_cmd_eval_free(void *result, void *userctx)
244 {
245         (void)userctx;
246         ec_free(result);
247 }
248
249 static const struct ec_node_expr_eval_ops test_ops = {
250         .eval_var = ec_node_cmd_eval_var,
251         .eval_pre_op = ec_node_cmd_eval_pre_op,
252         .eval_post_op = ec_node_cmd_eval_post_op,
253         .eval_bin_op = ec_node_cmd_eval_bin_op,
254         .eval_parenthesis = ec_node_cmd_eval_parenthesis,
255         .eval_free = ec_node_cmd_eval_free,
256 };
257
258 static int ec_node_cmd_build(struct ec_node_cmd *node)
259 {
260         struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL;
261         struct ec_parse *p = NULL;
262         void *result;
263         int ret;
264
265         ec_node_free(node->expr);
266         node->expr = NULL;
267         ec_node_free(node->lex);
268         node->lex = NULL;
269         ec_node_free(node->cmd);
270         node->cmd = NULL;
271
272         /* build the expression parser */
273         ret = -ENOMEM;
274         expr = ec_node("expr", "expr");
275         if (expr == NULL)
276                 goto fail;
277         ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID, "[a-zA-Z0-9]+"));
278         if (ret < 0)
279                 goto fail;
280         ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, ","));
281         if (ret < 0)
282                 goto fail;
283         ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, "|"));
284         if (ret < 0)
285                 goto fail;
286         ret = ec_node_expr_add_bin_op(expr, ec_node("empty", EC_NO_ID));
287         if (ret < 0)
288                 goto fail;
289         ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "+"));
290         if (ret < 0)
291                 goto fail;
292         ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "*"));
293         if (ret < 0)
294                 goto fail;
295         ret = ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "["),
296                 ec_node_str(EC_NO_ID, "]"));
297         if (ret < 0)
298                 goto fail;
299         ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "("),
300                 ec_node_str(EC_NO_ID, ")"));
301         if (ret < 0)
302                 goto fail;
303
304         /* prepend a lexer to the expression node */
305         ret = -ENOMEM;
306         lex = ec_node_re_lex(EC_NO_ID, ec_node_clone(expr));
307         if (lex == NULL)
308                 goto fail;
309
310         ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
311         if (ret < 0)
312                 goto fail;
313         ret = ec_node_re_lex_add(lex, "[*|,()]", 1);
314         if (ret < 0)
315                 goto fail;
316         ret = ec_node_re_lex_add(lex, "\\[", 1);
317         if (ret < 0)
318                 goto fail;
319         ret = ec_node_re_lex_add(lex, "\\]", 1);
320         if (ret < 0)
321                 goto fail;
322         ret = ec_node_re_lex_add(lex, "[         ]+", 0);
323         if (ret < 0)
324                 goto fail;
325
326         /* parse the command expression */
327         ret = -ENOMEM;
328         p = ec_node_parse(lex, node->cmd_str);
329         if (p == NULL)
330                 goto fail;
331
332         ret = -EINVAL;
333         if (!ec_parse_matches(p))
334                 goto fail;
335         if (!ec_parse_has_child(p))
336                 goto fail;
337
338         ret = ec_node_expr_eval(&result, expr, ec_parse_get_first_child(p),
339                                 &test_ops, node);
340         if (ret < 0)
341                 goto fail;
342
343         ec_parse_free(p);
344         p = NULL;
345
346         node->expr = expr;
347         node->lex = lex;
348         node->cmd = result;
349
350         return 0;
351
352 fail:
353         ec_parse_free(p);
354         ec_node_free(expr);
355         ec_node_free(lex);
356         ec_node_free(cmd);
357         return ret;
358 }
359
360 static int
361 ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parse *state,
362                 const struct ec_strvec *strvec)
363 {
364         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
365
366         if (node->cmd == NULL)
367                 return -ENOENT;
368         return ec_node_parse_child(node->cmd, state, strvec);
369 }
370
371 static int
372 ec_node_cmd_complete(const struct ec_node *gen_node,
373                 struct ec_comp *comp,
374                 const struct ec_strvec *strvec)
375 {
376         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
377
378         if (node->cmd == NULL)
379                 return -ENOENT;
380         return ec_node_complete_child(node->cmd, comp, strvec);
381 }
382
383 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
384 {
385         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
386         unsigned int i;
387
388         ec_free(node->cmd_str);
389         ec_node_free(node->cmd);
390         ec_node_free(node->expr);
391         ec_node_free(node->lex);
392         for (i = 0; i < node->len; i++)
393                 ec_node_free(node->table[i]);
394         ec_free(node->table);
395 }
396
397
398 static struct ec_node_type ec_node_cmd_type = {
399         .name = "cmd",
400         .parse = ec_node_cmd_parse,
401         .complete = ec_node_cmd_complete,
402         .size = sizeof(struct ec_node_cmd),
403         .free_priv = ec_node_cmd_free_priv,
404 };
405
406 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
407
408 int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
409 {
410         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
411         struct ec_node **table;
412         int ret;
413
414         assert(node != NULL);
415
416         if (child == NULL) {
417                 errno = EINVAL;
418                 goto fail;
419         }
420
421         if (ec_node_check_type(gen_node, &ec_node_cmd_type) < 0)
422                 goto fail;
423
424         if (node->cmd == NULL) {
425                 ret = ec_node_cmd_build(node);
426                 if (ret < 0)
427                         return ret;
428         }
429
430         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
431         if (table == NULL)
432                 goto fail;
433
434         node->table = table;
435
436         if (ec_node_add_child(gen_node, child) < 0)
437                 goto fail;
438
439         table[node->len] = child;
440         node->len++;
441
442         return 0;
443
444 fail:
445         ec_node_free(child);
446         return -1;
447 }
448
449 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
450 {
451         struct ec_node *gen_node = NULL;
452         struct ec_node_cmd *node = NULL;
453         struct ec_node *child;
454         va_list ap;
455         int fail = 0;
456
457         gen_node = __ec_node(&ec_node_cmd_type, id);
458         if (gen_node == NULL)
459                 fail = 1;
460
461         if (fail == 0) {
462                 node = (struct ec_node_cmd *)gen_node;
463                 node->cmd_str = ec_strdup(cmd);
464                 if (node->cmd_str == NULL)
465                         fail = 1;
466         }
467
468         va_start(ap, cmd);
469
470         for (child = va_arg(ap, struct ec_node *);
471              child != EC_NODE_ENDLIST;
472              child = va_arg(ap, struct ec_node *)) {
473
474                 /* on error, don't quit the loop to avoid leaks */
475                 if (fail == 1 || child == NULL ||
476                                 ec_node_cmd_add_child(&node->gen, child) < 0) {
477                         fail = 1;
478                         ec_node_free(child);
479                 }
480         }
481
482         va_end(ap);
483
484         if (fail == 1)
485                 goto fail;
486
487         if (ec_node_cmd_build(node) < 0)
488                 goto fail;
489
490         return gen_node;
491
492 fail:
493         ec_node_free(gen_node); /* will also free children */
494         return NULL;
495 }
496
497 struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
498 {
499         return __ec_node_cmd(id, cmd_str, EC_NODE_ENDLIST);
500 }
501
502 /* LCOV_EXCL_START */
503 static int ec_node_cmd_testcase(void)
504 {
505         struct ec_node *node;
506         int testres = 0;
507
508         node = EC_NODE_CMD(EC_NO_ID,
509                 "command [option] (subset1, subset2, subset3, subset4) x|y z*",
510                 ec_node_int("x", 0, 10, 10),
511                 ec_node_int("y", 20, 30, 10)
512         );
513         if (node == NULL) {
514                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
515                 return -1;
516         }
517         testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
518         testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "subset1", "1");
519         testres |= EC_TEST_CHECK_PARSE(node, 4, "command", "subset3", "subset2",
520                                 "1");
521         testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "subset2", "subset3",
522                                 "subset1", "1");
523         testres |= EC_TEST_CHECK_PARSE(node, 6, "command", "subset3", "subset1",
524                                 "subset4", "subset2", "4");
525         testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
526         testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
527         testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
528                                 "z", "z");
529         testres |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
530         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
531         ec_node_free(node);
532
533         node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
534                         ec_node_int("count", 0, 10, 10));
535         if (node == NULL) {
536                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
537                 return -1;
538         }
539         testres |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
540
541         testres |= EC_TEST_CHECK_COMPLETE(node,
542                 "", EC_NODE_ENDLIST,
543                 "good", EC_NODE_ENDLIST);
544         testres |= EC_TEST_CHECK_COMPLETE(node,
545                 "g", EC_NODE_ENDLIST,
546                 "good", EC_NODE_ENDLIST);
547         testres |= EC_TEST_CHECK_COMPLETE(node,
548                 "good", "morning", "", EC_NODE_ENDLIST,
549                 "bob", "bobby", "michael", EC_NODE_ENDLIST);
550
551         ec_node_free(node);
552
553         node = EC_NODE_CMD(EC_NO_ID, "[foo [bar]]");
554         if (node == NULL) {
555                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
556                 return -1;
557         }
558         testres |= EC_TEST_CHECK_PARSE(node, 0);
559         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
560         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
561         testres |= EC_TEST_CHECK_PARSE(node, 0, "x");
562         ec_node_free(node);
563
564         return testres;
565 }
566 /* LCOV_EXCL_STOP */
567
568 static struct ec_test ec_node_cmd_test = {
569         .name = "node_cmd",
570         .test = ec_node_cmd_testcase,
571 };
572
573 EC_TEST_REGISTER(ec_node_cmd_test);