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