Blame lib/Xm/regexp.c

Packit b099d7
/*
Packit b099d7
 *      $XConsortium: regexp.c /main/3 1995/07/13 18:30:18 drk $
Packit b099d7
 *
Packit b099d7
 *      @(#)regexp.c	1.4 04 Mar 1994
Packit b099d7
 *
Packit b099d7
 *      RESTRICTED CONFIDENTIAL INFORMATION:
Packit b099d7
 *
Packit b099d7
 *      The information in this document is subject to special
Packit b099d7
 *      restrictions in a confidential disclosure agreement between
Packit b099d7
 *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
Packit b099d7
 *      document outside HP, IBM, Sun, USL, SCO, or Univel without
Packit b099d7
 *      Sun's specific written approval.  This document and all copies
Packit b099d7
 *      and derivative works thereof must be returned or destroyed at
Packit b099d7
 *      Sun's request.
Packit b099d7
 *
Packit b099d7
 *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
Packit b099d7
 *
Packit b099d7
 */
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * _XmRegcomp and _XmRegexec
Packit b099d7
 *
Packit b099d7
 *	Copyright (c) 1986 by University of Toronto.
Packit b099d7
 *	Written by Henry Spencer.  Not derived from licensed software.
Packit b099d7
 *
Packit b099d7
 *	Permission is granted to anyone to use this software for any
Packit b099d7
 *	purpose on any computer system, and to redistribute it freely,
Packit b099d7
 *	subject to the following restrictions:
Packit b099d7
 *
Packit b099d7
 *	1. The author is not responsible for the consequences of use of
Packit b099d7
 *		this software, no matter how awful, even if they arise
Packit b099d7
 *		from defects in it.
Packit b099d7
 *
Packit b099d7
 *	2. The origin of this software must not be misrepresented, either
Packit b099d7
 *		by explicit claim or by omission.
Packit b099d7
 *
Packit b099d7
 *	3. Altered versions must be plainly marked as such, and must not
Packit b099d7
 *		be misrepresented as being the original software.
Packit b099d7
 *** THIS IS AN ALTERED VERSION.  It was altered by John Gilmore,
Packit b099d7
 *** hoptoad!gnu, on 27 Dec 1986, to add \n as an alternative to |
Packit b099d7
 *** to assist in implementing egrep.
Packit b099d7
 *** THIS IS AN ALTERED VERSION.  It was altered by John Gilmore,
Packit b099d7
 *** hoptoad!gnu, on 27 Dec 1986, to add \< and \> for word-matching
Packit b099d7
 *** as in BSD grep and ex.
Packit b099d7
 *** THIS IS AN ALTERED VERSION.  It was altered by John Gilmore,
Packit b099d7
 *** hoptoad!gnu, on 28 Dec 1986, to optimize characters quoted with \.
Packit b099d7
 *** THIS IS AN ALTERED VERSION.  It was altered by James A. Woods,
Packit b099d7
 *** ames!jaw, on 19 June 1987, to quash a regcomp() redundancy.
Packit b099d7
 *** THIS IS AN ALTERED VERSION.  It was altered by Ajay Vohra,
Packit b099d7
 *** ajay@eng.sun.com, on 2 March 1994, to make it work in all locales.
Packit b099d7
 **  The API was altered too.
Packit b099d7
 *
Packit b099d7
 * Beware that some of this code is subtly aware of the way operator
Packit b099d7
 * precedence is structured in regular expressions.  Serious changes in
Packit b099d7
 * regular-expression syntax might require a total rethink.
Packit b099d7
 */
Packit b099d7
#ifdef HAVE_CONFIG_H
Packit b099d7
#include <config.h>
Packit b099d7
#endif
Packit b099d7
Packit b099d7
Packit b099d7
#include "regexpI.h"
Packit b099d7
#include <stdio.h>
Packit b099d7
#include <ctype.h>
Packit b099d7
#include <wctype.h>
Packit b099d7
#include <stdlib.h>
Packit b099d7
#include <string.h>
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * The "internal use only" fields in regexpI.h are present to pass info from
Packit b099d7
 * compile to execute that permits the execute phase to run lots faster on
Packit b099d7
 * simple cases.  They are:
Packit b099d7
 *
Packit b099d7
 * regstart	char that must begin a match; '\0' if none obvious
Packit b099d7
 * reganch	is the match anchored (at beginning-of-line only)?
Packit b099d7
 * regmust	string (pointer into program) that match must include, or NULL
Packit b099d7
 * regmlen	length of regmust string
Packit b099d7
 *
Packit b099d7
 * Regstart and reganch permit very fast decisions on suitable starting points
Packit b099d7
 * for a match, cutting down the work a lot.  Regmust permits fast rejection
Packit b099d7
 * of lines that cannot possibly match.  The regmust tests are costly enough
Packit b099d7
 * that _XmRegcomp() supplies a regmust only if the r.e. contains something
Packit b099d7
 * potentially expensive (at present, the only such thing detected is * or +
Packit b099d7
 * at the start of the r.e., which can involve a lot of backup).  Regmlen is
Packit b099d7
 * supplied because the test in _XmRegexec() needs it and _XmRegcomp() is computing
Packit b099d7
 * it anyway.
Packit b099d7
 */
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * Structure for regexp "program".  This is essentially a linear encoding
Packit b099d7
 * of a nondeterministic finite-state machine (aka syntax charts or
Packit b099d7
 * "railroad normal form" in parsing technology).  Each node is an opcode
Packit b099d7
 * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
Packit b099d7
 * all nodes except BRANCH implement concatenation; a "next" pointer with
Packit b099d7
 * a BRANCH on both ends of it is connecting two alternatives.  (Here we
Packit b099d7
 * have one of the subtle syntax dependencies:  an individual BRANCH (as
Packit b099d7
 * opposed to a collection of them) is never concatenated with anything
Packit b099d7
 * because of operator precedence.)  The operand of some types of node is
Packit b099d7
 * a literal string; for others, it is a node leading into a sub-FSM.  In
Packit b099d7
 * particular, the operand of a BRANCH node is the first node of the branch.
Packit b099d7
 * (NB this is *not* a tree structure:  the tail of the branch connects
Packit b099d7
 * to the thing following the set of BRANCHes.)  The opcodes are:
Packit b099d7
 */
Packit b099d7
Packit b099d7
/* definition	number	opnd?	meaning */
Packit b099d7
#define	END	0	/* no	End of program. */
Packit b099d7
#define	BOL	1	/* no	Match "" at beginning of line. */
Packit b099d7
#define	EOL	2	/* no	Match "" at end of line. */
Packit b099d7
#define	ANY	3	/* no	Match any one character. */
Packit b099d7
#define	ANYOF	4	/* str	Match any character in this string. */
Packit b099d7
#define	ANYBUT	5	/* str	Match any character not in this string. */
Packit b099d7
#define	BRANCH	6	/* node	Match this alternative, or the next... */
Packit b099d7
#define	BACK	7	/* no	Match "", "next" ptr points backward. */
Packit b099d7
#define	EXACTLY	8	/* str	Match this string. */
Packit b099d7
#define	NOTHING	9	/* no	Match empty string. */
Packit b099d7
#define	STAR	10	/* node	Match this (simple) thing 0 or more times. */
Packit b099d7
#define	PLUS	11	/* node	Match this (simple) thing 1 or more times. */
Packit b099d7
#define	WORDA	12	/* no	Match "" at wordchar, where prev is nonword */
Packit b099d7
#define	WORDZ	13	/* no	Match "" at nonwordchar, where prev is word */
Packit b099d7
#define	OPEN	20	/* no	Mark this point in input as start of #n. */
Packit b099d7
/*	OPEN+1 is number 1, etc. */
Packit b099d7
#define	CLOSE	30	/* no	Analogous to OPEN. */
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * Opcode notes:
Packit b099d7
 *
Packit b099d7
 * BRANCH	The set of branches constituting a single choice are hooked
Packit b099d7
 *		together with their "next" pointers, since precedence prevents
Packit b099d7
 *		anything being concatenated to any individual branch.  The
Packit b099d7
 *		"next" pointer of the last BRANCH in a choice points to the
Packit b099d7
 *		thing following the whole choice.  This is also where the
Packit b099d7
 *		final "next" pointer of each individual branch points; each
Packit b099d7
 *		branch starts with the operand node of a BRANCH node.
Packit b099d7
 *
Packit b099d7
 * BACK		Normal "next" pointers all implicitly point forward; BACK
Packit b099d7
 *		exists to make loop structures possible.
Packit b099d7
 *
Packit b099d7
 * STAR,PLUS	'?', and complex '*' and '+', are implemented as circular
Packit b099d7
 *		BRANCH structures using BACK.  Simple cases (one character
Packit b099d7
 *		per match) are implemented with STAR and PLUS for speed
Packit b099d7
 *		and to minimize recursive plunges.
Packit b099d7
 *
Packit b099d7
 * OPEN,CLOSE	...are numbered at compile time.
Packit b099d7
 */
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * A node is one char of opcode followed by two chars of "next" pointer.
Packit b099d7
 * "Next" pointers are stored as two 8-bit pieces, high order first.  The
Packit b099d7
 * value is a positive offset from the opcode of the node containing it.
Packit b099d7
 * An operand, if any, simply follows the node.  (Note that much of the
Packit b099d7
 * code generation knows about this implicit relationship.)
Packit b099d7
 *
Packit b099d7
 * Using two bytes for the "next" pointer is vast overkill for most things,
Packit b099d7
 * but allows patterns to get big without disasters.
Packit b099d7
 */
Packit b099d7
#define	OP(p)	(*(p))
Packit b099d7
#define	NEXT(p)	(((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
Packit b099d7
#define	OPERAND(p)	((p) + 3)
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * See regmagic.h for one further detail of program structure.
Packit b099d7
 */
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * Utility definitions.
Packit b099d7
 */
Packit b099d7
#define INCRLEN(l)   (l ? l: 1)
Packit b099d7
#define CHARLEN(p)	(MB_CUR_MAX > 1 ? (mblen(p, MB_CUR_MAX)): (*p ? 1 : 0))
Packit b099d7
#ifndef CHARBITS
Packit b099d7
#define	UCHARAT(p)	((int)*(unsigned char *)(p))
Packit b099d7
#else
Packit b099d7
#define	UCHARAT(p)	((int)*(p)&CHARBITS)
Packit b099d7
#endif
Packit b099d7
Packit b099d7
#define	FAIL(m)	{ return(NULL); }
Packit b099d7
#define	ISMULT(p)	((CHARLEN(p) == 1) &&  ((*p) == '*' || (*p) == '+' || (*p) == '?') )
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * Flags to be passed up and down.
Packit b099d7
 */
Packit b099d7
#define	HASWIDTH	01	/* Known never to match null string. */
Packit b099d7
#define	SIMPLE		02	/* Simple enough to be STAR/PLUS operand. */
Packit b099d7
#define	SPSTART		04	/* Starts with * or +. */
Packit b099d7
#define	WORST		0	/* Worst case. */
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * Global work variables for _XmRegcomp().
Packit b099d7
 */
Packit b099d7
static char	*regparse;		/* Input-scan pointer. */
Packit b099d7
static int	regnpar;		/* () count. */
Packit b099d7
static char	regdummy;
Packit b099d7
static char	*regcode;		/* Code-emit pointer; &regdummy = don't. */
Packit b099d7
static long	regsize;		/* Code size. */
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * Forward declarations for _XmRegcomp()'s friends.
Packit b099d7
 */
Packit b099d7
static int	inclass(char *set, char *s);
Packit b099d7
static char	*reg(int paren, int *flagp);
Packit b099d7
static char	*regbranch(int *flagp);
Packit b099d7
static char	*regatom(int *flagp);
Packit b099d7
static char	*regpiece(int *flagp);
Packit b099d7
static char	*regnode(char op);
Packit b099d7
static void	regp(char *p);
Packit b099d7
static void	regc(char b);
Packit b099d7
static void	reginsert(char op, char *opnd);
Packit b099d7
static void	regtail(char *p, char *val);
Packit b099d7
static void	regoptail(char *p, char *val);
Packit b099d7
static char	*regnext(char *p);
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - inclass - find if a char* 's' is within the collation sequence defined by 'set' 
Packit b099d7
 * of characters. This replaces the original code where all the characters 
Packit b099d7
 * were explicitly enumerated after the ANYOF regnode and where strchr was used
Packit b099d7
 * to determine if the given character belonged to the character class or not.
Packit b099d7
 * In asian locales, enumerating all the characters between two characters
Packit b099d7
 * is neither possible nor feasible (there could be thousands of character
Packit b099d7
 * between two characters. Hence this new function. It returns 1 if 
Packit b099d7
 * char *s does belong to the class 'set'; else 0.
Packit b099d7
 */
Packit b099d7
Packit b099d7
static int	
Packit b099d7
inclass(char *set, char *s)
Packit b099d7
{
Packit b099d7
	char	*p = set;
Packit b099d7
	int	len = 0, plen = 0;
Packit b099d7
Packit b099d7
	while ((len = CHARLEN(p))) {
Packit b099d7
		switch (len) {
Packit b099d7
		case 1:
Packit b099d7
			if (*p == '-' 	 &&  
Packit b099d7
				p != set	 &&  
Packit b099d7
					CHARLEN((p + 1))) {
Packit b099d7
				char	*begin = p -plen;
Packit b099d7
				char	*end = p + 1;
Packit b099d7
Packit b099d7
				if (strcoll(s, begin) >= 0 &&  
Packit b099d7
					strcoll(s, end) <= 0)
Packit b099d7
					return 1;
Packit b099d7
			}
Packit b099d7
			if (*p == *s)
Packit b099d7
				return 1;
Packit b099d7
			break;
Packit b099d7
		default:
Packit b099d7
			if (!strncmp(p, s, len))
Packit b099d7
				return 1;
Packit b099d7
		}
Packit b099d7
		plen = len;
Packit b099d7
		p += len;
Packit b099d7
	}
Packit b099d7
	return 0;
Packit b099d7
}
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - _XmRegcomp - compile a regular expression into internal code
Packit b099d7
 *
Packit b099d7
 * We can't allocate space until we know how big the compiled form will be,
Packit b099d7
 * but we can't compile it (and thus know how big it is) until we've got a
Packit b099d7
 * place to put the code.  So we cheat:  we compile it twice, once with code
Packit b099d7
 * generation turned off and size counting turned on, and once "for real".
Packit b099d7
 * This also means that we don't allocate space until we are sure that the
Packit b099d7
 * thing really will compile successfully, and we never have to move the
Packit b099d7
 * code and thus invalidate pointers into it.  (Note that it has to be in
Packit b099d7
 * one piece because free() must be able to free it all.)
Packit b099d7
 *
Packit b099d7
 * Beware that the optimization-preparation code in here knows about some
Packit b099d7
 * of the structure of the compiled regexp.
Packit b099d7
 */
Packit b099d7
Packit b099d7
XmRegexpRec *
Packit b099d7
_XmRegcomp(char *exp)
Packit b099d7
{
Packit b099d7
	register XmRegexpRec *r;
Packit b099d7
	register char	*scan;
Packit b099d7
	register char	*longest;
Packit b099d7
	register int	len;
Packit b099d7
	int	flags;
Packit b099d7
Packit b099d7
	if (exp == NULL)
Packit b099d7
		FAIL("NULL argument");
Packit b099d7
Packit b099d7
	/* First pass: determine size, legality. */
Packit b099d7
#ifdef notdef
Packit b099d7
	if (exp[0] == '.' && exp[1] == '*')
Packit b099d7
		exp += 2;  /* aid grep */
Packit b099d7
#endif
Packit b099d7
	regparse = (char *)exp;
Packit b099d7
	regnpar = 1;
Packit b099d7
	regsize = 0L;
Packit b099d7
	regcode = &regdummy;
Packit b099d7
	regc(MAGIC);
Packit b099d7
	if (reg(0, &flags) == NULL)
Packit b099d7
		return(NULL);
Packit b099d7
Packit b099d7
	/* Small enough for pointer-storage convention? */
Packit b099d7
	if (regsize >= 32767L)		/* Probably could be 65535L. */
Packit b099d7
		FAIL("regexp too big");
Packit b099d7
Packit b099d7
	/* Allocate space. */
Packit b099d7
	r = (XmRegexpRec * )malloc(sizeof(XmRegexpRec) + (unsigned)regsize);
Packit b099d7
	if (r == NULL)
Packit b099d7
		FAIL("out of space");
Packit b099d7
Packit b099d7
	/* Second pass: emit code. */
Packit b099d7
	regparse = (char *)exp;
Packit b099d7
	regnpar = 1;
Packit b099d7
	regcode = r->program;
Packit b099d7
	regc(MAGIC);
Packit b099d7
	if (reg(0, &flags) == NULL)
Packit b099d7
		return(NULL);
Packit b099d7
Packit b099d7
	/* Dig out information for optimizations. */
Packit b099d7
	r->regstart = '\0';	/* Worst-case defaults. */
Packit b099d7
	r->reganch = 0;
Packit b099d7
	r->regmust = NULL;
Packit b099d7
	r->regmlen = 0;
Packit b099d7
	scan = r->program + 1;			/* First BRANCH. */
Packit b099d7
	if (OP(regnext(scan)) == END) {		/* Only one top-level choice. */
Packit b099d7
		scan = OPERAND(scan);
Packit b099d7
Packit b099d7
		/* Starting-point info. */
Packit b099d7
		if (OP(scan) == EXACTLY)
Packit b099d7
			r->regstart = *OPERAND(scan);
Packit b099d7
		else if (OP(scan) == BOL)
Packit b099d7
			r->reganch++;
Packit b099d7
Packit b099d7
		/*
Packit b099d7
		 * If there's something expensive in the r.e., find the
Packit b099d7
		 * longest literal string that must appear and make it the
Packit b099d7
		 * regmust.  Resolve ties in favor of later strings, since
Packit b099d7
		 * the regstart check works with the beginning of the r.e.
Packit b099d7
		 * and avoiding duplication strengthens checking.  Not a
Packit b099d7
		 * strong reason, but sufficient in the absence of others.
Packit b099d7
		 */
Packit b099d7
		if (flags & SPSTART) {
Packit b099d7
			longest = NULL;
Packit b099d7
			len = 0;
Packit b099d7
			for (; scan != NULL; scan = regnext(scan))
Packit b099d7
				if (OP(scan) == EXACTLY 	 &&  
Packit b099d7
					(int)strlen(OPERAND(scan)) >= (int)len) {
Packit b099d7
					longest = OPERAND(scan);
Packit b099d7
					len = strlen(OPERAND(scan));
Packit b099d7
				}
Packit b099d7
			r->regmust = longest;
Packit b099d7
			r->regmlen = len;
Packit b099d7
		}
Packit b099d7
	}
Packit b099d7
Packit b099d7
	return(r);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - reg - regular expression, i.e. main body or parenthesized thing
Packit b099d7
 *
Packit b099d7
 * Caller must absorb opening parenthesis.
Packit b099d7
 *
Packit b099d7
 * Combining parenthesis handling with the base level of regular expression
Packit b099d7
 * is a trifle forced, but the need to tie the tails of the branches to what
Packit b099d7
 * follows makes it hard to avoid.
Packit b099d7
 */
Packit b099d7
static char	*
Packit b099d7
reg(int paren, int *flagp)
Packit b099d7
{
Packit b099d7
	register char	*ret;
Packit b099d7
	register char	*br;
Packit b099d7
	register char	*ender;
Packit b099d7
	register int	parno;
Packit b099d7
	int	flags;
Packit b099d7
	int	len = 0;
Packit b099d7
Packit b099d7
	*flagp = HASWIDTH;	/* Tentatively. */
Packit b099d7
Packit b099d7
	/* Make an OPEN node, if parenthesized. */
Packit b099d7
	if (paren) {
Packit b099d7
		if (regnpar >= NSUBEXP)
Packit b099d7
			FAIL("too many ()");
Packit b099d7
		parno = regnpar;
Packit b099d7
		regnpar++;
Packit b099d7
		ret = regnode(OPEN + parno);
Packit b099d7
	} else
Packit b099d7
		ret = NULL;
Packit b099d7
Packit b099d7
	/* Pick up the branches, linking them together. */
Packit b099d7
	br = regbranch(&flags);
Packit b099d7
	if (br == NULL)
Packit b099d7
		return(NULL);
Packit b099d7
	if (ret != NULL)
Packit b099d7
		regtail(ret, br);	/* OPEN -> first. */
Packit b099d7
		else
Packit b099d7
		ret = br;
Packit b099d7
	if (!(flags & HASWIDTH))
Packit b099d7
		*flagp &= ~HASWIDTH;
Packit b099d7
	*flagp |= flags & SPSTART;
Packit b099d7
	while (  ((len = CHARLEN(regparse)) == 1) &&  
Packit b099d7
			(*regparse == '|' || *regparse == '\n')) {
Packit b099d7
		regparse++;
Packit b099d7
		br = regbranch(&flags);
Packit b099d7
		if (br == NULL)
Packit b099d7
			return(NULL);
Packit b099d7
		regtail(ret, br);	/* BRANCH -> BRANCH. */
Packit b099d7
		if (!(flags & HASWIDTH))
Packit b099d7
			*flagp &= ~HASWIDTH;
Packit b099d7
		*flagp |= flags & SPSTART;
Packit b099d7
	}
Packit b099d7
Packit b099d7
	/* Make a closing node, and hook it on the end. */
Packit b099d7
	ender = regnode((paren) ? CLOSE + parno : END);
Packit b099d7
	regtail(ret, ender);
Packit b099d7
Packit b099d7
	/* Hook the tails of the branches to the closing node. */
Packit b099d7
	for (br = ret; br != NULL; br = regnext(br))
Packit b099d7
		regoptail(br, ender);
Packit b099d7
Packit b099d7
	/* Check for proper termination. */
Packit b099d7
	if (paren && (len != 1 || *regparse++ != ')')) {
Packit b099d7
		FAIL("unmatched ()");
Packit b099d7
	} else if (!paren && (len != 0)) {
Packit b099d7
		if ((len == 1) && *regparse == ')') {
Packit b099d7
			FAIL("unmatched ()");
Packit b099d7
		} else
Packit b099d7
			FAIL("junk on end");	/* "Can't happen". */
Packit b099d7
		/* NOTREACHED */
Packit b099d7
	}
Packit b099d7
Packit b099d7
	return(ret);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regbranch - one alternative of an | operator
Packit b099d7
 *
Packit b099d7
 * Implements the concatenation operator.
Packit b099d7
 */
Packit b099d7
static char	*
Packit b099d7
regbranch(int *flagp)
Packit b099d7
{
Packit b099d7
	register char	*ret;
Packit b099d7
	register char	*chain;
Packit b099d7
	register char	*latest;
Packit b099d7
	int	flags;
Packit b099d7
	int	len = 0;
Packit b099d7
Packit b099d7
	*flagp = WORST;		/* Tentatively. */
Packit b099d7
Packit b099d7
	ret = regnode(BRANCH);
Packit b099d7
	chain = NULL;
Packit b099d7
	while ((len = CHARLEN(regparse))  &&  
Packit b099d7
		(len != 1 || (*regparse != ')' &&  
Packit b099d7
			*regparse != '\n' && *regparse != '|'))) {
Packit b099d7
		latest = regpiece(&flags);
Packit b099d7
		if (latest == NULL)
Packit b099d7
			return(NULL);
Packit b099d7
		*flagp |= flags & HASWIDTH;
Packit b099d7
		if (chain == NULL)	/* First piece. */
Packit b099d7
			*flagp |= flags & SPSTART;
Packit b099d7
			else
Packit b099d7
			regtail(chain, latest);
Packit b099d7
		chain = latest;
Packit b099d7
	}
Packit b099d7
	if (chain == NULL)	/* Loop ran zero times. */
Packit b099d7
		(void) regnode(NOTHING);
Packit b099d7
Packit b099d7
	return(ret);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regpiece - something followed by possible [*+?]
Packit b099d7
 *
Packit b099d7
 * Note that the branching code sequences used for ? and the general cases
Packit b099d7
 * of * and + are somewhat optimized:  they use the same NOTHING node as
Packit b099d7
 * both the endmarker for their branch list and the body of the last branch.
Packit b099d7
 * It might seem that this node could be dispensed with entirely, but the
Packit b099d7
 * endmarker role is not redundant.
Packit b099d7
 */
Packit b099d7
static char	*
Packit b099d7
regpiece(int *flagp)
Packit b099d7
{
Packit b099d7
	register char	*ret;
Packit b099d7
	register char	*op;
Packit b099d7
	register char	*next;
Packit b099d7
	int	flags;
Packit b099d7
	int	len = 0;
Packit b099d7
Packit b099d7
	ret = regatom(&flags);
Packit b099d7
	if (ret == NULL)
Packit b099d7
		return(NULL);
Packit b099d7
Packit b099d7
	op = regparse;
Packit b099d7
	if (!ISMULT(op)) {
Packit b099d7
		*flagp = flags;
Packit b099d7
		return(ret);
Packit b099d7
	}
Packit b099d7
Packit b099d7
	len = CHARLEN(op);
Packit b099d7
	if (!(flags & HASWIDTH) && ((len != 1) || (*op != '?')) )
Packit b099d7
		FAIL("*+ operand could be empty");
Packit b099d7
	*flagp = ((len != 1) || (*op != '+')) ?  (WORST | SPSTART) : (WORST | HASWIDTH);
Packit b099d7
Packit b099d7
	if ((len == 1) && (*op == '*') && (flags & SIMPLE))
Packit b099d7
		reginsert(STAR, ret);
Packit b099d7
	else if ((len == 1) && 
Packit b099d7
			(*op == '*')) {
Packit b099d7
		/* Emit x* as (x&|), where & means "self". */
Packit b099d7
		reginsert(BRANCH, ret);			/* Either x */
Packit b099d7
		regoptail(ret, regnode(BACK));		/* and loop */
Packit b099d7
		regoptail(ret, ret);			/* back */
Packit b099d7
		regtail(ret, regnode(BRANCH));		/* or */
Packit b099d7
		regtail(ret, regnode(NOTHING));		/* null. */
Packit b099d7
	} else if ((len == 1) && (*op == '+') && (flags & SIMPLE))
Packit b099d7
		reginsert(PLUS, ret);
Packit b099d7
	else if ((len == 1) && (*op == '+')) {
Packit b099d7
		/* Emit x+ as x(&|), where & means "self". */
Packit b099d7
		next = regnode(BRANCH);			/* Either */
Packit b099d7
		regtail(ret, next);
Packit b099d7
		regtail(regnode(BACK), ret);		/* loop back */
Packit b099d7
		regtail(next, regnode(BRANCH));		/* or */
Packit b099d7
		regtail(ret, regnode(NOTHING));		/* null. */
Packit b099d7
	} else if ((len == 1) && (*op == '?')) {
Packit b099d7
		/* Emit x? as (x|) */
Packit b099d7
		reginsert(BRANCH, ret);			/* Either x */
Packit b099d7
		regtail(ret, regnode(BRANCH));		/* or */
Packit b099d7
		next = regnode(NOTHING);		/* null. */
Packit b099d7
		regtail(ret, next);
Packit b099d7
		regoptail(ret, next);
Packit b099d7
	}
Packit b099d7
	regparse += INCRLEN(len);
Packit b099d7
	if (ISMULT(regparse))
Packit b099d7
		FAIL("nested *?+");
Packit b099d7
Packit b099d7
	return(ret);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regatom - the lowest level
Packit b099d7
 *
Packit b099d7
 * Optimization:  gobbles an entire sequence of ordinary characters so that
Packit b099d7
 * it can turn them into a single node, which is smaller to store and
Packit b099d7
 * faster to run.  Backslashed characters are exceptions, each becoming a
Packit b099d7
 * separate node; the code is simpler that way and it's not worth fixing.
Packit b099d7
 */
Packit b099d7
static char	*
Packit b099d7
regatom(int *flagp)
Packit b099d7
{
Packit b099d7
	register char	*ret;
Packit b099d7
	int	flags;
Packit b099d7
	int	len = 0, plen = 0;
Packit b099d7
	char	*cp = NULL;
Packit b099d7
Packit b099d7
	*flagp = WORST;		/* Tentatively. */
Packit b099d7
Packit b099d7
	len = CHARLEN(regparse);
Packit b099d7
	cp = regparse;
Packit b099d7
	regparse += INCRLEN(len);
Packit b099d7
	switch (len) {
Packit b099d7
		/* FIXME: these chars only have meaning at beg/end of pat? */
Packit b099d7
	case 0:
Packit b099d7
	case 1:
Packit b099d7
		switch (*cp) {
Packit b099d7
		case '^':
Packit b099d7
			ret = regnode(BOL);
Packit b099d7
			break;
Packit b099d7
		case '$':
Packit b099d7
			ret = regnode(EOL);
Packit b099d7
			break;
Packit b099d7
		case '.':
Packit b099d7
			ret = regnode(ANY);
Packit b099d7
			*flagp |= HASWIDTH | SIMPLE;
Packit b099d7
			break;
Packit b099d7
		case '[':
Packit b099d7
			 {
Packit b099d7
				register char	*classbegin;
Packit b099d7
				register char	*classend;
Packit b099d7
Packit b099d7
				len = CHARLEN(regparse);
Packit b099d7
				if (len == 1 && *regparse == '^') {	/* Complement of range. */
Packit b099d7
					ret = regnode(ANYBUT);
Packit b099d7
					regparse++;
Packit b099d7
				} else
Packit b099d7
					ret = regnode(ANYOF);
Packit b099d7
				if (len == 1 && (*regparse == ']' || *regparse == '-'))
Packit b099d7
					regc(*regparse++);
Packit b099d7
				while ((len = CHARLEN(regparse))  &&  
Packit b099d7
						(len != 1 || *regparse != ']')) {
Packit b099d7
					if (len == 1 && *regparse == '-') {
Packit b099d7
						regparse++;
Packit b099d7
						regc('-');
Packit b099d7
						if (((len = CHARLEN(regparse)) == 1)  &&  
Packit b099d7
							(*regparse == ']' || *regparse == '\0'))
Packit b099d7
								;
Packit b099d7
						else {
Packit b099d7
							classbegin = regparse - 1 - plen ;
Packit b099d7
							classend = regparse;
Packit b099d7
							if (strcoll(classbegin, classend) > 0)
Packit b099d7
								FAIL("invalid [] range");
Packit b099d7
							regp(classend);
Packit b099d7
							regparse += len;
Packit b099d7
						}
Packit b099d7
					plen = len;
Packit b099d7
					} else {
Packit b099d7
						regp(regparse);
Packit b099d7
						regparse += len;
Packit b099d7
						plen = len;
Packit b099d7
					}
Packit b099d7
				}
Packit b099d7
				regc('\0');
Packit b099d7
				if (len != 1 || *regparse != ']')
Packit b099d7
					FAIL("unmatched []");
Packit b099d7
				regparse++;
Packit b099d7
				*flagp |= HASWIDTH | SIMPLE;
Packit b099d7
			}
Packit b099d7
			break;
Packit b099d7
		case '(':
Packit b099d7
			ret = reg(1, &flags);
Packit b099d7
			if (ret == NULL)
Packit b099d7
				return(NULL);
Packit b099d7
			*flagp |= flags & (HASWIDTH | SPSTART);
Packit b099d7
			break;
Packit b099d7
		case '\0':
Packit b099d7
		case '|':
Packit b099d7
		case '\n':
Packit b099d7
		case ')':
Packit b099d7
			FAIL("internal urp");	/* Supposed to be caught earlier. */
Packit b099d7
			break;
Packit b099d7
		case '?':
Packit b099d7
		case '+':
Packit b099d7
		case '*':
Packit b099d7
			FAIL("?+* follows nothing");
Packit b099d7
			break;
Packit b099d7
		case '\\':
Packit b099d7
			cp = regparse;
Packit b099d7
			len = CHARLEN(regparse);
Packit b099d7
			regparse += INCRLEN(len);
Packit b099d7
			switch (len) {
Packit b099d7
			case 1:
Packit b099d7
			case 0:
Packit b099d7
				switch (*cp) {
Packit b099d7
				case '\0':
Packit b099d7
					FAIL("trailing \\");
Packit b099d7
					break;
Packit b099d7
				case '<':
Packit b099d7
					ret = regnode(WORDA);
Packit b099d7
					break;
Packit b099d7
				case '>':
Packit b099d7
					ret = regnode(WORDZ);
Packit b099d7
					break;
Packit b099d7
					/* FIXME: Someday handle \1, \2, ... */
Packit b099d7
				default:
Packit b099d7
					goto de_fault;
Packit b099d7
				}
Packit b099d7
			default:
Packit b099d7
				/* Handle general quoted chars in exact-match routine */
Packit b099d7
				goto de_fault;
Packit b099d7
			}
Packit b099d7
			break;
Packit b099d7
		default:
Packit b099d7
			goto de_fault;
Packit b099d7
		}
Packit b099d7
		break;
Packit b099d7
de_fault:
Packit b099d7
	default:
Packit b099d7
		/*
Packit b099d7
		 * Encode a string of characters to be matched exactly.
Packit b099d7
		 *
Packit b099d7
		 * This is a bit tricky due to quoted chars and due to
Packit b099d7
		 * '*', '+', and '?' taking the SINGLE char previous
Packit b099d7
		 * as their operand.
Packit b099d7
		 *
Packit b099d7
		 * On entry, the char at regparse[-1] is going to go
Packit b099d7
		 * into the string, no matter what it is.  (It could be
Packit b099d7
		 * following a \ if we are entered from the '\' case.)
Packit b099d7
		 * 
Packit b099d7
		 * Basic idea is to pick up a good char in  ch  and
Packit b099d7
		 * examine the next char.  If it's *+? then we twiddle.
Packit b099d7
		 * If it's \ then we frozzle.  If it's other magic char
Packit b099d7
		 * we push  ch  and terminate the string.  If none of the
Packit b099d7
		 * above, we push  ch  on the string and go around again.
Packit b099d7
		 *
Packit b099d7
		 *  regprev  is used to remember where "the current char"
Packit b099d7
		 * starts in the string, if due to a *+? we need to back
Packit b099d7
		 * up and put the current char in a separate, 1-char, string.
Packit b099d7
		 * When  regprev  is NULL,  ch  is the only char in the
Packit b099d7
		 * string; this is used in *+? handling, and in setting
Packit b099d7
		 * flags |= SIMPLE at the end.
Packit b099d7
		 */
Packit b099d7
		 {
Packit b099d7
			char	*regprev;
Packit b099d7
			register char	*ch = 0, *nextc = 0;
Packit b099d7
			int	nextlen = 0;
Packit b099d7
Packit b099d7
			regparse -= INCRLEN(len);			/* Look at cur char */
Packit b099d7
			ret = regnode(EXACTLY);
Packit b099d7
			for ( regprev = 0 ; ; ) {
Packit b099d7
				ch = regparse;
Packit b099d7
				len = CHARLEN(regparse);
Packit b099d7
				regparse += INCRLEN(len);
Packit b099d7
				len = CHARLEN(regparse);
Packit b099d7
				switch (len) { /* look at length of the next one */
Packit b099d7
				default:
Packit b099d7
					regp(ch); /* add cur to string */
Packit b099d7
					break;
Packit b099d7
				case 0:
Packit b099d7
				case 1:
Packit b099d7
					switch (*regparse) {	/* look at next one */
Packit b099d7
Packit b099d7
					default:
Packit b099d7
						regp(ch); /* add cur to string */
Packit b099d7
						break;
Packit b099d7
					case '.':
Packit b099d7
					case '[':
Packit b099d7
					case '(':
Packit b099d7
					case ')':
Packit b099d7
					case '|':
Packit b099d7
					case '\n':
Packit b099d7
					case '$':
Packit b099d7
					case '^':
Packit b099d7
					case '\0':
Packit b099d7
						/* FIXME, $ and ^ should not always be magic */
Packit b099d7
magic:
Packit b099d7
						regp(ch);	/* dump cur char */
Packit b099d7
						goto done;	/* and we are done */
Packit b099d7
Packit b099d7
					case '?':
Packit b099d7
					case '+':
Packit b099d7
					case '*':
Packit b099d7
						if (!regprev) 	/* If just ch in str, */
Packit b099d7
							goto magic;	/* use it */
Packit b099d7
						/* End mult-char string one early */
Packit b099d7
						regparse = regprev; /* Back up parse */
Packit b099d7
						goto done;
Packit b099d7
Packit b099d7
					case '\\':
Packit b099d7
						regp(ch);	/* Cur char OK */
Packit b099d7
						nextc = regparse + len;
Packit b099d7
						nextlen = CHARLEN(nextc);
Packit b099d7
						switch (nextlen) {
Packit b099d7
						case 0:
Packit b099d7
						case 1:
Packit b099d7
							switch (*nextc) { /* Look after \ */
Packit b099d7
							case '\0':
Packit b099d7
							case '<':
Packit b099d7
							case '>':
Packit b099d7
								/* FIXME: Someday handle \1, \2, ... */
Packit b099d7
								goto done; /* Not quoted */
Packit b099d7
							default:
Packit b099d7
								/* Backup point is \, scan							 * point is
Packit b099d7
Packit b099d7
																								    after it. */
Packit b099d7
								regprev = regparse;
Packit b099d7
								regparse += INCRLEN(len);
Packit b099d7
								continue;	/* NOT break; */
Packit b099d7
							}
Packit b099d7
						default:
Packit b099d7
							/* Backup point is \, scan
Packit b099d7
																					                         * point is aft
Packit b099d7
							    er it. */
Packit b099d7
							regprev = regparse;
Packit b099d7
							regparse += INCRLEN(len);
Packit b099d7
							continue;       /* NOT break; */
Packit b099d7
						}
Packit b099d7
					}
Packit b099d7
				}
Packit b099d7
				regprev = regparse;	/* Set backup point */
Packit b099d7
			}
Packit b099d7
done:
Packit b099d7
			regc('\0');
Packit b099d7
			*flagp |= HASWIDTH;
Packit b099d7
			if (!regprev)		/* One char? */
Packit b099d7
				*flagp |= SIMPLE;
Packit b099d7
		}
Packit b099d7
		break;
Packit b099d7
	}
Packit b099d7
Packit b099d7
	return(ret);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regnode - emit a node
Packit b099d7
 */
Packit b099d7
static char	*			/* Location. */
Packit b099d7
regnode(char op)
Packit b099d7
{
Packit b099d7
	register char	*ret;
Packit b099d7
	register char	*ptr;
Packit b099d7
Packit b099d7
	ret = regcode;
Packit b099d7
	if (ret == &regdummy) {
Packit b099d7
		regsize += 3;
Packit b099d7
		return(ret);
Packit b099d7
	}
Packit b099d7
Packit b099d7
	ptr = ret;
Packit b099d7
	*ptr++ = op;
Packit b099d7
	*ptr++ = '\0';		/* Null "next" pointer. */
Packit b099d7
	*ptr++ = '\0';
Packit b099d7
	regcode = ptr;
Packit b099d7
Packit b099d7
	return(ret);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regc - emit (if appropriate) a character of code
Packit b099d7
 */
Packit b099d7
static void	
Packit b099d7
regp(char *p)
Packit b099d7
{
Packit b099d7
	int	len = CHARLEN(p);
Packit b099d7
Packit b099d7
	if (regcode != &regdummy)  {
Packit b099d7
		memcpy(regcode, p, len);
Packit b099d7
		regcode += INCRLEN(len);
Packit b099d7
	} else
Packit b099d7
		regsize += INCRLEN(len);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regc - emit (if appropriate) a byte of code
Packit b099d7
 */
Packit b099d7
static void	
Packit b099d7
regc(char b)
Packit b099d7
{
Packit b099d7
	if (regcode != &regdummy)
Packit b099d7
		*regcode++ = b;
Packit b099d7
	else
Packit b099d7
		regsize++;
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - reginsert - insert an operator in front of already-emitted operand
Packit b099d7
 *
Packit b099d7
 * Means relocating the operand.
Packit b099d7
 */
Packit b099d7
static void	
Packit b099d7
reginsert(char op, char *opnd)
Packit b099d7
{
Packit b099d7
	register char	*src;
Packit b099d7
	register char	*dst;
Packit b099d7
	register char	*place;
Packit b099d7
Packit b099d7
	if (regcode == &regdummy) {
Packit b099d7
		regsize += 3;
Packit b099d7
		return;
Packit b099d7
	}
Packit b099d7
Packit b099d7
	src = regcode;
Packit b099d7
	regcode += 3;
Packit b099d7
	dst = regcode;
Packit b099d7
	while (src > opnd)
Packit b099d7
		*--dst = *--src;
Packit b099d7
Packit b099d7
	place = opnd;		/* Op node, where operand used to be. */
Packit b099d7
	*place++ = op;
Packit b099d7
	*place++ = '\0';
Packit b099d7
	*place++ = '\0';
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regtail - set the next-pointer at the end of a node chain
Packit b099d7
 */
Packit b099d7
static void	
Packit b099d7
regtail(char *p, char *val)
Packit b099d7
{
Packit b099d7
	register char	*scan;
Packit b099d7
	register char	*temp;
Packit b099d7
	register int	offset;
Packit b099d7
Packit b099d7
	if (p == &regdummy)
Packit b099d7
		return;
Packit b099d7
Packit b099d7
	/* Find last node. */
Packit b099d7
	scan = p;
Packit b099d7
	for (; ; ) {
Packit b099d7
		temp = regnext(scan);
Packit b099d7
		if (temp == NULL)
Packit b099d7
			break;
Packit b099d7
		scan = temp;
Packit b099d7
	}
Packit b099d7
Packit b099d7
	if (OP(scan) == BACK)
Packit b099d7
		offset = scan - val;
Packit b099d7
	else
Packit b099d7
		offset = val - scan;
Packit b099d7
	*(scan + 1) = (offset >> 8) & 0377;
Packit b099d7
	*(scan + 2) = offset & 0377;
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regoptail - regtail on operand of first argument; nop if operandless
Packit b099d7
 */
Packit b099d7
static void	
Packit b099d7
regoptail(char *p, char *val)
Packit b099d7
{
Packit b099d7
	/* "Operandless" and "op != BRANCH" are synonymous in practice. */
Packit b099d7
	if (p == NULL || p == &regdummy || OP(p) != BRANCH)
Packit b099d7
		return;
Packit b099d7
	regtail(OPERAND(p), val);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * _XmRegexec and friends
Packit b099d7
 */
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * Global work variables for _XmRegexec().
Packit b099d7
 */
Packit b099d7
static char	*reginput;		/* String-input pointer. */
Packit b099d7
static char	*regbol;		/* Beginning of input, for ^ check. */
Packit b099d7
static char	**regstartp;	/* Pointer to startp array. */
Packit b099d7
static char	**regendp;		/* Ditto for endp. */
Packit b099d7
static int	reglmlen;		/* length of last matched character */
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * Forwards.
Packit b099d7
 */
Packit b099d7
static int regtry(XmRegexpRec *prog, char *);
Packit b099d7
static int regmatch(char *prog);
Packit b099d7
static int regrepeat(char *p);
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - _XmRegexec - match a regexp against a string
Packit b099d7
 */
Packit b099d7
int
Packit b099d7
_XmRegexec(XmRegexpRec *prog, char *string)
Packit b099d7
{
Packit b099d7
	register char	*s;
Packit b099d7
Packit b099d7
	/* Be paranoid... */
Packit b099d7
	if (prog == NULL || string == NULL) {
Packit b099d7
		return(0);
Packit b099d7
	}
Packit b099d7
Packit b099d7
	/* Check validity of program. */
Packit b099d7
	if (UCHARAT(prog->program) != MAGIC) {
Packit b099d7
		return(0);
Packit b099d7
	}
Packit b099d7
Packit b099d7
	/* If there is a "must appear" string, look for it. */
Packit b099d7
	if (prog->regmust != NULL && MB_CUR_MAX == 1) {
Packit b099d7
		s = (char *)string;
Packit b099d7
		while ((s = strchr(s, prog->regmust[0])) != NULL) {
Packit b099d7
			if (strncmp(s, prog->regmust, prog->regmlen) == 0)
Packit b099d7
				break;	/* Found it. */
Packit b099d7
			s++;
Packit b099d7
		}
Packit b099d7
		if (s == NULL)	/* Not present. */
Packit b099d7
			return(0);
Packit b099d7
	}
Packit b099d7
Packit b099d7
	/* Mark beginning of line for ^ . */
Packit b099d7
	regbol = (char *)string;
Packit b099d7
Packit b099d7
	/* Simplest case:  anchored match need be tried only once. */
Packit b099d7
	if (prog->reganch)
Packit b099d7
		return(regtry(prog, string));
Packit b099d7
Packit b099d7
	/* Messy cases:  unanchored match. */
Packit b099d7
	s = (char *)string;
Packit b099d7
	if (prog->regstart != '\0' && MB_CUR_MAX == 1)
Packit b099d7
		/* We know what char it must start with. */
Packit b099d7
		while ((s = strchr(s, prog->regstart)) != NULL) {
Packit b099d7
			if (regtry(prog, s))
Packit b099d7
				return(1);
Packit b099d7
			s++;
Packit b099d7
		}
Packit b099d7
	else
Packit b099d7
		/* We don't -- general case. */
Packit b099d7
		while(1){
Packit b099d7
			int len;
Packit b099d7
Packit b099d7
			if (regtry(prog, s))
Packit b099d7
				return(1);
Packit b099d7
			if(!(len = CHARLEN(s)))
Packit b099d7
				break;
Packit b099d7
			s += len;
Packit b099d7
		};
Packit b099d7
Packit b099d7
	/* Failure. */
Packit b099d7
	return(0);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regtry - try match at specific point
Packit b099d7
 */
Packit b099d7
static int	/* 0 failure, 1 success */
Packit b099d7
regtry(XmRegexpRec *prog, char *string)
Packit b099d7
{
Packit b099d7
	register int	i;
Packit b099d7
	register char	**sp;
Packit b099d7
	register char	**ep;
Packit b099d7
Packit b099d7
	reginput = string;
Packit b099d7
	regstartp = prog->startp;
Packit b099d7
	regendp = prog->endp;
Packit b099d7
Packit b099d7
	sp = prog->startp;
Packit b099d7
	ep = prog->endp;
Packit b099d7
	for (i = NSUBEXP; i > 0; i--) {
Packit b099d7
		*sp++ = NULL;
Packit b099d7
		*ep++ = NULL;
Packit b099d7
	}
Packit b099d7
	if (regmatch(prog->program + 1)) {
Packit b099d7
		prog->startp[0] = string;
Packit b099d7
		prog->endp[0] = reginput;
Packit b099d7
		return(1);
Packit b099d7
	} else
Packit b099d7
		return(0);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regmatch - main matching routine
Packit b099d7
 *
Packit b099d7
 * Conceptually the strategy is simple:  check to see whether the current
Packit b099d7
 * node matches, call self recursively to see whether the rest matches,
Packit b099d7
 * and then act accordingly.  In practice we make some effort to avoid
Packit b099d7
 * recursion, in particular by going through "ordinary" nodes (that don't
Packit b099d7
 * need to know whether the rest of the match failed) by a loop instead of
Packit b099d7
 * by recursion.
Packit b099d7
 */
Packit b099d7
static int	/* 0 failure, 1 success */
Packit b099d7
regmatch(char *prog)
Packit b099d7
{
Packit b099d7
	register char	*scan;	/* Current node. */
Packit b099d7
	char	*next;		/* Next node. */
Packit b099d7
	wchar_t	wc = L'\0';
Packit b099d7
	int	len;
Packit b099d7
Packit b099d7
	scan = prog;
Packit b099d7
	while (scan != NULL) {
Packit b099d7
		next = regnext(scan);
Packit b099d7
Packit b099d7
		switch (OP(scan)) {
Packit b099d7
		case BOL:
Packit b099d7
			if (reginput != regbol)
Packit b099d7
				return(0);
Packit b099d7
			break;
Packit b099d7
		case EOL:
Packit b099d7
			if (CHARLEN(reginput) != 0)
Packit b099d7
				return(0);
Packit b099d7
			break;
Packit b099d7
		case WORDA:
Packit b099d7
			/* Must be looking at a letter, digit, or _ */
Packit b099d7
			len = mbtowc(&wc, reginput, MB_CUR_MAX);
Packit b099d7
			if ((!iswalnum(wc)) && wc != L'_')
Packit b099d7
				return(0);
Packit b099d7
			/* Prev must be BOL or nonword */
Packit b099d7
			len = mbtowc(&wc, reginput - reglmlen, MB_CUR_MAX);
Packit b099d7
			if (reginput > regbol &&  (iswalnum(wc) || wc == L'_'))
Packit b099d7
				return(0);
Packit b099d7
			break;
Packit b099d7
		case WORDZ:
Packit b099d7
			len = mbtowc(&wc, reginput, MB_CUR_MAX);
Packit b099d7
			/* Must be looking at non letter, digit, or _ */
Packit b099d7
			if (iswalnum(wc) || wc == L'_')
Packit b099d7
				return(0);
Packit b099d7
			/* We don't care what the previous char was */
Packit b099d7
			break;
Packit b099d7
		case ANY:
Packit b099d7
			if (!(len = CHARLEN(reginput)))
Packit b099d7
				return(0);
Packit b099d7
			reglmlen = len;
Packit b099d7
			reginput += INCRLEN(len);
Packit b099d7
			break;
Packit b099d7
		case EXACTLY:
Packit b099d7
			 {
Packit b099d7
				register int	len;
Packit b099d7
				register int	clen;
Packit b099d7
				register char	*opnd;
Packit b099d7
				register char	*op, *ip;
Packit b099d7
Packit b099d7
				opnd = OPERAND(scan);
Packit b099d7
				len = strlen(opnd);
Packit b099d7
				for (clen = len, op = opnd, ip = reginput; clen; ) {
Packit b099d7
					int	opl = CHARLEN(op), ipl = CHARLEN(ip);
Packit b099d7
					if (opl == ipl && !strncmp(op, ip, ipl))  {
Packit b099d7
						op += ipl;
Packit b099d7
						ip += ipl;
Packit b099d7
						clen -= ipl;
Packit b099d7
						reglmlen = ipl;
Packit b099d7
					} else
Packit b099d7
						break;
Packit b099d7
				}
Packit b099d7
Packit b099d7
				if (clen)
Packit b099d7
					return(0);
Packit b099d7
				reginput += len;
Packit b099d7
			}
Packit b099d7
			break;
Packit b099d7
		case ANYOF:
Packit b099d7
			if (!(len = CHARLEN(reginput)) ||  
Packit b099d7
				!inclass(OPERAND(scan), reginput))
Packit b099d7
				return 0;
Packit b099d7
			reginput += len;
Packit b099d7
			reglmlen = len;
Packit b099d7
			break;
Packit b099d7
		case ANYBUT:
Packit b099d7
			if (!(len = CHARLEN(reginput)) ||  
Packit b099d7
				inclass(OPERAND(scan), reginput))
Packit b099d7
				return 0;
Packit b099d7
			reginput += len;
Packit b099d7
			reglmlen = len;
Packit b099d7
			break;
Packit b099d7
		case NOTHING:
Packit b099d7
			break;
Packit b099d7
		case BACK:
Packit b099d7
			break;
Packit b099d7
		case OPEN + 1:
Packit b099d7
		case OPEN + 2:
Packit b099d7
		case OPEN + 3:
Packit b099d7
		case OPEN + 4:
Packit b099d7
		case OPEN + 5:
Packit b099d7
		case OPEN + 6:
Packit b099d7
		case OPEN + 7:
Packit b099d7
		case OPEN + 8:
Packit b099d7
		case OPEN + 9:
Packit b099d7
			 {
Packit b099d7
				register int	no;
Packit b099d7
				register char	*save;
Packit b099d7
Packit b099d7
				no = OP(scan) - OPEN;
Packit b099d7
				save = reginput;
Packit b099d7
Packit b099d7
				if (regmatch(next)) {
Packit b099d7
				/*
Packit b099d7
				 * Don't set startp if some later 
Packit b099d7
				 * invocation of the same parentheses
Packit b099d7
				 * already has.
Packit b099d7
				 */
Packit b099d7
					if (regstartp[no] == NULL)
Packit b099d7
						regstartp[no] = save;
Packit b099d7
					return(1);
Packit b099d7
				} else
Packit b099d7
					return(0);
Packit b099d7
			}
Packit b099d7
			break;
Packit b099d7
		case CLOSE + 1:
Packit b099d7
		case CLOSE + 2:
Packit b099d7
		case CLOSE + 3:
Packit b099d7
		case CLOSE + 4:
Packit b099d7
		case CLOSE + 5:
Packit b099d7
		case CLOSE + 6:
Packit b099d7
		case CLOSE + 7:
Packit b099d7
		case CLOSE + 8:
Packit b099d7
		case CLOSE + 9:
Packit b099d7
			 {
Packit b099d7
				register int	no;
Packit b099d7
				register char	*save;
Packit b099d7
Packit b099d7
				no = OP(scan) - CLOSE;
Packit b099d7
				save = reginput;
Packit b099d7
Packit b099d7
				if (regmatch(next)) {
Packit b099d7
					/*
Packit b099d7
					 * Don't set endp if some later
Packit b099d7
					 * invocation of the same parentheses
Packit b099d7
					 * already has.
Packit b099d7
					 */
Packit b099d7
					if (regendp[no] == NULL)
Packit b099d7
						regendp[no] = save;
Packit b099d7
					return(1);
Packit b099d7
				} else
Packit b099d7
					return(0);
Packit b099d7
			}
Packit b099d7
			break;
Packit b099d7
		case BRANCH:
Packit b099d7
			 {
Packit b099d7
				register char	*save;
Packit b099d7
Packit b099d7
				if (OP(next) != BRANCH)		/* No choice. */
Packit b099d7
					next = OPERAND(scan);	/* Avoid recursion. */
Packit b099d7
				else {
Packit b099d7
					do {
Packit b099d7
						save = reginput;
Packit b099d7
						if (regmatch(OPERAND(scan)))
Packit b099d7
							return(1);
Packit b099d7
						reginput = save;
Packit b099d7
						scan = regnext(scan);
Packit b099d7
					} while (scan != NULL && OP(scan) == BRANCH);
Packit b099d7
					return(0);
Packit b099d7
					/* NOTREACHED */
Packit b099d7
				}
Packit b099d7
			}
Packit b099d7
			break;
Packit b099d7
		case STAR:
Packit b099d7
		case PLUS:
Packit b099d7
			 {
Packit b099d7
				register char	*nextch;
Packit b099d7
				register int	no;
Packit b099d7
				register char	*save;
Packit b099d7
				register int	min;
Packit b099d7
				int	nchars = 0;
Packit b099d7
Packit b099d7
				/*
Packit b099d7
				 * Lookahead to avoid useless match attempts
Packit b099d7
				 * when we know what character comes next.
Packit b099d7
				 */
Packit b099d7
				nextch = 0;
Packit b099d7
				if (OP(next) == EXACTLY)
Packit b099d7
					nextch = OPERAND(next);
Packit b099d7
				min = (OP(scan) == STAR) ? 0 : 1;
Packit b099d7
				save = reginput;
Packit b099d7
				no = regrepeat(OPERAND(scan));
Packit b099d7
				while (no >= min) {
Packit b099d7
					/* If it could work, try it. */
Packit b099d7
					if (!nextch || !(len = CHARLEN(nextch)) ||  
Packit b099d7
						!strncmp(reginput, nextch, len) )
Packit b099d7
						if (regmatch(next))
Packit b099d7
							return(1);
Packit b099d7
					/* Couldn't or didn't -- back up. */
Packit b099d7
					no--;
Packit b099d7
					reginput = save;
Packit b099d7
					for (nchars = 0; nchars < no; nchars++)
Packit b099d7
						reginput += CHARLEN(reginput);
Packit b099d7
				}
Packit b099d7
				return(0);
Packit b099d7
			}
Packit b099d7
			break;
Packit b099d7
		case END:
Packit b099d7
			return(1);	/* Success! */
Packit b099d7
			break;
Packit b099d7
		default:
Packit b099d7
			return(0);
Packit b099d7
			break;
Packit b099d7
		}
Packit b099d7
Packit b099d7
		scan = next;
Packit b099d7
	}
Packit b099d7
Packit b099d7
	/*
Packit b099d7
	 * We get here only if there's trouble -- normally "case END" is
Packit b099d7
	 * the terminating point.
Packit b099d7
	 */
Packit b099d7
	return(0);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regrepeat - repeatedly match something simple, report how many
Packit b099d7
 */
Packit b099d7
static int	
Packit b099d7
regrepeat(char *p)
Packit b099d7
{
Packit b099d7
	register int	count = 0, len = 0;
Packit b099d7
	register char	*scan;
Packit b099d7
	register char	*opnd;
Packit b099d7
Packit b099d7
	scan = reginput;
Packit b099d7
	opnd = OPERAND(p);
Packit b099d7
	switch (OP(p)) {
Packit b099d7
	case ANY:
Packit b099d7
		while ((len = CHARLEN(scan))) {
Packit b099d7
			count++;
Packit b099d7
			scan += len;
Packit b099d7
			reglmlen = len;
Packit b099d7
		}
Packit b099d7
		break;
Packit b099d7
	case EXACTLY:
Packit b099d7
		 {
Packit b099d7
			int	len = 0;
Packit b099d7
Packit b099d7
			len = CHARLEN(opnd);
Packit b099d7
Packit b099d7
			while (len && 
Packit b099d7
				(CHARLEN(scan) == len) &&  
Packit b099d7
					!strncmp(opnd, scan, len)) {
Packit b099d7
				count++;
Packit b099d7
				scan += len;
Packit b099d7
				reglmlen = len;
Packit b099d7
			}
Packit b099d7
		}
Packit b099d7
		break;
Packit b099d7
	case ANYOF:
Packit b099d7
		while ((len = CHARLEN(scan)) &&  
Packit b099d7
			inclass(opnd, scan)) {
Packit b099d7
			count++;
Packit b099d7
			scan += len;
Packit b099d7
			reglmlen = len;
Packit b099d7
		}
Packit b099d7
		break;
Packit b099d7
	case ANYBUT:
Packit b099d7
		while ((len = CHARLEN(scan)) &&  
Packit b099d7
			!inclass(opnd, scan)) {
Packit b099d7
			count++;
Packit b099d7
			scan += len;
Packit b099d7
			reglmlen = len;
Packit b099d7
		}
Packit b099d7
		break;
Packit b099d7
	default:		/* Oh dear.  Called inappropriately. */
Packit b099d7
		count = 0;	/* Best compromise. */
Packit b099d7
		break;
Packit b099d7
	}
Packit b099d7
	reginput = scan;
Packit b099d7
Packit b099d7
	return(count);
Packit b099d7
}
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 - regnext - dig the "next" pointer out of a node
Packit b099d7
 */
Packit b099d7
static char	*
Packit b099d7
regnext(char *p)
Packit b099d7
{
Packit b099d7
	register int	offset;
Packit b099d7
Packit b099d7
	if (p == &regdummy)
Packit b099d7
		return(NULL);
Packit b099d7
Packit b099d7
	offset = NEXT(p);
Packit b099d7
	if (offset == 0)
Packit b099d7
		return(NULL);
Packit b099d7
Packit b099d7
	if (OP(p) == BACK)
Packit b099d7
		return(p - offset);
Packit b099d7
	else
Packit b099d7
		return(p + offset);
Packit b099d7
}