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                         // XXX ec_parsed_tk_free_children needed? see subset.c
80                         ec_parsed_tk_free_children(parsed_tk);
81                         return parsed_tk;
82                 }
83
84                 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
85                 len += ec_parsed_tk_len(child_parsed_tk);
86         }
87
88         match_strvec = ec_strvec_ndup(strvec, 0, len);
89         if (match_strvec == NULL)
90                 goto fail;
91
92         ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
93
94         return parsed_tk;
95
96 fail:
97         ec_strvec_free(childvec);
98         ec_parsed_tk_free(parsed_tk);
99         return NULL;
100 }
101
102 static struct ec_completed_tk *ec_tk_seq_complete(const struct ec_tk *gen_tk,
103         const struct ec_strvec *strvec)
104 {
105         struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
106         struct ec_completed_tk *completed_tk, *child_completed_tk;
107         struct ec_strvec *childvec = NULL;
108         struct ec_parsed_tk *parsed_tk;
109         size_t len = 0;
110         unsigned int i;
111
112         completed_tk = ec_completed_tk_new();
113         if (completed_tk == NULL)
114                 return NULL;
115
116         if (tk->len == 0)
117                 return completed_tk;
118
119         for (i = 0; i < tk->len && len < ec_strvec_len(strvec); i++) {
120                 childvec = ec_strvec_ndup(strvec, len,
121                         ec_strvec_len(strvec) - len);
122                 if (childvec == NULL)
123                         goto fail;
124
125                 child_completed_tk = ec_tk_complete_tokens(tk->table[i],
126                         childvec);
127                 if (child_completed_tk == NULL)
128                         goto fail;
129
130                 ec_completed_tk_merge(completed_tk, child_completed_tk);
131
132                 parsed_tk = ec_tk_parse_tokens(tk->table[i], childvec);
133                 if (parsed_tk == NULL)
134                         goto fail;
135
136                 ec_strvec_free(childvec);
137                 childvec = NULL;
138
139                 if (!ec_parsed_tk_matches(parsed_tk)) {
140                         ec_parsed_tk_free(parsed_tk);
141                         break;
142                 }
143
144                 len += ec_strvec_len(parsed_tk->strvec);
145                 ec_parsed_tk_free(parsed_tk);
146         }
147
148         return completed_tk;
149
150 fail:
151         ec_strvec_free(childvec);
152         ec_completed_tk_free(completed_tk);
153         return NULL;
154 }
155
156 static void ec_tk_seq_free_priv(struct ec_tk *gen_tk)
157 {
158         struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
159         unsigned int i;
160
161         for (i = 0; i < tk->len; i++)
162                 ec_tk_free(tk->table[i]);
163         ec_free(tk->table);
164 }
165
166 static struct ec_tk_type ec_tk_seq_type = {
167         .name = "seq",
168         .parse = ec_tk_seq_parse,
169         .complete = ec_tk_seq_complete,
170         .free_priv = ec_tk_seq_free_priv,
171 };
172
173 EC_TK_TYPE_REGISTER(ec_tk_seq_type);
174
175 int ec_tk_seq_add(struct ec_tk *gen_tk, struct ec_tk *child)
176 {
177         struct ec_tk_seq *tk = (struct ec_tk_seq *)gen_tk;
178         struct ec_tk **table;
179
180         // XXX check tk type
181
182         assert(tk != NULL);
183
184         if (child == NULL)
185                 return -EINVAL;
186
187         gen_tk->flags &= ~EC_TK_F_BUILT;
188
189         table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
190         if (table == NULL) {
191                 ec_tk_free(child);
192                 return -1;
193         }
194
195         tk->table = table;
196         table[tk->len] = child;
197         tk->len++;
198
199         child->parent = gen_tk;
200         TAILQ_INSERT_TAIL(&gen_tk->children, child, next); // XXX really needed?
201
202         return 0;
203 }
204
205 struct ec_tk *ec_tk_seq(const char *id)
206 {
207         struct ec_tk *gen_tk = NULL;
208         struct ec_tk_seq *tk = NULL;
209
210         gen_tk = ec_tk_new(id, &ec_tk_seq_type, sizeof(*tk));
211         if (gen_tk == NULL)
212                 return NULL;
213
214         tk = (struct ec_tk_seq *)gen_tk;
215         tk->table = NULL;
216         tk->len = 0;
217
218         return gen_tk;
219 }
220
221 struct ec_tk *__ec_tk_seq(const char *id, ...)
222 {
223         struct ec_tk *gen_tk = NULL;
224         struct ec_tk_seq *tk = NULL;
225         struct ec_tk *child;
226         va_list ap;
227         int fail = 0;
228
229         va_start(ap, id);
230
231         gen_tk = ec_tk_seq(id);
232         tk = (struct ec_tk_seq *)gen_tk;
233         if (tk == NULL)
234                 fail = 1;;
235
236         for (child = va_arg(ap, struct ec_tk *);
237              child != EC_TK_ENDLIST;
238              child = va_arg(ap, struct ec_tk *)) {
239
240                 /* on error, don't quit the loop to avoid leaks */
241                 if (fail == 1 || child == NULL ||
242                                 ec_tk_seq_add(&tk->gen, child) < 0) {
243                         fail = 1;
244                         ec_tk_free(child);
245                 }
246         }
247
248         if (fail == 1)
249                 goto fail;
250
251         va_end(ap);
252         return gen_tk;
253
254 fail:
255         ec_tk_free(gen_tk); /* will also free children */
256         va_end(ap);
257         return NULL;
258 }
259
260 static int ec_tk_seq_testcase(void)
261 {
262         struct ec_tk *tk;
263         int ret = 0;
264
265         tk = EC_TK_SEQ(NULL,
266                 ec_tk_str(NULL, "foo"),
267                 ec_tk_str(NULL, "bar")
268         );
269         if (tk == NULL) {
270                 ec_log(EC_LOG_ERR, "cannot create tk\n");
271                 return -1;
272         }
273         ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar");
274         ret |= EC_TEST_CHECK_TK_PARSE(tk, 2, "foo", "bar", "toto");
275         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo");
276         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foox", "bar");
277         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo", "barx");
278         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "bar", "foo");
279         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "", "foo");
280         ec_tk_free(tk);
281
282         /* test completion */
283         tk = EC_TK_SEQ(NULL,
284                 ec_tk_str(NULL, "foo"),
285                 ec_tk_option_new(NULL, ec_tk_str(NULL, "toto")),
286                 ec_tk_str(NULL, "bar")
287         );
288         if (tk == NULL) {
289                 ec_log(EC_LOG_ERR, "cannot create tk\n");
290                 return -1;
291         }
292         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
293                 "", EC_TK_ENDLIST,
294                 "foo", EC_TK_ENDLIST,
295                 "foo");
296         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
297                 "f", EC_TK_ENDLIST,
298                 "oo", EC_TK_ENDLIST,
299                 "oo");
300         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
301                 "foo", EC_TK_ENDLIST,
302                 "", EC_TK_ENDLIST,
303                 "");
304         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
305                 "foo", "", EC_TK_ENDLIST,
306                 "bar", "toto", EC_TK_ENDLIST,
307                 "");
308         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
309                 "foo", "t", EC_TK_ENDLIST,
310                 "oto", EC_TK_ENDLIST,
311                 "oto");
312         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
313                 "foo", "b", EC_TK_ENDLIST,
314                 "ar", EC_TK_ENDLIST,
315                 "ar");
316         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
317                 "foo", "bar", EC_TK_ENDLIST,
318                 "", EC_TK_ENDLIST,
319                 "");
320         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
321                 "x", EC_TK_ENDLIST,
322                 EC_TK_ENDLIST,
323                 "");
324         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
325                 "foobarx", EC_TK_ENDLIST,
326                 EC_TK_ENDLIST,
327                 "");
328         ec_tk_free(tk);
329
330         return ret;
331 }
332
333 static struct ec_test ec_tk_seq_test = {
334         .name = "tk_seq",
335         .test = ec_tk_seq_testcase,
336 };
337
338 EC_TEST_REGISTER(ec_tk_seq_test);