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_node_seq.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_node_option.h>
44 #include <ecoli_node_sh_lex.h>
46 struct ec_node_sh_lex {
48 struct ec_node *child;
51 static size_t eat_spaces(const char *str)
56 while (isblank(str[i]))
63 * Allocate a new string which is a copy of the input string with quotes
64 * removed. If quotes are not closed properly, set missing_quote to the
67 static char *unquote_str(const char *str, size_t n, int allow_missing_quote,
70 unsigned s = 1, d = 0;
81 /* copy string and remove quotes */
82 while (s < n && d < n && str[s] != '\0') {
83 if (str[s] == '\\' && str[s+1] == quote) {
88 if (str[s] == '\\' && str[s+1] == '\\') {
93 if (str[s] == quote) {
101 /* not enough room in dst buffer (should not happen) */
108 /* quote not closed */
110 if (missing_quote != NULL)
111 *missing_quote = str[0];
112 if (allow_missing_quote == 0) {
123 static size_t eat_quoted_str(const char *str)
128 while (str[i] != '\0') {
129 if (str[i] != '\\' && str[i+1] == quote)
134 /* unclosed quote, will be detected later */
138 static size_t eat_str(const char *str)
143 while (!isblank(str[i]) && str[i] != '\0')
149 static struct ec_strvec *tokenize(const char *str, int completion,
150 int allow_missing_quote, char *missing_quote)
152 struct ec_strvec *strvec = NULL;
153 size_t off = 0, len, suboff, sublen;
154 char *word = NULL, *concat = NULL, *tmp;
155 int last_is_space = 1;
157 // printf("str=%s\n", str);
159 strvec = ec_strvec_new();
163 while (str[off] != '\0') {
164 len = eat_spaces(&str[off]);
167 // printf("space=%zd\n", len);
172 while (str[suboff] != '\0') {
174 if (str[suboff] == '"' || str[suboff] == '\'') {
175 sublen = eat_quoted_str(&str[suboff]);
176 // printf("sublen=%zd\n", sublen);
177 word = unquote_str(&str[suboff], sublen,
178 allow_missing_quote, missing_quote);
180 sublen = eat_str(&str[suboff]);
181 // printf("sublen=%zd\n", sublen);
184 word = ec_strndup(&str[suboff], sublen);
189 // printf("word=%s\n", word);
194 if (concat == NULL) {
198 tmp = ec_realloc(concat, len + 1);
202 strcat(concat, word);
208 if (concat != NULL) {
209 if (ec_strvec_add(strvec, concat) < 0)
215 /* XXX remove all printf comments */
216 // printf("str off=%zd len=%zd\n", off, len);
220 /* in completion mode, append an empty string in the vector if
221 * the input string ends with space */
222 if (completion && last_is_space) {
223 if (ec_strvec_add(strvec, "") < 0)
232 ec_strvec_free(strvec);
236 static struct ec_parsed *ec_node_sh_lex_parse(const struct ec_node *gen_node,
237 const struct ec_strvec *strvec)
239 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
240 struct ec_strvec *new_vec = NULL, *match_strvec;
241 struct ec_parsed *parsed = NULL, *child_parsed;
244 parsed = ec_parsed_new();
248 if (ec_strvec_len(strvec) == 0)
251 str = ec_strvec_val(strvec, 0);
252 new_vec = tokenize(str, 0, 0, NULL);
256 child_parsed = ec_node_parse_strvec(node->child, new_vec);
257 if (child_parsed == NULL)
260 if (!ec_parsed_matches(child_parsed) ||
261 ec_parsed_len(child_parsed) !=
262 ec_strvec_len(new_vec)) {
263 ec_strvec_free(new_vec);
264 ec_parsed_free(child_parsed);
267 ec_strvec_free(new_vec);
270 ec_parsed_add_child(parsed, child_parsed);
271 match_strvec = ec_strvec_ndup(strvec, 0, 1);
272 if (match_strvec == NULL)
274 ec_parsed_set_match(parsed, gen_node, match_strvec);
279 ec_strvec_free(new_vec);
280 ec_parsed_free(parsed);
285 static struct ec_completed *ec_node_sh_lex_complete(const struct ec_node *gen_node,
286 const struct ec_strvec *strvec)
288 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
289 struct ec_completed *completed, *child_completed = NULL;
290 struct ec_strvec *new_vec = NULL;
294 // printf("==================\n");
295 completed = ec_completed_new();
296 if (completed == NULL)
299 if (ec_strvec_len(strvec) != 1)
302 str = ec_strvec_val(strvec, 0);
303 new_vec = tokenize(str, 1, 1, &missing_quote);
307 // ec_strvec_dump(new_vec, stdout);
309 child_completed = ec_node_complete_strvec(node->child, new_vec);
310 if (child_completed == NULL)
313 ec_strvec_free(new_vec);
315 ec_completed_merge(completed, child_completed);
320 ec_strvec_free(new_vec);
321 ec_completed_free(completed);
325 static void ec_node_sh_lex_free_priv(struct ec_node *gen_node)
327 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
329 ec_node_free(node->child);
332 static struct ec_node_type ec_node_sh_lex_type = {
334 .parse = ec_node_sh_lex_parse,
335 .complete = ec_node_sh_lex_complete,
336 .size = sizeof(struct ec_node_sh_lex),
337 .free_priv = ec_node_sh_lex_free_priv,
340 EC_NODE_TYPE_REGISTER(ec_node_sh_lex_type);
342 struct ec_node *ec_node_sh_lex(const char *id, struct ec_node *child)
344 struct ec_node_sh_lex *node = NULL;
349 node = (struct ec_node_sh_lex *)__ec_node_new(&ec_node_sh_lex_type, id);
360 static int ec_node_sh_lex_testcase(void)
362 struct ec_node *node;
365 node = ec_node_sh_lex(NULL,
367 ec_node_str(NULL, "foo"),
369 ec_node_str(NULL, "toto")
371 ec_node_str(NULL, "bar")
375 ec_log(EC_LOG_ERR, "cannot create node\n");
378 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo bar");
379 ret |= EC_TEST_CHECK_PARSE(node, 1, " foo bar");
380 ret |= EC_TEST_CHECK_PARSE(node, 1, " 'foo' \"bar\"");
381 ret |= EC_TEST_CHECK_PARSE(node, 1, " 'f'oo 'toto' bar");
384 /* test completion */
385 node = ec_node_sh_lex(NULL,
387 ec_node_str(NULL, "foo"),
389 ec_node_str(NULL, "toto")
391 ec_node_str(NULL, "bar"),
392 ec_node_str(NULL, "titi")
396 ec_log(EC_LOG_ERR, "cannot create node\n");
399 ret |= EC_TEST_CHECK_COMPLETE(node,
401 "foo", EC_NODE_ENDLIST,
403 ret |= EC_TEST_CHECK_COMPLETE(node,
404 " ", EC_NODE_ENDLIST,
405 "foo", EC_NODE_ENDLIST,
407 ret |= EC_TEST_CHECK_COMPLETE(node,
408 "f", EC_NODE_ENDLIST,
409 "oo", EC_NODE_ENDLIST,
411 ret |= EC_TEST_CHECK_COMPLETE(node,
412 "foo", EC_NODE_ENDLIST,
415 ret |= EC_TEST_CHECK_COMPLETE(node,
416 "foo ", EC_NODE_ENDLIST,
417 "bar", "toto", EC_NODE_ENDLIST,
419 ret |= EC_TEST_CHECK_COMPLETE(node,
420 "foo t", EC_NODE_ENDLIST,
421 "oto", EC_NODE_ENDLIST,
423 ret |= EC_TEST_CHECK_COMPLETE(node,
424 "foo b", EC_NODE_ENDLIST,
425 "ar", EC_NODE_ENDLIST,
427 ret |= EC_TEST_CHECK_COMPLETE(node,
428 "foo bar", EC_NODE_ENDLIST,
431 ret |= EC_TEST_CHECK_COMPLETE(node,
432 "foo bar ", EC_NODE_ENDLIST,
433 "titi", EC_NODE_ENDLIST,
435 ret |= EC_TEST_CHECK_COMPLETE(node,
436 "foo toto bar ", EC_NODE_ENDLIST,
437 "titi", EC_NODE_ENDLIST,
439 ret |= EC_TEST_CHECK_COMPLETE(node,
440 "x", EC_NODE_ENDLIST,
443 ret |= EC_TEST_CHECK_COMPLETE(node,
444 "foo barx", EC_NODE_ENDLIST,
452 static struct ec_test ec_node_sh_lex_test = {
453 .name = "node_sh_lex",
454 .test = ec_node_sh_lex_testcase,
457 EC_TEST_REGISTER(ec_node_sh_lex_test);