xref: /onnv-gate/usr/src/lib/libc/port/gen/regexpr.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * routines to do regular expression matching
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * Entry points:
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *	re_comp(s)
380Sstevel@tonic-gate  *		char *s;
390Sstevel@tonic-gate  *	 ... returns 0 if the string s was compiled successfully,
400Sstevel@tonic-gate  *		     a pointer to an error message otherwise.
410Sstevel@tonic-gate  *	     If passed 0 or a null string returns without changing
420Sstevel@tonic-gate  *           the currently compiled re (see note 11 below).
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  *	re_exec(s)
450Sstevel@tonic-gate  *		char *s;
460Sstevel@tonic-gate  *	 ... returns 1 if the string s matches the last compiled regular
470Sstevel@tonic-gate  *		       expression,
480Sstevel@tonic-gate  *		     0 if the string s failed to match the last compiled
490Sstevel@tonic-gate  *		       regular expression, and
500Sstevel@tonic-gate  *		    -1 if the compiled regular expression was invalid
510Sstevel@tonic-gate  *		       (indicating an internal error).
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The strings passed to both re_comp and re_exec may have trailing or
540Sstevel@tonic-gate  * embedded newline characters; they are terminated by nulls.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * The identity of the author of these routines is lost in antiquity;
570Sstevel@tonic-gate  * this is essentially the same as the re code in the original V6 ed.
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * The regular expressions recognized are described below. This description
600Sstevel@tonic-gate  * is essentially the same as that for ed.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  *	A regular expression specifies a set of strings of characters.
630Sstevel@tonic-gate  *	A member of this set of strings is said to be matched by
640Sstevel@tonic-gate  *	the regular expression.  In the following specification for
650Sstevel@tonic-gate  *	regular expressions the word `character' means any character but NUL.
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  *	1.  Any character except a special character matches itself.
680Sstevel@tonic-gate  *	    Special characters are the regular expression delimiter plus
690Sstevel@tonic-gate  *	    \ [ . and sometimes ^ * $.
700Sstevel@tonic-gate  *	2.  A . matches any character.
710Sstevel@tonic-gate  *	3.  A \ followed by any character except a digit or ( )
720Sstevel@tonic-gate  *	    matches that character.
730Sstevel@tonic-gate  *	4.  A nonempty string s bracketed [s] (or [^s]) matches any
740Sstevel@tonic-gate  *	    character in (or not in) s. In s, \ has no special meaning,
750Sstevel@tonic-gate  *	    and ] may only appear as the first letter. A substring
760Sstevel@tonic-gate  *	    a-b, with a and b in ascending ASCII order, stands for
770Sstevel@tonic-gate  *	    the inclusive range of ASCII characters.
780Sstevel@tonic-gate  *	5.  A regular expression of form 1-4 followed by * matches a
790Sstevel@tonic-gate  *	    sequence of 0 or more matches of the regular expression.
800Sstevel@tonic-gate  *	6.  A regular expression, x, of form 1-8, bracketed \(x\)
810Sstevel@tonic-gate  *	    matches what x matches.
820Sstevel@tonic-gate  *	7.  A \ followed by a digit n matches a copy of the string that the
830Sstevel@tonic-gate  *	    bracketed regular expression beginning with the nth \( matched.
840Sstevel@tonic-gate  *	8.  A regular expression of form 1-8, x, followed by a regular
850Sstevel@tonic-gate  *	    expression of form 1-7, y matches a match for x followed by
860Sstevel@tonic-gate  *	    a match for y, with the x match being as long as possible
870Sstevel@tonic-gate  *	    while still permitting a y match.
880Sstevel@tonic-gate  *	9.  A regular expression of form 1-8 preceded by ^ (or followed
890Sstevel@tonic-gate  *	    by $), is constrained to matches that begin at the left
900Sstevel@tonic-gate  *	    (or end at the right) end of a line.
910Sstevel@tonic-gate  *	10. A regular expression of form 1-9 picks out the longest among
920Sstevel@tonic-gate  *	    the leftmost matches in a line.
930Sstevel@tonic-gate  *	11. An empty regular expression stands for a copy of the last
940Sstevel@tonic-gate  *	    regular expression encountered.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
97*6812Sraf #include "lint.h"
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #include <stdlib.h>
1000Sstevel@tonic-gate #include <re_comp.h>
1010Sstevel@tonic-gate #include <stddef.h>
1020Sstevel@tonic-gate #include <sys/types.h>
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * constants for re's
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate #define	CBRA	1
1080Sstevel@tonic-gate #define	CCHR	2
1090Sstevel@tonic-gate #define	CDOT	4
1100Sstevel@tonic-gate #define	CCL	6
1110Sstevel@tonic-gate #define	NCCL	8
1120Sstevel@tonic-gate #define	CDOL	10
1130Sstevel@tonic-gate #define	CEOF	11
1140Sstevel@tonic-gate #define	CKET	12
1150Sstevel@tonic-gate #define	CBACK	18
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #define	CSTAR	01
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate #define	ESIZE	512
1200Sstevel@tonic-gate #define	NBRA	9
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static struct re_globals {
1230Sstevel@tonic-gate 	char	_expbuf[ESIZE];
1240Sstevel@tonic-gate 	char	*_braslist[NBRA], *_braelist[NBRA];
1250Sstevel@tonic-gate 	char	_circf;
1260Sstevel@tonic-gate } *re_globals;
1270Sstevel@tonic-gate #define	expbuf (_re->_expbuf)
1280Sstevel@tonic-gate #define	braslist (_re->_braslist)
1290Sstevel@tonic-gate #define	braelist (_re->_braelist)
1300Sstevel@tonic-gate #define	circf (_re->_circf)
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static int advance(const char *, char *);
1330Sstevel@tonic-gate static int backref(int, const char *);
1340Sstevel@tonic-gate static int cclass(char *, char, int);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * compile the regular expression argument into a dfa
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate char *
re_comp(const char * sp)1400Sstevel@tonic-gate re_comp(const char *sp)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	char	c;
1430Sstevel@tonic-gate 	struct re_globals *_re = re_globals;
1440Sstevel@tonic-gate 	char	*ep;
1450Sstevel@tonic-gate 	char	cclcnt, numbra = 0;
1460Sstevel@tonic-gate 	char	*lastep = NULL;
1470Sstevel@tonic-gate 	char	bracket[NBRA];
1480Sstevel@tonic-gate 	char	*bracketp = &bracket[0];
1490Sstevel@tonic-gate 	char	*retoolong = "Regular expression too long";
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (_re == NULL) {
1520Sstevel@tonic-gate 		_re = (struct re_globals *)calloc(1, sizeof (*_re));
1530Sstevel@tonic-gate 		if (_re == NULL)
1540Sstevel@tonic-gate 			return ("Out of memory");
1550Sstevel@tonic-gate 		re_globals = _re;
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 	ep = expbuf;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate #define	comerr(msg) {expbuf[0] = 0; return (msg); }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (sp == NULL || *sp == '\0') {
1620Sstevel@tonic-gate 		if (*ep == 0)
1630Sstevel@tonic-gate 			return ("No previous regular expression");
1640Sstevel@tonic-gate 		return (NULL);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 	if (*sp == '^') {
1670Sstevel@tonic-gate 		circf = 1;
1680Sstevel@tonic-gate 		sp++;
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	else
1710Sstevel@tonic-gate 		circf = 0;
1720Sstevel@tonic-gate 	for (;;) {
1730Sstevel@tonic-gate 		if (ep >= &expbuf[ESIZE])
1740Sstevel@tonic-gate 			comerr(retoolong);
1750Sstevel@tonic-gate 		if ((c = *sp++) == '\0') {
1760Sstevel@tonic-gate 			if (bracketp != bracket)
1770Sstevel@tonic-gate 				comerr("unmatched \\(");
1780Sstevel@tonic-gate 			*ep++ = CEOF;
1790Sstevel@tonic-gate 			*ep++ = 0;
1800Sstevel@tonic-gate 			return (NULL);
1810Sstevel@tonic-gate 		}
1820Sstevel@tonic-gate 		if (c != '*')
1830Sstevel@tonic-gate 			lastep = ep;
1840Sstevel@tonic-gate 		switch (c) {
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		case '.':
1870Sstevel@tonic-gate 			*ep++ = CDOT;
1880Sstevel@tonic-gate 			continue;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		case '*':
1910Sstevel@tonic-gate 			if (lastep == NULL || *lastep == CBRA ||
1920Sstevel@tonic-gate 			    *lastep == CKET)
1930Sstevel@tonic-gate 				goto defchar;
1940Sstevel@tonic-gate 			*lastep |= CSTAR;
1950Sstevel@tonic-gate 			continue;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		case '$':
1980Sstevel@tonic-gate 			if (*sp != '\0')
1990Sstevel@tonic-gate 				goto defchar;
2000Sstevel@tonic-gate 			*ep++ = CDOL;
2010Sstevel@tonic-gate 			continue;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 		case '[':
2040Sstevel@tonic-gate 			*ep++ = CCL;
2050Sstevel@tonic-gate 			*ep++ = 0;
2060Sstevel@tonic-gate 			cclcnt = 1;
2070Sstevel@tonic-gate 			if ((c = *sp++) == '^') {
2080Sstevel@tonic-gate 				c = *sp++;
2090Sstevel@tonic-gate 				ep[-2] = NCCL;
2100Sstevel@tonic-gate 			}
2110Sstevel@tonic-gate 			do {
2120Sstevel@tonic-gate 				if (c == '\0')
2130Sstevel@tonic-gate 					comerr("missing ]");
2140Sstevel@tonic-gate 				if (c == '-' && ep [-1] != 0) {
2150Sstevel@tonic-gate 					if ((c = *sp++) == ']') {
2160Sstevel@tonic-gate 						*ep++ = '-';
2170Sstevel@tonic-gate 						cclcnt++;
2180Sstevel@tonic-gate 						break;
2190Sstevel@tonic-gate 					}
2200Sstevel@tonic-gate 					while (ep[-1] < c) {
2210Sstevel@tonic-gate 						*ep = ep[-1] + 1;
2220Sstevel@tonic-gate 						ep++;
2230Sstevel@tonic-gate 						cclcnt++;
2240Sstevel@tonic-gate 						if (ep >= &expbuf[ESIZE])
2250Sstevel@tonic-gate 							comerr(retoolong);
2260Sstevel@tonic-gate 					}
2270Sstevel@tonic-gate 				}
2280Sstevel@tonic-gate 				*ep++ = c;
2290Sstevel@tonic-gate 				cclcnt++;
2300Sstevel@tonic-gate 				if (ep >= &expbuf[ESIZE])
2310Sstevel@tonic-gate 					comerr(retoolong);
2320Sstevel@tonic-gate 			} while ((c = *sp++) != ']');
2330Sstevel@tonic-gate 			lastep[1] = cclcnt;
2340Sstevel@tonic-gate 			continue;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		case '\\':
2370Sstevel@tonic-gate 			if ((c = *sp++) == '(') {
2380Sstevel@tonic-gate 				if (numbra >= NBRA)
2390Sstevel@tonic-gate 					comerr("too many \\(\\) pairs");
2400Sstevel@tonic-gate 				*bracketp++ = numbra;
2410Sstevel@tonic-gate 				*ep++ = CBRA;
2420Sstevel@tonic-gate 				*ep++ = numbra++;
2430Sstevel@tonic-gate 				continue;
2440Sstevel@tonic-gate 			}
2450Sstevel@tonic-gate 			if (c == ')') {
2460Sstevel@tonic-gate 				if (bracketp <= bracket)
2470Sstevel@tonic-gate 					comerr("unmatched \\)");
2480Sstevel@tonic-gate 				*ep++ = CKET;
2490Sstevel@tonic-gate 				*ep++ = *--bracketp;
2500Sstevel@tonic-gate 				continue;
2510Sstevel@tonic-gate 			}
2520Sstevel@tonic-gate 			if (c >= '1' && c < ('1' + NBRA)) {
2530Sstevel@tonic-gate 				*ep++ = CBACK;
2540Sstevel@tonic-gate 				*ep++ = c - '1';
2550Sstevel@tonic-gate 				continue;
2560Sstevel@tonic-gate 			}
2570Sstevel@tonic-gate 			*ep++ = CCHR;
2580Sstevel@tonic-gate 			*ep++ = c;
2590Sstevel@tonic-gate 			continue;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		defchar:
2620Sstevel@tonic-gate 		default:
2630Sstevel@tonic-gate 			*ep++ = CCHR;
2640Sstevel@tonic-gate 			*ep++ = c;
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * match the argument string against the compiled re
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate int
re_exec(const char * p1)2730Sstevel@tonic-gate re_exec(const char *p1)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	struct re_globals *_re = re_globals;
2760Sstevel@tonic-gate 	char	*p2;
2770Sstevel@tonic-gate 	int	c;
2780Sstevel@tonic-gate 	int	rv;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if (_re == NULL)
2810Sstevel@tonic-gate 		return (0);
2820Sstevel@tonic-gate 	p2 = expbuf;
2830Sstevel@tonic-gate 	for (c = 0; c < NBRA; c++) {
2840Sstevel@tonic-gate 		braslist[c] = 0;
2850Sstevel@tonic-gate 		braelist[c] = 0;
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 	if (circf)
2880Sstevel@tonic-gate 		return ((advance(p1, p2)));
2890Sstevel@tonic-gate 	/*
2900Sstevel@tonic-gate 	 * fast check for first character
2910Sstevel@tonic-gate 	 */
2920Sstevel@tonic-gate 	if (*p2 == CCHR) {
2930Sstevel@tonic-gate 		c = p2[1];
2940Sstevel@tonic-gate 		do {
2950Sstevel@tonic-gate 			if (*p1 != c)
2960Sstevel@tonic-gate 				continue;
2970Sstevel@tonic-gate 			if (rv = advance(p1, p2))
2980Sstevel@tonic-gate 				return (rv);
2990Sstevel@tonic-gate 		} while (*p1++);
3000Sstevel@tonic-gate 		return (0);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 	/*
3030Sstevel@tonic-gate 	 * regular algorithm
3040Sstevel@tonic-gate 	 */
305*6812Sraf 	do {
3060Sstevel@tonic-gate 		if (rv = advance(p1, p2))
3070Sstevel@tonic-gate 			return (rv);
308*6812Sraf 	} while (*p1++);
3090Sstevel@tonic-gate 	return (0);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * try to match the next thing in the dfa
3140Sstevel@tonic-gate  */
3150Sstevel@tonic-gate static int
advance(const char * lp,char * ep)3160Sstevel@tonic-gate advance(const char *lp, char *ep)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	const char	*curlp;
3190Sstevel@tonic-gate 	ptrdiff_t	ct;
3200Sstevel@tonic-gate 	int		i;
3210Sstevel@tonic-gate 	int		rv;
3220Sstevel@tonic-gate 	struct re_globals *_re = re_globals;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	for (;;)
3250Sstevel@tonic-gate 		switch (*ep++) {
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		case CCHR:
3280Sstevel@tonic-gate 			if (*ep++ == *lp++)
3290Sstevel@tonic-gate 				continue;
3300Sstevel@tonic-gate 			return (0);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		case CDOT:
3330Sstevel@tonic-gate 			if (*lp++)
3340Sstevel@tonic-gate 				continue;
3350Sstevel@tonic-gate 			return (0);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		case CDOL:
3380Sstevel@tonic-gate 			if (*lp == '\0')
3390Sstevel@tonic-gate 				continue;
3400Sstevel@tonic-gate 			return (0);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		case CEOF:
3430Sstevel@tonic-gate 			return (1);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		case CCL:
3460Sstevel@tonic-gate 			if (cclass(ep, *lp++, 1)) {
3470Sstevel@tonic-gate 				ep += *ep;
3480Sstevel@tonic-gate 				continue;
3490Sstevel@tonic-gate 			}
3500Sstevel@tonic-gate 			return (0);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 		case NCCL:
3530Sstevel@tonic-gate 			if (cclass(ep, *lp++, 0)) {
3540Sstevel@tonic-gate 				ep += *ep;
3550Sstevel@tonic-gate 				continue;
3560Sstevel@tonic-gate 			}
3570Sstevel@tonic-gate 			return (0);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		case CBRA:
3600Sstevel@tonic-gate 			braslist[*ep++] = (char *)lp;
3610Sstevel@tonic-gate 			continue;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 		case CKET:
3640Sstevel@tonic-gate 			braelist[*ep++] = (char *)lp;
3650Sstevel@tonic-gate 			continue;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		case CBACK:
3680Sstevel@tonic-gate 			if (braelist[i = *ep++] == NULL)
3690Sstevel@tonic-gate 				return (-1);
3700Sstevel@tonic-gate 			if (backref(i, lp)) {
3710Sstevel@tonic-gate 				lp += braelist[i] - braslist[i];
3720Sstevel@tonic-gate 				continue;
3730Sstevel@tonic-gate 			}
3740Sstevel@tonic-gate 			return (0);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		case CBACK|CSTAR:
3770Sstevel@tonic-gate 			if (braelist[i = *ep++] == NULL)
3780Sstevel@tonic-gate 				return (-1);
3790Sstevel@tonic-gate 			curlp = lp;
3800Sstevel@tonic-gate 			ct = braelist[i] - braslist[i];
3810Sstevel@tonic-gate 			while (backref(i, lp))
3820Sstevel@tonic-gate 				lp += ct;
3830Sstevel@tonic-gate 			while (lp >= curlp) {
3840Sstevel@tonic-gate 				if (rv = advance(lp, ep))
3850Sstevel@tonic-gate 					return (rv);
3860Sstevel@tonic-gate 				lp -= ct;
3870Sstevel@tonic-gate 			}
3880Sstevel@tonic-gate 			continue;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 		case CDOT|CSTAR:
3910Sstevel@tonic-gate 			curlp = lp;
3920Sstevel@tonic-gate 			while (*lp++)
3930Sstevel@tonic-gate 				;
3940Sstevel@tonic-gate 			goto star;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		case CCHR|CSTAR:
3970Sstevel@tonic-gate 			curlp = lp;
3980Sstevel@tonic-gate 			while (*lp++ == *ep)
3990Sstevel@tonic-gate 				;
4000Sstevel@tonic-gate 			ep++;
4010Sstevel@tonic-gate 			goto star;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		case CCL|CSTAR:
4040Sstevel@tonic-gate 		case NCCL|CSTAR:
4050Sstevel@tonic-gate 			curlp = lp;
4060Sstevel@tonic-gate 			while (cclass(ep, *lp++, ep[-1] == (CCL|CSTAR)))
4070Sstevel@tonic-gate 				;
4080Sstevel@tonic-gate 			ep += *ep;
4090Sstevel@tonic-gate 			goto star;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		star:
4120Sstevel@tonic-gate 			do {
4130Sstevel@tonic-gate 				lp--;
4140Sstevel@tonic-gate 				if (rv = advance(lp, ep))
4150Sstevel@tonic-gate 					return (rv);
4160Sstevel@tonic-gate 			} while (lp > curlp);
4170Sstevel@tonic-gate 			return (0);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		default:
4200Sstevel@tonic-gate 			return (-1);
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate static int
backref(int i,const char * lp)4250Sstevel@tonic-gate backref(int i, const char *lp)
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate 	char	*bp;
4280Sstevel@tonic-gate 	struct re_globals *_re = re_globals;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	bp = braslist[i];
4310Sstevel@tonic-gate 	while (*bp++ == *lp++)
4320Sstevel@tonic-gate 		if (bp >= braelist[i])
4330Sstevel@tonic-gate 			return (1);
4340Sstevel@tonic-gate 	return (0);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate static int
cclass(char * set,char c,int af)4380Sstevel@tonic-gate cclass(char *set, char c, int af)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	int	n;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (c == 0)
4430Sstevel@tonic-gate 		return (0);
4440Sstevel@tonic-gate 	n = *set++;
4450Sstevel@tonic-gate 	while (--n)
4460Sstevel@tonic-gate 		if (*set++ == c)
4470Sstevel@tonic-gate 			return (af);
4480Sstevel@tonic-gate 	return (! af);
4490Sstevel@tonic-gate }
450