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