2 * Sherlock Library -- Interface to Regular Expression Libraries
4 * (c) 1997--2004 Martin Mares <mj@ucw.cz>
5 * (c) 2001 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include "lib/chartype.h"
13 #include "lib/hashfunc.h"
19 #if !defined(CONFIG_PCRE) && !defined(CONFIG_POSIX_RE)
21 /* BSD regular expression library */
25 #define INITIAL_MEM 1024 /* Initial space allocated for each pattern */
26 #define CHAR_SET_SIZE 256 /* How many characters in the character set. */
29 struct re_pattern_buffer buf;
30 struct re_registers regs; /* Must not change between re_match() calls */
35 rx_compile(byte *p, int icase)
37 regex *r = xmalloc_zero(sizeof(regex));
40 r->buf.buffer = xmalloc(INITIAL_MEM);
41 r->buf.allocated = INITIAL_MEM;
45 r->buf.translate = xmalloc (CHAR_SET_SIZE);
46 /* Map uppercase characters to corresponding lowercase ones. */
47 for (i = 0; i < CHAR_SET_SIZE; i++)
48 r->buf.translate[i] = Cupcase(i);
51 r->buf.translate = NULL;
52 re_set_syntax(RE_SYNTAX_POSIX_EXTENDED);
53 msg = re_compile_pattern(p, strlen(p), &r->buf);
56 die("Error parsing pattern `%s': %s", p, msg);
64 xfree(r->buf.translate);
69 rx_match(regex *r, byte *s)
74 if (re_match(&r->buf, s, len, 0, &r->regs) < 0)
76 if (r->regs.start[0] || r->regs.end[0] != len) /* XXX: Why regex doesn't enforce implicit "^...$" ? */
82 rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen)
84 byte *end = dest + destlen - 1;
86 if (!rx_match(r, src))
94 if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
97 if (j < r->regs.num_regs)
99 byte *s = src + r->regs.start[j];
100 uns i = r->regs.end[j] - r->regs.start[j];
101 if (r->regs.start[j] > r->len_cache || r->regs.end[j] > r->len_cache)
120 #elif defined(CONFIG_POSIX_RE)
122 /* POSIX regular expression library */
128 regmatch_t matches[10];
132 rx_compile(byte *p, int icase)
134 regex *r = xmalloc_zero(sizeof(regex));
136 int err = regcomp(&r->rx, p, REG_EXTENDED | (icase ? REG_ICASE : 0));
140 regerror(err, &r->rx, msg, sizeof(msg)-1);
141 /* regfree(&r->rx) not needed */
142 die("Error parsing regular expression `%s': %s", p, msg);
155 rx_match(regex *r, byte *s)
157 int err = regexec(&r->rx, s, 10, r->matches, 0);
160 /* regexec doesn't support anchored expressions, so we have to check ourselves that the full string is matched */
161 return !(r->matches[0].rm_so || s[r->matches[0].rm_eo]);
163 else if (err == REG_NOMATCH)
165 else if (err == REG_ESPACE)
166 die("Regex matching ran out of memory");
168 die("Regex matching failed with unknown error %d", err);
172 rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen)
174 byte *end = dest + destlen - 1;
176 if (!rx_match(r, src))
184 if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
187 if (j <= r->rx.re_nsub && r->matches[j].rm_so >= 0)
189 byte *s = src + r->matches[j].rm_so;
190 uns i = r->matches[j].rm_eo - r->matches[j].rm_so;
217 uns match_array_size;
219 int matches[0]; /* (max_matches+1) pairs (pos,len) plus some workspace */
223 rx_compile(byte *p, int icase)
226 int errpos, match_array_size, eno;
228 pcre *rx = pcre_compile(p, PCRE_ANCHORED | PCRE_EXTRA | (icase ? PCRE_CASELESS : 0), &err, &errpos, NULL);
230 die("Error parsing regular expression `%s': %s at position %d", p, err, errpos);
231 eno = pcre_fullinfo(rx, NULL, PCRE_INFO_CAPTURECOUNT, &match_array_size);
233 die("Internal error: pcre_fullinfo() failed with error %d", eno);
234 match_array_size = 3*(match_array_size+1);
235 regex *r = xmalloc_zero(sizeof(regex) + match_array_size * sizeof(int));
237 r->match_array_size = match_array_size;
238 r->extra = pcre_study(r->rx, 0, &err);
240 die("Error studying regular expression `%s': %s", p, err);
253 rx_match(regex *r, byte *s)
255 int len = str_len(s);
256 int err = pcre_exec(r->rx, r->extra, s, len, 0, 0, r->matches, r->match_array_size);
259 r->real_matches = err;
260 /* need to check that the full string matches */
261 return !(r->matches[0] || s[r->matches[1]]);
263 else if (err == PCRE_ERROR_NOMATCH)
265 else if (err == PCRE_ERROR_NOMEMORY)
266 die("Regex matching ran out of memory");
268 die("Regex matching failed with unknown error %d", err);
272 rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen)
274 byte *end = dest + destlen - 1;
276 if (!rx_match(r, src))
284 if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
287 if (j < r->real_matches && r->matches[2*j] >= 0)
289 byte *s = src + r->matches[2*j];
290 uns i = r->matches[2*j+1] - r->matches[2*j];
312 int main(int argc, char **argv)
315 byte buf1[4096], buf2[4096];
318 if (!strcmp(argv[1], "-i"))
324 r = rx_compile(argv[1], opt_i);
325 while (fgets(buf1, sizeof(buf1), stdin))
327 char *p = strchr(buf1, '\n');
332 if (rx_match(r, buf1))
339 int i = rx_subst(r, argv[2], buf1, buf2, sizeof(buf2));