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. | |
0d071d52 KW |
25 | * |
26 | * All C programs have an underlying locale. Perl generally doesn't pay any | |
27 | * attention to it except within the scope of a 'use locale'. For most | |
28 | * categories, it accomplishes this by just using different operations if it is | |
29 | * in such scope than if not. However, various libc functions called by Perl | |
30 | * are affected by the LC_NUMERIC category, so there are macros in perl.h that | |
31 | * are used to toggle between the current locale and the C locale depending on | |
a9ad02a8 KW |
32 | * the desired behavior of those functions at the moment. And, LC_MESSAGES is |
33 | * switched to the C locale for outputting the message unless within the scope | |
34 | * of 'use locale'. | |
166f8a29 DM |
35 | */ |
36 | ||
98994639 HS |
37 | #include "EXTERN.h" |
38 | #define PERL_IN_LOCALE_C | |
39 | #include "perl.h" | |
40 | ||
b310b053 JH |
41 | #ifdef I_LANGINFO |
42 | # include <langinfo.h> | |
43 | #endif | |
44 | ||
a4af207c JH |
45 | #include "reentr.h" |
46 | ||
8ef6e574 KW |
47 | #ifdef USE_LOCALE |
48 | ||
98994639 | 49 | /* |
0d071d52 KW |
50 | * Standardize the locale name from a string returned by 'setlocale', possibly |
51 | * modifying that string. | |
98994639 | 52 | * |
0ef2a2b2 | 53 | * The typical return value of setlocale() is either |
98994639 HS |
54 | * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL |
55 | * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL | |
56 | * (the space-separated values represent the various sublocales, | |
0ef2a2b2 | 57 | * in some unspecified order). This is not handled by this function. |
98994639 HS |
58 | * |
59 | * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n", | |
0ef2a2b2 KW |
60 | * which is harmful for further use of the string in setlocale(). This |
61 | * function removes the trailing new line and everything up through the '=' | |
98994639 HS |
62 | * |
63 | */ | |
64 | STATIC char * | |
65 | S_stdize_locale(pTHX_ char *locs) | |
66 | { | |
7452cf6a | 67 | const char * const s = strchr(locs, '='); |
98994639 HS |
68 | bool okay = TRUE; |
69 | ||
7918f24d NC |
70 | PERL_ARGS_ASSERT_STDIZE_LOCALE; |
71 | ||
8772537c AL |
72 | if (s) { |
73 | const char * const t = strchr(s, '.'); | |
98994639 | 74 | okay = FALSE; |
8772537c AL |
75 | if (t) { |
76 | const char * const u = strchr(t, '\n'); | |
77 | if (u && (u[1] == 0)) { | |
78 | const STRLEN len = u - s; | |
79 | Move(s + 1, locs, len, char); | |
80 | locs[len] = 0; | |
81 | okay = TRUE; | |
98994639 HS |
82 | } |
83 | } | |
84 | } | |
85 | ||
86 | if (!okay) | |
87 | Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs); | |
88 | ||
89 | return locs; | |
90 | } | |
91 | ||
8ef6e574 KW |
92 | #endif |
93 | ||
98994639 HS |
94 | void |
95 | Perl_set_numeric_radix(pTHX) | |
96 | { | |
97 | #ifdef USE_LOCALE_NUMERIC | |
98 | # ifdef HAS_LOCALECONV | |
7452cf6a | 99 | const struct lconv* const lc = localeconv(); |
98994639 | 100 | |
98994639 HS |
101 | if (lc && lc->decimal_point) { |
102 | if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) { | |
103 | SvREFCNT_dec(PL_numeric_radix_sv); | |
a0714e2c | 104 | PL_numeric_radix_sv = NULL; |
98994639 HS |
105 | } |
106 | else { | |
107 | if (PL_numeric_radix_sv) | |
108 | sv_setpv(PL_numeric_radix_sv, lc->decimal_point); | |
109 | else | |
110 | PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0); | |
9f10db87 | 111 | if (! is_invariant_string((U8 *) lc->decimal_point, 0) |
28acfe03 | 112 | && is_utf8_string((U8 *) lc->decimal_point, 0) |
c1284011 | 113 | && _is_cur_LC_category_utf8(LC_NUMERIC)) |
28acfe03 KW |
114 | { |
115 | SvUTF8_on(PL_numeric_radix_sv); | |
116 | } | |
98994639 HS |
117 | } |
118 | } | |
119 | else | |
a0714e2c | 120 | PL_numeric_radix_sv = NULL; |
69014004 | 121 | |
ba99daed | 122 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Locale radix is '%s', ?UTF-8=%d\n", |
69014004 | 123 | (PL_numeric_radix_sv) |
37b7e435 KW |
124 | ? SvPVX(PL_numeric_radix_sv) |
125 | : "NULL", | |
126 | (PL_numeric_radix_sv) | |
39eb7305 | 127 | ? cBOOL(SvUTF8(PL_numeric_radix_sv)) |
37b7e435 | 128 | : 0)); |
69014004 | 129 | |
98994639 HS |
130 | # endif /* HAS_LOCALECONV */ |
131 | #endif /* USE_LOCALE_NUMERIC */ | |
132 | } | |
133 | ||
a39edc4c KW |
134 | /* Is the C string input 'name' "C" or "POSIX"? If so, and 'name' is the |
135 | * return of setlocale(), then this is extremely likely to be the C or POSIX | |
136 | * locale. However, the output of setlocale() is documented to be opaque, but | |
137 | * the odds are extremely small that it would return these two strings for some | |
138 | * other locale. Note that VMS in these two locales includes many non-ASCII | |
139 | * characters as controls and punctuation (below are hex bytes): | |
140 | * cntrl: 00-1F 7F 84-97 9B-9F | |
141 | * punct: 21-2F 3A-40 5B-60 7B-7E A1-A3 A5 A7-AB B0-B3 B5-B7 B9-BD BF-CF D1-DD DF-EF F1-FD | |
142 | * Oddly, none there are listed as alphas, though some represent alphabetics | |
143 | * http://www.nntp.perl.org/group/perl.perl5.porters/2013/02/msg198753.html */ | |
98b630b3 KW |
144 | #define isNAME_C_OR_POSIX(name) ((name) != NULL \ |
145 | && ((*(name) == 'C' && (*(name + 1)) == '\0') \ | |
a39edc4c KW |
146 | || strEQ((name), "POSIX"))) |
147 | ||
98994639 | 148 | void |
8772537c | 149 | Perl_new_numeric(pTHX_ const char *newnum) |
98994639 HS |
150 | { |
151 | #ifdef USE_LOCALE_NUMERIC | |
0d071d52 KW |
152 | |
153 | /* Called after all libc setlocale() calls affecting LC_NUMERIC, to tell | |
154 | * core Perl this and that 'newnum' is the name of the new locale. | |
155 | * It installs this locale as the current underlying default. | |
156 | * | |
157 | * The default locale and the C locale can be toggled between by use of the | |
158 | * set_numeric_local() and set_numeric_standard() functions, which should | |
159 | * probably not be called directly, but only via macros like | |
160 | * SET_NUMERIC_STANDARD() in perl.h. | |
161 | * | |
162 | * The toggling is necessary mainly so that a non-dot radix decimal point | |
163 | * character can be output, while allowing internal calculations to use a | |
164 | * dot. | |
165 | * | |
166 | * This sets several interpreter-level variables: | |
bb304765 | 167 | * PL_numeric_name The underlying locale's name: a copy of 'newnum' |
0d071d52 | 168 | * PL_numeric_local A boolean indicating if the toggled state is such |
7738054c KW |
169 | * that the current locale is the program's underlying |
170 | * locale | |
171 | * PL_numeric_standard An int indicating if the toggled state is such | |
172 | * that the current locale is the C locale. If non-zero, | |
173 | * it is in C; if > 1, it means it may not be toggled away | |
174 | * from C. | |
0d071d52 KW |
175 | * Note that both of the last two variables can be true at the same time, |
176 | * if the underlying locale is C. (Toggling is a no-op under these | |
177 | * circumstances.) | |
178 | * | |
179 | * Any code changing the locale (outside this file) should use | |
180 | * POSIX::setlocale, which calls this function. Therefore this function | |
181 | * should be called directly only from this file and from | |
182 | * POSIX::setlocale() */ | |
183 | ||
b03f34cf | 184 | char *save_newnum; |
98994639 HS |
185 | |
186 | if (! newnum) { | |
43c5f42d NC |
187 | Safefree(PL_numeric_name); |
188 | PL_numeric_name = NULL; | |
98994639 HS |
189 | PL_numeric_standard = TRUE; |
190 | PL_numeric_local = TRUE; | |
191 | return; | |
192 | } | |
193 | ||
b03f34cf | 194 | save_newnum = stdize_locale(savepv(newnum)); |
abe1abcf KW |
195 | |
196 | PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum); | |
197 | PL_numeric_local = TRUE; | |
198 | ||
b03f34cf | 199 | if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) { |
98994639 | 200 | Safefree(PL_numeric_name); |
b03f34cf | 201 | PL_numeric_name = save_newnum; |
b03f34cf | 202 | } |
abe1abcf KW |
203 | else { |
204 | Safefree(save_newnum); | |
205 | } | |
4c28b29c KW |
206 | |
207 | /* Keep LC_NUMERIC in the C locale. This is for XS modules, so they don't | |
208 | * have to worry about the radix being a non-dot. (Core operations that | |
209 | * need the underlying locale change to it temporarily). */ | |
210 | set_numeric_standard(); | |
211 | ||
e19f01cb | 212 | set_numeric_radix(); |
6959d69d | 213 | |
f2ce9e1c JH |
214 | #else |
215 | PERL_UNUSED_ARG(newnum); | |
98994639 HS |
216 | #endif /* USE_LOCALE_NUMERIC */ |
217 | } | |
218 | ||
219 | void | |
220 | Perl_set_numeric_standard(pTHX) | |
221 | { | |
222 | #ifdef USE_LOCALE_NUMERIC | |
28c1bf33 KW |
223 | /* Toggle the LC_NUMERIC locale to C. Most code should use the macros like |
224 | * SET_NUMERIC_STANDARD() in perl.h instead of calling this directly. The | |
225 | * macro avoids calling this routine if toggling isn't necessary according | |
226 | * to our records (which could be wrong if some XS code has changed the | |
227 | * locale behind our back) */ | |
0d071d52 | 228 | |
a9b8c0d8 KW |
229 | setlocale(LC_NUMERIC, "C"); |
230 | PL_numeric_standard = TRUE; | |
231 | PL_numeric_local = isNAME_C_OR_POSIX(PL_numeric_name); | |
232 | set_numeric_radix(); | |
69014004 KW |
233 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
234 | "Underlying LC_NUMERIC locale now is C\n")); | |
98994639 HS |
235 | |
236 | #endif /* USE_LOCALE_NUMERIC */ | |
237 | } | |
238 | ||
239 | void | |
240 | Perl_set_numeric_local(pTHX) | |
241 | { | |
242 | #ifdef USE_LOCALE_NUMERIC | |
28c1bf33 KW |
243 | /* Toggle the LC_NUMERIC locale to the current underlying default. Most |
244 | * code should use the macros like SET_NUMERIC_LOCAL() in perl.h instead of | |
245 | * calling this directly. The macro avoids calling this routine if | |
246 | * toggling isn't necessary according to our records (which could be wrong | |
247 | * if some XS code has changed the locale behind our back) */ | |
a9b8c0d8 KW |
248 | |
249 | setlocale(LC_NUMERIC, PL_numeric_name); | |
250 | PL_numeric_standard = isNAME_C_OR_POSIX(PL_numeric_name); | |
251 | PL_numeric_local = TRUE; | |
252 | set_numeric_radix(); | |
69014004 KW |
253 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
254 | "Underlying LC_NUMERIC locale now is %s\n", | |
255 | PL_numeric_name)); | |
98994639 HS |
256 | |
257 | #endif /* USE_LOCALE_NUMERIC */ | |
258 | } | |
259 | ||
260 | /* | |
261 | * Set up for a new ctype locale. | |
262 | */ | |
263 | void | |
8772537c | 264 | Perl_new_ctype(pTHX_ const char *newctype) |
98994639 HS |
265 | { |
266 | #ifdef USE_LOCALE_CTYPE | |
0d071d52 KW |
267 | |
268 | /* Called after all libc setlocale() calls affecting LC_CTYPE, to tell | |
269 | * core Perl this and that 'newctype' is the name of the new locale. | |
270 | * | |
271 | * This function sets up the folding arrays for all 256 bytes, assuming | |
272 | * that tofold() is tolc() since fold case is not a concept in POSIX, | |
273 | * | |
274 | * Any code changing the locale (outside this file) should use | |
275 | * POSIX::setlocale, which calls this function. Therefore this function | |
276 | * should be called directly only from this file and from | |
277 | * POSIX::setlocale() */ | |
278 | ||
27da23d5 | 279 | dVAR; |
68067e4e | 280 | UV i; |
98994639 | 281 | |
7918f24d NC |
282 | PERL_ARGS_ASSERT_NEW_CTYPE; |
283 | ||
215c5139 KW |
284 | /* We will replace any bad locale warning with 1) nothing if the new one is |
285 | * ok; or 2) a new warning for the bad new locale */ | |
286 | if (PL_warn_locale) { | |
287 | SvREFCNT_dec_NN(PL_warn_locale); | |
288 | PL_warn_locale = NULL; | |
289 | } | |
290 | ||
c1284011 | 291 | PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE); |
31f05a37 KW |
292 | |
293 | /* A UTF-8 locale gets standard rules. But note that code still has to | |
294 | * handle this specially because of the three problematic code points */ | |
295 | if (PL_in_utf8_CTYPE_locale) { | |
296 | Copy(PL_fold_latin1, PL_fold_locale, 256, U8); | |
297 | } | |
298 | else { | |
8c6180a9 KW |
299 | /* Assume enough space for every character being bad. 4 spaces each |
300 | * for the 94 printable characters that are output like "'x' "; and 5 | |
301 | * spaces each for "'\\' ", "'\t' ", and "'\n' "; plus a terminating | |
302 | * NUL */ | |
303 | char bad_chars_list[ (94 * 4) + (3 * 5) + 1 ]; | |
304 | ||
305 | bool check_for_problems = ckWARN_d(WARN_LOCALE); /* No warnings means | |
306 | no check */ | |
307 | bool multi_byte_locale = FALSE; /* Assume is a single-byte locale | |
308 | to start */ | |
309 | unsigned int bad_count = 0; /* Count of bad characters */ | |
310 | ||
baa60164 KW |
311 | for (i = 0; i < 256; i++) { |
312 | if (isUPPER_LC((U8) i)) | |
313 | PL_fold_locale[i] = (U8) toLOWER_LC((U8) i); | |
314 | else if (isLOWER_LC((U8) i)) | |
315 | PL_fold_locale[i] = (U8) toUPPER_LC((U8) i); | |
316 | else | |
317 | PL_fold_locale[i] = (U8) i; | |
8c6180a9 KW |
318 | |
319 | /* If checking for locale problems, see if the native ASCII-range | |
320 | * printables plus \n and \t are in their expected categories in | |
321 | * the new locale. If not, this could mean big trouble, upending | |
322 | * Perl's and most programs' assumptions, like having a | |
323 | * metacharacter with special meaning become a \w. Fortunately, | |
324 | * it's very rare to find locales that aren't supersets of ASCII | |
325 | * nowadays. It isn't a problem for most controls to be changed | |
326 | * into something else; we check only \n and \t, though perhaps \r | |
327 | * could be an issue as well. */ | |
328 | if (check_for_problems | |
329 | && (isGRAPH_A(i) || isBLANK_A(i) || i == '\n')) | |
330 | { | |
331 | if ((isALPHANUMERIC_A(i) && ! isALPHANUMERIC_LC(i)) | |
332 | || (isPUNCT_A(i) && ! isPUNCT_LC(i)) | |
333 | || (isBLANK_A(i) && ! isBLANK_LC(i)) | |
334 | || (i == '\n' && ! isCNTRL_LC(i))) | |
335 | { | |
336 | if (bad_count) { /* Separate multiple entries with a | |
337 | blank */ | |
338 | bad_chars_list[bad_count++] = ' '; | |
339 | } | |
340 | bad_chars_list[bad_count++] = '\''; | |
341 | if (isPRINT_A(i)) { | |
342 | bad_chars_list[bad_count++] = (char) i; | |
343 | } | |
344 | else { | |
345 | bad_chars_list[bad_count++] = '\\'; | |
346 | if (i == '\n') { | |
347 | bad_chars_list[bad_count++] = 'n'; | |
348 | } | |
349 | else { | |
350 | assert(i == '\t'); | |
351 | bad_chars_list[bad_count++] = 't'; | |
352 | } | |
353 | } | |
354 | bad_chars_list[bad_count++] = '\''; | |
355 | bad_chars_list[bad_count] = '\0'; | |
356 | } | |
357 | } | |
358 | } | |
359 | ||
360 | #ifdef MB_CUR_MAX | |
361 | /* We only handle single-byte locales (outside of UTF-8 ones; so if | |
d35fca5f | 362 | * this locale requires more than one byte, there are going to be |
8c6180a9 | 363 | * problems. */ |
ba1a4362 KW |
364 | if (check_for_problems && MB_CUR_MAX > 1 |
365 | ||
366 | /* Some platforms return MB_CUR_MAX > 1 for even the "C" | |
367 | * locale. Just assume that the implementation for them (plus | |
368 | * for POSIX) is correct and the > 1 value is spurious. (Since | |
369 | * these are specially handled to never be considered UTF-8 | |
370 | * locales, as long as this is the only problem, everything | |
371 | * should work fine */ | |
372 | && strNE(newctype, "C") && strNE(newctype, "POSIX")) | |
373 | { | |
8c6180a9 KW |
374 | multi_byte_locale = TRUE; |
375 | } | |
376 | #endif | |
377 | ||
378 | if (bad_count || multi_byte_locale) { | |
780fcc9f | 379 | PL_warn_locale = Perl_newSVpvf(aTHX_ |
8c6180a9 | 380 | "Locale '%s' may not work well.%s%s%s\n", |
780fcc9f | 381 | newctype, |
8c6180a9 KW |
382 | (multi_byte_locale) |
383 | ? " Some characters in it are not recognized by" | |
384 | " Perl." | |
385 | : "", | |
386 | (bad_count) | |
387 | ? "\nThe following characters (and maybe others)" | |
388 | " may not have the same meaning as the Perl" | |
389 | " program expects:\n" | |
390 | : "", | |
391 | (bad_count) | |
392 | ? bad_chars_list | |
393 | : "" | |
394 | ); | |
780fcc9f KW |
395 | /* If we are actually in the scope of the locale, output the |
396 | * message now. Otherwise we save it to be output at the first | |
397 | * operation using this locale, if that actually happens. Most | |
398 | * programs don't use locales, so they are immune to bad ones */ | |
399 | if (IN_LC(LC_CTYPE)) { | |
400 | ||
401 | /* We have to save 'newctype' because the setlocale() just | |
402 | * below may destroy it. The next setlocale() further down | |
403 | * should restore it properly so that the intermediate change | |
404 | * here is transparent to this function's caller */ | |
405 | const char * const badlocale = savepv(newctype); | |
406 | ||
407 | setlocale(LC_CTYPE, "C"); | |
408 | ||
409 | /* The '0' below suppresses a bogus gcc compiler warning */ | |
410 | Perl_warner(aTHX_ packWARN(WARN_LOCALE), SvPVX(PL_warn_locale), 0); | |
411 | setlocale(LC_CTYPE, badlocale); | |
c0f3a893 | 412 | Safefree(badlocale); |
780fcc9f KW |
413 | SvREFCNT_dec_NN(PL_warn_locale); |
414 | PL_warn_locale = NULL; | |
415 | } | |
baa60164 | 416 | } |
31f05a37 | 417 | } |
98994639 HS |
418 | |
419 | #endif /* USE_LOCALE_CTYPE */ | |
7918f24d | 420 | PERL_ARGS_ASSERT_NEW_CTYPE; |
8772537c | 421 | PERL_UNUSED_ARG(newctype); |
96a5add6 | 422 | PERL_UNUSED_CONTEXT; |
98994639 HS |
423 | } |
424 | ||
98994639 | 425 | void |
2726666d KW |
426 | Perl__warn_problematic_locale() |
427 | { | |
2726666d KW |
428 | |
429 | #ifdef USE_LOCALE_CTYPE | |
430 | ||
5f04a188 KW |
431 | dTHX; |
432 | ||
433 | /* Internal-to-core function that outputs the message in PL_warn_locale, | |
434 | * and then NULLS it. Should be called only through the macro | |
435 | * _CHECK_AND_WARN_PROBLEMATIC_LOCALE */ | |
436 | ||
2726666d KW |
437 | if (PL_warn_locale) { |
438 | /*GCC_DIAG_IGNORE(-Wformat-security); Didn't work */ | |
439 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), | |
440 | SvPVX(PL_warn_locale), | |
441 | 0 /* dummy to avoid compiler warning */ ); | |
442 | /* GCC_DIAG_RESTORE; */ | |
443 | SvREFCNT_dec_NN(PL_warn_locale); | |
444 | PL_warn_locale = NULL; | |
445 | } | |
446 | ||
447 | #endif | |
448 | ||
449 | } | |
450 | ||
451 | void | |
8772537c | 452 | Perl_new_collate(pTHX_ const char *newcoll) |
98994639 HS |
453 | { |
454 | #ifdef USE_LOCALE_COLLATE | |
0d071d52 KW |
455 | |
456 | /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell | |
457 | * core Perl this and that 'newcoll' is the name of the new locale. | |
458 | * | |
459 | * Any code changing the locale (outside this file) should use | |
460 | * POSIX::setlocale, which calls this function. Therefore this function | |
461 | * should be called directly only from this file and from | |
d35fca5f KW |
462 | * POSIX::setlocale(). |
463 | * | |
464 | * The design of locale collation is that every locale change is given an | |
465 | * index 'PL_collation_ix'. The first time a string particpates in an | |
466 | * operation that requires collation while locale collation is active, it | |
467 | * is given PERL_MAGIC_collxfrm magic (via sv_collxfrm_flags()). That | |
468 | * magic includes the collation index, and the transformation of the string | |
469 | * by strxfrm(), q.v. That transformation is used when doing comparisons, | |
470 | * instead of the string itself. If a string changes, the magic is | |
471 | * cleared. The next time the locale changes, the index is incremented, | |
472 | * and so we know during a comparison that the transformation is not | |
473 | * necessarily still valid, and so is recomputed. Note that if the locale | |
474 | * changes enough times, the index could wrap (a U32), and it is possible | |
475 | * that a transformation would improperly be considered valid, leading to | |
476 | * an unlikely bug */ | |
0d071d52 | 477 | |
98994639 HS |
478 | if (! newcoll) { |
479 | if (PL_collation_name) { | |
480 | ++PL_collation_ix; | |
481 | Safefree(PL_collation_name); | |
482 | PL_collation_name = NULL; | |
483 | } | |
484 | PL_collation_standard = TRUE; | |
00bf60ca | 485 | is_standard_collation: |
98994639 HS |
486 | PL_collxfrm_base = 0; |
487 | PL_collxfrm_mult = 2; | |
165a1c52 | 488 | PL_in_utf8_COLLATE_locale = FALSE; |
98994639 HS |
489 | return; |
490 | } | |
491 | ||
d35fca5f | 492 | /* If this is not the same locale as currently, set the new one up */ |
98994639 HS |
493 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
494 | ++PL_collation_ix; | |
495 | Safefree(PL_collation_name); | |
496 | PL_collation_name = stdize_locale(savepv(newcoll)); | |
a39edc4c | 497 | PL_collation_standard = isNAME_C_OR_POSIX(newcoll); |
00bf60ca KW |
498 | if (PL_collation_standard) { |
499 | goto is_standard_collation; | |
500 | } | |
98994639 | 501 | |
165a1c52 KW |
502 | PL_in_utf8_COLLATE_locale = _is_cur_LC_category_utf8(LC_COLLATE); |
503 | ||
98994639 | 504 | { |
d35fca5f KW |
505 | /* A locale collation definition includes primary, secondary, |
506 | * tertiary, etc. weights for each character. To sort, the primary | |
507 | * weights are used, and only if they compare equal, then the | |
508 | * secondary weights are used, and only if they compare equal, then | |
509 | * the tertiary, etc. strxfrm() works by taking the input string, | |
510 | * say ABC, and creating an output string consisting of first the | |
511 | * primary weights, A¹B¹C¹ followed by the secondary ones, A²B²C²; | |
512 | * and then the tertiary, etc, yielding A¹B¹C¹A²B²C²A³B³C³.... | |
513 | * Some characters may not have weights at every level. In our | |
514 | * example, let's say B doesn't have a tertiary weight, and A | |
515 | * doesn't have a secondary weight. The constructed string is then | |
516 | * going to be A¹B¹C¹B²C²A³C³.... This has the desired | |
517 | * characteristics that strcmp() will look at the secondary or | |
518 | * tertiary weights only if the strings compare equal at all higher | |
519 | * priority weights. The length of the transformed string is | |
520 | * roughly a linear function of the input string. It's not exactly | |
521 | * linear because some characters don't have weights at all levels, | |
522 | * and there are some complications, so there is often per-string | |
523 | * overhead. When we call strxfrm() we have to allocate some | |
524 | * memory to hold the transformed string. The calculations below | |
525 | * try to find constants for this locale 'm' and 'b' so that m*x + | |
526 | * b equals how much space we need given the size of the input | |
527 | * string in 'x'. If we calculate too small, we increase the size | |
528 | * as needed, and call strxfrm() again, but it is better to get it | |
529 | * right the first time to avoid wasted expensive string | |
530 | * transformations. */ | |
98994639 HS |
531 | /* 2: at most so many chars ('a', 'b'). */ |
532 | /* 50: surely no system expands a char more. */ | |
533 | #define XFRMBUFSIZE (2 * 50) | |
534 | char xbuf[XFRMBUFSIZE]; | |
8772537c AL |
535 | const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); |
536 | const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); | |
537 | const SSize_t mult = fb - fa; | |
e0601d3c | 538 | if (mult < 1 && !(fa == 0 && fb == 0)) |
ad49ad39 NC |
539 | Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf, |
540 | (UV) fa, (UV) fb); | |
eb160463 | 541 | PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0; |
98994639 HS |
542 | PL_collxfrm_mult = mult; |
543 | } | |
544 | } | |
545 | ||
f2ce9e1c JH |
546 | #else |
547 | PERL_UNUSED_ARG(newcoll); | |
98994639 HS |
548 | #endif /* USE_LOCALE_COLLATE */ |
549 | } | |
550 | ||
b385bb4d KW |
551 | #ifdef WIN32 |
552 | ||
553 | char * | |
554 | Perl_my_setlocale(pTHX_ int category, const char* locale) | |
555 | { | |
556 | /* This, for Windows, emulates POSIX setlocale() behavior. There is no | |
557 | * difference unless the input locale is "", which means on Windows to get | |
558 | * the machine default, which is set via the computer's "Regional and | |
559 | * Language Options" (or its current equivalent). In POSIX, it instead | |
560 | * means to find the locale from the user's environment. This routine | |
561 | * looks in the environment, and, if anything is found, uses that instead | |
562 | * of going to the machine default. If there is no environment override, | |
563 | * the machine default is used, as normal, by calling the real setlocale() | |
564 | * with "". The POSIX behavior is to use the LC_ALL variable if set; | |
565 | * otherwise to use the particular category's variable if set; otherwise to | |
566 | * use the LANG variable. */ | |
567 | ||
175c4cf9 | 568 | bool override_LC_ALL = FALSE; |
89f7b9aa KW |
569 | char * result; |
570 | ||
b385bb4d KW |
571 | if (locale && strEQ(locale, "")) { |
572 | # ifdef LC_ALL | |
573 | locale = PerlEnv_getenv("LC_ALL"); | |
574 | if (! locale) { | |
575 | #endif | |
576 | switch (category) { | |
577 | # ifdef LC_ALL | |
578 | case LC_ALL: | |
481465ea | 579 | override_LC_ALL = TRUE; |
b385bb4d KW |
580 | break; /* We already know its variable isn't set */ |
581 | # endif | |
582 | # ifdef USE_LOCALE_TIME | |
583 | case LC_TIME: | |
584 | locale = PerlEnv_getenv("LC_TIME"); | |
585 | break; | |
586 | # endif | |
587 | # ifdef USE_LOCALE_CTYPE | |
588 | case LC_CTYPE: | |
589 | locale = PerlEnv_getenv("LC_CTYPE"); | |
590 | break; | |
591 | # endif | |
592 | # ifdef USE_LOCALE_COLLATE | |
593 | case LC_COLLATE: | |
594 | locale = PerlEnv_getenv("LC_COLLATE"); | |
595 | break; | |
596 | # endif | |
597 | # ifdef USE_LOCALE_MONETARY | |
598 | case LC_MONETARY: | |
599 | locale = PerlEnv_getenv("LC_MONETARY"); | |
600 | break; | |
601 | # endif | |
602 | # ifdef USE_LOCALE_NUMERIC | |
603 | case LC_NUMERIC: | |
604 | locale = PerlEnv_getenv("LC_NUMERIC"); | |
605 | break; | |
606 | # endif | |
607 | # ifdef USE_LOCALE_MESSAGES | |
608 | case LC_MESSAGES: | |
609 | locale = PerlEnv_getenv("LC_MESSAGES"); | |
610 | break; | |
611 | # endif | |
612 | default: | |
613 | /* This is a category, like PAPER_SIZE that we don't | |
614 | * know about; and so can't provide a wrapper. */ | |
615 | break; | |
616 | } | |
617 | if (! locale) { | |
618 | locale = PerlEnv_getenv("LANG"); | |
481465ea | 619 | if (! locale) { |
b385bb4d KW |
620 | locale = ""; |
621 | } | |
622 | } | |
623 | # ifdef LC_ALL | |
624 | } | |
625 | # endif | |
626 | } | |
627 | ||
89f7b9aa | 628 | result = setlocale(category, locale); |
bbc98134 KW |
629 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", __FILE__, __LINE__, |
630 | _setlocale_debug_string(category, locale, result))); | |
89f7b9aa | 631 | |
481465ea | 632 | if (! override_LC_ALL) { |
89f7b9aa KW |
633 | return result; |
634 | } | |
635 | ||
dfd77d7a | 636 | /* Here the input category was LC_ALL, and we have set it to what is in the |
481465ea KW |
637 | * LANG variable or the system default if there is no LANG. But these have |
638 | * lower priority than the other LC_foo variables, so override it for each | |
639 | * one that is set. (If they are set to "", it means to use the same thing | |
640 | * we just set LC_ALL to, so can skip) */ | |
89f7b9aa KW |
641 | # ifdef USE_LOCALE_TIME |
642 | result = PerlEnv_getenv("LC_TIME"); | |
730252b2 | 643 | if (result && strNE(result, "")) { |
89f7b9aa | 644 | setlocale(LC_TIME, result); |
bbc98134 KW |
645 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
646 | __FILE__, __LINE__, | |
647 | _setlocale_debug_string(LC_TIME, result, "not captured"))); | |
89f7b9aa KW |
648 | } |
649 | # endif | |
650 | # ifdef USE_LOCALE_CTYPE | |
651 | result = PerlEnv_getenv("LC_CTYPE"); | |
730252b2 | 652 | if (result && strNE(result, "")) { |
89f7b9aa | 653 | setlocale(LC_CTYPE, result); |
bbc98134 KW |
654 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
655 | __FILE__, __LINE__, | |
656 | _setlocale_debug_string(LC_CTYPE, result, "not captured"))); | |
89f7b9aa KW |
657 | } |
658 | # endif | |
659 | # ifdef USE_LOCALE_COLLATE | |
660 | result = PerlEnv_getenv("LC_COLLATE"); | |
730252b2 | 661 | if (result && strNE(result, "")) { |
89f7b9aa | 662 | setlocale(LC_COLLATE, result); |
bbc98134 KW |
663 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
664 | __FILE__, __LINE__, | |
665 | _setlocale_debug_string(LC_COLLATE, result, "not captured"))); | |
89f7b9aa KW |
666 | } |
667 | # endif | |
668 | # ifdef USE_LOCALE_MONETARY | |
669 | result = PerlEnv_getenv("LC_MONETARY"); | |
730252b2 | 670 | if (result && strNE(result, "")) { |
89f7b9aa | 671 | setlocale(LC_MONETARY, result); |
bbc98134 KW |
672 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
673 | __FILE__, __LINE__, | |
674 | _setlocale_debug_string(LC_MONETARY, result, "not captured"))); | |
89f7b9aa KW |
675 | } |
676 | # endif | |
677 | # ifdef USE_LOCALE_NUMERIC | |
678 | result = PerlEnv_getenv("LC_NUMERIC"); | |
730252b2 | 679 | if (result && strNE(result, "")) { |
89f7b9aa | 680 | setlocale(LC_NUMERIC, result); |
bbc98134 KW |
681 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
682 | __FILE__, __LINE__, | |
683 | _setlocale_debug_string(LC_NUMERIC, result, "not captured"))); | |
89f7b9aa KW |
684 | } |
685 | # endif | |
686 | # ifdef USE_LOCALE_MESSAGES | |
687 | result = PerlEnv_getenv("LC_MESSAGES"); | |
730252b2 | 688 | if (result && strNE(result, "")) { |
89f7b9aa | 689 | setlocale(LC_MESSAGES, result); |
bbc98134 KW |
690 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
691 | __FILE__, __LINE__, | |
692 | _setlocale_debug_string(LC_MESSAGES, result, "not captured"))); | |
89f7b9aa KW |
693 | } |
694 | # endif | |
695 | ||
bbc98134 KW |
696 | result = setlocale(LC_ALL, NULL); |
697 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", | |
698 | __FILE__, __LINE__, | |
699 | _setlocale_debug_string(LC_ALL, NULL, result))); | |
89f7b9aa | 700 | |
bbc98134 | 701 | return result; |
b385bb4d KW |
702 | } |
703 | ||
704 | #endif | |
705 | ||
706 | ||
98994639 HS |
707 | /* |
708 | * Initialize locale awareness. | |
709 | */ | |
710 | int | |
711 | Perl_init_i18nl10n(pTHX_ int printwarn) | |
712 | { | |
0e92a118 KW |
713 | /* printwarn is |
714 | * | |
715 | * 0 if not to output warning when setup locale is bad | |
716 | * 1 if to output warning based on value of PERL_BADLANG | |
717 | * >1 if to output regardless of PERL_BADLANG | |
718 | * | |
719 | * returns | |
98994639 | 720 | * 1 = set ok or not applicable, |
0e92a118 KW |
721 | * 0 = fallback to a locale of lower priority |
722 | * -1 = fallback to all locales failed, not even to the C locale | |
6b058d42 KW |
723 | * |
724 | * Under -DDEBUGGING, if the environment variable PERL_DEBUG_LOCALE_INIT is | |
725 | * set, debugging information is output. | |
726 | * | |
727 | * This looks more complicated than it is, mainly due to the #ifdefs. | |
728 | * | |
729 | * We try to set LC_ALL to the value determined by the environment. If | |
730 | * there is no LC_ALL on this platform, we try the individual categories we | |
731 | * know about. If this works, we are done. | |
732 | * | |
733 | * But if it doesn't work, we have to do something else. We search the | |
734 | * environment variables ourselves instead of relying on the system to do | |
735 | * it. We look at, in order, LC_ALL, LANG, a system default locale (if we | |
736 | * think there is one), and the ultimate fallback "C". This is all done in | |
737 | * the same loop as above to avoid duplicating code, but it makes things | |
738 | * more complex. After the original failure, we add the fallback | |
739 | * possibilities to the list of locales to try, and iterate the loop | |
740 | * through them all until one succeeds. | |
741 | * | |
742 | * On Ultrix, the locale MUST come from the environment, so there is | |
743 | * preliminary code to set it. I (khw) am not sure that it is necessary, | |
744 | * and that this couldn't be folded into the loop, but barring any real | |
745 | * platforms to test on, it's staying as-is | |
746 | * | |
747 | * A slight complication is that in embedded Perls, the locale may already | |
748 | * be set-up, and we don't want to get it from the normal environment | |
749 | * variables. This is handled by having a special environment variable | |
750 | * indicate we're in this situation. We simply set setlocale's 2nd | |
751 | * parameter to be a NULL instead of "". That indicates to setlocale that | |
752 | * it is not to change anything, but to return the current value, | |
753 | * effectively initializing perl's db to what the locale already is. | |
754 | * | |
755 | * We play the same trick with NULL if a LC_ALL succeeds. We call | |
756 | * setlocale() on the individual categores with NULL to get their existing | |
757 | * values for our db, instead of trying to change them. | |
758 | * */ | |
98994639 | 759 | |
0e92a118 KW |
760 | int ok = 1; |
761 | ||
98994639 | 762 | #if defined(USE_LOCALE) |
98994639 HS |
763 | #ifdef USE_LOCALE_CTYPE |
764 | char *curctype = NULL; | |
765 | #endif /* USE_LOCALE_CTYPE */ | |
766 | #ifdef USE_LOCALE_COLLATE | |
767 | char *curcoll = NULL; | |
768 | #endif /* USE_LOCALE_COLLATE */ | |
769 | #ifdef USE_LOCALE_NUMERIC | |
770 | char *curnum = NULL; | |
771 | #endif /* USE_LOCALE_NUMERIC */ | |
772 | #ifdef __GLIBC__ | |
175c4cf9 | 773 | const char * const language = savepv(PerlEnv_getenv("LANGUAGE")); |
98994639 | 774 | #endif |
65ebb059 | 775 | |
ccd65d51 KW |
776 | /* NULL uses the existing already set up locale */ |
777 | const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT")) | |
778 | ? NULL | |
779 | : ""; | |
5d1187d1 KW |
780 | #ifdef DEBUGGING |
781 | const bool debug = (PerlEnv_getenv("PERL_DEBUG_LOCALE_INIT")) | |
782 | ? TRUE | |
783 | : FALSE; | |
784 | # define DEBUG_LOCALE_INIT(category, locale, result) \ | |
785 | STMT_START { \ | |
786 | if (debug) { \ | |
787 | PerlIO_printf(Perl_debug_log, \ | |
788 | "%s:%d: %s\n", \ | |
789 | __FILE__, __LINE__, \ | |
790 | _setlocale_debug_string(category, \ | |
791 | locale, \ | |
792 | result)); \ | |
793 | } \ | |
794 | } STMT_END | |
795 | #else | |
796 | # define DEBUG_LOCALE_INIT(a,b,c) | |
797 | #endif | |
c3fcd832 KW |
798 | const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */ |
799 | unsigned int trial_locales_count; | |
175c4cf9 KW |
800 | const char * const lc_all = savepv(PerlEnv_getenv("LC_ALL")); |
801 | const char * const lang = savepv(PerlEnv_getenv("LANG")); | |
98994639 | 802 | bool setlocale_failure = FALSE; |
65ebb059 KW |
803 | unsigned int i; |
804 | char *p; | |
175c4cf9 KW |
805 | |
806 | /* A later getenv() could zap this, so only use here */ | |
807 | const char * const bad_lang_use_once = PerlEnv_getenv("PERL_BADLANG"); | |
808 | ||
809 | const bool locwarn = (printwarn > 1 | |
810 | || (printwarn | |
811 | && (! bad_lang_use_once | |
22ff3130 HS |
812 | || ( |
813 | /* disallow with "" or "0" */ | |
814 | *bad_lang_use_once | |
815 | && strNE("0", bad_lang_use_once))))); | |
0e92a118 | 816 | bool done = FALSE; |
5d1187d1 KW |
817 | char * sl_result; /* return from setlocale() */ |
818 | char * locale_param; | |
6bce99ee JH |
819 | #ifdef WIN32 |
820 | /* In some systems you can find out the system default locale | |
821 | * and use that as the fallback locale. */ | |
822 | # define SYSTEM_DEFAULT_LOCALE | |
823 | #endif | |
824 | #ifdef SYSTEM_DEFAULT_LOCALE | |
65ebb059 | 825 | const char *system_default_locale = NULL; |
6bce99ee | 826 | #endif |
98994639 | 827 | |
0e92a118 KW |
828 | #ifndef LOCALE_ENVIRON_REQUIRED |
829 | PERL_UNUSED_VAR(done); | |
5d1187d1 | 830 | PERL_UNUSED_VAR(locale_param); |
0e92a118 | 831 | #else |
98994639 HS |
832 | |
833 | /* | |
834 | * Ultrix setlocale(..., "") fails if there are no environment | |
835 | * variables from which to get a locale name. | |
836 | */ | |
837 | ||
b3e384bf | 838 | # ifdef LC_ALL |
98994639 | 839 | if (lang) { |
5d1187d1 KW |
840 | sl_result = my_setlocale(LC_ALL, setlocale_init); |
841 | DEBUG_LOCALE_INIT(LC_ALL, setlocale_init, sl_result); | |
842 | if (sl_result) | |
98994639 HS |
843 | done = TRUE; |
844 | else | |
845 | setlocale_failure = TRUE; | |
846 | } | |
5d1187d1 | 847 | if (! setlocale_failure) { |
b3e384bf | 848 | # ifdef USE_LOCALE_CTYPE |
5d1187d1 KW |
849 | locale_param = (! done && (lang || PerlEnv_getenv("LC_CTYPE"))) |
850 | ? setlocale_init | |
851 | : NULL; | |
852 | curctype = my_setlocale(LC_CTYPE, locale_param); | |
853 | DEBUG_LOCALE_INIT(LC_CTYPE, locale_param, sl_result); | |
854 | if (! curctype) | |
98994639 HS |
855 | setlocale_failure = TRUE; |
856 | else | |
857 | curctype = savepv(curctype); | |
b3e384bf KW |
858 | # endif /* USE_LOCALE_CTYPE */ |
859 | # ifdef USE_LOCALE_COLLATE | |
5d1187d1 KW |
860 | locale_param = (! done && (lang || PerlEnv_getenv("LC_COLLATE"))) |
861 | ? setlocale_init | |
862 | : NULL; | |
863 | curcoll = my_setlocale(LC_COLLATE, locale_param); | |
864 | DEBUG_LOCALE_INIT(LC_COLLATE, locale_param, sl_result); | |
865 | if (! curcoll) | |
98994639 HS |
866 | setlocale_failure = TRUE; |
867 | else | |
868 | curcoll = savepv(curcoll); | |
b3e384bf KW |
869 | # endif /* USE_LOCALE_COLLATE */ |
870 | # ifdef USE_LOCALE_NUMERIC | |
5d1187d1 KW |
871 | locale_param = (! done && (lang || PerlEnv_getenv("LC_NUMERIC"))) |
872 | ? setlocale_init | |
873 | : NULL; | |
874 | curnum = my_setlocale(LC_NUMERIC, locale_param); | |
875 | DEBUG_LOCALE_INIT(LC_NUMERIC, locale_param, sl_result); | |
876 | if (! curnum) | |
98994639 HS |
877 | setlocale_failure = TRUE; |
878 | else | |
879 | curnum = savepv(curnum); | |
b3e384bf | 880 | # endif /* USE_LOCALE_NUMERIC */ |
a782673d | 881 | # ifdef USE_LOCALE_MESSAGES |
5d1187d1 KW |
882 | locale_param = (! done && (lang || PerlEnv_getenv("LC_MESSAGES"))) |
883 | ? setlocale_init | |
884 | : NULL; | |
885 | sl_result = my_setlocale(LC_MESSAGES, locale_param); | |
886 | DEBUG_LOCALE_INIT(LC_MESSAGES, locale_param, sl_result); | |
887 | if (! sl_result) | |
a782673d KW |
888 | setlocale_failure = TRUE; |
889 | } | |
890 | # endif /* USE_LOCALE_MESSAGES */ | |
c835d6be | 891 | # ifdef USE_LOCALE_MONETARY |
5d1187d1 KW |
892 | locale_param = (! done && (lang || PerlEnv_getenv("LC_MONETARY"))) |
893 | ? setlocale_init | |
894 | : NULL; | |
895 | sl_result = my_setlocale(LC_MONETARY, locale_param); | |
896 | DEBUG_LOCALE_INIT(LC_MONETARY, locale_param, sl_result); | |
897 | if (! sl_result) { | |
c835d6be KW |
898 | setlocale_failure = TRUE; |
899 | } | |
900 | # endif /* USE_LOCALE_MONETARY */ | |
98994639 HS |
901 | } |
902 | ||
b3e384bf | 903 | # endif /* LC_ALL */ |
98994639 HS |
904 | |
905 | #endif /* !LOCALE_ENVIRON_REQUIRED */ | |
906 | ||
65ebb059 | 907 | /* We try each locale in the list until we get one that works, or exhaust |
20a240df KW |
908 | * the list. Normally the loop is executed just once. But if setting the |
909 | * locale fails, inside the loop we add fallback trials to the array and so | |
910 | * will execute the loop multiple times */ | |
c3fcd832 KW |
911 | trial_locales[0] = setlocale_init; |
912 | trial_locales_count = 1; | |
65ebb059 KW |
913 | for (i= 0; i < trial_locales_count; i++) { |
914 | const char * trial_locale = trial_locales[i]; | |
915 | ||
916 | if (i > 0) { | |
917 | ||
918 | /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED | |
919 | * when i==0, but I (khw) don't think that behavior makes much | |
920 | * sense */ | |
921 | setlocale_failure = FALSE; | |
922 | ||
6bce99ee JH |
923 | #ifdef SYSTEM_DEFAULT_LOCALE |
924 | # ifdef WIN32 | |
65ebb059 KW |
925 | /* On Windows machines, an entry of "" after the 0th means to use |
926 | * the system default locale, which we now proceed to get. */ | |
927 | if (strEQ(trial_locale, "")) { | |
928 | unsigned int j; | |
929 | ||
930 | /* Note that this may change the locale, but we are going to do | |
931 | * that anyway just below */ | |
932 | system_default_locale = setlocale(LC_ALL, ""); | |
5d1187d1 | 933 | DEBUG_LOCALE_INIT(LC_ALL, "", system_default_locale); |
65ebb059 KW |
934 | |
935 | /* Skip if invalid or it's already on the list of locales to | |
936 | * try */ | |
937 | if (! system_default_locale) { | |
938 | goto next_iteration; | |
939 | } | |
940 | for (j = 0; j < trial_locales_count; j++) { | |
941 | if (strEQ(system_default_locale, trial_locales[j])) { | |
942 | goto next_iteration; | |
943 | } | |
944 | } | |
945 | ||
946 | trial_locale = system_default_locale; | |
947 | } | |
6bce99ee JH |
948 | # endif /* WIN32 */ |
949 | #endif /* SYSTEM_DEFAULT_LOCALE */ | |
65ebb059 KW |
950 | } |
951 | ||
98994639 | 952 | #ifdef LC_ALL |
5d1187d1 KW |
953 | sl_result = my_setlocale(LC_ALL, trial_locale); |
954 | DEBUG_LOCALE_INIT(LC_ALL, trial_locale, sl_result); | |
955 | if (! sl_result) { | |
49c85077 | 956 | setlocale_failure = TRUE; |
7cd8b568 KW |
957 | } |
958 | else { | |
959 | /* Since LC_ALL succeeded, it should have changed all the other | |
960 | * categories it can to its value; so we massage things so that the | |
961 | * setlocales below just return their category's current values. | |
962 | * This adequately handles the case in NetBSD where LC_COLLATE may | |
963 | * not be defined for a locale, and setting it individually will | |
964 | * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to | |
965 | * the POSIX locale. */ | |
966 | trial_locale = NULL; | |
967 | } | |
98994639 HS |
968 | #endif /* LC_ALL */ |
969 | ||
49c85077 | 970 | if (!setlocale_failure) { |
98994639 | 971 | #ifdef USE_LOCALE_CTYPE |
49c85077 | 972 | Safefree(curctype); |
5d1187d1 KW |
973 | curctype = my_setlocale(LC_CTYPE, trial_locale); |
974 | DEBUG_LOCALE_INIT(LC_CTYPE, trial_locale, curctype); | |
975 | if (! curctype) | |
49c85077 KW |
976 | setlocale_failure = TRUE; |
977 | else | |
978 | curctype = savepv(curctype); | |
98994639 HS |
979 | #endif /* USE_LOCALE_CTYPE */ |
980 | #ifdef USE_LOCALE_COLLATE | |
49c85077 | 981 | Safefree(curcoll); |
5d1187d1 KW |
982 | curcoll = my_setlocale(LC_COLLATE, trial_locale); |
983 | DEBUG_LOCALE_INIT(LC_COLLATE, trial_locale, curcoll); | |
984 | if (! curcoll) | |
49c85077 KW |
985 | setlocale_failure = TRUE; |
986 | else | |
987 | curcoll = savepv(curcoll); | |
98994639 HS |
988 | #endif /* USE_LOCALE_COLLATE */ |
989 | #ifdef USE_LOCALE_NUMERIC | |
49c85077 | 990 | Safefree(curnum); |
5d1187d1 KW |
991 | curnum = my_setlocale(LC_NUMERIC, trial_locale); |
992 | DEBUG_LOCALE_INIT(LC_NUMERIC, trial_locale, curnum); | |
993 | if (! curnum) | |
49c85077 KW |
994 | setlocale_failure = TRUE; |
995 | else | |
996 | curnum = savepv(curnum); | |
98994639 | 997 | #endif /* USE_LOCALE_NUMERIC */ |
a782673d | 998 | #ifdef USE_LOCALE_MESSAGES |
5d1187d1 KW |
999 | sl_result = my_setlocale(LC_MESSAGES, trial_locale); |
1000 | DEBUG_LOCALE_INIT(LC_MESSAGES, trial_locale, sl_result); | |
1001 | if (! (sl_result)) | |
a782673d KW |
1002 | setlocale_failure = TRUE; |
1003 | #endif /* USE_LOCALE_MESSAGES */ | |
c835d6be | 1004 | #ifdef USE_LOCALE_MONETARY |
5d1187d1 KW |
1005 | sl_result = my_setlocale(LC_MONETARY, trial_locale); |
1006 | DEBUG_LOCALE_INIT(LC_MONETARY, trial_locale, sl_result); | |
1007 | if (! (sl_result)) | |
c835d6be KW |
1008 | setlocale_failure = TRUE; |
1009 | #endif /* USE_LOCALE_MONETARY */ | |
1010 | ||
49c85077 KW |
1011 | if (! setlocale_failure) { /* Success */ |
1012 | break; | |
1013 | } | |
65ebb059 | 1014 | } |
98994639 | 1015 | |
49c85077 KW |
1016 | /* Here, something failed; will need to try a fallback. */ |
1017 | ok = 0; | |
65ebb059 | 1018 | |
49c85077 KW |
1019 | if (i == 0) { |
1020 | unsigned int j; | |
98994639 | 1021 | |
65ebb059 | 1022 | if (locwarn) { /* Output failure info only on the first one */ |
98994639 HS |
1023 | #ifdef LC_ALL |
1024 | ||
49c85077 KW |
1025 | PerlIO_printf(Perl_error_log, |
1026 | "perl: warning: Setting locale failed.\n"); | |
98994639 HS |
1027 | |
1028 | #else /* !LC_ALL */ | |
1029 | ||
49c85077 KW |
1030 | PerlIO_printf(Perl_error_log, |
1031 | "perl: warning: Setting locale failed for the categories:\n\t"); | |
20a240df | 1032 | # ifdef USE_LOCALE_CTYPE |
49c85077 KW |
1033 | if (! curctype) |
1034 | PerlIO_printf(Perl_error_log, "LC_CTYPE "); | |
20a240df KW |
1035 | # endif /* USE_LOCALE_CTYPE */ |
1036 | # ifdef USE_LOCALE_COLLATE | |
49c85077 KW |
1037 | if (! curcoll) |
1038 | PerlIO_printf(Perl_error_log, "LC_COLLATE "); | |
20a240df KW |
1039 | # endif /* USE_LOCALE_COLLATE */ |
1040 | # ifdef USE_LOCALE_NUMERIC | |
49c85077 KW |
1041 | if (! curnum) |
1042 | PerlIO_printf(Perl_error_log, "LC_NUMERIC "); | |
20a240df | 1043 | # endif /* USE_LOCALE_NUMERIC */ |
a782673d | 1044 | PerlIO_printf(Perl_error_log, "and possibly others\n"); |
98994639 HS |
1045 | |
1046 | #endif /* LC_ALL */ | |
1047 | ||
49c85077 KW |
1048 | PerlIO_printf(Perl_error_log, |
1049 | "perl: warning: Please check that your locale settings:\n"); | |
98994639 HS |
1050 | |
1051 | #ifdef __GLIBC__ | |
49c85077 KW |
1052 | PerlIO_printf(Perl_error_log, |
1053 | "\tLANGUAGE = %c%s%c,\n", | |
1054 | language ? '"' : '(', | |
1055 | language ? language : "unset", | |
1056 | language ? '"' : ')'); | |
98994639 HS |
1057 | #endif |
1058 | ||
49c85077 KW |
1059 | PerlIO_printf(Perl_error_log, |
1060 | "\tLC_ALL = %c%s%c,\n", | |
1061 | lc_all ? '"' : '(', | |
1062 | lc_all ? lc_all : "unset", | |
1063 | lc_all ? '"' : ')'); | |
98994639 HS |
1064 | |
1065 | #if defined(USE_ENVIRON_ARRAY) | |
49c85077 KW |
1066 | { |
1067 | char **e; | |
1068 | for (e = environ; *e; e++) { | |
1069 | if (strnEQ(*e, "LC_", 3) | |
1070 | && strnNE(*e, "LC_ALL=", 7) | |
1071 | && (p = strchr(*e, '='))) | |
1072 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", | |
1073 | (int)(p - *e), *e, p + 1); | |
1074 | } | |
1075 | } | |
98994639 | 1076 | #else |
49c85077 KW |
1077 | PerlIO_printf(Perl_error_log, |
1078 | "\t(possibly more locale environment variables)\n"); | |
98994639 HS |
1079 | #endif |
1080 | ||
49c85077 KW |
1081 | PerlIO_printf(Perl_error_log, |
1082 | "\tLANG = %c%s%c\n", | |
1083 | lang ? '"' : '(', | |
1084 | lang ? lang : "unset", | |
1085 | lang ? '"' : ')'); | |
98994639 | 1086 | |
49c85077 KW |
1087 | PerlIO_printf(Perl_error_log, |
1088 | " are supported and installed on your system.\n"); | |
1089 | } | |
98994639 | 1090 | |
65ebb059 | 1091 | /* Calculate what fallback locales to try. We have avoided this |
f6bab5f6 | 1092 | * until we have to, because failure is quite unlikely. This will |
65ebb059 KW |
1093 | * usually change the upper bound of the loop we are in. |
1094 | * | |
1095 | * Since the system's default way of setting the locale has not | |
1096 | * found one that works, We use Perl's defined ordering: LC_ALL, | |
1097 | * LANG, and the C locale. We don't try the same locale twice, so | |
1098 | * don't add to the list if already there. (On POSIX systems, the | |
1099 | * LC_ALL element will likely be a repeat of the 0th element "", | |
6b058d42 KW |
1100 | * but there's no harm done by doing it explicitly. |
1101 | * | |
1102 | * Note that this tries the LC_ALL environment variable even on | |
1103 | * systems which have no LC_ALL locale setting. This may or may | |
1104 | * not have been originally intentional, but there's no real need | |
1105 | * to change the behavior. */ | |
65ebb059 KW |
1106 | if (lc_all) { |
1107 | for (j = 0; j < trial_locales_count; j++) { | |
1108 | if (strEQ(lc_all, trial_locales[j])) { | |
1109 | goto done_lc_all; | |
1110 | } | |
1111 | } | |
1112 | trial_locales[trial_locales_count++] = lc_all; | |
1113 | } | |
1114 | done_lc_all: | |
98994639 | 1115 | |
65ebb059 KW |
1116 | if (lang) { |
1117 | for (j = 0; j < trial_locales_count; j++) { | |
1118 | if (strEQ(lang, trial_locales[j])) { | |
1119 | goto done_lang; | |
1120 | } | |
1121 | } | |
1122 | trial_locales[trial_locales_count++] = lang; | |
1123 | } | |
1124 | done_lang: | |
1125 | ||
1126 | #if defined(WIN32) && defined(LC_ALL) | |
1127 | /* For Windows, we also try the system default locale before "C". | |
1128 | * (If there exists a Windows without LC_ALL we skip this because | |
1129 | * it gets too complicated. For those, the "C" is the next | |
1130 | * fallback possibility). The "" is the same as the 0th element of | |
1131 | * the array, but the code at the loop above knows to treat it | |
1132 | * differently when not the 0th */ | |
1133 | trial_locales[trial_locales_count++] = ""; | |
1134 | #endif | |
1135 | ||
1136 | for (j = 0; j < trial_locales_count; j++) { | |
1137 | if (strEQ("C", trial_locales[j])) { | |
1138 | goto done_C; | |
1139 | } | |
1140 | } | |
1141 | trial_locales[trial_locales_count++] = "C"; | |
98994639 | 1142 | |
65ebb059 KW |
1143 | done_C: ; |
1144 | } /* end of first time through the loop */ | |
98994639 | 1145 | |
65ebb059 KW |
1146 | #ifdef WIN32 |
1147 | next_iteration: ; | |
1148 | #endif | |
1149 | ||
1150 | } /* end of looping through the trial locales */ | |
1151 | ||
1152 | if (ok < 1) { /* If we tried to fallback */ | |
1153 | const char* msg; | |
1154 | if (! setlocale_failure) { /* fallback succeeded */ | |
1155 | msg = "Falling back to"; | |
1156 | } | |
1157 | else { /* fallback failed */ | |
98994639 | 1158 | |
65ebb059 KW |
1159 | /* We dropped off the end of the loop, so have to decrement i to |
1160 | * get back to the value the last time through */ | |
1161 | i--; | |
98994639 | 1162 | |
65ebb059 KW |
1163 | ok = -1; |
1164 | msg = "Failed to fall back to"; | |
1165 | ||
1166 | /* To continue, we should use whatever values we've got */ | |
98994639 | 1167 | #ifdef USE_LOCALE_CTYPE |
49c85077 KW |
1168 | Safefree(curctype); |
1169 | curctype = savepv(setlocale(LC_CTYPE, NULL)); | |
5d1187d1 | 1170 | DEBUG_LOCALE_INIT(LC_CTYPE, NULL, curctype); |
98994639 HS |
1171 | #endif /* USE_LOCALE_CTYPE */ |
1172 | #ifdef USE_LOCALE_COLLATE | |
49c85077 KW |
1173 | Safefree(curcoll); |
1174 | curcoll = savepv(setlocale(LC_COLLATE, NULL)); | |
5d1187d1 | 1175 | DEBUG_LOCALE_INIT(LC_COLLATE, NULL, curcoll); |
98994639 HS |
1176 | #endif /* USE_LOCALE_COLLATE */ |
1177 | #ifdef USE_LOCALE_NUMERIC | |
49c85077 KW |
1178 | Safefree(curnum); |
1179 | curnum = savepv(setlocale(LC_NUMERIC, NULL)); | |
5d1187d1 | 1180 | DEBUG_LOCALE_INIT(LC_NUMERIC, NULL, curnum); |
98994639 | 1181 | #endif /* USE_LOCALE_NUMERIC */ |
65ebb059 KW |
1182 | } |
1183 | ||
1184 | if (locwarn) { | |
1185 | const char * description; | |
1186 | const char * name = ""; | |
1187 | if (strEQ(trial_locales[i], "C")) { | |
1188 | description = "the standard locale"; | |
1189 | name = "C"; | |
1190 | } | |
6bce99ee | 1191 | #ifdef SYSTEM_DEFAULT_LOCALE |
65ebb059 KW |
1192 | else if (strEQ(trial_locales[i], "")) { |
1193 | description = "the system default locale"; | |
1194 | if (system_default_locale) { | |
1195 | name = system_default_locale; | |
1196 | } | |
1197 | } | |
6bce99ee | 1198 | #endif /* SYSTEM_DEFAULT_LOCALE */ |
65ebb059 KW |
1199 | else { |
1200 | description = "a fallback locale"; | |
1201 | name = trial_locales[i]; | |
1202 | } | |
1203 | if (name && strNE(name, "")) { | |
1204 | PerlIO_printf(Perl_error_log, | |
1205 | "perl: warning: %s %s (\"%s\").\n", msg, description, name); | |
1206 | } | |
1207 | else { | |
1208 | PerlIO_printf(Perl_error_log, | |
1209 | "perl: warning: %s %s.\n", msg, description); | |
1210 | } | |
1211 | } | |
1212 | } /* End of tried to fallback */ | |
98994639 HS |
1213 | |
1214 | #ifdef USE_LOCALE_CTYPE | |
1215 | new_ctype(curctype); | |
1216 | #endif /* USE_LOCALE_CTYPE */ | |
1217 | ||
1218 | #ifdef USE_LOCALE_COLLATE | |
1219 | new_collate(curcoll); | |
1220 | #endif /* USE_LOCALE_COLLATE */ | |
1221 | ||
1222 | #ifdef USE_LOCALE_NUMERIC | |
1223 | new_numeric(curnum); | |
1224 | #endif /* USE_LOCALE_NUMERIC */ | |
b310b053 | 1225 | |
8ef6e574 | 1226 | #if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE) |
49c85077 KW |
1227 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE |
1228 | * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by | |
1229 | * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the | |
1230 | * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open | |
1231 | * discipline. */ | |
c1284011 | 1232 | PL_utf8locale = _is_cur_LC_category_utf8(LC_CTYPE); |
49c85077 | 1233 | |
a05d7ebb | 1234 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
fde18df1 JH |
1235 | This is an alternative to using the -C command line switch |
1236 | (the -C if present will override this). */ | |
1237 | { | |
dd374669 | 1238 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
a05d7ebb | 1239 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
5a22a2bb NC |
1240 | if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG) |
1241 | PL_utf8cache = -1; | |
b310b053 | 1242 | } |
ec71e770 | 1243 | #endif |
b310b053 | 1244 | |
98994639 | 1245 | #ifdef USE_LOCALE_CTYPE |
43c5f42d | 1246 | Safefree(curctype); |
98994639 HS |
1247 | #endif /* USE_LOCALE_CTYPE */ |
1248 | #ifdef USE_LOCALE_COLLATE | |
43c5f42d | 1249 | Safefree(curcoll); |
98994639 HS |
1250 | #endif /* USE_LOCALE_COLLATE */ |
1251 | #ifdef USE_LOCALE_NUMERIC | |
43c5f42d | 1252 | Safefree(curnum); |
98994639 | 1253 | #endif /* USE_LOCALE_NUMERIC */ |
8ef6e574 | 1254 | |
175c4cf9 KW |
1255 | #ifdef __GLIBC__ |
1256 | Safefree(language); | |
1257 | #endif | |
1258 | ||
1259 | Safefree(lc_all); | |
1260 | Safefree(lang); | |
1261 | ||
e3305790 KW |
1262 | #else /* !USE_LOCALE */ |
1263 | PERL_UNUSED_ARG(printwarn); | |
1264 | #endif /* USE_LOCALE */ | |
1265 | ||
98994639 HS |
1266 | return ok; |
1267 | } | |
1268 | ||
49c85077 | 1269 | |
98994639 HS |
1270 | #ifdef USE_LOCALE_COLLATE |
1271 | ||
1272 | /* | |
1273 | * mem_collxfrm() is a bit like strxfrm() but with two important | |
1274 | * differences. First, it handles embedded NULs. Second, it allocates | |
1275 | * a bit more memory than needed for the transformed data itself. | |
1276 | * The real transformed data begins at offset sizeof(collationix). | |
d35fca5f KW |
1277 | * *xlen is set to the length of that, and doesn't include the collation index |
1278 | * size. | |
98994639 HS |
1279 | * Please see sv_collxfrm() to see how this is used. |
1280 | */ | |
166f8a29 | 1281 | |
98994639 HS |
1282 | char * |
1283 | Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen) | |
1284 | { | |
1285 | char *xbuf; | |
1286 | STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */ | |
1287 | ||
7918f24d NC |
1288 | PERL_ARGS_ASSERT_MEM_COLLXFRM; |
1289 | ||
98994639 HS |
1290 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ |
1291 | /* the +1 is for the terminating NUL. */ | |
1292 | ||
1293 | xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1; | |
a02a5408 | 1294 | Newx(xbuf, xAlloc, char); |
bb0f664e | 1295 | if (UNLIKELY(! xbuf)) |
98994639 HS |
1296 | goto bad; |
1297 | ||
d35fca5f | 1298 | /* Store the collation id */ |
98994639 HS |
1299 | *(U32*)xbuf = PL_collation_ix; |
1300 | xout = sizeof(PL_collation_ix); | |
d35fca5f KW |
1301 | |
1302 | /* Then the transformation of the input. We loop until successful, or we | |
1303 | * give up */ | |
98994639 | 1304 | for (xin = 0; xin < len; ) { |
224e8ef5 | 1305 | Size_t xused; |
98994639 HS |
1306 | |
1307 | for (;;) { | |
1308 | xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout); | |
d35fca5f KW |
1309 | |
1310 | /* If the transformed string occupies less space than we told | |
1311 | * strxfrm() was available, it means it successfully transformed | |
1312 | * the whole string. */ | |
eb160463 | 1313 | if ((STRLEN)xused < xAlloc - xout) |
98994639 | 1314 | break; |
bb0f664e KW |
1315 | |
1316 | if (UNLIKELY(xused >= PERL_INT_MAX)) | |
1317 | goto bad; | |
d35fca5f KW |
1318 | |
1319 | /* Otherwise it should be that the transformation stopped in the | |
1320 | * middle because it ran out of space. Malloc more, and try again. | |
1321 | * */ | |
98994639 HS |
1322 | xAlloc = (2 * xAlloc) + 1; |
1323 | Renew(xbuf, xAlloc, char); | |
bb0f664e | 1324 | if (UNLIKELY(! xbuf)) |
98994639 HS |
1325 | goto bad; |
1326 | } | |
1327 | ||
1328 | xin += strlen(s + xin) + 1; | |
1329 | xout += xused; | |
1330 | ||
1331 | /* Embedded NULs are understood but silently skipped | |
1332 | * because they make no sense in locale collation. */ | |
1333 | } | |
1334 | ||
1335 | xbuf[xout] = '\0'; | |
1336 | *xlen = xout - sizeof(PL_collation_ix); | |
1337 | return xbuf; | |
1338 | ||
1339 | bad: | |
1340 | Safefree(xbuf); | |
1341 | *xlen = 0; | |
1342 | return NULL; | |
1343 | } | |
1344 | ||
1345 | #endif /* USE_LOCALE_COLLATE */ | |
1346 | ||
8ef6e574 KW |
1347 | #ifdef USE_LOCALE |
1348 | ||
c1284011 KW |
1349 | bool |
1350 | Perl__is_cur_LC_category_utf8(pTHX_ int category) | |
7d74bb61 KW |
1351 | { |
1352 | /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE | |
1353 | * otherwise. 'category' may not be LC_ALL. If the platform doesn't have | |
119ee68b | 1354 | * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence |
609548d2 KW |
1355 | * could give the wrong result. The result will very likely be correct for |
1356 | * languages that have commonly used non-ASCII characters, but for notably | |
1357 | * English, it comes down to if the locale's name ends in something like | |
1358 | * "UTF-8". It errs on the side of not being a UTF-8 locale. */ | |
7d74bb61 KW |
1359 | |
1360 | char *save_input_locale = NULL; | |
7d74bb61 KW |
1361 | STRLEN final_pos; |
1362 | ||
8ef6e574 | 1363 | #ifdef LC_ALL |
7d74bb61 | 1364 | assert(category != LC_ALL); |
8ef6e574 | 1365 | #endif |
7d74bb61 KW |
1366 | |
1367 | /* First dispose of the trivial cases */ | |
b07fffd1 | 1368 | save_input_locale = setlocale(category, NULL); |
7d74bb61 | 1369 | if (! save_input_locale) { |
69014004 KW |
1370 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1371 | "Could not find current locale for category %d\n", | |
1372 | category)); | |
7d74bb61 KW |
1373 | return FALSE; /* XXX maybe should croak */ |
1374 | } | |
b07fffd1 | 1375 | save_input_locale = stdize_locale(savepv(save_input_locale)); |
a39edc4c | 1376 | if (isNAME_C_OR_POSIX(save_input_locale)) { |
69014004 KW |
1377 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1378 | "Current locale for category %d is %s\n", | |
1379 | category, save_input_locale)); | |
b07fffd1 | 1380 | Safefree(save_input_locale); |
7d74bb61 KW |
1381 | return FALSE; |
1382 | } | |
1383 | ||
1d958db2 KW |
1384 | #if defined(USE_LOCALE_CTYPE) \ |
1385 | && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET))) | |
7d74bb61 | 1386 | |
1d958db2 | 1387 | { /* Next try nl_langinfo or MB_CUR_MAX if available */ |
7d74bb61 KW |
1388 | |
1389 | char *save_ctype_locale = NULL; | |
119ee68b | 1390 | bool is_utf8; |
7d74bb61 | 1391 | |
119ee68b | 1392 | if (category != LC_CTYPE) { /* These work only on LC_CTYPE */ |
7d74bb61 KW |
1393 | |
1394 | /* Get the current LC_CTYPE locale */ | |
4f72bb37 | 1395 | save_ctype_locale = setlocale(LC_CTYPE, NULL); |
7d74bb61 | 1396 | if (! save_ctype_locale) { |
69014004 KW |
1397 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1398 | "Could not find current locale for LC_CTYPE\n")); | |
7d74bb61 KW |
1399 | goto cant_use_nllanginfo; |
1400 | } | |
4f72bb37 | 1401 | save_ctype_locale = stdize_locale(savepv(save_ctype_locale)); |
7d74bb61 KW |
1402 | |
1403 | /* If LC_CTYPE and the desired category use the same locale, this | |
1404 | * means that finding the value for LC_CTYPE is the same as finding | |
1405 | * the value for the desired category. Otherwise, switch LC_CTYPE | |
1406 | * to the desired category's locale */ | |
1407 | if (strEQ(save_ctype_locale, save_input_locale)) { | |
1408 | Safefree(save_ctype_locale); | |
1409 | save_ctype_locale = NULL; | |
1410 | } | |
1411 | else if (! setlocale(LC_CTYPE, save_input_locale)) { | |
69014004 KW |
1412 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1413 | "Could not change LC_CTYPE locale to %s\n", | |
1414 | save_input_locale)); | |
7d74bb61 KW |
1415 | Safefree(save_ctype_locale); |
1416 | goto cant_use_nllanginfo; | |
1417 | } | |
1418 | } | |
1419 | ||
69014004 KW |
1420 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n", |
1421 | save_input_locale)); | |
1422 | ||
7d74bb61 | 1423 | /* Here the current LC_CTYPE is set to the locale of the category whose |
1d958db2 KW |
1424 | * information is desired. This means that nl_langinfo() and MB_CUR_MAX |
1425 | * should give the correct results */ | |
119ee68b | 1426 | |
1d958db2 KW |
1427 | # if defined(HAS_NL_LANGINFO) && defined(CODESET) |
1428 | { | |
4f72bb37 | 1429 | char *codeset = nl_langinfo(CODESET); |
1d958db2 | 1430 | if (codeset && strNE(codeset, "")) { |
4f72bb37 | 1431 | codeset = savepv(codeset); |
119ee68b | 1432 | |
1d958db2 KW |
1433 | /* If we switched LC_CTYPE, switch back */ |
1434 | if (save_ctype_locale) { | |
1435 | setlocale(LC_CTYPE, save_ctype_locale); | |
1436 | Safefree(save_ctype_locale); | |
1437 | } | |
1438 | ||
1439 | is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8")) | |
1440 | || foldEQ(codeset, STR_WITH_LEN("UTF8")); | |
1441 | ||
69014004 KW |
1442 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1443 | "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n", | |
1444 | codeset, is_utf8)); | |
1d958db2 KW |
1445 | Safefree(codeset); |
1446 | Safefree(save_input_locale); | |
1447 | return is_utf8; | |
1448 | } | |
119ee68b KW |
1449 | } |
1450 | ||
1d958db2 KW |
1451 | # endif |
1452 | # ifdef MB_CUR_MAX | |
1453 | ||
1454 | /* Here, either we don't have nl_langinfo, or it didn't return a | |
1455 | * codeset. Try MB_CUR_MAX */ | |
1456 | ||
119ee68b KW |
1457 | /* Standard UTF-8 needs at least 4 bytes to represent the maximum |
1458 | * Unicode code point. Since UTF-8 is the only non-single byte | |
1459 | * encoding we handle, we just say any such encoding is UTF-8, and if | |
1460 | * turns out to be wrong, other things will fail */ | |
1461 | is_utf8 = MB_CUR_MAX >= 4; | |
1462 | ||
69014004 KW |
1463 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1464 | "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n", | |
1465 | (int) MB_CUR_MAX, is_utf8)); | |
1466 | ||
119ee68b KW |
1467 | Safefree(save_input_locale); |
1468 | ||
1469 | # ifdef HAS_MBTOWC | |
1470 | ||
1471 | /* ... But, most system that have MB_CUR_MAX will also have mbtowc(), | |
1472 | * since they are both in the C99 standard. We can feed a known byte | |
1473 | * string to the latter function, and check that it gives the expected | |
1474 | * result */ | |
1475 | if (is_utf8) { | |
1476 | wchar_t wc; | |
856b881c | 1477 | PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */ |
69014004 | 1478 | errno = 0; |
f019f68f | 1479 | if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)) |
119ee68b KW |
1480 | != strlen(HYPHEN_UTF8) |
1481 | || wc != (wchar_t) 0x2010) | |
1482 | { | |
1483 | is_utf8 = FALSE; | |
abdcbdb8 | 1484 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", (unsigned int)wc)); |
69014004 KW |
1485 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1486 | "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n", | |
1487 | mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno)); | |
119ee68b KW |
1488 | } |
1489 | } | |
119ee68b KW |
1490 | # endif |
1491 | ||
1d958db2 KW |
1492 | /* If we switched LC_CTYPE, switch back */ |
1493 | if (save_ctype_locale) { | |
1494 | setlocale(LC_CTYPE, save_ctype_locale); | |
1495 | Safefree(save_ctype_locale); | |
119ee68b | 1496 | } |
7d74bb61 | 1497 | |
1d958db2 | 1498 | return is_utf8; |
119ee68b | 1499 | # endif |
7d74bb61 | 1500 | } |
119ee68b | 1501 | |
7d74bb61 KW |
1502 | cant_use_nllanginfo: |
1503 | ||
0080c90a KW |
1504 | #else /* nl_langinfo should work if available, so don't bother compiling this |
1505 | fallback code. The final fallback of looking at the name is | |
1506 | compiled, and will be executed if nl_langinfo fails */ | |
7d74bb61 | 1507 | |
97f4de96 KW |
1508 | /* nl_langinfo not available or failed somehow. Next try looking at the |
1509 | * currency symbol to see if it disambiguates things. Often that will be | |
1510 | * in the native script, and if the symbol isn't in UTF-8, we know that the | |
1511 | * locale isn't. If it is non-ASCII UTF-8, we infer that the locale is | |
609548d2 KW |
1512 | * too, as the odds of a non-UTF8 string being valid UTF-8 are quite small |
1513 | * */ | |
fa9b773e KW |
1514 | |
1515 | #ifdef HAS_LOCALECONV | |
fa9b773e | 1516 | # ifdef USE_LOCALE_MONETARY |
fa9b773e KW |
1517 | { |
1518 | char *save_monetary_locale = NULL; | |
fa9b773e | 1519 | bool only_ascii = FALSE; |
13542a67 KW |
1520 | bool is_utf8 = FALSE; |
1521 | struct lconv* lc; | |
fa9b773e | 1522 | |
97f4de96 KW |
1523 | /* Like above for LC_CTYPE, we first set LC_MONETARY to the locale of |
1524 | * the desired category, if it isn't that locale already */ | |
1525 | ||
fa9b773e KW |
1526 | if (category != LC_MONETARY) { |
1527 | ||
4f72bb37 | 1528 | save_monetary_locale = setlocale(LC_MONETARY, NULL); |
fa9b773e | 1529 | if (! save_monetary_locale) { |
69014004 KW |
1530 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1531 | "Could not find current locale for LC_MONETARY\n")); | |
fa9b773e KW |
1532 | goto cant_use_monetary; |
1533 | } | |
4f72bb37 | 1534 | save_monetary_locale = stdize_locale(savepv(save_monetary_locale)); |
fa9b773e | 1535 | |
13542a67 KW |
1536 | if (strEQ(save_monetary_locale, save_input_locale)) { |
1537 | Safefree(save_monetary_locale); | |
1538 | save_monetary_locale = NULL; | |
1539 | } | |
1540 | else if (! setlocale(LC_MONETARY, save_input_locale)) { | |
59c234b4 KW |
1541 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1542 | "Could not change LC_MONETARY locale to %s\n", | |
1543 | save_input_locale)); | |
1544 | Safefree(save_monetary_locale); | |
1545 | goto cant_use_monetary; | |
fa9b773e KW |
1546 | } |
1547 | } | |
1548 | ||
1549 | /* Here the current LC_MONETARY is set to the locale of the category | |
1550 | * whose information is desired. */ | |
1551 | ||
13542a67 KW |
1552 | lc = localeconv(); |
1553 | if (! lc | |
1554 | || ! lc->currency_symbol | |
9f10db87 | 1555 | || is_invariant_string((U8 *) lc->currency_symbol, 0)) |
13542a67 KW |
1556 | { |
1557 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Couldn't get currency symbol for %s, or contains only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale)); | |
1558 | only_ascii = TRUE; | |
1559 | } | |
1560 | else { | |
1561 | is_utf8 = is_utf8_string((U8 *) lc->currency_symbol, 0); | |
fa9b773e KW |
1562 | } |
1563 | ||
1564 | /* If we changed it, restore LC_MONETARY to its original locale */ | |
1565 | if (save_monetary_locale) { | |
1566 | setlocale(LC_MONETARY, save_monetary_locale); | |
1567 | Safefree(save_monetary_locale); | |
1568 | } | |
1569 | ||
13542a67 | 1570 | if (! only_ascii) { |
fa9b773e | 1571 | |
59c234b4 KW |
1572 | /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; |
1573 | * otherwise assume the locale is UTF-8 if and only if the symbol | |
1574 | * is non-ascii UTF-8. */ | |
1575 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?Currency symbol for %s is UTF-8=%d\n", | |
1576 | save_input_locale, is_utf8)); | |
1577 | Safefree(save_input_locale); | |
1578 | return is_utf8; | |
13542a67 | 1579 | } |
fa9b773e KW |
1580 | } |
1581 | cant_use_monetary: | |
1582 | ||
1583 | # endif /* USE_LOCALE_MONETARY */ | |
1584 | #endif /* HAS_LOCALECONV */ | |
1585 | ||
15f7e74e KW |
1586 | #if defined(HAS_STRFTIME) && defined(USE_LOCALE_TIME) |
1587 | ||
1588 | /* Still haven't found a non-ASCII string to disambiguate UTF-8 or not. Try | |
1589 | * the names of the months and weekdays, timezone, and am/pm indicator */ | |
1590 | { | |
1591 | char *save_time_locale = NULL; | |
1592 | int hour = 10; | |
1593 | bool is_dst = FALSE; | |
1594 | int dom = 1; | |
1595 | int month = 0; | |
1596 | int i; | |
1597 | char * formatted_time; | |
1598 | ||
1599 | ||
1600 | /* Like above for LC_MONETARY, we set LC_TIME to the locale of the | |
1601 | * desired category, if it isn't that locale already */ | |
1602 | ||
1603 | if (category != LC_TIME) { | |
1604 | ||
1605 | save_time_locale = setlocale(LC_TIME, NULL); | |
1606 | if (! save_time_locale) { | |
1607 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1608 | "Could not find current locale for LC_TIME\n")); | |
1609 | goto cant_use_time; | |
1610 | } | |
1611 | save_time_locale = stdize_locale(savepv(save_time_locale)); | |
1612 | ||
1613 | if (strEQ(save_time_locale, save_input_locale)) { | |
1614 | Safefree(save_time_locale); | |
1615 | save_time_locale = NULL; | |
1616 | } | |
1617 | else if (! setlocale(LC_TIME, save_input_locale)) { | |
1618 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1619 | "Could not change LC_TIME locale to %s\n", | |
1620 | save_input_locale)); | |
1621 | Safefree(save_time_locale); | |
1622 | goto cant_use_time; | |
1623 | } | |
1624 | } | |
1625 | ||
1626 | /* Here the current LC_TIME is set to the locale of the category | |
1627 | * whose information is desired. Look at all the days of the week and | |
9f10db87 | 1628 | * month names, and the timezone and am/pm indicator for UTF-8 variant |
15f7e74e KW |
1629 | * characters. The first such a one found will tell us if the locale |
1630 | * is UTF-8 or not */ | |
1631 | ||
1632 | for (i = 0; i < 7 + 12; i++) { /* 7 days; 12 months */ | |
1633 | formatted_time = my_strftime("%A %B %Z %p", | |
1634 | 0, 0, hour, dom, month, 112, 0, 0, is_dst); | |
9f10db87 | 1635 | if (! formatted_time || is_invariant_string((U8 *) formatted_time, 0)) { |
15f7e74e KW |
1636 | |
1637 | /* Here, we didn't find a non-ASCII. Try the next time through | |
1638 | * with the complemented dst and am/pm, and try with the next | |
1639 | * weekday. After we have gotten all weekdays, try the next | |
1640 | * month */ | |
1641 | is_dst = ! is_dst; | |
1642 | hour = (hour + 12) % 24; | |
1643 | dom++; | |
1644 | if (i > 6) { | |
1645 | month++; | |
1646 | } | |
1647 | continue; | |
1648 | } | |
1649 | ||
1650 | /* Here, we have a non-ASCII. Return TRUE is it is valid UTF8; | |
1651 | * false otherwise. But first, restore LC_TIME to its original | |
1652 | * locale if we changed it */ | |
1653 | if (save_time_locale) { | |
1654 | setlocale(LC_TIME, save_time_locale); | |
1655 | Safefree(save_time_locale); | |
1656 | } | |
1657 | ||
1658 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?time-related strings for %s are UTF-8=%d\n", | |
1659 | save_input_locale, | |
1660 | is_utf8_string((U8 *) formatted_time, 0))); | |
1661 | Safefree(save_input_locale); | |
1662 | return is_utf8_string((U8 *) formatted_time, 0); | |
1663 | } | |
1664 | ||
1665 | /* Falling off the end of the loop indicates all the names were just | |
1666 | * ASCII. Go on to the next test. If we changed it, restore LC_TIME | |
1667 | * to its original locale */ | |
1668 | if (save_time_locale) { | |
1669 | setlocale(LC_TIME, save_time_locale); | |
1670 | Safefree(save_time_locale); | |
1671 | } | |
1672 | DEBUG_L(PerlIO_printf(Perl_debug_log, "All time-related words for %s contain only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale)); | |
1673 | } | |
1674 | cant_use_time: | |
1675 | ||
1676 | #endif | |
1677 | ||
1678 | #if 0 && defined(USE_LOCALE_MESSAGES) && defined(HAS_SYS_ERRLIST) | |
855aeb93 JH |
1679 | |
1680 | /* This code is ifdefd out because it was found to not be necessary in testing | |
5857e934 KW |
1681 | * on our dromedary test machine, which has over 700 locales. There, this |
1682 | * added no value to looking at the currency symbol and the time strings. I | |
1683 | * left it in so as to avoid rewriting it if real-world experience indicates | |
1684 | * that dromedary is an outlier. Essentially, instead of returning abpve if we | |
855aeb93 JH |
1685 | * haven't found illegal utf8, we continue on and examine all the strerror() |
1686 | * messages on the platform for utf8ness. If all are ASCII, we still don't | |
1687 | * know the answer; but otherwise we have a pretty good indication of the | |
5857e934 KW |
1688 | * utf8ness. The reason this doesn't help much is that the messages may not |
1689 | * have been translated into the locale. The currency symbol and time strings | |
1690 | * are much more likely to have been translated. */ | |
1691 | { | |
855aeb93 | 1692 | int e; |
5857e934 KW |
1693 | bool is_utf8 = FALSE; |
1694 | bool non_ascii = FALSE; | |
855aeb93 | 1695 | char *save_messages_locale = NULL; |
5857e934 | 1696 | const char * errmsg = NULL; |
855aeb93 | 1697 | |
5857e934 KW |
1698 | /* Like above, we set LC_MESSAGES to the locale of the desired |
1699 | * category, if it isn't that locale already */ | |
855aeb93 JH |
1700 | |
1701 | if (category != LC_MESSAGES) { | |
1702 | ||
5857e934 | 1703 | save_messages_locale = setlocale(LC_MESSAGES, NULL); |
855aeb93 | 1704 | if (! save_messages_locale) { |
5857e934 KW |
1705 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1706 | "Could not find current locale for LC_MESSAGES\n")); | |
855aeb93 JH |
1707 | goto cant_use_messages; |
1708 | } | |
5857e934 | 1709 | save_messages_locale = stdize_locale(savepv(save_messages_locale)); |
855aeb93 JH |
1710 | |
1711 | if (strEQ(save_messages_locale, save_input_locale)) { | |
5857e934 KW |
1712 | Safefree(save_messages_locale); |
1713 | save_messages_locale = NULL; | |
855aeb93 JH |
1714 | } |
1715 | else if (! setlocale(LC_MESSAGES, save_input_locale)) { | |
5857e934 KW |
1716 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1717 | "Could not change LC_MESSAGES locale to %s\n", | |
1718 | save_input_locale)); | |
855aeb93 JH |
1719 | Safefree(save_messages_locale); |
1720 | goto cant_use_messages; | |
1721 | } | |
1722 | } | |
1723 | ||
1724 | /* Here the current LC_MESSAGES is set to the locale of the category | |
5857e934 KW |
1725 | * whose information is desired. Look through all the messages. We |
1726 | * can't use Strerror() here because it may expand to code that | |
1727 | * segfaults in miniperl */ | |
855aeb93 | 1728 | |
5857e934 KW |
1729 | for (e = 0; e <= sys_nerr; e++) { |
1730 | errno = 0; | |
1731 | errmsg = sys_errlist[e]; | |
1732 | if (errno || !errmsg) { | |
855aeb93 JH |
1733 | break; |
1734 | } | |
5857e934 | 1735 | errmsg = savepv(errmsg); |
9f10db87 | 1736 | if (! is_invariant_string((U8 *) errmsg, 0)) { |
5857e934 KW |
1737 | non_ascii = TRUE; |
1738 | is_utf8 = is_utf8_string((U8 *) errmsg, 0); | |
1739 | break; | |
855aeb93 JH |
1740 | } |
1741 | } | |
5857e934 | 1742 | Safefree(errmsg); |
855aeb93 JH |
1743 | |
1744 | /* And, if we changed it, restore LC_MESSAGES to its original locale */ | |
1745 | if (save_messages_locale) { | |
1746 | setlocale(LC_MESSAGES, save_messages_locale); | |
1747 | Safefree(save_messages_locale); | |
1748 | } | |
1749 | ||
5857e934 KW |
1750 | if (non_ascii) { |
1751 | ||
1752 | /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid, | |
1753 | * any non-ascii means it is one; otherwise we assume it isn't */ | |
1754 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?error messages for %s are UTF-8=%d\n", | |
1755 | save_input_locale, | |
1756 | is_utf8)); | |
1757 | Safefree(save_input_locale); | |
1758 | return is_utf8; | |
1759 | } | |
855aeb93 | 1760 | |
5857e934 | 1761 | DEBUG_L(PerlIO_printf(Perl_debug_log, "All error messages for %s contain only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale)); |
855aeb93 JH |
1762 | } |
1763 | cant_use_messages: | |
1764 | ||
1765 | #endif | |
fa9b773e | 1766 | |
0080c90a KW |
1767 | #endif /* the code that is compiled when no nl_langinfo */ |
1768 | ||
92c0a900 KW |
1769 | #ifndef EBCDIC /* On os390, even if the name ends with "UTF-8', it isn't a |
1770 | UTF-8 locale */ | |
97f4de96 KW |
1771 | /* As a last resort, look at the locale name to see if it matches |
1772 | * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the | |
1773 | * return of setlocale(), is actually defined to be opaque, so we can't | |
1774 | * really rely on the absence of various substrings in the name to indicate | |
1775 | * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to | |
1776 | * be a UTF-8 locale. Similarly for the other common names */ | |
1777 | ||
1778 | final_pos = strlen(save_input_locale) - 1; | |
1779 | if (final_pos >= 3) { | |
1780 | char *name = save_input_locale; | |
1781 | ||
1782 | /* Find next 'U' or 'u' and look from there */ | |
1783 | while ((name += strcspn(name, "Uu") + 1) | |
1784 | <= save_input_locale + final_pos - 2) | |
1785 | { | |
305b8651 KW |
1786 | if (!isALPHA_FOLD_NE(*name, 't') |
1787 | || isALPHA_FOLD_NE(*(name + 1), 'f')) | |
97f4de96 KW |
1788 | { |
1789 | continue; | |
1790 | } | |
1791 | name += 2; | |
1792 | if (*(name) == '-') { | |
1793 | if ((name > save_input_locale + final_pos - 1)) { | |
1794 | break; | |
1795 | } | |
1796 | name++; | |
1797 | } | |
1798 | if (*(name) == '8') { | |
97f4de96 KW |
1799 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1800 | "Locale %s ends with UTF-8 in name\n", | |
1801 | save_input_locale)); | |
00c54b9c | 1802 | Safefree(save_input_locale); |
97f4de96 KW |
1803 | return TRUE; |
1804 | } | |
1805 | } | |
1806 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1807 | "Locale %s doesn't end with UTF-8 in name\n", | |
1808 | save_input_locale)); | |
1809 | } | |
92c0a900 | 1810 | #endif |
97f4de96 KW |
1811 | |
1812 | #ifdef WIN32 | |
1813 | /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */ | |
1814 | if (final_pos >= 4 | |
1815 | && *(save_input_locale + final_pos - 0) == '1' | |
1816 | && *(save_input_locale + final_pos - 1) == '0' | |
1817 | && *(save_input_locale + final_pos - 2) == '0' | |
1818 | && *(save_input_locale + final_pos - 3) == '5' | |
1819 | && *(save_input_locale + final_pos - 4) == '6') | |
1820 | { | |
1821 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1822 | "Locale %s ends with 10056 in name, is UTF-8 locale\n", | |
1823 | save_input_locale)); | |
1824 | Safefree(save_input_locale); | |
1825 | return TRUE; | |
1826 | } | |
1827 | #endif | |
1828 | ||
1829 | /* Other common encodings are the ISO 8859 series, which aren't UTF-8. But | |
1830 | * since we are about to return FALSE anyway, there is no point in doing | |
1831 | * this extra work */ | |
1832 | #if 0 | |
1833 | if (instr(save_input_locale, "8859")) { | |
1834 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1835 | "Locale %s has 8859 in name, not UTF-8 locale\n", | |
1836 | save_input_locale)); | |
1837 | Safefree(save_input_locale); | |
1838 | return FALSE; | |
1839 | } | |
1840 | #endif | |
1841 | ||
69014004 KW |
1842 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1843 | "Assuming locale %s is not a UTF-8 locale\n", | |
1844 | save_input_locale)); | |
fa9b773e | 1845 | Safefree(save_input_locale); |
7d74bb61 KW |
1846 | return FALSE; |
1847 | } | |
1848 | ||
8ef6e574 | 1849 | #endif |
7d74bb61 | 1850 | |
d6ded950 KW |
1851 | |
1852 | bool | |
1853 | Perl__is_in_locale_category(pTHX_ const bool compiling, const int category) | |
1854 | { | |
1a4f13e1 | 1855 | dVAR; |
d6ded950 KW |
1856 | /* Internal function which returns if we are in the scope of a pragma that |
1857 | * enables the locale category 'category'. 'compiling' should indicate if | |
1858 | * this is during the compilation phase (TRUE) or not (FALSE). */ | |
1859 | ||
1860 | const COP * const cop = (compiling) ? &PL_compiling : PL_curcop; | |
1861 | ||
1862 | SV *categories = cop_hints_fetch_pvs(cop, "locale", 0); | |
1863 | if (! categories || categories == &PL_sv_placeholder) { | |
1864 | return FALSE; | |
1865 | } | |
1866 | ||
1867 | /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get | |
1868 | * a valid unsigned */ | |
1869 | assert(category >= -1); | |
1870 | return cBOOL(SvUV(categories) & (1U << (category + 1))); | |
1871 | } | |
1872 | ||
2c6ee1a7 KW |
1873 | char * |
1874 | Perl_my_strerror(pTHX_ const int errnum) { | |
a0b53297 | 1875 | dVAR; |
2c6ee1a7 KW |
1876 | |
1877 | /* Uses C locale for the error text unless within scope of 'use locale' for | |
1878 | * LC_MESSAGES */ | |
1879 | ||
1880 | #ifdef USE_LOCALE_MESSAGES | |
8ee4b769 | 1881 | if (! IN_LC(LC_MESSAGES)) { |
a0b53297 KW |
1882 | char * save_locale; |
1883 | ||
1884 | /* We have a critical section to prevent another thread from changing | |
1885 | * the locale out from under us (or zapping the buffer returned from | |
1886 | * setlocale() ) */ | |
1887 | LOCALE_LOCK; | |
1888 | ||
1889 | save_locale = setlocale(LC_MESSAGES, NULL); | |
a39edc4c | 1890 | if (! isNAME_C_OR_POSIX(save_locale)) { |
2c6ee1a7 KW |
1891 | char *errstr; |
1892 | ||
1893 | /* The next setlocale likely will zap this, so create a copy */ | |
1894 | save_locale = savepv(save_locale); | |
1895 | ||
1896 | setlocale(LC_MESSAGES, "C"); | |
1897 | ||
1898 | /* This points to the static space in Strerror, with all its | |
1899 | * limitations */ | |
1900 | errstr = Strerror(errnum); | |
1901 | ||
1902 | setlocale(LC_MESSAGES, save_locale); | |
1903 | Safefree(save_locale); | |
a0b53297 KW |
1904 | |
1905 | LOCALE_UNLOCK; | |
1906 | ||
2c6ee1a7 KW |
1907 | return errstr; |
1908 | } | |
a0b53297 KW |
1909 | |
1910 | LOCALE_UNLOCK; | |
2c6ee1a7 KW |
1911 | } |
1912 | #endif | |
1913 | ||
1914 | return Strerror(errnum); | |
1915 | } | |
1916 | ||
66610fdd | 1917 | /* |
747c467a KW |
1918 | |
1919 | =head1 Locale-related functions and macros | |
1920 | ||
1921 | =for apidoc sync_locale | |
1922 | ||
1923 | Changing the program's locale should be avoided by XS code. Nevertheless, | |
1924 | certain non-Perl libraries called from XS, such as C<Gtk> do so. When this | |
1925 | happens, Perl needs to be told that the locale has changed. Use this function | |
1926 | to do so, before returning to Perl. | |
1927 | ||
1928 | =cut | |
1929 | */ | |
1930 | ||
1931 | void | |
1932 | Perl_sync_locale(pTHX) | |
1933 | { | |
1934 | ||
1935 | #ifdef USE_LOCALE_CTYPE | |
1936 | new_ctype(setlocale(LC_CTYPE, NULL)); | |
1937 | #endif /* USE_LOCALE_CTYPE */ | |
1938 | ||
1939 | #ifdef USE_LOCALE_COLLATE | |
1940 | new_collate(setlocale(LC_COLLATE, NULL)); | |
1941 | #endif | |
1942 | ||
1943 | #ifdef USE_LOCALE_NUMERIC | |
1944 | set_numeric_local(); /* Switch from "C" to underlying LC_NUMERIC */ | |
1945 | new_numeric(setlocale(LC_NUMERIC, NULL)); | |
1946 | #endif /* USE_LOCALE_NUMERIC */ | |
1947 | ||
1948 | } | |
1949 | ||
5d1187d1 KW |
1950 | #if defined(DEBUGGING) && defined(USE_LOCALE) |
1951 | ||
1952 | char * | |
1953 | Perl__setlocale_debug_string(const int category, /* category number, | |
1954 | like LC_ALL */ | |
1955 | const char* const locale, /* locale name */ | |
1956 | ||
1957 | /* return value from setlocale() when attempting to | |
1958 | * set 'category' to 'locale' */ | |
1959 | const char* const retval) | |
1960 | { | |
1961 | /* Returns a pointer to a NUL-terminated string in static storage with | |
1962 | * added text about the info passed in. This is not thread safe and will | |
1963 | * be overwritten by the next call, so this should be used just to | |
fa07b8e5 | 1964 | * formulate a string to immediately print or savepv() on. */ |
5d1187d1 | 1965 | |
398a990f DM |
1966 | /* initialise to a non-null value to keep it out of BSS and so keep |
1967 | * -DPERL_GLOBAL_STRUCT_PRIVATE happy */ | |
1968 | static char ret[128] = "x"; | |
5d1187d1 | 1969 | |
fa07b8e5 | 1970 | my_strlcpy(ret, "setlocale(", sizeof(ret)); |
5d1187d1 KW |
1971 | |
1972 | switch (category) { | |
1973 | default: | |
fa07b8e5 | 1974 | my_snprintf(ret, sizeof(ret), "%s? %d", ret, category); |
5d1187d1 KW |
1975 | break; |
1976 | # ifdef LC_ALL | |
1977 | case LC_ALL: | |
fa07b8e5 | 1978 | my_strlcat(ret, "LC_ALL", sizeof(ret)); |
5d1187d1 KW |
1979 | break; |
1980 | # endif | |
1981 | # ifdef LC_CTYPE | |
1982 | case LC_CTYPE: | |
fa07b8e5 | 1983 | my_strlcat(ret, "LC_CTYPE", sizeof(ret)); |
5d1187d1 KW |
1984 | break; |
1985 | # endif | |
1986 | # ifdef LC_NUMERIC | |
1987 | case LC_NUMERIC: | |
fa07b8e5 | 1988 | my_strlcat(ret, "LC_NUMERIC", sizeof(ret)); |
5d1187d1 KW |
1989 | break; |
1990 | # endif | |
1991 | # ifdef LC_COLLATE | |
1992 | case LC_COLLATE: | |
fa07b8e5 | 1993 | my_strlcat(ret, "LC_COLLATE", sizeof(ret)); |
5d1187d1 KW |
1994 | break; |
1995 | # endif | |
1996 | # ifdef LC_TIME | |
1997 | case LC_TIME: | |
fa07b8e5 | 1998 | my_strlcat(ret, "LC_TIME", sizeof(ret)); |
5d1187d1 KW |
1999 | break; |
2000 | # endif | |
2001 | # ifdef LC_MONETARY | |
2002 | case LC_MONETARY: | |
fa07b8e5 | 2003 | my_strlcat(ret, "LC_MONETARY", sizeof(ret)); |
5d1187d1 KW |
2004 | break; |
2005 | # endif | |
2006 | # ifdef LC_MESSAGES | |
2007 | case LC_MESSAGES: | |
fa07b8e5 | 2008 | my_strlcat(ret, "LC_MESSAGES", sizeof(ret)); |
5d1187d1 KW |
2009 | break; |
2010 | # endif | |
2011 | } | |
2012 | ||
fa07b8e5 | 2013 | my_strlcat(ret, ", ", sizeof(ret)); |
5d1187d1 KW |
2014 | |
2015 | if (locale) { | |
fa07b8e5 KW |
2016 | my_strlcat(ret, "\"", sizeof(ret)); |
2017 | my_strlcat(ret, locale, sizeof(ret)); | |
2018 | my_strlcat(ret, "\"", sizeof(ret)); | |
5d1187d1 KW |
2019 | } |
2020 | else { | |
fa07b8e5 | 2021 | my_strlcat(ret, "NULL", sizeof(ret)); |
5d1187d1 KW |
2022 | } |
2023 | ||
fa07b8e5 | 2024 | my_strlcat(ret, ") returned ", sizeof(ret)); |
5d1187d1 KW |
2025 | |
2026 | if (retval) { | |
fa07b8e5 KW |
2027 | my_strlcat(ret, "\"", sizeof(ret)); |
2028 | my_strlcat(ret, retval, sizeof(ret)); | |
2029 | my_strlcat(ret, "\"", sizeof(ret)); | |
5d1187d1 KW |
2030 | } |
2031 | else { | |
fa07b8e5 | 2032 | my_strlcat(ret, "NULL", sizeof(ret)); |
5d1187d1 KW |
2033 | } |
2034 | ||
2035 | assert(strlen(ret) < sizeof(ret)); | |
2036 | ||
2037 | return ret; | |
2038 | } | |
2039 | ||
2040 | #endif | |
747c467a KW |
2041 | |
2042 | ||
2043 | /* | |
14d04a33 | 2044 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 2045 | */ |