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 <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 */
67         rl_completion_suppress_quote = 1;
68         rl_basic_quote_characters = "";
69
70         if (state == 0) {
71                 char *line;
72
73                 ec_completed_free(c);
74
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_MATCH | EC_PARTIAL_MATCH);
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_match == 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_MATCH)
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_node *node;
132         char *help = NULL;
133         const char *node_help = NULL;
134         const char *node_desc = NULL;
135 //      size_t i;
136
137         (void)item;
138 #if 0 //XXX
139         /* Retrieve the most precise help for this node. */
140         for (i = 0; i < item->pathlen; i++) {
141                 node = item->path[i];
142                 if (node_help == NULL)
143                         node_help = ec_keyval_get(ec_node_attrs(node), "help");
144                 if (node_desc == NULL)
145                         node_desc = ec_node_desc(node);
146         }
147 #else
148         node = ec_completed_item_get_node(item);
149         node_help = ec_keyval_get(ec_node_attrs(node), "help");
150         node_desc = ec_node_desc(node);
151 #endif
152
153         if (node_help == NULL)
154                 node_help = "-";
155         if (node_desc == NULL)
156                 return NULL;
157
158         if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
159                 return NULL;
160
161         return help;
162 }
163
164 static int show_help(int ignore, int invoking_key)
165 {
166         struct ec_completed_iter *iter;
167         const struct ec_completed_item *item;
168         const struct ec_node *prev_node = NULL;
169         struct ec_completed *c;
170         struct ec_parsed *p;
171         char *line = NULL;
172         unsigned int count;
173         char **helps = NULL;
174         int match = 0;
175
176         (void)ignore;
177         (void)invoking_key;
178
179         line = strdup(rl_line_buffer);
180         if (line == NULL)
181                 goto fail;
182
183         /* check if the current line matches */
184         p = ec_node_parse(commands, line);
185         if (ec_parsed_matches(p))
186                 match = 1;
187         ec_parsed_free(p);
188         p = NULL;
189
190         /* complete at current cursor position */
191         line[rl_point] = '\0';
192         c = ec_node_complete(commands, line);
193         free(line);
194         line = NULL;
195         if (c == NULL)
196                 goto fail;
197
198         //ec_completed_dump(stdout, c);
199
200         /* let's display one contextual help per node */
201         count = 0;
202         iter = ec_completed_iter(c, EC_NO_MATCH | EC_MATCH | EC_PARTIAL_MATCH);
203         if (iter == NULL)
204                 goto fail;
205
206         /* strangely, rl_display_match_list() expects first index at 1 */
207         helps = calloc(match + 1, sizeof(char *));
208         if (helps == NULL)
209                 goto fail;
210         if (match)
211                 helps[1] = "<return>";
212
213         while ((item = ec_completed_iter_next(iter)) != NULL) {
214                 const struct ec_node *node;
215                 char **tmp;
216
217                 /* keep one help per node, skip other items  */
218                 node = ec_completed_item_get_node(item);
219                 if (node == prev_node)
220                         continue;
221
222                 tmp = realloc(helps, (count + match + 2) * sizeof(char *));
223                 if (tmp == NULL)
224                         goto fail;
225                 helps = tmp;
226                 helps[count + match + 1] = get_node_help(item);
227                 count++;
228         }
229
230         ec_completed_iter_free(iter);
231         ec_completed_free(c);
232         rl_display_match_list(helps, count + match, 1000); /* XXX 1000 */
233         rl_forced_update_display();
234
235         return 0;
236
237 fail:
238         ec_completed_iter_free(iter);
239         ec_parsed_free(p);
240         free(line);
241         ec_completed_free(c);
242         if (helps != NULL) {
243                 while (count--)
244                         free(helps[count + match + 1]);
245         }
246         free(helps);
247
248         return 1;
249 }
250
251 static int create_commands(void)
252 {
253         struct ec_node *cmdlist = NULL, *cmd = NULL;
254
255         cmdlist = ec_node("or", NULL);
256         if (cmdlist == NULL)
257                 goto fail;
258
259
260         cmd = EC_NODE_SEQ(NULL,
261                 ec_node_str(NULL, "hello"),
262                 EC_NODE_OR("name",
263                         ec_node_str("john", "john"),
264                         ec_node_str(NULL, "johnny"),
265                         ec_node_str(NULL, "mike")
266                 ),
267                 ec_node_option(NULL, ec_node_int("int", 0, 10, 10))
268         );
269         if (cmd == NULL)
270                 goto fail;
271         ec_keyval_set(ec_node_attrs(cmd), "help",
272                 "say hello to someone several times", NULL);
273         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "john")),
274                 "help", "specific help for john", NULL);
275         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
276                 "help", "the name of the person", NULL);
277         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
278                 "help", "an integer (0-10)", NULL);
279         if (ec_node_or_add(cmdlist, cmd) < 0)
280                 goto fail;
281
282
283         cmd = EC_NODE_CMD(NULL, "good morning name [count]",
284                         EC_NODE_CMD("name", "bob|bobby|michael"),
285                         ec_node_int("count", 0, 10, 10));
286         if (cmd == NULL)
287                 goto fail;
288         ec_keyval_set(ec_node_attrs(cmd), "help",
289                 "say good morning to someone several times", NULL);
290         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
291                 "the person to greet", NULL);
292         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
293                 "how many times to greet (0-10)", NULL);
294         if (ec_node_or_add(cmdlist, cmd) < 0)
295                 goto fail;
296
297
298         cmd = EC_NODE_CMD(NULL,
299                         "buy potatoes,carrots,pumpkins");
300         if (cmd == NULL)
301                 goto fail;
302         ec_keyval_set(ec_node_attrs(cmd), "help",
303                 "buy some vegetables", NULL);
304         if (ec_node_or_add(cmdlist, cmd) < 0)
305                 goto fail;
306
307
308         cmd = EC_NODE_CMD(NULL, "eat vegetables",
309                         ec_node_many("vegetables",
310                                 EC_NODE_OR(NULL,
311                                         ec_node_str(NULL, "potatoes"),
312                                         ec_node_once(NULL,
313                                                 ec_node_str(NULL, "carrots")),
314                                         ec_node_once(NULL,
315                                                 ec_node_str(NULL, "pumpkins"))),
316                         1, 0));
317         if (cmd == NULL)
318                 goto fail;
319         ec_keyval_set(ec_node_attrs(cmd), "help",
320                 "eat vegetables (take some more potatoes)", NULL);
321         if (ec_node_or_add(cmdlist, cmd) < 0)
322                 goto fail;
323
324
325         cmd = EC_NODE_SEQ(NULL,
326                 ec_node_str(NULL, "bye")
327         );
328         ec_keyval_set(ec_node_attrs(cmd), "help", "say bye", NULL);
329         if (ec_node_or_add(cmdlist, cmd) < 0)
330                 goto fail;
331
332
333         cmd = EC_NODE_SEQ(NULL,
334                 ec_node_str(NULL, "load"),
335                 ec_node("file", NULL)
336         );
337         ec_keyval_set(ec_node_attrs(cmd), "help", "load a file", NULL);
338         if (ec_node_or_add(cmdlist, cmd) < 0)
339                 goto fail;
340
341
342         commands = ec_node_sh_lex(NULL, cmdlist);
343         if (commands == NULL)
344                 goto fail;
345
346         return 0;
347
348  fail:
349         fprintf(stderr, "cannot initialize nodes\n");
350         ec_node_free(cmdlist);
351         return -1;
352 }
353
354 int main(void)
355 {
356         struct ec_parsed *p;
357         char *line;
358
359         if (ec_init() < 0) {
360                 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
361                 return 1;
362         }
363
364         if (create_commands() < 0)
365                 return 1;
366
367         rl_bind_key('?', show_help);
368         rl_attempted_completion_function = my_attempted_completion;
369
370         while (1) {
371                 line = readline("> ");
372                 if (line == NULL)
373                         break;
374
375                 p = ec_node_parse(commands, line);
376                 ec_parsed_dump(stdout, p);
377                 add_history(line);
378                 ec_parsed_free(p);
379         }
380
381
382         ec_node_free(commands);
383         return 0;
384
385 }