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_node_once.h>
24 EC_LOG_TYPE_REGISTER(node_once);
28 struct ec_node *child;
32 count_node(struct ec_parse *parse, const struct ec_node *node)
34 struct ec_parse *child;
35 unsigned int count = 0;
40 if (ec_parse_get_node(parse) == node)
43 EC_PARSE_FOREACH_CHILD(child, parse)
44 count += count_node(child, node);
50 ec_node_once_parse(const struct ec_node *gen_node,
51 struct ec_parse *state,
52 const struct ec_strvec *strvec)
54 struct ec_node_once *node = (struct ec_node_once *)gen_node;
57 /* count the number of occurences of the node: if already parsed,
60 count = count_node(ec_parse_get_root(state), node->child);
62 return EC_PARSE_NOMATCH;
64 return ec_node_parse_child(node->child, state, strvec);
68 ec_node_once_complete(const struct ec_node *gen_node,
70 const struct ec_strvec *strvec)
72 struct ec_node_once *node = (struct ec_node_once *)gen_node;
73 struct ec_parse *parse = ec_comp_get_state(comp);
77 /* count the number of occurences of the node: if already parsed,
80 count = count_node(ec_parse_get_root(parse), node->child);
84 ret = ec_node_complete_child(node->child, comp, strvec);
91 static void ec_node_once_free_priv(struct ec_node *gen_node)
93 struct ec_node_once *node = (struct ec_node_once *)gen_node;
95 ec_node_free(node->child);
98 static struct ec_node_type ec_node_once_type = {
100 .parse = ec_node_once_parse,
101 .complete = ec_node_once_complete,
102 .size = sizeof(struct ec_node_once),
103 .free_priv = ec_node_once_free_priv,
106 EC_NODE_TYPE_REGISTER(ec_node_once_type);
108 int ec_node_once_set(struct ec_node *gen_node, struct ec_node *child)
110 struct ec_node_once *node = (struct ec_node_once *)gen_node;
112 if (gen_node == NULL || child == NULL) {
117 if (ec_node_check_type(gen_node, &ec_node_once_type) < 0)
120 if (ec_node_add_child(gen_node, child) < 0)
132 struct ec_node *ec_node_once(const char *id, struct ec_node *child)
134 struct ec_node *gen_node = NULL;
139 gen_node = __ec_node(&ec_node_once_type, id);
140 if (gen_node == NULL)
143 ec_node_once_set(gen_node, child);
153 /* LCOV_EXCL_START */
154 static int ec_node_once_testcase(void)
156 struct ec_node *node;
159 node = ec_node_many(EC_NO_ID,
161 ec_node_once(EC_NO_ID, ec_node_str(EC_NO_ID, "foo")),
162 ec_node_str(EC_NO_ID, "bar")
166 EC_LOG(EC_LOG_ERR, "cannot create node\n");
169 testres |= EC_TEST_CHECK_PARSE(node, 0);
170 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
171 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
172 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
173 testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "bar");
174 testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "bar");
175 testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo", "foo");
176 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
177 testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
179 testres |= EC_TEST_CHECK_COMPLETE(node,
181 "foo", "bar", EC_NODE_ENDLIST);
182 testres |= EC_TEST_CHECK_COMPLETE(node,
183 "f", EC_NODE_ENDLIST,
184 "foo", EC_NODE_ENDLIST);
185 testres |= EC_TEST_CHECK_COMPLETE(node,
186 "b", EC_NODE_ENDLIST,
187 "bar", EC_NODE_ENDLIST);
188 testres |= EC_TEST_CHECK_COMPLETE(node,
189 "foo", "", EC_NODE_ENDLIST,
190 "bar", EC_NODE_ENDLIST);
191 testres |= EC_TEST_CHECK_COMPLETE(node,
192 "bar", "", EC_NODE_ENDLIST,
193 "foo", "bar", EC_NODE_ENDLIST);
200 static struct ec_test ec_node_once_test = {
202 .test = ec_node_once_testcase,
205 EC_TEST_REGISTER(ec_node_once_test);