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