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