fix minor leaks
[protos/libecoli.git] / src / ecoli_node_re_lex.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include <string.h>
9 #include <regex.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_keyval.h>
17 #include <ecoli_node.h>
18 #include <ecoli_complete.h>
19 #include <ecoli_parse.h>
20 #include <ecoli_config.h>
21 #include <ecoli_node_many.h>
22 #include <ecoli_node_or.h>
23 #include <ecoli_node_str.h>
24 #include <ecoli_node_int.h>
25 #include <ecoli_node_re_lex.h>
26
27 EC_LOG_TYPE_REGISTER(node_re_lex);
28
29 struct regexp_pattern {
30         char *pattern;
31         char *attr_name;
32         regex_t r;
33         bool keep;
34 };
35
36 struct ec_node_re_lex {
37         struct ec_node gen;
38         struct ec_node *child;
39         struct regexp_pattern *table;
40         size_t len;
41 };
42
43 static struct ec_strvec *
44 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
45 {
46         struct ec_strvec *strvec = NULL;
47         struct ec_keyval *attrs = NULL;
48         char *dup = NULL;
49         char c;
50         size_t len, off = 0;
51         size_t i;
52         int ret;
53         regmatch_t pos;
54
55         dup = ec_strdup(str);
56         if (dup == NULL)
57                 goto fail;
58
59         strvec = ec_strvec();
60         if (strvec == NULL)
61                 goto fail;
62
63         len = strlen(dup);
64         while (off < len) {
65                 for (i = 0; i < table_len; i++) {
66                         ret = regexec(&table[i].r, &dup[off], 1, &pos, 0);
67                         if (ret != 0)
68                                 continue;
69                         if (pos.rm_so != 0 || pos.rm_eo == 0) {
70                                 ret = -1;
71                                 continue;
72                         }
73
74                         if (table[i].keep == 0)
75                                 break;
76
77                         c = dup[pos.rm_eo + off];
78                         dup[pos.rm_eo + off] = '\0';
79                         EC_LOG(EC_LOG_DEBUG, "re_lex match <%s>\n", &dup[off]);
80                         if (ec_strvec_add(strvec, &dup[off]) < 0)
81                                 goto fail;
82
83                         if (table[i].attr_name != NULL) {
84                                 attrs = ec_keyval();
85                                 if (attrs == NULL)
86                                         goto fail;
87                                 if (ec_keyval_set(attrs, table[i].attr_name,
88                                                 NULL, NULL) < 0)
89                                         goto fail;
90                                 if (ec_strvec_set_attrs(strvec,
91                                                 ec_strvec_len(strvec) - 1,
92                                                 attrs) < 0) {
93                                         attrs = NULL;
94                                         goto fail;
95                                 }
96                                 attrs = NULL;
97                         }
98
99                         dup[pos.rm_eo + off] = c;
100                         break;
101                 }
102
103                 if (ret != 0)
104                         goto fail;
105
106                 off += pos.rm_eo;
107         }
108
109         ec_free(dup);
110         return strvec;
111
112 fail:
113         ec_free(dup);
114         ec_strvec_free(strvec);
115         return NULL;
116 }
117
118 static int
119 ec_node_re_lex_parse(const struct ec_node *gen_node,
120                 struct ec_parse *state,
121                 const struct ec_strvec *strvec)
122 {
123         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
124         struct ec_strvec *new_vec = NULL;
125         struct ec_parse *child_parse;
126         const char *str;
127         int ret;
128
129         if (node->child == NULL) {
130                 errno = EINVAL;
131                 goto fail;
132         }
133
134         if (ec_strvec_len(strvec) == 0) {
135                 new_vec = ec_strvec();
136         } else {
137                 str = ec_strvec_val(strvec, 0);
138                 new_vec = tokenize(node->table, node->len, str);
139         }
140         if (new_vec == NULL)
141                 goto fail;
142
143         ret = ec_node_parse_child(node->child, state, new_vec);
144         if (ret < 0)
145                 goto fail;
146
147         if ((unsigned)ret == ec_strvec_len(new_vec)) {
148                 ret = 1;
149         } else if (ret != EC_PARSE_NOMATCH) {
150                 child_parse = ec_parse_get_last_child(state);
151                 ec_parse_unlink_child(state, child_parse);
152                 ec_parse_free(child_parse);
153                 ret = EC_PARSE_NOMATCH;
154         }
155
156         ec_strvec_free(new_vec);
157         new_vec = NULL;
158
159         return ret;
160
161  fail:
162         ec_strvec_free(new_vec);
163         return -1;
164 }
165
166 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
167 {
168         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
169         unsigned int i;
170
171         ec_node_free(node->child);
172         for (i = 0; i < node->len; i++) {
173                 ec_free(node->table[i].pattern);
174                 ec_free(node->table[i].attr_name);
175                 regfree(&node->table[i].r);
176         }
177
178         ec_free(node->table);
179 }
180
181 static size_t
182 ec_node_re_lex_get_children_count(const struct ec_node *gen_node)
183 {
184         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
185
186         if (node->child)
187                 return 1;
188         return 0;
189 }
190
191 static int
192 ec_node_re_lex_get_child(const struct ec_node *gen_node, size_t i,
193                         struct ec_node **child, unsigned int *refs)
194 {
195         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
196
197         if (i >= 1)
198                 return -1;
199
200         *child = node->child;
201         *refs = 2;
202         return 0;
203 }
204
205 static const struct ec_config_schema ec_node_re_lex_dict[] = {
206         {
207                 .key = "pattern",
208                 .desc = "The pattern to match.",
209                 .type = EC_CONFIG_TYPE_STRING,
210         },
211         {
212                 .key = "keep",
213                 .desc = "Whether to keep or drop the string matching "
214                 "the regular expression.",
215                 .type = EC_CONFIG_TYPE_BOOL,
216         },
217         {
218                 .key = "attr",
219                 .desc = "The optional attribute name to attach.",
220                 .type = EC_CONFIG_TYPE_STRING,
221         },
222         {
223                 .type = EC_CONFIG_TYPE_NONE,
224         },
225 };
226
227 static const struct ec_config_schema ec_node_re_lex_elt[] = {
228         {
229                 .desc = "A pattern element.",
230                 .type = EC_CONFIG_TYPE_DICT,
231                 .subschema = ec_node_re_lex_dict,
232         },
233         {
234                 .type = EC_CONFIG_TYPE_NONE,
235         },
236 };
237
238 static const struct ec_config_schema ec_node_re_lex_schema[] = {
239         {
240                 .key = "patterns",
241                 .desc = "The list of patterns elements.",
242                 .type = EC_CONFIG_TYPE_LIST,
243                 .subschema = ec_node_re_lex_elt,
244         },
245         {
246                 .key = "child",
247                 .desc = "The child node.",
248                 .type = EC_CONFIG_TYPE_NODE,
249         },
250         {
251                 .type = EC_CONFIG_TYPE_NONE,
252         },
253 };
254
255 static int ec_node_re_lex_set_config(struct ec_node *gen_node,
256                                 const struct ec_config *config)
257 {
258         struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
259         struct regexp_pattern *table = NULL;
260         const struct ec_config *patterns, *child, *elt, *pattern, *keep, *attr;
261         char *pattern_str = NULL, *attr_name = NULL;
262         ssize_t i, n = 0;
263         int ret;
264
265         child = ec_config_dict_get(config, "child");
266         if (child == NULL)
267                 goto fail;
268         if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
269                 errno = EINVAL;
270                 goto fail;
271         }
272
273         patterns = ec_config_dict_get(config, "patterns");
274         if (patterns != NULL) {
275                 n = ec_config_count(patterns);
276                 if (n < 0)
277                         goto fail;
278
279                 table = ec_calloc(n, sizeof(*table));
280                 if (table == NULL)
281                         goto fail;
282
283                 n = 0;
284                 TAILQ_FOREACH(elt, &patterns->list, next) {
285                         if (ec_config_get_type(elt) != EC_CONFIG_TYPE_DICT) {
286                                 errno = EINVAL;
287                                 goto fail;
288                         }
289                         pattern = ec_config_dict_get(elt, "pattern");
290                         if (pattern == NULL) {
291                                 errno = EINVAL;
292                                 goto fail;
293                         }
294                         if (ec_config_get_type(pattern) != EC_CONFIG_TYPE_STRING) {
295                                 errno = EINVAL;
296                                 goto fail;
297                         }
298                         keep = ec_config_dict_get(elt, "keep");
299                         if (keep == NULL) {
300                                 errno = EINVAL;
301                                 goto fail;
302                         }
303                         if (ec_config_get_type(keep) != EC_CONFIG_TYPE_BOOL) {
304                                 errno = EINVAL;
305                                 goto fail;
306                         }
307                         attr = ec_config_dict_get(elt, "attr");
308                         if (attr != NULL && ec_config_get_type(attr) !=
309                                         EC_CONFIG_TYPE_STRING) {
310                                 errno = EINVAL;
311                                 goto fail;
312                         }
313                         pattern_str = ec_strdup(pattern->string);
314                         if (pattern_str == NULL)
315                                 goto fail;
316                         if (attr != NULL && attr->string != NULL) {
317                                 attr_name = ec_strdup(attr->string);
318                                 if (attr_name == NULL)
319                                         goto fail;
320                         }
321
322                         ret = regcomp(&table[n].r, pattern_str, REG_EXTENDED);
323                         if (ret != 0) {
324                                 EC_LOG(EC_LOG_ERR,
325                                         "Regular expression <%s> compilation failed: %d\n",
326                                         pattern_str, ret);
327                                 if (ret == REG_ESPACE)
328                                         errno = ENOMEM;
329                                 else
330                                         errno = EINVAL;
331                                 goto fail;
332                         }
333                         table[n].pattern = pattern_str;
334                         table[n].keep = keep->boolean;
335                         table[n].attr_name = attr_name;
336                         pattern_str = NULL;
337                         attr_name = NULL;
338
339                         n++;
340                 }
341         }
342
343         if (node->child != NULL)
344                 ec_node_free(node->child);
345         node->child = ec_node_clone(child->node);
346         for (i = 0; i < (ssize_t)node->len; i++) {
347                 ec_free(node->table[i].pattern);
348                 ec_free(node->table[i].attr_name);
349                 regfree(&node->table[i].r);
350         }
351         ec_free(node->table);
352         node->table = table;
353         node->len = n;
354
355         return 0;
356
357 fail:
358         if (table != NULL) {
359                 for (i = 0; i < n; i++) {
360                         if (table[i].pattern != NULL) {
361                                 ec_free(table[i].pattern);
362                                 regfree(&table[i].r);
363                         }
364                 }
365         }
366         ec_free(table);
367         ec_free(pattern_str);
368         return -1;
369 }
370
371 static struct ec_node_type ec_node_re_lex_type = {
372         .name = "re_lex",
373         .schema = ec_node_re_lex_schema,
374         .set_config = ec_node_re_lex_set_config,
375         .parse = ec_node_re_lex_parse,
376         .complete = ec_node_complete_unknown,
377         .size = sizeof(struct ec_node_re_lex),
378         .free_priv = ec_node_re_lex_free_priv,
379         .get_children_count = ec_node_re_lex_get_children_count,
380         .get_child = ec_node_re_lex_get_child,
381 };
382
383 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
384
385 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep,
386         const char *attr_name)
387 {
388         const struct ec_config *cur_config = NULL;
389         struct ec_config *config = NULL, *patterns = NULL, *elt = NULL;
390         int ret;
391
392         if (ec_node_check_type(gen_node, &ec_node_re_lex_type) < 0)
393                 goto fail;
394
395         elt = ec_config_dict();
396         if (elt == NULL)
397                 goto fail;
398         if (ec_config_dict_set(elt, "pattern", ec_config_string(pattern)) < 0)
399                 goto fail;
400         if (ec_config_dict_set(elt, "keep", ec_config_bool(keep)) < 0)
401                 goto fail;
402         if (attr_name != NULL) {
403                 if (ec_config_dict_set(elt, "attr",
404                                         ec_config_string(attr_name)) < 0)
405                         goto fail;
406         }
407
408         cur_config = ec_node_get_config(gen_node);
409         if (cur_config == NULL)
410                 config = ec_config_dict();
411         else
412                 config = ec_config_dup(cur_config);
413         if (config == NULL)
414                 goto fail;
415
416         patterns = ec_config_dict_get(config, "patterns");
417         if (patterns == NULL) {
418                 patterns = ec_config_list();
419                 if (patterns == NULL)
420                         goto fail;
421
422                 if (ec_config_dict_set(config, "patterns", patterns) < 0)
423                         goto fail; /* patterns list is freed on error */
424         }
425
426         if (ec_config_list_add(patterns, elt) < 0) {
427                 elt = NULL;
428                 goto fail;
429         }
430         elt = NULL;
431
432         ret = ec_node_set_config(gen_node, config);
433         config = NULL; /* freed */
434         if (ret < 0)
435                 goto fail;
436
437         return 0;
438
439 fail:
440         ec_config_free(config);
441         ec_config_free(elt);
442         return -1;
443 }
444
445 static int
446 ec_node_re_lex_set_child(struct ec_node *gen_node, struct ec_node *child)
447 {
448         const struct ec_config *cur_config = NULL;
449         struct ec_config *config = NULL;
450         int ret;
451
452         if (ec_node_check_type(gen_node, &ec_node_re_lex_type) < 0)
453                 goto fail;
454
455         cur_config = ec_node_get_config(gen_node);
456         if (cur_config == NULL)
457                 config = ec_config_dict();
458         else
459                 config = ec_config_dup(cur_config);
460         if (config == NULL)
461                 goto fail;
462
463         if (ec_config_dict_set(config, "child", ec_config_node(child)) < 0) {
464                 child = NULL; /* freed */
465                 goto fail;
466         }
467         child = NULL; /* freed */
468
469         ret = ec_node_set_config(gen_node, config);
470         config = NULL; /* freed */
471         if (ret < 0)
472                 goto fail;
473
474         return 0;
475
476 fail:
477         ec_config_free(config);
478         ec_node_free(child);
479         return -1;
480 }
481
482 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
483 {
484         struct ec_node *gen_node = NULL;
485
486         if (child == NULL)
487                 return NULL;
488
489         gen_node = ec_node_from_type(&ec_node_re_lex_type, id);
490         if (gen_node == NULL)
491                 goto fail;
492
493         if (ec_node_re_lex_set_child(gen_node, child) < 0) {
494                 child = NULL; /* freed */
495                 goto fail;
496         }
497
498         return gen_node;
499
500 fail:
501         ec_node_free(gen_node);
502         ec_node_free(child);
503         return NULL;
504 }
505
506 /* LCOV_EXCL_START */
507 static int ec_node_re_lex_testcase(void)
508 {
509         struct ec_node *node;
510         int ret, testres = 0;
511
512         node = ec_node_re_lex(EC_NO_ID,
513                 ec_node_many(EC_NO_ID,
514                         EC_NODE_OR(EC_NO_ID,
515                                 ec_node_str(EC_NO_ID, "foo"),
516                                 ec_node_str(EC_NO_ID, "bar"),
517                                 ec_node_int(EC_NO_ID, 0, 1000, 0)
518                         ), 0, 0
519                 )
520         );
521         if (node == NULL) {
522                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
523                 return -1;
524         }
525
526         ret = ec_node_re_lex_add(node, "[a-zA-Z]+", 1, NULL);
527         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
528         ret = ec_node_re_lex_add(node, "[0-9]+", 1, NULL);
529         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
530         ret = ec_node_re_lex_add(node, "=", 1, NULL);
531         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
532         ret = ec_node_re_lex_add(node, "-", 1, NULL);
533         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
534         ret = ec_node_re_lex_add(node, "\\+", 1, NULL);
535         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
536         ret = ec_node_re_lex_add(node, "[       ]+", 0, NULL);
537         testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
538         if (ret != 0) {
539                 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
540                 ec_node_free(node);
541                 return -1;
542         }
543
544         testres |= EC_TEST_CHECK_PARSE(node, 1, "  foo bar  324 bar234");
545         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo bar324");
546         testres |= EC_TEST_CHECK_PARSE(node, 1, "");
547         testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
548
549         /* no completion */
550         testres |= EC_TEST_CHECK_COMPLETE(node,
551                 "", EC_NODE_ENDLIST,
552                 EC_NODE_ENDLIST);
553
554         ec_node_free(node);
555
556         return testres;
557 }
558 /* LCOV_EXCL_STOP */
559
560 static struct ec_test ec_node_re_lex_test = {
561         .name = "node_re_lex",
562         .test = ec_node_re_lex_testcase,
563 };
564
565 EC_TEST_REGISTER(ec_node_re_lex_test);