2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <ecoli_malloc.h>
36 #include <ecoli_log.h>
37 #include <ecoli_test.h>
38 #include <ecoli_strvec.h>
39 #include <ecoli_node.h>
40 #include <ecoli_parsed.h>
41 #include <ecoli_completed.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_node_option.h>
44 #include <ecoli_node_many.h>
46 EC_LOG_TYPE_REGISTER(node_many);
52 struct ec_node *child;
55 static int ec_node_many_parse(const struct ec_node *gen_node,
56 struct ec_parsed *state,
57 const struct ec_strvec *strvec)
59 struct ec_node_many *node = (struct ec_node_many *)gen_node;
60 struct ec_parsed *child_parsed;
61 struct ec_strvec *childvec = NULL;
62 size_t off = 0, count;
65 for (count = 0; node->max == 0 || count < node->max; count++) {
66 childvec = ec_strvec_ndup(strvec, off,
67 ec_strvec_len(strvec) - off);
68 if (childvec == NULL) {
73 ret = ec_node_parse_child(node->child, state, childvec);
77 ec_strvec_free(childvec);
80 if (ret == EC_PARSED_NOMATCH)
83 /* it matches an empty strvec, no need to continue */
85 child_parsed = ec_parsed_get_last_child(state);
86 ec_parsed_del_child(state, child_parsed);
87 ec_parsed_free(child_parsed);
94 if (count < node->min) {
95 ec_parsed_free_children(state);
96 return EC_PARSED_NOMATCH;
102 ec_strvec_free(childvec);
107 __ec_node_many_complete(struct ec_node_many *node, unsigned int max,
108 struct ec_completed *completed,
109 const struct ec_strvec *strvec)
111 struct ec_parsed *parsed = ec_completed_get_state(completed);
112 struct ec_strvec *childvec = NULL;
116 /* first, try to complete with the child node */
117 ret = ec_node_complete_child(node->child, completed, strvec);
121 /* we're done, we reached the max number of nodes */
125 /* if there is a maximum, decrease it before recursion */
129 /* then, if the node matches the beginning of the strvec, try to
130 * complete the rest */
131 for (i = 0; i < ec_strvec_len(strvec); i++) {
132 childvec = ec_strvec_ndup(strvec, 0, i);
133 if (childvec == NULL)
136 ret = ec_node_parse_child(node->child, parsed, childvec);
140 ec_strvec_free(childvec);
143 if ((unsigned int)ret != i) {
144 if (ret != EC_PARSED_NOMATCH)
145 ec_parsed_del_last_child(parsed);
149 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
150 if (childvec == NULL) {
151 ec_parsed_del_last_child(parsed);
155 ret = __ec_node_many_complete(node, max, completed, childvec);
156 ec_parsed_del_last_child(parsed);
157 ec_strvec_free(childvec);
167 ec_strvec_free(childvec);
172 ec_node_many_complete(const struct ec_node *gen_node,
173 struct ec_completed *completed,
174 const struct ec_strvec *strvec)
176 struct ec_node_many *node = (struct ec_node_many *)gen_node;
178 return __ec_node_many_complete(node, node->max, completed,
182 static void ec_node_many_free_priv(struct ec_node *gen_node)
184 struct ec_node_many *node = (struct ec_node_many *)gen_node;
186 ec_node_free(node->child);
189 static struct ec_node_type ec_node_many_type = {
191 .parse = ec_node_many_parse,
192 .complete = ec_node_many_complete,
193 .size = sizeof(struct ec_node_many),
194 .free_priv = ec_node_many_free_priv,
197 EC_NODE_TYPE_REGISTER(ec_node_many_type);
199 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
200 unsigned int min, unsigned int max)
202 struct ec_node_many *node = NULL;
207 node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
220 /* LCOV_EXCL_START */
221 static int ec_node_many_testcase(void)
223 struct ec_node *node;
226 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 0, 0);
228 EC_LOG(EC_LOG_ERR, "cannot create node\n");
231 testres |= EC_TEST_CHECK_PARSE(node, 0);
232 testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
233 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
234 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
235 testres |= EC_TEST_CHECK_PARSE(node, 0);
238 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 0);
240 EC_LOG(EC_LOG_ERR, "cannot create node\n");
243 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
244 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
245 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
246 testres |= EC_TEST_CHECK_PARSE(node, -1);
249 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 2);
251 EC_LOG(EC_LOG_ERR, "cannot create node\n");
254 testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
255 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
256 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
257 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
258 testres |= EC_TEST_CHECK_PARSE(node, -1);
261 /* test completion */
262 node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 2, 4);
264 EC_LOG(EC_LOG_ERR, "cannot create node\n");
267 testres |= EC_TEST_CHECK_COMPLETE(node,
269 "foo", EC_NODE_ENDLIST);
270 testres |= EC_TEST_CHECK_COMPLETE(node,
271 "f", EC_NODE_ENDLIST,
272 "foo", EC_NODE_ENDLIST);
273 testres |= EC_TEST_CHECK_COMPLETE(node,
274 "foo", EC_NODE_ENDLIST,
275 "foo", EC_NODE_ENDLIST);
276 testres |= EC_TEST_CHECK_COMPLETE(node,
277 "foo", "", EC_NODE_ENDLIST,
278 "foo", EC_NODE_ENDLIST);
279 testres |= EC_TEST_CHECK_COMPLETE(node,
280 "foo", "foo", "", EC_NODE_ENDLIST,
281 "foo", EC_NODE_ENDLIST);
282 testres |= EC_TEST_CHECK_COMPLETE(node,
283 "foo", "foo", "foo", "", EC_NODE_ENDLIST,
284 "foo", EC_NODE_ENDLIST);
285 testres |= EC_TEST_CHECK_COMPLETE(node,
286 "foo", "foo", "foo", "foo", "", EC_NODE_ENDLIST,
294 static struct ec_test ec_node_many_test = {
296 .test = ec_node_many_testcase,
299 EC_TEST_REGISTER(ec_node_many_test);