6095808e662f01a230059a016901388e7699e9db
[protos/libecoli.git] / examples / readline / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #define _GNU_SOURCE /* for asprintf */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <assert.h>
10
11 #include <readline/readline.h>
12 #include <readline/history.h>
13
14 #include <ecoli_init.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_dict.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_seq.h>
21 #include <ecoli_node_space.h>
22 #include <ecoli_node_or.h>
23 #include <ecoli_node_sh_lex.h>
24 #include <ecoli_node_int.h>
25 #include <ecoli_node_option.h>
26 #include <ecoli_node_cmd.h>
27 #include <ecoli_node_many.h>
28 #include <ecoli_node_once.h>
29 #include <ecoli_node_file.h>
30
31 static struct ec_node *commands;
32
33 static char *my_completion_entry(const char *s, int state)
34 {
35         static struct ec_comp *c;
36         static struct ec_comp_iter *iter;
37         const struct ec_comp_item *item;
38         enum ec_comp_type item_type;
39         const char *item_str, *item_display;
40
41         (void)s;
42
43         /* Don't append a quote. Note: there are still some bugs when
44          * completing a quoted token. */
45         rl_completion_suppress_quote = 1;
46         rl_completer_quote_characters = "\"'";
47
48         if (state == 0) {
49                 char *line;
50
51                 ec_comp_free(c);
52                 line = strdup(rl_line_buffer);
53                 if (line == NULL)
54                         return NULL;
55                 line[rl_point] = '\0';
56
57                 c = ec_node_complete(commands, line);
58                 free(line);
59                 if (c == NULL)
60                         return NULL;
61
62                 ec_comp_iter_free(iter);
63                 iter = ec_comp_iter(c, EC_COMP_FULL | EC_COMP_PARTIAL);
64                 if (iter == NULL)
65                         return NULL;
66         }
67
68         item = ec_comp_iter_next(iter);
69         if (item == NULL)
70                 return NULL;
71
72         item_str = ec_comp_item_get_str(item);
73         if (ec_comp_count(c, EC_COMP_FULL) == 1) {
74
75                 /* don't add the trailing space for partial completions */
76                 if (state == 0) {
77                         item_type = ec_comp_item_get_type(item);
78                         if (item_type == EC_COMP_FULL)
79                                 rl_completion_suppress_append = 0;
80                         else
81                                 rl_completion_suppress_append = 1;
82                 }
83
84                 return strdup(item_str);
85         } else if (rl_completion_type == '?') {
86                 /* on second try only show the display part */
87                 item_display = ec_comp_item_get_display(item);
88                 return strdup(item_display);
89         }
90
91         return strdup(item_str);
92 }
93
94 static char **my_attempted_completion(const char *text, int start, int end)
95 {
96         (void)start;
97         (void)end;
98
99         /* remove default file completion */
100         rl_attempted_completion_over = 1;
101
102         return rl_completion_matches(text, my_completion_entry);
103 }
104
105 /* this function builds the help string */
106 static char *get_node_help(const struct ec_comp_item *item)
107 {
108         const struct ec_comp_group *grp;
109         const struct ec_parse *state;
110         const struct ec_node *node;
111         char *help = NULL;
112         const char *node_help = NULL;
113         const char *node_desc = NULL;
114
115         grp = ec_comp_item_get_grp(item);
116         for (state = ec_comp_group_get_state(grp); state != NULL;
117              state = ec_parse_get_parent(state)) {
118                 node = ec_parse_get_node(state);
119                 if (node_help == NULL)
120                         node_help = ec_dict_get(ec_node_attrs(node), "help");
121                 if (node_desc == NULL)
122                         node_desc = ec_node_desc(node);
123         }
124
125         if (node_help == NULL)
126                 node_help = "-";
127         if (node_desc == NULL)
128                 return NULL;
129
130         if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
131                 return NULL;
132
133         return help;
134 }
135
136 static int show_help(int ignore, int invoking_key)
137 {
138         struct ec_comp_iter *iter = NULL;
139         const struct ec_comp_group *grp, *prev_grp = NULL;
140         const struct ec_comp_item *item;
141         struct ec_comp *c = NULL;
142         struct ec_parse *p = NULL;
143         char *line = NULL;
144         unsigned int count;
145         char **helps = NULL;
146         int match = 0;
147         int cols;
148
149         (void)ignore;
150         (void)invoking_key;
151
152         line = strdup(rl_line_buffer);
153         if (line == NULL)
154                 goto fail;
155
156         /* check if the current line matches */
157         p = ec_node_parse(commands, line);
158         if (ec_parse_matches(p))
159                 match = 1;
160         ec_parse_free(p);
161         p = NULL;
162
163         /* complete at current cursor position */
164         line[rl_point] = '\0';
165         c = ec_node_complete(commands, line);
166         free(line);
167         line = NULL;
168         if (c == NULL)
169                 goto fail;
170
171         /* let's display one contextual help per node */
172         count = 0;
173         iter = ec_comp_iter(c,
174                 EC_COMP_UNKNOWN | EC_COMP_FULL | EC_COMP_PARTIAL);
175         if (iter == NULL)
176                 goto fail;
177
178         /* strangely, rl_display_match_list() expects first index at 1 */
179         helps = calloc(match + 1, sizeof(char *));
180         if (helps == NULL)
181                 goto fail;
182         if (match)
183                 helps[1] = "<return>";
184
185         while ((item = ec_comp_iter_next(iter)) != NULL) {
186                 char **tmp;
187
188                 /* keep one help per group, skip other items  */
189                 grp = ec_comp_item_get_grp(item);
190                 if (grp == prev_grp)
191                         continue;
192
193                 prev_grp = grp;
194
195                 tmp = realloc(helps, (count + match + 2) * sizeof(char *));
196                 if (tmp == NULL)
197                         goto fail;
198                 helps = tmp;
199                 helps[count + match + 1] = get_node_help(item);
200                 count++;
201         }
202
203         ec_comp_iter_free(iter);
204         ec_comp_free(c);
205         /* ensure not more than 1 entry per line */
206         rl_get_screen_size(NULL, &cols);
207         rl_display_match_list(helps, count + match, cols);
208         rl_forced_update_display();
209
210         return 0;
211
212 fail:
213         ec_comp_iter_free(iter);
214         ec_parse_free(p);
215         free(line);
216         ec_comp_free(c);
217         if (helps != NULL) {
218                 while (count--)
219                         free(helps[count + match + 1]);
220         }
221         free(helps);
222
223         return 1;
224 }
225
226 static int create_commands(void)
227 {
228         struct ec_node *cmdlist = NULL, *cmd = NULL;
229
230         cmdlist = ec_node("or", EC_NO_ID);
231         if (cmdlist == NULL)
232                 goto fail;
233
234
235         cmd = EC_NODE_SEQ(EC_NO_ID,
236                 ec_node_str(EC_NO_ID, "hello"),
237                 EC_NODE_OR("name",
238                         ec_node_str("john", "john"),
239                         ec_node_str(EC_NO_ID, "johnny"),
240                         ec_node_str(EC_NO_ID, "mike")
241                 ),
242                 ec_node_option(EC_NO_ID, ec_node_int("int", 0, 10, 10))
243         );
244         if (cmd == NULL)
245                 goto fail;
246         ec_dict_set(ec_node_attrs(cmd), "help",
247                 "say hello to someone several times", NULL);
248         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "john")),
249                 "help", "specific help for john", NULL);
250         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "name")),
251                 "help", "the name of the person", NULL);
252         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "int")),
253                 "help", "an integer (0-10)", NULL);
254         if (ec_node_or_add(cmdlist, cmd) < 0)
255                 goto fail;
256
257
258         cmd = EC_NODE_CMD(EC_NO_ID, "good morning name [count]",
259                         EC_NODE_CMD("name", "bob|bobby|michael"),
260                         ec_node_int("count", 0, 10, 10));
261         if (cmd == NULL)
262                 goto fail;
263         ec_dict_set(ec_node_attrs(cmd), "help",
264                 "say good morning to someone several times", NULL);
265         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
266                 "the person to greet", NULL);
267         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
268                 "how many times to greet (0-10)", NULL);
269         if (ec_node_or_add(cmdlist, cmd) < 0)
270                 goto fail;
271
272
273         cmd = EC_NODE_CMD(EC_NO_ID,
274                         "buy potatoes,carrots,pumpkins");
275         if (cmd == NULL)
276                 goto fail;
277         ec_dict_set(ec_node_attrs(cmd), "help",
278                 "buy some vegetables", NULL);
279         if (ec_node_or_add(cmdlist, cmd) < 0)
280                 goto fail;
281
282
283         cmd = EC_NODE_CMD(EC_NO_ID, "eat vegetables",
284                         ec_node_many("vegetables",
285                                 EC_NODE_OR(EC_NO_ID,
286                                         ec_node_str(EC_NO_ID, "potatoes"),
287                                         ec_node_once(EC_NO_ID,
288                                                 ec_node_str(EC_NO_ID, "carrots")),
289                                         ec_node_once(EC_NO_ID,
290                                                 ec_node_str(EC_NO_ID, "pumpkins"))),
291                         1, 0));
292         if (cmd == NULL)
293                 goto fail;
294         ec_dict_set(ec_node_attrs(cmd), "help",
295                 "eat vegetables (take some more potatoes)", NULL);
296         if (ec_node_or_add(cmdlist, cmd) < 0)
297                 goto fail;
298
299
300         cmd = EC_NODE_SEQ(EC_NO_ID,
301                 ec_node_str(EC_NO_ID, "bye")
302         );
303         ec_dict_set(ec_node_attrs(cmd), "help", "say bye", NULL);
304         if (ec_node_or_add(cmdlist, cmd) < 0)
305                 goto fail;
306
307
308         cmd = EC_NODE_SEQ(EC_NO_ID,
309                 ec_node_str(EC_NO_ID, "load"),
310                 ec_node("file", EC_NO_ID)
311         );
312         ec_dict_set(ec_node_attrs(cmd), "help", "load a file", NULL);
313         if (ec_node_or_add(cmdlist, cmd) < 0)
314                 goto fail;
315
316
317         commands = ec_node_sh_lex(EC_NO_ID, cmdlist);
318         if (commands == NULL)
319                 goto fail;
320
321         return 0;
322
323  fail:
324         fprintf(stderr, "cannot initialize nodes\n");
325         ec_node_free(cmdlist);
326         return -1;
327 }
328
329 int main(void)
330 {
331         struct ec_parse *p;
332         char *line;
333
334         if (ec_init() < 0) {
335                 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
336                 return 1;
337         }
338
339         if (create_commands() < 0)
340                 return 1;
341
342         rl_bind_key('?', show_help);
343         rl_attempted_completion_function = my_attempted_completion;
344
345         while (1) {
346                 line = readline("> ");
347                 if (line == NULL)
348                         break;
349
350                 p = ec_node_parse(commands, line);
351                 ec_parse_dump(stdout, p);
352                 add_history(line);
353                 ec_parse_free(p);
354         }
355
356
357         ec_node_free(commands);
358         return 0;
359
360 }