54c100cfa1e27143ee1247b41f525698fbb8956a
[protos/libecoli.git] / src / ecoli_node_many.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 <string.h>
8 #include <assert.h>
9 #include <stdarg.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_node.h>
17 #include <ecoli_parse.h>
18 #include <ecoli_complete.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_option.h>
21 #include <ecoli_config.h>
22 #include <ecoli_node_many.h>
23
24 EC_LOG_TYPE_REGISTER(node_many);
25
26 struct ec_node_many {
27         unsigned int min;
28         unsigned int max;
29         struct ec_node *child;
30 };
31
32 static int ec_node_many_parse(const struct ec_node *node,
33                         struct ec_pnode *pstate,
34                         const struct ec_strvec *strvec)
35 {
36         struct ec_node_many *priv = ec_node_priv(node);
37         struct ec_pnode *child_parse;
38         struct ec_strvec *childvec = NULL;
39         size_t off = 0, count;
40         int ret;
41
42         for (count = 0; priv->max == 0 || count < priv->max; count++) {
43                 childvec = ec_strvec_ndup(strvec, off,
44                         ec_strvec_len(strvec) - off);
45                 if (childvec == NULL)
46                         goto fail;
47
48                 ret = ec_parse_child(priv->child, pstate, childvec);
49                 if (ret < 0)
50                         goto fail;
51
52                 ec_strvec_free(childvec);
53                 childvec = NULL;
54
55                 if (ret == EC_PARSE_NOMATCH)
56                         break;
57
58                 /* it matches an empty strvec, no need to continue */
59                 if (ret == 0) {
60                         child_parse = ec_pnode_get_last_child(pstate);
61                         ec_pnode_unlink_child(pstate, child_parse);
62                         ec_pnode_free(child_parse);
63                         break;
64                 }
65
66                 off += ret;
67         }
68
69         if (count < priv->min) {
70                 ec_pnode_free_children(pstate);
71                 return EC_PARSE_NOMATCH;
72         }
73
74         return off;
75
76 fail:
77         ec_strvec_free(childvec);
78         return -1;
79 }
80
81 static int
82 __ec_node_many_complete(struct ec_node_many *priv, unsigned int max,
83                         struct ec_comp *comp,
84                         const struct ec_strvec *strvec)
85 {
86         struct ec_pnode *parse = ec_comp_get_cur_pstate(comp);
87         struct ec_strvec *childvec = NULL;
88         unsigned int i;
89         int ret;
90
91         /* first, try to complete with the child node */
92         ret = ec_complete_child(priv->child, comp, strvec);
93         if (ret < 0)
94                 goto fail;
95
96         /* we're done, we reached the max number of nodes */
97         if (max == 1)
98                 return 0;
99
100         /* if there is a maximum, decrease it before recursion */
101         if (max != 0)
102                 max--;
103
104         /* then, if the node matches the beginning of the strvec, try to
105          * 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_parse_child(priv->child, 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_pnode_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_pnode_del_last_child(parse);
127                         goto fail;
128                 }
129
130                 ret = __ec_node_many_complete(priv, max, comp, childvec);
131                 ec_pnode_del_last_child(parse);
132                 ec_strvec_free(childvec);
133                 childvec = NULL;
134
135                 if (ret < 0)
136                         goto fail;
137         }
138
139         return 0;
140
141 fail:
142         ec_strvec_free(childvec);
143         return -1;
144 }
145
146 static int
147 ec_node_many_complete(const struct ec_node *node,
148                 struct ec_comp *comp,
149                 const struct ec_strvec *strvec)
150 {
151         struct ec_node_many *priv = ec_node_priv(node);
152
153         return __ec_node_many_complete(priv, priv->max, comp,
154                                 strvec);
155 }
156
157 static void ec_node_many_free_priv(struct ec_node *node)
158 {
159         struct ec_node_many *priv = ec_node_priv(node);
160
161         ec_node_free(priv->child);
162 }
163
164 static size_t
165 ec_node_many_get_children_count(const struct ec_node *node)
166 {
167         struct ec_node_many *priv = ec_node_priv(node);
168
169         if (priv->child)
170                 return 1;
171         return 0;
172 }
173
174 static int
175 ec_node_many_get_child(const struct ec_node *node, size_t i,
176                 struct ec_node **child, unsigned int *refs)
177 {
178         struct ec_node_many *priv = ec_node_priv(node);
179
180         if (i >= 1)
181                 return -1;
182
183         *child = priv->child;
184         *refs = 2;
185         return 0;
186 }
187
188 static const struct ec_config_schema ec_node_many_schema[] = {
189         {
190                 .key = "child",
191                 .desc = "The child node.",
192                 .type = EC_CONFIG_TYPE_NODE,
193         },
194         {
195                 .key = "min",
196                 .desc = "The minimum number of matches (default = 0).",
197                 .type = EC_CONFIG_TYPE_UINT64,
198         },
199         {
200                 .key = "max",
201                 .desc = "The maximum number of matches. If 0, there is "
202                 "no maximum (default = 0).",
203                 .type = EC_CONFIG_TYPE_UINT64,
204         },
205         {
206                 .type = EC_CONFIG_TYPE_NONE,
207         },
208 };
209
210 static int ec_node_many_set_config(struct ec_node *node,
211                                 const struct ec_config *config)
212 {
213         struct ec_node_many *priv = ec_node_priv(node);
214         const struct ec_config *child, *min, *max;
215
216         child = ec_config_dict_get(config, "child");
217         if (child == NULL)
218                 goto fail;
219         if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
220                 errno = EINVAL;
221                 goto fail;
222         }
223         min = ec_config_dict_get(config, "min");
224         if (min != NULL && (ec_config_get_type(min) != EC_CONFIG_TYPE_UINT64 ||
225                                 min->u64 >= UINT_MAX)) {
226                 errno = EINVAL;
227                 goto fail;
228         }
229         max = ec_config_dict_get(config, "max");
230         if (max != NULL && (ec_config_get_type(max) != EC_CONFIG_TYPE_UINT64 ||
231                                 max->u64 >= UINT_MAX)) {
232                 errno = EINVAL;
233                 goto fail;
234         }
235
236         if (priv->child != NULL)
237                 ec_node_free(priv->child);
238         priv->child = ec_node_clone(child->node);
239         if (min == NULL)
240                 priv->min = 0;
241         else
242                 priv->min = min->u64;
243         if (max == NULL)
244                 priv->max = 0;
245         else
246                 priv->max = max->u64;
247
248         return 0;
249
250 fail:
251         return -1;
252 }
253
254 static struct ec_node_type ec_node_many_type = {
255         .name = "many",
256         .schema = ec_node_many_schema,
257         .set_config = ec_node_many_set_config,
258         .parse = ec_node_many_parse,
259         .complete = ec_node_many_complete,
260         .size = sizeof(struct ec_node_many),
261         .free_priv = ec_node_many_free_priv,
262         .get_children_count = ec_node_many_get_children_count,
263         .get_child = ec_node_many_get_child,
264 };
265
266 EC_NODE_TYPE_REGISTER(ec_node_many_type);
267
268 int
269 ec_node_many_set_params(struct ec_node *node, struct ec_node *child,
270         unsigned int min, unsigned int max)
271 {
272         const struct ec_config *cur_config = NULL;
273         struct ec_config *config = NULL;
274         int ret;
275
276         if (ec_node_check_type(node, &ec_node_many_type) < 0)
277                 goto fail;
278
279         cur_config = ec_node_get_config(node);
280         if (cur_config == NULL)
281                 config = ec_config_dict();
282         else
283                 config = ec_config_dup(cur_config);
284         if (config == NULL)
285                 goto fail;
286
287         if (ec_config_dict_set(config, "child", ec_config_node(child)) < 0) {
288                 child = NULL; /* freed */
289                 goto fail;
290         }
291         child = NULL; /* freed */
292
293         if (ec_config_dict_set(config, "min", ec_config_u64(min)) < 0)
294                 goto fail;
295         if (ec_config_dict_set(config, "max", ec_config_u64(max)) < 0)
296                 goto fail;
297
298         ret = ec_node_set_config(node, config);
299         config = NULL; /* freed */
300         if (ret < 0)
301                 goto fail;
302
303         return 0;
304
305 fail:
306         ec_config_free(config);
307         ec_node_free(child);
308         return -1;
309 }
310
311 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
312         unsigned int min, unsigned int max)
313 {
314         struct ec_node *node = NULL;
315
316         if (child == NULL)
317                 return NULL;
318
319         node = ec_node_from_type(&ec_node_many_type, id);
320         if (node == NULL)
321                 goto fail;
322
323         if (ec_node_many_set_params(node, child, min, max) < 0) {
324                 child = NULL;
325                 goto fail;
326         }
327         child = NULL;
328
329         return node;
330
331 fail:
332         ec_node_free(node);
333         ec_node_free(child);
334         return NULL;
335 }
336
337 /* LCOV_EXCL_START */
338 static int ec_node_many_testcase(void)
339 {
340         struct ec_node *node;
341         int testres = 0;
342
343         node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 0, 0);
344         if (node == NULL) {
345                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
346                 return -1;
347         }
348         testres |= EC_TEST_CHECK_PARSE(node, 0);
349         testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
350         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
351         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
352         testres |= EC_TEST_CHECK_PARSE(node, 0);
353         ec_node_free(node);
354
355         node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 0);
356         if (node == NULL) {
357                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
358                 return -1;
359         }
360         testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
361         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
362         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
363         testres |= EC_TEST_CHECK_PARSE(node, -1);
364         ec_node_free(node);
365
366         node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 2);
367         if (node == NULL) {
368                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
369                 return -1;
370         }
371         testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
372         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
373         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
374         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
375         testres |= EC_TEST_CHECK_PARSE(node, -1);
376         ec_node_free(node);
377
378         /* test completion */
379         node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 2, 4);
380         if (node == NULL) {
381                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
382                 return -1;
383         }
384         testres |= EC_TEST_CHECK_COMPLETE(node,
385                 "", EC_VA_END,
386                 "foo", EC_VA_END);
387         testres |= EC_TEST_CHECK_COMPLETE(node,
388                 "f", EC_VA_END,
389                 "foo", EC_VA_END);
390         testres |= EC_TEST_CHECK_COMPLETE(node,
391                 "foo", EC_VA_END,
392                 "foo", EC_VA_END);
393         testres |= EC_TEST_CHECK_COMPLETE(node,
394                 "foo", "", EC_VA_END,
395                 "foo", EC_VA_END);
396         testres |= EC_TEST_CHECK_COMPLETE(node,
397                 "foo", "foo", "", EC_VA_END,
398                 "foo", EC_VA_END);
399         testres |= EC_TEST_CHECK_COMPLETE(node,
400                 "foo", "foo", "foo", "", EC_VA_END,
401                 "foo", EC_VA_END);
402         testres |= EC_TEST_CHECK_COMPLETE(node,
403                 "foo", "foo", "foo", "foo", "", EC_VA_END,
404                 EC_VA_END);
405         ec_node_free(node);
406
407         return testres;
408 }
409 /* LCOV_EXCL_STOP */
410
411 static struct ec_test ec_node_many_test = {
412         .name = "node_many",
413         .test = ec_node_many_testcase,
414 };
415
416 EC_TEST_REGISTER(ec_node_many_test);