Commit | Line | Data |
98994639 |
1 | /* locale.c |
2 | * |
1129b882 |
3 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
4 | * 2002, 2003, 2005, 2006, 2007, 2008 by Larry Wall and others |
98994639 |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. |
8 | * |
9 | */ |
10 | |
11 | /* |
4ac71550 |
12 | * A Elbereth Gilthoniel, |
13 | * silivren penna míriel |
14 | * o menel aglar elenath! |
15 | * Na-chaered palan-díriel |
16 | * o galadhremmin ennorath, |
17 | * Fanuilos, le linnathon |
18 | * nef aear, si nef aearon! |
19 | * |
20 | * [p.238 of _The Lord of the Rings_, II/i: "Many Meetings"] |
98994639 |
21 | */ |
22 | |
166f8a29 |
23 | /* utility functions for handling locale-specific stuff like what |
24 | * character represents the decimal point. |
25 | */ |
26 | |
98994639 |
27 | #include "EXTERN.h" |
28 | #define PERL_IN_LOCALE_C |
29 | #include "perl.h" |
30 | |
31 | #ifdef I_LOCALE |
32 | # include <locale.h> |
33 | #endif |
34 | |
b310b053 |
35 | #ifdef I_LANGINFO |
36 | # include <langinfo.h> |
37 | #endif |
38 | |
a4af207c |
39 | #include "reentr.h" |
40 | |
27da23d5 |
41 | #if defined(USE_LOCALE_NUMERIC) || defined(USE_LOCALE_COLLATE) |
98994639 |
42 | /* |
43 | * Standardize the locale name from a string returned by 'setlocale'. |
44 | * |
45 | * The standard return value of setlocale() is either |
46 | * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL |
47 | * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL |
48 | * (the space-separated values represent the various sublocales, |
49 | * in some unspecificed order) |
50 | * |
51 | * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n", |
52 | * which is harmful for further use of the string in setlocale(). |
53 | * |
54 | */ |
55 | STATIC char * |
56 | S_stdize_locale(pTHX_ char *locs) |
57 | { |
7452cf6a |
58 | const char * const s = strchr(locs, '='); |
98994639 |
59 | bool okay = TRUE; |
60 | |
7918f24d |
61 | PERL_ARGS_ASSERT_STDIZE_LOCALE; |
62 | |
8772537c |
63 | if (s) { |
64 | const char * const t = strchr(s, '.'); |
98994639 |
65 | okay = FALSE; |
8772537c |
66 | if (t) { |
67 | const char * const u = strchr(t, '\n'); |
68 | if (u && (u[1] == 0)) { |
69 | const STRLEN len = u - s; |
70 | Move(s + 1, locs, len, char); |
71 | locs[len] = 0; |
72 | okay = TRUE; |
98994639 |
73 | } |
74 | } |
75 | } |
76 | |
77 | if (!okay) |
78 | Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs); |
79 | |
80 | return locs; |
81 | } |
27da23d5 |
82 | #endif |
98994639 |
83 | |
84 | void |
85 | Perl_set_numeric_radix(pTHX) |
86 | { |
87 | #ifdef USE_LOCALE_NUMERIC |
97aff369 |
88 | dVAR; |
98994639 |
89 | # ifdef HAS_LOCALECONV |
7452cf6a |
90 | const struct lconv* const lc = localeconv(); |
98994639 |
91 | |
98994639 |
92 | if (lc && lc->decimal_point) { |
93 | if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) { |
94 | SvREFCNT_dec(PL_numeric_radix_sv); |
a0714e2c |
95 | PL_numeric_radix_sv = NULL; |
98994639 |
96 | } |
97 | else { |
98 | if (PL_numeric_radix_sv) |
99 | sv_setpv(PL_numeric_radix_sv, lc->decimal_point); |
100 | else |
101 | PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0); |
102 | } |
103 | } |
104 | else |
a0714e2c |
105 | PL_numeric_radix_sv = NULL; |
98994639 |
106 | # endif /* HAS_LOCALECONV */ |
107 | #endif /* USE_LOCALE_NUMERIC */ |
108 | } |
109 | |
110 | /* |
111 | * Set up for a new numeric locale. |
112 | */ |
113 | void |
8772537c |
114 | Perl_new_numeric(pTHX_ const char *newnum) |
98994639 |
115 | { |
116 | #ifdef USE_LOCALE_NUMERIC |
97aff369 |
117 | dVAR; |
98994639 |
118 | |
119 | if (! newnum) { |
43c5f42d |
120 | Safefree(PL_numeric_name); |
121 | PL_numeric_name = NULL; |
98994639 |
122 | PL_numeric_standard = TRUE; |
123 | PL_numeric_local = TRUE; |
124 | return; |
125 | } |
126 | |
127 | if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) { |
128 | Safefree(PL_numeric_name); |
129 | PL_numeric_name = stdize_locale(savepv(newnum)); |
770526c1 |
130 | PL_numeric_standard = ((*newnum == 'C' && newnum[1] == '\0') |
131 | || strEQ(newnum, "POSIX")); |
98994639 |
132 | PL_numeric_local = TRUE; |
133 | set_numeric_radix(); |
134 | } |
135 | |
136 | #endif /* USE_LOCALE_NUMERIC */ |
137 | } |
138 | |
139 | void |
140 | Perl_set_numeric_standard(pTHX) |
141 | { |
142 | #ifdef USE_LOCALE_NUMERIC |
97aff369 |
143 | dVAR; |
98994639 |
144 | |
145 | if (! PL_numeric_standard) { |
146 | setlocale(LC_NUMERIC, "C"); |
147 | PL_numeric_standard = TRUE; |
148 | PL_numeric_local = FALSE; |
149 | set_numeric_radix(); |
150 | } |
151 | |
152 | #endif /* USE_LOCALE_NUMERIC */ |
153 | } |
154 | |
155 | void |
156 | Perl_set_numeric_local(pTHX) |
157 | { |
158 | #ifdef USE_LOCALE_NUMERIC |
97aff369 |
159 | dVAR; |
98994639 |
160 | |
161 | if (! PL_numeric_local) { |
162 | setlocale(LC_NUMERIC, PL_numeric_name); |
163 | PL_numeric_standard = FALSE; |
164 | PL_numeric_local = TRUE; |
165 | set_numeric_radix(); |
166 | } |
167 | |
168 | #endif /* USE_LOCALE_NUMERIC */ |
169 | } |
170 | |
171 | /* |
172 | * Set up for a new ctype locale. |
173 | */ |
174 | void |
8772537c |
175 | Perl_new_ctype(pTHX_ const char *newctype) |
98994639 |
176 | { |
177 | #ifdef USE_LOCALE_CTYPE |
27da23d5 |
178 | dVAR; |
98994639 |
179 | int i; |
180 | |
7918f24d |
181 | PERL_ARGS_ASSERT_NEW_CTYPE; |
182 | |
98994639 |
183 | for (i = 0; i < 256; i++) { |
184 | if (isUPPER_LC(i)) |
185 | PL_fold_locale[i] = toLOWER_LC(i); |
186 | else if (isLOWER_LC(i)) |
187 | PL_fold_locale[i] = toUPPER_LC(i); |
188 | else |
189 | PL_fold_locale[i] = i; |
190 | } |
191 | |
192 | #endif /* USE_LOCALE_CTYPE */ |
7918f24d |
193 | PERL_ARGS_ASSERT_NEW_CTYPE; |
8772537c |
194 | PERL_UNUSED_ARG(newctype); |
96a5add6 |
195 | PERL_UNUSED_CONTEXT; |
98994639 |
196 | } |
197 | |
198 | /* |
199 | * Set up for a new collation locale. |
200 | */ |
201 | void |
8772537c |
202 | Perl_new_collate(pTHX_ const char *newcoll) |
98994639 |
203 | { |
204 | #ifdef USE_LOCALE_COLLATE |
97aff369 |
205 | dVAR; |
98994639 |
206 | |
207 | if (! newcoll) { |
208 | if (PL_collation_name) { |
209 | ++PL_collation_ix; |
210 | Safefree(PL_collation_name); |
211 | PL_collation_name = NULL; |
212 | } |
213 | PL_collation_standard = TRUE; |
214 | PL_collxfrm_base = 0; |
215 | PL_collxfrm_mult = 2; |
216 | return; |
217 | } |
218 | |
219 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
220 | ++PL_collation_ix; |
221 | Safefree(PL_collation_name); |
222 | PL_collation_name = stdize_locale(savepv(newcoll)); |
770526c1 |
223 | PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0') |
224 | || strEQ(newcoll, "POSIX")); |
98994639 |
225 | |
226 | { |
227 | /* 2: at most so many chars ('a', 'b'). */ |
228 | /* 50: surely no system expands a char more. */ |
229 | #define XFRMBUFSIZE (2 * 50) |
230 | char xbuf[XFRMBUFSIZE]; |
8772537c |
231 | const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); |
232 | const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); |
233 | const SSize_t mult = fb - fa; |
98994639 |
234 | if (mult < 1) |
235 | Perl_croak(aTHX_ "strxfrm() gets absurd"); |
eb160463 |
236 | PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0; |
98994639 |
237 | PL_collxfrm_mult = mult; |
238 | } |
239 | } |
240 | |
241 | #endif /* USE_LOCALE_COLLATE */ |
242 | } |
243 | |
244 | /* |
245 | * Initialize locale awareness. |
246 | */ |
247 | int |
248 | Perl_init_i18nl10n(pTHX_ int printwarn) |
249 | { |
250 | int ok = 1; |
251 | /* returns |
252 | * 1 = set ok or not applicable, |
253 | * 0 = fallback to C locale, |
254 | * -1 = fallback to C locale failed |
255 | */ |
256 | |
257 | #if defined(USE_LOCALE) |
97aff369 |
258 | dVAR; |
98994639 |
259 | |
260 | #ifdef USE_LOCALE_CTYPE |
261 | char *curctype = NULL; |
262 | #endif /* USE_LOCALE_CTYPE */ |
263 | #ifdef USE_LOCALE_COLLATE |
264 | char *curcoll = NULL; |
265 | #endif /* USE_LOCALE_COLLATE */ |
266 | #ifdef USE_LOCALE_NUMERIC |
267 | char *curnum = NULL; |
268 | #endif /* USE_LOCALE_NUMERIC */ |
269 | #ifdef __GLIBC__ |
7452cf6a |
270 | char * const language = PerlEnv_getenv("LANGUAGE"); |
98994639 |
271 | #endif |
7452cf6a |
272 | char * const lc_all = PerlEnv_getenv("LC_ALL"); |
273 | char * const lang = PerlEnv_getenv("LANG"); |
98994639 |
274 | bool setlocale_failure = FALSE; |
275 | |
276 | #ifdef LOCALE_ENVIRON_REQUIRED |
277 | |
278 | /* |
279 | * Ultrix setlocale(..., "") fails if there are no environment |
280 | * variables from which to get a locale name. |
281 | */ |
282 | |
283 | bool done = FALSE; |
284 | |
285 | #ifdef LC_ALL |
286 | if (lang) { |
287 | if (setlocale(LC_ALL, "")) |
288 | done = TRUE; |
289 | else |
290 | setlocale_failure = TRUE; |
291 | } |
292 | if (!setlocale_failure) { |
293 | #ifdef USE_LOCALE_CTYPE |
56279a21 |
294 | Safefree(curctype); |
98994639 |
295 | if (! (curctype = |
296 | setlocale(LC_CTYPE, |
297 | (!done && (lang || PerlEnv_getenv("LC_CTYPE"))) |
bd61b366 |
298 | ? "" : NULL))) |
98994639 |
299 | setlocale_failure = TRUE; |
300 | else |
301 | curctype = savepv(curctype); |
302 | #endif /* USE_LOCALE_CTYPE */ |
303 | #ifdef USE_LOCALE_COLLATE |
56279a21 |
304 | Safefree(curcoll); |
98994639 |
305 | if (! (curcoll = |
306 | setlocale(LC_COLLATE, |
307 | (!done && (lang || PerlEnv_getenv("LC_COLLATE"))) |
bd61b366 |
308 | ? "" : NULL))) |
98994639 |
309 | setlocale_failure = TRUE; |
310 | else |
311 | curcoll = savepv(curcoll); |
312 | #endif /* USE_LOCALE_COLLATE */ |
313 | #ifdef USE_LOCALE_NUMERIC |
56279a21 |
314 | Safefree(curnum); |
98994639 |
315 | if (! (curnum = |
316 | setlocale(LC_NUMERIC, |
317 | (!done && (lang || PerlEnv_getenv("LC_NUMERIC"))) |
bd61b366 |
318 | ? "" : NULL))) |
98994639 |
319 | setlocale_failure = TRUE; |
320 | else |
321 | curnum = savepv(curnum); |
322 | #endif /* USE_LOCALE_NUMERIC */ |
323 | } |
324 | |
325 | #endif /* LC_ALL */ |
326 | |
327 | #endif /* !LOCALE_ENVIRON_REQUIRED */ |
328 | |
329 | #ifdef LC_ALL |
330 | if (! setlocale(LC_ALL, "")) |
331 | setlocale_failure = TRUE; |
332 | #endif /* LC_ALL */ |
333 | |
334 | if (!setlocale_failure) { |
335 | #ifdef USE_LOCALE_CTYPE |
6cb43dbf |
336 | Safefree(curctype); |
98994639 |
337 | if (! (curctype = setlocale(LC_CTYPE, ""))) |
338 | setlocale_failure = TRUE; |
339 | else |
340 | curctype = savepv(curctype); |
341 | #endif /* USE_LOCALE_CTYPE */ |
342 | #ifdef USE_LOCALE_COLLATE |
6cb43dbf |
343 | Safefree(curcoll); |
98994639 |
344 | if (! (curcoll = setlocale(LC_COLLATE, ""))) |
345 | setlocale_failure = TRUE; |
346 | else |
347 | curcoll = savepv(curcoll); |
348 | #endif /* USE_LOCALE_COLLATE */ |
349 | #ifdef USE_LOCALE_NUMERIC |
6cb43dbf |
350 | Safefree(curnum); |
98994639 |
351 | if (! (curnum = setlocale(LC_NUMERIC, ""))) |
352 | setlocale_failure = TRUE; |
353 | else |
354 | curnum = savepv(curnum); |
355 | #endif /* USE_LOCALE_NUMERIC */ |
356 | } |
357 | |
358 | if (setlocale_failure) { |
359 | char *p; |
0bd48802 |
360 | const bool locwarn = (printwarn > 1 || |
98994639 |
361 | (printwarn && |
362 | (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)))); |
363 | |
364 | if (locwarn) { |
365 | #ifdef LC_ALL |
366 | |
367 | PerlIO_printf(Perl_error_log, |
368 | "perl: warning: Setting locale failed.\n"); |
369 | |
370 | #else /* !LC_ALL */ |
371 | |
372 | PerlIO_printf(Perl_error_log, |
373 | "perl: warning: Setting locale failed for the categories:\n\t"); |
374 | #ifdef USE_LOCALE_CTYPE |
375 | if (! curctype) |
376 | PerlIO_printf(Perl_error_log, "LC_CTYPE "); |
377 | #endif /* USE_LOCALE_CTYPE */ |
378 | #ifdef USE_LOCALE_COLLATE |
379 | if (! curcoll) |
380 | PerlIO_printf(Perl_error_log, "LC_COLLATE "); |
381 | #endif /* USE_LOCALE_COLLATE */ |
382 | #ifdef USE_LOCALE_NUMERIC |
383 | if (! curnum) |
384 | PerlIO_printf(Perl_error_log, "LC_NUMERIC "); |
385 | #endif /* USE_LOCALE_NUMERIC */ |
386 | PerlIO_printf(Perl_error_log, "\n"); |
387 | |
388 | #endif /* LC_ALL */ |
389 | |
390 | PerlIO_printf(Perl_error_log, |
391 | "perl: warning: Please check that your locale settings:\n"); |
392 | |
393 | #ifdef __GLIBC__ |
394 | PerlIO_printf(Perl_error_log, |
395 | "\tLANGUAGE = %c%s%c,\n", |
396 | language ? '"' : '(', |
397 | language ? language : "unset", |
398 | language ? '"' : ')'); |
399 | #endif |
400 | |
401 | PerlIO_printf(Perl_error_log, |
402 | "\tLC_ALL = %c%s%c,\n", |
403 | lc_all ? '"' : '(', |
404 | lc_all ? lc_all : "unset", |
405 | lc_all ? '"' : ')'); |
406 | |
407 | #if defined(USE_ENVIRON_ARRAY) |
408 | { |
409 | char **e; |
410 | for (e = environ; *e; e++) { |
411 | if (strnEQ(*e, "LC_", 3) |
412 | && strnNE(*e, "LC_ALL=", 7) |
413 | && (p = strchr(*e, '='))) |
414 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", |
415 | (int)(p - *e), *e, p + 1); |
416 | } |
417 | } |
418 | #else |
419 | PerlIO_printf(Perl_error_log, |
420 | "\t(possibly more locale environment variables)\n"); |
421 | #endif |
422 | |
423 | PerlIO_printf(Perl_error_log, |
424 | "\tLANG = %c%s%c\n", |
425 | lang ? '"' : '(', |
426 | lang ? lang : "unset", |
427 | lang ? '"' : ')'); |
428 | |
429 | PerlIO_printf(Perl_error_log, |
430 | " are supported and installed on your system.\n"); |
431 | } |
432 | |
433 | #ifdef LC_ALL |
434 | |
435 | if (setlocale(LC_ALL, "C")) { |
436 | if (locwarn) |
437 | PerlIO_printf(Perl_error_log, |
438 | "perl: warning: Falling back to the standard locale (\"C\").\n"); |
439 | ok = 0; |
440 | } |
441 | else { |
442 | if (locwarn) |
443 | PerlIO_printf(Perl_error_log, |
444 | "perl: warning: Failed to fall back to the standard locale (\"C\").\n"); |
445 | ok = -1; |
446 | } |
447 | |
448 | #else /* ! LC_ALL */ |
449 | |
450 | if (0 |
451 | #ifdef USE_LOCALE_CTYPE |
452 | || !(curctype || setlocale(LC_CTYPE, "C")) |
453 | #endif /* USE_LOCALE_CTYPE */ |
454 | #ifdef USE_LOCALE_COLLATE |
455 | || !(curcoll || setlocale(LC_COLLATE, "C")) |
456 | #endif /* USE_LOCALE_COLLATE */ |
457 | #ifdef USE_LOCALE_NUMERIC |
458 | || !(curnum || setlocale(LC_NUMERIC, "C")) |
459 | #endif /* USE_LOCALE_NUMERIC */ |
460 | ) |
461 | { |
462 | if (locwarn) |
463 | PerlIO_printf(Perl_error_log, |
464 | "perl: warning: Cannot fall back to the standard locale (\"C\").\n"); |
465 | ok = -1; |
466 | } |
467 | |
468 | #endif /* ! LC_ALL */ |
469 | |
470 | #ifdef USE_LOCALE_CTYPE |
56279a21 |
471 | Safefree(curctype); |
bd61b366 |
472 | curctype = savepv(setlocale(LC_CTYPE, NULL)); |
98994639 |
473 | #endif /* USE_LOCALE_CTYPE */ |
474 | #ifdef USE_LOCALE_COLLATE |
56279a21 |
475 | Safefree(curcoll); |
bd61b366 |
476 | curcoll = savepv(setlocale(LC_COLLATE, NULL)); |
98994639 |
477 | #endif /* USE_LOCALE_COLLATE */ |
478 | #ifdef USE_LOCALE_NUMERIC |
56279a21 |
479 | Safefree(curnum); |
bd61b366 |
480 | curnum = savepv(setlocale(LC_NUMERIC, NULL)); |
98994639 |
481 | #endif /* USE_LOCALE_NUMERIC */ |
482 | } |
483 | else { |
484 | |
485 | #ifdef USE_LOCALE_CTYPE |
486 | new_ctype(curctype); |
487 | #endif /* USE_LOCALE_CTYPE */ |
488 | |
489 | #ifdef USE_LOCALE_COLLATE |
490 | new_collate(curcoll); |
491 | #endif /* USE_LOCALE_COLLATE */ |
492 | |
493 | #ifdef USE_LOCALE_NUMERIC |
494 | new_numeric(curnum); |
495 | #endif /* USE_LOCALE_NUMERIC */ |
b310b053 |
496 | |
98994639 |
497 | } |
498 | |
499 | #endif /* USE_LOCALE */ |
500 | |
ec71e770 |
501 | #ifdef USE_PERLIO |
b310b053 |
502 | { |
fde18df1 |
503 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ |
ec71e770 |
504 | any of the following are true: |
085a54d9 |
505 | - nl_langinfo(CODESET) contains /^utf-?8/i |
61de9fb5 |
506 | - $ENV{LC_ALL} contains /^utf-?8/i |
085a54d9 |
507 | - $ENV{LC_CTYPE} contains /^utf-?8/i |
61de9fb5 |
508 | - $ENV{LANG} contains /^utf-?8/i |
509 | The LC_ALL, LC_CTYPE, LANG obey the usual override |
510 | hierarchy of locale environment variables. (LANGUAGE |
511 | affects only LC_MESSAGES only under glibc.) (If present, |
512 | it overrides LC_MESSAGES for GNU gettext, and it also |
513 | can have more than one locale, separated by spaces, |
514 | in case you need to know.) |
8aa8f774 |
515 | If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE}) |
a05d7ebb |
516 | are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer |
fde18df1 |
517 | on STDIN, STDOUT, STDERR, _and_ the default open discipline. |
085a54d9 |
518 | */ |
fde18df1 |
519 | bool utf8locale = FALSE; |
b310b053 |
520 | char *codeset = NULL; |
521 | #if defined(HAS_NL_LANGINFO) && defined(CODESET) |
522 | codeset = nl_langinfo(CODESET); |
523 | #endif |
61de9fb5 |
524 | if (codeset) |
89529cee |
525 | utf8locale = (Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF-8")) == 0 || |
526 | Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF8") ) == 0); |
077a72a9 |
527 | #if defined(USE_LOCALE) |
61de9fb5 |
528 | else { /* nl_langinfo(CODESET) is supposed to correctly |
529 | * interpret the locale environment variables, |
530 | * but just in case it fails, let's do this manually. */ |
531 | if (lang) |
89529cee |
532 | utf8locale = (Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF-8")) == 0 || |
533 | Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF8") ) == 0); |
b310b053 |
534 | #ifdef USE_LOCALE_CTYPE |
61de9fb5 |
535 | if (curctype) |
89529cee |
536 | utf8locale = (Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF-8")) == 0 || |
537 | Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF8") ) == 0); |
b310b053 |
538 | #endif |
61de9fb5 |
539 | if (lc_all) |
89529cee |
540 | utf8locale = (Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF-8")) == 0 || |
541 | Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF8") ) == 0); |
61de9fb5 |
542 | } |
fde18df1 |
543 | #endif /* USE_LOCALE */ |
544 | if (utf8locale) |
545 | PL_utf8locale = TRUE; |
546 | } |
a05d7ebb |
547 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
fde18df1 |
548 | This is an alternative to using the -C command line switch |
549 | (the -C if present will override this). */ |
550 | { |
dd374669 |
551 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
a05d7ebb |
552 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
5a22a2bb |
553 | if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG) |
554 | PL_utf8cache = -1; |
b310b053 |
555 | } |
ec71e770 |
556 | #endif |
b310b053 |
557 | |
98994639 |
558 | #ifdef USE_LOCALE_CTYPE |
43c5f42d |
559 | Safefree(curctype); |
98994639 |
560 | #endif /* USE_LOCALE_CTYPE */ |
561 | #ifdef USE_LOCALE_COLLATE |
43c5f42d |
562 | Safefree(curcoll); |
98994639 |
563 | #endif /* USE_LOCALE_COLLATE */ |
564 | #ifdef USE_LOCALE_NUMERIC |
43c5f42d |
565 | Safefree(curnum); |
98994639 |
566 | #endif /* USE_LOCALE_NUMERIC */ |
567 | return ok; |
568 | } |
569 | |
98994639 |
570 | #ifdef USE_LOCALE_COLLATE |
571 | |
572 | /* |
573 | * mem_collxfrm() is a bit like strxfrm() but with two important |
574 | * differences. First, it handles embedded NULs. Second, it allocates |
575 | * a bit more memory than needed for the transformed data itself. |
576 | * The real transformed data begins at offset sizeof(collationix). |
577 | * Please see sv_collxfrm() to see how this is used. |
578 | */ |
166f8a29 |
579 | |
98994639 |
580 | char * |
581 | Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen) |
582 | { |
97aff369 |
583 | dVAR; |
98994639 |
584 | char *xbuf; |
585 | STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */ |
586 | |
7918f24d |
587 | PERL_ARGS_ASSERT_MEM_COLLXFRM; |
588 | |
98994639 |
589 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ |
590 | /* the +1 is for the terminating NUL. */ |
591 | |
592 | xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1; |
a02a5408 |
593 | Newx(xbuf, xAlloc, char); |
98994639 |
594 | if (! xbuf) |
595 | goto bad; |
596 | |
597 | *(U32*)xbuf = PL_collation_ix; |
598 | xout = sizeof(PL_collation_ix); |
599 | for (xin = 0; xin < len; ) { |
224e8ef5 |
600 | Size_t xused; |
98994639 |
601 | |
602 | for (;;) { |
603 | xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout); |
224e8ef5 |
604 | if (xused >= PERL_INT_MAX) |
98994639 |
605 | goto bad; |
eb160463 |
606 | if ((STRLEN)xused < xAlloc - xout) |
98994639 |
607 | break; |
608 | xAlloc = (2 * xAlloc) + 1; |
609 | Renew(xbuf, xAlloc, char); |
610 | if (! xbuf) |
611 | goto bad; |
612 | } |
613 | |
614 | xin += strlen(s + xin) + 1; |
615 | xout += xused; |
616 | |
617 | /* Embedded NULs are understood but silently skipped |
618 | * because they make no sense in locale collation. */ |
619 | } |
620 | |
621 | xbuf[xout] = '\0'; |
622 | *xlen = xout - sizeof(PL_collation_ix); |
623 | return xbuf; |
624 | |
625 | bad: |
626 | Safefree(xbuf); |
627 | *xlen = 0; |
628 | return NULL; |
629 | } |
630 | |
631 | #endif /* USE_LOCALE_COLLATE */ |
632 | |
66610fdd |
633 | /* |
634 | * Local variables: |
635 | * c-indentation-style: bsd |
636 | * c-basic-offset: 4 |
637 | * indent-tabs-mode: t |
638 | * End: |
639 | * |
37442d52 |
640 | * ex: set ts=8 sts=4 sw=4 noet: |
641 | */ |