From 04720324fffa9d3d68a92ae63215984bfc42978d Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Thu, 15 Nov 2018 18:18:01 +0100 Subject: [PATCH] add config for node_re --- libecoli/ecoli_node_re.c | 82 ++++++++++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 16 deletions(-) diff --git a/libecoli/ecoli_node_re.c b/libecoli/ecoli_node_re.c index 6ac5182..69f352c 100644 --- a/libecoli/ecoli_node_re.c +++ b/libecoli/ecoli_node_re.c @@ -15,6 +15,7 @@ #include #include #include +#include #include EC_LOG_TYPE_REGISTER(node_re); @@ -58,30 +59,37 @@ static void ec_node_re_free_priv(struct ec_node *gen_node) } } -static struct ec_node_type ec_node_re_type = { - .name = "re", - .parse = ec_node_re_parse, - .complete = ec_node_complete_unknown, - .size = sizeof(struct ec_node_re), - .free_priv = ec_node_re_free_priv, +static const struct ec_config_schema ec_node_re_schema[] = { + { + .key = "pattern", + .desc = "The pattern to match.", + .type = EC_CONFIG_TYPE_STRING, + }, + { + .type = EC_CONFIG_TYPE_NONE, + }, }; -EC_NODE_TYPE_REGISTER(ec_node_re_type); - -int ec_node_re_set_regexp(struct ec_node *gen_node, const char *str) +static int ec_node_re_set_config(struct ec_node *gen_node, + const struct ec_config *config) { struct ec_node_re *node = (struct ec_node_re *)gen_node; - char *str_copy = NULL; + const struct ec_config *value = NULL; + char *s = NULL; regex_t re; int ret; - EC_CHECK_ARG(str != NULL, -1, EINVAL); + value = ec_config_dict_get(config, "pattern"); + if (value == NULL) { + errno = EINVAL; + goto fail; + } - str_copy = ec_strdup(str); - if (str_copy == NULL) + s = ec_strdup(value->string); + if (s == NULL) goto fail; - ret = regcomp(&re, str_copy, REG_EXTENDED); + ret = regcomp(&re, s, REG_EXTENDED); if (ret != 0) { if (ret == REG_ESPACE) errno = ENOMEM; @@ -94,13 +102,55 @@ int ec_node_re_set_regexp(struct ec_node *gen_node, const char *str) ec_free(node->re_str); regfree(&node->re); } - node->re_str = str_copy; + node->re_str = s; node->re = re; return 0; fail: - ec_free(str_copy); + ec_free(s); + return -1; +} + +static struct ec_node_type ec_node_re_type = { + .name = "re", + .schema = ec_node_re_schema, + .set_config = ec_node_re_set_config, + .parse = ec_node_re_parse, + .complete = ec_node_complete_unknown, + .size = sizeof(struct ec_node_re), + .free_priv = ec_node_re_free_priv, +}; + +EC_NODE_TYPE_REGISTER(ec_node_re_type); + +int ec_node_re_set_regexp(struct ec_node *gen_node, const char *str) +{ + struct ec_config *config = NULL; + int ret; + + EC_CHECK_ARG(str != NULL, -1, EINVAL); + + if (ec_node_check_type(gen_node, &ec_node_re_type) < 0) + goto fail; + + config = ec_config_dict(); + if (config == NULL) + goto fail; + + ret = ec_config_dict_set(config, "pattern", ec_config_string(str)); + if (ret < 0) + goto fail; + + ret = ec_node_set_config(gen_node, config); + config = NULL; + if (ret < 0) + goto fail; + + return 0; + +fail: + ec_config_free(config); return -1; } -- 2.39.5