api documentation for ec_parse
[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_pnode *pstate,
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)pstate;
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         .size = sizeof(struct ec_node_re),
120         .free_priv = ec_node_re_free_priv,
121 };
122
123 EC_NODE_TYPE_REGISTER(ec_node_re_type);
124
125 int ec_node_re_set_regexp(struct ec_node *node, const char *str)
126 {
127         struct ec_config *config = NULL;
128         int ret;
129
130         EC_CHECK_ARG(str != NULL, -1, EINVAL);
131
132         if (ec_node_check_type(node, &ec_node_re_type) < 0)
133                 goto fail;
134
135         config = ec_config_dict();
136         if (config == NULL)
137                 goto fail;
138
139         ret = ec_config_dict_set(config, "pattern", ec_config_string(str));
140         if (ret < 0)
141                 goto fail;
142
143         ret = ec_node_set_config(node, config);
144         config = NULL;
145         if (ret < 0)
146                 goto fail;
147
148         return 0;
149
150 fail:
151         ec_config_free(config);
152         return -1;
153 }
154
155 struct ec_node *ec_node_re(const char *id, const char *re_str)
156 {
157         struct ec_node *node = NULL;
158
159         node = ec_node_from_type(&ec_node_re_type, id);
160         if (node == NULL)
161                 goto fail;
162
163         if (ec_node_re_set_regexp(node, re_str) < 0)
164                 goto fail;
165
166         return node;
167
168 fail:
169         ec_node_free(node);
170         return NULL;
171 }
172
173 /* LCOV_EXCL_START */
174 static int ec_node_re_testcase(void)
175 {
176         struct ec_node *node;
177         int testres = 0;
178
179         node = ec_node_re(EC_NO_ID, "fo+|bar");
180         if (node == NULL) {
181                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
182                 return -1;
183         }
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, "");
190         ec_node_free(node);
191
192         return testres;
193 }
194 /* LCOV_EXCL_STOP */
195
196 static struct ec_test ec_node_re_test = {
197         .name = "node_re",
198         .test = ec_node_re_testcase,
199 };
200
201 EC_TEST_REGISTER(ec_node_re_test);