save
[protos/libecoli.git] / lib / ecoli_node_cmd.c
index b206172..5d47a7b 100644 (file)
@@ -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;
@@ -248,48 +247,19 @@ static const struct ec_node_expr_eval_ops test_ops = {
        .eval_free = ec_node_cmd_eval_free,
 };
 
-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_child(node->cmd, state, strvec);
-}
-
-static int
-ec_node_cmd_complete(const struct ec_node *gen_node,
-               struct ec_completed *completed,
-               const struct ec_strvec *strvec)
-{
-       struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
-
-       return ec_node_complete_child(node->cmd, completed, strvec);
-}
-
-static void ec_node_cmd_free_priv(struct ec_node *gen_node)
-{
-       struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
-       unsigned int i;
-
-       ec_free(node->cmd_str);
-       ec_node_free(node->cmd);
-       ec_node_free(node->expr);
-       ec_node_free(node->lex);
-       for (i = 0; i < node->len; i++)
-               ec_node_free(node->table[i]);
-       ec_free(node->table);
-}
-
-static int ec_node_cmd_build(struct ec_node *gen_node)
+static int ec_node_cmd_build(struct ec_node_cmd *node)
 {
        struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL;
-       struct ec_parsed *p, *child;
-       struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
+       struct ec_parsed *p = NULL, *child;
        void *result;
        int ret;
 
-       /* XXX the expr parser can be moved in the node init */
+       ec_node_free(node->expr);
+       node->expr = NULL;
+       ec_node_free(node->lex);
+       node->lex = NULL;
+       ec_node_free(node->cmd);
+       node->cmd = NULL;
 
        /* build the expression parser */
        ret = -ENOMEM;
@@ -373,13 +343,9 @@ 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;
-       ec_node_free(node->lex);
        node->lex = lex;
-       ec_node_free(node->cmd);
        node->cmd = cmd;
 
        return 0;
@@ -392,9 +358,46 @@ fail:
        return ret;
 }
 
+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;
+
+       if (node->cmd == NULL)
+               return -ENOENT;
+       return ec_node_parse_child(node->cmd, state, strvec);
+}
+
+static int
+ec_node_cmd_complete(const struct ec_node *gen_node,
+               struct ec_completed *completed,
+               const struct ec_strvec *strvec)
+{
+       struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
+
+       if (node->cmd == NULL)
+               return -ENOENT;
+       return ec_node_complete_child(node->cmd, completed, strvec);
+}
+
+static void ec_node_cmd_free_priv(struct ec_node *gen_node)
+{
+       struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
+       unsigned int i;
+
+       ec_free(node->cmd_str);
+       ec_node_free(node->cmd);
+       ec_node_free(node->expr);
+       ec_node_free(node->lex);
+       for (i = 0; i < node->len; i++)
+               ec_node_free(node->table[i]);
+       ec_free(node->table);
+}
+
+
 static struct ec_node_type ec_node_cmd_type = {
        .name = "cmd",
-       .build = ec_node_cmd_build,
        .parse = ec_node_cmd_parse,
        .complete = ec_node_cmd_complete,
        .size = sizeof(struct ec_node_cmd),
@@ -407,6 +410,7 @@ int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
 {
        struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node;
        struct ec_node **table;
+       int ret;
 
        // XXX check node type
 
@@ -415,7 +419,11 @@ int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
        if (child == NULL)
                return -EINVAL;
 
-       gen_node->flags &= ~EC_NODE_F_BUILT;
+       if (node->cmd == NULL) {
+               ret = ec_node_cmd_build(node);
+               if (ret < 0)
+                       return ret;
+       }
 
        table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
        if (table == NULL) {
@@ -427,33 +435,11 @@ int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child)
        table[node->len] = child;
        node->len++;
 
-       child->parent = gen_node;
        TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
 
        return 0;
 }
 
-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(&ec_node_cmd_type, id);
-       if (gen_node == NULL)
-               goto fail;
-
-       node = (struct ec_node_cmd *)gen_node;
-       node->cmd_str = ec_strdup(cmd_str);
-       if (node->cmd_str == NULL)
-               goto fail;
-
-       return gen_node;
-
-fail:
-       ec_node_free(gen_node);
-       return NULL;
-}
-
 struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
 {
        struct ec_node *gen_node = NULL;
@@ -462,12 +448,18 @@ struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
        va_list ap;
        int fail = 0;
 
-       va_start(ap, cmd);
+       gen_node = __ec_node(&ec_node_cmd_type, id);
+       if (gen_node == NULL)
+               fail = 1;
+
+       if (fail == 0) {
+               node = (struct ec_node_cmd *)gen_node;
+               node->cmd_str = ec_strdup(cmd);
+               if (node->cmd_str == NULL)
+                       fail = 1;
+       }
 
-       gen_node = ec_node_cmd(id, cmd);
-       node = (struct ec_node_cmd *)gen_node;
-       if (node == NULL)
-               fail = 1;;
+       va_start(ap, cmd);
 
        for (child = va_arg(ap, struct ec_node *);
             child != EC_NODE_ENDLIST;
@@ -481,18 +473,26 @@ struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...)
                }
        }
 
+       va_end(ap);
+
        if (fail == 1)
                goto fail;
 
-       va_end(ap);
+       if (ec_node_cmd_build(node) < 0)
+               goto fail;
+
        return gen_node;
 
 fail:
        ec_node_free(gen_node); /* will also free children */
-       va_end(ap);
        return NULL;
 }
 
+struct ec_node *ec_node_cmd(const char *id, const char *cmd_str)
+{
+       return __ec_node_cmd(id, cmd_str, EC_NODE_ENDLIST);
+}
+
 /* LCOV_EXCL_START */
 static int ec_node_cmd_testcase(void)
 {
@@ -500,7 +500,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 +511,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);