From 9d5863b3a263f4238616174e1ebe8948676d132b Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Thu, 1 Mar 2018 15:31:22 +0100 Subject: [PATCH] cleanup cmd node --- lib/ecoli_node.c | 6 ++++-- lib/ecoli_node_cmd.c | 28 ++++++++++++++-------------- lib/ecoli_node_expr.c | 5 ++++- lib/ecoli_node_expr_test.c | 2 -- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/ecoli_node.c b/lib/ecoli_node.c index 1c0544c..458d6bc 100644 --- a/lib/ecoli_node.c +++ b/lib/ecoli_node.c @@ -258,7 +258,9 @@ const char *ec_node_desc(const struct ec_node *node) int ec_node_check_type(const struct ec_node *node, const struct ec_node_type *type) { - if (strcmp(node->type->name, type->name)) - return -EINVAL; + if (strcmp(node->type->name, type->name)) { + errno = EINVAL; + return -1; + } return 0; } diff --git a/lib/ecoli_node_cmd.c b/lib/ecoli_node_cmd.c index b206172..dcaaa93 100644 --- a/lib/ecoli_node_cmd.c +++ b/lib/ecoli_node_cmd.c @@ -127,7 +127,8 @@ ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand, const struct ec_parsed *operator) { const struct ec_strvec *vec; - struct ec_node *eval = operand;; + struct ec_node *in = operand;; + struct ec_node *out = NULL;; (void)userctx; @@ -136,13 +137,16 @@ ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand, if (ec_strvec_len(vec) != 1) return -EINVAL; - if (!strcmp(ec_strvec_val(vec, 0), "*")) - eval = NULL; //XXX - else + if (!strcmp(ec_strvec_val(vec, 0), "*")) { + out = ec_node_many(EC_NO_ID, + ec_node_clone(in), 0, 0); + if (out == NULL) + return -EINVAL; + ec_node_free(in); + *result = out; + } else { return -EINVAL; - - //printf("eval post_op %p\n", eval); - *result = eval; + } return 0; } @@ -159,8 +163,6 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1, (void)userctx; - //printf("eval bin_op %p %p\n", in1, in2); - /* get parsed string vector, it should contain only one str */ vec = ec_parsed_strvec(operator); if (ec_strvec_len(vec) != 1) @@ -192,8 +194,6 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1, return -EINVAL; } - //printf("eval bin_op out %p\n", *result); - return 0; } @@ -226,7 +226,6 @@ ec_node_cmd_eval_parenthesis(void **result, void *userctx, return -EINVAL; } - //printf("eval paren\n"); *result = out; return 0; @@ -373,7 +372,6 @@ static int ec_node_cmd_build(struct ec_node *gen_node) } ec_parsed_free(p); p = NULL; - //ec_node_dump(stdout, cmd); ec_node_free(node->expr); node->expr = expr; @@ -500,7 +498,7 @@ static int ec_node_cmd_testcase(void) int ret = 0; node = EC_NODE_CMD(EC_NO_ID, - "command [option] (subset1, subset2, subset3) x|y", + "command [option] (subset1, subset2, subset3) x|y z*", ec_node_int("x", 0, 10, 10), ec_node_int("y", 20, 30, 10) ); @@ -511,6 +509,8 @@ static int ec_node_cmd_testcase(void) ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "1"); ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "23"); ret |= EC_TEST_CHECK_PARSE(node, 3, "command", "option", "23"); + ret |= EC_TEST_CHECK_PARSE(node, 5, "command", "option", "23", + "z", "z"); ret |= EC_TEST_CHECK_PARSE(node, -1, "command", "15"); ret |= EC_TEST_CHECK_PARSE(node, -1, "foo"); ec_node_free(node); diff --git a/lib/ecoli_node_expr.c b/lib/ecoli_node_expr.c index 72c121e..39f3b70 100644 --- a/lib/ecoli_node_expr.c +++ b/lib/ecoli_node_expr.c @@ -604,10 +604,13 @@ int ec_node_expr_eval(void **user_result, const struct ec_node *node, ops->eval_parenthesis == NULL || ops->eval_free == NULL) return -EINVAL; + ret = ec_node_check_type(node, &ec_node_expr_type); + if (ret < 0) + return ret; + if (!ec_parsed_matches(parsed)) return -EINVAL; - //ec_parsed_dump(stdout, parsed); //XXX ret = eval_expression(&result, userctx, ops, node, parsed); if (ret < 0) return ret; diff --git a/lib/ecoli_node_expr_test.c b/lib/ecoli_node_expr_test.c index a26fe08..ee67cbf 100644 --- a/lib/ecoli_node_expr_test.c +++ b/lib/ecoli_node_expr_test.c @@ -198,8 +198,6 @@ static int ec_node_expr_test_eval(struct ec_node *lex_node, struct my_eval_result *eval; int ret; - /* XXX check node type (again and again) */ - p = ec_node_parse(lex_node, str); if (p == NULL) return -1; -- 2.20.1