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 */
33 #include <readline/readline.h>
34 #include <readline/history.h>
36 #include <ecoli_node.h>
37 #include <ecoli_parsed.h>
38 #include <ecoli_completed.h>
39 #include <ecoli_keyval.h>
40 #include <ecoli_node_str.h>
41 #include <ecoli_node_seq.h>
42 #include <ecoli_node_space.h>
43 #include <ecoli_node_or.h>
44 #include <ecoli_node_sh_lex.h>
45 #include <ecoli_node_int.h>
46 #include <ecoli_node_option.h>
47 #include <ecoli_node_cmd.h>
48 #include <ecoli_node_many.h>
49 #include <ecoli_node_once.h>
50 #include <ecoli_node_file.h>
52 static struct ec_node *commands;
54 static char *my_completion_entry(const char *s, int state)
56 static struct ec_completed *c;
57 static struct ec_completed_iter *iter;
58 static const struct ec_completed_item *item;
61 /* don't append a quote */
62 rl_completion_suppress_quote = 1;
63 rl_basic_quote_characters = "";
70 line = strdup(rl_line_buffer);
73 line[rl_point] = '\0';
75 c = ec_node_complete(commands, line);
80 ec_completed_iter_free(iter);
81 iter = ec_completed_iter(c, EC_MATCH | EC_PARTIAL_MATCH);
86 item = ec_completed_iter_next(iter);
90 /* don't add the trailing space for partial completions */
92 if (item->type == EC_MATCH)
93 rl_completion_suppress_append = 0;
95 rl_completion_suppress_append = 1;
98 //XXX see printable_part() and rl_display_match_list()
99 //XXX or: if match count > 1, strip beginning (in node_file)
101 if (asprintf(&out_string, "%s%s", s, item->add) < 0) // XXX ec_asprintf (check all in code)
107 static char **my_attempted_completion(const char *text, int start, int end)
112 /* remove default file completion */
113 rl_attempted_completion_over = 1;
115 return rl_completion_matches(text, my_completion_entry);
118 /* this function builds the help string */
119 static char *get_node_help(const struct ec_completed_item *item)
121 const struct ec_node *node;
123 const char *node_help = NULL;
124 const char *node_desc = NULL;
127 for (i = 0; i < item->pathlen; i++) {
128 node = item->path[i];
129 if (node_help == NULL)
130 node_help = ec_keyval_get(ec_node_attrs(node), "help");
131 if (node_desc == NULL)
132 node_desc = ec_node_desc(node);
135 if (node_help == NULL)
137 if (node_desc == NULL)
140 if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
146 static int show_help(int ignore, int invoking_key)
148 const struct ec_completed_node *compnode;
149 // const struct ec_completed_item *item;
150 // struct ec_completed_iter *iter;
151 struct ec_completed *c;
154 unsigned int count, i;
161 line = strdup(rl_line_buffer);
165 /* check if the current line matches */
166 p = ec_node_parse(commands, line);
167 if (ec_parsed_matches(p))
171 /* complete at current cursor position */
172 line[rl_point] = '\0';
173 c = ec_node_complete(commands, line);
174 //ec_completed_dump(stdout, c);
180 count = ec_completed_count(c, EC_MATCH | EC_NO_MATCH |
182 helps = calloc(count + match + 1, sizeof(char *));
187 helps[1] = "<return>";
189 iter = ec_completed_iter(c, EC_MATCH | EC_NO_MATCH |
194 /* strangely, rl_display_match_list() expects first index at 1 */
195 for (i = match + 1, item = ec_completed_iter_next(iter);
196 i < count + match + 1 && item != NULL;
197 i++, item = ec_completed_iter_next(iter)) {
198 helps[i] = get_node_help(item);
202 TAILQ_FOREACH(compnode, &c->nodes, next) {
203 if (TAILQ_EMPTY(&compnode->matches))
208 helps = calloc(count + match + 1, sizeof(char *));
213 helps[1] = "<return>";
216 TAILQ_FOREACH(compnode, &c->nodes, next) {
217 if (TAILQ_EMPTY(&compnode->matches))
219 // we should pass compnode instead
220 helps[i++] = get_node_help(TAILQ_FIRST(&compnode->matches)); //XXX
224 ec_completed_dump(stdout, c);
225 ec_completed_free(c);
227 rl_display_match_list(helps, count + match, 1000); /* XXX 1000 */
229 rl_forced_update_display();
239 static int create_commands(void)
241 struct ec_node *cmdlist = NULL, *cmd = NULL;
243 cmdlist = ec_node("or", NULL);
248 cmd = EC_NODE_SEQ(NULL,
249 ec_node_str(NULL, "hello"),
251 ec_node_str("john", "john"),
252 ec_node_str(NULL, "johnny"),
253 ec_node_str(NULL, "mike")
255 ec_node_option(NULL, ec_node_int("int", 0, 10, 10))
259 ec_keyval_set(ec_node_attrs(cmd), "help",
260 "say hello to someone several times", NULL);
261 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "john")),
262 "help", "specific help for john", NULL);
263 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
264 "help", "the name of the person", NULL);
265 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
266 "help", "an integer", NULL);
267 if (ec_node_or_add(cmdlist, cmd) < 0)
271 cmd = EC_NODE_CMD(NULL, "good morning name [count]",
272 EC_NODE_CMD("name", "bob|bobby|michael"),
273 ec_node_int("count", 0, 10, 10));
276 ec_keyval_set(ec_node_attrs(cmd), "help",
277 "say good morning to someone several times", NULL);
278 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
279 "the person to greet", NULL);
280 ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
281 "how many times to greet", NULL);
282 if (ec_node_or_add(cmdlist, cmd) < 0)
286 cmd = EC_NODE_CMD(NULL,
287 "buy potatoes,carrots,pumpkins");
290 ec_keyval_set(ec_node_attrs(cmd), "help",
291 "buy some vegetables", NULL);
292 if (ec_node_or_add(cmdlist, cmd) < 0)
296 cmd = EC_NODE_CMD(NULL, "eat vegetables",
297 ec_node_many("vegetables",
299 ec_node_str(NULL, "potatoes"),
301 ec_node_str(NULL, "carrots")),
303 ec_node_str(NULL, "pumpkins"))),
307 ec_keyval_set(ec_node_attrs(cmd), "help",
308 "eat vegetables (take some more potatoes)", NULL);
309 if (ec_node_or_add(cmdlist, cmd) < 0)
313 cmd = EC_NODE_SEQ(NULL,
314 ec_node_str(NULL, "bye")
316 ec_keyval_set(ec_node_attrs(cmd), "help", "say bye", NULL);
317 if (ec_node_or_add(cmdlist, cmd) < 0)
321 cmd = EC_NODE_SEQ(NULL,
322 ec_node_str(NULL, "load"),
323 ec_node("file", NULL)
325 ec_keyval_set(ec_node_attrs(cmd), "help", "load a file", NULL);
326 if (ec_node_or_add(cmdlist, cmd) < 0)
330 commands = ec_node_sh_lex(NULL, cmdlist);
331 if (commands == NULL)
337 fprintf(stderr, "cannot initialize nodes\n");
338 ec_node_free(cmdlist);
347 if (create_commands() < 0)
350 rl_bind_key('?', show_help);
351 rl_attempted_completion_function = my_attempted_completion;
354 line = readline("> ");
358 p = ec_node_parse(commands, line);
359 ec_parsed_dump(stdout, p);
365 ec_node_free(commands);