a8a58409906257c458753f220989254ed7948f9c
[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         enum ec_completed_type item_type;
62         const char *item_str, *item_display;
63
64         (void)s;
65
66         /* Don't append a quote. Note: there are still some bugs when
67          * completing a quoted token. */
68         rl_completion_suppress_quote = 1;
69         rl_completer_quote_characters = "\"'";
70
71         if (state == 0) {
72                 char *line;
73
74                 ec_completed_free(c);
75                 line = strdup(rl_line_buffer);
76                 if (line == NULL)
77                         return NULL;
78                 line[rl_point] = '\0';
79
80                 c = ec_node_complete(commands, line);
81                 free(line);
82                 if (c == NULL)
83                         return NULL;
84
85                 ec_completed_iter_free(iter);
86                 iter = ec_completed_iter(c, EC_COMP_FULL | EC_COMP_PARTIAL);
87                 if (iter == NULL)
88                         return NULL;
89         }
90
91         item = ec_completed_iter_next(iter);
92         if (item == NULL)
93                 return NULL;
94
95         item_str = ec_completed_item_get_str(item);
96         if (c->count_full == 1) {
97
98                 /* don't add the trailing space for partial completions */
99                 if (state == 0) {
100                         item_type = ec_completed_item_get_type(item);
101                         if (item_type == EC_COMP_FULL)
102                                 rl_completion_suppress_append = 0;
103                         else
104                                 rl_completion_suppress_append = 1;
105                 }
106
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);
112         }
113
114         return strdup(item_str);
115 }
116
117 static char **my_attempted_completion(const char *text, int start, int end)
118 {
119         (void)start;
120         (void)end;
121
122         /* remove default file completion */
123         rl_attempted_completion_over = 1;
124
125         return rl_completion_matches(text, my_completion_entry);
126 }
127
128 /* this function builds the help string */
129 static char *get_node_help(const struct ec_completed_item *item)
130 {
131         const struct ec_completed_group *grp;
132         struct ec_parsed *state; // XXX keep const with macro
133         const struct ec_node *node;
134         char *help = NULL;
135         const char *node_help = NULL;
136         const char *node_desc = NULL;
137
138         grp = ec_completed_item_get_grp(item);
139         state = grp->state;
140         ec_parsed_dump(stdout, ec_parsed_get_root(state));
141         for (state = grp->state; state != NULL;
142              state = ec_parsed_get_parent(state)) {
143                 node = ec_parsed_get_node(state);
144                 if (node_help == NULL)
145                         node_help = ec_keyval_get(ec_node_attrs(node), "help");
146                 if (node_desc == NULL)
147                         node_desc = ec_node_desc(node);
148         }
149
150         if (node_help == NULL)
151                 node_help = "-";
152         if (node_desc == NULL)
153                 return NULL;
154
155         if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
156                 return NULL;
157
158         return help;
159 }
160
161 static int show_help(int ignore, int invoking_key)
162 {
163         struct ec_completed_iter *iter;
164         const struct ec_completed_group *grp, *prev_grp = NULL;
165         const struct ec_completed_item *item;
166         struct ec_completed *c;
167         struct ec_parsed *p;
168         char *line = NULL;
169         unsigned int count;
170         char **helps = NULL;
171         int match = 0;
172
173         (void)ignore;
174         (void)invoking_key;
175
176         line = strdup(rl_line_buffer);
177         if (line == NULL)
178                 goto fail;
179
180         /* check if the current line matches */
181         p = ec_node_parse(commands, line);
182         if (ec_parsed_matches(p))
183                 match = 1;
184         ec_parsed_free(p);
185         p = NULL;
186
187         /* complete at current cursor position */
188         line[rl_point] = '\0';
189         c = ec_node_complete(commands, line);
190         free(line);
191         line = NULL;
192         if (c == NULL)
193                 goto fail;
194
195         ec_completed_dump(stdout, c);
196
197         /* let's display one contextual help per node */
198         count = 0;
199         iter = ec_completed_iter(c,
200                 EC_COMP_UNKNOWN | EC_COMP_FULL | EC_COMP_PARTIAL);
201         if (iter == NULL)
202                 goto fail;
203
204         /* strangely, rl_display_match_list() expects first index at 1 */
205         helps = calloc(match + 1, sizeof(char *));
206         if (helps == NULL)
207                 goto fail;
208         if (match)
209                 helps[1] = "<return>";
210
211         while ((item = ec_completed_iter_next(iter)) != NULL) {
212                 char **tmp;
213
214                 /* keep one help per group, skip other items  */
215                 grp = ec_completed_item_get_grp(item);
216                 if (grp == prev_grp)
217                         continue;
218
219                 prev_grp = grp;
220
221                 tmp = realloc(helps, (count + match + 2) * sizeof(char *));
222                 if (tmp == NULL)
223                         goto fail;
224                 helps = tmp;
225                 helps[count + match + 1] = get_node_help(item);
226                 count++;
227         }
228
229         ec_completed_iter_free(iter);
230         ec_completed_free(c);
231         rl_display_match_list(helps, count + match, 1000); /* XXX 1000 */
232         rl_forced_update_display();
233
234         return 0;
235
236 fail:
237         ec_completed_iter_free(iter);
238         ec_parsed_free(p);
239         free(line);
240         ec_completed_free(c);
241         if (helps != NULL) {
242                 while (count--)
243                         free(helps[count + match + 1]);
244         }
245         free(helps);
246
247         return 1;
248 }
249
250 static int create_commands(void)
251 {
252         struct ec_node *cmdlist = NULL, *cmd = NULL;
253
254         cmdlist = ec_node("or", EC_NO_ID);
255         if (cmdlist == NULL)
256                 goto fail;
257
258
259         cmd = EC_NODE_SEQ(EC_NO_ID,
260                 ec_node_str(EC_NO_ID, "hello"),
261                 EC_NODE_OR("name",
262                         ec_node_str("john", "john"),
263                         ec_node_str(EC_NO_ID, "johnny"),
264                         ec_node_str(EC_NO_ID, "mike")
265                 ),
266                 ec_node_option(EC_NO_ID, ec_node_int("int", 0, 10, 10))
267         );
268         if (cmd == NULL)
269                 goto fail;
270         ec_keyval_set(ec_node_attrs(cmd), "help",
271                 "say hello to someone several times", NULL);
272         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "john")),
273                 "help", "specific help for john", NULL);
274         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
275                 "help", "the name of the person", NULL);
276         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
277                 "help", "an integer (0-10)", NULL);
278         if (ec_node_or_add(cmdlist, cmd) < 0)
279                 goto fail;
280
281
282         cmd = EC_NODE_CMD(EC_NO_ID, "good morning name [count]",
283                         EC_NODE_CMD("name", "bob|bobby|michael"),
284                         ec_node_int("count", 0, 10, 10));
285         if (cmd == NULL)
286                 goto fail;
287         ec_keyval_set(ec_node_attrs(cmd), "help",
288                 "say good morning to someone several times", NULL);
289         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
290                 "the person to greet", NULL);
291         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
292                 "how many times to greet (0-10)", NULL);
293         if (ec_node_or_add(cmdlist, cmd) < 0)
294                 goto fail;
295
296
297         cmd = EC_NODE_CMD(EC_NO_ID,
298                         "buy potatoes,carrots,pumpkins");
299         if (cmd == NULL)
300                 goto fail;
301         ec_keyval_set(ec_node_attrs(cmd), "help",
302                 "buy some vegetables", NULL);
303         if (ec_node_or_add(cmdlist, cmd) < 0)
304                 goto fail;
305
306
307         cmd = EC_NODE_CMD(EC_NO_ID, "eat vegetables",
308                         ec_node_many("vegetables",
309                                 EC_NODE_OR(EC_NO_ID,
310                                         ec_node_str(EC_NO_ID, "potatoes"),
311                                         ec_node_once(EC_NO_ID,
312                                                 ec_node_str(EC_NO_ID, "carrots")),
313                                         ec_node_once(EC_NO_ID,
314                                                 ec_node_str(EC_NO_ID, "pumpkins"))),
315                         1, 0));
316         if (cmd == NULL)
317                 goto fail;
318         ec_keyval_set(ec_node_attrs(cmd), "help",
319                 "eat vegetables (take some more potatoes)", NULL);
320         if (ec_node_or_add(cmdlist, cmd) < 0)
321                 goto fail;
322
323
324         cmd = EC_NODE_SEQ(EC_NO_ID,
325                 ec_node_str(EC_NO_ID, "bye")
326         );
327         ec_keyval_set(ec_node_attrs(cmd), "help", "say bye", NULL);
328         if (ec_node_or_add(cmdlist, cmd) < 0)
329                 goto fail;
330
331
332         cmd = EC_NODE_SEQ(EC_NO_ID,
333                 ec_node_str(EC_NO_ID, "load"),
334                 ec_node("file", EC_NO_ID)
335         );
336         ec_keyval_set(ec_node_attrs(cmd), "help", "load a file", NULL);
337         if (ec_node_or_add(cmdlist, cmd) < 0)
338                 goto fail;
339
340
341         commands = ec_node_sh_lex(EC_NO_ID, cmdlist);
342         if (commands == NULL)
343                 goto fail;
344
345         return 0;
346
347  fail:
348         fprintf(stderr, "cannot initialize nodes\n");
349         ec_node_free(cmdlist);
350         return -1;
351 }
352
353 int main(void)
354 {
355         struct ec_parsed *p;
356         char *line;
357
358         if (ec_init() < 0) {
359                 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
360                 return 1;
361         }
362
363         if (create_commands() < 0)
364                 return 1;
365
366         rl_bind_key('?', show_help);
367         rl_attempted_completion_function = my_attempted_completion;
368
369         while (1) {
370                 line = readline("> ");
371                 if (line == NULL)
372                         break;
373
374                 p = ec_node_parse(commands, line);
375                 ec_parsed_dump(stdout, p);
376                 add_history(line);
377                 ec_parsed_free(p);
378         }
379
380
381         ec_node_free(commands);
382         return 0;
383
384 }