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