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