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