X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fregex.c;h=f952333e7e061aabd30dd5013394d32b86017cd1;hb=81a17ab471944888106c70a719e99d6e6ba96f22;hp=b74fb6a290f5abdfe6b0fa5b3f70aec4942056f5;hpb=97a9eb186ea7f6726258e164d75503ebb18fc490;p=libucw.git diff --git a/lib/regex.c b/lib/regex.c index b74fb6a2..f952333e 100644 --- a/lib/regex.c +++ b/lib/regex.c @@ -16,112 +16,15 @@ #include #include -#if 1 - -/* BSD regular expression library */ - -#include - -#define INITIAL_MEM 1024 /* Initial space allocated for each pattern */ -#define CHAR_SET_SIZE 256 /* How many characters in the character set. */ - -struct regex { - struct re_pattern_buffer buf; - struct re_registers regs; /* Must not change between re_match() calls */ - int len_cache; -}; - -regex * -rx_compile(byte *p, int icase) -{ - regex *r = xmalloc_zero(sizeof(regex)); - const char *msg; - - r->buf.buffer = xmalloc(INITIAL_MEM); - r->buf.allocated = INITIAL_MEM; - if (icase) - { - unsigned i; - r->buf.translate = xmalloc (CHAR_SET_SIZE); - /* Map uppercase characters to corresponding lowercase ones. */ - for (i = 0; i < CHAR_SET_SIZE; i++) - r->buf.translate[i] = Cupcase(i); - } - else - r->buf.translate = NULL; - re_set_syntax(RE_SYNTAX_POSIX_EXTENDED); - msg = re_compile_pattern(p, strlen(p), &r->buf); - if (!msg) - return r; - die("Error parsing pattern `%s': %s", p, msg); -} - -void -rx_free(regex *r) -{ - xfree(r->buf.buffer); - if (r->buf.translate) - xfree(r->buf.translate); - xfree(r); -} - -int -rx_match(regex *r, byte *s) -{ - int len = strlen(s); - - r->len_cache = len; - if (re_match(&r->buf, s, len, 0, &r->regs) < 0) - return 0; - if (r->regs.start[0] || r->regs.end[0] != len) /* XXX: Why regex doesn't enforce implicit "^...$" ? */ - return 0; - return 1; -} - -int -rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) -{ - byte *end = dest + destlen - 1; - - if (!rx_match(r, src)) - return 0; - - while (*by) - { - if (*by == '\\') - { - by++; - if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */ - { - uns j = *by++ - '0'; - if (j < r->regs.num_regs) - { - byte *s = src + r->regs.start[j]; - uns i = r->regs.end[j] - r->regs.start[j]; - if (r->regs.start[j] > r->len_cache || r->regs.end[j] > r->len_cache) - return -1; - if (dest + i >= end) - return -1; - memcpy(dest, s, i); - dest += i; - continue; - } - } - } - if (dest < end) - *dest++ = *by++; - else - return -1; - } - *dest = 0; - return 1; -} - -#elif 0 +#if defined(CONFIG_OWN_REGEX) || defined(CONFIG_POSIX_REGEX) /* POSIX regular expression library */ +#ifdef CONFIG_OWN_REGEX +#include "lib/regex/regex-sh.h" +#else #include +#endif struct regex { regex_t rx; @@ -205,7 +108,7 @@ rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) return 1; } -#else +#elif defined(CONFIG_PCRE) /* PCRE library */ @@ -305,6 +208,111 @@ rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) return 1; } +#else + +/* BSD regular expression library */ + +#ifdef CONFIG_OWN_BSD_REGEX +#include "lib/regex/regex-sh.h" +#else +#include +#endif + +#define INITIAL_MEM 1024 /* Initial space allocated for each pattern */ +#define CHAR_SET_SIZE 256 /* How many characters in the character set. */ + +struct regex { + struct re_pattern_buffer buf; + struct re_registers regs; /* Must not change between re_match() calls */ + int len_cache; +}; + +regex * +rx_compile(byte *p, int icase) +{ + regex *r = xmalloc_zero(sizeof(regex)); + const char *msg; + + r->buf.buffer = xmalloc(INITIAL_MEM); + r->buf.allocated = INITIAL_MEM; + if (icase) + { + unsigned i; + r->buf.translate = xmalloc (CHAR_SET_SIZE); + /* Map uppercase characters to corresponding lowercase ones. */ + for (i = 0; i < CHAR_SET_SIZE; i++) + r->buf.translate[i] = Cupcase(i); + } + else + r->buf.translate = NULL; + re_set_syntax(RE_SYNTAX_POSIX_EXTENDED); + msg = re_compile_pattern(p, strlen(p), &r->buf); + if (!msg) + return r; + die("Error parsing pattern `%s': %s", p, msg); +} + +void +rx_free(regex *r) +{ + xfree(r->buf.buffer); + if (r->buf.translate) + xfree(r->buf.translate); + xfree(r); +} + +int +rx_match(regex *r, byte *s) +{ + int len = strlen(s); + + r->len_cache = len; + if (re_match(&r->buf, s, len, 0, &r->regs) < 0) + return 0; + if (r->regs.start[0] || r->regs.end[0] != len) /* XXX: Why regex doesn't enforce implicit "^...$" ? */ + return 0; + return 1; +} + +int +rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) +{ + byte *end = dest + destlen - 1; + + if (!rx_match(r, src)) + return 0; + + while (*by) + { + if (*by == '\\') + { + by++; + if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */ + { + uns j = *by++ - '0'; + if (j < r->regs.num_regs) + { + byte *s = src + r->regs.start[j]; + uns i = r->regs.end[j] - r->regs.start[j]; + if (r->regs.start[j] > r->len_cache || r->regs.end[j] > r->len_cache) + return -1; + if (dest + i >= end) + return -1; + memcpy(dest, s, i); + dest += i; + continue; + } + } + } + if (dest < end) + *dest++ = *by++; + else + return -1; + } + *dest = 0; + return 1; +} + #endif #ifdef TEST @@ -312,9 +320,16 @@ rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) int main(int argc, char **argv) { regex *r; - byte buf1[256], buf2[256]; + byte buf1[4096], buf2[4096]; + int opt_i = 0; - r = rx_compile(argv[1], 0); + if (!strcmp(argv[1], "-i")) + { + opt_i = 1; + argv++; + argc--; + } + r = rx_compile(argv[1], opt_i); while (fgets(buf1, sizeof(buf1), stdin)) { char *p = strchr(buf1, '\n');