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