#include <errno.h>
#include <limits.h>
+#include <ecoli_init.h>
#include <ecoli_malloc.h>
#include <ecoli_log.h>
#include <ecoli_test.h>
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. */
};
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;
unsigned int i;
/* get parsed string vector, it should contain only one str */
- vec = ec_parse_strvec(var);
+ vec = ec_pnode_get_strvec(var);
if (ec_strvec_len(vec) != 1) {
errno = EINVAL;
return -1;
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;
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;;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parse_strvec(operator);
+ vec = ec_pnode_get_strvec(operator);
if (ec_strvec_len(vec) != 1) {
errno = EINVAL;
return -1;
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;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parse_strvec(operator);
+ vec = ec_pnode_get_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);
*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);
*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);
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;
(void)close_paren;
/* get parsed string vector, it should contain only one str */
- vec = ec_parse_strvec(open_paren);
+ vec = ec_pnode_get_strvec(open_paren);
if (ec_strvec_len(vec) != 1) {
errno = EINVAL;
return -1;
}
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)) {
- errno = EINVAL;
- goto fail;
- }
- if (!ec_parse_has_child(p)) {
+ if (!ec_pnode_matches(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[] = {
},
};
-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;
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;
}
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;
}
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;
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();
}
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)
{
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);