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