Integration with GNU readline instead of libedit.
Demonstrates how to use libecoli's completion API with GNU readline by implementing custom completion functions.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <ecoli.h>
static char *my_completion_entry(const char *s, int state)
{
static struct ec_comp_item *item;
const char *item_str, *item_display;
(void)s;
rl_completion_suppress_quote = 1;
rl_completer_quote_characters = "\"'";
if (state == 0) {
char *line;
line = strdup(rl_line_buffer);
if (line == NULL)
return NULL;
line[rl_point] = '\0';
free(line);
if (c == NULL)
return NULL;
free(item);
if (item == NULL)
return NULL;
} else {
if (item == NULL)
return NULL;
}
if (state == 0) {
rl_completion_suppress_append = 0;
else
rl_completion_suppress_append = 1;
}
return strdup(item_str);
} else if (rl_completion_type == '?') {
return strdup(item_display);
}
return strdup(item_str);
}
static char **my_attempted_completion(const char *text, int start, int end)
{
(void)start;
(void)end;
rl_attempted_completion_over = 1;
return rl_completion_matches(text, my_completion_entry);
}
static char *get_node_help(const struct ec_comp_item *item)
{
const struct ec_comp_group *grp;
char *help = NULL;
const char *node_help = NULL;
char *node_desc = NULL;
if (node_help == NULL)
if (node_desc == NULL)
}
if (node_help == NULL)
node_help = "-";
if (node_desc == NULL)
return NULL;
if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
return NULL;
free(node_desc);
return help;
}
static int show_help(int ignore, int invoking_key)
{
const struct ec_comp_group *grp, *prev_grp = NULL;
struct ec_comp_item *item = NULL;
char *line = NULL;
size_t count = 0;
char **helps = NULL;
int match = 0;
int ret = 1;
int cols;
(void)ignore;
(void)invoking_key;
line = strdup(rl_line_buffer);
if (line == NULL)
goto fail;
match = 1;
p = NULL;
line[rl_point] = '\0';
free(line);
line = NULL;
if (c == NULL)
goto fail;
helps = calloc(match + 1, sizeof(char *));
if (helps == NULL)
goto fail;
if (match)
helps[1] = "<return>";
char **tmp;
if (grp == prev_grp)
continue;
prev_grp = grp;
tmp = realloc(helps, (count + match + 2) * sizeof(char *));
if (tmp == NULL)
goto fail;
helps = tmp;
helps[count + match + 1] = get_node_help(item);
count++;
}
rl_get_screen_size(NULL, &cols);
rl_display_match_list(helps, count + match, cols);
rl_forced_update_display();
ret = 0;
fail:
free(item);
free(line);
if (helps != NULL) {
while (count--)
free(helps[count + match + 1]);
}
free(helps);
return ret;
}
static int create_commands(void)
{
struct ec_node *cmdlist = NULL, *cmd = NULL;
if (cmdlist == NULL)
goto fail;
"name",
),
);
if (cmd == NULL)
goto fail;
);
);
goto fail;
"good morning name [count]",
);
if (cmd == NULL)
goto fail;
"help",
"how many times to greet (0-10)",
NULL
);
goto fail;
if (cmd == NULL)
goto fail;
goto fail;
"eat vegetables",
"vegetables",
),
1,
0
)
);
if (cmd == NULL)
goto fail;
goto fail;
goto fail;
goto fail;
if (commands == NULL)
goto fail;
return 0;
fail:
fprintf(stderr, "cannot initialize nodes\n");
return -1;
}
int main(void)
{
char *line;
fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
return 1;
}
if (create_commands() < 0)
return 1;
rl_bind_key('?', show_help);
rl_attempted_completion_function = my_attempted_completion;
while (1) {
line = readline("> ");
if (line == NULL)
break;
add_history(line);
}
return 0;
}
const struct ec_comp_group * ec_comp_item_get_grp(const struct ec_comp_item *item)
struct ec_comp_item * ec_comp_iter_next(struct ec_comp_item *item, enum ec_comp_type type)
const struct ec_pnode * ec_comp_group_get_pstate(const struct ec_comp_group *grp)
struct ec_comp * ec_comp(void)
struct ec_comp * ec_complete(const struct ec_node *node, const char *str)
struct ec_comp_item * ec_comp_iter_first(const struct ec_comp *comp, enum ec_comp_type type)
const char * ec_comp_item_get_display(const struct ec_comp_item *item)
size_t ec_comp_count(const struct ec_comp *comp, enum ec_comp_type type)
void ec_comp_free(struct ec_comp *comp)
const char * ec_comp_item_get_str(const struct ec_comp_item *item)
#define EC_COMP_FOREACH(item, comp, type)
enum ec_comp_type ec_comp_item_get_type(const struct ec_comp_item *item)
void * ec_dict_get(const struct ec_dict *dict, const char *key)
int ec_dict_set(struct ec_dict *dict, const char *key, void *val, ec_dict_elt_free_t free_cb)
#define EC_NODE_CMD(args...)
struct ec_node * ec_node_int(const char *id, int64_t min, int64_t max, unsigned int base)
struct ec_node * ec_node_many(const char *id, struct ec_node *child, unsigned int min, unsigned int max)
struct ec_node * ec_node_once(const char *id, struct ec_node *child)
struct ec_node * ec_node_option(const char *id, struct ec_node *node)
int ec_node_or_add(struct ec_node *node, struct ec_node *child)
#define EC_NODE_OR(args...)
#define EC_NODE_SEQ(args...)
struct ec_node * ec_node_sh_lex(const char *id, struct ec_node *child)
struct ec_node * ec_node_str(const char *id, const char *str)
struct ec_node * ec_node(const char *typename, const char *id)
struct ec_dict * ec_node_attrs(const struct ec_node *node)
struct ec_node * ec_node_find(struct ec_node *node, const char *id)
char * ec_node_desc(const struct ec_node *node)
void ec_node_free(struct ec_node *node)
bool ec_pnode_matches(const struct ec_pnode *pnode)
struct ec_pnode * ec_pnode_get_parent(const struct ec_pnode *pnode)
struct ec_pnode * ec_parse(const struct ec_node *node, const char *str)
void ec_pnode_dump(FILE *out, const struct ec_pnode *pnode)
void ec_pnode_free(struct ec_pnode *pnode)
const struct ec_node * ec_pnode_get_node(const struct ec_pnode *pnode)
struct ec_pnode * ec_pnode(const struct ec_node *node)