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.
34 #include <ecoli_malloc.h>
35 #include <ecoli_log.h>
36 #include <ecoli_test.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41 #include <ecoli_node_str.h>
42 #include <ecoli_node_option.h>
43 #include <ecoli_node_many.h>
49 struct ec_node *child;
52 static struct ec_parsed *ec_node_many_parse(const struct ec_node *gen_node,
53 const struct ec_strvec *strvec)
55 struct ec_node_many *node = (struct ec_node_many *)gen_node;
56 struct ec_parsed *parsed, *child_parsed;
57 struct ec_strvec *match_strvec;
58 struct ec_strvec *childvec = NULL;
59 size_t off = 0, len, 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);
71 child_parsed = ec_node_parse_strvec(node->child, childvec);
72 if (child_parsed == NULL)
75 ec_strvec_free(childvec);
78 if (!ec_parsed_matches(child_parsed)) {
79 ec_parsed_free(child_parsed);
83 ec_parsed_add_child(parsed, child_parsed);
85 /* it matches an empty strvec, no need to continue */
86 len = ec_parsed_len(child_parsed);
88 ec_parsed_free(child_parsed);
95 if (count < node->min) {
96 ec_parsed_free_children(parsed);
100 match_strvec = ec_strvec_ndup(strvec, 0, off);
101 if (match_strvec == NULL)
104 ec_parsed_set_match(parsed, gen_node, match_strvec);
109 ec_strvec_free(childvec);
110 ec_parsed_free(parsed);
114 #if 0 //XXX missing node_many_complete
115 static struct ec_completed *ec_node_many_complete(const struct ec_node *gen_node,
116 const struct ec_strvec *strvec)
118 struct ec_node_many *node = (struct ec_node_many *)gen_node;
119 struct ec_completed *completed, *child_completed;
120 struct ec_strvec *childvec;
121 struct ec_parsed *parsed;
125 completed = ec_completed();
126 if (completed == NULL)
132 for (i = 0; i < node->len; i++) {
133 childvec = ec_strvec_ndup(strvec, len,
134 ec_strvec_len(strvec) - len);
135 if (childvec == NULL)
136 goto fail; // XXX fail ?
138 child_completed = ec_node_complete_strvec(node->table[i],
140 if (child_completed == NULL)
143 ec_completed_merge(completed, child_completed);
145 parsed = ec_node_parse_strvec(node->table[i], childvec);
149 ec_strvec_free(childvec);
152 if (!ec_parsed_matches(parsed)) {
153 ec_parsed_free(parsed);
157 len += ec_strvec_len(parsed->strvec);
158 ec_parsed_free(parsed);
164 ec_strvec_free(childvec);
165 ec_completed_free(completed);
170 static void ec_node_many_free_priv(struct ec_node *gen_node)
172 struct ec_node_many *node = (struct ec_node_many *)gen_node;
174 ec_node_free(node->child);
177 static struct ec_node_type ec_node_many_type = {
179 .parse = ec_node_many_parse,
180 .complete = ec_node_default_complete,
181 //XXX .complete = ec_node_many_complete,
182 .size = sizeof(struct ec_node_many),
183 .free_priv = ec_node_many_free_priv,
186 EC_NODE_TYPE_REGISTER(ec_node_many_type);
188 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
189 unsigned int min, unsigned int max)
191 struct ec_node_many *node = NULL;
196 node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
209 static int ec_node_many_testcase(void)
211 struct ec_node *node;
214 node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 0, 0);
216 ec_log(EC_LOG_ERR, "cannot create node\n");
219 ret |= EC_TEST_CHECK_PARSE(node, 0, "bar");
220 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
221 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
222 ret |= EC_TEST_CHECK_PARSE(node, 0);
225 node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 0);
227 ec_log(EC_LOG_ERR, "cannot create node\n");
230 ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
231 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
232 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
233 ret |= EC_TEST_CHECK_PARSE(node, -1);
236 node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 2);
238 ec_log(EC_LOG_ERR, "cannot create node\n");
241 ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
242 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
243 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
244 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
245 ret |= EC_TEST_CHECK_PARSE(node, -1);
248 /* test completion */
254 static struct ec_test ec_node_many_test = {
256 .test = ec_node_many_testcase,
259 EC_TEST_REGISTER(ec_node_many_test);