rename __ec_node
[protos/libecoli.git] / lib / 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_node_many.h>
22
23 EC_LOG_TYPE_REGISTER(node_many);
24
25 struct ec_node_many {
26         struct ec_node gen;
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 *gen_node,
33                         struct ec_parse *state,
34                         const struct ec_strvec *strvec)
35 {
36         struct ec_node_many *node = (struct ec_node_many *)gen_node;
37         struct ec_parse *child_parse;
38         struct ec_strvec *childvec = NULL;
39         size_t off = 0, count;
40         int ret;
41
42         for (count = 0; node->max == 0 || count < node->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_node_parse_child(node->child, state, 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_parse_get_last_child(state);
61                         ec_parse_unlink_child(state, child_parse);
62                         ec_parse_free(child_parse);
63                         break;
64                 }
65
66                 off += ret;
67         }
68
69         if (count < node->min) {
70                 ec_parse_free_children(state);
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 *node, unsigned int max,
83                         struct ec_comp *comp,
84                         const struct ec_strvec *strvec)
85 {
86         struct ec_parse *parse = ec_comp_get_state(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_node_complete_child(node->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_node_parse_child(node->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_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_many_complete(node, max, comp, childvec);
131                 ec_parse_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 *gen_node,
148                 struct ec_comp *comp,
149                 const struct ec_strvec *strvec)
150 {
151         struct ec_node_many *node = (struct ec_node_many *)gen_node;
152
153         return __ec_node_many_complete(node, node->max, comp,
154                                 strvec);
155 }
156
157 static void ec_node_many_free_priv(struct ec_node *gen_node)
158 {
159         struct ec_node_many *node = (struct ec_node_many *)gen_node;
160
161         ec_node_free(node->child);
162 }
163
164 static size_t
165 ec_node_many_get_children_count(const struct ec_node *gen_node)
166 {
167         struct ec_node_many *node = (struct ec_node_many *)gen_node;
168
169         if (node->child)
170                 return 1;
171         return 0;
172 }
173
174 static int
175 ec_node_many_get_child(const struct ec_node *gen_node, size_t i,
176                 struct ec_node **child, unsigned int *refs)
177 {
178         struct ec_node_many *node = (struct ec_node_many *)gen_node;
179
180         if (i >= 1)
181                 return -1;
182
183         *child = node->child;
184         *refs = 1;
185         return 0;
186 }
187
188 static struct ec_node_type ec_node_many_type = {
189         .name = "many",
190         .parse = ec_node_many_parse,
191         .complete = ec_node_many_complete,
192         .size = sizeof(struct ec_node_many),
193         .free_priv = ec_node_many_free_priv,
194         .get_children_count = ec_node_many_get_children_count,
195         .get_child = ec_node_many_get_child,
196 };
197
198 EC_NODE_TYPE_REGISTER(ec_node_many_type);
199
200 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
201         unsigned int min, unsigned int max)
202 {
203         struct ec_node_many *node = NULL;
204
205         if (child == NULL)
206                 return NULL;
207
208         node = (struct ec_node_many *)ec_node_from_type(&ec_node_many_type, id);
209         if (node == NULL) {
210                 ec_node_free(child);
211                 return NULL;
212         }
213
214         node->child = child;
215         node->min = min;
216         node->max = max;
217
218         return &node->gen;
219 }
220
221 /* LCOV_EXCL_START */
222 static int ec_node_many_testcase(void)
223 {
224         struct ec_node *node;
225         int testres = 0;
226
227         node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 0, 0);
228         if (node == NULL) {
229                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
230                 return -1;
231         }
232         testres |= EC_TEST_CHECK_PARSE(node, 0);
233         testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
234         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
235         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
236         testres |= EC_TEST_CHECK_PARSE(node, 0);
237         ec_node_free(node);
238
239         node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 0);
240         if (node == NULL) {
241                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
242                 return -1;
243         }
244         testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
245         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
246         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
247         testres |= EC_TEST_CHECK_PARSE(node, -1);
248         ec_node_free(node);
249
250         node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 1, 2);
251         if (node == NULL) {
252                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
253                 return -1;
254         }
255         testres |= EC_TEST_CHECK_PARSE(node, -1, "bar");
256         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
257         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "bar");
258         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "foo", "foo");
259         testres |= EC_TEST_CHECK_PARSE(node, -1);
260         ec_node_free(node);
261
262         /* test completion */
263         node = ec_node_many(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"), 2, 4);
264         if (node == NULL) {
265                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
266                 return -1;
267         }
268         testres |= EC_TEST_CHECK_COMPLETE(node,
269                 "", EC_NODE_ENDLIST,
270                 "foo", EC_NODE_ENDLIST);
271         testres |= EC_TEST_CHECK_COMPLETE(node,
272                 "f", EC_NODE_ENDLIST,
273                 "foo", EC_NODE_ENDLIST);
274         testres |= EC_TEST_CHECK_COMPLETE(node,
275                 "foo", EC_NODE_ENDLIST,
276                 "foo", EC_NODE_ENDLIST);
277         testres |= EC_TEST_CHECK_COMPLETE(node,
278                 "foo", "", EC_NODE_ENDLIST,
279                 "foo", EC_NODE_ENDLIST);
280         testres |= EC_TEST_CHECK_COMPLETE(node,
281                 "foo", "foo", "", EC_NODE_ENDLIST,
282                 "foo", EC_NODE_ENDLIST);
283         testres |= EC_TEST_CHECK_COMPLETE(node,
284                 "foo", "foo", "foo", "", EC_NODE_ENDLIST,
285                 "foo", EC_NODE_ENDLIST);
286         testres |= EC_TEST_CHECK_COMPLETE(node,
287                 "foo", "foo", "foo", "foo", "", EC_NODE_ENDLIST,
288                 EC_NODE_ENDLIST);
289         ec_node_free(node);
290
291         return testres;
292 }
293 /* LCOV_EXCL_STOP */
294
295 static struct ec_test ec_node_many_test = {
296         .name = "node_many",
297         .test = ec_node_many_testcase,
298 };
299
300 EC_TEST_REGISTER(ec_node_many_test);