pass state to completed api
[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
51 static struct ec_node *commands;
52
53 static char *my_completion_entry(const char *s, int state)
54 {
55         static struct ec_completed *c;
56         static struct ec_completed_iter *iter;
57         static const struct ec_completed_elt *elt;
58         char *out_string;
59
60
61         if (state == 0) {
62                 char *line;
63
64                 ec_completed_free(c);
65
66                 line = strdup(rl_line_buffer);
67                 if (line == NULL)
68                         return NULL;
69                 line[rl_point] = '\0';
70
71                 c = ec_node_complete(commands, line);
72                 free(line);
73                 if (c == NULL)
74                         return NULL;
75
76                 ec_completed_iter_free(iter);
77                 iter = ec_completed_iter(c, EC_MATCH);
78                 if (iter == NULL)
79                         return NULL;
80         }
81
82         elt = ec_completed_iter_next(iter);
83         if (elt == NULL)
84                 return NULL;
85
86         if (asprintf(&out_string, "%s%s", s, elt->add) < 0)
87                 return NULL;
88
89         return out_string;
90 }
91
92 static char **my_attempted_completion(const char *text, int start, int end)
93 {
94         (void)start;
95         (void)end;
96
97         /* remove default file completion */
98         rl_attempted_completion_over = 1;
99
100         return rl_completion_matches(text, my_completion_entry);
101 }
102
103 /* this function builds the help string */
104 static char *get_tk_help(const struct ec_node *node)
105 {
106         const struct ec_node *node2;
107         char *help = NULL;
108         char *tk_help = NULL;
109
110         for (node2 = node;
111              node2 != NULL && tk_help == NULL;
112              node2 = ec_node_parent(node2))
113                 tk_help = ec_keyval_get(ec_node_attrs(node2), "help");
114
115         if (tk_help == NULL)
116                 tk_help = "";
117
118         if (asprintf(&help, "%-20s %s", ec_node_desc(node), tk_help) < 0)
119                 return NULL;
120
121         return help;
122 }
123
124 static int show_help(int ignore, int invoking_key)
125 {
126         const struct ec_completed_elt *elt;
127         struct ec_completed_iter *iter;
128         struct ec_completed *c;
129         struct ec_parsed *p;
130         char *line;
131         unsigned int count, i;
132         char **helps = NULL;
133         int match = 0;
134
135         (void)ignore;
136         (void)invoking_key;
137
138         line = strdup(rl_line_buffer);
139         if (line == NULL)
140                 return 1;
141
142         /* check if the current line matches */
143         p = ec_node_parse(commands, line);
144         if (ec_parsed_matches(p))
145                 match = 1;
146         ec_parsed_free(p);
147
148         /* complete at current cursor position */
149         line[rl_point] = '\0';
150         c = ec_node_complete(commands, line);
151         //ec_completed_dump(stdout, c);
152         free(line);
153         if (c == NULL)
154                 return 1;
155
156         count = ec_completed_count(c, EC_MATCH | EC_NO_MATCH);
157         helps = calloc(count + match + 1, sizeof(char *));
158         if (helps == NULL)
159                 return 1;
160
161         if (match)
162                 helps[1] = "<return>";
163
164         iter = ec_completed_iter(c, EC_MATCH | EC_NO_MATCH);
165         if (iter == NULL)
166                 goto fail;
167
168         /* strangely, rl_display_match_list() expects first index at 1 */
169         for (i = match + 1, elt = ec_completed_iter_next(iter);
170              i < count + match + 1 && elt != NULL;
171              i++, elt = ec_completed_iter_next(iter)) {
172                 helps[i] = get_tk_help(elt->node);
173         }
174
175         ec_completed_free(c);
176
177         rl_display_match_list(helps, count + match, 1000); /* XXX 1000 */
178
179         rl_forced_update_display();
180
181         return 0;
182
183 fail:
184         free(helps);
185         // free helps[n] XXX
186         return 1;
187 }
188
189 static int create_commands(void)
190 {
191         struct ec_node *cmdlist = NULL, *cmd = NULL;
192
193         cmdlist = ec_node("or", NULL);
194         if (cmdlist == NULL)
195                 goto fail;
196
197
198         cmd = EC_NODE_SEQ(NULL,
199                 ec_node_str(NULL, "hello"),
200                 EC_NODE_OR("name",
201                         ec_node_str(NULL, "john"),
202                         ec_node_str(NULL, "johnny"),
203                         ec_node_str(NULL, "mike")
204                 ),
205                 ec_node_option(NULL, ec_node_int("int", 0, 10, 10))
206         );
207         if (cmd == NULL)
208                 goto fail;
209         ec_keyval_set(ec_node_attrs(cmd), "help",
210                 "say hello to someone several times", NULL);
211         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
212                 "help", "the name of the person", NULL);
213         ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
214                 "help", "an integer", NULL);
215         if (ec_node_or_add(cmdlist, cmd) < 0)
216                 goto fail;
217
218
219         cmd = EC_NODE_CMD(NULL, "good morning bob|bobby|michael [count]",
220                         ec_node_int("count", 0, 10, 10));
221         if (cmd == NULL)
222                 goto fail;
223         ec_keyval_set(ec_node_attrs(cmd), "help",
224                 "say good morning to someone several times", NULL);
225         if (ec_node_or_add(cmdlist, cmd) < 0)
226                 goto fail;
227
228
229         cmd = EC_NODE_CMD(NULL,
230                         "buy potatoes,carrots,pumpkins");
231         if (cmd == NULL)
232                 goto fail;
233         ec_keyval_set(ec_node_attrs(cmd), "help",
234                 "buy some vegetables", NULL);
235         if (ec_node_or_add(cmdlist, cmd) < 0)
236                 goto fail;
237
238
239         cmd = EC_NODE_CMD(NULL, "sell vegetable",
240                         ec_node_many("vegetable",
241                                 EC_NODE_OR(NULL,
242                                         ec_node_once(NULL,
243                                                 ec_node_str(NULL, "potatoes")),
244                                         ec_node_once(NULL,
245                                                 ec_node_str(NULL, "carrots")),
246                                         ec_node_once(NULL,
247                                                 ec_node_str(NULL, "pumpkins"))),
248                         1, 0));
249         if (cmd == NULL)
250                 goto fail;
251         ec_keyval_set(ec_node_attrs(cmd), "help",
252                 "sell vegetables", NULL);
253         if (ec_node_or_add(cmdlist, cmd) < 0)
254                 goto fail;
255
256
257         cmd = EC_NODE_SEQ(NULL,
258                 ec_node_str(NULL, "bye")
259         );
260         ec_keyval_set(ec_node_attrs(cmd), "help", "say bye to someone", NULL);
261         if (ec_node_or_add(cmdlist, cmd) < 0)
262                 goto fail;
263
264
265         commands = ec_node_sh_lex(NULL, cmdlist);
266         if (commands == NULL)
267                 goto fail;
268
269         return 0;
270
271  fail:
272         fprintf(stderr, "cannot initialize nodes\n");
273         ec_node_free(cmdlist);
274         return -1;
275 }
276
277 int main(void)
278 {
279         struct ec_parsed *p;
280 //      const char *name;
281         char *line;
282
283
284         if (create_commands() < 0)
285                 return 1;
286
287         rl_bind_key('?', show_help);
288         rl_attempted_completion_function = my_attempted_completion;
289
290         while (1) {
291                 line = readline("> ");
292                 if (line == NULL)
293                         break;
294
295                 p = ec_node_parse(commands, line);
296                 ec_parsed_dump(stdout, p);
297                 add_history(line);
298                 ec_parsed_free(p);
299         }
300
301
302         ec_node_free(commands);
303         return 0;
304
305 }