home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gtak212.zip / 1.10 / regex.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  87KB  |  2,806 lines

  1. /*****************************************************************************
  2.  * $Id: regex.c,v 1.2 1992/09/02 20:08:37 ak Exp $
  3.  *****************************************************************************
  4.  * $Log: regex.c,v $
  5.  * Revision 1.2  1992/09/02  20:08:37  ak
  6.  * Version AK200
  7.  * - Tape access
  8.  * - Quick file access
  9.  * - OS/2 extended attributes
  10.  * - Some OS/2 fixes
  11.  * - Some fixes of Kai Uwe Rommel
  12.  *
  13.  * Revision 1.1.1.1  1992/09/02  19:22:27  ak
  14.  * Original GNU Tar 1.10 with some filenames changed for FAT compatibility.
  15.  *
  16.  * Revision 1.1  1992/09/02  19:22:23  ak
  17.  * Initial revision
  18.  *
  19.  *****************************************************************************/
  20.  
  21. static char *rcsid = "$Id: regex.c,v 1.2 1992/09/02 20:08:37 ak Exp $";
  22.  
  23. /* Extended regular expression matching and search library.
  24.    Copyright (C) 1985, 1989-90 Free Software Foundation, Inc.
  25.  
  26.    This program is free software; you can redistribute it and/or modify
  27.    it under the terms of the GNU General Public License as published by
  28.    the Free Software Foundation; either version 2, or (at your option)
  29.    any later version.
  30.  
  31.    This program is distributed in the hope that it will be useful,
  32.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.    GNU General Public License for more details.
  35.  
  36.    You should have received a copy of the GNU General Public License
  37.    along with this program; if not, write to the Free Software
  38.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  39.  
  40.  
  41. /* To test, compile with -Dtest.  This Dtestable feature turns this into
  42.    a self-contained program which reads a pattern, describes how it
  43.    compiles, then reads a string and searches for it.
  44.    
  45.    On the other hand, if you compile with both -Dtest and -Dcanned you
  46.    can run some tests we've already thought of.  */
  47.  
  48.  
  49. #ifdef emacs
  50.  
  51. /* The `emacs' switch turns on certain special matching commands
  52.   that make sense only in emacs. */
  53.  
  54. #include "config.h"
  55. #include "lisp.h"
  56. #include "buffer.h"
  57. #include "syntax.h"
  58.  
  59. #else  /* not emacs */
  60.  
  61. #if defined (USG) || defined (STDC_HEADERS)
  62. #ifndef BSTRING
  63. #include <string.h>
  64. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  65. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  66. #define bzero(s,n)    memset((s),0,(n))
  67. #endif
  68. #endif
  69.  
  70. #ifdef STDC_HEADERS
  71. #include <stdlib.h>
  72. #else
  73. char *malloc ();
  74. char *realloc ();
  75. #endif
  76.  
  77. /* Make alloca work the best possible way.  */
  78. #ifdef __GNUC__
  79. #define alloca __builtin_alloca
  80. #else
  81. #ifdef sparc
  82. #include <alloca.h>
  83. #else
  84. #ifdef _AIX
  85. #pragma alloca
  86. #else
  87. char *alloca ();
  88. #endif
  89. #endif
  90. #endif
  91.  
  92.  
  93. /* Define the syntax stuff, so we can do the \<, \>, etc.  */
  94.  
  95. /* This must be nonzero for the wordchar and notwordchar pattern
  96.    commands in re_match_2.  */
  97. #ifndef Sword 
  98. #define Sword 1
  99. #endif
  100.  
  101. #define SYNTAX(c) re_syntax_table[c]
  102.  
  103.  
  104. #ifdef SYNTAX_TABLE
  105.  
  106. char *re_syntax_table;
  107.  
  108. #else /* not SYNTAX_TABLE */
  109.  
  110. static char re_syntax_table[256];
  111.  
  112.  
  113. static void
  114. init_syntax_once ()
  115. {
  116.    register int c;
  117.    static int done = 0;
  118.  
  119.    if (done)
  120.      return;
  121.  
  122.    bzero (re_syntax_table, sizeof re_syntax_table);
  123.  
  124.    for (c = 'a'; c <= 'z'; c++)
  125.      re_syntax_table[c] = Sword;
  126.  
  127.    for (c = 'A'; c <= 'Z'; c++)
  128.      re_syntax_table[c] = Sword;
  129.  
  130.    for (c = '0'; c <= '9'; c++)
  131.      re_syntax_table[c] = Sword;
  132.  
  133.    done = 1;
  134. }
  135.  
  136. #endif /* SYNTAX_TABLE */
  137. #endif /* emacs */
  138.  
  139. /* We write fatal error messages on standard error.  */
  140. #include <stdio.h>
  141.  
  142. /* isalpha(3) etc. are used for the character classes.  */
  143. #include <ctype.h>
  144. /* Sequents are missing isgraph.  */
  145. #ifndef isgraph
  146. #define isgraph(c) (isprint((c)) && !isspace((c)))
  147. #endif
  148.  
  149. /* Get the interface, including the syntax bits.  */
  150. #include "regex.h"
  151.  
  152.  
  153. /* These are the command codes that appear in compiled regular
  154.    expressions, one per byte.  Some command codes are followed by
  155.    argument bytes.  A command code can specify any interpretation
  156.    whatsoever for its arguments.  Zero-bytes may appear in the compiled
  157.    regular expression.
  158.    
  159.    The value of `exactn' is needed in search.c (search_buffer) in emacs.
  160.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  161.    `exactn' we use here must also be 1.  */
  162.  
  163. enum regexpcode
  164.   {
  165.     unused=0,
  166.     exactn=1, /* Followed by one byte giving n, then by n literal bytes.  */
  167.     begline,  /* Fail unless at beginning of line.  */
  168.     endline,  /* Fail unless at end of line.  */
  169.     jump,     /* Followed by two bytes giving relative address to jump to.  */
  170.     on_failure_jump,     /* Followed by two bytes giving relative address of 
  171.                 place to resume at in case of failure.  */
  172.     finalize_jump,     /* Throw away latest failure point and then jump to 
  173.                 address.  */
  174.     maybe_finalize_jump, /* Like jump but finalize if safe to do so.
  175.                 This is used to jump back to the beginning
  176.                 of a repeat.  If the command that follows
  177.                 this jump is clearly incompatible with the
  178.                 one at the beginning of the repeat, such that
  179.                 we can be sure that there is no use backtracking
  180.                 out of repetitions already completed,
  181.                 then we finalize.  */
  182.     dummy_failure_jump,  /* Jump, and push a dummy failure point. This 
  183.                 failure point will be thrown away if an attempt 
  184.                             is made to use it for a failure. A + construct 
  185.                             makes this before the first repeat.  Also
  186.                             use it as an intermediary kind of jump when
  187.                             compiling an or construct.  */
  188.     succeed_n,     /* Used like on_failure_jump except has to succeed n times;
  189.             then gets turned into an on_failure_jump. The relative
  190.                     address following it is useless until then.  The
  191.                     address is followed by two bytes containing n.  */
  192.     jump_n,     /* Similar to jump, but jump n times only; also the relative
  193.             address following is in turn followed by yet two more bytes
  194.                     containing n.  */
  195.     set_number_at,    /* Set the following relative location to the
  196.                subsequent number.  */
  197.     anychar,     /* Matches any (more or less) one character.  */
  198.     charset,     /* Matches any one char belonging to specified set.
  199.             First following byte is number of bitmap bytes.
  200.             Then come bytes for a bitmap saying which chars are in.
  201.             Bits in each byte are ordered low-bit-first.
  202.             A character is in the set if its bit is 1.
  203.             A character too large to have a bit in the map
  204.             is automatically not in the set.  */
  205.     charset_not, /* Same parameters as charset, but match any character
  206.                     that is not one of those specified.  */
  207.     start_memory, /* Start remembering the text that is matched, for
  208.             storing in a memory register.  Followed by one
  209.                     byte containing the register number.  Register numbers
  210.                     must be in the range 0 through RE_NREGS.  */
  211.     stop_memory, /* Stop remembering the text that is matched
  212.             and store it in a memory register.  Followed by
  213.                     one byte containing the register number. Register
  214.                     numbers must be in the range 0 through RE_NREGS.  */
  215.     duplicate,   /* Match a duplicate of something remembered.
  216.             Followed by one byte containing the index of the memory 
  217.                     register.  */
  218.     before_dot,     /* Succeeds if before point.  */
  219.     at_dot,     /* Succeeds if at point.  */
  220.     after_dot,     /* Succeeds if after point.  */
  221.     begbuf,      /* Succeeds if at beginning of buffer.  */
  222.     endbuf,      /* Succeeds if at end of buffer.  */
  223.     wordchar,    /* Matches any word-constituent character.  */
  224.     notwordchar, /* Matches any char that is not a word-constituent.  */
  225.     wordbeg,     /* Succeeds if at word beginning.  */
  226.     wordend,     /* Succeeds if at word end.  */
  227.     wordbound,   /* Succeeds if at a word boundary.  */
  228.     notwordbound,/* Succeeds if not at a word boundary.  */
  229.     syntaxspec,  /* Matches any character whose syntax is specified.
  230.             followed by a byte which contains a syntax code,
  231.                     e.g., Sword.  */
  232.     notsyntaxspec /* Matches any character whose syntax differs from
  233.                      that specified.  */
  234.   };
  235.  
  236.  
  237. /* Number of failure points to allocate space for initially,
  238.    when matching.  If this number is exceeded, more space is allocated,
  239.    so it is not a hard limit.  */
  240.  
  241. #ifndef NFAILURES
  242. #define NFAILURES 80
  243. #endif
  244.  
  245. #ifdef CHAR_UNSIGNED
  246. #define SIGN_EXTEND_CHAR(c) ((c)>(char)127?(c)-256:(c)) /* for IBM RT */
  247. #endif
  248. #ifndef SIGN_EXTEND_CHAR
  249. #define SIGN_EXTEND_CHAR(x) (x)
  250. #endif
  251.  
  252.  
  253. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  254. #define STORE_NUMBER(destination, number)                \
  255.   { (destination)[0] = (number) & 0377;                    \
  256.     (destination)[1] = (number) >> 8; }
  257.   
  258. /* Same as STORE_NUMBER, except increment the destination pointer to
  259.    the byte after where the number is stored.  Watch out that values for
  260.    DESTINATION such as p + 1 won't work, whereas p will.  */
  261. #define STORE_NUMBER_AND_INCR(destination, number)            \
  262.   { STORE_NUMBER(destination, number);                    \
  263.     (destination) += 2; }
  264.  
  265.  
  266. /* Put into DESTINATION a number stored in two contingous bytes starting
  267.    at SOURCE.  */
  268. #define EXTRACT_NUMBER(destination, source)                \
  269.   { (destination) = *(source) & 0377;                    \
  270.     (destination) += SIGN_EXTEND_CHAR (*(char *)((source) + 1)) << 8; }
  271.  
  272. /* Same as EXTRACT_NUMBER, except increment the pointer for source to
  273.    point to second byte of SOURCE.  Note that SOURCE has to be a value
  274.    such as p, not, e.g., p + 1. */
  275. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  276.   { EXTRACT_NUMBER (destination, source);                \
  277.     (source) += 2; }
  278.  
  279.  
  280. /* Specify the precise syntax of regexps for compilation.  This provides
  281.    for compatibility for various utilities which historically have
  282.    different, incompatible syntaxes.
  283.    
  284.    The argument SYNTAX is a bit-mask comprised of the various bits
  285.    defined in regex.h.  */
  286.  
  287. int
  288. re_set_syntax (syntax)
  289.   int syntax;
  290. {
  291.   int ret;
  292.  
  293.   ret = obscure_syntax;
  294.   obscure_syntax = syntax;
  295.   return ret;
  296. }
  297.  
  298. /* Set by re_set_syntax to the current regexp syntax to recognize.  */
  299. int obscure_syntax = 0;
  300.  
  301.  
  302.  
  303. /* Macros for re_compile_pattern, which is found below these definitions.  */
  304.  
  305. #define CHAR_CLASS_MAX_LENGTH  6
  306.  
  307. /* Fetch the next character in the uncompiled pattern, translating it if
  308.    necessary.  */
  309. #define PATFETCH(c)                            \
  310.   {if (p == pend) goto end_of_pattern;                    \
  311.   c = * (unsigned char *) p++;                        \
  312.   if (translate) c = translate[c]; }
  313.  
  314. /* Fetch the next character in the uncompiled pattern, with no
  315.    translation.  */
  316. #define PATFETCH_RAW(c)                            \
  317.  {if (p == pend) goto end_of_pattern;                    \
  318.   c = * (unsigned char *) p++; }
  319.  
  320. #define PATUNFETCH p--
  321.  
  322.  
  323. /* If the buffer isn't allocated when it comes in, use this.  */
  324. #define INIT_BUF_SIZE  28
  325.  
  326. /* Make sure we have at least N more bytes of space in buffer.  */
  327. #define GET_BUFFER_SPACE(n)                        \
  328.   {                                        \
  329.     while (b - bufp->buffer + (n) >= bufp->allocated)            \
  330.       EXTEND_BUFFER;                            \
  331.   }
  332.  
  333. /* Make sure we have one more byte of buffer space and then add CH to it.  */
  334. #define BUFPUSH(ch)                            \
  335.   {                                    \
  336.     GET_BUFFER_SPACE (1);                        \
  337.     *b++ = (char) (ch);                            \
  338.   }
  339.   
  340. /* Extend the buffer by twice its current size via reallociation and
  341.    reset the pointers that pointed into the old allocation to point to
  342.    the correct places in the new allocation.  If extending the buffer
  343.    results in it being larger than 1 << 16, then flag memory exhausted.  */
  344. #define EXTEND_BUFFER                            \
  345.   { char *old_buffer = bufp->buffer;                    \
  346.     if (bufp->allocated == (1L<<16)) goto too_big;            \
  347.     bufp->allocated *= 2;                        \
  348.     if (bufp->allocated > (1L<<16)) bufp->allocated = (1L<<16);        \
  349.     bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated);    \
  350.     if (bufp->buffer == 0)                        \
  351.       goto memory_exhausted;                        \
  352.     b = (b - old_buffer) + bufp->buffer;                \
  353.     if (fixup_jump)                            \
  354.       fixup_jump = (fixup_jump - old_buffer) + bufp->buffer;        \
  355.     if (laststart)                            \
  356.       laststart = (laststart - old_buffer) + bufp->buffer;        \
  357.     begalt = (begalt - old_buffer) + bufp->buffer;            \
  358.     if (pending_exact)                            \
  359.       pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  360.   }
  361.  
  362. /* Set the bit for character C in a character set list.  */
  363. #define SET_LIST_BIT(c)  (b[(c) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
  364.  
  365. /* Get the next unsigned number in the uncompiled pattern.  */
  366. #define GET_UNSIGNED_NUMBER(num)                     \
  367.   { if (p != pend)                             \
  368.       {                                 \
  369.         PATFETCH (c);                             \
  370.     while (isdigit (c))                         \
  371.       {                                 \
  372.         if (num < 0)                         \
  373.            num = 0;                         \
  374.             num = num * 10 + c - '0';                     \
  375.         if (p == pend)                         \
  376.            break;                             \
  377.         PATFETCH (c);                         \
  378.       }                                 \
  379.         }                                 \
  380.   }
  381.  
  382. /* Subroutines for re_compile_pattern.  */
  383. static void store_jump (), insert_jump (), store_jump_n (),
  384.         insert_jump_n (), insert_op_2 ();
  385.  
  386.  
  387. /* re_compile_pattern takes a regular-expression string
  388.    and converts it into a buffer full of byte commands for matching.
  389.  
  390.    PATTERN   is the address of the pattern string
  391.    SIZE      is the length of it.
  392.    BUFP        is a  struct re_pattern_buffer *  which points to the info
  393.          on where to store the byte commands.
  394.          This structure contains a  char *  which points to the
  395.          actual space, which should have been obtained with malloc.
  396.          re_compile_pattern may use realloc to grow the buffer space.
  397.  
  398.    The number of bytes of commands can be found out by looking in
  399.    the `struct re_pattern_buffer' that bufp pointed to, after
  400.    re_compile_pattern returns. */
  401.  
  402. char *
  403. re_compile_pattern (pattern, size, bufp)
  404.      char *pattern;
  405.      int size;
  406.      struct re_pattern_buffer *bufp;
  407. {
  408.   register char *b = bufp->buffer;
  409.   register char *p = pattern;
  410.   char *pend = pattern + size;
  411.   register unsigned c, c1;
  412.   char *p1;
  413.   unsigned char *translate = (unsigned char *) bufp->translate;
  414.  
  415.   /* Address of the count-byte of the most recently inserted `exactn'
  416.      command.  This makes it possible to tell whether a new exact-match
  417.      character can be added to that command or requires a new `exactn'
  418.      command.  */
  419.      
  420.   char *pending_exact = 0;
  421.  
  422.   /* Address of the place where a forward-jump should go to the end of
  423.      the containing expression.  Each alternative of an `or', except the
  424.      last, ends with a forward-jump of this sort.  */
  425.  
  426.   char *fixup_jump = 0;
  427.  
  428.   /* Address of start of the most recently finished expression.
  429.      This tells postfix * where to find the start of its operand.  */
  430.  
  431.   char *laststart = 0;
  432.  
  433.   /* In processing a repeat, 1 means zero matches is allowed.  */
  434.  
  435.   char zero_times_ok;
  436.  
  437.   /* In processing a repeat, 1 means many matches is allowed.  */
  438.  
  439.   char many_times_ok;
  440.  
  441.   /* Address of beginning of regexp, or inside of last \(.  */
  442.  
  443.   char *begalt = b;
  444.  
  445.   /* In processing an interval, at least this many matches must be made.  */
  446.   int lower_bound;
  447.  
  448.   /* In processing an interval, at most this many matches can be made.  */
  449.   int upper_bound;
  450.  
  451.   /* Place in pattern (i.e., the {) to which to go back if the interval
  452.      is invalid.  */
  453.   char *beg_interval = 0;
  454.   
  455.   /* Stack of information saved by \( and restored by \).
  456.      Four stack elements are pushed by each \(:
  457.        First, the value of b.
  458.        Second, the value of fixup_jump.
  459.        Third, the value of regnum.
  460.        Fourth, the value of begalt.  */
  461.  
  462.   int stackb[40];
  463.   int *stackp = stackb;
  464.   int *stacke = stackb + 40;
  465.   int *stackt;
  466.  
  467.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  468.      where it becomes the register number to put in the stop_memory
  469.      command.  */
  470.  
  471.   int regnum = 1;
  472.  
  473.   bufp->fastmap_accurate = 0;
  474.  
  475. #ifndef emacs
  476. #ifndef SYNTAX_TABLE
  477.   /* Initialize the syntax table.  */
  478.    init_syntax_once();
  479. #endif
  480. #endif
  481.  
  482.   if (bufp->allocated == 0)
  483.     {
  484.       bufp->allocated = INIT_BUF_SIZE;
  485.       if (bufp->buffer)
  486.     /* EXTEND_BUFFER loses when bufp->allocated is 0.  */
  487.     bufp->buffer = (char *) realloc (bufp->buffer, INIT_BUF_SIZE);
  488.       else
  489.     /* Caller did not allocate a buffer.  Do it for them.  */
  490.     bufp->buffer = (char *) malloc (INIT_BUF_SIZE);
  491.       if (!bufp->buffer) goto memory_exhausted;
  492.       begalt = b = bufp->buffer;
  493.     }
  494.  
  495.   while (p != pend)
  496.     {
  497.       PATFETCH (c);
  498.  
  499.       switch (c)
  500.     {
  501.     case '$':
  502.       {
  503.         char *p1 = p;
  504.         /* When testing what follows the $,
  505.            look past the \-constructs that don't consume anything.  */
  506.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  507.           while (p1 != pend)
  508.         {
  509.           if (*p1 == '\\' && p1 + 1 != pend
  510.               && (p1[1] == '<' || p1[1] == '>'
  511.               || p1[1] == '`' || p1[1] == '\''
  512. #ifdef emacs
  513.               || p1[1] == '='
  514. #endif
  515.               || p1[1] == 'b' || p1[1] == 'B'))
  516.             p1 += 2;
  517.           else
  518.             break;
  519.         }
  520.             if (obscure_syntax & RE_TIGHT_VBAR)
  521.           {
  522.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p1 != pend)
  523.           goto normal_char;
  524.         /* Make operand of last vbar end before this `$'.  */
  525.         if (fixup_jump)
  526.           store_jump (fixup_jump, jump, b);
  527.         fixup_jump = 0;
  528.         BUFPUSH (endline);
  529.         break;
  530.           }
  531.         /* $ means succeed if at end of line, but only in special contexts.
  532.           If validly in the middle of a pattern, it is a normal character. */
  533.  
  534.             if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && p1 != pend)
  535.           goto invalid_pattern;
  536.         if (p1 == pend || *p1 == '\n'
  537.         || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  538.         || (obscure_syntax & RE_NO_BK_PARENS
  539.             ? *p1 == ')'
  540.             : *p1 == '\\' && p1[1] == ')')
  541.         || (obscure_syntax & RE_NO_BK_VBAR
  542.             ? *p1 == '|'
  543.             : *p1 == '\\' && p1[1] == '|'))
  544.           {
  545.         BUFPUSH (endline);
  546.         break;
  547.           }
  548.         goto normal_char;
  549.           }
  550.     case '^':
  551.       /* ^ means succeed if at beg of line, but only if no preceding 
  552.              pattern.  */
  553.              
  554.           if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && laststart)
  555.             goto invalid_pattern;
  556.           if (laststart && p - 2 >= pattern && p[-2] != '\n'
  557.            && !(obscure_syntax & RE_CONTEXT_INDEP_OPS))
  558.         goto normal_char;
  559.       if (obscure_syntax & RE_TIGHT_VBAR)
  560.         {
  561.           if (p != pattern + 1
  562.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  563.         goto normal_char;
  564.           BUFPUSH (begline);
  565.           begalt = b;
  566.         }
  567.       else
  568.         BUFPUSH (begline);
  569.       break;
  570.  
  571.     case '+':
  572.     case '?':
  573.       if ((obscure_syntax & RE_BK_PLUS_QM)
  574.           || (obscure_syntax & RE_LIMITED_OPS))
  575.         goto normal_char;
  576.     handle_plus:
  577.     case '*':
  578.       /* If there is no previous pattern, char not special. */
  579.       if (!laststart)
  580.             {
  581.               if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  582.                 goto invalid_pattern;
  583.               else if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  584.         goto normal_char;
  585.             }
  586.       /* If there is a sequence of repetition chars,
  587.          collapse it down to just one.  */
  588.       zero_times_ok = 0;
  589.       many_times_ok = 0;
  590.       while (1)
  591.         {
  592.           zero_times_ok |= c != '+';
  593.           many_times_ok |= c != '?';
  594.           if (p == pend)
  595.         break;
  596.           PATFETCH (c);
  597.           if (c == '*')
  598.         ;
  599.           else if (!(obscure_syntax & RE_BK_PLUS_QM)
  600.                && (c == '+' || c == '?'))
  601.         ;
  602.           else if ((obscure_syntax & RE_BK_PLUS_QM)
  603.                && c == '\\')
  604.         {
  605.           int c1;
  606.           PATFETCH (c1);
  607.           if (!(c1 == '+' || c1 == '?'))
  608.             {
  609.               PATUNFETCH;
  610.               PATUNFETCH;
  611.               break;
  612.             }
  613.           c = c1;
  614.         }
  615.           else
  616.         {
  617.           PATUNFETCH;
  618.           break;
  619.         }
  620.         }
  621.  
  622.       /* Star, etc. applied to an empty pattern is equivalent
  623.          to an empty pattern.  */
  624.       if (!laststart)  
  625.         break;
  626.  
  627.       /* Now we know whether or not zero matches is allowed
  628.          and also whether or not two or more matches is allowed.  */
  629.       if (many_times_ok)
  630.         {
  631.           /* If more than one repetition is allowed, put in at the
  632.                  end a backward relative jump from b to before the next
  633.                  jump we're going to put in below (which jumps from
  634.                  laststart to after this jump).  */
  635.               GET_BUFFER_SPACE (3);
  636.           store_jump (b, maybe_finalize_jump, laststart - 3);
  637.           b += 3;      /* Because store_jump put stuff here.  */
  638.         }
  639.           /* On failure, jump from laststart to b + 3, which will be the
  640.              end of the buffer after this jump is inserted.  */
  641.           GET_BUFFER_SPACE (3);
  642.       insert_jump (on_failure_jump, laststart, b + 3, b);
  643.       pending_exact = 0;
  644.       b += 3;
  645.       if (!zero_times_ok)
  646.         {
  647.           /* At least one repetition is required, so insert a
  648.                  dummy-failure before the initial on-failure-jump
  649.                  instruction of the loop. This effects a skip over that
  650.                  instruction the first time we hit that loop.  */
  651.               GET_BUFFER_SPACE (6);
  652.               insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  653.           b += 3;
  654.         }
  655.       break;
  656.  
  657.     case '.':
  658.       laststart = b;
  659.       BUFPUSH (anychar);
  660.       break;
  661.  
  662.         case '[':
  663.           if (p == pend)
  664.             goto invalid_pattern;
  665.       while (b - bufp->buffer
  666.          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  667.         EXTEND_BUFFER;
  668.  
  669.       laststart = b;
  670.       if (*p == '^')
  671.         {
  672.               BUFPUSH (charset_not); 
  673.               p++;
  674.             }
  675.       else
  676.         BUFPUSH (charset);
  677.       p1 = p;
  678.  
  679.       BUFPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  680.       /* Clear the whole map */
  681.       bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  682.           
  683.       if ((obscure_syntax & RE_HAT_NOT_NEWLINE) && b[-2] == charset_not)
  684.             SET_LIST_BIT ('\n');
  685.  
  686.  
  687.       /* Read in characters and ranges, setting map bits.  */
  688.       while (1)
  689.         {
  690.           /* Don't translate while fetching, in case it's a range bound.
  691.          When we set the bit for the character, we translate it.  */
  692.           PATFETCH_RAW (c);
  693.  
  694.           /* If set, \ escapes characters when inside [...].  */
  695.           if ((obscure_syntax & RE_AWK_CLASS_HACK) && c == '\\')
  696.             {
  697.               PATFETCH(c1);
  698.                   SET_LIST_BIT (c1);
  699.               continue;
  700.             }
  701.               if (c == ']')
  702.                 {
  703.                   if (p == p1 + 1)
  704.                     {
  705.               /* If this is an empty bracket expression.  */
  706.                       if ((obscure_syntax & RE_NO_EMPTY_BRACKETS) 
  707.                           && p == pend)
  708.                         goto invalid_pattern;
  709.                     }
  710.                   else 
  711.             /* Stop if this isn't merely a ] inside a bracket
  712.                        expression, but rather the end of a bracket
  713.                        expression.  */
  714.                     break;
  715.                 }
  716.               /* Get a range.  */
  717.               if (p[0] == '-' && p[1] != ']')
  718.         {
  719.                   PATFETCH (c1);
  720.           /* Don't translate the range bounds while fetching them.  */
  721.           PATFETCH_RAW (c1);
  722.                   
  723.           if ((obscure_syntax & RE_NO_EMPTY_RANGES) && c > c1)
  724.                     goto invalid_pattern;
  725.                     
  726.           if ((obscure_syntax & RE_NO_HYPHEN_RANGE_END) 
  727.                       && c1 == '-' && *p != ']')
  728.                     goto invalid_pattern;
  729.                     
  730.                   while (c <= c1)
  731.             {
  732.               /* Translate each char that's in the range.  */
  733.               if (translate)
  734.             SET_LIST_BIT (translate[c]);
  735.               else
  736.             SET_LIST_BIT (c);
  737.                       c++;
  738.             }
  739.                 }
  740.           else if ((obscure_syntax & RE_CHAR_CLASSES)
  741.             &&  c == '[' && p[0] == ':')
  742.                 {
  743.           /* Longest valid character class word has six characters.  */
  744.                   char str[CHAR_CLASS_MAX_LENGTH];
  745.           PATFETCH (c);
  746.           c1 = 0;
  747.           /* If no ] at end.  */
  748.                   if (p == pend)
  749.                     goto invalid_pattern;
  750.           while (1)
  751.             {
  752.               /* Don't translate the ``character class'' characters.  */
  753.                       PATFETCH_RAW (c);
  754.               if (c == ':' || c == ']' || p == pend
  755.                           || c1 == CHAR_CLASS_MAX_LENGTH)
  756.                 break;
  757.               str[c1++] = c;
  758.             }
  759.           str[c1] = '\0';
  760.           if (p == pend     
  761.               || c == ']'    /* End of the bracket expression.  */
  762.                       || p[0] != ']'
  763.               || p + 1 == pend
  764.                       || (strcmp (str, "alpha") != 0 
  765.                           && strcmp (str, "upper") != 0
  766.               && strcmp (str, "lower") != 0 
  767.                           && strcmp (str, "digit") != 0
  768.               && strcmp (str, "alnum") != 0 
  769.                           && strcmp (str, "xdigit") != 0
  770.               && strcmp (str, "space") != 0 
  771.                           && strcmp (str, "print") != 0
  772.               && strcmp (str, "punct") != 0 
  773.                           && strcmp (str, "graph") != 0
  774.               && strcmp (str, "cntrl") != 0))
  775.             {
  776.                /* Undo the ending character, the letters, and leave 
  777.                           the leading : and [ (but set bits for them).  */
  778.                       c1++;
  779.               while (c1--)    
  780.             PATUNFETCH;
  781.               SET_LIST_BIT ('[');
  782.               SET_LIST_BIT (':');
  783.                 }
  784.                   else
  785.                     {
  786.                       /* The ] at the end of the character class.  */
  787.                       PATFETCH (c);                    
  788.                       if (c != ']')
  789.                         goto invalid_pattern;
  790.               for (c = 0; c < (1 << BYTEWIDTH); c++)
  791.             {
  792.               if ((strcmp (str, "alpha") == 0  && isalpha (c))
  793.                    || (strcmp (str, "upper") == 0  && isupper (c))
  794.                    || (strcmp (str, "lower") == 0  && islower (c))
  795.                    || (strcmp (str, "digit") == 0  && isdigit (c))
  796.                    || (strcmp (str, "alnum") == 0  && isalnum (c))
  797.                    || (strcmp (str, "xdigit") == 0  && isxdigit (c))
  798.                    || (strcmp (str, "space") == 0  && isspace (c))
  799.                    || (strcmp (str, "print") == 0  && isprint (c))
  800.                    || (strcmp (str, "punct") == 0  && ispunct (c))
  801.                    || (strcmp (str, "graph") == 0  && isgraph (c))
  802.                    || (strcmp (str, "cntrl") == 0  && iscntrl (c)))
  803.                 SET_LIST_BIT (c);
  804.             }
  805.             }
  806.                 }
  807.               else if (translate)
  808.         SET_LIST_BIT (translate[c]);
  809.           else
  810.                 SET_LIST_BIT (c);
  811.         }
  812.  
  813.           /* Discard any character set/class bitmap bytes that are all
  814.              0 at the end of the map. Decrement the map-length byte too.  */
  815.           while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  816.             b[-1]--; 
  817.           b += b[-1];
  818.           break;
  819.  
  820.     case '(':
  821.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  822.         goto normal_char;
  823.       else
  824.         goto handle_open;
  825.  
  826.     case ')':
  827.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  828.         goto normal_char;
  829.       else
  830.         goto handle_close;
  831.  
  832.         case '\n':
  833.       if (! (obscure_syntax & RE_NEWLINE_OR))
  834.         goto normal_char;
  835.       else
  836.         goto handle_bar;
  837.  
  838.     case '|':
  839.       if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  840.               && (! laststart  ||  p == pend))
  841.         goto invalid_pattern;
  842.           else if (! (obscure_syntax & RE_NO_BK_VBAR))
  843.         goto normal_char;
  844.       else
  845.         goto handle_bar;
  846.  
  847.     case '{':
  848.            if (! ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  849.                   && (obscure_syntax & RE_INTERVALS)))
  850.              goto normal_char;
  851.            else
  852.              goto handle_interval;
  853.              
  854.         case '\\':
  855.       if (p == pend) goto invalid_pattern;
  856.       PATFETCH_RAW (c);
  857.       switch (c)
  858.         {
  859.         case '(':
  860.           if (obscure_syntax & RE_NO_BK_PARENS)
  861.         goto normal_backsl;
  862.         handle_open:
  863.           if (stackp == stacke) goto nesting_too_deep;
  864.  
  865.               /* Laststart should point to the start_memory that we are about
  866.                  to push (unless the pattern has RE_NREGS or more ('s).  */
  867.               *stackp++ = b - bufp->buffer;    
  868.           if (regnum < RE_NREGS)
  869.             {
  870.           BUFPUSH (start_memory);
  871.           BUFPUSH (regnum);
  872.             }
  873.           *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  874.           *stackp++ = regnum++;
  875.           *stackp++ = begalt - bufp->buffer;
  876.           fixup_jump = 0;
  877.           laststart = 0;
  878.           begalt = b;
  879.           break;
  880.  
  881.         case ')':
  882.           if (obscure_syntax & RE_NO_BK_PARENS)
  883.         goto normal_backsl;
  884.         handle_close:
  885.           if (stackp == stackb) goto unmatched_close;
  886.           begalt = *--stackp + bufp->buffer;
  887.           if (fixup_jump)
  888.         store_jump (fixup_jump, jump, b);
  889.           if (stackp[-1] < RE_NREGS)
  890.         {
  891.           BUFPUSH (stop_memory);
  892.           BUFPUSH (stackp[-1]);
  893.         }
  894.           stackp -= 2;
  895.               fixup_jump = *stackp ? *stackp + bufp->buffer - 1 : 0;
  896.               laststart = *--stackp + bufp->buffer;
  897.           break;
  898.  
  899.         case '|':
  900.               if ((obscure_syntax & RE_LIMITED_OPS)
  901.               || (obscure_syntax & RE_NO_BK_VBAR))
  902.         goto normal_backsl;
  903.         handle_bar:
  904.               if (obscure_syntax & RE_LIMITED_OPS)
  905.                 goto normal_char;
  906.           /* Insert before the previous alternative a jump which
  907.                  jumps to this alternative if the former fails.  */
  908.               GET_BUFFER_SPACE (6);
  909.               insert_jump (on_failure_jump, begalt, b + 6, b);
  910.           pending_exact = 0;
  911.           b += 3;
  912.           /* The alternative before the previous alternative has a
  913.                  jump after it which gets executed if it gets matched.
  914.                  Adjust that jump so it will jump to the previous
  915.                  alternative's analogous jump (put in below, which in
  916.                  turn will jump to the next (if any) alternative's such
  917.                  jump, etc.).  The last such jump jumps to the correct
  918.                  final destination.  */
  919.               if (fixup_jump)
  920.         store_jump (fixup_jump, jump, b);
  921.                 
  922.           /* Leave space for a jump after previous alternative---to be 
  923.                  filled in later.  */
  924.               fixup_jump = b;
  925.               b += 3;
  926.  
  927.               laststart = 0;
  928.           begalt = b;
  929.           break;
  930.  
  931.             case '{': 
  932.               if (! (obscure_syntax & RE_INTERVALS)
  933.           /* Let \{ be a literal.  */
  934.                   || ((obscure_syntax & RE_INTERVALS)
  935.                       && (obscure_syntax & RE_NO_BK_CURLY_BRACES))
  936.           /* If it's the string "\{".  */
  937.           || (p - 2 == pattern  &&  p == pend))
  938.                 goto normal_backsl;
  939.             handle_interval:
  940.           beg_interval = p - 1;        /* The {.  */
  941.               /* If there is no previous pattern, this isn't an interval.  */
  942.           if (!laststart)
  943.             {
  944.                   if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  945.             goto invalid_pattern;
  946.                   else
  947.                     goto normal_backsl;
  948.                 }
  949.               /* It also isn't an interval if not preceded by an re
  950.                  matching a single character or subexpression, or if
  951.                  the current type of intervals can't handle back
  952.                  references and the previous thing is a back reference.  */
  953.               if (! (*laststart == anychar
  954.              || *laststart == charset
  955.              || *laststart == charset_not
  956.              || *laststart == start_memory
  957.              || (*laststart == exactn  &&  laststart[1] == 1)
  958.              || (! (obscure_syntax & RE_NO_BK_REFS)
  959.                          && *laststart == duplicate)))
  960.                 {
  961.                   if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  962.                     goto normal_char;
  963.                     
  964.           /* Posix extended syntax is handled in previous
  965.                      statement; this is for Posix basic syntax.  */
  966.                   if (obscure_syntax & RE_INTERVALS)
  967.                     goto invalid_pattern;
  968.                     
  969.                   goto normal_backsl;
  970.         }
  971.               lower_bound = -1;            /* So can see if are set.  */
  972.           upper_bound = -1;
  973.               GET_UNSIGNED_NUMBER (lower_bound);
  974.           if (c == ',')
  975.         {
  976.           GET_UNSIGNED_NUMBER (upper_bound);
  977.           if (upper_bound < 0)
  978.             upper_bound = RE_DUP_MAX;
  979.         }
  980.           if (upper_bound < 0)
  981.         upper_bound = lower_bound;
  982.               if (! (obscure_syntax & RE_NO_BK_CURLY_BRACES)) 
  983.                 {
  984.                   if (c != '\\')
  985.                     goto invalid_pattern;
  986.                   PATFETCH (c);
  987.                 }
  988.           if (c != '}' || lower_bound < 0 || upper_bound > RE_DUP_MAX
  989.           || lower_bound > upper_bound 
  990.                   || ((obscure_syntax & RE_NO_BK_CURLY_BRACES) 
  991.               && p != pend  && *p == '{')) 
  992.             {
  993.           if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  994.                     goto unfetch_interval;
  995.                   else
  996.                     goto invalid_pattern;
  997.         }
  998.  
  999.           /* If upper_bound is zero, don't want to succeed at all; 
  1000.           jump from laststart to b + 3, which will be the end of
  1001.                  the buffer after this jump is inserted.  */
  1002.                  
  1003.                if (upper_bound == 0)
  1004.                  {
  1005.                    GET_BUFFER_SPACE (3);
  1006.                    insert_jump (jump, laststart, b + 3, b);
  1007.                    b += 3;
  1008.                  }
  1009.  
  1010.                /* Otherwise, after lower_bound number of succeeds, jump
  1011.                   to after the jump_n which will be inserted at the end
  1012.                   of the buffer, and insert that jump_n.  */
  1013.                else 
  1014.          { /* Set to 5 if only one repetition is allowed and
  1015.                   hence no jump_n is inserted at the current end of
  1016.                       the buffer; then only space for the succeed_n is
  1017.                       needed.  Otherwise, need space for both the
  1018.                       succeed_n and the jump_n.  */
  1019.                       
  1020.                    unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  1021.                      
  1022.                    GET_BUFFER_SPACE (slots_needed);
  1023.                    /* Initialize the succeed_n to n, even though it will
  1024.                       be set by its attendant set_number_at, because
  1025.                       re_compile_fastmap will need to know it.  Jump to
  1026.                       what the end of buffer will be after inserting
  1027.                       this succeed_n and possibly appending a jump_n.  */
  1028.                    insert_jump_n (succeed_n, laststart, b + slots_needed, 
  1029.                           b, lower_bound);
  1030.                    b += 5;     /* Just increment for the succeed_n here.  */
  1031.  
  1032.           /* More than one repetition is allowed, so put in at
  1033.              the end of the buffer a backward jump from b to the
  1034.                      succeed_n we put in above.  By the time we've gotten
  1035.                      to this jump when matching, we'll have matched once
  1036.                      already, so jump back only upper_bound - 1 times.  */
  1037.  
  1038.                    if (upper_bound > 1)
  1039.                      {
  1040.                        store_jump_n (b, jump_n, laststart, upper_bound - 1);
  1041.                        b += 5;
  1042.                        /* When hit this when matching, reset the
  1043.                           preceding jump_n's n to upper_bound - 1.  */
  1044.                        BUFPUSH (set_number_at);
  1045.                GET_BUFFER_SPACE (2);
  1046.                        STORE_NUMBER_AND_INCR (b, -5);
  1047.                        STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  1048.                      }
  1049.            /* When hit this when matching, set the succeed_n's n.  */
  1050.                    GET_BUFFER_SPACE (5);
  1051.            insert_op_2 (set_number_at, laststart, b, 5, lower_bound);
  1052.                    b += 5;
  1053.                  }
  1054.               pending_exact = 0;
  1055.           beg_interval = 0;
  1056.               break;
  1057.  
  1058.  
  1059.             unfetch_interval:
  1060.           /* If an invalid interval, match the characters as literals.  */
  1061.            if (beg_interval)
  1062.                  p = beg_interval;
  1063.              else
  1064.                  {
  1065.                    fprintf (stderr, 
  1066.               "regex: no interval beginning to which to backtrack.\n");
  1067.            exit (1);
  1068.                  }
  1069.                  
  1070.                beg_interval = 0;
  1071.                PATFETCH (c);        /* normal_char expects char in `c'.  */
  1072.            goto normal_char;
  1073.            break;
  1074.  
  1075. #ifdef emacs
  1076.         case '=':
  1077.           BUFPUSH (at_dot);
  1078.           break;
  1079.  
  1080.         case 's':    
  1081.           laststart = b;
  1082.           BUFPUSH (syntaxspec);
  1083.           PATFETCH (c);
  1084.           BUFPUSH (syntax_spec_code[c]);
  1085.           break;
  1086.  
  1087.         case 'S':
  1088.           laststart = b;
  1089.           BUFPUSH (notsyntaxspec);
  1090.           PATFETCH (c);
  1091.           BUFPUSH (syntax_spec_code[c]);
  1092.           break;
  1093. #endif /* emacs */
  1094.  
  1095.         case 'w':
  1096.           laststart = b;
  1097.           BUFPUSH (wordchar);
  1098.           break;
  1099.  
  1100.         case 'W':
  1101.           laststart = b;
  1102.           BUFPUSH (notwordchar);
  1103.           break;
  1104.  
  1105.         case '<':
  1106.           BUFPUSH (wordbeg);
  1107.           break;
  1108.  
  1109.         case '>':
  1110.           BUFPUSH (wordend);
  1111.           break;
  1112.  
  1113.         case 'b':
  1114.           BUFPUSH (wordbound);
  1115.           break;
  1116.  
  1117.         case 'B':
  1118.           BUFPUSH (notwordbound);
  1119.           break;
  1120.  
  1121.         case '`':
  1122.           BUFPUSH (begbuf);
  1123.           break;
  1124.  
  1125.         case '\'':
  1126.           BUFPUSH (endbuf);
  1127.           break;
  1128.  
  1129.         case '1':
  1130.         case '2':
  1131.         case '3':
  1132.         case '4':
  1133.         case '5':
  1134.         case '6':
  1135.         case '7':
  1136.         case '8':
  1137.         case '9':
  1138.           if (obscure_syntax & RE_NO_BK_REFS)
  1139.                 goto normal_char;
  1140.               c1 = c - '0';
  1141.           if (c1 >= regnum)
  1142.         {
  1143.             if (obscure_syntax & RE_NO_EMPTY_BK_REF)
  1144.                     goto invalid_pattern;
  1145.                   else
  1146.                     goto normal_char;
  1147.                 }
  1148.               /* Can't back reference to a subexpression if inside of it.  */
  1149.               for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  1150.          if (*stackt == c1)
  1151.           goto normal_char;
  1152.           laststart = b;
  1153.           BUFPUSH (duplicate);
  1154.           BUFPUSH (c1);
  1155.           break;
  1156.  
  1157.         case '+':
  1158.         case '?':
  1159.           if (obscure_syntax & RE_BK_PLUS_QM)
  1160.         goto handle_plus;
  1161.           else
  1162.                 goto normal_backsl;
  1163.               break;
  1164.  
  1165.             default:
  1166.         normal_backsl:
  1167.           /* You might think it would be useful for \ to mean
  1168.          not to translate; but if we don't translate it
  1169.          it will never match anything.  */
  1170.           if (translate) c = translate[c];
  1171.           goto normal_char;
  1172.         }
  1173.       break;
  1174.  
  1175.     default:
  1176.     normal_char:        /* Expects the character in `c'.  */
  1177.       if (!pending_exact || pending_exact + *pending_exact + 1 != b
  1178.           || *pending_exact == 0177 || *p == '*' || *p == '^'
  1179.           || ((obscure_syntax & RE_BK_PLUS_QM)
  1180.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  1181.           : (*p == '+' || *p == '?'))
  1182.           || ((obscure_syntax & RE_INTERVALS) 
  1183.                   && ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  1184.               ? *p == '{'
  1185.                       : (p[0] == '\\' && p[1] == '{'))))
  1186.         {
  1187.           laststart = b;
  1188.           BUFPUSH (exactn);
  1189.           pending_exact = b;
  1190.           BUFPUSH (0);
  1191.         }
  1192.       BUFPUSH (c);
  1193.       (*pending_exact)++;
  1194.     }
  1195.     }
  1196.  
  1197.   if (fixup_jump)
  1198.     store_jump (fixup_jump, jump, b);
  1199.  
  1200.   if (stackp != stackb) goto unmatched_open;
  1201.  
  1202.   bufp->used = b - bufp->buffer;
  1203.   return 0;
  1204.  
  1205.  invalid_pattern:
  1206.   return "Invalid regular expression";
  1207.  
  1208.  unmatched_open:
  1209.   return "Unmatched \\(";
  1210.  
  1211.  unmatched_close:
  1212.   return "Unmatched \\)";
  1213.  
  1214.  end_of_pattern:
  1215.   return "Premature end of regular expression";
  1216.  
  1217.  nesting_too_deep:
  1218.   return "Nesting too deep";
  1219.  
  1220.  too_big:
  1221.   return "Regular expression too big";
  1222.  
  1223.  memory_exhausted:
  1224.   return "Memory exhausted";
  1225. }
  1226.  
  1227.  
  1228. /* Store a jump of the form <OPCODE> <relative address>.
  1229.    Store in the location FROM a jump operation to jump to relative
  1230.    address FROM - TO.  OPCODE is the opcode to store.  */
  1231.  
  1232. static void
  1233. store_jump (from, opcode, to)
  1234.      char *from, *to;
  1235.      char opcode;
  1236. {
  1237.   from[0] = opcode;
  1238.   STORE_NUMBER(from + 1, to - (from + 3));
  1239. }
  1240.  
  1241.  
  1242. /* Open up space before char FROM, and insert there a jump to TO.
  1243.    CURRENT_END gives the end of the storage not in use, so we know 
  1244.    how much data to copy up. OP is the opcode of the jump to insert.
  1245.  
  1246.    If you call this function, you must zero out pending_exact.  */
  1247.  
  1248. static void
  1249. insert_jump (op, from, to, current_end)
  1250.      char op;
  1251.      char *from, *to, *current_end;
  1252. {
  1253.   register char *pfrom = current_end;        /* Copy from here...  */
  1254.   register char *pto = current_end + 3;        /* ...to here.  */
  1255.  
  1256.   while (pfrom != from)                   
  1257.     *--pto = *--pfrom;
  1258.   store_jump (from, op, to);
  1259. }
  1260.  
  1261.  
  1262. /* Store a jump of the form <opcode> <relative address> <n> .
  1263.  
  1264.    Store in the location FROM a jump operation to jump to relative
  1265.    address FROM - TO.  OPCODE is the opcode to store, N is a number the
  1266.    jump uses, say, to decide how many times to jump.
  1267.    
  1268.    If you call this function, you must zero out pending_exact.  */
  1269.  
  1270. static void
  1271. store_jump_n (from, opcode, to, n)
  1272.      char *from, *to;
  1273.      char opcode;
  1274.      unsigned n;
  1275. {
  1276.   from[0] = opcode;
  1277.   STORE_NUMBER (from + 1, to - (from + 3));
  1278.   STORE_NUMBER (from + 3, n);
  1279. }
  1280.  
  1281.  
  1282. /* Similar to insert_jump, but handles a jump which needs an extra
  1283.    number to handle minimum and maximum cases.  Open up space at
  1284.    location FROM, and insert there a jump to TO.  CURRENT_END gives the
  1285.    end of the storage in use, so we know how much data to copy up. OP is
  1286.    the opcode of the jump to insert.
  1287.  
  1288.    If you call this function, you must zero out pending_exact.  */
  1289.  
  1290. static void
  1291. insert_jump_n (op, from, to, current_end, n)
  1292.      char op;
  1293.      char *from, *to, *current_end;
  1294.      unsigned n;
  1295. {
  1296.   register char *pfrom = current_end;        /* Copy from here...  */
  1297.   register char *pto = current_end + 5;        /* ...to here.  */
  1298.  
  1299.   while (pfrom != from)                   
  1300.     *--pto = *--pfrom;
  1301.   store_jump_n (from, op, to, n);
  1302. }
  1303.  
  1304.  
  1305. /* Open up space at location THERE, and insert operation OP followed by
  1306.    NUM_1 and NUM_2.  CURRENT_END gives the end of the storage in use, so
  1307.    we know how much data to copy up.
  1308.  
  1309.    If you call this function, you must zero out pending_exact.  */
  1310.  
  1311. static void
  1312. insert_op_2 (op, there, current_end, num_1, num_2)
  1313.      char op;
  1314.      char *there, *current_end;
  1315.      int num_1, num_2;
  1316. {
  1317.   register char *pfrom = current_end;        /* Copy from here...  */
  1318.   register char *pto = current_end + 5;        /* ...to here.  */
  1319.  
  1320.   while (pfrom != there)                   
  1321.     *--pto = *--pfrom;
  1322.   
  1323.   there[0] = op;
  1324.   STORE_NUMBER (there + 1, num_1);
  1325.   STORE_NUMBER (there + 3, num_2);
  1326. }
  1327.  
  1328.  
  1329.  
  1330. /* Given a pattern, compute a fastmap from it.  The fastmap records
  1331.    which of the (1 << BYTEWIDTH) possible characters can start a string
  1332.    that matches the pattern.  This fastmap is used by re_search to skip
  1333.    quickly over totally implausible text.
  1334.  
  1335.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data 
  1336.    area as bufp->fastmap.
  1337.    The other components of bufp describe the pattern to be used.  */
  1338.  
  1339. void
  1340. re_compile_fastmap (bufp)
  1341.      struct re_pattern_buffer *bufp;
  1342. {
  1343.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  1344.   int size = bufp->used;
  1345.   register char *fastmap = bufp->fastmap;
  1346.   register unsigned char *p = pattern;
  1347.   register unsigned char *pend = pattern + size;
  1348.   register int j, k;
  1349.   unsigned char *translate = (unsigned char *) bufp->translate;
  1350.  
  1351.   unsigned char *stackb[NFAILURES];
  1352.   unsigned char **stackp = stackb;
  1353.  
  1354.   unsigned is_a_succeed_n;
  1355.  
  1356.   bzero (fastmap, (1 << BYTEWIDTH));
  1357.   bufp->fastmap_accurate = 1;
  1358.   bufp->can_be_null = 0;
  1359.       
  1360.   while (p)
  1361.     {
  1362.       is_a_succeed_n = 0;
  1363.       if (p == pend)
  1364.     {
  1365.       bufp->can_be_null = 1;
  1366.       break;
  1367.     }
  1368. #ifdef SWITCH_ENUM_BUG
  1369.       switch ((int) ((enum regexpcode) *p++))
  1370. #else
  1371.       switch ((enum regexpcode) *p++)
  1372. #endif
  1373.     {
  1374.     case exactn:
  1375.       if (translate)
  1376.         fastmap[translate[p[1]]] = 1;
  1377.       else
  1378.         fastmap[p[1]] = 1;
  1379.       break;
  1380.  
  1381.         case begline:
  1382.         case before_dot:
  1383.     case at_dot:
  1384.     case after_dot:
  1385.     case begbuf:
  1386.     case endbuf:
  1387.     case wordbound:
  1388.     case notwordbound:
  1389.     case wordbeg:
  1390.     case wordend:
  1391.           continue;
  1392.  
  1393.     case endline:
  1394.       if (translate)
  1395.         fastmap[translate['\n']] = 1;
  1396.       else
  1397.         fastmap['\n'] = 1;
  1398.             
  1399.       if (bufp->can_be_null != 1)
  1400.         bufp->can_be_null = 2;
  1401.       break;
  1402.  
  1403.     case jump_n:
  1404.         case finalize_jump:
  1405.     case maybe_finalize_jump:
  1406.     case jump:
  1407.     case dummy_failure_jump:
  1408.           EXTRACT_NUMBER_AND_INCR (j, p);
  1409.       p += j;    
  1410.       if (j > 0)
  1411.         continue;
  1412.           /* Jump backward reached implies we just went through
  1413.          the body of a loop and matched nothing.
  1414.          Opcode jumped to should be an on_failure_jump.
  1415.          Just treat it like an ordinary jump.
  1416.          For a * loop, it has pushed its failure point already;
  1417.          If so, discard that as redundant.  */
  1418.  
  1419.           if ((enum regexpcode) *p != on_failure_jump
  1420.           && (enum regexpcode) *p != succeed_n)
  1421.         continue;
  1422.           p++;
  1423.           EXTRACT_NUMBER_AND_INCR (j, p);
  1424.           p += j;        
  1425.           if (stackp != stackb && *stackp == p)
  1426.             stackp--;
  1427.           continue;
  1428.       
  1429.         case on_failure_jump:
  1430.     handle_on_failure_jump:
  1431.           EXTRACT_NUMBER_AND_INCR (j, p);
  1432.           *++stackp = p + j;
  1433.       if (is_a_succeed_n)
  1434.             EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  1435.       continue;
  1436.  
  1437.     case succeed_n:
  1438.       is_a_succeed_n = 1;
  1439.           /* Get to the number of times to succeed.  */
  1440.           p += 2;        
  1441.       /* Increment p past the n for when k != 0.  */
  1442.           EXTRACT_NUMBER_AND_INCR (k, p);
  1443.           if (k == 0)
  1444.         {
  1445.               p -= 4;
  1446.               goto handle_on_failure_jump;
  1447.             }
  1448.           continue;
  1449.           
  1450.     case set_number_at:
  1451.           p += 4;
  1452.           continue;
  1453.  
  1454.         case start_memory:
  1455.     case stop_memory:
  1456.       p++;
  1457.       continue;
  1458.  
  1459.     case duplicate:
  1460.       bufp->can_be_null = 1;
  1461.       fastmap['\n'] = 1;
  1462.     case anychar:
  1463.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1464.         if (j != '\n')
  1465.           fastmap[j] = 1;
  1466.       if (bufp->can_be_null)
  1467.         return;
  1468.       /* Don't return; check the alternative paths
  1469.          so we can set can_be_null if appropriate.  */
  1470.       break;
  1471.  
  1472.     case wordchar:
  1473.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1474.         if (SYNTAX (j) == Sword)
  1475.           fastmap[j] = 1;
  1476.       break;
  1477.  
  1478.     case notwordchar:
  1479.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1480.         if (SYNTAX (j) != Sword)
  1481.           fastmap[j] = 1;
  1482.       break;
  1483.  
  1484. #ifdef emacs
  1485.     case syntaxspec:
  1486.       k = *p++;
  1487.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1488.         if (SYNTAX (j) == (enum syntaxcode) k)
  1489.           fastmap[j] = 1;
  1490.       break;
  1491.  
  1492.     case notsyntaxspec:
  1493.       k = *p++;
  1494.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1495.         if (SYNTAX (j) != (enum syntaxcode) k)
  1496.           fastmap[j] = 1;
  1497.       break;
  1498. #endif /* not emacs */
  1499.  
  1500.     case charset:
  1501.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1502.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  1503.           {
  1504.         if (translate)
  1505.           fastmap[translate[j]] = 1;
  1506.         else
  1507.           fastmap[j] = 1;
  1508.           }
  1509.       break;
  1510.  
  1511.     case charset_not:
  1512.       /* Chars beyond end of map must be allowed */
  1513.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  1514.         if (translate)
  1515.           fastmap[translate[j]] = 1;
  1516.         else
  1517.           fastmap[j] = 1;
  1518.  
  1519.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1520.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  1521.           {
  1522.         if (translate)
  1523.           fastmap[translate[j]] = 1;
  1524.         else
  1525.           fastmap[j] = 1;
  1526.           }
  1527.       break;
  1528.     }
  1529.  
  1530.       /* Get here means we have successfully found the possible starting
  1531.          characters of one path of the pattern.  We need not follow this
  1532.          path any farther.  Instead, look at the next alternative
  1533.          remembered in the stack.  */
  1534.    if (stackp != stackb)
  1535.     p = *stackp--;
  1536.       else
  1537.     break;
  1538.     }
  1539. }
  1540.  
  1541.  
  1542.  
  1543. /* Like re_search_2, below, but only one string is specified, and
  1544.    doesn't let you say where to stop matching. */
  1545.  
  1546. int
  1547. re_search (pbufp, string, size, startpos, range, regs)
  1548.      struct re_pattern_buffer *pbufp;
  1549.      char *string;
  1550.      int size, startpos, range;
  1551.      struct re_registers *regs;
  1552. {
  1553.   return re_search_2 (pbufp, (char *) 0, 0, string, size, startpos, range, 
  1554.               regs, size);
  1555. }
  1556.  
  1557.  
  1558. /* Using the compiled pattern in PBUFP->buffer, first tries to match the
  1559.    virtual concatenation of STRING1 and STRING2, starting first at index
  1560.    STARTPOS, then at STARTPOS + 1, and so on.  RANGE is the number of
  1561.    places to try before giving up.  If RANGE is negative, it searches
  1562.    backwards, i.e., the starting positions tried are STARTPOS, STARTPOS
  1563.    - 1, etc.  STRING1 and STRING2 are of SIZE1 and SIZE2, respectively.
  1564.    In REGS, return the indices of the virtual concatenation of STRING1
  1565.    and STRING2 that matched the entire PBUFP->buffer and its contained
  1566.    subexpressions.  Do not consider matching one past the index MSTOP in
  1567.    the virtual concatenation of STRING1 and STRING2.
  1568.  
  1569.    The value returned is the position in the strings at which the match
  1570.    was found, or -1 if no match was found, or -2 if error (such as
  1571.    failure stack overflow).  */
  1572.  
  1573. int
  1574. re_search_2 (pbufp, string1, size1, string2, size2, startpos, range,
  1575.          regs, mstop)
  1576.      struct re_pattern_buffer *pbufp;
  1577.      char *string1, *string2;
  1578.      int size1, size2;
  1579.      int startpos;
  1580.      register int range;
  1581.      struct re_registers *regs;
  1582.      int mstop;
  1583. {
  1584.   register char *fastmap = pbufp->fastmap;
  1585.   register unsigned char *translate = (unsigned char *) pbufp->translate;
  1586.   int total_size = size1 + size2;
  1587.   int endpos = startpos + range;
  1588.   int val;
  1589.  
  1590.   /* Check for out-of-range starting position.  */
  1591.   if (startpos < 0  ||  startpos > total_size)
  1592.     return -1;
  1593.     
  1594.   /* Fix up range if it would eventually take startpos outside of the
  1595.      virtual concatenation of string1 and string2.  */
  1596.   if (endpos < -1)
  1597.     range = -1 - startpos;
  1598.   else if (endpos > total_size)
  1599.     range = total_size - startpos;
  1600.  
  1601.   /* Update the fastmap now if not correct already.  */
  1602.   if (fastmap && !pbufp->fastmap_accurate)
  1603.     re_compile_fastmap (pbufp);
  1604.   
  1605.   /* If the search isn't to be a backwards one, don't waste time in a
  1606.      long search for a pattern that says it is anchored.  */
  1607.   if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  1608.       && range > 0)
  1609.     {
  1610.       if (startpos > 0)
  1611.     return -1;
  1612.       else
  1613.     range = 1;
  1614.     }
  1615.  
  1616.   while (1)
  1617.     { 
  1618.       /* If a fastmap is supplied, skip quickly over characters that
  1619.          cannot possibly be the start of a match.  Note, however, that
  1620.          if the pattern can possibly match the null string, we must
  1621.          test it at each starting point so that we take the first null
  1622.          string we get.  */
  1623.  
  1624.       if (fastmap && startpos < total_size && pbufp->can_be_null != 1)
  1625.     {
  1626.       if (range > 0)    /* Searching forwards.  */
  1627.         {
  1628.           register int lim = 0;
  1629.           register unsigned char *p;
  1630.           int irange = range;
  1631.           if (startpos < size1 && startpos + range >= size1)
  1632.         lim = range - (size1 - startpos);
  1633.  
  1634.           p = ((unsigned char *)
  1635.            &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1636.  
  1637.               while (range > lim && !fastmap[translate 
  1638.                                              ? translate[*p++]
  1639.                                              : *p++])
  1640.             range--;
  1641.           startpos += irange - range;
  1642.         }
  1643.       else                /* Searching backwards.  */
  1644.         {
  1645.           register unsigned char c;
  1646.  
  1647.               if (string1 == 0 || startpos >= size1)
  1648.         c = string2[startpos - size1];
  1649.           else 
  1650.         c = string1[startpos];
  1651.  
  1652.               c &= 0xff;
  1653.           if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1654.         goto advance;
  1655.         }
  1656.     }
  1657.  
  1658.       if (range >= 0 && startpos == total_size
  1659.       && fastmap && pbufp->can_be_null == 0)
  1660.     return -1;
  1661.  
  1662.       val = re_match_2 (pbufp, string1, size1, string2, size2, startpos,
  1663.             regs, mstop);
  1664.       if (val >= 0)
  1665.     return startpos;
  1666.       if (val == -2)
  1667.     return -2;
  1668.  
  1669. #ifdef C_ALLOCA
  1670.       alloca (0);
  1671. #endif /* C_ALLOCA */
  1672.  
  1673.     advance:
  1674.       if (!range) 
  1675.         break;
  1676.       else if (range > 0) 
  1677.         {
  1678.           range--; 
  1679.           startpos++;
  1680.         }
  1681.       else
  1682.         {
  1683.           range++; 
  1684.           startpos--;
  1685.         }
  1686.     }
  1687.   return -1;
  1688. }
  1689.  
  1690.  
  1691.  
  1692. #ifndef emacs   /* emacs never uses this.  */
  1693. int
  1694. re_match (pbufp, string, size, pos, regs)
  1695.      struct re_pattern_buffer *pbufp;
  1696.      char *string;
  1697.      int size, pos;
  1698.      struct re_registers *regs;
  1699. {
  1700.   return re_match_2 (pbufp, (char *) 0, 0, string, size, pos, regs, size); 
  1701. }
  1702. #endif /* not emacs */
  1703.  
  1704.  
  1705. /* The following are used for re_match_2, defined below:  */
  1706.  
  1707. /* Roughly the maximum number of failure points on the stack.  Would be
  1708.    exactly that if always pushed MAX_NUM_FAILURE_ITEMS each time we failed.  */
  1709.    
  1710. int re_max_failures = 2000;
  1711.  
  1712. /* Routine used by re_match_2.  */
  1713. static int bcmp_translate ();
  1714.  
  1715.  
  1716. /* Structure and accessing macros used in re_match_2:  */
  1717.  
  1718. struct register_info
  1719. {
  1720.   unsigned is_active : 1;
  1721.   unsigned matched_something : 1;
  1722. };
  1723.  
  1724. #define IS_ACTIVE(R)  ((R).is_active)
  1725. #define MATCHED_SOMETHING(R)  ((R).matched_something)
  1726.  
  1727.  
  1728. /* Macros used by re_match_2:  */
  1729.  
  1730.  
  1731. /* I.e., regstart, regend, and reg_info.  */
  1732.  
  1733. #define NUM_REG_ITEMS  3
  1734.  
  1735. /* We push at most this many things on the stack whenever we
  1736.    fail.  The `+ 2' refers to PATTERN_PLACE and STRING_PLACE, which are
  1737.    arguments to the PUSH_FAILURE_POINT macro.  */
  1738.  
  1739. #define MAX_NUM_FAILURE_ITEMS   (RE_NREGS * NUM_REG_ITEMS + 2)
  1740.  
  1741.  
  1742. /* We push this many things on the stack whenever we fail.  */
  1743.  
  1744. #define NUM_FAILURE_ITEMS  (last_used_reg * NUM_REG_ITEMS + 2)
  1745.  
  1746.  
  1747. /* This pushes most of the information about the current state we will want
  1748.    if we ever fail back to it.  */
  1749.  
  1750. #define PUSH_FAILURE_POINT(pattern_place, string_place)            \
  1751.   {                                    \
  1752.     short last_used_reg, this_reg;                    \
  1753.                                     \
  1754.     /* Find out how many registers are active or have been matched.    \
  1755.        (Aside from register zero, which is only set at the end.)  */    \
  1756.     for (last_used_reg = RE_NREGS - 1; last_used_reg > 0; last_used_reg--)\
  1757.       if (regstart[last_used_reg] != (unsigned char *) -1)        \
  1758.         break;                                \
  1759.                                     \
  1760.     if (stacke - stackp < NUM_FAILURE_ITEMS)                \
  1761.       {                                    \
  1762.     unsigned char **stackx;                        \
  1763.     unsigned int len = stacke - stackb;                \
  1764.     if (len > re_max_failures * MAX_NUM_FAILURE_ITEMS)        \
  1765.       return -2;                            \
  1766.                                     \
  1767.         /* Roughly double the size of the stack.  */            \
  1768.         stackx = (unsigned char **) alloca (2 * len            \
  1769.                                             * sizeof (unsigned char *));\
  1770.     /* Only copy what is in use.  */                \
  1771.         bcopy (stackb, stackx, len * sizeof (char *));            \
  1772.     stackp = stackx + (stackp - stackb);                \
  1773.     stackb = stackx;                        \
  1774.     stacke = stackb + 2 * len;                    \
  1775.       }                                    \
  1776.                                     \
  1777.     /* Now push the info for each of those registers.  */        \
  1778.     for (this_reg = 1; this_reg <= last_used_reg; this_reg++)        \
  1779.       {                                    \
  1780.         *stackp++ = regstart[this_reg];                    \
  1781.         *stackp++ = regend[this_reg];                    \
  1782.         *stackp++ = (unsigned char *) ®_info[this_reg];        \
  1783.       }                                    \
  1784.                                     \
  1785.     /* Push how many registers we saved.  */                \
  1786.     *stackp++ = (unsigned char *) last_used_reg;            \
  1787.                                     \
  1788.     *stackp++ = pattern_place;                                          \
  1789.     *stackp++ = string_place;                                           \
  1790.   }
  1791.   
  1792.  
  1793. /* This pops what PUSH_FAILURE_POINT pushes.  */
  1794.  
  1795. #define POP_FAILURE_POINT()                        \
  1796.   {                                    \
  1797.     int temp;                                \
  1798.     stackp -= 2;        /* Remove failure points.  */        \
  1799.     temp = (int) *--stackp;    /* How many regs pushed.  */            \
  1800.     temp *= NUM_REG_ITEMS;    /* How much to take off the stack.  */    \
  1801.     stackp -= temp;         /* Remove the register info.  */    \
  1802.   }
  1803.  
  1804.  
  1805. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  1806.  
  1807. /* Is true if there is a first string and if PTR is pointing anywhere
  1808.    inside it or just past the end.  */
  1809.    
  1810. #define IS_IN_FIRST_STRING(ptr)                     \
  1811.     (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  1812.  
  1813. /* Call before fetching a character with *d.  This switches over to
  1814.    string2 if necessary.  */
  1815.  
  1816. #define PREFETCH                            \
  1817.  while (d == dend)                                \
  1818.   {                                    \
  1819.     /* end of string2 => fail.  */                    \
  1820.     if (dend == end_match_2)                         \
  1821.       goto fail;                            \
  1822.     /* end of string1 => advance to string2.  */             \
  1823.     d = string2;                                \
  1824.     dend = end_match_2;                            \
  1825.   }
  1826.  
  1827.  
  1828. /* Call this when have matched something; it sets `matched' flags for the
  1829.    registers corresponding to the subexpressions of which we currently
  1830.    are inside.  */
  1831. #define SET_REGS_MATCHED                         \
  1832.   { unsigned this_reg;                             \
  1833.     for (this_reg = 0; this_reg < RE_NREGS; this_reg++)         \
  1834.       {                                 \
  1835.         if (IS_ACTIVE(reg_info[this_reg]))                \
  1836.           MATCHED_SOMETHING(reg_info[this_reg]) = 1;            \
  1837.         else                                \
  1838.           MATCHED_SOMETHING(reg_info[this_reg]) = 0;            \
  1839.       }                                 \
  1840.   }
  1841.  
  1842. /* Test if at very beginning or at very end of the virtual concatenation
  1843.    of string1 and string2.  If there is only one string, we've put it in
  1844.    string2.  */
  1845.  
  1846. #define AT_STRINGS_BEG  (d == (size1 ? string1 : string2)  ||  !size2)
  1847. #define AT_STRINGS_END  (d == end2)    
  1848.  
  1849. #define AT_WORD_BOUNDARY                        \
  1850.   (AT_STRINGS_BEG || AT_STRINGS_END || IS_A_LETTER (d - 1) != IS_A_LETTER (d))
  1851.  
  1852. /* We have two special cases to check for: 
  1853.      1) if we're past the end of string1, we have to look at the first
  1854.         character in string2;
  1855.      2) if we're before the beginning of string2, we have to look at the
  1856.         last character in string1; we assume there is a string1, so use
  1857.         this in conjunction with AT_STRINGS_BEG.  */
  1858. #define IS_A_LETTER(d)                            \
  1859.   (SYNTAX ((d) == end1 ? *string2 : (d) == string2 - 1 ? *(end1 - 1) : *(d))\
  1860.    == Sword)
  1861.  
  1862.  
  1863. /* Match the pattern described by PBUFP against the virtual
  1864.    concatenation of STRING1 and STRING2, which are of SIZE1 and SIZE2,
  1865.    respectively.  Start the match at index POS in the virtual
  1866.    concatenation of STRING1 and STRING2.  In REGS, return the indices of
  1867.    the virtual concatenation of STRING1 and STRING2 that matched the
  1868.    entire PBUFP->buffer and its contained subexpressions.  Do not
  1869.    consider matching one past the index MSTOP in the virtual
  1870.    concatenation of STRING1 and STRING2.
  1871.  
  1872.    If pbufp->fastmap is nonzero, then it had better be up to date.
  1873.  
  1874.    The reason that the data to match are specified as two components
  1875.    which are to be regarded as concatenated is so this function can be
  1876.    used directly on the contents of an Emacs buffer.
  1877.  
  1878.    -1 is returned if there is no match.  -2 is returned if there is an
  1879.    error (such as match stack overflow).  Otherwise the value is the
  1880.    length of the substring which was matched.  */
  1881.  
  1882. int
  1883. re_match_2 (pbufp, string1_arg, size1, string2_arg, size2, pos, regs, mstop)
  1884.      struct re_pattern_buffer *pbufp;
  1885.      char *string1_arg, *string2_arg;
  1886.      int size1, size2;
  1887.      int pos;
  1888.      struct re_registers *regs;
  1889.      int mstop;
  1890. {
  1891.   register unsigned char *p = (unsigned char *) pbufp->buffer;
  1892.  
  1893.   /* Pointer to beyond end of buffer.  */
  1894.   register unsigned char *pend = p + pbufp->used;
  1895.  
  1896.   unsigned char *string1 = (unsigned char *) string1_arg;
  1897.   unsigned char *string2 = (unsigned char *) string2_arg;
  1898.   unsigned char *end1;        /* Just past end of first string.  */
  1899.   unsigned char *end2;        /* Just past end of second string.  */
  1900.  
  1901.   /* Pointers into string1 and string2, just past the last characters in
  1902.      each to consider matching.  */
  1903.   unsigned char *end_match_1, *end_match_2;
  1904.  
  1905.   register unsigned char *d, *dend;
  1906.   register int mcnt;            /* Multipurpose.  */
  1907.   unsigned char *translate = (unsigned char *) pbufp->translate;
  1908.   unsigned is_a_jump_n = 0;
  1909.  
  1910.  /* Failure point stack.  Each place that can handle a failure further
  1911.     down the line pushes a failure point on this stack.  It consists of
  1912.     restart, regend, and reg_info for all registers corresponding to the
  1913.     subexpressions we're currently inside, plus the number of such
  1914.     registers, and, finally, two char *'s.  The first char * is where to
  1915.     resume scanning the pattern; the second one is where to resume
  1916.     scanning the strings.  If the latter is zero, the failure point is a
  1917.     ``dummy''; if a failure happens and the failure point is a dummy, it
  1918.     gets discarded and the next next one is tried.  */
  1919.  
  1920.   unsigned char *initial_stack[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1921.   unsigned char **stackb = initial_stack;
  1922.   unsigned char **stackp = stackb;
  1923.   unsigned char **stacke = &stackb[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1924.  
  1925.  
  1926.   /* Information on the contents of registers. These are pointers into
  1927.      the input strings; they record just what was matched (on this
  1928.      attempt) by a subexpression part of the pattern, that is, the
  1929.      regnum-th regstart pointer points to where in the pattern we began
  1930.      matching and the regnum-th regend points to right after where we
  1931.      stopped matching the regnum-th subexpression.  (The zeroth register
  1932.      keeps track of what the whole pattern matches.)  */
  1933.      
  1934.   unsigned char *regstart[RE_NREGS];
  1935.   unsigned char *regend[RE_NREGS];
  1936.  
  1937.   /* The is_active field of reg_info helps us keep track of which (possibly
  1938.      nested) subexpressions we are currently in. The matched_something
  1939.      field of reg_info[reg_num] helps us tell whether or not we have
  1940.      matched any of the pattern so far this time through the reg_num-th
  1941.      subexpression.  These two fields get reset each time through any
  1942.      loop their register is in.  */
  1943.  
  1944.   struct register_info reg_info[RE_NREGS];
  1945.  
  1946.  
  1947.   /* The following record the register info as found in the above
  1948.      variables when we find a match better than any we've seen before. 
  1949.      This happens as we backtrack through the failure points, which in
  1950.      turn happens only if we have not yet matched the entire string.  */
  1951.  
  1952.   unsigned best_regs_set = 0;
  1953.   unsigned char *best_regstart[RE_NREGS];
  1954.   unsigned char *best_regend[RE_NREGS];
  1955.  
  1956.   /* Initialize subexpression text positions to -1 to mark ones that no
  1957.      \( or ( and \) or ) has been seen for. Also set all registers to
  1958.      inactive and mark them as not having matched anything or ever
  1959.      failed.  */
  1960.   for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1961.     {
  1962.       regstart[mcnt] = regend[mcnt] = (unsigned char *) -1;
  1963.       IS_ACTIVE (reg_info[mcnt]) = 0;
  1964.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  1965.     }
  1966.   
  1967.   if (regs)
  1968.     for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1969.       regs->start[mcnt] = regs->end[mcnt] = -1;
  1970.  
  1971.   /* Set up pointers to ends of strings.
  1972.      Don't allow the second string to be empty unless both are empty.  */
  1973.   if (size2 == 0)
  1974.     {
  1975.       string2 = string1;
  1976.       size2 = size1;
  1977.       string1 = 0;
  1978.       size1 = 0;
  1979.     }
  1980.   end1 = string1 + size1;
  1981.   end2 = string2 + size2;
  1982.  
  1983.   /* Compute where to stop matching, within the two strings.  */
  1984.   if (mstop <= size1)
  1985.     {
  1986.       end_match_1 = string1 + mstop;
  1987.       end_match_2 = string2;
  1988.     }
  1989.   else
  1990.     {
  1991.       end_match_1 = end1;
  1992.       end_match_2 = string2 + mstop - size1;
  1993.     }
  1994.  
  1995.   /* `p' scans through the pattern as `d' scans through the data. `dend'
  1996.      is the end of the input string that `d' points within. `d' is
  1997.      advanced into the following input string whenever necessary, but
  1998.      this happens before fetching; therefore, at the beginning of the
  1999.      loop, `d' can be pointing at the end of a string, but it cannot
  2000.      equal string2.  */
  2001.  
  2002.   if (size1 != 0 && pos <= size1)
  2003.     d = string1 + pos, dend = end_match_1;
  2004.   else
  2005.     d = string2 + pos - size1, dend = end_match_2;
  2006.  
  2007.  
  2008.   /* This loops over pattern commands.  It exits by returning from the
  2009.      function if match is complete, or it drops through if match fails
  2010.      at this starting point in the input data.  */
  2011.  
  2012.   while (1)
  2013.     {
  2014.       is_a_jump_n = 0;
  2015.       /* End of pattern means we might have succeeded.  */
  2016.       if (p == pend)
  2017.     {
  2018.       /* If not end of string, try backtracking.  Otherwise done.  */
  2019.           if (d != end_match_2)
  2020.         {
  2021.               if (stackp != stackb)
  2022.                 {
  2023.                   /* More failure points to try.  */
  2024.  
  2025.                   unsigned in_same_string = 
  2026.                           IS_IN_FIRST_STRING (best_regend[0]) 
  2027.                         == MATCHING_IN_FIRST_STRING;
  2028.  
  2029.                   /* If exceeds best match so far, save it.  */
  2030.                   if (! best_regs_set
  2031.                       || (in_same_string && d > best_regend[0])
  2032.                       || (! in_same_string && ! MATCHING_IN_FIRST_STRING))
  2033.                     {
  2034.                       best_regs_set = 1;
  2035.                       best_regend[0] = d;    /* Never use regstart[0].  */
  2036.                       
  2037.                       for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  2038.                         {
  2039.                           best_regstart[mcnt] = regstart[mcnt];
  2040.                           best_regend[mcnt] = regend[mcnt];
  2041.                         }
  2042.                     }
  2043.                   goto fail;           
  2044.                 }
  2045.               /* If no failure points, don't restore garbage.  */
  2046.               else if (best_regs_set)   
  2047.                 {
  2048.           restore_best_regs:
  2049.                   /* Restore best match.  */
  2050.                   d = best_regend[0];
  2051.                   
  2052.           for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  2053.             {
  2054.               regstart[mcnt] = best_regstart[mcnt];
  2055.               regend[mcnt] = best_regend[mcnt];
  2056.             }
  2057.                 }
  2058.             }
  2059.  
  2060.       /* If caller wants register contents data back, convert it 
  2061.          to indices.  */
  2062.       if (regs)
  2063.         {
  2064.           regs->start[0] = pos;
  2065.           if (MATCHING_IN_FIRST_STRING)
  2066.         regs->end[0] = d - string1;
  2067.           else
  2068.         regs->end[0] = d - string2 + size1;
  2069.           for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  2070.         {
  2071.           if (regend[mcnt] == (unsigned char *) -1)
  2072.             {
  2073.               regs->start[mcnt] = -1;
  2074.               regs->end[mcnt] = -1;
  2075.               continue;
  2076.             }
  2077.           if (IS_IN_FIRST_STRING (regstart[mcnt]))
  2078.             regs->start[mcnt] = regstart[mcnt] - string1;
  2079.           else
  2080.             regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  2081.                     
  2082.           if (IS_IN_FIRST_STRING (regend[mcnt]))
  2083.             regs->end[mcnt] = regend[mcnt] - string1;
  2084.           else
  2085.             regs->end[mcnt] = regend[mcnt] - string2 + size1;
  2086.         }
  2087.         }
  2088.       return d - pos - (MATCHING_IN_FIRST_STRING 
  2089.                 ? string1 
  2090.                 : string2 - size1);
  2091.         }
  2092.  
  2093.       /* Otherwise match next pattern command.  */
  2094. #ifdef SWITCH_ENUM_BUG
  2095.       switch ((int) ((enum regexpcode) *p++))
  2096. #else
  2097.       switch ((enum regexpcode) *p++)
  2098. #endif
  2099.     {
  2100.  
  2101.     /* \( [or `(', as appropriate] is represented by start_memory,
  2102.            \) by stop_memory.  Both of those commands are followed by
  2103.            a register number in the next byte.  The text matched
  2104.            within the \( and \) is recorded under that number.  */
  2105.     case start_memory:
  2106.           regstart[*p] = d;
  2107.           IS_ACTIVE (reg_info[*p]) = 1;
  2108.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  2109.           p++;
  2110.           break;
  2111.  
  2112.     case stop_memory:
  2113.           regend[*p] = d;
  2114.           IS_ACTIVE (reg_info[*p]) = 0;
  2115.  
  2116.           /* If just failed to match something this time around with a sub-
  2117.          expression that's in a loop, try to force exit from the loop.  */
  2118.           if ((! MATCHED_SOMETHING (reg_info[*p])
  2119.            || (enum regexpcode) p[-3] == start_memory)
  2120.           && (p + 1) != pend)              
  2121.             {
  2122.           register unsigned char *p2 = p + 1;
  2123.               mcnt = 0;
  2124.               switch (*p2++)
  2125.                 {
  2126.                   case jump_n:
  2127.             is_a_jump_n = 1;
  2128.                   case finalize_jump:
  2129.           case maybe_finalize_jump:
  2130.           case jump:
  2131.           case dummy_failure_jump:
  2132.                     EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2133.             if (is_a_jump_n)
  2134.               p2 += 2;
  2135.                     break;
  2136.                 }
  2137.           p2 += mcnt;
  2138.         
  2139.               /* If the next operation is a jump backwards in the pattern
  2140.              to an on_failure_jump, exit from the loop by forcing a
  2141.                  failure after pushing on the stack the on_failure_jump's 
  2142.                  jump in the pattern, and d.  */
  2143.           if (mcnt < 0 && (enum regexpcode) *p2++ == on_failure_jump)
  2144.         {
  2145.                   EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2146.                   PUSH_FAILURE_POINT (p2 + mcnt, d);
  2147.                   goto fail;
  2148.                 }
  2149.             }
  2150.           p++;
  2151.           break;
  2152.  
  2153.     /* \<digit> has been turned into a `duplicate' command which is
  2154.            followed by the numeric value of <digit> as the register number.  */
  2155.         case duplicate:
  2156.       {
  2157.         int regno = *p++;   /* Get which register to match against */
  2158.         register unsigned char *d2, *dend2;
  2159.  
  2160.         /* Where in input to try to start matching.  */
  2161.             d2 = regstart[regno];
  2162.             
  2163.             /* Where to stop matching; if both the place to start and
  2164.                the place to stop matching are in the same string, then
  2165.                set to the place to stop, otherwise, for now have to use
  2166.                the end of the first string.  */
  2167.  
  2168.             dend2 = ((IS_IN_FIRST_STRING (regstart[regno]) 
  2169.               == IS_IN_FIRST_STRING (regend[regno]))
  2170.              ? regend[regno] : end_match_1);
  2171.         while (1)
  2172.           {
  2173.         /* If necessary, advance to next segment in register
  2174.                    contents.  */
  2175.         while (d2 == dend2)
  2176.           {
  2177.             if (dend2 == end_match_2) break;
  2178.             if (dend2 == regend[regno]) break;
  2179.             d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  2180.           }
  2181.         /* At end of register contents => success */
  2182.         if (d2 == dend2) break;
  2183.  
  2184.         /* If necessary, advance to next segment in data.  */
  2185.         PREFETCH;
  2186.  
  2187.         /* How many characters left in this segment to match.  */
  2188.         mcnt = dend - d;
  2189.                 
  2190.         /* Want how many consecutive characters we can match in
  2191.                    one shot, so, if necessary, adjust the count.  */
  2192.                 if (mcnt > dend2 - d2)
  2193.           mcnt = dend2 - d2;
  2194.                   
  2195.         /* Compare that many; failure if mismatch, else move
  2196.                    past them.  */
  2197.         if (translate 
  2198.                     ? bcmp_translate (d, d2, mcnt, translate) 
  2199.                     : bcmp (d, d2, mcnt))
  2200.           goto fail;
  2201.         d += mcnt, d2 += mcnt;
  2202.           }
  2203.       }
  2204.       break;
  2205.  
  2206.     case anychar:
  2207.       PREFETCH;      /* Fetch a data character. */
  2208.       /* Match anything but a newline, maybe even a null.  */
  2209.       if ((translate ? translate[*d] : *d) == '\n'
  2210.               || ((obscure_syntax & RE_DOT_NOT_NULL) 
  2211.                   && (translate ? translate[*d] : *d) == '\000'))
  2212.         goto fail;
  2213.       SET_REGS_MATCHED;
  2214.           d++;
  2215.       break;
  2216.  
  2217.     case charset:
  2218.     case charset_not:
  2219.       {
  2220.         int not = 0;        /* Nonzero for charset_not.  */
  2221.         register int c;
  2222.         if (*(p - 1) == (unsigned char) charset_not)
  2223.           not = 1;
  2224.  
  2225.         PREFETCH;        /* Fetch a data character. */
  2226.  
  2227.         if (translate)
  2228.           c = translate[*d];
  2229.         else
  2230.           c = *d;
  2231.  
  2232.         if (c < *p * BYTEWIDTH
  2233.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2234.           not = !not;
  2235.  
  2236.         p += 1 + *p;
  2237.  
  2238.         if (!not) goto fail;
  2239.         SET_REGS_MATCHED;
  2240.             d++;
  2241.         break;
  2242.       }
  2243.  
  2244.     case begline:
  2245.           if ((size1 != 0 && d == string1)
  2246.               || (size1 == 0 && size2 != 0 && d == string2)
  2247.               || (d && d[-1] == '\n')
  2248.               || (size1 == 0 && size2 == 0))
  2249.             break;
  2250.           else
  2251.             goto fail;
  2252.             
  2253.     case endline:
  2254.       if (d == end2
  2255.           || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  2256.         break;
  2257.       goto fail;
  2258.  
  2259.     /* `or' constructs are handled by starting each alternative with
  2260.            an on_failure_jump that points to the start of the next
  2261.            alternative.  Each alternative except the last ends with a
  2262.            jump to the joining point.  (Actually, each jump except for
  2263.            the last one really jumps to the following jump, because
  2264.            tensioning the jumps is a hassle.)  */
  2265.  
  2266.     /* The start of a stupid repeat has an on_failure_jump that points
  2267.        past the end of the repeat text. This makes a failure point so 
  2268.            that on failure to match a repetition, matching restarts past
  2269.            as many repetitions have been found with no way to fail and
  2270.            look for another one.  */
  2271.  
  2272.     /* A smart repeat is similar but loops back to the on_failure_jump
  2273.        so that each repetition makes another failure point.  */
  2274.  
  2275.     case on_failure_jump:
  2276.         on_failure:
  2277.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2278.           PUSH_FAILURE_POINT (p + mcnt, d);
  2279.           break;
  2280.  
  2281.     /* The end of a smart repeat has a maybe_finalize_jump back.
  2282.        Change it either to a finalize_jump or an ordinary jump.  */
  2283.     case maybe_finalize_jump:
  2284.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2285.       {
  2286.         register unsigned char *p2 = p;
  2287.         /* Compare what follows with the beginning of the repeat.
  2288.            If we can establish that there is nothing that they would
  2289.            both match, we can change to finalize_jump.  */
  2290.         while (p2 + 1 != pend
  2291.            && (*p2 == (unsigned char) stop_memory
  2292.                || *p2 == (unsigned char) start_memory))
  2293.           p2 += 2;                /* Skip over reg number.  */
  2294.         if (p2 == pend)
  2295.           p[-3] = (unsigned char) finalize_jump;
  2296.         else if (*p2 == (unsigned char) exactn
  2297.              || *p2 == (unsigned char) endline)
  2298.           {
  2299.         register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  2300.         register unsigned char *p1 = p + mcnt;
  2301.         /* p1[0] ... p1[2] are an on_failure_jump.
  2302.            Examine what follows that.  */
  2303.         if (p1[3] == (unsigned char) exactn && p1[5] != c)
  2304.           p[-3] = (unsigned char) finalize_jump;
  2305.         else if (p1[3] == (unsigned char) charset
  2306.              || p1[3] == (unsigned char) charset_not)
  2307.           {
  2308.             int not = p1[3] == (unsigned char) charset_not;
  2309.             if (c < p1[4] * BYTEWIDTH
  2310.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2311.               not = !not;
  2312.             /* `not' is 1 if c would match.  */
  2313.             /* That means it is not safe to finalize.  */
  2314.             if (!not)
  2315.               p[-3] = (unsigned char) finalize_jump;
  2316.           }
  2317.           }
  2318.       }
  2319.       p -= 2;        /* Point at relative address again.  */
  2320.       if (p[-1] != (unsigned char) finalize_jump)
  2321.         {
  2322.           p[-1] = (unsigned char) jump;    
  2323.           goto nofinalize;
  2324.         }
  2325.         /* Note fall through.  */
  2326.  
  2327.     /* The end of a stupid repeat has a finalize_jump back to the
  2328.            start, where another failure point will be made which will
  2329.            point to after all the repetitions found so far.  */
  2330.  
  2331.         /* Take off failure points put on by matching on_failure_jump 
  2332.            because didn't fail.  Also remove the register information
  2333.            put on by the on_failure_jump.  */
  2334.         case finalize_jump:
  2335.           POP_FAILURE_POINT ();
  2336.         /* Note fall through.  */
  2337.         
  2338.     /* Jump without taking off any failure points.  */
  2339.         case jump:
  2340.     nofinalize:
  2341.       EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2342.       p += mcnt;
  2343.       break;
  2344.  
  2345.         case dummy_failure_jump:
  2346.           /* Normally, the on_failure_jump pushes a failure point, which
  2347.              then gets popped at finalize_jump.  We will end up at
  2348.              finalize_jump, also, and with a pattern of, say, `a+', we
  2349.              are skipping over the on_failure_jump, so we have to push
  2350.              something meaningless for finalize_jump to pop.  */
  2351.           PUSH_FAILURE_POINT (0, 0);
  2352.           goto nofinalize;
  2353.  
  2354.  
  2355.         /* Have to succeed matching what follows at least n times.  Then
  2356.           just handle like an on_failure_jump.  */
  2357.         case succeed_n: 
  2358.           EXTRACT_NUMBER (mcnt, p + 2);
  2359.           /* Originally, this is how many times we HAVE to succeed.  */
  2360.           if (mcnt)
  2361.             {
  2362.                mcnt--;
  2363.            p += 2;
  2364.                STORE_NUMBER_AND_INCR (p, mcnt);
  2365.             }
  2366.       else if (mcnt == 0)
  2367.             {
  2368.           p[2] = unused;
  2369.               p[3] = unused;
  2370.               goto on_failure;
  2371.             }
  2372.           else
  2373.         { 
  2374.               fprintf (stderr, "regex: the succeed_n's n is not set.\n");
  2375.               exit (1);
  2376.         }
  2377.           break;
  2378.         
  2379.         case jump_n: 
  2380.           EXTRACT_NUMBER (mcnt, p + 2);
  2381.           /* Originally, this is how many times we CAN jump.  */
  2382.           if (mcnt)
  2383.             {
  2384.                mcnt--;
  2385.                STORE_NUMBER(p + 2, mcnt);
  2386.            goto nofinalize;         /* Do the jump without taking off
  2387.                             any failure points.  */
  2388.             }
  2389.           /* If don't have to jump any more, skip over the rest of command.  */
  2390.       else      
  2391.         p += 4;             
  2392.           break;
  2393.         
  2394.     case set_number_at:
  2395.       {
  2396.           register unsigned char *p1;
  2397.  
  2398.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2399.             p1 = p + mcnt;
  2400.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2401.         STORE_NUMBER (p1, mcnt);
  2402.             break;
  2403.           }
  2404.  
  2405.         /* Ignore these.  Used to ignore the n of succeed_n's which
  2406.            currently have n == 0.  */
  2407.         case unused:
  2408.           break;
  2409.  
  2410.         case wordbound:
  2411.       if (AT_WORD_BOUNDARY)
  2412.         break;
  2413.       goto fail;
  2414.  
  2415.     case notwordbound:
  2416.       if (AT_WORD_BOUNDARY)
  2417.         goto fail;
  2418.       break;
  2419.  
  2420.     case wordbeg:
  2421.           /* Have to check if AT_STRINGS_BEG before looking at d - 1.  */
  2422.       if (IS_A_LETTER (d) && (AT_STRINGS_BEG || !IS_A_LETTER (d - 1)))
  2423.         break;
  2424.       goto fail;
  2425.  
  2426.     case wordend:
  2427.           /* Have to check if AT_STRINGS_BEG before looking at d - 1.  */
  2428.       if (!AT_STRINGS_BEG && IS_A_LETTER (d - 1) 
  2429.               && (!IS_A_LETTER (d) || AT_STRINGS_END))
  2430.         break;
  2431.       goto fail;
  2432.  
  2433. #ifdef emacs
  2434.     case before_dot:
  2435.       if (PTR_CHAR_POS (d) >= point)
  2436.         goto fail;
  2437.       break;
  2438.  
  2439.     case at_dot:
  2440.       if (PTR_CHAR_POS (d) != point)
  2441.         goto fail;
  2442.       break;
  2443.  
  2444.     case after_dot:
  2445.       if (PTR_CHAR_POS (d) <= point)
  2446.         goto fail;
  2447.       break;
  2448.  
  2449.     case wordchar:
  2450.       mcnt = (int) Sword;
  2451.       goto matchsyntax;
  2452.  
  2453.     case syntaxspec:
  2454.       mcnt = *p++;
  2455.     matchsyntax:
  2456.       PREFETCH;
  2457.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  2458.           SET_REGS_MATCHED;
  2459.       break;
  2460.       
  2461.     case notwordchar:
  2462.       mcnt = (int) Sword;
  2463.       goto matchnotsyntax;
  2464.  
  2465.     case notsyntaxspec:
  2466.       mcnt = *p++;
  2467.     matchnotsyntax:
  2468.       PREFETCH;
  2469.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  2470.       SET_REGS_MATCHED;
  2471.           break;
  2472.  
  2473. #else /* not emacs */
  2474.  
  2475.     case wordchar:
  2476.       PREFETCH;
  2477.           if (!IS_A_LETTER (d))
  2478.             goto fail;
  2479.       SET_REGS_MATCHED;
  2480.       break;
  2481.       
  2482.     case notwordchar:
  2483.       PREFETCH;
  2484.       if (IS_A_LETTER (d))
  2485.             goto fail;
  2486.           SET_REGS_MATCHED;
  2487.       break;
  2488.  
  2489. #endif /* not emacs */
  2490.  
  2491.     case begbuf:
  2492.           if (AT_STRINGS_BEG)
  2493.             break;
  2494.           goto fail;
  2495.  
  2496.         case endbuf:
  2497.       if (AT_STRINGS_END)
  2498.         break;
  2499.       goto fail;
  2500.  
  2501.     case exactn:
  2502.       /* Match the next few pattern characters exactly.
  2503.          mcnt is how many characters to match.  */
  2504.       mcnt = *p++;
  2505.       /* This is written out as an if-else so we don't waste time
  2506.              testing `translate' inside the loop.  */
  2507.           if (translate)
  2508.         {
  2509.           do
  2510.         {
  2511.           PREFETCH;
  2512.           if (translate[*d++] != *p++) goto fail;
  2513.         }
  2514.           while (--mcnt);
  2515.         }
  2516.       else
  2517.         {
  2518.           do
  2519.         {
  2520.           PREFETCH;
  2521.           if (*d++ != *p++) goto fail;
  2522.         }
  2523.           while (--mcnt);
  2524.         }
  2525.       SET_REGS_MATCHED;
  2526.           break;
  2527.     }
  2528.       continue;  /* Successfully executed one pattern command; keep going.  */
  2529.  
  2530.     /* Jump here if any matching operation fails. */
  2531.     fail:
  2532.       if (stackp != stackb)
  2533.     /* A restart point is known.  Restart there and pop it. */
  2534.     {
  2535.           short last_used_reg, this_reg;
  2536.           
  2537.           /* If this failure point is from a dummy_failure_point, just
  2538.              skip it.  */
  2539.       if (!stackp[-2])
  2540.             {
  2541.               POP_FAILURE_POINT ();
  2542.               goto fail;
  2543.             }
  2544.  
  2545.           d = *--stackp;
  2546.       p = *--stackp;
  2547.           if (d >= string1 && d <= end1)
  2548.         dend = end_match_1;
  2549.           /* Restore register info.  */
  2550.           last_used_reg = (short) *--stackp;
  2551.           
  2552.           /* Make the ones that weren't saved -1 or 0 again.  */
  2553.           for (this_reg = RE_NREGS - 1; this_reg > last_used_reg; this_reg--)
  2554.             {
  2555.               regend[this_reg] = (unsigned char *) -1;
  2556.               regstart[this_reg] = (unsigned char *) -1;
  2557.               IS_ACTIVE (reg_info[this_reg]) = 0;
  2558.               MATCHED_SOMETHING (reg_info[this_reg]) = 0;
  2559.             }
  2560.           
  2561.           /* And restore the rest from the stack.  */
  2562.           for ( ; this_reg > 0; this_reg--)
  2563.             {
  2564.               reg_info[this_reg] = *(struct register_info *) *--stackp;
  2565.               regend[this_reg] = *--stackp;
  2566.               regstart[this_reg] = *--stackp;
  2567.             }
  2568.     }
  2569.       else
  2570.         break;   /* Matching at this starting point really fails.  */
  2571.     }
  2572.  
  2573.   if (best_regs_set)
  2574.     goto restore_best_regs;
  2575.   return -1;                     /* Failure to match.  */
  2576. }
  2577.  
  2578.  
  2579. static int
  2580. bcmp_translate (s1, s2, len, translate)
  2581.      unsigned char *s1, *s2;
  2582.      register int len;
  2583.      unsigned char *translate;
  2584. {
  2585.   register unsigned char *p1 = s1, *p2 = s2;
  2586.   while (len)
  2587.     {
  2588.       if (translate [*p1++] != translate [*p2++]) return 1;
  2589.       len--;
  2590.     }
  2591.   return 0;
  2592. }
  2593.  
  2594.  
  2595.  
  2596. /* Entry points compatible with 4.2 BSD regex library.  */
  2597.  
  2598. #ifndef emacs
  2599.  
  2600. static struct re_pattern_buffer re_comp_buf;
  2601.  
  2602. char *
  2603. re_comp (s)
  2604.      char *s;
  2605. {
  2606.   if (!s)
  2607.     {
  2608.       if (!re_comp_buf.buffer)
  2609.     return "No previous regular expression";
  2610.       return 0;
  2611.     }
  2612.  
  2613.   if (!re_comp_buf.buffer)
  2614.     {
  2615.       if (!(re_comp_buf.buffer = (char *) malloc (200)))
  2616.     return "Memory exhausted";
  2617.       re_comp_buf.allocated = 200;
  2618.       if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  2619.     return "Memory exhausted";
  2620.     }
  2621.   return re_compile_pattern (s, strlen (s), &re_comp_buf);
  2622. }
  2623.  
  2624. int
  2625. re_exec (s)
  2626.      char *s;
  2627. {
  2628.   int len = strlen (s);
  2629.   return 0 <= re_search (&re_comp_buf, s, len, 0, len,
  2630.              (struct re_registers *) 0);
  2631. }
  2632. #endif /* not emacs */
  2633.  
  2634.  
  2635.  
  2636. #ifdef test
  2637.  
  2638. #include <stdio.h>
  2639.  
  2640. /* Indexed by a character, gives the upper case equivalent of the
  2641.    character.  */
  2642.  
  2643. char upcase[0400] = 
  2644.   { 000, 001, 002, 003, 004, 005, 006, 007,
  2645.     010, 011, 012, 013, 014, 015, 016, 017,
  2646.     020, 021, 022, 023, 024, 025, 026, 027,
  2647.     030, 031, 032, 033, 034, 035, 036, 037,
  2648.     040, 041, 042, 043, 044, 045, 046, 047,
  2649.     050, 051, 052, 053, 054, 055, 056, 057,
  2650.     060, 061, 062, 063, 064, 065, 066, 067,
  2651.     070, 071, 072, 073, 074, 075, 076, 077,
  2652.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2653.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2654.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2655.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  2656.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2657.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2658.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2659.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  2660.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  2661.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  2662.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  2663.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  2664.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  2665.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  2666.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  2667.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  2668.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  2669.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  2670.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  2671.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  2672.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  2673.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  2674.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  2675.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  2676.   };
  2677.  
  2678. #ifdef canned
  2679.  
  2680. #include "tests.h"
  2681.  
  2682. typedef enum { extended_test, basic_test } test_type;
  2683.  
  2684. /* Use this to run the tests we've thought of.  */
  2685.  
  2686. void
  2687. main ()
  2688. {
  2689.   test_type t = extended_test;
  2690.  
  2691.   if (t == basic_test)
  2692.     {
  2693.       printf ("Running basic tests:\n\n");
  2694.       test_posix_basic ();
  2695.     }
  2696.   else if (t == extended_test)
  2697.     {
  2698.       printf ("Running extended tests:\n\n");
  2699.       test_posix_extended (); 
  2700.     }
  2701. }
  2702.  
  2703. #else /* not canned */
  2704.  
  2705. /* Use this to run interactive tests.  */
  2706.  
  2707. void
  2708. main (argc, argv)
  2709.      int argc;
  2710.      char **argv;
  2711. {
  2712.   char pat[80];
  2713.   struct re_pattern_buffer buf;
  2714.   int i;
  2715.   char c;
  2716.   char fastmap[(1 << BYTEWIDTH)];
  2717.  
  2718.   /* Allow a command argument to specify the style of syntax.  */
  2719.   if (argc > 1)
  2720.     obscure_syntax = atoi (argv[1]);
  2721.  
  2722.   buf.allocated = 40;
  2723.   buf.buffer = (char *) malloc (buf.allocated);
  2724.   buf.fastmap = fastmap;
  2725.   buf.translate = upcase;
  2726.  
  2727.   while (1)
  2728.     {
  2729.       gets (pat);
  2730.  
  2731.       if (*pat)
  2732.     {
  2733.           re_compile_pattern (pat, strlen(pat), &buf);
  2734.  
  2735.       for (i = 0; i < buf.used; i++)
  2736.         printchar (buf.buffer[i]);
  2737.  
  2738.       putchar ('\n');
  2739.  
  2740.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  2741.  
  2742.       re_compile_fastmap (&buf);
  2743.       printf ("Allowed by fastmap: ");
  2744.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  2745.         if (fastmap[i]) printchar (i);
  2746.       putchar ('\n');
  2747.     }
  2748.  
  2749.       gets (pat);    /* Now read the string to match against */
  2750.  
  2751.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  2752.       printf ("Match value %d.\n", i);
  2753.     }
  2754. }
  2755.  
  2756. #endif
  2757.  
  2758.  
  2759. #ifdef NOTDEF
  2760. print_buf (bufp)
  2761.      struct re_pattern_buffer *bufp;
  2762. {
  2763.   int i;
  2764.  
  2765.   printf ("buf is :\n----------------\n");
  2766.   for (i = 0; i < bufp->used; i++)
  2767.     printchar (bufp->buffer[i]);
  2768.   
  2769.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  2770.   
  2771.   printf ("Allowed by fastmap: ");
  2772.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  2773.     if (bufp->fastmap[i])
  2774.       printchar (i);
  2775.   printf ("\nAllowed by translate: ");
  2776.   if (bufp->translate)
  2777.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  2778.       if (bufp->translate[i])
  2779.     printchar (i);
  2780.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  2781.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  2782. }
  2783. #endif /* NOTDEF */
  2784.  
  2785. printchar (c)
  2786.      char c;
  2787. {
  2788.   if (c < 040 || c >= 0177)
  2789.     {
  2790.       putchar ('\\');
  2791.       putchar (((c >> 6) & 3) + '0');
  2792.       putchar (((c >> 3) & 7) + '0');
  2793.       putchar ((c & 7) + '0');
  2794.     }
  2795.   else
  2796.     putchar (c);
  2797. }
  2798.  
  2799. error (string)
  2800.      char *string;
  2801. {
  2802.   puts (string);
  2803.   exit (1);
  2804. }
  2805. #endif /* test */
  2806.