2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #define _GNU_SOURCE /* for asprintf */
34 #include <readline/readline.h>
35 #include <readline/history.h>
37 #include <ecoli_init.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41 #include <ecoli_keyval.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_node_seq.h>
44 #include <ecoli_node_space.h>
45 #include <ecoli_node_or.h>
46 #include <ecoli_node_sh_lex.h>
47 #include <ecoli_node_int.h>
48 #include <ecoli_node_option.h>
49 #include <ecoli_node_cmd.h>
50 #include <ecoli_node_many.h>
51 #include <ecoli_node_once.h>
52 #include <ecoli_node_file.h>
54 static struct ec_node *commands;
56 static char *my_completion_entry(const char *s, int state)
58 static struct ec_completed *c;
59 static struct ec_completed_iter *iter;
60 const struct ec_completed_item *item;
61 enum ec_completed_type item_type;
62 const char *item_str, *item_display;
66 /* don't append a quote */
67 rl_completion_suppress_quote = 1;
68 rl_basic_quote_characters = "";
75 line = strdup(rl_line_buffer);
78 line[rl_point] = '\0';
80 c = ec_node_complete(commands, line);
85 ec_completed_iter_free(iter);
86 iter = ec_completed_iter(c, EC_MATCH | EC_PARTIAL_MATCH);
91 item = ec_completed_iter_next(iter);
95 item_str = ec_completed_item_get_str(item);
96 if (c->count_match == 1) {
98 /* don't add the trailing space for partial completions */
100 item_type = ec_completed_item_get_type(item);
101 if (item_type == EC_MATCH)
102 rl_completion_suppress_append = 0;
104 rl_completion_suppress_append = 1;
107 return strdup(item_str);
108 } else if (rl_completion_type == '?') {
109 /* on second try only show the display part */
110 item_display = ec_completed_item_get_display(item);
111 return strdup(item_display);
114 return strdup(item_str);
117 static char **my_attempted_completion(const char *text, int start, int end)
122 /* remove default file completion */
123 rl_attempted_completion_over = 1;
125 return rl_completion_matches(text, my_completion_entry);
128 /* this function builds the help string */
129 static char *get_node_help(const struct ec_completed_node *compnode)
131 // const struct ec_completed_item *item;
132 // const struct ec_node *node;
134 const char *node_help = NULL;
135 const char *node_desc = NULL;
140 /* Since we display only one help per node, only look at the first item
141 * to get the path. The objective is to retrieve the most precise
142 * help for this node. */
143 item = TAILQ_FIRST(&compnode->items);
144 for (i = 0; i < item->pathlen; i++) {
145 node = item->path[i];
146 if (node_help == NULL)
147 node_help = ec_keyval_get(ec_node_attrs(node), "help");
148 if (node_desc == NULL)
149 node_desc = ec_node_desc(node);
153 if (node_help == NULL)
155 if (node_desc == NULL)
158 if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
164 static int show_help(int ignore, int invoking_key)
166 const struct ec_completed_node *compnode;
167 struct ec_completed *c;
170 unsigned int count, i;
177 line = strdup(rl_line_buffer);
181 /* check if the current line matches */
182 p = ec_node_parse(commands, line);
183 if (ec_parsed_matches(p))
187 /* complete at current cursor position */
188 line[rl_point] = '\0';
189 c = ec_node_complete(commands, line);
190 //ec_completed_dump(stdout, c);
195 /* let's display one contextual help per node */
197 TAILQ_FOREACH(compnode, &c->nodes, next) {
198 if (TAILQ_EMPTY(&compnode->items))
203 helps = calloc(count + match + 1, sizeof(char *));
208 helps[1] = "<return>";
210 /* strangely, rl_display_match_list() expects first index at 1 */
212 TAILQ_FOREACH(compnode, &c->nodes, next) {
213 if (TAILQ_EMPTY(&compnode->items))
215 helps[i++] = get_node_help(compnode);
218 ec_completed_free(c);
220 rl_display_match_list(helps, count + match, 1000); /* XXX 1000 */
222 rl_forced_update_display();
226 // free helps[n] XXX on error ?
229 static int create_commands(void)
231 struct ec_node *cmdlist = NULL, *cmd = NULL;
233 cmdlist = ec_node("or", NULL);
238 cmd = EC_NODE_SEQ(NULL,
239 ec_node_str(NULL, "hello"),
241 ec_node_str("john", "john"),
242 ec_node_str(NULL, "johnny"),
243 ec_node_str(NULL, "mike")
245 ec_node_option(NULL, ec_node_int("int", 0, 10, 10))
249 ec_keyval_set(ec_node_attrs(cmd), "help",
250 "say hello to someone several times", NULL);
251 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "john")),
252 "help", "specific help for john", NULL);
253 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
254 "help", "the name of the person", NULL);
255 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
256 "help", "an integer (0-10)", NULL);
257 if (ec_node_or_add(cmdlist, cmd) < 0)
261 cmd = EC_NODE_CMD(NULL, "good morning name [count]",
262 EC_NODE_CMD("name", "bob|bobby|michael"),
263 ec_node_int("count", 0, 10, 10));
266 ec_keyval_set(ec_node_attrs(cmd), "help",
267 "say good morning to someone several times", NULL);
268 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
269 "the person to greet", NULL);
270 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
271 "how many times to greet (0-10)", NULL);
272 if (ec_node_or_add(cmdlist, cmd) < 0)
276 cmd = EC_NODE_CMD(NULL,
277 "buy potatoes,carrots,pumpkins");
280 ec_keyval_set(ec_node_attrs(cmd), "help",
281 "buy some vegetables", NULL);
282 if (ec_node_or_add(cmdlist, cmd) < 0)
286 cmd = EC_NODE_CMD(NULL, "eat vegetables",
287 ec_node_many("vegetables",
289 ec_node_str(NULL, "potatoes"),
291 ec_node_str(NULL, "carrots")),
293 ec_node_str(NULL, "pumpkins"))),
297 ec_keyval_set(ec_node_attrs(cmd), "help",
298 "eat vegetables (take some more potatoes)", NULL);
299 if (ec_node_or_add(cmdlist, cmd) < 0)
303 cmd = EC_NODE_SEQ(NULL,
304 ec_node_str(NULL, "bye")
306 ec_keyval_set(ec_node_attrs(cmd), "help", "say bye", NULL);
307 if (ec_node_or_add(cmdlist, cmd) < 0)
311 cmd = EC_NODE_SEQ(NULL,
312 ec_node_str(NULL, "load"),
313 ec_node("file", NULL)
315 ec_keyval_set(ec_node_attrs(cmd), "help", "load a file", NULL);
316 if (ec_node_or_add(cmdlist, cmd) < 0)
320 commands = ec_node_sh_lex(NULL, cmdlist);
321 if (commands == NULL)
327 fprintf(stderr, "cannot initialize nodes\n");
328 ec_node_free(cmdlist);
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("> ");
353 p = ec_node_parse(commands, line);
354 ec_parsed_dump(stdout, p);
360 ec_node_free(commands);