engine.c revision 1.9
1/*	$OpenBSD: engine.c,v 1.9 2004/04/02 18:34:33 otto Exp $	*/
2
3/*-
4 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5 * Copyright (c) 1992, 1993, 1994
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Henry Spencer.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)engine.c	8.5 (Berkeley) 3/20/94
36 */
37
38#if defined(SNAMES) && defined(LIBC_SCCS) && !defined(lint)
39static char enginercsid[] = "$OpenBSD: engine.c,v 1.9 2004/04/02 18:34:33 otto Exp $";
40#endif /* SNAMES and LIBC_SCCS and not lint */
41
42/*
43 * The matching engine and friends.  This file is #included by regexec.c
44 * after suitable #defines of a variety of macros used herein, so that
45 * different state representations can be used without duplicating masses
46 * of code.
47 */
48
49#ifdef SNAMES
50#define	matcher	smatcher
51#define	fast	sfast
52#define	slow	sslow
53#define	dissect	sdissect
54#define	backref	sbackref
55#define	step	sstep
56#define	print	sprint
57#define	at	sat
58#define	match	smat
59#define	nope	snope
60#endif
61#ifdef LNAMES
62#define	matcher	lmatcher
63#define	fast	lfast
64#define	slow	lslow
65#define	dissect	ldissect
66#define	backref	lbackref
67#define	step	lstep
68#define	print	lprint
69#define	at	lat
70#define	match	lmat
71#define	nope	lnope
72#endif
73
74/* another structure passed up and down to avoid zillions of parameters */
75struct match {
76	struct re_guts *g;
77	int eflags;
78	regmatch_t *pmatch;	/* [nsub+1] (0 element unused) */
79	char *offp;		/* offsets work from here */
80	char *beginp;		/* start of string -- virtual NUL precedes */
81	char *endp;		/* end of string -- virtual NUL here */
82	char *coldp;		/* can be no match starting before here */
83	char **lastpos;		/* [nplus+1] */
84	STATEVARS;
85	states st;		/* current states */
86	states fresh;		/* states for a fresh start */
87	states tmp;		/* temporary */
88	states empty;		/* empty set of states */
89};
90
91/* ========= begin header generated by ./mkh ========= */
92#ifdef __cplusplus
93extern "C" {
94#endif
95
96/* === engine.c === */
97static int matcher(struct re_guts *g, char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
98static char *dissect(struct match *m, char *start, char *stop, sopno startst, sopno stopst);
99static char *backref(struct match *m, char *start, char *stop, sopno startst, sopno stopst, sopno lev);
100static char *fast(struct match *m, char *start, char *stop, sopno startst, sopno stopst);
101static char *slow(struct match *m, char *start, char *stop, sopno startst, sopno stopst);
102static states step(struct re_guts *g, sopno start, sopno stop, states bef, int ch, states aft);
103#define	BOL	(OUT+1)
104#define	EOL	(BOL+1)
105#define	BOLEOL	(BOL+2)
106#define	NOTHING	(BOL+3)
107#define	BOW	(BOL+4)
108#define	EOW	(BOL+5)
109#define	CODEMAX	(BOL+5)		/* highest code used */
110#define	NONCHAR(c)	((c) > CHAR_MAX)
111#define	NNONCHAR	(CODEMAX-CHAR_MAX)
112#ifdef REDEBUG
113static void print(struct match *m, char *caption, states st, int ch, FILE *d);
114#endif
115#ifdef REDEBUG
116static void at(struct match *m, char *title, char *start, char *stop, sopno startst, sopno stopst);
117#endif
118#ifdef REDEBUG
119static char *pchar(int ch);
120#endif
121
122#ifdef __cplusplus
123}
124#endif
125/* ========= end header generated by ./mkh ========= */
126
127#ifdef REDEBUG
128#define	SP(t, s, c)	print(m, t, s, c, stdout)
129#define	AT(t, p1, p2, s1, s2)	at(m, t, p1, p2, s1, s2)
130#define	NOTE(str)	{ if (m->eflags&REG_TRACE) (void)printf("=%s\n", (str)); }
131static int nope = 0;
132#else
133#define	SP(t, s, c)	/* nothing */
134#define	AT(t, p1, p2, s1, s2)	/* nothing */
135#define	NOTE(s)	/* nothing */
136#endif
137
138/*
139 - matcher - the actual matching engine
140 == static int matcher(register struct re_guts *g, char *string, \
141 ==	size_t nmatch, regmatch_t pmatch[], int eflags);
142 */
143static int			/* 0 success, REG_NOMATCH failure */
144matcher(g, string, nmatch, pmatch, eflags)
145register struct re_guts *g;
146char *string;
147size_t nmatch;
148regmatch_t pmatch[];
149int eflags;
150{
151	register char *endp;
152	register int i;
153	struct match mv;
154	register struct match *m = &mv;
155	register char *dp;
156	const register sopno gf = g->firststate+1;	/* +1 for OEND */
157	const register sopno gl = g->laststate;
158	char *start;
159	char *stop;
160
161	/* simplify the situation where possible */
162	if (g->cflags&REG_NOSUB)
163		nmatch = 0;
164	if (eflags&REG_STARTEND) {
165		start = string + pmatch[0].rm_so;
166		stop = string + pmatch[0].rm_eo;
167	} else {
168		start = string;
169		stop = start + strlen(start);
170	}
171	if (stop < start)
172		return(REG_INVARG);
173
174	/* prescreening; this does wonders for this rather slow code */
175	if (g->must != NULL) {
176		for (dp = start; dp < stop; dp++)
177			if (*dp == g->must[0] && stop - dp >= g->mlen &&
178				memcmp(dp, g->must, (size_t)g->mlen) == 0)
179				break;
180		if (dp == stop)		/* we didn't find g->must */
181			return(REG_NOMATCH);
182	}
183
184	/* match struct setup */
185	m->g = g;
186	m->eflags = eflags;
187	m->pmatch = NULL;
188	m->lastpos = NULL;
189	m->offp = string;
190	m->beginp = start;
191	m->endp = stop;
192	STATESETUP(m, 4);
193	SETUP(m->st);
194	SETUP(m->fresh);
195	SETUP(m->tmp);
196	SETUP(m->empty);
197	CLEAR(m->empty);
198
199	/* this loop does only one repetition except for backrefs */
200	for (;;) {
201		endp = fast(m, start, stop, gf, gl);
202		if (endp == NULL) {		/* a miss */
203			STATETEARDOWN(m);
204			return(REG_NOMATCH);
205		}
206		if (nmatch == 0 && !g->backrefs)
207			break;		/* no further info needed */
208
209		/* where? */
210		assert(m->coldp != NULL);
211		for (;;) {
212			NOTE("finding start");
213			endp = slow(m, m->coldp, stop, gf, gl);
214			if (endp != NULL)
215				break;
216			assert(m->coldp < m->endp);
217			m->coldp++;
218		}
219		if (nmatch == 1 && !g->backrefs)
220			break;		/* no further info needed */
221
222		/* oh my, he wants the subexpressions... */
223		if (m->pmatch == NULL)
224			m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
225							sizeof(regmatch_t));
226		if (m->pmatch == NULL) {
227			STATETEARDOWN(m);
228			return(REG_ESPACE);
229		}
230		for (i = 1; i <= m->g->nsub; i++)
231			m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
232		if (!g->backrefs && !(m->eflags&REG_BACKR)) {
233			NOTE("dissecting");
234			dp = dissect(m, m->coldp, endp, gf, gl);
235		} else {
236			if (g->nplus > 0 && m->lastpos == NULL)
237				m->lastpos = (char **)malloc((g->nplus+1) *
238							sizeof(char *));
239			if (g->nplus > 0 && m->lastpos == NULL) {
240				free(m->pmatch);
241				STATETEARDOWN(m);
242				return(REG_ESPACE);
243			}
244			NOTE("backref dissect");
245			dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
246		}
247		if (dp != NULL)
248			break;
249
250		/* uh-oh... we couldn't find a subexpression-level match */
251		assert(g->backrefs);	/* must be back references doing it */
252		assert(g->nplus == 0 || m->lastpos != NULL);
253		for (;;) {
254			if (dp != NULL || endp <= m->coldp)
255				break;		/* defeat */
256			NOTE("backoff");
257			endp = slow(m, m->coldp, endp-1, gf, gl);
258			if (endp == NULL)
259				break;		/* defeat */
260			/* try it on a shorter possibility */
261#ifndef NDEBUG
262			for (i = 1; i <= m->g->nsub; i++) {
263				assert(m->pmatch[i].rm_so == -1);
264				assert(m->pmatch[i].rm_eo == -1);
265			}
266#endif
267			NOTE("backoff dissect");
268			dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
269		}
270		assert(dp == NULL || dp == endp);
271		if (dp != NULL)		/* found a shorter one */
272			break;
273
274		/* despite initial appearances, there is no match here */
275		NOTE("false alarm");
276		start = m->coldp + 1;	/* recycle starting later */
277		assert(start <= stop);
278	}
279
280	/* fill in the details if requested */
281	if (nmatch > 0) {
282		pmatch[0].rm_so = m->coldp - m->offp;
283		pmatch[0].rm_eo = endp - m->offp;
284	}
285	if (nmatch > 1) {
286		assert(m->pmatch != NULL);
287		for (i = 1; i < nmatch; i++)
288			if (i <= m->g->nsub)
289				pmatch[i] = m->pmatch[i];
290			else {
291				pmatch[i].rm_so = -1;
292				pmatch[i].rm_eo = -1;
293			}
294	}
295
296	if (m->pmatch != NULL)
297		free((char *)m->pmatch);
298	if (m->lastpos != NULL)
299		free((char *)m->lastpos);
300	STATETEARDOWN(m);
301	return(0);
302}
303
304/*
305 - dissect - figure out what matched what, no back references
306 == static char *dissect(register struct match *m, char *start, \
307 ==	char *stop, sopno startst, sopno stopst);
308 */
309static char *			/* == stop (success) always */
310dissect(m, start, stop, startst, stopst)
311register struct match *m;
312char *start;
313char *stop;
314sopno startst;
315sopno stopst;
316{
317	register int i;
318	register sopno ss;	/* start sop of current subRE */
319	register sopno es;	/* end sop of current subRE */
320	register char *sp;	/* start of string matched by it */
321	register char *stp;	/* string matched by it cannot pass here */
322	register char *rest;	/* start of rest of string */
323	register char *tail;	/* string unmatched by rest of RE */
324	register sopno ssub;	/* start sop of subsubRE */
325	register sopno esub;	/* end sop of subsubRE */
326	register char *ssp;	/* start of string matched by subsubRE */
327	register char *sep;	/* end of string matched by subsubRE */
328	register char *oldssp;	/* previous ssp */
329	register char *dp;
330
331	AT("diss", start, stop, startst, stopst);
332	sp = start;
333	for (ss = startst; ss < stopst; ss = es) {
334		/* identify end of subRE */
335		es = ss;
336		switch (OP(m->g->strip[es])) {
337		case OPLUS_:
338		case OQUEST_:
339			es += OPND(m->g->strip[es]);
340			break;
341		case OCH_:
342			while (OP(m->g->strip[es]) != O_CH)
343				es += OPND(m->g->strip[es]);
344			break;
345		}
346		es++;
347
348		/* figure out what it matched */
349		switch (OP(m->g->strip[ss])) {
350		case OEND:
351			assert(nope);
352			break;
353		case OCHAR:
354			sp++;
355			break;
356		case OBOL:
357		case OEOL:
358		case OBOW:
359		case OEOW:
360			break;
361		case OANY:
362		case OANYOF:
363			sp++;
364			break;
365		case OBACK_:
366		case O_BACK:
367			assert(nope);
368			break;
369		/* cases where length of match is hard to find */
370		case OQUEST_:
371			stp = stop;
372			for (;;) {
373				/* how long could this one be? */
374				rest = slow(m, sp, stp, ss, es);
375				assert(rest != NULL);	/* it did match */
376				/* could the rest match the rest? */
377				tail = slow(m, rest, stop, es, stopst);
378				if (tail == stop)
379					break;		/* yes! */
380				/* no -- try a shorter match for this one */
381				stp = rest - 1;
382				assert(stp >= sp);	/* it did work */
383			}
384			ssub = ss + 1;
385			esub = es - 1;
386			/* did innards match? */
387			if (slow(m, sp, rest, ssub, esub) != NULL) {
388				dp = dissect(m, sp, rest, ssub, esub);
389				assert(dp == rest);
390			} else		/* no */
391				assert(sp == rest);
392			sp = rest;
393			break;
394		case OPLUS_:
395			stp = stop;
396			for (;;) {
397				/* how long could this one be? */
398				rest = slow(m, sp, stp, ss, es);
399				assert(rest != NULL);	/* it did match */
400				/* could the rest match the rest? */
401				tail = slow(m, rest, stop, es, stopst);
402				if (tail == stop)
403					break;		/* yes! */
404				/* no -- try a shorter match for this one */
405				stp = rest - 1;
406				assert(stp >= sp);	/* it did work */
407			}
408			ssub = ss + 1;
409			esub = es - 1;
410			ssp = sp;
411			oldssp = ssp;
412			for (;;) {	/* find last match of innards */
413				sep = slow(m, ssp, rest, ssub, esub);
414				if (sep == NULL || sep == ssp)
415					break;	/* failed or matched null */
416				oldssp = ssp;	/* on to next try */
417				ssp = sep;
418			}
419			if (sep == NULL) {
420				/* last successful match */
421				sep = ssp;
422				ssp = oldssp;
423			}
424			assert(sep == rest);	/* must exhaust substring */
425			assert(slow(m, ssp, sep, ssub, esub) == rest);
426			dp = dissect(m, ssp, sep, ssub, esub);
427			assert(dp == sep);
428			sp = rest;
429			break;
430		case OCH_:
431			stp = stop;
432			for (;;) {
433				/* how long could this one be? */
434				rest = slow(m, sp, stp, ss, es);
435				assert(rest != NULL);	/* it did match */
436				/* could the rest match the rest? */
437				tail = slow(m, rest, stop, es, stopst);
438				if (tail == stop)
439					break;		/* yes! */
440				/* no -- try a shorter match for this one */
441				stp = rest - 1;
442				assert(stp >= sp);	/* it did work */
443			}
444			ssub = ss + 1;
445			esub = ss + OPND(m->g->strip[ss]) - 1;
446			assert(OP(m->g->strip[esub]) == OOR1);
447			for (;;) {	/* find first matching branch */
448				if (slow(m, sp, rest, ssub, esub) == rest)
449					break;	/* it matched all of it */
450				/* that one missed, try next one */
451				assert(OP(m->g->strip[esub]) == OOR1);
452				esub++;
453				assert(OP(m->g->strip[esub]) == OOR2);
454				ssub = esub + 1;
455				esub += OPND(m->g->strip[esub]);
456				if (OP(m->g->strip[esub]) == OOR2)
457					esub--;
458				else
459					assert(OP(m->g->strip[esub]) == O_CH);
460			}
461			dp = dissect(m, sp, rest, ssub, esub);
462			assert(dp == rest);
463			sp = rest;
464			break;
465		case O_PLUS:
466		case O_QUEST:
467		case OOR1:
468		case OOR2:
469		case O_CH:
470			assert(nope);
471			break;
472		case OLPAREN:
473			i = OPND(m->g->strip[ss]);
474			assert(0 < i && i <= m->g->nsub);
475			m->pmatch[i].rm_so = sp - m->offp;
476			break;
477		case ORPAREN:
478			i = OPND(m->g->strip[ss]);
479			assert(0 < i && i <= m->g->nsub);
480			m->pmatch[i].rm_eo = sp - m->offp;
481			break;
482		default:		/* uh oh */
483			assert(nope);
484			break;
485		}
486	}
487
488	assert(sp == stop);
489	return(sp);
490}
491
492/*
493 - backref - figure out what matched what, figuring in back references
494 == static char *backref(register struct match *m, char *start, \
495 ==	char *stop, sopno startst, sopno stopst, sopno lev);
496 */
497static char *			/* == stop (success) or NULL (failure) */
498backref(m, start, stop, startst, stopst, lev)
499register struct match *m;
500char *start;
501char *stop;
502sopno startst;
503sopno stopst;
504sopno lev;			/* PLUS nesting level */
505{
506	register int i;
507	register sopno ss;	/* start sop of current subRE */
508	register char *sp;	/* start of string matched by it */
509	register sopno ssub;	/* start sop of subsubRE */
510	register sopno esub;	/* end sop of subsubRE */
511	register char *ssp;	/* start of string matched by subsubRE */
512	register char *dp;
513	register size_t len;
514	register int hard;
515	register sop s;
516	register regoff_t offsave;
517	register cset *cs;
518
519	AT("back", start, stop, startst, stopst);
520	sp = start;
521
522	/* get as far as we can with easy stuff */
523	hard = 0;
524	for (ss = startst; !hard && ss < stopst; ss++)
525		switch (OP(s = m->g->strip[ss])) {
526		case OCHAR:
527			if (sp == stop || *sp++ != (char)OPND(s))
528				return(NULL);
529			break;
530		case OANY:
531			if (sp == stop)
532				return(NULL);
533			sp++;
534			break;
535		case OANYOF:
536			cs = &m->g->sets[OPND(s)];
537			if (sp == stop || !CHIN(cs, *sp++))
538				return(NULL);
539			break;
540		case OBOL:
541			if ( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
542					(sp < m->endp && *(sp-1) == '\n' &&
543						(m->g->cflags&REG_NEWLINE)) )
544				{ /* yes */ }
545			else
546				return(NULL);
547			break;
548		case OEOL:
549			if ( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
550					(sp < m->endp && *sp == '\n' &&
551						(m->g->cflags&REG_NEWLINE)) )
552				{ /* yes */ }
553			else
554				return(NULL);
555			break;
556		case OBOW:
557			if (( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
558					(sp < m->endp && *(sp-1) == '\n' &&
559						(m->g->cflags&REG_NEWLINE)) ||
560					(sp > m->beginp &&
561							!ISWORD(*(sp-1))) ) &&
562					(sp < m->endp && ISWORD(*sp)) )
563				{ /* yes */ }
564			else
565				return(NULL);
566			break;
567		case OEOW:
568			if (( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
569					(sp < m->endp && *sp == '\n' &&
570						(m->g->cflags&REG_NEWLINE)) ||
571					(sp < m->endp && !ISWORD(*sp)) ) &&
572					(sp > m->beginp && ISWORD(*(sp-1))) )
573				{ /* yes */ }
574			else
575				return(NULL);
576			break;
577		case O_QUEST:
578			break;
579		case OOR1:	/* matches null but needs to skip */
580			ss++;
581			s = m->g->strip[ss];
582			do {
583				assert(OP(s) == OOR2);
584				ss += OPND(s);
585			} while (OP(s = m->g->strip[ss]) != O_CH);
586			/* note that the ss++ gets us past the O_CH */
587			break;
588		default:	/* have to make a choice */
589			hard = 1;
590			break;
591		}
592	if (!hard) {		/* that was it! */
593		if (sp != stop)
594			return(NULL);
595		return(sp);
596	}
597	ss--;			/* adjust for the for's final increment */
598
599	/* the hard stuff */
600	AT("hard", sp, stop, ss, stopst);
601	s = m->g->strip[ss];
602	switch (OP(s)) {
603	case OBACK_:		/* the vilest depths */
604		i = OPND(s);
605		assert(0 < i && i <= m->g->nsub);
606		if (m->pmatch[i].rm_eo == -1)
607			return(NULL);
608		assert(m->pmatch[i].rm_so != -1);
609		len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
610		if (len == 0)
611			return(NULL);
612		assert(stop - m->beginp >= len);
613		if (sp > stop - len)
614			return(NULL);	/* not enough left to match */
615		ssp = m->offp + m->pmatch[i].rm_so;
616		if (memcmp(sp, ssp, len) != 0)
617			return(NULL);
618		while (m->g->strip[ss] != SOP(O_BACK, i))
619			ss++;
620		return(backref(m, sp+len, stop, ss+1, stopst, lev));
621		break;
622	case OQUEST_:		/* to null or not */
623		dp = backref(m, sp, stop, ss+1, stopst, lev);
624		if (dp != NULL)
625			return(dp);	/* not */
626		return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
627		break;
628	case OPLUS_:
629		assert(m->lastpos != NULL);
630		assert(lev+1 <= m->g->nplus);
631		m->lastpos[lev+1] = sp;
632		return(backref(m, sp, stop, ss+1, stopst, lev+1));
633		break;
634	case O_PLUS:
635		if (sp == m->lastpos[lev])	/* last pass matched null */
636			return(backref(m, sp, stop, ss+1, stopst, lev-1));
637		/* try another pass */
638		m->lastpos[lev] = sp;
639		dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
640		if (dp == NULL)
641			return(backref(m, sp, stop, ss+1, stopst, lev-1));
642		else
643			return(dp);
644		break;
645	case OCH_:		/* find the right one, if any */
646		ssub = ss + 1;
647		esub = ss + OPND(s) - 1;
648		assert(OP(m->g->strip[esub]) == OOR1);
649		for (;;) {	/* find first matching branch */
650			dp = backref(m, sp, stop, ssub, esub, lev);
651			if (dp != NULL)
652				return(dp);
653			/* that one missed, try next one */
654			if (OP(m->g->strip[esub]) == O_CH)
655				return(NULL);	/* there is none */
656			esub++;
657			assert(OP(m->g->strip[esub]) == OOR2);
658			ssub = esub + 1;
659			esub += OPND(m->g->strip[esub]);
660			if (OP(m->g->strip[esub]) == OOR2)
661				esub--;
662			else
663				assert(OP(m->g->strip[esub]) == O_CH);
664		}
665		break;
666	case OLPAREN:		/* must undo assignment if rest fails */
667		i = OPND(s);
668		assert(0 < i && i <= m->g->nsub);
669		offsave = m->pmatch[i].rm_so;
670		m->pmatch[i].rm_so = sp - m->offp;
671		dp = backref(m, sp, stop, ss+1, stopst, lev);
672		if (dp != NULL)
673			return(dp);
674		m->pmatch[i].rm_so = offsave;
675		return(NULL);
676		break;
677	case ORPAREN:		/* must undo assignment if rest fails */
678		i = OPND(s);
679		assert(0 < i && i <= m->g->nsub);
680		offsave = m->pmatch[i].rm_eo;
681		m->pmatch[i].rm_eo = sp - m->offp;
682		dp = backref(m, sp, stop, ss+1, stopst, lev);
683		if (dp != NULL)
684			return(dp);
685		m->pmatch[i].rm_eo = offsave;
686		return(NULL);
687		break;
688	default:		/* uh oh */
689		assert(nope);
690		break;
691	}
692
693	/* "can't happen" */
694	assert(nope);
695	/* NOTREACHED */
696}
697
698/*
699 - fast - step through the string at top speed
700 == static char *fast(register struct match *m, char *start, \
701 ==	char *stop, sopno startst, sopno stopst);
702 */
703static char *			/* where tentative match ended, or NULL */
704fast(m, start, stop, startst, stopst)
705register struct match *m;
706char *start;
707char *stop;
708sopno startst;
709sopno stopst;
710{
711	register states st = m->st;
712	register states fresh = m->fresh;
713	register states tmp = m->tmp;
714	register char *p = start;
715	register int c = (start == m->beginp) ? OUT : *(start-1);
716	register int lastc;	/* previous c */
717	register int flagch;
718	register int i;
719	register char *coldp;	/* last p after which no match was underway */
720
721	CLEAR(st);
722	SET1(st, startst);
723	st = step(m->g, startst, stopst, st, NOTHING, st);
724	ASSIGN(fresh, st);
725	SP("start", st, *p);
726	coldp = NULL;
727	for (;;) {
728		/* next character */
729		lastc = c;
730		c = (p == m->endp) ? OUT : *p;
731		if (EQ(st, fresh))
732			coldp = p;
733
734		/* is there an EOL and/or BOL between lastc and c? */
735		flagch = '\0';
736		i = 0;
737		if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
738				(lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
739			flagch = BOL;
740			i = m->g->nbol;
741		}
742		if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
743				(c == OUT && !(m->eflags&REG_NOTEOL)) ) {
744			flagch = (flagch == BOL) ? BOLEOL : EOL;
745			i += m->g->neol;
746		}
747		if (i != 0) {
748			for (; i > 0; i--)
749				st = step(m->g, startst, stopst, st, flagch, st);
750			SP("boleol", st, c);
751		}
752
753		/* how about a word boundary? */
754		if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
755					(c != OUT && ISWORD(c)) ) {
756			flagch = BOW;
757		}
758		if ( (lastc != OUT && ISWORD(lastc)) &&
759				(flagch == EOL || (c != OUT && !ISWORD(c))) ) {
760			flagch = EOW;
761		}
762		if (flagch == BOW || flagch == EOW) {
763			st = step(m->g, startst, stopst, st, flagch, st);
764			SP("boweow", st, c);
765		}
766
767		/* are we done? */
768		if (ISSET(st, stopst) || p == stop)
769			break;		/* NOTE BREAK OUT */
770
771		/* no, we must deal with this character */
772		ASSIGN(tmp, st);
773		ASSIGN(st, fresh);
774		assert(c != OUT);
775		st = step(m->g, startst, stopst, tmp, c, st);
776		SP("aft", st, c);
777		assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
778		p++;
779	}
780
781	assert(coldp != NULL);
782	m->coldp = coldp;
783	if (ISSET(st, stopst))
784		return(p+1);
785	else
786		return(NULL);
787}
788
789/*
790 - slow - step through the string more deliberately
791 == static char *slow(register struct match *m, char *start, \
792 ==	char *stop, sopno startst, sopno stopst);
793 */
794static char *			/* where it ended */
795slow(m, start, stop, startst, stopst)
796register struct match *m;
797char *start;
798char *stop;
799sopno startst;
800sopno stopst;
801{
802	register states st = m->st;
803	register states empty = m->empty;
804	register states tmp = m->tmp;
805	register char *p = start;
806	register int c = (start == m->beginp) ? OUT : *(start-1);
807	register int lastc;	/* previous c */
808	register int flagch;
809	register int i;
810	register char *matchp;	/* last p at which a match ended */
811
812	AT("slow", start, stop, startst, stopst);
813	CLEAR(st);
814	SET1(st, startst);
815	SP("sstart", st, *p);
816	st = step(m->g, startst, stopst, st, NOTHING, st);
817	matchp = NULL;
818	for (;;) {
819		/* next character */
820		lastc = c;
821		c = (p == m->endp) ? OUT : *p;
822
823		/* is there an EOL and/or BOL between lastc and c? */
824		flagch = '\0';
825		i = 0;
826		if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
827				(lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
828			flagch = BOL;
829			i = m->g->nbol;
830		}
831		if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
832				(c == OUT && !(m->eflags&REG_NOTEOL)) ) {
833			flagch = (flagch == BOL) ? BOLEOL : EOL;
834			i += m->g->neol;
835		}
836		if (i != 0) {
837			for (; i > 0; i--)
838				st = step(m->g, startst, stopst, st, flagch, st);
839			SP("sboleol", st, c);
840		}
841
842		/* how about a word boundary? */
843		if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
844					(c != OUT && ISWORD(c)) ) {
845			flagch = BOW;
846		}
847		if ( (lastc != OUT && ISWORD(lastc)) &&
848				(flagch == EOL || (c != OUT && !ISWORD(c))) ) {
849			flagch = EOW;
850		}
851		if (flagch == BOW || flagch == EOW) {
852			st = step(m->g, startst, stopst, st, flagch, st);
853			SP("sboweow", st, c);
854		}
855
856		/* are we done? */
857		if (ISSET(st, stopst))
858			matchp = p;
859		if (EQ(st, empty) || p == stop)
860			break;		/* NOTE BREAK OUT */
861
862		/* no, we must deal with this character */
863		ASSIGN(tmp, st);
864		ASSIGN(st, empty);
865		assert(c != OUT);
866		st = step(m->g, startst, stopst, tmp, c, st);
867		SP("saft", st, c);
868		assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
869		p++;
870	}
871
872	return(matchp);
873}
874
875
876/*
877 - step - map set of states reachable before char to set reachable after
878 == static states step(register struct re_guts *g, sopno start, sopno stop, \
879 ==	register states bef, int ch, register states aft);
880 == #define	BOL	(OUT+1)
881 == #define	EOL	(BOL+1)
882 == #define	BOLEOL	(BOL+2)
883 == #define	NOTHING	(BOL+3)
884 == #define	BOW	(BOL+4)
885 == #define	EOW	(BOL+5)
886 == #define	CODEMAX	(BOL+5)		// highest code used
887 == #define	NONCHAR(c)	((c) > CHAR_MAX)
888 == #define	NNONCHAR	(CODEMAX-CHAR_MAX)
889 */
890static states
891step(g, start, stop, bef, ch, aft)
892register struct re_guts *g;
893sopno start;			/* start state within strip */
894sopno stop;			/* state after stop state within strip */
895register states bef;		/* states reachable before */
896int ch;				/* character or NONCHAR code */
897register states aft;		/* states already known reachable after */
898{
899	register cset *cs;
900	register sop s;
901	register sopno pc;
902	register onestate here;		/* note, macros know this name */
903	register sopno look;
904	register int i;
905
906	for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
907		s = g->strip[pc];
908		switch (OP(s)) {
909		case OEND:
910			assert(pc == stop-1);
911			break;
912		case OCHAR:
913			/* only characters can match */
914			assert(!NONCHAR(ch) || ch != (char)OPND(s));
915			if (ch == (char)OPND(s))
916				FWD(aft, bef, 1);
917			break;
918		case OBOL:
919			if (ch == BOL || ch == BOLEOL)
920				FWD(aft, bef, 1);
921			break;
922		case OEOL:
923			if (ch == EOL || ch == BOLEOL)
924				FWD(aft, bef, 1);
925			break;
926		case OBOW:
927			if (ch == BOW)
928				FWD(aft, bef, 1);
929			break;
930		case OEOW:
931			if (ch == EOW)
932				FWD(aft, bef, 1);
933			break;
934		case OANY:
935			if (!NONCHAR(ch))
936				FWD(aft, bef, 1);
937			break;
938		case OANYOF:
939			cs = &g->sets[OPND(s)];
940			if (!NONCHAR(ch) && CHIN(cs, ch))
941				FWD(aft, bef, 1);
942			break;
943		case OBACK_:		/* ignored here */
944		case O_BACK:
945			FWD(aft, aft, 1);
946			break;
947		case OPLUS_:		/* forward, this is just an empty */
948			FWD(aft, aft, 1);
949			break;
950		case O_PLUS:		/* both forward and back */
951			FWD(aft, aft, 1);
952			i = ISSETBACK(aft, OPND(s));
953			BACK(aft, aft, OPND(s));
954			if (!i && ISSETBACK(aft, OPND(s))) {
955				/* oho, must reconsider loop body */
956				pc -= OPND(s) + 1;
957				INIT(here, pc);
958			}
959			break;
960		case OQUEST_:		/* two branches, both forward */
961			FWD(aft, aft, 1);
962			FWD(aft, aft, OPND(s));
963			break;
964		case O_QUEST:		/* just an empty */
965			FWD(aft, aft, 1);
966			break;
967		case OLPAREN:		/* not significant here */
968		case ORPAREN:
969			FWD(aft, aft, 1);
970			break;
971		case OCH_:		/* mark the first two branches */
972			FWD(aft, aft, 1);
973			assert(OP(g->strip[pc+OPND(s)]) == OOR2);
974			FWD(aft, aft, OPND(s));
975			break;
976		case OOR1:		/* done a branch, find the O_CH */
977			if (ISSTATEIN(aft, here)) {
978				for (look = 1;
979						OP(s = g->strip[pc+look]) != O_CH;
980						look += OPND(s))
981					assert(OP(s) == OOR2);
982				FWD(aft, aft, look);
983			}
984			break;
985		case OOR2:		/* propagate OCH_'s marking */
986			FWD(aft, aft, 1);
987			if (OP(g->strip[pc+OPND(s)]) != O_CH) {
988				assert(OP(g->strip[pc+OPND(s)]) == OOR2);
989				FWD(aft, aft, OPND(s));
990			}
991			break;
992		case O_CH:		/* just empty */
993			FWD(aft, aft, 1);
994			break;
995		default:		/* ooooops... */
996			assert(nope);
997			break;
998		}
999	}
1000
1001	return(aft);
1002}
1003
1004#ifdef REDEBUG
1005/*
1006 - print - print a set of states
1007 == #ifdef REDEBUG
1008 == static void print(struct match *m, char *caption, states st, \
1009 ==	int ch, FILE *d);
1010 == #endif
1011 */
1012static void
1013print(m, caption, st, ch, d)
1014struct match *m;
1015char *caption;
1016states st;
1017int ch;
1018FILE *d;
1019{
1020	register struct re_guts *g = m->g;
1021	register int i;
1022	register int first = 1;
1023
1024	if (!(m->eflags&REG_TRACE))
1025		return;
1026
1027	(void)fprintf(d, "%s", caption);
1028	if (ch != '\0')
1029		(void)fprintf(d, " %s", pchar(ch));
1030	for (i = 0; i < g->nstates; i++)
1031		if (ISSET(st, i)) {
1032			(void)fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
1033			first = 0;
1034		}
1035	(void)fprintf(d, "\n");
1036}
1037
1038/*
1039 - at - print current situation
1040 == #ifdef REDEBUG
1041 == static void at(struct match *m, char *title, char *start, char *stop, \
1042 ==						sopno startst, sopno stopst);
1043 == #endif
1044 */
1045static void
1046at(m, title, start, stop, startst, stopst)
1047struct match *m;
1048char *title;
1049char *start;
1050char *stop;
1051sopno startst;
1052sopno stopst;
1053{
1054	if (!(m->eflags&REG_TRACE))
1055		return;
1056
1057	(void)printf("%s %s-", title, pchar(*start));
1058	(void)printf("%s ", pchar(*stop));
1059	(void)printf("%ld-%ld\n", (long)startst, (long)stopst);
1060}
1061
1062#ifndef PCHARDONE
1063#define	PCHARDONE	/* never again */
1064/*
1065 - pchar - make a character printable
1066 == #ifdef REDEBUG
1067 == static char *pchar(int ch);
1068 == #endif
1069 *
1070 * Is this identical to regchar() over in debug.c?  Well, yes.  But a
1071 * duplicate here avoids having a debugging-capable regexec.o tied to
1072 * a matching debug.o, and this is convenient.  It all disappears in
1073 * the non-debug compilation anyway, so it doesn't matter much.
1074 */
1075static char *			/* -> representation */
1076pchar(ch)
1077int ch;
1078{
1079	static char pbuf[10];
1080
1081	if (isprint(ch) || ch == ' ')
1082		(void)snprintf(pbuf, sizeof pbuf, "%c", ch);
1083	else
1084		(void)snprintf(pbuf, sizeof pbuf, "\\%o", ch);
1085	return(pbuf);
1086}
1087#endif
1088#endif
1089
1090#undef	matcher
1091#undef	fast
1092#undef	slow
1093#undef	dissect
1094#undef	backref
1095#undef	step
1096#undef	print
1097#undef	at
1098#undef	match
1099#undef	nope
1100