e606a09dbb2b516f8e5082785ed46b33c5f93994
[protos/libecoli.git] / lib / ecoli_tk_seq.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 #include <assert.h>
32 #include <stdarg.h>
33
34 #include <ecoli_malloc.h>
35 #include <ecoli_log.h>
36 #include <ecoli_test.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_tk.h>
39 #include <ecoli_tk_str.h>
40 #include <ecoli_tk_option.h>
41 #include <ecoli_tk_seq.h>
42
43 struct ec_tk_seq {
44         struct ec_tk gen;
45         struct ec_tk **table;
46         unsigned int len;
47 };
48
49 static struct ec_parsed_tk *ec_tk_seq_parse(const struct ec_tk *gen_tk,
50         const struct ec_strvec *strvec)
51 {
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;
56         size_t len = 0;
57         unsigned int i;
58
59         parsed_tk = ec_parsed_tk_new();
60         if (parsed_tk == NULL)
61                 goto fail;
62
63         for (i = 0; i < tk->len; i++) {
64                 if (ec_strvec_slice(&childvec, strvec, len) < 0)
65                         goto fail;
66
67                 child_parsed_tk = ec_tk_parse_tokens(tk->table[i], &childvec);
68                 if (child_parsed_tk == NULL)
69                         goto fail;
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);
73                         return parsed_tk;
74                 }
75
76                 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
77                 len += ec_parsed_tk_len(child_parsed_tk);
78         }
79
80         match_strvec = ec_strvec_ndup(strvec, len);
81         if (match_strvec == NULL)
82                 goto fail;
83
84         ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
85
86         return parsed_tk;
87
88  fail:
89         ec_parsed_tk_free(parsed_tk);
90         return NULL;
91 }
92
93 static struct ec_completed_tk *ec_tk_seq_complete(const struct ec_tk *gen_tk,
94         const struct ec_strvec *strvec)
95 {
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;
100         size_t len = 0;
101         unsigned int i;
102
103         completed_tk = ec_completed_tk_new();
104         if (completed_tk == NULL)
105                 return NULL;
106
107         if (tk->len == 0)
108                 return completed_tk;
109
110         for (i = 0; i < tk->len && len < ec_strvec_len(strvec); i++) {
111                 if (ec_strvec_slice(&childvec, strvec, len) < 0)
112                         goto fail;
113
114                 child_completed_tk = ec_tk_complete_tokens(tk->table[i],
115                         &childvec);
116                 if (child_completed_tk == NULL) {
117                         ec_completed_tk_free(completed_tk);
118                         return NULL;
119                 }
120                 ec_completed_tk_merge(completed_tk, child_completed_tk);
121
122                 parsed_tk = ec_tk_parse_tokens(tk->table[i], &childvec);
123                 if (parsed_tk == NULL)
124                         goto fail;
125                 if (!ec_parsed_tk_matches(parsed_tk)) {
126                         ec_parsed_tk_free(parsed_tk);
127                         break;
128                 }
129
130                 len += ec_strvec_len(parsed_tk->strvec);
131                 ec_parsed_tk_free(parsed_tk);
132         }
133
134         return completed_tk;
135
136 fail:
137         /* XXX */
138         return NULL;
139 }
140
141 static void ec_tk_seq_free_priv(struct ec_tk *gen_tk)
142 {
143         struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
144         unsigned int i;
145
146         for (i = 0; i < tk->len; i++)
147                 ec_tk_free(tk->table[i]);
148         ec_free(tk->table);
149 }
150
151 static struct ec_tk_ops ec_tk_seq_ops = {
152         .typename = "seq",
153         .parse = ec_tk_seq_parse,
154         .complete = ec_tk_seq_complete,
155         .free_priv = ec_tk_seq_free_priv,
156 };
157
158 struct ec_tk *ec_tk_seq_new(const char *id)
159 {
160         struct ec_tk_seq *tk = NULL;
161
162         tk = (struct ec_tk_seq *)ec_tk_new(id, &ec_tk_seq_ops, sizeof(*tk));
163         if (tk == NULL)
164                 return NULL;
165
166         tk->table = NULL;
167         tk->len = 0;
168
169         return &tk->gen;
170 }
171
172 struct ec_tk *ec_tk_seq_new_list(const char *id, ...)
173 {
174         struct ec_tk_seq *tk = NULL;
175         struct ec_tk *child;
176         va_list ap;
177         int fail = 0;
178
179         va_start(ap, id);
180
181         tk = (struct ec_tk_seq *)ec_tk_seq_new(id);
182         if (tk == NULL)
183                 goto fail;
184
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 */
189
190                 if (child == NULL || ec_tk_seq_add(&tk->gen, child) < 0)
191                         fail = 1;
192         }
193
194         if (fail == 1)
195                 goto fail;
196
197         va_end(ap);
198         return &tk->gen;
199
200 fail:
201         ec_tk_free(&tk->gen); /* will also free children */
202         va_end(ap);
203         return NULL;
204 }
205
206 int ec_tk_seq_add(struct ec_tk *gen_tk, struct ec_tk *child)
207 {
208         struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
209         struct ec_tk **table;
210
211         // XXX check tk type
212
213         assert(tk != NULL);
214         assert(child != NULL);
215
216         table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
217         if (table == NULL)
218                 return -1;
219
220         tk->table = table;
221         table[tk->len] = child;
222         tk->len ++;
223
224         child->parent = gen_tk;
225         TAILQ_INSERT_TAIL(&gen_tk->children, child, next);
226
227         return 0;
228 }
229
230 static int ec_tk_seq_testcase(void)
231 {
232         struct ec_tk *tk;
233         int ret = 0;
234
235         tk = ec_tk_seq_new_list(NULL,
236                 ec_tk_str_new(NULL, "foo"),
237                 ec_tk_str_new(NULL, "bar"),
238                 EC_TK_ENDLIST);
239         if (tk == NULL) {
240                 ec_log(EC_LOG_ERR, "cannot create tk\n");
241                 return -1;
242         }
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",
245                 EC_TK_ENDLIST);
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);
251         ec_tk_free(tk);
252
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"),
258                 EC_TK_ENDLIST);
259         if (tk == NULL) {
260                 ec_log(EC_LOG_ERR, "cannot create tk\n");
261                 return -1;
262         }
263         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
264                 "", EC_TK_ENDLIST,
265                 "foo", EC_TK_ENDLIST,
266                 "foo");
267         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
268                 "f", EC_TK_ENDLIST,
269                 "oo", EC_TK_ENDLIST,
270                 "oo");
271         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
272                 "foo", EC_TK_ENDLIST,
273                 "", EC_TK_ENDLIST,
274                 "");
275         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
276                 "foo", "", EC_TK_ENDLIST,
277                 "bar", "toto", EC_TK_ENDLIST,
278                 "");
279         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
280                 "foo", "t", EC_TK_ENDLIST,
281                 "oto", EC_TK_ENDLIST,
282                 "oto");
283         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
284                 "foo", "b", EC_TK_ENDLIST,
285                 "ar", EC_TK_ENDLIST,
286                 "ar");
287         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
288                 "foo", "bar", EC_TK_ENDLIST,
289                 "", EC_TK_ENDLIST,
290                 "");
291         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
292                 "x", EC_TK_ENDLIST,
293                 EC_TK_ENDLIST,
294                 "");
295         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
296                 "foobarx", EC_TK_ENDLIST,
297                 EC_TK_ENDLIST,
298                 "");
299         ec_tk_free(tk);
300
301         return ret;
302 }
303
304 static struct ec_test ec_tk_seq_test = {
305         .name = "tk_seq",
306         .test = ec_tk_seq_testcase,
307 };
308
309 EC_REGISTER_TEST(ec_tk_seq_test);