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.
34 #include <ecoli_malloc.h>
35 #include <ecoli_log.h>
36 #include <ecoli_test.h>
37 #include <ecoli_strvec.h>
39 #include <ecoli_tk_str.h>
40 #include <ecoli_tk_option.h>
41 #include <ecoli_tk_seq.h>
49 static struct ec_parsed_tk *ec_tk_seq_parse(const struct ec_tk *gen_tk,
50 const struct ec_strvec *strvec)
52 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
53 struct ec_parsed_tk *parsed_tk, *child_parsed_tk;
54 struct ec_strvec *match_strvec;
55 struct ec_strvec childvec;
59 parsed_tk = ec_parsed_tk_new();
60 if (parsed_tk == NULL)
63 for (i = 0; i < tk->len; i++) {
64 if (ec_strvec_slice(&childvec, strvec, len) < 0)
67 child_parsed_tk = ec_tk_parse_tokens(tk->table[i], &childvec);
68 if (child_parsed_tk == NULL)
70 if (!ec_parsed_tk_matches(child_parsed_tk)) {
71 ec_parsed_tk_free(child_parsed_tk);
72 ec_parsed_tk_free_children(parsed_tk);
76 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
77 len += ec_parsed_tk_len(child_parsed_tk);
80 match_strvec = ec_strvec_ndup(strvec, len);
81 if (match_strvec == NULL)
84 ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
89 ec_parsed_tk_free(parsed_tk);
93 static struct ec_completed_tk *ec_tk_seq_complete(const struct ec_tk *gen_tk,
94 const struct ec_strvec *strvec)
96 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
97 struct ec_completed_tk *completed_tk, *child_completed_tk;
98 struct ec_strvec childvec;
99 struct ec_parsed_tk *parsed_tk;
103 completed_tk = ec_completed_tk_new();
104 if (completed_tk == NULL)
110 for (i = 0; i < tk->len && len < ec_strvec_len(strvec); i++) {
111 if (ec_strvec_slice(&childvec, strvec, len) < 0)
114 child_completed_tk = ec_tk_complete_tokens(tk->table[i],
116 if (child_completed_tk == NULL) {
117 ec_completed_tk_free(completed_tk);
120 ec_completed_tk_merge(completed_tk, child_completed_tk);
122 parsed_tk = ec_tk_parse_tokens(tk->table[i], &childvec);
123 if (parsed_tk == NULL)
125 if (!ec_parsed_tk_matches(parsed_tk)) {
126 ec_parsed_tk_free(parsed_tk);
130 len += ec_strvec_len(parsed_tk->strvec);
131 ec_parsed_tk_free(parsed_tk);
141 static void ec_tk_seq_free_priv(struct ec_tk *gen_tk)
143 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
146 for (i = 0; i < tk->len; i++)
147 ec_tk_free(tk->table[i]);
151 static struct ec_tk_ops ec_tk_seq_ops = {
153 .parse = ec_tk_seq_parse,
154 .complete = ec_tk_seq_complete,
155 .free_priv = ec_tk_seq_free_priv,
158 struct ec_tk *ec_tk_seq_new(const char *id)
160 struct ec_tk_seq *tk = NULL;
162 tk = (struct ec_tk_seq *)ec_tk_new(id, &ec_tk_seq_ops, sizeof(*tk));
172 struct ec_tk *ec_tk_seq_new_list(const char *id, ...)
174 struct ec_tk_seq *tk = NULL;
181 tk = (struct ec_tk_seq *)ec_tk_seq_new(id);
185 for (child = va_arg(ap, struct ec_tk *);
186 child != EC_TK_ENDLIST;
187 child = va_arg(ap, struct ec_tk *)) {
188 /* on error, don't quit the loop to avoid leaks */
190 if (child == NULL || ec_tk_seq_add(&tk->gen, child) < 0)
201 ec_tk_free(&tk->gen); /* will also free children */
206 int ec_tk_seq_add(struct ec_tk *gen_tk, struct ec_tk *child)
208 struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
209 struct ec_tk **table;
214 assert(child != NULL);
216 table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
221 table[tk->len] = child;
224 child->parent = gen_tk;
225 TAILQ_INSERT_TAIL(&gen_tk->children, child, next);
230 static int ec_tk_seq_testcase(void)
235 tk = ec_tk_seq_new_list(NULL,
236 ec_tk_str_new(NULL, "foo"),
237 ec_tk_str_new(NULL, "bar"),
240 ec_log(EC_LOG_ERR, "cannot create tk\n");
243 ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar", EC_TK_ENDLIST);
244 ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar", "toto",
246 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo", EC_TK_ENDLIST);
247 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foox", "bar", EC_TK_ENDLIST);
248 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo", "barx", EC_TK_ENDLIST);
249 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "bar", "foo", EC_TK_ENDLIST);
250 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "", "foo", EC_TK_ENDLIST);
253 /* test completion */
254 tk = ec_tk_seq_new_list(NULL,
255 ec_tk_str_new(NULL, "foo"),
256 ec_tk_option_new(NULL, ec_tk_str_new(NULL, "toto")),
257 ec_tk_str_new(NULL, "bar"),
260 ec_log(EC_LOG_ERR, "cannot create tk\n");
263 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
265 "foo", EC_TK_ENDLIST,
267 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
271 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
272 "foo", EC_TK_ENDLIST,
275 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
276 "foo", "", EC_TK_ENDLIST,
277 "bar", "toto", EC_TK_ENDLIST,
279 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
280 "foo", "t", EC_TK_ENDLIST,
281 "oto", EC_TK_ENDLIST,
283 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
284 "foo", "b", EC_TK_ENDLIST,
287 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
288 "foo", "bar", EC_TK_ENDLIST,
291 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
295 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
296 "foobarx", EC_TK_ENDLIST,
304 static struct ec_test ec_tk_seq_test = {
306 .test = ec_tk_seq_testcase,
309 EC_REGISTER_TEST(ec_tk_seq_test);