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_parsed.h>
18 #include <ecoli_completed.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 parsed_len; /* number of parsed node */
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_parsed *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_parsed *best_parsed = 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) {
62 for (i = 0; i < table_len; i++) {
63 /* try to parse elt i */
64 ret = ec_node_parse_child(table[i], state, strvec);
68 if (ret == EC_PARSED_NOMATCH)
71 /* build a new table without elt i */
72 for (j = 0; j < table_len; j++) {
74 child_table[j] = table[j];
76 child_table[j - 1] = table[j];
79 /* build a new strvec (ret is the len of matched strvec) */
81 childvec = ec_strvec_ndup(strvec, len,
82 ec_strvec_len(strvec) - len);
83 if (childvec == NULL) {
88 memset(&result, 0, sizeof(result));
89 ret = __ec_node_subset_parse(&result, child_table,
90 table_len - 1, state, childvec);
91 ec_strvec_free(childvec);
96 /* if result is not the best, ignore */
97 if (result.parsed_len < best_result.parsed_len) {
98 memset(&result, 0, sizeof(result));
99 ec_parsed_del_last_child(state);
103 /* replace the previous best result */
104 ec_parsed_free(best_parsed);
105 best_parsed = ec_parsed_get_last_child(state);
106 ec_parsed_unlink_child(state, best_parsed);
108 best_result.parsed_len = result.parsed_len + 1;
109 best_result.len = len + result.len;
111 memset(&result, 0, sizeof(result));
115 ec_free(child_table);
116 if (best_parsed != NULL)
117 ec_parsed_link_child(state, best_parsed);
122 ec_parsed_free(best_parsed);
123 ec_strvec_free(childvec);
124 ec_free(child_table);
129 ec_node_subset_parse(const struct ec_node *gen_node,
130 struct ec_parsed *state,
131 const struct ec_strvec *strvec)
133 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
134 struct ec_parsed *parsed = NULL;
135 struct parse_result result;
138 memset(&result, 0, sizeof(result));
140 ret = __ec_node_subset_parse(&result, node->table,
141 node->len, state, strvec);
145 /* if no child node matches, return a matching empty strvec */
146 if (result.parsed_len == 0)
152 ec_parsed_free(parsed);
157 __ec_node_subset_complete(struct ec_node **table, size_t table_len,
158 struct ec_completed *completed,
159 const struct ec_strvec *strvec)
161 struct ec_parsed *parsed = ec_completed_get_state(completed);
162 struct ec_strvec *childvec = NULL;
163 struct ec_node *save;
168 * example with table = [a, b, c]
169 * subset_complete([a,b,c], strvec) returns:
170 * complete(a, strvec) + complete(b, strvec) + complete(c, strvec) +
171 * + __subset_complete([b, c], childvec) if a matches
172 * + __subset_complete([a, c], childvec) if b matches
173 * + __subset_complete([a, b], childvec) if c matches
176 /* first, try to complete with each node of the table */
177 for (i = 0; i < table_len; i++) {
178 if (table[i] == NULL)
181 ret = ec_node_complete_child(table[i],
187 /* then, if a node matches, advance in strvec and try to complete with
188 * all the other nodes */
189 for (i = 0; i < table_len; i++) {
190 if (table[i] == NULL)
193 ret = ec_node_parse_child(table[i], parsed, strvec);
197 if (ret == EC_PARSED_NOMATCH)
201 childvec = ec_strvec_ndup(strvec, len,
202 ec_strvec_len(strvec) - len);
203 if (childvec == NULL) {
204 ec_parsed_del_last_child(parsed);
210 ret = __ec_node_subset_complete(table, table_len,
211 completed, childvec);
213 ec_strvec_free(childvec);
215 ec_parsed_del_last_child(parsed);
228 ec_node_subset_complete(const struct ec_node *gen_node,
229 struct ec_completed *completed,
230 const struct ec_strvec *strvec)
232 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
234 return __ec_node_subset_complete(node->table, node->len, completed,
238 static void ec_node_subset_free_priv(struct ec_node *gen_node)
240 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
243 for (i = 0; i < node->len; i++)
244 ec_node_free(node->table[i]);
245 ec_free(node->table);
248 static struct ec_node_type ec_node_subset_type = {
250 .parse = ec_node_subset_parse,
251 .complete = ec_node_subset_complete,
252 .size = sizeof(struct ec_node_subset),
253 .free_priv = ec_node_subset_free_priv,
256 EC_NODE_TYPE_REGISTER(ec_node_subset_type);
258 int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child)
260 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
261 struct ec_node **table;
263 assert(node != NULL); // XXX specific assert for it, like in libyang
270 if (ec_node_check_type(gen_node, &ec_node_subset_type) < 0)
273 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
281 if (ec_node_add_child(gen_node, child) < 0)
284 table[node->len] = child;
294 struct ec_node *__ec_node_subset(const char *id, ...)
296 struct ec_node *gen_node = NULL;
297 struct ec_node_subset *node = NULL;
298 struct ec_node *child;
304 gen_node = __ec_node(&ec_node_subset_type, id);
305 node = (struct ec_node_subset *)gen_node;
309 for (child = va_arg(ap, struct ec_node *);
310 child != EC_NODE_ENDLIST;
311 child = va_arg(ap, struct ec_node *)) {
313 /* on error, don't quit the loop to avoid leaks */
314 if (fail == 1 || child == NULL ||
315 ec_node_subset_add(gen_node, child) < 0) {
328 ec_node_free(gen_node); /* will also free children */
333 /* LCOV_EXCL_START */
334 static int ec_node_subset_testcase(void)
336 struct ec_node *node;
339 node = EC_NODE_SUBSET(EC_NO_ID,
341 ec_node_str(EC_NO_ID, "foo"),
342 ec_node_str(EC_NO_ID, "bar")),
343 ec_node_str(EC_NO_ID, "bar"),
344 ec_node_str(EC_NO_ID, "toto")
347 EC_LOG(EC_LOG_ERR, "cannot create node\n");
350 testres |= EC_TEST_CHECK_PARSE(node, 0);
351 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
352 testres |= EC_TEST_CHECK_PARSE(node, 1, "bar");
353 testres |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "titi");
354 testres |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "toto");
355 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
356 testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "bar");
357 testres |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo");
358 testres |= EC_TEST_CHECK_PARSE(node, 0, " ");
359 testres |= EC_TEST_CHECK_PARSE(node, 0, "foox");
362 /* test completion */
363 node = EC_NODE_SUBSET(EC_NO_ID,
364 ec_node_str(EC_NO_ID, "foo"),
365 ec_node_str(EC_NO_ID, "bar"),
366 ec_node_str(EC_NO_ID, "bar2"),
367 ec_node_str(EC_NO_ID, "toto"),
368 ec_node_str(EC_NO_ID, "titi")
371 EC_LOG(EC_LOG_ERR, "cannot create node\n");
374 testres |= EC_TEST_CHECK_COMPLETE(node,
376 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
377 testres |= EC_TEST_CHECK_COMPLETE(node,
379 "bar2", "bar", "foo", "toto", "titi", EC_NODE_ENDLIST);
380 testres |= EC_TEST_CHECK_COMPLETE(node,
381 "bar", "bar2", "", EC_NODE_ENDLIST,
382 "foo", "toto", "titi", EC_NODE_ENDLIST);
383 testres |= EC_TEST_CHECK_COMPLETE(node,
384 "f", EC_NODE_ENDLIST,
385 "foo", EC_NODE_ENDLIST);
386 testres |= EC_TEST_CHECK_COMPLETE(node,
387 "b", EC_NODE_ENDLIST,
388 "bar", "bar2", EC_NODE_ENDLIST);
389 testres |= EC_TEST_CHECK_COMPLETE(node,
390 "bar", EC_NODE_ENDLIST,
391 "bar", "bar2", EC_NODE_ENDLIST);
392 testres |= EC_TEST_CHECK_COMPLETE(node,
393 "bar", "b", EC_NODE_ENDLIST,
394 "bar2", EC_NODE_ENDLIST);
395 testres |= EC_TEST_CHECK_COMPLETE(node,
396 "t", EC_NODE_ENDLIST,
397 "toto", "titi", EC_NODE_ENDLIST);
398 testres |= EC_TEST_CHECK_COMPLETE(node,
399 "to", EC_NODE_ENDLIST,
400 "toto", EC_NODE_ENDLIST);
401 testres |= EC_TEST_CHECK_COMPLETE(node,
402 "x", EC_NODE_ENDLIST,
410 static struct ec_test ec_node_subset_test = {
411 .name = "node_subset",
412 .test = ec_node_subset_testcase,
415 EC_TEST_REGISTER(ec_node_subset_test);