home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gawk213s.lzh / GAWK213S / REGEX.C < prev    next >
C/C++ Source or Header  |  1993-07-29  |  86KB  |  2,860 lines

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