X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Frx.c;h=02a01d0acdac5b4dfd457a3eb1b0062ba8c8cd1f;hp=8604ad5412292188fe3511998b31bd6ed27ca98d;hb=6b771604433435b4280a5fbae2612134fe4129dd;hpb=9fde23d2b9a4ba8076eb425a1af4342de1e485b5 diff --git a/lib-lib/rx.c b/lib-lib/rx.c index 8604ad5..02a01d0 100644 --- a/lib-lib/rx.c +++ b/lib-lib/rx.c @@ -38,6 +38,21 @@ 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; @@ -58,22 +73,21 @@ 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->template); } -int rx_list_match(rx_t *l, const char *pat) +int rx_list_match(rx_t *l, const char *s) { - if (!pat || !*pat) + if (m_strisempty(s)) return 0; while (l) { - if (!REGEXEC(l->rx, pat)) + if (!REGEXEC(l->rx, s)) return 1; l = l->next; } @@ -81,13 +95,55 @@ int rx_list_match(rx_t *l, const char *pat) return 0; } +int rx_list_match2(rx_t *l, const char *s, char *dst, int dlen) +{ + static regmatch_t *pmatch = NULL; + static int nmatch = 0; + int pos = 0; + + if (m_strisempty(s)) + return 0; + + for (; l; l = l->next) { + if (l->nmatch > nmatch) { + p_realloc(&pmatch, l->nmatch); + nmatch = l->nmatch; + } + + if (regexec(l->rx, s, l->nmatch, pmatch, 0) == 0) { + /* Copy template into dst, with substitutions. */ + const char *p = l->template, *q; + + for (q = strchr(p, '%'); q; q = strchr(p + 1, '%')) { + int n; + + pos += m_strncpy(dst + pos, dlen - pos, p, q - p); + + if (!isdigit((unsigned char)q[1])) { + p = q + (q[1] == '%'); + continue; + } + + n = strtol(q + 1, (char **)&p, 10); /* find pmatch index */ + pos += m_strncpy(dst + pos, dlen - pos, s + pmatch[n].rm_so, + pmatch[n].rm_eo - pmatch[n].rm_so); + } + + pos += m_strcpy(dst + pos, dlen - pos, p); + return 1; + } + } + + return 0; +} + rx_t **rx_lookup(rx_t **l, const char *pat) { - if (!pat || !*pat) + if (m_strisempty(pat)) return NULL; while (*l) { - if (!strcmp((*l)->pattern, pat)) + if (!m_strcmp((*l)->pattern, pat)) return l; l = &(*l)->next; }