2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
20 * This file is part of mutt-ng, see http://www.muttng.org/.
21 * It's licensed under the GNU General Public License,
22 * please see the file GPL in the top level source directory.
27 rx_t *rx_compile(const char *s, int flags)
29 rx_t *pp = p_new(rx_t, 1);
31 pp->pattern = m_strdup(s);
32 pp->rx = p_new(regex_t, 1);
35 if (REGCOMP(pp->rx, NONULL(s), flags) != 0) {
44 rx_t *res = rx_compile(r->pattern, r->flags);
46 rx_set_template(res, r->tpl);
50 int rx_validate(const char *s, char *errbuf, ssize_t errlen)
56 res = REGCOMP(&re, NONULL(s), 0);
58 regerror(res, &re, errbuf, errlen);
65 void rx_set_template(rx_t *rx, const char *tpl)
69 m_strreplace(&rx->tpl, tpl);
71 if (m_strisempty(rx->tpl))
74 while ((p = strchr(p, '%'))) {
76 int n = strtol(p, (char **)&p, 10);
77 rx->nmatch = MAX(n, rx->nmatch);
84 rx->nmatch++; /* match 0 is always the whole expr */
87 void rx_wipe(rx_t *rx)
89 p_delete(&rx->pattern);
95 int rx_list_match(rx_t *l, const char *s)
101 if (!REGEXEC(l->rx, s))
109 int rx_list_match2(rx_t *l, const char *s, char *dst, int dlen)
111 static regmatch_t *pmatch = NULL;
112 static int nmatch = 0;
118 for (; l; l = l->next) {
119 if (l->nmatch > nmatch) {
120 p_realloc(&pmatch, l->nmatch);
124 if (regexec(l->rx, s, l->nmatch, pmatch, 0) == 0) {
125 /* Copy template into dst, with substitutions. */
126 const char *p = l->tpl, *q;
128 for (q = strchr(p, '%'); q; q = strchr(p + 1, '%')) {
131 pos += m_strncpy(dst + pos, dlen - pos, p, q - p);
133 if (!isdigit((unsigned char)q[1])) {
134 p = q + (q[1] == '%');
138 n = strtol(q + 1, (char **)&p, 10); /* find pmatch index */
139 pos += m_strncpy(dst + pos, dlen - pos, s + pmatch[n].rm_so,
140 pmatch[n].rm_eo - pmatch[n].rm_so);
143 pos += m_strcpy(dst + pos, dlen - pos, p);
151 rx_t **rx_lookup(rx_t **l, const char *pat)
153 if (m_strisempty(pat))
157 if (!m_strcmp((*l)->pattern, pat))
165 void rx_list_add(rx_t **l, rx_t *rxp)
167 l = rx_lookup(l, rxp->pattern);
169 rx_t *r = rx_list_pop(l);
172 rx_list_push(l, rxp);
175 void rx_list_add2(rx_t **l, rx_t **rxp)
177 l = rx_lookup(l, (*rxp)->pattern);
179 rx_t *r = rx_list_pop(l);
182 if (m_strisempty((*rxp)->tpl)) {
185 rx_list_push(l, *rxp);
189 void rx_list_remove(rx_t **l, const rx_t *r)
191 l = rx_lookup(l, r->pattern);
193 rx_t *tmp = rx_list_pop(l);
198 int rx_sanitize_string(char *dst, ssize_t n, const char *src)
204 /* these characters must be escaped in regular expressions */
205 if (strchr("^.[$()|*+?{\\", *src)) {
219 return *src ? -1 : 0;