save
[protos/libecoli.git] / lib / ecoli_node_subset.c
1 /*
2  * Copyright (c) 2016-2017, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <stdarg.h>
33 #include <errno.h>
34 #include <stdbool.h>
35
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>
46
47 struct ec_node_subset {
48         struct ec_node gen;
49         struct ec_node **table;
50         unsigned int len;
51 };
52
53 struct parse_result {
54         size_t parsed_len;          /* number of parsed node */
55         size_t len;                 /* consumed strings */
56 };
57
58 /* recursively find the longest list of nodes that matches: the state is
59  * updated accordingly. */
60 static int
61 __ec_node_subset_parse(struct parse_result *out, struct ec_node **table,
62                 size_t table_len, struct ec_parsed *state,
63                 const struct ec_strvec *strvec)
64 {
65         struct ec_node **child_table;
66         struct ec_strvec *childvec = NULL;
67         size_t i, j, len = 0;
68         struct parse_result best_result, result;
69         struct ec_parsed *best_parsed = NULL;
70         int ret;
71
72         if (table_len == 0)
73                 return 0;
74
75         memset(&best_result, 0, sizeof(best_result));
76
77         child_table = ec_calloc(table_len - 1, sizeof(*child_table));
78         if (child_table == NULL) {
79                 ret = -ENOMEM;
80                 goto fail;
81         }
82
83         for (i = 0; i < table_len; i++) {
84                 /* try to parse elt i */
85                 ret = ec_node_parse_child(table[i], state, strvec);
86                 if (ret == EC_PARSED_NOMATCH)
87                         continue;
88                 else if (ret < 0)
89                         goto fail;
90
91                 /* build a new table without elt i */
92                 for (j = 0; j < table_len; j++) {
93                         if (j < i)
94                                 child_table[j] = table[j];
95                         else if (j > i)
96                                 child_table[j - 1] = table[j];
97                 }
98
99                 /* build a new strvec (ret is the len of matched strvec) */
100                 len = ret;
101                 childvec = ec_strvec_ndup(strvec, len,
102                                         ec_strvec_len(strvec) - len);
103                 if (childvec == NULL) {
104                         ret = -ENOMEM;
105                         goto fail;
106                 }
107
108                 memset(&result, 0, sizeof(result));
109                 ret = __ec_node_subset_parse(&result, child_table,
110                                         table_len - 1, state, childvec);
111                 ec_strvec_free(childvec);
112                 childvec = NULL;
113                 if (ret < 0)
114                         goto fail;
115
116                 /* if result is not the best, ignore */
117                 if (result.parsed_len < best_result.parsed_len) {
118                         memset(&result, 0, sizeof(result));
119                         ec_parsed_del_last_child(state);
120                         continue;
121                 }
122
123                 /* replace the previous best result */
124                 ec_parsed_free(best_parsed);
125                 best_parsed = ec_parsed_get_last_child(state);
126                 ec_parsed_del_child(state, best_parsed);
127
128                 best_result.parsed_len = result.parsed_len + 1;
129                 best_result.len = len + result.len;
130
131                 memset(&result, 0, sizeof(result));
132         }
133
134         *out = best_result;
135         ec_free(child_table);
136         if (best_parsed != NULL)
137                 ec_parsed_add_child(state, best_parsed);
138
139         return 0;
140
141  fail:
142         ec_parsed_free(best_parsed);
143         ec_strvec_free(childvec);
144         ec_free(child_table);
145         return ret;
146 }
147
148 static int
149 ec_node_subset_parse(const struct ec_node *gen_node,
150                 struct ec_parsed *state,
151                 const struct ec_strvec *strvec)
152 {
153         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
154         struct ec_parsed *parsed = NULL;
155         struct parse_result result;
156         int ret;
157
158         memset(&result, 0, sizeof(result));
159
160         ret = __ec_node_subset_parse(&result, node->table,
161                                 node->len, state, strvec);
162         if (ret < 0)
163                 goto fail;
164
165         /* if no child node matches, return a matching empty strvec */
166         if (result.parsed_len == 0)
167                 return 0;
168
169         return result.len;
170
171  fail:
172         ec_parsed_free(parsed);
173         return ret;
174 }
175
176 static int
177 __ec_node_subset_complete(struct ec_node **table, size_t table_len,
178                         struct ec_completed *completed,
179                         struct ec_parsed *parsed,
180                         const struct ec_strvec *strvec)
181 {
182         struct ec_strvec *childvec = NULL;
183         struct ec_node *save;
184         size_t i, len;
185         int ret;
186
187         /*
188          * example with table = [a, b, c]
189          * subset_complete([a,b,c], strvec) returns:
190          *   complete(a, strvec) + complete(b, strvec) + complete(c, strvec) +
191          *   + __subset_complete([b, c], childvec) if a matches
192          *   + __subset_complete([a, c], childvec) if b matches
193          *   + __subset_complete([a, b], childvec) if c matches
194          */
195
196         /* first, try to complete with each node of the table */
197         for (i = 0; i < table_len; i++) {
198                 if (table[i] == NULL)
199                         continue;
200
201                 ret = ec_node_complete_child(table[i],
202                                         completed, parsed, strvec);
203                 if (ret < 0)
204                         goto fail;
205         }
206
207         /* then, if a node matches, advance in strvec and try to complete with
208          * all the other nodes */
209         for (i = 0; i < table_len; i++) {
210                 if (table[i] == NULL)
211                         continue;
212
213                 ret = ec_node_parse_child(table[i], parsed, strvec);
214                 if (ret == EC_PARSED_NOMATCH)
215                         continue;
216                 else if (ret < 0)
217                         goto fail;
218
219                 len = ret;
220                 childvec = ec_strvec_ndup(strvec, len,
221                                         ec_strvec_len(strvec) - len);
222                 if (childvec == NULL) {
223                         ec_parsed_del_last_child(parsed);
224                         goto fail;
225                 }
226
227                 save = table[i];
228                 table[i] = NULL;
229                 ret = __ec_node_subset_complete(table, table_len,
230                                                 completed, parsed, childvec);
231                 table[i] = save;
232                 ec_strvec_free(childvec);
233                 childvec = NULL;
234                 ec_parsed_del_last_child(parsed);
235
236                 if (ret < 0)
237                         goto fail;
238         }
239
240         return 0;
241
242 fail:
243         return -1;
244 }
245
246 static int
247 ec_node_subset_complete(const struct ec_node *gen_node,
248                         struct ec_completed *completed,
249                         struct ec_parsed *parsed,
250                         const struct ec_strvec *strvec)
251 {
252         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
253
254         return __ec_node_subset_complete(node->table, node->len, completed,
255                                         parsed, strvec);
256 }
257
258 static void ec_node_subset_free_priv(struct ec_node *gen_node)
259 {
260         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
261         unsigned int i;
262
263         for (i = 0; i < node->len; i++)
264                 ec_node_free(node->table[i]);
265         ec_free(node->table);
266 }
267
268 int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child)
269 {
270         struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
271         struct ec_node **table;
272
273         assert(node != NULL);
274
275         if (child == NULL)
276                 return -EINVAL;
277
278         gen_node->flags &= ~EC_NODE_F_BUILT;
279
280         table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
281         if (table == NULL) {
282                 ec_node_free(child);
283                 return -1;
284         }
285
286         node->table = table;
287         table[node->len] = child;
288         node->len++;
289
290         child->parent = gen_node;
291         TAILQ_INSERT_TAIL(&gen_node->children, child, next);
292
293         return 0;
294 }
295
296 static struct ec_node_type ec_node_subset_type = {
297         .name = "subset",
298         .parse = ec_node_subset_parse,
299         .complete = ec_node_subset_complete,
300         .size = sizeof(struct ec_node_subset),
301         .free_priv = ec_node_subset_free_priv,
302 };
303
304 EC_NODE_TYPE_REGISTER(ec_node_subset_type);
305
306 struct ec_node *__ec_node_subset(const char *id, ...)
307 {
308         struct ec_node *gen_node = NULL;
309         struct ec_node_subset *node = NULL;
310         struct ec_node *child;
311         va_list ap;
312         int fail = 0;
313
314         va_start(ap, id);
315
316         gen_node = __ec_node(&ec_node_subset_type, id);
317         node = (struct ec_node_subset *)gen_node;
318         if (node == NULL)
319                 fail = 1;;
320
321         for (child = va_arg(ap, struct ec_node *);
322              child != EC_NODE_ENDLIST;
323              child = va_arg(ap, struct ec_node *)) {
324
325                 /* on error, don't quit the loop to avoid leaks */
326                 if (fail == 1 || child == NULL ||
327                                 ec_node_subset_add(gen_node, child) < 0) {
328                         fail = 1;
329                         ec_node_free(child);
330                 }
331         }
332
333         if (fail == 1)
334                 goto fail;
335
336         va_end(ap);
337         return gen_node;
338
339 fail:
340         ec_node_free(gen_node); /* will also free children */
341         va_end(ap);
342         return NULL;
343 }
344
345 /* LCOV_EXCL_START */
346 static int ec_node_subset_testcase(void)
347 {
348         struct ec_node *node;
349         int ret = 0;
350
351         node = EC_NODE_SUBSET(NULL,
352                 EC_NODE_OR(NULL,
353                         ec_node_str(NULL, "foo"),
354                         ec_node_str(NULL, "bar")),
355                 ec_node_str(NULL, "bar"),
356                 ec_node_str(NULL, "toto")
357         );
358         if (node == NULL) {
359                 ec_log(EC_LOG_ERR, "cannot create node\n");
360                 return -1;
361         }
362         ret |= EC_TEST_CHECK_PARSE(node, 0);
363         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo");
364         ret |= EC_TEST_CHECK_PARSE(node, 1, "bar");
365         ret |= EC_TEST_CHECK_PARSE(node, 2, "foo", "bar", "titi");
366         ret |= EC_TEST_CHECK_PARSE(node, 3, "bar", "foo", "toto");
367         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo", "foo");
368         ret |= EC_TEST_CHECK_PARSE(node, 2, "bar", "bar");
369         ret |= EC_TEST_CHECK_PARSE(node, 2, "bar", "foo");
370         ret |= EC_TEST_CHECK_PARSE(node, 0, " ");
371         ret |= EC_TEST_CHECK_PARSE(node, 0, "foox");
372         ec_node_free(node);
373
374         /* test completion */
375         node = EC_NODE_SUBSET(NULL,
376                 ec_node_str(NULL, "foo"),
377                 ec_node_str(NULL, "bar"),
378                 ec_node_str(NULL, "bar2"),
379                 ec_node_str(NULL, "toto"),
380                 ec_node_str(NULL, "titi")
381         );
382         if (node == NULL) {
383                 ec_log(EC_LOG_ERR, "cannot create node\n");
384                 return -1;
385         }
386         ret |= EC_TEST_CHECK_COMPLETE(node,
387                 "", EC_NODE_ENDLIST,
388                 "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST,
389                 "");
390         ret |= EC_TEST_CHECK_COMPLETE(node,
391                 "bar", "bar2", "", EC_NODE_ENDLIST,
392                 "foo", "toto", "titi", EC_NODE_ENDLIST,
393                 "");
394         ret |= EC_TEST_CHECK_COMPLETE(node,
395                 "f", EC_NODE_ENDLIST,
396                 "oo", EC_NODE_ENDLIST,
397                 "oo");
398         ret |= EC_TEST_CHECK_COMPLETE(node,
399                 "b", EC_NODE_ENDLIST,
400                 "ar", "ar2", EC_NODE_ENDLIST,
401                 "ar");
402         ret |= EC_TEST_CHECK_COMPLETE(node,
403                 "bar", EC_NODE_ENDLIST,
404                 "", "2", EC_NODE_ENDLIST,
405                 "");
406         ret |= EC_TEST_CHECK_COMPLETE(node,
407                 "bar", "b", EC_NODE_ENDLIST,
408                 "ar2", EC_NODE_ENDLIST,
409                 "ar2");
410         ret |= EC_TEST_CHECK_COMPLETE(node,
411                 "t", EC_NODE_ENDLIST,
412                 "oto", "iti", EC_NODE_ENDLIST,
413                 "");
414         ret |= EC_TEST_CHECK_COMPLETE(node,
415                 "to", EC_NODE_ENDLIST,
416                 "to", EC_NODE_ENDLIST,
417                 "to");
418         ret |= EC_TEST_CHECK_COMPLETE(node,
419                 "x", EC_NODE_ENDLIST,
420                 EC_NODE_ENDLIST,
421                 "");
422         ec_node_free(node);
423
424         return ret;
425 }
426 /* LCOV_EXCL_STOP */
427
428 static struct ec_test ec_node_subset_test = {
429         .name = "node_subset",
430         .test = ec_node_subset_testcase,
431 };
432
433 EC_TEST_REGISTER(ec_node_subset_test);