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 | 25 | * |
7d4bcc4a KW |
26 | * All C programs have an underlying locale. Perl code generally doesn't pay |
27 | * any attention to it except within the scope of a 'use locale'. For most | |
0d071d52 KW |
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 | |
f7416781 | 39 | #include "perl_langinfo.h" |
98994639 HS |
40 | #include "perl.h" |
41 | ||
a4af207c JH |
42 | #include "reentr.h" |
43 | ||
0dec74cd KW |
44 | #ifdef I_WCHAR |
45 | # include <wchar.h> | |
46 | #endif | |
47 | ||
2fcc0ca9 KW |
48 | /* If the environment says to, we can output debugging information during |
49 | * initialization. This is done before option parsing, and before any thread | |
50 | * creation, so can be a file-level static */ | |
5a4b0634 KW |
51 | #if ! defined(DEBUGGING) || defined(PERL_GLOBAL_STRUCT) |
52 | # define debug_initialization 0 | |
53 | # define DEBUG_INITIALIZATION_set(v) | |
54 | #else | |
2fcc0ca9 | 55 | static bool debug_initialization = FALSE; |
5a4b0634 | 56 | # define DEBUG_INITIALIZATION_set(v) (debug_initialization = v) |
2fcc0ca9 KW |
57 | #endif |
58 | ||
48015184 KW |
59 | |
60 | /* Returns the Unix errno portion; ignoring any others. This is a macro here | |
61 | * instead of putting it into perl.h, because unclear to khw what should be | |
62 | * done generally. */ | |
63 | #define GET_ERRNO saved_errno | |
64 | ||
291a84fb KW |
65 | /* strlen() of a literal string constant. We might want this more general, |
66 | * but using it in just this file for now. A problem with more generality is | |
67 | * the compiler warnings about comparing unlike signs */ | |
ff1b739b KW |
68 | #define STRLENs(s) (sizeof("" s "") - 1) |
69 | ||
63e5b0d7 KW |
70 | /* Is the C string input 'name' "C" or "POSIX"? If so, and 'name' is the |
71 | * return of setlocale(), then this is extremely likely to be the C or POSIX | |
72 | * locale. However, the output of setlocale() is documented to be opaque, but | |
73 | * the odds are extremely small that it would return these two strings for some | |
74 | * other locale. Note that VMS in these two locales includes many non-ASCII | |
75 | * characters as controls and punctuation (below are hex bytes): | |
76 | * cntrl: 84-97 9B-9F | |
77 | * punct: A1-A3 A5 A7-AB B0-B3 B5-B7 B9-BD BF-CF D1-DD DF-EF F1-FD | |
78 | * Oddly, none there are listed as alphas, though some represent alphabetics | |
79 | * http://www.nntp.perl.org/group/perl.perl5.porters/2013/02/msg198753.html */ | |
80 | #define isNAME_C_OR_POSIX(name) \ | |
81 | ( (name) != NULL \ | |
82 | && (( *(name) == 'C' && (*(name + 1)) == '\0') \ | |
83 | || strEQ((name), "POSIX"))) | |
84 | ||
8ef6e574 KW |
85 | #ifdef USE_LOCALE |
86 | ||
47280b20 KW |
87 | /* This code keeps a LRU cache of the UTF-8ness of the locales it has so-far |
88 | * looked up. This is in the form of a C string: */ | |
89 | ||
90 | #define UTF8NESS_SEP "\v" | |
91 | #define UTF8NESS_PREFIX "\f" | |
92 | ||
93 | /* So, the string looks like: | |
98994639 | 94 | * |
47280b20 | 95 | * \vC\a0\vPOSIX\a0\vam_ET\a0\vaf_ZA.utf8\a1\ven_US.UTF-8\a1\0 |
98994639 | 96 | * |
47280b20 KW |
97 | * where the digit 0 after the \a indicates that the locale starting just |
98 | * after the preceding \v is not UTF-8, and the digit 1 mean it is. */ | |
99 | ||
100 | STATIC_ASSERT_DECL(STRLENs(UTF8NESS_SEP) == 1); | |
101 | STATIC_ASSERT_DECL(STRLENs(UTF8NESS_PREFIX) == 1); | |
102 | ||
103 | #define C_and_POSIX_utf8ness UTF8NESS_SEP "C" UTF8NESS_PREFIX "0" \ | |
104 | UTF8NESS_SEP "POSIX" UTF8NESS_PREFIX "0" | |
105 | ||
106 | /* The cache is initialized to C_and_POSIX_utf8ness at start up. These are | |
107 | * kept there always. The remining portion of the cache is LRU, with the | |
108 | * oldest looked-up locale at the tail end */ | |
109 | ||
98994639 HS |
110 | STATIC char * |
111 | S_stdize_locale(pTHX_ char *locs) | |
112 | { | |
47280b20 KW |
113 | /* Standardize the locale name from a string returned by 'setlocale', |
114 | * possibly modifying that string. | |
115 | * | |
116 | * The typical return value of setlocale() is either | |
117 | * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL | |
118 | * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL | |
119 | * (the space-separated values represent the various sublocales, | |
120 | * in some unspecified order). This is not handled by this function. | |
121 | * | |
122 | * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n", | |
123 | * which is harmful for further use of the string in setlocale(). This | |
124 | * function removes the trailing new line and everything up through the '=' | |
125 | * */ | |
126 | ||
7452cf6a | 127 | const char * const s = strchr(locs, '='); |
98994639 HS |
128 | bool okay = TRUE; |
129 | ||
7918f24d NC |
130 | PERL_ARGS_ASSERT_STDIZE_LOCALE; |
131 | ||
8772537c AL |
132 | if (s) { |
133 | const char * const t = strchr(s, '.'); | |
98994639 | 134 | okay = FALSE; |
8772537c AL |
135 | if (t) { |
136 | const char * const u = strchr(t, '\n'); | |
137 | if (u && (u[1] == 0)) { | |
138 | const STRLEN len = u - s; | |
139 | Move(s + 1, locs, len, char); | |
140 | locs[len] = 0; | |
141 | okay = TRUE; | |
98994639 HS |
142 | } |
143 | } | |
144 | } | |
145 | ||
146 | if (!okay) | |
147 | Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs); | |
148 | ||
149 | return locs; | |
150 | } | |
151 | ||
e5f10d49 KW |
152 | /* Two parallel arrays; first the locale categories Perl uses on this system; |
153 | * the second array is their names. These arrays are in mostly arbitrary | |
154 | * order. */ | |
155 | ||
156 | const int categories[] = { | |
157 | ||
158 | # ifdef USE_LOCALE_NUMERIC | |
159 | LC_NUMERIC, | |
160 | # endif | |
161 | # ifdef USE_LOCALE_CTYPE | |
162 | LC_CTYPE, | |
163 | # endif | |
164 | # ifdef USE_LOCALE_COLLATE | |
165 | LC_COLLATE, | |
166 | # endif | |
167 | # ifdef USE_LOCALE_TIME | |
168 | LC_TIME, | |
169 | # endif | |
170 | # ifdef USE_LOCALE_MESSAGES | |
171 | LC_MESSAGES, | |
172 | # endif | |
173 | # ifdef USE_LOCALE_MONETARY | |
174 | LC_MONETARY, | |
175 | # endif | |
9821811f KW |
176 | # ifdef USE_LOCALE_ADDRESS |
177 | LC_ADDRESS, | |
178 | # endif | |
179 | # ifdef USE_LOCALE_IDENTIFICATION | |
180 | LC_IDENTIFICATION, | |
181 | # endif | |
182 | # ifdef USE_LOCALE_MEASUREMENT | |
183 | LC_MEASUREMENT, | |
184 | # endif | |
185 | # ifdef USE_LOCALE_PAPER | |
186 | LC_PAPER, | |
187 | # endif | |
188 | # ifdef USE_LOCALE_TELEPHONE | |
189 | LC_TELEPHONE, | |
190 | # endif | |
e5f10d49 KW |
191 | # ifdef LC_ALL |
192 | LC_ALL, | |
193 | # endif | |
194 | -1 /* Placeholder because C doesn't allow a | |
195 | trailing comma, and it would get complicated | |
196 | with all the #ifdef's */ | |
197 | }; | |
198 | ||
199 | /* The top-most real element is LC_ALL */ | |
200 | ||
201 | const char * category_names[] = { | |
202 | ||
203 | # ifdef USE_LOCALE_NUMERIC | |
204 | "LC_NUMERIC", | |
205 | # endif | |
206 | # ifdef USE_LOCALE_CTYPE | |
207 | "LC_CTYPE", | |
208 | # endif | |
209 | # ifdef USE_LOCALE_COLLATE | |
210 | "LC_COLLATE", | |
211 | # endif | |
212 | # ifdef USE_LOCALE_TIME | |
213 | "LC_TIME", | |
214 | # endif | |
215 | # ifdef USE_LOCALE_MESSAGES | |
216 | "LC_MESSAGES", | |
217 | # endif | |
218 | # ifdef USE_LOCALE_MONETARY | |
219 | "LC_MONETARY", | |
220 | # endif | |
9821811f KW |
221 | # ifdef USE_LOCALE_ADDRESS |
222 | "LC_ADDRESS", | |
223 | # endif | |
224 | # ifdef USE_LOCALE_IDENTIFICATION | |
225 | "LC_IDENTIFICATION", | |
226 | # endif | |
227 | # ifdef USE_LOCALE_MEASUREMENT | |
228 | "LC_MEASUREMENT", | |
229 | # endif | |
230 | # ifdef USE_LOCALE_PAPER | |
231 | "LC_PAPER", | |
232 | # endif | |
233 | # ifdef USE_LOCALE_TELEPHONE | |
234 | "LC_TELEPHONE", | |
235 | # endif | |
e5f10d49 KW |
236 | # ifdef LC_ALL |
237 | "LC_ALL", | |
238 | # endif | |
239 | NULL /* Placeholder */ | |
240 | }; | |
241 | ||
242 | # ifdef LC_ALL | |
243 | ||
244 | /* On systems with LC_ALL, it is kept in the highest index position. (-2 | |
245 | * to account for the final unused placeholder element.) */ | |
246 | # define NOMINAL_LC_ALL_INDEX (C_ARRAY_LENGTH(categories) - 2) | |
247 | ||
248 | # else | |
249 | ||
250 | /* On systems without LC_ALL, we pretend it is there, one beyond the real | |
251 | * top element, hence in the unused placeholder element. */ | |
252 | # define NOMINAL_LC_ALL_INDEX (C_ARRAY_LENGTH(categories) - 1) | |
253 | ||
254 | # endif | |
255 | ||
256 | /* Pretending there is an LC_ALL element just above allows us to avoid most | |
257 | * special cases. Most loops through these arrays in the code below are | |
258 | * written like 'for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++)'. They will work | |
259 | * on either type of system. But the code must be written to not access the | |
948523db KW |
260 | * element at 'LC_ALL_INDEX' except on platforms that have it. This can be |
261 | * checked for at compile time by using the #define LC_ALL_INDEX which is only | |
262 | * defined if we do have LC_ALL. */ | |
e5f10d49 | 263 | |
b09aaf40 KW |
264 | STATIC const char * |
265 | S_category_name(const int category) | |
266 | { | |
267 | unsigned int i; | |
268 | ||
269 | #ifdef LC_ALL | |
270 | ||
271 | if (category == LC_ALL) { | |
272 | return "LC_ALL"; | |
273 | } | |
274 | ||
275 | #endif | |
276 | ||
277 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) { | |
278 | if (category == categories[i]) { | |
279 | return category_names[i]; | |
280 | } | |
281 | } | |
282 | ||
283 | { | |
284 | const char suffix[] = " (unknown)"; | |
285 | int temp = category; | |
286 | Size_t length = sizeof(suffix) + 1; | |
287 | char * unknown; | |
288 | dTHX; | |
289 | ||
290 | if (temp < 0) { | |
291 | length++; | |
292 | temp = - temp; | |
293 | } | |
294 | ||
295 | /* Calculate the number of digits */ | |
296 | while (temp >= 10) { | |
297 | temp /= 10; | |
298 | length++; | |
299 | } | |
300 | ||
301 | Newx(unknown, length, char); | |
302 | my_snprintf(unknown, length, "%d%s", category, suffix); | |
303 | SAVEFREEPV(unknown); | |
304 | return unknown; | |
305 | } | |
306 | } | |
307 | ||
948523db KW |
308 | /* Now create LC_foo_INDEX #defines for just those categories on this system */ |
309 | # ifdef USE_LOCALE_NUMERIC | |
310 | # define LC_NUMERIC_INDEX 0 | |
311 | # define _DUMMY_NUMERIC LC_NUMERIC_INDEX | |
312 | # else | |
313 | # define _DUMMY_NUMERIC -1 | |
314 | # endif | |
315 | # ifdef USE_LOCALE_CTYPE | |
316 | # define LC_CTYPE_INDEX _DUMMY_NUMERIC + 1 | |
317 | # define _DUMMY_CTYPE LC_CTYPE_INDEX | |
318 | # else | |
319 | # define _DUMMY_CTYPE _DUMMY_NUMERIC | |
320 | # endif | |
321 | # ifdef USE_LOCALE_COLLATE | |
322 | # define LC_COLLATE_INDEX _DUMMY_CTYPE + 1 | |
323 | # define _DUMMY_COLLATE LC_COLLATE_INDEX | |
324 | # else | |
325 | # define _DUMMY_COLLATE _DUMMY_COLLATE | |
326 | # endif | |
327 | # ifdef USE_LOCALE_TIME | |
328 | # define LC_TIME_INDEX _DUMMY_COLLATE + 1 | |
329 | # define _DUMMY_TIME LC_TIME_INDEX | |
330 | # else | |
331 | # define _DUMMY_TIME _DUMMY_COLLATE | |
332 | # endif | |
333 | # ifdef USE_LOCALE_MESSAGES | |
334 | # define LC_MESSAGES_INDEX _DUMMY_TIME + 1 | |
335 | # define _DUMMY_MESSAGES LC_MESSAGES_INDEX | |
336 | # else | |
337 | # define _DUMMY_MESSAGES _DUMMY_TIME | |
338 | # endif | |
339 | # ifdef USE_LOCALE_MONETARY | |
340 | # define LC_MONETARY_INDEX _DUMMY_MESSAGES + 1 | |
341 | # define _DUMMY_MONETARY LC_MONETARY_INDEX | |
342 | # else | |
343 | # define _DUMMY_MONETARY _DUMMY_MESSAGES | |
344 | # endif | |
9821811f KW |
345 | # ifdef USE_LOCALE_ADDRESS |
346 | # define LC_ADDRESS_INDEX _DUMMY_MONETARY + 1 | |
347 | # define _DUMMY_ADDRESS LC_ADDRESS_INDEX | |
348 | # else | |
349 | # define _DUMMY_ADDRESS _DUMMY_MONETARY | |
350 | # endif | |
351 | # ifdef USE_LOCALE_IDENTIFICATION | |
352 | # define LC_IDENTIFICATION_INDEX _DUMMY_ADDRESS + 1 | |
353 | # define _DUMMY_IDENTIFICATION LC_IDENTIFICATION_INDEX | |
354 | # else | |
355 | # define _DUMMY_IDENTIFICATION _DUMMY_ADDRESS | |
356 | # endif | |
357 | # ifdef USE_LOCALE_MEASUREMENT | |
358 | # define LC_MEASUREMENT_INDEX _DUMMY_IDENTIFICATION + 1 | |
359 | # define _DUMMY_MEASUREMENT LC_MEASUREMENT_INDEX | |
360 | # else | |
361 | # define _DUMMY_MEASUREMENT _DUMMY_IDENTIFICATION | |
362 | # endif | |
363 | # ifdef USE_LOCALE_PAPER | |
364 | # define LC_PAPER_INDEX _DUMMY_MEASUREMENT + 1 | |
365 | # define _DUMMY_PAPER LC_PAPER_INDEX | |
366 | # else | |
367 | # define _DUMMY_PAPER _DUMMY_MEASUREMENT | |
368 | # endif | |
369 | # ifdef USE_LOCALE_TELEPHONE | |
370 | # define LC_TELEPHONE_INDEX _DUMMY_PAPER + 1 | |
371 | # define _DUMMY_TELEPHONE LC_TELEPHONE_INDEX | |
372 | # else | |
373 | # define _DUMMY_TELEPHONE _DUMMY_PAPER | |
374 | # endif | |
948523db | 375 | # ifdef LC_ALL |
9821811f | 376 | # define LC_ALL_INDEX _DUMMY_TELEPHONE + 1 |
948523db KW |
377 | # endif |
378 | #endif /* ifdef USE_LOCALE */ | |
8ef6e574 | 379 | |
d2b24094 KW |
380 | /* Windows requres a customized base-level setlocale() */ |
381 | # ifdef WIN32 | |
382 | # define my_setlocale(cat, locale) win32_setlocale(cat, locale) | |
383 | # else | |
384 | # define my_setlocale(cat, locale) setlocale(cat, locale) | |
385 | # endif | |
386 | ||
837ce802 KW |
387 | /* Just placeholders for now. "_c" is intended to be called when the category |
388 | * is a constant known at compile time; "_r", not known until run time */ | |
d2b24094 KW |
389 | # define do_setlocale_c(category, locale) my_setlocale(category, locale) |
390 | # define do_setlocale_r(category, locale) my_setlocale(category, locale) | |
837ce802 | 391 | |
a4f00dcc | 392 | STATIC void |
86799d2d | 393 | S_set_numeric_radix(pTHX_ const bool use_locale) |
98994639 | 394 | { |
86799d2d KW |
395 | /* If 'use_locale' is FALSE, set to use a dot for the radix character. If |
396 | * TRUE, use the radix character derived from the current locale */ | |
7d4bcc4a | 397 | |
86799d2d KW |
398 | #if defined(USE_LOCALE_NUMERIC) && ( defined(HAS_LOCALECONV) \ |
399 | || defined(HAS_NL_LANGINFO)) | |
98994639 | 400 | |
87f8e8e7 KW |
401 | const char * radix = (use_locale) |
402 | ? my_nl_langinfo(PERL_RADIXCHAR, FALSE) | |
403 | /* FALSE => already in dest locale */ | |
404 | : "."; | |
2213a3be | 405 | |
87f8e8e7 | 406 | sv_setpv(PL_numeric_radix_sv, radix); |
86799d2d | 407 | |
87f8e8e7 KW |
408 | /* If this is valid UTF-8 that isn't totally ASCII, and we are in |
409 | * a UTF-8 locale, then mark the radix as being in UTF-8 */ | |
410 | if (is_utf8_non_invariant_string((U8 *) SvPVX(PL_numeric_radix_sv), | |
7a393424 | 411 | SvCUR(PL_numeric_radix_sv)) |
87f8e8e7 KW |
412 | && _is_cur_LC_category_utf8(LC_NUMERIC)) |
413 | { | |
414 | SvUTF8_on(PL_numeric_radix_sv); | |
415 | } | |
86799d2d KW |
416 | |
417 | # ifdef DEBUGGING | |
7d4bcc4a | 418 | |
2fcc0ca9 KW |
419 | if (DEBUG_L_TEST || debug_initialization) { |
420 | PerlIO_printf(Perl_debug_log, "Locale radix is '%s', ?UTF-8=%d\n", | |
3ca88433 KW |
421 | SvPVX(PL_numeric_radix_sv), |
422 | cBOOL(SvUTF8(PL_numeric_radix_sv))); | |
2fcc0ca9 | 423 | } |
69014004 | 424 | |
86799d2d KW |
425 | # endif |
426 | #endif /* USE_LOCALE_NUMERIC and can find the radix char */ | |
7d4bcc4a | 427 | |
98994639 HS |
428 | } |
429 | ||
a39edc4c | 430 | |
98994639 | 431 | void |
8772537c | 432 | Perl_new_numeric(pTHX_ const char *newnum) |
98994639 | 433 | { |
7d4bcc4a KW |
434 | |
435 | #ifndef USE_LOCALE_NUMERIC | |
436 | ||
437 | PERL_UNUSED_ARG(newnum); | |
438 | ||
439 | #else | |
0d071d52 | 440 | |
291a84fb | 441 | /* Called after each libc setlocale() call affecting LC_NUMERIC, to tell |
0d071d52 KW |
442 | * core Perl this and that 'newnum' is the name of the new locale. |
443 | * It installs this locale as the current underlying default. | |
444 | * | |
445 | * The default locale and the C locale can be toggled between by use of the | |
5792c642 KW |
446 | * set_numeric_underlying() and set_numeric_standard() functions, which |
447 | * should probably not be called directly, but only via macros like | |
0d071d52 KW |
448 | * SET_NUMERIC_STANDARD() in perl.h. |
449 | * | |
450 | * The toggling is necessary mainly so that a non-dot radix decimal point | |
451 | * character can be output, while allowing internal calculations to use a | |
452 | * dot. | |
453 | * | |
454 | * This sets several interpreter-level variables: | |
bb304765 | 455 | * PL_numeric_name The underlying locale's name: a copy of 'newnum' |
892e6465 | 456 | * PL_numeric_underlying A boolean indicating if the toggled state is such |
7738054c KW |
457 | * that the current locale is the program's underlying |
458 | * locale | |
459 | * PL_numeric_standard An int indicating if the toggled state is such | |
4c68b815 KW |
460 | * that the current locale is the C locale or |
461 | * indistinguishable from the C locale. If non-zero, it | |
462 | * is in C; if > 1, it means it may not be toggled away | |
7738054c | 463 | * from C. |
4c68b815 KW |
464 | * PL_numeric_underlying_is_standard A bool kept by this function |
465 | * indicating that the underlying locale and the standard | |
466 | * C locale are indistinguishable for the purposes of | |
467 | * LC_NUMERIC. This happens when both of the above two | |
468 | * variables are true at the same time. (Toggling is a | |
469 | * no-op under these circumstances.) This variable is | |
470 | * used to avoid having to recalculate. | |
0d071d52 KW |
471 | * Any code changing the locale (outside this file) should use |
472 | * POSIX::setlocale, which calls this function. Therefore this function | |
473 | * should be called directly only from this file and from | |
474 | * POSIX::setlocale() */ | |
475 | ||
b03f34cf | 476 | char *save_newnum; |
98994639 HS |
477 | |
478 | if (! newnum) { | |
43c5f42d NC |
479 | Safefree(PL_numeric_name); |
480 | PL_numeric_name = NULL; | |
98994639 | 481 | PL_numeric_standard = TRUE; |
892e6465 | 482 | PL_numeric_underlying = TRUE; |
4c68b815 | 483 | PL_numeric_underlying_is_standard = TRUE; |
98994639 HS |
484 | return; |
485 | } | |
486 | ||
b03f34cf | 487 | save_newnum = stdize_locale(savepv(newnum)); |
892e6465 | 488 | PL_numeric_underlying = TRUE; |
4c68b815 KW |
489 | PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum); |
490 | ||
491 | /* If its name isn't C nor POSIX, it could still be indistinguishable from | |
492 | * them */ | |
493 | if (! PL_numeric_standard) { | |
494 | PL_numeric_standard = cBOOL(strEQ(".", my_nl_langinfo(PERL_RADIXCHAR, | |
495 | FALSE /* Don't toggle locale */ )) | |
496 | && strEQ("", my_nl_langinfo(PERL_THOUSEP, | |
497 | FALSE))); | |
498 | } | |
abe1abcf | 499 | |
4c68b815 | 500 | /* Save the new name if it isn't the same as the previous one, if any */ |
b03f34cf | 501 | if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) { |
98994639 | 502 | Safefree(PL_numeric_name); |
b03f34cf | 503 | PL_numeric_name = save_newnum; |
b03f34cf | 504 | } |
abe1abcf KW |
505 | else { |
506 | Safefree(save_newnum); | |
507 | } | |
4c28b29c | 508 | |
4c68b815 KW |
509 | PL_numeric_underlying_is_standard = PL_numeric_standard; |
510 | ||
7e5377f7 | 511 | # ifdef HAS_POSIX_2008_LOCALE |
e1aa2579 KW |
512 | |
513 | PL_underlying_numeric_obj = newlocale(LC_NUMERIC_MASK, | |
514 | PL_numeric_name, | |
515 | PL_underlying_numeric_obj); | |
516 | ||
517 | #endif | |
518 | ||
4c68b815 KW |
519 | if (DEBUG_L_TEST || debug_initialization) { |
520 | PerlIO_printf(Perl_debug_log, "Called new_numeric with %s, PL_numeric_name=%s\n", newnum, PL_numeric_name); | |
521 | } | |
522 | ||
4c28b29c KW |
523 | /* Keep LC_NUMERIC in the C locale. This is for XS modules, so they don't |
524 | * have to worry about the radix being a non-dot. (Core operations that | |
525 | * need the underlying locale change to it temporarily). */ | |
526 | set_numeric_standard(); | |
527 | ||
98994639 | 528 | #endif /* USE_LOCALE_NUMERIC */ |
7d4bcc4a | 529 | |
98994639 HS |
530 | } |
531 | ||
532 | void | |
533 | Perl_set_numeric_standard(pTHX) | |
534 | { | |
7d4bcc4a | 535 | |
98994639 | 536 | #ifdef USE_LOCALE_NUMERIC |
7d4bcc4a | 537 | |
28c1bf33 KW |
538 | /* Toggle the LC_NUMERIC locale to C. Most code should use the macros like |
539 | * SET_NUMERIC_STANDARD() in perl.h instead of calling this directly. The | |
540 | * macro avoids calling this routine if toggling isn't necessary according | |
541 | * to our records (which could be wrong if some XS code has changed the | |
542 | * locale behind our back) */ | |
0d071d52 | 543 | |
837ce802 | 544 | do_setlocale_c(LC_NUMERIC, "C"); |
a9b8c0d8 | 545 | PL_numeric_standard = TRUE; |
4c68b815 | 546 | PL_numeric_underlying = PL_numeric_underlying_is_standard; |
86799d2d | 547 | set_numeric_radix(0); |
7d4bcc4a KW |
548 | |
549 | # ifdef DEBUGGING | |
550 | ||
2fcc0ca9 KW |
551 | if (DEBUG_L_TEST || debug_initialization) { |
552 | PerlIO_printf(Perl_debug_log, | |
58e4a467 | 553 | "LC_NUMERIC locale now is standard C\n"); |
2fcc0ca9 | 554 | } |
98994639 | 555 | |
7d4bcc4a | 556 | # endif |
98994639 | 557 | #endif /* USE_LOCALE_NUMERIC */ |
7d4bcc4a | 558 | |
98994639 HS |
559 | } |
560 | ||
561 | void | |
5792c642 | 562 | Perl_set_numeric_underlying(pTHX) |
98994639 | 563 | { |
7d4bcc4a | 564 | |
98994639 | 565 | #ifdef USE_LOCALE_NUMERIC |
7d4bcc4a | 566 | |
28c1bf33 | 567 | /* Toggle the LC_NUMERIC locale to the current underlying default. Most |
7d4bcc4a KW |
568 | * code should use the macros like SET_NUMERIC_UNDERLYING() in perl.h |
569 | * instead of calling this directly. The macro avoids calling this routine | |
570 | * if toggling isn't necessary according to our records (which could be | |
571 | * wrong if some XS code has changed the locale behind our back) */ | |
a9b8c0d8 | 572 | |
837ce802 | 573 | do_setlocale_c(LC_NUMERIC, PL_numeric_name); |
4c68b815 | 574 | PL_numeric_standard = PL_numeric_underlying_is_standard; |
892e6465 | 575 | PL_numeric_underlying = TRUE; |
3c62fd46 | 576 | set_numeric_radix(! PL_numeric_standard); |
7d4bcc4a KW |
577 | |
578 | # ifdef DEBUGGING | |
579 | ||
2fcc0ca9 KW |
580 | if (DEBUG_L_TEST || debug_initialization) { |
581 | PerlIO_printf(Perl_debug_log, | |
58e4a467 | 582 | "LC_NUMERIC locale now is %s\n", |
2fcc0ca9 KW |
583 | PL_numeric_name); |
584 | } | |
98994639 | 585 | |
7d4bcc4a | 586 | # endif |
98994639 | 587 | #endif /* USE_LOCALE_NUMERIC */ |
7d4bcc4a | 588 | |
98994639 HS |
589 | } |
590 | ||
591 | /* | |
592 | * Set up for a new ctype locale. | |
593 | */ | |
a4f00dcc KW |
594 | STATIC void |
595 | S_new_ctype(pTHX_ const char *newctype) | |
98994639 | 596 | { |
7d4bcc4a KW |
597 | |
598 | #ifndef USE_LOCALE_CTYPE | |
599 | ||
600 | PERL_ARGS_ASSERT_NEW_CTYPE; | |
601 | PERL_UNUSED_ARG(newctype); | |
602 | PERL_UNUSED_CONTEXT; | |
603 | ||
604 | #else | |
0d071d52 | 605 | |
291a84fb | 606 | /* Called after each libc setlocale() call affecting LC_CTYPE, to tell |
0d071d52 KW |
607 | * core Perl this and that 'newctype' is the name of the new locale. |
608 | * | |
609 | * This function sets up the folding arrays for all 256 bytes, assuming | |
610 | * that tofold() is tolc() since fold case is not a concept in POSIX, | |
611 | * | |
612 | * Any code changing the locale (outside this file) should use | |
9aac5db8 KW |
613 | * Perl_setlocale or POSIX::setlocale, which call this function. Therefore |
614 | * this function should be called directly only from this file and from | |
0d071d52 KW |
615 | * POSIX::setlocale() */ |
616 | ||
27da23d5 | 617 | dVAR; |
68067e4e | 618 | UV i; |
98994639 | 619 | |
8b7358b9 KW |
620 | /* Don't check for problems if we are suppressing the warnings */ |
621 | bool check_for_problems = ckWARN_d(WARN_LOCALE) || UNLIKELY(DEBUG_L_TEST); | |
622 | ||
7918f24d NC |
623 | PERL_ARGS_ASSERT_NEW_CTYPE; |
624 | ||
215c5139 KW |
625 | /* We will replace any bad locale warning with 1) nothing if the new one is |
626 | * ok; or 2) a new warning for the bad new locale */ | |
627 | if (PL_warn_locale) { | |
628 | SvREFCNT_dec_NN(PL_warn_locale); | |
629 | PL_warn_locale = NULL; | |
630 | } | |
631 | ||
c1284011 | 632 | PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE); |
31f05a37 KW |
633 | |
634 | /* A UTF-8 locale gets standard rules. But note that code still has to | |
635 | * handle this specially because of the three problematic code points */ | |
636 | if (PL_in_utf8_CTYPE_locale) { | |
637 | Copy(PL_fold_latin1, PL_fold_locale, 256, U8); | |
638 | } | |
8b7358b9 KW |
639 | |
640 | /* We don't populate the other lists if a UTF-8 locale, but do check that | |
641 | * everything works as expected, unless checking turned off */ | |
642 | if (check_for_problems || ! PL_in_utf8_CTYPE_locale) { | |
8c6180a9 KW |
643 | /* Assume enough space for every character being bad. 4 spaces each |
644 | * for the 94 printable characters that are output like "'x' "; and 5 | |
645 | * spaces each for "'\\' ", "'\t' ", and "'\n' "; plus a terminating | |
646 | * NUL */ | |
8b7358b9 | 647 | char bad_chars_list[ (94 * 4) + (3 * 5) + 1 ] = { '\0' }; |
8c6180a9 KW |
648 | bool multi_byte_locale = FALSE; /* Assume is a single-byte locale |
649 | to start */ | |
650 | unsigned int bad_count = 0; /* Count of bad characters */ | |
651 | ||
baa60164 | 652 | for (i = 0; i < 256; i++) { |
8b7358b9 | 653 | if (! PL_in_utf8_CTYPE_locale) { |
bd6d0898 KW |
654 | if (isupper(i)) |
655 | PL_fold_locale[i] = (U8) tolower(i); | |
656 | else if (islower(i)) | |
657 | PL_fold_locale[i] = (U8) toupper(i); | |
658 | else | |
659 | PL_fold_locale[i] = (U8) i; | |
8b7358b9 | 660 | } |
8c6180a9 KW |
661 | |
662 | /* If checking for locale problems, see if the native ASCII-range | |
663 | * printables plus \n and \t are in their expected categories in | |
664 | * the new locale. If not, this could mean big trouble, upending | |
665 | * Perl's and most programs' assumptions, like having a | |
666 | * metacharacter with special meaning become a \w. Fortunately, | |
667 | * it's very rare to find locales that aren't supersets of ASCII | |
668 | * nowadays. It isn't a problem for most controls to be changed | |
669 | * into something else; we check only \n and \t, though perhaps \r | |
670 | * could be an issue as well. */ | |
7d4bcc4a | 671 | if ( check_for_problems |
8c6180a9 KW |
672 | && (isGRAPH_A(i) || isBLANK_A(i) || i == '\n')) |
673 | { | |
8b7358b9 KW |
674 | bool is_bad = FALSE; |
675 | char name[3] = { '\0' }; | |
676 | ||
677 | /* Convert the name into a string */ | |
678 | if (isPRINT_A(i)) { | |
679 | name[0] = i; | |
680 | name[1] = '\0'; | |
681 | } | |
682 | else if (i == '\n') { | |
683 | my_strlcpy(name, "\n", sizeof(name)); | |
684 | } | |
685 | else { | |
686 | my_strlcpy(name, "\t", sizeof(name)); | |
687 | } | |
688 | ||
689 | /* Check each possibe class */ | |
690 | if (UNLIKELY(cBOOL(isalnum(i)) != cBOOL(isALPHANUMERIC_A(i)))) { | |
691 | is_bad = TRUE; | |
692 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
693 | "isalnum('%s') unexpectedly is %d\n", | |
694 | name, cBOOL(isalnum(i)))); | |
695 | } | |
696 | if (UNLIKELY(cBOOL(isalpha(i)) != cBOOL(isALPHA_A(i)))) { | |
697 | is_bad = TRUE; | |
698 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
699 | "isalpha('%s') unexpectedly is %d\n", | |
700 | name, cBOOL(isalpha(i)))); | |
701 | } | |
702 | if (UNLIKELY(cBOOL(isdigit(i)) != cBOOL(isDIGIT_A(i)))) { | |
703 | is_bad = TRUE; | |
704 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
705 | "isdigit('%s') unexpectedly is %d\n", | |
706 | name, cBOOL(isdigit(i)))); | |
707 | } | |
708 | if (UNLIKELY(cBOOL(isgraph(i)) != cBOOL(isGRAPH_A(i)))) { | |
709 | is_bad = TRUE; | |
710 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
711 | "isgraph('%s') unexpectedly is %d\n", | |
712 | name, cBOOL(isgraph(i)))); | |
713 | } | |
714 | if (UNLIKELY(cBOOL(islower(i)) != cBOOL(isLOWER_A(i)))) { | |
715 | is_bad = TRUE; | |
716 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
717 | "islower('%s') unexpectedly is %d\n", | |
718 | name, cBOOL(islower(i)))); | |
719 | } | |
720 | if (UNLIKELY(cBOOL(isprint(i)) != cBOOL(isPRINT_A(i)))) { | |
721 | is_bad = TRUE; | |
722 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
723 | "isprint('%s') unexpectedly is %d\n", | |
724 | name, cBOOL(isprint(i)))); | |
725 | } | |
726 | if (UNLIKELY(cBOOL(ispunct(i)) != cBOOL(isPUNCT_A(i)))) { | |
727 | is_bad = TRUE; | |
728 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
729 | "ispunct('%s') unexpectedly is %d\n", | |
730 | name, cBOOL(ispunct(i)))); | |
731 | } | |
732 | if (UNLIKELY(cBOOL(isspace(i)) != cBOOL(isSPACE_A(i)))) { | |
733 | is_bad = TRUE; | |
734 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
735 | "isspace('%s') unexpectedly is %d\n", | |
736 | name, cBOOL(isspace(i)))); | |
737 | } | |
738 | if (UNLIKELY(cBOOL(isupper(i)) != cBOOL(isUPPER_A(i)))) { | |
739 | is_bad = TRUE; | |
740 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
741 | "isupper('%s') unexpectedly is %d\n", | |
742 | name, cBOOL(isupper(i)))); | |
743 | } | |
744 | if (UNLIKELY(cBOOL(isxdigit(i))!= cBOOL(isXDIGIT_A(i)))) { | |
745 | is_bad = TRUE; | |
746 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
747 | "isxdigit('%s') unexpectedly is %d\n", | |
748 | name, cBOOL(isxdigit(i)))); | |
749 | } | |
750 | if (UNLIKELY(tolower(i) != (int) toLOWER_A(i))) { | |
751 | is_bad = TRUE; | |
752 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
753 | "tolower('%s')=0x%x instead of the expected 0x%x\n", | |
754 | name, tolower(i), (int) toLOWER_A(i))); | |
755 | } | |
756 | if (UNLIKELY(toupper(i) != (int) toUPPER_A(i))) { | |
757 | is_bad = TRUE; | |
758 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
759 | "toupper('%s')=0x%x instead of the expected 0x%x\n", | |
760 | name, toupper(i), (int) toUPPER_A(i))); | |
761 | } | |
762 | if (UNLIKELY((i == '\n' && ! isCNTRL_LC(i)))) { | |
763 | is_bad = TRUE; | |
764 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
765 | "'\\n' (=%02X) is not a control\n", (int) i)); | |
766 | } | |
767 | ||
768 | /* Add to the list; Separate multiple entries with a blank */ | |
769 | if (is_bad) { | |
770 | if (bad_count) { | |
771 | my_strlcat(bad_chars_list, " ", sizeof(bad_chars_list)); | |
8c6180a9 | 772 | } |
8b7358b9 KW |
773 | my_strlcat(bad_chars_list, name, sizeof(bad_chars_list)); |
774 | bad_count++; | |
8c6180a9 KW |
775 | } |
776 | } | |
777 | } | |
778 | ||
7d4bcc4a KW |
779 | # ifdef MB_CUR_MAX |
780 | ||
8c6180a9 | 781 | /* We only handle single-byte locales (outside of UTF-8 ones; so if |
d35fca5f | 782 | * this locale requires more than one byte, there are going to be |
8c6180a9 | 783 | * problems. */ |
9c8a6dc2 KW |
784 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
785 | "%s:%d: check_for_problems=%d, MB_CUR_MAX=%d\n", | |
786 | __FILE__, __LINE__, check_for_problems, (int) MB_CUR_MAX)); | |
787 | ||
8b7358b9 KW |
788 | if ( check_for_problems && MB_CUR_MAX > 1 |
789 | && ! PL_in_utf8_CTYPE_locale | |
ba1a4362 KW |
790 | |
791 | /* Some platforms return MB_CUR_MAX > 1 for even the "C" | |
792 | * locale. Just assume that the implementation for them (plus | |
793 | * for POSIX) is correct and the > 1 value is spurious. (Since | |
794 | * these are specially handled to never be considered UTF-8 | |
795 | * locales, as long as this is the only problem, everything | |
796 | * should work fine */ | |
797 | && strNE(newctype, "C") && strNE(newctype, "POSIX")) | |
798 | { | |
8c6180a9 KW |
799 | multi_byte_locale = TRUE; |
800 | } | |
7d4bcc4a KW |
801 | |
802 | # endif | |
8c6180a9 | 803 | |
8b7358b9 KW |
804 | if (UNLIKELY(bad_count) || UNLIKELY(multi_byte_locale)) { |
805 | if (UNLIKELY(bad_count) && PL_in_utf8_CTYPE_locale) { | |
806 | PL_warn_locale = Perl_newSVpvf(aTHX_ | |
807 | "Locale '%s' contains (at least) the following characters" | |
808 | " which have\nnon-standard meanings: %s\nThe Perl program" | |
809 | " will use the standard meanings", | |
810 | newctype, bad_chars_list); | |
811 | ||
812 | } | |
813 | else { | |
814 | PL_warn_locale = Perl_newSVpvf(aTHX_ | |
8c6180a9 | 815 | "Locale '%s' may not work well.%s%s%s\n", |
780fcc9f | 816 | newctype, |
8c6180a9 KW |
817 | (multi_byte_locale) |
818 | ? " Some characters in it are not recognized by" | |
819 | " Perl." | |
820 | : "", | |
821 | (bad_count) | |
822 | ? "\nThe following characters (and maybe others)" | |
823 | " may not have the same meaning as the Perl" | |
824 | " program expects:\n" | |
825 | : "", | |
826 | (bad_count) | |
827 | ? bad_chars_list | |
828 | : "" | |
829 | ); | |
8b7358b9 KW |
830 | } |
831 | ||
b119c2be KW |
832 | # ifdef HAS_NL_LANGINFO |
833 | ||
834 | Perl_sv_catpvf(aTHX_ PL_warn_locale, "; codeset=%s", | |
835 | /* parameter FALSE is a don't care here */ | |
836 | my_nl_langinfo(PERL_CODESET, FALSE)); | |
837 | ||
838 | # endif | |
839 | ||
8b7358b9 KW |
840 | Perl_sv_catpvf(aTHX_ PL_warn_locale, "\n"); |
841 | ||
cc9eaeb0 | 842 | /* If we are actually in the scope of the locale or are debugging, |
bddebb56 KW |
843 | * output the message now. If not in that scope, we save the |
844 | * message to be output at the first operation using this locale, | |
845 | * if that actually happens. Most programs don't use locales, so | |
846 | * they are immune to bad ones. */ | |
cc9eaeb0 | 847 | if (IN_LC(LC_CTYPE) || UNLIKELY(DEBUG_L_TEST)) { |
780fcc9f | 848 | |
780fcc9f KW |
849 | /* The '0' below suppresses a bogus gcc compiler warning */ |
850 | Perl_warner(aTHX_ packWARN(WARN_LOCALE), SvPVX(PL_warn_locale), 0); | |
bddebb56 | 851 | |
bddebb56 KW |
852 | if (IN_LC(LC_CTYPE)) { |
853 | SvREFCNT_dec_NN(PL_warn_locale); | |
854 | PL_warn_locale = NULL; | |
855 | } | |
780fcc9f | 856 | } |
baa60164 | 857 | } |
31f05a37 | 858 | } |
98994639 HS |
859 | |
860 | #endif /* USE_LOCALE_CTYPE */ | |
7d4bcc4a | 861 | |
98994639 HS |
862 | } |
863 | ||
98994639 | 864 | void |
2726666d KW |
865 | Perl__warn_problematic_locale() |
866 | { | |
2726666d KW |
867 | |
868 | #ifdef USE_LOCALE_CTYPE | |
869 | ||
5f04a188 KW |
870 | dTHX; |
871 | ||
872 | /* Internal-to-core function that outputs the message in PL_warn_locale, | |
873 | * and then NULLS it. Should be called only through the macro | |
874 | * _CHECK_AND_WARN_PROBLEMATIC_LOCALE */ | |
875 | ||
2726666d | 876 | if (PL_warn_locale) { |
2726666d KW |
877 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), |
878 | SvPVX(PL_warn_locale), | |
879 | 0 /* dummy to avoid compiler warning */ ); | |
2726666d KW |
880 | SvREFCNT_dec_NN(PL_warn_locale); |
881 | PL_warn_locale = NULL; | |
882 | } | |
883 | ||
884 | #endif | |
885 | ||
886 | } | |
887 | ||
a4f00dcc KW |
888 | STATIC void |
889 | S_new_collate(pTHX_ const char *newcoll) | |
98994639 | 890 | { |
7d4bcc4a KW |
891 | |
892 | #ifndef USE_LOCALE_COLLATE | |
893 | ||
894 | PERL_UNUSED_ARG(newcoll); | |
895 | PERL_UNUSED_CONTEXT; | |
896 | ||
897 | #else | |
0d071d52 | 898 | |
291a84fb | 899 | /* Called after each libc setlocale() call affecting LC_COLLATE, to tell |
0d071d52 KW |
900 | * core Perl this and that 'newcoll' is the name of the new locale. |
901 | * | |
d35fca5f KW |
902 | * The design of locale collation is that every locale change is given an |
903 | * index 'PL_collation_ix'. The first time a string particpates in an | |
904 | * operation that requires collation while locale collation is active, it | |
905 | * is given PERL_MAGIC_collxfrm magic (via sv_collxfrm_flags()). That | |
906 | * magic includes the collation index, and the transformation of the string | |
907 | * by strxfrm(), q.v. That transformation is used when doing comparisons, | |
908 | * instead of the string itself. If a string changes, the magic is | |
909 | * cleared. The next time the locale changes, the index is incremented, | |
910 | * and so we know during a comparison that the transformation is not | |
911 | * necessarily still valid, and so is recomputed. Note that if the locale | |
912 | * changes enough times, the index could wrap (a U32), and it is possible | |
913 | * that a transformation would improperly be considered valid, leading to | |
914 | * an unlikely bug */ | |
0d071d52 | 915 | |
98994639 HS |
916 | if (! newcoll) { |
917 | if (PL_collation_name) { | |
918 | ++PL_collation_ix; | |
919 | Safefree(PL_collation_name); | |
920 | PL_collation_name = NULL; | |
921 | } | |
922 | PL_collation_standard = TRUE; | |
00bf60ca | 923 | is_standard_collation: |
98994639 HS |
924 | PL_collxfrm_base = 0; |
925 | PL_collxfrm_mult = 2; | |
165a1c52 | 926 | PL_in_utf8_COLLATE_locale = FALSE; |
f28f4d2a | 927 | PL_strxfrm_NUL_replacement = '\0'; |
a4a439fb | 928 | PL_strxfrm_max_cp = 0; |
98994639 HS |
929 | return; |
930 | } | |
931 | ||
d35fca5f | 932 | /* If this is not the same locale as currently, set the new one up */ |
98994639 HS |
933 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
934 | ++PL_collation_ix; | |
935 | Safefree(PL_collation_name); | |
936 | PL_collation_name = stdize_locale(savepv(newcoll)); | |
a39edc4c | 937 | PL_collation_standard = isNAME_C_OR_POSIX(newcoll); |
00bf60ca KW |
938 | if (PL_collation_standard) { |
939 | goto is_standard_collation; | |
940 | } | |
98994639 | 941 | |
165a1c52 | 942 | PL_in_utf8_COLLATE_locale = _is_cur_LC_category_utf8(LC_COLLATE); |
f28f4d2a | 943 | PL_strxfrm_NUL_replacement = '\0'; |
a4a439fb | 944 | PL_strxfrm_max_cp = 0; |
165a1c52 | 945 | |
59c018b9 KW |
946 | /* A locale collation definition includes primary, secondary, tertiary, |
947 | * etc. weights for each character. To sort, the primary weights are | |
948 | * used, and only if they compare equal, then the secondary weights are | |
949 | * used, and only if they compare equal, then the tertiary, etc. | |
950 | * | |
951 | * strxfrm() works by taking the input string, say ABC, and creating an | |
952 | * output transformed string consisting of first the primary weights, | |
953 | * A¹B¹C¹ followed by the secondary ones, A²B²C²; and then the | |
954 | * tertiary, etc, yielding A¹B¹C¹ A²B²C² A³B³C³ .... Some characters | |
955 | * may not have weights at every level. In our example, let's say B | |
956 | * doesn't have a tertiary weight, and A doesn't have a secondary | |
957 | * weight. The constructed string is then going to be | |
958 | * A¹B¹C¹ B²C² A³C³ .... | |
959 | * This has the desired effect that strcmp() will look at the secondary | |
960 | * or tertiary weights only if the strings compare equal at all higher | |
961 | * priority weights. The spaces shown here, like in | |
c342d20e | 962 | * "A¹B¹C¹ A²B²C² " |
59c018b9 KW |
963 | * are not just for readability. In the general case, these must |
964 | * actually be bytes, which we will call here 'separator weights'; and | |
965 | * they must be smaller than any other weight value, but since these | |
966 | * are C strings, only the terminating one can be a NUL (some | |
967 | * implementations may include a non-NUL separator weight just before | |
968 | * the NUL). Implementations tend to reserve 01 for the separator | |
969 | * weights. They are needed so that a shorter string's secondary | |
970 | * weights won't be misconstrued as primary weights of a longer string, | |
971 | * etc. By making them smaller than any other weight, the shorter | |
972 | * string will sort first. (Actually, if all secondary weights are | |
973 | * smaller than all primary ones, there is no need for a separator | |
974 | * weight between those two levels, etc.) | |
975 | * | |
976 | * The length of the transformed string is roughly a linear function of | |
977 | * the input string. It's not exactly linear because some characters | |
978 | * don't have weights at all levels. When we call strxfrm() we have to | |
979 | * allocate some memory to hold the transformed string. The | |
980 | * calculations below try to find coefficients 'm' and 'b' for this | |
981 | * locale so that m*x + b equals how much space we need, given the size | |
982 | * of the input string in 'x'. If we calculate too small, we increase | |
983 | * the size as needed, and call strxfrm() again, but it is better to | |
984 | * get it right the first time to avoid wasted expensive string | |
985 | * transformations. */ | |
986 | ||
98994639 | 987 | { |
79f120c8 KW |
988 | /* We use the string below to find how long the tranformation of it |
989 | * is. Almost all locales are supersets of ASCII, or at least the | |
990 | * ASCII letters. We use all of them, half upper half lower, | |
991 | * because if we used fewer, we might hit just the ones that are | |
992 | * outliers in a particular locale. Most of the strings being | |
993 | * collated will contain a preponderance of letters, and even if | |
994 | * they are above-ASCII, they are likely to have the same number of | |
995 | * weight levels as the ASCII ones. It turns out that digits tend | |
996 | * to have fewer levels, and some punctuation has more, but those | |
997 | * are relatively sparse in text, and khw believes this gives a | |
998 | * reasonable result, but it could be changed if experience so | |
999 | * dictates. */ | |
1000 | const char longer[] = "ABCDEFGHIJKLMnopqrstuvwxyz"; | |
1001 | char * x_longer; /* Transformed 'longer' */ | |
1002 | Size_t x_len_longer; /* Length of 'x_longer' */ | |
1003 | ||
1004 | char * x_shorter; /* We also transform a substring of 'longer' */ | |
1005 | Size_t x_len_shorter; | |
1006 | ||
a4a439fb | 1007 | /* _mem_collxfrm() is used get the transformation (though here we |
79f120c8 KW |
1008 | * are interested only in its length). It is used because it has |
1009 | * the intelligence to handle all cases, but to work, it needs some | |
1010 | * values of 'm' and 'b' to get it started. For the purposes of | |
1011 | * this calculation we use a very conservative estimate of 'm' and | |
1012 | * 'b'. This assumes a weight can be multiple bytes, enough to | |
1013 | * hold any UV on the platform, and there are 5 levels, 4 weight | |
1014 | * bytes, and a trailing NUL. */ | |
1015 | PL_collxfrm_base = 5; | |
1016 | PL_collxfrm_mult = 5 * sizeof(UV); | |
1017 | ||
1018 | /* Find out how long the transformation really is */ | |
a4a439fb KW |
1019 | x_longer = _mem_collxfrm(longer, |
1020 | sizeof(longer) - 1, | |
1021 | &x_len_longer, | |
1022 | ||
1023 | /* We avoid converting to UTF-8 in the | |
1024 | * called function by telling it the | |
1025 | * string is in UTF-8 if the locale is a | |
1026 | * UTF-8 one. Since the string passed | |
1027 | * here is invariant under UTF-8, we can | |
1028 | * claim it's UTF-8 even though it isn't. | |
1029 | * */ | |
1030 | PL_in_utf8_COLLATE_locale); | |
79f120c8 KW |
1031 | Safefree(x_longer); |
1032 | ||
1033 | /* Find out how long the transformation of a substring of 'longer' | |
1034 | * is. Together the lengths of these transformations are | |
1035 | * sufficient to calculate 'm' and 'b'. The substring is all of | |
1036 | * 'longer' except the first character. This minimizes the chances | |
1037 | * of being swayed by outliers */ | |
a4a439fb | 1038 | x_shorter = _mem_collxfrm(longer + 1, |
79f120c8 | 1039 | sizeof(longer) - 2, |
a4a439fb KW |
1040 | &x_len_shorter, |
1041 | PL_in_utf8_COLLATE_locale); | |
79f120c8 KW |
1042 | Safefree(x_shorter); |
1043 | ||
1044 | /* If the results are nonsensical for this simple test, the whole | |
1045 | * locale definition is suspect. Mark it so that locale collation | |
1046 | * is not active at all for it. XXX Should we warn? */ | |
1047 | if ( x_len_shorter == 0 | |
1048 | || x_len_longer == 0 | |
1049 | || x_len_shorter >= x_len_longer) | |
1050 | { | |
1051 | PL_collxfrm_mult = 0; | |
1052 | PL_collxfrm_base = 0; | |
1053 | } | |
1054 | else { | |
1055 | SSize_t base; /* Temporary */ | |
1056 | ||
1057 | /* We have both: m * strlen(longer) + b = x_len_longer | |
1058 | * m * strlen(shorter) + b = x_len_shorter; | |
1059 | * subtracting yields: | |
1060 | * m * (strlen(longer) - strlen(shorter)) | |
1061 | * = x_len_longer - x_len_shorter | |
1062 | * But we have set things up so that 'shorter' is 1 byte smaller | |
1063 | * than 'longer'. Hence: | |
1064 | * m = x_len_longer - x_len_shorter | |
1065 | * | |
1066 | * But if something went wrong, make sure the multiplier is at | |
1067 | * least 1. | |
1068 | */ | |
1069 | if (x_len_longer > x_len_shorter) { | |
1070 | PL_collxfrm_mult = (STRLEN) x_len_longer - x_len_shorter; | |
1071 | } | |
1072 | else { | |
1073 | PL_collxfrm_mult = 1; | |
1074 | } | |
1075 | ||
1076 | /* mx + b = len | |
1077 | * so: b = len - mx | |
1078 | * but in case something has gone wrong, make sure it is | |
1079 | * non-negative */ | |
1080 | base = x_len_longer - PL_collxfrm_mult * (sizeof(longer) - 1); | |
1081 | if (base < 0) { | |
1082 | base = 0; | |
1083 | } | |
1084 | ||
1085 | /* Add 1 for the trailing NUL */ | |
1086 | PL_collxfrm_base = base + 1; | |
1087 | } | |
58eebef2 | 1088 | |
7d4bcc4a KW |
1089 | # ifdef DEBUGGING |
1090 | ||
58eebef2 KW |
1091 | if (DEBUG_L_TEST || debug_initialization) { |
1092 | PerlIO_printf(Perl_debug_log, | |
b07929e4 KW |
1093 | "%s:%d: ?UTF-8 locale=%d; x_len_shorter=%zu, " |
1094 | "x_len_longer=%zu," | |
1095 | " collate multipler=%zu, collate base=%zu\n", | |
58eebef2 KW |
1096 | __FILE__, __LINE__, |
1097 | PL_in_utf8_COLLATE_locale, | |
1098 | x_len_shorter, x_len_longer, | |
1099 | PL_collxfrm_mult, PL_collxfrm_base); | |
1100 | } | |
7d4bcc4a KW |
1101 | # endif |
1102 | ||
98994639 HS |
1103 | } |
1104 | } | |
1105 | ||
1106 | #endif /* USE_LOCALE_COLLATE */ | |
7d4bcc4a | 1107 | |
98994639 HS |
1108 | } |
1109 | ||
d2b24094 | 1110 | #ifdef WIN32 |
b8cc575c | 1111 | |
a4f00dcc | 1112 | STATIC char * |
b8cc575c | 1113 | S_win32_setlocale(pTHX_ int category, const char* locale) |
b385bb4d KW |
1114 | { |
1115 | /* This, for Windows, emulates POSIX setlocale() behavior. There is no | |
7d4bcc4a KW |
1116 | * difference between the two unless the input locale is "", which normally |
1117 | * means on Windows to get the machine default, which is set via the | |
1118 | * computer's "Regional and Language Options" (or its current equivalent). | |
1119 | * In POSIX, it instead means to find the locale from the user's | |
1120 | * environment. This routine changes the Windows behavior to first look in | |
1121 | * the environment, and, if anything is found, use that instead of going to | |
1122 | * the machine default. If there is no environment override, the machine | |
1123 | * default is used, by calling the real setlocale() with "". | |
1124 | * | |
1125 | * The POSIX behavior is to use the LC_ALL variable if set; otherwise to | |
1126 | * use the particular category's variable if set; otherwise to use the LANG | |
1127 | * variable. */ | |
b385bb4d | 1128 | |
175c4cf9 | 1129 | bool override_LC_ALL = FALSE; |
89f7b9aa | 1130 | char * result; |
e5f10d49 | 1131 | unsigned int i; |
89f7b9aa | 1132 | |
b385bb4d | 1133 | if (locale && strEQ(locale, "")) { |
7d4bcc4a KW |
1134 | |
1135 | # ifdef LC_ALL | |
1136 | ||
b385bb4d KW |
1137 | locale = PerlEnv_getenv("LC_ALL"); |
1138 | if (! locale) { | |
e5f10d49 KW |
1139 | if (category == LC_ALL) { |
1140 | override_LC_ALL = TRUE; | |
1141 | } | |
1142 | else { | |
7d4bcc4a KW |
1143 | |
1144 | # endif | |
7d4bcc4a | 1145 | |
e5f10d49 KW |
1146 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) { |
1147 | if (category == categories[i]) { | |
1148 | locale = PerlEnv_getenv(category_names[i]); | |
1149 | goto found_locale; | |
1150 | } | |
1151 | } | |
7d4bcc4a | 1152 | |
b385bb4d | 1153 | locale = PerlEnv_getenv("LANG"); |
481465ea | 1154 | if (! locale) { |
b385bb4d KW |
1155 | locale = ""; |
1156 | } | |
e5f10d49 KW |
1157 | |
1158 | found_locale: ; | |
7d4bcc4a KW |
1159 | |
1160 | # ifdef LC_ALL | |
1161 | ||
e5f10d49 | 1162 | } |
b385bb4d | 1163 | } |
7d4bcc4a KW |
1164 | |
1165 | # endif | |
1166 | ||
b385bb4d KW |
1167 | } |
1168 | ||
89f7b9aa | 1169 | result = setlocale(category, locale); |
48015184 KW |
1170 | DEBUG_L(STMT_START { |
1171 | dSAVE_ERRNO; | |
1172 | PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", __FILE__, __LINE__, | |
1173 | setlocale_debug_string(category, locale, result)); | |
1174 | RESTORE_ERRNO; | |
1175 | } STMT_END); | |
89f7b9aa | 1176 | |
481465ea | 1177 | if (! override_LC_ALL) { |
89f7b9aa KW |
1178 | return result; |
1179 | } | |
1180 | ||
dfd77d7a | 1181 | /* Here the input category was LC_ALL, and we have set it to what is in the |
481465ea KW |
1182 | * LANG variable or the system default if there is no LANG. But these have |
1183 | * lower priority than the other LC_foo variables, so override it for each | |
1184 | * one that is set. (If they are set to "", it means to use the same thing | |
1185 | * we just set LC_ALL to, so can skip) */ | |
7d4bcc4a | 1186 | |
948523db | 1187 | for (i = 0; i < LC_ALL_INDEX; i++) { |
e5f10d49 KW |
1188 | result = PerlEnv_getenv(category_names[i]); |
1189 | if (result && strNE(result, "")) { | |
1190 | setlocale(categories[i], result); | |
1191 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", | |
1192 | __FILE__, __LINE__, | |
1193 | setlocale_debug_string(categories[i], result, "not captured"))); | |
1194 | } | |
89f7b9aa | 1195 | } |
7d4bcc4a | 1196 | |
bbc98134 | 1197 | result = setlocale(LC_ALL, NULL); |
48015184 KW |
1198 | DEBUG_L(STMT_START { |
1199 | dSAVE_ERRNO; | |
1200 | PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", | |
bbc98134 | 1201 | __FILE__, __LINE__, |
48015184 KW |
1202 | setlocale_debug_string(LC_ALL, NULL, result)); |
1203 | RESTORE_ERRNO; | |
1204 | } STMT_END); | |
89f7b9aa | 1205 | |
bbc98134 | 1206 | return result; |
b385bb4d KW |
1207 | } |
1208 | ||
1209 | #endif | |
1210 | ||
9aac5db8 KW |
1211 | /* |
1212 | ||
1213 | =head1 Locale-related functions and macros | |
1214 | ||
1215 | =for apidoc Perl_setlocale | |
1216 | ||
1217 | This is an (almost) drop-in replacement for the system L<C<setlocale(3)>>, | |
1218 | taking the same parameters, and returning the same information, except that it | |
1219 | returns the correct underlying C<LC_NUMERIC> locale, instead of C<C> always, as | |
1220 | perl keeps that locale category as C<C>, changing it briefly during the | |
1221 | operations where the underlying one is required. | |
1222 | ||
1223 | The other reason it isn't completely a drop-in replacement is that it is | |
1224 | declared to return S<C<const char *>>, whereas the system setlocale omits the | |
1225 | C<const>. (If it were being written today, plain setlocale would be declared | |
1226 | const, since it is illegal to change the information it returns; doing so leads | |
1227 | to segfaults.) | |
1228 | ||
1229 | C<Perl_setlocale> should not be used to change the locale except on systems | |
1230 | where the predefined variable C<${^SAFE_LOCALES}> is 1. | |
1231 | ||
1232 | The return points to a per-thread static buffer, which is overwritten the next | |
1233 | time C<Perl_setlocale> is called from the same thread. | |
1234 | ||
1235 | =cut | |
1236 | ||
1237 | */ | |
1238 | ||
1239 | const char * | |
1240 | Perl_setlocale(const int category, const char * locale) | |
a4f00dcc KW |
1241 | { |
1242 | /* This wraps POSIX::setlocale() */ | |
1243 | ||
9aac5db8 KW |
1244 | const char * retval; |
1245 | const char * newlocale; | |
1246 | dSAVEDERRNO; | |
1247 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; | |
a4f00dcc KW |
1248 | dTHX; |
1249 | ||
a4f00dcc KW |
1250 | #ifdef USE_LOCALE_NUMERIC |
1251 | ||
291a84fb KW |
1252 | /* A NULL locale means only query what the current one is. We have the |
1253 | * LC_NUMERIC name saved, because we are normally switched into the C | |
1254 | * locale for it. For an LC_ALL query, switch back to get the correct | |
1255 | * results. All other categories don't require special handling */ | |
a4f00dcc KW |
1256 | if (locale == NULL) { |
1257 | if (category == LC_NUMERIC) { | |
9aac5db8 KW |
1258 | |
1259 | /* We don't have to copy this return value, as it is a per-thread | |
1260 | * variable, and won't change until a future setlocale */ | |
1261 | return PL_numeric_name; | |
a4f00dcc KW |
1262 | } |
1263 | ||
7d4bcc4a | 1264 | # ifdef LC_ALL |
a4f00dcc | 1265 | |
9aac5db8 KW |
1266 | else if (category == LC_ALL) { |
1267 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING(); | |
a4f00dcc KW |
1268 | } |
1269 | ||
7d4bcc4a | 1270 | # endif |
a4f00dcc KW |
1271 | |
1272 | } | |
1273 | ||
1274 | #endif | |
1275 | ||
9aac5db8 KW |
1276 | retval = do_setlocale_r(category, locale); |
1277 | SAVE_ERRNO; | |
1278 | ||
1279 | #if defined(USE_LOCALE_NUMERIC) && defined(LC_ALL) | |
1280 | ||
1281 | if (locale == NULL && category == LC_ALL) { | |
1282 | RESTORE_LC_NUMERIC(); | |
1283 | } | |
1284 | ||
1285 | #endif | |
a4f00dcc KW |
1286 | |
1287 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1288 | "%s:%d: %s\n", __FILE__, __LINE__, | |
1289 | setlocale_debug_string(category, locale, retval))); | |
7d4bcc4a | 1290 | |
9aac5db8 KW |
1291 | RESTORE_ERRNO; |
1292 | ||
1293 | if (! retval) { | |
a4f00dcc KW |
1294 | return NULL; |
1295 | } | |
1296 | ||
9aac5db8 KW |
1297 | save_to_buffer(retval, &PL_setlocale_buf, &PL_setlocale_bufsize, 0); |
1298 | retval = PL_setlocale_buf; | |
1299 | ||
1300 | /* If locale == NULL, we are just querying the state */ | |
a4f00dcc | 1301 | if (locale == NULL) { |
a4f00dcc KW |
1302 | return retval; |
1303 | } | |
a4f00dcc | 1304 | |
1159483a KW |
1305 | /* Now that have switched locales, we have to update our records to |
1306 | * correspond. */ | |
a4f00dcc | 1307 | |
1159483a | 1308 | switch (category) { |
a4f00dcc | 1309 | |
1159483a | 1310 | #ifdef USE_LOCALE_CTYPE |
a4f00dcc | 1311 | |
1159483a KW |
1312 | case LC_CTYPE: |
1313 | new_ctype(retval); | |
1314 | break; | |
a4f00dcc | 1315 | |
1159483a | 1316 | #endif |
a4f00dcc KW |
1317 | #ifdef USE_LOCALE_COLLATE |
1318 | ||
1159483a KW |
1319 | case LC_COLLATE: |
1320 | new_collate(retval); | |
1321 | break; | |
a4f00dcc | 1322 | |
1159483a | 1323 | #endif |
a4f00dcc KW |
1324 | #ifdef USE_LOCALE_NUMERIC |
1325 | ||
1159483a KW |
1326 | case LC_NUMERIC: |
1327 | new_numeric(retval); | |
1328 | break; | |
a4f00dcc | 1329 | |
1159483a KW |
1330 | #endif |
1331 | #ifdef LC_ALL | |
a4f00dcc | 1332 | |
1159483a | 1333 | case LC_ALL: |
a4f00dcc | 1334 | |
1159483a KW |
1335 | /* LC_ALL updates all the things we care about. The values may not |
1336 | * be the same as 'retval', as the locale "" may have set things | |
1337 | * individually */ | |
a4f00dcc | 1338 | |
1159483a | 1339 | # ifdef USE_LOCALE_CTYPE |
a4f00dcc | 1340 | |
1159483a KW |
1341 | newlocale = do_setlocale_c(LC_CTYPE, NULL); |
1342 | new_ctype(newlocale); | |
a4f00dcc | 1343 | |
1159483a KW |
1344 | # endif /* USE_LOCALE_CTYPE */ |
1345 | # ifdef USE_LOCALE_COLLATE | |
1346 | ||
1347 | newlocale = do_setlocale_c(LC_COLLATE, NULL); | |
1348 | new_collate(newlocale); | |
a4f00dcc | 1349 | |
7d4bcc4a | 1350 | # endif |
1159483a | 1351 | # ifdef USE_LOCALE_NUMERIC |
a4f00dcc | 1352 | |
1159483a KW |
1353 | newlocale = do_setlocale_c(LC_NUMERIC, NULL); |
1354 | new_numeric(newlocale); | |
a4f00dcc | 1355 | |
1159483a KW |
1356 | # endif /* USE_LOCALE_NUMERIC */ |
1357 | #endif /* LC_ALL */ | |
a4f00dcc | 1358 | |
1159483a KW |
1359 | default: |
1360 | break; | |
a4f00dcc KW |
1361 | } |
1362 | ||
1363 | return retval; | |
1364 | ||
f7416781 KW |
1365 | } |
1366 | ||
1367 | PERL_STATIC_INLINE const char * | |
1368 | S_save_to_buffer(const char * string, char **buf, Size_t *buf_size, const Size_t offset) | |
1369 | { | |
1370 | /* Copy the NUL-terminated 'string' to 'buf' + 'offset'. 'buf' has size 'buf_size', | |
1371 | * growing it if necessary */ | |
1372 | ||
1373 | const Size_t string_size = strlen(string) + offset + 1; | |
1374 | ||
1375 | PERL_ARGS_ASSERT_SAVE_TO_BUFFER; | |
1376 | ||
1377 | if (*buf_size == 0) { | |
1378 | Newx(*buf, string_size, char); | |
1379 | *buf_size = string_size; | |
1380 | } | |
1381 | else if (string_size > *buf_size) { | |
1382 | Renew(*buf, string_size, char); | |
1383 | *buf_size = string_size; | |
1384 | } | |
1385 | ||
1386 | Copy(string, *buf + offset, string_size - offset, char); | |
1387 | return *buf; | |
1388 | } | |
1389 | ||
1390 | /* | |
1391 | ||
f7416781 KW |
1392 | =for apidoc Perl_langinfo |
1393 | ||
7d4bcc4a | 1394 | This is an (almost ª) drop-in replacement for the system C<L<nl_langinfo(3)>>, |
f7416781 KW |
1395 | taking the same C<item> parameter values, and returning the same information. |
1396 | But it is more thread-safe than regular C<nl_langinfo()>, and hides the quirks | |
1397 | of Perl's locale handling from your code, and can be used on systems that lack | |
1398 | a native C<nl_langinfo>. | |
1399 | ||
1400 | Expanding on these: | |
1401 | ||
1402 | =over | |
1403 | ||
1404 | =item * | |
1405 | ||
1406 | It delivers the correct results for the C<RADIXCHAR> and C<THOUSESEP> items, | |
1407 | without you having to write extra code. The reason for the extra code would be | |
1408 | because these are from the C<LC_NUMERIC> locale category, which is normally | |
1409 | kept set to the C locale by Perl, no matter what the underlying locale is | |
1410 | supposed to be, and so to get the expected results, you have to temporarily | |
1411 | toggle into the underlying locale, and later toggle back. (You could use | |
1412 | plain C<nl_langinfo> and C<L</STORE_LC_NUMERIC_FORCE_TO_UNDERLYING>> for this | |
1413 | but then you wouldn't get the other advantages of C<Perl_langinfo()>; not | |
1414 | keeping C<LC_NUMERIC> in the C locale would break a lot of CPAN, which is | |
1415 | expecting the radix (decimal point) character to be a dot.) | |
1416 | ||
1417 | =item * | |
1418 | ||
1419 | Depending on C<item>, it works on systems that don't have C<nl_langinfo>, hence | |
1420 | makes your code more portable. Of the fifty-some possible items specified by | |
1421 | the POSIX 2008 standard, | |
1422 | L<http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/langinfo.h.html>, | |
1423 | only two are completely unimplemented. It uses various techniques to recover | |
1424 | the other items, including calling C<L<localeconv(3)>>, and C<L<strftime(3)>>, | |
1425 | both of which are specified in C89, so should be always be available. Later | |
1426 | C<strftime()> versions have additional capabilities; C<""> is returned for | |
1427 | those not available on your system. | |
1428 | ||
9f0bc911 KW |
1429 | It is important to note that on such systems, this calls C<localeconv>, and so |
1430 | overwrites the static buffer returned from previous explicit calls to that | |
1431 | function. Thus, if the program doesn't use or save the information from an | |
1432 | explicit C<localeconv> call (which good practice suggests should be done | |
1433 | anyway), use of this function can break it. | |
1434 | ||
f7416781 KW |
1435 | The details for those items which may differ from what this emulation returns |
1436 | and what a native C<nl_langinfo()> would return are: | |
1437 | ||
1438 | =over | |
1439 | ||
1440 | =item C<CODESET> | |
1441 | ||
1442 | =item C<ERA> | |
1443 | ||
1444 | Unimplemented, so returns C<"">. | |
1445 | ||
1446 | =item C<YESEXPR> | |
1447 | ||
c1566110 KW |
1448 | =item C<YESSTR> |
1449 | ||
f7416781 KW |
1450 | =item C<NOEXPR> |
1451 | ||
c1566110 KW |
1452 | =item C<NOSTR> |
1453 | ||
1454 | Only the values for English are returned. C<YESSTR> and C<NOSTR> have been | |
1455 | removed from POSIX 2008, and are retained for backwards compatibility. Your | |
1456 | platform's C<nl_langinfo> may not support them. | |
f7416781 KW |
1457 | |
1458 | =item C<D_FMT> | |
1459 | ||
1460 | Always evaluates to C<%x>, the locale's appropriate date representation. | |
1461 | ||
1462 | =item C<T_FMT> | |
1463 | ||
1464 | Always evaluates to C<%X>, the locale's appropriate time representation. | |
1465 | ||
1466 | =item C<D_T_FMT> | |
1467 | ||
1468 | Always evaluates to C<%c>, the locale's appropriate date and time | |
1469 | representation. | |
1470 | ||
1471 | =item C<CRNCYSTR> | |
1472 | ||
1473 | The return may be incorrect for those rare locales where the currency symbol | |
1474 | replaces the radix character. | |
1475 | Send email to L<mailto:perlbug@perl.org> if you have examples of it needing | |
1476 | to work differently. | |
1477 | ||
1478 | =item C<ALT_DIGITS> | |
1479 | ||
1480 | Currently this gives the same results as Linux does. | |
1481 | Send email to L<mailto:perlbug@perl.org> if you have examples of it needing | |
1482 | to work differently. | |
1483 | ||
1484 | =item C<ERA_D_FMT> | |
1485 | ||
1486 | =item C<ERA_T_FMT> | |
1487 | ||
1488 | =item C<ERA_D_T_FMT> | |
1489 | ||
1490 | =item C<T_FMT_AMPM> | |
1491 | ||
1492 | These are derived by using C<strftime()>, and not all versions of that function | |
1493 | know about them. C<""> is returned for these on such systems. | |
1494 | ||
1495 | =back | |
1496 | ||
1497 | When using C<Perl_langinfo> on systems that don't have a native | |
1498 | C<nl_langinfo()>, you must | |
1499 | ||
1500 | #include "perl_langinfo.h" | |
1501 | ||
1502 | before the C<perl.h> C<#include>. You can replace your C<langinfo.h> | |
1503 | C<#include> with this one. (Doing it this way keeps out the symbols that plain | |
1504 | C<langinfo.h> imports into the namespace for code that doesn't need it.) | |
1505 | ||
1506 | You also should not use the bare C<langinfo.h> item names, but should preface | |
1507 | them with C<PERL_>, so use C<PERL_RADIXCHAR> instead of plain C<RADIXCHAR>. | |
1508 | The C<PERL_I<foo>> versions will also work for this function on systems that do | |
1509 | have a native C<nl_langinfo>. | |
1510 | ||
1511 | =item * | |
1512 | ||
1513 | It is thread-friendly, returning its result in a buffer that won't be | |
1514 | overwritten by another thread, so you don't have to code for that possibility. | |
1515 | The buffer can be overwritten by the next call to C<nl_langinfo> or | |
1516 | C<Perl_langinfo> in the same thread. | |
1517 | ||
1518 | =item * | |
1519 | ||
7d4bcc4a | 1520 | ª It returns S<C<const char *>>, whereas plain C<nl_langinfo()> returns S<C<char |
f7416781 KW |
1521 | *>>, but you are (only by documentation) forbidden to write into the buffer. |
1522 | By declaring this C<const>, the compiler enforces this restriction. The extra | |
1523 | C<const> is why this isn't an unequivocal drop-in replacement for | |
1524 | C<nl_langinfo>. | |
1525 | ||
1526 | =back | |
1527 | ||
1528 | The original impetus for C<Perl_langinfo()> was so that code that needs to | |
1529 | find out the current currency symbol, floating point radix character, or digit | |
1530 | grouping separator can use, on all systems, the simpler and more | |
1531 | thread-friendly C<nl_langinfo> API instead of C<L<localeconv(3)>> which is a | |
1532 | pain to make thread-friendly. For other fields returned by C<localeconv>, it | |
1533 | is better to use the methods given in L<perlcall> to call | |
1534 | L<C<POSIX::localeconv()>|POSIX/localeconv>, which is thread-friendly. | |
1535 | ||
1536 | =cut | |
1537 | ||
1538 | */ | |
1539 | ||
1540 | const char * | |
1541 | #ifdef HAS_NL_LANGINFO | |
1542 | Perl_langinfo(const nl_item item) | |
1543 | #else | |
1544 | Perl_langinfo(const int item) | |
1545 | #endif | |
1546 | { | |
f61748ac KW |
1547 | return my_nl_langinfo(item, TRUE); |
1548 | } | |
1549 | ||
1550 | const char * | |
1551 | #ifdef HAS_NL_LANGINFO | |
1552 | S_my_nl_langinfo(const nl_item item, bool toggle) | |
1553 | #else | |
1554 | S_my_nl_langinfo(const int item, bool toggle) | |
1555 | #endif | |
1556 | { | |
ae74815b | 1557 | dTHX; |
f7416781 | 1558 | |
5a854ab3 KW |
1559 | /* We only need to toggle into the underlying LC_NUMERIC locale for these |
1560 | * two items, and only if not already there */ | |
1561 | if (toggle && (( item != PERL_RADIXCHAR && item != PERL_THOUSEP) | |
1562 | || PL_numeric_underlying)) | |
1563 | { | |
1564 | toggle = FALSE; | |
1565 | } | |
1566 | ||
ab340fff KW |
1567 | #if defined(HAS_NL_LANGINFO) /* nl_langinfo() is available. */ |
1568 | #if ! defined(HAS_POSIX_2008_LOCALE) | |
f7416781 | 1569 | |
ab340fff | 1570 | /* Here, use plain nl_langinfo(), switching to the underlying LC_NUMERIC |
ae74815b KW |
1571 | * for those items dependent on it. This must be copied to a buffer before |
1572 | * switching back, as some systems destroy the buffer when setlocale() is | |
1573 | * called */ | |
f7416781 | 1574 | |
038d3702 KW |
1575 | { |
1576 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; | |
1577 | ||
b2fee59c | 1578 | if (toggle) { |
038d3702 | 1579 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING(); |
b2fee59c | 1580 | } |
f7416781 | 1581 | |
5acc4454 KW |
1582 | LOCALE_LOCK; /* Prevent interference from another thread executing |
1583 | this code section (the only call to nl_langinfo in | |
1584 | the core) */ | |
1585 | ||
b2fee59c KW |
1586 | save_to_buffer(nl_langinfo(item), &PL_langinfo_buf, |
1587 | &PL_langinfo_bufsize, 0); | |
f7416781 | 1588 | |
5acc4454 KW |
1589 | LOCALE_UNLOCK; |
1590 | ||
b2fee59c | 1591 | if (toggle) { |
038d3702 | 1592 | RESTORE_LC_NUMERIC(); |
b2fee59c | 1593 | } |
f7416781 KW |
1594 | } |
1595 | ||
ab340fff KW |
1596 | # else /* Use nl_langinfo_l(), avoiding both a mutex and changing the locale */ |
1597 | ||
5a854ab3 | 1598 | { |
b2fee59c KW |
1599 | bool do_free = FALSE; |
1600 | locale_t cur = uselocale((locale_t) 0); | |
ab340fff | 1601 | |
b2fee59c KW |
1602 | if (cur == LC_GLOBAL_LOCALE) { |
1603 | cur = duplocale(LC_GLOBAL_LOCALE); | |
1604 | do_free = TRUE; | |
1605 | } | |
ab340fff | 1606 | |
b2fee59c | 1607 | if (toggle) { |
e1aa2579 KW |
1608 | if (PL_underlying_numeric_obj) { |
1609 | cur = PL_underlying_numeric_obj; | |
1610 | } | |
1611 | else { | |
1612 | cur = newlocale(LC_NUMERIC_MASK, PL_numeric_name, cur); | |
1613 | do_free = TRUE; | |
1614 | } | |
b2fee59c | 1615 | } |
ab340fff | 1616 | |
b2fee59c KW |
1617 | save_to_buffer(nl_langinfo_l(item, cur), |
1618 | &PL_langinfo_buf, &PL_langinfo_bufsize, 0); | |
1619 | if (do_free) { | |
1620 | freelocale(cur); | |
1621 | } | |
5a854ab3 | 1622 | } |
ab340fff | 1623 | |
c1566110 KW |
1624 | # endif |
1625 | ||
1626 | if (strEQ(PL_langinfo_buf, "")) { | |
1627 | if (item == PERL_YESSTR) { | |
1628 | return "yes"; | |
1629 | } | |
1630 | if (item == PERL_NOSTR) { | |
1631 | return "no"; | |
1632 | } | |
1633 | } | |
1634 | ||
ab340fff KW |
1635 | return PL_langinfo_buf; |
1636 | ||
f7416781 | 1637 | #else /* Below, emulate nl_langinfo as best we can */ |
43dd6b15 KW |
1638 | |
1639 | { | |
1640 | ||
f7416781 KW |
1641 | # ifdef HAS_LOCALECONV |
1642 | ||
43dd6b15 | 1643 | const struct lconv* lc; |
038d3702 | 1644 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; |
f7416781 KW |
1645 | |
1646 | # endif | |
1647 | # ifdef HAS_STRFTIME | |
1648 | ||
43dd6b15 KW |
1649 | struct tm tm; |
1650 | bool return_format = FALSE; /* Return the %format, not the value */ | |
1651 | const char * format; | |
f7416781 KW |
1652 | |
1653 | # endif | |
1654 | ||
43dd6b15 KW |
1655 | /* We copy the results to a per-thread buffer, even if not |
1656 | * multi-threaded. This is in part to simplify this code, and partly | |
1657 | * because we need a buffer anyway for strftime(), and partly because a | |
1658 | * call of localeconv() could otherwise wipe out the buffer, and the | |
1659 | * programmer would not be expecting this, as this is a nl_langinfo() | |
1660 | * substitute after all, so s/he might be thinking their localeconv() | |
1661 | * is safe until another localeconv() call. */ | |
f7416781 | 1662 | |
43dd6b15 KW |
1663 | switch (item) { |
1664 | Size_t len; | |
1665 | const char * retval; | |
f7416781 | 1666 | |
43dd6b15 KW |
1667 | /* These 2 are unimplemented */ |
1668 | case PERL_CODESET: | |
1669 | case PERL_ERA: /* For use with strftime() %E modifier */ | |
f7416781 | 1670 | |
43dd6b15 KW |
1671 | default: |
1672 | return ""; | |
f7416781 | 1673 | |
43dd6b15 KW |
1674 | /* We use only an English set, since we don't know any more */ |
1675 | case PERL_YESEXPR: return "^[+1yY]"; | |
1676 | case PERL_YESSTR: return "yes"; | |
1677 | case PERL_NOEXPR: return "^[-0nN]"; | |
1678 | case PERL_NOSTR: return "no"; | |
f7416781 KW |
1679 | |
1680 | # ifdef HAS_LOCALECONV | |
1681 | ||
43dd6b15 | 1682 | case PERL_CRNCYSTR: |
f7416781 | 1683 | |
43dd6b15 KW |
1684 | /* We don't bother with localeconv_l() because any system that |
1685 | * has it is likely to also have nl_langinfo() */ | |
291a84fb | 1686 | |
5acc4454 KW |
1687 | LOCALE_LOCK; /* Prevent interference with other threads |
1688 | using localeconv() */ | |
1689 | ||
43dd6b15 KW |
1690 | lc = localeconv(); |
1691 | if ( ! lc | |
1692 | || ! lc->currency_symbol | |
1693 | || strEQ("", lc->currency_symbol)) | |
1694 | { | |
1695 | LOCALE_UNLOCK; | |
1696 | return ""; | |
1697 | } | |
f7416781 | 1698 | |
43dd6b15 KW |
1699 | /* Leave the first spot empty to be filled in below */ |
1700 | save_to_buffer(lc->currency_symbol, &PL_langinfo_buf, | |
1701 | &PL_langinfo_bufsize, 1); | |
1702 | if (lc->mon_decimal_point && strEQ(lc->mon_decimal_point, "")) | |
1703 | { /* khw couldn't figure out how the localedef specifications | |
1704 | would show that the $ should replace the radix; this is | |
1705 | just a guess as to how it might work.*/ | |
1706 | *PL_langinfo_buf = '.'; | |
1707 | } | |
1708 | else if (lc->p_cs_precedes) { | |
1709 | *PL_langinfo_buf = '-'; | |
1710 | } | |
1711 | else { | |
1712 | *PL_langinfo_buf = '+'; | |
1713 | } | |
f7416781 | 1714 | |
43dd6b15 KW |
1715 | LOCALE_UNLOCK; |
1716 | break; | |
f7416781 | 1717 | |
43dd6b15 KW |
1718 | case PERL_RADIXCHAR: |
1719 | case PERL_THOUSEP: | |
f7416781 | 1720 | |
43dd6b15 | 1721 | if (toggle) { |
038d3702 | 1722 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING(); |
c0d737a8 | 1723 | } |
f7416781 | 1724 | |
5acc4454 KW |
1725 | LOCALE_LOCK; /* Prevent interference with other threads |
1726 | using localeconv() */ | |
1727 | ||
43dd6b15 KW |
1728 | lc = localeconv(); |
1729 | if (! lc) { | |
33394adc KW |
1730 | retval = ""; |
1731 | } | |
43dd6b15 KW |
1732 | else { |
1733 | retval = (item == PERL_RADIXCHAR) | |
1734 | ? lc->decimal_point | |
1735 | : lc->thousands_sep; | |
1736 | if (! retval) { | |
1737 | retval = ""; | |
1738 | } | |
1739 | } | |
f7416781 | 1740 | |
43dd6b15 KW |
1741 | save_to_buffer(retval, &PL_langinfo_buf, |
1742 | &PL_langinfo_bufsize, 0); | |
f7416781 | 1743 | |
5acc4454 KW |
1744 | LOCALE_UNLOCK; |
1745 | ||
43dd6b15 | 1746 | if (toggle) { |
038d3702 | 1747 | RESTORE_LC_NUMERIC(); |
43dd6b15 | 1748 | } |
f7416781 | 1749 | |
43dd6b15 | 1750 | break; |
f7416781 KW |
1751 | |
1752 | # endif | |
1753 | # ifdef HAS_STRFTIME | |
1754 | ||
43dd6b15 KW |
1755 | /* These are defined by C89, so we assume that strftime supports |
1756 | * them, and so are returned unconditionally; they may not be what | |
1757 | * the locale actually says, but should give good enough results | |
1758 | * for someone using them as formats (as opposed to trying to parse | |
1759 | * them to figure out what the locale says). The other format | |
1760 | * items are actually tested to verify they work on the platform */ | |
1761 | case PERL_D_FMT: return "%x"; | |
1762 | case PERL_T_FMT: return "%X"; | |
1763 | case PERL_D_T_FMT: return "%c"; | |
1764 | ||
1765 | /* These formats are only available in later strfmtime's */ | |
1766 | case PERL_ERA_D_FMT: case PERL_ERA_T_FMT: case PERL_ERA_D_T_FMT: | |
1767 | case PERL_T_FMT_AMPM: | |
1768 | ||
1769 | /* The rest can be gotten from most versions of strftime(). */ | |
1770 | case PERL_ABDAY_1: case PERL_ABDAY_2: case PERL_ABDAY_3: | |
1771 | case PERL_ABDAY_4: case PERL_ABDAY_5: case PERL_ABDAY_6: | |
1772 | case PERL_ABDAY_7: | |
1773 | case PERL_ALT_DIGITS: | |
1774 | case PERL_AM_STR: case PERL_PM_STR: | |
1775 | case PERL_ABMON_1: case PERL_ABMON_2: case PERL_ABMON_3: | |
1776 | case PERL_ABMON_4: case PERL_ABMON_5: case PERL_ABMON_6: | |
1777 | case PERL_ABMON_7: case PERL_ABMON_8: case PERL_ABMON_9: | |
1778 | case PERL_ABMON_10: case PERL_ABMON_11: case PERL_ABMON_12: | |
1779 | case PERL_DAY_1: case PERL_DAY_2: case PERL_DAY_3: case PERL_DAY_4: | |
1780 | case PERL_DAY_5: case PERL_DAY_6: case PERL_DAY_7: | |
1781 | case PERL_MON_1: case PERL_MON_2: case PERL_MON_3: case PERL_MON_4: | |
1782 | case PERL_MON_5: case PERL_MON_6: case PERL_MON_7: case PERL_MON_8: | |
1783 | case PERL_MON_9: case PERL_MON_10: case PERL_MON_11: | |
1784 | case PERL_MON_12: | |
1785 | ||
1786 | LOCALE_LOCK; | |
1787 | ||
1788 | init_tm(&tm); /* Precaution against core dumps */ | |
1789 | tm.tm_sec = 30; | |
1790 | tm.tm_min = 30; | |
1791 | tm.tm_hour = 6; | |
1792 | tm.tm_year = 2017 - 1900; | |
1793 | tm.tm_wday = 0; | |
1794 | tm.tm_mon = 0; | |
1795 | switch (item) { | |
1796 | default: | |
1797 | LOCALE_UNLOCK; | |
1798 | Perl_croak(aTHX_ | |
1799 | "panic: %s: %d: switch case: %d problem", | |
1800 | __FILE__, __LINE__, item); | |
1801 | NOT_REACHED; /* NOTREACHED */ | |
1802 | ||
1803 | case PERL_PM_STR: tm.tm_hour = 18; | |
1804 | case PERL_AM_STR: | |
1805 | format = "%p"; | |
1806 | break; | |
1807 | ||
1808 | case PERL_ABDAY_7: tm.tm_wday++; | |
1809 | case PERL_ABDAY_6: tm.tm_wday++; | |
1810 | case PERL_ABDAY_5: tm.tm_wday++; | |
1811 | case PERL_ABDAY_4: tm.tm_wday++; | |
1812 | case PERL_ABDAY_3: tm.tm_wday++; | |
1813 | case PERL_ABDAY_2: tm.tm_wday++; | |
1814 | case PERL_ABDAY_1: | |
1815 | format = "%a"; | |
1816 | break; | |
1817 | ||
1818 | case PERL_DAY_7: tm.tm_wday++; | |
1819 | case PERL_DAY_6: tm.tm_wday++; | |
1820 | case PERL_DAY_5: tm.tm_wday++; | |
1821 | case PERL_DAY_4: tm.tm_wday++; | |
1822 | case PERL_DAY_3: tm.tm_wday++; | |
1823 | case PERL_DAY_2: tm.tm_wday++; | |
1824 | case PERL_DAY_1: | |
1825 | format = "%A"; | |
1826 | break; | |
1827 | ||
1828 | case PERL_ABMON_12: tm.tm_mon++; | |
1829 | case PERL_ABMON_11: tm.tm_mon++; | |
1830 | case PERL_ABMON_10: tm.tm_mon++; | |
1831 | case PERL_ABMON_9: tm.tm_mon++; | |
1832 | case PERL_ABMON_8: tm.tm_mon++; | |
1833 | case PERL_ABMON_7: tm.tm_mon++; | |
1834 | case PERL_ABMON_6: tm.tm_mon++; | |
1835 | case PERL_ABMON_5: tm.tm_mon++; | |
1836 | case PERL_ABMON_4: tm.tm_mon++; | |
1837 | case PERL_ABMON_3: tm.tm_mon++; | |
1838 | case PERL_ABMON_2: tm.tm_mon++; | |
1839 | case PERL_ABMON_1: | |
1840 | format = "%b"; | |
1841 | break; | |
1842 | ||
1843 | case PERL_MON_12: tm.tm_mon++; | |
1844 | case PERL_MON_11: tm.tm_mon++; | |
1845 | case PERL_MON_10: tm.tm_mon++; | |
1846 | case PERL_MON_9: tm.tm_mon++; | |
1847 | case PERL_MON_8: tm.tm_mon++; | |
1848 | case PERL_MON_7: tm.tm_mon++; | |
1849 | case PERL_MON_6: tm.tm_mon++; | |
1850 | case PERL_MON_5: tm.tm_mon++; | |
1851 | case PERL_MON_4: tm.tm_mon++; | |
1852 | case PERL_MON_3: tm.tm_mon++; | |
1853 | case PERL_MON_2: tm.tm_mon++; | |
1854 | case PERL_MON_1: | |
1855 | format = "%B"; | |
1856 | break; | |
1857 | ||
1858 | case PERL_T_FMT_AMPM: | |
1859 | format = "%r"; | |
1860 | return_format = TRUE; | |
1861 | break; | |
1862 | ||
1863 | case PERL_ERA_D_FMT: | |
1864 | format = "%Ex"; | |
1865 | return_format = TRUE; | |
1866 | break; | |
1867 | ||
1868 | case PERL_ERA_T_FMT: | |
1869 | format = "%EX"; | |
1870 | return_format = TRUE; | |
1871 | break; | |
1872 | ||
1873 | case PERL_ERA_D_T_FMT: | |
1874 | format = "%Ec"; | |
1875 | return_format = TRUE; | |
1876 | break; | |
1877 | ||
1878 | case PERL_ALT_DIGITS: | |
1879 | tm.tm_wday = 0; | |
1880 | format = "%Ow"; /* Find the alternate digit for 0 */ | |
1881 | break; | |
1882 | } | |
f7416781 | 1883 | |
43dd6b15 KW |
1884 | /* We can't use my_strftime() because it doesn't look at |
1885 | * tm_wday */ | |
1886 | while (0 == strftime(PL_langinfo_buf, PL_langinfo_bufsize, | |
1887 | format, &tm)) | |
1888 | { | |
1889 | /* A zero return means one of: | |
1890 | * a) there wasn't enough space in PL_langinfo_buf | |
1891 | * b) the format, like a plain %p, returns empty | |
1892 | * c) it was an illegal format, though some | |
1893 | * implementations of strftime will just return the | |
1894 | * illegal format as a plain character sequence. | |
1895 | * | |
1896 | * To quickly test for case 'b)', try again but precede | |
1897 | * the format with a plain character. If that result is | |
1898 | * still empty, the problem is either 'a)' or 'c)' */ | |
1899 | ||
1900 | Size_t format_size = strlen(format) + 1; | |
1901 | Size_t mod_size = format_size + 1; | |
1902 | char * mod_format; | |
1903 | char * temp_result; | |
1904 | ||
1905 | Newx(mod_format, mod_size, char); | |
1906 | Newx(temp_result, PL_langinfo_bufsize, char); | |
6873aa47 | 1907 | *mod_format = ' '; |
43dd6b15 KW |
1908 | my_strlcpy(mod_format + 1, format, mod_size); |
1909 | len = strftime(temp_result, | |
1910 | PL_langinfo_bufsize, | |
1911 | mod_format, &tm); | |
1912 | Safefree(mod_format); | |
1913 | Safefree(temp_result); | |
1914 | ||
1915 | /* If 'len' is non-zero, it means that we had a case like | |
1916 | * %p which means the current locale doesn't use a.m. or | |
1917 | * p.m., and that is valid */ | |
1918 | if (len == 0) { | |
1919 | ||
1920 | /* Here, still didn't work. If we get well beyond a | |
1921 | * reasonable size, bail out to prevent an infinite | |
1922 | * loop. */ | |
1923 | ||
1924 | if (PL_langinfo_bufsize > 100 * format_size) { | |
1925 | *PL_langinfo_buf = '\0'; | |
1926 | } | |
1927 | else { | |
1928 | /* Double the buffer size to retry; Add 1 in case | |
1929 | * original was 0, so we aren't stuck at 0. */ | |
1930 | PL_langinfo_bufsize *= 2; | |
1931 | PL_langinfo_bufsize++; | |
1932 | Renew(PL_langinfo_buf, PL_langinfo_bufsize, char); | |
1933 | continue; | |
1934 | } | |
1935 | } | |
f7416781 | 1936 | |
f7416781 | 1937 | break; |
43dd6b15 | 1938 | } |
f7416781 | 1939 | |
43dd6b15 KW |
1940 | /* Here, we got a result. |
1941 | * | |
1942 | * If the item is 'ALT_DIGITS', PL_langinfo_buf contains the | |
1943 | * alternate format for wday 0. If the value is the same as | |
1944 | * the normal 0, there isn't an alternate, so clear the buffer. | |
1945 | * */ | |
1946 | if ( item == PERL_ALT_DIGITS | |
1947 | && strEQ(PL_langinfo_buf, "0")) | |
1948 | { | |
1949 | *PL_langinfo_buf = '\0'; | |
1950 | } | |
f7416781 | 1951 | |
43dd6b15 KW |
1952 | /* ALT_DIGITS is problematic. Experiments on it showed that |
1953 | * strftime() did not always work properly when going from | |
1954 | * alt-9 to alt-10. Only a few locales have this item defined, | |
1955 | * and in all of them on Linux that khw was able to find, | |
1956 | * nl_langinfo() merely returned the alt-0 character, possibly | |
1957 | * doubled. Most Unicode digits are in blocks of 10 | |
1958 | * consecutive code points, so that is sufficient information | |
1959 | * for those scripts, as we can infer alt-1, alt-2, .... But | |
1960 | * for a Japanese locale, a CJK ideographic 0 is returned, and | |
1961 | * the CJK digits are not in code point order, so you can't | |
1962 | * really infer anything. The localedef for this locale did | |
1963 | * specify the succeeding digits, so that strftime() works | |
1964 | * properly on them, without needing to infer anything. But | |
1965 | * the nl_langinfo() return did not give sufficient information | |
1966 | * for the caller to understand what's going on. So until | |
1967 | * there is evidence that it should work differently, this | |
1968 | * returns the alt-0 string for ALT_DIGITS. | |
1969 | * | |
1970 | * wday was chosen because its range is all a single digit. | |
1971 | * Things like tm_sec have two digits as the minimum: '00' */ | |
f7416781 | 1972 | |
43dd6b15 | 1973 | LOCALE_UNLOCK; |
f7416781 | 1974 | |
43dd6b15 KW |
1975 | /* If to return the format, not the value, overwrite the buffer |
1976 | * with it. But some strftime()s will keep the original format | |
1977 | * if illegal, so change those to "" */ | |
1978 | if (return_format) { | |
1979 | if (strEQ(PL_langinfo_buf, format)) { | |
f7416781 KW |
1980 | *PL_langinfo_buf = '\0'; |
1981 | } | |
43dd6b15 KW |
1982 | else { |
1983 | save_to_buffer(format, &PL_langinfo_buf, | |
1984 | &PL_langinfo_bufsize, 0); | |
f7416781 KW |
1985 | } |
1986 | } | |
1987 | ||
1988 | break; | |
f7416781 KW |
1989 | |
1990 | # endif | |
1991 | ||
43dd6b15 | 1992 | } |
f7416781 KW |
1993 | } |
1994 | ||
1995 | return PL_langinfo_buf; | |
1996 | ||
1997 | #endif | |
1998 | ||
a4f00dcc | 1999 | } |
b385bb4d | 2000 | |
98994639 HS |
2001 | /* |
2002 | * Initialize locale awareness. | |
2003 | */ | |
2004 | int | |
2005 | Perl_init_i18nl10n(pTHX_ int printwarn) | |
2006 | { | |
0e92a118 KW |
2007 | /* printwarn is |
2008 | * | |
2009 | * 0 if not to output warning when setup locale is bad | |
2010 | * 1 if to output warning based on value of PERL_BADLANG | |
2011 | * >1 if to output regardless of PERL_BADLANG | |
2012 | * | |
2013 | * returns | |
98994639 | 2014 | * 1 = set ok or not applicable, |
0e92a118 KW |
2015 | * 0 = fallback to a locale of lower priority |
2016 | * -1 = fallback to all locales failed, not even to the C locale | |
6b058d42 KW |
2017 | * |
2018 | * Under -DDEBUGGING, if the environment variable PERL_DEBUG_LOCALE_INIT is | |
2019 | * set, debugging information is output. | |
2020 | * | |
2021 | * This looks more complicated than it is, mainly due to the #ifdefs. | |
2022 | * | |
2023 | * We try to set LC_ALL to the value determined by the environment. If | |
2024 | * there is no LC_ALL on this platform, we try the individual categories we | |
2025 | * know about. If this works, we are done. | |
2026 | * | |
2027 | * But if it doesn't work, we have to do something else. We search the | |
2028 | * environment variables ourselves instead of relying on the system to do | |
2029 | * it. We look at, in order, LC_ALL, LANG, a system default locale (if we | |
2030 | * think there is one), and the ultimate fallback "C". This is all done in | |
2031 | * the same loop as above to avoid duplicating code, but it makes things | |
7d4bcc4a KW |
2032 | * more complex. The 'trial_locales' array is initialized with just one |
2033 | * element; it causes the behavior described in the paragraph above this to | |
2034 | * happen. If that fails, we add elements to 'trial_locales', and do extra | |
2035 | * loop iterations to cause the behavior described in this paragraph. | |
6b058d42 KW |
2036 | * |
2037 | * On Ultrix, the locale MUST come from the environment, so there is | |
2038 | * preliminary code to set it. I (khw) am not sure that it is necessary, | |
2039 | * and that this couldn't be folded into the loop, but barring any real | |
2040 | * platforms to test on, it's staying as-is | |
2041 | * | |
2042 | * A slight complication is that in embedded Perls, the locale may already | |
2043 | * be set-up, and we don't want to get it from the normal environment | |
2044 | * variables. This is handled by having a special environment variable | |
2045 | * indicate we're in this situation. We simply set setlocale's 2nd | |
2046 | * parameter to be a NULL instead of "". That indicates to setlocale that | |
2047 | * it is not to change anything, but to return the current value, | |
2048 | * effectively initializing perl's db to what the locale already is. | |
2049 | * | |
2050 | * We play the same trick with NULL if a LC_ALL succeeds. We call | |
2051 | * setlocale() on the individual categores with NULL to get their existing | |
2052 | * values for our db, instead of trying to change them. | |
2053 | * */ | |
98994639 | 2054 | |
0e92a118 KW |
2055 | int ok = 1; |
2056 | ||
7d4bcc4a KW |
2057 | #ifndef USE_LOCALE |
2058 | ||
2059 | PERL_UNUSED_ARG(printwarn); | |
2060 | ||
2061 | #else /* USE_LOCALE */ | |
7d4bcc4a KW |
2062 | # ifdef __GLIBC__ |
2063 | ||
175c4cf9 | 2064 | const char * const language = savepv(PerlEnv_getenv("LANGUAGE")); |
7d4bcc4a KW |
2065 | |
2066 | # endif | |
65ebb059 | 2067 | |
ccd65d51 KW |
2068 | /* NULL uses the existing already set up locale */ |
2069 | const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT")) | |
2070 | ? NULL | |
2071 | : ""; | |
c3fcd832 KW |
2072 | const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */ |
2073 | unsigned int trial_locales_count; | |
175c4cf9 KW |
2074 | const char * const lc_all = savepv(PerlEnv_getenv("LC_ALL")); |
2075 | const char * const lang = savepv(PerlEnv_getenv("LANG")); | |
98994639 | 2076 | bool setlocale_failure = FALSE; |
65ebb059 | 2077 | unsigned int i; |
175c4cf9 KW |
2078 | |
2079 | /* A later getenv() could zap this, so only use here */ | |
2080 | const char * const bad_lang_use_once = PerlEnv_getenv("PERL_BADLANG"); | |
2081 | ||
2082 | const bool locwarn = (printwarn > 1 | |
e5f10d49 KW |
2083 | || ( printwarn |
2084 | && ( ! bad_lang_use_once | |
22ff3130 | 2085 | || ( |
e5f10d49 KW |
2086 | /* disallow with "" or "0" */ |
2087 | *bad_lang_use_once | |
2088 | && strNE("0", bad_lang_use_once))))); | |
ea92aad8 | 2089 | |
291a84fb | 2090 | /* setlocale() return vals; not copied so must be looked at immediately */ |
8de4332b | 2091 | const char * sl_result[NOMINAL_LC_ALL_INDEX + 1]; |
291a84fb KW |
2092 | |
2093 | /* current locale for given category; should have been copied so aren't | |
2094 | * volatile */ | |
8de4332b | 2095 | const char * curlocales[NOMINAL_LC_ALL_INDEX + 1]; |
291a84fb | 2096 | |
7d4bcc4a KW |
2097 | # ifdef WIN32 |
2098 | ||
6bce99ee JH |
2099 | /* In some systems you can find out the system default locale |
2100 | * and use that as the fallback locale. */ | |
7d4bcc4a KW |
2101 | # define SYSTEM_DEFAULT_LOCALE |
2102 | # endif | |
2103 | # ifdef SYSTEM_DEFAULT_LOCALE | |
2104 | ||
65ebb059 | 2105 | const char *system_default_locale = NULL; |
98994639 | 2106 | |
7d4bcc4a | 2107 | # endif |
948523db KW |
2108 | |
2109 | # ifndef DEBUGGING | |
2110 | # define DEBUG_LOCALE_INIT(a,b,c) | |
2111 | # else | |
7d4bcc4a | 2112 | |
8298454c | 2113 | DEBUG_INITIALIZATION_set(cBOOL(PerlEnv_getenv("PERL_DEBUG_LOCALE_INIT"))); |
7d4bcc4a KW |
2114 | |
2115 | # define DEBUG_LOCALE_INIT(category, locale, result) \ | |
2fcc0ca9 KW |
2116 | STMT_START { \ |
2117 | if (debug_initialization) { \ | |
2118 | PerlIO_printf(Perl_debug_log, \ | |
2119 | "%s:%d: %s\n", \ | |
2120 | __FILE__, __LINE__, \ | |
a4f00dcc | 2121 | setlocale_debug_string(category, \ |
2fcc0ca9 KW |
2122 | locale, \ |
2123 | result)); \ | |
2124 | } \ | |
2125 | } STMT_END | |
2fcc0ca9 | 2126 | |
948523db KW |
2127 | /* Make sure the parallel arrays are properly set up */ |
2128 | # ifdef USE_LOCALE_NUMERIC | |
2129 | assert(categories[LC_NUMERIC_INDEX] == LC_NUMERIC); | |
2130 | assert(strEQ(category_names[LC_NUMERIC_INDEX], "LC_NUMERIC")); | |
2131 | # endif | |
2132 | # ifdef USE_LOCALE_CTYPE | |
2133 | assert(categories[LC_CTYPE_INDEX] == LC_CTYPE); | |
2134 | assert(strEQ(category_names[LC_CTYPE_INDEX], "LC_CTYPE")); | |
2135 | # endif | |
2136 | # ifdef USE_LOCALE_COLLATE | |
2137 | assert(categories[LC_COLLATE_INDEX] == LC_COLLATE); | |
2138 | assert(strEQ(category_names[LC_COLLATE_INDEX], "LC_COLLATE")); | |
2139 | # endif | |
2140 | # ifdef USE_LOCALE_TIME | |
2141 | assert(categories[LC_TIME_INDEX] == LC_TIME); | |
2142 | assert(strEQ(category_names[LC_TIME_INDEX], "LC_TIME")); | |
2143 | # endif | |
2144 | # ifdef USE_LOCALE_MESSAGES | |
2145 | assert(categories[LC_MESSAGES_INDEX] == LC_MESSAGES); | |
2146 | assert(strEQ(category_names[LC_MESSAGES_INDEX], "LC_MESSAGES")); | |
2147 | # endif | |
2148 | # ifdef USE_LOCALE_MONETARY | |
2149 | assert(categories[LC_MONETARY_INDEX] == LC_MONETARY); | |
2150 | assert(strEQ(category_names[LC_MONETARY_INDEX], "LC_MONETARY")); | |
2151 | # endif | |
9821811f KW |
2152 | # ifdef USE_LOCALE_ADDRESS |
2153 | assert(categories[LC_ADDRESS_INDEX] == LC_ADDRESS); | |
2154 | assert(strEQ(category_names[LC_ADDRESS_INDEX], "LC_ADDRESS")); | |
2155 | # endif | |
2156 | # ifdef USE_LOCALE_IDENTIFICATION | |
2157 | assert(categories[LC_IDENTIFICATION_INDEX] == LC_IDENTIFICATION); | |
2158 | assert(strEQ(category_names[LC_IDENTIFICATION_INDEX], "LC_IDENTIFICATION")); | |
2159 | # endif | |
2160 | # ifdef USE_LOCALE_MEASUREMENT | |
2161 | assert(categories[LC_MEASUREMENT_INDEX] == LC_MEASUREMENT); | |
2162 | assert(strEQ(category_names[LC_MEASUREMENT_INDEX], "LC_MEASUREMENT")); | |
2163 | # endif | |
2164 | # ifdef USE_LOCALE_PAPER | |
2165 | assert(categories[LC_PAPER_INDEX] == LC_PAPER); | |
2166 | assert(strEQ(category_names[LC_PAPER_INDEX], "LC_PAPER")); | |
2167 | # endif | |
2168 | # ifdef USE_LOCALE_TELEPHONE | |
2169 | assert(categories[LC_TELEPHONE_INDEX] == LC_TELEPHONE); | |
2170 | assert(strEQ(category_names[LC_TELEPHONE_INDEX], "LC_TELEPHONE")); | |
2171 | # endif | |
948523db KW |
2172 | # ifdef LC_ALL |
2173 | assert(categories[LC_ALL_INDEX] == LC_ALL); | |
2174 | assert(strEQ(category_names[LC_ALL_INDEX], "LC_ALL")); | |
2175 | assert(NOMINAL_LC_ALL_INDEX == LC_ALL_INDEX); | |
2176 | # endif | |
2177 | # endif /* DEBUGGING */ | |
47280b20 KW |
2178 | |
2179 | /* Initialize the cache of the program's UTF-8ness for the always known | |
2180 | * locales C and POSIX */ | |
2181 | my_strlcpy(PL_locale_utf8ness, C_and_POSIX_utf8ness, | |
2182 | sizeof(PL_locale_utf8ness)); | |
2183 | ||
3ca88433 KW |
2184 | PL_numeric_radix_sv = newSVpvs("."); |
2185 | ||
b56c4436 | 2186 | # ifdef LOCALE_ENVIRON_REQUIRED |
98994639 HS |
2187 | |
2188 | /* | |
2189 | * Ultrix setlocale(..., "") fails if there are no environment | |
2190 | * variables from which to get a locale name. | |
2191 | */ | |
2192 | ||
b56c4436 KW |
2193 | # ifndef LC_ALL |
2194 | # error Ultrix without LC_ALL not implemented | |
2195 | # else | |
7d4bcc4a | 2196 | |
b56c4436 KW |
2197 | { |
2198 | bool done = FALSE; | |
ea92aad8 KW |
2199 | if (lang) { |
2200 | sl_result[LC_ALL_INDEX] = do_setlocale_c(LC_ALL, setlocale_init); | |
2201 | DEBUG_LOCALE_INIT(LC_ALL, setlocale_init, sl_result[LC_ALL_INDEX]); | |
2202 | if (sl_result[LC_ALL_INDEX]) | |
2203 | done = TRUE; | |
2204 | else | |
e5f10d49 | 2205 | setlocale_failure = TRUE; |
ea92aad8 KW |
2206 | } |
2207 | if (! setlocale_failure) { | |
2208 | const char * locale_param; | |
2209 | for (i = 0; i < LC_ALL_INDEX; i++) { | |
2210 | locale_param = (! done && (lang || PerlEnv_getenv(category_names[i]))) | |
2211 | ? setlocale_init | |
2212 | : NULL; | |
2213 | sl_result[i] = do_setlocale_r(categories[i], locale_param); | |
2214 | if (! sl_result[i]) { | |
2215 | setlocale_failure = TRUE; | |
2216 | } | |
2217 | DEBUG_LOCALE_INIT(categories[i], locale_param, sl_result[i]); | |
e5f10d49 | 2218 | } |
c835d6be | 2219 | } |
7d4bcc4a KW |
2220 | } |
2221 | ||
2222 | # endif /* LC_ALL */ | |
e5f10d49 | 2223 | # endif /* LOCALE_ENVIRON_REQUIRED */ |
98994639 | 2224 | |
65ebb059 | 2225 | /* We try each locale in the list until we get one that works, or exhaust |
20a240df KW |
2226 | * the list. Normally the loop is executed just once. But if setting the |
2227 | * locale fails, inside the loop we add fallback trials to the array and so | |
2228 | * will execute the loop multiple times */ | |
c3fcd832 KW |
2229 | trial_locales[0] = setlocale_init; |
2230 | trial_locales_count = 1; | |
7d4bcc4a | 2231 | |
65ebb059 KW |
2232 | for (i= 0; i < trial_locales_count; i++) { |
2233 | const char * trial_locale = trial_locales[i]; | |
2234 | ||
2235 | if (i > 0) { | |
2236 | ||
2237 | /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED | |
2238 | * when i==0, but I (khw) don't think that behavior makes much | |
2239 | * sense */ | |
2240 | setlocale_failure = FALSE; | |
2241 | ||
7d4bcc4a | 2242 | # ifdef SYSTEM_DEFAULT_LOCALE |
291a84fb | 2243 | # ifdef WIN32 /* Note that assumes Win32 has LC_ALL */ |
7d4bcc4a | 2244 | |
65ebb059 KW |
2245 | /* On Windows machines, an entry of "" after the 0th means to use |
2246 | * the system default locale, which we now proceed to get. */ | |
2247 | if (strEQ(trial_locale, "")) { | |
2248 | unsigned int j; | |
2249 | ||
2250 | /* Note that this may change the locale, but we are going to do | |
2251 | * that anyway just below */ | |
837ce802 | 2252 | system_default_locale = do_setlocale_c(LC_ALL, ""); |
5d1187d1 | 2253 | DEBUG_LOCALE_INIT(LC_ALL, "", system_default_locale); |
65ebb059 | 2254 | |
7d4bcc4a | 2255 | /* Skip if invalid or if it's already on the list of locales to |
65ebb059 KW |
2256 | * try */ |
2257 | if (! system_default_locale) { | |
2258 | goto next_iteration; | |
2259 | } | |
2260 | for (j = 0; j < trial_locales_count; j++) { | |
2261 | if (strEQ(system_default_locale, trial_locales[j])) { | |
2262 | goto next_iteration; | |
2263 | } | |
2264 | } | |
2265 | ||
2266 | trial_locale = system_default_locale; | |
2267 | } | |
ec0202b5 KW |
2268 | # else |
2269 | # error SYSTEM_DEFAULT_LOCALE only implemented for Win32 | |
2270 | # endif | |
7d4bcc4a | 2271 | # endif /* SYSTEM_DEFAULT_LOCALE */ |
291a84fb KW |
2272 | |
2273 | } /* For i > 0 */ | |
65ebb059 | 2274 | |
7d4bcc4a KW |
2275 | # ifdef LC_ALL |
2276 | ||
948523db KW |
2277 | sl_result[LC_ALL_INDEX] = do_setlocale_c(LC_ALL, trial_locale); |
2278 | DEBUG_LOCALE_INIT(LC_ALL, trial_locale, sl_result[LC_ALL_INDEX]); | |
2279 | if (! sl_result[LC_ALL_INDEX]) { | |
49c85077 | 2280 | setlocale_failure = TRUE; |
7cd8b568 KW |
2281 | } |
2282 | else { | |
2283 | /* Since LC_ALL succeeded, it should have changed all the other | |
2284 | * categories it can to its value; so we massage things so that the | |
2285 | * setlocales below just return their category's current values. | |
2286 | * This adequately handles the case in NetBSD where LC_COLLATE may | |
2287 | * not be defined for a locale, and setting it individually will | |
7d4bcc4a | 2288 | * fail, whereas setting LC_ALL succeeds, leaving LC_COLLATE set to |
7cd8b568 KW |
2289 | * the POSIX locale. */ |
2290 | trial_locale = NULL; | |
2291 | } | |
7d4bcc4a KW |
2292 | |
2293 | # endif /* LC_ALL */ | |
98994639 | 2294 | |
e5f10d49 KW |
2295 | if (! setlocale_failure) { |
2296 | unsigned int j; | |
2297 | for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) { | |
2298 | curlocales[j] | |
2299 | = savepv(do_setlocale_r(categories[j], trial_locale)); | |
2300 | if (! curlocales[j]) { | |
2301 | setlocale_failure = TRUE; | |
2302 | } | |
2303 | DEBUG_LOCALE_INIT(categories[j], trial_locale, curlocales[j]); | |
2304 | } | |
c835d6be | 2305 | |
e5f10d49 KW |
2306 | if (! setlocale_failure) { /* All succeeded */ |
2307 | break; /* Exit trial_locales loop */ | |
49c85077 | 2308 | } |
65ebb059 | 2309 | } |
98994639 | 2310 | |
49c85077 KW |
2311 | /* Here, something failed; will need to try a fallback. */ |
2312 | ok = 0; | |
65ebb059 | 2313 | |
49c85077 KW |
2314 | if (i == 0) { |
2315 | unsigned int j; | |
98994639 | 2316 | |
65ebb059 | 2317 | if (locwarn) { /* Output failure info only on the first one */ |
7d4bcc4a KW |
2318 | |
2319 | # ifdef LC_ALL | |
98994639 | 2320 | |
49c85077 KW |
2321 | PerlIO_printf(Perl_error_log, |
2322 | "perl: warning: Setting locale failed.\n"); | |
98994639 | 2323 | |
7d4bcc4a | 2324 | # else /* !LC_ALL */ |
98994639 | 2325 | |
49c85077 KW |
2326 | PerlIO_printf(Perl_error_log, |
2327 | "perl: warning: Setting locale failed for the categories:\n\t"); | |
7d4bcc4a | 2328 | |
e5f10d49 KW |
2329 | for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) { |
2330 | if (! curlocales[j]) { | |
2331 | PerlIO_printf(Perl_error_log, category_names[j]); | |
2332 | } | |
2333 | else { | |
2334 | Safefree(curlocales[j]); | |
2335 | } | |
2336 | } | |
7d4bcc4a | 2337 | |
7d4bcc4a | 2338 | # endif /* LC_ALL */ |
98994639 | 2339 | |
49c85077 KW |
2340 | PerlIO_printf(Perl_error_log, |
2341 | "perl: warning: Please check that your locale settings:\n"); | |
98994639 | 2342 | |
7d4bcc4a KW |
2343 | # ifdef __GLIBC__ |
2344 | ||
49c85077 KW |
2345 | PerlIO_printf(Perl_error_log, |
2346 | "\tLANGUAGE = %c%s%c,\n", | |
2347 | language ? '"' : '(', | |
2348 | language ? language : "unset", | |
2349 | language ? '"' : ')'); | |
7d4bcc4a | 2350 | # endif |
98994639 | 2351 | |
49c85077 KW |
2352 | PerlIO_printf(Perl_error_log, |
2353 | "\tLC_ALL = %c%s%c,\n", | |
2354 | lc_all ? '"' : '(', | |
2355 | lc_all ? lc_all : "unset", | |
2356 | lc_all ? '"' : ')'); | |
98994639 | 2357 | |
7d4bcc4a KW |
2358 | # if defined(USE_ENVIRON_ARRAY) |
2359 | ||
49c85077 | 2360 | { |
cd999af9 | 2361 | char **e; |
d5e32b93 KW |
2362 | |
2363 | /* Look through the environment for any variables of the | |
2364 | * form qr/ ^ LC_ [A-Z]+ = /x, except LC_ALL which was | |
2365 | * already handled above. These are assumed to be locale | |
2366 | * settings. Output them and their values. */ | |
cd999af9 | 2367 | for (e = environ; *e; e++) { |
d5e32b93 KW |
2368 | const STRLEN prefix_len = sizeof("LC_") - 1; |
2369 | STRLEN uppers_len; | |
2370 | ||
cd999af9 | 2371 | if ( strBEGINs(*e, "LC_") |
c8b388b0 | 2372 | && ! strBEGINs(*e, "LC_ALL=") |
d5e32b93 KW |
2373 | && (uppers_len = strspn(*e + prefix_len, |
2374 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) | |
2375 | && ((*e)[prefix_len + uppers_len] == '=')) | |
cd999af9 KW |
2376 | { |
2377 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", | |
d5e32b93 KW |
2378 | (int) (prefix_len + uppers_len), *e, |
2379 | *e + prefix_len + uppers_len + 1); | |
cd999af9 KW |
2380 | } |
2381 | } | |
49c85077 | 2382 | } |
7d4bcc4a KW |
2383 | |
2384 | # else | |
2385 | ||
49c85077 KW |
2386 | PerlIO_printf(Perl_error_log, |
2387 | "\t(possibly more locale environment variables)\n"); | |
7d4bcc4a KW |
2388 | |
2389 | # endif | |
98994639 | 2390 | |
49c85077 KW |
2391 | PerlIO_printf(Perl_error_log, |
2392 | "\tLANG = %c%s%c\n", | |
2393 | lang ? '"' : '(', | |
2394 | lang ? lang : "unset", | |
2395 | lang ? '"' : ')'); | |
98994639 | 2396 | |
49c85077 KW |
2397 | PerlIO_printf(Perl_error_log, |
2398 | " are supported and installed on your system.\n"); | |
2399 | } | |
98994639 | 2400 | |
65ebb059 | 2401 | /* Calculate what fallback locales to try. We have avoided this |
f6bab5f6 | 2402 | * until we have to, because failure is quite unlikely. This will |
65ebb059 KW |
2403 | * usually change the upper bound of the loop we are in. |
2404 | * | |
2405 | * Since the system's default way of setting the locale has not | |
2406 | * found one that works, We use Perl's defined ordering: LC_ALL, | |
2407 | * LANG, and the C locale. We don't try the same locale twice, so | |
2408 | * don't add to the list if already there. (On POSIX systems, the | |
2409 | * LC_ALL element will likely be a repeat of the 0th element "", | |
6b058d42 KW |
2410 | * but there's no harm done by doing it explicitly. |
2411 | * | |
2412 | * Note that this tries the LC_ALL environment variable even on | |
2413 | * systems which have no LC_ALL locale setting. This may or may | |
2414 | * not have been originally intentional, but there's no real need | |
2415 | * to change the behavior. */ | |
65ebb059 KW |
2416 | if (lc_all) { |
2417 | for (j = 0; j < trial_locales_count; j++) { | |
2418 | if (strEQ(lc_all, trial_locales[j])) { | |
2419 | goto done_lc_all; | |
2420 | } | |
2421 | } | |
2422 | trial_locales[trial_locales_count++] = lc_all; | |
2423 | } | |
2424 | done_lc_all: | |
98994639 | 2425 | |
65ebb059 KW |
2426 | if (lang) { |
2427 | for (j = 0; j < trial_locales_count; j++) { | |
2428 | if (strEQ(lang, trial_locales[j])) { | |
2429 | goto done_lang; | |
2430 | } | |
2431 | } | |
2432 | trial_locales[trial_locales_count++] = lang; | |
2433 | } | |
2434 | done_lang: | |
2435 | ||
7d4bcc4a KW |
2436 | # if defined(WIN32) && defined(LC_ALL) |
2437 | ||
65ebb059 KW |
2438 | /* For Windows, we also try the system default locale before "C". |
2439 | * (If there exists a Windows without LC_ALL we skip this because | |
2440 | * it gets too complicated. For those, the "C" is the next | |
2441 | * fallback possibility). The "" is the same as the 0th element of | |
2442 | * the array, but the code at the loop above knows to treat it | |
2443 | * differently when not the 0th */ | |
2444 | trial_locales[trial_locales_count++] = ""; | |
7d4bcc4a KW |
2445 | |
2446 | # endif | |
65ebb059 KW |
2447 | |
2448 | for (j = 0; j < trial_locales_count; j++) { | |
2449 | if (strEQ("C", trial_locales[j])) { | |
2450 | goto done_C; | |
2451 | } | |
2452 | } | |
2453 | trial_locales[trial_locales_count++] = "C"; | |
98994639 | 2454 | |
65ebb059 KW |
2455 | done_C: ; |
2456 | } /* end of first time through the loop */ | |
98994639 | 2457 | |
7d4bcc4a KW |
2458 | # ifdef WIN32 |
2459 | ||
65ebb059 | 2460 | next_iteration: ; |
7d4bcc4a KW |
2461 | |
2462 | # endif | |
65ebb059 KW |
2463 | |
2464 | } /* end of looping through the trial locales */ | |
2465 | ||
2466 | if (ok < 1) { /* If we tried to fallback */ | |
2467 | const char* msg; | |
2468 | if (! setlocale_failure) { /* fallback succeeded */ | |
2469 | msg = "Falling back to"; | |
2470 | } | |
2471 | else { /* fallback failed */ | |
e5f10d49 | 2472 | unsigned int j; |
98994639 | 2473 | |
65ebb059 KW |
2474 | /* We dropped off the end of the loop, so have to decrement i to |
2475 | * get back to the value the last time through */ | |
2476 | i--; | |
98994639 | 2477 | |
65ebb059 KW |
2478 | ok = -1; |
2479 | msg = "Failed to fall back to"; | |
2480 | ||
2481 | /* To continue, we should use whatever values we've got */ | |
7d4bcc4a | 2482 | |
e5f10d49 KW |
2483 | for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) { |
2484 | Safefree(curlocales[j]); | |
2485 | curlocales[j] = savepv(do_setlocale_r(categories[j], NULL)); | |
2486 | DEBUG_LOCALE_INIT(categories[j], NULL, curlocales[j]); | |
2487 | } | |
65ebb059 KW |
2488 | } |
2489 | ||
2490 | if (locwarn) { | |
2491 | const char * description; | |
2492 | const char * name = ""; | |
2493 | if (strEQ(trial_locales[i], "C")) { | |
2494 | description = "the standard locale"; | |
2495 | name = "C"; | |
2496 | } | |
7d4bcc4a KW |
2497 | |
2498 | # ifdef SYSTEM_DEFAULT_LOCALE | |
2499 | ||
65ebb059 KW |
2500 | else if (strEQ(trial_locales[i], "")) { |
2501 | description = "the system default locale"; | |
2502 | if (system_default_locale) { | |
2503 | name = system_default_locale; | |
2504 | } | |
2505 | } | |
7d4bcc4a KW |
2506 | |
2507 | # endif /* SYSTEM_DEFAULT_LOCALE */ | |
2508 | ||
65ebb059 KW |
2509 | else { |
2510 | description = "a fallback locale"; | |
2511 | name = trial_locales[i]; | |
2512 | } | |
2513 | if (name && strNE(name, "")) { | |
2514 | PerlIO_printf(Perl_error_log, | |
2515 | "perl: warning: %s %s (\"%s\").\n", msg, description, name); | |
2516 | } | |
2517 | else { | |
2518 | PerlIO_printf(Perl_error_log, | |
2519 | "perl: warning: %s %s.\n", msg, description); | |
2520 | } | |
2521 | } | |
2522 | } /* End of tried to fallback */ | |
98994639 | 2523 | |
e5f10d49 KW |
2524 | /* Done with finding the locales; update our records */ |
2525 | ||
7d4bcc4a KW |
2526 | # ifdef USE_LOCALE_CTYPE |
2527 | ||
948523db | 2528 | new_ctype(curlocales[LC_CTYPE_INDEX]); |
98994639 | 2529 | |
e5f10d49 | 2530 | # endif |
7d4bcc4a KW |
2531 | # ifdef USE_LOCALE_COLLATE |
2532 | ||
948523db | 2533 | new_collate(curlocales[LC_COLLATE_INDEX]); |
98994639 | 2534 | |
e5f10d49 | 2535 | # endif |
7d4bcc4a KW |
2536 | # ifdef USE_LOCALE_NUMERIC |
2537 | ||
948523db | 2538 | new_numeric(curlocales[LC_NUMERIC_INDEX]); |
e5f10d49 KW |
2539 | |
2540 | # endif | |
2541 | ||
948523db | 2542 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) { |
47280b20 KW |
2543 | |
2544 | # if defined(USE_ITHREADS) | |
2545 | ||
2546 | /* This caches whether each category's locale is UTF-8 or not. This | |
2547 | * may involve changing the locale. It is ok to do this at | |
2548 | * initialization time before any threads have started, but not later. | |
2549 | * Caching means that if the program heeds our dictate not to change | |
2550 | * locales in threaded applications, this data will remain valid, and | |
9de6fe47 KW |
2551 | * it may get queried without having to change locales. If the |
2552 | * environment is such that all categories have the same locale, this | |
2553 | * isn't needed, as the code will not change the locale; but this | |
2554 | * handles the uncommon case where the environment has disparate | |
2555 | * locales for the categories */ | |
47280b20 KW |
2556 | (void) _is_cur_LC_category_utf8(categories[i]); |
2557 | ||
2558 | # endif | |
2559 | ||
e5f10d49 KW |
2560 | Safefree(curlocales[i]); |
2561 | } | |
b310b053 | 2562 | |
7d4bcc4a KW |
2563 | # if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE) |
2564 | ||
49c85077 | 2565 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE |
50bf02bd KW |
2566 | * locale is UTF-8. The call to new_ctype() just above has already |
2567 | * calculated the latter value and saved it in PL_in_utf8_CTYPE_locale. If | |
2568 | * both PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE}) | |
2569 | * are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer on | |
2570 | * STDIN, STDOUT, STDERR, _and_ the default open discipline. */ | |
2571 | PL_utf8locale = PL_in_utf8_CTYPE_locale; | |
49c85077 | 2572 | |
a05d7ebb | 2573 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
fde18df1 JH |
2574 | This is an alternative to using the -C command line switch |
2575 | (the -C if present will override this). */ | |
2576 | { | |
dd374669 | 2577 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
a05d7ebb | 2578 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
5a22a2bb NC |
2579 | if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG) |
2580 | PL_utf8cache = -1; | |
b310b053 JH |
2581 | } |
2582 | ||
7d4bcc4a | 2583 | # endif |
7d4bcc4a KW |
2584 | # ifdef __GLIBC__ |
2585 | ||
175c4cf9 | 2586 | Safefree(language); |
7d4bcc4a KW |
2587 | |
2588 | # endif | |
175c4cf9 KW |
2589 | |
2590 | Safefree(lc_all); | |
2591 | Safefree(lang); | |
2592 | ||
e3305790 | 2593 | #endif /* USE_LOCALE */ |
2fcc0ca9 | 2594 | #ifdef DEBUGGING |
7d4bcc4a | 2595 | |
2fcc0ca9 | 2596 | /* So won't continue to output stuff */ |
27cdc72e | 2597 | DEBUG_INITIALIZATION_set(FALSE); |
7d4bcc4a | 2598 | |
2fcc0ca9 KW |
2599 | #endif |
2600 | ||
98994639 HS |
2601 | return ok; |
2602 | } | |
2603 | ||
98994639 HS |
2604 | #ifdef USE_LOCALE_COLLATE |
2605 | ||
a4a439fb | 2606 | char * |
a4a439fb KW |
2607 | Perl__mem_collxfrm(pTHX_ const char *input_string, |
2608 | STRLEN len, /* Length of 'input_string' */ | |
2609 | STRLEN *xlen, /* Set to length of returned string | |
2610 | (not including the collation index | |
2611 | prefix) */ | |
2612 | bool utf8 /* Is the input in UTF-8? */ | |
6696cfa7 | 2613 | ) |
98994639 | 2614 | { |
a4a439fb KW |
2615 | |
2616 | /* _mem_collxfrm() is a bit like strxfrm() but with two important | |
2617 | * differences. First, it handles embedded NULs. Second, it allocates a bit | |
2618 | * more memory than needed for the transformed data itself. The real | |
55e5378d | 2619 | * transformed data begins at offset COLLXFRM_HDR_LEN. *xlen is set to |
a4a439fb KW |
2620 | * the length of that, and doesn't include the collation index size. |
2621 | * Please see sv_collxfrm() to see how this is used. */ | |
2622 | ||
55e5378d KW |
2623 | #define COLLXFRM_HDR_LEN sizeof(PL_collation_ix) |
2624 | ||
6696cfa7 KW |
2625 | char * s = (char *) input_string; |
2626 | STRLEN s_strlen = strlen(input_string); | |
79f120c8 | 2627 | char *xbuf = NULL; |
55e5378d | 2628 | STRLEN xAlloc; /* xalloc is a reserved word in VC */ |
17f41037 | 2629 | STRLEN length_in_chars; |
c664130f | 2630 | bool first_time = TRUE; /* Cleared after first loop iteration */ |
98994639 | 2631 | |
a4a439fb KW |
2632 | PERL_ARGS_ASSERT__MEM_COLLXFRM; |
2633 | ||
2634 | /* Must be NUL-terminated */ | |
2635 | assert(*(input_string + len) == '\0'); | |
7918f24d | 2636 | |
79f120c8 KW |
2637 | /* If this locale has defective collation, skip */ |
2638 | if (PL_collxfrm_base == 0 && PL_collxfrm_mult == 0) { | |
c7202dee KW |
2639 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
2640 | "_mem_collxfrm: locale's collation is defective\n")); | |
79f120c8 KW |
2641 | goto bad; |
2642 | } | |
2643 | ||
6696cfa7 KW |
2644 | /* Replace any embedded NULs with the control that sorts before any others. |
2645 | * This will give as good as possible results on strings that don't | |
2646 | * otherwise contain that character, but otherwise there may be | |
2647 | * less-than-perfect results with that character and NUL. This is | |
fdc080f3 | 2648 | * unavoidable unless we replace strxfrm with our own implementation. */ |
fd43f63c KW |
2649 | if (UNLIKELY(s_strlen < len)) { /* Only execute if there is an embedded |
2650 | NUL */ | |
6696cfa7 KW |
2651 | char * e = s + len; |
2652 | char * sans_nuls; | |
fdc080f3 | 2653 | STRLEN sans_nuls_len; |
94762aa0 | 2654 | int try_non_controls; |
afc4976f KW |
2655 | char this_replacement_char[] = "?\0"; /* Room for a two-byte string, |
2656 | making sure 2nd byte is NUL. | |
2657 | */ | |
2658 | STRLEN this_replacement_len; | |
2659 | ||
1e4c9676 KW |
2660 | /* If we don't know what non-NUL control character sorts lowest for |
2661 | * this locale, find it */ | |
f28f4d2a | 2662 | if (PL_strxfrm_NUL_replacement == '\0') { |
6696cfa7 | 2663 | int j; |
afc4976f | 2664 | char * cur_min_x = NULL; /* The min_char's xfrm, (except it also |
6696cfa7 KW |
2665 | includes the collation index |
2666 | prefixed. */ | |
2667 | ||
91c0e2e0 | 2668 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "Looking to replace NUL\n")); |
94762aa0 KW |
2669 | |
2670 | /* Unlikely, but it may be that no control will work to replace | |
1e4c9676 KW |
2671 | * NUL, in which case we instead look for any character. Controls |
2672 | * are preferred because collation order is, in general, context | |
2673 | * sensitive, with adjoining characters affecting the order, and | |
2674 | * controls are less likely to have such interactions, allowing the | |
2675 | * NUL-replacement to stand on its own. (Another way to look at it | |
2676 | * is to imagine what would happen if the NUL were replaced by a | |
2677 | * combining character; it wouldn't work out all that well.) */ | |
94762aa0 KW |
2678 | for (try_non_controls = 0; |
2679 | try_non_controls < 2; | |
2680 | try_non_controls++) | |
2681 | { | |
d4ff9586 KW |
2682 | /* Look through all legal code points (NUL isn't) */ |
2683 | for (j = 1; j < 256; j++) { | |
2684 | char * x; /* j's xfrm plus collation index */ | |
2685 | STRLEN x_len; /* length of 'x' */ | |
2686 | STRLEN trial_len = 1; | |
736a4fed | 2687 | char cur_source[] = { '\0', '\0' }; |
d4ff9586 | 2688 | |
736a4fed KW |
2689 | /* Skip non-controls the first time through the loop. The |
2690 | * controls in a UTF-8 locale are the L1 ones */ | |
afc4976f KW |
2691 | if (! try_non_controls && (PL_in_utf8_COLLATE_locale) |
2692 | ? ! isCNTRL_L1(j) | |
2693 | : ! isCNTRL_LC(j)) | |
2694 | { | |
d4ff9586 | 2695 | continue; |
6696cfa7 | 2696 | } |
6696cfa7 | 2697 | |
736a4fed KW |
2698 | /* Create a 1-char string of the current code point */ |
2699 | cur_source[0] = (char) j; | |
2700 | ||
d4ff9586 KW |
2701 | /* Then transform it */ |
2702 | x = _mem_collxfrm(cur_source, trial_len, &x_len, | |
afc4976f | 2703 | 0 /* The string is not in UTF-8 */); |
6696cfa7 | 2704 | |
1e4c9676 | 2705 | /* Ignore any character that didn't successfully transform. |
d4ff9586 KW |
2706 | * */ |
2707 | if (! x) { | |
2708 | continue; | |
2709 | } | |
6696cfa7 | 2710 | |
d4ff9586 KW |
2711 | /* If this character's transformation is lower than |
2712 | * the current lowest, this one becomes the lowest */ | |
2713 | if ( cur_min_x == NULL | |
2714 | || strLT(x + COLLXFRM_HDR_LEN, | |
2715 | cur_min_x + COLLXFRM_HDR_LEN)) | |
2716 | { | |
f28f4d2a | 2717 | PL_strxfrm_NUL_replacement = j; |
d4ff9586 | 2718 | cur_min_x = x; |
d4ff9586 KW |
2719 | } |
2720 | else { | |
2721 | Safefree(x); | |
2722 | } | |
1e4c9676 | 2723 | } /* end of loop through all 255 characters */ |
6696cfa7 | 2724 | |
1e4c9676 | 2725 | /* Stop looking if found */ |
94762aa0 KW |
2726 | if (cur_min_x) { |
2727 | break; | |
2728 | } | |
2729 | ||
2730 | /* Unlikely, but possible, if there aren't any controls that | |
2731 | * work in the locale, repeat the loop, looking for any | |
2732 | * character that works */ | |
2733 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
2734 | "_mem_collxfrm: No control worked. Trying non-controls\n")); | |
1e4c9676 | 2735 | } /* End of loop to try first the controls, then any char */ |
6696cfa7 | 2736 | |
94762aa0 KW |
2737 | if (! cur_min_x) { |
2738 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
2739 | "_mem_collxfrm: Couldn't find any character to replace" | |
2740 | " embedded NULs in locale %s with", PL_collation_name)); | |
2741 | goto bad; | |
58eebef2 KW |
2742 | } |
2743 | ||
94762aa0 KW |
2744 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
2745 | "_mem_collxfrm: Replacing embedded NULs in locale %s with " | |
f28f4d2a | 2746 | "0x%02X\n", PL_collation_name, PL_strxfrm_NUL_replacement)); |
94762aa0 | 2747 | |
6696cfa7 | 2748 | Safefree(cur_min_x); |
1e4c9676 | 2749 | } /* End of determining the character that is to replace NULs */ |
afc4976f KW |
2750 | |
2751 | /* If the replacement is variant under UTF-8, it must match the | |
291a84fb | 2752 | * UTF8-ness of the original */ |
f28f4d2a KW |
2753 | if ( ! UVCHR_IS_INVARIANT(PL_strxfrm_NUL_replacement) && utf8) { |
2754 | this_replacement_char[0] = | |
2755 | UTF8_EIGHT_BIT_HI(PL_strxfrm_NUL_replacement); | |
2756 | this_replacement_char[1] = | |
2757 | UTF8_EIGHT_BIT_LO(PL_strxfrm_NUL_replacement); | |
afc4976f KW |
2758 | this_replacement_len = 2; |
2759 | } | |
2760 | else { | |
f28f4d2a | 2761 | this_replacement_char[0] = PL_strxfrm_NUL_replacement; |
afc4976f KW |
2762 | /* this_replacement_char[1] = '\0' was done at initialization */ |
2763 | this_replacement_len = 1; | |
6696cfa7 KW |
2764 | } |
2765 | ||
2766 | /* The worst case length for the replaced string would be if every | |
2767 | * character in it is NUL. Multiply that by the length of each | |
2768 | * replacement, and allow for a trailing NUL */ | |
afc4976f | 2769 | sans_nuls_len = (len * this_replacement_len) + 1; |
fdc080f3 | 2770 | Newx(sans_nuls, sans_nuls_len, char); |
6696cfa7 KW |
2771 | *sans_nuls = '\0'; |
2772 | ||
6696cfa7 KW |
2773 | /* Replace each NUL with the lowest collating control. Loop until have |
2774 | * exhausted all the NULs */ | |
2775 | while (s + s_strlen < e) { | |
6069d6c5 | 2776 | my_strlcat(sans_nuls, s, sans_nuls_len); |
6696cfa7 KW |
2777 | |
2778 | /* Do the actual replacement */ | |
6069d6c5 | 2779 | my_strlcat(sans_nuls, this_replacement_char, sans_nuls_len); |
6696cfa7 KW |
2780 | |
2781 | /* Move past the input NUL */ | |
2782 | s += s_strlen + 1; | |
2783 | s_strlen = strlen(s); | |
2784 | } | |
2785 | ||
2786 | /* And add anything that trails the final NUL */ | |
6069d6c5 | 2787 | my_strlcat(sans_nuls, s, sans_nuls_len); |
6696cfa7 KW |
2788 | |
2789 | /* Switch so below we transform this modified string */ | |
2790 | s = sans_nuls; | |
2791 | len = strlen(s); | |
1e4c9676 | 2792 | } /* End of replacing NULs */ |
6696cfa7 | 2793 | |
a4a439fb KW |
2794 | /* Make sure the UTF8ness of the string and locale match */ |
2795 | if (utf8 != PL_in_utf8_COLLATE_locale) { | |
9de6fe47 | 2796 | /* XXX convert above Unicode to 10FFFF? */ |
a4a439fb KW |
2797 | const char * const t = s; /* Temporary so we can later find where the |
2798 | input was */ | |
2799 | ||
2800 | /* Here they don't match. Change the string's to be what the locale is | |
2801 | * expecting */ | |
2802 | ||
2803 | if (! utf8) { /* locale is UTF-8, but input isn't; upgrade the input */ | |
2804 | s = (char *) bytes_to_utf8((const U8 *) s, &len); | |
2805 | utf8 = TRUE; | |
2806 | } | |
2807 | else { /* locale is not UTF-8; but input is; downgrade the input */ | |
2808 | ||
2809 | s = (char *) bytes_from_utf8((const U8 *) s, &len, &utf8); | |
2810 | ||
2811 | /* If the downgrade was successful we are done, but if the input | |
2812 | * contains things that require UTF-8 to represent, have to do | |
2813 | * damage control ... */ | |
2814 | if (UNLIKELY(utf8)) { | |
2815 | ||
2816 | /* What we do is construct a non-UTF-8 string with | |
2817 | * 1) the characters representable by a single byte converted | |
2818 | * to be so (if necessary); | |
2819 | * 2) and the rest converted to collate the same as the | |
2820 | * highest collating representable character. That makes | |
2821 | * them collate at the end. This is similar to how we | |
2822 | * handle embedded NULs, but we use the highest collating | |
2823 | * code point instead of the smallest. Like the NUL case, | |
2824 | * this isn't perfect, but is the best we can reasonably | |
2825 | * do. Every above-255 code point will sort the same as | |
2826 | * the highest-sorting 0-255 code point. If that code | |
2827 | * point can combine in a sequence with some other code | |
2828 | * points for weight calculations, us changing something to | |
2829 | * be it can adversely affect the results. But in most | |
2830 | * cases, it should work reasonably. And note that this is | |
2831 | * really an illegal situation: using code points above 255 | |
2832 | * on a locale where only 0-255 are valid. If two strings | |
2833 | * sort entirely equal, then the sort order for the | |
2834 | * above-255 code points will be in code point order. */ | |
2835 | ||
2836 | utf8 = FALSE; | |
2837 | ||
2838 | /* If we haven't calculated the code point with the maximum | |
2839 | * collating order for this locale, do so now */ | |
2840 | if (! PL_strxfrm_max_cp) { | |
2841 | int j; | |
2842 | ||
2843 | /* The current transformed string that collates the | |
2844 | * highest (except it also includes the prefixed collation | |
2845 | * index. */ | |
2846 | char * cur_max_x = NULL; | |
2847 | ||
2848 | /* Look through all legal code points (NUL isn't) */ | |
2849 | for (j = 1; j < 256; j++) { | |
2850 | char * x; | |
2851 | STRLEN x_len; | |
736a4fed | 2852 | char cur_source[] = { '\0', '\0' }; |
a4a439fb | 2853 | |
736a4fed KW |
2854 | /* Create a 1-char string of the current code point */ |
2855 | cur_source[0] = (char) j; | |
a4a439fb KW |
2856 | |
2857 | /* Then transform it */ | |
2858 | x = _mem_collxfrm(cur_source, 1, &x_len, FALSE); | |
2859 | ||
2860 | /* If something went wrong (which it shouldn't), just | |
2861 | * ignore this code point */ | |
94762aa0 | 2862 | if (! x) { |
a4a439fb KW |
2863 | continue; |
2864 | } | |
2865 | ||
2866 | /* If this character's transformation is higher than | |
2867 | * the current highest, this one becomes the highest */ | |
2868 | if ( cur_max_x == NULL | |
55e5378d KW |
2869 | || strGT(x + COLLXFRM_HDR_LEN, |
2870 | cur_max_x + COLLXFRM_HDR_LEN)) | |
a4a439fb KW |
2871 | { |
2872 | PL_strxfrm_max_cp = j; | |
2873 | cur_max_x = x; | |
2874 | } | |
2875 | else { | |
2876 | Safefree(x); | |
2877 | } | |
2878 | } | |
2879 | ||
94762aa0 KW |
2880 | if (! cur_max_x) { |
2881 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
2882 | "_mem_collxfrm: Couldn't find any character to" | |
2883 | " replace above-Latin1 chars in locale %s with", | |
2884 | PL_collation_name)); | |
2885 | goto bad; | |
2886 | } | |
2887 | ||
58eebef2 KW |
2888 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
2889 | "_mem_collxfrm: highest 1-byte collating character" | |
2890 | " in locale %s is 0x%02X\n", | |
2891 | PL_collation_name, | |
2892 | PL_strxfrm_max_cp)); | |
58eebef2 | 2893 | |
a4a439fb KW |
2894 | Safefree(cur_max_x); |
2895 | } | |
2896 | ||
2897 | /* Here we know which legal code point collates the highest. | |
2898 | * We are ready to construct the non-UTF-8 string. The length | |
2899 | * will be at least 1 byte smaller than the input string | |
2900 | * (because we changed at least one 2-byte character into a | |
2901 | * single byte), but that is eaten up by the trailing NUL */ | |
2902 | Newx(s, len, char); | |
2903 | ||
2904 | { | |
2905 | STRLEN i; | |
2906 | STRLEN d= 0; | |
042d9e50 | 2907 | char * e = (char *) t + len; |
a4a439fb KW |
2908 | |
2909 | for (i = 0; i < len; i+= UTF8SKIP(t + i)) { | |
2910 | U8 cur_char = t[i]; | |
2911 | if (UTF8_IS_INVARIANT(cur_char)) { | |
2912 | s[d++] = cur_char; | |
2913 | } | |
042d9e50 | 2914 | else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(t + i, e)) { |
a4a439fb KW |
2915 | s[d++] = EIGHT_BIT_UTF8_TO_NATIVE(cur_char, t[i+1]); |
2916 | } | |
2917 | else { /* Replace illegal cp with highest collating | |
2918 | one */ | |
2919 | s[d++] = PL_strxfrm_max_cp; | |
2920 | } | |
2921 | } | |
2922 | s[d++] = '\0'; | |
2923 | Renew(s, d, char); /* Free up unused space */ | |
2924 | } | |
2925 | } | |
2926 | } | |
2927 | ||
2928 | /* Here, we have constructed a modified version of the input. It could | |
2929 | * be that we already had a modified copy before we did this version. | |
2930 | * If so, that copy is no longer needed */ | |
2931 | if (t != input_string) { | |
2932 | Safefree(t); | |
2933 | } | |
2934 | } | |
2935 | ||
17f41037 KW |
2936 | length_in_chars = (utf8) |
2937 | ? utf8_length((U8 *) s, (U8 *) s + len) | |
2938 | : len; | |
2939 | ||
59c018b9 KW |
2940 | /* The first element in the output is the collation id, used by |
2941 | * sv_collxfrm(); then comes the space for the transformed string. The | |
2942 | * equation should give us a good estimate as to how much is needed */ | |
55e5378d | 2943 | xAlloc = COLLXFRM_HDR_LEN |
a4a439fb | 2944 | + PL_collxfrm_base |
17f41037 | 2945 | + (PL_collxfrm_mult * length_in_chars); |
a02a5408 | 2946 | Newx(xbuf, xAlloc, char); |
c7202dee KW |
2947 | if (UNLIKELY(! xbuf)) { |
2948 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
2949 | "_mem_collxfrm: Couldn't malloc %zu bytes\n", xAlloc)); | |
98994639 | 2950 | goto bad; |
c7202dee | 2951 | } |
98994639 | 2952 | |
d35fca5f | 2953 | /* Store the collation id */ |
98994639 | 2954 | *(U32*)xbuf = PL_collation_ix; |
d35fca5f KW |
2955 | |
2956 | /* Then the transformation of the input. We loop until successful, or we | |
2957 | * give up */ | |
4ebeff16 | 2958 | for (;;) { |
1adab0a7 | 2959 | |
55e5378d | 2960 | *xlen = strxfrm(xbuf + COLLXFRM_HDR_LEN, s, xAlloc - COLLXFRM_HDR_LEN); |
4ebeff16 KW |
2961 | |
2962 | /* If the transformed string occupies less space than we told strxfrm() | |
2963 | * was available, it means it successfully transformed the whole | |
2964 | * string. */ | |
55e5378d | 2965 | if (*xlen < xAlloc - COLLXFRM_HDR_LEN) { |
17f41037 | 2966 | |
1adab0a7 KW |
2967 | /* Some systems include a trailing NUL in the returned length. |
2968 | * Ignore it, using a loop in case multiple trailing NULs are | |
2969 | * returned. */ | |
2970 | while ( (*xlen) > 0 | |
2971 | && *(xbuf + COLLXFRM_HDR_LEN + (*xlen) - 1) == '\0') | |
2972 | { | |
2973 | (*xlen)--; | |
2974 | } | |
2975 | ||
17f41037 KW |
2976 | /* If the first try didn't get it, it means our prediction was low. |
2977 | * Modify the coefficients so that we predict a larger value in any | |
2978 | * future transformations */ | |
2979 | if (! first_time) { | |
2980 | STRLEN needed = *xlen + 1; /* +1 For trailing NUL */ | |
2981 | STRLEN computed_guess = PL_collxfrm_base | |
2982 | + (PL_collxfrm_mult * length_in_chars); | |
e1c30f0c KW |
2983 | |
2984 | /* On zero-length input, just keep current slope instead of | |
2985 | * dividing by 0 */ | |
2986 | const STRLEN new_m = (length_in_chars != 0) | |
2987 | ? needed / length_in_chars | |
2988 | : PL_collxfrm_mult; | |
17f41037 KW |
2989 | |
2990 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
b07929e4 KW |
2991 | "%s: %d: initial size of %zu bytes for a length " |
2992 | "%zu string was insufficient, %zu needed\n", | |
17f41037 | 2993 | __FILE__, __LINE__, |
b07929e4 | 2994 | computed_guess, length_in_chars, needed)); |
17f41037 KW |
2995 | |
2996 | /* If slope increased, use it, but discard this result for | |
2997 | * length 1 strings, as we can't be sure that it's a real slope | |
2998 | * change */ | |
2999 | if (length_in_chars > 1 && new_m > PL_collxfrm_mult) { | |
7d4bcc4a KW |
3000 | |
3001 | # ifdef DEBUGGING | |
3002 | ||
17f41037 KW |
3003 | STRLEN old_m = PL_collxfrm_mult; |
3004 | STRLEN old_b = PL_collxfrm_base; | |
7d4bcc4a KW |
3005 | |
3006 | # endif | |
3007 | ||
17f41037 KW |
3008 | PL_collxfrm_mult = new_m; |
3009 | PL_collxfrm_base = 1; /* +1 For trailing NUL */ | |
3010 | computed_guess = PL_collxfrm_base | |
3011 | + (PL_collxfrm_mult * length_in_chars); | |
3012 | if (computed_guess < needed) { | |
3013 | PL_collxfrm_base += needed - computed_guess; | |
3014 | } | |
3015 | ||
3016 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
b07929e4 KW |
3017 | "%s: %d: slope is now %zu; was %zu, base " |
3018 | "is now %zu; was %zu\n", | |
17f41037 | 3019 | __FILE__, __LINE__, |
b07929e4 KW |
3020 | PL_collxfrm_mult, old_m, |
3021 | PL_collxfrm_base, old_b)); | |
17f41037 KW |
3022 | } |
3023 | else { /* Slope didn't change, but 'b' did */ | |
3024 | const STRLEN new_b = needed | |
3025 | - computed_guess | |
3026 | + PL_collxfrm_base; | |
3027 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
b07929e4 | 3028 | "%s: %d: base is now %zu; was %zu\n", |
17f41037 | 3029 | __FILE__, __LINE__, |
b07929e4 | 3030 | new_b, PL_collxfrm_base)); |
17f41037 KW |
3031 | PL_collxfrm_base = new_b; |
3032 | } | |
3033 | } | |
3034 | ||
4ebeff16 KW |
3035 | break; |
3036 | } | |
bb0f664e | 3037 | |
c7202dee KW |
3038 | if (UNLIKELY(*xlen >= PERL_INT_MAX)) { |
3039 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
3040 | "_mem_collxfrm: Needed %zu bytes, max permissible is %u\n", | |
3041 | *xlen, PERL_INT_MAX)); | |
4ebeff16 | 3042 | goto bad; |
c7202dee | 3043 | } |
d35fca5f | 3044 | |
c664130f | 3045 | /* A well-behaved strxfrm() returns exactly how much space it needs |
1adab0a7 KW |
3046 | * (usually not including the trailing NUL) when it fails due to not |
3047 | * enough space being provided. Assume that this is the case unless | |
3048 | * it's been proven otherwise */ | |
c664130f | 3049 | if (LIKELY(PL_strxfrm_is_behaved) && first_time) { |
55e5378d | 3050 | xAlloc = *xlen + COLLXFRM_HDR_LEN + 1; |
c664130f KW |
3051 | } |
3052 | else { /* Here, either: | |
3053 | * 1) The strxfrm() has previously shown bad behavior; or | |
3054 | * 2) It isn't the first time through the loop, which means | |
3055 | * that the strxfrm() is now showing bad behavior, because | |
3056 | * we gave it what it said was needed in the previous | |
3057 | * iteration, and it came back saying it needed still more. | |
3058 | * (Many versions of cygwin fit this. When the buffer size | |
3059 | * isn't sufficient, they return the input size instead of | |
3060 | * how much is needed.) | |
d4ff9586 KW |
3061 | * Increase the buffer size by a fixed percentage and try again. |
3062 | * */ | |
6ddd902c | 3063 | xAlloc += (xAlloc / 4) + 1; |
c664130f | 3064 | PL_strxfrm_is_behaved = FALSE; |
c664130f | 3065 | |
7d4bcc4a KW |
3066 | # ifdef DEBUGGING |
3067 | ||
58eebef2 KW |
3068 | if (DEBUG_Lv_TEST || debug_initialization) { |
3069 | PerlIO_printf(Perl_debug_log, | |
3070 | "_mem_collxfrm required more space than previously calculated" | |
b07929e4 | 3071 | " for locale %s, trying again with new guess=%d+%zu\n", |
58eebef2 | 3072 | PL_collation_name, (int) COLLXFRM_HDR_LEN, |
b07929e4 | 3073 | xAlloc - COLLXFRM_HDR_LEN); |
58eebef2 | 3074 | } |
7d4bcc4a KW |
3075 | |
3076 | # endif | |
3077 | ||
58eebef2 | 3078 | } |
c664130f | 3079 | |
4ebeff16 | 3080 | Renew(xbuf, xAlloc, char); |
c7202dee KW |
3081 | if (UNLIKELY(! xbuf)) { |
3082 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
3083 | "_mem_collxfrm: Couldn't realloc %zu bytes\n", xAlloc)); | |
4ebeff16 | 3084 | goto bad; |
c7202dee | 3085 | } |
c664130f KW |
3086 | |
3087 | first_time = FALSE; | |
4ebeff16 | 3088 | } |
98994639 | 3089 | |
6696cfa7 | 3090 | |
7d4bcc4a KW |
3091 | # ifdef DEBUGGING |
3092 | ||
58eebef2 | 3093 | if (DEBUG_Lv_TEST || debug_initialization) { |
c7202dee KW |
3094 | |
3095 | print_collxfrm_input_and_return(s, s + len, xlen, utf8); | |
3096 | PerlIO_printf(Perl_debug_log, "Its xfrm is:"); | |
7e2f38b2 KW |
3097 | PerlIO_printf(Perl_debug_log, "%s\n", |
3098 | _byte_dump_string((U8 *) xbuf + COLLXFRM_HDR_LEN, | |
3099 | *xlen, 1)); | |
58eebef2 | 3100 | } |
7d4bcc4a KW |
3101 | |
3102 | # endif | |
58eebef2 | 3103 | |
3c5f993e | 3104 | /* Free up unneeded space; retain ehough for trailing NUL */ |
55e5378d | 3105 | Renew(xbuf, COLLXFRM_HDR_LEN + *xlen + 1, char); |
98994639 | 3106 | |
6696cfa7 KW |
3107 | if (s != input_string) { |
3108 | Safefree(s); | |
98994639 HS |
3109 | } |
3110 | ||
98994639 HS |
3111 | return xbuf; |
3112 | ||
3113 | bad: | |
3114 | Safefree(xbuf); | |
6696cfa7 KW |
3115 | if (s != input_string) { |
3116 | Safefree(s); | |
3117 | } | |
98994639 | 3118 | *xlen = 0; |
7d4bcc4a KW |
3119 | |
3120 | # ifdef DEBUGGING | |
3121 | ||
58eebef2 | 3122 | if (DEBUG_Lv_TEST || debug_initialization) { |
c7202dee | 3123 | print_collxfrm_input_and_return(s, s + len, NULL, utf8); |
58eebef2 | 3124 | } |
7d4bcc4a KW |
3125 | |
3126 | # endif | |
3127 | ||
98994639 HS |
3128 | return NULL; |
3129 | } | |
3130 | ||
7d4bcc4a | 3131 | # ifdef DEBUGGING |
c7202dee | 3132 | |
4cbaac56 | 3133 | STATIC void |
c7202dee KW |
3134 | S_print_collxfrm_input_and_return(pTHX_ |
3135 | const char * const s, | |
3136 | const char * const e, | |
3137 | const STRLEN * const xlen, | |
3138 | const bool is_utf8) | |
3139 | { | |
c7202dee KW |
3140 | |
3141 | PERL_ARGS_ASSERT_PRINT_COLLXFRM_INPUT_AND_RETURN; | |
3142 | ||
511e4ff7 DM |
3143 | PerlIO_printf(Perl_debug_log, "_mem_collxfrm[%" UVuf "]: returning ", |
3144 | (UV)PL_collation_ix); | |
c7202dee | 3145 | if (xlen) { |
08b6dc1d | 3146 | PerlIO_printf(Perl_debug_log, "%zu", *xlen); |
c7202dee KW |
3147 | } |
3148 | else { | |
3149 | PerlIO_printf(Perl_debug_log, "NULL"); | |
3150 | } | |
3151 | PerlIO_printf(Perl_debug_log, " for locale '%s', string='", | |
3152 | PL_collation_name); | |
9c8a6dc2 KW |
3153 | print_bytes_for_locale(s, e, is_utf8); |
3154 | ||
3155 | PerlIO_printf(Perl_debug_log, "'\n"); | |
3156 | } | |
3157 | ||
3158 | STATIC void | |
3159 | S_print_bytes_for_locale(pTHX_ | |
3160 | const char * const s, | |
3161 | const char * const e, | |
3162 | const bool is_utf8) | |
3163 | { | |
3164 | const char * t = s; | |
3165 | bool prev_was_printable = TRUE; | |
3166 | bool first_time = TRUE; | |
3167 | ||
3168 | PERL_ARGS_ASSERT_PRINT_BYTES_FOR_LOCALE; | |
c7202dee KW |
3169 | |
3170 | while (t < e) { | |
3171 | UV cp = (is_utf8) | |
3172 | ? utf8_to_uvchr_buf((U8 *) t, e, NULL) | |
3173 | : * (U8 *) t; | |
3174 | if (isPRINT(cp)) { | |
3175 | if (! prev_was_printable) { | |
3176 | PerlIO_printf(Perl_debug_log, " "); | |
3177 | } | |
3178 | PerlIO_printf(Perl_debug_log, "%c", (U8) cp); | |
3179 | prev_was_printable = TRUE; | |
3180 | } | |
3181 | else { | |
3182 | if (! first_time) { | |
3183 | PerlIO_printf(Perl_debug_log, " "); | |
3184 | } | |
147e3846 | 3185 | PerlIO_printf(Perl_debug_log, "%02" UVXf, cp); |
c7202dee KW |
3186 | prev_was_printable = FALSE; |
3187 | } | |
3188 | t += (is_utf8) ? UTF8SKIP(t) : 1; | |
3189 | first_time = FALSE; | |
3190 | } | |
c7202dee KW |
3191 | } |
3192 | ||
7d4bcc4a | 3193 | # endif /* #ifdef DEBUGGING */ |
98994639 | 3194 | #endif /* USE_LOCALE_COLLATE */ |
58eebef2 | 3195 | |
8ef6e574 KW |
3196 | #ifdef USE_LOCALE |
3197 | ||
962aa53f KW |
3198 | STATIC const char * |
3199 | S_switch_category_locale_to_template(pTHX_ const int switch_category, const int template_category, const char * template_locale) | |
3200 | { | |
3201 | /* Changes the locale for LC_'switch_category" to that of | |
3202 | * LC_'template_category', if they aren't already the same. If not NULL, | |
3203 | * 'template_locale' is the locale that 'template_category' is in. | |
3204 | * | |
9de6fe47 KW |
3205 | * Returns a copy of the name of the original locale for 'switch_category' |
3206 | * so can be switched back to with the companion function | |
3207 | * restore_switched_locale(), (NULL if no restoral is necessary.) */ | |
962aa53f KW |
3208 | |
3209 | char * restore_to_locale = NULL; | |
3210 | ||
3211 | if (switch_category == template_category) { /* No changes needed */ | |
3212 | return NULL; | |
3213 | } | |
3214 | ||
3215 | /* Find the original locale of the category we may need to change, so that | |
3216 | * it can be restored to later */ | |
d6189047 KW |
3217 | restore_to_locale = stdize_locale(savepv(do_setlocale_r(switch_category, |
3218 | NULL))); | |
962aa53f KW |
3219 | if (! restore_to_locale) { |
3220 | Perl_croak(aTHX_ | |
3221 | "panic: %s: %d: Could not find current %s locale, errno=%d\n", | |
3222 | __FILE__, __LINE__, category_name(switch_category), errno); | |
3223 | } | |
962aa53f KW |
3224 | |
3225 | /* If the locale of the template category wasn't passed in, find it now */ | |
3226 | if (template_locale == NULL) { | |
3227 | template_locale = do_setlocale_r(template_category, NULL); | |
3228 | if (! template_locale) { | |
3229 | Perl_croak(aTHX_ | |
3230 | "panic: %s: %d: Could not find current %s locale, errno=%d\n", | |
3231 | __FILE__, __LINE__, category_name(template_category), errno); | |
3232 | } | |
3233 | } | |
3234 | ||
3235 | /* It the locales are the same, there's nothing to do */ | |
3236 | if (strEQ(restore_to_locale, template_locale)) { | |
3237 | Safefree(restore_to_locale); | |
3238 | ||
3239 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s locale unchanged as %s\n", | |
3240 | category_name(switch_category), restore_to_locale)); | |
3241 | ||
3242 | return NULL; | |
3243 | } | |
3244 | ||
3245 | /* Finally, change the locale to the template one */ | |
3246 | if (! do_setlocale_r(switch_category, template_locale)) { | |
3247 | Perl_croak(aTHX_ | |
3248 | "panic: %s: %d: Could not change %s locale to %s, errno=%d\n", | |
3249 | __FILE__, __LINE__, category_name(switch_category), | |
3250 | template_locale, errno); | |
3251 | } | |
3252 | ||
3253 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s locale switched to %s\n", | |
3254 | category_name(switch_category), template_locale)); | |
3255 | ||
3256 | return restore_to_locale; | |
3257 | } | |
3258 | ||
3259 | STATIC void | |
3260 | S_restore_switched_locale(pTHX_ const int category, const char * const original_locale) | |
3261 | { | |
9de6fe47 KW |
3262 | /* Restores the locale for LC_'category' to 'original_locale' (which is a |
3263 | * copy that will be freed by this function), or do nothing if the latter | |
3264 | * parameter is NULL */ | |
962aa53f KW |
3265 | |
3266 | if (original_locale == NULL) { | |
3267 | return; | |
3268 | } | |
3269 | ||
3270 | if (! do_setlocale_r(category, original_locale)) { | |
3271 | Perl_croak(aTHX_ | |
3272 | "panic: %s: %d: setlocale %s restore to %s failed, errno=%d\n", | |
3273 | __FILE__, __LINE__, | |
3274 | category_name(category), original_locale, errno); | |
3275 | } | |
3276 | ||
3277 | Safefree(original_locale); | |
3278 | } | |
3279 | ||
c1284011 KW |
3280 | bool |
3281 | Perl__is_cur_LC_category_utf8(pTHX_ int category) | |
7d74bb61 KW |
3282 | { |
3283 | /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE | |
3284 | * otherwise. 'category' may not be LC_ALL. If the platform doesn't have | |
119ee68b | 3285 | * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence |
609548d2 KW |
3286 | * could give the wrong result. The result will very likely be correct for |
3287 | * languages that have commonly used non-ASCII characters, but for notably | |
3288 | * English, it comes down to if the locale's name ends in something like | |
9de6fe47 KW |
3289 | * "UTF-8". It errs on the side of not being a UTF-8 locale. |
3290 | * | |
3291 | * If the platform is early C89, not containing mbtowc(), or we are | |
3292 | * compiled to not pay attention to LC_CTYPE, this employs heuristics. | |
3293 | * These work very well for non-Latin locales or those whose currency | |
3294 | * symbol isn't a '$' nor plain ASCII text. But without LC_CTYPE and at | |
3295 | * least MB_CUR_MAX, English locales with an ASCII currency symbol depend | |
3296 | * on the name containing UTF-8 or not. */ | |
7d74bb61 | 3297 | |
47280b20 | 3298 | /* Name of current locale corresponding to the input category */ |
8de4332b | 3299 | const char *save_input_locale = NULL; |
47280b20 KW |
3300 | |
3301 | bool is_utf8 = FALSE; /* The return value */ | |
7d74bb61 | 3302 | |
47280b20 KW |
3303 | /* The variables below are for the cache of previous lookups using this |
3304 | * function. The cache is a C string, described at the definition for | |
3305 | * 'C_and_POSIX_utf8ness'. | |
3306 | * | |
3307 | * The first part of the cache is fixed, for the C and POSIX locales. The | |
3308 | * varying part starts just after them. */ | |
3309 | char * utf8ness_cache = PL_locale_utf8ness + STRLENs(C_and_POSIX_utf8ness); | |
3310 | ||
3311 | Size_t utf8ness_cache_size; /* Size of the varying portion */ | |
3312 | Size_t input_name_len; /* Length in bytes of save_input_locale */ | |
3313 | Size_t input_name_len_with_overhead; /* plus extra chars used to store | |
3314 | the name in the cache */ | |
3315 | char * delimited; /* The name plus the delimiters used to store | |
3316 | it in the cache */ | |
3317 | char * name_pos; /* position of 'delimited' in the cache, or 0 | |
3318 | if not there */ | |
3319 | ||
3320 | ||
7d4bcc4a KW |
3321 | # ifdef LC_ALL |
3322 | ||
7d74bb61 | 3323 | assert(category != LC_ALL); |
7d4bcc4a KW |
3324 | |
3325 | # endif | |
7d74bb61 | 3326 | |
47280b20 | 3327 | /* Get the desired category's locale */ |
d6189047 | 3328 | save_input_locale = stdize_locale(savepv(do_setlocale_r(category, NULL))); |
7d74bb61 | 3329 | if (! save_input_locale) { |
47280b20 | 3330 | Perl_croak(aTHX_ |
d707d779 KW |
3331 | "panic: %s: %d: Could not find current %s locale, errno=%d\n", |
3332 | __FILE__, __LINE__, category_name(category), errno); | |
7d74bb61 | 3333 | } |
47280b20 | 3334 | |
47280b20 KW |
3335 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3336 | "Current locale for %s is %s\n", | |
3337 | category_name(category), save_input_locale)); | |
3338 | ||
3339 | input_name_len = strlen(save_input_locale); | |
3340 | ||
3341 | /* In our cache, each name is accompanied by two delimiters and a single | |
3342 | * utf8ness digit */ | |
3343 | input_name_len_with_overhead = input_name_len + 3; | |
3344 | ||
3345 | /* Allocate and populate space for a copy of the name surrounded by the | |
3346 | * delimiters */ | |
3347 | Newx(delimited, input_name_len_with_overhead, char); | |
3348 | delimited[0] = UTF8NESS_SEP[0]; | |
3349 | Copy(save_input_locale, delimited + 1, input_name_len, char); | |
3350 | delimited[input_name_len+1] = UTF8NESS_PREFIX[0]; | |
3351 | delimited[input_name_len+2] = '\0'; | |
3352 | ||
3353 | /* And see if that is in the cache */ | |
3354 | name_pos = instr(PL_locale_utf8ness, delimited); | |
3355 | if (name_pos) { | |
3356 | is_utf8 = *(name_pos + input_name_len_with_overhead - 1) - '0'; | |
3357 | ||
3358 | # ifdef DEBUGGING | |
3359 | ||
3360 | if (DEBUG_Lv_TEST || debug_initialization) { | |
3361 | PerlIO_printf(Perl_debug_log, "UTF8ness for locale %s=%d, \n", | |
3362 | save_input_locale, is_utf8); | |
3363 | } | |
3364 | ||
3365 | # endif | |
3366 | ||
3367 | /* And, if not already in that position, move it to the beginning of | |
3368 | * the non-constant portion of the list, since it is the most recently | |
3369 | * used. (We don't have to worry about overflow, since just moving | |
3370 | * existing names around) */ | |
3371 | if (name_pos > utf8ness_cache) { | |
3372 | Move(utf8ness_cache, | |
3373 | utf8ness_cache + input_name_len_with_overhead, | |
3374 | name_pos - utf8ness_cache, char); | |
3375 | Copy(delimited, | |
3376 | utf8ness_cache, | |
3377 | input_name_len_with_overhead - 1, char); | |
3378 | utf8ness_cache[input_name_len_with_overhead - 1] = is_utf8 + '0'; | |
3379 | } | |
3380 | ||
3381 | Safefree(delimited); | |
b07fffd1 | 3382 | Safefree(save_input_locale); |
47280b20 | 3383 | return is_utf8; |
7d74bb61 KW |
3384 | } |
3385 | ||
47280b20 KW |
3386 | /* Here we don't have stored the utf8ness for the input locale. We have to |
3387 | * calculate it */ | |
3388 | ||
94646a69 KW |
3389 | # if defined(USE_LOCALE_CTYPE) \ |
3390 | && ( (defined(HAS_NL_LANGINFO) && defined(CODESET)) \ | |
3391 | || (defined(HAS_MBTOWC) || defined(HAS_MBRTOWC))) | |
7d74bb61 | 3392 | |
0dec74cd | 3393 | { |
962aa53f KW |
3394 | const char *original_ctype_locale |
3395 | = switch_category_locale_to_template(LC_CTYPE, | |
3396 | category, | |
3397 | save_input_locale); | |
69014004 | 3398 | |
7d74bb61 | 3399 | /* Here the current LC_CTYPE is set to the locale of the category whose |
0dec74cd | 3400 | * information is desired. This means that nl_langinfo() and mbtowc() |
1d958db2 | 3401 | * should give the correct results */ |
119ee68b | 3402 | |
0dec74cd KW |
3403 | # ifdef MB_CUR_MAX /* But we can potentially rule out UTF-8ness, avoiding |
3404 | calling the functions if we have this */ | |
3405 | ||
3406 | /* Standard UTF-8 needs at least 4 bytes to represent the maximum | |
3407 | * Unicode code point. */ | |
3408 | ||
de3a3072 KW |
3409 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s: %d: MB_CUR_MAX=%d\n", |
3410 | __FILE__, __LINE__, (int) MB_CUR_MAX)); | |
0dec74cd KW |
3411 | if ((unsigned) MB_CUR_MAX < STRLENs(MAX_UNICODE_UTF8)) { |
3412 | is_utf8 = FALSE; | |
b5b2847c KW |
3413 | restore_switched_locale(LC_CTYPE, original_ctype_locale); |
3414 | goto finish_and_return; | |
0dec74cd KW |
3415 | } |
3416 | ||
3417 | # endif | |
7d4bcc4a KW |
3418 | # if defined(HAS_NL_LANGINFO) && defined(CODESET) |
3419 | ||
0dec74cd KW |
3420 | { /* The task is easiest if the platform has this POSIX 2001 function. |
3421 | Except on some platforms it can wrongly return "", so have to have | |
3422 | a fallback. And it can return that it's UTF-8, even if there are | |
3423 | variances from that. For example, Turkish locales may use the | |
3424 | alternate dotted I rules, and sometimes it appears to be a | |
3425 | defective locale definition. XXX We should probably check for | |
9de6fe47 KW |
3426 | these in the Latin1 range and warn (but on glibc, requires |
3427 | iswalnum() etc. due to their not handling 80-FF correctly */ | |
c70a3e68 KW |
3428 | const char *codeset = my_nl_langinfo(PERL_CODESET, FALSE); |
3429 | /* FALSE => already in dest locale */ | |
119ee68b | 3430 | |
af84e886 | 3431 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
c70a3e68 KW |
3432 | "\tnllanginfo returned CODESET '%s'\n", codeset)); |
3433 | ||
3434 | if (codeset && strNE(codeset, "")) { | |
1d958db2 | 3435 | |
1d075173 KW |
3436 | /* If the implementation of foldEQ() somehow were |
3437 | * to change to not go byte-by-byte, this could | |
3438 | * read past end of string, as only one length is | |
3439 | * checked. But currently, a premature NUL will | |
3440 | * compare false, and it will stop there */ | |
3441 | is_utf8 = cBOOL( foldEQ(codeset, STR_WITH_LEN("UTF-8")) | |
3442 | || foldEQ(codeset, STR_WITH_LEN("UTF8"))); | |
1d958db2 | 3443 | |
69014004 KW |
3444 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3445 | "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n", | |
3446 | codeset, is_utf8)); | |
b5b2847c KW |
3447 | restore_switched_locale(LC_CTYPE, original_ctype_locale); |
3448 | goto finish_and_return; | |
1d958db2 | 3449 | } |
119ee68b KW |
3450 | } |
3451 | ||
7d4bcc4a | 3452 | # endif |
94646a69 | 3453 | # if defined(HAS_MBTOWC) || defined(HAS_MBRTOWC) |
0dec74cd KW |
3454 | /* We can see if this is a UTF-8-like locale if have mbtowc(). It was a |
3455 | * late adder to C89, so very likely to have it. However, testing has | |
3456 | * shown that, like nl_langinfo() above, there are locales that are not | |
3457 | * strictly UTF-8 that this will return that they are */ | |
69014004 | 3458 | |
0dec74cd | 3459 | { |
119ee68b | 3460 | wchar_t wc; |
51fc4b19 | 3461 | int len; |
48015184 | 3462 | dSAVEDERRNO; |
51fc4b19 | 3463 | |
94646a69 KW |
3464 | # if defined(HAS_MBRTOWC) && defined(USE_ITHREADS) |
3465 | ||
3466 | mbstate_t ps; | |
3467 | ||
3468 | # endif | |
3469 | ||
3470 | /* mbrtowc() and mbtowc() convert a byte string to a wide | |
3471 | * character. Feed a byte string to one of them and check that the | |
3472 | * result is the expected Unicode code point */ | |
3473 | ||
3474 | # if defined(HAS_MBRTOWC) && defined(USE_ITHREADS) | |
3475 | /* Prefer this function if available, as it's reentrant */ | |
3476 | ||
3477 | memset(&ps, 0, sizeof(ps));; | |
3478 | PERL_UNUSED_RESULT(mbrtowc(&wc, NULL, 0, &ps)); /* Reset any shift | |
3479 | state */ | |
48015184 | 3480 | SETERRNO(0, 0); |
94646a69 | 3481 | len = mbrtowc(&wc, STR_WITH_LEN(REPLACEMENT_CHARACTER_UTF8), &ps); |
48015184 | 3482 | SAVE_ERRNO; |
94646a69 KW |
3483 | |
3484 | # else | |
0dec74cd | 3485 | |
5a91a58b | 3486 | LOCALE_LOCK; |
856b881c | 3487 | PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */ |
48015184 | 3488 | SETERRNO(0, 0); |
b1d4925c | 3489 | len = mbtowc(&wc, STR_WITH_LEN(REPLACEMENT_CHARACTER_UTF8)); |
48015184 | 3490 | SAVE_ERRNO; |
5a91a58b | 3491 | LOCALE_UNLOCK; |
51fc4b19 | 3492 | |
94646a69 KW |
3493 | # endif |
3494 | ||
48015184 | 3495 | RESTORE_ERRNO; |
af84e886 | 3496 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
0dec74cd | 3497 | "\treturn from mbtowc; len=%d; code_point=%x; errno=%d\n", |
48015184 | 3498 | len, (unsigned int) wc, GET_ERRNO)); |
b1d4925c | 3499 | |
0dec74cd KW |
3500 | is_utf8 = cBOOL( len == STRLENs(REPLACEMENT_CHARACTER_UTF8) |
3501 | && wc == (wchar_t) UNICODE_REPLACEMENT); | |
119ee68b | 3502 | } |
7d4bcc4a | 3503 | |
962aa53f | 3504 | restore_switched_locale(LC_CTYPE, original_ctype_locale); |
47280b20 | 3505 | goto finish_and_return; |
7d74bb61 | 3506 | } |
119ee68b | 3507 | |
0dec74cd KW |
3508 | # endif |
3509 | # else | |
7d74bb61 | 3510 | |
0dec74cd KW |
3511 | /* Here, we must have a C89 compiler that doesn't have mbtowc(). Next |
3512 | * try looking at the currency symbol to see if it disambiguates | |
3513 | * things. Often that will be in the native script, and if the symbol | |
3514 | * isn't in UTF-8, we know that the locale isn't. If it is non-ASCII | |
3515 | * UTF-8, we infer that the locale is too, as the odds of a non-UTF8 | |
3516 | * string being valid UTF-8 are quite small */ | |
fa9b773e | 3517 | |
660ee27b KW |
3518 | # ifdef USE_LOCALE_MONETARY |
3519 | ||
3520 | /* If have LC_MONETARY, we can look at the currency symbol. Often that | |
3521 | * will be in the native script. We do this one first because there is | |
3522 | * just one string to examine, so potentially avoids work */ | |
7d4bcc4a | 3523 | |
9db16864 KW |
3524 | { |
3525 | const char *original_monetary_locale | |
660ee27b KW |
3526 | = switch_category_locale_to_template(LC_MONETARY, |
3527 | category, | |
3528 | save_input_locale); | |
9db16864 | 3529 | bool only_ascii = FALSE; |
660ee27b KW |
3530 | const U8 * currency_string |
3531 | = (const U8 *) my_nl_langinfo(PERL_CRNCYSTR, FALSE); | |
3532 | /* 2nd param not relevant for this item */ | |
3533 | const U8 * first_variant; | |
fa9b773e | 3534 | |
660ee27b KW |
3535 | assert( *currency_string == '-' |
3536 | || *currency_string == '+' | |
3537 | || *currency_string == '.'); | |
97f4de96 | 3538 | |
660ee27b | 3539 | currency_string++; |
fa9b773e | 3540 | |
660ee27b | 3541 | if (is_utf8_invariant_string_loc(currency_string, 0, &first_variant)) |
9db16864 KW |
3542 | { |
3543 | 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)); | |
3544 | only_ascii = TRUE; | |
3545 | } | |
3546 | else { | |
660ee27b | 3547 | is_utf8 = is_strict_utf8_string(first_variant, 0); |
9db16864 | 3548 | } |
fa9b773e | 3549 | |
9db16864 | 3550 | restore_switched_locale(LC_MONETARY, original_monetary_locale); |
fa9b773e | 3551 | |
9db16864 | 3552 | if (! only_ascii) { |
fa9b773e | 3553 | |
9db16864 KW |
3554 | /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; |
3555 | * otherwise assume the locale is UTF-8 if and only if the symbol | |
3556 | * is non-ascii UTF-8. */ | |
af84e886 | 3557 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "\t?Currency symbol for %s is UTF-8=%d\n", |
9db16864 KW |
3558 | save_input_locale, is_utf8)); |
3559 | goto finish_and_return; | |
3560 | } | |
13542a67 | 3561 | } |
fa9b773e | 3562 | |
660ee27b | 3563 | # endif /* USE_LOCALE_MONETARY */ |
7d4bcc4a | 3564 | # if defined(HAS_STRFTIME) && defined(USE_LOCALE_TIME) |
15f7e74e | 3565 | |
9db16864 KW |
3566 | /* Still haven't found a non-ASCII string to disambiguate UTF-8 or not. Try |
3567 | * the names of the months and weekdays, timezone, and am/pm indicator */ | |
3568 | { | |
3569 | const char *original_time_locale | |
3570 | = switch_category_locale_to_template(LC_TIME, | |
3571 | category, | |
3572 | save_input_locale); | |
3573 | int hour = 10; | |
3574 | bool is_dst = FALSE; | |
3575 | int dom = 1; | |
3576 | int month = 0; | |
3577 | int i; | |
3578 | char * formatted_time; | |
3579 | ||
3580 | /* Here the current LC_TIME is set to the locale of the category | |
3581 | * whose information is desired. Look at all the days of the week and | |
3582 | * month names, and the timezone and am/pm indicator for UTF-8 variant | |
3583 | * characters. The first such a one found will tell us if the locale | |
3584 | * is UTF-8 or not */ | |
3585 | ||
3586 | for (i = 0; i < 7 + 12; i++) { /* 7 days; 12 months */ | |
3587 | formatted_time = my_strftime("%A %B %Z %p", | |
3588 | 0, 0, hour, dom, month, 2012 - 1900, 0, 0, is_dst); | |
3589 | if ( ! formatted_time | |
3590 | || is_utf8_invariant_string((U8 *) formatted_time, 0)) | |
3591 | { | |
15f7e74e | 3592 | |
9db16864 KW |
3593 | /* Here, we didn't find a non-ASCII. Try the next time through |
3594 | * with the complemented dst and am/pm, and try with the next | |
3595 | * weekday. After we have gotten all weekdays, try the next | |
3596 | * month */ | |
3597 | is_dst = ! is_dst; | |
3598 | hour = (hour + 12) % 24; | |
3599 | dom++; | |
3600 | if (i > 6) { | |
3601 | month++; | |
3602 | } | |
3603 | continue; | |
15f7e74e | 3604 | } |
9db16864 KW |
3605 | |
3606 | /* Here, we have a non-ASCII. Return TRUE is it is valid UTF8; | |
3607 | * false otherwise. But first, restore LC_TIME to its original | |
3608 | * locale if we changed it */ | |
3609 | restore_switched_locale(LC_TIME, original_time_locale); | |
3610 | ||
af84e886 | 3611 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "\t?time-related strings for %s are UTF-8=%d\n", |
9db16864 KW |
3612 | save_input_locale, |
3613 | is_utf8_string((U8 *) formatted_time, 0))); | |
3614 | is_utf8 = is_utf8_string((U8 *) formatted_time, 0); | |
3615 | goto finish_and_return; | |
15f7e74e KW |
3616 | } |
3617 | ||
9db16864 KW |
3618 | /* Falling off the end of the loop indicates all the names were just |
3619 | * ASCII. Go on to the next test. If we changed it, restore LC_TIME | |
3620 | * to its original locale */ | |
962aa53f | 3621 | restore_switched_locale(LC_TIME, original_time_locale); |
af84e886 | 3622 | DEBUG_Lv(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)); |
15f7e74e KW |
3623 | } |
3624 | ||
7d4bcc4a | 3625 | # endif |
15f7e74e | 3626 | |
7d4bcc4a | 3627 | # if 0 && defined(USE_LOCALE_MESSAGES) && defined(HAS_SYS_ERRLIST) |
855aeb93 | 3628 | |
9db16864 KW |
3629 | /* This code is ifdefd out because it was found to not be necessary in testing |
3630 | * on our dromedary test machine, which has over 700 locales. There, this | |
3631 | * added no value to looking at the currency symbol and the time strings. I | |
3632 | * left it in so as to avoid rewriting it if real-world experience indicates | |
3633 | * that dromedary is an outlier. Essentially, instead of returning abpve if we | |
3634 | * haven't found illegal utf8, we continue on and examine all the strerror() | |
3635 | * messages on the platform for utf8ness. If all are ASCII, we still don't | |
3636 | * know the answer; but otherwise we have a pretty good indication of the | |
3637 | * utf8ness. The reason this doesn't help much is that the messages may not | |
3638 | * have been translated into the locale. The currency symbol and time strings | |
3639 | * are much more likely to have been translated. */ | |
3640 | { | |
3641 | int e; | |
3642 | bool non_ascii = FALSE; | |
3643 | const char *original_messages_locale | |
3644 | = switch_category_locale_to_template(LC_MESSAGES, | |
3645 | category, | |
3646 | save_input_locale); | |
3647 | const char * errmsg = NULL; | |
3648 | ||
3649 | /* Here the current LC_MESSAGES is set to the locale of the category | |
3650 | * whose information is desired. Look through all the messages. We | |
3651 | * can't use Strerror() here because it may expand to code that | |
3652 | * segfaults in miniperl */ | |
3653 | ||
3654 | for (e = 0; e <= sys_nerr; e++) { | |
3655 | errno = 0; | |
3656 | errmsg = sys_errlist[e]; | |
3657 | if (errno || !errmsg) { | |
3658 | break; | |
3659 | } | |
3660 | errmsg = savepv(errmsg); | |
3661 | if (! is_utf8_invariant_string((U8 *) errmsg, 0)) { | |
3662 | non_ascii = TRUE; | |
3663 | is_utf8 = is_utf8_string((U8 *) errmsg, 0); | |
3664 | break; | |
3665 | } | |
855aeb93 | 3666 | } |
9db16864 | 3667 | Safefree(errmsg); |
855aeb93 | 3668 | |
9db16864 | 3669 | restore_switched_locale(LC_MESSAGES, original_messages_locale); |
855aeb93 | 3670 | |
9db16864 | 3671 | if (non_ascii) { |
5857e934 | 3672 | |
9db16864 KW |
3673 | /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid, |
3674 | * any non-ascii means it is one; otherwise we assume it isn't */ | |
af84e886 | 3675 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "\t?error messages for %s are UTF-8=%d\n", |
9db16864 KW |
3676 | save_input_locale, |
3677 | is_utf8)); | |
3678 | goto finish_and_return; | |
3679 | } | |
855aeb93 | 3680 | |
9db16864 KW |
3681 | 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)); |
3682 | } | |
855aeb93 | 3683 | |
7d4bcc4a | 3684 | # endif |
0dec74cd | 3685 | # ifndef EBCDIC /* On os390, even if the name ends with "UTF-8', it isn't a |
92c0a900 | 3686 | UTF-8 locale */ |
7d4bcc4a | 3687 | |
97f4de96 KW |
3688 | /* As a last resort, look at the locale name to see if it matches |
3689 | * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the | |
3690 | * return of setlocale(), is actually defined to be opaque, so we can't | |
3691 | * really rely on the absence of various substrings in the name to indicate | |
3692 | * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to | |
3693 | * be a UTF-8 locale. Similarly for the other common names */ | |
3694 | ||
0dec74cd | 3695 | { |
c36d8df8 | 3696 | const Size_t final_pos = strlen(save_input_locale) - 1; |
0dec74cd | 3697 | |
c36d8df8 KW |
3698 | if (final_pos >= 3) { |
3699 | const char *name = save_input_locale; | |
97f4de96 | 3700 | |
c36d8df8 KW |
3701 | /* Find next 'U' or 'u' and look from there */ |
3702 | while ((name += strcspn(name, "Uu") + 1) | |
3703 | <= save_input_locale + final_pos - 2) | |
97f4de96 | 3704 | { |
c36d8df8 KW |
3705 | if ( isALPHA_FOLD_NE(*name, 't') |
3706 | || isALPHA_FOLD_NE(*(name + 1), 'f')) | |
3707 | { | |
3708 | continue; | |
3709 | } | |
3710 | name += 2; | |
3711 | if (*(name) == '-') { | |
3712 | if ((name > save_input_locale + final_pos - 1)) { | |
3713 | break; | |
3714 | } | |
3715 | name++; | |
3716 | } | |
3717 | if (*(name) == '8') { | |
3718 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
3719 | "Locale %s ends with UTF-8 in name\n", | |
3720 | save_input_locale)); | |
3721 | is_utf8 = TRUE; | |
3722 | goto finish_and_return; | |
97f4de96 | 3723 | } |
97f4de96 | 3724 | } |
c36d8df8 KW |
3725 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3726 | "Locale %s doesn't end with UTF-8 in name\n", | |
3727 | save_input_locale)); | |
97f4de96 | 3728 | } |
97f4de96 | 3729 | |
4d8d465a | 3730 | # ifdef WIN32 |
7d4bcc4a | 3731 | |
c36d8df8 KW |
3732 | /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */ |
3733 | if (memENDs(save_input_locale, final_pos, "65001")) { | |
3734 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
8a0832a1 | 3735 | "Locale %s ends with 65001 in name, is UTF-8 locale\n", |
97f4de96 | 3736 | save_input_locale)); |
c36d8df8 KW |
3737 | is_utf8 = TRUE; |
3738 | goto finish_and_return; | |
3739 | } | |
0dec74cd | 3740 | } |
7d4bcc4a | 3741 | |