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