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>
45 EC_LOG_TYPE_REGISTER(node_or);
49 struct ec_node **table;
54 ec_node_or_parse(const struct ec_node *gen_node,
55 struct ec_parsed *state,
56 const struct ec_strvec *strvec)
58 struct ec_node_or *node = (struct ec_node_or *)gen_node;
62 for (i = 0; i < node->len; i++) {
63 ret = ec_node_parse_child(node->table[i], state, strvec);
64 if (ret == EC_PARSED_NOMATCH)
69 return EC_PARSED_NOMATCH;
73 ec_node_or_complete(const struct ec_node *gen_node,
74 struct ec_completed *completed,
75 const struct ec_strvec *strvec)
77 struct ec_node_or *node = (struct ec_node_or *)gen_node;
81 for (n = 0; n < node->len; n++) {
82 ret = ec_node_complete_child(node->table[n],
91 static void ec_node_or_free_priv(struct ec_node *gen_node)
93 struct ec_node_or *node = (struct ec_node_or *)gen_node;
96 for (i = 0; i < node->len; i++)
97 ec_node_free(node->table[i]);
101 static struct ec_node_type ec_node_or_type = {
103 .parse = ec_node_or_parse,
104 .complete = ec_node_or_complete,
105 .size = sizeof(struct ec_node_or),
106 .free_priv = ec_node_or_free_priv,
109 EC_NODE_TYPE_REGISTER(ec_node_or_type);
111 int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
113 struct ec_node_or *node = (struct ec_node_or *)gen_node;
114 struct ec_node **table;
116 assert(node != NULL);
118 assert(node != NULL);
125 if (ec_node_check_type(gen_node, &ec_node_or_type) < 0)
128 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
134 if (ec_node_add_child(gen_node, child) < 0)
137 table[node->len] = child;
147 struct ec_node *__ec_node_or(const char *id, ...)
149 struct ec_node *gen_node = NULL;
150 struct ec_node_or *node = NULL;
151 struct ec_node *child;
157 gen_node = __ec_node(&ec_node_or_type, id);
158 node = (struct ec_node_or *)gen_node;
162 for (child = va_arg(ap, struct ec_node *);
163 child != EC_NODE_ENDLIST;
164 child = va_arg(ap, struct ec_node *)) {
166 /* on error, don't quit the loop to avoid leaks */
167 if (fail == 1 || child == NULL ||
168 ec_node_or_add(gen_node, child) < 0) {
181 ec_node_free(gen_node); /* will also free children */
186 /* LCOV_EXCL_START */
187 static int ec_node_or_testcase(void)
189 struct ec_node *node;
192 node = EC_NODE_OR(EC_NO_ID,
193 ec_node_str(EC_NO_ID, "foo"),
194 ec_node_str(EC_NO_ID, "bar")
197 EC_LOG(EC_LOG_ERR, "cannot create node\n");
200 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
201 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
202 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
203 testres |= EC_TEST_CHECK_PARSE(node, -1, " ");
204 testres |= EC_TEST_CHECK_PARSE(node, -1, "foox");
205 testres |= EC_TEST_CHECK_PARSE(node, -1, "toto");
206 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
209 /* test completion */
210 node = EC_NODE_OR(EC_NO_ID,
211 ec_node_str(EC_NO_ID, "foo"),
212 ec_node_str(EC_NO_ID, "bar"),
213 ec_node_str(EC_NO_ID, "bar2"),
214 ec_node_str(EC_NO_ID, "toto"),
215 ec_node_str(EC_NO_ID, "titi")
218 EC_LOG(EC_LOG_ERR, "cannot create node\n");
221 testres |= EC_TEST_CHECK_COMPLETE(node,
223 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
224 testres |= EC_TEST_CHECK_COMPLETE(node,
225 "f", EC_NODE_ENDLIST,
226 "foo", EC_NODE_ENDLIST);
227 testres |= EC_TEST_CHECK_COMPLETE(node,
228 "b", EC_NODE_ENDLIST,
229 "bar", "bar2", EC_NODE_ENDLIST);
230 testres |= EC_TEST_CHECK_COMPLETE(node,
231 "bar", EC_NODE_ENDLIST,
232 "bar", "bar2", EC_NODE_ENDLIST);
233 testres |= EC_TEST_CHECK_COMPLETE(node,
234 "t", EC_NODE_ENDLIST,
235 "toto", "titi", EC_NODE_ENDLIST);
236 testres |= EC_TEST_CHECK_COMPLETE(node,
237 "to", EC_NODE_ENDLIST,
238 "toto", EC_NODE_ENDLIST);
239 testres |= EC_TEST_CHECK_COMPLETE(node,
240 "x", EC_NODE_ENDLIST,
248 static struct ec_test ec_node_or_test = {
250 .test = ec_node_or_testcase,
253 EC_TEST_REGISTER(ec_node_or_test);