1 --- regcomp.c.orig 2004-11-25 11:38:32.000000000 -0800
2 +++ regcomp.c 2005-04-05 14:46:18.000000000 -0700
5 __FBSDID("$FreeBSD: src/lib/libc/regex/regcomp.c,v 1.34 2004/10/03 15:42:59 stefanf Exp $");
7 +#include "xlocale_private.h"
13 sopno ssize; /* malloced strip size (allocated) */
14 sopno slen; /* malloced strip length (used) */
15 int ncsalloc; /* number of csets allocated */
18 +#endif /* __DARWIN_UNIX03 */
20 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
21 sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
23 static void p_b_eclass(struct parse *p, cset *cs);
24 static wint_t p_b_symbol(struct parse *p);
25 static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
26 -static wint_t othercase(wint_t ch);
27 +static wint_t othercase(wint_t ch, locale_t loc);
28 static void bothcases(struct parse *p, wint_t ch);
29 static void ordinary(struct parse *p, wint_t ch);
30 static void nonnewline(struct parse *p);
32 static void CHadd(struct parse *p, cset *cs, wint_t ch);
33 static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max);
34 static void CHaddtype(struct parse *p, cset *cs, wctype_t wct);
35 -static wint_t singleton(cset *cs);
36 +static wint_t singleton(cset *cs, locale_t loc);
37 static sopno dupl(struct parse *p, sopno start, sopno finish);
38 static void doemit(struct parse *p, sop op, size_t opnd);
39 static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
41 p->end = p->next + len;
46 +#endif /* __DARWIN_UNIX03 */
47 for (i = 0; i < NPAREN; i++) {
51 + g->loc = __current_locale();
57 while (MORE() && (c = PEEK()) != '|' && c != stop)
60 + if (!p->zerorepeats) REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
61 + else p->zerorepeats--;
63 (void)REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
67 break; /* NOTE BREAK OUT */
72 case '{': /* okay as ordinary except if digit follows */
73 - (void)REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
74 + (void)REQUIRE(!MORE() || !isdigit_l((uch)PEEK(), p->g->loc), REG_BADRPT);
80 /* we call { a repetition if followed by a digit */
81 if (!( c == '*' || c == '+' || c == '?' ||
82 - (c == '{' && MORE2() && isdigit((uch)PEEK2())) ))
83 + (c == '{' && MORE2() && isdigit_l((uch)PEEK2(), p->g->loc)) ))
84 return; /* no repetition, we're done */
91 - if (isdigit((uch)PEEK())) {
92 + if (isdigit_l((uch)PEEK(), p->g->loc)) {
94 (void)REQUIRE(count <= count2, REG_BADBR);
95 } else /* single number with comma */
99 if (!( c == '*' || c == '+' || c == '?' ||
100 - (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
101 + (c == '{' && MORE2() && isdigit_l((uch)PEEK2(), p->g->loc)) ) )
103 SETERROR(REG_BADRPT);
110 + if (!p->zerorepeats) REQUIRE(MORE(), REG_EMPTY);
111 + else p->zerorepeats--;
112 +#else /* !__DARWIN_UNIX03 */
113 (void)REQUIRE(MORE(), REG_EMPTY);
114 +#endif /* __DARWIN_UNIX03 */
116 ordinary(p, WGETNEXT());
119 p->g->iflags |= USEEOL;
124 + if (!p->zerorepeats) REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
125 + else p->zerorepeats--;
126 +#else /* !__DARWIN_UNIX03 */
127 (void)REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
128 +#endif /* __DARWIN_UNIX03 */
132 @@ -609,12 +631,22 @@
133 i = (c&~BACKSL) - '0';
135 if (p->pend[i] != 0) {
138 +#endif /* __DARWIN_UNIX03 */
139 assert(i <= p->g->nsub);
141 assert(p->pbegin[i] != 0);
142 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
143 assert(OP(p->strip[p->pend[i]]) == ORPAREN);
145 + if (OP(p->strip[p->pbegin[i]+skip]) == OBOL) {
146 + skip++; /* don't dup anchor in subexp */
148 + (void) dupl(p, p->pbegin[i]+skip, p->pend[i]);
149 +#else /* !__DARWIN_UNIX03 */
150 (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
151 +#endif /* __DARWIN_UNIX03 */
154 SETERROR(REG_ESUBREG);
156 INSERT(OQUEST_, pos);
157 ASTERN(O_QUEST, pos);
158 } else if (EATTWO('\\', '{')) {
159 + (void)REQUIRE(MORE(), REG_EBRACE);
162 - if (MORE() && isdigit((uch)PEEK())) {
163 + if (MORE() && isdigit_l((uch)PEEK(), p->g->loc)) {
165 (void)REQUIRE(count <= count2, REG_BADBR);
166 } else /* single number with comma */
171 - while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
172 + while (MORE() && isdigit_l((uch)PEEK(), p->g->loc) && count <= DUPMAX) {
173 count = count*10 + (GETNEXT() - '0');
176 @@ -709,10 +742,22 @@
181 + if (PEEK2() != '-' && PEEK2() != ']') { /* Don't eat '-' or ']' if they're part of ranges
182 + * but do process [^-] */
188 + if (MORE() && !SEETWO('-',']')) /* Parse RE []-'] */
190 +#else /* !__DARWIN_UNIX03 */
195 +#endif /* __DARWIN_UNIX03 */
196 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
200 if (cs->invert && p->g->cflags®_NEWLINE)
201 cs->bmp['\n' >> 3] |= 1 << ('\n' & 7);
203 - if ((ch = singleton(cs)) != OUT) { /* optimize singleton sets */
204 + if ((ch = singleton(cs, p->g->loc)) != OUT) { /* optimize singleton sets */
209 c = (MORE2()) ? PEEK2() : '\0';
213 + if (PEEK2() != '-') { /* Allow [---] */
214 + SETERROR(REG_ERANGE);
215 + return; /* NOTE RETURN */
218 +#else /* !__DARWIN_UNIX03 */
219 SETERROR(REG_ERANGE);
220 return; /* NOTE RETURN */
221 +#endif /* __DARWIN_UNIX03 */
227 (void)REQUIRE(MORE(), REG_EBRACK);
230 + REQUIRE(c != '-', REG_ECOLLATE); /* allow [=]=] */
231 +#else /* !__DARWIN_UNIX03 */
232 (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
233 +#endif /* __DARWIN_UNIX03 */
235 (void)REQUIRE(MORE(), REG_EBRACK);
236 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
237 @@ -792,14 +849,14 @@
241 - if (__collate_load_error) {
242 + if (p->g->loc->__collate_load_error) {
243 (void)REQUIRE((uch)start <= (uch)finish, REG_ERANGE);
244 CHaddrange(p, cs, start, finish);
246 - (void)REQUIRE(__collate_range_cmp(start, finish) <= 0, REG_ERANGE);
247 + (void)REQUIRE(__collate_range_cmp(start, finish, p->g->loc) <= 0, REG_ERANGE);
248 for (i = 0; i <= UCHAR_MAX; i++) {
249 - if ( __collate_range_cmp(start, i) <= 0
250 - && __collate_range_cmp(i, finish) <= 0
251 + if ( __collate_range_cmp(start, i, p->g->loc) <= 0
252 + && __collate_range_cmp(i, finish, p->g->loc) <= 0
260 - while (MORE() && isalpha((uch)PEEK()))
261 + while (MORE() && isalpha_l((uch)PEEK(), p->g->loc))
264 if (len >= sizeof(clname) - 1) {
267 memcpy(clname, sp, len);
269 - if ((wct = wctype(clname)) == 0) {
270 + if ((wct = wctype_l(clname, p->g->loc)) == 0) {
271 SETERROR(REG_ECTYPE);
274 @@ -842,16 +899,40 @@
276 - p_b_eclass - parse an equivalence-class name and deal with it
277 == static void p_b_eclass(struct parse *p, cset *cs);
279 - * This implementation is incomplete. xxx
288 + char *sp = p->next;
291 + int *newequiv_classes;
294 + while (MORE() && !SEETWO('=', ']'))
297 + SETERROR(REG_EBRACK);
300 + len = p->next - sp;
301 + memset(&mbs, 0, sizeof(mbs));
302 + ec = __collate_equiv_class(sp, len, &mbs, p->g->loc);
304 + newequiv_classes = realloc(cs->equiv_classes,
305 + (cs->nequiv_classes + 1) * sizeof(*cs->equiv_classes));
306 + if (newequiv_classes == NULL) {
307 + SETERROR(REG_ESPACE);
310 + cs->equiv_classes = newequiv_classes;
311 + cs->equiv_classes[cs->nequiv_classes++] = ec;
314 + /* not an equivalence class, so fallback to a collating element */
316 c = p_b_coll_elem(p, '=');
327 while (MORE() && !SEETWO(endc, ']'))
329 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
330 return(cp->code); /* known name */
331 memset(&mbs, 0, sizeof(mbs));
332 - if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len)
333 - return (wc); /* single character */
334 - else if (clen == (size_t)-1 || clen == (size_t)-2)
335 + clen = __collate_collating_symbol(wbuf, 16, sp, len, &mbs, p->g->loc);
337 + return (*wbuf); /* single character */
338 + else if (clen == (size_t)-1)
339 SETERROR(REG_ILLSEQ);
341 SETERROR(REG_ECOLLATE); /* neither */
342 @@ -914,17 +996,18 @@
345 - othercase - return the case counterpart of an alphabetic
346 - == static char othercase(int ch);
347 + == static char othercase(int ch, locale_t loc);
349 static wint_t /* if no counterpart, return ch */
355 - assert(iswalpha(ch));
357 - return(towlower(ch));
358 - else if (iswlower(ch))
359 - return(towupper(ch));
360 + assert(iswalpha_l(ch, loc));
361 + if (iswupper_l(ch, loc))
362 + return(towlower_l(ch, loc));
363 + else if (iswlower_l(ch, loc))
364 + return(towupper_l(ch, loc));
365 else /* peculiar, but could happen */
368 @@ -946,10 +1029,10 @@
372 - assert(othercase(ch) != ch); /* p_bracket() would recurse */
373 + assert(othercase(ch, p->g->loc) != ch); /* p_bracket() would recurse */
375 memset(&mbs, 0, sizeof(mbs));
376 - n = wcrtomb(bracket, ch, &mbs);
377 + n = wcrtomb_l(bracket, ch, &mbs, p->g->loc);
378 assert(n != (size_t)-1);
380 bracket[n + 1] = '\0';
385 - if ((p->g->cflags®_ICASE) && iswalpha(ch) && othercase(ch) != ch)
386 + if ((p->g->cflags®_ICASE) && iswalpha_l(ch, p->g->loc) && othercase(ch, p->g->loc) != ch)
388 else if ((ch & OPDMASK) == ch)
390 @@ -1039,10 +1122,22 @@
391 switch (REP(MAP(from), MAP(to))) {
392 case REP(0, 0): /* must be user doing this */
393 DROP(finish-start); /* drop the operand */
396 +#endif /* __DARWIN_UNIX03 */
398 + case REP(0, INF): /* as x{1,}? */
400 + /* this case does not require the (y|) trick, noKLUDGE */
401 + /* Just like * =+? */
402 + INSERT(OPLUS_, start);
403 + ASTERN(O_PLUS, start);
404 + INSERT(OQUEST_, start);
405 + ASTERN(O_QUEST, start);
407 +#endif /* __DARWIN_UNIX03 */
408 case REP(0, 1): /* as x{1,1}? */
409 case REP(0, N): /* as x{1,n}? */
410 - case REP(0, INF): /* as x{1,}? */
411 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
412 INSERT(OCH_, start); /* offset is wrong... */
413 repeat(p, start+1, 1, to);
414 @@ -1056,6 +1151,10 @@
417 case REP(1, N): /* as x?x{1,n-1} */
419 + INSERT(OQUEST_, start);
420 + ASTERN(O_QUEST, start);
421 +#else /* !__DARWIN_UNIX03 */
422 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
425 @@ -1063,6 +1162,7 @@
426 EMIT(OOR2, 0); /* offset very wrong... */
427 AHEAD(THERE()); /* ...so fix it */
428 ASTERN(O_CH, THERETHERE());
429 +#endif /* __DARWIN_UNIX03 */
430 copy = dupl(p, start+1, finish+1);
431 assert(copy == finish+4);
432 repeat(p, copy, 1, to-1);
433 @@ -1099,7 +1199,7 @@
436 memset(&mbs, 0, sizeof(mbs));
437 - n = mbrtowc(&wc, p->next, p->end - p->next, &mbs);
438 + n = mbrtowc_l(&wc, p->next, p->end - p->next, &mbs, p->g->loc);
439 if (n == (size_t)-1 || n == (size_t)-2) {
440 SETERROR(REG_ILLSEQ);
442 @@ -1172,13 +1272,14 @@
443 - returning it if so, otherwise returning OUT.
453 for (i = n = 0; i < NC; i++)
455 + if (CHIN(cs, i, loc)) {
459 @@ -1215,9 +1316,9 @@
460 cs->wides[cs->nwides++] = ch;
463 - if ((nch = towlower(ch)) < NC)
464 + if ((nch = towlower_l(ch, p->g->loc)) < NC)
465 cs->bmp[nch >> 3] |= 1 << (nch & 7);
466 - if ((nch = towupper(ch)) < NC)
467 + if ((nch = towupper_l(ch, p->g->loc)) < NC)
468 cs->bmp[nch >> 3] |= 1 << (nch & 7);
471 @@ -1262,7 +1363,7 @@
474 for (i = 0; i < NC; i++)
475 - if (iswctype(i, wct))
476 + if (iswctype_l(i, wct, p->g->loc))
478 newtypes = realloc(cs->types, (cs->ntypes + 1) *
480 @@ -1451,6 +1552,7 @@
481 char buf[MB_LEN_MAX];
484 + struct __xlocale_st_runelocale *rl = p->g->loc->__lc_ctype;
486 /* avoid making error situations worse */
488 @@ -1461,8 +1563,8 @@
489 * multibyte character strings, but it's safe for at least
490 * UTF-8 (see RFC 3629).
492 - if (MB_CUR_MAX > 1 &&
493 - strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0)
494 + if (rl->__mb_cur_max > 1 &&
495 + strcmp(rl->_CurrentRuneLocale.__encoding, "UTF-8") != 0)
498 /* find the longest OCHAR sequence in strip */
499 @@ -1478,7 +1580,7 @@
500 memset(&mbs, 0, sizeof(mbs));
503 - clen = wcrtomb(buf, OPND(s), &mbs);
504 + clen = wcrtomb_l(buf, OPND(s), &mbs, p->g->loc);
505 if (clen == (size_t)-1)
508 @@ -1597,7 +1699,7 @@
509 while (cp < g->must + g->mlen) {
510 while (OP(s = *scan++) != OCHAR)
512 - clen = wcrtomb(cp, OPND(s), &mbs);
513 + clen = wcrtomb_l(cp, OPND(s), &mbs, p->g->loc);
514 assert(clen != (size_t)-1);