1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_node.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_parse.h>
19 #include <ecoli_config.h>
20 #include <ecoli_node_many.h>
21 #include <ecoli_node_or.h>
22 #include <ecoli_node_str.h>
23 #include <ecoli_node_int.h>
24 #include <ecoli_node_re_lex.h>
26 EC_LOG_TYPE_REGISTER(node_re_lex);
28 struct regexp_pattern {
34 struct ec_node_re_lex {
36 struct ec_node *child;
37 struct regexp_pattern *table;
41 static struct ec_strvec *
42 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
44 struct ec_strvec *strvec = NULL;
62 for (i = 0; i < table_len; i++) {
63 ret = regexec(&table[i].r, &dup[off], 1, &pos, 0);
66 if (pos.rm_so != 0 || pos.rm_eo == 0) {
71 if (table[i].keep == 0)
74 c = dup[pos.rm_eo + off];
75 dup[pos.rm_eo + off] = '\0';
76 EC_LOG(EC_LOG_DEBUG, "re_lex match <%s>\n", &dup[off]);
77 if (ec_strvec_add(strvec, &dup[off]) < 0)
80 dup[pos.rm_eo + off] = c;
95 ec_strvec_free(strvec);
100 ec_node_re_lex_parse(const struct ec_node *gen_node,
101 struct ec_parse *state,
102 const struct ec_strvec *strvec)
104 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
105 struct ec_strvec *new_vec = NULL;
106 struct ec_parse *child_parse;
110 if (node->child == NULL) {
115 if (ec_strvec_len(strvec) == 0) {
116 new_vec = ec_strvec();
118 str = ec_strvec_val(strvec, 0);
119 new_vec = tokenize(node->table, node->len, str);
124 ret = ec_node_parse_child(node->child, state, new_vec);
128 if ((unsigned)ret == ec_strvec_len(new_vec)) {
130 } else if (ret != EC_PARSE_NOMATCH) {
131 child_parse = ec_parse_get_last_child(state);
132 ec_parse_unlink_child(state, child_parse);
133 ec_parse_free(child_parse);
134 ret = EC_PARSE_NOMATCH;
137 ec_strvec_free(new_vec);
143 ec_strvec_free(new_vec);
147 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
149 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
152 ec_node_free(node->child);
153 for (i = 0; i < node->len; i++) {
154 ec_free(node->table[i].pattern);
155 regfree(&node->table[i].r);
158 ec_free(node->table);
162 ec_node_re_lex_get_children_count(const struct ec_node *gen_node)
164 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
172 ec_node_re_lex_get_child(const struct ec_node *gen_node, size_t i,
173 struct ec_node **child, unsigned int *refs)
175 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
180 *child = node->child;
185 static const struct ec_config_schema ec_node_re_lex_dict[] = {
188 .desc = "The pattern to match.",
189 .type = EC_CONFIG_TYPE_STRING,
193 .desc = "Whether to keep or drop the string matching "
194 "the regular expression.",
195 .type = EC_CONFIG_TYPE_BOOL,
198 .type = EC_CONFIG_TYPE_NONE,
202 static const struct ec_config_schema ec_node_re_lex_elt[] = {
204 .desc = "A pattern element.",
205 .type = EC_CONFIG_TYPE_DICT,
206 .subschema = ec_node_re_lex_dict,
209 .type = EC_CONFIG_TYPE_NONE,
213 static const struct ec_config_schema ec_node_re_lex_schema[] = {
216 .desc = "The list of patterns elements.",
217 .type = EC_CONFIG_TYPE_LIST,
218 .subschema = ec_node_re_lex_elt,
222 .desc = "The child node.",
223 .type = EC_CONFIG_TYPE_NODE,
226 .type = EC_CONFIG_TYPE_NONE,
230 static int ec_node_re_lex_set_config(struct ec_node *gen_node,
231 const struct ec_config *config)
233 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
234 struct regexp_pattern *table = NULL;
235 const struct ec_config *patterns, *child, *elt, *pattern, *keep;
236 char *pattern_str = NULL;
240 child = ec_config_dict_get(config, "child");
243 if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
248 patterns = ec_config_dict_get(config, "patterns");
249 if (patterns != NULL) {
250 n = ec_config_count(patterns);
254 table = ec_calloc(n, sizeof(*table));
259 TAILQ_FOREACH(elt, &patterns->list, next) {
260 if (ec_config_get_type(elt) != EC_CONFIG_TYPE_DICT) {
264 pattern = ec_config_dict_get(elt, "pattern");
265 if (pattern == NULL) {
269 if (ec_config_get_type(pattern) != EC_CONFIG_TYPE_STRING) {
273 keep = ec_config_dict_get(elt, "keep");
278 if (ec_config_get_type(keep) != EC_CONFIG_TYPE_BOOL) {
282 pattern_str = ec_strdup(pattern->string);
283 if (pattern_str == NULL)
286 ret = regcomp(&table[n].r, pattern_str, REG_EXTENDED);
289 "Regular expression <%s> compilation failed: %d\n",
291 if (ret == REG_ESPACE)
297 table[n].pattern = pattern_str;
298 table[n].keep = keep->boolean;
305 if (node->child != NULL)
306 ec_node_free(node->child);
307 node->child = ec_node_clone(child->node);
308 for (i = 0; i < (ssize_t)node->len; i++) {
309 ec_free(node->table[i].pattern);
310 regfree(&node->table[i].r);
312 ec_free(node->table);
320 for (i = 0; i < n; i++) {
321 if (table[i].pattern != NULL) {
322 ec_free(table[i].pattern);
323 regfree(&table[i].r);
328 ec_free(pattern_str);
332 static struct ec_node_type ec_node_re_lex_type = {
334 .schema = ec_node_re_lex_schema,
335 .set_config = ec_node_re_lex_set_config,
336 .parse = ec_node_re_lex_parse,
337 .complete = ec_node_complete_unknown,
338 .size = sizeof(struct ec_node_re_lex),
339 .free_priv = ec_node_re_lex_free_priv,
340 .get_children_count = ec_node_re_lex_get_children_count,
341 .get_child = ec_node_re_lex_get_child,
344 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
346 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep)
348 const struct ec_config *cur_config = NULL;
349 struct ec_config *config = NULL, *patterns = NULL, *elt = NULL;
352 if (ec_node_check_type(gen_node, &ec_node_re_lex_type) < 0)
355 elt = ec_config_dict();
358 if (ec_config_dict_set(elt, "pattern", ec_config_string(pattern)) < 0)
360 if (ec_config_dict_set(elt, "keep", ec_config_bool(keep)) < 0)
363 cur_config = ec_node_get_config(gen_node);
364 if (cur_config == NULL)
365 config = ec_config_dict();
367 config = ec_config_dup(cur_config);
371 patterns = ec_config_dict_get(config, "patterns");
372 if (patterns == NULL) {
373 patterns = ec_config_list();
374 if (patterns == NULL)
377 if (ec_config_dict_set(config, "patterns", patterns) < 0)
378 goto fail; /* patterns list is freed on error */
381 if (ec_config_list_add(patterns, elt) < 0) {
387 ret = ec_node_set_config(gen_node, config);
388 config = NULL; /* freed */
395 ec_config_free(config);
401 ec_node_re_lex_set_child(struct ec_node *gen_node, struct ec_node *child)
403 const struct ec_config *cur_config = NULL;
404 struct ec_config *config = NULL;
407 if (ec_node_check_type(gen_node, &ec_node_re_lex_type) < 0)
410 cur_config = ec_node_get_config(gen_node);
411 if (cur_config == NULL)
412 config = ec_config_dict();
414 config = ec_config_dup(cur_config);
418 if (ec_config_dict_set(config, "child", ec_config_node(child)) < 0) {
419 child = NULL; /* freed */
422 child = NULL; /* freed */
424 ret = ec_node_set_config(gen_node, config);
425 config = NULL; /* freed */
432 ec_config_free(config);
437 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
439 struct ec_node *gen_node = NULL;
444 gen_node = ec_node_from_type(&ec_node_re_lex_type, id);
445 if (gen_node == NULL)
448 if (ec_node_re_lex_set_child(gen_node, child) < 0) {
449 child = NULL; /* freed */
456 ec_node_free(gen_node);
461 /* LCOV_EXCL_START */
462 static int ec_node_re_lex_testcase(void)
464 struct ec_node *node;
465 int ret, testres = 0;
467 node = ec_node_re_lex(EC_NO_ID,
468 ec_node_many(EC_NO_ID,
470 ec_node_str(EC_NO_ID, "foo"),
471 ec_node_str(EC_NO_ID, "bar"),
472 ec_node_int(EC_NO_ID, 0, 1000, 0)
477 EC_LOG(EC_LOG_ERR, "cannot create node\n");
481 ret = ec_node_re_lex_add(node, "[a-zA-Z]+", 1);
482 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
486 ret = ec_node_re_lex_add(node, "[0-9]+", 1);
487 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
488 ret = ec_node_re_lex_add(node, "=", 1);
489 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
490 ret = ec_node_re_lex_add(node, "-", 1);
491 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
492 ret = ec_node_re_lex_add(node, "\\+", 1);
493 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
494 ret = ec_node_re_lex_add(node, "[ ]+", 0);
495 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
497 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
502 testres |= EC_TEST_CHECK_PARSE(node, 1, " foo bar 324 bar234");
503 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo bar324");
504 testres |= EC_TEST_CHECK_PARSE(node, 1, "");
505 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
508 testres |= EC_TEST_CHECK_COMPLETE(node,
518 static struct ec_test ec_node_re_lex_test = {
519 .name = "node_re_lex",
520 .test = ec_node_re_lex_testcase,
523 EC_TEST_REGISTER(ec_node_re_lex_test);