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_seq.h>
48 struct ec_node **table;
52 static struct ec_parsed *ec_node_seq_parse(const struct ec_node *gen_node,
53 const struct ec_strvec *strvec)
55 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
56 struct ec_parsed *parsed, *child_parsed;
57 struct ec_strvec *match_strvec;
58 struct ec_strvec *childvec = NULL;
66 for (i = 0; i < node->len; i++) {
67 childvec = ec_strvec_ndup(strvec, len,
68 ec_strvec_len(strvec) - len);
72 child_parsed = ec_node_parse_strvec(node->table[i], childvec);
73 if (child_parsed == NULL)
76 ec_strvec_free(childvec);
79 if (!ec_parsed_matches(child_parsed)) {
80 ec_parsed_free(child_parsed);
81 // XXX ec_parsed_free_children needed? see subset.c
82 ec_parsed_free_children(parsed);
86 ec_parsed_add_child(parsed, child_parsed);
87 len += ec_parsed_len(child_parsed);
90 match_strvec = ec_strvec_ndup(strvec, 0, len);
91 if (match_strvec == NULL)
94 ec_parsed_set_match(parsed, gen_node, match_strvec);
99 ec_strvec_free(childvec);
100 ec_parsed_free(parsed);
104 static struct ec_completed *ec_node_seq_complete(const struct ec_node *gen_node,
105 const struct ec_strvec *strvec)
107 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
108 struct ec_completed *completed, *child_completed;
109 struct ec_strvec *childvec = NULL;
110 struct ec_parsed *parsed;
114 completed = ec_completed();
115 if (completed == NULL)
121 for (i = 0; i < node->len && len < ec_strvec_len(strvec); i++) {
122 childvec = ec_strvec_ndup(strvec, len,
123 ec_strvec_len(strvec) - len);
124 if (childvec == NULL)
127 child_completed = ec_node_complete_strvec(node->table[i],
129 if (child_completed == NULL)
132 ec_completed_merge(completed, child_completed);
134 parsed = ec_node_parse_strvec(node->table[i], childvec);
138 ec_strvec_free(childvec);
141 if (!ec_parsed_matches(parsed)) {
142 ec_parsed_free(parsed);
146 len += ec_strvec_len(parsed->strvec);
147 ec_parsed_free(parsed);
153 ec_strvec_free(childvec);
154 ec_completed_free(completed);
158 static void ec_node_seq_free_priv(struct ec_node *gen_node)
160 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
163 for (i = 0; i < node->len; i++)
164 ec_node_free(node->table[i]);
165 ec_free(node->table);
168 static struct ec_node_type ec_node_seq_type = {
170 .parse = ec_node_seq_parse,
171 .complete = ec_node_seq_complete,
172 .size = sizeof(struct ec_node_seq),
173 .free_priv = ec_node_seq_free_priv,
176 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
178 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
180 struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
181 struct ec_node **table;
183 // XXX check node type
185 assert(node != NULL);
190 gen_node->flags &= ~EC_NODE_F_BUILT;
192 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
199 table[node->len] = child;
202 child->parent = gen_node;
203 TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
208 struct ec_node *__ec_node_seq(const char *id, ...)
210 struct ec_node *gen_node = NULL;
211 struct ec_node_seq *node = NULL;
212 struct ec_node *child;
218 gen_node = __ec_node(&ec_node_seq_type, id);
219 node = (struct ec_node_seq *)gen_node;
223 for (child = va_arg(ap, struct ec_node *);
224 child != EC_NODE_ENDLIST;
225 child = va_arg(ap, struct ec_node *)) {
227 /* on error, don't quit the loop to avoid leaks */
228 if (fail == 1 || child == NULL ||
229 ec_node_seq_add(&node->gen, child) < 0) {
242 ec_node_free(gen_node); /* will also free children */
247 static int ec_node_seq_testcase(void)
249 struct ec_node *node;
252 node = EC_NODE_SEQ(NULL,
253 ec_node_str(NULL, "foo"),
254 ec_node_str(NULL, "bar")
257 ec_log(EC_LOG_ERR, "cannot create node\n");
260 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
261 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
262 ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
263 ret |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
264 ret |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
265 ret |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
266 ret |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
269 /* test completion */
270 node = EC_NODE_SEQ(NULL,
271 ec_node_str(NULL, "foo"),
272 ec_node_option(NULL, ec_node_str(NULL, "toto")),
273 ec_node_str(NULL, "bar")
276 ec_log(EC_LOG_ERR, "cannot create node\n");
279 ret |= EC_TEST_CHECK_COMPLETE(node,
281 "foo", EC_NODE_ENDLIST,
283 ret |= EC_TEST_CHECK_COMPLETE(node,
284 "f", EC_NODE_ENDLIST,
285 "oo", EC_NODE_ENDLIST,
287 ret |= EC_TEST_CHECK_COMPLETE(node,
288 "foo", EC_NODE_ENDLIST,
291 ret |= EC_TEST_CHECK_COMPLETE(node,
292 "foo", "", EC_NODE_ENDLIST,
293 "bar", "toto", EC_NODE_ENDLIST,
295 ret |= EC_TEST_CHECK_COMPLETE(node,
296 "foo", "t", EC_NODE_ENDLIST,
297 "oto", EC_NODE_ENDLIST,
299 ret |= EC_TEST_CHECK_COMPLETE(node,
300 "foo", "b", EC_NODE_ENDLIST,
301 "ar", EC_NODE_ENDLIST,
303 ret |= EC_TEST_CHECK_COMPLETE(node,
304 "foo", "bar", EC_NODE_ENDLIST,
307 ret |= EC_TEST_CHECK_COMPLETE(node,
308 "x", EC_NODE_ENDLIST,
311 ret |= EC_TEST_CHECK_COMPLETE(node,
312 "foobarx", EC_NODE_ENDLIST,
320 static struct ec_test ec_node_seq_test = {
322 .test = ec_node_seq_testcase,
325 EC_TEST_REGISTER(ec_node_seq_test);