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. */
43 char *cmd_str; /* the command string. */
44 struct ec_node *cmd; /* the command node. */
45 struct ec_node **table; /* table of node referenced in command. */
46 unsigned int len; /* len of the table. */
49 /* passed as user context to expression parser */
50 struct ec_node_cmd_ctx {
51 struct ec_node **table;
56 ec_node_cmd_eval_var(void **result, void *userctx,
57 const struct ec_parse *var)
59 const struct ec_strvec *vec;
60 struct ec_node_cmd_ctx *ctx = userctx;
61 struct ec_node *eval = NULL;
65 /* get parsed string vector, it should contain only one str */
66 vec = ec_parse_strvec(var);
67 if (ec_strvec_len(vec) != 1) {
71 str = ec_strvec_val(vec, 0);
73 for (i = 0; i < ctx->len; i++) {
74 id = ec_node_id(ctx->table[i]);
79 /* if id matches, use a node provided by the user... */
80 eval = ec_node_clone(ctx->table[i]);
86 /* ...or create a string node */
88 eval = ec_node_str(EC_NO_ID, str);
99 ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand,
100 const struct ec_parse *operator)
112 ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand,
113 const struct ec_parse *operator)
115 const struct ec_strvec *vec;
116 struct ec_node *in = operand;;
117 struct ec_node *out = NULL;;
121 /* get parsed string vector, it should contain only one str */
122 vec = ec_parse_strvec(operator);
123 if (ec_strvec_len(vec) != 1) {
128 if (!strcmp(ec_strvec_val(vec, 0), "*")) {
129 out = ec_node_many(EC_NO_ID,
130 ec_node_clone(in), 0, 0);
144 ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1,
145 const struct ec_parse *operator, void *operand2)
148 const struct ec_strvec *vec;
149 struct ec_node *out = NULL;
150 struct ec_node *in1 = operand1;
151 struct ec_node *in2 = operand2;
155 /* get parsed string vector, it should contain only one str */
156 vec = ec_parse_strvec(operator);
157 if (ec_strvec_len(vec) > 1) {
162 if (ec_strvec_len(vec) == 0) {
163 if (!strcmp(in1->type->name, "seq")) {
164 if (ec_node_seq_add(in1, ec_node_clone(in2)) < 0)
169 out = EC_NODE_SEQ(EC_NO_ID, ec_node_clone(in1),
177 } else if (!strcmp(ec_strvec_val(vec, 0), "|")) {
178 if (!strcmp(in2->type->name, "or")) {
179 if (ec_node_or_add(in2, ec_node_clone(in1)) < 0)
183 } else if (!strcmp(in1->type->name, "or")) {
184 if (ec_node_or_add(in1, ec_node_clone(in2)) < 0)
189 out = EC_NODE_OR(EC_NO_ID, ec_node_clone(in1),
197 } else if (!strcmp(ec_strvec_val(vec, 0), ",")) {
198 if (!strcmp(in2->type->name, "subset")) {
199 if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0)
203 } else if (!strcmp(in1->type->name, "subset")) {
204 if (ec_node_subset_add(in1, ec_node_clone(in2)) < 0)
209 out = EC_NODE_SUBSET(EC_NO_ID, ec_node_clone(in1),
226 ec_node_cmd_eval_parenthesis(void **result, void *userctx,
227 const struct ec_parse *open_paren,
228 const struct ec_parse *close_paren,
231 const struct ec_strvec *vec;
232 struct ec_node *in = value;;
233 struct ec_node *out = NULL;;
238 /* get parsed string vector, it should contain only one str */
239 vec = ec_parse_strvec(open_paren);
240 if (ec_strvec_len(vec) != 1) {
245 if (!strcmp(ec_strvec_val(vec, 0), "[")) {
246 out = ec_node_option(EC_NO_ID, ec_node_clone(in));
250 } else if (!strcmp(ec_strvec_val(vec, 0), "(")) {
263 ec_node_cmd_eval_free(void *result, void *userctx)
269 static const struct ec_node_expr_eval_ops expr_ops = {
270 .eval_var = ec_node_cmd_eval_var,
271 .eval_pre_op = ec_node_cmd_eval_pre_op,
272 .eval_post_op = ec_node_cmd_eval_post_op,
273 .eval_bin_op = ec_node_cmd_eval_bin_op,
274 .eval_parenthesis = ec_node_cmd_eval_parenthesis,
275 .eval_free = ec_node_cmd_eval_free,
278 static struct ec_node *
279 ec_node_cmd_build_expr(void)
281 struct ec_node *expr = NULL;
284 /* build the expression parser */
285 expr = ec_node("expr", "expr");
288 ret = ec_node_expr_set_val_node(expr, ec_node_re(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_str(EC_NO_ID, "|"));
298 ret = ec_node_expr_add_bin_op(expr, ec_node("empty", EC_NO_ID));
301 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "+"));
304 ret = ec_node_expr_add_post_op(expr, ec_node_str(EC_NO_ID, "*"));
307 ret = ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "["),
308 ec_node_str(EC_NO_ID, "]"));
311 ec_node_expr_add_parenthesis(expr, ec_node_str(EC_NO_ID, "("),
312 ec_node_str(EC_NO_ID, ")"));
323 static struct ec_node *
324 ec_node_cmd_build_parser(struct ec_node *expr)
326 struct ec_node *lex = NULL;
329 /* prepend a lexer to the expression node */
330 lex = ec_node_re_lex(EC_NO_ID, ec_node_clone(expr));
334 ret = ec_node_re_lex_add(lex, "[a-zA-Z0-9]+", 1, NULL);
337 ret = ec_node_re_lex_add(lex, "[*|,()]", 1, NULL);
340 ret = ec_node_re_lex_add(lex, "\\[", 1, NULL);
343 ret = ec_node_re_lex_add(lex, "\\]", 1, NULL);
346 ret = ec_node_re_lex_add(lex, "[ ]+", 0, NULL);
358 static struct ec_node *
359 ec_node_cmd_build(const char *cmd_str, struct ec_node **table, size_t len)
361 struct ec_node_cmd_ctx ctx = { table, len };
362 struct ec_parse *p = NULL;
366 /* parse the command expression */
367 p = ec_node_parse(ec_node_cmd_parser, cmd_str);
371 if (!ec_parse_matches(p)) {
375 if (!ec_parse_has_child(p)) {
380 ret = ec_node_expr_eval(&result, ec_node_cmd_expr,
381 ec_parse_get_first_child(p),
395 ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parse *state,
396 const struct ec_strvec *strvec)
398 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
400 return ec_node_parse_child(node->cmd, state, strvec);
404 ec_node_cmd_complete(const struct ec_node *gen_node,
405 struct ec_comp *comp,
406 const struct ec_strvec *strvec)
408 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
410 return ec_node_complete_child(node->cmd, comp, strvec);
413 static void ec_node_cmd_free_priv(struct ec_node *gen_node)
415 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
418 ec_free(node->cmd_str);
419 node->cmd_str = NULL;
420 ec_node_free(node->cmd);
422 for (i = 0; i < node->len; i++)
423 ec_node_free(node->table[i]);
424 ec_free(node->table);
429 static const struct ec_config_schema ec_node_cmd_subschema[] = {
431 .desc = "A child node whose id is referenced in the expression.",
432 .type = EC_CONFIG_TYPE_NODE,
435 .type = EC_CONFIG_TYPE_NONE,
439 static const struct ec_config_schema ec_node_cmd_schema[] = {
442 .desc = "The expression to match. Supported operators "
443 "are or '|', list ',', many '+', many-or-zero '*', "
444 "option '[]', group '()'. An identifier (alphanumeric) can "
445 "reference a node whose node_id matches. Else it is "
446 "interpreted as ec_node_str() matching this string. "
447 "Example: command [option] (subset1, subset2) x|y",
448 .type = EC_CONFIG_TYPE_STRING,
452 .desc = "The list of children nodes.",
453 .type = EC_CONFIG_TYPE_LIST,
454 .subschema = ec_node_cmd_subschema,
457 .type = EC_CONFIG_TYPE_NONE,
461 static int ec_node_cmd_set_config(struct ec_node *gen_node,
462 const struct ec_config *config)
464 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
465 const struct ec_config *expr = NULL;
466 struct ec_node *cmd = NULL;
467 struct ec_node **table = NULL;
468 char *cmd_str = NULL;
471 /* retrieve config locally */
472 expr = ec_config_dict_get(config, "expr");
478 table = ec_node_config_node_list_to_table(
479 ec_config_dict_get(config, "children"), &len);
483 cmd_str = ec_strdup(expr->string);
487 /* parse expression to build the cmd child node */
488 cmd = ec_node_cmd_build(cmd_str, table, len);
492 /* ok, store the config */
493 ec_node_free(node->cmd);
495 ec_free(node->cmd_str);
496 node->cmd_str = cmd_str;
497 for (i = 0; i < node->len; i++)
498 ec_node_free(node->table[i]);
499 ec_free(node->table);
506 for (i = 0; i < len; i++)
507 ec_node_free(table[i]);
515 ec_node_cmd_get_children_count(const struct ec_node *gen_node)
517 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
519 if (node->cmd == NULL)
525 ec_node_cmd_get_child(const struct ec_node *gen_node, size_t i,
526 struct ec_node **child, unsigned int *refs)
528 struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
538 static struct ec_node_type ec_node_cmd_type = {
540 .schema = ec_node_cmd_schema,
541 .set_config = ec_node_cmd_set_config,
542 .parse = ec_node_cmd_parse,
543 .complete = ec_node_cmd_complete,
544 .size = sizeof(struct ec_node_cmd),
545 .free_priv = ec_node_cmd_free_priv,
546 .get_children_count = ec_node_cmd_get_children_count,
547 .get_child = ec_node_cmd_get_child,
550 EC_NODE_TYPE_REGISTER(ec_node_cmd_type);
552 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
554 struct ec_config *config = NULL, *children = NULL;
555 struct ec_node *gen_node = NULL;
559 /* this block must stay first, it frees the nodes on error */
561 children = ec_node_config_node_list_from_vargs(ap);
563 if (children == NULL)
566 gen_node = ec_node_from_type(&ec_node_cmd_type, id);
567 if (gen_node == NULL)
570 config = ec_config_dict();
574 if (ec_config_dict_set(config, "expr", ec_config_string(cmd)) < 0)
577 if (ec_config_dict_set(config, "children", children) < 0) {
578 children = NULL; /* freed */
583 ret = ec_node_set_config(gen_node, config);
584 config = NULL; /* freed */
591 ec_node_free(gen_node); /* will also free added children */
592 ec_config_free(children);
593 ec_config_free(config);
598 static int ec_node_cmd_init_func(void)
600 ec_node_cmd_expr = ec_node_cmd_build_expr();
601 if (ec_node_cmd_expr == NULL)
604 ec_node_cmd_parser = ec_node_cmd_build_parser(ec_node_cmd_expr);
605 if (ec_node_cmd_parser == NULL)
611 EC_LOG(EC_LOG_ERR, "Failed to initialize command parser\n");
612 ec_node_free(ec_node_cmd_expr);
613 ec_node_cmd_expr = NULL;
614 ec_node_free(ec_node_cmd_parser);
615 ec_node_cmd_parser = NULL;
619 static void ec_node_cmd_exit_func(void)
621 ec_node_free(ec_node_cmd_expr);
622 ec_node_cmd_expr = NULL;
623 ec_node_free(ec_node_cmd_parser);
624 ec_node_cmd_parser = NULL;
627 static struct ec_init ec_node_cmd_init = {
628 .init = ec_node_cmd_init_func,
629 .exit = ec_node_cmd_exit_func,
633 EC_INIT_REGISTER(ec_node_cmd_init);
635 /* LCOV_EXCL_START */
636 static int ec_node_cmd_testcase(void)
638 struct ec_node *node;
641 node = EC_NODE_CMD(EC_NO_ID,
642 "command [option] (subset1, subset2, subset3, subset4) x|y z*",
643 ec_node_int("x", 0, 10, 10),
644 ec_node_int("y", 20, 30, 10)
647 EC_LOG(EC_LOG_ERR, "cannot create node\n");
650 testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "1");
651 testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "subset1", "1");
652 testres |= EC_TEST_CHECK_PARSE(node, 4, "command", "subset3", "subset2",
654 testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "subset2", "subset3",
656 testres |= EC_TEST_CHECK_PARSE(node, 6, "command", "subset3", "subset1",
657 "subset4", "subset2", "4");
658 testres |= EC_TEST_CHECK_PARSE(node, 2, "command", "23");
659 testres |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23");
660 testres |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23",
662 testres |= EC_TEST_CHECK_PARSE(node, -1, "command", "15");
663 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
666 node = EC_NODE_CMD(EC_NO_ID, "good morning [count] bob|bobby|michael",
667 ec_node_int("count", 0, 10, 10));
669 EC_LOG(EC_LOG_ERR, "cannot create node\n");
672 testres |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob");
674 testres |= EC_TEST_CHECK_COMPLETE(node,
676 "good", EC_NODE_ENDLIST);
677 testres |= EC_TEST_CHECK_COMPLETE(node,
678 "g", EC_NODE_ENDLIST,
679 "good", EC_NODE_ENDLIST);
680 testres |= EC_TEST_CHECK_COMPLETE(node,
681 "good", "morning", "", EC_NODE_ENDLIST,
682 "bob", "bobby", "michael", EC_NODE_ENDLIST);
686 node = EC_NODE_CMD(EC_NO_ID, "[foo [bar]]");
688 EC_LOG(EC_LOG_ERR, "cannot create node\n");
691 testres |= EC_TEST_CHECK_PARSE(node, 0);
692 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
693 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
694 testres |= EC_TEST_CHECK_PARSE(node, 0, "x");
701 static struct ec_test ec_node_cmd_test = {
703 .test = ec_node_cmd_testcase,
706 EC_TEST_REGISTER(ec_node_cmd_test);