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