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