/*
 * $Header: /smsa/bsd:include/regexp.h:bsd  1.4  $
 * $Source: /smsa/bsd:include/regexp.h: $
 *
 * Copyright (c) 1989 Acorn Computers Ltd., Cambridge, England
 *
 * Taken from the System V Release 3 source, copyrighted as below.
 */
/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ifndef __regexp_h
#define __regexp_h

typedef unsigned char Uchar;

#define	_RE_CBRA	2
#define	_RE_CCHR	4
#define	_RE_CDOT	8
#define	_RE_CCL		12
#define	_RE_CDOL	20
#define	_RE_CCEOF	22
#define	_RE_CKET	24
#define	_RE_CBACK	36

#define	_RE_STAR	01
#define _RE_RNGE	03

#define	_RE_NBRA	9

static char	*_RE_braslist[_RE_NBRA];
static char	*_RE_braelist[_RE_NBRA];
static int	_RE_ebra;
static int	_RE_nodelim;
static int	_RE_low;
static int	_RE_size;

#ifdef _XOPEN_C
static int sed, nbra, circf;
#else
int sed, nbra, circf;
#endif
char	*loc1, *loc2, *locs;


char *
compile(_instring, _expbuf, _endbuf, eof)
char *_expbuf;
char *_instring, *_endbuf;
int eof;
{
	unsigned char *instring = (unsigned char *) _instring;
	unsigned char *expbuf = (unsigned char *) _expbuf;
	unsigned char *endbuf = (unsigned char *) _endbuf;

	INIT	/* Dependent declarations and initializations */
#define _RE_PLACE(c)	expbuf[c >> 3] |= (1 << (c & 07))
#define _RE_ISTHERE(c)	(expbuf[c >> 3] & (1 << (c & 07)))
	register c;
	Uchar *lastep = instring;
	int cclcnt;
	Uchar bracket[_RE_NBRA], *bracketp;
	int closed;
	int neg;
	int lc;
	int i, cflg;

	lastep = 0;
	if((c = GETC()) == eof || c == '\n') {
		if(c == '\n') {
			UNGETC(c);
			_RE_nodelim = 1;
		}
		if(*expbuf == 0 && !sed)
			ERROR(41);
		RETURN(((char*)expbuf));
	}
	bracketp = bracket;
	circf = closed = nbra = _RE_ebra = 0;
	if(c == '^')
		circf++;
	else
		UNGETC(c);
	while(1) {
		if(expbuf >= endbuf)
			ERROR(50);
		c = GETC();
		if(c != '*' && ((c != '\\') || (PEEKC() != '{')))
			lastep = expbuf;
		if(c == eof) {
			*expbuf++ = _RE_CCEOF;
			RETURN(((char *)expbuf));
		}
		switch(c) {

		case '.':
			*expbuf++ = _RE_CDOT;
			continue;

		case '\n':
			if(!sed) {
				UNGETC(c);
				*expbuf++ = _RE_CCEOF;
				_RE_nodelim = 1;
				RETURN(((char *)expbuf));
			}
			else ERROR(36);
		case '*':
			if(lastep == 0 || *lastep == _RE_CBRA || *lastep == _RE_CKET)
				goto defchar;
			*lastep |= _RE_STAR;
			continue;

		case '$':
			if(PEEKC() != eof && PEEKC() != '\n')
				goto defchar;
			*expbuf++ = _RE_CDOL;
			continue;

		case '[':
			if(&expbuf[33] >= endbuf)
				ERROR(50);

			*expbuf++ = _RE_CCL;
			lc = 0;
			for(i = 0; i < 32; i++)
				expbuf[i] = 0;

			neg = 0;
			if((c = GETC()) == '^') {
				neg = 1;
				c = GETC();
			}

			do {
				if(c == '\0' || c == '\n')
					ERROR(49);
				if(c == '-' && lc != 0) {
					if((c = GETC()) == ']') {
						_RE_PLACE('-');
						break;
					}
					while(lc < c) {
						_RE_PLACE(lc);
						lc++;
					}
				}
				lc = c;
				_RE_PLACE(c);
			} while((c = GETC()) != ']');
			if(neg) {
				for(cclcnt = 0; cclcnt < 32; cclcnt++)
					expbuf[cclcnt] ^= -1;
				expbuf[0] &= 0376;
			}

			expbuf += 32;

			continue;

		case '\\':
			switch(c = GETC()) {

			case '(':
				if(nbra >= _RE_NBRA)
					ERROR(43);
				*bracketp++ = nbra;
				*expbuf++ = _RE_CBRA;
				*expbuf++ = nbra++;
				continue;

			case ')':
				if(bracketp <= bracket || ++_RE_ebra != nbra)
					ERROR(42);
				*expbuf++ = _RE_CKET;
				*expbuf++ = *--bracketp;
				closed++;
				continue;

			case '{':
				if(lastep == (Uchar *) 0)
					goto defchar;
				*lastep |= _RE_RNGE;
				cflg = 0;
			nlim:
				c = GETC();
				i = 0;
				do {
					if('0' <= c && c <= '9')
						i = 10 * i + c - '0';
					else
						ERROR(16);
				} while(((c = GETC()) != '\\') && (c != ','));
				if(i > 255)
					ERROR(11);
				*expbuf++ = i;
				if(c == ',') {
					if(cflg++)
						ERROR(44);
					if((c = GETC()) == '\\')
						*expbuf++ = 255;
					else {
						UNGETC(c);
						goto nlim;
						/* get 2'nd number */
					}
				}
				if(GETC() != '}')
					ERROR(45);
				if(!cflg)	/* one number */
					*expbuf++ = i;
				else if((expbuf[-1] & 0377) < (expbuf[-2] & 0377))
					ERROR(46);
				continue;

			case '\n':
				ERROR(36);

			case 'n':
				c = '\n';
				goto defchar;

			default:
				if(c >= '1' && c <= '9') {
					if((c -= '1') >= closed)
						ERROR(25);
					*expbuf++ = _RE_CBACK;
					*expbuf++ = c;
					continue;
				}
			}
	/* Drop through to default to use \ to turn off special chars */

		defchar:
		default:
			lastep = expbuf;
			*expbuf++ = _RE_CCHR;
			*expbuf++ = c;
		}
	}
}

step(_p1, _p2)
char *_p1, *_p2;
{
	Uchar *p1 = (Uchar *) _p1;
	Uchar *p2 = (Uchar *) _p2;
	register c;

	if(circf) {
		loc1 = (char *)p1;
		return(advance(p1, p2));
	}
	/* fast check for first character */
	if(*p2 == _RE_CCHR) {
		c = p2[1];
		do {
			if(*p1 != c)
				continue;
			if(advance(p1, p2)) {
				loc1 = (char *)p1;
				return(1);
			}
		} while(*p1++);
		return(0);
	}
		/* regular algorithm */
	do {
		if(advance(p1, p2)) {
			loc1 = (char *)p1;
			return(1);
		}
	} while(*p1++);
	return(0);
}

advance(_lp, _expbuf)
char *_lp, *_expbuf;
{
	Uchar *lp = (Uchar *) _lp;
	Uchar *expbuf = (Uchar *) _expbuf;
	register Uchar *curlp;
	int c;
	Uchar *bbeg;
	int ct;

#define _RE_ecmp(s1, s2, n)	(!strncmp(s1, s2, n))

	while(1) {
		switch(*expbuf++) {

		case _RE_CCHR:
			if(*expbuf++ == *lp++)
				continue;
			return(0);
	
		case _RE_CDOT:
			if(*lp++)
				continue;
			return(0);
	
		case _RE_CDOL:
			if(*lp == 0)
				continue;
			return(0);
	
		case _RE_CCEOF:
			loc2 = (char *)lp;
			return(1);
	
		case _RE_CCL:
			c = *lp++ & 0377;
			if(_RE_ISTHERE(c)) {
				expbuf += 32;
				continue;
			}
			return(0);
		case _RE_CBRA:
			_RE_braslist[*expbuf++] = (char *)lp;
			continue;
	
		case _RE_CKET:
			_RE_braelist[*expbuf++] = (char *)lp;
			continue;
	
		case _RE_CCHR | _RE_RNGE:
			c = *expbuf++;
			_RE_getrnge(expbuf);
			while(_RE_low--)
				if(*lp++ != c)
					return(0);
			curlp = lp;
			while(_RE_size--) 
				if(*lp++ != c)
					break;
			if(_RE_size < 0)
				lp++;
			expbuf += 2;
			goto star;
	
		case _RE_CDOT | _RE_RNGE:
			_RE_getrnge(expbuf);
			while(_RE_low--)
				if(*lp++ == '\0')
					return(0);
			curlp = lp;
			while(_RE_size--)
				if(*lp++ == '\0')
					break;
			if(_RE_size < 0)
				lp++;
			expbuf += 2;
			goto star;
	
		case _RE_CCL | _RE_RNGE:
			_RE_getrnge(expbuf + 32);
			while(_RE_low--) {
				c = *lp++ & 0377;
				if(!_RE_ISTHERE(c))
					return(0);
			}
			curlp = lp;
			while(_RE_size--) {
				c = *lp++ & 0377;
				if(!_RE_ISTHERE(c))
					break;
			}
			if(_RE_size < 0)
				lp++;
			expbuf += 34;		/* 32 + 2 */
			goto star;
	
		case _RE_CBACK:
			bbeg = (Uchar *)(_RE_braslist[*expbuf]);
			ct = ((Uchar *)(_RE_braelist[*expbuf++])) - bbeg;
	
			if(_RE_ecmp(bbeg, lp, ct)) {
				lp += ct;
				continue;
			}
			return(0);
	
		case _RE_CBACK | _RE_STAR:
			bbeg = (Uchar *)(_RE_braslist[*expbuf]);
			ct = ((Uchar *)(_RE_braelist[*expbuf++])) - bbeg;
			curlp = lp;
			while(_RE_ecmp(bbeg, lp, ct))
				lp += ct;
	
			while(lp >= curlp) {
				if(advance(lp, expbuf))	return(1);
				lp -= ct;
			}
			return(0);
	
	
		case _RE_CDOT | _RE_STAR:
			curlp = lp;
			while(*lp++);
			goto star;
	
		case _RE_CCHR | _RE_STAR:
			curlp = lp;
			while(*lp++ == *expbuf);
			expbuf++;
			goto star;
	
		case _RE_CCL | _RE_STAR:
			curlp = lp;
			do {
				c = *lp++ & 0377;
			} while(_RE_ISTHERE(c));
			expbuf += 32;
			goto star;
	
		star:
			do {
				if(--lp == (Uchar *)locs)
					break;
				if(advance(lp, expbuf))
					return(1);
			} while(lp > curlp);
			return(0);

		}
	}
}

static
_RE_getrnge(str)
register unsigned char *str;
{
	_RE_low = *str++;
	_RE_size = (*str == 255)? 20000: *str - _RE_low;
}

#endif/*__regexp_h*/

/* EOF regexp.h */
