continue !
[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_tk.h>
37 #include <ecoli_keyval.h>
38
39 #include <ecoli_tk_str.h>
40 #include <ecoli_tk_seq.h>
41 #include <ecoli_tk_space.h>
42 #include <ecoli_tk_or.h>
43 #include <ecoli_tk_shlex.h>
44 #include <ecoli_tk_int.h>
45
46 static struct ec_tk *commands;
47
48 static char *my_completion_entry(const char *s, int state)
49 {
50         static struct ec_completed_tk *c;
51         static struct ec_completed_tk_iter *iter;
52         static const struct ec_completed_tk_elt *elt;
53         char *out_string;
54
55
56         if (state == 0) {
57                 char *line;
58
59                 ec_completed_tk_free(c);
60
61                 line = strdup(rl_line_buffer);
62                 if (line == NULL)
63                         return NULL;
64                 line[rl_point] = '\0';
65
66                 c = ec_tk_complete(commands, line);
67                 free(line);
68                 if (c == NULL)
69                         return NULL;
70
71                 ec_completed_tk_iter_free(iter);
72                 iter = ec_completed_tk_iter_new(c, EC_MATCH);
73                 if (iter == NULL)
74                         return NULL;
75         }
76
77         elt = ec_completed_tk_iter_next(iter);
78         if (elt == NULL)
79                 return NULL;
80
81         if (asprintf(&out_string, "%s%s", s, elt->add) < 0)
82                 return NULL;
83
84         return out_string;
85 }
86
87 static char **my_attempted_completion(const char *text, int start, int end)
88 {
89         (void)start;
90         (void)end;
91
92         /* remove default file completion */
93         rl_attempted_completion_over = 1;
94
95         return rl_completion_matches(text, my_completion_entry);
96 }
97
98 /* this function builds the help string */
99 static char *get_tk_help(const struct ec_tk *tk)
100 {
101         const struct ec_tk *tk2;
102         char *help = NULL;
103         char *tk_help = NULL;
104
105         for (tk2 = tk; tk2 != NULL && tk_help == NULL; tk2 = ec_tk_parent(tk2))
106                 tk_help = ec_keyval_get(ec_tk_attrs(tk2), "help");
107
108         if (tk_help == NULL)
109                 tk_help = "";
110
111         if (asprintf(&help, "%-20s %s", ec_tk_desc(tk), tk_help) < 0)
112                 return NULL;
113
114         return help;
115 }
116
117 static int show_help(int ignore, int invoking_key)
118 {
119         const struct ec_completed_tk_elt *elt;
120         struct ec_completed_tk_iter *iter;
121         struct ec_completed_tk *c;
122         char *line;
123         unsigned int count, i;
124         char **helps = NULL;
125
126         (void)ignore;
127         (void)invoking_key;
128
129         line = strdup(rl_line_buffer);
130         if (line == NULL)
131                 return 1;
132         line[rl_point] = '\0';
133
134         c = ec_tk_complete(commands, line);
135         free(line);
136         if (c == NULL)
137                 return 1;
138         //ec_completed_tk_dump(stdout, c);
139
140         count = ec_completed_tk_count(c, EC_MATCH | EC_NO_MATCH);
141         helps = calloc(count + 1, sizeof(char *));
142         if (helps == NULL)
143                 return 1;
144
145         iter = ec_completed_tk_iter_new(c, EC_MATCH | EC_NO_MATCH);
146         if (iter == NULL)
147                 goto fail;
148
149         /* strangely, rl_display_match_list() expects first index at 1 */
150         for (i = 1, elt = ec_completed_tk_iter_next(iter);
151              i <= count && elt != NULL;
152              i++, elt = ec_completed_tk_iter_next(iter)) {
153                 helps[i] = get_tk_help(elt->tk);
154         }
155
156         ec_completed_tk_free(c);
157
158         rl_display_match_list(helps, count, 1000);
159
160         rl_forced_update_display();
161
162         return 0;
163
164 fail:
165         free(helps);
166         // free helps[n] XXX
167         return 1;
168 }
169
170 static int create_commands(void)
171 {
172         struct ec_tk *cmdlist = NULL, *cmd = NULL;
173
174         cmdlist = ec_tk_or_new(NULL);
175         if (cmdlist == NULL)
176                 goto fail;
177
178         cmd = ec_tk_seq_new_list(NULL,
179                 ec_tk_str_new(NULL, "hello"),
180                 ec_tk_or_new_list(NULL,
181                         ec_tk_or_new_list("name",
182                                 ec_tk_str_new(NULL, "john"),
183                                 ec_tk_str_new(NULL, "johnny"),
184                                 ec_tk_str_new(NULL, "mike"),
185                                 EC_TK_ENDLIST
186                         ),
187                         ec_tk_int_new("int", 0, 10, 10),
188                         EC_TK_ENDLIST
189                 ),
190                 EC_TK_ENDLIST
191         );
192         ec_keyval_set(ec_tk_attrs(cmd), "help", "say hello to someone", NULL);
193         ec_keyval_set(ec_tk_attrs(ec_tk_find(cmd, "name")),
194                 "help", "the name of the person", NULL);
195         ec_keyval_set(ec_tk_attrs(ec_tk_find(cmd, "int")),
196                 "help", "an integer", NULL);
197         if (ec_tk_or_add(cmdlist, cmd) < 0)
198                 goto fail;
199
200         cmd = ec_tk_seq_new_list(NULL,
201                 ec_tk_str_new(NULL, "bye"),
202                 EC_TK_ENDLIST
203         );
204         ec_keyval_set(ec_tk_attrs(cmd), "help", "say bye to someone", NULL);
205         if (ec_tk_or_add(cmdlist, cmd) < 0)
206                 goto fail;
207
208         commands = ec_tk_shlex_new(NULL, cmdlist);
209         if (commands == NULL)
210                 goto fail;
211
212         return 0;
213
214  fail:
215         fprintf(stderr, "cannot initialize tokens\n");
216         ec_tk_free(cmd);
217         ec_tk_free(cmdlist);
218         return -1;
219 }
220
221 int main(void)
222 {
223         struct ec_parsed_tk *p;
224 //      const char *name;
225         char *line;
226
227
228         if (create_commands() < 0)
229                 return 1;
230
231         rl_bind_key('?', show_help);
232         rl_attempted_completion_function = my_attempted_completion;
233
234         while (1) {
235                 line = readline("> ");
236                 if (line == NULL)
237                         break;
238
239                 p = ec_tk_parse(commands, line);
240                 ec_parsed_tk_dump(stdout, p);
241                 add_history(line);
242                 ec_parsed_tk_free(p);
243         }
244
245
246         ec_tk_free(commands);
247         return 0;
248
249 }