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_option.h>
21 #include <ecoli_node_many.h>
23 EC_LOG_TYPE_REGISTER(node_many);
29 struct ec_node *child;
32 static int ec_node_many_parse(const struct ec_node *gen_node,
33 struct ec_parse *state,
34 const struct ec_strvec *strvec)
36 struct ec_node_many *node = (struct ec_node_many *)gen_node;
37 struct ec_parse *child_parse;
38 struct ec_strvec *childvec = NULL;
39 size_t off = 0, count;
42 for (count = 0; node->max == 0 || count < node->max; count++) {
43 childvec = ec_strvec_ndup(strvec, off,
44 ec_strvec_len(strvec) - off);
48 ret = ec_node_parse_child(node->child, state, childvec);
52 ec_strvec_free(childvec);
55 if (ret == EC_PARSE_NOMATCH)
58 /* it matches an empty strvec, no need to continue */
60 child_parse = ec_parse_get_last_child(state);
61 ec_parse_unlink_child(state, child_parse);
62 ec_parse_free(child_parse);
69 if (count < node->min) {
70 ec_parse_free_children(state);
71 return EC_PARSE_NOMATCH;
77 ec_strvec_free(childvec);
82 __ec_node_many_complete(struct ec_node_many *node, unsigned int max,
84 const struct ec_strvec *strvec)
86 struct ec_parse *parse = ec_comp_get_state(comp);
87 struct ec_strvec *childvec = NULL;
91 /* first, try to complete with the child node */
92 ret = ec_node_complete_child(node->child, comp, strvec);
96 /* we're done, we reached the max number of nodes */
100 /* if there is a maximum, decrease it before recursion */
104 /* then, if the node matches the beginning of the strvec, try to
105 * complete the rest */
106 for (i = 0; i < ec_strvec_len(strvec); i++) {
107 childvec = ec_strvec_ndup(strvec, 0, i);
108 if (childvec == NULL)
111 ret = ec_node_parse_child(node->child, parse, childvec);
115 ec_strvec_free(childvec);
118 if ((unsigned int)ret != i) {
119 if (ret != EC_PARSE_NOMATCH)
120 ec_parse_del_last_child(parse);
124 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
125 if (childvec == NULL) {
126 ec_parse_del_last_child(parse);
130 ret = __ec_node_many_complete(node, max, comp, childvec);
131 ec_parse_del_last_child(parse);
132 ec_strvec_free(childvec);
142 ec_strvec_free(childvec);
147 ec_node_many_complete(const struct ec_node *gen_node,
148 struct ec_comp *comp,
149 const struct ec_strvec *strvec)
151 struct ec_node_many *node = (struct ec_node_many *)gen_node;
153 return __ec_node_many_complete(node, node->max, comp,
158 ec_node_many_get_children_count(const struct ec_node *gen_node)
160 struct ec_node_many *node = (struct ec_node_many *)gen_node;
167 static struct ec_node *
168 ec_node_many_get_child(const struct ec_node *gen_node, size_t i)
170 struct ec_node_many *node = (struct ec_node_many *)gen_node;
178 static struct ec_node_type ec_node_many_type = {
180 .parse = ec_node_many_parse,
181 .complete = ec_node_many_complete,
182 .size = sizeof(struct ec_node_many),
183 .get_children_count = ec_node_many_get_children_count,
184 .get_child = ec_node_many_get_child,
187 EC_NODE_TYPE_REGISTER(ec_node_many_type);
189 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
190 unsigned int min, unsigned int max)
192 struct ec_node_many *node = NULL;
197 node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
210 /* LCOV_EXCL_START */
211 static int ec_node_many_testcase(void)
213 struct ec_node *node;
216 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 0, 0);
218 EC_LOG(EC_LOG_ERR, "cannot create node\n");
221 testres |= EC_TEST_CHECK_PARSE(node, 0);
222 testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
223 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
224 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
225 testres |= EC_TEST_CHECK_PARSE(node, 0);
228 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 0);
230 EC_LOG(EC_LOG_ERR, "cannot create node\n");
233 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
234 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
235 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
236 testres |= EC_TEST_CHECK_PARSE(node, -1);
239 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 2);
241 EC_LOG(EC_LOG_ERR, "cannot create node\n");
244 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
245 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
246 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
247 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
248 testres |= EC_TEST_CHECK_PARSE(node, -1);
251 /* test completion */
252 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 2, 4);
254 EC_LOG(EC_LOG_ERR, "cannot create node\n");
257 testres |= EC_TEST_CHECK_COMPLETE(node,
259 "foo", EC_NODE_ENDLIST);
260 testres |= EC_TEST_CHECK_COMPLETE(node,
261 "f", EC_NODE_ENDLIST,
262 "foo", EC_NODE_ENDLIST);
263 testres |= EC_TEST_CHECK_COMPLETE(node,
264 "foo", EC_NODE_ENDLIST,
265 "foo", EC_NODE_ENDLIST);
266 testres |= EC_TEST_CHECK_COMPLETE(node,
267 "foo", "", EC_NODE_ENDLIST,
268 "foo", EC_NODE_ENDLIST);
269 testres |= EC_TEST_CHECK_COMPLETE(node,
270 "foo", "foo", "", EC_NODE_ENDLIST,
271 "foo", EC_NODE_ENDLIST);
272 testres |= EC_TEST_CHECK_COMPLETE(node,
273 "foo", "foo", "foo", "", EC_NODE_ENDLIST,
274 "foo", EC_NODE_ENDLIST);
275 testres |= EC_TEST_CHECK_COMPLETE(node,
276 "foo", "foo", "foo", "foo", "", EC_NODE_ENDLIST,
284 static struct ec_test ec_node_many_test = {
286 .test = ec_node_many_testcase,
289 EC_TEST_REGISTER(ec_node_many_test);