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>
40 #include <ecoli_tk_str.h>
41 #include <ecoli_tk_option.h>
42 #include <ecoli_tk_seq.h>
50 static struct ec_parsed_tk *ec_tk_seq_parse(const struct ec_tk *gen_tk,
51 const struct ec_strvec *strvec)
53 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
54 struct ec_parsed_tk *parsed_tk, *child_parsed_tk;
55 struct ec_strvec *match_strvec;
56 struct ec_strvec *childvec = NULL;
60 parsed_tk = ec_parsed_tk_new();
61 if (parsed_tk == NULL)
64 for (i = 0; i < tk->len; i++) {
65 childvec = ec_strvec_ndup(strvec, len,
66 ec_strvec_len(strvec) - len);
70 child_parsed_tk = ec_tk_parse_tokens(tk->table[i], childvec);
71 if (child_parsed_tk == NULL)
74 ec_strvec_free(childvec);
77 if (!ec_parsed_tk_matches(child_parsed_tk)) {
78 ec_parsed_tk_free(child_parsed_tk);
79 // XXX ec_parsed_tk_free_children needed? see subset.c
80 ec_parsed_tk_free_children(parsed_tk);
84 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
85 len += ec_parsed_tk_len(child_parsed_tk);
88 match_strvec = ec_strvec_ndup(strvec, 0, len);
89 if (match_strvec == NULL)
92 ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
97 ec_strvec_free(childvec);
98 ec_parsed_tk_free(parsed_tk);
102 static struct ec_completed_tk *ec_tk_seq_complete(const struct ec_tk *gen_tk,
103 const struct ec_strvec *strvec)
105 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
106 struct ec_completed_tk *completed_tk, *child_completed_tk;
107 struct ec_strvec *childvec = NULL;
108 struct ec_parsed_tk *parsed_tk;
112 completed_tk = ec_completed_tk_new();
113 if (completed_tk == NULL)
119 for (i = 0; i < tk->len && len < ec_strvec_len(strvec); i++) {
120 childvec = ec_strvec_ndup(strvec, len,
121 ec_strvec_len(strvec) - len);
122 if (childvec == NULL)
125 child_completed_tk = ec_tk_complete_tokens(tk->table[i],
127 if (child_completed_tk == NULL)
130 ec_completed_tk_merge(completed_tk, child_completed_tk);
132 parsed_tk = ec_tk_parse_tokens(tk->table[i], childvec);
133 if (parsed_tk == NULL)
136 ec_strvec_free(childvec);
139 if (!ec_parsed_tk_matches(parsed_tk)) {
140 ec_parsed_tk_free(parsed_tk);
144 len += ec_strvec_len(parsed_tk->strvec);
145 ec_parsed_tk_free(parsed_tk);
151 ec_strvec_free(childvec);
152 ec_completed_tk_free(completed_tk);
156 static void ec_tk_seq_free_priv(struct ec_tk *gen_tk)
158 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
161 for (i = 0; i < tk->len; i++)
162 ec_tk_free(tk->table[i]);
166 static struct ec_tk_type ec_tk_seq_type = {
168 .parse = ec_tk_seq_parse,
169 .complete = ec_tk_seq_complete,
170 .free_priv = ec_tk_seq_free_priv,
173 EC_TK_TYPE_REGISTER(ec_tk_seq_type);
175 int ec_tk_seq_add(struct ec_tk *gen_tk, struct ec_tk *child)
177 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
178 struct ec_tk **table;
187 gen_tk->flags &= ~EC_TK_F_BUILT;
189 table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
196 table[tk->len] = child;
199 child->parent = gen_tk;
200 TAILQ_INSERT_TAIL(&gen_tk->children, child, next); // XXX really needed?
205 struct ec_tk *ec_tk_seq(const char *id)
207 struct ec_tk *gen_tk = NULL;
208 struct ec_tk_seq *tk = NULL;
210 gen_tk = ec_tk_new(id, &ec_tk_seq_type, sizeof(*tk));
214 tk = (struct ec_tk_seq *)gen_tk;
221 struct ec_tk *__ec_tk_seq(const char *id, ...)
223 struct ec_tk *gen_tk = NULL;
224 struct ec_tk_seq *tk = NULL;
231 gen_tk = ec_tk_seq(id);
232 tk = (struct ec_tk_seq *)gen_tk;
236 for (child = va_arg(ap, struct ec_tk *);
237 child != EC_TK_ENDLIST;
238 child = va_arg(ap, struct ec_tk *)) {
240 /* on error, don't quit the loop to avoid leaks */
241 if (fail == 1 || child == NULL ||
242 ec_tk_seq_add(&tk->gen, child) < 0) {
255 ec_tk_free(gen_tk); /* will also free children */
260 static int ec_tk_seq_testcase(void)
266 ec_tk_str(NULL, "foo"),
267 ec_tk_str(NULL, "bar")
270 ec_log(EC_LOG_ERR, "cannot create tk\n");
273 ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar");
274 ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar", "toto");
275 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo");
276 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foox", "bar");
277 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo", "barx");
278 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "bar", "foo");
279 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "", "foo");
282 /* test completion */
284 ec_tk_str(NULL, "foo"),
285 ec_tk_option_new(NULL, ec_tk_str(NULL, "toto")),
286 ec_tk_str(NULL, "bar")
289 ec_log(EC_LOG_ERR, "cannot create tk\n");
292 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
294 "foo", EC_TK_ENDLIST,
296 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
300 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
301 "foo", EC_TK_ENDLIST,
304 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
305 "foo", "", EC_TK_ENDLIST,
306 "bar", "toto", EC_TK_ENDLIST,
308 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
309 "foo", "t", EC_TK_ENDLIST,
310 "oto", EC_TK_ENDLIST,
312 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
313 "foo", "b", EC_TK_ENDLIST,
316 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
317 "foo", "bar", EC_TK_ENDLIST,
320 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
324 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
325 "foobarx", EC_TK_ENDLIST,
333 static struct ec_test ec_tk_seq_test = {
335 .test = ec_tk_seq_testcase,
338 EC_TEST_REGISTER(ec_tk_seq_test);