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>
39 #include <ecoli_tk_or.h>
40 #include <ecoli_tk_str.h>
41 #include <ecoli_test.h>
49 static struct ec_parsed_tk *ec_tk_or_parse(const struct ec_tk *gen_tk,
50 const struct ec_strvec *strvec)
52 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
53 struct ec_parsed_tk *parsed_tk, *child_parsed_tk = NULL;
54 struct ec_strvec *match_strvec;
57 parsed_tk = ec_parsed_tk_new();
58 if (parsed_tk == NULL)
61 for (i = 0; i < tk->len; i++) {
62 child_parsed_tk = ec_tk_parse_tokens(tk->table[i], strvec);
63 if (child_parsed_tk == NULL)
65 if (ec_parsed_tk_matches(child_parsed_tk))
67 ec_parsed_tk_free(child_parsed_tk);
68 child_parsed_tk = NULL;
75 match_strvec = ec_strvec_dup(child_parsed_tk->strvec);
76 if (match_strvec == NULL)
79 ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
80 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
85 ec_parsed_tk_free(child_parsed_tk);
86 ec_parsed_tk_free(parsed_tk);
90 static struct ec_completed_tk *ec_tk_or_complete(const struct ec_tk *gen_tk,
91 const struct ec_strvec *strvec)
93 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
94 struct ec_completed_tk *completed_tk, *child_completed_tk;
97 completed_tk = ec_completed_tk_new();
98 if (completed_tk == NULL)
101 for (n = 0; n < tk->len; n++) {
102 child_completed_tk = ec_tk_complete_tokens(tk->table[n],
105 if (child_completed_tk == NULL) // XXX fail instead?
108 ec_completed_tk_merge(completed_tk, child_completed_tk);
114 static void ec_tk_or_free_priv(struct ec_tk *gen_tk)
116 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
119 for (i = 0; i < tk->len; i++)
120 ec_tk_free(tk->table[i]);
124 int ec_tk_or_add(struct ec_tk *gen_tk, struct ec_tk *child)
126 struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
127 struct ec_tk **table;
134 gen_tk->flags &= ~EC_TK_F_BUILT;
136 table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
143 table[tk->len] = child;
146 child->parent = gen_tk;
147 TAILQ_INSERT_TAIL(&gen_tk->children, child, next);
152 static struct ec_tk_type ec_tk_or_type = {
154 .parse = ec_tk_or_parse,
155 .complete = ec_tk_or_complete,
156 .free_priv = ec_tk_or_free_priv,
159 EC_TK_TYPE_REGISTER(ec_tk_or_type);
161 struct ec_tk *ec_tk_or(const char *id)
163 struct ec_tk *gen_tk = NULL;
164 struct ec_tk_or *tk = NULL;
166 gen_tk = ec_tk_new(id, &ec_tk_or_type, sizeof(*tk));
170 tk = (struct ec_tk_or *)gen_tk;
177 struct ec_tk *__ec_tk_or(const char *id, ...)
179 struct ec_tk *gen_tk = NULL;
180 struct ec_tk_or *tk = NULL;
187 gen_tk = ec_tk_or(id);
188 tk = (struct ec_tk_or *)gen_tk;
192 for (child = va_arg(ap, struct ec_tk *);
193 child != EC_TK_ENDLIST;
194 child = va_arg(ap, struct ec_tk *)) {
196 /* on error, don't quit the loop to avoid leaks */
197 if (fail == 1 || child == NULL ||
198 ec_tk_or_add(gen_tk, child) < 0) {
211 ec_tk_free(gen_tk); /* will also free children */
216 static int ec_tk_or_testcase(void)
222 ec_tk_str(NULL, "foo"),
223 ec_tk_str(NULL, "bar")
226 ec_log(EC_LOG_ERR, "cannot create tk\n");
229 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo");
230 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "bar");
231 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "bar");
232 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, " ");
233 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foox");
234 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "toto");
235 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "");
238 /* test completion */
240 ec_tk_str(NULL, "foo"),
241 ec_tk_str(NULL, "bar"),
242 ec_tk_str(NULL, "bar2"),
243 ec_tk_str(NULL, "toto"),
244 ec_tk_str(NULL, "titi")
247 ec_log(EC_LOG_ERR, "cannot create tk\n");
250 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
252 "foo", "bar", "bar2", "toto", "titi", EC_TK_ENDLIST,
254 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
258 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
260 "ar", "ar2", EC_TK_ENDLIST,
262 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
263 "bar", EC_TK_ENDLIST,
264 "", "2", EC_TK_ENDLIST,
266 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
268 "oto", "iti", EC_TK_ENDLIST,
270 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
274 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
283 static struct ec_test ec_tk_or_test = {
285 .test = ec_tk_or_testcase,
288 EC_TEST_REGISTER(ec_tk_or_test);