Sync usage with man page.
[netbsd-mini2440.git] / dist / nvi / regex / regcomp.c
blob64c00dbd86f15819ed5b42585f75c7cb310dab01
1 /* $NetBSD: regcomp.c,v 1.4 2009/01/18 03:45:50 lukem Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5 * Copyright (c) 1992, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Henry Spencer of the University of Toronto.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
39 * @(#)regcomp.c 8.4 (Berkeley) 3/19/94
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid[] = "@(#)regcomp.c 8.4 (Berkeley) 3/19/94";
44 #endif /* LIBC_SCCS and not lint */
46 #include <sys/types.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <limits.h>
51 #include <stdlib.h>
52 #include <regex.h>
54 #include "utils.h"
55 #include "regex2.h"
57 #include "cclass.h"
58 #include "cname.h"
61 * parse structure, passed up and down to avoid global variables and
62 * other clumsinesses
64 struct parse {
65 RCHAR_T *next; /* next character in RE */
66 RCHAR_T *end; /* end of string (-> NUL normally) */
67 int error; /* has an error been seen? */
68 sop *strip; /* malloced strip */
69 RCHAR_T *stripdata; /* malloced stripdata */
70 sopno ssize; /* malloced strip size (allocated) */
71 sopno slen; /* malloced strip length (used) */
72 int ncsalloc; /* number of csets allocated */
73 struct re_guts *g;
74 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
75 sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
76 sopno pend[NPAREN]; /* -> ) ([0] unused) */
79 /* ========= begin header generated by ./mkh ========= */
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
84 /* === regcomp.c === */
85 static void p_ere __P((struct parse *p, int stop));
86 static void p_ere_exp __P((struct parse *p));
87 static void p_str __P((struct parse *p));
88 static void p_bre __P((struct parse *p, int end1, int end2));
89 static int p_simp_re __P((struct parse *p, int starordinary));
90 static int p_count __P((struct parse *p));
91 static void p_bracket __P((struct parse *p));
92 static void p_b_term __P((struct parse *p, cset *cs));
93 static void p_b_cclass __P((struct parse *p, cset *cs));
94 static void p_b_eclass __P((struct parse *p, cset *cs));
95 static char p_b_symbol __P((struct parse *p));
96 static char p_b_coll_elem __P((struct parse *p, int endc));
97 static char othercase __P((int ch));
98 static void bothcases __P((struct parse *p, int ch));
99 static void ordinary __P((struct parse *p, int ch));
100 static void nonnewline __P((struct parse *p));
101 static void repeat __P((struct parse *p, sopno start, int from, int to));
102 static int seterr __P((struct parse *p, int e));
103 static cset *allocset __P((struct parse *p));
104 static void freeset __P((struct parse *p, cset *cs));
105 static int freezeset __P((struct parse *p, cset *cs));
106 static int firstch __P((struct parse *p, cset *cs));
107 static int nch __P((struct parse *p, cset *cs));
108 static void mcadd __P((struct parse *p, cset *cs, const char *cp));
109 #ifdef notdef
110 static void mcsub __P((cset *cs, char *cp));
111 static int mcin __P((cset *cs, char *cp));
112 static char *mcfind __P((cset *cs, char *cp));
113 #endif
114 static void mcinvert __P((struct parse *p, cset *cs));
115 static void mccase __P((struct parse *p, cset *cs));
116 #ifdef notdef
117 static int isinsets __P((struct re_guts *g, int c));
118 static int samesets __P((struct re_guts *g, int c1, int c2));
119 #endif
120 static void categorize __P((struct parse *p, struct re_guts *g));
121 static sopno dupl __P((struct parse *p, sopno start, sopno finish));
122 static void doemit __P((struct parse *p, sop op, size_t opnd));
123 static void doinsert __P((struct parse *p, sop op, size_t opnd, sopno pos));
124 static void dofwd __P((struct parse *p, sopno pos, sop value));
125 static void enlarge __P((struct parse *p, sopno size));
126 static void stripsnug __P((struct parse *p, struct re_guts *g));
127 static void findmust __P((struct parse *p, struct re_guts *g));
128 static sopno pluscount __P((struct parse *p, struct re_guts *g));
130 #ifdef __cplusplus
132 #endif
133 /* ========= end header generated by ./mkh ========= */
135 static RCHAR_T nuls[10]; /* place to point scanner in event of error */
138 * macros for use with parse structure
139 * BEWARE: these know that the parse structure is named `p' !!!
141 #define PEEK() (*p->next)
142 #define PEEK2() (*(p->next+1))
143 #define MORE() (p->next < p->end)
144 #define MORE2() (p->next+1 < p->end)
145 #define SEE(c) (MORE() && PEEK() == (c))
146 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
147 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
148 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
149 #define NEXT() (p->next++)
150 #define NEXT2() (p->next += 2)
151 #define NEXTn(n) (p->next += (n))
152 #define GETNEXT() (*p->next++)
153 #define SETERROR(e) seterr(p, (e))
154 #define REQUIRE(co, e) ((co) || SETERROR(e))
155 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
156 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
157 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
158 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
159 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
160 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
161 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
162 #define HERE() (p->slen)
163 #define THERE() (p->slen - 1)
164 #define THERETHERE() (p->slen - 2)
165 #define DROP(n) (p->slen -= (n))
167 #ifndef NDEBUG
168 static int never = 0; /* for use in asserts; shuts lint up */
169 #else
170 #define never 0 /* some <assert.h>s have bugs too */
171 #endif
174 - regcomp - interface for parser and compilation
175 = extern int regcomp(regex_t *, const RCHAR_T *, int);
176 = #define REG_BASIC 0000
177 = #define REG_EXTENDED 0001
178 = #define REG_ICASE 0002
179 = #define REG_NOSUB 0004
180 = #define REG_NEWLINE 0010
181 = #define REG_NOSPEC 0020
182 = #define REG_PEND 0040
183 = #define REG_DUMP 0200
185 int /* 0 success, otherwise REG_something */
186 regcomp(regex_t *preg, const RCHAR_T *pattern, int cflags)
188 struct parse pa;
189 register struct re_guts *g;
190 register struct parse *p = &pa;
191 register int i;
192 register size_t len;
193 #ifdef REDEBUG
194 # define GOODFLAGS(f) (f)
195 #else
196 # define GOODFLAGS(f) ((f)&~REG_DUMP)
197 #endif
199 cflags = GOODFLAGS(cflags);
200 if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
201 return(REG_INVARG);
203 if (cflags&REG_PEND) {
204 if (preg->re_endp < pattern)
205 return(REG_INVARG);
206 len = preg->re_endp - pattern;
207 } else
208 len = STRLEN(pattern);
210 /* do the mallocs early so failure handling is easy */
211 g = (struct re_guts *)malloc(sizeof(struct re_guts) +
212 (NC-1)*sizeof(cat_t));
213 if (g == NULL)
214 return(REG_ESPACE);
215 p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
216 p->strip = (sop *)malloc(p->ssize * sizeof(sop));
217 if (p->strip == NULL) {
218 free((char *)g);
219 return(REG_ESPACE);
221 p->stripdata = (RCHAR_T *)malloc(p->ssize * sizeof(RCHAR_T));
222 if (p->stripdata == NULL) {
223 free((char *)p->strip);
224 free((char *)g);
225 return(REG_ESPACE);
227 p->slen = 0;
229 /* set things up */
230 p->g = g;
231 p->next = (RCHAR_T *)__UNCONST(pattern); /* convenience; we do not modify it */
232 p->end = p->next + len;
233 p->error = 0;
234 p->ncsalloc = 0;
235 for (i = 0; i < NPAREN; i++) {
236 p->pbegin[i] = 0;
237 p->pend[i] = 0;
239 g->csetsize = NC;
240 g->sets = NULL;
241 g->setbits = NULL;
242 g->ncsets = 0;
243 g->cflags = cflags;
244 g->iflags = 0;
245 g->nbol = 0;
246 g->neol = 0;
247 g->must = NULL;
248 g->mlen = 0;
249 g->nsub = 0;
250 #if 0
251 g->ncategories = 1; /* category 0 is "everything else" */
252 g->categories = &g->catspace[-(CHAR_MIN)];
253 (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
254 #endif
255 g->backrefs = 0;
257 /* do it */
258 EMIT(OEND, 0);
259 g->firststate = THERE();
260 if (cflags&REG_EXTENDED)
261 p_ere(p, OUT);
262 else if (cflags&REG_NOSPEC)
263 p_str(p);
264 else
265 p_bre(p, OUT, OUT);
266 EMIT(OEND, 0);
267 g->laststate = THERE();
269 /* tidy up loose ends and fill things in */
270 categorize(p, g);
271 stripsnug(p, g);
272 findmust(p, g);
273 g->nplus = pluscount(p, g);
274 g->magic = MAGIC2;
275 preg->re_nsub = g->nsub;
276 preg->re_g = g;
277 preg->re_magic = MAGIC1;
278 #ifndef REDEBUG
279 /* not debugging, so can't rely on the assert() in regexec() */
280 if (g->iflags&BAD)
281 SETERROR(REG_ASSERT);
282 #endif
284 /* win or lose, we're done */
285 if (p->error != 0) /* lose */
286 regfree(preg);
287 return(p->error);
291 - p_ere - ERE parser top level, concatenation and alternation
292 == static void p_ere(register struct parse *p, int stop);
294 static void
295 p_ere(register struct parse *p, int stop)
297 /* character this ERE should end at */
299 register char c;
300 register sopno prevback = 0;
301 register sopno prevfwd = 0;
302 register sopno conc;
303 register int first = 1; /* is this the first alternative? */
305 for (;;) {
306 /* do a bunch of concatenated expressions */
307 conc = HERE();
308 while (MORE() && (c = PEEK()) != '|' && c != stop)
309 p_ere_exp(p);
310 (void)REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
312 if (!EAT('|'))
313 break; /* NOTE BREAK OUT */
315 if (first) {
316 INSERT(OCH_, conc); /* offset is wrong */
317 prevfwd = conc;
318 prevback = conc;
319 first = 0;
321 ASTERN(OOR1, prevback);
322 prevback = THERE();
323 AHEAD(prevfwd); /* fix previous offset */
324 prevfwd = HERE();
325 EMIT(OOR2, 0); /* offset is very wrong */
328 if (!first) { /* tail-end fixups */
329 AHEAD(prevfwd);
330 ASTERN(O_CH, prevback);
333 assert(!MORE() || SEE(stop));
337 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
338 == static void p_ere_exp(register struct parse *p);
340 static void
341 p_ere_exp(register struct parse *p)
343 register char c;
344 register sopno pos;
345 register int count;
346 register int count2;
347 register sopno subno;
348 int wascaret = 0;
350 assert(MORE()); /* caller should have ensured this */
351 c = GETNEXT();
353 pos = HERE();
354 switch (c) {
355 case '(':
356 (void)REQUIRE(MORE(), REG_EPAREN);
357 p->g->nsub++;
358 subno = p->g->nsub;
359 if (subno < NPAREN)
360 p->pbegin[subno] = HERE();
361 EMIT(OLPAREN, subno);
362 if (!SEE(')'))
363 p_ere(p, ')');
364 if (subno < NPAREN) {
365 p->pend[subno] = HERE();
366 assert(p->pend[subno] != 0);
368 EMIT(ORPAREN, subno);
369 (void)MUSTEAT(')', REG_EPAREN);
370 break;
371 #ifndef POSIX_MISTAKE
372 case ')': /* happens only if no current unmatched ( */
374 * You may ask, why the ifndef? Because I didn't notice
375 * this until slightly too late for 1003.2, and none of the
376 * other 1003.2 regular-expression reviewers noticed it at
377 * all. So an unmatched ) is legal POSIX, at least until
378 * we can get it fixed.
380 SETERROR(REG_EPAREN);
381 break;
382 #endif
383 case '^':
384 EMIT(OBOL, 0);
385 p->g->iflags |= USEBOL;
386 p->g->nbol++;
387 wascaret = 1;
388 break;
389 case '$':
390 EMIT(OEOL, 0);
391 p->g->iflags |= USEEOL;
392 p->g->neol++;
393 break;
394 case '|':
395 SETERROR(REG_EMPTY);
396 break;
397 case '*':
398 case '+':
399 case '?':
400 SETERROR(REG_BADRPT);
401 break;
402 case '.':
403 if (p->g->cflags&REG_NEWLINE)
404 nonnewline(p);
405 else
406 EMIT(OANY, 0);
407 break;
408 case '[':
409 p_bracket(p);
410 break;
411 case '\\':
412 (void)REQUIRE(MORE(), REG_EESCAPE);
413 c = GETNEXT();
414 ordinary(p, c);
415 break;
416 case '{': /* okay as ordinary except if digit follows */
417 (void)REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT);
418 /* FALLTHROUGH */
419 default:
420 ordinary(p, c);
421 break;
424 if (!MORE())
425 return;
426 c = PEEK();
427 /* we call { a repetition if followed by a digit */
428 if (!( c == '*' || c == '+' || c == '?' ||
429 (c == '{' && MORE2() && isdigit(PEEK2())) ))
430 return; /* no repetition, we're done */
431 NEXT();
433 (void)REQUIRE(!wascaret, REG_BADRPT);
434 switch (c) {
435 case '*': /* implemented as +? */
436 /* this case does not require the (y|) trick, noKLUDGE */
437 INSERT(OPLUS_, pos);
438 ASTERN(O_PLUS, pos);
439 INSERT(OQUEST_, pos);
440 ASTERN(O_QUEST, pos);
441 break;
442 case '+':
443 INSERT(OPLUS_, pos);
444 ASTERN(O_PLUS, pos);
445 break;
446 case '?':
447 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
448 INSERT(OCH_, pos); /* offset slightly wrong */
449 ASTERN(OOR1, pos); /* this one's right */
450 AHEAD(pos); /* fix the OCH_ */
451 EMIT(OOR2, 0); /* offset very wrong... */
452 AHEAD(THERE()); /* ...so fix it */
453 ASTERN(O_CH, THERETHERE());
454 break;
455 case '{':
456 count = p_count(p);
457 if (EAT(',')) {
458 if (isdigit(PEEK())) {
459 count2 = p_count(p);
460 (void)REQUIRE(count <= count2, REG_BADBR);
461 } else /* single number with comma */
462 count2 = INFINITY;
463 } else /* just a single number */
464 count2 = count;
465 repeat(p, pos, count, count2);
466 if (!EAT('}')) { /* error heuristics */
467 while (MORE() && PEEK() != '}')
468 NEXT();
469 (void)REQUIRE(MORE(), REG_EBRACE);
470 SETERROR(REG_BADBR);
472 break;
475 if (!MORE())
476 return;
477 c = PEEK();
478 if (!( c == '*' || c == '+' || c == '?' ||
479 (c == '{' && MORE2() && isdigit(PEEK2())) ) )
480 return;
481 SETERROR(REG_BADRPT);
485 - p_str - string (no metacharacters) "parser"
486 == static void p_str(register struct parse *p);
488 static void
489 p_str(register struct parse *p)
491 (void)REQUIRE(MORE(), REG_EMPTY);
492 while (MORE())
493 ordinary(p, GETNEXT());
497 - p_bre - BRE parser top level, anchoring and concatenation
498 == static void p_bre(register struct parse *p, register int end1, \
499 == register int end2);
500 * Giving end1 as OUT essentially eliminates the end1/end2 check.
502 * This implementation is a bit of a kludge, in that a trailing $ is first
503 * taken as an ordinary character and then revised to be an anchor. The
504 * only undesirable side effect is that '$' gets included as a character
505 * category in such cases. This is fairly harmless; not worth fixing.
506 * The amount of lookahead needed to avoid this kludge is excessive.
508 static void
509 p_bre(register struct parse *p, register int end1, register int end2)
511 /* first terminating character */
512 /* second terminating character */
514 register sopno start = HERE();
515 register int first = 1; /* first subexpression? */
516 register int wasdollar = 0;
518 if (EAT('^')) {
519 EMIT(OBOL, 0);
520 p->g->iflags |= USEBOL;
521 p->g->nbol++;
523 while (MORE() && !SEETWO(end1, end2)) {
524 wasdollar = p_simp_re(p, first);
525 first = 0;
527 if (wasdollar) { /* oops, that was a trailing anchor */
528 DROP(1);
529 EMIT(OEOL, 0);
530 p->g->iflags |= USEEOL;
531 p->g->neol++;
534 (void)REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
538 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
539 == static int p_simp_re(register struct parse *p, int starordinary);
541 static int /* was the simple RE an unbackslashed $? */
542 p_simp_re(register struct parse *p, int starordinary)
544 /* is a leading * an ordinary character? */
546 register int c;
547 register int count;
548 register int count2;
549 register sopno pos;
550 register int i;
551 register sopno subno;
552 int backsl;
554 pos = HERE(); /* repetion op, if any, covers from here */
556 assert(MORE()); /* caller should have ensured this */
557 c = GETNEXT();
558 backsl = c == '\\';
559 if (backsl) {
560 (void)REQUIRE(MORE(), REG_EESCAPE);
561 c = (unsigned char)GETNEXT();
562 switch (c) {
563 case '{':
564 SETERROR(REG_BADRPT);
565 break;
566 case '(':
567 p->g->nsub++;
568 subno = p->g->nsub;
569 if (subno < NPAREN)
570 p->pbegin[subno] = HERE();
571 EMIT(OLPAREN, subno);
572 /* the MORE here is an error heuristic */
573 if (MORE() && !SEETWO('\\', ')'))
574 p_bre(p, '\\', ')');
575 if (subno < NPAREN) {
576 p->pend[subno] = HERE();
577 assert(p->pend[subno] != 0);
579 EMIT(ORPAREN, subno);
580 (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
581 break;
582 case ')': /* should not get here -- must be user */
583 case '}':
584 SETERROR(REG_EPAREN);
585 break;
586 case '1':
587 case '2':
588 case '3':
589 case '4':
590 case '5':
591 case '6':
592 case '7':
593 case '8':
594 case '9':
595 i = c - '0';
596 assert(i < NPAREN);
597 if (p->pend[i] != 0) {
598 assert(i <= p->g->nsub);
599 EMIT(OBACK_, i);
600 assert(p->pbegin[i] != 0);
601 assert(p->strip[p->pbegin[i]] == OLPAREN);
602 assert(p->strip[p->pend[i]] == ORPAREN);
603 (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
604 EMIT(O_BACK, i);
605 } else
606 SETERROR(REG_ESUBREG);
607 p->g->backrefs = 1;
608 break;
609 default:
610 ordinary(p, c);
611 break;
613 } else {
614 switch (c) {
615 case '.':
616 if (p->g->cflags&REG_NEWLINE)
617 nonnewline(p);
618 else
619 EMIT(OANY, 0);
620 break;
621 case '[':
622 p_bracket(p);
623 break;
624 case '*':
625 (void)REQUIRE(starordinary, REG_BADRPT);
626 /* FALLTHROUGH */
627 default:
628 ordinary(p, c);
629 break;
633 if (EAT('*')) { /* implemented as +? */
634 /* this case does not require the (y|) trick, noKLUDGE */
635 INSERT(OPLUS_, pos);
636 ASTERN(O_PLUS, pos);
637 INSERT(OQUEST_, pos);
638 ASTERN(O_QUEST, pos);
639 } else if (EATTWO('\\', '{')) {
640 count = p_count(p);
641 if (EAT(',')) {
642 if (MORE() && isdigit(PEEK())) {
643 count2 = p_count(p);
644 (void)REQUIRE(count <= count2, REG_BADBR);
645 } else /* single number with comma */
646 count2 = INFINITY;
647 } else /* just a single number */
648 count2 = count;
649 repeat(p, pos, count, count2);
650 if (!EATTWO('\\', '}')) { /* error heuristics */
651 while (MORE() && !SEETWO('\\', '}'))
652 NEXT();
653 (void)REQUIRE(MORE(), REG_EBRACE);
654 SETERROR(REG_BADBR);
656 } else if (!backsl && c == (unsigned char)'$') /* $ (but not \$) ends it */
657 return(1);
659 return(0);
663 - p_count - parse a repetition count
664 == static int p_count(register struct parse *p);
666 static int /* the value */
667 p_count(register struct parse *p)
669 register int count = 0;
670 register int ndigits = 0;
672 while (MORE() && isdigit(PEEK()) && count <= DUPMAX) {
673 count = count*10 + (GETNEXT() - '0');
674 ndigits++;
677 (void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
678 return(count);
682 - p_bracket - parse a bracketed character list
683 == static void p_bracket(register struct parse *p);
685 * Note a significant property of this code: if the allocset() did SETERROR,
686 * no set operations are done.
688 static void
689 p_bracket(register struct parse *p)
691 register cset *cs = allocset(p);
692 register int invert = 0;
693 static RCHAR_T bow[] = { '[', ':', '<', ':', ']', ']' };
694 static RCHAR_T eow[] = { '[', ':', '>', ':', ']', ']' };
696 /* Dept of Truly Sickening Special-Case Kludges */
697 if (p->next + 5 < p->end && MEMCMP(p->next, bow, 6) == 0) {
698 EMIT(OBOW, 0);
699 NEXTn(6);
700 return;
702 if (p->next + 5 < p->end && MEMCMP(p->next, eow, 6) == 0) {
703 EMIT(OEOW, 0);
704 NEXTn(6);
705 return;
708 if (EAT('^'))
709 invert++; /* make note to invert set at end */
710 if (EAT(']'))
711 CHadd(cs, ']');
712 else if (EAT('-'))
713 CHadd(cs, '-');
714 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
715 p_b_term(p, cs);
716 if (EAT('-'))
717 CHadd(cs, '-');
718 (void)MUSTEAT(']', REG_EBRACK);
720 if (p->error != 0) /* don't mess things up further */
721 return;
723 if (p->g->cflags&REG_ICASE) {
724 register int i;
725 register int ci;
727 for (i = p->g->csetsize - 1; i >= 0; i--)
728 if (CHIN(cs, i) && isalpha(i)) {
729 ci = othercase(i);
730 if (ci != i)
731 CHadd(cs, ci);
733 if (cs->multis != NULL)
734 mccase(p, cs);
736 if (invert) {
737 register int i;
739 for (i = p->g->csetsize - 1; i >= 0; i--)
740 if (CHIN(cs, i))
741 CHsub(cs, i);
742 else
743 CHadd(cs, i);
744 if (p->g->cflags&REG_NEWLINE)
745 CHsub(cs, '\n');
746 if (cs->multis != NULL)
747 mcinvert(p, cs);
750 assert(cs->multis == NULL); /* xxx */
752 if (nch(p, cs) == 1) { /* optimize singleton sets */
753 ordinary(p, firstch(p, cs));
754 freeset(p, cs);
755 } else
756 EMIT(OANYOF, freezeset(p, cs));
760 - p_b_term - parse one term of a bracketed character list
761 == static void p_b_term(register struct parse *p, register cset *cs);
763 static void
764 p_b_term(register struct parse *p, register cset *cs)
766 register char c;
767 register char start, finish;
768 register int i;
770 /* classify what we've got */
771 switch ((MORE()) ? PEEK() : '\0') {
772 case '[':
773 c = (MORE2()) ? PEEK2() : '\0';
774 break;
775 case '-':
776 SETERROR(REG_ERANGE);
777 return; /* NOTE RETURN */
778 break;
779 default:
780 c = '\0';
781 break;
784 switch (c) {
785 case ':': /* character class */
786 NEXT2();
787 (void)REQUIRE(MORE(), REG_EBRACK);
788 c = PEEK();
789 (void)REQUIRE(c != '-' && c != ']', REG_ECTYPE);
790 p_b_cclass(p, cs);
791 (void)REQUIRE(MORE(), REG_EBRACK);
792 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
793 break;
794 case '=': /* equivalence class */
795 NEXT2();
796 (void)REQUIRE(MORE(), REG_EBRACK);
797 c = PEEK();
798 (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
799 p_b_eclass(p, cs);
800 (void)REQUIRE(MORE(), REG_EBRACK);
801 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
802 break;
803 default: /* symbol, ordinary character, or range */
804 /* xxx revision needed for multichar stuff */
805 start = p_b_symbol(p);
806 if (SEE('-') && MORE2() && PEEK2() != ']') {
807 /* range */
808 NEXT();
809 if (EAT('-'))
810 finish = '-';
811 else
812 finish = p_b_symbol(p);
813 } else
814 finish = start;
815 /* xxx what about signed chars here... */
816 (void)REQUIRE(start <= finish, REG_ERANGE);
817 for (i = start; i <= finish; i++)
818 CHadd(cs, i);
819 break;
824 - p_b_cclass - parse a character-class name and deal with it
825 == static void p_b_cclass(register struct parse *p, register cset *cs);
827 static void
828 p_b_cclass(register struct parse *p, register cset *cs)
830 register RCHAR_T *sp = p->next;
831 register struct cclass *cp;
832 register size_t len;
833 register const char *u;
834 register char c;
836 while (MORE() && isalpha(PEEK()))
837 NEXT();
838 len = p->next - sp;
839 for (cp = cclasses; cp->name != NULL; cp++)
840 if (STRLEN(cp->name) == len && MEMCMP(cp->name, sp, len))
841 break;
842 if (cp->name == NULL) {
843 /* oops, didn't find it */
844 SETERROR(REG_ECTYPE);
845 return;
848 u = cp->chars;
849 while ((c = *u++) != '\0')
850 CHadd(cs, c);
851 for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
852 MCadd(p, cs, u);
856 - p_b_eclass - parse an equivalence-class name and deal with it
857 == static void p_b_eclass(register struct parse *p, register cset *cs);
859 * This implementation is incomplete. xxx
861 static void
862 p_b_eclass(register struct parse *p, register cset *cs)
864 register char c;
866 c = p_b_coll_elem(p, '=');
867 CHadd(cs, c);
871 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
872 == static char p_b_symbol(register struct parse *p);
874 static char /* value of symbol */
875 p_b_symbol(register struct parse *p)
877 register char value;
879 (void)REQUIRE(MORE(), REG_EBRACK);
880 if (!EATTWO('[', '.'))
881 return(GETNEXT());
883 /* collating symbol */
884 value = p_b_coll_elem(p, '.');
885 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
886 return(value);
890 - p_b_coll_elem - parse a collating-element name and look it up
891 == static char p_b_coll_elem(register struct parse *p, int endc);
893 static char /* value of collating element */
894 p_b_coll_elem(register struct parse *p, int endc)
896 /* name ended by endc,']' */
898 register RCHAR_T *sp = p->next;
899 register struct cname *cp;
900 register size_t len;
902 while (MORE() && !SEETWO(endc, ']'))
903 NEXT();
904 if (!MORE()) {
905 SETERROR(REG_EBRACK);
906 return(0);
908 len = p->next - sp;
909 for (cp = cnames; cp->name != NULL; cp++)
910 if (STRLEN(cp->name) == len && MEMCMP(cp->name, sp, len))
911 return(cp->code); /* known name */
912 if (len == 1)
913 return(*sp); /* single character */
914 SETERROR(REG_ECOLLATE); /* neither */
915 return(0);
919 - othercase - return the case counterpart of an alphabetic
920 == static char othercase(int ch);
922 static char /* if no counterpart, return ch */
923 othercase(int ch)
925 assert(isalpha(ch));
926 if (isupper(ch))
927 return(tolower(ch));
928 else if (islower(ch))
929 return(toupper(ch));
930 else /* peculiar, but could happen */
931 return(ch);
935 - bothcases - emit a dualcase version of a two-case character
936 == static void bothcases(register struct parse *p, int ch);
938 * Boy, is this implementation ever a kludge...
940 static void
941 bothcases(register struct parse *p, int ch)
943 register RCHAR_T *oldnext = p->next;
944 register RCHAR_T *oldend = p->end;
945 RCHAR_T bracket[3];
947 assert(othercase(ch) != ch); /* p_bracket() would recurse */
948 p->next = bracket;
949 p->end = bracket+2;
950 bracket[0] = ch;
951 bracket[1] = ']';
952 bracket[2] = '\0';
953 p_bracket(p);
954 assert(p->next == bracket+2);
955 p->next = oldnext;
956 p->end = oldend;
960 - ordinary - emit an ordinary character
961 == static void ordinary(register struct parse *p, register int ch);
963 static void
964 ordinary(register struct parse *p, register int ch)
967 register cat_t *cap = p->g->categories;
970 if ((p->g->cflags&REG_ICASE) && isalpha(ch) && othercase(ch) != ch)
971 bothcases(p, ch);
972 else {
973 EMIT(OCHAR, (UCHAR_T)ch);
975 if (cap[ch] == 0)
976 cap[ch] = p->g->ncategories++;
982 - nonnewline - emit REG_NEWLINE version of OANY
983 == static void nonnewline(register struct parse *p);
985 * Boy, is this implementation ever a kludge...
987 static void
988 nonnewline(register struct parse *p)
990 register RCHAR_T *oldnext = p->next;
991 register RCHAR_T *oldend = p->end;
992 RCHAR_T bracket[4];
994 p->next = bracket;
995 p->end = bracket+3;
996 bracket[0] = '^';
997 bracket[1] = '\n';
998 bracket[2] = ']';
999 bracket[3] = '\0';
1000 p_bracket(p);
1001 assert(p->next == bracket+3);
1002 p->next = oldnext;
1003 p->end = oldend;
1007 - repeat - generate code for a bounded repetition, recursively if needed
1008 == static void repeat(register struct parse *p, sopno start, int from, int to);
1010 static void
1011 repeat(register struct parse *p, sopno start, int from, int to)
1013 /* operand from here to end of strip */
1014 /* repeated from this number */
1015 /* to this number of times (maybe INFINITY) */
1017 register sopno finish = HERE();
1018 # define N 2
1019 # define INF 3
1020 # define REP(f, t) ((f)*8 + (t))
1021 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1022 register sopno copy;
1024 if (p->error != 0) /* head off possible runaway recursion */
1025 return;
1027 assert(from <= to);
1029 switch (REP(MAP(from), MAP(to))) {
1030 case REP(0, 0): /* must be user doing this */
1031 DROP(finish-start); /* drop the operand */
1032 break;
1033 case REP(0, 1): /* as x{1,1}? */
1034 case REP(0, N): /* as x{1,n}? */
1035 case REP(0, INF): /* as x{1,}? */
1036 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1037 INSERT(OCH_, start); /* offset is wrong... */
1038 repeat(p, start+1, 1, to);
1039 ASTERN(OOR1, start);
1040 AHEAD(start); /* ... fix it */
1041 EMIT(OOR2, 0);
1042 AHEAD(THERE());
1043 ASTERN(O_CH, THERETHERE());
1044 break;
1045 case REP(1, 1): /* trivial case */
1046 /* done */
1047 break;
1048 case REP(1, N): /* as x?x{1,n-1} */
1049 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1050 INSERT(OCH_, start);
1051 ASTERN(OOR1, start);
1052 AHEAD(start);
1053 EMIT(OOR2, 0); /* offset very wrong... */
1054 AHEAD(THERE()); /* ...so fix it */
1055 ASTERN(O_CH, THERETHERE());
1056 copy = dupl(p, start+1, finish+1);
1057 assert(copy == finish+4);
1058 repeat(p, copy, 1, to-1);
1059 break;
1060 case REP(1, INF): /* as x+ */
1061 INSERT(OPLUS_, start);
1062 ASTERN(O_PLUS, start);
1063 break;
1064 case REP(N, N): /* as xx{m-1,n-1} */
1065 copy = dupl(p, start, finish);
1066 repeat(p, copy, from-1, to-1);
1067 break;
1068 case REP(N, INF): /* as xx{n-1,INF} */
1069 copy = dupl(p, start, finish);
1070 repeat(p, copy, from-1, to);
1071 break;
1072 default: /* "can't happen" */
1073 SETERROR(REG_ASSERT); /* just in case */
1074 break;
1079 - seterr - set an error condition
1080 == static int seterr(register struct parse *p, int e);
1082 static int /* useless but makes type checking happy */
1083 seterr(register struct parse *p, int e)
1085 if (p->error == 0) /* keep earliest error condition */
1086 p->error = e;
1087 p->next = nuls; /* try to bring things to a halt */
1088 p->end = nuls;
1089 return(0); /* make the return value well-defined */
1093 - allocset - allocate a set of characters for []
1094 == static cset *allocset(register struct parse *p);
1096 static cset *
1097 allocset(register struct parse *p)
1099 register int no = p->g->ncsets++;
1100 register size_t nc;
1101 register size_t nbytes;
1102 register cset *cs;
1103 register size_t css = (size_t)p->g->csetsize;
1104 register int i;
1106 if (no >= p->ncsalloc) { /* need another column of space */
1107 p->ncsalloc += CHAR_BIT;
1108 nc = p->ncsalloc;
1109 assert(nc % CHAR_BIT == 0);
1110 nbytes = nc / CHAR_BIT * css;
1111 if (p->g->sets == NULL)
1112 p->g->sets = (cset *)malloc(nc * sizeof(cset));
1113 else
1114 p->g->sets = (cset *)realloc((char *)p->g->sets,
1115 nc * sizeof(cset));
1116 if (p->g->setbits == NULL)
1117 p->g->setbits = (uch *)malloc(nbytes);
1118 else {
1119 p->g->setbits = (uch *)realloc((char *)p->g->setbits,
1120 nbytes);
1121 /* xxx this isn't right if setbits is now NULL */
1122 for (i = 0; i < no; i++)
1123 p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1125 if (p->g->sets != NULL && p->g->setbits != NULL)
1126 (void) memset((char *)p->g->setbits + (nbytes - css),
1127 0, css);
1128 else {
1129 no = 0;
1130 SETERROR(REG_ESPACE);
1131 /* caller's responsibility not to do set ops */
1135 assert(p->g->sets != NULL); /* xxx */
1136 cs = &p->g->sets[no];
1137 cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1138 cs->mask = 1 << ((no) % CHAR_BIT);
1139 cs->hash = 0;
1140 cs->smultis = 0;
1141 cs->multis = NULL;
1143 return(cs);
1147 - freeset - free a now-unused set
1148 == static void freeset(register struct parse *p, register cset *cs);
1150 static void
1151 freeset(register struct parse *p, register cset *cs)
1153 register size_t i;
1154 register cset *top = &p->g->sets[p->g->ncsets];
1155 register size_t css = (size_t)p->g->csetsize;
1157 for (i = 0; i < css; i++)
1158 CHsub(cs, i);
1159 if (cs == top-1) /* recover only the easy case */
1160 p->g->ncsets--;
1164 - freezeset - final processing on a set of characters
1165 == static int freezeset(register struct parse *p, register cset *cs);
1167 * The main task here is merging identical sets. This is usually a waste
1168 * of time (although the hash code minimizes the overhead), but can win
1169 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1170 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1171 * the same value!
1173 static int /* set number */
1174 freezeset(register struct parse *p, register cset *cs)
1176 register uch h = cs->hash;
1177 register size_t i;
1178 register cset *top = &p->g->sets[p->g->ncsets];
1179 register cset *cs2;
1180 register size_t css = (size_t)p->g->csetsize;
1182 /* look for an earlier one which is the same */
1183 for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1184 if (cs2->hash == h && cs2 != cs) {
1185 /* maybe */
1186 for (i = 0; i < css; i++)
1187 if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1188 break; /* no */
1189 if (i == css)
1190 break; /* yes */
1193 if (cs2 < top) { /* found one */
1194 freeset(p, cs);
1195 cs = cs2;
1198 return((int)(cs - p->g->sets));
1202 - firstch - return first character in a set (which must have at least one)
1203 == static int firstch(register struct parse *p, register cset *cs);
1205 static int /* character; there is no "none" value */
1206 firstch(register struct parse *p, register cset *cs)
1208 register size_t i;
1209 register size_t css = (size_t)p->g->csetsize;
1211 for (i = 0; i < css; i++)
1212 if (CHIN(cs, i))
1213 return((char)i);
1214 assert(never);
1215 return(0); /* arbitrary */
1219 - nch - number of characters in a set
1220 == static int nch(register struct parse *p, register cset *cs);
1222 static int
1223 nch(register struct parse *p, register cset *cs)
1225 register size_t i;
1226 register size_t css = (size_t)p->g->csetsize;
1227 register int n = 0;
1229 for (i = 0; i < css; i++)
1230 if (CHIN(cs, i))
1231 n++;
1232 return(n);
1236 - mcadd - add a collating element to a cset
1237 == static void mcadd(register struct parse *p, register cset *cs, \
1238 == register char *cp);
1240 static void
1241 mcadd(register struct parse *p, register cset *cs, register const char *cp)
1243 register size_t oldend = cs->smultis;
1245 cs->smultis += strlen(cp) + 1;
1246 if (cs->multis == NULL)
1247 cs->multis = malloc(cs->smultis);
1248 else
1249 cs->multis = realloc(cs->multis, cs->smultis);
1250 if (cs->multis == NULL) {
1251 SETERROR(REG_ESPACE);
1252 return;
1255 (void) strcpy(cs->multis + oldend - 1, cp);
1256 cs->multis[cs->smultis - 1] = '\0';
1259 #ifdef notdef
1261 - mcsub - subtract a collating element from a cset
1262 == static void mcsub(register cset *cs, register char *cp);
1264 static void
1265 mcsub(register cset *cs, register char *cp)
1267 register char *fp = mcfind(cs, cp);
1268 register size_t len = strlen(fp);
1270 assert(fp != NULL);
1271 (void) memmove(fp, fp + len + 1,
1272 cs->smultis - (fp + len + 1 - cs->multis));
1273 cs->smultis -= len;
1275 if (cs->smultis == 0) {
1276 free(cs->multis);
1277 cs->multis = NULL;
1278 return;
1281 cs->multis = realloc(cs->multis, cs->smultis);
1282 assert(cs->multis != NULL);
1286 - mcin - is a collating element in a cset?
1287 == static int mcin(register cset *cs, register char *cp);
1289 static int
1290 mcin(register cset *cs, register char *cp)
1292 return(mcfind(cs, cp) != NULL);
1296 - mcfind - find a collating element in a cset
1297 == static char *mcfind(register cset *cs, register char *cp);
1299 static char *
1300 mcfind(register cset *cs, register char *cp)
1302 register char *p;
1304 if (cs->multis == NULL)
1305 return(NULL);
1306 for (p = cs->multis; *p != '\0'; p += strlen(p) + 1)
1307 if (strcmp(cp, p) == 0)
1308 return(p);
1309 return(NULL);
1311 #endif
1314 - mcinvert - invert the list of collating elements in a cset
1315 == static void mcinvert(register struct parse *p, register cset *cs);
1317 * This would have to know the set of possibilities. Implementation
1318 * is deferred.
1320 static void
1321 mcinvert(register struct parse *p, register cset *cs)
1323 assert(cs->multis == NULL); /* xxx */
1327 - mccase - add case counterparts of the list of collating elements in a cset
1328 == static void mccase(register struct parse *p, register cset *cs);
1330 * This would have to know the set of possibilities. Implementation
1331 * is deferred.
1333 static void
1334 mccase(register struct parse *p, register cset *cs)
1336 assert(cs->multis == NULL); /* xxx */
1339 #ifdef notdef
1341 - isinsets - is this character in any sets?
1342 == static int isinsets(register struct re_guts *g, int c);
1344 static int /* predicate */
1345 isinsets(register struct re_guts *g, int c)
1347 register uch *col;
1348 register int i;
1349 register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1350 register unsigned uc = (unsigned char)c;
1352 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1353 if (col[uc] != 0)
1354 return(1);
1355 return(0);
1359 - samesets - are these two characters in exactly the same sets?
1360 == static int samesets(register struct re_guts *g, int c1, int c2);
1362 static int /* predicate */
1363 samesets(register struct re_guts *g, int c1, int c2)
1365 register uch *col;
1366 register int i;
1367 register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1368 register unsigned uc1 = (unsigned char)c1;
1369 register unsigned uc2 = (unsigned char)c2;
1371 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1372 if (col[uc1] != col[uc2])
1373 return(0);
1374 return(1);
1376 #endif
1379 - categorize - sort out character categories
1380 == static void categorize(struct parse *p, register struct re_guts *g);
1382 static void
1383 categorize(struct parse *p, register struct re_guts *g)
1385 #ifdef notdef
1386 register cat_t *cats = g->categories;
1387 register int c;
1388 register int c2;
1389 register cat_t cat;
1391 /* avoid making error situations worse */
1392 if (p->error != 0)
1393 return;
1395 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1396 if (cats[c] == 0 && isinsets(g, c)) {
1397 cat = g->ncategories++;
1398 cats[c] = cat;
1399 for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1400 if (cats[c2] == 0 && samesets(g, c, c2))
1401 cats[c2] = cat;
1403 #endif
1407 - dupl - emit a duplicate of a bunch of sops
1408 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1410 static sopno /* start of duplicate */
1411 dupl(register struct parse *p, sopno start, sopno finish)
1413 /* from here */
1414 /* to this less one */
1416 register sopno ret = HERE();
1417 register sopno len = finish - start;
1419 assert(finish >= start);
1420 if (len == 0)
1421 return(ret);
1422 enlarge(p, p->ssize + len); /* this many unexpected additions */
1423 assert(p->ssize >= p->slen + len);
1424 (void) memcpy((char *)(p->strip + p->slen),
1425 (char *)(p->strip + start), (size_t)len*sizeof(sop));
1426 (void) memcpy((char *)(p->stripdata + p->slen),
1427 (char *)(p->stripdata + start), (size_t)len*sizeof(RCHAR_T));
1428 p->slen += len;
1429 return(ret);
1433 - doemit - emit a strip operator
1434 == static void doemit(register struct parse *p, sop op, size_t opnd);
1436 * It might seem better to implement this as a macro with a function as
1437 * hard-case backup, but it's just too big and messy unless there are
1438 * some changes to the data structures. Maybe later.
1440 static void
1441 doemit(register struct parse *p, sop op, size_t opnd)
1443 /* avoid making error situations worse */
1444 if (p->error != 0)
1445 return;
1447 /* deal with oversize operands ("can't happen", more or less) */
1448 assert(opnd < 1);
1450 /* deal with undersized strip */
1451 if (p->slen >= p->ssize)
1452 enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1453 assert(p->slen < p->ssize);
1455 /* finally, it's all reduced to the easy case */
1456 p->strip[p->slen] = op;
1457 p->stripdata[p->slen] = opnd;
1458 p->slen++;
1462 - doinsert - insert a sop into the strip
1463 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1465 static void
1466 doinsert(register struct parse *p, sop op, size_t opnd, sopno pos)
1468 register sopno sn;
1469 register sop s;
1470 register RCHAR_T d;
1471 register int i;
1473 /* avoid making error situations worse */
1474 if (p->error != 0)
1475 return;
1477 sn = HERE();
1478 EMIT(op, opnd); /* do checks, ensure space */
1479 assert(HERE() == sn+1);
1480 s = p->strip[sn];
1481 d = p->stripdata[sn];
1483 /* adjust paren pointers */
1484 assert(pos > 0);
1485 for (i = 1; i < NPAREN; i++) {
1486 if (p->pbegin[i] >= pos) {
1487 p->pbegin[i]++;
1489 if (p->pend[i] >= pos) {
1490 p->pend[i]++;
1494 memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1495 (HERE()-pos-1)*sizeof(sop));
1496 memmove((char *)&p->stripdata[pos+1], (char *)&p->stripdata[pos],
1497 (HERE()-pos-1)*sizeof(RCHAR_T));
1498 p->strip[pos] = s;
1499 p->stripdata[pos] = d;
1503 - dofwd - complete a forward reference
1504 == static void dofwd(register struct parse *p, sopno pos, sop value);
1506 static void
1507 dofwd(register struct parse *p, register sopno pos, sop value)
1509 /* avoid making error situations worse */
1510 if (p->error != 0)
1511 return;
1513 assert(value < 1);
1514 p->stripdata[pos] = value;
1518 - enlarge - enlarge the strip
1519 == static void enlarge(register struct parse *p, sopno size);
1521 static void
1522 enlarge(register struct parse *p, register sopno size)
1524 register sop *sp;
1525 register RCHAR_T *dp;
1527 if (p->ssize >= size)
1528 return;
1530 sp = (sop *)realloc(p->strip, size*sizeof(sop));
1531 if (sp == NULL) {
1532 SETERROR(REG_ESPACE);
1533 return;
1535 p->strip = sp;
1536 dp = (RCHAR_T *)realloc(p->stripdata, size*sizeof(RCHAR_T));
1537 if (dp == NULL) {
1538 SETERROR(REG_ESPACE);
1539 return;
1541 p->stripdata = dp;
1542 p->ssize = size;
1546 - stripsnug - compact the strip
1547 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1549 static void
1550 stripsnug(register struct parse *p, register struct re_guts *g)
1552 g->nstates = p->slen;
1553 g->strip = (sop *)realloc((char *)p->strip,
1554 p->slen * sizeof(sop));
1555 if (g->strip == NULL) {
1556 SETERROR(REG_ESPACE);
1557 g->strip = p->strip;
1559 g->stripdata = (RCHAR_T *)realloc((char *)p->stripdata,
1560 p->slen * sizeof(RCHAR_T));
1561 if (g->stripdata == NULL) {
1562 SETERROR(REG_ESPACE);
1563 g->stripdata = p->stripdata;
1568 - findmust - fill in must and mlen with longest mandatory literal string
1569 == static void findmust(register struct parse *p, register struct re_guts *g);
1571 * This algorithm could do fancy things like analyzing the operands of |
1572 * for common subsequences. Someday. This code is simple and finds most
1573 * of the interesting cases.
1575 * Note that must and mlen got initialized during setup.
1577 static void
1578 findmust(struct parse *p, register struct re_guts *g)
1580 register sop *scans;
1581 register RCHAR_T *scand;
1582 sop *starts = 0;
1583 RCHAR_T *startd = NULL;
1584 register sop *newstarts = 0;
1585 register RCHAR_T *newstartd = NULL;
1586 register sopno newlen;
1587 register sop s;
1588 register RCHAR_T d;
1589 register RCHAR_T *cp;
1590 register sopno i;
1592 /* avoid making error situations worse */
1593 if (p->error != 0)
1594 return;
1596 /* find the longest OCHAR sequence in strip */
1597 newlen = 0;
1598 scans = g->strip + 1;
1599 scand = g->stripdata + 1;
1600 do {
1601 s = *scans++;
1602 d = *scand++;
1603 switch (s) {
1604 case OCHAR: /* sequence member */
1605 if (newlen == 0) { /* new sequence */
1606 newstarts = scans - 1;
1607 newstartd = scand - 1;
1609 newlen++;
1610 break;
1611 case OPLUS_: /* things that don't break one */
1612 case OLPAREN:
1613 case ORPAREN:
1614 break;
1615 case OQUEST_: /* things that must be skipped */
1616 case OCH_:
1617 scans--;
1618 scand--;
1619 do {
1620 scans += d;
1621 scand += d;
1622 s = *scans;
1623 d = *scand;
1624 /* assert() interferes w debug printouts */
1625 if (s != O_QUEST && s != O_CH && s != OOR2) {
1626 g->iflags |= BAD;
1627 return;
1629 } while (s != O_QUEST && s != O_CH);
1630 /* fallthrough */
1631 default: /* things that break a sequence */
1632 if (newlen > g->mlen) { /* ends one */
1633 starts = newstarts;
1634 startd = newstartd;
1635 g->mlen = newlen;
1637 newlen = 0;
1638 break;
1640 } while (s != OEND);
1642 if (g->mlen == 0) /* there isn't one */
1643 return;
1645 /* turn it into a character string */
1646 g->must = malloc(((size_t)g->mlen + 1) * sizeof(RCHAR_T));
1647 if (g->must == NULL) { /* argh; just forget it */
1648 g->mlen = 0;
1649 return;
1651 cp = g->must;
1652 scans = starts;
1653 scand = startd;
1654 for (i = g->mlen; i > 0; i--) {
1655 for (;;) {
1656 s = *scans++;
1657 d = *scand++;
1658 if (s == OCHAR)
1659 break;
1661 assert(cp < g->must + g->mlen);
1662 *cp++ = d;
1664 assert(cp == g->must + g->mlen);
1665 *cp++ = '\0'; /* just on general principles */
1669 - pluscount - count + nesting
1670 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
1672 static sopno /* nesting depth */
1673 pluscount(struct parse *p, register struct re_guts *g)
1675 register sop *scan;
1676 register sop s;
1677 register sopno plusnest = 0;
1678 register sopno maxnest = 0;
1680 if (p->error != 0)
1681 return(0); /* there may not be an OEND */
1683 scan = g->strip + 1;
1684 do {
1685 s = *scan++;
1686 switch (s) {
1687 case OPLUS_:
1688 plusnest++;
1689 break;
1690 case O_PLUS:
1691 if (plusnest > maxnest)
1692 maxnest = plusnest;
1693 plusnest--;
1694 break;
1696 } while (s != OEND);
1697 if (plusnest != 0)
1698 g->iflags |= BAD;
1699 return(maxnest);