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_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>
27 EC_LOG_TYPE_REGISTER(node_re_lex);
29 struct regexp_pattern {
36 struct ec_node_re_lex {
38 struct ec_node *child;
39 struct regexp_pattern *table;
43 static struct ec_strvec *
44 tokenize(struct regexp_pattern *table, size_t table_len, const char *str)
46 struct ec_strvec *strvec = NULL;
47 struct ec_keyval *attrs = NULL;
65 for (i = 0; i < table_len; i++) {
66 ret = regexec(&table[i].r, &dup[off], 1, &pos, 0);
69 if (pos.rm_so != 0 || pos.rm_eo == 0) {
74 if (table[i].keep == 0)
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)
83 if (table[i].attr_name != NULL) {
87 if (ec_keyval_set(attrs, table[i].attr_name,
90 if (ec_strvec_set_attrs(strvec,
91 ec_strvec_len(strvec) - 1,
99 dup[pos.rm_eo + off] = c;
114 ec_strvec_free(strvec);
119 ec_node_re_lex_parse(const struct ec_node *gen_node,
120 struct ec_parse *state,
121 const struct ec_strvec *strvec)
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;
129 if (node->child == NULL) {
134 if (ec_strvec_len(strvec) == 0) {
135 new_vec = ec_strvec();
137 str = ec_strvec_val(strvec, 0);
138 new_vec = tokenize(node->table, node->len, str);
143 ret = ec_node_parse_child(node->child, state, new_vec);
147 if ((unsigned)ret == ec_strvec_len(new_vec)) {
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;
156 ec_strvec_free(new_vec);
162 ec_strvec_free(new_vec);
166 static void ec_node_re_lex_free_priv(struct ec_node *gen_node)
168 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
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);
178 ec_free(node->table);
182 ec_node_re_lex_get_children_count(const struct ec_node *gen_node)
184 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
192 ec_node_re_lex_get_child(const struct ec_node *gen_node, size_t i,
193 struct ec_node **child, unsigned int *refs)
195 struct ec_node_re_lex *node = (struct ec_node_re_lex *)gen_node;
200 *child = node->child;
205 static const struct ec_config_schema ec_node_re_lex_dict[] = {
208 .desc = "The pattern to match.",
209 .type = EC_CONFIG_TYPE_STRING,
213 .desc = "Whether to keep or drop the string matching "
214 "the regular expression.",
215 .type = EC_CONFIG_TYPE_BOOL,
219 .desc = "The optional attribute name to attach.",
220 .type = EC_CONFIG_TYPE_STRING,
223 .type = EC_CONFIG_TYPE_NONE,
227 static const struct ec_config_schema ec_node_re_lex_elt[] = {
229 .desc = "A pattern element.",
230 .type = EC_CONFIG_TYPE_DICT,
231 .subschema = ec_node_re_lex_dict,
234 .type = EC_CONFIG_TYPE_NONE,
238 static const struct ec_config_schema ec_node_re_lex_schema[] = {
241 .desc = "The list of patterns elements.",
242 .type = EC_CONFIG_TYPE_LIST,
243 .subschema = ec_node_re_lex_elt,
247 .desc = "The child node.",
248 .type = EC_CONFIG_TYPE_NODE,
251 .type = EC_CONFIG_TYPE_NONE,
255 static int ec_node_re_lex_set_config(struct ec_node *gen_node,
256 const struct ec_config *config)
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;
265 child = ec_config_dict_get(config, "child");
268 if (ec_config_get_type(child) != EC_CONFIG_TYPE_NODE) {
273 patterns = ec_config_dict_get(config, "patterns");
274 if (patterns != NULL) {
275 n = ec_config_count(patterns);
279 table = ec_calloc(n, sizeof(*table));
284 TAILQ_FOREACH(elt, &patterns->list, next) {
285 if (ec_config_get_type(elt) != EC_CONFIG_TYPE_DICT) {
289 pattern = ec_config_dict_get(elt, "pattern");
290 if (pattern == NULL) {
294 if (ec_config_get_type(pattern) != EC_CONFIG_TYPE_STRING) {
298 keep = ec_config_dict_get(elt, "keep");
303 if (ec_config_get_type(keep) != EC_CONFIG_TYPE_BOOL) {
307 attr = ec_config_dict_get(elt, "attr");
308 if (attr != NULL && ec_config_get_type(attr) !=
309 EC_CONFIG_TYPE_STRING) {
313 pattern_str = ec_strdup(pattern->string);
314 if (pattern_str == NULL)
316 if (attr != NULL && attr->string != NULL) {
317 attr_name = ec_strdup(attr->string);
318 if (attr_name == NULL)
322 ret = regcomp(&table[n].r, pattern_str, REG_EXTENDED);
325 "Regular expression <%s> compilation failed: %d\n",
327 if (ret == REG_ESPACE)
333 table[n].pattern = pattern_str;
334 table[n].keep = keep->boolean;
335 table[n].attr_name = attr_name;
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 regfree(&node->table[i].r);
350 ec_free(node->table);
358 for (i = 0; i < n; i++) {
359 if (table[i].pattern != NULL) {
360 ec_free(table[i].pattern);
361 regfree(&table[i].r);
366 ec_free(pattern_str);
370 static struct ec_node_type ec_node_re_lex_type = {
372 .schema = ec_node_re_lex_schema,
373 .set_config = ec_node_re_lex_set_config,
374 .parse = ec_node_re_lex_parse,
375 .complete = ec_node_complete_unknown,
376 .size = sizeof(struct ec_node_re_lex),
377 .free_priv = ec_node_re_lex_free_priv,
378 .get_children_count = ec_node_re_lex_get_children_count,
379 .get_child = ec_node_re_lex_get_child,
382 EC_NODE_TYPE_REGISTER(ec_node_re_lex_type);
384 int ec_node_re_lex_add(struct ec_node *gen_node, const char *pattern, int keep,
385 const char *attr_name)
387 const struct ec_config *cur_config = NULL;
388 struct ec_config *config = NULL, *patterns = NULL, *elt = NULL;
391 if (ec_node_check_type(gen_node, &ec_node_re_lex_type) < 0)
394 elt = ec_config_dict();
397 if (ec_config_dict_set(elt, "pattern", ec_config_string(pattern)) < 0)
399 if (ec_config_dict_set(elt, "keep", ec_config_bool(keep)) < 0)
401 if (attr_name != NULL) {
402 if (ec_config_dict_set(elt, "attr",
403 ec_config_string(attr_name)) < 0)
407 cur_config = ec_node_get_config(gen_node);
408 if (cur_config == NULL)
409 config = ec_config_dict();
411 config = ec_config_dup(cur_config);
415 patterns = ec_config_dict_get(config, "patterns");
416 if (patterns == NULL) {
417 patterns = ec_config_list();
418 if (patterns == NULL)
421 if (ec_config_dict_set(config, "patterns", patterns) < 0)
422 goto fail; /* patterns list is freed on error */
425 if (ec_config_list_add(patterns, elt) < 0) {
431 ret = ec_node_set_config(gen_node, config);
432 config = NULL; /* freed */
439 ec_config_free(config);
445 ec_node_re_lex_set_child(struct ec_node *gen_node, struct ec_node *child)
447 const struct ec_config *cur_config = NULL;
448 struct ec_config *config = NULL;
451 if (ec_node_check_type(gen_node, &ec_node_re_lex_type) < 0)
454 cur_config = ec_node_get_config(gen_node);
455 if (cur_config == NULL)
456 config = ec_config_dict();
458 config = ec_config_dup(cur_config);
462 if (ec_config_dict_set(config, "child", ec_config_node(child)) < 0) {
463 child = NULL; /* freed */
466 child = NULL; /* freed */
468 ret = ec_node_set_config(gen_node, config);
469 config = NULL; /* freed */
476 ec_config_free(config);
481 struct ec_node *ec_node_re_lex(const char *id, struct ec_node *child)
483 struct ec_node *gen_node = NULL;
488 gen_node = ec_node_from_type(&ec_node_re_lex_type, id);
489 if (gen_node == NULL)
492 if (ec_node_re_lex_set_child(gen_node, child) < 0) {
493 child = NULL; /* freed */
500 ec_node_free(gen_node);
505 /* LCOV_EXCL_START */
506 static int ec_node_re_lex_testcase(void)
508 struct ec_node *node;
509 int ret, testres = 0;
511 node = ec_node_re_lex(EC_NO_ID,
512 ec_node_many(EC_NO_ID,
514 ec_node_str(EC_NO_ID, "foo"),
515 ec_node_str(EC_NO_ID, "bar"),
516 ec_node_int(EC_NO_ID, 0, 1000, 0)
521 EC_LOG(EC_LOG_ERR, "cannot create node\n");
525 ret = ec_node_re_lex_add(node, "[a-zA-Z]+", 1, NULL);
526 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
527 ret = ec_node_re_lex_add(node, "[0-9]+", 1, NULL);
528 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
529 ret = ec_node_re_lex_add(node, "=", 1, NULL);
530 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
531 ret = ec_node_re_lex_add(node, "-", 1, NULL);
532 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
533 ret = ec_node_re_lex_add(node, "\\+", 1, NULL);
534 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
535 ret = ec_node_re_lex_add(node, "[ ]+", 0, NULL);
536 testres |= EC_TEST_CHECK(ret == 0, "cannot add regexp");
538 EC_LOG(EC_LOG_ERR, "cannot add regexp to node\n");
543 testres |= EC_TEST_CHECK_PARSE(node, 1, " foo bar 324 bar234");
544 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo bar324");
545 testres |= EC_TEST_CHECK_PARSE(node, 1, "");
546 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
549 testres |= EC_TEST_CHECK_COMPLETE(node,
559 static struct ec_test ec_node_re_lex_test = {
560 .name = "node_re_lex",
561 .test = ec_node_re_lex_testcase,
564 EC_TEST_REGISTER(ec_node_re_lex_test);