1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
11 #include <ecoli_log.h>
12 #include <ecoli_malloc.h>
13 #include <ecoli_test.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_config.h>
19 #include <ecoli_node_re.h>
21 EC_LOG_TYPE_REGISTER(node_re);
29 ec_node_re_parse(const struct ec_node *node,
30 struct ec_pnode *pstate,
31 const struct ec_strvec *strvec)
33 struct ec_node_re *priv = ec_node_priv(node);
39 if (ec_strvec_len(strvec) == 0)
40 return EC_PARSE_NOMATCH;
42 str = ec_strvec_val(strvec, 0);
43 if (regexec(&priv->re, str, 1, &pos, 0) != 0)
44 return EC_PARSE_NOMATCH;
45 if (pos.rm_so != 0 || pos.rm_eo != (int)strlen(str))
46 return EC_PARSE_NOMATCH;
51 static void ec_node_re_free_priv(struct ec_node *node)
53 struct ec_node_re *priv = ec_node_priv(node);
55 if (priv->re_str != NULL) {
56 ec_free(priv->re_str);
61 static const struct ec_config_schema ec_node_re_schema[] = {
64 .desc = "The pattern to match.",
65 .type = EC_CONFIG_TYPE_STRING,
68 .type = EC_CONFIG_TYPE_NONE,
72 static int ec_node_re_set_config(struct ec_node *node,
73 const struct ec_config *config)
75 struct ec_node_re *priv = ec_node_priv(node);
76 const struct ec_config *value = NULL;
81 value = ec_config_dict_get(config, "pattern");
87 s = ec_strdup(value->string);
91 ret = regcomp(&re, s, REG_EXTENDED);
93 if (ret == REG_ESPACE)
100 if (priv->re_str != NULL) {
101 ec_free(priv->re_str);
114 static struct ec_node_type ec_node_re_type = {
116 .schema = ec_node_re_schema,
117 .set_config = ec_node_re_set_config,
118 .parse = ec_node_re_parse,
119 .size = sizeof(struct ec_node_re),
120 .free_priv = ec_node_re_free_priv,
123 EC_NODE_TYPE_REGISTER(ec_node_re_type);
125 int ec_node_re_set_regexp(struct ec_node *node, const char *str)
127 struct ec_config *config = NULL;
130 EC_CHECK_ARG(str != NULL, -1, EINVAL);
132 if (ec_node_check_type(node, &ec_node_re_type) < 0)
135 config = ec_config_dict();
139 ret = ec_config_dict_set(config, "pattern", ec_config_string(str));
143 ret = ec_node_set_config(node, config);
151 ec_config_free(config);
155 struct ec_node *ec_node_re(const char *id, const char *re_str)
157 struct ec_node *node = NULL;
159 node = ec_node_from_type(&ec_node_re_type, id);
163 if (ec_node_re_set_regexp(node, re_str) < 0)
173 /* LCOV_EXCL_START */
174 static int ec_node_re_testcase(void)
176 struct ec_node *node;
179 node = ec_node_re(EC_NO_ID, "fo+|bar");
181 EC_LOG(EC_LOG_ERR, "cannot create node\n");
184 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
185 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
186 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
187 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
188 testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
189 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
196 static struct ec_test ec_node_re_test = {
198 .test = ec_node_re_testcase,
201 EC_TEST_REGISTER(ec_node_re_test);