home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / utils / hwgrcs / hwgdiff / rcs.rcsfiles / regex.c,v < prev    next >
Encoding:
Text File  |  1993-02-20  |  111.1 KB  |  4,122 lines

  1. head    1.2;
  2. access;
  3. symbols
  4.     HWGDIFF_Fish:1.2
  5.     HWGDIFF:1.2;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 1.2
  11. date    93.01.19.14.32.45;    author heinz;    state Exp;
  12. branches;
  13. next    1.1;
  14.  
  15. 1.1
  16. date    93.01.19.14.04.33;    author heinz;    state Exp;
  17. branches;
  18. next    ;
  19.  
  20.  
  21. desc
  22. @RCS for the first time ...
  23. @
  24.  
  25.  
  26. 1.2
  27. log
  28. @Amiga port
  29. @
  30. text
  31. @/* Extended regular expression matching and search library.
  32.    Copyright (C) 1985, 1989-90 Free Software Foundation, Inc.
  33.  
  34.    This program is free software; you can redistribute it and/or modify
  35.    it under the terms of the GNU General Public License as published by
  36.    the Free Software Foundation; either version 1, or (at your option)
  37.    any later version.
  38.  
  39.    This program is distributed in the hope that it will be useful,
  40.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42.    GNU General Public License for more details.
  43.  
  44.    You should have received a copy of the GNU General Public License
  45.    along with this program; if not, write to the Free Software
  46.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  47.  
  48.  
  49. /* To test, compile with -Dtest.  This Dtestable feature turns this into
  50.    a self-contained program which reads a pattern, describes how it
  51.    compiles, then reads a string and searches for it.
  52.  
  53.    On the other hand, if you compile with both -Dtest and -Dcanned you
  54.    can run some tests we've already thought of.  */
  55.  
  56.  
  57. #ifdef emacs
  58.  
  59. /* The `emacs' switch turns on certain special matching commands
  60.   that make sense only in emacs. */
  61.  
  62. #include "config.h"
  63. #include "lisp.h"
  64. #include "buffer.h"
  65. #include "syntax.h"
  66.  
  67. #else  /* not emacs */
  68.  
  69. #ifdef AMIGA
  70. #include <string.h>
  71. #include <stdlib.h>
  72. #else
  73. #if defined (USG) || defined (STDC_HEADERS)
  74. #ifndef BSTRING
  75. #include <string.h>
  76. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  77. #define bcmp(s1,s2,n)   memcmp((s1),(s2),(n))
  78. #define bzero(s,n)      memset((s),0,(n))
  79. #endif
  80. #endif
  81.  
  82. #ifdef STDC_HEADERS
  83. #include <stdlib.h>
  84. #else
  85. char *malloc ();
  86. char *realloc ();
  87. #endif
  88.  
  89. /* Make alloca work the best possible way.  */
  90. #ifdef __GNUC__
  91. #define alloca __builtin_alloca
  92. #else
  93. #ifdef sparc
  94. #include <alloca.h>
  95. #else
  96. char *alloca ();
  97. #endif
  98. #endif
  99. #endif
  100.  
  101.  
  102. /* Define the syntax stuff, so we can do the \<, \>, etc.  */
  103.  
  104. /* This must be nonzero for the wordchar and notwordchar pattern
  105.    commands in re_match_2.  */
  106. #ifndef Sword
  107. #define Sword 1
  108. #endif
  109.  
  110. #define SYNTAX(c) re_syntax_table[c]
  111.  
  112.  
  113. #ifdef SYNTAX_TABLE
  114.  
  115. char *re_syntax_table;
  116.  
  117. #else /* not SYNTAX_TABLE */
  118.  
  119. static char re_syntax_table[256];
  120.  
  121.  
  122. static void
  123. init_syntax_once ()
  124. {
  125.    register int c;
  126.    static int done = 0;
  127.  
  128.    if (done)
  129.      return;
  130.  
  131.    bzero (re_syntax_table, sizeof re_syntax_table);
  132.  
  133.    for (c = 'a'; c <= 'z'; c++)
  134.      re_syntax_table[c] = Sword;
  135.  
  136.    for (c = 'A'; c <= 'Z'; c++)
  137.      re_syntax_table[c] = Sword;
  138.  
  139.    for (c = '0'; c <= '9'; c++)
  140.      re_syntax_table[c] = Sword;
  141.  
  142.    done = 1;
  143. }
  144.  
  145. #endif /* SYNTAX_TABLE */
  146. #endif /* emacs */
  147.  
  148. /* We write fatal error messages on standard error.  */
  149. #include <stdio.h>
  150.  
  151. /* isalpha(3) etc. are used for the character classes.  */
  152. #include <ctype.h>
  153. /* Sequents are missing isgraph.  */
  154. #ifndef isgraph
  155. #define isgraph(c) (isprint((c)) && !isspace((c)))
  156. #endif
  157.  
  158. /* Get the interface, including the syntax bits.  */
  159. #include "regex.h"
  160.  
  161.  
  162. /* These are the command codes that appear in compiled regular
  163.    expressions, one per byte.  Some command codes are followed by
  164.    argument bytes.  A command code can specify any interpretation
  165.    whatsoever for its arguments.  Zero-bytes may appear in the compiled
  166.    regular expression.
  167.  
  168.    The value of `exactn' is needed in search.c (search_buffer) in emacs.
  169.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  170.    `exactn' we use here must also be 1.  */
  171.  
  172. enum regexpcode
  173.   {
  174.     unused=0,
  175.     exactn=1, /* Followed by one byte giving n, then by n literal bytes.  */
  176.     begline,  /* Fail unless at beginning of line.  */
  177.     endline,  /* Fail unless at end of line.  */
  178.     jump,     /* Followed by two bytes giving relative address to jump to.  */
  179.     on_failure_jump,     /* Followed by two bytes giving relative address of
  180.                 place to resume at in case of failure.  */
  181.     finalize_jump,     /* Throw away latest failure point and then jump to
  182.                 address.  */
  183.     maybe_finalize_jump, /* Like jump but finalize if safe to do so.
  184.                 This is used to jump back to the beginning
  185.                 of a repeat.  If the command that follows
  186.                 this jump is clearly incompatible with the
  187.                 one at the beginning of the repeat, such that
  188.                 we can be sure that there is no use backtracking
  189.                 out of repetitions already completed,
  190.                 then we finalize.  */
  191.     dummy_failure_jump,  /* Jump, and push a dummy failure point. This
  192.                 failure point will be thrown away if an attempt
  193.                 is made to use it for a failure. A + construct
  194.                 makes this before the first repeat.  Also
  195.                 use it as an intermediary kind of jump when
  196.                 compiling an or construct.    */
  197.     succeed_n,     /* Used like on_failure_jump except has to succeed n times;
  198.             then gets turned into an on_failure_jump. The relative
  199.             address following it is useless until then.  The
  200.             address is followed by two bytes containing n.  */
  201.     jump_n,     /* Similar to jump, but jump n times only; also the relative
  202.             address following is in turn followed by yet two more bytes
  203.             containing n.  */
  204.     set_number_at,    /* Set the following relative location to the
  205.                subsequent number.  */
  206.     anychar,     /* Matches any (more or less) one character.  */
  207.     charset,     /* Matches any one char belonging to specified set.
  208.             First following byte is number of bitmap bytes.
  209.             Then come bytes for a bitmap saying which chars are in.
  210.             Bits in each byte are ordered low-bit-first.
  211.             A character is in the set if its bit is 1.
  212.             A character too large to have a bit in the map
  213.             is automatically not in the set.  */
  214.     charset_not, /* Same parameters as charset, but match any character
  215.             that is not one of those specified.  */
  216.     start_memory, /* Start remembering the text that is matched, for
  217.             storing in a memory register.  Followed by one
  218.             byte containing the register number.  Register numbers
  219.             must be in the range 0 through RE_NREGS.  */
  220.     stop_memory, /* Stop remembering the text that is matched
  221.             and store it in a memory register.    Followed by
  222.             one byte containing the register number. Register
  223.             numbers must be in the range 0 through RE_NREGS.  */
  224.     duplicate,     /* Match a duplicate of something remembered.
  225.             Followed by one byte containing the index of the memory
  226.             register.  */
  227.     before_dot,  /* Succeeds if before point.  */
  228.     at_dot,     /* Succeeds if at point.  */
  229.     after_dot,     /* Succeeds if after point.  */
  230.     begbuf,     /* Succeeds if at beginning of buffer.  */
  231.     endbuf,     /* Succeeds if at end of buffer.  */
  232.     wordchar,     /* Matches any word-constituent character.  */
  233.     notwordchar, /* Matches any char that is not a word-constituent.  */
  234.     wordbeg,     /* Succeeds if at word beginning.  */
  235.     wordend,     /* Succeeds if at word end.  */
  236.     wordbound,     /* Succeeds if at a word boundary.  */
  237.     notwordbound,/* Succeeds if not at a word boundary.  */
  238.     syntaxspec,  /* Matches any character whose syntax is specified.
  239.             followed by a byte which contains a syntax code,
  240.             e.g., Sword.  */
  241.     notsyntaxspec /* Matches any character whose syntax differs from
  242.              that specified.  */
  243.   };
  244.  
  245.  
  246. /* Number of failure points to allocate space for initially,
  247.    when matching.  If this number is exceeded, more space is allocated,
  248.    so it is not a hard limit.  */
  249.  
  250. #ifndef NFAILURES
  251. #define NFAILURES 80
  252. #endif
  253.  
  254. #ifdef CHAR_UNSIGNED
  255. #define SIGN_EXTEND_CHAR(c) ((c)>(char)127?(c)-256:(c)) /* for IBM RT */
  256. #endif
  257. #ifndef SIGN_EXTEND_CHAR
  258. #define SIGN_EXTEND_CHAR(x) (x)
  259. #endif
  260.  
  261.  
  262. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  263. #define STORE_NUMBER(destination, number)                               \
  264.   { (destination)[0] = (number) & 0377;                                 \
  265.     (destination)[1] = (number) >> 8; }
  266.  
  267. /* Same as STORE_NUMBER, except increment the destination pointer to
  268.    the byte after where the number is stored.  Watch out that values for
  269.    DESTINATION such as p + 1 won't work, whereas p will.  */
  270. #define STORE_NUMBER_AND_INCR(destination, number)                      \
  271.   { STORE_NUMBER(destination, number);                                  \
  272.     (destination) += 2; }
  273.  
  274.  
  275. /* Put into DESTINATION a number stored in two contingous bytes starting
  276.    at SOURCE.  */
  277. #define EXTRACT_NUMBER(destination, source)                             \
  278.   { (destination) = *(source) & 0377;                                   \
  279.     (destination) += SIGN_EXTEND_CHAR (*(char *)((source) + 1)) << 8; }
  280.  
  281. /* Same as EXTRACT_NUMBER, except increment the pointer for source to
  282.    point to second byte of SOURCE.  Note that SOURCE has to be a value
  283.    such as p, not, e.g., p + 1. */
  284. #define EXTRACT_NUMBER_AND_INCR(destination, source)                    \
  285.   { EXTRACT_NUMBER (destination, source);                               \
  286.     (source) += 2; }
  287.  
  288.  
  289. /* Specify the precise syntax of regexps for compilation.  This provides
  290.    for compatibility for various utilities which historically have
  291.    different, incompatible syntaxes.
  292.  
  293.    The argument SYNTAX is a bit-mask comprised of the various bits
  294.    defined in regex.h.    */
  295.  
  296. int
  297. re_set_syntax (syntax)
  298.   int syntax;
  299. {
  300.   int ret;
  301.  
  302.   ret = obscure_syntax;
  303.   obscure_syntax = syntax;
  304.   return ret;
  305. }
  306.  
  307. /* Set by re_set_syntax to the current regexp syntax to recognize.  */
  308. int obscure_syntax = 0;
  309.  
  310.  
  311.  
  312. /* Macros for re_compile_pattern, which is found below these definitions.  */
  313.  
  314. #define CHAR_CLASS_MAX_LENGTH  6
  315.  
  316. /* Fetch the next character in the uncompiled pattern, translating it if
  317.    necessary.  */
  318. #define PATFETCH(c)                                                     \
  319.   {if (p == pend) goto end_of_pattern;                                  \
  320.   c = * (unsigned char *) p++;                                          \
  321.   if (translate) c = translate[c]; }
  322.  
  323. /* Fetch the next character in the uncompiled pattern, with no
  324.    translation.  */
  325. #define PATFETCH_RAW(c)                                                 \
  326.  {if (p == pend) goto end_of_pattern;                                   \
  327.   c = * (unsigned char *) p++; }
  328.  
  329. #define PATUNFETCH p--
  330.  
  331.  
  332. /* If the buffer isn't allocated when it comes in, use this.  */
  333. #define INIT_BUF_SIZE  28
  334.  
  335. /* Make sure we have at least N more bytes of space in buffer.    */
  336. #define GET_BUFFER_SPACE(n)                                             \
  337.   {                                    \
  338.     while (b - bufp->buffer + (n) >= bufp->allocated)                   \
  339.       EXTEND_BUFFER;                            \
  340.   }
  341.  
  342. /* Make sure we have one more byte of buffer space and then add CH to it.  */
  343. #define BUFPUSH(ch)                                                     \
  344.   {                                    \
  345.     GET_BUFFER_SPACE (1);                                               \
  346.     *b++ = (char) (ch);                                                 \
  347.   }
  348.  
  349. /* Extend the buffer by twice its current size via reallociation and
  350.    reset the pointers that pointed into the old allocation to point to
  351.    the correct places in the new allocation.  If extending the buffer
  352.    results in it being larger than 1 << 16, then flag memory exhausted.  */
  353. #define EXTEND_BUFFER                            \
  354.   { char *old_buffer = bufp->buffer;                    \
  355.     if (bufp->allocated == (1L<<16)) goto too_big;                      \
  356.     bufp->allocated *= 2;                        \
  357.     if (bufp->allocated > (1L<<16)) bufp->allocated = (1L<<16);         \
  358.     bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated);    \
  359.     if (bufp->buffer == 0)                                              \
  360.       goto memory_exhausted;                        \
  361.     b = (b - old_buffer) + bufp->buffer;                                \
  362.     if (fixup_jump)                                                     \
  363.       fixup_jump = (fixup_jump - old_buffer) + bufp->buffer;            \
  364.     if (laststart)                                                      \
  365.       laststart = (laststart - old_buffer) + bufp->buffer;              \
  366.     begalt = (begalt - old_buffer) + bufp->buffer;                      \
  367.     if (pending_exact)                                                  \
  368.       pending_exact = (pending_exact - old_buffer) + bufp->buffer;      \
  369.   }
  370.  
  371. /* Set the bit for character C in a character set list.  */
  372. #define SET_LIST_BIT(c)  (b[(c) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
  373.  
  374. /* Get the next unsigned number in the uncompiled pattern.  */
  375. #define GET_UNSIGNED_NUMBER(num)                                        \
  376.   { if (p != pend)                                                      \
  377.       {                                 \
  378.     PATFETCH (c);                                                   \
  379.     while (isdigit (c))                                             \
  380.       {                                \
  381.         if (num < 0)                                                \
  382.            num = 0;                         \
  383.         num = num * 10 + c - '0';                                   \
  384.         if (p == pend)                                              \
  385.            break;                            \
  386.         PATFETCH (c);                                               \
  387.       }                                \
  388.     }                                \
  389.   }
  390.  
  391. /* Subroutines for re_compile_pattern.    */
  392. static void store_jump (), insert_jump (), store_jump_n (),
  393.         insert_jump_n (), insert_op_2 ();
  394.  
  395.  
  396. /* re_compile_pattern takes a regular-expression string
  397.    and converts it into a buffer full of byte commands for matching.
  398.  
  399.    PATTERN   is the address of the pattern string
  400.    SIZE      is the length of it.
  401.    BUFP     is a  struct re_pattern_buffer *  which points to the info
  402.          on where to store the byte commands.
  403.          This structure contains a    char *    which points to the
  404.          actual space, which should have been obtained with malloc.
  405.          re_compile_pattern may use realloc to grow the buffer space.
  406.  
  407.    The number of bytes of commands can be found out by looking in
  408.    the `struct re_pattern_buffer' that bufp pointed to, after
  409.    re_compile_pattern returns. */
  410.  
  411. char *
  412. re_compile_pattern (pattern, size, bufp)
  413.      char *pattern;
  414.      int size;
  415.      struct re_pattern_buffer *bufp;
  416. {
  417.   register char *b = bufp->buffer;
  418.   register char *p = pattern;
  419.   char *pend = pattern + size;
  420.   register unsigned c, c1;
  421.   char *p1;
  422.   unsigned char *translate = (unsigned char *) bufp->translate;
  423.  
  424.   /* Address of the count-byte of the most recently inserted `exactn'
  425.      command.  This makes it possible to tell whether a new exact-match
  426.      character can be added to that command or requires a new `exactn'
  427.      command.  */
  428.  
  429.   char *pending_exact = 0;
  430.  
  431.   /* Address of the place where a forward-jump should go to the end of
  432.      the containing expression.  Each alternative of an `or', except the
  433.      last, ends with a forward-jump of this sort.  */
  434.  
  435.   char *fixup_jump = 0;
  436.  
  437.   /* Address of start of the most recently finished expression.
  438.      This tells postfix * where to find the start of its operand.  */
  439.  
  440.   char *laststart = 0;
  441.  
  442.   /* In processing a repeat, 1 means zero matches is allowed.  */
  443.  
  444.   char zero_times_ok;
  445.  
  446.   /* In processing a repeat, 1 means many matches is allowed.  */
  447.  
  448.   char many_times_ok;
  449.  
  450.   /* Address of beginning of regexp, or inside of last \(.  */
  451.  
  452.   char *begalt = b;
  453.  
  454.   /* In processing an interval, at least this many matches must be made.  */
  455.   int lower_bound;
  456.  
  457.   /* In processing an interval, at most this many matches can be made.    */
  458.   int upper_bound;
  459.  
  460.   /* Place in pattern (i.e., the {) to which to go back if the interval
  461.      is invalid.  */
  462.   char *beg_interval = 0;
  463.  
  464.   /* Stack of information saved by \( and restored by \).
  465.      Four stack elements are pushed by each \(:
  466.        First, the value of b.
  467.        Second, the value of fixup_jump.
  468.        Third, the value of regnum.
  469.        Fourth, the value of begalt.  */
  470.  
  471.   int stackb[40];
  472.   int *stackp = stackb;
  473.   int *stacke = stackb + 40;
  474.   int *stackt;
  475.  
  476.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  477.      where it becomes the register number to put in the stop_memory
  478.      command.  */
  479.  
  480.   int regnum = 1;
  481.  
  482.   bufp->fastmap_accurate = 0;
  483.  
  484. #ifndef emacs
  485. #ifndef SYNTAX_TABLE
  486.   /* Initialize the syntax table.  */
  487.    init_syntax_once();
  488. #endif
  489. #endif
  490.  
  491.   if (bufp->allocated == 0)
  492.     {
  493.       bufp->allocated = INIT_BUF_SIZE;
  494.       if (bufp->buffer)
  495.     /* EXTEND_BUFFER loses when bufp->allocated is 0.  */
  496.     bufp->buffer = (char *) realloc (bufp->buffer, INIT_BUF_SIZE);
  497.       else
  498.     /* Caller did not allocate a buffer.  Do it for them.  */
  499.     bufp->buffer = (char *) malloc (INIT_BUF_SIZE);
  500.       if (!bufp->buffer) goto memory_exhausted;
  501.       begalt = b = bufp->buffer;
  502.     }
  503.  
  504.   while (p != pend)
  505.     {
  506.       PATFETCH (c);
  507.  
  508.       switch (c)
  509.     {
  510.     case '$':
  511.       {
  512.         char *p1 = p;
  513.         /* When testing what follows the $,
  514.            look past the \-constructs that don't consume anything.  */
  515.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  516.           while (p1 != pend)
  517.         {
  518.           if (*p1 == '\\' && p1 + 1 != pend
  519.               && (p1[1] == '<' || p1[1] == '>'
  520.               || p1[1] == '`' || p1[1] == '\''
  521. #ifdef emacs
  522.               || p1[1] == '='
  523. #endif
  524.               || p1[1] == 'b' || p1[1] == 'B'))
  525.             p1 += 2;
  526.           else
  527.             break;
  528.         }
  529.         if (obscure_syntax & RE_TIGHT_VBAR)
  530.           {
  531.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p1 != pend)
  532.           goto normal_char;
  533.         /* Make operand of last vbar end before this `$'.  */
  534.         if (fixup_jump)
  535.           store_jump (fixup_jump, jump, b);
  536.         fixup_jump = 0;
  537.         BUFPUSH (endline);
  538.         break;
  539.           }
  540.         /* $ means succeed if at end of line, but only in special contexts.
  541.           If validly in the middle of a pattern, it is a normal character. */
  542.  
  543.         if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && p1 != pend)
  544.           goto invalid_pattern;
  545.         if (p1 == pend || *p1 == '\n'
  546.         || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  547.         || (obscure_syntax & RE_NO_BK_PARENS
  548.             ? *p1 == ')'
  549.             : *p1 == '\\' && p1[1] == ')')
  550.         || (obscure_syntax & RE_NO_BK_VBAR
  551.             ? *p1 == '|'
  552.             : *p1 == '\\' && p1[1] == '|'))
  553.           {
  554.         BUFPUSH (endline);
  555.         break;
  556.           }
  557.         goto normal_char;
  558.       }
  559.     case '^':
  560.       /* ^ means succeed if at beg of line, but only if no preceding
  561.          pattern.  */
  562.  
  563.       if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && laststart)
  564.         goto invalid_pattern;
  565.       if (laststart && p - 2 >= pattern && p[-2] != '\n'
  566.            && !(obscure_syntax & RE_CONTEXT_INDEP_OPS))
  567.         goto normal_char;
  568.       if (obscure_syntax & RE_TIGHT_VBAR)
  569.         {
  570.           if (p != pattern + 1
  571.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  572.         goto normal_char;
  573.           BUFPUSH (begline);
  574.           begalt = b;
  575.         }
  576.       else
  577.         BUFPUSH (begline);
  578.       break;
  579.  
  580.     case '+':
  581.     case '?':
  582.       if ((obscure_syntax & RE_BK_PLUS_QM)
  583.           || (obscure_syntax & RE_LIMITED_OPS))
  584.         goto normal_char;
  585.     handle_plus:
  586.     case '*':
  587.       /* If there is no previous pattern, char not special. */
  588.       if (!laststart)
  589.         {
  590.           if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  591.         goto invalid_pattern;
  592.           else if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  593.         goto normal_char;
  594.         }
  595.       /* If there is a sequence of repetition chars,
  596.          collapse it down to just one.  */
  597.       zero_times_ok = 0;
  598.       many_times_ok = 0;
  599.       while (1)
  600.         {
  601.           zero_times_ok |= c != '+';
  602.           many_times_ok |= c != '?';
  603.           if (p == pend)
  604.         break;
  605.           PATFETCH (c);
  606.           if (c == '*')
  607.         ;
  608.           else if (!(obscure_syntax & RE_BK_PLUS_QM)
  609.                && (c == '+' || c == '?'))
  610.         ;
  611.           else if ((obscure_syntax & RE_BK_PLUS_QM)
  612.                && c == '\\')
  613.         {
  614.           int c1;
  615.           PATFETCH (c1);
  616.           if (!(c1 == '+' || c1 == '?'))
  617.             {
  618.               PATUNFETCH;
  619.               PATUNFETCH;
  620.               break;
  621.             }
  622.           c = c1;
  623.         }
  624.           else
  625.         {
  626.           PATUNFETCH;
  627.           break;
  628.         }
  629.         }
  630.  
  631.       /* Star, etc. applied to an empty pattern is equivalent
  632.          to an empty pattern.  */
  633.       if (!laststart)
  634.         break;
  635.  
  636.       /* Now we know whether or not zero matches is allowed
  637.          and also whether or not two or more matches is allowed.  */
  638.       if (many_times_ok)
  639.         {
  640.           /* If more than one repetition is allowed, put in at the
  641.          end a backward relative jump from b to before the next
  642.          jump we're going to put in below (which jumps from
  643.          laststart to after this jump).  */
  644.           GET_BUFFER_SPACE (3);
  645.           store_jump (b, maybe_finalize_jump, laststart - 3);
  646.           b += 3;    /* Because store_jump put stuff here.  */
  647.         }
  648.       /* On failure, jump from laststart to b + 3, which will be the
  649.          end of the buffer after this jump is inserted.  */
  650.       GET_BUFFER_SPACE (3);
  651.       insert_jump (on_failure_jump, laststart, b + 3, b);
  652.       pending_exact = 0;
  653.       b += 3;
  654.       if (!zero_times_ok)
  655.         {
  656.           /* At least one repetition is required, so insert a
  657.          dummy-failure before the initial on-failure-jump
  658.          instruction of the loop. This effects a skip over that
  659.          instruction the first time we hit that loop.  */
  660.           GET_BUFFER_SPACE (6);
  661.           insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  662.           b += 3;
  663.         }
  664.       break;
  665.  
  666.     case '.':
  667.       laststart = b;
  668.       BUFPUSH (anychar);
  669.       break;
  670.  
  671.     case '[':
  672.       if (p == pend)
  673.         goto invalid_pattern;
  674.       while (b - bufp->buffer
  675.          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  676.         EXTEND_BUFFER;
  677.  
  678.       laststart = b;
  679.       if (*p == '^')
  680.         {
  681.           BUFPUSH (charset_not);
  682.           p++;
  683.         }
  684.       else
  685.         BUFPUSH (charset);
  686.       p1 = p;
  687.  
  688.       BUFPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  689.       /* Clear the whole map */
  690.       bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  691.  
  692.       if ((obscure_syntax & RE_HAT_NOT_NEWLINE) && b[-2] == charset_not)
  693.         SET_LIST_BIT ('\n');
  694.  
  695.  
  696.       /* Read in characters and ranges, setting map bits.  */
  697.       while (1)
  698.         {
  699.           PATFETCH (c);
  700.  
  701.           /* If set, \ escapes characters when inside [...].  */
  702.           if ((obscure_syntax & RE_AWK_CLASS_HACK) && c == '\\')
  703.         {
  704.           PATFETCH(c1);
  705.           SET_LIST_BIT (c1);
  706.           continue;
  707.         }
  708.           if (c == ']')
  709.         {
  710.           if (p == p1 + 1)
  711.             {
  712.               /* If this is an empty bracket expression.  */
  713.               if ((obscure_syntax & RE_NO_EMPTY_BRACKETS)
  714.               && p == pend)
  715.             goto invalid_pattern;
  716.             }
  717.           else
  718.             /* Stop if this isn't merely a ] inside a bracket
  719.                expression, but rather the end of a bracket
  720.                expression.  */
  721.             break;
  722.         }
  723.           /* Get a range.  */
  724.           if (p[0] == '-' && p[1] != ']')
  725.         {
  726.           PATFETCH (c1);
  727.           PATFETCH (c1);
  728.  
  729.           if ((obscure_syntax & RE_NO_EMPTY_RANGES) && c > c1)
  730.             goto invalid_pattern;
  731.  
  732.           if ((obscure_syntax & RE_NO_HYPHEN_RANGE_END)
  733.               && c1 == '-' && *p != ']')
  734.             goto invalid_pattern;
  735.  
  736.           while (c <= c1)
  737.             {
  738.               SET_LIST_BIT (c);
  739.               c++;
  740.             }
  741.         }
  742.           else if ((obscure_syntax & RE_CHAR_CLASSES)
  743.             &&  c == '[' && p[0] == ':')
  744.         {
  745.           /* Longest valid character class word has six characters.  */
  746.           char str[CHAR_CLASS_MAX_LENGTH];
  747.           PATFETCH (c);
  748.           c1 = 0;
  749.           /* If no ] at end.  */
  750.           if (p == pend)
  751.             goto invalid_pattern;
  752.           while (1)
  753.             {
  754.               /* Don't translate the ``character class'' characters.  */
  755.               PATFETCH_RAW (c);
  756.               if (c == ':' || c == ']' || p == pend
  757.               || c1 == CHAR_CLASS_MAX_LENGTH)
  758.             break;
  759.               str[c1++] = c;
  760.             }
  761.           str[c1] = '\0';
  762.           if (p == pend
  763.               || c == ']'       /* End of the bracket expression.  */
  764.               || p[0] != ']'
  765.               || p + 1 == pend
  766.               || (strcmp (str, "alpha") != 0
  767.               && strcmp (str, "upper") != 0
  768.               && strcmp (str, "lower") != 0
  769.               && strcmp (str, "digit") != 0
  770.               && strcmp (str, "alnum") != 0
  771.               && strcmp (str, "xdigit") != 0
  772.               && strcmp (str, "space") != 0
  773.               && strcmp (str, "print") != 0
  774.               && strcmp (str, "punct") != 0
  775.               && strcmp (str, "graph") != 0
  776.               && strcmp (str, "cntrl") != 0))
  777.             {
  778.                /* Undo the ending character, the letters, and leave
  779.               the leading : and [ (but set bits for them).  */
  780.               c1++;
  781.               while (c1--)
  782.             PATUNFETCH;
  783.               SET_LIST_BIT ('[');
  784.               SET_LIST_BIT (':');
  785.             }
  786.           else
  787.             {
  788.               /* The ] at the end of the character class.  */
  789.               PATFETCH (c);
  790.               if (c != ']')
  791.             goto invalid_pattern;
  792.               for (c = 0; c < (1 << BYTEWIDTH); c++)
  793.             {
  794.               if ((strcmp (str, "alpha") == 0  && isalpha (c))
  795.                    || (strcmp (str, "upper") == 0  && isupper (c))
  796.                    || (strcmp (str, "lower") == 0  && islower (c))
  797.                    || (strcmp (str, "digit") == 0  && isdigit (c))
  798.                    || (strcmp (str, "alnum") == 0  && isalnum (c))
  799.                    || (strcmp (str, "xdigit") == 0  && isxdigit (c))
  800.                    || (strcmp (str, "space") == 0  && isspace (c))
  801.                    || (strcmp (str, "print") == 0  && isprint (c))
  802.                    || (strcmp (str, "punct") == 0  && ispunct (c))
  803.                    || (strcmp (str, "graph") == 0  && isgraph (c))
  804.                    || (strcmp (str, "cntrl") == 0  && iscntrl (c)))
  805.                 SET_LIST_BIT (c);
  806.             }
  807.             }
  808.         }
  809.           else
  810.         SET_LIST_BIT (c);
  811.         }
  812.  
  813.       /* Discard any character set/class bitmap bytes that are all
  814.          0 at the end of the map. Decrement the map-length byte too.  */
  815.       while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  816.         b[-1]--;
  817.       b += b[-1];
  818.       break;
  819.  
  820.     case '(':
  821.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  822.         goto normal_char;
  823.       else
  824.         goto handle_open;
  825.  
  826.     case ')':
  827.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  828.         goto normal_char;
  829.       else
  830.         goto handle_close;
  831.  
  832.     case '\n':
  833.       if (! (obscure_syntax & RE_NEWLINE_OR))
  834.         goto normal_char;
  835.       else
  836.         goto handle_bar;
  837.  
  838.     case '|':
  839.       if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  840.           && (! laststart  ||  p == pend))
  841.         goto invalid_pattern;
  842.       else if (! (obscure_syntax & RE_NO_BK_VBAR))
  843.         goto normal_char;
  844.       else
  845.         goto handle_bar;
  846.  
  847.     case '{':
  848.        if (! ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  849.           && (obscure_syntax & RE_INTERVALS)))
  850.          goto normal_char;
  851.        else
  852.          goto handle_interval;
  853.  
  854.     case '\\':
  855.       if (p == pend) goto invalid_pattern;
  856.       PATFETCH_RAW (c);
  857.       switch (c)
  858.         {
  859.         case '(':
  860.           if (obscure_syntax & RE_NO_BK_PARENS)
  861.         goto normal_backsl;
  862.         handle_open:
  863.           if (stackp == stacke) goto nesting_too_deep;
  864.  
  865.           /* Laststart should point to the start_memory that we are about
  866.          to push (unless the pattern has RE_NREGS or more ('s).  */
  867.           *stackp++ = b - bufp->buffer;
  868.           if (regnum < RE_NREGS)
  869.         {
  870.           BUFPUSH (start_memory);
  871.           BUFPUSH (regnum);
  872.         }
  873.           *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  874.           *stackp++ = regnum++;
  875.           *stackp++ = begalt - bufp->buffer;
  876.           fixup_jump = 0;
  877.           laststart = 0;
  878.           begalt = b;
  879.           break;
  880.  
  881.         case ')':
  882.           if (obscure_syntax & RE_NO_BK_PARENS)
  883.         goto normal_backsl;
  884.         handle_close:
  885.           if (stackp == stackb) goto unmatched_close;
  886.           begalt = *--stackp + bufp->buffer;
  887.           if (fixup_jump)
  888.         store_jump (fixup_jump, jump, b);
  889.           if (stackp[-1] < RE_NREGS)
  890.         {
  891.           BUFPUSH (stop_memory);
  892.           BUFPUSH (stackp[-1]);
  893.         }
  894.           stackp -= 2;
  895.           fixup_jump = *stackp ? *stackp + bufp->buffer - 1 : 0;
  896.           laststart = *--stackp + bufp->buffer;
  897.           break;
  898.  
  899.         case '|':
  900.           if ((obscure_syntax & RE_LIMITED_OPS)
  901.           || (obscure_syntax & RE_NO_BK_VBAR))
  902.         goto normal_backsl;
  903.         handle_bar:
  904.           if (obscure_syntax & RE_LIMITED_OPS)
  905.         goto normal_char;
  906.           /* Insert before the previous alternative a jump which
  907.          jumps to this alternative if the former fails.  */
  908.           GET_BUFFER_SPACE (6);
  909.           insert_jump (on_failure_jump, begalt, b + 6, b);
  910.           pending_exact = 0;
  911.           b += 3;
  912.           /* The alternative before the previous alternative has a
  913.          jump after it which gets executed if it gets matched.
  914.          Adjust that jump so it will jump to the previous
  915.          alternative's analogous jump (put in below, which in
  916.          turn will jump to the next (if any) alternative's such
  917.          jump, etc.).  The last such jump jumps to the correct
  918.          final destination.  */
  919.           if (fixup_jump)
  920.         store_jump (fixup_jump, jump, b);
  921.  
  922.           /* Leave space for a jump after previous alternative---to be
  923.          filled in later.  */
  924.           fixup_jump = b;
  925.           b += 3;
  926.  
  927.           laststart = 0;
  928.           begalt = b;
  929.           break;
  930.  
  931.         case '{':
  932.           if (! (obscure_syntax & RE_INTERVALS)
  933.           /* Let \{ be a literal.  */
  934.           || ((obscure_syntax & RE_INTERVALS)
  935.               && (obscure_syntax & RE_NO_BK_CURLY_BRACES))
  936.           /* If it's the string "\{".  */
  937.           || (p - 2 == pattern  &&  p == pend))
  938.         goto normal_backsl;
  939.         handle_interval:
  940.           beg_interval = p - 1;        /* The {.  */
  941.           /* If there is no previous pattern, this isn't an interval.  */
  942.           if (!laststart)
  943.         {
  944.           if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  945.             goto invalid_pattern;
  946.           else
  947.             goto normal_backsl;
  948.         }
  949.           /* It also isn't an interval if not preceded by an re
  950.          matching a single character or subexpression, or if
  951.          the current type of intervals can't handle back
  952.          references and the previous thing is a back reference.  */
  953.           if (! (*laststart == anychar
  954.              || *laststart == charset
  955.              || *laststart == charset_not
  956.              || *laststart == start_memory
  957.              || (*laststart == exactn  &&  laststart[1] == 1)
  958.              || (! (obscure_syntax & RE_NO_BK_REFS)
  959.              && *laststart == duplicate)))
  960.         {
  961.           if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  962.             goto normal_char;
  963.  
  964.           /* Posix extended syntax is handled in previous
  965.              statement; this is for Posix basic syntax.  */
  966.           if (obscure_syntax & RE_INTERVALS)
  967.             goto invalid_pattern;
  968.  
  969.           goto normal_backsl;
  970.         }
  971.           lower_bound = -1;         /* So can see if are set.  */
  972.           upper_bound = -1;
  973.           GET_UNSIGNED_NUMBER (lower_bound);
  974.           if (c == ',')
  975.         {
  976.           GET_UNSIGNED_NUMBER (upper_bound);
  977.           if (upper_bound < 0)
  978.             upper_bound = RE_DUP_MAX;
  979.         }
  980.           if (upper_bound < 0)
  981.         upper_bound = lower_bound;
  982.           if (! (obscure_syntax & RE_NO_BK_CURLY_BRACES))
  983.         {
  984.           if (c != '\\')
  985.             goto invalid_pattern;
  986.           PATFETCH (c);
  987.         }
  988.           if (c != '}' || lower_bound < 0 || upper_bound > RE_DUP_MAX
  989.           || lower_bound > upper_bound
  990.           || ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  991.               && p != pend  && *p == '{'))
  992.         {
  993.           if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  994.             goto unfetch_interval;
  995.           else
  996.             goto invalid_pattern;
  997.         }
  998.  
  999.           /* If upper_bound is zero, don't want to succeed at all;
  1000.          jump from laststart to b + 3, which will be the end of
  1001.          the buffer after this jump is inserted.  */
  1002.  
  1003.            if (upper_bound == 0)
  1004.          {
  1005.            GET_BUFFER_SPACE (3);
  1006.            insert_jump (jump, laststart, b + 3, b);
  1007.            b += 3;
  1008.          }
  1009.  
  1010.            /* Otherwise, after lower_bound number of succeeds, jump
  1011.           to after the jump_n which will be inserted at the end
  1012.           of the buffer, and insert that jump_n.  */
  1013.            else
  1014.          { /* Set to 5 if only one repetition is allowed and
  1015.               hence no jump_n is inserted at the current end of
  1016.               the buffer; then only space for the succeed_n is
  1017.               needed.  Otherwise, need space for both the
  1018.               succeed_n and the jump_n.  */
  1019.  
  1020.            unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  1021.  
  1022.            GET_BUFFER_SPACE (slots_needed);
  1023.            /* Initialize the succeed_n to n, even though it will
  1024.               be set by its attendant set_number_at, because
  1025.               re_compile_fastmap will need to know it.    Jump to
  1026.               what the end of buffer will be after inserting
  1027.               this succeed_n and possibly appending a jump_n.  */
  1028.            insert_jump_n (succeed_n, laststart, b + slots_needed,
  1029.                   b, lower_bound);
  1030.            b += 5;    /* Just increment for the succeed_n here.  */
  1031.  
  1032.           /* More than one repetition is allowed, so put in at
  1033.              the end of the buffer a backward jump from b to the
  1034.              succeed_n we put in above.  By the time we've gotten
  1035.              to this jump when matching, we'll have matched once
  1036.              already, so jump back only upper_bound - 1 times.    */
  1037.  
  1038.            if (upper_bound > 1)
  1039.              {
  1040.                store_jump_n (b, jump_n, laststart, upper_bound - 1);
  1041.                b += 5;
  1042.                /* When hit this when matching, reset the
  1043.               preceding jump_n's n to upper_bound - 1.  */
  1044.                BUFPUSH (set_number_at);
  1045.                GET_BUFFER_SPACE (2);
  1046.                STORE_NUMBER_AND_INCR (b, -5);
  1047.                STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  1048.              }
  1049.            /* When hit this when matching, set the succeed_n's n.  */
  1050.            GET_BUFFER_SPACE (5);
  1051.            insert_op_2 (set_number_at, laststart, b, 5, lower_bound);
  1052.            b += 5;
  1053.          }
  1054.           pending_exact = 0;
  1055.           beg_interval = 0;
  1056.           break;
  1057.  
  1058.  
  1059.         unfetch_interval:
  1060.           /* If an invalid interval, match the characters as literals.  */
  1061.            if (beg_interval)
  1062.          p = beg_interval;
  1063.            else
  1064.          {
  1065.            fprintf (stderr,
  1066.               "regex: no interval beginning to which to backtrack.\n");
  1067.            exit (1);
  1068.          }
  1069.  
  1070.            beg_interval = 0;
  1071.            PATFETCH (c);            /* normal_char expects char in `c'.  */
  1072.            goto normal_char;
  1073.            break;
  1074.  
  1075. #ifdef emacs
  1076.         case '=':
  1077.           BUFPUSH (at_dot);
  1078.           break;
  1079.  
  1080.         case 's':
  1081.           laststart = b;
  1082.           BUFPUSH (syntaxspec);
  1083.           PATFETCH (c);
  1084.           BUFPUSH (syntax_spec_code[c]);
  1085.           break;
  1086.  
  1087.         case 'S':
  1088.           laststart = b;
  1089.           BUFPUSH (notsyntaxspec);
  1090.           PATFETCH (c);
  1091.           BUFPUSH (syntax_spec_code[c]);
  1092.           break;
  1093. #endif /* emacs */
  1094.  
  1095.         case 'w':
  1096.           laststart = b;
  1097.           BUFPUSH (wordchar);
  1098.           break;
  1099.  
  1100.         case 'W':
  1101.           laststart = b;
  1102.           BUFPUSH (notwordchar);
  1103.           break;
  1104.  
  1105.         case '<':
  1106.           BUFPUSH (wordbeg);
  1107.           break;
  1108.  
  1109.         case '>':
  1110.           BUFPUSH (wordend);
  1111.           break;
  1112.  
  1113.         case 'b':
  1114.           BUFPUSH (wordbound);
  1115.           break;
  1116.  
  1117.         case 'B':
  1118.           BUFPUSH (notwordbound);
  1119.           break;
  1120.  
  1121.         case '`':
  1122.           BUFPUSH (begbuf);
  1123.           break;
  1124.  
  1125.         case '\'':
  1126.           BUFPUSH (endbuf);
  1127.           break;
  1128.  
  1129.         case '1':
  1130.         case '2':
  1131.         case '3':
  1132.         case '4':
  1133.         case '5':
  1134.         case '6':
  1135.         case '7':
  1136.         case '8':
  1137.         case '9':
  1138.           if (obscure_syntax & RE_NO_BK_REFS)
  1139.         goto normal_char;
  1140.           c1 = c - '0';
  1141.           if (c1 >= regnum)
  1142.         {
  1143.           if (obscure_syntax & RE_NO_EMPTY_BK_REF)
  1144.             goto invalid_pattern;
  1145.           else
  1146.             goto normal_char;
  1147.         }
  1148.           /* Can't back reference to a subexpression if inside of it.  */
  1149.           for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  1150.         if (*stackt == c1)
  1151.           goto normal_char;
  1152.           laststart = b;
  1153.           BUFPUSH (duplicate);
  1154.           BUFPUSH (c1);
  1155.           break;
  1156.  
  1157.         case '+':
  1158.         case '?':
  1159.           if (obscure_syntax & RE_BK_PLUS_QM)
  1160.         goto handle_plus;
  1161.           else
  1162.         goto normal_backsl;
  1163.           break;
  1164.  
  1165.         default:
  1166.         normal_backsl:
  1167.           /* You might think it would be useful for \ to mean
  1168.          not to translate; but if we don't translate it
  1169.          it will never match anything.    */
  1170.           if (translate) c = translate[c];
  1171.           goto normal_char;
  1172.         }
  1173.       break;
  1174.  
  1175.     default:
  1176.     normal_char:        /* Expects the character in `c'.  */
  1177.       if (!pending_exact || pending_exact + *pending_exact + 1 != b
  1178.           || *pending_exact == 0177 || *p == '*' || *p == '^'
  1179.           || ((obscure_syntax & RE_BK_PLUS_QM)
  1180.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  1181.           : (*p == '+' || *p == '?'))
  1182.           || ((obscure_syntax & RE_INTERVALS)
  1183.           && ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  1184.               ? *p == '{'
  1185.               : (p[0] == '\\' && p[1] == '{'))))
  1186.         {
  1187.           laststart = b;
  1188.           BUFPUSH (exactn);
  1189.           pending_exact = b;
  1190.           BUFPUSH (0);
  1191.         }
  1192.       BUFPUSH (c);
  1193.       (*pending_exact)++;
  1194.     }
  1195.     }
  1196.  
  1197.   if (fixup_jump)
  1198.     store_jump (fixup_jump, jump, b);
  1199.  
  1200.   if (stackp != stackb) goto unmatched_open;
  1201.  
  1202.   bufp->used = b - bufp->buffer;
  1203.   return 0;
  1204.  
  1205.  invalid_pattern:
  1206.   return "Invalid regular expression";
  1207.  
  1208.  unmatched_open:
  1209.   return "Unmatched \\(";
  1210.  
  1211.  unmatched_close:
  1212.   return "Unmatched \\)";
  1213.  
  1214.  end_of_pattern:
  1215.   return "Premature end of regular expression";
  1216.  
  1217.  nesting_too_deep:
  1218.   return "Nesting too deep";
  1219.  
  1220.  too_big:
  1221.   return "Regular expression too big";
  1222.  
  1223.  memory_exhausted:
  1224.   return "Memory exhausted";
  1225. }
  1226.  
  1227.  
  1228. /* Store a jump of the form <OPCODE> <relative address>.
  1229.    Store in the location FROM a jump operation to jump to relative
  1230.    address FROM - TO.  OPCODE is the opcode to store.  */
  1231.  
  1232. static void
  1233. store_jump (from, opcode, to)
  1234.      char *from, *to;
  1235.      char opcode;
  1236. {
  1237.   from[0] = opcode;
  1238.   STORE_NUMBER(from + 1, to - (from + 3));
  1239. }
  1240.  
  1241.  
  1242. /* Open up space before char FROM, and insert there a jump to TO.
  1243.    CURRENT_END gives the end of the storage not in use, so we know
  1244.    how much data to copy up. OP is the opcode of the jump to insert.
  1245.  
  1246.    If you call this function, you must zero out pending_exact.    */
  1247.  
  1248. static void
  1249. insert_jump (op, from, to, current_end)
  1250.      char op;
  1251.      char *from, *to, *current_end;
  1252. {
  1253.   register char *pfrom = current_end;        /* Copy from here...  */
  1254.   register char *pto = current_end + 3;     /* ...to here.    */
  1255.  
  1256.   while (pfrom != from)
  1257.     *--pto = *--pfrom;
  1258.   store_jump (from, op, to);
  1259. }
  1260.  
  1261.  
  1262. /* Store a jump of the form <opcode> <relative address> <n> .
  1263.  
  1264.    Store in the location FROM a jump operation to jump to relative
  1265.    address FROM - TO.  OPCODE is the opcode to store, N is a number the
  1266.    jump uses, say, to decide how many times to jump.
  1267.  
  1268.    If you call this function, you must zero out pending_exact.    */
  1269.  
  1270. static void
  1271. store_jump_n (from, opcode, to, n)
  1272.      char *from, *to;
  1273.      char opcode;
  1274.      unsigned n;
  1275. {
  1276.   from[0] = opcode;
  1277.   STORE_NUMBER (from + 1, to - (from + 3));
  1278.   STORE_NUMBER (from + 3, n);
  1279. }
  1280.  
  1281.  
  1282. /* Similar to insert_jump, but handles a jump which needs an extra
  1283.    number to handle minimum and maximum cases.    Open up space at
  1284.    location FROM, and insert there a jump to TO.  CURRENT_END gives the
  1285.    end of the storage in use, so we know how much data to copy up. OP is
  1286.    the opcode of the jump to insert.
  1287.  
  1288.    If you call this function, you must zero out pending_exact.    */
  1289.  
  1290. static void
  1291. insert_jump_n (op, from, to, current_end, n)
  1292.      char op;
  1293.      char *from, *to, *current_end;
  1294.      unsigned n;
  1295. {
  1296.   register char *pfrom = current_end;        /* Copy from here...  */
  1297.   register char *pto = current_end + 5;     /* ...to here.    */
  1298.  
  1299.   while (pfrom != from)
  1300.     *--pto = *--pfrom;
  1301.   store_jump_n (from, op, to, n);
  1302. }
  1303.  
  1304.  
  1305. /* Open up space at location THERE, and insert operation OP followed by
  1306.    NUM_1 and NUM_2.  CURRENT_END gives the end of the storage in use, so
  1307.    we know how much data to copy up.
  1308.  
  1309.    If you call this function, you must zero out pending_exact.    */
  1310.  
  1311. static void
  1312. insert_op_2 (op, there, current_end, num_1, num_2)
  1313.      char op;
  1314.      char *there, *current_end;
  1315.      int num_1, num_2;
  1316. {
  1317.   register char *pfrom = current_end;        /* Copy from here...  */
  1318.   register char *pto = current_end + 5;     /* ...to here.    */
  1319.  
  1320.   while (pfrom != there)
  1321.     *--pto = *--pfrom;
  1322.  
  1323.   there[0] = op;
  1324.   STORE_NUMBER (there + 1, num_1);
  1325.   STORE_NUMBER (there + 3, num_2);
  1326. }
  1327.  
  1328.  
  1329.  
  1330. /* Given a pattern, compute a fastmap from it.    The fastmap records
  1331.    which of the (1 << BYTEWIDTH) possible characters can start a string
  1332.    that matches the pattern.  This fastmap is used by re_search to skip
  1333.    quickly over totally implausible text.
  1334.  
  1335.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data
  1336.    area as bufp->fastmap.
  1337.    The other components of bufp describe the pattern to be used.  */
  1338.  
  1339. void
  1340. re_compile_fastmap (bufp)
  1341.      struct re_pattern_buffer *bufp;
  1342. {
  1343.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  1344.   int size = bufp->used;
  1345.   register char *fastmap = bufp->fastmap;
  1346.   register unsigned char *p = pattern;
  1347.   register unsigned char *pend = pattern + size;
  1348.   register int j, k;
  1349.   unsigned char *translate = (unsigned char *) bufp->translate;
  1350.  
  1351.   unsigned char *stackb[NFAILURES];
  1352.   unsigned char **stackp = stackb;
  1353.  
  1354.   unsigned is_a_succeed_n;
  1355.  
  1356.   bzero (fastmap, (1 << BYTEWIDTH));
  1357.   bufp->fastmap_accurate = 1;
  1358.   bufp->can_be_null = 0;
  1359.  
  1360.   while (p)
  1361.     {
  1362.       is_a_succeed_n = 0;
  1363.       if (p == pend)
  1364.     {
  1365.       bufp->can_be_null = 1;
  1366.       break;
  1367.     }
  1368. #ifdef SWITCH_ENUM_BUG
  1369.       switch ((int) ((enum regexpcode) *p++))
  1370. #else
  1371.       switch ((enum regexpcode) *p++)
  1372. #endif
  1373.     {
  1374.     case exactn:
  1375.       if (translate)
  1376.         fastmap[translate[p[1]]] = 1;
  1377.       else
  1378.         fastmap[p[1]] = 1;
  1379.       break;
  1380.  
  1381.     case begline:
  1382.     case before_dot:
  1383.     case at_dot:
  1384.     case after_dot:
  1385.     case begbuf:
  1386.     case endbuf:
  1387.     case wordbound:
  1388.     case notwordbound:
  1389.     case wordbeg:
  1390.     case wordend:
  1391.       continue;
  1392.  
  1393.     case endline:
  1394.       if (translate)
  1395.         fastmap[translate['\n']] = 1;
  1396.       else
  1397.         fastmap['\n'] = 1;
  1398.  
  1399.       if (bufp->can_be_null != 1)
  1400.         bufp->can_be_null = 2;
  1401.       break;
  1402.  
  1403.     case jump_n:
  1404.     case finalize_jump:
  1405.     case maybe_finalize_jump:
  1406.     case jump:
  1407.     case dummy_failure_jump:
  1408.       EXTRACT_NUMBER_AND_INCR (j, p);
  1409.       p += j;
  1410.       if (j > 0)
  1411.         continue;
  1412.       /* Jump backward reached implies we just went through
  1413.          the body of a loop and matched nothing.
  1414.          Opcode jumped to should be an on_failure_jump.
  1415.          Just treat it like an ordinary jump.
  1416.          For a * loop, it has pushed its failure point already;
  1417.          If so, discard that as redundant.    */
  1418.  
  1419.       if ((enum regexpcode) *p != on_failure_jump
  1420.           && (enum regexpcode) *p != succeed_n)
  1421.         continue;
  1422.       p++;
  1423.       EXTRACT_NUMBER_AND_INCR (j, p);
  1424.       p += j;
  1425.       if (stackp != stackb && *stackp == p)
  1426.         stackp--;
  1427.       continue;
  1428.  
  1429.     case on_failure_jump:
  1430.     handle_on_failure_jump:
  1431.       EXTRACT_NUMBER_AND_INCR (j, p);
  1432.       *++stackp = p + j;
  1433.       if (is_a_succeed_n)
  1434.         EXTRACT_NUMBER_AND_INCR (k, p);     /* Skip the n.  */
  1435.       continue;
  1436.  
  1437.     case succeed_n:
  1438.       is_a_succeed_n = 1;
  1439.       /* Get to the number of times to succeed.  */
  1440.       p += 2;
  1441.       /* Increment p past the n for when k != 0.  */
  1442.       EXTRACT_NUMBER_AND_INCR (k, p);
  1443.       if (k == 0)
  1444.         {
  1445.           p -= 4;
  1446.           goto handle_on_failure_jump;
  1447.         }
  1448.       continue;
  1449.  
  1450.     case set_number_at:
  1451.       p += 4;
  1452.       continue;
  1453.  
  1454.     case start_memory:
  1455.     case stop_memory:
  1456.       p++;
  1457.       continue;
  1458.  
  1459.     case duplicate:
  1460.       bufp->can_be_null = 1;
  1461.       fastmap['\n'] = 1;
  1462.     case anychar:
  1463.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1464.         if (j != '\n')
  1465.           fastmap[j] = 1;
  1466.       if (bufp->can_be_null)
  1467.         return;
  1468.       /* Don't return; check the alternative paths
  1469.          so we can set can_be_null if appropriate.    */
  1470.       break;
  1471.  
  1472.     case wordchar:
  1473.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1474.         if (SYNTAX (j) == Sword)
  1475.           fastmap[j] = 1;
  1476.       break;
  1477.  
  1478.     case notwordchar:
  1479.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1480.         if (SYNTAX (j) != Sword)
  1481.           fastmap[j] = 1;
  1482.       break;
  1483.  
  1484. #ifdef emacs
  1485.     case syntaxspec:
  1486.       k = *p++;
  1487.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1488.         if (SYNTAX (j) == (enum syntaxcode) k)
  1489.           fastmap[j] = 1;
  1490.       break;
  1491.  
  1492.     case notsyntaxspec:
  1493.       k = *p++;
  1494.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1495.         if (SYNTAX (j) != (enum syntaxcode) k)
  1496.           fastmap[j] = 1;
  1497.       break;
  1498. #endif /* not emacs */
  1499.  
  1500.     case charset:
  1501.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1502.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  1503.           {
  1504.         if (translate)
  1505.           fastmap[translate[j]] = 1;
  1506.         else
  1507.           fastmap[j] = 1;
  1508.           }
  1509.       break;
  1510.  
  1511.     case charset_not:
  1512.       /* Chars beyond end of map must be allowed */
  1513.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  1514.         if (translate)
  1515.           fastmap[translate[j]] = 1;
  1516.         else
  1517.           fastmap[j] = 1;
  1518.  
  1519.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1520.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  1521.           {
  1522.         if (translate)
  1523.           fastmap[translate[j]] = 1;
  1524.         else
  1525.           fastmap[j] = 1;
  1526.           }
  1527.       break;
  1528.     }
  1529.  
  1530.       /* Get here means we have successfully found the possible starting
  1531.      characters of one path of the pattern.  We need not follow this
  1532.      path any farther.  Instead, look at the next alternative
  1533.      remembered in the stack.  */
  1534.    if (stackp != stackb)
  1535.     p = *stackp--;
  1536.       else
  1537.     break;
  1538.     }
  1539. }
  1540.  
  1541.  
  1542.  
  1543. /* Like re_search_2, below, but only one string is specified, and
  1544.    doesn't let you say where to stop matching. */
  1545.  
  1546. int
  1547. re_search (pbufp, string, size, startpos, range, regs)
  1548.      struct re_pattern_buffer *pbufp;
  1549.      char *string;
  1550.      int size, startpos, range;
  1551.      struct re_registers *regs;
  1552. {
  1553.   return re_search_2 (pbufp, (char *) 0, 0, string, size, startpos, range,
  1554.               regs, size);
  1555. }
  1556.  
  1557.  
  1558. /* Using the compiled pattern in PBUFP->buffer, first tries to match the
  1559.    virtual concatenation of STRING1 and STRING2, starting first at index
  1560.    STARTPOS, then at STARTPOS + 1, and so on.  RANGE is the number of
  1561.    places to try before giving up.  If RANGE is negative, it searches
  1562.    backwards, i.e., the starting positions tried are STARTPOS, STARTPOS
  1563.    - 1, etc.  STRING1 and STRING2 are of SIZE1 and SIZE2, respectively.
  1564.    In REGS, return the indices of the virtual concatenation of STRING1
  1565.    and STRING2 that matched the entire PBUFP->buffer and its contained
  1566.    subexpressions.  Do not consider matching one past the index MSTOP in
  1567.    the virtual concatenation of STRING1 and STRING2.
  1568.  
  1569.    The value returned is the position in the strings at which the match
  1570.    was found, or -1 if no match was found, or -2 if error (such as
  1571.    failure stack overflow).  */
  1572.  
  1573. int
  1574. re_search_2 (pbufp, string1, size1, string2, size2, startpos, range,
  1575.          regs, mstop)
  1576.      struct re_pattern_buffer *pbufp;
  1577.      char *string1, *string2;
  1578.      int size1, size2;
  1579.      int startpos;
  1580.      register int range;
  1581.      struct re_registers *regs;
  1582.      int mstop;
  1583. {
  1584.   register char *fastmap = pbufp->fastmap;
  1585.   register unsigned char *translate = (unsigned char *) pbufp->translate;
  1586.   int total_size = size1 + size2;
  1587.   int endpos = startpos + range;
  1588.   int val;
  1589.  
  1590.   /* Check for out-of-range starting position.    */
  1591.   if (startpos < 0  ||  startpos > total_size)
  1592.     return -1;
  1593.  
  1594.   /* Fix up range if it would eventually take startpos outside of the
  1595.      virtual concatenation of string1 and string2.  */
  1596.   if (endpos < -1)
  1597.     range = -1 - startpos;
  1598.   else if (endpos > total_size)
  1599.     range = total_size - startpos;
  1600.  
  1601.   /* Update the fastmap now if not correct already.  */
  1602.   if (fastmap && !pbufp->fastmap_accurate)
  1603.     re_compile_fastmap (pbufp);
  1604.  
  1605.   /* If the search isn't to be a backwards one, don't waste time in a
  1606.      long search for a pattern that says it is anchored.  */
  1607.   if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  1608.       && range > 0)
  1609.     {
  1610.       if (startpos > 0)
  1611.     return -1;
  1612.       else
  1613.     range = 1;
  1614.     }
  1615.  
  1616.   while (1)
  1617.     {
  1618.       /* If a fastmap is supplied, skip quickly over characters that
  1619.      cannot possibly be the start of a match.  Note, however, that
  1620.      if the pattern can possibly match the null string, we must
  1621.      test it at each starting point so that we take the first null
  1622.      string we get.  */
  1623.  
  1624.       if (fastmap && startpos < total_size && pbufp->can_be_null != 1)
  1625.     {
  1626.       if (range > 0)        /* Searching forwards.  */
  1627.         {
  1628.           register int lim = 0;
  1629.           register unsigned char *p;
  1630.           int irange = range;
  1631.           if (startpos < size1 && startpos + range >= size1)
  1632.         lim = range - (size1 - startpos);
  1633.  
  1634.           p = ((unsigned char *)
  1635.            &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1636.  
  1637.           while (range > lim && !fastmap[translate
  1638.                          ? translate[*p++]
  1639.                          : *p++])
  1640.             range--;
  1641.           startpos += irange - range;
  1642.         }
  1643.       else                /* Searching backwards.  */
  1644.         {
  1645.           register unsigned char c;
  1646.  
  1647.           if (string1 == 0 || startpos >= size1)
  1648.         c = string2[startpos - size1];
  1649.           else
  1650.         c = string1[startpos];
  1651.  
  1652.           c &= 0xff;
  1653.           if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1654.         goto advance;
  1655.         }
  1656.     }
  1657.  
  1658.       if (range >= 0 && startpos == total_size
  1659.       && fastmap && pbufp->can_be_null == 0)
  1660.     return -1;
  1661.  
  1662.       val = re_match_2 (pbufp, string1, size1, string2, size2, startpos,
  1663.             regs, mstop);
  1664.       if (val >= 0)
  1665.     return startpos;
  1666.       if (val == -2)
  1667.     return -2;
  1668.  
  1669. #ifdef C_ALLOCA
  1670.       alloca (0);
  1671. #endif /* C_ALLOCA */
  1672.  
  1673.     advance:
  1674.       if (!range)
  1675.     break;
  1676.       else if (range > 0)
  1677.     {
  1678.       range--;
  1679.       startpos++;
  1680.     }
  1681.       else
  1682.     {
  1683.       range++;
  1684.       startpos--;
  1685.     }
  1686.     }
  1687.   return -1;
  1688. }
  1689.  
  1690.  
  1691.  
  1692. #ifndef emacs    /* emacs never uses this.  */
  1693. int
  1694. re_match (pbufp, string, size, pos, regs)
  1695.      struct re_pattern_buffer *pbufp;
  1696.      char *string;
  1697.      int size, pos;
  1698.      struct re_registers *regs;
  1699. {
  1700.   return re_match_2 (pbufp, (char *) 0, 0, string, size, pos, regs, size);
  1701. }
  1702. #endif /* not emacs */
  1703.  
  1704.  
  1705. /* The following are used for re_match_2, defined below:  */
  1706.  
  1707. /* Roughly the maximum number of failure points on the stack.  Would be
  1708.    exactly that if always pushed MAX_NUM_FAILURE_ITEMS each time we failed.  */
  1709.  
  1710. int re_max_failures = 2000;
  1711.  
  1712. /* Routine used by re_match_2.    */
  1713. static int bcmp_translate ();
  1714.  
  1715.  
  1716. /* Structure and accessing macros used in re_match_2:  */
  1717.  
  1718. struct register_info
  1719. {
  1720.   unsigned is_active : 1;
  1721.   unsigned matched_something : 1;
  1722. };
  1723.  
  1724. #define IS_ACTIVE(R)  ((R).is_active)
  1725. #define MATCHED_SOMETHING(R)  ((R).matched_something)
  1726.  
  1727.  
  1728. /* Macros used by re_match_2:  */
  1729.  
  1730.  
  1731. /* I.e., regstart, regend, and reg_info.  */
  1732.  
  1733. #define NUM_REG_ITEMS  3
  1734.  
  1735. /* We push at most this many things on the stack whenever we
  1736.    fail.  The `+ 2' refers to PATTERN_PLACE and STRING_PLACE, which are
  1737.    arguments to the PUSH_FAILURE_POINT macro.  */
  1738.  
  1739. #define MAX_NUM_FAILURE_ITEMS    (RE_NREGS * NUM_REG_ITEMS + 2)
  1740.  
  1741.  
  1742. /* We push this many things on the stack whenever we fail.  */
  1743.  
  1744. #define NUM_FAILURE_ITEMS  (last_used_reg * NUM_REG_ITEMS + 2)
  1745.  
  1746.  
  1747. /* This pushes most of the information about the current state we will want
  1748.    if we ever fail back to it.    */
  1749.  
  1750. #define PUSH_FAILURE_POINT(pattern_place, string_place)                 \
  1751.   {                                    \
  1752.     short last_used_reg, this_reg;                    \
  1753.                                     \
  1754.     /* Find out how many registers are active or have been matched.    \
  1755.        (Aside from register zero, which is only set at the end.)  */    \
  1756.     for (last_used_reg = RE_NREGS - 1; last_used_reg > 0; last_used_reg--)\
  1757.       if (regstart[last_used_reg] != (unsigned char *) -1)              \
  1758.     break;                                \
  1759.                                     \
  1760.     if (stacke - stackp < NUM_FAILURE_ITEMS)                            \
  1761.       {                                 \
  1762.     unsigned char **stackx;                     \
  1763.     if (stacke - stackb > re_max_failures * MAX_NUM_FAILURE_ITEMS)  \
  1764.       return -2;                            \
  1765.                                     \
  1766.     /* Roughly double the size of the stack.  */            \
  1767.     stackx = (unsigned char **) alloca (2 * MAX_NUM_FAILURE_ITEMS   \
  1768.                         * (stacke - stackb)         \
  1769.                         * sizeof (unsigned char *));\
  1770.     /* Only copy what is in use.  */                \
  1771.     bcopy (stackb, stackx, (stackp - stackb) * sizeof (char *));    \
  1772.     stackp = stackx + (stackp - stackb);                            \
  1773.     stackb = stackx;                        \
  1774.     stacke = stackb + 2 * MAX_NUM_FAILURE_ITEMS * (stacke - stackb);\
  1775.       }                                 \
  1776.                                     \
  1777.     /* Now push the info for each of those registers.  */        \
  1778.     for (this_reg = 1; this_reg <= last_used_reg; this_reg++)           \
  1779.       {                                 \
  1780.     *stackp++ = regstart[this_reg];                 \
  1781.     *stackp++ = regend[this_reg];                    \
  1782.     *stackp++ = (unsigned char *) ®_info[this_reg];              \
  1783.       }                                 \
  1784.                                     \
  1785.     /* Push how many registers we saved.  */                \
  1786.     *stackp++ = (unsigned char *) last_used_reg;                        \
  1787.                                     \
  1788.     *stackp++ = pattern_place;                        \
  1789.     *stackp++ = string_place;                        \
  1790.   }
  1791.  
  1792.  
  1793. /* This pops what PUSH_FAILURE_POINT pushes.  */
  1794.  
  1795. #define POP_FAILURE_POINT()                                             \
  1796.   {                                    \
  1797.     int temp;                                \
  1798.     stackp -= 2;        /* Remove failure points.  */        \
  1799.     temp = (int) *--stackp;     /* How many regs pushed.  */            \
  1800.     temp *= NUM_REG_ITEMS;    /* How much to take off the stack.  */    \
  1801.     stackp -= temp;        /* Remove the register info.  */    \
  1802.   }
  1803.  
  1804.  
  1805. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  1806.  
  1807. /* Is true if there is a first string and if PTR is pointing anywhere
  1808.    inside it or just past the end.  */
  1809.  
  1810. #define IS_IN_FIRST_STRING(ptr)                                         \
  1811.     (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  1812.  
  1813. /* Call before fetching a character with *d.  This switches over to
  1814.    string2 if necessary.  */
  1815.  
  1816. #define PREFETCH                            \
  1817.  while (d == dend)                                                      \
  1818.   {                                    \
  1819.     /* end of string2 => fail.    */                    \
  1820.     if (dend == end_match_2)                                            \
  1821.       goto fail;                            \
  1822.     /* end of string1 => advance to string2.  */            \
  1823.     d = string2;                            \
  1824.     dend = end_match_2;                         \
  1825.   }
  1826.  
  1827.  
  1828. /* Call this when have matched something; it sets `matched' flags for the
  1829.    registers corresponding to the subexpressions of which we currently
  1830.    are inside.    */
  1831. #define SET_REGS_MATCHED                        \
  1832.   { unsigned this_reg;                            \
  1833.     for (this_reg = 0; this_reg < RE_NREGS; this_reg++)                 \
  1834.       {                                 \
  1835.     if (IS_ACTIVE(reg_info[this_reg]))                              \
  1836.       MATCHED_SOMETHING(reg_info[this_reg]) = 1;                    \
  1837.     else                                \
  1838.       MATCHED_SOMETHING(reg_info[this_reg]) = 0;                    \
  1839.       }                                 \
  1840.   }
  1841.  
  1842. /* Test if at very beginning or at very end of the virtual concatenation
  1843.    of string1 and string2.  If there is only one string, we've put it in
  1844.    string2.  */
  1845.  
  1846. #define AT_STRINGS_BEG    (d == (size1 ? string1 : string2)  ||  !size2)
  1847. #define AT_STRINGS_END    (d == end2)
  1848.  
  1849. #define AT_WORD_BOUNDARY                        \
  1850.   (AT_STRINGS_BEG || AT_STRINGS_END || IS_A_LETTER (d - 1) != IS_A_LETTER (d))
  1851.  
  1852. /* We have two special cases to check for:
  1853.      1) if we're past the end of string1, we have to look at the first
  1854.     character in string2;
  1855.      2) if we're before the beginning of string2, we have to look at the
  1856.     last character in string1; we assume there is a string1, so use
  1857.     this in conjunction with AT_STRINGS_BEG.  */
  1858. #define IS_A_LETTER(d)                                                  \
  1859.   (SYNTAX ((d) == end1 ? *string2 : (d) == string2 - 1 ? *(end1 - 1) : *(d))\
  1860.    == Sword)
  1861.  
  1862.  
  1863. /* Match the pattern described by PBUFP against the virtual
  1864.    concatenation of STRING1 and STRING2, which are of SIZE1 and SIZE2,
  1865.    respectively.  Start the match at index POS in the virtual
  1866.    concatenation of STRING1 and STRING2.  In REGS, return the indices of
  1867.    the virtual concatenation of STRING1 and STRING2 that matched the
  1868.    entire PBUFP->buffer and its contained subexpressions.  Do not
  1869.    consider matching one past the index MSTOP in the virtual
  1870.    concatenation of STRING1 and STRING2.
  1871.  
  1872.    If pbufp->fastmap is nonzero, then it had better be up to date.
  1873.  
  1874.    The reason that the data to match are specified as two components
  1875.    which are to be regarded as concatenated is so this function can be
  1876.    used directly on the contents of an Emacs buffer.
  1877.  
  1878.    -1 is returned if there is no match.  -2 is returned if there is an
  1879.    error (such as match stack overflow).  Otherwise the value is the
  1880.    length of the substring which was matched.  */
  1881.  
  1882. int
  1883. re_match_2 (pbufp, string1_arg, size1, string2_arg, size2, pos, regs, mstop)
  1884.      struct re_pattern_buffer *pbufp;
  1885.      char *string1_arg, *string2_arg;
  1886.      int size1, size2;
  1887.      int pos;
  1888.      struct re_registers *regs;
  1889.      int mstop;
  1890. {
  1891.   register unsigned char *p = (unsigned char *) pbufp->buffer;
  1892.  
  1893.   /* Pointer to beyond end of buffer.  */
  1894.   register unsigned char *pend = p + pbufp->used;
  1895.  
  1896.   unsigned char *string1 = (unsigned char *) string1_arg;
  1897.   unsigned char *string2 = (unsigned char *) string2_arg;
  1898.   unsigned char *end1;        /* Just past end of first string.  */
  1899.   unsigned char *end2;        /* Just past end of second string.  */
  1900.  
  1901.   /* Pointers into string1 and string2, just past the last characters in
  1902.      each to consider matching.  */
  1903.   unsigned char *end_match_1, *end_match_2;
  1904.  
  1905.   register unsigned char *d, *dend;
  1906.   register int mcnt;            /* Multipurpose.  */
  1907.   unsigned char *translate = (unsigned char *) pbufp->translate;
  1908.   unsigned is_a_jump_n = 0;
  1909.  
  1910.  /* Failure point stack.  Each place that can handle a failure further
  1911.     down the line pushes a failure point on this stack.  It consists of
  1912.     restart, regend, and reg_info for all registers corresponding to the
  1913.     subexpressions we're currently inside, plus the number of such
  1914.     registers, and, finally, two char *'s.  The first char * is where to
  1915.     resume scanning the pattern; the second one is where to resume
  1916.     scanning the strings.  If the latter is zero, the failure point is a
  1917.     ``dummy''; if a failure happens and the failure point is a dummy, it
  1918.     gets discarded and the next next one is tried.  */
  1919.  
  1920.   unsigned char *initial_stack[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1921.   unsigned char **stackb = initial_stack;
  1922.   unsigned char **stackp = stackb;
  1923.   unsigned char **stacke = &stackb[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1924.  
  1925.  
  1926.   /* Information on the contents of registers. These are pointers into
  1927.      the input strings; they record just what was matched (on this
  1928.      attempt) by a subexpression part of the pattern, that is, the
  1929.      regnum-th regstart pointer points to where in the pattern we began
  1930.      matching and the regnum-th regend points to right after where we
  1931.      stopped matching the regnum-th subexpression.  (The zeroth register
  1932.      keeps track of what the whole pattern matches.)  */
  1933.  
  1934.   unsigned char *regstart[RE_NREGS];
  1935.   unsigned char *regend[RE_NREGS];
  1936.  
  1937.   /* The is_active field of reg_info helps us keep track of which (possibly
  1938.      nested) subexpressions we are currently in. The matched_something
  1939.      field of reg_info[reg_num] helps us tell whether or not we have
  1940.      matched any of the pattern so far this time through the reg_num-th
  1941.      subexpression.  These two fields get reset each time through any
  1942.      loop their register is in.  */
  1943.  
  1944.   struct register_info reg_info[RE_NREGS];
  1945.  
  1946.  
  1947.   /* The following record the register info as found in the above
  1948.      variables when we find a match better than any we've seen before.
  1949.      This happens as we backtrack through the failure points, which in
  1950.      turn happens only if we have not yet matched the entire string.  */
  1951.  
  1952.   unsigned best_regs_set = 0;
  1953.   unsigned char *best_regstart[RE_NREGS];
  1954.   unsigned char *best_regend[RE_NREGS];
  1955.  
  1956.  
  1957.   /* Initialize subexpression text positions to -1 to mark ones that no
  1958.      \( or ( and \) or ) has been seen for. Also set all registers to
  1959.      inactive and mark them as not having matched anything or ever
  1960.      failed.  */
  1961.   for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1962.     {
  1963.       regstart[mcnt] = regend[mcnt] = (unsigned char *) -1;
  1964.       IS_ACTIVE (reg_info[mcnt]) = 0;
  1965.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  1966.     }
  1967.  
  1968.   if (regs)
  1969.     for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1970.       regs->start[mcnt] = regs->end[mcnt] = -1;
  1971.  
  1972.   /* Set up pointers to ends of strings.
  1973.      Don't allow the second string to be empty unless both are empty.  */
  1974.   if (size2 == 0)
  1975.     {
  1976.       string2 = string1;
  1977.       size2 = size1;
  1978.       string1 = 0;
  1979.       size1 = 0;
  1980.     }
  1981.   end1 = string1 + size1;
  1982.   end2 = string2 + size2;
  1983.  
  1984.   /* Compute where to stop matching, within the two strings.  */
  1985.   if (mstop <= size1)
  1986.     {
  1987.       end_match_1 = string1 + mstop;
  1988.       end_match_2 = string2;
  1989.     }
  1990.   else
  1991.     {
  1992.       end_match_1 = end1;
  1993.       end_match_2 = string2 + mstop - size1;
  1994.     }
  1995.  
  1996.   /* `p' scans through the pattern as `d' scans through the data. `dend'
  1997.      is the end of the input string that `d' points within. `d' is
  1998.      advanced into the following input string whenever necessary, but
  1999.      this happens before fetching; therefore, at the beginning of the
  2000.      loop, `d' can be pointing at the end of a string, but it cannot
  2001.      equal string2.  */
  2002.  
  2003.   if (size1 != 0 && pos <= size1)
  2004.     d = string1 + pos, dend = end_match_1;
  2005.   else
  2006.     d = string2 + pos - size1, dend = end_match_2;
  2007.  
  2008.  
  2009.   /* This loops over pattern commands.    It exits by returning from the
  2010.      function if match is complete, or it drops through if match fails
  2011.      at this starting point in the input data.    */
  2012.  
  2013.   while (1)
  2014.     {
  2015.       is_a_jump_n = 0;
  2016.       /* End of pattern means we might have succeeded.    */
  2017.       if (p == pend)
  2018.     {
  2019.       /* If not end of string, try backtracking.  Otherwise done.  */
  2020.       if (d != end_match_2)
  2021.         {
  2022.           if (stackp != stackb)
  2023.         {
  2024.           /* More failure points to try.  */
  2025.  
  2026.           unsigned in_same_string =
  2027.                 IS_IN_FIRST_STRING (best_regend[0])
  2028.                 == MATCHING_IN_FIRST_STRING;
  2029.  
  2030.           /* If exceeds best match so far, save it.  */
  2031.           if (! best_regs_set
  2032.               || (in_same_string && d > best_regend[0])
  2033.               || (! in_same_string && ! MATCHING_IN_FIRST_STRING))
  2034.             {
  2035.               best_regs_set = 1;
  2036.               best_regend[0] = d;    /* Never use regstart[0].  */
  2037.  
  2038.               for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  2039.             {
  2040.               best_regstart[mcnt] = regstart[mcnt];
  2041.               best_regend[mcnt] = regend[mcnt];
  2042.             }
  2043.             }
  2044.           goto fail;
  2045.         }
  2046.           /* If no failure points, don't restore garbage.  */
  2047.           else if (best_regs_set)
  2048.         {
  2049.           restore_best_regs:
  2050.           /* Restore best match.  */
  2051.           d = best_regend[0];
  2052.  
  2053.           for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  2054.             {
  2055.               regstart[mcnt] = best_regstart[mcnt];
  2056.               regend[mcnt] = best_regend[mcnt];
  2057.             }
  2058.         }
  2059.         }
  2060.  
  2061.       /* If caller wants register contents data back, convert it
  2062.          to indices.  */
  2063.       if (regs)
  2064.         {
  2065.           regs->start[0] = pos;
  2066.           if (MATCHING_IN_FIRST_STRING)
  2067.         regs->end[0] = d - string1;
  2068.           else
  2069.         regs->end[0] = d - string2 + size1;
  2070.           for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  2071.         {
  2072.           if (regend[mcnt] == (unsigned char *) -1)
  2073.             {
  2074.               regs->start[mcnt] = -1;
  2075.               regs->end[mcnt] = -1;
  2076.               continue;
  2077.             }
  2078.           if (IS_IN_FIRST_STRING (regstart[mcnt]))
  2079.             regs->start[mcnt] = regstart[mcnt] - string1;
  2080.           else
  2081.             regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  2082.  
  2083.           if (IS_IN_FIRST_STRING (regend[mcnt]))
  2084.             regs->end[mcnt] = regend[mcnt] - string1;
  2085.           else
  2086.             regs->end[mcnt] = regend[mcnt] - string2 + size1;
  2087.         }
  2088.         }
  2089.       return d - pos - (MATCHING_IN_FIRST_STRING
  2090.                 ? string1
  2091.                 : string2 - size1);
  2092.     }
  2093.  
  2094.       /* Otherwise match next pattern command.    */
  2095. #ifdef SWITCH_ENUM_BUG
  2096.       switch ((int) ((enum regexpcode) *p++))
  2097. #else
  2098.       switch ((enum regexpcode) *p++)
  2099. #endif
  2100.     {
  2101.  
  2102.     /* \( [or `(', as appropriate] is represented by start_memory,
  2103.        \) by stop_memory.  Both of those commands are followed by
  2104.        a register number in the next byte.    The text matched
  2105.        within the \( and \) is recorded under that number.  */
  2106.     case start_memory:
  2107.       regstart[*p] = d;
  2108.       IS_ACTIVE (reg_info[*p]) = 1;
  2109.       MATCHED_SOMETHING (reg_info[*p]) = 0;
  2110.       p++;
  2111.       break;
  2112.  
  2113.     case stop_memory:
  2114.       regend[*p] = d;
  2115.       IS_ACTIVE (reg_info[*p]) = 0;
  2116.  
  2117.       /* If just failed to match something this time around with a sub-
  2118.          expression that's in a loop, try to force exit from the loop.  */
  2119.       if ((! MATCHED_SOMETHING (reg_info[*p])
  2120.            || (enum regexpcode) p[-3] == start_memory)
  2121.           && (p + 1) != pend)
  2122.         {
  2123.           register unsigned char *p2 = p + 1;
  2124.           mcnt = 0;
  2125.           switch (*p2++)
  2126.         {
  2127.           case jump_n:
  2128.             is_a_jump_n = 1;
  2129.           case finalize_jump:
  2130.           case maybe_finalize_jump:
  2131.           case jump:
  2132.           case dummy_failure_jump:
  2133.             EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2134.             if (is_a_jump_n)
  2135.               p2 += 2;
  2136.             break;
  2137.         }
  2138.           p2 += mcnt;
  2139.  
  2140.           /* If the next operation is a jump backwards in the pattern
  2141.          to an on_failure_jump, exit from the loop by forcing a
  2142.          failure after pushing on the stack the on_failure_jump's
  2143.          jump in the pattern, and d.  */
  2144.           if (mcnt < 0 && (enum regexpcode) *p2++ == on_failure_jump)
  2145.         {
  2146.           EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2147.           PUSH_FAILURE_POINT (p2 + mcnt, d);
  2148.           goto fail;
  2149.         }
  2150.         }
  2151.       p++;
  2152.       break;
  2153.  
  2154.     /* \<digit> has been turned into a `duplicate' command which is
  2155.        followed by the numeric value of <digit> as the register number.  */
  2156.     case duplicate:
  2157.       {
  2158.         int regno = *p++;    /* Get which register to match against */
  2159.         register unsigned char *d2, *dend2;
  2160.  
  2161.         /* Where in input to try to start matching.  */
  2162.         d2 = regstart[regno];
  2163.  
  2164.         /* Where to stop matching; if both the place to start and
  2165.            the place to stop matching are in the same string, then
  2166.            set to the place to stop, otherwise, for now have to use
  2167.            the end of the first string.  */
  2168.  
  2169.         dend2 = ((IS_IN_FIRST_STRING (regstart[regno])
  2170.               == IS_IN_FIRST_STRING (regend[regno]))
  2171.              ? regend[regno] : end_match_1);
  2172.         while (1)
  2173.           {
  2174.         /* If necessary, advance to next segment in register
  2175.            contents.  */
  2176.         while (d2 == dend2)
  2177.           {
  2178.             if (dend2 == end_match_2) break;
  2179.             if (dend2 == regend[regno]) break;
  2180.             d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  2181.           }
  2182.         /* At end of register contents => success */
  2183.         if (d2 == dend2) break;
  2184.  
  2185.         /* If necessary, advance to next segment in data.  */
  2186.         PREFETCH;
  2187.  
  2188.         /* How many characters left in this segment to match.  */
  2189.         mcnt = dend - d;
  2190.  
  2191.         /* Want how many consecutive characters we can match in
  2192.            one shot, so, if necessary, adjust the count.  */
  2193.         if (mcnt > dend2 - d2)
  2194.           mcnt = dend2 - d2;
  2195.  
  2196.         /* Compare that many; failure if mismatch, else move
  2197.            past them.  */
  2198.         if (translate
  2199.             ? bcmp_translate (d, d2, mcnt, translate)
  2200.             : bcmp (d, d2, mcnt))
  2201.           goto fail;
  2202.         d += mcnt, d2 += mcnt;
  2203.           }
  2204.       }
  2205.       break;
  2206.  
  2207.     case anychar:
  2208.       PREFETCH;      /* Fetch a data character. */
  2209.       /* Match anything but a newline, maybe even a null.  */
  2210.       if ((translate ? translate[*d] : *d) == '\n'
  2211.           || ((obscure_syntax & RE_DOT_NOT_NULL)
  2212.           && (translate ? translate[*d] : *d) == '\000'))
  2213.         goto fail;
  2214.       SET_REGS_MATCHED;
  2215.       d++;
  2216.       break;
  2217.  
  2218.     case charset:
  2219.     case charset_not:
  2220.       {
  2221.         int not = 0;        /* Nonzero for charset_not.  */
  2222.         register int c;
  2223.         if (*(p - 1) == (unsigned char) charset_not)
  2224.           not = 1;
  2225.  
  2226.         PREFETCH;        /* Fetch a data character. */
  2227.  
  2228.         if (translate)
  2229.           c = translate[*d];
  2230.         else
  2231.           c = *d;
  2232.  
  2233.         if (c < *p * BYTEWIDTH
  2234.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2235.           not = !not;
  2236.  
  2237.         p += 1 + *p;
  2238.  
  2239.         if (!not) goto fail;
  2240.         SET_REGS_MATCHED;
  2241.         d++;
  2242.         break;
  2243.       }
  2244.  
  2245.     case begline:
  2246.       if ((size1 != 0 && d == string1)
  2247.           || (size1 == 0 && size2 != 0 && d == string2)
  2248.           || (d && d[-1] == '\n')
  2249.           || (size1 == 0 && size2 == 0))
  2250.         break;
  2251.       else
  2252.         goto fail;
  2253.  
  2254.     case endline:
  2255.       if (d == end2
  2256.           || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  2257.         break;
  2258.       goto fail;
  2259.  
  2260.     /* `or' constructs are handled by starting each alternative with
  2261.        an on_failure_jump that points to the start of the next
  2262.        alternative.  Each alternative except the last ends with a
  2263.        jump to the joining point.  (Actually, each jump except for
  2264.        the last one really jumps to the following jump, because
  2265.        tensioning the jumps is a hassle.)  */
  2266.  
  2267.     /* The start of a stupid repeat has an on_failure_jump that points
  2268.        past the end of the repeat text. This makes a failure point so
  2269.        that on failure to match a repetition, matching restarts past
  2270.        as many repetitions have been found with no way to fail and
  2271.        look for another one.  */
  2272.  
  2273.     /* A smart repeat is similar but loops back to the on_failure_jump
  2274.        so that each repetition makes another failure point.  */
  2275.  
  2276.     case on_failure_jump:
  2277.     on_failure:
  2278.       EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2279.       PUSH_FAILURE_POINT (p + mcnt, d);
  2280.       break;
  2281.  
  2282.     /* The end of a smart repeat has a maybe_finalize_jump back.
  2283.        Change it either to a finalize_jump or an ordinary jump.  */
  2284.     case maybe_finalize_jump:
  2285.       EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2286.       {
  2287.         register unsigned char *p2 = p;
  2288.         /* Compare what follows with the beginning of the repeat.
  2289.            If we can establish that there is nothing that they would
  2290.            both match, we can change to finalize_jump.  */
  2291.         while (p2 + 1 != pend
  2292.            && (*p2 == (unsigned char) stop_memory
  2293.                || *p2 == (unsigned char) start_memory))
  2294.           p2 += 2;                /* Skip over reg number.  */
  2295.         if (p2 == pend)
  2296.           p[-3] = (unsigned char) finalize_jump;
  2297.         else if (*p2 == (unsigned char) exactn
  2298.              || *p2 == (unsigned char) endline)
  2299.           {
  2300.         register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  2301.         register unsigned char *p1 = p + mcnt;
  2302.         /* p1[0] ... p1[2] are an on_failure_jump.
  2303.            Examine what follows that.  */
  2304.         if (p1[3] == (unsigned char) exactn && p1[5] != c)
  2305.           p[-3] = (unsigned char) finalize_jump;
  2306.         else if (p1[3] == (unsigned char) charset
  2307.              || p1[3] == (unsigned char) charset_not)
  2308.           {
  2309.             int not = p1[3] == (unsigned char) charset_not;
  2310.             if (c < p1[4] * BYTEWIDTH
  2311.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2312.               not = !not;
  2313.             /* `not' is 1 if c would match.  */
  2314.             /* That means it is not safe to finalize.  */
  2315.             if (!not)
  2316.               p[-3] = (unsigned char) finalize_jump;
  2317.           }
  2318.           }
  2319.       }
  2320.       p -= 2;        /* Point at relative address again.  */
  2321.       if (p[-1] != (unsigned char) finalize_jump)
  2322.         {
  2323.           p[-1] = (unsigned char) jump;
  2324.           goto nofinalize;
  2325.         }
  2326.     /* Note fall through.  */
  2327.  
  2328.     /* The end of a stupid repeat has a finalize_jump back to the
  2329.        start, where another failure point will be made which will
  2330.        point to after all the repetitions found so far.  */
  2331.  
  2332.     /* Take off failure points put on by matching on_failure_jump
  2333.        because didn't fail.  Also remove the register information
  2334.        put on by the on_failure_jump.  */
  2335.     case finalize_jump:
  2336.       POP_FAILURE_POINT ();
  2337.     /* Note fall through.  */
  2338.  
  2339.     /* Jump without taking off any failure points.    */
  2340.     case jump:
  2341.     nofinalize:
  2342.       EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2343.       p += mcnt;
  2344.       break;
  2345.  
  2346.     case dummy_failure_jump:
  2347.       /* Normally, the on_failure_jump pushes a failure point, which
  2348.          then gets popped at finalize_jump.  We will end up at
  2349.          finalize_jump, also, and with a pattern of, say, `a+', we
  2350.          are skipping over the on_failure_jump, so we have to push
  2351.          something meaningless for finalize_jump to pop.  */
  2352.       PUSH_FAILURE_POINT (0, 0);
  2353.       goto nofinalize;
  2354.  
  2355.  
  2356.     /* Have to succeed matching what follows at least n times.  Then
  2357.       just handle like an on_failure_jump.    */
  2358.     case succeed_n:
  2359.       EXTRACT_NUMBER (mcnt, p + 2);
  2360.       /* Originally, this is how many times we HAVE to succeed.  */
  2361.       if (mcnt)
  2362.         {
  2363.            mcnt--;
  2364.            p += 2;
  2365.            STORE_NUMBER_AND_INCR (p, mcnt);
  2366.         }
  2367.       else if (mcnt == 0)
  2368.         {
  2369.           p[2] = unused;
  2370.           p[3] = unused;
  2371.           goto on_failure;
  2372.         }
  2373.       else
  2374.         {
  2375.           fprintf (stderr, "regex: the succeed_n's n is not set.\n");
  2376.           exit (1);
  2377.         }
  2378.       break;
  2379.  
  2380.     case jump_n:
  2381.       EXTRACT_NUMBER (mcnt, p + 2);
  2382.       /* Originally, this is how many times we CAN jump.  */
  2383.       if (mcnt)
  2384.         {
  2385.            mcnt--;
  2386.            STORE_NUMBER(p + 2, mcnt);
  2387.            goto nofinalize;      /* Do the jump without taking off
  2388.                     any failure points.  */
  2389.         }
  2390.       /* If don't have to jump any more, skip over the rest of command.  */
  2391.       else
  2392.         p += 4;
  2393.       break;
  2394.  
  2395.     case set_number_at:
  2396.       {
  2397.         register unsigned char *p1;
  2398.  
  2399.         EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2400.         p1 = p + mcnt;
  2401.         EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2402.         STORE_NUMBER (p1, mcnt);
  2403.         break;
  2404.       }
  2405.  
  2406.     /* Ignore these.  Used to ignore the n of succeed_n's which
  2407.        currently have n == 0.  */
  2408.     case unused:
  2409.       break;
  2410.  
  2411.     case wordbound:
  2412.       if (AT_WORD_BOUNDARY)
  2413.         break;
  2414.       goto fail;
  2415.  
  2416.     case notwordbound:
  2417.       if (AT_WORD_BOUNDARY)
  2418.         goto fail;
  2419.       break;
  2420.  
  2421.     case wordbeg:
  2422.       if (IS_A_LETTER (d) && (!IS_A_LETTER (d - 1) || AT_STRINGS_BEG))
  2423.         break;
  2424.       goto fail;
  2425.  
  2426.     case wordend:
  2427.       /* Have to check if AT_STRINGS_BEG before looking at d - 1.  */
  2428.       if (!AT_STRINGS_BEG && IS_A_LETTER (d - 1)
  2429.           && (!IS_A_LETTER (d) || AT_STRINGS_END))
  2430.         break;
  2431.       goto fail;
  2432.  
  2433. #ifdef emacs
  2434.     case before_dot:
  2435.       if (PTR_CHAR_POS (d) >= point)
  2436.         goto fail;
  2437.       break;
  2438.  
  2439.     case at_dot:
  2440.       if (PTR_CHAR_POS (d) != point)
  2441.         goto fail;
  2442.       break;
  2443.  
  2444.     case after_dot:
  2445.       if (PTR_CHAR_POS (d) <= point)
  2446.         goto fail;
  2447.       break;
  2448.  
  2449.     case wordchar:
  2450.       mcnt = (int) Sword;
  2451.       goto matchsyntax;
  2452.  
  2453.     case syntaxspec:
  2454.       mcnt = *p++;
  2455.     matchsyntax:
  2456.       PREFETCH;
  2457.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  2458.       SET_REGS_MATCHED;
  2459.       break;
  2460.  
  2461.     case notwordchar:
  2462.       mcnt = (int) Sword;
  2463.       goto matchnotsyntax;
  2464.  
  2465.     case notsyntaxspec:
  2466.       mcnt = *p++;
  2467.     matchnotsyntax:
  2468.       PREFETCH;
  2469.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  2470.       SET_REGS_MATCHED;
  2471.       break;
  2472.  
  2473. #else /* not emacs */
  2474.  
  2475.     case wordchar:
  2476.       PREFETCH;
  2477.       if (!IS_A_LETTER (d))
  2478.         goto fail;
  2479.       SET_REGS_MATCHED;
  2480.       break;
  2481.  
  2482.     case notwordchar:
  2483.       PREFETCH;
  2484.       if (IS_A_LETTER (d))
  2485.         goto fail;
  2486.       SET_REGS_MATCHED;
  2487.       break;
  2488.  
  2489. #endif /* not emacs */
  2490.  
  2491.     case begbuf:
  2492.       if (AT_STRINGS_BEG)
  2493.         break;
  2494.       goto fail;
  2495.  
  2496.     case endbuf:
  2497.       if (AT_STRINGS_END)
  2498.         break;
  2499.       goto fail;
  2500.  
  2501.     case exactn:
  2502.       /* Match the next few pattern characters exactly.
  2503.          mcnt is how many characters to match.  */
  2504.       mcnt = *p++;
  2505.       /* This is written out as an if-else so we don't waste time
  2506.          testing `translate' inside the loop.  */
  2507.       if (translate)
  2508.         {
  2509.           do
  2510.         {
  2511.           PREFETCH;
  2512.           if (translate[*d++] != *p++) goto fail;
  2513.         }
  2514.           while (--mcnt);
  2515.         }
  2516.       else
  2517.         {
  2518.           do
  2519.         {
  2520.           PREFETCH;
  2521.           if (*d++ != *p++) goto fail;
  2522.         }
  2523.           while (--mcnt);
  2524.         }
  2525.       SET_REGS_MATCHED;
  2526.       break;
  2527.     }
  2528.       continue;  /* Successfully executed one pattern command; keep going.  */
  2529.  
  2530.     /* Jump here if any matching operation fails. */
  2531.     fail:
  2532.       if (stackp != stackb)
  2533.     /* A restart point is known.  Restart there and pop it. */
  2534.     {
  2535.       short last_used_reg, this_reg;
  2536.  
  2537.       /* If this failure point is from a dummy_failure_point, just
  2538.          skip it.  */
  2539.       if (!stackp[-2])
  2540.         {
  2541.           POP_FAILURE_POINT ();
  2542.           goto fail;
  2543.         }
  2544.  
  2545.       d = *--stackp;
  2546.       p = *--stackp;
  2547.       if (d >= string1 && d <= end1)
  2548.         dend = end_match_1;
  2549.       /* Restore register info.  */
  2550.       last_used_reg = (short) *--stackp;
  2551.  
  2552.       /* Make the ones that weren't saved -1 or 0 again.  */
  2553.       for (this_reg = RE_NREGS - 1; this_reg > last_used_reg; this_reg--)
  2554.         {
  2555.           regend[this_reg] = (unsigned char *) -1;
  2556.           regstart[this_reg] = (unsigned char *) -1;
  2557.           IS_ACTIVE (reg_info[this_reg]) = 0;
  2558.           MATCHED_SOMETHING (reg_info[this_reg]) = 0;
  2559.         }
  2560.  
  2561.       /* And restore the rest from the stack.  */
  2562.       for ( ; this_reg > 0; this_reg--)
  2563.         {
  2564.           reg_info[this_reg] = *(struct register_info *) *--stackp;
  2565.           regend[this_reg] = *--stackp;
  2566.           regstart[this_reg] = *--stackp;
  2567.         }
  2568.     }
  2569.       else
  2570.     break;     /* Matching at this starting point really fails.  */
  2571.     }
  2572.  
  2573.   if (best_regs_set)
  2574.     goto restore_best_regs;
  2575.   return -1;                /* Failure to match.  */
  2576. }
  2577.  
  2578.  
  2579. static int
  2580. bcmp_translate (s1, s2, len, translate)
  2581.      unsigned char *s1, *s2;
  2582.      register int len;
  2583.      unsigned char *translate;
  2584. {
  2585.   register unsigned char *p1 = s1, *p2 = s2;
  2586.   while (len)
  2587.     {
  2588.       if (translate [*p1++] != translate [*p2++]) return 1;
  2589.       len--;
  2590.     }
  2591.   return 0;
  2592. }
  2593.  
  2594.  
  2595.  
  2596. /* Entry points compatible with 4.2 BSD regex library.    */
  2597.  
  2598. #ifndef emacs
  2599.  
  2600. static struct re_pattern_buffer re_comp_buf;
  2601.  
  2602. char *
  2603. re_comp (s)
  2604.      char *s;
  2605. {
  2606.   if (!s)
  2607.     {
  2608.       if (!re_comp_buf.buffer)
  2609.     return "No previous regular expression";
  2610.       return 0;
  2611.     }
  2612.  
  2613.   if (!re_comp_buf.buffer)
  2614.     {
  2615.       if (!(re_comp_buf.buffer = (char *) malloc (200)))
  2616.     return "Memory exhausted";
  2617.       re_comp_buf.allocated = 200;
  2618.       if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  2619.     return "Memory exhausted";
  2620.     }
  2621.   return re_compile_pattern (s, strlen (s), &re_comp_buf);
  2622. }
  2623.  
  2624. int
  2625. re_exec (s)
  2626.      char *s;
  2627. {
  2628.   int len = strlen (s);
  2629.   return 0 <= re_search (&re_comp_buf, s, len, 0, len,
  2630.              (struct re_registers *) 0);
  2631. }
  2632. #endif /* not emacs */
  2633.  
  2634.  
  2635.  
  2636. #ifdef test
  2637.  
  2638. #include <stdio.h>
  2639.  
  2640. /* Indexed by a character, gives the upper case equivalent of the
  2641.    character.  */
  2642.  
  2643. char upcase[0400] =
  2644.   { 000, 001, 002, 003, 004, 005, 006, 007,
  2645.     010, 011, 012, 013, 014, 015, 016, 017,
  2646.     020, 021, 022, 023, 024, 025, 026, 027,
  2647.     030, 031, 032, 033, 034, 035, 036, 037,
  2648.     040, 041, 042, 043, 044, 045, 046, 047,
  2649.     050, 051, 052, 053, 054, 055, 056, 057,
  2650.     060, 061, 062, 063, 064, 065, 066, 067,
  2651.     070, 071, 072, 073, 074, 075, 076, 077,
  2652.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2653.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2654.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2655.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  2656.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2657.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2658.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2659.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  2660.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  2661.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  2662.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  2663.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  2664.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  2665.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  2666.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  2667.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  2668.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  2669.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  2670.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  2671.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  2672.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  2673.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  2674.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  2675.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  2676.   };
  2677.  
  2678. #ifdef canned
  2679.  
  2680. #include "tests.h"
  2681.  
  2682. typedef enum { extended_test, basic_test } test_type;
  2683.  
  2684. /* Use this to run the tests we've thought of.  */
  2685.  
  2686. void
  2687. main ()
  2688. {
  2689.   test_type t = extended_test;
  2690.  
  2691.   if (t == basic_test)
  2692.     {
  2693.       printf ("Running basic tests:\n\n");
  2694.       test_posix_basic ();
  2695.     }
  2696.   else if (t == extended_test)
  2697.     {
  2698.       printf ("Running extended tests:\n\n");
  2699.       test_posix_extended ();
  2700.     }
  2701. }
  2702.  
  2703. #else /* not canned */
  2704.  
  2705. /* Use this to run interactive tests.  */
  2706.  
  2707. void
  2708. main (argc, argv)
  2709.      int argc;
  2710.      char **argv;
  2711. {
  2712.   char pat[80];
  2713.   struct re_pattern_buffer buf;
  2714.   int i;
  2715.   char c;
  2716.   char fastmap[(1 << BYTEWIDTH)];
  2717.  
  2718.   /* Allow a command argument to specify the style of syntax.  */
  2719.   if (argc > 1)
  2720.     obscure_syntax = atoi (argv[1]);
  2721.  
  2722.   buf.allocated = 40;
  2723.   buf.buffer = (char *) malloc (buf.allocated);
  2724.   buf.fastmap = fastmap;
  2725.   buf.translate = upcase;
  2726.  
  2727.   while (1)
  2728.     {
  2729.       gets (pat);
  2730.  
  2731.       if (*pat)
  2732.     {
  2733.       re_compile_pattern (pat, strlen(pat), &buf);
  2734.  
  2735.       for (i = 0; i < buf.used; i++)
  2736.         printchar (buf.buffer[i]);
  2737.  
  2738.       putchar ('\n');
  2739.  
  2740.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  2741.  
  2742.       re_compile_fastmap (&buf);
  2743.       printf ("Allowed by fastmap: ");
  2744.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  2745.         if (fastmap[i]) printchar (i);
  2746.       putchar ('\n');
  2747.     }
  2748.  
  2749.       gets (pat);       /* Now read the string to match against */
  2750.  
  2751.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  2752.       printf ("Match value %d.\n", i);
  2753.     }
  2754. }
  2755.  
  2756. #endif
  2757.  
  2758.  
  2759. #ifdef NOTDEF
  2760. print_buf (bufp)
  2761.      struct re_pattern_buffer *bufp;
  2762. {
  2763.   int i;
  2764.  
  2765.   printf ("buf is :\n----------------\n");
  2766.   for (i = 0; i < bufp->used; i++)
  2767.     printchar (bufp->buffer[i]);
  2768.  
  2769.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  2770.  
  2771.   printf ("Allowed by fastmap: ");
  2772.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  2773.     if (bufp->fastmap[i])
  2774.       printchar (i);
  2775.   printf ("\nAllowed by translate: ");
  2776.   if (bufp->translate)
  2777.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  2778.       if (bufp->translate[i])
  2779.     printchar (i);
  2780.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  2781.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  2782. }
  2783. #endif /* NOTDEF */
  2784.  
  2785. printchar (c)
  2786.      char c;
  2787. {
  2788.   if (c < 040 || c >= 0177)
  2789.     {
  2790.       putchar ('\\');
  2791.       putchar (((c >> 6) & 3) + '0');
  2792.       putchar (((c >> 3) & 7) + '0');
  2793.       putchar ((c & 7) + '0');
  2794.     }
  2795.   else
  2796.     putchar (c);
  2797. }
  2798.  
  2799. error (string)
  2800.      char *string;
  2801. {
  2802.   puts (string);
  2803.   exit (1);
  2804. }
  2805. #endif /* test */
  2806. @
  2807.  
  2808.  
  2809. 1.1
  2810. log
  2811. @Initial revision
  2812. @
  2813. text
  2814. @d22 1
  2815. a22 1
  2816.    
  2817. d39 4
  2818. d46 3
  2819. a48 3
  2820. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  2821. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  2822. #define bzero(s,n)    memset((s),0,(n))
  2823. d69 1
  2824. d76 1
  2825. a76 1
  2826. #ifndef Sword 
  2827. d137 1
  2828. a137 1
  2829.    
  2830. d149 1
  2831. a149 1
  2832.     on_failure_jump,     /* Followed by two bytes giving relative address of 
  2833. d151 1
  2834. a151 1
  2835.     finalize_jump,     /* Throw away latest failure point and then jump to 
  2836. d161 6
  2837. a166 6
  2838.     dummy_failure_jump,  /* Jump, and push a dummy failure point. This 
  2839.                 failure point will be thrown away if an attempt 
  2840.                             is made to use it for a failure. A + construct 
  2841.                             makes this before the first repeat.  Also
  2842.                             use it as an intermediary kind of jump when
  2843.                             compiling an or construct.  */
  2844. d169 2
  2845. a170 2
  2846.                     address following it is useless until then.  The
  2847.                     address is followed by two bytes containing n.  */
  2848. d173 1
  2849. a173 1
  2850.                     containing n.  */
  2851. d177 1
  2852. a177 1
  2853.     charset,     /* Matches any one char belonging to specified set.
  2854. d185 1
  2855. a185 1
  2856.                     that is not one of those specified.  */
  2857. d188 2
  2858. a189 2
  2859.                     byte containing the register number.  Register numbers
  2860.                     must be in the range 0 through RE_NREGS.  */
  2861. d191 7
  2862. a197 7
  2863.             and store it in a memory register.  Followed by
  2864.                     one byte containing the register number. Register
  2865.                     numbers must be in the range 0 through RE_NREGS.  */
  2866.     duplicate,   /* Match a duplicate of something remembered.
  2867.             Followed by one byte containing the index of the memory 
  2868.                     register.  */
  2869.     before_dot,     /* Succeeds if before point.  */
  2870. d200 3
  2871. a202 3
  2872.     begbuf,      /* Succeeds if at beginning of buffer.  */
  2873.     endbuf,      /* Succeeds if at end of buffer.  */
  2874.     wordchar,    /* Matches any word-constituent character.  */
  2875. d206 1
  2876. a206 1
  2877.     wordbound,   /* Succeeds if at a word boundary.  */
  2878. d210 1
  2879. a210 1
  2880.                     e.g., Sword.  */
  2881. d212 1
  2882. a212 1
  2883.                      that specified.  */
  2884. d215 1
  2885. a215 1
  2886.  
  2887. a229 1
  2888.  
  2889. d231 1
  2890. d233 2
  2891. a234 2
  2892. #define STORE_NUMBER(destination, number)                \
  2893.   { (destination)[0] = (number) & 0377;                    \
  2894. d236 1
  2895. a236 1
  2896.   
  2897. d240 2
  2898. a241 2
  2899. #define STORE_NUMBER_AND_INCR(destination, number)            \
  2900.   { STORE_NUMBER(destination, number);                    \
  2901. d247 2
  2902. a248 2
  2903. #define EXTRACT_NUMBER(destination, source)                \
  2904.   { (destination) = *(source) & 0377;                    \
  2905. d254 2
  2906. a255 2
  2907. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  2908.   { EXTRACT_NUMBER (destination, source);                \
  2909. d262 1
  2910. a262 1
  2911.    
  2912. d264 1
  2913. a264 1
  2914.    defined in regex.h.  */
  2915. d288 3
  2916. a290 3
  2917. #define PATFETCH(c)                            \
  2918.   {if (p == pend) goto end_of_pattern;                    \
  2919.   c = * (unsigned char *) p++;                        \
  2920. d295 2
  2921. a296 2
  2922. #define PATFETCH_RAW(c)                            \
  2923.  {if (p == pend) goto end_of_pattern;                    \
  2924. d305 4
  2925. a308 4
  2926. /* Make sure we have at least N more bytes of space in buffer.  */
  2927. #define GET_BUFFER_SPACE(n)                        \
  2928.   {                                        \
  2929.     while (b - bufp->buffer + (n) >= bufp->allocated)            \
  2930. d313 1
  2931. a313 1
  2932. #define BUFPUSH(ch)                            \
  2933. d315 2
  2934. a316 2
  2935.     GET_BUFFER_SPACE (1);                        \
  2936.     *b++ = (char) (ch);                            \
  2937. d318 1
  2938. a318 1
  2939.   
  2940. d325 1
  2941. a325 1
  2942.     if (bufp->allocated == (1L<<16)) goto too_big;            \
  2943. d327 3
  2944. a329 3
  2945.     if (bufp->allocated > (1L<<16)) bufp->allocated = (1L<<16);        \
  2946.     bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated);    \
  2947.     if (bufp->buffer == 0)                        \
  2948. d331 8
  2949. a338 8
  2950.     b = (b - old_buffer) + bufp->buffer;                \
  2951.     if (fixup_jump)                            \
  2952.       fixup_jump = (fixup_jump - old_buffer) + bufp->buffer;        \
  2953.     if (laststart)                            \
  2954.       laststart = (laststart - old_buffer) + bufp->buffer;        \
  2955.     begalt = (begalt - old_buffer) + bufp->buffer;            \
  2956.     if (pending_exact)                            \
  2957.       pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  2958. d345 2
  2959. a346 2
  2960. #define GET_UNSIGNED_NUMBER(num)                     \
  2961.   { if (p != pend)                             \
  2962. d348 4
  2963. a351 4
  2964.         PATFETCH (c);                             \
  2965.     while (isdigit (c))                         \
  2966.       {                                 \
  2967.         if (num < 0)                         \
  2968. d353 6
  2969. a358 6
  2970.             num = num * 10 + c - '0';                     \
  2971.         if (p == pend)                         \
  2972.            break;                             \
  2973.         PATFETCH (c);                         \
  2974.       }                                 \
  2975.         }                                 \
  2976. d361 1
  2977. a361 1
  2978. /* Subroutines for re_compile_pattern.  */
  2979. d371 1
  2980. a371 1
  2981.    BUFP        is a  struct re_pattern_buffer *  which points to the info
  2982. d373 1
  2983. a373 1
  2984.          This structure contains a  char *  which points to the
  2985. d398 1
  2986. a398 1
  2987.      
  2988. d427 1
  2989. a427 1
  2990.   /* In processing an interval, at most this many matches can be made.  */
  2991. d433 1
  2992. a433 1
  2993.   
  2994. d499 1
  2995. a499 1
  2996.             if (obscure_syntax & RE_TIGHT_VBAR)
  2997. d513 1
  2998. a513 1
  2999.             if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && p1 != pend)
  3000. d528 1
  3001. a528 1
  3002.           }
  3003. d530 6
  3004. a535 6
  3005.       /* ^ means succeed if at beg of line, but only if no preceding 
  3006.              pattern.  */
  3007.              
  3008.           if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && laststart)
  3009.             goto invalid_pattern;
  3010.           if (laststart && p - 2 >= pattern && p[-2] != '\n'
  3011. d559 4
  3012. a562 4
  3013.             {
  3014.               if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  3015.                 goto invalid_pattern;
  3016.               else if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  3017. d564 1
  3018. a564 1
  3019.             }
  3020. d603 1
  3021. a603 1
  3022.       if (!laststart)  
  3023. d611 4
  3024. a614 4
  3025.                  end a backward relative jump from b to before the next
  3026.                  jump we're going to put in below (which jumps from
  3027.                  laststart to after this jump).  */
  3028.               GET_BUFFER_SPACE (3);
  3029. d616 1
  3030. a616 1
  3031.           b += 3;      /* Because store_jump put stuff here.  */
  3032. d618 3
  3033. a620 3
  3034.           /* On failure, jump from laststart to b + 3, which will be the
  3035.              end of the buffer after this jump is inserted.  */
  3036.           GET_BUFFER_SPACE (3);
  3037. d627 5
  3038. a631 5
  3039.                  dummy-failure before the initial on-failure-jump
  3040.                  instruction of the loop. This effects a skip over that
  3041.                  instruction the first time we hit that loop.  */
  3042.               GET_BUFFER_SPACE (6);
  3043.               insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  3044. d641 3
  3045. a643 3
  3046.         case '[':
  3047.           if (p == pend)
  3048.             goto invalid_pattern;
  3049. d651 3
  3050. a653 3
  3051.               BUFPUSH (charset_not); 
  3052.               p++;
  3053.             }
  3054. d661 1
  3055. a661 1
  3056.           
  3057. d663 1
  3058. a663 1
  3059.             SET_LIST_BIT ('\n');
  3060. d673 9
  3061. a681 9
  3062.             {
  3063.               PATFETCH(c1);
  3064.                   SET_LIST_BIT (c1);
  3065.               continue;
  3066.             }
  3067.               if (c == ']')
  3068.                 {
  3069.                   if (p == p1 + 1)
  3070.                     {
  3071. d683 5
  3072. a687 5
  3073.                       if ((obscure_syntax & RE_NO_EMPTY_BRACKETS) 
  3074.                           && p == pend)
  3075.                         goto invalid_pattern;
  3076.                     }
  3077.                   else 
  3078. d689 6
  3079. a694 6
  3080.                        expression, but rather the end of a bracket
  3081.                        expression.  */
  3082.                     break;
  3083.                 }
  3084.               /* Get a range.  */
  3085.               if (p[0] == '-' && p[1] != ']')
  3086. d696 1
  3087. a696 1
  3088.                   PATFETCH (c1);
  3089. d698 1
  3090. a698 1
  3091.                   
  3092. d700 7
  3093. a706 7
  3094.                     goto invalid_pattern;
  3095.                     
  3096.           if ((obscure_syntax & RE_NO_HYPHEN_RANGE_END) 
  3097.                       && c1 == '-' && *p != ']')
  3098.                     goto invalid_pattern;
  3099.                     
  3100.                   while (c <= c1)
  3101. d708 2
  3102. a709 2
  3103.                       SET_LIST_BIT (c);
  3104.                       c++;
  3105. d711 1
  3106. a711 1
  3107.                 }
  3108. d714 1
  3109. a714 1
  3110.                 {
  3111. d716 1
  3112. a716 1
  3113.                   char str[CHAR_CLASS_MAX_LENGTH];
  3114. d720 2
  3115. a721 2
  3116.                   if (p == pend)
  3117.                     goto invalid_pattern;
  3118. d725 1
  3119. a725 1
  3120.                       PATFETCH_RAW (c);
  3121. d727 2
  3122. a728 2
  3123.                           || c1 == CHAR_CLASS_MAX_LENGTH)
  3124.                 break;
  3125. d732 3
  3126. a734 3
  3127.           if (p == pend     
  3128.               || c == ']'    /* End of the bracket expression.  */
  3129.                       || p[0] != ']'
  3130. d736 10
  3131. a745 10
  3132.                       || (strcmp (str, "alpha") != 0 
  3133.                           && strcmp (str, "upper") != 0
  3134.               && strcmp (str, "lower") != 0 
  3135.                           && strcmp (str, "digit") != 0
  3136.               && strcmp (str, "alnum") != 0 
  3137.                           && strcmp (str, "xdigit") != 0
  3138.               && strcmp (str, "space") != 0 
  3139.                           && strcmp (str, "print") != 0
  3140.               && strcmp (str, "punct") != 0 
  3141.                           && strcmp (str, "graph") != 0
  3142. d748 4
  3143. a751 4
  3144.                /* Undo the ending character, the letters, and leave 
  3145.                           the leading : and [ (but set bits for them).  */
  3146.                       c1++;
  3147.               while (c1--)    
  3148. d755 7
  3149. a761 7
  3150.                 }
  3151.                   else
  3152.                     {
  3153.                       /* The ] at the end of the character class.  */
  3154.                       PATFETCH (c);                    
  3155.                       if (c != ']')
  3156.                         goto invalid_pattern;
  3157. d778 3
  3158. a780 3
  3159.                 }
  3160.               else
  3161.                 SET_LIST_BIT (c);
  3162. d783 6
  3163. a788 6
  3164.           /* Discard any character set/class bitmap bytes that are all
  3165.              0 at the end of the map. Decrement the map-length byte too.  */
  3166.           while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  3167.             b[-1]--; 
  3168.           b += b[-1];
  3169.           break;
  3170. d802 1
  3171. a802 1
  3172.         case '\n':
  3173. d810 1
  3174. a810 1
  3175.               && (! laststart  ||  p == pend))
  3176. d812 1
  3177. a812 1
  3178.           else if (! (obscure_syntax & RE_NO_BK_VBAR))
  3179. d818 7
  3180. a824 7
  3181.            if (! ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  3182.                   && (obscure_syntax & RE_INTERVALS)))
  3183.              goto normal_char;
  3184.            else
  3185.              goto handle_interval;
  3186.              
  3187.         case '\\':
  3188. d835 3
  3189. a837 3
  3190.               /* Laststart should point to the start_memory that we are about
  3191.                  to push (unless the pattern has RE_NREGS or more ('s).  */
  3192.               *stackp++ = b - bufp->buffer;    
  3193. d839 1
  3194. a839 1
  3195.             {
  3196. d842 1
  3197. a842 1
  3198.             }
  3199. d865 2
  3200. a866 2
  3201.               fixup_jump = *stackp ? *stackp + bufp->buffer - 1 : 0;
  3202.               laststart = *--stackp + bufp->buffer;
  3203. d870 2
  3204. a871 2
  3205.               if ((obscure_syntax & RE_LIMITED_OPS)
  3206.               || (obscure_syntax & RE_NO_BK_VBAR))
  3207. d874 2
  3208. a875 2
  3209.               if (obscure_syntax & RE_LIMITED_OPS)
  3210.                 goto normal_char;
  3211. d877 3
  3212. a879 3
  3213.                  jumps to this alternative if the former fails.  */
  3214.               GET_BUFFER_SPACE (6);
  3215.               insert_jump (on_failure_jump, begalt, b + 6, b);
  3216. d883 7
  3217. a889 7
  3218.                  jump after it which gets executed if it gets matched.
  3219.                  Adjust that jump so it will jump to the previous
  3220.                  alternative's analogous jump (put in below, which in
  3221.                  turn will jump to the next (if any) alternative's such
  3222.                  jump, etc.).  The last such jump jumps to the correct
  3223.                  final destination.  */
  3224.               if (fixup_jump)
  3225. a890 5
  3226.                 
  3227.           /* Leave space for a jump after previous alternative---to be 
  3228.                  filled in later.  */
  3229.               fixup_jump = b;
  3230.               b += 3;
  3231. d892 6
  3232. a897 1
  3233.               laststart = 0;
  3234. d901 2
  3235. a902 2
  3236.             case '{': 
  3237.               if (! (obscure_syntax & RE_INTERVALS)
  3238. d904 2
  3239. a905 2
  3240.                   || ((obscure_syntax & RE_INTERVALS)
  3241.                       && (obscure_syntax & RE_NO_BK_CURLY_BRACES))
  3242. d908 2
  3243. a909 2
  3244.                 goto normal_backsl;
  3245.             handle_interval:
  3246. d911 1
  3247. a911 1
  3248.               /* If there is no previous pattern, this isn't an interval.  */
  3249. d913 2
  3250. a914 2
  3251.             {
  3252.                   if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  3253. d916 8
  3254. a923 8
  3255.                   else
  3256.                     goto normal_backsl;
  3257.                 }
  3258.               /* It also isn't an interval if not preceded by an re
  3259.                  matching a single character or subexpression, or if
  3260.                  the current type of intervals can't handle back
  3261.                  references and the previous thing is a back reference.  */
  3262.               if (! (*laststart == anychar
  3263. d929 5
  3264. a933 5
  3265.                          && *laststart == duplicate)))
  3266.                 {
  3267.                   if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  3268.                     goto normal_char;
  3269.                     
  3270. d935 5
  3271. a939 5
  3272.                      statement; this is for Posix basic syntax.  */
  3273.                   if (obscure_syntax & RE_INTERVALS)
  3274.                     goto invalid_pattern;
  3275.                     
  3276.                   goto normal_backsl;
  3277. d941 1
  3278. a941 1
  3279.               lower_bound = -1;            /* So can see if are set.  */
  3280. d943 1
  3281. a943 1
  3282.               GET_UNSIGNED_NUMBER (lower_bound);
  3283. d952 6
  3284. a957 6
  3285.               if (! (obscure_syntax & RE_NO_BK_CURLY_BRACES)) 
  3286.                 {
  3287.                   if (c != '\\')
  3288.                     goto invalid_pattern;
  3289.                   PATFETCH (c);
  3290.                 }
  3291. d959 4
  3292. a962 4
  3293.           || lower_bound > upper_bound 
  3294.                   || ((obscure_syntax & RE_NO_BK_CURLY_BRACES) 
  3295.               && p != pend  && *p == '{')) 
  3296.             {
  3297. d964 20
  3298. a983 20
  3299.                     goto unfetch_interval;
  3300.                   else
  3301.                     goto invalid_pattern;
  3302.         }
  3303.  
  3304.           /* If upper_bound is zero, don't want to succeed at all; 
  3305.           jump from laststart to b + 3, which will be the end of
  3306.                  the buffer after this jump is inserted.  */
  3307.                  
  3308.                if (upper_bound == 0)
  3309.                  {
  3310.                    GET_BUFFER_SPACE (3);
  3311.                    insert_jump (jump, laststart, b + 3, b);
  3312.                    b += 3;
  3313.                  }
  3314.  
  3315.                /* Otherwise, after lower_bound number of succeeds, jump
  3316.                   to after the jump_n which will be inserted at the end
  3317.                   of the buffer, and insert that jump_n.  */
  3318.                else 
  3319. d985 16
  3320. a1000 16
  3321.                   hence no jump_n is inserted at the current end of
  3322.                       the buffer; then only space for the succeed_n is
  3323.                       needed.  Otherwise, need space for both the
  3324.                       succeed_n and the jump_n.  */
  3325.                       
  3326.                    unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  3327.                      
  3328.                    GET_BUFFER_SPACE (slots_needed);
  3329.                    /* Initialize the succeed_n to n, even though it will
  3330.                       be set by its attendant set_number_at, because
  3331.                       re_compile_fastmap will need to know it.  Jump to
  3332.                       what the end of buffer will be after inserting
  3333.                       this succeed_n and possibly appending a jump_n.  */
  3334.                    insert_jump_n (succeed_n, laststart, b + slots_needed, 
  3335.                           b, lower_bound);
  3336.                    b += 5;     /* Just increment for the succeed_n here.  */
  3337. d1004 11
  3338. a1014 11
  3339.                      succeed_n we put in above.  By the time we've gotten
  3340.                      to this jump when matching, we'll have matched once
  3341.                      already, so jump back only upper_bound - 1 times.  */
  3342.  
  3343.                    if (upper_bound > 1)
  3344.                      {
  3345.                        store_jump_n (b, jump_n, laststart, upper_bound - 1);
  3346.                        b += 5;
  3347.                        /* When hit this when matching, reset the
  3348.                           preceding jump_n's n to upper_bound - 1.  */
  3349.                        BUFPUSH (set_number_at);
  3350. d1016 3
  3351. a1018 3
  3352.                        STORE_NUMBER_AND_INCR (b, -5);
  3353.                        STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  3354.                      }
  3355. d1020 1
  3356. a1020 1
  3357.                    GET_BUFFER_SPACE (5);
  3358. d1022 3
  3359. a1024 3
  3360.                    b += 5;
  3361.                  }
  3362.               pending_exact = 0;
  3363. d1026 1
  3364. a1026 1
  3365.               break;
  3366. d1029 1
  3367. a1029 1
  3368.             unfetch_interval:
  3369. d1032 4
  3370. a1035 4
  3371.                  p = beg_interval;
  3372.              else
  3373.                  {
  3374.                    fprintf (stderr, 
  3375. d1038 4
  3376. a1041 4
  3377.                  }
  3378.                  
  3379.                beg_interval = 0;
  3380.                PATFETCH (c);        /* normal_char expects char in `c'.  */
  3381. d1050 1
  3382. a1050 1
  3383.         case 's':    
  3384. d1109 2
  3385. a1110 2
  3386.                 goto normal_char;
  3387.               c1 = c - '0';
  3388. d1113 8
  3389. a1120 8
  3390.             if (obscure_syntax & RE_NO_EMPTY_BK_REF)
  3391.                     goto invalid_pattern;
  3392.                   else
  3393.                     goto normal_char;
  3394.                 }
  3395.               /* Can't back reference to a subexpression if inside of it.  */
  3396.               for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  3397.          if (*stackt == c1)
  3398. d1132 2
  3399. a1133 2
  3400.                 goto normal_backsl;
  3401.               break;
  3402. d1135 1
  3403. a1135 1
  3404.             default:
  3405. d1139 1
  3406. a1139 1
  3407.          it will never match anything.  */
  3408. d1152 2
  3409. a1153 2
  3410.           || ((obscure_syntax & RE_INTERVALS) 
  3411.                   && ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  3412. d1155 1
  3413. a1155 1
  3414.                       : (p[0] == '\\' && p[1] == '{'))))
  3415. d1213 1
  3416. a1213 1
  3417.    CURRENT_END gives the end of the storage not in use, so we know 
  3418. d1216 1
  3419. a1216 1
  3420.    If you call this function, you must zero out pending_exact.  */
  3421. d1224 1
  3422. a1224 1
  3423.   register char *pto = current_end + 3;        /* ...to here.  */
  3424. d1226 1
  3425. a1226 1
  3426.   while (pfrom != from)                   
  3427. d1237 2
  3428. a1238 2
  3429.    
  3430.    If you call this function, you must zero out pending_exact.  */
  3431. d1253 1
  3432. a1253 1
  3433.    number to handle minimum and maximum cases.  Open up space at
  3434. d1258 1
  3435. a1258 1
  3436.    If you call this function, you must zero out pending_exact.  */
  3437. d1267 1
  3438. a1267 1
  3439.   register char *pto = current_end + 5;        /* ...to here.  */
  3440. d1269 1
  3441. a1269 1
  3442.   while (pfrom != from)                   
  3443. d1279 1
  3444. a1279 1
  3445.    If you call this function, you must zero out pending_exact.  */
  3446. d1288 1
  3447. a1288 1
  3448.   register char *pto = current_end + 5;        /* ...to here.  */
  3449. d1290 1
  3450. a1290 1
  3451.   while (pfrom != there)                   
  3452. d1292 1
  3453. a1292 1
  3454.   
  3455. d1300 1
  3456. a1300 1
  3457. /* Given a pattern, compute a fastmap from it.  The fastmap records
  3458. d1305 1
  3459. a1305 1
  3460.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data 
  3461. d1329 1
  3462. a1329 1
  3463.       
  3464. d1351 2
  3465. a1352 2
  3466.         case begline:
  3467.         case before_dot:
  3468. d1361 1
  3469. a1361 1
  3470.           continue;
  3471. d1368 1
  3472. a1368 1
  3473.             
  3474. d1374 1
  3475. a1374 1
  3476.         case finalize_jump:
  3477. d1378 2
  3478. a1379 2
  3479.           EXTRACT_NUMBER_AND_INCR (j, p);
  3480.       p += j;    
  3481. d1382 1
  3482. a1382 1
  3483.           /* Jump backward reached implies we just went through
  3484. d1387 1
  3485. a1387 1
  3486.          If so, discard that as redundant.  */
  3487. d1389 1
  3488. a1389 1
  3489.           if ((enum regexpcode) *p != on_failure_jump
  3490. d1392 8
  3491. a1399 8
  3492.           p++;
  3493.           EXTRACT_NUMBER_AND_INCR (j, p);
  3494.           p += j;        
  3495.           if (stackp != stackb && *stackp == p)
  3496.             stackp--;
  3497.           continue;
  3498.       
  3499.         case on_failure_jump:
  3500. d1401 2
  3501. a1402 2
  3502.           EXTRACT_NUMBER_AND_INCR (j, p);
  3503.           *++stackp = p + j;
  3504. d1404 1
  3505. a1404 1
  3506.             EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  3507. d1409 2
  3508. a1410 2
  3509.           /* Get to the number of times to succeed.  */
  3510.           p += 2;        
  3511. d1412 2
  3512. a1413 2
  3513.           EXTRACT_NUMBER_AND_INCR (k, p);
  3514.           if (k == 0)
  3515. d1415 5
  3516. a1419 5
  3517.               p -= 4;
  3518.               goto handle_on_failure_jump;
  3519.             }
  3520.           continue;
  3521.           
  3522. d1421 2
  3523. a1422 2
  3524.           p += 4;
  3525.           continue;
  3526. d1424 1
  3527. a1424 1
  3528.         case start_memory:
  3529. d1439 1
  3530. a1439 1
  3531.          so we can set can_be_null if appropriate.  */
  3532. d1501 3
  3533. a1503 3
  3534.          characters of one path of the pattern.  We need not follow this
  3535.          path any farther.  Instead, look at the next alternative
  3536.          remembered in the stack.  */
  3537. d1523 1
  3538. a1523 1
  3539.   return re_search_2 (pbufp, (char *) 0, 0, string, size, startpos, range, 
  3540. d1560 1
  3541. a1560 1
  3542.   /* Check for out-of-range starting position.  */
  3543. d1563 1
  3544. a1563 1
  3545.     
  3546. d1574 1
  3547. a1574 1
  3548.   
  3549. d1587 1
  3550. a1587 1
  3551.     { 
  3552. d1589 4
  3553. a1592 4
  3554.          cannot possibly be the start of a match.  Note, however, that
  3555.          if the pattern can possibly match the null string, we must
  3556.          test it at each starting point so that we take the first null
  3557.          string we get.  */
  3558. d1596 1
  3559. a1596 1
  3560.       if (range > 0)    /* Searching forwards.  */
  3561. d1607 3
  3562. a1609 3
  3563.               while (range > lim && !fastmap[translate 
  3564.                                              ? translate[*p++]
  3565.                                              : *p++])
  3566. d1617 1
  3567. a1617 1
  3568.               if (string1 == 0 || startpos >= size1)
  3569. d1619 1
  3570. a1619 1
  3571.           else 
  3572. d1622 1
  3573. a1622 1
  3574.               c &= 0xff;
  3575. d1644 7
  3576. a1650 7
  3577.       if (!range) 
  3578.         break;
  3579.       else if (range > 0) 
  3580.         {
  3581.           range--; 
  3582.           startpos++;
  3583.         }
  3584. d1652 4
  3585. a1655 4
  3586.         {
  3587.           range++; 
  3588.           startpos--;
  3589.         }
  3590. d1662 1
  3591. a1662 1
  3592. #ifndef emacs   /* emacs never uses this.  */
  3593. d1670 1
  3594. a1670 1
  3595.   return re_match_2 (pbufp, (char *) 0, 0, string, size, pos, regs, size); 
  3596. d1679 1
  3597. a1679 1
  3598.    
  3599. d1682 1
  3600. a1682 1
  3601. /* Routine used by re_match_2.  */
  3602. d1709 1
  3603. a1709 1
  3604. #define MAX_NUM_FAILURE_ITEMS   (RE_NREGS * NUM_REG_ITEMS + 2)
  3605. d1718 1
  3606. a1718 1
  3607.    if we ever fail back to it.  */
  3608. d1720 1
  3609. a1720 1
  3610. #define PUSH_FAILURE_POINT(pattern_place, string_place)            \
  3611. d1725 1
  3612. a1725 1
  3613.        (Aside from register zero, which is only set at the end.)  */    \
  3614. d1727 2
  3615. a1728 2
  3616.       if (regstart[last_used_reg] != (unsigned char *) -1)        \
  3617.         break;                                \
  3618. d1730 4
  3619. a1733 4
  3620.     if (stacke - stackp < NUM_FAILURE_ITEMS)                \
  3621.       {                                    \
  3622.     unsigned char **stackx;                        \
  3623.     if (stacke - stackb > re_max_failures * MAX_NUM_FAILURE_ITEMS)    \
  3624. d1736 4
  3625. a1739 4
  3626.         /* Roughly double the size of the stack.  */            \
  3627.         stackx = (unsigned char **) alloca (2 * MAX_NUM_FAILURE_ITEMS    \
  3628.                             * (stacke - stackb)        \
  3629.                                             * sizeof (unsigned char *));\
  3630. d1741 2
  3631. a1742 2
  3632.         bcopy (stackb, stackx, (stackp - stackb) * sizeof (char *));    \
  3633.     stackp = stackx + (stackp - stackb);                \
  3634. d1745 1
  3635. a1745 1
  3636.       }                                    \
  3637. d1748 6
  3638. a1753 6
  3639.     for (this_reg = 1; this_reg <= last_used_reg; this_reg++)        \
  3640.       {                                    \
  3641.         *stackp++ = regstart[this_reg];                    \
  3642.         *stackp++ = regend[this_reg];                    \
  3643.         *stackp++ = (unsigned char *) ®_info[this_reg];        \
  3644.       }                                    \
  3645. d1756 1
  3646. a1756 1
  3647.     *stackp++ = (unsigned char *) last_used_reg;            \
  3648. d1758 2
  3649. a1759 2
  3650.     *stackp++ = pattern_place;                                          \
  3651.     *stackp++ = string_place;                                           \
  3652. a1760 1
  3653.   
  3654. d1762 1
  3655. d1765 1
  3656. a1765 1
  3657. #define POP_FAILURE_POINT()                        \
  3658. d1769 1
  3659. a1769 1
  3660.     temp = (int) *--stackp;    /* How many regs pushed.  */            \
  3661. d1771 1
  3662. a1771 1
  3663.     stackp -= temp;         /* Remove the register info.  */    \
  3664. d1779 2
  3665. a1780 2
  3666.    
  3667. #define IS_IN_FIRST_STRING(ptr)                     \
  3668. d1787 1
  3669. a1787 1
  3670.  while (d == dend)                                \
  3671. d1789 2
  3672. a1790 2
  3673.     /* end of string2 => fail.  */                    \
  3674.     if (dend == end_match_2)                         \
  3675. d1792 3
  3676. a1794 3
  3677.     /* end of string1 => advance to string2.  */             \
  3678.     d = string2;                                \
  3679.     dend = end_match_2;                            \
  3680. d1800 4
  3681. a1803 4
  3682.    are inside.  */
  3683. #define SET_REGS_MATCHED                         \
  3684.   { unsigned this_reg;                             \
  3685.     for (this_reg = 0; this_reg < RE_NREGS; this_reg++)         \
  3686. d1805 4
  3687. a1808 4
  3688.         if (IS_ACTIVE(reg_info[this_reg]))                \
  3689.           MATCHED_SOMETHING(reg_info[this_reg]) = 1;            \
  3690.         else                                \
  3691.           MATCHED_SOMETHING(reg_info[this_reg]) = 0;            \
  3692. d1816 2
  3693. a1817 2
  3694. #define AT_STRINGS_BEG  (d == (size1 ? string1 : string2)  ||  !size2)
  3695. #define AT_STRINGS_END  (d == end2)    
  3696. d1822 1
  3697. a1822 1
  3698. /* We have two special cases to check for: 
  3699. d1824 1
  3700. a1824 1
  3701.         character in string2;
  3702. d1826 3
  3703. a1828 3
  3704.         last character in string1; we assume there is a string1, so use
  3705.         this in conjunction with AT_STRINGS_BEG.  */
  3706. #define IS_A_LETTER(d)                            \
  3707. d1903 1
  3708. a1903 1
  3709.      
  3710. d1918 1
  3711. a1918 1
  3712.      variables when we find a match better than any we've seen before. 
  3713. d1937 1
  3714. a1937 1
  3715.   
  3716. d1979 1
  3717. a1979 1
  3718.   /* This loops over pattern commands.  It exits by returning from the
  3719. d1981 1
  3720. a1981 1
  3721.      at this starting point in the input data.  */
  3722. d1986 1
  3723. a1986 1
  3724.       /* End of pattern means we might have succeeded.  */
  3725. d1990 1
  3726. a1990 1
  3727.           if (d != end_match_2)
  3728. d1992 27
  3729. a2018 27
  3730.               if (stackp != stackb)
  3731.                 {
  3732.                   /* More failure points to try.  */
  3733.  
  3734.                   unsigned in_same_string = 
  3735.                           IS_IN_FIRST_STRING (best_regend[0]) 
  3736.                         == MATCHING_IN_FIRST_STRING;
  3737.  
  3738.                   /* If exceeds best match so far, save it.  */
  3739.                   if (! best_regs_set
  3740.                       || (in_same_string && d > best_regend[0])
  3741.                       || (! in_same_string && ! MATCHING_IN_FIRST_STRING))
  3742.                     {
  3743.                       best_regs_set = 1;
  3744.                       best_regend[0] = d;    /* Never use regstart[0].  */
  3745.                       
  3746.                       for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  3747.                         {
  3748.                           best_regstart[mcnt] = regstart[mcnt];
  3749.                           best_regend[mcnt] = regend[mcnt];
  3750.                         }
  3751.                     }
  3752.                   goto fail;           
  3753.                 }
  3754.               /* If no failure points, don't restore garbage.  */
  3755.               else if (best_regs_set)   
  3756.                 {
  3757. d2020 3
  3758. a2022 3
  3759.                   /* Restore best match.  */
  3760.                   d = best_regend[0];
  3761.                   
  3762. d2028 2
  3763. a2029 2
  3764.                 }
  3765.             }
  3766. d2031 1
  3767. a2031 1
  3768.       /* If caller wants register contents data back, convert it 
  3769. d2052 1
  3770. a2052 1
  3771.                     
  3772. d2059 2
  3773. a2060 2
  3774.       return d - pos - (MATCHING_IN_FIRST_STRING 
  3775.                 ? string1 
  3776. d2062 1
  3777. a2062 1
  3778.         }
  3779. d2064 1
  3780. a2064 1
  3781.       /* Otherwise match next pattern command.  */
  3782. d2073 3
  3783. a2075 3
  3784.            \) by stop_memory.  Both of those commands are followed by
  3785.            a register number in the next byte.  The text matched
  3786.            within the \( and \) is recorded under that number.  */
  3787. d2077 5
  3788. a2081 5
  3789.           regstart[*p] = d;
  3790.           IS_ACTIVE (reg_info[*p]) = 1;
  3791.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  3792.           p++;
  3793.           break;
  3794. d2084 2
  3795. a2085 2
  3796.           regend[*p] = d;
  3797.           IS_ACTIVE (reg_info[*p]) = 0;
  3798. d2087 1
  3799. a2087 1
  3800.           /* If just failed to match something this time around with a sub-
  3801. d2089 1
  3802. a2089 1
  3803.           if ((! MATCHED_SOMETHING (reg_info[*p])
  3804. d2091 2
  3805. a2092 2
  3806.           && (p + 1) != pend)              
  3807.             {
  3808. d2094 4
  3809. a2097 4
  3810.               mcnt = 0;
  3811.               switch (*p2++)
  3812.                 {
  3813.                   case jump_n:
  3814. d2099 1
  3815. a2099 1
  3816.                   case finalize_jump:
  3817. d2103 1
  3818. a2103 1
  3819.                     EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  3820. d2106 2
  3821. a2107 2
  3822.                     break;
  3823.                 }
  3824. d2109 5
  3825. a2113 5
  3826.         
  3827.               /* If the next operation is a jump backwards in the pattern
  3828.              to an on_failure_jump, exit from the loop by forcing a
  3829.                  failure after pushing on the stack the on_failure_jump's 
  3830.                  jump in the pattern, and d.  */
  3831. d2116 7
  3832. a2122 7
  3833.                   EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  3834.                   PUSH_FAILURE_POINT (p2 + mcnt, d);
  3835.                   goto fail;
  3836.                 }
  3837.             }
  3838.           p++;
  3839.           break;
  3840. d2125 2
  3841. a2126 2
  3842.            followed by the numeric value of <digit> as the register number.  */
  3843.         case duplicate:
  3844. d2128 1
  3845. a2128 1
  3846.         int regno = *p++;   /* Get which register to match against */
  3847. d2132 6
  3848. a2137 6
  3849.             d2 = regstart[regno];
  3850.             
  3851.             /* Where to stop matching; if both the place to start and
  3852.                the place to stop matching are in the same string, then
  3853.                set to the place to stop, otherwise, for now have to use
  3854.                the end of the first string.  */
  3855. d2139 1
  3856. a2139 1
  3857.             dend2 = ((IS_IN_FIRST_STRING (regstart[regno]) 
  3858. d2145 1
  3859. a2145 1
  3860.                    contents.  */
  3861. d2160 1
  3862. a2160 1
  3863.                 
  3864. d2162 2
  3865. a2163 2
  3866.                    one shot, so, if necessary, adjust the count.  */
  3867.                 if (mcnt > dend2 - d2)
  3868. d2165 1
  3869. a2165 1
  3870.                   
  3871. d2167 4
  3872. a2170 4
  3873.                    past them.  */
  3874.         if (translate 
  3875.                     ? bcmp_translate (d, d2, mcnt, translate) 
  3876.                     : bcmp (d, d2, mcnt))
  3877. d2181 2
  3878. a2182 2
  3879.               || ((obscure_syntax & RE_DOT_NOT_NULL) 
  3880.                   && (translate ? translate[*d] : *d) == '\000'))
  3881. d2185 1
  3882. a2185 1
  3883.           d++;
  3884. d2211 1
  3885. a2211 1
  3886.             d++;
  3887. d2216 8
  3888. a2223 8
  3889.           if ((size1 != 0 && d == string1)
  3890.               || (size1 == 0 && size2 != 0 && d == string2)
  3891.               || (d && d[-1] == '\n')
  3892.               || (size1 == 0 && size2 == 0))
  3893.             break;
  3894.           else
  3895.             goto fail;
  3896.             
  3897. d2231 5
  3898. a2235 5
  3899.            an on_failure_jump that points to the start of the next
  3900.            alternative.  Each alternative except the last ends with a
  3901.            jump to the joining point.  (Actually, each jump except for
  3902.            the last one really jumps to the following jump, because
  3903.            tensioning the jumps is a hassle.)  */
  3904. d2238 4
  3905. a2241 4
  3906.        past the end of the repeat text. This makes a failure point so 
  3907.            that on failure to match a repetition, matching restarts past
  3908.            as many repetitions have been found with no way to fail and
  3909.            look for another one.  */
  3910. d2247 4
  3911. a2250 4
  3912.         on_failure:
  3913.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3914.           PUSH_FAILURE_POINT (p + mcnt, d);
  3915.           break;
  3916. d2255 1
  3917. a2255 1
  3918.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3919. d2293 1
  3920. a2293 1
  3921.           p[-1] = (unsigned char) jump;    
  3922. d2296 1
  3923. a2296 1
  3924.         /* Note fall through.  */
  3925. d2299 2
  3926. a2300 2
  3927.            start, where another failure point will be made which will
  3928.            point to after all the repetitions found so far.  */
  3929. d2302 9
  3930. a2310 9
  3931.         /* Take off failure points put on by matching on_failure_jump 
  3932.            because didn't fail.  Also remove the register information
  3933.            put on by the on_failure_jump.  */
  3934.         case finalize_jump:
  3935.           POP_FAILURE_POINT ();
  3936.         /* Note fall through.  */
  3937.         
  3938.     /* Jump without taking off any failure points.  */
  3939.         case jump:
  3940. d2316 18
  3941. a2333 18
  3942.         case dummy_failure_jump:
  3943.           /* Normally, the on_failure_jump pushes a failure point, which
  3944.              then gets popped at finalize_jump.  We will end up at
  3945.              finalize_jump, also, and with a pattern of, say, `a+', we
  3946.              are skipping over the on_failure_jump, so we have to push
  3947.              something meaningless for finalize_jump to pop.  */
  3948.           PUSH_FAILURE_POINT (0, 0);
  3949.           goto nofinalize;
  3950.  
  3951.  
  3952.         /* Have to succeed matching what follows at least n times.  Then
  3953.           just handle like an on_failure_jump.  */
  3954.         case succeed_n: 
  3955.           EXTRACT_NUMBER (mcnt, p + 2);
  3956.           /* Originally, this is how many times we HAVE to succeed.  */
  3957.           if (mcnt)
  3958.             {
  3959.                mcnt--;
  3960. d2335 2
  3961. a2336 2
  3962.                STORE_NUMBER_AND_INCR (p, mcnt);
  3963.             }
  3964. d2338 1
  3965. a2338 1
  3966.             {
  3967. d2340 25
  3968. a2364 25
  3969.               p[3] = unused;
  3970.               goto on_failure;
  3971.             }
  3972.           else
  3973.         { 
  3974.               fprintf (stderr, "regex: the succeed_n's n is not set.\n");
  3975.               exit (1);
  3976.         }
  3977.           break;
  3978.         
  3979.         case jump_n: 
  3980.           EXTRACT_NUMBER (mcnt, p + 2);
  3981.           /* Originally, this is how many times we CAN jump.  */
  3982.           if (mcnt)
  3983.             {
  3984.                mcnt--;
  3985.                STORE_NUMBER(p + 2, mcnt);
  3986.            goto nofinalize;         /* Do the jump without taking off
  3987.                             any failure points.  */
  3988.             }
  3989.           /* If don't have to jump any more, skip over the rest of command.  */
  3990.       else      
  3991.         p += 4;             
  3992.           break;
  3993.         
  3994. d2367 1
  3995. a2367 1
  3996.           register unsigned char *p1;
  3997. d2369 3
  3998. a2371 3
  3999.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4000.             p1 = p + mcnt;
  4001.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4002. d2373 2
  4003. a2374 2
  4004.             break;
  4005.           }
  4006. d2376 4
  4007. a2379 4
  4008.         /* Ignore these.  Used to ignore the n of succeed_n's which
  4009.            currently have n == 0.  */
  4010.         case unused:
  4011.           break;
  4012. d2381 1
  4013. a2381 1
  4014.         case wordbound:
  4015. d2397 3
  4016. a2399 3
  4017.           /* Have to check if AT_STRINGS_BEG before looking at d - 1.  */
  4018.       if (!AT_STRINGS_BEG && IS_A_LETTER (d - 1) 
  4019.               && (!IS_A_LETTER (d) || AT_STRINGS_END))
  4020. d2428 1
  4021. a2428 1
  4022.           SET_REGS_MATCHED;
  4023. d2430 1
  4024. a2430 1
  4025.       
  4026. d2441 1
  4027. a2441 1
  4028.           break;
  4029. d2447 2
  4030. a2448 2
  4031.           if (!IS_A_LETTER (d))
  4032.             goto fail;
  4033. d2451 1
  4034. a2451 1
  4035.       
  4036. d2455 2
  4037. a2456 2
  4038.             goto fail;
  4039.           SET_REGS_MATCHED;
  4040. d2462 3
  4041. a2464 3
  4042.           if (AT_STRINGS_BEG)
  4043.             break;
  4044.           goto fail;
  4045. d2466 1
  4046. a2466 1
  4047.         case endbuf:
  4048. d2476 2
  4049. a2477 2
  4050.              testing `translate' inside the loop.  */
  4051.           if (translate)
  4052. d2496 1
  4053. a2496 1
  4054.           break;
  4055. d2505 4
  4056. a2508 4
  4057.           short last_used_reg, this_reg;
  4058.           
  4059.           /* If this failure point is from a dummy_failure_point, just
  4060.              skip it.  */
  4061. d2510 4
  4062. a2513 4
  4063.             {
  4064.               POP_FAILURE_POINT ();
  4065.               goto fail;
  4066.             }
  4067. d2515 1
  4068. a2515 1
  4069.           d = *--stackp;
  4070. d2517 1
  4071. a2517 1
  4072.           if (d >= string1 && d <= end1)
  4073. d2519 19
  4074. a2537 19
  4075.           /* Restore register info.  */
  4076.           last_used_reg = (short) *--stackp;
  4077.           
  4078.           /* Make the ones that weren't saved -1 or 0 again.  */
  4079.           for (this_reg = RE_NREGS - 1; this_reg > last_used_reg; this_reg--)
  4080.             {
  4081.               regend[this_reg] = (unsigned char *) -1;
  4082.               regstart[this_reg] = (unsigned char *) -1;
  4083.               IS_ACTIVE (reg_info[this_reg]) = 0;
  4084.               MATCHED_SOMETHING (reg_info[this_reg]) = 0;
  4085.             }
  4086.           
  4087.           /* And restore the rest from the stack.  */
  4088.           for ( ; this_reg > 0; this_reg--)
  4089.             {
  4090.               reg_info[this_reg] = *(struct register_info *) *--stackp;
  4091.               regend[this_reg] = *--stackp;
  4092.               regstart[this_reg] = *--stackp;
  4093.             }
  4094. d2540 1
  4095. a2540 1
  4096.         break;   /* Matching at this starting point really fails.  */
  4097. d2545 1
  4098. a2545 1
  4099.   return -1;                     /* Failure to match.  */
  4100. d2566 1
  4101. a2566 1
  4102. /* Entry points compatible with 4.2 BSD regex library.  */
  4103. d2613 1
  4104. a2613 1
  4105. char upcase[0400] = 
  4106. d2669 1
  4107. a2669 1
  4108.       test_posix_extended (); 
  4109. d2703 1
  4110. a2703 1
  4111.           re_compile_pattern (pat, strlen(pat), &buf);
  4112. d2719 1
  4113. a2719 1
  4114.       gets (pat);    /* Now read the string to match against */
  4115. d2738 1
  4116. a2738 1
  4117.   
  4118. d2740 1
  4119. a2740 1
  4120.   
  4121. @
  4122.