save
[protos/libecoli.git] / lib / main-readline.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #define _GNU_SOURCE /* for asprintf */
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <assert.h>
32
33 #include <readline/readline.h>
34 #include <readline/history.h>
35
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>
51
52 static struct ec_node *commands;
53
54 static char *my_completion_entry(const char *s, int state)
55 {
56         static struct ec_completed *c;
57         static struct ec_completed_iter *iter;
58         static const struct ec_completed_item *item;
59
60         (void)s;
61
62         /* don't append a quote */
63         rl_completion_suppress_quote = 1;
64         rl_basic_quote_characters = "";
65
66         if (state == 0) {
67                 char *line;
68
69                 ec_completed_free(c);
70
71                 line = strdup(rl_line_buffer);
72                 if (line == NULL)
73                         return NULL;
74                 line[rl_point] = '\0';
75
76                 c = ec_node_complete(commands, line);
77                 free(line);
78                 if (c == NULL)
79                         return NULL;
80
81                 ec_completed_iter_free(iter);
82                 iter = ec_completed_iter(c, EC_MATCH | EC_PARTIAL_MATCH);
83                 if (iter == NULL)
84                         return NULL;
85         }
86
87         item = ec_completed_iter_next(iter);
88         if (item == NULL)
89                 return NULL;
90
91         if (c->count_match == 1) {
92
93                 /* don't add the trailing space for partial completions */
94                 if (state == 0) {
95                         if (item->type == EC_MATCH)
96                                 rl_completion_suppress_append = 0;
97                         else
98                                 rl_completion_suppress_append = 1;
99                 }
100
101                 return strdup(item->str);
102         } else if (rl_completion_type == '?') {
103                 /* on second try only show the display part */
104                 return strdup(item->display);
105         }
106
107         return strdup(item->str);
108 }
109
110 static char **my_attempted_completion(const char *text, int start, int end)
111 {
112         (void)start;
113         (void)end;
114
115         /* remove default file completion */
116         rl_attempted_completion_over = 1;
117
118         return rl_completion_matches(text, my_completion_entry);
119 }
120
121 /* this function builds the help string */
122 static char *get_node_help(const struct ec_completed_node *compnode)
123 {
124         const struct ec_completed_item *item;
125         const struct ec_node *node;
126         char *help = NULL;
127         const char *node_help = NULL;
128         const char *node_desc = NULL;
129         size_t i;
130
131         /* Since we display only one help per node, only look at the first item
132          * to get the path. The objective is to retrieve the most precise
133          * help for this node. */
134         item = TAILQ_FIRST(&compnode->items);
135         for (i = 0; i < item->pathlen; i++) {
136                 node = item->path[i];
137                 if (node_help == NULL)
138                         node_help = ec_keyval_get(ec_node_attrs(node), "help");
139                 if (node_desc == NULL)
140                         node_desc = ec_node_desc(node);
141         }
142
143         if (node_help == NULL)
144                 node_help = "";
145         if (node_desc == NULL)
146                 return NULL;
147
148         if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
149                 return NULL;
150
151         return help;
152 }
153
154 static int show_help(int ignore, int invoking_key)
155 {
156         const struct ec_completed_node *compnode;
157         struct ec_completed *c;
158         struct ec_parsed *p;
159         char *line;
160         unsigned int count, i;
161         char **helps = NULL;
162         int match = 0;
163
164         (void)ignore;
165         (void)invoking_key;
166
167         line = strdup(rl_line_buffer);
168         if (line == NULL)
169                 return 1;
170
171         /* check if the current line matches */
172         p = ec_node_parse(commands, line);
173         if (ec_parsed_matches(p))
174                 match = 1;
175         ec_parsed_free(p);
176
177         /* complete at current cursor position */
178         line[rl_point] = '\0';
179         c = ec_node_complete(commands, line);
180         //ec_completed_dump(stdout, c);
181         free(line);
182         if (c == NULL)
183                 return 1;
184
185         /* let's display one contextual help per node */
186         count = 0;
187         TAILQ_FOREACH(compnode, &c->nodes, next) {
188                 if (TAILQ_EMPTY(&compnode->items))
189                         continue;
190                 count++;
191         }
192
193         helps = calloc(count + match + 1, sizeof(char *));
194         if (helps == NULL)
195                 return 1;
196
197         if (match)
198                 helps[1] = "<return>";
199
200         /* strangely, rl_display_match_list() expects first index at 1 */
201         i = match + 1;
202         TAILQ_FOREACH(compnode, &c->nodes, next) {
203                 if (TAILQ_EMPTY(&compnode->items))
204                         continue;
205                 helps[i++] = get_node_help(compnode);
206         }
207
208         ec_completed_dump(stdout, c);
209         ec_completed_free(c);
210
211         rl_display_match_list(helps, count + match, 1000); /* XXX 1000 */
212
213         rl_forced_update_display();
214
215         return 0;
216
217         // free helps[n] XXX on error ?
218 }
219
220 static int create_commands(void)
221 {
222         struct ec_node *cmdlist = NULL, *cmd = NULL;
223
224         cmdlist = ec_node("or", NULL);
225         if (cmdlist == NULL)
226                 goto fail;
227
228
229         cmd = EC_NODE_SEQ(NULL,
230                 ec_node_str(NULL, "hello"),
231                 EC_NODE_OR("name",
232                         ec_node_str("john", "john"),
233                         ec_node_str(NULL, "johnny"),
234                         ec_node_str(NULL, "mike")
235                 ),
236                 ec_node_option(NULL, ec_node_int("int", 0, 10, 10))
237         );
238         if (cmd == NULL)
239                 goto fail;
240         ec_keyval_set(ec_node_attrs(cmd), "help",
241                 "say hello to someone several times", NULL);
242         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "john")),
243                 "help", "specific help for john", NULL);
244         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
245                 "help", "the name of the person", NULL);
246         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
247                 "help", "an integer (0-10)", NULL);
248         if (ec_node_or_add(cmdlist, cmd) < 0)
249                 goto fail;
250
251
252         cmd = EC_NODE_CMD(NULL, "good morning name [count]",
253                         EC_NODE_CMD("name", "bob|bobby|michael"),
254                         ec_node_int("count", 0, 10, 10));
255         if (cmd == NULL)
256                 goto fail;
257         ec_keyval_set(ec_node_attrs(cmd), "help",
258                 "say good morning to someone several times", NULL);
259         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
260                 "the person to greet", NULL);
261         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
262                 "how many times to greet", NULL);
263         if (ec_node_or_add(cmdlist, cmd) < 0)
264                 goto fail;
265
266
267         cmd = EC_NODE_CMD(NULL,
268                         "buy potatoes,carrots,pumpkins");
269         if (cmd == NULL)
270                 goto fail;
271         ec_keyval_set(ec_node_attrs(cmd), "help",
272                 "buy some vegetables", NULL);
273         if (ec_node_or_add(cmdlist, cmd) < 0)
274                 goto fail;
275
276
277         cmd = EC_NODE_CMD(NULL, "eat vegetables",
278                         ec_node_many("vegetables",
279                                 EC_NODE_OR(NULL,
280                                         ec_node_str(NULL, "potatoes"),
281                                         ec_node_once(NULL,
282                                                 ec_node_str(NULL, "carrots")),
283                                         ec_node_once(NULL,
284                                                 ec_node_str(NULL, "pumpkins"))),
285                         1, 0));
286         if (cmd == NULL)
287                 goto fail;
288         ec_keyval_set(ec_node_attrs(cmd), "help",
289                 "eat vegetables (take some more potatoes)", NULL);
290         if (ec_node_or_add(cmdlist, cmd) < 0)
291                 goto fail;
292
293
294         cmd = EC_NODE_SEQ(NULL,
295                 ec_node_str(NULL, "bye")
296         );
297         ec_keyval_set(ec_node_attrs(cmd), "help", "say bye", NULL);
298         if (ec_node_or_add(cmdlist, cmd) < 0)
299                 goto fail;
300
301
302         cmd = EC_NODE_SEQ(NULL,
303                 ec_node_str(NULL, "load"),
304                 ec_node("file", NULL)
305         );
306         ec_keyval_set(ec_node_attrs(cmd), "help", "load a file", NULL);
307         if (ec_node_or_add(cmdlist, cmd) < 0)
308                 goto fail;
309
310
311         commands = ec_node_sh_lex(NULL, cmdlist);
312         if (commands == NULL)
313                 goto fail;
314
315         return 0;
316
317  fail:
318         fprintf(stderr, "cannot initialize nodes\n");
319         ec_node_free(cmdlist);
320         return -1;
321 }
322
323 int main(void)
324 {
325         struct ec_parsed *p;
326         char *line;
327
328         if (create_commands() < 0)
329                 return 1;
330
331         rl_bind_key('?', show_help);
332         rl_attempted_completion_function = my_attempted_completion;
333
334         while (1) {
335                 line = readline("> ");
336                 if (line == NULL)
337                         break;
338
339                 p = ec_node_parse(commands, line);
340                 ec_parsed_dump(stdout, p);
341                 add_history(line);
342                 ec_parsed_free(p);
343         }
344
345
346         ec_node_free(commands);
347         return 0;
348
349 }