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_expr.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_node_or.h>
44 #include <ecoli_node_subset.h>
45 #include <ecoli_node_int.h>
46 #include <ecoli_node_many.h>
47 #include <ecoli_node_seq.h>
48 #include <ecoli_node_option.h>
49 #include <ecoli_node_re.h>
50 #include <ecoli_node_re_lex.h>
51 #include <ecoli_node_cmd.h>
52
53 struct ec_node_cmd {
54         struct ec_node gen;
55         char *cmd_str;           /* the command string. */
56         struct ec_node *cmd;       /* the command node. */
57         struct ec_node *lex;       /* the lexer node. */
58         struct ec_node *expr;      /* the expression parser. */
59         struct ec_node **table;    /* table of node referenced in command. */
60         unsigned int len;        /* len of the table. */
61 };
62
63 static int
64 ec_node_cmd_eval_var(void **result, void *userctx,
65         const struct ec_parsed *var)
66 {
67         const struct ec_strvec *vec;
68         struct ec_node_cmd *node = userctx;
69         struct ec_node *eval = NULL;
70         const char *str, *id;
71         unsigned int i;
72
73         (void)userctx;
74
75         /* get parsed string vector, it should contain only one str */
76         vec = ec_parsed_strvec(var);
77         if (ec_strvec_len(vec) != 1)
78                 return -EINVAL;
79         str = ec_strvec_val(vec, 0);
80
81         for (i = 0; i < node->len; i++) {
82                 id = ec_node_id(node->table[i]);
83                 printf("i=%d id=%s\n", i, id);
84                 if (id == NULL)
85                         continue;
86                 if (strcmp(str, id))
87                         continue;
88                 /* if id matches, use a node provided by the user... */
89                 eval = ec_node_clone(node->table[i]);
90                 if (eval == NULL)
91                         return -ENOMEM;
92                 break;
93         }
94
95         /* ...or create a string node */
96         if (eval == NULL) {
97                 eval = ec_node_str(NULL, str);
98                 if (eval == NULL)
99                         return -ENOMEM;
100         }
101
102         printf("eval var %s %p\n", str, eval);
103         *result = eval;
104
105         return 0;
106 }
107
108 static int
109 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
110         const struct ec_parsed *operator)
111 {
112         (void)result;
113         (void)userctx;
114         (void)operand;
115         (void)operator;
116
117         return -EINVAL;
118 }
119
120 static int
121 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
122         const struct ec_parsed *operator)
123 {
124         const struct ec_strvec *vec;
125         struct ec_node *eval = operand;;
126
127         (void)userctx;
128
129         /* get parsed string vector, it should contain only one str */
130         vec = ec_parsed_strvec(operator);
131         if (ec_strvec_len(vec) != 1)
132                 return -EINVAL;
133
134         if (!strcmp(ec_strvec_val(vec, 0), "*"))
135                 eval = NULL; //XXX
136         else
137                 return -EINVAL;
138
139         printf("eval post_op %p\n", eval);
140         *result = eval;
141
142         return 0;
143 }
144
145 static int
146 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
147         const struct ec_parsed *operator, void *operand2)
148
149 {
150         const struct ec_strvec *vec;
151         struct ec_node *out = NULL;
152         struct ec_node *in1 = operand1;
153         struct ec_node *in2 = operand2;
154
155         (void)userctx;
156
157         printf("eval bin_op %p %p\n", in1, in2);
158
159         /* get parsed string vector, it should contain only one str */
160         vec = ec_parsed_strvec(operator);
161         if (ec_strvec_len(vec) != 1)
162                 return -EINVAL;
163
164         if (!strcmp(ec_strvec_val(vec, 0), "|")) {
165                 out = EC_NODE_OR(NULL, ec_node_clone(in1), ec_node_clone(in2));
166                 if (out == NULL)
167                         return -EINVAL;
168                 ec_node_free(in1);
169                 ec_node_free(in2);
170                 *result = out;
171         } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
172                 out = EC_NODE_SUBSET(NULL, 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 {
179                 return -EINVAL;
180         }
181
182         return 0;
183 }
184
185 static int
186 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
187         const struct ec_parsed *open_paren,
188         const struct ec_parsed *close_paren,
189         void *value)
190 {
191         const struct ec_strvec *vec;
192         struct ec_node *in = value;;
193         struct ec_node *out = NULL;;
194
195         (void)userctx;
196         (void)close_paren;
197
198         /* get parsed string vector, it should contain only one str */
199         vec = ec_parsed_strvec(open_paren);
200         if (ec_strvec_len(vec) != 1)
201                 return -EINVAL;
202
203         if (!strcmp(ec_strvec_val(vec, 0), "[")) {
204                 out = ec_node_option(NULL, ec_node_clone(in));
205                 if (out == NULL)
206                         return -EINVAL;
207                 ec_node_free(in);
208         } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
209                 out = in;
210         } else {
211                 return -EINVAL;
212         }
213
214         printf("eval paren\n");
215         *result = out;
216
217         return 0;
218 }
219
220 static void
221 ec_node_cmd_eval_free(void *result, void *userctx)
222 {
223         (void)userctx;
224         ec_free(result);
225 }
226
227 static const struct ec_node_expr_eval_ops test_ops = {
228         .eval_var = ec_node_cmd_eval_var,
229         .eval_pre_op = ec_node_cmd_eval_pre_op,
230         .eval_post_op = ec_node_cmd_eval_post_op,
231         .eval_bin_op = ec_node_cmd_eval_bin_op,
232         .eval_parenthesis = ec_node_cmd_eval_parenthesis,
233         .eval_free = ec_node_cmd_eval_free,
234 };
235
236 static struct ec_parsed *ec_node_cmd_parse(const struct ec_node *gen_node,
237         const struct ec_strvec *strvec)
238 {
239         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
240
241         return ec_node_parse_strvec(node->cmd, strvec);
242 }
243
244 static struct ec_completed *ec_node_cmd_complete(const struct ec_node *gen_node,
245         const struct ec_strvec *strvec)
246 {
247         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
248
249         return ec_node_complete_strvec(node->cmd, strvec);
250 }
251
252 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
253 {
254         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
255         unsigned int i;
256
257         ec_free(node->cmd_str);
258         ec_node_free(node->cmd);
259         ec_node_free(node->expr);
260         ec_node_free(node->lex);
261         for (i = 0; i < node->len; i++)
262                 ec_node_free(node->table[i]);
263         ec_free(node->table);
264 }
265
266 static int ec_node_cmd_build(struct ec_node *gen_node)
267 {
268         struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL;
269         struct ec_parsed *p, *child;
270         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
271         void *result;
272         int ret;
273
274         /* build the expression parser */
275         ret = -ENOMEM;
276         expr = ec_node_new("expr", "expr");
277         if (expr == NULL)
278                 goto fail;
279         ret = ec_node_expr_set_val_node(expr, ec_node_re(NULL, "[a-zA-Z0-9]+"));
280         if (ret < 0)
281                 goto fail;
282         ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, ","));
283         if (ret < 0)
284                 goto fail;
285         ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, "|"));
286         if (ret < 0)
287                 goto fail;
288         ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "+"));
289         if (ret < 0)
290                 goto fail;
291         ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "*"));
292         if (ret < 0)
293                 goto fail;
294         ret = ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "["),
295                 ec_node_str(NULL, "]"));
296         if (ret < 0)
297                 goto fail;
298         ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "("),
299                 ec_node_str(NULL, ")"));
300         if (ret < 0)
301                 goto fail;
302
303         /* prepend a lexer and a "many" to the expression node */
304         ret = -ENOMEM;
305         lex = ec_node_re_lex(NULL,
306                 ec_node_many(NULL, ec_node_clone(expr), 1, 0));
307         if (lex == NULL)
308                 goto fail;
309
310         ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 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, "\\]", 1);
320         if (ret < 0)
321                 goto fail;
322         ret = ec_node_re_lex_add(lex, "[         ]+", 0);
323         if (ret < 0)
324                 goto fail;
325
326         /* parse the command expression */
327         ret = -ENOMEM;
328         p = ec_node_parse(lex, node->cmd_str);
329         if (p == NULL)
330                 goto fail;
331
332         ret = -EINVAL;
333         if (!ec_parsed_matches(p))
334                 goto fail;
335         if (TAILQ_EMPTY(&p->children))
336                 goto fail;
337         if (TAILQ_EMPTY(&TAILQ_FIRST(&p->children)->children))
338                 goto fail;
339
340         ret = -ENOMEM;
341         cmd = ec_node_new("seq", NULL);
342         if (cmd == NULL)
343                 goto fail;
344
345         TAILQ_FOREACH(child, &TAILQ_FIRST(&p->children)->children, next) {
346                 ret = ec_node_expr_eval(&result, expr, child,
347                         &test_ops, node);
348                 if (ret < 0)
349                         goto fail;
350                 ret = ec_node_seq_add(cmd, result);
351                 if (ret < 0)
352                         goto fail;
353         }
354         ec_parsed_free(p);
355         ec_node_dump(stdout, cmd);
356
357         ec_node_free(node->expr);
358         node->expr = expr;
359         ec_node_free(node->lex);
360         node->lex = lex;
361         ec_node_free(node->cmd);
362         node->cmd = cmd;
363
364         return 0;
365
366 fail:
367         ec_node_free(expr);
368         ec_node_free(lex);
369         ec_node_free(cmd);
370         return ret;
371 }
372
373 static struct ec_node_type ec_node_cmd_type = {
374         .name = "cmd",
375         .build = ec_node_cmd_build,
376         .parse = ec_node_cmd_parse,
377         .complete = ec_node_cmd_complete,
378         .size = sizeof(struct ec_node_cmd),
379         .free_priv = ec_node_cmd_free_priv,
380 };
381
382 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
383
384 int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
385 {
386         struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
387         struct ec_node **table;
388
389         // XXX check node type
390
391         assert(node != NULL);
392
393         printf("add child %s\n", child->id);
394         if (child == NULL)
395                 return -EINVAL;
396
397         gen_node->flags &= ~EC_NODE_F_BUILT;
398
399         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
400         if (table == NULL) {
401                 ec_node_free(child);
402                 return -ENOMEM;
403         }
404
405         node->table = table;
406         table[node->len] = child;
407         node->len++;
408
409         child->parent = gen_node;
410         TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
411
412         return 0;
413 }
414
415 struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
416 {
417         struct ec_node *gen_node = NULL;
418         struct ec_node_cmd *node = NULL;
419
420         gen_node = __ec_node_new(&ec_node_cmd_type, id);
421         if (gen_node == NULL)
422                 goto fail;
423
424         node = (struct ec_node_cmd *)gen_node;
425         node->cmd_str = ec_strdup(cmd_str);
426         if (node->cmd_str == NULL)
427                 goto fail;
428
429         return gen_node;
430
431 fail:
432         ec_node_free(gen_node);
433         return NULL;
434 }
435
436 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
437 {
438         struct ec_node *gen_node = NULL;
439         struct ec_node_cmd *node = NULL;
440         struct ec_node *child;
441         va_list ap;
442         int fail = 0;
443
444         va_start(ap, cmd);
445
446         gen_node = ec_node_cmd(id, cmd);
447         node = (struct ec_node_cmd *)gen_node;
448         if (node == NULL)
449                 fail = 1;;
450
451         for (child = va_arg(ap, struct ec_node *);
452              child != EC_NODE_ENDLIST;
453              child = va_arg(ap, struct ec_node *)) {
454
455                 /* on error, don't quit the loop to avoid leaks */
456                 if (fail == 1 || child == NULL ||
457                                 ec_node_cmd_add_child(&node->gen, child) < 0) {
458                         fail = 1;
459                         ec_node_free(child);
460                 }
461         }
462
463         if (fail == 1)
464                 goto fail;
465
466         va_end(ap);
467         return gen_node;
468
469 fail:
470         ec_node_free(gen_node); /* will also free children */
471         va_end(ap);
472         return NULL;
473 }
474
475 static int ec_node_cmd_testcase(void)
476 {
477         struct ec_node *node;
478         int ret = 0;
479
480         node = EC_NODE_CMD(NULL,
481                 "command [option] (subset1, subset2) x | y",
482                 ec_node_int("x", 0, 10, 10),
483                 ec_node_int("y", 20, 30, 10)
484         );
485         if (node == NULL) {
486                 ec_log(EC_LOG_ERR, "cannot create node\n");
487                 return -1;
488         }
489         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
490         ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
491         ret |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
492         ret |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
493         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
494         ec_node_free(node);
495
496         // XXX completion
497
498         return ret;
499 }
500
501 static struct ec_test ec_node_cmd_test = {
502         .name = "node_cmd",
503         .test = ec_node_cmd_testcase,
504 };
505
506 EC_TEST_REGISTER(ec_node_cmd_test);