1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
14 #include <ecoli_init.h>
15 #include <ecoli_malloc.h>
16 #include <ecoli_log.h>
17 #include <ecoli_test.h>
18 #include <ecoli_strvec.h>
19 #include <ecoli_node.h>
20 #include <ecoli_config.h>
21 #include <ecoli_parse.h>
22 #include <ecoli_complete.h>
23 #include <ecoli_node_helper.h>
24 #include <ecoli_node_expr.h>
25 #include <ecoli_node_str.h>
26 #include <ecoli_node_or.h>
27 #include <ecoli_node_subset.h>
28 #include <ecoli_node_int.h>
29 #include <ecoli_node_many.h>
30 #include <ecoli_node_seq.h>
31 #include <ecoli_node_option.h>
32 #include <ecoli_node_re.h>
33 #include <ecoli_node_re_lex.h>
34 #include <ecoli_node_cmd.h>
36 EC_LOG_TYPE_REGISTER(node_cmd);
38 static struct ec_node *ec_node_cmd_parser; /* the expression parser. */
39 static struct ec_node *ec_node_cmd_expr; /* the expr parser without lexer. */
42 char *cmd_str; /* the command string. */
43 struct ec_node *cmd; /* the command node. */
44 struct ec_node **table; /* table of node referenced in command. */
45 unsigned int len; /* len of the table. */
48 /* passed as user context to expression parser */
49 struct ec_node_cmd_ctx {
50 struct ec_node **table;
55 ec_node_cmd_eval_var(void **result, void *userctx,
56 const struct ec_pnode *var)
58 const struct ec_strvec *vec;
59 struct ec_node_cmd_ctx *ctx = userctx;
60 struct ec_node *eval = NULL;
64 /* get parsed string vector, it should contain only one str */
65 vec = ec_pnode_get_strvec(var);
66 if (ec_strvec_len(vec) != 1) {
70 str = ec_strvec_val(vec, 0);
72 for (i = 0; i < ctx->len; i++) {
73 id = ec_node_id(ctx->table[i]);
78 /* if id matches, use a node provided by the user... */
79 eval = ec_node_clone(ctx->table[i]);
85 /* ...or create a string node */
87 eval = ec_node_str(EC_NO_ID, str);
98 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
99 const struct ec_pnode *operator)
111 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
112 const struct ec_pnode *operator)
114 const struct ec_strvec *vec;
115 struct ec_node *in = operand;;
116 struct ec_node *out = NULL;;
120 /* get parsed string vector, it should contain only one str */
121 vec = ec_pnode_get_strvec(operator);
122 if (ec_strvec_len(vec) != 1) {
127 if (!strcmp(ec_strvec_val(vec, 0), "*")) {
128 out = ec_node_many(EC_NO_ID,
129 ec_node_clone(in), 0, 0);
143 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
144 const struct ec_pnode *operator, void *operand2)
147 const struct ec_strvec *vec;
148 struct ec_node *out = NULL;
149 struct ec_node *in1 = operand1;
150 struct ec_node *in2 = operand2;
154 /* get parsed string vector, it should contain only one str */
155 vec = ec_pnode_get_strvec(operator);
156 if (ec_strvec_len(vec) > 1) {
161 if (ec_strvec_len(vec) == 0) {
162 if (!strcmp(ec_node_get_type_name(in1), "seq")) {
163 if (ec_node_seq_add(in1, ec_node_clone(in2)) < 0)
168 out = EC_NODE_SEQ(EC_NO_ID, ec_node_clone(in1),
176 } else if (!strcmp(ec_strvec_val(vec, 0), "|")) {
177 if (!strcmp(ec_node_get_type_name(in2), "or")) {
178 if (ec_node_or_add(in2, ec_node_clone(in1)) < 0)
182 } else if (!strcmp(ec_node_get_type_name(in1), "or")) {
183 if (ec_node_or_add(in1, ec_node_clone(in2)) < 0)
188 out = EC_NODE_OR(EC_NO_ID, ec_node_clone(in1),
196 } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
197 if (!strcmp(ec_node_get_type_name(in2), "subset")) {
198 if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
202 } else if (!strcmp(ec_node_get_type_name(in1), "subset")) {
203 if (ec_node_subset_add(in1, ec_node_clone(in2)) < 0)
208 out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
225 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
226 const struct ec_pnode *open_paren,
227 const struct ec_pnode *close_paren,
230 const struct ec_strvec *vec;
231 struct ec_node *in = value;;
232 struct ec_node *out = NULL;;
237 /* get parsed string vector, it should contain only one str */
238 vec = ec_pnode_get_strvec(open_paren);
239 if (ec_strvec_len(vec) != 1) {
244 if (!strcmp(ec_strvec_val(vec, 0), "[")) {
245 out = ec_node_option(EC_NO_ID, ec_node_clone(in));
249 } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
262 ec_node_cmd_eval_free(void *result, void *userctx)
268 static const struct ec_node_expr_eval_ops expr_ops = {
269 .eval_var = ec_node_cmd_eval_var,
270 .eval_pre_op = ec_node_cmd_eval_pre_op,
271 .eval_post_op = ec_node_cmd_eval_post_op,
272 .eval_bin_op = ec_node_cmd_eval_bin_op,
273 .eval_parenthesis = ec_node_cmd_eval_parenthesis,
274 .eval_free = ec_node_cmd_eval_free,
277 static struct ec_node *
278 ec_node_cmd_build_expr(void)
280 struct ec_node *expr = NULL;
283 /* build the expression parser */
284 expr = ec_node("expr", "expr");
287 ret = ec_node_expr_set_val_node(expr, ec_node_re(EC_NO_ID,
291 ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, ","));
294 ret = ec_node_expr_add_bin_op(expr, ec_node_str(EC_NO_ID, "|"));
297 ret = ec_node_expr_add_bin_op(expr, ec_node("empty", EC_NO_ID));
300 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "+"));
303 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "*"));
306 ret = ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "["),
307 ec_node_str(EC_NO_ID, "]"));
310 ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "("),
311 ec_node_str(EC_NO_ID, ")"));
322 static struct ec_node *
323 ec_node_cmd_build_parser(struct ec_node *expr)
325 struct ec_node *lex = NULL;
328 /* prepend a lexer to the expression node */
329 lex = ec_node_re_lex(EC_NO_ID, ec_node_clone(expr));
333 ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1, NULL);
336 ret = ec_node_re_lex_add(lex, "[*|,()]", 1, NULL);
339 ret = ec_node_re_lex_add(lex, "\\[", 1, NULL);
342 ret = ec_node_re_lex_add(lex, "\\]", 1, NULL);
345 ret = ec_node_re_lex_add(lex, "[ ]+", 0, NULL);
357 static struct ec_node *
358 ec_node_cmd_build(const char *cmd_str, struct ec_node **table, size_t len)
360 struct ec_node_cmd_ctx ctx = { table, len };
361 struct ec_pnode *p = NULL;
365 /* parse the command expression */
366 p = ec_parse(ec_node_cmd_parser, cmd_str);
370 if (!ec_pnode_matches(p)) {
375 ret = ec_node_expr_eval(&result, ec_node_cmd_expr,
376 ec_pnode_get_first_child(p),
390 ec_node_cmd_parse(const struct ec_node *node, struct ec_pnode *pstate,
391 const struct ec_strvec *strvec)
393 struct ec_node_cmd *priv = ec_node_priv(node);
395 return ec_parse_child(priv->cmd, pstate, strvec);
399 ec_node_cmd_complete(const struct ec_node *node,
400 struct ec_comp *comp,
401 const struct ec_strvec *strvec)
403 struct ec_node_cmd *priv = ec_node_priv(node);
405 return ec_complete_child(priv->cmd, comp, strvec);
408 static void ec_node_cmd_free_priv(struct ec_node *node)
410 struct ec_node_cmd *priv = ec_node_priv(node);
413 ec_free(priv->cmd_str);
414 priv->cmd_str = NULL;
415 ec_node_free(priv->cmd);
417 for (i = 0; i < priv->len; i++)
418 ec_node_free(priv->table[i]);
419 ec_free(priv->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,
430 .type = EC_CONFIG_TYPE_NONE,
434 static const struct ec_config_schema ec_node_cmd_schema[] = {
437 .desc = "The expression to match. Supported operators "
438 "are or '|', list ',', many '+', many-or-zero '*', "
439 "option '[]', group '()'. An identifier (alphanumeric) can "
440 "reference a node whose node_id matches. Else it is "
441 "interpreted as ec_node_str() matching this string. "
442 "Example: command [option] (subset1, subset2) x|y",
443 .type = EC_CONFIG_TYPE_STRING,
447 .desc = "The list of children nodes.",
448 .type = EC_CONFIG_TYPE_LIST,
449 .subschema = ec_node_cmd_subschema,
452 .type = EC_CONFIG_TYPE_NONE,
456 static int ec_node_cmd_set_config(struct ec_node *node,
457 const struct ec_config *config)
459 struct ec_node_cmd *priv = ec_node_priv(node);
460 const struct ec_config *expr = NULL;
461 struct ec_node *cmd = NULL;
462 struct ec_node **table = NULL;
463 char *cmd_str = NULL;
466 /* retrieve config locally */
467 expr = ec_config_dict_get(config, "expr");
473 table = ec_node_config_node_list_to_table(
474 ec_config_dict_get(config, "children"), &len);
478 cmd_str = ec_strdup(expr->string);
482 /* parse expression to build the cmd child node */
483 cmd = ec_node_cmd_build(cmd_str, table, len);
487 /* ok, store the config */
488 ec_node_free(priv->cmd);
490 ec_free(priv->cmd_str);
491 priv->cmd_str = cmd_str;
492 for (i = 0; i < priv->len; i++)
493 ec_node_free(priv->table[i]);
494 ec_free(priv->table);
501 for (i = 0; i < len; i++)
502 ec_node_free(table[i]);
510 ec_node_cmd_get_children_count(const struct ec_node *node)
512 struct ec_node_cmd *priv = ec_node_priv(node);
514 if (priv->cmd == NULL)
520 ec_node_cmd_get_child(const struct ec_node *node, size_t i,
521 struct ec_node **child, unsigned int *refs)
523 struct ec_node_cmd *priv = ec_node_priv(node);
533 static struct ec_node_type ec_node_cmd_type = {
535 .schema = ec_node_cmd_schema,
536 .set_config = ec_node_cmd_set_config,
537 .parse = ec_node_cmd_parse,
538 .complete = ec_node_cmd_complete,
539 .size = sizeof(struct ec_node_cmd),
540 .free_priv = ec_node_cmd_free_priv,
541 .get_children_count = ec_node_cmd_get_children_count,
542 .get_child = ec_node_cmd_get_child,
545 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
547 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
549 struct ec_config *config = NULL, *children = NULL;
550 struct ec_node *node = NULL;
554 /* this block must stay first, it frees the nodes on error */
556 children = ec_node_config_node_list_from_vargs(ap);
558 if (children == NULL)
561 node = ec_node_from_type(&ec_node_cmd_type, id);
565 config = ec_config_dict();
569 if (ec_config_dict_set(config, "expr", ec_config_string(cmd)) < 0)
572 if (ec_config_dict_set(config, "children", children) < 0) {
573 children = NULL; /* freed */
578 ret = ec_node_set_config(node, config);
579 config = NULL; /* freed */
586 ec_node_free(node); /* will also free added children */
587 ec_config_free(children);
588 ec_config_free(config);
593 static int ec_node_cmd_init_func(void)
595 ec_node_cmd_expr = ec_node_cmd_build_expr();
596 if (ec_node_cmd_expr == NULL)
599 ec_node_cmd_parser = ec_node_cmd_build_parser(ec_node_cmd_expr);
600 if (ec_node_cmd_parser == NULL)
606 EC_LOG(EC_LOG_ERR, "Failed to initialize command parser\n");
607 ec_node_free(ec_node_cmd_expr);
608 ec_node_cmd_expr = NULL;
609 ec_node_free(ec_node_cmd_parser);
610 ec_node_cmd_parser = NULL;
614 static void ec_node_cmd_exit_func(void)
616 ec_node_free(ec_node_cmd_expr);
617 ec_node_cmd_expr = NULL;
618 ec_node_free(ec_node_cmd_parser);
619 ec_node_cmd_parser = NULL;
622 static struct ec_init ec_node_cmd_init = {
623 .init = ec_node_cmd_init_func,
624 .exit = ec_node_cmd_exit_func,
628 EC_INIT_REGISTER(ec_node_cmd_init);
630 /* LCOV_EXCL_START */
631 static int ec_node_cmd_testcase(void)
633 struct ec_node *node;
636 node = EC_NODE_CMD(EC_NO_ID,
637 "command [option] (subset1, subset2, subset3, subset4) x|y z*",
638 ec_node_int("x", 0, 10, 10),
639 ec_node_int("y", 20, 30, 10)
642 EC_LOG(EC_LOG_ERR, "cannot create node\n");
645 testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
646 testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "subset1", "1");
647 testres |= EC_TEST_CHECK_PARSE(node, 4, "command", "subset3", "subset2",
649 testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "subset2", "subset3",
651 testres |= EC_TEST_CHECK_PARSE(node, 6, "command", "subset3", "subset1",
652 "subset4", "subset2", "4");
653 testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
654 testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
655 testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
657 testres |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
658 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
661 node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
662 ec_node_int("count", 0, 10, 10));
664 EC_LOG(EC_LOG_ERR, "cannot create node\n");
667 testres |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
669 testres |= EC_TEST_CHECK_COMPLETE(node,
672 testres |= EC_TEST_CHECK_COMPLETE(node,
675 testres |= EC_TEST_CHECK_COMPLETE(node,
676 "good", "morning", "", EC_VA_END,
677 "bob", "bobby", "michael", EC_VA_END);
681 node = EC_NODE_CMD(EC_NO_ID, "[foo [bar]]");
683 EC_LOG(EC_LOG_ERR, "cannot create node\n");
686 testres |= EC_TEST_CHECK_PARSE(node, 0);
687 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
688 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
689 testres |= EC_TEST_CHECK_PARSE(node, 0, "x");
696 static struct ec_test ec_node_cmd_test = {
698 .test = ec_node_cmd_testcase,
701 EC_TEST_REGISTER(ec_node_cmd_test);