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'. | |
e9bc6d6b KW |
35 | * |
36 | * This code now has multi-thread-safe locale handling on systems that support | |
37 | * that. This is completely transparent to most XS code. On earlier systems, | |
38 | * it would be possible to emulate thread-safe locales, but this likely would | |
39 | * involve a lot of locale switching, and would require XS code changes. | |
40 | * Macros could be written so that the code wouldn't have to know which type of | |
fe1c1494 KW |
41 | * system is being used. |
42 | * | |
43 | * Table-driven code is used for simplicity and clarity, as many operations | |
44 | * differ only in which category is being worked on. However the system | |
45 | * categories need not be small contiguous integers, so do not lend themselves | |
46 | * to table lookup. Instead we have created our own equivalent values which | |
47 | * are all small contiguous non-negative integers, and translation functions | |
48 | * between the two sets. For category 'LC_foo', the name of our index is | |
49 | * LC_foo_INDEX_. Various parallel tables, indexed by these, are used. | |
50 | * | |
51 | * Many of the macros and functions in this file have one of the suffixes '_c', | |
52 | * '_r', or '_i'. khw found these useful in remembering what type of locale | |
53 | * category to use as their parameter. '_r' takes an int category number as | |
54 | * passed to setlocale(), like LC_ALL, LC_CTYPE, etc. The 'r' indicates that | |
55 | * the value isn't known until runtime. '_c' also indicates such a category | |
56 | * number, but its value is known at compile time. These are both converted | |
57 | * into unsigned indexes into various tables of category information, where the | |
58 | * real work is generally done. The tables are generated at compile-time based | |
59 | * on platform characteristics and Configure options. They hide from the code | |
60 | * many of the vagaries of the different locale implementations out there. You | |
61 | * may have already guessed that '_i' indicates the parameter is such an | |
62 | * unsigned index. Converting from '_r' to '_i' requires run-time lookup. | |
63 | * '_c' is used to get cpp to do this at compile time. To avoid the runtime | |
64 | * expense, the code is structured to use '_r' at the API level, and once | |
65 | * converted, everything possible is done using the table indexes. | |
66 | * | |
67 | * On unthreaded perls, most operations expand out to just the basic | |
68 | * setlocale() calls. The same is true on threaded perls on modern Windows | |
69 | * systems where the same API, after set up, is used for thread-safe locale | |
70 | * handling. On other systems, there is a completely different API, specified | |
71 | * in POSIX 2008, to do thread-safe locales. On these systems, our | |
72 | * emulate_setlocale_i() function is used to hide the different API from the | |
73 | * outside. This makes it completely transparent to most XS code. | |
74 | * | |
75 | * A huge complicating factor is that the LC_NUMERIC category is normally held | |
76 | * in the C locale, except during those relatively rare times when it needs to | |
77 | * be in the underlying locale. There is a bunch of code to accomplish this, | |
78 | * and to allow easy switches from one state to the other. | |
79 | * | |
80 | * z/OS (os390) is an outlier. Locales really don't work under threads when | |
81 | * either the radix character isn't a dot, or attempts are made to change | |
82 | * locales after the first thread is created. The reason is that IBM has made | |
83 | * it thread-safe by refusing to change locales (returning failure if | |
84 | * attempted) any time after an application has called pthread_create() to | |
85 | * create another thread. The expectation is that an application will set up | |
86 | * its locale information before the first fork, and be stable thereafter. But | |
87 | * perl toggles LC_NUMERIC if the locale's radix character isn't a dot, as do | |
88 | * the other toggles, which are less common. | |
166f8a29 DM |
89 | */ |
90 | ||
4767484d KW |
91 | /* If the environment says to, we can output debugging information during |
92 | * initialization. This is done before option parsing, and before any thread | |
93 | * creation, so can be a file-level static. (Must come before #including | |
94 | * perl.h) */ | |
95 | #ifdef DEBUGGING | |
96 | static int debug_initialization = 0; | |
97 | # define DEBUG_INITIALIZATION_set(v) (debug_initialization = v) | |
98 | # define DEBUG_LOCALE_INITIALIZATION_ debug_initialization | |
99 | #else | |
100 | # define debug_initialization 0 | |
101 | # define DEBUG_INITIALIZATION_set(v) | |
102 | #endif | |
103 | ||
98994639 HS |
104 | #include "EXTERN.h" |
105 | #define PERL_IN_LOCALE_C | |
f7416781 | 106 | #include "perl_langinfo.h" |
98994639 HS |
107 | #include "perl.h" |
108 | ||
a4af207c JH |
109 | #include "reentr.h" |
110 | ||
0dec74cd KW |
111 | #ifdef I_WCHAR |
112 | # include <wchar.h> | |
113 | #endif | |
5b64f24c KW |
114 | #ifdef I_WCTYPE |
115 | # include <wctype.h> | |
116 | #endif | |
0dec74cd | 117 | |
48015184 KW |
118 | |
119 | /* Returns the Unix errno portion; ignoring any others. This is a macro here | |
120 | * instead of putting it into perl.h, because unclear to khw what should be | |
121 | * done generally. */ | |
122 | #define GET_ERRNO saved_errno | |
123 | ||
63e5b0d7 KW |
124 | /* Is the C string input 'name' "C" or "POSIX"? If so, and 'name' is the |
125 | * return of setlocale(), then this is extremely likely to be the C or POSIX | |
126 | * locale. However, the output of setlocale() is documented to be opaque, but | |
127 | * the odds are extremely small that it would return these two strings for some | |
128 | * other locale. Note that VMS in these two locales includes many non-ASCII | |
129 | * characters as controls and punctuation (below are hex bytes): | |
130 | * cntrl: 84-97 9B-9F | |
131 | * punct: A1-A3 A5 A7-AB B0-B3 B5-B7 B9-BD BF-CF D1-DD DF-EF F1-FD | |
132 | * Oddly, none there are listed as alphas, though some represent alphabetics | |
133 | * http://www.nntp.perl.org/group/perl.perl5.porters/2013/02/msg198753.html */ | |
134 | #define isNAME_C_OR_POSIX(name) \ | |
135 | ( (name) != NULL \ | |
136 | && (( *(name) == 'C' && (*(name + 1)) == '\0') \ | |
137 | || strEQ((name), "POSIX"))) | |
138 | ||
8ef6e574 KW |
139 | #ifdef USE_LOCALE |
140 | ||
47280b20 KW |
141 | /* This code keeps a LRU cache of the UTF-8ness of the locales it has so-far |
142 | * looked up. This is in the form of a C string: */ | |
143 | ||
fe1c1494 KW |
144 | # define UTF8NESS_SEP "\v" |
145 | # define UTF8NESS_PREFIX "\f" | |
47280b20 KW |
146 | |
147 | /* So, the string looks like: | |
98994639 | 148 | * |
47280b20 | 149 | * \vC\a0\vPOSIX\a0\vam_ET\a0\vaf_ZA.utf8\a1\ven_US.UTF-8\a1\0 |
98994639 | 150 | * |
47280b20 KW |
151 | * where the digit 0 after the \a indicates that the locale starting just |
152 | * after the preceding \v is not UTF-8, and the digit 1 mean it is. */ | |
153 | ||
154 | STATIC_ASSERT_DECL(STRLENs(UTF8NESS_SEP) == 1); | |
155 | STATIC_ASSERT_DECL(STRLENs(UTF8NESS_PREFIX) == 1); | |
156 | ||
fe1c1494 | 157 | # define C_and_POSIX_utf8ness UTF8NESS_SEP "C" UTF8NESS_PREFIX "0" \ |
47280b20 KW |
158 | UTF8NESS_SEP "POSIX" UTF8NESS_PREFIX "0" |
159 | ||
160 | /* The cache is initialized to C_and_POSIX_utf8ness at start up. These are | |
161 | * kept there always. The remining portion of the cache is LRU, with the | |
162 | * oldest looked-up locale at the tail end */ | |
163 | ||
d188fe6d KW |
164 | # ifdef DEBUGGING |
165 | # define setlocale_debug_string_c(category, locale, result) \ | |
166 | setlocale_debug_string_i(category##_INDEX_, locale, result) | |
167 | # define setlocale_debug_string_r(category, locale, result) \ | |
168 | setlocale_debug_string_i(get_category_index(category, locale), \ | |
169 | locale, result) | |
170 | # endif | |
171 | ||
fe1c1494 KW |
172 | /* Two parallel arrays indexed by our mapping of category numbers into small |
173 | * non-negative indexes; first the locale categories Perl uses on this system, | |
174 | * used to do the inverse mapping. The second array is their names. These | |
175 | * arrays are in mostly arbitrary order. */ | |
e5f10d49 | 176 | |
e129f2eb | 177 | STATIC const int categories[] = { |
e5f10d49 KW |
178 | |
179 | # ifdef USE_LOCALE_NUMERIC | |
180 | LC_NUMERIC, | |
181 | # endif | |
182 | # ifdef USE_LOCALE_CTYPE | |
183 | LC_CTYPE, | |
184 | # endif | |
185 | # ifdef USE_LOCALE_COLLATE | |
186 | LC_COLLATE, | |
187 | # endif | |
188 | # ifdef USE_LOCALE_TIME | |
189 | LC_TIME, | |
190 | # endif | |
191 | # ifdef USE_LOCALE_MESSAGES | |
192 | LC_MESSAGES, | |
193 | # endif | |
194 | # ifdef USE_LOCALE_MONETARY | |
195 | LC_MONETARY, | |
196 | # endif | |
9821811f KW |
197 | # ifdef USE_LOCALE_ADDRESS |
198 | LC_ADDRESS, | |
199 | # endif | |
200 | # ifdef USE_LOCALE_IDENTIFICATION | |
201 | LC_IDENTIFICATION, | |
202 | # endif | |
203 | # ifdef USE_LOCALE_MEASUREMENT | |
204 | LC_MEASUREMENT, | |
205 | # endif | |
206 | # ifdef USE_LOCALE_PAPER | |
207 | LC_PAPER, | |
208 | # endif | |
209 | # ifdef USE_LOCALE_TELEPHONE | |
210 | LC_TELEPHONE, | |
211 | # endif | |
36dbc955 KW |
212 | # ifdef USE_LOCALE_SYNTAX |
213 | LC_SYNTAX, | |
214 | # endif | |
215 | # ifdef USE_LOCALE_TOD | |
216 | LC_TOD, | |
217 | # endif | |
e5f10d49 KW |
218 | # ifdef LC_ALL |
219 | LC_ALL, | |
220 | # endif | |
7e1cacef KW |
221 | |
222 | /* Placeholder as a precaution if code fails to check the return of | |
223 | * get_category_index(), which returns this element to indicate an error */ | |
224 | -1 | |
e5f10d49 KW |
225 | }; |
226 | ||
227 | /* The top-most real element is LC_ALL */ | |
228 | ||
e129f2eb | 229 | STATIC const char * const category_names[] = { |
e5f10d49 KW |
230 | |
231 | # ifdef USE_LOCALE_NUMERIC | |
232 | "LC_NUMERIC", | |
233 | # endif | |
234 | # ifdef USE_LOCALE_CTYPE | |
235 | "LC_CTYPE", | |
236 | # endif | |
237 | # ifdef USE_LOCALE_COLLATE | |
238 | "LC_COLLATE", | |
239 | # endif | |
240 | # ifdef USE_LOCALE_TIME | |
241 | "LC_TIME", | |
242 | # endif | |
243 | # ifdef USE_LOCALE_MESSAGES | |
244 | "LC_MESSAGES", | |
245 | # endif | |
246 | # ifdef USE_LOCALE_MONETARY | |
247 | "LC_MONETARY", | |
248 | # endif | |
9821811f KW |
249 | # ifdef USE_LOCALE_ADDRESS |
250 | "LC_ADDRESS", | |
251 | # endif | |
252 | # ifdef USE_LOCALE_IDENTIFICATION | |
253 | "LC_IDENTIFICATION", | |
254 | # endif | |
255 | # ifdef USE_LOCALE_MEASUREMENT | |
256 | "LC_MEASUREMENT", | |
257 | # endif | |
258 | # ifdef USE_LOCALE_PAPER | |
259 | "LC_PAPER", | |
260 | # endif | |
261 | # ifdef USE_LOCALE_TELEPHONE | |
262 | "LC_TELEPHONE", | |
263 | # endif | |
36dbc955 KW |
264 | # ifdef USE_LOCALE_SYNTAX |
265 | "LC_SYNTAX", | |
266 | # endif | |
267 | # ifdef USE_LOCALE_TOD | |
268 | "LC_TOD", | |
269 | # endif | |
e5f10d49 KW |
270 | # ifdef LC_ALL |
271 | "LC_ALL", | |
272 | # endif | |
7e1cacef KW |
273 | |
274 | /* Placeholder as a precaution if code fails to check the return of | |
275 | * get_category_index(), which returns this element to indicate an error */ | |
276 | NULL | |
277 | }; | |
e5f10d49 | 278 | |
c7dc1ea8 KW |
279 | /* A few categories require additional setup when they are changed. This table |
280 | * points to the functions that do that setup */ | |
281 | STATIC void (*update_functions[]) (pTHX_ const char *) = { | |
282 | # ifdef USE_LOCALE_NUMERIC | |
283 | S_new_numeric, | |
284 | # endif | |
285 | # ifdef USE_LOCALE_CTYPE | |
286 | S_new_ctype, | |
287 | # endif | |
288 | # ifdef USE_LOCALE_COLLATE | |
289 | S_new_collate, | |
290 | # endif | |
291 | # ifdef USE_LOCALE_TIME | |
292 | NULL, | |
293 | # endif | |
294 | # ifdef USE_LOCALE_MESSAGES | |
295 | NULL, | |
296 | # endif | |
297 | # ifdef USE_LOCALE_MONETARY | |
298 | NULL, | |
299 | # endif | |
300 | # ifdef USE_LOCALE_ADDRESS | |
301 | NULL, | |
302 | # endif | |
303 | # ifdef USE_LOCALE_IDENTIFICATION | |
304 | NULL, | |
305 | # endif | |
306 | # ifdef USE_LOCALE_MEASUREMENT | |
307 | NULL, | |
308 | # endif | |
309 | # ifdef USE_LOCALE_PAPER | |
310 | NULL, | |
311 | # endif | |
312 | # ifdef USE_LOCALE_TELEPHONE | |
313 | NULL, | |
314 | # endif | |
315 | # ifdef USE_LOCALE_SYNTAX | |
316 | NULL, | |
317 | # endif | |
318 | # ifdef USE_LOCALE_TOD | |
319 | NULL, | |
320 | # endif | |
321 | /* No harm done to have this even without an LC_ALL */ | |
322 | S_new_LC_ALL, | |
323 | ||
324 | /* Placeholder as a precaution if code fails to check the return of | |
325 | * get_category_index(), which returns this element to indicate an error */ | |
326 | NULL | |
327 | }; | |
328 | ||
e5f10d49 KW |
329 | # ifdef LC_ALL |
330 | ||
331 | /* On systems with LC_ALL, it is kept in the highest index position. (-2 | |
332 | * to account for the final unused placeholder element.) */ | |
333 | # define NOMINAL_LC_ALL_INDEX (C_ARRAY_LENGTH(categories) - 2) | |
e5f10d49 KW |
334 | # else |
335 | ||
336 | /* On systems without LC_ALL, we pretend it is there, one beyond the real | |
337 | * top element, hence in the unused placeholder element. */ | |
338 | # define NOMINAL_LC_ALL_INDEX (C_ARRAY_LENGTH(categories) - 1) | |
e5f10d49 KW |
339 | # endif |
340 | ||
341 | /* Pretending there is an LC_ALL element just above allows us to avoid most | |
342 | * special cases. Most loops through these arrays in the code below are | |
343 | * written like 'for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++)'. They will work | |
344 | * on either type of system. But the code must be written to not access the | |
4fd76d49 KW |
345 | * element at 'LC_ALL_INDEX_' except on platforms that have it. This can be |
346 | * checked for at compile time by using the #define LC_ALL_INDEX_ which is only | |
948523db | 347 | * defined if we do have LC_ALL. */ |
e5f10d49 | 348 | |
7e1cacef KW |
349 | STATIC unsigned int |
350 | S_get_category_index(const int category, const char * locale) | |
351 | { | |
352 | /* Given a category, return the equivalent internal index we generally use | |
353 | * instead. | |
354 | * | |
355 | * 'locale' is for use in any generated diagnostics, and may be NULL | |
356 | * | |
357 | * Some sort of hash could be used instead of this loop, but the number of | |
358 | * elements is so far at most 12 */ | |
359 | ||
360 | unsigned int i; | |
fef64ad9 | 361 | const char * conditional_warn_text = "; can't set it to "; |
7e1cacef KW |
362 | |
363 | PERL_ARGS_ASSERT_GET_CATEGORY_INDEX; | |
364 | ||
365 | # ifdef LC_ALL | |
366 | for (i = 0; i <= LC_ALL_INDEX_; i++) | |
367 | # else | |
368 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) | |
369 | # endif | |
370 | { | |
371 | if (category == categories[i]) { | |
372 | dTHX_DEBUGGING; | |
373 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
374 | "%s:%d: index of category %d (%s) is %d\n", | |
375 | __FILE__, __LINE__, category, category_names[i], i)); | |
376 | return i; | |
377 | } | |
378 | } | |
379 | ||
380 | /* Here, we don't know about this category, so can't handle it. */ | |
fef64ad9 | 381 | |
7e1cacef | 382 | if (! locale) { |
fef64ad9 KW |
383 | locale = ""; |
384 | conditional_warn_text = ""; | |
7e1cacef | 385 | } |
fef64ad9 KW |
386 | |
387 | /* diag_listed_as: Unknown locale category %d; can't set it to %s */ | |
7e1cacef | 388 | Perl_warner_nocontext(packWARN(WARN_LOCALE), |
fef64ad9 KW |
389 | "Unknown locale category %d%s%s", |
390 | category, conditional_warn_text, locale); | |
7e1cacef | 391 | |
01c966a3 KW |
392 | # ifdef EINVAL |
393 | ||
394 | SETERRNO(EINVAL, LIB_INVARG); | |
395 | ||
396 | # endif | |
397 | ||
7e1cacef KW |
398 | /* Return an out-of-bounds value */ |
399 | return NOMINAL_LC_ALL_INDEX + 1; | |
400 | } | |
401 | ||
b09aaf40 KW |
402 | STATIC const char * |
403 | S_category_name(const int category) | |
404 | { | |
fef64ad9 | 405 | unsigned int index; |
b09aaf40 | 406 | |
fef64ad9 | 407 | index = get_category_index(category, NULL); |
b09aaf40 | 408 | |
fef64ad9 KW |
409 | if (index <= NOMINAL_LC_ALL_INDEX) { |
410 | return category_names[index]; | |
b09aaf40 KW |
411 | } |
412 | ||
fef64ad9 | 413 | return Perl_form_nocontext("%d (unknown)", category); |
b09aaf40 KW |
414 | } |
415 | ||
948523db | 416 | #endif /* ifdef USE_LOCALE */ |
8ef6e574 | 417 | |
94202fb8 KW |
418 | #define setlocale_failure_panic_c( \ |
419 | cat, current, failed, caller_0_line, caller_1_line) \ | |
420 | setlocale_failure_panic_i(cat##_INDEX_, current, failed, \ | |
421 | caller_0_line, caller_1_line) | |
422 | ||
fe1c1494 KW |
423 | /* porcelain_setlocale() presents a consistent POSIX-compliant interface to |
424 | * setlocale(). Windows requres a customized base-level setlocale() */ | |
e9bc6d6b | 425 | #ifdef WIN32 |
fb24522c | 426 | # define porcelain_setlocale(cat, locale) win32_setlocale(cat, locale) |
e9bc6d6b | 427 | #else |
d19f9f01 KW |
428 | # define porcelain_setlocale(cat, locale) \ |
429 | ((const char *) setlocale(cat, locale)) | |
e9bc6d6b KW |
430 | #endif |
431 | ||
3980cddb KW |
432 | /* The next many lines form a layer above the close-to-the-metal 'porcelain' |
433 | * macros. They are used to present a uniform API to the rest of the code in | |
434 | * this file in spite of the disparate underlying implementations. */ | |
fe1c1494 | 435 | |
e9bc6d6b KW |
436 | #ifndef USE_POSIX_2008_LOCALE |
437 | ||
fe1c1494 KW |
438 | /* For non-threaded perls (which we are not to use the POSIX 2008 API on), or a |
439 | * thread-safe Windows one in which threading is invisible to us, the added | |
440 | * layer just calls the base-level functions. See the introductory comments in | |
441 | * this file for the meaning of the suffixes '_c', '_r', '_i'. */ | |
442 | ||
3980cddb KW |
443 | # define setlocale_c(cat, locale) porcelain_setlocale(cat, locale) |
444 | # define setlocale_i(i, locale) setlocale_c(categories[i], locale) | |
445 | # define setlocale_r(cat, locale) setlocale_c(cat, locale) | |
446 | ||
94202fb8 KW |
447 | # define void_setlocale_i(i, locale) \ |
448 | STMT_START { \ | |
449 | if (! porcelain_setlocale(categories[i], locale)) { \ | |
450 | setlocale_failure_panic_i(i, NULL, locale, __LINE__, 0); \ | |
451 | NOT_REACHED; /* NOTREACHED */ \ | |
452 | } \ | |
453 | } STMT_END | |
454 | # define void_setlocale_c(cat, locale) \ | |
455 | void_setlocale_i(cat##_INDEX_, locale) | |
456 | # define void_setlocale_r(cat, locale) \ | |
457 | void_setlocale_i(get_category_index(cat, locale), locale) | |
3980cddb KW |
458 | |
459 | # define bool_setlocale_c(cat, locale) cBOOL(setlocale_c(cat, locale)) | |
460 | # define bool_setlocale_i(i, locale) cBOOL(setlocale_i(i, locale)) | |
461 | # define bool_setlocale_r(cat, locale) cBOOL(setlocale_r(cat, locale)) | |
a683a66a KW |
462 | |
463 | # define querylocale_c(cat) porcelain_setlocale(cat, NULL) | |
464 | # define querylocale_r(cat) querylocale_c(cat) | |
465 | # define querylocale_i(i) querylocale_c(categories[i]) | |
466 | ||
45cef8fb | 467 | # define FIX_GLIBC_LC_MESSAGES_BUG(i) |
e9bc6d6b KW |
468 | |
469 | #else /* Below uses POSIX 2008 */ | |
470 | ||
fe1c1494 KW |
471 | /* Here, there is a completely different API to get thread-safe locales. We |
472 | * emulate the setlocale() API with our own function(s). setlocale categories, | |
473 | * like LC_NUMERIC, are not valid here for the POSIX 2008 API. Instead, there | |
474 | * are equivalents, like LC_NUMERIC_MASK, which we use instead, converting to | |
475 | * by using get_category_index() followed by table lookup. */ | |
476 | ||
3980cddb KW |
477 | # define setlocale_i(i, locale) emulate_setlocale_i(i, locale) |
478 | # define setlocale_c(cat, locale) setlocale_i(cat##_INDEX_, locale) | |
479 | # define setlocale_r(cat, locale) \ | |
480 | setlocale_i(get_category_index(cat, locale), locale) | |
481 | ||
482 | # define void_setlocale_i(i, locale) ((void) setlocale_i(i, locale)) | |
483 | # define void_setlocale_c(cat, locale) \ | |
484 | void_setlocale_i(cat##_INDEX_, locale) | |
485 | # define void_setlocale_r(cat, locale) ((void) setlocale_r(cat, locale)) | |
486 | ||
487 | # define bool_setlocale_i(i, locale) cBOOL(setlocale_i(i, locale)) | |
488 | # define bool_setlocale_c(cat, locale) \ | |
489 | bool_setlocale_i(cat##_INDEX_, locale) | |
490 | # define bool_setlocale_r(cat, locale) cBOOL(setlocale_r(cat, locale)) | |
e9bc6d6b | 491 | |
a683a66a KW |
492 | # define querylocale_i(i) my_querylocale_i(i) |
493 | # define querylocale_c(cat) querylocale_i(cat##_INDEX_) | |
494 | # define querylocale_r(cat) querylocale_i(get_category_index(cat,NULL)) | |
495 | ||
67c13e2e | 496 | # ifndef USE_QUERYLOCALE |
434e0069 | 497 | # define USE_PL_CURLOCALES |
67c13e2e | 498 | # else |
d92b203c KW |
499 | # define isSINGLE_BIT_SET(mask) isPOWER_OF_2(mask) |
500 | ||
501 | /* This code used to think querylocale() was valid on LC_ALL. Make sure | |
502 | * all instances of that have been removed */ | |
503 | # define QUERYLOCALE_ASSERT(index) \ | |
504 | __ASSERT_(isSINGLE_BIT_SET(category_masks[index])) | |
67c13e2e KW |
505 | # if ! defined(HAS_QUERYLOCALE) && defined(_NL_LOCALE_NAME) |
506 | # define querylocale_l(index, locale_obj) \ | |
d92b203c KW |
507 | (QUERYLOCALE_ASSERT(index) \ |
508 | nl_langinfo_l(_NL_LOCALE_NAME(categories[index]), locale_obj)) | |
67c13e2e KW |
509 | # else |
510 | # define querylocale_l(index, locale_obj) \ | |
d92b203c KW |
511 | (QUERYLOCALE_ASSERT(index) \ |
512 | querylocale(category_masks[index], locale_obj)) | |
67c13e2e | 513 | # endif |
434e0069 | 514 | # endif |
45cef8fb | 515 | # if ! defined(__GLIBC__) || ! defined(USE_LOCALE_MESSAGES) |
45cef8fb | 516 | # define FIX_GLIBC_LC_MESSAGES_BUG(i) |
45cef8fb KW |
517 | # else /* Invalidate glibc cache of loaded translations, see [perl #134264] */ |
518 | ||
519 | # include <libintl.h> | |
520 | # define FIX_GLIBC_LC_MESSAGES_BUG(i) \ | |
521 | STMT_START { \ | |
4fd76d49 | 522 | if ((i) == LC_MESSAGES_INDEX_) { \ |
45cef8fb KW |
523 | textdomain(textdomain(NULL)); \ |
524 | } \ | |
525 | } STMT_END | |
526 | ||
527 | # endif | |
528 | ||
c7dc1ea8 | 529 | /* A fourth array, parallel to the ones above to map from category to its |
e9bc6d6b | 530 | * equivalent mask */ |
e129f2eb | 531 | STATIC const int category_masks[] = { |
e9bc6d6b KW |
532 | # ifdef USE_LOCALE_NUMERIC |
533 | LC_NUMERIC_MASK, | |
534 | # endif | |
535 | # ifdef USE_LOCALE_CTYPE | |
536 | LC_CTYPE_MASK, | |
537 | # endif | |
538 | # ifdef USE_LOCALE_COLLATE | |
539 | LC_COLLATE_MASK, | |
540 | # endif | |
541 | # ifdef USE_LOCALE_TIME | |
542 | LC_TIME_MASK, | |
543 | # endif | |
544 | # ifdef USE_LOCALE_MESSAGES | |
545 | LC_MESSAGES_MASK, | |
546 | # endif | |
547 | # ifdef USE_LOCALE_MONETARY | |
548 | LC_MONETARY_MASK, | |
549 | # endif | |
550 | # ifdef USE_LOCALE_ADDRESS | |
551 | LC_ADDRESS_MASK, | |
552 | # endif | |
553 | # ifdef USE_LOCALE_IDENTIFICATION | |
554 | LC_IDENTIFICATION_MASK, | |
555 | # endif | |
556 | # ifdef USE_LOCALE_MEASUREMENT | |
557 | LC_MEASUREMENT_MASK, | |
558 | # endif | |
559 | # ifdef USE_LOCALE_PAPER | |
560 | LC_PAPER_MASK, | |
561 | # endif | |
562 | # ifdef USE_LOCALE_TELEPHONE | |
563 | LC_TELEPHONE_MASK, | |
564 | # endif | |
36dbc955 KW |
565 | # ifdef USE_LOCALE_SYNTAX |
566 | LC_SYNTAX_MASK, | |
567 | # endif | |
568 | # ifdef USE_LOCALE_TOD | |
569 | LC_TOD_MASK, | |
570 | # endif | |
e9bc6d6b KW |
571 | /* LC_ALL can't be turned off by a Configure |
572 | * option, and in Posix 2008, should always be | |
573 | * here, so compile it in unconditionally. | |
574 | * This could catch some glitches at compile | |
575 | * time */ | |
7e1cacef KW |
576 | LC_ALL_MASK, |
577 | ||
578 | /* Placeholder as a precaution if code fails to check the return of | |
579 | * get_category_index(), which returns this element to indicate an error */ | |
580 | 0 | |
fe1c1494 | 581 | }; |
e9bc6d6b | 582 | |
935c09ec KW |
583 | # define my_querylocale_c(cat) my_querylocale_i(cat##_INDEX_) |
584 | ||
e9bc6d6b | 585 | STATIC const char * |
935c09ec | 586 | S_my_querylocale_i(pTHX_ const unsigned int index) |
e9bc6d6b | 587 | { |
935c09ec KW |
588 | /* This function returns the name of the locale category given by the input |
589 | * index into our parallel tables of them. | |
e9bc6d6b KW |
590 | * |
591 | * POSIX 2008, for some sick reason, chose not to provide a method to find | |
935c09ec KW |
592 | * the category name of a locale, discarding a basic linguistic tenet that |
593 | * for any object, people will create a name for it. Some vendors have | |
594 | * created a querylocale() function to do just that. This function is a | |
595 | * lot simpler to implement on systems that have this. Otherwise, we have | |
596 | * to keep track of what the locale has been set to, so that we can return | |
597 | * its name so as to emulate setlocale(). It's also possible for C code in | |
598 | * some library to change the locale without us knowing it, though as of | |
e9bc6d6b KW |
599 | * September 2017, there are no occurrences in CPAN of uselocale(). Some |
600 | * libraries do use setlocale(), but that changes the global locale, and | |
935c09ec | 601 | * threads using per-thread locales will just ignore those changes. */ |
e9bc6d6b | 602 | |
4bcc858c | 603 | int category; |
935c09ec | 604 | const locale_t cur_obj = uselocale((locale_t) 0); |
d92b203c | 605 | const char * retval; |
e9bc6d6b | 606 | |
935c09ec | 607 | PERL_ARGS_ASSERT_MY_QUERYLOCALE_I; |
4bcc858c | 608 | assert(index <= NOMINAL_LC_ALL_INDEX); |
e9bc6d6b | 609 | |
4bcc858c | 610 | category = categories[index]; |
e9bc6d6b | 611 | |
935c09ec | 612 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: my_querylocale_i %p\n", |
94d521c0 | 613 | __FILE__, __LINE__, cur_obj)); |
e9bc6d6b | 614 | if (cur_obj == LC_GLOBAL_LOCALE) { |
d92b203c | 615 | retval = porcelain_setlocale(category, NULL); |
e9bc6d6b | 616 | } |
d92b203c | 617 | else { |
e9bc6d6b | 618 | |
67c13e2e | 619 | # ifdef USE_QUERYLOCALE |
e9bc6d6b | 620 | |
d92b203c KW |
621 | /* We don't currently keep records when there is querylocale(), so have |
622 | * to get it anew each time */ | |
623 | retval = (index == LC_ALL_INDEX_) | |
624 | ? calculate_LC_ALL(cur_obj) | |
625 | : querylocale_l(index, cur_obj); | |
e9bc6d6b | 626 | |
d2b24094 | 627 | # else |
ecdda939 | 628 | |
d92b203c KW |
629 | /* But we do have up-to-date values when we keep our own records */ |
630 | retval = PL_curlocales[index]; | |
e9bc6d6b | 631 | |
d92b203c | 632 | # endif |
e9bc6d6b | 633 | |
e9bc6d6b KW |
634 | } |
635 | ||
94d521c0 | 636 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
d92b203c KW |
637 | "%s:%d: my_querylocale_i(%s) returning '%s'\n", |
638 | __FILE__, __LINE__, category_names[index], retval)); | |
639 | return retval; | |
935c09ec KW |
640 | } |
641 | ||
642 | STATIC const char * | |
643 | S_emulate_setlocale_i(pTHX_ const unsigned int index, const char * locale) | |
644 | { | |
645 | /* This function effectively performs a setlocale() on just the current | |
646 | * thread; thus it is thread-safe. It does this by using the POSIX 2008 | |
647 | * locale functions to emulate the behavior of setlocale(). Similar to | |
648 | * regular setlocale(), the return from this function points to memory that | |
649 | * can be overwritten by other system calls, so needs to be copied | |
650 | * immediately if you need to retain it. The difference here is that | |
651 | * system calls besides another setlocale() can overwrite it. | |
652 | * | |
653 | * By doing this, most locale-sensitive functions become thread-safe. The | |
654 | * exceptions are mostly those that return a pointer to static memory. | |
655 | * | |
656 | * This function takes our internal index of the 'category' setlocale is | |
657 | * called with, and the 'locale' to set the category to. It uses the | |
658 | * index to find the category mask that the POSIX 2008 functions use. */ | |
659 | ||
660 | int mask; | |
661 | int category; | |
662 | locale_t old_obj; | |
663 | locale_t new_obj; | |
664 | ||
665 | PERL_ARGS_ASSERT_EMULATE_SETLOCALE_I; | |
666 | assert(index <= NOMINAL_LC_ALL_INDEX); | |
667 | ||
668 | mask = category_masks[index]; | |
669 | category = categories[index]; | |
670 | ||
671 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
672 | "%s:%d: emulate_setlocale_i input=%d (%s), mask=0x%x, \"%s\", %d\n", | |
673 | __FILE__, __LINE__, category, category_name(category), mask, | |
674 | locale, index)); | |
e9bc6d6b | 675 | |
935c09ec KW |
676 | /* If just querying what the existing locale is ... */ |
677 | if (locale == NULL) { | |
678 | return my_querylocale_i(index); | |
679 | } | |
e9bc6d6b | 680 | |
67c13e2e | 681 | # ifndef USE_QUERYLOCALE |
e9bc6d6b KW |
682 | |
683 | if (strEQ(locale, "")) { | |
684 | ||
685 | /* For non-querylocale() systems, we do the setting of "" ourselves to | |
686 | * be sure that we really know what's going on. We follow the Linux | |
687 | * documented behavior (but if that differs from the actual behavior, | |
688 | * this won't work exactly as the OS implements). We go out and | |
689 | * examine the environment based on our understanding of how the system | |
fe1c1494 KW |
690 | * works, and use that to figure things out. |
691 | * | |
692 | * Another option would be to toggle to the global locale, and do a | |
693 | * straight setlocale(LC_ALL, ""). But that could cause races with any | |
694 | * other thread that has also switched. That's probably a rare event, | |
695 | * and we could have a global boolean that indicates if any thread has | |
696 | * switched, but we'd still need the following backup code anyway. The | |
697 | * only real reason to make the switch is because some alien library | |
698 | * that can't be changed, like GTk, is doing its own setlocales, */ | |
e9bc6d6b KW |
699 | |
700 | const char * const lc_all = PerlEnv_getenv("LC_ALL"); | |
701 | ||
702 | /* Use any "LC_ALL" environment variable, as it overrides everything | |
703 | * else. */ | |
704 | if (lc_all && strNE(lc_all, "")) { | |
705 | locale = lc_all; | |
706 | } | |
707 | else { | |
708 | ||
709 | /* Otherwise, we need to dig deeper. Unless overridden, the | |
710 | * default is the LANG environment variable; if it doesn't exist, | |
711 | * then "C" */ | |
712 | ||
713 | const char * default_name; | |
714 | ||
207cc8df | 715 | default_name = PerlEnv_getenv("LANG"); |
e9bc6d6b KW |
716 | |
717 | if (! default_name || strEQ(default_name, "")) { | |
929a7f8c | 718 | default_name = "C"; |
e9bc6d6b | 719 | } |
e9bc6d6b KW |
720 | |
721 | if (category != LC_ALL) { | |
722 | const char * const name = PerlEnv_getenv(category_names[index]); | |
723 | ||
724 | /* Here we are setting a single category. Assume will have the | |
725 | * default name */ | |
726 | locale = default_name; | |
727 | ||
728 | /* But then look for an overriding environment variable */ | |
729 | if (name && strNE(name, "")) { | |
730 | locale = name; | |
731 | } | |
732 | } | |
733 | else { | |
734 | bool did_override = FALSE; | |
735 | unsigned int i; | |
736 | ||
737 | /* Here, we are getting LC_ALL. Any categories that don't have | |
738 | * a corresponding environment variable set should be set to | |
739 | * LANG, or to "C" if there is no LANG. If no individual | |
740 | * categories differ from this, we can just set LC_ALL. This | |
741 | * is buggy on systems that have extra categories that we don't | |
742 | * know about. If there is an environment variable that sets | |
743 | * that category, we won't know to look for it, and so our use | |
744 | * of LANG or "C" improperly overrides it. On the other hand, | |
745 | * if we don't do what is done here, and there is no | |
746 | * environment variable, the category's locale should be set to | |
747 | * LANG or "C". So there is no good solution. khw thinks the | |
748 | * best is to look at systems to see what categories they have, | |
749 | * and include them, and then to assume that we know the | |
750 | * complete set */ | |
751 | ||
4fd76d49 | 752 | for (i = 0; i < LC_ALL_INDEX_; i++) { |
e9bc6d6b | 753 | const char * const env_override |
24f3e849 | 754 | = PerlEnv_getenv(category_names[i]); |
e9bc6d6b KW |
755 | const char * this_locale = ( env_override |
756 | && strNE(env_override, "")) | |
757 | ? env_override | |
758 | : default_name; | |
4bcc858c | 759 | if (! emulate_setlocale_i(i, this_locale)) |
6435f98d | 760 | { |
6435f98d KW |
761 | return NULL; |
762 | } | |
e9bc6d6b KW |
763 | |
764 | if (strNE(this_locale, default_name)) { | |
765 | did_override = TRUE; | |
766 | } | |
e9bc6d6b KW |
767 | } |
768 | ||
769 | /* If all the categories are the same, we can set LC_ALL to | |
770 | * that */ | |
771 | if (! did_override) { | |
772 | locale = default_name; | |
773 | } | |
774 | else { | |
775 | ||
776 | /* Here, LC_ALL is no longer valid, as some individual | |
777 | * categories don't match it. We call ourselves | |
778 | * recursively, as that will execute the code that | |
779 | * generates the proper locale string for this situation. | |
780 | * We don't do the remainder of this function, as that is | |
781 | * to update our records, and we've just done that for the | |
782 | * individual categories in the loop above, and doing so | |
783 | * would cause LC_ALL to be done as well */ | |
a683a66a | 784 | return querylocale_c(LC_ALL); |
e9bc6d6b KW |
785 | } |
786 | } | |
787 | } | |
6aba5c5e | 788 | } /* End of this being setlocale(LC_foo, "") */ |
e9bc6d6b KW |
789 | else if (strchr(locale, ';')) { |
790 | ||
791 | /* LC_ALL may actually incude a conglomeration of various categories. | |
792 | * Without querylocale, this code uses the glibc (as of this writing) | |
793 | * syntax for representing that, but that is not a stable API, and | |
794 | * other platforms do it differently, so we have to handle all cases | |
795 | * ourselves */ | |
796 | ||
f0023d47 | 797 | unsigned int i; |
e9bc6d6b KW |
798 | const char * s = locale; |
799 | const char * e = locale + strlen(locale); | |
800 | const char * p = s; | |
801 | const char * category_end; | |
802 | const char * name_start; | |
803 | const char * name_end; | |
804 | ||
f0023d47 KW |
805 | /* If the string that gives what to set doesn't include all categories, |
806 | * the omitted ones get set to "C". To get this behavior, first set | |
807 | * all the individual categories to "C", and override the furnished | |
808 | * ones below */ | |
4fd76d49 | 809 | for (i = 0; i < LC_ALL_INDEX_; i++) { |
4bcc858c | 810 | if (! emulate_setlocale_i(i, "C")) { |
f0023d47 KW |
811 | return NULL; |
812 | } | |
813 | } | |
814 | ||
e9bc6d6b | 815 | while (s < e) { |
e9bc6d6b KW |
816 | |
817 | /* Parse through the category */ | |
818 | while (isWORDCHAR(*p)) { | |
819 | p++; | |
820 | } | |
821 | category_end = p; | |
822 | ||
823 | if (*p++ != '=') { | |
824 | Perl_croak(aTHX_ | |
f052c885 KW |
825 | "panic: %s: %d: Unexpected character in locale category" |
826 | " name '%02X", | |
e9bc6d6b KW |
827 | __FILE__, __LINE__, *(p-1)); |
828 | } | |
829 | ||
830 | /* Parse through the locale name */ | |
831 | name_start = p; | |
bee74f4b KW |
832 | while (p < e && *p != ';') { |
833 | if (! isGRAPH(*p)) { | |
834 | Perl_croak(aTHX_ | |
835 | "panic: %s: %d: Unexpected character in locale name '%02X", | |
f1b080c5 | 836 | __FILE__, __LINE__, *p); |
bee74f4b | 837 | } |
e9bc6d6b KW |
838 | p++; |
839 | } | |
840 | name_end = p; | |
841 | ||
bee74f4b KW |
842 | /* Space past the semi-colon */ |
843 | if (p < e) { | |
844 | p++; | |
e9bc6d6b KW |
845 | } |
846 | ||
847 | /* Find the index of the category name in our lists */ | |
4fd76d49 | 848 | for (i = 0; i < LC_ALL_INDEX_; i++) { |
3c76a412 | 849 | char * individ_locale; |
e9bc6d6b KW |
850 | |
851 | /* Keep going if this isn't the index. The strnNE() avoids a | |
852 | * Perl_form(), but would fail if ever a category name could be | |
853 | * a substring of another one, like if there were a | |
854 | * "LC_TIME_DATE" */ | |
855 | if strnNE(s, category_names[i], category_end - s) { | |
856 | continue; | |
857 | } | |
858 | ||
859 | /* If this index is for the single category we're changing, we | |
860 | * have found the locale to set it to. */ | |
861 | if (category == categories[i]) { | |
862 | locale = Perl_form(aTHX_ "%.*s", | |
863 | (int) (name_end - name_start), | |
864 | name_start); | |
865 | goto ready_to_set; | |
866 | } | |
867 | ||
3c76a412 | 868 | assert(category == LC_ALL); |
bee74f4b KW |
869 | individ_locale = Perl_form(aTHX_ "%.*s", |
870 | (int) (name_end - name_start), name_start); | |
4bcc858c | 871 | if (! emulate_setlocale_i(i, individ_locale)) |
6435f98d KW |
872 | { |
873 | return NULL; | |
874 | } | |
e9bc6d6b KW |
875 | } |
876 | ||
877 | s = p; | |
878 | } | |
879 | ||
880 | /* Here we have set all the individual categories by recursive calls. | |
881 | * These collectively should have fixed up LC_ALL, so can just query | |
882 | * what that now is */ | |
883 | assert(category == LC_ALL); | |
884 | ||
a683a66a | 885 | return querylocale_c(LC_ALL); |
6aba5c5e KW |
886 | } /* End of this being setlocale(LC_ALL, |
887 | "LC_CTYPE=foo;LC_NUMERIC=bar;...") */ | |
e9bc6d6b KW |
888 | |
889 | ready_to_set: ; | |
890 | ||
f8115d60 KW |
891 | /* Here at the end of having to deal with the absence of querylocale(). |
892 | * Some cases have already been fully handled by recursive calls to this | |
893 | * function. But at this point, we haven't dealt with those, but are now | |
894 | * prepared to, knowing what the locale name to set this category to is. | |
895 | * This would have come for free if this system had had querylocale() */ | |
896 | ||
e9bc6d6b KW |
897 | # endif /* end of ! querylocale */ |
898 | ||
f8115d60 KW |
899 | assert(PL_C_locale_obj); |
900 | ||
901 | /* Switching locales generally entails freeing the current one's space (at | |
fe1c1494 KW |
902 | * the C library's discretion), hence we can't be using that locale at the |
903 | * time of the switch (this wasn't obvious to khw from the man pages). So | |
904 | * switch to a known locale object that we don't otherwise mess with; the | |
905 | * function returns the locale object in effect prior to the switch. */ | |
f8115d60 KW |
906 | old_obj = uselocale(PL_C_locale_obj); |
907 | ||
94d521c0 | 908 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
4bcc858c | 909 | "%s:%d: emulate_setlocale_i was using %p\n", |
94d521c0 | 910 | __FILE__, __LINE__, old_obj)); |
f8115d60 KW |
911 | |
912 | if (! old_obj) { | |
f8115d60 | 913 | dSAVE_ERRNO; |
94d521c0 | 914 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4bcc858c | 915 | "%s:%d: emulate_setlocale_i switching to C" |
94d521c0 | 916 | " failed: %d\n", __FILE__, __LINE__, GET_ERRNO)); |
f8115d60 | 917 | RESTORE_ERRNO; |
f8115d60 KW |
918 | |
919 | return NULL; | |
920 | } | |
921 | ||
94d521c0 | 922 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
4bcc858c | 923 | "%s:%d: emulate_setlocale_i now using %p\n", |
94d521c0 | 924 | __FILE__, __LINE__, PL_C_locale_obj)); |
f8115d60 | 925 | |
fe1c1494 KW |
926 | /* If this call is to switch LC_ALL to the 'C' locale, it already exists, |
927 | * and in fact, we already have switched to it (in preparation for what | |
6aba5c5e | 928 | * normally is to come). But since we're already there, continue to use |
70bd6bc8 KW |
929 | * it instead of trying to create a new locale */ |
930 | if (mask == LC_ALL_MASK && isNAME_C_OR_POSIX(locale)) { | |
931 | ||
94d521c0 KW |
932 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
933 | "%s:%d: will stay in C object\n", | |
934 | __FILE__, __LINE__)); | |
70bd6bc8 KW |
935 | |
936 | new_obj = PL_C_locale_obj; | |
937 | ||
938 | /* We already had switched to the C locale in preparation for freeing | |
b22e9937 | 939 | * 'old_obj' */ |
70bd6bc8 KW |
940 | if (old_obj != LC_GLOBAL_LOCALE && old_obj != PL_C_locale_obj) { |
941 | freelocale(old_obj); | |
942 | } | |
943 | } | |
944 | else { | |
b22e9937 KW |
945 | /* If we weren't in a thread safe locale, set so that newlocale() below |
946 | * which uses 'old_obj', uses an empty one. Same for our reserved C | |
947 | * object. The latter is defensive coding, so that, even if there is | |
948 | * some bug, we will never end up trying to modify either of these, as | |
fe1c1494 | 949 | * newlocale() just below would otherwise do. */ |
b22e9937 KW |
950 | if (old_obj == LC_GLOBAL_LOCALE || old_obj == PL_C_locale_obj) { |
951 | old_obj = (locale_t) 0; | |
952 | } | |
f8115d60 | 953 | |
b22e9937 KW |
954 | /* Ready to create a new locale by modification of the exising one */ |
955 | new_obj = newlocale(mask, locale, old_obj); | |
e9bc6d6b | 956 | |
b22e9937 KW |
957 | if (! new_obj) { |
958 | dSAVE_ERRNO; | |
e9bc6d6b | 959 | |
94d521c0 | 960 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4bcc858c | 961 | "%s:%d: emulate_setlocale_i creating new object" |
94d521c0 | 962 | " failed: %d\n", __FILE__, __LINE__, GET_ERRNO)); |
e9bc6d6b | 963 | |
b22e9937 | 964 | if (! uselocale(old_obj)) { |
94d521c0 | 965 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
b22e9937 | 966 | "%s:%d: switching back failed: %d\n", |
94d521c0 | 967 | __FILE__, __LINE__, GET_ERRNO)); |
b22e9937 KW |
968 | } |
969 | RESTORE_ERRNO; | |
970 | return NULL; | |
e9bc6d6b | 971 | } |
e9bc6d6b | 972 | |
94d521c0 | 973 | DEBUG_Lv(STMT_START { |
b22e9937 | 974 | PerlIO_printf(Perl_debug_log, |
4bcc858c | 975 | "%s:%d: emulate_setlocale_i created %p", |
b22e9937 | 976 | __FILE__, __LINE__, new_obj); |
94d521c0 | 977 | if (old_obj) PerlIO_printf(Perl_debug_log, |
b22e9937 | 978 | "; should have freed %p", old_obj); |
b22e9937 | 979 | PerlIO_printf(Perl_debug_log, "\n"); |
94d521c0 | 980 | } STMT_END); |
e9bc6d6b | 981 | |
b22e9937 KW |
982 | /* And switch into it */ |
983 | if (! uselocale(new_obj)) { | |
984 | dSAVE_ERRNO; | |
e9bc6d6b | 985 | |
94d521c0 | 986 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4bcc858c | 987 | "%s:%d: emulate_setlocale_i switching to new object" |
94d521c0 | 988 | " failed\n", __FILE__, __LINE__)); |
e9bc6d6b | 989 | |
b22e9937 | 990 | if (! uselocale(old_obj)) { |
e9bc6d6b | 991 | |
94d521c0 | 992 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
b22e9937 | 993 | "%s:%d: switching back failed: %d\n", |
94d521c0 | 994 | __FILE__, __LINE__, GET_ERRNO)); |
e9bc6d6b | 995 | |
b22e9937 KW |
996 | } |
997 | freelocale(new_obj); | |
998 | RESTORE_ERRNO; | |
999 | return NULL; | |
e9bc6d6b | 1000 | } |
70bd6bc8 | 1001 | } |
e9bc6d6b | 1002 | |
fe1c1494 | 1003 | /* Here, we are using 'new_obj' which matches the input 'locale'. */ |
94d521c0 | 1004 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
4bcc858c | 1005 | "%s:%d: emulate_setlocale_i now using %p\n", |
94d521c0 | 1006 | __FILE__, __LINE__, new_obj)); |
e9bc6d6b KW |
1007 | |
1008 | /* We are done, except for updating our records (if the system doesn't keep | |
1009 | * them) and in the case of locale "", we don't actually know what the | |
1010 | * locale that got switched to is, as it came from the environment. So | |
1011 | * have to find it */ | |
1012 | ||
67c13e2e | 1013 | # ifdef USE_QUERYLOCALE |
e9bc6d6b KW |
1014 | |
1015 | if (strEQ(locale, "")) { | |
d92b203c | 1016 | locale = querylocale_i(index); |
e9bc6d6b KW |
1017 | } |
1018 | ||
1019 | # else | |
1020 | ||
1021 | /* Here, 'locale' is the return value */ | |
1022 | ||
1023 | /* Without querylocale(), we have to update our records */ | |
1024 | ||
1025 | if (category == LC_ALL) { | |
1026 | unsigned int i; | |
1027 | ||
1028 | /* For LC_ALL, we change all individual categories to correspond */ | |
1029 | /* PL_curlocales is a parallel array, so has same | |
1030 | * length as 'categories' */ | |
4fd76d49 | 1031 | for (i = 0; i <= LC_ALL_INDEX_; i++) { |
e9bc6d6b KW |
1032 | Safefree(PL_curlocales[i]); |
1033 | PL_curlocales[i] = savepv(locale); | |
1034 | } | |
45cef8fb | 1035 | |
4fd76d49 | 1036 | FIX_GLIBC_LC_MESSAGES_BUG(LC_MESSAGES_INDEX_); |
e9bc6d6b KW |
1037 | } |
1038 | else { | |
1039 | ||
d92b203c | 1040 | /* Otherwise, update the single category, plus LC_ALL */ |
e9bc6d6b | 1041 | |
d92b203c KW |
1042 | Safefree(PL_curlocales[index]); |
1043 | PL_curlocales[index] = savepv(locale); | |
1044 | ||
1045 | if ( PL_curlocales[LC_ALL_INDEX_] == NULL | |
1046 | || strNE(PL_curlocales[LC_ALL_INDEX_], locale)) | |
fe1c1494 | 1047 | { |
4fd76d49 | 1048 | Safefree(PL_curlocales[LC_ALL_INDEX_]); |
d92b203c KW |
1049 | PL_curlocales[LC_ALL_INDEX_] = |
1050 | savepv(calculate_LC_ALL(PL_curlocales)); | |
e9bc6d6b KW |
1051 | } |
1052 | ||
45cef8fb | 1053 | FIX_GLIBC_LC_MESSAGES_BUG(index); |
e9bc6d6b KW |
1054 | } |
1055 | ||
1056 | # endif | |
1057 | ||
1058 | return locale; | |
1059 | } | |
1060 | ||
3980cddb KW |
1061 | #endif /* End of the various implementations of the setlocale and |
1062 | querylocale macros used in the remainder of this program */ | |
e9bc6d6b | 1063 | |
d36adde0 | 1064 | #ifdef USE_LOCALE |
837ce802 | 1065 | |
54ddc3dd KW |
1066 | STATIC char * |
1067 | S_stdize_locale(pTHX_ char *locs) | |
1068 | { | |
1069 | /* Standardize the locale name from a string returned by 'setlocale', | |
1070 | * possibly modifying that string. | |
1071 | * | |
1072 | * The typical return value of setlocale() is either | |
1073 | * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL | |
1074 | * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL | |
1075 | * (the space-separated values represent the various sublocales, | |
1076 | * in some unspecified order). This is not handled by this function. | |
1077 | * | |
1078 | * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n", | |
1079 | * which is harmful for further use of the string in setlocale(). This | |
1080 | * function removes the trailing new line and everything up through the '=' | |
1081 | * */ | |
1082 | ||
1083 | const char * const s = strchr(locs, '='); | |
1084 | bool okay = TRUE; | |
1085 | ||
1086 | PERL_ARGS_ASSERT_STDIZE_LOCALE; | |
1087 | ||
1088 | if (s) { | |
1089 | const char * const t = strchr(s, '.'); | |
1090 | okay = FALSE; | |
1091 | if (t) { | |
1092 | const char * const u = strchr(t, '\n'); | |
1093 | if (u && (u[1] == 0)) { | |
1094 | const STRLEN len = u - s; | |
1095 | Move(s + 1, locs, len, char); | |
1096 | locs[len] = 0; | |
1097 | okay = TRUE; | |
1098 | } | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | if (!okay) | |
1103 | Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs); | |
1104 | ||
1105 | return locs; | |
1106 | } | |
1107 | ||
6df0bfb6 | 1108 | #if defined(USE_POSIX_2008_LOCALE) |
d92b203c KW |
1109 | STATIC |
1110 | const char * | |
1111 | ||
1112 | # ifdef USE_QUERYLOCALE | |
1113 | S_calculate_LC_ALL(pTHX_ const locale_t cur_obj) | |
1114 | # else | |
1115 | S_calculate_LC_ALL(pTHX_ const char ** individ_locales) | |
1116 | # endif | |
1117 | ||
1118 | { | |
1119 | /* For POSIX 2008, we have to figure out LC_ALL ourselves when needed. | |
1120 | * querylocale(), on systems that have it, doesn't tend to work for LC_ALL. | |
1121 | * So we have to construct the answer ourselves based on the passed in | |
1122 | * data, which is either a locale_t object, for systems with querylocale(), | |
1123 | * or an array we keep updated to the proper values, otherwise. | |
1124 | * | |
1125 | * This returns a mortalized string containing the locale name(s) of | |
1126 | * LC_ALL. | |
1127 | * | |
1128 | * If all individual categories are the same locale, we can just set LC_ALL | |
1129 | * to that locale. But if not, we have to create an aggregation of all the | |
1130 | * categories on the system. Platforms differ as to the syntax they use | |
1131 | * for these non-uniform locales for LC_ALL. Some use a '/' or other | |
1132 | * delimiter of the locales with a predetermined order of categories; a | |
1133 | * Configure probe would be needed to tell us how to decipher those. glibc | |
1134 | * uses a series of name=value pairs, like | |
1135 | * LC_NUMERIC=C;LC_TIME=en_US.UTF-8;... | |
1136 | * The syntax we use for our aggregation doesn't much matter, as we take | |
1137 | * care not to use the native setlocale() function on whatever style is | |
1138 | * chosen. But, it would be possible for someone to call Perl_setlocale() | |
1139 | * using a native style we don't understand. So far no one has complained. | |
1140 | * | |
1141 | * For systems that have categories we don't know about, the algorithm | |
1142 | * below won't know about those missing categories, leading to potential | |
1143 | * bugs for code that looks at them. If there is an environment variable | |
1144 | * that sets that category, we won't know to look for it, and so our use of | |
1145 | * LANG or "C" improperly overrides it. On the other hand, if we don't do | |
1146 | * what is done here, and there is no environment variable, the category's | |
1147 | * locale should be set to LANG or "C". So there is no good solution. khw | |
1148 | * thinks the best is to make sure we have a complete list of possible | |
1149 | * categories, adding new ones as they show up on obscure platforms. | |
1150 | */ | |
1151 | ||
1152 | unsigned int i; | |
1153 | Size_t names_len = 0; | |
1154 | bool are_all_categories_the_same_locale = TRUE; | |
1155 | char * aggregate_locale; | |
1156 | char * previous_start = NULL; | |
36540e72 | 1157 | char * this_start = NULL; |
d92b203c KW |
1158 | Size_t entry_len = 0; |
1159 | ||
1160 | PERL_ARGS_ASSERT_CALCULATE_LC_ALL; | |
1161 | ||
1162 | /* First calculate the needed size for the string listing the categories | |
1163 | * and their locales. */ | |
1164 | for (i = 0; i < LC_ALL_INDEX_; i++) { | |
1165 | ||
1166 | # ifdef USE_QUERYLOCALE | |
1167 | const char * entry = querylocale_l(i, cur_obj); | |
1168 | # else | |
1169 | const char * entry = individ_locales[i]; | |
1170 | # endif | |
1171 | ||
1172 | if (entry == NULL) continue; /* XXX Temporary */ | |
1173 | names_len += strlen(category_names[i]) | |
1174 | + 1 /* '=' */ | |
1175 | + strlen(entry) | |
1176 | + 1; /* ';' */ | |
1177 | } | |
1178 | ||
1179 | names_len++; /* Trailing '\0' */ | |
1180 | ||
1181 | /* Allocate enough space for the aggregated string */ | |
1182 | SAVEFREEPV(Newxz(aggregate_locale, names_len, char)); | |
1183 | ||
1184 | /* Then fill it in */ | |
1185 | for (i = 0; i < LC_ALL_INDEX_; i++) { | |
1186 | Size_t new_len; | |
1187 | ||
1188 | # ifdef USE_QUERYLOCALE | |
1189 | const char * entry = querylocale_l(i, cur_obj); | |
1190 | # else | |
1191 | const char * entry = individ_locales[i]; | |
1192 | # endif | |
1193 | ||
1194 | if (entry == NULL) continue; /* XXX Temporary */ | |
1195 | new_len = my_strlcat(aggregate_locale, category_names[i], names_len); | |
1196 | assert(new_len <= names_len); | |
1197 | new_len = my_strlcat(aggregate_locale, "=", names_len); | |
1198 | assert(new_len <= names_len); | |
1199 | ||
1200 | this_start = aggregate_locale + strlen(aggregate_locale); | |
1201 | entry_len = strlen(entry); | |
1202 | ||
1203 | new_len = my_strlcat(aggregate_locale, entry, names_len); | |
1204 | assert(new_len <= names_len); | |
1205 | new_len = my_strlcat(aggregate_locale, ";", names_len); | |
1206 | assert(new_len <= names_len); | |
1207 | PERL_UNUSED_VAR(new_len); /* Only used in DEBUGGING */ | |
1208 | ||
1209 | if ( i > 0 | |
1210 | && are_all_categories_the_same_locale | |
1211 | && memNE(previous_start, this_start, entry_len + 1)) | |
1212 | { | |
1213 | are_all_categories_the_same_locale = FALSE; | |
1214 | } | |
1215 | else { | |
1216 | previous_start = this_start; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | /* If they are all the same, just return any one of them */ | |
1221 | if (are_all_categories_the_same_locale) { | |
1222 | aggregate_locale = this_start; | |
1223 | aggregate_locale[entry_len] = '\0'; | |
1224 | } | |
1225 | ||
1226 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
1227 | "%s:%d: calculate_LC_ALL returning '%s'\n", | |
1228 | __FILE__, __LINE__, aggregate_locale)); | |
1229 | ||
1230 | return aggregate_locale; | |
1231 | } | |
6df0bfb6 | 1232 | #endif /*defined(USE_POSIX_2008_LOCALE)*/ |
d92b203c | 1233 | |
8befffe0 | 1234 | #ifndef USE_POSIX_2008_LOCALE |
12f1a329 | 1235 | |
a4f00dcc | 1236 | STATIC void |
94202fb8 KW |
1237 | S_setlocale_failure_panic_i(pTHX_ |
1238 | const unsigned int cat_index, | |
1239 | const char * current, | |
1240 | const char * failed, | |
1241 | const line_t caller_0_line, | |
1242 | const line_t caller_1_line) | |
1243 | { | |
1244 | const int cat = categories[cat_index]; | |
1245 | const char * name = category_names[cat_index]; | |
1246 | dSAVE_ERRNO; | |
1247 | ||
1248 | PERL_ARGS_ASSERT_SETLOCALE_FAILURE_PANIC_I; | |
1249 | ||
1250 | #ifdef USE_C_BACKTRACE | |
1251 | dump_c_backtrace(Perl_debug_log, 20, 1); | |
1252 | #endif | |
1253 | ||
1254 | SETLOCALE_UNLOCK; | |
1255 | ||
1256 | if (current == NULL) { | |
1257 | current = querylocale_i(cat_index); | |
1258 | } | |
1259 | ||
1260 | RESTORE_ERRNO; | |
ee178617 KW |
1261 | Perl_croak(aTHX_ "panic: %s: %" LINE_Tf "d:(%" LINE_Tf |
1262 | "): Can't change locale for" " %s(%d)" | |
94202fb8 KW |
1263 | " from '%s' to '%s'; errno=%d\n", |
1264 | __FILE__, caller_0_line, caller_1_line, name, cat, | |
1265 | current, failed, errno); | |
1266 | NOT_REACHED; /* NOTREACHED */ | |
1267 | } | |
1268 | ||
12f1a329 KW |
1269 | #endif |
1270 | ||
94202fb8 | 1271 | STATIC void |
86799d2d | 1272 | S_set_numeric_radix(pTHX_ const bool use_locale) |
98994639 | 1273 | { |
86799d2d KW |
1274 | /* If 'use_locale' is FALSE, set to use a dot for the radix character. If |
1275 | * TRUE, use the radix character derived from the current locale */ | |
7d4bcc4a | 1276 | |
fe1c1494 | 1277 | # if defined(USE_LOCALE_NUMERIC) && ( defined(HAS_LOCALECONV) \ |
86799d2d | 1278 | || defined(HAS_NL_LANGINFO)) |
98994639 | 1279 | |
87f8e8e7 | 1280 | const char * radix = (use_locale) |
4e6826bf | 1281 | ? my_nl_langinfo(RADIXCHAR, FALSE) |
87f8e8e7 KW |
1282 | /* FALSE => already in dest locale */ |
1283 | : "."; | |
2213a3be | 1284 | |
87f8e8e7 | 1285 | sv_setpv(PL_numeric_radix_sv, radix); |
86799d2d | 1286 | |
87f8e8e7 KW |
1287 | /* If this is valid UTF-8 that isn't totally ASCII, and we are in |
1288 | * a UTF-8 locale, then mark the radix as being in UTF-8 */ | |
1289 | if (is_utf8_non_invariant_string((U8 *) SvPVX(PL_numeric_radix_sv), | |
7a393424 | 1290 | SvCUR(PL_numeric_radix_sv)) |
87f8e8e7 KW |
1291 | && _is_cur_LC_category_utf8(LC_NUMERIC)) |
1292 | { | |
1293 | SvUTF8_on(PL_numeric_radix_sv); | |
1294 | } | |
86799d2d | 1295 | |
94d521c0 | 1296 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Locale radix is '%s', ?UTF-8=%d\n", |
3ca88433 | 1297 | SvPVX(PL_numeric_radix_sv), |
94d521c0 | 1298 | cBOOL(SvUTF8(PL_numeric_radix_sv)))); |
fe1c1494 | 1299 | # else |
d36adde0 KW |
1300 | |
1301 | PERL_UNUSED_ARG(use_locale); | |
1302 | ||
fe1c1494 | 1303 | # endif /* USE_LOCALE_NUMERIC and can find the radix char */ |
7d4bcc4a | 1304 | |
98994639 HS |
1305 | } |
1306 | ||
398eeea9 KW |
1307 | STATIC void |
1308 | S_new_numeric(pTHX_ const char *newnum) | |
98994639 | 1309 | { |
7d4bcc4a | 1310 | |
fe1c1494 | 1311 | # ifndef USE_LOCALE_NUMERIC |
7d4bcc4a KW |
1312 | |
1313 | PERL_UNUSED_ARG(newnum); | |
1314 | ||
fe1c1494 | 1315 | # else |
0d071d52 | 1316 | |
291a84fb | 1317 | /* Called after each libc setlocale() call affecting LC_NUMERIC, to tell |
fe1c1494 KW |
1318 | * core Perl this and that 'newnum' is the name of the new locale, and we |
1319 | * are switched into it. It installs this locale as the current underlying | |
1320 | * default, and then switches to the C locale, if necessary, so that the | |
1321 | * code that has traditionally expected the radix character to be a dot may | |
1322 | * continue to do so. | |
0d071d52 KW |
1323 | * |
1324 | * The default locale and the C locale can be toggled between by use of the | |
5792c642 KW |
1325 | * set_numeric_underlying() and set_numeric_standard() functions, which |
1326 | * should probably not be called directly, but only via macros like | |
0d071d52 KW |
1327 | * SET_NUMERIC_STANDARD() in perl.h. |
1328 | * | |
1329 | * The toggling is necessary mainly so that a non-dot radix decimal point | |
fe1c1494 KW |
1330 | * character can be input and output, while allowing internal calculations |
1331 | * to use a dot. | |
0d071d52 KW |
1332 | * |
1333 | * This sets several interpreter-level variables: | |
bb304765 | 1334 | * PL_numeric_name The underlying locale's name: a copy of 'newnum' |
892e6465 | 1335 | * PL_numeric_underlying A boolean indicating if the toggled state is such |
7738054c KW |
1336 | * that the current locale is the program's underlying |
1337 | * locale | |
1338 | * PL_numeric_standard An int indicating if the toggled state is such | |
4c68b815 KW |
1339 | * that the current locale is the C locale or |
1340 | * indistinguishable from the C locale. If non-zero, it | |
1341 | * is in C; if > 1, it means it may not be toggled away | |
7738054c | 1342 | * from C. |
4c68b815 KW |
1343 | * PL_numeric_underlying_is_standard A bool kept by this function |
1344 | * indicating that the underlying locale and the standard | |
1345 | * C locale are indistinguishable for the purposes of | |
1346 | * LC_NUMERIC. This happens when both of the above two | |
1347 | * variables are true at the same time. (Toggling is a | |
1348 | * no-op under these circumstances.) This variable is | |
1349 | * used to avoid having to recalculate. | |
fe1c1494 KW |
1350 | * PL_numeric_radix_sv Contains the string that code should use for the |
1351 | * decimal point. It is set to either a dot or the | |
1352 | * program's underlying locale's radix character string, | |
1353 | * depending on the situation. | |
1354 | * PL_underlying_numeric_obj = (only on POSIX 2008 platforms) An object | |
1355 | * with everything set up properly so as to avoid work on | |
1356 | * such platforms. | |
398eeea9 | 1357 | */ |
0d071d52 | 1358 | |
b03f34cf | 1359 | char *save_newnum; |
98994639 HS |
1360 | |
1361 | if (! newnum) { | |
1604cfb0 MS |
1362 | Safefree(PL_numeric_name); |
1363 | PL_numeric_name = NULL; | |
1364 | PL_numeric_standard = TRUE; | |
1365 | PL_numeric_underlying = TRUE; | |
1366 | PL_numeric_underlying_is_standard = TRUE; | |
1367 | return; | |
98994639 HS |
1368 | } |
1369 | ||
b03f34cf | 1370 | save_newnum = stdize_locale(savepv(newnum)); |
892e6465 | 1371 | PL_numeric_underlying = TRUE; |
4c68b815 KW |
1372 | PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum); |
1373 | ||
fe1c1494 | 1374 | # ifndef TS_W32_BROKEN_LOCALECONV |
69c5e0db | 1375 | |
4c68b815 | 1376 | /* If its name isn't C nor POSIX, it could still be indistinguishable from |
69c5e0db KW |
1377 | * them. But on broken Windows systems calling my_nl_langinfo() for |
1378 | * THOUSEP can currently (but rarely) cause a race, so avoid doing that, | |
1379 | * and just always change the locale if not C nor POSIX on those systems */ | |
4c68b815 | 1380 | if (! PL_numeric_standard) { |
4e6826bf | 1381 | PL_numeric_standard = cBOOL(strEQ(".", my_nl_langinfo(RADIXCHAR, |
4c68b815 | 1382 | FALSE /* Don't toggle locale */ )) |
4e6826bf | 1383 | && strEQ("", my_nl_langinfo(THOUSEP, FALSE))); |
4c68b815 | 1384 | } |
abe1abcf | 1385 | |
fe1c1494 | 1386 | # endif |
69c5e0db | 1387 | |
4c68b815 | 1388 | /* Save the new name if it isn't the same as the previous one, if any */ |
b03f34cf | 1389 | if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) { |
fe1c1494 | 1390 | /* Save the locale name for future use */ |
1604cfb0 MS |
1391 | Safefree(PL_numeric_name); |
1392 | PL_numeric_name = save_newnum; | |
b03f34cf | 1393 | } |
abe1abcf | 1394 | else { |
1604cfb0 | 1395 | Safefree(save_newnum); |
abe1abcf | 1396 | } |
4c28b29c | 1397 | |
4c68b815 KW |
1398 | PL_numeric_underlying_is_standard = PL_numeric_standard; |
1399 | ||
4b06b4d0 | 1400 | # ifdef USE_POSIX_2008_LOCALE |
e1aa2579 | 1401 | |
fe1c1494 | 1402 | /* We keep a special object for easy switching to */ |
e1aa2579 KW |
1403 | PL_underlying_numeric_obj = newlocale(LC_NUMERIC_MASK, |
1404 | PL_numeric_name, | |
1405 | PL_underlying_numeric_obj); | |
1406 | ||
fe1c1494 | 1407 | # endif |
e1aa2579 | 1408 | |
94d521c0 KW |
1409 | DEBUG_L( PerlIO_printf(Perl_debug_log, |
1410 | "Called new_numeric with %s, PL_numeric_name=%s\n", | |
1411 | newnum, PL_numeric_name)); | |
4c68b815 | 1412 | |
fe1c1494 KW |
1413 | /* Keep LC_NUMERIC so that it has the C locale radix and thousands |
1414 | * separator. This is for XS modules, so they don't have to worry about | |
1415 | * the radix being a non-dot. (Core operations that need the underlying | |
1416 | * locale change to it temporarily). */ | |
84f1d29f KW |
1417 | if (PL_numeric_standard) { |
1418 | set_numeric_radix(0); | |
1419 | } | |
1420 | else { | |
1421 | set_numeric_standard(); | |
1422 | } | |
4c28b29c | 1423 | |
fe1c1494 | 1424 | # endif |
7d4bcc4a | 1425 | |
98994639 HS |
1426 | } |
1427 | ||
1428 | void | |
1429 | Perl_set_numeric_standard(pTHX) | |
1430 | { | |
7d4bcc4a | 1431 | |
fe1c1494 | 1432 | # ifdef USE_LOCALE_NUMERIC |
7d4bcc4a | 1433 | |
fe1c1494 KW |
1434 | /* Unconditionally toggle the LC_NUMERIC locale to the current underlying |
1435 | * default. | |
1436 | * | |
1437 | * Most code should use the macro SET_NUMERIC_STANDARD() in perl.h | |
1438 | * instead of calling this directly. The macro avoids calling this routine | |
1439 | * if toggling isn't necessary according to our records (which could be | |
1440 | * wrong if some XS code has changed the locale behind our back) */ | |
0d071d52 | 1441 | |
94d521c0 KW |
1442 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
1443 | "Setting LC_NUMERIC locale to standard C\n")); | |
7bb8f702 | 1444 | |
3980cddb | 1445 | void_setlocale_c(LC_NUMERIC, "C"); |
7bb8f702 KW |
1446 | PL_numeric_standard = TRUE; |
1447 | PL_numeric_underlying = PL_numeric_underlying_is_standard; | |
1448 | set_numeric_radix(0); | |
1449 | ||
fe1c1494 | 1450 | # endif /* USE_LOCALE_NUMERIC */ |
7d4bcc4a | 1451 | |
98994639 HS |
1452 | } |
1453 | ||
1454 | void | |
5792c642 | 1455 | Perl_set_numeric_underlying(pTHX) |
98994639 | 1456 | { |
7d4bcc4a | 1457 | |
fe1c1494 | 1458 | # ifdef USE_LOCALE_NUMERIC |
7d4bcc4a | 1459 | |
fe1c1494 KW |
1460 | /* Unconditionally toggle the LC_NUMERIC locale to the current underlying |
1461 | * default. | |
1462 | * | |
1463 | * Most code should use the macro SET_NUMERIC_UNDERLYING() in perl.h | |
7d4bcc4a KW |
1464 | * instead of calling this directly. The macro avoids calling this routine |
1465 | * if toggling isn't necessary according to our records (which could be | |
1466 | * wrong if some XS code has changed the locale behind our back) */ | |
a9b8c0d8 | 1467 | |
94d521c0 KW |
1468 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Setting LC_NUMERIC locale to %s\n", |
1469 | PL_numeric_name)); | |
7bb8f702 | 1470 | |
3980cddb | 1471 | void_setlocale_c(LC_NUMERIC, PL_numeric_name); |
7bb8f702 KW |
1472 | PL_numeric_standard = PL_numeric_underlying_is_standard; |
1473 | PL_numeric_underlying = TRUE; | |
1474 | set_numeric_radix(! PL_numeric_standard); | |
1475 | ||
fe1c1494 | 1476 | # endif /* USE_LOCALE_NUMERIC */ |
7d4bcc4a | 1477 | |
98994639 HS |
1478 | } |
1479 | ||
1480 | /* | |
1481 | * Set up for a new ctype locale. | |
1482 | */ | |
a4f00dcc KW |
1483 | STATIC void |
1484 | S_new_ctype(pTHX_ const char *newctype) | |
98994639 | 1485 | { |
7d4bcc4a | 1486 | |
fe1c1494 | 1487 | # ifndef USE_LOCALE_CTYPE |
7d4bcc4a | 1488 | |
7d4bcc4a KW |
1489 | PERL_UNUSED_ARG(newctype); |
1490 | PERL_UNUSED_CONTEXT; | |
1491 | ||
fe1c1494 | 1492 | # else |
0d071d52 | 1493 | |
291a84fb | 1494 | /* Called after each libc setlocale() call affecting LC_CTYPE, to tell |
0d071d52 KW |
1495 | * core Perl this and that 'newctype' is the name of the new locale. |
1496 | * | |
1497 | * This function sets up the folding arrays for all 256 bytes, assuming | |
1498 | * that tofold() is tolc() since fold case is not a concept in POSIX, | |
1499 | * | |
1500 | * Any code changing the locale (outside this file) should use | |
9aac5db8 KW |
1501 | * Perl_setlocale or POSIX::setlocale, which call this function. Therefore |
1502 | * this function should be called directly only from this file and from | |
0d071d52 KW |
1503 | * POSIX::setlocale() */ |
1504 | ||
013f4e03 | 1505 | unsigned int i; |
98994639 | 1506 | |
8b7358b9 KW |
1507 | /* Don't check for problems if we are suppressing the warnings */ |
1508 | bool check_for_problems = ckWARN_d(WARN_LOCALE) || UNLIKELY(DEBUG_L_TEST); | |
30d8090d | 1509 | bool maybe_utf8_turkic = FALSE; |
8b7358b9 | 1510 | |
7918f24d NC |
1511 | PERL_ARGS_ASSERT_NEW_CTYPE; |
1512 | ||
215c5139 KW |
1513 | /* We will replace any bad locale warning with 1) nothing if the new one is |
1514 | * ok; or 2) a new warning for the bad new locale */ | |
1515 | if (PL_warn_locale) { | |
1516 | SvREFCNT_dec_NN(PL_warn_locale); | |
1517 | PL_warn_locale = NULL; | |
1518 | } | |
1519 | ||
c1284011 | 1520 | PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE); |
31f05a37 KW |
1521 | |
1522 | /* A UTF-8 locale gets standard rules. But note that code still has to | |
1523 | * handle this specially because of the three problematic code points */ | |
1524 | if (PL_in_utf8_CTYPE_locale) { | |
1525 | Copy(PL_fold_latin1, PL_fold_locale, 256, U8); | |
30d8090d KW |
1526 | |
1527 | /* UTF-8 locales can have special handling for 'I' and 'i' if they are | |
fe1c1494 KW |
1528 | * Turkic. Make sure these two are the only anomalies. (We don't |
1529 | * require towupper and towlower because they aren't in C89.) */ | |
5b64f24c | 1530 | |
fe1c1494 | 1531 | # if defined(HAS_TOWUPPER) && defined (HAS_TOWLOWER) |
5b64f24c | 1532 | |
fe1c1494 | 1533 | if (towupper('i') == 0x130 && towlower('I') == 0x131) |
5b64f24c | 1534 | |
fe1c1494 | 1535 | # else |
5b64f24c | 1536 | |
fe1c1494 | 1537 | if (toU8_UPPER_LC('i') == 'i' && toU8_LOWER_LC('I') == 'I') |
5b64f24c | 1538 | |
fe1c1494 KW |
1539 | # endif |
1540 | ||
1541 | { | |
1542 | /* This is how we determine it really is Turkic */ | |
30d8090d KW |
1543 | check_for_problems = TRUE; |
1544 | maybe_utf8_turkic = TRUE; | |
1545 | } | |
31f05a37 | 1546 | } |
8b7358b9 KW |
1547 | |
1548 | /* We don't populate the other lists if a UTF-8 locale, but do check that | |
1549 | * everything works as expected, unless checking turned off */ | |
1550 | if (check_for_problems || ! PL_in_utf8_CTYPE_locale) { | |
8c6180a9 KW |
1551 | /* Assume enough space for every character being bad. 4 spaces each |
1552 | * for the 94 printable characters that are output like "'x' "; and 5 | |
1553 | * spaces each for "'\\' ", "'\t' ", and "'\n' "; plus a terminating | |
1554 | * NUL */ | |
8b7358b9 | 1555 | char bad_chars_list[ (94 * 4) + (3 * 5) + 1 ] = { '\0' }; |
8c6180a9 KW |
1556 | bool multi_byte_locale = FALSE; /* Assume is a single-byte locale |
1557 | to start */ | |
1558 | unsigned int bad_count = 0; /* Count of bad characters */ | |
1559 | ||
baa60164 | 1560 | for (i = 0; i < 256; i++) { |
8b7358b9 | 1561 | if (! PL_in_utf8_CTYPE_locale) { |
dcbeec20 KW |
1562 | if (isU8_UPPER_LC(i)) |
1563 | PL_fold_locale[i] = (U8) toU8_LOWER_LC(i); | |
1564 | else if (isU8_LOWER_LC(i)) | |
1565 | PL_fold_locale[i] = (U8) toU8_UPPER_LC(i); | |
bd6d0898 KW |
1566 | else |
1567 | PL_fold_locale[i] = (U8) i; | |
8b7358b9 | 1568 | } |
8c6180a9 KW |
1569 | |
1570 | /* If checking for locale problems, see if the native ASCII-range | |
1571 | * printables plus \n and \t are in their expected categories in | |
1572 | * the new locale. If not, this could mean big trouble, upending | |
1573 | * Perl's and most programs' assumptions, like having a | |
1574 | * metacharacter with special meaning become a \w. Fortunately, | |
1575 | * it's very rare to find locales that aren't supersets of ASCII | |
1576 | * nowadays. It isn't a problem for most controls to be changed | |
1577 | * into something else; we check only \n and \t, though perhaps \r | |
1578 | * could be an issue as well. */ | |
7d4bcc4a | 1579 | if ( check_for_problems |
8c6180a9 KW |
1580 | && (isGRAPH_A(i) || isBLANK_A(i) || i == '\n')) |
1581 | { | |
8b7358b9 | 1582 | bool is_bad = FALSE; |
6726b4f4 | 1583 | char name[4] = { '\0' }; |
8b7358b9 KW |
1584 | |
1585 | /* Convert the name into a string */ | |
6726b4f4 | 1586 | if (isGRAPH_A(i)) { |
8b7358b9 KW |
1587 | name[0] = i; |
1588 | name[1] = '\0'; | |
1589 | } | |
1590 | else if (i == '\n') { | |
6726b4f4 KW |
1591 | my_strlcpy(name, "\\n", sizeof(name)); |
1592 | } | |
1593 | else if (i == '\t') { | |
1594 | my_strlcpy(name, "\\t", sizeof(name)); | |
8b7358b9 KW |
1595 | } |
1596 | else { | |
6726b4f4 KW |
1597 | assert(i == ' '); |
1598 | my_strlcpy(name, "' '", sizeof(name)); | |
8b7358b9 KW |
1599 | } |
1600 | ||
1601 | /* Check each possibe class */ | |
dcbeec20 | 1602 | if (UNLIKELY(cBOOL(isU8_ALPHANUMERIC_LC(i)) != cBOOL(isALPHANUMERIC_A(i)))) { |
8b7358b9 KW |
1603 | is_bad = TRUE; |
1604 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1605 | "isalnum('%s') unexpectedly is %d\n", | |
dcbeec20 | 1606 | name, cBOOL(isU8_ALPHANUMERIC_LC(i)))); |
8b7358b9 | 1607 | } |
dcbeec20 | 1608 | if (UNLIKELY(cBOOL(isU8_ALPHA_LC(i)) != cBOOL(isALPHA_A(i)))) { |
8b7358b9 KW |
1609 | is_bad = TRUE; |
1610 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1611 | "isalpha('%s') unexpectedly is %d\n", | |
dcbeec20 | 1612 | name, cBOOL(isU8_ALPHA_LC(i)))); |
8b7358b9 | 1613 | } |
dcbeec20 | 1614 | if (UNLIKELY(cBOOL(isU8_DIGIT_LC(i)) != cBOOL(isDIGIT_A(i)))) { |
8b7358b9 KW |
1615 | is_bad = TRUE; |
1616 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1617 | "isdigit('%s') unexpectedly is %d\n", | |
dcbeec20 | 1618 | name, cBOOL(isU8_DIGIT_LC(i)))); |
8b7358b9 | 1619 | } |
dcbeec20 | 1620 | if (UNLIKELY(cBOOL(isU8_GRAPH_LC(i)) != cBOOL(isGRAPH_A(i)))) { |
8b7358b9 KW |
1621 | is_bad = TRUE; |
1622 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1623 | "isgraph('%s') unexpectedly is %d\n", | |
dcbeec20 | 1624 | name, cBOOL(isU8_GRAPH_LC(i)))); |
8b7358b9 | 1625 | } |
dcbeec20 | 1626 | if (UNLIKELY(cBOOL(isU8_LOWER_LC(i)) != cBOOL(isLOWER_A(i)))) { |
8b7358b9 KW |
1627 | is_bad = TRUE; |
1628 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1629 | "islower('%s') unexpectedly is %d\n", | |
dcbeec20 | 1630 | name, cBOOL(isU8_LOWER_LC(i)))); |
8b7358b9 | 1631 | } |
dcbeec20 | 1632 | if (UNLIKELY(cBOOL(isU8_PRINT_LC(i)) != cBOOL(isPRINT_A(i)))) { |
8b7358b9 KW |
1633 | is_bad = TRUE; |
1634 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1635 | "isprint('%s') unexpectedly is %d\n", | |
dcbeec20 | 1636 | name, cBOOL(isU8_PRINT_LC(i)))); |
8b7358b9 | 1637 | } |
dcbeec20 | 1638 | if (UNLIKELY(cBOOL(isU8_PUNCT_LC(i)) != cBOOL(isPUNCT_A(i)))) { |
8b7358b9 KW |
1639 | is_bad = TRUE; |
1640 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1641 | "ispunct('%s') unexpectedly is %d\n", | |
dcbeec20 | 1642 | name, cBOOL(isU8_PUNCT_LC(i)))); |
8b7358b9 | 1643 | } |
dcbeec20 | 1644 | if (UNLIKELY(cBOOL(isU8_SPACE_LC(i)) != cBOOL(isSPACE_A(i)))) { |
8b7358b9 KW |
1645 | is_bad = TRUE; |
1646 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1647 | "isspace('%s') unexpectedly is %d\n", | |
dcbeec20 | 1648 | name, cBOOL(isU8_SPACE_LC(i)))); |
8b7358b9 | 1649 | } |
dcbeec20 | 1650 | if (UNLIKELY(cBOOL(isU8_UPPER_LC(i)) != cBOOL(isUPPER_A(i)))) { |
8b7358b9 KW |
1651 | is_bad = TRUE; |
1652 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1653 | "isupper('%s') unexpectedly is %d\n", | |
dcbeec20 | 1654 | name, cBOOL(isU8_UPPER_LC(i)))); |
8b7358b9 | 1655 | } |
dcbeec20 | 1656 | if (UNLIKELY(cBOOL(isU8_XDIGIT_LC(i))!= cBOOL(isXDIGIT_A(i)))) { |
8b7358b9 KW |
1657 | is_bad = TRUE; |
1658 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1659 | "isxdigit('%s') unexpectedly is %d\n", | |
dcbeec20 | 1660 | name, cBOOL(isU8_XDIGIT_LC(i)))); |
8b7358b9 | 1661 | } |
dcbeec20 | 1662 | if (UNLIKELY(toU8_LOWER_LC(i) != (int) toLOWER_A(i))) { |
8b7358b9 KW |
1663 | is_bad = TRUE; |
1664 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1665 | "tolower('%s')=0x%x instead of the expected 0x%x\n", | |
dcbeec20 | 1666 | name, toU8_LOWER_LC(i), (int) toLOWER_A(i))); |
8b7358b9 | 1667 | } |
dcbeec20 | 1668 | if (UNLIKELY(toU8_UPPER_LC(i) != (int) toUPPER_A(i))) { |
8b7358b9 KW |
1669 | is_bad = TRUE; |
1670 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1671 | "toupper('%s')=0x%x instead of the expected 0x%x\n", | |
dcbeec20 | 1672 | name, toU8_UPPER_LC(i), (int) toUPPER_A(i))); |
8b7358b9 KW |
1673 | } |
1674 | if (UNLIKELY((i == '\n' && ! isCNTRL_LC(i)))) { | |
1675 | is_bad = TRUE; | |
1676 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
1677 | "'\\n' (=%02X) is not a control\n", (int) i)); | |
1678 | } | |
1679 | ||
1680 | /* Add to the list; Separate multiple entries with a blank */ | |
1681 | if (is_bad) { | |
1682 | if (bad_count) { | |
1683 | my_strlcat(bad_chars_list, " ", sizeof(bad_chars_list)); | |
8c6180a9 | 1684 | } |
8b7358b9 KW |
1685 | my_strlcat(bad_chars_list, name, sizeof(bad_chars_list)); |
1686 | bad_count++; | |
8c6180a9 KW |
1687 | } |
1688 | } | |
1689 | } | |
1690 | ||
30d8090d KW |
1691 | if (bad_count == 2 && maybe_utf8_turkic) { |
1692 | bad_count = 0; | |
1693 | *bad_chars_list = '\0'; | |
1694 | PL_fold_locale['I'] = 'I'; | |
1695 | PL_fold_locale['i'] = 'i'; | |
1696 | PL_in_utf8_turkic_locale = TRUE; | |
1697 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s is turkic\n", | |
1698 | __FILE__, __LINE__, newctype)); | |
1699 | } | |
1700 | else { | |
b8df1494 | 1701 | PL_in_utf8_turkic_locale = FALSE; |
30d8090d | 1702 | } |
b8df1494 | 1703 | |
7d4bcc4a KW |
1704 | # ifdef MB_CUR_MAX |
1705 | ||
8c6180a9 | 1706 | /* We only handle single-byte locales (outside of UTF-8 ones; so if |
d35fca5f | 1707 | * this locale requires more than one byte, there are going to be |
8c6180a9 | 1708 | * problems. */ |
9c8a6dc2 KW |
1709 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
1710 | "%s:%d: check_for_problems=%d, MB_CUR_MAX=%d\n", | |
1711 | __FILE__, __LINE__, check_for_problems, (int) MB_CUR_MAX)); | |
1712 | ||
8b7358b9 KW |
1713 | if ( check_for_problems && MB_CUR_MAX > 1 |
1714 | && ! PL_in_utf8_CTYPE_locale | |
ba1a4362 KW |
1715 | |
1716 | /* Some platforms return MB_CUR_MAX > 1 for even the "C" | |
1717 | * locale. Just assume that the implementation for them (plus | |
1718 | * for POSIX) is correct and the > 1 value is spurious. (Since | |
1719 | * these are specially handled to never be considered UTF-8 | |
1720 | * locales, as long as this is the only problem, everything | |
1721 | * should work fine */ | |
1722 | && strNE(newctype, "C") && strNE(newctype, "POSIX")) | |
1723 | { | |
8c6180a9 KW |
1724 | multi_byte_locale = TRUE; |
1725 | } | |
7d4bcc4a KW |
1726 | |
1727 | # endif | |
8c6180a9 | 1728 | |
30d8090d KW |
1729 | /* If we found problems and we want them output, do so */ |
1730 | if ( (UNLIKELY(bad_count) || UNLIKELY(multi_byte_locale)) | |
1731 | && (LIKELY(ckWARN_d(WARN_LOCALE)) || UNLIKELY(DEBUG_L_TEST))) | |
1732 | { | |
8b7358b9 KW |
1733 | if (UNLIKELY(bad_count) && PL_in_utf8_CTYPE_locale) { |
1734 | PL_warn_locale = Perl_newSVpvf(aTHX_ | |
1735 | "Locale '%s' contains (at least) the following characters" | |
578a6a87 KW |
1736 | " which have\nunexpected meanings: %s\nThe Perl program" |
1737 | " will use the expected meanings", | |
8b7358b9 | 1738 | newctype, bad_chars_list); |
8b7358b9 KW |
1739 | } |
1740 | else { | |
1741 | PL_warn_locale = Perl_newSVpvf(aTHX_ | |
8c6180a9 | 1742 | "Locale '%s' may not work well.%s%s%s\n", |
780fcc9f | 1743 | newctype, |
8c6180a9 KW |
1744 | (multi_byte_locale) |
1745 | ? " Some characters in it are not recognized by" | |
1746 | " Perl." | |
1747 | : "", | |
1748 | (bad_count) | |
1749 | ? "\nThe following characters (and maybe others)" | |
1750 | " may not have the same meaning as the Perl" | |
1751 | " program expects:\n" | |
1752 | : "", | |
1753 | (bad_count) | |
1754 | ? bad_chars_list | |
1755 | : "" | |
1756 | ); | |
8b7358b9 KW |
1757 | } |
1758 | ||
b119c2be KW |
1759 | # ifdef HAS_NL_LANGINFO |
1760 | ||
1761 | Perl_sv_catpvf(aTHX_ PL_warn_locale, "; codeset=%s", | |
1762 | /* parameter FALSE is a don't care here */ | |
4e6826bf | 1763 | my_nl_langinfo(CODESET, FALSE)); |
b119c2be KW |
1764 | |
1765 | # endif | |
1766 | ||
8b7358b9 KW |
1767 | Perl_sv_catpvf(aTHX_ PL_warn_locale, "\n"); |
1768 | ||
cc9eaeb0 | 1769 | /* If we are actually in the scope of the locale or are debugging, |
bddebb56 KW |
1770 | * output the message now. If not in that scope, we save the |
1771 | * message to be output at the first operation using this locale, | |
1772 | * if that actually happens. Most programs don't use locales, so | |
1773 | * they are immune to bad ones. */ | |
cc9eaeb0 | 1774 | if (IN_LC(LC_CTYPE) || UNLIKELY(DEBUG_L_TEST)) { |
780fcc9f | 1775 | |
780fcc9f | 1776 | /* The '0' below suppresses a bogus gcc compiler warning */ |
fe1c1494 KW |
1777 | Perl_warner(aTHX_ packWARN(WARN_LOCALE), SvPVX(PL_warn_locale), |
1778 | 0); | |
bddebb56 | 1779 | |
bddebb56 KW |
1780 | if (IN_LC(LC_CTYPE)) { |
1781 | SvREFCNT_dec_NN(PL_warn_locale); | |
1782 | PL_warn_locale = NULL; | |
1783 | } | |
780fcc9f | 1784 | } |
baa60164 | 1785 | } |
31f05a37 | 1786 | } |
98994639 | 1787 | |
fe1c1494 | 1788 | # endif /* USE_LOCALE_CTYPE */ |
7d4bcc4a | 1789 | |
98994639 HS |
1790 | } |
1791 | ||
98994639 | 1792 | void |
2726666d KW |
1793 | Perl__warn_problematic_locale() |
1794 | { | |
2726666d | 1795 | |
fe1c1494 | 1796 | # ifdef USE_LOCALE_CTYPE |
2726666d | 1797 | |
5f04a188 KW |
1798 | dTHX; |
1799 | ||
1800 | /* Internal-to-core function that outputs the message in PL_warn_locale, | |
1801 | * and then NULLS it. Should be called only through the macro | |
1629a27e | 1802 | * CHECK_AND_WARN_PROBLEMATIC_LOCALE_ */ |
5f04a188 | 1803 | |
2726666d | 1804 | if (PL_warn_locale) { |
2726666d KW |
1805 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), |
1806 | SvPVX(PL_warn_locale), | |
1807 | 0 /* dummy to avoid compiler warning */ ); | |
2726666d KW |
1808 | SvREFCNT_dec_NN(PL_warn_locale); |
1809 | PL_warn_locale = NULL; | |
1810 | } | |
1811 | ||
fe1c1494 | 1812 | # endif |
2726666d KW |
1813 | |
1814 | } | |
1815 | ||
a4f00dcc | 1816 | STATIC void |
c7dc1ea8 KW |
1817 | S_new_LC_ALL(pTHX_ const char *unused) |
1818 | { | |
1819 | unsigned int i; | |
1820 | ||
1821 | /* LC_ALL updates all the things we care about. */ | |
1822 | ||
1823 | PERL_UNUSED_ARG(unused); | |
1824 | ||
1825 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) { | |
1826 | if (update_functions[i]) { | |
1f9a6108 KW |
1827 | const char * this_locale = savepv(querylocale_i(i)); |
1828 | update_functions[i](aTHX_ this_locale); | |
1829 | Safefree(this_locale); | |
c7dc1ea8 KW |
1830 | } |
1831 | } | |
1832 | } | |
1833 | ||
1834 | STATIC void | |
a4f00dcc | 1835 | S_new_collate(pTHX_ const char *newcoll) |
98994639 | 1836 | { |
7d4bcc4a | 1837 | |
fe1c1494 | 1838 | # ifndef USE_LOCALE_COLLATE |
7d4bcc4a KW |
1839 | |
1840 | PERL_UNUSED_ARG(newcoll); | |
1841 | PERL_UNUSED_CONTEXT; | |
1842 | ||
fe1c1494 | 1843 | # else |
0d071d52 | 1844 | |
291a84fb | 1845 | /* Called after each libc setlocale() call affecting LC_COLLATE, to tell |
0d071d52 KW |
1846 | * core Perl this and that 'newcoll' is the name of the new locale. |
1847 | * | |
d35fca5f KW |
1848 | * The design of locale collation is that every locale change is given an |
1849 | * index 'PL_collation_ix'. The first time a string particpates in an | |
1850 | * operation that requires collation while locale collation is active, it | |
1851 | * is given PERL_MAGIC_collxfrm magic (via sv_collxfrm_flags()). That | |
1852 | * magic includes the collation index, and the transformation of the string | |
1853 | * by strxfrm(), q.v. That transformation is used when doing comparisons, | |
1854 | * instead of the string itself. If a string changes, the magic is | |
1855 | * cleared. The next time the locale changes, the index is incremented, | |
1856 | * and so we know during a comparison that the transformation is not | |
1857 | * necessarily still valid, and so is recomputed. Note that if the locale | |
1858 | * changes enough times, the index could wrap (a U32), and it is possible | |
1859 | * that a transformation would improperly be considered valid, leading to | |
1860 | * an unlikely bug */ | |
0d071d52 | 1861 | |
98994639 | 1862 | if (! newcoll) { |
1604cfb0 MS |
1863 | if (PL_collation_name) { |
1864 | ++PL_collation_ix; | |
1865 | Safefree(PL_collation_name); | |
1866 | PL_collation_name = NULL; | |
1867 | } | |
1868 | PL_collation_standard = TRUE; | |
00bf60ca | 1869 | is_standard_collation: |
1604cfb0 MS |
1870 | PL_collxfrm_base = 0; |
1871 | PL_collxfrm_mult = 2; | |
165a1c52 | 1872 | PL_in_utf8_COLLATE_locale = FALSE; |
f28f4d2a | 1873 | PL_strxfrm_NUL_replacement = '\0'; |
a4a439fb | 1874 | PL_strxfrm_max_cp = 0; |
1604cfb0 | 1875 | return; |
98994639 HS |
1876 | } |
1877 | ||
d35fca5f | 1878 | /* If this is not the same locale as currently, set the new one up */ |
98994639 | 1879 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
1604cfb0 MS |
1880 | ++PL_collation_ix; |
1881 | Safefree(PL_collation_name); | |
1882 | PL_collation_name = stdize_locale(savepv(newcoll)); | |
1883 | PL_collation_standard = isNAME_C_OR_POSIX(newcoll); | |
00bf60ca KW |
1884 | if (PL_collation_standard) { |
1885 | goto is_standard_collation; | |
1886 | } | |
98994639 | 1887 | |
165a1c52 | 1888 | PL_in_utf8_COLLATE_locale = _is_cur_LC_category_utf8(LC_COLLATE); |
f28f4d2a | 1889 | PL_strxfrm_NUL_replacement = '\0'; |
a4a439fb | 1890 | PL_strxfrm_max_cp = 0; |
165a1c52 | 1891 | |
59c018b9 KW |
1892 | /* A locale collation definition includes primary, secondary, tertiary, |
1893 | * etc. weights for each character. To sort, the primary weights are | |
1894 | * used, and only if they compare equal, then the secondary weights are | |
1895 | * used, and only if they compare equal, then the tertiary, etc. | |
1896 | * | |
1897 | * strxfrm() works by taking the input string, say ABC, and creating an | |
1898 | * output transformed string consisting of first the primary weights, | |
1899 | * A¹B¹C¹ followed by the secondary ones, A²B²C²; and then the | |
1900 | * tertiary, etc, yielding A¹B¹C¹ A²B²C² A³B³C³ .... Some characters | |
1901 | * may not have weights at every level. In our example, let's say B | |
1902 | * doesn't have a tertiary weight, and A doesn't have a secondary | |
1903 | * weight. The constructed string is then going to be | |
1904 | * A¹B¹C¹ B²C² A³C³ .... | |
1905 | * This has the desired effect that strcmp() will look at the secondary | |
1906 | * or tertiary weights only if the strings compare equal at all higher | |
1907 | * priority weights. The spaces shown here, like in | |
c342d20e | 1908 | * "A¹B¹C¹ A²B²C² " |
59c018b9 KW |
1909 | * are not just for readability. In the general case, these must |
1910 | * actually be bytes, which we will call here 'separator weights'; and | |
1911 | * they must be smaller than any other weight value, but since these | |
1912 | * are C strings, only the terminating one can be a NUL (some | |
1913 | * implementations may include a non-NUL separator weight just before | |
1914 | * the NUL). Implementations tend to reserve 01 for the separator | |
1915 | * weights. They are needed so that a shorter string's secondary | |
1916 | * weights won't be misconstrued as primary weights of a longer string, | |
1917 | * etc. By making them smaller than any other weight, the shorter | |
1918 | * string will sort first. (Actually, if all secondary weights are | |
1919 | * smaller than all primary ones, there is no need for a separator | |
1920 | * weight between those two levels, etc.) | |
1921 | * | |
1922 | * The length of the transformed string is roughly a linear function of | |
1923 | * the input string. It's not exactly linear because some characters | |
1924 | * don't have weights at all levels. When we call strxfrm() we have to | |
1925 | * allocate some memory to hold the transformed string. The | |
1926 | * calculations below try to find coefficients 'm' and 'b' for this | |
1927 | * locale so that m*x + b equals how much space we need, given the size | |
1928 | * of the input string in 'x'. If we calculate too small, we increase | |
1929 | * the size as needed, and call strxfrm() again, but it is better to | |
1930 | * get it right the first time to avoid wasted expensive string | |
1931 | * transformations. */ | |
1932 | ||
1604cfb0 | 1933 | { |
79f120c8 KW |
1934 | /* We use the string below to find how long the tranformation of it |
1935 | * is. Almost all locales are supersets of ASCII, or at least the | |
1936 | * ASCII letters. We use all of them, half upper half lower, | |
1937 | * because if we used fewer, we might hit just the ones that are | |
1938 | * outliers in a particular locale. Most of the strings being | |
1939 | * collated will contain a preponderance of letters, and even if | |
1940 | * they are above-ASCII, they are likely to have the same number of | |
1941 | * weight levels as the ASCII ones. It turns out that digits tend | |
1942 | * to have fewer levels, and some punctuation has more, but those | |
1943 | * are relatively sparse in text, and khw believes this gives a | |
1944 | * reasonable result, but it could be changed if experience so | |
1945 | * dictates. */ | |
1946 | const char longer[] = "ABCDEFGHIJKLMnopqrstuvwxyz"; | |
1947 | char * x_longer; /* Transformed 'longer' */ | |
1948 | Size_t x_len_longer; /* Length of 'x_longer' */ | |
1949 | ||
1950 | char * x_shorter; /* We also transform a substring of 'longer' */ | |
1951 | Size_t x_len_shorter; | |
1952 | ||
a4a439fb | 1953 | /* _mem_collxfrm() is used get the transformation (though here we |
79f120c8 KW |
1954 | * are interested only in its length). It is used because it has |
1955 | * the intelligence to handle all cases, but to work, it needs some | |
1956 | * values of 'm' and 'b' to get it started. For the purposes of | |
1957 | * this calculation we use a very conservative estimate of 'm' and | |
1958 | * 'b'. This assumes a weight can be multiple bytes, enough to | |
1959 | * hold any UV on the platform, and there are 5 levels, 4 weight | |
1960 | * bytes, and a trailing NUL. */ | |
1961 | PL_collxfrm_base = 5; | |
1962 | PL_collxfrm_mult = 5 * sizeof(UV); | |
1963 | ||
1964 | /* Find out how long the transformation really is */ | |
a4a439fb KW |
1965 | x_longer = _mem_collxfrm(longer, |
1966 | sizeof(longer) - 1, | |
1967 | &x_len_longer, | |
1968 | ||
1969 | /* We avoid converting to UTF-8 in the | |
1970 | * called function by telling it the | |
1971 | * string is in UTF-8 if the locale is a | |
1972 | * UTF-8 one. Since the string passed | |
1973 | * here is invariant under UTF-8, we can | |
1974 | * claim it's UTF-8 even though it isn't. | |
1975 | * */ | |
1976 | PL_in_utf8_COLLATE_locale); | |
79f120c8 KW |
1977 | Safefree(x_longer); |
1978 | ||
1979 | /* Find out how long the transformation of a substring of 'longer' | |
1980 | * is. Together the lengths of these transformations are | |
1981 | * sufficient to calculate 'm' and 'b'. The substring is all of | |
1982 | * 'longer' except the first character. This minimizes the chances | |
1983 | * of being swayed by outliers */ | |
a4a439fb | 1984 | x_shorter = _mem_collxfrm(longer + 1, |
79f120c8 | 1985 | sizeof(longer) - 2, |
a4a439fb KW |
1986 | &x_len_shorter, |
1987 | PL_in_utf8_COLLATE_locale); | |
79f120c8 KW |
1988 | Safefree(x_shorter); |
1989 | ||
1990 | /* If the results are nonsensical for this simple test, the whole | |
1991 | * locale definition is suspect. Mark it so that locale collation | |
1992 | * is not active at all for it. XXX Should we warn? */ | |
1993 | if ( x_len_shorter == 0 | |
1994 | || x_len_longer == 0 | |
1995 | || x_len_shorter >= x_len_longer) | |
1996 | { | |
1997 | PL_collxfrm_mult = 0; | |
1998 | PL_collxfrm_base = 0; | |
1999 | } | |
2000 | else { | |
2001 | SSize_t base; /* Temporary */ | |
2002 | ||
2003 | /* We have both: m * strlen(longer) + b = x_len_longer | |
2004 | * m * strlen(shorter) + b = x_len_shorter; | |
2005 | * subtracting yields: | |
2006 | * m * (strlen(longer) - strlen(shorter)) | |
2007 | * = x_len_longer - x_len_shorter | |
2008 | * But we have set things up so that 'shorter' is 1 byte smaller | |
2009 | * than 'longer'. Hence: | |
2010 | * m = x_len_longer - x_len_shorter | |
2011 | * | |
2012 | * But if something went wrong, make sure the multiplier is at | |
2013 | * least 1. | |
2014 | */ | |
2015 | if (x_len_longer > x_len_shorter) { | |
2016 | PL_collxfrm_mult = (STRLEN) x_len_longer - x_len_shorter; | |
2017 | } | |
2018 | else { | |
2019 | PL_collxfrm_mult = 1; | |
2020 | } | |
2021 | ||
2022 | /* mx + b = len | |
2023 | * so: b = len - mx | |
2024 | * but in case something has gone wrong, make sure it is | |
2025 | * non-negative */ | |
2026 | base = x_len_longer - PL_collxfrm_mult * (sizeof(longer) - 1); | |
2027 | if (base < 0) { | |
2028 | base = 0; | |
2029 | } | |
2030 | ||
2031 | /* Add 1 for the trailing NUL */ | |
2032 | PL_collxfrm_base = base + 1; | |
2033 | } | |
58eebef2 | 2034 | |
94d521c0 | 2035 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
b07929e4 KW |
2036 | "%s:%d: ?UTF-8 locale=%d; x_len_shorter=%zu, " |
2037 | "x_len_longer=%zu," | |
2038 | " collate multipler=%zu, collate base=%zu\n", | |
58eebef2 KW |
2039 | __FILE__, __LINE__, |
2040 | PL_in_utf8_COLLATE_locale, | |
2041 | x_len_shorter, x_len_longer, | |
94d521c0 | 2042 | PL_collxfrm_mult, PL_collxfrm_base)); |
1604cfb0 | 2043 | } |
98994639 HS |
2044 | } |
2045 | ||
fe1c1494 | 2046 | # endif /* USE_LOCALE_COLLATE */ |
7d4bcc4a | 2047 | |
98994639 HS |
2048 | } |
2049 | ||
fe1c1494 | 2050 | #endif /* USE_LOCALE */ |
d36adde0 | 2051 | |
d2b24094 | 2052 | #ifdef WIN32 |
b8cc575c | 2053 | |
6c332036 TC |
2054 | #define USE_WSETLOCALE |
2055 | ||
2056 | #ifdef USE_WSETLOCALE | |
2057 | ||
2058 | STATIC char * | |
2059 | S_wrap_wsetlocale(pTHX_ int category, const char *locale) { | |
2060 | wchar_t *wlocale; | |
2061 | wchar_t *wresult; | |
2062 | char *result; | |
2063 | ||
2064 | if (locale) { | |
2065 | int req_size = | |
2066 | MultiByteToWideChar(CP_UTF8, 0, locale, -1, NULL, 0); | |
2067 | ||
2068 | if (!req_size) { | |
2069 | errno = EINVAL; | |
2070 | return NULL; | |
2071 | } | |
2072 | ||
2073 | Newx(wlocale, req_size, wchar_t); | |
2074 | if (!MultiByteToWideChar(CP_UTF8, 0, locale, -1, wlocale, req_size)) { | |
2075 | Safefree(wlocale); | |
2076 | errno = EINVAL; | |
2077 | return NULL; | |
2078 | } | |
2079 | } | |
2080 | else { | |
2081 | wlocale = NULL; | |
2082 | } | |
2083 | wresult = _wsetlocale(category, wlocale); | |
2084 | Safefree(wlocale); | |
2085 | if (wresult) { | |
2086 | int req_size = | |
2087 | WideCharToMultiByte(CP_UTF8, 0, wresult, -1, NULL, 0, NULL, NULL); | |
2088 | Newx(result, req_size, char); | |
2089 | SAVEFREEPV(result); /* is there something better we can do here? */ | |
2090 | if (!WideCharToMultiByte(CP_UTF8, 0, wresult, -1, | |
2091 | result, req_size, NULL, NULL)) { | |
2092 | errno = EINVAL; | |
2093 | return NULL; | |
2094 | } | |
2095 | } | |
2096 | else { | |
2097 | result = NULL; | |
2098 | } | |
2099 | ||
2100 | return result; | |
2101 | } | |
2102 | ||
2103 | #endif | |
2104 | ||
a4f00dcc | 2105 | STATIC char * |
b8cc575c | 2106 | S_win32_setlocale(pTHX_ int category, const char* locale) |
b385bb4d KW |
2107 | { |
2108 | /* This, for Windows, emulates POSIX setlocale() behavior. There is no | |
7d4bcc4a KW |
2109 | * difference between the two unless the input locale is "", which normally |
2110 | * means on Windows to get the machine default, which is set via the | |
2111 | * computer's "Regional and Language Options" (or its current equivalent). | |
2112 | * In POSIX, it instead means to find the locale from the user's | |
2113 | * environment. This routine changes the Windows behavior to first look in | |
2114 | * the environment, and, if anything is found, use that instead of going to | |
2115 | * the machine default. If there is no environment override, the machine | |
2116 | * default is used, by calling the real setlocale() with "". | |
2117 | * | |
2118 | * The POSIX behavior is to use the LC_ALL variable if set; otherwise to | |
2119 | * use the particular category's variable if set; otherwise to use the LANG | |
2120 | * variable. */ | |
b385bb4d | 2121 | |
175c4cf9 | 2122 | bool override_LC_ALL = FALSE; |
89f7b9aa | 2123 | char * result; |
e5f10d49 | 2124 | unsigned int i; |
89f7b9aa | 2125 | |
b385bb4d | 2126 | if (locale && strEQ(locale, "")) { |
7d4bcc4a KW |
2127 | |
2128 | # ifdef LC_ALL | |
2129 | ||
b385bb4d KW |
2130 | locale = PerlEnv_getenv("LC_ALL"); |
2131 | if (! locale) { | |
e5f10d49 KW |
2132 | if (category == LC_ALL) { |
2133 | override_LC_ALL = TRUE; | |
2134 | } | |
2135 | else { | |
7d4bcc4a KW |
2136 | |
2137 | # endif | |
7d4bcc4a | 2138 | |
e5f10d49 KW |
2139 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) { |
2140 | if (category == categories[i]) { | |
2141 | locale = PerlEnv_getenv(category_names[i]); | |
2142 | goto found_locale; | |
2143 | } | |
2144 | } | |
7d4bcc4a | 2145 | |
b385bb4d | 2146 | locale = PerlEnv_getenv("LANG"); |
481465ea | 2147 | if (! locale) { |
b385bb4d KW |
2148 | locale = ""; |
2149 | } | |
e5f10d49 KW |
2150 | |
2151 | found_locale: ; | |
7d4bcc4a KW |
2152 | |
2153 | # ifdef LC_ALL | |
2154 | ||
e5f10d49 | 2155 | } |
b385bb4d | 2156 | } |
7d4bcc4a KW |
2157 | |
2158 | # endif | |
2159 | ||
b385bb4d KW |
2160 | } |
2161 | ||
6c332036 TC |
2162 | #ifdef USE_WSETLOCALE |
2163 | result = S_wrap_wsetlocale(aTHX_ category, locale); | |
2164 | #else | |
89f7b9aa | 2165 | result = setlocale(category, locale); |
6c332036 | 2166 | #endif |
48015184 KW |
2167 | DEBUG_L(STMT_START { |
2168 | dSAVE_ERRNO; | |
2169 | PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", __FILE__, __LINE__, | |
d188fe6d | 2170 | setlocale_debug_string_r(category, locale, result)); |
48015184 KW |
2171 | RESTORE_ERRNO; |
2172 | } STMT_END); | |
89f7b9aa | 2173 | |
481465ea | 2174 | if (! override_LC_ALL) { |
89f7b9aa KW |
2175 | return result; |
2176 | } | |
2177 | ||
dfd77d7a | 2178 | /* Here the input category was LC_ALL, and we have set it to what is in the |
481465ea KW |
2179 | * LANG variable or the system default if there is no LANG. But these have |
2180 | * lower priority than the other LC_foo variables, so override it for each | |
2181 | * one that is set. (If they are set to "", it means to use the same thing | |
2182 | * we just set LC_ALL to, so can skip) */ | |
7d4bcc4a | 2183 | |
4fd76d49 | 2184 | for (i = 0; i < LC_ALL_INDEX_; i++) { |
e5f10d49 KW |
2185 | result = PerlEnv_getenv(category_names[i]); |
2186 | if (result && strNE(result, "")) { | |
6c332036 | 2187 | #ifdef USE_WSETLOCALE |
17a1e9ac | 2188 | S_wrap_wsetlocale(aTHX_ categories[i], result); |
6c332036 | 2189 | #else |
17a1e9ac | 2190 | setlocale(categories[i], result); |
6c332036 | 2191 | #endif |
e5f10d49 KW |
2192 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
2193 | __FILE__, __LINE__, | |
d188fe6d | 2194 | setlocale_debug_string_i(i, result, "not captured"))); |
e5f10d49 | 2195 | } |
89f7b9aa | 2196 | } |
7d4bcc4a | 2197 | |
bbc98134 | 2198 | result = setlocale(LC_ALL, NULL); |
48015184 KW |
2199 | DEBUG_L(STMT_START { |
2200 | dSAVE_ERRNO; | |
2201 | PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", | |
bbc98134 | 2202 | __FILE__, __LINE__, |
d188fe6d | 2203 | setlocale_debug_string_c(LC_ALL, NULL, result)); |
48015184 KW |
2204 | RESTORE_ERRNO; |
2205 | } STMT_END); | |
89f7b9aa | 2206 | |
bbc98134 | 2207 | return result; |
b385bb4d KW |
2208 | } |
2209 | ||
2210 | #endif | |
2211 | ||
9aac5db8 | 2212 | /* |
9aac5db8 KW |
2213 | =for apidoc Perl_setlocale |
2214 | ||
2215 | This is an (almost) drop-in replacement for the system L<C<setlocale(3)>>, | |
2216 | taking the same parameters, and returning the same information, except that it | |
9487427b KW |
2217 | returns the correct underlying C<LC_NUMERIC> locale. Regular C<setlocale> will |
2218 | instead return C<C> if the underlying locale has a non-dot decimal point | |
2219 | character, or a non-empty thousands separator for displaying floating point | |
2220 | numbers. This is because perl keeps that locale category such that it has a | |
2221 | dot and empty separator, changing the locale briefly during the operations | |
2222 | where the underlying one is required. C<Perl_setlocale> knows about this, and | |
2223 | compensates; regular C<setlocale> doesn't. | |
9aac5db8 | 2224 | |
e9bc6d6b | 2225 | Another reason it isn't completely a drop-in replacement is that it is |
9aac5db8 | 2226 | declared to return S<C<const char *>>, whereas the system setlocale omits the |
163deff3 KW |
2227 | C<const> (presumably because its API was specified long ago, and can't be |
2228 | updated; it is illegal to change the information C<setlocale> returns; doing | |
2229 | so leads to segfaults.) | |
9aac5db8 | 2230 | |
e9bc6d6b KW |
2231 | Finally, C<Perl_setlocale> works under all circumstances, whereas plain |
2232 | C<setlocale> can be completely ineffective on some platforms under some | |
2233 | configurations. | |
2234 | ||
9aac5db8 | 2235 | C<Perl_setlocale> should not be used to change the locale except on systems |
e9bc6d6b KW |
2236 | where the predefined variable C<${^SAFE_LOCALES}> is 1. On some such systems, |
2237 | the system C<setlocale()> is ineffective, returning the wrong information, and | |
2238 | failing to actually change the locale. C<Perl_setlocale>, however works | |
2239 | properly in all circumstances. | |
9aac5db8 KW |
2240 | |
2241 | The return points to a per-thread static buffer, which is overwritten the next | |
2242 | time C<Perl_setlocale> is called from the same thread. | |
2243 | ||
2244 | =cut | |
2245 | ||
2246 | */ | |
2247 | ||
2248 | const char * | |
2249 | Perl_setlocale(const int category, const char * locale) | |
a4f00dcc KW |
2250 | { |
2251 | /* This wraps POSIX::setlocale() */ | |
2252 | ||
52129632 | 2253 | #ifndef USE_LOCALE |
d36adde0 KW |
2254 | |
2255 | PERL_UNUSED_ARG(category); | |
2256 | PERL_UNUSED_ARG(locale); | |
2257 | ||
2258 | return "C"; | |
2259 | ||
2260 | #else | |
2261 | ||
9aac5db8 | 2262 | const char * retval; |
c7dc1ea8 | 2263 | unsigned int cat_index; |
9aac5db8 | 2264 | dSAVEDERRNO; |
a4f00dcc | 2265 | dTHX; |
0bb37942 KW |
2266 | |
2267 | /* A NULL locale means only query what the current one is. */ | |
2268 | if (locale == NULL) { | |
2269 | ||
6a7aca81 | 2270 | # ifndef USE_LOCALE_NUMERIC |
0bb37942 | 2271 | |
6a7aca81 KW |
2272 | /* Without LC_NUMERIC, it's trivial; we just return the value */ |
2273 | return querylocale_r(category); | |
a4f00dcc | 2274 | |
6a7aca81 | 2275 | # else |
a4f00dcc | 2276 | |
0bb37942 KW |
2277 | /* We have the LC_NUMERIC name saved, because we are normally switched |
2278 | * into the C locale (or equivalent) for it. */ | |
a4f00dcc | 2279 | if (category == LC_NUMERIC) { |
9aac5db8 KW |
2280 | |
2281 | /* We don't have to copy this return value, as it is a per-thread | |
2282 | * variable, and won't change until a future setlocale */ | |
2283 | return PL_numeric_name; | |
a4f00dcc KW |
2284 | } |
2285 | ||
6a7aca81 KW |
2286 | # ifndef LC_ALL |
2287 | ||
2288 | /* Without LC_ALL, just return the value */ | |
2289 | return querylocale_r(category); | |
2290 | ||
2291 | # else | |
2292 | ||
2293 | /* Here, LC_ALL is available on this platform. It's the one | |
2294 | * complicating category (because it can contain a toggled LC_NUMERIC | |
2295 | * value), for all the remaining ones (we took care of LC_NUMERIC | |
2296 | * above), just return the value */ | |
2297 | if (category != LC_ALL) { | |
2298 | return querylocale_r(category); | |
2299 | } | |
2300 | ||
2301 | bool toggled = FALSE; | |
a4f00dcc | 2302 | |
0bb37942 KW |
2303 | /* For an LC_ALL query, switch back to the underlying numeric locale |
2304 | * (if we aren't there already) so as to get the correct results. Our | |
2305 | * records for all the other categories are valid without switching */ | |
6a7aca81 | 2306 | if (! PL_numeric_underlying) { |
996e6b9e KW |
2307 | set_numeric_underlying(); |
2308 | toggled = TRUE; | |
a4f00dcc KW |
2309 | } |
2310 | ||
6a7aca81 | 2311 | retval = querylocale_c(LC_ALL); |
996e6b9e KW |
2312 | |
2313 | if (toggled) { | |
0bb37942 | 2314 | |
996e6b9e KW |
2315 | /* This toggling back could destroy 'retval' */ |
2316 | retval = save_to_buffer(retval, | |
2317 | &PL_setlocale_buf, &PL_setlocale_bufsize, 0); | |
2318 | set_numeric_standard(); | |
6a7aca81 | 2319 | } |
0bb37942 KW |
2320 | |
2321 | return retval; | |
6a7aca81 KW |
2322 | |
2323 | # endif /* Has LC_ALL */ | |
2324 | # endif /* Has LC_NUMERIC */ | |
2325 | ||
0bb37942 | 2326 | } /* End of querying the current locale */ |
a4f00dcc | 2327 | |
c7dc1ea8 KW |
2328 | cat_index = get_category_index(category, NULL); |
2329 | retval = save_to_buffer(setlocale_i(cat_index, locale), | |
33e5a354 | 2330 | &PL_setlocale_buf, &PL_setlocale_bufsize, 0); |
9aac5db8 KW |
2331 | SAVE_ERRNO; |
2332 | ||
a4f00dcc KW |
2333 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
2334 | "%s:%d: %s\n", __FILE__, __LINE__, | |
d188fe6d | 2335 | setlocale_debug_string_r(category, locale, retval))); |
7d4bcc4a | 2336 | |
9aac5db8 KW |
2337 | RESTORE_ERRNO; |
2338 | ||
2339 | if (! retval) { | |
a4f00dcc KW |
2340 | return NULL; |
2341 | } | |
2342 | ||
fe1c1494 KW |
2343 | /* Now that have changed locales, we have to update our records to |
2344 | * correspond. Only certain categories have extra work to update. */ | |
c7dc1ea8 KW |
2345 | if (update_functions[cat_index]) { |
2346 | update_functions[cat_index](aTHX_ retval); | |
a4f00dcc KW |
2347 | } |
2348 | ||
2349 | return retval; | |
2350 | ||
d36adde0 KW |
2351 | #endif |
2352 | ||
f7416781 KW |
2353 | } |
2354 | ||
2355 | PERL_STATIC_INLINE const char * | |
fe1c1494 KW |
2356 | S_save_to_buffer(const char * string, char **buf, Size_t *buf_size, |
2357 | const Size_t offset) | |
f7416781 | 2358 | { |
fe1c1494 KW |
2359 | /* Copy the NUL-terminated 'string' to 'buf' + 'offset'. 'buf' has size |
2360 | * 'buf_size', growing it if necessary */ | |
f7416781 | 2361 | |
0b6697c5 | 2362 | Size_t string_size; |
f7416781 KW |
2363 | |
2364 | PERL_ARGS_ASSERT_SAVE_TO_BUFFER; | |
2365 | ||
0b6697c5 KW |
2366 | if (! string) { |
2367 | return NULL; | |
2368 | } | |
2369 | ||
2370 | string_size = strlen(string) + offset + 1; | |
2371 | ||
f7416781 KW |
2372 | if (*buf_size == 0) { |
2373 | Newx(*buf, string_size, char); | |
2374 | *buf_size = string_size; | |
2375 | } | |
2376 | else if (string_size > *buf_size) { | |
2377 | Renew(*buf, string_size, char); | |
2378 | *buf_size = string_size; | |
2379 | } | |
2380 | ||
2381 | Copy(string, *buf + offset, string_size - offset, char); | |
2382 | return *buf; | |
2383 | } | |
2384 | ||
2385 | /* | |
2386 | ||
f7416781 KW |
2387 | =for apidoc Perl_langinfo |
2388 | ||
ce181bf6 | 2389 | This is an (almost) drop-in replacement for the system C<L<nl_langinfo(3)>>, |
f7416781 KW |
2390 | taking the same C<item> parameter values, and returning the same information. |
2391 | But it is more thread-safe than regular C<nl_langinfo()>, and hides the quirks | |
2392 | of Perl's locale handling from your code, and can be used on systems that lack | |
2393 | a native C<nl_langinfo>. | |
2394 | ||
2395 | Expanding on these: | |
2396 | ||
2397 | =over | |
2398 | ||
2399 | =item * | |
2400 | ||
ce181bf6 KW |
2401 | The reason it isn't quite a drop-in replacement is actually an advantage. The |
2402 | only difference is that it returns S<C<const char *>>, whereas plain | |
2403 | C<nl_langinfo()> returns S<C<char *>>, but you are (only by documentation) | |
2404 | forbidden to write into the buffer. By declaring this C<const>, the compiler | |
2405 | enforces this restriction, so if it is violated, you know at compilation time, | |
2406 | rather than getting segfaults at runtime. | |
2407 | ||
2408 | =item * | |
2409 | ||
69e2275e | 2410 | It delivers the correct results for the C<RADIXCHAR> and C<THOUSEP> items, |
f7416781 KW |
2411 | without you having to write extra code. The reason for the extra code would be |
2412 | because these are from the C<LC_NUMERIC> locale category, which is normally | |
9487427b KW |
2413 | kept set by Perl so that the radix is a dot, and the separator is the empty |
2414 | string, no matter what the underlying locale is supposed to be, and so to get | |
2415 | the expected results, you have to temporarily toggle into the underlying | |
2416 | locale, and later toggle back. (You could use plain C<nl_langinfo> and | |
2417 | C<L</STORE_LC_NUMERIC_FORCE_TO_UNDERLYING>> for this but then you wouldn't get | |
2418 | the other advantages of C<Perl_langinfo()>; not keeping C<LC_NUMERIC> in the C | |
2419 | (or equivalent) locale would break a lot of CPAN, which is expecting the radix | |
2420 | (decimal point) character to be a dot.) | |
ce181bf6 KW |
2421 | |
2422 | =item * | |
2423 | ||
2424 | The system function it replaces can have its static return buffer trashed, | |
f1460a66 | 2425 | not only by a subsequent call to that function, but by a C<freelocale>, |
ce181bf6 KW |
2426 | C<setlocale>, or other locale change. The returned buffer of this function is |
2427 | not changed until the next call to it, so the buffer is never in a trashed | |
2428 | state. | |
f7416781 KW |
2429 | |
2430 | =item * | |
2431 | ||
ce181bf6 KW |
2432 | Its return buffer is per-thread, so it also is never overwritten by a call to |
2433 | this function from another thread; unlike the function it replaces. | |
2434 | ||
2435 | =item * | |
2436 | ||
2437 | But most importantly, it works on systems that don't have C<nl_langinfo>, such | |
2438 | as Windows, hence makes your code more portable. Of the fifty-some possible | |
2439 | items specified by the POSIX 2008 standard, | |
f7416781 | 2440 | L<http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/langinfo.h.html>, |
8d72e74e KW |
2441 | only one is completely unimplemented, though on non-Windows platforms, another |
2442 | significant one is also not implemented). It uses various techniques to | |
2443 | recover the other items, including calling C<L<localeconv(3)>>, and | |
2444 | C<L<strftime(3)>>, both of which are specified in C89, so should be always be | |
2445 | available. Later C<strftime()> versions have additional capabilities; C<""> is | |
2446 | returned for those not available on your system. | |
ce181bf6 KW |
2447 | |
2448 | It is important to note that when called with an item that is recovered by | |
2449 | using C<localeconv>, the buffer from any previous explicit call to | |
2450 | C<localeconv> will be overwritten. This means you must save that buffer's | |
c3997e39 KW |
2451 | contents if you need to access them after a call to this function. (But note |
2452 | that you might not want to be using C<localeconv()> directly anyway, because of | |
2453 | issues like the ones listed in the second item of this list (above) for | |
2454 | C<RADIXCHAR> and C<THOUSEP>. You can use the methods given in L<perlcall> to | |
2455 | call L<POSIX/localeconv> and avoid all the issues, but then you have a hash to | |
2456 | unpack). | |
9f0bc911 | 2457 | |
6201c220 KW |
2458 | The details for those items which may deviate from what this emulation returns |
2459 | and what a native C<nl_langinfo()> would return are specified in | |
2460 | L<I18N::Langinfo>. | |
f7416781 | 2461 | |
ce181bf6 KW |
2462 | =back |
2463 | ||
f7416781 KW |
2464 | When using C<Perl_langinfo> on systems that don't have a native |
2465 | C<nl_langinfo()>, you must | |
2466 | ||
2467 | #include "perl_langinfo.h" | |
2468 | ||
2469 | before the C<perl.h> C<#include>. You can replace your C<langinfo.h> | |
2470 | C<#include> with this one. (Doing it this way keeps out the symbols that plain | |
c3997e39 KW |
2471 | C<langinfo.h> would try to import into the namespace for code that doesn't need |
2472 | it.) | |
f7416781 | 2473 | |
f7416781 KW |
2474 | The original impetus for C<Perl_langinfo()> was so that code that needs to |
2475 | find out the current currency symbol, floating point radix character, or digit | |
2476 | grouping separator can use, on all systems, the simpler and more | |
2477 | thread-friendly C<nl_langinfo> API instead of C<L<localeconv(3)>> which is a | |
2478 | pain to make thread-friendly. For other fields returned by C<localeconv>, it | |
2479 | is better to use the methods given in L<perlcall> to call | |
2480 | L<C<POSIX::localeconv()>|POSIX/localeconv>, which is thread-friendly. | |
2481 | ||
2482 | =cut | |
2483 | ||
2484 | */ | |
2485 | ||
2486 | const char * | |
2487 | #ifdef HAS_NL_LANGINFO | |
2488 | Perl_langinfo(const nl_item item) | |
2489 | #else | |
2490 | Perl_langinfo(const int item) | |
2491 | #endif | |
2492 | { | |
f61748ac KW |
2493 | return my_nl_langinfo(item, TRUE); |
2494 | } | |
2495 | ||
59a15af0 | 2496 | STATIC const char * |
fe1c1494 | 2497 | # ifdef HAS_NL_LANGINFO |
f61748ac | 2498 | S_my_nl_langinfo(const nl_item item, bool toggle) |
fe1c1494 | 2499 | # else |
f61748ac | 2500 | S_my_nl_langinfo(const int item, bool toggle) |
fe1c1494 | 2501 | # endif |
f61748ac | 2502 | { |
ae74815b | 2503 | dTHX; |
0b6697c5 | 2504 | const char * retval; |
f7416781 | 2505 | |
fe1c1494 | 2506 | # ifdef USE_LOCALE_NUMERIC |
d36adde0 | 2507 | |
5a854ab3 KW |
2508 | /* We only need to toggle into the underlying LC_NUMERIC locale for these |
2509 | * two items, and only if not already there */ | |
4e6826bf | 2510 | if (toggle && (( item != RADIXCHAR && item != THOUSEP) |
5a854ab3 | 2511 | || PL_numeric_underlying)) |
d36adde0 | 2512 | |
fe1c1494 | 2513 | # endif /* No toggling needed if not using LC_NUMERIC */ |
d36adde0 | 2514 | |
5a854ab3 | 2515 | toggle = FALSE; |
5a854ab3 | 2516 | |
fe1c1494 KW |
2517 | /*--------------------------------------------------------------------------*/ |
2518 | /* Above is the common beginning to all the implementations of my_langinfo(). | |
2519 | * Below are the various completions */ | |
2520 | # if defined(HAS_NL_LANGINFO) /* nl_langinfo() is available. */ | |
ee90eb2d | 2521 | # if ! defined(HAS_THREAD_SAFE_NL_LANGINFO_L) \ |
4b06b4d0 | 2522 | || ! defined(USE_POSIX_2008_LOCALE) |
f7416781 | 2523 | |
ab340fff | 2524 | /* Here, use plain nl_langinfo(), switching to the underlying LC_NUMERIC |
ae74815b KW |
2525 | * for those items dependent on it. This must be copied to a buffer before |
2526 | * switching back, as some systems destroy the buffer when setlocale() is | |
2527 | * called */ | |
f7416781 | 2528 | |
038d3702 KW |
2529 | { |
2530 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; | |
2531 | ||
b2fee59c | 2532 | if (toggle) { |
038d3702 | 2533 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING(); |
b2fee59c | 2534 | } |
f7416781 | 2535 | |
7953f73f KW |
2536 | /* Prevent interference from another thread executing this code |
2537 | * section. */ | |
2538 | NL_LANGINFO_LOCK; | |
ee90eb2d | 2539 | |
628ff75a KW |
2540 | /* Copy to a per-thread buffer, which is also one that won't be |
2541 | * destroyed by a subsequent setlocale(), such as the | |
2542 | * RESTORE_LC_NUMERIC may do just below. */ | |
0b6697c5 KW |
2543 | retval = save_to_buffer(nl_langinfo(item), |
2544 | &PL_langinfo_buf, &PL_langinfo_bufsize, 0); | |
7953f73f | 2545 | NL_LANGINFO_UNLOCK; |
5acc4454 | 2546 | |
b2fee59c | 2547 | if (toggle) { |
038d3702 | 2548 | RESTORE_LC_NUMERIC(); |
b2fee59c | 2549 | } |
f7416781 | 2550 | } |
fe1c1494 KW |
2551 | /*--------------------------------------------------------------------------*/ |
2552 | # else /* Use nl_langinfo_l(), avoiding both a mutex and changing the | |
2553 | locale. */ | |
ab340fff | 2554 | |
5a854ab3 | 2555 | { |
b2fee59c KW |
2556 | bool do_free = FALSE; |
2557 | locale_t cur = uselocale((locale_t) 0); | |
ab340fff | 2558 | |
b2fee59c KW |
2559 | if (cur == LC_GLOBAL_LOCALE) { |
2560 | cur = duplocale(LC_GLOBAL_LOCALE); | |
2561 | do_free = TRUE; | |
2562 | } | |
ab340fff | 2563 | |
d36adde0 KW |
2564 | # ifdef USE_LOCALE_NUMERIC |
2565 | ||
b2fee59c | 2566 | if (toggle) { |
e1aa2579 KW |
2567 | if (PL_underlying_numeric_obj) { |
2568 | cur = PL_underlying_numeric_obj; | |
2569 | } | |
2570 | else { | |
2571 | cur = newlocale(LC_NUMERIC_MASK, PL_numeric_name, cur); | |
2572 | do_free = TRUE; | |
2573 | } | |
b2fee59c | 2574 | } |
ab340fff | 2575 | |
d36adde0 KW |
2576 | # endif |
2577 | ||
628ff75a KW |
2578 | /* We have to save it to a buffer, because the freelocale() just below |
2579 | * can invalidate the internal one */ | |
0b6697c5 KW |
2580 | retval = save_to_buffer(nl_langinfo_l(item, cur), |
2581 | &PL_langinfo_buf, &PL_langinfo_bufsize, 0); | |
ee90eb2d | 2582 | |
b2fee59c KW |
2583 | if (do_free) { |
2584 | freelocale(cur); | |
2585 | } | |
5a854ab3 | 2586 | } |
ab340fff | 2587 | |
c1566110 KW |
2588 | # endif |
2589 | ||
fe1c1494 | 2590 | /* We can return 'yes' and 'no' even if we didn't get a result */ |
0b6697c5 | 2591 | if (strEQ(retval, "")) { |
4e6826bf | 2592 | if (item == YESSTR) { |
c1566110 KW |
2593 | return "yes"; |
2594 | } | |
4e6826bf | 2595 | if (item == NOSTR) { |
c1566110 KW |
2596 | return "no"; |
2597 | } | |
2598 | } | |
2599 | ||
0b6697c5 | 2600 | return retval; |
fe1c1494 KW |
2601 | /*--------------------------------------------------------------------------*/ |
2602 | # else /* Below, emulate nl_langinfo as best we can */ | |
43dd6b15 KW |
2603 | |
2604 | { | |
2605 | ||
f7416781 KW |
2606 | # ifdef HAS_LOCALECONV |
2607 | ||
43dd6b15 | 2608 | const struct lconv* lc; |
628ff75a | 2609 | const char * temp; |
038d3702 | 2610 | DECLARATION_FOR_LC_NUMERIC_MANIPULATION; |
f7416781 | 2611 | |
69c5e0db KW |
2612 | # ifdef TS_W32_BROKEN_LOCALECONV |
2613 | ||
2614 | const char * save_global; | |
2615 | const char * save_thread; | |
2616 | int needed_size; | |
2617 | char * ptr; | |
2618 | char * e; | |
2619 | char * item_start; | |
2620 | ||
2621 | # endif | |
f7416781 KW |
2622 | # endif |
2623 | # ifdef HAS_STRFTIME | |
2624 | ||
43dd6b15 KW |
2625 | struct tm tm; |
2626 | bool return_format = FALSE; /* Return the %format, not the value */ | |
2627 | const char * format; | |
f7416781 KW |
2628 | |
2629 | # endif | |
2630 | ||
43dd6b15 KW |
2631 | /* We copy the results to a per-thread buffer, even if not |
2632 | * multi-threaded. This is in part to simplify this code, and partly | |
2633 | * because we need a buffer anyway for strftime(), and partly because a | |
2634 | * call of localeconv() could otherwise wipe out the buffer, and the | |
2635 | * programmer would not be expecting this, as this is a nl_langinfo() | |
2636 | * substitute after all, so s/he might be thinking their localeconv() | |
2637 | * is safe until another localeconv() call. */ | |
f7416781 | 2638 | |
43dd6b15 KW |
2639 | switch (item) { |
2640 | Size_t len; | |
f7416781 | 2641 | |
8d72e74e | 2642 | /* This is unimplemented */ |
4e6826bf | 2643 | case ERA: /* For use with strftime() %E modifier */ |
f7416781 | 2644 | |
43dd6b15 KW |
2645 | default: |
2646 | return ""; | |
f7416781 | 2647 | |
43dd6b15 | 2648 | /* We use only an English set, since we don't know any more */ |
4e6826bf KW |
2649 | case YESEXPR: return "^[+1yY]"; |
2650 | case YESSTR: return "yes"; | |
2651 | case NOEXPR: return "^[-0nN]"; | |
2652 | case NOSTR: return "no"; | |
2653 | ||
8d72e74e KW |
2654 | case CODESET: |
2655 | ||
2656 | # ifndef WIN32 | |
2657 | ||
2658 | /* On non-windows, this is unimplemented, in part because of | |
2659 | * inconsistencies between vendors. The Darwin native | |
2660 | * nl_langinfo() implementation simply looks at everything past | |
2661 | * any dot in the name, but that doesn't work for other | |
2662 | * vendors. Many Linux locales that don't have UTF-8 in their | |
2663 | * names really are UTF-8, for example; z/OS locales that do | |
2664 | * have UTF-8 in their names, aren't really UTF-8 */ | |
2665 | return ""; | |
2666 | ||
2667 | # else | |
2668 | ||
2669 | { /* But on Windows, the name does seem to be consistent, so | |
2670 | use that. */ | |
2671 | const char * p; | |
2672 | const char * first; | |
2673 | Size_t offset = 0; | |
9b271b93 | 2674 | const char * name = querylocale_c(LC_CTYPE); |
8d72e74e KW |
2675 | |
2676 | if (isNAME_C_OR_POSIX(name)) { | |
2677 | return "ANSI_X3.4-1968"; | |
2678 | } | |
2679 | ||
2680 | /* Find the dot in the locale name */ | |
2681 | first = (const char *) strchr(name, '.'); | |
2682 | if (! first) { | |
2683 | first = name; | |
2684 | goto has_nondigit; | |
2685 | } | |
2686 | ||
2687 | /* Look at everything past the dot */ | |
2688 | first++; | |
2689 | p = first; | |
2690 | ||
2691 | while (*p) { | |
2692 | if (! isDIGIT(*p)) { | |
2693 | goto has_nondigit; | |
2694 | } | |
2695 | ||
2696 | p++; | |
2697 | } | |
2698 | ||
2699 | /* Here everything past the dot is a digit. Treat it as a | |
2700 | * code page */ | |
18503cbd | 2701 | retval = save_to_buffer("CP", &PL_langinfo_buf, |
32a62865 | 2702 | &PL_langinfo_bufsize, 0); |
8d72e74e | 2703 | offset = STRLENs("CP"); |
f7416781 | 2704 | |
8d72e74e KW |
2705 | has_nondigit: |
2706 | ||
2707 | retval = save_to_buffer(first, &PL_langinfo_buf, | |
2708 | &PL_langinfo_bufsize, offset); | |
2709 | } | |
2710 | ||
2711 | break; | |
2712 | ||
2713 | # endif | |
f7416781 KW |
2714 | # ifdef HAS_LOCALECONV |
2715 | ||
4e6826bf | 2716 | case CRNCYSTR: |
f7416781 | 2717 | |
43dd6b15 KW |
2718 | /* We don't bother with localeconv_l() because any system that |
2719 | * has it is likely to also have nl_langinfo() */ | |
291a84fb | 2720 | |
8609fe00 KW |
2721 | LOCALECONV_LOCK; /* Prevent interference with other threads |
2722 | using localeconv() */ | |
69c5e0db KW |
2723 | |
2724 | # ifdef TS_W32_BROKEN_LOCALECONV | |
2725 | ||
2726 | /* This is a workaround for a Windows bug prior to VS 15. | |
2727 | * What we do here is, while locked, switch to the global | |
2728 | * locale so localeconv() works; then switch back just before | |
2729 | * the unlock. This can screw things up if some thread is | |
2730 | * already using the global locale while assuming no other is. | |
2731 | * A different workaround would be to call GetCurrencyFormat on | |
2732 | * a known value, and parse it; patches welcome | |
2733 | * | |
2734 | * We have to use LC_ALL instead of LC_MONETARY because of | |
2735 | * another bug in Windows */ | |
2736 | ||
9b271b93 | 2737 | save_thread = savepv(querylocale_c(LC_ALL)); |
69c5e0db | 2738 | _configthreadlocale(_DISABLE_PER_THREAD_LOCALE); |
9b271b93 | 2739 | save_global= savepv(querylocale_c(LC_ALL)); |
3980cddb | 2740 | void_setlocale_c(LC_ALL, save_thread); |
69c5e0db KW |
2741 | |
2742 | # endif | |
5acc4454 | 2743 | |
43dd6b15 | 2744 | lc = localeconv(); |
fe1c1494 | 2745 | |
43dd6b15 KW |
2746 | if ( ! lc |
2747 | || ! lc->currency_symbol | |
2748 | || strEQ("", lc->currency_symbol)) | |
2749 | { | |
8609fe00 | 2750 | LOCALECONV_UNLOCK; |
43dd6b15 KW |
2751 | return ""; |
2752 | } | |
f7416781 | 2753 | |
43dd6b15 | 2754 | /* Leave the first spot empty to be filled in below */ |
0b6697c5 KW |
2755 | retval = save_to_buffer(lc->currency_symbol, &PL_langinfo_buf, |
2756 | &PL_langinfo_bufsize, 1); | |
43dd6b15 KW |
2757 | if (lc->mon_decimal_point && strEQ(lc->mon_decimal_point, "")) |
2758 | { /* khw couldn't figure out how the localedef specifications | |
2759 | would show that the $ should replace the radix; this is | |
2760 | just a guess as to how it might work.*/ | |
a34edee3 | 2761 | PL_langinfo_buf[0] = '.'; |
43dd6b15 KW |
2762 | } |
2763 | else if (lc->p_cs_precedes) { | |
a34edee3 | 2764 | PL_langinfo_buf[0] = '-'; |
43dd6b15 KW |
2765 | } |
2766 | else { | |
a34edee3 | 2767 | PL_langinfo_buf[0] = '+'; |
43dd6b15 | 2768 | } |
f7416781 | 2769 | |
69c5e0db KW |
2770 | # ifdef TS_W32_BROKEN_LOCALECONV |
2771 | ||
3980cddb | 2772 | void_setlocale_c(LC_ALL, save_global); |
69c5e0db | 2773 | _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); |
3980cddb | 2774 | void_setlocale_c(LC_ALL, save_thread); |
69c5e0db KW |
2775 | Safefree(save_global); |
2776 | Safefree(save_thread); | |
2777 | ||
2778 | # endif | |
2779 | ||
8609fe00 | 2780 | LOCALECONV_UNLOCK; |
43dd6b15 | 2781 | break; |
f7416781 | 2782 | |
69c5e0db KW |
2783 | # ifdef TS_W32_BROKEN_LOCALECONV |
2784 | ||
4e6826bf | 2785 | case RADIXCHAR: |
69c5e0db KW |
2786 | |
2787 | /* For this, we output a known simple floating point number to | |
2788 | * a buffer, and parse it, looking for the radix */ | |
2789 | ||
2790 | if (toggle) { | |
2791 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING(); | |
2792 | } | |
2793 | ||
2794 | if (PL_langinfo_bufsize < 10) { | |
2795 | PL_langinfo_bufsize = 10; | |
2796 | Renew(PL_langinfo_buf, PL_langinfo_bufsize, char); | |
2797 | } | |
2798 | ||
2799 | needed_size = my_snprintf(PL_langinfo_buf, PL_langinfo_bufsize, | |
2800 | "%.1f", 1.5); | |
2801 | if (needed_size >= (int) PL_langinfo_bufsize) { | |
2802 | PL_langinfo_bufsize = needed_size + 1; | |
2803 | Renew(PL_langinfo_buf, PL_langinfo_bufsize, char); | |
fe1c1494 KW |
2804 | needed_size |
2805 | = my_snprintf(PL_langinfo_buf, PL_langinfo_bufsize, | |
69c5e0db KW |
2806 | "%.1f", 1.5); |
2807 | assert(needed_size < (int) PL_langinfo_bufsize); | |
2808 | } | |
2809 | ||
2810 | ptr = PL_langinfo_buf; | |
2811 | e = PL_langinfo_buf + PL_langinfo_bufsize; | |
2812 | ||
2813 | /* Find the '1' */ | |
2814 | while (ptr < e && *ptr != '1') { | |
2815 | ptr++; | |
2816 | } | |
2817 | ptr++; | |
2818 | ||
2819 | /* Find the '5' */ | |
2820 | item_start = ptr; | |
2821 | while (ptr < e && *ptr != '5') { | |
2822 | ptr++; | |
2823 | } | |
2824 | ||
2825 | /* Everything in between is the radix string */ | |
2826 | if (ptr >= e) { | |
2827 | PL_langinfo_buf[0] = '?'; | |
2828 | PL_langinfo_buf[1] = '\0'; | |
2829 | } | |
2830 | else { | |
2831 | *ptr = '\0'; | |
fe1c1494 KW |
2832 | Move(item_start, PL_langinfo_buf, ptr - PL_langinfo_buf, |
2833 | char); | |
69c5e0db KW |
2834 | } |
2835 | ||
2836 | if (toggle) { | |
2837 | RESTORE_LC_NUMERIC(); | |
2838 | } | |
2839 | ||
2840 | retval = PL_langinfo_buf; | |
2841 | break; | |
2842 | ||
2843 | # else | |
2844 | ||
2845 | case RADIXCHAR: /* No special handling needed */ | |
2846 | ||
2847 | # endif | |
2848 | ||
4e6826bf | 2849 | case THOUSEP: |
f7416781 | 2850 | |
43dd6b15 | 2851 | if (toggle) { |
038d3702 | 2852 | STORE_LC_NUMERIC_FORCE_TO_UNDERLYING(); |
c0d737a8 | 2853 | } |
f7416781 | 2854 | |
8609fe00 KW |
2855 | LOCALECONV_LOCK; /* Prevent interference with other threads |
2856 | using localeconv() */ | |
69c5e0db KW |
2857 | |
2858 | # ifdef TS_W32_BROKEN_LOCALECONV | |
2859 | ||
2860 | /* This should only be for the thousands separator. A | |
2861 | * different work around would be to use GetNumberFormat on a | |
2862 | * known value and parse the result to find the separator */ | |
9b271b93 | 2863 | save_thread = savepv(querylocale_c(LC_ALL)); |
69c5e0db | 2864 | _configthreadlocale(_DISABLE_PER_THREAD_LOCALE); |
9b271b93 | 2865 | save_global = savepv(querylocale_c(LC_ALL)); |
3980cddb | 2866 | void_setlocale_c(LC_ALL, save_thread); |
69c5e0db KW |
2867 | # if 0 |
2868 | /* This is the start of code that for broken Windows replaces | |
2869 | * the above and below code, and instead calls | |
2870 | * GetNumberFormat() and then would parse that to find the | |
2871 | * thousands separator. It needs to handle UTF-16 vs -8 | |
2872 | * issues. */ | |
2873 | ||
fe1c1494 KW |
2874 | needed_size = GetNumberFormatEx(PL_numeric_name, 0, "1234.5", |
2875 | NULL, PL_langinfo_buf, PL_langinfo_bufsize); | |
69c5e0db KW |
2876 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
2877 | "%s: %d: return from GetNumber, count=%d, val=%s\n", | |
2878 | __FILE__, __LINE__, needed_size, PL_langinfo_buf)); | |
2879 | ||
2880 | # endif | |
2881 | # endif | |
5acc4454 | 2882 | |
43dd6b15 KW |
2883 | lc = localeconv(); |
2884 | if (! lc) { | |
628ff75a | 2885 | temp = ""; |
33394adc | 2886 | } |
43dd6b15 | 2887 | else { |
4e6826bf | 2888 | temp = (item == RADIXCHAR) |
43dd6b15 KW |
2889 | ? lc->decimal_point |
2890 | : lc->thousands_sep; | |
628ff75a KW |
2891 | if (! temp) { |
2892 | temp = ""; | |
43dd6b15 KW |
2893 | } |
2894 | } | |
f7416781 | 2895 | |
0b6697c5 KW |
2896 | retval = save_to_buffer(temp, &PL_langinfo_buf, |
2897 | &PL_langinfo_bufsize, 0); | |
f7416781 | 2898 | |
69c5e0db KW |
2899 | # ifdef TS_W32_BROKEN_LOCALECONV |
2900 | ||
3980cddb | 2901 | void_setlocale_c(LC_ALL, save_global); |
69c5e0db | 2902 | _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); |
3980cddb | 2903 | void_setlocale_c(LC_ALL, save_thread); |
69c5e0db KW |
2904 | Safefree(save_global); |
2905 | Safefree(save_thread); | |
2906 | ||
2907 | # endif | |
2908 | ||
8609fe00 | 2909 | LOCALECONV_UNLOCK; |
5acc4454 | 2910 | |
43dd6b15 | 2911 | if (toggle) { |
038d3702 | 2912 | RESTORE_LC_NUMERIC(); |
43dd6b15 | 2913 | } |
f7416781 | 2914 | |
43dd6b15 | 2915 | break; |
f7416781 KW |
2916 | |
2917 | # endif | |
2918 | # ifdef HAS_STRFTIME | |
2919 | ||
43dd6b15 KW |
2920 | /* These are defined by C89, so we assume that strftime supports |
2921 | * them, and so are returned unconditionally; they may not be what | |
2922 | * the locale actually says, but should give good enough results | |
2923 | * for someone using them as formats (as opposed to trying to parse | |
2924 | * them to figure out what the locale says). The other format | |
2925 | * items are actually tested to verify they work on the platform */ | |
4e6826bf KW |
2926 | case D_FMT: return "%x"; |
2927 | case T_FMT: return "%X"; | |
2928 | case D_T_FMT: return "%c"; | |
43dd6b15 KW |
2929 | |
2930 | /* These formats are only available in later strfmtime's */ | |
4e6826bf | 2931 | case ERA_D_FMT: case ERA_T_FMT: case ERA_D_T_FMT: case T_FMT_AMPM: |
43dd6b15 KW |
2932 | |
2933 | /* The rest can be gotten from most versions of strftime(). */ | |
4e6826bf KW |
2934 | case ABDAY_1: case ABDAY_2: case ABDAY_3: |
2935 | case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7: | |
2936 | case ALT_DIGITS: | |
2937 | case AM_STR: case PM_STR: | |
2938 | case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4: | |
2939 | case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8: | |
2940 | case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12: | |
2941 | case DAY_1: case DAY_2: case DAY_3: case DAY_4: | |
2942 | case DAY_5: case DAY_6: case DAY_7: | |
2943 | case MON_1: case MON_2: case MON_3: case MON_4: | |
2944 | case MON_5: case MON_6: case MON_7: case MON_8: | |
2945 | case MON_9: case MON_10: case MON_11: case MON_12: | |
43dd6b15 | 2946 | |
43dd6b15 KW |
2947 | init_tm(&tm); /* Precaution against core dumps */ |
2948 | tm.tm_sec = 30; | |
2949 | tm.tm_min = 30; | |
2950 | tm.tm_hour = 6; | |
2951 | tm.tm_year = 2017 - 1900; | |
2952 | tm.tm_wday = 0; | |
2953 | tm.tm_mon = 0; | |
639a7c35 KW |
2954 | |
2955 | GCC_DIAG_IGNORE_STMT(-Wimplicit-fallthrough); | |
2956 | ||
43dd6b15 KW |
2957 | switch (item) { |
2958 | default: | |
43dd6b15 KW |
2959 | Perl_croak(aTHX_ |
2960 | "panic: %s: %d: switch case: %d problem", | |
2961 | __FILE__, __LINE__, item); | |
2962 | NOT_REACHED; /* NOTREACHED */ | |
2963 | ||
4e6826bf KW |
2964 | case PM_STR: tm.tm_hour = 18; |
2965 | case AM_STR: | |
43dd6b15 KW |
2966 | format = "%p"; |
2967 | break; | |
2968 | ||
4e6826bf KW |
2969 | case ABDAY_7: tm.tm_wday++; |
2970 | case ABDAY_6: tm.tm_wday++; | |
2971 | case ABDAY_5: tm.tm_wday++; | |
2972 | case ABDAY_4: tm.tm_wday++; | |
2973 | case ABDAY_3: tm.tm_wday++; | |
2974 | case ABDAY_2: tm.tm_wday++; | |
2975 | case ABDAY_1: | |
43dd6b15 KW |
2976 | format = "%a"; |
2977 | break; | |
2978 | ||
4e6826bf KW |
2979 | case DAY_7: tm.tm_wday++; |
2980 | case DAY_6: tm.tm_wday++; | |
2981 | case DAY_5: tm.tm_wday++; | |
2982 | case DAY_4: tm.tm_wday++; | |
2983 | case DAY_3: tm.tm_wday++; | |
2984 | case DAY_2: tm.tm_wday++; | |
2985 | case DAY_1: | |
43dd6b15 KW |
2986 | format = "%A"; |
2987 | break; | |
2988 | ||
4e6826bf KW |
2989 | case ABMON_12: tm.tm_mon++; |
2990 | case ABMON_11: tm.tm_mon++; | |
2991 | case ABMON_10: tm.tm_mon++; | |
2992 | case ABMON_9: tm.tm_mon++; | |
2993 | case ABMON_8: tm.tm_mon++; | |
2994 | case ABMON_7: tm.tm_mon++; | |
2995 | case ABMON_6: tm.tm_mon++; | |
2996 | case ABMON_5: tm.tm_mon++; | |
2997 | case ABMON_4: tm.tm_mon++; | |
2998 | case ABMON_3: tm.tm_mon++; | |
2999 | case ABMON_2: tm.tm_mon++; | |
3000 | case ABMON_1: | |
43dd6b15 KW |
3001 | format = "%b"; |
3002 | break; | |
3003 | ||
4e6826bf KW |
3004 | case MON_12: tm.tm_mon++; |
3005 | case MON_11: tm.tm_mon++; | |
3006 | case MON_10: tm.tm_mon++; | |
3007 | case MON_9: tm.tm_mon++; | |
3008 | case MON_8: tm.tm_mon++; | |
3009 | case MON_7: tm.tm_mon++; | |
3010 | case MON_6: tm.tm_mon++; | |
3011 | case MON_5: tm.tm_mon++; | |
3012 | case MON_4: tm.tm_mon++; | |
3013 | case MON_3: tm.tm_mon++; | |
3014 | case MON_2: tm.tm_mon++; | |
3015 | case MON_1: | |
43dd6b15 KW |
3016 | format = "%B"; |
3017 | break; | |
3018 | ||
4e6826bf | 3019 | case T_FMT_AMPM: |
43dd6b15 KW |
3020 | format = "%r"; |
3021 | return_format = TRUE; | |
3022 | break; | |
3023 | ||
4e6826bf | 3024 | case ERA_D_FMT: |
43dd6b15 KW |
3025 | format = "%Ex"; |
3026 | return_format = TRUE; | |
3027 | break; | |
3028 | ||
4e6826bf | 3029 | case ERA_T_FMT: |
43dd6b15 KW |
3030 | format = "%EX"; |
3031 | return_format = TRUE; | |
3032 | break; | |
3033 | ||
4e6826bf | 3034 | case ERA_D_T_FMT: |
43dd6b15 KW |
3035 | format = "%Ec"; |
3036 | return_format = TRUE; | |
3037 | break; | |
3038 | ||
4e6826bf | 3039 | case ALT_DIGITS: |
43dd6b15 KW |
3040 | tm.tm_wday = 0; |
3041 | format = "%Ow"; /* Find the alternate digit for 0 */ | |
3042 | break; | |
3043 | } | |
f7416781 | 3044 | |
639a7c35 KW |
3045 | GCC_DIAG_RESTORE_STMT; |
3046 | ||
43dd6b15 KW |
3047 | /* We can't use my_strftime() because it doesn't look at |
3048 | * tm_wday */ | |
3049 | while (0 == strftime(PL_langinfo_buf, PL_langinfo_bufsize, | |
3050 | format, &tm)) | |
3051 | { | |
3052 | /* A zero return means one of: | |
3053 | * a) there wasn't enough space in PL_langinfo_buf | |
3054 | * b) the format, like a plain %p, returns empty | |
3055 | * c) it was an illegal format, though some | |
3056 | * implementations of strftime will just return the | |
3057 | * illegal format as a plain character sequence. | |
3058 | * | |
3059 | * To quickly test for case 'b)', try again but precede | |
3060 | * the format with a plain character. If that result is | |
3061 | * still empty, the problem is either 'a)' or 'c)' */ | |
3062 | ||
3063 | Size_t format_size = strlen(format) + 1; | |
3064 | Size_t mod_size = format_size + 1; | |
3065 | char * mod_format; | |
3066 | char * temp_result; | |
3067 | ||
3068 | Newx(mod_format, mod_size, char); | |
3069 | Newx(temp_result, PL_langinfo_bufsize, char); | |
6873aa47 | 3070 | *mod_format = ' '; |
43dd6b15 KW |
3071 | my_strlcpy(mod_format + 1, format, mod_size); |
3072 | len = strftime(temp_result, | |
3073 | PL_langinfo_bufsize, | |
3074 | mod_format, &tm); | |
3075 | Safefree(mod_format); | |
3076 | Safefree(temp_result); | |
3077 | ||
3078 | /* If 'len' is non-zero, it means that we had a case like | |
3079 | * %p which means the current locale doesn't use a.m. or | |
3080 | * p.m., and that is valid */ | |
3081 | if (len == 0) { | |
3082 | ||
3083 | /* Here, still didn't work. If we get well beyond a | |
3084 | * reasonable size, bail out to prevent an infinite | |
3085 | * loop. */ | |
3086 | ||
3087 | if (PL_langinfo_bufsize > 100 * format_size) { | |
3088 | *PL_langinfo_buf = '\0'; | |
3089 | } | |
3090 | else { | |
3091 | /* Double the buffer size to retry; Add 1 in case | |
3092 | * original was 0, so we aren't stuck at 0. */ | |
3093 | PL_langinfo_bufsize *= 2; | |
3094 | PL_langinfo_bufsize++; | |
3095 | Renew(PL_langinfo_buf, PL_langinfo_bufsize, char); | |
3096 | continue; | |
3097 | } | |
3098 | } | |
f7416781 | 3099 | |
f7416781 | 3100 | break; |
43dd6b15 | 3101 | } |
f7416781 | 3102 | |
43dd6b15 KW |
3103 | /* Here, we got a result. |
3104 | * | |
fe1c1494 KW |
3105 | * If the item is 'ALT_DIGITS', 'PL_langinfo_buf' contains the |
3106 | * alternate format for wday 0. If the value is the same as the | |
3107 | * normal 0, there isn't an alternate, so clear the buffer. */ | |
3108 | if (item == ALT_DIGITS && strEQ(PL_langinfo_buf, "0")) { | |
43dd6b15 KW |
3109 | *PL_langinfo_buf = '\0'; |
3110 | } | |
f7416781 | 3111 | |
43dd6b15 | 3112 | /* ALT_DIGITS is problematic. Experiments on it showed that |
fe1c1494 KW |
3113 | * strftime() did not always work properly when going from alt-9 to |
3114 | * alt-10. Only a few locales have this item defined, and in all | |
3115 | * of them on Linux that khw was able to find, nl_langinfo() merely | |
3116 | * returned the alt-0 character, possibly doubled. Most Unicode | |
3117 | * digits are in blocks of 10 consecutive code points, so that is | |
3118 | * sufficient information for such scripts, as we can infer alt-1, | |
3119 | * alt-2, .... But for a Japanese locale, a CJK ideographic 0 is | |
3120 | * returned, and the CJK digits are not in code point order, so you | |
3121 | * can't really infer anything. The localedef for this locale did | |
3122 | * specify the succeeding digits, so that strftime() works properly | |
3123 | * on them, without needing to infer anything. But the | |
3124 | * nl_langinfo() return did not give sufficient information for the | |
3125 | * caller to understand what's going on. So until there is | |
3126 | * evidence that it should work differently, this returns the alt-0 | |
3127 | * string for ALT_DIGITS. | |
43dd6b15 KW |
3128 | * |
3129 | * wday was chosen because its range is all a single digit. | |
3130 | * Things like tm_sec have two digits as the minimum: '00' */ | |
f7416781 | 3131 | |
d1b150d9 KW |
3132 | retval = PL_langinfo_buf; |
3133 | ||
43dd6b15 | 3134 | /* If to return the format, not the value, overwrite the buffer |
fe1c1494 KW |
3135 | * with it. But some strftime()s will keep the original format if |
3136 | * illegal, so change those to "" */ | |
43dd6b15 KW |
3137 | if (return_format) { |
3138 | if (strEQ(PL_langinfo_buf, format)) { | |
f7416781 KW |
3139 | *PL_langinfo_buf = '\0'; |
3140 | } | |
43dd6b15 | 3141 | else { |
0b6697c5 KW |
3142 | retval = save_to_buffer(format, &PL_langinfo_buf, |
3143 | &PL_langinfo_bufsize, 0); | |
f7416781 KW |
3144 | } |
3145 | } | |
3146 | ||
3147 | break; | |
f7416781 KW |
3148 | |
3149 | # endif | |
3150 | ||
43dd6b15 | 3151 | } |
f7416781 KW |
3152 | } |
3153 | ||
0b6697c5 | 3154 | return retval; |
f7416781 | 3155 | |
fe1c1494 KW |
3156 | # endif |
3157 | /*--------------------------------------------------------------------------*/ | |
a4f00dcc | 3158 | } |
b385bb4d | 3159 | |
98994639 HS |
3160 | /* |
3161 | * Initialize locale awareness. | |
3162 | */ | |
3163 | int | |
3164 | Perl_init_i18nl10n(pTHX_ int printwarn) | |
3165 | { | |
0e92a118 KW |
3166 | /* printwarn is |
3167 | * | |
3168 | * 0 if not to output warning when setup locale is bad | |
3169 | * 1 if to output warning based on value of PERL_BADLANG | |
3170 | * >1 if to output regardless of PERL_BADLANG | |
3171 | * | |
3172 | * returns | |
98994639 | 3173 | * 1 = set ok or not applicable, |
0e92a118 KW |
3174 | * 0 = fallback to a locale of lower priority |
3175 | * -1 = fallback to all locales failed, not even to the C locale | |
6b058d42 KW |
3176 | * |
3177 | * Under -DDEBUGGING, if the environment variable PERL_DEBUG_LOCALE_INIT is | |
3178 | * set, debugging information is output. | |
3179 | * | |
b85af0ab KW |
3180 | * This looks more complicated than it is, mainly due to the #ifdefs and |
3181 | * error handling. | |
6b058d42 | 3182 | * |
b85af0ab | 3183 | * Besides some asserts, data structure initialization, and specific |
fe1c1494 KW |
3184 | * platform complications, this routine is effectively represented by this |
3185 | * pseudo-code: | |
b85af0ab | 3186 | * |
fe1c1494 KW |
3187 | * setlocale(LC_ALL, ""); x |
3188 | * foreach (subcategory) { x | |
3189 | * curlocales[f(subcategory)] = setlocale(subcategory, NULL); x | |
3190 | * } x | |
3191 | * if (platform_so_requires) { | |
3192 | * foreach (subcategory) { | |
3193 | * PL_curlocales[f(subcategory)] = curlocales[f(subcategory)] | |
3194 | * } | |
3195 | * } | |
3196 | * foreach (subcategory) { | |
3197 | * if (needs_special_handling[f(subcategory)] &this_subcat_handler | |
3198 | * } | |
b85af0ab | 3199 | * |
fe1c1494 KW |
3200 | * This sets all the categories to the values in the current environment, |
3201 | * saves them temporarily in curlocales[] until they can be handled and/or | |
3202 | * on some platforms saved in a per-thread array PL_curlocales[]. | |
b85af0ab | 3203 | * |
fe1c1494 KW |
3204 | * f(foo) is a mapping from the opaque system category numbers to small |
3205 | * non-negative integers used most everywhere in this file as indices into | |
3206 | * arrays (such as curlocales[]) so the program doesn't have to otherwise | |
3207 | * deal with the opaqueness. | |
b85af0ab | 3208 | * |
fe1c1494 KW |
3209 | * If the platform doesn't have LC_ALL, the lines marked 'x' above are |
3210 | * effectively replaced by: | |
3211 | * foreach (subcategory) { y | |
3212 | * curlocales[f(subcategory)] = setlocale(subcategory, ""); y | |
3213 | * } y | |
b85af0ab | 3214 | * |
fe1c1494 KW |
3215 | * The only differences being the lack of an LC_ALL call, and using "" |
3216 | * instead of NULL in the setlocale calls. | |
b85af0ab | 3217 | * |
fe1c1494 | 3218 | * But there are, of course, complications. |
b85af0ab | 3219 | * |
fe1c1494 KW |
3220 | * it has to deal with if this is an embedded perl, whose locale doesn't |
3221 | * come from the environment, but has been set up by the caller. This is | |
3222 | * pretty simply handled: the "" in the setlocale calls is not a string | |
3223 | * constant, but a variable which is set to NULL in the embedded case. | |
b85af0ab | 3224 | * |
fe1c1494 KW |
3225 | * But the major complication is handling failure and doing fallback. All |
3226 | * the code marked 'x' or 'y' above is actually enclosed in an outer loop, | |
3227 | * using the array trial_locales[]. On entry, trial_locales[] is | |
3228 | * initialized to just one entry, containing the NULL or "" locale argument | |
3229 | * shown above. If, as is almost always the case, everything works, it | |
3230 | * exits after just the one iteration, going on to the next step. | |
b85af0ab | 3231 | * |
fe1c1494 KW |
3232 | * But if there is a failure, the code tries its best to honor the |
3233 | * environment as much as possible. It self-modifies trial_locales[] to | |
3234 | * have more elements, one for each of the POSIX-specified settings from | |
3235 | * the environment, such as LANG, ending in the ultimate fallback, the C | |
3236 | * locale. Thus if there is something bogus with a higher priority | |
3237 | * environment variable, it will try with the next highest, until something | |
3238 | * works. If everything fails, it limps along with whatever state it got | |
3239 | * to. | |
b85af0ab KW |
3240 | * |
3241 | * A further complication is that Windows has an additional fallback, the | |
3242 | * user-default ANSI code page obtained from the operating system. This is | |
3243 | * added as yet another loop iteration, just before the final "C" | |
6b058d42 | 3244 | * |
1aa8e6dd KW |
3245 | * A slight complication is that in embedded Perls, the locale may already |
3246 | * be set-up, and we don't want to get it from the normal environment | |
3247 | * variables. This is handled by having a special environment variable | |
3248 | * indicate we're in this situation. We simply set setlocale's 2nd | |
3249 | * parameter to be a NULL instead of "". That indicates to setlocale that | |
3250 | * it is not to change anything, but to return the current value, | |
3251 | * effectively initializing perl's db to what the locale already is. | |
3252 | * | |
3253 | * We play the same trick with NULL if a LC_ALL succeeds. We call | |
3254 | * setlocale() on the individual categores with NULL to get their existing | |
3255 | * values for our db, instead of trying to change them. | |
3256 | * */ | |
1565c085 | 3257 | |
0e92a118 KW |
3258 | int ok = 1; |
3259 | ||
7d4bcc4a KW |
3260 | #ifndef USE_LOCALE |
3261 | ||
3262 | PERL_UNUSED_ARG(printwarn); | |
3263 | ||
3264 | #else /* USE_LOCALE */ | |
7d4bcc4a KW |
3265 | # ifdef __GLIBC__ |
3266 | ||
24f3e849 | 3267 | const char * const language = PerlEnv_getenv("LANGUAGE"); |
7d4bcc4a KW |
3268 | |
3269 | # endif | |
65ebb059 | 3270 | |
ccd65d51 KW |
3271 | /* NULL uses the existing already set up locale */ |
3272 | const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT")) | |
3273 | ? NULL | |
3274 | : ""; | |
c3fcd832 KW |
3275 | const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */ |
3276 | unsigned int trial_locales_count; | |
24f3e849 KW |
3277 | const char * const lc_all = PerlEnv_getenv("LC_ALL"); |
3278 | const char * const lang = PerlEnv_getenv("LANG"); | |
98994639 | 3279 | bool setlocale_failure = FALSE; |
65ebb059 | 3280 | unsigned int i; |
175c4cf9 KW |
3281 | |
3282 | /* A later getenv() could zap this, so only use here */ | |
3283 | const char * const bad_lang_use_once = PerlEnv_getenv("PERL_BADLANG"); | |
3284 | ||
3285 | const bool locwarn = (printwarn > 1 | |
e5f10d49 KW |
3286 | || ( printwarn |
3287 | && ( ! bad_lang_use_once | |
22ff3130 | 3288 | || ( |
e5f10d49 KW |
3289 | /* disallow with "" or "0" */ |
3290 | *bad_lang_use_once | |
3291 | && strNE("0", bad_lang_use_once))))); | |
ea92aad8 | 3292 | |
291a84fb | 3293 | /* setlocale() return vals; not copied so must be looked at immediately */ |
8de4332b | 3294 | const char * sl_result[NOMINAL_LC_ALL_INDEX + 1]; |
291a84fb KW |
3295 | |
3296 | /* current locale for given category; should have been copied so aren't | |
3297 | * volatile */ | |
8de4332b | 3298 | const char * curlocales[NOMINAL_LC_ALL_INDEX + 1]; |
291a84fb | 3299 | |
7d4bcc4a KW |
3300 | # ifdef WIN32 |
3301 | ||
6bce99ee JH |
3302 | /* In some systems you can find out the system default locale |
3303 | * and use that as the fallback locale. */ | |
7d4bcc4a KW |
3304 | # define SYSTEM_DEFAULT_LOCALE |
3305 | # endif | |
3306 | # ifdef SYSTEM_DEFAULT_LOCALE | |
3307 | ||
65ebb059 | 3308 | const char *system_default_locale = NULL; |
98994639 | 3309 | |
7d4bcc4a | 3310 | # endif |
948523db KW |
3311 | |
3312 | # ifndef DEBUGGING | |
3313 | # define DEBUG_LOCALE_INIT(a,b,c) | |
3314 | # else | |
7d4bcc4a | 3315 | |
8298454c | 3316 | DEBUG_INITIALIZATION_set(cBOOL(PerlEnv_getenv("PERL_DEBUG_LOCALE_INIT"))); |
7d4bcc4a | 3317 | |
d188fe6d | 3318 | # define DEBUG_LOCALE_INIT(cat_index, locale, result) \ |
94d521c0 KW |
3319 | DEBUG_L(PerlIO_printf(Perl_debug_log, \ |
3320 | "%s:%d: %s\n", __FILE__, __LINE__, \ | |
d188fe6d | 3321 | setlocale_debug_string_i(cat_index, locale, result))); |
2fcc0ca9 | 3322 | |
948523db KW |
3323 | /* Make sure the parallel arrays are properly set up */ |
3324 | # ifdef USE_LOCALE_NUMERIC | |
4fd76d49 KW |
3325 | assert(categories[LC_NUMERIC_INDEX_] == LC_NUMERIC); |
3326 | assert(strEQ(category_names[LC_NUMERIC_INDEX_], "LC_NUMERIC")); | |
e9bc6d6b | 3327 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3328 | assert(category_masks[LC_NUMERIC_INDEX_] == LC_NUMERIC_MASK); |
e9bc6d6b | 3329 | # endif |
948523db KW |
3330 | # endif |
3331 | # ifdef USE_LOCALE_CTYPE | |
4fd76d49 KW |
3332 | assert(categories[LC_CTYPE_INDEX_] == LC_CTYPE); |
3333 | assert(strEQ(category_names[LC_CTYPE_INDEX_], "LC_CTYPE")); | |
e9bc6d6b | 3334 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3335 | assert(category_masks[LC_CTYPE_INDEX_] == LC_CTYPE_MASK); |
e9bc6d6b | 3336 | # endif |
948523db KW |
3337 | # endif |
3338 | # ifdef USE_LOCALE_COLLATE | |
4fd76d49 KW |
3339 | assert(categories[LC_COLLATE_INDEX_] == LC_COLLATE); |
3340 | assert(strEQ(category_names[LC_COLLATE_INDEX_], "LC_COLLATE")); | |
e9bc6d6b | 3341 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3342 | assert(category_masks[LC_COLLATE_INDEX_] == LC_COLLATE_MASK); |
e9bc6d6b | 3343 | # endif |
948523db KW |
3344 | # endif |
3345 | # ifdef USE_LOCALE_TIME | |
4fd76d49 KW |
3346 | assert(categories[LC_TIME_INDEX_] == LC_TIME); |
3347 | assert(strEQ(category_names[LC_TIME_INDEX_], "LC_TIME")); | |
e9bc6d6b | 3348 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3349 | assert(category_masks[LC_TIME_INDEX_] == LC_TIME_MASK); |
e9bc6d6b | 3350 | # endif |
948523db KW |
3351 | # endif |
3352 | # ifdef USE_LOCALE_MESSAGES | |
4fd76d49 KW |
3353 | assert(categories[LC_MESSAGES_INDEX_] == LC_MESSAGES); |
3354 | assert(strEQ(category_names[LC_MESSAGES_INDEX_], "LC_MESSAGES")); | |
e9bc6d6b | 3355 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3356 | assert(category_masks[LC_MESSAGES_INDEX_] == LC_MESSAGES_MASK); |
e9bc6d6b | 3357 | # endif |
948523db KW |
3358 | # endif |
3359 | # ifdef USE_LOCALE_MONETARY | |
4fd76d49 KW |
3360 | assert(categories[LC_MONETARY_INDEX_] == LC_MONETARY); |
3361 | assert(strEQ(category_names[LC_MONETARY_INDEX_], "LC_MONETARY")); | |
e9bc6d6b | 3362 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3363 | assert(category_masks[LC_MONETARY_INDEX_] == LC_MONETARY_MASK); |
e9bc6d6b | 3364 | # endif |
948523db | 3365 | # endif |
9821811f | 3366 | # ifdef USE_LOCALE_ADDRESS |
4fd76d49 KW |
3367 | assert(categories[LC_ADDRESS_INDEX_] == LC_ADDRESS); |
3368 | assert(strEQ(category_names[LC_ADDRESS_INDEX_], "LC_ADDRESS")); | |
e9bc6d6b | 3369 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3370 | assert(category_masks[LC_ADDRESS_INDEX_] == LC_ADDRESS_MASK); |
e9bc6d6b | 3371 | # endif |
9821811f KW |
3372 | # endif |
3373 | # ifdef USE_LOCALE_IDENTIFICATION | |
4fd76d49 KW |
3374 | assert(categories[LC_IDENTIFICATION_INDEX_] == LC_IDENTIFICATION); |
3375 | assert(strEQ(category_names[LC_IDENTIFICATION_INDEX_], "LC_IDENTIFICATION")); | |
e9bc6d6b | 3376 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3377 | assert(category_masks[LC_IDENTIFICATION_INDEX_] == LC_IDENTIFICATION_MASK); |
e9bc6d6b | 3378 | # endif |
9821811f KW |
3379 | # endif |
3380 | # ifdef USE_LOCALE_MEASUREMENT | |
4fd76d49 KW |
3381 | assert(categories[LC_MEASUREMENT_INDEX_] == LC_MEASUREMENT); |
3382 | assert(strEQ(category_names[LC_MEASUREMENT_INDEX_], "LC_MEASUREMENT")); | |
e9bc6d6b | 3383 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3384 | assert(category_masks[LC_MEASUREMENT_INDEX_] == LC_MEASUREMENT_MASK); |
e9bc6d6b | 3385 | # endif |
9821811f KW |
3386 | # endif |
3387 | # ifdef USE_LOCALE_PAPER | |
4fd76d49 KW |
3388 | assert(categories[LC_PAPER_INDEX_] == LC_PAPER); |
3389 | assert(strEQ(category_names[LC_PAPER_INDEX_], "LC_PAPER")); | |
e9bc6d6b | 3390 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3391 | assert(category_masks[LC_PAPER_INDEX_] == LC_PAPER_MASK); |
e9bc6d6b | 3392 | # endif |
9821811f KW |
3393 | # endif |
3394 | # ifdef USE_LOCALE_TELEPHONE | |
4fd76d49 KW |
3395 | assert(categories[LC_TELEPHONE_INDEX_] == LC_TELEPHONE); |
3396 | assert(strEQ(category_names[LC_TELEPHONE_INDEX_], "LC_TELEPHONE")); | |
e9bc6d6b | 3397 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3398 | assert(category_masks[LC_TELEPHONE_INDEX_] == LC_TELEPHONE_MASK); |
e9bc6d6b | 3399 | # endif |
9821811f | 3400 | # endif |
36dbc955 | 3401 | # ifdef USE_LOCALE_SYNTAX |
4fd76d49 KW |
3402 | assert(categories[LC_SYNTAX_INDEX_] == LC_SYNTAX); |
3403 | assert(strEQ(category_names[LC_SYNTAX_INDEX_], "LC_SYNTAX")); | |
36dbc955 | 3404 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3405 | assert(category_masks[LC_SYNTAX_INDEX_] == LC_SYNTAX_MASK); |
36dbc955 KW |
3406 | # endif |
3407 | # endif | |
3408 | # ifdef USE_LOCALE_TOD | |
4fd76d49 KW |
3409 | assert(categories[LC_TOD_INDEX_] == LC_TOD); |
3410 | assert(strEQ(category_names[LC_TOD_INDEX_], "LC_TOD")); | |
36dbc955 | 3411 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3412 | assert(category_masks[LC_TOD_INDEX_] == LC_TOD_MASK); |
36dbc955 KW |
3413 | # endif |
3414 | # endif | |
948523db | 3415 | # ifdef LC_ALL |
4fd76d49 KW |
3416 | assert(categories[LC_ALL_INDEX_] == LC_ALL); |
3417 | assert(strEQ(category_names[LC_ALL_INDEX_], "LC_ALL")); | |
3418 | assert(NOMINAL_LC_ALL_INDEX == LC_ALL_INDEX_); | |
e9bc6d6b | 3419 | # ifdef USE_POSIX_2008_LOCALE |
4fd76d49 | 3420 | assert(category_masks[LC_ALL_INDEX_] == LC_ALL_MASK); |
e9bc6d6b | 3421 | # endif |
948523db KW |
3422 | # endif |
3423 | # endif /* DEBUGGING */ | |
47280b20 | 3424 | |
5a6637f0 KW |
3425 | /* Initialize the per-thread mbrFOO() state variables. See POSIX.xs for |
3426 | * why these particular incantations are used. */ | |
fe1c1494 | 3427 | # ifdef HAS_MBRLEN |
d2c9cb53 | 3428 | memzero(&PL_mbrlen_ps, sizeof(PL_mbrlen_ps)); |
fe1c1494 KW |
3429 | # endif |
3430 | # ifdef HAS_MBRTOWC | |
5a6637f0 | 3431 | memzero(&PL_mbrtowc_ps, sizeof(PL_mbrtowc_ps)); |
fe1c1494 KW |
3432 | # endif |
3433 | # ifdef HAS_WCTOMBR | |
5a6637f0 | 3434 | wcrtomb(NULL, L'\0', &PL_wcrtomb_ps); |
fe1c1494 | 3435 | # endif |
d2c9cb53 | 3436 | |
47280b20 KW |
3437 | /* Initialize the cache of the program's UTF-8ness for the always known |
3438 | * locales C and POSIX */ | |
3439 | my_strlcpy(PL_locale_utf8ness, C_and_POSIX_utf8ness, | |
3440 | sizeof(PL_locale_utf8ness)); | |
3441 | ||
8a2e41ff HS |
3442 | /* See https://github.com/Perl/perl5/issues/17824 */ |
3443 | Zero(curlocales, NOMINAL_LC_ALL_INDEX, char *); | |
3444 | ||
e9bc6d6b KW |
3445 | # ifdef USE_THREAD_SAFE_LOCALE |
3446 | # ifdef WIN32 | |
3447 | ||
3448 | _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); | |
3449 | ||
3450 | # endif | |
3451 | # endif | |
39e69e77 | 3452 | # ifdef USE_POSIX_2008_LOCALE |
e9bc6d6b KW |
3453 | |
3454 | PL_C_locale_obj = newlocale(LC_ALL_MASK, "C", (locale_t) 0); | |
3455 | if (! PL_C_locale_obj) { | |
3456 | Perl_croak_nocontext( | |
3457 | "panic: Cannot create POSIX 2008 C locale object; errno=%d", errno); | |
3458 | } | |
e9bc6d6b | 3459 | |
94d521c0 KW |
3460 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
3461 | "%s:%d: created C object %p\n", | |
3462 | __FILE__, __LINE__, PL_C_locale_obj)); | |
e9bc6d6b | 3463 | # endif |
78f7c09f KW |
3464 | # ifdef USE_LOCALE_NUMERIC |
3465 | ||
3ca88433 KW |
3466 | PL_numeric_radix_sv = newSVpvs("."); |
3467 | ||
78f7c09f | 3468 | # endif |
434e0069 | 3469 | # ifdef USE_PL_CURLOCALES |
e9bc6d6b KW |
3470 | |
3471 | /* Initialize our records. If we have POSIX 2008, we have LC_ALL */ | |
3980cddb | 3472 | void_setlocale_c(LC_ALL, porcelain_setlocale(LC_ALL, NULL)); |
e9bc6d6b KW |
3473 | |
3474 | # endif | |
98994639 | 3475 | |
65ebb059 | 3476 | /* We try each locale in the list until we get one that works, or exhaust |
20a240df KW |
3477 | * the list. Normally the loop is executed just once. But if setting the |
3478 | * locale fails, inside the loop we add fallback trials to the array and so | |
3479 | * will execute the loop multiple times */ | |
c3fcd832 KW |
3480 | trial_locales[0] = setlocale_init; |
3481 | trial_locales_count = 1; | |
7d4bcc4a | 3482 | |
65ebb059 KW |
3483 | for (i= 0; i < trial_locales_count; i++) { |
3484 | const char * trial_locale = trial_locales[i]; | |
503bf960 | 3485 | setlocale_failure = FALSE; |
65ebb059 KW |
3486 | |
3487 | if (i > 0) { | |
7d4bcc4a | 3488 | # ifdef SYSTEM_DEFAULT_LOCALE |
291a84fb | 3489 | # ifdef WIN32 /* Note that assumes Win32 has LC_ALL */ |
7d4bcc4a | 3490 | |
65ebb059 KW |
3491 | /* On Windows machines, an entry of "" after the 0th means to use |
3492 | * the system default locale, which we now proceed to get. */ | |
3493 | if (strEQ(trial_locale, "")) { | |
3494 | unsigned int j; | |
3495 | ||
3496 | /* Note that this may change the locale, but we are going to do | |
3497 | * that anyway just below */ | |
3980cddb | 3498 | system_default_locale = setlocale_c(LC_ALL, ""); |
d188fe6d | 3499 | DEBUG_LOCALE_INIT(LC_ALL_INDEX_, "", system_default_locale); |
65ebb059 | 3500 | |
7d4bcc4a | 3501 | /* Skip if invalid or if it's already on the list of locales to |
65ebb059 KW |
3502 | * try */ |
3503 | if (! system_default_locale) { | |
3504 | goto next_iteration; | |
3505 | } | |
3506 | for (j = 0; j < trial_locales_count; j++) { | |
3507 | if (strEQ(system_default_locale, trial_locales[j])) { | |
3508 | goto next_iteration; | |
3509 | } | |
3510 | } | |
3511 | ||
3512 | trial_locale = system_default_locale; | |
3513 | } | |
ec0202b5 KW |
3514 | # else |
3515 | # error SYSTEM_DEFAULT_LOCALE only implemented for Win32 | |
3516 | # endif | |
7d4bcc4a | 3517 | # endif /* SYSTEM_DEFAULT_LOCALE */ |
291a84fb KW |
3518 | |
3519 | } /* For i > 0 */ | |
65ebb059 | 3520 | |
7d4bcc4a KW |
3521 | # ifdef LC_ALL |
3522 | ||
3980cddb | 3523 | sl_result[LC_ALL_INDEX_] = setlocale_c(LC_ALL, trial_locale); |
d188fe6d | 3524 | DEBUG_LOCALE_INIT(LC_ALL_INDEX_, trial_locale, sl_result[LC_ALL_INDEX_]); |
4fd76d49 | 3525 | if (! sl_result[LC_ALL_INDEX_]) { |
49c85077 | 3526 | setlocale_failure = TRUE; |
7cd8b568 KW |
3527 | } |
3528 | else { | |
3529 | /* Since LC_ALL succeeded, it should have changed all the other | |
3530 | * categories it can to its value; so we massage things so that the | |
3531 | * setlocales below just return their category's current values. | |
3532 | * This adequately handles the case in NetBSD where LC_COLLATE may | |
3533 | * not be defined for a locale, and setting it individually will | |
7d4bcc4a | 3534 | * fail, whereas setting LC_ALL succeeds, leaving LC_COLLATE set to |
7cd8b568 KW |
3535 | * the POSIX locale. */ |
3536 | trial_locale = NULL; | |
3537 | } | |
7d4bcc4a KW |
3538 | |
3539 | # endif /* LC_ALL */ | |
98994639 | 3540 | |
e5f10d49 KW |
3541 | if (! setlocale_failure) { |
3542 | unsigned int j; | |
3543 | for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) { | |
cf59ced9 | 3544 | Safefree(curlocales[j]); |
3980cddb | 3545 | curlocales[j] = setlocale_i(j, trial_locale); |
e5f10d49 KW |
3546 | if (! curlocales[j]) { |
3547 | setlocale_failure = TRUE; | |
3548 | } | |
a683a66a | 3549 | curlocales[j] = savepv(curlocales[j]); |
d188fe6d | 3550 | DEBUG_LOCALE_INIT(j, trial_locale, curlocales[j]); |
e5f10d49 | 3551 | } |
c835d6be | 3552 | |
f6665bcd | 3553 | if (LIKELY(! setlocale_failure)) { /* All succeeded */ |
e5f10d49 | 3554 | break; /* Exit trial_locales loop */ |
49c85077 | 3555 | } |
65ebb059 | 3556 | } |
98994639 | 3557 | |
49c85077 KW |
3558 | /* Here, something failed; will need to try a fallback. */ |
3559 | ok = 0; | |
65ebb059 | 3560 | |
49c85077 KW |
3561 | if (i == 0) { |
3562 | unsigned int j; | |
98994639 | 3563 | |
65ebb059 | 3564 | if (locwarn) { /* Output failure info only on the first one */ |
7d4bcc4a KW |
3565 | |
3566 | # ifdef LC_ALL | |
98994639 | 3567 | |
49c85077 KW |
3568 | PerlIO_printf(Perl_error_log, |
3569 | "perl: warning: Setting locale failed.\n"); | |
98994639 | 3570 | |
7d4bcc4a | 3571 | # else /* !LC_ALL */ |
98994639 | 3572 | |
49c85077 | 3573 | PerlIO_printf(Perl_error_log, |
71ce8c74 | 3574 | "perl: warning: Setting locale failed for the categories:\n"); |
7d4bcc4a | 3575 | |
e5f10d49 KW |
3576 | for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) { |
3577 | if (! curlocales[j]) { | |
71ce8c74 | 3578 | PerlIO_printf(Perl_error_log, "\t%s\n", category_names[j]); |
e5f10d49 | 3579 | } |
e5f10d49 | 3580 | } |
7d4bcc4a | 3581 | |
7d4bcc4a | 3582 | # endif /* LC_ALL */ |
98994639 | 3583 | |
49c85077 KW |
3584 | PerlIO_printf(Perl_error_log, |
3585 | "perl: warning: Please check that your locale settings:\n"); | |
98994639 | 3586 | |
7d4bcc4a KW |
3587 | # ifdef __GLIBC__ |
3588 | ||
49c85077 KW |
3589 | PerlIO_printf(Perl_error_log, |
3590 | "\tLANGUAGE = %c%s%c,\n", | |
3591 | language ? '"' : '(', | |
3592 | language ? language : "unset", | |
3593 | language ? '"' : ')'); | |
7d4bcc4a | 3594 | # endif |
98994639 | 3595 | |
49c85077 KW |
3596 | PerlIO_printf(Perl_error_log, |
3597 | "\tLC_ALL = %c%s%c,\n", | |
3598 | lc_all ? '"' : '(', | |
3599 | lc_all ? lc_all : "unset", | |
3600 | lc_all ? '"' : ')'); | |
98994639 | 3601 | |
7d4bcc4a KW |
3602 | # if defined(USE_ENVIRON_ARRAY) |
3603 | ||
49c85077 | 3604 | { |
cd999af9 | 3605 | char **e; |
d5e32b93 KW |
3606 | |
3607 | /* Look through the environment for any variables of the | |
3608 | * form qr/ ^ LC_ [A-Z]+ = /x, except LC_ALL which was | |
3609 | * already handled above. These are assumed to be locale | |
3610 | * settings. Output them and their values. */ | |
cd999af9 | 3611 | for (e = environ; *e; e++) { |
d5e32b93 KW |
3612 | const STRLEN prefix_len = sizeof("LC_") - 1; |
3613 | STRLEN uppers_len; | |
3614 | ||
cd999af9 | 3615 | if ( strBEGINs(*e, "LC_") |
c8b388b0 | 3616 | && ! strBEGINs(*e, "LC_ALL=") |
d5e32b93 KW |
3617 | && (uppers_len = strspn(*e + prefix_len, |
3618 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) | |
3619 | && ((*e)[prefix_len + uppers_len] == '=')) | |
cd999af9 KW |
3620 | { |
3621 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", | |
d5e32b93 KW |
3622 | (int) (prefix_len + uppers_len), *e, |
3623 | *e + prefix_len + uppers_len + 1); | |
cd999af9 KW |
3624 | } |
3625 | } | |
49c85077 | 3626 | } |
7d4bcc4a KW |
3627 | |
3628 | # else | |
3629 | ||
49c85077 KW |
3630 | PerlIO_printf(Perl_error_log, |
3631 | "\t(possibly more locale environment variables)\n"); | |
7d4bcc4a KW |
3632 | |
3633 | # endif | |
98994639 | 3634 | |
49c85077 KW |
3635 | PerlIO_printf(Perl_error_log, |
3636 | "\tLANG = %c%s%c\n", | |
3637 | lang ? '"' : '(', | |
3638 | lang ? lang : "unset", | |
3639 | lang ? '"' : ')'); | |
98994639 | 3640 | |
49c85077 KW |
3641 | PerlIO_printf(Perl_error_log, |
3642 | " are supported and installed on your system.\n"); | |
3643 | } | |
98994639 | 3644 | |
65ebb059 | 3645 | /* Calculate what fallback locales to try. We have avoided this |
f6bab5f6 | 3646 | * until we have to, because failure is quite unlikely. This will |
65ebb059 KW |
3647 | * usually change the upper bound of the loop we are in. |
3648 | * | |
3649 | * Since the system's default way of setting the locale has not | |
3650 | * found one that works, We use Perl's defined ordering: LC_ALL, | |
3651 | * LANG, and the C locale. We don't try the same locale twice, so | |
3652 | * don't add to the list if already there. (On POSIX systems, the | |
3653 | * LC_ALL element will likely be a repeat of the 0th element "", | |
6b058d42 KW |
3654 | * but there's no harm done by doing it explicitly. |
3655 | * | |
3656 | * Note that this tries the LC_ALL environment variable even on | |
3657 | * systems which have no LC_ALL locale setting. This may or may | |
3658 | * not have been originally intentional, but there's no real need | |
3659 | * to change the behavior. */ | |
65ebb059 KW |
3660 | if (lc_all) { |
3661 | for (j = 0; j < trial_locales_count; j++) { | |
3662 | if (strEQ(lc_all, trial_locales[j])) { | |
3663 | goto done_lc_all; | |
3664 | } | |
3665 | } | |
3666 | trial_locales[trial_locales_count++] = lc_all; | |
3667 | } | |
3668 | done_lc_all: | |
98994639 | 3669 | |
65ebb059 KW |
3670 | if (lang) { |
3671 | for (j = 0; j < trial_locales_count; j++) { | |
3672 | if (strEQ(lang, trial_locales[j])) { | |
3673 | goto done_lang; | |
3674 | } | |
3675 | } | |
3676 | trial_locales[trial_locales_count++] = lang; | |
3677 | } | |
3678 | done_lang: | |
3679 | ||
7d4bcc4a KW |
3680 | # if defined(WIN32) && defined(LC_ALL) |
3681 | ||
65ebb059 KW |
3682 | /* For Windows, we also try the system default locale before "C". |
3683 | * (If there exists a Windows without LC_ALL we skip this because | |
3684 | * it gets too complicated. For those, the "C" is the next | |
3685 | * fallback possibility). The "" is the same as the 0th element of | |
3686 | * the array, but the code at the loop above knows to treat it | |
3687 | * differently when not the 0th */ | |
3688 | trial_locales[trial_locales_count++] = ""; | |
7d4bcc4a KW |
3689 | |
3690 | # endif | |
65ebb059 KW |
3691 | |
3692 | for (j = 0; j < trial_locales_count; j++) { | |
3693 | if (strEQ("C", trial_locales[j])) { | |
3694 | goto done_C; | |
3695 | } | |
3696 | } | |
3697 | trial_locales[trial_locales_count++] = "C"; | |
98994639 | 3698 | |
65ebb059 KW |
3699 | done_C: ; |
3700 | } /* end of first time through the loop */ | |
98994639 | 3701 | |
7d4bcc4a KW |
3702 | # ifdef WIN32 |
3703 | ||
65ebb059 | 3704 | next_iteration: ; |
7d4bcc4a KW |
3705 | |
3706 | # endif | |
65ebb059 KW |
3707 | |
3708 | } /* end of looping through the trial locales */ | |
3709 | ||
3710 | if (ok < 1) { /* If we tried to fallback */ | |
3711 | const char* msg; | |
3712 | if (! setlocale_failure) { /* fallback succeeded */ | |
3713 | msg = "Falling back to"; | |
3714 | } | |
3715 | else { /* fallback failed */ | |
e5f10d49 | 3716 | unsigned int j; |
98994639 | 3717 | |
65ebb059 KW |
3718 | /* We dropped off the end of the loop, so have to decrement i to |
3719 | * get back to the value the last time through */ | |
3720 | i--; | |
98994639 | 3721 | |
65ebb059 KW |
3722 | ok = -1; |
3723 | msg = "Failed to fall back to"; | |
3724 | ||
3725 | /* To continue, we should use whatever values we've got */ | |
7d4bcc4a | 3726 | |
e5f10d49 KW |
3727 | for (j = 0; j < NOMINAL_LC_ALL_INDEX; j++) { |
3728 | Safefree(curlocales[j]); | |
a683a66a | 3729 | curlocales[j] = savepv(querylocale_i(j)); |
d188fe6d | 3730 | DEBUG_LOCALE_INIT(j, NULL, curlocales[j]); |
e5f10d49 | 3731 | } |
65ebb059 KW |
3732 | } |
3733 | ||
3734 | if (locwarn) { | |
3735 | const char * description; | |
3736 | const char * name = ""; | |
3737 | if (strEQ(trial_locales[i], "C")) { | |
3738 | description = "the standard locale"; | |
3739 | name = "C"; | |
3740 | } | |
7d4bcc4a KW |
3741 | |
3742 | # ifdef SYSTEM_DEFAULT_LOCALE | |
3743 | ||
65ebb059 KW |
3744 | else if (strEQ(trial_locales[i], "")) { |
3745 | description = "the system default locale"; | |
3746 | if (system_default_locale) { | |
3747 | name = system_default_locale; | |
3748 | } | |
3749 | } | |
7d4bcc4a KW |
3750 | |
3751 | # endif /* SYSTEM_DEFAULT_LOCALE */ | |
3752 | ||
65ebb059 KW |
3753 | else { |
3754 | description = "a fallback locale"; | |
3755 | name = trial_locales[i]; | |
3756 | } | |
3757 | if (name && strNE(name, "")) { | |
3758 | PerlIO_printf(Perl_error_log, | |
3759 | "perl: warning: %s %s (\"%s\").\n", msg, description, name); | |
3760 | } | |
3761 | else { | |
3762 | PerlIO_printf(Perl_error_log, | |
3763 | "perl: warning: %s %s.\n", msg, description); | |
3764 | } | |
3765 | } | |
3766 | } /* End of tried to fallback */ | |
98994639 | 3767 | |
e5f10d49 | 3768 | /* Done with finding the locales; update our records */ |
c7dc1ea8 | 3769 | new_LC_ALL(NULL); |
e5f10d49 | 3770 | |
948523db | 3771 | for (i = 0; i < NOMINAL_LC_ALL_INDEX; i++) { |
47280b20 | 3772 | |
8751a11f | 3773 | # if defined(USE_LOCALE_THREADS) && ! defined(USE_THREAD_SAFE_LOCALE) |
47280b20 KW |
3774 | |
3775 | /* This caches whether each category's locale is UTF-8 or not. This | |
3776 | * may involve changing the locale. It is ok to do this at | |
e9bc6d6b KW |
3777 | * initialization time before any threads have started, but not later |
3778 | * unless thread-safe operations are used. | |
47280b20 KW |
3779 | * Caching means that if the program heeds our dictate not to change |
3780 | * locales in threaded applications, this data will remain valid, and | |
9de6fe47 KW |
3781 | * it may get queried without having to change locales. If the |
3782 | * environment is such that all categories have the same locale, this | |
3783 | * isn't needed, as the code will not change the locale; but this | |
3784 | * handles the uncommon case where the environment has disparate | |
3785 | * locales for the categories */ | |
47280b20 KW |
3786 | (void) _is_cur_LC_category_utf8(categories[i]); |
3787 | ||
3788 | # endif | |
3789 | ||
e5f10d49 KW |
3790 | Safefree(curlocales[i]); |
3791 | } | |
b310b053 | 3792 | |
7d4bcc4a KW |
3793 | # if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE) |
3794 | ||
49c85077 | 3795 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE |
50bf02bd KW |
3796 | * locale is UTF-8. The call to new_ctype() just above has already |
3797 | * calculated the latter value and saved it in PL_in_utf8_CTYPE_locale. If | |
3798 | * both PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE}) | |
3799 | * are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer on | |
3800 | * STDIN, STDOUT, STDERR, _and_ the default open discipline. */ | |
3801 | PL_utf8locale = PL_in_utf8_CTYPE_locale; | |
49c85077 | 3802 | |
a05d7ebb | 3803 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
fde18df1 JH |
3804 | This is an alternative to using the -C command line switch |
3805 | (the -C if present will override this). */ | |
3806 | { | |
1604cfb0 MS |
3807 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
3808 | PL_unicode = p ? parse_unicode_opts(&p) : 0; | |
3809 | if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG) | |
3810 | PL_utf8cache = -1; | |
b310b053 JH |
3811 | } |
3812 | ||
7d4bcc4a | 3813 | # endif |
e3305790 | 3814 | #endif /* USE_LOCALE */ |
7d4bcc4a | 3815 | |
2fcc0ca9 | 3816 | /* So won't continue to output stuff */ |
27cdc72e | 3817 | DEBUG_INITIALIZATION_set(FALSE); |
7d4bcc4a | 3818 | |
98994639 HS |
3819 | return ok; |
3820 | } | |
3821 | ||
98994639 HS |
3822 | #ifdef USE_LOCALE_COLLATE |
3823 | ||
a4a439fb | 3824 | char * |
a4a439fb KW |
3825 | Perl__mem_collxfrm(pTHX_ const char *input_string, |
3826 | STRLEN len, /* Length of 'input_string' */ | |
3827 | STRLEN *xlen, /* Set to length of returned string | |
3828 | (not including the collation index | |
3829 | prefix) */ | |
3830 | bool utf8 /* Is the input in UTF-8? */ | |
6696cfa7 | 3831 | ) |
98994639 | 3832 | { |
fe1c1494 KW |
3833 | /* _mem_collxfrm() is like strxfrm() but with two important differences. |
3834 | * First, it handles embedded NULs. Second, it allocates a bit more memory | |
3835 | * than needed for the transformed data itself. The real transformed data | |
3836 | * begins at offset COLLXFRM_HDR_LEN. *xlen is set to the length of that, | |
3837 | * and doesn't include the collation index size. | |
3838 | * | |
3839 | * It is the caller's responsibility to eventually free the memory returned | |
3840 | * by this function. | |
3841 | * | |
a4a439fb KW |
3842 | * Please see sv_collxfrm() to see how this is used. */ |
3843 | ||
fe1c1494 | 3844 | # define COLLXFRM_HDR_LEN sizeof(PL_collation_ix) |
55e5378d | 3845 | |
6696cfa7 KW |
3846 | char * s = (char *) input_string; |
3847 | STRLEN s_strlen = strlen(input_string); | |
79f120c8 | 3848 | char *xbuf = NULL; |
55e5378d | 3849 | STRLEN xAlloc; /* xalloc is a reserved word in VC */ |
17f41037 | 3850 | STRLEN length_in_chars; |
c664130f | 3851 | bool first_time = TRUE; /* Cleared after first loop iteration */ |
98994639 | 3852 | |
a4a439fb KW |
3853 | PERL_ARGS_ASSERT__MEM_COLLXFRM; |
3854 | ||
3855 | /* Must be NUL-terminated */ | |
3856 | assert(*(input_string + len) == '\0'); | |
7918f24d | 3857 | |
79f120c8 KW |
3858 | /* If this locale has defective collation, skip */ |
3859 | if (PL_collxfrm_base == 0 && PL_collxfrm_mult == 0) { | |
c7202dee KW |
3860 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3861 | "_mem_collxfrm: locale's collation is defective\n")); | |
79f120c8 KW |
3862 | goto bad; |
3863 | } | |
3864 | ||
6696cfa7 KW |
3865 | /* Replace any embedded NULs with the control that sorts before any others. |
3866 | * This will give as good as possible results on strings that don't | |
3867 | * otherwise contain that character, but otherwise there may be | |
3868 | * less-than-perfect results with that character and NUL. This is | |
fdc080f3 | 3869 | * unavoidable unless we replace strxfrm with our own implementation. */ |
fd43f63c KW |
3870 | if (UNLIKELY(s_strlen < len)) { /* Only execute if there is an embedded |
3871 | NUL */ | |
6696cfa7 KW |
3872 | char * e = s + len; |
3873 | char * sans_nuls; | |
fdc080f3 | 3874 | STRLEN sans_nuls_len; |
94762aa0 | 3875 | int try_non_controls; |
afc4976f KW |
3876 | char this_replacement_char[] = "?\0"; /* Room for a two-byte string, |
3877 | making sure 2nd byte is NUL. | |
3878 | */ | |
3879 | STRLEN this_replacement_len; | |
3880 | ||
1e4c9676 KW |
3881 | /* If we don't know what non-NUL control character sorts lowest for |
3882 | * this locale, find it */ | |
f28f4d2a | 3883 | if (PL_strxfrm_NUL_replacement == '\0') { |
6696cfa7 | 3884 | int j; |
afc4976f | 3885 | char * cur_min_x = NULL; /* The min_char's xfrm, (except it also |
6696cfa7 KW |
3886 | includes the collation index |
3887 | prefixed. */ | |
3888 | ||
91c0e2e0 | 3889 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "Looking to replace NUL\n")); |
94762aa0 KW |
3890 | |
3891 | /* Unlikely, but it may be that no control will work to replace | |
1e4c9676 KW |
3892 | * NUL, in which case we instead look for any character. Controls |
3893 | * are preferred because collation order is, in general, context | |
3894 | * sensitive, with adjoining characters affecting the order, and | |
3895 | * controls are less likely to have such interactions, allowing the | |
3896 | * NUL-replacement to stand on its own. (Another way to look at it | |
3897 | * is to imagine what would happen if the NUL were replaced by a | |
3898 | * combining character; it wouldn't work out all that well.) */ | |
94762aa0 KW |
3899 | for (try_non_controls = 0; |
3900 | try_non_controls < 2; | |
3901 | try_non_controls++) | |
3902 | { | |
d4ff9586 KW |
3903 | /* Look through all legal code points (NUL isn't) */ |
3904 | for (j = 1; j < 256; j++) { | |
3905 | char * x; /* j's xfrm plus collation index */ | |
3906 | STRLEN x_len; /* length of 'x' */ | |
3907 | STRLEN trial_len = 1; | |
736a4fed | 3908 | char cur_source[] = { '\0', '\0' }; |
d4ff9586 | 3909 | |
736a4fed KW |
3910 | /* Skip non-controls the first time through the loop. The |
3911 | * controls in a UTF-8 locale are the L1 ones */ | |
afc4976f KW |
3912 | if (! try_non_controls && (PL_in_utf8_COLLATE_locale) |
3913 | ? ! isCNTRL_L1(j) | |
3914 | : ! isCNTRL_LC(j)) | |
3915 | { | |
d4ff9586 | 3916 | continue; |
6696cfa7 | 3917 | } |
6696cfa7 | 3918 | |
736a4fed KW |
3919 | /* Create a 1-char string of the current code point */ |
3920 | cur_source[0] = (char) j; | |
3921 | ||
d4ff9586 KW |
3922 | /* Then transform it */ |
3923 | x = _mem_collxfrm(cur_source, trial_len, &x_len, | |
afc4976f | 3924 | 0 /* The string is not in UTF-8 */); |
6696cfa7 | 3925 | |
1e4c9676 | 3926 | /* Ignore any character that didn't successfully transform. |
d4ff9586 KW |
3927 | * */ |
3928 | if (! x) { | |
3929 | continue; | |
3930 | } | |
6696cfa7 | 3931 | |
d4ff9586 KW |
3932 | /* If this character's transformation is lower than |
3933 | * the current lowest, this one becomes the lowest */ | |
3934 | if ( cur_min_x == NULL | |
3935 | || strLT(x + COLLXFRM_HDR_LEN, | |
3936 | cur_min_x + COLLXFRM_HDR_LEN)) | |
3937 | { | |
f28f4d2a | 3938 | PL_strxfrm_NUL_replacement = j; |
62d66863 | 3939 | Safefree(cur_min_x); |
d4ff9586 | 3940 | cur_min_x = x; |
d4ff9586 KW |
3941 | } |
3942 | else { | |
3943 | Safefree(x); | |
3944 | } | |
1e4c9676 | 3945 | } /* end of loop through all 255 characters */ |
6696cfa7 | 3946 | |
1e4c9676 | 3947 | /* Stop looking if found */ |
94762aa0 KW |
3948 | if (cur_min_x) { |
3949 | break; | |
3950 | } | |
3951 | ||
3952 | /* Unlikely, but possible, if there aren't any controls that | |
3953 | * work in the locale, repeat the loop, looking for any | |
3954 | * character that works */ | |
3955 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
3956 | "_mem_collxfrm: No control worked. Trying non-controls\n")); | |
1e4c9676 | 3957 | } /* End of loop to try first the controls, then any char */ |
6696cfa7 | 3958 | |
94762aa0 KW |
3959 | if (! cur_min_x) { |
3960 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
3961 | "_mem_collxfrm: Couldn't find any character to replace" | |
3962 | " embedded NULs in locale %s with", PL_collation_name)); | |
3963 | goto bad; | |
58eebef2 KW |
3964 | } |
3965 | ||
94762aa0 KW |
3966 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
3967 | "_mem_collxfrm: Replacing embedded NULs in locale %s with " | |
f28f4d2a | 3968 | "0x%02X\n", PL_collation_name, PL_strxfrm_NUL_replacement)); |
94762aa0 | 3969 | |
6696cfa7 | 3970 | Safefree(cur_min_x); |
1e4c9676 | 3971 | } /* End of determining the character that is to replace NULs */ |
afc4976f KW |
3972 | |
3973 | /* If the replacement is variant under UTF-8, it must match the | |
291a84fb | 3974 | * UTF8-ness of the original */ |
f28f4d2a KW |
3975 | if ( ! UVCHR_IS_INVARIANT(PL_strxfrm_NUL_replacement) && utf8) { |
3976 | this_replacement_char[0] = | |
3977 | UTF8_EIGHT_BIT_HI(PL_strxfrm_NUL_replacement); | |
3978 | this_replacement_char[1] = | |
3979 | UTF8_EIGHT_BIT_LO(PL_strxfrm_NUL_replacement); | |
afc4976f KW |
3980 | this_replacement_len = 2; |
3981 | } | |
3982 | else { | |
f28f4d2a | 3983 | this_replacement_char[0] = PL_strxfrm_NUL_replacement; |
afc4976f KW |
3984 | /* this_replacement_char[1] = '\0' was done at initialization */ |
3985 | this_replacement_len = 1; | |
6696cfa7 KW |
3986 | } |
3987 | ||
3988 | /* The worst case length for the replaced string would be if every | |
3989 | * character in it is NUL. Multiply that by the length of each | |
3990 | * replacement, and allow for a trailing NUL */ | |
afc4976f | 3991 | sans_nuls_len = (len * this_replacement_len) + 1; |
fdc080f3 | 3992 | Newx(sans_nuls, sans_nuls_len, char); |
6696cfa7 KW |
3993 | *sans_nuls = '\0'; |
3994 | ||
6696cfa7 KW |
3995 | /* Replace each NUL with the lowest collating control. Loop until have |
3996 | * exhausted all the NULs */ | |
3997 | while (s + s_strlen < e) { | |
6069d6c5 | 3998 | my_strlcat(sans_nuls, s, sans_nuls_len); |
6696cfa7 KW |
3999 | |
4000 | /* Do the actual replacement */ | |
6069d6c5 | 4001 | my_strlcat(sans_nuls, this_replacement_char, sans_nuls_len); |
6696cfa7 KW |
4002 | |
4003 | /* Move past the input NUL */ | |
4004 | s += s_strlen + 1; | |
4005 | s_strlen = strlen(s); | |
4006 | } | |
4007 | ||
4008 | /* And add anything that trails the final NUL */ | |
6069d6c5 | 4009 | my_strlcat(sans_nuls, s, sans_nuls_len); |
6696cfa7 KW |
4010 | |
4011 | /* Switch so below we transform this modified string */ | |
4012 | s = sans_nuls; | |
4013 | len = strlen(s); | |
1e4c9676 | 4014 | } /* End of replacing NULs */ |
6696cfa7 | 4015 | |
a4a439fb KW |
4016 | /* Make sure the UTF8ness of the string and locale match */ |
4017 | if (utf8 != PL_in_utf8_COLLATE_locale) { | |
9de6fe47 | 4018 | /* XXX convert above Unicode to 10FFFF? */ |
a4a439fb KW |
4019 | const char * const t = s; /* Temporary so we can later find where the |
4020 | input was */ | |
4021 | ||
4022 | /* Here they don't match. Change the string's to be what the locale is | |
4023 | * expecting */ | |
4024 | ||
4025 | if (! utf8) { /* locale is UTF-8, but input isn't; upgrade the input */ | |
4026 | s = (char *) bytes_to_utf8((const U8 *) s, &len); | |
4027 | utf8 = TRUE; | |
4028 | } | |
4029 | else { /* locale is not UTF-8; but input is; downgrade the input */ | |
4030 | ||
4031 | s = (char *) bytes_from_utf8((const U8 *) s, &len, &utf8); | |
4032 | ||
4033 | /* If the downgrade was successful we are done, but if the input | |
4034 | * contains things that require UTF-8 to represent, have to do | |
4035 | * damage control ... */ | |
4036 | if (UNLIKELY(utf8)) { | |
4037 | ||
4038 | /* What we do is construct a non-UTF-8 string with | |
4039 | * 1) the characters representable by a single byte converted | |
4040 | * to be so (if necessary); | |
4041 | * 2) and the rest converted to collate the same as the | |
4042 | * highest collating representable character. That makes | |
4043 | * them collate at the end. This is similar to how we | |
4044 | * handle embedded NULs, but we use the highest collating | |
4045 | * code point instead of the smallest. Like the NUL case, | |
4046 | * this isn't perfect, but is the best we can reasonably | |
4047 | * do. Every above-255 code point will sort the same as | |
4048 | * the highest-sorting 0-255 code point. If that code | |
4049 | * point can combine in a sequence with some other code | |
4050 | * points for weight calculations, us changing something to | |
4051 | * be it can adversely affect the results. But in most | |
4052 | * cases, it should work reasonably. And note that this is | |
4053 | * really an illegal situation: using code points above 255 | |
4054 | * on a locale where only 0-255 are valid. If two strings | |
4055 | * sort entirely equal, then the sort order for the | |
4056 | * above-255 code points will be in code point order. */ | |
4057 | ||
4058 | utf8 = FALSE; | |
4059 | ||
4060 | /* If we haven't calculated the code point with the maximum | |
4061 | * collating order for this locale, do so now */ | |
4062 | if (! PL_strxfrm_max_cp) { | |
4063 | int j; | |
4064 | ||
4065 | /* The current transformed string that collates the | |
4066 | * highest (except it also includes the prefixed collation | |
4067 | * index. */ | |
4068 | char * cur_max_x = NULL; | |
4069 | ||
4070 | /* Look through all legal code points (NUL isn't) */ | |
4071 | for (j = 1; j < 256; j++) { | |
4072 | char * x; | |
4073 | STRLEN x_len; | |
736a4fed | 4074 | char cur_source[] = { '\0', '\0' }; |
a4a439fb | 4075 | |
736a4fed KW |
4076 | /* Create a 1-char string of the current code point */ |
4077 | cur_source[0] = (char) j; | |
a4a439fb KW |
4078 | |
4079 | /* Then transform it */ | |
4080 | x = _mem_collxfrm(cur_source, 1, &x_len, FALSE); | |
4081 | ||
4082 | /* If something went wrong (which it shouldn't), just | |
4083 | * ignore this code point */ | |
94762aa0 | 4084 | if (! x) { |
a4a439fb KW |
4085 | continue; |
4086 | } | |
4087 | ||
4088 | /* If this character's transformation is higher than | |
4089 | * the current highest, this one becomes the highest */ | |
4090 | if ( cur_max_x == NULL | |
55e5378d KW |
4091 | || strGT(x + COLLXFRM_HDR_LEN, |
4092 | cur_max_x + COLLXFRM_HDR_LEN)) | |
a4a439fb KW |
4093 | { |
4094 | PL_strxfrm_max_cp = j; | |
62d66863 | 4095 | Safefree(cur_max_x); |
a4a439fb KW |
4096 | cur_max_x = x; |
4097 | } | |
4098 | else { | |
4099 | Safefree(x); | |
4100 | } | |
4101 | } | |
4102 | ||
94762aa0 KW |
4103 | if (! cur_max_x) { |
4104 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
4105 | "_mem_collxfrm: Couldn't find any character to" | |
4106 | " replace above-Latin1 chars in locale %s with", | |
4107 | PL_collation_name)); | |
4108 | goto bad; | |
4109 | } | |
4110 | ||
58eebef2 KW |
4111 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4112 | "_mem_collxfrm: highest 1-byte collating character" | |
4113 | " in locale %s is 0x%02X\n", | |
4114 | PL_collation_name, | |
4115 | PL_strxfrm_max_cp)); | |
58eebef2 | 4116 | |
a4a439fb KW |
4117 | Safefree(cur_max_x); |
4118 | } | |
4119 | ||
4120 | /* Here we know which legal code point collates the highest. | |
4121 | * We are ready to construct the non-UTF-8 string. The length | |
4122 | * will be at least 1 byte smaller than the input string | |
4123 | * (because we changed at least one 2-byte character into a | |
4124 | * single byte), but that is eaten up by the trailing NUL */ | |
4125 | Newx(s, len, char); | |
4126 | ||
4127 | { | |
4128 | STRLEN i; | |
4129 | STRLEN d= 0; | |
042d9e50 | 4130 | char * e = (char *) t + len; |
a4a439fb KW |
4131 | |
4132 | for (i = 0; i < len; i+= UTF8SKIP(t + i)) { | |
4133 | U8 cur_char = t[i]; | |
4134 | if (UTF8_IS_INVARIANT(cur_char)) { | |
4135 | s[d++] = cur_char; | |
4136 | } | |
042d9e50 | 4137 | else if (UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(t + i, e)) { |
a4a439fb KW |
4138 | s[d++] = EIGHT_BIT_UTF8_TO_NATIVE(cur_char, t[i+1]); |
4139 | } | |
4140 | else { /* Replace illegal cp with highest collating | |
4141 | one */ | |
4142 | s[d++] = PL_strxfrm_max_cp; | |
4143 | } | |
4144 | } | |
4145 | s[d++] = '\0'; | |
4146 | Renew(s, d, char); /* Free up unused space */ | |
4147 | } | |
4148 | } | |
4149 | } | |
4150 | ||
4151 | /* Here, we have constructed a modified version of the input. It could | |
4152 | * be that we already had a modified copy before we did this version. | |
4153 | * If so, that copy is no longer needed */ | |
4154 | if (t != input_string) { | |
4155 | Safefree(t); | |
4156 | } | |
4157 | } | |
4158 | ||
17f41037 KW |
4159 | length_in_chars = (utf8) |
4160 | ? utf8_length((U8 *) s, (U8 *) s + len) | |
4161 | : len; | |
4162 | ||
59c018b9 KW |
4163 | /* The first element in the output is the collation id, used by |
4164 | * sv_collxfrm(); then comes the space for the transformed string. The | |
4165 | * equation should give us a good estimate as to how much is needed */ | |
55e5378d | 4166 | xAlloc = COLLXFRM_HDR_LEN |
a4a439fb | 4167 | + PL_collxfrm_base |
17f41037 | 4168 | + (PL_collxfrm_mult * length_in_chars); |
a02a5408 | 4169 | Newx(xbuf, xAlloc, char); |
c7202dee KW |
4170 | if (UNLIKELY(! xbuf)) { |
4171 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
4172 | "_mem_collxfrm: Couldn't malloc %zu bytes\n", xAlloc)); | |
1604cfb0 | 4173 | goto bad; |
c7202dee | 4174 | } |
98994639 | 4175 | |
d35fca5f | 4176 | /* Store the collation id */ |
98994639 | 4177 | *(U32*)xbuf = PL_collation_ix; |
d35fca5f KW |
4178 | |
4179 | /* Then the transformation of the input. We loop until successful, or we | |
4180 | * give up */ | |
4ebeff16 | 4181 | for (;;) { |
1adab0a7 | 4182 | |
55e5378d | 4183 | *xlen = strxfrm(xbuf + COLLXFRM_HDR_LEN, s, xAlloc - COLLXFRM_HDR_LEN); |
4ebeff16 KW |
4184 | |
4185 | /* If the transformed string occupies less space than we told strxfrm() | |
4186 | * was available, it means it successfully transformed the whole | |
4187 | * string. */ | |
55e5378d | 4188 | if (*xlen < xAlloc - COLLXFRM_HDR_LEN) { |
17f41037 | 4189 | |
1adab0a7 KW |
4190 | /* Some systems include a trailing NUL in the returned length. |
4191 | * Ignore it, using a loop in case multiple trailing NULs are | |
4192 | * returned. */ | |
4193 | while ( (*xlen) > 0 | |
4194 | && *(xbuf + COLLXFRM_HDR_LEN + (*xlen) - 1) == '\0') | |
4195 | { | |
4196 | (*xlen)--; | |
4197 | } | |
4198 | ||
17f41037 KW |
4199 | /* If the first try didn't get it, it means our prediction was low. |
4200 | * Modify the coefficients so that we predict a larger value in any | |
4201 | * future transformations */ | |
4202 | if (! first_time) { | |
4203 | STRLEN needed = *xlen + 1; /* +1 For trailing NUL */ | |
4204 | STRLEN computed_guess = PL_collxfrm_base | |
4205 | + (PL_collxfrm_mult * length_in_chars); | |
e1c30f0c KW |
4206 | |
4207 | /* On zero-length input, just keep current slope instead of | |
4208 | * dividing by 0 */ | |
4209 | const STRLEN new_m = (length_in_chars != 0) | |
4210 | ? needed / length_in_chars | |
4211 | : PL_collxfrm_mult; | |
17f41037 KW |
4212 | |
4213 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
b07929e4 KW |
4214 | "%s: %d: initial size of %zu bytes for a length " |
4215 | "%zu string was insufficient, %zu needed\n", | |
17f41037 | 4216 | __FILE__, __LINE__, |
b07929e4 | 4217 | computed_guess, length_in_chars, needed)); |
17f41037 KW |
4218 | |
4219 | /* If slope increased, use it, but discard this result for | |
4220 | * length 1 strings, as we can't be sure that it's a real slope | |
4221 | * change */ | |
4222 | if (length_in_chars > 1 && new_m > PL_collxfrm_mult) { | |
7d4bcc4a KW |
4223 | |
4224 | # ifdef DEBUGGING | |
4225 | ||
17f41037 KW |
4226 | STRLEN old_m = PL_collxfrm_mult; |
4227 | STRLEN old_b = PL_collxfrm_base; | |
7d4bcc4a KW |
4228 | |
4229 | # endif | |
4230 | ||
17f41037 KW |
4231 | PL_collxfrm_mult = new_m; |
4232 | PL_collxfrm_base = 1; /* +1 For trailing NUL */ | |
4233 | computed_guess = PL_collxfrm_base | |
4234 | + (PL_collxfrm_mult * length_in_chars); | |
4235 | if (computed_guess < needed) { | |
4236 | PL_collxfrm_base += needed - computed_guess; | |
4237 | } | |
4238 | ||
4239 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
b07929e4 KW |
4240 | "%s: %d: slope is now %zu; was %zu, base " |
4241 | "is now %zu; was %zu\n", | |
17f41037 | 4242 | __FILE__, __LINE__, |
b07929e4 KW |
4243 | PL_collxfrm_mult, old_m, |
4244 | PL_collxfrm_base, old_b)); | |
17f41037 KW |
4245 | } |
4246 | else { /* Slope didn't change, but 'b' did */ | |
4247 | const STRLEN new_b = needed | |
4248 | - computed_guess | |
4249 | + PL_collxfrm_base; | |
4250 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
b07929e4 | 4251 | "%s: %d: base is now %zu; was %zu\n", |
17f41037 | 4252 | __FILE__, __LINE__, |
b07929e4 | 4253 | new_b, PL_collxfrm_base)); |
17f41037 KW |
4254 | PL_collxfrm_base = new_b; |
4255 | } | |
4256 | } | |
4257 | ||
4ebeff16 KW |
4258 | break; |
4259 | } | |
bb0f664e | 4260 | |
c7202dee KW |
4261 | if (UNLIKELY(*xlen >= PERL_INT_MAX)) { |
4262 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
4263 | "_mem_collxfrm: Needed %zu bytes, max permissible is %u\n", | |
4264 | *xlen, PERL_INT_MAX)); | |
4ebeff16 | 4265 | goto bad; |
c7202dee | 4266 | } |
d35fca5f | 4267 | |
c664130f | 4268 | /* A well-behaved strxfrm() returns exactly how much space it needs |
1adab0a7 KW |
4269 | * (usually not including the trailing NUL) when it fails due to not |
4270 | * enough space being provided. Assume that this is the case unless | |
4271 | * it's been proven otherwise */ | |
c664130f | 4272 | if (LIKELY(PL_strxfrm_is_behaved) && first_time) { |
55e5378d | 4273 | xAlloc = *xlen + COLLXFRM_HDR_LEN + 1; |
c664130f KW |
4274 | } |
4275 | else { /* Here, either: | |
4276 | * 1) The strxfrm() has previously shown bad behavior; or | |
4277 | * 2) It isn't the first time through the loop, which means | |
4278 | * that the strxfrm() is now showing bad behavior, because | |
4279 | * we gave it what it said was needed in the previous | |
4280 | * iteration, and it came back saying it needed still more. | |
4281 | * (Many versions of cygwin fit this. When the buffer size | |
4282 | * isn't sufficient, they return the input size instead of | |
4283 | * how much is needed.) | |
d4ff9586 KW |
4284 | * Increase the buffer size by a fixed percentage and try again. |
4285 | * */ | |
6ddd902c | 4286 | xAlloc += (xAlloc / 4) + 1; |
c664130f | 4287 | PL_strxfrm_is_behaved = FALSE; |
c664130f | 4288 | |
94d521c0 KW |
4289 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
4290 | "_mem_collxfrm required more space than previously" | |
4291 | " calculated for locale %s, trying again with new" | |
4292 | " guess=%zu+%zu\n", | |
3e25f6d2 | 4293 | PL_collation_name, COLLXFRM_HDR_LEN, |
94d521c0 | 4294 | xAlloc - COLLXFRM_HDR_LEN)); |
58eebef2 | 4295 | } |
c664130f | 4296 | |
4ebeff16 | 4297 | Renew(xbuf, xAlloc, char); |
c7202dee KW |
4298 | if (UNLIKELY(! xbuf)) { |
4299 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
4300 | "_mem_collxfrm: Couldn't realloc %zu bytes\n", xAlloc)); | |
4ebeff16 | 4301 | goto bad; |
c7202dee | 4302 | } |
c664130f KW |
4303 | |
4304 | first_time = FALSE; | |
4ebeff16 | 4305 | } |
98994639 | 4306 | |
94d521c0 KW |
4307 | DEBUG_Lv((print_collxfrm_input_and_return(s, s + len, xlen, utf8), |
4308 | PerlIO_printf(Perl_debug_log, "Its xfrm is:"), | |
7e2f38b2 KW |
4309 | PerlIO_printf(Perl_debug_log, "%s\n", |
4310 | _byte_dump_string((U8 *) xbuf + COLLXFRM_HDR_LEN, | |
94d521c0 | 4311 | *xlen, 1)))); |
58eebef2 | 4312 | |
fe1c1494 | 4313 | /* Free up unneeded space; retain enough for trailing NUL */ |
55e5378d | 4314 | Renew(xbuf, COLLXFRM_HDR_LEN + *xlen + 1, char); |
98994639 | 4315 | |
6696cfa7 KW |
4316 | if (s != input_string) { |
4317 | Safefree(s); | |
98994639 HS |
4318 | } |
4319 | ||
98994639 HS |
4320 | return xbuf; |
4321 | ||
4322 | bad: | |
7d4bcc4a | 4323 | |
94d521c0 | 4324 | DEBUG_Lv(print_collxfrm_input_and_return(s, s + len, NULL, utf8)); |
7d4bcc4a | 4325 | |
21dce8f4 KW |
4326 | Safefree(xbuf); |
4327 | if (s != input_string) { | |
4328 | Safefree(s); | |
4329 | } | |
4330 | *xlen = 0; | |
4331 | ||
98994639 HS |
4332 | return NULL; |
4333 | } | |
4334 | ||
7d4bcc4a | 4335 | # ifdef DEBUGGING |
c7202dee | 4336 | |
4cbaac56 | 4337 | STATIC void |
c7202dee KW |
4338 | S_print_collxfrm_input_and_return(pTHX_ |
4339 | const char * const s, | |
4340 | const char * const e, | |
4341 | const STRLEN * const xlen, | |
4342 | const bool is_utf8) | |
4343 | { | |
c7202dee KW |
4344 | |
4345 | PERL_ARGS_ASSERT_PRINT_COLLXFRM_INPUT_AND_RETURN; | |
4346 | ||
511e4ff7 DM |
4347 | PerlIO_printf(Perl_debug_log, "_mem_collxfrm[%" UVuf "]: returning ", |
4348 | (UV)PL_collation_ix); | |
c7202dee | 4349 | if (xlen) { |
08b6dc1d | 4350 | PerlIO_printf(Perl_debug_log, "%zu", *xlen); |
c7202dee KW |
4351 | } |
4352 | else { | |
4353 | PerlIO_printf(Perl_debug_log, "NULL"); | |
4354 | } | |
4355 | PerlIO_printf(Perl_debug_log, " for locale '%s', string='", | |
4356 | PL_collation_name); | |
9c8a6dc2 KW |
4357 | print_bytes_for_locale(s, e, is_utf8); |
4358 | ||
4359 | PerlIO_printf(Perl_debug_log, "'\n"); | |
4360 | } | |
4361 | ||
4d9252fd KW |
4362 | # endif /* DEBUGGING */ |
4363 | #endif /* USE_LOCALE_COLLATE */ | |
fe1c1494 | 4364 | |
1c0e67b5 KW |
4365 | #ifdef USE_LOCALE |
4366 | # ifdef DEBUGGING | |
4d9252fd | 4367 | |
9c8a6dc2 KW |
4368 | STATIC void |
4369 | S_print_bytes_for_locale(pTHX_ | |
4370 | const char * const s, | |
4371 | const char * const e, | |
4372 | const bool is_utf8) | |
4373 | { | |
4374 | const char * t = s; | |
4375 | bool prev_was_printable = TRUE; | |
4376 | bool first_time = TRUE; | |
4377 | ||
4378 | PERL_ARGS_ASSERT_PRINT_BYTES_FOR_LOCALE; | |
c7202dee KW |
4379 | |
4380 | while (t < e) { | |
4381 | UV cp = (is_utf8) | |
4382 | ? utf8_to_uvchr_buf((U8 *) t, e, NULL) | |
4383 | : * (U8 *) t; | |
4384 | if (isPRINT(cp)) { | |
4385 | if (! prev_was_printable) { | |
4386 | PerlIO_printf(Perl_debug_log, " "); | |
4387 | } | |
4388 | PerlIO_printf(Perl_debug_log, "%c", (U8) cp); | |
4389 | prev_was_printable = TRUE; | |
4390 | } | |
4391 | else { | |
4392 | if (! first_time) { | |
4393 | PerlIO_printf(Perl_debug_log, " "); | |
4394 | } | |
147e3846 | 4395 | PerlIO_printf(Perl_debug_log, "%02" UVXf, cp); |
c7202dee KW |
4396 | prev_was_printable = FALSE; |
4397 | } | |
4398 | t += (is_utf8) ? UTF8SKIP(t) : 1; | |
4399 | first_time = FALSE; | |
4400 | } | |
c7202dee KW |
4401 | } |
4402 | ||
1c0e67b5 | 4403 | # endif /* #ifdef DEBUGGING */ |
8ef6e574 | 4404 | |
962aa53f | 4405 | STATIC const char * |
fe1c1494 KW |
4406 | S_switch_category_locale_to_template(pTHX_ const int switch_category, |
4407 | const int template_category, | |
4408 | const char * template_locale) | |
962aa53f KW |
4409 | { |
4410 | /* Changes the locale for LC_'switch_category" to that of | |
4411 | * LC_'template_category', if they aren't already the same. If not NULL, | |
4412 | * 'template_locale' is the locale that 'template_category' is in. | |
4413 | * | |
9de6fe47 KW |
4414 | * Returns a copy of the name of the original locale for 'switch_category' |
4415 | * so can be switched back to with the companion function | |
4416 | * restore_switched_locale(), (NULL if no restoral is necessary.) */ | |
962aa53f | 4417 | |
b0fb28a3 | 4418 | const char * restore_to_locale = NULL; |
962aa53f KW |
4419 | |
4420 | if (switch_category == template_category) { /* No changes needed */ | |
4421 | return NULL; | |
4422 | } | |
4423 | ||
4424 | /* Find the original locale of the category we may need to change, so that | |
4425 | * it can be restored to later */ | |
a683a66a KW |
4426 | restore_to_locale = |
4427 | stdize_locale(savepv(querylocale_r(switch_category))); | |
962aa53f KW |
4428 | if (! restore_to_locale) { |
4429 | Perl_croak(aTHX_ | |
4430 | "panic: %s: %d: Could not find current %s locale, errno=%d\n", | |
4431 | __FILE__, __LINE__, category_name(switch_category), errno); | |
4432 | } | |
962aa53f KW |
4433 | |
4434 | /* If the locale of the template category wasn't passed in, find it now */ | |
4435 | if (template_locale == NULL) { | |
a683a66a | 4436 | template_locale = querylocale_r(template_category); |
962aa53f KW |
4437 | if (! template_locale) { |
4438 | Perl_croak(aTHX_ | |
4439 | "panic: %s: %d: Could not find current %s locale, errno=%d\n", | |
4440 | __FILE__, __LINE__, category_name(template_category), errno); | |
4441 | } | |
4442 | } | |
4443 | ||
4444 | /* It the locales are the same, there's nothing to do */ | |
4445 | if (strEQ(restore_to_locale, template_locale)) { | |
4446 | Safefree(restore_to_locale); | |
4447 | ||
4448 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s locale unchanged as %s\n", | |
e1c8059f | 4449 | category_name(switch_category), template_locale)); |
962aa53f KW |
4450 | |
4451 | return NULL; | |
4452 | } | |
4453 | ||
4454 | /* Finally, change the locale to the template one */ | |
3980cddb | 4455 | if (! bool_setlocale_r(switch_category, template_locale)) { |
962aa53f KW |
4456 | Perl_croak(aTHX_ |
4457 | "panic: %s: %d: Could not change %s locale to %s, errno=%d\n", | |
4458 | __FILE__, __LINE__, category_name(switch_category), | |
4459 | template_locale, errno); | |
4460 | } | |
4461 | ||
4462 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s locale switched to %s\n", | |
4463 | category_name(switch_category), template_locale)); | |
4464 | ||
4465 | return restore_to_locale; | |
4466 | } | |
4467 | ||
4468 | STATIC void | |
fe1c1494 KW |
4469 | S_restore_switched_locale(pTHX_ const int category, |
4470 | const char * const original_locale) | |
962aa53f | 4471 | { |
9de6fe47 KW |
4472 | /* Restores the locale for LC_'category' to 'original_locale' (which is a |
4473 | * copy that will be freed by this function), or do nothing if the latter | |
4474 | * parameter is NULL */ | |
962aa53f KW |
4475 | |
4476 | if (original_locale == NULL) { | |
4477 | return; | |
4478 | } | |
4479 | ||
3980cddb | 4480 | if (! bool_setlocale_r(category, original_locale)) { |
962aa53f KW |
4481 | Perl_croak(aTHX_ |
4482 | "panic: %s: %d: setlocale %s restore to %s failed, errno=%d\n", | |
4483 | __FILE__, __LINE__, | |
4484 | category_name(category), original_locale, errno); | |
4485 | } | |
4486 | ||
4487 | Safefree(original_locale); | |
4488 | } | |
4489 | ||
51332242 | 4490 | /* is_cur_LC_category_utf8 uses a small char buffer to avoid malloc/free */ |
fe1c1494 | 4491 | # define CUR_LC_BUFFER_SIZE 64 |
51332242 | 4492 | |
c1284011 KW |
4493 | bool |
4494 | Perl__is_cur_LC_category_utf8(pTHX_ int category) | |
7d74bb61 KW |
4495 | { |
4496 | /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE | |
4497 | * otherwise. 'category' may not be LC_ALL. If the platform doesn't have | |
119ee68b | 4498 | * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence |
609548d2 KW |
4499 | * could give the wrong result. The result will very likely be correct for |
4500 | * languages that have commonly used non-ASCII characters, but for notably | |
4501 | * English, it comes down to if the locale's name ends in something like | |
9de6fe47 KW |
4502 | * "UTF-8". It errs on the side of not being a UTF-8 locale. |
4503 | * | |
4504 | * If the platform is early C89, not containing mbtowc(), or we are | |
4505 | * compiled to not pay attention to LC_CTYPE, this employs heuristics. | |
4506 | * These work very well for non-Latin locales or those whose currency | |
4507 | * symbol isn't a '$' nor plain ASCII text. But without LC_CTYPE and at | |
4508 | * least MB_CUR_MAX, English locales with an ASCII currency symbol depend | |
4509 | * on the name containing UTF-8 or not. */ | |
7d74bb61 | 4510 | |
47280b20 | 4511 | /* Name of current locale corresponding to the input category */ |
8de4332b | 4512 | const char *save_input_locale = NULL; |
47280b20 KW |
4513 | |
4514 | bool is_utf8 = FALSE; /* The return value */ | |
7d74bb61 | 4515 | |
47280b20 KW |
4516 | /* The variables below are for the cache of previous lookups using this |
4517 | * function. The cache is a C string, described at the definition for | |
4518 | * 'C_and_POSIX_utf8ness'. | |
4519 | * | |
4520 | * The first part of the cache is fixed, for the C and POSIX locales. The | |
4521 | * varying part starts just after them. */ | |
4522 | char * utf8ness_cache = PL_locale_utf8ness + STRLENs(C_and_POSIX_utf8ness); | |
4523 | ||
4524 | Size_t utf8ness_cache_size; /* Size of the varying portion */ | |
4525 | Size_t input_name_len; /* Length in bytes of save_input_locale */ | |
4526 | Size_t input_name_len_with_overhead; /* plus extra chars used to store | |
4527 | the name in the cache */ | |
4528 | char * delimited; /* The name plus the delimiters used to store | |
4529 | it in the cache */ | |
51332242 | 4530 | char buffer[CUR_LC_BUFFER_SIZE]; /* small buffer */ |
47280b20 KW |
4531 | char * name_pos; /* position of 'delimited' in the cache, or 0 |
4532 | if not there */ | |
4533 | ||
4534 | ||
7d4bcc4a KW |
4535 | # ifdef LC_ALL |
4536 | ||
7d74bb61 | 4537 | assert(category != LC_ALL); |
7d4bcc4a KW |
4538 | |
4539 | # endif | |
7d74bb61 | 4540 | |
47280b20 | 4541 | /* Get the desired category's locale */ |
a683a66a | 4542 | save_input_locale = stdize_locale(savepv(querylocale_r(category))); |
7d74bb61 | 4543 | if (! save_input_locale) { |
47280b20 | 4544 | Perl_croak(aTHX_ |
d707d779 KW |
4545 | "panic: %s: %d: Could not find current %s locale, errno=%d\n", |
4546 | __FILE__, __LINE__, category_name(category), errno); | |
7d74bb61 | 4547 | } |
47280b20 | 4548 | |
47280b20 KW |
4549 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4550 | "Current locale for %s is %s\n", | |
4551 | category_name(category), save_input_locale)); | |
4552 | ||
4553 | input_name_len = strlen(save_input_locale); | |
4554 | ||
4555 | /* In our cache, each name is accompanied by two delimiters and a single | |
4556 | * utf8ness digit */ | |
4557 | input_name_len_with_overhead = input_name_len + 3; | |
4558 | ||
51332242 N |
4559 | if ( input_name_len_with_overhead <= CUR_LC_BUFFER_SIZE ) { |
4560 | /* we can use the buffer, avoid a malloc */ | |
4561 | delimited = buffer; | |
4562 | } else { /* need a malloc */ | |
4563 | /* Allocate and populate space for a copy of the name surrounded by the | |
4564 | * delimiters */ | |
4565 | Newx(delimited, input_name_len_with_overhead, char); | |
4566 | } | |
4567 | ||
47280b20 KW |
4568 | delimited[0] = UTF8NESS_SEP[0]; |
4569 | Copy(save_input_locale, delimited + 1, input_name_len, char); | |
4570 | delimited[input_name_len+1] = UTF8NESS_PREFIX[0]; | |
4571 | delimited[input_name_len+2] = '\0'; | |
4572 | ||
4573 | /* And see if that is in the cache */ | |
4574 | name_pos = instr(PL_locale_utf8ness, delimited); | |
4575 | if (name_pos) { | |
4576 | is_utf8 = *(name_pos + input_name_len_with_overhead - 1) - '0'; | |
4577 | ||
94d521c0 KW |
4578 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
4579 | "UTF8ness for locale %s=%d, \n", | |
4580 | save_input_locale, is_utf8)); | |
47280b20 KW |
4581 | |
4582 | /* And, if not already in that position, move it to the beginning of | |
4583 | * the non-constant portion of the list, since it is the most recently | |
4584 | * used. (We don't have to worry about overflow, since just moving | |
4585 | * existing names around) */ | |
4586 | if (name_pos > utf8ness_cache) { | |
4587 | Move(utf8ness_cache, | |
4588 | utf8ness_cache + input_name_len_with_overhead, | |
4589 | name_pos - utf8ness_cache, char); | |
4590 | Copy(delimited, | |
4591 | utf8ness_cache, | |
4592 | input_name_len_with_overhead - 1, char); | |
4593 | utf8ness_cache[input_name_len_with_overhead - 1] = is_utf8 + '0'; | |
4594 | } | |
4595 | ||
51332242 N |
4596 | /* free only when not using the buffer */ |
4597 | if ( delimited != buffer ) Safefree(delimited); | |
b07fffd1 | 4598 | Safefree(save_input_locale); |
47280b20 | 4599 | return is_utf8; |
7d74bb61 KW |
4600 | } |
4601 | ||
47280b20 KW |
4602 | /* Here we don't have stored the utf8ness for the input locale. We have to |
4603 | * calculate it */ | |
4604 | ||
94646a69 | 4605 | # if defined(USE_LOCALE_CTYPE) \ |
6201c220 | 4606 | && ( defined(HAS_NL_LANGINFO) \ |
94646a69 | 4607 | || (defined(HAS_MBTOWC) || defined(HAS_MBRTOWC))) |
7d74bb61 | 4608 | |
0dec74cd | 4609 | { |
962aa53f KW |
4610 | const char *original_ctype_locale |
4611 | = switch_category_locale_to_template(LC_CTYPE, | |
4612 | category, | |
4613 | save_input_locale); | |
69014004 | 4614 | |
7d74bb61 | 4615 | /* Here the current LC_CTYPE is set to the locale of the category whose |
0dec74cd | 4616 | * information is desired. This means that nl_langinfo() and mbtowc() |
1d958db2 | 4617 | * should give the correct results */ |
119ee68b | 4618 | |
0dec74cd KW |
4619 | # ifdef MB_CUR_MAX /* But we can potentially rule out UTF-8ness, avoiding |
4620 | calling the functions if we have this */ | |
4621 | ||
4622 | /* Standard UTF-8 needs at least 4 bytes to represent the maximum | |
4623 | * Unicode code point. */ | |
4624 | ||
de3a3072 KW |
4625 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s: %d: MB_CUR_MAX=%d\n", |
4626 | __FILE__, __LINE__, (int) MB_CUR_MAX)); | |
0dec74cd KW |
4627 | if ((unsigned) MB_CUR_MAX < STRLENs(MAX_UNICODE_UTF8)) { |
4628 | is_utf8 = FALSE; | |
b5b2847c KW |
4629 | restore_switched_locale(LC_CTYPE, original_ctype_locale); |
4630 | goto finish_and_return; | |
0dec74cd KW |
4631 | } |
4632 | ||
4633 | # endif | |
6201c220 | 4634 | # if defined(HAS_NL_LANGINFO) |
7d4bcc4a | 4635 | |
0dec74cd KW |
4636 | { /* The task is easiest if the platform has this POSIX 2001 function. |
4637 | Except on some platforms it can wrongly return "", so have to have | |
4638 | a fallback. And it can return that it's UTF-8, even if there are | |
4639 | variances from that. For example, Turkish locales may use the | |
4640 | alternate dotted I rules, and sometimes it appears to be a | |
4641 | defective locale definition. XXX We should probably check for | |
9de6fe47 KW |
4642 | these in the Latin1 range and warn (but on glibc, requires |
4643 | iswalnum() etc. due to their not handling 80-FF correctly */ | |
4e6826bf | 4644 | const char *codeset = my_nl_langinfo(CODESET, FALSE); |
c70a3e68 | 4645 | /* FALSE => already in dest locale */ |
119ee68b | 4646 | |
af84e886 | 4647 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
c70a3e68 KW |
4648 | "\tnllanginfo returned CODESET '%s'\n", codeset)); |
4649 | ||
4650 | if (codeset && strNE(codeset, "")) { | |
1d958db2 | 4651 | |
1d075173 KW |
4652 | /* If the implementation of foldEQ() somehow were |
4653 | * to change to not go byte-by-byte, this could | |
4654 | * read past end of string, as only one length is | |
4655 | * checked. But currently, a premature NUL will | |
4656 | * compare false, and it will stop there */ | |
c3c9077b KW |
4657 | is_utf8 = cBOOL( foldEQ(codeset, "UTF-8", STRLENs("UTF-8")) |
4658 | || foldEQ(codeset, "UTF8", STRLENs("UTF8"))); | |
1d958db2 | 4659 | |
69014004 KW |
4660 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4661 | "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n", | |
4662 | codeset, is_utf8)); | |
b5b2847c KW |
4663 | restore_switched_locale(LC_CTYPE, original_ctype_locale); |
4664 | goto finish_and_return; | |
1d958db2 | 4665 | } |
119ee68b KW |
4666 | } |
4667 | ||
7d4bcc4a | 4668 | # endif |
94646a69 | 4669 | # if defined(HAS_MBTOWC) || defined(HAS_MBRTOWC) |
0dec74cd KW |
4670 | /* We can see if this is a UTF-8-like locale if have mbtowc(). It was a |
4671 | * late adder to C89, so very likely to have it. However, testing has | |
4672 | * shown that, like nl_langinfo() above, there are locales that are not | |
4673 | * strictly UTF-8 that this will return that they are */ | |
69014004 | 4674 | |
0dec74cd | 4675 | { |
119ee68b | 4676 | wchar_t wc; |
51fc4b19 | 4677 | int len; |
48015184 | 4678 | dSAVEDERRNO; |
51fc4b19 | 4679 | |
8751a11f | 4680 | # if defined(HAS_MBRTOWC) && defined(USE_LOCALE_THREADS) |
94646a69 KW |
4681 | |
4682 | mbstate_t ps; | |
4683 | ||
4684 | # endif | |
4685 | ||
4686 | /* mbrtowc() and mbtowc() convert a byte string to a wide | |
4687 | * character. Feed a byte string to one of them and check that the | |
4688 | * result is the expected Unicode code point */ | |
4689 | ||
8751a11f | 4690 | # if defined(HAS_MBRTOWC) && defined(USE_LOCALE_THREADS) |
94646a69 KW |
4691 | /* Prefer this function if available, as it's reentrant */ |
4692 | ||
a16bc7c9 | 4693 | memzero(&ps, sizeof(ps));; |
94646a69 KW |
4694 | PERL_UNUSED_RESULT(mbrtowc(&wc, NULL, 0, &ps)); /* Reset any shift |
4695 | state */ | |
48015184 | 4696 | SETERRNO(0, 0); |
94646a69 | 4697 | len = mbrtowc(&wc, STR_WITH_LEN(REPLACEMENT_CHARACTER_UTF8), &ps); |
48015184 | 4698 | SAVE_ERRNO; |
94646a69 KW |
4699 | |
4700 | # else | |
0dec74cd | 4701 | |
7953f73f | 4702 | MBTOWC_LOCK; |
856b881c | 4703 | PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */ |
48015184 | 4704 | SETERRNO(0, 0); |
b1d4925c | 4705 | len = mbtowc(&wc, STR_WITH_LEN(REPLACEMENT_CHARACTER_UTF8)); |
48015184 | 4706 | SAVE_ERRNO; |
7953f73f | 4707 | MBTOWC_UNLOCK; |
51fc4b19 | 4708 | |
94646a69 KW |
4709 | # endif |
4710 | ||
48015184 | 4711 | RESTORE_ERRNO; |
af84e886 | 4712 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
0dec74cd | 4713 | "\treturn from mbtowc; len=%d; code_point=%x; errno=%d\n", |
48015184 | 4714 | len, (unsigned int) wc, GET_ERRNO)); |
b1d4925c | 4715 | |
0dec74cd KW |
4716 | is_utf8 = cBOOL( len == STRLENs(REPLACEMENT_CHARACTER_UTF8) |
4717 | && wc == (wchar_t) UNICODE_REPLACEMENT); | |
119ee68b | 4718 | } |
7d4bcc4a | 4719 | |
17dd77cd FP |
4720 | # endif |
4721 | ||
962aa53f | 4722 | restore_switched_locale(LC_CTYPE, original_ctype_locale); |
47280b20 | 4723 | goto finish_and_return; |
7d74bb61 | 4724 | } |
119ee68b | 4725 | |
0dec74cd | 4726 | # else |
7d74bb61 | 4727 | |
0dec74cd KW |
4728 | /* Here, we must have a C89 compiler that doesn't have mbtowc(). Next |
4729 | * try looking at the currency symbol to see if it disambiguates | |
4730 | * things. Often that will be in the native script, and if the symbol | |
4731 | * isn't in UTF-8, we know that the locale isn't. If it is non-ASCII | |
4732 | * UTF-8, we infer that the locale is too, as the odds of a non-UTF8 | |
4733 | * string being valid UTF-8 are quite small */ | |
fa9b773e | 4734 | |
660ee27b KW |
4735 | # ifdef USE_LOCALE_MONETARY |
4736 | ||
4737 | /* If have LC_MONETARY, we can look at the currency symbol. Often that | |
4738 | * will be in the native script. We do this one first because there is | |
4739 | * just one string to examine, so potentially avoids work */ | |
7d4bcc4a | 4740 | |
9db16864 KW |
4741 | { |
4742 | const char *original_monetary_locale | |
660ee27b KW |
4743 | = switch_category_locale_to_template(LC_MONETARY, |
4744 | category, | |
4745 | save_input_locale); | |
9db16864 | 4746 | bool only_ascii = FALSE; |
660ee27b | 4747 | const U8 * currency_string |
4e6826bf | 4748 | = (const U8 *) my_nl_langinfo(CRNCYSTR, FALSE); |
660ee27b KW |
4749 | /* 2nd param not relevant for this item */ |
4750 | const U8 * first_variant; | |
fa9b773e | 4751 | |
660ee27b KW |
4752 | assert( *currency_string == '-' |
4753 | || *currency_string == '+' | |
4754 | || *currency_string == '.'); | |
97f4de96 | 4755 | |
660ee27b | 4756 | currency_string++; |
fa9b773e | 4757 | |
660ee27b | 4758 | if (is_utf8_invariant_string_loc(currency_string, 0, &first_variant)) |
9db16864 | 4759 | { |
fe1c1494 KW |
4760 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4761 | "Couldn't get currency symbol for %s, or contains" | |
4762 | " only ASCII; can't use for determining if UTF-8" | |
4763 | " locale\n", save_input_locale)); | |
9db16864 KW |
4764 | only_ascii = TRUE; |
4765 | } | |
4766 | else { | |
660ee27b | 4767 | is_utf8 = is_strict_utf8_string(first_variant, 0); |
9db16864 | 4768 | } |
fa9b773e | 4769 | |
9db16864 | 4770 | restore_switched_locale(LC_MONETARY, original_monetary_locale); |
fa9b773e | 4771 | |
9db16864 | 4772 | if (! only_ascii) { |
fa9b773e | 4773 | |
9db16864 KW |
4774 | /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; |
4775 | * otherwise assume the locale is UTF-8 if and only if the symbol | |
4776 | * is non-ascii UTF-8. */ | |
fe1c1494 KW |
4777 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
4778 | "\t?Currency symbol for %s is UTF-8=%d\n", | |
9db16864 KW |
4779 | save_input_locale, is_utf8)); |
4780 | goto finish_and_return; | |
4781 | } | |
13542a67 | 4782 | } |
fa9b773e | 4783 | |
660ee27b | 4784 | # endif /* USE_LOCALE_MONETARY */ |
7d4bcc4a | 4785 | # if defined(HAS_STRFTIME) && defined(USE_LOCALE_TIME) |
15f7e74e | 4786 | |
9db16864 KW |
4787 | /* Still haven't found a non-ASCII string to disambiguate UTF-8 or not. Try |
4788 | * the names of the months and weekdays, timezone, and am/pm indicator */ | |
4789 | { | |
4790 | const char *original_time_locale | |
4791 | = switch_category_locale_to_template(LC_TIME, | |
4792 | category, | |
4793 | save_input_locale); | |
4794 | int hour = 10; | |
4795 | bool is_dst = FALSE; | |
4796 | int dom = 1; | |
4797 | int month = 0; | |
4798 | int i; | |
4799 | char * formatted_time; | |
4800 | ||
4801 | /* Here the current LC_TIME is set to the locale of the category | |
fe1c1494 KW |
4802 | * whose information is desired. Look at all the days of the week |
4803 | * and month names, and the timezone and am/pm indicator for UTF-8 | |
4804 | * variant characters. The first such a one found will tell us if | |
4805 | * the locale is UTF-8 or not */ | |
9db16864 KW |
4806 | |
4807 | for (i = 0; i < 7 + 12; i++) { /* 7 days; 12 months */ | |
4808 | formatted_time = my_strftime("%A %B %Z %p", | |
4809 | 0, 0, hour, dom, month, 2012 - 1900, 0, 0, is_dst); | |
4810 | if ( ! formatted_time | |
4811 | || is_utf8_invariant_string((U8 *) formatted_time, 0)) | |
4812 | { | |
15f7e74e | 4813 | |
fe1c1494 KW |
4814 | /* Here, we didn't find a non-ASCII. Try the next time |
4815 | * through with the complemented dst and am/pm, and try | |
4816 | * with the next weekday. After we have gotten all | |
4817 | * weekdays, try the next month */ | |
9db16864 KW |
4818 | is_dst = ! is_dst; |
4819 | hour = (hour + 12) % 24; | |
4820 | dom++; | |
4821 | if (i > 6) { | |
4822 | month++; | |
4823 | } | |
4824 | continue; | |
15f7e74e | 4825 | } |
9db16864 KW |
4826 | |
4827 | /* Here, we have a non-ASCII. Return TRUE is it is valid UTF8; | |
4828 | * false otherwise. But first, restore LC_TIME to its original | |
4829 | * locale if we changed it */ | |
4830 | restore_switched_locale(LC_TIME, original_time_locale); | |
4831 | ||
fe1c1494 KW |
4832 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
4833 | "\t?time-related strings for %s are UTF-8=%d\n", | |
9db16864 KW |
4834 | save_input_locale, |
4835 | is_utf8_string((U8 *) formatted_time, 0))); | |
4836 | is_utf8 = is_utf8_string((U8 *) formatted_time, 0); | |
4837 | goto finish_and_return; | |
15f7e74e KW |
4838 | } |
4839 | ||
9db16864 KW |
4840 | /* Falling off the end of the loop indicates all the names were just |
4841 | * ASCII. Go on to the next test. If we changed it, restore LC_TIME | |
4842 | * to its original locale */ | |
962aa53f | 4843 | restore_switched_locale(LC_TIME, original_time_locale); |
fe1c1494 KW |
4844 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
4845 | "All time-related words for %s contain only ASCII;" | |
4846 | " can't use for determining if UTF-8 locale\n", | |
4847 | save_input_locale)); | |
15f7e74e KW |
4848 | } |
4849 | ||
7d4bcc4a | 4850 | # endif |
15f7e74e | 4851 | |
7d4bcc4a | 4852 | # if 0 && defined(USE_LOCALE_MESSAGES) && defined(HAS_SYS_ERRLIST) |
855aeb93 | 4853 | |
fe1c1494 KW |
4854 | /* This code is ifdefd out because it was found to not be necessary in |
4855 | * testing on our dromedary test machine, which has over 700 locales. | |
4856 | * There, this added no value to looking at the currency symbol and the | |
4857 | * time strings. I left it in so as to avoid rewriting it if real-world | |
4858 | * experience indicates that dromedary is an outlier. Essentially, instead | |
4859 | * of returning abpve if we haven't found illegal utf8, we continue on and | |
4860 | * examine all the strerror() messages on the platform for utf8ness. If | |
4861 | * all are ASCII, we still don't know the answer; but otherwise we have a | |
4862 | * pretty good indication of the utf8ness. The reason this doesn't help | |
4863 | * much is that the messages may not have been translated into the locale. | |
4864 | * The currency symbol and time strings are much more likely to have been | |
4865 | * translated. */ | |
9db16864 KW |
4866 | { |
4867 | int e; | |
4868 | bool non_ascii = FALSE; | |
4869 | const char *original_messages_locale | |
4870 | = switch_category_locale_to_template(LC_MESSAGES, | |
4871 | category, | |
4872 | save_input_locale); | |
4873 | const char * errmsg = NULL; | |
4874 | ||
4875 | /* Here the current LC_MESSAGES is set to the locale of the category | |
4876 | * whose information is desired. Look through all the messages. We | |
4877 | * can't use Strerror() here because it may expand to code that | |
4878 | * segfaults in miniperl */ | |
4879 | ||
4880 | for (e = 0; e <= sys_nerr; e++) { | |
4881 | errno = 0; | |
4882 | errmsg = sys_errlist[e]; | |
4883 | if (errno || !errmsg) { | |
4884 | break; | |
4885 | } | |
4886 | errmsg = savepv(errmsg); | |
4887 | if (! is_utf8_invariant_string((U8 *) errmsg, 0)) { | |
4888 | non_ascii = TRUE; | |
4889 | is_utf8 = is_utf8_string((U8 *) errmsg, 0); | |
4890 | break; | |
4891 | } | |
855aeb93 | 4892 | } |
9db16864 | 4893 | Safefree(errmsg); |
855aeb93 | 4894 | |
9db16864 | 4895 | restore_switched_locale(LC_MESSAGES, original_messages_locale); |
855aeb93 | 4896 | |
9db16864 | 4897 | if (non_ascii) { |
5857e934 | 4898 | |
fe1c1494 KW |
4899 | /* Any non-UTF-8 message means not a UTF-8 locale; if all are |
4900 | * valid, any non-ascii means it is one; otherwise we assume it | |
4901 | * isn't */ | |
4902 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, | |
4903 | "\t?error messages for %s are UTF-8=%d\n", | |
9db16864 KW |
4904 | save_input_locale, |
4905 | is_utf8)); | |
4906 | goto finish_and_return; | |
4907 | } | |
855aeb93 | 4908 | |
fe1c1494 KW |
4909 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4910 | "All error messages for %s contain only ASCII;" | |
4911 | " can't use for determining if UTF-8 locale\n", | |
4912 | save_input_locale)); | |
9db16864 | 4913 | } |
855aeb93 | 4914 | |
7d4bcc4a | 4915 | # endif |
0dec74cd | 4916 | # ifndef EBCDIC /* On os390, even if the name ends with "UTF-8', it isn't a |
92c0a900 | 4917 | UTF-8 locale */ |
7d4bcc4a | 4918 | |
97f4de96 KW |
4919 | /* As a last resort, look at the locale name to see if it matches |
4920 | * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the | |
4921 | * return of setlocale(), is actually defined to be opaque, so we can't | |
4922 | * really rely on the absence of various substrings in the name to indicate | |
4923 | * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to | |
4924 | * be a UTF-8 locale. Similarly for the other common names */ | |
4925 | ||
0dec74cd | 4926 | { |
c36d8df8 | 4927 | const Size_t final_pos = strlen(save_input_locale) - 1; |
0dec74cd | 4928 | |
c36d8df8 KW |
4929 | if (final_pos >= 3) { |
4930 | const char *name = save_input_locale; | |
97f4de96 | 4931 | |
c36d8df8 KW |
4932 | /* Find next 'U' or 'u' and look from there */ |
4933 | while ((name += strcspn(name, "Uu") + 1) | |
4934 | <= save_input_locale + final_pos - 2) | |
97f4de96 | 4935 | { |
c36d8df8 KW |
4936 | if ( isALPHA_FOLD_NE(*name, 't') |
4937 | || isALPHA_FOLD_NE(*(name + 1), 'f')) | |
4938 | { | |
4939 | continue; | |
4940 | } | |
4941 | name += 2; | |
4942 | if (*(name) == '-') { | |
4943 | if ((name > save_input_locale + final_pos - 1)) { | |
4944 | break; | |
4945 | } | |
4946 | name++; | |
4947 | } | |
4948 | if (*(name) == '8') { | |
4949 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
4950 | "Locale %s ends with UTF-8 in name\n", | |
4951 | save_input_locale)); | |
4952 | is_utf8 = TRUE; | |
4953 | goto finish_and_return; | |
97f4de96 | 4954 | } |
97f4de96 | 4955 | } |
c36d8df8 KW |
4956 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4957 | "Locale %s doesn't end with UTF-8 in name\n", | |
4958 | save_input_locale)); | |
97f4de96 | 4959 | } |
97f4de96 | 4960 | |
4d8d465a | 4961 | # ifdef WIN32 |
7d4bcc4a | 4962 | |
c36d8df8 KW |
4963 | /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */ |
4964 | if (memENDs(save_input_locale, final_pos, "65001")) { | |
4965 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
8a0832a1 | 4966 | "Locale %s ends with 65001 in name, is UTF-8 locale\n", |
97f4de96 | 4967 | save_input_locale)); |
c36d8df8 KW |
4968 | is_utf8 = TRUE; |
4969 | goto finish_and_return; | |
4970 | } | |
7d4bcc4a | 4971 | |
4d8d465a | 4972 | # endif |
17dd77cd | 4973 | } |
4d8d465a | 4974 | # endif |
97f4de96 KW |
4975 | |
4976 | /* Other common encodings are the ISO 8859 series, which aren't UTF-8. But | |
4977 | * since we are about to return FALSE anyway, there is no point in doing | |
4978 | * this extra work */ | |
7d4bcc4a | 4979 | |
0dec74cd | 4980 | # if 0 |
97f4de96 KW |
4981 | if (instr(save_input_locale, "8859")) { |
4982 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
4983 | "Locale %s has 8859 in name, not UTF-8 locale\n", | |
4984 | save_input_locale)); | |
47280b20 KW |
4985 | is_utf8 = FALSE; |
4986 | goto finish_and_return; | |
97f4de96 | 4987 | } |
0dec74cd | 4988 | # endif |
97f4de96 | 4989 | |
69014004 KW |
4990 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
4991 | "Assuming locale %s is not a UTF-8 locale\n", | |
4992 | save_input_locale)); | |
47280b20 KW |
4993 | is_utf8 = FALSE; |
4994 | ||
0dec74cd KW |
4995 | # endif /* the code that is compiled when no modern LC_CTYPE */ |
4996 | ||
47280b20 KW |
4997 | finish_and_return: |
4998 | ||
4999 | /* Cache this result so we don't have to go through all this next time. */ | |
5000 | utf8ness_cache_size = sizeof(PL_locale_utf8ness) | |
5001 | - (utf8ness_cache - PL_locale_utf8ness); | |
5002 | ||
5003 | /* But we can't save it if it is too large for the total space available */ | |
5004 | if (LIKELY(input_name_len_with_overhead < utf8ness_cache_size)) { | |
5005 | Size_t utf8ness_cache_len = strlen(utf8ness_cache); | |
5006 | ||
5007 | /* Here it can fit, but we may need to clear out the oldest cached | |
5008 | * result(s) to do so. Check */ | |
5009 | if (utf8ness_cache_len + input_name_len_with_overhead | |
5010 | >= utf8ness_cache_size) | |
5011 | { | |
5012 | /* Here we have to clear something out to make room for this. | |
5013 | * Start looking at the rightmost place where it could fit and find | |
5014 | * the beginning of the entry that extends past that. */ | |
5015 | char * cutoff = (char *) my_memrchr(utf8ness_cache, | |
5016 | UTF8NESS_SEP[0], | |
5017 | utf8ness_cache_size | |
5018 | - input_name_len_with_overhead); | |
5019 | ||
5020 | assert(cutoff); | |
5021 | assert(cutoff >= utf8ness_cache); | |
5022 | ||
5023 | /* This and all subsequent entries must be removed */ | |
5024 | *cutoff = '\0'; | |
5025 | utf8ness_cache_len = strlen(utf8ness_cache); | |
5026 | } | |
5027 | ||
5028 | /* Make space for the new entry */ | |
5029 | Move(utf8ness_cache, | |
5030 | utf8ness_cache + input_name_len_with_overhead, | |
5031 | utf8ness_cache_len + 1 /* Incl. trailing NUL */, char); | |
5032 | ||
5033 | /* And insert it */ | |
5034 | Copy(delimited, utf8ness_cache, input_name_len_with_overhead - 1, char); | |
5035 | utf8ness_cache[input_name_len_with_overhead - 1] = is_utf8 + '0'; | |
5036 | ||
72299e00 | 5037 | if ((PL_locale_utf8ness[strlen(PL_locale_utf8ness)-1] & ~1) != '0') { |
47280b20 | 5038 | Perl_croak(aTHX_ |
7fcf9272 KW |
5039 | "panic: %s: %d: Corrupt utf8ness_cache=%s\nlen=%zu," |
5040 | " inserted_name=%s, its_len=%zu\n", | |
47280b20 KW |
5041 | __FILE__, __LINE__, |
5042 | PL_locale_utf8ness, strlen(PL_locale_utf8ness), | |
5043 | delimited, input_name_len_with_overhead); | |
5044 | } | |
5045 | } | |
5046 | ||
5047 | # ifdef DEBUGGING | |
5048 | ||
de3a3072 KW |
5049 | if (DEBUG_Lv_TEST) { |
5050 | const char * s = PL_locale_utf8ness; | |
5051 | ||
5052 | /* Audit the structure */ | |
5053 | while (s < PL_locale_utf8ness + strlen(PL_locale_utf8ness)) { | |
5054 | const char *e; | |
5055 | ||
5056 | if (*s != UTF8NESS_SEP[0]) { | |
5057 | Perl_croak(aTHX_ | |
5058 | "panic: %s: %d: Corrupt utf8ness_cache: missing" | |
5059 | " separator %.*s<-- HERE %s\n", | |
5060 | __FILE__, __LINE__, | |
fe301c93 | 5061 | (int) (s - PL_locale_utf8ness), PL_locale_utf8ness, |
de3a3072 KW |
5062 | s); |
5063 | } | |
5064 | s++; | |
5065 | e = strchr(s, UTF8NESS_PREFIX[0]); | |
5066 | if (! e) { | |
01b91050 | 5067 | e = PL_locale_utf8ness + strlen(PL_locale_utf8ness); |
de3a3072 KW |
5068 | Perl_croak(aTHX_ |
5069 | "panic: %s: %d: Corrupt utf8ness_cache: missing" | |
5070 | " separator %.*s<-- HERE %s\n", | |
5071 | __FILE__, __LINE__, | |
fe301c93 | 5072 | (int) (e - PL_locale_utf8ness), PL_locale_utf8ness, |
de3a3072 KW |
5073 | e); |
5074 | } | |
5075 | e++; | |
5076 | if (*e != '0' && *e != '1') { | |
5077 | Perl_croak(aTHX_ | |
5078 | "panic: %s: %d: Corrupt utf8ness_cache: utf8ness" | |
5079 | " must be [01] %.*s<-- HERE %s\n", | |
5080 | __FILE__, __LINE__, | |
fe301c93 KW |
5081 | (int) (e + 1 - PL_locale_utf8ness), |
5082 | PL_locale_utf8ness, e + 1); | |
de3a3072 KW |
5083 | } |
5084 | if (ninstr(PL_locale_utf8ness, s, s-1, e)) { | |
5085 | Perl_croak(aTHX_ | |
5086 | "panic: %s: %d: Corrupt utf8ness_cache: entry" | |
5087 | " has duplicate %.*s<-- HERE %s\n", | |
5088 | __FILE__, __LINE__, | |
fe301c93 | 5089 | (int) (e - PL_locale_utf8ness), PL_locale_utf8ness, |
de3a3072 KW |
5090 | e); |
5091 | } | |
5092 | s = e + 1; | |
5093 | } | |
5094 | } | |
5095 | ||
94d521c0 | 5096 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
47280b20 | 5097 | "PL_locale_utf8ness is now %s; returning %d\n", |
94d521c0 | 5098 | PL_locale_utf8ness, is_utf8)); |
47280b20 KW |
5099 | |
5100 | # endif | |
5101 | ||
51332242 N |
5102 | /* free only when not using the buffer */ |
5103 | if ( delimited != buffer ) Safefree(delimited); | |
fa9b773e | 5104 | Safefree(save_input_locale); |
47280b20 | 5105 | return is_utf8; |
7d74bb61 KW |
5106 | } |
5107 | ||
8ef6e574 | 5108 | #endif |
7d74bb61 | 5109 | |
d6ded950 KW |
5110 | bool |
5111 | Perl__is_in_locale_category(pTHX_ const bool compiling, const int category) | |
5112 | { | |
5113 | /* Internal function which returns if we are in the scope of a pragma that | |
5114 | * enables the locale category 'category'. 'compiling' should indicate if | |
5115 | * this is during the compilation phase (TRUE) or not (FALSE). */ | |
5116 | ||
5117 | const COP * const cop = (compiling) ? &PL_compiling : PL_curcop; | |
5118 | ||
363d7d4a JK |
5119 | SV *these_categories = cop_hints_fetch_pvs(cop, "locale", 0); |
5120 | if (! these_categories || these_categories == &PL_sv_placeholder) { | |
d6ded950 KW |
5121 | return FALSE; |
5122 | } | |
5123 | ||
5124 | /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get | |
5125 | * a valid unsigned */ | |
5126 | assert(category >= -1); | |
363d7d4a | 5127 | return cBOOL(SvUV(these_categories) & (1U << (category + 1))); |
d6ded950 KW |
5128 | } |
5129 | ||
2c6ee1a7 | 5130 | char * |
6ebbc862 KW |
5131 | Perl_my_strerror(pTHX_ const int errnum) |
5132 | { | |
5133 | /* Returns a mortalized copy of the text of the error message associated | |
5134 | * with 'errnum'. It uses the current locale's text unless the platform | |
5135 | * doesn't have the LC_MESSAGES category or we are not being called from | |
5136 | * within the scope of 'use locale'. In the former case, it uses whatever | |
5137 | * strerror returns; in the latter case it uses the text from the C locale. | |
5138 | * | |
5139 | * The function just calls strerror(), but temporarily switches, if needed, | |
5140 | * to the C locale */ | |
5141 | ||
5142 | char *errstr; | |
5143 | ||
52770946 | 5144 | #ifndef USE_LOCALE_MESSAGES |
6ebbc862 | 5145 | |
52770946 KW |
5146 | /* If platform doesn't have messages category, we don't do any switching to |
5147 | * the C locale; we just use whatever strerror() returns */ | |
5148 | ||
5149 | errstr = savepv(Strerror(errnum)); | |
5150 | ||
5151 | #else /* Has locale messages */ | |
5152 | ||
5153 | const bool within_locale_scope = IN_LC(LC_MESSAGES); | |
2c6ee1a7 | 5154 | |
8751a11f | 5155 | # ifndef USE_LOCALE_THREADS |
7aaa36b1 | 5156 | |
39e69e77 KW |
5157 | /* This function is trivial without threads. */ |
5158 | if (within_locale_scope) { | |
f9ea7fff | 5159 | errstr = savepv(Strerror(errnum)); |
39e69e77 KW |
5160 | } |
5161 | else { | |
a683a66a | 5162 | const char * save_locale = savepv(querylocale_c(LC_MESSAGES)); |
39e69e77 | 5163 | |
3980cddb | 5164 | void_setlocale_c(LC_MESSAGES, "C"); |
f9ea7fff | 5165 | errstr = savepv(Strerror(errnum)); |
3980cddb | 5166 | void_setlocale_c(LC_MESSAGES, save_locale); |
b0bde642 | 5167 | Safefree(save_locale); |
39e69e77 KW |
5168 | } |
5169 | ||
fe1c1494 | 5170 | # elif defined(USE_POSIX_2008_LOCALE) && defined(HAS_STRERROR_L) |
39e69e77 KW |
5171 | |
5172 | /* This function is also trivial if we don't have to worry about thread | |
5173 | * safety and have strerror_l(), as it handles the switch of locales so we | |
5174 | * don't have to deal with that. We don't have to worry about thread | |
5175 | * safety if strerror_r() is also available. Both it and strerror_l() are | |
5176 | * thread-safe. Plain strerror() isn't thread safe. But on threaded | |
5177 | * builds when strerror_r() is available, the apparent call to strerror() | |
5178 | * below is actually a macro that behind-the-scenes calls strerror_r(). */ | |
43cb6651 | 5179 | |
39e69e77 | 5180 | # ifdef HAS_STRERROR_R |
7aaa36b1 KW |
5181 | |
5182 | if (within_locale_scope) { | |
f9ea7fff | 5183 | errstr = savepv(Strerror(errnum)); |
7aaa36b1 KW |
5184 | } |
5185 | else { | |
4eb27fc5 | 5186 | errstr = savepv(strerror_l(errnum, PL_C_locale_obj)); |
7aaa36b1 KW |
5187 | } |
5188 | ||
43cb6651 KW |
5189 | # else |
5190 | ||
5191 | /* Here we have strerror_l(), but not strerror_r() and we are on a | |
5192 | * threaded-build. We use strerror_l() for everything, constructing a | |
5193 | * locale to pass to it if necessary */ | |
5194 | ||
5195 | bool do_free = FALSE; | |
5196 | locale_t locale_to_use; | |
5197 | ||
5198 | if (within_locale_scope) { | |
5199 | locale_to_use = uselocale((locale_t) 0); | |
5200 | if (locale_to_use == LC_GLOBAL_LOCALE) { | |
5201 | locale_to_use = duplocale(LC_GLOBAL_LOCALE); | |
5202 | do_free = TRUE; | |
5203 | } | |
5204 | } | |
5205 | else { /* Use C locale if not within 'use locale' scope */ | |
5206 | locale_to_use = PL_C_locale_obj; | |
5207 | } | |
5208 | ||
5209 | errstr = savepv(strerror_l(errnum, locale_to_use)); | |
5210 | ||
5211 | if (do_free) { | |
5212 | freelocale(locale_to_use); | |
5213 | } | |
5214 | ||
5215 | # endif | |
5216 | # else /* Doesn't have strerror_l() */ | |
7aaa36b1 | 5217 | |
8de4332b | 5218 | const char * save_locale = NULL; |
c9dda6da | 5219 | bool locale_is_C = FALSE; |
2c6ee1a7 | 5220 | |
9de6fe47 | 5221 | /* We have a critical section to prevent another thread from executing this |
e9bc6d6b | 5222 | * same code at the same time. (On thread-safe perls, the LOCK is a |
9de6fe47 KW |
5223 | * no-op.) Since this is the only place in core that changes LC_MESSAGES |
5224 | * (unless the user has called setlocale(), this works to prevent races. */ | |
7953f73f | 5225 | SETLOCALE_LOCK; |
6ebbc862 | 5226 | |
9c8a6dc2 KW |
5227 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
5228 | "my_strerror called with errnum %d\n", errnum)); | |
6ebbc862 | 5229 | if (! within_locale_scope) { |
a683a66a | 5230 | save_locale = querylocale_c(LC_MESSAGES); |
c9dda6da | 5231 | if (! save_locale) { |
d523eeeb | 5232 | SETLOCALE_UNLOCK; |
d707d779 KW |
5233 | Perl_croak(aTHX_ |
5234 | "panic: %s: %d: Could not find current LC_MESSAGES locale," | |
5235 | " errno=%d\n", __FILE__, __LINE__, errno); | |
c9dda6da KW |
5236 | } |
5237 | else { | |
5238 | locale_is_C = isNAME_C_OR_POSIX(save_locale); | |
2c6ee1a7 | 5239 | |
c9dda6da KW |
5240 | /* Switch to the C locale if not already in it */ |
5241 | if (! locale_is_C) { | |
2c6ee1a7 | 5242 | |
c9dda6da KW |
5243 | /* The setlocale() just below likely will zap 'save_locale', so |
5244 | * create a copy. */ | |
5245 | save_locale = savepv(save_locale); | |
3980cddb | 5246 | if (! bool_setlocale_c(LC_MESSAGES, "C")) { |
a83cff23 KW |
5247 | |
5248 | /* If, for some reason, the locale change failed, we | |
5249 | * soldier on as best as possible under the circumstances, | |
5250 | * using the current locale, and clear save_locale, so we | |
5251 | * don't try to change back. On z/0S, all setlocale() | |
5252 | * calls fail after you've created a thread. This is their | |
5253 | * way of making sure the entire process is always a single | |
5254 | * locale. This means that 'use locale' is always in place | |
5255 | * for messages under these circumstances. */ | |
5256 | Safefree(save_locale); | |
5257 | save_locale = NULL; | |
5258 | } | |
c9dda6da | 5259 | } |
6ebbc862 | 5260 | } |
6ebbc862 | 5261 | } /* end of ! within_locale_scope */ |
9c8a6dc2 KW |
5262 | else { |
5263 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s: %d: WITHIN locale scope\n", | |
5264 | __FILE__, __LINE__)); | |
5265 | } | |
a0b53297 | 5266 | |
9c8a6dc2 KW |
5267 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
5268 | "Any locale change has been done; about to call Strerror\n")); | |
52770946 | 5269 | errstr = savepv(Strerror(errnum)); |
6ebbc862 KW |
5270 | |
5271 | if (! within_locale_scope) { | |
c9dda6da | 5272 | if (save_locale && ! locale_is_C) { |
3980cddb | 5273 | if (! bool_setlocale_c(LC_MESSAGES, save_locale)) { |
d523eeeb | 5274 | SETLOCALE_UNLOCK; |
d707d779 | 5275 | Perl_croak(aTHX_ |
cfaae47f KW |
5276 | "panic: %s: %d: setlocale restore to '%s' failed, errno=%d\n", |
5277 | __FILE__, __LINE__, save_locale, errno); | |
c9dda6da | 5278 | } |
6ebbc862 KW |
5279 | Safefree(save_locale); |
5280 | } | |
5281 | } | |
5282 | ||
7953f73f | 5283 | SETLOCALE_UNLOCK; |
6ebbc862 | 5284 | |
7aaa36b1 | 5285 | # endif /* End of doesn't have strerror_l */ |
6affbbf0 | 5286 | |
94d521c0 KW |
5287 | DEBUG_Lv((PerlIO_printf(Perl_debug_log, |
5288 | "Strerror returned; saving a copy: '"), | |
5289 | print_bytes_for_locale(errstr, errstr + strlen(errstr), 0), | |
5290 | PerlIO_printf(Perl_debug_log, "'\n"))); | |
6affbbf0 | 5291 | |
d36adde0 | 5292 | #endif /* End of does have locale messages */ |
2c6ee1a7 | 5293 | |
52770946 | 5294 | SAVEFREEPV(errstr); |
6ebbc862 | 5295 | return errstr; |
2c6ee1a7 KW |
5296 | } |
5297 | ||
66610fdd | 5298 | /* |
747c467a | 5299 | |
58e641fb KW |
5300 | =for apidoc switch_to_global_locale |
5301 | ||
75920755 | 5302 | On systems without locale support, or on typical single-threaded builds, or on |
58e641fb KW |
5303 | platforms that do not support per-thread locale operations, this function does |
5304 | nothing. On such systems that do have locale support, only a locale global to | |
5305 | the whole program is available. | |
5306 | ||
5307 | On multi-threaded builds on systems that do have per-thread locale operations, | |
5308 | this function converts the thread it is running in to use the global locale. | |
5309 | This is for code that has not yet or cannot be updated to handle multi-threaded | |
5310 | locale operation. As long as only a single thread is so-converted, everything | |
5311 | works fine, as all the other threads continue to ignore the global one, so only | |
5312 | this thread looks at it. | |
5313 | ||
69c5e0db KW |
5314 | However, on Windows systems this isn't quite true prior to Visual Studio 15, |
5315 | at which point Microsoft fixed a bug. A race can occur if you use the | |
5316 | following operations on earlier Windows platforms: | |
5317 | ||
5318 | =over | |
5319 | ||
5320 | =item L<POSIX::localeconv|POSIX/localeconv> | |
5321 | ||
5322 | =item L<I18N::Langinfo>, items C<CRNCYSTR> and C<THOUSEP> | |
5323 | ||
5324 | =item L<perlapi/Perl_langinfo>, items C<CRNCYSTR> and C<THOUSEP> | |
5325 | ||
5326 | =back | |
5327 | ||
5328 | The first item is not fixable (except by upgrading to a later Visual Studio | |
5329 | release), but it would be possible to work around the latter two items by using | |
5330 | the Windows API functions C<GetNumberFormat> and C<GetCurrencyFormat>; patches | |
5331 | welcome. | |
5332 | ||
58e641fb KW |
5333 | Without this function call, threads that use the L<C<setlocale(3)>> system |
5334 | function will not work properly, as all the locale-sensitive functions will | |
5335 | look at the per-thread locale, and C<setlocale> will have no effect on this | |
5336 | thread. | |
5337 | ||
5338 | Perl code should convert to either call | |
5339 | L<C<Perl_setlocale>|perlapi/Perl_setlocale> (which is a drop-in for the system | |
5340 | C<setlocale>) or use the methods given in L<perlcall> to call | |
5341 | L<C<POSIX::setlocale>|POSIX/setlocale>. Either one will transparently properly | |
5342 | handle all cases of single- vs multi-thread, POSIX 2008-supported or not. | |
5343 | ||
5344 | Non-Perl libraries, such as C<gtk>, that call the system C<setlocale> can | |
5345 | continue to work if this function is called before transferring control to the | |
5346 | library. | |
5347 | ||
5348 | Upon return from the code that needs to use the global locale, | |
5349 | L<C<sync_locale()>|perlapi/sync_locale> should be called to restore the safe | |
5350 | multi-thread operation. | |
5351 | ||
5352 | =cut | |
5353 | */ | |
5354 | ||
5355 | void | |
5356 | Perl_switch_to_global_locale() | |
5357 | { | |
4bcc858c | 5358 | dTHX; |
58e641fb KW |
5359 | |
5360 | #ifdef USE_THREAD_SAFE_LOCALE | |
5361 | # ifdef WIN32 | |
5362 | ||
5363 | _configthreadlocale(_DISABLE_PER_THREAD_LOCALE); | |
5364 | ||
5365 | # else | |
58e641fb KW |
5366 | |
5367 | { | |
5368 | unsigned int i; | |
5369 | ||
4fd76d49 | 5370 | for (i = 0; i < LC_ALL_INDEX_; i++) { |
a683a66a | 5371 | setlocale(categories[i], querylocale_i(i)); |
58e641fb KW |
5372 | } |
5373 | } | |
5374 | ||
58e641fb KW |
5375 | uselocale(LC_GLOBAL_LOCALE); |
5376 | ||
5377 | # endif | |
5378 | #endif | |
5379 | ||
5380 | } | |
5381 | ||
5382 | /* | |
5383 | ||
747c467a KW |
5384 | =for apidoc sync_locale |
5385 | ||
b2e9ba0c KW |
5386 | L<C<Perl_setlocale>|perlapi/Perl_setlocale> can be used at any time to query or |
5387 | change the locale (though changing the locale is antisocial and dangerous on | |
5388 | multi-threaded systems that don't have multi-thread safe locale operations. | |
5389 | (See L<perllocale/Multi-threaded operation>). Using the system | |
5390 | L<C<setlocale(3)>> should be avoided. Nevertheless, certain non-Perl libraries | |
5391 | called from XS, such as C<Gtk> do so, and this can't be changed. When the | |
5392 | locale is changed by XS code that didn't use | |
5393 | L<C<Perl_setlocale>|perlapi/Perl_setlocale>, Perl needs to be told that the | |
5394 | locale has changed. Use this function to do so, before returning to Perl. | |
5395 | ||
5396 | The return value is a boolean: TRUE if the global locale at the time of call | |
5397 | was in effect; and FALSE if a per-thread locale was in effect. This can be | |
5398 | used by the caller that needs to restore things as-they-were to decide whether | |
5399 | or not to call | |
5400 | L<C<Perl_switch_to_global_locale>|perlapi/switch_to_global_locale>. | |
747c467a KW |
5401 | |
5402 | =cut | |
5403 | */ | |
5404 | ||
b2e9ba0c KW |
5405 | bool |
5406 | Perl_sync_locale() | |
747c467a | 5407 | { |
d36adde0 KW |
5408 | |
5409 | #ifndef USE_LOCALE | |
5410 | ||
5411 | return TRUE; | |
5412 | ||
5413 | #else | |
5414 | ||
e9bc6d6b | 5415 | const char * newlocale; |
b2e9ba0c KW |
5416 | dTHX; |
5417 | ||
d36adde0 | 5418 | # ifdef USE_POSIX_2008_LOCALE |
b2e9ba0c KW |
5419 | |
5420 | bool was_in_global_locale = FALSE; | |
5421 | locale_t cur_obj = uselocale((locale_t) 0); | |
5422 | ||
5423 | /* On Windows, unless the foreign code has turned off the thread-safe | |
5424 | * locale setting, any plain setlocale() will have affected what we see, so | |
5425 | * no need to worry. Otherwise, If the foreign code has done a plain | |
5426 | * setlocale(), it will only affect the global locale on POSIX systems, but | |
5427 | * will affect the */ | |
5428 | if (cur_obj == LC_GLOBAL_LOCALE) { | |
5429 | ||
d36adde0 | 5430 | # ifdef HAS_QUERY_LOCALE |
b2e9ba0c | 5431 | |
a683a66a | 5432 | void_setlocale_c(LC_ALL, querylocale_c(LC_ALL)); |
b2e9ba0c | 5433 | |
d36adde0 | 5434 | # else |
b2e9ba0c KW |
5435 | |
5436 | unsigned int i; | |
5437 | ||
5438 | /* We can't trust that we can read the LC_ALL format on the | |
5439 | * platform, so do them individually */ | |
4fd76d49 | 5440 | for (i = 0; i < LC_ALL_INDEX_; i++) { |
3980cddb | 5441 | void_setlocale_i(i, querylocale_i(i)); |
b2e9ba0c | 5442 | } |
747c467a | 5443 | |
d36adde0 | 5444 | # endif |
b2e9ba0c KW |
5445 | |
5446 | was_in_global_locale = TRUE; | |
5447 | } | |
5448 | ||
d36adde0 | 5449 | # else |
b2e9ba0c KW |
5450 | |
5451 | bool was_in_global_locale = TRUE; | |
5452 | ||
d36adde0 KW |
5453 | # endif |
5454 | # ifdef USE_LOCALE_CTYPE | |
7d4bcc4a | 5455 | |
a683a66a | 5456 | newlocale = savepv(querylocale_c(LC_CTYPE)); |
9f82ea3e KW |
5457 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
5458 | "%s:%d: %s\n", __FILE__, __LINE__, | |
d188fe6d | 5459 | setlocale_debug_string_c(LC_CTYPE, NULL, newlocale))); |
9f82ea3e | 5460 | new_ctype(newlocale); |
b0bde642 | 5461 | Safefree(newlocale); |
747c467a | 5462 | |
d36adde0 KW |
5463 | # endif /* USE_LOCALE_CTYPE */ |
5464 | # ifdef USE_LOCALE_COLLATE | |
7d4bcc4a | 5465 | |
a683a66a | 5466 | newlocale = savepv(querylocale_c(LC_COLLATE)); |
9f82ea3e KW |
5467 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
5468 | "%s:%d: %s\n", __FILE__, __LINE__, | |
d188fe6d | 5469 | setlocale_debug_string_c(LC_COLLATE, NULL, newlocale))); |
9f82ea3e | 5470 | new_collate(newlocale); |
b0bde642 | 5471 | Safefree(newlocale); |
747c467a | 5472 | |
d36adde0 KW |
5473 | # endif |
5474 | # ifdef USE_LOCALE_NUMERIC | |
7d4bcc4a | 5475 | |
a683a66a | 5476 | newlocale = savepv(querylocale_c(LC_NUMERIC)); |
9f82ea3e KW |
5477 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
5478 | "%s:%d: %s\n", __FILE__, __LINE__, | |
d188fe6d | 5479 | setlocale_debug_string_c(LC_NUMERIC, NULL, newlocale))); |
9f82ea3e | 5480 | new_numeric(newlocale); |
b0bde642 | 5481 | Safefree(newlocale); |
7d4bcc4a | 5482 | |
d36adde0 | 5483 | # endif /* USE_LOCALE_NUMERIC */ |
747c467a | 5484 | |
b2e9ba0c | 5485 | return was_in_global_locale; |
d36adde0 KW |
5486 | |
5487 | #endif | |
5488 | ||
747c467a KW |
5489 | } |
5490 | ||
5d1187d1 KW |
5491 | #if defined(DEBUGGING) && defined(USE_LOCALE) |
5492 | ||
a4f00dcc | 5493 | STATIC char * |
d188fe6d KW |
5494 | S_setlocale_debug_string_i(const unsigned cat_index, |
5495 | const char* const locale, /* Optional locale name */ | |
5d1187d1 KW |
5496 | |
5497 | /* return value from setlocale() when attempting to | |
5498 | * set 'category' to 'locale' */ | |
5499 | const char* const retval) | |
5500 | { | |
5501 | /* Returns a pointer to a NUL-terminated string in static storage with | |
5502 | * added text about the info passed in. This is not thread safe and will | |
5503 | * be overwritten by the next call, so this should be used just to | |
fa07b8e5 | 5504 | * formulate a string to immediately print or savepv() on. */ |
5d1187d1 | 5505 | |
73af6c07 | 5506 | static char ret[1024]; |
d188fe6d | 5507 | assert(cat_index <= NOMINAL_LC_ALL_INDEX); |
7d4bcc4a | 5508 | |
e5f10d49 | 5509 | my_strlcpy(ret, "setlocale(", sizeof(ret)); |
d188fe6d | 5510 | my_strlcat(ret, category_names[cat_index], sizeof(ret)); |
fa07b8e5 | 5511 | my_strlcat(ret, ", ", sizeof(ret)); |
5d1187d1 KW |
5512 | |
5513 | if (locale) { | |
fa07b8e5 KW |
5514 | my_strlcat(ret, "\"", sizeof(ret)); |
5515 | my_strlcat(ret, locale, sizeof(ret)); | |
5516 | my_strlcat(ret, "\"", sizeof(ret)); | |
5d1187d1 KW |
5517 | } |
5518 | else { | |
fa07b8e5 | 5519 | my_strlcat(ret, "NULL", sizeof(ret)); |
5d1187d1 KW |
5520 | } |
5521 | ||
fa07b8e5 | 5522 | my_strlcat(ret, ") returned ", sizeof(ret)); |
5d1187d1 KW |
5523 | |
5524 | if (retval) { | |
fa07b8e5 KW |
5525 | my_strlcat(ret, "\"", sizeof(ret)); |
5526 | my_strlcat(ret, retval, sizeof(ret)); | |
5527 | my_strlcat(ret, "\"", sizeof(ret)); | |
5d1187d1 KW |
5528 | } |
5529 | else { | |
fa07b8e5 | 5530 | my_strlcat(ret, "NULL", sizeof(ret)); |
5d1187d1 KW |
5531 | } |
5532 | ||
5533 | assert(strlen(ret) < sizeof(ret)); | |
5534 | ||
5535 | return ret; | |
5536 | } | |
5537 | ||
5538 | #endif | |
747c467a | 5539 | |
e9bc6d6b KW |
5540 | void |
5541 | Perl_thread_locale_init() | |
5542 | { | |
5543 | /* Called from a thread on startup*/ | |
5544 | ||
5545 | #ifdef USE_THREAD_SAFE_LOCALE | |
5546 | ||
5547 | dTHX_DEBUGGING; | |
5548 | ||
e9bc6d6b KW |
5549 | |
5550 | DEBUG_L(PerlIO_printf(Perl_debug_log, | |
5551 | "%s:%d: new thread, initial locale is %s; calling setlocale\n", | |
5552 | __FILE__, __LINE__, setlocale(LC_ALL, NULL))); | |
5553 | ||
5554 | # ifdef WIN32 | |
5555 | ||
fe1c1494 | 5556 | /* On Windows, make sure new thread has per-thread locales enabled */ |
e9bc6d6b KW |
5557 | _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); |
5558 | ||
5559 | # else | |
5560 | ||
fe1c1494 | 5561 | /* This thread starts off in the C locale */ |
e9bc6d6b KW |
5562 | Perl_setlocale(LC_ALL, "C"); |
5563 | ||
5564 | # endif | |
5565 | #endif | |
5566 | ||
5567 | } | |
5568 | ||
5569 | void | |
5570 | Perl_thread_locale_term() | |
5571 | { | |
5572 | /* Called from a thread as it gets ready to terminate */ | |
5573 | ||
4b06b4d0 | 5574 | #ifdef USE_POSIX_2008_LOCALE |
e9bc6d6b KW |
5575 | |
5576 | /* C starts the new thread in the global C locale. If we are thread-safe, | |
5577 | * we want to not be in the global locale */ | |
5578 | ||
e9bc6d6b KW |
5579 | { /* Free up */ |
5580 | locale_t cur_obj = uselocale(LC_GLOBAL_LOCALE); | |
e72200e7 | 5581 | if (cur_obj != LC_GLOBAL_LOCALE && cur_obj != PL_C_locale_obj) { |
e9bc6d6b KW |
5582 | freelocale(cur_obj); |
5583 | } | |
5584 | } | |
5585 | ||
e9bc6d6b KW |
5586 | #endif |
5587 | ||
5588 | } | |
747c467a KW |
5589 | |
5590 | /* | |
14d04a33 | 5591 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 5592 | */ |