home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Projects / MiscFindPanel / SearchBench / regexpr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-19  |  41.3 KB  |  1,683 lines

  1. /*
  2. regexpr.c
  3.  
  4. Author: Tatu Ylonen <ylo@ngs.fi>
  5.  
  6. Copyright (c) 1991 Tatu Ylonen, Espoo, Finland
  7.  
  8. Permission to use, copy, modify, distribute, and sell this software
  9. and its documentation is hereby granted without fee, provided that the
  10. above copyright notice appears in all source code copies, the name of
  11. Tatu Ylonen is not used to advertise products containing this software
  12. or a derivation thereof, and all modified versions are clearly marked
  13. as such.
  14.  
  15. This software is provided "as is" without express or implied warranty.
  16.  
  17. Created: Thu Sep 26 17:14:05 1991 ylo
  18. Last modified: Sun Mar 29 16:47:31 1992 ylo
  19.  
  20. This code draws many ideas from the regular expression packages by
  21. Henry Spencer of the University of Toronto and Richard Stallman of the
  22. Free Software Foundation.
  23.  
  24. Emacs-specific code and syntax table code is almost directly borrowed
  25. from GNU regexp.
  26.  
  27. $Header: /u/src/lib/tools/RCS/regexpr.c,v 1.4 92/03/29 16:52:04 ylo Exp $
  28.  
  29. */
  30.  
  31. #include <stdio.h>
  32. #include <assert.h>
  33. /* Include of header file renamed for MiscKit 94/01/07 by Don Yacktman */
  34. #include <misckit/regexpr.h>
  35.  
  36. char *malloc();
  37. void free();
  38. char *realloc();
  39.  
  40. #define MACRO_BEGIN do {
  41. #define MACRO_END } while (0)
  42.  
  43. enum regexp_compiled_ops /* opcodes for compiled regexp */
  44. {
  45.   Cend,            /* end of pattern reached */
  46.   Cbol,            /* beginning of line */
  47.   Ceol,            /* end of line */
  48.   Cset,            /* character set.  Followed by 32 bytes of set. */
  49.   Cexact,        /* followed by a byte to match */
  50.   Canychar,        /* matches any character except newline */
  51.   Cstart_memory,    /* set register start addr (followed by reg number) */
  52.   Cend_memory,        /* set register end addr (followed by reg number) */
  53.   Cmatch_memory,    /* match a duplicate of reg contents (regnum follows)*/
  54.   Cjump,        /* followed by two bytes (lsb,msb) of displacement. */
  55.   Cstar_jump,        /* will change to jump/update_failure_jump at runtime */
  56.   Cfailure_jump,    /* jump to addr on failure */
  57.   Cupdate_failure_jump,    /* update topmost failure point and jump */
  58.   Cdummy_failure_jump,    /* push a dummy failure point and jump */
  59.   Cbegbuf,        /* match at beginning of buffer */
  60.   Cendbuf,        /* match at end of buffer */
  61.   Cwordbeg,        /* match at beginning of word */
  62.   Cwordend,        /* match at end of word */
  63.   Cwordbound,        /* match if at word boundary */
  64.   Cnotwordbound,    /* match if not at word boundary */
  65. #ifdef emacs
  66.   Cemacs_at_dot,    /* emacs only: matches at dot */
  67. #endif /* emacs */
  68.   Csyntaxspec,        /* matches syntax code (1 byte follows) */
  69.   Cnotsyntaxspec    /* matches if syntax code does not match (1 byte foll)*/
  70. };
  71.  
  72. enum regexp_syntax_op    /* syntax codes for plain and quoted characters */
  73. {
  74.   Rend,            /* special code for end of regexp */
  75.   Rnormal,        /* normal character */
  76.   Ranychar,        /* any character except newline */
  77.   Rquote,        /* the quote character */
  78.   Rbol,            /* match beginning of line */
  79.   Reol,            /* match end of line */
  80.   Roptional,        /* match preceding expression optionally */
  81.   Rstar,        /* match preceding expr zero or more times */
  82.   Rplus,        /* match preceding expr one or more times */
  83.   Ror,            /* match either of alternatives */
  84.   Ropenpar,        /* opening parenthesis */
  85.   Rclosepar,        /* closing parenthesis */
  86.   Rmemory,        /* match memory register */
  87.   Rextended_memory,    /* \vnn to match registers 10-99 */
  88.   Ropenset,        /* open set.  Internal syntax hard-coded below. */
  89.   /* the following are gnu extensions to "normal" regexp syntax */
  90.   Rbegbuf,        /* beginning of buffer */
  91.   Rendbuf,        /* end of buffer */
  92.   Rwordchar,        /* word character */
  93.   Rnotwordchar,        /* not word character */
  94.   Rwordbeg,        /* beginning of word */
  95.   Rwordend,        /* end of word */
  96.   Rwordbound,        /* word bound */
  97.   Rnotwordbound,    /* not word bound */
  98. #ifdef emacs
  99.   Remacs_at_dot,    /* emacs: at dot */
  100.   Remacs_syntaxspec,    /* syntaxspec */
  101.   Remacs_notsyntaxspec,    /* notsyntaxspec */
  102. #endif /* emacs */
  103.   Rnum_ops
  104. };
  105.  
  106. static int re_compile_initialized = 0;
  107. static int regexp_syntax = 0;
  108. static unsigned char regexp_plain_ops[256];
  109. static unsigned char regexp_quoted_ops[256];
  110. static unsigned char regexp_precedences[Rnum_ops];
  111. static int regexp_context_indep_ops;
  112. static int regexp_ansi_sequences;
  113.  
  114. #define NUM_LEVELS  5    /* number of precedence levels in use */
  115. #define MAX_NESTING 100  /* max nesting level of operators */
  116.  
  117. #ifdef emacs
  118.  
  119. /* This code is for emacs compatibility only. */
  120.  
  121. #include "config.h"
  122. #include "lisp.h"
  123. #include "buffer.h"
  124. #include "syntax.h"
  125.  
  126. /* emacs defines NULL in some strange way? */
  127. #undef NULL
  128. #define NULL 0
  129.  
  130. #else /* emacs */
  131.  
  132. #define SYNTAX(ch) re_syntax_table[(unsigned char)(ch)]
  133. #define Sword 1
  134.  
  135. /* 94/01/19 (cjk) made re_syntax_table unsigned */
  136. #ifdef SYNTAX_TABLE
  137. unsigned char *re_syntax_table;
  138. #else
  139. static unsigned char re_syntax_table[256];
  140. #endif /* SYNTAX_TABLE */
  141.  
  142. #endif /* emacs */
  143.  
  144. /* 94/01/19 (cjk) forward declaration for memset added */
  145. #if defined(NeXT)
  146.   void *memset(void *, unsigned char, unsigned int);
  147. #endif /* defined(NeXT) */
  148.  
  149. static void re_compile_initialize()
  150. {
  151.   int a;
  152.   
  153. #if !defined(emacs) && !defined(SYNTAX_TABLE)
  154.   static int syntax_table_inited = 0;
  155.   
  156.   if (!syntax_table_inited)
  157.     {
  158.       syntax_table_inited = 1;
  159.       memset(re_syntax_table, 0, 256);
  160.       for (a = 'a'; a <= 'z'; a++)
  161.     re_syntax_table[a] = Sword;
  162.       for (a = 'A'; a <= 'Z'; a++)
  163.     re_syntax_table[a] = Sword;
  164.       for (a = '0'; a <= '9'; a++)
  165.     re_syntax_table[a] = Sword;
  166.     }
  167. #endif /* !emacs && !SYNTAX_TABLE */
  168.   re_compile_initialized = 1;
  169.   for (a = 0; a < 256; a++)
  170.     {
  171.       regexp_plain_ops[a] = Rnormal;
  172.       regexp_quoted_ops[a] = Rnormal;
  173.     }
  174.   for (a = '0'; a <= '9'; a++)
  175.     regexp_quoted_ops[a] = Rmemory;
  176.   regexp_plain_ops['\134'] = Rquote;
  177.   if (regexp_syntax & RE_NO_BK_PARENS)
  178.     {
  179.       regexp_plain_ops['('] = Ropenpar;
  180.       regexp_plain_ops[')'] = Rclosepar;
  181.     }
  182.   else
  183.     {
  184.       regexp_quoted_ops['('] = Ropenpar;
  185.       regexp_quoted_ops[')'] = Rclosepar;
  186.     }
  187.   if (regexp_syntax & RE_NO_BK_VBAR)
  188.     regexp_plain_ops['\174'] = Ror;
  189.   else
  190.     regexp_quoted_ops['\174'] = Ror;
  191.   regexp_plain_ops['*'] = Rstar;
  192.   if (regexp_syntax & RE_BK_PLUS_QM)
  193.     {
  194.       regexp_quoted_ops['+'] = Rplus;
  195.       regexp_quoted_ops['?'] = Roptional;
  196.     }
  197.   else
  198.     {
  199.       regexp_plain_ops['+'] = Rplus;
  200.       regexp_plain_ops['?'] = Roptional;
  201.     }
  202.   if (regexp_syntax & RE_NEWLINE_OR)
  203.     regexp_plain_ops['\n'] = Ror;
  204.   regexp_plain_ops['\133'] = Ropenset;
  205.   regexp_plain_ops['\136'] = Rbol;
  206.   regexp_plain_ops['$'] = Reol;
  207.   regexp_plain_ops['.'] = Ranychar;
  208.   if (!(regexp_syntax & RE_NO_GNU_EXTENSIONS))
  209.     {
  210. #ifdef emacs
  211.       regexp_quoted_ops['='] = Remacs_at_dot;
  212.       regexp_quoted_ops['s'] = Remacs_syntaxspec;
  213.       regexp_quoted_ops['S'] = Remacs_notsyntaxspec;
  214. #endif /* emacs */
  215.       regexp_quoted_ops['w'] = Rwordchar;
  216.       regexp_quoted_ops['W'] = Rnotwordchar;
  217.       regexp_quoted_ops['<'] = Rwordbeg;
  218.       regexp_quoted_ops['>'] = Rwordend;
  219.       regexp_quoted_ops['b'] = Rwordbound;
  220.       regexp_quoted_ops['B'] = Rnotwordbound;
  221.       regexp_quoted_ops['`'] = Rbegbuf;
  222.       regexp_quoted_ops['\''] = Rendbuf;
  223.     }
  224.   if (regexp_syntax & RE_ANSI_HEX)
  225.     regexp_quoted_ops['v'] = Rextended_memory;
  226.   for (a = 0; a < Rnum_ops; a++)
  227.     regexp_precedences[a] = 4;
  228.   if (regexp_syntax & RE_TIGHT_VBAR)
  229.     {
  230.       regexp_precedences[Ror] = 3;
  231.       regexp_precedences[Rbol] = 2;
  232.       regexp_precedences[Reol] = 2;
  233.     }
  234.   else
  235.     {
  236.       regexp_precedences[Ror] = 2;
  237.       regexp_precedences[Rbol] = 3;
  238.       regexp_precedences[Reol] = 3;
  239.     }
  240.   regexp_precedences[Rclosepar] = 1;
  241.   regexp_precedences[Rend] = 0;
  242.   regexp_context_indep_ops = (regexp_syntax & RE_CONTEXT_INDEP_OPS) != 0;
  243.   regexp_ansi_sequences = (regexp_syntax & RE_ANSI_HEX) != 0;
  244. }
  245.  
  246. int re_set_syntax(syntax)
  247. int syntax;
  248. {
  249.   int ret;
  250.  
  251.   ret = regexp_syntax;
  252.   regexp_syntax = syntax;
  253.   re_compile_initialize();
  254.   return ret;
  255. }
  256.  
  257. static int hex_char_to_decimal(ch)
  258. int ch;
  259. {
  260.   if (ch >= '0' && ch <= '9')
  261.     return ch - '0';
  262.   if (ch >= 'a' && ch <= 'f')
  263.     return ch - 'a' + 10;
  264.   if (ch >= 'A' && ch <= 'F')
  265.     return ch - 'A' + 10;
  266.   return 16;
  267. }
  268.  
  269. char *re_compile_pattern(regex, size, bufp)
  270. char *regex;
  271. int size;
  272. regexp_t bufp;
  273. {
  274.   int a, pos, op, current_level, level, opcode;
  275.   int pattern_offset=0, alloc;  /* 94/01/19 (cjk) pattern_offset initialized */
  276.   int starts[NUM_LEVELS * MAX_NESTING], starts_base;
  277.   int future_jumps[MAX_NESTING], num_jumps;
  278.   unsigned char ch=0;  /* 94/01/19 (cjk) ch initialized */
  279.   char *pattern, *translate;
  280.   int next_register, paren_depth, num_open_registers, open_registers[RE_NREGS];
  281.   int beginning_context;
  282.  
  283. #define NEXTCHAR(var)            \
  284.   MACRO_BEGIN                \
  285.     if (pos >= size)            \
  286.       goto ends_prematurely;        \
  287.     (var) = regex[pos];            \
  288.     pos++;                \
  289.   MACRO_END
  290.  
  291. #define ALLOC(amount)                \
  292.   MACRO_BEGIN                    \
  293.     if (pattern_offset+(amount) > alloc)    \
  294.       {                        \
  295.     alloc += 256 + (amount);        \
  296.     pattern = realloc(pattern, alloc);    \
  297.     if (!pattern)                \
  298.       goto out_of_memory;            \
  299.       }                        \
  300.   MACRO_END
  301.  
  302. #define STORE(ch) pattern[pattern_offset++] = (ch)
  303.  
  304. #define CURRENT_LEVEL_START (starts[starts_base + current_level])
  305.  
  306. #define SET_LEVEL_START starts[starts_base + current_level] = pattern_offset
  307.  
  308. #define PUSH_LEVEL_STARTS if (starts_base < (MAX_NESTING-1)*NUM_LEVELS) \
  309.                     starts_base += NUM_LEVELS;            \
  310.                           else                        \
  311.                 goto too_complex
  312.  
  313. #define POP_LEVEL_STARTS starts_base -= NUM_LEVELS
  314.  
  315. #define PUT_ADDR(offset,addr)                \
  316.   MACRO_BEGIN                        \
  317.     int disp = (addr) - (offset) - 2;            \
  318.     pattern[(offset)] = disp & 0xff;            \
  319.     pattern[(offset)+1] = (disp>>8) & 0xff;        \
  320.   MACRO_END
  321.  
  322. #define INSERT_JUMP(pos,type,addr)            \
  323.   MACRO_BEGIN                        \
  324.     int a, p = (pos), t = (type), ad = (addr);        \
  325.     for (a = pattern_offset - 1; a >= p; a--)        \
  326.       pattern[a + 3] = pattern[a];            \
  327.     pattern[p] = t;                    \
  328.     PUT_ADDR(p+1,ad);                    \
  329.     pattern_offset += 3;                \
  330.   MACRO_END
  331.  
  332. #define SETBIT(buf,offset,bit) (buf)[(offset)+(bit)/8] |= (1<<((bit) & 7))
  333.  
  334. #define SET_FIELDS                \
  335.   MACRO_BEGIN                    \
  336.     bufp->allocated = alloc;            \
  337.     bufp->buffer = pattern;            \
  338.     bufp->used = pattern_offset;        \
  339.   MACRO_END
  340.     
  341. #define GETHEX(var)                        \
  342.   MACRO_BEGIN                            \
  343.     char gethex_ch, gethex_value;                \
  344.     NEXTCHAR(gethex_ch);                    \
  345.     gethex_value = hex_char_to_decimal(gethex_ch);        \
  346.     if (gethex_value == 16)                    \
  347.       goto hex_error;                        \
  348.     NEXTCHAR(gethex_ch);                    \
  349.     gethex_ch = hex_char_to_decimal(gethex_ch);            \
  350.     if (gethex_ch == 16)                    \
  351.       goto hex_error;                        \
  352.     (var) = gethex_value * 16 + gethex_ch;            \
  353.   MACRO_END
  354.  
  355. #define ANSI_TRANSLATE(ch)                \
  356.   MACRO_BEGIN                        \
  357.     switch (ch)                        \
  358.       {                            \
  359.       case 'a':                        \
  360.       case 'A':                        \
  361.     ch = 7; /* audible bell */            \
  362.     break;                        \
  363.       case 'b':                        \
  364.       case 'B':                        \
  365.     ch = 8; /* backspace */                \
  366.     break;                        \
  367.       case 'f':                        \
  368.       case 'F':                        \
  369.     ch = 12; /* form feed */            \
  370.     break;                        \
  371.       case 'n':                        \
  372.       case 'N':                        \
  373.     ch = 10; /* line feed */            \
  374.     break;                        \
  375.       case 'r':                        \
  376.       case 'R':                        \
  377.     ch = 13; /* carriage return */            \
  378.     break;                        \
  379.       case 't':                        \
  380.       case 'T':                        \
  381.     ch = 9; /* tab */                \
  382.     break;                        \
  383.       case 'v':                        \
  384.       case 'V':                        \
  385.     ch = 11; /* vertical tab */            \
  386.     break;                        \
  387.       case 'x': /* hex code */                \
  388.       case 'X':                        \
  389.     GETHEX(ch);                    \
  390.     break;                        \
  391.       default:                        \
  392.     /* other characters passed through */        \
  393.     if (translate)                    \
  394.       ch = translate[(unsigned char)ch];        \
  395.     break;                        \
  396.       }                            \
  397.   MACRO_END
  398.  
  399.   if (!re_compile_initialized)
  400.     re_compile_initialize();
  401.   bufp->used = 0;
  402.   bufp->fastmap_accurate = 0;
  403.   bufp->uses_registers = 0;
  404.   translate = bufp->translate;
  405.   pattern = bufp->buffer;
  406.   alloc = bufp->allocated;
  407.   if (alloc == 0 || pattern == NULL)
  408.     {
  409.       alloc = 256;
  410.       pattern = malloc(alloc);
  411.       if (!pattern)
  412.     goto out_of_memory;
  413.     }
  414.   pattern_offset = 0;
  415.   starts_base = 0;
  416.   num_jumps = 0;
  417.   current_level = 0;
  418.   SET_LEVEL_START;
  419.   num_open_registers = 0;
  420.   next_register = 1;
  421.   paren_depth = 0;
  422.   beginning_context = 1;
  423.   op = -1;
  424.   /* we use Rend dummy to ensure that pending jumps are updated (due to
  425.      low priority of Rend) before exiting the loop. */
  426.   pos = 0;
  427.   while (op != Rend)
  428.     {
  429.       if (pos >= size)
  430.     op = Rend;
  431.       else
  432.     {
  433.       NEXTCHAR(ch);
  434.       if (translate)
  435.         ch = translate[(unsigned char)ch];
  436.       op = regexp_plain_ops[(unsigned char)ch];
  437.       if (op == Rquote)
  438.         {
  439.           NEXTCHAR(ch);
  440.           op = regexp_quoted_ops[(unsigned char)ch];
  441.           if (op == Rnormal && regexp_ansi_sequences)
  442.         ANSI_TRANSLATE(ch);
  443.         }
  444.     }
  445.       level = regexp_precedences[op];
  446.       /* printf("ch='%c' op=%d level=%d current_level=%d curlevstart=%d\n",
  447.          ch, op, level, current_level, CURRENT_LEVEL_START); */
  448.       if (level > current_level)
  449.     {
  450.       for (current_level++; current_level < level; current_level++)
  451.         SET_LEVEL_START;
  452.       SET_LEVEL_START;
  453.     }
  454.       else
  455.     if (level < current_level)
  456.       {
  457.         current_level = level;
  458.         for (;num_jumps > 0 &&
  459.          future_jumps[num_jumps-1] >= CURRENT_LEVEL_START;
  460.          num_jumps--)
  461.           PUT_ADDR(future_jumps[num_jumps-1], pattern_offset);
  462.       }
  463.       switch (op)
  464.     {
  465.     case Rend:
  466.       break;
  467.     case Rnormal:
  468.     normal_char:
  469.       opcode = Cexact;
  470.     store_opcode_and_arg: /* opcode & ch must be set */
  471.       SET_LEVEL_START;
  472.       ALLOC(2);
  473.       STORE(opcode);
  474.       STORE(ch);
  475.       break;
  476.     case Ranychar:
  477.       opcode = Canychar;
  478.     store_opcode:
  479.       SET_LEVEL_START;
  480.       ALLOC(1);
  481.       STORE(opcode);
  482.       break;
  483.     case Rquote:
  484.       abort();
  485.       /*NOTREACHED*/
  486.     case Rbol:
  487.       if (!beginning_context)
  488.         if (regexp_context_indep_ops)
  489.           goto op_error;
  490.         else
  491.           goto normal_char;
  492.       opcode = Cbol;
  493.       goto store_opcode;
  494.     case Reol:
  495.       if (!((pos >= size) ||
  496.         ((regexp_syntax & RE_NO_BK_VBAR) ?
  497.          (regex[pos] == '\174') :
  498.          (pos+1 < size && regex[pos] == '\134' &&
  499.           regex[pos+1] == '\174')) ||
  500.         ((regexp_syntax & RE_NO_BK_PARENS)?
  501.          (regex[pos] == ')'):
  502.          (pos+1 < size && regex[pos] == '\134' &&
  503.           regex[pos+1] == ')'))))
  504.         if (regexp_context_indep_ops)
  505.           goto op_error;
  506.         else
  507.           goto normal_char;
  508.       opcode = Ceol;
  509.       goto store_opcode;
  510.       break;
  511.     case Roptional:
  512.       if (beginning_context)
  513.         if (regexp_context_indep_ops)
  514.           goto op_error;
  515.         else
  516.           goto normal_char;
  517.       if (CURRENT_LEVEL_START == pattern_offset)
  518.         break; /* ignore empty patterns for ? */
  519.       ALLOC(3);
  520.       INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
  521.               pattern_offset + 3);
  522.       break;
  523.     case Rstar:
  524.     case Rplus:
  525.       if (beginning_context)
  526.         if (regexp_context_indep_ops)
  527.           goto op_error;
  528.         else
  529.           goto normal_char;
  530.       if (CURRENT_LEVEL_START == pattern_offset)
  531.         break; /* ignore empty patterns for + and * */
  532.       ALLOC(9);
  533.       INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
  534.               pattern_offset + 6);
  535.       INSERT_JUMP(pattern_offset, Cstar_jump, CURRENT_LEVEL_START);
  536.       if (op == Rplus)  /* jump over initial failure_jump */
  537.         INSERT_JUMP(CURRENT_LEVEL_START, Cdummy_failure_jump,
  538.             CURRENT_LEVEL_START + 6);
  539.       break;
  540.     case Ror:
  541.       ALLOC(6);
  542.       INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
  543.               pattern_offset + 6);
  544.       if (num_jumps >= MAX_NESTING)
  545.         goto too_complex;
  546.       STORE(Cjump);
  547.       future_jumps[num_jumps++] = pattern_offset;
  548.       STORE(0);
  549.       STORE(0);
  550.       SET_LEVEL_START;
  551.       break;
  552.     case Ropenpar:
  553.       SET_LEVEL_START;
  554.       if (next_register < RE_NREGS)
  555.         {
  556.           bufp->uses_registers = 1;
  557.           ALLOC(2);
  558.           STORE(Cstart_memory);
  559.           STORE(next_register);
  560.           open_registers[num_open_registers++] = next_register;
  561.           next_register++;
  562.         }
  563.       paren_depth++;
  564.       PUSH_LEVEL_STARTS;
  565.       current_level = 0;
  566.       SET_LEVEL_START;
  567.       break;
  568.     case Rclosepar:
  569.       if (paren_depth <= 0)
  570.         goto parenthesis_error;
  571.       POP_LEVEL_STARTS;
  572.       current_level = regexp_precedences[Ropenpar];
  573.       paren_depth--;
  574.       if (paren_depth < num_open_registers)
  575.         {
  576.           bufp->uses_registers = 1;
  577.           ALLOC(2);
  578.           STORE(Cend_memory);
  579.           num_open_registers--;
  580.           STORE(open_registers[num_open_registers]);
  581.         }
  582.       break;
  583.     case Rmemory:
  584.       if (ch == '0')
  585.         goto bad_match_register;
  586.       assert(ch >= '0' && ch <= '9');
  587.       bufp->uses_registers = 1;
  588.       opcode = Cmatch_memory;
  589.       ch -= '0';
  590.       goto store_opcode_and_arg;
  591.     case Rextended_memory:
  592.       NEXTCHAR(ch);
  593.       if (ch < '0' || ch > '9')
  594.         goto bad_match_register;
  595.       NEXTCHAR(a);
  596.       if (a < '0' || a > '9')
  597.         goto bad_match_register;
  598.       ch = 10 * (a - '0') + ch - '0';
  599.       if (ch <= 0 || ch >= RE_NREGS)
  600.         goto bad_match_register;
  601.       bufp->uses_registers = 1;
  602.       opcode = Cmatch_memory;
  603.       goto store_opcode_and_arg;
  604.     case Ropenset:
  605.       {
  606.         int complement,prev,offset,range,firstchar;
  607.         
  608.         SET_LEVEL_START;
  609.         ALLOC(1+256/8);
  610.         STORE(Cset);
  611.         offset = pattern_offset;
  612.         for (a = 0; a < 256/8; a++)
  613.           STORE(0);
  614.         NEXTCHAR(ch);
  615.         if (translate)
  616.           ch = translate[(unsigned char)ch];
  617.         if (ch == '\136')
  618.           {
  619.         complement = 1;
  620.         NEXTCHAR(ch);
  621.         if (translate)
  622.           ch = translate[(unsigned char)ch];
  623.           }
  624.         else
  625.           complement = 0;
  626.         prev = -1;
  627.         range = 0;
  628.         firstchar = 1;
  629.         while (ch != '\135' || firstchar)
  630.           {
  631.         firstchar = 0;
  632.         if (regexp_ansi_sequences && ch == '\134')
  633.           {
  634.             NEXTCHAR(ch);
  635.             ANSI_TRANSLATE(ch);
  636.           }
  637.         if (range)
  638.           {
  639.             for (a = prev; a <= ch; a++)
  640.               SETBIT(pattern, offset, a);
  641.             prev = -1;
  642.             range = 0;
  643.           }
  644.         else
  645.           if (prev != -1 && ch == '-')
  646.             range = 1;
  647.           else
  648.             {
  649.               SETBIT(pattern, offset, ch);
  650.               prev = ch;
  651.             }
  652.         NEXTCHAR(ch);
  653.         if (translate)
  654.           ch = translate[(unsigned char)ch];
  655.           }
  656.         if (range)
  657.           SETBIT(pattern, offset, '-');
  658.         if (complement)
  659.           {
  660.         for (a = 0; a < 256/8; a++)
  661.           pattern[offset+a] ^= 0xff;
  662.           }
  663.         break;
  664.       }
  665.     case Rbegbuf:
  666.       opcode = Cbegbuf;
  667.       goto store_opcode;
  668.     case Rendbuf:
  669.       opcode = Cendbuf;
  670.       goto store_opcode;
  671.     case Rwordchar:
  672.       opcode = Csyntaxspec;
  673.       ch = Sword;
  674.       goto store_opcode_and_arg;
  675.     case Rnotwordchar:
  676.       opcode = Cnotsyntaxspec;
  677.       ch = Sword;
  678.       goto store_opcode_and_arg;
  679.     case Rwordbeg:
  680.       opcode = Cwordbeg;
  681.       goto store_opcode;
  682.     case Rwordend:
  683.       opcode = Cwordend;
  684.       goto store_opcode;
  685.     case Rwordbound:
  686.       opcode = Cwordbound;
  687.       goto store_opcode;
  688.     case Rnotwordbound:
  689.       opcode = Cnotwordbound;
  690.       goto store_opcode;
  691. #ifdef emacs
  692.     case Remacs_at_dot:
  693.       opcode = Cemacs_at_dot;
  694.       goto store_opcode;
  695.     case Remacs_syntaxspec:
  696.       NEXTCHAR(ch);
  697.       if (translate)
  698.         ch = translate[(unsigned char)ch];
  699.       opcode = Csyntaxspec;
  700.       ch = syntax_spec_code[(unsigned char)ch];
  701.       goto store_opcode_and_arg;
  702.     case Remacs_notsyntaxspec:
  703.       NEXTCHAR(ch);
  704.       if (translate)
  705.         ch = translate[(unsigned char)ch];
  706.       opcode = Cnotsyntaxspec;
  707.       ch = syntax_spec_code[(unsigned char)ch];
  708.       goto store_opcode_and_arg;
  709. #endif /* emacs */
  710.     default:
  711.       abort();
  712.     }
  713.       beginning_context = (op == Ropenpar || op == Ror);
  714.     }
  715.   if (starts_base != 0)
  716.     goto parenthesis_error;
  717.   assert(num_jumps == 0);
  718.   ALLOC(1);
  719.   STORE(Cend);
  720.   SET_FIELDS;
  721.   return NULL;
  722.  
  723.  op_error:
  724.   SET_FIELDS;
  725.   return "Badly placed special character";
  726.  
  727.  bad_match_register:
  728.   SET_FIELDS;
  729.   return "Bad match register number";
  730.  
  731.  hex_error:
  732.   SET_FIELDS;
  733.   return "Bad hexadecimal number";
  734.  
  735.  parenthesis_error:
  736.   SET_FIELDS;
  737.   return "Badly placed parenthesis";
  738.  
  739.  out_of_memory:
  740.   SET_FIELDS;
  741.   return "Out of memory";
  742.  
  743.  ends_prematurely:
  744.   SET_FIELDS;
  745.   return "Regular expression ends prematurely";
  746.  
  747.  too_complex:
  748.   SET_FIELDS;
  749.   return "Regular expression too complex";
  750. }
  751. #undef CHARAT
  752. #undef NEXTCHAR
  753. #undef GETHEX
  754. #undef ALLOC
  755. #undef STORE
  756. #undef CURRENT_LEVEL_START
  757. #undef SET_LEVEL_START
  758. #undef PUSH_LEVEL_STARTS
  759. #undef POP_LEVEL_STARTS
  760. #undef PUT_ADDR
  761. #undef INSERT_JUMP
  762. #undef SETBIT
  763. #undef SET_FIELDS
  764.  
  765. static void re_compile_fastmap_aux(code, pos, visited, can_be_null, fastmap)
  766. char *code, *visited, *can_be_null, *fastmap;
  767. int pos;
  768. {
  769.   int a, b, syntaxcode;
  770.  
  771.   if (visited[pos])
  772.     return;  /* we have already been here */
  773.   visited[pos] = 1;
  774.   for (;;)
  775.     switch (code[pos++])
  776.       {
  777.       case Cend:
  778.     *can_be_null = 1;
  779.     return;
  780.       case Cbol:
  781.       case Cbegbuf:
  782.       case Cendbuf:
  783.       case Cwordbeg:
  784.       case Cwordend:
  785.       case Cwordbound:
  786.       case Cnotwordbound:
  787. #ifdef emacs
  788.       case Cemacs_at_dot:
  789. #endif /* emacs */
  790.     break;
  791.       case Csyntaxspec:
  792.     syntaxcode = code[pos++];
  793.     for (a = 0; a < 256; a++)
  794.       if (SYNTAX(a) == syntaxcode)
  795.         fastmap[a] = 1;
  796.     return;
  797.       case Cnotsyntaxspec:
  798.     syntaxcode = code[pos++];
  799.     for (a = 0; a < 256; a++)
  800.       if (SYNTAX(a) != syntaxcode)
  801.         fastmap[a] = 1;
  802.     return;
  803.       case Ceol:
  804.     fastmap['\n'] = 1;
  805.     if (*can_be_null == 0)
  806.       *can_be_null = 2;  /* can match null, but only at end of buffer*/
  807.     return;
  808.       case Cset:
  809.     for (a = 0; a < 256/8; a++)
  810.       if (code[pos + a] != 0)
  811.         for (b = 0; b < 8; b++)
  812.           if (code[pos + a] & (1 << b))
  813.         fastmap[(a << 3) + b] = 1;
  814.     pos += 256/8;
  815.     return;
  816.       case Cexact:
  817.     fastmap[(unsigned char)code[pos]] = 1;
  818.     return;
  819.       case Canychar:
  820.     for (a = 0; a < 256; a++)
  821.       if (a != '\n')
  822.         fastmap[a] = 1;
  823.     return;
  824.       case Cstart_memory:
  825.       case Cend_memory:
  826.     pos++;
  827.     break;
  828.       case Cmatch_memory:
  829.     for (a = 0; a < 256; a++)
  830.       fastmap[a] = 1;
  831.     *can_be_null = 1;
  832.     return;
  833.       case Cjump:
  834.       case Cdummy_failure_jump:
  835.       case Cupdate_failure_jump:
  836.       case Cstar_jump:
  837.     a = (unsigned char)code[pos++];
  838.     a |= (unsigned char)code[pos++] << 8;
  839.     pos += (int)(short)a;
  840.     if (visited[pos])
  841.       {
  842.         /* argh... the regexp contains empty loops.  This is not
  843.            good, as this may cause a failure stack overflow when
  844.            matching.  Oh well. */
  845.         /* this path leads nowhere; pursue other paths. */
  846.         return;
  847.       }
  848.     visited[pos] = 1;
  849.     break;
  850.       case Cfailure_jump:
  851.     a = (unsigned char)code[pos++];
  852.     a |= (unsigned char)code[pos++] << 8;
  853.     a = pos + (int)(short)a;
  854.     re_compile_fastmap_aux(code, a, visited, can_be_null, fastmap);
  855.     break;
  856.       default:
  857.     abort();  /* probably some opcode is missing from this switch */
  858.     /*NOTREACHED*/
  859.       }
  860. }
  861.  
  862. static int re_do_compile_fastmap(buffer, used, pos, can_be_null, fastmap)
  863. char *buffer, *fastmap, *can_be_null;
  864. int used, pos;
  865. {
  866.   char small_visited[512], *visited;
  867.  
  868.   if (used <= sizeof(small_visited))
  869.     visited = small_visited;
  870.   else
  871.     {
  872.       visited = malloc(used);
  873.       if (!visited)
  874.     return 0;
  875.     }
  876.   *can_be_null = 0;
  877.   memset(fastmap, 0, 256);
  878.   memset(visited, 0, used);
  879.   re_compile_fastmap_aux(buffer, pos, visited, can_be_null, fastmap);
  880.   if (visited != small_visited)
  881.     free(visited);
  882.   return 1;
  883. }
  884.  
  885. void re_compile_fastmap(bufp)
  886. regexp_t bufp;
  887. {
  888.   if (!bufp->fastmap || bufp->fastmap_accurate)
  889.     return;
  890.   assert(bufp->used > 0);
  891.   if (!re_do_compile_fastmap(bufp->buffer, bufp->used, 0, &bufp->can_be_null,
  892.                  bufp->fastmap))
  893.     return;
  894.   if (bufp->buffer[0] == Cbol)
  895.     bufp->anchor = 1;   /* begline */
  896.   else
  897.     if (bufp->buffer[0] == Cbegbuf)
  898.       bufp->anchor = 2; /* begbuf */
  899.     else
  900.       bufp->anchor = 0; /* none */
  901.   bufp->fastmap_accurate = 1;
  902. }
  903.  
  904. #define INITIAL_FAILURES  128  /* initial # failure points to allocate */
  905. #define MAX_FAILURES     4100  /* max # of failure points before failing */
  906.  
  907. int re_match_pattern_2(bufp, string1, size1, string2, size2, pos, regs, mstop)
  908. regexp_t bufp;
  909. char *string1, *string2;
  910. int size1, size2, pos, mstop;
  911. regexp_registers_t regs;
  912. {
  913.   struct failure_point { char *text, *partend, *code; }
  914.     *failure_stack_start, *failure_sp, *failure_stack_end,
  915.     initial_failure_stack[INITIAL_FAILURES];
  916.   char *code, *translate, *text, *textend, *partend, *part_2_end;
  917.   char *regstart_text[RE_NREGS], *regstart_partend[RE_NREGS];
  918.   char *regend_text[RE_NREGS], *regend_partend[RE_NREGS];
  919.   int a, b, ch, reg, regch, match_end;
  920.   char *regtext, *regpartend, *regtextend;
  921.  
  922. #define PREFETCH                    \
  923.   MACRO_BEGIN                        \
  924.     if (text == partend)                \
  925.       {                            \
  926.     if (text == textend)                \
  927.       goto fail;                    \
  928.     text = string2;                    \
  929.     partend = part_2_end;                \
  930.       }                            \
  931.   MACRO_END
  932.  
  933. #define NEXTCHAR(var)                \
  934.   MACRO_BEGIN                    \
  935.     PREFETCH;                    \
  936.     (var) = (unsigned char)*text++;        \
  937.     if (translate)                \
  938.       (var) = (unsigned char)translate[(var)];    \
  939.   MACRO_END
  940.  
  941.   assert(pos >= 0 && size1 >= 0 && size2 >= 0 && mstop >= 0);
  942.   assert(mstop <= size1 + size2);
  943.   assert(pos <= mstop);
  944.  
  945.   if (pos <= size1)
  946.     {
  947.       text = string1 + pos;
  948.       if (mstop <= size1)
  949.     {
  950.       partend = string1 + mstop;
  951.       textend = partend;
  952.     }
  953.       else
  954.     {
  955.       partend = string1 + size1;
  956.       textend = string2 + mstop - size1;
  957.     }
  958.       part_2_end = string2 + mstop - size1;
  959.     }
  960.   else
  961.     {
  962.       text = string2 + pos - size1;
  963.       partend = string2 + mstop - size1;
  964.       textend = partend;
  965.       part_2_end = partend;
  966.     }
  967.  
  968.   if (bufp->uses_registers && regs != NULL)
  969.     for (a = 0; a < RE_NREGS; a++)
  970.       regend_text[a] = NULL;
  971.  
  972.   code = bufp->buffer;
  973.   translate = bufp->translate;
  974.   failure_stack_start = failure_sp = initial_failure_stack;
  975.   failure_stack_end = initial_failure_stack + INITIAL_FAILURES;
  976.  
  977. #if 0
  978.   /* re_search_pattern_2 has already done this, and otherwise we get little benefit
  979.      from this.  So I'll leave this out. */
  980.   if (bufp->fastmap_accurate && !bufp->can_be_null &&
  981.       text != textend &&
  982.       !bufp->fastmap[translate ?
  983.              (unsigned char)translate[(unsigned char)*text] :
  984.              (unsigned char)*text])
  985.     return -1;  /* it can't possibly match */
  986. #endif
  987.  
  988.  continue_matching:
  989.   for (;;)
  990.     {
  991.       switch (*code++)
  992.     {
  993.     case Cend:
  994.       if (partend != part_2_end)
  995.         match_end = text - string1;
  996.       else
  997.         match_end = text - string2 + size1;
  998.       if (regs)
  999.         {
  1000.           regs->start[0] = pos;
  1001.           regs->end[0] = match_end;
  1002.           if (!bufp->uses_registers)
  1003.         {
  1004.           for (a = 1; a < RE_NREGS; a++)
  1005.             {
  1006.               regs->start[a] = -1;
  1007.               regs->end[a] = -1;
  1008.             }
  1009.         }
  1010.           else
  1011.         {
  1012.           for (a = 1; a < RE_NREGS; a++)
  1013.             {
  1014.               if (regend_text[a] == NULL)
  1015.             {
  1016.               regs->start[a] = -1;
  1017.               regs->end[a] = -1;
  1018.               continue;
  1019.             }
  1020.               if (regstart_partend[a] != part_2_end)
  1021.             regs->start[a] = regstart_text[a] - string1;
  1022.               else
  1023.             regs->start[a] = regstart_text[a] - string2 + size1;
  1024.               if (regend_partend[a] != part_2_end)
  1025.             regs->end[a] = regend_text[a] - string1;
  1026.               else
  1027.             regs->end[a] = regend_text[a] - string2 + size1;
  1028.             }
  1029.         }
  1030.         }
  1031.       if (failure_stack_start != initial_failure_stack)
  1032.         free((char *)failure_stack_start);
  1033.       return match_end - pos;
  1034.     case Cbol:
  1035.       if (text == string1 || text[-1] == '\n') /* text[-1] always valid */
  1036.         break;
  1037.       goto fail;
  1038.     case Ceol:
  1039.       if (text == string2 + size2 ||
  1040.           (text == string1 + size1 ?
  1041.            (size2 == 0 || *string2 == '\n') :
  1042.            *text == '\n'))
  1043.         break;
  1044.       goto fail;
  1045.     case Cset:
  1046.       NEXTCHAR(ch);
  1047.       if (code[ch/8] & (1<<(ch & 7)))
  1048.         {
  1049.           code += 256/8;
  1050.           break;
  1051.         }
  1052.       goto fail;
  1053.     case Cexact:
  1054.       NEXTCHAR(ch);
  1055.       if (ch != (unsigned char)*code++)
  1056.         goto fail;
  1057.       break;
  1058.     case Canychar:
  1059.       NEXTCHAR(ch);
  1060.       if (ch == '\n')
  1061.         goto fail;
  1062.       break;
  1063.     case Cstart_memory:
  1064.       reg = *code++;
  1065.       regstart_text[reg] = text;
  1066.       regstart_partend[reg] = partend;
  1067.       break;
  1068.     case Cend_memory:
  1069.       reg = *code++;
  1070.       regend_text[reg] = text;
  1071.       regend_partend[reg] = partend;
  1072.       break;
  1073.     case Cmatch_memory:
  1074.       reg = *code++;
  1075.       if (regend_text[reg] == NULL)
  1076.         goto fail;  /* or should we just match nothing? */
  1077.       regtext = regstart_text[reg];
  1078.       regtextend = regend_text[reg];
  1079.       if (regstart_partend[reg] == regend_partend[reg])
  1080.         regpartend = regtextend;
  1081.       else
  1082.         regpartend = string1 + size1;
  1083.       
  1084.       for (;regtext != regtextend;)
  1085.         {
  1086.           NEXTCHAR(ch);
  1087.           if (regtext == regpartend)
  1088.         regtext = string2;
  1089.           regch = (unsigned char)*regtext++;
  1090.           if (translate)
  1091.         regch = (unsigned char)translate[regch];
  1092.           if (regch != ch)
  1093.         goto fail;
  1094.         }
  1095.       break;
  1096.     case Cstar_jump:
  1097.       /* star is coded as:
  1098.            1: failure_jump 2
  1099.               ... code for operand of star
  1100.           star_jump 1
  1101.            2: ... code after star
  1102.          We change the star_jump to update_failure_jump if we can determine
  1103.          that it is safe to do so; otherwise we change it to an ordinary
  1104.          jump.
  1105.          plus is coded as
  1106.               jump 2
  1107.            1: failure_jump 3
  1108.            2: ... code for operand of plus
  1109.               star_jump 1
  1110.            3: ... code after plus
  1111.          For star_jump considerations this is processed identically
  1112.          to star. */
  1113.       a = (unsigned char)*code++;
  1114.       a |= (unsigned char)*code++ << 8;
  1115.       a = (int)(short)a;
  1116.       {
  1117.         char map[256], can_be_null;
  1118.         char *p1, *p2;
  1119.  
  1120.         p1 = code + a + 3; /* skip the failure_jump */
  1121.         assert(p1[-3] == Cfailure_jump);
  1122.         p2 = code;
  1123.         /* p1 points inside loop, p2 points to after loop */
  1124.         if (!re_do_compile_fastmap(bufp->buffer, bufp->used,
  1125.                        p2 - bufp->buffer, &can_be_null, map))
  1126.           goto make_normal_jump;
  1127.         /* If we might introduce a new update point inside the loop,
  1128.            we can't optimize because then update_jump would update a
  1129.            wrong failure point.  Thus we have to be quite careful here. */
  1130.       loop_p1:
  1131.         /* loop until we find something that consumes a character */
  1132.         switch (*p1++)
  1133.           {
  1134.               case Cbol:
  1135.               case Ceol:
  1136.               case Cbegbuf:
  1137.               case Cendbuf:
  1138.               case Cwordbeg:
  1139.               case Cwordend:
  1140.               case Cwordbound:
  1141.               case Cnotwordbound:
  1142. #ifdef emacs
  1143.               case Cemacs_at_dot:
  1144. #endif /* emacs */
  1145.                 goto loop_p1;
  1146.               case Cstart_memory:
  1147.               case Cend_memory:
  1148.                 p1++;
  1149.                 goto loop_p1;
  1150.           case Cexact:
  1151.         ch = (unsigned char)*p1++;
  1152.         if (map[ch])
  1153.           goto make_normal_jump;
  1154.         break;
  1155.           case Canychar:
  1156.         for (b = 0; b < 256; b++)
  1157.           if (b != '\n' && map[b])
  1158.             goto make_normal_jump;
  1159.         break;
  1160.           case Cset:
  1161.         for (b = 0; b < 256; b++)
  1162.           if ((p1[b >> 3] & (1 << (b & 7))) && map[b])
  1163.             goto make_normal_jump;
  1164.         p1 += 256/8;
  1165.         break;
  1166.           default:
  1167.         goto make_normal_jump;
  1168.           }
  1169.         /* now we know that we can't backtrack. */
  1170.         while (p1 != p2 - 3)
  1171.           {
  1172.         switch (*p1++)
  1173.           {
  1174.           case Cend:
  1175.             abort();  /* we certainly shouldn't get this inside loop */
  1176.             /*NOTREACHED*/
  1177.           case Cbol:
  1178.           case Ceol:
  1179.           case Canychar:
  1180.           case Cbegbuf:
  1181.           case Cendbuf:
  1182.           case Cwordbeg:
  1183.           case Cwordend:
  1184.           case Cwordbound:
  1185.           case Cnotwordbound:
  1186. #ifdef emacs
  1187.           case Cemacs_at_dot:
  1188. #endif /* emacs */
  1189.             break;
  1190.           case Cset:
  1191.             p1 += 256/8;
  1192.             break;
  1193.           case Cexact:
  1194.           case Cstart_memory:
  1195.           case Cend_memory:
  1196.           case Cmatch_memory:
  1197.           case Csyntaxspec:
  1198.           case Cnotsyntaxspec:
  1199.             p1++;
  1200.             break;
  1201.           case Cjump:
  1202.           case Cstar_jump:
  1203.           case Cfailure_jump:
  1204.           case Cupdate_failure_jump:
  1205.           case Cdummy_failure_jump:
  1206.             goto make_normal_jump;
  1207.           default:
  1208.             printf("regexpr.c: processing star_jump: unknown op %d\n", p1[-1]);
  1209.             break;
  1210.           }
  1211.           }
  1212.         goto make_update_jump;
  1213.       }
  1214.     make_normal_jump:
  1215.       /* printf("changing to normal jump\n"); */
  1216.       code -= 3;
  1217.       *code = Cjump;
  1218.       break;
  1219.     make_update_jump:
  1220.       /* printf("changing to update jump\n"); */
  1221.       code -= 2;
  1222.       a += 3;  /* jump to after the Cfailure_jump */
  1223.       code[-1] = Cupdate_failure_jump;
  1224.       code[0] = a & 0xff;
  1225.       code[1] = a >> 8;
  1226.       /* fall to next case */
  1227.     case Cupdate_failure_jump:
  1228.       failure_sp[-1].text = text;
  1229.       failure_sp[-1].partend = partend;
  1230.       /* fall to next case */
  1231.     case Cjump:
  1232.       a = (unsigned char)*code++;
  1233.       a |= (unsigned char)*code++ << 8;
  1234.       code += (int)(short)a;
  1235.       break;
  1236.     case Cdummy_failure_jump:
  1237.     case Cfailure_jump:
  1238.       if (failure_sp == failure_stack_end)
  1239.         {
  1240.           if (failure_stack_start != initial_failure_stack)
  1241.         goto error;
  1242.           failure_stack_start = (struct failure_point *)
  1243.         malloc(MAX_FAILURES * sizeof(*failure_stack_start));
  1244.           failure_stack_end = failure_stack_start + MAX_FAILURES;
  1245.           memcpy((char *)failure_stack_start, (char *)initial_failure_stack,
  1246.              INITIAL_FAILURES * sizeof(*failure_stack_start));
  1247.           failure_sp = failure_stack_start + INITIAL_FAILURES;
  1248.         }
  1249.       a = (unsigned char)*code++;
  1250.       a |= (unsigned char)*code++ << 8;
  1251.       a = (int)(short)a;
  1252.       if (code[-3] == Cdummy_failure_jump)
  1253.         { /* this is only used in plus */
  1254.           assert(*code == Cfailure_jump);
  1255.           b = (unsigned char)code[1];
  1256.           b |= (unsigned char)code[2] << 8;
  1257.           failure_sp->code = code + (int)(short)b + 3;
  1258.           failure_sp->text = NULL;
  1259.           code += a;
  1260.         }
  1261.       else
  1262.         {
  1263.           failure_sp->code = code + a;
  1264.           failure_sp->text = text;
  1265.           failure_sp->partend = partend;
  1266.         }
  1267.       failure_sp++;
  1268.       break;
  1269.     case Cbegbuf:
  1270.       if (text == string1)
  1271.         break;
  1272.       goto fail;
  1273.     case Cendbuf:
  1274.       if (size2 == 0 ? text == string1 + size1 : text == string2 + size2)
  1275.         break;
  1276.       goto fail;
  1277.     case Cwordbeg:
  1278.       if (text == string2 + size2)
  1279.         goto fail;
  1280.       if (size2 == 0 && text == string1 + size1)
  1281.         goto fail;
  1282.       if (SYNTAX(text == string1 + size1 ? *string1 : *text) != Sword)
  1283.         goto fail;
  1284.       if (text == string1)
  1285.         break;
  1286.       if (SYNTAX(text[-1]) != Sword)
  1287.         break;
  1288.       goto fail;
  1289.     case Cwordend:
  1290.       if (text == string1)
  1291.         goto fail;
  1292.       if (SYNTAX(text[-1]) != Sword)
  1293.         goto fail;
  1294.       if (text == string2 + size2)
  1295.         break;
  1296.       if (size2 == 0 && text == string1 + size1)
  1297.         break;
  1298.       if (SYNTAX(*text) == Sword)
  1299.         goto fail;
  1300.       break;
  1301.     case Cwordbound:
  1302.       /* Note: as in gnu regexp, this also matches at the beginning
  1303.          and end of buffer. */
  1304.       if (text == string1 || text == string2 + size2 ||
  1305.           (size2 == 0 && text == string1 + size1))
  1306.         break;
  1307.       if ((SYNTAX(text[-1]) == Sword) ^
  1308.           (SYNTAX(text == string1 + size1 ? *string2 : *text) == Sword))
  1309.         break;
  1310.       goto fail;
  1311.     case Cnotwordbound:
  1312.       /* Note: as in gnu regexp, this never matches at the beginning
  1313.          and end of buffer. */
  1314.       if (text == string1 || text == string2 + size2 ||
  1315.           (size2 == 0 && text == string1 + size1))
  1316.         goto fail;
  1317.       if (!((SYNTAX(text[-1]) == Sword) ^
  1318.         (SYNTAX(text == string1 + size1 ? *string2 : *text) == Sword)))
  1319.         goto fail;
  1320.       break;
  1321.     case Csyntaxspec:
  1322.       NEXTCHAR(ch);
  1323.       if (SYNTAX(ch) != (unsigned char)*code++)
  1324.         goto fail;
  1325.       break;
  1326.     case Cnotsyntaxspec:
  1327.       NEXTCHAR(ch);
  1328.       if (SYNTAX(ch) != (unsigned char)*code++)
  1329.         break;
  1330.       goto fail;
  1331. #ifdef emacs
  1332.     case Cemacs_at_dot:
  1333.       if (PTR_CHAR_POS((unsigned char *)text) + 1 != point)
  1334.         goto fail;
  1335.       break;
  1336. #endif /* emacs */
  1337.     default:
  1338.       abort();
  1339.       /*NOTREACHED*/
  1340.     }
  1341.     }
  1342.   abort();
  1343.   /*NOTREACHED*/
  1344.  
  1345.  fail:
  1346.   if (failure_sp != failure_stack_start)
  1347.     {
  1348.       failure_sp--;
  1349.       text = failure_sp->text;
  1350.       if (text == NULL)
  1351.     goto fail;
  1352.       partend = failure_sp->partend;
  1353.       code = failure_sp->code;
  1354.       goto continue_matching;
  1355.     }
  1356.   if (failure_stack_start != initial_failure_stack)
  1357.     free((char *)failure_stack_start);
  1358.   return -1;
  1359.  
  1360.  error:
  1361.   if (failure_stack_start != initial_failure_stack)
  1362.     free((char *)failure_stack_start);
  1363.   return -2;
  1364. }
  1365.  
  1366. #undef PREFETCH
  1367. #undef NEXTCHAR
  1368. #undef PUSH_FAILURE
  1369.  
  1370. int re_match_pattern(bufp, string, size, pos, regs)
  1371. regexp_t bufp;
  1372. char *string;
  1373. int size, pos;
  1374. regexp_registers_t regs;
  1375. {
  1376.   return re_match_pattern_2(bufp, string, size, (char *)NULL, 0, pos, regs, size);
  1377. }
  1378.  
  1379. int re_search_pattern_2(bufp, string1, size1, string2, size2, pos, range, regs,
  1380.         mstop)
  1381. regexp_t bufp;
  1382. char *string1, *string2;
  1383. int size1, size2, pos, range, mstop;
  1384. regexp_registers_t regs;
  1385. {
  1386.   char *fastmap, *translate, *text, *partstart, *partend;
  1387.   int dir, ret;
  1388.   char anchor;
  1389.   
  1390.   assert(size1 >= 0 && size2 >= 0 && pos >= 0 && mstop >= 0);
  1391.   assert(pos + range >= 0 && pos + range <= size1 + size2);
  1392.   assert(pos <= mstop);
  1393.   
  1394.   fastmap = bufp->fastmap;
  1395.   translate = bufp->translate;
  1396.   if (fastmap && !bufp->fastmap_accurate)
  1397.     re_compile_fastmap(bufp);
  1398.   anchor = bufp->anchor;
  1399.   if (bufp->can_be_null == 1) /* can_be_null == 2: can match null at eob */
  1400.     fastmap = NULL;
  1401.   if (range < 0)
  1402.     {
  1403.       dir = -1;
  1404.       range = -range;
  1405.     }
  1406.   else
  1407.     dir = 1;
  1408.   if (anchor == 2)
  1409.     if (pos != 0)
  1410.       return -1;
  1411.     else
  1412.       range = 0;
  1413.   for (; range >= 0; range--, pos += dir)
  1414.     {
  1415.       if (fastmap)
  1416.     {
  1417.       if (dir == 1)
  1418.         { /* searching forwards */
  1419.           if (pos < size1)
  1420.         {
  1421.           text = string1 + pos;
  1422.           if (pos + range > size1)
  1423.             partend = string1 + size1;
  1424.           else
  1425.             partend = string1 + pos + range;
  1426.         }
  1427.           else
  1428.         {
  1429.           text = string2 + pos - size1;
  1430.           partend = string2 + pos + range - size1;
  1431.         }
  1432.           partstart = text;
  1433.           if (translate)
  1434.         while (text != partend &&
  1435.                !fastmap[(unsigned char)
  1436.                 translate[(unsigned char)*text]])
  1437.           text++;
  1438.           else
  1439.         while (text != partend && !fastmap[(unsigned char)*text])
  1440.           text++;
  1441.           pos += text - partstart;
  1442.           range -= text - partstart;
  1443.           if (pos == size1 + size2 && bufp->can_be_null == 0)
  1444.         return -1;
  1445.         }
  1446.       else
  1447.         { /* searching backwards */
  1448.           if (pos <= size1)
  1449.         {
  1450.           text = string1 + pos;
  1451.           partstart = string1 + pos - range;
  1452.         }
  1453.           else
  1454.         {
  1455.           text = string2 + pos - size1;
  1456.           if (range < pos - size1)
  1457.             partstart = string2 + pos - size1 - range;
  1458.           else
  1459.             partstart = string2;
  1460.         }
  1461.           partend = text;
  1462.           if (translate)
  1463.         while (text != partstart &&
  1464.                !fastmap[(unsigned char)
  1465.                 translate[(unsigned char)*text]])
  1466.           text--;
  1467.           else
  1468.         while (text != partstart &&
  1469.                !fastmap[(unsigned char)*text])
  1470.           text--;
  1471.           pos -= partend - text;
  1472.           range -= partend - text;
  1473.         }
  1474.     }
  1475.       if (anchor == 1)
  1476.     { /* anchored to begline */
  1477.       if (pos > 0 &&
  1478.           (pos <= size1 ? string1[pos - 1] :
  1479.            string2[pos - size1 - 1]) != '\n')
  1480.         continue;
  1481.     }
  1482.       assert(pos >= 0 && pos <= size1 + size2);
  1483.       ret = re_match_pattern_2(bufp, string1, size1, string2, size2, pos, regs, mstop);
  1484.       if (ret >= 0)
  1485.     return pos;
  1486.       if (ret == -2)
  1487.     return -2;
  1488.     }
  1489.   return -1;
  1490. }
  1491.  
  1492. int re_search_pattern(bufp, string, size, startpos, range, regs)
  1493. regexp_t bufp;
  1494. char *string;
  1495. int size, startpos, range;
  1496. regexp_registers_t regs;
  1497. {
  1498.   return re_search_pattern_2(bufp, string, size, (char *)NULL, 0,
  1499.              startpos, range, regs, size);
  1500. }
  1501.  
  1502. /* 94/01/19 (cjk) hide these on NeXT */
  1503. #if !defined(NeXT)
  1504. static struct re_pattern_buffer re_comp_buf;
  1505.  
  1506. char *re_comp(s)
  1507. char *s;
  1508. {
  1509.   if (s == NULL)
  1510.     {
  1511.       if (!re_comp_buf.buffer)
  1512.     return "Out of memory";
  1513.       return NULL;
  1514.     }
  1515.   if (!re_comp_buf.buffer)
  1516.     {
  1517.       /* the buffer will be allocated automatically */
  1518.       re_comp_buf.fastmap = malloc(256);
  1519.       re_comp_buf.translate = NULL;
  1520.     }
  1521.   return re_compile_pattern(s, strlen(s), &re_comp_buf);
  1522. }
  1523.  
  1524. int re_exec(s)
  1525. char *s;
  1526. {
  1527.   int len = strlen(s);
  1528.   
  1529.   return re_search_pattern(&re_comp_buf, s, len, 0, len, (regexp_registers_t)NULL) >= 0;
  1530. }
  1531. #endif /* !defined(NeXT) */
  1532.  
  1533. #ifdef TEST_REGEXP
  1534.  
  1535. int main()
  1536. {
  1537.   char buf[500];
  1538.   char *cp;
  1539.   struct re_pattern_buffer exp;
  1540.   struct re_registers regs;
  1541.   int a,pos;
  1542.   char fastmap[256];
  1543.  
  1544.   exp.allocated = 0;
  1545.   exp.buffer = 0;
  1546.   exp.translate = NULL;
  1547.   exp.fastmap = fastmap;
  1548.  
  1549.   /* re_set_syntax(RE_NO_BK_PARENS|RE_NO_BK_VBAR|RE_ANSI_HEX); */
  1550.  
  1551.   while (1)
  1552.     {
  1553.       printf("Enter regexp:\n");
  1554.       gets(buf);
  1555.       cp=re_compile_pattern(buf, strlen(buf), &exp);
  1556.       if (cp)
  1557.     {
  1558.       printf("Error: %s\n", cp);
  1559.       continue;
  1560.     }
  1561.       re_compile_fastmap(&exp);
  1562.       printf("dump:\n");
  1563.       for (pos = 0; pos < exp.used;)
  1564.     {
  1565.       printf("%d: ", pos);
  1566.       switch (exp.buffer[pos++])
  1567.         {
  1568.         case Cend:
  1569.           strcpy(buf, "end");
  1570.           break;
  1571.         case Cbol:
  1572.           strcpy(buf, "bol");
  1573.           break;
  1574.         case Ceol:
  1575.           strcpy(buf, "eol");
  1576.           break;
  1577.         case Cset:
  1578.           strcpy(buf, "set ");
  1579.           for (a = 0; a < 256/8; a++)
  1580.         sprintf(buf+strlen(buf)," %02x",
  1581.             (unsigned char)exp.buffer[pos++]);
  1582.           break;
  1583.         case Cexact:
  1584.           sprintf(buf, "exact '%c' 0x%x", exp.buffer[pos],
  1585.               (unsigned char)exp.buffer[pos]);
  1586.           pos++;
  1587.           break;
  1588.         case Canychar:
  1589.           strcpy(buf, "anychar");
  1590.           break;
  1591.         case Cstart_memory:
  1592.           sprintf(buf, "start_memory %d", exp.buffer[pos++]);
  1593.           break;
  1594.         case Cend_memory:
  1595.           sprintf(buf, "end_memory %d", exp.buffer[pos++]);
  1596.           break;
  1597.         case Cmatch_memory:
  1598.           sprintf(buf, "match_memory %d", exp.buffer[pos++]);
  1599.           break;
  1600.         case Cjump:
  1601.         case Cdummy_failure_jump:
  1602.         case Cstar_jump:
  1603.         case Cfailure_jump:
  1604.         case Cupdate_failure_jump:
  1605.           a = (unsigned char)exp.buffer[pos++];
  1606.           a += (unsigned char)exp.buffer[pos++] << 8;
  1607.           a = (int)(short)a;
  1608.           switch (exp.buffer[pos-3])
  1609.         {
  1610.         case Cjump:
  1611.           cp = "jump";
  1612.           break;
  1613.         case Cstar_jump:
  1614.           cp = "star_jump";
  1615.           break;
  1616.         case Cfailure_jump:
  1617.           cp = "failure_jump";
  1618.           break;
  1619.         case Cupdate_failure_jump:
  1620.           cp = "update_failure_jump";
  1621.           break;
  1622.         case Cdummy_failure_jump:
  1623.           cp = "dummy_failure_jump";
  1624.           break;
  1625.         default:
  1626.           cp = "unknown jump";
  1627.           break;
  1628.         }
  1629.           sprintf(buf, "%s %d", cp, a + pos);
  1630.           break;
  1631.         case Cbegbuf:
  1632.           strcpy(buf,"begbuf");
  1633.           break;
  1634.         case Cendbuf:
  1635.           strcpy(buf,"endbuf");
  1636.           break;
  1637.         case Cwordbeg:
  1638.           strcpy(buf,"wordbeg");
  1639.           break;
  1640.         case Cwordend:
  1641.           strcpy(buf,"wordend");
  1642.           break;
  1643.         case Cwordbound:
  1644.           strcpy(buf,"wordbound");
  1645.           break;
  1646.         case Cnotwordbound:
  1647.           strcpy(buf,"notwordbound");
  1648.           break;
  1649.         default:
  1650.           sprintf(buf, "unknown code %d",
  1651.               (unsigned char)exp.buffer[pos - 1]);
  1652.           break;
  1653.         }
  1654.       printf("%s\n", buf);
  1655.     }
  1656.       printf("can_be_null = %d uses_registers = %d anchor = %d\n",
  1657.          exp.can_be_null, exp.uses_registers, exp.anchor);
  1658.       
  1659.       printf("fastmap:");
  1660.       for (a = 0; a < 256; a++)
  1661.     if (exp.fastmap[a])
  1662.       printf(" %d", a);
  1663.       printf("\n");
  1664.       printf("Enter strings.  An empty line terminates.\n");
  1665.       while (fgets(buf, sizeof(buf), stdin))
  1666.     {
  1667.       if (buf[0] == '\n')
  1668.         break;
  1669.       a = re_search_pattern(&exp, buf, strlen(buf), 0, strlen(buf), ®s);
  1670.       printf("search returns %d\n", a);
  1671.       if (a != -1)
  1672.         {
  1673.           for (a = 0; a < RE_NREGS; a++)
  1674.         {
  1675.           printf("buf %d: %d to %d\n", a, regs.start[a], regs.end[a]);
  1676.         }
  1677.         }
  1678.     }
  1679.     }
  1680. }
  1681.  
  1682. #endif /* TEST_REGEXP */
  1683.