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_dict.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_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 (ec_comp_count(c, EC_COMP_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_pnode *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);
116 for (state = ec_comp_group_get_state(grp); state != NULL;
117 state = ec_pnode_get_parent(state)) {
118 node = ec_pnode_get_node(state);
119 if (node_help == NULL)
120 node_help = ec_dict_get(ec_node_attrs(node), "help");
121 if (node_desc == NULL)
122 node_desc = ec_node_desc(node);
125 if (node_help == NULL)
127 if (node_desc == NULL)
130 if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
136 static int show_help(int ignore, int invoking_key)
138 struct ec_comp_iter *iter = NULL;
139 const struct ec_comp_group *grp, *prev_grp = NULL;
140 const struct ec_comp_item *item;
141 struct ec_comp *c = NULL;
142 struct ec_pnode *p = NULL;
152 line = strdup(rl_line_buffer);
156 /* check if the current line matches */
157 p = ec_parse(commands, line);
158 if (ec_pnode_matches(p))
163 /* complete at current cursor position */
164 line[rl_point] = '\0';
165 c = ec_complete(commands, line);
171 /* let's display one contextual help per node */
173 iter = ec_comp_iter(c,
174 EC_COMP_UNKNOWN | EC_COMP_FULL | EC_COMP_PARTIAL);
178 /* strangely, rl_display_match_list() expects first index at 1 */
179 helps = calloc(match + 1, sizeof(char *));
183 helps[1] = "<return>";
185 while ((item = ec_comp_iter_next(iter)) != NULL) {
188 /* keep one help per group, skip other items */
189 grp = ec_comp_item_get_grp(item);
195 tmp = realloc(helps, (count + match + 2) * sizeof(char *));
199 helps[count + match + 1] = get_node_help(item);
203 ec_comp_iter_free(iter);
205 /* ensure not more than 1 entry per line */
206 rl_get_screen_size(NULL, &cols);
207 rl_display_match_list(helps, count + match, cols);
208 rl_forced_update_display();
213 ec_comp_iter_free(iter);
219 free(helps[count + match + 1]);
226 static int create_commands(void)
228 struct ec_node *cmdlist = NULL, *cmd = NULL;
230 cmdlist = ec_node("or", EC_NO_ID);
235 cmd = EC_NODE_SEQ(EC_NO_ID,
236 ec_node_str(EC_NO_ID, "hello"),
238 ec_node_str("john", "john"),
239 ec_node_str(EC_NO_ID, "johnny"),
240 ec_node_str(EC_NO_ID, "mike")
242 ec_node_option(EC_NO_ID, ec_node_int("int", 0, 10, 10))
246 ec_dict_set(ec_node_attrs(cmd), "help",
247 "say hello to someone several times", NULL);
248 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "john")),
249 "help", "specific help for john", NULL);
250 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "name")),
251 "help", "the name of the person", NULL);
252 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "int")),
253 "help", "an integer (0-10)", NULL);
254 if (ec_node_or_add(cmdlist, cmd) < 0)
258 cmd = EC_NODE_CMD(EC_NO_ID, "good morning name [count]",
259 EC_NODE_CMD("name", "bob|bobby|michael"),
260 ec_node_int("count", 0, 10, 10));
263 ec_dict_set(ec_node_attrs(cmd), "help",
264 "say good morning to someone several times", NULL);
265 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
266 "the person to greet", NULL);
267 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
268 "how many times to greet (0-10)", NULL);
269 if (ec_node_or_add(cmdlist, cmd) < 0)
273 cmd = EC_NODE_CMD(EC_NO_ID,
274 "buy potatoes,carrots,pumpkins");
277 ec_dict_set(ec_node_attrs(cmd), "help",
278 "buy some vegetables", NULL);
279 if (ec_node_or_add(cmdlist, cmd) < 0)
283 cmd = EC_NODE_CMD(EC_NO_ID, "eat vegetables",
284 ec_node_many("vegetables",
286 ec_node_str(EC_NO_ID, "potatoes"),
287 ec_node_once(EC_NO_ID,
288 ec_node_str(EC_NO_ID, "carrots")),
289 ec_node_once(EC_NO_ID,
290 ec_node_str(EC_NO_ID, "pumpkins"))),
294 ec_dict_set(ec_node_attrs(cmd), "help",
295 "eat vegetables (take some more potatoes)", NULL);
296 if (ec_node_or_add(cmdlist, cmd) < 0)
300 cmd = EC_NODE_SEQ(EC_NO_ID,
301 ec_node_str(EC_NO_ID, "bye")
303 ec_dict_set(ec_node_attrs(cmd), "help", "say bye", NULL);
304 if (ec_node_or_add(cmdlist, cmd) < 0)
308 cmd = EC_NODE_SEQ(EC_NO_ID,
309 ec_node_str(EC_NO_ID, "load"),
310 ec_node("file", EC_NO_ID)
312 ec_dict_set(ec_node_attrs(cmd), "help", "load a file", NULL);
313 if (ec_node_or_add(cmdlist, cmd) < 0)
317 commands = ec_node_sh_lex(EC_NO_ID, cmdlist);
318 if (commands == NULL)
324 fprintf(stderr, "cannot initialize nodes\n");
325 ec_node_free(cmdlist);
335 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
339 if (create_commands() < 0)
342 rl_bind_key('?', show_help);
343 rl_attempted_completion_function = my_attempted_completion;
346 line = readline("> ");
350 p = ec_parse(commands, line);
351 ec_pnode_dump(stdout, p);
357 ec_node_free(commands);