]>
git.saurik.com Git - apple/libc.git/blob - regex/regcomp-fbsd.c
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
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid
[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
42 #endif /* LIBC_SCCS and not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD: src/lib/libc/regex/regcomp.c,v 1.34 2004/10/03 15:42:59 stefanf Exp $");
46 #include "xlocale_private.h"
48 #include <sys/types.h>
67 * parse structure, passed up and down to avoid global variables and
71 char *next
; /* next character in RE */
72 char *end
; /* end of string (-> NUL normally) */
73 int error
; /* has an error been seen? */
74 sop
*strip
; /* malloced strip */
75 sopno ssize
; /* malloced strip size (allocated) */
76 sopno slen
; /* malloced strip length (used) */
77 int ncsalloc
; /* number of csets allocated */
80 #endif /* __DARWIN_UNIX03 */
82 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
83 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
84 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
87 /* ========= begin header generated by ./mkh ========= */
92 /* === regcomp.c === */
93 static void p_ere(struct parse
*p
, wint_t stop
);
94 static void p_ere_exp(struct parse
*p
);
95 static void p_str(struct parse
*p
);
96 static void p_bre(struct parse
*p
, wint_t end1
, wint_t end2
);
97 static int p_simp_re(struct parse
*p
, int starordinary
);
98 static int p_count(struct parse
*p
);
99 static void p_bracket(struct parse
*p
);
100 static void p_b_term(struct parse
*p
, cset
*cs
);
101 static void p_b_cclass(struct parse
*p
, cset
*cs
);
102 static void p_b_eclass(struct parse
*p
, cset
*cs
);
103 static wint_t p_b_symbol(struct parse
*p
);
104 static wint_t p_b_coll_elem(struct parse
*p
, wint_t endc
);
105 static wint_t othercase(wint_t ch
, locale_t loc
);
106 static void bothcases(struct parse
*p
, wint_t ch
);
107 static void ordinary(struct parse
*p
, wint_t ch
);
108 static void nonnewline(struct parse
*p
);
109 static void repeat(struct parse
*p
, sopno start
, int from
, int to
);
110 static int seterr(struct parse
*p
, int e
);
111 static cset
*allocset(struct parse
*p
);
112 static void freeset(struct parse
*p
, cset
*cs
);
113 static void CHadd(struct parse
*p
, cset
*cs
, wint_t ch
);
114 static void CHaddrange(struct parse
*p
, cset
*cs
, wint_t min
, wint_t max
);
115 static void CHaddtype(struct parse
*p
, cset
*cs
, wctype_t wct
);
116 static wint_t singleton(cset
*cs
, locale_t loc
);
117 static sopno
dupl(struct parse
*p
, sopno start
, sopno finish
);
118 static void doemit(struct parse
*p
, sop op
, size_t opnd
);
119 static void doinsert(struct parse
*p
, sop op
, size_t opnd
, sopno pos
);
120 static void dofwd(struct parse
*p
, sopno pos
, sop value
);
121 static void enlarge(struct parse
*p
, sopno size
);
122 static void stripsnug(struct parse
*p
, struct re_guts
*g
);
123 static void findmust(struct parse
*p
, struct re_guts
*g
);
124 static int altoffset(sop
*scan
, int offset
);
125 static void computejumps(struct parse
*p
, struct re_guts
*g
);
126 static void computematchjumps(struct parse
*p
, struct re_guts
*g
);
127 static sopno
pluscount(struct parse
*p
, struct re_guts
*g
);
128 static wint_t wgetnext(struct parse
*p
);
133 /* ========= end header generated by ./mkh ========= */
135 static char nuls
[10]; /* place to point scanner in event of error */
138 * macros for use with parse structure
139 * BEWARE: these know that the parse structure is named `p' !!!
141 #define PEEK() (*p->next)
142 #define PEEK2() (*(p->next+1))
143 #define MORE() (p->next < p->end)
144 #define MORE2() (p->next+1 < p->end)
145 #define SEE(c) (MORE() && PEEK() == (c))
146 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
147 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
148 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
149 #define NEXT() (p->next++)
150 #define NEXT2() (p->next += 2)
151 #define NEXTn(n) (p->next += (n))
152 #define GETNEXT() (*p->next++)
153 #define WGETNEXT() wgetnext(p)
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(preg
, pattern
, cflags
)
191 regex_t
* __restrict preg
;
192 const char * __restrict pattern
;
197 struct parse
*p
= &pa
;
201 # define GOODFLAGS(f) (f)
203 # define GOODFLAGS(f) ((f)&~REG_DUMP)
206 cflags
= GOODFLAGS(cflags
);
207 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
210 if (cflags
®_PEND
) {
211 if (preg
->re_endp
< pattern
)
213 len
= preg
->re_endp
- pattern
;
215 len
= strlen((char *)pattern
);
217 /* do the mallocs early so failure handling is easy */
218 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
));
221 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
222 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
224 if (p
->strip
== NULL
) {
231 p
->next
= (char *)pattern
; /* convenience; we do not modify it */
232 p
->end
= p
->next
+ len
;
237 #endif /* __DARWIN_UNIX03 */
238 for (i
= 0; i
< NPAREN
; i
++) {
242 g
->loc
= __current_locale();
259 g
->firststate
= THERE();
260 if (cflags
®_EXTENDED
)
262 else if (cflags
®_NOSPEC
)
267 g
->laststate
= THERE();
269 /* 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);
307 int stop
; /* character this ERE should end at */
313 int first
= 1; /* is this the first alternative? */
316 /* do a bunch of concatenated expressions */
318 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
321 if (!p
->zerorepeats
) REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
322 else p
->zerorepeats
--;
324 (void)REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
327 break; /* NOTE BREAK OUT */
330 INSERT(OCH_
, conc
); /* offset is wrong */
335 ASTERN(OOR1
, prevback
);
337 AHEAD(prevfwd
); /* fix previous offset */
339 EMIT(OOR2
, 0); /* offset is very wrong */
342 if (!first
) { /* tail-end fixups */
344 ASTERN(O_CH
, prevback
);
347 assert(!MORE() || SEE(stop
));
351 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
352 == static void p_ere_exp(struct parse *p);
366 assert(MORE()); /* caller should have ensured this */
372 (void)REQUIRE(MORE(), REG_EPAREN
);
376 p
->pbegin
[subno
] = HERE();
377 EMIT(OLPAREN
, subno
);
380 if (subno
< NPAREN
) {
381 p
->pend
[subno
] = HERE();
382 assert(p
->pend
[subno
] != 0);
384 EMIT(ORPAREN
, subno
);
385 (void)MUSTEAT(')', REG_EPAREN
);
387 #ifndef POSIX_MISTAKE
388 case ')': /* happens only if no current unmatched ( */
390 * You may ask, why the ifndef? Because I didn't notice
391 * this until slightly too late for 1003.2, and none of the
392 * other 1003.2 regular-expression reviewers noticed it at
393 * all. So an unmatched ) is legal POSIX, at least until
394 * we can get it fixed.
396 SETERROR(REG_EPAREN
);
401 p
->g
->iflags
|= USEBOL
;
407 p
->g
->iflags
|= USEEOL
;
416 SETERROR(REG_BADRPT
);
419 if (p
->g
->cflags
®_NEWLINE
)
428 (void)REQUIRE(MORE(), REG_EESCAPE
);
432 case '{': /* okay as ordinary except if digit follows */
433 (void)REQUIRE(!MORE() || !isdigit_l((uch
)PEEK(), p
->g
->loc
), REG_BADRPT
);
445 /* we call { a repetition if followed by a digit */
446 if (!( c
== '*' || c
== '+' || c
== '?' ||
447 (c
== '{' && MORE2() && isdigit_l((uch
)PEEK2(), p
->g
->loc
)) ))
448 return; /* no repetition, we're done */
451 (void)REQUIRE(!wascaret
, REG_BADRPT
);
453 case '*': /* implemented as +? */
454 /* this case does not require the (y|) trick, noKLUDGE */
457 INSERT(OQUEST_
, pos
);
458 ASTERN(O_QUEST
, pos
);
465 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
466 INSERT(OCH_
, pos
); /* offset slightly wrong */
467 ASTERN(OOR1
, pos
); /* this one's right */
468 AHEAD(pos
); /* fix the OCH_ */
469 EMIT(OOR2
, 0); /* offset very wrong... */
470 AHEAD(THERE()); /* ...so fix it */
471 ASTERN(O_CH
, THERETHERE());
476 if (isdigit_l((uch
)PEEK(), p
->g
->loc
)) {
478 (void)REQUIRE(count
<= count2
, REG_BADBR
);
479 } else /* single number with comma */
481 } else /* just a single number */
483 repeat(p
, pos
, count
, count2
);
484 if (!EAT('}')) { /* error heuristics */
485 while (MORE() && PEEK() != '}')
487 (void)REQUIRE(MORE(), REG_EBRACE
);
496 if (!( c
== '*' || c
== '+' || c
== '?' ||
497 (c
== '{' && MORE2() && isdigit_l((uch
)PEEK2(), p
->g
->loc
)) ) )
499 SETERROR(REG_BADRPT
);
503 - p_str - string (no metacharacters) "parser"
504 == static void p_str(struct parse *p);
511 if (!p
->zerorepeats
) REQUIRE(MORE(), REG_EMPTY
);
512 else p
->zerorepeats
--;
513 #else /* !__DARWIN_UNIX03 */
514 (void)REQUIRE(MORE(), REG_EMPTY
);
515 #endif /* __DARWIN_UNIX03 */
517 ordinary(p
, WGETNEXT());
521 - p_bre - BRE parser top level, anchoring and concatenation
522 == static void p_bre(struct parse *p, int end1, \
524 * Giving end1 as OUT essentially eliminates the end1/end2 check.
526 * This implementation is a bit of a kludge, in that a trailing $ is first
527 * taken as an ordinary character and then revised to be an anchor.
528 * The amount of lookahead needed to avoid this kludge is excessive.
533 int end1
; /* first terminating character */
534 int end2
; /* second terminating character */
536 sopno start
= HERE();
537 int first
= 1; /* first subexpression? */
542 p
->g
->iflags
|= USEBOL
;
545 while (MORE() && !SEETWO(end1
, end2
)) {
546 wasdollar
= p_simp_re(p
, first
);
549 if (wasdollar
) { /* oops, that was a trailing anchor */
552 p
->g
->iflags
|= USEEOL
;
556 if (!p
->zerorepeats
) REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
557 else p
->zerorepeats
--;
558 #else /* !__DARWIN_UNIX03 */
559 (void)REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
560 #endif /* __DARWIN_UNIX03 */
564 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
565 == static int p_simp_re(struct parse *p, int starordinary);
567 static int /* was the simple RE an unbackslashed $? */
568 p_simp_re(p
, starordinary
)
570 int starordinary
; /* is a leading * an ordinary character? */
579 # define BACKSL (1<<CHAR_BIT)
581 pos
= HERE(); /* repetion op, if any, covers from here */
583 assert(MORE()); /* caller should have ensured this */
586 (void)REQUIRE(MORE(), REG_EESCAPE
);
587 c
= BACKSL
| GETNEXT();
591 if (p
->g
->cflags
®_NEWLINE
)
600 SETERROR(REG_BADRPT
);
606 p
->pbegin
[subno
] = HERE();
607 EMIT(OLPAREN
, subno
);
608 /* the MORE here is an error heuristic */
609 if (MORE() && !SEETWO('\\', ')'))
611 if (subno
< NPAREN
) {
612 p
->pend
[subno
] = HERE();
613 assert(p
->pend
[subno
] != 0);
615 EMIT(ORPAREN
, subno
);
616 (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
618 case BACKSL
|')': /* should not get here -- must be user */
620 SETERROR(REG_EPAREN
);
631 i
= (c
&~BACKSL
) - '0';
633 if (p
->pend
[i
] != 0) {
636 #endif /* __DARWIN_UNIX03 */
637 assert(i
<= p
->g
->nsub
);
639 assert(p
->pbegin
[i
] != 0);
640 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
641 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
643 if (OP(p
->strip
[p
->pbegin
[i
]+skip
]) == OBOL
) {
644 skip
++; /* don't dup anchor in subexp */
646 (void) dupl(p
, p
->pbegin
[i
]+skip
, p
->pend
[i
]);
647 #else /* !__DARWIN_UNIX03 */
648 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
649 #endif /* __DARWIN_UNIX03 */
652 SETERROR(REG_ESUBREG
);
656 (void)REQUIRE(starordinary
, REG_BADRPT
);
665 if (EAT('*')) { /* implemented as +? */
666 /* this case does not require the (y|) trick, noKLUDGE */
669 INSERT(OQUEST_
, pos
);
670 ASTERN(O_QUEST
, pos
);
671 } else if (EATTWO('\\', '{')) {
672 (void)REQUIRE(MORE(), REG_EBRACE
);
675 if (MORE() && isdigit_l((uch
)PEEK(), p
->g
->loc
)) {
677 (void)REQUIRE(count
<= count2
, REG_BADBR
);
678 } else /* single number with comma */
680 } else /* just a single number */
682 repeat(p
, pos
, count
, count2
);
683 if (!EATTWO('\\', '}')) { /* error heuristics */
684 while (MORE() && !SEETWO('\\', '}'))
686 (void)REQUIRE(MORE(), REG_EBRACE
);
689 } else if (c
== '$') /* $ (but not \$) ends it */
696 - p_count - parse a repetition count
697 == static int p_count(struct parse *p);
699 static int /* the value */
706 while (MORE() && isdigit_l((uch
)PEEK(), p
->g
->loc
) && count
<= DUPMAX
) {
707 count
= count
*10 + (GETNEXT() - '0');
711 (void)REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
716 - p_bracket - parse a bracketed character list
717 == static void p_bracket(struct parse *p);
726 /* Dept of Truly Sickening Special-Case Kludges */
727 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:<:]]", 6) == 0) {
732 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:>:]]", 6) == 0) {
738 if ((cs
= allocset(p
)) == NULL
)
741 if (p
->g
->cflags
®_ICASE
)
746 if (PEEK2() != '-' && PEEK2() != ']') { /* Don't eat '-' or ']' if they're part of ranges
747 * but do process [^-] */
753 if (MORE() && !SEETWO('-',']')) /* Parse RE []-'] */
755 #else /* !__DARWIN_UNIX03 */
760 #endif /* __DARWIN_UNIX03 */
761 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
765 (void)MUSTEAT(']', REG_EBRACK
);
767 if (p
->error
!= 0) /* don't mess things up further */
770 if (cs
->invert
&& p
->g
->cflags
®_NEWLINE
)
771 cs
->bmp
['\n' >> 3] |= 1 << ('\n' & 7);
773 if ((ch
= singleton(cs
, p
->g
->loc
)) != OUT
) { /* optimize singleton sets */
777 EMIT(OANYOF
, (int)(cs
- p
->g
->sets
));
781 - p_b_term - parse one term of a bracketed character list
782 == static void p_b_term(struct parse *p, cset *cs);
790 wint_t start
, finish
;
793 /* classify what we've got */
794 switch ((MORE()) ? PEEK() : '\0') {
796 c
= (MORE2()) ? PEEK2() : '\0';
800 if (PEEK2() != '-') { /* Allow [---] */
801 SETERROR(REG_ERANGE
);
802 return; /* NOTE RETURN */
805 #else /* !__DARWIN_UNIX03 */
806 SETERROR(REG_ERANGE
);
807 return; /* NOTE RETURN */
808 #endif /* __DARWIN_UNIX03 */
816 case ':': /* character class */
818 (void)REQUIRE(MORE(), REG_EBRACK
);
820 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
822 (void)REQUIRE(MORE(), REG_EBRACK
);
823 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
825 case '=': /* equivalence class */
827 (void)REQUIRE(MORE(), REG_EBRACK
);
830 REQUIRE(c
!= '-', REG_ECOLLATE
); /* allow [=]=] */
831 #else /* !__DARWIN_UNIX03 */
832 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
833 #endif /* __DARWIN_UNIX03 */
835 (void)REQUIRE(MORE(), REG_EBRACK
);
836 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
838 default: /* symbol, ordinary character, or range */
839 start
= p_b_symbol(p
);
840 if (SEE('-') && MORE2() && PEEK2() != ']') {
846 finish
= p_b_symbol(p
);
852 if (p
->g
->loc
->__collate_load_error
) {
853 (void)REQUIRE((uch
)start
<= (uch
)finish
, REG_ERANGE
);
854 CHaddrange(p
, cs
, start
, finish
);
856 (void)REQUIRE(__collate_range_cmp(start
, finish
, p
->g
->loc
) <= 0, REG_ERANGE
);
857 for (i
= 0; i
<= UCHAR_MAX
; i
++) {
858 if ( __collate_range_cmp(start
, i
, p
->g
->loc
) <= 0
859 && __collate_range_cmp(i
, finish
, p
->g
->loc
) <= 0
870 - p_b_cclass - parse a character-class name and deal with it
871 == static void p_b_cclass(struct parse *p, cset *cs);
883 while (MORE() && isalpha_l((uch
)PEEK(), p
->g
->loc
))
886 if (len
>= sizeof(clname
) - 1) {
887 SETERROR(REG_ECTYPE
);
890 memcpy(clname
, sp
, len
);
892 if ((wct
= wctype_l(clname
, p
->g
->loc
)) == 0) {
893 SETERROR(REG_ECTYPE
);
896 CHaddtype(p
, cs
, wct
);
900 - p_b_eclass - parse an equivalence-class name and deal with it
901 == static void p_b_eclass(struct parse *p, cset *cs);
911 int *newequiv_classes
;
914 while (MORE() && !SEETWO('=', ']'))
917 SETERROR(REG_EBRACK
);
921 memset(&mbs
, 0, sizeof(mbs
));
922 ec
= __collate_equiv_class(sp
, len
, &mbs
, p
->g
->loc
);
924 newequiv_classes
= realloc(cs
->equiv_classes
,
925 (cs
->nequiv_classes
+ 1) * sizeof(*cs
->equiv_classes
));
926 if (newequiv_classes
== NULL
) {
927 SETERROR(REG_ESPACE
);
930 cs
->equiv_classes
= newequiv_classes
;
931 cs
->equiv_classes
[cs
->nequiv_classes
++] = ec
;
934 /* not an equivalence class, so fallback to a collating element */
936 c
= p_b_coll_elem(p
, '=');
941 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
942 == static char p_b_symbol(struct parse *p);
944 static wint_t /* value of symbol */
950 (void)REQUIRE(MORE(), REG_EBRACK
);
951 if (!EATTWO('[', '.'))
954 /* collating symbol */
955 value
= p_b_coll_elem(p
, '.');
956 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
961 - p_b_coll_elem - parse a collating-element name and look it up
962 == static char p_b_coll_elem(struct parse *p, int endc);
964 static wint_t /* value of collating element */
965 p_b_coll_elem(p
, endc
)
967 wint_t endc
; /* name ended by endc,']' */
976 while (MORE() && !SEETWO(endc
, ']'))
979 SETERROR(REG_EBRACK
);
983 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
984 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
985 return(cp
->code
); /* known name */
986 memset(&mbs
, 0, sizeof(mbs
));
987 clen
= __collate_collating_symbol(wbuf
, 16, sp
, len
, &mbs
, p
->g
->loc
);
989 return (*wbuf
); /* single character */
990 else if (clen
== (size_t)-1)
991 SETERROR(REG_ILLSEQ
);
993 SETERROR(REG_ECOLLATE
); /* neither */
998 - othercase - return the case counterpart of an alphabetic
999 == static char othercase(int ch, locale_t loc);
1001 static wint_t /* if no counterpart, return ch */
1006 assert(iswalpha_l(ch
, loc
));
1007 if (iswupper_l(ch
, loc
))
1008 return(towlower_l(ch
, loc
));
1009 else if (iswlower_l(ch
, loc
))
1010 return(towupper_l(ch
, loc
));
1011 else /* peculiar, but could happen */
1016 - bothcases - emit a dualcase version of a two-case character
1017 == static void bothcases(struct parse *p, int ch);
1019 * Boy, is this implementation ever a kludge...
1026 char *oldnext
= p
->next
;
1027 char *oldend
= p
->end
;
1028 char bracket
[3 + MB_LEN_MAX
];
1032 assert(othercase(ch
, p
->g
->loc
) != ch
); /* p_bracket() would recurse */
1034 memset(&mbs
, 0, sizeof(mbs
));
1035 n
= wcrtomb_l(bracket
, ch
, &mbs
, p
->g
->loc
);
1036 assert(n
!= (size_t)-1);
1038 bracket
[n
+ 1] = '\0';
1039 p
->end
= bracket
+n
+1;
1041 assert(p
->next
== p
->end
);
1047 - ordinary - emit an ordinary character
1048 == static void ordinary(struct parse *p, int ch);
1057 if ((p
->g
->cflags
®_ICASE
) && iswalpha_l(ch
, p
->g
->loc
) && othercase(ch
, p
->g
->loc
) != ch
)
1059 else if ((ch
& OPDMASK
) == ch
)
1063 * Kludge: character is too big to fit into an OCHAR operand.
1064 * Emit a singleton set.
1066 if ((cs
= allocset(p
)) == NULL
)
1069 EMIT(OANYOF
, (int)(cs
- p
->g
->sets
));
1074 - nonnewline - emit REG_NEWLINE version of OANY
1075 == static void nonnewline(struct parse *p);
1077 * Boy, is this implementation ever a kludge...
1083 char *oldnext
= p
->next
;
1084 char *oldend
= p
->end
;
1094 assert(p
->next
== bracket
+3);
1100 - repeat - generate code for a bounded repetition, recursively if needed
1101 == static void repeat(struct parse *p, sopno start, int from, int to);
1104 repeat(p
, start
, from
, to
)
1106 sopno start
; /* operand from here to end of strip */
1107 int from
; /* repeated from this number */
1108 int to
; /* to this number of times (maybe INFINITY) */
1110 sopno finish
= HERE();
1113 # define REP(f, t) ((f)*8 + (t))
1114 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1117 if (p
->error
!= 0) /* head off possible runaway recursion */
1122 switch (REP(MAP(from
), MAP(to
))) {
1123 case REP(0, 0): /* must be user doing this */
1124 DROP(finish
-start
); /* drop the operand */
1127 #endif /* __DARWIN_UNIX03 */
1129 case REP(0, INF
): /* as x{1,}? */
1131 /* this case does not require the (y|) trick, noKLUDGE */
1132 /* Just like * =+? */
1133 INSERT(OPLUS_
, start
);
1134 ASTERN(O_PLUS
, start
);
1135 INSERT(OQUEST_
, start
);
1136 ASTERN(O_QUEST
, start
);
1138 #endif /* __DARWIN_UNIX03 */
1139 case REP(0, 1): /* as x{1,1}? */
1140 case REP(0, N
): /* as x{1,n}? */
1141 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1142 INSERT(OCH_
, start
); /* offset is wrong... */
1143 repeat(p
, start
+1, 1, to
);
1144 ASTERN(OOR1
, start
);
1145 AHEAD(start
); /* ... fix it */
1148 ASTERN(O_CH
, THERETHERE());
1150 case REP(1, 1): /* trivial case */
1153 case REP(1, N
): /* as x?x{1,n-1} */
1155 INSERT(OQUEST_
, start
);
1156 ASTERN(O_QUEST
, start
);
1157 #else /* !__DARWIN_UNIX03 */
1158 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1159 INSERT(OCH_
, start
);
1160 ASTERN(OOR1
, start
);
1162 EMIT(OOR2
, 0); /* offset very wrong... */
1163 AHEAD(THERE()); /* ...so fix it */
1164 ASTERN(O_CH
, THERETHERE());
1165 #endif /* __DARWIN_UNIX03 */
1166 copy
= dupl(p
, start
+1, finish
+1);
1167 assert(copy
== finish
+4);
1168 repeat(p
, copy
, 1, to
-1);
1170 case REP(1, INF
): /* as x+ */
1171 INSERT(OPLUS_
, start
);
1172 ASTERN(O_PLUS
, start
);
1174 case REP(N
, N
): /* as xx{m-1,n-1} */
1175 copy
= dupl(p
, start
, finish
);
1176 repeat(p
, copy
, from
-1, to
-1);
1178 case REP(N
, INF
): /* as xx{n-1,INF} */
1179 copy
= dupl(p
, start
, finish
);
1180 repeat(p
, copy
, from
-1, to
);
1182 default: /* "can't happen" */
1183 SETERROR(REG_ASSERT
); /* just in case */
1189 - wgetnext - helper function for WGETNEXT() macro. Gets the next wide
1190 - character from the parse struct, signals a REG_ILLSEQ error if the
1191 - character can't be converted. Returns the number of bytes consumed.
1201 memset(&mbs
, 0, sizeof(mbs
));
1202 n
= mbrtowc_l(&wc
, p
->next
, p
->end
- p
->next
, &mbs
, p
->g
->loc
);
1203 if (n
== (size_t)-1 || n
== (size_t)-2) {
1204 SETERROR(REG_ILLSEQ
);
1214 - seterr - set an error condition
1215 == static int seterr(struct parse *p, int e);
1217 static int /* useless but makes type checking happy */
1222 if (p
->error
== 0) /* keep earliest error condition */
1224 p
->next
= nuls
; /* try to bring things to a halt */
1226 return(0); /* make the return value well-defined */
1230 - allocset - allocate a set of characters for []
1231 == static cset *allocset(struct parse *p);
1239 ncs
= realloc(p
->g
->sets
, (p
->g
->ncsets
+ 1) * sizeof(*ncs
));
1241 SETERROR(REG_ESPACE
);
1245 cs
= &p
->g
->sets
[p
->g
->ncsets
++];
1246 memset(cs
, 0, sizeof(*cs
));
1252 - freeset - free a now-unused set
1253 == static void freeset(struct parse *p, cset *cs);
1260 cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1265 memset(cs
, 0, sizeof(*cs
));
1266 if (cs
== top
-1) /* recover only the easy case */
1271 - singleton - Determine whether a set contains only one character,
1272 - returning it if so, otherwise returning OUT.
1281 for (i
= n
= 0; i
< NC
; i
++)
1282 if (CHIN(cs
, i
, loc
)) {
1288 if (cs
->nwides
== 1 && cs
->nranges
== 0 && cs
->ntypes
== 0 &&
1290 return (cs
->wides
[0]);
1291 /* Don't bother handling the other cases. */
1296 - CHadd - add character to character set.
1304 wint_t nch
, *newwides
;
1307 cs
->bmp
[ch
>> 3] |= 1 << (ch
& 7);
1309 newwides
= realloc(cs
->wides
, (cs
->nwides
+ 1) *
1310 sizeof(*cs
->wides
));
1311 if (newwides
== NULL
) {
1312 SETERROR(REG_ESPACE
);
1315 cs
->wides
= newwides
;
1316 cs
->wides
[cs
->nwides
++] = ch
;
1319 if ((nch
= towlower_l(ch
, p
->g
->loc
)) < NC
)
1320 cs
->bmp
[nch
>> 3] |= 1 << (nch
& 7);
1321 if ((nch
= towupper_l(ch
, p
->g
->loc
)) < NC
)
1322 cs
->bmp
[nch
>> 3] |= 1 << (nch
& 7);
1327 - CHaddrange - add all characters in the range [min,max] to a character set.
1330 CHaddrange(p
, cs
, min
, max
)
1337 for (; min
< NC
&& min
<= max
; min
++)
1341 newranges
= realloc(cs
->ranges
, (cs
->nranges
+ 1) *
1342 sizeof(*cs
->ranges
));
1343 if (newranges
== NULL
) {
1344 SETERROR(REG_ESPACE
);
1347 cs
->ranges
= newranges
;
1348 cs
->ranges
[cs
->nranges
].min
= min
;
1349 cs
->ranges
[cs
->nranges
].min
= max
;
1354 - CHaddtype - add all characters of a certain type to a character set.
1357 CHaddtype(p
, cs
, wct
)
1365 for (i
= 0; i
< NC
; i
++)
1366 if (iswctype_l(i
, wct
, p
->g
->loc
))
1368 newtypes
= realloc(cs
->types
, (cs
->ntypes
+ 1) *
1369 sizeof(*cs
->types
));
1370 if (newtypes
== NULL
) {
1371 SETERROR(REG_ESPACE
);
1374 cs
->types
= newtypes
;
1375 cs
->types
[cs
->ntypes
++] = wct
;
1379 - dupl - emit a duplicate of a bunch of sops
1380 == static sopno dupl(struct parse *p, sopno start, sopno finish);
1382 static sopno
/* start of duplicate */
1383 dupl(p
, start
, finish
)
1385 sopno start
; /* from here */
1386 sopno finish
; /* to this less one */
1389 sopno len
= finish
- start
;
1391 assert(finish
>= start
);
1394 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1395 assert(p
->ssize
>= p
->slen
+ len
);
1396 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1397 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1403 - doemit - emit a strip operator
1404 == static void doemit(struct parse *p, sop op, size_t opnd);
1406 * It might seem better to implement this as a macro with a function as
1407 * hard-case backup, but it's just too big and messy unless there are
1408 * some changes to the data structures. Maybe later.
1416 /* avoid making error situations worse */
1420 /* deal with oversize operands ("can't happen", more or less) */
1421 assert(opnd
< 1<<OPSHIFT
);
1423 /* deal with undersized strip */
1424 if (p
->slen
>= p
->ssize
)
1425 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1426 assert(p
->slen
< p
->ssize
);
1428 /* finally, it's all reduced to the easy case */
1429 p
->strip
[p
->slen
++] = SOP(op
, opnd
);
1433 - doinsert - insert a sop into the strip
1434 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
1437 doinsert(p
, op
, opnd
, pos
)
1447 /* avoid making error situations worse */
1452 EMIT(op
, opnd
); /* do checks, ensure space */
1453 assert(HERE() == sn
+1);
1456 /* adjust paren pointers */
1458 for (i
= 1; i
< NPAREN
; i
++) {
1459 if (p
->pbegin
[i
] >= pos
) {
1462 if (p
->pend
[i
] >= pos
) {
1467 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1468 (HERE()-pos
-1)*sizeof(sop
));
1473 - dofwd - complete a forward reference
1474 == static void dofwd(struct parse *p, sopno pos, sop value);
1477 dofwd(p
, pos
, value
)
1482 /* avoid making error situations worse */
1486 assert(value
< 1<<OPSHIFT
);
1487 p
->strip
[pos
] = OP(p
->strip
[pos
]) | value
;
1491 - enlarge - enlarge the strip
1492 == static void enlarge(struct parse *p, sopno size);
1501 if (p
->ssize
>= size
)
1504 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1506 SETERROR(REG_ESPACE
);
1514 - stripsnug - compact the strip
1515 == static void stripsnug(struct parse *p, struct re_guts *g);
1522 g
->nstates
= p
->slen
;
1523 g
->strip
= (sop
*)realloc((char *)p
->strip
, p
->slen
* sizeof(sop
));
1524 if (g
->strip
== NULL
) {
1525 SETERROR(REG_ESPACE
);
1526 g
->strip
= p
->strip
;
1531 - findmust - fill in must and mlen with longest mandatory literal string
1532 == static void findmust(struct parse *p, struct re_guts *g);
1534 * This algorithm could do fancy things like analyzing the operands of |
1535 * for common subsequences. Someday. This code is simple and finds most
1536 * of the interesting cases.
1538 * Note that must and mlen got initialized during setup.
1552 char buf
[MB_LEN_MAX
];
1555 struct __xlocale_st_runelocale
*rl
= p
->g
->loc
->__lc_ctype
;
1557 /* avoid making error situations worse */
1562 * It's not generally safe to do a ``char'' substring search on
1563 * multibyte character strings, but it's safe for at least
1564 * UTF-8 (see RFC 3629).
1566 if (rl
->__mb_cur_max
> 1 &&
1567 strcmp(rl
->_CurrentRuneLocale
.__encoding
, "UTF-8") != 0)
1570 /* find the longest OCHAR sequence in strip */
1574 scan
= g
->strip
+ 1;
1578 case OCHAR
: /* sequence member */
1579 if (newlen
== 0) { /* new sequence */
1580 memset(&mbs
, 0, sizeof(mbs
));
1581 newstart
= scan
- 1;
1583 clen
= wcrtomb_l(buf
, OPND(s
), &mbs
, p
->g
->loc
);
1584 if (clen
== (size_t)-1)
1588 case OPLUS_
: /* things that don't break one */
1592 case OQUEST_
: /* things that must be skipped */
1594 offset
= altoffset(scan
, offset
);
1599 /* assert() interferes w debug printouts */
1600 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1605 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1607 case OBOW
: /* things that break a sequence */
1614 if (newlen
> g
->mlen
) { /* ends one */
1618 g
->moffset
+= offset
;
1621 g
->moffset
= offset
;
1629 if (newlen
> g
->mlen
) { /* ends one */
1633 g
->moffset
+= offset
;
1636 g
->moffset
= offset
;
1645 case OANYOF
: /* may or may not invalidate offset */
1646 /* First, everything as OANY */
1647 if (newlen
> g
->mlen
) { /* ends one */
1651 g
->moffset
+= offset
;
1654 g
->moffset
= offset
;
1665 /* Anything here makes it impossible or too hard
1666 * to calculate the offset -- so we give up;
1667 * save the last known good offset, in case the
1668 * must sequence doesn't occur later.
1670 if (newlen
> g
->mlen
) { /* ends one */
1674 g
->moffset
+= offset
;
1676 g
->moffset
= offset
;
1682 } while (OP(s
) != OEND
);
1684 if (g
->mlen
== 0) { /* there isn't one */
1689 /* turn it into a character string */
1690 g
->must
= malloc((size_t)g
->mlen
+ 1);
1691 if (g
->must
== NULL
) { /* argh; just forget it */
1698 memset(&mbs
, 0, sizeof(mbs
));
1699 while (cp
< g
->must
+ g
->mlen
) {
1700 while (OP(s
= *scan
++) != OCHAR
)
1702 clen
= wcrtomb_l(cp
, OPND(s
), &mbs
, p
->g
->loc
);
1703 assert(clen
!= (size_t)-1);
1706 assert(cp
== g
->must
+ g
->mlen
);
1707 *cp
++ = '\0'; /* just on general principles */
1711 - altoffset - choose biggest offset among multiple choices
1712 == static int altoffset(sop *scan, int offset);
1714 * Compute, recursively if necessary, the largest offset among multiple
1718 altoffset(scan
, offset
)
1726 /* If we gave up already on offsets, return */
1733 while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
) {
1742 try = altoffset(scan
, try);
1749 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1752 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1753 /* We must skip to the next position, or we'll
1754 * leave altoffset() too early.
1780 return largest
+offset
;
1784 - computejumps - compute char jumps for BM scan
1785 == static void computejumps(struct parse *p, struct re_guts *g);
1787 * This algorithm assumes g->must exists and is has size greater than
1788 * zero. It's based on the algorithm found on Computer Algorithms by
1791 * A char jump is the number of characters one needs to jump based on
1792 * the value of the character from the text that was mismatched.
1802 /* Avoid making errors worse */
1806 g
->charjump
= (int*) malloc((NC
+ 1) * sizeof(int));
1807 if (g
->charjump
== NULL
) /* Not a fatal error */
1809 /* Adjust for signed chars, if necessary */
1810 g
->charjump
= &g
->charjump
[-(CHAR_MIN
)];
1812 /* If the character does not exist in the pattern, the jump
1813 * is equal to the number of characters in the pattern.
1815 for (ch
= CHAR_MIN
; ch
< (CHAR_MAX
+ 1); ch
++)
1816 g
->charjump
[ch
] = g
->mlen
;
1818 /* If the character does exist, compute the jump that would
1819 * take us to the last character in the pattern equal to it
1820 * (notice that we match right to left, so that last character
1821 * is the first one that would be matched).
1823 for (mindex
= 0; mindex
< g
->mlen
; mindex
++)
1824 g
->charjump
[(int)g
->must
[mindex
]] = g
->mlen
- mindex
- 1;
1828 - computematchjumps - compute match jumps for BM scan
1829 == static void computematchjumps(struct parse *p, struct re_guts *g);
1831 * This algorithm assumes g->must exists and is has size greater than
1832 * zero. It's based on the algorithm found on Computer Algorithms by
1835 * A match jump is the number of characters one needs to advance based
1836 * on the already-matched suffix.
1837 * Notice that all values here are minus (g->mlen-1), because of the way
1838 * the search algorithm works.
1841 computematchjumps(p
, g
)
1845 int mindex
; /* General "must" iterator */
1846 int suffix
; /* Keeps track of matching suffix */
1847 int ssuffix
; /* Keeps track of suffixes' suffix */
1848 int* pmatches
; /* pmatches[k] points to the next i
1849 * such that i+1...mlen is a substring
1850 * of k+1...k+mlen-i-1
1853 /* Avoid making errors worse */
1857 pmatches
= (int*) malloc(g
->mlen
* sizeof(unsigned int));
1858 if (pmatches
== NULL
) {
1859 g
->matchjump
= NULL
;
1863 g
->matchjump
= (int*) malloc(g
->mlen
* sizeof(unsigned int));
1864 if (g
->matchjump
== NULL
) /* Not a fatal error */
1867 /* Set maximum possible jump for each character in the pattern */
1868 for (mindex
= 0; mindex
< g
->mlen
; mindex
++)
1869 g
->matchjump
[mindex
] = 2*g
->mlen
- mindex
- 1;
1871 /* Compute pmatches[] */
1872 for (mindex
= g
->mlen
- 1, suffix
= g
->mlen
; mindex
>= 0;
1873 mindex
--, suffix
--) {
1874 pmatches
[mindex
] = suffix
;
1876 /* If a mismatch is found, interrupting the substring,
1877 * compute the matchjump for that position. If no
1878 * mismatch is found, then a text substring mismatched
1879 * against the suffix will also mismatch against the
1882 while (suffix
< g
->mlen
1883 && g
->must
[mindex
] != g
->must
[suffix
]) {
1884 g
->matchjump
[suffix
] = MIN(g
->matchjump
[suffix
],
1885 g
->mlen
- mindex
- 1);
1886 suffix
= pmatches
[suffix
];
1890 /* Compute the matchjump up to the last substring found to jump
1891 * to the beginning of the largest must pattern prefix matching
1894 for (mindex
= 0; mindex
<= suffix
; mindex
++)
1895 g
->matchjump
[mindex
] = MIN(g
->matchjump
[mindex
],
1896 g
->mlen
+ suffix
- mindex
);
1898 ssuffix
= pmatches
[suffix
];
1899 while (suffix
< g
->mlen
) {
1900 while (suffix
<= ssuffix
&& suffix
< g
->mlen
) {
1901 g
->matchjump
[suffix
] = MIN(g
->matchjump
[suffix
],
1902 g
->mlen
+ ssuffix
- suffix
);
1905 if (suffix
< g
->mlen
)
1906 ssuffix
= pmatches
[ssuffix
];
1913 - pluscount - count + nesting
1914 == static sopno pluscount(struct parse *p, struct re_guts *g);
1916 static sopno
/* nesting depth */
1927 return(0); /* there may not be an OEND */
1929 scan
= g
->strip
+ 1;
1937 if (plusnest
> maxnest
)
1942 } while (OP(s
) != OEND
);