change add to full str
[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 <stdint.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <stdarg.h>
34 #include <errno.h>
35
36 #include <ecoli_malloc.h>
37 #include <ecoli_log.h>
38 #include <ecoli_test.h>
39 #include <ecoli_strvec.h>
40 #include <ecoli_node.h>
41 #include <ecoli_parsed.h>
42 #include <ecoli_completed.h>
43 #include <ecoli_node_str.h>
44 #include <ecoli_node_option.h>
45 #include <ecoli_node_or.h>
46 #include <ecoli_node_many.h>
47 #include <ecoli_node_seq.h>
48
49 struct ec_node_seq {
50         struct ec_node gen;
51         struct ec_node **table;
52         size_t len;
53 };
54
55 static int
56 ec_node_seq_parse(const struct ec_node *gen_node,
57                 struct ec_parsed *state,
58                 const struct ec_strvec *strvec)
59 {
60         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
61         struct ec_strvec *childvec = NULL;
62         size_t len = 0;
63         unsigned int i;
64         int ret;
65
66         for (i = 0; i < node->len; i++) {
67                 childvec = ec_strvec_ndup(strvec, len,
68                         ec_strvec_len(strvec) - len);
69                 if (childvec == NULL) {
70                         ret = -ENOMEM;
71                         goto fail;
72                 }
73
74                 ret = ec_node_parse_child(node->table[i], state, childvec);
75                 ec_strvec_free(childvec);
76                 childvec = NULL;
77                 if (ret == EC_PARSED_NOMATCH) {
78                         ec_parsed_free_children(state);
79                         return EC_PARSED_NOMATCH;
80                 } else if (ret < 0) {
81                         goto fail;
82                 }
83
84                 len += ret;
85         }
86
87         return len;
88
89 fail:
90         ec_strvec_free(childvec);
91         return ret;
92 }
93
94 static int
95 __ec_node_seq_complete(struct ec_node **table, size_t table_len,
96                 struct ec_completed *completed,
97                 struct ec_parsed *parsed,
98                 const struct ec_strvec *strvec)
99 {
100         struct ec_strvec *childvec = NULL;
101         unsigned int i;
102         int ret;
103
104         if (table_len == 0)
105                 return 0;
106
107         /*
108          * Example of completion for a sequence node = [n1,n2] and an
109          * input = [a,b,c,d]:
110          *
111          * result = complete(n1, [a,b,c,d]) +
112          *    complete(n2, [b,c,d]) if n1 matches [a] +
113          *    complete(n2, [c,d]) if n1 matches [a,b] +
114          *    complete(n2, [d]) if n1 matches [a,b,c] +
115          *    complete(n2, []) if n1 matches [a,b,c,d]
116          */
117
118         /* first, try to complete with the first node of the table */
119         ret = ec_node_complete_child(table[0], completed, parsed, strvec);
120         if (ret < 0)
121                 goto fail;
122
123         /* then, if the first node of the table matches the beginning of the
124          * strvec, try to complete the rest */
125         for (i = 0; i < ec_strvec_len(strvec); i++) {
126                 childvec = ec_strvec_ndup(strvec, 0, i);
127                 if (childvec == NULL)
128                         goto fail;
129
130                 ret = ec_node_parse_child(table[0], parsed, childvec);
131                 if (ret < 0 && ret != EC_PARSED_NOMATCH)
132                         goto fail;
133
134                 ec_strvec_free(childvec);
135                 childvec = NULL;
136
137                 if ((unsigned int)ret != i) {
138                         if (ret != EC_PARSED_NOMATCH)
139                                 ec_parsed_del_last_child(parsed);
140                         continue;
141                 }
142
143                 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
144                 if (childvec == NULL) {
145                         ec_parsed_del_last_child(parsed);
146                         goto fail;
147                 }
148
149                 ret = __ec_node_seq_complete(&table[1],
150                                         table_len - 1,
151                                         completed, parsed, childvec);
152                 ec_parsed_del_last_child(parsed);
153                 ec_strvec_free(childvec);
154                 childvec = NULL;
155
156                 if (ret < 0)
157                         goto fail;
158         }
159
160         return 0;
161
162 fail:
163         ec_strvec_free(childvec);
164         return -1;
165 }
166
167 static int
168 ec_node_seq_complete(const struct ec_node *gen_node,
169                 struct ec_completed *completed,
170                 struct ec_parsed *parsed,
171                 const struct ec_strvec *strvec)
172 {
173         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
174
175         return __ec_node_seq_complete(node->table, node->len, completed,
176                                 parsed, strvec);
177 }
178
179 static size_t ec_node_seq_get_max_parse_len(const struct ec_node *gen_node)
180 {
181         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
182         size_t i, len, ret = 0;
183
184         for (i = 0; i < node->len; i++) {
185                 len = ec_node_get_max_parse_len(node->table[i]);
186                 if (len <= SIZE_MAX - ret)
187                         ret += len;
188                 else
189                         ret = SIZE_MAX;
190         }
191
192         return ret;
193 }
194
195 static void ec_node_seq_free_priv(struct ec_node *gen_node)
196 {
197         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
198         unsigned int i;
199
200         for (i = 0; i < node->len; i++)
201                 ec_node_free(node->table[i]);
202         ec_free(node->table);
203 }
204
205 static struct ec_node_type ec_node_seq_type = {
206         .name = "seq",
207         .parse = ec_node_seq_parse,
208         .complete = ec_node_seq_complete,
209         .get_max_parse_len = ec_node_seq_get_max_parse_len,
210         .size = sizeof(struct ec_node_seq),
211         .free_priv = ec_node_seq_free_priv,
212 };
213
214 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
215
216 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
217 {
218         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
219         struct ec_node **table;
220
221         // XXX check node type
222
223         assert(node != NULL);
224
225         if (child == NULL)
226                 return -EINVAL;
227
228         gen_node->flags &= ~EC_NODE_F_BUILT;
229
230         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
231         if (table == NULL) {
232                 ec_node_free(child);
233                 return -1;
234         }
235
236         node->table = table;
237         table[node->len] = child;
238         node->len++;
239
240         child->parent = gen_node;
241         TAILQ_INSERT_TAIL(&gen_node->children, child, next); // XXX really needed?
242
243         return 0;
244 }
245
246 struct ec_node *__ec_node_seq(const char *id, ...)
247 {
248         struct ec_node *gen_node = NULL;
249         struct ec_node_seq *node = NULL;
250         struct ec_node *child;
251         va_list ap;
252         int fail = 0;
253
254         va_start(ap, id);
255
256         gen_node = __ec_node(&ec_node_seq_type, id);
257         node = (struct ec_node_seq *)gen_node;
258         if (node == NULL)
259                 fail = 1;;
260
261         for (child = va_arg(ap, struct ec_node *);
262              child != EC_NODE_ENDLIST;
263              child = va_arg(ap, struct ec_node *)) {
264
265                 /* on error, don't quit the loop to avoid leaks */
266                 if (fail == 1 || child == NULL ||
267                                 ec_node_seq_add(&node->gen, child) < 0) {
268                         fail = 1;
269                         ec_node_free(child);
270                 }
271         }
272
273         if (fail == 1)
274                 goto fail;
275
276         va_end(ap);
277         return gen_node;
278
279 fail:
280         ec_node_free(gen_node); /* will also free children */
281         va_end(ap);
282         return NULL;
283 }
284
285 /* LCOV_EXCL_START */
286 static int ec_node_seq_testcase(void)
287 {
288         struct ec_node *node;
289         int ret = 0;
290
291         node = EC_NODE_SEQ(NULL,
292                 ec_node_str(NULL, "foo"),
293                 ec_node_str(NULL, "bar")
294         );
295         if (node == NULL) {
296                 ec_log(EC_LOG_ERR, "cannot create node\n");
297                 return -1;
298         }
299         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
300         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
301         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo");
302         ret |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
303         ret |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
304         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
305         ret |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
306         ec_node_free(node);
307
308         /* test completion */
309         node = EC_NODE_SEQ(NULL,
310                 ec_node_str(NULL, "foo"),
311                 ec_node_option(NULL, ec_node_str(NULL, "toto")),
312                 ec_node_str(NULL, "bar")
313         );
314         if (node == NULL) {
315                 ec_log(EC_LOG_ERR, "cannot create node\n");
316                 return -1;
317         }
318         ret |= EC_TEST_CHECK_COMPLETE(node,
319                 "", EC_NODE_ENDLIST,
320                 "foo", EC_NODE_ENDLIST);
321         ret |= EC_TEST_CHECK_COMPLETE(node,
322                 "f", EC_NODE_ENDLIST,
323                 "foo", EC_NODE_ENDLIST);
324         ret |= EC_TEST_CHECK_COMPLETE(node,
325                 "foo", EC_NODE_ENDLIST,
326                 "foo", EC_NODE_ENDLIST);
327         ret |= EC_TEST_CHECK_COMPLETE(node,
328                 "foo", "", EC_NODE_ENDLIST,
329                 "bar", "toto", EC_NODE_ENDLIST);
330         ret |= EC_TEST_CHECK_COMPLETE(node,
331                 "foo", "t", EC_NODE_ENDLIST,
332                 "toto", EC_NODE_ENDLIST);
333         ret |= EC_TEST_CHECK_COMPLETE(node,
334                 "foo", "b", EC_NODE_ENDLIST,
335                 "bar", EC_NODE_ENDLIST);
336         ret |= EC_TEST_CHECK_COMPLETE(node,
337                 "foo", "bar", EC_NODE_ENDLIST,
338                 "bar", EC_NODE_ENDLIST);
339         ret |= EC_TEST_CHECK_COMPLETE(node,
340                 "x", EC_NODE_ENDLIST,
341                 EC_NODE_ENDLIST);
342         ret |= EC_TEST_CHECK_COMPLETE(node,
343                 "foobarx", EC_NODE_ENDLIST,
344                 EC_NODE_ENDLIST);
345         ec_node_free(node);
346
347         return ret;
348 }
349 /* LCOV_EXCL_STOP */
350
351 static struct ec_test ec_node_seq_test = {
352         .name = "node_seq",
353         .test = ec_node_seq_testcase,
354 };
355
356 EC_TEST_REGISTER(ec_node_seq_test);