remove sigio blocking
[bpt/emacs.git] / src / regex.c
index 051a4fd..ac71b79 100644 (file)
@@ -2,7 +2,7 @@
    0.12.  (Implements POSIX draft P1003.2/D11.2, except for some of the
    internationalization features.)
 
-   Copyright (C) 1993-2013 Free Software Foundation, Inc.
+   Copyright (C) 1993-2014 Free Software Foundation, Inc.
 
    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
 
 /* Ignore some GCC warnings for now.  This section should go away
    once the Emacs and Gnulib regex code is merged.  */
-#if (__GNUC__ == 4 && 5 <= __GNUC_MINOR__) || 4 < __GNUC__
+#if 4 < __GNUC__ + (5 <= __GNUC_MINOR__) || defined __clang__
 # pragma GCC diagnostic ignored "-Wstrict-overflow"
 # ifndef emacs
-#  pragma GCC diagnostic ignored "-Wunused-but-set-variable"
 #  pragma GCC diagnostic ignored "-Wunused-function"
 #  pragma GCC diagnostic ignored "-Wunused-macros"
 #  pragma GCC diagnostic ignored "-Wunused-result"
 # endif
 #endif
 
+#if 4 < __GNUC__ + (6 <= __GNUC_MINOR__) && ! defined __clang__
+# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
+#endif
+
 #include <config.h>
 
 #include <stddef.h>
 # include "character.h"
 # include "buffer.h"
 
-/* Make syntax table lookup grant data in gl_state.  */
-# define SYNTAX_ENTRY_VIA_PROPERTY
-
 # include "syntax.h"
 # include "category.h"
 
+/* Make syntax table lookup grant data in gl_state.  */
+# define SYNTAX(c) syntax_property (c, 1)
+
 # ifdef malloc
 #  undef malloc
 # endif
@@ -254,15 +257,10 @@ xrealloc (void *block, size_t size)
 enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
 
 /* Dummy macros for non-Emacs environments.  */
-# define CHAR_CHARSET(c) 0
-# define CHARSET_LEADING_CODE_BASE(c) 0
 # define MAX_MULTIBYTE_LENGTH 1
 # define RE_MULTIBYTE_P(x) 0
 # define RE_TARGET_MULTIBYTE_P(x) 0
 # define WORD_BOUNDARY_P(c1, c2) (0)
-# define CHAR_HEAD_P(p) (1)
-# define SINGLE_BYTE_CHAR_P(c) (1)
-# define SAME_CHARSET_P(c1, c2) (1)
 # define BYTES_BY_CHAR_HEAD(p) (1)
 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
 # define STRING_CHAR(p) (*(p))
@@ -276,8 +274,6 @@ enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
   (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
 # define GET_CHAR_AFTER(c, p, len)     \
   (c = *p, len = 1)
-# define MAKE_CHAR(charset, c1, c2) (c1)
-# define BYTE8_TO_CHAR(c) (c)
 # define CHAR_BYTE8_P(c) (0)
 # define CHAR_LEADING_CODE(c) (c)
 
@@ -465,7 +461,7 @@ init_syntax_once (void)
 
 /* Assumes a `char *destination' variable.  */
 # define REGEX_REALLOCATE(source, osize, nsize)                                \
-  (destination = (char *) alloca (nsize),                              \
+  (destination = alloca (nsize),                                       \
    memcpy (destination, source, osize))
 
 /* No need to do anything to free, after alloca.  */
@@ -528,8 +524,10 @@ init_syntax_once (void)
 /* Type of source-pattern and string chars.  */
 #ifdef _MSC_VER
 typedef unsigned char re_char;
+typedef const re_char const_re_char;
 #else
 typedef const unsigned char re_char;
+typedef re_char const_re_char;
 #endif
 
 typedef char boolean;
@@ -710,51 +708,28 @@ typedef enum
    at SOURCE.  */
 
 #define EXTRACT_NUMBER(destination, source)                            \
-  do {                                                                 \
-    (destination) = *(source) & 0377;                                  \
-    (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8;          \
-  } while (0)
+  ((destination) = extract_number (source))
 
-#ifdef DEBUG
-static void
-extract_number (int *dest, re_char *source)
+static int
+extract_number (re_char *source)
 {
-  int temp = SIGN_EXTEND_CHAR (*(source + 1));
-  *dest = *source & 0377;
-  *dest += temp << 8;
+  unsigned leading_byte = SIGN_EXTEND_CHAR (source[1]);
+  return (leading_byte << 8) + source[0];
 }
 
-# ifndef EXTRACT_MACROS /* To debug the macros.  */
-#  undef EXTRACT_NUMBER
-#  define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
-# endif /* not EXTRACT_MACROS */
-
-#endif /* DEBUG */
-
 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
    SOURCE must be an lvalue.  */
 
 #define EXTRACT_NUMBER_AND_INCR(destination, source)                   \
-  do {                                                                 \
-    EXTRACT_NUMBER (destination, source);                              \
-    (source) += 2;                                                     \
-  } while (0)
+  ((destination) = extract_number_and_incr (&source))
 
-#ifdef DEBUG
-static void
-extract_number_and_incr (int *destination, re_char **source)
+static int
+extract_number_and_incr (re_char **source)
 {
-  extract_number (destination, *source);
+  int num = extract_number (*source);
   *source += 2;
+  return num;
 }
-
-# ifndef EXTRACT_MACROS
-#  undef EXTRACT_NUMBER_AND_INCR
-#  define EXTRACT_NUMBER_AND_INCR(dest, src) \
-  extract_number_and_incr (&dest, &src)
-# endif /* not EXTRACT_MACROS */
-
-#endif /* DEBUG */
 \f
 /* Store a multibyte character in three contiguous bytes starting
    DESTINATION, and increment DESTINATION to the byte after where the
@@ -794,10 +769,12 @@ extract_number_and_incr (int *destination, re_char **source)
    and the 2 bytes of flags at the start of the range table.  */
 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
 
+#ifdef emacs
 /* Extract the bit flags that start a range table.  */
 #define CHARSET_RANGE_TABLE_BITS(p)            \
   ((p)[2 + CHARSET_BITMAP_SIZE (p)]            \
    + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
+#endif
 
 /* Return the address of end of RANGE_TABLE.  COUNT is number of
    ranges (which is a pair of (start, end)) in the RANGE_TABLE.  `* 2'
@@ -861,10 +838,8 @@ extract_number_and_incr (int *destination, re_char **source)
 static int debug = -100000;
 
 # define DEBUG_STATEMENT(e) e
-# define DEBUG_PRINT1(x) if (debug > 0) printf (x)
-# define DEBUG_PRINT2(x1, x2) if (debug > 0) printf (x1, x2)
-# define DEBUG_PRINT3(x1, x2, x3) if (debug > 0) printf (x1, x2, x3)
-# define DEBUG_PRINT4(x1, x2, x3, x4) if (debug > 0) printf (x1, x2, x3, x4)
+# define DEBUG_PRINT(...) if (debug > 0) printf (__VA_ARGS__)
+# define DEBUG_COMPILES_ARGUMENTS
 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)                         \
   if (debug > 0) print_partial_compiled_pattern (s, e)
 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)                        \
@@ -873,9 +848,8 @@ static int debug = -100000;
 
 /* Print the fastmap in human-readable form.  */
 
-void
-print_fastmap (fastmap)
-    char *fastmap;
+static void
+print_fastmap (char *fastmap)
 {
   unsigned was_a_range = 0;
   unsigned i = 0;
@@ -905,10 +879,8 @@ print_fastmap (fastmap)
 /* Print a compiled pattern string in human-readable form, starting at
    the START pointer into it and ending just before the pointer END.  */
 
-void
-print_partial_compiled_pattern (start, end)
-    re_char *start;
-    re_char *end;
+static void
+print_partial_compiled_pattern (re_char *start, re_char *end)
 {
   int mcnt, mcnt2;
   re_char *p = start;
@@ -923,7 +895,7 @@ print_partial_compiled_pattern (start, end)
   /* Loop over pattern commands.  */
   while (p < pend)
     {
-      fprintf (stderr, "%d:\t", p - start);
+      fprintf (stderr, "%td:\t", p - start);
 
       switch ((re_opcode_t) *p++)
        {
@@ -1027,51 +999,58 @@ print_partial_compiled_pattern (start, end)
          break;
 
        case on_failure_jump:
-         extract_number_and_incr (&mcnt, &p);
-         fprintf (stderr, "/on_failure_jump to %d", p + mcnt - start);
+         EXTRACT_NUMBER_AND_INCR (mcnt, p);
+         fprintf (stderr, "/on_failure_jump to %td", p + mcnt - start);
          break;
 
        case on_failure_keep_string_jump:
-         extract_number_and_incr (&mcnt, &p);
-         fprintf (stderr, "/on_failure_keep_string_jump to %d", p + mcnt - start);
+         EXTRACT_NUMBER_AND_INCR (mcnt, p);
+         fprintf (stderr, "/on_failure_keep_string_jump to %td",
+                  p + mcnt - start);
          break;
 
        case on_failure_jump_nastyloop:
-         extract_number_and_incr (&mcnt, &p);
-         fprintf (stderr, "/on_failure_jump_nastyloop to %d", p + mcnt - start);
+         EXTRACT_NUMBER_AND_INCR (mcnt, p);
+         fprintf (stderr, "/on_failure_jump_nastyloop to %td",
+                  p + mcnt - start);
          break;
 
        case on_failure_jump_loop:
-         extract_number_and_incr (&mcnt, &p);
-         fprintf (stderr, "/on_failure_jump_loop to %d", p + mcnt - start);
+         EXTRACT_NUMBER_AND_INCR (mcnt, p);
+         fprintf (stderr, "/on_failure_jump_loop to %td",
+                  p + mcnt - start);
          break;
 
        case on_failure_jump_smart:
-         extract_number_and_incr (&mcnt, &p);
-         fprintf (stderr, "/on_failure_jump_smart to %d", p + mcnt - start);
+         EXTRACT_NUMBER_AND_INCR (mcnt, p);
+         fprintf (stderr, "/on_failure_jump_smart to %td",
+                  p + mcnt - start);
          break;
 
        case jump:
-         extract_number_and_incr (&mcnt, &p);
-         fprintf (stderr, "/jump to %d", p + mcnt - start);
+         EXTRACT_NUMBER_AND_INCR (mcnt, p);
+         fprintf (stderr, "/jump to %td", p + mcnt - start);
          break;
 
        case succeed_n:
-         extract_number_and_incr (&mcnt, &p);
-         extract_number_and_incr (&mcnt2, &p);
-         fprintf (stderr, "/succeed_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
+         EXTRACT_NUMBER_AND_INCR (mcnt, p);
+         EXTRACT_NUMBER_AND_INCR (mcnt2, p);
+         fprintf (stderr, "/succeed_n to %td, %d times",
+                  p - 2 + mcnt - start, mcnt2);
          break;
 
        case jump_n:
-         extract_number_and_incr (&mcnt, &p);
-         extract_number_and_incr (&mcnt2, &p);
-         fprintf (stderr, "/jump_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
+         EXTRACT_NUMBER_AND_INCR (mcnt, p);
+         EXTRACT_NUMBER_AND_INCR (mcnt2, p);
+         fprintf (stderr, "/jump_n to %td, %d times",
+                  p - 2 + mcnt - start, mcnt2);
          break;
 
        case set_number_at:
-         extract_number_and_incr (&mcnt, &p);
-         extract_number_and_incr (&mcnt2, &p);
-         fprintf (stderr, "/set_number_at location %d to %d", p - 2 + mcnt - start, mcnt2);
+         EXTRACT_NUMBER_AND_INCR (mcnt, p);
+         EXTRACT_NUMBER_AND_INCR (mcnt2, p);
+         fprintf (stderr, "/set_number_at location %td to %d",
+                  p - 2 + mcnt - start, mcnt2);
          break;
 
        case wordbound:
@@ -1151,13 +1130,12 @@ print_partial_compiled_pattern (start, end)
       fprintf (stderr, "\n");
     }
 
-  fprintf (stderr, "%d:\tend of pattern.\n", p - start);
+  fprintf (stderr, "%td:\tend of pattern.\n", p - start);
 }
 
 
-void
-print_compiled_pattern (bufp)
-    struct re_pattern_buffer *bufp;
+static void
+print_compiled_pattern (struct re_pattern_buffer *bufp)
 {
   re_char *buffer = bufp->buffer;
 
@@ -1171,7 +1149,7 @@ print_compiled_pattern (bufp)
       print_fastmap (bufp->fastmap);
     }
 
-  printf ("re_nsub: %d\t", bufp->re_nsub);
+  printf ("re_nsub: %zu\t", bufp->re_nsub);
   printf ("regs_alloc: %d\t", bufp->regs_allocated);
   printf ("can_be_null: %d\t", bufp->can_be_null);
   printf ("no_sub: %d\t", bufp->no_sub);
@@ -1183,13 +1161,9 @@ print_compiled_pattern (bufp)
 }
 
 
-void
-print_double_string (where, string1, size1, string2, size2)
-    re_char *where;
-    re_char *string1;
-    re_char *string2;
-    ssize_t size1;
-    ssize_t size2;
+static void
+print_double_string (re_char *where, re_char *string1, ssize_t size1,
+                    re_char *string2, ssize_t size2)
 {
   ssize_t this_char;
 
@@ -1216,10 +1190,7 @@ print_double_string (where, string1, size1, string2, size2)
 # define assert(e)
 
 # define DEBUG_STATEMENT(e)
-# define DEBUG_PRINT1(x)
-# define DEBUG_PRINT2(x1, x2)
-# define DEBUG_PRINT3(x1, x2, x3)
-# define DEBUG_PRINT4(x1, x2, x3, x4)
+# define DEBUG_PRINT(...)
 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
 
@@ -1258,12 +1229,12 @@ re_set_syntax (reg_syntax_t syntax)
 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
 
 /* Regexp to use to replace spaces, or NULL meaning don't.  */
-static re_char *whitespace_regexp;
+static const_re_char *whitespace_regexp;
 
 void
 re_set_whitespace_regexp (const char *regexp)
 {
-  whitespace_regexp = (re_char *) regexp;
+  whitespace_regexp = (const_re_char *) regexp;
 }
 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
 \f
@@ -1469,20 +1440,21 @@ typedef struct
 while (REMAINING_AVAIL_SLOTS <= space) {                               \
   if (!GROW_FAIL_STACK (fail_stack))                                   \
     return -2;                                                         \
-  DEBUG_PRINT2 ("\n  Doubled stack; size now: %d\n", (fail_stack).size);\
-  DEBUG_PRINT2 ("       slots available: %d\n", REMAINING_AVAIL_SLOTS);\
+  DEBUG_PRINT ("\n  Doubled stack; size now: %zd\n", (fail_stack).size);\
+  DEBUG_PRINT ("        slots available: %zd\n", REMAINING_AVAIL_SLOTS);\
 }
 
 /* Push register NUM onto the stack.  */
 #define PUSH_FAILURE_REG(num)                                          \
 do {                                                                   \
   char *destination;                                                   \
+  long n = num;                                                                \
   ENSURE_FAIL_STACK(3);                                                        \
-  DEBUG_PRINT4 ("    Push reg %d (spanning %p -> %p)\n",               \
-               num, regstart[num], regend[num]);                       \
-  PUSH_FAILURE_POINTER (regstart[num]);                                        \
-  PUSH_FAILURE_POINTER (regend[num]);                                  \
-  PUSH_FAILURE_INT (num);                                              \
+  DEBUG_PRINT ("    Push reg %ld (spanning %p -> %p)\n",               \
+              n, regstart[n], regend[n]);                              \
+  PUSH_FAILURE_POINTER (regstart[n]);                                  \
+  PUSH_FAILURE_POINTER (regend[n]);                                    \
+  PUSH_FAILURE_INT (n);                                                        \
 } while (0)
 
 /* Change the counter's value to VAL, but make sure that it will
@@ -1493,7 +1465,7 @@ do {                                                                      \
   int c;                                                               \
   ENSURE_FAIL_STACK(3);                                                        \
   EXTRACT_NUMBER (c, ptr);                                             \
-  DEBUG_PRINT4 ("    Push number %p = %d -> %d\n", ptr, c, val);       \
+  DEBUG_PRINT ("    Push number %p = %d -> %d\n", ptr, c, val);                \
   PUSH_FAILURE_INT (c);                                                        \
   PUSH_FAILURE_POINTER (ptr);                                          \
   PUSH_FAILURE_INT (-1);                                               \
@@ -1511,14 +1483,14 @@ do {                                                                    \
       unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER ();    \
       pfreg = POP_FAILURE_INT ();                                      \
       STORE_NUMBER (ptr, pfreg);                                       \
-      DEBUG_PRINT3 ("     Pop counter %p = %d\n", ptr, pfreg);         \
+      DEBUG_PRINT ("     Pop counter %p = %ld\n", ptr, pfreg);         \
     }                                                                  \
   else                                                                 \
     {                                                                  \
       regend[pfreg] = POP_FAILURE_POINTER ();                          \
       regstart[pfreg] = POP_FAILURE_POINTER ();                                \
-      DEBUG_PRINT4 ("     Pop reg %d (spanning %p -> %p)\n",           \
-                   pfreg, regstart[pfreg], regend[pfreg]);             \
+      DEBUG_PRINT ("     Pop reg %ld (spanning %p -> %p)\n",           \
+                  pfreg, regstart[pfreg], regend[pfreg]);              \
     }                                                                  \
 } while (0)
 
@@ -1538,10 +1510,10 @@ do {                                                                    \
          cycle = 1;                                                    \
          break;                                                        \
        }                                                               \
-      DEBUG_PRINT2 ("  Other pattern: %p\n", FAILURE_PAT (failure));   \
+      DEBUG_PRINT ("  Other pattern: %p\n", FAILURE_PAT (failure));    \
       failure = NEXT_FAILURE_HANDLE(failure);                          \
     }                                                                  \
-  DEBUG_PRINT2 ("  Other string: %p\n", FAILURE_STR (failure));                \
+  DEBUG_PRINT ("  Other string: %p\n", FAILURE_STR (failure));         \
 } while (0)
 
 /* Push the information about the state we will need
@@ -1560,23 +1532,23 @@ do {                                                                    \
      of 0 + -1 isn't done as unsigned.  */                             \
                                                                        \
   DEBUG_STATEMENT (nfailure_points_pushed++);                          \
-  DEBUG_PRINT1 ("\nPUSH_FAILURE_POINT:\n");                            \
-  DEBUG_PRINT2 ("  Before push, next avail: %d\n", (fail_stack).avail);        \
-  DEBUG_PRINT2 ("                      size: %d\n", (fail_stack).size);\
+  DEBUG_PRINT ("\nPUSH_FAILURE_POINT:\n");                             \
+  DEBUG_PRINT ("  Before push, next avail: %zd\n", (fail_stack).avail);        \
+  DEBUG_PRINT ("                       size: %zd\n", (fail_stack).size);\
                                                                        \
   ENSURE_FAIL_STACK (NUM_NONREG_ITEMS);                                        \
                                                                        \
-  DEBUG_PRINT1 ("\n");                                                 \
+  DEBUG_PRINT ("\n");                                                  \
                                                                        \
-  DEBUG_PRINT2 ("  Push frame index: %d\n", fail_stack.frame);         \
+  DEBUG_PRINT ("  Push frame index: %zd\n", fail_stack.frame);         \
   PUSH_FAILURE_INT (fail_stack.frame);                                 \
                                                                        \
-  DEBUG_PRINT2 ("  Push string %p: `", string_place);                  \
+  DEBUG_PRINT ("  Push string %p: `", string_place);                   \
   DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
-  DEBUG_PRINT1 ("'\n");                                                        \
+  DEBUG_PRINT ("'\n");                                                 \
   PUSH_FAILURE_POINTER (string_place);                                 \
                                                                        \
-  DEBUG_PRINT2 ("  Push pattern %p: ", pattern);                       \
+  DEBUG_PRINT ("  Push pattern %p: ", pattern);                                \
   DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend);                  \
   PUSH_FAILURE_POINTER (pattern);                                      \
                                                                        \
@@ -1609,28 +1581,28 @@ do {                                                                    \
   assert (!FAIL_STACK_EMPTY ());                                       \
                                                                        \
   /* Remove failure points and point to how many regs pushed.  */      \
-  DEBUG_PRINT1 ("POP_FAILURE_POINT:\n");                               \
-  DEBUG_PRINT2 ("  Before pop, next avail: %d\n", fail_stack.avail);   \
-  DEBUG_PRINT2 ("                   size: %d\n", fail_stack.size);     \
+  DEBUG_PRINT ("POP_FAILURE_POINT:\n");                                        \
+  DEBUG_PRINT ("  Before pop, next avail: %zd\n", fail_stack.avail);   \
+  DEBUG_PRINT ("                    size: %zd\n", fail_stack.size);    \
                                                                        \
   /* Pop the saved registers.  */                                      \
   while (fail_stack.frame < fail_stack.avail)                          \
     POP_FAILURE_REG_OR_COUNT ();                                       \
                                                                        \
-  pat = POP_FAILURE_POINTER ();                                \
-  DEBUG_PRINT2 ("  Popping pattern %p: ", pat);                                \
+  pat = POP_FAILURE_POINTER ();                                                \
+  DEBUG_PRINT ("  Popping pattern %p: ", pat);                         \
   DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend);                      \
                                                                        \
   /* If the saved string location is NULL, it came from an             \
      on_failure_keep_string_jump opcode, and we want to throw away the \
      saved NULL, thus retaining our current position in the string.  */        \
   str = POP_FAILURE_POINTER ();                                                \
-  DEBUG_PRINT2 ("  Popping string %p: `", str);                                \
+  DEBUG_PRINT ("  Popping string %p: `", str);                         \
   DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2);     \
-  DEBUG_PRINT1 ("'\n");                                                        \
+  DEBUG_PRINT ("'\n");                                                 \
                                                                        \
   fail_stack.frame = POP_FAILURE_INT ();                               \
-  DEBUG_PRINT2 ("  Popping  frame index: %d\n", fail_stack.frame);     \
+  DEBUG_PRINT ("  Popping  frame index: %zd\n", fail_stack.frame);     \
                                                                        \
   assert (fail_stack.avail >= 0);                                      \
   assert (fail_stack.frame <= fail_stack.avail);                       \
@@ -1849,6 +1821,8 @@ struct range_table_work_area
   int bits;                    /* flag to record character classes */
 };
 
+#ifdef emacs
+
 /* Make sure that WORK_AREA can hold more N multibyte characters.
    This is used only in set_image_of_range and set_image_of_range_1.
    It expects WORK_AREA to be a pointer.
@@ -1867,15 +1841,6 @@ struct range_table_work_area
 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit)          \
   (work_area).bits |= (bit)
 
-/* Bits used to implement the multibyte-part of the various character classes
-   such as [:alnum:] in a charset's range table.  */
-#define BIT_WORD       0x1
-#define BIT_LOWER      0x2
-#define BIT_PUNCT      0x4
-#define BIT_SPACE      0x8
-#define BIT_UPPER      0x10
-#define BIT_MULTIBYTE  0x20
-
 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA.  */
 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end)   \
   do {                                                                 \
@@ -1884,6 +1849,8 @@ struct range_table_work_area
     (work_area).table[(work_area).used++] = (range_end);               \
   } while (0)
 
+#endif /* emacs */
+
 /* Free allocated memory for WORK_AREA.  */
 #define FREE_RANGE_TABLE_WORK_AREA(work_area)  \
   do {                                         \
@@ -1895,6 +1862,15 @@ struct range_table_work_area
 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
+
+/* Bits used to implement the multibyte-part of the various character classes
+   such as [:alnum:] in a charset's range table.  */
+#define BIT_WORD       0x1
+#define BIT_LOWER      0x2
+#define BIT_PUNCT      0x4
+#define BIT_SPACE      0x8
+#define BIT_UPPER      0x10
+#define BIT_MULTIBYTE  0x20
 \f
 
 /* Set the bit for character C in a list.  */
@@ -2009,7 +1985,7 @@ struct range_table_work_area
 #endif /* emacs */
 
 /* Get the next unsigned number in the uncompiled pattern.  */
-#define GET_UNSIGNED_NUMBER(num)                                       \
+#define GET_INTERVAL_COUNT(num)                                        \
   do {                                                                 \
     if (p == pend)                                                     \
       FREE_STACK_RETURN (REG_EBRACE);                                  \
@@ -2018,13 +1994,11 @@ struct range_table_work_area
        PATFETCH (c);                                                   \
        while ('0' <= c && c <= '9')                                    \
          {                                                             \
-           int prev;                                                   \
            if (num < 0)                                                \
              num = 0;                                                  \
-           prev = num;                                                 \
-           num = num * 10 + c - '0';                                   \
-           if (num / 10 != prev)                                       \
+           if (RE_DUP_MAX / 10 - (RE_DUP_MAX % 10 < c - '0') < num)    \
              FREE_STACK_RETURN (REG_BADBR);                            \
+           num = num * 10 + c - '0';                                   \
            if (p == pend)                                              \
              FREE_STACK_RETURN (REG_EBRACE);                           \
            PATFETCH (c);                                               \
@@ -2036,7 +2010,7 @@ struct range_table_work_area
 
 /* Map a string to the char class it names (if any).  */
 re_wctype_t
-re_wctype (const re_char *str)
+re_wctype (const_re_char *str)
 {
   const char *string = (const char *) str;
   if      (STREQ (string, "alnum"))    return RECC_ALNUM;
@@ -2430,7 +2404,8 @@ do {                                                                      \
   } while (0)
 
 static reg_errcode_t
-regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct re_pattern_buffer *bufp)
+regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
+              struct re_pattern_buffer *bufp)
 {
   /* We fetch characters from PATTERN here.  */
   register re_wchar_t c, c1;
@@ -2493,7 +2468,7 @@ regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct
 
 #ifdef DEBUG
   debug++;
-  DEBUG_PRINT1 ("\nCompiling pattern: ");
+  DEBUG_PRINT ("\nCompiling pattern: ");
   if (debug > 0)
     {
       unsigned debug_count;
@@ -2646,7 +2621,7 @@ regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct
            goto normal_char;
        handle_plus:
        case '*':
-         /* If there is no previous pattern... */
+         /* If there is no previous pattern...  */
          if (!laststart)
            {
              if (syntax & RE_CONTEXT_INVALID_OPS)
@@ -2754,7 +2729,7 @@ regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct
                  }
              }
            else                /* not greedy */
-             { /* I wish the greedy and non-greedy cases could be merged. */
+             { /* I wish the greedy and non-greedy cases could be merged.  */
 
                GET_BUFFER_SPACE (7); /* We might use less.  */
                if (many_times_ok)
@@ -3058,7 +3033,7 @@ regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct
 
                /* Allocate space for COUNT + RANGE_TABLE.  Needs two
                   bytes for flags, two for COUNT, and three bytes for
-                  each character. */
+                  each character.  */
                GET_BUFFER_SPACE (4 + used * 3);
 
                /* Indicate the existence of range table.  */
@@ -3329,16 +3304,16 @@ regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct
 
                beg_interval = p;
 
-               GET_UNSIGNED_NUMBER (lower_bound);
+               GET_INTERVAL_COUNT (lower_bound);
 
                if (c == ',')
-                 GET_UNSIGNED_NUMBER (upper_bound);
+                 GET_INTERVAL_COUNT (upper_bound);
                else
                  /* Interval such as `{1}' => match exactly once. */
                  upper_bound = lower_bound;
 
-               if (lower_bound < 0 || upper_bound > RE_DUP_MAX
-                   || (upper_bound >= 0 && lower_bound > upper_bound))
+               if (lower_bound < 0
+                   || (0 <= upper_bound && upper_bound < lower_bound))
                  FREE_STACK_RETURN (REG_BADBR);
 
                if (!(syntax & RE_NO_BK_BRACES))
@@ -3485,6 +3460,7 @@ regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct
            /* There is no way to specify the before_dot and after_dot
               operators.  rms says this is ok.  --karl  */
            case '=':
+             laststart = b;
              BUF_PUSH (at_dot);
              break;
 
@@ -3533,12 +3509,14 @@ regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct
            case '<':
              if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
+             laststart = b;
              BUF_PUSH (wordbeg);
              break;
 
            case '>':
              if (syntax & RE_NO_GNU_OPS)
                goto normal_char;
+             laststart = b;
              BUF_PUSH (wordend);
              break;
 
@@ -3697,7 +3675,7 @@ regex_compile (const re_char *pattern, size_t size, reg_syntax_t syntax, struct
   if (debug > 0)
     {
       re_compile_fastmap (bufp);
-      DEBUG_PRINT1 ("\nCompiled pattern: \n");
+      DEBUG_PRINT ("\nCompiled pattern: \n");
       print_compiled_pattern (bufp);
     }
   debug--;
@@ -3783,7 +3761,7 @@ insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2, unsigned cha
    least one character before the ^.  */
 
 static boolean
-at_begline_loc_p (const re_char *pattern, const re_char *p, reg_syntax_t syntax)
+at_begline_loc_p (const_re_char *pattern, const_re_char *p, reg_syntax_t syntax)
 {
   re_char *prev = p - 2;
   boolean odd_backslashes;
@@ -3824,7 +3802,7 @@ at_begline_loc_p (const re_char *pattern, const re_char *p, reg_syntax_t syntax)
    at least one character after the $, i.e., `P < PEND'.  */
 
 static boolean
-at_endline_loc_p (const re_char *p, const re_char *pend, reg_syntax_t syntax)
+at_endline_loc_p (const_re_char *p, const_re_char *pend, reg_syntax_t syntax)
 {
   re_char *next = p;
   boolean next_backslash = *next == '\\';
@@ -3868,7 +3846,8 @@ group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
    Return -1 if fastmap was not updated accurately.  */
 
 static int
-analyse_first (const re_char *p, const re_char *pend, char *fastmap, const int multibyte)
+analyse_first (const_re_char *p, const_re_char *pend, char *fastmap,
+              const int multibyte)
 {
   int j, k;
   boolean not;
@@ -4222,7 +4201,7 @@ re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs, uns
     {
       bufp->regs_allocated = REGS_UNALLOCATED;
       regs->num_regs = 0;
-      regs->start = regs->end = (regoff_t *) 0;
+      regs->start = regs->end = 0;
     }
 }
 WEAK_ALIAS (__re_set_registers, re_set_registers)
@@ -4523,8 +4502,8 @@ static int bcmp_translate (re_char *s1, re_char *s2,
    and `string2' into an offset from the beginning of that string.  */
 #define POINTER_TO_OFFSET(ptr)                 \
   (FIRST_STRING_P (ptr)                                \
-   ? ((regoff_t) ((ptr) - string1))            \
-   : ((regoff_t) ((ptr) - string2 + size1)))
+   ? (ptr) - string1                           \
+   : (ptr) - string2 + (ptrdiff_t) size1)
 
 /* Call before fetching a character with *d.  This switches over to
    string2 if necessary.
@@ -4612,7 +4591,7 @@ static int bcmp_translate (re_char *s1, re_char *s2,
 /* If the operation is a match against one or more chars,
    return a pointer to the next operation, else return NULL.  */
 static re_char *
-skip_one_char (const re_char *p)
+skip_one_char (const_re_char *p)
 {
   switch (*p++)
     {
@@ -4654,7 +4633,7 @@ skip_one_char (const re_char *p)
 
 /* Jump over non-matching operations.  */
 static re_char *
-skip_noops (const re_char *p, const re_char *pend)
+skip_noops (const_re_char *p, const_re_char *pend)
 {
   int mcnt;
   while (p < pend)
@@ -4681,7 +4660,8 @@ skip_noops (const re_char *p, const re_char *pend)
 
 /* Non-zero if "p1 matches something" implies "p2 fails".  */
 static int
-mutually_exclusive_p (struct re_pattern_buffer *bufp, const re_char *p1, const re_char *p2)
+mutually_exclusive_p (struct re_pattern_buffer *bufp, const_re_char *p1,
+                     const_re_char *p2)
 {
   re_opcode_t op2;
   const boolean multibyte = RE_MULTIBYTE_P (bufp);
@@ -4711,7 +4691,7 @@ mutually_exclusive_p (struct re_pattern_buffer *bufp, const re_char *p1, const r
       /* If we're at the end of the pattern, we can change.  */
       if (skip_one_char (p1))
        {
-         DEBUG_PRINT1 ("  End of pattern: fast loop.\n");
+         DEBUG_PRINT ("  End of pattern: fast loop.\n");
          return 1;
        }
       break;
@@ -4727,7 +4707,7 @@ mutually_exclusive_p (struct re_pattern_buffer *bufp, const re_char *p1, const r
          {
            if (c != RE_STRING_CHAR (p1 + 2, multibyte))
              {
-               DEBUG_PRINT3 ("  '%c' != '%c' => fast loop.\n", c, p1[2]);
+               DEBUG_PRINT ("  '%c' != '%c' => fast loop.\n", c, p1[2]);
                return 1;
              }
          }
@@ -4752,14 +4732,14 @@ mutually_exclusive_p (struct re_pattern_buffer *bufp, const re_char *p1, const r
               that we can't change to pop_failure_jump.  */
            if (!not)
              {
-               DEBUG_PRINT1 ("  No match => fast loop.\n");
+               DEBUG_PRINT ("   No match => fast loop.\n");
                return 1;
              }
          }
        else if ((re_opcode_t) *p1 == anychar
                 && c == '\n')
          {
-           DEBUG_PRINT1 ("   . != \\n => fast loop.\n");
+           DEBUG_PRINT ("   . != \\n => fast loop.\n");
            return 1;
          }
       }
@@ -4802,7 +4782,7 @@ mutually_exclusive_p (struct re_pattern_buffer *bufp, const re_char *p1, const r
              if (idx == p2[1]
                  || idx == CHARSET_BITMAP_SIZE (p1))
                {
-                 DEBUG_PRINT1 ("        No match => fast loop.\n");
+                 DEBUG_PRINT ("         No match => fast loop.\n");
                  return 1;
                }
            }
@@ -4819,7 +4799,7 @@ mutually_exclusive_p (struct re_pattern_buffer *bufp, const re_char *p1, const r
 
              if (idx == p2[1])
                {
-                 DEBUG_PRINT1 ("        No match => fast loop.\n");
+                 DEBUG_PRINT ("         No match => fast loop.\n");
                  return 1;
                }
              }
@@ -4940,12 +4920,12 @@ WEAK_ALIAS (__re_match_2, re_match_2)
 /* This is a separate function so that we can force an alloca cleanup
    afterwards.  */
 static regoff_t
-re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
-                    size_t size1, const re_char *string2, size_t size2,
+re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
+                    size_t size1, const_re_char *string2, size_t size2,
                     ssize_t pos, struct re_registers *regs, ssize_t stop)
 {
   /* General temporaries.  */
-  ssize_t mcnt;
+  int mcnt;
   size_t reg;
 
   /* Just past the end of the corresponding string.  */
@@ -4987,7 +4967,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global.  */
   fail_stack_type fail_stack;
 #endif
-#ifdef DEBUG
+#ifdef DEBUG_COMPILES_ARGUMENTS
   unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
 #endif
 
@@ -5032,12 +5012,12 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
      and need to test it, it's not garbage.  */
   re_char *match_end = NULL;
 
-#ifdef DEBUG
+#ifdef DEBUG_COMPILES_ARGUMENTS
   /* Counts the total number of registers pushed.  */
   unsigned num_regs_pushed = 0;
 #endif
 
-  DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
+  DEBUG_PRINT ("\n\nEntering re_match_2.\n");
 
   INIT_FAIL_STACK ();
 
@@ -5133,22 +5113,25 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
       dend = end_match_1;
     }
 
-  DEBUG_PRINT1 ("The compiled pattern is: ");
+  DEBUG_PRINT ("The compiled pattern is: ");
   DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
-  DEBUG_PRINT1 ("The string to match is: `");
+  DEBUG_PRINT ("The string to match is: `");
   DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
-  DEBUG_PRINT1 ("'\n");
+  DEBUG_PRINT ("'\n");
 
   /* This loops over pattern commands.  It exits by returning from the
      function if the match is complete, or it drops through if the match
      fails at this starting point in the input data.  */
   for (;;)
     {
-      DEBUG_PRINT2 ("\n%p: ", p);
+      DEBUG_PRINT ("\n%p: ", p);
 
       if (p == pend)
-       { /* End of pattern means we might have succeeded.  */
-         DEBUG_PRINT1 ("end of pattern ... ");
+       {
+         ptrdiff_t dcnt;
+
+         /* End of pattern means we might have succeeded.  */
+         DEBUG_PRINT ("end of pattern ... ");
 
          /* If we haven't matched the entire string, and we want the
             longest match, try backtracking.  */
@@ -5168,7 +5151,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
              else
                best_match_p = !FIRST_STRING_P (d);
 
-             DEBUG_PRINT1 ("backtracking.\n");
+             DEBUG_PRINT ("backtracking.\n");
 
              if (!FAIL_STACK_EMPTY ())
                { /* More failure points to try.  */
@@ -5179,7 +5162,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
                      best_regs_set = true;
                      match_end = d;
 
-                     DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
+                     DEBUG_PRINT ("\nSAVING match as best so far.\n");
 
                      for (reg = 1; reg < num_regs; reg++)
                        {
@@ -5201,7 +5184,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
                     For example, the pattern `x.*y.*z' against the
                     strings `x-' and `y-z-', if the two strings are
                     not consecutive in memory.  */
-                 DEBUG_PRINT1 ("Restoring best registers.\n");
+                 DEBUG_PRINT ("Restoring best registers.\n");
 
                  d = match_end;
                  dend = ((d >= string1 && d <= end1)
@@ -5216,7 +5199,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
            } /* d != end_match_2 */
 
        succeed_label:
-         DEBUG_PRINT1 ("Accepting match.\n");
+         DEBUG_PRINT ("Accepting match.\n");
 
          /* If caller wants register contents data back, do it.  */
          if (regs && !bufp->no_sub)
@@ -5276,10 +5259,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
                    regs->start[reg] = regs->end[reg] = -1;
                  else
                    {
-                     regs->start[reg]
-                       = (regoff_t) POINTER_TO_OFFSET (regstart[reg]);
-                     regs->end[reg]
-                       = (regoff_t) POINTER_TO_OFFSET (regend[reg]);
+                     regs->start[reg] = POINTER_TO_OFFSET (regstart[reg]);
+                     regs->end[reg] = POINTER_TO_OFFSET (regend[reg]);
                    }
                }
 
@@ -5292,17 +5273,17 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
                regs->start[reg] = regs->end[reg] = -1;
            } /* regs && !bufp->no_sub */
 
-         DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
-                       nfailure_points_pushed, nfailure_points_popped,
-                       nfailure_points_pushed - nfailure_points_popped);
-         DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
+         DEBUG_PRINT ("%u failure points pushed, %u popped (%u remain).\n",
+                      nfailure_points_pushed, nfailure_points_popped,
+                      nfailure_points_pushed - nfailure_points_popped);
+         DEBUG_PRINT ("%u registers pushed.\n", num_regs_pushed);
 
-         mcnt = POINTER_TO_OFFSET (d) - pos;
+         dcnt = POINTER_TO_OFFSET (d) - pos;
 
-         DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
+         DEBUG_PRINT ("Returning %td from re_match_2.\n", dcnt);
 
          FREE_VARIABLES ();
-         return mcnt;
+         return dcnt;
        }
 
       /* Otherwise match next pattern command.  */
@@ -5311,11 +5292,11 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        /* Ignore these.  Used to ignore the n of succeed_n's which
           currently have n == 0.  */
        case no_op:
-         DEBUG_PRINT1 ("EXECUTING no_op.\n");
+         DEBUG_PRINT ("EXECUTING no_op.\n");
          break;
 
        case succeed:
-         DEBUG_PRINT1 ("EXECUTING succeed.\n");
+         DEBUG_PRINT ("EXECUTING succeed.\n");
          goto succeed_label;
 
        /* Match the next n pattern characters exactly.  The following
@@ -5323,7 +5304,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
           are the characters to match.  */
        case exactn:
          mcnt = *p++;
-         DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
+         DEBUG_PRINT ("EXECUTING exactn %d.\n", mcnt);
 
          /* Remember the start point to rollback upon failure.  */
          dfail = d;
@@ -5429,7 +5410,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
            int buf_charlen;
            re_wchar_t buf_ch;
 
-           DEBUG_PRINT1 ("EXECUTING anychar.\n");
+           DEBUG_PRINT ("EXECUTING anychar.\n");
 
            PREFETCH ();
            buf_ch = RE_STRING_CHAR_AND_LENGTH (d, buf_charlen,
@@ -5442,7 +5423,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
                    && buf_ch == '\000'))
              goto fail;
 
-           DEBUG_PRINT2 ("  Matched `%d'.\n", *d);
+           DEBUG_PRINT ("  Matched `%d'.\n", *d);
            d += buf_charlen;
          }
          break;
@@ -5469,7 +5450,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
            /* Whether matching against a unibyte character.  */
            boolean unibyte_char = false;
 
-           DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
+           DEBUG_PRINT ("EXECUTING charset%s.\n", not ? "_not" : "");
 
            range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
 
@@ -5553,14 +5534,14 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
           matched within the group is recorded (in the internal
           registers data structure) under the register number.  */
        case start_memory:
-         DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p);
+         DEBUG_PRINT ("EXECUTING start_memory %d:\n", *p);
 
          /* In case we need to undo this operation (via backtracking).  */
-         PUSH_FAILURE_REG ((unsigned int)*p);
+         PUSH_FAILURE_REG (*p);
 
          regstart[*p] = d;
          regend[*p] = NULL;    /* probably unnecessary.  -sm  */
-         DEBUG_PRINT2 ("  regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
+         DEBUG_PRINT ("  regstart: %td\n", POINTER_TO_OFFSET (regstart[*p]));
 
          /* Move past the register number and inner group count.  */
          p += 1;
@@ -5570,7 +5551,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        /* The stop_memory opcode represents the end of a group.  Its
           argument is the same as start_memory's: the register number.  */
        case stop_memory:
-         DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p);
+         DEBUG_PRINT ("EXECUTING stop_memory %d:\n", *p);
 
          assert (!REG_UNSET (regstart[*p]));
          /* Strictly speaking, there should be code such as:
@@ -5588,7 +5569,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
             is *not* undone.  */
 
          regend[*p] = d;
-         DEBUG_PRINT2 ("      regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
+         DEBUG_PRINT ("      regend: %td\n", POINTER_TO_OFFSET (regend[*p]));
 
          /* Move past the register number and the inner group count.  */
          p += 1;
@@ -5601,7 +5582,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
          {
            register re_char *d2, *dend2;
            int regno = *p++;   /* Get which register to match against.  */
-           DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
+           DEBUG_PRINT ("EXECUTING duplicate %d.\n", regno);
 
            /* Can't back reference a group which we've never matched.  */
            if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
@@ -5623,6 +5604,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
                     ? regend[regno] : end_match_1);
            for (;;)
              {
+               ptrdiff_t dcnt;
+
                /* If necessary, advance to next segment in register
                   contents.  */
                while (d2 == dend2)
@@ -5641,23 +5624,23 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
                PREFETCH ();
 
                /* How many characters left in this segment to match.  */
-               mcnt = dend - d;
+               dcnt = dend - d;
 
                /* Want how many consecutive characters we can match in
                   one shot, so, if necessary, adjust the count.  */
-               if (mcnt > dend2 - d2)
-                 mcnt = dend2 - d2;
+               if (dcnt > dend2 - d2)
+                 dcnt = dend2 - d2;
 
                /* Compare that many; failure if mismatch, else move
                   past them.  */
                if (RE_TRANSLATE_P (translate)
-                   ? bcmp_translate (d, d2, mcnt, translate, target_multibyte)
-                   : memcmp (d, d2, mcnt))
+                   ? bcmp_translate (d, d2, dcnt, translate, target_multibyte)
+                   : memcmp (d, d2, dcnt))
                  {
                    d = dfail;
                    goto fail;
                  }
-               d += mcnt, d2 += mcnt;
+               d += dcnt, d2 += dcnt;
              }
          }
          break;
@@ -5666,7 +5649,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        /* begline matches the empty string at the beginning of the string
           (unless `not_bol' is set in `bufp'), and after newlines.  */
        case begline:
-         DEBUG_PRINT1 ("EXECUTING begline.\n");
+         DEBUG_PRINT ("EXECUTING begline.\n");
 
          if (AT_STRINGS_BEG (d))
            {
@@ -5685,7 +5668,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
 
        /* endline is the dual of begline.  */
        case endline:
-         DEBUG_PRINT1 ("EXECUTING endline.\n");
+         DEBUG_PRINT ("EXECUTING endline.\n");
 
          if (AT_STRINGS_END (d))
            {
@@ -5702,7 +5685,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
 
        /* Match at the very beginning of the data.  */
        case begbuf:
-         DEBUG_PRINT1 ("EXECUTING begbuf.\n");
+         DEBUG_PRINT ("EXECUTING begbuf.\n");
          if (AT_STRINGS_BEG (d))
            break;
          goto fail;
@@ -5710,7 +5693,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
 
        /* Match at the very end of the data.  */
        case endbuf:
-         DEBUG_PRINT1 ("EXECUTING endbuf.\n");
+         DEBUG_PRINT ("EXECUTING endbuf.\n");
          if (AT_STRINGS_END (d))
            break;
          goto fail;
@@ -5734,8 +5717,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
           case; that seems worse than this.  */
        case on_failure_keep_string_jump:
          EXTRACT_NUMBER_AND_INCR (mcnt, p);
-         DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
-                       mcnt, p + mcnt);
+         DEBUG_PRINT ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
+                      mcnt, p + mcnt);
 
          PUSH_FAILURE_POINT (p - 3, NULL);
          break;
@@ -5756,8 +5739,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
             the loop.  */
        case on_failure_jump_nastyloop:
          EXTRACT_NUMBER_AND_INCR (mcnt, p);
-         DEBUG_PRINT3 ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
-                       mcnt, p + mcnt);
+         DEBUG_PRINT ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
+                      mcnt, p + mcnt);
 
          assert ((re_opcode_t)p[-4] == no_op);
          {
@@ -5777,8 +5760,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        case on_failure_jump_loop:
        on_failure:
          EXTRACT_NUMBER_AND_INCR (mcnt, p);
-         DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
-                       mcnt, p + mcnt);
+         DEBUG_PRINT ("EXECUTING on_failure_jump_loop %d (to %p):\n",
+                      mcnt, p + mcnt);
          {
            int cycle = 0;
            CHECK_INFINITE_LOOP (p - 3, d);
@@ -5809,8 +5792,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
           pop_failure_jump back to this on_failure_jump.  */
        case on_failure_jump:
          EXTRACT_NUMBER_AND_INCR (mcnt, p);
-         DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
-                       mcnt, p + mcnt);
+         DEBUG_PRINT ("EXECUTING on_failure_jump %d (to %p):\n",
+                      mcnt, p + mcnt);
 
          PUSH_FAILURE_POINT (p -3, d);
          break;
@@ -5824,8 +5807,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
           on_failure_keep_string_jump instead of on_failure_jump.  */
        case on_failure_jump_smart:
          EXTRACT_NUMBER_AND_INCR (mcnt, p);
-         DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
-                       mcnt, p + mcnt);
+         DEBUG_PRINT ("EXECUTING on_failure_jump_smart %d (to %p).\n",
+                      mcnt, p + mcnt);
          {
            re_char *p1 = p; /* Next operation.  */
            /* Here, we discard `const', making re_match non-reentrant.  */
@@ -5845,14 +5828,14 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
            if (mutually_exclusive_p (bufp, p1, p2))
              {
                /* Use a fast `on_failure_keep_string_jump' loop.  */
-               DEBUG_PRINT1 ("  smart exclusive => fast loop.\n");
+               DEBUG_PRINT ("  smart exclusive => fast loop.\n");
                *p3 = (unsigned char) on_failure_keep_string_jump;
                STORE_NUMBER (p2 - 2, mcnt + 3);
              }
            else
              {
                /* Default to a safe `on_failure_jump' loop.  */
-               DEBUG_PRINT1 ("  smart default => slow loop.\n");
+               DEBUG_PRINT ("  smart default => slow loop.\n");
                *p3 = (unsigned char) on_failure_jump;
              }
            DEBUG_STATEMENT (debug -= 2);
@@ -5864,9 +5847,9 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        unconditional_jump:
          IMMEDIATE_QUIT_CHECK;
          EXTRACT_NUMBER_AND_INCR (mcnt, p);    /* Get the amount to jump.  */
-         DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
+         DEBUG_PRINT ("EXECUTING jump %d ", mcnt);
          p += mcnt;                            /* Do the jump.  */
-         DEBUG_PRINT2 ("(to %p).\n", p);
+         DEBUG_PRINT ("(to %p).\n", p);
          break;
 
 
@@ -5875,7 +5858,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        case succeed_n:
          /* Signedness doesn't matter since we only compare MCNT to 0.  */
          EXTRACT_NUMBER (mcnt, p + 2);
-         DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
+         DEBUG_PRINT ("EXECUTING succeed_n %d.\n", mcnt);
 
          /* Originally, mcnt is how many times we HAVE to succeed.  */
          if (mcnt != 0)
@@ -5894,7 +5877,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        case jump_n:
          /* Signedness doesn't matter since we only compare MCNT to 0.  */
          EXTRACT_NUMBER (mcnt, p + 2);
-         DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
+         DEBUG_PRINT ("EXECUTING jump_n %d.\n", mcnt);
 
          /* Originally, this is how many times we CAN jump.  */
          if (mcnt != 0)
@@ -5913,14 +5896,14 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        case set_number_at:
          {
            unsigned char *p2;  /* Location of the counter.  */
-           DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
+           DEBUG_PRINT ("EXECUTING set_number_at.\n");
 
            EXTRACT_NUMBER_AND_INCR (mcnt, p);
            /* Here, we discard `const', making re_match non-reentrant.  */
            p2 = (unsigned char*) p + mcnt;
-           /* Signedness doesn't matter since we only copy MCNT's bits .  */
+           /* Signedness doesn't matter since we only copy MCNT's bits.  */
            EXTRACT_NUMBER_AND_INCR (mcnt, p);
-           DEBUG_PRINT3 ("  Setting %p to %d.\n", p2, mcnt);
+           DEBUG_PRINT ("  Setting %p to %d.\n", p2, mcnt);
            PUSH_NUMBER (p2, mcnt);
            break;
          }
@@ -5929,7 +5912,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        case notwordbound:
          {
            boolean not = (re_opcode_t) *(p - 1) == notwordbound;
-           DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
+           DEBUG_PRINT ("EXECUTING %swordbound.\n", not ? "not" : "");
 
            /* We SUCCEED (or FAIL) in one of the following cases: */
 
@@ -5971,7 +5954,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
          }
 
        case wordbeg:
-         DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
+         DEBUG_PRINT ("EXECUTING wordbeg.\n");
 
          /* We FAIL in one of the following cases: */
 
@@ -6016,7 +5999,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
          break;
 
        case wordend:
-         DEBUG_PRINT1 ("EXECUTING wordend.\n");
+         DEBUG_PRINT ("EXECUTING wordend.\n");
 
          /* We FAIL in one of the following cases: */
 
@@ -6061,7 +6044,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
          break;
 
        case symbeg:
-         DEBUG_PRINT1 ("EXECUTING symbeg.\n");
+         DEBUG_PRINT ("EXECUTING symbeg.\n");
 
          /* We FAIL in one of the following cases: */
 
@@ -6104,7 +6087,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
          break;
 
        case symend:
-         DEBUG_PRINT1 ("EXECUTING symend.\n");
+         DEBUG_PRINT ("EXECUTING symend.\n");
 
          /* We FAIL in one of the following cases: */
 
@@ -6151,7 +6134,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
          {
            boolean not = (re_opcode_t) *(p - 1) == notsyntaxspec;
            mcnt = *p++;
-           DEBUG_PRINT3 ("EXECUTING %ssyntaxspec %d.\n", not?"not":"", mcnt);
+           DEBUG_PRINT ("EXECUTING %ssyntaxspec %d.\n", not ? "not" : "",
+                        mcnt);
            PREFETCH ();
 #ifdef emacs
            {
@@ -6174,19 +6158,19 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
 
 #ifdef emacs
        case before_dot:
-         DEBUG_PRINT1 ("EXECUTING before_dot.\n");
+         DEBUG_PRINT ("EXECUTING before_dot.\n");
          if (PTR_BYTE_POS (d) >= PT_BYTE)
            goto fail;
          break;
 
        case at_dot:
-         DEBUG_PRINT1 ("EXECUTING at_dot.\n");
+         DEBUG_PRINT ("EXECUTING at_dot.\n");
          if (PTR_BYTE_POS (d) != PT_BYTE)
            goto fail;
          break;
 
        case after_dot:
-         DEBUG_PRINT1 ("EXECUTING after_dot.\n");
+         DEBUG_PRINT ("EXECUTING after_dot.\n");
          if (PTR_BYTE_POS (d) <= PT_BYTE)
            goto fail;
          break;
@@ -6196,8 +6180,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
          {
            boolean not = (re_opcode_t) *(p - 1) == notcategoryspec;
            mcnt = *p++;
-           DEBUG_PRINT3 ("EXECUTING %scategoryspec %d.\n",
-                         not?"not":"", mcnt);
+           DEBUG_PRINT ("EXECUTING %scategoryspec %d.\n",
+                        not ? "not" : "", mcnt);
            PREFETCH ();
 
            {
@@ -6226,7 +6210,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
        {
          re_char *str, *pat;
          /* A restart point is known.  Restore to that state.  */
-         DEBUG_PRINT1 ("\nFAIL:\n");
+         DEBUG_PRINT ("\nFAIL:\n");
          POP_FAILURE_POINT (str, pat);
          switch (*pat++)
            {
@@ -6271,7 +6255,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
   FREE_VARIABLES ();
 
   return -1;                           /* Failure to match.  */
-} /* re_match_2 */
+}
 \f
 /* Subroutine definitions for re_match_2.  */
 
@@ -6279,7 +6263,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const re_char *string1,
    bytes; nonzero otherwise.  */
 
 static int
-bcmp_translate (const re_char *s1, const re_char *s2, register ssize_t len,
+bcmp_translate (const_re_char *s1, const_re_char *s2, register ssize_t len,
                RE_TRANSLATE_TYPE translate, const int target_multibyte)
 {
   register re_char *p1 = s1, *p2 = s2;
@@ -6404,8 +6388,7 @@ weak_function
 re_exec (const char *s)
 {
   const size_t len = strlen (s);
-  return
-    0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
+  return re_search (&re_comp_buf, s, len, 0, len, 0) >= 0;
 }
 #endif /* _REGEX_RE_COMP */
 \f
@@ -6448,7 +6431,7 @@ re_exec (const char *s)
    the return codes and their meanings.)  */
 
 reg_errcode_t
-regcomp (regex_t *__restrict preg, const char *__restrict pattern,
+regcomp (regex_t *_Restrict_ preg, const char *_Restrict_ pattern,
         int cflags)
 {
   reg_errcode_t ret;
@@ -6529,8 +6512,8 @@ WEAK_ALIAS (__regcomp, regcomp)
    We return 0 if we find a match and REG_NOMATCH if not.  */
 
 reg_errcode_t
-regexec (const regex_t *__restrict preg, const char *__restrict string,
-        size_t nmatch, regmatch_t pmatch[__restrict_arr], int eflags)
+regexec (const regex_t *_Restrict_ preg, const char *_Restrict_ string,
+        size_t nmatch, regmatch_t pmatch[_Restrict_arr_], int eflags)
 {
   regoff_t ret;
   struct re_registers regs;
@@ -6569,7 +6552,7 @@ regexec (const regex_t *__restrict preg, const char *__restrict string,
   /* Perform the searching operation.  */
   ret = re_search (&private_preg, string, len,
                   /* start: */ 0, /* range: */ len,
-                  want_reg_info ? &regs : (struct re_registers *) 0);
+                  want_reg_info ? &regs : 0);
 
   /* Copy the register information to the POSIX structure.  */
   if (want_reg_info)