1 /* $NetBSD: regcomp.c,v 1.36 2015/09/12 19:08:47 christos Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
38 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
74 #include <sys/cdefs.h>
75 #if defined(LIBC_SCCS) && !defined(lint)
77 static char sccsid
[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
79 __RCSID("$NetBSD: regcomp.c,v 1.36 2015/09/12 19:08:47 christos Exp $");
81 #endif /* LIBC_SCCS and not lint */
83 #include "namespace.h"
84 #include <sys/types.h>
95 __weak_alias(regcomp
,_regcomp
)
105 * parse structure, passed up and down to avoid global variables and
109 const char *next
; /* next character in RE */
110 const char *end
; /* end of string (-> NUL normally) */
111 int error
; /* has an error been seen? */
112 sop
*strip
; /* malloced strip */
113 sopno ssize
; /* malloced strip size (allocated) */
114 sopno slen
; /* malloced strip length (used) */
115 size_t ncsalloc
; /* number of csets allocated */
117 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
118 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
119 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
122 /* ========= begin header generated by ./mkh ========= */
127 /* === regcomp.c === */
128 static void p_ere(struct parse
*p
, int stop
, size_t reclimit
);
129 static void p_ere_exp(struct parse
*p
, size_t reclimit
);
130 static void p_str(struct parse
*p
);
131 static void p_bre(struct parse
*p
, int end1
, int end2
, size_t reclimit
);
132 static int p_simp_re(struct parse
*p
, int starordinary
, size_t reclimit
);
133 static int p_count(struct parse
*p
);
134 static void p_bracket(struct parse
*p
);
135 static void p_b_term(struct parse
*p
, cset
*cs
);
136 static void p_b_cclass(struct parse
*p
, cset
*cs
);
137 static void p_b_eclass(struct parse
*p
, cset
*cs
);
138 static char p_b_symbol(struct parse
*p
);
139 static char p_b_coll_elem(struct parse
*p
, int endc
);
140 static int othercase(int ch
);
141 static void bothcases(struct parse
*p
, int ch
);
142 static void ordinary(struct parse
*p
, int ch
);
143 static void nonnewline(struct parse
*p
);
144 static void repeat(struct parse
*p
, sopno start
, int from
, int to
, size_t reclimit
);
145 static int seterr(struct parse
*p
, int e
);
146 static cset
*allocset(struct parse
*p
);
147 static void freeset(struct parse
*p
, cset
*cs
);
148 static sopno
freezeset(struct parse
*p
, cset
*cs
);
149 static int firstch(struct parse
*p
, cset
*cs
);
150 static int nch(struct parse
*p
, cset
*cs
);
151 static void mcadd(struct parse
*p
, cset
*cs
, const char *cp
);
153 static void mcsub(cset
*cs
, char *cp
);
154 static int mcin(cset
*cs
, char *cp
);
155 static char *mcfind(cset
*cs
, char *cp
);
157 static void mcinvert(struct parse
*p
, cset
*cs
);
158 static void mccase(struct parse
*p
, cset
*cs
);
159 static int isinsets(struct re_guts
*g
, int c
);
160 static int samesets(struct re_guts
*g
, int c1
, int c2
);
161 static void categorize(struct parse
*p
, struct re_guts
*g
);
162 static sopno
dupl(struct parse
*p
, sopno start
, sopno finish
);
163 static void doemit(struct parse
*p
, sop op
, sopno opnd
);
164 static void doinsert(struct parse
*p
, sop op
, sopno opnd
, sopno pos
);
165 static void dofwd(struct parse
*p
, sopno pos
, sopno value
);
166 static int enlarge(struct parse
*p
, sopno size
);
167 static void stripsnug(struct parse
*p
, struct re_guts
*g
);
168 static void findmust(struct parse
*p
, struct re_guts
*g
);
169 static sopno
pluscount(struct parse
*p
, struct re_guts
*g
);
174 /* ========= end header generated by ./mkh ========= */
176 static char nuls
[10]; /* place to point scanner in event of error */
179 * macros for use with parse structure
180 * BEWARE: these know that the parse structure is named `p' !!!
182 #define PEEK() (*p->next)
183 #define PEEK2() (*(p->next+1))
184 #define MORE() (p->next < p->end)
185 #define MORE2() (p->next+1 < p->end)
186 #define SEE(c) (MORE() && PEEK() == (c))
187 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
188 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
189 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
190 #define NEXT() (p->next++)
191 #define NEXT2() (p->next += 2)
192 #define NEXTn(n) (p->next += (n))
193 #define GETNEXT() (*p->next++)
194 #define SETERROR(e) seterr(p, (e))
195 #define REQUIRE(co, e) (void) ((co) || SETERROR(e))
196 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
197 #define MUSTEAT(c, e) (void) (REQUIRE(MORE() && GETNEXT() == (c), e))
198 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
199 #define EMIT(op, sopnd) doemit(p, (sop)(op), sopnd)
200 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
201 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
202 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
203 #define HERE() (p->slen)
204 #define THERE() (p->slen - 1)
205 #define THERETHERE() (p->slen - 2)
206 #define DROP(n) (p->slen -= (n))
209 static int never
= 0; /* for use in asserts; shuts lint up */
211 #define never 0 /* some <assert.h>s have bugs too */
214 #define MEMLIMIT 0x8000000
216 ((p)->ncsalloc / CHAR_BIT * (p)->g->csetsize + \
217 (p)->ncsalloc * sizeof(cset) + \
218 (p)->ssize * sizeof(sop))
222 - regcomp - interface for parser and compilation
223 = extern int regcomp(regex_t *, const char *, int);
224 = #define REG_BASIC 0000
225 = #define REG_EXTENDED 0001
226 = #define REG_ICASE 0002
227 = #define REG_NOSUB 0004
228 = #define REG_NEWLINE 0010
229 = #define REG_NOSPEC 0020
230 = #define REG_PEND 0040
231 = #define REG_DUMP 0200
233 int /* 0 success, otherwise REG_something */
241 struct parse
*p
= &pa
;
245 # define GOODFLAGS(f) (f)
247 # define GOODFLAGS(f) ((f)&~REG_DUMP)
250 _DIAGASSERT(preg
!= NULL
);
251 _DIAGASSERT(pattern
!= NULL
);
253 cflags
= GOODFLAGS(cflags
);
254 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
257 if (cflags
®_PEND
) {
258 if (preg
->re_endp
< pattern
)
260 len
= preg
->re_endp
- pattern
;
262 len
= strlen(pattern
);
264 /* do the mallocs early so failure handling is easy */
265 g
= malloc(sizeof(struct re_guts
) + (NC
- 1) * sizeof(cat_t
));
268 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
269 p
->strip
= calloc(p
->ssize
, sizeof(sop
));
271 if (p
->strip
== NULL
) {
279 p
->end
= p
->next
+ len
;
282 for (i
= 0; i
< NPAREN
; i
++) {
297 g
->ncategories
= 1; /* category 0 is "everything else" */
298 g
->categories
= &g
->catspace
[-(CHAR_MIN
)];
299 (void) memset((char *)g
->catspace
, 0, NC
*sizeof(cat_t
));
304 g
->firststate
= THERE();
305 if (cflags
®_EXTENDED
)
307 else if (cflags
®_NOSPEC
)
310 p_bre(p
, OUT
, OUT
, 0);
312 g
->laststate
= THERE();
314 /* tidy up loose ends and fill things in */
318 g
->nplus
= pluscount(p
, g
);
320 preg
->re_nsub
= g
->nsub
;
322 preg
->re_magic
= MAGIC1
;
324 /* not debugging, so can't rely on the assert() in regexec() */
326 SETERROR(REG_ASSERT
);
329 /* win or lose, we're done */
330 if (p
->error
!= 0) /* lose */
336 - p_ere - ERE parser top level, concatenation and alternation
337 == static void p_ere(struct parse *p, int stop, size_t reclimit);
342 int stop
, /* character this ERE should end at */
346 sopno prevback
= 0; /* pacify gcc */
347 sopno prevfwd
= 0; /* pacify gcc */
349 int first
= 1; /* is this the first alternative? */
351 _DIAGASSERT(p
!= NULL
);
353 if (reclimit
++ > RECLIMIT
|| p
->error
== REG_ESPACE
) {
354 p
->error
= REG_ESPACE
;
359 /* do a bunch of concatenated expressions */
361 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
362 p_ere_exp(p
, reclimit
);
363 REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
366 break; /* NOTE BREAK OUT */
369 INSERT(OCH_
, conc
); /* offset is wrong */
374 ASTERN(OOR1
, prevback
);
376 AHEAD(prevfwd
); /* fix previous offset */
378 EMIT(OOR2
, 0); /* offset is very wrong */
381 if (!first
) { /* tail-end fixups */
383 ASTERN(O_CH
, prevback
);
386 assert(!MORE() || SEE(stop
));
390 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
391 == static void p_ere_exp(struct parse *p, size_t reclimit);
405 _DIAGASSERT(p
!= NULL
);
407 assert(MORE()); /* caller should have ensured this */
413 REQUIRE(MORE(), REG_EPAREN
);
417 p
->pbegin
[subno
] = HERE();
418 EMIT(OLPAREN
, subno
);
420 p_ere(p
, ')', reclimit
);
421 if (subno
< NPAREN
) {
422 p
->pend
[subno
] = HERE();
423 assert(p
->pend
[subno
] != 0);
425 EMIT(ORPAREN
, subno
);
426 MUSTEAT(')', REG_EPAREN
);
428 #ifndef POSIX_MISTAKE
429 case ')': /* happens only if no current unmatched ( */
431 * You may ask, why the ifndef? Because I didn't notice
432 * this until slightly too late for 1003.2, and none of the
433 * other 1003.2 regular-expression reviewers noticed it at
434 * all. So an unmatched ) is legal POSIX, at least until
435 * we can get it fixed.
437 SETERROR(REG_EPAREN
);
442 p
->g
->iflags
|= USEBOL
;
448 p
->g
->iflags
|= USEEOL
;
457 SETERROR(REG_BADRPT
);
460 if (p
->g
->cflags
®_NEWLINE
)
469 REQUIRE(MORE(), REG_EESCAPE
);
473 case '{': /* okay as ordinary except if digit follows */
474 REQUIRE(!MORE() || !isdigit((unsigned char)PEEK()), REG_BADRPT
);
484 /* we call { a repetition if followed by a digit */
485 if (!( c
== '*' || c
== '+' || c
== '?' ||
486 (c
== '{' && MORE2() && isdigit((unsigned char)PEEK2())) ))
487 return; /* no repetition, we're done */
490 REQUIRE(!wascaret
, REG_BADRPT
);
492 case '*': /* implemented as +? */
493 /* this case does not require the (y|) trick, noKLUDGE */
496 INSERT(OQUEST_
, pos
);
497 ASTERN(O_QUEST
, pos
);
504 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
505 INSERT(OCH_
, pos
); /* offset slightly wrong */
506 ASTERN(OOR1
, pos
); /* this one's right */
507 AHEAD(pos
); /* fix the OCH_ */
508 EMIT(OOR2
, 0); /* offset very wrong... */
509 AHEAD(THERE()); /* ...so fix it */
510 ASTERN(O_CH
, THERETHERE());
515 if (isdigit((unsigned char)PEEK())) {
517 REQUIRE(count
<= count2
, REG_BADBR
);
518 } else /* single number with comma */
520 } else /* just a single number */
522 repeat(p
, pos
, count
, count2
, 0);
523 if (!EAT('}')) { /* error heuristics */
524 while (MORE() && PEEK() != '}')
526 REQUIRE(MORE(), REG_EBRACE
);
535 if (!( c
== '*' || c
== '+' || c
== '?' ||
536 (c
== '{' && MORE2() && isdigit((unsigned char)PEEK2())) ) )
538 SETERROR(REG_BADRPT
);
542 - p_str - string (no metacharacters) "parser"
543 == static void p_str(struct parse *p);
550 _DIAGASSERT(p
!= NULL
);
552 REQUIRE(MORE(), REG_EMPTY
);
554 ordinary(p
, GETNEXT());
558 - p_bre - BRE parser top level, anchoring and concatenation
559 == static void p_bre(struct parse *p, int end1, \
560 == int end2, size_t reclimit);
561 * Giving end1 as OUT essentially eliminates the end1/end2 check.
563 * This implementation is a bit of a kludge, in that a trailing $ is first
564 * taken as an ordinary character and then revised to be an anchor. The
565 * only undesirable side effect is that '$' gets included as a character
566 * category in such cases. This is fairly harmless; not worth fixing.
567 * The amount of lookahead needed to avoid this kludge is excessive.
572 int end1
, /* first terminating character */
573 int end2
, /* second terminating character */
577 int first
= 1; /* first subexpression? */
580 _DIAGASSERT(p
!= NULL
);
582 if (reclimit
++ > RECLIMIT
|| p
->error
== REG_ESPACE
) {
583 p
->error
= REG_ESPACE
;
591 p
->g
->iflags
|= USEBOL
;
594 while (MORE() && !SEETWO(end1
, end2
)) {
595 wasdollar
= p_simp_re(p
, first
, reclimit
);
598 if (wasdollar
) { /* oops, that was a trailing anchor */
601 p
->g
->iflags
|= USEEOL
;
605 REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
609 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
610 == static int p_simp_re(struct parse *p, int starordinary, size_t reclimit);
612 static int /* was the simple RE an unbackslashed $? */
615 int starordinary
, /* is a leading * an ordinary character? */
623 # define BACKSL (1<<CHAR_BIT)
625 _DIAGASSERT(p
!= NULL
);
627 pos
= HERE(); /* repetion op, if any, covers from here */
629 assert(MORE()); /* caller should have ensured this */
632 REQUIRE(MORE(), REG_EESCAPE
);
633 c
= BACKSL
| (unsigned char)GETNEXT();
637 if (p
->g
->cflags
®_NEWLINE
)
646 SETERROR(REG_BADRPT
);
652 p
->pbegin
[subno
] = HERE();
653 EMIT(OLPAREN
, subno
);
654 /* the MORE here is an error heuristic */
655 if (MORE() && !SEETWO('\\', ')'))
656 p_bre(p
, '\\', ')', reclimit
);
657 if (subno
< NPAREN
) {
658 p
->pend
[subno
] = HERE();
659 assert(p
->pend
[subno
] != 0);
661 EMIT(ORPAREN
, subno
);
662 REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
664 case BACKSL
|')': /* should not get here -- must be user */
666 SETERROR(REG_EPAREN
);
677 i
= (c
&~BACKSL
) - '0';
679 if (p
->pend
[i
] != 0) {
680 assert(i
<= p
->g
->nsub
);
682 assert(p
->pbegin
[i
] != 0);
683 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
684 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
685 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
688 SETERROR(REG_ESUBREG
);
692 REQUIRE(starordinary
, REG_BADRPT
);
695 ordinary(p
, c
&~ BACKSL
);
699 if (EAT('*')) { /* implemented as +? */
700 /* this case does not require the (y|) trick, noKLUDGE */
703 INSERT(OQUEST_
, pos
);
704 ASTERN(O_QUEST
, pos
);
705 } else if (EATTWO('\\', '{')) {
708 if (MORE() && isdigit((unsigned char)PEEK())) {
710 REQUIRE(count
<= count2
, REG_BADBR
);
711 } else /* single number with comma */
713 } else /* just a single number */
715 repeat(p
, pos
, count
, count2
, 0);
716 if (!EATTWO('\\', '}')) { /* error heuristics */
717 while (MORE() && !SEETWO('\\', '}'))
719 REQUIRE(MORE(), REG_EBRACE
);
722 } else if (c
== (unsigned char)'$') /* $ (but not \$) ends it */
729 - p_count - parse a repetition count
730 == static int p_count(struct parse *p);
732 static int /* the value */
739 _DIAGASSERT(p
!= NULL
);
741 while (MORE() && isdigit((unsigned char)PEEK()) && count
<= DUPMAX
) {
742 count
= count
*10 + (GETNEXT() - '0');
746 REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
751 - p_bracket - parse a bracketed character list
752 == static void p_bracket(struct parse *p);
754 * Note a significant property of this code: if the allocset() did SETERROR,
755 * no set operations are done.
763 _DIAGASSERT(p
!= NULL
);
769 /* Dept of Truly Sickening Special-Case Kludges */
770 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:<:]]",
776 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:>:]]",
784 invert
++; /* make note to invert set at end */
789 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
793 MUSTEAT(']', REG_EBRACK
);
795 if (p
->error
!= 0) /* don't mess things up further */
798 if (p
->g
->cflags
®_ICASE
) {
802 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
803 if (CHIN(cs
, i
) && isalpha(i
)) {
804 ci
= othercase((int)i
);
808 if (cs
->multis
!= NULL
)
814 for (i
= p
->g
->csetsize
- 1; i
>= 0; i
--)
819 if (p
->g
->cflags
®_NEWLINE
)
821 if (cs
->multis
!= NULL
)
825 assert(cs
->multis
== NULL
); /* xxx */
827 if (nch(p
, cs
) == 1) { /* optimize singleton sets */
828 ordinary(p
, firstch(p
, cs
));
831 EMIT(OANYOF
, freezeset(p
, cs
));
835 - p_b_term - parse one term of a bracketed character list
836 == static void p_b_term(struct parse *p, cset *cs);
847 _DIAGASSERT(p
!= NULL
);
848 _DIAGASSERT(cs
!= NULL
);
850 /* classify what we've got */
851 switch ((MORE()) ? PEEK() : '\0') {
853 c
= (MORE2()) ? PEEK2() : '\0';
857 SETERROR(REG_ERANGE
);
858 return; /* NOTE RETURN */
866 case ':': /* character class */
868 REQUIRE(MORE(), REG_EBRACK
);
870 REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
872 REQUIRE(MORE(), REG_EBRACK
);
873 REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
875 case '=': /* equivalence class */
877 REQUIRE(MORE(), REG_EBRACK
);
879 REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
881 REQUIRE(MORE(), REG_EBRACK
);
882 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
884 default: /* symbol, ordinary character, or range */
885 /* xxx revision needed for multichar stuff */
886 start
= p_b_symbol(p
);
887 if (SEE('-') && MORE2() && PEEK2() != ']') {
893 finish
= p_b_symbol(p
);
896 /* xxx what about signed chars here... */
897 REQUIRE(start
<= finish
, REG_ERANGE
);
898 for (i
= start
; i
<= finish
; i
++)
905 - p_b_cclass - parse a character-class name and deal with it
906 == static void p_b_cclass(struct parse *p, cset *cs);
914 const struct cclass
*cp
;
919 _DIAGASSERT(p
!= NULL
);
920 _DIAGASSERT(cs
!= NULL
);
924 while (MORE() && isalpha((unsigned char)PEEK()))
927 for (cp
= cclasses
; cp
->name
!= NULL
; cp
++)
928 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
930 if (cp
->name
== NULL
) {
931 /* oops, didn't find it */
932 SETERROR(REG_ECTYPE
);
937 while ((c
= *u
++) != '\0')
939 for (u
= cp
->multis
; *u
!= '\0'; u
+= strlen(u
) + 1)
944 - p_b_eclass - parse an equivalence-class name and deal with it
945 == static void p_b_eclass(struct parse *p, cset *cs);
947 * This implementation is incomplete. xxx
956 _DIAGASSERT(p
!= NULL
);
957 _DIAGASSERT(cs
!= NULL
);
959 c
= p_b_coll_elem(p
, '=');
964 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
965 == static char p_b_symbol(struct parse *p);
967 static char /* value of symbol */
973 _DIAGASSERT(p
!= NULL
);
975 REQUIRE(MORE(), REG_EBRACK
);
976 if (!EATTWO('[', '.'))
979 /* collating symbol */
980 value
= p_b_coll_elem(p
, '.');
981 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
986 - p_b_coll_elem - parse a collating-element name and look it up
987 == static char p_b_coll_elem(struct parse *p, int endc);
989 static char /* value of collating element */
992 int endc
) /* name ended by endc,']' */
995 const struct cname
*cp
;
998 _DIAGASSERT(p
!= NULL
);
1002 while (MORE() && !SEETWO(endc
, ']'))
1005 SETERROR(REG_EBRACK
);
1009 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
1010 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
1011 return(cp
->code
); /* known name */
1013 return(*sp
); /* single character */
1014 SETERROR(REG_ECOLLATE
); /* neither */
1019 - othercase - return the case counterpart of an alphabetic
1020 == static int othercase(int ch);
1022 static int /* 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(struct parse *p, int ch);
1039 * Boy, is this implementation ever a kludge...
1046 const char *oldnext
;
1050 _DIAGASSERT(p
!= NULL
);
1055 assert(othercase(ch
) != ch
); /* p_bracket() would recurse */
1062 assert(p
->next
== bracket
+2);
1068 - ordinary - emit an ordinary character
1069 == static void ordinary(struct parse *p, int ch);
1077 unsigned char uc
= (unsigned char)ch
;
1079 _DIAGASSERT(p
!= NULL
);
1081 cap
= p
->g
->categories
;
1082 if ((p
->g
->cflags
& REG_ICASE
) && isalpha(uc
) && othercase(uc
) != uc
)
1085 EMIT(OCHAR
, (sopno
)uc
);
1087 _DIAGASSERT(__type_fit(unsigned char,
1088 p
->g
->ncategories
+ 1));
1089 cap
[uc
] = (unsigned char)p
->g
->ncategories
++;
1095 - nonnewline - emit REG_NEWLINE version of OANY
1096 == static void nonnewline(struct parse *p);
1098 * Boy, is this implementation ever a kludge...
1104 const char *oldnext
;
1108 _DIAGASSERT(p
!= NULL
);
1120 assert(p
->next
== bracket
+3);
1126 - repeat - generate code for a bounded repetition, recursively if needed
1127 == static void repeat(struct parse *p, sopno start, int from, int to,
1128 == size_t reclimit);
1133 sopno start
, /* operand from here to end of strip */
1134 int from
, /* repeated from this number */
1135 int to
, /* to this number of times (maybe INFINITY) */
1141 # define REP(f, t) ((f)*8 + (t))
1142 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1145 _DIAGASSERT(p
!= NULL
);
1147 if (reclimit
++ > RECLIMIT
)
1148 p
->error
= REG_ESPACE
;
1156 switch (REP(MAP(from
), MAP(to
))) {
1157 case REP(0, 0): /* must be user doing this */
1158 DROP(finish
-start
); /* drop the operand */
1160 case REP(0, 1): /* as x{1,1}? */
1161 case REP(0, N
): /* as x{1,n}? */
1162 case REP(0, INF
): /* as x{1,}? */
1163 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1164 INSERT(OCH_
, start
); /* offset is wrong... */
1165 repeat(p
, start
+1, 1, to
, reclimit
);
1166 ASTERN(OOR1
, start
);
1167 AHEAD(start
); /* ... fix it */
1170 ASTERN(O_CH
, THERETHERE());
1172 case REP(1, 1): /* trivial case */
1175 case REP(1, N
): /* as x?x{1,n-1} */
1176 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1177 INSERT(OCH_
, start
);
1178 ASTERN(OOR1
, start
);
1180 EMIT(OOR2
, 0); /* offset very wrong... */
1181 AHEAD(THERE()); /* ...so fix it */
1182 ASTERN(O_CH
, THERETHERE());
1183 copy
= dupl(p
, start
+1, finish
+1);
1184 assert(copy
== finish
+4);
1185 repeat(p
, copy
, 1, to
-1, reclimit
);
1187 case REP(1, INF
): /* as x+ */
1188 INSERT(OPLUS_
, start
);
1189 ASTERN(O_PLUS
, start
);
1191 case REP(N
, N
): /* as xx{m-1,n-1} */
1192 copy
= dupl(p
, start
, finish
);
1193 repeat(p
, copy
, from
-1, to
-1, reclimit
);
1195 case REP(N
, INF
): /* as xx{n-1,INF} */
1196 copy
= dupl(p
, start
, finish
);
1197 repeat(p
, copy
, from
-1, to
, reclimit
);
1199 default: /* "can't happen" */
1200 SETERROR(REG_ASSERT
); /* just in case */
1206 - seterr - set an error condition
1207 == static int seterr(struct parse *p, int e);
1209 static int /* useless but makes type checking happy */
1215 _DIAGASSERT(p
!= NULL
);
1217 if (p
->error
== 0) /* keep earliest error condition */
1219 p
->next
= nuls
; /* try to bring things to a halt */
1221 return(0); /* make the return value well-defined */
1225 - allocset - allocate a set of characters for []
1226 == static cset *allocset(struct parse *p);
1240 _DIAGASSERT(p
!= NULL
);
1242 no
= p
->g
->ncsets
++;
1243 css
= (size_t)p
->g
->csetsize
;
1244 if (no
>= p
->ncsalloc
) { /* need another column of space */
1245 p
->ncsalloc
+= CHAR_BIT
;
1247 assert(nc
% CHAR_BIT
== 0);
1248 nbytes
= nc
/ CHAR_BIT
* css
;
1249 if (MEMSIZE(p
) > MEMLIMIT
)
1251 if (reallocarr(&p
->g
->sets
, nc
, sizeof(cset
)))
1253 old_ptr
= p
->g
->setbits
;
1254 if (reallocarr(&p
->g
->setbits
, nc
/ CHAR_BIT
, css
)) {
1258 if (old_ptr
!= p
->g
->setbits
) {
1259 for (i
= 0; i
< no
; i
++)
1260 p
->g
->sets
[i
].ptr
= p
->g
->setbits
+ css
*(i
/CHAR_BIT
);
1262 (void) memset((char *)p
->g
->setbits
+ (nbytes
- css
), 0, css
);
1265 cs
= &p
->g
->sets
[no
];
1266 cs
->ptr
= p
->g
->setbits
+ css
*((no
)/CHAR_BIT
);
1267 cs
->mask
= 1 << (unsigned int)((no
) % CHAR_BIT
);
1275 SETERROR(REG_ESPACE
);
1276 /* caller's responsibility not to do set ops */
1281 - freeset - free a now-unused set
1282 == static void freeset(struct parse *p, cset *cs);
1293 _DIAGASSERT(p
!= NULL
);
1294 _DIAGASSERT(cs
!= NULL
);
1296 top
= &p
->g
->sets
[p
->g
->ncsets
];
1297 css
= (size_t)p
->g
->csetsize
;
1299 for (i
= 0; i
< css
; i
++)
1301 if (cs
== top
-1) /* recover only the easy case */
1306 - freezeset - final processing on a set of characters
1307 == static int freezeset(struct parse *p, cset *cs);
1309 * The main task here is merging identical sets. This is usually a waste
1310 * of time (although the hash code minimizes the overhead), but can win
1311 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1312 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1315 static sopno
/* set number */
1326 _DIAGASSERT(p
!= NULL
);
1327 _DIAGASSERT(cs
!= NULL
);
1330 top
= &p
->g
->sets
[p
->g
->ncsets
];
1331 css
= (size_t)p
->g
->csetsize
;
1333 /* look for an earlier one which is the same */
1334 for (cs2
= &p
->g
->sets
[0]; cs2
< top
; cs2
++)
1335 if (cs2
->hash
== h
&& cs2
!= cs
) {
1337 for (i
= 0; i
< css
; i
++)
1338 if (!!CHIN(cs2
, i
) != !!CHIN(cs
, i
))
1344 if (cs2
< top
) { /* found one */
1349 return (sopno
)(cs
- p
->g
->sets
);
1353 - firstch - return first character in a set (which must have at least one)
1354 == static int firstch(struct parse *p, cset *cs);
1356 static int /* character; there is no "none" value */
1364 _DIAGASSERT(p
!= NULL
);
1365 _DIAGASSERT(cs
!= NULL
);
1367 css
= (size_t)p
->g
->csetsize
;
1369 for (i
= 0; i
< css
; i
++)
1373 return(0); /* arbitrary */
1377 - nch - number of characters in a set
1378 == static int nch(struct parse *p, cset *cs);
1389 _DIAGASSERT(p
!= NULL
);
1390 _DIAGASSERT(cs
!= NULL
);
1392 css
= (size_t)p
->g
->csetsize
;
1394 for (i
= 0; i
< css
; i
++)
1401 - mcadd - add a collating element to a cset
1402 == static void mcadd(struct parse *p, cset *cs, \
1413 _DIAGASSERT(p
!= NULL
);
1414 _DIAGASSERT(cs
!= NULL
);
1415 _DIAGASSERT(cp
!= NULL
);
1417 oldend
= cs
->smultis
;
1419 cs
->smultis
+= strlen(cp
) + 1;
1420 if (cs
->multis
== NULL
)
1421 cs
->multis
= malloc(cs
->smultis
);
1423 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1424 if (cs
->multis
== NULL
) {
1425 SETERROR(REG_ESPACE
);
1429 (void) strcpy(cs
->multis
+ oldend
- 1, cp
);
1430 cs
->multis
[cs
->smultis
- 1] = '\0';
1435 - mcsub - subtract a collating element from a cset
1436 == static void mcsub(cset *cs, char *cp);
1446 _DIAGASSERT(cs
!= NULL
);
1447 _DIAGASSERT(cp
!= NULL
);
1449 fp
= mcfind(cs
, cp
);
1453 (void) memmove(fp
, fp
+ len
+ 1,
1454 cs
->smultis
- (fp
+ len
+ 1 - cs
->multis
));
1457 if (cs
->smultis
== 0) {
1463 cs
->multis
= realloc(cs
->multis
, cs
->smultis
);
1464 assert(cs
->multis
!= NULL
);
1468 - mcin - is a collating element in a cset?
1469 == static int mcin(cset *cs, char *cp);
1477 _DIAGASSERT(cs
!= NULL
);
1478 _DIAGASSERT(cp
!= NULL
);
1480 return(mcfind(cs
, cp
) != NULL
);
1484 - mcfind - find a collating element in a cset
1485 == static char *mcfind(cset *cs, char *cp);
1494 _DIAGASSERT(cs
!= NULL
);
1495 _DIAGASSERT(cp
!= NULL
);
1497 if (cs
->multis
== NULL
)
1499 for (p
= cs
->multis
; *p
!= '\0'; p
+= strlen(p
) + 1)
1500 if (strcmp(cp
, p
) == 0)
1507 - mcinvert - invert the list of collating elements in a cset
1508 == static void mcinvert(struct parse *p, cset *cs);
1510 * This would have to know the set of possibilities. Implementation
1520 _DIAGASSERT(p
!= NULL
);
1521 _DIAGASSERT(cs
!= NULL
);
1523 assert(cs
->multis
== NULL
); /* xxx */
1527 - mccase - add case counterparts of the list of collating elements in a cset
1528 == static void mccase(struct parse *p, cset *cs);
1530 * This would have to know the set of possibilities. Implementation
1540 _DIAGASSERT(p
!= NULL
);
1541 _DIAGASSERT(cs
!= NULL
);
1543 assert(cs
->multis
== NULL
); /* xxx */
1547 - isinsets - is this character in any sets?
1548 == static int isinsets(struct re_guts *g, int c);
1550 static int /* predicate */
1558 unsigned uc
= (unsigned char)c
;
1560 _DIAGASSERT(g
!= NULL
);
1562 if (g
->setbits
== NULL
)
1565 ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1567 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1574 - samesets - are these two characters in exactly the same sets?
1575 == static int samesets(struct re_guts *g, int c1, int c2);
1577 static int /* predicate */
1586 unsigned uc1
= (unsigned char)c1
;
1587 unsigned uc2
= (unsigned char)c2
;
1589 _DIAGASSERT(g
!= NULL
);
1591 ncols
= (g
->ncsets
+(CHAR_BIT
-1)) / CHAR_BIT
;
1593 for (i
= 0, col
= g
->setbits
; i
< ncols
; i
++, col
+= g
->csetsize
)
1594 if (col
[uc1
] != col
[uc2
])
1600 - categorize - sort out character categories
1601 == static void categorize(struct parse *p, struct re_guts *g);
1613 _DIAGASSERT(p
!= NULL
);
1614 _DIAGASSERT(g
!= NULL
);
1616 cats
= g
->categories
;
1618 /* avoid making error situations worse */
1622 for (c
= CHAR_MIN
; c
<= CHAR_MAX
; c
++)
1623 if (cats
[c
] == 0 && isinsets(g
, c
)) {
1624 _DIAGASSERT(__type_fit(unsigned char,
1625 g
->ncategories
+ 1));
1626 cat
= g
->ncategories
++;
1628 for (c2
= c
+1; c2
<= CHAR_MAX
; c2
++)
1629 if (cats
[c2
] == 0 && samesets(g
, c
, c2
))
1635 - dupl - emit a duplicate of a bunch of sops
1636 == static sopno dupl(struct parse *p, sopno start, sopno finish);
1638 static sopno
/* start of duplicate */
1641 sopno start
, /* from here */
1642 sopno finish
) /* to this less one */
1645 sopno len
= finish
- start
;
1647 _DIAGASSERT(p
!= NULL
);
1651 assert(finish
>= start
);
1654 if (!enlarge(p
, p
->ssize
+ len
))/* this many unexpected additions */
1656 (void)memcpy(p
->strip
+ p
->slen
, p
->strip
+ start
,
1657 (size_t)len
* sizeof(sop
));
1663 - doemit - emit a strip operator
1664 == static void doemit(struct parse *p, sop op, size_t opnd);
1666 * It might seem better to implement this as a macro with a function as
1667 * hard-case backup, but it's just too big and messy unless there are
1668 * some changes to the data structures. Maybe later.
1676 _DIAGASSERT(p
!= NULL
);
1678 /* avoid making error situations worse */
1682 /* deal with oversize operands ("can't happen", more or less) */
1683 assert(opnd
< 1<<OPSHIFT
);
1685 /* deal with undersized strip */
1686 if (p
->slen
>= p
->ssize
)
1687 if (!enlarge(p
, (p
->ssize
+1) / 2 * 3)) /* +50% */
1690 /* finally, it's all reduced to the easy case */
1691 p
->strip
[p
->slen
++] = (sop
)SOP(op
, opnd
);
1695 - doinsert - insert a sop into the strip
1696 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
1709 _DIAGASSERT(p
!= NULL
);
1711 /* avoid making error situations worse */
1716 EMIT(op
, opnd
); /* do checks, ensure space */
1717 assert(HERE() == sn
+1);
1720 /* adjust paren pointers */
1722 for (i
= 1; i
< NPAREN
; i
++) {
1723 if (p
->pbegin
[i
] >= pos
) {
1726 if (p
->pend
[i
] >= pos
) {
1731 memmove(&p
->strip
[pos
+1], &p
->strip
[pos
], (HERE()-pos
-1)*sizeof(sop
));
1736 - dofwd - complete a forward reference
1737 == static void dofwd(struct parse *p, sopno pos, sop value);
1746 _DIAGASSERT(p
!= NULL
);
1748 /* avoid making error situations worse */
1752 assert(value
< 1<<OPSHIFT
);
1753 p
->strip
[pos
] = (sop
)(OP(p
->strip
[pos
]) | value
);
1757 - enlarge - enlarge the strip
1758 == static void enlarge(struct parse *p, sopno size);
1761 enlarge(struct parse
*p
, sopno size
)
1763 _DIAGASSERT(p
!= NULL
);
1765 if (p
->ssize
>= size
)
1768 if (MEMSIZE(p
) > MEMLIMIT
|| reallocarr(&p
->strip
, size
, sizeof(sop
))) {
1769 SETERROR(REG_ESPACE
);
1777 - stripsnug - compact the strip
1778 == static void stripsnug(struct parse *p, struct re_guts *g);
1786 _DIAGASSERT(p
!= NULL
);
1787 _DIAGASSERT(g
!= NULL
);
1789 g
->nstates
= p
->slen
;
1790 g
->strip
= p
->strip
;
1791 reallocarr(&g
->strip
, p
->slen
, sizeof(sop
));
1792 /* Ignore error as tries to free memory only. */
1796 - findmust - fill in must and mlen with longest mandatory literal string
1797 == static void findmust(struct parse *p, struct re_guts *g);
1799 * This algorithm could do fancy things like analyzing the operands of |
1800 * for common subsequences. Someday. This code is simple and finds most
1801 * of the interesting cases.
1803 * Note that must and mlen got initialized during setup.
1812 sop
*newstart
= NULL
;
1818 _DIAGASSERT(p
!= NULL
);
1819 _DIAGASSERT(g
!= NULL
);
1821 /* avoid making error situations worse */
1825 /* find the longest OCHAR sequence in strip */
1827 scan
= g
->strip
+ 1;
1831 case OCHAR
: /* sequence member */
1832 if (newlen
== 0) /* new sequence */
1833 newstart
= scan
- 1;
1836 case OPLUS_
: /* things that don't break one */
1840 case OQUEST_
: /* things that must be skipped */
1846 /* assert() interferes w debug printouts */
1847 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1852 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1854 default: /* things that break a sequence */
1855 if (newlen
> g
->mlen
) { /* ends one */
1862 } while (OP(s
) != OEND
);
1867 if (g
->mlen
== 0) /* there isn't one */
1870 /* turn it into a character string */
1871 g
->must
= malloc((size_t)g
->mlen
+ 1);
1872 if (g
->must
== NULL
) { /* argh; just forget it */
1878 for (i
= g
->mlen
; i
> 0; i
--) {
1879 while (OP(s
= *scan
++) != OCHAR
)
1881 assert(cp
< g
->must
+ g
->mlen
);
1882 *cp
++ = (char)OPND(s
);
1884 assert(cp
== g
->must
+ g
->mlen
);
1885 *cp
++ = '\0'; /* just on general principles */
1889 - pluscount - count + nesting
1890 == static sopno pluscount(struct parse *p, struct re_guts *g);
1892 static sopno
/* nesting depth */
1902 _DIAGASSERT(p
!= NULL
);
1903 _DIAGASSERT(g
!= NULL
);
1906 return(0); /* there may not be an OEND */
1908 scan
= g
->strip
+ 1;
1916 if (plusnest
> maxnest
)
1921 } while (OP(s
) != OEND
);