+ *refs = 2;
+ return 0;
+}
+
+static const struct ec_config_schema ec_node_re_lex_dict[] = {
+ {
+ .key = "pattern",
+ .desc = "The pattern to match.",
+ .type = EC_CONFIG_TYPE_STRING,
+ },
+ {
+ .key = "keep",
+ .desc = "Whether to keep or drop the string matching "
+ "the regular expression.",
+ .type = EC_CONFIG_TYPE_BOOL,
+ },
+ {
+ .type = EC_CONFIG_TYPE_NONE,
+ },
+};
+
+static const struct ec_config_schema ec_node_re_lex_elt[] = {
+ {
+ .desc = "A pattern element.",
+ .type = EC_CONFIG_TYPE_DICT,
+ .subschema = ec_node_re_lex_dict,
+ },
+ {
+ .type = EC_CONFIG_TYPE_NONE,
+ },
+};
+
+static const struct ec_config_schema ec_node_re_lex_schema[] = {
+ {
+ .key = "patterns",
+ .desc = "The list of patterns elements.",
+ .type = EC_CONFIG_TYPE_LIST,
+ .subschema = ec_node_re_lex_elt,
+ },
+ {
+ .key = "child",
+ .desc = "The child node.",
+ .type = EC_CONFIG_TYPE_NODE,
+ },
+ {
+ .type = EC_CONFIG_TYPE_NONE,
+ },
+};
+
+static int ec_node_re_lex_set_config(struct ec_node *gen_node,
+ const struct ec_config *config)
+{
+ struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
+ struct regexp_pattern *table = NULL;
+ const struct ec_config *patterns, *child, *elt, *pattern, *keep;
+ char *pattern_str = NULL;
+ ssize_t i, n = 0;
+ int ret;
+
+ child = ec_config_dict_get(config, "child");
+ if (child == NULL)
+ goto fail;
+ if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
+ errno = EINVAL;
+ goto fail;
+ }
+
+ patterns = ec_config_dict_get(config, "patterns");
+ if (patterns != NULL) {
+ n = ec_config_count(patterns);
+ if (n < 0)
+ goto fail;
+
+ table = ec_calloc(n, sizeof(*table));
+ if (table == NULL)
+ goto fail;
+
+ n = 0;
+ TAILQ_FOREACH(elt, &patterns->list, next) {
+ if (ec_config_get_type(elt) != EC_CONFIG_TYPE_DICT) {
+ errno = EINVAL;
+ goto fail;
+ }
+ pattern = ec_config_dict_get(elt, "pattern");
+ if (pattern == NULL) {
+ errno = EINVAL;
+ goto fail;
+ }
+ if (ec_config_get_type(pattern) != EC_CONFIG_TYPE_STRING) {
+ errno = EINVAL;
+ goto fail;
+ }
+ keep = ec_config_dict_get(elt, "keep");
+ if (keep == NULL) {
+ errno = EINVAL;
+ goto fail;
+ }
+ if (ec_config_get_type(keep) != EC_CONFIG_TYPE_BOOL) {
+ errno = EINVAL;
+ goto fail;
+ }
+ pattern_str = ec_strdup(pattern->string);
+ if (pattern_str == NULL)
+ goto fail;
+
+ ret = regcomp(&table[n].r, pattern_str, REG_EXTENDED);
+ if (ret != 0) {
+ EC_LOG(EC_LOG_ERR,
+ "Regular expression <%s> compilation failed: %d\n",
+ pattern_str, ret);
+ if (ret == REG_ESPACE)
+ errno = ENOMEM;
+ else
+ errno = EINVAL;
+ goto fail;
+ }
+ table[n].pattern = pattern_str;
+ table[n].keep = keep->boolean;
+ pattern_str = NULL;
+
+ n++;
+ }
+ }
+
+ if (node->child != NULL)
+ ec_node_free(node->child);
+ node->child = ec_node_clone(child->node);
+ for (i = 0; i < (ssize_t)node->len; i++) {
+ ec_free(node->table[i].pattern);
+ regfree(&node->table[i].r);
+ }
+ ec_free(node->table);
+ node->table = table;
+ node->len = n;
+