1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
5 #define _GNU_SOURCE /* for asprintf */
11 #include <readline/readline.h>
12 #include <readline/history.h>
14 #include <ecoli_init.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_keyval.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_seq.h>
21 #include <ecoli_node_space.h>
22 #include <ecoli_node_or.h>
23 #include <ecoli_node_sh_lex.h>
24 #include <ecoli_node_int.h>
25 #include <ecoli_node_option.h>
26 #include <ecoli_node_cmd.h>
27 #include <ecoli_node_many.h>
28 #include <ecoli_node_once.h>
29 #include <ecoli_node_file.h>
31 static struct ec_node *commands;
33 static char *my_completion_entry(const char *s, int state)
35 static struct ec_comp *c;
36 static struct ec_comp_iter *iter;
37 const struct ec_comp_item *item;
38 enum ec_comp_type item_type;
39 const char *item_str, *item_display;
43 /* Don't append a quote. Note: there are still some bugs when
44 * completing a quoted token. */
45 rl_completion_suppress_quote = 1;
46 rl_completer_quote_characters = "\"'";
52 line = strdup(rl_line_buffer);
55 line[rl_point] = '\0';
57 c = ec_node_complete(commands, line);
62 ec_comp_iter_free(iter);
63 iter = ec_comp_iter(c, EC_COMP_FULL | EC_COMP_PARTIAL);
68 item = ec_comp_iter_next(iter);
72 item_str = ec_comp_item_get_str(item);
73 if (c->count_full == 1) {
75 /* don't add the trailing space for partial completions */
77 item_type = ec_comp_item_get_type(item);
78 if (item_type == EC_COMP_FULL)
79 rl_completion_suppress_append = 0;
81 rl_completion_suppress_append = 1;
84 return strdup(item_str);
85 } else if (rl_completion_type == '?') {
86 /* on second try only show the display part */
87 item_display = ec_comp_item_get_display(item);
88 return strdup(item_display);
91 return strdup(item_str);
94 static char **my_attempted_completion(const char *text, int start, int end)
99 /* remove default file completion */
100 rl_attempted_completion_over = 1;
102 return rl_completion_matches(text, my_completion_entry);
105 /* this function builds the help string */
106 static char *get_node_help(const struct ec_comp_item *item)
108 const struct ec_comp_group *grp;
109 const struct ec_parse *state;
110 const struct ec_node *node;
112 const char *node_help = NULL;
113 const char *node_desc = NULL;
115 grp = ec_comp_item_get_grp(item);
117 for (state = grp->state; state != NULL;
118 state = ec_parse_get_parent(state)) {
119 node = ec_parse_get_node(state);
120 if (node_help == NULL)
121 node_help = ec_keyval_get(ec_node_attrs(node), "help");
122 if (node_desc == NULL)
123 node_desc = ec_node_desc(node);
126 if (node_help == NULL)
128 if (node_desc == NULL)
131 if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
137 static int show_help(int ignore, int invoking_key)
139 struct ec_comp_iter *iter = NULL;
140 const struct ec_comp_group *grp, *prev_grp = NULL;
141 const struct ec_comp_item *item;
142 struct ec_comp *c = NULL;
143 struct ec_parse *p = NULL;
153 line = strdup(rl_line_buffer);
157 /* check if the current line matches */
158 p = ec_node_parse(commands, line);
159 if (ec_parse_matches(p))
164 /* complete at current cursor position */
165 line[rl_point] = '\0';
166 c = ec_node_complete(commands, line);
172 /* let's display one contextual help per node */
174 iter = ec_comp_iter(c,
175 EC_COMP_UNKNOWN | EC_COMP_FULL | EC_COMP_PARTIAL);
179 /* strangely, rl_display_match_list() expects first index at 1 */
180 helps = calloc(match + 1, sizeof(char *));
184 helps[1] = "<return>";
186 while ((item = ec_comp_iter_next(iter)) != NULL) {
189 /* keep one help per group, skip other items */
190 grp = ec_comp_item_get_grp(item);
196 tmp = realloc(helps, (count + match + 2) * sizeof(char *));
200 helps[count + match + 1] = get_node_help(item);
204 ec_comp_iter_free(iter);
206 /* ensure not more than 1 entry per line */
207 rl_get_screen_size(NULL, &cols);
208 rl_display_match_list(helps, count + match, cols);
209 rl_forced_update_display();
214 ec_comp_iter_free(iter);
220 free(helps[count + match + 1]);
227 static int create_commands(void)
229 struct ec_node *cmdlist = NULL, *cmd = NULL;
231 cmdlist = ec_node("or", EC_NO_ID);
236 cmd = EC_NODE_SEQ(EC_NO_ID,
237 ec_node_str(EC_NO_ID, "hello"),
239 ec_node_str("john", "john"),
240 ec_node_str(EC_NO_ID, "johnny"),
241 ec_node_str(EC_NO_ID, "mike")
243 ec_node_option(EC_NO_ID, ec_node_int("int", 0, 10, 10))
247 ec_keyval_set(ec_node_attrs(cmd), "help",
248 "say hello to someone several times", NULL);
249 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "john")),
250 "help", "specific help for john", NULL);
251 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
252 "help", "the name of the person", NULL);
253 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
254 "help", "an integer (0-10)", NULL);
255 if (ec_node_or_add(cmdlist, cmd) < 0)
259 cmd = EC_NODE_CMD(EC_NO_ID, "good morning name [count]",
260 EC_NODE_CMD("name", "bob|bobby|michael"),
261 ec_node_int("count", 0, 10, 10));
264 ec_keyval_set(ec_node_attrs(cmd), "help",
265 "say good morning to someone several times", NULL);
266 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
267 "the person to greet", NULL);
268 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
269 "how many times to greet (0-10)", NULL);
270 if (ec_node_or_add(cmdlist, cmd) < 0)
274 cmd = EC_NODE_CMD(EC_NO_ID,
275 "buy potatoes,carrots,pumpkins");
278 ec_keyval_set(ec_node_attrs(cmd), "help",
279 "buy some vegetables", NULL);
280 if (ec_node_or_add(cmdlist, cmd) < 0)
284 cmd = EC_NODE_CMD(EC_NO_ID, "eat vegetables",
285 ec_node_many("vegetables",
287 ec_node_str(EC_NO_ID, "potatoes"),
288 ec_node_once(EC_NO_ID,
289 ec_node_str(EC_NO_ID, "carrots")),
290 ec_node_once(EC_NO_ID,
291 ec_node_str(EC_NO_ID, "pumpkins"))),
295 ec_keyval_set(ec_node_attrs(cmd), "help",
296 "eat vegetables (take some more potatoes)", NULL);
297 if (ec_node_or_add(cmdlist, cmd) < 0)
301 cmd = EC_NODE_SEQ(EC_NO_ID,
302 ec_node_str(EC_NO_ID, "bye")
304 ec_keyval_set(ec_node_attrs(cmd), "help", "say bye", NULL);
305 if (ec_node_or_add(cmdlist, cmd) < 0)
309 cmd = EC_NODE_SEQ(EC_NO_ID,
310 ec_node_str(EC_NO_ID, "load"),
311 ec_node("file", EC_NO_ID)
313 ec_keyval_set(ec_node_attrs(cmd), "help", "load a file", NULL);
314 if (ec_node_or_add(cmdlist, cmd) < 0)
318 commands = ec_node_sh_lex(EC_NO_ID, cmdlist);
319 if (commands == NULL)
325 fprintf(stderr, "cannot initialize nodes\n");
326 ec_node_free(cmdlist);
336 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
340 if (create_commands() < 0)
343 rl_bind_key('?', show_help);
344 rl_attempted_completion_function = my_attempted_completion;
347 line = readline("> ");
351 p = ec_node_parse(commands, line);
352 ec_parse_dump(stdout, p);
358 ec_node_free(commands);