9e6678c9997377feba8c319e1f9c0ed53e7da53c
[protos/libecoli.git] / src / ecoli_node_re.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <regex.h>
10
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>
20
21 EC_LOG_TYPE_REGISTER(node_re);
22
23 struct ec_node_re {
24         char *re_str;
25         regex_t re;
26 };
27
28 static int
29 ec_node_re_parse(const struct ec_node *node,
30                 struct ec_parse *state,
31                 const struct ec_strvec *strvec)
32 {
33         struct ec_node_re *priv = ec_node_priv(node);
34         const char *str;
35         regmatch_t pos;
36
37         (void)state;
38
39         if (ec_strvec_len(strvec) == 0)
40                 return EC_PARSE_NOMATCH;
41
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;
47
48         return 1;
49 }
50
51 static void ec_node_re_free_priv(struct ec_node *node)
52 {
53         struct ec_node_re *priv = ec_node_priv(node);
54
55         if (priv->re_str != NULL) {
56                 ec_free(priv->re_str);
57                 regfree(&priv->re);
58         }
59 }
60
61 static const struct ec_config_schema ec_node_re_schema[] = {
62         {
63                 .key = "pattern",
64                 .desc = "The pattern to match.",
65                 .type = EC_CONFIG_TYPE_STRING,
66         },
67         {
68                 .type = EC_CONFIG_TYPE_NONE,
69         },
70 };
71
72 static int ec_node_re_set_config(struct ec_node *node,
73                                 const struct ec_config *config)
74 {
75         struct ec_node_re *priv = ec_node_priv(node);
76         const struct ec_config *value = NULL;
77         char *s = NULL;
78         regex_t re;
79         int ret;
80
81         value = ec_config_dict_get(config, "pattern");
82         if (value == NULL) {
83                 errno = EINVAL;
84                 goto fail;
85         }
86
87         s = ec_strdup(value->string);
88         if (s == NULL)
89                 goto fail;
90
91         ret = regcomp(&re, s, REG_EXTENDED);
92         if (ret != 0) {
93                 if (ret == REG_ESPACE)
94                         errno = ENOMEM;
95                 else
96                         errno = EINVAL;
97                 goto fail;
98         }
99
100         if (priv->re_str != NULL) {
101                 ec_free(priv->re_str);
102                 regfree(&priv->re);
103         }
104         priv->re_str = s;
105         priv->re = re;
106
107         return 0;
108
109 fail:
110         ec_free(s);
111         return -1;
112 }
113
114 static struct ec_node_type ec_node_re_type = {
115         .name = "re",
116         .schema = ec_node_re_schema,
117         .set_config = ec_node_re_set_config,
118         .parse = ec_node_re_parse,
119         .complete = ec_node_complete_unknown,
120         .size = sizeof(struct ec_node_re),
121         .free_priv = ec_node_re_free_priv,
122 };
123
124 EC_NODE_TYPE_REGISTER(ec_node_re_type);
125
126 int ec_node_re_set_regexp(struct ec_node *node, const char *str)
127 {
128         struct ec_config *config = NULL;
129         int ret;
130
131         EC_CHECK_ARG(str != NULL, -1, EINVAL);
132
133         if (ec_node_check_type(node, &ec_node_re_type) < 0)
134                 goto fail;
135
136         config = ec_config_dict();
137         if (config == NULL)
138                 goto fail;
139
140         ret = ec_config_dict_set(config, "pattern", ec_config_string(str));
141         if (ret < 0)
142                 goto fail;
143
144         ret = ec_node_set_config(node, config);
145         config = NULL;
146         if (ret < 0)
147                 goto fail;
148
149         return 0;
150
151 fail:
152         ec_config_free(config);
153         return -1;
154 }
155
156 struct ec_node *ec_node_re(const char *id, const char *re_str)
157 {
158         struct ec_node *node = NULL;
159
160         node = ec_node_from_type(&ec_node_re_type, id);
161         if (node == NULL)
162                 goto fail;
163
164         if (ec_node_re_set_regexp(node, re_str) < 0)
165                 goto fail;
166
167         return node;
168
169 fail:
170         ec_node_free(node);
171         return NULL;
172 }
173
174 /* LCOV_EXCL_START */
175 static int ec_node_re_testcase(void)
176 {
177         struct ec_node *node;
178         int testres = 0;
179
180         node = ec_node_re(EC_NO_ID, "fo+|bar");
181         if (node == NULL) {
182                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
183                 return -1;
184         }
185         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
186         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
187         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
188         testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
189         testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
190         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
191         ec_node_free(node);
192
193         return testres;
194 }
195 /* LCOV_EXCL_STOP */
196
197 static struct ec_test ec_node_re_test = {
198         .name = "node_re",
199         .test = ec_node_re_testcase,
200 };
201
202 EC_TEST_REGISTER(ec_node_re_test);