X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Fecoli_node_cmd.c;h=0aca9b139afbd8e2168cb9d6e94a9e88482833a7;hb=ff1f48e335ca77cc97e49ea86b292233731ecb78;hp=d1d2c7d2eb992356325927fc319b30459554bea6;hpb=575fd5b430d5f1557d9a6bdb19c037efaf023759;p=protos%2Flibecoli.git diff --git a/lib/ecoli_node_cmd.c b/lib/ecoli_node_cmd.c index d1d2c7d..0aca9b1 100644 --- a/lib/ecoli_node_cmd.c +++ b/lib/ecoli_node_cmd.c @@ -38,6 +38,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -50,6 +53,8 @@ #include #include +EC_LOG_TYPE_REGISTER(node_cmd); + struct ec_node_cmd { struct ec_node gen; char *cmd_str; /* the command string. */ @@ -80,7 +85,7 @@ ec_node_cmd_eval_var(void **result, void *userctx, for (i = 0; i < node->len; i++) { id = ec_node_id(node->table[i]); - printf("i=%d id=%s\n", i, id); + //printf("i=%d id=%s\n", i, id); if (id == NULL) continue; if (strcmp(str, id)) @@ -99,7 +104,7 @@ ec_node_cmd_eval_var(void **result, void *userctx, return -ENOMEM; } - printf("eval var %s %p\n", str, eval); + //printf("eval var %s %p\n", str, eval); *result = eval; return 0; @@ -136,7 +141,7 @@ ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand, else return -EINVAL; - printf("eval post_op %p\n", eval); + //printf("eval post_op %p\n", eval); *result = eval; return 0; @@ -154,7 +159,7 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1, (void)userctx; - printf("eval bin_op %p %p\n", in1, in2); + //printf("eval bin_op %p %p\n", in1, in2); /* get parsed string vector, it should contain only one str */ vec = ec_parsed_strvec(operator); @@ -169,16 +174,26 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1, ec_node_free(in2); *result = out; } else if (!strcmp(ec_strvec_val(vec, 0), ",")) { - out = EC_NODE_SUBSET(NULL, ec_node_clone(in1), ec_node_clone(in2)); - if (out == NULL) - return -EINVAL; - ec_node_free(in1); - ec_node_free(in2); - *result = out; + if (!strcmp(in2->type->name, "subset")) { + if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0) + return -EINVAL; + ec_node_free(in1); + *result = in2; + } else { + out = EC_NODE_SUBSET(NULL, ec_node_clone(in1), + ec_node_clone(in2)); + if (out == NULL) + return -EINVAL; + ec_node_free(in1); + ec_node_free(in2); + *result = out; + } } else { return -EINVAL; } + //printf("eval bin_op out %p\n", *result); + return 0; } @@ -211,7 +226,7 @@ ec_node_cmd_eval_parenthesis(void **result, void *userctx, return -EINVAL; } - printf("eval paren\n"); + //printf("eval paren\n"); *result = out; return 0; @@ -233,20 +248,24 @@ static const struct ec_node_expr_eval_ops test_ops = { .eval_free = ec_node_cmd_eval_free, }; -static struct ec_parsed *ec_node_cmd_parse(const struct ec_node *gen_node, - const struct ec_strvec *strvec) +static int +ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state, + const struct ec_strvec *strvec) { struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; - return ec_node_parse_strvec(node->cmd, strvec); + return ec_node_parse_child(node->cmd, state, strvec); } -static struct ec_completed *ec_node_cmd_complete(const struct ec_node *gen_node, - const struct ec_strvec *strvec) +static int +ec_node_cmd_complete(const struct ec_node *gen_node, + struct ec_completed *completed, + struct ec_parsed *parsed, + const struct ec_strvec *strvec) { struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; - return ec_node_complete_strvec(node->cmd, strvec); + return ec_node_complete_child(node->cmd, completed, parsed, strvec); } static void ec_node_cmd_free_priv(struct ec_node *gen_node) @@ -271,9 +290,11 @@ static int ec_node_cmd_build(struct ec_node *gen_node) void *result; int ret; + /* XXX the expr parser can be moved in the node init */ + /* build the expression parser */ ret = -ENOMEM; - expr = ec_node_new("expr", "expr"); + expr = ec_node("expr", "expr"); if (expr == NULL) goto fail; ret = ec_node_expr_set_val_node(expr, ec_node_re(NULL, "[a-zA-Z0-9]+")); @@ -338,7 +359,7 @@ static int ec_node_cmd_build(struct ec_node *gen_node) goto fail; ret = -ENOMEM; - cmd = ec_node_new("seq", NULL); + cmd = ec_node("seq", NULL); if (cmd == NULL) goto fail; @@ -352,7 +373,8 @@ static int ec_node_cmd_build(struct ec_node *gen_node) goto fail; } ec_parsed_free(p); - ec_node_dump(stdout, cmd); + p = NULL; + //ec_node_dump(stdout, cmd); ec_node_free(node->expr); node->expr = expr; @@ -364,6 +386,7 @@ static int ec_node_cmd_build(struct ec_node *gen_node) return 0; fail: + ec_parsed_free(p); ec_node_free(expr); ec_node_free(lex); ec_node_free(cmd); @@ -390,7 +413,6 @@ int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child) assert(node != NULL); - printf("add child %s\n", child->id); if (child == NULL) return -EINVAL; @@ -417,7 +439,7 @@ struct ec_node *ec_node_cmd(const char *id, const char *cmd_str) struct ec_node *gen_node = NULL; struct ec_node_cmd *node = NULL; - gen_node = __ec_node_new(&ec_node_cmd_type, id); + gen_node = __ec_node(&ec_node_cmd_type, id); if (gen_node == NULL) goto fail; @@ -472,18 +494,19 @@ fail: return NULL; } +/* LCOV_EXCL_START */ static int ec_node_cmd_testcase(void) { struct ec_node *node; int ret = 0; node = EC_NODE_CMD(NULL, - "command [option] (subset1, subset2) x | y", + "command [option] (subset1, subset2, subset3) x|y", ec_node_int("x", 0, 10, 10), ec_node_int("y", 20, 30, 10) ); if (node == NULL) { - ec_log(EC_LOG_ERR, "cannot create node\n"); + EC_LOG(EC_LOG_ERR, "cannot create node\n"); return -1; } ret |= EC_TEST_CHECK_PARSE(node, 2, "command", "1"); @@ -493,10 +516,29 @@ static int ec_node_cmd_testcase(void) ret |= EC_TEST_CHECK_PARSE(node, -1, "foo"); ec_node_free(node); - // XXX completion + node = EC_NODE_CMD(NULL, "good morning [count] bob|bobby|michael", + ec_node_int("count", 0, 10, 10)); + if (node == NULL) { + EC_LOG(EC_LOG_ERR, "cannot create node\n"); + return -1; + } + ret |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob"); + + ret |= EC_TEST_CHECK_COMPLETE(node, + "", EC_NODE_ENDLIST, + "good", EC_NODE_ENDLIST); + ret |= EC_TEST_CHECK_COMPLETE(node, + "g", EC_NODE_ENDLIST, + "good", EC_NODE_ENDLIST); + ret |= EC_TEST_CHECK_COMPLETE(node, + "good", "morning", "", EC_NODE_ENDLIST, + "bob", "bobby", "michael", EC_NODE_ENDLIST); + + ec_node_free(node); return ret; } +/* LCOV_EXCL_STOP */ static struct ec_test ec_node_cmd_test = { .name = "node_cmd",