/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* bos720 src/bos/usr/include/regexp.h 1.9.1.7                            */
/*                                                                        */
/* Licensed Materials - Property of IBM                                   */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 1985,1994              */
/* All Rights Reserved                                                    */
/*                                                                        */
/* US Government Users Restricted Rights - Use, duplication or            */
/* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.      */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/* @(#)40  1.9.1.7  src/bos/usr/include/regexp.h, libcgen, bos720 6/1/07 13:10:12 */
/*
 * COMPONENT_NAME: (LIBCGEN) Standard C Library General Functions
 *
 * FUNCTIONS: 
 *
 * ORIGINS: 27
 *
 * (C) COPYRIGHT International Business Machines Corp. 1985, 1994
 * All Rights Reserved
 * Licensed Materials - Property of IBM
 *
 * US Government Users Restricted Rights - Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */
#ifndef _H_REGEXP
#define _H_REGEXP

#ifdef __cplusplus
extern "C" {
#endif

/*
 * These interfaces are obsolete and are not 64-bit safe.
 * The 32-bit versions will still be supported, but the 64-bit versions
 * will simply fail (set errno) and return a 0.
 * Applications should migrate to the fnmatch(), glob(), regcomp() and
 * regexec() functions which provide full internationalized regular expression
 * functionality compatible with ISO 9945-1:1996 (IEEE POSIX 1003.1) and
 * with the UNIX98 specification.
 */

#ifdef __64BIT__
char	*loc1, *loc2, *locs;

#include <errno.h>
char *
compile(char *instring, register char *ep, const char *endbuf, int seof)
{
	errno = ENOSYS;
	ERROR(50);		/* Not supported in 64bit mode. */
}

int advance(const char *lp, const char *ep)
{
	errno = ENOSYS;
	return(0);		/* Not supported in 64bit mode. */
}

int step(const char *p1, const char *p2)
{
	errno = ENOSYS;
	return(0);		/* Not supported in 64bit mode. */
}

#else /* these are ok for 32bit mode */

#if defined(_ALL_SOURCE) && !defined(__ia64)
#include	<NLregexp.h>	/* handle internationalised expressions */
#else	/* _ALL_SOURCE */
				
#define	CBRA	2
#define	CCHR	4
#define	CDOT	8
#define	CCL	12
#define	CDOL	20
#define	CCEOF	22
#define	CKET	24
#define	CBACK	36

#define	STAR	01
#define RNGE	03

#define	NBRA	9

#define PLACE(c)	ep[c >> 3] |= bittab[c & 07]
#define ISTHERE(c)	(ep[c >> 3] & bittab[c & 07])

char	*braslist[NBRA];
char	*braelist[NBRA];
char	*loc1, *loc2, *locs;
char	bittab[] = { 1, 2, 4, 8, 16, 32, 64, 128 };

int     sed, nbra;
int	ebra;
int	nodelim;
int	circf;
int	low;
int	size;

int advance(const char *, const char *);
void getrnge(char *);
int ecmp(char *, char *, int);

char *
compile(char *instring,
        register char *ep,
        const char *endbuf,
        int seof)
{
	INIT	/* Dependent declarations and initializations */
	register int c;
	register int eof = seof;
	char *lastep = instring;
	int cclcnt;
	char bracket[NBRA], *bracketp;
	int closed;
	char neg;
	int lc;
	int i, cflg;

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

		case '.':
			*ep++ = CDOT;
			continue;

		case '\n':
			if(!sed) {
				UNGETC(c);
				*ep++ = CCEOF;
				nodelim = 1;
				RETURN(ep);
			}
			else ERROR(36);
		case '*':
			if(lastep == 0 || *lastep == CBRA || *lastep == CKET)
				goto defchar;
			*lastep |= STAR;
			continue;

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

		case '[':
			if(&ep[17] >= endbuf)
				ERROR(50);

			*ep++ = CCL;
			lc = 0;
			for(i = 0; i < 16; i++)
				ep[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()) == ']') {
						PLACE('-');
						break;
					}
					while(lc < c) {
						PLACE(lc);
						lc++;
					}
				}
				lc = c;
				PLACE(c);
			} while((c = GETC()) != ']');
			if(neg) {
				for(cclcnt = 0; cclcnt < 16; cclcnt++)
					ep[cclcnt] ^= -1;
				ep[0] &= 0376;
			}

			ep += 16;

			continue;

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

			case '(':
				if(nbra >= NBRA)
					ERROR(43);
				*bracketp++ = nbra;
				*ep++ = CBRA;
				*ep++ = nbra++;
				continue;

			case ')':
				if(bracketp <= bracket || ++ebra != nbra)
					ERROR(42);
				*ep++ = CKET;
				*ep++ = *--bracketp;
				closed++;
				continue;

			case '{':
				if(lastep == (char *) 0)
					goto defchar;
				*lastep |= 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);
				*ep++ = i;
				if(c == ',') {
					if(cflg++)
						ERROR(44);
					if((c = GETC()) == '\\')
						*ep++ = 255;
					else {
						UNGETC(c);
						goto nlim;
						/* get 2'nd number */
					}
				}
				if(GETC() != '}')
					ERROR(45);
				if(!cflg)	/* one number */
					*ep++ = i;
				else if((ep[-1] & 0377) < (ep[-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);
					*ep++ = CBACK;
					*ep++ = c;
					continue;
				}
			}
	/* Drop through to default to use \ to turn off special chars */

		defchar:
		default:
			lastep = ep;
			*ep++ = CCHR;
			*ep++ = c;
		}
	}
}

int step(const char *p1, const char *p2)
{
	register int c;

	if(circf) {
		loc1 = (char *)p1;
		return(advance(p1, p2));
	}
	/* fast check for first character */
	if(*p2 == 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);
}

int advance(const char *lp, const char *ep)
{
	char *curlp;
	char c;
	char *bbeg;
	int ct;

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

		case CCHR:
			if(*ep++ == *lp++)
				continue;
			return(0);
	
		case CDOT:
			if(*lp++)
				continue;
			return(0);
	
		case CDOL:
			if(*lp == 0)
				continue;
			return(0);
	
		case CCEOF:
			loc2 = (char *)lp;
			return(1);
	
		case CCL:
			c = *lp++ & 0177;
			if(ISTHERE(c)) {
				ep += 16;
				continue;
			}
			return(0);
		case CBRA:
			braslist[*ep++] = (char *)lp;
			continue;
	
		case CKET:
			braelist[*ep++] = (char *)lp;
			continue;
	
		case CCHR | RNGE:
			c = *ep++;
			getrnge((char *)ep);
			while(low--)
				if(*lp++ != c)
					return(0);
			curlp = (char *)lp;
			while(size--) 
				if(*lp++ != c)
					break;
			if(size < 0)
				lp++;
			ep += 2;
			goto star;
	
		case CDOT | RNGE:
			getrnge((char *)ep);
			while(low--)
				if(*lp++ == '\0')
					return(0);
			curlp = (char *)lp;
			while(size--)
				if(*lp++ == '\0')
					break;
			if(size < 0)
				lp++;
			ep += 2;
			goto star;
	
		case CCL | RNGE:
			getrnge((char *)ep + 16);
			while(low--) {
				c = *lp++ & 0177;
				if(!ISTHERE(c))
					return(0);
			}
			curlp = (char *)lp;
			while(size--) {
				c = *lp++ & 0177;
				if(!ISTHERE(c))
					break;
			}
			if(size < 0)
				lp++;
			ep += 18;		/* 16 + 2 */
			goto star;
	
		case CBACK:
			bbeg = braslist[*ep];
			ct = braelist[*ep++] - bbeg;
	
			if(ecmp(bbeg, (char *)lp, ct)) {
				lp += ct;
				continue;
			}
			return(0);
	
		case CBACK | STAR:
			bbeg = braslist[*ep];
			ct = braelist[*ep++] - bbeg;
			curlp = (char *)lp;
			while(ecmp(bbeg, (char *)lp, ct))
				lp += ct;
	
			while(lp >= curlp) {
				if(advance(lp, ep))	return(1);
				lp -= ct;
			}
			return(0);
	
	
		case CDOT | STAR:
			curlp = (char *)lp;
			while(*lp++);
			goto star;
	
		case CCHR | STAR:
			curlp = (char *)lp;
			while(*lp++ == *ep);
			ep++;
			goto star;
	
		case CCL | STAR:
			curlp = (char *)lp;
			do {
				c = *lp++ & 0177;
			} while(ISTHERE(c));
			ep += 16;
			goto star;
	
		star:
			do {
				if(--lp == locs)
					break;
				if(advance(lp, ep))
					return(1);
			} while(lp > curlp);
			return(0);

		}
	}
}

void getrnge(register char *str)
{
	low = *str++ & 0377;
	size = (*str == 255)? 20000: (*str &0377) - low;
}

int ecmp(char *a, char *b, int count)
{
	while(count--)
		if(*a++ != *b++)
			return(0);
	return(1);
}

#endif	/* _ALL_SOURCE */
#endif /* __64BIT__ */

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _H_REGEXP */