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 ec_parsed_tk_free_children(parsed_tk);
83 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
84 len += ec_parsed_tk_len(child_parsed_tk);
87 match_strvec = ec_strvec_ndup(strvec, 0, len);
88 if (match_strvec == NULL)
91 ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
96 ec_strvec_free(childvec);
97 ec_parsed_tk_free(parsed_tk);
101 static struct ec_completed_tk *ec_tk_seq_complete(const struct ec_tk *gen_tk,
102 const struct ec_strvec *strvec)
104 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
105 struct ec_completed_tk *completed_tk, *child_completed_tk;
106 struct ec_strvec *childvec = NULL;
107 struct ec_parsed_tk *parsed_tk;
111 completed_tk = ec_completed_tk_new();
112 if (completed_tk == NULL)
118 for (i = 0; i < tk->len && len < ec_strvec_len(strvec); i++) {
119 childvec = ec_strvec_ndup(strvec, len,
120 ec_strvec_len(strvec) - len);
121 if (childvec == NULL)
124 child_completed_tk = ec_tk_complete_tokens(tk->table[i],
126 if (child_completed_tk == NULL)
129 ec_completed_tk_merge(completed_tk, child_completed_tk);
131 parsed_tk = ec_tk_parse_tokens(tk->table[i], childvec);
132 if (parsed_tk == NULL)
135 ec_strvec_free(childvec);
138 if (!ec_parsed_tk_matches(parsed_tk)) {
139 ec_parsed_tk_free(parsed_tk);
143 len += ec_strvec_len(parsed_tk->strvec);
144 ec_parsed_tk_free(parsed_tk);
150 ec_strvec_free(childvec);
151 ec_completed_tk_free(completed_tk);
155 static void ec_tk_seq_free_priv(struct ec_tk *gen_tk)
157 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
160 for (i = 0; i < tk->len; i++)
161 ec_tk_free(tk->table[i]);
165 static struct ec_tk_ops ec_tk_seq_ops = {
167 .parse = ec_tk_seq_parse,
168 .complete = ec_tk_seq_complete,
169 .free_priv = ec_tk_seq_free_priv,
172 int ec_tk_seq_add(struct ec_tk *gen_tk, struct ec_tk *child)
174 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
175 struct ec_tk **table;
184 gen_tk->flags &= ~EC_TK_F_BUILT;
186 table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
193 table[tk->len] = child;
196 child->parent = gen_tk;
197 TAILQ_INSERT_TAIL(&gen_tk->children, child, next); // XXX really needed?
202 struct ec_tk *ec_tk_seq(const char *id)
204 struct ec_tk *gen_tk = NULL;
205 struct ec_tk_seq *tk = NULL;
207 gen_tk = ec_tk_new(id, &ec_tk_seq_ops, sizeof(*tk));
211 tk = (struct ec_tk_seq *)gen_tk;
218 struct ec_tk *__ec_tk_seq(const char *id, ...)
220 struct ec_tk *gen_tk = NULL;
221 struct ec_tk_seq *tk = NULL;
228 gen_tk = ec_tk_seq(id);
229 tk = (struct ec_tk_seq *)gen_tk;
233 for (child = va_arg(ap, struct ec_tk *);
234 child != EC_TK_ENDLIST;
235 child = va_arg(ap, struct ec_tk *)) {
237 /* on error, don't quit the loop to avoid leaks */
238 if (fail == 1 || child == NULL ||
239 ec_tk_seq_add(&tk->gen, child) < 0) {
252 ec_tk_free(gen_tk); /* will also free children */
257 static int ec_tk_seq_testcase(void)
263 ec_tk_str(NULL, "foo"),
264 ec_tk_str(NULL, "bar")
267 ec_log(EC_LOG_ERR, "cannot create tk\n");
270 ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar");
271 ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar", "toto");
272 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo");
273 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foox", "bar");
274 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo", "barx");
275 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "bar", "foo");
276 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "", "foo");
279 /* test completion */
281 ec_tk_str(NULL, "foo"),
282 ec_tk_option_new(NULL, ec_tk_str(NULL, "toto")),
283 ec_tk_str(NULL, "bar")
286 ec_log(EC_LOG_ERR, "cannot create tk\n");
289 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
291 "foo", EC_TK_ENDLIST,
293 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
297 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
298 "foo", EC_TK_ENDLIST,
301 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
302 "foo", "", EC_TK_ENDLIST,
303 "bar", "toto", EC_TK_ENDLIST,
305 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
306 "foo", "t", EC_TK_ENDLIST,
307 "oto", EC_TK_ENDLIST,
309 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
310 "foo", "b", EC_TK_ENDLIST,
313 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
314 "foo", "bar", EC_TK_ENDLIST,
317 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
321 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
322 "foobarx", EC_TK_ENDLIST,
330 static struct ec_test ec_tk_seq_test = {
332 .test = ec_tk_seq_testcase,
335 EC_REGISTER_TEST(ec_tk_seq_test);