Loading...
Searching...
No Matches
readline/main.c

Integration with GNU readline instead of libedit.

Integration with GNU readline instead of libedit.Demonstrates how to use libecoli's completion API with GNU readline by implementing custom completion functions.

Example session:

> hello john 5
parse(match=yes, len=3)
seq(match=yes, len=3)
str(hello)(match=yes, len=1) "hello"
or(name)(match=yes, len=1)
str(john)(match=yes, len=1) "john"
int(int)(match=yes, len=1) "5"
> good morning bob
parse(match=yes, len=3)
cmd(match=yes, len=3)
cmd(name)(match=yes, len=1) "bob"
> buy potatoes
parse(match=yes, len=2)
cmd(match=yes, len=2)
> bye
parse(match=yes, len=1)
seq(match=yes, len=1)
str(bye)(match=yes, len=1) "bye"
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <ecoli.h>
static struct ec_node *commands;
static char *my_completion_entry(const char *s, int state)
{
static struct ec_comp *c;
static struct ec_comp_item *item;
enum ec_comp_type item_type;
const char *item_str, *item_display;
(void)s;
/* Don't append a quote. Note: there are still some bugs when
* completing a quoted token. */
rl_completion_suppress_quote = 1;
rl_completer_quote_characters = "\"'";
if (state == 0) {
char *line;
ec_comp_free(c);
line = strdup(rl_line_buffer);
if (line == NULL)
return NULL;
line[rl_point] = '\0';
c = ec_complete(commands, line);
free(line);
if (c == NULL)
return NULL;
free(item);
item = ec_comp_iter_first(c, EC_COMP_FULL | EC_COMP_PARTIAL);
if (item == NULL)
return NULL;
} else {
item = ec_comp_iter_next(item, EC_COMP_FULL | EC_COMP_PARTIAL);
if (item == NULL)
return NULL;
}
item_str = ec_comp_item_get_str(item);
if (ec_comp_count(c, EC_COMP_FULL | EC_COMP_PARTIAL) == 1) {
/* don't add the trailing space for partial completions */
if (state == 0) {
item_type = ec_comp_item_get_type(item);
if (item_type == EC_COMP_FULL)
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 */
item_display = ec_comp_item_get_display(item);
return strdup(item_display);
}
return strdup(item_str);
}
static char **my_attempted_completion(const char *text, int start, int end)
{
(void)start;
(void)end;
/* remove default file completion */
rl_attempted_completion_over = 1;
return rl_completion_matches(text, my_completion_entry);
}
/* this function builds the help string */
static char *get_node_help(const struct ec_comp_item *item)
{
const struct ec_comp_group *grp;
const struct ec_pnode *pstate;
const struct ec_node *node;
char *help = NULL;
const char *node_help = NULL;
char *node_desc = NULL;
grp = ec_comp_item_get_grp(item);
for (pstate = ec_comp_group_get_pstate(grp); pstate != NULL;
pstate = ec_pnode_get_parent(pstate)) {
node = ec_pnode_get_node(pstate);
if (node_help == NULL)
node_help = ec_dict_get(ec_node_attrs(node), "help");
if (node_desc == NULL)
node_desc = ec_node_desc(node);
}
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;
struct ec_comp *c = NULL;
struct ec_pnode *p = 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;
/* check if the current line matches */
p = ec_parse(commands, line);
if (ec_pnode_matches(p))
match = 1;
ec_pnode_free(p);
p = NULL;
/* complete at current cursor position */
line[rl_point] = '\0';
c = ec_complete(commands, line);
free(line);
line = NULL;
if (c == NULL)
goto fail;
/* strangely, rl_display_match_list() expects first index at 1 */
helps = calloc(match + 1, sizeof(char *));
if (helps == NULL)
goto fail;
if (match)
helps[1] = "<return>";
/* let's display one contextual help per node */
EC_COMP_FOREACH (item, c, EC_COMP_UNKNOWN | EC_COMP_FULL | EC_COMP_PARTIAL) {
char **tmp;
/* keep one help per group, skip other items */
grp = ec_comp_item_get_grp(item);
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++;
}
/* ensure not more than 1 entry per line */
rl_get_screen_size(NULL, &cols);
rl_display_match_list(helps, count + match, cols);
rl_forced_update_display();
ret = 0;
fail:
free(item);
ec_pnode_free(p);
free(line);
ec_comp_free(c);
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;
cmdlist = ec_node("or", EC_NO_ID);
if (cmdlist == NULL)
goto fail;
cmd = EC_NODE_SEQ(
EC_NO_ID,
ec_node_str(EC_NO_ID, "hello"),
EC_NODE_OR(
"name",
ec_node_str("john", "john"),
ec_node_str(EC_NO_ID, "johnny"),
ec_node_str(EC_NO_ID, "mike")
),
ec_node_option(EC_NO_ID, ec_node_int("int", 0, 10, 10))
);
if (cmd == NULL)
goto fail;
ec_dict_set(ec_node_attrs(cmd), "help", "say hello to someone several times", NULL);
ec_dict_set(
ec_node_attrs(ec_node_find(cmd, "john")), "help", "specific help for john", NULL
);
ec_dict_set(
ec_node_attrs(ec_node_find(cmd, "name")), "help", "the name of the person", NULL
);
ec_dict_set(ec_node_attrs(ec_node_find(cmd, "int")), "help", "an integer (0-10)", NULL);
if (ec_node_or_add(cmdlist, cmd) < 0)
goto fail;
cmd = EC_NODE_CMD(
EC_NO_ID,
"good morning name [count]",
EC_NODE_CMD("name", "bob|bobby|michael"),
ec_node_int("count", 0, 10, 10)
);
if (cmd == NULL)
goto fail;
ec_dict_set(ec_node_attrs(cmd), "help", "say good morning to someone several times", NULL);
ec_dict_set(ec_node_attrs(ec_node_find(cmd, "name")), "help", "the person to greet", NULL);
ec_dict_set(
ec_node_attrs(ec_node_find(cmd, "count")),
"help",
"how many times to greet (0-10)",
NULL
);
if (ec_node_or_add(cmdlist, cmd) < 0)
goto fail;
cmd = EC_NODE_CMD(EC_NO_ID, "buy potatoes,carrots,pumpkins");
if (cmd == NULL)
goto fail;
ec_dict_set(ec_node_attrs(cmd), "help", "buy some vegetables", NULL);
if (ec_node_or_add(cmdlist, cmd) < 0)
goto fail;
cmd = EC_NODE_CMD(
EC_NO_ID,
"eat vegetables",
ec_node_many(
"vegetables",
EC_NODE_OR(
EC_NO_ID,
ec_node_str(EC_NO_ID, "potatoes"),
ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "carrots")),
ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "pumpkins"))
),
1,
0
)
);
if (cmd == NULL)
goto fail;
ec_dict_set(ec_node_attrs(cmd), "help", "eat vegetables (take some more potatoes)", NULL);
if (ec_node_or_add(cmdlist, cmd) < 0)
goto fail;
cmd = EC_NODE_SEQ(EC_NO_ID, ec_node_str(EC_NO_ID, "bye"));
ec_dict_set(ec_node_attrs(cmd), "help", "say bye", NULL);
if (ec_node_or_add(cmdlist, cmd) < 0)
goto fail;
cmd = EC_NODE_SEQ(EC_NO_ID, ec_node_str(EC_NO_ID, "load"), ec_node("file", EC_NO_ID));
ec_dict_set(ec_node_attrs(cmd), "help", "load a file", NULL);
if (ec_node_or_add(cmdlist, cmd) < 0)
goto fail;
commands = ec_node_sh_lex(EC_NO_ID, cmdlist);
if (commands == NULL)
goto fail;
return 0;
fail:
fprintf(stderr, "cannot initialize nodes\n");
ec_node_free(cmdlist);
return -1;
}
int main(void)
{
struct ec_pnode *p;
char *line;
if (ec_init() < 0) {
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;
p = ec_parse(commands, line);
ec_pnode_dump(stdout, p);
add_history(line);
ec_pnode_free(p);
}
ec_node_free(commands);
return 0;
}
ec_comp_item_get_grp
const struct ec_comp_group * ec_comp_item_get_grp(const struct ec_comp_item *item)
Get the group of a completion item.
ec_comp_iter_next
struct ec_comp_item * ec_comp_iter_next(struct ec_comp_item *item, enum ec_comp_type type)
Get the next completion item matching the type.
ec_comp_group_get_pstate
const struct ec_pnode * ec_comp_group_get_pstate(const struct ec_comp_group *grp)
Get the completion group parsing state.
ec_comp
struct ec_comp * ec_comp(void)
Create an empty completion object (list of completion items).
ec_complete
struct ec_comp * ec_complete(const struct ec_node *node, const char *str)
Get the list of completions from a string input.
ec_comp_iter_first
struct ec_comp_item * ec_comp_iter_first(const struct ec_comp *comp, enum ec_comp_type type)
Get the first completion item matching the type.
ec_comp_item_get_display
const char * ec_comp_item_get_display(const struct ec_comp_item *item)
Get the display string value of a completion item.
ec_comp_count
size_t ec_comp_count(const struct ec_comp *comp, enum ec_comp_type type)
Get the number of completion items.
ec_comp_free
void ec_comp_free(struct ec_comp *comp)
Free a completion object and all its items.
ec_comp_type
ec_comp_type
Completion item type.
Definition complete.h:47
ec_comp_item_get_str
const char * ec_comp_item_get_str(const struct ec_comp_item *item)
Get the string value of a completion item.
EC_COMP_FOREACH
#define EC_COMP_FOREACH(item, comp, type)
Iterate items matching a given type.
Definition complete.h:522
ec_comp_item_get_type
enum ec_comp_type ec_comp_item_get_type(const struct ec_comp_item *item)
Get the type of a completion item.
EC_COMP_FULL
@ EC_COMP_FULL
The item is fully completed.
Definition complete.h:49
EC_COMP_PARTIAL
@ EC_COMP_PARTIAL
The item is partially completed.
Definition complete.h:50
EC_COMP_UNKNOWN
@ EC_COMP_UNKNOWN
Valid token but completion not possible.
Definition complete.h:48
ec_dict_get
void * ec_dict_get(const struct ec_dict *dict, const char *key)
Get a value from the hash table.
ec_dict_set
int ec_dict_set(struct ec_dict *dict, const char *key, void *val, ec_dict_elt_free_t free_cb)
Add/replace an object in the hash table.
ec_init
int ec_init(void)
Initialize ecoli library.
EC_NODE_CMD
#define EC_NODE_CMD(args...)
Create a command node from a format string.
Definition node_cmd.h:30
ec_node_int
struct ec_node * ec_node_int(const char *id, int64_t min, int64_t max, unsigned int base)
Create a signed integer node.
ec_node_many
struct ec_node * ec_node_many(const char *id, struct ec_node *child, unsigned int min, unsigned int max)
Create a many node that matches its child multiple times.
ec_node_once
struct ec_node * ec_node_once(const char *id, struct ec_node *child)
This node behaves like its child, but prevents it from being parsed more than once.
ec_node_option
struct ec_node * ec_node_option(const char *id, struct ec_node *node)
Create an option node that makes its child optional.
ec_node_or_add
int ec_node_or_add(struct ec_node *node, struct ec_node *child)
Add a child to an "or" node.
EC_NODE_OR
#define EC_NODE_OR(args...)
Create a new "or" node from an arbitrary list of child nodes.
Definition node_or.h:23
EC_NODE_SEQ
#define EC_NODE_SEQ(args...)
Create a sequence node from a list of child nodes.
Definition node_seq.h:29
ec_node_sh_lex
struct ec_node * ec_node_sh_lex(const char *id, struct ec_node *child)
Create a shell lexer node.
ec_node_str
struct ec_node * ec_node_str(const char *id, const char *str)
Create a string node that matches a specific string.
ec_node
struct ec_node * ec_node(const char *typename, const char *id)
Create a new node from its type name.
ec_node_attrs
struct ec_dict * ec_node_attrs(const struct ec_node *node)
Get the attributes dict of the node.
ec_node_find
struct ec_node * ec_node_find(struct ec_node *node, const char *id)
Find a node from its identifier string.
EC_NO_ID
#define EC_NO_ID
Node has no identifier.
Definition node.h:64
ec_node_desc
char * ec_node_desc(const struct ec_node *node)
Get node short description.
ec_node_free
void ec_node_free(struct ec_node *node)
Decrement node reference counter and free the node if it is the last reference.
ec_pnode_matches
bool ec_pnode_matches(const struct ec_pnode *pnode)
Check if the parsing tree matches the input.
ec_pnode_get_parent
struct ec_pnode * ec_pnode_get_parent(const struct ec_pnode *pnode)
Get the parent node in the parsing tree.
ec_parse
struct ec_pnode * ec_parse(const struct ec_node *node, const char *str)
Parse a string using a grammar tree.
ec_pnode_dump
void ec_pnode_dump(FILE *out, const struct ec_pnode *pnode)
Dump a parsing tree.
ec_pnode_free
void ec_pnode_free(struct ec_pnode *pnode)
Free a parsing tree.
ec_pnode_get_node
const struct ec_node * ec_pnode_get_node(const struct ec_pnode *pnode)
Get the grammar node corresponding to the parsing node.
ec_pnode
struct ec_pnode * ec_pnode(const struct ec_node *node)
Create an empty parsing tree.