change add to full str
[protos/libecoli.git] / lib / ecoli_completed.c
index 8a34c74..728b284 100644 (file)
@@ -47,38 +47,95 @@ struct ec_completed *ec_completed(void)
        if (completed == NULL)
                return NULL;
 
-       TAILQ_INIT(&completed->elts);
-       completed->count_match = 0;
+       TAILQ_INIT(&completed->nodes);
 
        return completed;
 }
 
-struct ec_completed_elt *ec_completed_elt(const struct ec_node *node,
-       const char *add)
+/* 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_completed *completed,
+               struct ec_parsed *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_parsed *child_state = NULL;
+       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)
+                               return ret;
                }
        }
+       node->flags |= EC_NODE_F_BUILT;
+
+       if (node->type->complete == NULL)
+               return -ENOTSUP;
+
+       child_state = ec_parsed();
+       if (child_state == NULL)
+               return -ENOMEM;
+       child_state->node = node;
+       ec_parsed_add_child(parsed_state, child_state);
 
-       return elt;
+       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, parsed_state);
+#endif
+
+       ec_parsed_del_child(parsed_state, child_state);
+       assert(TAILQ_EMPTY(&child_state->children));
+       ec_parsed_free(child_state);
+
+       return 0;
+}
+
+struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
+       const struct ec_strvec *strvec)
+{
+       struct ec_parsed *parsed_state = NULL;
+       struct ec_completed *completed = NULL;
+       int ret;
+
+       parsed_state = ec_parsed();
+       if (parsed_state == NULL)
+               goto fail;
+
+       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;
 }
 
-/* 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,180 +162,308 @@ 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_node *
+ec_completed_node(const struct ec_node *node)
+{
+       struct ec_completed_node *compnode = NULL;
 
-       completed_elt = ec_completed_elt(gen_node, NULL);
-       if (completed_elt == NULL) {
-               ec_completed_free(completed);
+       compnode = ec_calloc(1, sizeof(*compnode));
+       if (compnode == NULL)
                return NULL;
-       }
 
-       ec_completed_add_elt(completed, completed_elt);
+       compnode->node = node;
+       TAILQ_INIT(&compnode->items);
 
-       return completed;
+       return compnode;
 }
 
-struct ec_completed *ec_node_complete_strvec(struct ec_node *node,
-       const struct ec_strvec *strvec)
+static struct ec_completed_item *
+ec_completed_item(struct ec_parsed *state, const struct ec_node *node)
 {
-       int ret;
+       struct ec_completed_item *item = NULL;
+       struct ec_parsed *p;
+       size_t len;
 
-       /* 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;
-                       }
-               }
+       item = ec_calloc(1, sizeof(*item));
+       if (item == NULL)
+               return NULL;
+
+       /* 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 = EC_NO_MATCH;
+       item->node = node;
+
+       return item;
+
+fail:
+       if (item != NULL) {
+               ec_free(item->path);
+               ec_free(item->str);
        }
-       node->flags |= EC_NODE_F_BUILT;
+       ec_completed_item_free(item);
 
-       if (node->type->complete == NULL) {
-               errno = ENOTSUP;
-               return NULL;
+       return NULL;
+}
+
+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 *str)
+{
+       struct ec_completed_node *compnode = NULL;
+       struct ec_completed_item *item = NULL;
+       int ret = -ENOMEM;
+
+       /* find the compnode entry corresponding to this node */
+       TAILQ_FOREACH(compnode, &completed->nodes, next) {
+               if (compnode->node == node)
+                       break;
        }
+       if (compnode == NULL)
+               return -ENOENT;
 
-#if 0 // XXX dump
-       {
-               struct ec_completed *c;
-               c = node->type->complete(node, strvec);
-
-               printf("--------------------------------------------------------------\n");
-               ec_node_dump(stdout, node);
-               ec_strvec_dump(stdout, strvec);
-               ec_completed_dump(stdout, c);
-               return c;
+       item = ec_completed_item(parsed_state, node);
+       if (item == NULL)
+               goto fail;
+       item->type = type;
+       if (str != NULL) {
+               item->str = ec_strdup(str);
+               if (item->str == NULL)
+                       goto fail;
        }
-#else
-       return node->type->complete(node, strvec);
-#endif
+
+       if (item->str != NULL)
+               completed->count_match++;
+
+       TAILQ_INSERT_TAIL(&compnode->items, item, next);
+       completed->count++;
+
+       return 0;
+
+fail:
+       ec_completed_item_free(item);
+       return ret;
 }
 
-/* count the number of identical chars at the beginning of 2 strings */
-static size_t strcmp_count(const char *s1, const char *s2)
+int
+ec_completed_add_match(struct ec_completed *completed,
+               struct ec_parsed *parsed_state,
+               const struct ec_node *node, const char *str)
 {
-       size_t i = 0;
+       struct ec_completed_node *compnode = NULL;
+       struct ec_completed_item *item = NULL;
+       int ret = -ENOMEM;
 
-       while (s1[i] && s2[i] && s1[i] == s2[i])
-               i++;
+       /* find the compnode entry corresponding to this node */
+       TAILQ_FOREACH(compnode, &completed->nodes, next) {
+               if (compnode->node == node)
+                       break;
+       }
+       if (compnode == NULL)
+               return -ENOENT;
 
-       return i;
+       item = ec_completed_item(parsed_state, node);
+       if (item == NULL)
+               goto fail;
+       item->type = EC_MATCH;
+       if (str != NULL) {
+               item->str = ec_strdup(str);
+               if (item->str == NULL)
+                       goto fail;
+       }
+
+       if (item->str != NULL)
+               completed->count_match++;
+
+       TAILQ_INSERT_TAIL(&compnode->items, item, next);
+       completed->count++;
+
+       return 0;
+
+fail:
+       ec_completed_item_free(item);
+       return ret;
 }
 
-void ec_completed_add_elt(
-       struct ec_completed *completed, struct ec_completed_elt *elt)
+int
+ec_completed_add_no_match(struct ec_completed *completed,
+               struct ec_parsed *parsed_state,
+               const struct ec_node *node)
 {
-       size_t n;
+       return __ec_completed_add_match(EC_NO_MATCH, completed, parsed_state,
+                                       node, NULL);
+}
 
-       TAILQ_INSERT_TAIL(&completed->elts, elt, next);
-       completed->count++;
-       if (elt->add != NULL) {
-               completed->count_match++;
-               if (completed->smallest_start == NULL) {
-                       completed->smallest_start = ec_strdup(elt->add);
-               } else {
-                       n = strcmp_count(elt->add,
-                               completed->smallest_start);
-                       completed->smallest_start[n] = '\0';
-               }
-       }
+int
+ec_completed_add_partial_match(struct ec_completed *completed,
+               struct ec_parsed *parsed_state,
+               const struct ec_node *node, const char *str)
+{
+       return __ec_completed_add_match(EC_PARTIAL_MATCH, completed, parsed_state,
+                                       node, str);
+}
+
+int
+ec_completed_add_node(struct ec_completed *completed,
+               const struct ec_node *node)
+{
+       struct ec_completed_node *compnode = NULL;
+
+       compnode = ec_completed_node(node);
+       if (compnode == NULL)
+               return -ENOMEM;
+
+       TAILQ_INSERT_TAIL(&completed->nodes, compnode, next);
+       return 0;
 }
 
-void ec_completed_elt_free(struct ec_completed_elt *elt)
+void ec_completed_item_free(struct ec_completed_item *match)
 {
-       ec_free(elt->add);
-       ec_free(elt);
+       ec_free(match->str);
+       ec_free(match->path);
+       ec_free(match);
 }
 
-void ec_completed_merge(struct ec_completed *completed1,
-       struct ec_completed *completed2)
+/* default completion function: return a no-match element */
+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_elt *elt;
+       int ret;
 
-       assert(completed1 != NULL);
-       assert(completed2 != NULL);
+       if (ec_strvec_len(strvec) != 1)
+               return 0;
 
-       while (!TAILQ_EMPTY(&completed2->elts)) {
-               elt = TAILQ_FIRST(&completed2->elts);
-               TAILQ_REMOVE(&completed2->elts, elt, next);
-               ec_completed_add_elt(completed1, elt);
-       }
+       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_elt *elt;
+       struct ec_completed_node *compnode;
+       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->nodes)) {
+               compnode = TAILQ_FIRST(&completed->nodes);
+               TAILQ_REMOVE(&completed->nodes, compnode, next);
+
+               while (!TAILQ_EMPTY(&compnode->items)) {
+                       item = TAILQ_FIRST(&compnode->items);
+                       TAILQ_REMOVE(&compnode->items, item, next);
+                       ec_completed_item_free(item);
+               }
+               ec_free(compnode);
        }
-       ec_free(completed->smallest_start);
        ec_free(completed);
 }
 
 void ec_completed_dump(FILE *out, const struct ec_completed *completed)
 {
-       struct ec_completed_elt *elt;
+       struct ec_completed_node *compnode;
+       struct ec_completed_item *item;
 
        if (completed == NULL || completed->count == 0) {
                fprintf(out, "no completion\n");
                return;
        }
 
-       fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n",
-               completed->count, completed->count_match,
-               completed->smallest_start);
+       fprintf(out, "completion: count=%u match=%u\n",
+               completed->count, completed->count_match);
+
+       TAILQ_FOREACH(compnode, &completed->nodes, next) {
+               fprintf(out, "node=%p, node_type=%s\n",
+                       compnode->node, compnode->node->type->name);
+               TAILQ_FOREACH(item, &compnode->items, next) {
+                       const char *typestr;
 
-       TAILQ_FOREACH(elt, &completed->elts, next) {
-               fprintf(out, "add=<%s>, node=%p, node_type=%s\n",
-                       elt->add, elt->node, elt->node->type->name);
+                       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 str=<%s>\n", typestr, item->str);
+               }
        }
 }
 
-const char *ec_completed_smallest_start(
-       const struct ec_completed *completed)
+char *ec_completed_smallest_start(const struct ec_completed *completed)
 {
-       if (completed == NULL || completed->smallest_start == NULL)
-               return "";
+       struct ec_completed_node *compnode;
+       struct ec_completed_item *item;
+       char *smallest_start = NULL;
+       size_t n;
 
-       return completed->smallest_start;
+       if (completed == NULL)
+               goto fail;
+
+       TAILQ_FOREACH(compnode, &completed->nodes, next) {
+               TAILQ_FOREACH(item, &compnode->items, next) {
+                       if (item->str == NULL)
+                               continue;
+                       if (smallest_start == NULL) {
+                               smallest_start = ec_strdup(item->str);
+                               if (smallest_start == NULL)
+                                       goto fail;
+                       } else {
+                               n = strcmp_count(item->str, smallest_start);
+                               smallest_start[n] = '\0';
+                       }
+               }
+       }
+
+       return smallest_start;
+
+fail:
+       ec_free(smallest_start);
+       return NULL;
 }
 
 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;
@@ -286,7 +471,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;
 
@@ -295,39 +480,57 @@ ec_completed_iter(struct ec_completed *completed,
                return NULL;
 
        iter->completed = completed;
-       iter->flags = flags;
-       iter->cur = NULL;
+       iter->type = type;
+       iter->cur_node = NULL;
+       iter->cur_match = 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)
-               return NULL;
+       const struct ec_completed *completed = iter->completed;
+       const struct ec_completed_node *cur_node;
+       const struct ec_completed_item *cur_match;
 
-       do {
-               if (iter->cur == NULL) {
-                       iter->cur = TAILQ_FIRST(&iter->completed->elts);
-               } else {
-                       iter->cur = TAILQ_NEXT(iter->cur, next);
-               }
-
-               if (iter->cur == NULL)
-                       break;
+       if (completed == NULL)
+               return NULL;
 
-               if (iter->cur->add == NULL &&
-                               (iter->flags & EC_NO_MATCH))
-                       break;
+       cur_node = iter->cur_node;
+       cur_match = iter->cur_match;
 
-               if (iter->cur->add != NULL &&
-                               (iter->flags & EC_MATCH))
-                       break;
+       /* first call */
+       if (cur_node == NULL) {
+               TAILQ_FOREACH(cur_node, &completed->nodes, next) {
+                       TAILQ_FOREACH(cur_match, &cur_node->items, 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->items);
+                       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)