X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=lib%2Fecoli_node_str.c;h=d53ea394222db6c1222a14d59eb06b8e5909870a;hb=15718f19cbf3868bf6fba79f4e26b5254c57873e;hp=6b11ebc7ad9279a6a2cfdb7db3f396a66679fd98;hpb=8df9723e4c3a2bb0ba89a8f5a70c319c7702935f;p=protos%2Flibecoli.git diff --git a/lib/ecoli_node_str.c b/lib/ecoli_node_str.c index 6b11ebc..d53ea39 100644 --- a/lib/ecoli_node_str.c +++ b/lib/ecoli_node_str.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,11 @@ ec_node_str_parse(const struct ec_node *gen_node, (void)state; + if (node->string == NULL) { + errno = EINVAL; + return -1; + } + if (ec_strvec_len(strvec) == 0) return EC_PARSE_NOMATCH; @@ -87,8 +93,49 @@ static void ec_node_str_free_priv(struct ec_node *gen_node) ec_free(node->string); } +static const struct ec_config_schema ec_node_str_schema[] = { + { + .key = "string", + .desc = "The string to match.", + .type = EC_CONFIG_TYPE_STRING, + }, + { + .type = EC_CONFIG_TYPE_NONE, + }, +}; + +static int ec_node_str_set_config(struct ec_node *gen_node, + const struct ec_config *config) +{ + struct ec_node_str *node = (struct ec_node_str *)gen_node; + const struct ec_config *value = NULL; + char *s = NULL; + + value = ec_config_dict_get(config, "string"); + if (value == NULL) { + errno = EINVAL; + goto fail; + } + + s = ec_strdup(value->string); + if (s == NULL) + goto fail; + + ec_free(node->string); + node->string = s; + node->len = strlen(node->string); + + return 0; + +fail: + ec_free(s); + return -1; +} + static struct ec_node_type ec_node_str_type = { .name = "str", + .schema = ec_node_str_schema, + .set_config = ec_node_str_set_config, .parse = ec_node_str_parse, .complete = ec_node_str_complete, .desc = ec_node_str_desc, @@ -100,30 +147,42 @@ EC_NODE_TYPE_REGISTER(ec_node_str_type); int ec_node_str_set_str(struct ec_node *gen_node, const char *str) { - struct ec_node_str *node = (struct ec_node_str *)gen_node; + struct ec_config *config = NULL; int ret; - ret = ec_node_check_type(gen_node, &ec_node_str_type); - if (ret < 0) - return ret; + if (ec_node_check_type(gen_node, &ec_node_str_type) < 0) + goto fail; - if (str == NULL) - return -EINVAL; - ec_free(node->string); - node->string = ec_strdup(str); - if (node->string == NULL) - return -ENOMEM; + if (str == NULL) { + errno = EINVAL; + goto fail; + } - node->len = strlen(node->string); + config = ec_config_dict(); + if (config == NULL) + goto fail; + + ret = ec_config_dict_set(config, "string", 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; } struct ec_node *ec_node_str(const char *id, const char *str) { struct ec_node *gen_node = NULL; - gen_node = __ec_node(&ec_node_str_type, id); + gen_node = ec_node_from_type(&ec_node_str_type, id); if (gen_node == NULL) goto fail;