save
[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 struct ec_node_cmd {
57         struct ec_node gen;
58         char *cmd_str;           /* the command string. */
59         struct ec_node *cmd;       /* the command node. */
60         struct ec_node *lex;       /* the lexer node. */
61         struct ec_node *expr;      /* the expression parser. */
62         struct ec_node **table;    /* table of node referenced in command. */
63         unsigned int len;        /* len of the table. */
64 };
65
66 static int
67 ec_node_cmd_eval_var(void **result, void *userctx,
68         const struct ec_parsed *var)
69 {
70         const struct ec_strvec *vec;
71         struct ec_node_cmd *node = userctx;
72         struct ec_node *eval = NULL;
73         const char *str, *id;
74         unsigned int i;
75
76         (void)userctx;
77
78         /* get parsed string vector, it should contain only one str */
79         vec = ec_parsed_strvec(var);
80         if (ec_strvec_len(vec) != 1)
81                 return -EINVAL;
82         str = ec_strvec_val(vec, 0);
83
84         for (i = 0; i < node->len; i++) {
85                 id = ec_node_id(node->table[i]);
86                 printf("i=%d id=%s\n", i, id);
87                 if (id == NULL)
88                         continue;
89                 if (strcmp(str, id))
90                         continue;
91                 /* if id matches, use a node provided by the user... */
92                 eval = ec_node_clone(node->table[i]);
93                 if (eval == NULL)
94                         return -ENOMEM;
95                 break;
96         }
97
98         /* ...or create a string node */
99         if (eval == NULL) {
100                 eval = ec_node_str(NULL, str);
101                 if (eval == NULL)
102                         return -ENOMEM;
103         }
104
105         printf("eval var %s %p\n", str, eval);
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 *eval = operand;;
129
130         (void)userctx;
131
132         /* get parsed string vector, it should contain only one str */
133         vec = ec_parsed_strvec(operator);
134         if (ec_strvec_len(vec) != 1)
135                 return -EINVAL;
136
137         if (!strcmp(ec_strvec_val(vec, 0), "*"))
138                 eval = NULL; //XXX
139         else
140                 return -EINVAL;
141
142         printf("eval post_op %p\n", eval);
143         *result = eval;
144
145         return 0;
146 }
147
148 static int
149 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
150         const struct ec_parsed *operator, void *operand2)
151
152 {
153         const struct ec_strvec *vec;
154         struct ec_node *out = NULL;
155         struct ec_node *in1 = operand1;
156         struct ec_node *in2 = operand2;
157
158         (void)userctx;
159
160         printf("eval bin_op %p %p\n", in1, in2);
161
162         /* get parsed string vector, it should contain only one str */
163         vec = ec_parsed_strvec(operator);
164         if (ec_strvec_len(vec) != 1)
165                 return -EINVAL;
166
167         if (!strcmp(ec_strvec_val(vec, 0), "|")) {
168                 out = EC_NODE_OR(NULL, ec_node_clone(in1), ec_node_clone(in2));
169                 if (out == NULL)
170                         return -EINVAL;
171                 ec_node_free(in1);
172                 ec_node_free(in2);
173                 *result = out;
174         } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
175                 if (!strcmp(in2->type->name, "subset")) {
176                         if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
177                                 return -EINVAL;
178                         ec_node_free(in1);
179                         *result = in2;
180                 } else {
181                         out = EC_NODE_SUBSET(NULL, ec_node_clone(in1),
182                                         ec_node_clone(in2));
183                         if (out == NULL)
184                                 return -EINVAL;
185                         ec_node_free(in1);
186                         ec_node_free(in2);
187                         *result = out;
188                 }
189         } else {
190                 return -EINVAL;
191         }
192
193         printf("eval bin_op out %p\n", *result);
194
195         return 0;
196 }
197
198 static int
199 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
200         const struct ec_parsed *open_paren,
201         const struct ec_parsed *close_paren,
202         void *value)
203 {
204         const struct ec_strvec *vec;
205         struct ec_node *in = value;;
206         struct ec_node *out = NULL;;
207
208         (void)userctx;
209         (void)close_paren;
210
211         /* get parsed string vector, it should contain only one str */
212         vec = ec_parsed_strvec(open_paren);
213         if (ec_strvec_len(vec) != 1)
214                 return -EINVAL;
215
216         if (!strcmp(ec_strvec_val(vec, 0), "[")) {
217                 out = ec_node_option(NULL, ec_node_clone(in));
218                 if (out == NULL)
219                         return -EINVAL;
220                 ec_node_free(in);
221         } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
222                 out = in;
223         } else {
224                 return -EINVAL;
225         }
226
227         printf("eval paren\n");
228         *result = out;
229
230         return 0;
231 }
232
233 static void
234 ec_node_cmd_eval_free(void *result, void *userctx)
235 {
236         (void)userctx;
237         ec_free(result);
238 }
239
240 static const struct ec_node_expr_eval_ops test_ops = {
241         .eval_var = ec_node_cmd_eval_var,
242         .eval_pre_op = ec_node_cmd_eval_pre_op,
243         .eval_post_op = ec_node_cmd_eval_post_op,
244         .eval_bin_op = ec_node_cmd_eval_bin_op,
245         .eval_parenthesis = ec_node_cmd_eval_parenthesis,
246         .eval_free = ec_node_cmd_eval_free,
247 };
248
249 static int
250 ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state,
251                 const struct ec_strvec *strvec)
252 {
253         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
254
255         return ec_node_parse_child(node->cmd, state, strvec);
256 }
257
258 static int
259 ec_node_cmd_complete(const struct ec_node *gen_node,
260                 struct ec_completed *completed,
261                 struct ec_parsed *parsed,
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, parsed, 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(NULL, "[a-zA-Z0-9]+"));
299         if (ret < 0)
300                 goto fail;
301         ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, ","));
302         if (ret < 0)
303                 goto fail;
304         ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, "|"));
305         if (ret < 0)
306                 goto fail;
307         ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "+"));
308         if (ret < 0)
309                 goto fail;
310         ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "*"));
311         if (ret < 0)
312                 goto fail;
313         ret = ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "["),
314                 ec_node_str(NULL, "]"));
315         if (ret < 0)
316                 goto fail;
317         ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "("),
318                 ec_node_str(NULL, ")"));
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(NULL,
325                 ec_node_many(NULL, 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 (TAILQ_EMPTY(&p->children))
355                 goto fail;
356         if (TAILQ_EMPTY(&TAILQ_FIRST(&p->children)->children))
357                 goto fail;
358
359         ret = -ENOMEM;
360         cmd = ec_node("seq", NULL);
361         if (cmd == NULL)
362                 goto fail;
363
364         TAILQ_FOREACH(child, &TAILQ_FIRST(&p->children)->children, next) {
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         ec_node_dump(stdout, cmd);
376
377         ec_node_free(node->expr);
378         node->expr = expr;
379         ec_node_free(node->lex);
380         node->lex = lex;
381         ec_node_free(node->cmd);
382         node->cmd = cmd;
383
384         return 0;
385
386 fail:
387         ec_parsed_free(p);
388         ec_node_free(expr);
389         ec_node_free(lex);
390         ec_node_free(cmd);
391         return ret;
392 }
393
394 static struct ec_node_type ec_node_cmd_type = {
395         .name = "cmd",
396         .build = ec_node_cmd_build,
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
410         // XXX check node type
411
412         assert(node != NULL);
413
414         printf("add child %s\n", child->id);
415         if (child == NULL)
416                 return -EINVAL;
417
418         gen_node->flags &= ~EC_NODE_F_BUILT;
419
420         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
421         if (table == NULL) {
422                 ec_node_free(child);
423                 return -ENOMEM;
424         }
425
426         node->table = table;
427         table[node->len] = child;
428         node->len++;
429
430         child->parent = gen_node;
431         TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
432
433         return 0;
434 }
435
436 struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
437 {
438         struct ec_node *gen_node = NULL;
439         struct ec_node_cmd *node = NULL;
440
441         gen_node = __ec_node(&ec_node_cmd_type, id);
442         if (gen_node == NULL)
443                 goto fail;
444
445         node = (struct ec_node_cmd *)gen_node;
446         node->cmd_str = ec_strdup(cmd_str);
447         if (node->cmd_str == NULL)
448                 goto fail;
449
450         return gen_node;
451
452 fail:
453         ec_node_free(gen_node);
454         return NULL;
455 }
456
457 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
458 {
459         struct ec_node *gen_node = NULL;
460         struct ec_node_cmd *node = NULL;
461         struct ec_node *child;
462         va_list ap;
463         int fail = 0;
464
465         va_start(ap, cmd);
466
467         gen_node = ec_node_cmd(id, cmd);
468         node = (struct ec_node_cmd *)gen_node;
469         if (node == NULL)
470                 fail = 1;;
471
472         for (child = va_arg(ap, struct ec_node *);
473              child != EC_NODE_ENDLIST;
474              child = va_arg(ap, struct ec_node *)) {
475
476                 /* on error, don't quit the loop to avoid leaks */
477                 if (fail == 1 || child == NULL ||
478                                 ec_node_cmd_add_child(&node->gen, child) < 0) {
479                         fail = 1;
480                         ec_node_free(child);
481                 }
482         }
483
484         if (fail == 1)
485                 goto fail;
486
487         va_end(ap);
488         return gen_node;
489
490 fail:
491         ec_node_free(gen_node); /* will also free children */
492         va_end(ap);
493         return NULL;
494 }
495
496 /* LCOV_EXCL_START */
497 static int ec_node_cmd_testcase(void)
498 {
499         struct ec_node *node;
500         int ret = 0;
501
502         node = EC_NODE_CMD(NULL,
503                 "command [option] (subset1, subset2, subset3) x|y",
504                 ec_node_int("x", 0, 10, 10),
505                 ec_node_int("y", 20, 30, 10)
506         );
507         if (node == NULL) {
508                 ec_log(EC_LOG_ERR, "cannot create node\n");
509                 return -1;
510         }
511         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
512         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
513         ret |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
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(NULL, "good morning bob|bobby|michael [count]",
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", "bob", "1");
525         ec_node_free(node);
526
527         // XXX completion
528
529         return ret;
530 }
531 /* LCOV_EXCL_STOP */
532
533 static struct ec_test ec_node_cmd_test = {
534         .name = "node_cmd",
535         .test = ec_node_cmd_testcase,
536 };
537
538 EC_TEST_REGISTER(ec_node_cmd_test);