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>
56 EC_LOG_TYPE_REGISTER(node_cmd);
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. */
69 ec_node_cmd_eval_var(void **result, void *userctx,
70 const struct ec_parsed *var)
72 const struct ec_strvec *vec;
73 struct ec_node_cmd *node = userctx;
74 struct ec_node *eval = NULL;
80 /* get parsed string vector, it should contain only one str */
81 vec = ec_parsed_strvec(var);
82 if (ec_strvec_len(vec) != 1)
84 str = ec_strvec_val(vec, 0);
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);
93 /* if id matches, use a node provided by the user... */
94 eval = ec_node_clone(node->table[i]);
100 /* ...or create a string node */
102 eval = ec_node_str(EC_NO_ID, str);
107 //printf("eval var %s %p\n", str, eval);
114 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
115 const struct ec_parsed *operator)
126 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
127 const struct ec_parsed *operator)
129 const struct ec_strvec *vec;
130 struct ec_node *in = operand;;
131 struct ec_node *out = NULL;;
135 /* get parsed string vector, it should contain only one str */
136 vec = ec_parsed_strvec(operator);
137 if (ec_strvec_len(vec) != 1)
140 if (!strcmp(ec_strvec_val(vec, 0), "*")) {
141 out = ec_node_many(EC_NO_ID,
142 ec_node_clone(in), 0, 0);
155 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
156 const struct ec_parsed *operator, void *operand2)
159 const struct ec_strvec *vec;
160 struct ec_node *out = NULL;
161 struct ec_node *in1 = operand1;
162 struct ec_node *in2 = operand2;
166 /* get parsed string vector, it should contain only one str */
167 vec = ec_parsed_strvec(operator);
168 if (ec_strvec_len(vec) != 1)
171 if (!strcmp(ec_strvec_val(vec, 0), "|")) {
172 out = EC_NODE_OR(EC_NO_ID, ec_node_clone(in1), ec_node_clone(in2));
178 } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
179 if (!strcmp(in2->type->name, "subset")) {
180 if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
185 out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
201 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
202 const struct ec_parsed *open_paren,
203 const struct ec_parsed *close_paren,
206 const struct ec_strvec *vec;
207 struct ec_node *in = value;;
208 struct ec_node *out = NULL;;
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)
218 if (!strcmp(ec_strvec_val(vec, 0), "[")) {
219 out = ec_node_option(EC_NO_ID, ec_node_clone(in));
223 } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
235 ec_node_cmd_eval_free(void *result, void *userctx)
241 static const struct ec_node_expr_eval_ops test_ops = {
242 .eval_var = ec_node_cmd_eval_var,
243 .eval_pre_op = ec_node_cmd_eval_pre_op,
244 .eval_post_op = ec_node_cmd_eval_post_op,
245 .eval_bin_op = ec_node_cmd_eval_bin_op,
246 .eval_parenthesis = ec_node_cmd_eval_parenthesis,
247 .eval_free = ec_node_cmd_eval_free,
250 static int ec_node_cmd_build(struct ec_node_cmd *node)
252 struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL;
253 struct ec_parsed *p = NULL, *child;
257 ec_node_free(node->expr);
259 ec_node_free(node->lex);
261 ec_node_free(node->cmd);
264 /* build the expression parser */
266 expr = ec_node("expr", "expr");
269 ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID, "[a-zA-Z0-9]+"));
272 ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, ","));
275 ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, "|"));
278 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "+"));
281 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "*"));
284 ret = ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "["),
285 ec_node_str(EC_NO_ID, "]"));
288 ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "("),
289 ec_node_str(EC_NO_ID, ")"));
293 /* prepend a lexer and a "many" to the expression node */
295 lex = ec_node_re_lex(EC_NO_ID,
296 ec_node_many(EC_NO_ID, ec_node_clone(expr), 1, 0));
300 ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
303 ret = ec_node_re_lex_add(lex, "[*|,()]", 1);
306 ret = ec_node_re_lex_add(lex, "\\[", 1);
309 ret = ec_node_re_lex_add(lex, "\\]", 1);
312 ret = ec_node_re_lex_add(lex, "[ ]+", 0);
316 /* parse the command expression */
318 p = ec_node_parse(lex, node->cmd_str);
323 if (!ec_parsed_matches(p))
325 if (!ec_parsed_has_child(p))
327 if (!ec_parsed_has_child(ec_parsed_get_first_child(p)))
331 cmd = ec_node("seq", EC_NO_ID);
335 EC_PARSED_FOREACH_CHILD(child, ec_parsed_get_first_child(p)) {
336 ret = ec_node_expr_eval(&result, expr, child,
340 ret = ec_node_seq_add(cmd, result);
362 ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state,
363 const struct ec_strvec *strvec)
365 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
367 if (node->cmd == NULL)
369 return ec_node_parse_child(node->cmd, state, strvec);
373 ec_node_cmd_complete(const struct ec_node *gen_node,
374 struct ec_completed *completed,
375 const struct ec_strvec *strvec)
377 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
379 if (node->cmd == NULL)
381 return ec_node_complete_child(node->cmd, completed, strvec);
384 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
386 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
389 ec_free(node->cmd_str);
390 ec_node_free(node->cmd);
391 ec_node_free(node->expr);
392 ec_node_free(node->lex);
393 for (i = 0; i < node->len; i++)
394 ec_node_free(node->table[i]);
395 ec_free(node->table);
399 static struct ec_node_type ec_node_cmd_type = {
401 .parse = ec_node_cmd_parse,
402 .complete = ec_node_cmd_complete,
403 .size = sizeof(struct ec_node_cmd),
404 .free_priv = ec_node_cmd_free_priv,
407 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
409 int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
411 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
412 struct ec_node **table;
415 assert(node != NULL);
422 if (ec_node_check_type(gen_node, &ec_node_cmd_type) < 0)
425 if (node->cmd == NULL) {
426 ret = ec_node_cmd_build(node);
431 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
437 if (ec_node_add_child(gen_node, child) < 0)
440 table[node->len] = child;
450 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
452 struct ec_node *gen_node = NULL;
453 struct ec_node_cmd *node = NULL;
454 struct ec_node *child;
458 gen_node = __ec_node(&ec_node_cmd_type, id);
459 if (gen_node == NULL)
463 node = (struct ec_node_cmd *)gen_node;
464 node->cmd_str = ec_strdup(cmd);
465 if (node->cmd_str == NULL)
471 for (child = va_arg(ap, struct ec_node *);
472 child != EC_NODE_ENDLIST;
473 child = va_arg(ap, struct ec_node *)) {
475 /* on error, don't quit the loop to avoid leaks */
476 if (fail == 1 || child == NULL ||
477 ec_node_cmd_add_child(&node->gen, child) < 0) {
488 if (ec_node_cmd_build(node) < 0)
494 ec_node_free(gen_node); /* will also free children */
498 struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
500 return __ec_node_cmd(id, cmd_str, EC_NODE_ENDLIST);
503 /* LCOV_EXCL_START */
504 static int ec_node_cmd_testcase(void)
506 struct ec_node *node;
509 node = EC_NODE_CMD(EC_NO_ID,
510 "command [option] (subset1, subset2, subset3) x|y z*",
511 ec_node_int("x", 0, 10, 10),
512 ec_node_int("y", 20, 30, 10)
515 EC_LOG(EC_LOG_ERR, "cannot create node\n");
518 ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
519 ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
520 ret |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
521 ret |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
523 ret |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
524 ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
527 node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
528 ec_node_int("count", 0, 10, 10));
530 EC_LOG(EC_LOG_ERR, "cannot create node\n");
533 ret |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
535 ret |= EC_TEST_CHECK_COMPLETE(node,
537 "good", EC_NODE_ENDLIST);
538 ret |= EC_TEST_CHECK_COMPLETE(node,
539 "g", EC_NODE_ENDLIST,
540 "good", EC_NODE_ENDLIST);
541 ret |= EC_TEST_CHECK_COMPLETE(node,
542 "good", "morning", "", EC_NODE_ENDLIST,
543 "bob", "bobby", "michael", EC_NODE_ENDLIST);
551 static struct ec_test ec_node_cmd_test = {
553 .test = ec_node_cmd_testcase,
556 EC_TEST_REGISTER(ec_node_cmd_test);