use config for cmd node
[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         unsigned int 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 struct ec_node_type ec_node_subset_type = {
245         .name = "subset",
246         .parse = ec_node_subset_parse,
247         .complete = ec_node_subset_complete,
248         .size = sizeof(struct ec_node_subset),
249         .free_priv = ec_node_subset_free_priv,
250 };
251
252 EC_NODE_TYPE_REGISTER(ec_node_subset_type);
253
254 int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child)
255 {
256         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
257         struct ec_node **table;
258
259         assert(node != NULL); // XXX specific assert for it, like in libyang
260
261         if (child == NULL) {
262                 errno = EINVAL;
263                 goto fail;
264         }
265
266         if (ec_node_check_type(gen_node, &ec_node_subset_type) < 0)
267                 goto fail;
268
269         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
270         if (table == NULL) {
271                 ec_node_free(child);
272                 return -1;
273         }
274
275         node->table = table;
276
277         if (ec_node_add_child(gen_node, child) < 0)
278                 goto fail;
279
280         table[node->len] = child;
281         node->len++;
282
283         return 0;
284
285 fail:
286         ec_node_free(child);
287         return -1;
288 }
289
290 struct ec_node *__ec_node_subset(const char *id, ...)
291 {
292         struct ec_node *gen_node = NULL;
293         struct ec_node_subset *node = NULL;
294         struct ec_node *child;
295         va_list ap;
296         int fail = 0;
297
298         va_start(ap, id);
299
300         gen_node = __ec_node(&ec_node_subset_type, id);
301         node = (struct ec_node_subset *)gen_node;
302         if (node == NULL)
303                 fail = 1;;
304
305         for (child = va_arg(ap, struct ec_node *);
306              child != EC_NODE_ENDLIST;
307              child = va_arg(ap, struct ec_node *)) {
308
309                 /* on error, don't quit the loop to avoid leaks */
310                 if (fail == 1 || child == NULL ||
311                                 ec_node_subset_add(gen_node, child) < 0) {
312                         fail = 1;
313                         ec_node_free(child);
314                 }
315         }
316
317         if (fail == 1)
318                 goto fail;
319
320         va_end(ap);
321         return gen_node;
322
323 fail:
324         ec_node_free(gen_node); /* will also free children */
325         va_end(ap);
326         return NULL;
327 }
328
329 /* LCOV_EXCL_START */
330 static int ec_node_subset_testcase(void)
331 {
332         struct ec_node *node;
333         int testres = 0;
334
335         node = EC_NODE_SUBSET(EC_NO_ID,
336                 EC_NODE_OR(EC_NO_ID,
337                         ec_node_str(EC_NO_ID, "foo"),
338                         ec_node_str(EC_NO_ID, "bar")),
339                 ec_node_str(EC_NO_ID, "bar"),
340                 ec_node_str(EC_NO_ID, "toto")
341         );
342         if (node == NULL) {
343                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
344                 return -1;
345         }
346         testres |= EC_TEST_CHECK_PARSE(node, 0);
347         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
348         testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
349         testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "titi");
350         testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "toto");
351         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
352         testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "bar");
353         testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo");
354         testres |= EC_TEST_CHECK_PARSE(node, 0, " ");
355         testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
356         ec_node_free(node);
357
358         /* test completion */
359         node = EC_NODE_SUBSET(EC_NO_ID,
360                 ec_node_str(EC_NO_ID, "foo"),
361                 ec_node_str(EC_NO_ID, "bar"),
362                 ec_node_str(EC_NO_ID, "bar2"),
363                 ec_node_str(EC_NO_ID, "toto"),
364                 ec_node_str(EC_NO_ID, "titi")
365         );
366         if (node == NULL) {
367                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
368                 return -1;
369         }
370         testres |= EC_TEST_CHECK_COMPLETE(node,
371                 "", EC_NODE_ENDLIST,
372                 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
373         testres |= EC_TEST_CHECK_COMPLETE(node,
374                 "", EC_NODE_ENDLIST,
375                 "bar2", "bar", "foo", "toto", "titi", EC_NODE_ENDLIST);
376         testres |= EC_TEST_CHECK_COMPLETE(node,
377                 "bar", "bar2", "", EC_NODE_ENDLIST,
378                 "foo", "toto", "titi", EC_NODE_ENDLIST);
379         testres |= EC_TEST_CHECK_COMPLETE(node,
380                 "f", EC_NODE_ENDLIST,
381                 "foo", EC_NODE_ENDLIST);
382         testres |= EC_TEST_CHECK_COMPLETE(node,
383                 "b", EC_NODE_ENDLIST,
384                 "bar", "bar2", EC_NODE_ENDLIST);
385         testres |= EC_TEST_CHECK_COMPLETE(node,
386                 "bar", EC_NODE_ENDLIST,
387                 "bar", "bar2", EC_NODE_ENDLIST);
388         testres |= EC_TEST_CHECK_COMPLETE(node,
389                 "bar", "b", EC_NODE_ENDLIST,
390                 "bar2", EC_NODE_ENDLIST);
391         testres |= EC_TEST_CHECK_COMPLETE(node,
392                 "t", EC_NODE_ENDLIST,
393                 "toto", "titi", EC_NODE_ENDLIST);
394         testres |= EC_TEST_CHECK_COMPLETE(node,
395                 "to", EC_NODE_ENDLIST,
396                 "toto", EC_NODE_ENDLIST);
397         testres |= EC_TEST_CHECK_COMPLETE(node,
398                 "x", EC_NODE_ENDLIST,
399                 EC_NODE_ENDLIST);
400         ec_node_free(node);
401
402         return testres;
403 }
404 /* LCOV_EXCL_STOP */
405
406 static struct ec_test ec_node_subset_test = {
407         .name = "node_subset",
408         .test = ec_node_subset_testcase,
409 };
410
411 EC_TEST_REGISTER(ec_node_subset_test);