From ab698b70695b745e649e43ee07d4db257705a844 Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Thu, 1 Mar 2018 21:16:30 +0100 Subject: [PATCH] remove node build --- lib/ecoli_completed.c | 22 +------ lib/ecoli_node.h | 2 - lib/ecoli_node_cmd.c | 128 +++++++++++++++++++------------------ lib/ecoli_node_expr.c | 41 ++++-------- lib/ecoli_node_expr_test.c | 12 ++-- lib/ecoli_node_int.c | 18 +++--- lib/ecoli_node_once.c | 2 - lib/ecoli_node_or.c | 2 - lib/ecoli_node_seq.c | 2 - lib/ecoli_node_sh_lex.c | 5 +- lib/ecoli_node_subset.c | 2 - lib/ecoli_node_weakref.c | 2 - lib/ecoli_parsed.c | 10 --- lib/ecoli_test.c | 4 +- 14 files changed, 102 insertions(+), 150 deletions(-) diff --git a/lib/ecoli_completed.c b/lib/ecoli_completed.c index 944c566..089a2c7 100644 --- a/lib/ecoli_completed.c +++ b/lib/ecoli_completed.c @@ -84,23 +84,14 @@ struct ec_parsed *ec_completed_get_state(struct ec_completed *completed) } int -ec_node_complete_child(struct ec_node *node, struct ec_completed *completed, - const struct ec_strvec *strvec) +ec_node_complete_child(/* XXX const */struct ec_node *node, + struct ec_completed *completed, + const struct ec_strvec *strvec) { struct ec_parsed *child_state, *cur_state; struct ec_completed_group *cur_group; int ret; - /* build the node if required */ - if (node->type->build != NULL) { - if ((node->flags & EC_NODE_F_BUILT) == 0) { - ret = node->type->build(node); - if (ret < 0) - return ret; - } - } - node->flags |= EC_NODE_F_BUILT; - if (node->type->complete == NULL) return -ENOTSUP; @@ -131,13 +122,6 @@ ec_node_complete_child(struct ec_node *node, struct ec_completed *completed, if (ret < 0) return ret; -#if 0 // XXX dump - printf("----------------------------------------------------------\n"); - ec_node_dump(stdout, node); - ec_strvec_dump(stdout, strvec); - ec_completed_dump(stdout, completed); -#endif - return 0; } diff --git a/lib/ecoli_node.h b/lib/ecoli_node.h index d4f65f3..5a25a1f 100644 --- a/lib/ecoli_node.h +++ b/lib/ecoli_node.h @@ -156,8 +156,6 @@ struct ec_node { /* XXX ensure parent and child are properly set in all nodes */ struct ec_node *parent; unsigned int refcnt; -#define EC_NODE_F_BUILT 0x0001 /** set if configuration is built */ - unsigned int flags; TAILQ_ENTRY(ec_node) next; struct ec_node_list children; diff --git a/lib/ecoli_node_cmd.c b/lib/ecoli_node_cmd.c index dcaaa93..85b97b2 100644 --- a/lib/ecoli_node_cmd.c +++ b/lib/ecoli_node_cmd.c @@ -247,48 +247,19 @@ static const struct ec_node_expr_eval_ops test_ops = { .eval_free = ec_node_cmd_eval_free, }; -static int -ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state, - const struct ec_strvec *strvec) -{ - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; - - return ec_node_parse_child(node->cmd, state, strvec); -} - -static int -ec_node_cmd_complete(const struct ec_node *gen_node, - struct ec_completed *completed, - const struct ec_strvec *strvec) -{ - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; - - return ec_node_complete_child(node->cmd, completed, strvec); -} - -static void ec_node_cmd_free_priv(struct ec_node *gen_node) -{ - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; - unsigned int i; - - ec_free(node->cmd_str); - ec_node_free(node->cmd); - ec_node_free(node->expr); - ec_node_free(node->lex); - for (i = 0; i < node->len; i++) - ec_node_free(node->table[i]); - ec_free(node->table); -} - -static int ec_node_cmd_build(struct ec_node *gen_node) +static int ec_node_cmd_build(struct ec_node_cmd *node) { struct ec_node *expr = NULL, *lex = NULL, *cmd = NULL; struct ec_parsed *p, *child; - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; void *result; int ret; - /* XXX the expr parser can be moved in the node init */ + ec_node_free(node->expr); + node->expr = NULL; + ec_node_free(node->lex); + node->lex = NULL; + ec_node_free(node->cmd); + node->cmd = NULL; /* build the expression parser */ ret = -ENOMEM; @@ -373,11 +344,8 @@ static int ec_node_cmd_build(struct ec_node *gen_node) ec_parsed_free(p); p = NULL; - ec_node_free(node->expr); node->expr = expr; - ec_node_free(node->lex); node->lex = lex; - ec_node_free(node->cmd); node->cmd = cmd; return 0; @@ -390,9 +358,46 @@ fail: return ret; } +static int +ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parsed *state, + const struct ec_strvec *strvec) +{ + struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; + + if (node->cmd == NULL) + return -ENOENT; + return ec_node_parse_child(node->cmd, state, strvec); +} + +static int +ec_node_cmd_complete(const struct ec_node *gen_node, + struct ec_completed *completed, + const struct ec_strvec *strvec) +{ + struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; + + if (node->cmd == NULL) + return -ENOENT; + return ec_node_complete_child(node->cmd, completed, strvec); +} + +static void ec_node_cmd_free_priv(struct ec_node *gen_node) +{ + struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; + unsigned int i; + + ec_free(node->cmd_str); + ec_node_free(node->cmd); + ec_node_free(node->expr); + ec_node_free(node->lex); + for (i = 0; i < node->len; i++) + ec_node_free(node->table[i]); + ec_free(node->table); +} + + static struct ec_node_type ec_node_cmd_type = { .name = "cmd", - .build = ec_node_cmd_build, .parse = ec_node_cmd_parse, .complete = ec_node_cmd_complete, .size = sizeof(struct ec_node_cmd), @@ -405,6 +410,7 @@ int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child) { struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; struct ec_node **table; + int ret; // XXX check node type @@ -413,7 +419,11 @@ int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child) if (child == NULL) return -EINVAL; - gen_node->flags &= ~EC_NODE_F_BUILT; + if (node->cmd == NULL) { + ret = ec_node_cmd_build(node); + if (ret < 0) + return ret; + } table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table)); if (table == NULL) { @@ -431,42 +441,25 @@ int ec_node_cmd_add_child(struct ec_node *gen_node, struct ec_node *child) return 0; } -struct ec_node *ec_node_cmd(const char *id, const char *cmd_str) +struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...) { struct ec_node *gen_node = NULL; struct ec_node_cmd *node = NULL; + struct ec_node *child; + va_list ap; + int fail = 0; gen_node = __ec_node(&ec_node_cmd_type, id); if (gen_node == NULL) goto fail; node = (struct ec_node_cmd *)gen_node; - node->cmd_str = ec_strdup(cmd_str); + node->cmd_str = ec_strdup(cmd); if (node->cmd_str == NULL) goto fail; - return gen_node; - -fail: - ec_node_free(gen_node); - return NULL; -} - -struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...) -{ - struct ec_node *gen_node = NULL; - struct ec_node_cmd *node = NULL; - struct ec_node *child; - va_list ap; - int fail = 0; - va_start(ap, cmd); - gen_node = ec_node_cmd(id, cmd); - node = (struct ec_node_cmd *)gen_node; - if (node == NULL) - fail = 1;; - for (child = va_arg(ap, struct ec_node *); child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) { @@ -483,6 +476,10 @@ struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...) goto fail; va_end(ap); + + if (ec_node_cmd_build(node) < 0) + goto fail; + return gen_node; fail: @@ -491,6 +488,11 @@ fail: return NULL; } +struct ec_node *ec_node_cmd(const char *id, const char *cmd_str) +{ + return __ec_node_cmd(id, cmd_str, EC_NODE_ENDLIST); +} + /* LCOV_EXCL_START */ static int ec_node_cmd_testcase(void) { diff --git a/lib/ecoli_node_expr.c b/lib/ecoli_node_expr.c index 39f3b70..9fb48a7 100644 --- a/lib/ecoli_node_expr.c +++ b/lib/ecoli_node_expr.c @@ -74,6 +74,8 @@ static int ec_node_expr_parse(const struct ec_node *gen_node, { struct ec_node_expr *node = (struct ec_node_expr *)gen_node; + if (node->child == NULL) + return -ENOENT; return ec_node_parse_child(node->child, state, strvec); } @@ -84,6 +86,8 @@ ec_node_expr_complete(const struct ec_node *gen_node, { struct ec_node_expr *node = (struct ec_node_expr *)gen_node; + if (node->child == NULL) + return -ENOENT; return ec_node_complete_child(node->child, completed, strvec); } @@ -114,15 +118,17 @@ static void ec_node_expr_free_priv(struct ec_node *gen_node) ec_node_free(node->child); } -static int ec_node_expr_build(struct ec_node *gen_node) +static int ec_node_expr_build(struct ec_node_expr *node) { - struct ec_node_expr *node = (struct ec_node_expr *)gen_node; struct ec_node *term = NULL, *expr = NULL, *next = NULL, *pre_op = NULL, *post_op = NULL, *post = NULL, *weak = NULL; unsigned int i; int ret; + ec_node_free(node->child); + node->child = NULL; + if (node->val_node == NULL) return -EINVAL; if (node->bin_ops_len == 0 && node->pre_ops_len == 0 && @@ -212,7 +218,6 @@ static int ec_node_expr_build(struct ec_node *gen_node) weak = NULL; node->child = expr; - //ec_node_dump(stdout, node->child); //XXX return 0; @@ -229,7 +234,6 @@ fail: static struct ec_node_type ec_node_expr_type = { .name = "expr", - .build = ec_node_expr_build, .parse = ec_node_expr_parse, .complete = ec_node_expr_complete, .size = sizeof(struct ec_node_expr), @@ -250,15 +254,10 @@ int ec_node_expr_set_val_node(struct ec_node *gen_node, struct ec_node *val_node ret = -EINVAL; if (val_node == NULL) goto fail; - ret = -EPERM; - if (gen_node->flags & EC_NODE_F_BUILT) - goto fail; - ret = -EEXIST; - if (node->val_node != NULL) - goto fail; + ec_node_free(node->val_node); node->val_node = val_node; - gen_node->flags &= ~EC_NODE_F_BUILT; + ec_node_expr_build(node); return 0; @@ -281,9 +280,6 @@ int ec_node_expr_add_bin_op(struct ec_node *gen_node, struct ec_node *op) ret = -EINVAL; if (node == NULL || op == NULL) goto fail; - ret = -EPERM; - if (gen_node->flags & EC_NODE_F_BUILT) - goto fail; ret = -ENOMEM; bin_ops = ec_realloc(node->bin_ops, @@ -294,7 +290,7 @@ int ec_node_expr_add_bin_op(struct ec_node *gen_node, struct ec_node *op) node->bin_ops = bin_ops; bin_ops[node->bin_ops_len] = op; node->bin_ops_len++; - gen_node->flags &= ~EC_NODE_F_BUILT; + ec_node_expr_build(node); return 0; @@ -317,9 +313,6 @@ int ec_node_expr_add_pre_op(struct ec_node *gen_node, struct ec_node *op) ret = -EINVAL; if (node == NULL || op == NULL) goto fail; - ret = -EPERM; - if (gen_node->flags & EC_NODE_F_BUILT) - goto fail; ret = -ENOMEM; pre_ops = ec_realloc(node->pre_ops, @@ -330,7 +323,7 @@ int ec_node_expr_add_pre_op(struct ec_node *gen_node, struct ec_node *op) node->pre_ops = pre_ops; pre_ops[node->pre_ops_len] = op; node->pre_ops_len++; - gen_node->flags &= ~EC_NODE_F_BUILT; + ec_node_expr_build(node); return 0; @@ -353,9 +346,6 @@ int ec_node_expr_add_post_op(struct ec_node *gen_node, struct ec_node *op) ret = -EINVAL; if (node == NULL || op == NULL) goto fail; - ret = -EPERM; - if (gen_node->flags & EC_NODE_F_BUILT) - goto fail; ret = -ENOMEM; post_ops = ec_realloc(node->post_ops, @@ -366,7 +356,7 @@ int ec_node_expr_add_post_op(struct ec_node *gen_node, struct ec_node *op) node->post_ops = post_ops; post_ops[node->post_ops_len] = op; node->post_ops_len++; - gen_node->flags &= ~EC_NODE_F_BUILT; + ec_node_expr_build(node); return 0; @@ -390,9 +380,6 @@ int ec_node_expr_add_parenthesis(struct ec_node *gen_node, ret = -EINVAL; if (node == NULL || open == NULL || close == NULL) goto fail; - ret = -EPERM; - if (gen_node->flags & EC_NODE_F_BUILT) - goto fail;; ret = -ENOMEM; open_ops = ec_realloc(node->open_ops, @@ -409,7 +396,7 @@ int ec_node_expr_add_parenthesis(struct ec_node *gen_node, open_ops[node->paren_len] = open; close_ops[node->paren_len] = close; node->paren_len++; - gen_node->flags &= ~EC_NODE_F_BUILT; + ec_node_expr_build(node); return 0; diff --git a/lib/ecoli_node_expr_test.c b/lib/ecoli_node_expr_test.c index ee67cbf..45c169e 100644 --- a/lib/ecoli_node_expr_test.c +++ b/lib/ecoli_node_expr_test.c @@ -71,7 +71,7 @@ ec_node_expr_test_eval_var(void **result, void *userctx, return -ENOMEM; eval->val = val; - printf("eval var %d\n", eval->val); + EC_LOG(EC_LOG_DEBUG, "eval var %d\n", eval->val); *result = eval; return 0; @@ -96,7 +96,7 @@ ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand, else return -EINVAL; - printf("eval pre_op %d\n", eval->val); + EC_LOG(EC_LOG_DEBUG, "eval pre_op %d\n", eval->val); *result = eval; return 0; @@ -121,7 +121,7 @@ ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand, else return -EINVAL; - printf("eval post_op %d\n", eval->val); + EC_LOG(EC_LOG_DEBUG, "eval post_op %d\n", eval->val); *result = eval; return 0; @@ -150,7 +150,7 @@ ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1, else return -EINVAL; - printf("eval bin_op %d\n", eval1->val); + EC_LOG(EC_LOG_DEBUG, "eval bin_op %d\n", eval1->val); ec_free(eval2); *result = eval1; @@ -167,7 +167,7 @@ ec_node_expr_test_eval_parenthesis(void **result, void *userctx, (void)open_paren; (void)close_paren; - printf("eval paren\n"); + EC_LOG(EC_LOG_DEBUG, "eval paren\n"); *result = value; return 0; @@ -211,7 +211,7 @@ static int ec_node_expr_test_eval(struct ec_node *lex_node, eval = result; assert(eval != NULL); - printf("result: %d (expected %d)\n", eval->val, val); + EC_LOG(EC_LOG_DEBUG, "result: %d (expected %d)\n", eval->val, val); if (eval->val == val) ret = 0; else diff --git a/lib/ecoli_node_int.c b/lib/ecoli_node_int.c index 45786fa..9dae63a 100644 --- a/lib/ecoli_node_int.c +++ b/lib/ecoli_node_int.c @@ -379,16 +379,16 @@ static int ec_node_int_testcase(void) p = ec_node_parse(node, "0"); s = ec_strvec_val(ec_parsed_strvec(p), 0); - EC_TEST_ASSERT(s != NULL && - ec_node_int_getval(node, s, &i64) == 0 && - i64 == 0); + EC_TEST_ASSERT(s != NULL); + EC_TEST_ASSERT(ec_node_uint_getval(node, s, &u64) == 0); + EC_TEST_ASSERT (u64 == 0); ec_parsed_free(p); p = ec_node_parse(node, "10"); s = ec_strvec_val(ec_parsed_strvec(p), 0); - EC_TEST_ASSERT(s != NULL && - ec_node_uint_getval(node, s, &u64) == 0 && - u64 == 10); + EC_TEST_ASSERT(s != NULL); + EC_TEST_ASSERT(ec_node_uint_getval(node, s, &u64) == 0); + EC_TEST_ASSERT(u64 == 10); ec_parsed_free(p); ec_node_free(node); @@ -405,9 +405,9 @@ static int ec_node_int_testcase(void) p = ec_node_parse(node, "10"); s = ec_strvec_val(ec_parsed_strvec(p), 0); - EC_TEST_ASSERT(s != NULL && - ec_node_int_getval(node, s, &i64) == 0 && - i64 == 16); + EC_TEST_ASSERT(s != NULL); + EC_TEST_ASSERT(ec_node_int_getval(node, s, &i64) == 0); + EC_TEST_ASSERT(i64 == 16); ec_parsed_free(p); ec_node_free(node); diff --git a/lib/ecoli_node_once.c b/lib/ecoli_node_once.c index 9ecfa98..086f185 100644 --- a/lib/ecoli_node_once.c +++ b/lib/ecoli_node_once.c @@ -140,8 +140,6 @@ int ec_node_once_set(struct ec_node *gen_node, struct ec_node *child) if (ret < 0) return ret; - gen_node->flags &= ~EC_NODE_F_BUILT; - node->child = child; child->parent = gen_node; diff --git a/lib/ecoli_node_or.c b/lib/ecoli_node_or.c index 20999c6..ddf8281 100644 --- a/lib/ecoli_node_or.c +++ b/lib/ecoli_node_or.c @@ -122,8 +122,6 @@ int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child) if (child == NULL) return -EINVAL; - gen_node->flags &= ~EC_NODE_F_BUILT; - table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table)); if (table == NULL) { ec_node_free(child); diff --git a/lib/ecoli_node_seq.c b/lib/ecoli_node_seq.c index 0fc8eec..e239bcf 100644 --- a/lib/ecoli_node_seq.c +++ b/lib/ecoli_node_seq.c @@ -228,8 +228,6 @@ int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child) if (child == NULL) return -EINVAL; - gen_node->flags &= ~EC_NODE_F_BUILT; - table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table)); if (table == NULL) { ec_node_free(child); diff --git a/lib/ecoli_node_sh_lex.c b/lib/ecoli_node_sh_lex.c index b51069a..7f532fe 100644 --- a/lib/ecoli_node_sh_lex.c +++ b/lib/ecoli_node_sh_lex.c @@ -253,7 +253,10 @@ 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; } diff --git a/lib/ecoli_node_subset.c b/lib/ecoli_node_subset.c index 53b1a89..5d8ca0a 100644 --- a/lib/ecoli_node_subset.c +++ b/lib/ecoli_node_subset.c @@ -278,8 +278,6 @@ int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child) if (child == NULL) return -EINVAL; - gen_node->flags &= ~EC_NODE_F_BUILT; - table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table)); if (table == NULL) { ec_node_free(child); diff --git a/lib/ecoli_node_weakref.c b/lib/ecoli_node_weakref.c index 516b0bb..c598195 100644 --- a/lib/ecoli_node_weakref.c +++ b/lib/ecoli_node_weakref.c @@ -90,8 +90,6 @@ int ec_node_weakref_set(struct ec_node *gen_node, struct ec_node *child) if (child == NULL) return -EINVAL; - gen_node->flags &= ~EC_NODE_F_BUILT; - node->child = child; child->parent = gen_node; diff --git a/lib/ecoli_parsed.c b/lib/ecoli_parsed.c index 103b3b0..7423be5 100644 --- a/lib/ecoli_parsed.c +++ b/lib/ecoli_parsed.c @@ -65,16 +65,6 @@ static int __ec_node_parse_child(struct ec_node *node, struct ec_parsed *state, struct ec_parsed *child; int ret; - /* build the node if required */ - if (node->type->build != NULL) { - if ((node->flags & EC_NODE_F_BUILT) == 0) { - ret = node->type->build(node); - if (ret < 0) - return ret; - } - } - node->flags |= EC_NODE_F_BUILT; - if (node->type->parse == NULL) return -ENOTSUP; diff --git a/lib/ecoli_test.c b/lib/ecoli_test.c index b9b515b..6b168d6 100644 --- a/lib/ecoli_test.c +++ b/lib/ecoli_test.c @@ -76,7 +76,6 @@ int ec_test_check_parse(struct ec_node *tk, int expected, ...) } p = ec_node_parse_strvec(tk, vec); - ec_parsed_dump(stdout, p); /* XXX only for debug */ if (p == NULL) { EC_LOG(EC_LOG_ERR, "parsed is NULL\n"); } @@ -169,8 +168,7 @@ int ec_test_check_complete(struct ec_node *tk, ...) count, ec_completed_count(c, EC_COMP_FULL)); ec_completed_dump(stdout, c); ret = -1; - } else - ec_completed_dump(stdout, c); //XXX + } out: ec_strvec_free(vec); -- 2.39.5