home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnugrep.zip / regex.c < prev    next >
C/C++ Source or Header  |  1993-05-21  |  163KB  |  4,988 lines

  1. /* Extended regular expression matching and search library,
  2.    version 0.12.
  3.    (Implements POSIX draft P10003.2/D11.2, except for
  4.    internationalization features.)
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /* AIX requires this to be the first thing in the file. */
  23. #if defined (_AIX) && !defined (REGEX_MALLOC)
  24.   #pragma alloca
  25. #endif
  26.  
  27. #define _GNU_SOURCE
  28.  
  29. /* We need this for `regex.h', and perhaps for the Emacs include files.  */
  30. #include <sys/types.h>
  31.  
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35.  
  36. /* The `emacs' switch turns on certain matching commands
  37.    that make sense only in Emacs. */
  38. #ifdef emacs
  39.  
  40. #include "lisp.h"
  41. #include "buffer.h"
  42. #include "syntax.h"
  43.  
  44. /* Emacs uses `NULL' as a predicate.  */
  45. #undef NULL
  46.  
  47. #else  /* not emacs */
  48.  
  49. /* We used to test for `BSTRING' here, but only GCC and Emacs define
  50.    `BSTRING', as far as I know, and neither of them use this code.  */
  51. #if HAVE_STRING_H || STDC_HEADERS
  52. #include <string.h>
  53. #ifndef bcmp
  54. #define bcmp(s1, s2, n)    memcmp ((s1), (s2), (n))
  55. #endif
  56. #ifndef bcopy
  57. #define bcopy(s, d, n)    memcpy ((d), (s), (n))
  58. #endif
  59. #ifndef bzero
  60. #define bzero(s, n)    memset ((s), 0, (n))
  61. #endif
  62. #else
  63. #include <strings.h>
  64. #endif
  65.  
  66. #ifdef STDC_HEADERS
  67. #include <stdlib.h>
  68. #else
  69. char *malloc ();
  70. char *realloc ();
  71. #endif
  72.  
  73.  
  74. /* Define the syntax stuff for \<, \>, etc.  */
  75.  
  76. /* This must be nonzero for the wordchar and notwordchar pattern
  77.    commands in re_match_2.  */
  78. #ifndef Sword 
  79. #define Sword 1
  80. #endif
  81.  
  82. #ifdef SYNTAX_TABLE
  83.  
  84. extern char *re_syntax_table;
  85.  
  86. #else /* not SYNTAX_TABLE */
  87.  
  88. /* How many characters in the character set.  */
  89. #define CHAR_SET_SIZE 256
  90.  
  91. static char re_syntax_table[CHAR_SET_SIZE];
  92.  
  93. static void
  94. init_syntax_once ()
  95. {
  96.    register int c;
  97.    static int done = 0;
  98.  
  99.    if (done)
  100.      return;
  101.  
  102.    bzero (re_syntax_table, sizeof re_syntax_table);
  103.  
  104.    for (c = 'a'; c <= 'z'; c++)
  105.      re_syntax_table[c] = Sword;
  106.  
  107.    for (c = 'A'; c <= 'Z'; c++)
  108.      re_syntax_table[c] = Sword;
  109.  
  110.    for (c = '0'; c <= '9'; c++)
  111.      re_syntax_table[c] = Sword;
  112.  
  113.    re_syntax_table['_'] = Sword;
  114.  
  115.    done = 1;
  116. }
  117.  
  118. #endif /* not SYNTAX_TABLE */
  119.  
  120. #define SYNTAX(c) re_syntax_table[c]
  121.  
  122. #endif /* not emacs */
  123.  
  124. /* Get the interface, including the syntax bits.  */
  125. #include "regex.h"
  126.  
  127. /* isalpha etc. are used for the character classes.  */
  128. #include <ctype.h>
  129.  
  130. /* Jim Meyering writes:
  131.  
  132.    "... Some ctype macros are valid only for character codes that
  133.    isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
  134.    using /bin/cc or gcc but without giving an ansi option).  So, all
  135.    ctype uses should be through macros like ISPRINT...  If
  136.    STDC_HEADERS is defined, then autoconf has verified that the ctype
  137.    macros don't need to be guarded with references to isascii. ...
  138.    Defining isascii to 1 should let any compiler worth its salt
  139.    eliminate the && through constant folding."  */
  140. #if ! defined (isascii) || defined (STDC_HEADERS)
  141. #undef isascii
  142. #define isascii(c) 1
  143. #endif
  144.  
  145. #ifdef isblank
  146. #define ISBLANK(c) (isascii (c) && isblank (c))
  147. #else
  148. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  149. #endif
  150. #ifdef isgraph
  151. #define ISGRAPH(c) (isascii (c) && isgraph (c))
  152. #else
  153. #define ISGRAPH(c) (isascii (c) && isprint (c) && !isspace (c))
  154. #endif
  155.  
  156. #define ISPRINT(c) (isascii (c) && isprint (c))
  157. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  158. #define ISALNUM(c) (isascii (c) && isalnum (c))
  159. #define ISALPHA(c) (isascii (c) && isalpha (c))
  160. #define ISCNTRL(c) (isascii (c) && iscntrl (c))
  161. #define ISLOWER(c) (isascii (c) && islower (c))
  162. #define ISPUNCT(c) (isascii (c) && ispunct (c))
  163. #define ISSPACE(c) (isascii (c) && isspace (c))
  164. #define ISUPPER(c) (isascii (c) && isupper (c))
  165. #define ISXDIGIT(c) (isascii (c) && isxdigit (c))
  166.  
  167. #ifndef NULL
  168. #define NULL 0
  169. #endif
  170.  
  171. /* We remove any previous definition of `SIGN_EXTEND_CHAR',
  172.    since ours (we hope) works properly with all combinations of
  173.    machines, compilers, `char' and `unsigned char' argument types.
  174.    (Per Bothner suggested the basic approach.)  */
  175. #undef SIGN_EXTEND_CHAR
  176. #if __STDC__
  177. #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
  178. #else  /* not __STDC__ */
  179. /* As in Harbison and Steele.  */
  180. #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
  181. #endif
  182.  
  183. /* Should we use malloc or alloca?  If REGEX_MALLOC is not defined, we
  184.    use `alloca' instead of `malloc'.  This is because using malloc in
  185.    re_search* or re_match* could cause memory leaks when C-g is used in
  186.    Emacs; also, malloc is slower and causes storage fragmentation.  On
  187.    the other hand, malloc is more portable, and easier to debug.  
  188.    
  189.    Because we sometimes use alloca, some routines have to be macros,
  190.    not functions -- `alloca'-allocated space disappears at the end of the
  191.    function it is called in.  */
  192.  
  193. #ifdef REGEX_MALLOC
  194.  
  195. #define REGEX_ALLOCATE malloc
  196. #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
  197.  
  198. #else /* not REGEX_MALLOC  */
  199.  
  200. /* Emacs already defines alloca, sometimes.  */
  201. #ifndef alloca
  202.  
  203. /* Make alloca work the best possible way.  */
  204. #ifdef __GNUC__
  205. #define alloca __builtin_alloca
  206. #else /* not __GNUC__ */
  207. #if HAVE_ALLOCA_H
  208. #include <alloca.h>
  209. #else /* not __GNUC__ or HAVE_ALLOCA_H */
  210. #ifndef _AIX /* Already did AIX, up at the top.  */
  211. char *alloca ();
  212. #endif /* not _AIX */
  213. #endif /* not HAVE_ALLOCA_H */ 
  214. #endif /* not __GNUC__ */
  215.  
  216. #endif /* not alloca */
  217.  
  218. #define REGEX_ALLOCATE alloca
  219.  
  220. /* Assumes a `char *destination' variable.  */
  221. #define REGEX_REALLOCATE(source, osize, nsize)                \
  222.   (destination = (char *) alloca (nsize),                \
  223.    bcopy (source, destination, osize),                    \
  224.    destination)
  225.  
  226. #endif /* not REGEX_MALLOC */
  227.  
  228.  
  229. /* True if `size1' is non-NULL and PTR is pointing anywhere inside
  230.    `string1' or just past its end.  This works if PTR is NULL, which is
  231.    a good thing.  */
  232. #define FIRST_STRING_P(ptr)                     \
  233.   (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  234.  
  235. /* (Re)Allocate N items of type T using malloc, or fail.  */
  236. #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  237. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  238. #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
  239.  
  240. #define BYTEWIDTH 8 /* In bits.  */
  241.  
  242. #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  243.  
  244. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  245. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  246.  
  247. typedef char boolean;
  248. #define false 0
  249. #define true 1
  250.  
  251. /* These are the command codes that appear in compiled regular
  252.    expressions.  Some opcodes are followed by argument bytes.  A
  253.    command code can specify any interpretation whatsoever for its
  254.    arguments.  Zero bytes may appear in the compiled regular expression.
  255.  
  256.    The value of `exactn' is needed in search.c (search_buffer) in Emacs.
  257.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  258.    `exactn' we use here must also be 1.  */
  259.  
  260. typedef enum
  261. {
  262.   no_op = 0,
  263.  
  264.         /* Followed by one byte giving n, then by n literal bytes.  */
  265.   exactn = 1,
  266.  
  267.         /* Matches any (more or less) character.  */
  268.   anychar,
  269.  
  270.         /* Matches any one char belonging to specified set.  First
  271.            following byte is number of bitmap bytes.  Then come bytes
  272.            for a bitmap saying which chars are in.  Bits in each byte
  273.            are ordered low-bit-first.  A character is in the set if its
  274.            bit is 1.  A character too large to have a bit in the map is
  275.            automatically not in the set.  */
  276.   charset,
  277.  
  278.         /* Same parameters as charset, but match any character that is
  279.            not one of those specified.  */
  280.   charset_not,
  281.  
  282.         /* Start remembering the text that is matched, for storing in a
  283.            register.  Followed by one byte with the register number, in
  284.            the range 0 to one less than the pattern buffer's re_nsub
  285.            field.  Then followed by one byte with the number of groups
  286.            inner to this one.  (This last has to be part of the
  287.            start_memory only because we need it in the on_failure_jump
  288.            of re_match_2.)  */
  289.   start_memory,
  290.  
  291.         /* Stop remembering the text that is matched and store it in a
  292.            memory register.  Followed by one byte with the register
  293.            number, in the range 0 to one less than `re_nsub' in the
  294.            pattern buffer, and one byte with the number of inner groups,
  295.            just like `start_memory'.  (We need the number of inner
  296.            groups here because we don't have any easy way of finding the
  297.            corresponding start_memory when we're at a stop_memory.)  */
  298.   stop_memory,
  299.  
  300.         /* Match a duplicate of something remembered. Followed by one
  301.            byte containing the register number.  */
  302.   duplicate,
  303.  
  304.         /* Fail unless at beginning of line.  */
  305.   begline,
  306.  
  307.         /* Fail unless at end of line.  */
  308.   endline,
  309.  
  310.         /* Succeeds if at beginning of buffer (if emacs) or at beginning
  311.            of string to be matched (if not).  */
  312.   begbuf,
  313.  
  314.         /* Analogously, for end of buffer/string.  */
  315.   endbuf,
  316.  
  317.         /* Followed by two byte relative address to which to jump.  */
  318.   jump, 
  319.  
  320.     /* Same as jump, but marks the end of an alternative.  */
  321.   jump_past_alt,
  322.  
  323.         /* Followed by two-byte relative address of place to resume at
  324.            in case of failure.  */
  325.   on_failure_jump,
  326.     
  327.         /* Like on_failure_jump, but pushes a placeholder instead of the
  328.            current string position when executed.  */
  329.   on_failure_keep_string_jump,
  330.   
  331.         /* Throw away latest failure point and then jump to following
  332.            two-byte relative address.  */
  333.   pop_failure_jump,
  334.  
  335.         /* Change to pop_failure_jump if know won't have to backtrack to
  336.            match; otherwise change to jump.  This is used to jump
  337.            back to the beginning of a repeat.  If what follows this jump
  338.            clearly won't match what the repeat does, such that we can be
  339.            sure that there is no use backtracking out of repetitions
  340.            already matched, then we change it to a pop_failure_jump.
  341.            Followed by two-byte address.  */
  342.   maybe_pop_jump,
  343.  
  344.         /* Jump to following two-byte address, and push a dummy failure
  345.            point. This failure point will be thrown away if an attempt
  346.            is made to use it for a failure.  A `+' construct makes this
  347.            before the first repeat.  Also used as an intermediary kind
  348.            of jump when compiling an alternative.  */
  349.   dummy_failure_jump,
  350.  
  351.     /* Push a dummy failure point and continue.  Used at the end of
  352.        alternatives.  */
  353.   push_dummy_failure,
  354.  
  355.         /* Followed by two-byte relative address and two-byte number n.
  356.            After matching N times, jump to the address upon failure.  */
  357.   succeed_n,
  358.  
  359.         /* Followed by two-byte relative address, and two-byte number n.
  360.            Jump to the address N times, then fail.  */
  361.   jump_n,
  362.  
  363.         /* Set the following two-byte relative address to the
  364.            subsequent two-byte number.  The address *includes* the two
  365.            bytes of number.  */
  366.   set_number_at,
  367.  
  368.   wordchar,    /* Matches any word-constituent character.  */
  369.   notwordchar,    /* Matches any char that is not a word-constituent.  */
  370.  
  371.   wordbeg,    /* Succeeds if at word beginning.  */
  372.   wordend,    /* Succeeds if at word end.  */
  373.  
  374.   wordbound,    /* Succeeds if at a word boundary.  */
  375.   notwordbound    /* Succeeds if not at a word boundary.  */
  376.  
  377. #ifdef emacs
  378.   ,before_dot,    /* Succeeds if before point.  */
  379.   at_dot,    /* Succeeds if at point.  */
  380.   after_dot,    /* Succeeds if after point.  */
  381.  
  382.     /* Matches any character whose syntax is specified.  Followed by
  383.            a byte which contains a syntax code, e.g., Sword.  */
  384.   syntaxspec,
  385.  
  386.     /* Matches any character whose syntax is not that specified.  */
  387.   notsyntaxspec
  388. #endif /* emacs */
  389. } re_opcode_t;
  390.  
  391. /* Common operations on the compiled pattern.  */
  392.  
  393. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  394.  
  395. #define STORE_NUMBER(destination, number)                \
  396.   do {                                    \
  397.     (destination)[0] = (number) & 0377;                    \
  398.     (destination)[1] = (number) >> 8;                    \
  399.   } while (0)
  400.  
  401. /* Same as STORE_NUMBER, except increment DESTINATION to
  402.    the byte after where the number is stored.  Therefore, DESTINATION
  403.    must be an lvalue.  */
  404.  
  405. #define STORE_NUMBER_AND_INCR(destination, number)            \
  406.   do {                                    \
  407.     STORE_NUMBER (destination, number);                    \
  408.     (destination) += 2;                            \
  409.   } while (0)
  410.  
  411. /* Put into DESTINATION a number stored in two contiguous bytes starting
  412.    at SOURCE.  */
  413.  
  414. #define EXTRACT_NUMBER(destination, source)                \
  415.   do {                                    \
  416.     (destination) = *(source) & 0377;                    \
  417.     (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8;        \
  418.   } while (0)
  419.  
  420. #ifdef DEBUG
  421. static void
  422. extract_number (dest, source)
  423.     int *dest;
  424.     unsigned char *source;
  425. {
  426.   int temp = SIGN_EXTEND_CHAR (*(source + 1)); 
  427.   *dest = *source & 0377;
  428.   *dest += temp << 8;
  429. }
  430.  
  431. #ifndef EXTRACT_MACROS /* To debug the macros.  */
  432. #undef EXTRACT_NUMBER
  433. #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
  434. #endif /* not EXTRACT_MACROS */
  435.  
  436. #endif /* DEBUG */
  437.  
  438. /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
  439.    SOURCE must be an lvalue.  */
  440.  
  441. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  442.   do {                                    \
  443.     EXTRACT_NUMBER (destination, source);                \
  444.     (source) += 2;                             \
  445.   } while (0)
  446.  
  447. #ifdef DEBUG
  448. static void
  449. extract_number_and_incr (destination, source)
  450.     int *destination;
  451.     unsigned char **source;
  452.   extract_number (destination, *source);
  453.   *source += 2;
  454. }
  455.  
  456. #ifndef EXTRACT_MACROS
  457. #undef EXTRACT_NUMBER_AND_INCR
  458. #define EXTRACT_NUMBER_AND_INCR(dest, src) \
  459.   extract_number_and_incr (&dest, &src)
  460. #endif /* not EXTRACT_MACROS */
  461.  
  462. #endif /* DEBUG */
  463.  
  464. /* If DEBUG is defined, Regex prints many voluminous messages about what
  465.    it is doing (if the variable `debug' is nonzero).  If linked with the
  466.    main program in `iregex.c', you can enter patterns and strings
  467.    interactively.  And if linked with the main program in `main.c' and
  468.    the other test files, you can run the already-written tests.  */
  469.  
  470. #ifdef DEBUG
  471.  
  472. /* We use standard I/O for debugging.  */
  473. #include <stdio.h>
  474.  
  475. /* It is useful to test things that ``must'' be true when debugging.  */
  476. #include <assert.h>
  477.  
  478. static int debug = 0;
  479.  
  480. #define DEBUG_STATEMENT(e) e
  481. #define DEBUG_PRINT1(x) if (debug) printf (x)
  482. #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
  483. #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
  484. #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
  485. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)                 \
  486.   if (debug) print_partial_compiled_pattern (s, e)
  487. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)            \
  488.   if (debug) print_double_string (w, s1, sz1, s2, sz2)
  489.  
  490.  
  491. extern void printchar ();
  492.  
  493. /* Print the fastmap in human-readable form.  */
  494.  
  495. void
  496. print_fastmap (fastmap)
  497.     char *fastmap;
  498. {
  499.   unsigned was_a_range = 0;
  500.   unsigned i = 0;  
  501.   
  502.   while (i < (1 << BYTEWIDTH))
  503.     {
  504.       if (fastmap[i++])
  505.     {
  506.       was_a_range = 0;
  507.           printchar (i - 1);
  508.           while (i < (1 << BYTEWIDTH)  &&  fastmap[i])
  509.             {
  510.               was_a_range = 1;
  511.               i++;
  512.             }
  513.       if (was_a_range)
  514.             {
  515.               printf ("-");
  516.               printchar (i - 1);
  517.             }
  518.         }
  519.     }
  520.   putchar ('\n'); 
  521. }
  522.  
  523.  
  524. /* Print a compiled pattern string in human-readable form, starting at
  525.    the START pointer into it and ending just before the pointer END.  */
  526.  
  527. void
  528. print_partial_compiled_pattern (start, end)
  529.     unsigned char *start;
  530.     unsigned char *end;
  531. {
  532.   int mcnt, mcnt2;
  533.   unsigned char *p = start;
  534.   unsigned char *pend = end;
  535.  
  536.   if (start == NULL)
  537.     {
  538.       printf ("(null)\n");
  539.       return;
  540.     }
  541.     
  542.   /* Loop over pattern commands.  */
  543.   while (p < pend)
  544.     {
  545.       printf ("%d:\t", p - start);
  546.  
  547.       switch ((re_opcode_t) *p++)
  548.     {
  549.         case no_op:
  550.           printf ("/no_op");
  551.           break;
  552.  
  553.     case exactn:
  554.       mcnt = *p++;
  555.           printf ("/exactn/%d", mcnt);
  556.           do
  557.         {
  558.               putchar ('/');
  559.           printchar (*p++);
  560.             }
  561.           while (--mcnt);
  562.           break;
  563.  
  564.     case start_memory:
  565.           mcnt = *p++;
  566.           printf ("/start_memory/%d/%d", mcnt, *p++);
  567.           break;
  568.  
  569.     case stop_memory:
  570.           mcnt = *p++;
  571.       printf ("/stop_memory/%d/%d", mcnt, *p++);
  572.           break;
  573.  
  574.     case duplicate:
  575.       printf ("/duplicate/%d", *p++);
  576.       break;
  577.  
  578.     case anychar:
  579.       printf ("/anychar");
  580.       break;
  581.  
  582.     case charset:
  583.         case charset_not:
  584.           {
  585.             register int c, last = -100;
  586.         register int in_range = 0;
  587.  
  588.         printf ("/charset [%s",
  589.                 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
  590.             
  591.             assert (p + *p < pend);
  592.  
  593.             for (c = 0; c < 256; c++)
  594.           if (c / 8 < *p
  595.           && (p[1 + (c/8)] & (1 << (c % 8))))
  596.         {
  597.           /* Are we starting a range?  */
  598.           if (last + 1 == c && ! in_range)
  599.             {
  600.               putchar ('-');
  601.               in_range = 1;
  602.             }
  603.           /* Have we broken a range?  */
  604.           else if (last + 1 != c && in_range)
  605.               {
  606.               printchar (last);
  607.               in_range = 0;
  608.             }
  609.                 
  610.           if (! in_range)
  611.             printchar (c);
  612.  
  613.           last = c;
  614.               }
  615.  
  616.         if (in_range)
  617.           printchar (last);
  618.  
  619.         putchar (']');
  620.  
  621.         p += 1 + *p;
  622.       }
  623.       break;
  624.  
  625.     case begline:
  626.       printf ("/begline");
  627.           break;
  628.  
  629.     case endline:
  630.           printf ("/endline");
  631.           break;
  632.  
  633.     case on_failure_jump:
  634.           extract_number_and_incr (&mcnt, &p);
  635.         printf ("/on_failure_jump to %d", p + mcnt - start);
  636.           break;
  637.  
  638.     case on_failure_keep_string_jump:
  639.           extract_number_and_incr (&mcnt, &p);
  640.         printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
  641.           break;
  642.  
  643.     case dummy_failure_jump:
  644.           extract_number_and_incr (&mcnt, &p);
  645.         printf ("/dummy_failure_jump to %d", p + mcnt - start);
  646.           break;
  647.  
  648.     case push_dummy_failure:
  649.           printf ("/push_dummy_failure");
  650.           break;
  651.           
  652.         case maybe_pop_jump:
  653.           extract_number_and_incr (&mcnt, &p);
  654.         printf ("/maybe_pop_jump to %d", p + mcnt - start);
  655.       break;
  656.  
  657.         case pop_failure_jump:
  658.       extract_number_and_incr (&mcnt, &p);
  659.         printf ("/pop_failure_jump to %d", p + mcnt - start);
  660.       break;          
  661.           
  662.         case jump_past_alt:
  663.       extract_number_and_incr (&mcnt, &p);
  664.         printf ("/jump_past_alt to %d", p + mcnt - start);
  665.       break;          
  666.           
  667.         case jump:
  668.       extract_number_and_incr (&mcnt, &p);
  669.         printf ("/jump to %d", p + mcnt - start);
  670.       break;
  671.  
  672.         case succeed_n: 
  673.           extract_number_and_incr (&mcnt, &p);
  674.           extract_number_and_incr (&mcnt2, &p);
  675.       printf ("/succeed_n to %d, %d times", p + mcnt - start, mcnt2);
  676.           break;
  677.         
  678.         case jump_n: 
  679.           extract_number_and_incr (&mcnt, &p);
  680.           extract_number_and_incr (&mcnt2, &p);
  681.       printf ("/jump_n to %d, %d times", p + mcnt - start, mcnt2);
  682.           break;
  683.         
  684.         case set_number_at: 
  685.           extract_number_and_incr (&mcnt, &p);
  686.           extract_number_and_incr (&mcnt2, &p);
  687.       printf ("/set_number_at location %d to %d", p + mcnt - start, mcnt2);
  688.           break;
  689.         
  690.         case wordbound:
  691.       printf ("/wordbound");
  692.       break;
  693.  
  694.     case notwordbound:
  695.       printf ("/notwordbound");
  696.           break;
  697.  
  698.     case wordbeg:
  699.       printf ("/wordbeg");
  700.       break;
  701.           
  702.     case wordend:
  703.       printf ("/wordend");
  704.           
  705. #ifdef emacs
  706.     case before_dot:
  707.       printf ("/before_dot");
  708.           break;
  709.  
  710.     case at_dot:
  711.       printf ("/at_dot");
  712.           break;
  713.  
  714.     case after_dot:
  715.       printf ("/after_dot");
  716.           break;
  717.  
  718.     case syntaxspec:
  719.           printf ("/syntaxspec");
  720.       mcnt = *p++;
  721.       printf ("/%d", mcnt);
  722.           break;
  723.       
  724.     case notsyntaxspec:
  725.           printf ("/notsyntaxspec");
  726.       mcnt = *p++;
  727.       printf ("/%d", mcnt);
  728.       break;
  729. #endif /* emacs */
  730.  
  731.     case wordchar:
  732.       printf ("/wordchar");
  733.           break;
  734.       
  735.     case notwordchar:
  736.       printf ("/notwordchar");
  737.           break;
  738.  
  739.     case begbuf:
  740.       printf ("/begbuf");
  741.           break;
  742.  
  743.     case endbuf:
  744.       printf ("/endbuf");
  745.           break;
  746.  
  747.         default:
  748.           printf ("?%d", *(p-1));
  749.     }
  750.  
  751.       putchar ('\n');
  752.     }
  753.  
  754.   printf ("%d:\tend of pattern.\n", p - start);
  755. }
  756.  
  757.  
  758. void
  759. print_compiled_pattern (bufp)
  760.     struct re_pattern_buffer *bufp;
  761. {
  762.   unsigned char *buffer = bufp->buffer;
  763.  
  764.   print_partial_compiled_pattern (buffer, buffer + bufp->used);
  765.   printf ("%d bytes used/%d bytes allocated.\n", bufp->used, bufp->allocated);
  766.  
  767.   if (bufp->fastmap_accurate && bufp->fastmap)
  768.     {
  769.       printf ("fastmap: ");
  770.       print_fastmap (bufp->fastmap);
  771.     }
  772.  
  773.   printf ("re_nsub: %d\t", bufp->re_nsub);
  774.   printf ("regs_alloc: %d\t", bufp->regs_allocated);
  775.   printf ("can_be_null: %d\t", bufp->can_be_null);
  776.   printf ("newline_anchor: %d\n", bufp->newline_anchor);
  777.   printf ("no_sub: %d\t", bufp->no_sub);
  778.   printf ("not_bol: %d\t", bufp->not_bol);
  779.   printf ("not_eol: %d\t", bufp->not_eol);
  780.   printf ("syntax: %d\n", bufp->syntax);
  781.   /* Perhaps we should print the translate table?  */
  782. }
  783.  
  784.  
  785. void
  786. print_double_string (where, string1, size1, string2, size2)
  787.     const char *where;
  788.     const char *string1;
  789.     const char *string2;
  790.     int size1;
  791.     int size2;
  792. {
  793.   unsigned this_char;
  794.   
  795.   if (where == NULL)
  796.     printf ("(null)");
  797.   else
  798.     {
  799.       if (FIRST_STRING_P (where))
  800.         {
  801.           for (this_char = where - string1; this_char < size1; this_char++)
  802.             printchar (string1[this_char]);
  803.  
  804.           where = string2;    
  805.         }
  806.  
  807.       for (this_char = where - string2; this_char < size2; this_char++)
  808.         printchar (string2[this_char]);
  809.     }
  810. }
  811.  
  812. #else /* not DEBUG */
  813.  
  814. #undef assert
  815. #define assert(e)
  816.  
  817. #define DEBUG_STATEMENT(e)
  818. #define DEBUG_PRINT1(x)
  819. #define DEBUG_PRINT2(x1, x2)
  820. #define DEBUG_PRINT3(x1, x2, x3)
  821. #define DEBUG_PRINT4(x1, x2, x3, x4)
  822. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
  823. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
  824.  
  825. #endif /* not DEBUG */
  826.  
  827. /* Set by `re_set_syntax' to the current regexp syntax to recognize.  Can
  828.    also be assigned to arbitrarily: each pattern buffer stores its own
  829.    syntax, so it can be changed between regex compilations.  */
  830. reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS;
  831.  
  832.  
  833. /* Specify the precise syntax of regexps for compilation.  This provides
  834.    for compatibility for various utilities which historically have
  835.    different, incompatible syntaxes.
  836.  
  837.    The argument SYNTAX is a bit mask comprised of the various bits
  838.    defined in regex.h.  We return the old syntax.  */
  839.  
  840. reg_syntax_t
  841. re_set_syntax (syntax)
  842.     reg_syntax_t syntax;
  843. {
  844.   reg_syntax_t ret = re_syntax_options;
  845.   
  846.   re_syntax_options = syntax;
  847.   return ret;
  848. }
  849.  
  850. /* This table gives an error message for each of the error codes listed
  851.    in regex.h.  Obviously the order here has to be same as there.  */
  852.  
  853. static const char *re_error_msg[] =
  854.   { NULL,                    /* REG_NOERROR */
  855.     "No match",                    /* REG_NOMATCH */
  856.     "Invalid regular expression",        /* REG_BADPAT */
  857.     "Invalid collation character",        /* REG_ECOLLATE */
  858.     "Invalid character class name",        /* REG_ECTYPE */
  859.     "Trailing backslash",            /* REG_EESCAPE */
  860.     "Invalid back reference",            /* REG_ESUBREG */
  861.     "Unmatched [ or [^",            /* REG_EBRACK */
  862.     "Unmatched ( or \\(",            /* REG_EPAREN */
  863.     "Unmatched \\{",                /* REG_EBRACE */
  864.     "Invalid content of \\{\\}",        /* REG_BADBR */
  865.     "Invalid range end",            /* REG_ERANGE */
  866.     "Memory exhausted",                /* REG_ESPACE */
  867.     "Invalid preceding regular expression",    /* REG_BADRPT */
  868.     "Premature end of regular expression",    /* REG_EEND */
  869.     "Regular expression too big",        /* REG_ESIZE */
  870.     "Unmatched ) or \\)",            /* REG_ERPAREN */
  871.   };
  872.  
  873. /* Subroutine declarations and macros for regex_compile.  */
  874.  
  875. static void store_op1 (), store_op2 ();
  876. static void insert_op1 (), insert_op2 ();
  877. static boolean at_begline_loc_p (), at_endline_loc_p ();
  878. static boolean group_in_compile_stack ();
  879. static reg_errcode_t compile_range ();
  880.  
  881. /* Fetch the next character in the uncompiled pattern---translating it 
  882.    if necessary.  Also cast from a signed character in the constant
  883.    string passed to us by the user to an unsigned char that we can use
  884.    as an array index (in, e.g., `translate').  */
  885. #define PATFETCH(c)                            \
  886.   do {if (p == pend) return REG_EEND;                    \
  887.     c = (unsigned char) *p++;                        \
  888.     if (translate) c = translate[c];                     \
  889.   } while (0)
  890.  
  891. /* Fetch the next character in the uncompiled pattern, with no
  892.    translation.  */
  893. #define PATFETCH_RAW(c)                            \
  894.   do {if (p == pend) return REG_EEND;                    \
  895.     c = (unsigned char) *p++;                         \
  896.   } while (0)
  897.  
  898. /* Go backwards one character in the pattern.  */
  899. #define PATUNFETCH p--
  900.  
  901.  
  902. /* If `translate' is non-null, return translate[D], else just D.  We
  903.    cast the subscript to translate because some data is declared as
  904.    `char *', to avoid warnings when a string constant is passed.  But
  905.    when we use a character as a subscript we must make it unsigned.  */
  906. #define TRANSLATE(d) (translate ? translate[(unsigned char) (d)] : (d))
  907.  
  908.  
  909. /* Macros for outputting the compiled pattern into `buffer'.  */
  910.  
  911. /* If the buffer isn't allocated when it comes in, use this.  */
  912. #define INIT_BUF_SIZE  32
  913.  
  914. /* Make sure we have at least N more bytes of space in buffer.  */
  915. #define GET_BUFFER_SPACE(n)                        \
  916.     while (b - bufp->buffer + (n) > bufp->allocated)            \
  917.       EXTEND_BUFFER ()
  918.  
  919. /* Make sure we have one more byte of buffer space and then add C to it.  */
  920. #define BUF_PUSH(c)                            \
  921.   do {                                    \
  922.     GET_BUFFER_SPACE (1);                        \
  923.     *b++ = (unsigned char) (c);                        \
  924.   } while (0)
  925.  
  926.  
  927. /* Ensure we have two more bytes of buffer space and then append C1 and C2.  */
  928. #define BUF_PUSH_2(c1, c2)                        \
  929.   do {                                    \
  930.     GET_BUFFER_SPACE (2);                        \
  931.     *b++ = (unsigned char) (c1);                    \
  932.     *b++ = (unsigned char) (c2);                    \
  933.   } while (0)
  934.  
  935.  
  936. /* As with BUF_PUSH_2, except for three bytes.  */
  937. #define BUF_PUSH_3(c1, c2, c3)                        \
  938.   do {                                    \
  939.     GET_BUFFER_SPACE (3);                        \
  940.     *b++ = (unsigned char) (c1);                    \
  941.     *b++ = (unsigned char) (c2);                    \
  942.     *b++ = (unsigned char) (c3);                    \
  943.   } while (0)
  944.  
  945.  
  946. /* Store a jump with opcode OP at LOC to location TO.  We store a
  947.    relative address offset by the three bytes the jump itself occupies.  */
  948. #define STORE_JUMP(op, loc, to) \
  949.   store_op1 (op, loc, (to) - (loc) - 3)
  950.  
  951. /* Likewise, for a two-argument jump.  */
  952. #define STORE_JUMP2(op, loc, to, arg) \
  953.   store_op2 (op, loc, (to) - (loc) - 3, arg)
  954.  
  955. /* Like `STORE_JUMP', but for inserting.  Assume `b' is the buffer end.  */
  956. #define INSERT_JUMP(op, loc, to) \
  957.   insert_op1 (op, loc, (to) - (loc) - 3, b)
  958.  
  959. /* Like `STORE_JUMP2', but for inserting.  Assume `b' is the buffer end.  */
  960. #define INSERT_JUMP2(op, loc, to, arg) \
  961.   insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
  962.  
  963.  
  964. /* This is not an arbitrary limit: the arguments which represent offsets
  965.    into the pattern are two bytes long.  So if 2^16 bytes turns out to
  966.    be too small, many things would have to change.  */
  967. #define MAX_BUF_SIZE (1L << 16)
  968.  
  969.  
  970. /* Extend the buffer by twice its current size via realloc and
  971.    reset the pointers that pointed into the old block to point to the
  972.    correct places in the new one.  If extending the buffer results in it
  973.    being larger than MAX_BUF_SIZE, then flag memory exhausted.  */
  974. #define EXTEND_BUFFER()                            \
  975.   do {                                     \
  976.     unsigned char *old_buffer = bufp->buffer;                \
  977.     if (bufp->allocated == MAX_BUF_SIZE)                 \
  978.       return REG_ESIZE;                            \
  979.     bufp->allocated <<= 1;                        \
  980.     if (bufp->allocated > MAX_BUF_SIZE)                    \
  981.       bufp->allocated = MAX_BUF_SIZE;                     \
  982.     bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
  983.     if (bufp->buffer == NULL)                        \
  984.       return REG_ESPACE;                        \
  985.     /* If the buffer moved, move all the pointers into it.  */        \
  986.     if (old_buffer != bufp->buffer)                    \
  987.       {                                    \
  988.         b = (b - old_buffer) + bufp->buffer;                \
  989.         begalt = (begalt - old_buffer) + bufp->buffer;            \
  990.         if (fixup_alt_jump)                        \
  991.           fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
  992.         if (laststart)                            \
  993.           laststart = (laststart - old_buffer) + bufp->buffer;        \
  994.         if (pending_exact)                        \
  995.           pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  996.       }                                    \
  997.   } while (0)
  998.  
  999.  
  1000. /* Since we have one byte reserved for the register number argument to
  1001.    {start,stop}_memory, the maximum number of groups we can report
  1002.    things about is what fits in that byte.  */
  1003. #define MAX_REGNUM 255
  1004.  
  1005. /* But patterns can have more than `MAX_REGNUM' registers.  We just
  1006.    ignore the excess.  */
  1007. typedef unsigned regnum_t;
  1008.  
  1009.  
  1010. /* Macros for the compile stack.  */
  1011.  
  1012. /* Since offsets can go either forwards or backwards, this type needs to
  1013.    be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1.  */
  1014. typedef int pattern_offset_t;
  1015.  
  1016. typedef struct
  1017. {
  1018.   pattern_offset_t begalt_offset;
  1019.   pattern_offset_t fixup_alt_jump;
  1020.   pattern_offset_t inner_group_offset;
  1021.   pattern_offset_t laststart_offset;  
  1022.   regnum_t regnum;
  1023. } compile_stack_elt_t;
  1024.  
  1025.  
  1026. typedef struct
  1027. {
  1028.   compile_stack_elt_t *stack;
  1029.   unsigned size;
  1030.   unsigned avail;            /* Offset of next open position.  */
  1031. } compile_stack_type;
  1032.  
  1033.  
  1034. #define INIT_COMPILE_STACK_SIZE 32
  1035.  
  1036. #define COMPILE_STACK_EMPTY  (compile_stack.avail == 0)
  1037. #define COMPILE_STACK_FULL  (compile_stack.avail == compile_stack.size)
  1038.  
  1039. /* The next available element.  */
  1040. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  1041.  
  1042.  
  1043. /* Set the bit for character C in a list.  */
  1044. #define SET_LIST_BIT(c)                               \
  1045.   (b[((unsigned char) (c)) / BYTEWIDTH]               \
  1046.    |= 1 << (((unsigned char) c) % BYTEWIDTH))
  1047.  
  1048.  
  1049. /* Get the next unsigned number in the uncompiled pattern.  */
  1050. #define GET_UNSIGNED_NUMBER(num)                     \
  1051.   { if (p != pend)                            \
  1052.      {                                    \
  1053.        PATFETCH (c);                             \
  1054.        while (ISDIGIT (c))                         \
  1055.          {                                 \
  1056.            if (num < 0)                            \
  1057.               num = 0;                            \
  1058.            num = num * 10 + c - '0';                     \
  1059.            if (p == pend)                         \
  1060.               break;                             \
  1061.            PATFETCH (c);                        \
  1062.          }                                 \
  1063.        }                                 \
  1064.     }        
  1065.  
  1066. #define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
  1067.  
  1068. #define IS_CHAR_CLASS(string)                        \
  1069.    (STREQ (string, "alpha") || STREQ (string, "upper")            \
  1070.     || STREQ (string, "lower") || STREQ (string, "digit")        \
  1071.     || STREQ (string, "alnum") || STREQ (string, "xdigit")        \
  1072.     || STREQ (string, "space") || STREQ (string, "print")        \
  1073.     || STREQ (string, "punct") || STREQ (string, "graph")        \
  1074.     || STREQ (string, "cntrl") || STREQ (string, "blank"))
  1075.  
  1076. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  1077.    Returns one of error codes defined in `regex.h', or zero for success.
  1078.  
  1079.    Assumes the `allocated' (and perhaps `buffer') and `translate'
  1080.    fields are set in BUFP on entry.
  1081.  
  1082.    If it succeeds, results are put in BUFP (if it returns an error, the
  1083.    contents of BUFP are undefined):
  1084.      `buffer' is the compiled pattern;
  1085.      `syntax' is set to SYNTAX;
  1086.      `used' is set to the length of the compiled pattern;
  1087.      `fastmap_accurate' is zero;
  1088.      `re_nsub' is the number of subexpressions in PATTERN;
  1089.      `not_bol' and `not_eol' are zero;
  1090.    
  1091.    The `fastmap' and `newline_anchor' fields are neither
  1092.    examined nor set.  */
  1093.  
  1094. static reg_errcode_t
  1095. regex_compile (pattern, size, syntax, bufp)
  1096.      const char *pattern;
  1097.      int size;
  1098.      reg_syntax_t syntax;
  1099.      struct re_pattern_buffer *bufp;
  1100. {
  1101.   /* We fetch characters from PATTERN here.  Even though PATTERN is
  1102.      `char *' (i.e., signed), we declare these variables as unsigned, so
  1103.      they can be reliably used as array indices.  */
  1104.   register unsigned char c, c1;
  1105.   
  1106.   /* A random tempory spot in PATTERN.  */
  1107.   const char *p1;
  1108.  
  1109.   /* Points to the end of the buffer, where we should append.  */
  1110.   register unsigned char *b;
  1111.   
  1112.   /* Keeps track of unclosed groups.  */
  1113.   compile_stack_type compile_stack;
  1114.  
  1115.   /* Points to the current (ending) position in the pattern.  */
  1116.   const char *p = pattern;
  1117.   const char *pend = pattern + size;
  1118.   
  1119.   /* How to translate the characters in the pattern.  */
  1120.   char *translate = bufp->translate;
  1121.  
  1122.   /* Address of the count-byte of the most recently inserted `exactn'
  1123.      command.  This makes it possible to tell if a new exact-match
  1124.      character can be added to that command or if the character requires
  1125.      a new `exactn' command.  */
  1126.   unsigned char *pending_exact = 0;
  1127.  
  1128.   /* Address of start of the most recently finished expression.
  1129.      This tells, e.g., postfix * where to find the start of its
  1130.      operand.  Reset at the beginning of groups and alternatives.  */
  1131.   unsigned char *laststart = 0;
  1132.  
  1133.   /* Address of beginning of regexp, or inside of last group.  */
  1134.   unsigned char *begalt;
  1135.  
  1136.   /* Place in the uncompiled pattern (i.e., the {) to
  1137.      which to go back if the interval is invalid.  */
  1138.   const char *beg_interval;
  1139.                 
  1140.   /* Address of the place where a forward jump should go to the end of
  1141.      the containing expression.  Each alternative of an `or' -- except the
  1142.      last -- ends with a forward jump of this sort.  */
  1143.   unsigned char *fixup_alt_jump = 0;
  1144.  
  1145.   /* Counts open-groups as they are encountered.  Remembered for the
  1146.      matching close-group on the compile stack, so the same register
  1147.      number is put in the stop_memory as the start_memory.  */
  1148.   regnum_t regnum = 0;
  1149.  
  1150. #ifdef DEBUG
  1151.   DEBUG_PRINT1 ("\nCompiling pattern: ");
  1152.   if (debug)
  1153.     {
  1154.       unsigned debug_count;
  1155.       
  1156.       for (debug_count = 0; debug_count < size; debug_count++)
  1157.         printchar (pattern[debug_count]);
  1158.       putchar ('\n');
  1159.     }
  1160. #endif /* DEBUG */
  1161.  
  1162.   /* Initialize the compile stack.  */
  1163.   compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  1164.   if (compile_stack.stack == NULL)
  1165.     return REG_ESPACE;
  1166.  
  1167.   compile_stack.size = INIT_COMPILE_STACK_SIZE;
  1168.   compile_stack.avail = 0;
  1169.  
  1170.   /* Initialize the pattern buffer.  */
  1171.   bufp->syntax = syntax;
  1172.   bufp->fastmap_accurate = 0;
  1173.   bufp->not_bol = bufp->not_eol = 0;
  1174.  
  1175.   /* Set `used' to zero, so that if we return an error, the pattern
  1176.      printer (for debugging) will think there's no pattern.  We reset it
  1177.      at the end.  */
  1178.   bufp->used = 0;
  1179.   
  1180.   /* Always count groups, whether or not bufp->no_sub is set.  */
  1181.   bufp->re_nsub = 0;                
  1182.  
  1183. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  1184.   /* Initialize the syntax table.  */
  1185.    init_syntax_once ();
  1186. #endif
  1187.  
  1188.   if (bufp->allocated == 0)
  1189.     {
  1190.       if (bufp->buffer)
  1191.     { /* If zero allocated, but buffer is non-null, try to realloc
  1192.              enough space.  This loses if buffer's address is bogus, but
  1193.              that is the user's responsibility.  */
  1194.           RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
  1195.         }
  1196.       else
  1197.         { /* Caller did not allocate a buffer.  Do it for them.  */
  1198.           bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
  1199.         }
  1200.       if (!bufp->buffer) return REG_ESPACE;
  1201.  
  1202.       bufp->allocated = INIT_BUF_SIZE;
  1203.     }
  1204.  
  1205.   begalt = b = bufp->buffer;
  1206.  
  1207.   /* Loop through the uncompiled pattern until we're at the end.  */
  1208.   while (p != pend)
  1209.     {
  1210.       PATFETCH (c);
  1211.  
  1212.       switch (c)
  1213.         {
  1214.         case '^':
  1215.           {
  1216.             if (   /* If at start of pattern, it's an operator.  */
  1217.                    p == pattern + 1
  1218.                    /* If context independent, it's an operator.  */
  1219.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1220.                    /* Otherwise, depends on what's come before.  */
  1221.                 || at_begline_loc_p (pattern, p, syntax))
  1222.               BUF_PUSH (begline);
  1223.             else
  1224.               goto normal_char;
  1225.           }
  1226.           break;
  1227.  
  1228.  
  1229.         case '$':
  1230.           {
  1231.             if (   /* If at end of pattern, it's an operator.  */
  1232.                    p == pend 
  1233.                    /* If context independent, it's an operator.  */
  1234.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1235.                    /* Otherwise, depends on what's next.  */
  1236.                 || at_endline_loc_p (p, pend, syntax))
  1237.                BUF_PUSH (endline);
  1238.              else
  1239.                goto normal_char;
  1240.            }
  1241.            break;
  1242.  
  1243.  
  1244.     case '+':
  1245.         case '?':
  1246.           if ((syntax & RE_BK_PLUS_QM)
  1247.               || (syntax & RE_LIMITED_OPS))
  1248.             goto normal_char;
  1249.         handle_plus:
  1250.         case '*':
  1251.           /* If there is no previous pattern... */
  1252.           if (!laststart)
  1253.             {
  1254.               if (syntax & RE_CONTEXT_INVALID_OPS)
  1255.                 return REG_BADRPT;
  1256.               else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  1257.                 goto normal_char;
  1258.             }
  1259.  
  1260.           {
  1261.             /* Are we optimizing this jump?  */
  1262.             boolean keep_string_p = false;
  1263.             
  1264.             /* 1 means zero (many) matches is allowed.  */
  1265.             char zero_times_ok = 0, many_times_ok = 0;
  1266.  
  1267.             /* If there is a sequence of repetition chars, collapse it
  1268.                down to just one (the right one).  We can't combine
  1269.                interval operators with these because of, e.g., `a{2}*',
  1270.                which should only match an even number of `a's.  */
  1271.  
  1272.             for (;;)
  1273.               {
  1274.                 zero_times_ok |= c != '+';
  1275.                 many_times_ok |= c != '?';
  1276.  
  1277.                 if (p == pend)
  1278.                   break;
  1279.  
  1280.                 PATFETCH (c);
  1281.  
  1282.                 if (c == '*'
  1283.                     || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  1284.                   ;
  1285.  
  1286.                 else if (syntax & RE_BK_PLUS_QM  &&  c == '\\')
  1287.                   {
  1288.                     if (p == pend) return REG_EESCAPE;
  1289.  
  1290.                     PATFETCH (c1);
  1291.                     if (!(c1 == '+' || c1 == '?'))
  1292.                       {
  1293.                         PATUNFETCH;
  1294.                         PATUNFETCH;
  1295.                         break;
  1296.                       }
  1297.  
  1298.                     c = c1;
  1299.                   }
  1300.                 else
  1301.                   {
  1302.                     PATUNFETCH;
  1303.                     break;
  1304.                   }
  1305.  
  1306.                 /* If we get here, we found another repeat character.  */
  1307.                }
  1308.  
  1309.             /* Star, etc. applied to an empty pattern is equivalent
  1310.                to an empty pattern.  */
  1311.             if (!laststart)  
  1312.               break;
  1313.  
  1314.             /* Now we know whether or not zero matches is allowed
  1315.                and also whether or not two or more matches is allowed.  */
  1316.             if (many_times_ok)
  1317.               { /* More than one repetition is allowed, so put in at the
  1318.                    end a backward relative jump from `b' to before the next
  1319.                    jump we're going to put in below (which jumps from
  1320.                    laststart to after this jump).  
  1321.  
  1322.                    But if we are at the `*' in the exact sequence `.*\n',
  1323.                    insert an unconditional jump backwards to the .,
  1324.                    instead of the beginning of the loop.  This way we only
  1325.                    push a failure point once, instead of every time
  1326.                    through the loop.  */
  1327.                 assert (p - 1 > pattern);
  1328.  
  1329.                 /* Allocate the space for the jump.  */
  1330.                 GET_BUFFER_SPACE (3);
  1331.  
  1332.                 /* We know we are not at the first character of the pattern,
  1333.                    because laststart was nonzero.  And we've already
  1334.                    incremented `p', by the way, to be the character after
  1335.                    the `*'.  Do we have to do something analogous here
  1336.                    for null bytes, because of RE_DOT_NOT_NULL?  */
  1337.                 if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
  1338.             && zero_times_ok
  1339.                     && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
  1340.                     && !(syntax & RE_DOT_NEWLINE))
  1341.                   { /* We have .*\n.  */
  1342.                     STORE_JUMP (jump, b, laststart);
  1343.                     keep_string_p = true;
  1344.                   }
  1345.                 else
  1346.                   /* Anything else.  */
  1347.                   STORE_JUMP (maybe_pop_jump, b, laststart - 3);
  1348.  
  1349.                 /* We've added more stuff to the buffer.  */
  1350.                 b += 3;
  1351.               }
  1352.  
  1353.             /* On failure, jump from laststart to b + 3, which will be the
  1354.                end of the buffer after this jump is inserted.  */
  1355.             GET_BUFFER_SPACE (3);
  1356.             INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
  1357.                                        : on_failure_jump,
  1358.                          laststart, b + 3);
  1359.             pending_exact = 0;
  1360.             b += 3;
  1361.  
  1362.             if (!zero_times_ok)
  1363.               {
  1364.                 /* At least one repetition is required, so insert a
  1365.                    `dummy_failure_jump' before the initial
  1366.                    `on_failure_jump' instruction of the loop. This
  1367.                    effects a skip over that instruction the first time
  1368.                    we hit that loop.  */
  1369.                 GET_BUFFER_SPACE (3);
  1370.                 INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
  1371.                 b += 3;
  1372.               }
  1373.             }
  1374.       break;
  1375.  
  1376.  
  1377.     case '.':
  1378.           laststart = b;
  1379.           BUF_PUSH (anychar);
  1380.           break;
  1381.  
  1382.  
  1383.         case '[':
  1384.           {
  1385.             boolean had_char_class = false;
  1386.  
  1387.             if (p == pend) return REG_EBRACK;
  1388.  
  1389.             /* Ensure that we have enough space to push a charset: the
  1390.                opcode, the length count, and the bitset; 34 bytes in all.  */
  1391.         GET_BUFFER_SPACE (34);
  1392.  
  1393.             laststart = b;
  1394.  
  1395.             /* We test `*p == '^' twice, instead of using an if
  1396.                statement, so we only need one BUF_PUSH.  */
  1397.             BUF_PUSH (*p == '^' ? charset_not : charset); 
  1398.             if (*p == '^')
  1399.               p++;
  1400.  
  1401.             /* Remember the first position in the bracket expression.  */
  1402.             p1 = p;
  1403.  
  1404.             /* Push the number of bytes in the bitmap.  */
  1405.             BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  1406.  
  1407.             /* Clear the whole map.  */
  1408.             bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  1409.  
  1410.             /* charset_not matches newline according to a syntax bit.  */
  1411.             if ((re_opcode_t) b[-2] == charset_not
  1412.                 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  1413.               SET_LIST_BIT ('\n');
  1414.  
  1415.             /* Read in characters and ranges, setting map bits.  */
  1416.             for (;;)
  1417.               {
  1418.                 if (p == pend) return REG_EBRACK;
  1419.  
  1420.                 PATFETCH (c);
  1421.  
  1422.                 /* \ might escape characters inside [...] and [^...].  */
  1423.                 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  1424.                   {
  1425.                     if (p == pend) return REG_EESCAPE;
  1426.  
  1427.                     PATFETCH (c1);
  1428.                     SET_LIST_BIT (c1);
  1429.                     continue;
  1430.                   }
  1431.  
  1432.                 /* Could be the end of the bracket expression.  If it's
  1433.                    not (i.e., when the bracket expression is `[]' so
  1434.                    far), the ']' character bit gets set way below.  */
  1435.                 if (c == ']' && p != p1 + 1)
  1436.                   break;
  1437.  
  1438.                 /* Look ahead to see if it's a range when the last thing
  1439.                    was a character class.  */
  1440.                 if (had_char_class && c == '-' && *p != ']')
  1441.                   return REG_ERANGE;
  1442.  
  1443.                 /* Look ahead to see if it's a range when the last thing
  1444.                    was a character: if this is a hyphen not at the
  1445.                    beginning or the end of a list, then it's the range
  1446.                    operator.  */
  1447.                 if (c == '-' 
  1448.                     && !(p - 2 >= pattern && p[-2] == '[') 
  1449.                     && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  1450.                     && *p != ']')
  1451.                   {
  1452.                     reg_errcode_t ret
  1453.                       = compile_range (&p, pend, translate, syntax, b);
  1454.                     if (ret != REG_NOERROR) return ret;
  1455.                   }
  1456.  
  1457.                 else if (p[0] == '-' && p[1] != ']')
  1458.                   { /* This handles ranges made up of characters only.  */
  1459.                     reg_errcode_t ret;
  1460.  
  1461.             /* Move past the `-'.  */
  1462.                     PATFETCH (c1);
  1463.                     
  1464.                     ret = compile_range (&p, pend, translate, syntax, b);
  1465.                     if (ret != REG_NOERROR) return ret;
  1466.                   }
  1467.  
  1468.                 /* See if we're at the beginning of a possible character
  1469.                    class.  */
  1470.  
  1471.                 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  1472.                   { /* Leave room for the null.  */
  1473.                     char str[CHAR_CLASS_MAX_LENGTH + 1];
  1474.  
  1475.                     PATFETCH (c);
  1476.                     c1 = 0;
  1477.  
  1478.                     /* If pattern is `[[:'.  */
  1479.                     if (p == pend) return REG_EBRACK;
  1480.  
  1481.                     for (;;)
  1482.                       {
  1483.                         PATFETCH (c);
  1484.                         if (c == ':' || c == ']' || p == pend
  1485.                             || c1 == CHAR_CLASS_MAX_LENGTH)
  1486.                           break;
  1487.                         str[c1++] = c;
  1488.                       }
  1489.                     str[c1] = '\0';
  1490.  
  1491.                     /* If isn't a word bracketed by `[:' and:`]':
  1492.                        undo the ending character, the letters, and leave 
  1493.                        the leading `:' and `[' (but set bits for them).  */
  1494.                     if (c == ':' && *p == ']')
  1495.                       {
  1496.                         int ch;
  1497.                         boolean is_alnum = STREQ (str, "alnum");
  1498.                         boolean is_alpha = STREQ (str, "alpha");
  1499.                         boolean is_blank = STREQ (str, "blank");
  1500.                         boolean is_cntrl = STREQ (str, "cntrl");
  1501.                         boolean is_digit = STREQ (str, "digit");
  1502.                         boolean is_graph = STREQ (str, "graph");
  1503.                         boolean is_lower = STREQ (str, "lower");
  1504.                         boolean is_print = STREQ (str, "print");
  1505.                         boolean is_punct = STREQ (str, "punct");
  1506.                         boolean is_space = STREQ (str, "space");
  1507.                         boolean is_upper = STREQ (str, "upper");
  1508.                         boolean is_xdigit = STREQ (str, "xdigit");
  1509.                         
  1510.                         if (!IS_CHAR_CLASS (str)) return REG_ECTYPE;
  1511.  
  1512.                         /* Throw away the ] at the end of the character
  1513.                            class.  */
  1514.                         PATFETCH (c);                    
  1515.  
  1516.                         if (p == pend) return REG_EBRACK;
  1517.  
  1518.                         for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  1519.                           {
  1520.                             if (   (is_alnum  && ISALNUM (ch))
  1521.                                 || (is_alpha  && ISALPHA (ch))
  1522.                                 || (is_blank  && ISBLANK (ch))
  1523.                                 || (is_cntrl  && ISCNTRL (ch))
  1524.                                 || (is_digit  && ISDIGIT (ch))
  1525.                                 || (is_graph  && ISGRAPH (ch))
  1526.                                 || (is_lower  && ISLOWER (ch))
  1527.                                 || (is_print  && ISPRINT (ch))
  1528.                                 || (is_punct  && ISPUNCT (ch))
  1529.                                 || (is_space  && ISSPACE (ch))
  1530.                                 || (is_upper  && ISUPPER (ch))
  1531.                                 || (is_xdigit && ISXDIGIT (ch)))
  1532.                             SET_LIST_BIT (ch);
  1533.                           }
  1534.                         had_char_class = true;
  1535.                       }
  1536.                     else
  1537.                       {
  1538.                         c1++;
  1539.                         while (c1--)    
  1540.                           PATUNFETCH;
  1541.                         SET_LIST_BIT ('[');
  1542.                         SET_LIST_BIT (':');
  1543.                         had_char_class = false;
  1544.                       }
  1545.                   }
  1546.                 else
  1547.                   {
  1548.                     had_char_class = false;
  1549.                     SET_LIST_BIT (c);
  1550.                   }
  1551.               }
  1552.  
  1553.             /* Discard any (non)matching list bytes that are all 0 at the
  1554.                end of the map.  Decrease the map-length byte too.  */
  1555.             while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  1556.               b[-1]--; 
  1557.             b += b[-1];
  1558.           }
  1559.           break;
  1560.  
  1561.  
  1562.     case '(':
  1563.           if (syntax & RE_NO_BK_PARENS)
  1564.             goto handle_open;
  1565.           else
  1566.             goto normal_char;
  1567.  
  1568.  
  1569.         case ')':
  1570.           if (syntax & RE_NO_BK_PARENS)
  1571.             goto handle_close;
  1572.           else
  1573.             goto normal_char;
  1574.  
  1575.  
  1576.         case '\n':
  1577.           if (syntax & RE_NEWLINE_ALT)
  1578.             goto handle_alt;
  1579.           else
  1580.             goto normal_char;
  1581.  
  1582.  
  1583.     case '|':
  1584.           if (syntax & RE_NO_BK_VBAR)
  1585.             goto handle_alt;
  1586.           else
  1587.             goto normal_char;
  1588.  
  1589.  
  1590.         case '{':
  1591.            if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
  1592.              goto handle_interval;
  1593.            else
  1594.              goto normal_char;
  1595.  
  1596.  
  1597.         case '\\':
  1598.           if (p == pend) return REG_EESCAPE;
  1599.  
  1600.           /* Do not translate the character after the \, so that we can
  1601.              distinguish, e.g., \B from \b, even if we normally would
  1602.              translate, e.g., B to b.  */
  1603.           PATFETCH_RAW (c);
  1604.  
  1605.           switch (c)
  1606.             {
  1607.             case '(':
  1608.               if (syntax & RE_NO_BK_PARENS)
  1609.                 goto normal_backslash;
  1610.  
  1611.             handle_open:
  1612.               bufp->re_nsub++;
  1613.               regnum++;
  1614.  
  1615.               if (COMPILE_STACK_FULL)
  1616.                 { 
  1617.                   RETALLOC (compile_stack.stack, compile_stack.size << 1,
  1618.                             compile_stack_elt_t);
  1619.                   if (compile_stack.stack == NULL) return REG_ESPACE;
  1620.  
  1621.                   compile_stack.size <<= 1;
  1622.                 }
  1623.  
  1624.               /* These are the values to restore when we hit end of this
  1625.                  group.  They are all relative offsets, so that if the
  1626.                  whole pattern moves because of realloc, they will still
  1627.                  be valid.  */
  1628.               COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
  1629.               COMPILE_STACK_TOP.fixup_alt_jump 
  1630.                 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
  1631.               COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
  1632.               COMPILE_STACK_TOP.regnum = regnum;
  1633.  
  1634.               /* We will eventually replace the 0 with the number of
  1635.                  groups inner to this one.  But do not push a
  1636.                  start_memory for groups beyond the last one we can
  1637.                  represent in the compiled pattern.  */
  1638.               if (regnum <= MAX_REGNUM)
  1639.                 {
  1640.                   COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
  1641.                   BUF_PUSH_3 (start_memory, regnum, 0);
  1642.                 }
  1643.                 
  1644.               compile_stack.avail++;
  1645.  
  1646.               fixup_alt_jump = 0;
  1647.               laststart = 0;
  1648.               begalt = b;
  1649.           /* If we've reached MAX_REGNUM groups, then this open
  1650.          won't actually generate any code, so we'll have to
  1651.          clear pending_exact explicitly.  */
  1652.           pending_exact = 0;
  1653.               break;
  1654.  
  1655.  
  1656.             case ')':
  1657.               if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  1658.  
  1659.               if (COMPILE_STACK_EMPTY)
  1660.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  1661.                   goto normal_backslash;
  1662.                 else
  1663.                   return REG_ERPAREN;
  1664.  
  1665.             handle_close:
  1666.               if (fixup_alt_jump)
  1667.                 { /* Push a dummy failure point at the end of the
  1668.                      alternative for a possible future
  1669.                      `pop_failure_jump' to pop.  See comments at
  1670.                      `push_dummy_failure' in `re_match_2'.  */
  1671.                   BUF_PUSH (push_dummy_failure);
  1672.                   
  1673.                   /* We allocated space for this jump when we assigned
  1674.                      to `fixup_alt_jump', in the `handle_alt' case below.  */
  1675.                   STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
  1676.                 }
  1677.  
  1678.               /* See similar code for backslashed left paren above.  */
  1679.               if (COMPILE_STACK_EMPTY)
  1680.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  1681.                   goto normal_char;
  1682.                 else
  1683.                   return REG_ERPAREN;
  1684.  
  1685.               /* Since we just checked for an empty stack above, this
  1686.                  ``can't happen''.  */
  1687.               assert (compile_stack.avail != 0);
  1688.               {
  1689.                 /* We don't just want to restore into `regnum', because
  1690.                    later groups should continue to be numbered higher,
  1691.                    as in `(ab)c(de)' -- the second group is #2.  */
  1692.                 regnum_t this_group_regnum;
  1693.  
  1694.                 compile_stack.avail--;        
  1695.                 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
  1696.                 fixup_alt_jump
  1697.                   = COMPILE_STACK_TOP.fixup_alt_jump
  1698.                     ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1 
  1699.                     : 0;
  1700.                 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
  1701.                 this_group_regnum = COMPILE_STACK_TOP.regnum;
  1702.         /* If we've reached MAX_REGNUM groups, then this open
  1703.            won't actually generate any code, so we'll have to
  1704.            clear pending_exact explicitly.  */
  1705.         pending_exact = 0;
  1706.  
  1707.                 /* We're at the end of the group, so now we know how many
  1708.                    groups were inside this one.  */
  1709.                 if (this_group_regnum <= MAX_REGNUM)
  1710.                   {
  1711.                     unsigned char *inner_group_loc
  1712.                       = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
  1713.                     
  1714.                     *inner_group_loc = regnum - this_group_regnum;
  1715.                     BUF_PUSH_3 (stop_memory, this_group_regnum,
  1716.                                 regnum - this_group_regnum);
  1717.                   }
  1718.               }
  1719.               break;
  1720.  
  1721.  
  1722.             case '|':                    /* `\|'.  */
  1723.               if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
  1724.                 goto normal_backslash;
  1725.             handle_alt:
  1726.               if (syntax & RE_LIMITED_OPS)
  1727.                 goto normal_char;
  1728.  
  1729.               /* Insert before the previous alternative a jump which
  1730.                  jumps to this alternative if the former fails.  */
  1731.               GET_BUFFER_SPACE (3);
  1732.               INSERT_JUMP (on_failure_jump, begalt, b + 6);
  1733.               pending_exact = 0;
  1734.               b += 3;
  1735.  
  1736.               /* The alternative before this one has a jump after it
  1737.                  which gets executed if it gets matched.  Adjust that
  1738.                  jump so it will jump to this alternative's analogous
  1739.                  jump (put in below, which in turn will jump to the next
  1740.                  (if any) alternative's such jump, etc.).  The last such
  1741.                  jump jumps to the correct final destination.  A picture:
  1742.                           _____ _____ 
  1743.                           |   | |   |   
  1744.                           |   v |   v 
  1745.                          a | b   | c   
  1746.  
  1747.                  If we are at `b', then fixup_alt_jump right now points to a
  1748.                  three-byte space after `a'.  We'll put in the jump, set
  1749.                  fixup_alt_jump to right after `b', and leave behind three
  1750.                  bytes which we'll fill in when we get to after `c'.  */
  1751.  
  1752.               if (fixup_alt_jump)
  1753.                 STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  1754.  
  1755.               /* Mark and leave space for a jump after this alternative,
  1756.                  to be filled in later either by next alternative or
  1757.                  when know we're at the end of a series of alternatives.  */
  1758.               fixup_alt_jump = b;
  1759.               GET_BUFFER_SPACE (3);
  1760.               b += 3;
  1761.  
  1762.               laststart = 0;
  1763.               begalt = b;
  1764.               break;
  1765.  
  1766.  
  1767.             case '{': 
  1768.               /* If \{ is a literal.  */
  1769.               if (!(syntax & RE_INTERVALS)
  1770.                      /* If we're at `\{' and it's not the open-interval 
  1771.                         operator.  */
  1772.                   || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1773.                   || (p - 2 == pattern  &&  p == pend))
  1774.                 goto normal_backslash;
  1775.  
  1776.             handle_interval:
  1777.               {
  1778.                 /* If got here, then the syntax allows intervals.  */
  1779.  
  1780.                 /* At least (most) this many matches must be made.  */
  1781.                 int lower_bound = -1, upper_bound = -1;
  1782.  
  1783.                 beg_interval = p - 1;
  1784.  
  1785.                 if (p == pend)
  1786.                   {
  1787.                     if (syntax & RE_NO_BK_BRACES)
  1788.                       goto unfetch_interval;
  1789.                     else
  1790.                       return REG_EBRACE;
  1791.                   }
  1792.  
  1793.                 GET_UNSIGNED_NUMBER (lower_bound);
  1794.  
  1795.                 if (c == ',')
  1796.                   {
  1797.                     GET_UNSIGNED_NUMBER (upper_bound);
  1798.                     if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  1799.                   }
  1800.                 else
  1801.                   /* Interval such as `{1}' => match exactly once. */
  1802.                   upper_bound = lower_bound;
  1803.  
  1804.                 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  1805.                     || lower_bound > upper_bound)
  1806.                   {
  1807.                     if (syntax & RE_NO_BK_BRACES)
  1808.                       goto unfetch_interval;
  1809.                     else 
  1810.                       return REG_BADBR;
  1811.                   }
  1812.  
  1813.                 if (!(syntax & RE_NO_BK_BRACES)) 
  1814.                   {
  1815.                     if (c != '\\') return REG_EBRACE;
  1816.  
  1817.                     PATFETCH (c);
  1818.                   }
  1819.  
  1820.                 if (c != '}')
  1821.                   {
  1822.                     if (syntax & RE_NO_BK_BRACES)
  1823.                       goto unfetch_interval;
  1824.                     else 
  1825.                       return REG_BADBR;
  1826.                   }
  1827.  
  1828.                 /* We just parsed a valid interval.  */
  1829.  
  1830.                 /* If it's invalid to have no preceding re.  */
  1831.                 if (!laststart)
  1832.                   {
  1833.                     if (syntax & RE_CONTEXT_INVALID_OPS)
  1834.                       return REG_BADRPT;
  1835.                     else if (syntax & RE_CONTEXT_INDEP_OPS)
  1836.                       laststart = b;
  1837.                     else
  1838.                       goto unfetch_interval;
  1839.                   }
  1840.  
  1841.                 /* If the upper bound is zero, don't want to succeed at
  1842.                    all; jump from `laststart' to `b + 3', which will be
  1843.                    the end of the buffer after we insert the jump.  */
  1844.                  if (upper_bound == 0)
  1845.                    {
  1846.                      GET_BUFFER_SPACE (3);
  1847.                      INSERT_JUMP (jump, laststart, b + 3);
  1848.                      b += 3;
  1849.                    }
  1850.  
  1851.                  /* Otherwise, we have a nontrivial interval.  When
  1852.                     we're all done, the pattern will look like:
  1853.                       set_number_at <jump count> <upper bound>
  1854.                       set_number_at <succeed_n count> <lower bound>
  1855.                       succeed_n <after jump addr> <succed_n count>
  1856.                       <body of loop>
  1857.                       jump_n <succeed_n addr> <jump count>
  1858.                     (The upper bound and `jump_n' are omitted if
  1859.                     `upper_bound' is 1, though.)  */
  1860.                  else 
  1861.                    { /* If the upper bound is > 1, we need to insert
  1862.                         more at the end of the loop.  */
  1863.                      unsigned nbytes = 10 + (upper_bound > 1) * 10;
  1864.  
  1865.                      GET_BUFFER_SPACE (nbytes);
  1866.  
  1867.                      /* Initialize lower bound of the `succeed_n', even
  1868.                         though it will be set during matching by its
  1869.                         attendant `set_number_at' (inserted next),
  1870.                         because `re_compile_fastmap' needs to know.
  1871.                         Jump to the `jump_n' we might insert below.  */
  1872.                      INSERT_JUMP2 (succeed_n, laststart,
  1873.                                    b + 5 + (upper_bound > 1) * 5,
  1874.                                    lower_bound);
  1875.                      b += 5;
  1876.  
  1877.                      /* Code to initialize the lower bound.  Insert 
  1878.                         before the `succeed_n'.  The `5' is the last two
  1879.                         bytes of this `set_number_at', plus 3 bytes of
  1880.                         the following `succeed_n'.  */
  1881.                      insert_op2 (set_number_at, laststart, 5, lower_bound, b);
  1882.                      b += 5;
  1883.  
  1884.                      if (upper_bound > 1)
  1885.                        { /* More than one repetition is allowed, so
  1886.                             append a backward jump to the `succeed_n'
  1887.                             that starts this interval.
  1888.                             
  1889.                             When we've reached this during matching,
  1890.                             we'll have matched the interval once, so
  1891.                             jump back only `upper_bound - 1' times.  */
  1892.                          STORE_JUMP2 (jump_n, b, laststart + 5,
  1893.                                       upper_bound - 1);
  1894.                          b += 5;
  1895.  
  1896.                          /* The location we want to set is the second
  1897.                             parameter of the `jump_n'; that is `b-2' as
  1898.                             an absolute address.  `laststart' will be
  1899.                             the `set_number_at' we're about to insert;
  1900.                             `laststart+3' the number to set, the source
  1901.                             for the relative address.  But we are
  1902.                             inserting into the middle of the pattern --
  1903.                             so everything is getting moved up by 5.
  1904.                             Conclusion: (b - 2) - (laststart + 3) + 5,
  1905.                             i.e., b - laststart.
  1906.                             
  1907.                             We insert this at the beginning of the loop
  1908.                             so that if we fail during matching, we'll
  1909.                             reinitialize the bounds.  */
  1910.                          insert_op2 (set_number_at, laststart, b - laststart,
  1911.                                      upper_bound - 1, b);
  1912.                          b += 5;
  1913.                        }
  1914.                    }
  1915.                 pending_exact = 0;
  1916.                 beg_interval = NULL;
  1917.               }
  1918.               break;
  1919.  
  1920.             unfetch_interval:
  1921.               /* If an invalid interval, match the characters as literals.  */
  1922.                assert (beg_interval);
  1923.                p = beg_interval;
  1924.                beg_interval = NULL;
  1925.  
  1926.                /* normal_char and normal_backslash need `c'.  */
  1927.                PATFETCH (c);    
  1928.  
  1929.                if (!(syntax & RE_NO_BK_BRACES))
  1930.                  {
  1931.                    if (p > pattern  &&  p[-1] == '\\')
  1932.                      goto normal_backslash;
  1933.                  }
  1934.                goto normal_char;
  1935.  
  1936. #ifdef emacs
  1937.             /* There is no way to specify the before_dot and after_dot
  1938.                operators.  rms says this is ok.  --karl  */
  1939.             case '=':
  1940.               BUF_PUSH (at_dot);
  1941.               break;
  1942.  
  1943.             case 's':    
  1944.               laststart = b;
  1945.               PATFETCH (c);
  1946.               BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
  1947.               break;
  1948.  
  1949.             case 'S':
  1950.               laststart = b;
  1951.               PATFETCH (c);
  1952.               BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
  1953.               break;
  1954. #endif /* emacs */
  1955.  
  1956.  
  1957.             case 'w':
  1958.               laststart = b;
  1959.               BUF_PUSH (wordchar);
  1960.               break;
  1961.  
  1962.  
  1963.             case 'W':
  1964.               laststart = b;
  1965.               BUF_PUSH (notwordchar);
  1966.               break;
  1967.  
  1968.  
  1969.             case '<':
  1970.               BUF_PUSH (wordbeg);
  1971.               break;
  1972.  
  1973.             case '>':
  1974.               BUF_PUSH (wordend);
  1975.               break;
  1976.  
  1977.             case 'b':
  1978.               BUF_PUSH (wordbound);
  1979.               break;
  1980.  
  1981.             case 'B':
  1982.               BUF_PUSH (notwordbound);
  1983.               break;
  1984.  
  1985.             case '`':
  1986.               BUF_PUSH (begbuf);
  1987.               break;
  1988.  
  1989.             case '\'':
  1990.               BUF_PUSH (endbuf);
  1991.               break;
  1992.  
  1993.             case '1': case '2': case '3': case '4': case '5':
  1994.             case '6': case '7': case '8': case '9':
  1995.               if (syntax & RE_NO_BK_REFS)
  1996.                 goto normal_char;
  1997.  
  1998.               c1 = c - '0';
  1999.  
  2000.               if (c1 > regnum)
  2001.                 return REG_ESUBREG;
  2002.  
  2003.               /* Can't back reference to a subexpression if inside of it.  */
  2004.               if (group_in_compile_stack (compile_stack, c1))
  2005.                 goto normal_char;
  2006.  
  2007.               laststart = b;
  2008.               BUF_PUSH_2 (duplicate, c1);
  2009.               break;
  2010.  
  2011.  
  2012.             case '+':
  2013.             case '?':
  2014.               if (syntax & RE_BK_PLUS_QM)
  2015.                 goto handle_plus;
  2016.               else
  2017.                 goto normal_backslash;
  2018.  
  2019.             default:
  2020.             normal_backslash:
  2021.               /* You might think it would be useful for \ to mean
  2022.                  not to translate; but if we don't translate it
  2023.                  it will never match anything.  */
  2024.               c = TRANSLATE (c);
  2025.               goto normal_char;
  2026.             }
  2027.           break;
  2028.  
  2029.  
  2030.     default:
  2031.         /* Expects the character in `c'.  */
  2032.     normal_char:
  2033.           /* If no exactn currently being built.  */
  2034.           if (!pending_exact 
  2035.  
  2036.               /* If last exactn not at current position.  */
  2037.               || pending_exact + *pending_exact + 1 != b
  2038.               
  2039.               /* We have only one byte following the exactn for the count.  */
  2040.           || *pending_exact == (1 << BYTEWIDTH) - 1
  2041.  
  2042.               /* If followed by a repetition operator.  */
  2043.               || *p == '*' || *p == '^'
  2044.           || ((syntax & RE_BK_PLUS_QM)
  2045.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  2046.           : (*p == '+' || *p == '?'))
  2047.           || ((syntax & RE_INTERVALS)
  2048.                   && ((syntax & RE_NO_BK_BRACES)
  2049.               ? *p == '{'
  2050.                       : (p[0] == '\\' && p[1] == '{'))))
  2051.         {
  2052.           /* Start building a new exactn.  */
  2053.               
  2054.               laststart = b;
  2055.  
  2056.           BUF_PUSH_2 (exactn, 0);
  2057.           pending_exact = b - 1;
  2058.             }
  2059.             
  2060.       BUF_PUSH (c);
  2061.           (*pending_exact)++;
  2062.       break;
  2063.         } /* switch (c) */
  2064.     } /* while p != pend */
  2065.  
  2066.   
  2067.   /* Through the pattern now.  */
  2068.   
  2069.   if (fixup_alt_jump)
  2070.     STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2071.  
  2072.   if (!COMPILE_STACK_EMPTY) 
  2073.     return REG_EPAREN;
  2074.  
  2075.   free (compile_stack.stack);
  2076.  
  2077.   /* We have succeeded; set the length of the buffer.  */
  2078.   bufp->used = b - bufp->buffer;
  2079.  
  2080. #ifdef DEBUG
  2081.   if (debug)
  2082.     {
  2083.       DEBUG_PRINT1 ("\nCompiled pattern: \n");
  2084.       print_compiled_pattern (bufp);
  2085.     }
  2086. #endif /* DEBUG */
  2087.  
  2088.   return REG_NOERROR;
  2089. } /* regex_compile */
  2090.  
  2091. /* Subroutines for `regex_compile'.  */
  2092.  
  2093. /* Store OP at LOC followed by two-byte integer parameter ARG.  */
  2094.  
  2095. static void
  2096. store_op1 (op, loc, arg)
  2097.     re_opcode_t op;
  2098.     unsigned char *loc;
  2099.     int arg;
  2100. {
  2101.   *loc = (unsigned char) op;
  2102.   STORE_NUMBER (loc + 1, arg);
  2103. }
  2104.  
  2105.  
  2106. /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2.  */
  2107.  
  2108. static void
  2109. store_op2 (op, loc, arg1, arg2)
  2110.     re_opcode_t op;
  2111.     unsigned char *loc;
  2112.     int arg1, arg2;
  2113. {
  2114.   *loc = (unsigned char) op;
  2115.   STORE_NUMBER (loc + 1, arg1);
  2116.   STORE_NUMBER (loc + 3, arg2);
  2117. }
  2118.  
  2119.  
  2120. /* Copy the bytes from LOC to END to open up three bytes of space at LOC
  2121.    for OP followed by two-byte integer parameter ARG.  */
  2122.  
  2123. static void
  2124. insert_op1 (op, loc, arg, end)
  2125.     re_opcode_t op;
  2126.     unsigned char *loc;
  2127.     int arg;
  2128.     unsigned char *end;    
  2129. {
  2130.   register unsigned char *pfrom = end;
  2131.   register unsigned char *pto = end + 3;
  2132.  
  2133.   while (pfrom != loc)
  2134.     *--pto = *--pfrom;
  2135.     
  2136.   store_op1 (op, loc, arg);
  2137. }
  2138.  
  2139.  
  2140. /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2.  */
  2141.  
  2142. static void
  2143. insert_op2 (op, loc, arg1, arg2, end)
  2144.     re_opcode_t op;
  2145.     unsigned char *loc;
  2146.     int arg1, arg2;
  2147.     unsigned char *end;    
  2148. {
  2149.   register unsigned char *pfrom = end;
  2150.   register unsigned char *pto = end + 5;
  2151.  
  2152.   while (pfrom != loc)
  2153.     *--pto = *--pfrom;
  2154.     
  2155.   store_op2 (op, loc, arg1, arg2);
  2156. }
  2157.  
  2158.  
  2159. /* P points to just after a ^ in PATTERN.  Return true if that ^ comes
  2160.    after an alternative or a begin-subexpression.  We assume there is at
  2161.    least one character before the ^.  */
  2162.  
  2163. static boolean
  2164. at_begline_loc_p (pattern, p, syntax)
  2165.     const char *pattern, *p;
  2166.     reg_syntax_t syntax;
  2167. {
  2168.   const char *prev = p - 2;
  2169.   boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
  2170.   
  2171.   return
  2172.        /* After a subexpression?  */
  2173.        (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
  2174.        /* After an alternative?  */
  2175.     || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
  2176. }
  2177.  
  2178.  
  2179. /* The dual of at_begline_loc_p.  This one is for $.  We assume there is
  2180.    at least one character after the $, i.e., `P < PEND'.  */
  2181.  
  2182. static boolean
  2183. at_endline_loc_p (p, pend, syntax)
  2184.     const char *p, *pend;
  2185.     int syntax;
  2186. {
  2187.   const char *next = p;
  2188.   boolean next_backslash = *next == '\\';
  2189.   const char *next_next = p + 1 < pend ? p + 1 : NULL;
  2190.   
  2191.   return
  2192.        /* Before a subexpression?  */
  2193.        (syntax & RE_NO_BK_PARENS ? *next == ')'
  2194.         : next_backslash && next_next && *next_next == ')')
  2195.        /* Before an alternative?  */
  2196.     || (syntax & RE_NO_BK_VBAR ? *next == '|'
  2197.         : next_backslash && next_next && *next_next == '|');
  2198. }
  2199.  
  2200.  
  2201. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and 
  2202.    false if it's not.  */
  2203.  
  2204. static boolean
  2205. group_in_compile_stack (compile_stack, regnum)
  2206.     compile_stack_type compile_stack;
  2207.     regnum_t regnum;
  2208. {
  2209.   int this_element;
  2210.  
  2211.   for (this_element = compile_stack.avail - 1;  
  2212.        this_element >= 0; 
  2213.        this_element--)
  2214.     if (compile_stack.stack[this_element].regnum == regnum)
  2215.       return true;
  2216.  
  2217.   return false;
  2218. }
  2219.  
  2220.  
  2221. /* Read the ending character of a range (in a bracket expression) from the
  2222.    uncompiled pattern *P_PTR (which ends at PEND).  We assume the
  2223.    starting character is in `P[-2]'.  (`P[-1]' is the character `-'.)
  2224.    Then we set the translation of all bits between the starting and
  2225.    ending characters (inclusive) in the compiled pattern B.
  2226.    
  2227.    Return an error code.
  2228.    
  2229.    We use these short variable names so we can use the same macros as
  2230.    `regex_compile' itself.  */
  2231.  
  2232. static reg_errcode_t
  2233. compile_range (p_ptr, pend, translate, syntax, b)
  2234.     const char **p_ptr, *pend;
  2235.     char *translate;
  2236.     reg_syntax_t syntax;
  2237.     unsigned char *b;
  2238. {
  2239.   unsigned this_char;
  2240.  
  2241.   const char *p = *p_ptr;
  2242.   int range_start, range_end;
  2243.   
  2244.   if (p == pend)
  2245.     return REG_ERANGE;
  2246.  
  2247.   /* Even though the pattern is a signed `char *', we need to fetch
  2248.      with unsigned char *'s; if the high bit of the pattern character
  2249.      is set, the range endpoints will be negative if we fetch using a
  2250.      signed char *.
  2251.  
  2252.      We also want to fetch the endpoints without translating them; the 
  2253.      appropriate translation is done in the bit-setting loop below.  */
  2254.   range_start = ((unsigned char *) p)[-2];
  2255.   range_end   = ((unsigned char *) p)[0];
  2256.  
  2257.   /* Have to increment the pointer into the pattern string, so the
  2258.      caller isn't still at the ending character.  */
  2259.   (*p_ptr)++;
  2260.  
  2261.   /* If the start is after the end, the range is empty.  */
  2262.   if (range_start > range_end)
  2263.     return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
  2264.  
  2265.   /* Here we see why `this_char' has to be larger than an `unsigned
  2266.      char' -- the range is inclusive, so if `range_end' == 0xff
  2267.      (assuming 8-bit characters), we would otherwise go into an infinite
  2268.      loop, since all characters <= 0xff.  */
  2269.   for (this_char = range_start; this_char <= range_end; this_char++)
  2270.     {
  2271.       SET_LIST_BIT (TRANSLATE (this_char));
  2272.     }
  2273.   
  2274.   return REG_NOERROR;
  2275. }
  2276.  
  2277. /* Failure stack declarations and macros; both re_compile_fastmap and
  2278.    re_match_2 use a failure stack.  These have to be macros because of
  2279.    REGEX_ALLOCATE.  */
  2280.    
  2281.  
  2282. /* Number of failure points for which to initially allocate space
  2283.    when matching.  If this number is exceeded, we allocate more
  2284.    space, so it is not a hard limit.  */
  2285. #ifndef INIT_FAILURE_ALLOC
  2286. #define INIT_FAILURE_ALLOC 5
  2287. #endif
  2288.  
  2289. /* Roughly the maximum number of failure points on the stack.  Would be
  2290.    exactly that if always used MAX_FAILURE_SPACE each time we failed.
  2291.    This is a variable only so users of regex can assign to it; we never
  2292.    change it ourselves.  */
  2293. int re_max_failures = 2000;
  2294.  
  2295. typedef const unsigned char *fail_stack_elt_t;
  2296.  
  2297. typedef struct
  2298. {
  2299.   fail_stack_elt_t *stack;
  2300.   unsigned size;
  2301.   unsigned avail;            /* Offset of next open position.  */
  2302. } fail_stack_type;
  2303.  
  2304. #define FAIL_STACK_EMPTY()     (fail_stack.avail == 0)
  2305. #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
  2306. #define FAIL_STACK_FULL()      (fail_stack.avail == fail_stack.size)
  2307. #define FAIL_STACK_TOP()       (fail_stack.stack[fail_stack.avail])
  2308.  
  2309.  
  2310. /* Initialize `fail_stack'.  Do `return -2' if the alloc fails.  */
  2311.  
  2312. #define INIT_FAIL_STACK()                        \
  2313.   do {                                    \
  2314.     fail_stack.stack = (fail_stack_elt_t *)                \
  2315.       REGEX_ALLOCATE (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t));    \
  2316.                                     \
  2317.     if (fail_stack.stack == NULL)                    \
  2318.       return -2;                            \
  2319.                                     \
  2320.     fail_stack.size = INIT_FAILURE_ALLOC;                \
  2321.     fail_stack.avail = 0;                        \
  2322.   } while (0)
  2323.  
  2324.  
  2325. /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
  2326.  
  2327.    Return 1 if succeeds, and 0 if either ran out of memory
  2328.    allocating space for it or it was already too large.  
  2329.    
  2330.    REGEX_REALLOCATE requires `destination' be declared.   */
  2331.  
  2332. #define DOUBLE_FAIL_STACK(fail_stack)                    \
  2333.   ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS        \
  2334.    ? 0                                    \
  2335.    : ((fail_stack).stack = (fail_stack_elt_t *)                \
  2336.         REGEX_REALLOCATE ((fail_stack).stack,                 \
  2337.           (fail_stack).size * sizeof (fail_stack_elt_t),        \
  2338.           ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)),    \
  2339.                                     \
  2340.       (fail_stack).stack == NULL                    \
  2341.       ? 0                                \
  2342.       : ((fail_stack).size <<= 1,                     \
  2343.          1)))
  2344.  
  2345.  
  2346. /* Push PATTERN_OP on FAIL_STACK. 
  2347.  
  2348.    Return 1 if was able to do so and 0 if ran out of memory allocating
  2349.    space to do so.  */
  2350. #define PUSH_PATTERN_OP(pattern_op, fail_stack)                \
  2351.   ((FAIL_STACK_FULL ()                            \
  2352.     && !DOUBLE_FAIL_STACK (fail_stack))                    \
  2353.     ? 0                                    \
  2354.     : ((fail_stack).stack[(fail_stack).avail++] = pattern_op,        \
  2355.        1))
  2356.  
  2357. /* This pushes an item onto the failure stack.  Must be a four-byte
  2358.    value.  Assumes the variable `fail_stack'.  Probably should only
  2359.    be called from within `PUSH_FAILURE_POINT'.  */
  2360. #define PUSH_FAILURE_ITEM(item)                        \
  2361.   fail_stack.stack[fail_stack.avail++] = (fail_stack_elt_t) item
  2362.  
  2363. /* The complement operation.  Assumes `fail_stack' is nonempty.  */
  2364. #define POP_FAILURE_ITEM() fail_stack.stack[--fail_stack.avail]
  2365.  
  2366. /* Used to omit pushing failure point id's when we're not debugging.  */
  2367. #ifdef DEBUG
  2368. #define DEBUG_PUSH PUSH_FAILURE_ITEM
  2369. #define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_ITEM ()
  2370. #else
  2371. #define DEBUG_PUSH(item)
  2372. #define DEBUG_POP(item_addr)
  2373. #endif
  2374.  
  2375.  
  2376. /* Push the information about the state we will need
  2377.    if we ever fail back to it.  
  2378.    
  2379.    Requires variables fail_stack, regstart, regend, reg_info, and
  2380.    num_regs be declared.  DOUBLE_FAIL_STACK requires `destination' be
  2381.    declared.
  2382.    
  2383.    Does `return FAILURE_CODE' if runs out of memory.  */
  2384.  
  2385. #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code)    \
  2386.   do {                                    \
  2387.     char *destination;                            \
  2388.     /* Must be int, so when we don't save any registers, the arithmetic    \
  2389.        of 0 + -1 isn't done as unsigned.  */                \
  2390.     int this_reg;                            \
  2391.                                         \
  2392.     DEBUG_STATEMENT (failure_id++);                    \
  2393.     DEBUG_STATEMENT (nfailure_points_pushed++);                \
  2394.     DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id);        \
  2395.     DEBUG_PRINT2 ("  Before push, next avail: %d\n", (fail_stack).avail);\
  2396.     DEBUG_PRINT2 ("                     size: %d\n", (fail_stack).size);\
  2397.                                     \
  2398.     DEBUG_PRINT2 ("  slots needed: %d\n", NUM_FAILURE_ITEMS);        \
  2399.     DEBUG_PRINT2 ("     available: %d\n", REMAINING_AVAIL_SLOTS);    \
  2400.                                     \
  2401.     /* Ensure we have enough space allocated for what we will push.  */    \
  2402.     while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS)            \
  2403.       {                                    \
  2404.         if (!DOUBLE_FAIL_STACK (fail_stack))            \
  2405.           return failure_code;                        \
  2406.                                     \
  2407.         DEBUG_PRINT2 ("\n  Doubled stack; size now: %d\n",        \
  2408.                (fail_stack).size);                \
  2409.         DEBUG_PRINT2 ("  slots available: %d\n", REMAINING_AVAIL_SLOTS);\
  2410.       }                                    \
  2411.                                     \
  2412.     /* Push the info, starting with the registers.  */            \
  2413.     DEBUG_PRINT1 ("\n");                        \
  2414.                                     \
  2415.     for (this_reg = lowest_active_reg; this_reg <= highest_active_reg;    \
  2416.          this_reg++)                            \
  2417.       {                                    \
  2418.     DEBUG_PRINT2 ("  Pushing reg: %d\n", this_reg);            \
  2419.         DEBUG_STATEMENT (num_regs_pushed++);                \
  2420.                                     \
  2421.     DEBUG_PRINT2 ("    start: 0x%x\n", regstart[this_reg]);        \
  2422.         PUSH_FAILURE_ITEM (regstart[this_reg]);                \
  2423.                                                                         \
  2424.     DEBUG_PRINT2 ("    end: 0x%x\n", regend[this_reg]);        \
  2425.         PUSH_FAILURE_ITEM (regend[this_reg]);                \
  2426.                                     \
  2427.     DEBUG_PRINT2 ("    info: 0x%x\n      ", reg_info[this_reg]);    \
  2428.         DEBUG_PRINT2 (" match_null=%d",                    \
  2429.                       REG_MATCH_NULL_STRING_P (reg_info[this_reg]));    \
  2430.         DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg]));    \
  2431.         DEBUG_PRINT2 (" matched_something=%d",                \
  2432.                       MATCHED_SOMETHING (reg_info[this_reg]));        \
  2433.         DEBUG_PRINT2 (" ever_matched=%d",                \
  2434.                       EVER_MATCHED_SOMETHING (reg_info[this_reg]));    \
  2435.     DEBUG_PRINT1 ("\n");                        \
  2436.         PUSH_FAILURE_ITEM (reg_info[this_reg].word);            \
  2437.       }                                    \
  2438.                                     \
  2439.     DEBUG_PRINT2 ("  Pushing  low active reg: %d\n", lowest_active_reg);\
  2440.     PUSH_FAILURE_ITEM (lowest_active_reg);                \
  2441.                                     \
  2442.     DEBUG_PRINT2 ("  Pushing high active reg: %d\n", highest_active_reg);\
  2443.     PUSH_FAILURE_ITEM (highest_active_reg);                \
  2444.                                     \
  2445.     DEBUG_PRINT2 ("  Pushing pattern 0x%x: ", pattern_place);        \
  2446.     DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend);        \
  2447.     PUSH_FAILURE_ITEM (pattern_place);                    \
  2448.                                     \
  2449.     DEBUG_PRINT2 ("  Pushing string 0x%x: `", string_place);        \
  2450.     DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2,   \
  2451.                  size2);                \
  2452.     DEBUG_PRINT1 ("'\n");                        \
  2453.     PUSH_FAILURE_ITEM (string_place);                    \
  2454.                                     \
  2455.     DEBUG_PRINT2 ("  Pushing failure id: %u\n", failure_id);        \
  2456.     DEBUG_PUSH (failure_id);                        \
  2457.   } while (0)
  2458.  
  2459. /* This is the number of items that are pushed and popped on the stack
  2460.    for each register.  */
  2461. #define NUM_REG_ITEMS  3
  2462.  
  2463. /* Individual items aside from the registers.  */
  2464. #ifdef DEBUG
  2465. #define NUM_NONREG_ITEMS 5 /* Includes failure point id.  */
  2466. #else
  2467. #define NUM_NONREG_ITEMS 4
  2468. #endif
  2469.  
  2470. /* We push at most this many items on the stack.  */
  2471. #define MAX_FAILURE_ITEMS ((num_regs - 1) * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
  2472.  
  2473. /* We actually push this many items.  */
  2474. #define NUM_FAILURE_ITEMS                        \
  2475.   ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS     \
  2476.     + NUM_NONREG_ITEMS)
  2477.  
  2478. /* How many items can still be added to the stack without overflowing it.  */
  2479. #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
  2480.  
  2481.  
  2482. /* Pops what PUSH_FAIL_STACK pushes.
  2483.  
  2484.    We restore into the parameters, all of which should be lvalues:
  2485.      STR -- the saved data position.
  2486.      PAT -- the saved pattern position.
  2487.      LOW_REG, HIGH_REG -- the highest and lowest active registers.
  2488.      REGSTART, REGEND -- arrays of string positions.
  2489.      REG_INFO -- array of information about each subexpression.
  2490.    
  2491.    Also assumes the variables `fail_stack' and (if debugging), `bufp',
  2492.    `pend', `string1', `size1', `string2', and `size2'.  */
  2493.  
  2494. #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
  2495. {                                    \
  2496.   DEBUG_STATEMENT (fail_stack_elt_t failure_id;)            \
  2497.   int this_reg;                                \
  2498.   const unsigned char *string_temp;                    \
  2499.                                     \
  2500.   assert (!FAIL_STACK_EMPTY ());                    \
  2501.                                     \
  2502.   /* Remove failure points and point to how many regs pushed.  */    \
  2503.   DEBUG_PRINT1 ("POP_FAILURE_POINT:\n");                \
  2504.   DEBUG_PRINT2 ("  Before pop, next avail: %d\n", fail_stack.avail);    \
  2505.   DEBUG_PRINT2 ("                    size: %d\n", fail_stack.size);    \
  2506.                                     \
  2507.   assert (fail_stack.avail >= NUM_NONREG_ITEMS);            \
  2508.                                     \
  2509.   DEBUG_POP (&failure_id);                        \
  2510.   DEBUG_PRINT2 ("  Popping failure id: %u\n", failure_id);        \
  2511.                                     \
  2512.   /* If the saved string location is NULL, it came from an        \
  2513.      on_failure_keep_string_jump opcode, and we want to throw away the    \
  2514.      saved NULL, thus retaining our current position in the string.  */    \
  2515.   string_temp = POP_FAILURE_ITEM ();                    \
  2516.   if (string_temp != NULL)                        \
  2517.     str = (const char *) string_temp;                    \
  2518.                                     \
  2519.   DEBUG_PRINT2 ("  Popping string 0x%x: `", str);            \
  2520.   DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2);    \
  2521.   DEBUG_PRINT1 ("'\n");                            \
  2522.                                     \
  2523.   pat = (unsigned char *) POP_FAILURE_ITEM ();                \
  2524.   DEBUG_PRINT2 ("  Popping pattern 0x%x: ", pat);            \
  2525.   DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend);            \
  2526.                                     \
  2527.   /* Restore register info.  */                        \
  2528.   high_reg = (unsigned) POP_FAILURE_ITEM ();                \
  2529.   DEBUG_PRINT2 ("  Popping high active reg: %d\n", high_reg);        \
  2530.                                     \
  2531.   low_reg = (unsigned) POP_FAILURE_ITEM ();                \
  2532.   DEBUG_PRINT2 ("  Popping  low active reg: %d\n", low_reg);        \
  2533.                                     \
  2534.   for (this_reg = high_reg; this_reg >= low_reg; this_reg--)        \
  2535.     {                                    \
  2536.       DEBUG_PRINT2 ("    Popping reg: %d\n", this_reg);            \
  2537.                                     \
  2538.       reg_info[this_reg].word = POP_FAILURE_ITEM ();            \
  2539.       DEBUG_PRINT2 ("      info: 0x%x\n", reg_info[this_reg]);        \
  2540.                                     \
  2541.       regend[this_reg] = (const char *) POP_FAILURE_ITEM ();        \
  2542.       DEBUG_PRINT2 ("      end: 0x%x\n", regend[this_reg]);        \
  2543.                                     \
  2544.       regstart[this_reg] = (const char *) POP_FAILURE_ITEM ();        \
  2545.       DEBUG_PRINT2 ("      start: 0x%x\n", regstart[this_reg]);        \
  2546.     }                                    \
  2547.                                     \
  2548.   DEBUG_STATEMENT (nfailure_points_popped++);                \
  2549. } /* POP_FAILURE_POINT */
  2550.  
  2551. /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
  2552.    BUFP.  A fastmap records which of the (1 << BYTEWIDTH) possible
  2553.    characters can start a string that matches the pattern.  This fastmap
  2554.    is used by re_search to skip quickly over impossible starting points.
  2555.  
  2556.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data
  2557.    area as BUFP->fastmap.
  2558.    
  2559.    We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
  2560.    the pattern buffer.
  2561.  
  2562.    Returns 0 if we succeed, -2 if an internal error.   */
  2563.  
  2564. int
  2565. re_compile_fastmap (bufp)
  2566.      struct re_pattern_buffer *bufp;
  2567. {
  2568.   int j, k;
  2569.   fail_stack_type fail_stack;
  2570. #ifndef REGEX_MALLOC
  2571.   char *destination;
  2572. #endif
  2573.   /* We don't push any register information onto the failure stack.  */
  2574.   unsigned num_regs = 0;
  2575.   
  2576.   register char *fastmap = bufp->fastmap;
  2577.   unsigned char *pattern = bufp->buffer;
  2578.   unsigned long size = bufp->used;
  2579.   const unsigned char *p = pattern;
  2580.   register unsigned char *pend = pattern + size;
  2581.  
  2582.   /* Assume that each path through the pattern can be null until
  2583.      proven otherwise.  We set this false at the bottom of switch
  2584.      statement, to which we get only if a particular path doesn't
  2585.      match the empty string.  */
  2586.   boolean path_can_be_null = true;
  2587.  
  2588.   /* We aren't doing a `succeed_n' to begin with.  */
  2589.   boolean succeed_n_p = false;
  2590.  
  2591.   assert (fastmap != NULL && p != NULL);
  2592.   
  2593.   INIT_FAIL_STACK ();
  2594.   bzero (fastmap, 1 << BYTEWIDTH);  /* Assume nothing's valid.  */
  2595.   bufp->fastmap_accurate = 1;        /* It will be when we're done.  */
  2596.   bufp->can_be_null = 0;
  2597.       
  2598.   while (p != pend || !FAIL_STACK_EMPTY ())
  2599.     {
  2600.       if (p == pend)
  2601.         {
  2602.           bufp->can_be_null |= path_can_be_null;
  2603.           
  2604.           /* Reset for next path.  */
  2605.           path_can_be_null = true;
  2606.           
  2607.           p = fail_stack.stack[--fail_stack.avail];
  2608.     }
  2609.  
  2610.       /* We should never be about to go beyond the end of the pattern.  */
  2611.       assert (p < pend);
  2612.       
  2613. #ifdef SWITCH_ENUM_BUG
  2614.       switch ((int) ((re_opcode_t) *p++))
  2615. #else
  2616.       switch ((re_opcode_t) *p++)
  2617. #endif
  2618.     {
  2619.  
  2620.         /* I guess the idea here is to simply not bother with a fastmap
  2621.            if a backreference is used, since it's too hard to figure out
  2622.            the fastmap for the corresponding group.  Setting
  2623.            `can_be_null' stops `re_search_2' from using the fastmap, so
  2624.            that is all we do.  */
  2625.     case duplicate:
  2626.       bufp->can_be_null = 1;
  2627.           return 0;
  2628.  
  2629.  
  2630.       /* Following are the cases which match a character.  These end
  2631.          with `break'.  */
  2632.  
  2633.     case exactn:
  2634.           fastmap[p[1]] = 1;
  2635.       break;
  2636.  
  2637.  
  2638.         case charset:
  2639.           for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2640.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  2641.               fastmap[j] = 1;
  2642.       break;
  2643.  
  2644.  
  2645.     case charset_not:
  2646.       /* Chars beyond end of map must be allowed.  */
  2647.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  2648.             fastmap[j] = 1;
  2649.  
  2650.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2651.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  2652.               fastmap[j] = 1;
  2653.           break;
  2654.  
  2655.  
  2656.     case wordchar:
  2657.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2658.         if (SYNTAX (j) == Sword)
  2659.           fastmap[j] = 1;
  2660.       break;
  2661.  
  2662.  
  2663.     case notwordchar:
  2664.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2665.         if (SYNTAX (j) != Sword)
  2666.           fastmap[j] = 1;
  2667.       break;
  2668.  
  2669.  
  2670.         case anychar:
  2671.           /* `.' matches anything ...  */
  2672.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2673.             fastmap[j] = 1;
  2674.  
  2675.           /* ... except perhaps newline.  */
  2676.           if (!(bufp->syntax & RE_DOT_NEWLINE))
  2677.             fastmap['\n'] = 0;
  2678.  
  2679.           /* Return if we have already set `can_be_null'; if we have,
  2680.              then the fastmap is irrelevant.  Something's wrong here.  */
  2681.       else if (bufp->can_be_null)
  2682.         return 0;
  2683.  
  2684.           /* Otherwise, have to check alternative paths.  */
  2685.       break;
  2686.  
  2687.  
  2688. #ifdef emacs
  2689.         case syntaxspec:
  2690.       k = *p++;
  2691.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2692.         if (SYNTAX (j) == (enum syntaxcode) k)
  2693.           fastmap[j] = 1;
  2694.       break;
  2695.  
  2696.  
  2697.     case notsyntaxspec:
  2698.       k = *p++;
  2699.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  2700.         if (SYNTAX (j) != (enum syntaxcode) k)
  2701.           fastmap[j] = 1;
  2702.       break;
  2703.  
  2704.  
  2705.       /* All cases after this match the empty string.  These end with
  2706.          `continue'.  */
  2707.  
  2708.  
  2709.     case before_dot:
  2710.     case at_dot:
  2711.     case after_dot:
  2712.           continue;
  2713. #endif /* not emacs */
  2714.  
  2715.  
  2716.         case no_op:
  2717.         case begline:
  2718.         case endline:
  2719.     case begbuf:
  2720.     case endbuf:
  2721.     case wordbound:
  2722.     case notwordbound:
  2723.     case wordbeg:
  2724.     case wordend:
  2725.         case push_dummy_failure:
  2726.           continue;
  2727.  
  2728.  
  2729.     case jump_n:
  2730.         case pop_failure_jump:
  2731.     case maybe_pop_jump:
  2732.     case jump:
  2733.         case jump_past_alt:
  2734.     case dummy_failure_jump:
  2735.           EXTRACT_NUMBER_AND_INCR (j, p);
  2736.       p += j;    
  2737.       if (j > 0)
  2738.         continue;
  2739.             
  2740.           /* Jump backward implies we just went through the body of a
  2741.              loop and matched nothing.  Opcode jumped to should be
  2742.              `on_failure_jump' or `succeed_n'.  Just treat it like an
  2743.              ordinary jump.  For a * loop, it has pushed its failure
  2744.              point already; if so, discard that as redundant.  */
  2745.           if ((re_opcode_t) *p != on_failure_jump
  2746.           && (re_opcode_t) *p != succeed_n)
  2747.         continue;
  2748.  
  2749.           p++;
  2750.           EXTRACT_NUMBER_AND_INCR (j, p);
  2751.           p += j;        
  2752.       
  2753.           /* If what's on the stack is where we are now, pop it.  */
  2754.           if (!FAIL_STACK_EMPTY () 
  2755.           && fail_stack.stack[fail_stack.avail - 1] == p)
  2756.             fail_stack.avail--;
  2757.  
  2758.           continue;
  2759.  
  2760.  
  2761.         case on_failure_jump:
  2762.         case on_failure_keep_string_jump:
  2763.     handle_on_failure_jump:
  2764.           EXTRACT_NUMBER_AND_INCR (j, p);
  2765.  
  2766.           /* For some patterns, e.g., `(a?)?', `p+j' here points to the
  2767.              end of the pattern.  We don't want to push such a point,
  2768.              since when we restore it above, entering the switch will
  2769.              increment `p' past the end of the pattern.  We don't need
  2770.              to push such a point since we obviously won't find any more
  2771.              fastmap entries beyond `pend'.  Such a pattern can match
  2772.              the null string, though.  */
  2773.           if (p + j < pend)
  2774.             {
  2775.               if (!PUSH_PATTERN_OP (p + j, fail_stack))
  2776.                 return -2;
  2777.             }
  2778.           else
  2779.             bufp->can_be_null = 1;
  2780.  
  2781.           if (succeed_n_p)
  2782.             {
  2783.               EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  2784.               succeed_n_p = false;
  2785.         }
  2786.  
  2787.           continue;
  2788.  
  2789.  
  2790.     case succeed_n:
  2791.           /* Get to the number of times to succeed.  */
  2792.           p += 2;        
  2793.  
  2794.           /* Increment p past the n for when k != 0.  */
  2795.           EXTRACT_NUMBER_AND_INCR (k, p);
  2796.           if (k == 0)
  2797.         {
  2798.               p -= 4;
  2799.             succeed_n_p = true;  /* Spaghetti code alert.  */
  2800.               goto handle_on_failure_jump;
  2801.             }
  2802.           continue;
  2803.  
  2804.  
  2805.     case set_number_at:
  2806.           p += 4;
  2807.           continue;
  2808.  
  2809.  
  2810.     case start_memory:
  2811.         case stop_memory:
  2812.       p += 2;
  2813.       continue;
  2814.  
  2815.  
  2816.     default:
  2817.           abort (); /* We have listed all the cases.  */
  2818.         } /* switch *p++ */
  2819.  
  2820.       /* Getting here means we have found the possible starting
  2821.          characters for one path of the pattern -- and that the empty
  2822.          string does not match.  We need not follow this path further.
  2823.          Instead, look at the next alternative (remembered on the
  2824.          stack), or quit if no more.  The test at the top of the loop
  2825.          does these things.  */
  2826.       path_can_be_null = false;
  2827.       p = pend;
  2828.     } /* while p */
  2829.  
  2830.   /* Set `can_be_null' for the last path (also the first path, if the
  2831.      pattern is empty).  */
  2832.   bufp->can_be_null |= path_can_be_null;
  2833.   return 0;
  2834. } /* re_compile_fastmap */
  2835.  
  2836. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  2837.    ENDS.  Subsequent matches using PATTERN_BUFFER and REGS will use
  2838.    this memory for recording register information.  STARTS and ENDS
  2839.    must be allocated using the malloc library routine, and must each
  2840.    be at least NUM_REGS * sizeof (regoff_t) bytes long.
  2841.  
  2842.    If NUM_REGS == 0, then subsequent matches should allocate their own
  2843.    register data.
  2844.  
  2845.    Unless this function is called, the first search or match using
  2846.    PATTERN_BUFFER will allocate its own register data, without
  2847.    freeing the old data.  */
  2848.  
  2849. void
  2850. re_set_registers (bufp, regs, num_regs, starts, ends)
  2851.     struct re_pattern_buffer *bufp;
  2852.     struct re_registers *regs;
  2853.     unsigned num_regs;
  2854.     regoff_t *starts, *ends;
  2855. {
  2856.   if (num_regs)
  2857.     {
  2858.       bufp->regs_allocated = REGS_REALLOCATE;
  2859.       regs->num_regs = num_regs;
  2860.       regs->start = starts;
  2861.       regs->end = ends;
  2862.     }
  2863.   else
  2864.     {
  2865.       bufp->regs_allocated = REGS_UNALLOCATED;
  2866.       regs->num_regs = 0;
  2867.       regs->start = regs->end = (regoff_t) 0;
  2868.     }
  2869. }
  2870.  
  2871. /* Searching routines.  */
  2872.  
  2873. /* Like re_search_2, below, but only one string is specified, and
  2874.    doesn't let you say where to stop matching. */
  2875.  
  2876. int
  2877. re_search (bufp, string, size, startpos, range, regs)
  2878.      struct re_pattern_buffer *bufp;
  2879.      const char *string;
  2880.      int size, startpos, range;
  2881.      struct re_registers *regs;
  2882. {
  2883.   return re_search_2 (bufp, NULL, 0, string, size, startpos, range, 
  2884.               regs, size);
  2885. }
  2886.  
  2887.  
  2888. /* Using the compiled pattern in BUFP->buffer, first tries to match the
  2889.    virtual concatenation of STRING1 and STRING2, starting first at index
  2890.    STARTPOS, then at STARTPOS + 1, and so on.
  2891.    
  2892.    STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
  2893.    
  2894.    RANGE is how far to scan while trying to match.  RANGE = 0 means try
  2895.    only at STARTPOS; in general, the last start tried is STARTPOS +
  2896.    RANGE.
  2897.    
  2898.    In REGS, return the indices of the virtual concatenation of STRING1
  2899.    and STRING2 that matched the entire BUFP->buffer and its contained
  2900.    subexpressions.
  2901.    
  2902.    Do not consider matching one past the index STOP in the virtual
  2903.    concatenation of STRING1 and STRING2.
  2904.  
  2905.    We return either the position in the strings at which the match was
  2906.    found, -1 if no match, or -2 if error (such as failure
  2907.    stack overflow).  */
  2908.  
  2909. int
  2910. re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
  2911.      struct re_pattern_buffer *bufp;
  2912.      const char *string1, *string2;
  2913.      int size1, size2;
  2914.      int startpos;
  2915.      int range;
  2916.      struct re_registers *regs;
  2917.      int stop;
  2918. {
  2919.   int val;
  2920.   register char *fastmap = bufp->fastmap;
  2921.   register char *translate = bufp->translate;
  2922.   int total_size = size1 + size2;
  2923.   int endpos = startpos + range;
  2924.  
  2925.   /* Check for out-of-range STARTPOS.  */
  2926.   if (startpos < 0 || startpos > total_size)
  2927.     return -1;
  2928.     
  2929.   /* Fix up RANGE if it might eventually take us outside
  2930.      the virtual concatenation of STRING1 and STRING2.  */
  2931.   if (endpos < -1)
  2932.     range = -1 - startpos;
  2933.   else if (endpos > total_size)
  2934.     range = total_size - startpos;
  2935.  
  2936.   /* If the search isn't to be a backwards one, don't waste time in a
  2937.      search for a pattern that must be anchored.  */
  2938.   if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
  2939.     {
  2940.       if (startpos > 0)
  2941.     return -1;
  2942.       else
  2943.     range = 1;
  2944.     }
  2945.  
  2946.   /* Update the fastmap now if not correct already.  */
  2947.   if (fastmap && !bufp->fastmap_accurate)
  2948.     if (re_compile_fastmap (bufp) == -2)
  2949.       return -2;
  2950.   
  2951.   /* Loop through the string, looking for a place to start matching.  */
  2952.   for (;;)
  2953.     { 
  2954.       /* If a fastmap is supplied, skip quickly over characters that
  2955.          cannot be the start of a match.  If the pattern can match the
  2956.          null string, however, we don't need to skip characters; we want
  2957.          the first null string.  */
  2958.       if (fastmap && startpos < total_size && !bufp->can_be_null)
  2959.     {
  2960.       if (range > 0)    /* Searching forwards.  */
  2961.         {
  2962.           register const char *d;
  2963.           register int lim = 0;
  2964.           int irange = range;
  2965.  
  2966.               if (startpos < size1 && startpos + range >= size1)
  2967.                 lim = range - (size1 - startpos);
  2968.  
  2969.           d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
  2970.    
  2971.               /* Written out as an if-else to avoid testing `translate'
  2972.                  inside the loop.  */
  2973.           if (translate)
  2974.                 while (range > lim
  2975.                        && !fastmap[(unsigned char)
  2976.                    translate[(unsigned char) *d++]])
  2977.                   range--;
  2978.           else
  2979.                 while (range > lim && !fastmap[(unsigned char) *d++])
  2980.                   range--;
  2981.  
  2982.           startpos += irange - range;
  2983.         }
  2984.       else                /* Searching backwards.  */
  2985.         {
  2986.           register char c = (size1 == 0 || startpos >= size1
  2987.                                  ? string2[startpos - size1] 
  2988.                                  : string1[startpos]);
  2989.  
  2990.           if (!fastmap[(unsigned char) TRANSLATE (c)])
  2991.         goto advance;
  2992.         }
  2993.     }
  2994.  
  2995.       /* If can't match the null string, and that's all we have left, fail.  */
  2996.       if (range >= 0 && startpos == total_size && fastmap
  2997.           && !bufp->can_be_null)
  2998.     return -1;
  2999.  
  3000.       val = re_match_2 (bufp, string1, size1, string2, size2,
  3001.                     startpos, regs, stop);
  3002.       if (val >= 0)
  3003.     return startpos;
  3004.         
  3005.       if (val == -2)
  3006.     return -2;
  3007.  
  3008.     advance:
  3009.       if (!range) 
  3010.         break;
  3011.       else if (range > 0) 
  3012.         {
  3013.           range--; 
  3014.           startpos++;
  3015.         }
  3016.       else
  3017.         {
  3018.           range++; 
  3019.           startpos--;
  3020.         }
  3021.     }
  3022.   return -1;
  3023. } /* re_search_2 */
  3024.  
  3025. /* Declarations and macros for re_match_2.  */
  3026.  
  3027. static int bcmp_translate ();
  3028. static boolean alt_match_null_string_p (),
  3029.                common_op_match_null_string_p (),
  3030.                group_match_null_string_p ();
  3031.  
  3032. /* Structure for per-register (a.k.a. per-group) information.
  3033.    This must not be longer than one word, because we push this value
  3034.    onto the failure stack.  Other register information, such as the
  3035.    starting and ending positions (which are addresses), and the list of
  3036.    inner groups (which is a bits list) are maintained in separate
  3037.    variables.  
  3038.    
  3039.    We are making a (strictly speaking) nonportable assumption here: that
  3040.    the compiler will pack our bit fields into something that fits into
  3041.    the type of `word', i.e., is something that fits into one item on the
  3042.    failure stack.  */
  3043. typedef union
  3044. {
  3045.   fail_stack_elt_t word;
  3046.   struct
  3047.   {
  3048.       /* This field is one if this group can match the empty string,
  3049.          zero if not.  If not yet determined,  `MATCH_NULL_UNSET_VALUE'.  */
  3050. #define MATCH_NULL_UNSET_VALUE 3
  3051.     unsigned match_null_string_p : 2;
  3052.     unsigned is_active : 1;
  3053.     unsigned matched_something : 1;
  3054.     unsigned ever_matched_something : 1;
  3055.   } bits;
  3056. } register_info_type;
  3057.  
  3058. #define REG_MATCH_NULL_STRING_P(R)  ((R).bits.match_null_string_p)
  3059. #define IS_ACTIVE(R)  ((R).bits.is_active)
  3060. #define MATCHED_SOMETHING(R)  ((R).bits.matched_something)
  3061. #define EVER_MATCHED_SOMETHING(R)  ((R).bits.ever_matched_something)
  3062.  
  3063.  
  3064. /* Call this when have matched a real character; it sets `matched' flags
  3065.    for the subexpressions which we are currently inside.  Also records
  3066.    that those subexprs have matched.  */
  3067. #define SET_REGS_MATCHED()                        \
  3068.   do                                    \
  3069.     {                                    \
  3070.       unsigned r;                            \
  3071.       for (r = lowest_active_reg; r <= highest_active_reg; r++)        \
  3072.         {                                \
  3073.           MATCHED_SOMETHING (reg_info[r])                \
  3074.             = EVER_MATCHED_SOMETHING (reg_info[r])            \
  3075.             = 1;                            \
  3076.         }                                \
  3077.     }                                    \
  3078.   while (0)
  3079.  
  3080.  
  3081. /* This converts PTR, a pointer into one of the search strings `string1'
  3082.    and `string2' into an offset from the beginning of that string.  */
  3083. #define POINTER_TO_OFFSET(ptr)                        \
  3084.   (FIRST_STRING_P (ptr) ? (ptr) - string1 : (ptr) - string2 + size1)
  3085.  
  3086. /* Registers are set to a sentinel when they haven't yet matched.  */
  3087. #define REG_UNSET_VALUE ((char *) -1)
  3088. #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
  3089.  
  3090.  
  3091. /* Macros for dealing with the split strings in re_match_2.  */
  3092.  
  3093. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  3094.  
  3095. /* Call before fetching a character with *d.  This switches over to
  3096.    string2 if necessary.  */
  3097. #define PREFETCH()                            \
  3098.   while (d == dend)                                \
  3099.     {                                    \
  3100.       /* End of string2 => fail.  */                    \
  3101.       if (dend == end_match_2)                         \
  3102.         goto fail;                            \
  3103.       /* End of string1 => advance to string2.  */             \
  3104.       d = string2;                                \
  3105.       dend = end_match_2;                        \
  3106.     }
  3107.  
  3108.  
  3109. /* Test if at very beginning or at very end of the virtual concatenation
  3110.    of `string1' and `string2'.  If only one string, it's `string2'.  */
  3111. #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
  3112. #define AT_STRINGS_END(d) ((d) == end2)    
  3113.  
  3114.  
  3115. /* Test if D points to a character which is word-constituent.  We have
  3116.    two special cases to check for: if past the end of string1, look at
  3117.    the first character in string2; and if before the beginning of
  3118.    string2, look at the last character in string1.  */
  3119. #define WORDCHAR_P(d)                            \
  3120.   (SYNTAX ((d) == end1 ? *string2                    \
  3121.            : (d) == string2 - 1 ? *(end1 - 1) : *(d))            \
  3122.    == Sword)
  3123.  
  3124. /* Test if the character before D and the one at D differ with respect
  3125.    to being word-constituent.  */
  3126. #define AT_WORD_BOUNDARY(d)                        \
  3127.   (AT_STRINGS_BEG (d) || AT_STRINGS_END (d)                \
  3128.    || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
  3129.  
  3130.  
  3131. /* Free everything we malloc.  */
  3132. #ifdef REGEX_MALLOC
  3133. #define FREE_VAR(var) if (var) free (var); var = NULL
  3134. #define FREE_VARIABLES()                        \
  3135.   do {                                    \
  3136.     FREE_VAR (fail_stack.stack);                    \
  3137.     FREE_VAR (regstart);                        \
  3138.     FREE_VAR (regend);                            \
  3139.     FREE_VAR (old_regstart);                        \
  3140.     FREE_VAR (old_regend);                        \
  3141.     FREE_VAR (best_regstart);                        \
  3142.     FREE_VAR (best_regend);                        \
  3143.     FREE_VAR (reg_info);                        \
  3144.     FREE_VAR (reg_dummy);                        \
  3145.     FREE_VAR (reg_info_dummy);                        \
  3146.   } while (0)
  3147. #else /* not REGEX_MALLOC */
  3148. /* Some MIPS systems (at least) want this to free alloca'd storage.  */
  3149. #define FREE_VARIABLES() alloca (0)
  3150. #endif /* not REGEX_MALLOC */
  3151.  
  3152.  
  3153. /* These values must meet several constraints.  They must not be valid
  3154.    register values; since we have a limit of 255 registers (because
  3155.    we use only one byte in the pattern for the register number), we can
  3156.    use numbers larger than 255.  They must differ by 1, because of
  3157.    NUM_FAILURE_ITEMS above.  And the value for the lowest register must
  3158.    be larger than the value for the highest register, so we do not try
  3159.    to actually save any registers when none are active.  */
  3160. #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
  3161. #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
  3162.  
  3163. /* Matching routines.  */
  3164.  
  3165. #ifndef emacs   /* Emacs never uses this.  */
  3166. /* re_match is like re_match_2 except it takes only a single string.  */
  3167.  
  3168. int
  3169. re_match (bufp, string, size, pos, regs)
  3170.      struct re_pattern_buffer *bufp;
  3171.      const char *string;
  3172.      int size, pos;
  3173.      struct re_registers *regs;
  3174.  {
  3175.   return re_match_2 (bufp, NULL, 0, string, size, pos, regs, size); 
  3176. }
  3177. #endif /* not emacs */
  3178.  
  3179.  
  3180. /* re_match_2 matches the compiled pattern in BUFP against the
  3181.    the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
  3182.    and SIZE2, respectively).  We start matching at POS, and stop
  3183.    matching at STOP.
  3184.    
  3185.    If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
  3186.    store offsets for the substring each group matched in REGS.  See the
  3187.    documentation for exactly how many groups we fill.
  3188.  
  3189.    We return -1 if no match, -2 if an internal error (such as the
  3190.    failure stack overflowing).  Otherwise, we return the length of the
  3191.    matched substring.  */
  3192.  
  3193. int
  3194. re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  3195.      struct re_pattern_buffer *bufp;
  3196.      const char *string1, *string2;
  3197.      int size1, size2;
  3198.      int pos;
  3199.      struct re_registers *regs;
  3200.      int stop;
  3201. {
  3202.   /* General temporaries.  */
  3203.   int mcnt;
  3204.   unsigned char *p1;
  3205.  
  3206.   /* Just past the end of the corresponding string.  */
  3207.   const char *end1, *end2;
  3208.  
  3209.   /* Pointers into string1 and string2, just past the last characters in
  3210.      each to consider matching.  */
  3211.   const char *end_match_1, *end_match_2;
  3212.  
  3213.   /* Where we are in the data, and the end of the current string.  */
  3214.   const char *d, *dend;
  3215.   
  3216.   /* Where we are in the pattern, and the end of the pattern.  */
  3217.   unsigned char *p = bufp->buffer;
  3218.   register unsigned char *pend = p + bufp->used;
  3219.  
  3220.   /* We use this to map every character in the string.  */
  3221.   char *translate = bufp->translate;
  3222.  
  3223.   /* Failure point stack.  Each place that can handle a failure further
  3224.      down the line pushes a failure point on this stack.  It consists of
  3225.      restart, regend, and reg_info for all registers corresponding to
  3226.      the subexpressions we're currently inside, plus the number of such
  3227.      registers, and, finally, two char *'s.  The first char * is where
  3228.      to resume scanning the pattern; the second one is where to resume
  3229.      scanning the strings.  If the latter is zero, the failure point is
  3230.      a ``dummy''; if a failure happens and the failure point is a dummy,
  3231.      it gets discarded and the next next one is tried.  */
  3232.   fail_stack_type fail_stack;
  3233. #ifdef DEBUG
  3234.   static unsigned failure_id = 0;
  3235.   unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
  3236. #endif
  3237.  
  3238.   /* We fill all the registers internally, independent of what we
  3239.      return, for use in backreferences.  The number here includes
  3240.      an element for register zero.  */
  3241.   unsigned num_regs = bufp->re_nsub + 1;
  3242.   
  3243.   /* The currently active registers.  */
  3244.   unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3245.   unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3246.  
  3247.   /* Information on the contents of registers. These are pointers into
  3248.      the input strings; they record just what was matched (on this
  3249.      attempt) by a subexpression part of the pattern, that is, the
  3250.      regnum-th regstart pointer points to where in the pattern we began
  3251.      matching and the regnum-th regend points to right after where we
  3252.      stopped matching the regnum-th subexpression.  (The zeroth register
  3253.      keeps track of what the whole pattern matches.)  */
  3254.   const char **regstart, **regend;
  3255.  
  3256.   /* If a group that's operated upon by a repetition operator fails to
  3257.      match anything, then the register for its start will need to be
  3258.      restored because it will have been set to wherever in the string we
  3259.      are when we last see its open-group operator.  Similarly for a
  3260.      register's end.  */
  3261.   const char **old_regstart, **old_regend;
  3262.  
  3263.   /* The is_active field of reg_info helps us keep track of which (possibly
  3264.      nested) subexpressions we are currently in. The matched_something
  3265.      field of reg_info[reg_num] helps us tell whether or not we have
  3266.      matched any of the pattern so far this time through the reg_num-th
  3267.      subexpression.  These two fields get reset each time through any
  3268.      loop their register is in.  */
  3269.   register_info_type *reg_info; 
  3270.  
  3271.   /* The following record the register info as found in the above
  3272.      variables when we find a match better than any we've seen before. 
  3273.      This happens as we backtrack through the failure points, which in
  3274.      turn happens only if we have not yet matched the entire string. */
  3275.   unsigned best_regs_set = false;
  3276.   const char **best_regstart, **best_regend;
  3277.   
  3278.   /* Logically, this is `best_regend[0]'.  But we don't want to have to
  3279.      allocate space for that if we're not allocating space for anything
  3280.      else (see below).  Also, we never need info about register 0 for
  3281.      any of the other register vectors, and it seems rather a kludge to
  3282.      treat `best_regend' differently than the rest.  So we keep track of
  3283.      the end of the best match so far in a separate variable.  We
  3284.      initialize this to NULL so that when we backtrack the first time
  3285.      and need to test it, it's not garbage.  */
  3286.   const char *match_end = NULL;
  3287.  
  3288.   /* Used when we pop values we don't care about.  */
  3289.   const char **reg_dummy;
  3290.   register_info_type *reg_info_dummy;
  3291.  
  3292. #ifdef DEBUG
  3293.   /* Counts the total number of registers pushed.  */
  3294.   unsigned num_regs_pushed = 0;     
  3295. #endif
  3296.  
  3297.   DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
  3298.   
  3299.   INIT_FAIL_STACK ();
  3300.   
  3301.   /* Do not bother to initialize all the register variables if there are
  3302.      no groups in the pattern, as it takes a fair amount of time.  If
  3303.      there are groups, we include space for register 0 (the whole
  3304.      pattern), even though we never use it, since it simplifies the
  3305.      array indexing.  We should fix this.  */
  3306.   if (bufp->re_nsub)
  3307.     {
  3308.       regstart = REGEX_TALLOC (num_regs, const char *);
  3309.       regend = REGEX_TALLOC (num_regs, const char *);
  3310.       old_regstart = REGEX_TALLOC (num_regs, const char *);
  3311.       old_regend = REGEX_TALLOC (num_regs, const char *);
  3312.       best_regstart = REGEX_TALLOC (num_regs, const char *);
  3313.       best_regend = REGEX_TALLOC (num_regs, const char *);
  3314.       reg_info = REGEX_TALLOC (num_regs, register_info_type);
  3315.       reg_dummy = REGEX_TALLOC (num_regs, const char *);
  3316.       reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
  3317.  
  3318.       if (!(regstart && regend && old_regstart && old_regend && reg_info 
  3319.             && best_regstart && best_regend && reg_dummy && reg_info_dummy)) 
  3320.         {
  3321.           FREE_VARIABLES ();
  3322.           return -2;
  3323.         }
  3324.     }
  3325. #ifdef REGEX_MALLOC
  3326.   else
  3327.     {
  3328.       /* We must initialize all our variables to NULL, so that
  3329.          `FREE_VARIABLES' doesn't try to free them.  */
  3330.       regstart = regend = old_regstart = old_regend = best_regstart
  3331.         = best_regend = reg_dummy = NULL;
  3332.       reg_info = reg_info_dummy = (register_info_type *) NULL;
  3333.     }
  3334. #endif /* REGEX_MALLOC */
  3335.  
  3336.   /* The starting position is bogus.  */
  3337.   if (pos < 0 || pos > size1 + size2)
  3338.     {
  3339.       FREE_VARIABLES ();
  3340.       return -1;
  3341.     }
  3342.     
  3343.   /* Initialize subexpression text positions to -1 to mark ones that no
  3344.      start_memory/stop_memory has been seen for. Also initialize the
  3345.      register information struct.  */
  3346.   for (mcnt = 1; mcnt < num_regs; mcnt++)
  3347.     {
  3348.       regstart[mcnt] = regend[mcnt] 
  3349.         = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
  3350.         
  3351.       REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
  3352.       IS_ACTIVE (reg_info[mcnt]) = 0;
  3353.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3354.       EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3355.     }
  3356.   
  3357.   /* We move `string1' into `string2' if the latter's empty -- but not if
  3358.      `string1' is null.  */
  3359.   if (size2 == 0 && string1 != NULL)
  3360.     {
  3361.       string2 = string1;
  3362.       size2 = size1;
  3363.       string1 = 0;
  3364.       size1 = 0;
  3365.     }
  3366.   end1 = string1 + size1;
  3367.   end2 = string2 + size2;
  3368.  
  3369.   /* Compute where to stop matching, within the two strings.  */
  3370.   if (stop <= size1)
  3371.     {
  3372.       end_match_1 = string1 + stop;
  3373.       end_match_2 = string2;
  3374.     }
  3375.   else
  3376.     {
  3377.       end_match_1 = end1;
  3378.       end_match_2 = string2 + stop - size1;
  3379.     }
  3380.  
  3381.   /* `p' scans through the pattern as `d' scans through the data. 
  3382.      `dend' is the end of the input string that `d' points within.  `d'
  3383.      is advanced into the following input string whenever necessary, but
  3384.      this happens before fetching; therefore, at the beginning of the
  3385.      loop, `d' can be pointing at the end of a string, but it cannot
  3386.      equal `string2'.  */
  3387.   if (size1 > 0 && pos <= size1)
  3388.     {
  3389.       d = string1 + pos;
  3390.       dend = end_match_1;
  3391.     }
  3392.   else
  3393.     {
  3394.       d = string2 + pos - size1;
  3395.       dend = end_match_2;
  3396.     }
  3397.  
  3398.   DEBUG_PRINT1 ("The compiled pattern is: ");
  3399.   DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
  3400.   DEBUG_PRINT1 ("The string to match is: `");
  3401.   DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
  3402.   DEBUG_PRINT1 ("'\n");
  3403.   
  3404.   /* This loops over pattern commands.  It exits by returning from the
  3405.      function if the match is complete, or it drops through if the match
  3406.      fails at this starting point in the input data.  */
  3407.   for (;;)
  3408.     {
  3409.       DEBUG_PRINT2 ("\n0x%x: ", p);
  3410.  
  3411.       if (p == pend)
  3412.     { /* End of pattern means we might have succeeded.  */
  3413.           DEBUG_PRINT1 ("end of pattern ... ");
  3414.           
  3415.       /* If we haven't matched the entire string, and we want the
  3416.              longest match, try backtracking.  */
  3417.           if (d != end_match_2)
  3418.         {
  3419.               DEBUG_PRINT1 ("backtracking.\n");
  3420.               
  3421.               if (!FAIL_STACK_EMPTY ())
  3422.                 { /* More failure points to try.  */
  3423.                   boolean same_str_p = (FIRST_STRING_P (match_end) 
  3424.                                 == MATCHING_IN_FIRST_STRING);
  3425.  
  3426.                   /* If exceeds best match so far, save it.  */
  3427.                   if (!best_regs_set
  3428.                       || (same_str_p && d > match_end)
  3429.                       || (!same_str_p && !MATCHING_IN_FIRST_STRING))
  3430.                     {
  3431.                       best_regs_set = true;
  3432.                       match_end = d;
  3433.                       
  3434.                       DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
  3435.                       
  3436.                       for (mcnt = 1; mcnt < num_regs; mcnt++)
  3437.                         {
  3438.                           best_regstart[mcnt] = regstart[mcnt];
  3439.                           best_regend[mcnt] = regend[mcnt];
  3440.                         }
  3441.                     }
  3442.                   goto fail;           
  3443.                 }
  3444.  
  3445.               /* If no failure points, don't restore garbage.  */
  3446.               else if (best_regs_set)   
  3447.                 {
  3448.               restore_best_regs:
  3449.                   /* Restore best match.  It may happen that `dend ==
  3450.                      end_match_1' while the restored d is in string2.
  3451.                      For example, the pattern `x.*y.*z' against the
  3452.                      strings `x-' and `y-z-', if the two strings are
  3453.                      not consecutive in memory.  */
  3454.                   DEBUG_PRINT1 ("Restoring best registers.\n");
  3455.                   
  3456.                   d = match_end;
  3457.                   dend = ((d >= string1 && d <= end1)
  3458.                    ? end_match_1 : end_match_2);
  3459.  
  3460.           for (mcnt = 1; mcnt < num_regs; mcnt++)
  3461.             {
  3462.               regstart[mcnt] = best_regstart[mcnt];
  3463.               regend[mcnt] = best_regend[mcnt];
  3464.             }
  3465.                 }
  3466.             } /* d != end_match_2 */
  3467.  
  3468.           DEBUG_PRINT1 ("Accepting match.\n");
  3469.  
  3470.           /* If caller wants register contents data back, do it.  */
  3471.           if (regs && !bufp->no_sub)
  3472.         {
  3473.               /* Have the register data arrays been allocated?  */
  3474.               if (bufp->regs_allocated == REGS_UNALLOCATED)
  3475.                 { /* No.  So allocate them with malloc.  We need one
  3476.                      extra element beyond `num_regs' for the `-1' marker
  3477.                      GNU code uses.  */
  3478.                   regs->num_regs = MAX (RE_NREGS, num_regs + 1);
  3479.                   regs->start = TALLOC (regs->num_regs, regoff_t);
  3480.                   regs->end = TALLOC (regs->num_regs, regoff_t);
  3481.                   if (regs->start == NULL || regs->end == NULL)
  3482.                     return -2;
  3483.                   bufp->regs_allocated = REGS_REALLOCATE;
  3484.                 }
  3485.               else if (bufp->regs_allocated == REGS_REALLOCATE)
  3486.                 { /* Yes.  If we need more elements than were already
  3487.                      allocated, reallocate them.  If we need fewer, just
  3488.                      leave it alone.  */
  3489.                   if (regs->num_regs < num_regs + 1)
  3490.                     {
  3491.                       regs->num_regs = num_regs + 1;
  3492.                       RETALLOC (regs->start, regs->num_regs, regoff_t);
  3493.                       RETALLOC (regs->end, regs->num_regs, regoff_t);
  3494.                       if (regs->start == NULL || regs->end == NULL)
  3495.                         return -2;
  3496.                     }
  3497.                 }
  3498.               else
  3499.         {
  3500.           /* These braces fend off a "empty body in an else-statement"
  3501.              warning under GCC when assert expands to nothing.  */
  3502.           assert (bufp->regs_allocated == REGS_FIXED);
  3503.         }
  3504.  
  3505.               /* Convert the pointer data in `regstart' and `regend' to
  3506.                  indices.  Register zero has to be set differently,
  3507.                  since we haven't kept track of any info for it.  */
  3508.               if (regs->num_regs > 0)
  3509.                 {
  3510.                   regs->start[0] = pos;
  3511.                   regs->end[0] = (MATCHING_IN_FIRST_STRING ? d - string1
  3512.                       : d - string2 + size1);
  3513.                 }
  3514.               
  3515.               /* Go through the first `min (num_regs, regs->num_regs)'
  3516.                  registers, since that is all we initialized.  */
  3517.           for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
  3518.         {
  3519.                   if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
  3520.                     regs->start[mcnt] = regs->end[mcnt] = -1;
  3521.                   else
  3522.                     {
  3523.               regs->start[mcnt] = POINTER_TO_OFFSET (regstart[mcnt]);
  3524.                       regs->end[mcnt] = POINTER_TO_OFFSET (regend[mcnt]);
  3525.                     }
  3526.         }
  3527.               
  3528.               /* If the regs structure we return has more elements than
  3529.                  were in the pattern, set the extra elements to -1.  If
  3530.                  we (re)allocated the registers, this is the case,
  3531.                  because we always allocate enough to have at least one
  3532.                  -1 at the end.  */
  3533.               for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
  3534.                 regs->start[mcnt] = regs->end[mcnt] = -1;
  3535.         } /* regs && !bufp->no_sub */
  3536.  
  3537.           FREE_VARIABLES ();
  3538.           DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
  3539.                         nfailure_points_pushed, nfailure_points_popped,
  3540.                         nfailure_points_pushed - nfailure_points_popped);
  3541.           DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
  3542.  
  3543.           mcnt = d - pos - (MATCHING_IN_FIRST_STRING 
  3544.                 ? string1 
  3545.                 : string2 - size1);
  3546.  
  3547.           DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
  3548.  
  3549.           return mcnt;
  3550.         }
  3551.  
  3552.       /* Otherwise match next pattern command.  */
  3553. #ifdef SWITCH_ENUM_BUG
  3554.       switch ((int) ((re_opcode_t) *p++))
  3555. #else
  3556.       switch ((re_opcode_t) *p++)
  3557. #endif
  3558.     {
  3559.         /* Ignore these.  Used to ignore the n of succeed_n's which
  3560.            currently have n == 0.  */
  3561.         case no_op:
  3562.           DEBUG_PRINT1 ("EXECUTING no_op.\n");
  3563.           break;
  3564.  
  3565.  
  3566.         /* Match the next n pattern characters exactly.  The following
  3567.            byte in the pattern defines n, and the n bytes after that
  3568.            are the characters to match.  */
  3569.     case exactn:
  3570.       mcnt = *p++;
  3571.           DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
  3572.  
  3573.           /* This is written out as an if-else so we don't waste time
  3574.              testing `translate' inside the loop.  */
  3575.           if (translate)
  3576.         {
  3577.           do
  3578.         {
  3579.           PREFETCH ();
  3580.           if (translate[(unsigned char) *d++] != (char) *p++)
  3581.                     goto fail;
  3582.         }
  3583.           while (--mcnt);
  3584.         }
  3585.       else
  3586.         {
  3587.           do
  3588.         {
  3589.           PREFETCH ();
  3590.           if (*d++ != (char) *p++) goto fail;
  3591.         }
  3592.           while (--mcnt);
  3593.         }
  3594.       SET_REGS_MATCHED ();
  3595.           break;
  3596.  
  3597.  
  3598.         /* Match any character except possibly a newline or a null.  */
  3599.     case anychar:
  3600.           DEBUG_PRINT1 ("EXECUTING anychar.\n");
  3601.  
  3602.           PREFETCH ();
  3603.  
  3604.           if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
  3605.               || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
  3606.         goto fail;
  3607.  
  3608.           SET_REGS_MATCHED ();
  3609.           DEBUG_PRINT2 ("  Matched `%d'.\n", *d);
  3610.           d++;
  3611.       break;
  3612.  
  3613.  
  3614.     case charset:
  3615.     case charset_not:
  3616.       {
  3617.         register unsigned char c;
  3618.         boolean not = (re_opcode_t) *(p - 1) == charset_not;
  3619.  
  3620.             DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
  3621.  
  3622.         PREFETCH ();
  3623.         c = TRANSLATE (*d); /* The character to match.  */
  3624.  
  3625.             /* Cast to `unsigned' instead of `unsigned char' in case the
  3626.                bit list is a full 32 bytes long.  */
  3627.         if (c < (unsigned) (*p * BYTEWIDTH)
  3628.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  3629.           not = !not;
  3630.  
  3631.         p += 1 + *p;
  3632.  
  3633.         if (!not) goto fail;
  3634.             
  3635.         SET_REGS_MATCHED ();
  3636.             d++;
  3637.         break;
  3638.       }
  3639.  
  3640.  
  3641.         /* The beginning of a group is represented by start_memory.
  3642.            The arguments are the register number in the next byte, and the
  3643.            number of groups inner to this one in the next.  The text
  3644.            matched within the group is recorded (in the internal
  3645.            registers data structure) under the register number.  */
  3646.         case start_memory:
  3647.       DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
  3648.  
  3649.           /* Find out if this group can match the empty string.  */
  3650.       p1 = p;        /* To send to group_match_null_string_p.  */
  3651.           
  3652.           if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
  3653.             REG_MATCH_NULL_STRING_P (reg_info[*p]) 
  3654.               = group_match_null_string_p (&p1, pend, reg_info);
  3655.  
  3656.           /* Save the position in the string where we were the last time
  3657.              we were at this open-group operator in case the group is
  3658.              operated upon by a repetition operator, e.g., with `(a*)*b'
  3659.              against `ab'; then we want to ignore where we are now in
  3660.              the string in case this attempt to match fails.  */
  3661.           old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3662.                              ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
  3663.                              : regstart[*p];
  3664.       DEBUG_PRINT2 ("  old_regstart: %d\n", 
  3665.              POINTER_TO_OFFSET (old_regstart[*p]));
  3666.  
  3667.           regstart[*p] = d;
  3668.       DEBUG_PRINT2 ("  regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
  3669.  
  3670.           IS_ACTIVE (reg_info[*p]) = 1;
  3671.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  3672.           
  3673.           /* This is the new highest active register.  */
  3674.           highest_active_reg = *p;
  3675.           
  3676.           /* If nothing was active before, this is the new lowest active
  3677.              register.  */
  3678.           if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  3679.             lowest_active_reg = *p;
  3680.  
  3681.           /* Move past the register number and inner group count.  */
  3682.           p += 2;
  3683.           break;
  3684.  
  3685.  
  3686.         /* The stop_memory opcode represents the end of a group.  Its
  3687.            arguments are the same as start_memory's: the register
  3688.            number, and the number of inner groups.  */
  3689.     case stop_memory:
  3690.       DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
  3691.              
  3692.           /* We need to save the string position the last time we were at
  3693.              this close-group operator in case the group is operated
  3694.              upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
  3695.              against `aba'; then we want to ignore where we are now in
  3696.              the string in case this attempt to match fails.  */
  3697.           old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3698.                            ? REG_UNSET (regend[*p]) ? d : regend[*p]
  3699.                : regend[*p];
  3700.       DEBUG_PRINT2 ("      old_regend: %d\n", 
  3701.              POINTER_TO_OFFSET (old_regend[*p]));
  3702.  
  3703.           regend[*p] = d;
  3704.       DEBUG_PRINT2 ("      regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
  3705.  
  3706.           /* This register isn't active anymore.  */
  3707.           IS_ACTIVE (reg_info[*p]) = 0;
  3708.           
  3709.           /* If this was the only register active, nothing is active
  3710.              anymore.  */
  3711.           if (lowest_active_reg == highest_active_reg)
  3712.             {
  3713.               lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3714.               highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3715.             }
  3716.           else
  3717.             { /* We must scan for the new highest active register, since
  3718.                  it isn't necessarily one less than now: consider
  3719.                  (a(b)c(d(e)f)g).  When group 3 ends, after the f), the
  3720.                  new highest active register is 1.  */
  3721.               unsigned char r = *p - 1;
  3722.               while (r > 0 && !IS_ACTIVE (reg_info[r]))
  3723.                 r--;
  3724.               
  3725.               /* If we end up at register zero, that means that we saved
  3726.                  the registers as the result of an `on_failure_jump', not
  3727.                  a `start_memory', and we jumped to past the innermost
  3728.                  `stop_memory'.  For example, in ((.)*) we save
  3729.                  registers 1 and 2 as a result of the *, but when we pop
  3730.                  back to the second ), we are at the stop_memory 1.
  3731.                  Thus, nothing is active.  */
  3732.           if (r == 0)
  3733.                 {
  3734.                   lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3735.                   highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3736.                 }
  3737.               else
  3738.                 highest_active_reg = r;
  3739.             }
  3740.           
  3741.           /* If just failed to match something this time around with a
  3742.              group that's operated on by a repetition operator, try to
  3743.              force exit from the ``loop'', and restore the register
  3744.              information for this group that we had before trying this
  3745.              last match.  */
  3746.           if ((!MATCHED_SOMETHING (reg_info[*p])
  3747.                || (re_opcode_t) p[-3] == start_memory)
  3748.           && (p + 2) < pend)              
  3749.             {
  3750.               boolean is_a_jump_n = false;
  3751.               
  3752.               p1 = p + 2;
  3753.               mcnt = 0;
  3754.               switch ((re_opcode_t) *p1++)
  3755.                 {
  3756.                   case jump_n:
  3757.             is_a_jump_n = true;
  3758.                   case pop_failure_jump:
  3759.           case maybe_pop_jump:
  3760.           case jump:
  3761.           case dummy_failure_jump:
  3762.                     EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3763.             if (is_a_jump_n)
  3764.               p1 += 2;
  3765.                     break;
  3766.                   
  3767.                   default:
  3768.                     /* do nothing */ ;
  3769.                 }
  3770.           p1 += mcnt;
  3771.         
  3772.               /* If the next operation is a jump backwards in the pattern
  3773.              to an on_failure_jump right before the start_memory
  3774.                  corresponding to this stop_memory, exit from the loop
  3775.                  by forcing a failure after pushing on the stack the
  3776.                  on_failure_jump's jump in the pattern, and d.  */
  3777.               if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
  3778.                   && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
  3779.         {
  3780.                   /* If this group ever matched anything, then restore
  3781.                      what its registers were before trying this last
  3782.                      failed match, e.g., with `(a*)*b' against `ab' for
  3783.                      regstart[1], and, e.g., with `((a*)*(b*)*)*'
  3784.                      against `aba' for regend[3].
  3785.                      
  3786.                      Also restore the registers for inner groups for,
  3787.                      e.g., `((a*)(b*))*' against `aba' (register 3 would
  3788.                      otherwise get trashed).  */
  3789.                      
  3790.                   if (EVER_MATCHED_SOMETHING (reg_info[*p]))
  3791.             {
  3792.               unsigned r; 
  3793.         
  3794.                       EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
  3795.                       
  3796.               /* Restore this and inner groups' (if any) registers.  */
  3797.                       for (r = *p; r < *p + *(p + 1); r++)
  3798.                         {
  3799.                           regstart[r] = old_regstart[r];
  3800.  
  3801.                           /* xx why this test?  */
  3802.                           if ((int) old_regend[r] >= (int) regstart[r])
  3803.                             regend[r] = old_regend[r];
  3804.                         }     
  3805.                     }
  3806.           p1++;
  3807.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3808.                   PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
  3809.  
  3810.                   goto fail;
  3811.                 }
  3812.             }
  3813.           
  3814.           /* Move past the register number and the inner group count.  */
  3815.           p += 2;
  3816.           break;
  3817.  
  3818.  
  3819.     /* \<digit> has been turned into a `duplicate' command which is
  3820.            followed by the numeric value of <digit> as the register number.  */
  3821.         case duplicate:
  3822.       {
  3823.         register const char *d2, *dend2;
  3824.         int regno = *p++;   /* Get which register to match against.  */
  3825.         DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
  3826.  
  3827.         /* Can't back reference a group which we've never matched.  */
  3828.             if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
  3829.               goto fail;
  3830.               
  3831.             /* Where in input to try to start matching.  */
  3832.             d2 = regstart[regno];
  3833.             
  3834.             /* Where to stop matching; if both the place to start and
  3835.                the place to stop matching are in the same string, then
  3836.                set to the place to stop, otherwise, for now have to use
  3837.                the end of the first string.  */
  3838.  
  3839.             dend2 = ((FIRST_STRING_P (regstart[regno]) 
  3840.               == FIRST_STRING_P (regend[regno]))
  3841.              ? regend[regno] : end_match_1);
  3842.         for (;;)
  3843.           {
  3844.         /* If necessary, advance to next segment in register
  3845.                    contents.  */
  3846.         while (d2 == dend2)
  3847.           {
  3848.             if (dend2 == end_match_2) break;
  3849.             if (dend2 == regend[regno]) break;
  3850.  
  3851.                     /* End of string1 => advance to string2. */
  3852.                     d2 = string2;
  3853.                     dend2 = regend[regno];
  3854.           }
  3855.         /* At end of register contents => success */
  3856.         if (d2 == dend2) break;
  3857.  
  3858.         /* If necessary, advance to next segment in data.  */
  3859.         PREFETCH ();
  3860.  
  3861.         /* How many characters left in this segment to match.  */
  3862.         mcnt = dend - d;
  3863.                 
  3864.         /* Want how many consecutive characters we can match in
  3865.                    one shot, so, if necessary, adjust the count.  */
  3866.                 if (mcnt > dend2 - d2)
  3867.           mcnt = dend2 - d2;
  3868.                   
  3869.         /* Compare that many; failure if mismatch, else move
  3870.                    past them.  */
  3871.         if (translate 
  3872.                     ? bcmp_translate (d, d2, mcnt, translate) 
  3873.                     : bcmp (d, d2, mcnt))
  3874.           goto fail;
  3875.         d += mcnt, d2 += mcnt;
  3876.           }
  3877.       }
  3878.       break;
  3879.  
  3880.  
  3881.         /* begline matches the empty string at the beginning of the string
  3882.            (unless `not_bol' is set in `bufp'), and, if
  3883.            `newline_anchor' is set, after newlines.  */
  3884.     case begline:
  3885.           DEBUG_PRINT1 ("EXECUTING begline.\n");
  3886.           
  3887.           if (AT_STRINGS_BEG (d))
  3888.             {
  3889.               if (!bufp->not_bol) break;
  3890.             }
  3891.           else if (d[-1] == '\n' && bufp->newline_anchor)
  3892.             {
  3893.               break;
  3894.             }
  3895.           /* In all other cases, we fail.  */
  3896.           goto fail;
  3897.  
  3898.  
  3899.         /* endline is the dual of begline.  */
  3900.     case endline:
  3901.           DEBUG_PRINT1 ("EXECUTING endline.\n");
  3902.  
  3903.           if (AT_STRINGS_END (d))
  3904.             {
  3905.               if (!bufp->not_eol) break;
  3906.             }
  3907.           
  3908.           /* We have to ``prefetch'' the next character.  */
  3909.           else if ((d == end1 ? *string2 : *d) == '\n'
  3910.                    && bufp->newline_anchor)
  3911.             {
  3912.               break;
  3913.             }
  3914.           goto fail;
  3915.  
  3916.  
  3917.     /* Match at the very beginning of the data.  */
  3918.         case begbuf:
  3919.           DEBUG_PRINT1 ("EXECUTING begbuf.\n");
  3920.           if (AT_STRINGS_BEG (d))
  3921.             break;
  3922.           goto fail;
  3923.  
  3924.  
  3925.     /* Match at the very end of the data.  */
  3926.         case endbuf:
  3927.           DEBUG_PRINT1 ("EXECUTING endbuf.\n");
  3928.       if (AT_STRINGS_END (d))
  3929.         break;
  3930.           goto fail;
  3931.  
  3932.  
  3933.         /* on_failure_keep_string_jump is used to optimize `.*\n'.  It
  3934.            pushes NULL as the value for the string on the stack.  Then
  3935.            `pop_failure_point' will keep the current value for the
  3936.            string, instead of restoring it.  To see why, consider
  3937.            matching `foo\nbar' against `.*\n'.  The .* matches the foo;
  3938.            then the . fails against the \n.  But the next thing we want
  3939.            to do is match the \n against the \n; if we restored the
  3940.            string value, we would be back at the foo.
  3941.            
  3942.            Because this is used only in specific cases, we don't need to
  3943.            check all the things that `on_failure_jump' does, to make
  3944.            sure the right things get saved on the stack.  Hence we don't
  3945.            share its code.  The only reason to push anything on the
  3946.            stack at all is that otherwise we would have to change
  3947.            `anychar's code to do something besides goto fail in this
  3948.            case; that seems worse than this.  */
  3949.         case on_failure_keep_string_jump:
  3950.           DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
  3951.           
  3952.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3953.           DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
  3954.  
  3955.           PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
  3956.           break;
  3957.  
  3958.  
  3959.     /* Uses of on_failure_jump:
  3960.         
  3961.            Each alternative starts with an on_failure_jump that points
  3962.            to the beginning of the next alternative.  Each alternative
  3963.            except the last ends with a jump that in effect jumps past
  3964.            the rest of the alternatives.  (They really jump to the
  3965.            ending jump of the following alternative, because tensioning
  3966.            these jumps is a hassle.)
  3967.  
  3968.            Repeats start with an on_failure_jump that points past both
  3969.            the repetition text and either the following jump or
  3970.            pop_failure_jump back to this on_failure_jump.  */
  3971.     case on_failure_jump:
  3972.         on_failure:
  3973.           DEBUG_PRINT1 ("EXECUTING on_failure_jump");
  3974.  
  3975.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3976.           DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
  3977.  
  3978.           /* If this on_failure_jump comes right before a group (i.e.,
  3979.              the original * applied to a group), save the information
  3980.              for that group and all inner ones, so that if we fail back
  3981.              to this point, the group's information will be correct.
  3982.              For example, in \(a*\)*\1, we need the preceding group,
  3983.              and in \(\(a*\)b*\)\2, we need the inner group.  */
  3984.  
  3985.           /* We can't use `p' to check ahead because we push
  3986.              a failure point to `p + mcnt' after we do this.  */
  3987.           p1 = p;
  3988.  
  3989.           /* We need to skip no_op's before we look for the
  3990.              start_memory in case this on_failure_jump is happening as
  3991.              the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
  3992.              against aba.  */
  3993.           while (p1 < pend && (re_opcode_t) *p1 == no_op)
  3994.             p1++;
  3995.  
  3996.           if (p1 < pend && (re_opcode_t) *p1 == start_memory)
  3997.             {
  3998.               /* We have a new highest active register now.  This will
  3999.                  get reset at the start_memory we are about to get to,
  4000.                  but we will have saved all the registers relevant to
  4001.                  this repetition op, as described above.  */
  4002.               highest_active_reg = *(p1 + 1) + *(p1 + 2);
  4003.               if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  4004.                 lowest_active_reg = *(p1 + 1);
  4005.             }
  4006.  
  4007.           DEBUG_PRINT1 (":\n");
  4008.           PUSH_FAILURE_POINT (p + mcnt, d, -2);
  4009.           break;
  4010.  
  4011.  
  4012.         /* A smart repeat ends with `maybe_pop_jump'.
  4013.        We change it to either `pop_failure_jump' or `jump'.  */
  4014.         case maybe_pop_jump:
  4015.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4016.           DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
  4017.           {
  4018.         register unsigned char *p2 = p;
  4019.  
  4020.             /* Compare the beginning of the repeat with what in the
  4021.                pattern follows its end. If we can establish that there
  4022.                is nothing that they would both match, i.e., that we
  4023.                would have to backtrack because of (as in, e.g., `a*a')
  4024.                then we can change to pop_failure_jump, because we'll
  4025.                never have to backtrack.
  4026.                
  4027.                This is not true in the case of alternatives: in
  4028.                `(a|ab)*' we do need to backtrack to the `ab' alternative
  4029.                (e.g., if the string was `ab').  But instead of trying to
  4030.                detect that here, the alternative has put on a dummy
  4031.                failure point which is what we will end up popping.  */
  4032.  
  4033.         /* Skip over open/close-group commands.  */
  4034.         while (p2 + 2 < pend
  4035.            && ((re_opcode_t) *p2 == stop_memory
  4036.                || (re_opcode_t) *p2 == start_memory))
  4037.           p2 += 3;            /* Skip over args, too.  */
  4038.  
  4039.             /* If we're at the end of the pattern, we can change.  */
  4040.             if (p2 == pend)
  4041.           {
  4042.         /* Consider what happens when matching ":\(.*\)"
  4043.            against ":/".  I don't really understand this code
  4044.            yet.  */
  4045.               p[-3] = (unsigned char) pop_failure_jump;
  4046.                 DEBUG_PRINT1
  4047.                   ("  End of pattern: change to `pop_failure_jump'.\n");
  4048.               }
  4049.  
  4050.             else if ((re_opcode_t) *p2 == exactn
  4051.              || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
  4052.           {
  4053.         register unsigned char c
  4054.                   = *p2 == (unsigned char) endline ? '\n' : p2[2];
  4055.         p1 = p + mcnt;
  4056.  
  4057.                 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
  4058.                    to the `maybe_finalize_jump' of this case.  Examine what 
  4059.                    follows.  */
  4060.                 if ((re_opcode_t) p1[3] == exactn && p1[5] != c)
  4061.                   {
  4062.               p[-3] = (unsigned char) pop_failure_jump;
  4063.                     DEBUG_PRINT3 ("  %c != %c => pop_failure_jump.\n",
  4064.                                   c, p1[5]);
  4065.                   }
  4066.                   
  4067.         else if ((re_opcode_t) p1[3] == charset
  4068.              || (re_opcode_t) p1[3] == charset_not)
  4069.           {
  4070.             int not = (re_opcode_t) p1[3] == charset_not;
  4071.                     
  4072.             if (c < (unsigned char) (p1[4] * BYTEWIDTH)
  4073.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  4074.               not = !not;
  4075.  
  4076.                     /* `not' is equal to 1 if c would match, which means
  4077.                         that we can't change to pop_failure_jump.  */
  4078.             if (!not)
  4079.                       {
  4080.                   p[-3] = (unsigned char) pop_failure_jump;
  4081.                         DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
  4082.                       }
  4083.           }
  4084.           }
  4085.       }
  4086.       p -= 2;        /* Point at relative address again.  */
  4087.       if ((re_opcode_t) p[-1] != pop_failure_jump)
  4088.         {
  4089.           p[-1] = (unsigned char) jump;
  4090.               DEBUG_PRINT1 ("  Match => jump.\n");
  4091.           goto unconditional_jump;
  4092.         }
  4093.         /* Note fall through.  */
  4094.  
  4095.  
  4096.     /* The end of a simple repeat has a pop_failure_jump back to
  4097.            its matching on_failure_jump, where the latter will push a
  4098.            failure point.  The pop_failure_jump takes off failure
  4099.            points put on by this pop_failure_jump's matching
  4100.            on_failure_jump; we got through the pattern to here from the
  4101.            matching on_failure_jump, so didn't fail.  */
  4102.         case pop_failure_jump:
  4103.           {
  4104.             /* We need to pass separate storage for the lowest and
  4105.                highest registers, even though we don't care about the
  4106.                actual values.  Otherwise, we will restore only one
  4107.                register from the stack, since lowest will == highest in
  4108.                `pop_failure_point'.  */
  4109.             unsigned dummy_low_reg, dummy_high_reg;
  4110.             unsigned char *pdummy;
  4111.             const char *sdummy;
  4112.  
  4113.             DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
  4114.             POP_FAILURE_POINT (sdummy, pdummy,
  4115.                                dummy_low_reg, dummy_high_reg,
  4116.                                reg_dummy, reg_dummy, reg_info_dummy);
  4117.           }
  4118.           /* Note fall through.  */
  4119.  
  4120.           
  4121.         /* Unconditionally jump (without popping any failure points).  */
  4122.         case jump:
  4123.     unconditional_jump:
  4124.       EXTRACT_NUMBER_AND_INCR (mcnt, p);    /* Get the amount to jump.  */
  4125.           DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
  4126.       p += mcnt;                /* Do the jump.  */
  4127.           DEBUG_PRINT2 ("(to 0x%x).\n", p);
  4128.       break;
  4129.  
  4130.     
  4131.         /* We need this opcode so we can detect where alternatives end
  4132.            in `group_match_null_string_p' et al.  */
  4133.         case jump_past_alt:
  4134.           DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
  4135.           goto unconditional_jump;
  4136.  
  4137.  
  4138.         /* Normally, the on_failure_jump pushes a failure point, which
  4139.            then gets popped at pop_failure_jump.  We will end up at
  4140.            pop_failure_jump, also, and with a pattern of, say, `a+', we
  4141.            are skipping over the on_failure_jump, so we have to push
  4142.            something meaningless for pop_failure_jump to pop.  */
  4143.         case dummy_failure_jump:
  4144.           DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
  4145.           /* It doesn't matter what we push for the string here.  What
  4146.              the code at `fail' tests is the value for the pattern.  */
  4147.           PUSH_FAILURE_POINT (0, 0, -2);
  4148.           goto unconditional_jump;
  4149.  
  4150.  
  4151.         /* At the end of an alternative, we need to push a dummy failure
  4152.            point in case we are followed by a `pop_failure_jump', because
  4153.            we don't want the failure point for the alternative to be
  4154.            popped.  For example, matching `(a|ab)*' against `aab'
  4155.            requires that we match the `ab' alternative.  */
  4156.         case push_dummy_failure:
  4157.           DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
  4158.           /* See comments just above at `dummy_failure_jump' about the
  4159.              two zeroes.  */
  4160.           PUSH_FAILURE_POINT (0, 0, -2);
  4161.           break;
  4162.  
  4163.         /* Have to succeed matching what follows at least n times.
  4164.            After that, handle like `on_failure_jump'.  */
  4165.         case succeed_n: 
  4166.           EXTRACT_NUMBER (mcnt, p + 2);
  4167.           DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
  4168.  
  4169.           assert (mcnt >= 0);
  4170.           /* Originally, this is how many times we HAVE to succeed.  */
  4171.           if (mcnt > 0)
  4172.             {
  4173.                mcnt--;
  4174.            p += 2;
  4175.                STORE_NUMBER_AND_INCR (p, mcnt);
  4176.                DEBUG_PRINT3 ("  Setting 0x%x to %d.\n", p, mcnt);
  4177.             }
  4178.       else if (mcnt == 0)
  4179.             {
  4180.               DEBUG_PRINT2 ("  Setting two bytes from 0x%x to no_op.\n", p+2);
  4181.           p[2] = (unsigned char) no_op;
  4182.               p[3] = (unsigned char) no_op;
  4183.               goto on_failure;
  4184.             }
  4185.           break;
  4186.         
  4187.         case jump_n: 
  4188.           EXTRACT_NUMBER (mcnt, p + 2);
  4189.           DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
  4190.  
  4191.           /* Originally, this is how many times we CAN jump.  */
  4192.           if (mcnt)
  4193.             {
  4194.                mcnt--;
  4195.                STORE_NUMBER (p + 2, mcnt);
  4196.            goto unconditional_jump;         
  4197.             }
  4198.           /* If don't have to jump any more, skip over the rest of command.  */
  4199.       else      
  4200.         p += 4;             
  4201.           break;
  4202.         
  4203.     case set_number_at:
  4204.       {
  4205.             DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
  4206.  
  4207.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4208.             p1 = p + mcnt;
  4209.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4210.             DEBUG_PRINT3 ("  Setting 0x%x to %d.\n", p1, mcnt);
  4211.         STORE_NUMBER (p1, mcnt);
  4212.             break;
  4213.           }
  4214.  
  4215.         case wordbound:
  4216.           DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  4217.           if (AT_WORD_BOUNDARY (d))
  4218.         break;
  4219.           goto fail;
  4220.  
  4221.     case notwordbound:
  4222.           DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  4223.       if (AT_WORD_BOUNDARY (d))
  4224.         goto fail;
  4225.           break;
  4226.  
  4227.     case wordbeg:
  4228.           DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
  4229.       if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
  4230.         break;
  4231.           goto fail;
  4232.  
  4233.     case wordend:
  4234.           DEBUG_PRINT1 ("EXECUTING wordend.\n");
  4235.       if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
  4236.               && (!WORDCHAR_P (d) || AT_STRINGS_END (d)))
  4237.         break;
  4238.           goto fail;
  4239.  
  4240. #ifdef emacs
  4241. #ifdef emacs19
  4242.       case before_dot:
  4243.           DEBUG_PRINT1 ("EXECUTING before_dot.\n");
  4244.        if (PTR_CHAR_POS ((unsigned char *) d) >= point)
  4245.           goto fail;
  4246.         break;
  4247.   
  4248.       case at_dot:
  4249.           DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  4250.        if (PTR_CHAR_POS ((unsigned char *) d) != point)
  4251.           goto fail;
  4252.         break;
  4253.   
  4254.       case after_dot:
  4255.           DEBUG_PRINT1 ("EXECUTING after_dot.\n");
  4256.           if (PTR_CHAR_POS ((unsigned char *) d) <= point)
  4257.           goto fail;
  4258.         break;
  4259. #else /* not emacs19 */
  4260.     case at_dot:
  4261.           DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  4262.       if (PTR_CHAR_POS ((unsigned char *) d) + 1 != point)
  4263.         goto fail;
  4264.       break;
  4265. #endif /* not emacs19 */
  4266.  
  4267.     case syntaxspec:
  4268.           DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
  4269.       mcnt = *p++;
  4270.       goto matchsyntax;
  4271.  
  4272.         case wordchar:
  4273.           DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
  4274.       mcnt = (int) Sword;
  4275.         matchsyntax:
  4276.       PREFETCH ();
  4277.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt)
  4278.             goto fail;
  4279.           SET_REGS_MATCHED ();
  4280.       break;
  4281.  
  4282.     case notsyntaxspec:
  4283.           DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
  4284.       mcnt = *p++;
  4285.       goto matchnotsyntax;
  4286.  
  4287.         case notwordchar:
  4288.           DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
  4289.       mcnt = (int) Sword;
  4290.         matchnotsyntax:
  4291.       PREFETCH ();
  4292.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt)
  4293.             goto fail;
  4294.       SET_REGS_MATCHED ();
  4295.           break;
  4296.  
  4297. #else /* not emacs */
  4298.     case wordchar:
  4299.           DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
  4300.       PREFETCH ();
  4301.           if (!WORDCHAR_P (d))
  4302.             goto fail;
  4303.       SET_REGS_MATCHED ();
  4304.           d++;
  4305.       break;
  4306.       
  4307.     case notwordchar:
  4308.           DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
  4309.       PREFETCH ();
  4310.       if (WORDCHAR_P (d))
  4311.             goto fail;
  4312.           SET_REGS_MATCHED ();
  4313.           d++;
  4314.       break;
  4315. #endif /* not emacs */
  4316.           
  4317.         default:
  4318.           abort ();
  4319.     }
  4320.       continue;  /* Successfully executed one pattern command; keep going.  */
  4321.  
  4322.  
  4323.     /* We goto here if a matching operation fails. */
  4324.     fail:
  4325.       if (!FAIL_STACK_EMPTY ())
  4326.     { /* A restart point is known.  Restore to that state.  */
  4327.           DEBUG_PRINT1 ("\nFAIL:\n");
  4328.           POP_FAILURE_POINT (d, p,
  4329.                              lowest_active_reg, highest_active_reg,
  4330.                              regstart, regend, reg_info);
  4331.  
  4332.           /* If this failure point is a dummy, try the next one.  */
  4333.           if (!p)
  4334.         goto fail;
  4335.  
  4336.           /* If we failed to the end of the pattern, don't examine *p.  */
  4337.       assert (p <= pend);
  4338.           if (p < pend)
  4339.             {
  4340.               boolean is_a_jump_n = false;
  4341.               
  4342.               /* If failed to a backwards jump that's part of a repetition
  4343.                  loop, need to pop this failure point and use the next one.  */
  4344.               switch ((re_opcode_t) *p)
  4345.                 {
  4346.                 case jump_n:
  4347.                   is_a_jump_n = true;
  4348.                 case maybe_pop_jump:
  4349.                 case pop_failure_jump:
  4350.                 case jump:
  4351.                   p1 = p + 1;
  4352.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4353.                   p1 += mcnt;    
  4354.  
  4355.                   if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
  4356.                       || (!is_a_jump_n
  4357.                           && (re_opcode_t) *p1 == on_failure_jump))
  4358.                     goto fail;
  4359.                   break;
  4360.                 default:
  4361.                   /* do nothing */ ;
  4362.                 }
  4363.             }
  4364.  
  4365.           if (d >= string1 && d <= end1)
  4366.         dend = end_match_1;
  4367.         }
  4368.       else
  4369.         break;   /* Matching at this starting point really fails.  */
  4370.     } /* for (;;) */
  4371.  
  4372.   if (best_regs_set)
  4373.     goto restore_best_regs;
  4374.  
  4375.   FREE_VARIABLES ();
  4376.  
  4377.   return -1;                     /* Failure to match.  */
  4378. } /* re_match_2 */
  4379.  
  4380. /* Subroutine definitions for re_match_2.  */
  4381.  
  4382.  
  4383. /* We are passed P pointing to a register number after a start_memory.
  4384.    
  4385.    Return true if the pattern up to the corresponding stop_memory can
  4386.    match the empty string, and false otherwise.
  4387.    
  4388.    If we find the matching stop_memory, sets P to point to one past its number.
  4389.    Otherwise, sets P to an undefined byte less than or equal to END.
  4390.  
  4391.    We don't handle duplicates properly (yet).  */
  4392.  
  4393. static boolean
  4394. group_match_null_string_p (p, end, reg_info)
  4395.     unsigned char **p, *end;
  4396.     register_info_type *reg_info;
  4397. {
  4398.   int mcnt;
  4399.   /* Point to after the args to the start_memory.  */
  4400.   unsigned char *p1 = *p + 2;
  4401.   
  4402.   while (p1 < end)
  4403.     {
  4404.       /* Skip over opcodes that can match nothing, and return true or
  4405.      false, as appropriate, when we get to one that can't, or to the
  4406.          matching stop_memory.  */
  4407.       
  4408.       switch ((re_opcode_t) *p1)
  4409.         {
  4410.         /* Could be either a loop or a series of alternatives.  */
  4411.         case on_failure_jump:
  4412.           p1++;
  4413.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4414.           
  4415.           /* If the next operation is not a jump backwards in the
  4416.          pattern.  */
  4417.  
  4418.       if (mcnt >= 0)
  4419.         {
  4420.               /* Go through the on_failure_jumps of the alternatives,
  4421.                  seeing if any of the alternatives cannot match nothing.
  4422.                  The last alternative starts with only a jump,
  4423.                  whereas the rest start with on_failure_jump and end
  4424.                  with a jump, e.g., here is the pattern for `a|b|c':
  4425.  
  4426.                  /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
  4427.                  /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
  4428.                  /exactn/1/c                        
  4429.  
  4430.                  So, we have to first go through the first (n-1)
  4431.                  alternatives and then deal with the last one separately.  */
  4432.  
  4433.  
  4434.               /* Deal with the first (n-1) alternatives, which start
  4435.                  with an on_failure_jump (see above) that jumps to right
  4436.                  past a jump_past_alt.  */
  4437.  
  4438.               while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
  4439.                 {
  4440.                   /* `mcnt' holds how many bytes long the alternative
  4441.                      is, including the ending `jump_past_alt' and
  4442.                      its number.  */
  4443.  
  4444.                   if (!alt_match_null_string_p (p1, p1 + mcnt - 3, 
  4445.                                       reg_info))
  4446.                     return false;
  4447.  
  4448.                   /* Move to right after this alternative, including the
  4449.              jump_past_alt.  */
  4450.                   p1 += mcnt;    
  4451.  
  4452.                   /* Break if it's the beginning of an n-th alternative
  4453.                      that doesn't begin with an on_failure_jump.  */
  4454.                   if ((re_opcode_t) *p1 != on_failure_jump)
  4455.                     break;
  4456.         
  4457.           /* Still have to check that it's not an n-th
  4458.              alternative that starts with an on_failure_jump.  */
  4459.           p1++;
  4460.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4461.                   if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
  4462.                     {
  4463.               /* Get to the beginning of the n-th alternative.  */
  4464.                       p1 -= 3;
  4465.                       break;
  4466.                     }
  4467.                 }
  4468.  
  4469.               /* Deal with the last alternative: go back and get number
  4470.                  of the `jump_past_alt' just before it.  `mcnt' contains
  4471.                  the length of the alternative.  */
  4472.               EXTRACT_NUMBER (mcnt, p1 - 2);
  4473.  
  4474.               if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
  4475.                 return false;
  4476.  
  4477.               p1 += mcnt;    /* Get past the n-th alternative.  */
  4478.             } /* if mcnt > 0 */
  4479.           break;
  4480.  
  4481.           
  4482.         case stop_memory:
  4483.       assert (p1[1] == **p);
  4484.           *p = p1 + 2;
  4485.           return true;
  4486.  
  4487.         
  4488.         default: 
  4489.           if (!common_op_match_null_string_p (&p1, end, reg_info))
  4490.             return false;
  4491.         }
  4492.     } /* while p1 < end */
  4493.  
  4494.   return false;
  4495. } /* group_match_null_string_p */
  4496.  
  4497.  
  4498. /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
  4499.    It expects P to be the first byte of a single alternative and END one
  4500.    byte past the last. The alternative can contain groups.  */
  4501.    
  4502. static boolean
  4503. alt_match_null_string_p (p, end, reg_info)
  4504.     unsigned char *p, *end;
  4505.     register_info_type *reg_info;
  4506. {
  4507.   int mcnt;
  4508.   unsigned char *p1 = p;
  4509.   
  4510.   while (p1 < end)
  4511.     {
  4512.       /* Skip over opcodes that can match nothing, and break when we get 
  4513.          to one that can't.  */
  4514.       
  4515.       switch ((re_opcode_t) *p1)
  4516.         {
  4517.     /* It's a loop.  */
  4518.         case on_failure_jump:
  4519.           p1++;
  4520.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4521.           p1 += mcnt;
  4522.           break;
  4523.           
  4524.     default: 
  4525.           if (!common_op_match_null_string_p (&p1, end, reg_info))
  4526.             return false;
  4527.         }
  4528.     }  /* while p1 < end */
  4529.  
  4530.   return true;
  4531. } /* alt_match_null_string_p */
  4532.  
  4533.  
  4534. /* Deals with the ops common to group_match_null_string_p and
  4535.    alt_match_null_string_p.  
  4536.    
  4537.    Sets P to one after the op and its arguments, if any.  */
  4538.  
  4539. static boolean
  4540. common_op_match_null_string_p (p, end, reg_info)
  4541.     unsigned char **p, *end;
  4542.     register_info_type *reg_info;
  4543. {
  4544.   int mcnt;
  4545.   boolean ret;
  4546.   int reg_no;
  4547.   unsigned char *p1 = *p;
  4548.  
  4549.   switch ((re_opcode_t) *p1++)
  4550.     {
  4551.     case no_op:
  4552.     case begline:
  4553.     case endline:
  4554.     case begbuf:
  4555.     case endbuf:
  4556.     case wordbeg:
  4557.     case wordend:
  4558.     case wordbound:
  4559.     case notwordbound:
  4560. #ifdef emacs
  4561.     case before_dot:
  4562.     case at_dot:
  4563.     case after_dot:
  4564. #endif
  4565.       break;
  4566.  
  4567.     case start_memory:
  4568.       reg_no = *p1;
  4569.       assert (reg_no > 0 && reg_no <= MAX_REGNUM);
  4570.       ret = group_match_null_string_p (&p1, end, reg_info);
  4571.       
  4572.       /* Have to set this here in case we're checking a group which
  4573.          contains a group and a back reference to it.  */
  4574.  
  4575.       if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
  4576.         REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
  4577.  
  4578.       if (!ret)
  4579.         return false;
  4580.       break;
  4581.           
  4582.     /* If this is an optimized succeed_n for zero times, make the jump.  */
  4583.     case jump:
  4584.       EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4585.       if (mcnt >= 0)
  4586.         p1 += mcnt;
  4587.       else
  4588.         return false;
  4589.       break;
  4590.  
  4591.     case succeed_n:
  4592.       /* Get to the number of times to succeed.  */
  4593.       p1 += 2;        
  4594.       EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4595.  
  4596.       if (mcnt == 0)
  4597.         {
  4598.           p1 -= 4;
  4599.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4600.           p1 += mcnt;
  4601.         }
  4602.       else
  4603.         return false;
  4604.       break;
  4605.  
  4606.     case duplicate: 
  4607.       if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
  4608.         return false;
  4609.       break;
  4610.  
  4611.     case set_number_at:
  4612.       p1 += 4;
  4613.  
  4614.     default:
  4615.       /* All other opcodes mean we cannot match the empty string.  */
  4616.       return false;
  4617.   }
  4618.  
  4619.   *p = p1;
  4620.   return true;
  4621. } /* common_op_match_null_string_p */
  4622.  
  4623.  
  4624. /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
  4625.    bytes; nonzero otherwise.  */
  4626.    
  4627. static int
  4628. bcmp_translate (s1, s2, len, translate)
  4629.      unsigned char *s1, *s2;
  4630.      register int len;
  4631.      char *translate;
  4632. {
  4633.   register unsigned char *p1 = s1, *p2 = s2;
  4634.   while (len)
  4635.     {
  4636.       if (translate[*p1++] != translate[*p2++]) return 1;
  4637.       len--;
  4638.     }
  4639.   return 0;
  4640. }
  4641.  
  4642. /* Entry points for GNU code.  */
  4643.  
  4644. /* re_compile_pattern is the GNU regular expression compiler: it
  4645.    compiles PATTERN (of length SIZE) and puts the result in BUFP.
  4646.    Returns 0 if the pattern was valid, otherwise an error string.
  4647.    
  4648.    Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  4649.    are set in BUFP on entry.
  4650.    
  4651.    We call regex_compile to do the actual compilation.  */
  4652.  
  4653. const char *
  4654. re_compile_pattern (pattern, length, bufp)
  4655.      const char *pattern;
  4656.      int length;
  4657.      struct re_pattern_buffer *bufp;
  4658. {
  4659.   reg_errcode_t ret;
  4660.   
  4661.   /* GNU code is written to assume at least RE_NREGS registers will be set
  4662.      (and at least one extra will be -1).  */
  4663.   bufp->regs_allocated = REGS_UNALLOCATED;
  4664.   
  4665.   /* And GNU code determines whether or not to get register information
  4666.      by passing null for the REGS argument to re_match, etc., not by
  4667.      setting no_sub.  */
  4668.   bufp->no_sub = 0;
  4669.   
  4670.   /* Match anchors at newline.  */
  4671.   bufp->newline_anchor = 1;
  4672.   
  4673.   ret = regex_compile (pattern, length, re_syntax_options, bufp);
  4674.  
  4675.   return re_error_msg[(int) ret];
  4676. }     
  4677.  
  4678. /* Entry points compatible with 4.2 BSD regex library.  We don't define
  4679.    them if this is an Emacs or POSIX compilation.  */
  4680.  
  4681. #if !defined (emacs) && !defined (_POSIX_SOURCE)
  4682.  
  4683. /* BSD has one and only one pattern buffer.  */
  4684. static struct re_pattern_buffer re_comp_buf;
  4685.  
  4686. char *
  4687. re_comp (s)
  4688.     const char *s;
  4689. {
  4690.   reg_errcode_t ret;
  4691.   
  4692.   if (!s)
  4693.     {
  4694.       if (!re_comp_buf.buffer)
  4695.     return "No previous regular expression";
  4696.       return 0;
  4697.     }
  4698.  
  4699.   if (!re_comp_buf.buffer)
  4700.     {
  4701.       re_comp_buf.buffer = (unsigned char *) malloc (200);
  4702.       if (re_comp_buf.buffer == NULL)
  4703.         return "Memory exhausted";
  4704.       re_comp_buf.allocated = 200;
  4705.  
  4706.       re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  4707.       if (re_comp_buf.fastmap == NULL)
  4708.     return "Memory exhausted";
  4709.     }
  4710.  
  4711.   /* Since `re_exec' always passes NULL for the `regs' argument, we
  4712.      don't need to initialize the pattern buffer fields which affect it.  */
  4713.  
  4714.   /* Match anchors at newlines.  */
  4715.   re_comp_buf.newline_anchor = 1;
  4716.  
  4717.   ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
  4718.   
  4719.   /* Yes, we're discarding `const' here.  */
  4720.   return (char *) re_error_msg[(int) ret];
  4721. }
  4722.  
  4723.  
  4724. int
  4725. re_exec (s)
  4726.     const char *s;
  4727. {
  4728.   const int len = strlen (s);
  4729.   return
  4730.     0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
  4731. }
  4732. #endif /* not emacs and not _POSIX_SOURCE */
  4733.  
  4734. /* POSIX.2 functions.  Don't define these for Emacs.  */
  4735.  
  4736. #ifndef emacs
  4737.  
  4738. /* regcomp takes a regular expression as a string and compiles it.
  4739.  
  4740.    PREG is a regex_t *.  We do not expect any fields to be initialized,
  4741.    since POSIX says we shouldn't.  Thus, we set
  4742.  
  4743.      `buffer' to the compiled pattern;
  4744.      `used' to the length of the compiled pattern;
  4745.      `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  4746.        REG_EXTENDED bit in CFLAGS is set; otherwise, to
  4747.        RE_SYNTAX_POSIX_BASIC;
  4748.      `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  4749.      `fastmap' and `fastmap_accurate' to zero;
  4750.      `re_nsub' to the number of subexpressions in PATTERN.
  4751.  
  4752.    PATTERN is the address of the pattern string.
  4753.  
  4754.    CFLAGS is a series of bits which affect compilation.
  4755.  
  4756.      If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  4757.      use POSIX basic syntax.
  4758.  
  4759.      If REG_NEWLINE is set, then . and [^...] don't match newline.
  4760.      Also, regexec will try a match beginning after every newline.
  4761.  
  4762.      If REG_ICASE is set, then we considers upper- and lowercase
  4763.      versions of letters to be equivalent when matching.
  4764.  
  4765.      If REG_NOSUB is set, then when PREG is passed to regexec, that
  4766.      routine will report only success or failure, and nothing about the
  4767.      registers.
  4768.  
  4769.    It returns 0 if it succeeds, nonzero if it doesn't.  (See regex.h for
  4770.    the return codes and their meanings.)  */
  4771.  
  4772. int
  4773. regcomp (preg, pattern, cflags)
  4774.     regex_t *preg;
  4775.     const char *pattern; 
  4776.     int cflags;
  4777. {
  4778.   reg_errcode_t ret;
  4779.   unsigned syntax
  4780.     = (cflags & REG_EXTENDED) ?
  4781.       RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  4782.  
  4783.   /* regex_compile will allocate the space for the compiled pattern.  */
  4784.   preg->buffer = 0;
  4785.   preg->allocated = 0;
  4786.   preg->used = 0;
  4787.   
  4788.   /* Don't bother to use a fastmap when searching.  This simplifies the
  4789.      REG_NEWLINE case: if we used a fastmap, we'd have to put all the
  4790.      characters after newlines into the fastmap.  This way, we just try
  4791.      every character.  */
  4792.   preg->fastmap = 0;
  4793.   
  4794.   if (cflags & REG_ICASE)
  4795.     {
  4796.       unsigned i;
  4797.       
  4798.       preg->translate = (char *) malloc (CHAR_SET_SIZE);
  4799.       if (preg->translate == NULL)
  4800.         return (int) REG_ESPACE;
  4801.  
  4802.       /* Map uppercase characters to corresponding lowercase ones.  */
  4803.       for (i = 0; i < CHAR_SET_SIZE; i++)
  4804.         preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
  4805.     }
  4806.   else
  4807.     preg->translate = NULL;
  4808.  
  4809.   /* If REG_NEWLINE is set, newlines are treated differently.  */
  4810.   if (cflags & REG_NEWLINE)
  4811.     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
  4812.       syntax &= ~RE_DOT_NEWLINE;
  4813.       syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  4814.       /* It also changes the matching behavior.  */
  4815.       preg->newline_anchor = 1;
  4816.     }
  4817.   else
  4818.     preg->newline_anchor = 0;
  4819.  
  4820.   preg->no_sub = !!(cflags & REG_NOSUB);
  4821.  
  4822.   /* POSIX says a null character in the pattern terminates it, so we 
  4823.      can use strlen here in compiling the pattern.  */
  4824.   ret = regex_compile (pattern, strlen (pattern), syntax, preg);
  4825.   
  4826.   /* POSIX doesn't distinguish between an unmatched open-group and an
  4827.      unmatched close-group: both are REG_EPAREN.  */
  4828.   if (ret == REG_ERPAREN) ret = REG_EPAREN;
  4829.   
  4830.   return (int) ret;
  4831. }
  4832.  
  4833.  
  4834. /* regexec searches for a given pattern, specified by PREG, in the
  4835.    string STRING.
  4836.    
  4837.    If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  4838.    `regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
  4839.    least NMATCH elements, and we set them to the offsets of the
  4840.    corresponding matched substrings.
  4841.    
  4842.    EFLAGS specifies `execution flags' which affect matching: if
  4843.    REG_NOTBOL is set, then ^ does not match at the beginning of the
  4844.    string; if REG_NOTEOL is set, then $ does not match at the end.
  4845.    
  4846.    We return 0 if we find a match and REG_NOMATCH if not.  */
  4847.  
  4848. int
  4849. regexec (preg, string, nmatch, pmatch, eflags)
  4850.     const regex_t *preg;
  4851.     const char *string; 
  4852.     size_t nmatch; 
  4853.     regmatch_t pmatch[]; 
  4854.     int eflags;
  4855. {
  4856.   int ret;
  4857.   struct re_registers regs;
  4858.   regex_t private_preg;
  4859.   int len = strlen (string);
  4860.   boolean want_reg_info = !preg->no_sub && nmatch > 0;
  4861.  
  4862.   private_preg = *preg;
  4863.   
  4864.   private_preg.not_bol = !!(eflags & REG_NOTBOL);
  4865.   private_preg.not_eol = !!(eflags & REG_NOTEOL);
  4866.   
  4867.   /* The user has told us exactly how many registers to return
  4868.      information about, via `nmatch'.  We have to pass that on to the
  4869.      matching routines.  */
  4870.   private_preg.regs_allocated = REGS_FIXED;
  4871.   
  4872.   if (want_reg_info)
  4873.     {
  4874.       regs.num_regs = nmatch;
  4875.       regs.start = TALLOC (nmatch, regoff_t);
  4876.       regs.end = TALLOC (nmatch, regoff_t);
  4877.       if (regs.start == NULL || regs.end == NULL)
  4878.         return (int) REG_NOMATCH;
  4879.     }
  4880.  
  4881.   /* Perform the searching operation.  */
  4882.   ret = re_search (&private_preg, string, len,
  4883.                    /* start: */ 0, /* range: */ len,
  4884.                    want_reg_info ? ®s : (struct re_registers *) 0);
  4885.   
  4886.   /* Copy the register information to the POSIX structure.  */
  4887.   if (want_reg_info)
  4888.     {
  4889.       if (ret >= 0)
  4890.         {
  4891.           unsigned r;
  4892.  
  4893.           for (r = 0; r < nmatch; r++)
  4894.             {
  4895.               pmatch[r].rm_so = regs.start[r];
  4896.               pmatch[r].rm_eo = regs.end[r];
  4897.             }
  4898.         }
  4899.  
  4900.       /* If we needed the temporary register info, free the space now.  */
  4901.       free (regs.start);
  4902.       free (regs.end);
  4903.     }
  4904.  
  4905.   /* We want zero return to mean success, unlike `re_search'.  */
  4906.   return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  4907. }
  4908.  
  4909.  
  4910. /* Returns a message corresponding to an error code, ERRCODE, returned
  4911.    from either regcomp or regexec.   We don't use PREG here.  */
  4912.  
  4913. size_t
  4914. regerror (errcode, preg, errbuf, errbuf_size)
  4915.     int errcode;
  4916.     const regex_t *preg;
  4917.     char *errbuf;
  4918.     size_t errbuf_size;
  4919. {
  4920.   const char *msg;
  4921.   size_t msg_size;
  4922.  
  4923.   if (errcode < 0
  4924.       || errcode >= (sizeof (re_error_msg) / sizeof (re_error_msg[0])))
  4925.     /* Only error codes returned by the rest of the code should be passed 
  4926.        to this routine.  If we are given anything else, or if other regex
  4927.        code generates an invalid error code, then the program has a bug.
  4928.        Dump core so we can fix it.  */
  4929.     abort ();
  4930.  
  4931.   msg = re_error_msg[errcode];
  4932.  
  4933.   /* POSIX doesn't require that we do anything in this case, but why
  4934.      not be nice.  */
  4935.   if (! msg)
  4936.     msg = "Success";
  4937.  
  4938.   msg_size = strlen (msg) + 1; /* Includes the null.  */
  4939.   
  4940.   if (errbuf_size != 0)
  4941.     {
  4942.       if (msg_size > errbuf_size)
  4943.         {
  4944.           strncpy (errbuf, msg, errbuf_size - 1);
  4945.           errbuf[errbuf_size - 1] = 0;
  4946.         }
  4947.       else
  4948.         strcpy (errbuf, msg);
  4949.     }
  4950.  
  4951.   return msg_size;
  4952. }
  4953.  
  4954.  
  4955. /* Free dynamically allocated space used by PREG.  */
  4956.  
  4957. void
  4958. regfree (preg)
  4959.     regex_t *preg;
  4960. {
  4961.   if (preg->buffer != NULL)
  4962.     free (preg->buffer);
  4963.   preg->buffer = NULL;
  4964.   
  4965.   preg->allocated = 0;
  4966.   preg->used = 0;
  4967.  
  4968.   if (preg->fastmap != NULL)
  4969.     free (preg->fastmap);
  4970.   preg->fastmap = NULL;
  4971.   preg->fastmap_accurate = 0;
  4972.  
  4973.   if (preg->translate != NULL)
  4974.     free (preg->translate);
  4975.   preg->translate = NULL;
  4976. }
  4977.  
  4978. #endif /* not emacs  */
  4979.  
  4980. /*
  4981. Local variables:
  4982. make-backup-files: t
  4983. version-control: t
  4984. trim-versions-without-asking: nil
  4985. End:
  4986. */
  4987.