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>
50 struct ec_node *child;
53 static int ec_node_many_parse(const struct ec_node *gen_node,
54 struct ec_parsed *state,
55 const struct ec_strvec *strvec)
57 struct ec_node_many *node = (struct ec_node_many *)gen_node;
58 struct ec_parsed *child_parsed;
59 struct ec_strvec *childvec = NULL;
60 size_t off = 0, count;
63 for (count = 0; node->max == 0 || count < node->max; count++) {
64 childvec = ec_strvec_ndup(strvec, off,
65 ec_strvec_len(strvec) - off);
66 if (childvec == NULL) {
71 ret = ec_node_parse_child(node->child, state, childvec);
72 ec_strvec_free(childvec);
75 if (ret == EC_PARSED_NOMATCH)
80 /* it matches an empty strvec, no need to continue */
82 child_parsed = ec_parsed_get_last_child(state);
83 ec_parsed_del_child(state, child_parsed);
84 ec_parsed_free(child_parsed);
91 if (count < node->min) {
92 ec_parsed_free_children(state);
93 return EC_PARSED_NOMATCH;
99 ec_strvec_free(childvec);
103 static struct ec_completed *
104 __ec_node_many_complete(struct ec_node_many *node, unsigned int max,
105 struct ec_parsed *state, const struct ec_strvec *strvec)
107 struct ec_completed *completed, *child_completed;
108 struct ec_strvec *childvec = NULL;
112 completed = ec_completed();
113 if (completed == NULL)
116 /* first, try to complete with the child node */
117 child_completed = ec_node_complete_child(node->child, state, strvec);
118 if (child_completed == NULL)
120 ec_completed_merge(completed, child_completed);
121 child_completed = NULL;
123 /* we're done, we reached the max number of nodes */
127 /* if there is a maximum, decrease it before recursion */
131 /* then, if the node matches the beginning of the strvec, try to
132 * complete the rest */
133 for (i = 0; i < ec_strvec_len(strvec); i++) {
134 childvec = ec_strvec_ndup(strvec, 0, i);
135 if (childvec == NULL)
138 ret = ec_node_parse_child(node->child, state, childvec);
139 if (ret < 0 && ret != EC_PARSED_NOMATCH)
142 ec_strvec_free(childvec);
145 if ((unsigned int)ret != i) {
146 if (ret != EC_PARSED_NOMATCH)
147 ec_parsed_del_last_child(state);
151 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
152 if (childvec == NULL) {
153 ec_parsed_del_last_child(state);
157 child_completed = __ec_node_many_complete(node, max,
159 ec_parsed_del_last_child(state);
160 ec_strvec_free(childvec);
163 if (child_completed == NULL)
166 ec_completed_merge(completed, child_completed);
167 child_completed = NULL;
173 ec_strvec_free(childvec);
174 ec_completed_free(child_completed);
175 ec_completed_free(completed);
179 static struct ec_completed *
180 ec_node_many_complete(const struct ec_node *gen_node,
181 struct ec_parsed *state,
182 const struct ec_strvec *strvec)
184 struct ec_node_many *node = (struct ec_node_many *)gen_node;
186 return __ec_node_many_complete(node, node->max, state, strvec);
189 static void ec_node_many_free_priv(struct ec_node *gen_node)
191 struct ec_node_many *node = (struct ec_node_many *)gen_node;
193 ec_node_free(node->child);
196 static struct ec_node_type ec_node_many_type = {
198 .parse = ec_node_many_parse,
199 .complete = ec_node_many_complete,
200 .size = sizeof(struct ec_node_many),
201 .free_priv = ec_node_many_free_priv,
204 EC_NODE_TYPE_REGISTER(ec_node_many_type);
206 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
207 unsigned int min, unsigned int max)
209 struct ec_node_many *node = NULL;
214 node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
227 /* LCOV_EXCL_START */
228 static int ec_node_many_testcase(void)
230 struct ec_node *node;
233 node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 0, 0);
235 ec_log(EC_LOG_ERR, "cannot create node\n");
238 ret |= EC_TEST_CHECK_PARSE(node, 0);
239 ret |= EC_TEST_CHECK_PARSE(node, 0, "bar");
240 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
241 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
242 ret |= EC_TEST_CHECK_PARSE(node, 0);
245 node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 0);
247 ec_log(EC_LOG_ERR, "cannot create node\n");
250 ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
251 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
252 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
253 ret |= EC_TEST_CHECK_PARSE(node, -1);
256 node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 2);
258 ec_log(EC_LOG_ERR, "cannot create node\n");
261 ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
262 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
263 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
264 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
265 ret |= EC_TEST_CHECK_PARSE(node, -1);
268 /* test completion */
269 node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 2, 4);
271 ec_log(EC_LOG_ERR, "cannot create node\n");
274 ret |= EC_TEST_CHECK_COMPLETE(node,
276 "foo", EC_NODE_ENDLIST,
278 ret |= EC_TEST_CHECK_COMPLETE(node,
279 "f", EC_NODE_ENDLIST,
280 "oo", EC_NODE_ENDLIST,
282 ret |= EC_TEST_CHECK_COMPLETE(node,
283 "foo", EC_NODE_ENDLIST,
286 ret |= EC_TEST_CHECK_COMPLETE(node,
287 "foo", "", EC_NODE_ENDLIST,
288 "foo", EC_NODE_ENDLIST,
290 ret |= EC_TEST_CHECK_COMPLETE(node,
291 "foo", "foo", "", EC_NODE_ENDLIST,
292 "foo", EC_NODE_ENDLIST,
294 ret |= EC_TEST_CHECK_COMPLETE(node,
295 "foo", "foo", "foo", "", EC_NODE_ENDLIST,
296 "foo", EC_NODE_ENDLIST,
298 ret |= EC_TEST_CHECK_COMPLETE(node,
299 "foo", "foo", "foo", "foo", "", EC_NODE_ENDLIST,
308 static struct ec_test ec_node_many_test = {
310 .test = ec_node_many_testcase,
313 EC_TEST_REGISTER(ec_node_many_test);