fix cmd node
[protos/libecoli.git] / lib / ecoli_node_once.c
index 54aa67b..3e716e0 100644 (file)
@@ -60,10 +60,10 @@ count_node(struct ec_parsed *parsed, const struct ec_node *node)
        if (parsed == NULL)
                return 0;
 
-       if (parsed->node == node)
+       if (ec_parsed_get_node(parsed) == node)
                count++;
 
-       TAILQ_FOREACH(child, &parsed->children, next)
+       EC_PARSED_FOREACH_CHILD(child, parsed)
                count += count_node(child, node);
 
        return count;
@@ -90,21 +90,21 @@ ec_node_once_parse(const struct ec_node *gen_node,
 static int
 ec_node_once_complete(const struct ec_node *gen_node,
                struct ec_completed *completed,
-               struct ec_parsed *parsed,
                const struct ec_strvec *strvec)
 {
        struct ec_node_once *node = (struct ec_node_once *)gen_node;
+       struct ec_parsed *parsed = ec_completed_get_state(completed);
        unsigned int count;
        int ret;
 
        /* count the number of occurences of the node: if already parsed,
         * do not match
         */
-       count = count_node(ec_parsed_get_root(parsed), node->child); //XXX
+       count = count_node(ec_parsed_get_root(parsed), node->child);
        if (count > 0)
                return 0;
 
-       ret = ec_node_complete_child(node->child, completed, parsed, strvec);
+       ret = ec_node_complete_child(node->child, completed, strvec);
        if (ret < 0)
                return ret;
 
@@ -132,21 +132,24 @@ int ec_node_once_set(struct ec_node *gen_node, struct ec_node *child)
 {
        struct ec_node_once *node = (struct ec_node_once *)gen_node;
 
-       // XXX check node type
-
-       assert(node != NULL);
+       if (gen_node == NULL || child == NULL) {
+               errno = EINVAL;
+               goto fail;
+       }
 
-       if (child == NULL)
-               return -EINVAL;
+       if (ec_node_check_type(gen_node, &ec_node_once_type) < 0)
+               goto fail;
 
-       gen_node->flags &= ~EC_NODE_F_BUILT;
+       if (ec_node_add_child(gen_node, child) < 0)
+               goto fail;
 
        node->child = child;
 
-       child->parent = gen_node;
-       TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
-
        return 0;
+
+fail:
+       ec_node_free(child);
+       return -1;
 }
 
 struct ec_node *ec_node_once(const char *id, struct ec_node *child)
@@ -158,11 +161,16 @@ struct ec_node *ec_node_once(const char *id, struct ec_node *child)
 
        gen_node = __ec_node(&ec_node_once_type, id);
        if (gen_node == NULL)
-               return NULL;
+               goto fail;
 
        ec_node_once_set(gen_node, child);
+       child = NULL;
 
        return gen_node;
+
+fail:
+       ec_node_free(child);
+       return NULL;
 }
 
 /* LCOV_EXCL_START */
@@ -171,10 +179,10 @@ static int ec_node_once_testcase(void)
        struct ec_node *node;
        int ret = 0;
 
-       node = ec_node_many(NULL,
-                       EC_NODE_OR(NULL,
-                               ec_node_once(NULL, ec_node_str(NULL, "foo")),
-                               ec_node_str(NULL, "bar")
+       node = ec_node_many(EC_NO_ID,
+                       EC_NODE_OR(EC_NO_ID,
+                               ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "foo")),
+                               ec_node_str(EC_NO_ID, "bar")
                                ), 0, 0
                );
        if (node == NULL) {
@@ -195,12 +203,12 @@ static int ec_node_once_testcase(void)
 
 #if 0 //XXX no completion test for node_once
        /* test completion */
-       node = EC_NODE_OR(NULL,
-               ec_node_str(NULL, "foo"),
-               ec_node_str(NULL, "bar"),
-               ec_node_str(NULL, "bar2"),
-               ec_node_str(NULL, "toto"),
-               ec_node_str(NULL, "titi")
+       node = EC_NODE_OR(EC_NO_ID,
+               ec_node_str(EC_NO_ID, "foo"),
+               ec_node_str(EC_NO_ID, "bar"),
+               ec_node_str(EC_NO_ID, "bar2"),
+               ec_node_str(EC_NO_ID, "toto"),
+               ec_node_str(EC_NO_ID, "titi")
        );
        if (node == NULL) {
                EC_LOG(EC_LOG_ERR, "cannot create node\n");