duplicate and rename keyval in dict + htable
[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 (c->count_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         state = grp->state;
117         for (state = grp->state; state != NULL;
118              state = ec_parse_get_parent(state)) {
119                 node = ec_parse_get_node(state);
120                 if (node_help == NULL)
121                         node_help = ec_dict_get(ec_node_attrs(node), "help");
122                 if (node_desc == NULL)
123                         node_desc = ec_node_desc(node);
124         }
125
126         if (node_help == NULL)
127                 node_help = "-";
128         if (node_desc == NULL)
129                 return NULL;
130
131         if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
132                 return NULL;
133
134         return help;
135 }
136
137 static int show_help(int ignore, int invoking_key)
138 {
139         struct ec_comp_iter *iter = NULL;
140         const struct ec_comp_group *grp, *prev_grp = NULL;
141         const struct ec_comp_item *item;
142         struct ec_comp *c = NULL;
143         struct ec_parse *p = NULL;
144         char *line = NULL;
145         unsigned int count;
146         char **helps = NULL;
147         int match = 0;
148         int cols;
149
150         (void)ignore;
151         (void)invoking_key;
152
153         line = strdup(rl_line_buffer);
154         if (line == NULL)
155                 goto fail;
156
157         /* check if the current line matches */
158         p = ec_node_parse(commands, line);
159         if (ec_parse_matches(p))
160                 match = 1;
161         ec_parse_free(p);
162         p = NULL;
163
164         /* complete at current cursor position */
165         line[rl_point] = '\0';
166         c = ec_node_complete(commands, line);
167         free(line);
168         line = NULL;
169         if (c == NULL)
170                 goto fail;
171
172         /* let's display one contextual help per node */
173         count = 0;
174         iter = ec_comp_iter(c,
175                 EC_COMP_UNKNOWN | EC_COMP_FULL | EC_COMP_PARTIAL);
176         if (iter == NULL)
177                 goto fail;
178
179         /* strangely, rl_display_match_list() expects first index at 1 */
180         helps = calloc(match + 1, sizeof(char *));
181         if (helps == NULL)
182                 goto fail;
183         if (match)
184                 helps[1] = "<return>";
185
186         while ((item = ec_comp_iter_next(iter)) != NULL) {
187                 char **tmp;
188
189                 /* keep one help per group, skip other items  */
190                 grp = ec_comp_item_get_grp(item);
191                 if (grp == prev_grp)
192                         continue;
193
194                 prev_grp = grp;
195
196                 tmp = realloc(helps, (count + match + 2) * sizeof(char *));
197                 if (tmp == NULL)
198                         goto fail;
199                 helps = tmp;
200                 helps[count + match + 1] = get_node_help(item);
201                 count++;
202         }
203
204         ec_comp_iter_free(iter);
205         ec_comp_free(c);
206         /* ensure not more than 1 entry per line */
207         rl_get_screen_size(NULL, &cols);
208         rl_display_match_list(helps, count + match, cols);
209         rl_forced_update_display();
210
211         return 0;
212
213 fail:
214         ec_comp_iter_free(iter);
215         ec_parse_free(p);
216         free(line);
217         ec_comp_free(c);
218         if (helps != NULL) {
219                 while (count--)
220                         free(helps[count + match + 1]);
221         }
222         free(helps);
223
224         return 1;
225 }
226
227 static int create_commands(void)
228 {
229         struct ec_node *cmdlist = NULL, *cmd = NULL;
230
231         cmdlist = ec_node("or", EC_NO_ID);
232         if (cmdlist == NULL)
233                 goto fail;
234
235
236         cmd = EC_NODE_SEQ(EC_NO_ID,
237                 ec_node_str(EC_NO_ID, "hello"),
238                 EC_NODE_OR("name",
239                         ec_node_str("john", "john"),
240                         ec_node_str(EC_NO_ID, "johnny"),
241                         ec_node_str(EC_NO_ID, "mike")
242                 ),
243                 ec_node_option(EC_NO_ID, ec_node_int("int", 0, 10, 10))
244         );
245         if (cmd == NULL)
246                 goto fail;
247         ec_dict_set(ec_node_attrs(cmd), "help",
248                 "say hello to someone several times", NULL);
249         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "john")),
250                 "help", "specific help for john", NULL);
251         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "name")),
252                 "help", "the name of the person", NULL);
253         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "int")),
254                 "help", "an integer (0-10)", NULL);
255         if (ec_node_or_add(cmdlist, cmd) < 0)
256                 goto fail;
257
258
259         cmd = EC_NODE_CMD(EC_NO_ID, "good morning name [count]",
260                         EC_NODE_CMD("name", "bob|bobby|michael"),
261                         ec_node_int("count", 0, 10, 10));
262         if (cmd == NULL)
263                 goto fail;
264         ec_dict_set(ec_node_attrs(cmd), "help",
265                 "say good morning to someone several times", NULL);
266         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
267                 "the person to greet", NULL);
268         ec_dict_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
269                 "how many times to greet (0-10)", NULL);
270         if (ec_node_or_add(cmdlist, cmd) < 0)
271                 goto fail;
272
273
274         cmd = EC_NODE_CMD(EC_NO_ID,
275                         "buy potatoes,carrots,pumpkins");
276         if (cmd == NULL)
277                 goto fail;
278         ec_dict_set(ec_node_attrs(cmd), "help",
279                 "buy some vegetables", NULL);
280         if (ec_node_or_add(cmdlist, cmd) < 0)
281                 goto fail;
282
283
284         cmd = EC_NODE_CMD(EC_NO_ID, "eat vegetables",
285                         ec_node_many("vegetables",
286                                 EC_NODE_OR(EC_NO_ID,
287                                         ec_node_str(EC_NO_ID, "potatoes"),
288                                         ec_node_once(EC_NO_ID,
289                                                 ec_node_str(EC_NO_ID, "carrots")),
290                                         ec_node_once(EC_NO_ID,
291                                                 ec_node_str(EC_NO_ID, "pumpkins"))),
292                         1, 0));
293         if (cmd == NULL)
294                 goto fail;
295         ec_dict_set(ec_node_attrs(cmd), "help",
296                 "eat vegetables (take some more potatoes)", NULL);
297         if (ec_node_or_add(cmdlist, cmd) < 0)
298                 goto fail;
299
300
301         cmd = EC_NODE_SEQ(EC_NO_ID,
302                 ec_node_str(EC_NO_ID, "bye")
303         );
304         ec_dict_set(ec_node_attrs(cmd), "help", "say bye", NULL);
305         if (ec_node_or_add(cmdlist, cmd) < 0)
306                 goto fail;
307
308
309         cmd = EC_NODE_SEQ(EC_NO_ID,
310                 ec_node_str(EC_NO_ID, "load"),
311                 ec_node("file", EC_NO_ID)
312         );
313         ec_dict_set(ec_node_attrs(cmd), "help", "load a file", NULL);
314         if (ec_node_or_add(cmdlist, cmd) < 0)
315                 goto fail;
316
317
318         commands = ec_node_sh_lex(EC_NO_ID, cmdlist);
319         if (commands == NULL)
320                 goto fail;
321
322         return 0;
323
324  fail:
325         fprintf(stderr, "cannot initialize nodes\n");
326         ec_node_free(cmdlist);
327         return -1;
328 }
329
330 int main(void)
331 {
332         struct ec_parse *p;
333         char *line;
334
335         if (ec_init() < 0) {
336                 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
337                 return 1;
338         }
339
340         if (create_commands() < 0)
341                 return 1;
342
343         rl_bind_key('?', show_help);
344         rl_attempted_completion_function = my_attempted_completion;
345
346         while (1) {
347                 line = readline("> ");
348                 if (line == NULL)
349                         break;
350
351                 p = ec_node_parse(commands, line);
352                 ec_parse_dump(stdout, p);
353                 add_history(line);
354                 ec_parse_free(p);
355         }
356
357
358         ec_node_free(commands);
359         return 0;
360
361 }