get node children and refs in the same function
[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 int
238 ec_node_seq_get_child(const struct ec_node *gen_node, size_t i,
239                 struct ec_node **child, unsigned int *refs)
240 {
241         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
242
243         if (i >= node->len)
244                 return -1;
245
246         *child = node->table[i];
247         /* each child node is referenced twice: once in the config and
248          * once in the node->table[] */
249         *refs = 2;
250         return 0;
251 }
252
253 static struct ec_node_type ec_node_seq_type = {
254         .name = "seq",
255         .schema = ec_node_seq_schema,
256         .schema_len = EC_COUNT_OF(ec_node_seq_schema),
257         .set_config = ec_node_seq_set_config,
258         .parse = ec_node_seq_parse,
259         .complete = ec_node_seq_complete,
260         .size = sizeof(struct ec_node_seq),
261         .free_priv = ec_node_seq_free_priv,
262         .get_children_count = ec_node_seq_get_children_count,
263         .get_child = ec_node_seq_get_child,
264 };
265
266 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
267
268 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
269 {
270         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
271         const struct ec_config *cur_config = NULL;
272         struct ec_config *config = NULL, *children;
273         int ret;
274
275         assert(node != NULL);
276
277         /* XXX factorize this code in a helper */
278
279         if (ec_node_check_type(gen_node, &ec_node_seq_type) < 0)
280                 goto fail;
281
282         cur_config = ec_node_get_config(gen_node);
283         if (cur_config == NULL)
284                 config = ec_config_dict();
285         else
286                 config = ec_config_dup(cur_config);
287         if (config == NULL)
288                 goto fail;
289
290         children = ec_config_dict_get(config, "children");
291         if (children == NULL) {
292                 children = ec_config_list();
293                 if (children == NULL)
294                         goto fail;
295
296                 if (ec_config_dict_set(config, "children", children) < 0)
297                         goto fail; /* children list is freed on error */
298         }
299
300         if (ec_config_list_add(children, ec_config_node(child)) < 0) {
301                 child = NULL;
302                 goto fail;
303         }
304
305         ret = ec_node_set_config(gen_node, config);
306         config = NULL; /* freed */
307         if (ret < 0)
308                 goto fail;
309
310         return 0;
311
312 fail:
313         ec_config_free(config);
314         ec_node_free(child);
315         return -1;
316 }
317
318 struct ec_node *__ec_node_seq(const char *id, ...)
319 {
320         struct ec_config *config = NULL, *children = NULL;
321         struct ec_node *gen_node = NULL;
322         struct ec_node *child;
323         va_list ap;
324         int ret;
325
326         va_start(ap, id);
327         child = va_arg(ap, struct ec_node *);
328
329         gen_node = __ec_node(&ec_node_seq_type, id);
330         if (gen_node == NULL)
331                 goto fail_free_children;
332
333         config = ec_config_dict();
334         if (config == NULL)
335                 goto fail_free_children;
336
337         children = ec_config_list();
338         if (children == NULL)
339                 goto fail_free_children;
340
341         for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) {
342                 if (child == NULL)
343                         goto fail_free_children;
344
345                 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
346                         child = NULL;
347                         goto fail_free_children;
348                 }
349         }
350
351         if (ec_config_dict_set(config, "children", children) < 0) {
352                 children = NULL; /* freed */
353                 goto fail;
354         }
355         children = NULL;
356
357         ret = ec_node_set_config(gen_node, config);
358         config = NULL; /* freed */
359         if (ret < 0)
360                 goto fail;
361
362         va_end(ap);
363
364         return gen_node;
365
366 fail_free_children:
367         for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *))
368                 ec_node_free(child);
369 fail:
370         ec_node_free(gen_node); /* will also free added children */
371         ec_config_free(children);
372         ec_config_free(config);
373         va_end(ap);
374
375         return NULL;
376 }
377
378 /* LCOV_EXCL_START */
379 static int ec_node_seq_testcase(void)
380 {
381         struct ec_node *node = NULL;
382         int testres = 0;
383
384         node = EC_NODE_SEQ(EC_NO_ID,
385                 ec_node_str(EC_NO_ID, "foo"),
386                 ec_node_str(EC_NO_ID, "bar")
387         );
388         if (node == NULL) {
389                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
390                 return -1;
391         }
392         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
393         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
394         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
395         testres |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
396         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
397         testres |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
398         testres |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
399
400         testres |= (ec_node_seq_add(node, ec_node_str(EC_NO_ID, "grr")) < 0);
401         testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "grr");
402
403         ec_node_free(node);
404
405         /* test completion */
406         node = EC_NODE_SEQ(EC_NO_ID,
407                 ec_node_str(EC_NO_ID, "foo"),
408                 ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "toto")),
409                 ec_node_str(EC_NO_ID, "bar")
410         );
411         if (node == NULL) {
412                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
413                 return -1;
414         }
415         testres |= EC_TEST_CHECK_COMPLETE(node,
416                 "", EC_NODE_ENDLIST,
417                 "foo", EC_NODE_ENDLIST);
418         testres |= EC_TEST_CHECK_COMPLETE(node,
419                 "f", EC_NODE_ENDLIST,
420                 "foo", EC_NODE_ENDLIST);
421         testres |= EC_TEST_CHECK_COMPLETE(node,
422                 "foo", EC_NODE_ENDLIST,
423                 "foo", EC_NODE_ENDLIST);
424         testres |= EC_TEST_CHECK_COMPLETE(node,
425                 "foo", "", EC_NODE_ENDLIST,
426                 "bar", "toto", EC_NODE_ENDLIST);
427         testres |= EC_TEST_CHECK_COMPLETE(node,
428                 "foo", "t", EC_NODE_ENDLIST,
429                 "toto", EC_NODE_ENDLIST);
430         testres |= EC_TEST_CHECK_COMPLETE(node,
431                 "foo", "b", EC_NODE_ENDLIST,
432                 "bar", EC_NODE_ENDLIST);
433         testres |= EC_TEST_CHECK_COMPLETE(node,
434                 "foo", "bar", EC_NODE_ENDLIST,
435                 "bar", EC_NODE_ENDLIST);
436         testres |= EC_TEST_CHECK_COMPLETE(node,
437                 "x", EC_NODE_ENDLIST,
438                 EC_NODE_ENDLIST);
439         testres |= EC_TEST_CHECK_COMPLETE(node,
440                 "foobarx", EC_NODE_ENDLIST,
441                 EC_NODE_ENDLIST);
442         ec_node_free(node);
443
444         return testres;
445 }
446 /* LCOV_EXCL_STOP */
447
448 static struct ec_test ec_node_seq_test = {
449         .name = "node_seq",
450         .test = ec_node_seq_testcase,
451 };
452
453 EC_TEST_REGISTER(ec_node_seq_test);