Google
 

Trailing-Edge - PDP-10 Archives - SRI_NIC_PERM_FS_1_19910112 - c/gnu/lib/regex.c
There are 8 other files named regex.c in the archive. Click here to see a list.
/* Extended regular expression matching and search.
   Copyright (C) 1984 Richard M. Stallman

   Permission is granted to anyone to make or distribute
   verbatim copies of this program
   provided that the copyright notice and this permission notice are preserved;
   and provided that the recipient is not asked to waive or limit his right to
   redistribute copies as permitted by this permission notice;
   and provided that anyone possessing a machine-executable copy
   is granted access to copy the source code, in machine-readable form,
   in some reasonable manner.

   Permission is granted to distribute derived works or enhanced versions of
   this program under the above conditions with the additional conditions
   that the entire derivative or enhanced work
   must be covered by a permission notice identical to this one.

   Anything distributed as part of a package containing portions derived
   from this program, which cannot in current practice perform its function
   usefully in the absence of what was derived directly from this program,
   is to be considered as forming, together with the latter,
   a single work derived from this program,
   which must be entirely covered by a permission notice identical to this one
   in order for distribution of the package to be permitted.

   This software is distributed in the hope that it will be useful,
   but there is no warranty of any sort, and no contributor accepts
   responsibility for the consequences of using this program or for whether
   it serves any purpose in particular.

 In other words, you are welcome to use, share and improve this program.
 You are forbidden to forbid anyone else to use, share and improve
 what you give them.   Help stamp out software-hoarding!  */

/* To test, compile with -Dtest.
 That turns this into a self-contained program
 which reads a pattern, describes how it compiles,
 then reads a string and searches for it.  */

#include "regex.h"

#ifdef emacs

/* The `emacs' switch turns on certain special matching commands
 that make sense only in emacs. */

#include "config.h"
#include "lisp.h"
#include "buffer.h"
#include "window.h"
#include "syntax.h"

#endif emacs

/* Number of failure points to allocate space for initially,
 when matching.  If this number is exceeded, more space is allocated,
 so it is not a hard limit.  It is set to 4 now just to test
 the code that extends the stack.  */

#ifndef NFAILURES
#define NFAILURES 4
#endif NFAILURES

/* These are the command codes that appear in compiled regular expressions, one per byte.
  Some command codes are followed by argument bytes.
  A command code can specify any interpretation whatever for its arguments.
  Zero-bytes may appear in the compiled regular expression. */

enum regexpcode
  {
    unused,
    exactn,    /* followed by one byte giving n, and then by n literal bytes */
    begline,   /* fails unless at beginning of line */
    endline,   /* fails unless at end of line */
    jump,	 /* followed by two bytes giving relative address to jump to */
    on_failure_jump,	 /* followed by two bytes giving relative address of place
		            to resume at in case of failure. */
    start_repeat,	 /* Followed by stuff to be repeated under control of a *. */
    end_repeat,		 /* Terminates stuff to be repeated. */
    skip_repeat_jump,	 /* Jump around stuff to be repeated 0 or more times. */
			 /* Followed by a two-byte jump address as for jump. */
    finalize_jump,	 /* Throw away latest failure point and then jump to address. */
    anychar,	 /* matches any one character */
    charset,     /* matches any one char belonging to specified set.
		    The following bytes specify the set.
		    The first byte is n, the number of individual characters.
		    This is followed by n individual characters.
		    Then comes m, the number of character ranges.
		    This is followed by m pairs of characters, each specifying a range.
		    The smaller of the two characters in a range comes first.
		    The range is inclusive. */
    charset_not, /* similar but match any character that is NOT one of those specified */
    start_memory, /* starts remembering the text that is matched
		    and stores it in a memory register.
		    followed by one byte containing the register number.
		    Register numbers must be in the range 0 through NREGS. */
    stop_memory, /* stops remembering the text that is matched
		    and stores it in a memory register.
		    followed by one byte containing the register number.
		    Register numbers must be in the range 0 through NREGS. */
    duplicate,    /* match a duplicate of something remembered.
		    Followed by one byte containing the index of the memory register. */
    before_dot,	 /* Succeeds if before dot */
    at_dot,	 /* Succeeds if at dot */
    after_dot,	 /* Succeeds if after dot */
    begbuf,      /* Succeeds if at beginning of buffer */
    endbuf,      /* Succeeds if at end of buffer */
    wordchar,    /* Matches any word-constituent character */
    notwordchar, /* Matches any char that is not a word-constituent */
    wordbound,   /* Succeeds if at a word boundary */
    notwordbound, /* Succeeds if not at a word boundary */
    syntaxspec,  /* Matches any character whose syntax is specified.
		    followed by a byte which contains a syntax code, Sword or such like */
    notsyntaxspec /* Matches any character whose syntax differs from the specified. */
  };
/* compile_pattern takes a regular-expression descriptor string in the user's format
  and converts it into a buffer full of byte commands for matching.

  pattern   is the address of the pattern string
  size      is the length of it.
  bufp	    is a  struct re_pattern_buffer *  which points to the info
	    on where to store the byte commands.
	    This structure contains a  char *  which points to the
	    actual space, which should have been obtained with malloc.
	    compile_pattern may use  realloc  to grow the buffer space.

  The number of bytes of commands can be found out by looking in
  the  struct re_pattern_buffer  that bufp pointed to,
  after compile_pattern returns.
*/

#define PATPUSH(ch) (*b++ = (char) ch)

#define PATFETCH(c) \
 {if (p == pend) goto end_of_pattern; \
  c = * (unsigned char *) p++; \
  if (translate) c = translate[c]; }

#define EXTEND_BUFFER \
  { old_buffer = bufp->buffer; \
    if (bufp->allocated == (1<<16)) goto too_big; \
    bufp->allocated *= 2; \
    if (bufp->allocated > (1<<16)) bufp->allocated = (1<<16); \
    if (!(bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated))) \
      goto memory_exhausted; \
    b += bufp->buffer - old_buffer; \
    if (fixup_jump) \
      fixup_jump += bufp->buffer - old_buffer; \
    if (laststart) \
      laststart += bufp->buffer - old_buffer; \
    begalt += bufp->buffer - old_buffer; \
    if (pending_exact) \
      pending_exact += bufp->buffer - old_buffer; \
    bend = bufp->buffer + bufp->allocated; \
  }

static int store_jump (), insert_repeat (), insert_stupid_repeat (), insert_fail_jump ();

char *
re_compile_pattern (pattern, size, bufp)
     char *pattern;
     int size;
     struct re_pattern_buffer *bufp;
{
  register char *b = bufp->buffer;
  register char *bend = bufp->buffer + bufp->allocated;
  register char *p = pattern;
  register char *pend = pattern + size;
  register unsigned char c;
  char *b1, *p1;
  int b1offset;
  char *translate = bufp->translate;

  /* Temporary used when buffer is made bigger. */

  char *old_buffer;

  /* address of the count-byte of the most recently inserted "exactn" command.
    This makes it possible to tell whether a new exact-match character
    can be added to that command or requires a new "exactn" command. */
     
  char *pending_exact = 0;

  /* address of the place where a forward-jump should go
    to the end of the containing expression.
    Each alternative of an "or", except the last, ends with a forward-jump
    of this sort. */

  char *fixup_jump = 0;

  /* address of start of the most recently finished expression.
    This tells postfix * where to find the start of its operand. */

  char *laststart = 0;

  /* address of beginning of regexp, or inside of last \( */

  char *begalt = b;

  /* Stack of information saved by \( and restored by \).
     Four stack elements are pushed by each \(:
       First, the value of b.
       Second, the value of fixup_jump.
       Third, the value of regnum.
       Fourth, the value of begalt.  */

  char *stackb[40];
  char **stackp = stackb;
  char **stacke = stackb + 40;

  /* Counts \('s as they are encountered.  Remembered for the matching \),
     where it becomes the "register number" to put in the stop_memory command */

  int regnum = 1;

  bufp->fastmap_accurate = 0;

  while (p != pend)
    {
      if (b - bufp->buffer > bufp->allocated - 10)
	EXTEND_BUFFER;

      PATFETCH (c);
      switch (c)
	{
	case '$':
	  /* $ means succeed if at end of line, but only in special contexts.
	    If randonly in the middle of a pattern, it is a normal character. */
	  if (p == pend || (*p == '\\' && (p[1] == ')' || p[1] == '|')))
	    {
	      PATPUSH (endline);
	      break;
	    }
	  goto normal_char;

	case '^':
	  /* ^ means succeed if at beg of line, but only if no preceding pattern. */
	  if (laststart) goto normal_char;
	  PATPUSH (begline);
	  break;

	case '*':
	  /* * means repeat previous pattern zero or more times with no backtracking.
	     If there is no previous pattern, * is not special. */
	  if (!laststart) goto normal_char;
	  store_jump (b, finalize_jump, laststart - 3);
	  b += 3;
	  insert_fail_jump (laststart, b + 3, b);
	  b += 3;
	  break;

	case '.':
	  laststart = b;
	  PATPUSH (anychar);
	  break;

	case '[':
	  laststart = b;
	  if (*p == '^')
	    PATPUSH (charset_not), p++;
	  else
	    PATPUSH (charset);
	  p1 = p;

	  /* first make sure buffer is big enough */

	  while (1)	/* find the individual chars specified and store them. */
	    {
	      PATFETCH (c);
	      if (c == ']' && p != p1 + 1) break;
	      if (*p == '-')
	        { PATFETCH (c); PATFETCH (c); }
	    }
	  while (b - bufp->buffer > bufp->allocated - (p - p1) - 10)
	    EXTEND_BUFFER;

	  /* Find all the individual characters specified, and store them,
	     preceded by a count of them. */

	  b1offset = b - bufp->buffer;	/* remember address of byte containing # individual chars. */
	  PATPUSH (0);
	  p = p1;
	  while (1)	/* find the individual chars specified and store them. */
	    {
	      PATFETCH (c);
	      if (c == ']' && p != p1 + 1) break;
	      if (*p != '-')
		PATPUSH (c);
	      else p += 2;
	    }
	  b1 = b1offset + bufp->buffer;
	  if (b - b1 - 1 > 0377)
	    goto charset_too_big;
	  *b1 = b - b1 - 1;	/* store how many individual chars there were. */

	  /* Find all the character ranges specified, and store them,
	     preceded by a count of them. */

	  b1offset = b - bufp->buffer;	/* remember address of byte containing # char ranges. */
	  PATPUSH (0);
	  p = p1;
	  while (1)	/* find the char ranges specified and store them. */
	    {
	      PATFETCH (c);
	      if (c == ']' && p != p1 + 1) break;
	      if (*p == '-')
		{
		  PATPUSH (c);
		  PATPUSH (p[1]);
		  p += 2;
		}
	    }
	  b1 = b1offset + bufp->buffer;
	  if ((b - b1 - 1) / 2 > 0377)
	    goto charset_too_big;
	  *b1 = (b - b1 - 1) / 2;	/* store how many ranges there were */
	  break;

        case '\\':
	  if (p == pend) goto invalid_pattern;
	  PATFETCH (c);
	  switch (c)
	    {
	    case '(':
	      if (stackp == stacke) goto nesting_too_deep;
	      if (regnum < RE_NREGS)
	        {
		  PATPUSH (start_memory);
		  PATPUSH (regnum);
	        }
	      *++stackp = b;
	      *++stackp = fixup_jump;
	      *++stackp = (char *) (regnum++);
	      *++stackp = begalt;
	      fixup_jump = 0;
	      laststart = 0;
	      begalt = b;
	      break;

	    case ')':
	      if (stackp == stackb) goto unmatched_close;
	      if ((char) *stackp < RE_NREGS)
		{
		  PATPUSH (stop_memory);
		  PATPUSH ((char) *stackp);
		}
	      begalt = *stackp--;
	      stackp--;
	      if (fixup_jump)
		store_jump (fixup_jump, jump, b);
	      fixup_jump = *stackp--;
	      laststart = *stackp--;
	      break;

	    case '|':
	      insert_fail_jump (begalt, b + 6, b);
	      b += 3;
	      if (fixup_jump)
		store_jump (fixup_jump, jump, b);
	      fixup_jump = b;
	      b += 3;
	      laststart = 0;
	      begalt = b;
	      break;

	    case 'r':
	      if (!laststart) goto invalid_pattern;
	      PATPUSH (end_repeat);
	      insert_repeat (laststart, b + 4, b);
	      b += 4;
	      break;

#ifdef emacs
	    case '<':
	      PATPUSH (before_dot);
	      break;

	    case '=':
	      PATPUSH (at_dot);
	      break;

	    case '>':
	      PATPUSH (after_dot);
	      break;

	    case '`':
	      PATPUSH (begbuf);
	      break;

	    case '\'':
	      PATPUSH (endbuf);
	      break;

	    case 'w':
	      laststart = b;
	      PATPUSH (wordchar);
	      break;

	    case 'W':
	      laststart = b;
	      PATPUSH (notwordchar);
	      break;

	    case 'b':
	      PATPUSH (wordbound);
	      break;

	    case 'B':
	      PATPUSH (notwordbound);
	      break;

	    case 's':	
	      laststart = b;
	      PATPUSH (syntaxspec);
	      PATFETCH (c);
	      PATPUSH (syntax_spec_code[c]);
	      break;

	    case 'S':
	      laststart = b;
	      PATPUSH (notsyntaxspec);
	      PATFETCH (c);
	      PATPUSH (syntax_spec_code[c]);
	      break;
#endif emacs

	    case '1':
	    case '2':
	    case '3':
	    case '4':
	    case '5':
	    case '6':
	    case '7':
	    case '8':
	    case '9':
	      laststart = b;
	      PATPUSH (duplicate);
	      PATPUSH (c - '1');
	      break;
	    default:
	      goto normal_char;
	    }
	  break;

	default:
	normal_char:
	  if (!pending_exact || pending_exact + *pending_exact + 1 != b
		|| *pending_exact == 0377 || *p == '*' || *p == '^')
	    {
	      laststart = b;
	      PATPUSH (exactn);
	      pending_exact = b;
	      PATPUSH (0);
	    }
	  PATPUSH (c);
	  (*pending_exact)++;
	}
    }

  if (fixup_jump)
    store_jump (fixup_jump, jump, b);

  if (stackp != stackb) goto unmatched_open;

  bufp->used = b - bufp->buffer;
  return 0;

 invalid_pattern:
  return "Invalid regular expression";

 unmatched_open:
  return "Unmatched \(";

 unmatched_close:
  return "Unmatched \)";

 end_of_pattern:
  return "Premature end of regular expression";

 charset_too_big:
  return "Charset too big";

 nesting_too_deep:
  return "Nesting too deep";

 too_big:
  return "Regular expression too big";

 memory_exhausted:
  return "Memory exhausted";
}

/* Store where `from' points a jump operation to jump to where `to' points.
  `opcode' is the opcode to store. */

static int
store_jump (from, opcode, to)
     char *from, *to;
     char opcode;
{
  from[0] = opcode;
  from[1] = (to - (from + 3)) & 0377;
  from[2] = (to - (from + 3)) >> 8;
}

static int
insert_fail_jump (from, to, current_end)
     char *from, *to, *current_end;
{
  register char *pto = current_end + 3;
  register char *pfrom = current_end;
  while (pfrom != from)
    *--pto = *--pfrom;
  store_jump (from, on_failure_jump, to);
}

static int
insert_repeat (from, to, current_end)
     char *from, *to, *current_end;
{
  register char *pto = current_end + 4;
  register char *pfrom = current_end;
  while (pfrom != from)
    *--pto = *--pfrom;
  store_jump (from, skip_repeat_jump, to);
  from[3] = (char) start_repeat;
}
/* Given a pattern, compute a fastmap from it.
 The fastmap records which of the 0400 possible characters
 can start a string that matches the pattern.
 This fastmap is used by re_search to skip quickly over totally implausible text.

 The caller must supply the address of a 0400-byte data area
 as bufp->fastmap.
 The other components of bufp describe the pattern to be used.  */

re_compile_fastmap (bufp)
     struct re_pattern_buffer *bufp;
{
  char *pattern = bufp->buffer;
  int size = bufp->used;
  register char *fastmap = bufp->fastmap;
  register char *p = pattern;
  register char *pend = pattern + size;
  register int j, k, k1;
  char *translate = bufp->translate;

  char *stackb[NFAILURES];
  char **stackp = stackb;

  char temp[0400];

  bzero (fastmap, 0400);
  bufp->fastmap_accurate = 1;
      
  while (p)
    {
      switch ((enum regexpcode) *p++)
	{
	case exactn:
	  if (translate)
	    fastmap[translate[p[1]]] = 1;
	  else
	    fastmap[p[1]] = 1;
	  break;

        case begline:
        case before_dot:
	case at_dot:
	case after_dot:
	case begbuf:
	case endbuf:
	case wordbound:
	case notwordbound:
	  continue;

	case endline:
	  if (translate)
	    fastmap[translate['\n']] = 1;
	  else
	    fastmap['\n'] = 1;
	  break;

	case jump:
	  j = *p++ & 0377;
	  j += (*p++) << 8;
	  p += j;
	  continue;

	case on_failure_jump:
	case skip_repeat_jump:
	  j = *p++ & 0377;
	  j += (*p++) << 8;
	  *++stackp = p + j;
	  continue;
	  
	case start_repeat:
	case end_repeat:
	  continue;

	case start_memory:
	case stop_memory:
	  *p++;
	  continue;

	case duplicate:
	case anychar:
	  for (j = 0; j < 0400; j++)
	    fastmap[j] = 1;
	  return;

#ifdef emacs
	case wordchar:
	  for (j = 0; j < 0400; j++)
	    if (SYNTAX (j) == Sword)
	      fastmap[j] = 1;
	  break;

	case notwordchar:
	  for (j = 0; j < 0400; j++)
	    if (SYNTAX (j) != Sword)
	      fastmap[j] = 1;
	  break;

	case syntaxspec:
	  k = *p++;
	  for (j = 0; j < 0400; j++)
	    if (SYNTAX (j) == (enum syntaxcode) k)
	      fastmap[j] = 1;
	  break;

	case notsyntaxspec:
	  for (j = 0; j < 0400; j++)
	    if (SYNTAX (j) != (enum syntaxcode) k)
	      fastmap[j] = 1;
	  break;
#endif emacs

	case charset:
	  for (j = *p++; j; j--)
	    if (translate)
	      fastmap[translate[*p++]] = 1;
	    else
	      fastmap[*p++] = 1;
	  for (j = *p++; j; j--)
	    {
	      k = *p++;
	      k1 = *p++;
	      while (k <= k1)
		if (translate)
		  fastmap[translate[k++]] = 1;
		else
		  fastmap[k++] = 1;
	    }
	  break;

	case charset_not:
	  /* First compute the set of specified characters, in temp. */
	  bzero (temp, 0400);
	  for (j = *p++; j; j--)
	    if (translate)
	      temp[translate[*p++]] = 1;
	    else
	      temp[*p++] = 1;
	  for (j = *p++; j; j--)
	    {
	      k = *p++;
	      k1 = *p++;
	      while (k <= k1)
		if (translate)
		  temp[translate[k++]] = 1;
		else
		  temp[k++] = 1;
	    }
	  /* Then any character not specified should be marked 1 in fastmap. */
	  for (j = 0; j < 0400; j++)
	    if (temp[j] == 0) fastmap[j] = 1;
	  break;
	}

      /* Get here means we have successfully found the possible starting characters
	 of one path of the pattern.  We need not follow this path any farther.
	 Instead, look at the next alternative remembered in the stack. */
      if (stackp != stackb)
	p = *stackp--;
      else
	break;
    }
}
/* Like re_search_2, below, but only one string is specified. */

re_search (pbufp, string, size, startpos, range, regs)
     struct re_pattern_buffer *pbufp;
     char *string;
     int size, startpos, range;
     struct re_registers *regs;
{
  return re_search_2 (pbufp, 0, 0, string, size, range, regs);
}

/* Like re_match_2 but tries first a match starting at index `startpos',
 then at startpos + 1, and so on.
 `range' is the number of places to try before giving up.
 If `range' is negative, the starting positions tried are
  startpos, startpos - 1, etc.
 It is up to the caller to make sure that range is not so large
  as to take the starting position outside of the input strings.

The value returned is the position at which the match was found,
 or -1 if no match was found. */

int
re_search_2 (pbufp, string1, size1, string2, size2, startpos, range, regs)
     struct re_pattern_buffer *pbufp;
     char *string1, *string2;
     int size1, size2;
     register int startpos, range;
     struct re_registers *regs;
{
  register char *fastmap = pbufp->fastmap;
  register char *translate = pbufp->translate;

  /* Update the fastmap now if not correct already */
  if (fastmap && !pbufp->fastmap_accurate)
    re_compile_fastmap (pbufp);

  while (1)
    {
      /* If a fastmap is supplied, skip quickly over characters
	 that cannot possibly be the start of a match. */

      if (fastmap)
	{
	  register char c;
	  if (startpos >= size1) c = string2[startpos - size1];
	  else c = string1[startpos];
	  if (translate ? !fastmap[translate[c]] : !fastmap[c])
	    goto advance;
	}

      if (0 <= re_match_2 (pbufp, string1, size1, string2, size2, startpos, regs))
	return startpos;

    advance:
      if (!range) break;
      if (range > 0) range--, startpos++; else range++, startpos--;
    }
  return -1;
}
re_match (pbufp, string, size, pos, regs)
     struct re_pattern_buffer *pbufp;
     char *string;
     int size, pos;
     struct re_registers *regs;
{
  return re_match_2 (pbufp, 0, 0, string, size, pos, regs);
}

/* Match the pattern described by `pbufp'
  against data which is the virtual concatenation of `string1' and `string2'.
  `size1' and `size2' are the sizes of the two data strings.

  If pbufp->fastmap is nonzero, then it had better be up to date.

  The reason that the data to match is specified as two components
  which are to be regarded as concatenated
  is so that this function can be used directly on the contents of an Emacs buffer.

  -1 is returned if there is no match.  Otherwise the value is the length
  of the substring which was matched.
*/

int
re_match_2 (pbufp, string1, size1, string2, size2, pos, regs)
     struct re_pattern_buffer *pbufp;
     char *string1, *string2;
     int size1, size2;
     int pos;
     struct re_registers *regs;
{
  register char *p = pbufp->buffer;
  register char *pend = p + pbufp->used;
  char *d = string1, *dend = d + size1;
  char *dx = string2, *dendx = dx + size2;
  register char *d1, *dend1;
  register int mcnt;
  char *translate = pbufp->translate;

 /* Failure point stack.  Each place that can handle a failure further down the line
    pushes a failure point on this stack.  It consists of two char *'s.
    The first one pushed is where to resume scanning the pattern;
    the second pushed is where to resume scanning the strings.
    If the latter is zero, the failure point is "dormant".
    If a failure happens and the innermost failure point is dormant,
    it discards that failure point and tries the next one. */

  char **stackb = (char **) alloca (2 * NFAILURES * sizeof (char *));
  char **stackp = stackb, **stacke = &stackb[2 * NFAILURES];

/* Advance stacke to make space for more failure points */
#define GROWSTACK \
  { alloca ((stacke - stackb) * sizeof (char *)); \
    stacke += stacke - stackb; }

  /* Information on the "contents" of registers.
     These are pointers into the input strings; they record
     just what was matched (on this attempt) by some part of the pattern.
     The start_memory command stores the start of a register's contents
     and the stop_memory command stores the end.

     At that point, regstart[regnum] points to the first character in the register,
     regend[regnum] points to the first character beyond the end of the register,
     and regstart_segend[regnum] is either the same as regend[regnum]
     or else points to the end of the input string into which regstart[regnum] points.
     The latter case happens when regstart[regnum] is in string1 and
     regend[regnum] is in string2.  */

  char *regstart[RE_NREGS];
  char *regstart_segend[RE_NREGS];
  char *regend[RE_NREGS];

  bzero (regstart, sizeof regstart);

  /* `p' scans through the pattern as `d1' scans through the data.
     `dend1' is the end of the input string that `d1' points within.
     `d1' is advanced into the following input string whenever necessary. */

  if (pos < size1)
    d1 = d + pos, dend1 = dend;
  else
    d1 = dx + pos - size1, dend1 = dendx;

  /* This loop loops over pattern commands.
     It exits by returning from the function if match is complete,
     or it drops through if match fails at this starting point in the input data. */

  while (1)
    {
      if (p == pend)
	/* End of pattern means we have succeeded! */
	{
	  /* If caller wants register contents data back, convert it to indices */
	  if (regs)
	    {
	      bzero (regs, sizeof (*regs));

	      regend[0] = d1;
	      regstart[0] = d;
	      for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
		{
		  if (!regstart[mcnt]) continue;
		  if (regstart[mcnt] - string1 < 0 || regstart[mcnt] - string1 > size1)
		    regs->start[mcnt] = regstart[mcnt] - string2 + size1;
		  else
		    regs->start[mcnt] = regstart[mcnt] - string1;
		  if (regend[mcnt] - string1 < 0 || regend[mcnt] - string1 > size1)
		    regs->end[mcnt] = regend[mcnt] - string2 + size1;
		  else
		    regs->end[mcnt] = regend[mcnt] - string1;
		}
	      regs->start[0] = pos;
	    }
	  if (d1 - string1 >= 0 && d1 - string1 <= size1)
	    return d1 - string1 - pos;
	  else
	    return d1 - string2 + size1 - pos;
	}

      /* Otherwise match next pattern command */
      switch ((enum regexpcode) *p++)
	{

	/* \( is represented by a start_memory, \) by a stop_memory.
	    Both of those commands contain a "register number" argument.
	    The text matched within the \( and \) is recorded under that number.
	    Then, \<digit> turns into a `duplicate' command which
	    is followed by the numeric value of <digit> as the register number. */

	case start_memory:
	  regstart[*p] = d1;
	  regstart_segend[*p++] = dend1;
	  break;

	case stop_memory:
	  regend[*p] = d1;
	  if (regstart_segend[*p] == dend1)
	    regstart_segend[*p] = d1;
	  p++;
	  break;

	case duplicate:
	  {
	    int regno = *p++;   /* Get which register to match against */
	    register char *d2, *dend2;

	    d2 = regstart[regno];
	    dend2 = regstart_segend[regno];
	    while (1)
	      {
		/* Advance to next segment in register contents, if necessary */
		while (d2 == dend2)
		  {
		    if (dend2 == dendx) break;
		    if (dend2 == regend[regno]) break;
		    d2 = dx, dend2 = regend[regno];  /* end of string1 => advance to string2. */
		  }
		/* At end of register contents => success */
		if (d2 == dend2) break;

		/* Advance to next segment in data being matched, if necessary */
		while (d1 == dend1)
		  {
		    if (dend1 == dendx) goto fail;  /* end of string2 => failure */
		    d1 = dx, dend1 = dendx;  /* end of string1 => advance to string2. */
		  }

		/* mcnt gets # consecutive chars to compare */
		mcnt = dend1 - d1;
		if (mcnt > dend2 - d2)
		  mcnt = dend2 - d2;
		/* Compare that many; failure if mismatch, else skip them. */
		if (translate ? bcmp_translate (d1, d2, mcnt, translate) : bcmp (d1, d2, mcnt))
		  goto fail;
		d1 += mcnt, d2 += mcnt;
	      }
	  }
	  break;

	case anychar:
	  /* fetch a data character */
	  while (d1 == dend1)
	    {
	      if (dend1 == dendx) goto fail;  /* end of string2 => failure */
	      d1 = dx, dend1 = dendx;  /* end of string1 => advance to string2. */
	    }
	  /* Match anything but a newline.  */
	  if ((translate ? translate[*d1++] : *d1++) == '\n')
	    goto fail;
	  break;

	case charset:
	case charset_not:
	  {
	    /* Nonzero for charset_not */
	    int not = *(p - 1) == (char) charset_not;
	    register char c;
	    register int count;

	    /* fetch a data character */
	    while (d1 == dend1)
	      {
		if (dend1 == dendx) goto fail;  /* end of string2 => failure */
		d1 = dx, dend1 = dendx;  /* end of string1 => advance to string2. */
	      }

	    if (translate)
	      c = translate [*d1];
	    else
	      c = *d1;

	    /* Try each individual character in the set. */
	    for (count = *p++; count; count--)
	      if (*p++ == c)
		{
		  p += count - 1;
		  count = *p++;
		  p += count + count;
		  goto found;
		}
	    /* Try each character range in the set.  */
	    for (count = *p++; count; count--)
	      if (c >= *p++ && c <= *p++)
		{
		  p += count + count - 2;
		  goto found;
		}

	    /* Data char is not one of those specified. */
	    if (!not) goto fail;
	    d1++;
	    break;

	  found:		/* data char is one of those specified. */
	    if (not) goto fail;
	    d1++;
	    break;
	  }

	case begline:
	  if (!(d1 == string1 || (d1 == string2 ? dend[-1] : d1[-1]) == '\n')) goto fail;
	  break;

	case endline:
	  if (!(d1 == dendx || (d1 == dend1 ? *dx : *d1) == '\n')) goto fail;
	  break;

	/* To start a repeat ("*" construct), make a dormant failure point.
	   It is dormant because its data scanning restart address is zero.
	   At the end of a repetition, the current data scanning address is stored,
	   activating the failure point, whose pattern restart is the start of the repeat.
	   So any failure parsing what follows the repeated stuff causes
	   an attempt to find another repetition of it.
	   At that time, the failure point is added back on, dormant again.
	   So a failure in parsing the repeated pattern
	   propagates to the previous failure point. */

	case start_repeat:
	  if (stackp == stacke) GROWSTACK;
	  *++stackp = p-1;	/* Push the failure point */
	  *++stackp = 0;	/* but leave it dormant until the end of the repeat. */
	  break;

	case end_repeat:
	  *stackp = d1;	/* Activate the failure point. */
	  break;

	/* If a repeated pattern is allowed to match zero or more times,
	   it is preceded by a skip_repeat_jump that jumps around it
	   but makes a failure point to begin repeating it. */

	case skip_repeat_jump:
	  if (stackp == stacke) GROWSTACK;
	  mcnt = *p++ & 0377;   /* Now jump around the repeated pattern. */
	  mcnt += *p++ << 8;    /* If we fail later, the first repetition will be done. */
	  *++stackp = p;	/* Push the failure point */
	  *++stackp = d1;	/* and activate it right away, assuming zero repetitions */
	  p += mcnt;
	  break;

	/* "or" constructs ("|") are handled by starting each alternative
	    with an on_failure_jump that points to the start of the next alternative.
	    Each alternative except the last ends with a jump to the joining point.
	    (Actually, each jump except for the last one really jumps
	     to the following jump, because tensioning the jumps is a hassle.) */

	/* The start of a stupid repeat has an on_failure_jump that points
	   past the end of the repeat text.
	   This makes a failure point so that, on failure to match a repetition,
	   matching restarts past as many repetitions have been found
	   with no way to fail and look for another one.  */

	case on_failure_jump:
	  if (stackp == stacke) GROWSTACK;
	  mcnt = *p++ & 0377;
	  mcnt += *p++ << 8;
	  *++stackp = mcnt + p;
	  *++stackp = d1;
	  break;

	/* The end of a stupid repeat has a finalize-jump
	   back to the start, where another failure point will be made
	   which will point after all the repetitions found so far. */

	case finalize_jump:
	  stackp -= 2;

	case jump:
	  mcnt = *p++ & 0377;
	  mcnt += *p++ << 8;
	  p += mcnt;
	  break;

#ifdef emacs
	case before_dot:
	  if (((dend1 == dendx) ? d1 - bf_p2 : d1 - bf_p1) >= dot)
	    goto fail;
	  break;

	case at_dot:
	  if (((dend1 == dendx) ? d1 - bf_p2 : d1 - bf_p1) == dot)
	    goto fail;
	  break;

	case after_dot:
	  if (((dend1 == dendx) ? d1 - bf_p2 : d1 - bf_p1) <= dot)
	    goto fail;
	  break;

	case begbuf:
	  if (((dend1 == dendx) ? d1 - bf_p2 : d1 - bf_p1) != FirstCharacter)
	    goto fail;
	  break;

	case endbuf:
	  if (((dend1 == dendx) ? d1 - bf_p2 : d1 - bf_p1) != NumCharacters + 1)
	    goto fail;
	  break;
	  
	case wordbound:
	  mcnt = (dend1 == dendx) ? d1 - bf_p2 : d1 - bf_p1;  /* Calculate char number */
	  if (mcnt == FirstCharacter || mcnt == NumCharacters + 1 ||
	      (SYNTAX (CharAt (mcnt - 1)) == Sword) != (SYNTAX (CharAt (mcnt)) == Sword))
	    break;
	  goto fail;
	  
	case notwordbound:
	  mcnt = (dend1 == dendx) ? d1 - bf_p2 : d1 - bf_p1;  /* Calculate char number */
	  if (mcnt == FirstCharacter || mcnt == NumCharacters + 1 ||
	      (SYNTAX (CharAt (mcnt - 1)) == Sword) != (SYNTAX (CharAt (mcnt)) == Sword))
	    goto fail;
	  break;
	      
	case wordchar:
	  mcnt = (int) Sword;
	  goto matchsyntax;

	case syntaxspec:
	  mcnt = *p++;
	matchsyntax:
	  while (d1 == dend1)
	    {
	      if (dend1 == dendx) goto fail;  /* end of string2 => failure */
	      d1 = dx, dend1 = dendx;  /* end of string1 => advance to string2. */
	    }
	  if (SYNTAX (*d1++) != (enum syntaxcode) mcnt) goto fail;
	  break;
	  
	case notwordchar:
	  mcnt = (int) Sword;
	  goto matchnotsyntax;

	case notsyntaxspec:
	  mcnt = *p++;
	matchnotsyntax:
	  while (d1 == dend1)
	    {
	      if (dend1 == dendx) goto fail;  /* end of string2 => failure */
	      d1 = dx, dend1 = dendx;  /* end of string1 => advance to string2. */
	    }
	  if (SYNTAX (*d1++) == (enum syntaxcode) mcnt) goto fail;
	  break;
#endif emacs

	case exactn:
	  /* Match the next few pattern characters exactly.
	     mcnt is how many characters to match. */
	  mcnt = *p++;
	  if (translate)
	    {
	      do
		{
		  while (d1 == dend1)
		    {
		      if (dend1 == dendx) goto fail;  /* end of string2 => failure */
		      d1 = dx, dend1 = dendx;  /* end of string1 => advance to string2. */
		    }
		  if (translate[*d1++] != *p++) goto fail;
		}
	      while (--mcnt);
	    }
	  else
	    {
	      do
		{
		  while (d1 == dend1)
		    {
		      if (dend1 == dendx) goto fail;  /* end of string2 => failure */
		      d1 = dx, dend1 = dendx;  /* end of string1 => advance to string2. */
		    }
		  if (*d1++ != *p++) goto fail;
		}
	      while (--mcnt);
	    }
	  break;
	}
      continue;    /* Successfully matched one pattern command; keep matching */

      /* Jump here if any matching operation fails. */
    fail:
      if (stackp != stackb)
	/* A restart point is known.  Restart there and pop it. */
	{
	  if (!*stackp)
	    {   /* If innermost failure point is dormant, flush it and keep looking */
	      stackp -= 2;
	      goto fail;
	    }
	  d1 = *stackp--;
	  p = *stackp--;
	  if (d1 >= d && d1 <= dend)
	    dend1 = dend;
	}
      else break;   /* Matching at this starting point really fails! */
    }
  return -1;         /* Failure to match */
}

static int
bcmp_translate (s1, s2, len, translate)
     char *s1, *s2;
     register int len;
     char *translate;
{
  register char *p1 = s1, *p2 = s2;
  while (len)
    {
      if (translate [*p1++] != translate [*p2++]) return 1;
      len--;
    }
  return 0;
}
/* Entry points compatible with Unix regex library */

static struct re_pattern_buffer re_comp_buf;

char *
re_comp (s)
     char *s;
{
  char *value;

  if (!s)
    {
      if (!re_comp_buf.buffer)
	return "No previous regular expression";
      return 0;
    }

  if (!re_comp_buf.buffer)
    {
      if (!(re_comp_buf.buffer = (char *) malloc (200)))
	return "Memory exhausted";
      re_comp_buf.allocated = 200;
      re_comp_buf.fastmap = 0;   /* Don't need one, since re_exec does not search. */
    }
  return re_compile_pattern (s, strlen (s), &re_comp_buf);
}

int
re_exec (s)
     char *s;
{
  return 0 <= re_match (&re_comp_buf, s, strlen (s), 0, 0, 0);
}
#ifdef test

#include <stdio.h>

/* Indexed by a character, gives the upper case equivalent of the character */

static char upcase[0400] = 
  { 000, 001, 002, 003, 004, 005, 006, 007,
    010, 011, 012, 013, 014, 015, 016, 017,
    020, 021, 022, 023, 024, 025, 026, 027,
    030, 031, 032, 033, 034, 035, 036, 037,
    040, 041, 042, 043, 044, 045, 046, 047,
    050, 051, 052, 053, 054, 055, 056, 057,
    060, 061, 062, 063, 064, 065, 066, 067,
    070, 071, 072, 073, 074, 075, 076, 077,
    0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
    0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
    0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
    0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
    0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
    0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
    0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
    0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
    0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
    0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
    0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
    0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
    0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
    0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
    0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
    0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
    0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
    0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
    0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
    0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
    0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
    0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
    0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
    0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  };

main ()
{
  char pat[80];
  struct re_pattern_buffer buf;
  int i;
  char c;
  char fastmap[0400];

  buf.allocated = 20;
  buf.buffer = (char *) malloc (20);
  buf.fastmap = fastmap;
  buf.translate = upcase;
  buf.regs = 0;

  while (1)
    {
      gets (pat);

      if (*pat)
	{
          re_compile_pattern (pat, strlen(pat), &buf);

	  for (i = 0; i < buf.used; i++)
	    printchar (buf.buffer[i]);

	  putchar ('\n');

	  printf ("%d allocated, %d used.\n", buf.allocated, buf.used);

	  re_compile_fastmap (&buf);
	  printf ("Allowed by fastmap: ");
	  for (i = 0; i < 0400; i++)
	    if (fastmap[i]) printchar (i);
	  putchar ('\n');
	}

      gets (pat);	/* Now read the string to match against */

      i = re_match (&buf, pat, strlen (pat), 0, 0);
      printf ("Match value %d.\n", i);
    }
}

printchar (c)
     char c;
{
  if (c < 041 || c >= 0177)
    {
      putchar ('\\');
      putchar (((c >> 6) & 3) + '0');
      putchar (((c >> 3) & 7) + '0');
      putchar ((c & 7) + '0');
    }
  else
    putchar (c);
}

error (string)
     char *string;
{
  puts (string);
  exit (1);
}

#endif test