remove bruno's 11-03 entry for unicodeio.c
[gnulib.git] / lib / regex.c
index d16bd60..25a219e 100644 (file)
@@ -2,25 +2,22 @@
    version 0.12.
    (Implements POSIX draft P1003.2/D11.2, except for some of the
    internationalization features.)
-   Copyright (C) 1993, 94, 95, 96, 97, 98 Free Software Foundation, Inc.
+   Copyright (C) 1993-1999, 2000 Free Software Foundation, Inc.
 
-   NOTE: The canonical source of this file is maintained with the GNU C Library.
-   Bugs can be reported to bug-glibc@prep.ai.mit.edu.
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
 
-   This program is free software; you can redistribute it and/or modify it
-   under the terms of the GNU General Public License as published by the
-   Free Software Foundation; either version 2, or (at your option) any
-   later version.
-
-   This program is distributed in the hope that it will be useful,
+   The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
-   USA.  */
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
 
 /* AIX requires this to be the first thing in the file. */
 #if defined _AIX && !defined REGEX_MALLOC
 # include <sys/types.h>
 #endif
 
-#define WIDE_CHAR_SUPPORT \
-  defined _LIBC || (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
+#define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
 
 /* For platform which support the ISO C amendement 1 functionality we
    support user defined character classes.  */
-#if WIDE_CHAR_SUPPORT
+#if defined _LIBC || WIDE_CHAR_SUPPORT
+/* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.  */
 # include <wchar.h>
 # include <wctype.h>
 #endif
 
+#ifdef _LIBC
+/* We have to keep the namespace clean.  */
+# define regfree(preg) __regfree (preg)
+# define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
+# define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
+# define regerror(errcode, preg, errbuf, errbuf_size) \
+       __regerror(errcode, preg, errbuf, errbuf_size)
+# define re_set_registers(bu, re, nu, st, en) \
+       __re_set_registers (bu, re, nu, st, en)
+# define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
+       __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
+# define re_match(bufp, string, size, pos, regs) \
+       __re_match (bufp, string, size, pos, regs)
+# define re_search(bufp, string, size, startpos, range, regs) \
+       __re_search (bufp, string, size, startpos, range, regs)
+# define re_compile_pattern(pattern, length, bufp) \
+       __re_compile_pattern (pattern, length, bufp)
+# define re_set_syntax(syntax) __re_set_syntax (syntax)
+# define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
+       __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
+# define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
+
+# define btowc __btowc
+
+/* We are also using some library internals.  */
+# include <locale/localeinfo.h>
+# include <locale/elem-hash.h>
+# include <langinfo.h>
+#endif
+
 /* This is for other GNU distributions with internationalized messages.  */
 #if HAVE_LIBINTL_H || defined _LIBC
 # include <libintl.h>
+# ifdef _LIBC
+#  undef gettext
+#  define gettext(msgid) __dcgettext ("libc", msgid, LC_MESSAGES)
+# endif
 #else
 # define gettext(msgid) (msgid)
 #endif
@@ -110,8 +141,12 @@ char *realloc ();
 # ifndef INHIBIT_STRING_HEADER
 #  if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
 #   include <string.h>
-#   if !defined bzero && !defined _LIBC
-#    define bzero(s, n)                (memset (s, '\0', n), (s))
+#   ifndef bzero
+#    ifndef _LIBC
+#     define bzero(s, n)       (memset (s, '\0', n), (s))
+#    else
+#     define bzero(s, n)       __bzero (s, n)
+#    endif
 #   endif
 #  else
 #   include <strings.h>
@@ -138,50 +173,18 @@ char *realloc ();
 #  define SWITCH_ENUM_CAST(x) (x)
 # endif
 
-/* How many characters in the character set.  */
-# define CHAR_SET_SIZE 256
-
-# ifdef SYNTAX_TABLE
-
-extern char *re_syntax_table;
-
-# else /* not SYNTAX_TABLE */
-
-static char re_syntax_table[CHAR_SET_SIZE];
-
-static void
-init_syntax_once ()
-{
-   register int c;
-   static int done = 0;
-
-   if (done)
-     return;
-
-   bzero (re_syntax_table, sizeof re_syntax_table);
-
-   for (c = 'a'; c <= 'z'; c++)
-     re_syntax_table[c] = Sword;
-
-   for (c = 'A'; c <= 'Z'; c++)
-     re_syntax_table[c] = Sword;
-
-   for (c = '0'; c <= '9'; c++)
-     re_syntax_table[c] = Sword;
-
-   re_syntax_table['_'] = Sword;
-
-   done = 1;
-}
-
-# endif /* not SYNTAX_TABLE */
+#endif /* not emacs */
 
-# define SYNTAX(c) re_syntax_table[c]
+#if defined _LIBC || HAVE_LIMITS_H
+# include <limits.h>
+#endif
 
-#endif /* not emacs */
+#ifndef MB_LEN_MAX
+# define MB_LEN_MAX 1
+#endif
 \f
 /* Get the interface, including the syntax bits.  */
-#include "regex.h"
+#include <regex.h>
 
 /* isalpha etc. are used for the character classes.  */
 #include <ctype.h>
@@ -195,37 +198,43 @@ init_syntax_once ()
    STDC_HEADERS is defined, then autoconf has verified that the ctype
    macros don't need to be guarded with references to isascii. ...
    Defining isascii to 1 should let any compiler worth its salt
-   eliminate the && through constant folding."  */
+   eliminate the && through constant folding."
+   Solaris defines some of these symbols so we must undefine them first.  */
 
-#undef ISASCII
 #if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
-# define ISASCII(c) 1
+# define IN_CTYPE_DOMAIN(c) 1
 #else
-# define ISASCII(c) isascii(c)
+# define IN_CTYPE_DOMAIN(c) isascii(c)
 #endif
 
 #ifdef isblank
-# define ISBLANK(c) (ISASCII (c) && isblank (c))
+# define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
 #else
 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
 #endif
 #ifdef isgraph
-# define ISGRAPH(c) (ISASCII (c) && isgraph (c))
+# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
 #else
-# define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
+# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
 #endif
 
 #undef ISPRINT
-#define ISPRINT(c) (ISASCII (c) && isprint (c))
-#define ISDIGIT(c) (ISASCII (c) && isdigit (c))
-#define ISALNUM(c) (ISASCII (c) && isalnum (c))
-#define ISALPHA(c) (ISASCII (c) && isalpha (c))
-#define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
-#define ISLOWER(c) (ISASCII (c) && islower (c))
-#define ISPUNCT(c) (ISASCII (c) && ispunct (c))
-#define ISSPACE(c) (ISASCII (c) && isspace (c))
-#define ISUPPER(c) (ISASCII (c) && isupper (c))
-#define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
+#define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
+#define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
+#define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
+#define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
+#define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
+#define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
+#define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
+#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
+#define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
+#define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
+
+#ifdef _tolower
+# define TOLOWER(c) _tolower(c)
+#else
+# define TOLOWER(c) tolower(c)
+#endif
 
 #ifndef NULL
 # define NULL (void *)0
@@ -243,6 +252,43 @@ init_syntax_once ()
 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
 #endif
 \f
+#ifndef emacs
+/* How many characters in the character set.  */
+# define CHAR_SET_SIZE 256
+
+# ifdef SYNTAX_TABLE
+
+extern char *re_syntax_table;
+
+# else /* not SYNTAX_TABLE */
+
+static char re_syntax_table[CHAR_SET_SIZE];
+
+static void
+init_syntax_once ()
+{
+   register int c;
+   static int done = 0;
+
+   if (done)
+     return;
+   bzero (re_syntax_table, sizeof re_syntax_table);
+
+   for (c = 0; c < CHAR_SET_SIZE; ++c)
+     if (ISALNUM (c))
+       re_syntax_table[c] = Sword;
+
+   re_syntax_table['_'] = Sword;
+
+   done = 1;
+}
+
+# endif /* not SYNTAX_TABLE */
+
+# define SYNTAX(c) re_syntax_table[(unsigned char) (c)]
+
+#endif /* emacs */
+\f
 /* Should we use malloc or alloca?  If REGEX_MALLOC is not defined, we
    use `alloca' instead of `malloc'.  This is because using malloc in
    re_search* or re_match* could cause memory leaks when C-g is used in
@@ -582,7 +628,7 @@ extract_number_and_incr (destination, source)
 /* It is useful to test things that ``must'' be true when debugging.  */
 # include <assert.h>
 
-static int debug = 0;
+static int debug;
 
 # define DEBUG_STATEMENT(e) e
 # define DEBUG_PRINT1(x) if (debug) printf (x)
@@ -648,7 +694,11 @@ print_partial_compiled_pattern (start, end)
   /* Loop over pattern commands.  */
   while (p < pend)
     {
-      printf ("%d:\t", p - start);
+#ifdef _LIBC
+      printf ("%t:\t", p - start);
+#else
+      printf ("%ld:\t", (long int) (p - start));
+#endif
 
       switch ((re_opcode_t) *p++)
        {
@@ -738,17 +788,30 @@ print_partial_compiled_pattern (start, end)
 
        case on_failure_jump:
           extract_number_and_incr (&mcnt, &p);
-         printf ("/on_failure_jump to %d", p + mcnt - start);
+#ifdef _LIBC
+         printf ("/on_failure_jump to %t", p + mcnt - start);
+#else
+         printf ("/on_failure_jump to %ld", (long int) (p + mcnt - start));
+#endif
           break;
 
        case on_failure_keep_string_jump:
           extract_number_and_incr (&mcnt, &p);
-         printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
+#ifdef _LIBC
+         printf ("/on_failure_keep_string_jump to %t", p + mcnt - start);
+#else
+         printf ("/on_failure_keep_string_jump to %ld",
+                 (long int) (p + mcnt - start));
+#endif
           break;
 
        case dummy_failure_jump:
           extract_number_and_incr (&mcnt, &p);
-         printf ("/dummy_failure_jump to %d", p + mcnt - start);
+#ifdef _LIBC
+         printf ("/dummy_failure_jump to %t", p + mcnt - start);
+#else
+         printf ("/dummy_failure_jump to %ld", (long int) (p + mcnt - start));
+#endif
           break;
 
        case push_dummy_failure:
@@ -757,29 +820,50 @@ print_partial_compiled_pattern (start, end)
 
         case maybe_pop_jump:
           extract_number_and_incr (&mcnt, &p);
-         printf ("/maybe_pop_jump to %d", p + mcnt - start);
+#ifdef _LIBC
+         printf ("/maybe_pop_jump to %t", p + mcnt - start);
+#else
+         printf ("/maybe_pop_jump to %ld", (long int) (p + mcnt - start));
+#endif
          break;
 
         case pop_failure_jump:
          extract_number_and_incr (&mcnt, &p);
-         printf ("/pop_failure_jump to %d", p + mcnt - start);
+#ifdef _LIBC
+         printf ("/pop_failure_jump to %t", p + mcnt - start);
+#else
+         printf ("/pop_failure_jump to %ld", (long int) (p + mcnt - start));
+#endif
          break;
 
         case jump_past_alt:
          extract_number_and_incr (&mcnt, &p);
-         printf ("/jump_past_alt to %d", p + mcnt - start);
+#ifdef _LIBC
+         printf ("/jump_past_alt to %t", p + mcnt - start);
+#else
+         printf ("/jump_past_alt to %ld", (long int) (p + mcnt - start));
+#endif
          break;
 
         case jump:
          extract_number_and_incr (&mcnt, &p);
-         printf ("/jump to %d", p + mcnt - start);
+#ifdef _LIBC
+         printf ("/jump to %t", p + mcnt - start);
+#else
+         printf ("/jump to %ld", (long int) (p + mcnt - start));
+#endif
          break;
 
         case succeed_n:
           extract_number_and_incr (&mcnt, &p);
          p1 = p + mcnt;
           extract_number_and_incr (&mcnt2, &p);
-         printf ("/succeed_n to %d, %d times", p1 - start, mcnt2);
+#ifdef _LIBC
+         printf ("/succeed_n to %t, %d times", p1 - start, mcnt2);
+#else
+         printf ("/succeed_n to %ld, %d times",
+                 (long int) (p1 - start), mcnt2);
+#endif
           break;
 
         case jump_n:
@@ -793,7 +877,12 @@ print_partial_compiled_pattern (start, end)
           extract_number_and_incr (&mcnt, &p);
          p1 = p + mcnt;
           extract_number_and_incr (&mcnt2, &p);
-         printf ("/set_number_at location %d to %d", p1 - start, mcnt2);
+#ifdef _LIBC
+         printf ("/set_number_at location %t to %d", p1 - start, mcnt2);
+#else
+         printf ("/set_number_at location %ld to %d",
+                 (long int) (p1 - start), mcnt2);
+#endif
           break;
 
         case wordbound:
@@ -860,7 +949,11 @@ print_partial_compiled_pattern (start, end)
       putchar ('\n');
     }
 
-  printf ("%d:\tend of pattern.\n", p - start);
+#ifdef _LIBC
+  printf ("%t:\tend of pattern.\n", p - start);
+#else
+  printf ("%ld:\tend of pattern.\n", (long int) (p - start));
+#endif
 }
 
 
@@ -880,7 +973,11 @@ print_compiled_pattern (bufp)
       print_fastmap (bufp->fastmap);
     }
 
-  printf ("re_nsub: %d\t", bufp->re_nsub);
+#ifdef _LIBC
+  printf ("re_nsub: %Zd\t", bufp->re_nsub);
+#else
+  printf ("re_nsub: %ld\t", (long int) bufp->re_nsub);
+#endif
   printf ("regs_alloc: %d\t", bufp->regs_allocated);
   printf ("can_be_null: %d\t", bufp->can_be_null);
   printf ("newline_anchor: %d\n", bufp->newline_anchor);
@@ -971,31 +1068,88 @@ re_set_syntax (syntax)
 #endif /* DEBUG */
   return ret;
 }
+#ifdef _LIBC
+weak_alias (__re_set_syntax, re_set_syntax)
+#endif
 \f
 /* This table gives an error message for each of the error codes listed
    in regex.h.  Obviously the order here has to be same as there.
    POSIX doesn't require that we do anything for REG_NOERROR,
    but why not be nice?  */
 
-static const char *re_error_msgid[] =
+static const char re_error_msgid[] =
   {
-    gettext_noop ("Success"),  /* REG_NOERROR */
-    gettext_noop ("No match"), /* REG_NOMATCH */
-    gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
-    gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
-    gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
-    gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
-    gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
-    gettext_noop ("Unmatched [ or [^"),        /* REG_EBRACK */
-    gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
-    gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
-    gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
-    gettext_noop ("Invalid range end"),        /* REG_ERANGE */
-    gettext_noop ("Memory exhausted"), /* REG_ESPACE */
-    gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
-    gettext_noop ("Premature end of regular expression"), /* REG_EEND */
-    gettext_noop ("Regular expression too big"), /* REG_ESIZE */
-    gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
+#define REG_NOERROR_IDX        0
+    gettext_noop ("Success")   /* REG_NOERROR */
+    "\0"
+#define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
+    gettext_noop ("No match")  /* REG_NOMATCH */
+    "\0"
+#define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
+    gettext_noop ("Invalid regular expression") /* REG_BADPAT */
+    "\0"
+#define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
+    gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
+    "\0"
+#define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
+    gettext_noop ("Invalid character class name") /* REG_ECTYPE */
+    "\0"
+#define REG_EESCAPE_IDX        (REG_ECTYPE_IDX + sizeof "Invalid character class name")
+    gettext_noop ("Trailing backslash") /* REG_EESCAPE */
+    "\0"
+#define REG_ESUBREG_IDX        (REG_EESCAPE_IDX + sizeof "Trailing backslash")
+    gettext_noop ("Invalid back reference") /* REG_ESUBREG */
+    "\0"
+#define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
+    gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
+    "\0"
+#define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
+    gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
+    "\0"
+#define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
+    gettext_noop ("Unmatched \\{") /* REG_EBRACE */
+    "\0"
+#define REG_BADBR_IDX  (REG_EBRACE_IDX + sizeof "Unmatched \\{")
+    gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
+    "\0"
+#define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
+    gettext_noop ("Invalid range end") /* REG_ERANGE */
+    "\0"
+#define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
+    gettext_noop ("Memory exhausted") /* REG_ESPACE */
+    "\0"
+#define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
+    gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
+    "\0"
+#define REG_EEND_IDX   (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
+    gettext_noop ("Premature end of regular expression") /* REG_EEND */
+    "\0"
+#define REG_ESIZE_IDX  (REG_EEND_IDX + sizeof "Premature end of regular expression")
+    gettext_noop ("Regular expression too big") /* REG_ESIZE */
+    "\0"
+#define REG_ERPAREN_IDX        (REG_ESIZE_IDX + sizeof "Regular expression too big")
+    gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
+  };
+
+static const size_t re_error_msgid_idx[] =
+  {
+    REG_NOERROR_IDX,
+    REG_NOMATCH_IDX,
+    REG_BADPAT_IDX,
+    REG_ECOLLATE_IDX,
+    REG_ECTYPE_IDX,
+    REG_EESCAPE_IDX,
+    REG_ESUBREG_IDX,
+    REG_EBRACK_IDX,
+    REG_EPAREN_IDX,
+    REG_EBRACE_IDX,
+    REG_BADBR_IDX,
+    REG_ERANGE_IDX,
+    REG_ESPACE_IDX,
+    REG_BADRPT_IDX,
+    REG_EEND_IDX,
+    REG_ESIZE_IDX,
+    REG_ERPAREN_IDX
   };
 \f
 /* Avoiding alloca during matching, to placate r_alloc.  */
@@ -1083,7 +1237,7 @@ typedef struct
 # if defined MATCH_MAY_ALLOCATE
 /* 4400 was enough to cause a crash on Alpha OSF/1,
    whose default stack limit is 2mb.  */
-int re_max_failures = 20000;
+int re_max_failures = 4000;
 # else
 int re_max_failures = 2000;
 # endif
@@ -1478,7 +1632,8 @@ static boolean at_begline_loc_p _RE_ARGS ((const char *pattern, const char *p,
                                           reg_syntax_t syntax));
 static boolean at_endline_loc_p _RE_ARGS ((const char *p, const char *pend,
                                           reg_syntax_t syntax));
-static reg_errcode_t compile_range _RE_ARGS ((const char **p_ptr,
+static reg_errcode_t compile_range _RE_ARGS ((unsigned int range_start,
+                                             const char **p_ptr,
                                              const char *pend,
                                              char *translate,
                                              reg_syntax_t syntax,
@@ -1595,29 +1750,51 @@ static reg_errcode_t compile_range _RE_ARGS ((const char **p_ptr,
    reset the pointers that pointed into the old block to point to the
    correct places in the new one.  If extending the buffer results in it
    being larger than MAX_BUF_SIZE, then flag memory exhausted.  */
+#if __BOUNDED_POINTERS__
+# define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
+# define MOVE_BUFFER_POINTER(P) \
+  (__ptrlow (P) += incr, SET_HIGH_BOUND (P), __ptrvalue (P) += incr)
+# define ELSE_EXTEND_BUFFER_HIGH_BOUND         \
+  else                                         \
+    {                                          \
+      SET_HIGH_BOUND (b);                      \
+      SET_HIGH_BOUND (begalt);                 \
+      if (fixup_alt_jump)                      \
+       SET_HIGH_BOUND (fixup_alt_jump);        \
+      if (laststart)                           \
+       SET_HIGH_BOUND (laststart);             \
+      if (pending_exact)                       \
+       SET_HIGH_BOUND (pending_exact);         \
+    }
+#else
+# define MOVE_BUFFER_POINTER(P) (P) += incr
+# define ELSE_EXTEND_BUFFER_HIGH_BOUND
+#endif
 #define EXTEND_BUFFER()                                                        \
-  do {                                                                         \
+  do {                                                                 \
     unsigned char *old_buffer = bufp->buffer;                          \
-    if (bufp->allocated == MAX_BUF_SIZE)                               \
+    if (bufp->allocated == MAX_BUF_SIZE)                               \
       return REG_ESIZE;                                                        \
     bufp->allocated <<= 1;                                             \
     if (bufp->allocated > MAX_BUF_SIZE)                                        \
-      bufp->allocated = MAX_BUF_SIZE;                                  \
+      bufp->allocated = MAX_BUF_SIZE;                                  \
     bufp->buffer = (unsigned char *) REALLOC (bufp->buffer, bufp->allocated);\
     if (bufp->buffer == NULL)                                          \
       return REG_ESPACE;                                               \
     /* If the buffer moved, move all the pointers into it.  */         \
     if (old_buffer != bufp->buffer)                                    \
       {                                                                        \
-        b = (b - old_buffer) + bufp->buffer;                           \
-        begalt = (begalt - old_buffer) + bufp->buffer;                 \
-        if (fixup_alt_jump)                                            \
-          fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
-        if (laststart)                                                 \
-          laststart = (laststart - old_buffer) + bufp->buffer;         \
-        if (pending_exact)                                             \
-          pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
+       int incr = bufp->buffer - old_buffer;                           \
+       MOVE_BUFFER_POINTER (b);                                        \
+       MOVE_BUFFER_POINTER (begalt);                                   \
+       if (fixup_alt_jump)                                             \
+         MOVE_BUFFER_POINTER (fixup_alt_jump);                         \
+       if (laststart)                                                  \
+         MOVE_BUFFER_POINTER (laststart);                              \
+       if (pending_exact)                                              \
+         MOVE_BUFFER_POINTER (pending_exact);                          \
       }                                                                        \
+    ELSE_EXTEND_BUFFER_HIGH_BOUND                                      \
   } while (0)
 
 
@@ -1676,7 +1853,7 @@ typedef struct
   { if (p != pend)                                                     \
      {                                                                 \
        PATFETCH (c);                                                   \
-       while (ISDIGIT (c))                                             \
+       while ('0' <= c && c <= '9')                                    \
          {                                                             \
            if (num < 0)                                                        \
               num = 0;                                                 \
@@ -1688,7 +1865,7 @@ typedef struct
        }                                                               \
     }
 
-#if WIDE_CHAR_SUPPORT
+#if defined _LIBC || WIDE_CHAR_SUPPORT
 /* The GNU C library provides support for user-defined character classes
    and the functions from ISO C amendement 1.  */
 # ifdef CHARCLASS_NAME_MAX
@@ -1699,7 +1876,11 @@ typedef struct
 #  define CHAR_CLASS_MAX_LENGTH 256
 # endif
 
-# define IS_CHAR_CLASS(string) wctype (string)
+# ifdef _LIBC
+#  define IS_CHAR_CLASS(string) __wctype (string)
+# else
+#  define IS_CHAR_CLASS(string) wctype (string)
+# endif
 #else
 # define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
 
@@ -2078,6 +2259,7 @@ regex_compile (pattern, size, syntax, bufp)
         case '[':
           {
             boolean had_char_class = false;
+           unsigned int range_start = 0xffffffff;
 
             if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
 
@@ -2121,6 +2303,7 @@ regex_compile (pattern, size, syntax, bufp)
 
                     PATFETCH (c1);
                     SET_LIST_BIT (c1);
+                   range_start = c1;
                     continue;
                   }
 
@@ -2145,8 +2328,10 @@ regex_compile (pattern, size, syntax, bufp)
                     && *p != ']')
                   {
                     reg_errcode_t ret
-                      = compile_range (&p, pend, translate, syntax, b);
+                      = compile_range (range_start, &p, pend, translate,
+                                      syntax, b);
                     if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
+                   range_start = 0xffffffff;
                   }
 
                 else if (p[0] == '-' && p[1] != ']')
@@ -2156,8 +2341,9 @@ regex_compile (pattern, size, syntax, bufp)
                    /* Move past the `-'.  */
                     PATFETCH (c1);
 
-                    ret = compile_range (&p, pend, translate, syntax, b);
+                    ret = compile_range (c, &p, pend, translate, syntax, b);
                     if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
+                   range_start = 0xffffffff;
                   }
 
                 /* See if we're at the beginning of a possible character
@@ -2176,25 +2362,28 @@ regex_compile (pattern, size, syntax, bufp)
                     for (;;)
                       {
                         PATFETCH (c);
-                        if (c == ':' || c == ']' || p == pend
-                            || c1 == CHAR_CLASS_MAX_LENGTH)
+                        if ((c == ':' && *p == ']') || p == pend)
                           break;
-                        str[c1++] = c;
+                       if (c1 < CHAR_CLASS_MAX_LENGTH)
+                         str[c1++] = c;
+                       else
+                         /* This is in any case an invalid class name.  */
+                         str[0] = '\0';
                       }
                     str[c1] = '\0';
 
-                    /* If isn't a word bracketed by `[:' and:`]':
+                    /* If isn't a word bracketed by `[:' and `:]':
                        undo the ending character, the letters, and leave
                        the leading `:' and `[' (but set bits for them).  */
                     if (c == ':' && *p == ']')
                       {
-#if WIDE_CHAR_SUPPORT
+#if defined _LIBC || WIDE_CHAR_SUPPORT
                         boolean is_lower = STREQ (str, "lower");
                         boolean is_upper = STREQ (str, "upper");
                        wctype_t wt;
                         int ch;
 
-                       wt = wctype (str);
+                       wt = IS_CHAR_CLASS (str);
                        if (wt == 0)
                          FREE_STACK_RETURN (REG_ECTYPE);
 
@@ -2206,8 +2395,13 @@ regex_compile (pattern, size, syntax, bufp)
 
                         for (ch = 0; ch < 1 << BYTEWIDTH; ++ch)
                          {
+# ifdef _LIBC
+                           if (__iswctype (__btowc (ch), wt))
+                             SET_LIST_BIT (ch);
+# else
                            if (iswctype (btowc (ch), wt))
                              SET_LIST_BIT (ch);
+# endif
 
                            if (translate && (is_upper || is_lower)
                                && (ISUPPER (ch) || ISLOWER (ch)))
@@ -2272,13 +2466,292 @@ regex_compile (pattern, size, syntax, bufp)
                           PATUNFETCH;
                         SET_LIST_BIT ('[');
                         SET_LIST_BIT (':');
+                       range_start = ':';
                         had_char_class = false;
                       }
                   }
+                else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == '=')
+                 {
+                   unsigned char str[MB_LEN_MAX + 1];
+#ifdef _LIBC
+                   uint32_t nrules =
+                     _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
+#endif
+
+                   PATFETCH (c);
+                   c1 = 0;
+
+                   /* If pattern is `[[='.  */
+                   if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
+
+                   for (;;)
+                     {
+                       PATFETCH (c);
+                       if ((c == '=' && *p == ']') || p == pend)
+                         break;
+                       if (c1 < MB_LEN_MAX)
+                         str[c1++] = c;
+                       else
+                         /* This is in any case an invalid class name.  */
+                         str[0] = '\0';
+                      }
+                   str[c1] = '\0';
+
+                   if (c == '=' && *p == ']' && str[0] != '\0')
+                     {
+                       /* If we have no collation data we use the default
+                          collation in which each character is in a class
+                          by itself.  It also means that ASCII is the
+                          character set and therefore we cannot have character
+                          with more than one byte in the multibyte
+                          representation.  */
+#ifdef _LIBC
+                       if (nrules == 0)
+#endif
+                         {
+                           if (c1 != 1)
+                             FREE_STACK_RETURN (REG_ECOLLATE);
+
+                           /* Throw away the ] at the end of the equivalence
+                              class.  */
+                           PATFETCH (c);
+
+                           /* Set the bit for the character.  */
+                           SET_LIST_BIT (str[0]);
+                         }
+#ifdef _LIBC
+                       else
+                         {
+                           /* Try to match the byte sequence in `str' against
+                              those known to the collate implementation.
+                              First find out whether the bytes in `str' are
+                              actually from exactly one character.  */
+                           const int32_t *table;
+                           const unsigned char *weights;
+                           const unsigned char *extra;
+                           const int32_t *indirect;
+                           int32_t idx;
+                           const unsigned char *cp = str;
+                           int ch;
+
+                           /* This #include defines a local function!  */
+# include <locale/weight.h>
+
+                           table = (const int32_t *)
+                             _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
+                           weights = (const unsigned char *)
+                             _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
+                           extra = (const unsigned char *)
+                             _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
+                           indirect = (const int32_t *)
+                             _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
+
+                           idx = findidx (&cp);
+                           if (idx == 0 || cp < str + c1)
+                             /* This is no valid character.  */
+                             FREE_STACK_RETURN (REG_ECOLLATE);
+
+                           /* Throw away the ] at the end of the equivalence
+                              class.  */
+                           PATFETCH (c);
+
+                           /* Now we have to go throught the whole table
+                              and find all characters which have the same
+                              first level weight.
+
+                              XXX Note that this is not entirely correct.
+                              we would have to match multibyte sequences
+                              but this is not possible with the current
+                              implementation.  */
+                           for (ch = 1; ch < 256; ++ch)
+                             /* XXX This test would have to be changed if we
+                                would allow matching multibyte sequences.  */
+                             if (table[ch] > 0)
+                               {
+                                 int32_t idx2 = table[ch];
+                                 size_t len = weights[idx2];
+
+                                 /* Test whether the lenghts match.  */
+                                 if (weights[idx] == len)
+                                   {
+                                     /* They do.  New compare the bytes of
+                                        the weight.  */
+                                     size_t cnt = 0;
+
+                                     while (cnt < len
+                                            && (weights[idx + 1 + cnt]
+                                                == weights[idx2 + 1 + cnt]))
+                                       ++len;
+
+                                     if (cnt == len)
+                                       /* They match.  Mark the character as
+                                          acceptable.  */
+                                       SET_LIST_BIT (ch);
+                                   }
+                               }
+                         }
+#endif
+                       had_char_class = true;
+                     }
+                    else
+                      {
+                        c1++;
+                        while (c1--)
+                          PATUNFETCH;
+                        SET_LIST_BIT ('[');
+                        SET_LIST_BIT ('=');
+                       range_start = '=';
+                        had_char_class = false;
+                      }
+                 }
+                else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == '.')
+                 {
+                   unsigned char str[128];     /* Should be large enough.  */
+#ifdef _LIBC
+                   uint32_t nrules =
+                     _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
+#endif
+
+                   PATFETCH (c);
+                   c1 = 0;
+
+                   /* If pattern is `[[='.  */
+                   if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
+
+                   for (;;)
+                     {
+                       PATFETCH (c);
+                       if ((c == '.' && *p == ']') || p == pend)
+                         break;
+                       if (c1 < sizeof (str))
+                         str[c1++] = c;
+                       else
+                         /* This is in any case an invalid class name.  */
+                         str[0] = '\0';
+                      }
+                   str[c1] = '\0';
+
+                   if (c == '.' && *p == ']' && str[0] != '\0')
+                     {
+                       /* If we have no collation data we use the default
+                          collation in which each character is the name
+                          for its own class which contains only the one
+                          character.  It also means that ASCII is the
+                          character set and therefore we cannot have character
+                          with more than one byte in the multibyte
+                          representation.  */
+#ifdef _LIBC
+                       if (nrules == 0)
+#endif
+                         {
+                           if (c1 != 1)
+                             FREE_STACK_RETURN (REG_ECOLLATE);
+
+                           /* Throw away the ] at the end of the equivalence
+                              class.  */
+                           PATFETCH (c);
+
+                           /* Set the bit for the character.  */
+                           SET_LIST_BIT (str[0]);
+                           range_start = ((const unsigned char *) str)[0];
+                         }
+#ifdef _LIBC
+                       else
+                         {
+                           /* Try to match the byte sequence in `str' against
+                              those known to the collate implementation.
+                              First find out whether the bytes in `str' are
+                              actually from exactly one character.  */
+                           int32_t table_size;
+                           const int32_t *symb_table;
+                           const unsigned char *extra;
+                           int32_t idx;
+                           int32_t elem;
+                           int32_t second;
+                           int32_t hash;
+
+                           table_size =
+                             _NL_CURRENT_WORD (LC_COLLATE,
+                                               _NL_COLLATE_SYMB_HASH_SIZEMB);
+                           symb_table = (const int32_t *)
+                             _NL_CURRENT (LC_COLLATE,
+                                          _NL_COLLATE_SYMB_TABLEMB);
+                           extra = (const unsigned char *)
+                             _NL_CURRENT (LC_COLLATE,
+                                          _NL_COLLATE_SYMB_EXTRAMB);
+
+                           /* Locate the character in the hashing table.  */
+                           hash = elem_hash (str, c1);
+
+                           idx = 0;
+                           elem = hash % table_size;
+                           second = hash % (table_size - 2);
+                           while (symb_table[2 * elem] != 0)
+                             {
+                               /* First compare the hashing value.  */
+                               if (symb_table[2 * elem] == hash
+                                   && c1 == extra[symb_table[2 * elem + 1]]
+                                   && memcmp (str,
+                                              &extra[symb_table[2 * elem + 1]
+                                                    + 1],
+                                              c1) == 0)
+                                 {
+                                   /* Yep, this is the entry.  */
+                                   idx = symb_table[2 * elem + 1];
+                                   idx += 1 + extra[idx];
+                                   break;
+                                 }
+
+                               /* Next entry.  */
+                               elem += second;
+                             }
+
+                           if (symb_table[2 * elem] == 0)
+                             /* This is no valid character.  */
+                             FREE_STACK_RETURN (REG_ECOLLATE);
+
+                           /* Throw away the ] at the end of the equivalence
+                              class.  */
+                           PATFETCH (c);
+
+                           /* Now add the multibyte character(s) we found
+                              to the accept list.
+
+                              XXX Note that this is not entirely correct.
+                              we would have to match multibyte sequences
+                              but this is not possible with the current
+                              implementation.  Also, we have to match
+                              collating symbols, which expand to more than
+                              one file, as a whole and not allow the
+                              individual bytes.  */
+                           c1 = extra[idx++];
+                           if (c1 == 1)
+                             range_start = extra[idx];
+                           while (c1-- > 0)
+                             {
+                               SET_LIST_BIT (extra[idx]);
+                               ++idx;
+                             }
+                         }
+#endif
+                       had_char_class = false;
+                     }
+                    else
+                      {
+                        c1++;
+                        while (c1--)
+                          PATUNFETCH;
+                        SET_LIST_BIT ('[');
+                        SET_LIST_BIT ('.');
+                       range_start = '.';
+                        had_char_class = false;
+                      }
+                 }
                 else
                   {
                     had_char_class = false;
                     SET_LIST_BIT (c);
+                   range_start = c;
                   }
               }
 
@@ -2505,8 +2978,7 @@ regex_compile (pattern, size, syntax, bufp)
               if (!(syntax & RE_INTERVALS)
                      /* If we're at `\{' and it's not the open-interval
                         operator.  */
-                  || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
-                  || (p - 2 == pattern  &&  p == pend))
+                 || (syntax & RE_NO_BK_BRACES))
                 goto normal_backslash;
 
             handle_interval:
@@ -2520,7 +2992,7 @@ regex_compile (pattern, size, syntax, bufp)
 
                 if (p == pend)
                   {
-                    if (syntax & RE_NO_BK_BRACES)
+                    if (!(syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
                       goto unfetch_interval;
                     else
                       FREE_STACK_RETURN (REG_EBRACE);
@@ -2531,7 +3003,12 @@ regex_compile (pattern, size, syntax, bufp)
                 if (c == ',')
                   {
                     GET_UNSIGNED_NUMBER (upper_bound);
-                    if (upper_bound < 0) upper_bound = RE_DUP_MAX;
+                   if ((!(syntax & RE_NO_BK_BRACES) && c != '\\')
+                       || ((syntax & RE_NO_BK_BRACES) && c != '}'))
+                     FREE_STACK_RETURN (REG_BADBR);
+
+                   if (upper_bound < 0)
+                     upper_bound = RE_DUP_MAX;
                   }
                 else
                   /* Interval such as `{1}' => match exactly once. */
@@ -2540,7 +3017,7 @@ regex_compile (pattern, size, syntax, bufp)
                 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
                     || lower_bound > upper_bound)
                   {
-                    if (syntax & RE_NO_BK_BRACES)
+                    if (!(syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
                       goto unfetch_interval;
                     else
                       FREE_STACK_RETURN (REG_BADBR);
@@ -2555,7 +3032,7 @@ regex_compile (pattern, size, syntax, bufp)
 
                 if (c != '}')
                   {
-                    if (syntax & RE_NO_BK_BRACES)
+                    if (!(syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
                       goto unfetch_interval;
                     else
                       FREE_STACK_RETURN (REG_BADBR);
@@ -2691,7 +3168,7 @@ regex_compile (pattern, size, syntax, bufp)
 
 
             case 'w':
-             if (re_syntax_options & RE_NO_GNU_OPS)
+             if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
               laststart = b;
               BUF_PUSH (wordchar);
@@ -2699,7 +3176,7 @@ regex_compile (pattern, size, syntax, bufp)
 
 
             case 'W':
-             if (re_syntax_options & RE_NO_GNU_OPS)
+             if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
               laststart = b;
               BUF_PUSH (notwordchar);
@@ -2707,37 +3184,37 @@ regex_compile (pattern, size, syntax, bufp)
 
 
             case '<':
-             if (re_syntax_options & RE_NO_GNU_OPS)
+             if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
               BUF_PUSH (wordbeg);
               break;
 
             case '>':
-             if (re_syntax_options & RE_NO_GNU_OPS)
+             if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
               BUF_PUSH (wordend);
               break;
 
             case 'b':
-             if (re_syntax_options & RE_NO_GNU_OPS)
+             if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
               BUF_PUSH (wordbound);
               break;
 
             case 'B':
-             if (re_syntax_options & RE_NO_GNU_OPS)
+             if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
               BUF_PUSH (notwordbound);
               break;
 
             case '`':
-             if (re_syntax_options & RE_NO_GNU_OPS)
+             if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
               BUF_PUSH (begbuf);
               break;
 
             case '\'':
-             if (re_syntax_options & RE_NO_GNU_OPS)
+             if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
               BUF_PUSH (endbuf);
               break;
@@ -3028,49 +3505,64 @@ group_in_compile_stack (compile_stack, regnum)
    `regex_compile' itself.  */
 
 static reg_errcode_t
-compile_range (p_ptr, pend, translate, syntax, b)
-    const char **p_ptr, *pend;
-    RE_TRANSLATE_TYPE translate;
-    reg_syntax_t syntax;
-    unsigned char *b;
+compile_range (range_start_char, p_ptr, pend, translate, syntax, b)
+     unsigned int range_start_char;
+     const char **p_ptr, *pend;
+     RE_TRANSLATE_TYPE translate;
+     reg_syntax_t syntax;
+     unsigned char *b;
 {
   unsigned this_char;
-
   const char *p = *p_ptr;
-  unsigned int range_start, range_end;
+  reg_errcode_t ret;
+#if _LIBC
+  const unsigned char *collseq;
+  unsigned int start_colseq;
+  unsigned int end_colseq;
+#else
+  unsigned end_char;
+#endif
 
   if (p == pend)
     return REG_ERANGE;
 
-  /* Even though the pattern is a signed `char *', we need to fetch
-     with unsigned char *'s; if the high bit of the pattern character
-     is set, the range endpoints will be negative if we fetch using a
-     signed char *.
-
-     We also want to fetch the endpoints without translating them; the
-     appropriate translation is done in the bit-setting loop below.  */
-  /* The SVR4 compiler on the 3B2 had trouble with unsigned const char *.  */
-  range_start = ((const unsigned char *) p)[-2];
-  range_end   = ((const unsigned char *) p)[0];
-
   /* Have to increment the pointer into the pattern string, so the
      caller isn't still at the ending character.  */
   (*p_ptr)++;
 
-  /* If the start is after the end, the range is empty.  */
-  if (range_start > range_end)
-    return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
+  /* Report an error if the range is empty and the syntax prohibits this.  */
+  ret = syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
 
+#if _LIBC
+  collseq = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
+                                                _NL_COLLATE_COLLSEQMB);
+
+  start_colseq = collseq[(unsigned char) TRANSLATE (range_start_char)];
+  end_colseq = collseq[(unsigned char) TRANSLATE (p[0])];
+  for (this_char = 0; this_char <= (unsigned char) -1; ++this_char)
+    {
+      unsigned int this_colseq = collseq[(unsigned char) TRANSLATE (this_char)];
+
+      if (start_colseq <= this_colseq && this_colseq <= end_colseq)
+       {
+         SET_LIST_BIT (TRANSLATE (this_char));
+         ret = REG_NOERROR;
+       }
+    }
+#else
   /* Here we see why `this_char' has to be larger than an `unsigned
-     char' -- the range is inclusive, so if `range_end' == 0xff
-     (assuming 8-bit characters), we would otherwise go into an infinite
-     loop, since all characters <= 0xff.  */
-  for (this_char = range_start; this_char <= range_end; this_char++)
+     char' -- we would otherwise go into an infinite loop, since all
+     characters <= 0xff.  */
+  range_start_char = TRANSLATE (range_start_char);
+  end_char = TRANSLATE (p[0]);
+  for (this_char = range_start_char; this_char <= end_char; ++this_char)
     {
       SET_LIST_BIT (TRANSLATE (this_char));
+      ret = REG_NOERROR;
     }
+#endif
 
-  return REG_NOERROR;
+  return ret;
 }
 \f
 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
@@ -3375,6 +3867,9 @@ re_compile_fastmap (bufp)
   RESET_FAIL_STACK ();
   return 0;
 } /* re_compile_fastmap */
+#ifdef _LIBC
+weak_alias (__re_compile_fastmap, re_compile_fastmap)
+#endif
 \f
 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
    ENDS.  Subsequent matches using PATTERN_BUFFER and REGS will use
@@ -3410,6 +3905,9 @@ re_set_registers (bufp, regs, num_regs, starts, ends)
       regs->start = regs->end = (regoff_t *) 0;
     }
 }
+#ifdef _LIBC
+weak_alias (__re_set_registers, re_set_registers)
+#endif
 \f
 /* Searching routines.  */
 
@@ -3426,6 +3924,9 @@ re_search (bufp, string, size, startpos, range, regs)
   return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
                      regs, size);
 }
+#ifdef _LIBC
+weak_alias (__re_search, re_search)
+#endif
 
 
 /* Using the compiled pattern in BUFP->buffer, first tries to match the
@@ -3479,7 +3980,11 @@ re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
 
   /* If the search isn't to be a backwards one, don't waste time in a
      search for a pattern that must be anchored.  */
-  if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
+  if (bufp->used > 0 && range > 0
+      && ((re_opcode_t) bufp->buffer[0] == begbuf
+         /* `begline' is like `begbuf' if it cannot match at newlines.  */
+         || ((re_opcode_t) bufp->buffer[0] == begline
+             && !bufp->newline_anchor)))
     {
       if (startpos > 0)
        return -1;
@@ -3582,6 +4087,9 @@ re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
     }
   return -1;
 } /* re_search_2 */
+#ifdef _LIBC
+weak_alias (__re_search_2, re_search_2)
+#endif
 \f
 /* This converts PTR, a pointer into one of the search strings `string1'
    and `string2' into an offset from the beginning of that string.  */
@@ -3683,6 +4191,9 @@ re_match (bufp, string, size, pos, regs)
 # endif
   return result;
 }
+# ifdef _LIBC
+weak_alias (__re_match, re_match)
+# endif
 #endif /* not emacs */
 
 static boolean group_match_null_string_p _RE_ARGS ((unsigned char **p,
@@ -3728,6 +4239,9 @@ re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
 #endif
   return result;
 }
+#ifdef _LIBC
+weak_alias (__re_match_2, re_match_2)
+#endif
 
 /* This is a separate function so that we can force an alloca cleanup
    afterwards.  */
@@ -3778,7 +4292,7 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
   fail_stack_type fail_stack;
 #endif
 #ifdef DEBUG
-  static unsigned failure_id = 0;
+  static unsigned failure_id;
   unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
 #endif
 
@@ -4709,26 +5223,15 @@ re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
              }
             else if ((re_opcode_t) *p2 == charset)
              {
-#ifdef DEBUG
-               register unsigned char c
-                  = *p2 == (unsigned char) endline ? '\n' : p2[2];
-#endif
-
-#if 0
-                if ((re_opcode_t) p1[3] == exactn
-                   && ! ((int) p2[1] * BYTEWIDTH > (int) p1[5]
-                         && (p2[2 + p1[5] / BYTEWIDTH]
-                             & (1 << (p1[5] % BYTEWIDTH)))))
-#else
+               /* We win if the first character of the loop is not part
+                   of the charset.  */
                 if ((re_opcode_t) p1[3] == exactn
-                   && ! ((int) p2[1] * BYTEWIDTH > (int) p1[4]
-                         && (p2[2 + p1[4] / BYTEWIDTH]
-                             & (1 << (p1[4] % BYTEWIDTH)))))
-#endif
-                  {
-                   p[-3] = (unsigned char) pop_failure_jump;
-                    DEBUG_PRINT3 ("  %c != %c => pop_failure_jump.\n",
-                                  c, p1[5]);
+                   && ! ((int) p2[1] * BYTEWIDTH > (int) p1[5]
+                         && (p2[2 + p1[5] / BYTEWIDTH]
+                             & (1 << (p1[5] % BYTEWIDTH)))))
+                 {
+                   p[-3] = (unsigned char) pop_failure_jump;
+                   DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
                   }
 
                else if ((re_opcode_t) p1[3] == charset_not)
@@ -5419,8 +5922,11 @@ re_compile_pattern (pattern, length, bufp)
 
   if (!ret)
     return NULL;
-  return gettext (re_error_msgid[(int) ret]);
+  return gettext (re_error_msgid + re_error_msgid_idx[(int) ret]);
 }
+#ifdef _LIBC
+weak_alias (__re_compile_pattern, re_compile_pattern)
+#endif
 \f
 /* Entry points compatible with 4.2 BSD regex library.  We don't define
    them unless specifically requested.  */
@@ -5453,12 +5959,14 @@ re_comp (s)
     {
       re_comp_buf.buffer = (unsigned char *) malloc (200);
       if (re_comp_buf.buffer == NULL)
-        return gettext (re_error_msgid[(int) REG_ESPACE]);
+        return (char *) gettext (re_error_msgid
+                                + re_error_msgid_idx[(int) REG_ESPACE]);
       re_comp_buf.allocated = 200;
 
       re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
       if (re_comp_buf.fastmap == NULL)
-       return gettext (re_error_msgid[(int) REG_ESPACE]);
+       return (char *) gettext (re_error_msgid
+                                + re_error_msgid_idx[(int) REG_ESPACE]);
     }
 
   /* Since `re_exec' always passes NULL for the `regs' argument, we
@@ -5473,7 +5981,7 @@ re_comp (s)
     return NULL;
 
   /* Yes, we're discarding `const' here if !HAVE_LIBINTL.  */
-  return (char *) gettext (re_error_msgid[(int) ret]);
+  return (char *) gettext (re_error_msgid + re_error_msgid_idx[(int) ret]);
 }
 
 
@@ -5506,7 +6014,8 @@ re_exec (s)
        REG_EXTENDED bit in CFLAGS is set; otherwise, to
        RE_SYNTAX_POSIX_BASIC;
      `newline_anchor' to REG_NEWLINE being set in CFLAGS;
-     `fastmap' and `fastmap_accurate' to zero;
+     `fastmap' to an allocated space for the fastmap;
+     `fastmap_accurate' to zero;
      `re_nsub' to the number of subexpressions in PATTERN.
 
    PATTERN is the address of the pattern string.
@@ -5545,11 +6054,8 @@ regcomp (preg, pattern, cflags)
   preg->allocated = 0;
   preg->used = 0;
 
-  /* Don't bother to use a fastmap when searching.  This simplifies the
-     REG_NEWLINE case: if we used a fastmap, we'd have to put all the
-     characters after newlines into the fastmap.  This way, we just try
-     every character.  */
-  preg->fastmap = 0;
+  /* Try to allocate space for the fastmap.  */
+  preg->fastmap = (char *) malloc (1 << BYTEWIDTH);
 
   if (cflags & REG_ICASE)
     {
@@ -5563,7 +6069,7 @@ regcomp (preg, pattern, cflags)
 
       /* Map uppercase characters to corresponding lowercase ones.  */
       for (i = 0; i < CHAR_SET_SIZE; i++)
-        preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
+        preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
     }
   else
     preg->translate = NULL;
@@ -5589,8 +6095,24 @@ regcomp (preg, pattern, cflags)
      unmatched close-group: both are REG_EPAREN.  */
   if (ret == REG_ERPAREN) ret = REG_EPAREN;
 
+  if (ret == REG_NOERROR && preg->fastmap)
+    {
+      /* Compute the fastmap now, since regexec cannot modify the pattern
+        buffer.  */
+      if (re_compile_fastmap (preg) == -2)
+       {
+         /* Some error occurred while computing the fastmap, just forget
+            about it.  */
+         free (preg->fastmap);
+         preg->fastmap = NULL;
+       }
+    }
+
   return (int) ret;
 }
+#ifdef _LIBC
+weak_alias (__regcomp, regcomp)
+#endif
 
 
 /* regexec searches for a given pattern, specified by PREG, in the
@@ -5634,10 +6156,10 @@ regexec (preg, string, nmatch, pmatch, eflags)
   if (want_reg_info)
     {
       regs.num_regs = nmatch;
-      regs.start = TALLOC (nmatch, regoff_t);
-      regs.end = TALLOC (nmatch, regoff_t);
-      if (regs.start == NULL || regs.end == NULL)
+      regs.start = TALLOC (nmatch * 2, regoff_t);
+      if (regs.start == NULL)
         return (int) REG_NOMATCH;
+      regs.end = regs.start + nmatch;
     }
 
   /* Perform the searching operation.  */
@@ -5661,12 +6183,14 @@ regexec (preg, string, nmatch, pmatch, eflags)
 
       /* If we needed the temporary register info, free the space now.  */
       free (regs.start);
-      free (regs.end);
     }
 
   /* We want zero return to mean success, unlike `re_search'.  */
   return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
 }
+#ifdef _LIBC
+weak_alias (__regexec, regexec)
+#endif
 
 
 /* Returns a message corresponding to an error code, ERRCODE, returned
@@ -5683,15 +6207,15 @@ regerror (errcode, preg, errbuf, errbuf_size)
   size_t msg_size;
 
   if (errcode < 0
-      || errcode >= (int) (sizeof (re_error_msgid)
-                          / sizeof (re_error_msgid[0])))
+      || errcode >= (int) (sizeof (re_error_msgid_idx)
+                          / sizeof (re_error_msgid_idx[0])))
     /* Only error codes returned by the rest of the code should be passed
        to this routine.  If we are given anything else, or if other regex
        code generates an invalid error code, then the program has a bug.
        Dump core so we can fix it.  */
     abort ();
 
-  msg = gettext (re_error_msgid[errcode]);
+  msg = gettext (re_error_msgid + re_error_msgid_idx[errcode]);
 
   msg_size = strlen (msg) + 1; /* Includes the null.  */
 
@@ -5712,6 +6236,9 @@ regerror (errcode, preg, errbuf, errbuf_size)
 
   return msg_size;
 }
+#ifdef _LIBC
+weak_alias (__regerror, regerror)
+#endif
 
 
 /* Free dynamically allocated space used by PREG.  */
@@ -5736,5 +6263,8 @@ regfree (preg)
     free (preg->translate);
   preg->translate = NULL;
 }
+#ifdef _LIBC
+weak_alias (__regfree, regfree)
+#endif
 
 #endif /* not emacs  */