home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / DIFF15.ZIP / REGEX.C < prev    next >
C/C++ Source or Header  |  1990-11-29  |  83KB  |  2,771 lines

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