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.h>
19 #include <ecoli_config.h>
20 #include <ecoli_node_helper.h>
21 #include <ecoli_node_or.h>
22 #include <ecoli_node_str.h>
23 #include <ecoli_test.h>
25 EC_LOG_TYPE_REGISTER(node_or);
29 struct ec_node **table;
34 ec_node_or_parse(const struct ec_node *gen_node,
35 struct ec_parse *state,
36 const struct ec_strvec *strvec)
38 struct ec_node_or *node = (struct ec_node_or *)gen_node;
42 for (i = 0; i < node->len; i++) {
43 ret = ec_node_parse_child(node->table[i], state, strvec);
44 if (ret == EC_PARSE_NOMATCH)
49 return EC_PARSE_NOMATCH;
53 ec_node_or_complete(const struct ec_node *gen_node,
55 const struct ec_strvec *strvec)
57 struct ec_node_or *node = (struct ec_node_or *)gen_node;
61 for (n = 0; n < node->len; n++) {
62 ret = ec_node_complete_child(node->table[n],
71 static void ec_node_or_free_priv(struct ec_node *gen_node)
73 struct ec_node_or *node = (struct ec_node_or *)gen_node;
76 for (i = 0; i < node->len; i++)
77 ec_node_free(node->table[i]);
83 static const struct ec_config_schema ec_node_or_subschema[] = {
85 .desc = "A child node which is part of the choice.",
86 .type = EC_CONFIG_TYPE_NODE,
89 .type = EC_CONFIG_TYPE_NONE,
93 static const struct ec_config_schema ec_node_or_schema[] = {
96 .desc = "The list of children nodes defining the choice "
98 .type = EC_CONFIG_TYPE_LIST,
99 .subschema = ec_node_or_subschema,
102 .type = EC_CONFIG_TYPE_NONE,
106 static int ec_node_or_set_config(struct ec_node *gen_node,
107 const struct ec_config *config)
109 struct ec_node_or *node = (struct ec_node_or *)gen_node;
110 struct ec_node **table = NULL;
113 table = ec_node_config_node_list_to_table(
114 ec_config_dict_get(config, "children"), &len);
118 for (i = 0; i < node->len; i++)
119 ec_node_free(node->table[i]);
120 ec_free(node->table);
127 for (i = 0; i < len; i++)
128 ec_node_free(table[i]);
134 ec_node_or_get_children_count(const struct ec_node *gen_node)
136 struct ec_node_or *node = (struct ec_node_or *)gen_node;
141 ec_node_or_get_child(const struct ec_node *gen_node, size_t i,
142 struct ec_node **child, unsigned int *refs)
144 struct ec_node_or *node = (struct ec_node_or *)gen_node;
149 *child = node->table[i];
150 /* each child node is referenced twice: once in the config and
151 * once in the node->table[] */
156 static struct ec_node_type ec_node_or_type = {
158 .schema = ec_node_or_schema,
159 .set_config = ec_node_or_set_config,
160 .parse = ec_node_or_parse,
161 .complete = ec_node_or_complete,
162 .size = sizeof(struct ec_node_or),
163 .free_priv = ec_node_or_free_priv,
164 .get_children_count = ec_node_or_get_children_count,
165 .get_child = ec_node_or_get_child,
168 EC_NODE_TYPE_REGISTER(ec_node_or_type);
170 int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
172 struct ec_node_or *node = (struct ec_node_or *)gen_node;
173 const struct ec_config *cur_config = NULL;
174 struct ec_config *config = NULL, *children;
177 assert(node != NULL);
179 /* XXX factorize this code in a helper */
181 if (ec_node_check_type(gen_node, &ec_node_or_type) < 0)
184 cur_config = ec_node_get_config(gen_node);
185 if (cur_config == NULL)
186 config = ec_config_dict();
188 config = ec_config_dup(cur_config);
192 children = ec_config_dict_get(config, "children");
193 if (children == NULL) {
194 children = ec_config_list();
195 if (children == NULL)
198 if (ec_config_dict_set(config, "children", children) < 0)
199 goto fail; /* children list is freed on error */
202 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
207 ret = ec_node_set_config(gen_node, config);
208 config = NULL; /* freed */
215 ec_config_free(config);
220 struct ec_node *__ec_node_or(const char *id, ...)
222 struct ec_config *config = NULL, *children = NULL;
223 struct ec_node *gen_node = NULL;
224 struct ec_node *child;
229 child = va_arg(ap, struct ec_node *);
231 gen_node = ec_node_from_type(&ec_node_or_type, id);
232 if (gen_node == NULL)
233 goto fail_free_children;
235 config = ec_config_dict();
237 goto fail_free_children;
239 children = ec_config_list();
240 if (children == NULL)
241 goto fail_free_children;
243 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) {
245 goto fail_free_children;
247 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
249 goto fail_free_children;
253 if (ec_config_dict_set(config, "children", children) < 0) {
254 children = NULL; /* freed */
259 ret = ec_node_set_config(gen_node, config);
260 config = NULL; /* freed */
269 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *))
272 ec_node_free(gen_node); /* will also free added children */
273 ec_config_free(children);
274 ec_config_free(config);
280 /* LCOV_EXCL_START */
281 static int ec_node_or_testcase(void)
283 struct ec_node *node;
286 node = EC_NODE_OR(EC_NO_ID,
287 ec_node_str(EC_NO_ID, "foo"),
288 ec_node_str(EC_NO_ID, "bar")
291 EC_LOG(EC_LOG_ERR, "cannot create node\n");
294 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
295 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
296 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
297 testres |= EC_TEST_CHECK_PARSE(node, -1, " ");
298 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox");
299 testres |= EC_TEST_CHECK_PARSE(node, -1, "toto");
300 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
303 /* test completion */
304 node = EC_NODE_OR(EC_NO_ID,
305 ec_node_str(EC_NO_ID, "foo"),
306 ec_node_str(EC_NO_ID, "bar"),
307 ec_node_str(EC_NO_ID, "bar2"),
308 ec_node_str(EC_NO_ID, "toto"),
309 ec_node_str(EC_NO_ID, "titi")
312 EC_LOG(EC_LOG_ERR, "cannot create node\n");
315 testres |= EC_TEST_CHECK_COMPLETE(node,
317 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
318 testres |= EC_TEST_CHECK_COMPLETE(node,
319 "f", EC_NODE_ENDLIST,
320 "foo", EC_NODE_ENDLIST);
321 testres |= EC_TEST_CHECK_COMPLETE(node,
322 "b", EC_NODE_ENDLIST,
323 "bar", "bar2", EC_NODE_ENDLIST);
324 testres |= EC_TEST_CHECK_COMPLETE(node,
325 "bar", EC_NODE_ENDLIST,
326 "bar", "bar2", EC_NODE_ENDLIST);
327 testres |= EC_TEST_CHECK_COMPLETE(node,
328 "t", EC_NODE_ENDLIST,
329 "toto", "titi", EC_NODE_ENDLIST);
330 testres |= EC_TEST_CHECK_COMPLETE(node,
331 "to", EC_NODE_ENDLIST,
332 "toto", EC_NODE_ENDLIST);
333 testres |= EC_TEST_CHECK_COMPLETE(node,
334 "x", EC_NODE_ENDLIST,
342 static struct ec_test ec_node_or_test = {
344 .test = ec_node_or_testcase,
347 EC_TEST_REGISTER(ec_node_or_test);