158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 Henry Spencer. 358f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 458f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 558f0484fSRodney W. Grimes * 658f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 758f0484fSRodney W. Grimes * Henry Spencer. 858f0484fSRodney W. Grimes * 958f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1058f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1158f0484fSRodney W. Grimes * are met: 1258f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1458f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1658f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 3258f0484fSRodney W. Grimes * 3358f0484fSRodney W. Grimes * @(#)engine.c 8.5 (Berkeley) 3/20/94 3458f0484fSRodney W. Grimes */ 3558f0484fSRodney W. Grimes 36333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 37333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38333fc21eSDavid E. O'Brien 3958f0484fSRodney W. Grimes /* 4058f0484fSRodney W. Grimes * The matching engine and friends. This file is #included by regexec.c 4158f0484fSRodney W. Grimes * after suitable #defines of a variety of macros used herein, so that 4258f0484fSRodney W. Grimes * different state representations can be used without duplicating masses 4358f0484fSRodney W. Grimes * of code. 4458f0484fSRodney W. Grimes */ 4558f0484fSRodney W. Grimes 4658f0484fSRodney W. Grimes #ifdef SNAMES 4758f0484fSRodney W. Grimes #define matcher smatcher 4858f0484fSRodney W. Grimes #define fast sfast 4958f0484fSRodney W. Grimes #define slow sslow 5058f0484fSRodney W. Grimes #define dissect sdissect 5158f0484fSRodney W. Grimes #define backref sbackref 5258f0484fSRodney W. Grimes #define step sstep 5358f0484fSRodney W. Grimes #define print sprint 5458f0484fSRodney W. Grimes #define at sat 5558f0484fSRodney W. Grimes #define match smat 5658f0484fSRodney W. Grimes #endif 5758f0484fSRodney W. Grimes #ifdef LNAMES 5858f0484fSRodney W. Grimes #define matcher lmatcher 5958f0484fSRodney W. Grimes #define fast lfast 6058f0484fSRodney W. Grimes #define slow lslow 6158f0484fSRodney W. Grimes #define dissect ldissect 6258f0484fSRodney W. Grimes #define backref lbackref 6358f0484fSRodney W. Grimes #define step lstep 6458f0484fSRodney W. Grimes #define print lprint 6558f0484fSRodney W. Grimes #define at lat 6658f0484fSRodney W. Grimes #define match lmat 6758f0484fSRodney W. Grimes #endif 68e5996857STim J. Robbins #ifdef MNAMES 69e5996857STim J. Robbins #define matcher mmatcher 70e5996857STim J. Robbins #define fast mfast 71e5996857STim J. Robbins #define slow mslow 72e5996857STim J. Robbins #define dissect mdissect 73e5996857STim J. Robbins #define backref mbackref 74e5996857STim J. Robbins #define step mstep 75e5996857STim J. Robbins #define print mprint 76e5996857STim J. Robbins #define at mat 77e5996857STim J. Robbins #define match mmat 78e5996857STim J. Robbins #endif 7958f0484fSRodney W. Grimes 8058f0484fSRodney W. Grimes /* another structure passed up and down to avoid zillions of parameters */ 8158f0484fSRodney W. Grimes struct match { 8258f0484fSRodney W. Grimes struct re_guts *g; 8358f0484fSRodney W. Grimes int eflags; 8458f0484fSRodney W. Grimes regmatch_t *pmatch; /* [nsub+1] (0 element unused) */ 8558f0484fSRodney W. Grimes char *offp; /* offsets work from here */ 8658f0484fSRodney W. Grimes char *beginp; /* start of string -- virtual NUL precedes */ 8758f0484fSRodney W. Grimes char *endp; /* end of string -- virtual NUL here */ 8858f0484fSRodney W. Grimes char *coldp; /* can be no match starting before here */ 8958f0484fSRodney W. Grimes char **lastpos; /* [nplus+1] */ 9058f0484fSRodney W. Grimes STATEVARS; 9158f0484fSRodney W. Grimes states st; /* current states */ 9258f0484fSRodney W. Grimes states fresh; /* states for a fresh start */ 9358f0484fSRodney W. Grimes states tmp; /* temporary */ 9458f0484fSRodney W. Grimes states empty; /* empty set of states */ 95e5996857STim J. Robbins mbstate_t mbs; /* multibyte conversion state */ 9658f0484fSRodney W. Grimes }; 9758f0484fSRodney W. Grimes 9858f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */ 9958f0484fSRodney W. Grimes #ifdef __cplusplus 10058f0484fSRodney W. Grimes extern "C" { 10158f0484fSRodney W. Grimes #endif 10258f0484fSRodney W. Grimes 10358f0484fSRodney W. Grimes /* === engine.c === */ 104c05ac53bSDavid E. O'Brien static int matcher(struct re_guts *g, char *string, size_t nmatch, regmatch_t pmatch[], int eflags); 105c05ac53bSDavid E. O'Brien static char *dissect(struct match *m, char *start, char *stop, sopno startst, sopno stopst); 106c05ac53bSDavid E. O'Brien static char *backref(struct match *m, char *start, char *stop, sopno startst, sopno stopst, sopno lev); 107c05ac53bSDavid E. O'Brien static char *fast(struct match *m, char *start, char *stop, sopno startst, sopno stopst); 108c05ac53bSDavid E. O'Brien static char *slow(struct match *m, char *start, char *stop, sopno startst, sopno stopst); 109e5996857STim J. Robbins static states step(struct re_guts *g, sopno start, sopno stop, states bef, wint_t ch, states aft); 110e5996857STim J. Robbins #define BOL (OUT-1) 111e5996857STim J. Robbins #define EOL (BOL-1) 112e5996857STim J. Robbins #define BOLEOL (BOL-2) 113e5996857STim J. Robbins #define NOTHING (BOL-3) 114e5996857STim J. Robbins #define BOW (BOL-4) 115e5996857STim J. Robbins #define EOW (BOL-5) 116e5996857STim J. Robbins #define BADCHAR (BOL-6) 117e5996857STim J. Robbins #define NONCHAR(c) ((c) <= OUT) 11858f0484fSRodney W. Grimes #ifdef REDEBUG 119c05ac53bSDavid E. O'Brien static void print(struct match *m, char *caption, states st, int ch, FILE *d); 12058f0484fSRodney W. Grimes #endif 12158f0484fSRodney W. Grimes #ifdef REDEBUG 122c05ac53bSDavid E. O'Brien static void at(struct match *m, char *title, char *start, char *stop, sopno startst, sopno stopst); 12358f0484fSRodney W. Grimes #endif 12458f0484fSRodney W. Grimes #ifdef REDEBUG 125c05ac53bSDavid E. O'Brien static char *pchar(int ch); 12658f0484fSRodney W. Grimes #endif 12758f0484fSRodney W. Grimes 12858f0484fSRodney W. Grimes #ifdef __cplusplus 12958f0484fSRodney W. Grimes } 13058f0484fSRodney W. Grimes #endif 13158f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */ 13258f0484fSRodney W. Grimes 13358f0484fSRodney W. Grimes #ifdef REDEBUG 13458f0484fSRodney W. Grimes #define SP(t, s, c) print(m, t, s, c, stdout) 13558f0484fSRodney W. Grimes #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2) 13658f0484fSRodney W. Grimes #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); } 13758f0484fSRodney W. Grimes #else 13858f0484fSRodney W. Grimes #define SP(t, s, c) /* nothing */ 13958f0484fSRodney W. Grimes #define AT(t, p1, p2, s1, s2) /* nothing */ 14058f0484fSRodney W. Grimes #define NOTE(s) /* nothing */ 14158f0484fSRodney W. Grimes #endif 14258f0484fSRodney W. Grimes 14358f0484fSRodney W. Grimes /* 14458f0484fSRodney W. Grimes - matcher - the actual matching engine 1458fb3f3f6SDavid E. O'Brien == static int matcher(struct re_guts *g, char *string, \ 14658f0484fSRodney W. Grimes == size_t nmatch, regmatch_t pmatch[], int eflags); 14758f0484fSRodney W. Grimes */ 14858f0484fSRodney W. Grimes static int /* 0 success, REG_NOMATCH failure */ 14958f0484fSRodney W. Grimes matcher(g, string, nmatch, pmatch, eflags) 1508fb3f3f6SDavid E. O'Brien struct re_guts *g; 15158f0484fSRodney W. Grimes char *string; 15258f0484fSRodney W. Grimes size_t nmatch; 15358f0484fSRodney W. Grimes regmatch_t pmatch[]; 15458f0484fSRodney W. Grimes int eflags; 15558f0484fSRodney W. Grimes { 1568fb3f3f6SDavid E. O'Brien char *endp; 1578fb3f3f6SDavid E. O'Brien int i; 15858f0484fSRodney W. Grimes struct match mv; 1598fb3f3f6SDavid E. O'Brien struct match *m = &mv; 1608fb3f3f6SDavid E. O'Brien char *dp; 1618fb3f3f6SDavid E. O'Brien const sopno gf = g->firststate+1; /* +1 for OEND */ 1628fb3f3f6SDavid E. O'Brien const sopno gl = g->laststate; 16358f0484fSRodney W. Grimes char *start; 16458f0484fSRodney W. Grimes char *stop; 1656049d9f0SDaniel C. Sobral /* Boyer-Moore algorithms variables */ 1668fb3f3f6SDavid E. O'Brien char *pp; 1676049d9f0SDaniel C. Sobral int cj, mj; 1688fb3f3f6SDavid E. O'Brien char *mustfirst; 1698fb3f3f6SDavid E. O'Brien char *mustlast; 1708fb3f3f6SDavid E. O'Brien int *matchjump; 1718fb3f3f6SDavid E. O'Brien int *charjump; 17258f0484fSRodney W. Grimes 17358f0484fSRodney W. Grimes /* simplify the situation where possible */ 17458f0484fSRodney W. Grimes if (g->cflags®_NOSUB) 17558f0484fSRodney W. Grimes nmatch = 0; 17658f0484fSRodney W. Grimes if (eflags®_STARTEND) { 17758f0484fSRodney W. Grimes start = string + pmatch[0].rm_so; 17858f0484fSRodney W. Grimes stop = string + pmatch[0].rm_eo; 17958f0484fSRodney W. Grimes } else { 18058f0484fSRodney W. Grimes start = string; 18158f0484fSRodney W. Grimes stop = start + strlen(start); 18258f0484fSRodney W. Grimes } 18358f0484fSRodney W. Grimes if (stop < start) 18458f0484fSRodney W. Grimes return(REG_INVARG); 18558f0484fSRodney W. Grimes 18658f0484fSRodney W. Grimes /* prescreening; this does wonders for this rather slow code */ 18758f0484fSRodney W. Grimes if (g->must != NULL) { 1886049d9f0SDaniel C. Sobral if (g->charjump != NULL && g->matchjump != NULL) { 1896049d9f0SDaniel C. Sobral mustfirst = g->must; 1906049d9f0SDaniel C. Sobral mustlast = g->must + g->mlen - 1; 1916049d9f0SDaniel C. Sobral charjump = g->charjump; 1926049d9f0SDaniel C. Sobral matchjump = g->matchjump; 1936049d9f0SDaniel C. Sobral pp = mustlast; 194c5e125bbSDaniel C. Sobral for (dp = start+g->mlen-1; dp < stop;) { 1956049d9f0SDaniel C. Sobral /* Fast skip non-matches */ 196e0554a53SJacques Vidrine while (dp < stop && charjump[(int)*dp]) 197e0554a53SJacques Vidrine dp += charjump[(int)*dp]; 1986049d9f0SDaniel C. Sobral 199c5e125bbSDaniel C. Sobral if (dp >= stop) 2006049d9f0SDaniel C. Sobral break; 2016049d9f0SDaniel C. Sobral 2026049d9f0SDaniel C. Sobral /* Greedy matcher */ 2036049d9f0SDaniel C. Sobral /* We depend on not being used for 2046049d9f0SDaniel C. Sobral * for strings of length 1 2056049d9f0SDaniel C. Sobral */ 206c5e125bbSDaniel C. Sobral while (*--dp == *--pp && pp != mustfirst); 2076049d9f0SDaniel C. Sobral 208c5e125bbSDaniel C. Sobral if (*dp == *pp) 2096049d9f0SDaniel C. Sobral break; 2106049d9f0SDaniel C. Sobral 2116049d9f0SDaniel C. Sobral /* Jump to next possible match */ 2126049d9f0SDaniel C. Sobral mj = matchjump[pp - mustfirst]; 213e0554a53SJacques Vidrine cj = charjump[(int)*dp]; 214c5e125bbSDaniel C. Sobral dp += (cj < mj ? mj : cj); 2156049d9f0SDaniel C. Sobral pp = mustlast; 2166049d9f0SDaniel C. Sobral } 2176049d9f0SDaniel C. Sobral if (pp != mustfirst) 2186049d9f0SDaniel C. Sobral return(REG_NOMATCH); 2196049d9f0SDaniel C. Sobral } else { 22058f0484fSRodney W. Grimes for (dp = start; dp < stop; dp++) 2216049d9f0SDaniel C. Sobral if (*dp == g->must[0] && 2226049d9f0SDaniel C. Sobral stop - dp >= g->mlen && 22358f0484fSRodney W. Grimes memcmp(dp, g->must, (size_t)g->mlen) == 0) 22458f0484fSRodney W. Grimes break; 22558f0484fSRodney W. Grimes if (dp == stop) /* we didn't find g->must */ 22658f0484fSRodney W. Grimes return(REG_NOMATCH); 22758f0484fSRodney W. Grimes } 2286049d9f0SDaniel C. Sobral } 22958f0484fSRodney W. Grimes 23058f0484fSRodney W. Grimes /* match struct setup */ 23158f0484fSRodney W. Grimes m->g = g; 23258f0484fSRodney W. Grimes m->eflags = eflags; 23358f0484fSRodney W. Grimes m->pmatch = NULL; 23458f0484fSRodney W. Grimes m->lastpos = NULL; 23558f0484fSRodney W. Grimes m->offp = string; 23658f0484fSRodney W. Grimes m->beginp = start; 23758f0484fSRodney W. Grimes m->endp = stop; 23858f0484fSRodney W. Grimes STATESETUP(m, 4); 23958f0484fSRodney W. Grimes SETUP(m->st); 24058f0484fSRodney W. Grimes SETUP(m->fresh); 24158f0484fSRodney W. Grimes SETUP(m->tmp); 24258f0484fSRodney W. Grimes SETUP(m->empty); 24358f0484fSRodney W. Grimes CLEAR(m->empty); 244e5996857STim J. Robbins ZAPSTATE(&m->mbs); 24558f0484fSRodney W. Grimes 246e6a886d8SDaniel C. Sobral /* Adjust start according to moffset, to speed things up */ 247e6a886d8SDaniel C. Sobral if (g->moffset > -1) 248b6c1a561SDaniel C. Sobral start = ((dp - g->moffset) < start) ? start : dp - g->moffset; 249e6a886d8SDaniel C. Sobral 25058f0484fSRodney W. Grimes /* this loop does only one repetition except for backrefs */ 25158f0484fSRodney W. Grimes for (;;) { 25258f0484fSRodney W. Grimes endp = fast(m, start, stop, gf, gl); 25358f0484fSRodney W. Grimes if (endp == NULL) { /* a miss */ 254c7ce9e21SDiomidis Spinellis if (m->pmatch != NULL) 255c7ce9e21SDiomidis Spinellis free((char *)m->pmatch); 256c7ce9e21SDiomidis Spinellis if (m->lastpos != NULL) 257c7ce9e21SDiomidis Spinellis free((char *)m->lastpos); 25858f0484fSRodney W. Grimes STATETEARDOWN(m); 25958f0484fSRodney W. Grimes return(REG_NOMATCH); 26058f0484fSRodney W. Grimes } 26158f0484fSRodney W. Grimes if (nmatch == 0 && !g->backrefs) 26258f0484fSRodney W. Grimes break; /* no further info needed */ 26358f0484fSRodney W. Grimes 26458f0484fSRodney W. Grimes /* where? */ 26558f0484fSRodney W. Grimes assert(m->coldp != NULL); 26658f0484fSRodney W. Grimes for (;;) { 26758f0484fSRodney W. Grimes NOTE("finding start"); 26858f0484fSRodney W. Grimes endp = slow(m, m->coldp, stop, gf, gl); 26958f0484fSRodney W. Grimes if (endp != NULL) 27058f0484fSRodney W. Grimes break; 27158f0484fSRodney W. Grimes assert(m->coldp < m->endp); 272e5996857STim J. Robbins m->coldp += XMBRTOWC(NULL, m->coldp, 273e5996857STim J. Robbins m->endp - m->coldp, &m->mbs, 0); 27458f0484fSRodney W. Grimes } 27558f0484fSRodney W. Grimes if (nmatch == 1 && !g->backrefs) 27658f0484fSRodney W. Grimes break; /* no further info needed */ 27758f0484fSRodney W. Grimes 27858f0484fSRodney W. Grimes /* oh my, he wants the subexpressions... */ 27958f0484fSRodney W. Grimes if (m->pmatch == NULL) 28058f0484fSRodney W. Grimes m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) * 28158f0484fSRodney W. Grimes sizeof(regmatch_t)); 28258f0484fSRodney W. Grimes if (m->pmatch == NULL) { 28358f0484fSRodney W. Grimes STATETEARDOWN(m); 28458f0484fSRodney W. Grimes return(REG_ESPACE); 28558f0484fSRodney W. Grimes } 28658f0484fSRodney W. Grimes for (i = 1; i <= m->g->nsub; i++) 28758f0484fSRodney W. Grimes m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1; 28858f0484fSRodney W. Grimes if (!g->backrefs && !(m->eflags®_BACKR)) { 28958f0484fSRodney W. Grimes NOTE("dissecting"); 29058f0484fSRodney W. Grimes dp = dissect(m, m->coldp, endp, gf, gl); 29158f0484fSRodney W. Grimes } else { 29258f0484fSRodney W. Grimes if (g->nplus > 0 && m->lastpos == NULL) 29358f0484fSRodney W. Grimes m->lastpos = (char **)malloc((g->nplus+1) * 29458f0484fSRodney W. Grimes sizeof(char *)); 29558f0484fSRodney W. Grimes if (g->nplus > 0 && m->lastpos == NULL) { 29658f0484fSRodney W. Grimes free(m->pmatch); 29758f0484fSRodney W. Grimes STATETEARDOWN(m); 29858f0484fSRodney W. Grimes return(REG_ESPACE); 29958f0484fSRodney W. Grimes } 30058f0484fSRodney W. Grimes NOTE("backref dissect"); 30158f0484fSRodney W. Grimes dp = backref(m, m->coldp, endp, gf, gl, (sopno)0); 30258f0484fSRodney W. Grimes } 30358f0484fSRodney W. Grimes if (dp != NULL) 30458f0484fSRodney W. Grimes break; 30558f0484fSRodney W. Grimes 30658f0484fSRodney W. Grimes /* uh-oh... we couldn't find a subexpression-level match */ 30758f0484fSRodney W. Grimes assert(g->backrefs); /* must be back references doing it */ 30858f0484fSRodney W. Grimes assert(g->nplus == 0 || m->lastpos != NULL); 30958f0484fSRodney W. Grimes for (;;) { 31058f0484fSRodney W. Grimes if (dp != NULL || endp <= m->coldp) 31158f0484fSRodney W. Grimes break; /* defeat */ 31258f0484fSRodney W. Grimes NOTE("backoff"); 31358f0484fSRodney W. Grimes endp = slow(m, m->coldp, endp-1, gf, gl); 31458f0484fSRodney W. Grimes if (endp == NULL) 31558f0484fSRodney W. Grimes break; /* defeat */ 31658f0484fSRodney W. Grimes /* try it on a shorter possibility */ 31758f0484fSRodney W. Grimes #ifndef NDEBUG 31858f0484fSRodney W. Grimes for (i = 1; i <= m->g->nsub; i++) { 31958f0484fSRodney W. Grimes assert(m->pmatch[i].rm_so == -1); 32058f0484fSRodney W. Grimes assert(m->pmatch[i].rm_eo == -1); 32158f0484fSRodney W. Grimes } 32258f0484fSRodney W. Grimes #endif 32358f0484fSRodney W. Grimes NOTE("backoff dissect"); 32458f0484fSRodney W. Grimes dp = backref(m, m->coldp, endp, gf, gl, (sopno)0); 32558f0484fSRodney W. Grimes } 32658f0484fSRodney W. Grimes assert(dp == NULL || dp == endp); 32758f0484fSRodney W. Grimes if (dp != NULL) /* found a shorter one */ 32858f0484fSRodney W. Grimes break; 32958f0484fSRodney W. Grimes 33058f0484fSRodney W. Grimes /* despite initial appearances, there is no match here */ 33158f0484fSRodney W. Grimes NOTE("false alarm"); 332e5996857STim J. Robbins /* recycle starting later */ 333e5996857STim J. Robbins start = m->coldp + XMBRTOWC(NULL, m->coldp, 334bd9643b1STim J. Robbins stop - m->coldp, &m->mbs, 0); 33558f0484fSRodney W. Grimes assert(start <= stop); 33658f0484fSRodney W. Grimes } 33758f0484fSRodney W. Grimes 33858f0484fSRodney W. Grimes /* fill in the details if requested */ 33958f0484fSRodney W. Grimes if (nmatch > 0) { 34058f0484fSRodney W. Grimes pmatch[0].rm_so = m->coldp - m->offp; 34158f0484fSRodney W. Grimes pmatch[0].rm_eo = endp - m->offp; 34258f0484fSRodney W. Grimes } 34358f0484fSRodney W. Grimes if (nmatch > 1) { 34458f0484fSRodney W. Grimes assert(m->pmatch != NULL); 34558f0484fSRodney W. Grimes for (i = 1; i < nmatch; i++) 34658f0484fSRodney W. Grimes if (i <= m->g->nsub) 34758f0484fSRodney W. Grimes pmatch[i] = m->pmatch[i]; 34858f0484fSRodney W. Grimes else { 34958f0484fSRodney W. Grimes pmatch[i].rm_so = -1; 35058f0484fSRodney W. Grimes pmatch[i].rm_eo = -1; 35158f0484fSRodney W. Grimes } 35258f0484fSRodney W. Grimes } 35358f0484fSRodney W. Grimes 35458f0484fSRodney W. Grimes if (m->pmatch != NULL) 35558f0484fSRodney W. Grimes free((char *)m->pmatch); 35658f0484fSRodney W. Grimes if (m->lastpos != NULL) 35758f0484fSRodney W. Grimes free((char *)m->lastpos); 35858f0484fSRodney W. Grimes STATETEARDOWN(m); 35958f0484fSRodney W. Grimes return(0); 36058f0484fSRodney W. Grimes } 36158f0484fSRodney W. Grimes 36258f0484fSRodney W. Grimes /* 36358f0484fSRodney W. Grimes - dissect - figure out what matched what, no back references 3648fb3f3f6SDavid E. O'Brien == static char *dissect(struct match *m, char *start, \ 36558f0484fSRodney W. Grimes == char *stop, sopno startst, sopno stopst); 36658f0484fSRodney W. Grimes */ 36758f0484fSRodney W. Grimes static char * /* == stop (success) always */ 36858f0484fSRodney W. Grimes dissect(m, start, stop, startst, stopst) 3698fb3f3f6SDavid E. O'Brien struct match *m; 37058f0484fSRodney W. Grimes char *start; 37158f0484fSRodney W. Grimes char *stop; 37258f0484fSRodney W. Grimes sopno startst; 37358f0484fSRodney W. Grimes sopno stopst; 37458f0484fSRodney W. Grimes { 3758fb3f3f6SDavid E. O'Brien int i; 3768fb3f3f6SDavid E. O'Brien sopno ss; /* start sop of current subRE */ 3778fb3f3f6SDavid E. O'Brien sopno es; /* end sop of current subRE */ 3788fb3f3f6SDavid E. O'Brien char *sp; /* start of string matched by it */ 3798fb3f3f6SDavid E. O'Brien char *stp; /* string matched by it cannot pass here */ 3808fb3f3f6SDavid E. O'Brien char *rest; /* start of rest of string */ 3818fb3f3f6SDavid E. O'Brien char *tail; /* string unmatched by rest of RE */ 3828fb3f3f6SDavid E. O'Brien sopno ssub; /* start sop of subsubRE */ 3838fb3f3f6SDavid E. O'Brien sopno esub; /* end sop of subsubRE */ 3848fb3f3f6SDavid E. O'Brien char *ssp; /* start of string matched by subsubRE */ 3858fb3f3f6SDavid E. O'Brien char *sep; /* end of string matched by subsubRE */ 3868fb3f3f6SDavid E. O'Brien char *oldssp; /* previous ssp */ 3878fb3f3f6SDavid E. O'Brien char *dp; 38858f0484fSRodney W. Grimes 38958f0484fSRodney W. Grimes AT("diss", start, stop, startst, stopst); 39058f0484fSRodney W. Grimes sp = start; 39158f0484fSRodney W. Grimes for (ss = startst; ss < stopst; ss = es) { 39258f0484fSRodney W. Grimes /* identify end of subRE */ 39358f0484fSRodney W. Grimes es = ss; 39458f0484fSRodney W. Grimes switch (OP(m->g->strip[es])) { 39558f0484fSRodney W. Grimes case OPLUS_: 39658f0484fSRodney W. Grimes case OQUEST_: 39758f0484fSRodney W. Grimes es += OPND(m->g->strip[es]); 39858f0484fSRodney W. Grimes break; 39958f0484fSRodney W. Grimes case OCH_: 40058f0484fSRodney W. Grimes while (OP(m->g->strip[es]) != O_CH) 40158f0484fSRodney W. Grimes es += OPND(m->g->strip[es]); 40258f0484fSRodney W. Grimes break; 40358f0484fSRodney W. Grimes } 40458f0484fSRodney W. Grimes es++; 40558f0484fSRodney W. Grimes 40658f0484fSRodney W. Grimes /* figure out what it matched */ 40758f0484fSRodney W. Grimes switch (OP(m->g->strip[ss])) { 40858f0484fSRodney W. Grimes case OEND: 40958f0484fSRodney W. Grimes assert(nope); 41058f0484fSRodney W. Grimes break; 41158f0484fSRodney W. Grimes case OCHAR: 412e5996857STim J. Robbins sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0); 41358f0484fSRodney W. Grimes break; 41458f0484fSRodney W. Grimes case OBOL: 41558f0484fSRodney W. Grimes case OEOL: 41658f0484fSRodney W. Grimes case OBOW: 41758f0484fSRodney W. Grimes case OEOW: 41858f0484fSRodney W. Grimes break; 41958f0484fSRodney W. Grimes case OANY: 42058f0484fSRodney W. Grimes case OANYOF: 421e5996857STim J. Robbins sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0); 42258f0484fSRodney W. Grimes break; 42358f0484fSRodney W. Grimes case OBACK_: 42458f0484fSRodney W. Grimes case O_BACK: 42558f0484fSRodney W. Grimes assert(nope); 42658f0484fSRodney W. Grimes break; 42758f0484fSRodney W. Grimes /* cases where length of match is hard to find */ 42858f0484fSRodney W. Grimes case OQUEST_: 42958f0484fSRodney W. Grimes stp = stop; 43058f0484fSRodney W. Grimes for (;;) { 43158f0484fSRodney W. Grimes /* how long could this one be? */ 43258f0484fSRodney W. Grimes rest = slow(m, sp, stp, ss, es); 43358f0484fSRodney W. Grimes assert(rest != NULL); /* it did match */ 43458f0484fSRodney W. Grimes /* could the rest match the rest? */ 43558f0484fSRodney W. Grimes tail = slow(m, rest, stop, es, stopst); 43658f0484fSRodney W. Grimes if (tail == stop) 43758f0484fSRodney W. Grimes break; /* yes! */ 43858f0484fSRodney W. Grimes /* no -- try a shorter match for this one */ 43958f0484fSRodney W. Grimes stp = rest - 1; 44058f0484fSRodney W. Grimes assert(stp >= sp); /* it did work */ 44158f0484fSRodney W. Grimes } 44258f0484fSRodney W. Grimes ssub = ss + 1; 44358f0484fSRodney W. Grimes esub = es - 1; 44458f0484fSRodney W. Grimes /* did innards match? */ 44558f0484fSRodney W. Grimes if (slow(m, sp, rest, ssub, esub) != NULL) { 44658f0484fSRodney W. Grimes dp = dissect(m, sp, rest, ssub, esub); 44758f0484fSRodney W. Grimes assert(dp == rest); 44858f0484fSRodney W. Grimes } else /* no */ 44958f0484fSRodney W. Grimes assert(sp == rest); 45058f0484fSRodney W. Grimes sp = rest; 45158f0484fSRodney W. Grimes break; 45258f0484fSRodney W. Grimes case OPLUS_: 45358f0484fSRodney W. Grimes stp = stop; 45458f0484fSRodney W. Grimes for (;;) { 45558f0484fSRodney W. Grimes /* how long could this one be? */ 45658f0484fSRodney W. Grimes rest = slow(m, sp, stp, ss, es); 45758f0484fSRodney W. Grimes assert(rest != NULL); /* it did match */ 45858f0484fSRodney W. Grimes /* could the rest match the rest? */ 45958f0484fSRodney W. Grimes tail = slow(m, rest, stop, es, stopst); 46058f0484fSRodney W. Grimes if (tail == stop) 46158f0484fSRodney W. Grimes break; /* yes! */ 46258f0484fSRodney W. Grimes /* no -- try a shorter match for this one */ 46358f0484fSRodney W. Grimes stp = rest - 1; 46458f0484fSRodney W. Grimes assert(stp >= sp); /* it did work */ 46558f0484fSRodney W. Grimes } 46658f0484fSRodney W. Grimes ssub = ss + 1; 46758f0484fSRodney W. Grimes esub = es - 1; 46858f0484fSRodney W. Grimes ssp = sp; 46958f0484fSRodney W. Grimes oldssp = ssp; 47058f0484fSRodney W. Grimes for (;;) { /* find last match of innards */ 47158f0484fSRodney W. Grimes sep = slow(m, ssp, rest, ssub, esub); 47258f0484fSRodney W. Grimes if (sep == NULL || sep == ssp) 47358f0484fSRodney W. Grimes break; /* failed or matched null */ 47458f0484fSRodney W. Grimes oldssp = ssp; /* on to next try */ 47558f0484fSRodney W. Grimes ssp = sep; 47658f0484fSRodney W. Grimes } 47758f0484fSRodney W. Grimes if (sep == NULL) { 47858f0484fSRodney W. Grimes /* last successful match */ 47958f0484fSRodney W. Grimes sep = ssp; 48058f0484fSRodney W. Grimes ssp = oldssp; 48158f0484fSRodney W. Grimes } 48258f0484fSRodney W. Grimes assert(sep == rest); /* must exhaust substring */ 48358f0484fSRodney W. Grimes assert(slow(m, ssp, sep, ssub, esub) == rest); 48458f0484fSRodney W. Grimes dp = dissect(m, ssp, sep, ssub, esub); 48558f0484fSRodney W. Grimes assert(dp == sep); 48658f0484fSRodney W. Grimes sp = rest; 48758f0484fSRodney W. Grimes break; 48858f0484fSRodney W. Grimes case OCH_: 48958f0484fSRodney W. Grimes stp = stop; 49058f0484fSRodney W. Grimes for (;;) { 49158f0484fSRodney W. Grimes /* how long could this one be? */ 49258f0484fSRodney W. Grimes rest = slow(m, sp, stp, ss, es); 49358f0484fSRodney W. Grimes assert(rest != NULL); /* it did match */ 49458f0484fSRodney W. Grimes /* could the rest match the rest? */ 49558f0484fSRodney W. Grimes tail = slow(m, rest, stop, es, stopst); 49658f0484fSRodney W. Grimes if (tail == stop) 49758f0484fSRodney W. Grimes break; /* yes! */ 49858f0484fSRodney W. Grimes /* no -- try a shorter match for this one */ 49958f0484fSRodney W. Grimes stp = rest - 1; 50058f0484fSRodney W. Grimes assert(stp >= sp); /* it did work */ 50158f0484fSRodney W. Grimes } 50258f0484fSRodney W. Grimes ssub = ss + 1; 50358f0484fSRodney W. Grimes esub = ss + OPND(m->g->strip[ss]) - 1; 50458f0484fSRodney W. Grimes assert(OP(m->g->strip[esub]) == OOR1); 50558f0484fSRodney W. Grimes for (;;) { /* find first matching branch */ 50658f0484fSRodney W. Grimes if (slow(m, sp, rest, ssub, esub) == rest) 50758f0484fSRodney W. Grimes break; /* it matched all of it */ 50858f0484fSRodney W. Grimes /* that one missed, try next one */ 50958f0484fSRodney W. Grimes assert(OP(m->g->strip[esub]) == OOR1); 51058f0484fSRodney W. Grimes esub++; 51158f0484fSRodney W. Grimes assert(OP(m->g->strip[esub]) == OOR2); 51258f0484fSRodney W. Grimes ssub = esub + 1; 51358f0484fSRodney W. Grimes esub += OPND(m->g->strip[esub]); 51458f0484fSRodney W. Grimes if (OP(m->g->strip[esub]) == OOR2) 51558f0484fSRodney W. Grimes esub--; 51658f0484fSRodney W. Grimes else 51758f0484fSRodney W. Grimes assert(OP(m->g->strip[esub]) == O_CH); 51858f0484fSRodney W. Grimes } 51958f0484fSRodney W. Grimes dp = dissect(m, sp, rest, ssub, esub); 52058f0484fSRodney W. Grimes assert(dp == rest); 52158f0484fSRodney W. Grimes sp = rest; 52258f0484fSRodney W. Grimes break; 52358f0484fSRodney W. Grimes case O_PLUS: 52458f0484fSRodney W. Grimes case O_QUEST: 52558f0484fSRodney W. Grimes case OOR1: 52658f0484fSRodney W. Grimes case OOR2: 52758f0484fSRodney W. Grimes case O_CH: 52858f0484fSRodney W. Grimes assert(nope); 52958f0484fSRodney W. Grimes break; 53058f0484fSRodney W. Grimes case OLPAREN: 53158f0484fSRodney W. Grimes i = OPND(m->g->strip[ss]); 53258f0484fSRodney W. Grimes assert(0 < i && i <= m->g->nsub); 53358f0484fSRodney W. Grimes m->pmatch[i].rm_so = sp - m->offp; 53458f0484fSRodney W. Grimes break; 53558f0484fSRodney W. Grimes case ORPAREN: 53658f0484fSRodney W. Grimes i = OPND(m->g->strip[ss]); 53758f0484fSRodney W. Grimes assert(0 < i && i <= m->g->nsub); 53858f0484fSRodney W. Grimes m->pmatch[i].rm_eo = sp - m->offp; 53958f0484fSRodney W. Grimes break; 54058f0484fSRodney W. Grimes default: /* uh oh */ 54158f0484fSRodney W. Grimes assert(nope); 54258f0484fSRodney W. Grimes break; 54358f0484fSRodney W. Grimes } 54458f0484fSRodney W. Grimes } 54558f0484fSRodney W. Grimes 54658f0484fSRodney W. Grimes assert(sp == stop); 54758f0484fSRodney W. Grimes return(sp); 54858f0484fSRodney W. Grimes } 54958f0484fSRodney W. Grimes 55058f0484fSRodney W. Grimes /* 55158f0484fSRodney W. Grimes - backref - figure out what matched what, figuring in back references 5528fb3f3f6SDavid E. O'Brien == static char *backref(struct match *m, char *start, \ 55358f0484fSRodney W. Grimes == char *stop, sopno startst, sopno stopst, sopno lev); 55458f0484fSRodney W. Grimes */ 55558f0484fSRodney W. Grimes static char * /* == stop (success) or NULL (failure) */ 55658f0484fSRodney W. Grimes backref(m, start, stop, startst, stopst, lev) 5578fb3f3f6SDavid E. O'Brien struct match *m; 55858f0484fSRodney W. Grimes char *start; 55958f0484fSRodney W. Grimes char *stop; 56058f0484fSRodney W. Grimes sopno startst; 56158f0484fSRodney W. Grimes sopno stopst; 56258f0484fSRodney W. Grimes sopno lev; /* PLUS nesting level */ 56358f0484fSRodney W. Grimes { 5648fb3f3f6SDavid E. O'Brien int i; 5658fb3f3f6SDavid E. O'Brien sopno ss; /* start sop of current subRE */ 5668fb3f3f6SDavid E. O'Brien char *sp; /* start of string matched by it */ 5678fb3f3f6SDavid E. O'Brien sopno ssub; /* start sop of subsubRE */ 5688fb3f3f6SDavid E. O'Brien sopno esub; /* end sop of subsubRE */ 5698fb3f3f6SDavid E. O'Brien char *ssp; /* start of string matched by subsubRE */ 5708fb3f3f6SDavid E. O'Brien char *dp; 5718fb3f3f6SDavid E. O'Brien size_t len; 5728fb3f3f6SDavid E. O'Brien int hard; 5738fb3f3f6SDavid E. O'Brien sop s; 5748fb3f3f6SDavid E. O'Brien regoff_t offsave; 5758fb3f3f6SDavid E. O'Brien cset *cs; 576e5996857STim J. Robbins wint_t wc; 57758f0484fSRodney W. Grimes 57858f0484fSRodney W. Grimes AT("back", start, stop, startst, stopst); 57958f0484fSRodney W. Grimes sp = start; 58058f0484fSRodney W. Grimes 58158f0484fSRodney W. Grimes /* get as far as we can with easy stuff */ 58258f0484fSRodney W. Grimes hard = 0; 58358f0484fSRodney W. Grimes for (ss = startst; !hard && ss < stopst; ss++) 58458f0484fSRodney W. Grimes switch (OP(s = m->g->strip[ss])) { 58558f0484fSRodney W. Grimes case OCHAR: 586e5996857STim J. Robbins if (sp == stop) 587e5996857STim J. Robbins return(NULL); 588e5996857STim J. Robbins sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR); 589e5996857STim J. Robbins if (wc != OPND(s)) 59058f0484fSRodney W. Grimes return(NULL); 59158f0484fSRodney W. Grimes break; 59258f0484fSRodney W. Grimes case OANY: 59358f0484fSRodney W. Grimes if (sp == stop) 59458f0484fSRodney W. Grimes return(NULL); 595e5996857STim J. Robbins sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR); 596e5996857STim J. Robbins if (wc == BADCHAR) 597e5996857STim J. Robbins return (NULL); 59858f0484fSRodney W. Grimes break; 59958f0484fSRodney W. Grimes case OANYOF: 600e5996857STim J. Robbins if (sp == stop) 601e5996857STim J. Robbins return (NULL); 60258f0484fSRodney W. Grimes cs = &m->g->sets[OPND(s)]; 603e5996857STim J. Robbins sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR); 604e5996857STim J. Robbins if (wc == BADCHAR || !CHIN(cs, wc)) 60558f0484fSRodney W. Grimes return(NULL); 60658f0484fSRodney W. Grimes break; 60758f0484fSRodney W. Grimes case OBOL: 60858f0484fSRodney W. Grimes if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) || 60958f0484fSRodney W. Grimes (sp < m->endp && *(sp-1) == '\n' && 61058f0484fSRodney W. Grimes (m->g->cflags®_NEWLINE)) ) 61158f0484fSRodney W. Grimes { /* yes */ } 61258f0484fSRodney W. Grimes else 61358f0484fSRodney W. Grimes return(NULL); 61458f0484fSRodney W. Grimes break; 61558f0484fSRodney W. Grimes case OEOL: 61658f0484fSRodney W. Grimes if ( (sp == m->endp && !(m->eflags®_NOTEOL)) || 61758f0484fSRodney W. Grimes (sp < m->endp && *sp == '\n' && 61858f0484fSRodney W. Grimes (m->g->cflags®_NEWLINE)) ) 61958f0484fSRodney W. Grimes { /* yes */ } 62058f0484fSRodney W. Grimes else 62158f0484fSRodney W. Grimes return(NULL); 62258f0484fSRodney W. Grimes break; 62358f0484fSRodney W. Grimes case OBOW: 62458f0484fSRodney W. Grimes if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) || 62558f0484fSRodney W. Grimes (sp < m->endp && *(sp-1) == '\n' && 62658f0484fSRodney W. Grimes (m->g->cflags®_NEWLINE)) || 62758f0484fSRodney W. Grimes (sp > m->beginp && 62858f0484fSRodney W. Grimes !ISWORD(*(sp-1))) ) && 62958f0484fSRodney W. Grimes (sp < m->endp && ISWORD(*sp)) ) 63058f0484fSRodney W. Grimes { /* yes */ } 63158f0484fSRodney W. Grimes else 63258f0484fSRodney W. Grimes return(NULL); 63358f0484fSRodney W. Grimes break; 63458f0484fSRodney W. Grimes case OEOW: 63558f0484fSRodney W. Grimes if (( (sp == m->endp && !(m->eflags®_NOTEOL)) || 63658f0484fSRodney W. Grimes (sp < m->endp && *sp == '\n' && 63758f0484fSRodney W. Grimes (m->g->cflags®_NEWLINE)) || 63858f0484fSRodney W. Grimes (sp < m->endp && !ISWORD(*sp)) ) && 63958f0484fSRodney W. Grimes (sp > m->beginp && ISWORD(*(sp-1))) ) 64058f0484fSRodney W. Grimes { /* yes */ } 64158f0484fSRodney W. Grimes else 64258f0484fSRodney W. Grimes return(NULL); 64358f0484fSRodney W. Grimes break; 64458f0484fSRodney W. Grimes case O_QUEST: 64558f0484fSRodney W. Grimes break; 64658f0484fSRodney W. Grimes case OOR1: /* matches null but needs to skip */ 64758f0484fSRodney W. Grimes ss++; 64858f0484fSRodney W. Grimes s = m->g->strip[ss]; 64958f0484fSRodney W. Grimes do { 65058f0484fSRodney W. Grimes assert(OP(s) == OOR2); 65158f0484fSRodney W. Grimes ss += OPND(s); 65258f0484fSRodney W. Grimes } while (OP(s = m->g->strip[ss]) != O_CH); 65358f0484fSRodney W. Grimes /* note that the ss++ gets us past the O_CH */ 65458f0484fSRodney W. Grimes break; 65558f0484fSRodney W. Grimes default: /* have to make a choice */ 65658f0484fSRodney W. Grimes hard = 1; 65758f0484fSRodney W. Grimes break; 65858f0484fSRodney W. Grimes } 65958f0484fSRodney W. Grimes if (!hard) { /* that was it! */ 66058f0484fSRodney W. Grimes if (sp != stop) 66158f0484fSRodney W. Grimes return(NULL); 66258f0484fSRodney W. Grimes return(sp); 66358f0484fSRodney W. Grimes } 66458f0484fSRodney W. Grimes ss--; /* adjust for the for's final increment */ 66558f0484fSRodney W. Grimes 66658f0484fSRodney W. Grimes /* the hard stuff */ 66758f0484fSRodney W. Grimes AT("hard", sp, stop, ss, stopst); 66858f0484fSRodney W. Grimes s = m->g->strip[ss]; 66958f0484fSRodney W. Grimes switch (OP(s)) { 67058f0484fSRodney W. Grimes case OBACK_: /* the vilest depths */ 67158f0484fSRodney W. Grimes i = OPND(s); 67258f0484fSRodney W. Grimes assert(0 < i && i <= m->g->nsub); 67358f0484fSRodney W. Grimes if (m->pmatch[i].rm_eo == -1) 67458f0484fSRodney W. Grimes return(NULL); 67558f0484fSRodney W. Grimes assert(m->pmatch[i].rm_so != -1); 67658f0484fSRodney W. Grimes len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so; 6770f4481c5SXin LI if (len == 0) 6780f4481c5SXin LI return(NULL); 67958f0484fSRodney W. Grimes assert(stop - m->beginp >= len); 68058f0484fSRodney W. Grimes if (sp > stop - len) 68158f0484fSRodney W. Grimes return(NULL); /* not enough left to match */ 68258f0484fSRodney W. Grimes ssp = m->offp + m->pmatch[i].rm_so; 68358f0484fSRodney W. Grimes if (memcmp(sp, ssp, len) != 0) 68458f0484fSRodney W. Grimes return(NULL); 68558f0484fSRodney W. Grimes while (m->g->strip[ss] != SOP(O_BACK, i)) 68658f0484fSRodney W. Grimes ss++; 68758f0484fSRodney W. Grimes return(backref(m, sp+len, stop, ss+1, stopst, lev)); 68858f0484fSRodney W. Grimes break; 68958f0484fSRodney W. Grimes case OQUEST_: /* to null or not */ 69058f0484fSRodney W. Grimes dp = backref(m, sp, stop, ss+1, stopst, lev); 69158f0484fSRodney W. Grimes if (dp != NULL) 69258f0484fSRodney W. Grimes return(dp); /* not */ 69358f0484fSRodney W. Grimes return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev)); 69458f0484fSRodney W. Grimes break; 69558f0484fSRodney W. Grimes case OPLUS_: 69658f0484fSRodney W. Grimes assert(m->lastpos != NULL); 69758f0484fSRodney W. Grimes assert(lev+1 <= m->g->nplus); 69858f0484fSRodney W. Grimes m->lastpos[lev+1] = sp; 69958f0484fSRodney W. Grimes return(backref(m, sp, stop, ss+1, stopst, lev+1)); 70058f0484fSRodney W. Grimes break; 70158f0484fSRodney W. Grimes case O_PLUS: 70258f0484fSRodney W. Grimes if (sp == m->lastpos[lev]) /* last pass matched null */ 70358f0484fSRodney W. Grimes return(backref(m, sp, stop, ss+1, stopst, lev-1)); 70458f0484fSRodney W. Grimes /* try another pass */ 70558f0484fSRodney W. Grimes m->lastpos[lev] = sp; 70658f0484fSRodney W. Grimes dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev); 70758f0484fSRodney W. Grimes if (dp == NULL) 70858f0484fSRodney W. Grimes return(backref(m, sp, stop, ss+1, stopst, lev-1)); 70958f0484fSRodney W. Grimes else 71058f0484fSRodney W. Grimes return(dp); 71158f0484fSRodney W. Grimes break; 71258f0484fSRodney W. Grimes case OCH_: /* find the right one, if any */ 71358f0484fSRodney W. Grimes ssub = ss + 1; 71458f0484fSRodney W. Grimes esub = ss + OPND(s) - 1; 71558f0484fSRodney W. Grimes assert(OP(m->g->strip[esub]) == OOR1); 71658f0484fSRodney W. Grimes for (;;) { /* find first matching branch */ 71758f0484fSRodney W. Grimes dp = backref(m, sp, stop, ssub, esub, lev); 71858f0484fSRodney W. Grimes if (dp != NULL) 71958f0484fSRodney W. Grimes return(dp); 72058f0484fSRodney W. Grimes /* that one missed, try next one */ 72158f0484fSRodney W. Grimes if (OP(m->g->strip[esub]) == O_CH) 72258f0484fSRodney W. Grimes return(NULL); /* there is none */ 72358f0484fSRodney W. Grimes esub++; 72458f0484fSRodney W. Grimes assert(OP(m->g->strip[esub]) == OOR2); 72558f0484fSRodney W. Grimes ssub = esub + 1; 72658f0484fSRodney W. Grimes esub += OPND(m->g->strip[esub]); 72758f0484fSRodney W. Grimes if (OP(m->g->strip[esub]) == OOR2) 72858f0484fSRodney W. Grimes esub--; 72958f0484fSRodney W. Grimes else 73058f0484fSRodney W. Grimes assert(OP(m->g->strip[esub]) == O_CH); 73158f0484fSRodney W. Grimes } 73258f0484fSRodney W. Grimes break; 73358f0484fSRodney W. Grimes case OLPAREN: /* must undo assignment if rest fails */ 73458f0484fSRodney W. Grimes i = OPND(s); 73558f0484fSRodney W. Grimes assert(0 < i && i <= m->g->nsub); 73658f0484fSRodney W. Grimes offsave = m->pmatch[i].rm_so; 73758f0484fSRodney W. Grimes m->pmatch[i].rm_so = sp - m->offp; 73858f0484fSRodney W. Grimes dp = backref(m, sp, stop, ss+1, stopst, lev); 73958f0484fSRodney W. Grimes if (dp != NULL) 74058f0484fSRodney W. Grimes return(dp); 74158f0484fSRodney W. Grimes m->pmatch[i].rm_so = offsave; 74258f0484fSRodney W. Grimes return(NULL); 74358f0484fSRodney W. Grimes break; 74458f0484fSRodney W. Grimes case ORPAREN: /* must undo assignment if rest fails */ 74558f0484fSRodney W. Grimes i = OPND(s); 74658f0484fSRodney W. Grimes assert(0 < i && i <= m->g->nsub); 74758f0484fSRodney W. Grimes offsave = m->pmatch[i].rm_eo; 74858f0484fSRodney W. Grimes m->pmatch[i].rm_eo = sp - m->offp; 74958f0484fSRodney W. Grimes dp = backref(m, sp, stop, ss+1, stopst, lev); 75058f0484fSRodney W. Grimes if (dp != NULL) 75158f0484fSRodney W. Grimes return(dp); 75258f0484fSRodney W. Grimes m->pmatch[i].rm_eo = offsave; 75358f0484fSRodney W. Grimes return(NULL); 75458f0484fSRodney W. Grimes break; 75558f0484fSRodney W. Grimes default: /* uh oh */ 75658f0484fSRodney W. Grimes assert(nope); 75758f0484fSRodney W. Grimes break; 75858f0484fSRodney W. Grimes } 75958f0484fSRodney W. Grimes 76058f0484fSRodney W. Grimes /* "can't happen" */ 76158f0484fSRodney W. Grimes assert(nope); 76258f0484fSRodney W. Grimes /* NOTREACHED */ 76316252f11SPoul-Henning Kamp return "shut up gcc"; 76458f0484fSRodney W. Grimes } 76558f0484fSRodney W. Grimes 76658f0484fSRodney W. Grimes /* 76758f0484fSRodney W. Grimes - fast - step through the string at top speed 7688fb3f3f6SDavid E. O'Brien == static char *fast(struct match *m, char *start, \ 76958f0484fSRodney W. Grimes == char *stop, sopno startst, sopno stopst); 77058f0484fSRodney W. Grimes */ 77158f0484fSRodney W. Grimes static char * /* where tentative match ended, or NULL */ 77258f0484fSRodney W. Grimes fast(m, start, stop, startst, stopst) 7738fb3f3f6SDavid E. O'Brien struct match *m; 77458f0484fSRodney W. Grimes char *start; 77558f0484fSRodney W. Grimes char *stop; 77658f0484fSRodney W. Grimes sopno startst; 77758f0484fSRodney W. Grimes sopno stopst; 77858f0484fSRodney W. Grimes { 7798fb3f3f6SDavid E. O'Brien states st = m->st; 7808fb3f3f6SDavid E. O'Brien states fresh = m->fresh; 7818fb3f3f6SDavid E. O'Brien states tmp = m->tmp; 7828fb3f3f6SDavid E. O'Brien char *p = start; 783e5996857STim J. Robbins wint_t c; 784e5996857STim J. Robbins wint_t lastc; /* previous c */ 785e5996857STim J. Robbins wint_t flagch; 7868fb3f3f6SDavid E. O'Brien int i; 7878fb3f3f6SDavid E. O'Brien char *coldp; /* last p after which no match was underway */ 788e5996857STim J. Robbins size_t clen; 78958f0484fSRodney W. Grimes 79058f0484fSRodney W. Grimes CLEAR(st); 79158f0484fSRodney W. Grimes SET1(st, startst); 79258f0484fSRodney W. Grimes st = step(m->g, startst, stopst, st, NOTHING, st); 79358f0484fSRodney W. Grimes ASSIGN(fresh, st); 79458f0484fSRodney W. Grimes SP("start", st, *p); 79558f0484fSRodney W. Grimes coldp = NULL; 796e5996857STim J. Robbins if (start == m->beginp) 797e5996857STim J. Robbins c = OUT; 798e5996857STim J. Robbins else { 799e5996857STim J. Robbins /* 800e5996857STim J. Robbins * XXX Wrong if the previous character was multi-byte. 801e5996857STim J. Robbins * Newline never is (in encodings supported by FreeBSD), 802e5996857STim J. Robbins * so this only breaks the ISWORD tests below. 803e5996857STim J. Robbins */ 804e5996857STim J. Robbins c = (uch)*(start - 1); 805e5996857STim J. Robbins } 80658f0484fSRodney W. Grimes for (;;) { 80758f0484fSRodney W. Grimes /* next character */ 80858f0484fSRodney W. Grimes lastc = c; 8091ee0dbeeSTim J. Robbins if (p == m->endp) { 8101ee0dbeeSTim J. Robbins clen = 0; 811e5996857STim J. Robbins c = OUT; 8121ee0dbeeSTim J. Robbins } else 8131ee0dbeeSTim J. Robbins clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR); 81458f0484fSRodney W. Grimes if (EQ(st, fresh)) 81558f0484fSRodney W. Grimes coldp = p; 81658f0484fSRodney W. Grimes 81758f0484fSRodney W. Grimes /* is there an EOL and/or BOL between lastc and c? */ 81858f0484fSRodney W. Grimes flagch = '\0'; 81958f0484fSRodney W. Grimes i = 0; 82058f0484fSRodney W. Grimes if ( (lastc == '\n' && m->g->cflags®_NEWLINE) || 82158f0484fSRodney W. Grimes (lastc == OUT && !(m->eflags®_NOTBOL)) ) { 82258f0484fSRodney W. Grimes flagch = BOL; 82358f0484fSRodney W. Grimes i = m->g->nbol; 82458f0484fSRodney W. Grimes } 82558f0484fSRodney W. Grimes if ( (c == '\n' && m->g->cflags®_NEWLINE) || 82658f0484fSRodney W. Grimes (c == OUT && !(m->eflags®_NOTEOL)) ) { 82758f0484fSRodney W. Grimes flagch = (flagch == BOL) ? BOLEOL : EOL; 82858f0484fSRodney W. Grimes i += m->g->neol; 82958f0484fSRodney W. Grimes } 83058f0484fSRodney W. Grimes if (i != 0) { 83158f0484fSRodney W. Grimes for (; i > 0; i--) 83258f0484fSRodney W. Grimes st = step(m->g, startst, stopst, st, flagch, st); 83358f0484fSRodney W. Grimes SP("boleol", st, c); 83458f0484fSRodney W. Grimes } 83558f0484fSRodney W. Grimes 83658f0484fSRodney W. Grimes /* how about a word boundary? */ 83758f0484fSRodney W. Grimes if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) && 83858f0484fSRodney W. Grimes (c != OUT && ISWORD(c)) ) { 83958f0484fSRodney W. Grimes flagch = BOW; 84058f0484fSRodney W. Grimes } 84158f0484fSRodney W. Grimes if ( (lastc != OUT && ISWORD(lastc)) && 84258f0484fSRodney W. Grimes (flagch == EOL || (c != OUT && !ISWORD(c))) ) { 84358f0484fSRodney W. Grimes flagch = EOW; 84458f0484fSRodney W. Grimes } 84558f0484fSRodney W. Grimes if (flagch == BOW || flagch == EOW) { 84658f0484fSRodney W. Grimes st = step(m->g, startst, stopst, st, flagch, st); 84758f0484fSRodney W. Grimes SP("boweow", st, c); 84858f0484fSRodney W. Grimes } 84958f0484fSRodney W. Grimes 85058f0484fSRodney W. Grimes /* are we done? */ 8511ee0dbeeSTim J. Robbins if (ISSET(st, stopst) || p == stop || clen > stop - p) 85258f0484fSRodney W. Grimes break; /* NOTE BREAK OUT */ 85358f0484fSRodney W. Grimes 85458f0484fSRodney W. Grimes /* no, we must deal with this character */ 85558f0484fSRodney W. Grimes ASSIGN(tmp, st); 85658f0484fSRodney W. Grimes ASSIGN(st, fresh); 85758f0484fSRodney W. Grimes assert(c != OUT); 85858f0484fSRodney W. Grimes st = step(m->g, startst, stopst, tmp, c, st); 85958f0484fSRodney W. Grimes SP("aft", st, c); 86058f0484fSRodney W. Grimes assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st)); 861e5996857STim J. Robbins p += clen; 86258f0484fSRodney W. Grimes } 86358f0484fSRodney W. Grimes 86458f0484fSRodney W. Grimes assert(coldp != NULL); 86558f0484fSRodney W. Grimes m->coldp = coldp; 86658f0484fSRodney W. Grimes if (ISSET(st, stopst)) 867bd9643b1STim J. Robbins return(p+XMBRTOWC(NULL, p, stop - p, &m->mbs, 0)); 86858f0484fSRodney W. Grimes else 86958f0484fSRodney W. Grimes return(NULL); 87058f0484fSRodney W. Grimes } 87158f0484fSRodney W. Grimes 87258f0484fSRodney W. Grimes /* 87358f0484fSRodney W. Grimes - slow - step through the string more deliberately 8748fb3f3f6SDavid E. O'Brien == static char *slow(struct match *m, char *start, \ 87558f0484fSRodney W. Grimes == char *stop, sopno startst, sopno stopst); 87658f0484fSRodney W. Grimes */ 87758f0484fSRodney W. Grimes static char * /* where it ended */ 87858f0484fSRodney W. Grimes slow(m, start, stop, startst, stopst) 8798fb3f3f6SDavid E. O'Brien struct match *m; 88058f0484fSRodney W. Grimes char *start; 88158f0484fSRodney W. Grimes char *stop; 88258f0484fSRodney W. Grimes sopno startst; 88358f0484fSRodney W. Grimes sopno stopst; 88458f0484fSRodney W. Grimes { 8858fb3f3f6SDavid E. O'Brien states st = m->st; 8868fb3f3f6SDavid E. O'Brien states empty = m->empty; 8878fb3f3f6SDavid E. O'Brien states tmp = m->tmp; 8888fb3f3f6SDavid E. O'Brien char *p = start; 889e5996857STim J. Robbins wint_t c; 890e5996857STim J. Robbins wint_t lastc; /* previous c */ 891e5996857STim J. Robbins wint_t flagch; 8928fb3f3f6SDavid E. O'Brien int i; 8938fb3f3f6SDavid E. O'Brien char *matchp; /* last p at which a match ended */ 894e5996857STim J. Robbins size_t clen; 89558f0484fSRodney W. Grimes 89658f0484fSRodney W. Grimes AT("slow", start, stop, startst, stopst); 89758f0484fSRodney W. Grimes CLEAR(st); 89858f0484fSRodney W. Grimes SET1(st, startst); 89958f0484fSRodney W. Grimes SP("sstart", st, *p); 90058f0484fSRodney W. Grimes st = step(m->g, startst, stopst, st, NOTHING, st); 90158f0484fSRodney W. Grimes matchp = NULL; 902e5996857STim J. Robbins if (start == m->beginp) 903e5996857STim J. Robbins c = OUT; 904e5996857STim J. Robbins else { 905e5996857STim J. Robbins /* 906e5996857STim J. Robbins * XXX Wrong if the previous character was multi-byte. 907e5996857STim J. Robbins * Newline never is (in encodings supported by FreeBSD), 908e5996857STim J. Robbins * so this only breaks the ISWORD tests below. 909e5996857STim J. Robbins */ 910e5996857STim J. Robbins c = (uch)*(start - 1); 911e5996857STim J. Robbins } 91258f0484fSRodney W. Grimes for (;;) { 91358f0484fSRodney W. Grimes /* next character */ 91458f0484fSRodney W. Grimes lastc = c; 915e5996857STim J. Robbins if (p == m->endp) { 916e5996857STim J. Robbins c = OUT; 917e5996857STim J. Robbins clen = 0; 918e5996857STim J. Robbins } else 9191ee0dbeeSTim J. Robbins clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR); 92058f0484fSRodney W. Grimes 92158f0484fSRodney W. Grimes /* is there an EOL and/or BOL between lastc and c? */ 92258f0484fSRodney W. Grimes flagch = '\0'; 92358f0484fSRodney W. Grimes i = 0; 92458f0484fSRodney W. Grimes if ( (lastc == '\n' && m->g->cflags®_NEWLINE) || 92558f0484fSRodney W. Grimes (lastc == OUT && !(m->eflags®_NOTBOL)) ) { 92658f0484fSRodney W. Grimes flagch = BOL; 92758f0484fSRodney W. Grimes i = m->g->nbol; 92858f0484fSRodney W. Grimes } 92958f0484fSRodney W. Grimes if ( (c == '\n' && m->g->cflags®_NEWLINE) || 93058f0484fSRodney W. Grimes (c == OUT && !(m->eflags®_NOTEOL)) ) { 93158f0484fSRodney W. Grimes flagch = (flagch == BOL) ? BOLEOL : EOL; 93258f0484fSRodney W. Grimes i += m->g->neol; 93358f0484fSRodney W. Grimes } 93458f0484fSRodney W. Grimes if (i != 0) { 93558f0484fSRodney W. Grimes for (; i > 0; i--) 93658f0484fSRodney W. Grimes st = step(m->g, startst, stopst, st, flagch, st); 93758f0484fSRodney W. Grimes SP("sboleol", st, c); 93858f0484fSRodney W. Grimes } 93958f0484fSRodney W. Grimes 94058f0484fSRodney W. Grimes /* how about a word boundary? */ 94158f0484fSRodney W. Grimes if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) && 94258f0484fSRodney W. Grimes (c != OUT && ISWORD(c)) ) { 94358f0484fSRodney W. Grimes flagch = BOW; 94458f0484fSRodney W. Grimes } 94558f0484fSRodney W. Grimes if ( (lastc != OUT && ISWORD(lastc)) && 94658f0484fSRodney W. Grimes (flagch == EOL || (c != OUT && !ISWORD(c))) ) { 94758f0484fSRodney W. Grimes flagch = EOW; 94858f0484fSRodney W. Grimes } 94958f0484fSRodney W. Grimes if (flagch == BOW || flagch == EOW) { 95058f0484fSRodney W. Grimes st = step(m->g, startst, stopst, st, flagch, st); 95158f0484fSRodney W. Grimes SP("sboweow", st, c); 95258f0484fSRodney W. Grimes } 95358f0484fSRodney W. Grimes 95458f0484fSRodney W. Grimes /* are we done? */ 95558f0484fSRodney W. Grimes if (ISSET(st, stopst)) 95658f0484fSRodney W. Grimes matchp = p; 9571ee0dbeeSTim J. Robbins if (EQ(st, empty) || p == stop || clen > stop - p) 95858f0484fSRodney W. Grimes break; /* NOTE BREAK OUT */ 95958f0484fSRodney W. Grimes 96058f0484fSRodney W. Grimes /* no, we must deal with this character */ 96158f0484fSRodney W. Grimes ASSIGN(tmp, st); 96258f0484fSRodney W. Grimes ASSIGN(st, empty); 96358f0484fSRodney W. Grimes assert(c != OUT); 96458f0484fSRodney W. Grimes st = step(m->g, startst, stopst, tmp, c, st); 96558f0484fSRodney W. Grimes SP("saft", st, c); 96658f0484fSRodney W. Grimes assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st)); 967e5996857STim J. Robbins p += clen; 96858f0484fSRodney W. Grimes } 96958f0484fSRodney W. Grimes 97058f0484fSRodney W. Grimes return(matchp); 97158f0484fSRodney W. Grimes } 97258f0484fSRodney W. Grimes 97358f0484fSRodney W. Grimes 97458f0484fSRodney W. Grimes /* 97558f0484fSRodney W. Grimes - step - map set of states reachable before char to set reachable after 9768fb3f3f6SDavid E. O'Brien == static states step(struct re_guts *g, sopno start, sopno stop, \ 9778fb3f3f6SDavid E. O'Brien == states bef, int ch, states aft); 978e5996857STim J. Robbins == #define BOL (OUT-1) 979e5996857STim J. Robbins == #define EOL (BOL-1) 980e5996857STim J. Robbins == #define BOLEOL (BOL-2) 981e5996857STim J. Robbins == #define NOTHING (BOL-3) 982e5996857STim J. Robbins == #define BOW (BOL-4) 983e5996857STim J. Robbins == #define EOW (BOL-5) 984e5996857STim J. Robbins == #define BADCHAR (BOL-6) 985e5996857STim J. Robbins == #define NONCHAR(c) ((c) <= OUT) 98658f0484fSRodney W. Grimes */ 98758f0484fSRodney W. Grimes static states 98858f0484fSRodney W. Grimes step(g, start, stop, bef, ch, aft) 9898fb3f3f6SDavid E. O'Brien struct re_guts *g; 99058f0484fSRodney W. Grimes sopno start; /* start state within strip */ 99158f0484fSRodney W. Grimes sopno stop; /* state after stop state within strip */ 9928fb3f3f6SDavid E. O'Brien states bef; /* states reachable before */ 993e5996857STim J. Robbins wint_t ch; /* character or NONCHAR code */ 9948fb3f3f6SDavid E. O'Brien states aft; /* states already known reachable after */ 99558f0484fSRodney W. Grimes { 9968fb3f3f6SDavid E. O'Brien cset *cs; 9978fb3f3f6SDavid E. O'Brien sop s; 9988fb3f3f6SDavid E. O'Brien sopno pc; 9998fb3f3f6SDavid E. O'Brien onestate here; /* note, macros know this name */ 10008fb3f3f6SDavid E. O'Brien sopno look; 10018fb3f3f6SDavid E. O'Brien int i; 100258f0484fSRodney W. Grimes 100358f0484fSRodney W. Grimes for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) { 100458f0484fSRodney W. Grimes s = g->strip[pc]; 100558f0484fSRodney W. Grimes switch (OP(s)) { 100658f0484fSRodney W. Grimes case OEND: 100758f0484fSRodney W. Grimes assert(pc == stop-1); 100858f0484fSRodney W. Grimes break; 100958f0484fSRodney W. Grimes case OCHAR: 101058f0484fSRodney W. Grimes /* only characters can match */ 1011e5996857STim J. Robbins assert(!NONCHAR(ch) || ch != OPND(s)); 1012e5996857STim J. Robbins if (ch == OPND(s)) 101358f0484fSRodney W. Grimes FWD(aft, bef, 1); 101458f0484fSRodney W. Grimes break; 101558f0484fSRodney W. Grimes case OBOL: 101658f0484fSRodney W. Grimes if (ch == BOL || ch == BOLEOL) 101758f0484fSRodney W. Grimes FWD(aft, bef, 1); 101858f0484fSRodney W. Grimes break; 101958f0484fSRodney W. Grimes case OEOL: 102058f0484fSRodney W. Grimes if (ch == EOL || ch == BOLEOL) 102158f0484fSRodney W. Grimes FWD(aft, bef, 1); 102258f0484fSRodney W. Grimes break; 102358f0484fSRodney W. Grimes case OBOW: 102458f0484fSRodney W. Grimes if (ch == BOW) 102558f0484fSRodney W. Grimes FWD(aft, bef, 1); 102658f0484fSRodney W. Grimes break; 102758f0484fSRodney W. Grimes case OEOW: 102858f0484fSRodney W. Grimes if (ch == EOW) 102958f0484fSRodney W. Grimes FWD(aft, bef, 1); 103058f0484fSRodney W. Grimes break; 103158f0484fSRodney W. Grimes case OANY: 103258f0484fSRodney W. Grimes if (!NONCHAR(ch)) 103358f0484fSRodney W. Grimes FWD(aft, bef, 1); 103458f0484fSRodney W. Grimes break; 103558f0484fSRodney W. Grimes case OANYOF: 103658f0484fSRodney W. Grimes cs = &g->sets[OPND(s)]; 103758f0484fSRodney W. Grimes if (!NONCHAR(ch) && CHIN(cs, ch)) 103858f0484fSRodney W. Grimes FWD(aft, bef, 1); 103958f0484fSRodney W. Grimes break; 104058f0484fSRodney W. Grimes case OBACK_: /* ignored here */ 104158f0484fSRodney W. Grimes case O_BACK: 104258f0484fSRodney W. Grimes FWD(aft, aft, 1); 104358f0484fSRodney W. Grimes break; 104458f0484fSRodney W. Grimes case OPLUS_: /* forward, this is just an empty */ 104558f0484fSRodney W. Grimes FWD(aft, aft, 1); 104658f0484fSRodney W. Grimes break; 104758f0484fSRodney W. Grimes case O_PLUS: /* both forward and back */ 104858f0484fSRodney W. Grimes FWD(aft, aft, 1); 104958f0484fSRodney W. Grimes i = ISSETBACK(aft, OPND(s)); 105058f0484fSRodney W. Grimes BACK(aft, aft, OPND(s)); 105158f0484fSRodney W. Grimes if (!i && ISSETBACK(aft, OPND(s))) { 105258f0484fSRodney W. Grimes /* oho, must reconsider loop body */ 105358f0484fSRodney W. Grimes pc -= OPND(s) + 1; 105458f0484fSRodney W. Grimes INIT(here, pc); 105558f0484fSRodney W. Grimes } 105658f0484fSRodney W. Grimes break; 105758f0484fSRodney W. Grimes case OQUEST_: /* two branches, both forward */ 105858f0484fSRodney W. Grimes FWD(aft, aft, 1); 105958f0484fSRodney W. Grimes FWD(aft, aft, OPND(s)); 106058f0484fSRodney W. Grimes break; 106158f0484fSRodney W. Grimes case O_QUEST: /* just an empty */ 106258f0484fSRodney W. Grimes FWD(aft, aft, 1); 106358f0484fSRodney W. Grimes break; 106458f0484fSRodney W. Grimes case OLPAREN: /* not significant here */ 106558f0484fSRodney W. Grimes case ORPAREN: 106658f0484fSRodney W. Grimes FWD(aft, aft, 1); 106758f0484fSRodney W. Grimes break; 106858f0484fSRodney W. Grimes case OCH_: /* mark the first two branches */ 106958f0484fSRodney W. Grimes FWD(aft, aft, 1); 107058f0484fSRodney W. Grimes assert(OP(g->strip[pc+OPND(s)]) == OOR2); 107158f0484fSRodney W. Grimes FWD(aft, aft, OPND(s)); 107258f0484fSRodney W. Grimes break; 107358f0484fSRodney W. Grimes case OOR1: /* done a branch, find the O_CH */ 107458f0484fSRodney W. Grimes if (ISSTATEIN(aft, here)) { 107558f0484fSRodney W. Grimes for (look = 1; 107658f0484fSRodney W. Grimes OP(s = g->strip[pc+look]) != O_CH; 107758f0484fSRodney W. Grimes look += OPND(s)) 107858f0484fSRodney W. Grimes assert(OP(s) == OOR2); 107958f0484fSRodney W. Grimes FWD(aft, aft, look); 108058f0484fSRodney W. Grimes } 108158f0484fSRodney W. Grimes break; 108258f0484fSRodney W. Grimes case OOR2: /* propagate OCH_'s marking */ 108358f0484fSRodney W. Grimes FWD(aft, aft, 1); 108458f0484fSRodney W. Grimes if (OP(g->strip[pc+OPND(s)]) != O_CH) { 108558f0484fSRodney W. Grimes assert(OP(g->strip[pc+OPND(s)]) == OOR2); 108658f0484fSRodney W. Grimes FWD(aft, aft, OPND(s)); 108758f0484fSRodney W. Grimes } 108858f0484fSRodney W. Grimes break; 108958f0484fSRodney W. Grimes case O_CH: /* just empty */ 109058f0484fSRodney W. Grimes FWD(aft, aft, 1); 109158f0484fSRodney W. Grimes break; 109258f0484fSRodney W. Grimes default: /* ooooops... */ 109358f0484fSRodney W. Grimes assert(nope); 109458f0484fSRodney W. Grimes break; 109558f0484fSRodney W. Grimes } 109658f0484fSRodney W. Grimes } 109758f0484fSRodney W. Grimes 109858f0484fSRodney W. Grimes return(aft); 109958f0484fSRodney W. Grimes } 110058f0484fSRodney W. Grimes 110158f0484fSRodney W. Grimes #ifdef REDEBUG 110258f0484fSRodney W. Grimes /* 110358f0484fSRodney W. Grimes - print - print a set of states 110458f0484fSRodney W. Grimes == #ifdef REDEBUG 110558f0484fSRodney W. Grimes == static void print(struct match *m, char *caption, states st, \ 110658f0484fSRodney W. Grimes == int ch, FILE *d); 110758f0484fSRodney W. Grimes == #endif 110858f0484fSRodney W. Grimes */ 110958f0484fSRodney W. Grimes static void 111058f0484fSRodney W. Grimes print(m, caption, st, ch, d) 111158f0484fSRodney W. Grimes struct match *m; 111258f0484fSRodney W. Grimes char *caption; 111358f0484fSRodney W. Grimes states st; 111458f0484fSRodney W. Grimes int ch; 111558f0484fSRodney W. Grimes FILE *d; 111658f0484fSRodney W. Grimes { 11178fb3f3f6SDavid E. O'Brien struct re_guts *g = m->g; 11188fb3f3f6SDavid E. O'Brien int i; 11198fb3f3f6SDavid E. O'Brien int first = 1; 112058f0484fSRodney W. Grimes 112158f0484fSRodney W. Grimes if (!(m->eflags®_TRACE)) 112258f0484fSRodney W. Grimes return; 112358f0484fSRodney W. Grimes 112458f0484fSRodney W. Grimes fprintf(d, "%s", caption); 112558f0484fSRodney W. Grimes if (ch != '\0') 112658f0484fSRodney W. Grimes fprintf(d, " %s", pchar(ch)); 112758f0484fSRodney W. Grimes for (i = 0; i < g->nstates; i++) 112858f0484fSRodney W. Grimes if (ISSET(st, i)) { 112958f0484fSRodney W. Grimes fprintf(d, "%s%d", (first) ? "\t" : ", ", i); 113058f0484fSRodney W. Grimes first = 0; 113158f0484fSRodney W. Grimes } 113258f0484fSRodney W. Grimes fprintf(d, "\n"); 113358f0484fSRodney W. Grimes } 113458f0484fSRodney W. Grimes 113558f0484fSRodney W. Grimes /* 113658f0484fSRodney W. Grimes - at - print current situation 113758f0484fSRodney W. Grimes == #ifdef REDEBUG 113858f0484fSRodney W. Grimes == static void at(struct match *m, char *title, char *start, char *stop, \ 113958f0484fSRodney W. Grimes == sopno startst, sopno stopst); 114058f0484fSRodney W. Grimes == #endif 114158f0484fSRodney W. Grimes */ 114258f0484fSRodney W. Grimes static void 114358f0484fSRodney W. Grimes at(m, title, start, stop, startst, stopst) 114458f0484fSRodney W. Grimes struct match *m; 114558f0484fSRodney W. Grimes char *title; 114658f0484fSRodney W. Grimes char *start; 114758f0484fSRodney W. Grimes char *stop; 114858f0484fSRodney W. Grimes sopno startst; 114958f0484fSRodney W. Grimes sopno stopst; 115058f0484fSRodney W. Grimes { 115158f0484fSRodney W. Grimes if (!(m->eflags®_TRACE)) 115258f0484fSRodney W. Grimes return; 115358f0484fSRodney W. Grimes 115458f0484fSRodney W. Grimes printf("%s %s-", title, pchar(*start)); 115558f0484fSRodney W. Grimes printf("%s ", pchar(*stop)); 115658f0484fSRodney W. Grimes printf("%ld-%ld\n", (long)startst, (long)stopst); 115758f0484fSRodney W. Grimes } 115858f0484fSRodney W. Grimes 115958f0484fSRodney W. Grimes #ifndef PCHARDONE 116058f0484fSRodney W. Grimes #define PCHARDONE /* never again */ 116158f0484fSRodney W. Grimes /* 116258f0484fSRodney W. Grimes - pchar - make a character printable 116358f0484fSRodney W. Grimes == #ifdef REDEBUG 116458f0484fSRodney W. Grimes == static char *pchar(int ch); 116558f0484fSRodney W. Grimes == #endif 116658f0484fSRodney W. Grimes * 116758f0484fSRodney W. Grimes * Is this identical to regchar() over in debug.c? Well, yes. But a 116858f0484fSRodney W. Grimes * duplicate here avoids having a debugging-capable regexec.o tied to 116958f0484fSRodney W. Grimes * a matching debug.o, and this is convenient. It all disappears in 117058f0484fSRodney W. Grimes * the non-debug compilation anyway, so it doesn't matter much. 117158f0484fSRodney W. Grimes */ 117258f0484fSRodney W. Grimes static char * /* -> representation */ 117358f0484fSRodney W. Grimes pchar(ch) 117458f0484fSRodney W. Grimes int ch; 117558f0484fSRodney W. Grimes { 117658f0484fSRodney W. Grimes static char pbuf[10]; 117758f0484fSRodney W. Grimes 1178b5363c4aSAndrey A. Chernov if (isprint((uch)ch) || ch == ' ') 117958f0484fSRodney W. Grimes sprintf(pbuf, "%c", ch); 118058f0484fSRodney W. Grimes else 118158f0484fSRodney W. Grimes sprintf(pbuf, "\\%o", ch); 118258f0484fSRodney W. Grimes return(pbuf); 118358f0484fSRodney W. Grimes } 118458f0484fSRodney W. Grimes #endif 118558f0484fSRodney W. Grimes #endif 118658f0484fSRodney W. Grimes 118758f0484fSRodney W. Grimes #undef matcher 118858f0484fSRodney W. Grimes #undef fast 118958f0484fSRodney W. Grimes #undef slow 119058f0484fSRodney W. Grimes #undef dissect 119158f0484fSRodney W. Grimes #undef backref 119258f0484fSRodney W. Grimes #undef step 119358f0484fSRodney W. Grimes #undef print 119458f0484fSRodney W. Grimes #undef at 119558f0484fSRodney W. Grimes #undef match 1196