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_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_sh_lex.h>
44 #include <ecoli_tk_int.h>
45 #include <ecoli_tk_option.h>
46 #include <ecoli_tk_cmd.h>
47
48 static struct ec_tk *commands;
49
50 static char *my_completion_entry(const char *s, int state)
51 {
52         static struct ec_completed_tk *c;
53         static struct ec_completed_tk_iter *iter;
54         static const struct ec_completed_tk_elt *elt;
55         char *out_string;
56
57
58         if (state == 0) {
59                 char *line;
60
61                 ec_completed_tk_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_tk_complete(commands, line);
69                 free(line);
70                 if (c == NULL)
71                         return NULL;
72
73                 ec_completed_tk_iter_free(iter);
74                 iter = ec_completed_tk_iter_new(c, EC_MATCH);
75                 if (iter == NULL)
76                         return NULL;
77         }
78
79         elt = ec_completed_tk_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_tk *tk)
102 {
103         const struct ec_tk *tk2;
104         char *help = NULL;
105         char *tk_help = NULL;
106
107         for (tk2 = tk; tk2 != NULL && tk_help == NULL; tk2 = ec_tk_parent(tk2))
108                 tk_help = ec_keyval_get(ec_tk_attrs(tk2), "help");
109
110         if (tk_help == NULL)
111                 tk_help = "";
112
113         if (asprintf(&help, "%-20s %s", ec_tk_desc(tk), tk_help) < 0)
114                 return NULL;
115
116         return help;
117 }
118
119 static int show_help(int ignore, int invoking_key)
120 {
121         const struct ec_completed_tk_elt *elt;
122         struct ec_completed_tk_iter *iter;
123         struct ec_completed_tk *c;
124         char *line;
125         unsigned int count, i;
126         char **helps = NULL;
127
128         (void)ignore;
129         (void)invoking_key;
130
131         line = strdup(rl_line_buffer);
132         if (line == NULL)
133                 return 1;
134         line[rl_point] = '\0';
135
136         c = ec_tk_complete(commands, line);
137         free(line);
138         if (c == NULL)
139                 return 1;
140         //ec_completed_tk_dump(stdout, c);
141
142         count = ec_completed_tk_count(c, EC_MATCH | EC_NO_MATCH);
143         helps = calloc(count + 1, sizeof(char *));
144         if (helps == NULL)
145                 return 1;
146
147         iter = ec_completed_tk_iter_new(c, EC_MATCH | EC_NO_MATCH);
148         if (iter == NULL)
149                 goto fail;
150
151         /* strangely, rl_display_match_list() expects first index at 1 */
152         for (i = 1, elt = ec_completed_tk_iter_next(iter);
153              i <= count && elt != NULL;
154              i++, elt = ec_completed_tk_iter_next(iter)) {
155                 helps[i] = get_tk_help(elt->tk);
156         }
157
158         ec_completed_tk_free(c);
159
160         rl_display_match_list(helps, count, 1000);
161
162         rl_forced_update_display();
163
164         return 0;
165
166 fail:
167         free(helps);
168         // free helps[n] XXX
169         return 1;
170 }
171
172 static int create_commands(void)
173 {
174         struct ec_tk *cmdlist = NULL, *cmd = NULL;
175
176         cmdlist = ec_tk_or(NULL);
177         if (cmdlist == NULL)
178                 goto fail;
179
180         cmd = EC_TK_SEQ(NULL,
181                 ec_tk_str(NULL, "hello"),
182                 EC_TK_OR("name",
183                         ec_tk_str(NULL, "john"),
184                         ec_tk_str(NULL, "johnny"),
185                         ec_tk_str(NULL, "mike")
186                 ),
187                 ec_tk_option_new(NULL, ec_tk_int("int", 0, 10, 10))
188         );
189         if (cmd == NULL)
190                 goto fail;
191         ec_keyval_set(ec_tk_attrs(cmd), "help",
192                 "say hello to someone several times", 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 #if 0
201         cmd = EC_TK_CMD(NULL, "good morning john|johnny|mike [count]",
202                         ec_tk_int("count", 0, 10, 10));
203         if (cmd == NULL)
204                 goto fail;
205         ec_keyval_set(ec_tk_attrs(cmd), "help",
206                 "say good morning to someone several times", NULL);
207         if (ec_tk_or_add(cmdlist, cmd) < 0)
208                 goto fail;
209 #endif
210
211         cmd = EC_TK_SEQ(NULL,
212                 ec_tk_str(NULL, "bye")
213         );
214         ec_keyval_set(ec_tk_attrs(cmd), "help", "say bye to someone", NULL);
215         if (ec_tk_or_add(cmdlist, cmd) < 0)
216                 goto fail;
217
218         commands = ec_tk_sh_lex_new(NULL, cmdlist);
219         if (commands == NULL)
220                 goto fail;
221
222         return 0;
223
224  fail:
225         fprintf(stderr, "cannot initialize tokens\n");
226         ec_tk_free(cmdlist);
227         return -1;
228 }
229
230 int main(void)
231 {
232         struct ec_parsed_tk *p;
233 //      const char *name;
234         char *line;
235
236
237         if (create_commands() < 0)
238                 return 1;
239
240         rl_bind_key('?', show_help);
241         rl_attempted_completion_function = my_attempted_completion;
242
243         while (1) {
244                 line = readline("> ");
245                 if (line == NULL)
246                         break;
247
248                 p = ec_tk_parse(commands, line);
249                 ec_parsed_tk_dump(stdout, p);
250                 add_history(line);
251                 ec_parsed_tk_free(p);
252         }
253
254
255         ec_tk_free(commands);
256         return 0;
257
258 }