fix the real bug, that was hidden in the .h
[apps/madmutt.git] / lib-lib / rx.c
1 /*
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.
6  *
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.
11  *
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,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
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.
23  */
24
25 #include "lib-lib.h"
26
27 rx_t *rx_compile(const char *s, int flags)
28 {
29     rx_t *pp = p_new(rx_t, 1);
30
31     pp->pattern = m_strdup(s);
32     pp->rx = p_new(regex_t, 1);
33
34     if (REGCOMP(pp->rx, NONULL(s), flags) != 0) {
35         rx_delete(&pp);
36     }
37
38     return pp;
39 }
40
41 void rx_set_template(rx_t *rx, const char *tpl)
42 {
43     const char *p = tpl;
44
45     m_strreplace(&rx->template, tpl);
46     rx->nmatch = 0;
47
48     while ((p = strchr(p, '%'))) {
49         if (isdigit(*++p)) {
50             int n = strtol(p, (char **)&p, 10);
51             rx->nmatch = MAX(n, rx->nmatch);
52         } else {
53             if (*p == '%')
54                 p++;
55         }
56     }
57
58     rx->nmatch++;     /* match 0 is always the whole expr */
59 }
60
61 void rx_delete(rx_t **p)
62 {
63     p_delete(&(*p)->pattern);
64     regfree((*p)->rx);
65     p_delete(&(*p)->rx);
66     p_delete(&(*p)->template);
67     p_delete(p);
68 }
69
70 int rx_list_match(rx_t *l, const char *s)
71 {
72     if (m_strisempty(s))
73         return 0;
74
75     while (l) {
76         if (!REGEXEC(l->rx, s))
77             return 1;
78         l = l->next;
79     }
80
81     return 0;
82 }
83
84 int rx_list_match2(rx_t *l, const char *s, char *dst, int dlen)
85 {
86     static regmatch_t *pmatch = NULL;
87     static int nmatch = 0;
88     int pos = 0;
89
90     if (m_strisempty(s))
91         return 0;
92
93     for (; l; l = l->next) {
94         if (l->nmatch > nmatch) {
95             p_realloc(&pmatch, l->nmatch);
96             nmatch = l->nmatch;
97         }
98
99         if (regexec(l->rx, s, l->nmatch, pmatch, 0) == 0) {
100             /* Copy template into dst, with substitutions. */
101             const char *p = l->template, *q;
102
103             for (q = strchr(p, '%'); q; q = strchr(p + 1, '%')) {
104                 int n;
105
106                 pos += m_strncpy(dst + pos, dlen - pos, p, q - p);
107
108                 if (!isdigit((unsigned char)q[1])) {
109                     p = q + (q[1] == '%');
110                     continue;
111                 }
112
113                 n = strtol(q + 1, (char **)&p, 10); /* find pmatch index */
114                 pos += m_strncpy(dst + pos, dlen - pos, s + pmatch[n].rm_so,
115                                  pmatch[n].rm_eo - pmatch[n].rm_so);
116             }
117
118             pos += m_strcpy(dst + pos, dlen - pos, p);
119             return 1;
120         }
121     }
122
123     return 0;
124 }
125
126 rx_t **rx_lookup(rx_t **l, const char *pat)
127 {
128     if (m_strisempty(pat))
129         return NULL;
130
131     while (*l) {
132         if (!m_strcmp((*l)->pattern, pat))
133             return l;
134         l = &(*l)->next;
135     }
136
137     return NULL;
138 }
139
140 int rx_sanitize_string(char *dst, ssize_t n, const char *src)
141 {
142     while (*src) {
143         if (n <= 1)
144             break;
145
146         /* these characters must be escaped in regular expressions */
147         if (strchr("^.[$()|*+?{\\", *src)) {
148             if (n <= 2)
149                 break;
150
151             *dst++ = '\\';
152             n--;
153         }
154
155         *dst++ = *src++;
156         n--;
157     }
158
159     *dst = '\0';
160
161     return *src ? -1 : 0;
162 }