completed = ec_calloc(1, sizeof(*completed));
if (completed == NULL)
- return NULL;
+ goto fail;
TAILQ_INIT(&completed->nodes);
- TAILQ_INIT(&completed->matches);
+
+ completed->attrs = ec_keyval();
+ if (completed->attrs == NULL)
+ goto fail;
return completed;
+
+ fail:
+ if (completed != NULL)
+ ec_keyval_free(completed->attrs);
+ ec_free(completed);
+
+ return NULL;
}
/* XXX on error, states are not freed ?
child_state->node = node;
ec_parsed_add_child(parsed_state, child_state);
- ret = ec_completed_add_node(completed, child_state, node);
- if (ret < 0)
- return ret;
-
- ret = node->type->complete(node, completed,
- child_state, strvec);
+ ret = node->type->complete(node, completed, child_state, strvec);
if (ret < 0)
return ret;
return NULL;
}
-/* count the number of identical chars at the beginning of 2 strings */
-static size_t strcmp_count(const char *s1, const char *s2)
+static struct ec_completed_node *
+ec_completed_node(const struct ec_node *node)
{
- size_t i = 0;
+ struct ec_completed_node *compnode = NULL;
+
+ compnode = ec_calloc(1, sizeof(*compnode));
+ if (compnode == NULL)
+ return NULL;
- while (s1[i] && s2[i] && s1[i] == s2[i])
- i++;
+ compnode->node = node;
+ TAILQ_INIT(&compnode->items);
- return i;
+ return compnode;
}
-static struct ec_completed_item *
-ec_completed_item(enum ec_completed_type type, struct ec_parsed *state,
- const struct ec_node *node, const char *add)
+struct ec_completed_item *
+ec_completed_item(struct ec_parsed *state, const struct ec_node *node)
{
struct ec_completed_item *item = NULL;
+ struct ec_parsed *p;
+ size_t len;
item = ec_calloc(1, sizeof(*item));
if (item == NULL)
- return NULL;
-
- /* XXX can state be NULL? */
- if (state != NULL) {
- struct ec_parsed *p;
- size_t len;
-
- /* get path len */
- for (p = state, len = 0; p != NULL;
- p = ec_parsed_get_parent(p), len++)
- ;
-
- item->path = ec_calloc(len, sizeof(*item->path));
- if (item->path == NULL)
- goto fail;
+ goto fail;
- item->pathlen = len;
+ item->attrs = ec_keyval();
+ if (item->attrs == NULL)
+ goto fail;
- /* write path in array */
- for (p = state, len = 0; p != NULL;
- p = ec_parsed_get_parent(p), len++)
- item->path[len] = p->node;
- }
+ /* get path len */
+ for (p = state, len = 0; p != NULL;
+ p = ec_parsed_get_parent(p), len++)
+ ;
+ /* allocate room for path */
+ item->path = ec_calloc(len, sizeof(*item->path));
+ if (item->path == NULL)
+ goto fail;
+ item->pathlen = len;
+ /* write path in array */
+ for (p = state, len = 0; p != NULL;
+ p = ec_parsed_get_parent(p), len++)
+ item->path[len] = p->node;
- item->type = type;
+ item->type = EC_NO_MATCH;
item->node = node;
- if (add != NULL) {
- item->add = ec_strdup(add);
- if (item->add == NULL)
- goto fail;
- }
return item;
fail:
if (item != NULL) {
ec_free(item->path);
- ec_free(item->add);
+ ec_free(item->str);
+ ec_free(item->display);
+ ec_keyval_free(item->attrs);
}
- ec_completed_item_free(item);
+ ec_free(item);
return NULL;
}
int
-ec_completed_add_match(struct ec_completed *completed,
- struct ec_parsed *parsed_state,
- const struct ec_node *node, const char *add)
+ec_completed_item_set(struct ec_completed_item *item,
+ enum ec_completed_type type, const char *str)
{
- struct ec_completed_item *item = NULL;
- int ret = -ENOMEM;
- size_t n;
+ char *str_copy = NULL;
+ char *display_copy = NULL;
+ int ret = 0;
- item = ec_completed_item(EC_MATCH, parsed_state, node, add);
if (item == NULL)
- goto fail;
+ return -EINVAL;
+ if (item->str != NULL)
+ return -EEXIST;
+
+ switch (type) {
+ case EC_NO_MATCH:
+ if (str != NULL)
+ return -EINVAL;
+ break;
+ case EC_MATCH:
+ case EC_PARTIAL_MATCH:
+ if (str == NULL)
+ return -EINVAL;
+ break;
+ default:
+ return -EINVAL;
+ }
- if (item->add != NULL) {
- if (completed->smallest_start == NULL) {
- completed->smallest_start = ec_strdup(item->add);
- if (completed->smallest_start == NULL)
- goto fail;
- } else {
- n = strcmp_count(item->add,
- completed->smallest_start);
- completed->smallest_start[n] = '\0';
- }
- completed->count_match++;
+ if (str != NULL) {
+ ret = -ENOMEM;
+ str_copy = ec_strdup(str);
+ if (str_copy == NULL)
+ goto fail;
+ display_copy = ec_strdup(str);
+ if (display_copy == NULL)
+ goto fail;
}
- TAILQ_INSERT_TAIL(&completed->matches, item, next);
- completed->count++;
+ item->type = type;
+ item->str = str_copy;
+ item->display = display_copy;
+ return 0;
+
+fail:
+ ec_free(str_copy);
+ ec_free(display_copy);
+ return ret;
+}
+
+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_NO_MATCH || item->str == NULL)
+ return -EINVAL;
+
+ ret = -ENOMEM;
+ display_copy = ec_strdup(display);
+ if (display_copy == NULL)
+ goto fail;
+
+ ec_free(item->display);
+ item->display = display_copy;
return 0;
fail:
- ec_completed_item_free(item);
+ ec_free(display_copy);
return ret;
}
int
-ec_completed_add_node(struct ec_completed *completed,
- struct ec_parsed *parsed_state,
- const struct ec_node *node)
+ec_completed_item_add(struct ec_completed *completed,
+ struct ec_completed_item *item)
{
-#if 0
- struct ec_completed_item *item = NULL;
- int ret;
-
- item = ec_completed_item(EC_NO_MATCH, parsed_state, node, NULL);
- if (item == NULL)
- return -ENOMEM;
+ struct ec_completed_node *compnode = NULL;
+
+ if (completed == NULL || item == NULL || item->node == NULL)
+ return -EINVAL;
+
+ switch (item->type) {
+ case EC_NO_MATCH:
+ break;
+ case EC_MATCH:
+ case EC_PARTIAL_MATCH:
+ completed->count_match++; //XXX
+ break;
+ default:
+ return -EINVAL;
+ }
- ret = ec_completed_add_item(completed, item);
- if (ret < 0) {
- ec_completed_item_free(item);
- return ret;
+ /* find the compnode entry corresponding to this node */
+ TAILQ_FOREACH(compnode, &completed->nodes, next) {
+ if (compnode->node == item->node)
+ break;
}
-#endif
- (void)completed;
- (void)parsed_state;
- (void)node;
+ if (compnode == NULL) {
+ compnode = ec_completed_node(item->node);
+ if (compnode == NULL)
+ return -ENOMEM;
+ TAILQ_INSERT_TAIL(&completed->nodes, compnode, next);
+ }
+
+ completed->count++;
+ TAILQ_INSERT_TAIL(&compnode->items, item, next);
+
return 0;
}
void ec_completed_item_free(struct ec_completed_item *item)
{
- ec_free(item->add);
+ if (item == NULL)
+ return;
+
+ ec_free(item->str);
+ ec_free(item->display);
ec_free(item->path);
+ ec_keyval_free(item->attrs);
ec_free(item);
}
-/* default completion function: return a no-item element */
+/* default completion function: return a no-match element */
int
-ec_node_default_complete(const struct ec_node *gen_node,
+ec_node_default_complete(const struct ec_node *gen_node, // XXX rename in nomatch
struct ec_completed *completed,
- struct ec_parsed *parsed,
+ struct ec_parsed *parsed_state,
const struct ec_strvec *strvec)
{
- (void)strvec;
+ struct ec_completed_item *item = NULL;
+ int ret;
- if (ec_strvec_len(strvec) != 1) //XXX needed?
+ if (ec_strvec_len(strvec) != 1)
return 0;
- if (ec_completed_add_node(completed, parsed, gen_node) < 0)
- return -1;
-
- return 0;
-}
-
-void ec_completed_merge(struct ec_completed *completed1,
- struct ec_completed *completed2)
-{
- struct ec_completed_item *item;
-
- assert(completed1 != NULL);
- assert(completed2 != NULL);
-
- while (!TAILQ_EMPTY(&completed2->matches)) {
- item = TAILQ_FIRST(&completed2->matches);
- TAILQ_REMOVE(&completed2->matches, item, next);
- //ec_completed_add_item(completed1, item);
+ item = ec_completed_item(parsed_state, gen_node);
+ if (item == NULL)
+ return -ENOMEM;
+ ret = ec_completed_item_set(item, EC_NO_MATCH, NULL);
+ if (ret < 0) {
+ ec_completed_item_free(item);
+ return ret;
+ }
+ ret = ec_completed_item_add(completed, item);
+ if (ret < 0) {
+ ec_completed_item_free(item);
+ return ret;
}
- ec_completed_free(completed2);
+ return 0;
}
void ec_completed_free(struct ec_completed *completed)
{
+ struct ec_completed_node *compnode;
struct ec_completed_item *item;
if (completed == NULL)
return;
- while (!TAILQ_EMPTY(&completed->matches)) {
- item = TAILQ_FIRST(&completed->matches);
- TAILQ_REMOVE(&completed->matches, item, next);
- ec_completed_item_free(item);
+ while (!TAILQ_EMPTY(&completed->nodes)) {
+ compnode = TAILQ_FIRST(&completed->nodes);
+ TAILQ_REMOVE(&completed->nodes, compnode, next);
+
+ while (!TAILQ_EMPTY(&compnode->items)) {
+ item = TAILQ_FIRST(&compnode->items);
+ TAILQ_REMOVE(&compnode->items, item, next);
+ ec_completed_item_free(item);
+ }
+ ec_free(compnode);
}
- ec_free(completed->smallest_start);
+ ec_keyval_free(completed->attrs);
ec_free(completed);
}
void ec_completed_dump(FILE *out, const struct ec_completed *completed)
{
+ struct ec_completed_node *compnode;
struct ec_completed_item *item;
if (completed == NULL || completed->count == 0) {
return;
}
- fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n",
- completed->count, completed->count_match,
- completed->smallest_start);
+ fprintf(out, "completion: count=%u match=%u\n",
+ completed->count, completed->count_match);
- TAILQ_FOREACH(item, &completed->matches, next) {
- fprintf(out, "add=<%s>, node=%p, node_type=%s\n",
- item->add, item->node, item->node->type->name);
- }
-}
+ TAILQ_FOREACH(compnode, &completed->nodes, next) {
+ fprintf(out, "node=%p, node_type=%s\n",
+ compnode->node, compnode->node->type->name);
+ TAILQ_FOREACH(item, &compnode->items, next) {
+ const char *typestr;
-const char *ec_completed_smallest_start(
- const struct ec_completed *completed)
-{
- if (completed == NULL || completed->smallest_start == NULL)
- return "";
+ switch (item->type) {
+ case EC_NO_MATCH: typestr = "no-match"; break;
+ case EC_MATCH: typestr = "match"; break;
+ case EC_PARTIAL_MATCH: typestr = "partial-match"; break;
+ default: typestr = "unknown"; break;
+ }
- return completed->smallest_start;
+ fprintf(out, " type=%s str=<%s> disp=<%s>\n",
+ typestr, item->str, item->display);
+ }
+ }
}
unsigned int ec_completed_count(
iter->completed = completed;
iter->type = type;
- iter->cur_item = NULL;
+ iter->cur_node = NULL;
+ iter->cur_match = NULL;
return iter;
}
struct ec_completed_iter *iter)
{
const struct ec_completed *completed = iter->completed;
+ const struct ec_completed_node *cur_node;
+ const struct ec_completed_item *cur_match;
if (completed == NULL)
return NULL;
- do {
- if (iter->cur_item == NULL)
- iter->cur_item = TAILQ_FIRST(&completed->matches);
- else
- iter->cur_item = TAILQ_NEXT(iter->cur_item, next);
-
- if (iter->cur_item == NULL)
- break;
-
- if (iter->cur_item->add == NULL &&
- (iter->type & EC_NO_MATCH))
- break;
-
- if (iter->cur_item->add != NULL &&
- (iter->type & EC_MATCH))
- break;
+ cur_node = iter->cur_node;
+ cur_match = iter->cur_match;
+
+ /* first call */
+ if (cur_node == NULL) {
+ TAILQ_FOREACH(cur_node, &completed->nodes, next) {
+ TAILQ_FOREACH(cur_match, &cur_node->items, next) {
+ if (cur_match != NULL &&
+ cur_match->type & iter->type)
+ goto found;
+ }
+ }
+ return NULL;
+ } else {
+ cur_match = TAILQ_NEXT(cur_match, next);
+ if (cur_match != NULL &&
+ cur_match->type & iter->type)
+ goto found;
+ cur_node = TAILQ_NEXT(cur_node, next);
+ while (cur_node != NULL) {
+ cur_match = TAILQ_FIRST(&cur_node->items);
+ if (cur_match != NULL &&
+ cur_match->type & iter->type)
+ goto found;
+ cur_node = TAILQ_NEXT(cur_node, next);
+ }
+ return NULL;
+ }
- } while (iter->cur_item != NULL);
+found:
+ iter->cur_node = cur_node;
+ iter->cur_match = cur_match;
- return iter->cur_item;
+ return iter->cur_match;
}
void ec_completed_iter_free(struct ec_completed_iter *iter)