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