check for double test registration
[protos/libecoli.git] / lib / ecoli_node_sh_lex.c
index 0ce54fe..f3dce9e 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <ecoli_malloc.h>
 #include <ecoli_log.h>
+#include <ecoli_string.h>
 #include <ecoli_test.h>
 #include <ecoli_strvec.h>
 #include <ecoli_node.h>
@@ -45,6 +46,8 @@
 #include <ecoli_node_option.h>
 #include <ecoli_node_sh_lex.h>
 
+EC_LOG_TYPE_REGISTER(node_sh_lex);
+
 struct ec_node_sh_lex {
        struct ec_node gen;
        struct ec_node *child;
@@ -141,8 +144,9 @@ static size_t eat_str(const char *str)
 {
        size_t i = 0;
 
-       /* skip spaces */
-       while (!isblank(str[i]) && str[i] != '\0')
+       /* eat chars until we find a quote, space, or end of string  */
+       while (!isblank(str[i]) && str[i] != '\0' &&
+                       str[i] != '"' && str[i] != '\'')
                i++;
 
        return i;
@@ -156,31 +160,30 @@ static struct ec_strvec *tokenize(const char *str, int completion,
        char *word = NULL, *concat = NULL, *tmp;
        int last_is_space = 1;
 
-//     printf("str=%s\n", str);
-
        strvec = ec_strvec();
        if (strvec == NULL)
                goto fail;
 
        while (str[off] != '\0') {
+               if (missing_quote != NULL)
+                       *missing_quote = '\0';
                len = eat_spaces(&str[off]);
                if (len > 0)
                        last_is_space = 1;
-//             printf("space=%zd\n", len);
                off += len;
 
                len = 0;
                suboff = off;
                while (str[suboff] != '\0') {
+                       if (missing_quote != NULL)
+                               *missing_quote = '\0';
                        last_is_space = 0;
                        if (str[suboff] == '"' || str[suboff] == '\'') {
                                sublen = eat_quoted_str(&str[suboff]);
-//                             printf("sublen=%zd\n", sublen);
                                word = unquote_str(&str[suboff], sublen,
                                        allow_missing_quote, missing_quote);
                        } else {
                                sublen = eat_str(&str[suboff]);
-//                             printf("sublen=%zd\n", sublen);
                                if (sublen == 0)
                                        break;
                                word = ec_strndup(&str[suboff], sublen);
@@ -188,7 +191,6 @@ static struct ec_strvec *tokenize(const char *str, int completion,
 
                        if (word == NULL)
                                goto fail;
-//                     printf("word=%s\n", word);
 
                        len += sublen;
                        suboff += sublen;
@@ -214,8 +216,6 @@ static struct ec_strvec *tokenize(const char *str, int completion,
                        concat = NULL;
                }
 
-               /* XXX remove all printf comments */
-//             printf("str off=%zd len=%zd\n", off, len);
                off += len;
        }
 
@@ -253,20 +253,24 @@ ec_node_sh_lex_parse(const struct ec_node *gen_node,
                new_vec = tokenize(str, 0, 0, NULL);
        }
        if (new_vec == NULL) {
-               ret = -ENOMEM;
+               if (errno == EINVAL)
+                       ret = EC_PARSED_NOMATCH;
+               else
+                       ret = -ENOMEM;
                goto fail;
        }
 
        ret = ec_node_parse_child(node->child, state, new_vec);
-       if (ret >= 0) {
-               if ((unsigned)ret == ec_strvec_len(new_vec)) {
-                       ret = 1;
-               } else {
-                       child_parsed = ec_parsed_get_last_child(state);
-                       ec_parsed_del_child(state, child_parsed);
-                       ec_parsed_free(child_parsed);
-                       ret = EC_PARSED_NOMATCH;
-               }
+       if (ret < 0)
+               goto fail;
+
+       if ((unsigned)ret == ec_strvec_len(new_vec)) {
+               ret = 1;
+       } else if (ret != EC_PARSED_NOMATCH) {
+               child_parsed = ec_parsed_get_last_child(state);
+               ec_parsed_del_child(state, child_parsed);
+               ec_parsed_free(child_parsed);
+               ret = EC_PARSED_NOMATCH;
        }
 
        ec_strvec_free(new_vec);
@@ -279,46 +283,83 @@ ec_node_sh_lex_parse(const struct ec_node *gen_node,
        return ret;
 }
 
-static struct ec_completed *
+static int
 ec_node_sh_lex_complete(const struct ec_node *gen_node,
-                       struct ec_parsed *state,
+                       struct ec_completed *completed,
                        const struct ec_strvec *strvec)
 {
        struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
-       struct ec_completed *completed, *child_completed = NULL;
+       struct ec_completed *tmp_completed = NULL;
        struct ec_strvec *new_vec = NULL;
+       struct ec_completed_iter *iter = NULL;
+       struct ec_completed_item *item = NULL;
+       char *new_str = NULL;
        const char *str;
        char missing_quote;
-
-//     printf("==================\n");
-       completed = ec_completed();
-       if (completed == NULL)
-               return NULL;
+       int ret;
 
        if (ec_strvec_len(strvec) != 1)
-               return completed;
+               return 0;
 
        str = ec_strvec_val(strvec, 0);
        new_vec = tokenize(str, 1, 1, &missing_quote);
        if (new_vec == NULL)
                goto fail;
 
-//     ec_strvec_dump(new_vec, stdout);
+       /* we will store the completions in a temporary struct, because
+        * we want to update them (ex: add missing quotes) */
+       tmp_completed = ec_completed(ec_completed_get_state(completed));
+       if (tmp_completed == NULL)
+               goto fail;
 
-       child_completed = ec_node_complete_child(node->child, state, new_vec);
-       if (child_completed == NULL)
+       ret = ec_node_complete_child(node->child, tmp_completed, new_vec);
+       if (ret < 0)
                goto fail;
 
+       /* add missing quote for full completions  */
+       if (missing_quote != '\0') {
+               iter = ec_completed_iter(tmp_completed, EC_COMP_FULL);
+               if (iter == NULL)
+                       goto fail;
+               while ((item = ec_completed_iter_next(iter)) != NULL) {
+                       str = ec_completed_item_get_str(item);
+                       if (ec_asprintf(&new_str, "%c%s%c", missing_quote, str,
+                                       missing_quote) < 0) {
+                               new_str = NULL;
+                               goto fail;
+                       }
+                       if (ec_completed_item_set_str(item, new_str) < 0)
+                               goto fail;
+                       ec_free(new_str);
+                       new_str = NULL;
+
+                       str = ec_completed_item_get_completion(item);
+                       if (ec_asprintf(&new_str, "%s%c", str,
+                                       missing_quote) < 0) {
+                               new_str = NULL;
+                               goto fail;
+                       }
+                       if (ec_completed_item_set_completion(item, new_str) < 0)
+                               goto fail;
+                       ec_free(new_str);
+                       new_str = NULL;
+               }
+       }
+
+       ec_completed_iter_free(iter);
        ec_strvec_free(new_vec);
-       new_vec = NULL;
-       ec_completed_merge(completed, child_completed);
 
-       return completed;
+       ec_completed_merge(completed, tmp_completed);
+
+       return 0;
 
  fail:
+       ec_completed_free(tmp_completed);
+       ec_completed_iter_free(iter);
        ec_strvec_free(new_vec);
-       ec_completed_free(completed);
-       return NULL;
+       ec_free(new_str);
+
+       return -1;
 }
 
 static void ec_node_sh_lex_free_priv(struct ec_node *gen_node)
@@ -360,93 +401,85 @@ struct ec_node *ec_node_sh_lex(const char *id, struct ec_node *child)
 static int ec_node_sh_lex_testcase(void)
 {
        struct ec_node *node;
-       int ret = 0;
+       int testres = 0;
 
-       node = ec_node_sh_lex(NULL,
-               EC_NODE_SEQ(NULL,
-                       ec_node_str(NULL, "foo"),
-                       ec_node_option(NULL,
-                               ec_node_str(NULL, "toto")
+       node = ec_node_sh_lex(EC_NO_ID,
+               EC_NODE_SEQ(EC_NO_ID,
+                       ec_node_str(EC_NO_ID, "foo"),
+                       ec_node_option(EC_NO_ID,
+                               ec_node_str(EC_NO_ID, "toto")
                        ),
-                       ec_node_str(NULL, "bar")
+                       ec_node_str(EC_NO_ID, "bar")
                )
        );
        if (node == NULL) {
-               ec_log(EC_LOG_ERR, "cannot create node\n");
+               EC_LOG(EC_LOG_ERR, "cannot create node\n");
                return -1;
        }
-       ret |= EC_TEST_CHECK_PARSE(node, 1, "foo bar");
-       ret |= EC_TEST_CHECK_PARSE(node, 1, "  foo   bar");
-       ret |= EC_TEST_CHECK_PARSE(node, 1, "  'foo' \"bar\"");
-       ret |= EC_TEST_CHECK_PARSE(node, 1, "  'f'oo 'toto' bar");
+       testres |= EC_TEST_CHECK_PARSE(node, 1, "foo bar");
+       testres |= EC_TEST_CHECK_PARSE(node, 1, "  foo   bar");
+       testres |= EC_TEST_CHECK_PARSE(node, 1, "  'foo' \"bar\"");
+       testres |= EC_TEST_CHECK_PARSE(node, 1, "  'f'oo 'toto' bar");
+       testres |= EC_TEST_CHECK_PARSE(node, -1, "  foo toto bar'");
        ec_node_free(node);
 
        /* test completion */
-       node = ec_node_sh_lex(NULL,
-               EC_NODE_SEQ(NULL,
-                       ec_node_str(NULL, "foo"),
-                       ec_node_option(NULL,
-                               ec_node_str(NULL, "toto")
+       node = ec_node_sh_lex(EC_NO_ID,
+               EC_NODE_SEQ(EC_NO_ID,
+                       ec_node_str(EC_NO_ID, "foo"),
+                       ec_node_option(EC_NO_ID,
+                               ec_node_str(EC_NO_ID, "toto")
                        ),
-                       ec_node_str(NULL, "bar"),
-                       ec_node_str(NULL, "titi")
+                       ec_node_str(EC_NO_ID, "bar"),
+                       ec_node_str(EC_NO_ID, "titi")
                )
        );
        if (node == NULL) {
-               ec_log(EC_LOG_ERR, "cannot create node\n");
+               EC_LOG(EC_LOG_ERR, "cannot create node\n");
                return -1;
        }
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "", EC_NODE_ENDLIST,
-               "foo", EC_NODE_ENDLIST,
-               "foo");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "foo", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                " ", EC_NODE_ENDLIST,
-               "foo", EC_NODE_ENDLIST,
-               "foo");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "foo", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "f", EC_NODE_ENDLIST,
-               "oo", EC_NODE_ENDLIST,
-               "oo");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "foo", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "foo", EC_NODE_ENDLIST,
-               "", EC_NODE_ENDLIST,
-               "");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "foo", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "foo ", EC_NODE_ENDLIST,
-               "bar", "toto", EC_NODE_ENDLIST,
-               "");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "bar", "toto", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "foo t", EC_NODE_ENDLIST,
-               "oto", EC_NODE_ENDLIST,
-               "oto");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "toto", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "foo b", EC_NODE_ENDLIST,
-               "ar", EC_NODE_ENDLIST,
-               "ar");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "bar", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "foo bar", EC_NODE_ENDLIST,
-               "", EC_NODE_ENDLIST,
-               "");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "bar", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "foo bar ", EC_NODE_ENDLIST,
-               "titi", EC_NODE_ENDLIST,
-               "titi");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "titi", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "foo toto bar ", EC_NODE_ENDLIST,
-               "titi", EC_NODE_ENDLIST,
-               "titi");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "titi", EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "x", EC_NODE_ENDLIST,
-               EC_NODE_ENDLIST,
-               "");
-       ret |= EC_TEST_CHECK_COMPLETE(node,
+               EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
                "foo barx", EC_NODE_ENDLIST,
-               EC_NODE_ENDLIST,
-               "");
+               EC_NODE_ENDLIST);
+       testres |= EC_TEST_CHECK_COMPLETE(node,
+               "foo 'b", EC_NODE_ENDLIST,
+               "'bar'", EC_NODE_ENDLIST);
 
        ec_node_free(node);
-       return ret;
+       return testres;
 }
 /* LCOV_EXCL_STOP */