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_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>
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. */
64 ec_node_cmd_eval_var(void **result, void *userctx,
65 const struct ec_parsed *var)
67 const struct ec_strvec *vec;
68 struct ec_node_cmd *node = userctx;
69 struct ec_node *eval = NULL;
75 /* get parsed string vector, it should contain only one str */
76 vec = ec_parsed_strvec(var);
77 if (ec_strvec_len(vec) != 1)
79 str = ec_strvec_val(vec, 0);
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);
88 /* if id matches, use a node provided by the user... */
89 eval = ec_node_clone(node->table[i]);
95 /* ...or create a string node */
97 eval = ec_node_str(NULL, str);
102 printf("eval var %s %p\n", str, eval);
109 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
110 const struct ec_parsed *operator)
121 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
122 const struct ec_parsed *operator)
124 const struct ec_strvec *vec;
125 struct ec_node *eval = operand;;
129 /* get parsed string vector, it should contain only one str */
130 vec = ec_parsed_strvec(operator);
131 if (ec_strvec_len(vec) != 1)
134 if (!strcmp(ec_strvec_val(vec, 0), "*"))
139 printf("eval post_op %p\n", eval);
146 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
147 const struct ec_parsed *operator, void *operand2)
150 const struct ec_strvec *vec;
151 struct ec_node *out = NULL;
152 struct ec_node *in1 = operand1;
153 struct ec_node *in2 = operand2;
157 printf("eval bin_op %p %p\n", in1, in2);
159 /* get parsed string vector, it should contain only one str */
160 vec = ec_parsed_strvec(operator);
161 if (ec_strvec_len(vec) != 1)
164 if (!strcmp(ec_strvec_val(vec, 0), "|")) {
165 out = EC_NODE_OR(NULL, ec_node_clone(in1), ec_node_clone(in2));
171 } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
172 out = EC_NODE_SUBSET(NULL, ec_node_clone(in1), ec_node_clone(in2));
186 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
187 const struct ec_parsed *open_paren,
188 const struct ec_parsed *close_paren,
191 const struct ec_strvec *vec;
192 struct ec_node *in = value;;
193 struct ec_node *out = NULL;;
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)
203 if (!strcmp(ec_strvec_val(vec, 0), "[")) {
204 out = ec_node_option(NULL, ec_node_clone(in));
208 } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
214 printf("eval paren\n");
221 ec_node_cmd_eval_free(void *result, void *userctx)
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,
236 static struct ec_parsed *ec_node_cmd_parse(const struct ec_node *gen_node,
237 const struct ec_strvec *strvec)
239 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
241 return ec_node_parse_strvec(node->cmd, strvec);
244 static struct ec_completed *ec_node_cmd_complete(const struct ec_node *gen_node,
245 const struct ec_strvec *strvec)
247 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
249 return ec_node_complete_strvec(node->cmd, strvec);
252 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
254 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
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);
266 static int ec_node_cmd_build(struct ec_node *gen_node)
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;
274 /* build the expression parser */
276 expr = ec_node_new("expr", "expr");
279 ret = ec_node_expr_set_val_node(expr, ec_node_re(NULL, "[a-zA-Z0-9]+"));
282 ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, ","));
285 ret = ec_node_expr_add_bin_op(expr, ec_node_str(NULL, "|"));
288 ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "+"));
291 ret = ec_node_expr_add_post_op(expr, ec_node_str(NULL, "*"));
294 ret = ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "["),
295 ec_node_str(NULL, "]"));
298 ec_node_expr_add_parenthesis(expr, ec_node_str(NULL, "("),
299 ec_node_str(NULL, ")"));
303 /* prepend a lexer and a "many" to the expression node */
305 lex = ec_node_re_lex(NULL,
306 ec_node_many(NULL, ec_node_clone(expr), 1, 0));
310 ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
313 ret = ec_node_re_lex_add(lex, "[*|,()]", 1);
316 ret = ec_node_re_lex_add(lex, "\\[", 1);
319 ret = ec_node_re_lex_add(lex, "\\]", 1);
322 ret = ec_node_re_lex_add(lex, "[ ]+", 0);
326 /* parse the command expression */
328 p = ec_node_parse(lex, node->cmd_str);
333 if (!ec_parsed_matches(p))
335 if (TAILQ_EMPTY(&p->children))
337 if (TAILQ_EMPTY(&TAILQ_FIRST(&p->children)->children))
341 cmd = ec_node_new("seq", NULL);
345 TAILQ_FOREACH(child, &TAILQ_FIRST(&p->children)->children, next) {
346 ret = ec_node_expr_eval(&result, expr, child,
350 ret = ec_node_seq_add(cmd, result);
355 ec_node_dump(stdout, cmd);
357 ec_node_free(node->expr);
359 ec_node_free(node->lex);
361 ec_node_free(node->cmd);
373 static struct ec_node_type ec_node_cmd_type = {
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,
382 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
384 int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
386 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
387 struct ec_node **table;
389 // XXX check node type
391 assert(node != NULL);
393 printf("add child %s\n", child->id);
397 gen_node->flags &= ~EC_NODE_F_BUILT;
399 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
406 table[node->len] = child;
409 child->parent = gen_node;
410 TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
415 struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
417 struct ec_node *gen_node = NULL;
418 struct ec_node_cmd *node = NULL;
420 gen_node = __ec_node_new(&ec_node_cmd_type, id);
421 if (gen_node == NULL)
424 node = (struct ec_node_cmd *)gen_node;
425 node->cmd_str = ec_strdup(cmd_str);
426 if (node->cmd_str == NULL)
432 ec_node_free(gen_node);
436 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
438 struct ec_node *gen_node = NULL;
439 struct ec_node_cmd *node = NULL;
440 struct ec_node *child;
446 gen_node = ec_node_cmd(id, cmd);
447 node = (struct ec_node_cmd *)gen_node;
451 for (child = va_arg(ap, struct ec_node *);
452 child != EC_NODE_ENDLIST;
453 child = va_arg(ap, struct ec_node *)) {
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) {
470 ec_node_free(gen_node); /* will also free children */
475 static int ec_node_cmd_testcase(void)
477 struct ec_node *node;
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)
486 ec_log(EC_LOG_ERR, "cannot create node\n");
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");
501 static struct ec_test ec_node_cmd_test = {
503 .test = ec_node_cmd_testcase,
506 EC_TEST_REGISTER(ec_node_cmd_test);