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 #if 0 //XXX missing node_many_complete
104 static struct ec_completed *ec_node_many_complete(const struct ec_node *gen_node,
105         const struct ec_strvec *strvec)
106 {
107         struct ec_node_many *node = (struct ec_node_many *)gen_node;
108         struct ec_completed *completed, *child_completed;
109         struct ec_strvec *childvec;
110         struct ec_parsed *parsed;
111         size_t len = 0;
112         unsigned int i;
113
114         completed = ec_completed();
115         if (completed == NULL)
116                 return NULL;
117
118         if (node->len == 0)
119                 return completed;
120
121         for (i = 0; i < node->len; i++) {
122                 childvec = ec_strvec_ndup(strvec, len,
123                         ec_strvec_len(strvec) - len);
124                 if (childvec == NULL)
125                         goto fail; // XXX fail ?
126
127                 child_completed = ec_node_complete_strvec(node->table[i],
128                         childvec);
129                 if (child_completed == NULL)
130                         goto fail;
131
132                 ec_completed_merge(completed, child_completed);
133
134                 parsed = ec_node_parse_strvec(node->table[i], childvec);
135                 if (parsed == NULL)
136                         goto fail;
137
138                 ec_strvec_free(childvec);
139                 childvec = NULL;
140
141                 if (!ec_parsed_matches(parsed)) {
142                         ec_parsed_free(parsed);
143                         break;
144                 }
145
146                 len += ec_strvec_len(parsed->strvec);
147                 ec_parsed_free(parsed);
148         }
149
150         return completed;
151
152 fail:
153         ec_strvec_free(childvec);
154         ec_completed_free(completed);
155         return NULL;
156 }
157 #endif
158
159 static void ec_node_many_free_priv(struct ec_node *gen_node)
160 {
161         struct ec_node_many *node = (struct ec_node_many *)gen_node;
162
163         ec_node_free(node->child);
164 }
165
166 static struct ec_node_type ec_node_many_type = {
167         .name = "many",
168         .parse = ec_node_many_parse,
169         .complete = ec_node_default_complete,
170 //XXX   .complete = ec_node_many_complete,
171         .size = sizeof(struct ec_node_many),
172         .free_priv = ec_node_many_free_priv,
173 };
174
175 EC_NODE_TYPE_REGISTER(ec_node_many_type);
176
177 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
178         unsigned int min, unsigned int max)
179 {
180         struct ec_node_many *node = NULL;
181
182         if (child == NULL)
183                 return NULL;
184
185         node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
186         if (node == NULL) {
187                 ec_node_free(child);
188                 return NULL;
189         }
190
191         node->child = child;
192         node->min = min;
193         node->max = max;
194
195         return &node->gen;
196 }
197
198 static int ec_node_many_testcase(void)
199 {
200         struct ec_node *node;
201         int ret = 0;
202
203         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 0, 0);
204         if (node == NULL) {
205                 ec_log(EC_LOG_ERR, "cannot create node\n");
206                 return -1;
207         }
208         ret |= EC_TEST_CHECK_PARSE(node, 0);
209         ret |= EC_TEST_CHECK_PARSE(node, 0, "bar");
210         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
211         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
212         ret |= EC_TEST_CHECK_PARSE(node, 0);
213         ec_node_free(node);
214
215         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 0);
216         if (node == NULL) {
217                 ec_log(EC_LOG_ERR, "cannot create node\n");
218                 return -1;
219         }
220         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
221         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
222         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
223         ret |= EC_TEST_CHECK_PARSE(node, -1);
224         ec_node_free(node);
225
226         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 2);
227         if (node == NULL) {
228                 ec_log(EC_LOG_ERR, "cannot create node\n");
229                 return -1;
230         }
231         ret |= EC_TEST_CHECK_PARSE(node, -1, "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, 2, "foo", "foo", "foo");
235         ret |= EC_TEST_CHECK_PARSE(node, -1);
236         ec_node_free(node);
237
238         /* test completion */
239         /* XXX */
240
241         return ret;
242 }
243
244 static struct ec_test ec_node_many_test = {
245         .name = "many",
246         .test = ec_node_many_testcase,
247 };
248
249 EC_TEST_REGISTER(ec_node_many_test);