From: Olivier Matz Date: Mon, 12 Mar 2018 20:51:25 +0000 (+0100) Subject: completed -> comp X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=539a859dde94b304e93f44a52b7e7fc0734e3a5d;p=protos%2Flibecoli.git completed -> comp --- diff --git a/lib/Makefile b/lib/Makefile index 9ee7f97..a6d5057 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -20,7 +20,7 @@ LDFLAGS += --coverage srcs := srcs += ecoli_assert.c -srcs += ecoli_completed.c +srcs += ecoli_complete.c srcs += ecoli_keyval.c srcs += ecoli_init.c srcs += ecoli_log.c diff --git a/lib/ecoli_complete.c b/lib/ecoli_complete.c new file mode 100644 index 0000000..72abdf9 --- /dev/null +++ b/lib/ecoli_complete.c @@ -0,0 +1,636 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2016, Olivier MATZ + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +struct ec_comp_item { + TAILQ_ENTRY(ec_comp_item) next; + enum ec_comp_type type; + const struct ec_node *node; + struct ec_comp_group *grp; + char *start; /* the initial token */ + 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_comp *ec_comp(struct ec_parsed *state) +{ + struct ec_comp *comp = NULL; + + comp = ec_calloc(1, sizeof(*comp)); + if (comp == NULL) + goto fail; + + comp->attrs = ec_keyval(); + if (comp->attrs == NULL) + goto fail; + + TAILQ_INIT(&comp->groups); + + comp->cur_state = state; + + return comp; + + fail: + if (comp != NULL) + ec_keyval_free(comp->attrs); + ec_free(comp); + + return NULL; +} + +struct ec_parsed *ec_comp_get_state(struct ec_comp *comp) +{ + return comp->cur_state; +} + +int +ec_node_complete_child(const struct ec_node *node, + struct ec_comp *comp, + const struct ec_strvec *strvec) +{ + struct ec_parsed *child_state, *cur_state; + struct ec_comp_group *cur_group; + int ret; + + if (node->type->complete == NULL) + return -ENOTSUP; + + /* save previous parse state, prepare child state */ + cur_state = comp->cur_state; + child_state = ec_parsed(node); + if (child_state == NULL) + return -ENOMEM; + + if (cur_state != NULL) + ec_parsed_link_child(cur_state, child_state); + comp->cur_state = child_state; + cur_group = comp->cur_group; + comp->cur_group = NULL; + + /* fill the comp struct with items */ + ret = node->type->complete(node, comp, strvec); + + /* restore parent parse state */ + if (cur_state != NULL) { + ec_parsed_unlink_child(cur_state, child_state); + assert(!ec_parsed_has_child(child_state)); + } + ec_parsed_free(child_state); + comp->cur_state = cur_state; + comp->cur_group = cur_group; + + if (ret < 0) + return ret; + + return 0; +} + +struct ec_comp *ec_node_complete_strvec(const struct ec_node *node, + const struct ec_strvec *strvec) +{ + struct ec_comp *comp = NULL; + int ret; + + comp = ec_comp(NULL); + if (comp == NULL) + goto fail; + + ret = ec_node_complete_child(node, comp, strvec); + if (ret < 0) + goto fail; + + return comp; + +fail: + ec_comp_free(comp); + return NULL; +} + +struct ec_comp *ec_node_complete(const struct ec_node *node, + const char *str) +{ + struct ec_strvec *strvec = NULL; + struct ec_comp *comp; + + errno = ENOMEM; + strvec = ec_strvec(); + if (strvec == NULL) + goto fail; + + if (ec_strvec_add(strvec, str) < 0) + goto fail; + + comp = ec_node_complete_strvec(node, strvec); + if (comp == NULL) + goto fail; + + ec_strvec_free(strvec); + return comp; + + fail: + ec_strvec_free(strvec); + return NULL; +} + +static struct ec_comp_group * +ec_comp_group(const struct ec_node *node, struct ec_parsed *parsed) +{ + struct ec_comp_group *grp = NULL; + + grp = ec_calloc(1, sizeof(*grp)); + if (grp == NULL) + return NULL; + + grp->attrs = ec_keyval(); + if (grp->attrs == NULL) + goto fail; + + grp->state = ec_parsed_dup(parsed); + if (grp->state == NULL) + goto fail; + + grp->node = node; + TAILQ_INIT(&grp->items); + + return grp; + +fail: + if (grp != NULL) { + ec_parsed_free(grp->state); + ec_keyval_free(grp->attrs); + } + ec_free(grp); + return NULL; +} + +static struct ec_comp_item * +ec_comp_item(const struct ec_node *node, enum ec_comp_type type, + const char *start, const char *full) +{ + struct ec_comp_item *item = NULL; + struct ec_keyval *attrs = NULL; + char *comp_cp = NULL, *start_cp = NULL; + char *full_cp = NULL, *display_cp = NULL; + + if (type == EC_COMP_UNKNOWN && full != NULL) { + errno = EINVAL; + return NULL; + } + if (type != EC_COMP_UNKNOWN && full == NULL) { + errno = EINVAL; + return NULL; + } + + item = ec_calloc(1, sizeof(*item)); + if (item == NULL) + goto fail; + + attrs = ec_keyval(); + if (attrs == NULL) + goto fail; + + if (start != NULL) { + start_cp = ec_strdup(start); + if (start_cp == NULL) + goto fail; + + if (ec_str_startswith(full, start)) { + comp_cp = ec_strdup(&full[strlen(start)]); + if (comp_cp == NULL) + goto fail; + } + } + if (full != NULL) { + full_cp = ec_strdup(full); + if (full_cp == NULL) + goto fail; + display_cp = ec_strdup(full); + if (display_cp == NULL) + goto fail; + } + + item->node = node; + item->type = type; + item->start = start_cp; + item->full = full_cp; + item->completion = comp_cp; + item->display = display_cp; + item->attrs = attrs; + + return item; + +fail: + ec_keyval_free(attrs); + ec_free(comp_cp); + ec_free(start_cp); + ec_free(full_cp); + ec_free(display_cp); + ec_free(item); + + return NULL; +} + +int ec_comp_item_set_display(struct ec_comp_item *item, + const char *display) +{ + char *display_copy = NULL; + int ret = 0; + + if (item == NULL || display == NULL || + item->type == EC_COMP_UNKNOWN) + return -EINVAL; + + display_copy = ec_strdup(display); + if (display_copy == NULL) + goto fail; + + ec_free(item->display); + item->display = display_copy; + + return 0; + +fail: + ec_free(display_copy); + return ret; +} + +int +ec_comp_item_set_completion(struct ec_comp_item *item, + const char *completion) +{ + char *completion_copy = NULL; + int ret = 0; + + if (item == NULL || completion == NULL || + item->type == EC_COMP_UNKNOWN) + return -EINVAL; + + ret = -ENOMEM; + completion_copy = ec_strdup(completion); + if (completion_copy == NULL) + goto fail; + + ec_free(item->completion); + item->completion = completion_copy; + + return 0; + +fail: + ec_free(completion_copy); + return ret; +} + +int +ec_comp_item_set_str(struct ec_comp_item *item, + const char *str) +{ + char *str_copy = NULL; + int ret = 0; + + if (item == NULL || str == NULL || + item->type == EC_COMP_UNKNOWN) + return -EINVAL; + + ret = -ENOMEM; + str_copy = ec_strdup(str); + if (str_copy == NULL) + goto fail; + + ec_free(item->full); + item->full = str_copy; + + return 0; + +fail: + ec_free(str_copy); + return ret; +} + +static int +ec_comp_item_add(struct ec_comp *comp, + struct ec_comp_item *item) +{ + if (comp == NULL || item == NULL || item->node == NULL) + return -EINVAL; + + switch (item->type) { + case EC_COMP_UNKNOWN: + comp->count_unknown++; + break; + case EC_COMP_FULL: + comp->count_full++; + break; + case EC_COMP_PARTIAL: + comp->count_partial++; + break; + default: + return -EINVAL; + } + + if (comp->cur_group == NULL) { + struct ec_comp_group *grp; + + grp = ec_comp_group(item->node, comp->cur_state); + if (grp == NULL) + return -ENOMEM; + TAILQ_INSERT_TAIL(&comp->groups, grp, next); + comp->cur_group = grp; + } + + comp->count++; + TAILQ_INSERT_TAIL(&comp->cur_group->items, item, next); + item->grp = comp->cur_group; + + return 0; +} + +const char * +ec_comp_item_get_str(const struct ec_comp_item *item) +{ + return item->full; +} + +const char * +ec_comp_item_get_display(const struct ec_comp_item *item) +{ + return item->display; +} + +const char * +ec_comp_item_get_completion(const struct ec_comp_item *item) +{ + return item->completion; +} + +enum ec_comp_type +ec_comp_item_get_type(const struct ec_comp_item *item) +{ + return item->type; +} + +const struct ec_node * +ec_comp_item_get_node(const struct ec_comp_item *item) +{ + return item->node; +} + +const struct ec_comp_group * +ec_comp_item_get_grp(const struct ec_comp_item *item) +{ + return item->grp; +} + +static void +ec_comp_item_free(struct ec_comp_item *item) +{ + if (item == NULL) + return; + + ec_free(item->full); + ec_free(item->start); + ec_free(item->completion); + ec_free(item->display); + ec_keyval_free(item->attrs); + ec_free(item); +} + +int ec_comp_add_item(struct ec_comp *comp, + const struct ec_node *node, + struct ec_comp_item **p_item, + enum ec_comp_type type, + const char *start, const char *full) +{ + struct ec_comp_item *item = NULL; + int ret; + + item = ec_comp_item(node, type, start, full); + if (item == NULL) + return -1; + + ret = ec_comp_item_add(comp, item); + if (ret < 0) + goto fail; + + if (p_item != NULL) + *p_item = item; + + return 0; + +fail: + ec_comp_item_free(item); + + return -1; +} + +/* return a completion item of type "unknown" */ +int +ec_node_complete_unknown(const struct ec_node *gen_node, + struct ec_comp *comp, + const struct ec_strvec *strvec) +{ + int ret; + + if (ec_strvec_len(strvec) != 1) + return 0; + + ret = ec_comp_add_item(comp, gen_node, NULL, + EC_COMP_UNKNOWN, NULL, NULL); + if (ret < 0) + return ret; + + return 0; +} + +static void ec_comp_group_free(struct ec_comp_group *grp) +{ + struct ec_comp_item *item; + + if (grp == NULL) + return; + + while (!TAILQ_EMPTY(&grp->items)) { + item = TAILQ_FIRST(&grp->items); + TAILQ_REMOVE(&grp->items, item, next); + ec_comp_item_free(item); + } + ec_parsed_free(ec_parsed_get_root(grp->state)); + ec_keyval_free(grp->attrs); + ec_free(grp); +} + +void ec_comp_free(struct ec_comp *comp) +{ + struct ec_comp_group *grp; + + if (comp == NULL) + return; + + while (!TAILQ_EMPTY(&comp->groups)) { + grp = TAILQ_FIRST(&comp->groups); + TAILQ_REMOVE(&comp->groups, grp, next); + ec_comp_group_free(grp); + } + ec_keyval_free(comp->attrs); + ec_free(comp); +} + +void ec_comp_dump(FILE *out, const struct ec_comp *comp) +{ + struct ec_comp_group *grp; + struct ec_comp_item *item; + + if (comp == NULL || comp->count == 0) { + fprintf(out, "no completion\n"); + return; + } + + fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n", + comp->count, comp->count_full, + comp->count_partial, comp->count_unknown); + + TAILQ_FOREACH(grp, &comp->groups, next) { + fprintf(out, "node=%p, node_type=%s\n", + grp->node, grp->node->type->name); + TAILQ_FOREACH(item, &grp->items, next) { + const char *typestr; + + switch (item->type) { + case EC_COMP_UNKNOWN: typestr = "unknown"; break; + case EC_COMP_FULL: typestr = "full"; break; + case EC_COMP_PARTIAL: typestr = "partial"; break; + default: typestr = "unknown"; break; + } + + fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n", + typestr, item->full, item->completion, + item->display); + } + } +} + +int ec_comp_merge(struct ec_comp *to, + struct ec_comp *from) +{ + struct ec_comp_group *grp; + + while (!TAILQ_EMPTY(&from->groups)) { + grp = TAILQ_FIRST(&from->groups); + TAILQ_REMOVE(&from->groups, grp, next); + TAILQ_INSERT_TAIL(&to->groups, grp, next); + } + to->count += from->count; + to->count_full += from->count_full; + to->count_partial += from->count_partial; + to->count_unknown += from->count_unknown; + + ec_comp_free(from); + return 0; +} + +unsigned int ec_comp_count( + const struct ec_comp *comp, + enum ec_comp_type type) +{ + unsigned int count = 0; + + if (comp == NULL) + return count; + + if (type & EC_COMP_FULL) + count += comp->count_full; + if (type & EC_COMP_PARTIAL) + count += comp->count_partial; + if (type & EC_COMP_UNKNOWN) + count += comp->count_unknown; + + return count; +} + +struct ec_comp_iter * +ec_comp_iter(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) +{ + struct ec_comp *comp; + struct ec_comp_group *cur_node; + 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; + } + } + 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; + } + +found: + iter->cur_node = cur_node; + iter->cur_match = cur_match; + + return iter->cur_match; +} + +void ec_comp_iter_free(struct ec_comp_iter *iter) +{ + ec_free(iter); +} diff --git a/lib/ecoli_complete.h b/lib/ecoli_complete.h new file mode 100644 index 0000000..123d3fe --- /dev/null +++ b/lib/ecoli_complete.h @@ -0,0 +1,234 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2016, Olivier MATZ + */ + +/** + * API for generating completions item on a node. + * + * This file provide helpers to list and manipulate the possible + * completions for a given input. + * + * XXX comp vs item + */ + +#ifndef ECOLI_COMPLETE_ +#define ECOLI_COMPLETE_ + +#include +#include +#include + +struct ec_node; + +enum ec_comp_type { + EC_COMP_UNKNOWN = 0x1, + EC_COMP_FULL = 0x2, + EC_COMP_PARTIAL = 0x4, + EC_COMP_ALL = 0x7, +}; + +struct ec_comp_item; + +TAILQ_HEAD(ec_comp_item_list, ec_comp_item); + +struct ec_comp_group { + TAILQ_ENTRY(ec_comp_group) next; + const struct ec_node *node; + struct ec_comp_item_list items; + struct ec_parsed *state; + struct ec_keyval *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_parsed *cur_state; + struct ec_comp_group *cur_group; + struct ec_comp_group_list groups; + struct ec_keyval *attrs; +}; + +/* + * return a comp object filled with items + * return NULL on error (nomem?) + */ +struct ec_comp *ec_node_complete(const struct ec_node *node, + const char *str); +struct ec_comp *ec_node_complete_strvec(const struct ec_node *node, + const struct ec_strvec *strvec); + +/* internal: used by nodes */ +int ec_node_complete_child(const struct ec_node *node, + struct ec_comp *comp, + const struct ec_strvec *strvec); + +/** + * Create a completion object (list of completion items). + * + * + */ +struct ec_comp *ec_comp(struct ec_parsed *state); + +/** + * Free a completion object and all its items. + * + * + */ +void ec_comp_free(struct ec_comp *comp); + +/** + * + * + * + */ +void ec_comp_dump(FILE *out, + const struct ec_comp *comp); + +/** + * Merge items contained in 'from' into 'to' + * + * The 'from' comp struct is freed. + */ +int ec_comp_merge(struct ec_comp *to, + struct ec_comp *from); + +struct ec_parsed *ec_comp_get_state(struct ec_comp *comp); + +/* shortcut for ec_comp_item() + ec_comp_item_add() */ +int ec_comp_add_item(struct ec_comp *comp, + const struct ec_node *node, + struct ec_comp_item **p_item, + enum ec_comp_type type, + const char *start, const char *full); + +/** + * + */ +int ec_comp_item_set_str(struct ec_comp_item *item, + const char *str); + +/** + * Get the string value of a completion item. + * + * + */ +const char * +ec_comp_item_get_str(const struct ec_comp_item *item); + +/** + * Get the display string value of a completion item. + * + * + */ +const char * +ec_comp_item_get_display(const struct ec_comp_item *item); + +/** + * Get the completion string value of a completion item. + * + * + */ +const char * +ec_comp_item_get_completion(const struct ec_comp_item *item); + +/** + * Get the group of a completion item. + * + * + */ +const struct ec_comp_group * +ec_comp_item_get_grp(const struct ec_comp_item *item); + +/** + * Get the type of a completion item. + * + * + */ +enum ec_comp_type +ec_comp_item_get_type(const struct ec_comp_item *item); + +/** + * Get the node associated to a completion item. + * + * + */ +const struct ec_node * +ec_comp_item_get_node(const struct ec_comp_item *item); + +/** + * Set the display value of an item. + * + * + */ +int ec_comp_item_set_display(struct ec_comp_item *item, + const char *display); + +/** + * Set the completion value of an item. + * + * + */ +int ec_comp_item_set_completion(struct ec_comp_item *item, + const char *completion); + +/** + * + * + * + */ +int +ec_node_complete_unknown(const struct ec_node *gen_node, + struct ec_comp *comp, + const struct ec_strvec *strvec); + +/** + * + * + * + */ +unsigned int ec_comp_count( + const struct ec_comp *comp, + enum ec_comp_type flags); + +/** + * + * + * + */ +struct ec_comp_iter { + enum ec_comp_type type; + struct ec_comp *comp; + struct ec_comp_group *cur_node; + struct ec_comp_item *cur_match; +}; + +/** + * + * + * + */ +struct ec_comp_iter * +ec_comp_iter(struct ec_comp *comp, + enum ec_comp_type type); + +/** + * + * + * + */ +struct ec_comp_item *ec_comp_iter_next( + struct ec_comp_iter *iter); + +/** + * + * + * + */ +void ec_comp_iter_free(struct ec_comp_iter *iter); + + +#endif diff --git a/lib/ecoli_completed.c b/lib/ecoli_completed.c deleted file mode 100644 index cde9e9d..0000000 --- a/lib/ecoli_completed.c +++ /dev/null @@ -1,636 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * Copyright 2016, Olivier MATZ - */ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -struct ec_completed_item { - TAILQ_ENTRY(ec_completed_item) next; - enum ec_completed_type type; - const struct ec_node *node; - struct ec_completed_group *grp; - char *start; /* the initial token */ - 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_completed *ec_completed(struct ec_parsed *state) -{ - struct ec_completed *completed = NULL; - - completed = ec_calloc(1, sizeof(*completed)); - if (completed == NULL) - goto fail; - - completed->attrs = ec_keyval(); - if (completed->attrs == NULL) - goto fail; - - TAILQ_INIT(&completed->groups); - - completed->cur_state = state; - - return completed; - - fail: - if (completed != NULL) - ec_keyval_free(completed->attrs); - ec_free(completed); - - return NULL; -} - -struct ec_parsed *ec_completed_get_state(struct ec_completed *completed) -{ - return completed->cur_state; -} - -int -ec_node_complete_child(const struct ec_node *node, - struct ec_completed *completed, - const struct ec_strvec *strvec) -{ - struct ec_parsed *child_state, *cur_state; - struct ec_completed_group *cur_group; - int ret; - - if (node->type->complete == NULL) - return -ENOTSUP; - - /* save previous parse state, prepare child state */ - cur_state = completed->cur_state; - child_state = ec_parsed(node); - if (child_state == NULL) - return -ENOMEM; - - if (cur_state != NULL) - ec_parsed_link_child(cur_state, child_state); - completed->cur_state = child_state; - cur_group = completed->cur_group; - completed->cur_group = NULL; - - /* fill the completed struct with items */ - ret = node->type->complete(node, completed, strvec); - - /* restore parent parse state */ - if (cur_state != NULL) { - ec_parsed_unlink_child(cur_state, child_state); - assert(!ec_parsed_has_child(child_state)); - } - ec_parsed_free(child_state); - completed->cur_state = cur_state; - completed->cur_group = cur_group; - - if (ret < 0) - return ret; - - return 0; -} - -struct ec_completed *ec_node_complete_strvec(const struct ec_node *node, - const struct ec_strvec *strvec) -{ - struct ec_completed *completed = NULL; - int ret; - - completed = ec_completed(NULL); - if (completed == NULL) - goto fail; - - ret = ec_node_complete_child(node, completed, strvec); - if (ret < 0) - goto fail; - - return completed; - -fail: - ec_completed_free(completed); - return NULL; -} - -struct ec_completed *ec_node_complete(const struct ec_node *node, - const char *str) -{ - struct ec_strvec *strvec = NULL; - struct ec_completed *completed; - - errno = ENOMEM; - strvec = ec_strvec(); - if (strvec == NULL) - goto fail; - - if (ec_strvec_add(strvec, str) < 0) - goto fail; - - completed = ec_node_complete_strvec(node, strvec); - if (completed == NULL) - goto fail; - - ec_strvec_free(strvec); - return completed; - - fail: - ec_strvec_free(strvec); - return NULL; -} - -static struct ec_completed_group * -ec_completed_group(const struct ec_node *node, struct ec_parsed *parsed) -{ - struct ec_completed_group *grp = NULL; - - grp = ec_calloc(1, sizeof(*grp)); - if (grp == NULL) - return NULL; - - grp->attrs = ec_keyval(); - if (grp->attrs == NULL) - goto fail; - - grp->state = ec_parsed_dup(parsed); - if (grp->state == NULL) - goto fail; - - grp->node = node; - TAILQ_INIT(&grp->items); - - return grp; - -fail: - if (grp != NULL) { - ec_parsed_free(grp->state); - ec_keyval_free(grp->attrs); - } - ec_free(grp); - return NULL; -} - -static struct ec_completed_item * -ec_completed_item(const struct ec_node *node, enum ec_completed_type type, - const char *start, const char *full) -{ - struct ec_completed_item *item = NULL; - struct ec_keyval *attrs = NULL; - char *comp_cp = NULL, *start_cp = NULL; - char *full_cp = NULL, *display_cp = NULL; - - if (type == EC_COMP_UNKNOWN && full != NULL) { - errno = EINVAL; - return NULL; - } - if (type != EC_COMP_UNKNOWN && full == NULL) { - errno = EINVAL; - return NULL; - } - - item = ec_calloc(1, sizeof(*item)); - if (item == NULL) - goto fail; - - attrs = ec_keyval(); - if (attrs == NULL) - goto fail; - - if (start != NULL) { - start_cp = ec_strdup(start); - if (start_cp == NULL) - goto fail; - - if (ec_str_startswith(full, start)) { - comp_cp = ec_strdup(&full[strlen(start)]); - if (comp_cp == NULL) - goto fail; - } - } - if (full != NULL) { - full_cp = ec_strdup(full); - if (full_cp == NULL) - goto fail; - display_cp = ec_strdup(full); - if (display_cp == NULL) - goto fail; - } - - item->node = node; - item->type = type; - item->start = start_cp; - item->full = full_cp; - item->completion = comp_cp; - item->display = display_cp; - item->attrs = attrs; - - return item; - -fail: - ec_keyval_free(attrs); - ec_free(comp_cp); - ec_free(start_cp); - ec_free(full_cp); - ec_free(display_cp); - ec_free(item); - - return NULL; -} - -int ec_completed_item_set_display(struct ec_completed_item *item, - const char *display) -{ - char *display_copy = NULL; - int ret = 0; - - if (item == NULL || display == NULL || - item->type == EC_COMP_UNKNOWN) - return -EINVAL; - - display_copy = ec_strdup(display); - if (display_copy == NULL) - goto fail; - - ec_free(item->display); - item->display = display_copy; - - return 0; - -fail: - ec_free(display_copy); - return ret; -} - -int -ec_completed_item_set_completion(struct ec_completed_item *item, - const char *completion) -{ - char *completion_copy = NULL; - int ret = 0; - - if (item == NULL || completion == NULL || - item->type == EC_COMP_UNKNOWN) - return -EINVAL; - - ret = -ENOMEM; - completion_copy = ec_strdup(completion); - if (completion_copy == NULL) - goto fail; - - ec_free(item->completion); - item->completion = completion_copy; - - return 0; - -fail: - ec_free(completion_copy); - return ret; -} - -int -ec_completed_item_set_str(struct ec_completed_item *item, - const char *str) -{ - char *str_copy = NULL; - int ret = 0; - - if (item == NULL || str == NULL || - item->type == EC_COMP_UNKNOWN) - return -EINVAL; - - ret = -ENOMEM; - str_copy = ec_strdup(str); - if (str_copy == NULL) - goto fail; - - ec_free(item->full); - item->full = str_copy; - - return 0; - -fail: - ec_free(str_copy); - return ret; -} - -static int -ec_completed_item_add(struct ec_completed *completed, - struct ec_completed_item *item) -{ - if (completed == NULL || item == NULL || item->node == NULL) - return -EINVAL; - - switch (item->type) { - case EC_COMP_UNKNOWN: - completed->count_unknown++; - break; - case EC_COMP_FULL: - completed->count_full++; - break; - case EC_COMP_PARTIAL: - completed->count_partial++; - break; - default: - return -EINVAL; - } - - if (completed->cur_group == NULL) { - struct ec_completed_group *grp; - - grp = ec_completed_group(item->node, completed->cur_state); - if (grp == NULL) - return -ENOMEM; - TAILQ_INSERT_TAIL(&completed->groups, grp, next); - completed->cur_group = grp; - } - - completed->count++; - TAILQ_INSERT_TAIL(&completed->cur_group->items, item, next); - item->grp = completed->cur_group; - - return 0; -} - -const char * -ec_completed_item_get_str(const struct ec_completed_item *item) -{ - return item->full; -} - -const char * -ec_completed_item_get_display(const struct ec_completed_item *item) -{ - return item->display; -} - -const char * -ec_completed_item_get_completion(const struct ec_completed_item *item) -{ - return item->completion; -} - -enum ec_completed_type -ec_completed_item_get_type(const struct ec_completed_item *item) -{ - return item->type; -} - -const struct ec_node * -ec_completed_item_get_node(const struct ec_completed_item *item) -{ - return item->node; -} - -const struct ec_completed_group * -ec_completed_item_get_grp(const struct ec_completed_item *item) -{ - return item->grp; -} - -static void -ec_completed_item_free(struct ec_completed_item *item) -{ - if (item == NULL) - return; - - ec_free(item->full); - ec_free(item->start); - ec_free(item->completion); - ec_free(item->display); - ec_keyval_free(item->attrs); - ec_free(item); -} - -int ec_completed_add_item(struct ec_completed *completed, - const struct ec_node *node, - struct ec_completed_item **p_item, - enum ec_completed_type type, - const char *start, const char *full) -{ - struct ec_completed_item *item = NULL; - int ret; - - item = ec_completed_item(node, type, start, full); - if (item == NULL) - return -1; - - ret = ec_completed_item_add(completed, item); - if (ret < 0) - goto fail; - - if (p_item != NULL) - *p_item = item; - - return 0; - -fail: - ec_completed_item_free(item); - - return -1; -} - -/* return a completion item of type "unknown" */ -int -ec_node_complete_unknown(const struct ec_node *gen_node, - struct ec_completed *completed, - const struct ec_strvec *strvec) -{ - int ret; - - if (ec_strvec_len(strvec) != 1) - return 0; - - ret = ec_completed_add_item(completed, gen_node, NULL, - EC_COMP_UNKNOWN, NULL, NULL); - if (ret < 0) - return ret; - - return 0; -} - -static void ec_completed_group_free(struct ec_completed_group *grp) -{ - struct ec_completed_item *item; - - if (grp == NULL) - return; - - while (!TAILQ_EMPTY(&grp->items)) { - item = TAILQ_FIRST(&grp->items); - TAILQ_REMOVE(&grp->items, item, next); - ec_completed_item_free(item); - } - ec_parsed_free(ec_parsed_get_root(grp->state)); - ec_keyval_free(grp->attrs); - ec_free(grp); -} - -void ec_completed_free(struct ec_completed *completed) -{ - struct ec_completed_group *grp; - - if (completed == NULL) - return; - - while (!TAILQ_EMPTY(&completed->groups)) { - grp = TAILQ_FIRST(&completed->groups); - TAILQ_REMOVE(&completed->groups, grp, next); - ec_completed_group_free(grp); - } - ec_keyval_free(completed->attrs); - ec_free(completed); -} - -void ec_completed_dump(FILE *out, const struct ec_completed *completed) -{ - struct ec_completed_group *grp; - struct ec_completed_item *item; - - if (completed == NULL || completed->count == 0) { - fprintf(out, "no completion\n"); - return; - } - - fprintf(out, "completion: count=%u full=%u partial=%u unknown=%u\n", - completed->count, completed->count_full, - completed->count_partial, completed->count_unknown); - - TAILQ_FOREACH(grp, &completed->groups, next) { - fprintf(out, "node=%p, node_type=%s\n", - grp->node, grp->node->type->name); - TAILQ_FOREACH(item, &grp->items, next) { - const char *typestr; - - switch (item->type) { - case EC_COMP_UNKNOWN: typestr = "unknown"; break; - case EC_COMP_FULL: typestr = "full"; break; - case EC_COMP_PARTIAL: typestr = "partial"; break; - default: typestr = "unknown"; break; - } - - fprintf(out, " type=%s str=<%s> comp=<%s> disp=<%s>\n", - typestr, item->full, item->completion, - item->display); - } - } -} - -int ec_completed_merge(struct ec_completed *to, - struct ec_completed *from) -{ - struct ec_completed_group *grp; - - while (!TAILQ_EMPTY(&from->groups)) { - grp = TAILQ_FIRST(&from->groups); - TAILQ_REMOVE(&from->groups, grp, next); - TAILQ_INSERT_TAIL(&to->groups, grp, next); - } - to->count += from->count; - to->count_full += from->count_full; - to->count_partial += from->count_partial; - to->count_unknown += from->count_unknown; - - ec_completed_free(from); - return 0; -} - -unsigned int ec_completed_count( - const struct ec_completed *completed, - enum ec_completed_type type) -{ - unsigned int count = 0; - - if (completed == NULL) - return count; - - if (type & EC_COMP_FULL) - count += completed->count_full; - if (type & EC_COMP_PARTIAL) - count += completed->count_partial; - if (type & EC_COMP_UNKNOWN) - count += completed->count_unknown; - - return count; -} - -struct ec_completed_iter * -ec_completed_iter(struct ec_completed *completed, - enum ec_completed_type type) -{ - struct ec_completed_iter *iter; - - iter = ec_calloc(1, sizeof(*iter)); - if (iter == NULL) - return NULL; - - iter->completed = completed; - iter->type = type; - iter->cur_node = NULL; - iter->cur_match = NULL; - - return iter; -} - -struct ec_completed_item *ec_completed_iter_next( - struct ec_completed_iter *iter) -{ - struct ec_completed *completed; - struct ec_completed_group *cur_node; - struct ec_completed_item *cur_match; - - if (iter == NULL) - return NULL; - completed = iter->completed; - if (completed == NULL) - return NULL; - - cur_node = iter->cur_node; - cur_match = iter->cur_match; - - /* first call */ - if (cur_node == NULL) { - TAILQ_FOREACH(cur_node, &completed->groups, 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; - } - -found: - iter->cur_node = cur_node; - iter->cur_match = cur_match; - - return iter->cur_match; -} - -void ec_completed_iter_free(struct ec_completed_iter *iter) -{ - ec_free(iter); -} diff --git a/lib/ecoli_completed.h b/lib/ecoli_completed.h deleted file mode 100644 index 7aeb905..0000000 --- a/lib/ecoli_completed.h +++ /dev/null @@ -1,234 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * Copyright 2016, Olivier MATZ - */ - -/** - * API for generating completions item on a node. - * - * This file provide helpers to list and manipulate the possible - * completions for a given input. - * - * XXX completed vs item - */ - -#ifndef ECOLI_COMPLETED_ -#define ECOLI_COMPLETED_ - -#include -#include -#include - -struct ec_node; - -enum ec_completed_type { - EC_COMP_UNKNOWN = 0x1, - EC_COMP_FULL = 0x2, - EC_COMP_PARTIAL = 0x4, - EC_COMP_ALL = 0x7, -}; - -struct ec_completed_item; - -TAILQ_HEAD(ec_completed_item_list, ec_completed_item); - -struct ec_completed_group { - TAILQ_ENTRY(ec_completed_group) next; - const struct ec_node *node; - struct ec_completed_item_list items; - struct ec_parsed *state; - struct ec_keyval *attrs; -}; - -TAILQ_HEAD(ec_completed_group_list, ec_completed_group); - -struct ec_completed { - unsigned count; - unsigned count_full; - unsigned count_partial; - unsigned count_unknown; - struct ec_parsed *cur_state; - struct ec_completed_group *cur_group; - struct ec_completed_group_list groups; - struct ec_keyval *attrs; -}; - -/* - * return a completed object filled with items - * return NULL on error (nomem?) - */ -struct ec_completed *ec_node_complete(const struct ec_node *node, - const char *str); -struct ec_completed *ec_node_complete_strvec(const struct ec_node *node, - const struct ec_strvec *strvec); - -/* internal: used by nodes */ -int ec_node_complete_child(const struct ec_node *node, - struct ec_completed *completed, - const struct ec_strvec *strvec); - -/** - * Create a completion object (list of completion items). - * - * - */ -struct ec_completed *ec_completed(struct ec_parsed *state); - -/** - * Free a completion object and all its items. - * - * - */ -void ec_completed_free(struct ec_completed *completed); - -/** - * - * - * - */ -void ec_completed_dump(FILE *out, - const struct ec_completed *completed); - -/** - * Merge items contained in 'from' into 'to' - * - * The 'from' completed struct is freed. - */ -int ec_completed_merge(struct ec_completed *to, - struct ec_completed *from); - -struct ec_parsed *ec_completed_get_state(struct ec_completed *completed); - -/* shortcut for ec_completed_item() + ec_completed_item_add() */ -int ec_completed_add_item(struct ec_completed *completed, - const struct ec_node *node, - struct ec_completed_item **p_item, - enum ec_completed_type type, - const char *start, const char *full); - -/** - * - */ -int ec_completed_item_set_str(struct ec_completed_item *item, - const char *str); - -/** - * Get the string value of a completion item. - * - * - */ -const char * -ec_completed_item_get_str(const struct ec_completed_item *item); - -/** - * Get the display string value of a completion item. - * - * - */ -const char * -ec_completed_item_get_display(const struct ec_completed_item *item); - -/** - * Get the completion string value of a completion item. - * - * - */ -const char * -ec_completed_item_get_completion(const struct ec_completed_item *item); - -/** - * Get the group of a completion item. - * - * - */ -const struct ec_completed_group * -ec_completed_item_get_grp(const struct ec_completed_item *item); - -/** - * Get the type of a completion item. - * - * - */ -enum ec_completed_type -ec_completed_item_get_type(const struct ec_completed_item *item); - -/** - * Get the node associated to a completion item. - * - * - */ -const struct ec_node * -ec_completed_item_get_node(const struct ec_completed_item *item); - -/** - * Set the display value of an item. - * - * - */ -int ec_completed_item_set_display(struct ec_completed_item *item, - const char *display); - -/** - * Set the completion value of an item. - * - * - */ -int ec_completed_item_set_completion(struct ec_completed_item *item, - const char *completion); - -/** - * - * - * - */ -int -ec_node_complete_unknown(const struct ec_node *gen_node, - struct ec_completed *completed, - const struct ec_strvec *strvec); - -/** - * - * - * - */ -unsigned int ec_completed_count( - const struct ec_completed *completed, - enum ec_completed_type flags); - -/** - * - * - * - */ -struct ec_completed_iter { - enum ec_completed_type type; - struct ec_completed *completed; - struct ec_completed_group *cur_node; - struct ec_completed_item *cur_match; -}; - -/** - * - * - * - */ -struct ec_completed_iter * -ec_completed_iter(struct ec_completed *completed, - enum ec_completed_type type); - -/** - * - * - * - */ -struct ec_completed_item *ec_completed_iter_next( - struct ec_completed_iter *iter); - -/** - * - * - * - */ -void ec_completed_iter_free(struct ec_completed_iter *iter); - - -#endif diff --git a/lib/ecoli_node.h b/lib/ecoli_node.h index c880324..34b1139 100644 --- a/lib/ecoli_node.h +++ b/lib/ecoli_node.h @@ -52,7 +52,7 @@ struct ec_node; struct ec_parsed; -struct ec_completed; +struct ec_comp; struct ec_strvec; struct ec_keyval; @@ -76,7 +76,7 @@ typedef int (*ec_node_parse_t)(const struct ec_node *node, struct ec_parsed *state, const struct ec_strvec *strvec); typedef int (*ec_node_complete_t)(const struct ec_node *node, - struct ec_completed *completed_state, + struct ec_comp *comp_state, const struct ec_strvec *strvec); typedef const char * (*ec_node_desc_t)(const struct ec_node *); typedef int (*ec_node_init_priv_t)(struct ec_node *); diff --git a/lib/ecoli_node_any.c b/lib/ecoli_node_any.c index 9ee1d39..e9452b2 100644 --- a/lib/ecoli_node_any.c +++ b/lib/ecoli_node_any.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include EC_LOG_TYPE_REGISTER(node_any); diff --git a/lib/ecoli_node_cmd.c b/lib/ecoli_node_cmd.c index 01d96ac..180be43 100644 --- a/lib/ecoli_node_cmd.c +++ b/lib/ecoli_node_cmd.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include #include @@ -370,14 +370,14 @@ ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state, static int ec_node_cmd_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; if (node->cmd == NULL) return -ENOENT; - return ec_node_complete_child(node->cmd, completed, strvec); + return ec_node_complete_child(node->cmd, comp, strvec); } static void ec_node_cmd_free_priv(struct ec_node *gen_node) diff --git a/lib/ecoli_node_dynamic.c b/lib/ecoli_node_dynamic.c index 1d11f1c..84b603e 100644 --- a/lib/ecoli_node_dynamic.c +++ b/lib/ecoli_node_dynamic.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include @@ -63,7 +63,7 @@ fail: static int ec_node_dynamic_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node; @@ -73,7 +73,7 @@ ec_node_dynamic_complete(const struct ec_node *gen_node, char key[64]; int ret = -1; - parsed = ec_completed_get_state(completed); + parsed = ec_comp_get_state(comp); child = node->build(parsed, node->opaque); if (child == NULL) goto fail; @@ -81,14 +81,14 @@ ec_node_dynamic_complete(const struct ec_node *gen_node, /* add the node pointer in the attributes, so it will be freed * when parsed is freed */ snprintf(key, sizeof(key), "_dyn_%p", child); - ret = ec_keyval_set(completed->attrs, key, child, + ret = ec_keyval_set(comp->attrs, key, child, (void *)node_free); if (ret < 0) { child = NULL; /* already freed */ goto fail; } - return ec_node_complete_child(child, completed, strvec); + return ec_node_complete_child(child, comp, strvec); fail: ec_node_free(child); diff --git a/lib/ecoli_node_empty.c b/lib/ecoli_node_empty.c index 98a7614..7d40c6c 100644 --- a/lib/ecoli_node_empty.c +++ b/lib/ecoli_node_empty.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include EC_LOG_TYPE_REGISTER(node_empty); diff --git a/lib/ecoli_node_expr.c b/lib/ecoli_node_expr.c index b4cc72d..b722220 100644 --- a/lib/ecoli_node_expr.c +++ b/lib/ecoli_node_expr.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include #include @@ -58,14 +58,14 @@ static int ec_node_expr_parse(const struct ec_node *gen_node, static int ec_node_expr_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_expr *node = (struct ec_node_expr *)gen_node; if (node->child == NULL) return -ENOENT; - return ec_node_complete_child(node->child, completed, strvec); + return ec_node_complete_child(node->child, comp, strvec); } static void ec_node_expr_free_priv(struct ec_node *gen_node) diff --git a/lib/ecoli_node_file.c b/lib/ecoli_node_file.c index fd8bef4..ab772dc 100644 --- a/lib/ecoli_node_file.c +++ b/lib/ecoli_node_file.c @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include EC_LOG_TYPE_REGISTER(node_file); @@ -100,13 +100,13 @@ static int split_path(const char *path, char **dname_p, char **bname_p) static int ec_node_file_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_file *node = (struct ec_node_file *)gen_node; char *dname = NULL, *bname = NULL, *effective_dir; - struct ec_completed_item *item = NULL; - enum ec_completed_type type; + struct ec_comp_item *item = NULL; + enum ec_comp_type type; struct stat st, st2; const char *input; size_t bname_len; @@ -208,13 +208,13 @@ ec_node_file_complete(const struct ec_node *gen_node, if (ec_asprintf(&disp_str, "%s", de->d_name) < 0) goto fail; } - if (ec_completed_add_item(completed, gen_node, &item, + if (ec_comp_add_item(comp, gen_node, &item, type, input, comp_str) < 0) goto out; /* fix the display string: we don't want to display the full * path. */ - if (ec_completed_item_set_display(item, disp_str) < 0) + if (ec_comp_item_set_display(item, disp_str) < 0) goto out; item = NULL; diff --git a/lib/ecoli_node_int.c b/lib/ecoli_node_int.c index 8e92c8a..48577cf 100644 --- a/lib/ecoli_node_int.c +++ b/lib/ecoli_node_int.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include diff --git a/lib/ecoli_node_many.c b/lib/ecoli_node_many.c index 7d38a89..7c9a50a 100644 --- a/lib/ecoli_node_many.c +++ b/lib/ecoli_node_many.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -82,16 +82,16 @@ fail: static int __ec_node_many_complete(struct ec_node_many *node, unsigned int max, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_parsed *parsed = ec_completed_get_state(completed); + struct ec_parsed *parsed = ec_comp_get_state(comp); struct ec_strvec *childvec = NULL; unsigned int i; int ret; /* first, try to complete with the child node */ - ret = ec_node_complete_child(node->child, completed, strvec); + ret = ec_node_complete_child(node->child, comp, strvec); if (ret < 0) goto fail; @@ -129,7 +129,7 @@ __ec_node_many_complete(struct ec_node_many *node, unsigned int max, goto fail; } - ret = __ec_node_many_complete(node, max, completed, childvec); + ret = __ec_node_many_complete(node, max, comp, childvec); ec_parsed_del_last_child(parsed); ec_strvec_free(childvec); childvec = NULL; @@ -147,12 +147,12 @@ fail: static int ec_node_many_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_many *node = (struct ec_node_many *)gen_node; - return __ec_node_many_complete(node, node->max, completed, + return __ec_node_many_complete(node, node->max, comp, strvec); } diff --git a/lib/ecoli_node_none.c b/lib/ecoli_node_none.c index 8c5b3a9..be1558a 100644 --- a/lib/ecoli_node_none.c +++ b/lib/ecoli_node_none.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include EC_LOG_TYPE_REGISTER(node_none); @@ -35,11 +35,11 @@ static int ec_node_none_parse(const struct ec_node *gen_node, static int ec_node_none_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { (void)gen_node; - (void)completed; + (void)comp; (void)strvec; return 0; diff --git a/lib/ecoli_node_once.c b/lib/ecoli_node_once.c index 51538f1..6052f6a 100644 --- a/lib/ecoli_node_once.c +++ b/lib/ecoli_node_once.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -66,11 +66,11 @@ 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_comp *comp, 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); + struct ec_parsed *parsed = ec_comp_get_state(comp); unsigned int count; int ret; @@ -81,7 +81,7 @@ ec_node_once_complete(const struct ec_node *gen_node, if (count > 0) return 0; - ret = ec_node_complete_child(node->child, completed, strvec); + ret = ec_node_complete_child(node->child, comp, strvec); if (ret < 0) return ret; diff --git a/lib/ecoli_node_option.c b/lib/ecoli_node_option.c index 2d9db77..b454de2 100644 --- a/lib/ecoli_node_option.c +++ b/lib/ecoli_node_option.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include @@ -46,12 +46,12 @@ ec_node_option_parse(const struct ec_node *gen_node, static int ec_node_option_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_option *node = (struct ec_node_option *)gen_node; - return ec_node_complete_child(node->child, completed, strvec); + return ec_node_complete_child(node->child, comp, strvec); } static void ec_node_option_free_priv(struct ec_node *gen_node) diff --git a/lib/ecoli_node_or.c b/lib/ecoli_node_or.c index 38f8a3d..b1af790 100644 --- a/lib/ecoli_node_or.c +++ b/lib/ecoli_node_or.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include @@ -48,7 +48,7 @@ ec_node_or_parse(const struct ec_node *gen_node, static int ec_node_or_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_or *node = (struct ec_node_or *)gen_node; @@ -57,7 +57,7 @@ ec_node_or_complete(const struct ec_node *gen_node, for (n = 0; n < node->len; n++) { ret = ec_node_complete_child(node->table[n], - completed, strvec); + comp, strvec); if (ret < 0) return ret; } diff --git a/lib/ecoli_node_re.c b/lib/ecoli_node_re.c index 8563d6d..2a42fa2 100644 --- a/lib/ecoli_node_re.c +++ b/lib/ecoli_node_re.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include EC_LOG_TYPE_REGISTER(node_re); diff --git a/lib/ecoli_node_re_lex.c b/lib/ecoli_node_re_lex.c index 91e4f82..91ddeed 100644 --- a/lib/ecoli_node_re_lex.c +++ b/lib/ecoli_node_re_lex.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/lib/ecoli_node_seq.c b/lib/ecoli_node_seq.c index 21221f6..ff370aa 100644 --- a/lib/ecoli_node_seq.c +++ b/lib/ecoli_node_seq.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -74,10 +74,10 @@ fail: static int __ec_node_seq_complete(struct ec_node **table, size_t table_len, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_parsed *parsed = ec_completed_get_state(completed); + struct ec_parsed *parsed = ec_comp_get_state(comp); struct ec_strvec *childvec = NULL; unsigned int i; int ret; @@ -97,7 +97,7 @@ __ec_node_seq_complete(struct ec_node **table, size_t table_len, */ /* first, try to complete with the first node of the table */ - ret = ec_node_complete_child(table[0], completed, strvec); + ret = ec_node_complete_child(table[0], comp, strvec); if (ret < 0) goto fail; @@ -129,7 +129,7 @@ __ec_node_seq_complete(struct ec_node **table, size_t table_len, ret = __ec_node_seq_complete(&table[1], table_len - 1, - completed, childvec); + comp, childvec); ec_parsed_del_last_child(parsed); ec_strvec_free(childvec); childvec = NULL; @@ -147,12 +147,12 @@ fail: static int ec_node_seq_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_seq *node = (struct ec_node_seq *)gen_node; - return __ec_node_seq_complete(node->table, node->len, completed, + return __ec_node_seq_complete(node->table, node->len, comp, strvec); } diff --git a/lib/ecoli_node_sh_lex.c b/lib/ecoli_node_sh_lex.c index d5c86eb..82da5d7 100644 --- a/lib/ecoli_node_sh_lex.c +++ b/lib/ecoli_node_sh_lex.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include #include @@ -262,14 +262,14 @@ ec_node_sh_lex_parse(const struct ec_node *gen_node, static int ec_node_sh_lex_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node; - struct ec_completed *tmp_completed = NULL; + struct ec_comp *tmp_comp = NULL; struct ec_strvec *new_vec = NULL; - struct ec_completed_iter *iter = NULL; - struct ec_completed_item *item = NULL; + struct ec_comp_iter *iter = NULL; + struct ec_comp_item *item = NULL; char *new_str = NULL; const char *str; char missing_quote = '\0'; @@ -285,54 +285,54 @@ ec_node_sh_lex_complete(const struct ec_node *gen_node, /* we will store the completions in a temporary struct, because * we want to update them (ex: add missing quotes) */ - tmp_completed = ec_completed(ec_completed_get_state(completed)); - if (tmp_completed == NULL) + tmp_comp = ec_comp(ec_comp_get_state(comp)); + if (tmp_comp == NULL) goto fail; - ret = ec_node_complete_child(node->child, tmp_completed, new_vec); + ret = ec_node_complete_child(node->child, tmp_comp, new_vec); if (ret < 0) goto fail; /* add missing quote for full completions */ if (missing_quote != '\0') { - iter = ec_completed_iter(tmp_completed, EC_COMP_FULL); + iter = ec_comp_iter(tmp_comp, EC_COMP_FULL); if (iter == NULL) goto fail; - while ((item = ec_completed_iter_next(iter)) != NULL) { - str = ec_completed_item_get_str(item); + while ((item = ec_comp_iter_next(iter)) != NULL) { + str = ec_comp_item_get_str(item); if (ec_asprintf(&new_str, "%c%s%c", missing_quote, str, missing_quote) < 0) { new_str = NULL; goto fail; } - if (ec_completed_item_set_str(item, new_str) < 0) + if (ec_comp_item_set_str(item, new_str) < 0) goto fail; ec_free(new_str); new_str = NULL; - str = ec_completed_item_get_completion(item); + str = ec_comp_item_get_completion(item); if (ec_asprintf(&new_str, "%s%c", str, missing_quote) < 0) { new_str = NULL; goto fail; } - if (ec_completed_item_set_completion(item, new_str) < 0) + if (ec_comp_item_set_completion(item, new_str) < 0) goto fail; ec_free(new_str); new_str = NULL; } } - ec_completed_iter_free(iter); + ec_comp_iter_free(iter); ec_strvec_free(new_vec); - ec_completed_merge(completed, tmp_completed); + ec_comp_merge(comp, tmp_comp); return 0; fail: - ec_completed_free(tmp_completed); - ec_completed_iter_free(iter); + ec_comp_free(tmp_comp); + ec_comp_iter_free(iter); ec_strvec_free(new_vec); ec_free(new_str); diff --git a/lib/ecoli_node_space.c b/lib/ecoli_node_space.c index c905882..3f34629 100644 --- a/lib/ecoli_node_space.c +++ b/lib/ecoli_node_space.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include EC_LOG_TYPE_REGISTER(node_space); diff --git a/lib/ecoli_node_str.c b/lib/ecoli_node_str.c index 1efe224..fdb277e 100644 --- a/lib/ecoli_node_str.c +++ b/lib/ecoli_node_str.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include EC_LOG_TYPE_REGISTER(node_str); @@ -46,7 +46,7 @@ ec_node_str_parse(const struct ec_node *gen_node, static int ec_node_str_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_str *node = (struct ec_node_str *)gen_node; @@ -66,7 +66,7 @@ ec_node_str_complete(const struct ec_node *gen_node, if (str[n] != '\0') return EC_PARSED_NOMATCH; - if (ec_completed_add_item(completed, gen_node, NULL, EC_COMP_FULL, + if (ec_comp_add_item(comp, gen_node, NULL, EC_COMP_FULL, str, node->string) < 0) return -1; diff --git a/lib/ecoli_node_subset.c b/lib/ecoli_node_subset.c index 3ec44dc..756def1 100644 --- a/lib/ecoli_node_subset.c +++ b/lib/ecoli_node_subset.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -155,10 +155,10 @@ ec_node_subset_parse(const struct ec_node *gen_node, static int __ec_node_subset_complete(struct ec_node **table, size_t table_len, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_parsed *parsed = ec_completed_get_state(completed); + struct ec_parsed *parsed = ec_comp_get_state(comp); struct ec_strvec *childvec = NULL; struct ec_node *save; size_t i, len; @@ -179,7 +179,7 @@ __ec_node_subset_complete(struct ec_node **table, size_t table_len, continue; ret = ec_node_complete_child(table[i], - completed, strvec); + comp, strvec); if (ret < 0) goto fail; } @@ -208,7 +208,7 @@ __ec_node_subset_complete(struct ec_node **table, size_t table_len, save = table[i]; table[i] = NULL; ret = __ec_node_subset_complete(table, table_len, - completed, childvec); + comp, childvec); table[i] = save; ec_strvec_free(childvec); childvec = NULL; @@ -226,12 +226,12 @@ fail: static int ec_node_subset_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_subset *node = (struct ec_node_subset *)gen_node; - return __ec_node_subset_complete(node->table, node->len, completed, + return __ec_node_subset_complete(node->table, node->len, comp, strvec); } diff --git a/lib/ecoli_node_weakref.c b/lib/ecoli_node_weakref.c index 4d75cfb..fad8e82 100644 --- a/lib/ecoli_node_weakref.c +++ b/lib/ecoli_node_weakref.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -42,12 +42,12 @@ ec_node_weakref_parse(const struct ec_node *gen_node, static int ec_node_weakref_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_weakref *node = (struct ec_node_weakref *)gen_node; - return ec_node_complete_child(node->child, completed, strvec); + return ec_node_complete_child(node->child, comp, strvec); } static struct ec_node_type ec_node_weakref_type = { diff --git a/lib/ecoli_test.c b/lib/ecoli_test.c index b62e169..319e7e8 100644 --- a/lib/ecoli_test.c +++ b/lib/ecoli_test.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list); @@ -93,9 +93,9 @@ out: return ret; } -int ec_test_check_complete(struct ec_node *tk, enum ec_completed_type type, ...) +int ec_test_check_complete(struct ec_node *tk, enum ec_comp_type type, ...) { - struct ec_completed *c = NULL; + struct ec_comp *c = NULL; struct ec_strvec *vec = NULL; const char *s; int ret = 0; @@ -129,8 +129,8 @@ int ec_test_check_complete(struct ec_node *tk, enum ec_completed_type type, ...) for (s = va_arg(ap, const char *); s != EC_NODE_ENDLIST; s = va_arg(ap, const char *)) { - struct ec_completed_iter *iter; - const struct ec_completed_item *item; + struct ec_comp_iter *iter; + const struct ec_comp_item *item; if (s == NULL) { ret = -1; @@ -140,9 +140,9 @@ int ec_test_check_complete(struct ec_node *tk, enum ec_completed_type type, ...) count++; /* only check matching completions */ - iter = ec_completed_iter(c, type); - while ((item = ec_completed_iter_next(iter)) != NULL) { - const char *str = ec_completed_item_get_str(item); + iter = ec_comp_iter(c, type); + while ((item = ec_comp_iter_next(iter)) != NULL) { + const char *str = ec_comp_item_get_str(item); if (str != NULL && strcmp(str, s) == 0) break; } @@ -152,21 +152,21 @@ int ec_test_check_complete(struct ec_node *tk, enum ec_completed_type type, ...) "completion <%s> not in list\n", s); ret = -1; } - ec_completed_iter_free(iter); + ec_comp_iter_free(iter); } /* check if we have more completions (or less) than expected */ - if (count != ec_completed_count(c, type)) { + if (count != ec_comp_count(c, type)) { EC_LOG(EC_LOG_ERR, "nb_completion (%d) does not match (%d)\n", - count, ec_completed_count(c, type)); - ec_completed_dump(stdout, c); + count, ec_comp_count(c, type)); + ec_comp_dump(stdout, c); ret = -1; } out: ec_strvec_free(vec); - ec_completed_free(c); + ec_comp_free(c); va_end(ap); return ret; } diff --git a/lib/ecoli_test.h b/lib/ecoli_test.h index 56102cf..82094ce 100644 --- a/lib/ecoli_test.h +++ b/lib/ecoli_test.h @@ -10,7 +10,7 @@ #include struct ec_node; -enum ec_completed_type; +enum ec_comp_type; #define EC_TEST_REGISTER(t) \ static void ec_test_init_##t(void); \ @@ -78,7 +78,7 @@ int ec_test_check_parse(struct ec_node *node, int expected, ...); }) int ec_test_check_complete(struct ec_node *node, - enum ec_completed_type type, ...); + enum ec_comp_type type, ...); #define EC_TEST_CHECK_COMPLETE(node, args...) ({ \ int ret_ = ec_test_check_complete(node, EC_COMP_FULL, args); \ diff --git a/lib/main-readline.c b/lib/main-readline.c index 0f6abb2..0fb57ab 100644 --- a/lib/main-readline.c +++ b/lib/main-readline.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include @@ -32,10 +32,10 @@ static struct ec_node *commands; static char *my_completion_entry(const char *s, int state) { - static struct ec_completed *c; - static struct ec_completed_iter *iter; - const struct ec_completed_item *item; - enum ec_completed_type item_type; + static struct ec_comp *c; + static struct ec_comp_iter *iter; + const struct ec_comp_item *item; + enum ec_comp_type item_type; const char *item_str, *item_display; (void)s; @@ -48,7 +48,7 @@ static char *my_completion_entry(const char *s, int state) if (state == 0) { char *line; - ec_completed_free(c); + ec_comp_free(c); line = strdup(rl_line_buffer); if (line == NULL) return NULL; @@ -59,22 +59,22 @@ static char *my_completion_entry(const char *s, int state) if (c == NULL) return NULL; - ec_completed_iter_free(iter); - iter = ec_completed_iter(c, EC_COMP_FULL | EC_COMP_PARTIAL); + ec_comp_iter_free(iter); + iter = ec_comp_iter(c, EC_COMP_FULL | EC_COMP_PARTIAL); if (iter == NULL) return NULL; } - item = ec_completed_iter_next(iter); + item = ec_comp_iter_next(iter); if (item == NULL) return NULL; - item_str = ec_completed_item_get_str(item); + item_str = ec_comp_item_get_str(item); if (c->count_full == 1) { /* don't add the trailing space for partial completions */ if (state == 0) { - item_type = ec_completed_item_get_type(item); + item_type = ec_comp_item_get_type(item); if (item_type == EC_COMP_FULL) rl_completion_suppress_append = 0; else @@ -84,7 +84,7 @@ static char *my_completion_entry(const char *s, int state) return strdup(item_str); } else if (rl_completion_type == '?') { /* on second try only show the display part */ - item_display = ec_completed_item_get_display(item); + item_display = ec_comp_item_get_display(item); return strdup(item_display); } @@ -103,16 +103,16 @@ static char **my_attempted_completion(const char *text, int start, int end) } /* this function builds the help string */ -static char *get_node_help(const struct ec_completed_item *item) +static char *get_node_help(const struct ec_comp_item *item) { - const struct ec_completed_group *grp; + const struct ec_comp_group *grp; const struct ec_parsed *state; const struct ec_node *node; char *help = NULL; const char *node_help = NULL; const char *node_desc = NULL; - grp = ec_completed_item_get_grp(item); + grp = ec_comp_item_get_grp(item); state = grp->state; for (state = grp->state; state != NULL; state = ec_parsed_get_parent(state)) { @@ -136,10 +136,10 @@ static char *get_node_help(const struct ec_completed_item *item) static int show_help(int ignore, int invoking_key) { - struct ec_completed_iter *iter; - const struct ec_completed_group *grp, *prev_grp = NULL; - const struct ec_completed_item *item; - struct ec_completed *c; + struct ec_comp_iter *iter; + const struct ec_comp_group *grp, *prev_grp = NULL; + const struct ec_comp_item *item; + struct ec_comp *c; struct ec_parsed *p; char *line = NULL; unsigned int count; @@ -171,7 +171,7 @@ static int show_help(int ignore, int invoking_key) /* let's display one contextual help per node */ count = 0; - iter = ec_completed_iter(c, + iter = ec_comp_iter(c, EC_COMP_UNKNOWN | EC_COMP_FULL | EC_COMP_PARTIAL); if (iter == NULL) goto fail; @@ -183,11 +183,11 @@ static int show_help(int ignore, int invoking_key) if (match) helps[1] = ""; - while ((item = ec_completed_iter_next(iter)) != NULL) { + while ((item = ec_comp_iter_next(iter)) != NULL) { char **tmp; /* keep one help per group, skip other items */ - grp = ec_completed_item_get_grp(item); + grp = ec_comp_item_get_grp(item); if (grp == prev_grp) continue; @@ -201,8 +201,8 @@ static int show_help(int ignore, int invoking_key) count++; } - ec_completed_iter_free(iter); - ec_completed_free(c); + ec_comp_iter_free(iter); + ec_comp_free(c); /* ensure not more than 1 entry per line */ rl_get_screen_size(NULL, &cols); rl_display_match_list(helps, count + match, cols); @@ -211,10 +211,10 @@ static int show_help(int ignore, int invoking_key) return 0; fail: - ec_completed_iter_free(iter); + ec_comp_iter_free(iter); ec_parsed_free(p); free(line); - ec_completed_free(c); + ec_comp_free(c); if (helps != NULL) { while (count--) free(helps[count + match + 1]);