home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / LESS177.ZIP / src / regex.c < prev    next >
C/C++ Source or Header  |  1992-07-18  |  156KB  |  4,868 lines

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