X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=src%2Fecoli_node_cond.c;h=649ecbbcf9c7146c73b0afb30bffc00c4bd14a09;hb=HEAD;hp=9c7c071a1236bad5de97fca7196369d3cc3ae7a8;hpb=e4011b72a49de13b0bc5af2547bbb02903d5c44f;p=protos%2Flibecoli.git diff --git a/src/ecoli_node_cond.c b/src/ecoli_node_cond.c index 9c7c071..649ecbb 100644 --- a/src/ecoli_node_cond.c +++ b/src/ecoli_node_cond.c @@ -42,9 +42,8 @@ static struct ec_node *ec_node_cond_parser; /* the expression parser. */ static struct ec_dict *ec_node_cond_functions; /* functions dictionary */ struct ec_node_cond { - struct ec_node gen; char *cond_str; /* the condition string. */ - struct ec_parse *parsed_cond; /* the parsed condition. */ + struct ec_pnode *parsed_cond; /* the parsed condition. */ struct ec_node *child; /* the child node. */ }; @@ -72,10 +71,11 @@ struct cond_result { }; typedef struct cond_result *(cond_func_t)( - const struct ec_parse *state, + const struct ec_pnode *pstate, struct cond_result **in, size_t in_len); -void cond_result_free(struct cond_result *res) +static void +cond_result_free(struct cond_result *res) { if (res == NULL) return; @@ -95,7 +95,8 @@ void cond_result_free(struct cond_result *res) ec_free(res); } -void cond_result_table_free(struct cond_result **table, size_t len) +static void +cond_result_table_free(struct cond_result **table, size_t len) { size_t i; @@ -178,21 +179,17 @@ fail: return NULL; } -static struct ec_parse * +static struct ec_pnode * ec_node_cond_build(const char *cond_str) { - struct ec_parse *p = NULL; + struct ec_pnode *p = NULL; /* parse the condition expression */ - p = ec_node_parse(ec_node_cond_parser, cond_str); + p = ec_parse(ec_node_cond_parser, cond_str); if (p == NULL) goto fail; - if (!ec_parse_matches(p)) { - errno = EINVAL; - goto fail; - } - if (!ec_parse_has_child(p)) { + if (!ec_pnode_matches(p)) { errno = EINVAL; goto fail; } @@ -200,15 +197,15 @@ ec_node_cond_build(const char *cond_str) return p; fail: - ec_parse_free(p); + ec_pnode_free(p); return NULL; } static struct cond_result * -eval_root(const struct ec_parse *state, struct cond_result **in, size_t in_len) +eval_root(const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { struct cond_result *out = NULL; - const struct ec_parse *root = NULL; + const struct ec_pnode *root = NULL; (void)in; @@ -227,7 +224,7 @@ eval_root(const struct ec_parse *state, struct cond_result **in, size_t in_len) if (out->htable == NULL) goto fail; - root = ec_parse_get_root(state); + root = EC_PNODE_GET_ROOT(pstate); if (ec_htable_set(out->htable, &root, sizeof(root), NULL, NULL) < 0) goto fail; @@ -241,7 +238,7 @@ fail: } static struct cond_result * -eval_current(const struct ec_parse *state, struct cond_result **in, +eval_current(const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { struct cond_result *out = NULL; @@ -263,7 +260,7 @@ eval_current(const struct ec_parse *state, struct cond_result **in, if (out->htable == NULL) goto fail; - if (ec_htable_set(out->htable, &state, sizeof(state), NULL, NULL) < 0) + if (ec_htable_set(out->htable, &pstate, sizeof(pstate), NULL, NULL) < 0) goto fail; cond_result_table_free(in, in_len); @@ -293,11 +290,11 @@ boolean_value(const struct cond_result *res) } static struct cond_result * -eval_bool(const struct ec_parse *state, struct cond_result **in, size_t in_len) +eval_bool(const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { struct cond_result *out = NULL; - (void)state; + (void)pstate; if (in_len != 1) { EC_LOG(LOG_ERR, "bool() takes one argument.\n"); @@ -322,12 +319,12 @@ fail: } static struct cond_result * -eval_or(const struct ec_parse *state, struct cond_result **in, size_t in_len) +eval_or(const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { struct cond_result *out = NULL; size_t i; - (void)state; + (void)pstate; if (in_len < 2) { EC_LOG(LOG_ERR, "or() takes at least two arguments\n"); @@ -356,12 +353,12 @@ fail: } static struct cond_result * -eval_and(const struct ec_parse *state, struct cond_result **in, size_t in_len) +eval_and(const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { struct cond_result *out = NULL; size_t i; - (void)state; + (void)pstate; if (in_len < 2) { EC_LOG(LOG_ERR, "or() takes at least two arguments\n"); @@ -390,15 +387,15 @@ fail: } static struct cond_result * -eval_first_child(const struct ec_parse *state, struct cond_result **in, +eval_first_child(const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { struct cond_result *out = NULL; struct ec_htable_elt_ref *iter; - const struct ec_parse * const *pparse; - struct ec_parse *parse; + const struct ec_pnode * const *pparse; + struct ec_pnode *parse; - (void)state; + (void)pstate; if (in_len != 1 || in[0]->type != NODESET) { EC_LOG(LOG_ERR, "first_child() takes one argument of type nodeset.\n"); @@ -418,7 +415,7 @@ eval_first_child(const struct ec_parse *state, struct cond_result **in, for (iter = ec_htable_iter(in[0]->htable); iter != NULL; iter = ec_htable_iter_next(iter)) { pparse = ec_htable_iter_get_key(iter); - parse = ec_parse_get_first_child(*pparse); + parse = ec_pnode_get_first_child(*pparse); if (parse == NULL) continue; if (ec_htable_set(out->htable, &parse, sizeof(parse), NULL, @@ -436,16 +433,16 @@ fail: } static struct cond_result * -eval_find(const struct ec_parse *state, struct cond_result **in, +eval_find(const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { struct cond_result *out = NULL; struct ec_htable_elt_ref *iter; - struct ec_parse * const *pparse; - struct ec_parse *parse; + struct ec_pnode * const *pparse; + struct ec_pnode *parse; const char *id; - (void)state; + (void)pstate; if (in_len != 2 || in[0]->type != NODESET || in[1]->type != STR) { EC_LOG(LOG_ERR, "find() takes two arguments (nodeset, str).\n"); @@ -466,13 +463,13 @@ eval_find(const struct ec_parse *state, struct cond_result **in, for (iter = ec_htable_iter(in[0]->htable); iter != NULL; iter = ec_htable_iter_next(iter)) { pparse = ec_htable_iter_get_key(iter); - parse = ec_parse_find(*pparse, id); + parse = ec_pnode_find(*pparse, id); while (parse != NULL) { if (ec_htable_set(out->htable, &parse, sizeof(parse), NULL, NULL) < 0) goto fail; - parse = ec_parse_find_next(*pparse, parse, id, 1); + parse = ec_pnode_find_next(*pparse, parse, id, 1); } } @@ -486,14 +483,14 @@ fail: } static struct cond_result * -eval_cmp(const struct ec_parse *state, struct cond_result **in, +eval_cmp(const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { struct cond_result *out = NULL; struct ec_htable_elt_ref *iter; bool eq = false, gt = false; - (void)state; + (void)pstate; if (in_len != 3 || in[0]->type != STR || in[1]->type != in[2]->type) { EC_LOG(LOG_ERR, "cmp() takes 3 arguments (str, , ).\n"); @@ -530,7 +527,7 @@ eval_cmp(const struct ec_parse *state, struct cond_result **in, if (ec_htable_get( in[2]->htable, ec_htable_iter_get_key(iter), - sizeof(struct ec_parse *)) == NULL) { + sizeof(struct ec_pnode *)) == NULL) { eq = false; break; } @@ -569,11 +566,11 @@ fail: } static struct cond_result * -eval_count(const struct ec_parse *state, struct cond_result **in, size_t in_len) +eval_count(const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { struct cond_result *out = NULL; - (void)state; + (void)pstate; if (in_len != 1 || in[0]->type != NODESET) { EC_LOG(LOG_ERR, "count() takes one argument of type nodeset.\n"); @@ -598,7 +595,7 @@ fail: } static struct cond_result * -eval_func(const char *name, const struct ec_parse *state, +eval_func(const char *name, const struct ec_pnode *pstate, struct cond_result **in, size_t in_len) { cond_func_t *f; @@ -612,68 +609,68 @@ eval_func(const char *name, const struct ec_parse *state, return NULL; } - return f(state, in, in_len); + return f(pstate, in, in_len); } static struct cond_result * -eval_condition(const struct ec_parse *cond, const struct ec_parse *state) +eval_condition(const struct ec_pnode *cond, const struct ec_pnode *pstate) { - const struct ec_parse *iter; + const struct ec_pnode *iter; struct cond_result *res = NULL; struct cond_result **args = NULL; - const struct ec_parse *func = NULL, *func_name = NULL, *arg_list = NULL; - const struct ec_parse *value = NULL; + const struct ec_pnode *func = NULL, *func_name = NULL, *arg_list = NULL; + const struct ec_pnode *value = NULL; const char *id; size_t n_arg = 0; // XXX fix cast (x3) - func = ec_parse_find((void *)cond, "id_function"); + func = ec_pnode_find((void *)cond, "id_function"); if (func != NULL) { - EC_PARSE_FOREACH_CHILD(iter, func) { - id = ec_node_id(ec_parse_get_node(iter)); + EC_PNODE_FOREACH_CHILD(iter, func) { + id = ec_node_id(ec_pnode_get_node(iter)); if (!strcmp(id, "id_function_name")) func_name = iter; if (!strcmp(id, "id_arg_list")) arg_list = iter; } - iter = ec_parse_find((void *)arg_list, "id_arg"); + iter = ec_pnode_find((void *)arg_list, "id_arg"); while (iter != NULL) { args = ec_realloc(args, (n_arg + 1) * sizeof(*args)); - args[n_arg] = eval_condition(iter, state); + args[n_arg] = eval_condition(iter, pstate); if (args[n_arg] == NULL) goto fail; n_arg++; - iter = ec_parse_find_next((void *)arg_list, + iter = ec_pnode_find_next((void *)arg_list, (void *)iter, "id_arg", 0); } - res = eval_func(ec_strvec_val(ec_parse_strvec(func_name), 0), - state, args, n_arg); + res = eval_func(ec_strvec_val(ec_pnode_get_strvec(func_name), 0), + pstate, args, n_arg); args = NULL; return res; } - value = ec_parse_find((void *)cond, "id_value_str"); + value = ec_pnode_find((void *)cond, "id_value_str"); if (value != NULL) { res = ec_malloc(sizeof(*res)); if (res == NULL) goto fail; res->type = STR; - res->str = ec_strdup(ec_strvec_val(ec_parse_strvec(value), 0)); + res->str = ec_strdup(ec_strvec_val(ec_pnode_get_strvec(value), 0)); if (res->str == NULL) goto fail; return res; } - value = ec_parse_find((void *)cond, "id_value_int"); + value = ec_pnode_find((void *)cond, "id_value_int"); if (value != NULL) { res = ec_malloc(sizeof(*res)); if (res == NULL) goto fail; res->type = INT; - if (ec_str_parse_llint(ec_strvec_val(ec_parse_strvec(value), 0), + if (ec_str_parse_llint(ec_strvec_val(ec_pnode_get_strvec(value), 0), 0, LLONG_MIN, LLONG_MAX, &res->int64) < 0) goto fail; @@ -687,12 +684,12 @@ fail: } static int -validate_condition(const struct ec_parse *cond, const struct ec_parse *state) +validate_condition(const struct ec_pnode *cond, const struct ec_pnode *pstate) { struct cond_result *res; int ret; - res = eval_condition(cond, state); + res = eval_condition(cond, pstate); if (res == NULL) return -1; @@ -703,25 +700,25 @@ validate_condition(const struct ec_parse *cond, const struct ec_parse *state) } static int -ec_node_cond_parse(const struct ec_node *gen_node, struct ec_parse *state, +ec_node_cond_parse(const struct ec_node *node, struct ec_pnode *pstate, const struct ec_strvec *strvec) { - struct ec_node_cond *node = (struct ec_node_cond *)gen_node; - struct ec_parse *child; + struct ec_node_cond *priv = ec_node_priv(node); + struct ec_pnode *child; int ret, valid; - ret = ec_node_parse_child(node->child, state, strvec); + ret = ec_parse_child(priv->child, pstate, strvec); if (ret <= 0) return ret; - valid = validate_condition(node->parsed_cond, state); + valid = validate_condition(priv->parsed_cond, pstate); if (valid < 0) return valid; if (valid == 0) { - child = ec_parse_get_last_child(state); - ec_parse_unlink_child(state, child); - ec_parse_free(child); + child = ec_pnode_get_last_child(pstate); + ec_pnode_unlink_child(child); + ec_pnode_free(child); return EC_PARSE_NOMATCH; } @@ -729,27 +726,27 @@ ec_node_cond_parse(const struct ec_node *gen_node, struct ec_parse *state, } static int -ec_node_cond_complete(const struct ec_node *gen_node, +ec_node_cond_complete(const struct ec_node *node, struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_node_cond *node = (struct ec_node_cond *)gen_node; + struct ec_node_cond *priv = ec_node_priv(node); // XXX eval condition // XXX before or after completing ? configurable ? - return ec_node_complete_child(node->child, comp, strvec); + return ec_complete_child(priv->child, comp, strvec); } -static void ec_node_cond_free_priv(struct ec_node *gen_node) +static void ec_node_cond_free_priv(struct ec_node *node) { - struct ec_node_cond *node = (struct ec_node_cond *)gen_node; + struct ec_node_cond *priv = ec_node_priv(node); - ec_free(node->cond_str); - node->cond_str = NULL; - ec_parse_free(node->parsed_cond); - node->parsed_cond = NULL; - ec_node_free(node->child); + ec_free(priv->cond_str); + priv->cond_str = NULL; + ec_pnode_free(priv->parsed_cond); + priv->parsed_cond = NULL; + ec_node_free(priv->child); } static const struct ec_config_schema ec_node_cond_schema[] = { @@ -768,12 +765,12 @@ static const struct ec_config_schema ec_node_cond_schema[] = { }, }; -static int ec_node_cond_set_config(struct ec_node *gen_node, +static int ec_node_cond_set_config(struct ec_node *node, const struct ec_config *config) { - struct ec_node_cond *node = (struct ec_node_cond *)gen_node; + struct ec_node_cond *priv = ec_node_priv(node); const struct ec_config *cond = NULL; - struct ec_parse *parsed_cond = NULL; + struct ec_pnode *parsed_cond = NULL; const struct ec_config *child; char *cond_str = NULL; @@ -797,41 +794,41 @@ static int ec_node_cond_set_config(struct ec_node *gen_node, goto fail; /* ok, store the config */ - ec_parse_free(node->parsed_cond); - node->parsed_cond = parsed_cond; - ec_free(node->cond_str); - node->cond_str = cond_str; - ec_node_free(node->child); - node->child = ec_node_clone(child->node); + ec_pnode_free(priv->parsed_cond); + priv->parsed_cond = parsed_cond; + ec_free(priv->cond_str); + priv->cond_str = cond_str; + ec_node_free(priv->child); + priv->child = ec_node_clone(child->node); return 0; fail: - ec_parse_free(parsed_cond); + ec_pnode_free(parsed_cond); ec_free(cond_str); return -1; } static size_t -ec_node_cond_get_children_count(const struct ec_node *gen_node) +ec_node_cond_get_children_count(const struct ec_node *node) { - struct ec_node_cond *node = (struct ec_node_cond *)gen_node; + struct ec_node_cond *priv = ec_node_priv(node); - if (node->child == NULL) + if (priv->child == NULL) return 0; return 1; } static int -ec_node_cond_get_child(const struct ec_node *gen_node, size_t i, +ec_node_cond_get_child(const struct ec_node *node, size_t i, struct ec_node **child, unsigned int *refs) { - struct ec_node_cond *node = (struct ec_node_cond *)gen_node; + struct ec_node_cond *priv = ec_node_priv(node); if (i > 0) return -1; - *child = node->child; + *child = priv->child; *refs = 1; return 0; } @@ -854,14 +851,14 @@ struct ec_node *ec_node_cond(const char *id, const char *cmd, struct ec_node *child) { struct ec_config *config = NULL; - struct ec_node *gen_node = NULL; + struct ec_node *node = NULL; int ret; if (child == NULL) return NULL; - gen_node = ec_node_from_type(&ec_node_cond_type, id); - if (gen_node == NULL) + node = ec_node_from_type(&ec_node_cond_type, id); + if (node == NULL) goto fail; config = ec_config_dict(); @@ -877,15 +874,15 @@ struct ec_node *ec_node_cond(const char *id, const char *cmd, } child = NULL; - ret = ec_node_set_config(gen_node, config); + ret = ec_node_set_config(node, config); config = NULL; /* freed */ if (ret < 0) goto fail; - return gen_node; + return node; fail: - ec_node_free(gen_node); + ec_node_free(node); ec_node_free(child); ec_config_free(config);