save
[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 int
104 __ec_node_many_complete(struct ec_node_many *node, unsigned int max,
105                         struct ec_completed *completed,
106                         struct ec_parsed *parsed,
107                         const struct ec_strvec *strvec)
108 {
109         struct ec_strvec *childvec = NULL;
110         unsigned int i;
111         int ret;
112
113         /* first, try to complete with the child node */
114         ret = ec_node_complete_child(node->child, completed, parsed, strvec);
115         if (ret < 0)
116                 goto fail;
117
118         /* we're done, we reached the max number of nodes */
119         if (max == 1)
120                 return 0;
121
122         /* if there is a maximum, decrease it before recursion */
123         if (max != 0)
124                 max--;
125
126         /* then, if the node matches the beginning of the strvec, try to
127          * complete the rest */
128         for (i = 0; i < ec_strvec_len(strvec); i++) {
129                 childvec = ec_strvec_ndup(strvec, 0, i);
130                 if (childvec == NULL)
131                         goto fail;
132
133                 ret = ec_node_parse_child(node->child, parsed, childvec);
134                 if (ret < 0 && ret != EC_PARSED_NOMATCH)
135                         goto fail;
136
137                 ec_strvec_free(childvec);
138                 childvec = NULL;
139
140                 if ((unsigned int)ret != i) {
141                         if (ret != EC_PARSED_NOMATCH)
142                                 ec_parsed_del_last_child(parsed);
143                         continue;
144                 }
145
146                 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
147                 if (childvec == NULL) {
148                         ec_parsed_del_last_child(parsed);
149                         goto fail;
150                 }
151
152                 ret = __ec_node_many_complete(node, max, completed,
153                                         parsed, childvec);
154                 ec_parsed_del_last_child(parsed);
155                 ec_strvec_free(childvec);
156                 childvec = NULL;
157
158                 if (ret < 0)
159                         goto fail;
160         }
161
162         return 0;
163
164 fail:
165         ec_strvec_free(childvec);
166         return -1;
167 }
168
169 static int
170 ec_node_many_complete(const struct ec_node *gen_node,
171                 struct ec_completed *completed,
172                 struct ec_parsed *parsed,
173                 const struct ec_strvec *strvec)
174 {
175         struct ec_node_many *node = (struct ec_node_many *)gen_node;
176
177         return __ec_node_many_complete(node, node->max, completed,
178                                 parsed, strvec);
179 }
180
181 static void ec_node_many_free_priv(struct ec_node *gen_node)
182 {
183         struct ec_node_many *node = (struct ec_node_many *)gen_node;
184
185         ec_node_free(node->child);
186 }
187
188 static struct ec_node_type ec_node_many_type = {
189         .name = "many",
190         .parse = ec_node_many_parse,
191         .complete = ec_node_many_complete,
192         .size = sizeof(struct ec_node_many),
193         .free_priv = ec_node_many_free_priv,
194 };
195
196 EC_NODE_TYPE_REGISTER(ec_node_many_type);
197
198 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
199         unsigned int min, unsigned int max)
200 {
201         struct ec_node_many *node = NULL;
202
203         if (child == NULL)
204                 return NULL;
205
206         node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
207         if (node == NULL) {
208                 ec_node_free(child);
209                 return NULL;
210         }
211
212         node->child = child;
213         node->min = min;
214         node->max = max;
215
216         return &node->gen;
217 }
218
219 /* LCOV_EXCL_START */
220 static int ec_node_many_testcase(void)
221 {
222         struct ec_node *node;
223         int ret = 0;
224
225         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 0, 0);
226         if (node == NULL) {
227                 ec_log(EC_LOG_ERR, "cannot create node\n");
228                 return -1;
229         }
230         ret |= EC_TEST_CHECK_PARSE(node, 0);
231         ret |= EC_TEST_CHECK_PARSE(node, 0, "bar");
232         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
233         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
234         ret |= EC_TEST_CHECK_PARSE(node, 0);
235         ec_node_free(node);
236
237         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 0);
238         if (node == NULL) {
239                 ec_log(EC_LOG_ERR, "cannot create node\n");
240                 return -1;
241         }
242         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
243         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
244         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
245         ret |= EC_TEST_CHECK_PARSE(node, -1);
246         ec_node_free(node);
247
248         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 2);
249         if (node == NULL) {
250                 ec_log(EC_LOG_ERR, "cannot create node\n");
251                 return -1;
252         }
253         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
254         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
255         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
256         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
257         ret |= EC_TEST_CHECK_PARSE(node, -1);
258         ec_node_free(node);
259
260         /* test completion */
261         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 2, 4);
262         if (node == NULL) {
263                 ec_log(EC_LOG_ERR, "cannot create node\n");
264                 return -1;
265         }
266         ret |= EC_TEST_CHECK_COMPLETE(node,
267                 "", EC_NODE_ENDLIST,
268                 "foo", EC_NODE_ENDLIST,
269                 "foo");
270         ret |= EC_TEST_CHECK_COMPLETE(node,
271                 "f", EC_NODE_ENDLIST,
272                 "oo", EC_NODE_ENDLIST,
273                 "oo");
274         ret |= EC_TEST_CHECK_COMPLETE(node,
275                 "foo", EC_NODE_ENDLIST,
276                 "", EC_NODE_ENDLIST,
277                 "");
278         ret |= EC_TEST_CHECK_COMPLETE(node,
279                 "foo", "", EC_NODE_ENDLIST,
280                 "foo", EC_NODE_ENDLIST,
281                 "foo");
282         ret |= EC_TEST_CHECK_COMPLETE(node,
283                 "foo", "foo", "", EC_NODE_ENDLIST,
284                 "foo", EC_NODE_ENDLIST,
285                 "foo");
286         ret |= EC_TEST_CHECK_COMPLETE(node,
287                 "foo", "foo", "foo", "", EC_NODE_ENDLIST,
288                 "foo", EC_NODE_ENDLIST,
289                 "foo");
290         ret |= EC_TEST_CHECK_COMPLETE(node,
291                 "foo", "foo", "foo", "foo", "", EC_NODE_ENDLIST,
292                 EC_NODE_ENDLIST,
293                 "");
294         ec_node_free(node);
295
296         return ret;
297 }
298 /* LCOV_EXCL_STOP */
299
300 static struct ec_test ec_node_many_test = {
301         .name = "node_many",
302         .test = ec_node_many_testcase,
303 };
304
305 EC_TEST_REGISTER(ec_node_many_test);