engine.c revision 1.2 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.2 jtc assert(0 < i && i <= m->g->nsub);
397 1.1 jtc m->pmatch[i].rm_so = sp - m->offp;
398 1.1 jtc break;
399 1.1 jtc case ORPAREN:
400 1.1 jtc i = OPND(m->g->strip[ss]);
401 1.2 jtc assert(0 < i && i <= m->g->nsub);
402 1.1 jtc m->pmatch[i].rm_eo = sp - m->offp;
403 1.1 jtc break;
404 1.1 jtc default: /* uh oh */
405 1.1 jtc assert(nope);
406 1.1 jtc break;
407 1.1 jtc }
408 1.1 jtc }
409 1.1 jtc
410 1.1 jtc assert(sp == stop);
411 1.1 jtc return(sp);
412 1.1 jtc }
413 1.1 jtc
414 1.1 jtc /*
415 1.1 jtc - backref - figure out what matched what, figuring in back references
416 1.1 jtc == static char *backref(register struct match *m, char *start, \
417 1.1 jtc == char *stop, sopno startst, sopno stopst, sopno lev);
418 1.1 jtc */
419 1.1 jtc static char * /* == stop (success) or NULL (failure) */
420 1.1 jtc backref(m, start, stop, startst, stopst, lev)
421 1.1 jtc register struct match *m;
422 1.1 jtc char *start;
423 1.1 jtc char *stop;
424 1.1 jtc sopno startst;
425 1.1 jtc sopno stopst;
426 1.1 jtc sopno lev; /* PLUS nesting level */
427 1.1 jtc {
428 1.1 jtc register int i;
429 1.1 jtc register sopno ss; /* start sop of current subRE */
430 1.1 jtc register char *sp; /* start of string matched by it */
431 1.1 jtc register sopno ssub; /* start sop of subsubRE */
432 1.1 jtc register sopno esub; /* end sop of subsubRE */
433 1.1 jtc register char *ssp; /* start of string matched by subsubRE */
434 1.1 jtc register char *dp;
435 1.1 jtc register size_t len;
436 1.1 jtc register int hard;
437 1.1 jtc register sop s;
438 1.1 jtc register regoff_t offsave;
439 1.1 jtc register cset *cs;
440 1.1 jtc
441 1.1 jtc AT("back", start, stop, startst, stopst);
442 1.1 jtc sp = start;
443 1.1 jtc
444 1.1 jtc /* get as far as we can with easy stuff */
445 1.1 jtc hard = 0;
446 1.1 jtc for (ss = startst; !hard && ss < stopst; ss++)
447 1.1 jtc switch (OP(s = m->g->strip[ss])) {
448 1.1 jtc case OCHAR:
449 1.1 jtc if (sp == stop || *sp++ != (char)OPND(s))
450 1.1 jtc return(NULL);
451 1.1 jtc break;
452 1.1 jtc case OANY:
453 1.1 jtc if (sp == stop)
454 1.1 jtc return(NULL);
455 1.1 jtc sp++;
456 1.1 jtc break;
457 1.1 jtc case OANYOF:
458 1.1 jtc cs = &m->g->sets[OPND(s)];
459 1.1 jtc if (sp == stop || !CHIN(cs, *sp++))
460 1.1 jtc return(NULL);
461 1.1 jtc break;
462 1.1 jtc case OBOL:
463 1.1 jtc if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
464 1.1 jtc (sp < m->endp && *(sp-1) == '\n' &&
465 1.1 jtc (m->g->cflags®_NEWLINE)) )
466 1.1 jtc { /* yes */ }
467 1.1 jtc else
468 1.1 jtc return(NULL);
469 1.1 jtc break;
470 1.1 jtc case OEOL:
471 1.1 jtc if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
472 1.1 jtc (sp < m->endp && *sp == '\n' &&
473 1.1 jtc (m->g->cflags®_NEWLINE)) )
474 1.1 jtc { /* yes */ }
475 1.1 jtc else
476 1.1 jtc return(NULL);
477 1.1 jtc break;
478 1.1 jtc case OBOW:
479 1.1 jtc if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
480 1.1 jtc (sp < m->endp && *(sp-1) == '\n' &&
481 1.1 jtc (m->g->cflags®_NEWLINE)) ||
482 1.1 jtc (sp > m->beginp &&
483 1.1 jtc !isalnum(*(sp-1))) ) &&
484 1.1 jtc (sp < m->endp && isalnum(*sp)) )
485 1.1 jtc { /* yes */ }
486 1.1 jtc else
487 1.1 jtc return(NULL);
488 1.1 jtc break;
489 1.1 jtc case OEOW:
490 1.1 jtc if (( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
491 1.1 jtc (sp < m->endp && *sp == '\n' &&
492 1.1 jtc (m->g->cflags®_NEWLINE)) ||
493 1.1 jtc (sp < m->endp && !isalnum(*sp)) ) &&
494 1.1 jtc (sp > m->beginp && isalnum(*(sp-1))) )
495 1.1 jtc { /* yes */ }
496 1.1 jtc else
497 1.1 jtc return(NULL);
498 1.1 jtc break;
499 1.1 jtc case O_QUEST:
500 1.1 jtc break;
501 1.1 jtc case OOR1: /* matches null but needs to skip */
502 1.1 jtc ss++;
503 1.1 jtc s = m->g->strip[ss];
504 1.1 jtc do {
505 1.1 jtc assert(OP(s) == OOR2);
506 1.1 jtc ss += OPND(s);
507 1.1 jtc } while (OP(s = m->g->strip[ss]) != O_CH);
508 1.1 jtc /* note that the ss++ gets us past the O_CH */
509 1.1 jtc break;
510 1.1 jtc default: /* have to make a choice */
511 1.1 jtc hard = 1;
512 1.1 jtc break;
513 1.1 jtc }
514 1.1 jtc if (!hard) { /* that was it! */
515 1.1 jtc if (sp != stop)
516 1.1 jtc return(NULL);
517 1.1 jtc return(sp);
518 1.1 jtc }
519 1.1 jtc ss--; /* adjust for the for's final increment */
520 1.1 jtc
521 1.1 jtc /* the hard stuff */
522 1.1 jtc AT("hard", sp, stop, ss, stopst);
523 1.1 jtc s = m->g->strip[ss];
524 1.1 jtc switch (OP(s)) {
525 1.1 jtc case OBACK_: /* the vilest depths */
526 1.1 jtc i = OPND(s);
527 1.2 jtc assert(0 < i && i <= m->g->nsub);
528 1.1 jtc if (m->pmatch[i].rm_eo == -1)
529 1.1 jtc return(NULL);
530 1.1 jtc assert(m->pmatch[i].rm_so != -1);
531 1.1 jtc len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
532 1.1 jtc assert(stop - m->beginp >= len);
533 1.1 jtc if (sp > stop - len)
534 1.1 jtc return(NULL); /* not enough left to match */
535 1.1 jtc ssp = m->offp + m->pmatch[i].rm_so;
536 1.1 jtc if (memcmp(sp, ssp, len) != 0)
537 1.1 jtc return(NULL);
538 1.1 jtc while (m->g->strip[ss] != SOP(O_BACK, i))
539 1.1 jtc ss++;
540 1.1 jtc return(backref(m, sp+len, stop, ss+1, stopst, lev));
541 1.1 jtc break;
542 1.1 jtc case OQUEST_: /* to null or not */
543 1.1 jtc dp = backref(m, sp, stop, ss+1, stopst, lev);
544 1.1 jtc if (dp != NULL)
545 1.1 jtc return(dp); /* not */
546 1.1 jtc return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
547 1.1 jtc break;
548 1.1 jtc case OPLUS_:
549 1.1 jtc assert(m->lastpos != NULL);
550 1.1 jtc assert(lev+1 <= m->g->nplus);
551 1.1 jtc m->lastpos[lev+1] = sp;
552 1.1 jtc return(backref(m, sp, stop, ss+1, stopst, lev+1));
553 1.1 jtc break;
554 1.1 jtc case O_PLUS:
555 1.1 jtc if (sp == m->lastpos[lev]) /* last pass matched null */
556 1.1 jtc return(backref(m, sp, stop, ss+1, stopst, lev-1));
557 1.1 jtc /* try another pass */
558 1.1 jtc m->lastpos[lev] = sp;
559 1.1 jtc dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
560 1.1 jtc if (dp == NULL)
561 1.1 jtc return(backref(m, sp, stop, ss+1, stopst, lev-1));
562 1.1 jtc else
563 1.1 jtc return(dp);
564 1.1 jtc break;
565 1.1 jtc case OCH_: /* find the right one, if any */
566 1.1 jtc ssub = ss + 1;
567 1.1 jtc esub = ss + OPND(s) - 1;
568 1.1 jtc assert(OP(m->g->strip[esub]) == OOR1);
569 1.1 jtc for (;;) { /* find first matching branch */
570 1.1 jtc dp = backref(m, sp, stop, ssub, esub, lev);
571 1.1 jtc if (dp != NULL)
572 1.1 jtc return(dp);
573 1.1 jtc /* that one missed, try next one */
574 1.1 jtc if (OP(m->g->strip[esub]) == O_CH)
575 1.1 jtc return(NULL); /* there is none */
576 1.1 jtc esub++;
577 1.1 jtc assert(OP(m->g->strip[esub]) == OOR2);
578 1.1 jtc ssub = esub + 1;
579 1.1 jtc esub += OPND(m->g->strip[esub]);
580 1.1 jtc if (OP(m->g->strip[esub]) == OOR2)
581 1.1 jtc esub--;
582 1.1 jtc else
583 1.1 jtc assert(OP(m->g->strip[esub]) == O_CH);
584 1.1 jtc }
585 1.1 jtc break;
586 1.1 jtc case OLPAREN: /* must undo assignment if rest fails */
587 1.1 jtc i = OPND(s);
588 1.2 jtc assert(0 < i && i <= m->g->nsub);
589 1.1 jtc offsave = m->pmatch[i].rm_so;
590 1.1 jtc m->pmatch[i].rm_so = sp - m->offp;
591 1.1 jtc dp = backref(m, sp, stop, ss+1, stopst, lev);
592 1.1 jtc if (dp != NULL)
593 1.1 jtc return(dp);
594 1.1 jtc m->pmatch[i].rm_so = offsave;
595 1.1 jtc return(NULL);
596 1.1 jtc break;
597 1.1 jtc case ORPAREN: /* must undo assignment if rest fails */
598 1.1 jtc i = OPND(s);
599 1.2 jtc assert(0 < i && i <= m->g->nsub);
600 1.1 jtc offsave = m->pmatch[i].rm_eo;
601 1.1 jtc m->pmatch[i].rm_eo = sp - m->offp;
602 1.1 jtc dp = backref(m, sp, stop, ss+1, stopst, lev);
603 1.1 jtc if (dp != NULL)
604 1.1 jtc return(dp);
605 1.1 jtc m->pmatch[i].rm_eo = offsave;
606 1.1 jtc return(NULL);
607 1.1 jtc break;
608 1.1 jtc default: /* uh oh */
609 1.1 jtc assert(nope);
610 1.1 jtc break;
611 1.1 jtc }
612 1.1 jtc
613 1.1 jtc /* "can't happen" */
614 1.1 jtc assert(nope);
615 1.1 jtc /* NOTREACHED */
616 1.1 jtc }
617 1.1 jtc
618 1.1 jtc /*
619 1.1 jtc - fast - step through the string at top speed
620 1.1 jtc == static char *fast(register struct match *m, char *start, \
621 1.1 jtc == char *stop, sopno startst, sopno stopst);
622 1.1 jtc */
623 1.1 jtc static char * /* where tentative match ended, or NULL */
624 1.1 jtc fast(m, start, stop, startst, stopst)
625 1.1 jtc register struct match *m;
626 1.1 jtc char *start;
627 1.1 jtc char *stop;
628 1.1 jtc sopno startst;
629 1.1 jtc sopno stopst;
630 1.1 jtc {
631 1.1 jtc register states st = m->st;
632 1.1 jtc register states fresh = m->fresh;
633 1.1 jtc register states tmp = m->tmp;
634 1.1 jtc register char *p = start;
635 1.1 jtc register int c = (start == m->beginp) ? OUT : *(start-1);
636 1.1 jtc register int lastc; /* previous c */
637 1.1 jtc register int flagch;
638 1.1 jtc register int i;
639 1.1 jtc register char *coldp; /* last p after which no match was underway */
640 1.1 jtc
641 1.1 jtc CLEAR(st);
642 1.1 jtc SET1(st, startst);
643 1.1 jtc st = step(m->g, startst, stopst, st, NOTHING, st);
644 1.1 jtc ASSIGN(fresh, st);
645 1.1 jtc SP("start", st, *p);
646 1.1 jtc coldp = NULL;
647 1.1 jtc for (;;) {
648 1.1 jtc /* next character */
649 1.1 jtc lastc = c;
650 1.1 jtc c = (p == m->endp) ? OUT : *p;
651 1.1 jtc if (EQ(st, fresh))
652 1.1 jtc coldp = p;
653 1.1 jtc
654 1.1 jtc /* is there an EOL and/or BOL between lastc and c? */
655 1.1 jtc flagch = '\0';
656 1.1 jtc i = 0;
657 1.1 jtc if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
658 1.1 jtc (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
659 1.1 jtc flagch = BOL;
660 1.1 jtc i = m->g->nbol;
661 1.1 jtc }
662 1.1 jtc if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
663 1.1 jtc (c == OUT && !(m->eflags®_NOTEOL)) ) {
664 1.1 jtc flagch = (flagch == BOL) ? BOLEOL : EOL;
665 1.1 jtc i += m->g->neol;
666 1.1 jtc }
667 1.1 jtc if (i != 0) {
668 1.1 jtc for (; i > 0; i--)
669 1.1 jtc st = step(m->g, startst, stopst, st, flagch, st);
670 1.1 jtc SP("boleol", st, c);
671 1.1 jtc }
672 1.1 jtc
673 1.1 jtc /* how about a word boundary? */
674 1.1 jtc if ( (flagch == BOL || (lastc != OUT && !isalnum(lastc))) &&
675 1.1 jtc (c != OUT && isalnum(c)) ) {
676 1.1 jtc flagch = BOW;
677 1.1 jtc }
678 1.1 jtc if ( (lastc != OUT && isalnum(lastc)) &&
679 1.1 jtc (flagch == EOL || (c != OUT && !isalnum(c))) ) {
680 1.1 jtc flagch = EOW;
681 1.1 jtc }
682 1.1 jtc if (flagch == BOW || flagch == EOW) {
683 1.1 jtc st = step(m->g, startst, stopst, st, flagch, st);
684 1.1 jtc SP("boweow", st, c);
685 1.1 jtc }
686 1.1 jtc
687 1.1 jtc /* are we done? */
688 1.1 jtc if (ISSET(st, stopst) || p == stop)
689 1.1 jtc break; /* NOTE BREAK OUT */
690 1.1 jtc
691 1.1 jtc /* no, we must deal with this character */
692 1.1 jtc ASSIGN(tmp, st);
693 1.1 jtc ASSIGN(st, fresh);
694 1.1 jtc assert(c != OUT);
695 1.1 jtc st = step(m->g, startst, stopst, tmp, c, st);
696 1.1 jtc SP("aft", st, c);
697 1.1 jtc assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
698 1.1 jtc p++;
699 1.1 jtc }
700 1.1 jtc
701 1.1 jtc assert(coldp != NULL);
702 1.1 jtc m->coldp = coldp;
703 1.1 jtc if (ISSET(st, stopst))
704 1.1 jtc return(p+1);
705 1.1 jtc else
706 1.1 jtc return(NULL);
707 1.1 jtc }
708 1.1 jtc
709 1.1 jtc /*
710 1.1 jtc - slow - step through the string more deliberately
711 1.1 jtc == static char *slow(register struct match *m, char *start, \
712 1.1 jtc == char *stop, sopno startst, sopno stopst);
713 1.1 jtc */
714 1.1 jtc static char * /* where it ended */
715 1.1 jtc slow(m, start, stop, startst, stopst)
716 1.1 jtc register struct match *m;
717 1.1 jtc char *start;
718 1.1 jtc char *stop;
719 1.1 jtc sopno startst;
720 1.1 jtc sopno stopst;
721 1.1 jtc {
722 1.1 jtc register states st = m->st;
723 1.1 jtc register states empty = m->empty;
724 1.1 jtc register states tmp = m->tmp;
725 1.1 jtc register char *p = start;
726 1.1 jtc register int c = (start == m->beginp) ? OUT : *(start-1);
727 1.1 jtc register int lastc; /* previous c */
728 1.1 jtc register int flagch;
729 1.1 jtc register int i;
730 1.1 jtc register char *matchp; /* last p at which a match ended */
731 1.1 jtc
732 1.1 jtc AT("slow", start, stop, startst, stopst);
733 1.1 jtc CLEAR(st);
734 1.1 jtc SET1(st, startst);
735 1.1 jtc SP("sstart", st, *p);
736 1.1 jtc st = step(m->g, startst, stopst, st, NOTHING, st);
737 1.1 jtc matchp = NULL;
738 1.1 jtc for (;;) {
739 1.1 jtc /* next character */
740 1.1 jtc lastc = c;
741 1.1 jtc c = (p == m->endp) ? OUT : *p;
742 1.1 jtc
743 1.1 jtc /* is there an EOL and/or BOL between lastc and c? */
744 1.1 jtc flagch = '\0';
745 1.1 jtc i = 0;
746 1.1 jtc if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
747 1.1 jtc (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
748 1.1 jtc flagch = BOL;
749 1.1 jtc i = m->g->nbol;
750 1.1 jtc }
751 1.1 jtc if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
752 1.1 jtc (c == OUT && !(m->eflags®_NOTEOL)) ) {
753 1.1 jtc flagch = (flagch == BOL) ? BOLEOL : EOL;
754 1.1 jtc i += m->g->neol;
755 1.1 jtc }
756 1.1 jtc if (i != 0) {
757 1.1 jtc for (; i > 0; i--)
758 1.1 jtc st = step(m->g, startst, stopst, st, flagch, st);
759 1.1 jtc SP("sboleol", st, c);
760 1.1 jtc }
761 1.1 jtc
762 1.1 jtc /* how about a word boundary? */
763 1.1 jtc if ( (flagch == BOL || (lastc != OUT && !isalnum(lastc))) &&
764 1.1 jtc (c != OUT && isalnum(c)) ) {
765 1.1 jtc flagch = BOW;
766 1.1 jtc }
767 1.1 jtc if ( (lastc != OUT && isalnum(lastc)) &&
768 1.1 jtc (flagch == EOL || (c != OUT && !isalnum(c))) ) {
769 1.1 jtc flagch = EOW;
770 1.1 jtc }
771 1.1 jtc if (flagch == BOW || flagch == EOW) {
772 1.1 jtc st = step(m->g, startst, stopst, st, flagch, st);
773 1.1 jtc SP("sboweow", st, c);
774 1.1 jtc }
775 1.1 jtc
776 1.1 jtc /* are we done? */
777 1.1 jtc if (ISSET(st, stopst))
778 1.1 jtc matchp = p;
779 1.1 jtc if (EQ(st, empty) || p == stop)
780 1.1 jtc break; /* NOTE BREAK OUT */
781 1.1 jtc
782 1.1 jtc /* no, we must deal with this character */
783 1.1 jtc ASSIGN(tmp, st);
784 1.1 jtc ASSIGN(st, empty);
785 1.1 jtc assert(c != OUT);
786 1.1 jtc st = step(m->g, startst, stopst, tmp, c, st);
787 1.1 jtc SP("saft", st, c);
788 1.1 jtc assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
789 1.1 jtc p++;
790 1.1 jtc }
791 1.1 jtc
792 1.1 jtc return(matchp);
793 1.1 jtc }
794 1.1 jtc
795 1.1 jtc
796 1.1 jtc /*
797 1.1 jtc - step - map set of states reachable before char to set reachable after
798 1.1 jtc == static states step(register struct re_guts *g, int start, int stop, \
799 1.1 jtc == register states bef, int ch, register states aft);
800 1.1 jtc == #define BOL (OUT+1)
801 1.1 jtc == #define EOL (BOL+1)
802 1.1 jtc == #define BOLEOL (BOL+2)
803 1.1 jtc == #define NOTHING (BOL+3)
804 1.1 jtc == #define BOW (BOL+4)
805 1.1 jtc == #define EOW (BOL+5)
806 1.1 jtc == #define CODEMAX (BOL+5) // highest code used
807 1.1 jtc == #define NONCHAR(c) ((c) > CHAR_MAX)
808 1.1 jtc == #define NNONCHAR (CODEMAX-CHAR_MAX)
809 1.1 jtc */
810 1.1 jtc static states
811 1.1 jtc step(g, start, stop, bef, ch, aft)
812 1.1 jtc register struct re_guts *g;
813 1.1 jtc int start; /* start state within strip */
814 1.1 jtc int stop; /* state after stop state within strip */
815 1.1 jtc register states bef; /* states reachable before */
816 1.1 jtc int ch; /* character or NONCHAR code */
817 1.1 jtc register states aft; /* states already known reachable after */
818 1.1 jtc {
819 1.1 jtc register cset *cs;
820 1.1 jtc register sop s;
821 1.1 jtc register sopno pc;
822 1.1 jtc register onestate here; /* note, macros know this name */
823 1.1 jtc register sopno look;
824 1.1 jtc register int i;
825 1.1 jtc
826 1.1 jtc for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
827 1.1 jtc s = g->strip[pc];
828 1.1 jtc switch (OP(s)) {
829 1.1 jtc case OEND:
830 1.1 jtc assert(pc == stop-1);
831 1.1 jtc break;
832 1.1 jtc case OCHAR:
833 1.1 jtc /* only characters can match */
834 1.1 jtc assert(!NONCHAR(ch) || ch != (char)OPND(s));
835 1.1 jtc if (ch == (char)OPND(s))
836 1.1 jtc FWD(aft, bef, 1);
837 1.1 jtc break;
838 1.1 jtc case OBOL:
839 1.1 jtc if (ch == BOL || ch == BOLEOL)
840 1.1 jtc FWD(aft, bef, 1);
841 1.1 jtc break;
842 1.1 jtc case OEOL:
843 1.1 jtc if (ch == EOL || ch == BOLEOL)
844 1.1 jtc FWD(aft, bef, 1);
845 1.1 jtc break;
846 1.1 jtc case OBOW:
847 1.1 jtc if (ch == BOW)
848 1.1 jtc FWD(aft, bef, 1);
849 1.1 jtc break;
850 1.1 jtc case OEOW:
851 1.1 jtc if (ch == EOW)
852 1.1 jtc FWD(aft, bef, 1);
853 1.1 jtc break;
854 1.1 jtc case OANY:
855 1.1 jtc if (!NONCHAR(ch))
856 1.1 jtc FWD(aft, bef, 1);
857 1.1 jtc break;
858 1.1 jtc case OANYOF:
859 1.1 jtc cs = &g->sets[OPND(s)];
860 1.1 jtc if (!NONCHAR(ch) && CHIN(cs, ch))
861 1.1 jtc FWD(aft, bef, 1);
862 1.1 jtc break;
863 1.1 jtc case OBACK_: /* ignored here */
864 1.1 jtc case O_BACK:
865 1.1 jtc FWD(aft, aft, 1);
866 1.1 jtc break;
867 1.1 jtc case OPLUS_: /* forward, this is just an empty */
868 1.1 jtc FWD(aft, aft, 1);
869 1.1 jtc break;
870 1.1 jtc case O_PLUS: /* both forward and back */
871 1.1 jtc FWD(aft, aft, 1);
872 1.1 jtc i = ISSETBACK(aft, OPND(s));
873 1.1 jtc BACK(aft, aft, OPND(s));
874 1.1 jtc if (!i && ISSETBACK(aft, OPND(s))) {
875 1.1 jtc /* oho, must reconsider loop body */
876 1.1 jtc pc -= OPND(s) + 1;
877 1.1 jtc INIT(here, pc);
878 1.1 jtc }
879 1.1 jtc break;
880 1.1 jtc case OQUEST_: /* two branches, both forward */
881 1.1 jtc FWD(aft, aft, 1);
882 1.1 jtc FWD(aft, aft, OPND(s));
883 1.1 jtc break;
884 1.1 jtc case O_QUEST: /* just an empty */
885 1.1 jtc FWD(aft, aft, 1);
886 1.1 jtc break;
887 1.1 jtc case OLPAREN: /* not significant here */
888 1.1 jtc case ORPAREN:
889 1.1 jtc FWD(aft, aft, 1);
890 1.1 jtc break;
891 1.1 jtc case OCH_: /* mark the first two branches */
892 1.1 jtc FWD(aft, aft, 1);
893 1.1 jtc assert(OP(g->strip[pc+OPND(s)]) == OOR2);
894 1.1 jtc FWD(aft, aft, OPND(s));
895 1.1 jtc break;
896 1.1 jtc case OOR1: /* done a branch, find the O_CH */
897 1.1 jtc if (ISSTATEIN(aft, here)) {
898 1.1 jtc for (look = 1;
899 1.1 jtc OP(s = g->strip[pc+look]) != O_CH;
900 1.1 jtc look += OPND(s))
901 1.1 jtc assert(OP(s) == OOR2);
902 1.1 jtc FWD(aft, aft, look);
903 1.1 jtc }
904 1.1 jtc break;
905 1.1 jtc case OOR2: /* propagate OCH_'s marking */
906 1.1 jtc FWD(aft, aft, 1);
907 1.1 jtc if (OP(g->strip[pc+OPND(s)]) != O_CH) {
908 1.1 jtc assert(OP(g->strip[pc+OPND(s)]) == OOR2);
909 1.1 jtc FWD(aft, aft, OPND(s));
910 1.1 jtc }
911 1.1 jtc break;
912 1.1 jtc case O_CH: /* just empty */
913 1.1 jtc FWD(aft, aft, 1);
914 1.1 jtc break;
915 1.1 jtc default: /* ooooops... */
916 1.1 jtc assert(nope);
917 1.1 jtc break;
918 1.1 jtc }
919 1.1 jtc }
920 1.1 jtc
921 1.1 jtc return(aft);
922 1.1 jtc }
923 1.1 jtc
924 1.1 jtc #ifdef REDEBUG
925 1.1 jtc /*
926 1.1 jtc - print - print a set of states
927 1.1 jtc == #ifdef REDEBUG
928 1.1 jtc == static void print(struct match *m, char *caption, states st, \
929 1.1 jtc == int ch, FILE *d);
930 1.1 jtc == #endif
931 1.1 jtc */
932 1.1 jtc static void
933 1.1 jtc print(m, caption, st, ch, d)
934 1.1 jtc struct match *m;
935 1.1 jtc char *caption;
936 1.1 jtc states st;
937 1.1 jtc int ch;
938 1.1 jtc FILE *d;
939 1.1 jtc {
940 1.1 jtc register struct re_guts *g = m->g;
941 1.1 jtc register int i;
942 1.1 jtc register int first = 1;
943 1.1 jtc
944 1.1 jtc if (!(m->eflags®_TRACE))
945 1.1 jtc return;
946 1.1 jtc
947 1.1 jtc fprintf(d, "%s", caption);
948 1.1 jtc if (ch != '\0')
949 1.1 jtc fprintf(d, " %s", pchar(ch));
950 1.1 jtc for (i = 0; i < g->nstates; i++)
951 1.1 jtc if (ISSET(st, i)) {
952 1.1 jtc fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
953 1.1 jtc first = 0;
954 1.1 jtc }
955 1.1 jtc fprintf(d, "\n");
956 1.1 jtc }
957 1.1 jtc
958 1.1 jtc /*
959 1.1 jtc - at - print current situation
960 1.1 jtc == #ifdef REDEBUG
961 1.1 jtc == static void at(struct match *m, char *title, char *start, char *stop, \
962 1.1 jtc == sopno startst, sopno stopst);
963 1.1 jtc == #endif
964 1.1 jtc */
965 1.1 jtc static void
966 1.1 jtc at(m, title, start, stop, startst, stopst)
967 1.1 jtc struct match *m;
968 1.1 jtc char *title;
969 1.1 jtc char *start;
970 1.1 jtc char *stop;
971 1.1 jtc sopno startst;
972 1.1 jtc sopno stopst;
973 1.1 jtc {
974 1.1 jtc if (!(m->eflags®_TRACE))
975 1.1 jtc return;
976 1.1 jtc
977 1.1 jtc printf("%s %s-", title, pchar(*start));
978 1.1 jtc printf("%s ", pchar(*stop));
979 1.1 jtc printf("%ld-%ld\n", (long)startst, (long)stopst);
980 1.1 jtc }
981 1.1 jtc
982 1.1 jtc #ifndef PCHARDONE
983 1.1 jtc #define PCHARDONE /* never again */
984 1.1 jtc /*
985 1.1 jtc - pchar - make a character printable
986 1.1 jtc == #ifdef REDEBUG
987 1.1 jtc == static char *pchar(int ch);
988 1.1 jtc == #endif
989 1.1 jtc *
990 1.1 jtc * Is this identical to regchar() over in debug.c? Well, yes. But a
991 1.1 jtc * duplicate here avoids having a debugging-capable regexec.o tied to
992 1.1 jtc * a matching debug.o, and this is convenient. It all disappears in
993 1.1 jtc * the non-debug compilation anyway, so it doesn't matter much.
994 1.1 jtc */
995 1.1 jtc static char * /* -> representation */
996 1.1 jtc pchar(ch)
997 1.1 jtc int ch;
998 1.1 jtc {
999 1.1 jtc static char pbuf[10];
1000 1.1 jtc
1001 1.1 jtc if (isprint(ch) || ch == ' ')
1002 1.1 jtc sprintf(pbuf, "%c", ch);
1003 1.1 jtc else
1004 1.1 jtc sprintf(pbuf, "\\%o", ch);
1005 1.1 jtc return(pbuf);
1006 1.1 jtc }
1007 1.1 jtc #endif
1008 1.1 jtc #endif
1009 1.1 jtc
1010 1.1 jtc #undef matcher
1011 1.1 jtc #undef fast
1012 1.1 jtc #undef slow
1013 1.1 jtc #undef dissect
1014 1.1 jtc #undef backref
1015 1.1 jtc #undef step
1016 1.1 jtc #undef print
1017 1.1 jtc #undef at
1018 1.1 jtc #undef match
1019