home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 278.lha / RegexLibrary_v1.0 / regex.c < prev    next >
C/C++ Source or Header  |  1989-08-06  |  50KB  |  1,621 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.  You may charge a distribution fee for the
  36. physical act of transferring a copy.
  37.  
  38.   2. You may modify your copy or copies of this source file or
  39. any portion of it, and copy and distribute such modifications under
  40. the terms of Paragraph 1 above, provided that you also do the following:
  41.  
  42.     a) cause the modified files to carry prominent notices stating
  43.     that you changed the files and the date of any change; and
  44.  
  45.     b) cause the whole of any work that you distribute or publish,
  46.     that in whole or in part contains or is a derivative of this
  47.     program or any part thereof, to be licensed at no charge to all
  48.     third parties on terms identical to those contained in this
  49.     License Agreement (except that you may choose to grant more extensive
  50.     warranty protection to some or all third parties, at your option).
  51.  
  52.     c) You may charge a distribution fee for the physical act of
  53.     transferring a copy, and you may at your option offer warranty
  54.     protection in exchange for a fee.
  55.  
  56. Mere aggregation of another unrelated program with this program (or its
  57. derivative) on a volume of a storage or distribution medium does not bring
  58. the other program under the scope of these terms.
  59.  
  60.   3. You may copy and distribute this program (or a portion or derivative
  61. of it, under Paragraph 2) in object code or executable form under the terms
  62. of Paragraphs 1 and 2 above provided that you also do one of the following:
  63.  
  64.     a) accompany it with the complete corresponding machine-readable
  65.     source code, which must be distributed under the terms of
  66.     Paragraphs 1 and 2 above; or,
  67.  
  68.     b) accompany it with a written offer, valid for at least three
  69.     years, to give any third party free (except for a nominal
  70.     shipping charge) a complete machine-readable copy of the
  71.     corresponding source code, to be distributed under the terms of
  72.     Paragraphs 1 and 2 above; or,
  73.  
  74.     c) accompany it with the information you received as to where the
  75.     corresponding source code may be obtained.  (This alternative is
  76.     allowed only for noncommercial distribution and only if you
  77.     received the program in object code or executable form alone.)
  78.  
  79. For an executable file, complete source code means all the source code for
  80. all modules it contains; but, as a special exception, it need not include
  81. source code for modules which are standard libraries that accompany the
  82. operating system on which the executable file runs.
  83.  
  84.   4. You may not copy, sublicense, distribute or transfer this program
  85. except as expressly provided under this License Agreement.  Any attempt
  86. otherwise to copy, sublicense, distribute or transfer this program is void and
  87. your rights to use the program under this License agreement shall be
  88. automatically terminated.  However, parties who have received computer
  89. software programs from you with this License Agreement will not have
  90. their licenses terminated so long as such parties remain in full compliance.
  91.  
  92.   5. If you wish to incorporate parts of this program into other free
  93. programs whose distribution conditions are different, write to the Free
  94. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  95. worked out a simple rule that can be stated here, but we will often permit
  96. this.  We will be guided by the two goals of preserving the free status of
  97. all derivatives of our free software and of promoting the sharing and reuse of
  98. software.
  99.  
  100.  
  101. In other words, you are welcome to use, share and improve this program.
  102. You are forbidden to forbid anyone else to use, share and improve
  103. what you give them.   Help stamp out software-hoarding!  */
  104.  
  105. /*
  106.  * Ported to the Amiga under Manx Aztec C v3.6
  107.  * by Edwin Hoogerbeets 18/07/89
  108.  *
  109.  */
  110. #include "regex.h"
  111.  
  112. extern char *malloc();
  113. extern void free();
  114.  
  115. #ifdef SYNTAX_TABLE
  116. extern char *re_syntax_table;
  117. #endif
  118.  
  119. #ifdef AZTEC_C
  120. #define bcopy(src,dst,n)  movmem((src),(dst),(n))
  121. #define memcpy(dst,src,n) movmem((src),(dst),(n))
  122. #define bcmp(s1,s2,n)   memcmp((s1),(s2),(n))
  123. #define memset(src,value,howmany) setmem((src),(howmany),(value))
  124. #define bzero(src,howmany) setmem((src),(howmany),0)
  125.  
  126. #endif /* AZTEC_C */
  127.  
  128. extern char *realloc();
  129.  
  130. char myname[] = "regex.library";
  131. char myid[] = "Gnu Regular Expression Sharable Amiga Library v1.0 1989";
  132. char author[] = "Ported by Edwin Hoogerbeets 22/07/89";
  133.  
  134. #ifdef AZTEC_C
  135. int memcmp(a,b,length)
  136. char *a, *b;
  137. int length;
  138. {
  139.   register int index;
  140.  
  141.   for ( index = 0; index < length; index++ ) {
  142.     if ( a[index] != b[index] ) {
  143.       return(a[index] > b[index] ? -1 : 1);
  144.     }
  145.   }
  146.  
  147.   return(0);
  148. }
  149. #endif
  150.  
  151. /*
  152.  * Define the syntax stuff, so we can do the \<...\> things.
  153.  */
  154.  
  155. #ifndef Sword /* must be non-zero in some of the tests below... */
  156. #define Sword 1
  157. #endif
  158.  
  159. #define SYNTAX(c) re_syntax_table[c]
  160.  
  161. #ifdef SYNTAX_TABLE
  162.  
  163. char *re_syntax_table;
  164.  
  165. #else
  166.  
  167. static char re_syntax_table[256];
  168.  
  169. ULONG init_syntax_once()
  170. {
  171.    register int c;
  172.    static int done = 0;
  173.  
  174.    if (done)
  175.      return;
  176.  
  177.    bzero(re_syntax_table, sizeof re_syntax_table);
  178.  
  179.    for (c = 'a'; c <= 'z'; c++)
  180.      re_syntax_table[c] = Sword;
  181.  
  182.    for (c = 'A'; c <= 'Z'; c++)
  183.      re_syntax_table[c] = Sword;
  184.  
  185.    for (c = '0'; c <= '9'; c++)
  186.      re_syntax_table[c] = Sword;
  187.  
  188.    done = 1;
  189. }
  190.  
  191. #endif /* SYNTAX_TABLE */
  192.  
  193.  
  194. /* Number of failure points to allocate space for initially,
  195.  when matching.  If this number is exceeded, more space is allocated,
  196.  so it is not a hard limit.  */
  197.  
  198. #ifndef NFAILURES
  199. #define NFAILURES 80
  200. #endif /* NFAILURES */
  201.  
  202. #ifndef SIGN_EXTEND_CHAR
  203. #define SIGN_EXTEND_CHAR(x) (x)
  204. #endif
  205.  
  206. /* re_compile_pattern takes a regular-expression string
  207.    and converts it into a buffer full of byte commands for matching.
  208.  
  209.   PATTERN   is the address of the pattern string
  210.   SIZE      is the length of it.
  211.   BUFP      is a  struct re_pattern_buffer *  which points to the info
  212.             on where to store the byte commands.
  213.             This structure contains a  char *  which points to the
  214.             actual space, which should have been obtained with malloc.
  215.             re_compile_pattern may use  realloc  to grow the buffer space.
  216.  
  217.   The number of bytes of commands can be found out by looking in
  218.   the  struct re_pattern_buffer  that bufp pointed to,
  219.   after re_compile_pattern returns.
  220. */
  221.  
  222. #define PATPUSH(ch) (*b++ = (char) (ch))
  223.  
  224. #define PATFETCH(c) \
  225.  {if (p == pend) goto end_of_pattern; \
  226.   c = * (unsigned char *) p++; \
  227.   if (translate) c = translate[c]; }
  228.  
  229. #define PATFETCH_RAW(c) \
  230.  {if (p == pend) goto end_of_pattern; \
  231.   c = * (unsigned char *) p++; }
  232.  
  233. #define PATUNFETCH p--
  234.  
  235. #define EXTEND_BUFFER \
  236.   { char *old_buffer = bufp->buffer; \
  237.     if (bufp->allocated == (1<<16)) goto too_big; \
  238.     bufp->allocated *= 2; \
  239.     if (bufp->allocated > (1<<16)) bufp->allocated = (1<<16); \
  240.     if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
  241.       goto memory_exhausted; \
  242.     c = bufp->buffer - old_buffer; \
  243.     b += c; \
  244.     if (fixup_jump) \
  245.       fixup_jump += c; \
  246.     if (laststart) \
  247.       laststart += c; \
  248.     begalt += c; \
  249.     if (pending_exact) \
  250.       pending_exact += c; \
  251.   }
  252.  
  253. static int store_jump(), insert_jump();
  254.  
  255. #ifdef AMIGA
  256. char *
  257. re_initialize_buffer(bufp, table)
  258. struct re_pattern_buffer *bufp;
  259. char *table;
  260. {
  261.   if ( !bufp ) {
  262.     return("No buffer");
  263.   }
  264.  
  265.   bufp->allocated = 512;
  266.  
  267.   if ( !(bufp->buffer = (char *) malloc(bufp->allocated)) ||
  268.        !(bufp->fastmap = (char *) malloc(1 << BYTEWIDTH)) ) {
  269.     return ("Memory exhausted");
  270.   }
  271.  
  272.   bufp->translate = table;
  273.   bufp->fastmap_accurate = bufp->can_be_null = bufp->used = 0;
  274.  
  275. #ifndef SYNTAX_TABLE
  276.   /*
  277.    * Initialize the syntax table.
  278.    */
  279.   init_syntax_once();
  280. #endif
  281.  
  282.   return(NULL);
  283. }
  284.  
  285. LONG
  286. re_terminate_buffer(bufp)
  287. struct re_pattern_buffer *bufp;
  288. {
  289.   if ( bufp ) {
  290.     free(bufp->buffer);
  291.     free(bufp->fastmap);
  292.     return(1L);
  293.   } else {
  294.     return(0L);
  295.   }
  296. }
  297. #endif /* AMIGA */
  298.  
  299. char *
  300. re_compile_pattern(pattern, size, bufp, ob)
  301. char *pattern;
  302. long size, ob;
  303. struct re_pattern_buffer *bufp;
  304. {
  305.   register char *b = bufp->buffer;
  306.   register char *p = pattern;
  307.   char *pend = pattern + size;
  308.   register unsigned c, c1;
  309.   char *p1;
  310.   unsigned char *translate = (unsigned char *) bufp->translate;
  311.  
  312.   /* address of the count-byte of the most recently inserted "exactn" command.
  313.     This makes it possible to tell whether a new exact-match character
  314.     can be added to that command or requires a new "exactn" command. */
  315.  
  316.   char *pending_exact = 0;
  317.  
  318.   /* address of the place where a forward-jump should go
  319.     to the end of the containing expression.
  320.     Each alternative of an "or", except the last, ends with a forward-jump
  321.     of this sort. */
  322.  
  323.   char *fixup_jump = 0;
  324.  
  325.   /* address of start of the most recently finished expression.
  326.     This tells postfix * where to find the start of its operand. */
  327.  
  328.   char *laststart = 0;
  329.  
  330.   /* In processing a repeat, 1 means zero matches is allowed */
  331.  
  332.   char zero_times_ok;
  333.  
  334.   /* In processing a repeat, 1 means many matches is allowed */
  335.  
  336.   char many_times_ok;
  337.  
  338.   /* address of beginning of regexp, or inside of last \( */
  339.  
  340.   char *begalt = b;
  341.  
  342.   /* Stack of information saved by \( and restored by \).
  343.      Four stack elements are pushed by each \(:
  344.        First, the value of b.
  345.        Second, the value of fixup_jump.
  346.        Third, the value of regnum.
  347.        Fourth, the value of begalt.  */
  348.  
  349.   int stackb[40];
  350.   int *stackp = stackb;
  351.   int *stacke = stackb + 40;
  352.   int *stackt;
  353.  
  354.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  355.      where it becomes the "register number" to put in the stop_memory command */
  356.  
  357.   int regnum = 1;
  358.  
  359.   bufp->fastmap_accurate = 0;
  360.  
  361. #if !AMIGA && !SYNTAX_TABLE
  362.   /*
  363.    * Initialize the syntax table.
  364.    */
  365.   init_syntax_once();
  366. #endif
  367.  
  368.   if (bufp->allocated == 0) {
  369.     bufp->allocated = 1024L;
  370.     if (bufp->buffer)
  371.       /* EXTEND_BUFFER loses when bufp->allocated is 0 */
  372.       bufp->buffer = (char *) realloc (bufp->buffer, 1024L);
  373.     else
  374.       /* Caller did not allocate a buffer.  Do it for him */
  375.       bufp->buffer = (char *) malloc (1024L);
  376.     if (!bufp->buffer) goto memory_exhausted;
  377.     begalt = b = bufp->buffer;
  378.   }
  379.  
  380.   while (p != pend)
  381.     {
  382.       if (b - bufp->buffer > bufp->allocated - 10)
  383.         /* Note that EXTEND_BUFFER clobbers c */
  384.         EXTEND_BUFFER;
  385.  
  386.       PATFETCH (c);
  387.  
  388.       switch (c)
  389.         {
  390.         case '$':
  391.           if ( ob & RE_TIGHT_VBAR)
  392.             {
  393.               if (! ( ob & RE_CONTEXT_INDEP_OPS) && p != pend)
  394.                 goto normal_char;
  395.               /* Make operand of last vbar end before this `$'.  */
  396.               if (fixup_jump)
  397.                 store_jump (fixup_jump, jump, b);
  398.               fixup_jump = 0;
  399.               PATPUSH (endline);
  400.               break;
  401.             }
  402.  
  403.           /* $ means succeed if at end of line, but only in special contexts.
  404.             If randomly in the middle of a pattern, it is a normal character. */
  405.           if (p == pend || *p == '\n'
  406.               || (ob & RE_CONTEXT_INDEP_OPS)
  407.               || (ob & RE_NO_BK_PARENS
  408.                   ? *p == ')'
  409.                   : *p == '\\' && p[1] == ')')
  410.               || (ob & RE_NO_BK_VBAR
  411.                   ? *p == '|'
  412.                   : *p == '\\' && p[1] == '|'))
  413.             {
  414.               PATPUSH (endline);
  415.               break;
  416.             }
  417.           goto normal_char;
  418.  
  419.         case '^':
  420.           /* ^ means succeed if at beg of line, but only if no preceding pattern. */
  421.  
  422.           if (laststart && p[-2] != '\n'
  423.               && ! (ob & RE_CONTEXT_INDEP_OPS))
  424.             goto normal_char;
  425.           if (ob & RE_TIGHT_VBAR)
  426.             {
  427.               if (p != pattern + 1
  428.                   && ! (ob & RE_CONTEXT_INDEP_OPS))
  429.                 goto normal_char;
  430.               PATPUSH (begline);
  431.               begalt = b;
  432.             }
  433.           else
  434.             PATPUSH (begline);
  435.           break;
  436.  
  437.         case '+':
  438.         case '?':
  439.           if (ob & RE_BK_PLUS_QM)
  440.             goto normal_char;
  441.         handle_plus:
  442.         case '*':
  443.           /* If there is no previous pattern, char not special. */
  444.           if (!laststart && ! (ob & RE_CONTEXT_INDEP_OPS))
  445.             goto normal_char;
  446.           /* If there is a sequence of repetition chars,
  447.              collapse it down to equivalent to just one.  */
  448.           zero_times_ok = 0;
  449.           many_times_ok = 0;
  450.           while (1)
  451.             {
  452.               zero_times_ok |= c != '+';
  453.               many_times_ok |= c != '?';
  454.               if (p == pend)
  455.                 break;
  456.               PATFETCH (c);
  457.               if (c == '*')
  458.                 ;
  459.               else if (!(ob & RE_BK_PLUS_QM)
  460.                        && (c == '+' || c == '?'))
  461.                 ;
  462.               else if ((ob & RE_BK_PLUS_QM)
  463.                        && c == '\\')
  464.                 {
  465.                   int c1;
  466.                   PATFETCH (c1);
  467.                   if (!(c1 == '+' || c1 == '?'))
  468.                     {
  469.                       PATUNFETCH;
  470.                       PATUNFETCH;
  471.                       break;
  472.                     }
  473.                   c = c1;
  474.                 }
  475.               else
  476.                 {
  477.                   PATUNFETCH;
  478.                   break;
  479.                 }
  480.             }
  481.  
  482.           /* Star, etc. applied to an empty pattern is equivalent
  483.              to an empty pattern.  */
  484.           if (!laststart)
  485.             break;
  486.  
  487.           /* Now we know whether 0 matches is allowed,
  488.              and whether 2 or more matches is allowed.  */
  489.           if (many_times_ok)
  490.             {
  491.               /* If more than one repetition is allowed,
  492.                  put in a backward jump at the end.  */
  493.               store_jump (b, maybe_finalize_jump, laststart - 3);
  494.               b += 3;
  495.             }
  496.           insert_jump (on_failure_jump, laststart, b + 3, b);
  497.           pending_exact = 0;
  498.           b += 3;
  499.           if (!zero_times_ok)
  500.             {
  501.               /* At least one repetition required: insert before the loop
  502.                  a skip over the initial on-failure-jump instruction */
  503.               insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  504.               b += 3;
  505.             }
  506.           break;
  507.  
  508.         case '.':
  509.           laststart = b;
  510.           PATPUSH (anychar);
  511.           break;
  512.  
  513.         case '[':
  514.           while (b - bufp->buffer
  515.                  > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  516.             /* Note that EXTEND_BUFFER clobbers c */
  517.             EXTEND_BUFFER;
  518.  
  519.           laststart = b;
  520.           if (*p == '^')
  521.             PATPUSH (charset_not), p++;
  522.           else
  523.             PATPUSH (charset);
  524.           p1 = p;
  525.  
  526.           PATPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  527.           /* Clear the whole map */
  528.           bzero(b, (1 << BYTEWIDTH) / BYTEWIDTH);
  529.           /* Read in characters and ranges, setting map bits */
  530.           while (1)
  531.             {
  532.               PATFETCH (c);
  533.               if (c == ']' && p != p1 + 1) break;
  534.               if (*p == '-' && p[1] != ']')
  535.                 {
  536.                   PATFETCH (c1);
  537.                   PATFETCH (c1);
  538.                   while (c <= c1)
  539.                     b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH), c++;
  540.                 }
  541.               else
  542.                 {
  543.                   b[c / BYTEWIDTH] |= 1 << (c % BYTEWIDTH);
  544.                 }
  545.             }
  546.           /* Discard any bitmap bytes that are all 0 at the end of the map.
  547.              Decrement the map-length byte too. */
  548.           while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  549.             b[-1]--;
  550.           b += b[-1];
  551.           break;
  552.  
  553.         case '(':
  554.           if (! (ob & RE_NO_BK_PARENS))
  555.             goto normal_char;
  556.           else
  557.             goto handle_open;
  558.  
  559.         case ')':
  560.           if (! (ob & RE_NO_BK_PARENS))
  561.             goto normal_char;
  562.           else
  563.             goto handle_close;
  564.  
  565.         case '\n':
  566.           if (! (ob & RE_NEWLINE_OR))
  567.             goto normal_char;
  568.           else
  569.             goto handle_bar;
  570.  
  571.         case '|':
  572.           if (! (ob & RE_NO_BK_VBAR))
  573.             goto normal_char;
  574.           else
  575.             goto handle_bar;
  576.  
  577.         case '\\':
  578.           if (p == pend) goto invalid_pattern;
  579.           PATFETCH_RAW (c);
  580.           switch (c)
  581.             {
  582.             case '(':
  583.               if (ob & RE_NO_BK_PARENS)
  584.                 goto normal_backsl;
  585.             handle_open:
  586.               if (stackp == stacke) goto nesting_too_deep;
  587.               if (regnum < RE_NREGS)
  588.                 {
  589.                   PATPUSH (start_memory);
  590.                   PATPUSH (regnum);
  591.                 }
  592.               *stackp++ = b - bufp->buffer;
  593.               *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  594.               *stackp++ = regnum++;
  595.               *stackp++ = begalt - bufp->buffer;
  596.               fixup_jump = 0;
  597.               laststart = 0;
  598.               begalt = b;
  599.               break;
  600.  
  601.             case ')':
  602.               if (ob & RE_NO_BK_PARENS)
  603.                 goto normal_backsl;
  604.             handle_close:
  605.               if (stackp == stackb) goto unmatched_close;
  606.               begalt = *--stackp + bufp->buffer;
  607.               if (fixup_jump)
  608.                 store_jump (fixup_jump, jump, b);
  609.               if (stackp[-1] < RE_NREGS)
  610.                 {
  611.                   PATPUSH (stop_memory);
  612.                   PATPUSH (stackp[-1]);
  613.                 }
  614.               stackp -= 2;
  615.               fixup_jump = 0;
  616.               if (*stackp)
  617.                 fixup_jump = *stackp + bufp->buffer - 1;
  618.               laststart = *--stackp + bufp->buffer;
  619.               break;
  620.  
  621.             case '|':
  622.               if (ob & RE_NO_BK_VBAR)
  623.                 goto normal_backsl;
  624.             handle_bar:
  625.               insert_jump (on_failure_jump, begalt, b + 6, b);
  626.               pending_exact = 0;
  627.               b += 3;
  628.               if (fixup_jump)
  629.                 store_jump (fixup_jump, jump, b);
  630.               fixup_jump = b;
  631.               b += 3;
  632.               laststart = 0;
  633.               begalt = b;
  634.               break;
  635.  
  636.             case 'w':
  637.               laststart = b;
  638.               PATPUSH (wordchar);
  639.               break;
  640.  
  641.             case 'W':
  642.               laststart = b;
  643.               PATPUSH (notwordchar);
  644.               break;
  645.  
  646.             case '<':
  647.               PATPUSH (wordbeg);
  648.               break;
  649.  
  650.             case '>':
  651.               PATPUSH (wordend);
  652.               break;
  653.  
  654.             case 'b':
  655.               PATPUSH (wordbound);
  656.               break;
  657.  
  658.             case 'B':
  659.               PATPUSH (notwordbound);
  660.               break;
  661.  
  662.             case '`':
  663.               PATPUSH (begbuf);
  664.               break;
  665.  
  666.             case '\'':
  667.               PATPUSH (endbuf);
  668.               break;
  669.  
  670.             case '1':
  671.             case '2':
  672.             case '3':
  673.             case '4':
  674.             case '5':
  675.             case '6':
  676.             case '7':
  677.             case '8':
  678.             case '9':
  679.               c1 = c - '0';
  680.               if (c1 >= regnum)
  681.                 goto normal_char;
  682.               for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  683.                 if (*stackt == c1)
  684.                   goto normal_char;
  685.               laststart = b;
  686.               PATPUSH (duplicate);
  687.               PATPUSH (c1);
  688.               break;
  689.  
  690.             case '+':
  691.             case '?':
  692.               if (ob & RE_BK_PLUS_QM)
  693.                 goto handle_plus;
  694.  
  695.             default:
  696.             normal_backsl:
  697.               /* You might think it would be useful for \ to mean
  698.                  not to translate; but if we don't translate it
  699.                  it will never match anything.  */
  700.               if (translate) c = translate[c];
  701.               goto normal_char;
  702.             }
  703.           break;
  704.  
  705.         default:
  706.         normal_char:
  707.           if (!pending_exact || pending_exact + *pending_exact + 1 != b
  708.               || *pending_exact == 0177 || *p == '*' || *p == '^'
  709.               || ((ob & RE_BK_PLUS_QM)
  710.                   ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  711.                   : (*p == '+' || *p == '?')))
  712.             {
  713.               laststart = b;
  714.               PATPUSH (exactn);
  715.               pending_exact = b;
  716.               PATPUSH (0);
  717.             }
  718.           PATPUSH (c);
  719.           (*pending_exact)++;
  720.         }
  721.     }
  722.  
  723.   if (fixup_jump)
  724.     store_jump (fixup_jump, jump, b);
  725.  
  726.   if (stackp != stackb) goto unmatched_open;
  727.  
  728.   bufp->used = b - bufp->buffer;
  729.   return( (char *) 0 );
  730.  
  731.  invalid_pattern:
  732.   return ("Invalid regular expression");
  733.  
  734.  unmatched_open:
  735.   return ("Unmatched \\(");
  736.  
  737.  unmatched_close:
  738.   return ("Unmatched \\)");
  739.  
  740.  end_of_pattern:
  741.   return ("Premature end of regular expression");
  742.  
  743.  nesting_too_deep:
  744.   return ("Nesting too deep");
  745.  
  746.  too_big:
  747.   return ("Regular expression too big");
  748.  
  749.  memory_exhausted:
  750.   return ("Memory exhausted");
  751. }
  752.  
  753. /* Store where `from' points a jump operation to jump to where `to' points.
  754.   `opcode' is the opcode to store. */
  755.  
  756. static int
  757. store_jump(from, opcode, to)
  758.      char *from, *to;
  759.      char opcode;
  760. {
  761.   from[0] = opcode;
  762.   from[1] = (to - (from + 3)) & 0377;
  763.   from[2] = (to - (from + 3)) >> 8;
  764. }
  765.  
  766. /* Open up space at char FROM, and insert there a jump to TO.
  767.    CURRENT_END gives te end of the storage no in use,
  768.    so we know how much data to copy up.
  769.    OP is the opcode of the jump to insert.
  770.  
  771.    If you call this function, you must zero out pending_exact.  */
  772.  
  773. static int
  774. insert_jump(op, from, to, current_end)
  775.      char op;
  776.      char *from, *to, *current_end;
  777. {
  778.   register char *pto = current_end + 3;
  779.   register char *pfrom = current_end;
  780.   while (pfrom != from)
  781.     *--pto = *--pfrom;
  782.   store_jump(from, op, to);
  783. }
  784.  
  785. /* Given a pattern, compute a fastmap from it.
  786.  The fastmap records which of the (1 << BYTEWIDTH) possible characters
  787.  can start a string that matches the pattern.
  788.  This fastmap is used by re_search to skip quickly over totally implausible text.
  789.  
  790.  The caller must supply the address of a (1 << BYTEWIDTH)-byte data area
  791.  as bufp->fastmap.
  792.  The other components of bufp describe the pattern to be used.  */
  793.  
  794. LONG
  795. re_compile_fastmap (bufp)
  796. struct re_pattern_buffer *bufp;
  797. {
  798.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  799.   int size = bufp->used;
  800.   register char *fastmap = bufp->fastmap;
  801.   register unsigned char *p = pattern;
  802.   register unsigned char *pend = pattern + size;
  803.   register int j, k;
  804.   unsigned char *translate = (unsigned char *) bufp->translate;
  805.  
  806.   unsigned char *stackb[NFAILURES];
  807.   unsigned char **stackp = stackb;
  808.  
  809.   bzero(fastmap,(1 << BYTEWIDTH));
  810.  
  811.   bufp->fastmap_accurate = 1;
  812.   bufp->can_be_null = 0;
  813.  
  814.   while (p)
  815.     {
  816.       if (p == pend)
  817.         {
  818.           bufp->can_be_null = 1;
  819.           break;
  820.         }
  821. #ifdef SWITCH_ENUM_BUG
  822.       switch ((int) ((enum regexpcode) *p++))
  823. #else
  824.       switch ((enum regexpcode) *p++)
  825. #endif
  826.         {
  827.         case exactn:
  828.           if (translate)
  829.             fastmap[translate[p[1]]] = 1;
  830.           else
  831.             fastmap[p[1]] = 1;
  832.           break;
  833.  
  834.         case begline:
  835.         case before_dot:
  836.         case at_dot:
  837.         case after_dot:
  838.         case begbuf:
  839.         case endbuf:
  840.         case wordbound:
  841.         case notwordbound:
  842.         case wordbeg:
  843.         case wordend:
  844.           continue;
  845.  
  846.         case endline:
  847.           if (translate)
  848.             fastmap[translate['\n']] = 1;
  849.           else
  850.             fastmap['\n'] = 1;
  851.           if (bufp->can_be_null != 1)
  852.             bufp->can_be_null = 2;
  853.           break;
  854.  
  855.         case finalize_jump:
  856.         case maybe_finalize_jump:
  857.         case jump:
  858.         case dummy_failure_jump:
  859.           bufp->can_be_null = 1;
  860.           j = *p++ & 0377;
  861.           j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  862.           p += j + 1;           /* The 1 compensates for missing ++ above */
  863.           if (j > 0)
  864.             continue;
  865.           /* Jump backward reached implies we just went through
  866.              the body of a loop and matched nothing.
  867.              Opcode jumped to should be an on_failure_jump.
  868.              Just treat it like an ordinary jump.
  869.              For a * loop, it has pushed its failure point already;
  870.              if so, discard that as redundant.  */
  871.           if ((enum regexpcode) *p != on_failure_jump)
  872.             continue;
  873.           p++;
  874.           j = *p++ & 0377;
  875.           j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  876.           p += j + 1;           /* The 1 compensates for missing ++ above */
  877.           if (stackp != stackb && *stackp == p)
  878.             stackp--;
  879.           continue;
  880.  
  881.         case on_failure_jump:
  882.           j = *p++ & 0377;
  883.           j += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  884.           p++;
  885.           *++stackp = p + j;
  886.           continue;
  887.  
  888.         case start_memory:
  889.         case stop_memory:
  890.           p++;
  891.           continue;
  892.  
  893.         case duplicate:
  894.           bufp->can_be_null = 1;
  895.           fastmap['\n'] = 1;
  896.         case anychar:
  897.           for (j = 0; j < (1 << BYTEWIDTH); j++)
  898.             if (j != '\n')
  899.               fastmap[j] = 1;
  900.           if (bufp->can_be_null)
  901.             return(0L);
  902.           /* Don't return; check the alternative paths
  903.              so we can set can_be_null if appropriate.  */
  904.           break;
  905.  
  906.         case wordchar:
  907.           for (j = 0; j < (1 << BYTEWIDTH); j++)
  908.             if (SYNTAX(j) == Sword)
  909.               fastmap[j] = 1;
  910.           break;
  911.  
  912.         case notwordchar:
  913.           for (j = 0; j < (1 << BYTEWIDTH); j++)
  914.             if (SYNTAX(j) != Sword)
  915.               fastmap[j] = 1;
  916.           break;
  917.  
  918.         case charset:
  919.           for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  920.             if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  921.               {
  922.                 if (translate)
  923.                   fastmap[translate[j]] = 1;
  924.                 else
  925.                   fastmap[j] = 1;
  926.               }
  927.           break;
  928.  
  929.         case charset_not:
  930.           /* Chars beyond end of map must be allowed */
  931.           for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  932.             if (translate)
  933.               fastmap[translate[j]] = 1;
  934.             else
  935.               fastmap[j] = 1;
  936.  
  937.           for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  938.             if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  939.               {
  940.                 if (translate)
  941.                   fastmap[translate[j]] = 1;
  942.                 else
  943.                   fastmap[j] = 1;
  944.               }
  945.           break;
  946.         }
  947.  
  948.       /* Get here means we have successfully found the possible starting characters
  949.          of one path of the pattern.  We need not follow this path any farther.
  950.          Instead, look at the next alternative remembered in the stack. */
  951.       if (stackp != stackb)
  952.         p = *stackp--;
  953.       else
  954.         break;
  955.     }
  956.   return(0L);
  957. }
  958.  
  959. /* Like re_search_2, below, but only one string is specified. */
  960.  
  961. LONG
  962. re_search(pbufp, string, size, startpos, range, regs)
  963. struct re_pattern_buffer *pbufp;
  964. char *string;
  965. long size, startpos, range;
  966. struct re_registers *regs;
  967. {
  968.   return(re_search_2(pbufp, 0L, 0L, string, size, startpos, range,
  969.                      regs, size));
  970. }
  971.  
  972. /* Like re_match_2 but tries first a match starting at index STARTPOS,
  973.    then at STARTPOS + 1, and so on.
  974.    RANGE is the number of places to try before giving up.
  975.    If RANGE is negative, the starting positions tried are
  976.     STARTPOS, STARTPOS - 1, etc.
  977.    It is up to the caller to make sure that range is not so large
  978.    as to take the starting position outside of the input strings.
  979.  
  980. The value returned is the position at which the match was found,
  981.  or -1 if no match was found,
  982.  or -2 if error (such as failure stack overflow).  */
  983.  
  984. LONG
  985. re_search_2(pbufp, string1, size1, string2, size2, startpos, range, regs, mstop)
  986. struct re_pattern_buffer *pbufp;
  987. char *string1, *string2;
  988. long size1, size2;
  989. long startpos;
  990. register long range;
  991. struct re_registers *regs;
  992. long mstop;
  993. {
  994.   register char *fastmap = pbufp->fastmap;
  995.   register unsigned char *translate = (unsigned char *) pbufp->translate;
  996.   int total = size1 + size2;
  997.   int val;
  998.  
  999.   /* Update the fastmap now if not correct already */
  1000.   if (fastmap && !pbufp->fastmap_accurate)
  1001.     re_compile_fastmap(pbufp);
  1002.  
  1003.   /* Don't waste time in a long search for a pattern
  1004.      that says it is anchored.  */
  1005.   if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  1006.       && range > 0)
  1007.     {
  1008.       if (startpos > 0)
  1009.         return(-1);
  1010.       else
  1011.         range = 1;
  1012.     }
  1013.  
  1014.   while (1)
  1015.     {
  1016.       /* If a fastmap is supplied, skip quickly over characters
  1017.          that cannot possibly be the start of a match.
  1018.          Note, however, that if the pattern can possibly match
  1019.          the null string, we must test it at each starting point
  1020.          so that we take the first null string we get.  */
  1021.  
  1022.       if (fastmap && startpos < total && pbufp->can_be_null != 1)
  1023.         {
  1024.           if (range > 0)
  1025.             {
  1026.               register int lim = 0;
  1027.               register unsigned char *p;
  1028.               int irange = range;
  1029.               if (startpos < size1 && startpos + range >= size1)
  1030.                 lim = range - (size1 - startpos);
  1031.  
  1032.               p = ((unsigned char *)
  1033.                    &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1034.  
  1035.               if (translate)
  1036.                 {
  1037.                   while (range > lim && !fastmap[translate[*p++]])
  1038.                     range--;
  1039.                 }
  1040.               else
  1041.                 {
  1042.                   while (range > lim && !fastmap[*p++])
  1043.                     range--;
  1044.                 }
  1045.               startpos += irange - range;
  1046.             }
  1047.           else
  1048.             {
  1049.               register unsigned char c;
  1050.               if (startpos >= size1)
  1051.                 c = string2[startpos - size1];
  1052.               else
  1053.                 c = string1[startpos];
  1054.               c &= 0xff;
  1055.                 if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1056.                 goto advance;
  1057.             }
  1058.         }
  1059.  
  1060.       if (range >= 0 && startpos == total
  1061.           && fastmap && pbufp->can_be_null == 0)
  1062.         return(-1);
  1063.  
  1064.       val = re_match_2(pbufp, string1, size1, string2, size2, startpos, regs, mstop);
  1065.       if (0 <= val)
  1066.         {
  1067.           if (val == -2)
  1068.             return(-2);
  1069.           return(startpos);
  1070.         }
  1071.  
  1072.     advance:
  1073.       if (!range) break;
  1074.       if (range > 0) range--, startpos++; else range++, startpos--;
  1075.     }
  1076.   return(-1);
  1077. }
  1078.  
  1079. LONG
  1080. re_match(pbufp, string, size, pos, regs)
  1081. struct re_pattern_buffer *pbufp;
  1082. char *string;
  1083. long size, pos;
  1084. struct re_registers *regs;
  1085. {
  1086.   return(re_match_2(pbufp, 0L, 0L, string, size, pos, regs, size));
  1087. }
  1088.  
  1089. /* Maximum size of failure stack.  Beyond this, overflow is an error.  */
  1090.  
  1091. #define re_max_failures 2000
  1092.  
  1093. static int bcmp_translate();
  1094. /* Match the pattern described by PBUFP
  1095.    against data which is the virtual concatenation of STRING1 and STRING2.
  1096.    SIZE1 and SIZE2 are the sizes of the two data strings.
  1097.    Start the match at position POS.
  1098.    Do not consider matching past the position MSTOP.
  1099.  
  1100.    If pbufp->fastmap is nonzero, then it had better be up to date.
  1101.  
  1102.    The reason that the data to match are specified as two components
  1103.    which are to be regarded as concatenated
  1104.    is so this function can be used directly on the contents of an Emacs buffer.
  1105.  
  1106.    -1 is returned if there is no match.  -2 is returned if there is
  1107.    an error (such as match stack overflow).  Otherwise the value is the length
  1108.    of the substring which was matched.  */
  1109.  
  1110. LONG
  1111. re_match_2(pbufp, string1, size1, string2, size2, pos, regs, mstop)
  1112. struct re_pattern_buffer *pbufp;
  1113. unsigned char *string1, *string2;
  1114. long size1, size2;
  1115. long pos;
  1116. struct re_registers *regs;
  1117. long mstop;
  1118. {
  1119.   register unsigned char *p = (unsigned char *) pbufp->buffer;
  1120.   register unsigned char *pend = p + pbufp->used;
  1121.   /* End of first string */
  1122.   unsigned char *end1;
  1123.   /* End of second string */
  1124.   unsigned char *end2;
  1125.   /* Pointer just past last char to consider matching */
  1126.   unsigned char *end_match_1, *end_match_2;
  1127.   register unsigned char *d, *dend;
  1128.   register long mcnt;
  1129.   unsigned char *translate = (unsigned char *) pbufp->translate;
  1130.  
  1131.  /* Failure point stack.  Each place that can handle a failure further down
  1132.     the line pushes a failure point on this stack.  It consists of two char
  1133.     *'s. The first one pushed is where to resume scanning the pattern; the
  1134.     second pushed is where to resume scanning the strings. If the latter is
  1135.     zero, the failure point is a "dummy". If a failure happens and the
  1136.     innermost failure point is dormant, it discards that failure point and
  1137.     tries the next one. */
  1138.  
  1139. #ifdef AMIGA
  1140.   unsigned char **stackb = (unsigned char **)
  1141.                            malloc( 2 * NFAILURES * sizeof(char *) );
  1142. #else
  1143.   unsigned char *initial_stack[2 * NFAILURES];
  1144. #endif
  1145.  
  1146.   unsigned char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];
  1147.  
  1148.   /* Information on the "contents" of registers.
  1149.      These are pointers into the input strings; they record
  1150.      just what was matched (on this attempt) by some part of the pattern.
  1151.      The start_memory command stores the start of a register's contents
  1152.      and the stop_memory command stores the end.
  1153.  
  1154.      At that point, regstart[regnum] points to the first character in the
  1155.      register, regend[regnum] points to the first character beyond the end
  1156.      of the register, regstart_seg1[regnum] is true iff regstart[regnum]
  1157.      points into string1, and regend_seg1[regnum] is true iff
  1158.      regend[regnum] points into string1.  */
  1159.  
  1160.   unsigned char *regstart[RE_NREGS];
  1161.   unsigned char *regend[RE_NREGS];
  1162.   unsigned char regstart_seg1[RE_NREGS], regend_seg1[RE_NREGS];
  1163.  
  1164.   /* Set up pointers to ends of strings.
  1165.      Don't allow the second string to be empty unless both are empty.  */
  1166.   if (!size2)
  1167.     {
  1168.       string2 = string1;
  1169.       size2 = size1;
  1170.       string1 = 0;
  1171.       size1 = 0;
  1172.     }
  1173.   end1 = string1 + size1;
  1174.   end2 = string2 + size2;
  1175.  
  1176.   /* Compute where to stop matching, within the two strings */
  1177.   if (mstop <= size1)
  1178.     {
  1179.       end_match_1 = string1 + mstop;
  1180.       end_match_2 = string2;
  1181.     }
  1182.   else
  1183.     {
  1184.       end_match_1 = end1;
  1185.       end_match_2 = string2 + mstop - size1;
  1186.     }
  1187.  
  1188.   /* Initialize \) text positions to -1
  1189.      to mark ones that no \( or \) has been seen for.  */
  1190.  
  1191.   for (mcnt = 0; mcnt < sizeof (regend) / sizeof (*regend); mcnt++)
  1192.     regend[mcnt] = (unsigned char *) -1;
  1193.  
  1194.   /* `p' scans through the pattern as `d' scans through the data.
  1195.      `dend' is the end of the input string that `d' points within.
  1196.      `d' is advanced into the following input string whenever necessary,
  1197.      but this happens before fetching;
  1198.      therefore, at the beginning of the loop,
  1199.      `d' can be pointing at the end of a string,
  1200.      but it cannot equal string2.  */
  1201.  
  1202.   if (pos <= size1)
  1203.     d = string1 + pos, dend = end_match_1;
  1204.   else
  1205.     d = string2 + pos - size1, dend = end_match_2;
  1206.  
  1207. /* Write PREFETCH; just before fetching a character with *d.  */
  1208. #define PREFETCH \
  1209.  while (d == dend)                                                  \
  1210.   { if (dend == end_match_2) goto fail;  /* end of string2 => failure */   \
  1211.     d = string2;  /* end of string1 => advance to string2. */       \
  1212.     dend = end_match_2; }
  1213.  
  1214.   /* This loop loops over pattern commands.
  1215.      It exits by returning from the function if match is complete,
  1216.      or it drops through if match fails at this starting point in the input data. */
  1217.  
  1218.   while (1)
  1219.     {
  1220.       if (p == pend)
  1221.         /* End of pattern means we have succeeded! */
  1222.         {
  1223.           /* If caller wants register contents data back, convert it to
  1224.              indices */
  1225.           if (regs)
  1226.             {
  1227.               regs->start[0] = pos;
  1228.               if (dend == end_match_1)
  1229.                 regs->end[0] = d - string1;
  1230.               else
  1231.                 regs->end[0] = d - string2 + size1;
  1232.               for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  1233.                 {
  1234.                   if (regend[mcnt] == (unsigned char *) -1)
  1235.                     {
  1236.                       regs->start[mcnt] = -1;
  1237.                       regs->end[mcnt] = -1;
  1238.                       continue;
  1239.                     }
  1240.                   if (regstart_seg1[mcnt])
  1241.                     regs->start[mcnt] = regstart[mcnt] - string1;
  1242.                   else
  1243.                     regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  1244.                   if (regend_seg1[mcnt])
  1245.                     regs->end[mcnt] = regend[mcnt] - string1;
  1246.                   else
  1247.                     regs->end[mcnt] = regend[mcnt] - string2 + size1;
  1248.                 }
  1249.             }
  1250.  
  1251. #ifdef AMIGA
  1252.           free(stackb);
  1253. #endif
  1254.           if (dend == end_match_1) {
  1255.             return (d - string1 - pos);
  1256.           } else {
  1257.             return (d - string2 + size1 - pos);
  1258.           }
  1259.         }
  1260.  
  1261.       /* Otherwise match next pattern command */
  1262. #ifdef SWITCH_ENUM_BUG
  1263.       switch ((int) ((enum regexpcode) *p++))
  1264. #else
  1265.       switch ((enum regexpcode) *p++)
  1266. #endif
  1267.         {
  1268.  
  1269.         /* \( is represented by a start_memory, \) by a stop_memory.
  1270.             Both of those commands contain a "register number" argument.
  1271.             The text matched within the \( and \) is recorded under that number.
  1272.             Then, \<digit> turns into a `duplicate' command which
  1273.             is followed by the numeric value of <digit> as the register number. */
  1274.  
  1275.         case start_memory:
  1276.           regstart[*p] = d;
  1277.           regstart_seg1[*p++] = (dend == end_match_1);
  1278.           break;
  1279.  
  1280.         case stop_memory:
  1281.           regend[*p] = d;
  1282.           regend_seg1[*p++] = (dend == end_match_1);
  1283.           break;
  1284.  
  1285.         case duplicate:
  1286.           {
  1287.             int regno = *p++;   /* Get which register to match against */
  1288.             register unsigned char *d2, *dend2;
  1289.  
  1290.             d2 = regstart[regno];
  1291.             dend2 = ((regstart_seg1[regno] == regend_seg1[regno])
  1292.                      ? regend[regno] : end_match_1);
  1293.             while (1)
  1294.               {
  1295.                 /* Advance to next segment in register contents, if necessary */
  1296.                 while (d2 == dend2)
  1297.                   {
  1298.                     if (dend2 == end_match_2) break;
  1299.                     if (dend2 == regend[regno]) break;
  1300.                     d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  1301.                   }
  1302.                 /* At end of register contents => success */
  1303.                 if (d2 == dend2) break;
  1304.  
  1305.                 /* Advance to next segment in data being matched, if necessary */
  1306.                 PREFETCH;
  1307.  
  1308.                 /* mcnt gets # consecutive chars to compare */
  1309.                 mcnt = dend - d;
  1310.                 if (mcnt > dend2 - d2)
  1311.                   mcnt = dend2 - d2;
  1312.                 /* Compare that many; failure if mismatch, else skip them. */
  1313.                 if (translate ? bcmp_translate(d, d2, mcnt, translate) : bcmp(d, d2, mcnt))
  1314.                   goto fail;
  1315.                 d += mcnt, d2 += mcnt;
  1316.               }
  1317.           }
  1318.           break;
  1319.  
  1320.         case anychar:
  1321.           /* fetch a data character */
  1322.           PREFETCH;
  1323.           /* Match anything but a newline.  */
  1324.           if ((translate ? translate[*d++] : *d++) == '\n')
  1325.             goto fail;
  1326.           break;
  1327.  
  1328.         case charset:
  1329.         case charset_not:
  1330.           {
  1331.             /* Nonzero for charset_not */
  1332.             int not = 0;
  1333.             register int c;
  1334.             if (*(p - 1) == (unsigned char) charset_not)
  1335.               not = 1;
  1336.  
  1337.             /* fetch a data character */
  1338.             PREFETCH;
  1339.  
  1340.             if (translate)
  1341.               c = translate [*d];
  1342.             else
  1343.               c = *d;
  1344.  
  1345.             if (c < *p * BYTEWIDTH
  1346.                 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1347.               not = !not;
  1348.  
  1349.             p += 1 + *p;
  1350.  
  1351.             if (!not) goto fail;
  1352.             d++;
  1353.             break;
  1354.           }
  1355.  
  1356.         case begline:
  1357.           if (d == string1 || d[-1] == '\n')
  1358.             break;
  1359.           goto fail;
  1360.  
  1361.         case endline:
  1362.           if (d == end2
  1363.               || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  1364.             break;
  1365.           goto fail;
  1366.  
  1367.         /* "or" constructs ("|") are handled by starting each alternative
  1368.             with an on_failure_jump that points to the start of the next alternative.
  1369.             Each alternative except the last ends with a jump to the joining point.
  1370.             (Actually, each jump except for the last one really jumps
  1371.              to the following jump, because tensioning the jumps is a hassle.) */
  1372.  
  1373.         /* The start of a stupid repeat has an on_failure_jump that points
  1374.            past the end of the repeat text.
  1375.            This makes a failure point so that, on failure to match a repetition,
  1376.            matching restarts past as many repetitions have been found
  1377.            with no way to fail and look for another one.  */
  1378.  
  1379.         /* A smart repeat is similar but loops back to the on_failure_jump
  1380.            so that each repetition makes another failure point. */
  1381.  
  1382.         case on_failure_jump:
  1383.           if (stackp == stacke)
  1384.             {
  1385.               unsigned char **stackx;
  1386.               if (stacke - stackb > re_max_failures * 2) {
  1387. #ifdef AMIGA
  1388.                 free(stackb);
  1389. #endif
  1390.                 return(-2);
  1391.               }
  1392.               stackx = (unsigned char **) malloc(
  1393.                               2 * (stacke - stackb) * sizeof (char *));
  1394.               bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1395.               stackp = stackx + (stackp - stackb);
  1396.               stacke = stackx + 2 * (stacke - stackb);
  1397. #ifdef AMIGA
  1398.               free(stackb);
  1399. #endif
  1400.               stackb = stackx;
  1401.             }
  1402.           mcnt = *p++ & 0377;
  1403.           mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1404.           p++;
  1405.           *stackp++ = mcnt + p;
  1406.           *stackp++ = d;
  1407.           break;
  1408.  
  1409.         /* The end of a smart repeat has an maybe_finalize_jump back.
  1410.            Change it either to a finalize_jump or an ordinary jump. */
  1411.  
  1412.         case maybe_finalize_jump:
  1413.           mcnt = *p++ & 0377;
  1414.           mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1415.           p++;
  1416.           {
  1417.             register unsigned char *p2 = p;
  1418.             /* Compare what follows with the begining of the repeat.
  1419.                If we can establish that there is nothing that they would
  1420.                both match, we can change to finalize_jump */
  1421.             while (p2 != pend
  1422.                    && (*p2 == (unsigned char) stop_memory
  1423.                        || *p2 == (unsigned char) start_memory))
  1424.               p2++;
  1425.             if (p2 == pend)
  1426.               p[-3] = (unsigned char) finalize_jump;
  1427.             else if (*p2 == (unsigned char) exactn
  1428.                      || *p2 == (unsigned char) endline)
  1429.               {
  1430.                 register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  1431.                 register unsigned char *p1 = p + mcnt;
  1432.                 /* p1[0] ... p1[2] are an on_failure_jump.
  1433.                    Examine what follows that */
  1434.                 if (p1[3] == (unsigned char) exactn && p1[5] != c)
  1435.                   p[-3] = (unsigned char) finalize_jump;
  1436.                 else if (p1[3] == (unsigned char) charset
  1437.                          || p1[3] == (unsigned char) charset_not)
  1438.                   {
  1439.                     int not = p1[3] == (unsigned char) charset_not;
  1440.                     if (c < p1[4] * BYTEWIDTH
  1441.                         && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  1442.                       not = !not;
  1443.                     /* not is 1 if c would match */
  1444.                     /* That means it is not safe to finalize */
  1445.                     if (!not)
  1446.                       p[-3] = (unsigned char) finalize_jump;
  1447.                   }
  1448.               }
  1449.           }
  1450.           p -= 2;
  1451.           if (p[-1] != (unsigned char) finalize_jump)
  1452.             {
  1453.               p[-1] = (unsigned char) jump;
  1454.               goto nofinalize;
  1455.             }
  1456.  
  1457.         /* The end of a stupid repeat has a finalize-jump
  1458.            back to the start, where another failure point will be made
  1459.            which will point after all the repetitions found so far. */
  1460.  
  1461.         case finalize_jump:
  1462.           stackp -= 2;
  1463.  
  1464.         case jump:
  1465.         nofinalize:
  1466.           mcnt = *p++ & 0377;
  1467.           mcnt += SIGN_EXTEND_CHAR (*(char *)p) << 8;
  1468.           p += mcnt + 1;        /* The 1 compensates for missing ++ above */
  1469.           break;
  1470.  
  1471.         case dummy_failure_jump:
  1472.           if (stackp == stacke)
  1473.             {
  1474.               unsigned char **stackx
  1475.                 = (unsigned char **) malloc(
  1476.                                 2 * (stacke - stackb) * sizeof (char *));
  1477.               bcopy (stackb, stackx, (stacke - stackb) * sizeof (char *));
  1478.               stackp = stackx + (stackp - stackb);
  1479.               stacke = stackx + 2 * (stacke - stackb);
  1480. #ifdef AMIGA
  1481.               free(stackb);
  1482. #endif
  1483.               stackb = stackx;
  1484.             }
  1485.           *stackp++ = 0;
  1486.           *stackp++ = 0;
  1487.           goto nofinalize;
  1488.  
  1489.         case wordbound:
  1490.           if (d == string1  /* Points to first char */
  1491.               || d == end2  /* Points to end */
  1492.               || (d == end1 && size2 == 0)) /* Points to end */
  1493.             break;
  1494.           if ((SYNTAX(d[-1]) == Sword)
  1495.               != (SYNTAX(d == end1 ? *string2 : *d) == Sword))
  1496.             break;
  1497.           goto fail;
  1498.  
  1499.         case notwordbound:
  1500.           if (d == string1  /* Points to first char */
  1501.               || d == end2  /* Points to end */
  1502.               || (d == end1 && size2 == 0)) /* Points to end */
  1503.             goto fail;
  1504.           if ((SYNTAX(d[-1]) == Sword)
  1505.               != (SYNTAX(d == end1 ? *string2 : *d) == Sword))
  1506.             goto fail;
  1507.           break;
  1508.  
  1509.         case wordbeg:
  1510.           if (d == end2  /* Points to end */
  1511.               || (d == end1 && size2 == 0) /* Points to end */
  1512.               || SYNTAX(* (d == end1 ? string2 : d)) != Sword) /* Next char not a letter */
  1513.             goto fail;
  1514.           if (d == string1  /* Points to first char */
  1515.               || SYNTAX(d[-1]) != Sword)  /* prev char not letter */
  1516.             break;
  1517.           goto fail;
  1518.  
  1519.         case wordend:
  1520.           if (d == string1  /* Points to first char */
  1521.               || SYNTAX(d[-1]) != Sword)  /* prev char not letter */
  1522.             goto fail;
  1523.           if (d == end2  /* Points to end */
  1524.               || (d == end1 && size2 == 0) /* Points to end */
  1525.               || SYNTAX(d == end1 ? *string2 : *d) != Sword) /* Next char not a letter */
  1526.             break;
  1527.           goto fail;
  1528.  
  1529.         case wordchar:
  1530.           PREFETCH;
  1531.           if (SYNTAX(*d++) == 0) goto fail;
  1532.           break;
  1533.  
  1534.         case notwordchar:
  1535.           PREFETCH;
  1536.           if (SYNTAX(*d++) != 0) goto fail;
  1537.           break;
  1538.  
  1539.         case begbuf:
  1540.           if (d == string1)     /* Note, d cannot equal string2 */
  1541.             break;              /* unless string1 == string2.  */
  1542.           goto fail;
  1543.  
  1544.         case endbuf:
  1545.           if (d == end2 || (d == end1 && size2 == 0))
  1546.             break;
  1547.           goto fail;
  1548.  
  1549.         case exactn:
  1550.           /* Match the next few pattern characters exactly.
  1551.              mcnt is how many characters to match. */
  1552.           mcnt = *p++;
  1553.           if (translate)
  1554.             {
  1555.               do
  1556.                 {
  1557.                   PREFETCH;
  1558.                   if (translate[*d++] != *p++) goto fail;
  1559.                 }
  1560.               while (--mcnt);
  1561.             }
  1562.           else
  1563.             {
  1564.               do
  1565.                 {
  1566.                   PREFETCH;
  1567.                   if (*d++ != *p++) goto fail;
  1568.                 }
  1569.               while (--mcnt);
  1570.             }
  1571.           break;
  1572.         }
  1573.       continue;    /* Successfully matched one pattern command; keep matching */
  1574.  
  1575.       /* Jump here if any matching operation fails. */
  1576.     fail:
  1577.       if (stackp != stackb)
  1578.         /* A restart point is known.  Restart there and pop it. */
  1579.         {
  1580.           if (!stackp[-2])
  1581.             {   /* If innermost failure point is dormant, flush it and keep looking */
  1582.               stackp -= 2;
  1583.               goto fail;
  1584.             }
  1585.           d = *--stackp;
  1586.           p = *--stackp;
  1587.           if (d >= string1 && d <= end1)
  1588.             dend = end_match_1;
  1589.         }
  1590.       else {
  1591.         break;   /* Matching at this starting point really fails! */
  1592.       }
  1593.     }
  1594.  
  1595. #ifdef AMIGA
  1596.   if ( stackb )
  1597.     free(stackb);
  1598. #endif
  1599.  
  1600.   return(-1);         /* Failure to match */
  1601. }
  1602.  
  1603. static int
  1604. bcmp_translate (s1, s2, len, translate)
  1605. unsigned char *s1, *s2;
  1606. register long len;
  1607. unsigned char *translate;
  1608. {
  1609.   register unsigned char *p1 = s1, *p2 = s2;
  1610.   while (len)
  1611.     {
  1612.       if (translate [*p1++] != translate [*p2++])
  1613.         return(1);
  1614.       len--;
  1615.     }
  1616.   return(0);
  1617. }
  1618.  
  1619.  
  1620.  
  1621.