1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
13 #include <ecoli_malloc.h>
14 #include <ecoli_log.h>
15 #include <ecoli_string.h>
16 #include <ecoli_test.h>
17 #include <ecoli_strvec.h>
18 #include <ecoli_node.h>
19 #include <ecoli_parse.h>
20 #include <ecoli_complete.h>
21 #include <ecoli_node_seq.h>
22 #include <ecoli_node_str.h>
23 #include <ecoli_node_option.h>
24 #include <ecoli_node_sh_lex.h>
26 EC_LOG_TYPE_REGISTER(node_sh_lex);
28 struct ec_node_sh_lex {
30 struct ec_node *child;
33 static size_t eat_spaces(const char *str)
38 while (isblank(str[i]))
45 * Allocate a new string which is a copy of the input string with quotes
46 * removed. If quotes are not closed properly, set missing_quote to the
49 static char *unquote_str(const char *str, size_t n, int allow_missing_quote,
52 unsigned s = 1, d = 0;
63 /* copy string and remove quotes */
64 while (s < n && d < n && str[s] != '\0') {
65 if (str[s] == '\\' && str[s+1] == quote) {
70 if (str[s] == '\\' && str[s+1] == '\\') {
75 if (str[s] == quote) {
83 /* not enough room in dst buffer (should not happen) */
90 /* quote not closed */
92 if (missing_quote != NULL)
93 *missing_quote = str[0];
94 if (allow_missing_quote == 0) {
105 static size_t eat_quoted_str(const char *str)
110 while (str[i] != '\0') {
111 if (str[i] != '\\' && str[i+1] == quote)
116 /* unclosed quote, will be detected later */
120 static size_t eat_str(const char *str)
124 /* eat chars until we find a quote, space, or end of string */
125 while (!isblank(str[i]) && str[i] != '\0' &&
126 str[i] != '"' && str[i] != '\'')
132 static struct ec_strvec *tokenize(const char *str, int completion,
133 int allow_missing_quote, char *missing_quote)
135 struct ec_strvec *strvec = NULL;
136 size_t off = 0, len, suboff, sublen;
137 char *word = NULL, *concat = NULL, *tmp;
138 int last_is_space = 1;
140 strvec = ec_strvec();
144 while (str[off] != '\0') {
145 if (missing_quote != NULL)
146 *missing_quote = '\0';
147 len = eat_spaces(&str[off]);
154 while (str[suboff] != '\0') {
155 if (missing_quote != NULL)
156 *missing_quote = '\0';
158 if (str[suboff] == '"' || str[suboff] == '\'') {
159 sublen = eat_quoted_str(&str[suboff]);
160 word = unquote_str(&str[suboff], sublen,
161 allow_missing_quote, missing_quote);
163 sublen = eat_str(&str[suboff]);
166 word = ec_strndup(&str[suboff], sublen);
175 if (concat == NULL) {
179 tmp = ec_realloc(concat, len + 1);
183 strcat(concat, word);
189 if (concat != NULL) {
190 if (ec_strvec_add(strvec, concat) < 0)
199 /* in completion mode, append an empty string in the vector if
200 * the input string ends with space */
201 if (completion && last_is_space) {
202 if (ec_strvec_add(strvec, "") < 0)
211 ec_strvec_free(strvec);
216 ec_node_sh_lex_parse(const struct ec_node *gen_node,
217 struct ec_parse *state,
218 const struct ec_strvec *strvec)
220 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
221 struct ec_strvec *new_vec = NULL;
222 struct ec_parse *child_parse;
226 if (ec_strvec_len(strvec) == 0) {
227 new_vec = ec_strvec();
229 str = ec_strvec_val(strvec, 0);
230 new_vec = tokenize(str, 0, 0, NULL);
232 if (new_vec == NULL && errno == EBADMSG) /* quotes not closed */
233 return EC_PARSE_NOMATCH;
237 ret = ec_node_parse_child(node->child, state, new_vec);
241 if ((unsigned)ret == ec_strvec_len(new_vec)) {
243 } else if (ret != EC_PARSE_NOMATCH) {
244 child_parse = ec_parse_get_last_child(state);
245 ec_parse_unlink_child(state, child_parse);
246 ec_parse_free(child_parse);
247 ret = EC_PARSE_NOMATCH;
250 ec_strvec_free(new_vec);
256 ec_strvec_free(new_vec);
261 ec_node_sh_lex_complete(const struct ec_node *gen_node,
262 struct ec_comp *comp,
263 const struct ec_strvec *strvec)
265 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
266 struct ec_comp *tmp_comp = NULL;
267 struct ec_strvec *new_vec = NULL;
268 struct ec_comp_iter *iter = NULL;
269 struct ec_comp_item *item = NULL;
270 char *new_str = NULL;
272 char missing_quote = '\0';
275 if (ec_strvec_len(strvec) != 1)
278 str = ec_strvec_val(strvec, 0);
279 new_vec = tokenize(str, 1, 1, &missing_quote);
283 /* we will store the completions in a temporary struct, because
284 * we want to update them (ex: add missing quotes) */
285 tmp_comp = ec_comp(ec_comp_get_state(comp));
286 if (tmp_comp == NULL)
289 ret = ec_node_complete_child(node->child, tmp_comp, new_vec);
293 /* add missing quote for full completions */
294 if (missing_quote != '\0') {
295 iter = ec_comp_iter(tmp_comp, EC_COMP_FULL);
298 while ((item = ec_comp_iter_next(iter)) != NULL) {
299 str = ec_comp_item_get_str(item);
300 if (ec_asprintf(&new_str, "%c%s%c", missing_quote, str,
301 missing_quote) < 0) {
305 if (ec_comp_item_set_str(item, new_str) < 0)
310 str = ec_comp_item_get_completion(item);
311 if (ec_asprintf(&new_str, "%s%c", str,
312 missing_quote) < 0) {
316 if (ec_comp_item_set_completion(item, new_str) < 0)
323 ec_comp_iter_free(iter);
324 ec_strvec_free(new_vec);
326 ec_comp_merge(comp, tmp_comp);
331 ec_comp_free(tmp_comp);
332 ec_comp_iter_free(iter);
333 ec_strvec_free(new_vec);
339 static void ec_node_sh_lex_free_priv(struct ec_node *gen_node)
341 struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
343 ec_node_free(node->child);
346 static struct ec_node_type ec_node_sh_lex_type = {
348 .parse = ec_node_sh_lex_parse,
349 .complete = ec_node_sh_lex_complete,
350 .size = sizeof(struct ec_node_sh_lex),
351 .free_priv = ec_node_sh_lex_free_priv,
354 EC_NODE_TYPE_REGISTER(ec_node_sh_lex_type);
356 struct ec_node *ec_node_sh_lex(const char *id, struct ec_node *child)
358 struct ec_node_sh_lex *node = NULL;
363 node = (struct ec_node_sh_lex *)__ec_node(&ec_node_sh_lex_type, id);
374 /* LCOV_EXCL_START */
375 static int ec_node_sh_lex_testcase(void)
377 struct ec_node *node;
380 node = ec_node_sh_lex(EC_NO_ID,
381 EC_NODE_SEQ(EC_NO_ID,
382 ec_node_str(EC_NO_ID, "foo"),
383 ec_node_option(EC_NO_ID,
384 ec_node_str(EC_NO_ID, "toto")
386 ec_node_str(EC_NO_ID, "bar")
390 EC_LOG(EC_LOG_ERR, "cannot create node\n");
393 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo bar");
394 testres |= EC_TEST_CHECK_PARSE(node, 1, " foo bar");
395 testres |= EC_TEST_CHECK_PARSE(node, 1, " 'foo' \"bar\"");
396 testres |= EC_TEST_CHECK_PARSE(node, 1, " 'f'oo 'toto' bar");
397 testres |= EC_TEST_CHECK_PARSE(node, -1, " foo toto bar'");
400 /* test completion */
401 node = ec_node_sh_lex(EC_NO_ID,
402 EC_NODE_SEQ(EC_NO_ID,
403 ec_node_str(EC_NO_ID, "foo"),
404 ec_node_option(EC_NO_ID,
405 ec_node_str(EC_NO_ID, "toto")
407 ec_node_str(EC_NO_ID, "bar"),
408 ec_node_str(EC_NO_ID, "titi")
412 EC_LOG(EC_LOG_ERR, "cannot create node\n");
415 testres |= EC_TEST_CHECK_COMPLETE(node,
417 "foo", EC_NODE_ENDLIST);
418 testres |= EC_TEST_CHECK_COMPLETE(node,
419 " ", EC_NODE_ENDLIST,
420 "foo", EC_NODE_ENDLIST);
421 testres |= EC_TEST_CHECK_COMPLETE(node,
422 "f", EC_NODE_ENDLIST,
423 "foo", EC_NODE_ENDLIST);
424 testres |= EC_TEST_CHECK_COMPLETE(node,
425 "foo", EC_NODE_ENDLIST,
426 "foo", EC_NODE_ENDLIST);
427 testres |= EC_TEST_CHECK_COMPLETE(node,
428 "foo ", EC_NODE_ENDLIST,
429 "bar", "toto", EC_NODE_ENDLIST);
430 testres |= EC_TEST_CHECK_COMPLETE(node,
431 "foo t", EC_NODE_ENDLIST,
432 "toto", EC_NODE_ENDLIST);
433 testres |= EC_TEST_CHECK_COMPLETE(node,
434 "foo b", EC_NODE_ENDLIST,
435 "bar", EC_NODE_ENDLIST);
436 testres |= EC_TEST_CHECK_COMPLETE(node,
437 "foo bar", EC_NODE_ENDLIST,
438 "bar", EC_NODE_ENDLIST);
439 testres |= EC_TEST_CHECK_COMPLETE(node,
440 "foo bar ", EC_NODE_ENDLIST,
441 "titi", EC_NODE_ENDLIST);
442 testres |= EC_TEST_CHECK_COMPLETE(node,
443 "foo toto bar ", EC_NODE_ENDLIST,
444 "titi", EC_NODE_ENDLIST);
445 testres |= EC_TEST_CHECK_COMPLETE(node,
446 "x", EC_NODE_ENDLIST,
448 testres |= EC_TEST_CHECK_COMPLETE(node,
449 "foo barx", EC_NODE_ENDLIST,
451 testres |= EC_TEST_CHECK_COMPLETE(node,
452 "foo 'b", EC_NODE_ENDLIST,
453 "'bar'", EC_NODE_ENDLIST);
460 static struct ec_test ec_node_sh_lex_test = {
461 .name = "node_sh_lex",
462 .test = ec_node_sh_lex_testcase,
465 EC_TEST_REGISTER(ec_node_sh_lex_test);