2 * Copyright (c) 2016-2017, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include <sys/queue.h>
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>
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. */
67 ec_node_cmd_eval_var(void **result, void *userctx,
68 const struct ec_parsed *var)
70 const struct ec_strvec *vec;
71 struct ec_node_cmd *node = userctx;
72 struct ec_node *eval = NULL;
78 /* get parsed string vector, it should contain only one str */
79 vec = ec_parsed_strvec(var);
80 if (ec_strvec_len(vec) != 1)
82 str = ec_strvec_val(vec, 0);
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);
91 /* if id matches, use a node provided by the user... */
92 eval = ec_node_clone(node->table[i]);
98 /* ...or create a string node */
100 eval = ec_node_str(NULL, str);
105 printf("eval var %s %p\n", str, eval);
112 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
113 const struct ec_parsed *operator)
124 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
125 const struct ec_parsed *operator)
127 const struct ec_strvec *vec;
128 struct ec_node *eval = operand;;
132 /* get parsed string vector, it should contain only one str */
133 vec = ec_parsed_strvec(operator);
134 if (ec_strvec_len(vec) != 1)
137 if (!strcmp(ec_strvec_val(vec, 0), "*"))
142 printf("eval post_op %p\n", eval);
149 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
150 const struct ec_parsed *operator, void *operand2)
153 const struct ec_strvec *vec;
154 struct ec_node *out = NULL;
155 struct ec_node *in1 = operand1;
156 struct ec_node *in2 = operand2;
160 printf("eval bin_op %p %p\n", in1, in2);
162 /* get parsed string vector, it should contain only one str */
163 vec = ec_parsed_strvec(operator);
164 if (ec_strvec_len(vec) != 1)
167 if (!strcmp(ec_strvec_val(vec, 0), "|")) {
168 out = EC_NODE_OR(NULL, ec_node_clone(in1), ec_node_clone(in2));
174 } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
175 out = EC_NODE_SUBSET(NULL, ec_node_clone(in1), ec_node_clone(in2));
189 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
190 const struct ec_parsed *open_paren,
191 const struct ec_parsed *close_paren,
194 const struct ec_strvec *vec;
195 struct ec_node *in = value;;
196 struct ec_node *out = NULL;;
201 /* get parsed string vector, it should contain only one str */
202 vec = ec_parsed_strvec(open_paren);
203 if (ec_strvec_len(vec) != 1)
206 if (!strcmp(ec_strvec_val(vec, 0), "[")) {
207 out = ec_node_option(NULL, ec_node_clone(in));
211 } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
217 printf("eval paren\n");
224 ec_node_cmd_eval_free(void *result, void *userctx)
230 static const struct ec_node_expr_eval_ops test_ops = {
231 .eval_var = ec_node_cmd_eval_var,
232 .eval_pre_op = ec_node_cmd_eval_pre_op,
233 .eval_post_op = ec_node_cmd_eval_post_op,
234 .eval_bin_op = ec_node_cmd_eval_bin_op,
235 .eval_parenthesis = ec_node_cmd_eval_parenthesis,
236 .eval_free = ec_node_cmd_eval_free,
239 static struct ec_parsed *ec_node_cmd_parse(const struct ec_node *gen_node,
240 const struct ec_strvec *strvec)
242 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
244 return ec_node_parse_strvec(node->cmd, strvec);
247 static struct ec_completed *ec_node_cmd_complete(const struct ec_node *gen_node,
248 const struct ec_strvec *strvec)
250 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
252 return ec_node_complete_strvec(node->cmd, strvec);
255 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
257 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
260 ec_free(node->cmd_str);
261 ec_node_free(node->cmd);
262 ec_node_free(node->expr);
263 ec_node_free(node->lex);
264 for (i = 0; i < node->len; i++)
265 ec_node_free(node->table[i]);
266 ec_free(node->table);
269 static int ec_node_cmd_build(struct ec_node *gen_node)
271 struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL;
272 struct ec_parsed *p, *child;
273 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
277 /* XXX the expr parser can be moved in the node init */
279 /* build the expression parser */
281 expr = ec_node("expr", "expr");
284 ret = ec_node_expr_set_val_node(expr, ec_node_re(NULL, "[a-zA-Z0-9]+"));
287 ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, ","));
290 ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, "|"));
293 ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "+"));
296 ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "*"));
299 ret = ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "["),
300 ec_node_str(NULL, "]"));
303 ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "("),
304 ec_node_str(NULL, ")"));
308 /* prepend a lexer and a "many" to the expression node */
310 lex = ec_node_re_lex(NULL,
311 ec_node_many(NULL, ec_node_clone(expr), 1, 0));
315 ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
318 ret = ec_node_re_lex_add(lex, "[*|,()]", 1);
321 ret = ec_node_re_lex_add(lex, "\\[", 1);
324 ret = ec_node_re_lex_add(lex, "\\]", 1);
327 ret = ec_node_re_lex_add(lex, "[ ]+", 0);
331 /* parse the command expression */
333 p = ec_node_parse(lex, node->cmd_str);
338 if (!ec_parsed_matches(p))
340 if (TAILQ_EMPTY(&p->children))
342 if (TAILQ_EMPTY(&TAILQ_FIRST(&p->children)->children))
346 cmd = ec_node("seq", NULL);
350 TAILQ_FOREACH(child, &TAILQ_FIRST(&p->children)->children, next) {
351 ret = ec_node_expr_eval(&result, expr, child,
355 ret = ec_node_seq_add(cmd, result);
361 ec_node_dump(stdout, cmd);
363 ec_node_free(node->expr);
365 ec_node_free(node->lex);
367 ec_node_free(node->cmd);
380 static struct ec_node_type ec_node_cmd_type = {
382 .build = ec_node_cmd_build,
383 .parse = ec_node_cmd_parse,
384 .complete = ec_node_cmd_complete,
385 .size = sizeof(struct ec_node_cmd),
386 .free_priv = ec_node_cmd_free_priv,
389 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
391 int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
393 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
394 struct ec_node **table;
396 // XXX check node type
398 assert(node != NULL);
400 printf("add child %s\n", child->id);
404 gen_node->flags &= ~EC_NODE_F_BUILT;
406 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
413 table[node->len] = child;
416 child->parent = gen_node;
417 TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
422 struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
424 struct ec_node *gen_node = NULL;
425 struct ec_node_cmd *node = NULL;
427 gen_node = __ec_node(&ec_node_cmd_type, id);
428 if (gen_node == NULL)
431 node = (struct ec_node_cmd *)gen_node;
432 node->cmd_str = ec_strdup(cmd_str);
433 if (node->cmd_str == NULL)
439 ec_node_free(gen_node);
443 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
445 struct ec_node *gen_node = NULL;
446 struct ec_node_cmd *node = NULL;
447 struct ec_node *child;
453 gen_node = ec_node_cmd(id, cmd);
454 node = (struct ec_node_cmd *)gen_node;
458 for (child = va_arg(ap, struct ec_node *);
459 child != EC_NODE_ENDLIST;
460 child = va_arg(ap, struct ec_node *)) {
462 /* on error, don't quit the loop to avoid leaks */
463 if (fail == 1 || child == NULL ||
464 ec_node_cmd_add_child(&node->gen, child) < 0) {
477 ec_node_free(gen_node); /* will also free children */
482 static int ec_node_cmd_testcase(void)
484 struct ec_node *node;
487 node = EC_NODE_CMD(NULL,
488 "command [option] (subset1, subset2) x | y",
489 ec_node_int("x", 0, 10, 10),
490 ec_node_int("y", 20, 30, 10)
493 ec_log(EC_LOG_ERR, "cannot create node\n");
496 ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
497 ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
498 ret |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
499 ret |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
500 ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
503 node = EC_NODE_CMD(NULL, "good morning bob|bobby|michael [count]",
504 ec_node_int("count", 0, 10, 10));
506 ec_log(EC_LOG_ERR, "cannot create node\n");
509 ret |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "bob", "1");
517 static struct ec_test ec_node_cmd_test = {
519 .test = ec_node_cmd_testcase,
522 EC_TEST_REGISTER(ec_node_cmd_test);