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