1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
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>
24 EC_LOG_TYPE_REGISTER(node_subset);
26 struct ec_node_subset {
28 struct ec_node **table;
33 size_t parse_len; /* number of parsed nodes */
34 size_t len; /* consumed strings */
37 /* recursively find the longest list of nodes that matches: the state is
38 * updated accordingly. */
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)
44 struct ec_node **child_table;
45 struct ec_strvec *childvec = NULL;
47 struct parse_result best_result, result;
48 struct ec_parse *best_parse = NULL;
54 memset(&best_result, 0, sizeof(best_result));
56 child_table = ec_calloc(table_len - 1, sizeof(*child_table));
57 if (child_table == NULL)
60 for (i = 0; i < table_len; i++) {
61 /* try to parse elt i */
62 ret = ec_node_parse_child(table[i], state, strvec);
66 if (ret == EC_PARSE_NOMATCH)
69 /* build a new table without elt i */
70 for (j = 0; j < table_len; j++) {
72 child_table[j] = table[j];
74 child_table[j - 1] = table[j];
77 /* build a new strvec (ret is the len of matched strvec) */
79 childvec = ec_strvec_ndup(strvec, len,
80 ec_strvec_len(strvec) - len);
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);
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);
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);
104 best_result.parse_len = result.parse_len + 1;
105 best_result.len = len + result.len;
107 memset(&result, 0, sizeof(result));
111 ec_free(child_table);
112 if (best_parse != NULL)
113 ec_parse_link_child(state, best_parse);
118 ec_parse_free(best_parse);
119 ec_strvec_free(childvec);
120 ec_free(child_table);
125 ec_node_subset_parse(const struct ec_node *gen_node,
126 struct ec_parse *state,
127 const struct ec_strvec *strvec)
129 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
130 struct ec_parse *parse = NULL;
131 struct parse_result result;
134 memset(&result, 0, sizeof(result));
136 ret = __ec_node_subset_parse(&result, node->table,
137 node->len, state, strvec);
141 /* if no child node matches, return a matching empty strvec */
142 if (result.parse_len == 0)
148 ec_parse_free(parse);
153 __ec_node_subset_complete(struct ec_node **table, size_t table_len,
154 struct ec_comp *comp,
155 const struct ec_strvec *strvec)
157 struct ec_parse *parse = ec_comp_get_state(comp);
158 struct ec_strvec *childvec = NULL;
159 struct ec_node *save;
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
172 /* first, try to complete with each node of the table */
173 for (i = 0; i < table_len; i++) {
174 if (table[i] == NULL)
177 ret = ec_node_complete_child(table[i],
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)
189 ret = ec_node_parse_child(table[i], parse, strvec);
193 if (ret == EC_PARSE_NOMATCH)
197 childvec = ec_strvec_ndup(strvec, len,
198 ec_strvec_len(strvec) - len);
199 if (childvec == NULL) {
200 ec_parse_del_last_child(parse);
206 ret = __ec_node_subset_complete(table, table_len,
209 ec_strvec_free(childvec);
211 ec_parse_del_last_child(parse);
224 ec_node_subset_complete(const struct ec_node *gen_node,
225 struct ec_comp *comp,
226 const struct ec_strvec *strvec)
228 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
230 return __ec_node_subset_complete(node->table, node->len, comp,
234 static void ec_node_subset_free_priv(struct ec_node *gen_node)
236 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
239 for (i = 0; i < node->len; i++)
240 ec_node_free(node->table[i]);
241 ec_free(node->table);
245 ec_node_subset_get_children_count(const struct ec_node *gen_node)
247 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
252 ec_node_subset_get_child(const struct ec_node *gen_node, size_t i,
253 struct ec_node **child, unsigned int *refs)
255 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
260 *child = node->table[i];
265 static struct ec_node_type ec_node_subset_type = {
267 .parse = ec_node_subset_parse,
268 .complete = ec_node_subset_complete,
269 .size = sizeof(struct ec_node_subset),
270 .free_priv = ec_node_subset_free_priv,
271 .get_children_count = ec_node_subset_get_children_count,
272 .get_child = ec_node_subset_get_child,
275 EC_NODE_TYPE_REGISTER(ec_node_subset_type);
277 int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child)
279 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
280 struct ec_node **table;
282 assert(node != NULL); // XXX specific assert for it, like in libyang
289 if (ec_node_check_type(gen_node, &ec_node_subset_type) < 0)
292 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
299 table[node->len] = child;
309 struct ec_node *__ec_node_subset(const char *id, ...)
311 struct ec_node *gen_node = NULL;
312 struct ec_node_subset *node = NULL;
313 struct ec_node *child;
319 gen_node = ec_node_from_type(&ec_node_subset_type, id);
320 node = (struct ec_node_subset *)gen_node;
324 for (child = va_arg(ap, struct ec_node *);
325 child != EC_NODE_ENDLIST;
326 child = va_arg(ap, struct ec_node *)) {
328 /* on error, don't quit the loop to avoid leaks */
329 if (fail == 1 || child == NULL ||
330 ec_node_subset_add(gen_node, child) < 0) {
343 ec_node_free(gen_node); /* will also free children */
348 /* LCOV_EXCL_START */
349 static int ec_node_subset_testcase(void)
351 struct ec_node *node;
354 node = EC_NODE_SUBSET(EC_NO_ID,
356 ec_node_str(EC_NO_ID, "foo"),
357 ec_node_str(EC_NO_ID, "bar")),
358 ec_node_str(EC_NO_ID, "bar"),
359 ec_node_str(EC_NO_ID, "toto")
362 EC_LOG(EC_LOG_ERR, "cannot create node\n");
365 testres |= EC_TEST_CHECK_PARSE(node, 0);
366 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
367 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
368 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "titi");
369 testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "toto");
370 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
371 testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "bar");
372 testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo");
373 testres |= EC_TEST_CHECK_PARSE(node, 0, " ");
374 testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
377 /* test completion */
378 node = EC_NODE_SUBSET(EC_NO_ID,
379 ec_node_str(EC_NO_ID, "foo"),
380 ec_node_str(EC_NO_ID, "bar"),
381 ec_node_str(EC_NO_ID, "bar2"),
382 ec_node_str(EC_NO_ID, "toto"),
383 ec_node_str(EC_NO_ID, "titi")
386 EC_LOG(EC_LOG_ERR, "cannot create node\n");
389 testres |= EC_TEST_CHECK_COMPLETE(node,
391 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
392 testres |= EC_TEST_CHECK_COMPLETE(node,
394 "bar2", "bar", "foo", "toto", "titi", EC_NODE_ENDLIST);
395 testres |= EC_TEST_CHECK_COMPLETE(node,
396 "bar", "bar2", "", EC_NODE_ENDLIST,
397 "foo", "toto", "titi", EC_NODE_ENDLIST);
398 testres |= EC_TEST_CHECK_COMPLETE(node,
399 "f", EC_NODE_ENDLIST,
400 "foo", EC_NODE_ENDLIST);
401 testres |= EC_TEST_CHECK_COMPLETE(node,
402 "b", EC_NODE_ENDLIST,
403 "bar", "bar2", EC_NODE_ENDLIST);
404 testres |= EC_TEST_CHECK_COMPLETE(node,
405 "bar", EC_NODE_ENDLIST,
406 "bar", "bar2", EC_NODE_ENDLIST);
407 testres |= EC_TEST_CHECK_COMPLETE(node,
408 "bar", "b", EC_NODE_ENDLIST,
409 "bar2", EC_NODE_ENDLIST);
410 testres |= EC_TEST_CHECK_COMPLETE(node,
411 "t", EC_NODE_ENDLIST,
412 "toto", "titi", EC_NODE_ENDLIST);
413 testres |= EC_TEST_CHECK_COMPLETE(node,
414 "to", EC_NODE_ENDLIST,
415 "toto", EC_NODE_ENDLIST);
416 testres |= EC_TEST_CHECK_COMPLETE(node,
417 "x", EC_NODE_ENDLIST,
425 static struct ec_test ec_node_subset_test = {
426 .name = "node_subset",
427 .test = ec_node_subset_testcase,
430 EC_TEST_REGISTER(ec_node_subset_test);