ed7325fdd2c0ff62648b09bdd95795d30dd2bc35
[protos/libecoli.git] / lib / ecoli_node_seq.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 <stdint.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <stdarg.h>
11 #include <errno.h>
12
13 #include <ecoli_malloc.h>
14 #include <ecoli_log.h>
15 #include <ecoli_test.h>
16 #include <ecoli_strvec.h>
17 #include <ecoli_node.h>
18 #include <ecoli_parse.h>
19 #include <ecoli_complete.h>
20 #include <ecoli_node_str.h>
21 #include <ecoli_node_option.h>
22 #include <ecoli_node_or.h>
23 #include <ecoli_node_many.h>
24 #include <ecoli_node_seq.h>
25
26 EC_LOG_TYPE_REGISTER(node_seq);
27
28 struct ec_node_seq {
29         struct ec_node gen;
30         struct ec_node **table;
31         size_t len;
32 };
33
34 static int
35 ec_node_seq_parse(const struct ec_node *gen_node,
36                 struct ec_parse *state,
37                 const struct ec_strvec *strvec)
38 {
39         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
40         struct ec_strvec *childvec = NULL;
41         size_t len = 0;
42         unsigned int i;
43         int ret;
44
45         for (i = 0; i < node->len; i++) {
46                 childvec = ec_strvec_ndup(strvec, len,
47                         ec_strvec_len(strvec) - len);
48                 if (childvec == NULL)
49                         goto fail;
50
51                 ret = ec_node_parse_child(node->table[i], state, childvec);
52                 if (ret < 0)
53                         goto fail;
54
55                 ec_strvec_free(childvec);
56                 childvec = NULL;
57
58                 if (ret == EC_PARSE_NOMATCH) {
59                         ec_parse_free_children(state);
60                         return EC_PARSE_NOMATCH;
61                 }
62
63                 len += ret;
64         }
65
66         return len;
67
68 fail:
69         ec_strvec_free(childvec);
70         return -1;
71 }
72
73 static int
74 __ec_node_seq_complete(struct ec_node **table, size_t table_len,
75                 struct ec_comp *comp,
76                 const struct ec_strvec *strvec)
77 {
78         struct ec_parse *parse = ec_comp_get_state(comp);
79         struct ec_strvec *childvec = NULL;
80         unsigned int i;
81         int ret;
82
83         if (table_len == 0)
84                 return 0;
85
86         /*
87          * Example of completion for a sequence node = [n1,n2] and an
88          * input = [a,b,c,d]:
89          *
90          * result = complete(n1, [a,b,c,d]) +
91          *    complete(n2, [b,c,d]) if n1 matches [a] +
92          *    complete(n2, [c,d]) if n1 matches [a,b] +
93          *    complete(n2, [d]) if n1 matches [a,b,c] +
94          *    complete(n2, []) if n1 matches [a,b,c,d]
95          */
96
97         /* first, try to complete with the first node of the table */
98         ret = ec_node_complete_child(table[0], comp, strvec);
99         if (ret < 0)
100                 goto fail;
101
102         /* then, if the first node of the table matches the beginning of the
103          * strvec, try to complete the rest */
104         for (i = 0; i < ec_strvec_len(strvec); i++) {
105                 childvec = ec_strvec_ndup(strvec, 0, i);
106                 if (childvec == NULL)
107                         goto fail;
108
109                 ret = ec_node_parse_child(table[0], parse, childvec);
110                 if (ret < 0)
111                         goto fail;
112
113                 ec_strvec_free(childvec);
114                 childvec = NULL;
115
116                 if ((unsigned int)ret != i) {
117                         if (ret != EC_PARSE_NOMATCH)
118                                 ec_parse_del_last_child(parse);
119                         continue;
120                 }
121
122                 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
123                 if (childvec == NULL) {
124                         ec_parse_del_last_child(parse);
125                         goto fail;
126                 }
127
128                 ret = __ec_node_seq_complete(&table[1],
129                                         table_len - 1,
130                                         comp, childvec);
131                 ec_parse_del_last_child(parse);
132                 ec_strvec_free(childvec);
133                 childvec = NULL;
134
135                 if (ret < 0)
136                         goto fail;
137         }
138
139         return 0;
140
141 fail:
142         ec_strvec_free(childvec);
143         return -1;
144 }
145
146 static int
147 ec_node_seq_complete(const struct ec_node *gen_node,
148                 struct ec_comp *comp,
149                 const struct ec_strvec *strvec)
150 {
151         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
152
153         return __ec_node_seq_complete(node->table, node->len, comp,
154                                 strvec);
155 }
156
157 static void ec_node_seq_free_priv(struct ec_node *gen_node)
158 {
159         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
160
161         ec_free(node->table);
162 }
163
164 static size_t
165 ec_node_seq_get_children_count(const struct ec_node *gen_node)
166 {
167         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
168         return node->len;
169 }
170
171 static struct ec_node *
172 ec_node_seq_get_child(const struct ec_node *gen_node, size_t i)
173 {
174         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
175
176         if (i >= node->len)
177                 return NULL;
178
179         return node->table[i];
180 }
181
182 static struct ec_node_type ec_node_seq_type = {
183         .name = "seq",
184         .parse = ec_node_seq_parse,
185         .complete = ec_node_seq_complete,
186         .size = sizeof(struct ec_node_seq),
187         .free_priv = ec_node_seq_free_priv,
188         .get_children_count = ec_node_seq_get_children_count,
189         .get_child = ec_node_seq_get_child,
190 };
191
192 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
193
194 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
195 {
196         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
197         struct ec_node **table;
198
199         assert(node != NULL);
200
201         if (child == NULL) {
202                 errno = EINVAL;
203                 goto fail;
204         }
205
206         if (ec_node_check_type(gen_node, &ec_node_seq_type) < 0)
207                 goto fail;
208
209         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
210         if (table == NULL)
211                 goto fail;
212
213         node->table = table;
214         table[node->len] = child;
215         node->len++;
216
217         return 0;
218
219 fail:
220         ec_node_free(child);
221         return -1;
222 }
223
224 struct ec_node *__ec_node_seq(const char *id, ...)
225 {
226         struct ec_node *gen_node = NULL;
227         struct ec_node_seq *node = NULL;
228         struct ec_node *child;
229         va_list ap;
230         int fail = 0;
231
232         va_start(ap, id);
233
234         gen_node = __ec_node(&ec_node_seq_type, id);
235         node = (struct ec_node_seq *)gen_node;
236         if (node == NULL)
237                 fail = 1;;
238
239         for (child = va_arg(ap, struct ec_node *);
240              child != EC_NODE_ENDLIST;
241              child = va_arg(ap, struct ec_node *)) {
242
243                 /* on error, don't quit the loop to avoid leaks */
244                 if (fail == 1 || child == NULL ||
245                                 ec_node_seq_add(&node->gen, child) < 0) {
246                         fail = 1;
247                         ec_node_free(child);
248                 }
249         }
250
251         if (fail == 1)
252                 goto fail;
253
254         va_end(ap);
255         return gen_node;
256
257 fail:
258         ec_node_free(gen_node); /* will also free children */
259         va_end(ap);
260         return NULL;
261 }
262
263 /* LCOV_EXCL_START */
264 static int ec_node_seq_testcase(void)
265 {
266         struct ec_node *node = NULL;
267         int testres = 0;
268
269         node = EC_NODE_SEQ(EC_NO_ID,
270                 ec_node_str(EC_NO_ID, "foo"),
271                 ec_node_str(EC_NO_ID, "bar")
272         );
273         if (node == NULL) {
274                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
275                 return -1;
276         }
277         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
278         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
279         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
280         testres |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
281         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
282         testres |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
283         testres |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
284         ec_node_free(node);
285
286         /* test completion */
287         node = EC_NODE_SEQ(EC_NO_ID,
288                 ec_node_str(EC_NO_ID, "foo"),
289                 ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "toto")),
290                 ec_node_str(EC_NO_ID, "bar")
291         );
292         if (node == NULL) {
293                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
294                 return -1;
295         }
296         testres |= EC_TEST_CHECK_COMPLETE(node,
297                 "", EC_NODE_ENDLIST,
298                 "foo", EC_NODE_ENDLIST);
299         testres |= EC_TEST_CHECK_COMPLETE(node,
300                 "f", EC_NODE_ENDLIST,
301                 "foo", EC_NODE_ENDLIST);
302         testres |= EC_TEST_CHECK_COMPLETE(node,
303                 "foo", EC_NODE_ENDLIST,
304                 "foo", EC_NODE_ENDLIST);
305         testres |= EC_TEST_CHECK_COMPLETE(node,
306                 "foo", "", EC_NODE_ENDLIST,
307                 "bar", "toto", EC_NODE_ENDLIST);
308         testres |= EC_TEST_CHECK_COMPLETE(node,
309                 "foo", "t", EC_NODE_ENDLIST,
310                 "toto", EC_NODE_ENDLIST);
311         testres |= EC_TEST_CHECK_COMPLETE(node,
312                 "foo", "b", EC_NODE_ENDLIST,
313                 "bar", EC_NODE_ENDLIST);
314         testres |= EC_TEST_CHECK_COMPLETE(node,
315                 "foo", "bar", EC_NODE_ENDLIST,
316                 "bar", EC_NODE_ENDLIST);
317         testres |= EC_TEST_CHECK_COMPLETE(node,
318                 "x", EC_NODE_ENDLIST,
319                 EC_NODE_ENDLIST);
320         testres |= EC_TEST_CHECK_COMPLETE(node,
321                 "foobarx", EC_NODE_ENDLIST,
322                 EC_NODE_ENDLIST);
323         ec_node_free(node);
324
325         return testres;
326 }
327 /* LCOV_EXCL_STOP */
328
329 static struct ec_test ec_node_seq_test = {
330         .name = "node_seq",
331         .test = ec_node_seq_testcase,
332 };
333
334 EC_TEST_REGISTER(ec_node_seq_test);