home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / sl / src / regex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-13  |  87.3 KB  |  2,875 lines

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