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