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