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);
244 static struct ec_node_type ec_node_subset_type = {
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,
252 EC_NODE_TYPE_REGISTER(ec_node_subset_type);
254 int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child)
256 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
257 struct ec_node **table;
259 assert(node != NULL); // XXX specific assert for it, like in libyang
266 if (ec_node_check_type(gen_node, &ec_node_subset_type) < 0)
269 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
277 if (ec_node_add_child(gen_node, child) < 0)
280 table[node->len] = child;
290 struct ec_node *__ec_node_subset(const char *id, ...)
292 struct ec_node *gen_node = NULL;
293 struct ec_node_subset *node = NULL;
294 struct ec_node *child;
300 gen_node = __ec_node(&ec_node_subset_type, id);
301 node = (struct ec_node_subset *)gen_node;
305 for (child = va_arg(ap, struct ec_node *);
306 child != EC_NODE_ENDLIST;
307 child = va_arg(ap, struct ec_node *)) {
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) {
324 ec_node_free(gen_node); /* will also free children */
329 /* LCOV_EXCL_START */
330 static int ec_node_subset_testcase(void)
332 struct ec_node *node;
335 node = EC_NODE_SUBSET(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")
343 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
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")
367 EC_LOG(EC_LOG_ERR, "cannot create node\n");
370 testres |= EC_TEST_CHECK_COMPLETE(node,
372 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
373 testres |= EC_TEST_CHECK_COMPLETE(node,
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,
406 static struct ec_test ec_node_subset_test = {
407 .name = "node_subset",
408 .test = ec_node_subset_testcase,
411 EC_TEST_REGISTER(ec_node_subset_test);