home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / g / gina15.zip / basic / regex.C < prev   
C/C++ Source or Header  |  1992-02-27  |  49KB  |  1,803 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. //   Module      : regex.C   Version 1.2
  4. //   LastSCCS    : 1/14/92  15:17:42
  5. //   LastEdit    : "Fri Nov 29 12:58:53 1991"
  6. //   Description : 
  7. //   Author      : 
  8. //   Copyright   : GMD Schloss Birlinghoven
  9.  
  10. /* Extended regular expression matching and search.
  11.    Copyright (C) 1985 Free Software Foundation, Inc.
  12.  
  13.                NO WARRANTY
  14.  
  15.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  16. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  17. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  18. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  19. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  20. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  21. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  22. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  23. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  24. CORRECTION.
  25.  
  26.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  27. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  28. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  29. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  30. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  31. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  32. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  33. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  34. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  35. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  36.  
  37.         GENERAL PUBLIC LICENSE TO COPY
  38.  
  39.   1. You may copy and distribute verbatim copies of this source file
  40. as you receive it, in any medium, provided that you conspicuously and
  41. appropriately publish on each copy a valid copyright notice "Copyright
  42. (C) 1985 Free Software Foundation, Inc."; and include following the
  43. copyright notice a verbatim copy of the above disclaimer of warranty
  44. and of this License.  You may charge a distribution fee for the
  45. physical act of transferring a copy.
  46.  
  47.   2. You may modify your copy or copies of this source file or
  48. any portion of it, and copy and distribute such modifications under
  49. the terms of Paragraph 1 above, provided that you also do the following:
  50.  
  51.     a) cause the modified files to carry prominent notices stating
  52.     that you changed the files and the date of any change; and
  53.  
  54.     b) cause the whole of any work that you distribute or publish,
  55.     that in whole or in part contains or is a derivative of this
  56.     program or any part thereof, to be licensed at no charge to all
  57.     third parties on terms identical to those contained in this
  58.     License Agreement (except that you may choose to grant more extensive
  59.     warranty protection to some or all third parties, at your option).
  60.  
  61.     c) You may charge a distribution fee for the physical act of
  62.     transferring a copy, and you may at your option offer warranty
  63.     protection in exchange for a fee.
  64.  
  65. Mere aggregation of another unrelated program with this program (or its
  66. derivative) on a volume of a storage or distribution medium does not bring
  67. the other program under the scope of these terms.
  68.  
  69.   3. You may copy and distribute this program (or a portion or derivative
  70. of it, under Paragraph 2) in object code or executable form under the terms
  71. of Paragraphs 1 and 2 above provided that you also do one of the following:
  72.  
  73.     a) accompany it with the complete corresponding machine-readable
  74.     source code, which must be distributed under the terms of
  75.     Paragraphs 1 and 2 above; or,
  76.  
  77.     b) accompany it with a written offer, valid for at least three
  78.     years, to give any third party free (except for a nominal
  79.     shipping charge) a complete machine-readable copy of the
  80.     corresponding source code, to be distributed under the terms of
  81.     Paragraphs 1 and 2 above; or,
  82.  
  83.     c) accompany it with the information you received as to where the
  84.     corresponding source code may be obtained.  (This alternative is
  85.     allowed only for noncommercial distribution and only if you
  86.     received the program in object code or executable form alone.)
  87.  
  88. For an executable file, complete source code means all the source code for
  89. all modules it contains; but, as a special exception, it need not include
  90. source code for modules which are standard libraries that accompany the
  91. operating system on which the executable file runs.
  92.  
  93.   4. You may not copy, sublicense, distribute or transfer this program
  94. except as expressly provided under this License Agreement.  Any attempt
  95. otherwise to copy, sublicense, distribute or transfer this program is void and
  96. your rights to use the program under this License agreement shall be
  97. automatically terminated.  However, parties who have received computer
  98. software programs from you with this License Agreement will not have
  99. their licenses terminated so long as such parties remain in full compliance.
  100.  
  101.   5. If you wish to incorporate parts of this program into other free
  102. programs whose distribution conditions are different, write to the Free
  103. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  104. worked out a simple rule that can be stated here, but we will often permit
  105. this.  We will be guided by the two goals of preserving the free status of
  106. all derivatives of our free software and of promoting the sharing and reuse of
  107. software.
  108.  
  109.  
  110. In other words, you are welcome to use, share and improve this program.
  111. You are forbidden to forbid anyone else to use, share and improve
  112. what you give them.   Help stamp out software-hoarding!  */
  113.  
  114.  
  115. /* To test, compile with -Dtest.
  116.  This Dtestable feature turns this into a self-contained program
  117.  which reads a pattern, describes how it compiles,
  118.  then reads a string and searches for it.  */
  119.  
  120. #ifdef emacs
  121.  
  122. /* The `emacs' switch turns on certain special matching commands
  123.  that make sense only in emacs. */
  124.  
  125. #ifdef __GNUG__
  126. #pragma implementation
  127. #else
  128. # define volatile
  129. # define signed
  130. #endif
  131.  
  132. #include "config.h"
  133. #include "lisp.h"
  134. #include "buffer.h"
  135. #include "syntax.h"
  136.  
  137. #else  /* not emacs */
  138.  
  139. //#ifndef __GNUG__
  140. //#  include <libc.h>
  141. //#  include <osfcn.h>
  142. //#  include <strings.h>
  143. //#else __GNUG__
  144. //#  include <std.h>
  145. //#endif
  146.  
  147. //#include <malloc.h>
  148.  
  149. /*
  150.  * Define the syntax stuff, so we can do the \<...\> things.
  151.  */
  152.  
  153. #ifndef Sword /* must be non-zero in some of the tests below... */
  154. #define Sword 1
  155. #endif
  156.  
  157. #define SYNTAX(c) re_syntax_table[c]
  158.  
  159. #ifdef SYNTAX_TABLE
  160.  
  161. char *re_syntax_table;
  162.  
  163. #else
  164.  
  165. static char re_syntax_table[256];
  166.  
  167. static void
  168. init_syntax_once (void)
  169. {
  170.    register int c;
  171.    static int done = 0;
  172.  
  173.    if (done)
  174.      return;
  175.  
  176.    bzero (re_syntax_table, sizeof re_syntax_table);
  177.  
  178.    for (c = 'a'; c <= 'z'; c++)
  179.      re_syntax_table[c] = Sword;
  180.  
  181.    for (c = 'A'; c <= 'Z'; c++)
  182.      re_syntax_table[c] = Sword;
  183.  
  184.    for (c = '0'; c <= '9'; c++)
  185.      re_syntax_table[c] = Sword;
  186.  
  187.    done = 1;
  188. }
  189.  
  190. #endif /* SYNTAX_TABLE */
  191. #endif /* not emacs */
  192.  
  193. //#include "regex.h"
  194.  
  195. /* Number of failure points to allocate space for initially,
  196.  when matching.  If this number is exceeded, more space is allocated,
  197.  so it is not a hard limit.  */
  198.  
  199. #ifndef NFAILURES
  200. #define NFAILURES 80
  201. #endif /* NFAILURES */
  202.  
  203. /* width of a byte in bits */
  204.  
  205. #define BYTEWIDTH 8
  206.  
  207. #ifndef SIGN_EXTEND_CHAR
  208. #define SIGN_EXTEND_CHAR(x) (x)
  209. #endif
  210.  
  211. static int obscure_syntax = 0;
  212.  
  213. /* Specify the precise syntax of regexp for compilation.
  214.    This provides for compatibility for various utilities
  215.    which historically have different, incompatible syntaxes.
  216.  
  217.    The argument SYNTAX is a bit-mask containing the two bits
  218.    RE_NO_BK_PARENS and RE_NO_BK_VBAR.  */
  219.  
  220. int
  221. re_set_syntax (int syntax)
  222. {
  223.   int ret;
  224.  
  225.   ret = obscure_syntax;
  226.   obscure_syntax = syntax;
  227.   return ret;
  228. }
  229.  
  230. /* re_compile_pattern takes a regular-expression string
  231.    and converts it into a buffer full of byte commands for matching.
  232.  
  233.   PATTERN   is the address of the pattern string
  234.   SIZE      is the length of it.
  235.   BUFP        is a  struct re_pattern_buffer *  which points to the info
  236.         on where to store the byte commands.
  237.         This structure contains a  char *  which points to the
  238.         actual space, which should have been obtained with malloc.
  239.         re_compile_pattern may use  realloc  to grow the buffer space.
  240.  
  241.   The number of bytes of commands can be found out by looking in
  242.   the  struct re_pattern_buffer  that bufp pointed to,
  243.   after re_compile_pattern returns.
  244. */
  245.  
  246. #define PATPUSH(ch) (*b++ = (char) (ch))
  247.  
  248. #define PATFETCH(c) \
  249.  {if (p == pend) goto end_of_pattern; \
  250.   c = * (unsigned char *) p++; \
  251.   if (translate) c = translate[c]; }
  252.  
  253. #define PATFETCH_RAW(c) \
  254.  {if (p == pend) goto end_of_pattern; \
  255.   c = * (unsigned char *) p++; }
  256.  
  257. #define PATUNFETCH p--
  258.  
  259. #define EXTEND_BUFFER \
  260.   { char *old_buffer = bufp->buffer; \
  261.     if (bufp->allocated == (1<<16)) goto too_big; \
  262.     bufp->allocated *= 2; \
  263.     if (bufp->allocated > (1<<16)) bufp->allocated = (1<<16); \
  264.     if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
  265.       goto memory_exhausted; \
  266.     c = bufp->buffer - old_buffer; \
  267.     b += c; \
  268.     if (fixup_jump) \
  269.       fixup_jump += c; \
  270.     if (laststart) \
  271.       laststart += c; \
  272.     begalt += c; \
  273.     if (pending_exact) \
  274.       pending_exact += c; \
  275.   }
  276.  
  277. static void store_jump (char *, char, char *);
  278. static void insert_jump (char, char *, char *, char *);
  279. int re_match_2 (struct re_pattern_buffer *, unsigned char *, int, unsigned char *, 
  280.             int, int, struct re_registers *, int mstop);
  281.  
  282. char *
  283. re_compile_pattern (char *pattern, int size, struct re_pattern_buffer *bufp)
  284. {
  285.   register char *b = bufp->buffer;
  286.   register char *p = pattern;
  287.   char *pend = pattern + size;
  288.   register unsigned c, c1;
  289.   char *p1;
  290.   unsigned char *translate = (unsigned char *) bufp->translate;
  291.  
  292.   /* address of the count-byte of the most recently inserted "exactn" command.
  293.     This makes it possible to tell whether a new exact-match character
  294.     can be added to that command or requires a new "exactn" command. */
  295.      
  296.   char *pending_exact = 0;
  297.  
  298.   /* address of the place where a forward-jump should go
  299.     to the end of the containing expression.
  300.     Each alternative of an "or", except the last, ends with a forward-jump
  301.     of this sort. */
  302.  
  303.   char *fixup_jump = 0;
  304.  
  305.   /* address of start of the most recently finished expression.
  306.     This tells postfix * where to find the start of its operand. */
  307.  
  308.   char *laststart = 0;
  309.  
  310.   /* In processing a repeat, 1 means zero matches is allowed */
  311.  
  312.   char zero_times_ok;
  313.  
  314.   /* In processing a repeat, 1 means many matches is allowed */
  315.  
  316.   char many_times_ok;
  317.  
  318.   /* address of beginning of regexp, or inside of last \( */
  319.  
  320.   char *begalt = b;
  321.  
  322.   /* Stack of information saved by \( and restored by \).
  323.      Four stack elements are pushed by each \(:
  324.        First, the value of b.
  325.        Second, the value of fixup_jump.
  326.        Third, the value of regnum.
  327.        Fourth, the value of begalt.  */
  328.  
  329.   int stackb[40];
  330.   int *stackp = stackb;
  331.   int *stacke = stackb + 40;
  332.   int *stackt;
  333.  
  334.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  335.      where it becomes the "register number" to put in the stop_memory command */
  336.  
  337.   int regnum = 1;
  338.  
  339.   bufp->fastmap_accurate = 0;
  340.  
  341. #ifndef emacs
  342. #ifndef SYNTAX_TABLE
  343.   /*
  344.    * Initialize the syntax table.
  345.    */
  346.    init_syntax_once();
  347. #endif
  348. #endif
  349.  
  350.   if (bufp->allocated == 0)
  351.     {
  352.       bufp->allocated = 28;
  353.       if (bufp->buffer)
  354.     /* EXTEND_BUFFER loses when bufp->allocated is 0 */
  355.     bufp->buffer = (char *) realloc (bufp->buffer, 28);
  356.       else
  357.     /* Caller did not allocate a buffer.  Do it for him */
  358.     bufp->buffer = (char *) new char[28];
  359.       if (!bufp->buffer) goto memory_exhausted;
  360.       begalt = b = bufp->buffer;
  361.     }
  362.  
  363.   while (p != pend)
  364.     {
  365.       if (b - bufp->buffer > bufp->allocated - 10)
  366.     /* Note that EXTEND_BUFFER clobbers c */
  367.     EXTEND_BUFFER;
  368.  
  369.       PATFETCH (c);
  370.  
  371.       switch (c)
  372.     {
  373.     case '$':
  374.       if (obscure_syntax & RE_TIGHT_VBAR)
  375.         {
  376.           if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p != pend)
  377.         goto normal_char;
  378.           /* Make operand of last vbar end before this `$'.  */
  379.           if (fixup_jump)
  380.         store_jump (fixup_jump, jump, b);
  381.           fixup_jump = 0;
  382.           PATPUSH (endline);
  383.           break;
  384.         }
  385.  
  386.       /* $ means succeed if at end of line, but only in special contexts.
  387.         If randomly in the middle of a pattern, it is a normal character. */
  388.       if (p == pend || *p == '\n'
  389.           || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  390.           || (obscure_syntax & RE_NO_BK_PARENS
  391.           ? *p == ')'
  392.           : *p == '\\' && p[1] == ')')
  393.           || (obscure_syntax & RE_NO_BK_VBAR
  394.           ? *p == '|'
  395.           : *p == '\\' && p[1] == '|'))
  396.         {
  397.           PATPUSH (endline);
  398.           break;
  399.         }
  400.       goto normal_char;
  401.  
  402.     case '^':
  403.       /* ^ means succeed if at beg of line, but only if no preceding pattern. */
  404.  
  405.       if (laststart && p[-2] != '\n'
  406.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  407.         goto normal_char;
  408.       if (obscure_syntax & RE_TIGHT_VBAR)
  409.         {
  410.           if (p != pattern + 1
  411.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  412.         goto normal_char;
  413.           PATPUSH (begline);
  414.           begalt = b;
  415.         }
  416.       else
  417.         PATPUSH (begline);
  418.       break;
  419.  
  420.     case '+':
  421.     case '?':
  422.       if (obscure_syntax & RE_BK_PLUS_QM)
  423.         goto normal_char;
  424.     handle_plus:
  425.     case '*':
  426.       /* If there is no previous pattern, char not special. */
  427.       if (!laststart && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  428.         goto normal_char;
  429.       /* If there is a sequence of repetition chars,
  430.          collapse it down to equivalent to just one.  */
  431.       zero_times_ok = 0;
  432.       many_times_ok = 0;
  433.       while (1)
  434.         {
  435.           zero_times_ok |= c != '+';
  436.           many_times_ok |= c != '?';
  437.           if (p == pend)
  438.         break;
  439.           PATFETCH (c);
  440.           if (c == '*')
  441.         ;
  442.           else if (!(obscure_syntax & RE_BK_PLUS_QM)
  443.                && (c == '+' || c == '?'))
  444.         ;
  445.           else if ((obscure_syntax & RE_BK_PLUS_QM)
  446.                && c == '\\')
  447.         {
  448.           int c1;
  449.           PATFETCH (c1);
  450.           if (!(c1 == '+' || c1 == '?'))
  451.             {
  452.               PATUNFETCH;
  453.               PATUNFETCH;
  454.               break;
  455.             }
  456.           c = c1;
  457.         }
  458.           else
  459.         {
  460.           PATUNFETCH;
  461.           break;
  462.         }
  463.         }
  464.  
  465.       /* Star, etc. applied to an empty pattern is equivalent
  466.          to an empty pattern.  */
  467.       if (!laststart)
  468.         break;
  469.  
  470.       /* Now we know whether 0 matches is allowed,
  471.          and whether 2 or more matches is allowed.  */
  472.       if (many_times_ok)
  473.         {
  474.           /* If more than one repetition is allowed,
  475.          put in a backward jump at the end.  */
  476.           store_jump (b, maybe_finalize_jump, laststart - 3);
  477.           b += 3;
  478.         }
  479.       insert_jump (on_failure_jump, laststart, b + 3, b);
  480.       pending_exact = 0;
  481.       b += 3;
  482.       if (!zero_times_ok)
  483.         {
  484.           /* At least one repetition required: insert before the loop
  485.          a skip over the initial on-failure-jump instruction */
  486.           insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  487.           b += 3;
  488.         }
  489.       break;
  490.  
  491.     case '.':
  492.       laststart = b;
  493.       PATPUSH (anychar);
  494.       break;
  495.  
  496.     case '[':
  497.       while (b - bufp->buffer
  498.          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  499.         /* Note that EXTEND_BUFFER clobbers c */
  500.         EXTEND_BUFFER;
  501.  
  502.       laststart = b;
  503.       if (*p == '^')
  504.         PATPUSH (charset_not), p++;
  505.       else
  506.         PATPUSH (charset);
  507.       p1 = p;
  508.  
  509.       PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  510.       /* Clear the whole map */
  511.       bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  512.       /* Read in characters and ranges, setting map bits */
  513.       while (1)
  514.         {
  515.           PATFETCH (c);
  516.           if (c == ']' && p != p1 + 1) break;
  517.           if (*p == '-' && p[1] != ']')
  518.         {
  519.           PATFETCH (c1);
  520.           PATFETCH (c1);
  521.           while (c <= c1)
  522.             b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH), c++;
  523.         }
  524.           else
  525.         {
  526.           b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH);
  527.         }
  528.         }
  529.       /* Discard any bitmap bytes that are all 0 at the end of the map.
  530.          Decrement the map-length byte too. */
  531.       while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  532.         b[-1]--;
  533.       b += b[-1];
  534.       break;
  535.  
  536.     case '(':
  537.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  538.         goto normal_char;
  539.       else
  540.         goto handle_open;
  541.  
  542.     case ')':
  543.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  544.         goto normal_char;
  545.       else
  546.         goto handle_close;
  547.  
  548.     case '\n':
  549.       if (! (obscure_syntax & RE_NEWLINE_OR))
  550.         goto normal_char;
  551.       else
  552.         goto handle_bar;
  553.  
  554.     case '|':
  555.       if (! (obscure_syntax & RE_NO_BK_VBAR))
  556.         goto normal_char;
  557.       else
  558.         goto handle_bar;
  559.  
  560.         case '\\':
  561.       if (p == pend) goto invalid_pattern;
  562.       PATFETCH_RAW (c);
  563.       switch (c)
  564.         {
  565.         case '(':
  566.           if (obscure_syntax & RE_NO_BK_PARENS)
  567.         goto normal_backsl;
  568.         handle_open:
  569.           if (stackp == stacke) goto nesting_too_deep;
  570.           if (regnum < RE_NREGS)
  571.             {
  572.           PATPUSH (start_memory);
  573.           PATPUSH (regnum);
  574.             }
  575.           *stackp++ = b - bufp->buffer;
  576.           *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  577.           *stackp++ = regnum++;
  578.           *stackp++ = begalt - bufp->buffer;
  579.           fixup_jump = 0;
  580.           laststart = 0;
  581.           begalt = b;
  582.           break;
  583.  
  584.         case ')':
  585.           if (obscure_syntax & RE_NO_BK_PARENS)
  586.         goto normal_backsl;
  587.         handle_close:
  588.           if (stackp == stackb) goto unmatched_close;
  589.           begalt = *--stackp + bufp->buffer;
  590.           if (fixup_jump)
  591.         store_jump (fixup_jump, jump, b);
  592.           if (stackp[-1] < RE_NREGS)
  593.         {
  594.           PATPUSH (stop_memory);
  595.           PATPUSH (stackp[-1]);
  596.         }
  597.           stackp -= 2;
  598.           fixup_jump = 0;
  599.           if (*stackp)
  600.         fixup_jump = *stackp + bufp->buffer - 1;
  601.           laststart = *--stackp + bufp->buffer;
  602.           break;
  603.  
  604.         case '|':
  605.           if (obscure_syntax & RE_NO_BK_VBAR)
  606.         goto normal_backsl;
  607.         handle_bar:
  608.           insert_jump (on_failure_jump, begalt, b + 6, b);
  609.           pending_exact = 0;
  610.           b += 3;
  611.           if (fixup_jump)
  612.         store_jump (fixup_jump, jump, b);
  613.           fixup_jump = b;
  614.           b += 3;
  615.           laststart = 0;
  616.           begalt = b;
  617.           break;
  618.  
  619. #ifdef emacs
  620.         case '=':
  621.           PATPUSH (at_dot);
  622.           break;
  623.  
  624.         case 's':    
  625.           laststart = b;
  626.           PATPUSH (syntaxspec);
  627.           PATFETCH (c);
  628.           PATPUSH (syntax_spec_code[c]);
  629.           break;
  630.  
  631.         case 'S':
  632.           laststart = b;
  633.           PATPUSH (notsyntaxspec);
  634.           PATFETCH (c);
  635.           PATPUSH (syntax_spec_code[c]);
  636.           break;
  637. #endif /* emacs */
  638.  
  639.         case 'w':
  640.           laststart = b;
  641.           PATPUSH (wordchar);
  642.           break;
  643.  
  644.         case 'W':
  645.           laststart = b;
  646.           PATPUSH (notwordchar);
  647.           break;
  648.  
  649.         case '<':
  650.           PATPUSH (wordbeg);
  651.           break;
  652.  
  653.         case '>':
  654.           PATPUSH (wordend);
  655.           break;
  656.  
  657.         case 'b':
  658.           PATPUSH (wordbound);
  659.           break;
  660.  
  661.         case 'B':
  662.           PATPUSH (notwordbound);
  663.           break;
  664.  
  665.         case '`':
  666.           PATPUSH (begbuf);
  667.           break;
  668.  
  669.         case '\'':
  670.           PATPUSH (endbuf);
  671.           break;
  672.  
  673.         case '1':
  674.         case '2':
  675.         case '3':
  676.         case '4':
  677.         case '5':
  678.         case '6':
  679.         case '7':
  680.         case '8':
  681.         case '9':
  682.           c1 = c - '0';
  683.           if (c1 >= regnum)
  684.         goto normal_char;
  685.           for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  686.          if (*stackt == c1)
  687.           goto normal_char;
  688.           laststart = b;
  689.           PATPUSH (duplicate);
  690.           PATPUSH (c1);
  691.           break;
  692.  
  693.         case '+':
  694.         case '?':
  695.           if (obscure_syntax & RE_BK_PLUS_QM)
  696.         goto handle_plus;
  697.  
  698.         default:
  699.         normal_backsl:
  700.           /* You might think it would be useful for \ to mean
  701.          not to translate; but if we don't translate it
  702.          it will never match anything.  */
  703.           if (translate) c = translate[c];
  704.           goto normal_char;
  705.         }
  706.       break;
  707.  
  708.     default:
  709.     normal_char:
  710.       if (!pending_exact || pending_exact + *pending_exact + 1 != b
  711.           || *pending_exact == 0177 || *p == '*' || *p == '^'
  712.           || ((obscure_syntax & RE_BK_PLUS_QM)
  713.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  714.           : (*p == '+' || *p == '?')))
  715.         {
  716.           laststart = b;
  717.           PATPUSH (exactn);
  718.           pending_exact = b;
  719.           PATPUSH (0);
  720.         }
  721.       PATPUSH (c);
  722.       (*pending_exact)++;
  723.     }
  724.     }
  725.  
  726.   if (fixup_jump)
  727.     store_jump (fixup_jump, jump, b);
  728.  
  729.   if (stackp != stackb) goto unmatched_open;
  730.  
  731.   bufp->used = b - bufp->buffer;
  732.   return 0;
  733.  
  734.  invalid_pattern:
  735.   return "Invalid regular expression";
  736.  
  737.  unmatched_open:
  738.   return "Unmatched \\(";
  739.  
  740.  unmatched_close:
  741.   return "Unmatched \\)";
  742.  
  743.  end_of_pattern:
  744.   return "Premature end of regular expression";
  745.  
  746.  nesting_too_deep:
  747.   return "Nesting too deep";
  748.  
  749.  too_big:
  750.   return "Regular expression too big";
  751.  
  752.  memory_exhausted:
  753.   return "Memory exhausted";
  754. }
  755.  
  756. /* Store where `from' points a jump operation to jump to where `to' points.
  757.   `opcode' is the opcode to store. */
  758.  
  759. static void
  760. store_jump (char *from, char opcode, char *to)
  761. {
  762.   from[0] = opcode;
  763.   from[1] = (to - (from + 3)) & 0377;
  764.   from[2] = (to - (from + 3)) >> 8;
  765. }
  766.  
  767. /* Open up space at char FROM, and insert there a jump to TO.
  768.    CURRENT_END gives te end of the storage no in use,
  769.    so we know how much data to copy up.
  770.    OP is the opcode of the jump to insert.
  771.  
  772.    If you call this function, you must zero out pending_exact.  */
  773.  
  774. static void
  775. insert_jump (char op, char *from, char *to, char *current_end)
  776. {
  777.   register char *pto = current_end + 3;
  778.   register char *pfrom = current_end;
  779.   while (pfrom != from)
  780.     *--pto = *--pfrom;
  781.   store_jump (from, op, to);
  782. }
  783.  
  784. /* Given a pattern, compute a fastmap from it.
  785.  The fastmap records which of the (1 << BYTEWIDTH) possible characters
  786.  can start a string that matches the pattern.
  787.  This fastmap is used by re_search to skip quickly over totally implausible text.
  788.  
  789.  The caller must supply the address of a (1 << BYTEWIDTH)-byte data area
  790.  as bufp->fastmap.
  791.  The other components of bufp describe the pattern to be used.  */
  792.  
  793. void
  794. re_compile_fastmap (struct re_pattern_buffer *bufp)
  795. {
  796.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  797.   int size = bufp->used;
  798.   register char *fastmap = bufp->fastmap;
  799.   register unsigned char *p = pattern;
  800.   register unsigned char *pend = pattern + size;
  801.   register int j;
  802.   unsigned char *translate = (unsigned char *) bufp->translate;
  803.  
  804.   unsigned char *stackb[NFAILURES];
  805.   unsigned char **stackp = stackb;
  806.  
  807.   bzero (fastmap, (1 << BYTEWIDTH));
  808.   bufp->fastmap_accurate = 1;
  809.   bufp->can_be_null = 0;
  810.       
  811.   while (p)
  812.     {
  813.       if (p == pend)
  814.     {
  815.       bufp->can_be_null = 1;
  816.       break;
  817.     }
  818. #ifdef SWITCH_ENUM_BUG
  819.       switch ((int) ((enum regexpcode) *p++))
  820. #else
  821.       switch ((enum regexpcode) *p++)
  822. #endif
  823.     {
  824.     case exactn:
  825.       if (translate)
  826.         fastmap[translate[p[1]]] = 1;
  827.       else
  828.         fastmap[p[1]] = 1;
  829.       break;
  830.  
  831.         case begline:
  832.         case before_dot:
  833.     case at_dot:
  834.     case after_dot:
  835.     case begbuf:
  836.     case endbuf:
  837.     case wordbound:
  838.     case notwordbound:
  839.     case wordbeg:
  840.     case wordend:
  841.       continue;
  842.  
  843.     case endline:
  844.       if (translate)
  845.         fastmap[translate['\n']] = 1;
  846.       else
  847.         fastmap['\n'] = 1;
  848.       if (bufp->can_be_null != 1)
  849.         bufp->can_be_null = 2;
  850.       break;
  851.  
  852.     case finalize_jump:
  853.     case maybe_finalize_jump:
  854.     case jump:
  855.     case dummy_failure_jump:
  856.       bufp->can_be_null = 1;
  857.       j = *p++ & 0377;
  858.       j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  859.       p += j + 1;        /* The 1 compensates for missing ++ above */
  860.       if (j > 0)
  861.         continue;
  862.       /* Jump backward reached implies we just went through
  863.          the body of a loop and matched nothing.
  864.          Opcode jumped to should be an on_failure_jump.
  865.          Just treat it like an ordinary jump.
  866.          For a * loop, it has pushed its failure point already;
  867.          if so, discard that as redundant.  */
  868.       if ((enum regexpcode) *p != on_failure_jump)
  869.         continue;
  870.       p++;
  871.       j = *p++ & 0377;
  872.       j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  873.       p += j + 1;        /* The 1 compensates for missing ++ above */
  874.       if (stackp != stackb && *stackp == p)
  875.         stackp--;
  876.       continue;
  877.       
  878.     case on_failure_jump:
  879.       j = *p++ & 0377;
  880.       j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  881.       p++;
  882.       *++stackp = p + j;
  883.       continue;
  884.  
  885.     case start_memory:
  886.     case stop_memory:
  887.       p++;
  888.       continue;
  889.  
  890.     case duplicate:
  891.       bufp->can_be_null = 1;
  892.       fastmap['\n'] = 1;
  893.     case anychar:
  894.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  895.         if (j != '\n')
  896.           fastmap[j] = 1;
  897.       if (bufp->can_be_null)
  898.         return;
  899.       /* Don't return; check the alternative paths
  900.          so we can set can_be_null if appropriate.  */
  901.       break;
  902.  
  903.     case wordchar:
  904.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  905.         if (SYNTAX (j) == Sword)
  906.           fastmap[j] = 1;
  907.       break;
  908.  
  909.     case notwordchar:
  910.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  911.         if (SYNTAX (j) != Sword)
  912.           fastmap[j] = 1;
  913.       break;
  914.  
  915. #ifdef emacs
  916.     case syntaxspec:
  917.       k = *p++;
  918.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  919.         if (SYNTAX (j) == (enum syntaxcode) k)
  920.           fastmap[j] = 1;
  921.       break;
  922.  
  923.     case notsyntaxspec:
  924.       k = *p++;
  925.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  926.         if (SYNTAX (j) != (enum syntaxcode) k)
  927.           fastmap[j] = 1;
  928.       break;
  929. #endif /* emacs */
  930.  
  931.     case charset:
  932.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  933.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  934.           {
  935.         if (translate)
  936.           fastmap[translate[j]] = 1;
  937.         else
  938.           fastmap[j] = 1;
  939.           }
  940.       break;
  941.  
  942.     case charset_not:
  943.       /* Chars beyond end of map must be allowed */
  944.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  945.         if (translate)
  946.           fastmap[translate[j]] = 1;
  947.         else
  948.           fastmap[j] = 1;
  949.  
  950.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  951.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  952.           {
  953.         if (translate)
  954.           fastmap[translate[j]] = 1;
  955.         else
  956.           fastmap[j] = 1;
  957.           }
  958.       break;
  959.       }
  960.  
  961.       /* Get here means we have successfully found the possible starting characters
  962.      of one path of the pattern.  We need not follow this path any farther.
  963.      Instead, look at the next alternative remembered in the stack. */
  964.       if (stackp != stackb)
  965.     p = *stackp--;
  966.       else
  967.     break;
  968.   }
  969. }
  970.  
  971. /* Like re_search_2, below, but only one string is specified. */
  972.  
  973. int
  974. re_search (struct re_pattern_buffer *pbufp, char *string, int size, 
  975.            int startpos, int range, struct re_registers *regs)
  976. {
  977.   return re_search_2 (pbufp, 0, 0, string, size, startpos, range, regs, size);
  978. }
  979.  
  980. /* Like re_match_2 but tries first a match starting at index STARTPOS,
  981.    then at STARTPOS + 1, and so on.
  982.    RANGE is the number of places to try before giving up.
  983.    If RANGE is negative, the starting positions tried are
  984.     STARTPOS, STARTPOS - 1, etc.
  985.    It is up to the caller to make sure that range is not so large
  986.    as to take the starting position outside of the input strings.
  987.  
  988. The value returned is the position at which the match was found,
  989.  or -1 if no match was found,
  990.  or -2 if error (such as failure stack overflow).  */
  991.  
  992. int
  993. re_search_2 (struct re_pattern_buffer *pbufp, char *string1, int size1, char *string2, 
  994.              int size2, int startpos, register int range, struct re_registers *regs, 
  995.              int mstop)
  996. {
  997.   register char *fastmap = pbufp->fastmap;
  998.   register unsigned char *translate = (unsigned char *) pbufp->translate;
  999.   int total = size1 + size2;
  1000.   int val;
  1001.  
  1002.   /* Update the fastmap now if not correct already */
  1003.   if (fastmap && !pbufp->fastmap_accurate)
  1004.     re_compile_fastmap (pbufp);
  1005.   
  1006.   /* Don't waste time in a long search for a pattern
  1007.      that says it is anchored.  */
  1008.   if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  1009.       && range > 0)
  1010.     {
  1011.       if (startpos > 0)
  1012.     return -1;
  1013.       else
  1014.     range = 1;
  1015.     }
  1016.  
  1017.   while (1)
  1018.     {
  1019.       /* If a fastmap is supplied, skip quickly over characters
  1020.      that cannot possibly be the start of a match.
  1021.      Note, however, that if the pattern can possibly match
  1022.      the null string, we must test it at each starting point
  1023.      so that we take the first null string we get.  */
  1024.  
  1025.       if (fastmap && startpos < total && pbufp->can_be_null != 1)
  1026.     {
  1027.       if (range > 0)
  1028.         {
  1029.           register int lim = 0;
  1030.           register unsigned char *p;
  1031.           int irange = range;
  1032.           if (startpos < size1 && startpos + range >= size1)
  1033.         lim = range - (size1 - startpos);
  1034.  
  1035.           p = ((unsigned char *)
  1036.            &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1037.  
  1038.           if (translate)
  1039.         {
  1040.           while (range > lim && !fastmap[translate[*p++]])
  1041.             range--;
  1042.         }
  1043.           else
  1044.         {
  1045.           while (range > lim && !fastmap[*p++])
  1046.             range--;
  1047.         }
  1048.           startpos += irange - range;
  1049.         }
  1050.       else
  1051.         {
  1052.           register unsigned char c;
  1053.           if (startpos >= size1)
  1054.         c = string2[startpos - size1];
  1055.           else
  1056.         c = string1[startpos];
  1057.           c &= 0xff;
  1058.           if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1059.         goto advance;
  1060.         }
  1061.     }
  1062.  
  1063.       if (range >= 0 && startpos == total
  1064.       && fastmap && pbufp->can_be_null == 0)
  1065.     return -1;
  1066.  
  1067.       val = re_match_2 (pbufp, (unsigned char *) string1, size1, (unsigned char *) string2, size2, startpos, regs, mstop);
  1068.       if (0 <= val)
  1069.     {
  1070.       if (val == -2)
  1071.         return -2;
  1072.       return startpos;
  1073.     }
  1074.  
  1075. #ifdef C_ALLOCA
  1076.       alloca (0);
  1077. #endif /* C_ALLOCA */
  1078.  
  1079.     advance:
  1080.       if (!range) break;
  1081.       if (range > 0) range--, startpos++; else range++, startpos--;
  1082.     }
  1083.   return -1;
  1084. }
  1085.  
  1086. #ifndef emacs   /* emacs never uses this */
  1087. int
  1088. re_match (struct re_pattern_buffer *pbufp, char *string, int size, int pos, 
  1089.           struct re_registers *regs)
  1090. {
  1091.   return re_match_2 (pbufp, (unsigned char *) 0, 0, (unsigned char *) string, size, pos, regs, size);
  1092. }
  1093. #endif /* emacs */
  1094.  
  1095. /* Maximum size of failure stack.  Beyond this, overflow is an error.  */
  1096.  
  1097. int re_max_failures = 2000;
  1098.  
  1099. static int bcmp_translate(unsigned char *, unsigned char *, int, unsigned char *);
  1100.  
  1101. /* Match the pattern described by PBUFP
  1102.    against data which is the virtual concatenation of STRING1 and STRING2.
  1103.    SIZE1 and SIZE2 are the sizes of the two data strings.
  1104.    Start the match at position POS.
  1105.    Do not consider matching past the position MSTOP.
  1106.  
  1107.    If pbufp->fastmap is nonzero, then it had better be up to date.
  1108.  
  1109.    The reason that the data to match are specified as two components
  1110.    which are to be regarded as concatenated
  1111.    is so this function can be used directly on the contents of an Emacs buffer.
  1112.  
  1113.    -1 is returned if there is no match.  -2 is returned if there is
  1114.    an error (such as match stack overflow).  Otherwise the value is the length
  1115.    of the substring which was matched.  */
  1116.  
  1117. int
  1118. re_match_2 (struct re_pattern_buffer *pbufp, unsigned char *string1, int size1, 
  1119.             unsigned char *string2, int size2, int pos, struct re_registers *regs, 
  1120.             int mstop)
  1121. {
  1122.   register unsigned char *p = (unsigned char *) pbufp->buffer;
  1123.   register unsigned char *pend = p + pbufp->used;
  1124.   /* End of first string */
  1125.   unsigned char *end1;
  1126.   /* End of second string */
  1127.   unsigned char *end2;
  1128.   /* Pointer just past last char to consider matching */
  1129.   unsigned char *end_match_1, *end_match_2;
  1130.   register unsigned char *d, *dend;
  1131.   register int mcnt;
  1132.   unsigned char *translate = (unsigned char *) pbufp->translate;
  1133.  
  1134.  /* Failure point stack.  Each place that can handle a failure further down the line
  1135.     pushes a failure point on this stack.  It consists of two char *'s.
  1136.     The first one pushed is where to resume scanning the pattern;
  1137.     the second pushed is where to resume scanning the strings.
  1138.     If the latter is zero, the failure point is a "dummy".
  1139.     If a failure happens and the innermost failure point is dormant,
  1140.     it discards that failure point and tries the next one. */
  1141.  
  1142.   unsigned char *initial_stack[2 * NFAILURES];
  1143.   unsigned char **stackb = initial_stack;
  1144.   unsigned char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];
  1145.  
  1146.   /* Information on the "contents" of registers.
  1147.      These are pointers into the input strings; they record
  1148.      just what was matched (on this attempt) by some part of the pattern.
  1149.      The start_memory command stores the start of a register's contents
  1150.      and the stop_memory command stores the end.
  1151.  
  1152.      At that point, regstart[regnum] points to the first character in the register,
  1153.      regend[regnum] points to the first character beyond the end of the register,
  1154.      regstart_seg1[regnum] is true iff regstart[regnum] points into string1,
  1155.      and regend_seg1[regnum] is true iff regend[regnum] points into string1.  */
  1156.  
  1157.   unsigned char *regstart[RE_NREGS];
  1158.   unsigned char *regend[RE_NREGS];
  1159.   unsigned char regstart_seg1[RE_NREGS], regend_seg1[RE_NREGS];
  1160.  
  1161.   /* Set up pointers to ends of strings.
  1162.      Don't allow the second string to be empty unless both are empty.  */
  1163.   if (!size2)
  1164.     {
  1165.       string2 = string1;
  1166.       size2 = size1;
  1167.       string1 = 0;
  1168.       size1 = 0;
  1169.     }
  1170.   end1 = string1 + size1;
  1171.   end2 = string2 + size2;
  1172.  
  1173.   /* Compute where to stop matching, within the two strings */
  1174.   if (mstop <= size1)
  1175.     {
  1176.       end_match_1 = string1 + mstop;
  1177.       end_match_2 = string2;
  1178.     }
  1179.   else
  1180.     {
  1181.       end_match_1 = end1;
  1182.       end_match_2 = string2 + mstop - size1;
  1183.     }
  1184.  
  1185.   /* Initialize \) text positions to -1
  1186.      to mark ones that no \( or \) has been seen for.  */
  1187.  
  1188.   for (mcnt = 0; mcnt < sizeof (regend) / sizeof (*regend); mcnt++)
  1189.     regend[mcnt] = (unsigned char *) -1;
  1190.  
  1191.   /* `p' scans through the pattern as `d' scans through the data.
  1192.      `dend' is the end of the input string that `d' points within.
  1193.      `d' is advanced into the following input string whenever necessary,
  1194.      but this happens before fetching;
  1195.      therefore, at the beginning of the loop,
  1196.      `d' can be pointing at the end of a string,
  1197.      but it cannot equal string2.  */
  1198.  
  1199.   if (pos <= size1)
  1200.     d = string1 + pos, dend = end_match_1;
  1201.   else
  1202.     d = string2 + pos - size1, dend = end_match_2;
  1203.  
  1204. /* Write PREFETCH; just before fetching a character with *d.  */
  1205. #define PREFETCH \
  1206.  while (d == dend)                            \
  1207.   { if (dend == end_match_2) goto fail;  /* end of string2 => failure */   \
  1208.     d = string2;  /* end of string1 => advance to string2. */       \
  1209.     dend = end_match_2; }
  1210.  
  1211.   /* This loop loops over pattern commands.
  1212.      It exits by returning from the function if match is complete,
  1213.      or it drops through if match fails at this starting point in the input data. */
  1214.  
  1215.   while (1)
  1216.     {
  1217.       if (p == pend)
  1218.     /* End of pattern means we have succeeded! */
  1219.     {
  1220.       /* If caller wants register contents data back, convert it to indices */
  1221.       if (regs)
  1222.         {
  1223.            regs->start[0] = pos;
  1224.            if (dend == end_match_1)
  1225.          regs->end[0] = d - string1;
  1226.            else
  1227.          regs->end[0] = d - string2 + size1;
  1228.            for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  1229.         {
  1230.           if (regend[mcnt] == (unsigned char *) -1)
  1231.             {
  1232.               regs->start[mcnt] = -1;
  1233.               regs->end[mcnt] = -1;
  1234.               continue;
  1235.             }
  1236.            if (regstart_seg1[mcnt])
  1237.             regs->start[mcnt] = regstart[mcnt] - string1;
  1238.           else
  1239.             regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  1240.            if (regend_seg1[mcnt])
  1241.             regs->end[mcnt] = regend[mcnt] - string1;
  1242.           else
  1243.             regs->end[mcnt] = regend[mcnt] - string2 + size1;
  1244.         }
  1245.         }
  1246.        if (dend == end_match_1)
  1247.         return (d - string1 - pos);
  1248.       else
  1249.         return d - string2 + size1 - pos;
  1250.     }
  1251.  
  1252.       /* Otherwise match next pattern command */
  1253. #ifdef SWITCH_ENUM_BUG
  1254.       switch ((int) ((enum regexpcode) *p++))
  1255. #else
  1256.       switch ((enum regexpcode) *p++)
  1257. #endif
  1258.     {
  1259.  
  1260.     /* \( is represented by a start_memory, \) by a stop_memory.
  1261.         Both of those commands contain a "register number" argument.
  1262.         The text matched within the \( and \) is recorded under that number.
  1263.         Then, \<digit> turns into a `duplicate' command which
  1264.         is followed by the numeric value of <digit> as the register number. */
  1265.  
  1266.     case start_memory:
  1267.       regstart[*p] = d;
  1268.        regstart_seg1[*p++] = (dend == end_match_1);
  1269.       break;
  1270.  
  1271.     case stop_memory:
  1272.       regend[*p] = d;
  1273.        regend_seg1[*p++] = (dend == end_match_1);
  1274.       break;
  1275.  
  1276.     case duplicate:
  1277.       {
  1278.         int regno = *p++;   /* Get which register to match against */
  1279.         register unsigned char *d2, *dend2;
  1280.  
  1281.         d2 = regstart[regno];
  1282.          dend2 = ((regstart_seg1[regno] == regend_seg1[regno])
  1283.              ? regend[regno] : end_match_1);
  1284.         while (1)
  1285.           {
  1286.         /* Advance to next segment in register contents, if necessary */
  1287.         while (d2 == dend2)
  1288.           {
  1289.             if (dend2 == end_match_2) break;
  1290.             if (dend2 == regend[regno]) break;
  1291.             d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  1292.           }
  1293.         /* At end of register contents => success */
  1294.         if (d2 == dend2) break;
  1295.  
  1296.         /* Advance to next segment in data being matched, if necessary */
  1297.         PREFETCH;
  1298.  
  1299.         /* mcnt gets # consecutive chars to compare */
  1300.         mcnt = dend - d;
  1301.         if (mcnt > dend2 - d2)
  1302.           mcnt = dend2 - d2;
  1303.         /* Compare that many; failure if mismatch, else skip them. */
  1304.         if (translate ? bcmp_translate (d, d2, mcnt, translate) : bcmp (d, d2, mcnt))
  1305.           goto fail;
  1306.         d += mcnt, d2 += mcnt;
  1307.           }
  1308.       }
  1309.       break;
  1310.  
  1311.     case anychar:
  1312.       /* fetch a data character */
  1313.       PREFETCH;
  1314.       /* Match anything but a newline.  */
  1315.       if ((translate ? translate[*d++] : *d++) == '\n')
  1316.         goto fail;
  1317.       break;
  1318.  
  1319.     case charset:
  1320.     case charset_not:
  1321.       {
  1322.         /* Nonzero for charset_not */
  1323.         int not = 0;
  1324.         register int c;
  1325.         if (*(p - 1) == (unsigned char) charset_not)
  1326.           not = 1;
  1327.  
  1328.         /* fetch a data character */
  1329.         PREFETCH;
  1330.  
  1331.         if (translate)
  1332.           c = translate [*d];
  1333.         else
  1334.           c = *d;
  1335.  
  1336.         if (c < *p * BYTEWIDTH
  1337.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1338.           not = !not;
  1339.  
  1340.         p += 1 + *p;
  1341.  
  1342.         if (!not) goto fail;
  1343.         d++;
  1344.         break;
  1345.       }
  1346.  
  1347.     case begline:
  1348.       if (d == string1 || d[-1] == '\n')
  1349.         break;
  1350.       goto fail;
  1351.  
  1352.     case endline:
  1353.       if (d == end2
  1354.           || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  1355.         break;
  1356.       goto fail;
  1357.  
  1358.     /* "or" constructs ("|") are handled by starting each alternative
  1359.         with an on_failure_jump that points to the start of the next alternative.
  1360.         Each alternative except the last ends with a jump to the joining point.
  1361.         (Actually, each jump except for the last one really jumps
  1362.          to the following jump, because tensioning the jumps is a hassle.) */
  1363.  
  1364.     /* The start of a stupid repeat has an on_failure_jump that points
  1365.        past the end of the repeat text.
  1366.        This makes a failure point so that, on failure to match a repetition,
  1367.        matching restarts past as many repetitions have been found
  1368.        with no way to fail and look for another one.  */
  1369.  
  1370.     /* A smart repeat is similar but loops back to the on_failure_jump
  1371.        so that each repetition makes another failure point. */
  1372.  
  1373.     case on_failure_jump:
  1374.       if (stackp == stacke)
  1375.         {
  1376.           unsigned char **stackx;
  1377.           if (stacke - stackb > re_max_failures * 2)
  1378.         return -2;
  1379.           stackx = (unsigned char **) alloca (2 * (stacke - stackb)
  1380.                      * sizeof (char *));
  1381.           bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1382.           stackp = stackx + (stackp - stackb);
  1383.           stacke = stackx + 2 * (stacke - stackb);
  1384.           stackb = stackx;
  1385.         }
  1386.       mcnt = *p++ & 0377;
  1387.       mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1388.       p++;
  1389.       *stackp++ = mcnt + p;
  1390.       *stackp++ = d;
  1391.       break;
  1392.  
  1393.     /* The end of a smart repeat has an maybe_finalize_jump back.
  1394.        Change it either to a finalize_jump or an ordinary jump. */
  1395.  
  1396.     case maybe_finalize_jump:
  1397.       mcnt = *p++ & 0377;
  1398.       mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1399.       p++;
  1400.       {
  1401.         register unsigned char *p2 = p;
  1402.         /* Compare what follows with the begining of the repeat.
  1403.            If we can establish that there is nothing that they would
  1404.            both match, we can change to finalize_jump */
  1405.         while (p2 != pend
  1406.            && (*p2 == (unsigned char) stop_memory
  1407.                || *p2 == (unsigned char) start_memory))
  1408.           p2++;
  1409.         if (p2 == pend)
  1410.           p[-3] = (unsigned char) finalize_jump;
  1411.         else if (*p2 == (unsigned char) exactn
  1412.              || *p2 == (unsigned char) endline)
  1413.           {
  1414.         register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  1415.         register unsigned char *p1 = p + mcnt;
  1416.         /* p1[0] ... p1[2] are an on_failure_jump.
  1417.            Examine what follows that */
  1418.         if (p1[3] == (unsigned char) exactn && p1[5] != c)
  1419.           p[-3] = (unsigned char) finalize_jump;
  1420.         else if (p1[3] == (unsigned char) charset
  1421.              || p1[3] == (unsigned char) charset_not)
  1422.           {
  1423.             int not = p1[3] == (unsigned char) charset_not;
  1424.             if (c < p1[4] * BYTEWIDTH
  1425.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1426.               not = !not;
  1427.             /* not is 1 if c would match */
  1428.             /* That means it is not safe to finalize */
  1429.             if (!not)
  1430.               p[-3] = (unsigned char) finalize_jump;
  1431.           }
  1432.           }
  1433.       }
  1434.       p -= 2;
  1435.       if (p[-1] != (unsigned char) finalize_jump)
  1436.         {
  1437.           p[-1] = (unsigned char) jump;
  1438.           goto nofinalize;
  1439.         }
  1440.  
  1441.     /* The end of a stupid repeat has a finalize-jump
  1442.        back to the start, where another failure point will be made
  1443.        which will point after all the repetitions found so far. */
  1444.  
  1445.     case finalize_jump:
  1446.       stackp -= 2;
  1447.  
  1448.     case jump:
  1449.     nofinalize:
  1450.       mcnt = *p++ & 0377;
  1451.       mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1452.       p += mcnt + 1;    /* The 1 compensates for missing ++ above */
  1453.       break;
  1454.  
  1455.     case dummy_failure_jump:
  1456.       if (stackp == stacke)
  1457.         {
  1458.           unsigned char **stackx
  1459.         = (unsigned char **) alloca (2 * (stacke - stackb)
  1460.                          * sizeof (char *));
  1461.           bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1462.           stackp = stackx + (stackp - stackb);
  1463.           stacke = stackx + 2 * (stacke - stackb);
  1464.           stackb = stackx;
  1465.         }
  1466.       *stackp++ = 0;
  1467.       *stackp++ = 0;
  1468.       goto nofinalize;
  1469.  
  1470.     case wordbound:
  1471.       if (d == string1  /* Points to first char */
  1472.           || d == end2  /* Points to end */
  1473.           || (d == end1 && size2 == 0)) /* Points to end */
  1474.         break;
  1475.       if ((SYNTAX (d[-1]) == Sword)
  1476.           != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1477.         break;
  1478.       goto fail;
  1479.  
  1480.     case notwordbound:
  1481.       if (d == string1  /* Points to first char */
  1482.           || d == end2  /* Points to end */
  1483.           || (d == end1 && size2 == 0)) /* Points to end */
  1484.         goto fail;
  1485.       if ((SYNTAX (d[-1]) == Sword)
  1486.           != (SYNTAX (d == end1 ? *string2 : *d) == Sword))
  1487.         goto fail;
  1488.       break;
  1489.  
  1490.     case wordbeg:
  1491.       if (d == end2  /* Points to end */
  1492.           || (d == end1 && size2 == 0) /* Points to end */
  1493.           || SYNTAX (* (d == end1 ? string2 : d)) != Sword) /* Next char not a letter */
  1494.         goto fail;
  1495.       if (d == string1  /* Points to first char */
  1496.           || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1497.         break;
  1498.       goto fail;
  1499.  
  1500.     case wordend:
  1501.       if (d == string1  /* Points to first char */
  1502.           || SYNTAX (d[-1]) != Sword)  /* prev char not letter */
  1503.         goto fail;
  1504.       if (d == end2  /* Points to end */
  1505.           || (d == end1 && size2 == 0) /* Points to end */
  1506.           || SYNTAX (d == end1 ? *string2 : *d) != Sword) /* Next char not a letter */
  1507.         break;
  1508.       goto fail;
  1509.  
  1510. #ifdef emacs
  1511.     case before_dot:
  1512.       if (((d - string2 <= (unsigned) size2)
  1513.            ? d - bf_p2 : d - bf_p1)
  1514.           <= point)
  1515.         goto fail;
  1516.       break;
  1517.  
  1518.     case at_dot:
  1519.       if (((d - string2 <= (unsigned) size2)
  1520.            ? d - bf_p2 : d - bf_p1)
  1521.           == point)
  1522.         goto fail;
  1523.       break;
  1524.  
  1525.     case after_dot:
  1526.       if (((d - string2 <= (unsigned) size2)
  1527.            ? d - bf_p2 : d - bf_p1)
  1528.           >= point)
  1529.         goto fail;
  1530.       break;
  1531.  
  1532.     case wordchar:
  1533.       mcnt = (int) Sword;
  1534.       goto matchsyntax;
  1535.  
  1536.     case syntaxspec:
  1537.       mcnt = *p++;
  1538.     matchsyntax:
  1539.       PREFETCH;
  1540.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  1541.       break;
  1542.       
  1543.     case notwordchar:
  1544.       mcnt = (int) Sword;
  1545.       goto matchnotsyntax;
  1546.  
  1547.     case notsyntaxspec:
  1548.       mcnt = *p++;
  1549.     matchnotsyntax:
  1550.       PREFETCH;
  1551.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  1552.       break;
  1553. #else
  1554.     case wordchar:
  1555.       PREFETCH;
  1556.       if (SYNTAX (*d++) == 0) goto fail;
  1557.       break;
  1558.       
  1559.     case notwordchar:
  1560.       PREFETCH;
  1561.       if (SYNTAX (*d++) != 0) goto fail;
  1562.       break;
  1563. #endif /* not emacs */
  1564.  
  1565.     case begbuf:
  1566.       if (d == string1)    /* Note, d cannot equal string2 */
  1567.         break;        /* unless string1 == string2.  */
  1568.       goto fail;
  1569.  
  1570.     case endbuf:
  1571.       if (d == end2 || (d == end1 && size2 == 0))
  1572.         break;
  1573.       goto fail;
  1574.  
  1575.     case exactn:
  1576.       /* Match the next few pattern characters exactly.
  1577.          mcnt is how many characters to match. */
  1578.       mcnt = *p++;
  1579.       if (translate)
  1580.         {
  1581.           do
  1582.         {
  1583.           PREFETCH;
  1584.           if (translate[*d++] != *p++) goto fail;
  1585.         }
  1586.           while (--mcnt);
  1587.         }
  1588.       else
  1589.         {
  1590.           do
  1591.         {
  1592.           PREFETCH;
  1593.           if (*d++ != *p++) goto fail;
  1594.         }
  1595.           while (--mcnt);
  1596.         }
  1597.       break;
  1598.     }
  1599.       continue;    /* Successfully matched one pattern command; keep matching */
  1600.  
  1601.       /* Jump here if any matching operation fails. */
  1602.     fail:
  1603.       if (stackp != stackb)
  1604.     /* A restart point is known.  Restart there and pop it. */
  1605.     {
  1606.       if (!stackp[-2])
  1607.         {   /* If innermost failure point is dormant, flush it and keep looking */
  1608.           stackp -= 2;
  1609.           goto fail;
  1610.         }
  1611.       d = *--stackp;
  1612.       p = *--stackp;
  1613.       if (d >= string1 && d <= end1)
  1614.         dend = end_match_1;
  1615.     }
  1616.       else break;   /* Matching at this starting point really fails! */
  1617.     }
  1618.   return -1;         /* Failure to match */
  1619. }
  1620.  
  1621. static int
  1622. bcmp_translate (unsigned char *s1, unsigned char *s2, register int len, 
  1623.                 unsigned char *translate)
  1624. {
  1625.   register unsigned char *p1 = s1, *p2 = s2;
  1626.   while (len)
  1627.     {
  1628.       if (translate [*p1++] != translate [*p2++]) return 1;
  1629.       len--;
  1630.     }
  1631.   return 0;
  1632. }
  1633.  
  1634. /* Entry points compatible with bsd4.2 regex library */
  1635.  
  1636. #ifndef emacs
  1637.  
  1638. static struct re_pattern_buffer re_comp_buf;
  1639.  
  1640. char *
  1641. re_comp (char *s)
  1642. {
  1643.   if (!s)
  1644.     {
  1645.       if (!re_comp_buf.buffer)
  1646.     return "No previous regular expression";
  1647.       return 0;
  1648.     }
  1649.  
  1650.   if (!re_comp_buf.buffer)
  1651.     {
  1652.       if (!(re_comp_buf.buffer = (char *) new char[200]))
  1653.     return "Memory exhausted";
  1654.       re_comp_buf.allocated = 200;
  1655.       if (!(re_comp_buf.fastmap = (char *) new char[ (1 << BYTEWIDTH)]))
  1656.     return "Memory exhausted";
  1657.     }
  1658.   return re_compile_pattern (s, strlen (s), &re_comp_buf);
  1659. }
  1660.  
  1661. int
  1662. re_exec (char *s)
  1663. {
  1664.   int len = strlen (s);
  1665.   return 0 <= re_search (&re_comp_buf, s, len, 0, len, 0);
  1666. }
  1667.  
  1668. #endif /* emacs */
  1669.  
  1670. #ifdef test
  1671.  
  1672. #include <stdio.h>
  1673.  
  1674. /* Indexed by a character, gives the upper case equivalent of the character */
  1675.  
  1676. static char upcase[0400] = 
  1677.   { 000, 001, 002, 003, 004, 005, 006, 007,
  1678.     010, 011, 012, 013, 014, 015, 016, 017,
  1679.     020, 021, 022, 023, 024, 025, 026, 027,
  1680.     030, 031, 032, 033, 034, 035, 036, 037,
  1681.     040, 041, 042, 043, 044, 045, 046, 047,
  1682.     050, 051, 052, 053, 054, 055, 056, 057,
  1683.     060, 061, 062, 063, 064, 065, 066, 067,
  1684.     070, 071, 072, 073, 074, 075, 076, 077,
  1685.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1686.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1687.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1688.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  1689.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  1690.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  1691.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  1692.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  1693.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  1694.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  1695.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  1696.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  1697.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  1698.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  1699.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  1700.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  1701.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  1702.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  1703.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  1704.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  1705.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  1706.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  1707.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  1708.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  1709.   };
  1710.  
  1711. main (int argc, char **argv)
  1712. {
  1713.   char pat[80];
  1714.   struct re_pattern_buffer buf;
  1715.   int i;
  1716.   char c;
  1717.   char fastmap[(1 << BYTEWIDTH)];
  1718.  
  1719.   /* Allow a command argument to specify the style of syntax.  */
  1720.   if (argc > 1)
  1721.     obscure_syntax = atoi (argv[1]);
  1722.  
  1723.   buf.allocated = 40;
  1724.   buf.buffer = (char *) new char[buf.allocated];
  1725.   buf.fastmap = fastmap;
  1726.   buf.translate = upcase;
  1727.  
  1728.   while (1)
  1729.     {
  1730.       gets (pat);
  1731.  
  1732.       if (*pat)
  1733.     {
  1734.           re_compile_pattern (pat, strlen(pat), &buf);
  1735.  
  1736.       for (i = 0; i < buf.used; i++)
  1737.         printchar (buf.buffer[i]);
  1738.  
  1739.       putchar ('\n');
  1740.  
  1741.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  1742.  
  1743.       re_compile_fastmap (&buf);
  1744.       printf ("Allowed by fastmap: ");
  1745.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  1746.         if (fastmap[i]) printchar (i);
  1747.       putchar ('\n');
  1748.     }
  1749.  
  1750.       gets (pat);    /* Now read the string to match against */
  1751.  
  1752.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  1753.       printf ("Match value %d.\n", i);
  1754.     }
  1755. }
  1756.  
  1757. #ifdef NOTDEF
  1758. print_buf (struct re_pattern_buffer *bufp)
  1759. {
  1760.   int i;
  1761.  
  1762.   printf ("buf is :\n----------------\n");
  1763.   for (i = 0; i < bufp->used; i++)
  1764.     printchar (bufp->buffer[i]);
  1765.   
  1766.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  1767.   
  1768.   printf ("Allowed by fastmap: ");
  1769.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  1770.     if (bufp->fastmap[i])
  1771.       printchar (i);
  1772.   printf ("\nAllowed by translate: ");
  1773.   if (bufp->translate)
  1774.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  1775.       if (bufp->translate[i])
  1776.     printchar (i);
  1777.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  1778.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  1779. }
  1780. #endif
  1781.  
  1782. printchar (char c)
  1783. {
  1784.   if (c < 041 || c >= 0177)
  1785.     {
  1786.       putchar ('\\');
  1787.       putchar (((c >> 6) & 3) + '0');
  1788.       putchar (((c >> 3) & 7) + '0');
  1789.       putchar ((c & 7) + '0');
  1790.     }
  1791.   else
  1792.     putchar (c);
  1793. }
  1794.  
  1795. error (char *string)
  1796. {
  1797.   puts (string);
  1798.   exit (1);
  1799. }
  1800.  
  1801. #endif /* test */
  1802.  
  1803.