free children in free_priv
[protos/libecoli.git] / lib / ecoli_node_or.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_strvec.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_node_or.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_test.h>
21
22 EC_LOG_TYPE_REGISTER(node_or);
23
24 struct ec_node_or {
25         struct ec_node gen;
26         struct ec_node **table;
27         unsigned int len;
28 };
29
30 static int
31 ec_node_or_parse(const struct ec_node *gen_node,
32                 struct ec_parse *state,
33                 const struct ec_strvec *strvec)
34 {
35         struct ec_node_or *node = (struct ec_node_or *)gen_node;
36         unsigned int i;
37         int ret;
38
39         for (i = 0; i < node->len; i++) {
40                 ret = ec_node_parse_child(node->table[i], state, strvec);
41                 if (ret == EC_PARSE_NOMATCH)
42                         continue;
43                 return ret;
44         }
45
46         return EC_PARSE_NOMATCH;
47 }
48
49 static int
50 ec_node_or_complete(const struct ec_node *gen_node,
51                 struct ec_comp *comp,
52                 const struct ec_strvec *strvec)
53 {
54         struct ec_node_or *node = (struct ec_node_or *)gen_node;
55         int ret;
56         size_t n;
57
58         for (n = 0; n < node->len; n++) {
59                 ret = ec_node_complete_child(node->table[n],
60                                         comp, strvec);
61                 if (ret < 0)
62                         return ret;
63         }
64
65         return 0;
66 }
67
68 static void ec_node_or_free_priv(struct ec_node *gen_node)
69 {
70         struct ec_node_or *node = (struct ec_node_or *)gen_node;
71         size_t i;
72
73         for (i = 0; i < node->len; i++)
74                 ec_node_free(node->table[i]);
75         ec_free(node->table);
76 }
77
78 static size_t
79 ec_node_or_get_children_count(const struct ec_node *gen_node)
80 {
81         struct ec_node_or *node = (struct ec_node_or *)gen_node;
82         return node->len;
83 }
84
85 static struct ec_node *
86 ec_node_or_get_child(const struct ec_node *gen_node, size_t i)
87 {
88         struct ec_node_or *node = (struct ec_node_or *)gen_node;
89
90         if (i >= node->len)
91                 return NULL;
92
93         return node->table[i];
94 }
95
96 static struct ec_node_type ec_node_or_type = {
97         .name = "or",
98         .parse = ec_node_or_parse,
99         .complete = ec_node_or_complete,
100         .size = sizeof(struct ec_node_or),
101         .free_priv = ec_node_or_free_priv,
102         .get_children_count = ec_node_or_get_children_count,
103         .get_child = ec_node_or_get_child,
104 };
105
106 EC_NODE_TYPE_REGISTER(ec_node_or_type);
107
108 int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
109 {
110         struct ec_node_or *node = (struct ec_node_or *)gen_node;
111         struct ec_node **table;
112
113         assert(node != NULL);
114
115         assert(node != NULL);
116
117         if (child == NULL) {
118                 errno = EINVAL;
119                 goto fail;
120         }
121
122         if (ec_node_check_type(gen_node, &ec_node_or_type) < 0)
123                 goto fail;
124
125         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
126         if (table == NULL)
127                 goto fail;
128
129         node->table = table;
130         table[node->len] = child;
131         node->len++;
132
133         return 0;
134
135 fail:
136         ec_node_free(child);
137         return -1;
138 }
139
140 struct ec_node *__ec_node_or(const char *id, ...)
141 {
142         struct ec_node *gen_node = NULL;
143         struct ec_node_or *node = NULL;
144         struct ec_node *child;
145         va_list ap;
146         int fail = 0;
147
148         va_start(ap, id);
149
150         gen_node = __ec_node(&ec_node_or_type, id);
151         node = (struct ec_node_or *)gen_node;
152         if (node == NULL)
153                 fail = 1;;
154
155         for (child = va_arg(ap, struct ec_node *);
156              child != EC_NODE_ENDLIST;
157              child = va_arg(ap, struct ec_node *)) {
158
159                 /* on error, don't quit the loop to avoid leaks */
160                 if (fail == 1 || child == NULL ||
161                                 ec_node_or_add(gen_node, child) < 0) {
162                         fail = 1;
163                         ec_node_free(child);
164                 }
165         }
166
167         if (fail == 1)
168                 goto fail;
169
170         va_end(ap);
171         return gen_node;
172
173 fail:
174         ec_node_free(gen_node); /* will also free children */
175         va_end(ap);
176         return NULL;
177 }
178
179 /* LCOV_EXCL_START */
180 static int ec_node_or_testcase(void)
181 {
182         struct ec_node *node;
183         int testres = 0;
184
185         node = EC_NODE_OR(EC_NO_ID,
186                 ec_node_str(EC_NO_ID, "foo"),
187                 ec_node_str(EC_NO_ID, "bar")
188         );
189         if (node == NULL) {
190                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
191                 return -1;
192         }
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, 1, "foo", "bar");
196         testres |= EC_TEST_CHECK_PARSE(node, -1, " ");
197         testres |= EC_TEST_CHECK_PARSE(node, -1, "foox");
198         testres |= EC_TEST_CHECK_PARSE(node, -1, "toto");
199         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
200         ec_node_free(node);
201
202         /* test completion */
203         node = EC_NODE_OR(EC_NO_ID,
204                 ec_node_str(EC_NO_ID, "foo"),
205                 ec_node_str(EC_NO_ID, "bar"),
206                 ec_node_str(EC_NO_ID, "bar2"),
207                 ec_node_str(EC_NO_ID, "toto"),
208                 ec_node_str(EC_NO_ID, "titi")
209         );
210         if (node == NULL) {
211                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
212                 return -1;
213         }
214         testres |= EC_TEST_CHECK_COMPLETE(node,
215                 "", EC_NODE_ENDLIST,
216                 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
217         testres |= EC_TEST_CHECK_COMPLETE(node,
218                 "f", EC_NODE_ENDLIST,
219                 "foo", EC_NODE_ENDLIST);
220         testres |= EC_TEST_CHECK_COMPLETE(node,
221                 "b", EC_NODE_ENDLIST,
222                 "bar", "bar2", EC_NODE_ENDLIST);
223         testres |= EC_TEST_CHECK_COMPLETE(node,
224                 "bar", EC_NODE_ENDLIST,
225                 "bar", "bar2", EC_NODE_ENDLIST);
226         testres |= EC_TEST_CHECK_COMPLETE(node,
227                 "t", EC_NODE_ENDLIST,
228                 "toto", "titi", EC_NODE_ENDLIST);
229         testres |= EC_TEST_CHECK_COMPLETE(node,
230                 "to", EC_NODE_ENDLIST,
231                 "toto", EC_NODE_ENDLIST);
232         testres |= EC_TEST_CHECK_COMPLETE(node,
233                 "x", EC_NODE_ENDLIST,
234                 EC_NODE_ENDLIST);
235         ec_node_free(node);
236
237         return testres;
238 }
239 /* LCOV_EXCL_STOP */
240
241 static struct ec_test ec_node_or_test = {
242         .name = "node_or",
243         .test = ec_node_or_testcase,
244 };
245
246 EC_TEST_REGISTER(ec_node_or_test);