home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume24 / gnudiff1.15 / part07 / regex.c1
Text File  |  1991-03-05  |  45KB  |  1,505 lines

  1. /* Extended regular expression matching and search library.
  2.    Copyright (C) 1985, 1989-90 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. /* To test, compile with -Dtest.  This Dtestable feature turns this into
  20.    a self-contained program which reads a pattern, describes how it
  21.    compiles, then reads a string and searches for it.
  22.    
  23.    On the other hand, if you compile with both -Dtest and -Dcanned you
  24.    can run some tests we've already thought of.  */
  25.  
  26.  
  27. #ifdef emacs
  28.  
  29. /* The `emacs' switch turns on certain special matching commands
  30.   that make sense only in emacs. */
  31.  
  32. #include "config.h"
  33. #include "lisp.h"
  34. #include "buffer.h"
  35. #include "syntax.h"
  36.  
  37. #else  /* not emacs */
  38.  
  39. #if defined (USG) || defined (STDC_HEADERS)
  40. #ifndef BSTRING
  41. #include <string.h>
  42. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  43. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  44. #define bzero(s,n)    memset((s),0,(n))
  45. #endif
  46. #endif
  47.  
  48. #ifdef STDC_HEADERS
  49. #include <stdlib.h>
  50. #else
  51. char *malloc ();
  52. char *realloc ();
  53. #endif
  54.  
  55. /* Make alloca work the best possible way.  */
  56. #ifdef __GNUC__
  57. #define alloca __builtin_alloca
  58. #else
  59. #ifdef sparc
  60. #include <alloca.h>
  61. #else
  62. char *alloca ();
  63. #endif
  64. #endif
  65.  
  66.  
  67. /* Define the syntax stuff, so we can do the \<, \>, etc.  */
  68.  
  69. /* This must be nonzero for the wordchar and notwordchar pattern
  70.    commands in re_match_2.  */
  71. #ifndef Sword 
  72. #define Sword 1
  73. #endif
  74.  
  75. #define SYNTAX(c) re_syntax_table[c]
  76.  
  77.  
  78. #ifdef SYNTAX_TABLE
  79.  
  80. char *re_syntax_table;
  81.  
  82. #else /* not SYNTAX_TABLE */
  83.  
  84. static char re_syntax_table[256];
  85.  
  86.  
  87. static void
  88. init_syntax_once ()
  89. {
  90.    register int c;
  91.    static int done = 0;
  92.  
  93.    if (done)
  94.      return;
  95.  
  96.    bzero (re_syntax_table, sizeof re_syntax_table);
  97.  
  98.    for (c = 'a'; c <= 'z'; c++)
  99.      re_syntax_table[c] = Sword;
  100.  
  101.    for (c = 'A'; c <= 'Z'; c++)
  102.      re_syntax_table[c] = Sword;
  103.  
  104.    for (c = '0'; c <= '9'; c++)
  105.      re_syntax_table[c] = Sword;
  106.  
  107.    done = 1;
  108. }
  109.  
  110. #endif /* SYNTAX_TABLE */
  111. #endif /* emacs */
  112.  
  113. /* We write fatal error messages on standard error.  */
  114. #include <stdio.h>
  115.  
  116. /* isalpha(3) etc. are used for the character classes.  */
  117. #include <ctype.h>
  118. /* Sequents are missing isgraph.  */
  119. #ifndef isgraph
  120. #define isgraph(c) (isprint((c)) && !isspace((c)))
  121. #endif
  122.  
  123. /* Get the interface, including the syntax bits.  */
  124. #include "regex.h"
  125.  
  126.  
  127. /* These are the command codes that appear in compiled regular
  128.    expressions, one per byte.  Some command codes are followed by
  129.    argument bytes.  A command code can specify any interpretation
  130.    whatsoever for its arguments.  Zero-bytes may appear in the compiled
  131.    regular expression.
  132.    
  133.    The value of `exactn' is needed in search.c (search_buffer) in emacs.
  134.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  135.    `exactn' we use here must also be 1.  */
  136.  
  137. enum regexpcode
  138.   {
  139.     unused=0,
  140.     exactn=1, /* Followed by one byte giving n, then by n literal bytes.  */
  141.     begline,  /* Fail unless at beginning of line.  */
  142.     endline,  /* Fail unless at end of line.  */
  143.     jump,     /* Followed by two bytes giving relative address to jump to.  */
  144.     on_failure_jump,     /* Followed by two bytes giving relative address of 
  145.                 place to resume at in case of failure.  */
  146.     finalize_jump,     /* Throw away latest failure point and then jump to 
  147.                 address.  */
  148.     maybe_finalize_jump, /* Like jump but finalize if safe to do so.
  149.                 This is used to jump back to the beginning
  150.                 of a repeat.  If the command that follows
  151.                 this jump is clearly incompatible with the
  152.                 one at the beginning of the repeat, such that
  153.                 we can be sure that there is no use backtracking
  154.                 out of repetitions already completed,
  155.                 then we finalize.  */
  156.     dummy_failure_jump,  /* Jump, and push a dummy failure point. This 
  157.                 failure point will be thrown away if an attempt 
  158.                             is made to use it for a failure. A + construct 
  159.                             makes this before the first repeat.  Also
  160.                             use it as an intermediary kind of jump when
  161.                             compiling an or construct.  */
  162.     succeed_n,     /* Used like on_failure_jump except has to succeed n times;
  163.             then gets turned into an on_failure_jump. The relative
  164.                     address following it is useless until then.  The
  165.                     address is followed by two bytes containing n.  */
  166.     jump_n,     /* Similar to jump, but jump n times only; also the relative
  167.             address following is in turn followed by yet two more bytes
  168.                     containing n.  */
  169.     set_number_at,    /* Set the following relative location to the
  170.                subsequent number.  */
  171.     anychar,     /* Matches any (more or less) one character.  */
  172.     charset,     /* Matches any one char belonging to specified set.
  173.             First following byte is number of bitmap bytes.
  174.             Then come bytes for a bitmap saying which chars are in.
  175.             Bits in each byte are ordered low-bit-first.
  176.             A character is in the set if its bit is 1.
  177.             A character too large to have a bit in the map
  178.             is automatically not in the set.  */
  179.     charset_not, /* Same parameters as charset, but match any character
  180.                     that is not one of those specified.  */
  181.     start_memory, /* Start remembering the text that is matched, for
  182.             storing in a memory register.  Followed by one
  183.                     byte containing the register number.  Register numbers
  184.                     must be in the range 0 through RE_NREGS.  */
  185.     stop_memory, /* Stop remembering the text that is matched
  186.             and store it in a memory register.  Followed by
  187.                     one byte containing the register number. Register
  188.                     numbers must be in the range 0 through RE_NREGS.  */
  189.     duplicate,   /* Match a duplicate of something remembered.
  190.             Followed by one byte containing the index of the memory 
  191.                     register.  */
  192.     before_dot,     /* Succeeds if before point.  */
  193.     at_dot,     /* Succeeds if at point.  */
  194.     after_dot,     /* Succeeds if after point.  */
  195.     begbuf,      /* Succeeds if at beginning of buffer.  */
  196.     endbuf,      /* Succeeds if at end of buffer.  */
  197.     wordchar,    /* Matches any word-constituent character.  */
  198.     notwordchar, /* Matches any char that is not a word-constituent.  */
  199.     wordbeg,     /* Succeeds if at word beginning.  */
  200.     wordend,     /* Succeeds if at word end.  */
  201.     wordbound,   /* Succeeds if at a word boundary.  */
  202.     notwordbound,/* Succeeds if not at a word boundary.  */
  203.     syntaxspec,  /* Matches any character whose syntax is specified.
  204.             followed by a byte which contains a syntax code,
  205.                     e.g., Sword.  */
  206.     notsyntaxspec /* Matches any character whose syntax differs from
  207.                      that specified.  */
  208.   };
  209.  
  210.  
  211. /* Number of failure points to allocate space for initially,
  212.    when matching.  If this number is exceeded, more space is allocated,
  213.    so it is not a hard limit.  */
  214.  
  215. #ifndef NFAILURES
  216. #define NFAILURES 80
  217. #endif
  218.  
  219. #ifdef CHAR_UNSIGNED
  220. #define SIGN_EXTEND_CHAR(c) ((c)>(char)127?(c)-256:(c)) /* for IBM RT */
  221. #endif
  222. #ifndef SIGN_EXTEND_CHAR
  223. #define SIGN_EXTEND_CHAR(x) (x)
  224. #endif
  225.  
  226.  
  227. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  228. #define STORE_NUMBER(destination, number)                \
  229.   { (destination)[0] = (number) & 0377;                    \
  230.     (destination)[1] = (number) >> 8; }
  231.   
  232. /* Same as STORE_NUMBER, except increment the destination pointer to
  233.    the byte after where the number is stored.  Watch out that values for
  234.    DESTINATION such as p + 1 won't work, whereas p will.  */
  235. #define STORE_NUMBER_AND_INCR(destination, number)            \
  236.   { STORE_NUMBER(destination, number);                    \
  237.     (destination) += 2; }
  238.  
  239.  
  240. /* Put into DESTINATION a number stored in two contingous bytes starting
  241.    at SOURCE.  */
  242. #define EXTRACT_NUMBER(destination, source)                \
  243.   { (destination) = *(source) & 0377;                    \
  244.     (destination) += SIGN_EXTEND_CHAR (*(char *)((source) + 1)) << 8; }
  245.  
  246. /* Same as EXTRACT_NUMBER, except increment the pointer for source to
  247.    point to second byte of SOURCE.  Note that SOURCE has to be a value
  248.    such as p, not, e.g., p + 1. */
  249. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  250.   { EXTRACT_NUMBER (destination, source);                \
  251.     (source) += 2; }
  252.  
  253.  
  254. /* Specify the precise syntax of regexps for compilation.  This provides
  255.    for compatibility for various utilities which historically have
  256.    different, incompatible syntaxes.
  257.    
  258.    The argument SYNTAX is a bit-mask comprised of the various bits
  259.    defined in regex.h.  */
  260.  
  261. int
  262. re_set_syntax (syntax)
  263.   int syntax;
  264. {
  265.   int ret;
  266.  
  267.   ret = obscure_syntax;
  268.   obscure_syntax = syntax;
  269.   return ret;
  270. }
  271.  
  272. /* Set by re_set_syntax to the current regexp syntax to recognize.  */
  273. int obscure_syntax = 0;
  274.  
  275.  
  276.  
  277. /* Macros for re_compile_pattern, which is found below these definitions.  */
  278.  
  279. #define CHAR_CLASS_MAX_LENGTH  6
  280.  
  281. /* Fetch the next character in the uncompiled pattern, translating it if
  282.    necessary.  */
  283. #define PATFETCH(c)                            \
  284.   {if (p == pend) goto end_of_pattern;                    \
  285.   c = * (unsigned char *) p++;                        \
  286.   if (translate) c = translate[c]; }
  287.  
  288. /* Fetch the next character in the uncompiled pattern, with no
  289.    translation.  */
  290. #define PATFETCH_RAW(c)                            \
  291.  {if (p == pend) goto end_of_pattern;                    \
  292.   c = * (unsigned char *) p++; }
  293.  
  294. #define PATUNFETCH p--
  295.  
  296.  
  297. /* If the buffer isn't allocated when it comes in, use this.  */
  298. #define INIT_BUF_SIZE  28
  299.  
  300. /* Make sure we have at least N more bytes of space in buffer.  */
  301. #define GET_BUFFER_SPACE(n)                        \
  302.   {                                        \
  303.     while (b - bufp->buffer + (n) >= bufp->allocated)            \
  304.       EXTEND_BUFFER;                            \
  305.   }
  306.  
  307. /* Make sure we have one more byte of buffer space and then add CH to it.  */
  308. #define BUFPUSH(ch)                            \
  309.   {                                    \
  310.     GET_BUFFER_SPACE (1);                        \
  311.     *b++ = (char) (ch);                            \
  312.   }
  313.   
  314. /* Extend the buffer by twice its current size via reallociation and
  315.    reset the pointers that pointed into the old allocation to point to
  316.    the correct places in the new allocation.  If extending the buffer
  317.    results in it being larger than 1 << 16, then flag memory exhausted.  */
  318. #define EXTEND_BUFFER                            \
  319.   { char *old_buffer = bufp->buffer;                    \
  320.     if (bufp->allocated == (1L<<16)) goto too_big;            \
  321.     bufp->allocated *= 2;                        \
  322.     if (bufp->allocated > (1L<<16)) bufp->allocated = (1L<<16);        \
  323.     bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated);    \
  324.     if (bufp->buffer == 0)                        \
  325.       goto memory_exhausted;                        \
  326.     b = (b - old_buffer) + bufp->buffer;                \
  327.     if (fixup_jump)                            \
  328.       fixup_jump = (fixup_jump - old_buffer) + bufp->buffer;        \
  329.     if (laststart)                            \
  330.       laststart = (laststart - old_buffer) + bufp->buffer;        \
  331.     begalt = (begalt - old_buffer) + bufp->buffer;            \
  332.     if (pending_exact)                            \
  333.       pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  334.   }
  335.  
  336. /* Set the bit for character C in a character set list.  */
  337. #define SET_LIST_BIT(c)  (b[(c) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
  338.  
  339. /* Get the next unsigned number in the uncompiled pattern.  */
  340. #define GET_UNSIGNED_NUMBER(num)                     \
  341.   { if (p != pend)                             \
  342.       {                                 \
  343.         PATFETCH (c);                             \
  344.     while (isdigit (c))                         \
  345.       {                                 \
  346.         if (num < 0)                         \
  347.            num = 0;                         \
  348.             num = num * 10 + c - '0';                     \
  349.         if (p == pend)                         \
  350.            break;                             \
  351.         PATFETCH (c);                         \
  352.       }                                 \
  353.         }                                 \
  354.   }
  355.  
  356. /* Subroutines for re_compile_pattern.  */
  357. static void store_jump (), insert_jump (), store_jump_n (),
  358.         insert_jump_n (), insert_op_2 ();
  359.  
  360.  
  361. /* re_compile_pattern takes a regular-expression string
  362.    and converts it into a buffer full of byte commands for matching.
  363.  
  364.    PATTERN   is the address of the pattern string
  365.    SIZE      is the length of it.
  366.    BUFP        is a  struct re_pattern_buffer *  which points to the info
  367.          on where to store the byte commands.
  368.          This structure contains a  char *  which points to the
  369.          actual space, which should have been obtained with malloc.
  370.          re_compile_pattern may use realloc to grow the buffer space.
  371.  
  372.    The number of bytes of commands can be found out by looking in
  373.    the `struct re_pattern_buffer' that bufp pointed to, after
  374.    re_compile_pattern returns. */
  375.  
  376. char *
  377. re_compile_pattern (pattern, size, bufp)
  378.      char *pattern;
  379.      int size;
  380.      struct re_pattern_buffer *bufp;
  381. {
  382.   register char *b = bufp->buffer;
  383.   register char *p = pattern;
  384.   char *pend = pattern + size;
  385.   register unsigned c, c1;
  386.   char *p1;
  387.   unsigned char *translate = (unsigned char *) bufp->translate;
  388.  
  389.   /* Address of the count-byte of the most recently inserted `exactn'
  390.      command.  This makes it possible to tell whether a new exact-match
  391.      character can be added to that command or requires a new `exactn'
  392.      command.  */
  393.      
  394.   char *pending_exact = 0;
  395.  
  396.   /* Address of the place where a forward-jump should go to the end of
  397.      the containing expression.  Each alternative of an `or', except the
  398.      last, ends with a forward-jump of this sort.  */
  399.  
  400.   char *fixup_jump = 0;
  401.  
  402.   /* Address of start of the most recently finished expression.
  403.      This tells postfix * where to find the start of its operand.  */
  404.  
  405.   char *laststart = 0;
  406.  
  407.   /* In processing a repeat, 1 means zero matches is allowed.  */
  408.  
  409.   char zero_times_ok;
  410.  
  411.   /* In processing a repeat, 1 means many matches is allowed.  */
  412.  
  413.   char many_times_ok;
  414.  
  415.   /* Address of beginning of regexp, or inside of last \(.  */
  416.  
  417.   char *begalt = b;
  418.  
  419.   /* In processing an interval, at least this many matches must be made.  */
  420.   int lower_bound;
  421.  
  422.   /* In processing an interval, at most this many matches can be made.  */
  423.   int upper_bound;
  424.  
  425.   /* Place in pattern (i.e., the {) to which to go back if the interval
  426.      is invalid.  */
  427.   char *beg_interval = 0;
  428.   
  429.   /* Stack of information saved by \( and restored by \).
  430.      Four stack elements are pushed by each \(:
  431.        First, the value of b.
  432.        Second, the value of fixup_jump.
  433.        Third, the value of regnum.
  434.        Fourth, the value of begalt.  */
  435.  
  436.   int stackb[40];
  437.   int *stackp = stackb;
  438.   int *stacke = stackb + 40;
  439.   int *stackt;
  440.  
  441.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  442.      where it becomes the register number to put in the stop_memory
  443.      command.  */
  444.  
  445.   int regnum = 1;
  446.  
  447.   bufp->fastmap_accurate = 0;
  448.  
  449. #ifndef emacs
  450. #ifndef SYNTAX_TABLE
  451.   /* Initialize the syntax table.  */
  452.    init_syntax_once();
  453. #endif
  454. #endif
  455.  
  456.   if (bufp->allocated == 0)
  457.     {
  458.       bufp->allocated = INIT_BUF_SIZE;
  459.       if (bufp->buffer)
  460.     /* EXTEND_BUFFER loses when bufp->allocated is 0.  */
  461.     bufp->buffer = (char *) realloc (bufp->buffer, INIT_BUF_SIZE);
  462.       else
  463.     /* Caller did not allocate a buffer.  Do it for them.  */
  464.     bufp->buffer = (char *) malloc (INIT_BUF_SIZE);
  465.       if (!bufp->buffer) goto memory_exhausted;
  466.       begalt = b = bufp->buffer;
  467.     }
  468.  
  469.   while (p != pend)
  470.     {
  471.       PATFETCH (c);
  472.  
  473.       switch (c)
  474.     {
  475.     case '$':
  476.       {
  477.         char *p1 = p;
  478.         /* When testing what follows the $,
  479.            look past the \-constructs that don't consume anything.  */
  480.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  481.           while (p1 != pend)
  482.         {
  483.           if (*p1 == '\\' && p1 + 1 != pend
  484.               && (p1[1] == '<' || p1[1] == '>'
  485.               || p1[1] == '`' || p1[1] == '\''
  486. #ifdef emacs
  487.               || p1[1] == '='
  488. #endif
  489.               || p1[1] == 'b' || p1[1] == 'B'))
  490.             p1 += 2;
  491.           else
  492.             break;
  493.         }
  494.             if (obscure_syntax & RE_TIGHT_VBAR)
  495.           {
  496.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p1 != pend)
  497.           goto normal_char;
  498.         /* Make operand of last vbar end before this `$'.  */
  499.         if (fixup_jump)
  500.           store_jump (fixup_jump, jump, b);
  501.         fixup_jump = 0;
  502.         BUFPUSH (endline);
  503.         break;
  504.           }
  505.         /* $ means succeed if at end of line, but only in special contexts.
  506.           If validly in the middle of a pattern, it is a normal character. */
  507.  
  508.             if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && p1 != pend)
  509.           goto invalid_pattern;
  510.         if (p1 == pend || *p1 == '\n'
  511.         || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  512.         || (obscure_syntax & RE_NO_BK_PARENS
  513.             ? *p1 == ')'
  514.             : *p1 == '\\' && p1[1] == ')')
  515.         || (obscure_syntax & RE_NO_BK_VBAR
  516.             ? *p1 == '|'
  517.             : *p1 == '\\' && p1[1] == '|'))
  518.           {
  519.         BUFPUSH (endline);
  520.         break;
  521.           }
  522.         goto normal_char;
  523.           }
  524.     case '^':
  525.       /* ^ means succeed if at beg of line, but only if no preceding 
  526.              pattern.  */
  527.              
  528.           if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && laststart)
  529.             goto invalid_pattern;
  530.           if (laststart && p - 2 >= pattern && p[-2] != '\n'
  531.            && !(obscure_syntax & RE_CONTEXT_INDEP_OPS))
  532.         goto normal_char;
  533.       if (obscure_syntax & RE_TIGHT_VBAR)
  534.         {
  535.           if (p != pattern + 1
  536.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  537.         goto normal_char;
  538.           BUFPUSH (begline);
  539.           begalt = b;
  540.         }
  541.       else
  542.         BUFPUSH (begline);
  543.       break;
  544.  
  545.     case '+':
  546.     case '?':
  547.       if ((obscure_syntax & RE_BK_PLUS_QM)
  548.           || (obscure_syntax & RE_LIMITED_OPS))
  549.         goto normal_char;
  550.     handle_plus:
  551.     case '*':
  552.       /* If there is no previous pattern, char not special. */
  553.       if (!laststart)
  554.             {
  555.               if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  556.                 goto invalid_pattern;
  557.               else if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  558.         goto normal_char;
  559.             }
  560.       /* If there is a sequence of repetition chars,
  561.          collapse it down to just one.  */
  562.       zero_times_ok = 0;
  563.       many_times_ok = 0;
  564.       while (1)
  565.         {
  566.           zero_times_ok |= c != '+';
  567.           many_times_ok |= c != '?';
  568.           if (p == pend)
  569.         break;
  570.           PATFETCH (c);
  571.           if (c == '*')
  572.         ;
  573.           else if (!(obscure_syntax & RE_BK_PLUS_QM)
  574.                && (c == '+' || c == '?'))
  575.         ;
  576.           else if ((obscure_syntax & RE_BK_PLUS_QM)
  577.                && c == '\\')
  578.         {
  579.           int c1;
  580.           PATFETCH (c1);
  581.           if (!(c1 == '+' || c1 == '?'))
  582.             {
  583.               PATUNFETCH;
  584.               PATUNFETCH;
  585.               break;
  586.             }
  587.           c = c1;
  588.         }
  589.           else
  590.         {
  591.           PATUNFETCH;
  592.           break;
  593.         }
  594.         }
  595.  
  596.       /* Star, etc. applied to an empty pattern is equivalent
  597.          to an empty pattern.  */
  598.       if (!laststart)  
  599.         break;
  600.  
  601.       /* Now we know whether or not zero matches is allowed
  602.          and also whether or not two or more matches is allowed.  */
  603.       if (many_times_ok)
  604.         {
  605.           /* If more than one repetition is allowed, put in at the
  606.                  end a backward relative jump from b to before the next
  607.                  jump we're going to put in below (which jumps from
  608.                  laststart to after this jump).  */
  609.               GET_BUFFER_SPACE (3);
  610.           store_jump (b, maybe_finalize_jump, laststart - 3);
  611.           b += 3;      /* Because store_jump put stuff here.  */
  612.         }
  613.           /* On failure, jump from laststart to b + 3, which will be the
  614.              end of the buffer after this jump is inserted.  */
  615.           GET_BUFFER_SPACE (3);
  616.       insert_jump (on_failure_jump, laststart, b + 3, b);
  617.       pending_exact = 0;
  618.       b += 3;
  619.       if (!zero_times_ok)
  620.         {
  621.           /* At least one repetition is required, so insert a
  622.                  dummy-failure before the initial on-failure-jump
  623.                  instruction of the loop. This effects a skip over that
  624.                  instruction the first time we hit that loop.  */
  625.               GET_BUFFER_SPACE (6);
  626.               insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  627.           b += 3;
  628.         }
  629.       break;
  630.  
  631.     case '.':
  632.       laststart = b;
  633.       BUFPUSH (anychar);
  634.       break;
  635.  
  636.         case '[':
  637.           if (p == pend)
  638.             goto invalid_pattern;
  639.       while (b - bufp->buffer
  640.          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  641.         EXTEND_BUFFER;
  642.  
  643.       laststart = b;
  644.       if (*p == '^')
  645.         {
  646.               BUFPUSH (charset_not); 
  647.               p++;
  648.             }
  649.       else
  650.         BUFPUSH (charset);
  651.       p1 = p;
  652.  
  653.       BUFPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  654.       /* Clear the whole map */
  655.       bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  656.           
  657.       if ((obscure_syntax & RE_HAT_NOT_NEWLINE) && b[-2] == charset_not)
  658.             SET_LIST_BIT ('\n');
  659.  
  660.  
  661.       /* Read in characters and ranges, setting map bits.  */
  662.       while (1)
  663.         {
  664.           PATFETCH (c);
  665.  
  666.           /* If set, \ escapes characters when inside [...].  */
  667.           if ((obscure_syntax & RE_AWK_CLASS_HACK) && c == '\\')
  668.             {
  669.               PATFETCH(c1);
  670.                   SET_LIST_BIT (c1);
  671.               continue;
  672.             }
  673.               if (c == ']')
  674.                 {
  675.                   if (p == p1 + 1)
  676.                     {
  677.               /* If this is an empty bracket expression.  */
  678.                       if ((obscure_syntax & RE_NO_EMPTY_BRACKETS) 
  679.                           && p == pend)
  680.                         goto invalid_pattern;
  681.                     }
  682.                   else 
  683.             /* Stop if this isn't merely a ] inside a bracket
  684.                        expression, but rather the end of a bracket
  685.                        expression.  */
  686.                     break;
  687.                 }
  688.               /* Get a range.  */
  689.               if (p[0] == '-' && p[1] != ']')
  690.         {
  691.                   PATFETCH (c1);
  692.           PATFETCH (c1);
  693.                   
  694.           if ((obscure_syntax & RE_NO_EMPTY_RANGES) && c > c1)
  695.                     goto invalid_pattern;
  696.                     
  697.           if ((obscure_syntax & RE_NO_HYPHEN_RANGE_END) 
  698.                       && c1 == '-' && *p != ']')
  699.                     goto invalid_pattern;
  700.                     
  701.                   while (c <= c1)
  702.             {
  703.                       SET_LIST_BIT (c);
  704.                       c++;
  705.             }
  706.                 }
  707.           else if ((obscure_syntax & RE_CHAR_CLASSES)
  708.             &&  c == '[' && p[0] == ':')
  709.                 {
  710.           /* Longest valid character class word has six characters.  */
  711.                   char str[CHAR_CLASS_MAX_LENGTH];
  712.           PATFETCH (c);
  713.           c1 = 0;
  714.           /* If no ] at end.  */
  715.                   if (p == pend)
  716.                     goto invalid_pattern;
  717.           while (1)
  718.             {
  719.               /* Don't translate the ``character class'' characters.  */
  720.                       PATFETCH_RAW (c);
  721.               if (c == ':' || c == ']' || p == pend
  722.                           || c1 == CHAR_CLASS_MAX_LENGTH)
  723.                 break;
  724.               str[c1++] = c;
  725.             }
  726.           str[c1] = '\0';
  727.           if (p == pend     
  728.               || c == ']'    /* End of the bracket expression.  */
  729.                       || p[0] != ']'
  730.               || p + 1 == pend
  731.                       || (strcmp (str, "alpha") != 0 
  732.                           && strcmp (str, "upper") != 0
  733.               && strcmp (str, "lower") != 0 
  734.                           && strcmp (str, "digit") != 0
  735.               && strcmp (str, "alnum") != 0 
  736.                           && strcmp (str, "xdigit") != 0
  737.               && strcmp (str, "space") != 0 
  738.                           && strcmp (str, "print") != 0
  739.               && strcmp (str, "punct") != 0 
  740.                           && strcmp (str, "graph") != 0
  741.               && strcmp (str, "cntrl") != 0))
  742.             {
  743.                /* Undo the ending character, the letters, and leave 
  744.                           the leading : and [ (but set bits for them).  */
  745.                       c1++;
  746.               while (c1--)    
  747.             PATUNFETCH;
  748.               SET_LIST_BIT ('[');
  749.               SET_LIST_BIT (':');
  750.                 }
  751.                   else
  752.                     {
  753.                       /* The ] at the end of the character class.  */
  754.                       PATFETCH (c);                    
  755.                       if (c != ']')
  756.                         goto invalid_pattern;
  757.               for (c = 0; c < (1 << BYTEWIDTH); c++)
  758.             {
  759.               if ((strcmp (str, "alpha") == 0  && isalpha (c))
  760.                    || (strcmp (str, "upper") == 0  && isupper (c))
  761.                    || (strcmp (str, "lower") == 0  && islower (c))
  762.                    || (strcmp (str, "digit") == 0  && isdigit (c))
  763.                    || (strcmp (str, "alnum") == 0  && isalnum (c))
  764.                    || (strcmp (str, "xdigit") == 0  && isxdigit (c))
  765.                    || (strcmp (str, "space") == 0  && isspace (c))
  766.                    || (strcmp (str, "print") == 0  && isprint (c))
  767.                    || (strcmp (str, "punct") == 0  && ispunct (c))
  768.                    || (strcmp (str, "graph") == 0  && isgraph (c))
  769.                    || (strcmp (str, "cntrl") == 0  && iscntrl (c)))
  770.                 SET_LIST_BIT (c);
  771.             }
  772.             }
  773.                 }
  774.               else
  775.                 SET_LIST_BIT (c);
  776.         }
  777.  
  778.           /* Discard any character set/class bitmap bytes that are all
  779.              0 at the end of the map. Decrement the map-length byte too.  */
  780.           while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  781.             b[-1]--; 
  782.           b += b[-1];
  783.           break;
  784.  
  785.     case '(':
  786.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  787.         goto normal_char;
  788.       else
  789.         goto handle_open;
  790.  
  791.     case ')':
  792.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  793.         goto normal_char;
  794.       else
  795.         goto handle_close;
  796.  
  797.         case '\n':
  798.       if (! (obscure_syntax & RE_NEWLINE_OR))
  799.         goto normal_char;
  800.       else
  801.         goto handle_bar;
  802.  
  803.     case '|':
  804.       if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  805.               && (! laststart  ||  p == pend))
  806.         goto invalid_pattern;
  807.           else if (! (obscure_syntax & RE_NO_BK_VBAR))
  808.         goto normal_char;
  809.       else
  810.         goto handle_bar;
  811.  
  812.     case '{':
  813.            if (! ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  814.                   && (obscure_syntax & RE_INTERVALS)))
  815.              goto normal_char;
  816.            else
  817.              goto handle_interval;
  818.              
  819.         case '\\':
  820.       if (p == pend) goto invalid_pattern;
  821.       PATFETCH_RAW (c);
  822.       switch (c)
  823.         {
  824.         case '(':
  825.           if (obscure_syntax & RE_NO_BK_PARENS)
  826.         goto normal_backsl;
  827.         handle_open:
  828.           if (stackp == stacke) goto nesting_too_deep;
  829.  
  830.               /* Laststart should point to the start_memory that we are about
  831.                  to push (unless the pattern has RE_NREGS or more ('s).  */
  832.               *stackp++ = b - bufp->buffer;    
  833.           if (regnum < RE_NREGS)
  834.             {
  835.           BUFPUSH (start_memory);
  836.           BUFPUSH (regnum);
  837.             }
  838.           *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  839.           *stackp++ = regnum++;
  840.           *stackp++ = begalt - bufp->buffer;
  841.           fixup_jump = 0;
  842.           laststart = 0;
  843.           begalt = b;
  844.           break;
  845.  
  846.         case ')':
  847.           if (obscure_syntax & RE_NO_BK_PARENS)
  848.         goto normal_backsl;
  849.         handle_close:
  850.           if (stackp == stackb) goto unmatched_close;
  851.           begalt = *--stackp + bufp->buffer;
  852.           if (fixup_jump)
  853.         store_jump (fixup_jump, jump, b);
  854.           if (stackp[-1] < RE_NREGS)
  855.         {
  856.           BUFPUSH (stop_memory);
  857.           BUFPUSH (stackp[-1]);
  858.         }
  859.           stackp -= 2;
  860.               fixup_jump = *stackp ? *stackp + bufp->buffer - 1 : 0;
  861.               laststart = *--stackp + bufp->buffer;
  862.           break;
  863.  
  864.         case '|':
  865.               if ((obscure_syntax & RE_LIMITED_OPS)
  866.               || (obscure_syntax & RE_NO_BK_VBAR))
  867.         goto normal_backsl;
  868.         handle_bar:
  869.               if (obscure_syntax & RE_LIMITED_OPS)
  870.                 goto normal_char;
  871.           /* Insert before the previous alternative a jump which
  872.                  jumps to this alternative if the former fails.  */
  873.               GET_BUFFER_SPACE (6);
  874.               insert_jump (on_failure_jump, begalt, b + 6, b);
  875.           pending_exact = 0;
  876.           b += 3;
  877.           /* The alternative before the previous alternative has a
  878.                  jump after it which gets executed if it gets matched.
  879.                  Adjust that jump so it will jump to the previous
  880.                  alternative's analogous jump (put in below, which in
  881.                  turn will jump to the next (if any) alternative's such
  882.                  jump, etc.).  The last such jump jumps to the correct
  883.                  final destination.  */
  884.               if (fixup_jump)
  885.         store_jump (fixup_jump, jump, b);
  886.                 
  887.           /* Leave space for a jump after previous alternative---to be 
  888.                  filled in later.  */
  889.               fixup_jump = b;
  890.               b += 3;
  891.  
  892.               laststart = 0;
  893.           begalt = b;
  894.           break;
  895.  
  896.             case '{': 
  897.               if (! (obscure_syntax & RE_INTERVALS)
  898.           /* Let \{ be a literal.  */
  899.                   || ((obscure_syntax & RE_INTERVALS)
  900.                       && (obscure_syntax & RE_NO_BK_CURLY_BRACES))
  901.           /* If it's the string "\{".  */
  902.           || (p - 2 == pattern  &&  p == pend))
  903.                 goto normal_backsl;
  904.             handle_interval:
  905.           beg_interval = p - 1;        /* The {.  */
  906.               /* If there is no previous pattern, this isn't an interval.  */
  907.           if (!laststart)
  908.             {
  909.                   if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  910.             goto invalid_pattern;
  911.                   else
  912.                     goto normal_backsl;
  913.                 }
  914.               /* It also isn't an interval if not preceded by an re
  915.                  matching a single character or subexpression, or if
  916.                  the current type of intervals can't handle back
  917.                  references and the previous thing is a back reference.  */
  918.               if (! (*laststart == anychar
  919.              || *laststart == charset
  920.              || *laststart == charset_not
  921.              || *laststart == start_memory
  922.              || (*laststart == exactn  &&  laststart[1] == 1)
  923.              || (! (obscure_syntax & RE_NO_BK_REFS)
  924.                          && *laststart == duplicate)))
  925.                 {
  926.                   if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  927.                     goto normal_char;
  928.                     
  929.           /* Posix extended syntax is handled in previous
  930.                      statement; this is for Posix basic syntax.  */
  931.                   if (obscure_syntax & RE_INTERVALS)
  932.                     goto invalid_pattern;
  933.                     
  934.                   goto normal_backsl;
  935.         }
  936.               lower_bound = -1;            /* So can see if are set.  */
  937.           upper_bound = -1;
  938.               GET_UNSIGNED_NUMBER (lower_bound);
  939.           if (c == ',')
  940.         {
  941.           GET_UNSIGNED_NUMBER (upper_bound);
  942.           if (upper_bound < 0)
  943.             upper_bound = RE_DUP_MAX;
  944.         }
  945.           if (upper_bound < 0)
  946.         upper_bound = lower_bound;
  947.               if (! (obscure_syntax & RE_NO_BK_CURLY_BRACES)) 
  948.                 {
  949.                   if (c != '\\')
  950.                     goto invalid_pattern;
  951.                   PATFETCH (c);
  952.                 }
  953.           if (c != '}' || lower_bound < 0 || upper_bound > RE_DUP_MAX
  954.           || lower_bound > upper_bound 
  955.                   || ((obscure_syntax & RE_NO_BK_CURLY_BRACES) 
  956.               && p != pend  && *p == '{')) 
  957.             {
  958.           if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  959.                     goto unfetch_interval;
  960.                   else
  961.                     goto invalid_pattern;
  962.         }
  963.  
  964.           /* If upper_bound is zero, don't want to succeed at all; 
  965.           jump from laststart to b + 3, which will be the end of
  966.                  the buffer after this jump is inserted.  */
  967.                  
  968.                if (upper_bound == 0)
  969.                  {
  970.                    GET_BUFFER_SPACE (3);
  971.                    insert_jump (jump, laststart, b + 3, b);
  972.                    b += 3;
  973.                  }
  974.  
  975.                /* Otherwise, after lower_bound number of succeeds, jump
  976.                   to after the jump_n which will be inserted at the end
  977.                   of the buffer, and insert that jump_n.  */
  978.                else 
  979.          { /* Set to 5 if only one repetition is allowed and
  980.                   hence no jump_n is inserted at the current end of
  981.                       the buffer; then only space for the succeed_n is
  982.                       needed.  Otherwise, need space for both the
  983.                       succeed_n and the jump_n.  */
  984.                       
  985.                    unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  986.                      
  987.                    GET_BUFFER_SPACE (slots_needed);
  988.                    /* Initialize the succeed_n to n, even though it will
  989.                       be set by its attendant set_number_at, because
  990.                       re_compile_fastmap will need to know it.  Jump to
  991.                       what the end of buffer will be after inserting
  992.                       this succeed_n and possibly appending a jump_n.  */
  993.                    insert_jump_n (succeed_n, laststart, b + slots_needed, 
  994.                           b, lower_bound);
  995.                    b += 5;     /* Just increment for the succeed_n here.  */
  996.  
  997.           /* More than one repetition is allowed, so put in at
  998.              the end of the buffer a backward jump from b to the
  999.                      succeed_n we put in above.  By the time we've gotten
  1000.                      to this jump when matching, we'll have matched once
  1001.                      already, so jump back only upper_bound - 1 times.  */
  1002.  
  1003.                    if (upper_bound > 1)
  1004.                      {
  1005.                        store_jump_n (b, jump_n, laststart, upper_bound - 1);
  1006.                        b += 5;
  1007.                        /* When hit this when matching, reset the
  1008.                           preceding jump_n's n to upper_bound - 1.  */
  1009.                        BUFPUSH (set_number_at);
  1010.                GET_BUFFER_SPACE (2);
  1011.                        STORE_NUMBER_AND_INCR (b, -5);
  1012.                        STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  1013.                      }
  1014.            /* When hit this when matching, set the succeed_n's n.  */
  1015.                    GET_BUFFER_SPACE (5);
  1016.            insert_op_2 (set_number_at, laststart, b, 5, lower_bound);
  1017.                    b += 5;
  1018.                  }
  1019.               pending_exact = 0;
  1020.           beg_interval = 0;
  1021.               break;
  1022.  
  1023.  
  1024.             unfetch_interval:
  1025.           /* If an invalid interval, match the characters as literals.  */
  1026.            if (beg_interval)
  1027.                  p = beg_interval;
  1028.              else
  1029.                  {
  1030.                    fprintf (stderr, 
  1031.               "regex: no interval beginning to which to backtrack.\n");
  1032.            exit (1);
  1033.                  }
  1034.                  
  1035.                beg_interval = 0;
  1036.                PATFETCH (c);        /* normal_char expects char in `c'.  */
  1037.            goto normal_char;
  1038.            break;
  1039.  
  1040. #ifdef emacs
  1041.         case '=':
  1042.           BUFPUSH (at_dot);
  1043.           break;
  1044.  
  1045.         case 's':    
  1046.           laststart = b;
  1047.           BUFPUSH (syntaxspec);
  1048.           PATFETCH (c);
  1049.           BUFPUSH (syntax_spec_code[c]);
  1050.           break;
  1051.  
  1052.         case 'S':
  1053.           laststart = b;
  1054.           BUFPUSH (notsyntaxspec);
  1055.           PATFETCH (c);
  1056.           BUFPUSH (syntax_spec_code[c]);
  1057.           break;
  1058. #endif /* emacs */
  1059.  
  1060.         case 'w':
  1061.           laststart = b;
  1062.           BUFPUSH (wordchar);
  1063.           break;
  1064.  
  1065.         case 'W':
  1066.           laststart = b;
  1067.           BUFPUSH (notwordchar);
  1068.           break;
  1069.  
  1070.         case '<':
  1071.           BUFPUSH (wordbeg);
  1072.           break;
  1073.  
  1074.         case '>':
  1075.           BUFPUSH (wordend);
  1076.           break;
  1077.  
  1078.         case 'b':
  1079.           BUFPUSH (wordbound);
  1080.           break;
  1081.  
  1082.         case 'B':
  1083.           BUFPUSH (notwordbound);
  1084.           break;
  1085.  
  1086.         case '`':
  1087.           BUFPUSH (begbuf);
  1088.           break;
  1089.  
  1090.         case '\'':
  1091.           BUFPUSH (endbuf);
  1092.           break;
  1093.  
  1094.         case '1':
  1095.         case '2':
  1096.         case '3':
  1097.         case '4':
  1098.         case '5':
  1099.         case '6':
  1100.         case '7':
  1101.         case '8':
  1102.         case '9':
  1103.           if (obscure_syntax & RE_NO_BK_REFS)
  1104.                 goto normal_char;
  1105.               c1 = c - '0';
  1106.           if (c1 >= regnum)
  1107.         {
  1108.             if (obscure_syntax & RE_NO_EMPTY_BK_REF)
  1109.                     goto invalid_pattern;
  1110.                   else
  1111.                     goto normal_char;
  1112.                 }
  1113.               /* Can't back reference to a subexpression if inside of it.  */
  1114.               for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  1115.          if (*stackt == c1)
  1116.           goto normal_char;
  1117.           laststart = b;
  1118.           BUFPUSH (duplicate);
  1119.           BUFPUSH (c1);
  1120.           break;
  1121.  
  1122.         case '+':
  1123.         case '?':
  1124.           if (obscure_syntax & RE_BK_PLUS_QM)
  1125.         goto handle_plus;
  1126.           else
  1127.                 goto normal_backsl;
  1128.               break;
  1129.  
  1130.             default:
  1131.         normal_backsl:
  1132.           /* You might think it would be useful for \ to mean
  1133.          not to translate; but if we don't translate it
  1134.          it will never match anything.  */
  1135.           if (translate) c = translate[c];
  1136.           goto normal_char;
  1137.         }
  1138.       break;
  1139.  
  1140.     default:
  1141.     normal_char:        /* Expects the character in `c'.  */
  1142.       if (!pending_exact || pending_exact + *pending_exact + 1 != b
  1143.           || *pending_exact == 0177 || *p == '*' || *p == '^'
  1144.           || ((obscure_syntax & RE_BK_PLUS_QM)
  1145.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  1146.           : (*p == '+' || *p == '?'))
  1147.           || ((obscure_syntax & RE_INTERVALS) 
  1148.                   && ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  1149.               ? *p == '{'
  1150.                       : (p[0] == '\\' && p[1] == '{'))))
  1151.         {
  1152.           laststart = b;
  1153.           BUFPUSH (exactn);
  1154.           pending_exact = b;
  1155.           BUFPUSH (0);
  1156.         }
  1157.       BUFPUSH (c);
  1158.       (*pending_exact)++;
  1159.     }
  1160.     }
  1161.  
  1162.   if (fixup_jump)
  1163.     store_jump (fixup_jump, jump, b);
  1164.  
  1165.   if (stackp != stackb) goto unmatched_open;
  1166.  
  1167.   bufp->used = b - bufp->buffer;
  1168.   return 0;
  1169.  
  1170.  invalid_pattern:
  1171.   return "Invalid regular expression";
  1172.  
  1173.  unmatched_open:
  1174.   return "Unmatched \\(";
  1175.  
  1176.  unmatched_close:
  1177.   return "Unmatched \\)";
  1178.  
  1179.  end_of_pattern:
  1180.   return "Premature end of regular expression";
  1181.  
  1182.  nesting_too_deep:
  1183.   return "Nesting too deep";
  1184.  
  1185.  too_big:
  1186.   return "Regular expression too big";
  1187.  
  1188.  memory_exhausted:
  1189.   return "Memory exhausted";
  1190. }
  1191.  
  1192.  
  1193. /* Store a jump of the form <OPCODE> <relative address>.
  1194.    Store in the location FROM a jump operation to jump to relative
  1195.    address FROM - TO.  OPCODE is the opcode to store.  */
  1196.  
  1197. static void
  1198. store_jump (from, opcode, to)
  1199.      char *from, *to;
  1200.      char opcode;
  1201. {
  1202.   from[0] = opcode;
  1203.   STORE_NUMBER(from + 1, to - (from + 3));
  1204. }
  1205.  
  1206.  
  1207. /* Open up space before char FROM, and insert there a jump to TO.
  1208.    CURRENT_END gives the end of the storage not in use, so we know 
  1209.    how much data to copy up. OP is the opcode of the jump to insert.
  1210.  
  1211.    If you call this function, you must zero out pending_exact.  */
  1212.  
  1213. static void
  1214. insert_jump (op, from, to, current_end)
  1215.      char op;
  1216.      char *from, *to, *current_end;
  1217. {
  1218.   register char *pfrom = current_end;        /* Copy from here...  */
  1219.   register char *pto = current_end + 3;        /* ...to here.  */
  1220.  
  1221.   while (pfrom != from)                   
  1222.     *--pto = *--pfrom;
  1223.   store_jump (from, op, to);
  1224. }
  1225.  
  1226.  
  1227. /* Store a jump of the form <opcode> <relative address> <n> .
  1228.  
  1229.    Store in the location FROM a jump operation to jump to relative
  1230.    address FROM - TO.  OPCODE is the opcode to store, N is a number the
  1231.    jump uses, say, to decide how many times to jump.
  1232.    
  1233.    If you call this function, you must zero out pending_exact.  */
  1234.  
  1235. static void
  1236. store_jump_n (from, opcode, to, n)
  1237.      char *from, *to;
  1238.      char opcode;
  1239.      unsigned n;
  1240. {
  1241.   from[0] = opcode;
  1242.   STORE_NUMBER (from + 1, to - (from + 3));
  1243.   STORE_NUMBER (from + 3, n);
  1244. }
  1245.  
  1246.  
  1247. /* Similar to insert_jump, but handles a jump which needs an extra
  1248.    number to handle minimum and maximum cases.  Open up space at
  1249.    location FROM, and insert there a jump to TO.  CURRENT_END gives the
  1250.    end of the storage in use, so we know how much data to copy up. OP is
  1251.    the opcode of the jump to insert.
  1252.  
  1253.    If you call this function, you must zero out pending_exact.  */
  1254.  
  1255. static void
  1256. insert_jump_n (op, from, to, current_end, n)
  1257.      char op;
  1258.      char *from, *to, *current_end;
  1259.      unsigned n;
  1260. {
  1261.   register char *pfrom = current_end;        /* Copy from here...  */
  1262.   register char *pto = current_end + 5;        /* ...to here.  */
  1263.  
  1264.   while (pfrom != from)                   
  1265.     *--pto = *--pfrom;
  1266.   store_jump_n (from, op, to, n);
  1267. }
  1268.  
  1269.  
  1270. /* Open up space at location THERE, and insert operation OP followed by
  1271.    NUM_1 and NUM_2.  CURRENT_END gives the end of the storage in use, so
  1272.    we know how much data to copy up.
  1273.  
  1274.    If you call this function, you must zero out pending_exact.  */
  1275.  
  1276. static void
  1277. insert_op_2 (op, there, current_end, num_1, num_2)
  1278.      char op;
  1279.      char *there, *current_end;
  1280.      int num_1, num_2;
  1281. {
  1282.   register char *pfrom = current_end;        /* Copy from here...  */
  1283.   register char *pto = current_end + 5;        /* ...to here.  */
  1284.  
  1285.   while (pfrom != there)                   
  1286.     *--pto = *--pfrom;
  1287.   
  1288.   there[0] = op;
  1289.   STORE_NUMBER (there + 1, num_1);
  1290.   STORE_NUMBER (there + 3, num_2);
  1291. }
  1292.  
  1293.  
  1294.  
  1295. /* Given a pattern, compute a fastmap from it.  The fastmap records
  1296.    which of the (1 << BYTEWIDTH) possible characters can start a string
  1297.    that matches the pattern.  This fastmap is used by re_search to skip
  1298.    quickly over totally implausible text.
  1299.  
  1300.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data 
  1301.    area as bufp->fastmap.
  1302.    The other components of bufp describe the pattern to be used.  */
  1303.  
  1304. void
  1305. re_compile_fastmap (bufp)
  1306.      struct re_pattern_buffer *bufp;
  1307. {
  1308.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  1309.   int size = bufp->used;
  1310.   register char *fastmap = bufp->fastmap;
  1311.   register unsigned char *p = pattern;
  1312.   register unsigned char *pend = pattern + size;
  1313.   register int j, k;
  1314.   unsigned char *translate = (unsigned char *) bufp->translate;
  1315.  
  1316.   unsigned char *stackb[NFAILURES];
  1317.   unsigned char **stackp = stackb;
  1318.  
  1319.   unsigned is_a_succeed_n;
  1320.  
  1321.   bzero (fastmap, (1 << BYTEWIDTH));
  1322.   bufp->fastmap_accurate = 1;
  1323.   bufp->can_be_null = 0;
  1324.       
  1325.   while (p)
  1326.     {
  1327.       is_a_succeed_n = 0;
  1328.       if (p == pend)
  1329.     {
  1330.       bufp->can_be_null = 1;
  1331.       break;
  1332.     }
  1333. #ifdef SWITCH_ENUM_BUG
  1334.       switch ((int) ((enum regexpcode) *p++))
  1335. #else
  1336.       switch ((enum regexpcode) *p++)
  1337. #endif
  1338.     {
  1339.     case exactn:
  1340.       if (translate)
  1341.         fastmap[translate[p[1]]] = 1;
  1342.       else
  1343.         fastmap[p[1]] = 1;
  1344.       break;
  1345.  
  1346.         case begline:
  1347.         case before_dot:
  1348.     case at_dot:
  1349.     case after_dot:
  1350.     case begbuf:
  1351.     case endbuf:
  1352.     case wordbound:
  1353.     case notwordbound:
  1354.     case wordbeg:
  1355.     case wordend:
  1356.           continue;
  1357.  
  1358.     case endline:
  1359.       if (translate)
  1360.         fastmap[translate['\n']] = 1;
  1361.       else
  1362.         fastmap['\n'] = 1;
  1363.             
  1364.       if (bufp->can_be_null != 1)
  1365.         bufp->can_be_null = 2;
  1366.       break;
  1367.  
  1368.     case jump_n:
  1369.         case finalize_jump:
  1370.     case maybe_finalize_jump:
  1371.     case jump:
  1372.     case dummy_failure_jump:
  1373.           EXTRACT_NUMBER_AND_INCR (j, p);
  1374.       p += j;    
  1375.       if (j > 0)
  1376.         continue;
  1377.           /* Jump backward reached implies we just went through
  1378.          the body of a loop and matched nothing.
  1379.          Opcode jumped to should be an on_failure_jump.
  1380.          Just treat it like an ordinary jump.
  1381.          For a * loop, it has pushed its failure point already;
  1382.          If so, discard that as redundant.  */
  1383.  
  1384.           if ((enum regexpcode) *p != on_failure_jump
  1385.           && (enum regexpcode) *p != succeed_n)
  1386.         continue;
  1387.           p++;
  1388.           EXTRACT_NUMBER_AND_INCR (j, p);
  1389.           p += j;        
  1390.           if (stackp != stackb && *stackp == p)
  1391.             stackp--;
  1392.           continue;
  1393.       
  1394.         case on_failure_jump:
  1395.     handle_on_failure_jump:
  1396.           EXTRACT_NUMBER_AND_INCR (j, p);
  1397.           *++stackp = p + j;
  1398.       if (is_a_succeed_n)
  1399.             EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  1400.       continue;
  1401.  
  1402.     case succeed_n:
  1403.       is_a_succeed_n = 1;
  1404.           /* Get to the number of times to succeed.  */
  1405.           p += 2;        
  1406.       /* Increment p past the n for when k != 0.  */
  1407.           EXTRACT_NUMBER_AND_INCR (k, p);
  1408.           if (k == 0)
  1409.         {
  1410.               p -= 4;
  1411.               goto handle_on_failure_jump;
  1412.             }
  1413.           continue;
  1414.           
  1415.     case set_number_at:
  1416.           p += 4;
  1417.           continue;
  1418.  
  1419.         case start_memory:
  1420.     case stop_memory:
  1421.       p++;
  1422.       continue;
  1423.  
  1424.     case duplicate:
  1425.       bufp->can_be_null = 1;
  1426.       fastmap['\n'] = 1;
  1427.     case anychar:
  1428.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1429.         if (j != '\n')
  1430.           fastmap[j] = 1;
  1431.       if (bufp->can_be_null)
  1432.         return;
  1433.       /* Don't return; check the alternative paths
  1434.          so we can set can_be_null if appropriate.  */
  1435.       break;
  1436.  
  1437.     case wordchar:
  1438.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1439.         if (SYNTAX (j) == Sword)
  1440.           fastmap[j] = 1;
  1441.       break;
  1442.  
  1443.     case notwordchar:
  1444.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1445.         if (SYNTAX (j) != Sword)
  1446.           fastmap[j] = 1;
  1447.       break;
  1448.  
  1449. #ifdef emacs
  1450.     case syntaxspec:
  1451.       k = *p++;
  1452.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1453.         if (SYNTAX (j) == (enum syntaxcode) k)
  1454.           fastmap[j] = 1;
  1455.       break;
  1456.  
  1457.     case notsyntaxspec:
  1458.       k = *p++;
  1459.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1460.         if (SYNTAX (j) != (enum syntaxcode) k)
  1461.           fastmap[j] = 1;
  1462.       break;
  1463. #endif /* not emacs */
  1464.  
  1465.     case charset:
  1466.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1467.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  1468.           {
  1469.         if (translate)
  1470.           fastmap[translate[j]] = 1;
  1471.         else
  1472.           fastmap[j] = 1;
  1473.           }
  1474.       break;
  1475.  
  1476.     case charset_not:
  1477.       /* Chars beyond end of map must be allowed */
  1478.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  1479.         if (translate)
  1480.           fastmap[translate[j]] = 1;
  1481.         else
  1482.           fastmap[j] = 1;
  1483.  
  1484.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1485.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  1486.           {
  1487.         if (translate)
  1488.           fastmap[translate[j]] = 1;
  1489.         else
  1490.           fastmap[j] = 1;
  1491.           }
  1492.       break;
  1493.     }
  1494.  
  1495.       /* Get here means we have successfully found the possible starting
  1496.          characters of one path of the pattern.  We need not follow this
  1497.          path any farther.  Instead, look at the next alternative
  1498.          remembered in the stack.  */
  1499.    if (stackp != stackb)
  1500.     p = *stackp--;
  1501.       else
  1502.     break;
  1503.     }
  1504. }
  1505.