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.
36 #include <ecoli_malloc.h>
37 #include <ecoli_log.h>
38 #include <ecoli_test.h>
39 #include <ecoli_strvec.h>
40 #include <ecoli_node.h>
41 #include <ecoli_parsed.h>
42 #include <ecoli_completed.h>
43 #include <ecoli_node_seq.h>
44 #include <ecoli_node_str.h>
45 #include <ecoli_node_option.h>
46 #include <ecoli_node_sh_lex.h>
48 EC_LOG_TYPE_REGISTER(node_sh_lex);
50 struct ec_node_sh_lex {
52 struct ec_node *child;
55 static size_t eat_spaces(const char *str)
60 while (isblank(str[i]))
67 * Allocate a new string which is a copy of the input string with quotes
68 * removed. If quotes are not closed properly, set missing_quote to the
71 static char *unquote_str(const char *str, size_t n, int allow_missing_quote,
74 unsigned s = 1, d = 0;
85 /* copy string and remove quotes */
86 while (s < n && d < n && str[s] != '\0') {
87 if (str[s] == '\\' && str[s+1] == quote) {
92 if (str[s] == '\\' && str[s+1] == '\\') {
97 if (str[s] == quote) {
105 /* not enough room in dst buffer (should not happen) */
112 /* quote not closed */
114 if (missing_quote != NULL)
115 *missing_quote = str[0];
116 if (allow_missing_quote == 0) {
127 static size_t eat_quoted_str(const char *str)
132 while (str[i] != '\0') {
133 if (str[i] != '\\' && str[i+1] == quote)
138 /* unclosed quote, will be detected later */
142 static size_t eat_str(const char *str)
146 /* eat chars until we find a quote, space, or end of string */
147 while (!isblank(str[i]) && str[i] != '\0' &&
148 str[i] != '"' && str[i] != '\'')
154 static struct ec_strvec *tokenize(const char *str, int completion,
155 int allow_missing_quote, char *missing_quote)
157 struct ec_strvec *strvec = NULL;
158 size_t off = 0, len, suboff, sublen;
159 char *word = NULL, *concat = NULL, *tmp;
160 int last_is_space = 1;
162 strvec = ec_strvec();
166 while (str[off] != '\0') {
167 len = eat_spaces(&str[off]);
174 while (str[suboff] != '\0') {
176 if (str[suboff] == '"' || str[suboff] == '\'') {
177 sublen = eat_quoted_str(&str[suboff]);
178 word = unquote_str(&str[suboff], sublen,
179 allow_missing_quote, missing_quote);
181 sublen = eat_str(&str[suboff]);
184 word = ec_strndup(&str[suboff], sublen);
193 if (concat == NULL) {
197 tmp = ec_realloc(concat, len + 1);
201 strcat(concat, word);
207 if (concat != NULL) {
208 if (ec_strvec_add(strvec, concat) < 0)
214 /* XXX remove all printf comments */
215 // printf("str off=%zd len=%zd\n", off, len);
219 /* in completion mode, append an empty string in the vector if
220 * the input string ends with space */
221 if (completion && last_is_space) {
222 if (ec_strvec_add(strvec, "") < 0)
231 ec_strvec_free(strvec);
236 ec_node_sh_lex_parse(const struct ec_node *gen_node,
237 struct ec_parsed *state,
238 const struct ec_strvec *strvec)
240 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
241 struct ec_strvec *new_vec = NULL;
242 struct ec_parsed *child_parsed;
246 if (ec_strvec_len(strvec) == 0) {
247 new_vec = ec_strvec();
249 str = ec_strvec_val(strvec, 0);
250 new_vec = tokenize(str, 0, 0, NULL);
252 if (new_vec == NULL) {
257 ret = ec_node_parse_child(node->child, state, new_vec);
261 if ((unsigned)ret == ec_strvec_len(new_vec)) {
263 } else if (ret != EC_PARSED_NOMATCH) {
264 child_parsed = ec_parsed_get_last_child(state);
265 ec_parsed_del_child(state, child_parsed);
266 ec_parsed_free(child_parsed);
267 ret = EC_PARSED_NOMATCH;
270 ec_strvec_free(new_vec);
276 ec_strvec_free(new_vec);
281 ec_node_sh_lex_complete(const struct ec_node *gen_node,
282 struct ec_completed *completed,
283 const struct ec_strvec *strvec)
285 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
286 struct ec_strvec *new_vec = NULL;
291 if (ec_strvec_len(strvec) != 1)
294 str = ec_strvec_val(strvec, 0);
295 // printf("\nold:%s\n", str);
296 new_vec = tokenize(str, 1, 1, &missing_quote);
299 // printf("new:%s\n", ec_strvec_val(new_vec, 0));
301 // XXX: complete should add the quotes for !EC_PARTIAL
302 // XXX: if no quotes, replace " " by "\ "
303 ret = ec_node_complete_child(node->child, completed, new_vec);
307 ec_strvec_free(new_vec);
312 ec_strvec_free(new_vec);
316 static void ec_node_sh_lex_free_priv(struct ec_node *gen_node)
318 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
320 ec_node_free(node->child);
323 static struct ec_node_type ec_node_sh_lex_type = {
325 .parse = ec_node_sh_lex_parse,
326 .complete = ec_node_sh_lex_complete,
327 .size = sizeof(struct ec_node_sh_lex),
328 .free_priv = ec_node_sh_lex_free_priv,
331 EC_NODE_TYPE_REGISTER(ec_node_sh_lex_type);
333 struct ec_node *ec_node_sh_lex(const char *id, struct ec_node *child)
335 struct ec_node_sh_lex *node = NULL;
340 node = (struct ec_node_sh_lex *)__ec_node(&ec_node_sh_lex_type, id);
351 /* LCOV_EXCL_START */
352 static int ec_node_sh_lex_testcase(void)
354 struct ec_node *node;
357 node = ec_node_sh_lex(NULL,
359 ec_node_str(NULL, "foo"),
361 ec_node_str(NULL, "toto")
363 ec_node_str(NULL, "bar")
367 EC_LOG(EC_LOG_ERR, "cannot create node\n");
370 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo bar");
371 ret |= EC_TEST_CHECK_PARSE(node, 1, " foo bar");
372 ret |= EC_TEST_CHECK_PARSE(node, 1, " 'foo' \"bar\"");
373 ret |= EC_TEST_CHECK_PARSE(node, 1, " 'f'oo 'toto' bar");
374 ret |= EC_TEST_CHECK_PARSE(node, -1, " foo toto bar'");
377 /* test completion */
378 node = ec_node_sh_lex(NULL,
380 ec_node_str(NULL, "foo"),
382 ec_node_str(NULL, "toto")
384 ec_node_str(NULL, "bar"),
385 ec_node_str(NULL, "titi")
389 EC_LOG(EC_LOG_ERR, "cannot create node\n");
392 ret |= EC_TEST_CHECK_COMPLETE(node,
394 "foo", EC_NODE_ENDLIST);
395 ret |= EC_TEST_CHECK_COMPLETE(node,
396 " ", EC_NODE_ENDLIST,
397 "foo", EC_NODE_ENDLIST);
398 ret |= EC_TEST_CHECK_COMPLETE(node,
399 "f", EC_NODE_ENDLIST,
400 "foo", EC_NODE_ENDLIST);
401 ret |= EC_TEST_CHECK_COMPLETE(node,
402 "foo", EC_NODE_ENDLIST,
403 "foo", EC_NODE_ENDLIST);
404 ret |= EC_TEST_CHECK_COMPLETE(node,
405 "foo ", EC_NODE_ENDLIST,
406 "bar", "toto", EC_NODE_ENDLIST);
407 ret |= EC_TEST_CHECK_COMPLETE(node,
408 "foo t", EC_NODE_ENDLIST,
409 "toto", EC_NODE_ENDLIST);
410 ret |= EC_TEST_CHECK_COMPLETE(node,
411 "foo b", EC_NODE_ENDLIST,
412 "bar", EC_NODE_ENDLIST);
413 ret |= EC_TEST_CHECK_COMPLETE(node,
414 "foo bar", EC_NODE_ENDLIST,
415 "bar", EC_NODE_ENDLIST);
416 ret |= EC_TEST_CHECK_COMPLETE(node,
417 "foo bar ", EC_NODE_ENDLIST,
418 "titi", EC_NODE_ENDLIST);
419 ret |= EC_TEST_CHECK_COMPLETE(node,
420 "foo toto bar ", EC_NODE_ENDLIST,
421 "titi", EC_NODE_ENDLIST);
422 ret |= EC_TEST_CHECK_COMPLETE(node,
423 "x", EC_NODE_ENDLIST,
425 ret |= EC_TEST_CHECK_COMPLETE(node,
426 "foo barx", EC_NODE_ENDLIST,
434 static struct ec_test ec_node_sh_lex_test = {
435 .name = "node_sh_lex",
436 .test = ec_node_sh_lex_testcase,
439 EC_TEST_REGISTER(ec_node_sh_lex_test);