]> mj.ucw.cz Git - libucw.git/blobdiff - lib/regex.c
shup up, gcc
[libucw.git] / lib / regex.c
index b74fb6a290f5abdfe6b0fa5b3f70aec4942056f5..e1054fb35ef5321eea24cefde14566be60d3fb39 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- *     Sherlock Library -- Interface to Regular Expression Libraries
+ *     UCW Library -- Interface to Regular Expression Libraries
  *
  *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
  *     (c) 2001 Robert Spalek <robert@ucw.cz>
  *
  *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
  *     (c) 2001 Robert Spalek <robert@ucw.cz>
 #include <string.h>
 #include <stdlib.h>
 
 #include <string.h>
 #include <stdlib.h>
 
-#if 1
-
-/* BSD regular expression library */
-
-#include <regex.h>
-
-#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 */
 
 
 /* POSIX regular expression library */
 
+#ifdef CONFIG_OWN_REGEX
+#include "lib/regex/regex-sh.h"
+#else
 #include <regex.h>
 #include <regex.h>
+#endif
 
 struct regex {
   regex_t rx;
 
 struct regex {
   regex_t rx;
@@ -205,7 +108,7 @@ rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen)
   return 1;
 }
 
   return 1;
 }
 
-#else
+#elif defined(CONFIG_PCRE)
 
 /* PCRE library */
 
 
 /* PCRE library */
 
@@ -305,6 +208,111 @@ rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen)
   return 1;
 }
 
   return 1;
 }
 
+#else
+
+/* BSD regular expression library */
+
+#ifdef CONFIG_OWN_BSD_REGEX
+#include "lib/regex/regex-sh.h"
+#else
+#include <regex.h>
+#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
 #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;
 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');
   while (fgets(buf1, sizeof(buf1), stdin))
     {
       char *p = strchr(buf1, '\n');