* Update to version 2.19.99.
[alpine.git] / regex / regcomp.c
blob0747190a335d67b5c94e24348967ea4e0ac59765
1 /*-
2 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Henry Spencer.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
42 #endif /* LIBC_SCCS and not lint */
44 #include <sys/types.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include <limits.h>
49 #include <stdlib.h>
50 #include "regex.h"
52 #include "utils.h"
53 #include "regex2.h"
55 #include "cclass.h"
56 #include "cname.h"
59 * parse structure, passed up and down to avoid global variables and
60 * other clumsinesses
62 struct parse {
63 char *next; /* next character in RE */
64 char *end; /* end of string (-> NUL normally) */
65 int error; /* has an error been seen? */
66 sop *strip; /* malloced strip */
67 sopno ssize; /* malloced strip size (allocated) */
68 sopno slen; /* malloced strip length (used) */
69 int ncsalloc; /* number of csets allocated */
70 struct re_guts *g;
71 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
72 sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
73 sopno pend[NPAREN]; /* -> ) ([0] unused) */
76 /* ========= begin header generated by ./mkh ========= */
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
81 /* === regcomp.c === */
82 static void p_ere __P((struct parse *p, int stop));
83 static void p_ere_exp __P((struct parse *p));
84 static void p_str __P((struct parse *p));
85 static void p_bre __P((struct parse *p, int end1, int end2));
86 static int p_simp_re __P((struct parse *p, int starordinary));
87 static int p_count __P((struct parse *p));
88 static void p_bracket __P((struct parse *p));
89 static void p_b_term __P((struct parse *p, cset *cs));
90 static void p_b_cclass __P((struct parse *p, cset *cs));
91 static void p_b_eclass __P((struct parse *p, cset *cs));
92 static char p_b_symbol __P((struct parse *p));
93 static char p_b_coll_elem __P((struct parse *p, int endc));
94 static char othercase __P((int ch));
95 static void bothcases __P((struct parse *p, int ch));
96 static void ordinary __P((struct parse *p, int ch));
97 static void nonnewline __P((struct parse *p));
98 static void repeat __P((struct parse *p, sopno start, int from, int to));
99 static int seterr __P((struct parse *p, int e));
100 static cset *allocset __P((struct parse *p));
101 static void freeset __P((struct parse *p, cset *cs));
102 static int freezeset __P((struct parse *p, cset *cs));
103 static int firstch __P((struct parse *p, cset *cs));
104 static int nch __P((struct parse *p, cset *cs));
105 static void mcadd __P((struct parse *p, cset *cs, char *cp));
106 static void mcsub __P((cset *cs, char *cp));
107 static int mcin __P((cset *cs, char *cp));
108 static char *mcfind __P((cset *cs, char *cp));
109 static void mcinvert __P((struct parse *p, cset *cs));
110 static void mccase __P((struct parse *p, cset *cs));
111 static int isinsets __P((struct re_guts *g, int c));
112 static int samesets __P((struct re_guts *g, int c1, int c2));
113 static void categorize __P((struct parse *p, struct re_guts *g));
114 static sopno dupl __P((struct parse *p, sopno start, sopno finish));
115 static void doemit __P((struct parse *p, sop op, size_t opnd));
116 static void doinsert __P((struct parse *p, sop op, size_t opnd, sopno pos));
117 static void dofwd __P((struct parse *p, sopno pos, sop value));
118 static void enlarge __P((struct parse *p, sopno size));
119 static void stripsnug __P((struct parse *p, struct re_guts *g));
120 static void findmust __P((struct parse *p, struct re_guts *g));
121 static sopno pluscount __P((struct parse *p, struct re_guts *g));
123 #ifdef __cplusplus
125 #endif
126 /* ========= end header generated by ./mkh ========= */
128 static char nuls[10]; /* place to point scanner in event of error */
131 * macros for use with parse structure
132 * BEWARE: these know that the parse structure is named `p' !!!
134 #define PEEK() (*p->next)
135 #define PEEK2() (*(p->next+1))
136 #define MORE() (p->next < p->end)
137 #define MORE2() (p->next+1 < p->end)
138 #define SEE(c) (MORE() && PEEK() == (c))
139 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
140 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
141 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
142 #define NEXT() (p->next++)
143 #define NEXT2() (p->next += 2)
144 #define NEXTn(n) (p->next += (n))
145 #define GETNEXT() (*p->next++)
146 #define SETERROR(e) seterr(p, (e))
147 #define REQUIRE(co, e) ((void) ((co) || SETERROR(e)))
148 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
149 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
150 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
151 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
152 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
153 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
154 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
155 #define HERE() (p->slen)
156 #define THERE() (p->slen - 1)
157 #define THERETHERE() (p->slen - 2)
158 #define DROP(n) (p->slen -= (n))
160 #ifndef NDEBUG
161 static int never = 0; /* for use in asserts; shuts lint up */
162 #else
163 #define never 0 /* some <assert.h>s have bugs too */
164 #endif
167 - regcomp - interface for parser and compilation
168 = extern int regcomp(regex_t *, const char *, int);
169 = #define REG_BASIC 0000
170 = #define REG_EXTENDED 0001
171 = #define REG_ICASE 0002
172 = #define REG_NOSUB 0004
173 = #define REG_NEWLINE 0010
174 = #define REG_NOSPEC 0020
175 = #define REG_PEND 0040
176 = #define REG_DUMP 0200
178 int __stdcall /* 0 success, otherwise REG_something */
179 regcomp(regex_t *preg, const char *pattern, int cflags)
181 struct parse pa;
182 register struct re_guts *g;
183 register struct parse *p = &pa;
184 register int i;
185 register size_t len;
186 #ifdef REDEBUG
187 # define GOODFLAGS(f) (f)
188 #else
189 # define GOODFLAGS(f) ((f)&~REG_DUMP)
190 #endif
192 cflags = GOODFLAGS(cflags);
193 if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
194 return(REG_INVARG);
196 if (cflags&REG_PEND) {
197 if (preg->re_endp < pattern)
198 return(REG_INVARG);
199 len = preg->re_endp - pattern;
200 } else
201 len = strlen((char *)pattern);
203 /* do the mallocs early so failure handling is easy */
204 g = (struct re_guts *)malloc(sizeof(struct re_guts) +
205 (NC-1)*sizeof(cat_t));
206 if (g == NULL)
207 return(REG_ESPACE);
208 p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
209 p->strip = (sop *)malloc(p->ssize * sizeof(sop));
210 p->slen = 0;
211 if (p->strip == NULL) {
212 free((char *)g);
213 return(REG_ESPACE);
216 /* set things up */
217 p->g = g;
218 p->next = (char *)pattern; /* convenience; we do not modify it */
219 p->end = p->next + len;
220 p->error = 0;
221 p->ncsalloc = 0;
222 for (i = 0; i < NPAREN; i++) {
223 p->pbegin[i] = 0;
224 p->pend[i] = 0;
226 g->csetsize = NC;
227 g->sets = NULL;
228 g->setbits = NULL;
229 g->ncsets = 0;
230 g->cflags = cflags;
231 g->iflags = 0;
232 g->nbol = 0;
233 g->neol = 0;
234 g->must = NULL;
235 g->mlen = 0;
236 g->nsub = 0;
237 g->ncategories = 1; /* category 0 is "everything else" */
238 g->categories = &g->catspace[-(CHAR_MIN)];
239 (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
240 g->backrefs = 0;
242 /* do it */
243 EMIT(OEND, 0);
244 g->firststate = THERE();
245 if (cflags&REG_EXTENDED)
246 p_ere(p, OUT);
247 else if (cflags&REG_NOSPEC)
248 p_str(p);
249 else
250 p_bre(p, OUT, OUT);
251 EMIT(OEND, 0);
252 g->laststate = THERE();
254 /* tidy up loose ends and fill things in */
255 categorize(p, g);
256 stripsnug(p, g);
257 findmust(p, g);
258 g->nplus = pluscount(p, g);
259 g->magic = MAGIC2;
260 preg->re_nsub = g->nsub;
261 preg->re_g = g;
262 preg->re_magic = MAGIC1;
263 #ifndef REDEBUG
264 /* not debugging, so can't rely on the assert() in regexec() */
265 if (g->iflags&BAD)
266 SETERROR(REG_ASSERT);
267 #endif
269 /* win or lose, we're done */
270 if (p->error != 0) /* lose */
271 regfree(preg);
272 return(p->error);
276 - p_ere - ERE parser top level, concatenation and alternation
277 == static void p_ere(register struct parse *p, int stop);
279 static void
280 p_ere(struct parse *p,
281 int stop) /* character this ERE should end at */
283 register char c;
284 register sopno prevback;
285 register sopno prevfwd;
286 register sopno conc;
287 register int first = 1; /* is this the first alternative? */
289 for (;;) {
290 /* do a bunch of concatenated expressions */
291 conc = HERE();
292 while (MORE() && (c = PEEK()) != '|' && c != stop)
293 p_ere_exp(p);
294 REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
296 if (!EAT('|'))
297 break; /* NOTE BREAK OUT */
299 if (first) {
300 INSERT(OCH_, conc); /* offset is wrong */
301 prevfwd = conc;
302 prevback = conc;
303 first = 0;
305 ASTERN(OOR1, prevback);
306 prevback = THERE();
307 AHEAD(prevfwd); /* fix previous offset */
308 prevfwd = HERE();
309 EMIT(OOR2, 0); /* offset is very wrong */
312 if (!first) { /* tail-end fixups */
313 AHEAD(prevfwd);
314 ASTERN(O_CH, prevback);
317 assert(!MORE() || SEE(stop));
321 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
322 == static void p_ere_exp(register struct parse *p);
324 static void
325 p_ere_exp(struct parse *p)
327 register char c;
328 register sopno pos;
329 register int count;
330 register int count2;
331 register sopno subno;
332 int wascaret = 0;
334 assert(MORE()); /* caller should have ensured this */
335 c = GETNEXT();
337 pos = HERE();
338 switch (c) {
339 case '(':
340 REQUIRE(MORE(), REG_EPAREN);
341 p->g->nsub++;
342 subno = p->g->nsub;
343 if (subno < NPAREN)
344 p->pbegin[subno] = HERE();
345 EMIT(OLPAREN, subno);
346 if (!SEE(')'))
347 p_ere(p, ')');
348 if (subno < NPAREN) {
349 p->pend[subno] = HERE();
350 assert(p->pend[subno] != 0);
352 EMIT(ORPAREN, subno);
353 MUSTEAT(')', REG_EPAREN);
354 break;
355 #ifndef POSIX_MISTAKE
356 case ')': /* happens only if no current unmatched ( */
358 * You may ask, why the ifndef? Because I didn't notice
359 * this until slightly too late for 1003.2, and none of the
360 * other 1003.2 regular-expression reviewers noticed it at
361 * all. So an unmatched ) is legal POSIX, at least until
362 * we can get it fixed.
364 SETERROR(REG_EPAREN);
365 break;
366 #endif
367 case '^':
368 EMIT(OBOL, 0);
369 p->g->iflags |= USEBOL;
370 p->g->nbol++;
371 wascaret = 1;
372 break;
373 case '$':
374 EMIT(OEOL, 0);
375 p->g->iflags |= USEEOL;
376 p->g->neol++;
377 break;
378 case '|':
379 SETERROR(REG_EMPTY);
380 break;
381 case '*':
382 case '+':
383 case '?':
384 SETERROR(REG_BADRPT);
385 break;
386 case '.':
387 if (p->g->cflags&REG_NEWLINE)
388 nonnewline(p);
389 else
390 EMIT(OANY, 0);
391 break;
392 case '[':
393 p_bracket(p);
394 break;
395 case '\\':
396 REQUIRE(MORE(), REG_EESCAPE);
397 c = GETNEXT();
398 ordinary(p, c);
399 break;
400 case '{': /* okay as ordinary except if digit follows */
401 REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT);
402 /* FALLTHROUGH */
403 default:
404 ordinary(p, c);
405 break;
408 if (!MORE())
409 return;
410 c = PEEK();
411 /* we call { a repetition if followed by a digit */
412 if (!( c == '*' || c == '+' || c == '?' ||
413 (c == '{' && MORE2() && isdigit(PEEK2())) ))
414 return; /* no repetition, we're done */
415 NEXT();
417 REQUIRE(!wascaret, REG_BADRPT);
418 switch (c) {
419 case '*': /* implemented as +? */
420 /* this case does not require the (y|) trick, noKLUDGE */
421 INSERT(OPLUS_, pos);
422 ASTERN(O_PLUS, pos);
423 INSERT(OQUEST_, pos);
424 ASTERN(O_QUEST, pos);
425 break;
426 case '+':
427 INSERT(OPLUS_, pos);
428 ASTERN(O_PLUS, pos);
429 break;
430 case '?':
431 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
432 INSERT(OCH_, pos); /* offset slightly wrong */
433 ASTERN(OOR1, pos); /* this one's right */
434 AHEAD(pos); /* fix the OCH_ */
435 EMIT(OOR2, 0); /* offset very wrong... */
436 AHEAD(THERE()); /* ...so fix it */
437 ASTERN(O_CH, THERETHERE());
438 break;
439 case '{':
440 count = p_count(p);
441 if (EAT(',')) {
442 if (isdigit(PEEK())) {
443 count2 = p_count(p);
444 REQUIRE(count <= count2, REG_BADBR);
445 } else /* single number with comma */
446 count2 = INFINITY;
447 } else /* just a single number */
448 count2 = count;
449 repeat(p, pos, count, count2);
450 if (!EAT('}')) { /* error heuristics */
451 while (MORE() && PEEK() != '}')
452 NEXT();
453 REQUIRE(MORE(), REG_EBRACE);
454 SETERROR(REG_BADBR);
456 break;
459 if (!MORE())
460 return;
461 c = PEEK();
462 if (!( c == '*' || c == '+' || c == '?' ||
463 (c == '{' && MORE2() && isdigit(PEEK2())) ) )
464 return;
465 SETERROR(REG_BADRPT);
469 - p_str - string (no metacharacters) "parser"
470 == static void p_str(register struct parse *p);
472 static void
473 p_str(struct parse *p)
475 REQUIRE(MORE(), REG_EMPTY);
476 while (MORE())
477 ordinary(p, GETNEXT());
481 - p_bre - BRE parser top level, anchoring and concatenation
482 == static void p_bre(register struct parse *p, register int end1, \
483 == register int end2);
484 * Giving end1 as OUT essentially eliminates the end1/end2 check.
486 * This implementation is a bit of a kludge, in that a trailing $ is first
487 * taken as an ordinary character and then revised to be an anchor. The
488 * only undesirable side effect is that '$' gets included as a character
489 * category in such cases. This is fairly harmless; not worth fixing.
490 * The amount of lookahead needed to avoid this kludge is excessive.
492 static void
493 p_bre(struct parse *p,
494 int end1, /* first terminating character */
495 int end2) /* second terminating character */
497 register sopno start = HERE();
498 register int first = 1; /* first subexpression? */
499 register int wasdollar = 0;
501 if (EAT('^')) {
502 EMIT(OBOL, 0);
503 p->g->iflags |= USEBOL;
504 p->g->nbol++;
506 while (MORE() && !SEETWO(end1, end2)) {
507 wasdollar = p_simp_re(p, first);
508 first = 0;
510 if (wasdollar) { /* oops, that was a trailing anchor */
511 DROP(1);
512 EMIT(OEOL, 0);
513 p->g->iflags |= USEEOL;
514 p->g->neol++;
517 REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
521 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
522 == static int p_simp_re(register struct parse *p, int starordinary);
524 static int /* was the simple RE an unbackslashed $? */
525 p_simp_re(struct parse *p,
526 int starordinary) /* is a leading * an ordinary character? */
528 register int c;
529 register int count;
530 register int count2;
531 register sopno pos;
532 register int i;
533 register sopno subno;
534 # define BACKSL (1<<CHAR_BIT)
536 pos = HERE(); /* repetion op, if any, covers from here */
538 assert(MORE()); /* caller should have ensured this */
539 c = GETNEXT();
540 if (c == '\\') {
541 REQUIRE(MORE(), REG_EESCAPE);
542 c = BACKSL | (unsigned char)GETNEXT();
544 switch (c) {
545 case '.':
546 if (p->g->cflags&REG_NEWLINE)
547 nonnewline(p);
548 else
549 EMIT(OANY, 0);
550 break;
551 case '[':
552 p_bracket(p);
553 break;
554 case BACKSL|'{':
555 SETERROR(REG_BADRPT);
556 break;
557 case BACKSL|'(':
558 p->g->nsub++;
559 subno = p->g->nsub;
560 if (subno < NPAREN)
561 p->pbegin[subno] = HERE();
562 EMIT(OLPAREN, subno);
563 /* the MORE here is an error heuristic */
564 if (MORE() && !SEETWO('\\', ')'))
565 p_bre(p, '\\', ')');
566 if (subno < NPAREN) {
567 p->pend[subno] = HERE();
568 assert(p->pend[subno] != 0);
570 EMIT(ORPAREN, subno);
571 REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
572 break;
573 case BACKSL|')': /* should not get here -- must be user */
574 case BACKSL|'}':
575 SETERROR(REG_EPAREN);
576 break;
577 case BACKSL|'1':
578 case BACKSL|'2':
579 case BACKSL|'3':
580 case BACKSL|'4':
581 case BACKSL|'5':
582 case BACKSL|'6':
583 case BACKSL|'7':
584 case BACKSL|'8':
585 case BACKSL|'9':
586 i = (c&~BACKSL) - '0';
587 assert(i < NPAREN);
588 if (p->pend[i] != 0) {
589 assert(i <= p->g->nsub);
590 EMIT(OBACK_, i);
591 assert(p->pbegin[i] != 0);
592 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
593 assert(OP(p->strip[p->pend[i]]) == ORPAREN);
594 (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
595 EMIT(O_BACK, i);
596 } else
597 SETERROR(REG_ESUBREG);
598 p->g->backrefs = 1;
599 break;
600 case '*':
601 REQUIRE(starordinary, REG_BADRPT);
602 /* FALLTHROUGH */
603 default:
604 ordinary(p, c &~ BACKSL);
605 break;
608 if (EAT('*')) { /* implemented as +? */
609 /* this case does not require the (y|) trick, noKLUDGE */
610 INSERT(OPLUS_, pos);
611 ASTERN(O_PLUS, pos);
612 INSERT(OQUEST_, pos);
613 ASTERN(O_QUEST, pos);
614 } else if (EATTWO('\\', '{')) {
615 count = p_count(p);
616 if (EAT(',')) {
617 if (MORE() && isdigit(PEEK())) {
618 count2 = p_count(p);
619 REQUIRE(count <= count2, REG_BADBR);
620 } else /* single number with comma */
621 count2 = INFINITY;
622 } else /* just a single number */
623 count2 = count;
624 repeat(p, pos, count, count2);
625 if (!EATTWO('\\', '}')) { /* error heuristics */
626 while (MORE() && !SEETWO('\\', '}'))
627 NEXT();
628 REQUIRE(MORE(), REG_EBRACE);
629 SETERROR(REG_BADBR);
631 } else if (c == (unsigned char)'$') /* $ (but not \$) ends it */
632 return(1);
634 return(0);
638 - p_count - parse a repetition count
639 == static int p_count(register struct parse *p);
641 static int /* the value */
642 p_count(struct parse *p)
644 register int count = 0;
645 register int ndigits = 0;
647 while (MORE() && isdigit(PEEK()) && count <= DUPMAX) {
648 count = count*10 + (GETNEXT() - '0');
649 ndigits++;
652 REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
653 return(count);
657 - p_bracket - parse a bracketed character list
658 == static void p_bracket(register struct parse *p);
660 * Note a significant property of this code: if the allocset() did SETERROR,
661 * no set operations are done.
663 static void
664 p_bracket(struct parse *p)
666 register cset *cs = allocset(p);
667 register int invert = 0;
669 /* Dept of Truly Sickening Special-Case Kludges */
670 if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
671 EMIT(OBOW, 0);
672 NEXTn(6);
673 return;
675 if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
676 EMIT(OEOW, 0);
677 NEXTn(6);
678 return;
681 if (EAT('^'))
682 invert++; /* make note to invert set at end */
683 if (EAT(']'))
684 CHadd(cs, ']');
685 else if (EAT('-'))
686 CHadd(cs, '-');
687 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
688 p_b_term(p, cs);
689 if (EAT('-'))
690 CHadd(cs, '-');
691 MUSTEAT(']', REG_EBRACK);
693 if (p->error != 0) /* don't mess things up further */
694 return;
696 if (p->g->cflags&REG_ICASE) {
697 register int i;
698 register int ci;
700 for (i = p->g->csetsize - 1; i >= 0; i--)
701 if (CHIN(cs, i) && isalpha(i)) {
702 ci = othercase(i);
703 if (ci != i)
704 CHadd(cs, ci);
706 if (cs->multis != NULL)
707 mccase(p, cs);
709 if (invert) {
710 register int i;
712 for (i = p->g->csetsize - 1; i >= 0; i--)
713 if (CHIN(cs, i))
714 CHsub(cs, i);
715 else
716 CHadd(cs, i);
717 if (p->g->cflags&REG_NEWLINE)
718 CHsub(cs, '\n');
719 if (cs->multis != NULL)
720 mcinvert(p, cs);
723 assert(cs->multis == NULL); /* xxx */
725 if (nch(p, cs) == 1) { /* optimize singleton sets */
726 ordinary(p, firstch(p, cs));
727 freeset(p, cs);
728 } else
729 EMIT(OANYOF, freezeset(p, cs));
733 - p_b_term - parse one term of a bracketed character list
734 == static void p_b_term(register struct parse *p, register cset *cs);
736 static void
737 p_b_term(struct parse *p, cset *cs)
739 register char c;
740 register char start, finish;
741 register int i;
743 /* classify what we've got */
744 switch ((MORE()) ? PEEK() : '\0') {
745 case '[':
746 c = (MORE2()) ? PEEK2() : '\0';
747 break;
748 case '-':
749 SETERROR(REG_ERANGE);
750 return; /* NOTE RETURN */
751 break;
752 default:
753 c = '\0';
754 break;
757 switch (c) {
758 case ':': /* character class */
759 NEXT2();
760 REQUIRE(MORE(), REG_EBRACK);
761 c = PEEK();
762 REQUIRE(c != '-' && c != ']', REG_ECTYPE);
763 p_b_cclass(p, cs);
764 REQUIRE(MORE(), REG_EBRACK);
765 REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
766 break;
767 case '=': /* equivalence class */
768 NEXT2();
769 REQUIRE(MORE(), REG_EBRACK);
770 c = PEEK();
771 REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
772 p_b_eclass(p, cs);
773 REQUIRE(MORE(), REG_EBRACK);
774 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
775 break;
776 default: /* symbol, ordinary character, or range */
777 /* xxx revision needed for multichar stuff */
778 start = p_b_symbol(p);
779 if (SEE('-') && MORE2() && PEEK2() != ']') {
780 /* range */
781 NEXT();
782 if (EAT('-'))
783 finish = '-';
784 else
785 finish = p_b_symbol(p);
786 } else
787 finish = start;
788 /* xxx what about signed chars here... */
789 REQUIRE(start <= finish, REG_ERANGE);
790 for (i = start; i <= finish; i++)
791 CHadd(cs, i);
792 break;
797 - p_b_cclass - parse a character-class name and deal with it
798 == static void p_b_cclass(register struct parse *p, register cset *cs);
800 static void
801 p_b_cclass(struct parse *p, cset *cs)
803 register char *sp = p->next;
804 register struct cclass *cp;
805 register size_t len;
806 register char *u;
807 register char c;
809 while (MORE() && isalpha(PEEK()))
810 NEXT();
811 len = p->next - sp;
812 for (cp = cclasses; cp->name != NULL; cp++)
813 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
814 break;
815 if (cp->name == NULL) {
816 /* oops, didn't find it */
817 SETERROR(REG_ECTYPE);
818 return;
821 u = cp->chars;
822 while ((c = *u++) != '\0')
823 CHadd(cs, c);
824 for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
825 MCadd(p, cs, u);
829 - p_b_eclass - parse an equivalence-class name and deal with it
830 == static void p_b_eclass(register struct parse *p, register cset *cs);
832 * This implementation is incomplete. xxx
834 static void
835 p_b_eclass(struct parse *p, cset *cs)
837 register char c;
839 c = p_b_coll_elem(p, '=');
840 CHadd(cs, c);
844 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
845 == static char p_b_symbol(register struct parse *p);
847 static char /* value of symbol */
848 p_b_symbol(struct parse *p)
850 register char value;
852 REQUIRE(MORE(), REG_EBRACK);
853 if (!EATTWO('[', '.'))
854 return(GETNEXT());
856 /* collating symbol */
857 value = p_b_coll_elem(p, '.');
858 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
859 return(value);
863 - p_b_coll_elem - parse a collating-element name and look it up
864 == static char p_b_coll_elem(register struct parse *p, int endc);
866 static char /* value of collating element */
867 p_b_coll_elem(struct parse *p,
868 int endc) /* name ended by endc,']' */
870 register char *sp = p->next;
871 register struct cname *cp;
872 register int len;
874 while (MORE() && !SEETWO(endc, ']'))
875 NEXT();
876 if (!MORE()) {
877 SETERROR(REG_EBRACK);
878 return(0);
880 len = p->next - sp;
881 for (cp = cnames; cp->name != NULL; cp++)
882 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
883 return(cp->code); /* known name */
884 if (len == 1)
885 return(*sp); /* single character */
886 SETERROR(REG_ECOLLATE); /* neither */
887 return(0);
891 - othercase - return the case counterpart of an alphabetic
892 == static char othercase(int ch);
894 static char /* if no counterpart, return ch */
895 othercase(int ch)
897 assert(isalpha(ch));
898 if (isupper(ch))
899 return(tolower(ch));
900 else if (islower(ch))
901 return(toupper(ch));
902 else /* peculiar, but could happen */
903 return(ch);
907 - bothcases - emit a dualcase version of a two-case character
908 == static void bothcases(register struct parse *p, int ch);
910 * Boy, is this implementation ever a kludge...
912 static void
913 bothcases(struct parse *p, int ch)
915 register char *oldnext = p->next;
916 register char *oldend = p->end;
917 char bracket[3];
919 assert(othercase(ch) != ch); /* p_bracket() would recurse */
920 p->next = bracket;
921 p->end = bracket+2;
922 bracket[0] = ch;
923 bracket[1] = ']';
924 bracket[2] = '\0';
925 p_bracket(p);
926 assert(p->next == bracket+2);
927 p->next = oldnext;
928 p->end = oldend;
932 - ordinary - emit an ordinary character
933 == static void ordinary(register struct parse *p, register int ch);
935 static void
936 ordinary(struct parse *p, int ch)
938 register cat_t *cap = p->g->categories;
940 if ((p->g->cflags&REG_ICASE) && isalpha(ch) && othercase(ch) != ch)
941 bothcases(p, ch);
942 else {
943 EMIT(OCHAR, (unsigned char)ch);
944 if (cap[ch] == 0)
945 cap[ch] = p->g->ncategories++;
950 - nonnewline - emit REG_NEWLINE version of OANY
951 == static void nonnewline(register struct parse *p);
953 * Boy, is this implementation ever a kludge...
955 static void
956 nonnewline(struct parse *p)
958 register char *oldnext = p->next;
959 register char *oldend = p->end;
960 char bracket[4];
962 p->next = bracket;
963 p->end = bracket+3;
964 bracket[0] = '^';
965 bracket[1] = '\n';
966 bracket[2] = ']';
967 bracket[3] = '\0';
968 p_bracket(p);
969 assert(p->next == bracket+3);
970 p->next = oldnext;
971 p->end = oldend;
975 - repeat - generate code for a bounded repetition, recursively if needed
976 == static void repeat(register struct parse *p, sopno start, int from, int to);
978 static void
979 repeat(struct parse *p,
980 sopno start, /* operand from here to end of strip */
981 int from, /* repeated from this number */
982 int to) /* to this number of times (maybe INFINITY) */
984 register sopno finish = HERE();
985 # define N 2
986 # define INF 3
987 # define REP(f, t) ((f)*8 + (t))
988 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
989 register sopno copy;
991 if (p->error != 0) /* head off possible runaway recursion */
992 return;
994 assert(from <= to);
996 switch (REP(MAP(from), MAP(to))) {
997 case REP(0, 0): /* must be user doing this */
998 DROP(finish-start); /* drop the operand */
999 break;
1000 case REP(0, 1): /* as x{1,1}? */
1001 case REP(0, N): /* as x{1,n}? */
1002 case REP(0, INF): /* as x{1,}? */
1003 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1004 INSERT(OCH_, start); /* offset is wrong... */
1005 repeat(p, start+1, 1, to);
1006 ASTERN(OOR1, start);
1007 AHEAD(start); /* ... fix it */
1008 EMIT(OOR2, 0);
1009 AHEAD(THERE());
1010 ASTERN(O_CH, THERETHERE());
1011 break;
1012 case REP(1, 1): /* trivial case */
1013 /* done */
1014 break;
1015 case REP(1, N): /* as x?x{1,n-1} */
1016 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1017 INSERT(OCH_, start);
1018 ASTERN(OOR1, start);
1019 AHEAD(start);
1020 EMIT(OOR2, 0); /* offset very wrong... */
1021 AHEAD(THERE()); /* ...so fix it */
1022 ASTERN(O_CH, THERETHERE());
1023 copy = dupl(p, start+1, finish+1);
1024 assert(copy == finish+4);
1025 repeat(p, copy, 1, to-1);
1026 break;
1027 case REP(1, INF): /* as x+ */
1028 INSERT(OPLUS_, start);
1029 ASTERN(O_PLUS, start);
1030 break;
1031 case REP(N, N): /* as xx{m-1,n-1} */
1032 copy = dupl(p, start, finish);
1033 repeat(p, copy, from-1, to-1);
1034 break;
1035 case REP(N, INF): /* as xx{n-1,INF} */
1036 copy = dupl(p, start, finish);
1037 repeat(p, copy, from-1, to);
1038 break;
1039 default: /* "can't happen" */
1040 SETERROR(REG_ASSERT); /* just in case */
1041 break;
1046 - seterr - set an error condition
1047 == static int seterr(register struct parse *p, int e);
1049 static int /* useless but makes type checking happy */
1050 seterr(struct parse *p, int e)
1052 if (p->error == 0) /* keep earliest error condition */
1053 p->error = e;
1054 p->next = nuls; /* try to bring things to a halt */
1055 p->end = nuls;
1056 return(0); /* make the return value well-defined */
1060 - allocset - allocate a set of characters for []
1061 == static cset *allocset(register struct parse *p);
1063 static cset *
1064 allocset(struct parse *p)
1066 register int no = p->g->ncsets++;
1067 register size_t nc;
1068 register size_t nbytes;
1069 register cset *cs;
1070 register size_t css = (size_t)p->g->csetsize;
1071 register int i;
1073 if (no >= p->ncsalloc) { /* need another column of space */
1074 p->ncsalloc += CHAR_BIT;
1075 nc = p->ncsalloc;
1076 assert(nc % CHAR_BIT == 0);
1077 nbytes = nc / CHAR_BIT * css;
1078 if (p->g->sets == NULL)
1079 p->g->sets = (cset *)malloc(nc * sizeof(cset));
1080 else
1081 p->g->sets = (cset *)realloc((char *)p->g->sets,
1082 nc * sizeof(cset));
1083 if (p->g->setbits == NULL)
1084 p->g->setbits = (uch *)malloc(nbytes);
1085 else {
1086 p->g->setbits = (uch *)realloc((char *)p->g->setbits,
1087 nbytes);
1088 /* xxx this isn't right if setbits is now NULL */
1089 for (i = 0; i < no; i++)
1090 p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1092 if (p->g->sets != NULL && p->g->setbits != NULL)
1093 (void) memset((char *)p->g->setbits + (nbytes - css),
1094 0, css);
1095 else {
1096 no = 0;
1097 SETERROR(REG_ESPACE);
1098 /* caller's responsibility not to do set ops */
1102 assert(p->g->sets != NULL); /* xxx */
1103 cs = &p->g->sets[no];
1104 cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1105 cs->mask = 1 << ((no) % CHAR_BIT);
1106 cs->hash = 0;
1107 cs->smultis = 0;
1108 cs->multis = NULL;
1110 return(cs);
1114 - freeset - free a now-unused set
1115 == static void freeset(register struct parse *p, register cset *cs);
1117 static void
1118 freeset(struct parse *p, cset *cs)
1120 register int i;
1121 register cset *top = &p->g->sets[p->g->ncsets];
1122 register size_t css = (size_t)p->g->csetsize;
1124 for (i = 0; i < css; i++)
1125 CHsub(cs, i);
1126 if (cs == top-1) /* recover only the easy case */
1127 p->g->ncsets--;
1131 - freezeset - final processing on a set of characters
1132 == static int freezeset(register struct parse *p, register cset *cs);
1134 * The main task here is merging identical sets. This is usually a waste
1135 * of time (although the hash code minimizes the overhead), but can win
1136 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1137 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1138 * the same value!
1140 static int /* set number */
1141 freezeset(struct parse *p, cset *cs)
1143 register uch h = cs->hash;
1144 register int i;
1145 register cset *top = &p->g->sets[p->g->ncsets];
1146 register cset *cs2;
1147 register size_t css = (size_t)p->g->csetsize;
1149 /* look for an earlier one which is the same */
1150 for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1151 if (cs2->hash == h && cs2 != cs) {
1152 /* maybe */
1153 for (i = 0; i < css; i++)
1154 if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1155 break; /* no */
1156 if (i == css)
1157 break; /* yes */
1160 if (cs2 < top) { /* found one */
1161 freeset(p, cs);
1162 cs = cs2;
1165 return((int)(cs - p->g->sets));
1169 - firstch - return first character in a set (which must have at least one)
1170 == static int firstch(register struct parse *p, register cset *cs);
1172 static int /* character; there is no "none" value */
1173 firstch(struct parse *p, cset *cs)
1175 register int i;
1176 register size_t css = (size_t)p->g->csetsize;
1178 for (i = 0; i < css; i++)
1179 if (CHIN(cs, i))
1180 return((char)i);
1181 assert(never);
1182 return(0); /* arbitrary */
1186 - nch - number of characters in a set
1187 == static int nch(register struct parse *p, register cset *cs);
1189 static int
1190 nch(struct parse *p, cset *cs)
1192 register int i;
1193 register size_t css = (size_t)p->g->csetsize;
1194 register int n = 0;
1196 for (i = 0; i < css; i++)
1197 if (CHIN(cs, i))
1198 n++;
1199 return(n);
1203 - mcadd - add a collating element to a cset
1204 == static void mcadd(register struct parse *p, register cset *cs, \
1205 == register char *cp);
1207 static void
1208 mcadd(struct parse *p, cset *cs, char *cp)
1210 register size_t oldend = cs->smultis;
1212 cs->smultis += strlen(cp) + 1;
1213 if (cs->multis == NULL)
1214 cs->multis = malloc(cs->smultis);
1215 else
1216 cs->multis = realloc(cs->multis, cs->smultis);
1217 if (cs->multis == NULL) {
1218 SETERROR(REG_ESPACE);
1219 return;
1222 (void) strcpy(cs->multis + oldend - 1, cp);
1223 cs->multis[cs->smultis - 1] = '\0';
1227 - mcsub - subtract a collating element from a cset
1228 == static void mcsub(register cset *cs, register char *cp);
1230 static void
1231 mcsub(cset *cs, char *cp)
1233 register char *fp = mcfind(cs, cp);
1234 register size_t len = strlen(fp);
1236 assert(fp != NULL);
1237 (void) memmove(fp, fp + len + 1,
1238 cs->smultis - (fp + len + 1 - cs->multis));
1239 cs->smultis -= len;
1241 if (cs->smultis == 0) {
1242 free(cs->multis);
1243 cs->multis = NULL;
1244 return;
1247 cs->multis = realloc(cs->multis, cs->smultis);
1248 assert(cs->multis != NULL);
1252 - mcin - is a collating element in a cset?
1253 == static int mcin(register cset *cs, register char *cp);
1255 static int
1256 mcin(cset *cs, char *cp)
1258 return(mcfind(cs, cp) != NULL);
1262 - mcfind - find a collating element in a cset
1263 == static char *mcfind(register cset *cs, register char *cp);
1265 static char *
1266 mcfind(cset *cs, char *cp)
1268 register char *p;
1270 if (cs->multis == NULL)
1271 return(NULL);
1272 for (p = cs->multis; *p != '\0'; p += strlen(p) + 1)
1273 if (strcmp(cp, p) == 0)
1274 return(p);
1275 return(NULL);
1279 - mcinvert - invert the list of collating elements in a cset
1280 == static void mcinvert(register struct parse *p, register cset *cs);
1282 * This would have to know the set of possibilities. Implementation
1283 * is deferred.
1285 static void
1286 mcinvert(struct parse *p, cset *cs)
1288 assert(cs->multis == NULL); /* xxx */
1292 - mccase - add case counterparts of the list of collating elements in a cset
1293 == static void mccase(register struct parse *p, register cset *cs);
1295 * This would have to know the set of possibilities. Implementation
1296 * is deferred.
1298 static void
1299 mccase(struct parse *p, cset *cs)
1301 assert(cs->multis == NULL); /* xxx */
1305 - isinsets - is this character in any sets?
1306 == static int isinsets(register struct re_guts *g, int c);
1308 static int /* predicate */
1309 isinsets(struct re_guts *g, int c)
1311 register uch *col;
1312 register int i;
1313 register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1314 register unsigned uc = (unsigned char)c;
1316 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1317 if (col[uc] != 0)
1318 return(1);
1319 return(0);
1323 - samesets - are these two characters in exactly the same sets?
1324 == static int samesets(register struct re_guts *g, int c1, int c2);
1326 static int /* predicate */
1327 samesets(struct re_guts *g, int c1, int c2)
1329 register uch *col;
1330 register int i;
1331 register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1332 register unsigned uc1 = (unsigned char)c1;
1333 register unsigned uc2 = (unsigned char)c2;
1335 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1336 if (col[uc1] != col[uc2])
1337 return(0);
1338 return(1);
1342 - categorize - sort out character categories
1343 == static void categorize(struct parse *p, register struct re_guts *g);
1345 static void
1346 categorize(struct parse *p, struct re_guts *g)
1348 register cat_t *cats = g->categories;
1349 register int c;
1350 register int c2;
1351 register cat_t cat;
1353 /* avoid making error situations worse */
1354 if (p->error != 0)
1355 return;
1357 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1358 if (cats[c] == 0 && isinsets(g, c)) {
1359 cat = g->ncategories++;
1360 cats[c] = cat;
1361 for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1362 if (cats[c2] == 0 && samesets(g, c, c2))
1363 cats[c2] = cat;
1368 - dupl - emit a duplicate of a bunch of sops
1369 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1371 static sopno /* start of duplicate */
1372 dupl(struct parse *p,
1373 sopno start, /* from here */
1374 sopno finish) /* to this less one */
1376 register sopno ret = HERE();
1377 register sopno len = finish - start;
1379 assert(finish >= start);
1380 if (len == 0)
1381 return(ret);
1382 enlarge(p, p->ssize + len); /* this many unexpected additions */
1383 assert(p->ssize >= p->slen + len);
1384 (void) memcpy((char *)(p->strip + p->slen),
1385 (char *)(p->strip + start), (size_t)len*sizeof(sop));
1386 p->slen += len;
1387 return(ret);
1391 - doemit - emit a strip operator
1392 == static void doemit(register struct parse *p, sop op, size_t opnd);
1394 * It might seem better to implement this as a macro with a function as
1395 * hard-case backup, but it's just too big and messy unless there are
1396 * some changes to the data structures. Maybe later.
1398 static void
1399 doemit(struct parse *p, sop op, size_t opnd)
1401 /* avoid making error situations worse */
1402 if (p->error != 0)
1403 return;
1405 /* deal with oversize operands ("can't happen", more or less) */
1406 assert(opnd < 1<<OPSHIFT);
1408 /* deal with undersized strip */
1409 if (p->slen >= p->ssize)
1410 enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1411 assert(p->slen < p->ssize);
1413 /* finally, it's all reduced to the easy case */
1414 p->strip[p->slen++] = SOP(op, opnd);
1418 - doinsert - insert a sop into the strip
1419 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1421 static void
1422 doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
1424 register sopno sn;
1425 register sop s;
1426 register int i;
1428 /* avoid making error situations worse */
1429 if (p->error != 0)
1430 return;
1432 sn = HERE();
1433 EMIT(op, opnd); /* do checks, ensure space */
1434 assert(HERE() == sn+1);
1435 s = p->strip[sn];
1437 /* adjust paren pointers */
1438 assert(pos > 0);
1439 for (i = 1; i < NPAREN; i++) {
1440 if (p->pbegin[i] >= pos) {
1441 p->pbegin[i]++;
1443 if (p->pend[i] >= pos) {
1444 p->pend[i]++;
1448 memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1449 (HERE()-pos-1)*sizeof(sop));
1450 p->strip[pos] = s;
1454 - dofwd - complete a forward reference
1455 == static void dofwd(register struct parse *p, sopno pos, sop value);
1457 static void
1458 dofwd(struct parse *p, sopno pos, sop value)
1460 /* avoid making error situations worse */
1461 if (p->error != 0)
1462 return;
1464 assert(value < 1<<OPSHIFT);
1465 p->strip[pos] = OP(p->strip[pos]) | value;
1469 - enlarge - enlarge the strip
1470 == static void enlarge(register struct parse *p, sopno size);
1472 static void
1473 enlarge(struct parse *p, sopno size)
1475 register sop *sp;
1477 if (p->ssize >= size)
1478 return;
1480 sp = (sop *)realloc(p->strip, size*sizeof(sop));
1481 if (sp == NULL) {
1482 SETERROR(REG_ESPACE);
1483 return;
1485 p->strip = sp;
1486 p->ssize = size;
1490 - stripsnug - compact the strip
1491 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1493 static void
1494 stripsnug(struct parse *p, struct re_guts *g)
1496 g->nstates = p->slen;
1497 g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
1498 if (g->strip == NULL) {
1499 SETERROR(REG_ESPACE);
1500 g->strip = p->strip;
1505 - findmust - fill in must and mlen with longest mandatory literal string
1506 == static void findmust(register struct parse *p, register struct re_guts *g);
1508 * This algorithm could do fancy things like analyzing the operands of |
1509 * for common subsequences. Someday. This code is simple and finds most
1510 * of the interesting cases.
1512 * Note that must and mlen got initialized during setup.
1514 static void
1515 findmust(struct parse *p, struct re_guts *g)
1517 register sop *scan;
1518 sop *start;
1519 register sop *newstart;
1520 register sopno newlen;
1521 register sop s;
1522 register char *cp;
1523 register sopno i;
1525 /* avoid making error situations worse */
1526 if (p->error != 0)
1527 return;
1529 /* find the longest OCHAR sequence in strip */
1530 newlen = 0;
1531 scan = g->strip + 1;
1532 do {
1533 s = *scan++;
1534 switch (OP(s)) {
1535 case OCHAR: /* sequence member */
1536 if (newlen == 0) /* new sequence */
1537 newstart = scan - 1;
1538 newlen++;
1539 break;
1540 case OPLUS_: /* things that don't break one */
1541 case OLPAREN:
1542 case ORPAREN:
1543 break;
1544 case OQUEST_: /* things that must be skipped */
1545 case OCH_:
1546 scan--;
1547 do {
1548 scan += OPND(s);
1549 s = *scan;
1550 /* assert() interferes w debug printouts */
1551 if (OP(s) != O_QUEST && OP(s) != O_CH &&
1552 OP(s) != OOR2) {
1553 g->iflags |= BAD;
1554 return;
1556 } while (OP(s) != O_QUEST && OP(s) != O_CH);
1557 /* fallthrough */
1558 default: /* things that break a sequence */
1559 if (newlen > g->mlen) { /* ends one */
1560 start = newstart;
1561 g->mlen = newlen;
1563 newlen = 0;
1564 break;
1566 } while (OP(s) != OEND);
1568 if (g->mlen == 0) /* there isn't one */
1569 return;
1571 /* turn it into a character string */
1572 g->must = malloc((size_t)g->mlen + 1);
1573 if (g->must == NULL) { /* argh; just forget it */
1574 g->mlen = 0;
1575 return;
1577 cp = g->must;
1578 scan = start;
1579 for (i = g->mlen; i > 0; i--) {
1580 while (OP(s = *scan++) != OCHAR)
1581 continue;
1582 assert(cp < g->must + g->mlen);
1583 *cp++ = (char)OPND(s);
1585 assert(cp == g->must + g->mlen);
1586 *cp++ = '\0'; /* just on general principles */
1590 - pluscount - count + nesting
1591 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1593 static sopno /* nesting depth */
1594 pluscount(struct parse *p, struct re_guts *g)
1596 register sop *scan;
1597 register sop s;
1598 register sopno plusnest = 0;
1599 register sopno maxnest = 0;
1601 if (p->error != 0)
1602 return(0); /* there may not be an OEND */
1604 scan = g->strip + 1;
1605 do {
1606 s = *scan++;
1607 switch (OP(s)) {
1608 case OPLUS_:
1609 plusnest++;
1610 break;
1611 case O_PLUS:
1612 if (plusnest > maxnest)
1613 maxnest = plusnest;
1614 plusnest--;
1615 break;
1617 } while (OP(s) != OEND);
1618 if (plusnest != 0)
1619 g->iflags |= BAD;
1620 return(maxnest);