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;
85 static struct ec_node *
86 ec_node_or_get_child(const struct ec_node *gen_node, size_t i)
88 struct ec_node_or *node = (struct ec_node_or *)gen_node;
93 return node->table[i];
96 static struct ec_node_type ec_node_or_type = {
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,
106 EC_NODE_TYPE_REGISTER(ec_node_or_type);
108 int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
110 struct ec_node_or *node = (struct ec_node_or *)gen_node;
111 struct ec_node **table;
113 assert(node != NULL);
115 assert(node != NULL);
122 if (ec_node_check_type(gen_node, &ec_node_or_type) < 0)
125 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
130 table[node->len] = child;
140 struct ec_node *__ec_node_or(const char *id, ...)
142 struct ec_node *gen_node = NULL;
143 struct ec_node_or *node = NULL;
144 struct ec_node *child;
150 gen_node = __ec_node(&ec_node_or_type, id);
151 node = (struct ec_node_or *)gen_node;
155 for (child = va_arg(ap, struct ec_node *);
156 child != EC_NODE_ENDLIST;
157 child = va_arg(ap, struct ec_node *)) {
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) {
174 ec_node_free(gen_node); /* will also free children */
179 /* LCOV_EXCL_START */
180 static int ec_node_or_testcase(void)
182 struct ec_node *node;
185 node = EC_NODE_OR(EC_NO_ID,
186 ec_node_str(EC_NO_ID, "foo"),
187 ec_node_str(EC_NO_ID, "bar")
190 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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, "");
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")
211 EC_LOG(EC_LOG_ERR, "cannot create node\n");
214 testres |= EC_TEST_CHECK_COMPLETE(node,
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,
241 static struct ec_test ec_node_or_test = {
243 .test = ec_node_or_testcase,
246 EC_TEST_REGISTER(ec_node_or_test);