option, shlex
[protos/libecoli.git] / lib / ecoli_tk_str.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <ecoli_log.h>
33 #include <ecoli_malloc.h>
34 #include <ecoli_test.h>
35 #include <ecoli_tk.h>
36 #include <ecoli_tk_str.h>
37
38 static struct ec_parsed_tk *ec_tk_str_parse(const struct ec_tk *gen_tk,
39         const char *str)
40 {
41         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
42         struct ec_parsed_tk *parsed_tk;
43
44         if (strncmp(str, tk->string, tk->len) != 0)
45                 return NULL;
46
47         parsed_tk = ec_parsed_tk_new(gen_tk);
48         if (parsed_tk == NULL)
49                 return NULL;
50
51         parsed_tk->str = ec_strndup(str, tk->len);
52
53         return parsed_tk;
54 }
55
56 static struct ec_completed_tk *ec_tk_str_complete(const struct ec_tk *gen_tk,
57         const char *str)
58 {
59         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
60         struct ec_completed_tk *completed_tk;
61         struct ec_completed_tk_elt *completed_tk_elt;
62         size_t n;
63
64         completed_tk = ec_completed_tk_new();
65         if (completed_tk == NULL)
66                 return NULL;
67
68         /* check the string has the same beginning than the token */
69         for (n = 0; n < tk->len; n++) {
70                 if (str[n] != tk->string[n])
71                         break;
72         }
73
74         if (str[n] != '\0')
75                 return completed_tk;
76         if (tk->string[n] == '\0')
77                 return completed_tk;
78
79         completed_tk_elt = ec_completed_tk_elt_new(gen_tk, tk->string + n,
80                 tk->string);
81         if (completed_tk_elt == NULL) {
82                 ec_completed_tk_free(completed_tk);
83                 return NULL;
84         }
85
86         ec_completed_tk_add_elt(completed_tk, completed_tk_elt);
87
88         return completed_tk;
89 }
90
91 static void ec_tk_str_free_priv(struct ec_tk *gen_tk)
92 {
93         struct ec_tk_str *tk = (struct ec_tk_str *)gen_tk;
94
95         ec_free(tk->string);
96 }
97
98 static struct ec_tk_ops ec_tk_str_ops = {
99         .parse = ec_tk_str_parse,
100         .complete = ec_tk_str_complete,
101         .free_priv = ec_tk_str_free_priv,
102 };
103
104 struct ec_tk *ec_tk_str_new(const char *id, const char *str)
105 {
106         struct ec_tk_str *tk = NULL;
107         char *s = NULL;
108
109         tk = (struct ec_tk_str *)ec_tk_new(id, &ec_tk_str_ops, sizeof(*tk));
110         if (tk == NULL)
111                 goto fail;
112
113         s = ec_strdup(str);
114         if (s == NULL)
115                 goto fail;
116
117         tk->string = s;
118         tk->len = strlen(s);
119
120         return &tk->gen;
121
122 fail:
123         ec_free(s);
124         ec_free(tk);
125         return NULL;
126 }
127
128 static int ec_tk_str_testcase(void)
129 {
130         struct ec_tk *tk;
131         int ret = 0;
132
133         tk = ec_tk_str_new(NULL, "foo");
134         if (tk == NULL) {
135                 ec_log(EC_LOG_ERR, "cannot create tk\n");
136                 return -1;
137         }
138         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", "foo");
139         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foobar", "foo");
140         ret |= EC_TEST_CHECK_TK_PARSE(tk, " foo", NULL);
141         ret |= EC_TEST_CHECK_TK_PARSE(tk, "", NULL);
142         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", "foo");
143         ec_tk_free(tk);
144
145         tk = ec_tk_str_new(NULL, "Здравствуйте");
146         if (tk == NULL) {
147                 ec_log(EC_LOG_ERR, "cannot create tk\n");
148                 return -1;
149         }
150         ret |= EC_TEST_CHECK_TK_PARSE(tk, "Здравствуйте", "Здравствуйте");
151         ret |= EC_TEST_CHECK_TK_PARSE(tk, "Здравствуйте John!", "Здравствуйте");
152         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", NULL);
153         ret |= EC_TEST_CHECK_TK_PARSE(tk, "", NULL);
154         ec_tk_free(tk);
155
156         /* an empty token string always matches */
157         tk = ec_tk_str_new(NULL, "");
158         if (tk == NULL) {
159                 ec_log(EC_LOG_ERR, "cannot create tk\n");
160                 return -1;
161         }
162         ret |= EC_TEST_CHECK_TK_PARSE(tk, "", "");
163         ret |= EC_TEST_CHECK_TK_PARSE(tk, "foo", "");
164         ec_tk_free(tk);
165
166         /* test completion */
167         tk = ec_tk_str_new(NULL, "foo");
168         if (tk == NULL) {
169                 ec_log(EC_LOG_ERR, "cannot create tk\n");
170                 return -1;
171         }
172         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "", "foo");
173         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "f", "oo");
174         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "foo", "");
175         ret |= EC_TEST_CHECK_TK_COMPLETE(tk, "x", "");
176         ec_tk_free(tk);
177
178         return ret;
179 }
180
181 static struct ec_test ec_tk_str_test = {
182         .name = "tk_str",
183         .test = ec_tk_str_testcase,
184 };
185
186 EC_REGISTER_TEST(ec_tk_str_test);