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