home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / lib / regex.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  162KB  |  4,958 lines

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