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