63098783dbaeb03a5eed9f0b93d910bfeb77a6f3
[protos/libecoli.git] / libecoli / ecoli_node_once.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 <assert.h>
9 #include <stdarg.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_node.h>
17 #include <ecoli_parse.h>
18 #include <ecoli_complete.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_or.h>
21 #include <ecoli_node_many.h>
22 #include <ecoli_node_once.h>
23
24 EC_LOG_TYPE_REGISTER(node_once);
25
26 struct ec_node_once {
27         struct ec_node gen;
28         struct ec_node *child;
29 };
30
31 static unsigned int
32 count_node(struct ec_parse *parse, const struct ec_node *node)
33 {
34         struct ec_parse *child;
35         unsigned int count = 0;
36
37         if (parse == NULL)
38                 return 0;
39
40         if (ec_parse_get_node(parse) == node)
41                 count++;
42
43         EC_PARSE_FOREACH_CHILD(child, parse)
44                 count += count_node(child, node);
45
46         return count;
47 }
48
49 static int
50 ec_node_once_parse(const struct ec_node *gen_node,
51                 struct ec_parse *state,
52                 const struct ec_strvec *strvec)
53 {
54         struct ec_node_once *node = (struct ec_node_once *)gen_node;
55         unsigned int count;
56
57         /* count the number of occurences of the node: if already parsed,
58          * do not match
59          */
60         count = count_node(ec_parse_get_root(state), node->child);
61         if (count > 0)
62                 return EC_PARSE_NOMATCH;
63
64         return ec_node_parse_child(node->child, state, strvec);
65 }
66
67 static int
68 ec_node_once_complete(const struct ec_node *gen_node,
69                 struct ec_comp *comp,
70                 const struct ec_strvec *strvec)
71 {
72         struct ec_node_once *node = (struct ec_node_once *)gen_node;
73         struct ec_parse *parse = ec_comp_get_state(comp);
74         unsigned int count;
75         int ret;
76
77         /* count the number of occurences of the node: if already parsed,
78          * do not match
79          */
80         count = count_node(ec_parse_get_root(parse), node->child);
81         if (count > 0)
82                 return 0;
83
84         ret = ec_node_complete_child(node->child, comp, strvec);
85         if (ret < 0)
86                 return ret;
87
88         return 0;
89 }
90
91 static void ec_node_once_free_priv(struct ec_node *gen_node)
92 {
93         struct ec_node_once *node = (struct ec_node_once *)gen_node;
94
95         ec_node_free(node->child);
96 }
97
98 static size_t
99 ec_node_once_get_children_count(const struct ec_node *gen_node)
100 {
101         struct ec_node_once *node = (struct ec_node_once *)gen_node;
102
103         if (node->child)
104                 return 1;
105         return 0;
106 }
107
108 static int
109 ec_node_once_get_child(const struct ec_node *gen_node, size_t i,
110                 struct ec_node **child, unsigned int *refs)
111 {
112         struct ec_node_once *node = (struct ec_node_once *)gen_node;
113
114         if (i >= 1)
115                 return -1;
116
117         *child = node->child;
118         *refs = 1;
119         return 0;
120 }
121
122 static struct ec_node_type ec_node_once_type = {
123         .name = "once",
124         .parse = ec_node_once_parse,
125         .complete = ec_node_once_complete,
126         .size = sizeof(struct ec_node_once),
127         .free_priv = ec_node_once_free_priv,
128         .get_children_count = ec_node_once_get_children_count,
129         .get_child = ec_node_once_get_child,
130 };
131
132 EC_NODE_TYPE_REGISTER(ec_node_once_type);
133
134 int ec_node_once_set(struct ec_node *gen_node, struct ec_node *child)
135 {
136         struct ec_node_once *node = (struct ec_node_once *)gen_node;
137
138         if (gen_node == NULL || child == NULL) {
139                 errno = EINVAL;
140                 goto fail;
141         }
142
143         if (ec_node_check_type(gen_node, &ec_node_once_type) < 0)
144                 goto fail;
145
146         node->child = child;
147
148         return 0;
149
150 fail:
151         ec_node_free(child);
152         return -1;
153 }
154
155 struct ec_node *ec_node_once(const char *id, struct ec_node *child)
156 {
157         struct ec_node *gen_node = NULL;
158
159         if (child == NULL)
160                 return NULL;
161
162         gen_node = ec_node_from_type(&ec_node_once_type, id);
163         if (gen_node == NULL)
164                 goto fail;
165
166         ec_node_once_set(gen_node, child);
167         child = NULL;
168
169         return gen_node;
170
171 fail:
172         ec_node_free(child);
173         return NULL;
174 }
175
176 /* LCOV_EXCL_START */
177 static int ec_node_once_testcase(void)
178 {
179         struct ec_node *node;
180         int testres = 0;
181
182         node = ec_node_many(EC_NO_ID,
183                         EC_NODE_OR(EC_NO_ID,
184                                 ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "foo")),
185                                 ec_node_str(EC_NO_ID, "bar")
186                                 ), 0, 0
187                 );
188         if (node == NULL) {
189                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
190                 return -1;
191         }
192         testres |= EC_TEST_CHECK_PARSE(node, 0);
193         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
194         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
195         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
196         testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "bar");
197         testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "bar");
198         testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo", "foo");
199         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
200         testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
201
202         testres |= EC_TEST_CHECK_COMPLETE(node,
203                 "", EC_NODE_ENDLIST,
204                 "foo", "bar", EC_NODE_ENDLIST);
205         testres |= EC_TEST_CHECK_COMPLETE(node,
206                 "f", EC_NODE_ENDLIST,
207                 "foo", EC_NODE_ENDLIST);
208         testres |= EC_TEST_CHECK_COMPLETE(node,
209                 "b", EC_NODE_ENDLIST,
210                 "bar", EC_NODE_ENDLIST);
211         testres |= EC_TEST_CHECK_COMPLETE(node,
212                 "foo", "", EC_NODE_ENDLIST,
213                 "bar", EC_NODE_ENDLIST);
214         testres |= EC_TEST_CHECK_COMPLETE(node,
215                 "bar", "", EC_NODE_ENDLIST,
216                 "foo", "bar", EC_NODE_ENDLIST);
217         ec_node_free(node);
218
219         return testres;
220 }
221 /* LCOV_EXCL_STOP */
222
223 static struct ec_test ec_node_once_test = {
224         .name = "node_once",
225         .test = ec_node_once_testcase,
226 };
227
228 EC_TEST_REGISTER(ec_node_once_test);