dcaaa9366549bed8034482578f34c54bc4055bad
[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);
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 (!strcmp(ec_strvec_val(vec, 0), "|")) {
172                 out = EC_NODE_OR(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                 if (!strcmp(in2->type->name, "subset")) {
180                         if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
181                                 return -EINVAL;
182                         ec_node_free(in1);
183                         *result = in2;
184                 } else {
185                         out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
186                                         ec_node_clone(in2));
187                         if (out == NULL)
188                                 return -EINVAL;
189                         ec_node_free(in1);
190                         ec_node_free(in2);
191                         *result = out;
192                 }
193         } else {
194                 return -EINVAL;
195         }
196
197         return 0;
198 }
199
200 static int
201 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
202         const struct ec_parsed *open_paren,
203         const struct ec_parsed *close_paren,
204         void *value)
205 {
206         const struct ec_strvec *vec;
207         struct ec_node *in = value;;
208         struct ec_node *out = NULL;;
209
210         (void)userctx;
211         (void)close_paren;
212
213         /* get parsed string vector, it should contain only one str */
214         vec = ec_parsed_strvec(open_paren);
215         if (ec_strvec_len(vec) != 1)
216                 return -EINVAL;
217
218         if (!strcmp(ec_strvec_val(vec, 0), "[")) {
219                 out = ec_node_option(EC_NO_ID, ec_node_clone(in));
220                 if (out == NULL)
221                         return -EINVAL;
222                 ec_node_free(in);
223         } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
224                 out = in;
225         } else {
226                 return -EINVAL;
227         }
228
229         *result = out;
230
231         return 0;
232 }
233
234 static void
235 ec_node_cmd_eval_free(void *result, void *userctx)
236 {
237         (void)userctx;
238         ec_free(result);
239 }
240
241 static const struct ec_node_expr_eval_ops test_ops = {
242         .eval_var = ec_node_cmd_eval_var,
243         .eval_pre_op = ec_node_cmd_eval_pre_op,
244         .eval_post_op = ec_node_cmd_eval_post_op,
245         .eval_bin_op = ec_node_cmd_eval_bin_op,
246         .eval_parenthesis = ec_node_cmd_eval_parenthesis,
247         .eval_free = ec_node_cmd_eval_free,
248 };
249
250 static int
251 ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state,
252                 const struct ec_strvec *strvec)
253 {
254         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
255
256         return ec_node_parse_child(node->cmd, state, strvec);
257 }
258
259 static int
260 ec_node_cmd_complete(const struct ec_node *gen_node,
261                 struct ec_completed *completed,
262                 const struct ec_strvec *strvec)
263 {
264         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
265
266         return ec_node_complete_child(node->cmd, completed, strvec);
267 }
268
269 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
270 {
271         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
272         unsigned int i;
273
274         ec_free(node->cmd_str);
275         ec_node_free(node->cmd);
276         ec_node_free(node->expr);
277         ec_node_free(node->lex);
278         for (i = 0; i < node->len; i++)
279                 ec_node_free(node->table[i]);
280         ec_free(node->table);
281 }
282
283 static int ec_node_cmd_build(struct ec_node *gen_node)
284 {
285         struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL;
286         struct ec_parsed *p, *child;
287         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
288         void *result;
289         int ret;
290
291         /* XXX the expr parser can be moved in the node init */
292
293         /* build the expression parser */
294         ret = -ENOMEM;
295         expr = ec_node("expr", "expr");
296         if (expr == NULL)
297                 goto fail;
298         ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID, "[a-zA-Z0-9]+"));
299         if (ret < 0)
300                 goto fail;
301         ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, ","));
302         if (ret < 0)
303                 goto fail;
304         ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, "|"));
305         if (ret < 0)
306                 goto fail;
307         ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "+"));
308         if (ret < 0)
309                 goto fail;
310         ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "*"));
311         if (ret < 0)
312                 goto fail;
313         ret = ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "["),
314                 ec_node_str(EC_NO_ID, "]"));
315         if (ret < 0)
316                 goto fail;
317         ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "("),
318                 ec_node_str(EC_NO_ID, ")"));
319         if (ret < 0)
320                 goto fail;
321
322         /* prepend a lexer and a "many" to the expression node */
323         ret = -ENOMEM;
324         lex = ec_node_re_lex(EC_NO_ID,
325                 ec_node_many(EC_NO_ID, ec_node_clone(expr), 1, 0));
326         if (lex == NULL)
327                 goto fail;
328
329         ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
330         if (ret < 0)
331                 goto fail;
332         ret = ec_node_re_lex_add(lex, "[*|,()]", 1);
333         if (ret < 0)
334                 goto fail;
335         ret = ec_node_re_lex_add(lex, "\\[", 1);
336         if (ret < 0)
337                 goto fail;
338         ret = ec_node_re_lex_add(lex, "\\]", 1);
339         if (ret < 0)
340                 goto fail;
341         ret = ec_node_re_lex_add(lex, "[         ]+", 0);
342         if (ret < 0)
343                 goto fail;
344
345         /* parse the command expression */
346         ret = -ENOMEM;
347         p = ec_node_parse(lex, node->cmd_str);
348         if (p == NULL)
349                 goto fail;
350
351         ret = -EINVAL;
352         if (!ec_parsed_matches(p))
353                 goto fail;
354         if (!ec_parsed_has_child(p))
355                 goto fail;
356         if (!ec_parsed_has_child(ec_parsed_get_first_child(p)))
357                 goto fail;
358
359         ret = -ENOMEM;
360         cmd = ec_node("seq", EC_NO_ID);
361         if (cmd == NULL)
362                 goto fail;
363
364         EC_PARSED_FOREACH_CHILD(child, ec_parsed_get_first_child(p)) {
365                 ret = ec_node_expr_eval(&result, expr, child,
366                         &test_ops, node);
367                 if (ret < 0)
368                         goto fail;
369                 ret = ec_node_seq_add(cmd, result);
370                 if (ret < 0)
371                         goto fail;
372         }
373         ec_parsed_free(p);
374         p = NULL;
375
376         ec_node_free(node->expr);
377         node->expr = expr;
378         ec_node_free(node->lex);
379         node->lex = lex;
380         ec_node_free(node->cmd);
381         node->cmd = cmd;
382
383         return 0;
384
385 fail:
386         ec_parsed_free(p);
387         ec_node_free(expr);
388         ec_node_free(lex);
389         ec_node_free(cmd);
390         return ret;
391 }
392
393 static struct ec_node_type ec_node_cmd_type = {
394         .name = "cmd",
395         .build = ec_node_cmd_build,
396         .parse = ec_node_cmd_parse,
397         .complete = ec_node_cmd_complete,
398         .size = sizeof(struct ec_node_cmd),
399         .free_priv = ec_node_cmd_free_priv,
400 };
401
402 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
403
404 int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
405 {
406         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
407         struct ec_node **table;
408
409         // XXX check node type
410
411         assert(node != NULL);
412
413         if (child == NULL)
414                 return -EINVAL;
415
416         gen_node->flags &= ~EC_NODE_F_BUILT;
417
418         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
419         if (table == NULL) {
420                 ec_node_free(child);
421                 return -ENOMEM;
422         }
423
424         node->table = table;
425         table[node->len] = child;
426         node->len++;
427
428         child->parent = gen_node;
429         TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
430
431         return 0;
432 }
433
434 struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
435 {
436         struct ec_node *gen_node = NULL;
437         struct ec_node_cmd *node = NULL;
438
439         gen_node = __ec_node(&ec_node_cmd_type, id);
440         if (gen_node == NULL)
441                 goto fail;
442
443         node = (struct ec_node_cmd *)gen_node;
444         node->cmd_str = ec_strdup(cmd_str);
445         if (node->cmd_str == NULL)
446                 goto fail;
447
448         return gen_node;
449
450 fail:
451         ec_node_free(gen_node);
452         return NULL;
453 }
454
455 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
456 {
457         struct ec_node *gen_node = NULL;
458         struct ec_node_cmd *node = NULL;
459         struct ec_node *child;
460         va_list ap;
461         int fail = 0;
462
463         va_start(ap, cmd);
464
465         gen_node = ec_node_cmd(id, cmd);
466         node = (struct ec_node_cmd *)gen_node;
467         if (node == NULL)
468                 fail = 1;;
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         if (fail == 1)
483                 goto fail;
484
485         va_end(ap);
486         return gen_node;
487
488 fail:
489         ec_node_free(gen_node); /* will also free children */
490         va_end(ap);
491         return NULL;
492 }
493
494 /* LCOV_EXCL_START */
495 static int ec_node_cmd_testcase(void)
496 {
497         struct ec_node *node;
498         int ret = 0;
499
500         node = EC_NODE_CMD(EC_NO_ID,
501                 "command [option] (subset1, subset2, subset3) x|y z*",
502                 ec_node_int("x", 0, 10, 10),
503                 ec_node_int("y", 20, 30, 10)
504         );
505         if (node == NULL) {
506                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
507                 return -1;
508         }
509         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
510         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
511         ret |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
512         ret |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
513                                 "z", "z");
514         ret |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
515         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
516         ec_node_free(node);
517
518         node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
519                         ec_node_int("count", 0, 10, 10));
520         if (node == NULL) {
521                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
522                 return -1;
523         }
524         ret |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
525
526         ret |= EC_TEST_CHECK_COMPLETE(node,
527                 "", EC_NODE_ENDLIST,
528                 "good", EC_NODE_ENDLIST);
529         ret |= EC_TEST_CHECK_COMPLETE(node,
530                 "g", EC_NODE_ENDLIST,
531                 "good", EC_NODE_ENDLIST);
532         ret |= EC_TEST_CHECK_COMPLETE(node,
533                 "good", "morning", "", EC_NODE_ENDLIST,
534                 "bob", "bobby", "michael", EC_NODE_ENDLIST);
535
536         ec_node_free(node);
537
538         return ret;
539 }
540 /* LCOV_EXCL_STOP */
541
542 static struct ec_test ec_node_cmd_test = {
543         .name = "node_cmd",
544         .test = ec_node_cmd_testcase,
545 };
546
547 EC_TEST_REGISTER(ec_node_cmd_test);