regcomp.c revision 1.1 1 1.1 jtc #include <sys/types.h>
2 1.1 jtc #include <stdio.h>
3 1.1 jtc #include <string.h>
4 1.1 jtc #include <ctype.h>
5 1.1 jtc #include <limits.h>
6 1.1 jtc #include <stdlib.h>
7 1.1 jtc #include <regex.h>
8 1.1 jtc
9 1.1 jtc #include "utils.h"
10 1.1 jtc #include "regex2.h"
11 1.1 jtc
12 1.1 jtc #include "cclass.h"
13 1.1 jtc #include "cname.h"
14 1.1 jtc
15 1.1 jtc /*
16 1.1 jtc * parse structure, passed up and down to avoid global variables and
17 1.1 jtc * other clumsinesses
18 1.1 jtc */
19 1.1 jtc struct parse {
20 1.1 jtc char *next; /* next character in RE */
21 1.1 jtc char *end; /* end of string (-> NUL normally) */
22 1.1 jtc int error; /* has an error been seen? */
23 1.1 jtc sop *strip; /* malloced strip */
24 1.1 jtc sopno ssize; /* malloced strip size (allocated) */
25 1.1 jtc sopno slen; /* malloced strip length (used) */
26 1.1 jtc int ncsalloc; /* number of csets allocated */
27 1.1 jtc struct re_guts *g;
28 1.1 jtc # define NPAREN 10 /* we need to remember () 1-9 for back refs */
29 1.1 jtc sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
30 1.1 jtc sopno pend[NPAREN]; /* -> ) ([0] unused) */
31 1.1 jtc };
32 1.1 jtc
33 1.1 jtc #include "regcomp.ih"
34 1.1 jtc
35 1.1 jtc static char nuls[10]; /* place to point scanner in event of error */
36 1.1 jtc
37 1.1 jtc /*
38 1.1 jtc * macros for use with parse structure
39 1.1 jtc * BEWARE: these know that the parse structure is named `p' !!!
40 1.1 jtc */
41 1.1 jtc #define PEEK() (*p->next)
42 1.1 jtc #define PEEK2() (*(p->next+1))
43 1.1 jtc #define MORE() (p->next < p->end)
44 1.1 jtc #define MORE2() (p->next+1 < p->end)
45 1.1 jtc #define SEE(c) (MORE() && PEEK() == (c))
46 1.1 jtc #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
47 1.1 jtc #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
48 1.1 jtc #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
49 1.1 jtc #define NEXT() (p->next++)
50 1.1 jtc #define NEXT2() (p->next += 2)
51 1.1 jtc #define NEXTn(n) (p->next += (n))
52 1.1 jtc #define GETNEXT() (*p->next++)
53 1.1 jtc #define SETERROR(e) seterr(p, (e))
54 1.1 jtc #define REQUIRE(co, e) ((co) || SETERROR(e))
55 1.1 jtc #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
56 1.1 jtc #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
57 1.1 jtc #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
58 1.1 jtc #define EMIT(sop, sopnd) doemit(p, sop, (size_t)(sopnd))
59 1.1 jtc #define INSERT(sop, pos) doinsert(p, sop, HERE()-(pos)+1, pos)
60 1.1 jtc #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
61 1.1 jtc #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
62 1.1 jtc #define HERE() (p->slen)
63 1.1 jtc #define THERE() (p->slen - 1)
64 1.1 jtc #define DROP(n) (p->slen -= (n))
65 1.1 jtc
66 1.1 jtc #ifndef NDEBUG
67 1.1 jtc static int never = 0; /* for use in asserts; shuts lint up */
68 1.1 jtc #endif
69 1.1 jtc
70 1.1 jtc /*
71 1.1 jtc - regcomp - interface for parser and compilation
72 1.1 jtc = extern int regcomp(regex_t *preg, const char *pattern, int cflags);
73 1.1 jtc = #define REG_BASIC 0000
74 1.1 jtc = #define REG_EXTENDED 0001
75 1.1 jtc = #define REG_ICASE 0002
76 1.1 jtc = #define REG_NOSUB 0004
77 1.1 jtc = #define REG_NEWLINE 0010
78 1.1 jtc = #define REG_NOSPEC 0020
79 1.1 jtc = #define REG_PEND 0040
80 1.1 jtc = #define REG_DUMP 0200
81 1.1 jtc */
82 1.1 jtc int /* 0 success, otherwise REG_something */
83 1.1 jtc regcomp(preg, pattern, cflags)
84 1.1 jtc regex_t *preg;
85 1.1 jtc const char *pattern;
86 1.1 jtc int cflags;
87 1.1 jtc {
88 1.1 jtc struct parse pa;
89 1.1 jtc register struct re_guts *g;
90 1.1 jtc register struct parse *p = &pa;
91 1.1 jtc register int i;
92 1.1 jtc register size_t len;
93 1.1 jtc
94 1.1 jtc if ((cflags®_EXTENDED) && (cflags®_NOSPEC))
95 1.1 jtc return(REG_INVARG);
96 1.1 jtc
97 1.1 jtc if (cflags®_PEND) {
98 1.1 jtc if (preg->re_endp < pattern)
99 1.1 jtc return(REG_INVARG);
100 1.1 jtc len = preg->re_endp - pattern;
101 1.1 jtc } else
102 1.1 jtc len = strlen((char *)pattern);
103 1.1 jtc
104 1.1 jtc /* do the mallocs early so failure handling is easy */
105 1.1 jtc g = (struct re_guts *)malloc(sizeof(struct re_guts) +
106 1.1 jtc (NC-1)*sizeof(cat_t));
107 1.1 jtc if (g == NULL)
108 1.1 jtc return(REG_ESPACE);
109 1.1 jtc p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
110 1.1 jtc p->strip = (sop *)malloc(p->ssize * sizeof(sop));
111 1.1 jtc p->slen = 0;
112 1.1 jtc if (p->strip == NULL) {
113 1.1 jtc free((char *)g);
114 1.1 jtc return(REG_ESPACE);
115 1.1 jtc }
116 1.1 jtc
117 1.1 jtc /* set things up */
118 1.1 jtc p->g = g;
119 1.1 jtc p->next = (char *)pattern; /* convenience; we do not modify it */
120 1.1 jtc p->end = p->next + len;
121 1.1 jtc p->error = 0;
122 1.1 jtc p->ncsalloc = 0;
123 1.1 jtc for (i = 0; i < NPAREN; i++) {
124 1.1 jtc p->pbegin[i] = 0;
125 1.1 jtc p->pend[i] = 0;
126 1.1 jtc }
127 1.1 jtc g->csetsize = NC;
128 1.1 jtc g->sets = NULL;
129 1.1 jtc g->setbits = NULL;
130 1.1 jtc g->ncsets = 0;
131 1.1 jtc g->cflags = cflags;
132 1.1 jtc g->iflags = 0;
133 1.1 jtc g->nbol = 0;
134 1.1 jtc g->neol = 0;
135 1.1 jtc g->must = NULL;
136 1.1 jtc g->mlen = 0;
137 1.1 jtc g->nsub = 0;
138 1.1 jtc g->ncategories = 1; /* category 0 is "everything else" */
139 1.1 jtc g->categories = &g->catspace[-(CHAR_MIN)];
140 1.1 jtc (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
141 1.1 jtc g->backrefs = 0;
142 1.1 jtc
143 1.1 jtc /* do it */
144 1.1 jtc EMIT(OEND, 0);
145 1.1 jtc g->firststate = THERE();
146 1.1 jtc if (cflags®_EXTENDED)
147 1.1 jtc p_ere(p, OUT);
148 1.1 jtc else if (cflags®_NOSPEC)
149 1.1 jtc p_str(p);
150 1.1 jtc else
151 1.1 jtc p_bre(p, OUT, OUT);
152 1.1 jtc EMIT(OEND, 0);
153 1.1 jtc g->laststate = THERE();
154 1.1 jtc
155 1.1 jtc /* tidy up loose ends and fill things in */
156 1.1 jtc categorize(p, g);
157 1.1 jtc stripsnug(p, g);
158 1.1 jtc findmust(p, g);
159 1.1 jtc g->nplus = pluscount(p, g);
160 1.1 jtc g->magic = MAGIC2;
161 1.1 jtc preg->re_nsub = g->nsub;
162 1.1 jtc preg->re_g = g;
163 1.1 jtc preg->re_magic = MAGIC1;
164 1.1 jtc #ifndef REDEBUG
165 1.1 jtc /* not debugging, so can't rely on the assert() in regexec() */
166 1.1 jtc if (g->iflags&BAD)
167 1.1 jtc SETERROR(REG_ASSERT);
168 1.1 jtc #endif
169 1.1 jtc
170 1.1 jtc /* win or lose, we're done */
171 1.1 jtc if (p->error != 0) /* lose */
172 1.1 jtc regfree(preg);
173 1.1 jtc return(p->error);
174 1.1 jtc }
175 1.1 jtc
176 1.1 jtc /*
177 1.1 jtc - p_ere - ERE parser top level, concatenation and alternation
178 1.1 jtc == static void p_ere(register struct parse *p, int stop);
179 1.1 jtc */
180 1.1 jtc static void
181 1.1 jtc p_ere(p, stop)
182 1.1 jtc register struct parse *p;
183 1.1 jtc int stop; /* character this ERE should end at */
184 1.1 jtc {
185 1.1 jtc register char c;
186 1.1 jtc register sopno prevback;
187 1.1 jtc register sopno prevfwd;
188 1.1 jtc register sopno conc;
189 1.1 jtc register int first = 1; /* is this the first alternative? */
190 1.1 jtc
191 1.1 jtc for (;;) {
192 1.1 jtc /* do a bunch of concatenated expressions */
193 1.1 jtc conc = HERE();
194 1.1 jtc while (MORE() && (c = PEEK()) != '|' && c != stop)
195 1.1 jtc p_ere_exp(p);
196 1.1 jtc REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
197 1.1 jtc
198 1.1 jtc if (!EAT('|'))
199 1.1 jtc break; /* NOTE BREAK OUT */
200 1.1 jtc
201 1.1 jtc if (first) {
202 1.1 jtc INSERT(OCH_, conc); /* offset is wrong */
203 1.1 jtc prevfwd = conc;
204 1.1 jtc prevback = conc;
205 1.1 jtc first = 0;
206 1.1 jtc }
207 1.1 jtc ASTERN(OOR1, prevback);
208 1.1 jtc prevback = THERE();
209 1.1 jtc AHEAD(prevfwd); /* fix previous offset */
210 1.1 jtc prevfwd = HERE();
211 1.1 jtc EMIT(OOR2, 0); /* offset is very wrong */
212 1.1 jtc }
213 1.1 jtc
214 1.1 jtc if (!first) { /* tail-end fixups */
215 1.1 jtc AHEAD(prevfwd);
216 1.1 jtc ASTERN(O_CH, prevback);
217 1.1 jtc }
218 1.1 jtc
219 1.1 jtc assert(!MORE() || SEE(stop));
220 1.1 jtc }
221 1.1 jtc
222 1.1 jtc /*
223 1.1 jtc - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
224 1.1 jtc == static void p_ere_exp(register struct parse *p);
225 1.1 jtc */
226 1.1 jtc static void
227 1.1 jtc p_ere_exp(p)
228 1.1 jtc register struct parse *p;
229 1.1 jtc {
230 1.1 jtc register char c;
231 1.1 jtc register sopno pos;
232 1.1 jtc register int count;
233 1.1 jtc register int count2;
234 1.1 jtc register sopno subno;
235 1.1 jtc int wascaret = 0;
236 1.1 jtc
237 1.1 jtc assert(MORE()); /* caller should have ensured this */
238 1.1 jtc c = GETNEXT();
239 1.1 jtc
240 1.1 jtc pos = HERE();
241 1.1 jtc switch (c) {
242 1.1 jtc case '(':
243 1.1 jtc REQUIRE(MORE(), REG_EPAREN);
244 1.1 jtc p->g->nsub++;
245 1.1 jtc subno = p->g->nsub;
246 1.1 jtc if (subno < NPAREN)
247 1.1 jtc p->pbegin[subno] = HERE();
248 1.1 jtc EMIT(OLPAREN, subno);
249 1.1 jtc if (!SEE(')'))
250 1.1 jtc p_ere(p, ')');
251 1.1 jtc if (subno < NPAREN) {
252 1.1 jtc p->pend[subno] = HERE();
253 1.1 jtc assert(p->pend[subno] != 0);
254 1.1 jtc }
255 1.1 jtc EMIT(ORPAREN, subno);
256 1.1 jtc MUSTEAT(')', REG_EPAREN);
257 1.1 jtc break;
258 1.1 jtc #ifndef POSIX_MISTAKE
259 1.1 jtc case ')': /* happens only if no current unmatched ( */
260 1.1 jtc /*
261 1.1 jtc * You may ask, why the ifndef? Because I didn't notice
262 1.1 jtc * this until slightly too late for 1003.2, and none of the
263 1.1 jtc * other 1003.2 regular-expression reviewers noticed it at
264 1.1 jtc * all. So an unmatched ) is legal POSIX, at least until
265 1.1 jtc * we can get it fixed.
266 1.1 jtc */
267 1.1 jtc SETERROR(REG_EPAREN);
268 1.1 jtc break;
269 1.1 jtc #endif
270 1.1 jtc case '^':
271 1.1 jtc EMIT(OBOL, 0);
272 1.1 jtc p->g->iflags |= USEBOL;
273 1.1 jtc p->g->nbol++;
274 1.1 jtc wascaret = 1;
275 1.1 jtc break;
276 1.1 jtc case '$':
277 1.1 jtc EMIT(OEOL, 0);
278 1.1 jtc p->g->iflags |= USEEOL;
279 1.1 jtc p->g->neol++;
280 1.1 jtc break;
281 1.1 jtc case '|':
282 1.1 jtc SETERROR(REG_EMPTY);
283 1.1 jtc break;
284 1.1 jtc case '*':
285 1.1 jtc case '+':
286 1.1 jtc case '?':
287 1.1 jtc SETERROR(REG_BADRPT);
288 1.1 jtc break;
289 1.1 jtc case '.':
290 1.1 jtc if (p->g->cflags®_NEWLINE)
291 1.1 jtc nonnewline(p);
292 1.1 jtc else
293 1.1 jtc EMIT(OANY, 0);
294 1.1 jtc break;
295 1.1 jtc case '[':
296 1.1 jtc p_bracket(p);
297 1.1 jtc break;
298 1.1 jtc case '\\':
299 1.1 jtc REQUIRE(MORE(), REG_EESCAPE);
300 1.1 jtc c = GETNEXT();
301 1.1 jtc ordinary(p, c);
302 1.1 jtc break;
303 1.1 jtc case '{': /* okay as ordinary except if digit follows */
304 1.1 jtc REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT);
305 1.1 jtc /* FALLTHROUGH */
306 1.1 jtc default:
307 1.1 jtc ordinary(p, c);
308 1.1 jtc break;
309 1.1 jtc }
310 1.1 jtc
311 1.1 jtc if (!MORE())
312 1.1 jtc return;
313 1.1 jtc c = PEEK();
314 1.1 jtc /* we call { a repetition if followed by a digit */
315 1.1 jtc if (!( c == '*' || c == '+' || c == '?' ||
316 1.1 jtc (c == '{' && MORE2() && isdigit(PEEK2())) ))
317 1.1 jtc return; /* no repetition, we're done */
318 1.1 jtc NEXT();
319 1.1 jtc
320 1.1 jtc REQUIRE(!wascaret, REG_BADRPT);
321 1.1 jtc switch (c) {
322 1.1 jtc case '*': /* implemented as +? */
323 1.1 jtc INSERT(OPLUS_, pos);
324 1.1 jtc ASTERN(O_PLUS, pos);
325 1.1 jtc INSERT(OQUEST_, pos);
326 1.1 jtc ASTERN(O_QUEST, pos);
327 1.1 jtc break;
328 1.1 jtc case '+':
329 1.1 jtc INSERT(OPLUS_, pos);
330 1.1 jtc ASTERN(O_PLUS, pos);
331 1.1 jtc break;
332 1.1 jtc case '?':
333 1.1 jtc INSERT(OQUEST_, pos);
334 1.1 jtc ASTERN(O_QUEST, pos);
335 1.1 jtc break;
336 1.1 jtc case '{':
337 1.1 jtc count = p_count(p);
338 1.1 jtc if (EAT(',')) {
339 1.1 jtc if (isdigit(PEEK())) {
340 1.1 jtc count2 = p_count(p);
341 1.1 jtc REQUIRE(count <= count2, REG_BADBR);
342 1.1 jtc } else /* single number with comma */
343 1.1 jtc count2 = INFINITY;
344 1.1 jtc } else /* just a single number */
345 1.1 jtc count2 = count;
346 1.1 jtc repeat(p, pos, count, count2);
347 1.1 jtc if (!EAT('}')) { /* error heuristics */
348 1.1 jtc while (MORE() && PEEK() != '}')
349 1.1 jtc NEXT();
350 1.1 jtc REQUIRE(MORE(), REG_EBRACE);
351 1.1 jtc SETERROR(REG_BADBR);
352 1.1 jtc }
353 1.1 jtc break;
354 1.1 jtc }
355 1.1 jtc
356 1.1 jtc if (!MORE())
357 1.1 jtc return;
358 1.1 jtc c = PEEK();
359 1.1 jtc if (!( c == '*' || c == '+' || c == '?' ||
360 1.1 jtc (c == '{' && MORE2() && isdigit(PEEK2())) ) )
361 1.1 jtc return;
362 1.1 jtc SETERROR(REG_BADRPT);
363 1.1 jtc }
364 1.1 jtc
365 1.1 jtc /*
366 1.1 jtc - p_str - string (no metacharacters) "parser"
367 1.1 jtc == static void p_str(register struct parse *p);
368 1.1 jtc */
369 1.1 jtc static void
370 1.1 jtc p_str(p)
371 1.1 jtc register struct parse *p;
372 1.1 jtc {
373 1.1 jtc REQUIRE(MORE(), REG_EMPTY);
374 1.1 jtc while (MORE())
375 1.1 jtc ordinary(p, GETNEXT());
376 1.1 jtc }
377 1.1 jtc
378 1.1 jtc /*
379 1.1 jtc - p_bre - BRE parser top level, anchoring and concatenation
380 1.1 jtc == static void p_bre(register struct parse *p, register int end1, \
381 1.1 jtc == register int end2);
382 1.1 jtc * Giving end1 as OUT essentially eliminates the end1/end2 check.
383 1.1 jtc *
384 1.1 jtc * This implementation is a bit of a kludge, in that a trailing $ is first
385 1.1 jtc * taken as an ordinary character and then revised to be an anchor. The
386 1.1 jtc * only undesirable side effect is that '$' gets included as a character
387 1.1 jtc * category in such cases. This is fairly harmless; not worth fixing.
388 1.1 jtc * The amount of lookahead needed to avoid this kludge is excessive.
389 1.1 jtc */
390 1.1 jtc static void
391 1.1 jtc p_bre(p, end1, end2)
392 1.1 jtc register struct parse *p;
393 1.1 jtc register int end1; /* first terminating character */
394 1.1 jtc register int end2; /* second terminating character */
395 1.1 jtc {
396 1.1 jtc register sopno start = HERE();
397 1.1 jtc register int first = 1; /* first subexpression? */
398 1.1 jtc register int wasdollar = 0;
399 1.1 jtc
400 1.1 jtc if (EAT('^')) {
401 1.1 jtc EMIT(OBOL, 0);
402 1.1 jtc p->g->iflags |= USEBOL;
403 1.1 jtc p->g->nbol++;
404 1.1 jtc }
405 1.1 jtc while (MORE() && !SEETWO(end1, end2)) {
406 1.1 jtc wasdollar = p_simp_re(p, first);
407 1.1 jtc first = 0;
408 1.1 jtc }
409 1.1 jtc if (wasdollar) { /* oops, that was a trailing anchor */
410 1.1 jtc DROP(1);
411 1.1 jtc EMIT(OEOL, 0);
412 1.1 jtc p->g->iflags |= USEEOL;
413 1.1 jtc p->g->neol++;
414 1.1 jtc }
415 1.1 jtc
416 1.1 jtc REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
417 1.1 jtc }
418 1.1 jtc
419 1.1 jtc /*
420 1.1 jtc - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
421 1.1 jtc == static int p_simp_re(register struct parse *p, int starordinary);
422 1.1 jtc */
423 1.1 jtc static int /* was the simple RE an unbackslashed $? */
424 1.1 jtc p_simp_re(p, starordinary)
425 1.1 jtc register struct parse *p;
426 1.1 jtc int starordinary; /* is a leading * an ordinary character? */
427 1.1 jtc {
428 1.1 jtc register int c;
429 1.1 jtc register int count;
430 1.1 jtc register int count2;
431 1.1 jtc register sopno pos;
432 1.1 jtc register int i;
433 1.1 jtc register sopno subno;
434 1.1 jtc # define BACKSL (1<<CHAR_BIT)
435 1.1 jtc
436 1.1 jtc pos = HERE(); /* repetion op, if any, covers from here */
437 1.1 jtc
438 1.1 jtc assert(MORE()); /* caller should have ensured this */
439 1.1 jtc c = GETNEXT();
440 1.1 jtc if (c == '\\') {
441 1.1 jtc REQUIRE(MORE(), REG_EESCAPE);
442 1.1 jtc c = BACKSL | (unsigned char)GETNEXT();
443 1.1 jtc }
444 1.1 jtc switch (c) {
445 1.1 jtc case '.':
446 1.1 jtc if (p->g->cflags®_NEWLINE)
447 1.1 jtc nonnewline(p);
448 1.1 jtc else
449 1.1 jtc EMIT(OANY, 0);
450 1.1 jtc break;
451 1.1 jtc case '[':
452 1.1 jtc p_bracket(p);
453 1.1 jtc break;
454 1.1 jtc case BACKSL|'{':
455 1.1 jtc SETERROR(REG_BADRPT);
456 1.1 jtc break;
457 1.1 jtc case BACKSL|'(':
458 1.1 jtc p->g->nsub++;
459 1.1 jtc subno = p->g->nsub;
460 1.1 jtc if (subno < NPAREN)
461 1.1 jtc p->pbegin[subno] = HERE();
462 1.1 jtc EMIT(OLPAREN, subno);
463 1.1 jtc /* the MORE here is an error heuristic */
464 1.1 jtc if (MORE() && !SEETWO('\\', ')'))
465 1.1 jtc p_bre(p, '\\', ')');
466 1.1 jtc if (subno < NPAREN) {
467 1.1 jtc p->pend[subno] = HERE();
468 1.1 jtc assert(p->pend[subno] != 0);
469 1.1 jtc }
470 1.1 jtc EMIT(ORPAREN, subno);
471 1.1 jtc REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
472 1.1 jtc break;
473 1.1 jtc case BACKSL|')': /* should not get here -- must be user */
474 1.1 jtc case BACKSL|'}':
475 1.1 jtc SETERROR(REG_EPAREN);
476 1.1 jtc break;
477 1.1 jtc case BACKSL|'1':
478 1.1 jtc case BACKSL|'2':
479 1.1 jtc case BACKSL|'3':
480 1.1 jtc case BACKSL|'4':
481 1.1 jtc case BACKSL|'5':
482 1.1 jtc case BACKSL|'6':
483 1.1 jtc case BACKSL|'7':
484 1.1 jtc case BACKSL|'8':
485 1.1 jtc case BACKSL|'9':
486 1.1 jtc i = (c&~BACKSL) - '0';
487 1.1 jtc assert(i < NPAREN);
488 1.1 jtc if (p->pend[i] != 0) {
489 1.1 jtc assert(i <= p->g->nsub);
490 1.1 jtc EMIT(OBACK_, i);
491 1.1 jtc assert(p->pbegin[i] != 0);
492 1.1 jtc assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
493 1.1 jtc assert(OP(p->strip[p->pend[i]]) == ORPAREN);
494 1.1 jtc (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
495 1.1 jtc EMIT(O_BACK, i);
496 1.1 jtc } else
497 1.1 jtc SETERROR(REG_ESUBREG);
498 1.1 jtc p->g->backrefs = 1;
499 1.1 jtc break;
500 1.1 jtc case '*':
501 1.1 jtc REQUIRE(starordinary, REG_BADRPT);
502 1.1 jtc /* FALLTHROUGH */
503 1.1 jtc default:
504 1.1 jtc ordinary(p, c &~ BACKSL);
505 1.1 jtc break;
506 1.1 jtc }
507 1.1 jtc
508 1.1 jtc if (EAT('*')) { /* implemented as +? */
509 1.1 jtc INSERT(OPLUS_, pos);
510 1.1 jtc ASTERN(O_PLUS, pos);
511 1.1 jtc INSERT(OQUEST_, pos);
512 1.1 jtc ASTERN(O_QUEST, pos);
513 1.1 jtc } else if (EATTWO('\\', '{')) {
514 1.1 jtc count = p_count(p);
515 1.1 jtc if (EAT(',')) {
516 1.1 jtc if (MORE() && isdigit(PEEK())) {
517 1.1 jtc count2 = p_count(p);
518 1.1 jtc REQUIRE(count <= count2, REG_BADBR);
519 1.1 jtc } else /* single number with comma */
520 1.1 jtc count2 = INFINITY;
521 1.1 jtc } else /* just a single number */
522 1.1 jtc count2 = count;
523 1.1 jtc repeat(p, pos, count, count2);
524 1.1 jtc if (!EATTWO('\\', '}')) { /* error heuristics */
525 1.1 jtc while (MORE() && !SEETWO('\\', '}'))
526 1.1 jtc NEXT();
527 1.1 jtc REQUIRE(MORE(), REG_EBRACE);
528 1.1 jtc SETERROR(REG_BADBR);
529 1.1 jtc }
530 1.1 jtc } else if (c == (unsigned char)'$') /* $ (but not \$) ends it */
531 1.1 jtc return(1);
532 1.1 jtc
533 1.1 jtc return(0);
534 1.1 jtc }
535 1.1 jtc
536 1.1 jtc /*
537 1.1 jtc - p_count - parse a repetition count
538 1.1 jtc == static int p_count(register struct parse *p);
539 1.1 jtc */
540 1.1 jtc static int /* the value */
541 1.1 jtc p_count(p)
542 1.1 jtc register struct parse *p;
543 1.1 jtc {
544 1.1 jtc register int count = 0;
545 1.1 jtc register int ndigits = 0;
546 1.1 jtc
547 1.1 jtc while (MORE() && isdigit(PEEK()) && count <= DUPMAX) {
548 1.1 jtc count = count*10 + (GETNEXT() - '0');
549 1.1 jtc ndigits++;
550 1.1 jtc }
551 1.1 jtc
552 1.1 jtc REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
553 1.1 jtc return(count);
554 1.1 jtc }
555 1.1 jtc
556 1.1 jtc /*
557 1.1 jtc - p_bracket - parse a bracketed character list
558 1.1 jtc == static void p_bracket(register struct parse *p);
559 1.1 jtc *
560 1.1 jtc * Note a significant property of this code: if the allocset() did SETERROR,
561 1.1 jtc * no set operations are done.
562 1.1 jtc */
563 1.1 jtc static void
564 1.1 jtc p_bracket(p)
565 1.1 jtc register struct parse *p;
566 1.1 jtc {
567 1.1 jtc register char c;
568 1.1 jtc register cset *cs = allocset(p);
569 1.1 jtc register int invert = 0;
570 1.1 jtc
571 1.1 jtc /* Dept of Truly Sickening Special-Case Kludges */
572 1.1 jtc if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
573 1.1 jtc EMIT(OBOW, 0);
574 1.1 jtc NEXTn(6);
575 1.1 jtc return;
576 1.1 jtc }
577 1.1 jtc if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
578 1.1 jtc EMIT(OEOW, 0);
579 1.1 jtc NEXTn(6);
580 1.1 jtc return;
581 1.1 jtc }
582 1.1 jtc
583 1.1 jtc if (EAT('^'))
584 1.1 jtc invert++; /* make note to invert set at end */
585 1.1 jtc if (EAT(']'))
586 1.1 jtc CHadd(cs, ']');
587 1.1 jtc else if (EAT('-'))
588 1.1 jtc CHadd(cs, '-');
589 1.1 jtc while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
590 1.1 jtc p_b_term(p, cs);
591 1.1 jtc if (EAT('-'))
592 1.1 jtc CHadd(cs, '-');
593 1.1 jtc MUSTEAT(']', REG_EBRACK);
594 1.1 jtc
595 1.1 jtc if (p->error != 0) /* don't mess things up further */
596 1.1 jtc return;
597 1.1 jtc
598 1.1 jtc if (p->g->cflags®_ICASE) {
599 1.1 jtc register int i;
600 1.1 jtc register int ci;
601 1.1 jtc
602 1.1 jtc for (i = p->g->csetsize - 1; i >= 0; i--)
603 1.1 jtc if (CHIN(cs, i) && isalpha(i)) {
604 1.1 jtc ci = othercase(i);
605 1.1 jtc if (ci != i)
606 1.1 jtc CHadd(cs, ci);
607 1.1 jtc }
608 1.1 jtc if (cs->multis != NULL)
609 1.1 jtc mccase(cs);
610 1.1 jtc }
611 1.1 jtc if (invert) {
612 1.1 jtc register int i;
613 1.1 jtc
614 1.1 jtc for (i = p->g->csetsize - 1; i >= 0; i--)
615 1.1 jtc if (CHIN(cs, i))
616 1.1 jtc CHsub(cs, i);
617 1.1 jtc else
618 1.1 jtc CHadd(cs, i);
619 1.1 jtc if (p->g->cflags®_NEWLINE)
620 1.1 jtc CHsub(cs, '\n');
621 1.1 jtc if (cs->multis != NULL)
622 1.1 jtc mcinvert(cs);
623 1.1 jtc }
624 1.1 jtc
625 1.1 jtc assert(cs->multis == NULL); /* xxx */
626 1.1 jtc
627 1.1 jtc if (nch(p, cs) == 1) { /* optimize singleton sets */
628 1.1 jtc ordinary(p, firstch(p, cs));
629 1.1 jtc freeset(p, cs);
630 1.1 jtc } else
631 1.1 jtc EMIT(OANYOF, freezeset(p, cs));
632 1.1 jtc }
633 1.1 jtc
634 1.1 jtc /*
635 1.1 jtc - p_b_term - parse one term of a bracketed character list
636 1.1 jtc == static void p_b_term(register struct parse *p, register cset *cs);
637 1.1 jtc */
638 1.1 jtc static void
639 1.1 jtc p_b_term(p, cs)
640 1.1 jtc register struct parse *p;
641 1.1 jtc register cset *cs;
642 1.1 jtc {
643 1.1 jtc register char c;
644 1.1 jtc register char start, finish;
645 1.1 jtc register int i;
646 1.1 jtc
647 1.1 jtc /* classify what we've got */
648 1.1 jtc switch ((MORE()) ? PEEK() : '\0') {
649 1.1 jtc case '[':
650 1.1 jtc c = (MORE2()) ? PEEK2() : '\0';
651 1.1 jtc break;
652 1.1 jtc case '-':
653 1.1 jtc SETERROR(REG_ERANGE);
654 1.1 jtc return; /* NOTE RETURN */
655 1.1 jtc break;
656 1.1 jtc default:
657 1.1 jtc c = '\0';
658 1.1 jtc break;
659 1.1 jtc }
660 1.1 jtc
661 1.1 jtc switch (c) {
662 1.1 jtc case ':': /* character class */
663 1.1 jtc NEXT2();
664 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
665 1.1 jtc c = PEEK();
666 1.1 jtc REQUIRE(c != '-' && c != ']', REG_ECTYPE);
667 1.1 jtc p_b_cclass(p, cs);
668 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
669 1.1 jtc REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
670 1.1 jtc break;
671 1.1 jtc case '=': /* equivalence class */
672 1.1 jtc NEXT2();
673 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
674 1.1 jtc c = PEEK();
675 1.1 jtc REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
676 1.1 jtc p_b_eclass(p, cs);
677 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
678 1.1 jtc REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
679 1.1 jtc break;
680 1.1 jtc default: /* symbol, ordinary character, or range */
681 1.1 jtc /* xxx revision needed for multichar stuff */
682 1.1 jtc start = p_b_symbol(p);
683 1.1 jtc if (SEE('-') && MORE2() && PEEK2() != ']') {
684 1.1 jtc /* range */
685 1.1 jtc NEXT();
686 1.1 jtc if (EAT('-'))
687 1.1 jtc finish = '-';
688 1.1 jtc else
689 1.1 jtc finish = p_b_symbol(p);
690 1.1 jtc } else
691 1.1 jtc finish = start;
692 1.1 jtc /* xxx what about signed chars here... */
693 1.1 jtc REQUIRE(start <= finish, REG_ERANGE);
694 1.1 jtc for (i = start; i <= finish; i++)
695 1.1 jtc CHadd(cs, i);
696 1.1 jtc break;
697 1.1 jtc }
698 1.1 jtc }
699 1.1 jtc
700 1.1 jtc /*
701 1.1 jtc - p_b_cclass - parse a character-class name and deal with it
702 1.1 jtc == static void p_b_cclass(register struct parse *p, register cset *cs);
703 1.1 jtc */
704 1.1 jtc static void
705 1.1 jtc p_b_cclass(p, cs)
706 1.1 jtc register struct parse *p;
707 1.1 jtc register cset *cs;
708 1.1 jtc {
709 1.1 jtc register char *sp = p->next;
710 1.1 jtc register struct cclass *cp;
711 1.1 jtc register size_t len;
712 1.1 jtc register char *u;
713 1.1 jtc register char c;
714 1.1 jtc
715 1.1 jtc while (MORE() && isalpha(PEEK()))
716 1.1 jtc NEXT();
717 1.1 jtc len = p->next - sp;
718 1.1 jtc for (cp = cclasses; cp->name != NULL; cp++)
719 1.1 jtc if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
720 1.1 jtc break;
721 1.1 jtc if (cp->name == NULL) {
722 1.1 jtc /* oops, didn't find it */
723 1.1 jtc SETERROR(REG_ECTYPE);
724 1.1 jtc return;
725 1.1 jtc }
726 1.1 jtc
727 1.1 jtc u = cp->chars;
728 1.1 jtc while ((c = *u++) != '\0')
729 1.1 jtc CHadd(cs, c);
730 1.1 jtc for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
731 1.1 jtc MCadd(cs, u);
732 1.1 jtc }
733 1.1 jtc
734 1.1 jtc /*
735 1.1 jtc - p_b_eclass - parse an equivalence-class name and deal with it
736 1.1 jtc == static void p_b_eclass(register struct parse *p, register cset *cs);
737 1.1 jtc *
738 1.1 jtc * This implementation is incomplete. xxx
739 1.1 jtc */
740 1.1 jtc static void
741 1.1 jtc p_b_eclass(p, cs)
742 1.1 jtc register struct parse *p;
743 1.1 jtc register cset *cs;
744 1.1 jtc {
745 1.1 jtc register char c;
746 1.1 jtc
747 1.1 jtc c = p_b_coll_elem(p, '=');
748 1.1 jtc CHadd(cs, c);
749 1.1 jtc }
750 1.1 jtc
751 1.1 jtc /*
752 1.1 jtc - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
753 1.1 jtc == static char p_b_symbol(register struct parse *p);
754 1.1 jtc */
755 1.1 jtc static char /* value of symbol */
756 1.1 jtc p_b_symbol(p)
757 1.1 jtc register struct parse *p;
758 1.1 jtc {
759 1.1 jtc register char value;
760 1.1 jtc
761 1.1 jtc REQUIRE(MORE(), REG_EBRACK);
762 1.1 jtc if (!EATTWO('[', '.'))
763 1.1 jtc return(GETNEXT());
764 1.1 jtc
765 1.1 jtc /* collating symbol */
766 1.1 jtc value = p_b_coll_elem(p, '.');
767 1.1 jtc REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
768 1.1 jtc return(value);
769 1.1 jtc }
770 1.1 jtc
771 1.1 jtc /*
772 1.1 jtc - p_b_coll_elem - parse a collating-element name and look it up
773 1.1 jtc == static char p_b_coll_elem(register struct parse *p, int endc);
774 1.1 jtc */
775 1.1 jtc static char /* value of collating element */
776 1.1 jtc p_b_coll_elem(p, endc)
777 1.1 jtc register struct parse *p;
778 1.1 jtc int endc; /* name ended by endc,']' */
779 1.1 jtc {
780 1.1 jtc register char *sp = p->next;
781 1.1 jtc register struct cname *cp;
782 1.1 jtc register int len;
783 1.1 jtc register char c;
784 1.1 jtc
785 1.1 jtc while (MORE() && !SEETWO(endc, ']'))
786 1.1 jtc NEXT();
787 1.1 jtc if (!MORE()) {
788 1.1 jtc SETERROR(REG_EBRACK);
789 1.1 jtc return(0);
790 1.1 jtc }
791 1.1 jtc len = p->next - sp;
792 1.1 jtc for (cp = cnames; cp->name != NULL; cp++)
793 1.1 jtc if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
794 1.1 jtc return(cp->code); /* known name */
795 1.1 jtc if (len == 1)
796 1.1 jtc return(*sp); /* single character */
797 1.1 jtc SETERROR(REG_ECOLLATE); /* neither */
798 1.1 jtc return(0);
799 1.1 jtc }
800 1.1 jtc
801 1.1 jtc /*
802 1.1 jtc - othercase - return the case counterpart of an alphabetic
803 1.1 jtc == static char othercase(int ch);
804 1.1 jtc */
805 1.1 jtc static char /* if no counterpart, return ch */
806 1.1 jtc othercase(ch)
807 1.1 jtc int ch;
808 1.1 jtc {
809 1.1 jtc assert(isalpha(ch));
810 1.1 jtc if (isupper(ch))
811 1.1 jtc return(tolower(ch));
812 1.1 jtc else if (islower(ch))
813 1.1 jtc return(toupper(ch));
814 1.1 jtc else /* peculiar, but could happen */
815 1.1 jtc return(ch);
816 1.1 jtc }
817 1.1 jtc
818 1.1 jtc /*
819 1.1 jtc - bothcases - emit a dualcase version of a two-case character
820 1.1 jtc == static void bothcases(register struct parse *p, int ch);
821 1.1 jtc *
822 1.1 jtc * Boy, is this implementation ever a kludge...
823 1.1 jtc */
824 1.1 jtc static void
825 1.1 jtc bothcases(p, ch)
826 1.1 jtc register struct parse *p;
827 1.1 jtc int ch;
828 1.1 jtc {
829 1.1 jtc register char *oldnext = p->next;
830 1.1 jtc register char *oldend = p->end;
831 1.1 jtc char bracket[3];
832 1.1 jtc
833 1.1 jtc assert(othercase(ch) != ch); /* p_bracket() would recurse */
834 1.1 jtc p->next = bracket;
835 1.1 jtc p->end = bracket+2;
836 1.1 jtc bracket[0] = ch;
837 1.1 jtc bracket[1] = ']';
838 1.1 jtc bracket[2] = '\0';
839 1.1 jtc p_bracket(p);
840 1.1 jtc assert(p->next == bracket+2);
841 1.1 jtc p->next = oldnext;
842 1.1 jtc p->end = oldend;
843 1.1 jtc }
844 1.1 jtc
845 1.1 jtc /*
846 1.1 jtc - ordinary - emit an ordinary character
847 1.1 jtc == static void ordinary(register struct parse *p, register int ch);
848 1.1 jtc */
849 1.1 jtc static void
850 1.1 jtc ordinary(p, ch)
851 1.1 jtc register struct parse *p;
852 1.1 jtc register int ch;
853 1.1 jtc {
854 1.1 jtc register cat_t *cap = p->g->categories;
855 1.1 jtc
856 1.1 jtc if ((p->g->cflags®_ICASE) && isalpha(ch) && othercase(ch) != ch)
857 1.1 jtc bothcases(p, ch);
858 1.1 jtc else {
859 1.1 jtc EMIT(OCHAR, (unsigned char)ch);
860 1.1 jtc if (cap[ch] == 0)
861 1.1 jtc cap[ch] = p->g->ncategories++;
862 1.1 jtc }
863 1.1 jtc }
864 1.1 jtc
865 1.1 jtc /*
866 1.1 jtc - nonnewline - emit REG_NEWLINE version of OANY
867 1.1 jtc == static void nonnewline(register struct parse *p);
868 1.1 jtc *
869 1.1 jtc * Boy, is this implementation ever a kludge...
870 1.1 jtc */
871 1.1 jtc static void
872 1.1 jtc nonnewline(p)
873 1.1 jtc register struct parse *p;
874 1.1 jtc {
875 1.1 jtc register char *oldnext = p->next;
876 1.1 jtc register char *oldend = p->end;
877 1.1 jtc char bracket[4];
878 1.1 jtc
879 1.1 jtc p->next = bracket;
880 1.1 jtc p->end = bracket+3;
881 1.1 jtc bracket[0] = '^';
882 1.1 jtc bracket[1] = '\n';
883 1.1 jtc bracket[2] = ']';
884 1.1 jtc bracket[3] = '\0';
885 1.1 jtc p_bracket(p);
886 1.1 jtc assert(p->next == bracket+3);
887 1.1 jtc p->next = oldnext;
888 1.1 jtc p->end = oldend;
889 1.1 jtc }
890 1.1 jtc
891 1.1 jtc /*
892 1.1 jtc - repeat - generate code for a bounded repetition, recursively if needed
893 1.1 jtc == static void repeat(register struct parse *p, sopno start, int from, int to);
894 1.1 jtc */
895 1.1 jtc static void
896 1.1 jtc repeat(p, start, from, to)
897 1.1 jtc register struct parse *p;
898 1.1 jtc sopno start; /* operand from here to end of strip */
899 1.1 jtc int from; /* repeated from this number */
900 1.1 jtc int to; /* to this number of times (maybe INFINITY) */
901 1.1 jtc {
902 1.1 jtc register sopno finish = HERE();
903 1.1 jtc # define N 2
904 1.1 jtc # define INF 3
905 1.1 jtc # define REP(f, t) ((f)*8 + (t))
906 1.1 jtc # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
907 1.1 jtc register sopno copy;
908 1.1 jtc
909 1.1 jtc if (p->error != 0) /* head off possible runaway recursion */
910 1.1 jtc return;
911 1.1 jtc
912 1.1 jtc assert(from <= to);
913 1.1 jtc
914 1.1 jtc switch (REP(MAP(from), MAP(to))) {
915 1.1 jtc case REP(0, 0): /* must be user doing this */
916 1.1 jtc DROP(finish-start); /* drop the operand */
917 1.1 jtc break;
918 1.1 jtc case REP(0, 1): /* as x{1,1}? */
919 1.1 jtc case REP(0, N): /* as x{1,n}? */
920 1.1 jtc case REP(0, INF): /* as x{1,}? */
921 1.1 jtc INSERT(OQUEST_, start); /* offset is wrong... */
922 1.1 jtc repeat(p, start+1, 1, to);
923 1.1 jtc AHEAD(start); /* ... fix it */
924 1.1 jtc ASTERN(O_QUEST, start);
925 1.1 jtc break;
926 1.1 jtc case REP(1, 1): /* trivial case */
927 1.1 jtc /* done */
928 1.1 jtc break;
929 1.1 jtc case REP(1, N): /* as x?x{1,n-1} */
930 1.1 jtc INSERT(OQUEST_, start);
931 1.1 jtc ASTERN(O_QUEST, start);
932 1.1 jtc copy = dupl(p, start+1, finish+1);
933 1.1 jtc assert(copy == finish+2);
934 1.1 jtc repeat(p, copy, 1, to-1);
935 1.1 jtc break;
936 1.1 jtc case REP(1, INF): /* as x+ */
937 1.1 jtc INSERT(OPLUS_, start);
938 1.1 jtc ASTERN(O_PLUS, start);
939 1.1 jtc break;
940 1.1 jtc case REP(N, N): /* as xx{m-1,n-1} */
941 1.1 jtc copy = dupl(p, start, finish);
942 1.1 jtc repeat(p, copy, from-1, to-1);
943 1.1 jtc break;
944 1.1 jtc case REP(N, INF): /* as xx{n-1,INF} */
945 1.1 jtc copy = dupl(p, start, finish);
946 1.1 jtc repeat(p, copy, from-1, to);
947 1.1 jtc break;
948 1.1 jtc default: /* "can't happen" */
949 1.1 jtc SETERROR(REG_ASSERT); /* just in case */
950 1.1 jtc break;
951 1.1 jtc }
952 1.1 jtc }
953 1.1 jtc
954 1.1 jtc /*
955 1.1 jtc - seterr - set an error condition
956 1.1 jtc == static int seterr(register struct parse *p, int e);
957 1.1 jtc */
958 1.1 jtc static int /* useless but makes type checking happy */
959 1.1 jtc seterr(p, e)
960 1.1 jtc register struct parse *p;
961 1.1 jtc int e;
962 1.1 jtc {
963 1.1 jtc if (p->error == 0) /* keep earliest error condition */
964 1.1 jtc p->error = e;
965 1.1 jtc p->next = nuls; /* try to bring things to a halt */
966 1.1 jtc p->end = nuls;
967 1.1 jtc return(0); /* make the return value well-defined */
968 1.1 jtc }
969 1.1 jtc
970 1.1 jtc /*
971 1.1 jtc - allocset - allocate a set of characters for []
972 1.1 jtc == static cset *allocset(register struct parse *p);
973 1.1 jtc */
974 1.1 jtc static cset *
975 1.1 jtc allocset(p)
976 1.1 jtc register struct parse *p;
977 1.1 jtc {
978 1.1 jtc register int no = p->g->ncsets++;
979 1.1 jtc register size_t nc;
980 1.1 jtc register size_t nbytes;
981 1.1 jtc register cset *cs;
982 1.1 jtc register size_t css = (size_t)p->g->csetsize;
983 1.1 jtc
984 1.1 jtc if (no >= p->ncsalloc) { /* need another column of space */
985 1.1 jtc p->ncsalloc += CHAR_BIT;
986 1.1 jtc nc = p->ncsalloc;
987 1.1 jtc assert(nc % CHAR_BIT == 0);
988 1.1 jtc nbytes = nc / CHAR_BIT * css;
989 1.1 jtc if (p->g->sets == NULL)
990 1.1 jtc p->g->sets = (cset *)malloc(nc * sizeof(cset));
991 1.1 jtc else
992 1.1 jtc p->g->sets = (cset *)realloc((char *)p->g->sets,
993 1.1 jtc nc * sizeof(cset));
994 1.1 jtc if (p->g->setbits == NULL)
995 1.1 jtc p->g->setbits = (uchar *)malloc(nbytes);
996 1.1 jtc else
997 1.1 jtc p->g->setbits = (uchar *)realloc((char *)p->g->setbits,
998 1.1 jtc nbytes);
999 1.1 jtc if (p->g->sets != NULL && p->g->setbits != NULL)
1000 1.1 jtc (void) memset((char *)p->g->setbits + (nbytes - css),
1001 1.1 jtc 0, css);
1002 1.1 jtc else {
1003 1.1 jtc no = 0;
1004 1.1 jtc SETERROR(REG_ESPACE);
1005 1.1 jtc /* caller's responsibility not to do set ops */
1006 1.1 jtc }
1007 1.1 jtc }
1008 1.1 jtc
1009 1.1 jtc assert(p->g->sets != NULL); /* xxx */
1010 1.1 jtc cs = &p->g->sets[no];
1011 1.1 jtc cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1012 1.1 jtc cs->mask = 1 << ((no) % CHAR_BIT);
1013 1.1 jtc cs->hash = 0;
1014 1.1 jtc cs->smultis = 0;
1015 1.1 jtc cs->multis = NULL;
1016 1.1 jtc
1017 1.1 jtc return(cs);
1018 1.1 jtc }
1019 1.1 jtc
1020 1.1 jtc /*
1021 1.1 jtc - freeset - free a now-unused set
1022 1.1 jtc == static void freeset(register struct parse *p, register cset *cs);
1023 1.1 jtc */
1024 1.1 jtc static void
1025 1.1 jtc freeset(p, cs)
1026 1.1 jtc register struct parse *p;
1027 1.1 jtc register cset *cs;
1028 1.1 jtc {
1029 1.1 jtc register int i;
1030 1.1 jtc register cset *top = &p->g->sets[p->g->ncsets];
1031 1.1 jtc register size_t css = (size_t)p->g->csetsize;
1032 1.1 jtc
1033 1.1 jtc for (i = 0; i < css; i++)
1034 1.1 jtc CHsub(cs, i);
1035 1.1 jtc if (cs == top-1) /* recover only the easy case */
1036 1.1 jtc p->g->ncsets--;
1037 1.1 jtc }
1038 1.1 jtc
1039 1.1 jtc /*
1040 1.1 jtc - freezeset - final processing on a set of characters
1041 1.1 jtc == static int freezeset(register struct parse *p, register cset *cs);
1042 1.1 jtc *
1043 1.1 jtc * The main task here is merging identical sets. This is usually a waste
1044 1.1 jtc * of time (although the hash code minimizes the overhead), but can win
1045 1.1 jtc * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1046 1.1 jtc * is done using addition rather than xor -- all ASCII [aA] sets xor to
1047 1.1 jtc * the same value!
1048 1.1 jtc */
1049 1.1 jtc static int /* set number */
1050 1.1 jtc freezeset(p, cs)
1051 1.1 jtc register struct parse *p;
1052 1.1 jtc register cset *cs;
1053 1.1 jtc {
1054 1.1 jtc register uchar h = cs->hash;
1055 1.1 jtc register int i;
1056 1.1 jtc register cset *top = &p->g->sets[p->g->ncsets];
1057 1.1 jtc register cset *cs2;
1058 1.1 jtc register size_t css = (size_t)p->g->csetsize;
1059 1.1 jtc
1060 1.1 jtc /* look for an earlier one which is the same */
1061 1.1 jtc for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1062 1.1 jtc if (cs2->hash == h && cs2 != cs) {
1063 1.1 jtc /* maybe */
1064 1.1 jtc for (i = 0; i < css; i++)
1065 1.1 jtc if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1066 1.1 jtc break; /* no */
1067 1.1 jtc if (i == css)
1068 1.1 jtc break; /* yes */
1069 1.1 jtc }
1070 1.1 jtc
1071 1.1 jtc if (cs2 < top) { /* found one */
1072 1.1 jtc freeset(p, cs);
1073 1.1 jtc cs = cs2;
1074 1.1 jtc }
1075 1.1 jtc
1076 1.1 jtc return((int)(cs - p->g->sets));
1077 1.1 jtc }
1078 1.1 jtc
1079 1.1 jtc /*
1080 1.1 jtc - firstch - return first character in a set (which must have at least one)
1081 1.1 jtc == static int firstch(register struct parse *p, register cset *cs);
1082 1.1 jtc */
1083 1.1 jtc static int /* character; there is no "none" value */
1084 1.1 jtc firstch(p, cs)
1085 1.1 jtc register struct parse *p;
1086 1.1 jtc register cset *cs;
1087 1.1 jtc {
1088 1.1 jtc register int i;
1089 1.1 jtc register size_t css = (size_t)p->g->csetsize;
1090 1.1 jtc
1091 1.1 jtc for (i = 0; i < css; i++)
1092 1.1 jtc if (CHIN(cs, i))
1093 1.1 jtc return((char)i);
1094 1.1 jtc assert(never);
1095 1.1 jtc return(0); /* arbitrary */
1096 1.1 jtc }
1097 1.1 jtc
1098 1.1 jtc /*
1099 1.1 jtc - nch - number of characters in a set
1100 1.1 jtc == static int nch(register struct parse *p, register cset *cs);
1101 1.1 jtc */
1102 1.1 jtc static int
1103 1.1 jtc nch(p, cs)
1104 1.1 jtc register struct parse *p;
1105 1.1 jtc register cset *cs;
1106 1.1 jtc {
1107 1.1 jtc register int i;
1108 1.1 jtc register size_t css = (size_t)p->g->csetsize;
1109 1.1 jtc register int n = 0;
1110 1.1 jtc
1111 1.1 jtc for (i = 0; i < css; i++)
1112 1.1 jtc if (CHIN(cs, i))
1113 1.1 jtc n++;
1114 1.1 jtc return(n);
1115 1.1 jtc }
1116 1.1 jtc
1117 1.1 jtc /*
1118 1.1 jtc - mcadd - add a collating element to a cset
1119 1.1 jtc == static void mcadd(register struct parse *p, register cset *cs, \
1120 1.1 jtc == register char *cp);
1121 1.1 jtc */
1122 1.1 jtc static void
1123 1.1 jtc mcadd(p, cs, cp)
1124 1.1 jtc register struct parse *p;
1125 1.1 jtc register cset *cs;
1126 1.1 jtc register char *cp;
1127 1.1 jtc {
1128 1.1 jtc register size_t oldend = cs->smultis;
1129 1.1 jtc
1130 1.1 jtc cs->smultis += strlen(cp) + 1;
1131 1.1 jtc if (cs->multis == NULL)
1132 1.1 jtc cs->multis = malloc(cs->smultis);
1133 1.1 jtc else
1134 1.1 jtc cs->multis = realloc(cs->multis, cs->smultis);
1135 1.1 jtc if (cs->multis == NULL) {
1136 1.1 jtc SETERROR(REG_ESPACE);
1137 1.1 jtc return;
1138 1.1 jtc }
1139 1.1 jtc
1140 1.1 jtc (void) strcpy(cs->multis + oldend - 1, cp);
1141 1.1 jtc cs->multis[cs->smultis - 1] = '\0';
1142 1.1 jtc }
1143 1.1 jtc
1144 1.1 jtc /*
1145 1.1 jtc - mcsub - subtract a collating element from a cset
1146 1.1 jtc == static void mcsub(register cset *cs, register char *cp);
1147 1.1 jtc */
1148 1.1 jtc static void
1149 1.1 jtc mcsub(cs, cp)
1150 1.1 jtc register cset *cs;
1151 1.1 jtc register char *cp;
1152 1.1 jtc {
1153 1.1 jtc register char *fp = mcfind(cs, cp);
1154 1.1 jtc register size_t len = strlen(fp);
1155 1.1 jtc
1156 1.1 jtc assert(fp != NULL);
1157 1.1 jtc (void) memmove(fp, fp + len + 1,
1158 1.1 jtc cs->smultis - (fp + len + 1 - cs->multis));
1159 1.1 jtc cs->smultis -= len;
1160 1.1 jtc
1161 1.1 jtc if (cs->smultis == 0) {
1162 1.1 jtc free(cs->multis);
1163 1.1 jtc cs->multis = NULL;
1164 1.1 jtc return;
1165 1.1 jtc }
1166 1.1 jtc
1167 1.1 jtc cs->multis = realloc(cs->multis, cs->smultis);
1168 1.1 jtc assert(cs->multis != NULL);
1169 1.1 jtc }
1170 1.1 jtc
1171 1.1 jtc /*
1172 1.1 jtc - mcin - is a collating element in a cset?
1173 1.1 jtc == static int mcin(register cset *cs, register char *cp);
1174 1.1 jtc */
1175 1.1 jtc static int
1176 1.1 jtc mcin(cs, cp)
1177 1.1 jtc register cset *cs;
1178 1.1 jtc register char *cp;
1179 1.1 jtc {
1180 1.1 jtc return(mcfind(cs, cp) != NULL);
1181 1.1 jtc }
1182 1.1 jtc
1183 1.1 jtc /*
1184 1.1 jtc - mcfind - find a collating element in a cset
1185 1.1 jtc == static char *mcfind(register cset *cs, register char *cp);
1186 1.1 jtc */
1187 1.1 jtc static char *
1188 1.1 jtc mcfind(cs, cp)
1189 1.1 jtc register cset *cs;
1190 1.1 jtc register char *cp;
1191 1.1 jtc {
1192 1.1 jtc register char *p;
1193 1.1 jtc
1194 1.1 jtc if (cs->multis == NULL)
1195 1.1 jtc return(NULL);
1196 1.1 jtc for (p = cs->multis; *p != '\0'; p += strlen(p) + 1)
1197 1.1 jtc if (strcmp(cp, p) == 0)
1198 1.1 jtc return(p);
1199 1.1 jtc return(NULL);
1200 1.1 jtc }
1201 1.1 jtc
1202 1.1 jtc /*
1203 1.1 jtc - mcinvert - invert the list of collating elements in a cset
1204 1.1 jtc == static void mcinvert(register cset *cs);
1205 1.1 jtc *
1206 1.1 jtc * This would have to know the set of possibilities. Implementation
1207 1.1 jtc * is deferred.
1208 1.1 jtc */
1209 1.1 jtc static void
1210 1.1 jtc mcinvert(cs)
1211 1.1 jtc register cset *cs;
1212 1.1 jtc {
1213 1.1 jtc assert(cs->multis == NULL); /* xxx */
1214 1.1 jtc }
1215 1.1 jtc
1216 1.1 jtc /*
1217 1.1 jtc - mccase - add case counterparts of the list of collating elements in a cset
1218 1.1 jtc == static void mccase(register cset *cs);
1219 1.1 jtc *
1220 1.1 jtc * This would have to know the set of possibilities. Implementation
1221 1.1 jtc * is deferred.
1222 1.1 jtc */
1223 1.1 jtc static void
1224 1.1 jtc mccase(cs)
1225 1.1 jtc register cset *cs;
1226 1.1 jtc {
1227 1.1 jtc assert(cs->multis == NULL); /* xxx */
1228 1.1 jtc }
1229 1.1 jtc
1230 1.1 jtc /*
1231 1.1 jtc - isinsets - is this character in any sets?
1232 1.1 jtc == static int isinsets(register struct re_guts *g, int c);
1233 1.1 jtc */
1234 1.1 jtc static int /* predicate */
1235 1.1 jtc isinsets(g, c)
1236 1.1 jtc register struct re_guts *g;
1237 1.1 jtc int c;
1238 1.1 jtc {
1239 1.1 jtc register uchar *col;
1240 1.1 jtc register int i;
1241 1.1 jtc register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1242 1.1 jtc register unsigned uc = (unsigned char)c;
1243 1.1 jtc
1244 1.1 jtc for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1245 1.1 jtc if (col[uc] != 0)
1246 1.1 jtc return(1);
1247 1.1 jtc return(0);
1248 1.1 jtc }
1249 1.1 jtc
1250 1.1 jtc /*
1251 1.1 jtc - samesets - are these two characters in exactly the same sets?
1252 1.1 jtc == static int samesets(register struct re_guts *g, int c1, int c2);
1253 1.1 jtc */
1254 1.1 jtc static int /* predicate */
1255 1.1 jtc samesets(g, c1, c2)
1256 1.1 jtc register struct re_guts *g;
1257 1.1 jtc int c1;
1258 1.1 jtc int c2;
1259 1.1 jtc {
1260 1.1 jtc register uchar *col;
1261 1.1 jtc register int i;
1262 1.1 jtc register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1263 1.1 jtc register unsigned uc1 = (unsigned char)c1;
1264 1.1 jtc register unsigned uc2 = (unsigned char)c2;
1265 1.1 jtc
1266 1.1 jtc for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1267 1.1 jtc if (col[uc1] != col[uc2])
1268 1.1 jtc return(0);
1269 1.1 jtc return(1);
1270 1.1 jtc }
1271 1.1 jtc
1272 1.1 jtc /*
1273 1.1 jtc - categorize - sort out character categories
1274 1.1 jtc == static void categorize(struct parse *p, register struct re_guts *g);
1275 1.1 jtc */
1276 1.1 jtc static void
1277 1.1 jtc categorize(p, g)
1278 1.1 jtc struct parse *p;
1279 1.1 jtc register struct re_guts *g;
1280 1.1 jtc {
1281 1.1 jtc register cat_t *cats = g->categories;
1282 1.1 jtc register int c;
1283 1.1 jtc register int c2;
1284 1.1 jtc register cat_t cat;
1285 1.1 jtc
1286 1.1 jtc /* avoid making error situations worse */
1287 1.1 jtc if (p->error != 0)
1288 1.1 jtc return;
1289 1.1 jtc
1290 1.1 jtc for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1291 1.1 jtc if (cats[c] == 0 && isinsets(g, c)) {
1292 1.1 jtc cat = g->ncategories++;
1293 1.1 jtc cats[c] = cat;
1294 1.1 jtc for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1295 1.1 jtc if (cats[c2] == 0 && samesets(g, c, c2))
1296 1.1 jtc cats[c2] = cat;
1297 1.1 jtc }
1298 1.1 jtc }
1299 1.1 jtc
1300 1.1 jtc /*
1301 1.1 jtc - dupl - emit a duplicate of a bunch of sops
1302 1.1 jtc == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1303 1.1 jtc */
1304 1.1 jtc static sopno /* start of duplicate */
1305 1.1 jtc dupl(p, start, finish)
1306 1.1 jtc register struct parse *p;
1307 1.1 jtc sopno start; /* from here */
1308 1.1 jtc sopno finish; /* to this less one */
1309 1.1 jtc {
1310 1.1 jtc register sopno ret = HERE();
1311 1.1 jtc register sopno len = finish - start;
1312 1.1 jtc
1313 1.1 jtc assert(finish >= start);
1314 1.1 jtc if (len == 0)
1315 1.1 jtc return(ret);
1316 1.1 jtc enlarge(p, p->ssize + len); /* this many unexpected additions */
1317 1.1 jtc assert(p->ssize >= p->slen + len);
1318 1.1 jtc (void) memcpy((char *)(p->strip + p->slen),
1319 1.1 jtc (char *)(p->strip + start), (size_t)len*sizeof(sop));
1320 1.1 jtc p->slen += len;
1321 1.1 jtc return(ret);
1322 1.1 jtc }
1323 1.1 jtc
1324 1.1 jtc /*
1325 1.1 jtc - doemit - emit a strip operator
1326 1.1 jtc == static void doemit(register struct parse *p, sop op, size_t opnd);
1327 1.1 jtc *
1328 1.1 jtc * It might seem better to implement this as a macro with a function as
1329 1.1 jtc * hard-case backup, but it's just too big and messy unless there are
1330 1.1 jtc * some changes to the data structures. Maybe later.
1331 1.1 jtc */
1332 1.1 jtc static void
1333 1.1 jtc doemit(p, op, opnd)
1334 1.1 jtc register struct parse *p;
1335 1.1 jtc sop op;
1336 1.1 jtc size_t opnd;
1337 1.1 jtc {
1338 1.1 jtc /* avoid making error situations worse */
1339 1.1 jtc if (p->error != 0)
1340 1.1 jtc return;
1341 1.1 jtc
1342 1.1 jtc /* deal with oversize operands ("can't happen", more or less) */
1343 1.1 jtc assert(opnd < 1<<OPSHIFT);
1344 1.1 jtc
1345 1.1 jtc /* deal with undersized strip */
1346 1.1 jtc if (p->slen >= p->ssize)
1347 1.1 jtc enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1348 1.1 jtc assert(p->slen < p->ssize);
1349 1.1 jtc
1350 1.1 jtc /* finally, it's all reduced to the easy case */
1351 1.1 jtc p->strip[p->slen++] = SOP(op, opnd);
1352 1.1 jtc }
1353 1.1 jtc
1354 1.1 jtc /*
1355 1.1 jtc - doinsert - insert a sop into the strip
1356 1.1 jtc == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1357 1.1 jtc */
1358 1.1 jtc static void
1359 1.1 jtc doinsert(p, op, opnd, pos)
1360 1.1 jtc register struct parse *p;
1361 1.1 jtc sop op;
1362 1.1 jtc size_t opnd;
1363 1.1 jtc sopno pos;
1364 1.1 jtc {
1365 1.1 jtc register sopno sn;
1366 1.1 jtc register sop s;
1367 1.1 jtc register int i;
1368 1.1 jtc
1369 1.1 jtc /* avoid making error situations worse */
1370 1.1 jtc if (p->error != 0)
1371 1.1 jtc return;
1372 1.1 jtc
1373 1.1 jtc sn = HERE();
1374 1.1 jtc EMIT(op, opnd); /* do checks, ensure space */
1375 1.1 jtc assert(HERE() == sn+1);
1376 1.1 jtc s = p->strip[sn];
1377 1.1 jtc
1378 1.1 jtc /* adjust paren pointers */
1379 1.1 jtc assert(pos > 0);
1380 1.1 jtc for (i = 1; i < NPAREN; i++) {
1381 1.1 jtc if (p->pbegin[i] >= pos) {
1382 1.1 jtc p->pbegin[i]++;
1383 1.1 jtc }
1384 1.1 jtc if (p->pend[i] >= pos) {
1385 1.1 jtc p->pend[i]++;
1386 1.1 jtc }
1387 1.1 jtc }
1388 1.1 jtc
1389 1.1 jtc memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1390 1.1 jtc (HERE()-pos-1)*sizeof(sop));
1391 1.1 jtc p->strip[pos] = s;
1392 1.1 jtc }
1393 1.1 jtc
1394 1.1 jtc /*
1395 1.1 jtc - dofwd - complete a forward reference
1396 1.1 jtc == static void dofwd(register struct parse *p, sopno pos, sop value);
1397 1.1 jtc */
1398 1.1 jtc static void
1399 1.1 jtc dofwd(p, pos, value)
1400 1.1 jtc register struct parse *p;
1401 1.1 jtc register sopno pos;
1402 1.1 jtc sop value;
1403 1.1 jtc {
1404 1.1 jtc /* avoid making error situations worse */
1405 1.1 jtc if (p->error != 0)
1406 1.1 jtc return;
1407 1.1 jtc
1408 1.1 jtc assert(value < 1<<OPSHIFT);
1409 1.1 jtc p->strip[pos] = OP(p->strip[pos]) | value;
1410 1.1 jtc }
1411 1.1 jtc
1412 1.1 jtc /*
1413 1.1 jtc - enlarge - enlarge the strip
1414 1.1 jtc == static void enlarge(register struct parse *p, sopno size);
1415 1.1 jtc */
1416 1.1 jtc static void
1417 1.1 jtc enlarge(p, size)
1418 1.1 jtc register struct parse *p;
1419 1.1 jtc register sopno size;
1420 1.1 jtc {
1421 1.1 jtc register sop *sp;
1422 1.1 jtc
1423 1.1 jtc if (p->ssize >= size)
1424 1.1 jtc return;
1425 1.1 jtc
1426 1.1 jtc sp = (sop *)realloc(p->strip, size*sizeof(sop));
1427 1.1 jtc if (sp == NULL) {
1428 1.1 jtc SETERROR(REG_ESPACE);
1429 1.1 jtc return;
1430 1.1 jtc }
1431 1.1 jtc p->strip = sp;
1432 1.1 jtc p->ssize = size;
1433 1.1 jtc }
1434 1.1 jtc
1435 1.1 jtc /*
1436 1.1 jtc - stripsnug - compact the strip
1437 1.1 jtc == static void stripsnug(register struct parse *p, register struct re_guts *g);
1438 1.1 jtc */
1439 1.1 jtc static void
1440 1.1 jtc stripsnug(p, g)
1441 1.1 jtc register struct parse *p;
1442 1.1 jtc register struct re_guts *g;
1443 1.1 jtc {
1444 1.1 jtc g->nstates = p->slen;
1445 1.1 jtc g->strip = (sop *)realloc((sop *)p->strip, p->slen * sizeof(sop));
1446 1.1 jtc if (g->strip == NULL) {
1447 1.1 jtc SETERROR(REG_ESPACE);
1448 1.1 jtc g->strip = p->strip;
1449 1.1 jtc }
1450 1.1 jtc }
1451 1.1 jtc
1452 1.1 jtc /*
1453 1.1 jtc - findmust - fill in must and mlen with longest mandatory literal string
1454 1.1 jtc == static void findmust(register struct parse *p, register struct re_guts *g);
1455 1.1 jtc *
1456 1.1 jtc * This algorithm could do fancy things like analyzing the operands of |
1457 1.1 jtc * for common subsequences. Someday. This code is simple and finds most
1458 1.1 jtc * of the interesting cases.
1459 1.1 jtc *
1460 1.1 jtc * Note that must and mlen got initialized during setup.
1461 1.1 jtc */
1462 1.1 jtc static void
1463 1.1 jtc findmust(p, g)
1464 1.1 jtc struct parse *p;
1465 1.1 jtc register struct re_guts *g;
1466 1.1 jtc {
1467 1.1 jtc register sop *scan;
1468 1.1 jtc sop *start;
1469 1.1 jtc register sop *newstart;
1470 1.1 jtc register sopno newlen;
1471 1.1 jtc register sop s;
1472 1.1 jtc register char *cp;
1473 1.1 jtc register sopno i;
1474 1.1 jtc
1475 1.1 jtc /* avoid making error situations worse */
1476 1.1 jtc if (p->error != 0)
1477 1.1 jtc return;
1478 1.1 jtc
1479 1.1 jtc /* find the longest OCHAR sequence in strip */
1480 1.1 jtc newlen = 0;
1481 1.1 jtc scan = g->strip + 1;
1482 1.1 jtc do {
1483 1.1 jtc s = *scan++;
1484 1.1 jtc switch (OP(s)) {
1485 1.1 jtc case OCHAR: /* sequence member */
1486 1.1 jtc if (newlen == 0) /* new sequence */
1487 1.1 jtc newstart = scan - 1;
1488 1.1 jtc newlen++;
1489 1.1 jtc break;
1490 1.1 jtc case OPLUS_: /* things that don't break one */
1491 1.1 jtc case OLPAREN:
1492 1.1 jtc case ORPAREN:
1493 1.1 jtc break;
1494 1.1 jtc case OQUEST_: /* things that must be skipped */
1495 1.1 jtc case OCH_:
1496 1.1 jtc scan--;
1497 1.1 jtc do {
1498 1.1 jtc scan += OPND(s);
1499 1.1 jtc s = *scan;
1500 1.1 jtc /* assert() interferes w debug printouts */
1501 1.1 jtc if (OP(s) != O_QUEST && OP(s) != O_CH &&
1502 1.1 jtc OP(s) != OOR2) {
1503 1.1 jtc g->iflags |= BAD;
1504 1.1 jtc return;
1505 1.1 jtc }
1506 1.1 jtc } while (OP(s) != O_QUEST && OP(s) != O_CH);
1507 1.1 jtc /* fallthrough */
1508 1.1 jtc default: /* things that break a sequence */
1509 1.1 jtc if (newlen > g->mlen) { /* ends one */
1510 1.1 jtc start = newstart;
1511 1.1 jtc g->mlen = newlen;
1512 1.1 jtc }
1513 1.1 jtc newlen = 0;
1514 1.1 jtc break;
1515 1.1 jtc }
1516 1.1 jtc } while (OP(s) != OEND);
1517 1.1 jtc
1518 1.1 jtc if (g->mlen == 0) /* there isn't one */
1519 1.1 jtc return;
1520 1.1 jtc
1521 1.1 jtc /* turn it into a character string */
1522 1.1 jtc g->must = malloc((size_t)g->mlen + 1);
1523 1.1 jtc if (g->must == NULL) { /* argh; just forget it */
1524 1.1 jtc g->mlen = 0;
1525 1.1 jtc return;
1526 1.1 jtc }
1527 1.1 jtc cp = g->must;
1528 1.1 jtc scan = start;
1529 1.1 jtc for (i = g->mlen; i > 0; i--) {
1530 1.1 jtc while (OP(s = *scan++) != OCHAR)
1531 1.1 jtc continue;
1532 1.1 jtc *cp++ = (char)OPND(s);
1533 1.1 jtc }
1534 1.1 jtc *cp++ = '\0'; /* just on general principles */
1535 1.1 jtc }
1536 1.1 jtc
1537 1.1 jtc /*
1538 1.1 jtc - pluscount - count + nesting
1539 1.1 jtc == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1540 1.1 jtc */
1541 1.1 jtc static sopno /* nesting depth */
1542 1.1 jtc pluscount(p, g)
1543 1.1 jtc struct parse *p;
1544 1.1 jtc register struct re_guts *g;
1545 1.1 jtc {
1546 1.1 jtc register sop *scan;
1547 1.1 jtc register sop s;
1548 1.1 jtc register sopno plusnest = 0;
1549 1.1 jtc register sopno maxnest = 0;
1550 1.1 jtc
1551 1.1 jtc if (p->error != 0)
1552 1.1 jtc return(0); /* there may not be an OEND */
1553 1.1 jtc
1554 1.1 jtc scan = g->strip + 1;
1555 1.1 jtc do {
1556 1.1 jtc s = *scan++;
1557 1.1 jtc switch (OP(s)) {
1558 1.1 jtc case OPLUS_:
1559 1.1 jtc plusnest++;
1560 1.1 jtc break;
1561 1.1 jtc case O_PLUS:
1562 1.1 jtc if (plusnest > maxnest)
1563 1.1 jtc maxnest = plusnest;
1564 1.1 jtc plusnest--;
1565 1.1 jtc break;
1566 1.1 jtc }
1567 1.1 jtc } while (OP(s) != OEND);
1568 1.1 jtc if (plusnest != 0)
1569 1.1 jtc g->iflags |= BAD;
1570 1.1 jtc return(maxnest);
1571 1.1 jtc }
1572