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