X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Frx.c;h=58e943c22f75b7b13bc4885f0c1c533e5a37fa26;hp=02a01d0acdac5b4dfd457a3eb1b0062ba8c8cd1f;hb=2b9f5916b3aebd8d34903d420ce8160121171534;hpb=6b771604433435b4280a5fbae2612134fe4129dd diff --git a/lib-lib/rx.c b/lib-lib/rx.c index 02a01d0..58e943c 100644 --- a/lib-lib/rx.c +++ b/lib-lib/rx.c @@ -29,7 +29,8 @@ rx_t *rx_compile(const char *s, int flags) rx_t *pp = p_new(rx_t, 1); pp->pattern = m_strdup(s); - pp->rx = p_new(regex_t, 1); + pp->rx = p_new(regex_t, 1); + pp->flags = flags; if (REGCOMP(pp->rx, NONULL(s), flags) != 0) { rx_delete(&pp); @@ -38,6 +39,14 @@ rx_t *rx_compile(const char *s, int flags) return pp; } +rx_t *rx_dup(rx_t *r) +{ + rx_t *res = rx_compile(r->pattern, r->flags); + res->neg = r->neg; + rx_set_template(res, r->tpl); + return res; +} + int rx_validate(const char *s, char *errbuf, ssize_t errlen) { regex_t re; @@ -57,8 +66,10 @@ void rx_set_template(rx_t *rx, const char *tpl) { const char *p = tpl; - m_strreplace(&rx->template, tpl); + m_strreplace(&rx->tpl, tpl); rx->nmatch = 0; + if (m_strisempty(rx->tpl)) + return; while ((p = strchr(p, '%'))) { if (isdigit(*++p)) { @@ -78,7 +89,7 @@ void rx_wipe(rx_t *rx) p_delete(&rx->pattern); regfree(rx->rx); p_delete(&rx->rx); - p_delete(&rx->template); + p_delete(&rx->tpl); } int rx_list_match(rx_t *l, const char *s) @@ -112,7 +123,7 @@ int rx_list_match2(rx_t *l, const char *s, char *dst, int dlen) if (regexec(l->rx, s, l->nmatch, pmatch, 0) == 0) { /* Copy template into dst, with substitutions. */ - const char *p = l->template, *q; + const char *p = l->tpl, *q; for (q = strchr(p, '%'); q; q = strchr(p + 1, '%')) { int n; @@ -148,7 +159,40 @@ rx_t **rx_lookup(rx_t **l, const char *pat) l = &(*l)->next; } - return NULL; + return l; +} + +void rx_list_add(rx_t **l, rx_t *rxp) +{ + l = rx_lookup(l, rxp->pattern); + if (*l) { + rx_t *r = rx_list_pop(l); + rx_delete(&r); + } + rx_list_push(l, rxp); +} + +void rx_list_add2(rx_t **l, rx_t **rxp) +{ + l = rx_lookup(l, (*rxp)->pattern); + if (*l) { + rx_t *r = rx_list_pop(l); + rx_delete(&r); + } + if (m_strisempty((*rxp)->tpl)) { + rx_delete(rxp); + } else { + rx_list_push(l, *rxp); + } +} + +void rx_list_remove(rx_t **l, const rx_t *r) +{ + l = rx_lookup(l, r->pattern); + if (*l) { + rx_t *tmp = rx_list_pop(l); + rx_delete(&tmp); + } } int rx_sanitize_string(char *dst, ssize_t n, const char *src)