save
[protos/libecoli.git] / lib / ecoli_tk_or.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_strvec.h>
38 #include <ecoli_tk.h>
39 #include <ecoli_tk_or.h>
40 #include <ecoli_tk_str.h>
41 #include <ecoli_test.h>
42
43 struct ec_tk_or {
44         struct ec_tk gen;
45         struct ec_tk **table;
46         unsigned int len;
47 };
48
49 static struct ec_parsed_tk *ec_tk_or_parse(const struct ec_tk *gen_tk,
50         const struct ec_strvec *strvec)
51 {
52         struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
53         struct ec_parsed_tk *parsed_tk, *child_parsed_tk = NULL;
54         struct ec_strvec *match_strvec;
55         unsigned int i;
56
57         parsed_tk = ec_parsed_tk_new();
58         if (parsed_tk == NULL)
59                 goto fail;
60
61         for (i = 0; i < tk->len; i++) {
62                 child_parsed_tk = ec_tk_parse_tokens(tk->table[i], strvec);
63                 if (child_parsed_tk == NULL)
64                         goto fail;
65                 if (ec_parsed_tk_matches(child_parsed_tk))
66                         break;
67                 ec_parsed_tk_free(child_parsed_tk);
68                 child_parsed_tk = NULL;
69         }
70
71         /* no match */
72         if (i == tk->len)
73                 return parsed_tk;
74
75         match_strvec = ec_strvec_dup(child_parsed_tk->strvec);
76         if (match_strvec == NULL)
77                 goto fail;
78
79         ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
80         ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
81
82         return parsed_tk;
83
84  fail:
85         ec_parsed_tk_free(child_parsed_tk);
86         ec_parsed_tk_free(parsed_tk);
87         return NULL;
88 }
89
90 static struct ec_completed_tk *ec_tk_or_complete(const struct ec_tk *gen_tk,
91         const struct ec_strvec *strvec)
92 {
93         struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
94         struct ec_completed_tk *completed_tk, *child_completed_tk;
95         size_t n;
96
97         completed_tk = ec_completed_tk_new();
98         if (completed_tk == NULL)
99                 return NULL;
100
101         for (n = 0; n < tk->len; n++) {
102                 child_completed_tk = ec_tk_complete_tokens(tk->table[n],
103                         strvec);
104
105                 if (child_completed_tk == NULL) // XXX fail instead?
106                         continue;
107
108                 ec_completed_tk_merge(completed_tk, child_completed_tk);
109         }
110
111         return completed_tk;
112 }
113
114 static void ec_tk_or_free_priv(struct ec_tk *gen_tk)
115 {
116         struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
117         unsigned int i;
118
119         for (i = 0; i < tk->len; i++)
120                 ec_tk_free(tk->table[i]);
121         ec_free(tk->table);
122 }
123
124 static struct ec_tk_ops ec_tk_or_ops = {
125         .typename = "or",
126         .parse = ec_tk_or_parse,
127         .complete = ec_tk_or_complete,
128         .free_priv = ec_tk_or_free_priv,
129 };
130
131 int ec_tk_or_add(struct ec_tk *gen_tk, struct ec_tk *child)
132 {
133         struct ec_tk_or *tk = (struct ec_tk_or *)gen_tk;
134         struct ec_tk **table;
135
136         assert(tk != NULL);
137
138         if (child == NULL)
139                 return -EINVAL;
140
141         gen_tk->flags &= ~EC_TK_F_BUILT;
142
143         table = ec_realloc(tk->table, (tk->len + 1) * sizeof(*tk->table));
144         if (table == NULL) {
145                 ec_tk_free(child);
146                 return -1;
147         }
148
149         tk->table = table;
150         table[tk->len] = child;
151         tk->len++;
152
153         child->parent = gen_tk;
154         TAILQ_INSERT_TAIL(&gen_tk->children, child, next);
155
156         return 0;
157 }
158
159 struct ec_tk *ec_tk_or(const char *id)
160 {
161         struct ec_tk *gen_tk = NULL;
162         struct ec_tk_or *tk = NULL;
163
164         gen_tk = ec_tk_new(id, &ec_tk_or_ops, sizeof(*tk));
165         if (gen_tk == NULL)
166                 return NULL;
167
168         tk = (struct ec_tk_or *)gen_tk;
169         tk->table = NULL;
170         tk->len = 0;
171
172         return gen_tk;
173 }
174
175 struct ec_tk *__ec_tk_or(const char *id, ...)
176 {
177         struct ec_tk *gen_tk = NULL;
178         struct ec_tk_or *tk = NULL;
179         struct ec_tk *child;
180         va_list ap;
181         int fail = 0;
182
183         va_start(ap, id);
184
185         gen_tk = ec_tk_or(id);
186         tk = (struct ec_tk_or *)gen_tk;
187         if (tk == NULL)
188                 fail = 1;;
189
190         for (child = va_arg(ap, struct ec_tk *);
191              child != EC_TK_ENDLIST;
192              child = va_arg(ap, struct ec_tk *)) {
193
194                 /* on error, don't quit the loop to avoid leaks */
195                 if (fail == 1 || child == NULL ||
196                                 ec_tk_or_add(gen_tk, child) < 0) {
197                         fail = 1;
198                         ec_tk_free(child);
199                 }
200         }
201
202         if (fail == 1)
203                 goto fail;
204
205         va_end(ap);
206         return gen_tk;
207
208 fail:
209         ec_tk_free(gen_tk); /* will also free children */
210         va_end(ap);
211         return NULL;
212 }
213
214 static int ec_tk_or_testcase(void)
215 {
216         struct ec_tk *tk;
217         int ret = 0;
218
219         tk = EC_TK_OR(NULL,
220                 ec_tk_str(NULL, "foo"),
221                 ec_tk_str(NULL, "bar")
222         );
223         if (tk == NULL) {
224                 ec_log(EC_LOG_ERR, "cannot create tk\n");
225                 return -1;
226         }
227         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo");
228         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "bar");
229         ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, "foo", "bar");
230         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, " ");
231         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foox");
232         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "toto");
233         ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "");
234         ec_tk_free(tk);
235
236         /* test completion */
237         tk = EC_TK_OR(NULL,
238                 ec_tk_str(NULL, "foo"),
239                 ec_tk_str(NULL, "bar"),
240                 ec_tk_str(NULL, "bar2"),
241                 ec_tk_str(NULL, "toto"),
242                 ec_tk_str(NULL, "titi")
243         );
244         if (tk == NULL) {
245                 ec_log(EC_LOG_ERR, "cannot create tk\n");
246                 return -1;
247         }
248         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
249                 "", EC_TK_ENDLIST,
250                 "foo", "bar", "bar2", "toto", "titi", EC_TK_ENDLIST,
251                 "");
252         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
253                 "f", EC_TK_ENDLIST,
254                 "oo", EC_TK_ENDLIST,
255                 "oo");
256         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
257                 "b", EC_TK_ENDLIST,
258                 "ar", "ar2", EC_TK_ENDLIST,
259                 "ar");
260         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
261                 "bar", EC_TK_ENDLIST,
262                 "", "2", EC_TK_ENDLIST,
263                 "");
264         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
265                 "t", EC_TK_ENDLIST,
266                 "oto", "iti", EC_TK_ENDLIST,
267                 "");
268         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
269                 "to", EC_TK_ENDLIST,
270                 "to", EC_TK_ENDLIST,
271                 "to");
272         ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
273                 "x", EC_TK_ENDLIST,
274                 EC_TK_ENDLIST,
275                 "");
276         ec_tk_free(tk);
277
278         return ret;
279 }
280
281 static struct ec_test ec_tk_or_test = {
282         .name = "tk_or",
283         .test = ec_tk_or_testcase,
284 };
285
286 EC_REGISTER_TEST(ec_tk_or_test);