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; |
6696cfa7 | 489 | *PL_strxfrm_min_char = '\0'; |
98994639 HS |
490 | return; |
491 | } | |
492 | ||
d35fca5f | 493 | /* If this is not the same locale as currently, set the new one up */ |
98994639 HS |
494 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
495 | ++PL_collation_ix; | |
496 | Safefree(PL_collation_name); | |
497 | PL_collation_name = stdize_locale(savepv(newcoll)); | |
a39edc4c | 498 | PL_collation_standard = isNAME_C_OR_POSIX(newcoll); |
00bf60ca KW |
499 | if (PL_collation_standard) { |
500 | goto is_standard_collation; | |
501 | } | |
98994639 | 502 | |
165a1c52 | 503 | PL_in_utf8_COLLATE_locale = _is_cur_LC_category_utf8(LC_COLLATE); |
6696cfa7 | 504 | *PL_strxfrm_min_char = '\0'; |
165a1c52 | 505 | |
59c018b9 KW |
506 | /* A locale collation definition includes primary, secondary, tertiary, |
507 | * etc. weights for each character. To sort, the primary weights are | |
508 | * used, and only if they compare equal, then the secondary weights are | |
509 | * used, and only if they compare equal, then the tertiary, etc. | |
510 | * | |
511 | * strxfrm() works by taking the input string, say ABC, and creating an | |
512 | * output transformed string consisting of first the primary weights, | |
513 | * A¹B¹C¹ followed by the secondary ones, A²B²C²; and then the | |
514 | * tertiary, etc, yielding A¹B¹C¹ A²B²C² A³B³C³ .... Some characters | |
515 | * may not have weights at every level. In our example, let's say B | |
516 | * doesn't have a tertiary weight, and A doesn't have a secondary | |
517 | * weight. The constructed string is then going to be | |
518 | * A¹B¹C¹ B²C² A³C³ .... | |
519 | * This has the desired effect that strcmp() will look at the secondary | |
520 | * or tertiary weights only if the strings compare equal at all higher | |
521 | * priority weights. The spaces shown here, like in | |
522 | * "A¹B¹C¹ * A²B²C² " | |
523 | * are not just for readability. In the general case, these must | |
524 | * actually be bytes, which we will call here 'separator weights'; and | |
525 | * they must be smaller than any other weight value, but since these | |
526 | * are C strings, only the terminating one can be a NUL (some | |
527 | * implementations may include a non-NUL separator weight just before | |
528 | * the NUL). Implementations tend to reserve 01 for the separator | |
529 | * weights. They are needed so that a shorter string's secondary | |
530 | * weights won't be misconstrued as primary weights of a longer string, | |
531 | * etc. By making them smaller than any other weight, the shorter | |
532 | * string will sort first. (Actually, if all secondary weights are | |
533 | * smaller than all primary ones, there is no need for a separator | |
534 | * weight between those two levels, etc.) | |
535 | * | |
536 | * The length of the transformed string is roughly a linear function of | |
537 | * the input string. It's not exactly linear because some characters | |
538 | * don't have weights at all levels. When we call strxfrm() we have to | |
539 | * allocate some memory to hold the transformed string. The | |
540 | * calculations below try to find coefficients 'm' and 'b' for this | |
541 | * locale so that m*x + b equals how much space we need, given the size | |
542 | * of the input string in 'x'. If we calculate too small, we increase | |
543 | * the size as needed, and call strxfrm() again, but it is better to | |
544 | * get it right the first time to avoid wasted expensive string | |
545 | * transformations. */ | |
546 | ||
98994639 HS |
547 | { |
548 | /* 2: at most so many chars ('a', 'b'). */ | |
549 | /* 50: surely no system expands a char more. */ | |
550 | #define XFRMBUFSIZE (2 * 50) | |
551 | char xbuf[XFRMBUFSIZE]; | |
8772537c AL |
552 | const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); |
553 | const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); | |
554 | const SSize_t mult = fb - fa; | |
e0601d3c | 555 | if (mult < 1 && !(fa == 0 && fb == 0)) |
ad49ad39 NC |
556 | Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf, |
557 | (UV) fa, (UV) fb); | |
eb160463 | 558 | PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0; |
98994639 HS |
559 | PL_collxfrm_mult = mult; |
560 | } | |
561 | } | |
562 | ||
f2ce9e1c JH |
563 | #else |
564 | PERL_UNUSED_ARG(newcoll); | |
98994639 HS |
565 | #endif /* USE_LOCALE_COLLATE */ |
566 | } | |
567 | ||
b385bb4d KW |
568 | #ifdef WIN32 |
569 | ||
570 | char * | |
571 | Perl_my_setlocale(pTHX_ int category, const char* locale) | |
572 | { | |
573 | /* This, for Windows, emulates POSIX setlocale() behavior. There is no | |
574 | * difference unless the input locale is "", which means on Windows to get | |
575 | * the machine default, which is set via the computer's "Regional and | |
576 | * Language Options" (or its current equivalent). In POSIX, it instead | |
577 | * means to find the locale from the user's environment. This routine | |
578 | * looks in the environment, and, if anything is found, uses that instead | |
579 | * of going to the machine default. If there is no environment override, | |
580 | * the machine default is used, as normal, by calling the real setlocale() | |
581 | * with "". The POSIX behavior is to use the LC_ALL variable if set; | |
582 | * otherwise to use the particular category's variable if set; otherwise to | |
583 | * use the LANG variable. */ | |
584 | ||
175c4cf9 | 585 | bool override_LC_ALL = FALSE; |
89f7b9aa KW |
586 | char * result; |
587 | ||
b385bb4d KW |
588 | if (locale && strEQ(locale, "")) { |
589 | # ifdef LC_ALL | |
590 | locale = PerlEnv_getenv("LC_ALL"); | |
591 | if (! locale) { | |
592 | #endif | |
593 | switch (category) { | |
594 | # ifdef LC_ALL | |
595 | case LC_ALL: | |
481465ea | 596 | override_LC_ALL = TRUE; |
b385bb4d KW |
597 | break; /* We already know its variable isn't set */ |
598 | # endif | |
599 | # ifdef USE_LOCALE_TIME | |
600 | case LC_TIME: | |
601 | locale = PerlEnv_getenv("LC_TIME"); | |
602 | break; | |
603 | # endif | |
604 | # ifdef USE_LOCALE_CTYPE | |
605 | case LC_CTYPE: | |
606 | locale = PerlEnv_getenv("LC_CTYPE"); | |
607 | break; | |
608 | # endif | |
609 | # ifdef USE_LOCALE_COLLATE | |
610 | case LC_COLLATE: | |
611 | locale = PerlEnv_getenv("LC_COLLATE"); | |
612 | break; | |
613 | # endif | |
614 | # ifdef USE_LOCALE_MONETARY | |
615 | case LC_MONETARY: | |
616 | locale = PerlEnv_getenv("LC_MONETARY"); | |
617 | break; | |
618 | # endif | |
619 | # ifdef USE_LOCALE_NUMERIC | |
620 | case LC_NUMERIC: | |
621 | locale = PerlEnv_getenv("LC_NUMERIC"); | |
622 | break; | |
623 | # endif | |
624 | # ifdef USE_LOCALE_MESSAGES | |
625 | case LC_MESSAGES: | |
626 | locale = PerlEnv_getenv("LC_MESSAGES"); | |
627 | break; | |
628 | # endif | |
629 | default: | |
630 | /* This is a category, like PAPER_SIZE that we don't | |
631 | * know about; and so can't provide a wrapper. */ | |
632 | break; | |
633 | } | |
634 | if (! locale) { | |
635 | locale = PerlEnv_getenv("LANG"); | |
481465ea | 636 | if (! locale) { |
b385bb4d KW |
637 | locale = ""; |
638 | } | |
639 | } | |
640 | # ifdef LC_ALL | |
641 | } | |
642 | # endif | |
643 | } | |
644 | ||
89f7b9aa | 645 | result = setlocale(category, locale); |
bbc98134 KW |
646 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", __FILE__, __LINE__, |
647 | _setlocale_debug_string(category, locale, result))); | |
89f7b9aa | 648 | |
481465ea | 649 | if (! override_LC_ALL) { |
89f7b9aa KW |
650 | return result; |
651 | } | |
652 | ||
dfd77d7a | 653 | /* Here the input category was LC_ALL, and we have set it to what is in the |
481465ea KW |
654 | * LANG variable or the system default if there is no LANG. But these have |
655 | * lower priority than the other LC_foo variables, so override it for each | |
656 | * one that is set. (If they are set to "", it means to use the same thing | |
657 | * we just set LC_ALL to, so can skip) */ | |
89f7b9aa KW |
658 | # ifdef USE_LOCALE_TIME |
659 | result = PerlEnv_getenv("LC_TIME"); | |
730252b2 | 660 | if (result && strNE(result, "")) { |
89f7b9aa | 661 | setlocale(LC_TIME, result); |
bbc98134 KW |
662 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
663 | __FILE__, __LINE__, | |
664 | _setlocale_debug_string(LC_TIME, result, "not captured"))); | |
89f7b9aa KW |
665 | } |
666 | # endif | |
667 | # ifdef USE_LOCALE_CTYPE | |
668 | result = PerlEnv_getenv("LC_CTYPE"); | |
730252b2 | 669 | if (result && strNE(result, "")) { |
89f7b9aa | 670 | setlocale(LC_CTYPE, result); |
bbc98134 KW |
671 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
672 | __FILE__, __LINE__, | |
673 | _setlocale_debug_string(LC_CTYPE, result, "not captured"))); | |
89f7b9aa KW |
674 | } |
675 | # endif | |
676 | # ifdef USE_LOCALE_COLLATE | |
677 | result = PerlEnv_getenv("LC_COLLATE"); | |
730252b2 | 678 | if (result && strNE(result, "")) { |
89f7b9aa | 679 | setlocale(LC_COLLATE, result); |
bbc98134 KW |
680 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
681 | __FILE__, __LINE__, | |
682 | _setlocale_debug_string(LC_COLLATE, result, "not captured"))); | |
89f7b9aa KW |
683 | } |
684 | # endif | |
685 | # ifdef USE_LOCALE_MONETARY | |
686 | result = PerlEnv_getenv("LC_MONETARY"); | |
730252b2 | 687 | if (result && strNE(result, "")) { |
89f7b9aa | 688 | setlocale(LC_MONETARY, result); |
bbc98134 KW |
689 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
690 | __FILE__, __LINE__, | |
691 | _setlocale_debug_string(LC_MONETARY, result, "not captured"))); | |
89f7b9aa KW |
692 | } |
693 | # endif | |
694 | # ifdef USE_LOCALE_NUMERIC | |
695 | result = PerlEnv_getenv("LC_NUMERIC"); | |
730252b2 | 696 | if (result && strNE(result, "")) { |
89f7b9aa | 697 | setlocale(LC_NUMERIC, result); |
bbc98134 KW |
698 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
699 | __FILE__, __LINE__, | |
700 | _setlocale_debug_string(LC_NUMERIC, result, "not captured"))); | |
89f7b9aa KW |
701 | } |
702 | # endif | |
703 | # ifdef USE_LOCALE_MESSAGES | |
704 | result = PerlEnv_getenv("LC_MESSAGES"); | |
730252b2 | 705 | if (result && strNE(result, "")) { |
89f7b9aa | 706 | setlocale(LC_MESSAGES, result); |
bbc98134 KW |
707 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
708 | __FILE__, __LINE__, | |
709 | _setlocale_debug_string(LC_MESSAGES, result, "not captured"))); | |
89f7b9aa KW |
710 | } |
711 | # endif | |
712 | ||
bbc98134 KW |
713 | result = setlocale(LC_ALL, NULL); |
714 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", | |
715 | __FILE__, __LINE__, | |
716 | _setlocale_debug_string(LC_ALL, NULL, result))); | |
89f7b9aa | 717 | |
bbc98134 | 718 | return result; |
b385bb4d KW |
719 | } |
720 | ||
721 | #endif | |
722 | ||
723 | ||
98994639 HS |
724 | /* |
725 | * Initialize locale awareness. | |
726 | */ | |
727 | int | |
728 | Perl_init_i18nl10n(pTHX_ int printwarn) | |
729 | { | |
0e92a118 KW |
730 | /* printwarn is |
731 | * | |
732 | * 0 if not to output warning when setup locale is bad | |
733 | * 1 if to output warning based on value of PERL_BADLANG | |
734 | * >1 if to output regardless of PERL_BADLANG | |
735 | * | |
736 | * returns | |
98994639 | 737 | * 1 = set ok or not applicable, |
0e92a118 KW |
738 | * 0 = fallback to a locale of lower priority |
739 | * -1 = fallback to all locales failed, not even to the C locale | |
6b058d42 KW |
740 | * |
741 | * Under -DDEBUGGING, if the environment variable PERL_DEBUG_LOCALE_INIT is | |
742 | * set, debugging information is output. | |
743 | * | |
744 | * This looks more complicated than it is, mainly due to the #ifdefs. | |
745 | * | |
746 | * We try to set LC_ALL to the value determined by the environment. If | |
747 | * there is no LC_ALL on this platform, we try the individual categories we | |
748 | * know about. If this works, we are done. | |
749 | * | |
750 | * But if it doesn't work, we have to do something else. We search the | |
751 | * environment variables ourselves instead of relying on the system to do | |
752 | * it. We look at, in order, LC_ALL, LANG, a system default locale (if we | |
753 | * think there is one), and the ultimate fallback "C". This is all done in | |
754 | * the same loop as above to avoid duplicating code, but it makes things | |
755 | * more complex. After the original failure, we add the fallback | |
756 | * possibilities to the list of locales to try, and iterate the loop | |
757 | * through them all until one succeeds. | |
758 | * | |
759 | * On Ultrix, the locale MUST come from the environment, so there is | |
760 | * preliminary code to set it. I (khw) am not sure that it is necessary, | |
761 | * and that this couldn't be folded into the loop, but barring any real | |
762 | * platforms to test on, it's staying as-is | |
763 | * | |
764 | * A slight complication is that in embedded Perls, the locale may already | |
765 | * be set-up, and we don't want to get it from the normal environment | |
766 | * variables. This is handled by having a special environment variable | |
767 | * indicate we're in this situation. We simply set setlocale's 2nd | |
768 | * parameter to be a NULL instead of "". That indicates to setlocale that | |
769 | * it is not to change anything, but to return the current value, | |
770 | * effectively initializing perl's db to what the locale already is. | |
771 | * | |
772 | * We play the same trick with NULL if a LC_ALL succeeds. We call | |
773 | * setlocale() on the individual categores with NULL to get their existing | |
774 | * values for our db, instead of trying to change them. | |
775 | * */ | |
98994639 | 776 | |
0e92a118 KW |
777 | int ok = 1; |
778 | ||
98994639 | 779 | #if defined(USE_LOCALE) |
98994639 HS |
780 | #ifdef USE_LOCALE_CTYPE |
781 | char *curctype = NULL; | |
782 | #endif /* USE_LOCALE_CTYPE */ | |
783 | #ifdef USE_LOCALE_COLLATE | |
784 | char *curcoll = NULL; | |
785 | #endif /* USE_LOCALE_COLLATE */ | |
786 | #ifdef USE_LOCALE_NUMERIC | |
787 | char *curnum = NULL; | |
788 | #endif /* USE_LOCALE_NUMERIC */ | |
789 | #ifdef __GLIBC__ | |
175c4cf9 | 790 | const char * const language = savepv(PerlEnv_getenv("LANGUAGE")); |
98994639 | 791 | #endif |
65ebb059 | 792 | |
ccd65d51 KW |
793 | /* NULL uses the existing already set up locale */ |
794 | const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT")) | |
795 | ? NULL | |
796 | : ""; | |
5d1187d1 KW |
797 | #ifdef DEBUGGING |
798 | const bool debug = (PerlEnv_getenv("PERL_DEBUG_LOCALE_INIT")) | |
799 | ? TRUE | |
800 | : FALSE; | |
801 | # define DEBUG_LOCALE_INIT(category, locale, result) \ | |
802 | STMT_START { \ | |
803 | if (debug) { \ | |
804 | PerlIO_printf(Perl_debug_log, \ | |
805 | "%s:%d: %s\n", \ | |
806 | __FILE__, __LINE__, \ | |
807 | _setlocale_debug_string(category, \ | |
808 | locale, \ | |
809 | result)); \ | |
810 | } \ | |
811 | } STMT_END | |
812 | #else | |
813 | # define DEBUG_LOCALE_INIT(a,b,c) | |
814 | #endif | |
c3fcd832 KW |
815 | const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */ |
816 | unsigned int trial_locales_count; | |
175c4cf9 KW |
817 | const char * const lc_all = savepv(PerlEnv_getenv("LC_ALL")); |
818 | const char * const lang = savepv(PerlEnv_getenv("LANG")); | |
98994639 | 819 | bool setlocale_failure = FALSE; |
65ebb059 KW |
820 | unsigned int i; |
821 | char *p; | |
175c4cf9 KW |
822 | |
823 | /* A later getenv() could zap this, so only use here */ | |
824 | const char * const bad_lang_use_once = PerlEnv_getenv("PERL_BADLANG"); | |
825 | ||
826 | const bool locwarn = (printwarn > 1 | |
827 | || (printwarn | |
828 | && (! bad_lang_use_once | |
22ff3130 HS |
829 | || ( |
830 | /* disallow with "" or "0" */ | |
831 | *bad_lang_use_once | |
832 | && strNE("0", bad_lang_use_once))))); | |
0e92a118 | 833 | bool done = FALSE; |
5d1187d1 KW |
834 | char * sl_result; /* return from setlocale() */ |
835 | char * locale_param; | |
6bce99ee JH |
836 | #ifdef WIN32 |
837 | /* In some systems you can find out the system default locale | |
838 | * and use that as the fallback locale. */ | |
839 | # define SYSTEM_DEFAULT_LOCALE | |
840 | #endif | |
841 | #ifdef SYSTEM_DEFAULT_LOCALE | |
65ebb059 | 842 | const char *system_default_locale = NULL; |
6bce99ee | 843 | #endif |
98994639 | 844 | |
0e92a118 KW |
845 | #ifndef LOCALE_ENVIRON_REQUIRED |
846 | PERL_UNUSED_VAR(done); | |
5d1187d1 | 847 | PERL_UNUSED_VAR(locale_param); |
0e92a118 | 848 | #else |
98994639 HS |
849 | |
850 | /* | |
851 | * Ultrix setlocale(..., "") fails if there are no environment | |
852 | * variables from which to get a locale name. | |
853 | */ | |
854 | ||
b3e384bf | 855 | # ifdef LC_ALL |
98994639 | 856 | if (lang) { |
5d1187d1 KW |
857 | sl_result = my_setlocale(LC_ALL, setlocale_init); |
858 | DEBUG_LOCALE_INIT(LC_ALL, setlocale_init, sl_result); | |
859 | if (sl_result) | |
98994639 HS |
860 | done = TRUE; |
861 | else | |
862 | setlocale_failure = TRUE; | |
863 | } | |
5d1187d1 | 864 | if (! setlocale_failure) { |
b3e384bf | 865 | # ifdef USE_LOCALE_CTYPE |
5d1187d1 KW |
866 | locale_param = (! done && (lang || PerlEnv_getenv("LC_CTYPE"))) |
867 | ? setlocale_init | |
868 | : NULL; | |
869 | curctype = my_setlocale(LC_CTYPE, locale_param); | |
870 | DEBUG_LOCALE_INIT(LC_CTYPE, locale_param, sl_result); | |
871 | if (! curctype) | |
98994639 HS |
872 | setlocale_failure = TRUE; |
873 | else | |
874 | curctype = savepv(curctype); | |
b3e384bf KW |
875 | # endif /* USE_LOCALE_CTYPE */ |
876 | # ifdef USE_LOCALE_COLLATE | |
5d1187d1 KW |
877 | locale_param = (! done && (lang || PerlEnv_getenv("LC_COLLATE"))) |
878 | ? setlocale_init | |
879 | : NULL; | |
880 | curcoll = my_setlocale(LC_COLLATE, locale_param); | |
881 | DEBUG_LOCALE_INIT(LC_COLLATE, locale_param, sl_result); | |
882 | if (! curcoll) | |
98994639 HS |
883 | setlocale_failure = TRUE; |
884 | else | |
885 | curcoll = savepv(curcoll); | |
b3e384bf KW |
886 | # endif /* USE_LOCALE_COLLATE */ |
887 | # ifdef USE_LOCALE_NUMERIC | |
5d1187d1 KW |
888 | locale_param = (! done && (lang || PerlEnv_getenv("LC_NUMERIC"))) |
889 | ? setlocale_init | |
890 | : NULL; | |
891 | curnum = my_setlocale(LC_NUMERIC, locale_param); | |
892 | DEBUG_LOCALE_INIT(LC_NUMERIC, locale_param, sl_result); | |
893 | if (! curnum) | |
98994639 HS |
894 | setlocale_failure = TRUE; |
895 | else | |
896 | curnum = savepv(curnum); | |
b3e384bf | 897 | # endif /* USE_LOCALE_NUMERIC */ |
a782673d | 898 | # ifdef USE_LOCALE_MESSAGES |
5d1187d1 KW |
899 | locale_param = (! done && (lang || PerlEnv_getenv("LC_MESSAGES"))) |
900 | ? setlocale_init | |
901 | : NULL; | |
902 | sl_result = my_setlocale(LC_MESSAGES, locale_param); | |
903 | DEBUG_LOCALE_INIT(LC_MESSAGES, locale_param, sl_result); | |
904 | if (! sl_result) | |
a782673d KW |
905 | setlocale_failure = TRUE; |
906 | } | |
907 | # endif /* USE_LOCALE_MESSAGES */ | |
c835d6be | 908 | # ifdef USE_LOCALE_MONETARY |
5d1187d1 KW |
909 | locale_param = (! done && (lang || PerlEnv_getenv("LC_MONETARY"))) |
910 | ? setlocale_init | |
911 | : NULL; | |
912 | sl_result = my_setlocale(LC_MONETARY, locale_param); | |
913 | DEBUG_LOCALE_INIT(LC_MONETARY, locale_param, sl_result); | |
914 | if (! sl_result) { | |
c835d6be KW |
915 | setlocale_failure = TRUE; |
916 | } | |
917 | # endif /* USE_LOCALE_MONETARY */ | |
98994639 HS |
918 | } |
919 | ||
b3e384bf | 920 | # endif /* LC_ALL */ |
98994639 HS |
921 | |
922 | #endif /* !LOCALE_ENVIRON_REQUIRED */ | |
923 | ||
65ebb059 | 924 | /* We try each locale in the list until we get one that works, or exhaust |
20a240df KW |
925 | * the list. Normally the loop is executed just once. But if setting the |
926 | * locale fails, inside the loop we add fallback trials to the array and so | |
927 | * will execute the loop multiple times */ | |
c3fcd832 KW |
928 | trial_locales[0] = setlocale_init; |
929 | trial_locales_count = 1; | |
65ebb059 KW |
930 | for (i= 0; i < trial_locales_count; i++) { |
931 | const char * trial_locale = trial_locales[i]; | |
932 | ||
933 | if (i > 0) { | |
934 | ||
935 | /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED | |
936 | * when i==0, but I (khw) don't think that behavior makes much | |
937 | * sense */ | |
938 | setlocale_failure = FALSE; | |
939 | ||
6bce99ee JH |
940 | #ifdef SYSTEM_DEFAULT_LOCALE |
941 | # ifdef WIN32 | |
65ebb059 KW |
942 | /* On Windows machines, an entry of "" after the 0th means to use |
943 | * the system default locale, which we now proceed to get. */ | |
944 | if (strEQ(trial_locale, "")) { | |
945 | unsigned int j; | |
946 | ||
947 | /* Note that this may change the locale, but we are going to do | |
948 | * that anyway just below */ | |
949 | system_default_locale = setlocale(LC_ALL, ""); | |
5d1187d1 | 950 | DEBUG_LOCALE_INIT(LC_ALL, "", system_default_locale); |
65ebb059 KW |
951 | |
952 | /* Skip if invalid or it's already on the list of locales to | |
953 | * try */ | |
954 | if (! system_default_locale) { | |
955 | goto next_iteration; | |
956 | } | |
957 | for (j = 0; j < trial_locales_count; j++) { | |
958 | if (strEQ(system_default_locale, trial_locales[j])) { | |
959 | goto next_iteration; | |
960 | } | |
961 | } | |
962 | ||
963 | trial_locale = system_default_locale; | |
964 | } | |
6bce99ee JH |
965 | # endif /* WIN32 */ |
966 | #endif /* SYSTEM_DEFAULT_LOCALE */ | |
65ebb059 KW |
967 | } |
968 | ||
98994639 | 969 | #ifdef LC_ALL |
5d1187d1 KW |
970 | sl_result = my_setlocale(LC_ALL, trial_locale); |
971 | DEBUG_LOCALE_INIT(LC_ALL, trial_locale, sl_result); | |
972 | if (! sl_result) { | |
49c85077 | 973 | setlocale_failure = TRUE; |
7cd8b568 KW |
974 | } |
975 | else { | |
976 | /* Since LC_ALL succeeded, it should have changed all the other | |
977 | * categories it can to its value; so we massage things so that the | |
978 | * setlocales below just return their category's current values. | |
979 | * This adequately handles the case in NetBSD where LC_COLLATE may | |
980 | * not be defined for a locale, and setting it individually will | |
981 | * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to | |
982 | * the POSIX locale. */ | |
983 | trial_locale = NULL; | |
984 | } | |
98994639 HS |
985 | #endif /* LC_ALL */ |
986 | ||
49c85077 | 987 | if (!setlocale_failure) { |
98994639 | 988 | #ifdef USE_LOCALE_CTYPE |
49c85077 | 989 | Safefree(curctype); |
5d1187d1 KW |
990 | curctype = my_setlocale(LC_CTYPE, trial_locale); |
991 | DEBUG_LOCALE_INIT(LC_CTYPE, trial_locale, curctype); | |
992 | if (! curctype) | |
49c85077 KW |
993 | setlocale_failure = TRUE; |
994 | else | |
995 | curctype = savepv(curctype); | |
98994639 HS |
996 | #endif /* USE_LOCALE_CTYPE */ |
997 | #ifdef USE_LOCALE_COLLATE | |
49c85077 | 998 | Safefree(curcoll); |
5d1187d1 KW |
999 | curcoll = my_setlocale(LC_COLLATE, trial_locale); |
1000 | DEBUG_LOCALE_INIT(LC_COLLATE, trial_locale, curcoll); | |
1001 | if (! curcoll) | |
49c85077 KW |
1002 | setlocale_failure = TRUE; |
1003 | else | |
1004 | curcoll = savepv(curcoll); | |
98994639 HS |
1005 | #endif /* USE_LOCALE_COLLATE */ |
1006 | #ifdef USE_LOCALE_NUMERIC | |
49c85077 | 1007 | Safefree(curnum); |
5d1187d1 KW |
1008 | curnum = my_setlocale(LC_NUMERIC, trial_locale); |
1009 | DEBUG_LOCALE_INIT(LC_NUMERIC, trial_locale, curnum); | |
1010 | if (! curnum) | |
49c85077 KW |
1011 | setlocale_failure = TRUE; |
1012 | else | |
1013 | curnum = savepv(curnum); | |
98994639 | 1014 | #endif /* USE_LOCALE_NUMERIC */ |
a782673d | 1015 | #ifdef USE_LOCALE_MESSAGES |
5d1187d1 KW |
1016 | sl_result = my_setlocale(LC_MESSAGES, trial_locale); |
1017 | DEBUG_LOCALE_INIT(LC_MESSAGES, trial_locale, sl_result); | |
1018 | if (! (sl_result)) | |
a782673d KW |
1019 | setlocale_failure = TRUE; |
1020 | #endif /* USE_LOCALE_MESSAGES */ | |
c835d6be | 1021 | #ifdef USE_LOCALE_MONETARY |
5d1187d1 KW |
1022 | sl_result = my_setlocale(LC_MONETARY, trial_locale); |
1023 | DEBUG_LOCALE_INIT(LC_MONETARY, trial_locale, sl_result); | |
1024 | if (! (sl_result)) | |
c835d6be KW |
1025 | setlocale_failure = TRUE; |
1026 | #endif /* USE_LOCALE_MONETARY */ | |
1027 | ||
49c85077 KW |
1028 | if (! setlocale_failure) { /* Success */ |
1029 | break; | |
1030 | } | |
65ebb059 | 1031 | } |
98994639 | 1032 | |
49c85077 KW |
1033 | /* Here, something failed; will need to try a fallback. */ |
1034 | ok = 0; | |
65ebb059 | 1035 | |
49c85077 KW |
1036 | if (i == 0) { |
1037 | unsigned int j; | |
98994639 | 1038 | |
65ebb059 | 1039 | if (locwarn) { /* Output failure info only on the first one */ |
98994639 HS |
1040 | #ifdef LC_ALL |
1041 | ||
49c85077 KW |
1042 | PerlIO_printf(Perl_error_log, |
1043 | "perl: warning: Setting locale failed.\n"); | |
98994639 HS |
1044 | |
1045 | #else /* !LC_ALL */ | |
1046 | ||
49c85077 KW |
1047 | PerlIO_printf(Perl_error_log, |
1048 | "perl: warning: Setting locale failed for the categories:\n\t"); | |
20a240df | 1049 | # ifdef USE_LOCALE_CTYPE |
49c85077 KW |
1050 | if (! curctype) |
1051 | PerlIO_printf(Perl_error_log, "LC_CTYPE "); | |
20a240df KW |
1052 | # endif /* USE_LOCALE_CTYPE */ |
1053 | # ifdef USE_LOCALE_COLLATE | |
49c85077 KW |
1054 | if (! curcoll) |
1055 | PerlIO_printf(Perl_error_log, "LC_COLLATE "); | |
20a240df KW |
1056 | # endif /* USE_LOCALE_COLLATE */ |
1057 | # ifdef USE_LOCALE_NUMERIC | |
49c85077 KW |
1058 | if (! curnum) |
1059 | PerlIO_printf(Perl_error_log, "LC_NUMERIC "); | |
20a240df | 1060 | # endif /* USE_LOCALE_NUMERIC */ |
a782673d | 1061 | PerlIO_printf(Perl_error_log, "and possibly others\n"); |
98994639 HS |
1062 | |
1063 | #endif /* LC_ALL */ | |
1064 | ||
49c85077 KW |
1065 | PerlIO_printf(Perl_error_log, |
1066 | "perl: warning: Please check that your locale settings:\n"); | |
98994639 HS |
1067 | |
1068 | #ifdef __GLIBC__ | |
49c85077 KW |
1069 | PerlIO_printf(Perl_error_log, |
1070 | "\tLANGUAGE = %c%s%c,\n", | |
1071 | language ? '"' : '(', | |
1072 | language ? language : "unset", | |
1073 | language ? '"' : ')'); | |
98994639 HS |
1074 | #endif |
1075 | ||
49c85077 KW |
1076 | PerlIO_printf(Perl_error_log, |
1077 | "\tLC_ALL = %c%s%c,\n", | |
1078 | lc_all ? '"' : '(', | |
1079 | lc_all ? lc_all : "unset", | |
1080 | lc_all ? '"' : ')'); | |
98994639 HS |
1081 | |
1082 | #if defined(USE_ENVIRON_ARRAY) | |
49c85077 KW |
1083 | { |
1084 | char **e; | |
1085 | for (e = environ; *e; e++) { | |
1086 | if (strnEQ(*e, "LC_", 3) | |
1087 | && strnNE(*e, "LC_ALL=", 7) | |
1088 | && (p = strchr(*e, '='))) | |
1089 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", | |
1090 | (int)(p - *e), *e, p + 1); | |
1091 | } | |
1092 | } | |
98994639 | 1093 | #else |
49c85077 KW |
1094 | PerlIO_printf(Perl_error_log, |
1095 | "\t(possibly more locale environment variables)\n"); | |
98994639 HS |
1096 | #endif |
1097 | ||
49c85077 KW |
1098 | PerlIO_printf(Perl_error_log, |
1099 | "\tLANG = %c%s%c\n", | |
1100 | lang ? '"' : '(', | |
1101 | lang ? lang : "unset", | |
1102 | lang ? '"' : ')'); | |
98994639 | 1103 | |
49c85077 KW |
1104 | PerlIO_printf(Perl_error_log, |
1105 | " are supported and installed on your system.\n"); | |
1106 | } | |
98994639 | 1107 | |
65ebb059 | 1108 | /* Calculate what fallback locales to try. We have avoided this |
f6bab5f6 | 1109 | * until we have to, because failure is quite unlikely. This will |
65ebb059 KW |
1110 | * usually change the upper bound of the loop we are in. |
1111 | * | |
1112 | * Since the system's default way of setting the locale has not | |
1113 | * found one that works, We use Perl's defined ordering: LC_ALL, | |
1114 | * LANG, and the C locale. We don't try the same locale twice, so | |
1115 | * don't add to the list if already there. (On POSIX systems, the | |
1116 | * LC_ALL element will likely be a repeat of the 0th element "", | |
6b058d42 KW |
1117 | * but there's no harm done by doing it explicitly. |
1118 | * | |
1119 | * Note that this tries the LC_ALL environment variable even on | |
1120 | * systems which have no LC_ALL locale setting. This may or may | |
1121 | * not have been originally intentional, but there's no real need | |
1122 | * to change the behavior. */ | |
65ebb059 KW |
1123 | if (lc_all) { |
1124 | for (j = 0; j < trial_locales_count; j++) { | |
1125 | if (strEQ(lc_all, trial_locales[j])) { | |
1126 | goto done_lc_all; | |
1127 | } | |
1128 | } | |
1129 | trial_locales[trial_locales_count++] = lc_all; | |
1130 | } | |
1131 | done_lc_all: | |
98994639 | 1132 | |
65ebb059 KW |
1133 | if (lang) { |
1134 | for (j = 0; j < trial_locales_count; j++) { | |
1135 | if (strEQ(lang, trial_locales[j])) { | |
1136 | goto done_lang; | |
1137 | } | |
1138 | } | |
1139 | trial_locales[trial_locales_count++] = lang; | |
1140 | } | |
1141 | done_lang: | |
1142 | ||
1143 | #if defined(WIN32) && defined(LC_ALL) | |
1144 | /* For Windows, we also try the system default locale before "C". | |
1145 | * (If there exists a Windows without LC_ALL we skip this because | |
1146 | * it gets too complicated. For those, the "C" is the next | |
1147 | * fallback possibility). The "" is the same as the 0th element of | |
1148 | * the array, but the code at the loop above knows to treat it | |
1149 | * differently when not the 0th */ | |
1150 | trial_locales[trial_locales_count++] = ""; | |
1151 | #endif | |
1152 | ||
1153 | for (j = 0; j < trial_locales_count; j++) { | |
1154 | if (strEQ("C", trial_locales[j])) { | |
1155 | goto done_C; | |
1156 | } | |
1157 | } | |
1158 | trial_locales[trial_locales_count++] = "C"; | |
98994639 | 1159 | |
65ebb059 KW |
1160 | done_C: ; |
1161 | } /* end of first time through the loop */ | |
98994639 | 1162 | |
65ebb059 KW |
1163 | #ifdef WIN32 |
1164 | next_iteration: ; | |
1165 | #endif | |
1166 | ||
1167 | } /* end of looping through the trial locales */ | |
1168 | ||
1169 | if (ok < 1) { /* If we tried to fallback */ | |
1170 | const char* msg; | |
1171 | if (! setlocale_failure) { /* fallback succeeded */ | |
1172 | msg = "Falling back to"; | |
1173 | } | |
1174 | else { /* fallback failed */ | |
98994639 | 1175 | |
65ebb059 KW |
1176 | /* We dropped off the end of the loop, so have to decrement i to |
1177 | * get back to the value the last time through */ | |
1178 | i--; | |
98994639 | 1179 | |
65ebb059 KW |
1180 | ok = -1; |
1181 | msg = "Failed to fall back to"; | |
1182 | ||
1183 | /* To continue, we should use whatever values we've got */ | |
98994639 | 1184 | #ifdef USE_LOCALE_CTYPE |
49c85077 KW |
1185 | Safefree(curctype); |
1186 | curctype = savepv(setlocale(LC_CTYPE, NULL)); | |
5d1187d1 | 1187 | DEBUG_LOCALE_INIT(LC_CTYPE, NULL, curctype); |
98994639 HS |
1188 | #endif /* USE_LOCALE_CTYPE */ |
1189 | #ifdef USE_LOCALE_COLLATE | |
49c85077 KW |
1190 | Safefree(curcoll); |
1191 | curcoll = savepv(setlocale(LC_COLLATE, NULL)); | |
5d1187d1 | 1192 | DEBUG_LOCALE_INIT(LC_COLLATE, NULL, curcoll); |
98994639 HS |
1193 | #endif /* USE_LOCALE_COLLATE */ |
1194 | #ifdef USE_LOCALE_NUMERIC | |
49c85077 KW |
1195 | Safefree(curnum); |
1196 | curnum = savepv(setlocale(LC_NUMERIC, NULL)); | |
5d1187d1 | 1197 | DEBUG_LOCALE_INIT(LC_NUMERIC, NULL, curnum); |
98994639 | 1198 | #endif /* USE_LOCALE_NUMERIC */ |
65ebb059 KW |
1199 | } |
1200 | ||
1201 | if (locwarn) { | |
1202 | const char * description; | |
1203 | const char * name = ""; | |
1204 | if (strEQ(trial_locales[i], "C")) { | |
1205 | description = "the standard locale"; | |
1206 | name = "C"; | |
1207 | } | |
6bce99ee | 1208 | #ifdef SYSTEM_DEFAULT_LOCALE |
65ebb059 KW |
1209 | else if (strEQ(trial_locales[i], "")) { |
1210 | description = "the system default locale"; | |
1211 | if (system_default_locale) { | |
1212 | name = system_default_locale; | |
1213 | } | |
1214 | } | |
6bce99ee | 1215 | #endif /* SYSTEM_DEFAULT_LOCALE */ |
65ebb059 KW |
1216 | else { |
1217 | description = "a fallback locale"; | |
1218 | name = trial_locales[i]; | |
1219 | } | |
1220 | if (name && strNE(name, "")) { | |
1221 | PerlIO_printf(Perl_error_log, | |
1222 | "perl: warning: %s %s (\"%s\").\n", msg, description, name); | |
1223 | } | |
1224 | else { | |
1225 | PerlIO_printf(Perl_error_log, | |
1226 | "perl: warning: %s %s.\n", msg, description); | |
1227 | } | |
1228 | } | |
1229 | } /* End of tried to fallback */ | |
98994639 HS |
1230 | |
1231 | #ifdef USE_LOCALE_CTYPE | |
1232 | new_ctype(curctype); | |
1233 | #endif /* USE_LOCALE_CTYPE */ | |
1234 | ||
1235 | #ifdef USE_LOCALE_COLLATE | |
1236 | new_collate(curcoll); | |
1237 | #endif /* USE_LOCALE_COLLATE */ | |
1238 | ||
1239 | #ifdef USE_LOCALE_NUMERIC | |
1240 | new_numeric(curnum); | |
1241 | #endif /* USE_LOCALE_NUMERIC */ | |
b310b053 | 1242 | |
8ef6e574 | 1243 | #if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE) |
49c85077 KW |
1244 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE |
1245 | * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by | |
1246 | * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the | |
1247 | * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open | |
1248 | * discipline. */ | |
c1284011 | 1249 | PL_utf8locale = _is_cur_LC_category_utf8(LC_CTYPE); |
49c85077 | 1250 | |
a05d7ebb | 1251 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
fde18df1 JH |
1252 | This is an alternative to using the -C command line switch |
1253 | (the -C if present will override this). */ | |
1254 | { | |
dd374669 | 1255 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
a05d7ebb | 1256 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
5a22a2bb NC |
1257 | if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG) |
1258 | PL_utf8cache = -1; | |
b310b053 | 1259 | } |
ec71e770 | 1260 | #endif |
b310b053 | 1261 | |
98994639 | 1262 | #ifdef USE_LOCALE_CTYPE |
43c5f42d | 1263 | Safefree(curctype); |
98994639 HS |
1264 | #endif /* USE_LOCALE_CTYPE */ |
1265 | #ifdef USE_LOCALE_COLLATE | |
43c5f42d | 1266 | Safefree(curcoll); |
98994639 HS |
1267 | #endif /* USE_LOCALE_COLLATE */ |
1268 | #ifdef USE_LOCALE_NUMERIC | |
43c5f42d | 1269 | Safefree(curnum); |
98994639 | 1270 | #endif /* USE_LOCALE_NUMERIC */ |
8ef6e574 | 1271 | |
175c4cf9 KW |
1272 | #ifdef __GLIBC__ |
1273 | Safefree(language); | |
1274 | #endif | |
1275 | ||
1276 | Safefree(lc_all); | |
1277 | Safefree(lang); | |
1278 | ||
e3305790 KW |
1279 | #else /* !USE_LOCALE */ |
1280 | PERL_UNUSED_ARG(printwarn); | |
1281 | #endif /* USE_LOCALE */ | |
1282 | ||
98994639 HS |
1283 | return ok; |
1284 | } | |
1285 | ||
49c85077 | 1286 | |
98994639 HS |
1287 | #ifdef USE_LOCALE_COLLATE |
1288 | ||
1289 | /* | |
1290 | * mem_collxfrm() is a bit like strxfrm() but with two important | |
1291 | * differences. First, it handles embedded NULs. Second, it allocates | |
1292 | * a bit more memory than needed for the transformed data itself. | |
1293 | * The real transformed data begins at offset sizeof(collationix). | |
d35fca5f KW |
1294 | * *xlen is set to the length of that, and doesn't include the collation index |
1295 | * size. | |
98994639 HS |
1296 | * Please see sv_collxfrm() to see how this is used. |
1297 | */ | |
166f8a29 | 1298 | |
98994639 | 1299 | char * |
6696cfa7 KW |
1300 | Perl_mem_collxfrm(pTHX_ const char *input_string, |
1301 | STRLEN len, | |
1302 | STRLEN *xlen | |
1303 | ) | |
98994639 | 1304 | { |
6696cfa7 KW |
1305 | char * s = (char *) input_string; |
1306 | STRLEN s_strlen = strlen(input_string); | |
98994639 | 1307 | char *xbuf; |
6696cfa7 | 1308 | STRLEN xAlloc, xout; /* xalloc is a reserved word in VC */ |
c664130f | 1309 | bool first_time = TRUE; /* Cleared after first loop iteration */ |
98994639 | 1310 | |
7918f24d NC |
1311 | PERL_ARGS_ASSERT_MEM_COLLXFRM; |
1312 | ||
6696cfa7 KW |
1313 | /* Replace any embedded NULs with the control that sorts before any others. |
1314 | * This will give as good as possible results on strings that don't | |
1315 | * otherwise contain that character, but otherwise there may be | |
1316 | * less-than-perfect results with that character and NUL. This is | |
1317 | * unavoidable unless we replace strxfrm with our own implementation. | |
1318 | * | |
1319 | * XXX This code may be overkill. khw wrote it before realizing that if | |
1320 | * you change a NUL into some other character, that that may change the | |
1321 | * strxfrm results if that character is part of a sequence with other | |
1322 | * characters for weight calculations. To minimize the chances of this, | |
1323 | * now the replacement is restricted to another control (likely to be | |
1324 | * \001). But the full generality has been retained. | |
1325 | * | |
1326 | * This is one of the few places in the perl core, where we can use | |
1327 | * standard functions like strlen() and strcat(). It's because we're | |
1328 | * looking for NULs. */ | |
1329 | if (s_strlen < len) { | |
1330 | char * e = s + len; | |
1331 | char * sans_nuls; | |
1332 | STRLEN cur_min_char_len; | |
1333 | ||
1334 | /* If we don't know what control character sorts lowest for this | |
1335 | * locale, find it */ | |
1336 | if (*PL_strxfrm_min_char == '\0') { | |
1337 | int j; | |
1338 | char * cur_min_x = NULL; /* Cur cp's xfrm, (except it also | |
1339 | includes the collation index | |
1340 | prefixed. */ | |
1341 | ||
1342 | /* Look through all legal code points (NUL isn't) */ | |
1343 | for (j = 1; j < 256; j++) { | |
1344 | char * x; /* j's xfrm plus collation index */ | |
1345 | STRLEN x_len; /* length of 'x' */ | |
1346 | STRLEN trial_len = 1; | |
1347 | ||
1348 | /* Create a 1 byte string of the current code point, but with | |
1349 | * room to be 2 bytes */ | |
1350 | char cur_source[] = { (char) j, '\0' , '\0' }; | |
1351 | ||
1352 | if (PL_in_utf8_COLLATE_locale) { | |
1353 | if (! isCNTRL_L1(j)) { | |
1354 | continue; | |
1355 | } | |
1356 | ||
1357 | /* If needs to be 2 bytes, find them */ | |
1358 | if (! UVCHR_IS_INVARIANT(j)) { | |
1359 | continue; /* Can't handle variants yet */ | |
1360 | } | |
1361 | } | |
1362 | else if (! isCNTRL_LC(j)) { | |
1363 | continue; | |
1364 | } | |
1365 | ||
1366 | /* Then transform it */ | |
1367 | x = mem_collxfrm(cur_source, trial_len, &x_len); | |
1368 | ||
1369 | /* If something went wrong (which it shouldn't), just | |
1370 | * ignore this code point */ | |
1371 | if ( x_len == 0 | |
1372 | || strlen(x + sizeof(PL_collation_ix)) < x_len) | |
1373 | { | |
1374 | continue; | |
1375 | } | |
1376 | ||
1377 | /* If this character's transformation is lower than | |
1378 | * the current lowest, this one becomes the lowest */ | |
1379 | if ( cur_min_x == NULL | |
1380 | || strLT(x + sizeof(PL_collation_ix), | |
1381 | cur_min_x + sizeof(PL_collation_ix))) | |
1382 | { | |
1383 | strcpy(PL_strxfrm_min_char, cur_source); | |
1384 | cur_min_x = x; | |
1385 | } | |
1386 | else { | |
1387 | Safefree(x); | |
1388 | } | |
1389 | } /* end of loop through all bytes */ | |
1390 | ||
1391 | /* Unlikely, but possible, if there aren't any controls in the | |
1392 | * locale, arbitrarily use \001 */ | |
1393 | if (cur_min_x == NULL) { | |
1394 | STRLEN x_len; /* temporary */ | |
1395 | cur_min_x = mem_collxfrm("\001", 1, &x_len); | |
1396 | /* cur_min_cp was already initialized to 1 */ | |
1397 | } | |
1398 | ||
1399 | Safefree(cur_min_x); | |
1400 | } | |
1401 | ||
1402 | /* The worst case length for the replaced string would be if every | |
1403 | * character in it is NUL. Multiply that by the length of each | |
1404 | * replacement, and allow for a trailing NUL */ | |
1405 | cur_min_char_len = strlen(PL_strxfrm_min_char); | |
1406 | Newx(sans_nuls, (len * cur_min_char_len) + 1, char); | |
1407 | *sans_nuls = '\0'; | |
1408 | ||
1409 | ||
1410 | /* Replace each NUL with the lowest collating control. Loop until have | |
1411 | * exhausted all the NULs */ | |
1412 | while (s + s_strlen < e) { | |
1413 | strcat(sans_nuls, s); | |
1414 | ||
1415 | /* Do the actual replacement */ | |
1416 | strcat(sans_nuls, PL_strxfrm_min_char); | |
1417 | ||
1418 | /* Move past the input NUL */ | |
1419 | s += s_strlen + 1; | |
1420 | s_strlen = strlen(s); | |
1421 | } | |
1422 | ||
1423 | /* And add anything that trails the final NUL */ | |
1424 | strcat(sans_nuls, s); | |
1425 | ||
1426 | /* Switch so below we transform this modified string */ | |
1427 | s = sans_nuls; | |
1428 | len = strlen(s); | |
1429 | } | |
1430 | ||
59c018b9 KW |
1431 | /* The first element in the output is the collation id, used by |
1432 | * sv_collxfrm(); then comes the space for the transformed string. The | |
1433 | * equation should give us a good estimate as to how much is needed */ | |
98994639 | 1434 | xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1; |
a02a5408 | 1435 | Newx(xbuf, xAlloc, char); |
bb0f664e | 1436 | if (UNLIKELY(! xbuf)) |
98994639 HS |
1437 | goto bad; |
1438 | ||
d35fca5f | 1439 | /* Store the collation id */ |
98994639 HS |
1440 | *(U32*)xbuf = PL_collation_ix; |
1441 | xout = sizeof(PL_collation_ix); | |
d35fca5f KW |
1442 | |
1443 | /* Then the transformation of the input. We loop until successful, or we | |
1444 | * give up */ | |
4ebeff16 KW |
1445 | for (;;) { |
1446 | STRLEN xused = strxfrm(xbuf + xout, s, xAlloc - xout); | |
1447 | ||
1448 | /* If the transformed string occupies less space than we told strxfrm() | |
1449 | * was available, it means it successfully transformed the whole | |
1450 | * string. */ | |
1451 | if (xused < xAlloc - xout) { | |
1452 | xout += xused; | |
1453 | break; | |
1454 | } | |
bb0f664e | 1455 | |
4ebeff16 KW |
1456 | if (UNLIKELY(xused >= PERL_INT_MAX)) |
1457 | goto bad; | |
d35fca5f | 1458 | |
c664130f KW |
1459 | /* A well-behaved strxfrm() returns exactly how much space it needs |
1460 | * (not including the trailing NUL) when it fails due to not enough | |
1461 | * space being provided. Assume that this is the case unless it's been | |
1462 | * proven otherwise */ | |
1463 | if (LIKELY(PL_strxfrm_is_behaved) && first_time) { | |
1464 | xAlloc = xused + sizeof(PL_collation_ix) + 1; | |
1465 | } | |
1466 | else { /* Here, either: | |
1467 | * 1) The strxfrm() has previously shown bad behavior; or | |
1468 | * 2) It isn't the first time through the loop, which means | |
1469 | * that the strxfrm() is now showing bad behavior, because | |
1470 | * we gave it what it said was needed in the previous | |
1471 | * iteration, and it came back saying it needed still more. | |
1472 | * (Many versions of cygwin fit this. When the buffer size | |
1473 | * isn't sufficient, they return the input size instead of | |
1474 | * how much is needed.) | |
1475 | * Increase the buffer size by a fixed percentage and try again. */ | |
1476 | xAlloc = (2 * xAlloc) + 1; | |
1477 | PL_strxfrm_is_behaved = FALSE; | |
1478 | } | |
1479 | ||
1480 | ||
4ebeff16 KW |
1481 | Renew(xbuf, xAlloc, char); |
1482 | if (UNLIKELY(! xbuf)) | |
1483 | goto bad; | |
c664130f KW |
1484 | |
1485 | first_time = FALSE; | |
4ebeff16 | 1486 | } |
98994639 | 1487 | |
6696cfa7 KW |
1488 | *xlen = xout - sizeof(PL_collation_ix); |
1489 | ||
3c5f993e KW |
1490 | /* Free up unneeded space; retain ehough for trailing NUL */ |
1491 | Renew(xbuf, xout + 1, char); | |
98994639 | 1492 | |
6696cfa7 KW |
1493 | if (s != input_string) { |
1494 | Safefree(s); | |
98994639 HS |
1495 | } |
1496 | ||
98994639 HS |
1497 | return xbuf; |
1498 | ||
1499 | bad: | |
1500 | Safefree(xbuf); | |
6696cfa7 KW |
1501 | if (s != input_string) { |
1502 | Safefree(s); | |
1503 | } | |
98994639 HS |
1504 | *xlen = 0; |
1505 | return NULL; | |
1506 | } | |
1507 | ||
1508 | #endif /* USE_LOCALE_COLLATE */ | |
1509 | ||
8ef6e574 KW |
1510 | #ifdef USE_LOCALE |
1511 | ||
c1284011 KW |
1512 | bool |
1513 | Perl__is_cur_LC_category_utf8(pTHX_ int category) | |
7d74bb61 KW |
1514 | { |
1515 | /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE | |
1516 | * otherwise. 'category' may not be LC_ALL. If the platform doesn't have | |
119ee68b | 1517 | * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence |
609548d2 KW |
1518 | * could give the wrong result. The result will very likely be correct for |
1519 | * languages that have commonly used non-ASCII characters, but for notably | |
1520 | * English, it comes down to if the locale's name ends in something like | |
1521 | * "UTF-8". It errs on the side of not being a UTF-8 locale. */ | |
7d74bb61 KW |
1522 | |
1523 | char *save_input_locale = NULL; | |
7d74bb61 KW |
1524 | STRLEN final_pos; |
1525 | ||
8ef6e574 | 1526 | #ifdef LC_ALL |
7d74bb61 | 1527 | assert(category != LC_ALL); |
8ef6e574 | 1528 | #endif |
7d74bb61 KW |
1529 | |
1530 | /* First dispose of the trivial cases */ | |
b07fffd1 | 1531 | save_input_locale = setlocale(category, NULL); |
7d74bb61 | 1532 | if (! save_input_locale) { |
69014004 KW |
1533 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1534 | "Could not find current locale for category %d\n", | |
1535 | category)); | |
7d74bb61 KW |
1536 | return FALSE; /* XXX maybe should croak */ |
1537 | } | |
b07fffd1 | 1538 | save_input_locale = stdize_locale(savepv(save_input_locale)); |
a39edc4c | 1539 | if (isNAME_C_OR_POSIX(save_input_locale)) { |
69014004 KW |
1540 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1541 | "Current locale for category %d is %s\n", | |
1542 | category, save_input_locale)); | |
b07fffd1 | 1543 | Safefree(save_input_locale); |
7d74bb61 KW |
1544 | return FALSE; |
1545 | } | |
1546 | ||
1d958db2 KW |
1547 | #if defined(USE_LOCALE_CTYPE) \ |
1548 | && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET))) | |
7d74bb61 | 1549 | |
1d958db2 | 1550 | { /* Next try nl_langinfo or MB_CUR_MAX if available */ |
7d74bb61 KW |
1551 | |
1552 | char *save_ctype_locale = NULL; | |
119ee68b | 1553 | bool is_utf8; |
7d74bb61 | 1554 | |
119ee68b | 1555 | if (category != LC_CTYPE) { /* These work only on LC_CTYPE */ |
7d74bb61 KW |
1556 | |
1557 | /* Get the current LC_CTYPE locale */ | |
4f72bb37 | 1558 | save_ctype_locale = setlocale(LC_CTYPE, NULL); |
7d74bb61 | 1559 | if (! save_ctype_locale) { |
69014004 KW |
1560 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1561 | "Could not find current locale for LC_CTYPE\n")); | |
7d74bb61 KW |
1562 | goto cant_use_nllanginfo; |
1563 | } | |
4f72bb37 | 1564 | save_ctype_locale = stdize_locale(savepv(save_ctype_locale)); |
7d74bb61 KW |
1565 | |
1566 | /* If LC_CTYPE and the desired category use the same locale, this | |
1567 | * means that finding the value for LC_CTYPE is the same as finding | |
1568 | * the value for the desired category. Otherwise, switch LC_CTYPE | |
1569 | * to the desired category's locale */ | |
1570 | if (strEQ(save_ctype_locale, save_input_locale)) { | |
1571 | Safefree(save_ctype_locale); | |
1572 | save_ctype_locale = NULL; | |
1573 | } | |
1574 | else if (! setlocale(LC_CTYPE, save_input_locale)) { | |
69014004 KW |
1575 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1576 | "Could not change LC_CTYPE locale to %s\n", | |
1577 | save_input_locale)); | |
7d74bb61 KW |
1578 | Safefree(save_ctype_locale); |
1579 | goto cant_use_nllanginfo; | |
1580 | } | |
1581 | } | |
1582 | ||
69014004 KW |
1583 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n", |
1584 | save_input_locale)); | |
1585 | ||
7d74bb61 | 1586 | /* Here the current LC_CTYPE is set to the locale of the category whose |
1d958db2 KW |
1587 | * information is desired. This means that nl_langinfo() and MB_CUR_MAX |
1588 | * should give the correct results */ | |
119ee68b | 1589 | |
1d958db2 KW |
1590 | # if defined(HAS_NL_LANGINFO) && defined(CODESET) |
1591 | { | |
4f72bb37 | 1592 | char *codeset = nl_langinfo(CODESET); |
1d958db2 | 1593 | if (codeset && strNE(codeset, "")) { |
4f72bb37 | 1594 | codeset = savepv(codeset); |
119ee68b | 1595 | |
1d958db2 KW |
1596 | /* If we switched LC_CTYPE, switch back */ |
1597 | if (save_ctype_locale) { | |
1598 | setlocale(LC_CTYPE, save_ctype_locale); | |
1599 | Safefree(save_ctype_locale); | |
1600 | } | |
1601 | ||
1602 | is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8")) | |
1603 | || foldEQ(codeset, STR_WITH_LEN("UTF8")); | |
1604 | ||
69014004 KW |
1605 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1606 | "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n", | |
1607 | codeset, is_utf8)); | |
1d958db2 KW |
1608 | Safefree(codeset); |
1609 | Safefree(save_input_locale); | |
1610 | return is_utf8; | |
1611 | } | |
119ee68b KW |
1612 | } |
1613 | ||
1d958db2 KW |
1614 | # endif |
1615 | # ifdef MB_CUR_MAX | |
1616 | ||
1617 | /* Here, either we don't have nl_langinfo, or it didn't return a | |
1618 | * codeset. Try MB_CUR_MAX */ | |
1619 | ||
119ee68b KW |
1620 | /* Standard UTF-8 needs at least 4 bytes to represent the maximum |
1621 | * Unicode code point. Since UTF-8 is the only non-single byte | |
1622 | * encoding we handle, we just say any such encoding is UTF-8, and if | |
1623 | * turns out to be wrong, other things will fail */ | |
1624 | is_utf8 = MB_CUR_MAX >= 4; | |
1625 | ||
69014004 KW |
1626 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1627 | "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n", | |
1628 | (int) MB_CUR_MAX, is_utf8)); | |
1629 | ||
119ee68b KW |
1630 | Safefree(save_input_locale); |
1631 | ||
1632 | # ifdef HAS_MBTOWC | |
1633 | ||
1634 | /* ... But, most system that have MB_CUR_MAX will also have mbtowc(), | |
1635 | * since they are both in the C99 standard. We can feed a known byte | |
1636 | * string to the latter function, and check that it gives the expected | |
1637 | * result */ | |
1638 | if (is_utf8) { | |
1639 | wchar_t wc; | |
856b881c | 1640 | PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */ |
69014004 | 1641 | errno = 0; |
f019f68f | 1642 | if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)) |
119ee68b KW |
1643 | != strlen(HYPHEN_UTF8) |
1644 | || wc != (wchar_t) 0x2010) | |
1645 | { | |
1646 | is_utf8 = FALSE; | |
abdcbdb8 | 1647 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", (unsigned int)wc)); |
69014004 KW |
1648 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1649 | "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n", | |
1650 | mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno)); | |
119ee68b KW |
1651 | } |
1652 | } | |
119ee68b KW |
1653 | # endif |
1654 | ||
1d958db2 KW |
1655 | /* If we switched LC_CTYPE, switch back */ |
1656 | if (save_ctype_locale) { | |
1657 | setlocale(LC_CTYPE, save_ctype_locale); | |
1658 | Safefree(save_ctype_locale); | |
119ee68b | 1659 | } |
7d74bb61 | 1660 | |
1d958db2 | 1661 | return is_utf8; |
119ee68b | 1662 | # endif |
7d74bb61 | 1663 | } |
119ee68b | 1664 | |
7d74bb61 KW |
1665 | cant_use_nllanginfo: |
1666 | ||
0080c90a KW |
1667 | #else /* nl_langinfo should work if available, so don't bother compiling this |
1668 | fallback code. The final fallback of looking at the name is | |
1669 | compiled, and will be executed if nl_langinfo fails */ | |
7d74bb61 | 1670 | |
97f4de96 KW |
1671 | /* nl_langinfo not available or failed somehow. Next try looking at the |
1672 | * currency symbol to see if it disambiguates things. Often that will be | |
1673 | * in the native script, and if the symbol isn't in UTF-8, we know that the | |
1674 | * locale isn't. If it is non-ASCII UTF-8, we infer that the locale is | |
609548d2 KW |
1675 | * too, as the odds of a non-UTF8 string being valid UTF-8 are quite small |
1676 | * */ | |
fa9b773e KW |
1677 | |
1678 | #ifdef HAS_LOCALECONV | |
fa9b773e | 1679 | # ifdef USE_LOCALE_MONETARY |
fa9b773e KW |
1680 | { |
1681 | char *save_monetary_locale = NULL; | |
fa9b773e | 1682 | bool only_ascii = FALSE; |
13542a67 KW |
1683 | bool is_utf8 = FALSE; |
1684 | struct lconv* lc; | |
fa9b773e | 1685 | |
97f4de96 KW |
1686 | /* Like above for LC_CTYPE, we first set LC_MONETARY to the locale of |
1687 | * the desired category, if it isn't that locale already */ | |
1688 | ||
fa9b773e KW |
1689 | if (category != LC_MONETARY) { |
1690 | ||
4f72bb37 | 1691 | save_monetary_locale = setlocale(LC_MONETARY, NULL); |
fa9b773e | 1692 | if (! save_monetary_locale) { |
69014004 KW |
1693 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1694 | "Could not find current locale for LC_MONETARY\n")); | |
fa9b773e KW |
1695 | goto cant_use_monetary; |
1696 | } | |
4f72bb37 | 1697 | save_monetary_locale = stdize_locale(savepv(save_monetary_locale)); |
fa9b773e | 1698 | |
13542a67 KW |
1699 | if (strEQ(save_monetary_locale, save_input_locale)) { |
1700 | Safefree(save_monetary_locale); | |
1701 | save_monetary_locale = NULL; | |
1702 | } | |
1703 | else if (! setlocale(LC_MONETARY, save_input_locale)) { | |
59c234b4 KW |
1704 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1705 | "Could not change LC_MONETARY locale to %s\n", | |
1706 | save_input_locale)); | |
1707 | Safefree(save_monetary_locale); | |
1708 | goto cant_use_monetary; | |
fa9b773e KW |
1709 | } |
1710 | } | |
1711 | ||
1712 | /* Here the current LC_MONETARY is set to the locale of the category | |
1713 | * whose information is desired. */ | |
1714 | ||
13542a67 KW |
1715 | lc = localeconv(); |
1716 | if (! lc | |
1717 | || ! lc->currency_symbol | |
9f10db87 | 1718 | || is_invariant_string((U8 *) lc->currency_symbol, 0)) |
13542a67 KW |
1719 | { |
1720 | 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)); | |
1721 | only_ascii = TRUE; | |
1722 | } | |
1723 | else { | |
1724 | is_utf8 = is_utf8_string((U8 *) lc->currency_symbol, 0); | |
fa9b773e KW |
1725 | } |
1726 | ||
1727 | /* If we changed it, restore LC_MONETARY to its original locale */ | |
1728 | if (save_monetary_locale) { | |
1729 | setlocale(LC_MONETARY, save_monetary_locale); | |
1730 | Safefree(save_monetary_locale); | |
1731 | } | |
1732 | ||
13542a67 | 1733 | if (! only_ascii) { |
fa9b773e | 1734 | |
59c234b4 KW |
1735 | /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; |
1736 | * otherwise assume the locale is UTF-8 if and only if the symbol | |
1737 | * is non-ascii UTF-8. */ | |
1738 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?Currency symbol for %s is UTF-8=%d\n", | |
1739 | save_input_locale, is_utf8)); | |
1740 | Safefree(save_input_locale); | |
1741 | return is_utf8; | |
13542a67 | 1742 | } |
fa9b773e KW |
1743 | } |
1744 | cant_use_monetary: | |
1745 | ||
1746 | # endif /* USE_LOCALE_MONETARY */ | |
1747 | #endif /* HAS_LOCALECONV */ | |
1748 | ||
15f7e74e KW |
1749 | #if defined(HAS_STRFTIME) && defined(USE_LOCALE_TIME) |
1750 | ||
1751 | /* Still haven't found a non-ASCII string to disambiguate UTF-8 or not. Try | |
1752 | * the names of the months and weekdays, timezone, and am/pm indicator */ | |
1753 | { | |
1754 | char *save_time_locale = NULL; | |
1755 | int hour = 10; | |
1756 | bool is_dst = FALSE; | |
1757 | int dom = 1; | |
1758 | int month = 0; | |
1759 | int i; | |
1760 | char * formatted_time; | |
1761 | ||
1762 | ||
1763 | /* Like above for LC_MONETARY, we set LC_TIME to the locale of the | |
1764 | * desired category, if it isn't that locale already */ | |
1765 | ||
1766 | if (category != LC_TIME) { | |
1767 | ||
1768 | save_time_locale = setlocale(LC_TIME, NULL); | |
1769 | if (! save_time_locale) { | |
1770 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1771 | "Could not find current locale for LC_TIME\n")); | |
1772 | goto cant_use_time; | |
1773 | } | |
1774 | save_time_locale = stdize_locale(savepv(save_time_locale)); | |
1775 | ||
1776 | if (strEQ(save_time_locale, save_input_locale)) { | |
1777 | Safefree(save_time_locale); | |
1778 | save_time_locale = NULL; | |
1779 | } | |
1780 | else if (! setlocale(LC_TIME, save_input_locale)) { | |
1781 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1782 | "Could not change LC_TIME locale to %s\n", | |
1783 | save_input_locale)); | |
1784 | Safefree(save_time_locale); | |
1785 | goto cant_use_time; | |
1786 | } | |
1787 | } | |
1788 | ||
1789 | /* Here the current LC_TIME is set to the locale of the category | |
1790 | * whose information is desired. Look at all the days of the week and | |
9f10db87 | 1791 | * month names, and the timezone and am/pm indicator for UTF-8 variant |
15f7e74e KW |
1792 | * characters. The first such a one found will tell us if the locale |
1793 | * is UTF-8 or not */ | |
1794 | ||
1795 | for (i = 0; i < 7 + 12; i++) { /* 7 days; 12 months */ | |
1796 | formatted_time = my_strftime("%A %B %Z %p", | |
1797 | 0, 0, hour, dom, month, 112, 0, 0, is_dst); | |
9f10db87 | 1798 | if (! formatted_time || is_invariant_string((U8 *) formatted_time, 0)) { |
15f7e74e KW |
1799 | |
1800 | /* Here, we didn't find a non-ASCII. Try the next time through | |
1801 | * with the complemented dst and am/pm, and try with the next | |
1802 | * weekday. After we have gotten all weekdays, try the next | |
1803 | * month */ | |
1804 | is_dst = ! is_dst; | |
1805 | hour = (hour + 12) % 24; | |
1806 | dom++; | |
1807 | if (i > 6) { | |
1808 | month++; | |
1809 | } | |
1810 | continue; | |
1811 | } | |
1812 | ||
1813 | /* Here, we have a non-ASCII. Return TRUE is it is valid UTF8; | |
1814 | * false otherwise. But first, restore LC_TIME to its original | |
1815 | * locale if we changed it */ | |
1816 | if (save_time_locale) { | |
1817 | setlocale(LC_TIME, save_time_locale); | |
1818 | Safefree(save_time_locale); | |
1819 | } | |
1820 | ||
1821 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?time-related strings for %s are UTF-8=%d\n", | |
1822 | save_input_locale, | |
1823 | is_utf8_string((U8 *) formatted_time, 0))); | |
1824 | Safefree(save_input_locale); | |
1825 | return is_utf8_string((U8 *) formatted_time, 0); | |
1826 | } | |
1827 | ||
1828 | /* Falling off the end of the loop indicates all the names were just | |
1829 | * ASCII. Go on to the next test. If we changed it, restore LC_TIME | |
1830 | * to its original locale */ | |
1831 | if (save_time_locale) { | |
1832 | setlocale(LC_TIME, save_time_locale); | |
1833 | Safefree(save_time_locale); | |
1834 | } | |
1835 | 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)); | |
1836 | } | |
1837 | cant_use_time: | |
1838 | ||
1839 | #endif | |
1840 | ||
1841 | #if 0 && defined(USE_LOCALE_MESSAGES) && defined(HAS_SYS_ERRLIST) | |
855aeb93 JH |
1842 | |
1843 | /* This code is ifdefd out because it was found to not be necessary in testing | |
5857e934 KW |
1844 | * on our dromedary test machine, which has over 700 locales. There, this |
1845 | * added no value to looking at the currency symbol and the time strings. I | |
1846 | * left it in so as to avoid rewriting it if real-world experience indicates | |
1847 | * that dromedary is an outlier. Essentially, instead of returning abpve if we | |
855aeb93 JH |
1848 | * haven't found illegal utf8, we continue on and examine all the strerror() |
1849 | * messages on the platform for utf8ness. If all are ASCII, we still don't | |
1850 | * know the answer; but otherwise we have a pretty good indication of the | |
5857e934 KW |
1851 | * utf8ness. The reason this doesn't help much is that the messages may not |
1852 | * have been translated into the locale. The currency symbol and time strings | |
1853 | * are much more likely to have been translated. */ | |
1854 | { | |
855aeb93 | 1855 | int e; |
5857e934 KW |
1856 | bool is_utf8 = FALSE; |
1857 | bool non_ascii = FALSE; | |
855aeb93 | 1858 | char *save_messages_locale = NULL; |
5857e934 | 1859 | const char * errmsg = NULL; |
855aeb93 | 1860 | |
5857e934 KW |
1861 | /* Like above, we set LC_MESSAGES to the locale of the desired |
1862 | * category, if it isn't that locale already */ | |
855aeb93 JH |
1863 | |
1864 | if (category != LC_MESSAGES) { | |
1865 | ||
5857e934 | 1866 | save_messages_locale = setlocale(LC_MESSAGES, NULL); |
855aeb93 | 1867 | if (! save_messages_locale) { |
5857e934 KW |
1868 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1869 | "Could not find current locale for LC_MESSAGES\n")); | |
855aeb93 JH |
1870 | goto cant_use_messages; |
1871 | } | |
5857e934 | 1872 | save_messages_locale = stdize_locale(savepv(save_messages_locale)); |
855aeb93 JH |
1873 | |
1874 | if (strEQ(save_messages_locale, save_input_locale)) { | |
5857e934 KW |
1875 | Safefree(save_messages_locale); |
1876 | save_messages_locale = NULL; | |
855aeb93 JH |
1877 | } |
1878 | else if (! setlocale(LC_MESSAGES, save_input_locale)) { | |
5857e934 KW |
1879 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1880 | "Could not change LC_MESSAGES locale to %s\n", | |
1881 | save_input_locale)); | |
855aeb93 JH |
1882 | Safefree(save_messages_locale); |
1883 | goto cant_use_messages; | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | /* Here the current LC_MESSAGES is set to the locale of the category | |
5857e934 KW |
1888 | * whose information is desired. Look through all the messages. We |
1889 | * can't use Strerror() here because it may expand to code that | |
1890 | * segfaults in miniperl */ | |
855aeb93 | 1891 | |
5857e934 KW |
1892 | for (e = 0; e <= sys_nerr; e++) { |
1893 | errno = 0; | |
1894 | errmsg = sys_errlist[e]; | |
1895 | if (errno || !errmsg) { | |
855aeb93 JH |
1896 | break; |
1897 | } | |
5857e934 | 1898 | errmsg = savepv(errmsg); |
9f10db87 | 1899 | if (! is_invariant_string((U8 *) errmsg, 0)) { |
5857e934 KW |
1900 | non_ascii = TRUE; |
1901 | is_utf8 = is_utf8_string((U8 *) errmsg, 0); | |
1902 | break; | |
855aeb93 JH |
1903 | } |
1904 | } | |
5857e934 | 1905 | Safefree(errmsg); |
855aeb93 JH |
1906 | |
1907 | /* And, if we changed it, restore LC_MESSAGES to its original locale */ | |
1908 | if (save_messages_locale) { | |
1909 | setlocale(LC_MESSAGES, save_messages_locale); | |
1910 | Safefree(save_messages_locale); | |
1911 | } | |
1912 | ||
5857e934 KW |
1913 | if (non_ascii) { |
1914 | ||
1915 | /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid, | |
1916 | * any non-ascii means it is one; otherwise we assume it isn't */ | |
1917 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?error messages for %s are UTF-8=%d\n", | |
1918 | save_input_locale, | |
1919 | is_utf8)); | |
1920 | Safefree(save_input_locale); | |
1921 | return is_utf8; | |
1922 | } | |
855aeb93 | 1923 | |
5857e934 | 1924 | 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 |
1925 | } |
1926 | cant_use_messages: | |
1927 | ||
1928 | #endif | |
fa9b773e | 1929 | |
0080c90a KW |
1930 | #endif /* the code that is compiled when no nl_langinfo */ |
1931 | ||
92c0a900 KW |
1932 | #ifndef EBCDIC /* On os390, even if the name ends with "UTF-8', it isn't a |
1933 | UTF-8 locale */ | |
97f4de96 KW |
1934 | /* As a last resort, look at the locale name to see if it matches |
1935 | * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the | |
1936 | * return of setlocale(), is actually defined to be opaque, so we can't | |
1937 | * really rely on the absence of various substrings in the name to indicate | |
1938 | * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to | |
1939 | * be a UTF-8 locale. Similarly for the other common names */ | |
1940 | ||
1941 | final_pos = strlen(save_input_locale) - 1; | |
1942 | if (final_pos >= 3) { | |
1943 | char *name = save_input_locale; | |
1944 | ||
1945 | /* Find next 'U' or 'u' and look from there */ | |
1946 | while ((name += strcspn(name, "Uu") + 1) | |
1947 | <= save_input_locale + final_pos - 2) | |
1948 | { | |
305b8651 KW |
1949 | if (!isALPHA_FOLD_NE(*name, 't') |
1950 | || isALPHA_FOLD_NE(*(name + 1), 'f')) | |
97f4de96 KW |
1951 | { |
1952 | continue; | |
1953 | } | |
1954 | name += 2; | |
1955 | if (*(name) == '-') { | |
1956 | if ((name > save_input_locale + final_pos - 1)) { | |
1957 | break; | |
1958 | } | |
1959 | name++; | |
1960 | } | |
1961 | if (*(name) == '8') { | |
97f4de96 KW |
1962 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1963 | "Locale %s ends with UTF-8 in name\n", | |
1964 | save_input_locale)); | |
00c54b9c | 1965 | Safefree(save_input_locale); |
97f4de96 KW |
1966 | return TRUE; |
1967 | } | |
1968 | } | |
1969 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1970 | "Locale %s doesn't end with UTF-8 in name\n", | |
1971 | save_input_locale)); | |
1972 | } | |
92c0a900 | 1973 | #endif |
97f4de96 KW |
1974 | |
1975 | #ifdef WIN32 | |
1976 | /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */ | |
1977 | if (final_pos >= 4 | |
1978 | && *(save_input_locale + final_pos - 0) == '1' | |
1979 | && *(save_input_locale + final_pos - 1) == '0' | |
1980 | && *(save_input_locale + final_pos - 2) == '0' | |
1981 | && *(save_input_locale + final_pos - 3) == '5' | |
1982 | && *(save_input_locale + final_pos - 4) == '6') | |
1983 | { | |
1984 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1985 | "Locale %s ends with 10056 in name, is UTF-8 locale\n", | |
1986 | save_input_locale)); | |
1987 | Safefree(save_input_locale); | |
1988 | return TRUE; | |
1989 | } | |
1990 | #endif | |
1991 | ||
1992 | /* Other common encodings are the ISO 8859 series, which aren't UTF-8. But | |
1993 | * since we are about to return FALSE anyway, there is no point in doing | |
1994 | * this extra work */ | |
1995 | #if 0 | |
1996 | if (instr(save_input_locale, "8859")) { | |
1997 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1998 | "Locale %s has 8859 in name, not UTF-8 locale\n", | |
1999 | save_input_locale)); | |
2000 | Safefree(save_input_locale); | |
2001 | return FALSE; | |
2002 | } | |
2003 | #endif | |
2004 | ||
69014004 KW |
2005 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
2006 | "Assuming locale %s is not a UTF-8 locale\n", | |
2007 | save_input_locale)); | |
fa9b773e | 2008 | Safefree(save_input_locale); |
7d74bb61 KW |
2009 | return FALSE; |
2010 | } | |
2011 | ||
8ef6e574 | 2012 | #endif |
7d74bb61 | 2013 | |
d6ded950 KW |
2014 | |
2015 | bool | |
2016 | Perl__is_in_locale_category(pTHX_ const bool compiling, const int category) | |
2017 | { | |
1a4f13e1 | 2018 | dVAR; |
d6ded950 KW |
2019 | /* Internal function which returns if we are in the scope of a pragma that |
2020 | * enables the locale category 'category'. 'compiling' should indicate if | |
2021 | * this is during the compilation phase (TRUE) or not (FALSE). */ | |
2022 | ||
2023 | const COP * const cop = (compiling) ? &PL_compiling : PL_curcop; | |
2024 | ||
2025 | SV *categories = cop_hints_fetch_pvs(cop, "locale", 0); | |
2026 | if (! categories || categories == &PL_sv_placeholder) { | |
2027 | return FALSE; | |
2028 | } | |
2029 | ||
2030 | /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get | |
2031 | * a valid unsigned */ | |
2032 | assert(category >= -1); | |
2033 | return cBOOL(SvUV(categories) & (1U << (category + 1))); | |
2034 | } | |
2035 | ||
2c6ee1a7 KW |
2036 | char * |
2037 | Perl_my_strerror(pTHX_ const int errnum) { | |
a0b53297 | 2038 | dVAR; |
2c6ee1a7 KW |
2039 | |
2040 | /* Uses C locale for the error text unless within scope of 'use locale' for | |
2041 | * LC_MESSAGES */ | |
2042 | ||
2043 | #ifdef USE_LOCALE_MESSAGES | |
8ee4b769 | 2044 | if (! IN_LC(LC_MESSAGES)) { |
a0b53297 KW |
2045 | char * save_locale; |
2046 | ||
2047 | /* We have a critical section to prevent another thread from changing | |
2048 | * the locale out from under us (or zapping the buffer returned from | |
2049 | * setlocale() ) */ | |
2050 | LOCALE_LOCK; | |
2051 | ||
2052 | save_locale = setlocale(LC_MESSAGES, NULL); | |
a39edc4c | 2053 | if (! isNAME_C_OR_POSIX(save_locale)) { |
2c6ee1a7 KW |
2054 | char *errstr; |
2055 | ||
2056 | /* The next setlocale likely will zap this, so create a copy */ | |
2057 | save_locale = savepv(save_locale); | |
2058 | ||
2059 | setlocale(LC_MESSAGES, "C"); | |
2060 | ||
2061 | /* This points to the static space in Strerror, with all its | |
2062 | * limitations */ | |
2063 | errstr = Strerror(errnum); | |
2064 | ||
2065 | setlocale(LC_MESSAGES, save_locale); | |
2066 | Safefree(save_locale); | |
a0b53297 KW |
2067 | |
2068 | LOCALE_UNLOCK; | |
2069 | ||
2c6ee1a7 KW |
2070 | return errstr; |
2071 | } | |
a0b53297 KW |
2072 | |
2073 | LOCALE_UNLOCK; | |
2c6ee1a7 KW |
2074 | } |
2075 | #endif | |
2076 | ||
2077 | return Strerror(errnum); | |
2078 | } | |
2079 | ||
66610fdd | 2080 | /* |
747c467a KW |
2081 | |
2082 | =head1 Locale-related functions and macros | |
2083 | ||
2084 | =for apidoc sync_locale | |
2085 | ||
2086 | Changing the program's locale should be avoided by XS code. Nevertheless, | |
2087 | certain non-Perl libraries called from XS, such as C<Gtk> do so. When this | |
2088 | happens, Perl needs to be told that the locale has changed. Use this function | |
2089 | to do so, before returning to Perl. | |
2090 | ||
2091 | =cut | |
2092 | */ | |
2093 | ||
2094 | void | |
2095 | Perl_sync_locale(pTHX) | |
2096 | { | |
2097 | ||
2098 | #ifdef USE_LOCALE_CTYPE | |
2099 | new_ctype(setlocale(LC_CTYPE, NULL)); | |
2100 | #endif /* USE_LOCALE_CTYPE */ | |
2101 | ||
2102 | #ifdef USE_LOCALE_COLLATE | |
2103 | new_collate(setlocale(LC_COLLATE, NULL)); | |
2104 | #endif | |
2105 | ||
2106 | #ifdef USE_LOCALE_NUMERIC | |
2107 | set_numeric_local(); /* Switch from "C" to underlying LC_NUMERIC */ | |
2108 | new_numeric(setlocale(LC_NUMERIC, NULL)); | |
2109 | #endif /* USE_LOCALE_NUMERIC */ | |
2110 | ||
2111 | } | |
2112 | ||
5d1187d1 KW |
2113 | #if defined(DEBUGGING) && defined(USE_LOCALE) |
2114 | ||
2115 | char * | |
2116 | Perl__setlocale_debug_string(const int category, /* category number, | |
2117 | like LC_ALL */ | |
2118 | const char* const locale, /* locale name */ | |
2119 | ||
2120 | /* return value from setlocale() when attempting to | |
2121 | * set 'category' to 'locale' */ | |
2122 | const char* const retval) | |
2123 | { | |
2124 | /* Returns a pointer to a NUL-terminated string in static storage with | |
2125 | * added text about the info passed in. This is not thread safe and will | |
2126 | * be overwritten by the next call, so this should be used just to | |
fa07b8e5 | 2127 | * formulate a string to immediately print or savepv() on. */ |
5d1187d1 | 2128 | |
398a990f DM |
2129 | /* initialise to a non-null value to keep it out of BSS and so keep |
2130 | * -DPERL_GLOBAL_STRUCT_PRIVATE happy */ | |
2131 | static char ret[128] = "x"; | |
5d1187d1 | 2132 | |
fa07b8e5 | 2133 | my_strlcpy(ret, "setlocale(", sizeof(ret)); |
5d1187d1 KW |
2134 | |
2135 | switch (category) { | |
2136 | default: | |
fa07b8e5 | 2137 | my_snprintf(ret, sizeof(ret), "%s? %d", ret, category); |
5d1187d1 KW |
2138 | break; |
2139 | # ifdef LC_ALL | |
2140 | case LC_ALL: | |
fa07b8e5 | 2141 | my_strlcat(ret, "LC_ALL", sizeof(ret)); |
5d1187d1 KW |
2142 | break; |
2143 | # endif | |
2144 | # ifdef LC_CTYPE | |
2145 | case LC_CTYPE: | |
fa07b8e5 | 2146 | my_strlcat(ret, "LC_CTYPE", sizeof(ret)); |
5d1187d1 KW |
2147 | break; |
2148 | # endif | |
2149 | # ifdef LC_NUMERIC | |
2150 | case LC_NUMERIC: | |
fa07b8e5 | 2151 | my_strlcat(ret, "LC_NUMERIC", sizeof(ret)); |
5d1187d1 KW |
2152 | break; |
2153 | # endif | |
2154 | # ifdef LC_COLLATE | |
2155 | case LC_COLLATE: | |
fa07b8e5 | 2156 | my_strlcat(ret, "LC_COLLATE", sizeof(ret)); |
5d1187d1 KW |
2157 | break; |
2158 | # endif | |
2159 | # ifdef LC_TIME | |
2160 | case LC_TIME: | |
fa07b8e5 | 2161 | my_strlcat(ret, "LC_TIME", sizeof(ret)); |
5d1187d1 KW |
2162 | break; |
2163 | # endif | |
2164 | # ifdef LC_MONETARY | |
2165 | case LC_MONETARY: | |
fa07b8e5 | 2166 | my_strlcat(ret, "LC_MONETARY", sizeof(ret)); |
5d1187d1 KW |
2167 | break; |
2168 | # endif | |
2169 | # ifdef LC_MESSAGES | |
2170 | case LC_MESSAGES: | |
fa07b8e5 | 2171 | my_strlcat(ret, "LC_MESSAGES", sizeof(ret)); |
5d1187d1 KW |
2172 | break; |
2173 | # endif | |
2174 | } | |
2175 | ||
fa07b8e5 | 2176 | my_strlcat(ret, ", ", sizeof(ret)); |
5d1187d1 KW |
2177 | |
2178 | if (locale) { | |
fa07b8e5 KW |
2179 | my_strlcat(ret, "\"", sizeof(ret)); |
2180 | my_strlcat(ret, locale, sizeof(ret)); | |
2181 | my_strlcat(ret, "\"", sizeof(ret)); | |
5d1187d1 KW |
2182 | } |
2183 | else { | |
fa07b8e5 | 2184 | my_strlcat(ret, "NULL", sizeof(ret)); |
5d1187d1 KW |
2185 | } |
2186 | ||
fa07b8e5 | 2187 | my_strlcat(ret, ") returned ", sizeof(ret)); |
5d1187d1 KW |
2188 | |
2189 | if (retval) { | |
fa07b8e5 KW |
2190 | my_strlcat(ret, "\"", sizeof(ret)); |
2191 | my_strlcat(ret, retval, sizeof(ret)); | |
2192 | my_strlcat(ret, "\"", sizeof(ret)); | |
5d1187d1 KW |
2193 | } |
2194 | else { | |
fa07b8e5 | 2195 | my_strlcat(ret, "NULL", sizeof(ret)); |
5d1187d1 KW |
2196 | } |
2197 | ||
2198 | assert(strlen(ret) < sizeof(ret)); | |
2199 | ||
2200 | return ret; | |
2201 | } | |
2202 | ||
2203 | #endif | |
747c467a KW |
2204 | |
2205 | ||
2206 | /* | |
14d04a33 | 2207 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 2208 | */ |