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 *pstate;
110 const struct ec_node *node;
112 const char *node_help = NULL;
113 char *node_desc = NULL;
115 grp = ec_comp_item_get_grp(item);
116 for (pstate = ec_comp_group_get_pstate(grp); pstate != NULL;
117 pstate = ec_pnode_get_parent(pstate)) {
118 node = ec_pnode_get_node(pstate);
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)
138 static int show_help(int ignore, int invoking_key)
140 struct ec_comp_iter *iter = NULL;
141 const struct ec_comp_group *grp, *prev_grp = NULL;
142 const struct ec_comp_item *item;
143 struct ec_comp *c = NULL;
144 struct ec_pnode *p = NULL;
154 line = strdup(rl_line_buffer);
158 /* check if the current line matches */
159 p = ec_parse(commands, line);
160 if (ec_pnode_matches(p))
165 /* complete at current cursor position */
166 line[rl_point] = '\0';
167 c = ec_complete(commands, line);
173 /* let's display one contextual help per node */
175 iter = ec_comp_iter(c,
176 EC_COMP_UNKNOWN | EC_COMP_FULL | EC_COMP_PARTIAL);
180 /* strangely, rl_display_match_list() expects first index at 1 */
181 helps = calloc(match + 1, sizeof(char *));
185 helps[1] = "<return>";
187 while ((item = ec_comp_iter_next(iter)) != NULL) {
190 /* keep one help per group, skip other items */
191 grp = ec_comp_item_get_grp(item);
197 tmp = realloc(helps, (count + match + 2) * sizeof(char *));
201 helps[count + match + 1] = get_node_help(item);
205 ec_comp_iter_free(iter);
207 /* ensure not more than 1 entry per line */
208 rl_get_screen_size(NULL, &cols);
209 rl_display_match_list(helps, count + match, cols);
210 rl_forced_update_display();
215 ec_comp_iter_free(iter);
221 free(helps[count + match + 1]);
228 static int create_commands(void)
230 struct ec_node *cmdlist = NULL, *cmd = NULL;
232 cmdlist = ec_node("or", EC_NO_ID);
237 cmd = EC_NODE_SEQ(EC_NO_ID,
238 ec_node_str(EC_NO_ID, "hello"),
240 ec_node_str("john", "john"),
241 ec_node_str(EC_NO_ID, "johnny"),
242 ec_node_str(EC_NO_ID, "mike")
244 ec_node_option(EC_NO_ID, ec_node_int("int", 0, 10, 10))
248 ec_dict_set(ec_node_attrs(cmd), "help",
249 "say hello to someone several times", NULL);
250 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "john")),
251 "help", "specific help for john", NULL);
252 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "name")),
253 "help", "the name of the person", NULL);
254 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "int")),
255 "help", "an integer (0-10)", NULL);
256 if (ec_node_or_add(cmdlist, cmd) < 0)
260 cmd = EC_NODE_CMD(EC_NO_ID, "good morning name [count]",
261 EC_NODE_CMD("name", "bob|bobby|michael"),
262 ec_node_int("count", 0, 10, 10));
265 ec_dict_set(ec_node_attrs(cmd), "help",
266 "say good morning to someone several times", NULL);
267 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
268 "the person to greet", NULL);
269 ec_dict_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
270 "how many times to greet (0-10)", NULL);
271 if (ec_node_or_add(cmdlist, cmd) < 0)
275 cmd = EC_NODE_CMD(EC_NO_ID,
276 "buy potatoes,carrots,pumpkins");
279 ec_dict_set(ec_node_attrs(cmd), "help",
280 "buy some vegetables", NULL);
281 if (ec_node_or_add(cmdlist, cmd) < 0)
285 cmd = EC_NODE_CMD(EC_NO_ID, "eat vegetables",
286 ec_node_many("vegetables",
288 ec_node_str(EC_NO_ID, "potatoes"),
289 ec_node_once(EC_NO_ID,
290 ec_node_str(EC_NO_ID, "carrots")),
291 ec_node_once(EC_NO_ID,
292 ec_node_str(EC_NO_ID, "pumpkins"))),
296 ec_dict_set(ec_node_attrs(cmd), "help",
297 "eat vegetables (take some more potatoes)", NULL);
298 if (ec_node_or_add(cmdlist, cmd) < 0)
302 cmd = EC_NODE_SEQ(EC_NO_ID,
303 ec_node_str(EC_NO_ID, "bye")
305 ec_dict_set(ec_node_attrs(cmd), "help", "say bye", NULL);
306 if (ec_node_or_add(cmdlist, cmd) < 0)
310 cmd = EC_NODE_SEQ(EC_NO_ID,
311 ec_node_str(EC_NO_ID, "load"),
312 ec_node("file", EC_NO_ID)
314 ec_dict_set(ec_node_attrs(cmd), "help", "load a file", NULL);
315 if (ec_node_or_add(cmdlist, cmd) < 0)
319 commands = ec_node_sh_lex(EC_NO_ID, cmdlist);
320 if (commands == NULL)
326 fprintf(stderr, "cannot initialize nodes\n");
327 ec_node_free(cmdlist);
337 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
341 if (create_commands() < 0)
344 rl_bind_key('?', show_help);
345 rl_attempted_completion_function = my_attempted_completion;
348 line = readline("> ");
352 p = ec_parse(commands, line);
353 ec_pnode_dump(stdout, p);
359 ec_node_free(commands);