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_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_node.h>
17 #include <ecoli_parse.h>
18 #include <ecoli_complete.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_or.h>
21 #include <ecoli_node_many.h>
22 #include <ecoli_config.h>
23 #include <ecoli_node_once.h>
25 EC_LOG_TYPE_REGISTER(node_once);
28 struct ec_node *child;
32 count_node(struct ec_pnode *parse, const struct ec_node *node)
34 struct ec_pnode *child;
35 unsigned int count = 0;
40 if (ec_pnode_get_node(parse) == node)
43 EC_PNODE_FOREACH_CHILD(child, parse)
44 count += count_node(child, node);
50 ec_node_once_parse(const struct ec_node *node,
51 struct ec_pnode *state,
52 const struct ec_strvec *strvec)
54 struct ec_node_once *priv = ec_node_priv(node);
57 /* count the number of occurences of the node: if already parsed,
60 count = count_node(ec_pnode_get_root(state), priv->child);
62 return EC_PARSE_NOMATCH;
64 return ec_parse_child(priv->child, state, strvec);
68 ec_node_once_complete(const struct ec_node *node,
70 const struct ec_strvec *strvec)
72 struct ec_node_once *priv = ec_node_priv(node);
73 struct ec_pnode *parse = ec_comp_get_state(comp);
77 /* count the number of occurences of the node: if already parsed,
80 count = count_node(ec_pnode_get_root(parse), priv->child);
84 ret = ec_complete_child(priv->child, comp, strvec);
91 static void ec_node_once_free_priv(struct ec_node *node)
93 struct ec_node_once *priv = ec_node_priv(node);
95 ec_node_free(priv->child);
99 ec_node_once_get_children_count(const struct ec_node *node)
101 struct ec_node_once *priv = ec_node_priv(node);
109 ec_node_once_get_child(const struct ec_node *node, size_t i,
110 struct ec_node **child, unsigned int *refs)
112 struct ec_node_once *priv = ec_node_priv(node);
117 *child = priv->child;
122 static const struct ec_config_schema ec_node_once_schema[] = {
125 .desc = "The child node.",
126 .type = EC_CONFIG_TYPE_NODE,
129 .type = EC_CONFIG_TYPE_NONE,
133 static int ec_node_once_set_config(struct ec_node *node,
134 const struct ec_config *config)
136 struct ec_node_once *priv = ec_node_priv(node);
137 const struct ec_config *child;
139 child = ec_config_dict_get(config, "child");
142 if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
147 if (priv->child != NULL)
148 ec_node_free(priv->child);
149 priv->child = ec_node_clone(child->node);
157 static struct ec_node_type ec_node_once_type = {
159 .schema = ec_node_once_schema,
160 .set_config = ec_node_once_set_config,
161 .parse = ec_node_once_parse,
162 .complete = ec_node_once_complete,
163 .size = sizeof(struct ec_node_once),
164 .free_priv = ec_node_once_free_priv,
165 .get_children_count = ec_node_once_get_children_count,
166 .get_child = ec_node_once_get_child,
169 EC_NODE_TYPE_REGISTER(ec_node_once_type);
172 ec_node_once_set_child(struct ec_node *node, struct ec_node *child)
174 const struct ec_config *cur_config = NULL;
175 struct ec_config *config = NULL;
178 if (ec_node_check_type(node, &ec_node_once_type) < 0)
181 cur_config = ec_node_get_config(node);
182 if (cur_config == NULL)
183 config = ec_config_dict();
185 config = ec_config_dup(cur_config);
189 if (ec_config_dict_set(config, "child", ec_config_node(child)) < 0) {
190 child = NULL; /* freed */
193 child = NULL; /* freed */
195 ret = ec_node_set_config(node, config);
196 config = NULL; /* freed */
203 ec_config_free(config);
208 struct ec_node *ec_node_once(const char *id, struct ec_node *child)
210 struct ec_node *node = NULL;
215 node = ec_node_from_type(&ec_node_once_type, id);
219 ec_node_once_set_child(node, child);
229 /* LCOV_EXCL_START */
230 static int ec_node_once_testcase(void)
232 struct ec_node *node;
235 node = ec_node_many(EC_NO_ID,
237 ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "foo")),
238 ec_node_str(EC_NO_ID, "bar")
242 EC_LOG(EC_LOG_ERR, "cannot create node\n");
245 testres |= EC_TEST_CHECK_PARSE(node, 0);
246 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
247 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
248 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
249 testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "bar");
250 testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "bar");
251 testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo", "foo");
252 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
253 testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
255 testres |= EC_TEST_CHECK_COMPLETE(node,
257 "foo", "bar", EC_VA_END);
258 testres |= EC_TEST_CHECK_COMPLETE(node,
261 testres |= EC_TEST_CHECK_COMPLETE(node,
264 testres |= EC_TEST_CHECK_COMPLETE(node,
265 "foo", "", EC_VA_END,
267 testres |= EC_TEST_CHECK_COMPLETE(node,
268 "bar", "", EC_VA_END,
269 "foo", "bar", EC_VA_END);
276 static struct ec_test ec_node_once_test = {
278 .test = ec_node_once_testcase,
281 EC_TEST_REGISTER(ec_node_once_test);