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]);
78 static struct ec_node_type ec_node_or_type = {
80 .parse = ec_node_or_parse,
81 .complete = ec_node_or_complete,
82 .size = sizeof(struct ec_node_or),
83 .free_priv = ec_node_or_free_priv,
86 EC_NODE_TYPE_REGISTER(ec_node_or_type);
88 int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
90 struct ec_node_or *node = (struct ec_node_or *)gen_node;
91 struct ec_node **table;
102 if (ec_node_check_type(gen_node, &ec_node_or_type) < 0)
105 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
111 if (ec_node_add_child(gen_node, child) < 0)
114 table[node->len] = child;
124 struct ec_node *__ec_node_or(const char *id, ...)
126 struct ec_node *gen_node = NULL;
127 struct ec_node_or *node = NULL;
128 struct ec_node *child;
134 gen_node = __ec_node(&ec_node_or_type, id);
135 node = (struct ec_node_or *)gen_node;
139 for (child = va_arg(ap, struct ec_node *);
140 child != EC_NODE_ENDLIST;
141 child = va_arg(ap, struct ec_node *)) {
143 /* on error, don't quit the loop to avoid leaks */
144 if (fail == 1 || child == NULL ||
145 ec_node_or_add(gen_node, child) < 0) {
158 ec_node_free(gen_node); /* will also free children */
163 /* LCOV_EXCL_START */
164 static int ec_node_or_testcase(void)
166 struct ec_node *node;
169 node = EC_NODE_OR(EC_NO_ID,
170 ec_node_str(EC_NO_ID, "foo"),
171 ec_node_str(EC_NO_ID, "bar")
174 EC_LOG(EC_LOG_ERR, "cannot create node\n");
177 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
178 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
179 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
180 testres |= EC_TEST_CHECK_PARSE(node, -1, " ");
181 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox");
182 testres |= EC_TEST_CHECK_PARSE(node, -1, "toto");
183 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
186 /* test completion */
187 node = EC_NODE_OR(EC_NO_ID,
188 ec_node_str(EC_NO_ID, "foo"),
189 ec_node_str(EC_NO_ID, "bar"),
190 ec_node_str(EC_NO_ID, "bar2"),
191 ec_node_str(EC_NO_ID, "toto"),
192 ec_node_str(EC_NO_ID, "titi")
195 EC_LOG(EC_LOG_ERR, "cannot create node\n");
198 testres |= EC_TEST_CHECK_COMPLETE(node,
200 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
201 testres |= EC_TEST_CHECK_COMPLETE(node,
202 "f", EC_NODE_ENDLIST,
203 "foo", EC_NODE_ENDLIST);
204 testres |= EC_TEST_CHECK_COMPLETE(node,
205 "b", EC_NODE_ENDLIST,
206 "bar", "bar2", EC_NODE_ENDLIST);
207 testres |= EC_TEST_CHECK_COMPLETE(node,
208 "bar", EC_NODE_ENDLIST,
209 "bar", "bar2", EC_NODE_ENDLIST);
210 testres |= EC_TEST_CHECK_COMPLETE(node,
211 "t", EC_NODE_ENDLIST,
212 "toto", "titi", EC_NODE_ENDLIST);
213 testres |= EC_TEST_CHECK_COMPLETE(node,
214 "to", EC_NODE_ENDLIST,
215 "toto", EC_NODE_ENDLIST);
216 testres |= EC_TEST_CHECK_COMPLETE(node,
217 "x", EC_NODE_ENDLIST,
225 static struct ec_test ec_node_or_test = {
227 .test = ec_node_or_testcase,
230 EC_TEST_REGISTER(ec_node_or_test);