67047b37681285f2d4ee93913ea02b5c008d8b78
[protos/libecoli.git] / lib / ecoli_node_or.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <stdarg.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_node_or.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_test.h>
21
22 EC_LOG_TYPE_REGISTER(node_or);
23
24 struct ec_node_or {
25         struct ec_node gen;
26         struct ec_node **table;
27         unsigned int len;
28 };
29
30 static int
31 ec_node_or_parse(const struct ec_node *gen_node,
32                 struct ec_parse *state,
33                 const struct ec_strvec *strvec)
34 {
35         struct ec_node_or *node = (struct ec_node_or *)gen_node;
36         unsigned int i;
37         int ret;
38
39         for (i = 0; i < node->len; i++) {
40                 ret = ec_node_parse_child(node->table[i], state, strvec);
41                 if (ret == EC_PARSE_NOMATCH)
42                         continue;
43                 return ret;
44         }
45
46         return EC_PARSE_NOMATCH;
47 }
48
49 static int
50 ec_node_or_complete(const struct ec_node *gen_node,
51                 struct ec_comp *comp,
52                 const struct ec_strvec *strvec)
53 {
54         struct ec_node_or *node = (struct ec_node_or *)gen_node;
55         int ret;
56         size_t n;
57
58         for (n = 0; n < node->len; n++) {
59                 ret = ec_node_complete_child(node->table[n],
60                                         comp, strvec);
61                 if (ret < 0)
62                         return ret;
63         }
64
65         return 0;
66 }
67
68 static void ec_node_or_free_priv(struct ec_node *gen_node)
69 {
70         struct ec_node_or *node = (struct ec_node_or *)gen_node;
71
72         ec_free(node->table);
73 }
74
75 static size_t
76 ec_node_or_get_children_count(const struct ec_node *gen_node)
77 {
78         struct ec_node_or *node = (struct ec_node_or *)gen_node;
79         return node->len;
80 }
81
82 static struct ec_node *
83 ec_node_or_get_child(const struct ec_node *gen_node, size_t i)
84 {
85         struct ec_node_or *node = (struct ec_node_or *)gen_node;
86
87         if (i >= node->len)
88                 return NULL;
89
90         return node->table[i];
91 }
92
93 static struct ec_node_type ec_node_or_type = {
94         .name = "or",
95         .parse = ec_node_or_parse,
96         .complete = ec_node_or_complete,
97         .size = sizeof(struct ec_node_or),
98         .free_priv = ec_node_or_free_priv,
99         .get_children_count = ec_node_or_get_children_count,
100         .get_child = ec_node_or_get_child,
101 };
102
103 EC_NODE_TYPE_REGISTER(ec_node_or_type);
104
105 int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
106 {
107         struct ec_node_or *node = (struct ec_node_or *)gen_node;
108         struct ec_node **table;
109
110         assert(node != NULL);
111
112         assert(node != NULL);
113
114         if (child == NULL) {
115                 errno = EINVAL;
116                 goto fail;
117         }
118
119         if (ec_node_check_type(gen_node, &ec_node_or_type) < 0)
120                 goto fail;
121
122         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
123         if (table == NULL)
124                 goto fail;
125
126         node->table = table;
127         table[node->len] = child;
128         node->len++;
129
130         return 0;
131
132 fail:
133         ec_node_free(child);
134         return -1;
135 }
136
137 struct ec_node *__ec_node_or(const char *id, ...)
138 {
139         struct ec_node *gen_node = NULL;
140         struct ec_node_or *node = NULL;
141         struct ec_node *child;
142         va_list ap;
143         int fail = 0;
144
145         va_start(ap, id);
146
147         gen_node = __ec_node(&ec_node_or_type, id);
148         node = (struct ec_node_or *)gen_node;
149         if (node == NULL)
150                 fail = 1;;
151
152         for (child = va_arg(ap, struct ec_node *);
153              child != EC_NODE_ENDLIST;
154              child = va_arg(ap, struct ec_node *)) {
155
156                 /* on error, don't quit the loop to avoid leaks */
157                 if (fail == 1 || child == NULL ||
158                                 ec_node_or_add(gen_node, child) < 0) {
159                         fail = 1;
160                         ec_node_free(child);
161                 }
162         }
163
164         if (fail == 1)
165                 goto fail;
166
167         va_end(ap);
168         return gen_node;
169
170 fail:
171         ec_node_free(gen_node); /* will also free children */
172         va_end(ap);
173         return NULL;
174 }
175
176 /* LCOV_EXCL_START */
177 static int ec_node_or_testcase(void)
178 {
179         struct ec_node *node;
180         int testres = 0;
181
182         node = EC_NODE_OR(EC_NO_ID,
183                 ec_node_str(EC_NO_ID, "foo"),
184                 ec_node_str(EC_NO_ID, "bar")
185         );
186         if (node == NULL) {
187                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
188                 return -1;
189         }
190         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
191         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
192         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
193         testres |= EC_TEST_CHECK_PARSE(node, -1, " ");
194         testres |= EC_TEST_CHECK_PARSE(node, -1, "foox");
195         testres |= EC_TEST_CHECK_PARSE(node, -1, "toto");
196         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
197         ec_node_free(node);
198
199         /* test completion */
200         node = EC_NODE_OR(EC_NO_ID,
201                 ec_node_str(EC_NO_ID, "foo"),
202                 ec_node_str(EC_NO_ID, "bar"),
203                 ec_node_str(EC_NO_ID, "bar2"),
204                 ec_node_str(EC_NO_ID, "toto"),
205                 ec_node_str(EC_NO_ID, "titi")
206         );
207         if (node == NULL) {
208                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
209                 return -1;
210         }
211         testres |= EC_TEST_CHECK_COMPLETE(node,
212                 "", EC_NODE_ENDLIST,
213                 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
214         testres |= EC_TEST_CHECK_COMPLETE(node,
215                 "f", EC_NODE_ENDLIST,
216                 "foo", EC_NODE_ENDLIST);
217         testres |= EC_TEST_CHECK_COMPLETE(node,
218                 "b", EC_NODE_ENDLIST,
219                 "bar", "bar2", EC_NODE_ENDLIST);
220         testres |= EC_TEST_CHECK_COMPLETE(node,
221                 "bar", EC_NODE_ENDLIST,
222                 "bar", "bar2", EC_NODE_ENDLIST);
223         testres |= EC_TEST_CHECK_COMPLETE(node,
224                 "t", EC_NODE_ENDLIST,
225                 "toto", "titi", EC_NODE_ENDLIST);
226         testres |= EC_TEST_CHECK_COMPLETE(node,
227                 "to", EC_NODE_ENDLIST,
228                 "toto", EC_NODE_ENDLIST);
229         testres |= EC_TEST_CHECK_COMPLETE(node,
230                 "x", EC_NODE_ENDLIST,
231                 EC_NODE_ENDLIST);
232         ec_node_free(node);
233
234         return testres;
235 }
236 /* LCOV_EXCL_STOP */
237
238 static struct ec_test ec_node_or_test = {
239         .name = "node_or",
240         .test = ec_node_or_testcase,
241 };
242
243 EC_TEST_REGISTER(ec_node_or_test);