xref: /titanic_51/usr/src/lib/libbc/libc/gen/common/regex.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1987 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * routines to do regular expression matching
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Entry points:
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  *	re_comp(s)
357c478bd9Sstevel@tonic-gate  *		char *s;
367c478bd9Sstevel@tonic-gate  *	 ... returns 0 if the string s was compiled successfully,
377c478bd9Sstevel@tonic-gate  *		     a pointer to an error message otherwise.
387c478bd9Sstevel@tonic-gate  *	     If passed 0 or a null string returns without changing
397c478bd9Sstevel@tonic-gate  *           the currently compiled re (see note 11 below).
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *	re_exec(s)
427c478bd9Sstevel@tonic-gate  *		char *s;
437c478bd9Sstevel@tonic-gate  *	 ... returns 1 if the string s matches the last compiled regular
447c478bd9Sstevel@tonic-gate  *		       expression,
457c478bd9Sstevel@tonic-gate  *		     0 if the string s failed to match the last compiled
467c478bd9Sstevel@tonic-gate  *		       regular expression, and
477c478bd9Sstevel@tonic-gate  *		    -1 if the compiled regular expression was invalid
487c478bd9Sstevel@tonic-gate  *		       (indicating an internal error).
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * The strings passed to both re_comp and re_exec may have trailing or
517c478bd9Sstevel@tonic-gate  * embedded newline characters; they are terminated by nulls.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * The identity of the author of these routines is lost in antiquity;
547c478bd9Sstevel@tonic-gate  * this is essentially the same as the re code in the original V6 ed.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * The regular expressions recognized are described below. This description
577c478bd9Sstevel@tonic-gate  * is essentially the same as that for ed.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  *	A regular expression specifies a set of strings of characters.
607c478bd9Sstevel@tonic-gate  *	A member of this set of strings is said to be matched by
617c478bd9Sstevel@tonic-gate  *	the regular expression.  In the following specification for
627c478bd9Sstevel@tonic-gate  *	regular expressions the word `character' means any character but NUL.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *	1.  Any character except a special character matches itself.
657c478bd9Sstevel@tonic-gate  *	    Special characters are the regular expression delimiter plus
667c478bd9Sstevel@tonic-gate  *	    \ [ . and sometimes ^ * $.
677c478bd9Sstevel@tonic-gate  *	2.  A . matches any character.
687c478bd9Sstevel@tonic-gate  *	3.  A \ followed by any character except a digit or ( )
697c478bd9Sstevel@tonic-gate  *	    matches that character.
707c478bd9Sstevel@tonic-gate  *	4.  A nonempty string s bracketed [s] (or [^s]) matches any
717c478bd9Sstevel@tonic-gate  *	    character in (or not in) s. In s, \ has no special meaning,
727c478bd9Sstevel@tonic-gate  *	    and ] may only appear as the first letter. A substring
737c478bd9Sstevel@tonic-gate  *	    a-b, with a and b in ascending ASCII order, stands for
747c478bd9Sstevel@tonic-gate  *	    the inclusive range of ASCII characters.
757c478bd9Sstevel@tonic-gate  *	5.  A regular expression of form 1-4 followed by * matches a
767c478bd9Sstevel@tonic-gate  *	    sequence of 0 or more matches of the regular expression.
777c478bd9Sstevel@tonic-gate  *	6.  A regular expression, x, of form 1-8, bracketed \(x\)
787c478bd9Sstevel@tonic-gate  *	    matches what x matches.
797c478bd9Sstevel@tonic-gate  *	7.  A \ followed by a digit n matches a copy of the string that the
807c478bd9Sstevel@tonic-gate  *	    bracketed regular expression beginning with the nth \( matched.
817c478bd9Sstevel@tonic-gate  *	8.  A regular expression of form 1-8, x, followed by a regular
827c478bd9Sstevel@tonic-gate  *	    expression of form 1-7, y matches a match for x followed by
837c478bd9Sstevel@tonic-gate  *	    a match for y, with the x match being as long as possible
847c478bd9Sstevel@tonic-gate  *	    while still permitting a y match.
857c478bd9Sstevel@tonic-gate  *	9.  A regular expression of form 1-8 preceded by ^ (or followed
867c478bd9Sstevel@tonic-gate  *	    by $), is constrained to matches that begin at the left
877c478bd9Sstevel@tonic-gate  *	    (or end at the right) end of a line.
887c478bd9Sstevel@tonic-gate  *	10. A regular expression of form 1-9 picks out the longest among
897c478bd9Sstevel@tonic-gate  *	    the leftmost matches in a line.
907c478bd9Sstevel@tonic-gate  *	11. An empty regular expression stands for a copy of the last
917c478bd9Sstevel@tonic-gate  *	    regular expression encountered.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * constants for re's
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate #define	CBRA	1
987c478bd9Sstevel@tonic-gate #define	CCHR	2
997c478bd9Sstevel@tonic-gate #define	CDOT	4
1007c478bd9Sstevel@tonic-gate #define	CCL	6
1017c478bd9Sstevel@tonic-gate #define	NCCL	8
1027c478bd9Sstevel@tonic-gate #define	CDOL	10
1037c478bd9Sstevel@tonic-gate #define	CEOF	11
1047c478bd9Sstevel@tonic-gate #define	CKET	12
1057c478bd9Sstevel@tonic-gate #define	CBACK	18
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	CSTAR	01
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #define	ESIZE	512
1107c478bd9Sstevel@tonic-gate #define	NBRA	9
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static struct re_globals {
1137c478bd9Sstevel@tonic-gate 	char	_expbuf[ESIZE];
1147c478bd9Sstevel@tonic-gate 	char	*_braslist[NBRA], *_braelist[NBRA];
1157c478bd9Sstevel@tonic-gate 	char	_circf;
1167c478bd9Sstevel@tonic-gate } *re_globals;
1177c478bd9Sstevel@tonic-gate #define	expbuf (_re->_expbuf)
1187c478bd9Sstevel@tonic-gate #define	braslist (_re->_braslist)
1197c478bd9Sstevel@tonic-gate #define	braelist (_re->_braelist)
1207c478bd9Sstevel@tonic-gate #define	circf (_re->_circf)
1217c478bd9Sstevel@tonic-gate 
122*5d54f3d8Smuffin static int	advance(char *, char *);
123*5d54f3d8Smuffin static int	backref(int, char *);
124*5d54f3d8Smuffin static int	cclass(char *, char, int);
125*5d54f3d8Smuffin 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * compile the regular expression argument into a dfa
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate char *
130*5d54f3d8Smuffin re_comp(char *sp)
1317c478bd9Sstevel@tonic-gate {
132*5d54f3d8Smuffin 	int	c;
133*5d54f3d8Smuffin 	struct re_globals *_re = re_globals;
134*5d54f3d8Smuffin 	char	*ep;
1357c478bd9Sstevel@tonic-gate 	int	cclcnt, numbra = 0;
1367c478bd9Sstevel@tonic-gate 	char	*lastep = 0;
1377c478bd9Sstevel@tonic-gate 	char	bracket[NBRA];
1387c478bd9Sstevel@tonic-gate 	char	*bracketp = &bracket[0];
1397c478bd9Sstevel@tonic-gate 	char	*retoolong = "Regular expression too long";
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (_re == 0) {
1427c478bd9Sstevel@tonic-gate 		_re = (struct re_globals *)calloc(1, sizeof (*_re));
1437c478bd9Sstevel@tonic-gate 		if (_re == 0)
1447c478bd9Sstevel@tonic-gate 			return ("Out of memory");
1457c478bd9Sstevel@tonic-gate 		re_globals = _re;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	ep = expbuf;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #define	comerr(msg) {expbuf[0] = 0; numbra = 0; return(msg); }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (sp == 0 || *sp == '\0') {
1527c478bd9Sstevel@tonic-gate 		if (*ep == 0)
1537c478bd9Sstevel@tonic-gate 			return("No previous regular expression");
1547c478bd9Sstevel@tonic-gate 		return (0);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	if (*sp == '^') {
1577c478bd9Sstevel@tonic-gate 		circf = 1;
1587c478bd9Sstevel@tonic-gate 		sp++;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	else
1617c478bd9Sstevel@tonic-gate 		circf = 0;
1627c478bd9Sstevel@tonic-gate 	for (;;) {
1637c478bd9Sstevel@tonic-gate 		if (ep >= &expbuf[ESIZE])
1647c478bd9Sstevel@tonic-gate 			comerr(retoolong);
1657c478bd9Sstevel@tonic-gate 		if ((c = *sp++) == '\0') {
1667c478bd9Sstevel@tonic-gate 			if (bracketp != bracket)
1677c478bd9Sstevel@tonic-gate 				comerr("unmatched \\(");
1687c478bd9Sstevel@tonic-gate 			*ep++ = CEOF;
1697c478bd9Sstevel@tonic-gate 			*ep++ = 0;
1707c478bd9Sstevel@tonic-gate 			return (0);
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 		if (c != '*')
1737c478bd9Sstevel@tonic-gate 			lastep = ep;
1747c478bd9Sstevel@tonic-gate 		switch (c) {
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		case '.':
1777c478bd9Sstevel@tonic-gate 			*ep++ = CDOT;
1787c478bd9Sstevel@tonic-gate 			continue;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		case '*':
1817c478bd9Sstevel@tonic-gate 			if (lastep == 0 || *lastep == CBRA || *lastep == CKET)
1827c478bd9Sstevel@tonic-gate 				goto defchar;
1837c478bd9Sstevel@tonic-gate 			*lastep |= CSTAR;
1847c478bd9Sstevel@tonic-gate 			continue;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		case '$':
1877c478bd9Sstevel@tonic-gate 			if (*sp != '\0')
1887c478bd9Sstevel@tonic-gate 				goto defchar;
1897c478bd9Sstevel@tonic-gate 			*ep++ = CDOL;
1907c478bd9Sstevel@tonic-gate 			continue;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		case '[':
1937c478bd9Sstevel@tonic-gate 			*ep++ = CCL;
1947c478bd9Sstevel@tonic-gate 			*ep++ = 0;
1957c478bd9Sstevel@tonic-gate 			cclcnt = 1;
1967c478bd9Sstevel@tonic-gate 			if ((c = *sp++) == '^') {
1977c478bd9Sstevel@tonic-gate 				c = *sp++;
1987c478bd9Sstevel@tonic-gate 				ep[-2] = NCCL;
1997c478bd9Sstevel@tonic-gate 			}
2007c478bd9Sstevel@tonic-gate 			do {
2017c478bd9Sstevel@tonic-gate 				if (c == '\0')
2027c478bd9Sstevel@tonic-gate 					comerr("missing ]");
2037c478bd9Sstevel@tonic-gate 				if (c == '-' && ep [-1] != 0) {
2047c478bd9Sstevel@tonic-gate 					if ((c = *sp++) == ']') {
2057c478bd9Sstevel@tonic-gate 						*ep++ = '-';
2067c478bd9Sstevel@tonic-gate 						cclcnt++;
2077c478bd9Sstevel@tonic-gate 						break;
2087c478bd9Sstevel@tonic-gate 					}
2097c478bd9Sstevel@tonic-gate 					while (ep[-1] < c) {
2107c478bd9Sstevel@tonic-gate 						*ep = ep[-1] + 1;
2117c478bd9Sstevel@tonic-gate 						ep++;
2127c478bd9Sstevel@tonic-gate 						cclcnt++;
2137c478bd9Sstevel@tonic-gate 						if (ep >= &expbuf[ESIZE])
2147c478bd9Sstevel@tonic-gate 							comerr(retoolong);
2157c478bd9Sstevel@tonic-gate 					}
2167c478bd9Sstevel@tonic-gate 				}
2177c478bd9Sstevel@tonic-gate 				*ep++ = c;
2187c478bd9Sstevel@tonic-gate 				cclcnt++;
2197c478bd9Sstevel@tonic-gate 				if (ep >= &expbuf[ESIZE])
2207c478bd9Sstevel@tonic-gate 					comerr(retoolong);
2217c478bd9Sstevel@tonic-gate 			} while ((c = *sp++) != ']');
2227c478bd9Sstevel@tonic-gate 			lastep[1] = cclcnt;
2237c478bd9Sstevel@tonic-gate 			continue;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		case '\\':
2267c478bd9Sstevel@tonic-gate 			if ((c = *sp++) == '(') {
2277c478bd9Sstevel@tonic-gate 				if (numbra >= NBRA)
2287c478bd9Sstevel@tonic-gate 					comerr("too many \\(\\) pairs");
2297c478bd9Sstevel@tonic-gate 				*bracketp++ = numbra;
2307c478bd9Sstevel@tonic-gate 				*ep++ = CBRA;
2317c478bd9Sstevel@tonic-gate 				*ep++ = numbra++;
2327c478bd9Sstevel@tonic-gate 				continue;
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 			if (c == ')') {
2357c478bd9Sstevel@tonic-gate 				if (bracketp <= bracket)
2367c478bd9Sstevel@tonic-gate 					comerr("unmatched \\)");
2377c478bd9Sstevel@tonic-gate 				*ep++ = CKET;
2387c478bd9Sstevel@tonic-gate 				*ep++ = *--bracketp;
2397c478bd9Sstevel@tonic-gate 				continue;
2407c478bd9Sstevel@tonic-gate 			}
2417c478bd9Sstevel@tonic-gate 			if (c >= '1' && c < ('1' + NBRA)) {
2427c478bd9Sstevel@tonic-gate 				*ep++ = CBACK;
2437c478bd9Sstevel@tonic-gate 				*ep++ = c - '1';
2447c478bd9Sstevel@tonic-gate 				continue;
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 			*ep++ = CCHR;
2477c478bd9Sstevel@tonic-gate 			*ep++ = c;
2487c478bd9Sstevel@tonic-gate 			continue;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		defchar:
2517c478bd9Sstevel@tonic-gate 		default:
2527c478bd9Sstevel@tonic-gate 			*ep++ = CCHR;
2537c478bd9Sstevel@tonic-gate 			*ep++ = c;
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * match the argument string against the compiled re
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate int
262*5d54f3d8Smuffin re_exec(char *p1)
2637c478bd9Sstevel@tonic-gate {
264*5d54f3d8Smuffin 	struct re_globals *_re = re_globals;
265*5d54f3d8Smuffin 	char	*p2;
266*5d54f3d8Smuffin 	int	c;
2677c478bd9Sstevel@tonic-gate 	int	rv;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if (_re == 0)
2707c478bd9Sstevel@tonic-gate 		return (0);
2717c478bd9Sstevel@tonic-gate 	p2 = expbuf;
2727c478bd9Sstevel@tonic-gate 	for (c = 0; c < NBRA; c++) {
2737c478bd9Sstevel@tonic-gate 		braslist[c] = 0;
2747c478bd9Sstevel@tonic-gate 		braelist[c] = 0;
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	if (circf)
2777c478bd9Sstevel@tonic-gate 		return((advance(p1, p2)));
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * fast check for first character
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	if (*p2 == CCHR) {
2827c478bd9Sstevel@tonic-gate 		c = p2[1];
2837c478bd9Sstevel@tonic-gate 		do {
2847c478bd9Sstevel@tonic-gate 			if (*p1 != c)
2857c478bd9Sstevel@tonic-gate 				continue;
2867c478bd9Sstevel@tonic-gate 			if (rv = advance(p1, p2))
2877c478bd9Sstevel@tonic-gate 				return(rv);
2887c478bd9Sstevel@tonic-gate 		} while (*p1++);
2897c478bd9Sstevel@tonic-gate 		return(0);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	/*
2927c478bd9Sstevel@tonic-gate 	 * regular algorithm
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	do
2957c478bd9Sstevel@tonic-gate 		if (rv = advance(p1, p2))
2967c478bd9Sstevel@tonic-gate 			return(rv);
2977c478bd9Sstevel@tonic-gate 	while (*p1++);
2987c478bd9Sstevel@tonic-gate 	return(0);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate  * try to match the next thing in the dfa
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate static	int
305*5d54f3d8Smuffin advance(char *lp, char *ep)
3067c478bd9Sstevel@tonic-gate {
307*5d54f3d8Smuffin 	char	*curlp;
3087c478bd9Sstevel@tonic-gate 	int	ct, i;
3097c478bd9Sstevel@tonic-gate 	int	rv;
310*5d54f3d8Smuffin 	struct re_globals *_re = re_globals;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	for (;;)
3137c478bd9Sstevel@tonic-gate 		switch (*ep++) {
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		case CCHR:
3167c478bd9Sstevel@tonic-gate 			if (*ep++ == *lp++)
3177c478bd9Sstevel@tonic-gate 				continue;
3187c478bd9Sstevel@tonic-gate 			return(0);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		case CDOT:
3217c478bd9Sstevel@tonic-gate 			if (*lp++)
3227c478bd9Sstevel@tonic-gate 				continue;
3237c478bd9Sstevel@tonic-gate 			return(0);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		case CDOL:
3267c478bd9Sstevel@tonic-gate 			if (*lp == '\0')
3277c478bd9Sstevel@tonic-gate 				continue;
3287c478bd9Sstevel@tonic-gate 			return(0);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		case CEOF:
3317c478bd9Sstevel@tonic-gate 			return(1);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		case CCL:
3347c478bd9Sstevel@tonic-gate 			if (cclass(ep, *lp++, 1)) {
3357c478bd9Sstevel@tonic-gate 				ep += *ep;
3367c478bd9Sstevel@tonic-gate 				continue;
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 			return(0);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		case NCCL:
3417c478bd9Sstevel@tonic-gate 			if (cclass(ep, *lp++, 0)) {
3427c478bd9Sstevel@tonic-gate 				ep += *ep;
3437c478bd9Sstevel@tonic-gate 				continue;
3447c478bd9Sstevel@tonic-gate 			}
3457c478bd9Sstevel@tonic-gate 			return(0);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		case CBRA:
3487c478bd9Sstevel@tonic-gate 			braslist[*ep++] = lp;
3497c478bd9Sstevel@tonic-gate 			continue;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		case CKET:
3527c478bd9Sstevel@tonic-gate 			braelist[*ep++] = lp;
3537c478bd9Sstevel@tonic-gate 			continue;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		case CBACK:
3567c478bd9Sstevel@tonic-gate 			if (braelist[i = *ep++] == 0)
3577c478bd9Sstevel@tonic-gate 				return(-1);
3587c478bd9Sstevel@tonic-gate 			if (backref(i, lp)) {
3597c478bd9Sstevel@tonic-gate 				lp += braelist[i] - braslist[i];
3607c478bd9Sstevel@tonic-gate 				continue;
3617c478bd9Sstevel@tonic-gate 			}
3627c478bd9Sstevel@tonic-gate 			return(0);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		case CBACK|CSTAR:
3657c478bd9Sstevel@tonic-gate 			if (braelist[i = *ep++] == 0)
3667c478bd9Sstevel@tonic-gate 				return(-1);
3677c478bd9Sstevel@tonic-gate 			curlp = lp;
3687c478bd9Sstevel@tonic-gate 			ct = braelist[i] - braslist[i];
3697c478bd9Sstevel@tonic-gate 			while (backref(i, lp))
3707c478bd9Sstevel@tonic-gate 				lp += ct;
3717c478bd9Sstevel@tonic-gate 			while (lp >= curlp) {
3727c478bd9Sstevel@tonic-gate 				if (rv = advance(lp, ep))
3737c478bd9Sstevel@tonic-gate 					return(rv);
3747c478bd9Sstevel@tonic-gate 				lp -= ct;
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 			continue;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		case CDOT|CSTAR:
3797c478bd9Sstevel@tonic-gate 			curlp = lp;
3807c478bd9Sstevel@tonic-gate 			while (*lp++)
3817c478bd9Sstevel@tonic-gate 				;
3827c478bd9Sstevel@tonic-gate 			goto star;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		case CCHR|CSTAR:
3857c478bd9Sstevel@tonic-gate 			curlp = lp;
3867c478bd9Sstevel@tonic-gate 			while (*lp++ == *ep)
3877c478bd9Sstevel@tonic-gate 				;
3887c478bd9Sstevel@tonic-gate 			ep++;
3897c478bd9Sstevel@tonic-gate 			goto star;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		case CCL|CSTAR:
3927c478bd9Sstevel@tonic-gate 		case NCCL|CSTAR:
3937c478bd9Sstevel@tonic-gate 			curlp = lp;
3947c478bd9Sstevel@tonic-gate 			while (cclass(ep, *lp++, ep[-1] == (CCL|CSTAR)))
3957c478bd9Sstevel@tonic-gate 				;
3967c478bd9Sstevel@tonic-gate 			ep += *ep;
3977c478bd9Sstevel@tonic-gate 			goto star;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		star:
4007c478bd9Sstevel@tonic-gate 			do {
4017c478bd9Sstevel@tonic-gate 				lp--;
4027c478bd9Sstevel@tonic-gate 				if (rv = advance(lp, ep))
4037c478bd9Sstevel@tonic-gate 					return(rv);
4047c478bd9Sstevel@tonic-gate 			} while (lp > curlp);
4057c478bd9Sstevel@tonic-gate 			return(0);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		default:
4087c478bd9Sstevel@tonic-gate 			return(-1);
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
412*5d54f3d8Smuffin static int
413*5d54f3d8Smuffin backref(int i, char *lp)
4147c478bd9Sstevel@tonic-gate {
415*5d54f3d8Smuffin 	char	*bp;
416*5d54f3d8Smuffin 	struct re_globals *_re = re_globals;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	bp = braslist[i];
4197c478bd9Sstevel@tonic-gate 	while (*bp++ == *lp++)
4207c478bd9Sstevel@tonic-gate 		if (bp >= braelist[i])
4217c478bd9Sstevel@tonic-gate 			return (1);
4227c478bd9Sstevel@tonic-gate 	return (0);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate static int
426*5d54f3d8Smuffin cclass(char *set, char c, int af)
4277c478bd9Sstevel@tonic-gate {
428*5d54f3d8Smuffin 	int	n;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if (c == 0)
4317c478bd9Sstevel@tonic-gate 		return(0);
4327c478bd9Sstevel@tonic-gate 	n = *set++;
4337c478bd9Sstevel@tonic-gate 	while (--n)
4347c478bd9Sstevel@tonic-gate 		if (*set++ == c)
4357c478bd9Sstevel@tonic-gate 			return (af);
4367c478bd9Sstevel@tonic-gate 	return (!af);
4377c478bd9Sstevel@tonic-gate }
438