xref: /onnv-gate/usr/src/ucblib/libucb/port/gen/regex.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate /* 	Portions Copyright(c) 1988, Sun Microsystems Inc.	*/
32*0Sstevel@tonic-gate /*	All Rights Reserved					*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate /*LINTLIBRARY*/
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * routines to do regular expression matching
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * Entry points:
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  *	re_comp(s)
44*0Sstevel@tonic-gate  *		char *s;
45*0Sstevel@tonic-gate  *	 ... returns 0 if the string s was compiled successfully,
46*0Sstevel@tonic-gate  *		     a pointer to an error message otherwise.
47*0Sstevel@tonic-gate  *	     If passed 0 or a null string returns without changing
48*0Sstevel@tonic-gate  *           the currently compiled re (see note 11 below).
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  *	re_exec(s)
51*0Sstevel@tonic-gate  *		char *s;
52*0Sstevel@tonic-gate  *	 ... returns 1 if the string s matches the last compiled regular
53*0Sstevel@tonic-gate  *		       expression,
54*0Sstevel@tonic-gate  *		     0 if the string s failed to match the last compiled
55*0Sstevel@tonic-gate  *		       regular expression, and
56*0Sstevel@tonic-gate  *		    -1 if the compiled regular expression was invalid
57*0Sstevel@tonic-gate  *		       (indicating an internal error).
58*0Sstevel@tonic-gate  *
59*0Sstevel@tonic-gate  * The strings passed to both re_comp and re_exec may have trailing or
60*0Sstevel@tonic-gate  * embedded newline characters; they are terminated by nulls.
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  * The identity of the author of these routines is lost in antiquity;
63*0Sstevel@tonic-gate  * this is essentially the same as the re code in the original V6 ed.
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  * The regular expressions recognized are described below. This description
66*0Sstevel@tonic-gate  * is essentially the same as that for ed.
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  *	A regular expression specifies a set of strings of characters.
69*0Sstevel@tonic-gate  *	A member of this set of strings is said to be matched by
70*0Sstevel@tonic-gate  *	the regular expression.  In the following specification for
71*0Sstevel@tonic-gate  *	regular expressions the word `character' means any character but NUL.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  *	1.  Any character except a special character matches itself.
74*0Sstevel@tonic-gate  *	    Special characters are the regular expression delimiter plus
75*0Sstevel@tonic-gate  *	    \ [ . and sometimes ^ * $.
76*0Sstevel@tonic-gate  *	2.  A . matches any character.
77*0Sstevel@tonic-gate  *	3.  A \ followed by any character except a digit or ( )
78*0Sstevel@tonic-gate  *	    matches that character.
79*0Sstevel@tonic-gate  *	4.  A nonempty string s bracketed [s] (or [^s]) matches any
80*0Sstevel@tonic-gate  *	    character in (or not in) s. In s, \ has no special meaning,
81*0Sstevel@tonic-gate  *	    and ] may only appear as the first letter. A substring
82*0Sstevel@tonic-gate  *	    a-b, with a and b in ascending ASCII order, stands for
83*0Sstevel@tonic-gate  *	    the inclusive range of ASCII characters.
84*0Sstevel@tonic-gate  *	5.  A regular expression of form 1-4 followed by * matches a
85*0Sstevel@tonic-gate  *	    sequence of 0 or more matches of the regular expression.
86*0Sstevel@tonic-gate  *	6.  A regular expression, x, of form 1-8, bracketed \(x\)
87*0Sstevel@tonic-gate  *	    matches what x matches.
88*0Sstevel@tonic-gate  *	7.  A \ followed by a digit n matches a copy of the string that the
89*0Sstevel@tonic-gate  *	    bracketed regular expression beginning with the nth \( matched.
90*0Sstevel@tonic-gate  *	8.  A regular expression of form 1-8, x, followed by a regular
91*0Sstevel@tonic-gate  *	    expression of form 1-7, y matches a match for x followed by
92*0Sstevel@tonic-gate  *	    a match for y, with the x match being as long as possible
93*0Sstevel@tonic-gate  *	    while still permitting a y match.
94*0Sstevel@tonic-gate  *	9.  A regular expression of form 1-8 preceded by ^ (or followed
95*0Sstevel@tonic-gate  *	    by $), is constrained to matches that begin at the left
96*0Sstevel@tonic-gate  *	    (or end at the right) end of a line.
97*0Sstevel@tonic-gate  *	10. A regular expression of form 1-9 picks out the longest among
98*0Sstevel@tonic-gate  *	    the leftmost matches in a line.
99*0Sstevel@tonic-gate  *	11. An empty regular expression stands for a copy of the last
100*0Sstevel@tonic-gate  *	    regular expression encountered.
101*0Sstevel@tonic-gate  */
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate #include <sys/types.h>
104*0Sstevel@tonic-gate #include <stdlib.h>
105*0Sstevel@tonic-gate #include <stddef.h>
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * constants for re's
109*0Sstevel@tonic-gate  */
110*0Sstevel@tonic-gate #define	CBRA	1
111*0Sstevel@tonic-gate #define	CCHR	2
112*0Sstevel@tonic-gate #define	CDOT	4
113*0Sstevel@tonic-gate #define	CCL	6
114*0Sstevel@tonic-gate #define	NCCL	8
115*0Sstevel@tonic-gate #define	CDOL	10
116*0Sstevel@tonic-gate #define	CEOF	11
117*0Sstevel@tonic-gate #define	CKET	12
118*0Sstevel@tonic-gate #define	CBACK	18
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate #define	CSTAR	01
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate #define	ESIZE	512
123*0Sstevel@tonic-gate #define	NBRA	9
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate static struct re_globals {
126*0Sstevel@tonic-gate 	char	_expbuf[ESIZE];
127*0Sstevel@tonic-gate 	char	*_braslist[NBRA], *_braelist[NBRA];
128*0Sstevel@tonic-gate 	char	_circf;
129*0Sstevel@tonic-gate } *re_globals;
130*0Sstevel@tonic-gate #define	expbuf (_re->_expbuf)
131*0Sstevel@tonic-gate #define	braslist (_re->_braslist)
132*0Sstevel@tonic-gate #define	braelist (_re->_braelist)
133*0Sstevel@tonic-gate #define	circf (_re->_circf)
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate /*
136*0Sstevel@tonic-gate  * forward declarations
137*0Sstevel@tonic-gate  */
138*0Sstevel@tonic-gate static int backref(int, char *);
139*0Sstevel@tonic-gate static int advance(char *, char *);
140*0Sstevel@tonic-gate static int cclass(char *, char, int);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate /*
143*0Sstevel@tonic-gate  * compile the regular expression argument into a dfa
144*0Sstevel@tonic-gate  */
145*0Sstevel@tonic-gate char *
re_comp(char * sp)146*0Sstevel@tonic-gate re_comp(char *sp)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	char	c;
149*0Sstevel@tonic-gate 	struct re_globals *_re = re_globals;
150*0Sstevel@tonic-gate 	char	*ep;
151*0Sstevel@tonic-gate 	int	cclcnt, numbra = 0;
152*0Sstevel@tonic-gate 	char	*lastep = 0;
153*0Sstevel@tonic-gate 	char	bracket[NBRA];
154*0Sstevel@tonic-gate 	char	*bracketp = &bracket[0];
155*0Sstevel@tonic-gate 	char	*retoolong = "Regular expression too long";
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if (_re == 0) {
158*0Sstevel@tonic-gate 		_re = (struct re_globals *)calloc(1, sizeof (*_re));
159*0Sstevel@tonic-gate 		if (_re == 0)
160*0Sstevel@tonic-gate 			return ("Out of memory");
161*0Sstevel@tonic-gate 		re_globals = _re;
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 	ep = expbuf;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate #define	comerr(msg) {expbuf[0] = 0; return (msg); }
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	if (sp == 0 || *sp == '\0') {
168*0Sstevel@tonic-gate 		if (*ep == 0)
169*0Sstevel@tonic-gate 			return ("No previous regular expression");
170*0Sstevel@tonic-gate 		return (0);
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 	if (*sp == '^') {
173*0Sstevel@tonic-gate 		circf = 1;
174*0Sstevel@tonic-gate 		sp++;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 	else
177*0Sstevel@tonic-gate 		circf = 0;
178*0Sstevel@tonic-gate 	for (;;) {
179*0Sstevel@tonic-gate 		if (ep >= &expbuf[ESIZE])
180*0Sstevel@tonic-gate 			comerr(retoolong);
181*0Sstevel@tonic-gate 		if ((c = *sp++) == '\0') {
182*0Sstevel@tonic-gate 			if (bracketp != bracket)
183*0Sstevel@tonic-gate 				comerr("unmatched \\(");
184*0Sstevel@tonic-gate 			*ep++ = CEOF;
185*0Sstevel@tonic-gate 			*ep++ = 0;
186*0Sstevel@tonic-gate 			return (0);
187*0Sstevel@tonic-gate 		}
188*0Sstevel@tonic-gate 		if (c != '*')
189*0Sstevel@tonic-gate 			lastep = ep;
190*0Sstevel@tonic-gate 		switch (c) {
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		case '.':
193*0Sstevel@tonic-gate 			*ep++ = CDOT;
194*0Sstevel@tonic-gate 			continue;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 		case '*':
197*0Sstevel@tonic-gate 			if (lastep == 0 || *lastep == CBRA || *lastep == CKET)
198*0Sstevel@tonic-gate 				goto defchar;
199*0Sstevel@tonic-gate 			*lastep |= CSTAR;
200*0Sstevel@tonic-gate 			continue;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 		case '$':
203*0Sstevel@tonic-gate 			if (*sp != '\0')
204*0Sstevel@tonic-gate 				goto defchar;
205*0Sstevel@tonic-gate 			*ep++ = CDOL;
206*0Sstevel@tonic-gate 			continue;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 		case '[':
209*0Sstevel@tonic-gate 			*ep++ = CCL;
210*0Sstevel@tonic-gate 			*ep++ = 0;
211*0Sstevel@tonic-gate 			cclcnt = 1;
212*0Sstevel@tonic-gate 			if ((c = *sp++) == '^') {
213*0Sstevel@tonic-gate 				c = *sp++;
214*0Sstevel@tonic-gate 				ep[-2] = NCCL;
215*0Sstevel@tonic-gate 			}
216*0Sstevel@tonic-gate 			do {
217*0Sstevel@tonic-gate 				if (c == '\0')
218*0Sstevel@tonic-gate 					comerr("missing ]");
219*0Sstevel@tonic-gate 				if (c == '-' && ep [-1] != 0) {
220*0Sstevel@tonic-gate 					if ((c = *sp++) == ']') {
221*0Sstevel@tonic-gate 						*ep++ = '-';
222*0Sstevel@tonic-gate 						cclcnt++;
223*0Sstevel@tonic-gate 						break;
224*0Sstevel@tonic-gate 					}
225*0Sstevel@tonic-gate 					while (ep[-1] < c) {
226*0Sstevel@tonic-gate 						*ep = ep[-1] + 1;
227*0Sstevel@tonic-gate 						ep++;
228*0Sstevel@tonic-gate 						cclcnt++;
229*0Sstevel@tonic-gate 						if (ep >= &expbuf[ESIZE])
230*0Sstevel@tonic-gate 							comerr(retoolong);
231*0Sstevel@tonic-gate 					}
232*0Sstevel@tonic-gate 				}
233*0Sstevel@tonic-gate 				*ep++ = c;
234*0Sstevel@tonic-gate 				cclcnt++;
235*0Sstevel@tonic-gate 				if (ep >= &expbuf[ESIZE])
236*0Sstevel@tonic-gate 					comerr(retoolong);
237*0Sstevel@tonic-gate 			} while ((c = *sp++) != ']');
238*0Sstevel@tonic-gate 			lastep[1] = (char)cclcnt;
239*0Sstevel@tonic-gate 			continue;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 		case '\\':
242*0Sstevel@tonic-gate 			if ((c = *sp++) == '(') {
243*0Sstevel@tonic-gate 				if (numbra >= NBRA)
244*0Sstevel@tonic-gate 					comerr("too many \\(\\) pairs");
245*0Sstevel@tonic-gate 				*bracketp++ = (char)numbra;
246*0Sstevel@tonic-gate 				*ep++ = CBRA;
247*0Sstevel@tonic-gate 				*ep++ = numbra++;
248*0Sstevel@tonic-gate 				continue;
249*0Sstevel@tonic-gate 			}
250*0Sstevel@tonic-gate 			if (c == ')') {
251*0Sstevel@tonic-gate 				if (bracketp <= bracket)
252*0Sstevel@tonic-gate 					comerr("unmatched \\)");
253*0Sstevel@tonic-gate 				*ep++ = CKET;
254*0Sstevel@tonic-gate 				*ep++ = *--bracketp;
255*0Sstevel@tonic-gate 				continue;
256*0Sstevel@tonic-gate 			}
257*0Sstevel@tonic-gate 			if (c >= '1' && c < ('1' + NBRA)) {
258*0Sstevel@tonic-gate 				*ep++ = CBACK;
259*0Sstevel@tonic-gate 				*ep++ = c - '1';
260*0Sstevel@tonic-gate 				continue;
261*0Sstevel@tonic-gate 			}
262*0Sstevel@tonic-gate 			*ep++ = CCHR;
263*0Sstevel@tonic-gate 			*ep++ = c;
264*0Sstevel@tonic-gate 			continue;
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 		defchar:
267*0Sstevel@tonic-gate 		default:
268*0Sstevel@tonic-gate 			*ep++ = CCHR;
269*0Sstevel@tonic-gate 			*ep++ = c;
270*0Sstevel@tonic-gate 		}
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate /*
275*0Sstevel@tonic-gate  * match the argument string against the compiled re
276*0Sstevel@tonic-gate  */
277*0Sstevel@tonic-gate int
re_exec(char * p1)278*0Sstevel@tonic-gate re_exec(char *p1)
279*0Sstevel@tonic-gate {
280*0Sstevel@tonic-gate 	struct re_globals *_re = re_globals;
281*0Sstevel@tonic-gate 	char	*p2;
282*0Sstevel@tonic-gate 	int	c;
283*0Sstevel@tonic-gate 	int	rv;
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	if (_re == 0)
286*0Sstevel@tonic-gate 		return (0);
287*0Sstevel@tonic-gate 	p2 = expbuf;
288*0Sstevel@tonic-gate 	for (c = 0; c < NBRA; c++) {
289*0Sstevel@tonic-gate 		braslist[c] = 0;
290*0Sstevel@tonic-gate 		braelist[c] = 0;
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate 	if (circf)
293*0Sstevel@tonic-gate 		return ((advance(p1, p2)));
294*0Sstevel@tonic-gate 	/*
295*0Sstevel@tonic-gate 	 * fast check for first character
296*0Sstevel@tonic-gate 	 */
297*0Sstevel@tonic-gate 	if (*p2 == CCHR) {
298*0Sstevel@tonic-gate 		c = p2[1];
299*0Sstevel@tonic-gate 		do {
300*0Sstevel@tonic-gate 			if (*p1 != c)
301*0Sstevel@tonic-gate 				continue;
302*0Sstevel@tonic-gate 			if (rv = advance(p1, p2))
303*0Sstevel@tonic-gate 				return (rv);
304*0Sstevel@tonic-gate 		} while (*p1++);
305*0Sstevel@tonic-gate 		return (0);
306*0Sstevel@tonic-gate 	}
307*0Sstevel@tonic-gate 	/*
308*0Sstevel@tonic-gate 	 * regular algorithm
309*0Sstevel@tonic-gate 	 */
310*0Sstevel@tonic-gate 	do
311*0Sstevel@tonic-gate 		if (rv = advance(p1, p2))
312*0Sstevel@tonic-gate 			return (rv);
313*0Sstevel@tonic-gate 	while (*p1++);
314*0Sstevel@tonic-gate 	return (0);
315*0Sstevel@tonic-gate }
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate /*
318*0Sstevel@tonic-gate  * try to match the next thing in the dfa
319*0Sstevel@tonic-gate  */
320*0Sstevel@tonic-gate static int
advance(char * lp,char * ep)321*0Sstevel@tonic-gate advance(char *lp, char *ep)
322*0Sstevel@tonic-gate {
323*0Sstevel@tonic-gate 	char	*curlp;
324*0Sstevel@tonic-gate 	int	i;
325*0Sstevel@tonic-gate 	ptrdiff_t	ct;
326*0Sstevel@tonic-gate 	int	rv;
327*0Sstevel@tonic-gate 	struct re_globals *_re = re_globals;
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	for (;;)
330*0Sstevel@tonic-gate 		switch (*ep++) {
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		case CCHR:
333*0Sstevel@tonic-gate 			if (*ep++ == *lp++)
334*0Sstevel@tonic-gate 				continue;
335*0Sstevel@tonic-gate 			return (0);
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 		case CDOT:
338*0Sstevel@tonic-gate 			if (*lp++)
339*0Sstevel@tonic-gate 				continue;
340*0Sstevel@tonic-gate 			return (0);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 		case CDOL:
343*0Sstevel@tonic-gate 			if (*lp == '\0')
344*0Sstevel@tonic-gate 				continue;
345*0Sstevel@tonic-gate 			return (0);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 		case CEOF:
348*0Sstevel@tonic-gate 			return (1);
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 		case CCL:
351*0Sstevel@tonic-gate 			if (cclass(ep, *lp++, 1)) {
352*0Sstevel@tonic-gate 				ep += *ep;
353*0Sstevel@tonic-gate 				continue;
354*0Sstevel@tonic-gate 			}
355*0Sstevel@tonic-gate 			return (0);
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 		case NCCL:
358*0Sstevel@tonic-gate 			if (cclass(ep, *lp++, 0)) {
359*0Sstevel@tonic-gate 				ep += *ep;
360*0Sstevel@tonic-gate 				continue;
361*0Sstevel@tonic-gate 			}
362*0Sstevel@tonic-gate 			return (0);
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 		case CBRA:
365*0Sstevel@tonic-gate 			braslist[*ep++] = lp;
366*0Sstevel@tonic-gate 			continue;
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 		case CKET:
369*0Sstevel@tonic-gate 			braelist[*ep++] = lp;
370*0Sstevel@tonic-gate 			continue;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 		case CBACK:
373*0Sstevel@tonic-gate 			if (braelist[i = *ep++] == 0)
374*0Sstevel@tonic-gate 				return (-1);
375*0Sstevel@tonic-gate 			if (backref(i, lp)) {
376*0Sstevel@tonic-gate 				lp += braelist[i] - braslist[i];
377*0Sstevel@tonic-gate 				continue;
378*0Sstevel@tonic-gate 			}
379*0Sstevel@tonic-gate 			return (0);
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 		case CBACK|CSTAR:
382*0Sstevel@tonic-gate 			if (braelist[i = *ep++] == 0)
383*0Sstevel@tonic-gate 				return (-1);
384*0Sstevel@tonic-gate 			curlp = lp;
385*0Sstevel@tonic-gate 			ct = braelist[i] - braslist[i];
386*0Sstevel@tonic-gate 			while (backref(i, lp))
387*0Sstevel@tonic-gate 				lp += ct;
388*0Sstevel@tonic-gate 			while (lp >= curlp) {
389*0Sstevel@tonic-gate 				if (rv = advance(lp, ep))
390*0Sstevel@tonic-gate 					return (rv);
391*0Sstevel@tonic-gate 				lp -= ct;
392*0Sstevel@tonic-gate 			}
393*0Sstevel@tonic-gate 			continue;
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 		case CDOT|CSTAR:
396*0Sstevel@tonic-gate 			curlp = lp;
397*0Sstevel@tonic-gate 			while (*lp++)
398*0Sstevel@tonic-gate 				;
399*0Sstevel@tonic-gate 			goto star;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 		case CCHR|CSTAR:
402*0Sstevel@tonic-gate 			curlp = lp;
403*0Sstevel@tonic-gate 			while (*lp++ == *ep)
404*0Sstevel@tonic-gate 				;
405*0Sstevel@tonic-gate 			ep++;
406*0Sstevel@tonic-gate 			goto star;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 		case CCL|CSTAR:
409*0Sstevel@tonic-gate 		case NCCL|CSTAR:
410*0Sstevel@tonic-gate 			curlp = lp;
411*0Sstevel@tonic-gate 			while (cclass(ep, *lp++, ep[-1] == (CCL|CSTAR)))
412*0Sstevel@tonic-gate 				;
413*0Sstevel@tonic-gate 			ep += *ep;
414*0Sstevel@tonic-gate 			goto star;
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 		star:
417*0Sstevel@tonic-gate 			do {
418*0Sstevel@tonic-gate 				lp--;
419*0Sstevel@tonic-gate 				if (rv = advance(lp, ep))
420*0Sstevel@tonic-gate 					return (rv);
421*0Sstevel@tonic-gate 			} while (lp > curlp);
422*0Sstevel@tonic-gate 			return (0);
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 		default:
425*0Sstevel@tonic-gate 			return (-1);
426*0Sstevel@tonic-gate 		}
427*0Sstevel@tonic-gate }
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate static int
backref(int i,char * lp)430*0Sstevel@tonic-gate backref(int i, char *lp)
431*0Sstevel@tonic-gate {
432*0Sstevel@tonic-gate 	char	*bp;
433*0Sstevel@tonic-gate 	struct re_globals *_re = re_globals;
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	bp = braslist[i];
436*0Sstevel@tonic-gate 	while (*bp++ == *lp++)
437*0Sstevel@tonic-gate 		if (bp >= braelist[i])
438*0Sstevel@tonic-gate 			return (1);
439*0Sstevel@tonic-gate 	return (0);
440*0Sstevel@tonic-gate }
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate static int
cclass(char * set,char c,int af)443*0Sstevel@tonic-gate cclass(char *set, char c, int af)
444*0Sstevel@tonic-gate {
445*0Sstevel@tonic-gate 	int	n;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	if (c == 0)
448*0Sstevel@tonic-gate 		return (0);
449*0Sstevel@tonic-gate 	n = *set++;
450*0Sstevel@tonic-gate 	while (--n)
451*0Sstevel@tonic-gate 		if (*set++ == c)
452*0Sstevel@tonic-gate 			return (af);
453*0Sstevel@tonic-gate 	return (! af);
454*0Sstevel@tonic-gate }
455