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.
33 #include <ecoli_log.h>
34 #include <ecoli_malloc.h>
35 #include <ecoli_test.h>
36 #include <ecoli_strvec.h>
38 #include <ecoli_tk_str.h>
46 static struct ec_parsed_tk *ec_tk_str_parse(const struct ec_tk *gen_tk,
47 const struct ec_strvec *strvec)
49 struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
50 struct ec_strvec *match_strvec;
51 struct ec_parsed_tk *parsed_tk = NULL;
54 parsed_tk = ec_parsed_tk_new();
55 if (parsed_tk == NULL)
58 if (ec_strvec_len(strvec) == 0)
61 str = ec_strvec_val(strvec, 0);
62 if (strcmp(str, tk->string) != 0)
65 match_strvec = ec_strvec_ndup(strvec, 0, 1);
66 if (match_strvec == NULL)
69 ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
74 ec_parsed_tk_free(parsed_tk);
78 static struct ec_completed_tk *ec_tk_str_complete(const struct ec_tk *gen_tk,
79 const struct ec_strvec *strvec)
81 struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
82 struct ec_completed_tk *completed_tk;
83 struct ec_completed_tk_elt *completed_tk_elt;
84 const char *str, *add;
87 completed_tk = ec_completed_tk_new();
88 if (completed_tk == NULL)
91 if (ec_strvec_len(strvec) > 1)
94 if (ec_strvec_len(strvec) == 1) {
95 str = ec_strvec_val(strvec, 0);
96 for (n = 0; n < tk->len; n++) {
97 if (str[n] != tk->string[n])
104 add = tk->string + n;
107 completed_tk_elt = ec_completed_tk_elt_new(gen_tk, add);
108 if (completed_tk_elt == NULL) {
109 ec_completed_tk_free(completed_tk);
113 ec_completed_tk_add_elt(completed_tk, completed_tk_elt);
118 static const char *ec_tk_str_desc(const struct ec_tk *gen_tk)
120 struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
125 static void ec_tk_str_free_priv(struct ec_tk *gen_tk)
127 struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
132 static const struct ec_tk_ops ec_tk_str_ops = {
134 .parse = ec_tk_str_parse,
135 .complete = ec_tk_str_complete,
136 .desc = ec_tk_str_desc,
137 .free_priv = ec_tk_str_free_priv,
140 struct ec_tk *ec_tk_str_new(const char *id)
142 struct ec_tk *gen_tk = NULL;
144 gen_tk = ec_tk_new(id, &ec_tk_str_ops, sizeof(struct ec_tk_str));
151 int ec_tk_str_set_str(struct ec_tk *gen_tk, const char *str)
153 struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
157 if (tk->string != NULL)
160 tk->string = ec_strdup(str);
161 if (tk->string == NULL)
164 tk->len = strlen(tk->string);
169 struct ec_tk *ec_tk_str(const char *id, const char *str)
171 struct ec_tk *gen_tk = NULL;
173 gen_tk = ec_tk_str_new(id);
177 if (ec_tk_str_set_str(gen_tk, str) < 0)
187 static int ec_tk_str_testcase(void)
192 tk = ec_tk_str(NULL, "foo");
194 ec_log(EC_LOG_ERR, "cannot create tk\n");
197 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo");
198 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "bar");
199 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foobar");
200 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, " foo");
201 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "");
204 tk = ec_tk_str(NULL, "Здравствуйте");
206 ec_log(EC_LOG_ERR, "cannot create tk\n");
209 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "Здравствуйте");
210 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "Здравствуйте",
212 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo");
213 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "");
216 /* an empty token string always matches */
217 tk = ec_tk_str(NULL, "");
219 ec_log(EC_LOG_ERR, "cannot create tk\n");
222 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "");
223 ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "", "foo");
224 ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo");
227 /* test completion */
228 tk = ec_tk_str(NULL, "foo");
230 ec_log(EC_LOG_ERR, "cannot create tk\n");
233 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
235 "foo", EC_TK_ENDLIST,
237 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
241 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
242 "foo", EC_TK_ENDLIST,
245 ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
254 static struct ec_test ec_tk_str_test = {
256 .test = ec_tk_str_testcase,
259 EC_REGISTER_TEST(ec_tk_str_test);