X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=src%2Fecoli_node_cmd.c;h=1fb006a9c9d8a1a67973254389cb3e0a0a56fb76;hb=331d87b8f34493ea7eb4db75fa9d90b2e3ed503b;hp=3b560845057aa5ccc84b5b6434990bdf11016235;hpb=18d03456d96f7a086a2ccc82ce97fcf056848d90;p=protos%2Flibecoli.git diff --git a/src/ecoli_node_cmd.c b/src/ecoli_node_cmd.c index 3b56084..1fb006a 100644 --- a/src/ecoli_node_cmd.c +++ b/src/ecoli_node_cmd.c @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -34,12 +35,12 @@ EC_LOG_TYPE_REGISTER(node_cmd); +static struct ec_node *ec_node_cmd_parser; /* the expression parser. */ +static struct ec_node *ec_node_cmd_expr; /* the expr parser without lexer. */ + struct ec_node_cmd { - struct ec_node gen; char *cmd_str; /* the command string. */ struct ec_node *cmd; /* the command node. */ - struct ec_node *parser; /* the expression parser. */ - struct ec_node *expr; /* the expression parser without lexer. */ struct ec_node **table; /* table of node referenced in command. */ unsigned int len; /* len of the table. */ }; @@ -52,7 +53,7 @@ struct ec_node_cmd_ctx { static int ec_node_cmd_eval_var(void **result, void *userctx, - const struct ec_parse *var) + const struct ec_pnode *var) { const struct ec_strvec *vec; struct ec_node_cmd_ctx *ctx = userctx; @@ -61,7 +62,7 @@ ec_node_cmd_eval_var(void **result, void *userctx, unsigned int i; /* get parsed string vector, it should contain only one str */ - vec = ec_parse_strvec(var); + vec = ec_pnode_strvec(var); if (ec_strvec_len(vec) != 1) { errno = EINVAL; return -1; @@ -95,7 +96,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_parse *operator) + const struct ec_pnode *operator) { (void)result; (void)userctx; @@ -108,7 +109,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_parse *operator) + const struct ec_pnode *operator) { const struct ec_strvec *vec; struct ec_node *in = operand;; @@ -117,7 +118,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_parse_strvec(operator); + vec = ec_pnode_strvec(operator); if (ec_strvec_len(vec) != 1) { errno = EINVAL; return -1; @@ -140,7 +141,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_parse *operator, void *operand2) + const struct ec_pnode *operator, void *operand2) { const struct ec_strvec *vec; @@ -151,14 +152,14 @@ 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_parse_strvec(operator); + vec = ec_pnode_strvec(operator); if (ec_strvec_len(vec) > 1) { errno = EINVAL; return -1; } if (ec_strvec_len(vec) == 0) { - if (!strcmp(in1->type->name, "seq")) { + if (!strcmp(ec_node_get_type_name(in1), "seq")) { if (ec_node_seq_add(in1, ec_node_clone(in2)) < 0) return -1; ec_node_free(in2); @@ -173,12 +174,12 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1, *result = out; } } else if (!strcmp(ec_strvec_val(vec, 0), "|")) { - if (!strcmp(in2->type->name, "or")) { + if (!strcmp(ec_node_get_type_name(in2), "or")) { if (ec_node_or_add(in2, ec_node_clone(in1)) < 0) return -1; ec_node_free(in1); *result = in2; - } else if (!strcmp(in1->type->name, "or")) { + } else if (!strcmp(ec_node_get_type_name(in1), "or")) { if (ec_node_or_add(in1, ec_node_clone(in2)) < 0) return -1; ec_node_free(in2); @@ -193,12 +194,12 @@ ec_node_cmd_eval_bin_op(void **result, void *userctx, void *operand1, *result = out; } } else if (!strcmp(ec_strvec_val(vec, 0), ",")) { - if (!strcmp(in2->type->name, "subset")) { + if (!strcmp(ec_node_get_type_name(in2), "subset")) { if (ec_node_subset_add(in2, ec_node_clone(in1)) < 0) return -1; ec_node_free(in1); *result = in2; - } else if (!strcmp(in1->type->name, "subset")) { + } else if (!strcmp(ec_node_get_type_name(in1), "subset")) { if (ec_node_subset_add(in1, ec_node_clone(in2)) < 0) return -1; ec_node_free(in2); @@ -222,8 +223,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_parse *open_paren, - const struct ec_parse *close_paren, + const struct ec_pnode *open_paren, + const struct ec_pnode *close_paren, void *value) { const struct ec_strvec *vec; @@ -234,7 +235,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_parse_strvec(open_paren); + vec = ec_pnode_strvec(open_paren); if (ec_strvec_len(vec) != 1) { errno = EINVAL; return -1; @@ -354,79 +355,74 @@ fail: } static struct ec_node * -ec_node_cmd_build(struct ec_node_cmd *node, const char *cmd_str, - struct ec_node **table, size_t len) +ec_node_cmd_build(const char *cmd_str, struct ec_node **table, size_t len) { struct ec_node_cmd_ctx ctx = { table, len }; - struct ec_parse *p = NULL; + struct ec_pnode *p = NULL; void *result; int ret; /* parse the command expression */ - p = ec_node_parse(node->parser, cmd_str); + p = ec_parse(ec_node_cmd_parser, cmd_str); if (p == NULL) goto fail; - if (!ec_parse_matches(p)) { + if (!ec_pnode_matches(p)) { errno = EINVAL; goto fail; } - if (!ec_parse_has_child(p)) { + if (!ec_pnode_has_child(p)) { errno = EINVAL; goto fail; } - ret = ec_node_expr_eval(&result, node->expr, - ec_parse_get_first_child(p), + ret = ec_node_expr_eval(&result, ec_node_cmd_expr, + ec_pnode_get_first_child(p), &expr_ops, &ctx); if (ret < 0) goto fail; - ec_parse_free(p); + ec_pnode_free(p); return result; fail: - ec_parse_free(p); + ec_pnode_free(p); return NULL; } static int -ec_node_cmd_parse(const struct ec_node *gen_node, struct ec_parse *state, +ec_node_cmd_parse(const struct ec_node *node, struct ec_pnode *pstate, const struct ec_strvec *strvec) { - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; + struct ec_node_cmd *priv = ec_node_priv(node); - return ec_node_parse_child(node->cmd, state, strvec); + return ec_parse_child(priv->cmd, pstate, strvec); } static int -ec_node_cmd_complete(const struct ec_node *gen_node, +ec_node_cmd_complete(const struct ec_node *node, struct ec_comp *comp, const struct ec_strvec *strvec) { - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; + struct ec_node_cmd *priv = ec_node_priv(node); - return ec_node_complete_child(node->cmd, comp, strvec); + return ec_complete_child(priv->cmd, comp, strvec); } -static void ec_node_cmd_free_priv(struct ec_node *gen_node) +static void ec_node_cmd_free_priv(struct ec_node *node) { - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; + struct ec_node_cmd *priv = ec_node_priv(node); size_t i; - ec_free(node->cmd_str); - node->cmd_str = NULL; - ec_node_free(node->expr); - node->expr = NULL; - ec_node_free(node->parser); - node->parser = NULL; - ec_node_free(node->cmd); - node->cmd = NULL; - for (i = 0; i < node->len; i++) - ec_node_free(node->table[i]); - ec_free(node->table); - node->table = NULL; - node->len = 0; + ec_free(priv->cmd_str); + priv->cmd_str = NULL; + ec_node_free(priv->cmd); + priv->cmd = NULL; + for (i = 0; i < priv->len; i++) + ec_node_free(priv->table[i]); + ec_free(priv->table); + priv->table = NULL; + priv->len = 0; } static const struct ec_config_schema ec_node_cmd_subschema[] = { @@ -461,10 +457,10 @@ static const struct ec_config_schema ec_node_cmd_schema[] = { }, }; -static int ec_node_cmd_set_config(struct ec_node *gen_node, +static int ec_node_cmd_set_config(struct ec_node *node, const struct ec_config *config) { - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; + struct ec_node_cmd *priv = ec_node_priv(node); const struct ec_config *expr = NULL; struct ec_node *cmd = NULL; struct ec_node **table = NULL; @@ -488,20 +484,20 @@ static int ec_node_cmd_set_config(struct ec_node *gen_node, goto fail; /* parse expression to build the cmd child node */ - cmd = ec_node_cmd_build(node, cmd_str, table, len); + cmd = ec_node_cmd_build(cmd_str, table, len); if (cmd == NULL) goto fail; /* ok, store the config */ - ec_node_free(node->cmd); - node->cmd = cmd; - ec_free(node->cmd_str); - node->cmd_str = cmd_str; - for (i = 0; i < node->len; i++) - ec_node_free(node->table[i]); - ec_free(node->table); - node->table = table; - node->len = len; + ec_node_free(priv->cmd); + priv->cmd = cmd; + ec_free(priv->cmd_str); + priv->cmd_str = cmd_str; + for (i = 0; i < priv->len; i++) + ec_node_free(priv->table[i]); + ec_free(priv->table); + priv->table = table; + priv->len = len; return 0; @@ -515,25 +511,25 @@ fail: } static size_t -ec_node_cmd_get_children_count(const struct ec_node *gen_node) +ec_node_cmd_get_children_count(const struct ec_node *node) { - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; + struct ec_node_cmd *priv = ec_node_priv(node); - if (node->cmd == NULL) + if (priv->cmd == NULL) return 0; return 1; } static int -ec_node_cmd_get_child(const struct ec_node *gen_node, size_t i, +ec_node_cmd_get_child(const struct ec_node *node, size_t i, struct ec_node **child, unsigned int *refs) { - struct ec_node_cmd *node = (struct ec_node_cmd *)gen_node; + struct ec_node_cmd *priv = ec_node_priv(node); if (i > 0) return -1; - *child = node->cmd; + *child = priv->cmd; *refs = 1; return 0; } @@ -555,8 +551,7 @@ EC_NODE_TYPE_REGISTER(ec_node_cmd_type); struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...) { struct ec_config *config = NULL, *children = NULL; - struct ec_node *gen_node = NULL; - struct ec_node_cmd *node = NULL; + struct ec_node *node = NULL; va_list ap; int ret; @@ -567,17 +562,8 @@ struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...) if (children == NULL) goto fail; - gen_node = ec_node_from_type(&ec_node_cmd_type, id); - if (gen_node == NULL) - goto fail; - node = (struct ec_node_cmd *)gen_node; - - node->expr = ec_node_cmd_build_expr(); - if (node->expr == NULL) - goto fail; - - node->parser = ec_node_cmd_build_parser(node->expr); - if (node->parser == NULL) + node = ec_node_from_type(&ec_node_cmd_type, id); + if (node == NULL) goto fail; config = ec_config_dict(); @@ -593,21 +579,58 @@ struct ec_node *__ec_node_cmd(const char *id, const char *cmd, ...) } children = 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); /* will also free added children */ + ec_node_free(node); /* will also free added children */ ec_config_free(children); ec_config_free(config); return NULL; } +static int ec_node_cmd_init_func(void) +{ + ec_node_cmd_expr = ec_node_cmd_build_expr(); + if (ec_node_cmd_expr == NULL) + goto fail; + + ec_node_cmd_parser = ec_node_cmd_build_parser(ec_node_cmd_expr); + if (ec_node_cmd_parser == NULL) + goto fail; + + return 0; + +fail: + EC_LOG(EC_LOG_ERR, "Failed to initialize command parser\n"); + ec_node_free(ec_node_cmd_expr); + ec_node_cmd_expr = NULL; + ec_node_free(ec_node_cmd_parser); + ec_node_cmd_parser = NULL; + return -1; +} + +static void ec_node_cmd_exit_func(void) +{ + ec_node_free(ec_node_cmd_expr); + ec_node_cmd_expr = NULL; + ec_node_free(ec_node_cmd_parser); + ec_node_cmd_parser = NULL; +} + +static struct ec_init ec_node_cmd_init = { + .init = ec_node_cmd_init_func, + .exit = ec_node_cmd_exit_func, + .priority = 75, +}; + +EC_INIT_REGISTER(ec_node_cmd_init); + /* LCOV_EXCL_START */ static int ec_node_cmd_testcase(void) { @@ -648,14 +671,14 @@ static int ec_node_cmd_testcase(void) testres |= EC_TEST_CHECK_PARSE(node, 4, "good", "morning", "1", "bob"); testres |= EC_TEST_CHECK_COMPLETE(node, - "", EC_NODE_ENDLIST, - "good", EC_NODE_ENDLIST); + "", EC_VA_END, + "good", EC_VA_END); testres |= EC_TEST_CHECK_COMPLETE(node, - "g", EC_NODE_ENDLIST, - "good", EC_NODE_ENDLIST); + "g", EC_VA_END, + "good", EC_VA_END); testres |= EC_TEST_CHECK_COMPLETE(node, - "good", "morning", "", EC_NODE_ENDLIST, - "bob", "bobby", "michael", EC_NODE_ENDLIST); + "good", "morning", "", EC_VA_END, + "bob", "bobby", "michael", EC_VA_END); ec_node_free(node);