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>
45 #include <ecoli_tk_option.h>
46 #include <ecoli_tk_cmd.h>
48 static struct ec_tk *commands;
50 static char *my_completion_entry(const char *s, int state)
52 static struct ec_completed_tk *c;
53 static struct ec_completed_tk_iter *iter;
54 static const struct ec_completed_tk_elt *elt;
61 ec_completed_tk_free(c);
63 line = strdup(rl_line_buffer);
66 line[rl_point] = '\0';
68 c = ec_tk_complete(commands, line);
73 ec_completed_tk_iter_free(iter);
74 iter = ec_completed_tk_iter_new(c, EC_MATCH);
79 elt = ec_completed_tk_iter_next(iter);
83 if (asprintf(&out_string, "%s%s", s, elt->add) < 0)
89 static char **my_attempted_completion(const char *text, int start, int end)
94 /* remove default file completion */
95 rl_attempted_completion_over = 1;
97 return rl_completion_matches(text, my_completion_entry);
100 /* this function builds the help string */
101 static char *get_tk_help(const struct ec_tk *tk)
103 const struct ec_tk *tk2;
105 char *tk_help = NULL;
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");
113 if (asprintf(&help, "%-20s %s", ec_tk_desc(tk), tk_help) < 0)
119 static int show_help(int ignore, int invoking_key)
121 const struct ec_completed_tk_elt *elt;
122 struct ec_completed_tk_iter *iter;
123 struct ec_completed_tk *c;
125 unsigned int count, i;
131 line = strdup(rl_line_buffer);
134 line[rl_point] = '\0';
136 c = ec_tk_complete(commands, line);
140 //ec_completed_tk_dump(stdout, c);
142 count = ec_completed_tk_count(c, EC_MATCH | EC_NO_MATCH);
143 helps = calloc(count + 1, sizeof(char *));
147 iter = ec_completed_tk_iter_new(c, EC_MATCH | EC_NO_MATCH);
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);
158 ec_completed_tk_free(c);
160 rl_display_match_list(helps, count, 1000);
162 rl_forced_update_display();
172 static int create_commands(void)
174 struct ec_tk *cmdlist = NULL, *cmd = NULL;
176 cmdlist = ec_tk_or(NULL);
180 cmd = EC_TK_SEQ(NULL,
181 ec_tk_str(NULL, "hello"),
183 ec_tk_str(NULL, "john"),
184 ec_tk_str(NULL, "johnny"),
185 ec_tk_str(NULL, "mike")
187 ec_tk_option_new(NULL, ec_tk_int("int", 0, 10, 10))
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)
201 cmd = EC_TK_CMD(NULL, "good morning john|johnny|mike [count]",
202 ec_tk_int("count", 0, 10, 10));
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)
211 cmd = EC_TK_SEQ(NULL,
212 ec_tk_str(NULL, "bye")
214 ec_keyval_set(ec_tk_attrs(cmd), "help", "say bye to someone", NULL);
215 if (ec_tk_or_add(cmdlist, cmd) < 0)
218 commands = ec_tk_sh_lex_new(NULL, cmdlist);
219 if (commands == NULL)
225 fprintf(stderr, "cannot initialize tokens\n");
232 struct ec_parsed_tk *p;
237 if (create_commands() < 0)
240 rl_bind_key('?', show_help);
241 rl_attempted_completion_function = my_attempted_completion;
244 line = readline("> ");
248 p = ec_tk_parse(commands, line);
249 ec_parsed_tk_dump(stdout, p);
251 ec_parsed_tk_free(p);
255 ec_tk_free(commands);