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