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