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
--- /dev/null
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+
+#include <ecoli_malloc.h>
+#include <ecoli_string.h>
+#include <ecoli_strvec.h>
+#include <ecoli_keyval.h>
+#include <ecoli_log.h>
+#include <ecoli_node.h>
+#include <ecoli_parsed.h>
+#include <ecoli_complete.h>
+
+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);
+}
--- /dev/null
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
+ */
+
+/**
+ * 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 <sys/queue.h>
+#include <sys/types.h>
+#include <stdio.h>
+
+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
+++ /dev/null
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include <errno.h>
-
-#include <ecoli_malloc.h>
-#include <ecoli_string.h>
-#include <ecoli_strvec.h>
-#include <ecoli_keyval.h>
-#include <ecoli_log.h>
-#include <ecoli_node.h>
-#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
-
-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);
-}
+++ /dev/null
-/* SPDX-License-Identifier: BSD-3-Clause
- * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
- */
-
-/**
- * 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 <sys/queue.h>
-#include <sys/types.h>
-#include <stdio.h>
-
-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
struct ec_node;
struct ec_parsed;
-struct ec_completed;
+struct ec_comp;
struct ec_strvec;
struct ec_keyval;
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 *);
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_any.h>
EC_LOG_TYPE_REGISTER(node_any);
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_expr.h>
#include <ecoli_node_str.h>
#include <ecoli_node_or.h>
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)
#include <ecoli_keyval.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_string.h>
#include <ecoli_node_str.h>
#include <ecoli_node_many.h>
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;
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;
/* 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);
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_empty.h>
EC_LOG_TYPE_REGISTER(node_empty);
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_seq.h>
#include <ecoli_node_many.h>
#include <ecoli_node_or.h>
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)
#include <ecoli_string.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_file.h>
EC_LOG_TYPE_REGISTER(node_file);
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;
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;
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_int.h>
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_node_option.h>
#include <ecoli_node_many.h>
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;
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;
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);
}
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_none.h>
EC_LOG_TYPE_REGISTER(node_none);
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;
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_node_or.h>
#include <ecoli_node_many.h>
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;
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;
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_test.h>
#include <ecoli_node_option.h>
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)
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_or.h>
#include <ecoli_node_str.h>
#include <ecoli_test.h>
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;
for (n = 0; n < node->len; n++) {
ret = ec_node_complete_child(node->table[n],
- completed, strvec);
+ comp, strvec);
if (ret < 0)
return ret;
}
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_re.h>
EC_LOG_TYPE_REGISTER(node_re);
#include <ecoli_test.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_parsed.h>
#include <ecoli_node_many.h>
#include <ecoli_node_or.h>
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_node_option.h>
#include <ecoli_node_or.h>
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;
*/
/* 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;
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;
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);
}
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_seq.h>
#include <ecoli_node_str.h>
#include <ecoli_node_option.h>
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';
/* 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);
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_space.h>
EC_LOG_TYPE_REGISTER(node_space);
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_str.h>
EC_LOG_TYPE_REGISTER(node_str);
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;
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;
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_subset.h>
#include <ecoli_node_str.h>
#include <ecoli_node_or.h>
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;
continue;
ret = ec_node_complete_child(table[i],
- completed, strvec);
+ comp, strvec);
if (ret < 0)
goto fail;
}
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;
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);
}
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_node_str.h>
#include <ecoli_node_option.h>
#include <ecoli_node_weakref.h>
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 = {
#include <ecoli_strvec.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_parsed.h>
static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list);
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;
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;
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;
}
"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;
}
#include <ecoli_log.h>
struct ec_node;
-enum ec_completed_type;
+enum ec_comp_type;
#define EC_TEST_REGISTER(t) \
static void ec_test_init_##t(void); \
})
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); \
#include <ecoli_init.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
-#include <ecoli_completed.h>
+#include <ecoli_complete.h>
#include <ecoli_keyval.h>
#include <ecoli_node_str.h>
#include <ecoli_node_seq.h>
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;
if (state == 0) {
char *line;
- ec_completed_free(c);
+ ec_comp_free(c);
line = strdup(rl_line_buffer);
if (line == NULL)
return NULL;
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
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);
}
}
/* 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)) {
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;
/* 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;
if (match)
helps[1] = "<return>";
- 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;
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);
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]);