engine.c revision 1.1 1 1.1 jtc /*
2 1.1 jtc * The matching engine and friends. This file is #included by regexec.c
3 1.1 jtc * after suitable #defines of a variety of macros used herein, so that
4 1.1 jtc * different state representations can be used without duplicating masses
5 1.1 jtc * of code.
6 1.1 jtc */
7 1.1 jtc
8 1.1 jtc #ifdef SNAMES
9 1.1 jtc #define matcher smatcher
10 1.1 jtc #define fast sfast
11 1.1 jtc #define slow sslow
12 1.1 jtc #define dissect sdissect
13 1.1 jtc #define backref sbackref
14 1.1 jtc #define step sstep
15 1.1 jtc #define print sprint
16 1.1 jtc #define at sat
17 1.1 jtc #define match smat
18 1.1 jtc #endif
19 1.1 jtc #ifdef LNAMES
20 1.1 jtc #define matcher lmatcher
21 1.1 jtc #define fast lfast
22 1.1 jtc #define slow lslow
23 1.1 jtc #define dissect ldissect
24 1.1 jtc #define backref lbackref
25 1.1 jtc #define step lstep
26 1.1 jtc #define print lprint
27 1.1 jtc #define at lat
28 1.1 jtc #define match lmat
29 1.1 jtc #endif
30 1.1 jtc
31 1.1 jtc /* another structure passed up and down to avoid zillions of parameters */
32 1.1 jtc struct match {
33 1.1 jtc struct re_guts *g;
34 1.1 jtc int eflags;
35 1.1 jtc regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
36 1.1 jtc char *offp; /* offsets work from here */
37 1.1 jtc char *beginp; /* start of string -- virtual NUL precedes */
38 1.1 jtc char *endp; /* end of string -- virtual NUL here */
39 1.1 jtc char *coldp; /* can be no match starting before here */
40 1.1 jtc char **lastpos; /* [nplus+1] */
41 1.1 jtc STATEVARS;
42 1.1 jtc states st; /* current states */
43 1.1 jtc states fresh; /* states for a fresh start */
44 1.1 jtc states tmp; /* temporary */
45 1.1 jtc states empty; /* empty set of states */
46 1.1 jtc };
47 1.1 jtc
48 1.1 jtc #include "engine.ih"
49 1.1 jtc
50 1.1 jtc #ifdef REDEBUG
51 1.1 jtc #define SP(t, s, c) print(m, t, s, c, stdout)
52 1.1 jtc #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
53 1.1 jtc #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
54 1.1 jtc #else
55 1.1 jtc #define SP(t, s, c) /* nothing */
56 1.1 jtc #define AT(t, p1, p2, s1, s2) /* nothing */
57 1.1 jtc #define NOTE(s) /* nothing */
58 1.1 jtc #endif
59 1.1 jtc
60 1.1 jtc /*
61 1.1 jtc - matcher - the actual matching engine
62 1.1 jtc == static int matcher(register struct re_guts *g, char *string, \
63 1.1 jtc == size_t nmatch, regmatch_t pmatch[], int eflags);
64 1.1 jtc */
65 1.1 jtc static int /* 0 success, REG_NOMATCH failure */
66 1.1 jtc matcher(g, string, nmatch, pmatch, eflags)
67 1.1 jtc register struct re_guts *g;
68 1.1 jtc char *string;
69 1.1 jtc size_t nmatch;
70 1.1 jtc regmatch_t pmatch[];
71 1.1 jtc int eflags;
72 1.1 jtc {
73 1.1 jtc register char *endp;
74 1.1 jtc register int i;
75 1.1 jtc struct match mv;
76 1.1 jtc register struct match *m = &mv;
77 1.1 jtc register char *dp;
78 1.1 jtc const register sopno gf = g->firststate+1; /* +1 for OEND */
79 1.1 jtc const register sopno gl = g->laststate;
80 1.1 jtc char *start;
81 1.1 jtc char *stop;
82 1.1 jtc
83 1.1 jtc /* simplify the situation where possible */
84 1.1 jtc if (g->cflags®_NOSUB)
85 1.1 jtc nmatch = 0;
86 1.1 jtc if (eflags®_STARTEND) {
87 1.1 jtc start = string + pmatch[0].rm_so;
88 1.1 jtc stop = string + pmatch[0].rm_eo;
89 1.1 jtc } else {
90 1.1 jtc start = string;
91 1.1 jtc stop = start + strlen(start);
92 1.1 jtc }
93 1.1 jtc if (stop < start)
94 1.1 jtc return(REG_INVARG);
95 1.1 jtc
96 1.1 jtc /* prescreening; this does wonders for this rather slow code */
97 1.1 jtc if (g->must != NULL) {
98 1.1 jtc for (dp = start; dp < stop; dp++)
99 1.1 jtc if (*dp == g->must[0] && stop - dp >= g->mlen &&
100 1.1 jtc memcmp(dp, g->must, (size_t)g->mlen) == 0)
101 1.1 jtc break;
102 1.1 jtc if (dp == stop) /* we didn't find g->must */
103 1.1 jtc return(REG_NOMATCH);
104 1.1 jtc }
105 1.1 jtc
106 1.1 jtc /* match struct setup */
107 1.1 jtc m->g = g;
108 1.1 jtc m->eflags = eflags;
109 1.1 jtc m->pmatch = NULL;
110 1.1 jtc m->lastpos = NULL;
111 1.1 jtc m->offp = string;
112 1.1 jtc m->beginp = start;
113 1.1 jtc m->endp = stop;
114 1.1 jtc STATESETUP(m, 4);
115 1.1 jtc SETUP(m->st);
116 1.1 jtc SETUP(m->fresh);
117 1.1 jtc SETUP(m->tmp);
118 1.1 jtc SETUP(m->empty);
119 1.1 jtc CLEAR(m->empty);
120 1.1 jtc
121 1.1 jtc /* this loop does only one repetition except for backrefs */
122 1.1 jtc for (;;) {
123 1.1 jtc endp = fast(m, start, stop, gf, gl);
124 1.1 jtc if (endp == NULL) { /* a miss */
125 1.1 jtc STATETEARDOWN(m);
126 1.1 jtc return(REG_NOMATCH);
127 1.1 jtc }
128 1.1 jtc if (nmatch == 0 && !g->backrefs)
129 1.1 jtc break; /* no further info needed */
130 1.1 jtc
131 1.1 jtc /* where? */
132 1.1 jtc assert(m->coldp != NULL);
133 1.1 jtc for (;;) {
134 1.1 jtc NOTE("finding start");
135 1.1 jtc endp = slow(m, m->coldp, stop, gf, gl);
136 1.1 jtc if (endp != NULL)
137 1.1 jtc break;
138 1.1 jtc assert(m->coldp < m->endp);
139 1.1 jtc m->coldp++;
140 1.1 jtc }
141 1.1 jtc if (nmatch == 1 && !g->backrefs)
142 1.1 jtc break; /* no further info needed */
143 1.1 jtc
144 1.1 jtc /* oh my, he wants the subexpressions... */
145 1.1 jtc if (m->pmatch == NULL)
146 1.1 jtc m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
147 1.1 jtc sizeof(regmatch_t));
148 1.1 jtc if (m->pmatch == NULL) {
149 1.1 jtc STATETEARDOWN(m);
150 1.1 jtc return(REG_ESPACE);
151 1.1 jtc }
152 1.1 jtc for (i = 1; i <= m->g->nsub; i++)
153 1.1 jtc m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
154 1.1 jtc if (!g->backrefs && !(m->eflags®_BACKR)) {
155 1.1 jtc NOTE("dissecting");
156 1.1 jtc dp = dissect(m, m->coldp, endp, gf, gl);
157 1.1 jtc } else {
158 1.1 jtc if (g->nplus > 0 && m->lastpos == NULL)
159 1.1 jtc m->lastpos = (char **)malloc((g->nplus+1) *
160 1.1 jtc sizeof(char *));
161 1.1 jtc if (g->nplus > 0 && m->lastpos == NULL) {
162 1.1 jtc free(m->pmatch);
163 1.1 jtc STATETEARDOWN(m);
164 1.1 jtc return(REG_ESPACE);
165 1.1 jtc }
166 1.1 jtc NOTE("backref dissect");
167 1.1 jtc dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
168 1.1 jtc }
169 1.1 jtc if (dp != NULL)
170 1.1 jtc break;
171 1.1 jtc
172 1.1 jtc /* uh-oh... we couldn't find a subexpression-level match */
173 1.1 jtc assert(g->backrefs); /* must be back references doing it */
174 1.1 jtc assert(g->nplus == 0 || m->lastpos != NULL);
175 1.1 jtc for (;;) {
176 1.1 jtc if (dp != NULL || endp <= m->coldp)
177 1.1 jtc break; /* defeat */
178 1.1 jtc NOTE("backoff");
179 1.1 jtc endp = slow(m, m->coldp, endp-1, gf, gl);
180 1.1 jtc if (endp == NULL)
181 1.1 jtc break; /* defeat */
182 1.1 jtc /* try it on a shorter possibility */
183 1.1 jtc #ifndef NDEBUG
184 1.1 jtc for (i = 1; i <= m->g->nsub; i++) {
185 1.1 jtc assert(m->pmatch[i].rm_so == -1);
186 1.1 jtc assert(m->pmatch[i].rm_eo == -1);
187 1.1 jtc }
188 1.1 jtc #endif
189 1.1 jtc NOTE("backoff dissect");
190 1.1 jtc dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
191 1.1 jtc }
192 1.1 jtc assert(dp == NULL || dp == endp);
193 1.1 jtc if (dp != NULL) /* found a shorter one */
194 1.1 jtc break;
195 1.1 jtc
196 1.1 jtc /* despite initial appearances, there is no match here */
197 1.1 jtc NOTE("false alarm");
198 1.1 jtc start = m->coldp + 1; /* recycle starting later */
199 1.1 jtc assert(start <= stop);
200 1.1 jtc }
201 1.1 jtc
202 1.1 jtc /* fill in the details if requested */
203 1.1 jtc if (nmatch > 0) {
204 1.1 jtc pmatch[0].rm_so = m->coldp - m->offp;
205 1.1 jtc pmatch[0].rm_eo = endp - m->offp;
206 1.1 jtc }
207 1.1 jtc if (nmatch > 1) {
208 1.1 jtc assert(m->pmatch != NULL);
209 1.1 jtc for (i = 1; i < nmatch; i++)
210 1.1 jtc if (i <= m->g->nsub)
211 1.1 jtc pmatch[i] = m->pmatch[i];
212 1.1 jtc else {
213 1.1 jtc pmatch[i].rm_so = -1;
214 1.1 jtc pmatch[i].rm_eo = -1;
215 1.1 jtc }
216 1.1 jtc }
217 1.1 jtc
218 1.1 jtc if (m->pmatch != NULL)
219 1.1 jtc free((char *)m->pmatch);
220 1.1 jtc if (m->lastpos != NULL)
221 1.1 jtc free((char *)m->lastpos);
222 1.1 jtc STATETEARDOWN(m);
223 1.1 jtc return(0);
224 1.1 jtc }
225 1.1 jtc
226 1.1 jtc /*
227 1.1 jtc - dissect - figure out what matched what, no back references
228 1.1 jtc == static char *dissect(register struct match *m, char *start, \
229 1.1 jtc == char *stop, sopno startst, sopno stopst);
230 1.1 jtc */
231 1.1 jtc static char * /* == stop (success) always */
232 1.1 jtc dissect(m, start, stop, startst, stopst)
233 1.1 jtc register struct match *m;
234 1.1 jtc char *start;
235 1.1 jtc char *stop;
236 1.1 jtc sopno startst;
237 1.1 jtc sopno stopst;
238 1.1 jtc {
239 1.1 jtc register int i;
240 1.1 jtc register sopno ss; /* start sop of current subRE */
241 1.1 jtc register sopno es; /* end sop of current subRE */
242 1.1 jtc register char *sp; /* start of string matched by it */
243 1.1 jtc register char *stp; /* string matched by it cannot pass here */
244 1.1 jtc register char *rest; /* start of rest of string */
245 1.1 jtc register char *tail; /* string unmatched by rest of RE */
246 1.1 jtc register sopno ssub; /* start sop of subsubRE */
247 1.1 jtc register sopno esub; /* end sop of subsubRE */
248 1.1 jtc register char *ssp; /* start of string matched by subsubRE */
249 1.1 jtc register char *sep; /* end of string matched by subsubRE */
250 1.1 jtc register char *oldssp; /* previous ssp */
251 1.1 jtc register char *dp;
252 1.1 jtc
253 1.1 jtc AT("diss", start, stop, startst, stopst);
254 1.1 jtc sp = start;
255 1.1 jtc for (ss = startst; ss < stopst; ss = es) {
256 1.1 jtc /* identify end of subRE */
257 1.1 jtc es = ss;
258 1.1 jtc switch (OP(m->g->strip[es])) {
259 1.1 jtc case OPLUS_:
260 1.1 jtc case OQUEST_:
261 1.1 jtc es += OPND(m->g->strip[es]);
262 1.1 jtc break;
263 1.1 jtc case OCH_:
264 1.1 jtc while (OP(m->g->strip[es]) != O_CH)
265 1.1 jtc es += OPND(m->g->strip[es]);
266 1.1 jtc break;
267 1.1 jtc }
268 1.1 jtc es++;
269 1.1 jtc
270 1.1 jtc /* figure out what it matched */
271 1.1 jtc switch (OP(m->g->strip[ss])) {
272 1.1 jtc case OEND:
273 1.1 jtc assert(nope);
274 1.1 jtc break;
275 1.1 jtc case OCHAR:
276 1.1 jtc sp++;
277 1.1 jtc break;
278 1.1 jtc case OBOL:
279 1.1 jtc case OEOL:
280 1.1 jtc case OBOW:
281 1.1 jtc case OEOW:
282 1.1 jtc break;
283 1.1 jtc case OANY:
284 1.1 jtc case OANYOF:
285 1.1 jtc sp++;
286 1.1 jtc break;
287 1.1 jtc case OBACK_:
288 1.1 jtc case O_BACK:
289 1.1 jtc assert(nope);
290 1.1 jtc break;
291 1.1 jtc /* cases where length of match is hard to find */
292 1.1 jtc case OQUEST_:
293 1.1 jtc stp = stop;
294 1.1 jtc for (;;) {
295 1.1 jtc /* how long could this one be? */
296 1.1 jtc rest = slow(m, sp, stp, ss, es);
297 1.1 jtc assert(rest != NULL); /* it did match */
298 1.1 jtc /* could the rest match the rest? */
299 1.1 jtc tail = slow(m, rest, stop, es, stopst);
300 1.1 jtc if (tail == stop)
301 1.1 jtc break; /* yes! */
302 1.1 jtc /* no -- try a shorter match for this one */
303 1.1 jtc stp = rest - 1;
304 1.1 jtc assert(stp >= sp); /* it did work */
305 1.1 jtc }
306 1.1 jtc ssub = ss + 1;
307 1.1 jtc esub = es - 1;
308 1.1 jtc /* did innards match? */
309 1.1 jtc if (slow(m, sp, rest, ssub, esub) != NULL) {
310 1.1 jtc dp = dissect(m, sp, rest, ssub, esub);
311 1.1 jtc assert(dp == rest);
312 1.1 jtc } else /* no */
313 1.1 jtc assert(sp == rest);
314 1.1 jtc sp = rest;
315 1.1 jtc break;
316 1.1 jtc case OPLUS_:
317 1.1 jtc stp = stop;
318 1.1 jtc for (;;) {
319 1.1 jtc /* how long could this one be? */
320 1.1 jtc rest = slow(m, sp, stp, ss, es);
321 1.1 jtc assert(rest != NULL); /* it did match */
322 1.1 jtc /* could the rest match the rest? */
323 1.1 jtc tail = slow(m, rest, stop, es, stopst);
324 1.1 jtc if (tail == stop)
325 1.1 jtc break; /* yes! */
326 1.1 jtc /* no -- try a shorter match for this one */
327 1.1 jtc stp = rest - 1;
328 1.1 jtc assert(stp >= sp); /* it did work */
329 1.1 jtc }
330 1.1 jtc ssub = ss + 1;
331 1.1 jtc esub = es - 1;
332 1.1 jtc ssp = sp;
333 1.1 jtc oldssp = ssp;
334 1.1 jtc for (;;) { /* find last match of innards */
335 1.1 jtc sep = slow(m, ssp, rest, ssub, esub);
336 1.1 jtc if (sep == NULL || sep == ssp)
337 1.1 jtc break; /* failed or matched null */
338 1.1 jtc oldssp = ssp; /* on to next try */
339 1.1 jtc ssp = sep;
340 1.1 jtc }
341 1.1 jtc if (sep == NULL) {
342 1.1 jtc /* last successful match */
343 1.1 jtc sep = ssp;
344 1.1 jtc ssp = oldssp;
345 1.1 jtc }
346 1.1 jtc assert(sep == rest); /* must exhaust substring */
347 1.1 jtc assert(slow(m, ssp, sep, ssub, esub) == rest);
348 1.1 jtc dp = dissect(m, ssp, sep, ssub, esub);
349 1.1 jtc assert(dp == sep);
350 1.1 jtc sp = rest;
351 1.1 jtc break;
352 1.1 jtc case OCH_:
353 1.1 jtc stp = stop;
354 1.1 jtc for (;;) {
355 1.1 jtc /* how long could this one be? */
356 1.1 jtc rest = slow(m, sp, stp, ss, es);
357 1.1 jtc assert(rest != NULL); /* it did match */
358 1.1 jtc /* could the rest match the rest? */
359 1.1 jtc tail = slow(m, rest, stop, es, stopst);
360 1.1 jtc if (tail == stop)
361 1.1 jtc break; /* yes! */
362 1.1 jtc /* no -- try a shorter match for this one */
363 1.1 jtc stp = rest - 1;
364 1.1 jtc assert(stp >= sp); /* it did work */
365 1.1 jtc }
366 1.1 jtc ssub = ss + 1;
367 1.1 jtc esub = ss + OPND(m->g->strip[ss]) - 1;
368 1.1 jtc assert(OP(m->g->strip[esub]) == OOR1);
369 1.1 jtc for (;;) { /* find first matching branch */
370 1.1 jtc if (slow(m, sp, rest, ssub, esub) == rest)
371 1.1 jtc break; /* it matched all of it */
372 1.1 jtc /* that one missed, try next one */
373 1.1 jtc assert(OP(m->g->strip[esub]) == OOR1);
374 1.1 jtc esub++;
375 1.1 jtc assert(OP(m->g->strip[esub]) == OOR2);
376 1.1 jtc ssub = esub + 1;
377 1.1 jtc esub += OPND(m->g->strip[esub]);
378 1.1 jtc if (OP(m->g->strip[esub]) == OOR2)
379 1.1 jtc esub--;
380 1.1 jtc else
381 1.1 jtc assert(OP(m->g->strip[esub]) == O_CH);
382 1.1 jtc }
383 1.1 jtc dp = dissect(m, sp, rest, ssub, esub);
384 1.1 jtc assert(dp == rest);
385 1.1 jtc sp = rest;
386 1.1 jtc break;
387 1.1 jtc case O_PLUS:
388 1.1 jtc case O_QUEST:
389 1.1 jtc case OOR1:
390 1.1 jtc case OOR2:
391 1.1 jtc case O_CH:
392 1.1 jtc assert(nope);
393 1.1 jtc break;
394 1.1 jtc case OLPAREN:
395 1.1 jtc i = OPND(m->g->strip[ss]);
396 1.1 jtc m->pmatch[i].rm_so = sp - m->offp;
397 1.1 jtc break;
398 1.1 jtc case ORPAREN:
399 1.1 jtc i = OPND(m->g->strip[ss]);
400 1.1 jtc m->pmatch[i].rm_eo = sp - m->offp;
401 1.1 jtc break;
402 1.1 jtc default: /* uh oh */
403 1.1 jtc assert(nope);
404 1.1 jtc break;
405 1.1 jtc }
406 1.1 jtc }
407 1.1 jtc
408 1.1 jtc assert(sp == stop);
409 1.1 jtc return(sp);
410 1.1 jtc }
411 1.1 jtc
412 1.1 jtc /*
413 1.1 jtc - backref - figure out what matched what, figuring in back references
414 1.1 jtc == static char *backref(register struct match *m, char *start, \
415 1.1 jtc == char *stop, sopno startst, sopno stopst, sopno lev);
416 1.1 jtc */
417 1.1 jtc static char * /* == stop (success) or NULL (failure) */
418 1.1 jtc backref(m, start, stop, startst, stopst, lev)
419 1.1 jtc register struct match *m;
420 1.1 jtc char *start;
421 1.1 jtc char *stop;
422 1.1 jtc sopno startst;
423 1.1 jtc sopno stopst;
424 1.1 jtc sopno lev; /* PLUS nesting level */
425 1.1 jtc {
426 1.1 jtc register int i;
427 1.1 jtc register sopno ss; /* start sop of current subRE */
428 1.1 jtc register char *sp; /* start of string matched by it */
429 1.1 jtc register sopno ssub; /* start sop of subsubRE */
430 1.1 jtc register sopno esub; /* end sop of subsubRE */
431 1.1 jtc register char *ssp; /* start of string matched by subsubRE */
432 1.1 jtc register char *dp;
433 1.1 jtc register size_t len;
434 1.1 jtc register int hard;
435 1.1 jtc register sop s;
436 1.1 jtc register regoff_t offsave;
437 1.1 jtc register cset *cs;
438 1.1 jtc
439 1.1 jtc AT("back", start, stop, startst, stopst);
440 1.1 jtc sp = start;
441 1.1 jtc
442 1.1 jtc /* get as far as we can with easy stuff */
443 1.1 jtc hard = 0;
444 1.1 jtc for (ss = startst; !hard && ss < stopst; ss++)
445 1.1 jtc switch (OP(s = m->g->strip[ss])) {
446 1.1 jtc case OCHAR:
447 1.1 jtc if (sp == stop || *sp++ != (char)OPND(s))
448 1.1 jtc return(NULL);
449 1.1 jtc break;
450 1.1 jtc case OANY:
451 1.1 jtc if (sp == stop)
452 1.1 jtc return(NULL);
453 1.1 jtc sp++;
454 1.1 jtc break;
455 1.1 jtc case OANYOF:
456 1.1 jtc cs = &m->g->sets[OPND(s)];
457 1.1 jtc if (sp == stop || !CHIN(cs, *sp++))
458 1.1 jtc return(NULL);
459 1.1 jtc break;
460 1.1 jtc case OBOL:
461 1.1 jtc if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
462 1.1 jtc (sp < m->endp && *(sp-1) == '\n' &&
463 1.1 jtc (m->g->cflags®_NEWLINE)) )
464 1.1 jtc { /* yes */ }
465 1.1 jtc else
466 1.1 jtc return(NULL);
467 1.1 jtc break;
468 1.1 jtc case OEOL:
469 1.1 jtc if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
470 1.1 jtc (sp < m->endp && *sp == '\n' &&
471 1.1 jtc (m->g->cflags®_NEWLINE)) )
472 1.1 jtc { /* yes */ }
473 1.1 jtc else
474 1.1 jtc return(NULL);
475 1.1 jtc break;
476 1.1 jtc case OBOW:
477 1.1 jtc if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
478 1.1 jtc (sp < m->endp && *(sp-1) == '\n' &&
479 1.1 jtc (m->g->cflags®_NEWLINE)) ||
480 1.1 jtc (sp > m->beginp &&
481 1.1 jtc !isalnum(*(sp-1))) ) &&
482 1.1 jtc (sp < m->endp && isalnum(*sp)) )
483 1.1 jtc { /* yes */ }
484 1.1 jtc else
485 1.1 jtc return(NULL);
486 1.1 jtc break;
487 1.1 jtc case OEOW:
488 1.1 jtc if (( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
489 1.1 jtc (sp < m->endp && *sp == '\n' &&
490 1.1 jtc (m->g->cflags®_NEWLINE)) ||
491 1.1 jtc (sp < m->endp && !isalnum(*sp)) ) &&
492 1.1 jtc (sp > m->beginp && isalnum(*(sp-1))) )
493 1.1 jtc { /* yes */ }
494 1.1 jtc else
495 1.1 jtc return(NULL);
496 1.1 jtc break;
497 1.1 jtc case O_QUEST:
498 1.1 jtc break;
499 1.1 jtc case OOR1: /* matches null but needs to skip */
500 1.1 jtc ss++;
501 1.1 jtc s = m->g->strip[ss];
502 1.1 jtc do {
503 1.1 jtc assert(OP(s) == OOR2);
504 1.1 jtc ss += OPND(s);
505 1.1 jtc } while (OP(s = m->g->strip[ss]) != O_CH);
506 1.1 jtc /* note that the ss++ gets us past the O_CH */
507 1.1 jtc break;
508 1.1 jtc default: /* have to make a choice */
509 1.1 jtc hard = 1;
510 1.1 jtc break;
511 1.1 jtc }
512 1.1 jtc if (!hard) { /* that was it! */
513 1.1 jtc if (sp != stop)
514 1.1 jtc return(NULL);
515 1.1 jtc return(sp);
516 1.1 jtc }
517 1.1 jtc ss--; /* adjust for the for's final increment */
518 1.1 jtc
519 1.1 jtc /* the hard stuff */
520 1.1 jtc AT("hard", sp, stop, ss, stopst);
521 1.1 jtc s = m->g->strip[ss];
522 1.1 jtc switch (OP(s)) {
523 1.1 jtc case OBACK_: /* the vilest depths */
524 1.1 jtc i = OPND(s);
525 1.1 jtc if (m->pmatch[i].rm_eo == -1)
526 1.1 jtc return(NULL);
527 1.1 jtc assert(m->pmatch[i].rm_so != -1);
528 1.1 jtc len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
529 1.1 jtc assert(stop - m->beginp >= len);
530 1.1 jtc if (sp > stop - len)
531 1.1 jtc return(NULL); /* not enough left to match */
532 1.1 jtc ssp = m->offp + m->pmatch[i].rm_so;
533 1.1 jtc if (memcmp(sp, ssp, len) != 0)
534 1.1 jtc return(NULL);
535 1.1 jtc while (m->g->strip[ss] != SOP(O_BACK, i))
536 1.1 jtc ss++;
537 1.1 jtc return(backref(m, sp+len, stop, ss+1, stopst, lev));
538 1.1 jtc break;
539 1.1 jtc case OQUEST_: /* to null or not */
540 1.1 jtc dp = backref(m, sp, stop, ss+1, stopst, lev);
541 1.1 jtc if (dp != NULL)
542 1.1 jtc return(dp); /* not */
543 1.1 jtc return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
544 1.1 jtc break;
545 1.1 jtc case OPLUS_:
546 1.1 jtc assert(m->lastpos != NULL);
547 1.1 jtc assert(lev+1 <= m->g->nplus);
548 1.1 jtc m->lastpos[lev+1] = sp;
549 1.1 jtc return(backref(m, sp, stop, ss+1, stopst, lev+1));
550 1.1 jtc break;
551 1.1 jtc case O_PLUS:
552 1.1 jtc if (sp == m->lastpos[lev]) /* last pass matched null */
553 1.1 jtc return(backref(m, sp, stop, ss+1, stopst, lev-1));
554 1.1 jtc /* try another pass */
555 1.1 jtc m->lastpos[lev] = sp;
556 1.1 jtc dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
557 1.1 jtc if (dp == NULL)
558 1.1 jtc return(backref(m, sp, stop, ss+1, stopst, lev-1));
559 1.1 jtc else
560 1.1 jtc return(dp);
561 1.1 jtc break;
562 1.1 jtc case OCH_: /* find the right one, if any */
563 1.1 jtc ssub = ss + 1;
564 1.1 jtc esub = ss + OPND(s) - 1;
565 1.1 jtc assert(OP(m->g->strip[esub]) == OOR1);
566 1.1 jtc for (;;) { /* find first matching branch */
567 1.1 jtc dp = backref(m, sp, stop, ssub, esub, lev);
568 1.1 jtc if (dp != NULL)
569 1.1 jtc return(dp);
570 1.1 jtc /* that one missed, try next one */
571 1.1 jtc if (OP(m->g->strip[esub]) == O_CH)
572 1.1 jtc return(NULL); /* there is none */
573 1.1 jtc esub++;
574 1.1 jtc assert(OP(m->g->strip[esub]) == OOR2);
575 1.1 jtc ssub = esub + 1;
576 1.1 jtc esub += OPND(m->g->strip[esub]);
577 1.1 jtc if (OP(m->g->strip[esub]) == OOR2)
578 1.1 jtc esub--;
579 1.1 jtc else
580 1.1 jtc assert(OP(m->g->strip[esub]) == O_CH);
581 1.1 jtc }
582 1.1 jtc break;
583 1.1 jtc case OLPAREN: /* must undo assignment if rest fails */
584 1.1 jtc i = OPND(s);
585 1.1 jtc offsave = m->pmatch[i].rm_so;
586 1.1 jtc m->pmatch[i].rm_so = sp - m->offp;
587 1.1 jtc dp = backref(m, sp, stop, ss+1, stopst, lev);
588 1.1 jtc if (dp != NULL)
589 1.1 jtc return(dp);
590 1.1 jtc m->pmatch[i].rm_so = offsave;
591 1.1 jtc return(NULL);
592 1.1 jtc break;
593 1.1 jtc case ORPAREN: /* must undo assignment if rest fails */
594 1.1 jtc i = OPND(s);
595 1.1 jtc offsave = m->pmatch[i].rm_eo;
596 1.1 jtc m->pmatch[i].rm_eo = sp - m->offp;
597 1.1 jtc dp = backref(m, sp, stop, ss+1, stopst, lev);
598 1.1 jtc if (dp != NULL)
599 1.1 jtc return(dp);
600 1.1 jtc m->pmatch[i].rm_eo = offsave;
601 1.1 jtc return(NULL);
602 1.1 jtc break;
603 1.1 jtc default: /* uh oh */
604 1.1 jtc assert(nope);
605 1.1 jtc break;
606 1.1 jtc }
607 1.1 jtc
608 1.1 jtc /* "can't happen" */
609 1.1 jtc assert(nope);
610 1.1 jtc /* NOTREACHED */
611 1.1 jtc }
612 1.1 jtc
613 1.1 jtc /*
614 1.1 jtc - fast - step through the string at top speed
615 1.1 jtc == static char *fast(register struct match *m, char *start, \
616 1.1 jtc == char *stop, sopno startst, sopno stopst);
617 1.1 jtc */
618 1.1 jtc static char * /* where tentative match ended, or NULL */
619 1.1 jtc fast(m, start, stop, startst, stopst)
620 1.1 jtc register struct match *m;
621 1.1 jtc char *start;
622 1.1 jtc char *stop;
623 1.1 jtc sopno startst;
624 1.1 jtc sopno stopst;
625 1.1 jtc {
626 1.1 jtc register states st = m->st;
627 1.1 jtc register states fresh = m->fresh;
628 1.1 jtc register states tmp = m->tmp;
629 1.1 jtc register char *p = start;
630 1.1 jtc register int c = (start == m->beginp) ? OUT : *(start-1);
631 1.1 jtc register int lastc; /* previous c */
632 1.1 jtc register int flagch;
633 1.1 jtc register int i;
634 1.1 jtc register char *coldp; /* last p after which no match was underway */
635 1.1 jtc
636 1.1 jtc CLEAR(st);
637 1.1 jtc SET1(st, startst);
638 1.1 jtc st = step(m->g, startst, stopst, st, NOTHING, st);
639 1.1 jtc ASSIGN(fresh, st);
640 1.1 jtc SP("start", st, *p);
641 1.1 jtc coldp = NULL;
642 1.1 jtc for (;;) {
643 1.1 jtc /* next character */
644 1.1 jtc lastc = c;
645 1.1 jtc c = (p == m->endp) ? OUT : *p;
646 1.1 jtc if (EQ(st, fresh))
647 1.1 jtc coldp = p;
648 1.1 jtc
649 1.1 jtc /* is there an EOL and/or BOL between lastc and c? */
650 1.1 jtc flagch = '\0';
651 1.1 jtc i = 0;
652 1.1 jtc if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
653 1.1 jtc (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
654 1.1 jtc flagch = BOL;
655 1.1 jtc i = m->g->nbol;
656 1.1 jtc }
657 1.1 jtc if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
658 1.1 jtc (c == OUT && !(m->eflags®_NOTEOL)) ) {
659 1.1 jtc flagch = (flagch == BOL) ? BOLEOL : EOL;
660 1.1 jtc i += m->g->neol;
661 1.1 jtc }
662 1.1 jtc if (i != 0) {
663 1.1 jtc for (; i > 0; i--)
664 1.1 jtc st = step(m->g, startst, stopst, st, flagch, st);
665 1.1 jtc SP("boleol", st, c);
666 1.1 jtc }
667 1.1 jtc
668 1.1 jtc /* how about a word boundary? */
669 1.1 jtc if ( (flagch == BOL || (lastc != OUT && !isalnum(lastc))) &&
670 1.1 jtc (c != OUT && isalnum(c)) ) {
671 1.1 jtc flagch = BOW;
672 1.1 jtc }
673 1.1 jtc if ( (lastc != OUT && isalnum(lastc)) &&
674 1.1 jtc (flagch == EOL || (c != OUT && !isalnum(c))) ) {
675 1.1 jtc flagch = EOW;
676 1.1 jtc }
677 1.1 jtc if (flagch == BOW || flagch == EOW) {
678 1.1 jtc st = step(m->g, startst, stopst, st, flagch, st);
679 1.1 jtc SP("boweow", st, c);
680 1.1 jtc }
681 1.1 jtc
682 1.1 jtc /* are we done? */
683 1.1 jtc if (ISSET(st, stopst) || p == stop)
684 1.1 jtc break; /* NOTE BREAK OUT */
685 1.1 jtc
686 1.1 jtc /* no, we must deal with this character */
687 1.1 jtc ASSIGN(tmp, st);
688 1.1 jtc ASSIGN(st, fresh);
689 1.1 jtc assert(c != OUT);
690 1.1 jtc st = step(m->g, startst, stopst, tmp, c, st);
691 1.1 jtc SP("aft", st, c);
692 1.1 jtc assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
693 1.1 jtc p++;
694 1.1 jtc }
695 1.1 jtc
696 1.1 jtc assert(coldp != NULL);
697 1.1 jtc m->coldp = coldp;
698 1.1 jtc if (ISSET(st, stopst))
699 1.1 jtc return(p+1);
700 1.1 jtc else
701 1.1 jtc return(NULL);
702 1.1 jtc }
703 1.1 jtc
704 1.1 jtc /*
705 1.1 jtc - slow - step through the string more deliberately
706 1.1 jtc == static char *slow(register struct match *m, char *start, \
707 1.1 jtc == char *stop, sopno startst, sopno stopst);
708 1.1 jtc */
709 1.1 jtc static char * /* where it ended */
710 1.1 jtc slow(m, start, stop, startst, stopst)
711 1.1 jtc register struct match *m;
712 1.1 jtc char *start;
713 1.1 jtc char *stop;
714 1.1 jtc sopno startst;
715 1.1 jtc sopno stopst;
716 1.1 jtc {
717 1.1 jtc register states st = m->st;
718 1.1 jtc register states empty = m->empty;
719 1.1 jtc register states tmp = m->tmp;
720 1.1 jtc register char *p = start;
721 1.1 jtc register int c = (start == m->beginp) ? OUT : *(start-1);
722 1.1 jtc register int lastc; /* previous c */
723 1.1 jtc register int flagch;
724 1.1 jtc register int i;
725 1.1 jtc register char *matchp; /* last p at which a match ended */
726 1.1 jtc
727 1.1 jtc AT("slow", start, stop, startst, stopst);
728 1.1 jtc CLEAR(st);
729 1.1 jtc SET1(st, startst);
730 1.1 jtc SP("sstart", st, *p);
731 1.1 jtc st = step(m->g, startst, stopst, st, NOTHING, st);
732 1.1 jtc matchp = NULL;
733 1.1 jtc for (;;) {
734 1.1 jtc /* next character */
735 1.1 jtc lastc = c;
736 1.1 jtc c = (p == m->endp) ? OUT : *p;
737 1.1 jtc
738 1.1 jtc /* is there an EOL and/or BOL between lastc and c? */
739 1.1 jtc flagch = '\0';
740 1.1 jtc i = 0;
741 1.1 jtc if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
742 1.1 jtc (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
743 1.1 jtc flagch = BOL;
744 1.1 jtc i = m->g->nbol;
745 1.1 jtc }
746 1.1 jtc if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
747 1.1 jtc (c == OUT && !(m->eflags®_NOTEOL)) ) {
748 1.1 jtc flagch = (flagch == BOL) ? BOLEOL : EOL;
749 1.1 jtc i += m->g->neol;
750 1.1 jtc }
751 1.1 jtc if (i != 0) {
752 1.1 jtc for (; i > 0; i--)
753 1.1 jtc st = step(m->g, startst, stopst, st, flagch, st);
754 1.1 jtc SP("sboleol", st, c);
755 1.1 jtc }
756 1.1 jtc
757 1.1 jtc /* how about a word boundary? */
758 1.1 jtc if ( (flagch == BOL || (lastc != OUT && !isalnum(lastc))) &&
759 1.1 jtc (c != OUT && isalnum(c)) ) {
760 1.1 jtc flagch = BOW;
761 1.1 jtc }
762 1.1 jtc if ( (lastc != OUT && isalnum(lastc)) &&
763 1.1 jtc (flagch == EOL || (c != OUT && !isalnum(c))) ) {
764 1.1 jtc flagch = EOW;
765 1.1 jtc }
766 1.1 jtc if (flagch == BOW || flagch == EOW) {
767 1.1 jtc st = step(m->g, startst, stopst, st, flagch, st);
768 1.1 jtc SP("sboweow", st, c);
769 1.1 jtc }
770 1.1 jtc
771 1.1 jtc /* are we done? */
772 1.1 jtc if (ISSET(st, stopst))
773 1.1 jtc matchp = p;
774 1.1 jtc if (EQ(st, empty) || p == stop)
775 1.1 jtc break; /* NOTE BREAK OUT */
776 1.1 jtc
777 1.1 jtc /* no, we must deal with this character */
778 1.1 jtc ASSIGN(tmp, st);
779 1.1 jtc ASSIGN(st, empty);
780 1.1 jtc assert(c != OUT);
781 1.1 jtc st = step(m->g, startst, stopst, tmp, c, st);
782 1.1 jtc SP("saft", st, c);
783 1.1 jtc assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
784 1.1 jtc p++;
785 1.1 jtc }
786 1.1 jtc
787 1.1 jtc return(matchp);
788 1.1 jtc }
789 1.1 jtc
790 1.1 jtc
791 1.1 jtc /*
792 1.1 jtc - step - map set of states reachable before char to set reachable after
793 1.1 jtc == static states step(register struct re_guts *g, int start, int stop, \
794 1.1 jtc == register states bef, int ch, register states aft);
795 1.1 jtc == #define BOL (OUT+1)
796 1.1 jtc == #define EOL (BOL+1)
797 1.1 jtc == #define BOLEOL (BOL+2)
798 1.1 jtc == #define NOTHING (BOL+3)
799 1.1 jtc == #define BOW (BOL+4)
800 1.1 jtc == #define EOW (BOL+5)
801 1.1 jtc == #define CODEMAX (BOL+5) // highest code used
802 1.1 jtc == #define NONCHAR(c) ((c) > CHAR_MAX)
803 1.1 jtc == #define NNONCHAR (CODEMAX-CHAR_MAX)
804 1.1 jtc */
805 1.1 jtc static states
806 1.1 jtc step(g, start, stop, bef, ch, aft)
807 1.1 jtc register struct re_guts *g;
808 1.1 jtc int start; /* start state within strip */
809 1.1 jtc int stop; /* state after stop state within strip */
810 1.1 jtc register states bef; /* states reachable before */
811 1.1 jtc int ch; /* character or NONCHAR code */
812 1.1 jtc register states aft; /* states already known reachable after */
813 1.1 jtc {
814 1.1 jtc register cset *cs;
815 1.1 jtc register sop s;
816 1.1 jtc register sopno pc;
817 1.1 jtc register onestate here; /* note, macros know this name */
818 1.1 jtc register sopno look;
819 1.1 jtc register int i;
820 1.1 jtc
821 1.1 jtc for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
822 1.1 jtc s = g->strip[pc];
823 1.1 jtc switch (OP(s)) {
824 1.1 jtc case OEND:
825 1.1 jtc assert(pc == stop-1);
826 1.1 jtc break;
827 1.1 jtc case OCHAR:
828 1.1 jtc /* only characters can match */
829 1.1 jtc assert(!NONCHAR(ch) || ch != (char)OPND(s));
830 1.1 jtc if (ch == (char)OPND(s))
831 1.1 jtc FWD(aft, bef, 1);
832 1.1 jtc break;
833 1.1 jtc case OBOL:
834 1.1 jtc if (ch == BOL || ch == BOLEOL)
835 1.1 jtc FWD(aft, bef, 1);
836 1.1 jtc break;
837 1.1 jtc case OEOL:
838 1.1 jtc if (ch == EOL || ch == BOLEOL)
839 1.1 jtc FWD(aft, bef, 1);
840 1.1 jtc break;
841 1.1 jtc case OBOW:
842 1.1 jtc if (ch == BOW)
843 1.1 jtc FWD(aft, bef, 1);
844 1.1 jtc break;
845 1.1 jtc case OEOW:
846 1.1 jtc if (ch == EOW)
847 1.1 jtc FWD(aft, bef, 1);
848 1.1 jtc break;
849 1.1 jtc case OANY:
850 1.1 jtc if (!NONCHAR(ch))
851 1.1 jtc FWD(aft, bef, 1);
852 1.1 jtc break;
853 1.1 jtc case OANYOF:
854 1.1 jtc cs = &g->sets[OPND(s)];
855 1.1 jtc if (!NONCHAR(ch) && CHIN(cs, ch))
856 1.1 jtc FWD(aft, bef, 1);
857 1.1 jtc break;
858 1.1 jtc case OBACK_: /* ignored here */
859 1.1 jtc case O_BACK:
860 1.1 jtc FWD(aft, aft, 1);
861 1.1 jtc break;
862 1.1 jtc case OPLUS_: /* forward, this is just an empty */
863 1.1 jtc FWD(aft, aft, 1);
864 1.1 jtc break;
865 1.1 jtc case O_PLUS: /* both forward and back */
866 1.1 jtc FWD(aft, aft, 1);
867 1.1 jtc i = ISSETBACK(aft, OPND(s));
868 1.1 jtc BACK(aft, aft, OPND(s));
869 1.1 jtc if (!i && ISSETBACK(aft, OPND(s))) {
870 1.1 jtc /* oho, must reconsider loop body */
871 1.1 jtc pc -= OPND(s) + 1;
872 1.1 jtc INIT(here, pc);
873 1.1 jtc }
874 1.1 jtc break;
875 1.1 jtc case OQUEST_: /* two branches, both forward */
876 1.1 jtc FWD(aft, aft, 1);
877 1.1 jtc FWD(aft, aft, OPND(s));
878 1.1 jtc break;
879 1.1 jtc case O_QUEST: /* just an empty */
880 1.1 jtc FWD(aft, aft, 1);
881 1.1 jtc break;
882 1.1 jtc case OLPAREN: /* not significant here */
883 1.1 jtc case ORPAREN:
884 1.1 jtc FWD(aft, aft, 1);
885 1.1 jtc break;
886 1.1 jtc case OCH_: /* mark the first two branches */
887 1.1 jtc FWD(aft, aft, 1);
888 1.1 jtc assert(OP(g->strip[pc+OPND(s)]) == OOR2);
889 1.1 jtc FWD(aft, aft, OPND(s));
890 1.1 jtc break;
891 1.1 jtc case OOR1: /* done a branch, find the O_CH */
892 1.1 jtc if (ISSTATEIN(aft, here)) {
893 1.1 jtc for (look = 1;
894 1.1 jtc OP(s = g->strip[pc+look]) != O_CH;
895 1.1 jtc look += OPND(s))
896 1.1 jtc assert(OP(s) == OOR2);
897 1.1 jtc FWD(aft, aft, look);
898 1.1 jtc }
899 1.1 jtc break;
900 1.1 jtc case OOR2: /* propagate OCH_'s marking */
901 1.1 jtc FWD(aft, aft, 1);
902 1.1 jtc if (OP(g->strip[pc+OPND(s)]) != O_CH) {
903 1.1 jtc assert(OP(g->strip[pc+OPND(s)]) == OOR2);
904 1.1 jtc FWD(aft, aft, OPND(s));
905 1.1 jtc }
906 1.1 jtc break;
907 1.1 jtc case O_CH: /* just empty */
908 1.1 jtc FWD(aft, aft, 1);
909 1.1 jtc break;
910 1.1 jtc default: /* ooooops... */
911 1.1 jtc assert(nope);
912 1.1 jtc break;
913 1.1 jtc }
914 1.1 jtc }
915 1.1 jtc
916 1.1 jtc return(aft);
917 1.1 jtc }
918 1.1 jtc
919 1.1 jtc #ifdef REDEBUG
920 1.1 jtc /*
921 1.1 jtc - print - print a set of states
922 1.1 jtc == #ifdef REDEBUG
923 1.1 jtc == static void print(struct match *m, char *caption, states st, \
924 1.1 jtc == int ch, FILE *d);
925 1.1 jtc == #endif
926 1.1 jtc */
927 1.1 jtc static void
928 1.1 jtc print(m, caption, st, ch, d)
929 1.1 jtc struct match *m;
930 1.1 jtc char *caption;
931 1.1 jtc states st;
932 1.1 jtc int ch;
933 1.1 jtc FILE *d;
934 1.1 jtc {
935 1.1 jtc register struct re_guts *g = m->g;
936 1.1 jtc register int i;
937 1.1 jtc register int first = 1;
938 1.1 jtc
939 1.1 jtc if (!(m->eflags®_TRACE))
940 1.1 jtc return;
941 1.1 jtc
942 1.1 jtc fprintf(d, "%s", caption);
943 1.1 jtc if (ch != '\0')
944 1.1 jtc fprintf(d, " %s", pchar(ch));
945 1.1 jtc for (i = 0; i < g->nstates; i++)
946 1.1 jtc if (ISSET(st, i)) {
947 1.1 jtc fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
948 1.1 jtc first = 0;
949 1.1 jtc }
950 1.1 jtc fprintf(d, "\n");
951 1.1 jtc }
952 1.1 jtc
953 1.1 jtc /*
954 1.1 jtc - at - print current situation
955 1.1 jtc == #ifdef REDEBUG
956 1.1 jtc == static void at(struct match *m, char *title, char *start, char *stop, \
957 1.1 jtc == sopno startst, sopno stopst);
958 1.1 jtc == #endif
959 1.1 jtc */
960 1.1 jtc static void
961 1.1 jtc at(m, title, start, stop, startst, stopst)
962 1.1 jtc struct match *m;
963 1.1 jtc char *title;
964 1.1 jtc char *start;
965 1.1 jtc char *stop;
966 1.1 jtc sopno startst;
967 1.1 jtc sopno stopst;
968 1.1 jtc {
969 1.1 jtc if (!(m->eflags®_TRACE))
970 1.1 jtc return;
971 1.1 jtc
972 1.1 jtc printf("%s %s-", title, pchar(*start));
973 1.1 jtc printf("%s ", pchar(*stop));
974 1.1 jtc printf("%ld-%ld\n", (long)startst, (long)stopst);
975 1.1 jtc }
976 1.1 jtc
977 1.1 jtc #ifndef PCHARDONE
978 1.1 jtc #define PCHARDONE /* never again */
979 1.1 jtc /*
980 1.1 jtc - pchar - make a character printable
981 1.1 jtc == #ifdef REDEBUG
982 1.1 jtc == static char *pchar(int ch);
983 1.1 jtc == #endif
984 1.1 jtc *
985 1.1 jtc * Is this identical to regchar() over in debug.c? Well, yes. But a
986 1.1 jtc * duplicate here avoids having a debugging-capable regexec.o tied to
987 1.1 jtc * a matching debug.o, and this is convenient. It all disappears in
988 1.1 jtc * the non-debug compilation anyway, so it doesn't matter much.
989 1.1 jtc */
990 1.1 jtc static char * /* -> representation */
991 1.1 jtc pchar(ch)
992 1.1 jtc int ch;
993 1.1 jtc {
994 1.1 jtc static char pbuf[10];
995 1.1 jtc
996 1.1 jtc if (isprint(ch) || ch == ' ')
997 1.1 jtc sprintf(pbuf, "%c", ch);
998 1.1 jtc else
999 1.1 jtc sprintf(pbuf, "\\%o", ch);
1000 1.1 jtc return(pbuf);
1001 1.1 jtc }
1002 1.1 jtc #endif
1003 1.1 jtc #endif
1004 1.1 jtc
1005 1.1 jtc #undef matcher
1006 1.1 jtc #undef fast
1007 1.1 jtc #undef slow
1008 1.1 jtc #undef dissect
1009 1.1 jtc #undef backref
1010 1.1 jtc #undef step
1011 1.1 jtc #undef print
1012 1.1 jtc #undef at
1013 1.1 jtc #undef match
1014