save
[protos/libecoli.git] / lib / ecoli_node_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_node.h>
40 #include <ecoli_node_str.h>
41 #include <ecoli_node_option.h>
42 #include <ecoli_node_seq.h>
43
44 struct ec_node_seq {
45         struct ec_node gen;
46         struct ec_node **table;
47         unsigned int len;
48 };
49
50 static struct ec_parsed *ec_node_seq_parse(const struct ec_node *gen_node,
51         const struct ec_strvec *strvec)
52 {
53         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
54         struct ec_parsed *parsed, *child_parsed;
55         struct ec_strvec *match_strvec;
56         struct ec_strvec *childvec = NULL;
57         size_t len = 0;
58         unsigned int i;
59
60         parsed = ec_parsed_new();
61         if (parsed == NULL)
62                 goto fail;
63
64         for (i = 0; i < node->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 = ec_node_parse_strvec(node->table[i], childvec);
71                 if (child_parsed == NULL)
72                         goto fail;
73
74                 ec_strvec_free(childvec);
75                 childvec = NULL;
76
77                 if (!ec_parsed_matches(child_parsed)) {
78                         ec_parsed_free(child_parsed);
79                         // XXX ec_parsed_free_children needed? see subset.c
80                         ec_parsed_free_children(parsed);
81                         return parsed;
82                 }
83
84                 ec_parsed_add_child(parsed, child_parsed);
85                 len += ec_parsed_len(child_parsed);
86         }
87
88         match_strvec = ec_strvec_ndup(strvec, 0, len);
89         if (match_strvec == NULL)
90                 goto fail;
91
92         ec_parsed_set_match(parsed, gen_node, match_strvec);
93
94         return parsed;
95
96 fail:
97         ec_strvec_free(childvec);
98         ec_parsed_free(parsed);
99         return NULL;
100 }
101
102 static struct ec_completed *ec_node_seq_complete(const struct ec_node *gen_node,
103         const struct ec_strvec *strvec)
104 {
105         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
106         struct ec_completed *completed, *child_completed;
107         struct ec_strvec *childvec = NULL;
108         struct ec_parsed *parsed;
109         size_t len = 0;
110         unsigned int i;
111
112         completed = ec_completed_new();
113         if (completed == NULL)
114                 return NULL;
115
116         if (node->len == 0)
117                 return completed;
118
119         for (i = 0; i < node->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 = ec_node_complete_strvec(node->table[i],
126                         childvec);
127                 if (child_completed == NULL)
128                         goto fail;
129
130                 ec_completed_merge(completed, child_completed);
131
132                 parsed = ec_node_parse_strvec(node->table[i], childvec);
133                 if (parsed == NULL)
134                         goto fail;
135
136                 ec_strvec_free(childvec);
137                 childvec = NULL;
138
139                 if (!ec_parsed_matches(parsed)) {
140                         ec_parsed_free(parsed);
141                         break;
142                 }
143
144                 len += ec_strvec_len(parsed->strvec);
145                 ec_parsed_free(parsed);
146         }
147
148         return completed;
149
150 fail:
151         ec_strvec_free(childvec);
152         ec_completed_free(completed);
153         return NULL;
154 }
155
156 static void ec_node_seq_free_priv(struct ec_node *gen_node)
157 {
158         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
159         unsigned int i;
160
161         for (i = 0; i < node->len; i++)
162                 ec_node_free(node->table[i]);
163         ec_free(node->table);
164 }
165
166 static struct ec_node_type ec_node_seq_type = {
167         .name = "seq",
168         .parse = ec_node_seq_parse,
169         .complete = ec_node_seq_complete,
170         .size = sizeof(struct ec_node_seq),
171         .free_priv = ec_node_seq_free_priv,
172 };
173
174 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
175
176 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
177 {
178         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
179         struct ec_node **table;
180
181         // XXX check node type
182
183         assert(node != NULL);
184
185         if (child == NULL)
186                 return -EINVAL;
187
188         gen_node->flags &= ~EC_NODE_F_BUILT;
189
190         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
191         if (table == NULL) {
192                 ec_node_free(child);
193                 return -1;
194         }
195
196         node->table = table;
197         table[node->len] = child;
198         node->len++;
199
200         child->parent = gen_node;
201         TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
202
203         return 0;
204 }
205
206 struct ec_node *__ec_node_seq(const char *id, ...)
207 {
208         struct ec_node *gen_node = NULL;
209         struct ec_node_seq *node = NULL;
210         struct ec_node *child;
211         va_list ap;
212         int fail = 0;
213
214         va_start(ap, id);
215
216         gen_node = __ec_node_new(&ec_node_seq_type, id);
217         node = (struct ec_node_seq *)gen_node;
218         if (node == NULL)
219                 fail = 1;;
220
221         for (child = va_arg(ap, struct ec_node *);
222              child != EC_NODE_ENDLIST;
223              child = va_arg(ap, struct ec_node *)) {
224
225                 /* on error, don't quit the loop to avoid leaks */
226                 if (fail == 1 || child == NULL ||
227                                 ec_node_seq_add(&node->gen, child) < 0) {
228                         fail = 1;
229                         ec_node_free(child);
230                 }
231         }
232
233         if (fail == 1)
234                 goto fail;
235
236         va_end(ap);
237         return gen_node;
238
239 fail:
240         ec_node_free(gen_node); /* will also free children */
241         va_end(ap);
242         return NULL;
243 }
244
245 static int ec_node_seq_testcase(void)
246 {
247         struct ec_node *node;
248         int ret = 0;
249
250         node = EC_NODE_SEQ(NULL,
251                 ec_node_str(NULL, "foo"),
252                 ec_node_str(NULL, "bar")
253         );
254         if (node == NULL) {
255                 ec_log(EC_LOG_ERR, "cannot create node\n");
256                 return -1;
257         }
258         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
259         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
260         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
261         ret |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
262         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
263         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
264         ret |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
265         ec_node_free(node);
266
267         /* test completion */
268         node = EC_NODE_SEQ(NULL,
269                 ec_node_str(NULL, "foo"),
270                 ec_node_option(NULL, ec_node_str(NULL, "toto")),
271                 ec_node_str(NULL, "bar")
272         );
273         if (node == NULL) {
274                 ec_log(EC_LOG_ERR, "cannot create node\n");
275                 return -1;
276         }
277         ret |= EC_TEST_CHECK_COMPLETE(node,
278                 "", EC_NODE_ENDLIST,
279                 "foo", EC_NODE_ENDLIST,
280                 "foo");
281         ret |= EC_TEST_CHECK_COMPLETE(node,
282                 "f", EC_NODE_ENDLIST,
283                 "oo", EC_NODE_ENDLIST,
284                 "oo");
285         ret |= EC_TEST_CHECK_COMPLETE(node,
286                 "foo", EC_NODE_ENDLIST,
287                 "", EC_NODE_ENDLIST,
288                 "");
289         ret |= EC_TEST_CHECK_COMPLETE(node,
290                 "foo", "", EC_NODE_ENDLIST,
291                 "bar", "toto", EC_NODE_ENDLIST,
292                 "");
293         ret |= EC_TEST_CHECK_COMPLETE(node,
294                 "foo", "t", EC_NODE_ENDLIST,
295                 "oto", EC_NODE_ENDLIST,
296                 "oto");
297         ret |= EC_TEST_CHECK_COMPLETE(node,
298                 "foo", "b", EC_NODE_ENDLIST,
299                 "ar", EC_NODE_ENDLIST,
300                 "ar");
301         ret |= EC_TEST_CHECK_COMPLETE(node,
302                 "foo", "bar", EC_NODE_ENDLIST,
303                 "", EC_NODE_ENDLIST,
304                 "");
305         ret |= EC_TEST_CHECK_COMPLETE(node,
306                 "x", EC_NODE_ENDLIST,
307                 EC_NODE_ENDLIST,
308                 "");
309         ret |= EC_TEST_CHECK_COMPLETE(node,
310                 "foobarx", EC_NODE_ENDLIST,
311                 EC_NODE_ENDLIST,
312                 "");
313         ec_node_free(node);
314
315         return ret;
316 }
317
318 static struct ec_test ec_node_seq_test = {
319         .name = "node_seq",
320         .test = ec_node_seq_testcase,
321 };
322
323 EC_TEST_REGISTER(ec_node_seq_test);