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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
37 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
39 * $FreeBSD: src/lib/libc/regex/regcomp.c,v 1.13.2.1 2000/07/31 06:30:37 dcs Exp $
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
44 #endif /* LIBC_SCCS and not lint */
46 #include <sys/types.h>
52 #include "regex/regex.h"
54 // removed collate stuff --SDM
55 // #include "collate.h"
58 #define isblank(c) ((c) == ' ' || (c) == '\t')
68 * parse structure, passed up and down to avoid global variables and
72 char *next; /* next character in RE */
73 char *end; /* end of string (-> NUL normally) */
74 int error; /* has an error been seen? */
75 sop *strip; /* malloced strip */
76 sopno ssize; /* malloced strip size (allocated) */
77 sopno slen; /* malloced strip length (used) */
78 int ncsalloc; /* number of csets allocated */
80 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
81 sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
82 sopno pend[NPAREN]; /* -> ) ([0] unused) */
85 /* ========= begin header generated by ./mkh ========= */
90 /* === regcomp.c === */
91 static void p_ere (struct parse *p, int stop);
92 static void p_ere_exp (struct parse *p);
93 static void p_str (struct parse *p);
94 static void p_bre (struct parse *p, int end1, int end2);
95 static int p_simp_re (struct parse *p, int starordinary);
96 static int p_count (struct parse *p);
97 static void p_bracket (struct parse *p);
98 static void p_b_term (struct parse *p, cset *cs);
99 static void p_b_cclass (struct parse *p, cset *cs);
100 static void p_b_eclass (struct parse *p, cset *cs);
101 static char p_b_symbol (struct parse *p);
102 static char p_b_coll_elem (struct parse *p, int endc);
103 static char othercase (int ch);
104 static void bothcases (struct parse *p, int ch);
105 static void ordinary (struct parse *p, int ch);
106 static void nonnewline (struct parse *p);
107 static void repeat (struct parse *p, sopno start, int from, int to);
108 static int seterr (struct parse *p, int e);
109 static cset *allocset (struct parse *p);
110 static void freeset (struct parse *p, cset *cs);
111 static int freezeset (struct parse *p, cset *cs);
112 static int firstch (struct parse *p, cset *cs);
113 static int nch (struct parse *p, cset *cs);
114 static void mcadd (struct parse *p, cset *cs, char *cp);
116 static void mcsub (cset *cs, char *cp);
117 static int mcin (cset *cs, char *cp);
118 static char *mcfind (cset *cs, char *cp);
120 static void mcinvert (struct parse *p, cset *cs);
121 static void mccase (struct parse *p, cset *cs);
122 static int isinsets (struct re_guts *g, int c);
123 static int samesets (struct re_guts *g, int c1, int c2);
124 static void categorize (struct parse *p, struct re_guts *g);
125 static sopno dupl (struct parse *p, sopno start, sopno finish);
126 static void doemit (struct parse *p, sop op, size_t opnd);
127 static void doinsert (struct parse *p, sop op, size_t opnd, sopno pos);
128 static void dofwd (struct parse *p, sopno pos, sop value);
129 static void enlarge (struct parse *p, sopno size);
130 static void stripsnug (struct parse *p, struct re_guts *g);
131 static void findmust (struct parse *p, struct re_guts *g);
132 static int altoffset (sop *scan, int offset, int mccs);
133 static void computejumps (struct parse *p, struct re_guts *g);
134 static void computematchjumps (struct parse *p, struct re_guts *g);
135 static sopno pluscount (struct parse *p, struct re_guts *g);
140 /* ========= end header generated by ./mkh ========= */
142 static char nuls[10]; /* place to point scanner in event of error */
145 * macros for use with parse structure
146 * BEWARE: these know that the parse structure is named `p' !!!
148 #define PEEK() (*p->next)
149 #define PEEK2() (*(p->next+1))
150 #define MORE() (p->next < p->end)
151 #define MORE2() (p->next+1 < p->end)
152 #define SEE(c) (MORE() && PEEK() == (c))
153 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
154 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
155 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
156 #define NEXT() (p->next++)
157 #define NEXT2() (p->next += 2)
158 #define NEXTn(n) (p->next += (n))
159 #define GETNEXT() (*p->next++)
160 #define SETERROR(e) seterr(p, (e))
161 #define REQUIRE(co, e) ((co) || SETERROR(e))
162 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
163 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
164 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
165 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
166 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
167 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
168 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
169 #define HERE() (p->slen)
170 #define THERE() (p->slen - 1)
171 #define THERETHERE() (p->slen - 2)
172 #define DROP(n) (p->slen -= (n))
175 static int never = 0; /* for use in asserts; shuts lint up */
177 #define never 0 /* some <assert.h>s have bugs too */
180 /* Macro used by computejump()/computematchjump() */
181 #define MIN(a,b) ((a)<(b)?(a):(b))
184 - regcomp - interface for parser and compilation
185 = extern int regcomp(regex_t *, const char *, int);
186 = #define REG_BASIC 0000
187 = #define REG_EXTENDED 0001
188 = #define REG_ICASE 0002
189 = #define REG_NOSUB 0004
190 = #define REG_NEWLINE 0010
191 = #define REG_NOSPEC 0020
192 = #define REG_PEND 0040
193 = #define REG_DUMP 0200
195 int /* 0 success, otherwise REG_something */
196 regcomp(preg, pattern, cflags)
202 register struct re_guts *g;
203 register struct parse *p = &pa;
207 # define GOODFLAGS(f) (f)
209 # define GOODFLAGS(f) ((f)&~REG_DUMP)
212 cflags = GOODFLAGS(cflags);
213 if ((cflags®_EXTENDED) && (cflags®_NOSPEC))
216 if (cflags®_PEND) {
217 if (preg->re_endp < pattern)
219 len = preg->re_endp - pattern;
221 len = strlen((char *)pattern);
223 /* do the mallocs early so failure handling is easy */
224 g = (struct re_guts *)malloc(sizeof(struct re_guts) +
225 (NC-1)*sizeof(cat_t));
228 p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
229 p->strip = (sop *)malloc(p->ssize * sizeof(sop));
231 if (p->strip == NULL) {
238 p->next = (char *)pattern; /* convenience; we do not modify it */
239 p->end = p->next + len;
242 for (i = 0; i < NPAREN; i++) {
260 g->ncategories = 1; /* category 0 is "everything else" */
261 g->categories = &g->catspace[-(CHAR_MIN)];
262 (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
267 g->firststate = THERE();
268 if (cflags®_EXTENDED)
270 else if (cflags®_NOSPEC)
275 g->laststate = THERE();
277 /* tidy up loose ends and fill things in */
281 /* only use Boyer-Moore algorithm if the pattern is bigger
282 * than three characters
286 computematchjumps(p, g);
287 if(g->matchjump == NULL && g->charjump != NULL) {
292 g->nplus = pluscount(p, g);
294 preg->re_nsub = g->nsub;
296 preg->re_magic = MAGIC1;
298 /* not debugging, so can't rely on the assert() in regexec() */
300 SETERROR(REG_ASSERT);
303 /* win or lose, we're done */
304 if (p->error != 0) /* lose */
310 - p_ere - ERE parser top level, concatenation and alternation
311 == static void p_ere(register struct parse *p, int stop);
315 register struct parse *p;
316 int stop; /* character this ERE should end at */
319 register sopno prevback;
320 register sopno prevfwd;
322 register int first = 1; /* is this the first alternative? */
325 /* do a bunch of concatenated expressions */
327 while (MORE() && (c = PEEK()) != '|' && c != stop)
329 (void)REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
332 break; /* NOTE BREAK OUT */
335 INSERT(OCH_, conc); /* offset is wrong */
340 ASTERN(OOR1, prevback);
342 AHEAD(prevfwd); /* fix previous offset */
344 EMIT(OOR2, 0); /* offset is very wrong */
347 if (!first) { /* tail-end fixups */
349 ASTERN(O_CH, prevback);
352 assert(!MORE() || SEE(stop));
356 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
357 == static void p_ere_exp(register struct parse *p);
361 register struct parse *p;
367 register sopno subno;
370 assert(MORE()); /* caller should have ensured this */
376 (void)REQUIRE(MORE(), REG_EPAREN);
380 p->pbegin[subno] = HERE();
381 EMIT(OLPAREN, subno);
384 if (subno < NPAREN) {
385 p->pend[subno] = HERE();
386 assert(p->pend[subno] != 0);
388 EMIT(ORPAREN, subno);
389 (void)MUSTEAT(')', REG_EPAREN);
391 #ifndef POSIX_MISTAKE
392 case ')': /* happens only if no current unmatched ( */
394 * You may ask, why the ifndef? Because I didn't notice
395 * this until slightly too late for 1003.2, and none of the
396 * other 1003.2 regular-expression reviewers noticed it at
397 * all. So an unmatched ) is legal POSIX, at least until
398 * we can get it fixed.
400 SETERROR(REG_EPAREN);
405 p->g->iflags |= USEBOL;
411 p->g->iflags |= USEEOL;
420 SETERROR(REG_BADRPT);
423 if (p->g->cflags®_NEWLINE)
432 (void)REQUIRE(MORE(), REG_EESCAPE);
436 case '{': /* okay as ordinary except if digit follows */
437 (void)REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
447 /* we call { a repetition if followed by a digit */
448 if (!( c == '*' || c == '+' || c == '?' ||
449 (c == '{' && MORE2() && isdigit((uch)PEEK2())) ))
450 return; /* no repetition, we're done */
453 (void)REQUIRE(!wascaret, REG_BADRPT);
455 case '*': /* implemented as +? */
456 /* this case does not require the (y|) trick, noKLUDGE */
459 INSERT(OQUEST_, pos);
460 ASTERN(O_QUEST, pos);
467 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
468 INSERT(OCH_, pos); /* offset slightly wrong */
469 ASTERN(OOR1, pos); /* this one's right */
470 AHEAD(pos); /* fix the OCH_ */
471 EMIT(OOR2, 0); /* offset very wrong... */
472 AHEAD(THERE()); /* ...so fix it */
473 ASTERN(O_CH, THERETHERE());
478 if (isdigit((uch)PEEK())) {
480 (void)REQUIRE(count <= count2, REG_BADBR);
481 } else /* single number with comma */
483 } else /* just a single number */
485 repeat(p, pos, count, count2);
486 if (!EAT('}')) { /* error heuristics */
487 while (MORE() && PEEK() != '}')
489 (void)REQUIRE(MORE(), REG_EBRACE);
498 if (!( c == '*' || c == '+' || c == '?' ||
499 (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
501 SETERROR(REG_BADRPT);
505 - p_str - string (no metacharacters) "parser"
506 == static void p_str(register struct parse *p);
510 register struct parse *p;
512 (void)REQUIRE(MORE(), REG_EMPTY);
514 ordinary(p, GETNEXT());
518 - p_bre - BRE parser top level, anchoring and concatenation
519 == static void p_bre(register struct parse *p, register int end1, \
520 == register int end2);
521 * Giving end1 as OUT essentially eliminates the end1/end2 check.
523 * This implementation is a bit of a kludge, in that a trailing $ is first
524 * taken as an ordinary character and then revised to be an anchor. The
525 * only undesirable side effect is that '$' gets included as a character
526 * category in such cases. This is fairly harmless; not worth fixing.
527 * The amount of lookahead needed to avoid this kludge is excessive.
531 register struct parse *p;
532 register int end1; /* first terminating character */
533 register int end2; /* second terminating character */
535 register sopno start = HERE();
536 register int first = 1; /* first subexpression? */
537 register int wasdollar = 0;
541 p->g->iflags |= USEBOL;
544 while (MORE() && !SEETWO(end1, end2)) {
545 wasdollar = p_simp_re(p, first);
548 if (wasdollar) { /* oops, that was a trailing anchor */
551 p->g->iflags |= USEEOL;
555 (void)REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
559 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
560 == static int p_simp_re(register struct parse *p, int starordinary);
562 static int /* was the simple RE an unbackslashed $? */
563 p_simp_re(p, starordinary)
564 register struct parse *p;
565 int starordinary; /* is a leading * an ordinary character? */
572 register sopno subno;
573 # define BACKSL (1<<CHAR_BIT)
575 pos = HERE(); /* repetion op, if any, covers from here */
577 assert(MORE()); /* caller should have ensured this */
580 (void)REQUIRE(MORE(), REG_EESCAPE);
581 c = BACKSL | GETNEXT();
585 if (p->g->cflags®_NEWLINE)
594 SETERROR(REG_BADRPT);
600 p->pbegin[subno] = HERE();
601 EMIT(OLPAREN, subno);
602 /* the MORE here is an error heuristic */
603 if (MORE() && !SEETWO('\\', ')'))
605 if (subno < NPAREN) {
606 p->pend[subno] = HERE();
607 assert(p->pend[subno] != 0);
609 EMIT(ORPAREN, subno);
610 (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
612 case BACKSL|')': /* should not get here -- must be user */
614 SETERROR(REG_EPAREN);
625 i = (c&~BACKSL) - '0';
627 if (p->pend[i] != 0) {
628 assert(i <= p->g->nsub);
630 assert(p->pbegin[i] != 0);
631 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
632 assert(OP(p->strip[p->pend[i]]) == ORPAREN);
633 (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
636 SETERROR(REG_ESUBREG);
640 (void)REQUIRE(starordinary, REG_BADRPT);
643 ordinary(p, (char)c);
647 if (EAT('*')) { /* implemented as +? */
648 /* this case does not require the (y|) trick, noKLUDGE */
651 INSERT(OQUEST_, pos);
652 ASTERN(O_QUEST, pos);
653 } else if (EATTWO('\\', '{')) {
656 if (MORE() && isdigit((uch)PEEK())) {
658 (void)REQUIRE(count <= count2, REG_BADBR);
659 } else /* single number with comma */
661 } else /* just a single number */
663 repeat(p, pos, count, count2);
664 if (!EATTWO('\\', '}')) { /* error heuristics */
665 while (MORE() && !SEETWO('\\', '}'))
667 (void)REQUIRE(MORE(), REG_EBRACE);
670 } else if (c == '$') /* $ (but not \$) ends it */
677 - p_count - parse a repetition count
678 == static int p_count(register struct parse *p);
680 static int /* the value */
682 register struct parse *p;
684 register int count = 0;
685 register int ndigits = 0;
687 while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
688 count = count*10 + (GETNEXT() - '0');
692 (void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
697 - p_bracket - parse a bracketed character list
698 == static void p_bracket(register struct parse *p);
700 * Note a significant property of this code: if the allocset() did SETERROR,
701 * no set operations are done.
705 register struct parse *p;
707 register cset *cs = allocset(p);
708 register int invert = 0;
710 /* Dept of Truly Sickening Special-Case Kludges */
711 if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
716 if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
723 invert++; /* make note to invert set at end */
728 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
732 (void)MUSTEAT(']', REG_EBRACK);
734 if (p->error != 0) /* don't mess things up further */
737 if (p->g->cflags®_ICASE) {
741 for (i = p->g->csetsize - 1; i >= 0; i--)
742 if (CHIN(cs, i) && isalpha(i)) {
747 if (cs->multis != NULL)
753 for (i = p->g->csetsize - 1; i >= 0; i--)
758 if (p->g->cflags®_NEWLINE)
760 if (cs->multis != NULL)
764 assert(cs->multis == NULL); /* xxx */
766 if (nch(p, cs) == 1) { /* optimize singleton sets */
767 ordinary(p, firstch(p, cs));
770 EMIT(OANYOF, freezeset(p, cs));
774 - p_b_term - parse one term of a bracketed character list
775 == static void p_b_term(register struct parse *p, register cset *cs);
779 register struct parse *p;
783 register char start, finish;
786 /* classify what we've got */
787 switch ((MORE()) ? PEEK() : '\0') {
789 c = (MORE2()) ? PEEK2() : '\0';
792 SETERROR(REG_ERANGE);
793 return; /* NOTE RETURN */
801 case ':': /* character class */
803 (void)REQUIRE(MORE(), REG_EBRACK);
805 (void)REQUIRE(c != '-' && c != ']', REG_ECTYPE);
807 (void)REQUIRE(MORE(), REG_EBRACK);
808 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
810 case '=': /* equivalence class */
812 (void)REQUIRE(MORE(), REG_EBRACK);
814 (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
816 (void)REQUIRE(MORE(), REG_EBRACK);
817 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
819 default: /* symbol, ordinary character, or range */
820 /* xxx revision needed for multichar stuff */
821 start = p_b_symbol(p);
822 if (SEE('-') && MORE2() && PEEK2() != ']') {
828 finish = p_b_symbol(p);
834 // remove collate stuff --SDM
836 if (__collate_load_error) {
837 (void)REQUIRE((uch)start <= (uch)finish, REG_ERANGE);
839 for (i = (uch)start; i <= (uch)finish; i++)
843 (void)REQUIRE(__collate_range_cmp(start, finish) <= 0, REG_ERANGE);
844 for (i = CHAR_MIN; i <= CHAR_MAX; i++) {
845 if ( __collate_range_cmp(start, i) <= 0
846 && __collate_range_cmp(i, finish) <= 0
858 - p_b_cclass - parse a character-class name and deal with it
859 == static void p_b_cclass(register struct parse *p, register cset *cs);
863 register struct parse *p;
867 register char *sp = p->next;
868 register struct cclass *cp;
871 while (MORE() && isalpha((uch)PEEK()))
874 for (cp = cclasses; cp->name != NULL; cp++)
875 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
877 if (cp->name == NULL) {
878 /* oops, didn't find it */
879 SETERROR(REG_ECTYPE);
885 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
890 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
895 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
900 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
905 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
910 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
915 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
920 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
925 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
930 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
935 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
940 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
941 if (isxdigit((uch)c))
946 for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
952 - p_b_eclass - parse an equivalence-class name and deal with it
953 == static void p_b_eclass(register struct parse *p, register cset *cs);
955 * This implementation is incomplete. xxx
959 register struct parse *p;
964 c = p_b_coll_elem(p, '=');
969 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
970 == static char p_b_symbol(register struct parse *p);
972 static char /* value of symbol */
974 register struct parse *p;
978 (void)REQUIRE(MORE(), REG_EBRACK);
979 if (!EATTWO('[', '.'))
982 /* collating symbol */
983 value = p_b_coll_elem(p, '.');
984 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
989 - p_b_coll_elem - parse a collating-element name and look it up
990 == static char p_b_coll_elem(register struct parse *p, int endc);
992 static char /* value of collating element */
993 p_b_coll_elem(p, endc)
994 register struct parse *p;
995 int endc; /* name ended by endc,']' */
997 register char *sp = p->next;
998 register struct cname *cp;
1001 while (MORE() && !SEETWO(endc, ']'))
1004 SETERROR(REG_EBRACK);
1008 for (cp = cnames; cp->name != NULL; cp++)
1009 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
1010 return(cp->code); /* known name */
1012 return(*sp); /* single character */
1013 SETERROR(REG_ECOLLATE); /* neither */
1018 - othercase - return the case counterpart of an alphabetic
1019 == static char othercase(int ch);
1021 static char /* if no counterpart, return ch */
1026 assert(isalpha(ch));
1028 return(tolower(ch));
1029 else if (islower(ch))
1030 return(toupper(ch));
1031 else /* peculiar, but could happen */
1036 - bothcases - emit a dualcase version of a two-case character
1037 == static void bothcases(register struct parse *p, int ch);
1039 * Boy, is this implementation ever a kludge...
1043 register struct parse *p;
1046 register char *oldnext = p->next;
1047 register char *oldend = p->end;
1051 assert(othercase(ch) != ch); /* p_bracket() would recurse */
1058 assert(p->next == bracket+2);
1064 - ordinary - emit an ordinary character
1065 == static void ordinary(register struct parse *p, register int ch);
1069 register struct parse *p;
1072 register cat_t *cap = p->g->categories;
1074 if ((p->g->cflags®_ICASE) && isalpha((uch)ch) && othercase(ch) != ch)
1077 EMIT(OCHAR, (uch)ch);
1079 cap[ch] = p->g->ncategories++;
1084 - nonnewline - emit REG_NEWLINE version of OANY
1085 == static void nonnewline(register struct parse *p);
1087 * Boy, is this implementation ever a kludge...
1091 register struct parse *p;
1093 register char *oldnext = p->next;
1094 register char *oldend = p->end;
1104 assert(p->next == bracket+3);
1110 - repeat - generate code for a bounded repetition, recursively if needed
1111 == static void repeat(register struct parse *p, sopno start, int from, int to);
1114 repeat(p, start, from, to)
1115 register struct parse *p;
1116 sopno start; /* operand from here to end of strip */
1117 int from; /* repeated from this number */
1118 int to; /* to this number of times (maybe INFINITY) */
1120 register sopno finish = HERE();
1123 # define REP(f, t) ((f)*8 + (t))
1124 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1125 register sopno copy;
1127 if (p->error != 0) /* head off possible runaway recursion */
1132 switch (REP(MAP(from), MAP(to))) {
1133 case REP(0, 0): /* must be user doing this */
1134 DROP(finish-start); /* drop the operand */
1136 case REP(0, 1): /* as x{1,1}? */
1137 case REP(0, N): /* as x{1,n}? */
1138 case REP(0, INF): /* as x{1,}? */
1139 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1140 INSERT(OCH_, start); /* offset is wrong... */
1141 repeat(p, start+1, 1, to);
1142 ASTERN(OOR1, start);
1143 AHEAD(start); /* ... fix it */
1146 ASTERN(O_CH, THERETHERE());
1148 case REP(1, 1): /* trivial case */
1151 case REP(1, N): /* as x?x{1,n-1} */
1152 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1153 INSERT(OCH_, start);
1154 ASTERN(OOR1, start);
1156 EMIT(OOR2, 0); /* offset very wrong... */
1157 AHEAD(THERE()); /* ...so fix it */
1158 ASTERN(O_CH, THERETHERE());
1159 copy = dupl(p, start+1, finish+1);
1160 assert(copy == finish+4);
1161 repeat(p, copy, 1, to-1);
1163 case REP(1, INF): /* as x+ */
1164 INSERT(OPLUS_, start);
1165 ASTERN(O_PLUS, start);
1167 case REP(N, N): /* as xx{m-1,n-1} */
1168 copy = dupl(p, start, finish);
1169 repeat(p, copy, from-1, to-1);
1171 case REP(N, INF): /* as xx{n-1,INF} */
1172 copy = dupl(p, start, finish);
1173 repeat(p, copy, from-1, to);
1175 default: /* "can't happen" */
1176 SETERROR(REG_ASSERT); /* just in case */
1182 - seterr - set an error condition
1183 == static int seterr(register struct parse *p, int e);
1185 static int /* useless but makes type checking happy */
1187 register struct parse *p;
1190 if (p->error == 0) /* keep earliest error condition */
1192 p->next = nuls; /* try to bring things to a halt */
1194 return(0); /* make the return value well-defined */
1198 - allocset - allocate a set of characters for []
1199 == static cset *allocset(register struct parse *p);
1203 register struct parse *p;
1205 register int no = p->g->ncsets++;
1207 register size_t nbytes;
1209 register size_t css = (size_t)p->g->csetsize;
1212 if (no >= p->ncsalloc) { /* need another column of space */
1213 p->ncsalloc += CHAR_BIT;
1215 assert(nc % CHAR_BIT == 0);
1216 nbytes = nc / CHAR_BIT * css;
1217 if (p->g->sets == NULL)
1218 p->g->sets = (cset *)malloc(nc * sizeof(cset));
1220 p->g->sets = (cset *)reallocf((char *)p->g->sets,
1222 if (p->g->setbits == NULL)
1223 p->g->setbits = (uch *)malloc(nbytes);
1225 p->g->setbits = (uch *)reallocf((char *)p->g->setbits,
1227 /* xxx this isn't right if setbits is now NULL */
1228 for (i = 0; i < no; i++)
1229 p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1231 if (p->g->sets != NULL && p->g->setbits != NULL)
1232 (void) memset((char *)p->g->setbits + (nbytes - css),
1236 SETERROR(REG_ESPACE);
1237 /* caller's responsibility not to do set ops */
1241 assert(p->g->sets != NULL); /* xxx */
1242 cs = &p->g->sets[no];
1243 cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1244 cs->mask = 1 << ((no) % CHAR_BIT);
1253 - freeset - free a now-unused set
1254 == static void freeset(register struct parse *p, register cset *cs);
1258 register struct parse *p;
1262 register cset *top = &p->g->sets[p->g->ncsets];
1263 register size_t css = (size_t)p->g->csetsize;
1265 for (i = 0; i < css; i++)
1267 if (cs == top-1) /* recover only the easy case */
1272 - freezeset - final processing on a set of characters
1273 == static int freezeset(register struct parse *p, register cset *cs);
1275 * The main task here is merging identical sets. This is usually a waste
1276 * of time (although the hash code minimizes the overhead), but can win
1277 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1278 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1281 static int /* set number */
1283 register struct parse *p;
1286 register short h = cs->hash;
1288 register cset *top = &p->g->sets[p->g->ncsets];
1290 register size_t css = (size_t)p->g->csetsize;
1292 /* look for an earlier one which is the same */
1293 for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1294 if (cs2->hash == h && cs2 != cs) {
1296 for (i = 0; i < css; i++)
1297 if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1303 if (cs2 < top) { /* found one */
1308 return((int)(cs - p->g->sets));
1312 - firstch - return first character in a set (which must have at least one)
1313 == static int firstch(register struct parse *p, register cset *cs);
1315 static int /* character; there is no "none" value */
1317 register struct parse *p;
1321 register size_t css = (size_t)p->g->csetsize;
1323 for (i = 0; i < css; i++)
1327 return(0); /* arbitrary */
1331 - nch - number of characters in a set
1332 == static int nch(register struct parse *p, register cset *cs);
1336 register struct parse *p;
1340 register size_t css = (size_t)p->g->csetsize;
1343 for (i = 0; i < css; i++)
1350 - mcadd - add a collating element to a cset
1351 == static void mcadd(register struct parse *p, register cset *cs, \
1352 == register char *cp);
1356 register struct parse *p;
1360 register size_t oldend = cs->smultis;
1362 cs->smultis += strlen(cp) + 1;
1363 if (cs->multis == NULL)
1364 cs->multis = malloc(cs->smultis);
1366 cs->multis = reallocf(cs->multis, cs->smultis);
1367 if (cs->multis == NULL) {
1368 SETERROR(REG_ESPACE);
1372 (void) strcpy(cs->multis + oldend - 1, cp);
1373 cs->multis[cs->smultis - 1] = '\0';
1378 - mcsub - subtract a collating element from a cset
1379 == static void mcsub(register cset *cs, register char *cp);
1386 register char *fp = mcfind(cs, cp);
1387 register size_t len = strlen(fp);
1390 (void) memmove(fp, fp + len + 1,
1391 cs->smultis - (fp + len + 1 - cs->multis));
1394 if (cs->smultis == 0) {
1400 cs->multis = reallocf(cs->multis, cs->smultis);
1401 assert(cs->multis != NULL);
1405 - mcin - is a collating element in a cset?
1406 == static int mcin(register cset *cs, register char *cp);
1413 return(mcfind(cs, cp) != NULL);
1417 - mcfind - find a collating element in a cset
1418 == static char *mcfind(register cset *cs, register char *cp);
1427 if (cs->multis == NULL)
1429 for (p = cs->multis; *p != '\0'; p += strlen(p) + 1)
1430 if (strcmp(cp, p) == 0)
1437 - mcinvert - invert the list of collating elements in a cset
1438 == static void mcinvert(register struct parse *p, register cset *cs);
1440 * This would have to know the set of possibilities. Implementation
1445 register struct parse *p;
1448 assert(cs->multis == NULL); /* xxx */
1452 - mccase - add case counterparts of the list of collating elements in a cset
1453 == static void mccase(register struct parse *p, register cset *cs);
1455 * This would have to know the set of possibilities. Implementation
1460 register struct parse *p;
1463 assert(cs->multis == NULL); /* xxx */
1467 - isinsets - is this character in any sets?
1468 == static int isinsets(register struct re_guts *g, int c);
1470 static int /* predicate */
1472 register struct re_guts *g;
1477 register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1478 register unsigned uc = (uch)c;
1480 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1487 - samesets - are these two characters in exactly the same sets?
1488 == static int samesets(register struct re_guts *g, int c1, int c2);
1490 static int /* predicate */
1492 register struct re_guts *g;
1498 register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1499 register unsigned uc1 = (uch)c1;
1500 register unsigned uc2 = (uch)c2;
1502 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1503 if (col[uc1] != col[uc2])
1509 - categorize - sort out character categories
1510 == static void categorize(struct parse *p, register struct re_guts *g);
1515 register struct re_guts *g;
1517 register cat_t *cats = g->categories;
1522 /* avoid making error situations worse */
1526 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1527 if (cats[c] == 0 && isinsets(g, c)) {
1528 cat = g->ncategories++;
1530 for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1531 if (cats[c2] == 0 && samesets(g, c, c2))
1537 - dupl - emit a duplicate of a bunch of sops
1538 == static sopno dupl(register struct parse *p, sopno start, sopno finish);
1540 static sopno /* start of duplicate */
1541 dupl(p, start, finish)
1542 register struct parse *p;
1543 sopno start; /* from here */
1544 sopno finish; /* to this less one */
1546 register sopno ret = HERE();
1547 register sopno len = finish - start;
1549 assert(finish >= start);
1552 enlarge(p, p->ssize + len); /* this many unexpected additions */
1553 assert(p->ssize >= p->slen + len);
1554 (void) memcpy((char *)(p->strip + p->slen),
1555 (char *)(p->strip + start), (size_t)len*sizeof(sop));
1561 - doemit - emit a strip operator
1562 == static void doemit(register struct parse *p, sop op, size_t opnd);
1564 * It might seem better to implement this as a macro with a function as
1565 * hard-case backup, but it's just too big and messy unless there are
1566 * some changes to the data structures. Maybe later.
1570 register struct parse *p;
1574 /* avoid making error situations worse */
1578 /* deal with oversize operands ("can't happen", more or less) */
1579 assert(opnd < 1<<OPSHIFT);
1581 /* deal with undersized strip */
1582 if (p->slen >= p->ssize)
1583 enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1584 assert(p->slen < p->ssize);
1586 /* finally, it's all reduced to the easy case */
1587 p->strip[p->slen++] = SOP(op, opnd);
1591 - doinsert - insert a sop into the strip
1592 == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
1595 doinsert(p, op, opnd, pos)
1596 register struct parse *p;
1605 /* avoid making error situations worse */
1610 EMIT(op, opnd); /* do checks, ensure space */
1611 assert(HERE() == sn+1);
1614 /* adjust paren pointers */
1616 for (i = 1; i < NPAREN; i++) {
1617 if (p->pbegin[i] >= pos) {
1620 if (p->pend[i] >= pos) {
1625 memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1626 (HERE()-pos-1)*sizeof(sop));
1631 - dofwd - complete a forward reference
1632 == static void dofwd(register struct parse *p, sopno pos, sop value);
1635 dofwd(p, pos, value)
1636 register struct parse *p;
1640 /* avoid making error situations worse */
1644 assert(value < 1<<OPSHIFT);
1645 p->strip[pos] = OP(p->strip[pos]) | value;
1649 - enlarge - enlarge the strip
1650 == static void enlarge(register struct parse *p, sopno size);
1654 register struct parse *p;
1655 register sopno size;
1659 if (p->ssize >= size)
1662 sp = (sop *)realloc(p->strip, size*sizeof(sop));
1664 SETERROR(REG_ESPACE);
1672 - stripsnug - compact the strip
1673 == static void stripsnug(register struct parse *p, register struct re_guts *g);
1677 register struct parse *p;
1678 register struct re_guts *g;
1680 g->nstates = p->slen;
1681 g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
1682 if (g->strip == NULL) {
1683 SETERROR(REG_ESPACE);
1684 g->strip = p->strip;
1689 - findmust - fill in must and mlen with longest mandatory literal string
1690 == static void findmust(register struct parse *p, register struct re_guts *g);
1692 * This algorithm could do fancy things like analyzing the operands of |
1693 * for common subsequences. Someday. This code is simple and finds most
1694 * of the interesting cases.
1696 * Note that must and mlen got initialized during setup.
1701 register struct re_guts *g;
1705 register sop *newstart;
1706 register sopno newlen;
1713 /* avoid making error situations worse */
1717 /* Find out if we can handle OANYOF or not */
1719 for (cs = 0; cs < g->ncsets; cs++)
1720 if (g->sets[cs].multis != NULL)
1723 /* find the longest OCHAR sequence in strip */
1727 scan = g->strip + 1;
1731 case OCHAR: /* sequence member */
1732 if (newlen == 0) /* new sequence */
1733 newstart = scan - 1;
1736 case OPLUS_: /* things that don't break one */
1740 case OQUEST_: /* things that must be skipped */
1742 offset = altoffset(scan, offset, mccs);
1747 /* assert() interferes w debug printouts */
1748 if (OP(s) != O_QUEST && OP(s) != O_CH &&
1753 } while (OP(s) != O_QUEST && OP(s) != O_CH);
1755 case OBOW: /* things that break a sequence */
1762 if (newlen > g->mlen) { /* ends one */
1766 g->moffset += offset;
1769 g->moffset = offset;
1777 if (newlen > g->mlen) { /* ends one */
1781 g->moffset += offset;
1784 g->moffset = offset;
1793 case OANYOF: /* may or may not invalidate offset */
1794 /* First, everything as OANY */
1795 if (newlen > g->mlen) { /* ends one */
1799 g->moffset += offset;
1802 g->moffset = offset;
1810 /* And, now, if we found out we can't deal with
1811 * it, make offset = -1.
1817 /* Anything here makes it impossible or too hard
1818 * to calculate the offset -- so we give up;
1819 * save the last known good offset, in case the
1820 * must sequence doesn't occur later.
1822 if (newlen > g->mlen) { /* ends one */
1826 g->moffset += offset;
1828 g->moffset = offset;
1834 } while (OP(s) != OEND);
1836 if (g->mlen == 0) { /* there isn't one */
1841 /* turn it into a character string */
1842 g->must = malloc((size_t)g->mlen + 1);
1843 if (g->must == NULL) { /* argh; just forget it */
1850 for (i = g->mlen; i > 0; i--) {
1851 while (OP(s = *scan++) != OCHAR)
1853 assert(cp < g->must + g->mlen);
1854 *cp++ = (char)OPND(s);
1856 assert(cp == g->must + g->mlen);
1857 *cp++ = '\0'; /* just on general principles */
1861 - altoffset - choose biggest offset among multiple choices
1862 == static int altoffset(sop *scan, int offset, int mccs);
1864 * Compute, recursively if necessary, the largest offset among multiple
1868 altoffset(scan, offset, mccs)
1877 /* If we gave up already on offsets, return */
1884 while (OP(s) != O_QUEST && OP(s) != O_CH) {
1893 try = altoffset(scan, try, mccs);
1900 if (OP(s) != O_QUEST && OP(s) != O_CH &&
1903 } while (OP(s) != O_QUEST && OP(s) != O_CH);
1904 /* We must skip to the next position, or we'll
1905 * leave altoffset() too early.
1933 return largest+offset;
1937 - computejumps - compute char jumps for BM scan
1938 == static void computejumps(register struct parse *p, register struct re_guts *g);
1940 * This algorithm assumes g->must exists and is has size greater than
1941 * zero. It's based on the algorithm found on Computer Algorithms by
1944 * A char jump is the number of characters one needs to jump based on
1945 * the value of the character from the text that was mismatched.
1955 /* Avoid making errors worse */
1959 g->charjump = (int*) malloc((NC + 1) * sizeof(int));
1960 if (g->charjump == NULL) /* Not a fatal error */
1962 /* Adjust for signed chars, if necessary */
1963 g->charjump = &g->charjump[-(CHAR_MIN)];
1965 /* If the character does not exist in the pattern, the jump
1966 * is equal to the number of characters in the pattern.
1968 for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++)
1969 g->charjump[ch] = g->mlen;
1971 /* If the character does exist, compute the jump that would
1972 * take us to the last character in the pattern equal to it
1973 * (notice that we match right to left, so that last character
1974 * is the first one that would be matched).
1976 for (mindex = 0; mindex < g->mlen; mindex++)
1977 g->charjump[g->must[mindex]] = g->mlen - mindex - 1;
1981 - computematchjumps - compute match jumps for BM scan
1982 == static void computematchjumps(register struct parse *p, register struct re_guts *g);
1984 * This algorithm assumes g->must exists and is has size greater than
1985 * zero. It's based on the algorithm found on Computer Algorithms by
1988 * A match jump is the number of characters one needs to advance based
1989 * on the already-matched suffix.
1990 * Notice that all values here are minus (g->mlen-1), because of the way
1991 * the search algorithm works.
1994 computematchjumps(p, g)
1998 int mindex; /* General "must" iterator */
1999 int suffix; /* Keeps track of matching suffix */
2000 int ssuffix; /* Keeps track of suffixes' suffix */
2001 int* pmatches; /* pmatches[k] points to the next i
2002 * such that i+1...mlen is a substring
2003 * of k+1...k+mlen-i-1
2006 /* Avoid making errors worse */
2010 pmatches = (int*) malloc(g->mlen * sizeof(unsigned int));
2011 if (pmatches == NULL) {
2012 g->matchjump = NULL;
2016 g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int));
2017 if (g->matchjump == NULL) /* Not a fatal error */
2020 /* Set maximum possible jump for each character in the pattern */
2021 for (mindex = 0; mindex < g->mlen; mindex++)
2022 g->matchjump[mindex] = 2*g->mlen - mindex - 1;
2024 /* Compute pmatches[] */
2025 for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0;
2026 mindex--, suffix--) {
2027 pmatches[mindex] = suffix;
2029 /* If a mismatch is found, interrupting the substring,
2030 * compute the matchjump for that position. If no
2031 * mismatch is found, then a text substring mismatched
2032 * against the suffix will also mismatch against the
2035 while (suffix < g->mlen
2036 && g->must[mindex] != g->must[suffix]) {
2037 g->matchjump[suffix] = MIN(g->matchjump[suffix],
2038 g->mlen - mindex - 1);
2039 suffix = pmatches[suffix];
2043 /* Compute the matchjump up to the last substring found to jump
2044 * to the beginning of the largest must pattern prefix matching
2047 for (mindex = 0; mindex <= suffix; mindex++)
2048 g->matchjump[mindex] = MIN(g->matchjump[mindex],
2049 g->mlen + suffix - mindex);
2051 ssuffix = pmatches[suffix];
2052 while (suffix < g->mlen) {
2053 while (suffix <= ssuffix && suffix < g->mlen) {
2054 g->matchjump[suffix] = MIN(g->matchjump[suffix],
2055 g->mlen + ssuffix - suffix);
2058 ssuffix = pmatches[ssuffix];
2065 - pluscount - count + nesting
2066 == static sopno pluscount(register struct parse *p, register struct re_guts *g);
2068 static sopno /* nesting depth */
2071 register struct re_guts *g;
2075 register sopno plusnest = 0;
2076 register sopno maxnest = 0;
2079 return(0); /* there may not be an OEND */
2081 scan = g->strip + 1;
2089 if (plusnest > maxnest)
2094 } while (OP(s) != OEND);