2 * This code is derived from OpenBSD's libc/regex, original license follows:
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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
38 #include <sys/types.h>
44 #include "regex_impl.h"
49 #include "regcclass.h"
53 * parse structure, passed up and down to avoid global variables and
57 char *next; /* next character in RE */
58 char *end; /* end of string (-> NUL normally) */
59 int error; /* has an error been seen? */
60 sop *strip; /* malloced strip */
61 sopno ssize; /* malloced strip size (allocated) */
62 sopno slen; /* malloced strip length (used) */
63 int ncsalloc; /* number of csets allocated */
65 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
66 sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
67 sopno pend[NPAREN]; /* -> ) ([0] unused) */
70 static void p_ere(struct parse *, int);
71 static void p_ere_exp(struct parse *);
72 static void p_str(struct parse *);
73 static void p_bre(struct parse *, int, int);
74 static int p_simp_re(struct parse *, int);
75 static int p_count(struct parse *);
76 static void p_bracket(struct parse *);
77 static void p_b_term(struct parse *, cset *);
78 static void p_b_cclass(struct parse *, cset *);
79 static void p_b_eclass(struct parse *, cset *);
80 static char p_b_symbol(struct parse *);
81 static char p_b_coll_elem(struct parse *, int);
82 static char othercase(int);
83 static void bothcases(struct parse *, int);
84 static void ordinary(struct parse *, int);
85 static void nonnewline(struct parse *);
86 static void repeat(struct parse *, sopno, int, int);
87 static int seterr(struct parse *, int);
88 static cset *allocset(struct parse *);
89 static void freeset(struct parse *, cset *);
90 static int freezeset(struct parse *, cset *);
91 static int firstch(struct parse *, cset *);
92 static int nch(struct parse *, cset *);
93 static void mcadd(struct parse *, cset *, const char *);
94 static void mcinvert(struct parse *, cset *);
95 static void mccase(struct parse *, cset *);
96 static int isinsets(struct re_guts *, int);
97 static int samesets(struct re_guts *, int, int);
98 static void categorize(struct parse *, struct re_guts *);
99 static sopno dupl(struct parse *, sopno, sopno);
100 static void doemit(struct parse *, sop, size_t);
101 static void doinsert(struct parse *, sop, size_t, sopno);
102 static void dofwd(struct parse *, sopno, sop);
103 static void enlarge(struct parse *, sopno);
104 static void stripsnug(struct parse *, struct re_guts *);
105 static void findmust(struct parse *, struct re_guts *);
106 static sopno pluscount(struct parse *, struct re_guts *);
108 static char nuls[10]; /* place to point scanner in event of error */
111 * macros for use with parse structure
112 * BEWARE: these know that the parse structure is named `p' !!!
114 #define PEEK() (*p->next)
115 #define PEEK2() (*(p->next+1))
116 #define MORE() (p->next < p->end)
117 #define MORE2() (p->next+1 < p->end)
118 #define SEE(c) (MORE() && PEEK() == (c))
119 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
120 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
121 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
122 #define NEXT() (p->next++)
123 #define NEXT2() (p->next += 2)
124 #define NEXTn(n) (p->next += (n))
125 #define GETNEXT() (*p->next++)
126 #define SETERROR(e) seterr(p, (e))
127 #define REQUIRE(co, e) (void)((co) || SETERROR(e))
128 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
129 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
130 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
131 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
132 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
133 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
134 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
135 #define HERE() (p->slen)
136 #define THERE() (p->slen - 1)
137 #define THERETHERE() (p->slen - 2)
138 #define DROP(n) (p->slen -= (n))
140 #ifdef _POSIX2_RE_DUP_MAX
141 #define DUPMAX _POSIX2_RE_DUP_MAX
145 #define INFINITY (DUPMAX + 1)
148 static int never = 0; /* for use in asserts; shuts lint up */
150 #define never 0 /* some <assert.h>s have bugs too */
154 - llvm_regcomp - interface for parser and compilation
156 int /* 0 success, otherwise REG_something */
157 llvm_regcomp(llvm_regex_t *preg, const char *pattern, int cflags)
161 struct parse *p = &pa;
165 # define GOODFLAGS(f) (f)
167 # define GOODFLAGS(f) ((f)&~REG_DUMP)
170 cflags = GOODFLAGS(cflags);
171 if ((cflags®_EXTENDED) && (cflags®_NOSPEC))
174 if (cflags®_PEND) {
175 if (preg->re_endp < pattern)
177 len = preg->re_endp - pattern;
179 len = strlen((const char *)pattern);
181 /* do the mallocs early so failure handling is easy */
182 g = (struct re_guts *)malloc(sizeof(struct re_guts) +
183 (NC-1)*sizeof(cat_t));
186 p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
187 p->strip = (sop *)calloc(p->ssize, sizeof(sop));
189 if (p->strip == NULL) {
196 p->next = (char *)pattern; /* convenience; we do not modify it */
197 p->end = p->next + len;
200 for (i = 0; i < NPAREN; i++) {
215 g->ncategories = 1; /* category 0 is "everything else" */
216 g->categories = &g->catspace[-(CHAR_MIN)];
217 (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
222 g->firststate = THERE();
223 if (cflags®_EXTENDED)
225 else if (cflags®_NOSPEC)
230 g->laststate = THERE();
232 /* tidy up loose ends and fill things in */
236 g->nplus = pluscount(p, g);
238 preg->re_nsub = g->nsub;
240 preg->re_magic = MAGIC1;
242 /* not debugging, so can't rely on the assert() in llvm_regexec() */
243 if (g->iflags®EX_BAD)
244 SETERROR(REG_ASSERT);
247 /* win or lose, we're done */
248 if (p->error != 0) /* lose */
254 - p_ere - ERE parser top level, concatenation and alternation
257 p_ere(struct parse *p, int stop) /* character this ERE should end at */
263 int first = 1; /* is this the first alternative? */
266 /* do a bunch of concatenated expressions */
268 while (MORE() && (c = PEEK()) != '|' && c != stop)
270 REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
273 break; /* NOTE BREAK OUT */
276 INSERT(OCH_, conc); /* offset is wrong */
281 ASTERN(OOR1, prevback);
283 AHEAD(prevfwd); /* fix previous offset */
285 EMIT(OOR2, 0); /* offset is very wrong */
288 if (!first) { /* tail-end fixups */
290 ASTERN(O_CH, prevback);
293 assert(!MORE() || SEE(stop));
297 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
300 p_ere_exp(struct parse *p)
309 assert(MORE()); /* caller should have ensured this */
315 REQUIRE(MORE(), REG_EPAREN);
319 p->pbegin[subno] = HERE();
320 EMIT(OLPAREN, subno);
323 if (subno < NPAREN) {
324 p->pend[subno] = HERE();
325 assert(p->pend[subno] != 0);
327 EMIT(ORPAREN, subno);
328 MUSTEAT(')', REG_EPAREN);
330 #ifndef POSIX_MISTAKE
331 case ')': /* happens only if no current unmatched ( */
333 * You may ask, why the ifndef? Because I didn't notice
334 * this until slightly too late for 1003.2, and none of the
335 * other 1003.2 regular-expression reviewers noticed it at
336 * all. So an unmatched ) is legal POSIX, at least until
337 * we can get it fixed.
339 SETERROR(REG_EPAREN);
344 p->g->iflags |= USEBOL;
350 p->g->iflags |= USEEOL;
359 SETERROR(REG_BADRPT);
362 if (p->g->cflags®_NEWLINE)
371 REQUIRE(MORE(), REG_EESCAPE);
375 case '{': /* okay as ordinary except if digit follows */
376 REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
386 /* we call { a repetition if followed by a digit */
387 if (!( c == '*' || c == '+' || c == '?' ||
388 (c == '{' && MORE2() && isdigit((uch)PEEK2())) ))
389 return; /* no repetition, we're done */
392 REQUIRE(!wascaret, REG_BADRPT);
394 case '*': /* implemented as +? */
395 /* this case does not require the (y|) trick, noKLUDGE */
398 INSERT(OQUEST_, pos);
399 ASTERN(O_QUEST, pos);
406 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
407 INSERT(OCH_, pos); /* offset slightly wrong */
408 ASTERN(OOR1, pos); /* this one's right */
409 AHEAD(pos); /* fix the OCH_ */
410 EMIT(OOR2, 0); /* offset very wrong... */
411 AHEAD(THERE()); /* ...so fix it */
412 ASTERN(O_CH, THERETHERE());
417 if (isdigit((uch)PEEK())) {
419 REQUIRE(count <= count2, REG_BADBR);
420 } else /* single number with comma */
422 } else /* just a single number */
424 repeat(p, pos, count, count2);
425 if (!EAT('}')) { /* error heuristics */
426 while (MORE() && PEEK() != '}')
428 REQUIRE(MORE(), REG_EBRACE);
437 if (!( c == '*' || c == '+' || c == '?' ||
438 (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
440 SETERROR(REG_BADRPT);
444 - p_str - string (no metacharacters) "parser"
447 p_str(struct parse *p)
449 REQUIRE(MORE(), REG_EMPTY);
451 ordinary(p, GETNEXT());
455 - p_bre - BRE parser top level, anchoring and concatenation
456 * Giving end1 as OUT essentially eliminates the end1/end2 check.
458 * This implementation is a bit of a kludge, in that a trailing $ is first
459 * taken as an ordinary character and then revised to be an anchor. The
460 * only undesirable side effect is that '$' gets included as a character
461 * category in such cases. This is fairly harmless; not worth fixing.
462 * The amount of lookahead needed to avoid this kludge is excessive.
465 p_bre(struct parse *p,
466 int end1, /* first terminating character */
467 int end2) /* second terminating character */
469 sopno start = HERE();
470 int first = 1; /* first subexpression? */
475 p->g->iflags |= USEBOL;
478 while (MORE() && !SEETWO(end1, end2)) {
479 wasdollar = p_simp_re(p, first);
482 if (wasdollar) { /* oops, that was a trailing anchor */
485 p->g->iflags |= USEEOL;
489 REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
493 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
495 static int /* was the simple RE an unbackslashed $? */
496 p_simp_re(struct parse *p,
497 int starordinary) /* is a leading * an ordinary character? */
505 # define BACKSL (1<<CHAR_BIT)
507 pos = HERE(); /* repetion op, if any, covers from here */
509 assert(MORE()); /* caller should have ensured this */
512 REQUIRE(MORE(), REG_EESCAPE);
513 c = BACKSL | GETNEXT();
517 if (p->g->cflags®_NEWLINE)
526 SETERROR(REG_BADRPT);
532 p->pbegin[subno] = HERE();
533 EMIT(OLPAREN, subno);
534 /* the MORE here is an error heuristic */
535 if (MORE() && !SEETWO('\\', ')'))
537 if (subno < NPAREN) {
538 p->pend[subno] = HERE();
539 assert(p->pend[subno] != 0);
541 EMIT(ORPAREN, subno);
542 REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
544 case BACKSL|')': /* should not get here -- must be user */
546 SETERROR(REG_EPAREN);
557 i = (c&~BACKSL) - '0';
559 if (p->pend[i] != 0) {
560 assert(i <= p->g->nsub);
562 assert(p->pbegin[i] != 0);
563 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
564 assert(OP(p->strip[p->pend[i]]) == ORPAREN);
565 (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
568 SETERROR(REG_ESUBREG);
572 REQUIRE(starordinary, REG_BADRPT);
575 ordinary(p, (char)c);
579 if (EAT('*')) { /* implemented as +? */
580 /* this case does not require the (y|) trick, noKLUDGE */
583 INSERT(OQUEST_, pos);
584 ASTERN(O_QUEST, pos);
585 } else if (EATTWO('\\', '{')) {
588 if (MORE() && isdigit((uch)PEEK())) {
590 REQUIRE(count <= count2, REG_BADBR);
591 } else /* single number with comma */
593 } else /* just a single number */
595 repeat(p, pos, count, count2);
596 if (!EATTWO('\\', '}')) { /* error heuristics */
597 while (MORE() && !SEETWO('\\', '}'))
599 REQUIRE(MORE(), REG_EBRACE);
602 } else if (c == '$') /* $ (but not \$) ends it */
609 - p_count - parse a repetition count
611 static int /* the value */
612 p_count(struct parse *p)
617 while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
618 count = count*10 + (GETNEXT() - '0');
622 REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
627 - p_bracket - parse a bracketed character list
629 * Note a significant property of this code: if the allocset() did SETERROR,
630 * no set operations are done.
633 p_bracket(struct parse *p)
638 /* Dept of Truly Sickening Special-Case Kludges */
639 if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
644 if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
650 if ((cs = allocset(p)) == NULL) {
651 /* allocset did set error status in p */
656 invert++; /* make note to invert set at end */
661 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
665 MUSTEAT(']', REG_EBRACK);
667 if (p->error != 0) { /* don't mess things up further */
672 if (p->g->cflags®_ICASE) {
676 for (i = p->g->csetsize - 1; i >= 0; i--)
677 if (CHIN(cs, i) && isalpha(i)) {
682 if (cs->multis != NULL)
688 for (i = p->g->csetsize - 1; i >= 0; i--)
693 if (p->g->cflags®_NEWLINE)
695 if (cs->multis != NULL)
699 assert(cs->multis == NULL); /* xxx */
701 if (nch(p, cs) == 1) { /* optimize singleton sets */
702 ordinary(p, firstch(p, cs));
705 EMIT(OANYOF, freezeset(p, cs));
709 - p_b_term - parse one term of a bracketed character list
712 p_b_term(struct parse *p, cset *cs)
718 /* classify what we've got */
719 switch ((MORE()) ? PEEK() : '\0') {
721 c = (MORE2()) ? PEEK2() : '\0';
724 SETERROR(REG_ERANGE);
725 return; /* NOTE RETURN */
733 case ':': /* character class */
735 REQUIRE(MORE(), REG_EBRACK);
737 REQUIRE(c != '-' && c != ']', REG_ECTYPE);
739 REQUIRE(MORE(), REG_EBRACK);
740 REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
742 case '=': /* equivalence class */
744 REQUIRE(MORE(), REG_EBRACK);
746 REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
748 REQUIRE(MORE(), REG_EBRACK);
749 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
751 default: /* symbol, ordinary character, or range */
752 /* xxx revision needed for multichar stuff */
753 start = p_b_symbol(p);
754 if (SEE('-') && MORE2() && PEEK2() != ']') {
760 finish = p_b_symbol(p);
763 /* xxx what about signed chars here... */
764 REQUIRE(start <= finish, REG_ERANGE);
765 for (i = start; i <= finish; i++)
772 - p_b_cclass - parse a character-class name and deal with it
775 p_b_cclass(struct parse *p, cset *cs)
783 while (MORE() && isalpha(PEEK()))
786 for (cp = cclasses; cp->name != NULL; cp++)
787 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
789 if (cp->name == NULL) {
790 /* oops, didn't find it */
791 SETERROR(REG_ECTYPE);
796 while ((c = *u++) != '\0')
798 for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
803 - p_b_eclass - parse an equivalence-class name and deal with it
805 * This implementation is incomplete. xxx
808 p_b_eclass(struct parse *p, cset *cs)
812 c = p_b_coll_elem(p, '=');
817 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
819 static char /* value of symbol */
820 p_b_symbol(struct parse *p)
824 REQUIRE(MORE(), REG_EBRACK);
825 if (!EATTWO('[', '.'))
828 /* collating symbol */
829 value = p_b_coll_elem(p, '.');
830 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
835 - p_b_coll_elem - parse a collating-element name and look it up
837 static char /* value of collating element */
838 p_b_coll_elem(struct parse *p,
839 int endc) /* name ended by endc,']' */
845 while (MORE() && !SEETWO(endc, ']'))
848 SETERROR(REG_EBRACK);
852 for (cp = cnames; cp->name != NULL; cp++)
853 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
854 return(cp->code); /* known name */
856 return(*sp); /* single character */
857 SETERROR(REG_ECOLLATE); /* neither */
862 - othercase - return the case counterpart of an alphabetic
864 static char /* if no counterpart, return ch */
870 return ((uch)tolower(ch));
871 else if (islower(ch))
872 return ((uch)toupper(ch));
873 else /* peculiar, but could happen */
878 - bothcases - emit a dualcase version of a two-case character
880 * Boy, is this implementation ever a kludge...
883 bothcases(struct parse *p, int ch)
885 char *oldnext = p->next;
886 char *oldend = p->end;
890 assert(othercase(ch) != ch); /* p_bracket() would recurse */
897 assert(p->next == bracket+2);
903 - ordinary - emit an ordinary character
906 ordinary(struct parse *p, int ch)
908 cat_t *cap = p->g->categories;
910 if ((p->g->cflags®_ICASE) && isalpha((uch)ch) && othercase(ch) != ch)
913 EMIT(OCHAR, (uch)ch);
915 cap[ch] = p->g->ncategories++;
920 - nonnewline - emit REG_NEWLINE version of OANY
922 * Boy, is this implementation ever a kludge...
925 nonnewline(struct parse *p)
927 char *oldnext = p->next;
928 char *oldend = p->end;
938 assert(p->next == bracket+3);
944 - repeat - generate code for a bounded repetition, recursively if needed
947 repeat(struct parse *p,
948 sopno start, /* operand from here to end of strip */
949 int from, /* repeated from this number */
950 int to) /* to this number of times (maybe INFINITY) */
952 sopno finish = HERE();
955 # define REP(f, t) ((f)*8 + (t))
956 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
959 if (p->error != 0) /* head off possible runaway recursion */
964 switch (REP(MAP(from), MAP(to))) {
965 case REP(0, 0): /* must be user doing this */
966 DROP(finish-start); /* drop the operand */
968 case REP(0, 1): /* as x{1,1}? */
969 case REP(0, N): /* as x{1,n}? */
970 case REP(0, INF): /* as x{1,}? */
971 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
972 INSERT(OCH_, start); /* offset is wrong... */
973 repeat(p, start+1, 1, to);
975 AHEAD(start); /* ... fix it */
978 ASTERN(O_CH, THERETHERE());
980 case REP(1, 1): /* trivial case */
983 case REP(1, N): /* as x?x{1,n-1} */
984 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
988 EMIT(OOR2, 0); /* offset very wrong... */
989 AHEAD(THERE()); /* ...so fix it */
990 ASTERN(O_CH, THERETHERE());
991 copy = dupl(p, start+1, finish+1);
992 assert(copy == finish+4);
993 repeat(p, copy, 1, to-1);
995 case REP(1, INF): /* as x+ */
996 INSERT(OPLUS_, start);
997 ASTERN(O_PLUS, start);
999 case REP(N, N): /* as xx{m-1,n-1} */
1000 copy = dupl(p, start, finish);
1001 repeat(p, copy, from-1, to-1);
1003 case REP(N, INF): /* as xx{n-1,INF} */
1004 copy = dupl(p, start, finish);
1005 repeat(p, copy, from-1, to);
1007 default: /* "can't happen" */
1008 SETERROR(REG_ASSERT); /* just in case */
1014 - seterr - set an error condition
1016 static int /* useless but makes type checking happy */
1017 seterr(struct parse *p, int e)
1019 if (p->error == 0) /* keep earliest error condition */
1021 p->next = nuls; /* try to bring things to a halt */
1023 return(0); /* make the return value well-defined */
1027 - allocset - allocate a set of characters for []
1030 allocset(struct parse *p)
1032 int no = p->g->ncsets++;
1036 size_t css = (size_t)p->g->csetsize;
1039 if (no >= p->ncsalloc) { /* need another column of space */
1042 p->ncsalloc += CHAR_BIT;
1044 assert(nc % CHAR_BIT == 0);
1045 nbytes = nc / CHAR_BIT * css;
1047 ptr = (cset *)realloc((char *)p->g->sets, nc * sizeof(cset));
1052 ptr = (uch *)realloc((char *)p->g->setbits, nbytes);
1055 p->g->setbits = ptr;
1057 for (i = 0; i < no; i++)
1058 p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1060 (void) memset((char *)p->g->setbits + (nbytes - css), 0, css);
1062 /* XXX should not happen */
1063 if (p->g->sets == NULL || p->g->setbits == NULL)
1066 cs = &p->g->sets[no];
1067 cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1068 cs->mask = 1 << ((no) % CHAR_BIT);
1077 free(p->g->setbits);
1078 p->g->setbits = NULL;
1080 SETERROR(REG_ESPACE);
1081 /* caller's responsibility not to do set ops */
1086 - freeset - free a now-unused set
1089 freeset(struct parse *p, cset *cs)
1092 cset *top = &p->g->sets[p->g->ncsets];
1093 size_t css = (size_t)p->g->csetsize;
1095 for (i = 0; i < css; i++)
1097 if (cs == top-1) /* recover only the easy case */
1102 - freezeset - final processing on a set of characters
1104 * The main task here is merging identical sets. This is usually a waste
1105 * of time (although the hash code minimizes the overhead), but can win
1106 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1107 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1110 static int /* set number */
1111 freezeset(struct parse *p, cset *cs)
1115 cset *top = &p->g->sets[p->g->ncsets];
1117 size_t css = (size_t)p->g->csetsize;
1119 /* look for an earlier one which is the same */
1120 for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1121 if (cs2->hash == h && cs2 != cs) {
1123 for (i = 0; i < css; i++)
1124 if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1130 if (cs2 < top) { /* found one */
1135 return((int)(cs - p->g->sets));
1139 - firstch - return first character in a set (which must have at least one)
1141 static int /* character; there is no "none" value */
1142 firstch(struct parse *p, cset *cs)
1145 size_t css = (size_t)p->g->csetsize;
1147 for (i = 0; i < css; i++)
1151 return(0); /* arbitrary */
1155 - nch - number of characters in a set
1158 nch(struct parse *p, cset *cs)
1161 size_t css = (size_t)p->g->csetsize;
1164 for (i = 0; i < css; i++)
1171 - mcadd - add a collating element to a cset
1174 mcadd( struct parse *p, cset *cs, const char *cp)
1176 size_t oldend = cs->smultis;
1179 cs->smultis += strlen(cp) + 1;
1180 np = realloc(cs->multis, cs->smultis);
1185 SETERROR(REG_ESPACE);
1190 llvm_strlcpy(cs->multis + oldend - 1, cp, cs->smultis - oldend + 1);
1194 - mcinvert - invert the list of collating elements in a cset
1196 * This would have to know the set of possibilities. Implementation
1201 mcinvert(struct parse *p, cset *cs)
1203 assert(cs->multis == NULL); /* xxx */
1207 - mccase - add case counterparts of the list of collating elements in a cset
1209 * This would have to know the set of possibilities. Implementation
1214 mccase(struct parse *p, cset *cs)
1216 assert(cs->multis == NULL); /* xxx */
1220 - isinsets - is this character in any sets?
1222 static int /* predicate */
1223 isinsets(struct re_guts *g, int c)
1227 int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1228 unsigned uc = (uch)c;
1230 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1237 - samesets - are these two characters in exactly the same sets?
1239 static int /* predicate */
1240 samesets(struct re_guts *g, int c1, int c2)
1244 int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1245 unsigned uc1 = (uch)c1;
1246 unsigned uc2 = (uch)c2;
1248 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1249 if (col[uc1] != col[uc2])
1255 - categorize - sort out character categories
1258 categorize(struct parse *p, struct re_guts *g)
1260 cat_t *cats = g->categories;
1265 /* avoid making error situations worse */
1269 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1270 if (cats[c] == 0 && isinsets(g, c)) {
1271 cat = g->ncategories++;
1273 for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1274 if (cats[c2] == 0 && samesets(g, c, c2))
1280 - dupl - emit a duplicate of a bunch of sops
1282 static sopno /* start of duplicate */
1283 dupl(struct parse *p,
1284 sopno start, /* from here */
1285 sopno finish) /* to this less one */
1288 sopno len = finish - start;
1290 assert(finish >= start);
1293 enlarge(p, p->ssize + len); /* this many unexpected additions */
1294 assert(p->ssize >= p->slen + len);
1295 (void) memmove((char *)(p->strip + p->slen),
1296 (char *)(p->strip + start), (size_t)len*sizeof(sop));
1302 - doemit - emit a strip operator
1304 * It might seem better to implement this as a macro with a function as
1305 * hard-case backup, but it's just too big and messy unless there are
1306 * some changes to the data structures. Maybe later.
1309 doemit(struct parse *p, sop op, size_t opnd)
1311 /* avoid making error situations worse */
1315 /* deal with oversize operands ("can't happen", more or less) */
1316 assert(opnd < 1<<OPSHIFT);
1318 /* deal with undersized strip */
1319 if (p->slen >= p->ssize)
1320 enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1321 assert(p->slen < p->ssize);
1323 /* finally, it's all reduced to the easy case */
1324 p->strip[p->slen++] = SOP(op, opnd);
1328 - doinsert - insert a sop into the strip
1331 doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
1337 /* avoid making error situations worse */
1342 EMIT(op, opnd); /* do checks, ensure space */
1343 assert(HERE() == sn+1);
1346 /* adjust paren pointers */
1348 for (i = 1; i < NPAREN; i++) {
1349 if (p->pbegin[i] >= pos) {
1352 if (p->pend[i] >= pos) {
1357 memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1358 (HERE()-pos-1)*sizeof(sop));
1363 - dofwd - complete a forward reference
1366 dofwd(struct parse *p, sopno pos, sop value)
1368 /* avoid making error situations worse */
1372 assert(value < 1<<OPSHIFT);
1373 p->strip[pos] = OP(p->strip[pos]) | value;
1377 - enlarge - enlarge the strip
1380 enlarge(struct parse *p, sopno size)
1384 if (p->ssize >= size)
1387 sp = (sop *)realloc(p->strip, size*sizeof(sop));
1389 SETERROR(REG_ESPACE);
1397 - stripsnug - compact the strip
1400 stripsnug(struct parse *p, struct re_guts *g)
1402 g->nstates = p->slen;
1403 g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
1404 if (g->strip == NULL) {
1405 SETERROR(REG_ESPACE);
1406 g->strip = p->strip;
1411 - findmust - fill in must and mlen with longest mandatory literal string
1413 * This algorithm could do fancy things like analyzing the operands of |
1414 * for common subsequences. Someday. This code is simple and finds most
1415 * of the interesting cases.
1417 * Note that must and mlen got initialized during setup.
1420 findmust(struct parse *p, struct re_guts *g)
1423 sop *start = 0; /* start initialized in the default case, after that */
1424 sop *newstart = 0; /* newstart was initialized in the OCHAR case */
1430 /* avoid making error situations worse */
1434 /* find the longest OCHAR sequence in strip */
1436 scan = g->strip + 1;
1440 case OCHAR: /* sequence member */
1441 if (newlen == 0) /* new sequence */
1442 newstart = scan - 1;
1445 case OPLUS_: /* things that don't break one */
1449 case OQUEST_: /* things that must be skipped */
1455 /* assert() interferes w debug printouts */
1456 if (OP(s) != O_QUEST && OP(s) != O_CH &&
1458 g->iflags |= REGEX_BAD;
1461 } while (OP(s) != O_QUEST && OP(s) != O_CH);
1463 default: /* things that break a sequence */
1464 if (newlen > g->mlen) { /* ends one */
1471 } while (OP(s) != OEND);
1473 if (g->mlen == 0) /* there isn't one */
1476 /* turn it into a character string */
1477 g->must = malloc((size_t)g->mlen + 1);
1478 if (g->must == NULL) { /* argh; just forget it */
1484 for (i = g->mlen; i > 0; i--) {
1485 while (OP(s = *scan++) != OCHAR)
1487 assert(cp < g->must + g->mlen);
1488 *cp++ = (char)OPND(s);
1490 assert(cp == g->must + g->mlen);
1491 *cp++ = '\0'; /* just on general principles */
1495 - pluscount - count + nesting
1497 static sopno /* nesting depth */
1498 pluscount(struct parse *p, struct re_guts *g)
1506 return(0); /* there may not be an OEND */
1508 scan = g->strip + 1;
1516 if (plusnest > maxnest)
1521 } while (OP(s) != OEND);
1523 g->iflags |= REGEX_BAD;