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