pass state to completed api
[protos/libecoli.git] / lib / ecoli_node_many.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_parsed.h>
41 #include <ecoli_completed.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_node_option.h>
44 #include <ecoli_node_many.h>
45
46 struct ec_node_many {
47         struct ec_node gen;
48         unsigned int min;
49         unsigned int max;
50         struct ec_node *child;
51 };
52
53 static int ec_node_many_parse(const struct ec_node *gen_node,
54                         struct ec_parsed *state,
55                         const struct ec_strvec *strvec)
56 {
57         struct ec_node_many *node = (struct ec_node_many *)gen_node;
58         struct ec_parsed *child_parsed;
59         struct ec_strvec *childvec = NULL;
60         size_t off = 0, count;
61         int ret;
62
63         for (count = 0; node->max == 0 || count < node->max; count++) {
64                 childvec = ec_strvec_ndup(strvec, off,
65                         ec_strvec_len(strvec) - off);
66                 if (childvec == NULL) {
67                         ret = -ENOMEM;
68                         goto fail;
69                 }
70
71                 ret = ec_node_parse_child(node->child, state, childvec);
72                 ec_strvec_free(childvec);
73                 childvec = NULL;
74
75                 if (ret == EC_PARSED_NOMATCH)
76                         break;
77                 else if (ret < 0)
78                         goto fail;
79
80                 /* it matches an empty strvec, no need to continue */
81                 if (ret == 0) {
82                         child_parsed = ec_parsed_get_last_child(state);
83                         ec_parsed_del_child(state, child_parsed);
84                         ec_parsed_free(child_parsed);
85                         break;
86                 }
87
88                 off += ret;
89         }
90
91         if (count < node->min) {
92                 ec_parsed_free_children(state);
93                 return EC_PARSED_NOMATCH;
94         }
95
96         return off;
97
98 fail:
99         ec_strvec_free(childvec);
100         return ret;
101 }
102
103 static struct ec_completed *
104 __ec_node_many_complete(struct ec_node_many *node, unsigned int max,
105                         struct ec_parsed *state, const struct ec_strvec *strvec)
106 {
107         struct ec_completed *completed, *child_completed;
108         struct ec_strvec *childvec = NULL;
109         unsigned int i;
110         int ret;
111
112         completed = ec_completed();
113         if (completed == NULL)
114                 return NULL;
115
116         /* first, try to complete with the child node */
117         child_completed = ec_node_complete_child(node->child, state, strvec);
118         if (child_completed == NULL)
119                 goto fail;
120         ec_completed_merge(completed, child_completed);
121         child_completed = NULL;
122
123         /* we're done, we reached the max number of nodes */
124         if (max == 1)
125                 return completed;
126
127         /* if there is a maximum, decrease it before recursion */
128         if (max != 0)
129                 max--;
130
131         /* then, if the node matches the beginning of the strvec, try to
132          * complete the rest */
133         for (i = 0; i < ec_strvec_len(strvec); i++) {
134                 childvec = ec_strvec_ndup(strvec, 0, i);
135                 if (childvec == NULL)
136                         goto fail;
137
138                 ret = ec_node_parse_child(node->child, state, childvec);
139                 if (ret < 0 && ret != EC_PARSED_NOMATCH)
140                         goto fail;
141
142                 ec_strvec_free(childvec);
143                 childvec = NULL;
144
145                 if ((unsigned int)ret != i) {
146                         if (ret != EC_PARSED_NOMATCH)
147                                 ec_parsed_del_last_child(state);
148                         continue;
149                 }
150
151                 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
152                 if (childvec == NULL) {
153                         ec_parsed_del_last_child(state);
154                         goto fail;
155                 }
156
157                 child_completed = __ec_node_many_complete(node, max,
158                                                         state, childvec);
159                 ec_parsed_del_last_child(state);
160                 ec_strvec_free(childvec);
161                 childvec = NULL;
162
163                 if (child_completed == NULL)
164                         goto fail;
165
166                 ec_completed_merge(completed, child_completed);
167                 child_completed = NULL;
168         }
169
170         return completed;
171
172 fail:
173         ec_strvec_free(childvec);
174         ec_completed_free(child_completed);
175         ec_completed_free(completed);
176         return NULL;
177 }
178
179 static struct ec_completed *
180 ec_node_many_complete(const struct ec_node *gen_node,
181                 struct ec_parsed *state,
182                 const struct ec_strvec *strvec)
183 {
184         struct ec_node_many *node = (struct ec_node_many *)gen_node;
185
186         return __ec_node_many_complete(node, node->max, state, strvec);
187 }
188
189 static void ec_node_many_free_priv(struct ec_node *gen_node)
190 {
191         struct ec_node_many *node = (struct ec_node_many *)gen_node;
192
193         ec_node_free(node->child);
194 }
195
196 static struct ec_node_type ec_node_many_type = {
197         .name = "many",
198         .parse = ec_node_many_parse,
199         .complete = ec_node_many_complete,
200         .size = sizeof(struct ec_node_many),
201         .free_priv = ec_node_many_free_priv,
202 };
203
204 EC_NODE_TYPE_REGISTER(ec_node_many_type);
205
206 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
207         unsigned int min, unsigned int max)
208 {
209         struct ec_node_many *node = NULL;
210
211         if (child == NULL)
212                 return NULL;
213
214         node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
215         if (node == NULL) {
216                 ec_node_free(child);
217                 return NULL;
218         }
219
220         node->child = child;
221         node->min = min;
222         node->max = max;
223
224         return &node->gen;
225 }
226
227 static int ec_node_many_testcase(void)
228 {
229         struct ec_node *node;
230         int ret = 0;
231
232         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 0, 0);
233         if (node == NULL) {
234                 ec_log(EC_LOG_ERR, "cannot create node\n");
235                 return -1;
236         }
237         ret |= EC_TEST_CHECK_PARSE(node, 0);
238         ret |= EC_TEST_CHECK_PARSE(node, 0, "bar");
239         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
240         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
241         ret |= EC_TEST_CHECK_PARSE(node, 0);
242         ec_node_free(node);
243
244         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 0);
245         if (node == NULL) {
246                 ec_log(EC_LOG_ERR, "cannot create node\n");
247                 return -1;
248         }
249         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
250         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
251         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
252         ret |= EC_TEST_CHECK_PARSE(node, -1);
253         ec_node_free(node);
254
255         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 2);
256         if (node == NULL) {
257                 ec_log(EC_LOG_ERR, "cannot create node\n");
258                 return -1;
259         }
260         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
261         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
262         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
263         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
264         ret |= EC_TEST_CHECK_PARSE(node, -1);
265         ec_node_free(node);
266
267         /* test completion */
268         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 2, 4);
269         if (node == NULL) {
270                 ec_log(EC_LOG_ERR, "cannot create node\n");
271                 return -1;
272         }
273         ret |= EC_TEST_CHECK_COMPLETE(node,
274                 "", EC_NODE_ENDLIST,
275                 "foo", EC_NODE_ENDLIST,
276                 "foo");
277         ret |= EC_TEST_CHECK_COMPLETE(node,
278                 "f", EC_NODE_ENDLIST,
279                 "oo", EC_NODE_ENDLIST,
280                 "oo");
281         ret |= EC_TEST_CHECK_COMPLETE(node,
282                 "foo", EC_NODE_ENDLIST,
283                 "", EC_NODE_ENDLIST,
284                 "");
285         ret |= EC_TEST_CHECK_COMPLETE(node,
286                 "foo", "", EC_NODE_ENDLIST,
287                 "foo", EC_NODE_ENDLIST,
288                 "foo");
289         ret |= EC_TEST_CHECK_COMPLETE(node,
290                 "foo", "foo", "", EC_NODE_ENDLIST,
291                 "foo", EC_NODE_ENDLIST,
292                 "foo");
293         ret |= EC_TEST_CHECK_COMPLETE(node,
294                 "foo", "foo", "foo", "", EC_NODE_ENDLIST,
295                 "foo", EC_NODE_ENDLIST,
296                 "foo");
297         ret |= EC_TEST_CHECK_COMPLETE(node,
298                 "foo", "foo", "foo", "foo", "", EC_NODE_ENDLIST,
299                 EC_NODE_ENDLIST,
300                 "");
301         ec_node_free(node);
302
303         return ret;
304 }
305
306 static struct ec_test ec_node_many_test = {
307         .name = "node_many",
308         .test = ec_node_many_testcase,
309 };
310
311 EC_TEST_REGISTER(ec_node_many_test);