2 * UCW 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_OWN_REGEX) || defined(CONFIG_POSIX_REGEX)
21 /* POSIX regular expression library */
23 #ifdef CONFIG_OWN_REGEX
24 #include "lib/regex/regex-sh.h"
31 regmatch_t matches[10];
35 rx_compile(byte *p, int icase)
37 regex *r = xmalloc_zero(sizeof(regex));
39 int err = regcomp(&r->rx, p, REG_EXTENDED | (icase ? REG_ICASE : 0));
43 regerror(err, &r->rx, msg, sizeof(msg)-1);
44 /* regfree(&r->rx) not needed */
45 die("Error parsing regular expression `%s': %s", p, msg);
58 rx_match(regex *r, byte *s)
60 int err = regexec(&r->rx, s, 10, r->matches, 0);
63 /* regexec doesn't support anchored expressions, so we have to check ourselves that the full string is matched */
64 return !(r->matches[0].rm_so || s[r->matches[0].rm_eo]);
66 else if (err == REG_NOMATCH)
68 else if (err == REG_ESPACE)
69 die("Regex matching ran out of memory");
71 die("Regex matching failed with unknown error %d", err);
75 rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen)
77 byte *end = dest + destlen - 1;
79 if (!rx_match(r, src))
87 if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
90 if (j <= r->rx.re_nsub && r->matches[j].rm_so >= 0)
92 byte *s = src + r->matches[j].rm_so;
93 uns i = r->matches[j].rm_eo - r->matches[j].rm_so;
111 #elif defined(CONFIG_PCRE)
120 uns match_array_size;
122 int matches[0]; /* (max_matches+1) pairs (pos,len) plus some workspace */
126 rx_compile(byte *p, int icase)
129 int errpos, match_array_size, eno;
131 pcre *rx = pcre_compile(p, PCRE_ANCHORED | PCRE_EXTRA | (icase ? PCRE_CASELESS : 0), &err, &errpos, NULL);
133 die("Error parsing regular expression `%s': %s at position %d", p, err, errpos);
134 eno = pcre_fullinfo(rx, NULL, PCRE_INFO_CAPTURECOUNT, &match_array_size);
136 die("Internal error: pcre_fullinfo() failed with error %d", eno);
137 match_array_size = 3*(match_array_size+1);
138 regex *r = xmalloc_zero(sizeof(regex) + match_array_size * sizeof(int));
140 r->match_array_size = match_array_size;
141 r->extra = pcre_study(r->rx, 0, &err);
143 die("Error studying regular expression `%s': %s", p, err);
156 rx_match(regex *r, byte *s)
158 int len = str_len(s);
159 int err = pcre_exec(r->rx, r->extra, s, len, 0, 0, r->matches, r->match_array_size);
162 r->real_matches = err;
163 /* need to check that the full string matches */
164 return !(r->matches[0] || s[r->matches[1]]);
166 else if (err == PCRE_ERROR_NOMATCH)
168 else if (err == PCRE_ERROR_NOMEMORY)
169 die("Regex matching ran out of memory");
171 die("Regex matching failed with unknown error %d", err);
175 rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen)
177 byte *end = dest + destlen - 1;
179 if (!rx_match(r, src))
187 if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
190 if (j < r->real_matches && r->matches[2*j] >= 0)
192 byte *s = src + r->matches[2*j];
193 uns i = r->matches[2*j+1] - r->matches[2*j];
213 /* BSD regular expression library */
215 #ifdef CONFIG_OWN_BSD_REGEX
216 #include "lib/regex/regex-sh.h"
221 #define INITIAL_MEM 1024 /* Initial space allocated for each pattern */
222 #define CHAR_SET_SIZE 256 /* How many characters in the character set. */
225 struct re_pattern_buffer buf;
226 struct re_registers regs; /* Must not change between re_match() calls */
231 rx_compile(byte *p, int icase)
233 regex *r = xmalloc_zero(sizeof(regex));
236 r->buf.buffer = xmalloc(INITIAL_MEM);
237 r->buf.allocated = INITIAL_MEM;
241 r->buf.translate = xmalloc (CHAR_SET_SIZE);
242 /* Map uppercase characters to corresponding lowercase ones. */
243 for (i = 0; i < CHAR_SET_SIZE; i++)
244 r->buf.translate[i] = Cupcase(i);
247 r->buf.translate = NULL;
248 re_set_syntax(RE_SYNTAX_POSIX_EXTENDED);
249 msg = re_compile_pattern(p, strlen(p), &r->buf);
252 die("Error parsing pattern `%s': %s", p, msg);
258 xfree(r->buf.buffer);
259 if (r->buf.translate)
260 xfree(r->buf.translate);
265 rx_match(regex *r, byte *s)
270 if (re_match(&r->buf, s, len, 0, &r->regs) < 0)
272 if (r->regs.start[0] || r->regs.end[0] != len) /* XXX: Why regex doesn't enforce implicit "^...$" ? */
278 rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen)
280 byte *end = dest + destlen - 1;
282 if (!rx_match(r, src))
290 if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
293 if (j < r->regs.num_regs)
295 byte *s = src + r->regs.start[j];
296 uns i = r->regs.end[j] - r->regs.start[j];
297 if (r->regs.start[j] > r->len_cache || r->regs.end[j] > r->len_cache)
320 int main(int argc, char **argv)
323 byte buf1[4096], buf2[4096];
326 if (!strcmp(argv[1], "-i"))
332 r = rx_compile(argv[1], opt_i);
333 while (fgets(buf1, sizeof(buf1), stdin))
335 char *p = strchr(buf1, '\n');
340 if (rx_match(r, buf1))
347 int i = rx_subst(r, argv[2], buf1, buf2, sizeof(buf2));