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