2 * Copyright (c) 2016-2017, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <ecoli_malloc.h>
37 #include <ecoli_log.h>
38 #include <ecoli_strvec.h>
39 #include <ecoli_node.h>
40 #include <ecoli_node_subset.h>
41 #include <ecoli_node_str.h>
42 #include <ecoli_node_or.h>
43 #include <ecoli_test.h>
45 struct ec_node_subset {
47 struct ec_node **table;
52 struct ec_parsed **parsed_table; /* list of parsed node */
53 size_t parsed_table_len; /* number of parsed node */
54 size_t len; /* consumed strings */
57 static int __ec_node_subset_parse(struct ec_node **table, size_t table_len,
58 const struct ec_strvec *strvec,
59 struct parse_result *out)
61 struct ec_node **child_table;
62 struct ec_parsed *child_parsed = NULL;
63 struct ec_strvec *childvec = NULL;
65 struct parse_result best_result, result;
71 memset(&best_result, 0, sizeof(best_result));
72 best_result.parsed_table =
73 ec_calloc(table_len, sizeof(*best_result.parsed_table[0]));
74 if (best_result.parsed_table == NULL)
77 child_table = ec_calloc(table_len - 1, sizeof(*child_table));
78 if (child_table == NULL)
81 for (i = 0; i < table_len; i++) {
82 /* try to parse elt i */
83 child_parsed = ec_node_parse_strvec(table[i], strvec);
84 if (child_parsed == NULL)
87 if (!ec_parsed_matches(child_parsed)) {
88 ec_parsed_free(child_parsed);
93 /* build a new table without elt i */
94 for (j = 0; j < table_len; j++) {
96 child_table[j] = table[j];
98 child_table[j - 1] = table[j];
101 /* build a new strvec */
102 len = ec_parsed_len(child_parsed);
103 childvec = ec_strvec_ndup(strvec, len,
104 ec_strvec_len(strvec) - len);
105 if (childvec == NULL)
108 memset(&result, 0, sizeof(result));
110 ret = __ec_node_subset_parse(child_table, table_len - 1,
112 ec_strvec_free(childvec);
117 /* if result is not the best, ignore */
118 if (result.parsed_table_len + 1 <=
119 best_result.parsed_table_len) {
120 ec_parsed_free(child_parsed);
122 for (j = 0; j < result.parsed_table_len; j++)
123 ec_parsed_free(result.parsed_table[j]);
124 ec_free(result.parsed_table);
125 memset(&result, 0, sizeof(result));
129 /* replace the previous best result */
130 for (j = 0; j < best_result.parsed_table_len; j++)
131 ec_parsed_free(best_result.parsed_table[j]);
132 best_result.parsed_table[0] = child_parsed;
134 for (j = 0; j < result.parsed_table_len; j++)
135 best_result.parsed_table[j+1] = result.parsed_table[j];
136 best_result.parsed_table_len = result.parsed_table_len + 1;
137 best_result.len = len + result.len;
138 ec_free(result.parsed_table);
142 ec_free(child_table);
147 ec_parsed_free(child_parsed);
148 ec_strvec_free(childvec);
149 for (j = 0; j < best_result.parsed_table_len; j++)
150 ec_parsed_free(best_result.parsed_table[j]);
151 ec_free(best_result.parsed_table);
152 ec_free(child_table);
156 static struct ec_parsed *ec_node_subset_parse(const struct ec_node *gen_node,
157 const struct ec_strvec *strvec)
159 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
160 struct ec_parsed *parsed = NULL;
161 struct parse_result result;
162 struct ec_strvec *match_strvec;
166 memset(&result, 0, sizeof(result));
168 parsed = ec_parsed();
172 ret = __ec_node_subset_parse(node->table, node->len, strvec, &result);
176 /* if no child node matches, return a matching empty strvec */
177 if (result.parsed_table_len == 0) {
178 ec_free(result.parsed_table);
179 match_strvec = ec_strvec();
180 if (match_strvec == NULL)
182 ec_parsed_set_match(parsed, gen_node, match_strvec);
186 for (i = 0; i < result.parsed_table_len; i++) {
187 ec_parsed_add_child(parsed, result.parsed_table[i]);
188 result.parsed_table[i] = NULL;
190 ec_free(result.parsed_table);
191 result.parsed_table = NULL;
193 match_strvec = ec_strvec_ndup(strvec, 0, result.len);
194 if (match_strvec == NULL)
197 ec_parsed_set_match(parsed, gen_node, match_strvec);
202 for (i = 0; i < result.parsed_table_len; i++)
203 ec_parsed_free(result.parsed_table[i]);
204 ec_free(result.parsed_table);
205 ec_parsed_free(parsed);
210 static struct ec_completed *
211 __ec_node_subset_complete(struct ec_node **table, size_t table_len,
212 const struct ec_strvec *strvec)
214 struct ec_completed *completed = NULL;
215 struct ec_completed *child_completed = NULL;
216 struct ec_strvec *childvec = NULL;
217 struct ec_parsed *parsed = NULL;
218 struct ec_node *save;
222 * example with table = [a, b, c]
223 * subset_complete([a,b,c], strvec) returns:
224 * complete(a, strvec) + complete(b, strvec) + complete(c, strvec) +
225 * + __subset_complete([b, c], childvec) if a matches
226 * + __subset_complete([a, c], childvec) if b matches
227 * + __subset_complete([a, b], childvec) if c matches
230 completed = ec_completed();
231 if (completed == NULL)
234 /* first, try to complete with each node of the table */
235 for (i = 0; i < table_len; i++) {
236 if (table[i] == NULL)
239 child_completed = ec_node_complete_strvec(table[i],
242 if (child_completed == NULL)
245 ec_completed_merge(completed, child_completed);
246 child_completed = NULL;
249 /* then, if a node matches, advance in strvec and try to complete with
250 * all the other nodes */
251 for (i = 0; i < table_len; i++) {
252 if (table[i] == NULL)
255 parsed = ec_node_parse_strvec(table[i], strvec);
259 if (!ec_parsed_matches(parsed)) {
260 ec_parsed_free(parsed);
265 len = ec_parsed_len(parsed);
266 childvec = ec_strvec_ndup(strvec, len,
267 ec_strvec_len(strvec) - len);
268 if (childvec == NULL)
273 child_completed = __ec_node_subset_complete(table,
274 table_len, childvec);
276 ec_strvec_free(childvec);
279 if (child_completed == NULL)
282 ec_completed_merge(completed, child_completed);
283 child_completed = NULL;
285 ec_parsed_free(parsed);
291 ec_parsed_free(parsed);
292 ec_completed_free(child_completed);
293 ec_completed_free(completed);
298 static struct ec_completed *ec_node_subset_complete(const struct ec_node *gen_node,
299 const struct ec_strvec *strvec)
301 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
303 return __ec_node_subset_complete(node->table, node->len, strvec);
306 static void ec_node_subset_free_priv(struct ec_node *gen_node)
308 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
311 for (i = 0; i < node->len; i++)
312 ec_node_free(node->table[i]);
313 ec_free(node->table);
316 int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child)
318 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
319 struct ec_node **table;
321 assert(node != NULL);
326 gen_node->flags &= ~EC_NODE_F_BUILT;
328 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
335 table[node->len] = child;
338 child->parent = gen_node;
339 TAILQ_INSERT_TAIL(&gen_node->children, child, next);
344 static struct ec_node_type ec_node_subset_type = {
346 .parse = ec_node_subset_parse,
347 .complete = ec_node_subset_complete,
348 .size = sizeof(struct ec_node_subset),
349 .free_priv = ec_node_subset_free_priv,
352 EC_NODE_TYPE_REGISTER(ec_node_subset_type);
354 struct ec_node *__ec_node_subset(const char *id, ...)
356 struct ec_node *gen_node = NULL;
357 struct ec_node_subset *node = NULL;
358 struct ec_node *child;
364 gen_node = __ec_node(&ec_node_subset_type, id);
365 node = (struct ec_node_subset *)gen_node;
369 for (child = va_arg(ap, struct ec_node *);
370 child != EC_NODE_ENDLIST;
371 child = va_arg(ap, struct ec_node *)) {
373 /* on error, don't quit the loop to avoid leaks */
374 if (fail == 1 || child == NULL ||
375 ec_node_subset_add(gen_node, child) < 0) {
388 ec_node_free(gen_node); /* will also free children */
393 static int ec_node_subset_testcase(void)
395 struct ec_node *node;
398 node = EC_NODE_SUBSET(NULL,
400 ec_node_str(NULL, "foo"),
401 ec_node_str(NULL, "bar")),
402 ec_node_str(NULL, "bar"),
403 ec_node_str(NULL, "toto")
406 ec_log(EC_LOG_ERR, "cannot create node\n");
409 ret |= EC_TEST_CHECK_PARSE(node, 0);
410 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo");
411 ret |= EC_TEST_CHECK_PARSE(node, 1, "bar");
412 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "titi");
413 ret |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "toto");
414 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
415 ret |= EC_TEST_CHECK_PARSE(node, 2, "bar", "bar");
416 ret |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo");
417 ret |= EC_TEST_CHECK_PARSE(node, 0, " ");
418 ret |= EC_TEST_CHECK_PARSE(node, 0, "foox");
421 /* test completion */
422 node = EC_NODE_SUBSET(NULL,
423 ec_node_str(NULL, "foo"),
424 ec_node_str(NULL, "bar"),
425 ec_node_str(NULL, "bar2"),
426 ec_node_str(NULL, "toto"),
427 ec_node_str(NULL, "titi")
430 ec_log(EC_LOG_ERR, "cannot create node\n");
433 ret |= EC_TEST_CHECK_COMPLETE(node,
435 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST,
437 ret |= EC_TEST_CHECK_COMPLETE(node,
438 "bar", "bar2", "", EC_NODE_ENDLIST,
439 "foo", "toto", "titi", EC_NODE_ENDLIST,
441 ret |= EC_TEST_CHECK_COMPLETE(node,
442 "f", EC_NODE_ENDLIST,
443 "oo", EC_NODE_ENDLIST,
445 ret |= EC_TEST_CHECK_COMPLETE(node,
446 "b", EC_NODE_ENDLIST,
447 "ar", "ar2", EC_NODE_ENDLIST,
449 ret |= EC_TEST_CHECK_COMPLETE(node,
450 "bar", EC_NODE_ENDLIST,
451 "", "2", EC_NODE_ENDLIST,
453 ret |= EC_TEST_CHECK_COMPLETE(node,
454 "bar", "b", EC_NODE_ENDLIST,
455 "ar2", EC_NODE_ENDLIST,
457 ret |= EC_TEST_CHECK_COMPLETE(node,
458 "t", EC_NODE_ENDLIST,
459 "oto", "iti", EC_NODE_ENDLIST,
461 ret |= EC_TEST_CHECK_COMPLETE(node,
462 "to", EC_NODE_ENDLIST,
463 "to", EC_NODE_ENDLIST,
465 ret |= EC_TEST_CHECK_COMPLETE(node,
466 "x", EC_NODE_ENDLIST,
474 static struct ec_test ec_node_subset_test = {
475 .name = "node_subset",
476 .test = ec_node_subset_testcase,
479 EC_TEST_REGISTER(ec_node_subset_test);