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_parsed.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_parsed *state,
34 const struct ec_strvec *strvec)
36 struct ec_node_many *node = (struct ec_node_many *)gen_node;
37 struct ec_parsed *child_parsed;
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);
45 if (childvec == NULL) {
50 ret = ec_node_parse_child(node->child, state, childvec);
54 ec_strvec_free(childvec);
57 if (ret == EC_PARSED_NOMATCH)
60 /* it matches an empty strvec, no need to continue */
62 child_parsed = ec_parsed_get_last_child(state);
63 ec_parsed_unlink_child(state, child_parsed);
64 ec_parsed_free(child_parsed);
71 if (count < node->min) {
72 ec_parsed_free_children(state);
73 return EC_PARSED_NOMATCH;
79 ec_strvec_free(childvec);
84 __ec_node_many_complete(struct ec_node_many *node, unsigned int max,
86 const struct ec_strvec *strvec)
88 struct ec_parsed *parsed = ec_comp_get_state(comp);
89 struct ec_strvec *childvec = NULL;
93 /* first, try to complete with the child node */
94 ret = ec_node_complete_child(node->child, comp, strvec);
98 /* we're done, we reached the max number of nodes */
102 /* if there is a maximum, decrease it before recursion */
106 /* then, if the node matches the beginning of the strvec, try to
107 * complete the rest */
108 for (i = 0; i < ec_strvec_len(strvec); i++) {
109 childvec = ec_strvec_ndup(strvec, 0, i);
110 if (childvec == NULL)
113 ret = ec_node_parse_child(node->child, parsed, childvec);
117 ec_strvec_free(childvec);
120 if ((unsigned int)ret != i) {
121 if (ret != EC_PARSED_NOMATCH)
122 ec_parsed_del_last_child(parsed);
126 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
127 if (childvec == NULL) {
128 ec_parsed_del_last_child(parsed);
132 ret = __ec_node_many_complete(node, max, comp, childvec);
133 ec_parsed_del_last_child(parsed);
134 ec_strvec_free(childvec);
144 ec_strvec_free(childvec);
149 ec_node_many_complete(const struct ec_node *gen_node,
150 struct ec_comp *comp,
151 const struct ec_strvec *strvec)
153 struct ec_node_many *node = (struct ec_node_many *)gen_node;
155 return __ec_node_many_complete(node, node->max, comp,
159 static void ec_node_many_free_priv(struct ec_node *gen_node)
161 struct ec_node_many *node = (struct ec_node_many *)gen_node;
163 ec_node_free(node->child);
166 static struct ec_node_type ec_node_many_type = {
168 .parse = ec_node_many_parse,
169 .complete = ec_node_many_complete,
170 .size = sizeof(struct ec_node_many),
171 .free_priv = ec_node_many_free_priv,
174 EC_NODE_TYPE_REGISTER(ec_node_many_type);
176 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
177 unsigned int min, unsigned int max)
179 struct ec_node_many *node = NULL;
184 node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
197 /* LCOV_EXCL_START */
198 static int ec_node_many_testcase(void)
200 struct ec_node *node;
203 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 0, 0);
205 EC_LOG(EC_LOG_ERR, "cannot create node\n");
208 testres |= EC_TEST_CHECK_PARSE(node, 0);
209 testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
210 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
211 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
212 testres |= EC_TEST_CHECK_PARSE(node, 0);
215 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 0);
217 EC_LOG(EC_LOG_ERR, "cannot create node\n");
220 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
221 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
222 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
223 testres |= EC_TEST_CHECK_PARSE(node, -1);
226 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 2);
228 EC_LOG(EC_LOG_ERR, "cannot create node\n");
231 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
232 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
233 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
234 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
235 testres |= EC_TEST_CHECK_PARSE(node, -1);
238 /* test completion */
239 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 2, 4);
241 EC_LOG(EC_LOG_ERR, "cannot create node\n");
244 testres |= EC_TEST_CHECK_COMPLETE(node,
246 "foo", EC_NODE_ENDLIST);
247 testres |= EC_TEST_CHECK_COMPLETE(node,
248 "f", EC_NODE_ENDLIST,
249 "foo", EC_NODE_ENDLIST);
250 testres |= EC_TEST_CHECK_COMPLETE(node,
251 "foo", EC_NODE_ENDLIST,
252 "foo", EC_NODE_ENDLIST);
253 testres |= EC_TEST_CHECK_COMPLETE(node,
254 "foo", "", EC_NODE_ENDLIST,
255 "foo", EC_NODE_ENDLIST);
256 testres |= EC_TEST_CHECK_COMPLETE(node,
257 "foo", "foo", "", EC_NODE_ENDLIST,
258 "foo", EC_NODE_ENDLIST);
259 testres |= EC_TEST_CHECK_COMPLETE(node,
260 "foo", "foo", "foo", "", EC_NODE_ENDLIST,
261 "foo", EC_NODE_ENDLIST);
262 testres |= EC_TEST_CHECK_COMPLETE(node,
263 "foo", "foo", "foo", "foo", "", EC_NODE_ENDLIST,
271 static struct ec_test ec_node_many_test = {
273 .test = ec_node_many_testcase,
276 EC_TEST_REGISTER(ec_node_many_test);