X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=lib-lib%2Frx.c;h=d89d23468f5ed697e779ba30ee6edda329b5bfab;hb=6aac4a85332ec3647c29a7b996cade972895a8b8;hp=0edce593907000c360bea7f048adc1350d3fea7f;hpb=4e64abbe6c52d61bb01b85c266e33e30b257ba68;p=apps%2Fmadmutt.git diff --git a/lib-lib/rx.c b/lib-lib/rx.c index 0edce59..d89d234 100644 --- a/lib-lib/rx.c +++ b/lib-lib/rx.c @@ -38,11 +38,26 @@ rx_t *rx_compile(const char *s, int flags) return pp; } +int rx_validate(const char *s, char *errbuf, ssize_t errlen) +{ + regex_t re; + int res; + + p_clear(&re, 1); + res = REGCOMP(&re, NONULL(s), 0); + if (res) { + regerror(res, &re, errbuf, errlen); + } + regfree(&re); + + return res; +} + 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; while ((p = strchr(p, '%'))) { @@ -58,13 +73,12 @@ void rx_set_template(rx_t *rx, const char *tpl) rx->nmatch++; /* match 0 is always the whole expr */ } -void rx_delete(rx_t **p) +void rx_wipe(rx_t *rx) { - p_delete(&(*p)->pattern); - regfree((*p)->rx); - p_delete(&(*p)->rx); - p_delete(&(*p)->template); - p_delete(p); + p_delete(&rx->pattern); + regfree(rx->rx); + p_delete(&rx->rx); + p_delete(&rx->tpl); } int rx_list_match(rx_t *l, const char *s) @@ -98,7 +112,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; @@ -134,7 +148,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)