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