add meson support
[protos/libecoli.git] / src / 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_config.h>
19 #include <ecoli_parse.h>
20 #include <ecoli_complete.h>
21 #include <ecoli_node_helper.h>
22 #include <ecoli_node_str.h>
23 #include <ecoli_node_option.h>
24 #include <ecoli_node_or.h>
25 #include <ecoli_node_many.h>
26 #include <ecoli_node_seq.h>
27
28 EC_LOG_TYPE_REGISTER(node_seq);
29
30 struct ec_node_seq {
31         struct ec_node gen;
32         struct ec_node **table;
33         size_t len;
34 };
35
36 static int
37 ec_node_seq_parse(const struct ec_node *gen_node,
38                 struct ec_parse *state,
39                 const struct ec_strvec *strvec)
40 {
41         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
42         struct ec_strvec *childvec = NULL;
43         size_t len = 0;
44         unsigned int i;
45         int ret;
46
47         for (i = 0; i < node->len; i++) {
48                 childvec = ec_strvec_ndup(strvec, len,
49                         ec_strvec_len(strvec) - len);
50                 if (childvec == NULL)
51                         goto fail;
52
53                 ret = ec_node_parse_child(node->table[i], state, childvec);
54                 if (ret < 0)
55                         goto fail;
56
57                 ec_strvec_free(childvec);
58                 childvec = NULL;
59
60                 if (ret == EC_PARSE_NOMATCH) {
61                         ec_parse_free_children(state);
62                         return EC_PARSE_NOMATCH;
63                 }
64
65                 len += ret;
66         }
67
68         return len;
69
70 fail:
71         ec_strvec_free(childvec);
72         return -1;
73 }
74
75 static int
76 __ec_node_seq_complete(struct ec_node **table, size_t table_len,
77                 struct ec_comp *comp,
78                 const struct ec_strvec *strvec)
79 {
80         struct ec_parse *parse = ec_comp_get_state(comp);
81         struct ec_strvec *childvec = NULL;
82         unsigned int i;
83         int ret;
84
85         if (table_len == 0)
86                 return 0;
87
88         /*
89          * Example of completion for a sequence node = [n1,n2] and an
90          * input = [a,b,c,d]:
91          *
92          * result = complete(n1, [a,b,c,d]) +
93          *    complete(n2, [b,c,d]) if n1 matches [a] +
94          *    complete(n2, [c,d]) if n1 matches [a,b] +
95          *    complete(n2, [d]) if n1 matches [a,b,c] +
96          *    complete(n2, []) if n1 matches [a,b,c,d]
97          */
98
99         /* first, try to complete with the first node of the table */
100         ret = ec_node_complete_child(table[0], comp, strvec);
101         if (ret < 0)
102                 goto fail;
103
104         /* then, if the first node of the table matches the beginning of the
105          * strvec, try to complete the rest */
106         for (i = 0; i < ec_strvec_len(strvec); i++) {
107                 childvec = ec_strvec_ndup(strvec, 0, i);
108                 if (childvec == NULL)
109                         goto fail;
110
111                 ret = ec_node_parse_child(table[0], parse, childvec);
112                 if (ret < 0)
113                         goto fail;
114
115                 ec_strvec_free(childvec);
116                 childvec = NULL;
117
118                 if ((unsigned int)ret != i) {
119                         if (ret != EC_PARSE_NOMATCH)
120                                 ec_parse_del_last_child(parse);
121                         continue;
122                 }
123
124                 childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
125                 if (childvec == NULL) {
126                         ec_parse_del_last_child(parse);
127                         goto fail;
128                 }
129
130                 ret = __ec_node_seq_complete(&table[1],
131                                         table_len - 1,
132                                         comp, childvec);
133                 ec_parse_del_last_child(parse);
134                 ec_strvec_free(childvec);
135                 childvec = NULL;
136
137                 if (ret < 0)
138                         goto fail;
139         }
140
141         return 0;
142
143 fail:
144         ec_strvec_free(childvec);
145         return -1;
146 }
147
148 static int
149 ec_node_seq_complete(const struct ec_node *gen_node,
150                 struct ec_comp *comp,
151                 const struct ec_strvec *strvec)
152 {
153         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
154
155         return __ec_node_seq_complete(node->table, node->len, comp,
156                                 strvec);
157 }
158
159 static void ec_node_seq_free_priv(struct ec_node *gen_node)
160 {
161         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
162         size_t i;
163
164         for (i = 0; i < node->len; i++)
165                 ec_node_free(node->table[i]);
166         ec_free(node->table);
167         node->table = NULL;
168         node->len = 0;
169 }
170
171 static const struct ec_config_schema ec_node_seq_subschema[] = {
172         {
173                 .desc = "A child node which is part of the sequence.",
174                 .type = EC_CONFIG_TYPE_NODE,
175         },
176         {
177                 .type = EC_CONFIG_TYPE_NONE,
178         },
179 };
180
181 static const struct ec_config_schema ec_node_seq_schema[] = {
182         {
183                 .key = "children",
184                 .desc = "The list of children nodes, to be parsed in sequence.",
185                 .type = EC_CONFIG_TYPE_LIST,
186                 .subschema = ec_node_seq_subschema,
187         },
188         {
189                 .type = EC_CONFIG_TYPE_NONE,
190         },
191 };
192
193 static int ec_node_seq_set_config(struct ec_node *gen_node,
194                                 const struct ec_config *config)
195 {
196         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
197         struct ec_node **table = NULL;
198         size_t i, len = 0;
199
200         table = ec_node_config_node_list_to_table(
201                 ec_config_dict_get(config, "children"), &len);
202         if (table == NULL)
203                 goto fail;
204
205         for (i = 0; i < node->len; i++)
206                 ec_node_free(node->table[i]);
207         ec_free(node->table);
208         node->table = table;
209         node->len = len;
210
211         return 0;
212
213 fail:
214         for (i = 0; i < len; i++)
215                 ec_node_free(table[i]);
216         ec_free(table);
217         return -1;
218 }
219
220 static size_t
221 ec_node_seq_get_children_count(const struct ec_node *gen_node)
222 {
223         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
224         return node->len;
225 }
226
227 static int
228 ec_node_seq_get_child(const struct ec_node *gen_node, size_t i,
229                 struct ec_node **child, unsigned int *refs)
230 {
231         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
232
233         if (i >= node->len)
234                 return -1;
235
236         *child = node->table[i];
237         /* each child node is referenced twice: once in the config and
238          * once in the node->table[] */
239         *refs = 2;
240         return 0;
241 }
242
243 static struct ec_node_type ec_node_seq_type = {
244         .name = "seq",
245         .schema = ec_node_seq_schema,
246         .set_config = ec_node_seq_set_config,
247         .parse = ec_node_seq_parse,
248         .complete = ec_node_seq_complete,
249         .size = sizeof(struct ec_node_seq),
250         .free_priv = ec_node_seq_free_priv,
251         .get_children_count = ec_node_seq_get_children_count,
252         .get_child = ec_node_seq_get_child,
253 };
254
255 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
256
257 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
258 {
259         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
260         const struct ec_config *cur_config = NULL;
261         struct ec_config *config = NULL, *children;
262         int ret;
263
264         assert(node != NULL);
265
266         /* XXX factorize this code in a helper */
267
268         if (ec_node_check_type(gen_node, &ec_node_seq_type) < 0)
269                 goto fail;
270
271         cur_config = ec_node_get_config(gen_node);
272         if (cur_config == NULL)
273                 config = ec_config_dict();
274         else
275                 config = ec_config_dup(cur_config);
276         if (config == NULL)
277                 goto fail;
278
279         children = ec_config_dict_get(config, "children");
280         if (children == NULL) {
281                 children = ec_config_list();
282                 if (children == NULL)
283                         goto fail;
284
285                 if (ec_config_dict_set(config, "children", children) < 0)
286                         goto fail; /* children list is freed on error */
287         }
288
289         if (ec_config_list_add(children, ec_config_node(child)) < 0) {
290                 child = NULL;
291                 goto fail;
292         }
293
294         ret = ec_node_set_config(gen_node, config);
295         config = NULL; /* freed */
296         if (ret < 0)
297                 goto fail;
298
299         return 0;
300
301 fail:
302         ec_config_free(config);
303         ec_node_free(child);
304         return -1;
305 }
306
307 struct ec_node *__ec_node_seq(const char *id, ...)
308 {
309         struct ec_config *config = NULL, *children = NULL;
310         struct ec_node *gen_node = NULL;
311         struct ec_node *child;
312         va_list ap;
313         int ret;
314
315         va_start(ap, id);
316         child = va_arg(ap, struct ec_node *);
317
318         gen_node = ec_node_from_type(&ec_node_seq_type, id);
319         if (gen_node == NULL)
320                 goto fail_free_children;
321
322         config = ec_config_dict();
323         if (config == NULL)
324                 goto fail_free_children;
325
326         children = ec_config_list();
327         if (children == NULL)
328                 goto fail_free_children;
329
330         for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) {
331                 if (child == NULL)
332                         goto fail_free_children;
333
334                 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
335                         child = NULL;
336                         goto fail_free_children;
337                 }
338         }
339
340         if (ec_config_dict_set(config, "children", children) < 0) {
341                 children = NULL; /* freed */
342                 goto fail;
343         }
344         children = NULL;
345
346         ret = ec_node_set_config(gen_node, config);
347         config = NULL; /* freed */
348         if (ret < 0)
349                 goto fail;
350
351         va_end(ap);
352
353         return gen_node;
354
355 fail_free_children:
356         for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *))
357                 ec_node_free(child);
358 fail:
359         ec_node_free(gen_node); /* will also free added children */
360         ec_config_free(children);
361         ec_config_free(config);
362         va_end(ap);
363
364         return NULL;
365 }
366
367 /* LCOV_EXCL_START */
368 static int ec_node_seq_testcase(void)
369 {
370         struct ec_node *node = NULL;
371         int testres = 0;
372
373         node = EC_NODE_SEQ(EC_NO_ID,
374                 ec_node_str(EC_NO_ID, "foo"),
375                 ec_node_str(EC_NO_ID, "bar")
376         );
377         if (node == NULL) {
378                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
379                 return -1;
380         }
381         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
382         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
383         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
384         testres |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
385         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
386         testres |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
387         testres |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
388
389         testres |= (ec_node_seq_add(node, ec_node_str(EC_NO_ID, "grr")) < 0);
390         testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "grr");
391
392         ec_node_free(node);
393
394         /* test completion */
395         node = EC_NODE_SEQ(EC_NO_ID,
396                 ec_node_str(EC_NO_ID, "foo"),
397                 ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "toto")),
398                 ec_node_str(EC_NO_ID, "bar")
399         );
400         if (node == NULL) {
401                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
402                 return -1;
403         }
404         testres |= EC_TEST_CHECK_COMPLETE(node,
405                 "", EC_NODE_ENDLIST,
406                 "foo", EC_NODE_ENDLIST);
407         testres |= EC_TEST_CHECK_COMPLETE(node,
408                 "f", EC_NODE_ENDLIST,
409                 "foo", EC_NODE_ENDLIST);
410         testres |= EC_TEST_CHECK_COMPLETE(node,
411                 "foo", EC_NODE_ENDLIST,
412                 "foo", EC_NODE_ENDLIST);
413         testres |= EC_TEST_CHECK_COMPLETE(node,
414                 "foo", "", EC_NODE_ENDLIST,
415                 "bar", "toto", EC_NODE_ENDLIST);
416         testres |= EC_TEST_CHECK_COMPLETE(node,
417                 "foo", "t", EC_NODE_ENDLIST,
418                 "toto", EC_NODE_ENDLIST);
419         testres |= EC_TEST_CHECK_COMPLETE(node,
420                 "foo", "b", EC_NODE_ENDLIST,
421                 "bar", EC_NODE_ENDLIST);
422         testres |= EC_TEST_CHECK_COMPLETE(node,
423                 "foo", "bar", EC_NODE_ENDLIST,
424                 "bar", EC_NODE_ENDLIST);
425         testres |= EC_TEST_CHECK_COMPLETE(node,
426                 "x", EC_NODE_ENDLIST,
427                 EC_NODE_ENDLIST);
428         testres |= EC_TEST_CHECK_COMPLETE(node,
429                 "foobarx", EC_NODE_ENDLIST,
430                 EC_NODE_ENDLIST);
431         ec_node_free(node);
432
433         return testres;
434 }
435 /* LCOV_EXCL_STOP */
436
437 static struct ec_test ec_node_seq_test = {
438         .name = "node_seq",
439         .test = ec_node_seq_testcase,
440 };
441
442 EC_TEST_REGISTER(ec_node_seq_test);