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 "ucw/chartype.h"
13 #include "ucw/hashfunc.h"
14 #include "ucw/regex.h"
19 #ifdef CONFIG_POSIX_REGEX
21 /* POSIX regular expression library */
27 regmatch_t matches[10];
31 rx_compile(const char *p, int icase)
33 regex *r = xmalloc_zero(sizeof(regex));
35 int err = regcomp(&r->rx, p, REG_EXTENDED | (icase ? REG_ICASE : 0));
39 regerror(err, &r->rx, msg, sizeof(msg)-1);
40 /* regfree(&r->rx) not needed */
41 die("Error parsing regular expression `%s': %s", p, msg);
54 rx_match(regex *r, const char *s)
56 int err = regexec(&r->rx, s, 10, r->matches, 0);
59 /* regexec doesn't support anchored expressions, so we have to check ourselves that the full string is matched */
60 return !(r->matches[0].rm_so || s[r->matches[0].rm_eo]);
62 else if (err == REG_NOMATCH)
64 else if (err == REG_ESPACE)
65 die("Regex matching ran out of memory");
67 die("Regex matching failed with unknown error %d", err);
71 rx_subst(regex *r, const char *by, const char *src, char *dest, uns destlen)
73 char *end = dest + destlen - 1;
75 if (!rx_match(r, src))
83 if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
86 if (j <= r->rx.re_nsub && r->matches[j].rm_so >= 0)
88 const char *s = src + r->matches[j].rm_so;
89 uns i = r->matches[j].rm_eo - r->matches[j].rm_so;
107 #elif defined(CONFIG_PCRE)
116 uns match_array_size;
118 int matches[0]; /* (max_matches+1) pairs (pos,len) plus some workspace */
122 rx_compile(const char *p, int icase)
125 int errpos, match_array_size, eno;
127 pcre *rx = pcre_compile(p, PCRE_ANCHORED | PCRE_EXTRA | (icase ? PCRE_CASELESS : 0), &err, &errpos, NULL);
129 die("Error parsing regular expression `%s': %s at position %d", p, err, errpos);
130 eno = pcre_fullinfo(rx, NULL, PCRE_INFO_CAPTURECOUNT, &match_array_size);
132 die("Internal error: pcre_fullinfo() failed with error %d", eno);
133 match_array_size = 3*(match_array_size+1);
134 regex *r = xmalloc_zero(sizeof(regex) + match_array_size * sizeof(int));
136 r->match_array_size = match_array_size;
137 r->extra = pcre_study(r->rx, 0, &err);
139 die("Error studying regular expression `%s': %s", p, err);
152 rx_match(regex *r, const char *s)
154 int len = str_len(s);
155 int err = pcre_exec(r->rx, r->extra, s, len, 0, 0, r->matches, r->match_array_size);
158 r->real_matches = err;
159 /* need to check that the full string matches */
160 return !(r->matches[0] || s[r->matches[1]]);
162 else if (err == PCRE_ERROR_NOMATCH)
164 else if (err == PCRE_ERROR_NOMEMORY)
165 die("Regex matching ran out of memory");
167 die("Regex matching failed with unknown error %d", err);
171 rx_subst(regex *r, const char *by, const char *src, char *dest, uns destlen)
173 char *end = dest + destlen - 1;
175 if (!rx_match(r, src))
183 if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
186 if (j < r->real_matches && r->matches[2*j] >= 0)
188 char *s = src + r->matches[2*j];
189 uns i = r->matches[2*j+1] - r->matches[2*j];
209 /* BSD regular expression library */
211 #ifdef CONFIG_OWN_BSD_REGEX
212 #include "ucw/regex/regex-sh.h"
217 #define INITIAL_MEM 1024 /* Initial space allocated for each pattern */
218 #define CHAR_SET_SIZE 256 /* How many characters in the character set. */
221 struct re_pattern_buffer buf;
222 struct re_registers regs; /* Must not change between re_match() calls */
227 rx_compile(const char *p, int icase)
229 regex *r = xmalloc_zero(sizeof(regex));
232 r->buf.buffer = xmalloc(INITIAL_MEM);
233 r->buf.allocated = INITIAL_MEM;
237 r->buf.translate = xmalloc (CHAR_SET_SIZE);
238 /* Map uppercase characters to corresponding lowercase ones. */
239 for (i = 0; i < CHAR_SET_SIZE; i++)
240 r->buf.translate[i] = Cupcase(i);
243 r->buf.translate = NULL;
244 re_set_syntax(RE_SYNTAX_POSIX_EXTENDED);
245 msg = re_compile_pattern(p, strlen(p), &r->buf);
248 die("Error parsing pattern `%s': %s", p, msg);
254 xfree(r->buf.buffer);
255 if (r->buf.translate)
256 xfree(r->buf.translate);
261 rx_match(regex *r, const char *s)
266 if (re_match(&r->buf, s, len, 0, &r->regs) < 0)
268 if (r->regs.start[0] || r->regs.end[0] != len) /* XXX: Why regex doesn't enforce implicit "^...$" ? */
274 rx_subst(regex *r, const char *by, const char *src, char *dest, uns destlen)
276 char *end = dest + destlen - 1;
278 if (!rx_match(r, src))
286 if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
289 if (j < r->regs.num_regs)
291 const char *s = src + r->regs.start[j];
292 uns i = r->regs.end[j] - r->regs.start[j];
293 if (r->regs.start[j] > r->len_cache || r->regs.end[j] > r->len_cache)
316 int main(int argc, char **argv)
319 char buf1[4096], buf2[4096];
322 if (!strcmp(argv[1], "-i"))
328 r = rx_compile(argv[1], opt_i);
329 while (fgets(buf1, sizeof(buf1), stdin))
331 char *p = strchr(buf1, '\n');
336 if (rx_match(r, buf1))
343 int i = rx_subst(r, argv[2], buf1, buf2, sizeof(buf2));