add get_child_refs, tests are ok
[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
162         ec_free(node->table);
163 }
164
165 static const struct ec_config_schema ec_node_seq_subschema[] = {
166         {
167                 .desc = "A child node which is part of the sequence.",
168                 .type = EC_CONFIG_TYPE_NODE,
169         },
170 };
171
172 static const struct ec_config_schema ec_node_seq_schema[] = {
173         {
174                 .key = "children",
175                 .desc = "The list of children nodes, to be parsed in sequence.",
176                 .type = EC_CONFIG_TYPE_LIST,
177                 .subschema = ec_node_seq_subschema,
178                 .subschema_len = EC_COUNT_OF(ec_node_seq_subschema),
179         },
180 };
181
182 static int ec_node_seq_set_config(struct ec_node *gen_node,
183                                 const struct ec_config *config)
184 {
185         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
186         const struct ec_config *children = NULL, *child;
187         struct ec_node **table = NULL;
188         size_t n, i;
189
190         children = ec_config_dict_get(config, "children");
191         if (children == NULL) {
192                 errno = EINVAL;
193                 goto fail;
194         }
195
196         n = 0;
197         TAILQ_FOREACH(child, &children->list, next)
198                 n++;
199
200         table = ec_malloc(n * sizeof(*table));
201         if (table == NULL)
202                 goto fail;
203
204         n = 0;
205         TAILQ_FOREACH(child, &children->list, next) {
206                 table[n] = ec_node_clone(child->node);
207                 n++;
208         }
209
210         for (i = 0; i < node->len; i++)
211                 ec_node_free(node->table[i]);
212         ec_free(node->table);
213         node->table = table;
214         node->len = n;
215
216         return 0;
217
218 fail:
219         if (table != NULL) {
220                 for (i = 0; i < n; i++)
221                         ec_node_free(table[i]);
222         }
223         ec_free(table);
224         return -1;
225 }
226
227 static size_t
228 ec_node_seq_get_children_count(const struct ec_node *gen_node)
229 {
230         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
231         return node->len;
232 }
233
234 static struct ec_node *
235 ec_node_seq_get_child(const struct ec_node *gen_node, size_t i)
236 {
237         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
238
239         if (i >= node->len)
240                 return NULL;
241
242         return node->table[i];
243 }
244
245 static unsigned int
246 ec_node_seq_get_child_refs(const struct ec_node *gen_node, size_t i)
247 {
248         (void)gen_node;
249         (void)i;
250
251         /* each child node is referenced twice: once in the config and
252          * once in the node->table[] */
253         return 2;
254 }
255
256 static struct ec_node_type ec_node_seq_type = {
257         .name = "seq",
258         .schema = ec_node_seq_schema,
259         .schema_len = EC_COUNT_OF(ec_node_seq_schema),
260         .set_config = ec_node_seq_set_config,
261         .parse = ec_node_seq_parse,
262         .complete = ec_node_seq_complete,
263         .size = sizeof(struct ec_node_seq),
264         .free_priv = ec_node_seq_free_priv,
265         .get_children_count = ec_node_seq_get_children_count,
266         .get_child = ec_node_seq_get_child,
267         .get_child_refs = ec_node_seq_get_child_refs,
268 };
269
270 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
271
272 int ec_node_seq_add(struct ec_node *gen_node, struct ec_node *child)
273 {
274         struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
275         const struct ec_config *cur_config = NULL;
276         struct ec_config *config = NULL, *children;
277         int ret;
278
279         assert(node != NULL);
280
281         /* XXX factorize this code in a helper */
282
283         if (ec_node_check_type(gen_node, &ec_node_seq_type) < 0)
284                 goto fail;
285
286         cur_config = ec_node_get_config(gen_node);
287         if (cur_config == NULL)
288                 config = ec_config_dict();
289         else
290                 config = ec_config_dup(cur_config);
291         if (config == NULL)
292                 goto fail;
293
294         children = ec_config_dict_get(config, "children");
295         if (children == NULL) {
296                 children = ec_config_list();
297                 if (children == NULL)
298                         goto fail;
299
300                 if (ec_config_dict_set(config, "children", children) < 0)
301                         goto fail; /* children list is freed on error */
302         }
303
304         if (ec_config_list_add(children, ec_config_node(child)) < 0) {
305                 child = NULL;
306                 goto fail;
307         }
308
309         ret = ec_node_set_config(gen_node, config);
310         config = NULL; /* freed */
311         if (ret < 0)
312                 goto fail;
313
314         return 0;
315
316 fail:
317         ec_config_free(config);
318         ec_node_free(child);
319         return -1;
320 }
321
322 struct ec_node *__ec_node_seq(const char *id, ...)
323 {
324         struct ec_config *config = NULL, *children = NULL;
325         struct ec_node *gen_node = NULL;
326         struct ec_node *child;
327         va_list ap;
328         int ret;
329
330         va_start(ap, id);
331         child = va_arg(ap, struct ec_node *);
332
333         gen_node = __ec_node(&ec_node_seq_type, id);
334         if (gen_node == NULL)
335                 goto fail_free_children;
336
337         config = ec_config_dict();
338         if (config == NULL)
339                 goto fail_free_children;
340
341         children = ec_config_list();
342         if (children == NULL)
343                 goto fail_free_children;
344
345         for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *)) {
346                 if (child == NULL)
347                         goto fail_free_children;
348
349                 if (ec_config_list_add(children, ec_config_node(child)) < 0) {
350                         child = NULL;
351                         goto fail_free_children;
352                 }
353         }
354
355         if (ec_config_dict_set(config, "children", children) < 0) {
356                 children = NULL; /* freed */
357                 goto fail;
358         }
359         children = NULL;
360
361         ret = ec_node_set_config(gen_node, config);
362         config = NULL; /* freed */
363         if (ret < 0)
364                 goto fail;
365
366         va_end(ap);
367
368         return gen_node;
369
370 fail_free_children:
371         for (; child != EC_NODE_ENDLIST; child = va_arg(ap, struct ec_node *))
372                 ec_node_free(child);
373 fail:
374         ec_node_free(gen_node); /* will also free added children */
375         ec_config_free(children);
376         ec_config_free(config);
377         va_end(ap);
378
379         return NULL;
380 }
381
382 /* LCOV_EXCL_START */
383 static int ec_node_seq_testcase(void)
384 {
385         struct ec_node *node = NULL;
386         int testres = 0;
387
388         node = EC_NODE_SEQ(EC_NO_ID,
389                 ec_node_str(EC_NO_ID, "foo"),
390                 ec_node_str(EC_NO_ID, "bar")
391         );
392         if (node == NULL) {
393                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
394                 return -1;
395         }
396         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar");
397         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "toto");
398         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
399         testres |= EC_TEST_CHECK_PARSE(node, -1, "foox", "bar");
400         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo", "barx");
401         testres |= EC_TEST_CHECK_PARSE(node, -1, "bar", "foo");
402         testres |= EC_TEST_CHECK_PARSE(node, -1, "", "foo");
403
404         testres |= (ec_node_seq_add(node, ec_node_str(EC_NO_ID, "grr")) < 0);
405         testres |= EC_TEST_CHECK_PARSE(node, 3, "foo", "bar", "grr");
406
407         ec_node_free(node);
408
409         /* test completion */
410         node = EC_NODE_SEQ(EC_NO_ID,
411                 ec_node_str(EC_NO_ID, "foo"),
412                 ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "toto")),
413                 ec_node_str(EC_NO_ID, "bar")
414         );
415         if (node == NULL) {
416                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
417                 return -1;
418         }
419         testres |= EC_TEST_CHECK_COMPLETE(node,
420                 "", EC_NODE_ENDLIST,
421                 "foo", EC_NODE_ENDLIST);
422         testres |= EC_TEST_CHECK_COMPLETE(node,
423                 "f", EC_NODE_ENDLIST,
424                 "foo", EC_NODE_ENDLIST);
425         testres |= EC_TEST_CHECK_COMPLETE(node,
426                 "foo", EC_NODE_ENDLIST,
427                 "foo", EC_NODE_ENDLIST);
428         testres |= EC_TEST_CHECK_COMPLETE(node,
429                 "foo", "", EC_NODE_ENDLIST,
430                 "bar", "toto", EC_NODE_ENDLIST);
431         testres |= EC_TEST_CHECK_COMPLETE(node,
432                 "foo", "t", EC_NODE_ENDLIST,
433                 "toto", EC_NODE_ENDLIST);
434         testres |= EC_TEST_CHECK_COMPLETE(node,
435                 "foo", "b", EC_NODE_ENDLIST,
436                 "bar", EC_NODE_ENDLIST);
437         testres |= EC_TEST_CHECK_COMPLETE(node,
438                 "foo", "bar", EC_NODE_ENDLIST,
439                 "bar", EC_NODE_ENDLIST);
440         testres |= EC_TEST_CHECK_COMPLETE(node,
441                 "x", EC_NODE_ENDLIST,
442                 EC_NODE_ENDLIST);
443         testres |= EC_TEST_CHECK_COMPLETE(node,
444                 "foobarx", EC_NODE_ENDLIST,
445                 EC_NODE_ENDLIST);
446         ec_node_free(node);
447
448         return testres;
449 }
450 /* LCOV_EXCL_STOP */
451
452 static struct ec_test ec_node_seq_test = {
453         .name = "node_seq",
454         .test = ec_node_seq_testcase,
455 };
456
457 EC_TEST_REGISTER(ec_node_seq_test);