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