srcs += ecoli_node_subset.c
srcs += ecoli_node_weakref.c
srcs += ecoli_parsed.c
+srcs += ecoli_string.c
srcs += ecoli_vec.c
shlib-y-$(O)libecoli.so := $(srcs)
child_state->node = node;
ec_parsed_add_child(parsed_state, child_state);
- ret = ec_completed_add_node(completed, 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)
-{
- size_t i = 0;
-
- while (s1[i] && s2[i] && s1[i] == s2[i])
- i++;
-
- return i;
-}
-
static struct ec_completed_node *
ec_completed_node(const struct ec_node *node)
{
return compnode;
}
-static struct ec_completed_item *
+struct ec_completed_item *
ec_completed_item(struct ec_parsed *state, const struct ec_node *node)
{
struct ec_completed_item *item = NULL;
if (item != NULL) {
ec_free(item->path);
ec_free(item->str);
+ ec_free(item->display);
}
ec_completed_item_free(item);
return NULL;
}
-static int
-__ec_completed_add_match(enum ec_completed_type type,
- struct ec_completed *completed,
- struct ec_parsed *parsed_state,
- const struct ec_node *node, const char *str)
+int
+ec_completed_item_set(struct ec_completed_item *item,
+ enum ec_completed_type type, const char *str)
{
- struct ec_completed_node *compnode = NULL;
- struct ec_completed_item *item = NULL;
- int ret = -ENOMEM;
+ char *str_copy = NULL;
+ char *display_copy = NULL;
+ int ret = 0;
- /* find the compnode entry corresponding to this node */
- TAILQ_FOREACH(compnode, &completed->nodes, next) {
- if (compnode->node == node)
- break;
+ if (item == NULL)
+ 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 (compnode == NULL)
- return -ENOENT;
- item = ec_completed_item(parsed_state, node);
- if (item == NULL)
- goto fail;
- item->type = type;
if (str != NULL) {
- item->str = ec_strdup(str);
- if (item->str == NULL)
+ ret = -ENOMEM;
+ str_copy = ec_strdup(str);
+ if (str_copy == NULL)
+ goto fail;
+ display_copy = ec_strdup(str);
+ if (display_copy == NULL)
goto fail;
}
- if (item->str != NULL)
- completed->count_match++;
-
- TAILQ_INSERT_TAIL(&compnode->items, item, next);
- completed->count++;
-
+ item->type = type;
+ item->str = str_copy;
+ item->display = display_copy;
return 0;
fail:
- ec_completed_item_free(item);
+ ec_free(str_copy);
+ ec_free(display_copy);
return ret;
}
-int
-ec_completed_add_match(struct ec_completed *completed,
- struct ec_parsed *parsed_state,
- const struct ec_node *node, const char *str)
+int ec_completed_item_set_display(struct ec_completed_item *item,
+ const char *display)
{
- struct ec_completed_node *compnode = NULL;
- struct ec_completed_item *item = NULL;
- int ret = -ENOMEM;
+ char *display_copy = NULL;
+ int ret = 0;
- /* find the compnode entry corresponding to this node */
- TAILQ_FOREACH(compnode, &completed->nodes, next) {
- if (compnode->node == node)
- break;
- }
- if (compnode == NULL)
- return -ENOENT;
+ if (item == NULL || display == NULL ||
+ item->type == EC_NO_MATCH || item->str == NULL)
+ return -EINVAL;
- item = ec_completed_item(parsed_state, node);
- if (item == NULL)
+ ret = -ENOMEM;
+ display_copy = ec_strdup(display);
+ if (display_copy == NULL)
goto fail;
- item->type = EC_MATCH;
- if (str != NULL) {
- item->str = ec_strdup(str);
- if (item->str == NULL)
- goto fail;
- }
- if (item->str != NULL)
- completed->count_match++;
-
- TAILQ_INSERT_TAIL(&compnode->items, item, next);
- completed->count++;
+ 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_no_match(struct ec_completed *completed,
- struct ec_parsed *parsed_state,
- const struct ec_node *node)
-{
- return __ec_completed_add_match(EC_NO_MATCH, completed, parsed_state,
- node, NULL);
-}
-
-int
-ec_completed_add_partial_match(struct ec_completed *completed,
- struct ec_parsed *parsed_state,
- const struct ec_node *node, const char *str)
-{
- return __ec_completed_add_match(EC_PARTIAL_MATCH, completed, parsed_state,
- node, str);
}
int
-ec_completed_add_node(struct ec_completed *completed,
- const struct ec_node *node)
+ec_completed_item_add(struct ec_completed *completed,
+ struct ec_completed_item *item)
{
struct ec_completed_node *compnode = NULL;
- compnode = ec_completed_node(node);
- if (compnode == NULL)
- return -ENOMEM;
+ 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;
+ }
+
+ /* find the compnode entry corresponding to this node */
+ TAILQ_FOREACH(compnode, &completed->nodes, next) {
+ if (compnode->node == item->node)
+ break;
+ }
+ 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);
- TAILQ_INSERT_TAIL(&completed->nodes, compnode, next);
return 0;
}
-void ec_completed_item_free(struct ec_completed_item *match)
+void ec_completed_item_free(struct ec_completed_item *item)
{
- ec_free(match->str);
- ec_free(match->path);
- ec_free(match);
+ if (item == NULL)
+ return;
+
+ ec_free(item->str);
+ ec_free(item->display);
+ ec_free(item->path);
+ ec_free(item);
}
/* 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_state,
const struct ec_strvec *strvec)
{
+ struct ec_completed_item *item = NULL;
int ret;
if (ec_strvec_len(strvec) != 1)
return 0;
- ret = ec_completed_add_no_match(completed, parsed_state, gen_node);
- if (ret < 0)
+ 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;
+ }
return 0;
}
default: typestr = "unknown"; break;
}
- fprintf(out, " type=%s str=<%s>\n", typestr, item->str);
- }
- }
-}
-
-char *ec_completed_smallest_start(const struct ec_completed *completed)
-{
- struct ec_completed_node *compnode;
- struct ec_completed_item *item;
- char *smallest_start = NULL;
- size_t n;
-
- if (completed == NULL)
- goto fail;
-
- TAILQ_FOREACH(compnode, &completed->nodes, next) {
- TAILQ_FOREACH(item, &compnode->items, next) {
- if (item->str == NULL)
- continue;
- if (smallest_start == NULL) {
- smallest_start = ec_strdup(item->str);
- if (smallest_start == NULL)
- goto fail;
- } else {
- n = strcmp_count(item->str, smallest_start);
- smallest_start[n] = '\0';
- }
+ fprintf(out, " type=%s str=<%s> disp=<%s>\n",
+ typestr, item->str, item->display);
}
}
-
- return smallest_start;
-
-fail:
- ec_free(smallest_start);
- return NULL;
}
unsigned int ec_completed_count(
enum ec_completed_type type;
const struct ec_node *node;
char *str;
+ char *display;
/* reverse order: [0] = last, [len-1] = root */
const struct ec_node **path;
struct ec_completed *ec_completed(void);
-int ec_completed_add_match(struct ec_completed *completed,
- struct ec_parsed *state,
- const struct ec_node *node, const char *add);
-int ec_completed_add_no_match(struct ec_completed *completed,
- struct ec_parsed *parsed_state,
- const struct ec_node *node);
-int ec_completed_add_partial_match(struct ec_completed *completed,
- 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);
+int ec_completed_item_set(struct ec_completed_item *item,
+ enum ec_completed_type type, const char *str);
+int ec_completed_item_add(struct ec_completed *completed,
+ struct ec_completed_item *item);
+void ec_completed_item_free(struct ec_completed_item *item);
-int ec_completed_add_node(struct ec_completed *completed,
- const struct ec_node *node);
+int ec_completed_item_set_display(struct ec_completed_item *item,
+ const char *display);
-void ec_completed_item_free(struct ec_completed_item *item);
void ec_completed_free(struct ec_completed *completed);
void ec_completed_dump(FILE *out,
const struct ec_completed *completed);
struct ec_parsed *state,
const struct ec_strvec *strvec);
-/* return the smallest string start, or NULL on error */
-char *ec_completed_smallest_start(const struct ec_completed *completed);
-
unsigned int ec_completed_count(
const struct ec_completed *completed,
enum ec_completed_type flags);
#include <ecoli_malloc.h>
#include <ecoli_test.h>
#include <ecoli_strvec.h>
+#include <ecoli_string.h>
#include <ecoli_node.h>
#include <ecoli_parsed.h>
#include <ecoli_completed.h>
struct ec_parsed *state,
const struct ec_strvec *strvec)
{
+ struct ec_completed_item *item = NULL;
struct stat st;
- const char *path;
+ const char *input;
size_t bname_len;
struct dirent *de = NULL;
DIR *dir = NULL;
char *dname = NULL, *bname = NULL, *effective_dir;
- char *add = NULL;
+ char *comp_str = NULL;
+ char *disp_str = NULL;
int ret;
int is_dir = 0;
if (ec_strvec_len(strvec) != 1)
goto out;
- path = ec_strvec_val(strvec, 0);
- ret = split_path(path, &dname, &bname);
+ input = ec_strvec_val(strvec, 0);
+ ret = split_path(input, &dname, &bname);
if (ret < 0) {
ec_completed_free(completed);
completed = NULL;
if (de == NULL)
goto out;
- if (strncmp(bname, de->d_name, bname_len))
+ if (!ec_str_startswith(de->d_name, bname))
continue;
if (bname[0] != '.' && de->d_name[0] == '.')
continue;
is_dir = 0;
}
+ item = ec_completed_item(state, gen_node);
+ if (item == NULL) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
if (is_dir) {
- if (asprintf(&add, "%s%s/", path,
+ if (asprintf(&comp_str, "%s%s/", input,
&de->d_name[bname_len]) < 0) {
ret = -errno;
goto out;
}
- if (ec_completed_add_partial_match(
- completed, state, gen_node, add) < 0) {
- ec_completed_free(completed);
- completed = NULL;
+ if (asprintf(&disp_str, "%s/", de->d_name) < 0) {
+ ret = -errno;
goto out;
}
+ ret = ec_completed_item_set(item, EC_PARTIAL_MATCH,
+ comp_str);
+ if (ret < 0)
+ goto out;
} else {
- if (asprintf(&add, "%s%s", path,
+ if (asprintf(&comp_str, "%s%s", input,
&de->d_name[bname_len]) < 0) {
ret = -errno;
goto out;
}
- if (ec_completed_add_match(completed, state, gen_node,
- add) < 0) {
- ec_completed_free(completed);
- completed = NULL;
+ if (asprintf(&disp_str, "%s", de->d_name) < 0) {
+ ret = -errno;
goto out;
}
+ ret = ec_completed_item_set(item, EC_MATCH,
+ comp_str);
+ if (ret < 0)
+ goto out;
}
+ ret = ec_completed_item_set_display(item, disp_str);
+ if (ret < 0)
+ goto out;
+ ret = ec_completed_item_add(completed, item);
+ if (ret < 0)
+ goto out;
+
+ item = NULL;
+ free(comp_str);
+ comp_str = NULL;
+ free(disp_str);
+ disp_str = NULL;
}
ret = 0;
out:
- free(add);
+ ec_completed_item_free(item);
+ free(comp_str);
+ free(disp_str);
ec_free(dname);
ec_free(bname);
if (dir != NULL)
struct ec_parsed *parsed,
const struct ec_strvec *strvec)
{
+ struct ec_completed_item *item = NULL;
struct ec_node_str *node = (struct ec_node_str *)gen_node;
const char *str;
size_t n = 0;
+ int ret;
(void)parsed;
break;
}
- if (str[n] == '\0') {
- if (ec_completed_add_match(completed, parsed, gen_node,
- node->string) < 0)
- return -1;
+ /* no completion */
+ if (str[n] != '\0')
+ return 0; // XXX add a no_match instead?
+
+ item = ec_completed_item(parsed, gen_node);
+ if (item == NULL)
+ return -ENOMEM;
+ ret = ec_completed_item_set(item, EC_MATCH, node->string);
+ 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;
}
return 0;
if (item == NULL)
return NULL;
- /* don't add the trailing space for partial completions */
- if (state == 0) {
- if (item->type == EC_MATCH)
- rl_completion_suppress_append = 0;
- else
- rl_completion_suppress_append = 1;
+ if (c->count_match == 1) {
+
+ /* don't add the trailing space for partial completions */
+ if (state == 0) {
+ if (item->type == EC_MATCH)
+ rl_completion_suppress_append = 0;
+ else
+ rl_completion_suppress_append = 1;
+ }
+
+ return strdup(item->str);
+ } else if (rl_completion_type == '?') {
+ /* on second try only show the display part */
+ return strdup(item->display);
}
- //XXX see printable_part() and rl_display_match_list()
- //XXX or: if match count > 1, strip beginning (in node_file)
-
return strdup(item->str);
}
}
/* 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_completed_node *compnode)
{
+ const struct ec_completed_item *item;
const struct ec_node *node;
char *help = NULL;
const char *node_help = NULL;
const char *node_desc = NULL;
size_t i;
+ /* Since we display only one help per node, only look at the first item
+ * to get the path. The objective is to retrieve the most precise
+ * help for this node. */
+ item = TAILQ_FIRST(&compnode->items);
for (i = 0; i < item->pathlen; i++) {
node = item->path[i];
if (node_help == NULL)
static int show_help(int ignore, int invoking_key)
{
const struct ec_completed_node *compnode;
-// const struct ec_completed_item *item;
-// struct ec_completed_iter *iter;
struct ec_completed *c;
struct ec_parsed *p;
char *line;
if (c == NULL)
return 1;
-#if 0 // old method
- count = ec_completed_count(c, EC_MATCH | EC_NO_MATCH |
- EC_PARTIAL_MATCH);
- helps = calloc(count + match + 1, sizeof(char *));
- if (helps == NULL)
- return 1;
-
- if (match)
- helps[1] = "<return>";
-
- iter = ec_completed_iter(c, EC_MATCH | EC_NO_MATCH |
- EC_PARTIAL_MATCH);
- if (iter == NULL)
- goto fail;
-
- /* strangely, rl_display_match_list() expects first index at 1 */
- for (i = match + 1, item = ec_completed_iter_next(iter);
- i < count + match + 1 && item != NULL;
- i++, item = ec_completed_iter_next(iter)) {
- helps[i] = get_node_help(item);
- }
-#else
+ /* let's display one contextual help per node */
count = 0;
TAILQ_FOREACH(compnode, &c->nodes, next) {
if (TAILQ_EMPTY(&compnode->items))
TAILQ_FOREACH(compnode, &c->nodes, next) {
if (TAILQ_EMPTY(&compnode->items))
continue;
- // we should pass compnode instead
- helps[i++] = get_node_help(TAILQ_FIRST(&compnode->items)); //XXX
+ helps[i++] = get_node_help(compnode);
}
-#endif
ec_completed_dump(stdout, c);
ec_completed_free(c);
return 0;
-//fail:
- free(helps);
- // free helps[n] XXX
- return 1;
+ // free helps[n] XXX on error ?
}
static int create_commands(void)
ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
"help", "the name of the person", NULL);
ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
- "help", "an integer", NULL);
+ "help", "an integer (0-10)", NULL);
if (ec_node_or_add(cmdlist, cmd) < 0)
goto fail;
cleanup / rework
================
+- ec_completed_item_update()
+- ec_completed_item_set_display_value()
+
- add_no_match
- add_partial_match
- check XXX in code