From 8df9723e4c3a2bb0ba89a8f5a70c319c7702935f Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Mon, 12 Mar 2018 21:57:17 +0100 Subject: [PATCH] parsed -> parse --- lib/Makefile | 2 +- lib/ecoli_complete.c | 26 +-- lib/ecoli_complete.h | 8 +- lib/ecoli_node.h | 4 +- lib/ecoli_node_any.c | 6 +- lib/ecoli_node_cmd.c | 36 ++-- lib/ecoli_node_dynamic.c | 30 +-- lib/ecoli_node_dynamic.h | 4 +- lib/ecoli_node_empty.c | 4 +- lib/ecoli_node_expr.c | 28 +-- lib/ecoli_node_expr.h | 18 +- lib/ecoli_node_expr_test.c | 30 +-- lib/ecoli_node_file.c | 6 +- lib/ecoli_node_int.c | 24 +-- lib/ecoli_node_many.c | 30 +-- lib/ecoli_node_none.c | 6 +- lib/ecoli_node_once.c | 22 +- lib/ecoli_node_option.c | 6 +- lib/ecoli_node_or.c | 8 +- lib/ecoli_node_re.c | 10 +- lib/ecoli_node_re_lex.c | 16 +- lib/ecoli_node_seq.c | 22 +- lib/ecoli_node_sh_lex.c | 18 +- lib/ecoli_node_space.c | 8 +- lib/ecoli_node_str.c | 10 +- lib/ecoli_node_subset.c | 46 ++-- lib/ecoli_node_weakref.c | 4 +- lib/ecoli_parse.c | 423 +++++++++++++++++++++++++++++++++++++ lib/ecoli_parse.h | 237 +++++++++++++++++++++ lib/ecoli_parsed.c | 423 ------------------------------------- lib/ecoli_parsed.h | 237 --------------------- lib/ecoli_test.c | 16 +- lib/main-readline.c | 22 +- 33 files changed, 895 insertions(+), 895 deletions(-) create mode 100644 lib/ecoli_parse.c create mode 100644 lib/ecoli_parse.h delete mode 100644 lib/ecoli_parsed.c delete mode 100644 lib/ecoli_parsed.h diff --git a/lib/Makefile b/lib/Makefile index a6d5057..e033dd8 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -50,7 +50,7 @@ srcs += ecoli_node_space.c srcs += ecoli_node_str.c srcs += ecoli_node_subset.c srcs += ecoli_node_weakref.c -srcs += ecoli_parsed.c +srcs += ecoli_parse.c srcs += ecoli_string.c srcs += ecoli_vec.c diff --git a/lib/ecoli_complete.c b/lib/ecoli_complete.c index 72abdf9..e488e0e 100644 --- a/lib/ecoli_complete.c +++ b/lib/ecoli_complete.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include struct ec_comp_item { @@ -29,7 +29,7 @@ struct ec_comp_item { struct ec_keyval *attrs; }; -struct ec_comp *ec_comp(struct ec_parsed *state) +struct ec_comp *ec_comp(struct ec_parse *state) { struct ec_comp *comp = NULL; @@ -55,7 +55,7 @@ struct ec_comp *ec_comp(struct ec_parsed *state) return NULL; } -struct ec_parsed *ec_comp_get_state(struct ec_comp *comp) +struct ec_parse *ec_comp_get_state(struct ec_comp *comp) { return comp->cur_state; } @@ -65,7 +65,7 @@ ec_node_complete_child(const struct ec_node *node, struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_parsed *child_state, *cur_state; + struct ec_parse *child_state, *cur_state; struct ec_comp_group *cur_group; int ret; @@ -74,12 +74,12 @@ ec_node_complete_child(const struct ec_node *node, /* save previous parse state, prepare child state */ cur_state = comp->cur_state; - child_state = ec_parsed(node); + child_state = ec_parse(node); if (child_state == NULL) return -ENOMEM; if (cur_state != NULL) - ec_parsed_link_child(cur_state, child_state); + ec_parse_link_child(cur_state, child_state); comp->cur_state = child_state; cur_group = comp->cur_group; comp->cur_group = NULL; @@ -89,10 +89,10 @@ ec_node_complete_child(const struct ec_node *node, /* restore parent parse state */ if (cur_state != NULL) { - ec_parsed_unlink_child(cur_state, child_state); - assert(!ec_parsed_has_child(child_state)); + ec_parse_unlink_child(cur_state, child_state); + assert(!ec_parse_has_child(child_state)); } - ec_parsed_free(child_state); + ec_parse_free(child_state); comp->cur_state = cur_state; comp->cur_group = cur_group; @@ -150,7 +150,7 @@ struct ec_comp *ec_node_complete(const struct ec_node *node, } static struct ec_comp_group * -ec_comp_group(const struct ec_node *node, struct ec_parsed *parsed) +ec_comp_group(const struct ec_node *node, struct ec_parse *parse) { struct ec_comp_group *grp = NULL; @@ -162,7 +162,7 @@ ec_comp_group(const struct ec_node *node, struct ec_parsed *parsed) if (grp->attrs == NULL) goto fail; - grp->state = ec_parsed_dup(parsed); + grp->state = ec_parse_dup(parse); if (grp->state == NULL) goto fail; @@ -173,7 +173,7 @@ ec_comp_group(const struct ec_node *node, struct ec_parsed *parsed) fail: if (grp != NULL) { - ec_parsed_free(grp->state); + ec_parse_free(grp->state); ec_keyval_free(grp->attrs); } ec_free(grp); @@ -470,7 +470,7 @@ static void ec_comp_group_free(struct ec_comp_group *grp) TAILQ_REMOVE(&grp->items, item, next); ec_comp_item_free(item); } - ec_parsed_free(ec_parsed_get_root(grp->state)); + ec_parse_free(ec_parse_get_root(grp->state)); ec_keyval_free(grp->attrs); ec_free(grp); } diff --git a/lib/ecoli_complete.h b/lib/ecoli_complete.h index 123d3fe..14d4b85 100644 --- a/lib/ecoli_complete.h +++ b/lib/ecoli_complete.h @@ -35,7 +35,7 @@ struct ec_comp_group { TAILQ_ENTRY(ec_comp_group) next; const struct ec_node *node; struct ec_comp_item_list items; - struct ec_parsed *state; + struct ec_parse *state; struct ec_keyval *attrs; }; @@ -46,7 +46,7 @@ struct ec_comp { unsigned count_full; unsigned count_partial; unsigned count_unknown; - struct ec_parsed *cur_state; + struct ec_parse *cur_state; struct ec_comp_group *cur_group; struct ec_comp_group_list groups; struct ec_keyval *attrs; @@ -71,7 +71,7 @@ int ec_node_complete_child(const struct ec_node *node, * * */ -struct ec_comp *ec_comp(struct ec_parsed *state); +struct ec_comp *ec_comp(struct ec_parse *state); /** * Free a completion object and all its items. @@ -96,7 +96,7 @@ void ec_comp_dump(FILE *out, int ec_comp_merge(struct ec_comp *to, struct ec_comp *from); -struct ec_parsed *ec_comp_get_state(struct ec_comp *comp); +struct ec_parse *ec_comp_get_state(struct ec_comp *comp); /* shortcut for ec_comp_item() + ec_comp_item_add() */ int ec_comp_add_item(struct ec_comp *comp, diff --git a/lib/ecoli_node.h b/lib/ecoli_node.h index 34b1139..e371a8b 100644 --- a/lib/ecoli_node.h +++ b/lib/ecoli_node.h @@ -51,7 +51,7 @@ #define EC_NODE_ENDLIST ((void *)1) struct ec_node; -struct ec_parsed; +struct ec_parse; struct ec_comp; struct ec_strvec; struct ec_keyval; @@ -73,7 +73,7 @@ TAILQ_HEAD(ec_node_type_list, ec_node_type); typedef int (*ec_node_build_t)(struct ec_node *node); typedef int (*ec_node_parse_t)(const struct ec_node *node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec); typedef int (*ec_node_complete_t)(const struct ec_node *node, struct ec_comp *comp_state, diff --git a/lib/ecoli_node_any.c b/lib/ecoli_node_any.c index e9452b2..197a2c5 100644 --- a/lib/ecoli_node_any.c +++ b/lib/ecoli_node_any.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include @@ -23,14 +23,14 @@ struct ec_node_any { }; static int ec_node_any_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { (void)gen_node; (void)state; if (ec_strvec_len(strvec) == 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; return 1; } diff --git a/lib/ecoli_node_cmd.c b/lib/ecoli_node_cmd.c index 180be43..64242e6 100644 --- a/lib/ecoli_node_cmd.c +++ b/lib/ecoli_node_cmd.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -44,7 +44,7 @@ struct ec_node_cmd { static int ec_node_cmd_eval_var(void **result, void *userctx, - const struct ec_parsed *var) + const struct ec_parse *var) { const struct ec_strvec *vec; struct ec_node_cmd *node = userctx; @@ -55,7 +55,7 @@ ec_node_cmd_eval_var(void **result, void *userctx, (void)userctx; /* get parsed string vector, it should contain only one str */ - vec = ec_parsed_strvec(var); + vec = ec_parse_strvec(var); if (ec_strvec_len(vec) != 1) return -EINVAL; str = ec_strvec_val(vec, 0); @@ -87,7 +87,7 @@ ec_node_cmd_eval_var(void **result, void *userctx, static int ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand, - const struct ec_parsed *operator) + const struct ec_parse *operator) { (void)result; (void)userctx; @@ -99,7 +99,7 @@ ec_node_cmd_eval_pre_op(void **result, void *userctx, void *operand, static int ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand, - const struct ec_parsed *operator) + const struct ec_parse *operator) { const struct ec_strvec *vec; struct ec_node *in = operand;; @@ -108,7 +108,7 @@ ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand, (void)userctx; /* get parsed string vector, it should contain only one str */ - vec = ec_parsed_strvec(operator); + vec = ec_parse_strvec(operator); if (ec_strvec_len(vec) != 1) return -EINVAL; @@ -128,7 +128,7 @@ ec_node_cmd_eval_post_op(void **result, void *userctx, void *operand, static int ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1, - const struct ec_parsed *operator, void *operand2) + const struct ec_parse *operator, void *operand2) { const struct ec_strvec *vec; @@ -139,7 +139,7 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1, (void)userctx; /* get parsed string vector, it should contain only one str */ - vec = ec_parsed_strvec(operator); + vec = ec_parse_strvec(operator); if (ec_strvec_len(vec) > 1) return -EINVAL; @@ -207,8 +207,8 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1, static int ec_node_cmd_eval_parenthesis(void **result, void *userctx, - const struct ec_parsed *open_paren, - const struct ec_parsed *close_paren, + const struct ec_parse *open_paren, + const struct ec_parse *close_paren, void *value) { const struct ec_strvec *vec; @@ -219,7 +219,7 @@ ec_node_cmd_eval_parenthesis(void **result, void *userctx, (void)close_paren; /* get parsed string vector, it should contain only one str */ - vec = ec_parsed_strvec(open_paren); + vec = ec_parse_strvec(open_paren); if (ec_strvec_len(vec) != 1) return -EINVAL; @@ -258,7 +258,7 @@ static const struct ec_node_expr_eval_ops test_ops = { static int ec_node_cmd_build(struct ec_node_cmd *node) { struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL; - struct ec_parsed *p = NULL; + struct ec_parse *p = NULL; void *result; int ret; @@ -330,17 +330,17 @@ static int ec_node_cmd_build(struct ec_node_cmd *node) goto fail; ret = -EINVAL; - if (!ec_parsed_matches(p)) + if (!ec_parse_matches(p)) goto fail; - if (!ec_parsed_has_child(p)) + if (!ec_parse_has_child(p)) goto fail; - ret = ec_node_expr_eval(&result, expr, ec_parsed_get_first_child(p), + ret = ec_node_expr_eval(&result, expr, ec_parse_get_first_child(p), &test_ops, node); if (ret < 0) goto fail; - ec_parsed_free(p); + ec_parse_free(p); p = NULL; node->expr = expr; @@ -350,7 +350,7 @@ static int ec_node_cmd_build(struct ec_node_cmd *node) return 0; fail: - ec_parsed_free(p); + ec_parse_free(p); ec_node_free(expr); ec_node_free(lex); ec_node_free(cmd); @@ -358,7 +358,7 @@ fail: } static int -ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state, +ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; diff --git a/lib/ecoli_node_dynamic.c b/lib/ecoli_node_dynamic.c index 84b603e..9032257 100644 --- a/lib/ecoli_node_dynamic.c +++ b/lib/ecoli_node_dynamic.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include @@ -31,7 +31,7 @@ struct ec_node_dynamic { static int ec_node_dynamic_parse(const struct ec_node *gen_node, - struct ec_parsed *parsed, + struct ec_parse *parse, const struct ec_strvec *strvec) { struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node; @@ -40,21 +40,21 @@ ec_node_dynamic_parse(const struct ec_node *gen_node, char key[64]; int ret = -1; - child = node->build(parsed, node->opaque); + child = node->build(parse, node->opaque); if (child == NULL) goto fail; /* add the node pointer in the attributes, so it will be freed - * when parsed is freed */ + * when parse is freed */ snprintf(key, sizeof(key), "_dyn_%p", child); - ret = ec_keyval_set(ec_parsed_get_attrs(parsed), key, child, + ret = ec_keyval_set(ec_parse_get_attrs(parse), key, child, (void *)node_free); if (ret < 0) { child = NULL; /* already freed */ goto fail; } - return ec_node_parse_child(child, parsed, strvec); + return ec_node_parse_child(child, parse, strvec); fail: ec_node_free(child); @@ -67,19 +67,19 @@ ec_node_dynamic_complete(const struct ec_node *gen_node, const struct ec_strvec *strvec) { struct ec_node_dynamic *node = (struct ec_node_dynamic *)gen_node; - struct ec_parsed *parsed; + struct ec_parse *parse; struct ec_node *child = NULL; void (*node_free)(struct ec_node *) = ec_node_free; char key[64]; int ret = -1; - parsed = ec_comp_get_state(comp); - child = node->build(parsed, node->opaque); + parse = ec_comp_get_state(comp); + child = node->build(parse, node->opaque); if (child == NULL) goto fail; /* add the node pointer in the attributes, so it will be freed - * when parsed is freed */ + * when parse is freed */ snprintf(key, sizeof(key), "_dyn_%p", child); ret = ec_keyval_set(comp->attrs, key, child, (void *)node_free); @@ -132,17 +132,17 @@ fail: EC_NODE_TYPE_REGISTER(ec_node_dynamic_type); static struct ec_node * -build_counter(struct ec_parsed *parsed, void *opaque) +build_counter(struct ec_parse *parse, void *opaque) { const struct ec_node *node; - struct ec_parsed *iter; + struct ec_parse *iter; unsigned int count = 0; char buf[32]; (void)opaque; - for (iter = ec_parsed_get_root(parsed); iter != NULL; - iter = ec_parsed_iter_next(iter)) { - node = ec_parsed_get_node(iter); + for (iter = ec_parse_get_root(parse); iter != NULL; + iter = ec_parse_iter_next(iter)) { + node = ec_parse_get_node(iter); if (node->id && !strcmp(node->id, "my-id")) count++; } diff --git a/lib/ecoli_node_dynamic.h b/lib/ecoli_node_dynamic.h index bb0acaf..4f2535e 100644 --- a/lib/ecoli_node_dynamic.h +++ b/lib/ecoli_node_dynamic.h @@ -6,12 +6,12 @@ #define ECOLI_NODE_DYNAMIC_ struct ec_node; -struct ec_parsed; +struct ec_parse; /* callback invoked by parse() or complete() to build the dynamic node * the behavior of the node can depend on what is already parsed */ typedef struct ec_node *(*ec_node_dynamic_build_t)( - struct ec_parsed *state, void *opaque); + struct ec_parse *state, void *opaque); struct ec_node *ec_node_dynamic(const char *id, ec_node_dynamic_build_t build, void *opaque); diff --git a/lib/ecoli_node_empty.c b/lib/ecoli_node_empty.c index 7d40c6c..6ce76e8 100644 --- a/lib/ecoli_node_empty.c +++ b/lib/ecoli_node_empty.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include @@ -23,7 +23,7 @@ struct ec_node_empty { }; static int ec_node_empty_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { (void)gen_node; diff --git a/lib/ecoli_node_expr.c b/lib/ecoli_node_expr.c index b722220..8a0d319 100644 --- a/lib/ecoli_node_expr.c +++ b/lib/ecoli_node_expr.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -46,7 +46,7 @@ struct ec_node_expr { }; static int ec_node_expr_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_expr *node = (struct ec_node_expr *)gen_node; @@ -444,7 +444,7 @@ static enum expr_node_type get_node_type(const struct ec_node *expr_gen_node, struct result { bool has_val; void *val; - const struct ec_parsed *op; + const struct ec_parse *op; enum expr_node_type op_type; }; @@ -505,32 +505,32 @@ static int eval_expression(struct result *result, void *userctx, const struct ec_node_expr_eval_ops *ops, const struct ec_node *expr_gen_node, - const struct ec_parsed *parsed) + const struct ec_parse *parse) { - struct ec_parsed *open = NULL, *close = NULL; + struct ec_parse *open = NULL, *close = NULL; struct result child_result; - struct ec_parsed *child; + struct ec_parse *child; enum expr_node_type type; int ret; memset(result, 0, sizeof(*result)); memset(&child_result, 0, sizeof(child_result)); - type = get_node_type(expr_gen_node, ec_parsed_get_node(parsed)); + type = get_node_type(expr_gen_node, ec_parse_get_node(parse)); if (type == VAL) { - ret = ops->eval_var(&result->val, userctx, parsed); + ret = ops->eval_var(&result->val, userctx, parse); if (ret < 0) goto fail; result->has_val = 1; } else if (type == PRE_OP || type == POST_OP || type == BIN_OP) { - result->op = parsed; + result->op = parse; result->op_type = type; } - EC_PARSED_FOREACH_CHILD(child, parsed) { + EC_PARSE_FOREACH_CHILD(child, parse) { - type = get_node_type(expr_gen_node, ec_parsed_get_node(child)); + type = get_node_type(expr_gen_node, ec_parse_get_node(child)); if (type == PAREN_OPEN) { open = child; continue; @@ -571,7 +571,7 @@ fail: } int ec_node_expr_eval(void **user_result, const struct ec_node *node, - struct ec_parsed *parsed, const struct ec_node_expr_eval_ops *ops, + struct ec_parse *parse, const struct ec_node_expr_eval_ops *ops, void *userctx) { struct result result; @@ -586,10 +586,10 @@ int ec_node_expr_eval(void **user_result, const struct ec_node *node, if (ret < 0) return ret; - if (!ec_parsed_matches(parsed)) + if (!ec_parse_matches(parse)) return -EINVAL; - ret = eval_expression(&result, userctx, ops, node, parsed); + ret = eval_expression(&result, userctx, ops, node, parse); if (ret < 0) return ret; diff --git a/lib/ecoli_node_expr.h b/lib/ecoli_node_expr.h index 7188f3f..4f21d81 100644 --- a/lib/ecoli_node_expr.h +++ b/lib/ecoli_node_expr.h @@ -17,14 +17,14 @@ * A user-defined context passed to all callback functions, which * can be used to maintain a state or store global information. * @param var - * The parsed result referencing the variable. + * The parse result referencing the variable. * @return * 0 on success (*result must be set), or -errno on error (*result * is undefined). */ typedef int (*ec_node_expr_eval_var_t)( void **result, void *userctx, - const struct ec_parsed *var); + const struct ec_parse *var); /** * Callback function type for evaluating a prefix-operator @@ -38,7 +38,7 @@ typedef int (*ec_node_expr_eval_var_t)( * @param operand * The evaluated expression on which the operation should be applied. * @param var - * The parsed result referencing the operator. + * The parse result referencing the operator. * @return * 0 on success (*result must be set, operand is freed), * or -errno on error (*result is undefined, operand is not freed). @@ -46,23 +46,23 @@ typedef int (*ec_node_expr_eval_var_t)( typedef int (*ec_node_expr_eval_pre_op_t)( void **result, void *userctx, void *operand, - const struct ec_parsed *operator); + const struct ec_parse *operator); typedef int (*ec_node_expr_eval_post_op_t)( void **result, void *userctx, void *operand, - const struct ec_parsed *operator); + const struct ec_parse *operator); typedef int (*ec_node_expr_eval_bin_op_t)( void **result, void *userctx, void *operand1, - const struct ec_parsed *operator, + const struct ec_parse *operator, void *operand2); typedef int (*ec_node_expr_eval_parenthesis_t)( void **result, void *userctx, - const struct ec_parsed *open_paren, - const struct ec_parsed *close_paren, + const struct ec_parse *open_paren, + const struct ec_parse *close_paren, void * value); typedef void (*ec_node_expr_eval_free_t)( @@ -87,7 +87,7 @@ struct ec_node_expr_eval_ops { }; int ec_node_expr_eval(void **result, const struct ec_node *node, - struct ec_parsed *parsed, const struct ec_node_expr_eval_ops *ops, + struct ec_parse *parse, const struct ec_node_expr_eval_ops *ops, void *userctx); #endif diff --git a/lib/ecoli_node_expr_test.c b/lib/ecoli_node_expr_test.c index cd99022..2c9d58e 100644 --- a/lib/ecoli_node_expr_test.c +++ b/lib/ecoli_node_expr_test.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -25,7 +25,7 @@ struct my_eval_result { static int ec_node_expr_test_eval_var(void **result, void *userctx, - const struct ec_parsed *var) + const struct ec_parse *var) { const struct ec_strvec *vec; const struct ec_node *node; @@ -35,11 +35,11 @@ ec_node_expr_test_eval_var(void **result, void *userctx, (void)userctx; /* get parsed string vector, it should contain only one str */ - vec = ec_parsed_strvec(var); + vec = ec_parse_strvec(var); if (ec_strvec_len(vec) != 1) return -EINVAL; - node = ec_parsed_get_node(var); + node = ec_parse_get_node(var); if (ec_node_int_getval(node, ec_strvec_val(vec, 0), &val) < 0) return -EINVAL; @@ -56,7 +56,7 @@ ec_node_expr_test_eval_var(void **result, void *userctx, static int ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand, - const struct ec_parsed *operator) + const struct ec_parse *operator) { const struct ec_strvec *vec; struct my_eval_result *eval = operand;; @@ -64,7 +64,7 @@ ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand, (void)userctx; /* get parsed string vector, it should contain only one str */ - vec = ec_parsed_strvec(operator); + vec = ec_parse_strvec(operator); if (ec_strvec_len(vec) != 1) return -EINVAL; @@ -81,7 +81,7 @@ ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand, static int ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand, - const struct ec_parsed *operator) + const struct ec_parse *operator) { const struct ec_strvec *vec; struct my_eval_result *eval = operand;; @@ -89,7 +89,7 @@ ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand, (void)userctx; /* get parsed string vector, it should contain only one str */ - vec = ec_parsed_strvec(operator); + vec = ec_parse_strvec(operator); if (ec_strvec_len(vec) != 1) return -EINVAL; @@ -106,7 +106,7 @@ ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand, static int ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1, - const struct ec_parsed *operator, void *operand2) + const struct ec_parse *operator, void *operand2) { const struct ec_strvec *vec; @@ -116,7 +116,7 @@ ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1, (void)userctx; /* get parsed string vector, it should contain only one str */ - vec = ec_parsed_strvec(operator); + vec = ec_parse_strvec(operator); if (ec_strvec_len(vec) != 1) return -EINVAL; @@ -136,8 +136,8 @@ ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1, static int ec_node_expr_test_eval_parenthesis(void **result, void *userctx, - const struct ec_parsed *open_paren, - const struct ec_parsed *close_paren, + const struct ec_parse *open_paren, + const struct ec_parse *close_paren, void *value) { (void)userctx; @@ -170,7 +170,7 @@ static int ec_node_expr_test_eval(struct ec_node *lex_node, const struct ec_node *expr_node, const char *str, int val) { - struct ec_parsed *p; + struct ec_parse *p; void *result; struct my_eval_result *eval; int ret; @@ -180,11 +180,11 @@ static int ec_node_expr_test_eval(struct ec_node *lex_node, return -1; ret = ec_node_expr_eval(&result, expr_node, p, &test_ops, NULL); - ec_parsed_free(p); + ec_parse_free(p); if (ret < 0) return -1; - /* the parsed value is an integer */ + /* the parse value is an integer */ eval = result; assert(eval != NULL); diff --git a/lib/ecoli_node_file.c b/lib/ecoli_node_file.c index ab772dc..09f96fc 100644 --- a/lib/ecoli_node_file.c +++ b/lib/ecoli_node_file.c @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include @@ -38,14 +38,14 @@ struct ec_node_file { static int ec_node_file_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { (void)gen_node; (void)state; if (ec_strvec_len(strvec) == 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; return 1; } diff --git a/lib/ecoli_node_int.c b/lib/ecoli_node_int.c index 48577cf..2e1f2a2 100644 --- a/lib/ecoli_node_int.c +++ b/lib/ecoli_node_int.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -93,7 +93,7 @@ static int parse_ullint(struct ec_node_int_uint *node, const char *str, } static int ec_node_int_uint_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node; @@ -104,15 +104,15 @@ static int ec_node_int_uint_parse(const struct ec_node *gen_node, (void)state; if (ec_strvec_len(strvec) == 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; str = ec_strvec_val(strvec, 0); if (node->is_signed) { if (parse_llint(node, str, &i64) < 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; } else { if (parse_ullint(node, str, &u64) < 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; } return 1; } @@ -328,7 +328,7 @@ int ec_node_uint_getval(const struct ec_node *gen_node, const char *str, /* LCOV_EXCL_START */ static int ec_node_int_testcase(void) { - struct ec_parsed *p; + struct ec_parse *p; struct ec_node *node; const char *s; int testres = 0, ret; @@ -356,18 +356,18 @@ static int ec_node_int_testcase(void) testres |= EC_TEST_CHECK_PARSE(node, 1, "0"); p = ec_node_parse(node, "1"); - s = ec_strvec_val(ec_parsed_strvec(p), 0); + s = ec_strvec_val(ec_parse_strvec(p), 0); testres |= EC_TEST_CHECK(s != NULL && ec_node_uint_getval(node, s, &u64) == 0 && u64 == 1, "bad integer value"); - ec_parsed_free(p); + ec_parse_free(p); p = ec_node_parse(node, "10"); - s = ec_strvec_val(ec_parsed_strvec(p), 0); + s = ec_strvec_val(ec_parse_strvec(p), 0); testres |= EC_TEST_CHECK(s != NULL && ec_node_uint_getval(node, s, &u64) == 0 && u64 == 10, "bad integer value"); - ec_parsed_free(p); + ec_parse_free(p); ec_node_free(node); node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16); @@ -388,11 +388,11 @@ static int ec_node_int_testcase(void) testres |= EC_TEST_CHECK_PARSE(node, 1, "-2"); p = ec_node_parse(node, "10"); - s = ec_strvec_val(ec_parsed_strvec(p), 0); + s = ec_strvec_val(ec_parse_strvec(p), 0); testres |= EC_TEST_CHECK(s != NULL && ec_node_int_getval(node, s, &i64) == 0 && i64 == 16, "bad integer value"); - ec_parsed_free(p); + ec_parse_free(p); ec_node_free(node); node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10); diff --git a/lib/ecoli_node_many.c b/lib/ecoli_node_many.c index 7c9a50a..7748045 100644 --- a/lib/ecoli_node_many.c +++ b/lib/ecoli_node_many.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include @@ -30,11 +30,11 @@ struct ec_node_many { }; static int ec_node_many_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_many *node = (struct ec_node_many *)gen_node; - struct ec_parsed *child_parsed; + struct ec_parse *child_parse; struct ec_strvec *childvec = NULL; size_t off = 0, count; int ret; @@ -54,14 +54,14 @@ static int ec_node_many_parse(const struct ec_node *gen_node, ec_strvec_free(childvec); childvec = NULL; - if (ret == EC_PARSED_NOMATCH) + if (ret == EC_PARSE_NOMATCH) break; /* it matches an empty strvec, no need to continue */ if (ret == 0) { - child_parsed = ec_parsed_get_last_child(state); - ec_parsed_unlink_child(state, child_parsed); - ec_parsed_free(child_parsed); + child_parse = ec_parse_get_last_child(state); + ec_parse_unlink_child(state, child_parse); + ec_parse_free(child_parse); break; } @@ -69,8 +69,8 @@ static int ec_node_many_parse(const struct ec_node *gen_node, } if (count < node->min) { - ec_parsed_free_children(state); - return EC_PARSED_NOMATCH; + ec_parse_free_children(state); + return EC_PARSE_NOMATCH; } return off; @@ -85,7 +85,7 @@ __ec_node_many_complete(struct ec_node_many *node, unsigned int max, struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_parsed *parsed = ec_comp_get_state(comp); + struct ec_parse *parse = ec_comp_get_state(comp); struct ec_strvec *childvec = NULL; unsigned int i; int ret; @@ -110,7 +110,7 @@ __ec_node_many_complete(struct ec_node_many *node, unsigned int max, if (childvec == NULL) goto fail; - ret = ec_node_parse_child(node->child, parsed, childvec); + ret = ec_node_parse_child(node->child, parse, childvec); if (ret < 0) goto fail; @@ -118,19 +118,19 @@ __ec_node_many_complete(struct ec_node_many *node, unsigned int max, childvec = NULL; if ((unsigned int)ret != i) { - if (ret != EC_PARSED_NOMATCH) - ec_parsed_del_last_child(parsed); + if (ret != EC_PARSE_NOMATCH) + ec_parse_del_last_child(parse); continue; } childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i); if (childvec == NULL) { - ec_parsed_del_last_child(parsed); + ec_parse_del_last_child(parse); goto fail; } ret = __ec_node_many_complete(node, max, comp, childvec); - ec_parsed_del_last_child(parsed); + ec_parse_del_last_child(parse); ec_strvec_free(childvec); childvec = NULL; diff --git a/lib/ecoli_node_none.c b/lib/ecoli_node_none.c index be1558a..dba9ebf 100644 --- a/lib/ecoli_node_none.c +++ b/lib/ecoli_node_none.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include @@ -23,14 +23,14 @@ struct ec_node_none { }; static int ec_node_none_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { (void)gen_node; (void)state; (void)strvec; - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; } static int diff --git a/lib/ecoli_node_once.c b/lib/ecoli_node_once.c index 6052f6a..8218a29 100644 --- a/lib/ecoli_node_once.c +++ b/lib/ecoli_node_once.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include @@ -29,18 +29,18 @@ struct ec_node_once { }; static unsigned int -count_node(struct ec_parsed *parsed, const struct ec_node *node) +count_node(struct ec_parse *parse, const struct ec_node *node) { - struct ec_parsed *child; + struct ec_parse *child; unsigned int count = 0; - if (parsed == NULL) + if (parse == NULL) return 0; - if (ec_parsed_get_node(parsed) == node) + if (ec_parse_get_node(parse) == node) count++; - EC_PARSED_FOREACH_CHILD(child, parsed) + EC_PARSE_FOREACH_CHILD(child, parse) count += count_node(child, node); return count; @@ -48,7 +48,7 @@ count_node(struct ec_parsed *parsed, const struct ec_node *node) static int ec_node_once_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_once *node = (struct ec_node_once *)gen_node; @@ -57,9 +57,9 @@ ec_node_once_parse(const struct ec_node *gen_node, /* count the number of occurences of the node: if already parsed, * do not match */ - count = count_node(ec_parsed_get_root(state), node->child); + count = count_node(ec_parse_get_root(state), node->child); if (count > 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; return ec_node_parse_child(node->child, state, strvec); } @@ -70,14 +70,14 @@ ec_node_once_complete(const struct ec_node *gen_node, const struct ec_strvec *strvec) { struct ec_node_once *node = (struct ec_node_once *)gen_node; - struct ec_parsed *parsed = ec_comp_get_state(comp); + struct ec_parse *parse = ec_comp_get_state(comp); unsigned int count; int ret; /* count the number of occurences of the node: if already parsed, * do not match */ - count = count_node(ec_parsed_get_root(parsed), node->child); + count = count_node(ec_parse_get_root(parse), node->child); if (count > 0) return 0; diff --git a/lib/ecoli_node_option.c b/lib/ecoli_node_option.c index b454de2..7509251 100644 --- a/lib/ecoli_node_option.c +++ b/lib/ecoli_node_option.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include @@ -28,7 +28,7 @@ struct ec_node_option { static int ec_node_option_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_option *node = (struct ec_node_option *)gen_node; @@ -38,7 +38,7 @@ ec_node_option_parse(const struct ec_node *gen_node, if (ret < 0) return ret; - if (ret == EC_PARSED_NOMATCH) + if (ret == EC_PARSE_NOMATCH) return 0; return ret; diff --git a/lib/ecoli_node_or.c b/lib/ecoli_node_or.c index b1af790..c72642c 100644 --- a/lib/ecoli_node_or.c +++ b/lib/ecoli_node_or.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include @@ -29,7 +29,7 @@ struct ec_node_or { static int ec_node_or_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_or *node = (struct ec_node_or *)gen_node; @@ -38,12 +38,12 @@ ec_node_or_parse(const struct ec_node *gen_node, for (i = 0; i < node->len; i++) { ret = ec_node_parse_child(node->table[i], state, strvec); - if (ret == EC_PARSED_NOMATCH) + if (ret == EC_PARSE_NOMATCH) continue; return ret; } - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; } static int diff --git a/lib/ecoli_node_re.c b/lib/ecoli_node_re.c index 2a42fa2..ee381d2 100644 --- a/lib/ecoli_node_re.c +++ b/lib/ecoli_node_re.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include @@ -27,7 +27,7 @@ struct ec_node_re { static int ec_node_re_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_re *node = (struct ec_node_re *)gen_node; @@ -37,13 +37,13 @@ ec_node_re_parse(const struct ec_node *gen_node, (void)state; if (ec_strvec_len(strvec) == 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; str = ec_strvec_val(strvec, 0); if (regexec(&node->re, str, 1, &pos, 0) != 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; if (pos.rm_so != 0 || pos.rm_eo != (int)strlen(str)) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; return 1; } diff --git a/lib/ecoli_node_re_lex.c b/lib/ecoli_node_re_lex.c index 91ddeed..a74685f 100644 --- a/lib/ecoli_node_re_lex.c +++ b/lib/ecoli_node_re_lex.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -97,12 +97,12 @@ fail: static int ec_node_re_lex_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node; struct ec_strvec *new_vec = NULL; - struct ec_parsed *child_parsed; + struct ec_parse *child_parse; const char *str; int ret; @@ -123,11 +123,11 @@ ec_node_re_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_unlink_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); diff --git a/lib/ecoli_node_seq.c b/lib/ecoli_node_seq.c index ff370aa..164063e 100644 --- a/lib/ecoli_node_seq.c +++ b/lib/ecoli_node_seq.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -33,7 +33,7 @@ struct ec_node_seq { static int ec_node_seq_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_seq *node = (struct ec_node_seq *)gen_node; @@ -57,9 +57,9 @@ ec_node_seq_parse(const struct ec_node *gen_node, ec_strvec_free(childvec); childvec = NULL; - if (ret == EC_PARSED_NOMATCH) { - ec_parsed_free_children(state); - return EC_PARSED_NOMATCH; + if (ret == EC_PARSE_NOMATCH) { + ec_parse_free_children(state); + return EC_PARSE_NOMATCH; } len += ret; @@ -77,7 +77,7 @@ __ec_node_seq_complete(struct ec_node **table, size_t table_len, struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_parsed *parsed = ec_comp_get_state(comp); + struct ec_parse *parse = ec_comp_get_state(comp); struct ec_strvec *childvec = NULL; unsigned int i; int ret; @@ -108,7 +108,7 @@ __ec_node_seq_complete(struct ec_node **table, size_t table_len, if (childvec == NULL) goto fail; - ret = ec_node_parse_child(table[0], parsed, childvec); + ret = ec_node_parse_child(table[0], parse, childvec); if (ret < 0) goto fail; @@ -116,21 +116,21 @@ __ec_node_seq_complete(struct ec_node **table, size_t table_len, childvec = NULL; if ((unsigned int)ret != i) { - if (ret != EC_PARSED_NOMATCH) - ec_parsed_del_last_child(parsed); + if (ret != EC_PARSE_NOMATCH) + ec_parse_del_last_child(parse); continue; } childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i); if (childvec == NULL) { - ec_parsed_del_last_child(parsed); + ec_parse_del_last_child(parse); goto fail; } ret = __ec_node_seq_complete(&table[1], table_len - 1, comp, childvec); - ec_parsed_del_last_child(parsed); + ec_parse_del_last_child(parse); ec_strvec_free(childvec); childvec = NULL; diff --git a/lib/ecoli_node_sh_lex.c b/lib/ecoli_node_sh_lex.c index 82da5d7..c3a79ab 100644 --- a/lib/ecoli_node_sh_lex.c +++ b/lib/ecoli_node_sh_lex.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -214,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; @@ -231,7 +231,7 @@ ec_node_sh_lex_parse(const struct ec_node *gen_node, } if (new_vec == NULL) { if (errno == EINVAL) - ret = EC_PARSED_NOMATCH; + ret = EC_PARSE_NOMATCH; else ret = -ENOMEM; goto fail; @@ -243,11 +243,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_unlink_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); diff --git a/lib/ecoli_node_space.c b/lib/ecoli_node_space.c index 3f34629..761ed76 100644 --- a/lib/ecoli_node_space.c +++ b/lib/ecoli_node_space.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include @@ -24,7 +24,7 @@ struct ec_node_space { static int ec_node_space_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { const char *str; @@ -34,13 +34,13 @@ ec_node_space_parse(const struct ec_node *gen_node, (void)gen_node; if (ec_strvec_len(strvec) == 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; str = ec_strvec_val(strvec, 0); while (isspace(str[len])) len++; if (len == 0 || len != strlen(str)) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; return 1; } diff --git a/lib/ecoli_node_str.c b/lib/ecoli_node_str.c index fdb277e..6b11ebc 100644 --- a/lib/ecoli_node_str.c +++ b/lib/ecoli_node_str.c @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include @@ -26,7 +26,7 @@ struct ec_node_str { static int ec_node_str_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_str *node = (struct ec_node_str *)gen_node; @@ -35,11 +35,11 @@ ec_node_str_parse(const struct ec_node *gen_node, (void)state; if (ec_strvec_len(strvec) == 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; str = ec_strvec_val(strvec, 0); if (strcmp(str, node->string) != 0) - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; return 1; } @@ -64,7 +64,7 @@ ec_node_str_complete(const struct ec_node *gen_node, /* no completion */ if (str[n] != '\0') - return EC_PARSED_NOMATCH; + return EC_PARSE_NOMATCH; if (ec_comp_add_item(comp, gen_node, NULL, EC_COMP_FULL, str, node->string) < 0) diff --git a/lib/ecoli_node_subset.c b/lib/ecoli_node_subset.c index 756def1..62a2871 100644 --- a/lib/ecoli_node_subset.c +++ b/lib/ecoli_node_subset.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include @@ -30,7 +30,7 @@ struct ec_node_subset { }; struct parse_result { - size_t parsed_len; /* number of parsed node */ + size_t parse_len; /* number of parsed nodes */ size_t len; /* consumed strings */ }; @@ -38,14 +38,14 @@ struct parse_result { * updated accordingly. */ static int __ec_node_subset_parse(struct parse_result *out, struct ec_node **table, - size_t table_len, struct ec_parsed *state, + size_t table_len, struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node **child_table; struct ec_strvec *childvec = NULL; size_t i, j, len = 0; struct parse_result best_result, result; - struct ec_parsed *best_parsed = NULL; + struct ec_parse *best_parse = NULL; int ret; if (table_len == 0) @@ -65,7 +65,7 @@ __ec_node_subset_parse(struct parse_result *out, struct ec_node **table, if (ret < 0) goto fail; - if (ret == EC_PARSED_NOMATCH) + if (ret == EC_PARSE_NOMATCH) continue; /* build a new table without elt i */ @@ -94,18 +94,18 @@ __ec_node_subset_parse(struct parse_result *out, struct ec_node **table, goto fail; /* if result is not the best, ignore */ - if (result.parsed_len < best_result.parsed_len) { + if (result.parse_len < best_result.parse_len) { memset(&result, 0, sizeof(result)); - ec_parsed_del_last_child(state); + ec_parse_del_last_child(state); continue; } /* replace the previous best result */ - ec_parsed_free(best_parsed); - best_parsed = ec_parsed_get_last_child(state); - ec_parsed_unlink_child(state, best_parsed); + ec_parse_free(best_parse); + best_parse = ec_parse_get_last_child(state); + ec_parse_unlink_child(state, best_parse); - best_result.parsed_len = result.parsed_len + 1; + best_result.parse_len = result.parse_len + 1; best_result.len = len + result.len; memset(&result, 0, sizeof(result)); @@ -113,13 +113,13 @@ __ec_node_subset_parse(struct parse_result *out, struct ec_node **table, *out = best_result; ec_free(child_table); - if (best_parsed != NULL) - ec_parsed_link_child(state, best_parsed); + if (best_parse != NULL) + ec_parse_link_child(state, best_parse); return 0; fail: - ec_parsed_free(best_parsed); + ec_parse_free(best_parse); ec_strvec_free(childvec); ec_free(child_table); return ret; @@ -127,11 +127,11 @@ __ec_node_subset_parse(struct parse_result *out, struct ec_node **table, static int ec_node_subset_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_subset *node = (struct ec_node_subset *)gen_node; - struct ec_parsed *parsed = NULL; + struct ec_parse *parse = NULL; struct parse_result result; int ret; @@ -143,13 +143,13 @@ ec_node_subset_parse(const struct ec_node *gen_node, goto fail; /* if no child node matches, return a matching empty strvec */ - if (result.parsed_len == 0) + if (result.parse_len == 0) return 0; return result.len; fail: - ec_parsed_free(parsed); + ec_parse_free(parse); return ret; } @@ -158,7 +158,7 @@ __ec_node_subset_complete(struct ec_node **table, size_t table_len, struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_parsed *parsed = ec_comp_get_state(comp); + struct ec_parse *parse = ec_comp_get_state(comp); struct ec_strvec *childvec = NULL; struct ec_node *save; size_t i, len; @@ -190,18 +190,18 @@ __ec_node_subset_complete(struct ec_node **table, size_t table_len, if (table[i] == NULL) continue; - ret = ec_node_parse_child(table[i], parsed, strvec); + ret = ec_node_parse_child(table[i], parse, strvec); if (ret < 0) goto fail; - if (ret == EC_PARSED_NOMATCH) + if (ret == EC_PARSE_NOMATCH) continue; len = ret; childvec = ec_strvec_ndup(strvec, len, ec_strvec_len(strvec) - len); if (childvec == NULL) { - ec_parsed_del_last_child(parsed); + ec_parse_del_last_child(parse); goto fail; } @@ -212,7 +212,7 @@ __ec_node_subset_complete(struct ec_node **table, size_t table_len, table[i] = save; ec_strvec_free(childvec); childvec = NULL; - ec_parsed_del_last_child(parsed); + ec_parse_del_last_child(parse); if (ret < 0) goto fail; diff --git a/lib/ecoli_node_weakref.c b/lib/ecoli_node_weakref.c index fad8e82..0a097aa 100644 --- a/lib/ecoli_node_weakref.c +++ b/lib/ecoli_node_weakref.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include @@ -32,7 +32,7 @@ struct ec_node_weakref { static int ec_node_weakref_parse(const struct ec_node *gen_node, - struct ec_parsed *state, + struct ec_parse *state, const struct ec_strvec *strvec) { struct ec_node_weakref *node = (struct ec_node_weakref *)gen_node; diff --git a/lib/ecoli_parse.c b/lib/ecoli_parse.c new file mode 100644 index 0000000..7f36226 --- /dev/null +++ b/lib/ecoli_parse.c @@ -0,0 +1,423 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2016, Olivier MATZ + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +TAILQ_HEAD(ec_parse_list, ec_parse); + +struct ec_parse { + TAILQ_ENTRY(ec_parse) next; + struct ec_parse_list children; + struct ec_parse *parent; + const struct ec_node *node; + struct ec_strvec *strvec; + struct ec_keyval *attrs; +}; + +static int __ec_node_parse_child(const struct ec_node *node, + struct ec_parse *state, + bool is_root, const struct ec_strvec *strvec) +{ + struct ec_strvec *match_strvec; + struct ec_parse *child = NULL; + int ret; + + if (node->type->parse == NULL) + return -ENOTSUP; + + if (!is_root) { + child = ec_parse(node); + if (child == NULL) + return -ENOMEM; + + ec_parse_link_child(state, child); + } else { + child = state; + } + ret = node->type->parse(node, child, strvec); + if (ret < 0 || ret == EC_PARSE_NOMATCH) + goto free; + + match_strvec = ec_strvec_ndup(strvec, 0, ret); + if (match_strvec == NULL) { + ret = -ENOMEM; + goto free; + } + + child->strvec = match_strvec; + + return ret; + +free: + if (!is_root) { + ec_parse_unlink_child(state, child); + ec_parse_free(child); + } + return ret; +} + +int ec_node_parse_child(const struct ec_node *node, struct ec_parse *state, + const struct ec_strvec *strvec) +{ + assert(state != NULL); + return __ec_node_parse_child(node, state, false, strvec); +} + +struct ec_parse *ec_node_parse_strvec(const struct ec_node *node, + const struct ec_strvec *strvec) +{ + struct ec_parse *parse = ec_parse(node); + int ret; + + if (parse == NULL) + return NULL; + + ret = __ec_node_parse_child(node, parse, true, strvec); + if (ret < 0) { + ec_parse_free(parse); + return NULL; + } + + return parse; +} + +struct ec_parse *ec_node_parse(const struct ec_node *node, const char *str) +{ + struct ec_strvec *strvec = NULL; + struct ec_parse *parse = NULL; + + errno = ENOMEM; + strvec = ec_strvec(); + if (strvec == NULL) + goto fail; + + if (ec_strvec_add(strvec, str) < 0) + goto fail; + + parse = ec_node_parse_strvec(node, strvec); + if (parse == NULL) + goto fail; + + ec_strvec_free(strvec); + return parse; + + fail: + ec_strvec_free(strvec); + ec_parse_free(parse); + return NULL; +} + +struct ec_parse *ec_parse(const struct ec_node *node) +{ + struct ec_parse *parse = NULL; + + parse = ec_calloc(1, sizeof(*parse)); + if (parse == NULL) + goto fail; + + TAILQ_INIT(&parse->children); + + parse->node = node; + parse->attrs = ec_keyval(); + if (parse->attrs == NULL) + goto fail; + + return parse; + + fail: + if (parse != NULL) + ec_keyval_free(parse->attrs); + ec_free(parse); + + return NULL; +} + +static struct ec_parse * +__ec_parse_dup(const struct ec_parse *root, const struct ec_parse *ref, + struct ec_parse **new_ref) +{ + struct ec_parse *dup = NULL; + struct ec_parse *child, *dup_child; + struct ec_keyval *attrs = NULL; + + if (root == NULL) + return NULL; + + dup = ec_parse(root->node); + if (dup == NULL) + return NULL; + + if (root == ref) + *new_ref = dup; + + attrs = ec_keyval_dup(root->attrs); + if (attrs == NULL) + goto fail; + ec_keyval_free(dup->attrs); + dup->attrs = attrs; + + if (root->strvec != NULL) { + dup->strvec = ec_strvec_dup(root->strvec); + if (dup->strvec == NULL) + goto fail; + } + + TAILQ_FOREACH(child, &root->children, next) { + dup_child = __ec_parse_dup(child, ref, new_ref); + if (dup_child == NULL) + goto fail; + ec_parse_link_child(dup, dup_child); + } + + return dup; + +fail: + ec_parse_free(dup); + return NULL; +} + +struct ec_parse *ec_parse_dup(const struct ec_parse *parse) +{ + const struct ec_parse *root; + struct ec_parse *dup_root, *dup = NULL; + + root = ec_parse_get_root(parse); + dup_root = __ec_parse_dup(root, parse, &dup); + if (dup_root == NULL) + return NULL; + assert(dup != NULL); + + return dup; +} + +void ec_parse_free_children(struct ec_parse *parse) +{ + struct ec_parse *child; + + if (parse == NULL) + return; + + while (!TAILQ_EMPTY(&parse->children)) { + child = TAILQ_FIRST(&parse->children); + TAILQ_REMOVE(&parse->children, child, next); + child->parent = NULL; + ec_parse_free(child); + } +} + +void ec_parse_free(struct ec_parse *parse) +{ + if (parse == NULL) + return; + + ec_assert_print(parse->parent == NULL, + "parent not NULL in ec_parse_free()"); + + ec_parse_free_children(parse); + ec_strvec_free(parse->strvec); + ec_keyval_free(parse->attrs); + ec_free(parse); +} + +static void __ec_parse_dump(FILE *out, + const struct ec_parse *parse, size_t indent) +{ + struct ec_parse *child; + const struct ec_strvec *vec; + const char *id, *typename = "none"; + + /* node can be null when parsing is incomplete */ + if (parse->node != NULL) { + id = parse->node->id; + typename = parse->node->type->name; + } + + fprintf(out, "%*s" "type=%s id=%s vec=", + (int)indent * 4, "", typename, id); + vec = ec_parse_strvec(parse); + ec_strvec_dump(out, vec); + + TAILQ_FOREACH(child, &parse->children, next) + __ec_parse_dump(out, child, indent + 1); +} + +void ec_parse_dump(FILE *out, const struct ec_parse *parse) +{ + fprintf(out, "------------------- parse dump:\n"); + + if (parse == NULL) { + fprintf(out, "parse is NULL\n"); + return; + } + + /* only exist if it does not match (strvec == NULL) and if it + * does not have children: an incomplete parse, like those + * generated by complete() don't match but have children that + * may match. */ + if (!ec_parse_matches(parse) && TAILQ_EMPTY(&parse->children)) { + fprintf(out, "no match\n"); + return; + } + + __ec_parse_dump(out, parse, 0); +} + +void ec_parse_link_child(struct ec_parse *parse, + struct ec_parse *child) +{ + TAILQ_INSERT_TAIL(&parse->children, child, next); + child->parent = parse; +} + +void ec_parse_unlink_child(struct ec_parse *parse, + struct ec_parse *child) +{ + TAILQ_REMOVE(&parse->children, child, next); + child->parent = NULL; +} + +struct ec_parse * +ec_parse_get_first_child(const struct ec_parse *parse) +{ + return TAILQ_FIRST(&parse->children); +} + +struct ec_parse * +ec_parse_get_last_child(const struct ec_parse *parse) +{ + return TAILQ_LAST(&parse->children, ec_parse_list); +} + +struct ec_parse *ec_parse_get_next(const struct ec_parse *parse) +{ + return TAILQ_NEXT(parse, next); +} + +bool ec_parse_has_child(const struct ec_parse *parse) +{ + return !TAILQ_EMPTY(&parse->children); +} + +const struct ec_node *ec_parse_get_node(const struct ec_parse *parse) +{ + return parse->node; +} + +void ec_parse_del_last_child(struct ec_parse *parse) +{ + struct ec_parse *child; + + child = ec_parse_get_last_child(parse); + ec_parse_unlink_child(parse, child); + ec_parse_free(child); +} + +struct ec_parse *__ec_parse_get_root(struct ec_parse *parse) +{ + if (parse == NULL) + return NULL; + + while (parse->parent != NULL) + parse = parse->parent; + + return parse; +} + +struct ec_parse *ec_parse_get_parent(const struct ec_parse *parse) +{ + if (parse == NULL) + return NULL; + + return parse->parent; +} + +struct ec_parse *ec_parse_iter_next(struct ec_parse *parse) +{ + struct ec_parse *child, *parent, *next; + + child = TAILQ_FIRST(&parse->children); + if (child != NULL) + return child; + parent = parse->parent; + while (parent != NULL) { + next = TAILQ_NEXT(parse, next); + if (next != NULL) + return next; + parse = parent; + parent = parse->parent; + } + return NULL; +} + +struct ec_parse *ec_parse_find_first(struct ec_parse *parse, + const char *id) +{ + struct ec_parse *child, *ret; + + if (parse == NULL) + return NULL; + + if (parse->node != NULL && + parse->node->id != NULL && + !strcmp(parse->node->id, id)) + return parse; + + TAILQ_FOREACH(child, &parse->children, next) { + ret = ec_parse_find_first(child, id); + if (ret != NULL) + return ret; + } + + return NULL; +} + +struct ec_keyval * +ec_parse_get_attrs(struct ec_parse *parse) +{ + if (parse == NULL) + return NULL; + + return parse->attrs; +} + +const struct ec_strvec *ec_parse_strvec(const struct ec_parse *parse) +{ + if (parse == NULL || parse->strvec == NULL) + return NULL; + + return parse->strvec; +} + +/* number of strings in the parse vector */ +size_t ec_parse_len(const struct ec_parse *parse) +{ + if (parse == NULL || parse->strvec == NULL) + return 0; + + return ec_strvec_len(parse->strvec); +} + +size_t ec_parse_matches(const struct ec_parse *parse) +{ + if (parse == NULL) + return 0; + + if (parse->strvec == NULL) + return 0; + + return 1; +} diff --git a/lib/ecoli_parse.h b/lib/ecoli_parse.h new file mode 100644 index 0000000..0559214 --- /dev/null +++ b/lib/ecoli_parse.h @@ -0,0 +1,237 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2016, Olivier MATZ + */ + +/** + * Node parse API. + * + * The parse operation is to check if an input (a string or vector of + * strings) matches the node tree. On success, the result is stored in a + * tree that describes which part of the input matches which node. + */ + +#ifndef ECOLI_PARSE_ +#define ECOLI_PARSE_ + +#include +#include +#include +#include + +struct ec_node; +struct ec_parse; + +/** + * Create an empty parse tree. + * + * @return + * The empty parse tree. + */ +struct ec_parse *ec_parse(const struct ec_node *node); + +/** + * + * + * + */ +void ec_parse_free(struct ec_parse *parse); + +/** + * + * + * + */ +void ec_parse_free_children(struct ec_parse *parse); + +/** + * + * + * + */ +struct ec_parse *ec_parse_dup(const struct ec_parse *parse); + +/** + * + * + * + */ +const struct ec_strvec *ec_parse_strvec(const struct ec_parse *parse); + +/* a NULL return value is an error, with errno set + ENOTSUP: no ->parse() operation +*/ +/** + * + * + * + */ +struct ec_parse *ec_node_parse(const struct ec_node *node, const char *str); + +/** + * + * + * + */ +struct ec_parse *ec_node_parse_strvec(const struct ec_node *node, + const struct ec_strvec *strvec); + +/** + * + * + * + */ +#define EC_PARSE_NOMATCH INT_MAX + +/* internal: used by nodes + * + * state is the current parse tree, which is built piece by piece while + * parsing the node tree: ec_node_parse_child() creates a new child in + * this state parse tree, and calls the parse() method for the child + * node, with state pointing to this new child. If it does not match, + * the child is removed in the state, else it is kept, with its + * possible descendants. + * + * return: + * EC_PARSE_NOMATCH (positive) if it does not match + * any other negative value (-errno) for other errors + * the number of matched strings in strvec + */ +int ec_node_parse_child(const struct ec_node *node, + struct ec_parse *state, + const struct ec_strvec *strvec); + +/** + * + * + * + */ +void ec_parse_link_child(struct ec_parse *parse, + struct ec_parse *child); +/** + * + * + * + */ +void ec_parse_unlink_child(struct ec_parse *parse, + struct ec_parse *child); + +/* keep the const */ +#define ec_parse_get_root(parse) ({ \ + const struct ec_parse *p_ = parse; /* check type */ \ + struct ec_parse *parse_ = (struct ec_parse *)parse; \ + typeof(parse) res_; \ + (void)p_; \ + res_ = __ec_parse_get_root(parse_); \ + res_; \ +}) + +/** + * + * + * + */ +struct ec_parse *__ec_parse_get_root(struct ec_parse *parse); + +/** + * + * + * + */ +struct ec_parse *ec_parse_get_parent(const struct ec_parse *parse); + +/** + * Get the first child of a tree. + * + */ +struct ec_parse *ec_parse_get_first_child(const struct ec_parse *parse); + +/** + * + * + * + */ +struct ec_parse *ec_parse_get_last_child(const struct ec_parse *parse); + +/** + * + * + * + */ +struct ec_parse *ec_parse_get_next(const struct ec_parse *parse); + +/** + * + * + * + */ +#define EC_PARSE_FOREACH_CHILD(child, parse) \ + for (child = ec_parse_get_first_child(parse); \ + child != NULL; \ + child = ec_parse_get_next(child)) \ + +/** + * + * + * + */ +bool ec_parse_has_child(const struct ec_parse *parse); + +/** + * + * + * + */ +const struct ec_node *ec_parse_get_node(const struct ec_parse *parse); + +/** + * + * + * + */ +void ec_parse_del_last_child(struct ec_parse *parse); + +/** + * + * + * + */ +struct ec_keyval *ec_parse_get_attrs(struct ec_parse *parse); + +/** + * + * + * + */ +void ec_parse_dump(FILE *out, const struct ec_parse *parse); + +/** + * + * + * + */ +struct ec_parse *ec_parse_find_first(struct ec_parse *parse, + const char *id); + +/** + * Iterate among parse tree + * + * Use it with: + * for (iter = state; iter != NULL; iter = ec_parse_iter_next(iter)) + */ +struct ec_parse *ec_parse_iter_next(struct ec_parse *parse); + +/** + * + * + * + */ +size_t ec_parse_len(const struct ec_parse *parse); + +/** + * + * + * + */ +size_t ec_parse_matches(const struct ec_parse *parse); + +#endif diff --git a/lib/ecoli_parsed.c b/lib/ecoli_parsed.c deleted file mode 100644 index c2e7c8e..0000000 --- a/lib/ecoli_parsed.c +++ /dev/null @@ -1,423 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * Copyright 2016, Olivier MATZ - */ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -TAILQ_HEAD(ec_parsed_list, ec_parsed); - -struct ec_parsed { - TAILQ_ENTRY(ec_parsed) next; - struct ec_parsed_list children; - struct ec_parsed *parent; - const struct ec_node *node; - struct ec_strvec *strvec; - struct ec_keyval *attrs; -}; - -static int __ec_node_parse_child(const struct ec_node *node, - struct ec_parsed *state, - bool is_root, const struct ec_strvec *strvec) -{ - struct ec_strvec *match_strvec; - struct ec_parsed *child = NULL; - int ret; - - if (node->type->parse == NULL) - return -ENOTSUP; - - if (!is_root) { - child = ec_parsed(node); - if (child == NULL) - return -ENOMEM; - - ec_parsed_link_child(state, child); - } else { - child = state; - } - ret = node->type->parse(node, child, strvec); - if (ret < 0 || ret == EC_PARSED_NOMATCH) - goto free; - - match_strvec = ec_strvec_ndup(strvec, 0, ret); - if (match_strvec == NULL) { - ret = -ENOMEM; - goto free; - } - - child->strvec = match_strvec; - - return ret; - -free: - if (!is_root) { - ec_parsed_unlink_child(state, child); - ec_parsed_free(child); - } - return ret; -} - -int ec_node_parse_child(const struct ec_node *node, struct ec_parsed *state, - const struct ec_strvec *strvec) -{ - assert(state != NULL); - return __ec_node_parse_child(node, state, false, strvec); -} - -struct ec_parsed *ec_node_parse_strvec(const struct ec_node *node, - const struct ec_strvec *strvec) -{ - struct ec_parsed *parsed = ec_parsed(node); - int ret; - - if (parsed == NULL) - return NULL; - - ret = __ec_node_parse_child(node, parsed, true, strvec); - if (ret < 0) { - ec_parsed_free(parsed); - return NULL; - } - - return parsed; -} - -struct ec_parsed *ec_node_parse(const struct ec_node *node, const char *str) -{ - struct ec_strvec *strvec = NULL; - struct ec_parsed *parsed = NULL; - - errno = ENOMEM; - strvec = ec_strvec(); - if (strvec == NULL) - goto fail; - - if (ec_strvec_add(strvec, str) < 0) - goto fail; - - parsed = ec_node_parse_strvec(node, strvec); - if (parsed == NULL) - goto fail; - - ec_strvec_free(strvec); - return parsed; - - fail: - ec_strvec_free(strvec); - ec_parsed_free(parsed); - return NULL; -} - -struct ec_parsed *ec_parsed(const struct ec_node *node) -{ - struct ec_parsed *parsed = NULL; - - parsed = ec_calloc(1, sizeof(*parsed)); - if (parsed == NULL) - goto fail; - - TAILQ_INIT(&parsed->children); - - parsed->node = node; - parsed->attrs = ec_keyval(); - if (parsed->attrs == NULL) - goto fail; - - return parsed; - - fail: - if (parsed != NULL) - ec_keyval_free(parsed->attrs); - ec_free(parsed); - - return NULL; -} - -static struct ec_parsed * -__ec_parsed_dup(const struct ec_parsed *root, const struct ec_parsed *ref, - struct ec_parsed **new_ref) -{ - struct ec_parsed *dup = NULL; - struct ec_parsed *child, *dup_child; - struct ec_keyval *attrs = NULL; - - if (root == NULL) - return NULL; - - dup = ec_parsed(root->node); - if (dup == NULL) - return NULL; - - if (root == ref) - *new_ref = dup; - - attrs = ec_keyval_dup(root->attrs); - if (attrs == NULL) - goto fail; - ec_keyval_free(dup->attrs); - dup->attrs = attrs; - - if (root->strvec != NULL) { - dup->strvec = ec_strvec_dup(root->strvec); - if (dup->strvec == NULL) - goto fail; - } - - TAILQ_FOREACH(child, &root->children, next) { - dup_child = __ec_parsed_dup(child, ref, new_ref); - if (dup_child == NULL) - goto fail; - ec_parsed_link_child(dup, dup_child); - } - - return dup; - -fail: - ec_parsed_free(dup); - return NULL; -} - -struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed) -{ - const struct ec_parsed *root; - struct ec_parsed *dup_root, *dup = NULL; - - root = ec_parsed_get_root(parsed); - dup_root = __ec_parsed_dup(root, parsed, &dup); - if (dup_root == NULL) - return NULL; - assert(dup != NULL); - - return dup; -} - -void ec_parsed_free_children(struct ec_parsed *parsed) -{ - struct ec_parsed *child; - - if (parsed == NULL) - return; - - while (!TAILQ_EMPTY(&parsed->children)) { - child = TAILQ_FIRST(&parsed->children); - TAILQ_REMOVE(&parsed->children, child, next); - child->parent = NULL; - ec_parsed_free(child); - } -} - -void ec_parsed_free(struct ec_parsed *parsed) -{ - if (parsed == NULL) - return; - - ec_assert_print(parsed->parent == NULL, - "parent not NULL in ec_parsed_free()"); - - ec_parsed_free_children(parsed); - ec_strvec_free(parsed->strvec); - ec_keyval_free(parsed->attrs); - ec_free(parsed); -} - -static void __ec_parsed_dump(FILE *out, - const struct ec_parsed *parsed, size_t indent) -{ - struct ec_parsed *child; - const struct ec_strvec *vec; - const char *id, *typename = "none"; - - /* node can be null when parsing is incomplete */ - if (parsed->node != NULL) { - id = parsed->node->id; - typename = parsed->node->type->name; - } - - fprintf(out, "%*s" "type=%s id=%s vec=", - (int)indent * 4, "", typename, id); - vec = ec_parsed_strvec(parsed); - ec_strvec_dump(out, vec); - - TAILQ_FOREACH(child, &parsed->children, next) - __ec_parsed_dump(out, child, indent + 1); -} - -void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed) -{ - fprintf(out, "------------------- parsed dump:\n"); - - if (parsed == NULL) { - fprintf(out, "parsed is NULL, error in parse\n"); - return; - } - - /* only exist if it does not match (strvec == NULL) and if it - * does not have children: an incomplete parse, like those - * generated by complete() don't match but have children that - * may match. */ - if (!ec_parsed_matches(parsed) && TAILQ_EMPTY(&parsed->children)) { - fprintf(out, "no match\n"); - return; - } - - __ec_parsed_dump(out, parsed, 0); -} - -void ec_parsed_link_child(struct ec_parsed *parsed, - struct ec_parsed *child) -{ - TAILQ_INSERT_TAIL(&parsed->children, child, next); - child->parent = parsed; -} - -void ec_parsed_unlink_child(struct ec_parsed *parsed, - struct ec_parsed *child) -{ - TAILQ_REMOVE(&parsed->children, child, next); - child->parent = NULL; -} - -struct ec_parsed * -ec_parsed_get_first_child(const struct ec_parsed *parsed) -{ - return TAILQ_FIRST(&parsed->children); -} - -struct ec_parsed * -ec_parsed_get_last_child(const struct ec_parsed *parsed) -{ - return TAILQ_LAST(&parsed->children, ec_parsed_list); -} - -struct ec_parsed *ec_parsed_get_next(const struct ec_parsed *parsed) -{ - return TAILQ_NEXT(parsed, next); -} - -bool ec_parsed_has_child(const struct ec_parsed *parsed) -{ - return !TAILQ_EMPTY(&parsed->children); -} - -const struct ec_node *ec_parsed_get_node(const struct ec_parsed *parsed) -{ - return parsed->node; -} - -void ec_parsed_del_last_child(struct ec_parsed *parsed) -{ - struct ec_parsed *child; - - child = ec_parsed_get_last_child(parsed); - ec_parsed_unlink_child(parsed, child); - ec_parsed_free(child); -} - -struct ec_parsed *__ec_parsed_get_root(struct ec_parsed *parsed) -{ - if (parsed == NULL) - return NULL; - - while (parsed->parent != NULL) - parsed = parsed->parent; - - return parsed; -} - -struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed) -{ - if (parsed == NULL) - return NULL; - - return parsed->parent; -} - -struct ec_parsed *ec_parsed_iter_next(struct ec_parsed *parsed) -{ - struct ec_parsed *child, *parent, *next; - - child = TAILQ_FIRST(&parsed->children); - if (child != NULL) - return child; - parent = parsed->parent; - while (parent != NULL) { - next = TAILQ_NEXT(parsed, next); - if (next != NULL) - return next; - parsed = parent; - parent = parsed->parent; - } - return NULL; -} - -struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed, - const char *id) -{ - struct ec_parsed *child, *ret; - - if (parsed == NULL) - return NULL; - - if (parsed->node != NULL && - parsed->node->id != NULL && - !strcmp(parsed->node->id, id)) - return parsed; - - TAILQ_FOREACH(child, &parsed->children, next) { - ret = ec_parsed_find_first(child, id); - if (ret != NULL) - return ret; - } - - return NULL; -} - -struct ec_keyval * -ec_parsed_get_attrs(struct ec_parsed *parsed) -{ - if (parsed == NULL) - return NULL; - - return parsed->attrs; -} - -const struct ec_strvec *ec_parsed_strvec(const struct ec_parsed *parsed) -{ - if (parsed == NULL || parsed->strvec == NULL) - return NULL; - - return parsed->strvec; -} - -/* number of strings in the parsed vector */ -size_t ec_parsed_len(const struct ec_parsed *parsed) -{ - if (parsed == NULL || parsed->strvec == NULL) - return 0; - - return ec_strvec_len(parsed->strvec); -} - -size_t ec_parsed_matches(const struct ec_parsed *parsed) -{ - if (parsed == NULL) - return 0; - - if (parsed->strvec == NULL) - return 0; - - return 1; -} diff --git a/lib/ecoli_parsed.h b/lib/ecoli_parsed.h deleted file mode 100644 index 9f7a698..0000000 --- a/lib/ecoli_parsed.h +++ /dev/null @@ -1,237 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * Copyright 2016, Olivier MATZ - */ - -/** - * Node parse API. - * - * The parse operation is to check if an input (a string or vector of - * strings) matches the node tree. On success, the result is stored in a - * tree that describes which part of the input matches which node. - */ - -#ifndef ECOLI_PARSED_ -#define ECOLI_PARSED_ - -#include -#include -#include -#include - -struct ec_node; -struct ec_parsed; - -/** - * Create an empty parse tree. - * - * @return - * The empty parse tree. - */ -struct ec_parsed *ec_parsed(const struct ec_node *node); - -/** - * - * - * - */ -void ec_parsed_free(struct ec_parsed *parsed); - -/** - * - * - * - */ -void ec_parsed_free_children(struct ec_parsed *parsed); - -/** - * - * - * - */ -struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed); - -/** - * - * - * - */ -const struct ec_strvec *ec_parsed_strvec(const struct ec_parsed *parsed); - -/* a NULL return value is an error, with errno set - ENOTSUP: no ->parse() operation -*/ -/** - * - * - * - */ -struct ec_parsed *ec_node_parse(const struct ec_node *node, const char *str); - -/** - * - * - * - */ -struct ec_parsed *ec_node_parse_strvec(const struct ec_node *node, - const struct ec_strvec *strvec); - -/** - * - * - * - */ -#define EC_PARSED_NOMATCH INT_MAX - -/* internal: used by nodes - * - * state is the current parse tree, which is built piece by piece while - * parsing the node tree: ec_node_parse_child() creates a new child in - * this state parse tree, and calls the parse() method for the child - * node, with state pointing to this new child. If it does not match, - * the child is removed in the state, else it is kept, with its - * possible descendants. - * - * return: - * EC_PARSED_NOMATCH (positive) if it does not match - * any other negative value (-errno) for other errors - * the number of matched strings in strvec - */ -int ec_node_parse_child(const struct ec_node *node, - struct ec_parsed *state, - const struct ec_strvec *strvec); - -/** - * - * - * - */ -void ec_parsed_link_child(struct ec_parsed *parsed, - struct ec_parsed *child); -/** - * - * - * - */ -void ec_parsed_unlink_child(struct ec_parsed *parsed, - struct ec_parsed *child); - -/* keep the const */ -#define ec_parsed_get_root(parsed) ({ \ - const struct ec_parsed *p_ = parsed; /* check type */ \ - struct ec_parsed *parsed_ = (struct ec_parsed *)parsed; \ - typeof(parsed) res_; \ - (void)p_; \ - res_ = __ec_parsed_get_root(parsed_); \ - res_; \ -}) - -/** - * - * - * - */ -struct ec_parsed *__ec_parsed_get_root(struct ec_parsed *parsed); - -/** - * - * - * - */ -struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed); - -/** - * Get the first child of a tree. - * - */ -struct ec_parsed *ec_parsed_get_first_child(const struct ec_parsed *parsed); - -/** - * - * - * - */ -struct ec_parsed *ec_parsed_get_last_child(const struct ec_parsed *parsed); - -/** - * - * - * - */ -struct ec_parsed *ec_parsed_get_next(const struct ec_parsed *parsed); - -/** - * - * - * - */ -#define EC_PARSED_FOREACH_CHILD(child, parsed) \ - for (child = ec_parsed_get_first_child(parsed); \ - child != NULL; \ - child = ec_parsed_get_next(child)) \ - -/** - * - * - * - */ -bool ec_parsed_has_child(const struct ec_parsed *parsed); - -/** - * - * - * - */ -const struct ec_node *ec_parsed_get_node(const struct ec_parsed *parsed); - -/** - * - * - * - */ -void ec_parsed_del_last_child(struct ec_parsed *parsed); - -/** - * - * - * - */ -struct ec_keyval *ec_parsed_get_attrs(struct ec_parsed *parsed); - -/** - * - * - * - */ -void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed); - -/** - * - * - * - */ -struct ec_parsed *ec_parsed_find_first(struct ec_parsed *parsed, - const char *id); - -/** - * Iterate among parsed tree - * - * Use it with: - * for (iter = state; iter != NULL; iter = ec_parsed_iter_next(iter)) - */ -struct ec_parsed *ec_parsed_iter_next(struct ec_parsed *parsed); - -/** - * - * - * - */ -size_t ec_parsed_len(const struct ec_parsed *parsed); - -/** - * - * - * - */ -size_t ec_parsed_matches(const struct ec_parsed *parsed); - -#endif diff --git a/lib/ecoli_test.c b/lib/ecoli_test.c index 319e7e8..b090bd3 100644 --- a/lib/ecoli_test.c +++ b/lib/ecoli_test.c @@ -13,9 +13,9 @@ #include #include #include -#include +#include #include -#include +#include static struct ec_test_list test_list = TAILQ_HEAD_INITIALIZER(test_list); @@ -46,7 +46,7 @@ int ec_test_register(struct ec_test *test) int ec_test_check_parse(struct ec_node *tk, int expected, ...) { - struct ec_parsed *p; + struct ec_parse *p; struct ec_strvec *vec = NULL; const char *s; int ret = -1, match; @@ -71,21 +71,21 @@ int ec_test_check_parse(struct ec_node *tk, int expected, ...) p = ec_node_parse_strvec(tk, vec); if (p == NULL) { - EC_LOG(EC_LOG_ERR, "parsed is NULL\n"); + EC_LOG(EC_LOG_ERR, "parse is NULL\n"); } - if (ec_parsed_matches(p)) - match = ec_parsed_len(p); + if (ec_parse_matches(p)) + match = ec_parse_len(p); else match = -1; if (expected == match) { ret = 0; } else { EC_LOG(EC_LOG_ERR, - "tk parsed len (%d) does not match expected (%d)\n", + "parse len (%d) does not match expected (%d)\n", match, expected); } - ec_parsed_free(p); + ec_parse_free(p); out: ec_strvec_free(vec); diff --git a/lib/main-readline.c b/lib/main-readline.c index 0fb57ab..91a1e6e 100644 --- a/lib/main-readline.c +++ b/lib/main-readline.c @@ -13,7 +13,7 @@ #include #include -#include +#include #include #include #include @@ -106,7 +106,7 @@ static char **my_attempted_completion(const char *text, int start, int end) static char *get_node_help(const struct ec_comp_item *item) { const struct ec_comp_group *grp; - const struct ec_parsed *state; + const struct ec_parse *state; const struct ec_node *node; char *help = NULL; const char *node_help = NULL; @@ -115,8 +115,8 @@ static char *get_node_help(const struct ec_comp_item *item) grp = ec_comp_item_get_grp(item); state = grp->state; for (state = grp->state; state != NULL; - state = ec_parsed_get_parent(state)) { - node = ec_parsed_get_node(state); + state = ec_parse_get_parent(state)) { + node = ec_parse_get_node(state); if (node_help == NULL) node_help = ec_keyval_get(ec_node_attrs(node), "help"); if (node_desc == NULL) @@ -140,7 +140,7 @@ static int show_help(int ignore, int invoking_key) const struct ec_comp_group *grp, *prev_grp = NULL; const struct ec_comp_item *item; struct ec_comp *c; - struct ec_parsed *p; + struct ec_parse *p; char *line = NULL; unsigned int count; char **helps = NULL; @@ -156,9 +156,9 @@ static int show_help(int ignore, int invoking_key) /* check if the current line matches */ p = ec_node_parse(commands, line); - if (ec_parsed_matches(p)) + if (ec_parse_matches(p)) match = 1; - ec_parsed_free(p); + ec_parse_free(p); p = NULL; /* complete at current cursor position */ @@ -212,7 +212,7 @@ static int show_help(int ignore, int invoking_key) fail: ec_comp_iter_free(iter); - ec_parsed_free(p); + ec_parse_free(p); free(line); ec_comp_free(c); if (helps != NULL) { @@ -329,7 +329,7 @@ static int create_commands(void) int main(void) { - struct ec_parsed *p; + struct ec_parse *p; char *line; if (ec_init() < 0) { @@ -349,9 +349,9 @@ int main(void) break; p = ec_node_parse(commands, line); - ec_parsed_dump(stdout, p); + ec_parse_dump(stdout, p); add_history(line); - ec_parsed_free(p); + ec_parse_free(p); } -- 2.39.5