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