parsed -> parse
[protos/libecoli.git] / lib / 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         if (str == NULL)
79                 return -EINVAL;
80
81         ret = -ENOMEM;
82         str_copy = ec_strdup(str);
83         if (str_copy == NULL)
84                 goto fail;
85
86         ret = -EINVAL;
87         if (regcomp(&re, str_copy, REG_EXTENDED) != 0)
88                 goto fail;
89
90         if (node->re_str != NULL) {
91                 ec_free(node->re_str);
92                 regfree(&node->re);
93         }
94         node->re_str = str_copy;
95         node->re = re;
96
97         return 0;
98
99 fail:
100         ec_free(str_copy);
101         return ret;
102 }
103
104 struct ec_node *ec_node_re(const char *id, const char *re_str)
105 {
106         struct ec_node *gen_node = NULL;
107
108         gen_node = __ec_node(&ec_node_re_type, id);
109         if (gen_node == NULL)
110                 goto fail;
111
112         if (ec_node_re_set_regexp(gen_node, re_str) < 0)
113                 goto fail;
114
115         return gen_node;
116
117 fail:
118         ec_node_free(gen_node);
119         return NULL;
120 }
121
122 /* LCOV_EXCL_START */
123 static int ec_node_re_testcase(void)
124 {
125         struct ec_node *node;
126         int testres = 0;
127
128         node = ec_node_re(EC_NO_ID, "fo+|bar");
129         if (node == NULL) {
130                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
131                 return -1;
132         }
133         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
134         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
135         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
136         testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
137         testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
138         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
139         ec_node_free(node);
140
141         return testres;
142 }
143 /* LCOV_EXCL_STOP */
144
145 static struct ec_test ec_node_re_test = {
146         .name = "node_re",
147         .test = ec_node_re_testcase,
148 };
149
150 EC_TEST_REGISTER(ec_node_re_test);