save
[protos/libecoli.git] / lib / ecoli_node_or.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_strvec.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41 #include <ecoli_node_or.h>
42 #include <ecoli_node_str.h>
43 #include <ecoli_test.h>
44
45 struct ec_node_or {
46         struct ec_node gen;
47         struct ec_node **table;
48         unsigned int len;
49 };
50
51 static struct ec_parsed *ec_node_or_parse(const struct ec_node *gen_node,
52         const struct ec_strvec *strvec)
53 {
54         struct ec_node_or *node = (struct ec_node_or *)gen_node;
55         struct ec_parsed *parsed, *child_parsed = NULL;
56         struct ec_strvec *match_strvec;
57         unsigned int i;
58
59         parsed = ec_parsed();
60         if (parsed == NULL)
61                 goto fail;
62
63         for (i = 0; i < node->len; i++) {
64                 child_parsed = ec_node_parse_strvec(node->table[i], strvec);
65                 if (child_parsed == NULL)
66                         goto fail;
67                 if (ec_parsed_matches(child_parsed))
68                         break;
69                 ec_parsed_free(child_parsed);
70                 child_parsed = NULL;
71         }
72
73         /* no match */
74         if (i == node->len)
75                 return parsed;
76
77         match_strvec = ec_strvec_dup(child_parsed->strvec);
78         if (match_strvec == NULL)
79                 goto fail;
80
81         ec_parsed_set_match(parsed, gen_node, match_strvec);
82         ec_parsed_add_child(parsed, child_parsed);
83
84         return parsed;
85
86  fail:
87         ec_parsed_free(child_parsed);
88         ec_parsed_free(parsed);
89         return NULL;
90 }
91
92 static struct ec_completed *ec_node_or_complete(const struct ec_node *gen_node,
93         const struct ec_strvec *strvec)
94 {
95         struct ec_node_or *node = (struct ec_node_or *)gen_node;
96         struct ec_completed *completed, *child_completed;
97         size_t n;
98
99         completed = ec_completed();
100         if (completed == NULL)
101                 return NULL;
102
103         for (n = 0; n < node->len; n++) {
104                 child_completed = ec_node_complete_strvec(node->table[n],
105                         strvec);
106
107                 if (child_completed == NULL) // XXX fail instead?
108                         continue;
109
110                 ec_completed_merge(completed, child_completed);
111         }
112
113         return completed;
114 }
115
116 static void ec_node_or_free_priv(struct ec_node *gen_node)
117 {
118         struct ec_node_or *node = (struct ec_node_or *)gen_node;
119         unsigned int i;
120
121         for (i = 0; i < node->len; i++)
122                 ec_node_free(node->table[i]);
123         ec_free(node->table);
124 }
125
126 int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
127 {
128         struct ec_node_or *node = (struct ec_node_or *)gen_node;
129         struct ec_node **table;
130
131         assert(node != NULL);
132
133         if (child == NULL)
134                 return -EINVAL;
135
136         gen_node->flags &= ~EC_NODE_F_BUILT;
137
138         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
139         if (table == NULL) {
140                 ec_node_free(child);
141                 return -1;
142         }
143
144         node->table = table;
145         table[node->len] = child;
146         node->len++;
147
148         child->parent = gen_node;
149         TAILQ_INSERT_TAIL(&gen_node->children, child, next);
150
151         return 0;
152 }
153
154 static struct ec_node_type ec_node_or_type = {
155         .name = "or",
156         .parse = ec_node_or_parse,
157         .complete = ec_node_or_complete,
158         .size = sizeof(struct ec_node_or),
159         .free_priv = ec_node_or_free_priv,
160 };
161
162 EC_NODE_TYPE_REGISTER(ec_node_or_type);
163
164 struct ec_node *__ec_node_or(const char *id, ...)
165 {
166         struct ec_node *gen_node = NULL;
167         struct ec_node_or *node = NULL;
168         struct ec_node *child;
169         va_list ap;
170         int fail = 0;
171
172         va_start(ap, id);
173
174         gen_node = __ec_node(&ec_node_or_type, id);
175         node = (struct ec_node_or *)gen_node;
176         if (node == NULL)
177                 fail = 1;;
178
179         for (child = va_arg(ap, struct ec_node *);
180              child != EC_NODE_ENDLIST;
181              child = va_arg(ap, struct ec_node *)) {
182
183                 /* on error, don't quit the loop to avoid leaks */
184                 if (fail == 1 || child == NULL ||
185                                 ec_node_or_add(gen_node, child) < 0) {
186                         fail = 1;
187                         ec_node_free(child);
188                 }
189         }
190
191         if (fail == 1)
192                 goto fail;
193
194         va_end(ap);
195         return gen_node;
196
197 fail:
198         ec_node_free(gen_node); /* will also free children */
199         va_end(ap);
200         return NULL;
201 }
202
203 static int ec_node_or_testcase(void)
204 {
205         struct ec_node *node;
206         int ret = 0;
207
208         node = EC_NODE_OR(NULL,
209                 ec_node_str(NULL, "foo"),
210                 ec_node_str(NULL, "bar")
211         );
212         if (node == NULL) {
213                 ec_log(EC_LOG_ERR, "cannot create node\n");
214                 return -1;
215         }
216         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo");
217         ret |= EC_TEST_CHECK_PARSE(node, 1, "bar");
218         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
219         ret |= EC_TEST_CHECK_PARSE(node, -1, " ");
220         ret |= EC_TEST_CHECK_PARSE(node, -1, "foox");
221         ret |= EC_TEST_CHECK_PARSE(node, -1, "toto");
222         ret |= EC_TEST_CHECK_PARSE(node, -1, "");
223         ec_node_free(node);
224
225         /* test completion */
226         node = EC_NODE_OR(NULL,
227                 ec_node_str(NULL, "foo"),
228                 ec_node_str(NULL, "bar"),
229                 ec_node_str(NULL, "bar2"),
230                 ec_node_str(NULL, "toto"),
231                 ec_node_str(NULL, "titi")
232         );
233         if (node == NULL) {
234                 ec_log(EC_LOG_ERR, "cannot create node\n");
235                 return -1;
236         }
237         ret |= EC_TEST_CHECK_COMPLETE(node,
238                 "", EC_NODE_ENDLIST,
239                 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST,
240                 "");
241         ret |= EC_TEST_CHECK_COMPLETE(node,
242                 "f", EC_NODE_ENDLIST,
243                 "oo", EC_NODE_ENDLIST,
244                 "oo");
245         ret |= EC_TEST_CHECK_COMPLETE(node,
246                 "b", EC_NODE_ENDLIST,
247                 "ar", "ar2", EC_NODE_ENDLIST,
248                 "ar");
249         ret |= EC_TEST_CHECK_COMPLETE(node,
250                 "bar", EC_NODE_ENDLIST,
251                 "", "2", EC_NODE_ENDLIST,
252                 "");
253         ret |= EC_TEST_CHECK_COMPLETE(node,
254                 "t", EC_NODE_ENDLIST,
255                 "oto", "iti", EC_NODE_ENDLIST,
256                 "");
257         ret |= EC_TEST_CHECK_COMPLETE(node,
258                 "to", EC_NODE_ENDLIST,
259                 "to", EC_NODE_ENDLIST,
260                 "to");
261         ret |= EC_TEST_CHECK_COMPLETE(node,
262                 "x", EC_NODE_ENDLIST,
263                 EC_NODE_ENDLIST,
264                 "");
265         ec_node_free(node);
266
267         return ret;
268 }
269
270 static struct ec_test ec_node_or_test = {
271         .name = "node_or",
272         .test = ec_node_or_testcase,
273 };
274
275 EC_TEST_REGISTER(ec_node_or_test);