x
[protos/libecoli.git] / lib / ecoli_completed.c
index 8ef14d5..0b0e641 100644 (file)
@@ -47,38 +47,76 @@ struct ec_completed *ec_completed(void)
        if (completed == NULL)
                return NULL;
 
-       TAILQ_INIT(&completed->elts);
-       completed->count_match = 0;
+       TAILQ_INIT(&completed->nodes);
+       TAILQ_INIT(&completed->matches);
 
        return completed;
 }
 
-struct ec_completed_elt *ec_completed_elt(const struct ec_node *node,
-       const char *add)
+struct ec_completed *
+ec_node_complete_child(struct ec_node *node,
+               struct ec_parsed *state,
+               const struct ec_strvec *strvec)
 {
-       struct ec_completed_elt *elt = NULL;
-
-       elt = ec_calloc(1, sizeof(*elt));
-       if (elt == NULL)
-               return NULL;
+       struct ec_completed *completed;
+       struct ec_parsed *child;
+       int ret;
 
-       elt->node = node;
-       if (add != NULL) {
-               elt->add = ec_strdup(add);
-               if (elt->add == NULL) {
-                       ec_completed_elt_free(elt);
-                       return NULL;
+       /* 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;
+                       }
                }
        }
+       node->flags |= EC_NODE_F_BUILT;
+
+       if (node->type->complete == NULL) {
+               errno = ENOTSUP;
+               return NULL;
+       }
+
+       child = ec_parsed();
+       if (child == NULL)
+               return NULL;
+
+       child->node = node;
+       ec_parsed_add_child(state, child);
+       completed = node->type->complete(node, child, strvec);
 
-       return elt;
+#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);
+#endif
+
+       ec_parsed_del_child(state, child);
+       assert(TAILQ_EMPTY(&child->children));
+       ec_parsed_free(child);
+
+       return completed;
+}
+
+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;
+
+       if (state == NULL)
+               return NULL;
+
+       completed = ec_node_complete_child(node, state, strvec);
+       ec_parsed_free(state);
+
+       return completed;
 }
 
-/* XXX define when to use ec_node_complete() or node->complete()
- * (same for parse)
- * suggestion: node->op() is internal, user calls the function
- * other idea: have 2 functions
- */
 struct ec_completed *ec_node_complete(struct ec_node *node,
        const char *str)
 {
@@ -105,106 +143,176 @@ struct ec_completed *ec_node_complete(struct ec_node *node,
        return NULL;
 }
 
-/* default completion function: return a no-match element */
-struct ec_completed *ec_node_default_complete(const struct ec_node *gen_node,
-       const struct ec_strvec *strvec)
+/* count the number of identical chars at the beginning of 2 strings */
+static size_t strcmp_count(const char *s1, const char *s2)
 {
-       struct ec_completed *completed;
-       struct ec_completed_elt *completed_elt;
+       size_t i = 0;
 
-       (void)strvec;
+       while (s1[i] && s2[i] && s1[i] == s2[i])
+               i++;
 
-       completed = ec_completed();
-       if (completed == NULL)
-               return NULL;
+       return i;
+}
 
-       if (ec_strvec_len(strvec) != 1)
-               return completed;
+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)
+{
+       struct ec_completed_item *item = NULL;
 
-       completed_elt = ec_completed_elt(gen_node, NULL);
-       if (completed_elt == NULL) {
-               ec_completed_free(completed);
+       item = ec_calloc(1, sizeof(*item));
+       if (item == NULL)
                return NULL;
-       }
 
-       ec_completed_add_elt(completed, completed_elt);
+       /* XXX can state be NULL? */
+       if (state != NULL) {
+               struct ec_parsed *p;
+               size_t len;
 
-       return completed;
-}
+               /* get path len */
+               for (p = state, len = 0; p != NULL;
+                    p = ec_parsed_get_parent(p), len++)
+                       ;
 
-struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
-       const struct ec_strvec *strvec)
-{
-       int ret;
+               item->path = ec_calloc(len, sizeof(*item->path));
+               if (item->path == NULL)
+                       goto fail;
 
-       /* 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;
-                       }
-               }
-       }
-       node->flags |= EC_NODE_F_BUILT;
+               item->pathlen = len;
 
-       if (node->type->complete == NULL) {
-               errno = ENOTSUP;
-               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;
        }
 
-       return node->type->complete(node, strvec);
-}
+       item->type = type;
+       item->node = node;
+       if (add != NULL) {
+               item->add = ec_strdup(add);
+               if (item->add == NULL)
+                       goto fail;
+       }
 
-/* count the number of identical chars at the beginning of 2 strings */
-static size_t strcmp_count(const char *s1, const char *s2)
-{
-       size_t i = 0;
+       return item;
 
-       while (s1[i] && s2[i] && s1[i] == s2[i])
-               i++;
+fail:
+       if (item != NULL) {
+               ec_free(item->path);
+               ec_free(item->add);
+       }
+       ec_completed_item_free(item);
 
-       return i;
+       return NULL;
 }
 
-void ec_completed_add_elt(
-       struct ec_completed *completed, struct ec_completed_elt *elt)
+static int ec_completed_add_item(struct ec_completed *completed,
+                               struct ec_completed_item *item)
 {
        size_t n;
 
-       TAILQ_INSERT_TAIL(&completed->elts, elt, next);
-       completed->count++;
-       if (elt->add != NULL) {
-               completed->count_match++;
+       if (item->add != NULL) {
                if (completed->smallest_start == NULL) {
-                       completed->smallest_start = ec_strdup(elt->add);
+                       completed->smallest_start = ec_strdup(item->add);
+                       if (completed->smallest_start == NULL)
+                               return -ENOMEM;
                } else {
-                       n = strcmp_count(elt->add,
+                       n = strcmp_count(item->add,
                                completed->smallest_start);
                        completed->smallest_start[n] = '\0';
                }
+               completed->count_match++;
+       }
+
+       TAILQ_INSERT_TAIL(&completed->matches, item, next);
+       completed->count++;
+
+       return 0;
+}
+
+int ec_completed_add_match(struct ec_completed *completed,
+                       struct ec_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;
+
+       ret = ec_completed_add_item(completed, item);
+       if (ret < 0) {
+               ec_completed_item_free(item);
+               return ret;
        }
+
+       return 0;
+}
+
+int ec_completed_add_no_match(struct ec_completed *completed,
+                       struct ec_parsed *state, const struct ec_node *node)
+{
+       struct ec_completed_item *item;
+       int ret;
+
+       item = ec_completed_item(EC_NO_MATCH, state, node, NULL);
+       if (item == NULL)
+               return -ENOMEM;
+
+       ret = ec_completed_add_item(completed, item);
+       if (ret < 0) {
+               ec_completed_item_free(item);
+               return ret;
+       }
+
+       return 0;
 }
 
-void ec_completed_elt_free(struct ec_completed_elt *elt)
+void ec_completed_item_free(struct ec_completed_item *item)
 {
-       ec_free(elt->add);
-       ec_free(elt);
+       ec_free(item->add);
+       ec_free(item->path);
+       ec_free(item);
+}
+
+/* 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)
+{
+       struct ec_completed *completed;
+
+       (void)strvec;
+       (void)state;
+
+       completed = ec_completed();
+       if (completed == NULL)
+               return NULL;
+
+       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_elt *elt;
+       struct ec_completed_item *item;
 
        assert(completed1 != NULL);
        assert(completed2 != NULL);
 
-       while (!TAILQ_EMPTY(&completed2->elts)) {
-               elt = TAILQ_FIRST(&completed2->elts);
-               TAILQ_REMOVE(&completed2->elts, elt, next);
-               ec_completed_add_elt(completed1, elt);
+       while (!TAILQ_EMPTY(&completed2->matches)) {
+               item = TAILQ_FIRST(&completed2->matches);
+               TAILQ_REMOVE(&completed2->matches, item, next);
+               ec_completed_add_item(completed1, item);
        }
 
        ec_completed_free(completed2);
@@ -212,15 +320,15 @@ void ec_completed_merge(struct ec_completed *completed1,
 
 void ec_completed_free(struct ec_completed *completed)
 {
-       struct ec_completed_elt *elt;
+       struct ec_completed_item *item;
 
        if (completed == NULL)
                return;
 
-       while (!TAILQ_EMPTY(&completed->elts)) {
-               elt = TAILQ_FIRST(&completed->elts);
-               TAILQ_REMOVE(&completed->elts, elt, next);
-               ec_completed_elt_free(elt);
+       while (!TAILQ_EMPTY(&completed->matches)) {
+               item = TAILQ_FIRST(&completed->matches);
+               TAILQ_REMOVE(&completed->matches, item, next);
+               ec_completed_item_free(item);
        }
        ec_free(completed->smallest_start);
        ec_free(completed);
@@ -228,7 +336,7 @@ void ec_completed_free(struct ec_completed *completed)
 
 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
 {
-       struct ec_completed_elt *elt;
+       struct ec_completed_item *item;
 
        if (completed == NULL || completed->count == 0) {
                fprintf(out, "no completion\n");
@@ -239,9 +347,9 @@ void ec_completed_dump(FILE *out, const struct ec_completed *completed)
                completed->count, completed->count_match,
                completed->smallest_start);
 
-       TAILQ_FOREACH(elt, &completed->elts, next) {
+       TAILQ_FOREACH(item, &completed->matches, next) {
                fprintf(out, "add=<%s>, node=%p, node_type=%s\n",
-                       elt->add, elt->node, elt->node->type->name);
+                       item->add, item->node, item->node->type->name);
        }
 }
 
@@ -256,16 +364,16 @@ const char *ec_completed_smallest_start(
 
 unsigned int ec_completed_count(
        const struct ec_completed *completed,
-       enum ec_completed_filter_flags flags)
+       enum ec_completed_type type)
 {
        unsigned int count = 0;
 
        if (completed == NULL)
                return count;
 
-       if (flags & EC_MATCH)
+       if (type & EC_MATCH)
                count += completed->count_match;
-       if (flags & EC_NO_MATCH)
+       if (type & EC_NO_MATCH)
                count += (completed->count - completed->count_match); //XXX
 
        return count;
@@ -273,7 +381,7 @@ unsigned int ec_completed_count(
 
 struct ec_completed_iter *
 ec_completed_iter(struct ec_completed *completed,
-       enum ec_completed_filter_flags flags)
+       enum ec_completed_type type)
 {
        struct ec_completed_iter *iter;
 
@@ -282,39 +390,40 @@ ec_completed_iter(struct ec_completed *completed,
                return NULL;
 
        iter->completed = completed;
-       iter->flags = flags;
-       iter->cur = NULL;
+       iter->type = type;
+       iter->cur_item = NULL;
 
        return iter;
 }
 
-const struct ec_completed_elt *ec_completed_iter_next(
+const struct ec_completed_item *ec_completed_iter_next(
        struct ec_completed_iter *iter)
 {
-       if (iter->completed == NULL)
+       const struct ec_completed *completed = iter->completed;
+
+       if (completed == NULL)
                return NULL;
 
        do {
-               if (iter->cur == NULL) {
-                       iter->cur = TAILQ_FIRST(&iter->completed->elts);
-               } else {
-                       iter->cur = TAILQ_NEXT(iter->cur, next);
-               }
+               if (iter->cur_item == NULL)
+                       iter->cur_item = TAILQ_FIRST(&completed->matches);
+               else
+                       iter->cur_item = TAILQ_NEXT(iter->cur_item, next);
 
-               if (iter->cur == NULL)
+               if (iter->cur_item == NULL)
                        break;
 
-               if (iter->cur->add == NULL &&
-                               (iter->flags & EC_NO_MATCH))
+               if (iter->cur_item->add == NULL &&
+                               (iter->type & EC_NO_MATCH))
                        break;
 
-               if (iter->cur->add != NULL &&
-                               (iter->flags & EC_MATCH))
+               if (iter->cur_item->add != NULL &&
+                               (iter->type & EC_MATCH))
                        break;
 
-       } while (iter->cur != NULL);
+       } while (iter->cur_item != NULL);
 
-       return iter->cur;
+       return iter->cur_item;
 }
 
 void ec_completed_iter_free(struct ec_completed_iter *iter)