Commit | Line | Data |
---|---|---|
98994639 HS |
1 | /* locale.c |
2 | * | |
1129b882 NC |
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 HS |
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, |
cdad3b53 | 13 | * silivren penna míriel |
4ac71550 | 14 | * o menel aglar elenath! |
cdad3b53 | 15 | * Na-chaered palan-díriel |
4ac71550 TC |
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 HS |
21 | */ |
22 | ||
166f8a29 DM |
23 | /* utility functions for handling locale-specific stuff like what |
24 | * character represents the decimal point. | |
25 | */ | |
26 | ||
98994639 HS |
27 | #include "EXTERN.h" |
28 | #define PERL_IN_LOCALE_C | |
29 | #include "perl.h" | |
30 | ||
b310b053 JH |
31 | #ifdef I_LANGINFO |
32 | # include <langinfo.h> | |
33 | #endif | |
34 | ||
a4af207c JH |
35 | #include "reentr.h" |
36 | ||
98994639 HS |
37 | /* |
38 | * Standardize the locale name from a string returned by 'setlocale'. | |
39 | * | |
0ef2a2b2 | 40 | * The typical return value of setlocale() is either |
98994639 HS |
41 | * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL |
42 | * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL | |
43 | * (the space-separated values represent the various sublocales, | |
0ef2a2b2 | 44 | * in some unspecified order). This is not handled by this function. |
98994639 HS |
45 | * |
46 | * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n", | |
0ef2a2b2 KW |
47 | * which is harmful for further use of the string in setlocale(). This |
48 | * function removes the trailing new line and everything up through the '=' | |
98994639 HS |
49 | * |
50 | */ | |
51 | STATIC char * | |
52 | S_stdize_locale(pTHX_ char *locs) | |
53 | { | |
7452cf6a | 54 | const char * const s = strchr(locs, '='); |
98994639 HS |
55 | bool okay = TRUE; |
56 | ||
7918f24d NC |
57 | PERL_ARGS_ASSERT_STDIZE_LOCALE; |
58 | ||
8772537c AL |
59 | if (s) { |
60 | const char * const t = strchr(s, '.'); | |
98994639 | 61 | okay = FALSE; |
8772537c AL |
62 | if (t) { |
63 | const char * const u = strchr(t, '\n'); | |
64 | if (u && (u[1] == 0)) { | |
65 | const STRLEN len = u - s; | |
66 | Move(s + 1, locs, len, char); | |
67 | locs[len] = 0; | |
68 | okay = TRUE; | |
98994639 HS |
69 | } |
70 | } | |
71 | } | |
72 | ||
73 | if (!okay) | |
74 | Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs); | |
75 | ||
76 | return locs; | |
77 | } | |
78 | ||
79 | void | |
80 | Perl_set_numeric_radix(pTHX) | |
81 | { | |
82 | #ifdef USE_LOCALE_NUMERIC | |
97aff369 | 83 | dVAR; |
98994639 | 84 | # ifdef HAS_LOCALECONV |
7452cf6a | 85 | const struct lconv* const lc = localeconv(); |
98994639 | 86 | |
98994639 HS |
87 | if (lc && lc->decimal_point) { |
88 | if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) { | |
89 | SvREFCNT_dec(PL_numeric_radix_sv); | |
a0714e2c | 90 | PL_numeric_radix_sv = NULL; |
98994639 HS |
91 | } |
92 | else { | |
93 | if (PL_numeric_radix_sv) | |
94 | sv_setpv(PL_numeric_radix_sv, lc->decimal_point); | |
95 | else | |
96 | PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0); | |
28acfe03 KW |
97 | if (! is_ascii_string((U8 *) lc->decimal_point, 0) |
98 | && is_utf8_string((U8 *) lc->decimal_point, 0) | |
99 | && is_cur_LC_category_utf8(LC_NUMERIC)) | |
100 | { | |
101 | SvUTF8_on(PL_numeric_radix_sv); | |
102 | } | |
98994639 HS |
103 | } |
104 | } | |
105 | else | |
a0714e2c | 106 | PL_numeric_radix_sv = NULL; |
98994639 HS |
107 | # endif /* HAS_LOCALECONV */ |
108 | #endif /* USE_LOCALE_NUMERIC */ | |
109 | } | |
110 | ||
111 | /* | |
112 | * Set up for a new numeric locale. | |
113 | */ | |
114 | void | |
8772537c | 115 | Perl_new_numeric(pTHX_ const char *newnum) |
98994639 HS |
116 | { |
117 | #ifdef USE_LOCALE_NUMERIC | |
b03f34cf | 118 | char *save_newnum; |
97aff369 | 119 | dVAR; |
98994639 HS |
120 | |
121 | if (! newnum) { | |
43c5f42d NC |
122 | Safefree(PL_numeric_name); |
123 | PL_numeric_name = NULL; | |
98994639 HS |
124 | PL_numeric_standard = TRUE; |
125 | PL_numeric_local = TRUE; | |
126 | return; | |
127 | } | |
128 | ||
b03f34cf KW |
129 | save_newnum = stdize_locale(savepv(newnum)); |
130 | if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) { | |
98994639 | 131 | Safefree(PL_numeric_name); |
b03f34cf KW |
132 | PL_numeric_name = save_newnum; |
133 | PL_numeric_standard = ((*save_newnum == 'C' && save_newnum[1] == '\0') | |
134 | || strEQ(save_newnum, "POSIX")); | |
98994639 HS |
135 | PL_numeric_local = TRUE; |
136 | set_numeric_radix(); | |
137 | } | |
b03f34cf KW |
138 | else { |
139 | Safefree(save_newnum); | |
140 | } | |
98994639 HS |
141 | |
142 | #endif /* USE_LOCALE_NUMERIC */ | |
143 | } | |
144 | ||
145 | void | |
146 | Perl_set_numeric_standard(pTHX) | |
147 | { | |
148 | #ifdef USE_LOCALE_NUMERIC | |
97aff369 | 149 | dVAR; |
98994639 HS |
150 | |
151 | if (! PL_numeric_standard) { | |
152 | setlocale(LC_NUMERIC, "C"); | |
153 | PL_numeric_standard = TRUE; | |
154 | PL_numeric_local = FALSE; | |
155 | set_numeric_radix(); | |
156 | } | |
157 | ||
158 | #endif /* USE_LOCALE_NUMERIC */ | |
159 | } | |
160 | ||
161 | void | |
162 | Perl_set_numeric_local(pTHX) | |
163 | { | |
164 | #ifdef USE_LOCALE_NUMERIC | |
97aff369 | 165 | dVAR; |
98994639 HS |
166 | |
167 | if (! PL_numeric_local) { | |
168 | setlocale(LC_NUMERIC, PL_numeric_name); | |
169 | PL_numeric_standard = FALSE; | |
170 | PL_numeric_local = TRUE; | |
171 | set_numeric_radix(); | |
172 | } | |
173 | ||
174 | #endif /* USE_LOCALE_NUMERIC */ | |
175 | } | |
176 | ||
177 | /* | |
178 | * Set up for a new ctype locale. | |
179 | */ | |
180 | void | |
8772537c | 181 | Perl_new_ctype(pTHX_ const char *newctype) |
98994639 HS |
182 | { |
183 | #ifdef USE_LOCALE_CTYPE | |
27da23d5 | 184 | dVAR; |
98994639 HS |
185 | int i; |
186 | ||
7918f24d NC |
187 | PERL_ARGS_ASSERT_NEW_CTYPE; |
188 | ||
98994639 HS |
189 | for (i = 0; i < 256; i++) { |
190 | if (isUPPER_LC(i)) | |
191 | PL_fold_locale[i] = toLOWER_LC(i); | |
192 | else if (isLOWER_LC(i)) | |
193 | PL_fold_locale[i] = toUPPER_LC(i); | |
194 | else | |
195 | PL_fold_locale[i] = i; | |
196 | } | |
197 | ||
198 | #endif /* USE_LOCALE_CTYPE */ | |
7918f24d | 199 | PERL_ARGS_ASSERT_NEW_CTYPE; |
8772537c | 200 | PERL_UNUSED_ARG(newctype); |
96a5add6 | 201 | PERL_UNUSED_CONTEXT; |
98994639 HS |
202 | } |
203 | ||
204 | /* | |
205 | * Set up for a new collation locale. | |
206 | */ | |
207 | void | |
8772537c | 208 | Perl_new_collate(pTHX_ const char *newcoll) |
98994639 HS |
209 | { |
210 | #ifdef USE_LOCALE_COLLATE | |
97aff369 | 211 | dVAR; |
98994639 HS |
212 | |
213 | if (! newcoll) { | |
214 | if (PL_collation_name) { | |
215 | ++PL_collation_ix; | |
216 | Safefree(PL_collation_name); | |
217 | PL_collation_name = NULL; | |
218 | } | |
219 | PL_collation_standard = TRUE; | |
220 | PL_collxfrm_base = 0; | |
221 | PL_collxfrm_mult = 2; | |
222 | return; | |
223 | } | |
224 | ||
225 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { | |
226 | ++PL_collation_ix; | |
227 | Safefree(PL_collation_name); | |
228 | PL_collation_name = stdize_locale(savepv(newcoll)); | |
770526c1 NC |
229 | PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0') |
230 | || strEQ(newcoll, "POSIX")); | |
98994639 HS |
231 | |
232 | { | |
233 | /* 2: at most so many chars ('a', 'b'). */ | |
234 | /* 50: surely no system expands a char more. */ | |
235 | #define XFRMBUFSIZE (2 * 50) | |
236 | char xbuf[XFRMBUFSIZE]; | |
8772537c AL |
237 | const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); |
238 | const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); | |
239 | const SSize_t mult = fb - fa; | |
e0601d3c | 240 | if (mult < 1 && !(fa == 0 && fb == 0)) |
ad49ad39 NC |
241 | Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf, |
242 | (UV) fa, (UV) fb); | |
eb160463 | 243 | PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0; |
98994639 HS |
244 | PL_collxfrm_mult = mult; |
245 | } | |
246 | } | |
247 | ||
248 | #endif /* USE_LOCALE_COLLATE */ | |
249 | } | |
250 | ||
251 | /* | |
252 | * Initialize locale awareness. | |
253 | */ | |
254 | int | |
255 | Perl_init_i18nl10n(pTHX_ int printwarn) | |
256 | { | |
257 | int ok = 1; | |
258 | /* returns | |
259 | * 1 = set ok or not applicable, | |
260 | * 0 = fallback to C locale, | |
261 | * -1 = fallback to C locale failed | |
262 | */ | |
263 | ||
264 | #if defined(USE_LOCALE) | |
97aff369 | 265 | dVAR; |
98994639 HS |
266 | |
267 | #ifdef USE_LOCALE_CTYPE | |
268 | char *curctype = NULL; | |
269 | #endif /* USE_LOCALE_CTYPE */ | |
270 | #ifdef USE_LOCALE_COLLATE | |
271 | char *curcoll = NULL; | |
272 | #endif /* USE_LOCALE_COLLATE */ | |
273 | #ifdef USE_LOCALE_NUMERIC | |
274 | char *curnum = NULL; | |
275 | #endif /* USE_LOCALE_NUMERIC */ | |
276 | #ifdef __GLIBC__ | |
7452cf6a | 277 | char * const language = PerlEnv_getenv("LANGUAGE"); |
98994639 | 278 | #endif |
ccd65d51 KW |
279 | /* NULL uses the existing already set up locale */ |
280 | const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT")) | |
281 | ? NULL | |
282 | : ""; | |
7452cf6a AL |
283 | char * const lc_all = PerlEnv_getenv("LC_ALL"); |
284 | char * const lang = PerlEnv_getenv("LANG"); | |
98994639 HS |
285 | bool setlocale_failure = FALSE; |
286 | ||
287 | #ifdef LOCALE_ENVIRON_REQUIRED | |
288 | ||
289 | /* | |
290 | * Ultrix setlocale(..., "") fails if there are no environment | |
291 | * variables from which to get a locale name. | |
292 | */ | |
293 | ||
294 | bool done = FALSE; | |
295 | ||
b3e384bf | 296 | # ifdef LC_ALL |
98994639 | 297 | if (lang) { |
ccd65d51 | 298 | if (setlocale(LC_ALL, setlocale_init)) |
98994639 HS |
299 | done = TRUE; |
300 | else | |
301 | setlocale_failure = TRUE; | |
302 | } | |
303 | if (!setlocale_failure) { | |
b3e384bf | 304 | # ifdef USE_LOCALE_CTYPE |
56279a21 | 305 | Safefree(curctype); |
98994639 HS |
306 | if (! (curctype = |
307 | setlocale(LC_CTYPE, | |
308 | (!done && (lang || PerlEnv_getenv("LC_CTYPE"))) | |
ccd65d51 | 309 | ? setlocale_init : NULL))) |
98994639 HS |
310 | setlocale_failure = TRUE; |
311 | else | |
312 | curctype = savepv(curctype); | |
b3e384bf KW |
313 | # endif /* USE_LOCALE_CTYPE */ |
314 | # ifdef USE_LOCALE_COLLATE | |
56279a21 | 315 | Safefree(curcoll); |
98994639 HS |
316 | if (! (curcoll = |
317 | setlocale(LC_COLLATE, | |
318 | (!done && (lang || PerlEnv_getenv("LC_COLLATE"))) | |
ccd65d51 | 319 | ? setlocale_init : NULL))) |
98994639 HS |
320 | setlocale_failure = TRUE; |
321 | else | |
322 | curcoll = savepv(curcoll); | |
b3e384bf KW |
323 | # endif /* USE_LOCALE_COLLATE */ |
324 | # ifdef USE_LOCALE_NUMERIC | |
56279a21 | 325 | Safefree(curnum); |
98994639 HS |
326 | if (! (curnum = |
327 | setlocale(LC_NUMERIC, | |
328 | (!done && (lang || PerlEnv_getenv("LC_NUMERIC"))) | |
ccd65d51 | 329 | ? setlocale_init : NULL))) |
98994639 HS |
330 | setlocale_failure = TRUE; |
331 | else | |
332 | curnum = savepv(curnum); | |
b3e384bf | 333 | # endif /* USE_LOCALE_NUMERIC */ |
98994639 HS |
334 | } |
335 | ||
b3e384bf | 336 | # endif /* LC_ALL */ |
98994639 HS |
337 | |
338 | #endif /* !LOCALE_ENVIRON_REQUIRED */ | |
339 | ||
340 | #ifdef LC_ALL | |
ccd65d51 | 341 | if (! setlocale(LC_ALL, setlocale_init)) |
98994639 HS |
342 | setlocale_failure = TRUE; |
343 | #endif /* LC_ALL */ | |
344 | ||
345 | if (!setlocale_failure) { | |
346 | #ifdef USE_LOCALE_CTYPE | |
6cb43dbf | 347 | Safefree(curctype); |
ccd65d51 | 348 | if (! (curctype = setlocale(LC_CTYPE, setlocale_init))) |
98994639 HS |
349 | setlocale_failure = TRUE; |
350 | else | |
351 | curctype = savepv(curctype); | |
352 | #endif /* USE_LOCALE_CTYPE */ | |
353 | #ifdef USE_LOCALE_COLLATE | |
6cb43dbf | 354 | Safefree(curcoll); |
ccd65d51 | 355 | if (! (curcoll = setlocale(LC_COLLATE, setlocale_init))) |
98994639 HS |
356 | setlocale_failure = TRUE; |
357 | else | |
358 | curcoll = savepv(curcoll); | |
359 | #endif /* USE_LOCALE_COLLATE */ | |
360 | #ifdef USE_LOCALE_NUMERIC | |
6cb43dbf | 361 | Safefree(curnum); |
ccd65d51 | 362 | if (! (curnum = setlocale(LC_NUMERIC, setlocale_init))) |
98994639 HS |
363 | setlocale_failure = TRUE; |
364 | else | |
365 | curnum = savepv(curnum); | |
366 | #endif /* USE_LOCALE_NUMERIC */ | |
367 | } | |
368 | ||
369 | if (setlocale_failure) { | |
370 | char *p; | |
0bd48802 | 371 | const bool locwarn = (printwarn > 1 || |
98994639 HS |
372 | (printwarn && |
373 | (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)))); | |
374 | ||
375 | if (locwarn) { | |
376 | #ifdef LC_ALL | |
377 | ||
378 | PerlIO_printf(Perl_error_log, | |
379 | "perl: warning: Setting locale failed.\n"); | |
380 | ||
381 | #else /* !LC_ALL */ | |
382 | ||
383 | PerlIO_printf(Perl_error_log, | |
384 | "perl: warning: Setting locale failed for the categories:\n\t"); | |
385 | #ifdef USE_LOCALE_CTYPE | |
386 | if (! curctype) | |
387 | PerlIO_printf(Perl_error_log, "LC_CTYPE "); | |
388 | #endif /* USE_LOCALE_CTYPE */ | |
389 | #ifdef USE_LOCALE_COLLATE | |
390 | if (! curcoll) | |
391 | PerlIO_printf(Perl_error_log, "LC_COLLATE "); | |
392 | #endif /* USE_LOCALE_COLLATE */ | |
393 | #ifdef USE_LOCALE_NUMERIC | |
394 | if (! curnum) | |
395 | PerlIO_printf(Perl_error_log, "LC_NUMERIC "); | |
396 | #endif /* USE_LOCALE_NUMERIC */ | |
397 | PerlIO_printf(Perl_error_log, "\n"); | |
398 | ||
399 | #endif /* LC_ALL */ | |
400 | ||
401 | PerlIO_printf(Perl_error_log, | |
402 | "perl: warning: Please check that your locale settings:\n"); | |
403 | ||
404 | #ifdef __GLIBC__ | |
405 | PerlIO_printf(Perl_error_log, | |
406 | "\tLANGUAGE = %c%s%c,\n", | |
407 | language ? '"' : '(', | |
408 | language ? language : "unset", | |
409 | language ? '"' : ')'); | |
410 | #endif | |
411 | ||
412 | PerlIO_printf(Perl_error_log, | |
413 | "\tLC_ALL = %c%s%c,\n", | |
414 | lc_all ? '"' : '(', | |
415 | lc_all ? lc_all : "unset", | |
416 | lc_all ? '"' : ')'); | |
417 | ||
418 | #if defined(USE_ENVIRON_ARRAY) | |
419 | { | |
420 | char **e; | |
421 | for (e = environ; *e; e++) { | |
422 | if (strnEQ(*e, "LC_", 3) | |
423 | && strnNE(*e, "LC_ALL=", 7) | |
424 | && (p = strchr(*e, '='))) | |
425 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", | |
426 | (int)(p - *e), *e, p + 1); | |
427 | } | |
428 | } | |
429 | #else | |
430 | PerlIO_printf(Perl_error_log, | |
431 | "\t(possibly more locale environment variables)\n"); | |
432 | #endif | |
433 | ||
434 | PerlIO_printf(Perl_error_log, | |
435 | "\tLANG = %c%s%c\n", | |
436 | lang ? '"' : '(', | |
437 | lang ? lang : "unset", | |
438 | lang ? '"' : ')'); | |
439 | ||
440 | PerlIO_printf(Perl_error_log, | |
441 | " are supported and installed on your system.\n"); | |
442 | } | |
443 | ||
444 | #ifdef LC_ALL | |
445 | ||
446 | if (setlocale(LC_ALL, "C")) { | |
447 | if (locwarn) | |
448 | PerlIO_printf(Perl_error_log, | |
449 | "perl: warning: Falling back to the standard locale (\"C\").\n"); | |
450 | ok = 0; | |
451 | } | |
452 | else { | |
453 | if (locwarn) | |
454 | PerlIO_printf(Perl_error_log, | |
455 | "perl: warning: Failed to fall back to the standard locale (\"C\").\n"); | |
456 | ok = -1; | |
457 | } | |
458 | ||
459 | #else /* ! LC_ALL */ | |
460 | ||
461 | if (0 | |
462 | #ifdef USE_LOCALE_CTYPE | |
463 | || !(curctype || setlocale(LC_CTYPE, "C")) | |
464 | #endif /* USE_LOCALE_CTYPE */ | |
465 | #ifdef USE_LOCALE_COLLATE | |
466 | || !(curcoll || setlocale(LC_COLLATE, "C")) | |
467 | #endif /* USE_LOCALE_COLLATE */ | |
468 | #ifdef USE_LOCALE_NUMERIC | |
469 | || !(curnum || setlocale(LC_NUMERIC, "C")) | |
470 | #endif /* USE_LOCALE_NUMERIC */ | |
471 | ) | |
472 | { | |
473 | if (locwarn) | |
474 | PerlIO_printf(Perl_error_log, | |
475 | "perl: warning: Cannot fall back to the standard locale (\"C\").\n"); | |
476 | ok = -1; | |
477 | } | |
478 | ||
479 | #endif /* ! LC_ALL */ | |
480 | ||
481 | #ifdef USE_LOCALE_CTYPE | |
56279a21 | 482 | Safefree(curctype); |
bd61b366 | 483 | curctype = savepv(setlocale(LC_CTYPE, NULL)); |
98994639 HS |
484 | #endif /* USE_LOCALE_CTYPE */ |
485 | #ifdef USE_LOCALE_COLLATE | |
56279a21 | 486 | Safefree(curcoll); |
bd61b366 | 487 | curcoll = savepv(setlocale(LC_COLLATE, NULL)); |
98994639 HS |
488 | #endif /* USE_LOCALE_COLLATE */ |
489 | #ifdef USE_LOCALE_NUMERIC | |
56279a21 | 490 | Safefree(curnum); |
bd61b366 | 491 | curnum = savepv(setlocale(LC_NUMERIC, NULL)); |
98994639 HS |
492 | #endif /* USE_LOCALE_NUMERIC */ |
493 | } | |
494 | else { | |
495 | ||
496 | #ifdef USE_LOCALE_CTYPE | |
497 | new_ctype(curctype); | |
498 | #endif /* USE_LOCALE_CTYPE */ | |
499 | ||
500 | #ifdef USE_LOCALE_COLLATE | |
501 | new_collate(curcoll); | |
502 | #endif /* USE_LOCALE_COLLATE */ | |
503 | ||
504 | #ifdef USE_LOCALE_NUMERIC | |
505 | new_numeric(curnum); | |
506 | #endif /* USE_LOCALE_NUMERIC */ | |
b310b053 | 507 | |
98994639 HS |
508 | } |
509 | ||
510 | #endif /* USE_LOCALE */ | |
511 | ||
ec71e770 | 512 | #ifdef USE_PERLIO |
b310b053 | 513 | { |
fde18df1 | 514 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ |
7d74bb61 | 515 | the current LC_CTYPE locale is UTF-8. |
8aa8f774 | 516 | If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE}) |
a05d7ebb | 517 | are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer |
fde18df1 | 518 | on STDIN, STDOUT, STDERR, _and_ the default open discipline. |
085a54d9 | 519 | */ |
7d74bb61 | 520 | PL_utf8locale = is_cur_LC_category_utf8(LC_CTYPE); |
fde18df1 | 521 | } |
a05d7ebb | 522 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
fde18df1 JH |
523 | This is an alternative to using the -C command line switch |
524 | (the -C if present will override this). */ | |
525 | { | |
dd374669 | 526 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
a05d7ebb | 527 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
5a22a2bb NC |
528 | if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG) |
529 | PL_utf8cache = -1; | |
b310b053 | 530 | } |
ec71e770 | 531 | #endif |
b310b053 | 532 | |
98994639 | 533 | #ifdef USE_LOCALE_CTYPE |
43c5f42d | 534 | Safefree(curctype); |
98994639 HS |
535 | #endif /* USE_LOCALE_CTYPE */ |
536 | #ifdef USE_LOCALE_COLLATE | |
43c5f42d | 537 | Safefree(curcoll); |
98994639 HS |
538 | #endif /* USE_LOCALE_COLLATE */ |
539 | #ifdef USE_LOCALE_NUMERIC | |
43c5f42d | 540 | Safefree(curnum); |
98994639 HS |
541 | #endif /* USE_LOCALE_NUMERIC */ |
542 | return ok; | |
543 | } | |
544 | ||
98994639 HS |
545 | #ifdef USE_LOCALE_COLLATE |
546 | ||
547 | /* | |
548 | * mem_collxfrm() is a bit like strxfrm() but with two important | |
549 | * differences. First, it handles embedded NULs. Second, it allocates | |
550 | * a bit more memory than needed for the transformed data itself. | |
551 | * The real transformed data begins at offset sizeof(collationix). | |
552 | * Please see sv_collxfrm() to see how this is used. | |
553 | */ | |
166f8a29 | 554 | |
98994639 HS |
555 | char * |
556 | Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen) | |
557 | { | |
97aff369 | 558 | dVAR; |
98994639 HS |
559 | char *xbuf; |
560 | STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */ | |
561 | ||
7918f24d NC |
562 | PERL_ARGS_ASSERT_MEM_COLLXFRM; |
563 | ||
98994639 HS |
564 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ |
565 | /* the +1 is for the terminating NUL. */ | |
566 | ||
567 | xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1; | |
a02a5408 | 568 | Newx(xbuf, xAlloc, char); |
98994639 HS |
569 | if (! xbuf) |
570 | goto bad; | |
571 | ||
572 | *(U32*)xbuf = PL_collation_ix; | |
573 | xout = sizeof(PL_collation_ix); | |
574 | for (xin = 0; xin < len; ) { | |
224e8ef5 | 575 | Size_t xused; |
98994639 HS |
576 | |
577 | for (;;) { | |
578 | xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout); | |
224e8ef5 | 579 | if (xused >= PERL_INT_MAX) |
98994639 | 580 | goto bad; |
eb160463 | 581 | if ((STRLEN)xused < xAlloc - xout) |
98994639 HS |
582 | break; |
583 | xAlloc = (2 * xAlloc) + 1; | |
584 | Renew(xbuf, xAlloc, char); | |
585 | if (! xbuf) | |
586 | goto bad; | |
587 | } | |
588 | ||
589 | xin += strlen(s + xin) + 1; | |
590 | xout += xused; | |
591 | ||
592 | /* Embedded NULs are understood but silently skipped | |
593 | * because they make no sense in locale collation. */ | |
594 | } | |
595 | ||
596 | xbuf[xout] = '\0'; | |
597 | *xlen = xout - sizeof(PL_collation_ix); | |
598 | return xbuf; | |
599 | ||
600 | bad: | |
601 | Safefree(xbuf); | |
602 | *xlen = 0; | |
603 | return NULL; | |
604 | } | |
605 | ||
606 | #endif /* USE_LOCALE_COLLATE */ | |
607 | ||
637917bb | 608 | STATIC bool |
7d74bb61 KW |
609 | S_is_cur_LC_category_utf8(pTHX_ int category) |
610 | { | |
611 | /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE | |
612 | * otherwise. 'category' may not be LC_ALL. If the platform doesn't have | |
613 | * nl_langinfo(), this employs a heuristic, which hence could give the | |
614 | * wrong result. It errs on the side of not being a UTF-8 locale. */ | |
615 | ||
616 | char *save_input_locale = NULL; | |
7d74bb61 KW |
617 | STRLEN final_pos; |
618 | ||
619 | assert(category != LC_ALL); | |
620 | ||
621 | /* First dispose of the trivial cases */ | |
622 | save_input_locale = stdize_locale(setlocale(category, NULL)); | |
623 | if (! save_input_locale) { | |
624 | return FALSE; /* XXX maybe should croak */ | |
625 | } | |
626 | if ((*save_input_locale == 'C' && save_input_locale[1] == '\0') | |
627 | || strEQ(save_input_locale, "POSIX")) | |
628 | { | |
629 | return FALSE; | |
630 | } | |
631 | ||
632 | save_input_locale = savepv(save_input_locale); | |
633 | ||
634 | #if defined(HAS_NL_LANGINFO) && defined(CODESET) && defined(USE_LOCALE_CTYPE) | |
635 | ||
636 | { /* Next try nl_langinfo if available */ | |
637 | ||
638 | char *save_ctype_locale = NULL; | |
639 | char *codeset = NULL; | |
640 | ||
641 | if (category != LC_CTYPE) { /* nl_langinfo works only on LC_CTYPE */ | |
642 | ||
643 | /* Get the current LC_CTYPE locale */ | |
644 | save_ctype_locale = stdize_locale(savepv(setlocale(LC_CTYPE, NULL))); | |
645 | if (! save_ctype_locale) { | |
646 | goto cant_use_nllanginfo; | |
647 | } | |
648 | ||
649 | /* If LC_CTYPE and the desired category use the same locale, this | |
650 | * means that finding the value for LC_CTYPE is the same as finding | |
651 | * the value for the desired category. Otherwise, switch LC_CTYPE | |
652 | * to the desired category's locale */ | |
653 | if (strEQ(save_ctype_locale, save_input_locale)) { | |
654 | Safefree(save_ctype_locale); | |
655 | save_ctype_locale = NULL; | |
656 | } | |
657 | else if (! setlocale(LC_CTYPE, save_input_locale)) { | |
658 | Safefree(save_ctype_locale); | |
659 | goto cant_use_nllanginfo; | |
660 | } | |
661 | } | |
662 | ||
663 | /* Here the current LC_CTYPE is set to the locale of the category whose | |
664 | * information is desired. This means that nl_langinfo() should give | |
665 | * the correct results */ | |
666 | codeset = savepv(nl_langinfo(CODESET)); | |
667 | if (codeset) { | |
668 | bool is_utf8; | |
669 | ||
670 | /* If we switched LC_CTYPE, switch back */ | |
671 | if (save_ctype_locale) { | |
672 | setlocale(LC_CTYPE, save_ctype_locale); | |
673 | Safefree(save_ctype_locale); | |
674 | } | |
675 | ||
676 | is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8")) | |
677 | || foldEQ(codeset, STR_WITH_LEN("UTF8")); | |
678 | ||
679 | Safefree(codeset); | |
680 | Safefree(save_input_locale); | |
681 | return is_utf8; | |
682 | } | |
683 | ||
684 | } | |
685 | cant_use_nllanginfo: | |
686 | ||
687 | #endif /* HAS_NL_LANGINFO etc */ | |
688 | ||
689 | /* nl_langinfo not available or failed somehow. Look at the locale name to | |
751e9426 | 690 | * see if it matches qr/UTF -? 8 /ix */ |
7d74bb61 KW |
691 | |
692 | final_pos = strlen(save_input_locale) - 1; | |
751e9426 KW |
693 | if (final_pos >= 3) { |
694 | char *name = save_input_locale; | |
695 | ||
696 | /* Find next 'U' or 'u' and look from there */ | |
697 | while ((name += strcspn(name, "Uu") + 1) | |
698 | <= save_input_locale + final_pos - 2) | |
7d74bb61 | 699 | { |
751e9426 KW |
700 | if (toFOLD(*(name)) != 't' |
701 | || toFOLD(*(name + 1)) != 'f') | |
702 | { | |
703 | continue; | |
704 | } | |
705 | name += 2; | |
706 | if (*(name) == '-') { | |
707 | if ((name > save_input_locale + final_pos - 1)) { | |
708 | break; | |
709 | } | |
710 | name++; | |
711 | } | |
712 | if (*(name) == '8') { | |
713 | Safefree(save_input_locale); | |
714 | return TRUE; | |
715 | } | |
7d74bb61 KW |
716 | } |
717 | } | |
718 | ||
719 | #ifdef WIN32 | |
720 | /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */ | |
721 | if (final_pos >= 4 | |
722 | && *(save_input_locale + final_pos - 0) == '1' | |
723 | && *(save_input_locale + final_pos - 1) == '0' | |
724 | && *(save_input_locale + final_pos - 2) == '0' | |
725 | && *(save_input_locale + final_pos - 3) == '5' | |
726 | && *(save_input_locale + final_pos - 4) == '6') | |
727 | { | |
728 | Safefree(save_input_locale); | |
729 | return TRUE; | |
730 | } | |
731 | #endif | |
732 | ||
fa9b773e KW |
733 | /* Other common encodings are the ISO 8859 series, which aren't UTF-8 */ |
734 | if (instr(save_input_locale, "8859")) { | |
735 | Safefree(save_input_locale); | |
736 | return FALSE; | |
737 | } | |
738 | ||
739 | #ifdef HAS_LOCALECONV | |
740 | ||
741 | # ifdef USE_LOCALE_MONETARY | |
742 | ||
743 | /* Here, there is nothing in the locale name to indicate whether the locale | |
744 | * is UTF-8 or not. This "name", the return of setlocale(), is actually | |
745 | * defined to be opaque, so we can't really rely on the absence of various | |
746 | * substrings in the name to indicate its UTF-8ness. Look at the locale's | |
747 | * currency symbol. Often that will be in the native script, and if the | |
748 | * symbol isn't in UTF-8, we know that the locale isn't. If it is | |
749 | * non-ASCII UTF-8, we infer that the locale is too. | |
750 | * To do this, like above for LC_CTYPE, we first set LC_MONETARY to the | |
751 | * locale of the desired category, if it isn't that locale already */ | |
752 | ||
753 | { | |
754 | char *save_monetary_locale = NULL; | |
755 | bool illegal_utf8 = FALSE; | |
756 | bool only_ascii = FALSE; | |
757 | const struct lconv* const lc = localeconv(); | |
758 | ||
759 | if (category != LC_MONETARY) { | |
760 | ||
761 | save_monetary_locale = stdize_locale(savepv(setlocale(LC_MONETARY, | |
762 | NULL))); | |
763 | if (! save_monetary_locale) { | |
764 | goto cant_use_monetary; | |
765 | } | |
766 | ||
767 | if (strNE(save_monetary_locale, save_input_locale)) { | |
768 | if (! setlocale(LC_MONETARY, save_input_locale)) { | |
769 | Safefree(save_monetary_locale); | |
770 | goto cant_use_monetary; | |
771 | } | |
772 | } | |
773 | } | |
774 | ||
775 | /* Here the current LC_MONETARY is set to the locale of the category | |
776 | * whose information is desired. */ | |
777 | ||
778 | if (lc && lc->currency_symbol) { | |
779 | if (! is_utf8_string((U8 *) lc->currency_symbol, 0)) { | |
780 | illegal_utf8 = TRUE; | |
781 | } | |
782 | else if (is_ascii_string((U8 *) lc->currency_symbol, 0)) { | |
783 | only_ascii = TRUE; | |
784 | } | |
785 | } | |
786 | ||
787 | /* If we changed it, restore LC_MONETARY to its original locale */ | |
788 | if (save_monetary_locale) { | |
789 | setlocale(LC_MONETARY, save_monetary_locale); | |
790 | Safefree(save_monetary_locale); | |
791 | } | |
792 | ||
793 | Safefree(save_input_locale); | |
794 | ||
795 | /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; otherwise | |
796 | * assume the locale is UTF-8 if and only if the symbol is non-ascii | |
797 | * UTF-8. (We can't really tell if the locale is UTF-8 or not if the | |
798 | * symbol is just a '$', so we err on the side of it not being UTF-8) | |
799 | * */ | |
800 | return (illegal_utf8) | |
801 | ? FALSE | |
802 | : ! only_ascii; | |
803 | ||
804 | } | |
805 | cant_use_monetary: | |
806 | ||
807 | # endif /* USE_LOCALE_MONETARY */ | |
808 | #endif /* HAS_LOCALECONV */ | |
809 | ||
810 | #if 0 && defined(HAS_STRERROR) && defined(USE_LOCALE_MESSAGES) | |
811 | ||
812 | /* This code is ifdefd out because it was found to not be necessary in testing | |
813 | * on our dromedary test machine, which has over 700 locales. There, looking | |
814 | * at just the currency symbol gave essentially the same results as doing this | |
815 | * extra work. Executing this also caused segfaults in miniperl. I left it in | |
816 | * so as to avoid rewriting it if real-world experience indicates that | |
817 | * dromedary is an outlier. Essentially, instead of returning abpve if we | |
818 | * haven't found illegal utf8, we continue on and examine all the strerror() | |
819 | * messages on the platform for utf8ness. If all are ASCII, we still don't | |
820 | * know the answer; but otherwise we have a pretty good indication of the | |
821 | * utf8ness. The reason this doesn't necessarily help much is that the | |
822 | * messages may not have been translated into the locale. The currency symbol | |
823 | * is much more likely to have been translated. The code below would need to | |
824 | * be altered somewhat to just be a continuation of testing the currency | |
825 | * symbol. */ | |
826 | int e; | |
827 | unsigned int failures = 0, non_ascii = 0; | |
828 | char *save_messages_locale = NULL; | |
829 | ||
830 | /* Like above for LC_CTYPE, we set LC_MESSAGES to the locale of the | |
831 | * desired category, if it isn't that locale already */ | |
832 | ||
833 | if (category != LC_MESSAGES) { | |
834 | ||
835 | save_messages_locale = stdize_locale(savepv(setlocale(LC_MESSAGES, | |
836 | NULL))); | |
837 | if (! save_messages_locale) { | |
838 | goto cant_use_messages; | |
839 | } | |
840 | ||
841 | if (strEQ(save_messages_locale, save_input_locale)) { | |
842 | Safefree(save_input_locale); | |
843 | } | |
844 | else if (! setlocale(LC_MESSAGES, save_input_locale)) { | |
845 | Safefree(save_messages_locale); | |
846 | goto cant_use_messages; | |
847 | } | |
848 | } | |
849 | ||
850 | /* Here the current LC_MESSAGES is set to the locale of the category | |
851 | * whose information is desired. Look through all the messages */ | |
852 | ||
853 | for (e = 0; | |
854 | #ifdef HAS_SYS_ERRLIST | |
855 | e <= sys_nerr | |
856 | #endif | |
857 | ; e++) | |
858 | { | |
859 | const U8* const errmsg = (U8 *) Strerror(e) ; | |
860 | if (!errmsg) | |
861 | break; | |
862 | if (! is_utf8_string(errmsg, 0)) { | |
863 | failures++; | |
864 | break; | |
865 | } | |
866 | else if (! is_ascii_string(errmsg, 0)) { | |
867 | non_ascii++; | |
868 | } | |
869 | } | |
870 | ||
871 | /* And, if we changed it, restore LC_MESSAGES to its original locale */ | |
872 | if (save_messages_locale) { | |
873 | setlocale(LC_MESSAGES, save_messages_locale); | |
874 | Safefree(save_messages_locale); | |
875 | } | |
876 | ||
877 | /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid, | |
878 | * any non-ascii means it is one; otherwise we assume it isn't */ | |
879 | return (failures) ? FALSE : non_ascii; | |
880 | ||
881 | } | |
882 | cant_use_messages: | |
883 | ||
884 | #endif | |
885 | ||
886 | Safefree(save_input_locale); | |
7d74bb61 KW |
887 | return FALSE; |
888 | } | |
889 | ||
890 | ||
891 | ||
66610fdd RGS |
892 | /* |
893 | * Local variables: | |
894 | * c-indentation-style: bsd | |
895 | * c-basic-offset: 4 | |
14d04a33 | 896 | * indent-tabs-mode: nil |
66610fdd RGS |
897 | * End: |
898 | * | |
14d04a33 | 899 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 900 | */ |