free children in free_priv
[protos/libecoli.git] / lib / 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 struct ec_node *
109 ec_node_once_get_child(const struct ec_node *gen_node, size_t i)
110 {
111         struct ec_node_once *node = (struct ec_node_once *)gen_node;
112
113         if (i >= 1)
114                 return NULL;
115
116         return node->child;
117 }
118
119 static struct ec_node_type ec_node_once_type = {
120         .name = "once",
121         .parse = ec_node_once_parse,
122         .complete = ec_node_once_complete,
123         .size = sizeof(struct ec_node_once),
124         .free_priv = ec_node_once_free_priv,
125         .get_children_count = ec_node_once_get_children_count,
126         .get_child = ec_node_once_get_child,
127 };
128
129 EC_NODE_TYPE_REGISTER(ec_node_once_type);
130
131 int ec_node_once_set(struct ec_node *gen_node, struct ec_node *child)
132 {
133         struct ec_node_once *node = (struct ec_node_once *)gen_node;
134
135         if (gen_node == NULL || child == NULL) {
136                 errno = EINVAL;
137                 goto fail;
138         }
139
140         if (ec_node_check_type(gen_node, &ec_node_once_type) < 0)
141                 goto fail;
142
143         node->child = child;
144
145         return 0;
146
147 fail:
148         ec_node_free(child);
149         return -1;
150 }
151
152 struct ec_node *ec_node_once(const char *id, struct ec_node *child)
153 {
154         struct ec_node *gen_node = NULL;
155
156         if (child == NULL)
157                 return NULL;
158
159         gen_node = __ec_node(&ec_node_once_type, id);
160         if (gen_node == NULL)
161                 goto fail;
162
163         ec_node_once_set(gen_node, child);
164         child = NULL;
165
166         return gen_node;
167
168 fail:
169         ec_node_free(child);
170         return NULL;
171 }
172
173 /* LCOV_EXCL_START */
174 static int ec_node_once_testcase(void)
175 {
176         struct ec_node *node;
177         int testres = 0;
178
179         node = ec_node_many(EC_NO_ID,
180                         EC_NODE_OR(EC_NO_ID,
181                                 ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "foo")),
182                                 ec_node_str(EC_NO_ID, "bar")
183                                 ), 0, 0
184                 );
185         if (node == NULL) {
186                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
187                 return -1;
188         }
189         testres |= EC_TEST_CHECK_PARSE(node, 0);
190         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
191         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
192         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
193         testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "bar");
194         testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "bar");
195         testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo", "foo");
196         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
197         testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
198
199         testres |= EC_TEST_CHECK_COMPLETE(node,
200                 "", EC_NODE_ENDLIST,
201                 "foo", "bar", EC_NODE_ENDLIST);
202         testres |= EC_TEST_CHECK_COMPLETE(node,
203                 "f", EC_NODE_ENDLIST,
204                 "foo", EC_NODE_ENDLIST);
205         testres |= EC_TEST_CHECK_COMPLETE(node,
206                 "b", EC_NODE_ENDLIST,
207                 "bar", EC_NODE_ENDLIST);
208         testres |= EC_TEST_CHECK_COMPLETE(node,
209                 "foo", "", EC_NODE_ENDLIST,
210                 "bar", EC_NODE_ENDLIST);
211         testres |= EC_TEST_CHECK_COMPLETE(node,
212                 "bar", "", EC_NODE_ENDLIST,
213                 "foo", "bar", EC_NODE_ENDLIST);
214         ec_node_free(node);
215
216         return testres;
217 }
218 /* LCOV_EXCL_STOP */
219
220 static struct ec_test ec_node_once_test = {
221         .name = "node_once",
222         .test = ec_node_once_testcase,
223 };
224
225 EC_TEST_REGISTER(ec_node_once_test);