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
34 #include <ecoli_malloc.h>
35 #include <ecoli_log.h>
36 #include <ecoli_test.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_node.h>
39 #include <ecoli_node_str.h>
40 #include <ecoli_node_option.h>
41 #include <ecoli_node_many.h>
42
43 struct ec_node_many {
44         struct ec_node gen;
45         unsigned int min;
46         unsigned int max;
47         struct ec_node *child;
48 };
49
50 static struct ec_parsed *ec_node_many_parse(const struct ec_node *gen_node,
51         const struct ec_strvec *strvec)
52 {
53         struct ec_node_many *node = (struct ec_node_many *)gen_node;
54         struct ec_parsed *parsed, *child_parsed;
55         struct ec_strvec *match_strvec;
56         struct ec_strvec *childvec = NULL;
57         size_t off = 0, len, count;
58
59         parsed = ec_parsed();
60         if (parsed == NULL)
61                 goto fail;
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                         goto fail;
68
69                 child_parsed = ec_node_parse_strvec(node->child, childvec);
70                 if (child_parsed == NULL)
71                         goto fail;
72
73                 ec_strvec_free(childvec);
74                 childvec = NULL;
75
76                 if (!ec_parsed_matches(child_parsed)) {
77                         ec_parsed_free(child_parsed);
78                         break;
79                 }
80
81                 ec_parsed_add_child(parsed, child_parsed);
82
83                 /* it matches an empty strvec, no need to continue */
84                 len = ec_parsed_len(child_parsed);
85                 if (len == 0) {
86                         ec_parsed_free(child_parsed);
87                         break;
88                 }
89
90                 off += len;
91         }
92
93         if (count < node->min) {
94                 ec_parsed_free_children(parsed);
95                 return parsed;
96         }
97
98         match_strvec = ec_strvec_ndup(strvec, 0, off);
99         if (match_strvec == NULL)
100                 goto fail;
101
102         ec_parsed_set_match(parsed, gen_node, match_strvec);
103
104         return parsed;
105
106 fail:
107         ec_strvec_free(childvec);
108         ec_parsed_free(parsed);
109         return NULL;
110 }
111
112 #if 0 //XXX missing node_many_complete
113 static struct ec_completed *ec_node_many_complete(const struct ec_node *gen_node,
114         const struct ec_strvec *strvec)
115 {
116         struct ec_node_many *node = (struct ec_node_many *)gen_node;
117         struct ec_completed *completed, *child_completed;
118         struct ec_strvec *childvec;
119         struct ec_parsed *parsed;
120         size_t len = 0;
121         unsigned int i;
122
123         completed = ec_completed();
124         if (completed == NULL)
125                 return NULL;
126
127         if (node->len == 0)
128                 return completed;
129
130         for (i = 0; i < node->len; i++) {
131                 childvec = ec_strvec_ndup(strvec, len,
132                         ec_strvec_len(strvec) - len);
133                 if (childvec == NULL)
134                         goto fail; // XXX fail ?
135
136                 child_completed = ec_node_complete_strvec(node->table[i],
137                         childvec);
138                 if (child_completed == NULL)
139                         goto fail;
140
141                 ec_completed_merge(completed, child_completed);
142
143                 parsed = ec_node_parse_strvec(node->table[i], childvec);
144                 if (parsed == NULL)
145                         goto fail;
146
147                 ec_strvec_free(childvec);
148                 childvec = NULL;
149
150                 if (!ec_parsed_matches(parsed)) {
151                         ec_parsed_free(parsed);
152                         break;
153                 }
154
155                 len += ec_strvec_len(parsed->strvec);
156                 ec_parsed_free(parsed);
157         }
158
159         return completed;
160
161 fail:
162         ec_strvec_free(childvec);
163         ec_completed_free(completed);
164         return NULL;
165 }
166 #endif
167
168 static void ec_node_many_free_priv(struct ec_node *gen_node)
169 {
170         struct ec_node_many *node = (struct ec_node_many *)gen_node;
171
172         ec_node_free(node->child);
173 }
174
175 static struct ec_node_type ec_node_many_type = {
176         .name = "many",
177         .parse = ec_node_many_parse,
178         .complete = ec_node_default_complete,
179 //XXX   .complete = ec_node_many_complete,
180         .size = sizeof(struct ec_node_many),
181         .free_priv = ec_node_many_free_priv,
182 };
183
184 EC_NODE_TYPE_REGISTER(ec_node_many_type);
185
186 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
187         unsigned int min, unsigned int max)
188 {
189         struct ec_node_many *node = NULL;
190
191         if (child == NULL)
192                 return NULL;
193
194         node = (struct ec_node_many *)__ec_node(&ec_node_many_type, id);
195         if (node == NULL) {
196                 ec_node_free(child);
197                 return NULL;
198         }
199
200         node->child = child;
201         node->min = min;
202         node->max = max;
203
204         return &node->gen;
205 }
206
207 static int ec_node_many_testcase(void)
208 {
209         struct ec_node *node;
210         int ret = 0;
211
212         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 0, 0);
213         if (node == NULL) {
214                 ec_log(EC_LOG_ERR, "cannot create node\n");
215                 return -1;
216         }
217         ret |= EC_TEST_CHECK_PARSE(node, 0, "bar");
218         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
219         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
220         ret |= EC_TEST_CHECK_PARSE(node, 0);
221         ec_node_free(node);
222
223         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 0);
224         if (node == NULL) {
225                 ec_log(EC_LOG_ERR, "cannot create node\n");
226                 return -1;
227         }
228         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
229         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
230         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
231         ret |= EC_TEST_CHECK_PARSE(node, -1);
232         ec_node_free(node);
233
234         node = ec_node_many(NULL, ec_node_str(NULL, "foo"), 1, 2);
235         if (node == NULL) {
236                 ec_log(EC_LOG_ERR, "cannot create node\n");
237                 return -1;
238         }
239         ret |= EC_TEST_CHECK_PARSE(node, -1, "bar");
240         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
241         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
242         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
243         ret |= EC_TEST_CHECK_PARSE(node, -1);
244         ec_node_free(node);
245
246         /* test completion */
247         /* XXX */
248
249         return ret;
250 }
251
252 static struct ec_test ec_node_many_test = {
253         .name = "many",
254         .test = ec_node_many_testcase,
255 };
256
257 EC_TEST_REGISTER(ec_node_many_test);