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_string.h>
39 #include <ecoli_test.h>
40 #include <ecoli_strvec.h>
41 #include <ecoli_node.h>
42 #include <ecoli_parsed.h>
43 #include <ecoli_completed.h>
44 #include <ecoli_node_seq.h>
45 #include <ecoli_node_str.h>
46 #include <ecoli_node_option.h>
47 #include <ecoli_node_sh_lex.h>
49 EC_LOG_TYPE_REGISTER(node_sh_lex);
51 struct ec_node_sh_lex {
53 struct ec_node *child;
56 static size_t eat_spaces(const char *str)
61 while (isblank(str[i]))
68 * Allocate a new string which is a copy of the input string with quotes
69 * removed. If quotes are not closed properly, set missing_quote to the
72 static char *unquote_str(const char *str, size_t n, int allow_missing_quote,
75 unsigned s = 1, d = 0;
86 /* copy string and remove quotes */
87 while (s < n && d < n && str[s] != '\0') {
88 if (str[s] == '\\' && str[s+1] == quote) {
93 if (str[s] == '\\' && str[s+1] == '\\') {
98 if (str[s] == quote) {
106 /* not enough room in dst buffer (should not happen) */
113 /* quote not closed */
115 if (missing_quote != NULL)
116 *missing_quote = str[0];
117 if (allow_missing_quote == 0) {
128 static size_t eat_quoted_str(const char *str)
133 while (str[i] != '\0') {
134 if (str[i] != '\\' && str[i+1] == quote)
139 /* unclosed quote, will be detected later */
143 static size_t eat_str(const char *str)
147 /* eat chars until we find a quote, space, or end of string */
148 while (!isblank(str[i]) && str[i] != '\0' &&
149 str[i] != '"' && str[i] != '\'')
155 static struct ec_strvec *tokenize(const char *str, int completion,
156 int allow_missing_quote, char *missing_quote)
158 struct ec_strvec *strvec = NULL;
159 size_t off = 0, len, suboff, sublen;
160 char *word = NULL, *concat = NULL, *tmp;
161 int last_is_space = 1;
163 strvec = ec_strvec();
167 while (str[off] != '\0') {
168 if (missing_quote != NULL)
169 *missing_quote = '\0';
170 len = eat_spaces(&str[off]);
177 while (str[suboff] != '\0') {
178 if (missing_quote != NULL)
179 *missing_quote = '\0';
181 if (str[suboff] == '"' || str[suboff] == '\'') {
182 sublen = eat_quoted_str(&str[suboff]);
183 word = unquote_str(&str[suboff], sublen,
184 allow_missing_quote, missing_quote);
186 sublen = eat_str(&str[suboff]);
189 word = ec_strndup(&str[suboff], sublen);
198 if (concat == NULL) {
202 tmp = ec_realloc(concat, len + 1);
206 strcat(concat, word);
212 if (concat != NULL) {
213 if (ec_strvec_add(strvec, concat) < 0)
222 /* in completion mode, append an empty string in the vector if
223 * the input string ends with space */
224 if (completion && last_is_space) {
225 if (ec_strvec_add(strvec, "") < 0)
234 ec_strvec_free(strvec);
239 ec_node_sh_lex_parse(const struct ec_node *gen_node,
240 struct ec_parsed *state,
241 const struct ec_strvec *strvec)
243 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
244 struct ec_strvec *new_vec = NULL;
245 struct ec_parsed *child_parsed;
249 if (ec_strvec_len(strvec) == 0) {
250 new_vec = ec_strvec();
252 str = ec_strvec_val(strvec, 0);
253 new_vec = tokenize(str, 0, 0, NULL);
255 if (new_vec == NULL) {
257 ret = EC_PARSED_NOMATCH;
263 ret = ec_node_parse_child(node->child, state, new_vec);
267 if ((unsigned)ret == ec_strvec_len(new_vec)) {
269 } else if (ret != EC_PARSED_NOMATCH) {
270 child_parsed = ec_parsed_get_last_child(state);
271 ec_parsed_del_child(state, child_parsed);
272 ec_parsed_free(child_parsed);
273 ret = EC_PARSED_NOMATCH;
276 ec_strvec_free(new_vec);
282 ec_strvec_free(new_vec);
287 ec_node_sh_lex_complete(const struct ec_node *gen_node,
288 struct ec_completed *completed,
289 const struct ec_strvec *strvec)
291 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
292 struct ec_completed *tmp_completed = NULL;
293 struct ec_strvec *new_vec = NULL;
294 struct ec_completed_iter *iter = NULL;
295 struct ec_completed_item *item = NULL;
296 char *new_str = NULL;
301 if (ec_strvec_len(strvec) != 1)
304 str = ec_strvec_val(strvec, 0);
305 new_vec = tokenize(str, 1, 1, &missing_quote);
309 /* we will store the completions in a temporary struct, because
310 * we want to update them (ex: add missing quotes) */
311 tmp_completed = ec_completed(ec_completed_get_state(completed));
312 if (tmp_completed == NULL)
315 ret = ec_node_complete_child(node->child, tmp_completed, new_vec);
319 /* add missing quote for full completions */
320 if (missing_quote != '\0') {
321 iter = ec_completed_iter(tmp_completed, EC_COMP_FULL);
324 while ((item = ec_completed_iter_next(iter)) != NULL) {
325 str = ec_completed_item_get_str(item);
326 if (ec_asprintf(&new_str, "%c%s%c", missing_quote, str,
327 missing_quote) < 0) {
331 if (ec_completed_item_set_str(item, new_str) < 0)
336 str = ec_completed_item_get_completion(item);
337 if (ec_asprintf(&new_str, "%s%c", str,
338 missing_quote) < 0) {
342 if (ec_completed_item_set_completion(item, new_str) < 0)
349 ec_completed_iter_free(iter);
350 ec_strvec_free(new_vec);
352 ec_completed_merge(completed, tmp_completed);
357 ec_completed_free(tmp_completed);
358 ec_completed_iter_free(iter);
359 ec_strvec_free(new_vec);
365 static void ec_node_sh_lex_free_priv(struct ec_node *gen_node)
367 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
369 ec_node_free(node->child);
372 static struct ec_node_type ec_node_sh_lex_type = {
374 .parse = ec_node_sh_lex_parse,
375 .complete = ec_node_sh_lex_complete,
376 .size = sizeof(struct ec_node_sh_lex),
377 .free_priv = ec_node_sh_lex_free_priv,
380 EC_NODE_TYPE_REGISTER(ec_node_sh_lex_type);
382 struct ec_node *ec_node_sh_lex(const char *id, struct ec_node *child)
384 struct ec_node_sh_lex *node = NULL;
389 node = (struct ec_node_sh_lex *)__ec_node(&ec_node_sh_lex_type, id);
400 /* LCOV_EXCL_START */
401 static int ec_node_sh_lex_testcase(void)
403 struct ec_node *node;
406 node = ec_node_sh_lex(EC_NO_ID,
407 EC_NODE_SEQ(EC_NO_ID,
408 ec_node_str(EC_NO_ID, "foo"),
409 ec_node_option(EC_NO_ID,
410 ec_node_str(EC_NO_ID, "toto")
412 ec_node_str(EC_NO_ID, "bar")
416 EC_LOG(EC_LOG_ERR, "cannot create node\n");
419 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo bar");
420 testres |= EC_TEST_CHECK_PARSE(node, 1, " foo bar");
421 testres |= EC_TEST_CHECK_PARSE(node, 1, " 'foo' \"bar\"");
422 testres |= EC_TEST_CHECK_PARSE(node, 1, " 'f'oo 'toto' bar");
423 testres |= EC_TEST_CHECK_PARSE(node, -1, " foo toto bar'");
426 /* test completion */
427 node = ec_node_sh_lex(EC_NO_ID,
428 EC_NODE_SEQ(EC_NO_ID,
429 ec_node_str(EC_NO_ID, "foo"),
430 ec_node_option(EC_NO_ID,
431 ec_node_str(EC_NO_ID, "toto")
433 ec_node_str(EC_NO_ID, "bar"),
434 ec_node_str(EC_NO_ID, "titi")
438 EC_LOG(EC_LOG_ERR, "cannot create node\n");
441 testres |= EC_TEST_CHECK_COMPLETE(node,
443 "foo", EC_NODE_ENDLIST);
444 testres |= EC_TEST_CHECK_COMPLETE(node,
445 " ", EC_NODE_ENDLIST,
446 "foo", EC_NODE_ENDLIST);
447 testres |= EC_TEST_CHECK_COMPLETE(node,
448 "f", EC_NODE_ENDLIST,
449 "foo", EC_NODE_ENDLIST);
450 testres |= EC_TEST_CHECK_COMPLETE(node,
451 "foo", EC_NODE_ENDLIST,
452 "foo", EC_NODE_ENDLIST);
453 testres |= EC_TEST_CHECK_COMPLETE(node,
454 "foo ", EC_NODE_ENDLIST,
455 "bar", "toto", EC_NODE_ENDLIST);
456 testres |= EC_TEST_CHECK_COMPLETE(node,
457 "foo t", EC_NODE_ENDLIST,
458 "toto", EC_NODE_ENDLIST);
459 testres |= EC_TEST_CHECK_COMPLETE(node,
460 "foo b", EC_NODE_ENDLIST,
461 "bar", EC_NODE_ENDLIST);
462 testres |= EC_TEST_CHECK_COMPLETE(node,
463 "foo bar", EC_NODE_ENDLIST,
464 "bar", EC_NODE_ENDLIST);
465 testres |= EC_TEST_CHECK_COMPLETE(node,
466 "foo bar ", EC_NODE_ENDLIST,
467 "titi", EC_NODE_ENDLIST);
468 testres |= EC_TEST_CHECK_COMPLETE(node,
469 "foo toto bar ", EC_NODE_ENDLIST,
470 "titi", EC_NODE_ENDLIST);
471 testres |= EC_TEST_CHECK_COMPLETE(node,
472 "x", EC_NODE_ENDLIST,
474 testres |= EC_TEST_CHECK_COMPLETE(node,
475 "foo barx", EC_NODE_ENDLIST,
477 testres |= EC_TEST_CHECK_COMPLETE(node,
478 "foo 'b", EC_NODE_ENDLIST,
479 "'bar'", EC_NODE_ENDLIST);
486 static struct ec_test ec_node_sh_lex_test = {
487 .name = "node_sh_lex",
488 .test = ec_node_sh_lex_testcase,
491 EC_TEST_REGISTER(ec_node_sh_lex_test);