2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #define _GNU_SOURCE /* for asprintf */
33 #include <readline/readline.h>
34 #include <readline/history.h>
37 #include <ecoli_keyval.h>
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>
46 static struct ec_tk *commands;
48 static char *my_completion_entry(const char *s, int state)
50 static struct ec_completed_tk *c;
51 static struct ec_completed_tk_iter *iter;
52 static const struct ec_completed_tk_elt *elt;
59 ec_completed_tk_free(c);
61 line = strdup(rl_line_buffer);
64 line[rl_point] = '\0';
66 c = ec_tk_complete(commands, line);
71 ec_completed_tk_iter_free(iter);
72 iter = ec_completed_tk_iter_new(c, EC_MATCH);
77 elt = ec_completed_tk_iter_next(iter);
81 if (asprintf(&out_string, "%s%s", s, elt->add) < 0)
87 static char **my_attempted_completion(const char *text, int start, int end)
92 /* remove default file completion */
93 rl_attempted_completion_over = 1;
95 return rl_completion_matches(text, my_completion_entry);
98 /* this function builds the help string */
99 static char *get_tk_help(const struct ec_tk *tk)
101 const struct ec_tk *tk2;
103 char *tk_help = NULL;
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");
111 if (asprintf(&help, "%-20s %s", ec_tk_desc(tk), tk_help) < 0)
117 static int show_help(int ignore, int invoking_key)
119 const struct ec_completed_tk_elt *elt;
120 struct ec_completed_tk_iter *iter;
121 struct ec_completed_tk *c;
123 unsigned int count, i;
129 line = strdup(rl_line_buffer);
132 line[rl_point] = '\0';
134 c = ec_tk_complete(commands, line);
138 //ec_completed_tk_dump(stdout, c);
140 count = ec_completed_tk_count(c, EC_MATCH | EC_NO_MATCH);
141 helps = calloc(count + 1, sizeof(char *));
145 iter = ec_completed_tk_iter_new(c, EC_MATCH | EC_NO_MATCH);
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);
156 ec_completed_tk_free(c);
158 rl_display_match_list(helps, count, 1000);
160 rl_forced_update_display();
170 static int create_commands(void)
172 struct ec_tk *cmdlist = NULL, *cmd = NULL;
174 cmdlist = ec_tk_or(NULL);
178 cmd = EC_TK_SEQ(NULL,
179 ec_tk_str(NULL, "hello"),
182 ec_tk_str(NULL, "john"),
183 ec_tk_str(NULL, "johnny"),
184 ec_tk_str(NULL, "mike")
186 ec_tk_int("int", 0, 10, 10)
189 ec_keyval_set(ec_tk_attrs(cmd), "help", "say hello to someone", NULL);
190 ec_keyval_set(ec_tk_attrs(ec_tk_find(cmd, "name")),
191 "help", "the name of the person", NULL);
192 ec_keyval_set(ec_tk_attrs(ec_tk_find(cmd, "int")),
193 "help", "an integer", NULL);
194 if (ec_tk_or_add(cmdlist, cmd) < 0)
197 cmd = EC_TK_SEQ(NULL,
198 ec_tk_str(NULL, "bye")
200 ec_keyval_set(ec_tk_attrs(cmd), "help", "say bye to someone", NULL);
201 if (ec_tk_or_add(cmdlist, cmd) < 0)
204 commands = ec_tk_sh_lex_new(NULL, cmdlist);
205 if (commands == NULL)
211 fprintf(stderr, "cannot initialize tokens\n");
218 struct ec_parsed_tk *p;
223 if (create_commands() < 0)
226 rl_bind_key('?', show_help);
227 rl_attempted_completion_function = my_attempted_completion;
230 line = readline("> ");
234 p = ec_tk_parse(commands, line);
235 ec_parsed_tk_dump(stdout, p);
237 ec_parsed_tk_free(p);
241 ec_tk_free(commands);