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