6ac5182517d8e5262818ed3e6abc331c6a21bac3
[protos/libecoli.git] / libecoli / 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_node_re.h>
19
20 EC_LOG_TYPE_REGISTER(node_re);
21
22 struct ec_node_re {
23         struct ec_node gen;
24         char *re_str;
25         regex_t re;
26 };
27
28 static int
29 ec_node_re_parse(const struct ec_node *gen_node,
30                 struct ec_parse *state,
31                 const struct ec_strvec *strvec)
32 {
33         struct ec_node_re *node = (struct ec_node_re *)gen_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(&node->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 *gen_node)
52 {
53         struct ec_node_re *node = (struct ec_node_re *)gen_node;
54
55         if (node->re_str != NULL) {
56                 ec_free(node->re_str);
57                 regfree(&node->re);
58         }
59 }
60
61 static struct ec_node_type ec_node_re_type = {
62         .name = "re",
63         .parse = ec_node_re_parse,
64         .complete = ec_node_complete_unknown,
65         .size = sizeof(struct ec_node_re),
66         .free_priv = ec_node_re_free_priv,
67 };
68
69 EC_NODE_TYPE_REGISTER(ec_node_re_type);
70
71 int ec_node_re_set_regexp(struct ec_node *gen_node, const char *str)
72 {
73         struct ec_node_re *node = (struct ec_node_re *)gen_node;
74         char *str_copy = NULL;
75         regex_t re;
76         int ret;
77
78         EC_CHECK_ARG(str != NULL, -1, EINVAL);
79
80         str_copy = ec_strdup(str);
81         if (str_copy == NULL)
82                 goto fail;
83
84         ret = regcomp(&re, str_copy, REG_EXTENDED);
85         if (ret != 0) {
86                 if (ret == REG_ESPACE)
87                         errno = ENOMEM;
88                 else
89                         errno = EINVAL;
90                 goto fail;
91         }
92
93         if (node->re_str != NULL) {
94                 ec_free(node->re_str);
95                 regfree(&node->re);
96         }
97         node->re_str = str_copy;
98         node->re = re;
99
100         return 0;
101
102 fail:
103         ec_free(str_copy);
104         return -1;
105 }
106
107 struct ec_node *ec_node_re(const char *id, const char *re_str)
108 {
109         struct ec_node *gen_node = NULL;
110
111         gen_node = ec_node_from_type(&ec_node_re_type, id);
112         if (gen_node == NULL)
113                 goto fail;
114
115         if (ec_node_re_set_regexp(gen_node, re_str) < 0)
116                 goto fail;
117
118         return gen_node;
119
120 fail:
121         ec_node_free(gen_node);
122         return NULL;
123 }
124
125 /* LCOV_EXCL_START */
126 static int ec_node_re_testcase(void)
127 {
128         struct ec_node *node;
129         int testres = 0;
130
131         node = ec_node_re(EC_NO_ID, "fo+|bar");
132         if (node == NULL) {
133                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
134                 return -1;
135         }
136         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
137         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
138         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
139         testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
140         testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
141         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
142         ec_node_free(node);
143
144         return testres;
145 }
146 /* LCOV_EXCL_STOP */
147
148 static struct ec_test ec_node_re_test = {
149         .name = "node_re",
150         .test = ec_node_re_testcase,
151 };
152
153 EC_TEST_REGISTER(ec_node_re_test);