X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=src%2Fecoli_complete.c;h=b1afb33ac0dd211094c9f6e5449cfb079e5a6cf2;hb=67b4d8d2b2593e3e64a848ce548bc0fd76bc440a;hp=a9becdf67ff4fd4206c4f38194d928afee06a523;hpb=18d03456d96f7a086a2ccc82ce97fcf056848d90;p=protos%2Flibecoli.git diff --git a/src/ecoli_complete.c b/src/ecoli_complete.c index a9becdf..b1afb33 100644 --- a/src/ecoli_complete.c +++ b/src/ecoli_complete.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -31,10 +31,34 @@ struct ec_comp_item { char *full; /* the full token after completion */ char *completion; /* chars that are added, NULL if not applicable */ char *display; /* what should be displayed by help/completers */ - struct ec_keyval *attrs; + struct ec_dict *attrs; }; -struct ec_comp *ec_comp(struct ec_parse *state) +TAILQ_HEAD(ec_comp_item_list, ec_comp_item); + +struct ec_comp_group { + /* XXX counts ? */ + TAILQ_ENTRY(ec_comp_group) next; + const struct ec_node *node; + struct ec_comp_item_list items; + struct ec_pnode *state; + struct ec_dict *attrs; +}; + +TAILQ_HEAD(ec_comp_group_list, ec_comp_group); + +struct ec_comp { + unsigned count; + unsigned count_full; + unsigned count_partial; + unsigned count_unknown; + struct ec_pnode *cur_state; + struct ec_comp_group *cur_group; + struct ec_comp_group_list groups; + struct ec_dict *attrs; +}; + +struct ec_comp *ec_comp(struct ec_pnode *state) { struct ec_comp *comp = NULL; @@ -42,7 +66,7 @@ struct ec_comp *ec_comp(struct ec_parse *state) if (comp == NULL) goto fail; - comp->attrs = ec_keyval(); + comp->attrs = ec_dict(); if (comp->attrs == NULL) goto fail; @@ -54,26 +78,38 @@ struct ec_comp *ec_comp(struct ec_parse *state) fail: if (comp != NULL) - ec_keyval_free(comp->attrs); + ec_dict_free(comp->attrs); ec_free(comp); return NULL; } -struct ec_parse *ec_comp_get_state(struct ec_comp *comp) +struct ec_pnode *ec_comp_get_state(const struct ec_comp *comp) { return comp->cur_state; } +struct ec_comp_group *ec_comp_get_group(const struct ec_comp *comp) +{ + return comp->cur_group; +} + +struct ec_dict *ec_comp_get_attrs(const struct ec_comp *comp) +{ + return comp->attrs; +} + int -ec_node_complete_child(const struct ec_node *node, +ec_complete_child(const struct ec_node *node, struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_parse *child_state, *cur_state; + struct ec_pnode *child_state, *cur_state; struct ec_comp_group *cur_group; int ret; + //XXX call ec_complete_unknown() instead, as + //described in API doc. if (ec_node_type(node)->complete == NULL) { errno = ENOTSUP; return -1; @@ -81,12 +117,12 @@ ec_node_complete_child(const struct ec_node *node, /* save previous parse state, prepare child state */ cur_state = comp->cur_state; - child_state = ec_parse(node); + child_state = ec_pnode(node); if (child_state == NULL) return -1; if (cur_state != NULL) - ec_parse_link_child(cur_state, child_state); + ec_pnode_link_child(cur_state, child_state); comp->cur_state = child_state; cur_group = comp->cur_group; comp->cur_group = NULL; @@ -96,10 +132,10 @@ ec_node_complete_child(const struct ec_node *node, /* restore parent parse state */ if (cur_state != NULL) { - ec_parse_unlink_child(cur_state, child_state); - assert(!ec_parse_has_child(child_state)); + ec_pnode_unlink_child(cur_state, child_state); + assert(!ec_pnode_has_child(child_state)); } - ec_parse_free(child_state); + ec_pnode_free(child_state); comp->cur_state = cur_state; comp->cur_group = cur_group; @@ -109,7 +145,7 @@ ec_node_complete_child(const struct ec_node *node, return 0; } -struct ec_comp *ec_node_complete_strvec(const struct ec_node *node, +struct ec_comp *ec_complete_strvec(const struct ec_node *node, const struct ec_strvec *strvec) { struct ec_comp *comp = NULL; @@ -119,7 +155,7 @@ struct ec_comp *ec_node_complete_strvec(const struct ec_node *node, if (comp == NULL) goto fail; - ret = ec_node_complete_child(node, comp, strvec); + ret = ec_complete_child(node, comp, strvec); if (ret < 0) goto fail; @@ -130,7 +166,7 @@ fail: return NULL; } -struct ec_comp *ec_node_complete(const struct ec_node *node, +struct ec_comp *ec_complete(const struct ec_node *node, const char *str) { struct ec_strvec *strvec = NULL; @@ -144,7 +180,7 @@ struct ec_comp *ec_node_complete(const struct ec_node *node, if (ec_strvec_add(strvec, str) < 0) goto fail; - comp = ec_node_complete_strvec(node, strvec); + comp = ec_complete_strvec(node, strvec); if (comp == NULL) goto fail; @@ -157,7 +193,7 @@ struct ec_comp *ec_node_complete(const struct ec_node *node, } static struct ec_comp_group * -ec_comp_group(const struct ec_node *node, struct ec_parse *parse) +ec_comp_group(const struct ec_node *node, struct ec_pnode *parse) { struct ec_comp_group *grp = NULL; @@ -165,11 +201,11 @@ ec_comp_group(const struct ec_node *node, struct ec_parse *parse) if (grp == NULL) return NULL; - grp->attrs = ec_keyval(); + grp->attrs = ec_dict(); if (grp->attrs == NULL) goto fail; - grp->state = ec_parse_dup(parse); + grp->state = ec_pnode_dup(parse); if (grp->state == NULL) goto fail; @@ -180,8 +216,8 @@ ec_comp_group(const struct ec_node *node, struct ec_parse *parse) fail: if (grp != NULL) { - ec_parse_free(grp->state); - ec_keyval_free(grp->attrs); + ec_pnode_free(grp->state); + ec_dict_free(grp->attrs); } ec_free(grp); return NULL; @@ -192,7 +228,7 @@ ec_comp_item(enum ec_comp_type type, const char *start, const char *full) { struct ec_comp_item *item = NULL; - struct ec_keyval *attrs = NULL; + struct ec_dict *attrs = NULL; char *comp_cp = NULL, *start_cp = NULL; char *full_cp = NULL, *display_cp = NULL; @@ -209,7 +245,7 @@ ec_comp_item(enum ec_comp_type type, if (item == NULL) goto fail; - attrs = ec_keyval(); + attrs = ec_dict(); if (attrs == NULL) goto fail; @@ -243,7 +279,7 @@ ec_comp_item(enum ec_comp_type type, return item; fail: - ec_keyval_free(attrs); + ec_dict_free(attrs); ec_free(comp_cp); ec_free(start_cp); ec_free(full_cp); @@ -417,7 +453,7 @@ ec_comp_item_free(struct ec_comp_item *item) ec_free(item->start); ec_free(item->completion); ec_free(item->display); - ec_keyval_free(item->attrs); + ec_dict_free(item->attrs); ec_free(item); } @@ -449,9 +485,10 @@ fail: return -1; } +/* XXX move in helpers + rename ? */ /* return a completion item of type "unknown" */ int -ec_node_complete_unknown(const struct ec_node *gen_node, +ec_complete_unknown(const struct ec_node *gen_node, struct ec_comp *comp, const struct ec_strvec *strvec) { @@ -480,11 +517,29 @@ static void ec_comp_group_free(struct ec_comp_group *grp) TAILQ_REMOVE(&grp->items, item, next); ec_comp_item_free(item); } - ec_parse_free(ec_parse_get_root(grp->state)); - ec_keyval_free(grp->attrs); + ec_pnode_free(ec_pnode_get_root(grp->state)); + ec_dict_free(grp->attrs); ec_free(grp); } +const struct ec_node * +ec_comp_group_get_node(const struct ec_comp_group *grp) +{ + return grp->node; +} + +const struct ec_pnode * +ec_comp_group_get_state(const struct ec_comp_group *grp) +{ + return grp->state; +} + +const struct ec_dict * +ec_comp_group_get_attrs(const struct ec_comp_group *grp) +{ + return grp->attrs; +} + void ec_comp_free(struct ec_comp *comp) { struct ec_comp_group *grp; @@ -497,7 +552,7 @@ void ec_comp_free(struct ec_comp *comp) TAILQ_REMOVE(&comp->groups, grp, next); ec_comp_group_free(grp); } - ec_keyval_free(comp->attrs); + ec_dict_free(comp->attrs); ec_free(comp); } @@ -573,76 +628,55 @@ unsigned int ec_comp_count( return count; } -struct ec_comp_iter * -ec_comp_iter(const struct ec_comp *comp, - enum ec_comp_type type) -{ - struct ec_comp_iter *iter; - - iter = ec_calloc(1, sizeof(*iter)); - if (iter == NULL) - return NULL; - - iter->comp = comp; - iter->type = type; - iter->cur_node = NULL; - iter->cur_match = NULL; - - return iter; -} - -struct ec_comp_item *ec_comp_iter_next( - struct ec_comp_iter *iter) +static struct ec_comp_item * +__ec_comp_iter_next(const struct ec_comp *comp, struct ec_comp_item *item, + enum ec_comp_type type) { - const struct ec_comp *comp; - struct ec_comp_group *cur_node; + struct ec_comp_group *cur_grp; struct ec_comp_item *cur_match; - if (iter == NULL) - return NULL; - comp = iter->comp; - if (comp == NULL) - return NULL; - - cur_node = iter->cur_node; - cur_match = iter->cur_match; - /* first call */ - if (cur_node == NULL) { - TAILQ_FOREACH(cur_node, &comp->groups, next) { - TAILQ_FOREACH(cur_match, &cur_node->items, next) { - if (cur_match != NULL && - cur_match->type & iter->type) - goto found; + if (item == NULL) { + TAILQ_FOREACH(cur_grp, &comp->groups, next) { + TAILQ_FOREACH(cur_match, &cur_grp->items, next) { + if (cur_match->type & type) + return cur_match; } } return NULL; - } else { + } + + cur_grp = item->grp; + cur_match = TAILQ_NEXT(item, next); + while (cur_match != NULL) { + if (cur_match->type & type) + return cur_match; 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); + } + cur_grp = TAILQ_NEXT(cur_grp, next); + while (cur_grp != NULL) { + TAILQ_FOREACH(cur_match, &cur_grp->items, next) { + if (cur_match->type & type) + return cur_match; } - return NULL; } -found: - iter->cur_node = cur_node; - iter->cur_match = cur_match; + return NULL; +} - return iter->cur_match; +struct ec_comp_item * +ec_comp_iter_next(struct ec_comp_item *item, enum ec_comp_type type) +{ + if (item == NULL) + return NULL; + return __ec_comp_iter_next(item->grp->comp, item, type); } -void ec_comp_iter_free(struct ec_comp_iter *iter) + +struct ec_comp_item * +ec_comp_iter_first(const struct ec_comp *comp, enum ec_comp_type type) { - ec_free(iter); + return __ec_comp_iter_next(comp, NULL, type); } /* LCOV_EXCL_START */ @@ -650,7 +684,6 @@ static int ec_comp_testcase(void) { struct ec_node *node = NULL; struct ec_comp *c = NULL; - struct ec_comp_iter *iter = NULL; struct ec_comp_item *item; FILE *f = NULL; char *buf = NULL; @@ -664,19 +697,19 @@ static int ec_comp_testcase(void) if (node == NULL) goto fail; - c = ec_node_complete(node, "xcdscds"); + c = ec_complete(node, "xcdscds"); testres |= EC_TEST_CHECK( c != NULL && ec_comp_count(c, EC_COMP_ALL) == 0, "complete count should is not 0\n"); ec_comp_free(c); - c = ec_node_complete(node, "x"); + c = ec_complete(node, "x"); testres |= EC_TEST_CHECK( c != NULL && ec_comp_count(c, EC_COMP_ALL) == 1, "complete count should is not 1\n"); ec_comp_free(c); - c = ec_node_complete(node, ""); + c = ec_complete(node, ""); testres |= EC_TEST_CHECK( c != NULL && ec_comp_count(c, EC_COMP_ALL) == 2, "complete count should is not 2\n"); @@ -707,8 +740,7 @@ static int ec_comp_testcase(void) free(buf); buf = NULL; - iter = ec_comp_iter(c, EC_COMP_ALL); - item = ec_comp_iter_next(iter); + item = ec_comp_iter_first(c, EC_COMP_ALL); if (item == NULL) goto fail; @@ -722,7 +754,7 @@ static int ec_comp_testcase(void) !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_x"), "bad item node\n"); - item = ec_comp_iter_next(iter); + item = ec_comp_iter_next(item, EC_COMP_ALL); if (item == NULL) goto fail; @@ -736,17 +768,15 @@ static int ec_comp_testcase(void) !strcmp(ec_node_id(ec_comp_item_get_node(item)), "id_y"), "bad item node\n"); - item = ec_comp_iter_next(iter); + item = ec_comp_iter_next(item, EC_COMP_ALL); testres |= EC_TEST_CHECK(item == NULL, "should be the last item\n"); - ec_comp_iter_free(iter); ec_comp_free(c); ec_node_free(node); return testres; fail: - ec_comp_iter_free(iter); ec_comp_free(c); ec_node_free(node); if (f != NULL)