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_expr.h>
23 #include <ecoli_node_str.h>
24 #include <ecoli_node_or.h>
25 #include <ecoli_node_subset.h>
26 #include <ecoli_node_int.h>
27 #include <ecoli_node_many.h>
28 #include <ecoli_node_seq.h>
29 #include <ecoli_node_option.h>
30 #include <ecoli_node_re.h>
31 #include <ecoli_node_re_lex.h>
32 #include <ecoli_node_cmd.h>
34 EC_LOG_TYPE_REGISTER(node_cmd);
38 char *cmd_str; /* the command string. */
39 struct ec_node *cmd; /* the command node. */
40 struct ec_node *parser; /* the expression parser. */
41 struct ec_node *expr; /* the expression parser without lexer. */
42 struct ec_node **table; /* table of node referenced in command. */
43 unsigned int len; /* len of the table. */
46 /* passed as user context to expression parser */
47 struct ec_node_cmd_ctx {
48 struct ec_node **table;
53 ec_node_cmd_eval_var(void **result, void *userctx,
54 const struct ec_parse *var)
56 const struct ec_strvec *vec;
57 struct ec_node_cmd_ctx *ctx = userctx;
58 struct ec_node *eval = NULL;
62 /* get parsed string vector, it should contain only one str */
63 vec = ec_parse_strvec(var);
64 if (ec_strvec_len(vec) != 1) {
68 str = ec_strvec_val(vec, 0);
70 for (i = 0; i < ctx->len; i++) {
71 id = ec_node_id(ctx->table[i]);
76 /* if id matches, use a node provided by the user... */
77 eval = ec_node_clone(ctx->table[i]);
83 /* ...or create a string node */
85 eval = ec_node_str(EC_NO_ID, str);
96 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
97 const struct ec_parse *operator)
109 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
110 const struct ec_parse *operator)
112 const struct ec_strvec *vec;
113 struct ec_node *in = operand;;
114 struct ec_node *out = NULL;;
118 /* get parsed string vector, it should contain only one str */
119 vec = ec_parse_strvec(operator);
120 if (ec_strvec_len(vec) != 1) {
125 if (!strcmp(ec_strvec_val(vec, 0), "*")) {
126 out = ec_node_many(EC_NO_ID,
127 ec_node_clone(in), 0, 0);
141 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
142 const struct ec_parse *operator, void *operand2)
145 const struct ec_strvec *vec;
146 struct ec_node *out = NULL;
147 struct ec_node *in1 = operand1;
148 struct ec_node *in2 = operand2;
152 /* get parsed string vector, it should contain only one str */
153 vec = ec_parse_strvec(operator);
154 if (ec_strvec_len(vec) > 1) {
159 if (ec_strvec_len(vec) == 0) {
160 if (!strcmp(in1->type->name, "seq")) {
161 if (ec_node_seq_add(in1, ec_node_clone(in2)) < 0)
166 out = EC_NODE_SEQ(EC_NO_ID, ec_node_clone(in1),
174 } else if (!strcmp(ec_strvec_val(vec, 0), "|")) {
175 if (!strcmp(in2->type->name, "or")) {
176 if (ec_node_or_add(in2, ec_node_clone(in1)) < 0)
180 } else if (!strcmp(in1->type->name, "or")) {
181 if (ec_node_or_add(in1, ec_node_clone(in2)) < 0)
186 out = EC_NODE_OR(EC_NO_ID, ec_node_clone(in1),
194 } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
195 if (!strcmp(in2->type->name, "subset")) {
196 if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
200 } else if (!strcmp(in1->type->name, "subset")) {
201 if (ec_node_subset_add(in1, ec_node_clone(in2)) < 0)
206 out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
223 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
224 const struct ec_parse *open_paren,
225 const struct ec_parse *close_paren,
228 const struct ec_strvec *vec;
229 struct ec_node *in = value;;
230 struct ec_node *out = NULL;;
235 /* get parsed string vector, it should contain only one str */
236 vec = ec_parse_strvec(open_paren);
237 if (ec_strvec_len(vec) != 1) {
242 if (!strcmp(ec_strvec_val(vec, 0), "[")) {
243 out = ec_node_option(EC_NO_ID, ec_node_clone(in));
247 } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
260 ec_node_cmd_eval_free(void *result, void *userctx)
266 static const struct ec_node_expr_eval_ops expr_ops = {
267 .eval_var = ec_node_cmd_eval_var,
268 .eval_pre_op = ec_node_cmd_eval_pre_op,
269 .eval_post_op = ec_node_cmd_eval_post_op,
270 .eval_bin_op = ec_node_cmd_eval_bin_op,
271 .eval_parenthesis = ec_node_cmd_eval_parenthesis,
272 .eval_free = ec_node_cmd_eval_free,
275 static struct ec_node *
276 ec_node_cmd_build_expr(void)
278 struct ec_node *expr = NULL;
281 /* build the expression parser */
282 expr = ec_node("expr", "expr");
285 ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID,
289 ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, ","));
292 ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, "|"));
295 ret = ec_node_expr_add_bin_op(expr, ec_node("empty", EC_NO_ID));
298 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "+"));
301 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "*"));
304 ret = ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "["),
305 ec_node_str(EC_NO_ID, "]"));
308 ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "("),
309 ec_node_str(EC_NO_ID, ")"));
320 static struct ec_node *
321 ec_node_cmd_build_parser(struct ec_node *expr)
323 struct ec_node *lex = NULL;
326 /* prepend a lexer to the expression node */
327 lex = ec_node_re_lex(EC_NO_ID, ec_node_clone(expr));
331 ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1);
334 ret = ec_node_re_lex_add(lex, "[*|,()]", 1);
337 ret = ec_node_re_lex_add(lex, "\\[", 1);
340 ret = ec_node_re_lex_add(lex, "\\]", 1);
343 ret = ec_node_re_lex_add(lex, "[ ]+", 0);
355 static struct ec_node *
356 ec_node_cmd_build(struct ec_node_cmd *node, const char *cmd_str,
357 struct ec_node **table, size_t len)
359 struct ec_node_cmd_ctx ctx = { table, len };
360 struct ec_parse *p = NULL;
364 /* parse the command expression */
365 p = ec_node_parse(node->parser, cmd_str);
369 if (!ec_parse_matches(p)) {
373 if (!ec_parse_has_child(p)) {
378 ret = ec_node_expr_eval(&result, node->expr,
379 ec_parse_get_first_child(p),
393 ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parse *state,
394 const struct ec_strvec *strvec)
396 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
398 return ec_node_parse_child(node->cmd, state, strvec);
402 ec_node_cmd_complete(const struct ec_node *gen_node,
403 struct ec_comp *comp,
404 const struct ec_strvec *strvec)
406 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
408 return ec_node_complete_child(node->cmd, comp, strvec);
411 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
413 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
416 ec_free(node->cmd_str);
417 ec_node_free(node->expr);
418 ec_node_free(node->parser);
419 for (i = 0; i < node->len; i++)
420 ec_node_free(node->table[i]);
421 ec_free(node->table);
424 static const struct ec_config_schema ec_node_cmd_subschema[] = {
426 .desc = "A child node whose id is referenced in the expression.",
427 .type = EC_CONFIG_TYPE_NODE,
431 static const struct ec_config_schema ec_node_cmd_schema[] = {
434 .desc = "The expression to match. Supported operators "
435 "are or '|', list ',', many '+', many-or-zero '*', "
436 "option '[]', group '()'. An identifier (alphanumeric) can "
437 "reference a node whose node_id matches. Else it is "
438 "interpreted as ec_node_str() matching this string. "
439 "Example: command [option] (subset1, subset2) x|y",
440 .type = EC_CONFIG_TYPE_STRING,
444 .desc = "The list of children nodes.",
445 .type = EC_CONFIG_TYPE_LIST,
446 .subschema = ec_node_cmd_subschema,
447 .subschema_len = EC_COUNT_OF(ec_node_cmd_subschema),
451 static int ec_node_cmd_set_config(struct ec_node *gen_node,
452 const struct ec_config *config)
454 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
455 const struct ec_config *expr = NULL, *children = NULL, *child;
456 struct ec_node *cmd = NULL;
457 struct ec_node **table = NULL;
458 char *cmd_str = NULL;
461 /* retrieve config locally */
462 expr = ec_config_dict_get(config, "expr");
468 children = ec_config_dict_get(config, "children");
469 if (children == NULL) {
474 cmd_str = ec_strdup(expr->string);
479 TAILQ_FOREACH(child, &children->list, next)
482 table = ec_malloc(n * sizeof(*table));
487 TAILQ_FOREACH(child, &children->list, next) {
488 table[n] = ec_node_clone(child->node);
492 /* parse expression to build the cmd child node */
493 cmd = ec_node_cmd_build(node, cmd_str, table, n);
497 ec_node_free(node->cmd);
499 ec_free(node->cmd_str);
500 node->cmd_str = cmd_str;
501 for (i = 0; i < node->len; i++)
502 ec_node_free(node->table[i]);
503 ec_free(node->table);
511 for (i = 0; i < n; i++)
512 ec_node_free(table[i]);
521 ec_node_cmd_get_children_count(const struct ec_node *gen_node)
523 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
525 if (node->cmd == NULL)
530 static struct ec_node *
531 ec_node_cmd_get_child(const struct ec_node *gen_node, size_t i)
533 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 .schema_len = EC_COUNT_OF(ec_node_cmd_schema),
545 .set_config = ec_node_cmd_set_config,
546 .parse = ec_node_cmd_parse,
547 .complete = ec_node_cmd_complete,
548 .size = sizeof(struct ec_node_cmd),
549 .free_priv = ec_node_cmd_free_priv,
550 .get_children_count = ec_node_cmd_get_children_count,
551 .get_child = ec_node_cmd_get_child,
554 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
556 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
558 struct ec_config *config = NULL, *children = NULL;
559 struct ec_node *gen_node = NULL;
560 struct ec_node_cmd *node = NULL;
561 struct ec_node *child;
566 child = va_arg(ap, struct ec_node *);
568 gen_node = __ec_node(&ec_node_cmd_type, id);
569 if (gen_node == NULL)
570 goto fail_free_children;
571 node = (struct ec_node_cmd *)gen_node;
573 node->expr = ec_node_cmd_build_expr();
574 if (node->expr == NULL)
575 goto fail_free_children;
577 node->parser = ec_node_cmd_build_parser(node->expr);
578 if (node->parser == NULL)
579 goto fail_free_children;
581 config = ec_config_dict();
583 goto fail_free_children;
585 if (ec_config_dict_set(config, "expr", ec_config_string(cmd)) < 0)
586 goto fail_free_children;
588 children = ec_config_list();
589 if (children == NULL)
590 goto fail_free_children;
592 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) {
594 goto fail_free_children;
596 if (ec_config_list_add(children, ec_config_node(child)) < 0)
597 goto fail_free_children;
600 if (ec_config_dict_set(config, "children", children) < 0) {
601 children = NULL; /* freed */
606 ret = ec_node_set_config(gen_node, config);
607 config = NULL; /* freed */
616 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *))
619 ec_node_free(gen_node); /* will also free added children */
620 ec_config_free(children);
621 ec_config_free(config);
627 /* LCOV_EXCL_START */
628 static int ec_node_cmd_testcase(void)
630 struct ec_node *node;
633 node = EC_NODE_CMD(EC_NO_ID,
634 "command [option] (subset1, subset2, subset3, subset4) x|y z*",
635 ec_node_int("x", 0, 10, 10),
636 ec_node_int("y", 20, 30, 10)
639 EC_LOG(EC_LOG_ERR, "cannot create node\n");
642 testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
643 testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "subset1", "1");
644 testres |= EC_TEST_CHECK_PARSE(node, 4, "command", "subset3", "subset2",
646 testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "subset2", "subset3",
648 testres |= EC_TEST_CHECK_PARSE(node, 6, "command", "subset3", "subset1",
649 "subset4", "subset2", "4");
650 testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
651 testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
652 testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
654 testres |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
655 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
658 node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
659 ec_node_int("count", 0, 10, 10));
661 EC_LOG(EC_LOG_ERR, "cannot create node\n");
664 testres |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
666 testres |= EC_TEST_CHECK_COMPLETE(node,
668 "good", EC_NODE_ENDLIST);
669 testres |= EC_TEST_CHECK_COMPLETE(node,
670 "g", EC_NODE_ENDLIST,
671 "good", EC_NODE_ENDLIST);
672 testres |= EC_TEST_CHECK_COMPLETE(node,
673 "good", "morning", "", EC_NODE_ENDLIST,
674 "bob", "bobby", "michael", EC_NODE_ENDLIST);
678 node = EC_NODE_CMD(EC_NO_ID, "[foo [bar]]");
680 EC_LOG(EC_LOG_ERR, "cannot create node\n");
683 testres |= EC_TEST_CHECK_PARSE(node, 0);
684 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
685 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
686 testres |= EC_TEST_CHECK_PARSE(node, 0, "x");
693 static struct ec_test ec_node_cmd_test = {
695 .test = ec_node_cmd_testcase,
698 EC_TEST_REGISTER(ec_node_cmd_test);