xref: /illumos-gate/usr/src/head/regexp.h (revision 9acbbeaf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9acbbeafSnn  * Common Development and Distribution License (the "License").
6*9acbbeafSnn  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*9acbbeafSnn  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #ifndef _REGEXP_H
317c478bd9Sstevel@tonic-gate #define	_REGEXP_H
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.9	*/
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
387c478bd9Sstevel@tonic-gate extern "C" {
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	CBRA	2
427c478bd9Sstevel@tonic-gate #define	CCHR	4
437c478bd9Sstevel@tonic-gate #define	CDOT	8
447c478bd9Sstevel@tonic-gate #define	CCL	12
457c478bd9Sstevel@tonic-gate #define	CXCL	16
467c478bd9Sstevel@tonic-gate #define	CDOL	20
477c478bd9Sstevel@tonic-gate #define	CCEOF	22
487c478bd9Sstevel@tonic-gate #define	CKET	24
497c478bd9Sstevel@tonic-gate #define	CBACK	36
507c478bd9Sstevel@tonic-gate #define	NCCL	40
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	STAR	01
537c478bd9Sstevel@tonic-gate #define	RNGE	03
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	NBRA	9
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	PLACE(c)	ep[c >> 3] |= bittab[c & 07]
587c478bd9Sstevel@tonic-gate #define	ISTHERE(c)	(ep[c >> 3] & bittab[c & 07])
597c478bd9Sstevel@tonic-gate #define	ecmp(s1, s2, n)	(strncmp(s1, s2, n) == 0)
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static char	*braslist[NBRA];
627c478bd9Sstevel@tonic-gate static char	*braelist[NBRA];
637c478bd9Sstevel@tonic-gate int	sed, nbra;
647c478bd9Sstevel@tonic-gate char	*loc1, *loc2, *locs;
657c478bd9Sstevel@tonic-gate static int	nodelim;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate int	circf;
687c478bd9Sstevel@tonic-gate static int	low;
697c478bd9Sstevel@tonic-gate static int	size;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static unsigned char	bittab[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #ifdef	__STDC__
747c478bd9Sstevel@tonic-gate int advance(const char *lp, const char *ep);
757c478bd9Sstevel@tonic-gate static void getrnge(const char *str);
767c478bd9Sstevel@tonic-gate #else
777c478bd9Sstevel@tonic-gate int advance();
787c478bd9Sstevel@tonic-gate static void getrnge();
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate char *
827c478bd9Sstevel@tonic-gate #ifdef	__STDC__
837c478bd9Sstevel@tonic-gate compile(char *instring, char *ep, const char *endbuf, int seof)
847c478bd9Sstevel@tonic-gate #else
857c478bd9Sstevel@tonic-gate compile(instring, ep, endbuf, seof)
867c478bd9Sstevel@tonic-gate register char *ep;
877c478bd9Sstevel@tonic-gate char *instring, *endbuf;
887c478bd9Sstevel@tonic-gate int seof;
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	INIT	/* Dependent declarations and initializations */
927c478bd9Sstevel@tonic-gate 	register int c;
937c478bd9Sstevel@tonic-gate 	register int eof = seof;
947c478bd9Sstevel@tonic-gate 	char *lastep;
957c478bd9Sstevel@tonic-gate 	int cclcnt;
967c478bd9Sstevel@tonic-gate 	char bracket[NBRA], *bracketp;
977c478bd9Sstevel@tonic-gate 	int closed;
987c478bd9Sstevel@tonic-gate 	int neg;
997c478bd9Sstevel@tonic-gate 	int lc;
1007c478bd9Sstevel@tonic-gate 	int i, cflg;
1017c478bd9Sstevel@tonic-gate 	int iflag; /* used for non-ascii characters in brackets */
1027c478bd9Sstevel@tonic-gate 
103*9acbbeafSnn #ifdef __lint
104*9acbbeafSnn 	/* make lint happy */
105*9acbbeafSnn 	c = nodelim;
106*9acbbeafSnn #endif
107*9acbbeafSnn 
1087c478bd9Sstevel@tonic-gate 	lastep = NULL;
1097c478bd9Sstevel@tonic-gate 	if ((c = GETC()) == eof || c == '\n') {
1107c478bd9Sstevel@tonic-gate 		if (c == '\n') {
1117c478bd9Sstevel@tonic-gate 			UNGETC(c);
1127c478bd9Sstevel@tonic-gate 			nodelim = 1;
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 		if (*ep == 0 && !sed)
1157c478bd9Sstevel@tonic-gate 			ERROR(41);
1167c478bd9Sstevel@tonic-gate 		RETURN(ep);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	bracketp = bracket;
1197c478bd9Sstevel@tonic-gate 	circf = closed = nbra = 0;
1207c478bd9Sstevel@tonic-gate 	if (c == '^')
1217c478bd9Sstevel@tonic-gate 		circf++;
1227c478bd9Sstevel@tonic-gate 	else
1237c478bd9Sstevel@tonic-gate 		UNGETC(c);
124*9acbbeafSnn 	for (;;) {
1257c478bd9Sstevel@tonic-gate 		if (ep >= endbuf)
1267c478bd9Sstevel@tonic-gate 			ERROR(50);
1277c478bd9Sstevel@tonic-gate 		c = GETC();
1287c478bd9Sstevel@tonic-gate 		if (c != '*' && ((c != '\\') || (PEEKC() != '{')))
1297c478bd9Sstevel@tonic-gate 			lastep = ep;
1307c478bd9Sstevel@tonic-gate 		if (c == eof) {
1317c478bd9Sstevel@tonic-gate 			*ep++ = CCEOF;
1327c478bd9Sstevel@tonic-gate 			if (bracketp != bracket)
1337c478bd9Sstevel@tonic-gate 				ERROR(42);
1347c478bd9Sstevel@tonic-gate 			RETURN(ep);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		switch (c) {
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		case '.':
1397c478bd9Sstevel@tonic-gate 			*ep++ = CDOT;
1407c478bd9Sstevel@tonic-gate 			continue;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		case '\n':
1437c478bd9Sstevel@tonic-gate 			if (!sed) {
1447c478bd9Sstevel@tonic-gate 				UNGETC(c);
1457c478bd9Sstevel@tonic-gate 				*ep++ = CCEOF;
1467c478bd9Sstevel@tonic-gate 				nodelim = 1;
1477c478bd9Sstevel@tonic-gate 				if (bracketp != bracket)
1487c478bd9Sstevel@tonic-gate 					ERROR(42);
1497c478bd9Sstevel@tonic-gate 				RETURN(ep);
1507c478bd9Sstevel@tonic-gate 			} else ERROR(36);
1517c478bd9Sstevel@tonic-gate 		case '*':
1527c478bd9Sstevel@tonic-gate 			if (lastep == NULL || *lastep == CBRA ||
1537c478bd9Sstevel@tonic-gate 			    *lastep == CKET)
1547c478bd9Sstevel@tonic-gate 				goto defchar;
1557c478bd9Sstevel@tonic-gate 			*lastep |= STAR;
1567c478bd9Sstevel@tonic-gate 			continue;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		case '$':
1597c478bd9Sstevel@tonic-gate 			if (PEEKC() != eof && PEEKC() != '\n')
1607c478bd9Sstevel@tonic-gate 				goto defchar;
1617c478bd9Sstevel@tonic-gate 			*ep++ = CDOL;
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		case '[':
1657c478bd9Sstevel@tonic-gate 			if (&ep[17] >= endbuf)
1667c478bd9Sstevel@tonic-gate 				ERROR(50);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 			*ep++ = CCL;
1697c478bd9Sstevel@tonic-gate 			lc = 0;
1707c478bd9Sstevel@tonic-gate 			for (i = 0; i < 16; i++)
1717c478bd9Sstevel@tonic-gate 				ep[i] = 0;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 			neg = 0;
1747c478bd9Sstevel@tonic-gate 			if ((c = GETC()) == '^') {
1757c478bd9Sstevel@tonic-gate 				neg = 1;
1767c478bd9Sstevel@tonic-gate 				c = GETC();
1777c478bd9Sstevel@tonic-gate 			}
1787c478bd9Sstevel@tonic-gate 			iflag = 1;
1797c478bd9Sstevel@tonic-gate 			do {
1807c478bd9Sstevel@tonic-gate 				c &= 0377;
1817c478bd9Sstevel@tonic-gate 				if (c == '\0' || c == '\n')
1827c478bd9Sstevel@tonic-gate 					ERROR(49);
1837c478bd9Sstevel@tonic-gate 				if ((c & 0200) && iflag) {
1847c478bd9Sstevel@tonic-gate 					iflag = 0;
1857c478bd9Sstevel@tonic-gate 					if (&ep[32] >= endbuf)
1867c478bd9Sstevel@tonic-gate 						ERROR(50);
1877c478bd9Sstevel@tonic-gate 					ep[-1] = CXCL;
1887c478bd9Sstevel@tonic-gate 					for (i = 16; i < 32; i++)
1897c478bd9Sstevel@tonic-gate 						ep[i] = 0;
1907c478bd9Sstevel@tonic-gate 				}
1917c478bd9Sstevel@tonic-gate 				if (c == '-' && lc != 0) {
1927c478bd9Sstevel@tonic-gate 					if ((c = GETC()) == ']') {
1937c478bd9Sstevel@tonic-gate 						PLACE('-');
1947c478bd9Sstevel@tonic-gate 						break;
1957c478bd9Sstevel@tonic-gate 					}
1967c478bd9Sstevel@tonic-gate 					if ((c & 0200) && iflag) {
1977c478bd9Sstevel@tonic-gate 						iflag = 0;
1987c478bd9Sstevel@tonic-gate 						if (&ep[32] >= endbuf)
1997c478bd9Sstevel@tonic-gate 							ERROR(50);
2007c478bd9Sstevel@tonic-gate 						ep[-1] = CXCL;
2017c478bd9Sstevel@tonic-gate 						for (i = 16; i < 32; i++)
2027c478bd9Sstevel@tonic-gate 							ep[i] = 0;
2037c478bd9Sstevel@tonic-gate 					}
2047c478bd9Sstevel@tonic-gate 					while (lc < c) {
2057c478bd9Sstevel@tonic-gate 						PLACE(lc);
2067c478bd9Sstevel@tonic-gate 						lc++;
2077c478bd9Sstevel@tonic-gate 					}
2087c478bd9Sstevel@tonic-gate 				}
2097c478bd9Sstevel@tonic-gate 				lc = c;
2107c478bd9Sstevel@tonic-gate 				PLACE(c);
2117c478bd9Sstevel@tonic-gate 			} while ((c = GETC()) != ']');
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 			if (iflag)
2147c478bd9Sstevel@tonic-gate 				iflag = 16;
2157c478bd9Sstevel@tonic-gate 			else
2167c478bd9Sstevel@tonic-gate 				iflag = 32;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 			if (neg) {
2197c478bd9Sstevel@tonic-gate 				if (iflag == 32) {
2207c478bd9Sstevel@tonic-gate 					for (cclcnt = 0; cclcnt < iflag;
2217c478bd9Sstevel@tonic-gate 					    cclcnt++)
2227c478bd9Sstevel@tonic-gate 						ep[cclcnt] ^= 0377;
2237c478bd9Sstevel@tonic-gate 					ep[0] &= 0376;
2247c478bd9Sstevel@tonic-gate 				} else {
2257c478bd9Sstevel@tonic-gate 					ep[-1] = NCCL;
2267c478bd9Sstevel@tonic-gate 					/* make nulls match so test fails */
2277c478bd9Sstevel@tonic-gate 					ep[0] |= 01;
2287c478bd9Sstevel@tonic-gate 				}
2297c478bd9Sstevel@tonic-gate 			}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			ep += iflag;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 			continue;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		case '\\':
2367c478bd9Sstevel@tonic-gate 			switch (c = GETC()) {
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 			case '(':
2397c478bd9Sstevel@tonic-gate 				if (nbra >= NBRA)
2407c478bd9Sstevel@tonic-gate 					ERROR(43);
2417c478bd9Sstevel@tonic-gate 				*bracketp++ = (char)nbra;
2427c478bd9Sstevel@tonic-gate 				*ep++ = CBRA;
2437c478bd9Sstevel@tonic-gate 				*ep++ = (char)nbra++;
2447c478bd9Sstevel@tonic-gate 				continue;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 			case ')':
2477c478bd9Sstevel@tonic-gate 				if (bracketp <= bracket)
2487c478bd9Sstevel@tonic-gate 					ERROR(42);
2497c478bd9Sstevel@tonic-gate 				*ep++ = CKET;
2507c478bd9Sstevel@tonic-gate 				*ep++ = *--bracketp;
2517c478bd9Sstevel@tonic-gate 				closed++;
2527c478bd9Sstevel@tonic-gate 				continue;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 			case '{':
2557c478bd9Sstevel@tonic-gate 				if (lastep == NULL)
2567c478bd9Sstevel@tonic-gate 					goto defchar;
2577c478bd9Sstevel@tonic-gate 				*lastep |= RNGE;
2587c478bd9Sstevel@tonic-gate 				cflg = 0;
2597c478bd9Sstevel@tonic-gate 			nlim:
2607c478bd9Sstevel@tonic-gate 				c = GETC();
2617c478bd9Sstevel@tonic-gate 				i = 0;
2627c478bd9Sstevel@tonic-gate 				do {
2637c478bd9Sstevel@tonic-gate 					if ('0' <= c && c <= '9')
2647c478bd9Sstevel@tonic-gate 						i = 10 * i + c - '0';
2657c478bd9Sstevel@tonic-gate 					else
2667c478bd9Sstevel@tonic-gate 						ERROR(16);
2677c478bd9Sstevel@tonic-gate 				} while (((c = GETC()) != '\\') && (c != ','));
2687c478bd9Sstevel@tonic-gate 				if (i >= 255)
2697c478bd9Sstevel@tonic-gate 					ERROR(11);
2707c478bd9Sstevel@tonic-gate 				*ep++ = (char)i;
2717c478bd9Sstevel@tonic-gate 				if (c == ',') {
2727c478bd9Sstevel@tonic-gate 					if (cflg++)
2737c478bd9Sstevel@tonic-gate 						ERROR(44);
2747c478bd9Sstevel@tonic-gate 					if ((c = GETC()) == '\\')
2757c478bd9Sstevel@tonic-gate 						*ep++ = (char)255;
2767c478bd9Sstevel@tonic-gate 					else {
2777c478bd9Sstevel@tonic-gate 						UNGETC(c);
2787c478bd9Sstevel@tonic-gate 						goto nlim;
2797c478bd9Sstevel@tonic-gate 						/* get 2'nd number */
2807c478bd9Sstevel@tonic-gate 					}
2817c478bd9Sstevel@tonic-gate 				}
2827c478bd9Sstevel@tonic-gate 				if (GETC() != '}')
2837c478bd9Sstevel@tonic-gate 					ERROR(45);
2847c478bd9Sstevel@tonic-gate 				if (!cflg)	/* one number */
2857c478bd9Sstevel@tonic-gate 					*ep++ = (char)i;
2867c478bd9Sstevel@tonic-gate 				else if ((ep[-1] & 0377) < (ep[-2] & 0377))
2877c478bd9Sstevel@tonic-gate 					ERROR(46);
2887c478bd9Sstevel@tonic-gate 				continue;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 			case '\n':
2917c478bd9Sstevel@tonic-gate 				ERROR(36);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 			case 'n':
2947c478bd9Sstevel@tonic-gate 				c = '\n';
2957c478bd9Sstevel@tonic-gate 				goto defchar;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 			default:
2987c478bd9Sstevel@tonic-gate 				if (c >= '1' && c <= '9') {
2997c478bd9Sstevel@tonic-gate 					if ((c -= '1') >= closed)
3007c478bd9Sstevel@tonic-gate 						ERROR(25);
3017c478bd9Sstevel@tonic-gate 					*ep++ = CBACK;
3027c478bd9Sstevel@tonic-gate 					*ep++ = (char)c;
3037c478bd9Sstevel@tonic-gate 					continue;
3047c478bd9Sstevel@tonic-gate 				}
3057c478bd9Sstevel@tonic-gate 			}
3067c478bd9Sstevel@tonic-gate 	/* Drop through to default to use \ to turn off special chars */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		defchar:
3097c478bd9Sstevel@tonic-gate 		default:
3107c478bd9Sstevel@tonic-gate 			lastep = ep;
3117c478bd9Sstevel@tonic-gate 			*ep++ = CCHR;
3127c478bd9Sstevel@tonic-gate 			*ep++ = (char)c;
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 	}
315*9acbbeafSnn 	/*NOTREACHED*/
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate #ifdef	__STDC__
3197c478bd9Sstevel@tonic-gate int
3207c478bd9Sstevel@tonic-gate step(const char *p1, const char *p2)
3217c478bd9Sstevel@tonic-gate #else
3227c478bd9Sstevel@tonic-gate int
3237c478bd9Sstevel@tonic-gate step(p1, p2)
3247c478bd9Sstevel@tonic-gate register char *p1, *p2;
3257c478bd9Sstevel@tonic-gate #endif
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	char c;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (circf) {
3317c478bd9Sstevel@tonic-gate 		loc1 = (char *)p1;
3327c478bd9Sstevel@tonic-gate 		return (advance(p1, p2));
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	/* fast check for first character */
3357c478bd9Sstevel@tonic-gate 	if (*p2 == CCHR) {
3367c478bd9Sstevel@tonic-gate 		c = p2[1];
3377c478bd9Sstevel@tonic-gate 		do {
3387c478bd9Sstevel@tonic-gate 			if (*p1 != c)
3397c478bd9Sstevel@tonic-gate 				continue;
3407c478bd9Sstevel@tonic-gate 			if (advance(p1, p2)) {
3417c478bd9Sstevel@tonic-gate 				loc1 = (char *)p1;
3427c478bd9Sstevel@tonic-gate 				return (1);
3437c478bd9Sstevel@tonic-gate 			}
3447c478bd9Sstevel@tonic-gate 		} while (*p1++);
3457c478bd9Sstevel@tonic-gate 		return (0);
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 		/* regular algorithm */
3487c478bd9Sstevel@tonic-gate 	do {
3497c478bd9Sstevel@tonic-gate 		if (advance(p1, p2)) {
3507c478bd9Sstevel@tonic-gate 			loc1 = (char *)p1;
3517c478bd9Sstevel@tonic-gate 			return (1);
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 	} while (*p1++);
3547c478bd9Sstevel@tonic-gate 	return (0);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate int
3587c478bd9Sstevel@tonic-gate #ifdef	__STDC__
3597c478bd9Sstevel@tonic-gate advance(const char *lp, const char *ep)
3607c478bd9Sstevel@tonic-gate #else
3617c478bd9Sstevel@tonic-gate advance(lp, ep)
3627c478bd9Sstevel@tonic-gate register char *lp, *ep;
3637c478bd9Sstevel@tonic-gate #endif
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate #ifdef	__STDC__
3667c478bd9Sstevel@tonic-gate 	const char *curlp;
3677c478bd9Sstevel@tonic-gate #else
3687c478bd9Sstevel@tonic-gate 	register char *curlp;
3697c478bd9Sstevel@tonic-gate #endif
3707c478bd9Sstevel@tonic-gate 	int c;
3717c478bd9Sstevel@tonic-gate 	char *bbeg;
3727c478bd9Sstevel@tonic-gate 	register char neg;
3737c478bd9Sstevel@tonic-gate 	size_t ct;
3747c478bd9Sstevel@tonic-gate 
375*9acbbeafSnn 	for (;;) {
3767c478bd9Sstevel@tonic-gate 		neg = 0;
3777c478bd9Sstevel@tonic-gate 		switch (*ep++) {
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		case CCHR:
3807c478bd9Sstevel@tonic-gate 			if (*ep++ == *lp++)
3817c478bd9Sstevel@tonic-gate 				continue;
3827c478bd9Sstevel@tonic-gate 			return (0);
3837c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		case CDOT:
3867c478bd9Sstevel@tonic-gate 			if (*lp++)
3877c478bd9Sstevel@tonic-gate 				continue;
3887c478bd9Sstevel@tonic-gate 			return (0);
3897c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		case CDOL:
3927c478bd9Sstevel@tonic-gate 			if (*lp == 0)
3937c478bd9Sstevel@tonic-gate 				continue;
3947c478bd9Sstevel@tonic-gate 			return (0);
3957c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		case CCEOF:
3987c478bd9Sstevel@tonic-gate 			loc2 = (char *)lp;
3997c478bd9Sstevel@tonic-gate 			return (1);
4007c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		case CXCL:
4037c478bd9Sstevel@tonic-gate 			c = (unsigned char)*lp++;
4047c478bd9Sstevel@tonic-gate 			if (ISTHERE(c)) {
4057c478bd9Sstevel@tonic-gate 				ep += 32;
4067c478bd9Sstevel@tonic-gate 				continue;
4077c478bd9Sstevel@tonic-gate 			}
4087c478bd9Sstevel@tonic-gate 			return (0);
4097c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 		case NCCL:
4127c478bd9Sstevel@tonic-gate 			neg = 1;
4137c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		case CCL:
4167c478bd9Sstevel@tonic-gate 			c = *lp++;
4177c478bd9Sstevel@tonic-gate 			if (((c & 0200) == 0 && ISTHERE(c)) ^ neg) {
4187c478bd9Sstevel@tonic-gate 				ep += 16;
4197c478bd9Sstevel@tonic-gate 				continue;
4207c478bd9Sstevel@tonic-gate 			}
4217c478bd9Sstevel@tonic-gate 			return (0);
4227c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		case CBRA:
4257c478bd9Sstevel@tonic-gate 			braslist[*ep++] = (char *)lp;
4267c478bd9Sstevel@tonic-gate 			continue;
4277c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		case CKET:
4307c478bd9Sstevel@tonic-gate 			braelist[*ep++] = (char *)lp;
4317c478bd9Sstevel@tonic-gate 			continue;
4327c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		case CCHR | RNGE:
4357c478bd9Sstevel@tonic-gate 			c = *ep++;
4367c478bd9Sstevel@tonic-gate 			getrnge(ep);
4377c478bd9Sstevel@tonic-gate 			while (low--)
4387c478bd9Sstevel@tonic-gate 				if (*lp++ != c)
4397c478bd9Sstevel@tonic-gate 					return (0);
4407c478bd9Sstevel@tonic-gate 			curlp = lp;
4417c478bd9Sstevel@tonic-gate 			while (size--)
4427c478bd9Sstevel@tonic-gate 				if (*lp++ != c)
4437c478bd9Sstevel@tonic-gate 					break;
4447c478bd9Sstevel@tonic-gate 			if (size < 0)
4457c478bd9Sstevel@tonic-gate 				lp++;
4467c478bd9Sstevel@tonic-gate 			ep += 2;
4477c478bd9Sstevel@tonic-gate 			goto star;
4487c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		case CDOT | RNGE:
4517c478bd9Sstevel@tonic-gate 			getrnge(ep);
4527c478bd9Sstevel@tonic-gate 			while (low--)
4537c478bd9Sstevel@tonic-gate 				if (*lp++ == '\0')
4547c478bd9Sstevel@tonic-gate 					return (0);
4557c478bd9Sstevel@tonic-gate 			curlp = lp;
4567c478bd9Sstevel@tonic-gate 			while (size--)
4577c478bd9Sstevel@tonic-gate 				if (*lp++ == '\0')
4587c478bd9Sstevel@tonic-gate 					break;
4597c478bd9Sstevel@tonic-gate 			if (size < 0)
4607c478bd9Sstevel@tonic-gate 				lp++;
4617c478bd9Sstevel@tonic-gate 			ep += 2;
4627c478bd9Sstevel@tonic-gate 			goto star;
4637c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		case CXCL | RNGE:
4667c478bd9Sstevel@tonic-gate 			getrnge(ep + 32);
4677c478bd9Sstevel@tonic-gate 			while (low--) {
4687c478bd9Sstevel@tonic-gate 				c = (unsigned char)*lp++;
4697c478bd9Sstevel@tonic-gate 				if (!ISTHERE(c))
4707c478bd9Sstevel@tonic-gate 					return (0);
4717c478bd9Sstevel@tonic-gate 			}
4727c478bd9Sstevel@tonic-gate 			curlp = lp;
4737c478bd9Sstevel@tonic-gate 			while (size--) {
4747c478bd9Sstevel@tonic-gate 				c = (unsigned char)*lp++;
4757c478bd9Sstevel@tonic-gate 				if (!ISTHERE(c))
4767c478bd9Sstevel@tonic-gate 					break;
4777c478bd9Sstevel@tonic-gate 			}
4787c478bd9Sstevel@tonic-gate 			if (size < 0)
4797c478bd9Sstevel@tonic-gate 				lp++;
4807c478bd9Sstevel@tonic-gate 			ep += 34;		/* 32 + 2 */
4817c478bd9Sstevel@tonic-gate 			goto star;
4827c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		case NCCL | RNGE:
4857c478bd9Sstevel@tonic-gate 			neg = 1;
4867c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		case CCL | RNGE:
4897c478bd9Sstevel@tonic-gate 			getrnge(ep + 16);
4907c478bd9Sstevel@tonic-gate 			while (low--) {
4917c478bd9Sstevel@tonic-gate 				c = *lp++;
4927c478bd9Sstevel@tonic-gate 				if (((c & 0200) || !ISTHERE(c)) ^ neg)
4937c478bd9Sstevel@tonic-gate 					return (0);
4947c478bd9Sstevel@tonic-gate 			}
4957c478bd9Sstevel@tonic-gate 			curlp = lp;
4967c478bd9Sstevel@tonic-gate 			while (size--) {
4977c478bd9Sstevel@tonic-gate 				c = *lp++;
4987c478bd9Sstevel@tonic-gate 				if (((c & 0200) || !ISTHERE(c)) ^ neg)
4997c478bd9Sstevel@tonic-gate 					break;
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 			if (size < 0)
5027c478bd9Sstevel@tonic-gate 				lp++;
5037c478bd9Sstevel@tonic-gate 			ep += 18; 		/* 16 + 2 */
5047c478bd9Sstevel@tonic-gate 			goto star;
5057c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		case CBACK:
5087c478bd9Sstevel@tonic-gate 			bbeg = braslist[*ep];
5097c478bd9Sstevel@tonic-gate 			ct = braelist[*ep++] - bbeg;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 			if (ecmp(bbeg, lp, ct)) {
5127c478bd9Sstevel@tonic-gate 				lp += ct;
5137c478bd9Sstevel@tonic-gate 				continue;
5147c478bd9Sstevel@tonic-gate 			}
5157c478bd9Sstevel@tonic-gate 			return (0);
5167c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		case CBACK | STAR:
5197c478bd9Sstevel@tonic-gate 			bbeg = braslist[*ep];
5207c478bd9Sstevel@tonic-gate 			ct = braelist[*ep++] - bbeg;
5217c478bd9Sstevel@tonic-gate 			curlp = lp;
5227c478bd9Sstevel@tonic-gate 			while (ecmp(bbeg, lp, ct))
5237c478bd9Sstevel@tonic-gate 				lp += ct;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 			while (lp >= curlp) {
5267c478bd9Sstevel@tonic-gate 				if (advance(lp, ep))
5277c478bd9Sstevel@tonic-gate 					return (1);
5287c478bd9Sstevel@tonic-gate 				lp -= ct;
5297c478bd9Sstevel@tonic-gate 			}
5307c478bd9Sstevel@tonic-gate 			return (0);
5317c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		case CDOT | STAR:
5347c478bd9Sstevel@tonic-gate 			curlp = lp;
5357c478bd9Sstevel@tonic-gate 			while (*lp++);
5367c478bd9Sstevel@tonic-gate 			goto star;
5377c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		case CCHR | STAR:
5407c478bd9Sstevel@tonic-gate 			curlp = lp;
5417c478bd9Sstevel@tonic-gate 			while (*lp++ == *ep);
5427c478bd9Sstevel@tonic-gate 			ep++;
5437c478bd9Sstevel@tonic-gate 			goto star;
5447c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		case CXCL | STAR:
5477c478bd9Sstevel@tonic-gate 			curlp = lp;
5487c478bd9Sstevel@tonic-gate 			do {
5497c478bd9Sstevel@tonic-gate 				c = (unsigned char)*lp++;
5507c478bd9Sstevel@tonic-gate 			} while (ISTHERE(c));
5517c478bd9Sstevel@tonic-gate 			ep += 32;
5527c478bd9Sstevel@tonic-gate 			goto star;
5537c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		case NCCL | STAR:
5567c478bd9Sstevel@tonic-gate 			neg = 1;
5577c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		case CCL | STAR:
5607c478bd9Sstevel@tonic-gate 			curlp = lp;
5617c478bd9Sstevel@tonic-gate 			do {
5627c478bd9Sstevel@tonic-gate 				c = *lp++;
5637c478bd9Sstevel@tonic-gate 			} while (((c & 0200) == 0 && ISTHERE(c)) ^ neg);
5647c478bd9Sstevel@tonic-gate 			ep += 16;
5657c478bd9Sstevel@tonic-gate 			goto star;
5667c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		star:
5697c478bd9Sstevel@tonic-gate 			do {
5707c478bd9Sstevel@tonic-gate 				if (--lp == locs)
5717c478bd9Sstevel@tonic-gate 					break;
5727c478bd9Sstevel@tonic-gate 				if (advance(lp, ep))
5737c478bd9Sstevel@tonic-gate 					return (1);
5747c478bd9Sstevel@tonic-gate 			} while (lp > curlp);
5757c478bd9Sstevel@tonic-gate 			return (0);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 	}
579*9acbbeafSnn 	/*NOTREACHED*/
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate static void
5837c478bd9Sstevel@tonic-gate #ifdef	__STDC__
5847c478bd9Sstevel@tonic-gate getrnge(const char *str)
5857c478bd9Sstevel@tonic-gate #else
5867c478bd9Sstevel@tonic-gate getrnge(str)
5877c478bd9Sstevel@tonic-gate register char *str;
5887c478bd9Sstevel@tonic-gate #endif
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	low = *str++ & 0377;
5917c478bd9Sstevel@tonic-gate 	size = ((*str & 0377) == 255)? 20000: (*str &0377) - low;
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate #endif
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate #endif	/* _REGEXP_H */
599