home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / NGAWK1.ZIP / REGEX.C < prev    next >
C/C++ Source or Header  |  1988-07-17  |  48KB  |  1,703 lines

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