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