dynamic log types
[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(NULL, 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 *eval = operand;;
131
132         (void)userctx;
133
134         /* get parsed string vector, it should contain only one str */
135         vec = ec_parsed_strvec(operator);
136         if (ec_strvec_len(vec) != 1)
137                 return -EINVAL;
138
139         if (!strcmp(ec_strvec_val(vec, 0), "*"))
140                 eval = NULL; //XXX
141         else
142                 return -EINVAL;
143
144         printf("eval post_op %p\n", eval);
145         *result = eval;
146
147         return 0;
148 }
149
150 static int
151 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
152         const struct ec_parsed *operator, void *operand2)
153
154 {
155         const struct ec_strvec *vec;
156         struct ec_node *out = NULL;
157         struct ec_node *in1 = operand1;
158         struct ec_node *in2 = operand2;
159
160         (void)userctx;
161
162         printf("eval bin_op %p %p\n", in1, in2);
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 (!strcmp(ec_strvec_val(vec, 0), "|")) {
170                 out = EC_NODE_OR(NULL, 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                 if (!strcmp(in2->type->name, "subset")) {
178                         if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
179                                 return -EINVAL;
180                         ec_node_free(in1);
181                         *result = in2;
182                 } else {
183                         out = EC_NODE_SUBSET(NULL, ec_node_clone(in1),
184                                         ec_node_clone(in2));
185                         if (out == NULL)
186                                 return -EINVAL;
187                         ec_node_free(in1);
188                         ec_node_free(in2);
189                         *result = out;
190                 }
191         } else {
192                 return -EINVAL;
193         }
194
195         printf("eval bin_op out %p\n", *result);
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(NULL, 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         printf("eval paren\n");
230         *result = out;
231
232         return 0;
233 }
234
235 static void
236 ec_node_cmd_eval_free(void *result, void *userctx)
237 {
238         (void)userctx;
239         ec_free(result);
240 }
241
242 static const struct ec_node_expr_eval_ops test_ops = {
243         .eval_var = ec_node_cmd_eval_var,
244         .eval_pre_op = ec_node_cmd_eval_pre_op,
245         .eval_post_op = ec_node_cmd_eval_post_op,
246         .eval_bin_op = ec_node_cmd_eval_bin_op,
247         .eval_parenthesis = ec_node_cmd_eval_parenthesis,
248         .eval_free = ec_node_cmd_eval_free,
249 };
250
251 static int
252 ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state,
253                 const struct ec_strvec *strvec)
254 {
255         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
256
257         return ec_node_parse_child(node->cmd, state, strvec);
258 }
259
260 static int
261 ec_node_cmd_complete(const struct ec_node *gen_node,
262                 struct ec_completed *completed,
263                 struct ec_parsed *parsed,
264                 const struct ec_strvec *strvec)
265 {
266         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
267
268         return ec_node_complete_child(node->cmd, completed, parsed, strvec);
269 }
270
271 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
272 {
273         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
274         unsigned int i;
275
276         ec_free(node->cmd_str);
277         ec_node_free(node->cmd);
278         ec_node_free(node->expr);
279         ec_node_free(node->lex);
280         for (i = 0; i < node->len; i++)
281                 ec_node_free(node->table[i]);
282         ec_free(node->table);
283 }
284
285 static int ec_node_cmd_build(struct ec_node *gen_node)
286 {
287         struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL;
288         struct ec_parsed *p, *child;
289         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
290         void *result;
291         int ret;
292
293         /* XXX the expr parser can be moved in the node init */
294
295         /* build the expression parser */
296         ret = -ENOMEM;
297         expr = ec_node("expr", "expr");
298         if (expr == NULL)
299                 goto fail;
300         ret = ec_node_expr_set_val_node(expr, ec_node_re(NULL, "[a-zA-Z0-9]+"));
301         if (ret < 0)
302                 goto fail;
303         ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, ","));
304         if (ret < 0)
305                 goto fail;
306         ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, "|"));
307         if (ret < 0)
308                 goto fail;
309         ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "+"));
310         if (ret < 0)
311                 goto fail;
312         ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "*"));
313         if (ret < 0)
314                 goto fail;
315         ret = ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "["),
316                 ec_node_str(NULL, "]"));
317         if (ret < 0)
318                 goto fail;
319         ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "("),
320                 ec_node_str(NULL, ")"));
321         if (ret < 0)
322                 goto fail;
323
324         /* prepend a lexer and a "many" to the expression node */
325         ret = -ENOMEM;
326         lex = ec_node_re_lex(NULL,
327                 ec_node_many(NULL, ec_node_clone(expr), 1, 0));
328         if (lex == NULL)
329                 goto fail;
330
331         ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
332         if (ret < 0)
333                 goto fail;
334         ret = ec_node_re_lex_add(lex, "[*|,()]", 1);
335         if (ret < 0)
336                 goto fail;
337         ret = ec_node_re_lex_add(lex, "\\[", 1);
338         if (ret < 0)
339                 goto fail;
340         ret = ec_node_re_lex_add(lex, "\\]", 1);
341         if (ret < 0)
342                 goto fail;
343         ret = ec_node_re_lex_add(lex, "[         ]+", 0);
344         if (ret < 0)
345                 goto fail;
346
347         /* parse the command expression */
348         ret = -ENOMEM;
349         p = ec_node_parse(lex, node->cmd_str);
350         if (p == NULL)
351                 goto fail;
352
353         ret = -EINVAL;
354         if (!ec_parsed_matches(p))
355                 goto fail;
356         if (TAILQ_EMPTY(&p->children))
357                 goto fail;
358         if (TAILQ_EMPTY(&TAILQ_FIRST(&p->children)->children))
359                 goto fail;
360
361         ret = -ENOMEM;
362         cmd = ec_node("seq", NULL);
363         if (cmd == NULL)
364                 goto fail;
365
366         TAILQ_FOREACH(child, &TAILQ_FIRST(&p->children)->children, next) {
367                 ret = ec_node_expr_eval(&result, expr, child,
368                         &test_ops, node);
369                 if (ret < 0)
370                         goto fail;
371                 ret = ec_node_seq_add(cmd, result);
372                 if (ret < 0)
373                         goto fail;
374         }
375         ec_parsed_free(p);
376         p = NULL;
377         ec_node_dump(stdout, cmd);
378
379         ec_node_free(node->expr);
380         node->expr = expr;
381         ec_node_free(node->lex);
382         node->lex = lex;
383         ec_node_free(node->cmd);
384         node->cmd = cmd;
385
386         return 0;
387
388 fail:
389         ec_parsed_free(p);
390         ec_node_free(expr);
391         ec_node_free(lex);
392         ec_node_free(cmd);
393         return ret;
394 }
395
396 static struct ec_node_type ec_node_cmd_type = {
397         .name = "cmd",
398         .build = ec_node_cmd_build,
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
412         // XXX check node type
413
414         assert(node != NULL);
415
416         printf("add child %s\n", child->id);
417         if (child == NULL)
418                 return -EINVAL;
419
420         gen_node->flags &= ~EC_NODE_F_BUILT;
421
422         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
423         if (table == NULL) {
424                 ec_node_free(child);
425                 return -ENOMEM;
426         }
427
428         node->table = table;
429         table[node->len] = child;
430         node->len++;
431
432         child->parent = gen_node;
433         TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
434
435         return 0;
436 }
437
438 struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
439 {
440         struct ec_node *gen_node = NULL;
441         struct ec_node_cmd *node = NULL;
442
443         gen_node = __ec_node(&ec_node_cmd_type, id);
444         if (gen_node == NULL)
445                 goto fail;
446
447         node = (struct ec_node_cmd *)gen_node;
448         node->cmd_str = ec_strdup(cmd_str);
449         if (node->cmd_str == NULL)
450                 goto fail;
451
452         return gen_node;
453
454 fail:
455         ec_node_free(gen_node);
456         return NULL;
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         va_start(ap, cmd);
468
469         gen_node = ec_node_cmd(id, cmd);
470         node = (struct ec_node_cmd *)gen_node;
471         if (node == NULL)
472                 fail = 1;;
473
474         for (child = va_arg(ap, struct ec_node *);
475              child != EC_NODE_ENDLIST;
476              child = va_arg(ap, struct ec_node *)) {
477
478                 /* on error, don't quit the loop to avoid leaks */
479                 if (fail == 1 || child == NULL ||
480                                 ec_node_cmd_add_child(&node->gen, child) < 0) {
481                         fail = 1;
482                         ec_node_free(child);
483                 }
484         }
485
486         if (fail == 1)
487                 goto fail;
488
489         va_end(ap);
490         return gen_node;
491
492 fail:
493         ec_node_free(gen_node); /* will also free children */
494         va_end(ap);
495         return NULL;
496 }
497
498 /* LCOV_EXCL_START */
499 static int ec_node_cmd_testcase(void)
500 {
501         struct ec_node *node;
502         int ret = 0;
503
504         node = EC_NODE_CMD(NULL,
505                 "command [option] (subset1, subset2, subset3) x|y",
506                 ec_node_int("x", 0, 10, 10),
507                 ec_node_int("y", 20, 30, 10)
508         );
509         if (node == NULL) {
510                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
511                 return -1;
512         }
513         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
514         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
515         ret |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
516         ret |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
517         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
518         ec_node_free(node);
519
520         node = EC_NODE_CMD(NULL, "good morning [count] bob|bobby|michael",
521                         ec_node_int("count", 0, 10, 10));
522         if (node == NULL) {
523                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
524                 return -1;
525         }
526         ret |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
527
528         ret |= EC_TEST_CHECK_COMPLETE(node,
529                 "", EC_NODE_ENDLIST,
530                 "good", EC_NODE_ENDLIST);
531         ret |= EC_TEST_CHECK_COMPLETE(node,
532                 "g", EC_NODE_ENDLIST,
533                 "good", EC_NODE_ENDLIST);
534         ret |= EC_TEST_CHECK_COMPLETE(node,
535                 "good", "morning", "", EC_NODE_ENDLIST,
536                 "bob", "bobby", "michael", EC_NODE_ENDLIST);
537
538         ec_node_free(node);
539
540         return ret;
541 }
542 /* LCOV_EXCL_STOP */
543
544 static struct ec_test ec_node_cmd_test = {
545         .name = "node_cmd",
546         .test = ec_node_cmd_testcase,
547 };
548
549 EC_TEST_REGISTER(ec_node_cmd_test);