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>
36 #include <ecoli_node.h>
37 #include <ecoli_keyval.h>
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>
48 static struct ec_node *commands;
50 static char *my_completion_entry(const char *s, int state)
52 static struct ec_completed *c;
53 static struct ec_completed_iter *iter;
54 static const struct ec_completed_elt *elt;
63 line = strdup(rl_line_buffer);
66 line[rl_point] = '\0';
68 c = ec_node_complete(commands, line);
73 ec_completed_iter_free(iter);
74 iter = ec_completed_iter(c, EC_MATCH);
79 elt = ec_completed_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_node *node)
103 const struct ec_node *node2;
105 char *tk_help = NULL;
108 node2 != NULL && tk_help == NULL;
109 node2 = ec_node_parent(node2))
110 tk_help = ec_keyval_get(ec_node_attrs(node2), "help");
115 if (asprintf(&help, "%-20s %s", ec_node_desc(node), tk_help) < 0)
121 static int show_help(int ignore, int invoking_key)
123 const struct ec_completed_elt *elt;
124 struct ec_completed_iter *iter;
125 struct ec_completed *c;
127 unsigned int count, i;
133 line = strdup(rl_line_buffer);
136 line[rl_point] = '\0';
138 c = ec_node_complete(commands, line);
142 //ec_completed_dump(stdout, c);
144 count = ec_completed_count(c, EC_MATCH | EC_NO_MATCH);
145 helps = calloc(count + 1, sizeof(char *));
149 iter = ec_completed_iter(c, EC_MATCH | EC_NO_MATCH);
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);
160 ec_completed_free(c);
162 rl_display_match_list(helps, count, 1000);
164 rl_forced_update_display();
174 static int create_commands(void)
176 struct ec_node *cmdlist = NULL, *cmd = NULL;
178 cmdlist = ec_node("or", NULL);
182 cmd = EC_NODE_SEQ(NULL,
183 ec_node_str(NULL, "hello"),
185 ec_node_str(NULL, "john"),
186 ec_node_str(NULL, "johnny"),
187 ec_node_str(NULL, "mike")
189 ec_node_option(NULL, ec_node_int("int", 0, 10, 10))
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)
203 cmd = EC_NODE_CMD(NULL, "good morning john|johnny|mike [count]",
204 ec_node_int("count", 0, 10, 10));
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)
213 cmd = EC_NODE_SEQ(NULL,
214 ec_node_str(NULL, "bye")
216 ec_keyval_set(ec_node_attrs(cmd), "help", "say bye to someone", NULL);
217 if (ec_node_or_add(cmdlist, cmd) < 0)
220 commands = ec_node_sh_lex(NULL, cmdlist);
221 if (commands == NULL)
227 fprintf(stderr, "cannot initialize nodes\n");
228 ec_node_free(cmdlist);
239 if (create_commands() < 0)
242 rl_bind_key('?', show_help);
243 rl_attempted_completion_function = my_attempted_completion;
246 line = readline("> ");
250 p = ec_node_parse(commands, line);
251 ec_parsed_dump(stdout, p);
257 ec_node_free(commands);