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