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_parsed.h>
41 #include <ecoli_completed.h>
42 #include <ecoli_node_subset.h>
43 #include <ecoli_node_str.h>
44 #include <ecoli_node_or.h>
45 #include <ecoli_test.h>
47 struct ec_node_subset {
49 struct ec_node **table;
54 struct ec_parsed **parsed_table; /* list of parsed node */
55 size_t parsed_table_len; /* number of parsed node */
56 size_t len; /* consumed strings */
59 static int __ec_node_subset_parse(struct ec_node **table, size_t table_len,
60 const struct ec_strvec *strvec,
61 struct parse_result *out)
63 struct ec_node **child_table;
64 struct ec_parsed *child_parsed = NULL;
65 struct ec_strvec *childvec = NULL;
67 struct parse_result best_result, result;
73 memset(&best_result, 0, sizeof(best_result));
74 best_result.parsed_table =
75 ec_calloc(table_len, sizeof(*best_result.parsed_table[0]));
76 if (best_result.parsed_table == NULL)
79 child_table = ec_calloc(table_len - 1, sizeof(*child_table));
80 if (child_table == NULL)
83 for (i = 0; i < table_len; i++) {
84 /* try to parse elt i */
85 child_parsed = ec_node_parse_strvec(table[i], strvec);
86 if (child_parsed == NULL)
89 if (!ec_parsed_matches(child_parsed)) {
90 ec_parsed_free(child_parsed);
95 /* build a new table without elt i */
96 for (j = 0; j < table_len; j++) {
98 child_table[j] = table[j];
100 child_table[j - 1] = table[j];
103 /* build a new strvec */
104 len = ec_parsed_len(child_parsed);
105 childvec = ec_strvec_ndup(strvec, len,
106 ec_strvec_len(strvec) - len);
107 if (childvec == NULL)
110 memset(&result, 0, sizeof(result));
112 ret = __ec_node_subset_parse(child_table, table_len - 1,
114 ec_strvec_free(childvec);
119 /* if result is not the best, ignore */
120 if (result.parsed_table_len + 1 <=
121 best_result.parsed_table_len) {
122 ec_parsed_free(child_parsed);
124 for (j = 0; j < result.parsed_table_len; j++)
125 ec_parsed_free(result.parsed_table[j]);
126 ec_free(result.parsed_table);
127 memset(&result, 0, sizeof(result));
131 /* replace the previous best result */
132 for (j = 0; j < best_result.parsed_table_len; j++)
133 ec_parsed_free(best_result.parsed_table[j]);
134 best_result.parsed_table[0] = child_parsed;
136 for (j = 0; j < result.parsed_table_len; j++)
137 best_result.parsed_table[j+1] = result.parsed_table[j];
138 best_result.parsed_table_len = result.parsed_table_len + 1;
139 best_result.len = len + result.len;
140 ec_free(result.parsed_table);
144 ec_free(child_table);
149 ec_parsed_free(child_parsed);
150 ec_strvec_free(childvec);
151 for (j = 0; j < best_result.parsed_table_len; j++)
152 ec_parsed_free(best_result.parsed_table[j]);
153 ec_free(best_result.parsed_table);
154 ec_free(child_table);
158 static struct ec_parsed *ec_node_subset_parse(const struct ec_node *gen_node,
159 const struct ec_strvec *strvec)
161 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
162 struct ec_parsed *parsed = NULL;
163 struct parse_result result;
164 struct ec_strvec *match_strvec;
168 memset(&result, 0, sizeof(result));
170 parsed = ec_parsed();
174 ret = __ec_node_subset_parse(node->table, node->len, strvec, &result);
178 /* if no child node matches, return a matching empty strvec */
179 if (result.parsed_table_len == 0) {
180 ec_free(result.parsed_table);
181 match_strvec = ec_strvec();
182 if (match_strvec == NULL)
184 ec_parsed_set_match(parsed, gen_node, match_strvec);
188 for (i = 0; i < result.parsed_table_len; i++) {
189 ec_parsed_add_child(parsed, result.parsed_table[i]);
190 result.parsed_table[i] = NULL;
192 ec_free(result.parsed_table);
193 result.parsed_table = NULL;
195 match_strvec = ec_strvec_ndup(strvec, 0, result.len);
196 if (match_strvec == NULL)
199 ec_parsed_set_match(parsed, gen_node, match_strvec);
204 for (i = 0; i < result.parsed_table_len; i++)
205 ec_parsed_free(result.parsed_table[i]);
206 ec_free(result.parsed_table);
207 ec_parsed_free(parsed);
212 static struct ec_completed *
213 __ec_node_subset_complete(struct ec_node **table, size_t table_len,
214 const struct ec_strvec *strvec)
216 struct ec_completed *completed = NULL;
217 struct ec_completed *child_completed = NULL;
218 struct ec_strvec *childvec = NULL;
219 struct ec_parsed *parsed = NULL;
220 struct ec_node *save;
224 * example with table = [a, b, c]
225 * subset_complete([a,b,c], strvec) returns:
226 * complete(a, strvec) + complete(b, strvec) + complete(c, strvec) +
227 * + __subset_complete([b, c], childvec) if a matches
228 * + __subset_complete([a, c], childvec) if b matches
229 * + __subset_complete([a, b], childvec) if c matches
232 completed = ec_completed();
233 if (completed == NULL)
236 /* first, try to complete with each node of the table */
237 for (i = 0; i < table_len; i++) {
238 if (table[i] == NULL)
241 child_completed = ec_node_complete_strvec(table[i],
244 if (child_completed == NULL)
247 ec_completed_merge(completed, child_completed);
248 child_completed = NULL;
251 /* then, if a node matches, advance in strvec and try to complete with
252 * all the other nodes */
253 for (i = 0; i < table_len; i++) {
254 if (table[i] == NULL)
257 parsed = ec_node_parse_strvec(table[i], strvec);
261 if (!ec_parsed_matches(parsed)) {
262 ec_parsed_free(parsed);
267 len = ec_parsed_len(parsed);
268 childvec = ec_strvec_ndup(strvec, len,
269 ec_strvec_len(strvec) - len);
270 if (childvec == NULL)
275 child_completed = __ec_node_subset_complete(table,
276 table_len, childvec);
278 ec_strvec_free(childvec);
281 if (child_completed == NULL)
284 ec_completed_merge(completed, child_completed);
285 child_completed = NULL;
287 ec_parsed_free(parsed);
293 ec_parsed_free(parsed);
294 ec_completed_free(child_completed);
295 ec_completed_free(completed);
300 static struct ec_completed *ec_node_subset_complete(const struct ec_node *gen_node,
301 const struct ec_strvec *strvec)
303 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
305 return __ec_node_subset_complete(node->table, node->len, strvec);
308 static void ec_node_subset_free_priv(struct ec_node *gen_node)
310 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
313 for (i = 0; i < node->len; i++)
314 ec_node_free(node->table[i]);
315 ec_free(node->table);
318 int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child)
320 struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
321 struct ec_node **table;
323 assert(node != NULL);
328 gen_node->flags &= ~EC_NODE_F_BUILT;
330 table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
337 table[node->len] = child;
340 child->parent = gen_node;
341 TAILQ_INSERT_TAIL(&gen_node->children, child, next);
346 static struct ec_node_type ec_node_subset_type = {
348 .parse = ec_node_subset_parse,
349 .complete = ec_node_subset_complete,
350 .size = sizeof(struct ec_node_subset),
351 .free_priv = ec_node_subset_free_priv,
354 EC_NODE_TYPE_REGISTER(ec_node_subset_type);
356 struct ec_node *__ec_node_subset(const char *id, ...)
358 struct ec_node *gen_node = NULL;
359 struct ec_node_subset *node = NULL;
360 struct ec_node *child;
366 gen_node = __ec_node(&ec_node_subset_type, id);
367 node = (struct ec_node_subset *)gen_node;
371 for (child = va_arg(ap, struct ec_node *);
372 child != EC_NODE_ENDLIST;
373 child = va_arg(ap, struct ec_node *)) {
375 /* on error, don't quit the loop to avoid leaks */
376 if (fail == 1 || child == NULL ||
377 ec_node_subset_add(gen_node, child) < 0) {
390 ec_node_free(gen_node); /* will also free children */
395 static int ec_node_subset_testcase(void)
397 struct ec_node *node;
400 node = EC_NODE_SUBSET(NULL,
402 ec_node_str(NULL, "foo"),
403 ec_node_str(NULL, "bar")),
404 ec_node_str(NULL, "bar"),
405 ec_node_str(NULL, "toto")
408 ec_log(EC_LOG_ERR, "cannot create node\n");
411 ret |= EC_TEST_CHECK_PARSE(node, 0);
412 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo");
413 ret |= EC_TEST_CHECK_PARSE(node, 1, "bar");
414 ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "titi");
415 ret |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "toto");
416 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
417 ret |= EC_TEST_CHECK_PARSE(node, 2, "bar", "bar");
418 ret |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo");
419 ret |= EC_TEST_CHECK_PARSE(node, 0, " ");
420 ret |= EC_TEST_CHECK_PARSE(node, 0, "foox");
423 /* test completion */
424 node = EC_NODE_SUBSET(NULL,
425 ec_node_str(NULL, "foo"),
426 ec_node_str(NULL, "bar"),
427 ec_node_str(NULL, "bar2"),
428 ec_node_str(NULL, "toto"),
429 ec_node_str(NULL, "titi")
432 ec_log(EC_LOG_ERR, "cannot create node\n");
435 ret |= EC_TEST_CHECK_COMPLETE(node,
437 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST,
439 ret |= EC_TEST_CHECK_COMPLETE(node,
440 "bar", "bar2", "", EC_NODE_ENDLIST,
441 "foo", "toto", "titi", EC_NODE_ENDLIST,
443 ret |= EC_TEST_CHECK_COMPLETE(node,
444 "f", EC_NODE_ENDLIST,
445 "oo", EC_NODE_ENDLIST,
447 ret |= EC_TEST_CHECK_COMPLETE(node,
448 "b", EC_NODE_ENDLIST,
449 "ar", "ar2", EC_NODE_ENDLIST,
451 ret |= EC_TEST_CHECK_COMPLETE(node,
452 "bar", EC_NODE_ENDLIST,
453 "", "2", EC_NODE_ENDLIST,
455 ret |= EC_TEST_CHECK_COMPLETE(node,
456 "bar", "b", EC_NODE_ENDLIST,
457 "ar2", EC_NODE_ENDLIST,
459 ret |= EC_TEST_CHECK_COMPLETE(node,
460 "t", EC_NODE_ENDLIST,
461 "oto", "iti", EC_NODE_ENDLIST,
463 ret |= EC_TEST_CHECK_COMPLETE(node,
464 "to", EC_NODE_ENDLIST,
465 "to", EC_NODE_ENDLIST,
467 ret |= EC_TEST_CHECK_COMPLETE(node,
468 "x", EC_NODE_ENDLIST,
476 static struct ec_test ec_node_subset_test = {
477 .name = "node_subset",
478 .test = ec_node_subset_testcase,
481 EC_TEST_REGISTER(ec_node_subset_test);