]>
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 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid
[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94";
38 #endif /* LIBC_SCCS and not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/lib/libc/regex/regcomp.c,v 1.36 2007/06/11 03:05:54 delphij Exp $");
42 #include "xlocale_private.h"
44 #include <sys/types.h>
63 * parse structure, passed up and down to avoid global variables and
67 char *next
; /* next character in RE */
68 char *end
; /* end of string (-> NUL normally) */
69 int error
; /* has an error been seen? */
70 sop
*strip
; /* malloced strip */
71 sopno ssize
; /* malloced strip size (allocated) */
72 sopno slen
; /* malloced strip length (used) */
73 int ncsalloc
; /* number of csets allocated */
76 #endif /* __DARWIN_UNIX03 */
78 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
79 sopno pbegin
[NPAREN
]; /* -> ( ([0] unused) */
80 sopno pend
[NPAREN
]; /* -> ) ([0] unused) */
83 /* ========= begin header generated by ./mkh ========= */
88 /* === regcomp.c === */
89 static void p_ere(struct parse
*p
, wint_t stop
);
90 static void p_ere_exp(struct parse
*p
);
91 static void p_str(struct parse
*p
);
92 static void p_bre(struct parse
*p
, wint_t end1
, wint_t end2
);
93 static int p_simp_re(struct parse
*p
, int starordinary
);
94 static int p_count(struct parse
*p
);
95 static void p_bracket(struct parse
*p
);
96 static void p_b_term(struct parse
*p
, cset
*cs
);
97 static void p_b_cclass(struct parse
*p
, cset
*cs
);
98 static void p_b_eclass(struct parse
*p
, cset
*cs
);
99 static wint_t p_b_symbol(struct parse
*p
);
100 static wint_t p_b_coll_elem(struct parse
*p
, wint_t endc
);
101 static wint_t othercase(wint_t ch
, locale_t loc
);
102 static void bothcases(struct parse
*p
, wint_t ch
);
103 static void ordinary(struct parse
*p
, wint_t ch
);
104 static void nonnewline(struct parse
*p
);
105 static void repeat(struct parse
*p
, sopno start
, int from
, int to
);
106 static int seterr(struct parse
*p
, int e
);
107 static cset
*allocset(struct parse
*p
);
108 static void freeset(struct parse
*p
, cset
*cs
);
109 static void CHadd(struct parse
*p
, cset
*cs
, wint_t ch
);
110 static void CHaddrange(struct parse
*p
, cset
*cs
, wint_t min
, wint_t max
);
111 static void CHaddtype(struct parse
*p
, cset
*cs
, wctype_t wct
);
112 static wint_t singleton(cset
*cs
, locale_t loc
);
113 static sopno
dupl(struct parse
*p
, sopno start
, sopno finish
);
114 static void doemit(struct parse
*p
, sop op
, size_t opnd
);
115 static void doinsert(struct parse
*p
, sop op
, size_t opnd
, sopno pos
);
116 static void dofwd(struct parse
*p
, sopno pos
, sop value
);
117 static void enlarge(struct parse
*p
, sopno size
);
118 static void stripsnug(struct parse
*p
, struct re_guts
*g
);
119 static void findmust(struct parse
*p
, struct re_guts
*g
);
120 static int altoffset(sop
*scan
, int offset
);
121 static void computejumps(struct parse
*p
, struct re_guts
*g
);
122 static void computematchjumps(struct parse
*p
, struct re_guts
*g
);
123 static sopno
pluscount(struct parse
*p
, struct re_guts
*g
);
124 static wint_t wgetnext(struct parse
*p
);
129 /* ========= end header generated by ./mkh ========= */
131 static char nuls
[10]; /* place to point scanner in event of error */
134 * macros for use with parse structure
135 * BEWARE: these know that the parse structure is named `p' !!!
137 #define PEEK() (*p->next)
138 #define PEEK2() (*(p->next+1))
139 #define MORE() (p->next < p->end)
140 #define MORE2() (p->next+1 < p->end)
141 #define SEE(c) (MORE() && PEEK() == (c))
142 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
143 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
144 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
145 #define NEXT() (p->next++)
146 #define NEXT2() (p->next += 2)
147 #define NEXTn(n) (p->next += (n))
148 #define GETNEXT() (*p->next++)
149 #define WGETNEXT() wgetnext(p)
150 #define SETERROR(e) seterr(p, (e))
151 #define REQUIRE(co, e) ((co) || SETERROR(e))
152 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
153 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
154 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
155 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
156 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
157 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
158 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
159 #define HERE() (p->slen)
160 #define THERE() (p->slen - 1)
161 #define THERETHERE() (p->slen - 2)
162 #define DROP(n) (p->slen -= (n))
165 static int never
= 0; /* for use in asserts; shuts lint up */
167 #define never 0 /* some <assert.h>s have bugs too */
170 /* Macro used by computejump()/computematchjump() */
171 #define MIN(a,b) ((a)<(b)?(a):(b))
174 - regcomp - interface for parser and compilation
175 = extern int regcomp(regex_t *, const char *, int);
176 = #define REG_BASIC 0000
177 = #define REG_EXTENDED 0001
178 = #define REG_ICASE 0002
179 = #define REG_NOSUB 0004
180 = #define REG_NEWLINE 0010
181 = #define REG_NOSPEC 0020
182 = #define REG_PEND 0040
183 = #define REG_DUMP 0200
185 int /* 0 success, otherwise REG_something */
186 regcomp(regex_t
* __restrict preg
,
187 const char * __restrict pattern
,
192 struct parse
*p
= &pa
;
196 # define GOODFLAGS(f) (f)
198 # define GOODFLAGS(f) ((f)&~REG_DUMP)
201 cflags
= GOODFLAGS(cflags
);
202 if ((cflags
®_EXTENDED
) && (cflags
®_NOSPEC
))
205 if (cflags
®_PEND
) {
206 if (preg
->re_endp
< pattern
)
208 len
= preg
->re_endp
- pattern
;
210 len
= strlen((char *)pattern
);
212 /* do the mallocs early so failure handling is easy */
213 g
= (struct re_guts
*)malloc(sizeof(struct re_guts
));
216 p
->ssize
= len
/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
217 p
->strip
= (sop
*)malloc(p
->ssize
* sizeof(sop
));
219 if (p
->strip
== NULL
) {
226 p
->next
= (char *)pattern
; /* convenience; we do not modify it */
227 p
->end
= p
->next
+ len
;
232 #endif /* __DARWIN_UNIX03 */
233 for (i
= 0; i
< NPAREN
; i
++) {
237 g
->loc
= __current_locale();
254 g
->firststate
= THERE();
255 if (cflags
®_EXTENDED
)
257 else if (cflags
®_NOSPEC
)
262 g
->laststate
= THERE();
264 /* tidy up loose ends and fill things in */
267 /* only use Boyer-Moore algorithm if the pattern is bigger
268 * than three characters
272 computematchjumps(p
, g
);
273 if(g
->matchjump
== NULL
&& g
->charjump
!= NULL
) {
278 g
->nplus
= pluscount(p
, g
);
280 preg
->re_nsub
= g
->nsub
;
282 preg
->re_magic
= MAGIC1
;
284 /* not debugging, so can't rely on the assert() in regexec() */
286 SETERROR(REG_ASSERT
);
289 /* win or lose, we're done */
290 if (p
->error
!= 0) /* lose */
296 - p_ere - ERE parser top level, concatenation and alternation
297 == static void p_ere(struct parse *p, int stop);
300 p_ere(struct parse
*p
,
301 int stop
) /* character this ERE should end at */
307 int first
= 1; /* is this the first alternative? */
310 /* do a bunch of concatenated expressions */
312 while (MORE() && (c
= PEEK()) != '|' && c
!= stop
)
315 if (!p
->zerorepeats
) REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
316 else p
->zerorepeats
--;
318 (void)REQUIRE(HERE() != conc
, REG_EMPTY
); /* require nonempty */
321 break; /* NOTE BREAK OUT */
324 INSERT(OCH_
, conc
); /* offset is wrong */
329 ASTERN(OOR1
, prevback
);
331 AHEAD(prevfwd
); /* fix previous offset */
333 EMIT(OOR2
, 0); /* offset is very wrong */
336 if (!first
) { /* tail-end fixups */
338 ASTERN(O_CH
, prevback
);
341 assert(!MORE() || SEE(stop
));
345 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
346 == static void p_ere_exp(struct parse *p);
349 p_ere_exp(struct parse
*p
)
359 assert(MORE()); /* caller should have ensured this */
365 (void)REQUIRE(MORE(), REG_EPAREN
);
369 p
->pbegin
[subno
] = HERE();
370 EMIT(OLPAREN
, subno
);
373 if (subno
< NPAREN
) {
374 p
->pend
[subno
] = HERE();
375 assert(p
->pend
[subno
] != 0);
377 EMIT(ORPAREN
, subno
);
378 (void)MUSTEAT(')', REG_EPAREN
);
380 #ifndef POSIX_MISTAKE
381 case ')': /* happens only if no current unmatched ( */
383 * You may ask, why the ifndef? Because I didn't notice
384 * this until slightly too late for 1003.2, and none of the
385 * other 1003.2 regular-expression reviewers noticed it at
386 * all. So an unmatched ) is legal POSIX, at least until
387 * we can get it fixed.
389 SETERROR(REG_EPAREN
);
394 p
->g
->iflags
|= USEBOL
;
400 p
->g
->iflags
|= USEEOL
;
409 SETERROR(REG_BADRPT
);
412 if (p
->g
->cflags
®_NEWLINE
)
421 (void)REQUIRE(MORE(), REG_EESCAPE
);
425 case '{': /* okay as ordinary except if digit follows */
426 (void)REQUIRE(!MORE() || !isdigit_l((uch
)PEEK(), p
->g
->loc
), REG_BADRPT
);
438 /* we call { a repetition if followed by a digit */
439 if (!( c
== '*' || c
== '+' || c
== '?' ||
440 (c
== '{' && MORE2() && isdigit_l((uch
)PEEK2(), p
->g
->loc
)) ))
441 return; /* no repetition, we're done */
444 (void)REQUIRE(!wascaret
, REG_BADRPT
);
446 case '*': /* implemented as +? */
447 /* this case does not require the (y|) trick, noKLUDGE */
450 INSERT(OQUEST_
, pos
);
451 ASTERN(O_QUEST
, pos
);
458 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
459 INSERT(OCH_
, pos
); /* offset slightly wrong */
460 ASTERN(OOR1
, pos
); /* this one's right */
461 AHEAD(pos
); /* fix the OCH_ */
462 EMIT(OOR2
, 0); /* offset very wrong... */
463 AHEAD(THERE()); /* ...so fix it */
464 ASTERN(O_CH
, THERETHERE());
469 if (isdigit_l((uch
)PEEK(), p
->g
->loc
)) {
471 (void)REQUIRE(count
<= count2
, REG_BADBR
);
472 } else /* single number with comma */
474 } else /* just a single number */
476 repeat(p
, pos
, count
, count2
);
477 if (!EAT('}')) { /* error heuristics */
478 while (MORE() && PEEK() != '}')
480 (void)REQUIRE(MORE(), REG_EBRACE
);
489 if (!( c
== '*' || c
== '+' || c
== '?' ||
490 (c
== '{' && MORE2() && isdigit_l((uch
)PEEK2(), p
->g
->loc
)) ) )
492 SETERROR(REG_BADRPT
);
496 - p_str - string (no metacharacters) "parser"
497 == static void p_str(struct parse *p);
500 p_str(struct parse
*p
)
503 if (!p
->zerorepeats
) REQUIRE(MORE(), REG_EMPTY
);
504 else p
->zerorepeats
--;
505 #else /* !__DARWIN_UNIX03 */
506 (void)REQUIRE(MORE(), REG_EMPTY
);
507 #endif /* __DARWIN_UNIX03 */
509 ordinary(p
, WGETNEXT());
513 - p_bre - BRE parser top level, anchoring and concatenation
514 == static void p_bre(struct parse *p, int end1, \
516 * Giving end1 as OUT essentially eliminates the end1/end2 check.
518 * This implementation is a bit of a kludge, in that a trailing $ is first
519 * taken as an ordinary character and then revised to be an anchor.
520 * The amount of lookahead needed to avoid this kludge is excessive.
523 p_bre(struct parse
*p
,
524 int end1
, /* first terminating character */
525 int end2
) /* second terminating character */
527 sopno start
= HERE();
528 int first
= 1; /* first subexpression? */
533 p
->g
->iflags
|= USEBOL
;
536 while (MORE() && !SEETWO(end1
, end2
)) {
537 wasdollar
= p_simp_re(p
, first
);
540 if (wasdollar
) { /* oops, that was a trailing anchor */
543 p
->g
->iflags
|= USEEOL
;
547 if (!p
->zerorepeats
) REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
548 else p
->zerorepeats
--;
549 #else /* !__DARWIN_UNIX03 */
550 (void)REQUIRE(HERE() != start
, REG_EMPTY
); /* require nonempty */
551 #endif /* __DARWIN_UNIX03 */
555 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
556 == static int p_simp_re(struct parse *p, int starordinary);
558 static int /* was the simple RE an unbackslashed $? */
559 p_simp_re(struct parse
*p
,
560 int starordinary
) /* is a leading * an ordinary character? */
569 # define BACKSL (1<<CHAR_BIT)
571 pos
= HERE(); /* repetion op, if any, covers from here */
573 assert(MORE()); /* caller should have ensured this */
576 (void)REQUIRE(MORE(), REG_EESCAPE
);
577 c
= BACKSL
| GETNEXT();
581 if (p
->g
->cflags
®_NEWLINE
)
590 SETERROR(REG_BADRPT
);
596 p
->pbegin
[subno
] = HERE();
597 EMIT(OLPAREN
, subno
);
598 /* the MORE here is an error heuristic */
599 if (MORE() && !SEETWO('\\', ')'))
601 if (subno
< NPAREN
) {
602 p
->pend
[subno
] = HERE();
603 assert(p
->pend
[subno
] != 0);
605 EMIT(ORPAREN
, subno
);
606 (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN
);
608 case BACKSL
|')': /* should not get here -- must be user */
610 SETERROR(REG_EPAREN
);
621 i
= (c
&~BACKSL
) - '0';
623 if (p
->pend
[i
] != 0) {
626 #endif /* __DARWIN_UNIX03 */
627 assert(i
<= p
->g
->nsub
);
629 assert(p
->pbegin
[i
] != 0);
630 assert(OP(p
->strip
[p
->pbegin
[i
]]) == OLPAREN
);
631 assert(OP(p
->strip
[p
->pend
[i
]]) == ORPAREN
);
633 if (OP(p
->strip
[p
->pbegin
[i
]+skip
]) == OBOL
) {
634 skip
++; /* don't dup anchor in subexp */
636 (void) dupl(p
, p
->pbegin
[i
]+skip
, p
->pend
[i
]);
637 #else /* !__DARWIN_UNIX03 */
638 (void) dupl(p
, p
->pbegin
[i
]+1, p
->pend
[i
]);
639 #endif /* __DARWIN_UNIX03 */
642 SETERROR(REG_ESUBREG
);
646 (void)REQUIRE(starordinary
, REG_BADRPT
);
655 if (EAT('*')) { /* implemented as +? */
656 /* this case does not require the (y|) trick, noKLUDGE */
659 INSERT(OQUEST_
, pos
);
660 ASTERN(O_QUEST
, pos
);
661 } else if (EATTWO('\\', '{')) {
662 (void)REQUIRE(MORE(), REG_EBRACE
);
665 if (MORE() && isdigit_l((uch
)PEEK(), p
->g
->loc
)) {
667 (void)REQUIRE(count
<= count2
, REG_BADBR
);
668 } else /* single number with comma */
670 } else /* just a single number */
672 repeat(p
, pos
, count
, count2
);
673 if (!EATTWO('\\', '}')) { /* error heuristics */
674 while (MORE() && !SEETWO('\\', '}'))
676 (void)REQUIRE(MORE(), REG_EBRACE
);
679 } else if (c
== '$') /* $ (but not \$) ends it */
686 - p_count - parse a repetition count
687 == static int p_count(struct parse *p);
689 static int /* the value */
690 p_count(struct parse
*p
)
695 while (MORE() && isdigit_l((uch
)PEEK(), p
->g
->loc
) && count
<= DUPMAX
) {
696 count
= count
*10 + (GETNEXT() - '0');
700 (void)REQUIRE(ndigits
> 0 && count
<= DUPMAX
, REG_BADBR
);
705 - p_bracket - parse a bracketed character list
706 == static void p_bracket(struct parse *p);
709 p_bracket(struct parse
*p
)
714 /* Dept of Truly Sickening Special-Case Kludges */
715 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:<:]]", 6) == 0) {
720 if (p
->next
+ 5 < p
->end
&& strncmp(p
->next
, "[:>:]]", 6) == 0) {
726 if ((cs
= allocset(p
)) == NULL
)
729 if (p
->g
->cflags
®_ICASE
)
734 if (PEEK2() != '-' && PEEK2() != ']') { /* Don't eat '-' or ']' if they're part of ranges
735 * but do process [^-] */
741 if (MORE() && !SEETWO('-',']')) /* Parse RE []-'] */
743 #else /* !__DARWIN_UNIX03 */
748 #endif /* __DARWIN_UNIX03 */
749 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
753 (void)MUSTEAT(']', REG_EBRACK
);
755 if (p
->error
!= 0) /* don't mess things up further */
758 if (cs
->invert
&& p
->g
->cflags
®_NEWLINE
)
759 cs
->bmp
['\n' >> 3] |= 1 << ('\n' & 7);
761 if ((ch
= singleton(cs
, p
->g
->loc
)) != OUT
) { /* optimize singleton sets */
765 EMIT(OANYOF
, (int)(cs
- p
->g
->sets
));
769 - p_b_term - parse one term of a bracketed character list
770 == static void p_b_term(struct parse *p, cset *cs);
773 p_b_term(struct parse
*p
, cset
*cs
)
776 wint_t start
, finish
;
779 /* classify what we've got */
780 switch ((MORE()) ? PEEK() : '\0') {
782 c
= (MORE2()) ? PEEK2() : '\0';
786 if (PEEK2() != '-') { /* Allow [---] */
787 SETERROR(REG_ERANGE
);
788 return; /* NOTE RETURN */
791 #else /* !__DARWIN_UNIX03 */
792 SETERROR(REG_ERANGE
);
793 return; /* NOTE RETURN */
794 #endif /* __DARWIN_UNIX03 */
802 case ':': /* character class */
804 (void)REQUIRE(MORE(), REG_EBRACK
);
806 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECTYPE
);
808 (void)REQUIRE(MORE(), REG_EBRACK
);
809 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE
);
811 case '=': /* equivalence class */
813 (void)REQUIRE(MORE(), REG_EBRACK
);
816 REQUIRE(c
!= '-', REG_ECOLLATE
); /* allow [=]=] */
817 #else /* !__DARWIN_UNIX03 */
818 (void)REQUIRE(c
!= '-' && c
!= ']', REG_ECOLLATE
);
819 #endif /* __DARWIN_UNIX03 */
821 (void)REQUIRE(MORE(), REG_EBRACK
);
822 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE
);
824 default: /* symbol, ordinary character, or range */
825 start
= p_b_symbol(p
);
826 if (SEE('-') && MORE2() && PEEK2() != ']') {
832 finish
= p_b_symbol(p
);
838 if (p
->g
->loc
->__collate_load_error
) {
839 (void)REQUIRE((uch
)start
<= (uch
)finish
, REG_ERANGE
);
840 CHaddrange(p
, cs
, start
, finish
);
842 (void)REQUIRE(__collate_range_cmp(start
, finish
, p
->g
->loc
) <= 0, REG_ERANGE
);
843 for (i
= 0; i
<= UCHAR_MAX
; i
++) {
844 if ( __collate_range_cmp(start
, i
, p
->g
->loc
) <= 0
845 && __collate_range_cmp(i
, finish
, p
->g
->loc
) <= 0
856 - p_b_cclass - parse a character-class name and deal with it
857 == static void p_b_cclass(struct parse *p, cset *cs);
860 p_b_cclass(struct parse
*p
, cset
*cs
)
867 while (MORE() && isalpha_l((uch
)PEEK(), p
->g
->loc
))
870 if (len
>= sizeof(clname
) - 1) {
871 SETERROR(REG_ECTYPE
);
874 memcpy(clname
, sp
, len
);
876 if ((wct
= wctype_l(clname
, p
->g
->loc
)) == 0) {
877 SETERROR(REG_ECTYPE
);
880 CHaddtype(p
, cs
, wct
);
884 - p_b_eclass - parse an equivalence-class name and deal with it
885 == static void p_b_eclass(struct parse *p, cset *cs);
888 p_b_eclass(struct parse
*p
, cset
*cs
)
893 int *newequiv_classes
;
896 while (MORE() && !SEETWO('=', ']'))
899 SETERROR(REG_EBRACK
);
903 memset(&mbs
, 0, sizeof(mbs
));
904 ec
= __collate_equiv_class(sp
, len
, &mbs
, p
->g
->loc
);
906 newequiv_classes
= realloc(cs
->equiv_classes
,
907 (cs
->nequiv_classes
+ 1) * sizeof(*cs
->equiv_classes
));
908 if (newequiv_classes
== NULL
) {
909 SETERROR(REG_ESPACE
);
912 cs
->equiv_classes
= newequiv_classes
;
913 cs
->equiv_classes
[cs
->nequiv_classes
++] = ec
;
916 /* not an equivalence class, so fallback to a collating element */
918 c
= p_b_coll_elem(p
, '=');
923 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
924 == static char p_b_symbol(struct parse *p);
926 static wint_t /* value of symbol */
927 p_b_symbol(struct parse
*p
)
931 (void)REQUIRE(MORE(), REG_EBRACK
);
932 if (!EATTWO('[', '.'))
935 /* collating symbol */
936 value
= p_b_coll_elem(p
, '.');
937 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE
);
942 - p_b_coll_elem - parse a collating-element name and look it up
943 == static char p_b_coll_elem(struct parse *p, int endc);
945 static wint_t /* value of collating element */
946 p_b_coll_elem(struct parse
*p
,
947 wint_t endc
) /* name ended by endc,']' */
950 const struct cname
*cp
;
956 while (MORE() && !SEETWO(endc
, ']'))
959 SETERROR(REG_EBRACK
);
963 for (cp
= cnames
; cp
->name
!= NULL
; cp
++)
964 if (strncmp(cp
->name
, sp
, len
) == 0 && cp
->name
[len
] == '\0')
965 return(cp
->code
); /* known name */
966 memset(&mbs
, 0, sizeof(mbs
));
967 clen
= __collate_collating_symbol(wbuf
, 16, sp
, len
, &mbs
, p
->g
->loc
);
969 return (*wbuf
); /* single character */
970 else if (clen
== (size_t)-1)
971 SETERROR(REG_ILLSEQ
);
973 SETERROR(REG_ECOLLATE
); /* neither */
978 - othercase - return the case counterpart of an alphabetic
979 == static char othercase(wint_t ch, locale_t loc);
981 static wint_t /* if no counterpart, return ch */
982 othercase(wint_t ch
, locale_t loc
)
984 assert(iswalpha_l(ch
, loc
));
985 if (iswupper_l(ch
, loc
))
986 return(towlower_l(ch
, loc
));
987 else if (iswlower_l(ch
, loc
))
988 return(towupper_l(ch
, loc
));
989 else /* peculiar, but could happen */
994 - bothcases - emit a dualcase version of a two-case character
995 == static void bothcases(struct parse *p, int ch);
997 * Boy, is this implementation ever a kludge...
1000 bothcases(struct parse
*p
, wint_t ch
)
1002 char *oldnext
= p
->next
;
1003 char *oldend
= p
->end
;
1004 char bracket
[3 + MB_LEN_MAX
];
1008 assert(othercase(ch
, p
->g
->loc
) != ch
); /* p_bracket() would recurse */
1010 memset(&mbs
, 0, sizeof(mbs
));
1011 n
= wcrtomb_l(bracket
, ch
, &mbs
, p
->g
->loc
);
1012 assert(n
!= (size_t)-1);
1014 bracket
[n
+ 1] = '\0';
1015 p
->end
= bracket
+n
+1;
1017 assert(p
->next
== p
->end
);
1023 - ordinary - emit an ordinary character
1024 == static void ordinary(struct parse *p, int ch);
1027 ordinary(struct parse
*p
, wint_t ch
)
1031 if ((p
->g
->cflags
®_ICASE
) && iswalpha_l(ch
, p
->g
->loc
) && othercase(ch
, p
->g
->loc
) != ch
)
1033 else if ((ch
& OPDMASK
) == ch
)
1037 * Kludge: character is too big to fit into an OCHAR operand.
1038 * Emit a singleton set.
1040 if ((cs
= allocset(p
)) == NULL
)
1043 EMIT(OANYOF
, (int)(cs
- p
->g
->sets
));
1048 - nonnewline - emit REG_NEWLINE version of OANY
1049 == static void nonnewline(struct parse *p);
1051 * Boy, is this implementation ever a kludge...
1054 nonnewline(struct parse
*p
)
1056 char *oldnext
= p
->next
;
1057 char *oldend
= p
->end
;
1067 assert(p
->next
== bracket
+3);
1073 - repeat - generate code for a bounded repetition, recursively if needed
1074 == static void repeat(struct parse *p, sopno start, int from, int to);
1077 repeat(struct parse
*p
,
1078 sopno start
, /* operand from here to end of strip */
1079 int from
, /* repeated from this number */
1080 int to
) /* to this number of times (maybe INFINITY) */
1082 sopno finish
= HERE();
1085 # define REP(f, t) ((f)*8 + (t))
1086 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1089 if (p
->error
!= 0) /* head off possible runaway recursion */
1094 switch (REP(MAP(from
), MAP(to
))) {
1095 case REP(0, 0): /* must be user doing this */
1096 DROP(finish
-start
); /* drop the operand */
1099 #endif /* __DARWIN_UNIX03 */
1101 case REP(0, INF
): /* as x{1,}? */
1103 /* this case does not require the (y|) trick, noKLUDGE */
1104 /* Just like * =+? */
1105 INSERT(OPLUS_
, start
);
1106 ASTERN(O_PLUS
, start
);
1107 INSERT(OQUEST_
, start
);
1108 ASTERN(O_QUEST
, start
);
1110 #endif /* __DARWIN_UNIX03 */
1111 case REP(0, 1): /* as x{1,1}? */
1112 case REP(0, N
): /* as x{1,n}? */
1113 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1114 INSERT(OCH_
, start
); /* offset is wrong... */
1115 repeat(p
, start
+1, 1, to
);
1116 ASTERN(OOR1
, start
);
1117 AHEAD(start
); /* ... fix it */
1120 ASTERN(O_CH
, THERETHERE());
1122 case REP(1, 1): /* trivial case */
1125 case REP(1, N
): /* as x?x{1,n-1} */
1127 INSERT(OQUEST_
, start
);
1128 ASTERN(O_QUEST
, start
);
1129 #else /* !__DARWIN_UNIX03 */
1130 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1131 INSERT(OCH_
, start
);
1132 ASTERN(OOR1
, start
);
1134 EMIT(OOR2
, 0); /* offset very wrong... */
1135 AHEAD(THERE()); /* ...so fix it */
1136 ASTERN(O_CH
, THERETHERE());
1137 #endif /* __DARWIN_UNIX03 */
1138 copy
= dupl(p
, start
+1, finish
+1);
1139 assert(copy
== finish
+4);
1140 repeat(p
, copy
, 1, to
-1);
1142 case REP(1, INF
): /* as x+ */
1143 INSERT(OPLUS_
, start
);
1144 ASTERN(O_PLUS
, start
);
1146 case REP(N
, N
): /* as xx{m-1,n-1} */
1147 copy
= dupl(p
, start
, finish
);
1148 repeat(p
, copy
, from
-1, to
-1);
1150 case REP(N
, INF
): /* as xx{n-1,INF} */
1151 copy
= dupl(p
, start
, finish
);
1152 repeat(p
, copy
, from
-1, to
);
1154 default: /* "can't happen" */
1155 SETERROR(REG_ASSERT
); /* just in case */
1161 - wgetnext - helper function for WGETNEXT() macro. Gets the next wide
1162 - character from the parse struct, signals a REG_ILLSEQ error if the
1163 - character can't be converted. Returns the number of bytes consumed.
1166 wgetnext(struct parse
*p
)
1172 memset(&mbs
, 0, sizeof(mbs
));
1173 n
= mbrtowc_l(&wc
, p
->next
, p
->end
- p
->next
, &mbs
, p
->g
->loc
);
1174 if (n
== (size_t)-1 || n
== (size_t)-2) {
1175 SETERROR(REG_ILLSEQ
);
1185 - seterr - set an error condition
1186 == static int seterr(struct parse *p, int e);
1188 static int /* useless but makes type checking happy */
1189 seterr(struct parse
*p
, int e
)
1191 if (p
->error
== 0) /* keep earliest error condition */
1193 p
->next
= nuls
; /* try to bring things to a halt */
1195 return(0); /* make the return value well-defined */
1199 - allocset - allocate a set of characters for []
1200 == static cset *allocset(struct parse *p);
1203 allocset(struct parse
*p
)
1207 ncs
= realloc(p
->g
->sets
, (p
->g
->ncsets
+ 1) * sizeof(*ncs
));
1209 SETERROR(REG_ESPACE
);
1213 cs
= &p
->g
->sets
[p
->g
->ncsets
++];
1214 memset(cs
, 0, sizeof(*cs
));
1220 - freeset - free a now-unused set
1221 == static void freeset(struct parse *p, cset *cs);
1224 freeset(struct parse
*p
, cset
*cs
)
1226 cset
*top
= &p
->g
->sets
[p
->g
->ncsets
];
1231 memset(cs
, 0, sizeof(*cs
));
1232 if (cs
== top
-1) /* recover only the easy case */
1237 - singleton - Determine whether a set contains only one character,
1238 - returning it if so, otherwise returning OUT.
1241 singleton(cset
*cs
, locale_t loc
)
1245 for (i
= n
= 0; i
< NC
; i
++)
1246 if (CHIN(cs
, i
, loc
)) {
1252 if (cs
->nwides
== 1 && cs
->nranges
== 0 && cs
->ntypes
== 0 &&
1254 return (cs
->wides
[0]);
1255 /* Don't bother handling the other cases. */
1260 - CHadd - add character to character set.
1263 CHadd(struct parse
*p
, cset
*cs
, wint_t ch
)
1265 wint_t nch
, *newwides
;
1268 cs
->bmp
[ch
>> 3] |= 1 << (ch
& 7);
1270 newwides
= realloc(cs
->wides
, (cs
->nwides
+ 1) *
1271 sizeof(*cs
->wides
));
1272 if (newwides
== NULL
) {
1273 SETERROR(REG_ESPACE
);
1276 cs
->wides
= newwides
;
1277 cs
->wides
[cs
->nwides
++] = ch
;
1280 if ((nch
= towlower_l(ch
, p
->g
->loc
)) < NC
)
1281 cs
->bmp
[nch
>> 3] |= 1 << (nch
& 7);
1282 if ((nch
= towupper_l(ch
, p
->g
->loc
)) < NC
)
1283 cs
->bmp
[nch
>> 3] |= 1 << (nch
& 7);
1288 - CHaddrange - add all characters in the range [min,max] to a character set.
1291 CHaddrange(struct parse
*p
, cset
*cs
, wint_t min
, wint_t max
)
1295 for (; min
< NC
&& min
<= max
; min
++)
1299 newranges
= realloc(cs
->ranges
, (cs
->nranges
+ 1) *
1300 sizeof(*cs
->ranges
));
1301 if (newranges
== NULL
) {
1302 SETERROR(REG_ESPACE
);
1305 cs
->ranges
= newranges
;
1306 cs
->ranges
[cs
->nranges
].min
= min
;
1307 cs
->ranges
[cs
->nranges
].min
= max
;
1312 - CHaddtype - add all characters of a certain type to a character set.
1315 CHaddtype(struct parse
*p
, cset
*cs
, wctype_t wct
)
1320 for (i
= 0; i
< NC
; i
++)
1321 if (iswctype_l(i
, wct
, p
->g
->loc
))
1323 newtypes
= realloc(cs
->types
, (cs
->ntypes
+ 1) *
1324 sizeof(*cs
->types
));
1325 if (newtypes
== NULL
) {
1326 SETERROR(REG_ESPACE
);
1329 cs
->types
= newtypes
;
1330 cs
->types
[cs
->ntypes
++] = wct
;
1334 - dupl - emit a duplicate of a bunch of sops
1335 == static sopno dupl(struct parse *p, sopno start, sopno finish);
1337 static sopno
/* start of duplicate */
1338 dupl(struct parse
*p
,
1339 sopno start
, /* from here */
1340 sopno finish
) /* to this less one */
1343 sopno len
= finish
- start
;
1345 assert(finish
>= start
);
1348 enlarge(p
, p
->ssize
+ len
); /* this many unexpected additions */
1349 assert(p
->ssize
>= p
->slen
+ len
);
1350 (void) memcpy((char *)(p
->strip
+ p
->slen
),
1351 (char *)(p
->strip
+ start
), (size_t)len
*sizeof(sop
));
1357 - doemit - emit a strip operator
1358 == static void doemit(struct parse *p, sop op, size_t opnd);
1360 * It might seem better to implement this as a macro with a function as
1361 * hard-case backup, but it's just too big and messy unless there are
1362 * some changes to the data structures. Maybe later.
1365 doemit(struct parse
*p
, sop op
, size_t opnd
)
1367 /* avoid making error situations worse */
1371 /* deal with oversize operands ("can't happen", more or less) */
1372 assert(opnd
< 1<<OPSHIFT
);
1374 /* deal with undersized strip */
1375 if (p
->slen
>= p
->ssize
)
1376 enlarge(p
, (p
->ssize
+1) / 2 * 3); /* +50% */
1377 assert(p
->slen
< p
->ssize
);
1379 /* finally, it's all reduced to the easy case */
1380 p
->strip
[p
->slen
++] = SOP(op
, opnd
);
1384 - doinsert - insert a sop into the strip
1385 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
1388 doinsert(struct parse
*p
, sop op
, size_t opnd
, sopno pos
)
1394 /* avoid making error situations worse */
1399 EMIT(op
, opnd
); /* do checks, ensure space */
1400 assert(HERE() == sn
+1);
1403 /* adjust paren pointers */
1405 for (i
= 1; i
< NPAREN
; i
++) {
1406 if (p
->pbegin
[i
] >= pos
) {
1409 if (p
->pend
[i
] >= pos
) {
1414 memmove((char *)&p
->strip
[pos
+1], (char *)&p
->strip
[pos
],
1415 (HERE()-pos
-1)*sizeof(sop
));
1420 - dofwd - complete a forward reference
1421 == static void dofwd(struct parse *p, sopno pos, sop value);
1424 dofwd(struct parse
*p
, sopno pos
, sop value
)
1426 /* avoid making error situations worse */
1430 assert(value
< 1<<OPSHIFT
);
1431 p
->strip
[pos
] = OP(p
->strip
[pos
]) | value
;
1435 - enlarge - enlarge the strip
1436 == static void enlarge(struct parse *p, sopno size);
1439 enlarge(struct parse
*p
, sopno size
)
1443 if (p
->ssize
>= size
)
1446 sp
= (sop
*)realloc(p
->strip
, size
*sizeof(sop
));
1448 SETERROR(REG_ESPACE
);
1456 - stripsnug - compact the strip
1457 == static void stripsnug(struct parse *p, struct re_guts *g);
1460 stripsnug(struct parse
*p
, struct re_guts
*g
)
1462 g
->nstates
= p
->slen
;
1463 g
->strip
= (sop
*)realloc((char *)p
->strip
, p
->slen
* sizeof(sop
));
1464 if (g
->strip
== NULL
) {
1465 SETERROR(REG_ESPACE
);
1466 g
->strip
= p
->strip
;
1471 - findmust - fill in must and mlen with longest mandatory literal string
1472 == static void findmust(struct parse *p, struct re_guts *g);
1474 * This algorithm could do fancy things like analyzing the operands of |
1475 * for common subsequences. Someday. This code is simple and finds most
1476 * of the interesting cases.
1478 * Note that must and mlen got initialized during setup.
1481 findmust(struct parse
*p
, struct re_guts
*g
)
1490 char buf
[MB_LEN_MAX
];
1493 struct __xlocale_st_runelocale
*rl
= p
->g
->loc
->__lc_ctype
;
1495 /* avoid making error situations worse */
1500 * It's not generally safe to do a ``char'' substring search on
1501 * multibyte character strings, but it's safe for at least
1502 * UTF-8 (see RFC 3629).
1504 if (rl
->__mb_cur_max
> 1 &&
1505 strcmp(rl
->_CurrentRuneLocale
.__encoding
, "UTF-8") != 0)
1508 /* find the longest OCHAR sequence in strip */
1512 scan
= g
->strip
+ 1;
1516 case OCHAR
: /* sequence member */
1517 if (newlen
== 0) { /* new sequence */
1518 memset(&mbs
, 0, sizeof(mbs
));
1519 newstart
= scan
- 1;
1521 clen
= wcrtomb_l(buf
, OPND(s
), &mbs
, p
->g
->loc
);
1522 if (clen
== (size_t)-1)
1526 case OPLUS_
: /* things that don't break one */
1530 case OQUEST_
: /* things that must be skipped */
1532 offset
= altoffset(scan
, offset
);
1537 /* assert() interferes w debug printouts */
1538 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1543 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1545 case OBOW
: /* things that break a sequence */
1552 if (newlen
> g
->mlen
) { /* ends one */
1556 g
->moffset
+= offset
;
1559 g
->moffset
= offset
;
1567 if (newlen
> g
->mlen
) { /* ends one */
1571 g
->moffset
+= offset
;
1574 g
->moffset
= offset
;
1583 case OANYOF
: /* may or may not invalidate offset */
1584 /* First, everything as OANY */
1585 if (newlen
> g
->mlen
) { /* ends one */
1589 g
->moffset
+= offset
;
1592 g
->moffset
= offset
;
1603 /* Anything here makes it impossible or too hard
1604 * to calculate the offset -- so we give up;
1605 * save the last known good offset, in case the
1606 * must sequence doesn't occur later.
1608 if (newlen
> g
->mlen
) { /* ends one */
1612 g
->moffset
+= offset
;
1614 g
->moffset
= offset
;
1620 } while (OP(s
) != OEND
);
1622 if (g
->mlen
== 0) { /* there isn't one */
1627 /* turn it into a character string */
1628 g
->must
= malloc((size_t)g
->mlen
+ 1);
1629 if (g
->must
== NULL
) { /* argh; just forget it */
1636 memset(&mbs
, 0, sizeof(mbs
));
1637 while (cp
< g
->must
+ g
->mlen
) {
1638 while (OP(s
= *scan
++) != OCHAR
)
1640 clen
= wcrtomb_l(cp
, OPND(s
), &mbs
, p
->g
->loc
);
1641 assert(clen
!= (size_t)-1);
1644 assert(cp
== g
->must
+ g
->mlen
);
1645 *cp
++ = '\0'; /* just on general principles */
1649 - altoffset - choose biggest offset among multiple choices
1650 == static int altoffset(sop *scan, int offset);
1652 * Compute, recursively if necessary, the largest offset among multiple
1656 altoffset(sop
*scan
, int offset
)
1662 /* If we gave up already on offsets, return */
1669 while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
) {
1678 try = altoffset(scan
, try);
1685 if (OP(s
) != O_QUEST
&& OP(s
) != O_CH
&&
1688 } while (OP(s
) != O_QUEST
&& OP(s
) != O_CH
);
1689 /* We must skip to the next position, or we'll
1690 * leave altoffset() too early.
1716 return largest
+offset
;
1720 - computejumps - compute char jumps for BM scan
1721 == static void computejumps(struct parse *p, struct re_guts *g);
1723 * This algorithm assumes g->must exists and is has size greater than
1724 * zero. It's based on the algorithm found on Computer Algorithms by
1727 * A char jump is the number of characters one needs to jump based on
1728 * the value of the character from the text that was mismatched.
1731 computejumps(struct parse
*p
, struct re_guts
*g
)
1736 /* Avoid making errors worse */
1740 g
->charjump
= (int*) malloc((NC
+ 1) * sizeof(int));
1741 if (g
->charjump
== NULL
) /* Not a fatal error */
1743 /* Adjust for signed chars, if necessary */
1744 g
->charjump
= &g
->charjump
[-(CHAR_MIN
)];
1746 /* If the character does not exist in the pattern, the jump
1747 * is equal to the number of characters in the pattern.
1749 for (ch
= CHAR_MIN
; ch
< (CHAR_MAX
+ 1); ch
++)
1750 g
->charjump
[ch
] = g
->mlen
;
1752 /* If the character does exist, compute the jump that would
1753 * take us to the last character in the pattern equal to it
1754 * (notice that we match right to left, so that last character
1755 * is the first one that would be matched).
1757 for (mindex
= 0; mindex
< g
->mlen
; mindex
++)
1758 g
->charjump
[(int)g
->must
[mindex
]] = g
->mlen
- mindex
- 1;
1762 - computematchjumps - compute match jumps for BM scan
1763 == static void computematchjumps(struct parse *p, struct re_guts *g);
1765 * This algorithm assumes g->must exists and is has size greater than
1766 * zero. It's based on the algorithm found on Computer Algorithms by
1769 * A match jump is the number of characters one needs to advance based
1770 * on the already-matched suffix.
1771 * Notice that all values here are minus (g->mlen-1), because of the way
1772 * the search algorithm works.
1775 computematchjumps(struct parse
*p
, struct re_guts
*g
)
1777 int mindex
; /* General "must" iterator */
1778 int suffix
; /* Keeps track of matching suffix */
1779 int ssuffix
; /* Keeps track of suffixes' suffix */
1780 int* pmatches
; /* pmatches[k] points to the next i
1781 * such that i+1...mlen is a substring
1782 * of k+1...k+mlen-i-1
1785 /* Avoid making errors worse */
1789 pmatches
= (int*) malloc(g
->mlen
* sizeof(unsigned int));
1790 if (pmatches
== NULL
) {
1791 g
->matchjump
= NULL
;
1795 g
->matchjump
= (int*) malloc(g
->mlen
* sizeof(unsigned int));
1796 if (g
->matchjump
== NULL
) /* Not a fatal error */
1799 /* Set maximum possible jump for each character in the pattern */
1800 for (mindex
= 0; mindex
< g
->mlen
; mindex
++)
1801 g
->matchjump
[mindex
] = 2*g
->mlen
- mindex
- 1;
1803 /* Compute pmatches[] */
1804 for (mindex
= g
->mlen
- 1, suffix
= g
->mlen
; mindex
>= 0;
1805 mindex
--, suffix
--) {
1806 pmatches
[mindex
] = suffix
;
1808 /* If a mismatch is found, interrupting the substring,
1809 * compute the matchjump for that position. If no
1810 * mismatch is found, then a text substring mismatched
1811 * against the suffix will also mismatch against the
1814 while (suffix
< g
->mlen
1815 && g
->must
[mindex
] != g
->must
[suffix
]) {
1816 g
->matchjump
[suffix
] = MIN(g
->matchjump
[suffix
],
1817 g
->mlen
- mindex
- 1);
1818 suffix
= pmatches
[suffix
];
1822 /* Compute the matchjump up to the last substring found to jump
1823 * to the beginning of the largest must pattern prefix matching
1826 for (mindex
= 0; mindex
<= suffix
; mindex
++)
1827 g
->matchjump
[mindex
] = MIN(g
->matchjump
[mindex
],
1828 g
->mlen
+ suffix
- mindex
);
1830 ssuffix
= pmatches
[suffix
];
1831 while (suffix
< g
->mlen
) {
1832 while (suffix
<= ssuffix
&& suffix
< g
->mlen
) {
1833 g
->matchjump
[suffix
] = MIN(g
->matchjump
[suffix
],
1834 g
->mlen
+ ssuffix
- suffix
);
1837 if (suffix
< g
->mlen
)
1838 ssuffix
= pmatches
[ssuffix
];
1845 - pluscount - count + nesting
1846 == static sopno pluscount(struct parse *p, struct re_guts *g);
1848 static sopno
/* nesting depth */
1849 pluscount(struct parse
*p
, struct re_guts
*g
)
1857 return(0); /* there may not be an OEND */
1859 scan
= g
->strip
+ 1;
1867 if (plusnest
> maxnest
)
1872 } while (OP(s
) != OEND
);