sq
[protos/libecoli.git] / lib / ecoli_completed.c
index bec0273..c9aff48 100644 (file)
@@ -47,74 +47,93 @@ struct ec_completed *ec_completed(void)
        if (completed == NULL)
                return NULL;
 
-       TAILQ_INIT(&completed->match_items);
-       TAILQ_INIT(&completed->no_match_items);
+       TAILQ_INIT(&completed->nodes);
 
        return completed;
 }
 
-struct ec_completed *
+/* XXX on error, states are not freed ?
+ * they can be left in a bad state and should not be reused */
+int
 ec_node_complete_child(struct ec_node *node,
-               struct ec_parsed *state,
+               struct ec_completed *completed,
+               struct ec_parsed *parsed_state,
                const struct ec_strvec *strvec)
 {
-       struct ec_completed *completed;
-       struct ec_parsed *child;
+       struct ec_parsed *child_state = NULL;
        int ret;
 
        /* build the node if required */
        if (node->type->build != NULL) {
                if ((node->flags & EC_NODE_F_BUILT) == 0) {
                        ret = node->type->build(node);
-                       if (ret < 0) {
-                               errno = -ret;
-                               return NULL;
-                       }
+                       if (ret < 0)
+                               return ret;
                }
        }
        node->flags |= EC_NODE_F_BUILT;
 
-       if (node->type->complete == NULL) {
-               errno = ENOTSUP;
-               return NULL;
-       }
+       if (node->type->complete == NULL)
+               return -ENOTSUP;
 
-       child = ec_parsed();
-       if (child == NULL)
-               return NULL;
+       child_state = ec_parsed();
+       if (child_state == NULL)
+               return -ENOMEM;
+       child_state->node = node;
+       ec_parsed_add_child(parsed_state, child_state);
 
-       child->node = node;
-       ec_parsed_add_child(state, child);
-       completed = node->type->complete(node, child, strvec);
+       ret = ec_completed_add_node(completed, node);
+       if (ret < 0)
+               return ret;
+
+       ret = node->type->complete(node, completed,
+                               child_state, strvec);
+       if (ret < 0)
+               return ret;
 
 #if 0 // XXX dump
        printf("----------------------------------------------------------\n");
        ec_node_dump(stdout, node);
        ec_strvec_dump(stdout, strvec);
        ec_completed_dump(stdout, completed);
-       ec_parsed_dump(stdout, state);
+       ec_parsed_dump(stdout, parsed_state);
 #endif
 
-       ec_parsed_del_child(state, child);
-       assert(TAILQ_EMPTY(&child->children));
-       ec_parsed_free(child);
+       ec_parsed_del_child(parsed_state, child_state);
+       assert(TAILQ_EMPTY(&child_state->children));
+       ec_parsed_free(child_state);
 
-       return completed;
+       return 0;
 }
 
 struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
        const struct ec_strvec *strvec)
 {
-       struct ec_parsed *state = ec_parsed();
-       struct ec_completed *completed;
+       struct ec_parsed *parsed_state = NULL;
+       struct ec_completed *completed = NULL;
+       int ret;
 
-       if (state == NULL)
-               return NULL;
+       parsed_state = ec_parsed();
+       if (parsed_state == NULL)
+               goto fail;
 
-       completed = ec_node_complete_child(node, state, strvec);
-       ec_parsed_free(state);
+       completed = ec_completed();
+       if (completed == NULL)
+               goto fail;
+
+       ret = ec_node_complete_child(node, completed,
+                               parsed_state, strvec);
+       if (ret < 0)
+               goto fail;
+
+       ec_parsed_free(parsed_state);
 
        return completed;
+
+fail:
+       ec_parsed_free(parsed_state);
+       ec_completed_free(completed);
+       return NULL;
 }
 
 struct ec_completed *ec_node_complete(struct ec_node *node,
@@ -154,37 +173,46 @@ static size_t strcmp_count(const char *s1, const char *s2)
        return i;
 }
 
-static struct ec_completed_item *
-ec_completed_item(enum ec_completed_type type, struct ec_parsed *state,
-               const struct ec_node *node, const char *add)
+static struct ec_completed_node *
+ec_completed_node(const struct ec_node *node)
 {
-       struct ec_completed_item *item = NULL;
+       struct ec_completed_node *compnode = NULL;
 
-       item = ec_calloc(1, sizeof(*item));
-       if (item == NULL)
+       compnode = ec_calloc(1, sizeof(*compnode));
+       if (compnode == NULL)
                return NULL;
 
-       /* XXX can state be NULL? */
-       if (state != NULL) {
-               struct ec_parsed *p;
-               size_t len;
+       compnode->node = node;
+       TAILQ_INIT(&compnode->matches);
 
-               /* get path len */
-               for (p = state, len = 0; p != NULL;
-                    p = ec_parsed_get_parent(p), len++)
-                       ;
+       return compnode;
+}
 
-               item->path = ec_calloc(len, sizeof(*item->path));
-               if (item->path == NULL)
-                       goto fail;
+static struct ec_completed_match *
+ec_completed_match(enum ec_completed_type type, struct ec_parsed *state,
+               const struct ec_node *node, const char *add)
+{
+       struct ec_completed_match *item = NULL;
+       struct ec_parsed *p;
+       size_t len;
 
-               item->pathlen = len;
+       item = ec_calloc(1, sizeof(*item));
+       if (item == NULL)
+               return NULL;
 
-               /* write path in array */
-               for (p = state, len = 0; p != NULL;
-                    p = ec_parsed_get_parent(p), len++)
-                       item->path[len] = p->node;
-       }
+       /* get path len */
+       for (p = state, len = 0; p != NULL;
+            p = ec_parsed_get_parent(p), len++)
+               ;
+       /* allocate room for path */
+       item->path = ec_calloc(len, sizeof(*item->path));
+       if (item->path == NULL)
+               goto fail;
+       item->pathlen = len;
+       /* write path in array */
+       for (p = state, len = 0; p != NULL;
+            p = ec_parsed_get_parent(p), len++)
+               item->path[len] = p->node;
 
        item->type = type;
        item->node = node;
@@ -201,134 +229,142 @@ fail:
                ec_free(item->path);
                ec_free(item->add);
        }
-       ec_completed_item_free(item);
+       ec_completed_match_free(item);
 
        return NULL;
 }
 
-static int ec_completed_add_item(struct ec_completed *completed,
-                               struct ec_completed_item *item)
+static int
+__ec_completed_add_match(enum ec_completed_type type,
+                       struct ec_completed *completed,
+                       struct ec_parsed *parsed_state,
+                       const struct ec_node *node, const char *add)
 {
+       struct ec_completed_node *compnode = NULL;
+       struct ec_completed_match *match = NULL;
+       int ret = -ENOMEM;
        size_t n;
 
-       if (item->add != NULL) {
+       /* find the compnode entry corresponding to this node */
+       TAILQ_FOREACH(compnode, &completed->nodes, next) {
+               if (compnode->node == node)
+                       break;
+       }
+       if (compnode == NULL)
+               return -ENOENT;
+
+       match = ec_completed_match(type, parsed_state, node, add);
+       if (match == NULL)
+               goto fail;
+
+       if (match->add != NULL) {
                if (completed->smallest_start == NULL) {
-                       completed->smallest_start = ec_strdup(item->add);
+                       completed->smallest_start = ec_strdup(match->add);
                        if (completed->smallest_start == NULL)
-                               return -ENOMEM;
+                               goto fail;
                } else {
-                       n = strcmp_count(item->add,
+                       n = strcmp_count(match->add,
                                completed->smallest_start);
                        completed->smallest_start[n] = '\0';
                }
                completed->count_match++;
        }
 
-       TAILQ_INSERT_TAIL(&completed->match_items, item, next);
+       TAILQ_INSERT_TAIL(&compnode->matches, match, next);
        completed->count++;
 
        return 0;
+
+fail:
+       ec_completed_match_free(match);
+       return ret;
 }
 
-int ec_completed_add_match(struct ec_completed *completed,
-                       struct ec_parsed *state,
-                       const struct ec_node *node, const char *add)
+int
+ec_completed_add_match(struct ec_completed *completed,
+               struct ec_parsed *parsed_state,
+               const struct ec_node *node, const char *add)
 {
-       struct ec_completed_item *item;
-       int ret;
-
-       item = ec_completed_item(EC_MATCH, state, node, add);
-       if (item == NULL)
-               return -ENOMEM;
+       return __ec_completed_add_match(EC_MATCH, completed, parsed_state,
+                                       node, add);
+}
 
-       ret = ec_completed_add_item(completed, item);
-       if (ret < 0) {
-               ec_completed_item_free(item);
-               return ret;
-       }
+int
+ec_completed_add_no_match(struct ec_completed *completed,
+               struct ec_parsed *parsed_state,
+               const struct ec_node *node)
+{
+       return __ec_completed_add_match(EC_NO_MATCH, completed, parsed_state,
+                                       node, NULL);
+}
 
-       return 0;
+int
+ec_completed_add_partial_match(struct ec_completed *completed,
+               struct ec_parsed *parsed_state,
+               const struct ec_node *node, const char *add)
+{
+       return __ec_completed_add_match(EC_PARTIAL_MATCH, completed, parsed_state,
+                                       node, add);
 }
 
-int ec_completed_add_no_match(struct ec_completed *completed,
-                       struct ec_parsed *state, const struct ec_node *node)
+int
+ec_completed_add_node(struct ec_completed *completed,
+               const struct ec_node *node)
 {
-       struct ec_completed_item *item;
-       int ret;
+       struct ec_completed_node *compnode = NULL;
 
-       item = ec_completed_item(EC_NO_MATCH, state, node, NULL);
-       if (item == NULL)
+       compnode = ec_completed_node(node);
+       if (compnode == NULL)
                return -ENOMEM;
 
-       ret = ec_completed_add_item(completed, item);
-       if (ret < 0) {
-               ec_completed_item_free(item);
-               return ret;
-       }
-
+       TAILQ_INSERT_TAIL(&completed->nodes, compnode, next);
        return 0;
 }
 
-void ec_completed_item_free(struct ec_completed_item *item)
+void ec_completed_match_free(struct ec_completed_match *match)
 {
-       ec_free(item->add);
-       ec_free(item->path);
-       ec_free(item);
+       ec_free(match->add);
+       ec_free(match->path);
+       ec_free(match);
 }
 
 /* default completion function: return a no-match element */
-struct ec_completed *ec_node_default_complete(const struct ec_node *gen_node,
-                                       struct ec_parsed *state,
-                                       const struct ec_strvec *strvec)
+int
+ec_node_default_complete(const struct ec_node *gen_node,
+                       struct ec_completed *completed,
+                       struct ec_parsed *parsed_state,
+                       const struct ec_strvec *strvec)
 {
-       struct ec_completed *completed;
-
-       (void)strvec;
-       (void)state;
-
-       completed = ec_completed();
-       if (completed == NULL)
-               return NULL;
+       int ret;
 
        if (ec_strvec_len(strvec) != 1)
-               return completed;
-
-       if (ec_completed_add_no_match(completed, state, gen_node) < 0) {
-               ec_completed_free(completed);
-               return NULL;
-       }
-
-       return completed;
-}
-
-void ec_completed_merge(struct ec_completed *completed1,
-       struct ec_completed *completed2)
-{
-       struct ec_completed_item *item;
-
-       assert(completed1 != NULL);
-       assert(completed2 != NULL);
+               return 0;
 
-       while (!TAILQ_EMPTY(&completed2->match_items)) {
-               item = TAILQ_FIRST(&completed2->match_items);
-               TAILQ_REMOVE(&completed2->match_items, item, next);
-               ec_completed_add_item(completed1, item);
-       }
+       ret = ec_completed_add_no_match(completed, parsed_state, gen_node);
+       if (ret < 0)
+               return ret;
 
-       ec_completed_free(completed2);
+       return 0;
 }
 
 void ec_completed_free(struct ec_completed *completed)
 {
-       struct ec_completed_item *item;
+       struct ec_completed_node *compnode;
+       struct ec_completed_match *item;
 
        if (completed == NULL)
                return;
 
-       while (!TAILQ_EMPTY(&completed->match_items)) {
-               item = TAILQ_FIRST(&completed->match_items);
-               TAILQ_REMOVE(&completed->match_items, item, next);
-               ec_completed_item_free(item);
+       while (!TAILQ_EMPTY(&completed->nodes)) {
+               compnode = TAILQ_FIRST(&completed->nodes);
+               TAILQ_REMOVE(&completed->nodes, compnode, next);
+
+               while (!TAILQ_EMPTY(&compnode->matches)) {
+                       item = TAILQ_FIRST(&compnode->matches);
+                       TAILQ_REMOVE(&compnode->matches, item, next);
+                       ec_completed_match_free(item);
+               }
+               ec_free(compnode);
        }
        ec_free(completed->smallest_start);
        ec_free(completed);
@@ -336,7 +372,8 @@ void ec_completed_free(struct ec_completed *completed)
 
 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
 {
-       struct ec_completed_item *item;
+       struct ec_completed_node *compnode;
+       struct ec_completed_match *item;
 
        if (completed == NULL || completed->count == 0) {
                fprintf(out, "no completion\n");
@@ -347,9 +384,21 @@ void ec_completed_dump(FILE *out, const struct ec_completed *completed)
                completed->count, completed->count_match,
                completed->smallest_start);
 
-       TAILQ_FOREACH(item, &completed->match_items, next) {
-               fprintf(out, "add=<%s>, node=%p, node_type=%s\n",
-                       item->add, item->node, item->node->type->name);
+       TAILQ_FOREACH(compnode, &completed->nodes, next) {
+               fprintf(out, "node=%p, node_type=%s\n",
+                       compnode->node, compnode->node->type->name);
+               TAILQ_FOREACH(item, &compnode->matches, next) {
+                       const char *typestr;
+
+                       switch (item->type) {
+                       case EC_NO_MATCH: typestr = "no-match"; break;
+                       case EC_MATCH: typestr = "match"; break;
+                       case EC_PARTIAL_MATCH: typestr = "partial-match"; break;
+                       default: typestr = "unknown"; break;
+                       }
+
+                       fprintf(out, "  type=%s add=<%s>\n", typestr, item->add);
+               }
        }
 }
 
@@ -391,37 +440,56 @@ ec_completed_iter(struct ec_completed *completed,
 
        iter->completed = completed;
        iter->type = type;
-       iter->cur = NULL;
+       iter->cur_node = NULL;
+       iter->cur_match = NULL;
 
        return iter;
 }
 
-const struct ec_completed_item *ec_completed_iter_next(
+const struct ec_completed_match *ec_completed_iter_next(
        struct ec_completed_iter *iter)
 {
-       if (iter->completed == NULL)
-               return NULL;
+       const struct ec_completed *completed = iter->completed;
+       const struct ec_completed_node *cur_node;
+       const struct ec_completed_match *cur_match;
 
-       do {
-               if (iter->cur == NULL)
-                       iter->cur = TAILQ_FIRST(&completed->match_items);
-               else
-                       iter->cur = TAILQ_NEXT(iter->cur, next);
-
-               if (iter->cur == NULL)
-                       break;
+       if (completed == NULL)
+               return NULL;
 
-               if (iter->cur->add == NULL &&
-                               (iter->type & EC_NO_MATCH))
-                       break;
+       cur_node = iter->cur_node;
+       cur_match = iter->cur_match;
 
-               if (iter->cur->add != NULL &&
-                               (iter->type & EC_MATCH))
-                       break;
+       /* first call */
+       if (cur_node == NULL) {
+               TAILQ_FOREACH(cur_node, &completed->nodes, next) {
+                       TAILQ_FOREACH(cur_match, &cur_node->matches, next) {
+                               if (cur_match != NULL &&
+                                               cur_match->type & iter->type)
+                                       goto found;
+                       }
+               }
+               return NULL;
+       } else {
+               cur_match = TAILQ_NEXT(cur_match, next);
+               if (cur_match != NULL &&
+                               cur_match->type & iter->type)
+                       goto found;
+               cur_node = TAILQ_NEXT(cur_node, next);
+               while (cur_node != NULL) {
+                       cur_match = TAILQ_FIRST(&cur_node->matches);
+                       if (cur_match != NULL &&
+                                       cur_match->type & iter->type)
+                               goto found;
+                       cur_node = TAILQ_NEXT(cur_node, next);
+               }
+               return NULL;
+       }
 
-       } while (iter->cur != NULL);
+found:
+       iter->cur_node = cur_node;
+       iter->cur_match = cur_match;
 
-       return iter->cur;
+       return iter->cur_match;
 }
 
 void ec_completed_iter_free(struct ec_completed_iter *iter)