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