]> git.saurik.com Git - apple/libc.git/blob - regex/FreeBSD/regcomp.c.patch
a52baac56861a29ab5ef36980f03d099c5324f0f
[apple/libc.git] / regex / FreeBSD / regcomp.c.patch
1 --- regcomp.c.orig 2004-11-25 11:38:32.000000000 -0800
2 +++ regcomp.c 2005-04-05 14:46:18.000000000 -0700
3 @@ -43,6 +43,8 @@
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD: src/lib/libc/regex/regcomp.c,v 1.34 2004/10/03 15:42:59 stefanf Exp $");
6
7 +#include "xlocale_private.h"
8 +
9 #include <sys/types.h>
10 #include <stdio.h>
11 #include <string.h>
12 @@ -73,6 +75,9 @@
13 sopno ssize; /* malloced strip size (allocated) */
14 sopno slen; /* malloced strip length (used) */
15 int ncsalloc; /* number of csets allocated */
16 +#if __DARWIN_UNIX03
17 + int zerorepeats;
18 +#endif /* __DARWIN_UNIX03 */
19 struct re_guts *g;
20 # define NPAREN 10 /* we need to remember () 1-9 for back refs */
21 sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
22 @@ -97,7 +102,7 @@
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);
31 @@ -108,7 +113,7 @@
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);
40 @@ -227,10 +232,14 @@
41 p->end = p->next + len;
42 p->error = 0;
43 p->ncsalloc = 0;
44 +#if __DARWIN_UNIX03
45 + p->zerorepeats = 0;
46 +#endif /* __DARWIN_UNIX03 */
47 for (i = 0; i < NPAREN; i++) {
48 p->pbegin[i] = 0;
49 p->pend[i] = 0;
50 }
51 + g->loc = __current_locale();
52 g->sets = NULL;
53 g->ncsets = 0;
54 g->cflags = cflags;
55 @@ -308,8 +317,12 @@
56 conc = HERE();
57 while (MORE() && (c = PEEK()) != '|' && c != stop)
58 p_ere_exp(p);
59 +#if __DARWIN_UNIX03
60 + if (!p->zerorepeats) REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
61 + else p->zerorepeats--;
62 +#else
63 (void)REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
64 -
65 +#endif
66 if (!EAT('|'))
67 break; /* NOTE BREAK OUT */
68
69 @@ -417,7 +430,7 @@
70 ordinary(p, wc);
71 break;
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);
75 /* FALLTHROUGH */
76 default:
77 p->next--;
78 @@ -431,7 +444,7 @@
79 c = PEEK();
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 */
85 NEXT();
86
87 @@ -460,7 +473,7 @@
88 case '{':
89 count = p_count(p);
90 if (EAT(',')) {
91 - if (isdigit((uch)PEEK())) {
92 + if (isdigit_l((uch)PEEK(), p->g->loc)) {
93 count2 = p_count(p);
94 (void)REQUIRE(count <= count2, REG_BADBR);
95 } else /* single number with comma */
96 @@ -481,7 +494,7 @@
97 return;
98 c = PEEK();
99 if (!( c == '*' || c == '+' || c == '?' ||
100 - (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
101 + (c == '{' && MORE2() && isdigit_l((uch)PEEK2(), p->g->loc)) ) )
102 return;
103 SETERROR(REG_BADRPT);
104 }
105 @@ -494,7 +507,12 @@
106 p_str(p)
107 struct parse *p;
108 {
109 +#if __DARWIN_UNIX03
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 */
115 while (MORE())
116 ordinary(p, WGETNEXT());
117 }
118 @@ -534,8 +552,12 @@
119 p->g->iflags |= USEEOL;
120 p->g->neol++;
121 }
122 -
123 +#if __DARWIN_UNIX03
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 */
129 }
130
131 /*
132 @@ -609,12 +631,22 @@
133 i = (c&~BACKSL) - '0';
134 assert(i < NPAREN);
135 if (p->pend[i] != 0) {
136 +#if __DARWIN_UNIX03
137 + int skip = 1;
138 +#endif /* __DARWIN_UNIX03 */
139 assert(i <= p->g->nsub);
140 EMIT(OBACK_, i);
141 assert(p->pbegin[i] != 0);
142 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
143 assert(OP(p->strip[p->pend[i]]) == ORPAREN);
144 +#if __DARWIN_UNIX03
145 + if (OP(p->strip[p->pbegin[i]+skip]) == OBOL) {
146 + skip++; /* don't dup anchor in subexp */
147 + }
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 */
152 EMIT(O_BACK, i);
153 } else
154 SETERROR(REG_ESUBREG);
155 @@ -637,9 +669,10 @@
156 INSERT(OQUEST_, pos);
157 ASTERN(O_QUEST, pos);
158 } else if (EATTWO('\\', '{')) {
159 + (void)REQUIRE(MORE(), REG_EBRACE);
160 count = p_count(p);
161 if (EAT(',')) {
162 - if (MORE() && isdigit((uch)PEEK())) {
163 + if (MORE() && isdigit_l((uch)PEEK(), p->g->loc)) {
164 count2 = p_count(p);
165 (void)REQUIRE(count <= count2, REG_BADBR);
166 } else /* single number with comma */
167 @@ -670,7 +703,7 @@
168 int count = 0;
169 int ndigits = 0;
170
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');
174 ndigits++;
175 }
176 @@ -709,10 +742,22 @@
177 cs->icase = 1;
178 if (EAT('^'))
179 cs->invert = 1;
180 +#if __DARWIN_UNIX03
181 + if (PEEK2() != '-' && PEEK2() != ']') { /* Don't eat '-' or ']' if they're part of ranges
182 + * but do process [^-] */
183 if (EAT(']'))
184 CHadd(p, cs, ']');
185 else if (EAT('-'))
186 CHadd(p, cs, '-');
187 + }
188 + if (MORE() && !SEETWO('-',']')) /* Parse RE []-'] */
189 + p_b_term(p, cs);
190 +#else /* !__DARWIN_UNIX03 */
191 + if (EAT(']'))
192 + CHadd(p, cs, ']');
193 + else if (EAT('-'))
194 + CHadd(p, cs, '-');
195 +#endif /* __DARWIN_UNIX03 */
196 while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
197 p_b_term(p, cs);
198 if (EAT('-'))
199 @@ -725,7 +770,7 @@
200 if (cs->invert && p->g->cflags&REG_NEWLINE)
201 cs->bmp['\n' >> 3] |= 1 << ('\n' & 7);
202
203 - if ((ch = singleton(cs)) != OUT) { /* optimize singleton sets */
204 + if ((ch = singleton(cs, p->g->loc)) != OUT) { /* optimize singleton sets */
205 ordinary(p, ch);
206 freeset(p, cs);
207 } else
208 @@ -751,8 +796,16 @@
209 c = (MORE2()) ? PEEK2() : '\0';
210 break;
211 case '-':
212 +#if __DARWIN_UNIX03
213 + if (PEEK2() != '-') { /* Allow [---] */
214 + SETERROR(REG_ERANGE);
215 + return; /* NOTE RETURN */
216 + } else
217 + c = '-';
218 +#else /* !__DARWIN_UNIX03 */
219 SETERROR(REG_ERANGE);
220 return; /* NOTE RETURN */
221 +#endif /* __DARWIN_UNIX03 */
222 break;
223 default:
224 c = '\0';
225 @@ -773,7 +826,11 @@
226 NEXT2();
227 (void)REQUIRE(MORE(), REG_EBRACK);
228 c = PEEK();
229 +#if __DARWIN_UNIX03
230 + REQUIRE(c != '-', REG_ECOLLATE); /* allow [=]=] */
231 +#else /* !__DARWIN_UNIX03 */
232 (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
233 +#endif /* __DARWIN_UNIX03 */
234 p_b_eclass(p, cs);
235 (void)REQUIRE(MORE(), REG_EBRACK);
236 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
237 @@ -792,14 +849,14 @@
238 if (start == finish)
239 CHadd(p, cs, start);
240 else {
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);
245 } else {
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
253 )
254 CHadd(p, cs, i);
255 }
256 @@ -823,7 +880,7 @@
257 wctype_t wct;
258 char clname[16];
259
260 - while (MORE() && isalpha((uch)PEEK()))
261 + while (MORE() && isalpha_l((uch)PEEK(), p->g->loc))
262 NEXT();
263 len = p->next - sp;
264 if (len >= sizeof(clname) - 1) {
265 @@ -832,7 +889,7 @@
266 }
267 memcpy(clname, sp, len);
268 clname[len] = '\0';
269 - if ((wct = wctype(clname)) == 0) {
270 + if ((wct = wctype_l(clname, p->g->loc)) == 0) {
271 SETERROR(REG_ECTYPE);
272 return;
273 }
274 @@ -842,16 +899,40 @@
275 /*
276 - p_b_eclass - parse an equivalence-class name and deal with it
277 == static void p_b_eclass(struct parse *p, cset *cs);
278 - *
279 - * This implementation is incomplete. xxx
280 */
281 static void
282 p_b_eclass(p, cs)
283 struct parse *p;
284 cset *cs;
285 {
286 - wint_t c;
287 -
288 + char *sp = p->next;
289 + int len, ec;
290 + mbstate_t mbs;
291 + int *newequiv_classes;
292 + wint_t c;
293 +
294 + while (MORE() && !SEETWO('=', ']'))
295 + NEXT();
296 + if (!MORE()) {
297 + SETERROR(REG_EBRACK);
298 + return;
299 + }
300 + len = p->next - sp;
301 + memset(&mbs, 0, sizeof(mbs));
302 + ec = __collate_equiv_class(sp, len, &mbs, p->g->loc);
303 + if (ec > 0) {
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);
308 + return;
309 + }
310 + cs->equiv_classes = newequiv_classes;
311 + cs->equiv_classes[cs->nequiv_classes++] = ec;
312 + return;
313 + }
314 + /* not an equivalence class, so fallback to a collating element */
315 + p->next = sp;
316 c = p_b_coll_elem(p, '=');
317 CHadd(p, cs, c);
318 }
319 @@ -889,7 +970,7 @@
320 struct cname *cp;
321 int len;
322 mbstate_t mbs;
323 - wchar_t wc;
324 + wchar_t wbuf[16];
325 size_t clen;
326
327 while (MORE() && !SEETWO(endc, ']'))
328 @@ -903,9 +984,10 @@
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);
336 + if (clen == 1)
337 + return (*wbuf); /* single character */
338 + else if (clen == (size_t)-1)
339 SETERROR(REG_ILLSEQ);
340 else
341 SETERROR(REG_ECOLLATE); /* neither */
342 @@ -914,17 +996,18 @@
343
344 /*
345 - othercase - return the case counterpart of an alphabetic
346 - == static char othercase(int ch);
347 + == static char othercase(int ch, locale_t loc);
348 */
349 static wint_t /* if no counterpart, return ch */
350 -othercase(ch)
351 +othercase(ch, loc)
352 wint_t ch;
353 +locale_t loc;
354 {
355 - assert(iswalpha(ch));
356 - if (iswupper(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 */
366 return(ch);
367 }
368 @@ -946,10 +1029,10 @@
369 size_t n;
370 mbstate_t mbs;
371
372 - assert(othercase(ch) != ch); /* p_bracket() would recurse */
373 + assert(othercase(ch, p->g->loc) != ch); /* p_bracket() would recurse */
374 p->next = bracket;
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);
379 bracket[n] = ']';
380 bracket[n + 1] = '\0';
381 @@ -971,7 +1054,7 @@
382 {
383 cset *cs;
384
385 - if ((p->g->cflags&REG_ICASE) && iswalpha(ch) && othercase(ch) != ch)
386 + if ((p->g->cflags&REG_ICASE) && iswalpha_l(ch, p->g->loc) && othercase(ch, p->g->loc) != ch)
387 bothcases(p, ch);
388 else if ((ch & OPDMASK) == ch)
389 EMIT(OCHAR, 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 */
394 +#if __DARWIN_UNIX03
395 + p->zerorepeats++;
396 +#endif /* __DARWIN_UNIX03 */
397 break;
398 + case REP(0, INF): /* as x{1,}? */
399 +#if __DARWIN_UNIX03
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);
406 + break;
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 @@
415 /* done */
416 break;
417 case REP(1, N): /* as x?x{1,n-1} */
418 +#if __DARWIN_UNIX03
419 + INSERT(OQUEST_, start);
420 + ASTERN(O_QUEST, start);
421 +#else /* !__DARWIN_UNIX03 */
422 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
423 INSERT(OCH_, start);
424 ASTERN(OOR1, start);
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 @@
434 size_t n;
435
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);
441 return (0);
442 @@ -1172,13 +1272,14 @@
443 - returning it if so, otherwise returning OUT.
444 */
445 static wint_t
446 -singleton(cs)
447 +singleton(cs, loc)
448 cset *cs;
449 +locale_t loc;
450 {
451 wint_t i, s, n;
452
453 for (i = n = 0; i < NC; i++)
454 - if (CHIN(cs, i)) {
455 + if (CHIN(cs, i, loc)) {
456 n++;
457 s = i;
458 }
459 @@ -1215,9 +1316,9 @@
460 cs->wides[cs->nwides++] = ch;
461 }
462 if (cs->icase) {
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);
469 }
470 }
471 @@ -1262,7 +1363,7 @@
472 wctype_t *newtypes;
473
474 for (i = 0; i < NC; i++)
475 - if (iswctype(i, wct))
476 + if (iswctype_l(i, wct, p->g->loc))
477 CHadd(p, cs, i);
478 newtypes = realloc(cs->types, (cs->ntypes + 1) *
479 sizeof(*cs->types));
480 @@ -1451,6 +1552,7 @@
481 char buf[MB_LEN_MAX];
482 size_t clen;
483 mbstate_t mbs;
484 + struct __xlocale_st_runelocale *rl = p->g->loc->__lc_ctype;
485
486 /* avoid making error situations worse */
487 if (p->error != 0)
488 @@ -1461,8 +1563,8 @@
489 * multibyte character strings, but it's safe for at least
490 * UTF-8 (see RFC 3629).
491 */
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)
496 return;
497
498 /* find the longest OCHAR sequence in strip */
499 @@ -1478,7 +1580,7 @@
500 memset(&mbs, 0, sizeof(mbs));
501 newstart = scan - 1;
502 }
503 - clen = wcrtomb(buf, OPND(s), &mbs);
504 + clen = wcrtomb_l(buf, OPND(s), &mbs, p->g->loc);
505 if (clen == (size_t)-1)
506 goto toohard;
507 newlen += clen;
508 @@ -1597,7 +1699,7 @@
509 while (cp < g->must + g->mlen) {
510 while (OP(s = *scan++) != OCHAR)
511 continue;
512 - clen = wcrtomb(cp, OPND(s), &mbs);
513 + clen = wcrtomb_l(cp, OPND(s), &mbs, p->g->loc);
514 assert(clen != (size_t)-1);
515 cp += clen;
516 }