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_strvec.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41 #include <ecoli_node_or.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_test.h>
47 struct ec_node **table;
52 ec_node_or_parse(const struct ec_node *gen_node,
53 struct ec_parsed *state,
54 const struct ec_strvec *strvec)
56 struct ec_node_or *node = (struct ec_node_or *)gen_node;
60 for (i = 0; i < node->len; i++) {
61 ret = ec_node_parse_child(node->table[i], state, strvec);
62 if (ret == EC_PARSED_NOMATCH)
67 return EC_PARSED_NOMATCH;
71 ec_node_or_complete(const struct ec_node *gen_node,
72 struct ec_completed *completed,
73 struct ec_parsed *parsed,
74 const struct ec_strvec *strvec)
76 struct ec_node_or *node = (struct ec_node_or *)gen_node;
80 for (n = 0; n < node->len; n++) {
81 ret = ec_node_complete_child(node->table[n],
82 completed, parsed, strvec);
90 static size_t ec_node_or_get_max_parse_len(const struct ec_node *gen_node)
92 struct ec_node_or *node = (struct ec_node_or *)gen_node;
93 size_t i, ret = 0, len;
95 for (i = 0; i < node->len; i++) {
96 len = ec_node_get_max_parse_len(node->table[i]);
104 static void ec_node_or_free_priv(struct ec_node *gen_node)
106 struct ec_node_or *node = (struct ec_node_or *)gen_node;
109 for (i = 0; i < node->len; i++)
110 ec_node_free(node->table[i]);
111 ec_free(node->table);
114 int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
116 struct ec_node_or *node = (struct ec_node_or *)gen_node;
117 struct ec_node **table;
119 assert(node != NULL);
124 gen_node->flags &= ~EC_NODE_F_BUILT;
126 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
133 table[node->len] = child;
136 child->parent = gen_node;
137 TAILQ_INSERT_TAIL(&gen_node->children, child, next);
142 static struct ec_node_type ec_node_or_type = {
144 .parse = ec_node_or_parse,
145 .complete = ec_node_or_complete,
146 .get_max_parse_len = ec_node_or_get_max_parse_len,
147 .size = sizeof(struct ec_node_or),
148 .free_priv = ec_node_or_free_priv,
151 EC_NODE_TYPE_REGISTER(ec_node_or_type);
153 struct ec_node *__ec_node_or(const char *id, ...)
155 struct ec_node *gen_node = NULL;
156 struct ec_node_or *node = NULL;
157 struct ec_node *child;
163 gen_node = __ec_node(&ec_node_or_type, id);
164 node = (struct ec_node_or *)gen_node;
168 for (child = va_arg(ap, struct ec_node *);
169 child != EC_NODE_ENDLIST;
170 child = va_arg(ap, struct ec_node *)) {
172 /* on error, don't quit the loop to avoid leaks */
173 if (fail == 1 || child == NULL ||
174 ec_node_or_add(gen_node, child) < 0) {
187 ec_node_free(gen_node); /* will also free children */
192 /* LCOV_EXCL_START */
193 static int ec_node_or_testcase(void)
195 struct ec_node *node;
198 node = EC_NODE_OR(NULL,
199 ec_node_str(NULL, "foo"),
200 ec_node_str(NULL, "bar")
203 ec_log(EC_LOG_ERR, "cannot create node\n");
206 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo");
207 ret |= EC_TEST_CHECK_PARSE(node, 1, "bar");
208 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
209 ret |= EC_TEST_CHECK_PARSE(node, -1, " ");
210 ret |= EC_TEST_CHECK_PARSE(node, -1, "foox");
211 ret |= EC_TEST_CHECK_PARSE(node, -1, "toto");
212 ret |= EC_TEST_CHECK_PARSE(node, -1, "");
215 /* test completion */
216 node = EC_NODE_OR(NULL,
217 ec_node_str(NULL, "foo"),
218 ec_node_str(NULL, "bar"),
219 ec_node_str(NULL, "bar2"),
220 ec_node_str(NULL, "toto"),
221 ec_node_str(NULL, "titi")
224 ec_log(EC_LOG_ERR, "cannot create node\n");
227 ret |= EC_TEST_CHECK_COMPLETE(node,
229 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST,
231 ret |= EC_TEST_CHECK_COMPLETE(node,
232 "f", EC_NODE_ENDLIST,
233 "oo", EC_NODE_ENDLIST,
235 ret |= EC_TEST_CHECK_COMPLETE(node,
236 "b", EC_NODE_ENDLIST,
237 "ar", "ar2", EC_NODE_ENDLIST,
239 ret |= EC_TEST_CHECK_COMPLETE(node,
240 "bar", EC_NODE_ENDLIST,
241 "", "2", EC_NODE_ENDLIST,
243 ret |= EC_TEST_CHECK_COMPLETE(node,
244 "t", EC_NODE_ENDLIST,
245 "oto", "iti", EC_NODE_ENDLIST,
247 ret |= EC_TEST_CHECK_COMPLETE(node,
248 "to", EC_NODE_ENDLIST,
249 "to", EC_NODE_ENDLIST,
251 ret |= EC_TEST_CHECK_COMPLETE(node,
252 "x", EC_NODE_ENDLIST,
261 static struct ec_test ec_node_or_test = {
263 .test = ec_node_or_testcase,
266 EC_TEST_REGISTER(ec_node_or_test);