9653c0ebf26560e984290ff85d99217fd787ef76
[protos/libecoli.git] / lib / ecoli_node_cmd.c
1 /*
2  * Copyright (c) 2016-2017, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/queue.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <stdarg.h>
34 #include <errno.h>
35 #include <limits.h>
36
37 #include <ecoli_malloc.h>
38 #include <ecoli_log.h>
39 #include <ecoli_test.h>
40 #include <ecoli_strvec.h>
41 #include <ecoli_node.h>
42 #include <ecoli_parsed.h>
43 #include <ecoli_completed.h>
44 #include <ecoli_node_expr.h>
45 #include <ecoli_node_str.h>
46 #include <ecoli_node_or.h>
47 #include <ecoli_node_subset.h>
48 #include <ecoli_node_int.h>
49 #include <ecoli_node_many.h>
50 #include <ecoli_node_seq.h>
51 #include <ecoli_node_option.h>
52 #include <ecoli_node_re.h>
53 #include <ecoli_node_re_lex.h>
54 #include <ecoli_node_cmd.h>
55
56 EC_LOG_TYPE_REGISTER(node_cmd);
57
58 struct ec_node_cmd {
59         struct ec_node gen;
60         char *cmd_str;           /* the command string. */
61         struct ec_node *cmd;       /* the command node. */
62         struct ec_node *lex;       /* the lexer node. */
63         struct ec_node *expr;      /* the expression parser. */
64         struct ec_node **table;    /* table of node referenced in command. */
65         unsigned int len;        /* len of the table. */
66 };
67
68 static int
69 ec_node_cmd_eval_var(void **result, void *userctx,
70         const struct ec_parsed *var)
71 {
72         const struct ec_strvec *vec;
73         struct ec_node_cmd *node = userctx;
74         struct ec_node *eval = NULL;
75         const char *str, *id;
76         unsigned int i;
77
78         (void)userctx;
79
80         /* get parsed string vector, it should contain only one str */
81         vec = ec_parsed_strvec(var);
82         if (ec_strvec_len(vec) != 1)
83                 return -EINVAL;
84         str = ec_strvec_val(vec, 0);
85
86         for (i = 0; i < node->len; i++) {
87                 id = ec_node_id(node->table[i]);
88                 if (id == NULL)
89                         continue;
90                 if (strcmp(str, id))
91                         continue;
92                 /* if id matches, use a node provided by the user... */
93                 eval = ec_node_clone(node->table[i]);
94                 if (eval == NULL)
95                         return -ENOMEM;
96                 break;
97         }
98
99         /* ...or create a string node */
100         if (eval == NULL) {
101                 eval = ec_node_str(EC_NO_ID, str);
102                 if (eval == NULL)
103                         return -ENOMEM;
104         }
105
106         *result = eval;
107
108         return 0;
109 }
110
111 static int
112 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
113         const struct ec_parsed *operator)
114 {
115         (void)result;
116         (void)userctx;
117         (void)operand;
118         (void)operator;
119
120         return -EINVAL;
121 }
122
123 static int
124 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
125         const struct ec_parsed *operator)
126 {
127         const struct ec_strvec *vec;
128         struct ec_node *in = operand;;
129         struct ec_node *out = NULL;;
130
131         (void)userctx;
132
133         /* get parsed string vector, it should contain only one str */
134         vec = ec_parsed_strvec(operator);
135         if (ec_strvec_len(vec) != 1)
136                 return -EINVAL;
137
138         if (!strcmp(ec_strvec_val(vec, 0), "*")) {
139                 out = ec_node_many(EC_NO_ID,
140                                 ec_node_clone(in), 0, 0);
141                 if (out == NULL)
142                         return -EINVAL;
143                 ec_node_free(in);
144                 *result = out;
145         } else {
146                 return -EINVAL;
147         }
148
149         return 0;
150 }
151
152 static int
153 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
154         const struct ec_parsed *operator, void *operand2)
155
156 {
157         const struct ec_strvec *vec;
158         struct ec_node *out = NULL;
159         struct ec_node *in1 = operand1;
160         struct ec_node *in2 = operand2;
161
162         (void)userctx;
163
164         /* get parsed string vector, it should contain only one str */
165         vec = ec_parsed_strvec(operator);
166         if (ec_strvec_len(vec) > 1)
167                 return -EINVAL;
168
169         if (ec_strvec_len(vec) == 0) {
170                 out = EC_NODE_SEQ(EC_NO_ID, ec_node_clone(in1), ec_node_clone(in2));
171                 if (out == NULL)
172                         return -EINVAL;
173                 ec_node_free(in1);
174                 ec_node_free(in2);
175                 *result = out;
176         } else if (!strcmp(ec_strvec_val(vec, 0), "|")) {
177                 out = EC_NODE_OR(EC_NO_ID, ec_node_clone(in1), ec_node_clone(in2));
178                 if (out == NULL)
179                         return -EINVAL;
180                 ec_node_free(in1);
181                 ec_node_free(in2);
182                 *result = out;
183         } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
184                 if (!strcmp(in2->type->name, "subset")) {
185                         if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
186                                 return -EINVAL;
187                         ec_node_free(in1);
188                         *result = in2;
189                 } else {
190                         out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
191                                         ec_node_clone(in2));
192                         if (out == NULL)
193                                 return -EINVAL;
194                         ec_node_free(in1);
195                         ec_node_free(in2);
196                         *result = out;
197                 }
198         } else {
199                 return -EINVAL;
200         }
201
202         return 0;
203 }
204
205 static int
206 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
207         const struct ec_parsed *open_paren,
208         const struct ec_parsed *close_paren,
209         void *value)
210 {
211         const struct ec_strvec *vec;
212         struct ec_node *in = value;;
213         struct ec_node *out = NULL;;
214
215         (void)userctx;
216         (void)close_paren;
217
218         /* get parsed string vector, it should contain only one str */
219         vec = ec_parsed_strvec(open_paren);
220         if (ec_strvec_len(vec) != 1)
221                 return -EINVAL;
222
223         if (!strcmp(ec_strvec_val(vec, 0), "[")) {
224                 out = ec_node_option(EC_NO_ID, ec_node_clone(in));
225                 if (out == NULL)
226                         return -EINVAL;
227                 ec_node_free(in);
228         } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
229                 out = in;
230         } else {
231                 return -EINVAL;
232         }
233
234         *result = out;
235
236         return 0;
237 }
238
239 static void
240 ec_node_cmd_eval_free(void *result, void *userctx)
241 {
242         (void)userctx;
243         ec_free(result);
244 }
245
246 static const struct ec_node_expr_eval_ops test_ops = {
247         .eval_var = ec_node_cmd_eval_var,
248         .eval_pre_op = ec_node_cmd_eval_pre_op,
249         .eval_post_op = ec_node_cmd_eval_post_op,
250         .eval_bin_op = ec_node_cmd_eval_bin_op,
251         .eval_parenthesis = ec_node_cmd_eval_parenthesis,
252         .eval_free = ec_node_cmd_eval_free,
253 };
254
255 static int ec_node_cmd_build(struct ec_node_cmd *node)
256 {
257         struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL;
258         struct ec_parsed *p = NULL;
259         void *result;
260         int ret;
261
262         ec_node_free(node->expr);
263         node->expr = NULL;
264         ec_node_free(node->lex);
265         node->lex = NULL;
266         ec_node_free(node->cmd);
267         node->cmd = NULL;
268
269         /* build the expression parser */
270         ret = -ENOMEM;
271         expr = ec_node("expr", "expr");
272         if (expr == NULL)
273                 goto fail;
274         ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID, "[a-zA-Z0-9]+"));
275         if (ret < 0)
276                 goto fail;
277         ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, ","));
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("empty", EC_NO_ID));
284         if (ret < 0)
285                 goto fail;
286         ret = ec_node_expr_add_post_op(expr, ec_node_str(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_parenthesis(expr, ec_node_str(EC_NO_ID, "["),
293                 ec_node_str(EC_NO_ID, "]"));
294         if (ret < 0)
295                 goto fail;
296         ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "("),
297                 ec_node_str(EC_NO_ID, ")"));
298         if (ret < 0)
299                 goto fail;
300
301         /* prepend a lexer to the expression node */
302         ret = -ENOMEM;
303         lex = ec_node_re_lex(EC_NO_ID, ec_node_clone(expr));
304         if (lex == NULL)
305                 goto fail;
306
307         ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
308         if (ret < 0)
309                 goto fail;
310         ret = ec_node_re_lex_add(lex, "[*|,()]", 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, "[         ]+", 0);
320         if (ret < 0)
321                 goto fail;
322
323         /* parse the command expression */
324         ret = -ENOMEM;
325         p = ec_node_parse(lex, node->cmd_str);
326         if (p == NULL)
327                 goto fail;
328
329         ret = -EINVAL;
330         if (!ec_parsed_matches(p))
331                 goto fail;
332         if (!ec_parsed_has_child(p))
333                 goto fail;
334
335         ret = ec_node_expr_eval(&result, expr, ec_parsed_get_first_child(p),
336                                 &test_ops, node);
337         if (ret < 0)
338                 goto fail;
339
340         ec_parsed_free(p);
341         p = NULL;
342
343         node->expr = expr;
344         node->lex = lex;
345         node->cmd = result;
346
347         return 0;
348
349 fail:
350         ec_parsed_free(p);
351         ec_node_free(expr);
352         ec_node_free(lex);
353         ec_node_free(cmd);
354         return ret;
355 }
356
357 static int
358 ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state,
359                 const struct ec_strvec *strvec)
360 {
361         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
362
363         if (node->cmd == NULL)
364                 return -ENOENT;
365         return ec_node_parse_child(node->cmd, state, strvec);
366 }
367
368 static int
369 ec_node_cmd_complete(const struct ec_node *gen_node,
370                 struct ec_completed *completed,
371                 const struct ec_strvec *strvec)
372 {
373         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
374
375         if (node->cmd == NULL)
376                 return -ENOENT;
377         return ec_node_complete_child(node->cmd, completed, strvec);
378 }
379
380 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
381 {
382         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
383         unsigned int i;
384
385         ec_free(node->cmd_str);
386         ec_node_free(node->cmd);
387         ec_node_free(node->expr);
388         ec_node_free(node->lex);
389         for (i = 0; i < node->len; i++)
390                 ec_node_free(node->table[i]);
391         ec_free(node->table);
392 }
393
394
395 static struct ec_node_type ec_node_cmd_type = {
396         .name = "cmd",
397         .parse = ec_node_cmd_parse,
398         .complete = ec_node_cmd_complete,
399         .size = sizeof(struct ec_node_cmd),
400         .free_priv = ec_node_cmd_free_priv,
401 };
402
403 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
404
405 int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
406 {
407         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
408         struct ec_node **table;
409         int ret;
410
411         assert(node != NULL);
412
413         if (child == NULL) {
414                 errno = EINVAL;
415                 goto fail;
416         }
417
418         if (ec_node_check_type(gen_node, &ec_node_cmd_type) < 0)
419                 goto fail;
420
421         if (node->cmd == NULL) {
422                 ret = ec_node_cmd_build(node);
423                 if (ret < 0)
424                         return ret;
425         }
426
427         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
428         if (table == NULL)
429                 goto fail;
430
431         node->table = table;
432
433         if (ec_node_add_child(gen_node, child) < 0)
434                 goto fail;
435
436         table[node->len] = child;
437         node->len++;
438
439         return 0;
440
441 fail:
442         ec_node_free(child);
443         return -1;
444 }
445
446 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
447 {
448         struct ec_node *gen_node = NULL;
449         struct ec_node_cmd *node = NULL;
450         struct ec_node *child;
451         va_list ap;
452         int fail = 0;
453
454         gen_node = __ec_node(&ec_node_cmd_type, id);
455         if (gen_node == NULL)
456                 fail = 1;
457
458         if (fail == 0) {
459                 node = (struct ec_node_cmd *)gen_node;
460                 node->cmd_str = ec_strdup(cmd);
461                 if (node->cmd_str == NULL)
462                         fail = 1;
463         }
464
465         va_start(ap, cmd);
466
467         for (child = va_arg(ap, struct ec_node *);
468              child != EC_NODE_ENDLIST;
469              child = va_arg(ap, struct ec_node *)) {
470
471                 /* on error, don't quit the loop to avoid leaks */
472                 if (fail == 1 || child == NULL ||
473                                 ec_node_cmd_add_child(&node->gen, child) < 0) {
474                         fail = 1;
475                         ec_node_free(child);
476                 }
477         }
478
479         va_end(ap);
480
481         if (fail == 1)
482                 goto fail;
483
484         if (ec_node_cmd_build(node) < 0)
485                 goto fail;
486
487         return gen_node;
488
489 fail:
490         ec_node_free(gen_node); /* will also free children */
491         return NULL;
492 }
493
494 struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
495 {
496         return __ec_node_cmd(id, cmd_str, EC_NODE_ENDLIST);
497 }
498
499 /* LCOV_EXCL_START */
500 static int ec_node_cmd_testcase(void)
501 {
502         struct ec_node *node;
503         int ret = 0;
504
505         node = EC_NODE_CMD(EC_NO_ID,
506                 "command [option] (subset1, subset2, subset3) x|y z*",
507                 ec_node_int("x", 0, 10, 10),
508                 ec_node_int("y", 20, 30, 10)
509         );
510         if (node == NULL) {
511                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
512                 return -1;
513         }
514         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
515         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
516         ret |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
517         ret |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
518                                 "z", "z");
519         ret |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
520         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
521         ec_node_free(node);
522
523         node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
524                         ec_node_int("count", 0, 10, 10));
525         if (node == NULL) {
526                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
527                 return -1;
528         }
529         ret |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
530
531         ret |= EC_TEST_CHECK_COMPLETE(node,
532                 "", EC_NODE_ENDLIST,
533                 "good", EC_NODE_ENDLIST);
534         ret |= EC_TEST_CHECK_COMPLETE(node,
535                 "g", EC_NODE_ENDLIST,
536                 "good", EC_NODE_ENDLIST);
537         ret |= EC_TEST_CHECK_COMPLETE(node,
538                 "good", "morning", "", EC_NODE_ENDLIST,
539                 "bob", "bobby", "michael", EC_NODE_ENDLIST);
540
541         ec_node_free(node);
542
543         node = EC_NODE_CMD(EC_NO_ID, "[foo [bar]]");
544         if (node == NULL) {
545                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
546                 return -1;
547         }
548         ret |= EC_TEST_CHECK_PARSE(node, 0);
549         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo");
550         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
551         ret |= EC_TEST_CHECK_PARSE(node, 0, "x");
552         ec_node_free(node);
553
554         return ret;
555 }
556 /* LCOV_EXCL_STOP */
557
558 static struct ec_test ec_node_cmd_test = {
559         .name = "node_cmd",
560         .test = ec_node_cmd_testcase,
561 };
562
563 EC_TEST_REGISTER(ec_node_cmd_test);