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