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