free children in free_priv
[protos/libecoli.git] / lib / ecoli_node_subset.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 #include <stdbool.h>
12
13 #include <ecoli_malloc.h>
14 #include <ecoli_log.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_subset.h>
20 #include <ecoli_node_str.h>
21 #include <ecoli_node_or.h>
22 #include <ecoli_test.h>
23
24 EC_LOG_TYPE_REGISTER(node_subset);
25
26 struct ec_node_subset {
27         struct ec_node gen;
28         struct ec_node **table;
29         unsigned int len;
30 };
31
32 struct parse_result {
33         size_t parse_len;           /* number of parsed nodes */
34         size_t len;                 /* consumed strings */
35 };
36
37 /* recursively find the longest list of nodes that matches: the state is
38  * updated accordingly. */
39 static int
40 __ec_node_subset_parse(struct parse_result *out, struct ec_node **table,
41                 size_t table_len, struct ec_parse *state,
42                 const struct ec_strvec *strvec)
43 {
44         struct ec_node **child_table;
45         struct ec_strvec *childvec = NULL;
46         size_t i, j, len = 0;
47         struct parse_result best_result, result;
48         struct ec_parse *best_parse = NULL;
49         int ret;
50
51         if (table_len == 0)
52                 return 0;
53
54         memset(&best_result, 0, sizeof(best_result));
55
56         child_table = ec_calloc(table_len - 1, sizeof(*child_table));
57         if (child_table == NULL)
58                 goto fail;
59
60         for (i = 0; i < table_len; i++) {
61                 /* try to parse elt i */
62                 ret = ec_node_parse_child(table[i], state, strvec);
63                 if (ret < 0)
64                         goto fail;
65
66                 if (ret == EC_PARSE_NOMATCH)
67                         continue;
68
69                 /* build a new table without elt i */
70                 for (j = 0; j < table_len; j++) {
71                         if (j < i)
72                                 child_table[j] = table[j];
73                         else if (j > i)
74                                 child_table[j - 1] = table[j];
75                 }
76
77                 /* build a new strvec (ret is the len of matched strvec) */
78                 len = ret;
79                 childvec = ec_strvec_ndup(strvec, len,
80                                         ec_strvec_len(strvec) - len);
81                 if (childvec == NULL)
82                         goto fail;
83
84                 memset(&result, 0, sizeof(result));
85                 ret = __ec_node_subset_parse(&result, child_table,
86                                         table_len - 1, state, childvec);
87                 ec_strvec_free(childvec);
88                 childvec = NULL;
89                 if (ret < 0)
90                         goto fail;
91
92                 /* if result is not the best, ignore */
93                 if (result.parse_len < best_result.parse_len) {
94                         memset(&result, 0, sizeof(result));
95                         ec_parse_del_last_child(state);
96                         continue;
97                 }
98
99                 /* replace the previous best result */
100                 ec_parse_free(best_parse);
101                 best_parse = ec_parse_get_last_child(state);
102                 ec_parse_unlink_child(state, best_parse);
103
104                 best_result.parse_len = result.parse_len + 1;
105                 best_result.len = len + result.len;
106
107                 memset(&result, 0, sizeof(result));
108         }
109
110         *out = best_result;
111         ec_free(child_table);
112         if (best_parse != NULL)
113                 ec_parse_link_child(state, best_parse);
114
115         return 0;
116
117  fail:
118         ec_parse_free(best_parse);
119         ec_strvec_free(childvec);
120         ec_free(child_table);
121         return -1;
122 }
123
124 static int
125 ec_node_subset_parse(const struct ec_node *gen_node,
126                 struct ec_parse *state,
127                 const struct ec_strvec *strvec)
128 {
129         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
130         struct ec_parse *parse = NULL;
131         struct parse_result result;
132         int ret;
133
134         memset(&result, 0, sizeof(result));
135
136         ret = __ec_node_subset_parse(&result, node->table,
137                                 node->len, state, strvec);
138         if (ret < 0)
139                 goto fail;
140
141         /* if no child node matches, return a matching empty strvec */
142         if (result.parse_len == 0)
143                 return 0;
144
145         return result.len;
146
147  fail:
148         ec_parse_free(parse);
149         return ret;
150 }
151
152 static int
153 __ec_node_subset_complete(struct ec_node **table, size_t table_len,
154                         struct ec_comp *comp,
155                         const struct ec_strvec *strvec)
156 {
157         struct ec_parse *parse = ec_comp_get_state(comp);
158         struct ec_strvec *childvec = NULL;
159         struct ec_node *save;
160         size_t i, len;
161         int ret;
162
163         /*
164          * example with table = [a, b, c]
165          * subset_complete([a,b,c], strvec) returns:
166          *   complete(a, strvec) + complete(b, strvec) + complete(c, strvec) +
167          *   + __subset_complete([b, c], childvec) if a matches
168          *   + __subset_complete([a, c], childvec) if b matches
169          *   + __subset_complete([a, b], childvec) if c matches
170          */
171
172         /* first, try to complete with each node of the table */
173         for (i = 0; i < table_len; i++) {
174                 if (table[i] == NULL)
175                         continue;
176
177                 ret = ec_node_complete_child(table[i],
178                                         comp, strvec);
179                 if (ret < 0)
180                         goto fail;
181         }
182
183         /* then, if a node matches, advance in strvec and try to complete with
184          * all the other nodes */
185         for (i = 0; i < table_len; i++) {
186                 if (table[i] == NULL)
187                         continue;
188
189                 ret = ec_node_parse_child(table[i], parse, strvec);
190                 if (ret < 0)
191                         goto fail;
192
193                 if (ret == EC_PARSE_NOMATCH)
194                         continue;
195
196                 len = ret;
197                 childvec = ec_strvec_ndup(strvec, len,
198                                         ec_strvec_len(strvec) - len);
199                 if (childvec == NULL) {
200                         ec_parse_del_last_child(parse);
201                         goto fail;
202                 }
203
204                 save = table[i];
205                 table[i] = NULL;
206                 ret = __ec_node_subset_complete(table, table_len,
207                                                 comp, childvec);
208                 table[i] = save;
209                 ec_strvec_free(childvec);
210                 childvec = NULL;
211                 ec_parse_del_last_child(parse);
212
213                 if (ret < 0)
214                         goto fail;
215         }
216
217         return 0;
218
219 fail:
220         return -1;
221 }
222
223 static int
224 ec_node_subset_complete(const struct ec_node *gen_node,
225                         struct ec_comp *comp,
226                         const struct ec_strvec *strvec)
227 {
228         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
229
230         return __ec_node_subset_complete(node->table, node->len, comp,
231                                         strvec);
232 }
233
234 static void ec_node_subset_free_priv(struct ec_node *gen_node)
235 {
236         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
237         size_t i;
238
239         for (i = 0; i < node->len; i++)
240                 ec_node_free(node->table[i]);
241         ec_free(node->table);
242 }
243
244 static size_t
245 ec_node_subset_get_children_count(const struct ec_node *gen_node)
246 {
247         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
248         return node->len;
249 }
250
251 static struct ec_node *
252 ec_node_subset_get_child(const struct ec_node *gen_node, size_t i)
253 {
254         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
255
256         if (i >= node->len)
257                 return NULL;
258
259         return node->table[i];
260 }
261
262 static struct ec_node_type ec_node_subset_type = {
263         .name = "subset",
264         .parse = ec_node_subset_parse,
265         .complete = ec_node_subset_complete,
266         .size = sizeof(struct ec_node_subset),
267         .free_priv = ec_node_subset_free_priv,
268         .get_children_count = ec_node_subset_get_children_count,
269         .get_child = ec_node_subset_get_child,
270 };
271
272 EC_NODE_TYPE_REGISTER(ec_node_subset_type);
273
274 int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child)
275 {
276         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
277         struct ec_node **table;
278
279         assert(node != NULL); // XXX specific assert for it, like in libyang
280
281         if (child == NULL) {
282                 errno = EINVAL;
283                 goto fail;
284         }
285
286         if (ec_node_check_type(gen_node, &ec_node_subset_type) < 0)
287                 goto fail;
288
289         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
290         if (table == NULL) {
291                 ec_node_free(child);
292                 return -1;
293         }
294
295         node->table = table;
296         table[node->len] = child;
297         node->len++;
298
299         return 0;
300
301 fail:
302         ec_node_free(child);
303         return -1;
304 }
305
306 struct ec_node *__ec_node_subset(const char *id, ...)
307 {
308         struct ec_node *gen_node = NULL;
309         struct ec_node_subset *node = NULL;
310         struct ec_node *child;
311         va_list ap;
312         int fail = 0;
313
314         va_start(ap, id);
315
316         gen_node = __ec_node(&ec_node_subset_type, id);
317         node = (struct ec_node_subset *)gen_node;
318         if (node == NULL)
319                 fail = 1;;
320
321         for (child = va_arg(ap, struct ec_node *);
322              child != EC_NODE_ENDLIST;
323              child = va_arg(ap, struct ec_node *)) {
324
325                 /* on error, don't quit the loop to avoid leaks */
326                 if (fail == 1 || child == NULL ||
327                                 ec_node_subset_add(gen_node, child) < 0) {
328                         fail = 1;
329                         ec_node_free(child);
330                 }
331         }
332
333         if (fail == 1)
334                 goto fail;
335
336         va_end(ap);
337         return gen_node;
338
339 fail:
340         ec_node_free(gen_node); /* will also free children */
341         va_end(ap);
342         return NULL;
343 }
344
345 /* LCOV_EXCL_START */
346 static int ec_node_subset_testcase(void)
347 {
348         struct ec_node *node;
349         int testres = 0;
350
351         node = EC_NODE_SUBSET(EC_NO_ID,
352                 EC_NODE_OR(EC_NO_ID,
353                         ec_node_str(EC_NO_ID, "foo"),
354                         ec_node_str(EC_NO_ID, "bar")),
355                 ec_node_str(EC_NO_ID, "bar"),
356                 ec_node_str(EC_NO_ID, "toto")
357         );
358         if (node == NULL) {
359                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
360                 return -1;
361         }
362         testres |= EC_TEST_CHECK_PARSE(node, 0);
363         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
364         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
365         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "titi");
366         testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "toto");
367         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
368         testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "bar");
369         testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo");
370         testres |= EC_TEST_CHECK_PARSE(node, 0, " ");
371         testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
372         ec_node_free(node);
373
374         /* test completion */
375         node = EC_NODE_SUBSET(EC_NO_ID,
376                 ec_node_str(EC_NO_ID, "foo"),
377                 ec_node_str(EC_NO_ID, "bar"),
378                 ec_node_str(EC_NO_ID, "bar2"),
379                 ec_node_str(EC_NO_ID, "toto"),
380                 ec_node_str(EC_NO_ID, "titi")
381         );
382         if (node == NULL) {
383                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
384                 return -1;
385         }
386         testres |= EC_TEST_CHECK_COMPLETE(node,
387                 "", EC_NODE_ENDLIST,
388                 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
389         testres |= EC_TEST_CHECK_COMPLETE(node,
390                 "", EC_NODE_ENDLIST,
391                 "bar2", "bar", "foo", "toto", "titi", EC_NODE_ENDLIST);
392         testres |= EC_TEST_CHECK_COMPLETE(node,
393                 "bar", "bar2", "", EC_NODE_ENDLIST,
394                 "foo", "toto", "titi", EC_NODE_ENDLIST);
395         testres |= EC_TEST_CHECK_COMPLETE(node,
396                 "f", EC_NODE_ENDLIST,
397                 "foo", EC_NODE_ENDLIST);
398         testres |= EC_TEST_CHECK_COMPLETE(node,
399                 "b", EC_NODE_ENDLIST,
400                 "bar", "bar2", EC_NODE_ENDLIST);
401         testres |= EC_TEST_CHECK_COMPLETE(node,
402                 "bar", EC_NODE_ENDLIST,
403                 "bar", "bar2", EC_NODE_ENDLIST);
404         testres |= EC_TEST_CHECK_COMPLETE(node,
405                 "bar", "b", EC_NODE_ENDLIST,
406                 "bar2", EC_NODE_ENDLIST);
407         testres |= EC_TEST_CHECK_COMPLETE(node,
408                 "t", EC_NODE_ENDLIST,
409                 "toto", "titi", EC_NODE_ENDLIST);
410         testres |= EC_TEST_CHECK_COMPLETE(node,
411                 "to", EC_NODE_ENDLIST,
412                 "toto", EC_NODE_ENDLIST);
413         testres |= EC_TEST_CHECK_COMPLETE(node,
414                 "x", EC_NODE_ENDLIST,
415                 EC_NODE_ENDLIST);
416         ec_node_free(node);
417
418         return testres;
419 }
420 /* LCOV_EXCL_STOP */
421
422 static struct ec_test ec_node_subset_test = {
423         .name = "node_subset",
424         .test = ec_node_subset_testcase,
425 };
426
427 EC_TEST_REGISTER(ec_node_subset_test);