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