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);
30 ec_node_re_parse(const struct ec_node *gen_node,
31 struct ec_parse *state,
32 const struct ec_strvec *strvec)
34 struct ec_node_re *node = (struct ec_node_re *)gen_node;
40 if (ec_strvec_len(strvec) == 0)
41 return EC_PARSE_NOMATCH;
43 str = ec_strvec_val(strvec, 0);
44 if (regexec(&node->re, str, 1, &pos, 0) != 0)
45 return EC_PARSE_NOMATCH;
46 if (pos.rm_so != 0 || pos.rm_eo != (int)strlen(str))
47 return EC_PARSE_NOMATCH;
52 static void ec_node_re_free_priv(struct ec_node *gen_node)
54 struct ec_node_re *node = (struct ec_node_re *)gen_node;
56 if (node->re_str != NULL) {
57 ec_free(node->re_str);
62 static const struct ec_config_schema ec_node_re_schema[] = {
65 .desc = "The pattern to match.",
66 .type = EC_CONFIG_TYPE_STRING,
69 .type = EC_CONFIG_TYPE_NONE,
73 static int ec_node_re_set_config(struct ec_node *gen_node,
74 const struct ec_config *config)
76 struct ec_node_re *node = (struct ec_node_re *)gen_node;
77 const struct ec_config *value = NULL;
82 value = ec_config_dict_get(config, "pattern");
88 s = ec_strdup(value->string);
92 ret = regcomp(&re, s, REG_EXTENDED);
94 if (ret == REG_ESPACE)
101 if (node->re_str != NULL) {
102 ec_free(node->re_str);
115 static struct ec_node_type ec_node_re_type = {
117 .schema = ec_node_re_schema,
118 .set_config = ec_node_re_set_config,
119 .parse = ec_node_re_parse,
120 .complete = ec_node_complete_unknown,
121 .size = sizeof(struct ec_node_re),
122 .free_priv = ec_node_re_free_priv,
125 EC_NODE_TYPE_REGISTER(ec_node_re_type);
127 int ec_node_re_set_regexp(struct ec_node *gen_node, const char *str)
129 struct ec_config *config = NULL;
132 EC_CHECK_ARG(str != NULL, -1, EINVAL);
134 if (ec_node_check_type(gen_node, &ec_node_re_type) < 0)
137 config = ec_config_dict();
141 ret = ec_config_dict_set(config, "pattern", ec_config_string(str));
145 ret = ec_node_set_config(gen_node, config);
153 ec_config_free(config);
157 struct ec_node *ec_node_re(const char *id, const char *re_str)
159 struct ec_node *gen_node = NULL;
161 gen_node = ec_node_from_type(&ec_node_re_type, id);
162 if (gen_node == NULL)
165 if (ec_node_re_set_regexp(gen_node, re_str) < 0)
171 ec_node_free(gen_node);
175 /* LCOV_EXCL_START */
176 static int ec_node_re_testcase(void)
178 struct ec_node *node;
181 node = ec_node_re(EC_NO_ID, "fo+|bar");
183 EC_LOG(EC_LOG_ERR, "cannot create node\n");
186 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
187 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
188 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
189 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
190 testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
191 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
198 static struct ec_test ec_node_re_test = {
200 .test = ec_node_re_testcase,
203 EC_TEST_REGISTER(ec_node_re_test);