39#include <readline/history.h>
40#include <readline/readline.h>
46static char *my_completion_entry(
const char *s,
int state)
49 static struct ec_comp_item *item;
51 const char *item_str, *item_display;
57 rl_completion_suppress_quote = 1;
58 rl_completer_quote_characters =
"\"'";
64 line = strdup(rl_line_buffer);
67 line[rl_point] =
'\0';
90 rl_completion_suppress_append = 0;
92 rl_completion_suppress_append = 1;
95 return strdup(item_str);
96 }
else if (rl_completion_type ==
'?') {
99 return strdup(item_display);
102 return strdup(item_str);
105static char **my_attempted_completion(
const char *text,
int start,
int end)
111 rl_attempted_completion_over = 1;
113 return rl_completion_matches(text, my_completion_entry);
117static char *get_node_help(
const struct ec_comp_item *item)
119 const struct ec_comp_group *grp;
123 const char *node_help = NULL;
124 char *node_desc = NULL;
130 if (node_help == NULL)
132 if (node_desc == NULL)
136 if (node_help == NULL)
138 if (node_desc == NULL)
141 if (asprintf(&help,
"%-20s %s", node_desc, node_help) < 0)
149static int show_help(
int ignore,
int invoking_key)
151 const struct ec_comp_group *grp, *prev_grp = NULL;
152 struct ec_comp_item *item = NULL;
165 line = strdup(rl_line_buffer);
177 line[rl_point] =
'\0';
185 helps = calloc(match + 1,
sizeof(
char *));
189 helps[1] =
"<return>";
202 tmp = realloc(helps, (count + match + 2) *
sizeof(
char *));
206 helps[count + match + 1] = get_node_help(item);
211 rl_get_screen_size(NULL, &cols);
212 rl_display_match_list(helps, count + match, cols);
213 rl_forced_update_display();
224 free(helps[count + match + 1]);
231static int create_commands(
void)
233 struct ec_node *cmdlist = NULL, *cmd = NULL;
265 "good morning name [count]",
276 "how many times to greet (0-10)",
321 if (commands == NULL)
327 fprintf(stderr,
"cannot initialize nodes\n");
338 fprintf(stderr,
"cannot init ecoli: %s\n", strerror(errno));
342 if (create_commands() < 0)
345 rl_bind_key(
'?', show_help);
346 rl_attempted_completion_function = my_attempted_completion;
349 line = readline(
"> ");
const struct ec_comp_group * ec_comp_item_get_grp(const struct ec_comp_item *item)
Get the group of a completion item.
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.
const struct ec_pnode * ec_comp_group_get_pstate(const struct ec_comp_group *grp)
Get the completion group parsing state.
struct ec_comp * ec_comp(void)
Create an empty completion object (list of completion items).
struct ec_comp * ec_complete(const struct ec_node *node, const char *str)
Get the list of completions from a string input.
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.
const char * ec_comp_item_get_display(const struct ec_comp_item *item)
Get the display string value of a completion item.
size_t ec_comp_count(const struct ec_comp *comp, enum ec_comp_type type)
Get the number of completion items.
void ec_comp_free(struct ec_comp *comp)
Free a completion object and all its items.
ec_comp_type
Completion item type.
const char * ec_comp_item_get_str(const struct ec_comp_item *item)
Get the string value of a completion item.
#define EC_COMP_FOREACH(item, comp, type)
Iterate items matching a given 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
The item is fully completed.
@ EC_COMP_PARTIAL
The item is partially completed.
@ EC_COMP_UNKNOWN
Valid token but completion not possible.
void * ec_dict_get(const struct ec_dict *dict, const char *key)
Get a value from the hash table.
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.
int ec_init(void)
Initialize ecoli library.
#define EC_NODE_CMD(args...)
Create a command node from a format string.
struct ec_node * ec_node_int(const char *id, int64_t min, int64_t max, unsigned int base)
Create a signed integer node.
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.
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.
struct ec_node * ec_node_option(const char *id, struct ec_node *node)
Create an option node that makes its child optional.
int ec_node_or_add(struct ec_node *node, struct ec_node *child)
Add a child to an "or" node.
#define EC_NODE_OR(args...)
Create a new "or" node from an arbitrary list of child nodes.
#define EC_NODE_SEQ(args...)
Create a sequence node from a list of child nodes.
struct ec_node * ec_node_sh_lex(const char *id, struct ec_node *child)
Create a shell lexer node.
struct ec_node * ec_node_str(const char *id, const char *str)
Create a string node that matches a specific string.
struct ec_node * ec_node(const char *typename, const char *id)
Create a new node from its type name.
struct ec_dict * ec_node_attrs(const struct ec_node *node)
Get the attributes dict of the node.
struct ec_node * ec_node_find(struct ec_node *node, const char *id)
Find a node from its identifier string.
#define EC_NO_ID
Node has no identifier.
char * ec_node_desc(const struct ec_node *node)
Get node short description.
void ec_node_free(struct ec_node *node)
Decrement node reference counter and free the node if it is the last reference.
bool ec_pnode_matches(const struct ec_pnode *pnode)
Check if the parsing tree matches the input.
struct ec_pnode * ec_pnode_get_parent(const struct ec_pnode *pnode)
Get the parent node in the parsing tree.
struct ec_pnode * ec_parse(const struct ec_node *node, const char *str)
Parse a string using a grammar tree.
void ec_pnode_dump(FILE *out, const struct ec_pnode *pnode)
Dump a parsing tree.
void ec_pnode_free(struct ec_pnode *pnode)
Free a parsing tree.
const struct ec_node * ec_pnode_get_node(const struct ec_pnode *pnode)
Get the grammar node corresponding to the parsing node.
struct ec_pnode * ec_pnode(const struct ec_node *node)
Create an empty parsing tree.