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.2 2002/03/20 13:13:15 dcs Exp $
40 * $DragonFly: src/lib/libc/regex/regcomp.c,v 1.7 2005/11/20 09:18:37 swildner Exp $
42 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
45 #include <sys/types.h>
62 * parse structure, passed up and down to avoid global variables and
66 char *next; /* next character in RE */
67 char *end; /* end of string (-> NUL normally) */
68 int error; /* has an error been seen? */
69 sop *strip; /* malloced strip */
70 sopno ssize; /* malloced strip size (allocated) */
71 sopno slen; /* malloced strip length (used) */
72 int ncsalloc; /* number of csets allocated */
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 ========= */
84 /* === regcomp.c === */
85 static void p_ere(struct parse *p, int stop);
86 static void p_ere_exp(struct parse *p);
87 static void p_str(struct parse *p);
88 static void p_bre(struct parse *p, int end1, int end2);
89 static int p_simp_re(struct parse *p, int starordinary);
90 static int p_count(struct parse *p);
91 static void p_bracket(struct parse *p);
92 static void p_b_term(struct parse *p, cset *cs);
93 static void p_b_cclass(struct parse *p, cset *cs);
94 static void p_b_eclass(struct parse *p, cset *cs);
95 static char p_b_symbol(struct parse *p);
96 static char p_b_coll_elem(struct parse *p, int endc);
97 static char othercase(int ch);
98 static void bothcases(struct parse *p, int ch);
99 static void ordinary(struct parse *p, int ch);
100 static void nonnewline(struct parse *p);
101 static void repeat(struct parse *p, sopno start, int from, int to);
102 static int seterr(struct parse *p, int e);
103 static cset *allocset(struct parse *p);
104 static void freeset(struct parse *p, cset *cs);
105 static int freezeset(struct parse *p, cset *cs);
106 static int firstch(struct parse *p, cset *cs);
107 static int nch(struct parse *p, cset *cs);
108 static void mcadd(struct parse *p, cset *cs, char *cp);
110 static void mcsub(cset *cs, char *cp);
111 static int mcin(cset *cs, char *cp);
112 static char *mcfind(cset *cs, char *cp);
114 static void mcinvert(struct parse *p, cset *cs);
115 static void mccase(struct parse *p, cset *cs);
116 static int isinsets(struct re_guts *g, int c);
117 static int samesets(struct re_guts *g, int c1, int c2);
118 static void categorize(struct parse *p, struct re_guts *g);
119 static sopno dupl(struct parse *p, sopno start, sopno finish);
120 static void doemit(struct parse *p, sop op, size_t opnd);
121 static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
122 static void dofwd(struct parse *p, sopno pos, sop value);
123 static void enlarge(struct parse *p, sopno size);
124 static void stripsnug(struct parse *p, struct re_guts *g);
125 static void findmust(struct parse *p, struct re_guts *g);
126 static int altoffset(sop *scan, int offset, int mccs);
127 static void computejumps(struct parse *p, struct re_guts *g);
128 static void computematchjumps(struct parse *p, struct re_guts *g);
129 static sopno pluscount(struct parse *p, struct re_guts *g);
134 /* ========= end header generated by ./mkh ========= */
136 static char nuls[10]; /* place to point scanner in event of error */
139 * macros for use with parse structure
140 * BEWARE: these know that the parse structure is named `p' !!!
142 #define PEEK() (*p->next)
143 #define PEEK2() (*(p->next+1))
144 #define MORE() (p->next < p->end)
145 #define MORE2() (p->next+1 < p->end)
146 #define SEE(c) (MORE() && PEEK() == (c))
147 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
148 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
149 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
150 #define NEXT() (p->next++)
151 #define NEXT2() (p->next += 2)
152 #define NEXTn(n) (p->next += (n))
153 #define GETNEXT() (*p->next++)
154 #define SETERROR(e) seterr(p, (e))
155 #define REQUIRE(co, e) ((co) || SETERROR(e))
156 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
157 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
158 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
159 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
160 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
161 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
162 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
163 #define HERE() (p->slen)
164 #define THERE() (p->slen - 1)
165 #define THERETHERE() (p->slen - 2)
166 #define DROP(n) (p->slen -= (n))
169 static int never = 0; /* for use in asserts; shuts lint up */
171 #define never 0 /* some <assert.h>s have bugs too */
174 /* Macro used by computejump()/computematchjump() */
175 #define MIN(a,b) ((a)<(b)?(a):(b))
178 - regcomp - interface for parser and compilation
179 = extern int regcomp(regex_t *, const char *, int);
180 = #define REG_BASIC 0000
181 = #define REG_EXTENDED 0001
182 = #define REG_ICASE 0002
183 = #define REG_NOSUB 0004
184 = #define REG_NEWLINE 0010
185 = #define REG_NOSPEC 0020
186 = #define REG_PEND 0040
187 = #define REG_DUMP 0200
189 int /* 0 success, otherwise REG_something */
190 regcomp(regex_t *preg, const char *pattern, int cflags)
194 struct parse *p = &pa;
198 # define GOODFLAGS(f) (f)
200 # define GOODFLAGS(f) ((f)&~REG_DUMP)
203 cflags = GOODFLAGS(cflags);
204 if ((cflags®_EXTENDED) && (cflags®_NOSPEC))
207 if (cflags®_PEND) {
208 if (preg->re_endp < pattern)
210 len = preg->re_endp - pattern;
212 len = strlen((char *)pattern);
214 /* do the mallocs early so failure handling is easy */
215 g = (struct re_guts *)malloc(sizeof(struct re_guts) +
216 (NC-1)*sizeof(cat_t));
219 p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
220 p->strip = (sop *)malloc(p->ssize * sizeof(sop));
222 if (p->strip == NULL) {
229 p->next = (char *)pattern; /* convenience; we do not modify it */
230 p->end = p->next + len;
233 for (i = 0; i < NPAREN; i++) {
251 g->ncategories = 1; /* category 0 is "everything else" */
252 g->categories = &g->catspace[-(CHAR_MIN)];
253 memset((char *)g->catspace, 0, NC*sizeof(cat_t));
258 g->firststate = THERE();
259 if (cflags®_EXTENDED)
261 else if (cflags®_NOSPEC)
266 g->laststate = THERE();
268 /* tidy up loose ends and fill things in */
272 /* only use Boyer-Moore algorithm if the pattern is bigger
273 * than three characters
277 computematchjumps(p, g);
278 if(g->matchjump == NULL && g->charjump != NULL) {
283 g->nplus = pluscount(p, g);
285 preg->re_nsub = g->nsub;
287 preg->re_magic = MAGIC1;
289 /* not debugging, so can't rely on the assert() in regexec() */
291 SETERROR(REG_ASSERT);
294 /* win or lose, we're done */
295 if (p->error != 0) /* lose */
301 - p_ere - ERE parser top level, concatenation and alternation
302 == static void p_ere(struct parse *p, int stop);
305 p_ere(struct parse *p,
306 int stop) /* character this ERE should end at */
312 int first = 1; /* is this the first alternative? */
318 /* do a bunch of concatenated expressions */
320 while (MORE() && (c = PEEK()) != '|' && c != stop)
322 REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
325 break; /* NOTE BREAK OUT */
328 INSERT(OCH_, conc); /* offset is wrong */
333 ASTERN(OOR1, prevback);
335 AHEAD(prevfwd); /* fix previous offset */
337 EMIT(OOR2, 0); /* offset is very wrong */
340 if (!first) { /* tail-end fixups */
342 ASTERN(O_CH, prevback);
345 assert(!MORE() || SEE(stop));
349 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
350 == static void p_ere_exp(struct parse *p);
353 p_ere_exp(struct parse *p)
362 assert(MORE()); /* caller should have ensured this */
368 REQUIRE(MORE(), REG_EPAREN);
372 p->pbegin[subno] = HERE();
373 EMIT(OLPAREN, subno);
376 if (subno < NPAREN) {
377 p->pend[subno] = HERE();
378 assert(p->pend[subno] != 0);
380 EMIT(ORPAREN, subno);
381 MUSTEAT(')', REG_EPAREN);
383 #ifndef POSIX_MISTAKE
384 case ')': /* happens only if no current unmatched ( */
386 * You may ask, why the ifndef? Because I didn't notice
387 * this until slightly too late for 1003.2, and none of the
388 * other 1003.2 regular-expression reviewers noticed it at
389 * all. So an unmatched ) is legal POSIX, at least until
390 * we can get it fixed.
392 SETERROR(REG_EPAREN);
397 p->g->iflags |= USEBOL;
403 p->g->iflags |= USEEOL;
412 SETERROR(REG_BADRPT);
415 if (p->g->cflags®_NEWLINE)
424 REQUIRE(MORE(), REG_EESCAPE);
428 case '{': /* okay as ordinary except if digit follows */
429 REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
439 /* we call { a repetition if followed by a digit */
440 if (!( c == '*' || c == '+' || c == '?' ||
441 (c == '{' && MORE2() && isdigit((uch)PEEK2())) ))
442 return; /* no repetition, we're done */
445 REQUIRE(!wascaret, REG_BADRPT);
447 case '*': /* implemented as +? */
448 /* this case does not require the (y|) trick, noKLUDGE */
451 INSERT(OQUEST_, pos);
452 ASTERN(O_QUEST, pos);
459 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
460 INSERT(OCH_, pos); /* offset slightly wrong */
461 ASTERN(OOR1, pos); /* this one's right */
462 AHEAD(pos); /* fix the OCH_ */
463 EMIT(OOR2, 0); /* offset very wrong... */
464 AHEAD(THERE()); /* ...so fix it */
465 ASTERN(O_CH, THERETHERE());
470 if (isdigit((uch)PEEK())) {
472 REQUIRE(count <= count2, REG_BADBR);
473 } else /* single number with comma */
475 } else /* just a single number */
477 repeat(p, pos, count, count2);
478 if (!EAT('}')) { /* error heuristics */
479 while (MORE() && PEEK() != '}')
481 REQUIRE(MORE(), REG_EBRACE);
490 if (!( c == '*' || c == '+' || c == '?' ||
491 (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
493 SETERROR(REG_BADRPT);
497 - p_str - string (no metacharacters) "parser"
498 == static void p_str(struct parse *p);
501 p_str(struct parse *p)
503 REQUIRE(MORE(), REG_EMPTY);
505 ordinary(p, GETNEXT());
509 - p_bre - BRE parser top level, anchoring and concatenation
510 == static void p_bre(struct parse *p, int end1, \
512 * Giving end1 as OUT essentially eliminates the end1/end2 check.
514 * This implementation is a bit of a kludge, in that a trailing $ is first
515 * taken as an ordinary character and then revised to be an anchor. The
516 * only undesirable side effect is that '$' gets included as a character
517 * category in such cases. This is fairly harmless; not worth fixing.
518 * The amount of lookahead needed to avoid this kludge is excessive.
521 p_bre(struct parse *p,
522 int end1, /* first terminating character */
523 int end2) /* second terminating character */
525 sopno start = HERE();
526 int first = 1; /* first subexpression? */
531 p->g->iflags |= USEBOL;
534 while (MORE() && !SEETWO(end1, end2)) {
535 wasdollar = p_simp_re(p, first);
538 if (wasdollar) { /* oops, that was a trailing anchor */
541 p->g->iflags |= USEEOL;
545 REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
549 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
550 == static int p_simp_re(struct parse *p, int starordinary);
552 static int /* was the simple RE an unbackslashed $? */
553 p_simp_re(struct parse *p,
554 int starordinary) /* is a leading * an ordinary character? */
562 # define BACKSL (1<<CHAR_BIT)
564 pos = HERE(); /* repetion op, if any, covers from here */
566 assert(MORE()); /* caller should have ensured this */
569 REQUIRE(MORE(), REG_EESCAPE);
570 c = BACKSL | GETNEXT();
574 if (p->g->cflags®_NEWLINE)
583 SETERROR(REG_BADRPT);
589 p->pbegin[subno] = HERE();
590 EMIT(OLPAREN, subno);
591 /* the MORE here is an error heuristic */
592 if (MORE() && !SEETWO('\\', ')'))
594 if (subno < NPAREN) {
595 p->pend[subno] = HERE();
596 assert(p->pend[subno] != 0);
598 EMIT(ORPAREN, subno);
599 REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
601 case BACKSL|')': /* should not get here -- must be user */
603 SETERROR(REG_EPAREN);
614 i = (c&~BACKSL) - '0';
616 if (p->pend[i] != 0) {
617 assert(i <= p->g->nsub);
619 assert(p->pbegin[i] != 0);
620 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
621 assert(OP(p->strip[p->pend[i]]) == ORPAREN);
622 dupl(p, p->pbegin[i]+1, p->pend[i]);
625 SETERROR(REG_ESUBREG);
629 REQUIRE(starordinary, REG_BADRPT);
632 ordinary(p, (char)c);
636 if (EAT('*')) { /* implemented as +? */
637 /* this case does not require the (y|) trick, noKLUDGE */
640 INSERT(OQUEST_, pos);
641 ASTERN(O_QUEST, pos);
642 } else if (EATTWO('\\', '{')) {
645 if (MORE() && isdigit((uch)PEEK())) {
647 REQUIRE(count <= count2, REG_BADBR);
648 } else /* single number with comma */
650 } else /* just a single number */
652 repeat(p, pos, count, count2);
653 if (!EATTWO('\\', '}')) { /* error heuristics */
654 while (MORE() && !SEETWO('\\', '}'))
656 REQUIRE(MORE(), REG_EBRACE);
659 } else if (c == '$') /* $ (but not \$) ends it */
666 - p_count - parse a repetition count
667 == static int p_count(struct parse *p);
669 static int /* the value */
670 p_count(struct parse *p)
675 while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
676 count = count*10 + (GETNEXT() - '0');
680 REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
685 - p_bracket - parse a bracketed character list
686 == static void p_bracket(struct parse *p);
688 * Note a significant property of this code: if the allocset() did SETERROR,
689 * no set operations are done.
692 p_bracket(struct parse *p)
694 cset *cs = allocset(p);
697 /* Dept of Truly Sickening Special-Case Kludges */
698 if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
703 if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
710 invert++; /* make note to invert set at end */
715 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
719 MUSTEAT(']', REG_EBRACK);
721 if (p->error != 0) /* don't mess things up further */
724 if (p->g->cflags®_ICASE) {
728 for (i = p->g->csetsize - 1; i >= 0; i--)
729 if (CHIN(cs, i) && isalpha(i)) {
734 if (cs->multis != NULL)
740 for (i = p->g->csetsize - 1; i >= 0; i--)
745 if (p->g->cflags®_NEWLINE)
747 if (cs->multis != NULL)
751 assert(cs->multis == NULL); /* xxx */
753 if (nch(p, cs) == 1) { /* optimize singleton sets */
754 ordinary(p, firstch(p, cs));
757 EMIT(OANYOF, freezeset(p, cs));
761 - p_b_term - parse one term of a bracketed character list
762 == static void p_b_term(struct parse *p, cset *cs);
765 p_b_term(struct parse *p, cset *cs)
771 /* classify what we've got */
772 switch ((MORE()) ? PEEK() : '\0') {
774 c = (MORE2()) ? PEEK2() : '\0';
777 SETERROR(REG_ERANGE);
778 return; /* NOTE RETURN */
786 case ':': /* character class */
788 REQUIRE(MORE(), REG_EBRACK);
790 REQUIRE(c != '-' && c != ']', REG_ECTYPE);
792 REQUIRE(MORE(), REG_EBRACK);
793 REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
795 case '=': /* equivalence class */
797 REQUIRE(MORE(), REG_EBRACK);
799 REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
801 REQUIRE(MORE(), REG_EBRACK);
802 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
804 default: /* symbol, ordinary character, or range */
805 /* xxx revision needed for multichar stuff */
806 start = p_b_symbol(p);
807 if (SEE('-') && MORE2() && PEEK2() != ']') {
813 finish = p_b_symbol(p);
819 if (__collate_load_error) {
820 REQUIRE((uch)start <= (uch)finish, REG_ERANGE);
821 for (i = (uch)start; i <= (uch)finish; i++)
824 REQUIRE(__collate_range_cmp(start, finish) <= 0, REG_ERANGE);
825 for (i = CHAR_MIN; i <= CHAR_MAX; i++) {
826 if ( __collate_range_cmp(start, i) <= 0
827 && __collate_range_cmp(i, finish) <= 0
838 - p_b_cclass - parse a character-class name and deal with it
839 == static void p_b_cclass(struct parse *p, cset *cs);
842 p_b_cclass(struct parse *p, cset *cs)
849 while (MORE() && isalpha((uch)PEEK()))
852 for (cp = cclasses; cp->name != NULL; cp++)
853 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
855 if (cp->name == NULL) {
856 /* oops, didn't find it */
857 SETERROR(REG_ECTYPE);
863 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
868 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
873 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
878 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
883 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
888 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
893 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
898 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
903 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
908 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
913 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
918 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
919 if (isxdigit((uch)c))
924 for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
930 - p_b_eclass - parse an equivalence-class name and deal with it
931 == static void p_b_eclass(struct parse *p, cset *cs);
933 * This implementation is incomplete. xxx
936 p_b_eclass(struct parse *p, cset *cs)
940 c = p_b_coll_elem(p, '=');
945 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
946 == static char p_b_symbol(struct parse *p);
948 static char /* value of symbol */
949 p_b_symbol(struct parse *p)
953 REQUIRE(MORE(), REG_EBRACK);
954 if (!EATTWO('[', '.'))
957 /* collating symbol */
958 value = p_b_coll_elem(p, '.');
959 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
964 - p_b_coll_elem - parse a collating-element name and look it up
965 == static char p_b_coll_elem(struct parse *p, int endc);
967 static char /* value of collating element */
968 p_b_coll_elem(struct parse *p,
969 int endc) /* name ended by endc,']' */
975 while (MORE() && !SEETWO(endc, ']'))
978 SETERROR(REG_EBRACK);
982 for (cp = cnames; cp->name != NULL; cp++)
983 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
984 return(cp->code); /* known name */
986 return(*sp); /* single character */
987 SETERROR(REG_ECOLLATE); /* neither */
992 - othercase - return the case counterpart of an alphabetic
993 == static char othercase(int ch);
995 static char /* if no counterpart, return ch */
1001 return(tolower(ch));
1002 else if (islower(ch))
1003 return(toupper(ch));
1004 else /* peculiar, but could happen */
1009 - bothcases - emit a dualcase version of a two-case character
1010 == static void bothcases(struct parse *p, int ch);
1012 * Boy, is this implementation ever a kludge...
1015 bothcases(struct parse *p, int ch)
1017 char *oldnext = p->next;
1018 char *oldend = p->end;
1022 assert(othercase(ch) != ch); /* p_bracket() would recurse */
1029 assert(p->next == bracket+2);
1035 - ordinary - emit an ordinary character
1036 == static void ordinary(struct parse *p, int ch);
1039 ordinary(struct parse *p, int ch)
1041 cat_t *cap = p->g->categories;
1043 if ((p->g->cflags®_ICASE) && isalpha((uch)ch) && othercase(ch) != ch)
1046 EMIT(OCHAR, (uch)ch);
1048 cap[ch] = p->g->ncategories++;
1053 - nonnewline - emit REG_NEWLINE version of OANY
1054 == static void nonnewline(struct parse *p);
1056 * Boy, is this implementation ever a kludge...
1059 nonnewline(struct parse *p)
1061 char *oldnext = p->next;
1062 char *oldend = p->end;
1072 assert(p->next == bracket+3);
1078 - repeat - generate code for a bounded repetition, recursively if needed
1079 == static void repeat(struct parse *p, sopno start, int from, int to);
1082 repeat(struct parse *p,
1083 sopno start, /* operand from here to end of strip */
1084 int from, /* repeated from this number */
1085 int to) /* to this number of times (maybe INFINITY) */
1087 sopno finish = HERE();
1090 # define REP(f, t) ((f)*8 + (t))
1091 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1094 if (p->error != 0) /* head off possible runaway recursion */
1099 switch (REP(MAP(from), MAP(to))) {
1100 case REP(0, 0): /* must be user doing this */
1101 DROP(finish-start); /* drop the operand */
1103 case REP(0, 1): /* as x{1,1}? */
1104 case REP(0, N): /* as x{1,n}? */
1105 case REP(0, INF): /* as x{1,}? */
1106 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1107 INSERT(OCH_, start); /* offset is wrong... */
1108 repeat(p, start+1, 1, to);
1109 ASTERN(OOR1, start);
1110 AHEAD(start); /* ... fix it */
1113 ASTERN(O_CH, THERETHERE());
1115 case REP(1, 1): /* trivial case */
1118 case REP(1, N): /* as x?x{1,n-1} */
1119 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1120 INSERT(OCH_, start);
1121 ASTERN(OOR1, start);
1123 EMIT(OOR2, 0); /* offset very wrong... */
1124 AHEAD(THERE()); /* ...so fix it */
1125 ASTERN(O_CH, THERETHERE());
1126 copy = dupl(p, start+1, finish+1);
1127 assert(copy == finish+4);
1128 repeat(p, copy, 1, to-1);
1130 case REP(1, INF): /* as x+ */
1131 INSERT(OPLUS_, start);
1132 ASTERN(O_PLUS, start);
1134 case REP(N, N): /* as xx{m-1,n-1} */
1135 copy = dupl(p, start, finish);
1136 repeat(p, copy, from-1, to-1);
1138 case REP(N, INF): /* as xx{n-1,INF} */
1139 copy = dupl(p, start, finish);
1140 repeat(p, copy, from-1, to);
1142 default: /* "can't happen" */
1143 SETERROR(REG_ASSERT); /* just in case */
1149 - seterr - set an error condition
1150 == static int seterr(struct parse *p, int e);
1152 static int /* useless but makes type checking happy */
1153 seterr(struct parse *p, int e)
1155 if (p->error == 0) /* keep earliest error condition */
1157 p->next = nuls; /* try to bring things to a halt */
1159 return(0); /* make the return value well-defined */
1163 - allocset - allocate a set of characters for []
1164 == static cset *allocset(struct parse *p);
1167 allocset(struct parse *p)
1169 int no = p->g->ncsets++;
1173 size_t css = (size_t)p->g->csetsize;
1176 if (no >= p->ncsalloc) { /* need another column of space */
1177 p->ncsalloc += CHAR_BIT;
1179 assert(nc % CHAR_BIT == 0);
1180 nbytes = nc / CHAR_BIT * css;
1181 if (p->g->sets == NULL)
1182 p->g->sets = (cset *)malloc(nc * sizeof(cset));
1184 p->g->sets = (cset *)reallocf((char *)p->g->sets,
1186 if (p->g->setbits == NULL)
1187 p->g->setbits = (uch *)malloc(nbytes);
1189 p->g->setbits = (uch *)reallocf((char *)p->g->setbits,
1191 /* xxx this isn't right if setbits is now NULL */
1192 for (i = 0; i < no; i++)
1193 p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1195 if (p->g->sets != NULL && p->g->setbits != NULL)
1196 memset((char *)p->g->setbits + (nbytes - css), 0, css);
1199 SETERROR(REG_ESPACE);
1200 /* caller's responsibility not to do set ops */
1204 assert(p->g->sets != NULL); /* xxx */
1205 cs = &p->g->sets[no];
1206 cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1207 cs->mask = 1 << ((no) % CHAR_BIT);
1216 - freeset - free a now-unused set
1217 == static void freeset(struct parse *p, cset *cs);
1220 freeset(struct parse *p, cset *cs)
1223 cset *top = &p->g->sets[p->g->ncsets];
1224 size_t css = (size_t)p->g->csetsize;
1226 for (i = 0; i < css; i++)
1228 if (cs == top-1) /* recover only the easy case */
1233 - freezeset - final processing on a set of characters
1234 == static int freezeset(struct parse *p, cset *cs);
1236 * The main task here is merging identical sets. This is usually a waste
1237 * of time (although the hash code minimizes the overhead), but can win
1238 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1239 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1242 static int /* set number */
1243 freezeset(struct parse *p, cset *cs)
1247 cset *top = &p->g->sets[p->g->ncsets];
1249 size_t css = (size_t)p->g->csetsize;
1251 /* look for an earlier one which is the same */
1252 for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1253 if (cs2->hash == h && cs2 != cs) {
1255 for (i = 0; i < css; i++)
1256 if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1262 if (cs2 < top) { /* found one */
1267 return((int)(cs - p->g->sets));
1271 - firstch - return first character in a set (which must have at least one)
1272 == static int firstch(struct parse *p, cset *cs);
1274 static int /* character; there is no "none" value */
1275 firstch(struct parse *p, cset *cs)
1278 size_t css = (size_t)p->g->csetsize;
1280 for (i = 0; i < css; i++)
1284 return(0); /* arbitrary */
1288 - nch - number of characters in a set
1289 == static int nch(struct parse *p, cset *cs);
1292 nch(struct parse *p, cset *cs)
1295 size_t css = (size_t)p->g->csetsize;
1298 for (i = 0; i < css; i++)
1305 - mcadd - add a collating element to a cset
1306 == static void mcadd(struct parse *p, cset *cs, \
1310 mcadd(struct parse *p, cset *cs, char *cp)
1312 size_t oldend = cs->smultis;
1314 cs->smultis += strlen(cp) + 1;
1315 if (cs->multis == NULL)
1316 cs->multis = malloc(cs->smultis);
1318 cs->multis = reallocf(cs->multis, cs->smultis);
1319 if (cs->multis == NULL) {
1320 SETERROR(REG_ESPACE);
1324 strcpy(cs->multis + oldend - 1, cp);
1325 cs->multis[cs->smultis - 1] = '\0';
1330 - mcsub - subtract a collating element from a cset
1331 == static void mcsub(cset *cs, char *cp);
1334 mcsub(cset *cs, char *cp)
1336 char *fp = mcfind(cs, cp);
1337 size_t len = strlen(fp);
1340 memmove(fp, fp + len + 1, cs->smultis - (fp + len + 1 - cs->multis));
1343 if (cs->smultis == 0) {
1349 cs->multis = reallocf(cs->multis, cs->smultis);
1350 assert(cs->multis != NULL);
1354 - mcin - is a collating element in a cset?
1355 == static int mcin(cset *cs, char *cp);
1358 mcin(cset *cs, char *cp)
1360 return(mcfind(cs, cp) != NULL);
1364 - mcfind - find a collating element in a cset
1365 == static char *mcfind(cset *cs, char *cp);
1368 mcfind(cset *cs, char *cp)
1372 if (cs->multis == NULL)
1374 for (p = cs->multis; *p != '\0'; p += strlen(p) + 1)
1375 if (strcmp(cp, p) == 0)
1382 - mcinvert - invert the list of collating elements in a cset
1383 == static void mcinvert(struct parse *p, cset *cs);
1385 * This would have to know the set of possibilities. Implementation
1389 mcinvert(struct parse *p __unused, cset *cs)
1391 assert(cs->multis == NULL); /* xxx */
1395 - mccase - add case counterparts of the list of collating elements in a cset
1396 == static void mccase(struct parse *p, cset *cs);
1398 * This would have to know the set of possibilities. Implementation
1402 mccase(struct parse *p __unused, cset *cs)
1404 assert(cs->multis == NULL); /* xxx */
1408 - isinsets - is this character in any sets?
1409 == static int isinsets(struct re_guts *g, int c);
1411 static int /* predicate */
1412 isinsets(struct re_guts *g, int c)
1416 int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1417 unsigned uc = (uch)c;
1419 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1426 - samesets - are these two characters in exactly the same sets?
1427 == static int samesets(struct re_guts *g, int c1, int c2);
1429 static int /* predicate */
1430 samesets(struct re_guts *g, int c1, int c2)
1434 int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1435 unsigned uc1 = (uch)c1;
1436 unsigned uc2 = (uch)c2;
1438 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1439 if (col[uc1] != col[uc2])
1445 - categorize - sort out character categories
1446 == static void categorize(struct parse *p, struct re_guts *g);
1449 categorize(struct parse *p, struct re_guts *g)
1451 cat_t *cats = g->categories;
1456 /* avoid making error situations worse */
1460 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1461 if (cats[c] == 0 && isinsets(g, c)) {
1462 cat = g->ncategories++;
1464 for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1465 if (cats[c2] == 0 && samesets(g, c, c2))
1471 - dupl - emit a duplicate of a bunch of sops
1472 == static sopno dupl(struct parse *p, sopno start, sopno finish);
1474 static sopno /* start of duplicate */
1475 dupl(struct parse *p,
1476 sopno start, /* from here */
1477 sopno finish) /* to this less one */
1480 sopno len = finish - start;
1482 assert(finish >= start);
1485 enlarge(p, p->ssize + len); /* this many unexpected additions */
1486 assert(p->ssize >= p->slen + len);
1487 memcpy((char *)(p->strip + p->slen), (char *)(p->strip + start),
1488 (size_t)len*sizeof(sop));
1494 - doemit - emit a strip operator
1495 == static void doemit(struct parse *p, sop op, size_t opnd);
1497 * It might seem better to implement this as a macro with a function as
1498 * hard-case backup, but it's just too big and messy unless there are
1499 * some changes to the data structures. Maybe later.
1502 doemit(struct parse *p, sop op, size_t opnd)
1504 /* avoid making error situations worse */
1508 /* deal with oversize operands ("can't happen", more or less) */
1509 assert(opnd < 1<<OPSHIFT);
1511 /* deal with undersized strip */
1512 if (p->slen >= p->ssize)
1513 enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1514 assert(p->slen < p->ssize);
1516 /* finally, it's all reduced to the easy case */
1517 p->strip[p->slen++] = SOP(op, opnd);
1521 - doinsert - insert a sop into the strip
1522 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
1525 doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
1531 /* avoid making error situations worse */
1536 EMIT(op, opnd); /* do checks, ensure space */
1537 assert(HERE() == sn+1);
1540 /* adjust paren pointers */
1542 for (i = 1; i < NPAREN; i++) {
1543 if (p->pbegin[i] >= pos) {
1546 if (p->pend[i] >= pos) {
1551 memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1552 (HERE()-pos-1)*sizeof(sop));
1557 - dofwd - complete a forward reference
1558 == static void dofwd(struct parse *p, sopno pos, sop value);
1561 dofwd(struct parse *p, sopno pos, sop value)
1563 /* avoid making error situations worse */
1567 assert(value < 1<<OPSHIFT);
1568 p->strip[pos] = OP(p->strip[pos]) | value;
1572 - enlarge - enlarge the strip
1573 == static void enlarge(struct parse *p, sopno size);
1576 enlarge(struct parse *p, sopno size)
1580 if (p->ssize >= size)
1583 sp = (sop *)realloc(p->strip, size*sizeof(sop));
1585 SETERROR(REG_ESPACE);
1593 - stripsnug - compact the strip
1594 == static void stripsnug(struct parse *p, struct re_guts *g);
1597 stripsnug(struct parse *p, struct re_guts *g)
1599 g->nstates = p->slen;
1600 g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
1601 if (g->strip == NULL) {
1602 SETERROR(REG_ESPACE);
1603 g->strip = p->strip;
1608 - findmust - fill in must and mlen with longest mandatory literal string
1609 == static void findmust(struct parse *p, struct re_guts *g);
1611 * This algorithm could do fancy things like analyzing the operands of |
1612 * for common subsequences. Someday. This code is simple and finds most
1613 * of the interesting cases.
1615 * Note that must and mlen got initialized during setup.
1618 findmust(struct parse *p, struct re_guts *g)
1633 /* avoid making error situations worse */
1637 /* Find out if we can handle OANYOF or not */
1639 for (cs = 0; cs < g->ncsets; cs++)
1640 if (g->sets[cs].multis != NULL)
1643 /* find the longest OCHAR sequence in strip */
1647 scan = g->strip + 1;
1651 case OCHAR: /* sequence member */
1652 if (newlen == 0) /* new sequence */
1653 newstart = scan - 1;
1656 case OPLUS_: /* things that don't break one */
1660 case OQUEST_: /* things that must be skipped */
1662 offset = altoffset(scan, offset, mccs);
1667 /* assert() interferes w debug printouts */
1668 if (OP(s) != O_QUEST && OP(s) != O_CH &&
1673 } while (OP(s) != O_QUEST && OP(s) != O_CH);
1675 case OBOW: /* things that break a sequence */
1682 if (newlen > g->mlen) { /* ends one */
1686 g->moffset += offset;
1689 g->moffset = offset;
1697 if (newlen > g->mlen) { /* ends one */
1701 g->moffset += offset;
1704 g->moffset = offset;
1713 case OANYOF: /* may or may not invalidate offset */
1714 /* First, everything as OANY */
1715 if (newlen > g->mlen) { /* ends one */
1719 g->moffset += offset;
1722 g->moffset = offset;
1730 /* And, now, if we found out we can't deal with
1731 * it, make offset = -1.
1737 /* Anything here makes it impossible or too hard
1738 * to calculate the offset -- so we give up;
1739 * save the last known good offset, in case the
1740 * must sequence doesn't occur later.
1742 if (newlen > g->mlen) { /* ends one */
1746 g->moffset += offset;
1748 g->moffset = offset;
1754 } while (OP(s) != OEND);
1756 if (g->mlen == 0) { /* there isn't one */
1761 /* turn it into a character string */
1762 g->must = malloc((size_t)g->mlen + 1);
1763 if (g->must == NULL) { /* argh; just forget it */
1770 for (i = g->mlen; i > 0; i--) {
1771 while (OP(s = *scan++) != OCHAR)
1773 assert(cp < g->must + g->mlen);
1774 *cp++ = (char)OPND(s);
1776 assert(cp == g->must + g->mlen);
1777 *cp++ = '\0'; /* just on general principles */
1781 - altoffset - choose biggest offset among multiple choices
1782 == static int altoffset(sop *scan, int offset, int mccs);
1784 * Compute, recursively if necessary, the largest offset among multiple
1788 altoffset(sop *scan, int offset, int mccs)
1794 /* If we gave up already on offsets, return */
1801 while (OP(s) != O_QUEST && OP(s) != O_CH) {
1810 try = altoffset(scan, try, mccs);
1817 if (OP(s) != O_QUEST && OP(s) != O_CH &&
1820 } while (OP(s) != O_QUEST && OP(s) != O_CH);
1821 /* We must skip to the next position, or we'll
1822 * leave altoffset() too early.
1850 return largest+offset;
1854 - computejumps - compute char jumps for BM scan
1855 == static void computejumps(struct parse *p, struct re_guts *g);
1857 * This algorithm assumes g->must exists and is has size greater than
1858 * zero. It's based on the algorithm found on Computer Algorithms by
1861 * A char jump is the number of characters one needs to jump based on
1862 * the value of the character from the text that was mismatched.
1865 computejumps(struct parse *p, struct re_guts *g)
1870 /* Avoid making errors worse */
1874 g->charjump = (int*) malloc((NC + 1) * sizeof(int));
1875 if (g->charjump == NULL) /* Not a fatal error */
1877 /* Adjust for signed chars, if necessary */
1878 g->charjump = &g->charjump[-(CHAR_MIN)];
1880 /* If the character does not exist in the pattern, the jump
1881 * is equal to the number of characters in the pattern.
1883 for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++)
1884 g->charjump[ch] = g->mlen;
1886 /* If the character does exist, compute the jump that would
1887 * take us to the last character in the pattern equal to it
1888 * (notice that we match right to left, so that last character
1889 * is the first one that would be matched).
1891 for (mindex = 0; mindex < g->mlen; mindex++)
1892 g->charjump[g->must[mindex]] = g->mlen - mindex - 1;
1896 - computematchjumps - compute match jumps for BM scan
1897 == static void computematchjumps(struct parse *p, struct re_guts *g);
1899 * This algorithm assumes g->must exists and is has size greater than
1900 * zero. It's based on the algorithm found on Computer Algorithms by
1903 * A match jump is the number of characters one needs to advance based
1904 * on the already-matched suffix.
1905 * Notice that all values here are minus (g->mlen-1), because of the way
1906 * the search algorithm works.
1909 computematchjumps(struct parse *p, struct re_guts *g)
1911 int mindex; /* General "must" iterator */
1912 int suffix; /* Keeps track of matching suffix */
1913 int ssuffix; /* Keeps track of suffixes' suffix */
1914 int* pmatches; /* pmatches[k] points to the next i
1915 * such that i+1...mlen is a substring
1916 * of k+1...k+mlen-i-1
1919 /* Avoid making errors worse */
1923 pmatches = (int*) malloc(g->mlen * sizeof(unsigned int));
1924 if (pmatches == NULL) {
1925 g->matchjump = NULL;
1929 g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int));
1930 if (g->matchjump == NULL) /* Not a fatal error */
1933 /* Set maximum possible jump for each character in the pattern */
1934 for (mindex = 0; mindex < g->mlen; mindex++)
1935 g->matchjump[mindex] = 2*g->mlen - mindex - 1;
1937 /* Compute pmatches[] */
1938 for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0;
1939 mindex--, suffix--) {
1940 pmatches[mindex] = suffix;
1942 /* If a mismatch is found, interrupting the substring,
1943 * compute the matchjump for that position. If no
1944 * mismatch is found, then a text substring mismatched
1945 * against the suffix will also mismatch against the
1948 while (suffix < g->mlen
1949 && g->must[mindex] != g->must[suffix]) {
1950 g->matchjump[suffix] = MIN(g->matchjump[suffix],
1951 g->mlen - mindex - 1);
1952 suffix = pmatches[suffix];
1956 /* Compute the matchjump up to the last substring found to jump
1957 * to the beginning of the largest must pattern prefix matching
1960 for (mindex = 0; mindex <= suffix; mindex++)
1961 g->matchjump[mindex] = MIN(g->matchjump[mindex],
1962 g->mlen + suffix - mindex);
1964 ssuffix = pmatches[suffix];
1965 while (suffix < g->mlen) {
1966 while (suffix <= ssuffix && suffix < g->mlen) {
1967 g->matchjump[suffix] = MIN(g->matchjump[suffix],
1968 g->mlen + ssuffix - suffix);
1971 if (suffix < g->mlen)
1972 ssuffix = pmatches[ssuffix];
1979 - pluscount - count + nesting
1980 == static sopno pluscount(struct parse *p, struct re_guts *g);
1982 static sopno /* nesting depth */
1983 pluscount(struct parse *p, struct re_guts *g)
1991 return(0); /* there may not be an OEND */
1993 scan = g->strip + 1;
2001 if (plusnest > maxnest)
2006 } while (OP(s) != OEND);