X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Fecoli_node_sh_lex.c;h=e27f21bdb1135635c3a5a83aca54751fe2571dab;hb=15718f19cbf3868bf6fba79f4e26b5254c57873e;hp=f3dce9e216163ab41278794c35c348a264e963fa;hpb=665f6b9392d6a6b37bc55e59437a6fa02cd9006c;p=protos%2Flibecoli.git diff --git a/lib/ecoli_node_sh_lex.c b/lib/ecoli_node_sh_lex.c index f3dce9e..e27f21b 100644 --- a/lib/ecoli_node_sh_lex.c +++ b/lib/ecoli_node_sh_lex.c @@ -1,28 +1,5 @@ -/* - * Copyright (c) 2016, Olivier MATZ - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of the University of California, Berkeley nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2016, Olivier MATZ */ #include @@ -39,8 +16,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include @@ -116,7 +93,7 @@ static char *unquote_str(const char *str, size_t n, int allow_missing_quote, *missing_quote = str[0]; if (allow_missing_quote == 0) { ec_free(dst); - errno = EINVAL; + errno = EBADMSG; return NULL; } } @@ -237,12 +214,12 @@ static struct ec_strvec *tokenize(const char *str, int completion, static int ec_node_sh_lex_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node; struct ec_strvec *new_vec = NULL; - struct ec_parsed *child_parsed; + struct ec_parse *child_parse; const char *str; int ret; @@ -252,13 +229,10 @@ ec_node_sh_lex_parse(const struct ec_node *gen_node, str = ec_strvec_val(strvec, 0); new_vec = tokenize(str, 0, 0, NULL); } - if (new_vec == NULL) { - if (errno == EINVAL) - ret = EC_PARSED_NOMATCH; - else - ret = -ENOMEM; + if (new_vec == NULL && errno == EBADMSG) /* quotes not closed */ + return EC_PARSE_NOMATCH; + if (new_vec == NULL) goto fail; - } ret = ec_node_parse_child(node->child, state, new_vec); if (ret < 0) @@ -266,11 +240,11 @@ ec_node_sh_lex_parse(const struct ec_node *gen_node, 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; + } else if (ret != EC_PARSE_NOMATCH) { + child_parse = ec_parse_get_last_child(state); + ec_parse_unlink_child(state, child_parse); + ec_parse_free(child_parse); + ret = EC_PARSE_NOMATCH; } ec_strvec_free(new_vec); @@ -280,22 +254,22 @@ ec_node_sh_lex_parse(const struct ec_node *gen_node, fail: ec_strvec_free(new_vec); - return ret; + return -1; } static int ec_node_sh_lex_complete(const struct ec_node *gen_node, - struct ec_completed *completed, + struct ec_comp *comp, const struct ec_strvec *strvec) { struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node; - struct ec_completed *tmp_completed = NULL; + struct ec_comp *tmp_comp = NULL; struct ec_strvec *new_vec = NULL; - struct ec_completed_iter *iter = NULL; - struct ec_completed_item *item = NULL; + struct ec_comp_iter *iter = NULL; + struct ec_comp_item *item = NULL; char *new_str = NULL; const char *str; - char missing_quote; + char missing_quote = '\0'; int ret; if (ec_strvec_len(strvec) != 1) @@ -308,54 +282,54 @@ ec_node_sh_lex_complete(const struct ec_node *gen_node, /* 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) + tmp_comp = ec_comp(ec_comp_get_state(comp)); + if (tmp_comp == NULL) goto fail; - ret = ec_node_complete_child(node->child, tmp_completed, new_vec); + ret = ec_node_complete_child(node->child, tmp_comp, 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); + iter = ec_comp_iter(tmp_comp, EC_COMP_FULL); if (iter == NULL) goto fail; - while ((item = ec_completed_iter_next(iter)) != NULL) { - str = ec_completed_item_get_str(item); + while ((item = ec_comp_iter_next(iter)) != NULL) { + str = ec_comp_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) + if (ec_comp_item_set_str(item, new_str) < 0) goto fail; ec_free(new_str); new_str = NULL; - str = ec_completed_item_get_completion(item); + str = ec_comp_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) + if (ec_comp_item_set_completion(item, new_str) < 0) goto fail; ec_free(new_str); new_str = NULL; } } - ec_completed_iter_free(iter); + ec_comp_iter_free(iter); ec_strvec_free(new_vec); - ec_completed_merge(completed, tmp_completed); + ec_comp_merge(comp, tmp_comp); return 0; fail: - ec_completed_free(tmp_completed); - ec_completed_iter_free(iter); + ec_comp_free(tmp_comp); + ec_comp_iter_free(iter); ec_strvec_free(new_vec); ec_free(new_str); @@ -369,12 +343,38 @@ static void ec_node_sh_lex_free_priv(struct ec_node *gen_node) ec_node_free(node->child); } +static size_t +ec_node_sh_lex_get_children_count(const struct ec_node *gen_node) +{ + struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node; + + if (node->child) + return 1; + return 0; +} + +static int +ec_node_sh_lex_get_child(const struct ec_node *gen_node, size_t i, + struct ec_node **child, unsigned int *refs) +{ + struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node; + + if (i >= 1) + return -1; + + *refs = 1; + *child = node->child; + return 0; +} + static struct ec_node_type ec_node_sh_lex_type = { .name = "sh_lex", .parse = ec_node_sh_lex_parse, .complete = ec_node_sh_lex_complete, .size = sizeof(struct ec_node_sh_lex), .free_priv = ec_node_sh_lex_free_priv, + .get_children_count = ec_node_sh_lex_get_children_count, + .get_child = ec_node_sh_lex_get_child, }; EC_NODE_TYPE_REGISTER(ec_node_sh_lex_type); @@ -386,7 +386,7 @@ struct ec_node *ec_node_sh_lex(const char *id, struct ec_node *child) if (child == NULL) return NULL; - node = (struct ec_node_sh_lex *)__ec_node(&ec_node_sh_lex_type, id); + node = (struct ec_node_sh_lex *)ec_node_from_type(&ec_node_sh_lex_type, id); if (node == NULL) { ec_node_free(child); return NULL;