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);
28 struct ec_node **table;
33 ec_node_or_parse(const struct ec_node *node,
34 struct ec_parse *state,
35 const struct ec_strvec *strvec)
37 struct ec_node_or *priv = ec_node_priv(node);
41 for (i = 0; i < priv->len; i++) {
42 ret = ec_node_parse_child(priv->table[i], state, strvec);
43 if (ret == EC_PARSE_NOMATCH)
48 return EC_PARSE_NOMATCH;
52 ec_node_or_complete(const struct ec_node *node,
54 const struct ec_strvec *strvec)
56 struct ec_node_or *priv = ec_node_priv(node);
60 for (n = 0; n < priv->len; n++) {
61 ret = ec_node_complete_child(priv->table[n],
70 static void ec_node_or_free_priv(struct ec_node *node)
72 struct ec_node_or *priv = ec_node_priv(node);
75 for (i = 0; i < priv->len; i++)
76 ec_node_free(priv->table[i]);
82 static const struct ec_config_schema ec_node_or_subschema[] = {
84 .desc = "A child node which is part of the choice.",
85 .type = EC_CONFIG_TYPE_NODE,
88 .type = EC_CONFIG_TYPE_NONE,
92 static const struct ec_config_schema ec_node_or_schema[] = {
95 .desc = "The list of children nodes defining the choice "
97 .type = EC_CONFIG_TYPE_LIST,
98 .subschema = ec_node_or_subschema,
101 .type = EC_CONFIG_TYPE_NONE,
105 static int ec_node_or_set_config(struct ec_node *node,
106 const struct ec_config *config)
108 struct ec_node_or *priv = ec_node_priv(node);
109 struct ec_node **table = NULL;
112 table = ec_node_config_node_list_to_table(
113 ec_config_dict_get(config, "children"), &len);
117 for (i = 0; i < priv->len; i++)
118 ec_node_free(priv->table[i]);
119 ec_free(priv->table);
126 for (i = 0; i < len; i++)
127 ec_node_free(table[i]);
133 ec_node_or_get_children_count(const struct ec_node *node)
135 struct ec_node_or *priv = ec_node_priv(node);
140 ec_node_or_get_child(const struct ec_node *node, size_t i,
141 struct ec_node **child, unsigned int *refs)
143 struct ec_node_or *priv = ec_node_priv(node);
148 *child = priv->table[i];
149 /* each child node is referenced twice: once in the config and
150 * once in the priv->table[] */
155 static struct ec_node_type ec_node_or_type = {
157 .schema = ec_node_or_schema,
158 .set_config = ec_node_or_set_config,
159 .parse = ec_node_or_parse,
160 .complete = ec_node_or_complete,
161 .size = sizeof(struct ec_node_or),
162 .free_priv = ec_node_or_free_priv,
163 .get_children_count = ec_node_or_get_children_count,
164 .get_child = ec_node_or_get_child,
167 EC_NODE_TYPE_REGISTER(ec_node_or_type);
169 int ec_node_or_add(struct ec_node *node, struct ec_node *child)
171 const struct ec_config *cur_config = NULL;
172 struct ec_config *config = NULL, *children;
175 assert(node != NULL);
177 /* XXX factorize this code in a helper */
179 if (ec_node_check_type(node, &ec_node_or_type) < 0)
182 cur_config = ec_node_get_config(node);
183 if (cur_config == NULL)
184 config = ec_config_dict();
186 config = ec_config_dup(cur_config);
190 children = ec_config_dict_get(config, "children");
191 if (children == NULL) {
192 children = ec_config_list();
193 if (children == NULL)
196 if (ec_config_dict_set(config, "children", children) < 0)
197 goto fail; /* children list is freed on error */
200 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
205 ret = ec_node_set_config(node, config);
206 config = NULL; /* freed */
213 ec_config_free(config);
218 struct ec_node *__ec_node_or(const char *id, ...)
220 struct ec_config *config = NULL, *children = NULL;
221 struct ec_node *node = NULL;
222 struct ec_node *child;
227 child = va_arg(ap, struct ec_node *);
229 node = ec_node_from_type(&ec_node_or_type, id);
231 goto fail_free_children;
233 config = ec_config_dict();
235 goto fail_free_children;
237 children = ec_config_list();
238 if (children == NULL)
239 goto fail_free_children;
241 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) {
243 goto fail_free_children;
245 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
247 goto fail_free_children;
251 if (ec_config_dict_set(config, "children", children) < 0) {
252 children = NULL; /* freed */
257 ret = ec_node_set_config(node, config);
258 config = NULL; /* freed */
267 for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *))
270 ec_node_free(node); /* will also free added children */
271 ec_config_free(children);
272 ec_config_free(config);
278 /* LCOV_EXCL_START */
279 static int ec_node_or_testcase(void)
281 struct ec_node *node;
284 node = EC_NODE_OR(EC_NO_ID,
285 ec_node_str(EC_NO_ID, "foo"),
286 ec_node_str(EC_NO_ID, "bar")
289 EC_LOG(EC_LOG_ERR, "cannot create node\n");
292 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
293 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
294 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
295 testres |= EC_TEST_CHECK_PARSE(node, -1, " ");
296 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox");
297 testres |= EC_TEST_CHECK_PARSE(node, -1, "toto");
298 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
301 /* test completion */
302 node = EC_NODE_OR(EC_NO_ID,
303 ec_node_str(EC_NO_ID, "foo"),
304 ec_node_str(EC_NO_ID, "bar"),
305 ec_node_str(EC_NO_ID, "bar2"),
306 ec_node_str(EC_NO_ID, "toto"),
307 ec_node_str(EC_NO_ID, "titi")
310 EC_LOG(EC_LOG_ERR, "cannot create node\n");
313 testres |= EC_TEST_CHECK_COMPLETE(node,
315 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
316 testres |= EC_TEST_CHECK_COMPLETE(node,
317 "f", EC_NODE_ENDLIST,
318 "foo", EC_NODE_ENDLIST);
319 testres |= EC_TEST_CHECK_COMPLETE(node,
320 "b", EC_NODE_ENDLIST,
321 "bar", "bar2", EC_NODE_ENDLIST);
322 testres |= EC_TEST_CHECK_COMPLETE(node,
323 "bar", EC_NODE_ENDLIST,
324 "bar", "bar2", EC_NODE_ENDLIST);
325 testres |= EC_TEST_CHECK_COMPLETE(node,
326 "t", EC_NODE_ENDLIST,
327 "toto", "titi", EC_NODE_ENDLIST);
328 testres |= EC_TEST_CHECK_COMPLETE(node,
329 "to", EC_NODE_ENDLIST,
330 "toto", EC_NODE_ENDLIST);
331 testres |= EC_TEST_CHECK_COMPLETE(node,
332 "x", EC_NODE_ENDLIST,
340 static struct ec_test ec_node_or_test = {
342 .test = ec_node_or_testcase,
345 EC_TEST_REGISTER(ec_node_or_test);