4601ae8ca55617e5c37826f2354450fa877b612f
[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         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_free(c);
209
210         rl_display_match_list(helps, count + match, 1000); /* XXX 1000 */
211
212         rl_forced_update_display();
213
214         return 0;
215
216         // free helps[n] XXX on error ?
217 }
218
219 static int create_commands(void)
220 {
221         struct ec_node *cmdlist = NULL, *cmd = NULL;
222
223         cmdlist = ec_node("or", NULL);
224         if (cmdlist == NULL)
225                 goto fail;
226
227
228         cmd = EC_NODE_SEQ(NULL,
229                 ec_node_str(NULL, "hello"),
230                 EC_NODE_OR("name",
231                         ec_node_str("john", "john"),
232                         ec_node_str(NULL, "johnny"),
233                         ec_node_str(NULL, "mike")
234                 ),
235                 ec_node_option(NULL, ec_node_int("int", 0, 10, 10))
236         );
237         if (cmd == NULL)
238                 goto fail;
239         ec_keyval_set(ec_node_attrs(cmd), "help",
240                 "say hello to someone several times", NULL);
241         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "john")),
242                 "help", "specific help for john", NULL);
243         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
244                 "help", "the name of the person", NULL);
245         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
246                 "help", "an integer (0-10)", NULL);
247         if (ec_node_or_add(cmdlist, cmd) < 0)
248                 goto fail;
249
250
251         cmd = EC_NODE_CMD(NULL, "good morning name [count]",
252                         EC_NODE_CMD("name", "bob|bobby|michael"),
253                         ec_node_int("count", 0, 10, 10));
254         if (cmd == NULL)
255                 goto fail;
256         ec_keyval_set(ec_node_attrs(cmd), "help",
257                 "say good morning to someone several times", NULL);
258         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
259                 "the person to greet", NULL);
260         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
261                 "how many times to greet", NULL);
262         if (ec_node_or_add(cmdlist, cmd) < 0)
263                 goto fail;
264
265
266         cmd = EC_NODE_CMD(NULL,
267                         "buy potatoes,carrots,pumpkins");
268         if (cmd == NULL)
269                 goto fail;
270         ec_keyval_set(ec_node_attrs(cmd), "help",
271                 "buy some vegetables", NULL);
272         if (ec_node_or_add(cmdlist, cmd) < 0)
273                 goto fail;
274
275
276         cmd = EC_NODE_CMD(NULL, "eat vegetables",
277                         ec_node_many("vegetables",
278                                 EC_NODE_OR(NULL,
279                                         ec_node_str(NULL, "potatoes"),
280                                         ec_node_once(NULL,
281                                                 ec_node_str(NULL, "carrots")),
282                                         ec_node_once(NULL,
283                                                 ec_node_str(NULL, "pumpkins"))),
284                         1, 0));
285         if (cmd == NULL)
286                 goto fail;
287         ec_keyval_set(ec_node_attrs(cmd), "help",
288                 "eat vegetables (take some more potatoes)", NULL);
289         if (ec_node_or_add(cmdlist, cmd) < 0)
290                 goto fail;
291
292
293         cmd = EC_NODE_SEQ(NULL,
294                 ec_node_str(NULL, "bye")
295         );
296         ec_keyval_set(ec_node_attrs(cmd), "help", "say bye", NULL);
297         if (ec_node_or_add(cmdlist, cmd) < 0)
298                 goto fail;
299
300
301         cmd = EC_NODE_SEQ(NULL,
302                 ec_node_str(NULL, "load"),
303                 ec_node("file", NULL)
304         );
305         ec_keyval_set(ec_node_attrs(cmd), "help", "load a file", NULL);
306         if (ec_node_or_add(cmdlist, cmd) < 0)
307                 goto fail;
308
309
310         commands = ec_node_sh_lex(NULL, cmdlist);
311         if (commands == NULL)
312                 goto fail;
313
314         return 0;
315
316  fail:
317         fprintf(stderr, "cannot initialize nodes\n");
318         ec_node_free(cmdlist);
319         return -1;
320 }
321
322 int main(void)
323 {
324         struct ec_parsed *p;
325         char *line;
326
327         if (create_commands() < 0)
328                 return 1;
329
330         rl_bind_key('?', show_help);
331         rl_attempted_completion_function = my_attempted_completion;
332
333         while (1) {
334                 line = readline("> ");
335                 if (line == NULL)
336                         break;
337
338                 p = ec_node_parse(commands, line);
339                 ec_parsed_dump(stdout, p);
340                 add_history(line);
341                 ec_parsed_free(p);
342         }
343
344
345         ec_node_free(commands);
346         return 0;
347
348 }