Partially sync files.el from XEmacs 21.5 for wildcard support.
[sxemacs] / src / regex.c
index b5ced47..6f9fb9c 100644 (file)
@@ -403,7 +403,7 @@ void *alloca();
 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
    `string1' or just past its end.  This works if PTR is NULL, which is
    a good thing.  */
-#define FIRST_STRING_P(ptr)                                    \
+#define FIRST_STRING_P(ptr)                                    \
   (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
 
 /* (Re)Allocate N items of type T using malloc, or fail.  */
@@ -685,7 +685,7 @@ static int debug = 0;
 #define DEBUG_PRINT2(x1, x2)           printf(x1, x2)
 #define DEBUG_PRINT3(x1, x2, x3)       printf(x1, x2, x3)
 #define DEBUG_PRINT4(x1, x2, x3, x4)   printf(x1, x2, x3, x4)
-#define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)                          \
+#define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)                          \
        print_partial_compiled_pattern(s, e)
 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)                 \
        print_double_string (w, s1, sz1, s2, sz2)
@@ -1260,14 +1260,14 @@ typedef struct {
   ((int) (fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS       \
    ? 0                                                                 \
    : ((fail_stack).stack = (fail_stack_elt_t *)                                \
-        REGEX_REALLOCATE_STACK ((fail_stack).stack,                    \
-          (fail_stack).size * sizeof (fail_stack_elt_t),               \
-          ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)),       \
+       REGEX_REALLOCATE_STACK ((fail_stack).stack,                     \
+         (fail_stack).size * sizeof (fail_stack_elt_t),                \
+         ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)),        \
                                                                        \
       (fail_stack).stack == NULL                                       \
       ? 0                                                              \
-      : ((fail_stack).size <<= 1,                                      \
-         1)))
+      : ((fail_stack).size <<= 1,                                      \
+        1)))
 
 /* Push pointer POINTER on FAIL_STACK.
    Return 1 if was able to do so and 0 if ran out of memory allocating
@@ -1437,7 +1437,7 @@ typedef struct {
 
 /* We actually push this many items.  */
 #define NUM_FAILURE_ITEMS                                              \
-  ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS        \
+  ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS        \
     + NUM_NONREG_ITEMS)
 
 /* How many items can still be added to the stack without overflowing it.  */
@@ -1456,11 +1456,11 @@ typedef struct {
    `pend', `string1', `size1', `string2', and `size2'.  */
 
 #define POP_FAILURE_POINT(str, pat, low_reg, high_reg,                 \
-                          regstart, regend, reg_info)                  \
+                         regstart, regend, reg_info)                   \
        do {                                                            \
-               DEBUG_STATEMENT (fail_stack_elt_t ffailure_id;)         \
-                       int this_reg;                                   \
+               int this_reg;                                           \
                const unsigned char *string_temp;                       \
+               DEBUG_STATEMENT (fail_stack_elt_t ffailure_id);         \
                                                                        \
                assert (!FAIL_STACK_EMPTY ());                          \
                                                                        \
@@ -1482,7 +1482,8 @@ typedef struct {
                   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.  */    \
-               string_temp = POP_FAILURE_POINTER ();                   \
+               string_temp = (const unsigned char *)                   \
+                       POP_FAILURE_POINTER ();                         \
                if (string_temp != NULL)                                \
                        str = string_temp;                              \
                                                                        \
@@ -1594,7 +1595,7 @@ static unsigned char reg_unset_dummy;
 #define PATFETCH_RAW(c)                                                        \
   do {if (p == pend) return REG_EEND;                                  \
     assert (p < pend);                                                 \
-    c = charptr_emchar (p);                                            \
+    c = charptr_emchar (p);                                            \
     INC_CHARPTR (p);                                                   \
   } while (0)
 
@@ -1820,20 +1821,20 @@ typedef struct {
 #endif
 
 /* Get the next unsigned number in the uncompiled pattern.  */
-#define GET_UNSIGNED_NUMBER(num)                                       \
+#define GET_UNSIGNED_NUMBER(num)                                       \
   { if (p != pend)                                                     \
      {                                                                 \
-       PATFETCH (c);                                                   \
-       while (ISDIGIT (c))                                             \
-         {                                                             \
-           if (num < 0)                                                        \
-              num = 0;                                                 \
-           num = num * 10 + c - '0';                                   \
-           if (p == pend)                                              \
-              break;                                                   \
-           PATFETCH (c);                                               \
-         }                                                             \
-       }                                                               \
+       PATFETCH (c);                                                   \
+       while (ISDIGIT (c))                                             \
+                                                                     \
+          if (num < 0)                                                 \
+             num = 0;                                                  \
+          num = num * 10 + c - '0';                                    \
+          if (p == pend)                                               \
+             break;                                                    \
+          PATFETCH (c);                                                \
+                                                                     \
+       }                                                               \
     }
 
 #define CHAR_CLASS_MAX_LENGTH  9       /* Namely, `multibyte'.  */
@@ -1974,7 +1975,7 @@ re_wctype(re_char *str)
        } else if (STREQ (string, "multibyte")) {
                return RECC_MULTIBYTE;
        } else {
-               return 0;
+               return RECC_ERROR;
        }
 }
 
@@ -2039,7 +2040,7 @@ re_iswctype(int ch, re_wctype_t cc)
      `used' is set to the length of the compiled pattern;
      `fastmap_accurate' is zero;
      `re_ngroups' is the number of groups/subexpressions (including shy
-        groups) in PATTERN;
+       groups) in PATTERN;
      `re_nsub' is the number of non-shy groups in PATTERN;
      `not_bol' and `not_eol' are zero;
 
@@ -2101,14 +2102,9 @@ regex_compile(re_char * pattern, int size, reg_syntax_t syntax,
           matching close-group on the compile stack, so the same register
           number is put in the stop_memory as the start_memory.  */
        regnum_t regnum = 0;
-       auto inline void free_stack(void) __attribute__((always_inline));
 
        /* we need to close over compile_stack */
-       auto inline void free_stack(void)
-       {
-               xfree(compile_stack.stack);
-               return;
-       }
+#      define free_stack(args...)      xfree(compile_stack.stack)
 
 #ifdef REGEX_DEBUG_FLAG
        DEBUG_PRINT1("\nCompiling pattern: ");
@@ -2634,7 +2630,7 @@ regex_compile(re_char * pattern, int size, reg_syntax_t syntax,
                                   character class.  */
 
                                else if (syntax & RE_CHAR_CLASSES &&
-                                        c == '[' && *p == ':') {       
+                                        c == '[' && *p == ':') {
                                        /* Leave room for the null.  */
                                        char str[CHAR_CLASS_MAX_LENGTH + 1];
 
@@ -3376,7 +3372,7 @@ regex_compile(re_char * pattern, int size, reg_syntax_t syntax,
                                /* Convert external to internal as soon
                                   as possible. */
                                reg = bufp->external_to_internal_register[reg];
+
                                /* Can't back reference to a
                                   subexpression if inside it. */
                                if (group_in_compile_stack(
@@ -4554,12 +4550,12 @@ re_search_2(struct re_pattern_buffer *bufp, const char *str1,
 /* Call before fetching a character with *d.  This switches over to
    string2 if necessary.  */
 #define REGEX_PREFETCH()                                                       \
-  while (d == dend)                                                    \
+  while (d == dend)                                                    \
     {                                                                  \
       /* End of string2 => fail.  */                                   \
-      if (dend == end_match_2)                                                 \
-        goto fail;                                                     \
-      /* End of string1 => advance to string2.  */                     \
+      if (dend == end_match_2)                                         \
+       goto fail;                                                      \
+      /* End of string1 => advance to string2.  */                     \
       d = string2;                                                     \
       dend = end_match_2;                                              \
     }
@@ -4579,7 +4575,7 @@ re_search_2(struct re_pattern_buffer *bufp, const char *str1,
 /* Test if CH is a word-constituent character. (XEmacs change) */
 #define WORDCHAR_P_UNSAFE(ch)                                             \
   (SYNTAX_UNSAFE (XCHAR_TABLE (regex_emacs_buffer->mirror_syntax_table),   \
-                               ch) == Sword)
+                              ch) == Sword)
 
 /* Free everything we malloc.  */
 #ifdef MATCH_MAY_ALLOCATE
@@ -4684,7 +4680,7 @@ re_match_2_internal(struct re_pattern_buffer *bufp, re_char * string1,
        re_char *end_match_1, *end_match_2;
 
        /* Where we are in the data, and the end of the current string.  */
-       re_char *d, *dend;
+       const re_char *d, *dend;
 
        /* Where we are in the pattern, and the end of the pattern.  */
        unsigned char *p = bufp->buffer;
@@ -4985,8 +4981,8 @@ re_match_2_internal(struct re_pattern_buffer *bufp, re_char * string1,
                                if (regs && !bufp->no_sub) {
                                        /* Have the register data arrays been
                                           allocated?  */
-                                       if (bufp->regs_allocated == REGS_UNALLOCATED) { 
-                                          /* No.  So allocate them with malloc.
+                                       if (bufp->regs_allocated == REGS_UNALLOCATED) {
+                                         /* No.  So allocate them with malloc.
                                             We need one extra element beyond
                                             `num_regs' for the `-1' marker GNU
                                             code uses.  */
@@ -4998,8 +4994,8 @@ re_match_2_internal(struct re_pattern_buffer *bufp, re_char * string1,
                                                        return -2;
                                                }
                                                bufp->regs_allocated = REGS_REALLOCATE;
-                                       } else if (bufp->regs_allocated == REGS_REALLOCATE) { 
-                                          /* Yes.  If we need more elements than were already
+                                       } else if (bufp->regs_allocated == REGS_REALLOCATE) {
+                                         /* Yes.  If we need more elements than were already
                                             allocated, reallocate them.  If we need fewer, just
                                             leave it alone.  */
                                                if (regs->num_regs < num_nonshy_regs + 1) {
@@ -5658,7 +5654,7 @@ re_match_2_internal(struct re_pattern_buffer *bufp, re_char * string1,
                        EXTRACT_NUMBER_AND_INCR(mcnt, p);
                        DEBUG_PRINT2("EXECUTING maybe_pop_jump %d.\n", mcnt);
                        {
-                               REGISTER unsigned char *p2 = p;
+                               REGISTER const unsigned char *p2 = p;
 
                                /* Compare the beginning of the repeat with what
                                   in the pattern follows its end. If we can
@@ -5851,8 +5847,8 @@ re_match_2_internal(struct re_pattern_buffer *bufp, re_char * string1,
                           from the stack, since lowest will == highest
                           in `pop_failure_point'.  */
                        int dummy_low_reg, dummy_high_reg;
-                       re_char *pdummy;
-                       re_char *sdummy = NULL;
+                       const unsigned char *pdummy = NULL;
+                       const unsigned char *sdummy = NULL;
 
                        DEBUG_PRINT1("EXECUTING pop_failure_jump.\n");
                        POP_FAILURE_POINT(sdummy, pdummy,
@@ -6268,9 +6264,9 @@ re_match_2_internal(struct re_pattern_buffer *bufp, re_char * string1,
                if (!FAIL_STACK_EMPTY()) {
                        /* A restart point is known.  Restore to that state.  */
                        DEBUG_PRINT1("\nFAIL:\n");
-                       POP_FAILURE_POINT(d, p /* used to be const char* */,
-                                         lowest_active_reg, highest_active_reg,
-                                         regstart, regend, reg_info);
+                       POP_FAILURE_POINT(d, p, lowest_active_reg,
+                                         highest_active_reg, regstart, regend,
+                                         reg_info);
 
                        /* If this failure point is a dummy, try the next one.  */
                        if (!p)
@@ -6320,6 +6316,7 @@ re_match_2_internal(struct re_pattern_buffer *bufp, re_char * string1,
 
        return -1;              /* Failure to match.  */
 }                              /* re_match_2 */
+
 \f
 /* Subroutine definitions for re_match_2.  */
 
@@ -6540,6 +6537,7 @@ common_op_match_null_string_p(unsigned char **p, unsigned char *end,
 
        case set_number_at:
                p1 += 4;
+               break;
 
        default:
                /* All other opcodes mean we cannot match the empty string.  */
@@ -6861,8 +6859,8 @@ regerror(int errcode, const regex_t * preg, char *errbuf,
        msg_size = strlen(msg) + 1;     /* Includes the null.  */
 
        if (errbuf_size != 0) {
-                strncpy(errbuf, msg, errbuf_size - 1);
-                errbuf[errbuf_size-1]='\0';
+               strncpy(errbuf, msg, errbuf_size - 1);
+               errbuf[errbuf_size-1]='\0';
        }
 
        return msg_size;