-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathregexp.c
290 lines (268 loc) · 9.49 KB
/
regexp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/****************************************************************************/
/* pfixtools: a collection of postfix related tools */
/* ~~~~~~~~~ */
/* ______________________________________________________________________ */
/* */
/* Redistribution and use in source and binary forms, with or without */
/* modification, are permitted provided that the following conditions */
/* are met: */
/* */
/* 1. Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* 2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the */
/* distribution. */
/* 3. The names of its contributors may not be used to endorse or promote */
/* products derived from this software without specific prior written */
/* permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY */
/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE */
/* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
/* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
/* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
/* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, */
/* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE */
/* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
/* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/* Copyright (c) 2006-2014 the Authors */
/* see AUTHORS and source files for details */
/****************************************************************************/
#include "regexp.h"
regexp_t *regexp_new(void)
{
return p_new(regexp_t, 1);
}
void regexp_wipe(regexp_t *re)
{
pcre_free(re->re);
pcre_free(re->extra);
}
void regexp_delete(regexp_t **re)
{
if (*re) {
regexp_wipe(*re);
p_delete(re);
}
}
bool regexp_compile(regexp_t *re, const char *str, bool cs)
{
const char *error = NULL;
int erroffset = 0;
int flags = (cs ? 0 : PCRE_CASELESS);
debug("compiling regexp: %s", str);
re->re = pcre_compile(str, flags, &error, &erroffset, NULL);
if (re->re == NULL) {
err("cannot compile regexp: %s (at %d)", error, erroffset);
return false;
}
re->extra = pcre_study(re->re, 0, &error);
if (re->extra == NULL && error != NULL) {
warn("regexp inspection failed: %s", error);
}
return true;
}
bool regexp_compile_str(regexp_t *re, const clstr_t *str, bool cs)
{
if (str->str[str->len] == '\0') {
return regexp_compile(re, str->str, cs);
} else {
// TODO: Use a buffer to avoid stupid allocations
char *cpy = p_dupstr(str->str, str->len);
bool ok = regexp_compile(re, cpy, cs);
p_delete(&cpy);
return ok;
}
}
bool regexp_match_str(const regexp_t *re, const clstr_t *str)
{
return 0 == pcre_exec(re->re, re->extra, str->str, str->len,
0, 0, NULL, 0);
}
bool regexp_match(const regexp_t *re, const char *str)
{
clstr_t s = { str, m_strlen(str) };
return regexp_match_str(re, &s);
}
/** Returns true if the character has a special meaning in regexp when
* non escaped.
*/
static inline bool is_wildcard(const char c) {
return c == '.' || c == '$' || c == '^'
|| c == '?' || c == '+' || c == '*' || c == '|'
|| c == '(' || c == ')' || c == '[' || c == ']'
|| c == '{' || c == '}';
}
/** Returns true if the character affects the previous character behaviour
* in a regexp when non escaped.
*/
static inline bool is_modifier(const char c) {
return c == '?' || c == '+' || c == '*' || c == '{';
}
/** Returns true if the character has a special in regexp when espaced.
*/
static inline bool is_special(const char c) {
return c == 'x' || c == '0'
|| c == 'd' || c == 'D' || c == 'h' || c == 'H'
|| c == 's' || c == 'S' || c == 'v' || c == 'V'
|| c == 'w' || c == 'W' || c == 'b' || c == 'B';
}
/** Return true if the character is a valid regexp delimiter.
*/
static inline bool is_valid_delimiter(const char c) {
return !isspace(c) && !isalnum(c) && isascii(c) && !is_wildcard(c);
}
bool regexp_parse_str(const clstr_t *str, buffer_t *prefix,
buffer_t *re, buffer_t *suffix, bool *cs) {
if (str == NULL || re == NULL || str->len < 2) {
err("Invalid argument");
return false;
}
const char *p = str->str;
const char *end = str->str + str->len;
char delim = *(p++);
if (!is_valid_delimiter(delim)) {
err("Invalid delimiter %c", delim);
return false;
}
buffer_reset(re);
/* Read literal prefix */
if (prefix != NULL) {
buffer_reset(prefix);
if (*p == '^') {
buffer_addch(re, '^');
++p;
while (p < end - 1 && !is_wildcard(*p) && *p != delim) {
if (*p == '\\') {
++p;
if (is_special(*p)) {
--p;
break;
}
}
buffer_addch(prefix, *p);
++p;
}
if (p >= end) {
err("Reached the end of the regexp");
return false;
}
if (is_modifier(*p)) {
--prefix->len;
prefix->data[prefix->len] = '\0';
}
}
}
/* Read the regexp */
while (p < end && *p != delim) {
if (*p == '\\') {
++p;
if (p == end) {
err("Read the end of the regexp");
return false;
}
if (*p != delim) {
buffer_addch(re, '\\');
}
}
buffer_addch(re, *p);
++p;
}
if (p == end) {
err("Reached the end of the regexp");
return false;
}
if (*p != delim) {
err("Invalid end of regexp, found %c while expecting %c", *p, delim);
return false;
}
/* Read the modifiers (if any) */
++p;
if (cs) {
*cs = true;
}
if (p < end) {
if (*p != 'i') {
err("Invalid regexp modifier %c", *p);
return false;
}
if (cs) {
*cs = false;
}
}
/* Extract literal suffix (if any) */
if (suffix != NULL) {
suffix->len = 0;
if (re->len > 0 && array_last(*re) == '$') {
int pos = re->len - 2;
int bs = 0;
while (pos >= 0 && array_elt(*re, pos) == '\\') {
--pos;
++bs;
}
if ((bs & 1) == 1) {
/* The termination $ is escaped, it is not a termination,
* so, no suffix
*/
return true;
}
bs = 0;
while (pos >= 0) {
const char c = array_elt(*re, pos);
bs = 0;
if (is_wildcard(c) || is_special(c)) {
--pos;
while (pos >= 0 && array_elt(*re, pos) == '\\') {
--pos;
++bs;
}
if (is_wildcard(c)) {
if ((bs & 1) == 0) {
/* Character not escaped, break, wildcard found */
++pos;
break;
}
} else {
if ((bs & 1) == 1) {
/* Special sequence */
++pos;
break;
}
}
} else {
--pos;
}
}
if (pos != 0 || bs != 0) {
pos += bs + 1;
}
bs = pos;
while (pos < (int)re->len - 1) {
char c = array_elt(*re, pos);
if (c == '\\') {
++pos;
c = array_elt(*re, pos);
}
buffer_addch(suffix, c);
++pos;
}
if (pos == (int)re->len) {
return false;
}
re->len = bs;
buffer_addch(re, '$');
}
}
return true;
}
bool regexp_parse(const char *str, buffer_t *prefix, buffer_t *re,
buffer_t *suffix, bool *cs)
{
clstr_t s = { str, m_strlen(str) };
return regexp_parse_str(&s, prefix, re, suffix, cs);
}
/* vim:set et sw=4 sts=4 sws=4: */