1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
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>
22 EC_LOG_TYPE_REGISTER(node_or);
26 struct ec_node **table;
31 ec_node_or_parse(const struct ec_node *gen_node,
32 struct ec_parse *state,
33 const struct ec_strvec *strvec)
35 struct ec_node_or *node = (struct ec_node_or *)gen_node;
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)
46 return EC_PARSE_NOMATCH;
50 ec_node_or_complete(const struct ec_node *gen_node,
52 const struct ec_strvec *strvec)
54 struct ec_node_or *node = (struct ec_node_or *)gen_node;
58 for (n = 0; n < node->len; n++) {
59 ret = ec_node_complete_child(node->table[n],
68 static void ec_node_or_free_priv(struct ec_node *gen_node)
70 struct ec_node_or *node = (struct ec_node_or *)gen_node;
73 for (i = 0; i < node->len; i++)
74 ec_node_free(node->table[i]);
79 ec_node_or_get_children_count(const struct ec_node *gen_node)
81 struct ec_node_or *node = (struct ec_node_or *)gen_node;
86 ec_node_or_get_child(const struct ec_node *gen_node, size_t i,
87 struct ec_node **child, unsigned int *refs)
89 struct ec_node_or *node = (struct ec_node_or *)gen_node;
94 *child = node->table[i];
99 static struct ec_node_type ec_node_or_type = {
101 .parse = ec_node_or_parse,
102 .complete = ec_node_or_complete,
103 .size = sizeof(struct ec_node_or),
104 .free_priv = ec_node_or_free_priv,
105 .get_children_count = ec_node_or_get_children_count,
106 .get_child = ec_node_or_get_child,
109 EC_NODE_TYPE_REGISTER(ec_node_or_type);
111 int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
113 struct ec_node_or *node = (struct ec_node_or *)gen_node;
114 struct ec_node **table;
116 assert(node != NULL);
118 assert(node != NULL);
125 if (ec_node_check_type(gen_node, &ec_node_or_type) < 0)
128 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
133 table[node->len] = child;
143 struct ec_node *__ec_node_or(const char *id, ...)
145 struct ec_node *gen_node = NULL;
146 struct ec_node_or *node = NULL;
147 struct ec_node *child;
153 gen_node = ec_node_from_type(&ec_node_or_type, id);
154 node = (struct ec_node_or *)gen_node;
158 for (child = va_arg(ap, struct ec_node *);
159 child != EC_NODE_ENDLIST;
160 child = va_arg(ap, struct ec_node *)) {
162 /* on error, don't quit the loop to avoid leaks */
163 if (fail == 1 || child == NULL ||
164 ec_node_or_add(gen_node, child) < 0) {
177 ec_node_free(gen_node); /* will also free children */
182 /* LCOV_EXCL_START */
183 static int ec_node_or_testcase(void)
185 struct ec_node *node;
188 node = EC_NODE_OR(EC_NO_ID,
189 ec_node_str(EC_NO_ID, "foo"),
190 ec_node_str(EC_NO_ID, "bar")
193 EC_LOG(EC_LOG_ERR, "cannot create node\n");
196 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
197 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
198 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
199 testres |= EC_TEST_CHECK_PARSE(node, -1, " ");
200 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox");
201 testres |= EC_TEST_CHECK_PARSE(node, -1, "toto");
202 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
205 /* test completion */
206 node = EC_NODE_OR(EC_NO_ID,
207 ec_node_str(EC_NO_ID, "foo"),
208 ec_node_str(EC_NO_ID, "bar"),
209 ec_node_str(EC_NO_ID, "bar2"),
210 ec_node_str(EC_NO_ID, "toto"),
211 ec_node_str(EC_NO_ID, "titi")
214 EC_LOG(EC_LOG_ERR, "cannot create node\n");
217 testres |= EC_TEST_CHECK_COMPLETE(node,
219 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
220 testres |= EC_TEST_CHECK_COMPLETE(node,
221 "f", EC_NODE_ENDLIST,
222 "foo", EC_NODE_ENDLIST);
223 testres |= EC_TEST_CHECK_COMPLETE(node,
224 "b", EC_NODE_ENDLIST,
225 "bar", "bar2", EC_NODE_ENDLIST);
226 testres |= EC_TEST_CHECK_COMPLETE(node,
227 "bar", EC_NODE_ENDLIST,
228 "bar", "bar2", EC_NODE_ENDLIST);
229 testres |= EC_TEST_CHECK_COMPLETE(node,
230 "t", EC_NODE_ENDLIST,
231 "toto", "titi", EC_NODE_ENDLIST);
232 testres |= EC_TEST_CHECK_COMPLETE(node,
233 "to", EC_NODE_ENDLIST,
234 "toto", EC_NODE_ENDLIST);
235 testres |= EC_TEST_CHECK_COMPLETE(node,
236 "x", EC_NODE_ENDLIST,
244 static struct ec_test ec_node_or_test = {
246 .test = ec_node_or_testcase,
249 EC_TEST_REGISTER(ec_node_or_test);