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