1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
14 #include <ecoli_malloc.h>
15 #include <ecoli_log.h>
16 #include <ecoli_test.h>
17 #include <ecoli_strvec.h>
18 #include <ecoli_node.h>
19 #include <ecoli_config.h>
20 #include <ecoli_parse.h>
21 #include <ecoli_complete.h>
22 #include <ecoli_node_helper.h>
23 #include <ecoli_node_expr.h>
24 #include <ecoli_node_str.h>
25 #include <ecoli_node_or.h>
26 #include <ecoli_node_subset.h>
27 #include <ecoli_node_int.h>
28 #include <ecoli_node_many.h>
29 #include <ecoli_node_seq.h>
30 #include <ecoli_node_option.h>
31 #include <ecoli_node_re.h>
32 #include <ecoli_node_re_lex.h>
33 #include <ecoli_node_cmd.h>
35 EC_LOG_TYPE_REGISTER(node_cmd);
39 char *cmd_str; /* the command string. */
40 struct ec_node *cmd; /* the command node. */
41 struct ec_node *parser; /* the expression parser. */
42 struct ec_node *expr; /* the expression parser without lexer. */
43 struct ec_node **table; /* table of node referenced in command. */
44 unsigned int len; /* len of the table. */
47 /* passed as user context to expression parser */
48 struct ec_node_cmd_ctx {
49 struct ec_node **table;
54 ec_node_cmd_eval_var(void **result, void *userctx,
55 const struct ec_parse *var)
57 const struct ec_strvec *vec;
58 struct ec_node_cmd_ctx *ctx = userctx;
59 struct ec_node *eval = NULL;
63 /* get parsed string vector, it should contain only one str */
64 vec = ec_parse_strvec(var);
65 if (ec_strvec_len(vec) != 1) {
69 str = ec_strvec_val(vec, 0);
71 for (i = 0; i < ctx->len; i++) {
72 id = ec_node_id(ctx->table[i]);
77 /* if id matches, use a node provided by the user... */
78 eval = ec_node_clone(ctx->table[i]);
84 /* ...or create a string node */
86 eval = ec_node_str(EC_NO_ID, str);
97 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
98 const struct ec_parse *operator)
110 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
111 const struct ec_parse *operator)
113 const struct ec_strvec *vec;
114 struct ec_node *in = operand;;
115 struct ec_node *out = NULL;;
119 /* get parsed string vector, it should contain only one str */
120 vec = ec_parse_strvec(operator);
121 if (ec_strvec_len(vec) != 1) {
126 if (!strcmp(ec_strvec_val(vec, 0), "*")) {
127 out = ec_node_many(EC_NO_ID,
128 ec_node_clone(in), 0, 0);
142 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
143 const struct ec_parse *operator, void *operand2)
146 const struct ec_strvec *vec;
147 struct ec_node *out = NULL;
148 struct ec_node *in1 = operand1;
149 struct ec_node *in2 = operand2;
153 /* get parsed string vector, it should contain only one str */
154 vec = ec_parse_strvec(operator);
155 if (ec_strvec_len(vec) > 1) {
160 if (ec_strvec_len(vec) == 0) {
161 if (!strcmp(in1->type->name, "seq")) {
162 if (ec_node_seq_add(in1, ec_node_clone(in2)) < 0)
167 out = EC_NODE_SEQ(EC_NO_ID, ec_node_clone(in1),
175 } else if (!strcmp(ec_strvec_val(vec, 0), "|")) {
176 if (!strcmp(in2->type->name, "or")) {
177 if (ec_node_or_add(in2, ec_node_clone(in1)) < 0)
181 } else if (!strcmp(in1->type->name, "or")) {
182 if (ec_node_or_add(in1, ec_node_clone(in2)) < 0)
187 out = EC_NODE_OR(EC_NO_ID, ec_node_clone(in1),
195 } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
196 if (!strcmp(in2->type->name, "subset")) {
197 if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
201 } else if (!strcmp(in1->type->name, "subset")) {
202 if (ec_node_subset_add(in1, ec_node_clone(in2)) < 0)
207 out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
224 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
225 const struct ec_parse *open_paren,
226 const struct ec_parse *close_paren,
229 const struct ec_strvec *vec;
230 struct ec_node *in = value;;
231 struct ec_node *out = NULL;;
236 /* get parsed string vector, it should contain only one str */
237 vec = ec_parse_strvec(open_paren);
238 if (ec_strvec_len(vec) != 1) {
243 if (!strcmp(ec_strvec_val(vec, 0), "[")) {
244 out = ec_node_option(EC_NO_ID, ec_node_clone(in));
248 } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
261 ec_node_cmd_eval_free(void *result, void *userctx)
267 static const struct ec_node_expr_eval_ops expr_ops = {
268 .eval_var = ec_node_cmd_eval_var,
269 .eval_pre_op = ec_node_cmd_eval_pre_op,
270 .eval_post_op = ec_node_cmd_eval_post_op,
271 .eval_bin_op = ec_node_cmd_eval_bin_op,
272 .eval_parenthesis = ec_node_cmd_eval_parenthesis,
273 .eval_free = ec_node_cmd_eval_free,
276 static struct ec_node *
277 ec_node_cmd_build_expr(void)
279 struct ec_node *expr = NULL;
282 /* build the expression parser */
283 expr = ec_node("expr", "expr");
286 ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID,
290 ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, ","));
293 ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, "|"));
296 ret = ec_node_expr_add_bin_op(expr, ec_node("empty", EC_NO_ID));
299 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "+"));
302 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "*"));
305 ret = ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "["),
306 ec_node_str(EC_NO_ID, "]"));
309 ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "("),
310 ec_node_str(EC_NO_ID, ")"));
321 static struct ec_node *
322 ec_node_cmd_build_parser(struct ec_node *expr)
324 struct ec_node *lex = NULL;
327 /* prepend a lexer to the expression node */
328 lex = ec_node_re_lex(EC_NO_ID, ec_node_clone(expr));
332 ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
335 ret = ec_node_re_lex_add(lex, "[*|,()]", 1);
338 ret = ec_node_re_lex_add(lex, "\\[", 1);
341 ret = ec_node_re_lex_add(lex, "\\]", 1);
344 ret = ec_node_re_lex_add(lex, "[ ]+", 0);
356 static struct ec_node *
357 ec_node_cmd_build(struct ec_node_cmd *node, const char *cmd_str,
358 struct ec_node **table, size_t len)
360 struct ec_node_cmd_ctx ctx = { table, len };
361 struct ec_parse *p = NULL;
365 /* parse the command expression */
366 p = ec_node_parse(node->parser, cmd_str);
370 if (!ec_parse_matches(p)) {
374 if (!ec_parse_has_child(p)) {
379 ret = ec_node_expr_eval(&result, node->expr,
380 ec_parse_get_first_child(p),
394 ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parse *state,
395 const struct ec_strvec *strvec)
397 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
399 return ec_node_parse_child(node->cmd, state, strvec);
403 ec_node_cmd_complete(const struct ec_node *gen_node,
404 struct ec_comp *comp,
405 const struct ec_strvec *strvec)
407 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
409 return ec_node_complete_child(node->cmd, comp, strvec);
412 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
414 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
417 ec_free(node->cmd_str);
418 node->cmd_str = NULL;
419 ec_node_free(node->expr);
421 ec_node_free(node->parser);
423 ec_node_free(node->cmd);
425 for (i = 0; i < node->len; i++)
426 ec_node_free(node->table[i]);
427 ec_free(node->table);
432 static const struct ec_config_schema ec_node_cmd_subschema[] = {
434 .desc = "A child node whose id is referenced in the expression.",
435 .type = EC_CONFIG_TYPE_NODE,
438 .type = EC_CONFIG_TYPE_NONE,
442 static const struct ec_config_schema ec_node_cmd_schema[] = {
445 .desc = "The expression to match. Supported operators "
446 "are or '|', list ',', many '+', many-or-zero '*', "
447 "option '[]', group '()'. An identifier (alphanumeric) can "
448 "reference a node whose node_id matches. Else it is "
449 "interpreted as ec_node_str() matching this string. "
450 "Example: command [option] (subset1, subset2) x|y",
451 .type = EC_CONFIG_TYPE_STRING,
455 .desc = "The list of children nodes.",
456 .type = EC_CONFIG_TYPE_LIST,
457 .subschema = ec_node_cmd_subschema,
460 .type = EC_CONFIG_TYPE_NONE,
464 static int ec_node_cmd_set_config(struct ec_node *gen_node,
465 const struct ec_config *config)
467 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
468 const struct ec_config *expr = NULL;
469 struct ec_node *cmd = NULL;
470 struct ec_node **table = NULL;
471 char *cmd_str = NULL;
474 /* retrieve config locally */
475 expr = ec_config_dict_get(config, "expr");
481 table = ec_node_config_node_list_to_table(
482 ec_config_dict_get(config, "children"), &len);
486 cmd_str = ec_strdup(expr->string);
490 /* parse expression to build the cmd child node */
491 cmd = ec_node_cmd_build(node, cmd_str, table, len);
495 /* ok, store the config */
496 ec_node_free(node->cmd);
498 ec_free(node->cmd_str);
499 node->cmd_str = cmd_str;
500 for (i = 0; i < node->len; i++)
501 ec_node_free(node->table[i]);
502 ec_free(node->table);
509 for (i = 0; i < len; i++)
510 ec_node_free(table[i]);
518 ec_node_cmd_get_children_count(const struct ec_node *gen_node)
520 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
522 if (node->cmd == NULL)
528 ec_node_cmd_get_child(const struct ec_node *gen_node, size_t i,
529 struct ec_node **child, unsigned int *refs)
531 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
541 static struct ec_node_type ec_node_cmd_type = {
543 .schema = ec_node_cmd_schema,
544 .set_config = ec_node_cmd_set_config,
545 .parse = ec_node_cmd_parse,
546 .complete = ec_node_cmd_complete,
547 .size = sizeof(struct ec_node_cmd),
548 .free_priv = ec_node_cmd_free_priv,
549 .get_children_count = ec_node_cmd_get_children_count,
550 .get_child = ec_node_cmd_get_child,
553 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
555 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
557 struct ec_config *config = NULL, *children = NULL;
558 struct ec_node *gen_node = NULL;
559 struct ec_node_cmd *node = NULL;
563 /* this block must stay first, it frees the nodes on error */
565 children = ec_node_config_node_list_from_vargs(ap);
567 if (children == NULL)
570 gen_node = ec_node_from_type(&ec_node_cmd_type, id);
571 if (gen_node == NULL)
573 node = (struct ec_node_cmd *)gen_node;
575 node->expr = ec_node_cmd_build_expr();
576 if (node->expr == NULL)
579 node->parser = ec_node_cmd_build_parser(node->expr);
580 if (node->parser == NULL)
583 config = ec_config_dict();
587 if (ec_config_dict_set(config, "expr", ec_config_string(cmd)) < 0)
590 if (ec_config_dict_set(config, "children", children) < 0) {
591 children = NULL; /* freed */
596 ret = ec_node_set_config(gen_node, config);
597 config = NULL; /* freed */
604 ec_node_free(gen_node); /* will also free added children */
605 ec_config_free(children);
606 ec_config_free(config);
611 /* LCOV_EXCL_START */
612 static int ec_node_cmd_testcase(void)
614 struct ec_node *node;
617 node = EC_NODE_CMD(EC_NO_ID,
618 "command [option] (subset1, subset2, subset3, subset4) x|y z*",
619 ec_node_int("x", 0, 10, 10),
620 ec_node_int("y", 20, 30, 10)
623 EC_LOG(EC_LOG_ERR, "cannot create node\n");
626 testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
627 testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "subset1", "1");
628 testres |= EC_TEST_CHECK_PARSE(node, 4, "command", "subset3", "subset2",
630 testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "subset2", "subset3",
632 testres |= EC_TEST_CHECK_PARSE(node, 6, "command", "subset3", "subset1",
633 "subset4", "subset2", "4");
634 testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
635 testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
636 testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
638 testres |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
639 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
642 node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
643 ec_node_int("count", 0, 10, 10));
645 EC_LOG(EC_LOG_ERR, "cannot create node\n");
648 testres |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
650 testres |= EC_TEST_CHECK_COMPLETE(node,
652 "good", EC_NODE_ENDLIST);
653 testres |= EC_TEST_CHECK_COMPLETE(node,
654 "g", EC_NODE_ENDLIST,
655 "good", EC_NODE_ENDLIST);
656 testres |= EC_TEST_CHECK_COMPLETE(node,
657 "good", "morning", "", EC_NODE_ENDLIST,
658 "bob", "bobby", "michael", EC_NODE_ENDLIST);
662 node = EC_NODE_CMD(EC_NO_ID, "[foo [bar]]");
664 EC_LOG(EC_LOG_ERR, "cannot create node\n");
667 testres |= EC_TEST_CHECK_PARSE(node, 0);
668 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
669 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
670 testres |= EC_TEST_CHECK_PARSE(node, 0, "x");
677 static struct ec_test ec_node_cmd_test = {
679 .test = ec_node_cmd_testcase,
682 EC_TEST_REGISTER(ec_node_cmd_test);