save
[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 #include <errno.h>
34
35 #include <ecoli_malloc.h>
36 #include <ecoli_log.h>
37 #include <ecoli_test.h>
38 #include <ecoli_strvec.h>
39 #include <ecoli_tk.h>
40 #include <ecoli_tk_str.h>
41 #include <ecoli_tk_option.h>
42 #include <ecoli_tk_seq.h>
43
44 struct ec_tk_seq {
45         struct ec_tk gen;
46         struct ec_tk **table;
47         unsigned int len;
48 };
49
50 static struct ec_parsed_tk *ec_tk_seq_parse(const struct ec_tk *gen_tk,
51         const struct ec_strvec *strvec)
52 {
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;
57         size_t len = 0;
58         unsigned int i;
59
60         parsed_tk = ec_parsed_tk_new();
61         if (parsed_tk == NULL)
62                 goto fail;
63
64         for (i = 0; i < tk->len; i++) {
65                 childvec = ec_strvec_ndup(strvec, len,
66                         ec_strvec_len(strvec) - len);
67                 if (childvec == NULL)
68                         goto fail;
69
70                 child_parsed_tk = ec_tk_parse_tokens(tk->table[i], childvec);
71                 if (child_parsed_tk == NULL)
72                         goto fail;
73
74                 ec_strvec_free(childvec);
75                 childvec = NULL;
76
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);
80                         return parsed_tk;
81                 }
82
83                 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
84                 len += ec_parsed_tk_len(child_parsed_tk);
85         }
86
87         match_strvec = ec_strvec_ndup(strvec, 0, len);
88         if (match_strvec == NULL)
89                 goto fail;
90
91         ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
92
93         return parsed_tk;
94
95 fail:
96         ec_strvec_free(childvec);
97         ec_parsed_tk_free(parsed_tk);
98         return NULL;
99 }
100
101 static struct ec_completed_tk *ec_tk_seq_complete(const struct ec_tk *gen_tk,
102         const struct ec_strvec *strvec)
103 {
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;
108         size_t len = 0;
109         unsigned int i;
110
111         completed_tk = ec_completed_tk_new();
112         if (completed_tk == NULL)
113                 return NULL;
114
115         if (tk->len == 0)
116                 return completed_tk;
117
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)
122                         goto fail;
123
124                 child_completed_tk = ec_tk_complete_tokens(tk->table[i],
125                         childvec);
126                 if (child_completed_tk == NULL)
127                         goto fail;
128
129                 ec_completed_tk_merge(completed_tk, child_completed_tk);
130
131                 parsed_tk = ec_tk_parse_tokens(tk->table[i], childvec);
132                 if (parsed_tk == NULL)
133                         goto fail;
134
135                 ec_strvec_free(childvec);
136                 childvec = NULL;
137
138                 if (!ec_parsed_tk_matches(parsed_tk)) {
139                         ec_parsed_tk_free(parsed_tk);
140                         break;
141                 }
142
143                 len += ec_strvec_len(parsed_tk->strvec);
144                 ec_parsed_tk_free(parsed_tk);
145         }
146
147         return completed_tk;
148
149 fail:
150         ec_strvec_free(childvec);
151         ec_completed_tk_free(completed_tk);
152         return NULL;
153 }
154
155 static void ec_tk_seq_free_priv(struct ec_tk *gen_tk)
156 {
157         struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
158         unsigned int i;
159
160         for (i = 0; i < tk->len; i++)
161                 ec_tk_free(tk->table[i]);
162         ec_free(tk->table);
163 }
164
165 static struct ec_tk_ops ec_tk_seq_ops = {
166         .typename = "seq",
167         .parse = ec_tk_seq_parse,
168         .complete = ec_tk_seq_complete,
169         .free_priv = ec_tk_seq_free_priv,
170 };
171
172 int ec_tk_seq_add(struct ec_tk *gen_tk, struct ec_tk *child)
173 {
174         struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
175         struct ec_tk **table;
176
177         // XXX check tk type
178
179         assert(tk != NULL);
180
181         if (child == NULL)
182                 return -EINVAL;
183
184         gen_tk->flags &= ~EC_TK_F_BUILT;
185
186         table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
187         if (table == NULL) {
188                 ec_tk_free(child);
189                 return -1;
190         }
191
192         tk->table = table;
193         table[tk->len] = child;
194         tk->len++;
195
196         child->parent = gen_tk;
197         TAILQ_INSERT_TAIL(&gen_tk->children, child, next); // XXX really needed?
198
199         return 0;
200 }
201
202 struct ec_tk *ec_tk_seq(const char *id)
203 {
204         struct ec_tk *gen_tk = NULL;
205         struct ec_tk_seq *tk = NULL;
206
207         gen_tk = ec_tk_new(id, &ec_tk_seq_ops, sizeof(*tk));
208         if (gen_tk == NULL)
209                 return NULL;
210
211         tk = (struct ec_tk_seq *)gen_tk;
212         tk->table = NULL;
213         tk->len = 0;
214
215         return gen_tk;
216 }
217
218 struct ec_tk *__ec_tk_seq(const char *id, ...)
219 {
220         struct ec_tk *gen_tk = NULL;
221         struct ec_tk_seq *tk = NULL;
222         struct ec_tk *child;
223         va_list ap;
224         int fail = 0;
225
226         va_start(ap, id);
227
228         gen_tk = ec_tk_seq(id);
229         tk = (struct ec_tk_seq *)gen_tk;
230         if (tk == NULL)
231                 fail = 1;;
232
233         for (child = va_arg(ap, struct ec_tk *);
234              child != EC_TK_ENDLIST;
235              child = va_arg(ap, struct ec_tk *)) {
236
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) {
240                         fail = 1;
241                         ec_tk_free(child);
242                 }
243         }
244
245         if (fail == 1)
246                 goto fail;
247
248         va_end(ap);
249         return gen_tk;
250
251 fail:
252         ec_tk_free(gen_tk); /* will also free children */
253         va_end(ap);
254         return NULL;
255 }
256
257 static int ec_tk_seq_testcase(void)
258 {
259         struct ec_tk *tk;
260         int ret = 0;
261
262         tk = EC_TK_SEQ(NULL,
263                 ec_tk_str(NULL, "foo"),
264                 ec_tk_str(NULL, "bar")
265         );
266         if (tk == NULL) {
267                 ec_log(EC_LOG_ERR, "cannot create tk\n");
268                 return -1;
269         }
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");
277         ec_tk_free(tk);
278
279         /* test completion */
280         tk = EC_TK_SEQ(NULL,
281                 ec_tk_str(NULL, "foo"),
282                 ec_tk_option_new(NULL, ec_tk_str(NULL, "toto")),
283                 ec_tk_str(NULL, "bar")
284         );
285         if (tk == NULL) {
286                 ec_log(EC_LOG_ERR, "cannot create tk\n");
287                 return -1;
288         }
289         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
290                 "", EC_TK_ENDLIST,
291                 "foo", EC_TK_ENDLIST,
292                 "foo");
293         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
294                 "f", EC_TK_ENDLIST,
295                 "oo", EC_TK_ENDLIST,
296                 "oo");
297         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
298                 "foo", EC_TK_ENDLIST,
299                 "", EC_TK_ENDLIST,
300                 "");
301         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
302                 "foo", "", EC_TK_ENDLIST,
303                 "bar", "toto", EC_TK_ENDLIST,
304                 "");
305         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
306                 "foo", "t", EC_TK_ENDLIST,
307                 "oto", EC_TK_ENDLIST,
308                 "oto");
309         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
310                 "foo", "b", EC_TK_ENDLIST,
311                 "ar", EC_TK_ENDLIST,
312                 "ar");
313         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
314                 "foo", "bar", EC_TK_ENDLIST,
315                 "", EC_TK_ENDLIST,
316                 "");
317         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
318                 "x", EC_TK_ENDLIST,
319                 EC_TK_ENDLIST,
320                 "");
321         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
322                 "foobarx", EC_TK_ENDLIST,
323                 EC_TK_ENDLIST,
324                 "");
325         ec_tk_free(tk);
326
327         return ret;
328 }
329
330 static struct ec_test ec_tk_seq_test = {
331         .name = "tk_seq",
332         .test = ec_tk_seq_testcase,
333 };
334
335 EC_REGISTER_TEST(ec_tk_seq_test);