This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move unlikely executed macro to function
[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
KW
25 *
26 * All C programs have an underlying locale. Perl generally doesn't pay any
27 * attention to it except within the scope of a 'use locale'. For most
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
32 * the desired behavior of those functions at the moment.
166f8a29
DM
33 */
34
98994639
HS
35#include "EXTERN.h"
36#define PERL_IN_LOCALE_C
37#include "perl.h"
38
b310b053
JH
39#ifdef I_LANGINFO
40# include <langinfo.h>
41#endif
42
a4af207c
JH
43#include "reentr.h"
44
8ef6e574
KW
45#ifdef USE_LOCALE
46
98994639 47/*
0d071d52
KW
48 * Standardize the locale name from a string returned by 'setlocale', possibly
49 * modifying that string.
98994639 50 *
0ef2a2b2 51 * The typical return value of setlocale() is either
98994639
HS
52 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
53 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
54 * (the space-separated values represent the various sublocales,
0ef2a2b2 55 * in some unspecified order). This is not handled by this function.
98994639
HS
56 *
57 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
0ef2a2b2
KW
58 * which is harmful for further use of the string in setlocale(). This
59 * function removes the trailing new line and everything up through the '='
98994639
HS
60 *
61 */
62STATIC char *
63S_stdize_locale(pTHX_ char *locs)
64{
7452cf6a 65 const char * const s = strchr(locs, '=');
98994639
HS
66 bool okay = TRUE;
67
7918f24d
NC
68 PERL_ARGS_ASSERT_STDIZE_LOCALE;
69
8772537c
AL
70 if (s) {
71 const char * const t = strchr(s, '.');
98994639 72 okay = FALSE;
8772537c
AL
73 if (t) {
74 const char * const u = strchr(t, '\n');
75 if (u && (u[1] == 0)) {
76 const STRLEN len = u - s;
77 Move(s + 1, locs, len, char);
78 locs[len] = 0;
79 okay = TRUE;
98994639
HS
80 }
81 }
82 }
83
84 if (!okay)
85 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
86
87 return locs;
88}
89
8ef6e574
KW
90#endif
91
98994639
HS
92void
93Perl_set_numeric_radix(pTHX)
94{
95#ifdef USE_LOCALE_NUMERIC
96# ifdef HAS_LOCALECONV
7452cf6a 97 const struct lconv* const lc = localeconv();
98994639 98
98994639
HS
99 if (lc && lc->decimal_point) {
100 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
101 SvREFCNT_dec(PL_numeric_radix_sv);
a0714e2c 102 PL_numeric_radix_sv = NULL;
98994639
HS
103 }
104 else {
105 if (PL_numeric_radix_sv)
106 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
107 else
108 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
9f10db87 109 if (! is_invariant_string((U8 *) lc->decimal_point, 0)
28acfe03 110 && is_utf8_string((U8 *) lc->decimal_point, 0)
c1284011 111 && _is_cur_LC_category_utf8(LC_NUMERIC))
28acfe03
KW
112 {
113 SvUTF8_on(PL_numeric_radix_sv);
114 }
98994639
HS
115 }
116 }
117 else
a0714e2c 118 PL_numeric_radix_sv = NULL;
69014004
KW
119
120 DEBUG_L(PerlIO_printf(Perl_debug_log, "Locale radix is %s\n",
121 (PL_numeric_radix_sv)
122 ? lc->decimal_point
123 : "NULL"));
124
98994639
HS
125# endif /* HAS_LOCALECONV */
126#endif /* USE_LOCALE_NUMERIC */
127}
128
a39edc4c
KW
129/* Is the C string input 'name' "C" or "POSIX"? If so, and 'name' is the
130 * return of setlocale(), then this is extremely likely to be the C or POSIX
131 * locale. However, the output of setlocale() is documented to be opaque, but
132 * the odds are extremely small that it would return these two strings for some
133 * other locale. Note that VMS in these two locales includes many non-ASCII
134 * characters as controls and punctuation (below are hex bytes):
135 * cntrl: 00-1F 7F 84-97 9B-9F
136 * punct: 21-2F 3A-40 5B-60 7B-7E A1-A3 A5 A7-AB B0-B3 B5-B7 B9-BD BF-CF D1-DD DF-EF F1-FD
137 * Oddly, none there are listed as alphas, though some represent alphabetics
138 * http://www.nntp.perl.org/group/perl.perl5.porters/2013/02/msg198753.html */
98b630b3
KW
139#define isNAME_C_OR_POSIX(name) ((name) != NULL \
140 && ((*(name) == 'C' && (*(name + 1)) == '\0') \
a39edc4c
KW
141 || strEQ((name), "POSIX")))
142
98994639 143void
8772537c 144Perl_new_numeric(pTHX_ const char *newnum)
98994639
HS
145{
146#ifdef USE_LOCALE_NUMERIC
0d071d52
KW
147
148 /* Called after all libc setlocale() calls affecting LC_NUMERIC, to tell
149 * core Perl this and that 'newnum' is the name of the new locale.
150 * It installs this locale as the current underlying default.
151 *
152 * The default locale and the C locale can be toggled between by use of the
153 * set_numeric_local() and set_numeric_standard() functions, which should
154 * probably not be called directly, but only via macros like
155 * SET_NUMERIC_STANDARD() in perl.h.
156 *
157 * The toggling is necessary mainly so that a non-dot radix decimal point
158 * character can be output, while allowing internal calculations to use a
159 * dot.
160 *
161 * This sets several interpreter-level variables:
bb304765 162 * PL_numeric_name The underlying locale's name: a copy of 'newnum'
0d071d52 163 * PL_numeric_local A boolean indicating if the toggled state is such
7738054c
KW
164 * that the current locale is the program's underlying
165 * locale
166 * PL_numeric_standard An int indicating if the toggled state is such
167 * that the current locale is the C locale. If non-zero,
168 * it is in C; if > 1, it means it may not be toggled away
169 * from C.
0d071d52
KW
170 * Note that both of the last two variables can be true at the same time,
171 * if the underlying locale is C. (Toggling is a no-op under these
172 * circumstances.)
173 *
174 * Any code changing the locale (outside this file) should use
175 * POSIX::setlocale, which calls this function. Therefore this function
176 * should be called directly only from this file and from
177 * POSIX::setlocale() */
178
b03f34cf 179 char *save_newnum;
98994639
HS
180
181 if (! newnum) {
43c5f42d
NC
182 Safefree(PL_numeric_name);
183 PL_numeric_name = NULL;
98994639
HS
184 PL_numeric_standard = TRUE;
185 PL_numeric_local = TRUE;
186 return;
187 }
188
b03f34cf
KW
189 save_newnum = stdize_locale(savepv(newnum));
190 if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) {
98994639 191 Safefree(PL_numeric_name);
b03f34cf 192 PL_numeric_name = save_newnum;
b03f34cf 193 }
98994639 194
a39edc4c 195 PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum);
e19f01cb 196 PL_numeric_local = TRUE;
4c28b29c
KW
197
198 /* Keep LC_NUMERIC in the C locale. This is for XS modules, so they don't
199 * have to worry about the radix being a non-dot. (Core operations that
200 * need the underlying locale change to it temporarily). */
201 set_numeric_standard();
202
e19f01cb 203 set_numeric_radix();
6959d69d 204
f2ce9e1c
JH
205#else
206 PERL_UNUSED_ARG(newnum);
98994639
HS
207#endif /* USE_LOCALE_NUMERIC */
208}
209
210void
211Perl_set_numeric_standard(pTHX)
212{
213#ifdef USE_LOCALE_NUMERIC
28c1bf33
KW
214 /* Toggle the LC_NUMERIC locale to C. Most code should use the macros like
215 * SET_NUMERIC_STANDARD() in perl.h instead of calling this directly. The
216 * macro avoids calling this routine if toggling isn't necessary according
217 * to our records (which could be wrong if some XS code has changed the
218 * locale behind our back) */
0d071d52 219
a9b8c0d8
KW
220 setlocale(LC_NUMERIC, "C");
221 PL_numeric_standard = TRUE;
222 PL_numeric_local = isNAME_C_OR_POSIX(PL_numeric_name);
223 set_numeric_radix();
69014004
KW
224 DEBUG_L(PerlIO_printf(Perl_debug_log,
225 "Underlying LC_NUMERIC locale now is C\n"));
98994639
HS
226
227#endif /* USE_LOCALE_NUMERIC */
228}
229
230void
231Perl_set_numeric_local(pTHX)
232{
233#ifdef USE_LOCALE_NUMERIC
28c1bf33
KW
234 /* Toggle the LC_NUMERIC locale to the current underlying default. Most
235 * code should use the macros like SET_NUMERIC_LOCAL() in perl.h instead of
236 * calling this directly. The macro avoids calling this routine if
237 * toggling isn't necessary according to our records (which could be wrong
238 * if some XS code has changed the locale behind our back) */
a9b8c0d8
KW
239
240 setlocale(LC_NUMERIC, PL_numeric_name);
241 PL_numeric_standard = isNAME_C_OR_POSIX(PL_numeric_name);
242 PL_numeric_local = TRUE;
243 set_numeric_radix();
69014004
KW
244 DEBUG_L(PerlIO_printf(Perl_debug_log,
245 "Underlying LC_NUMERIC locale now is %s\n",
246 PL_numeric_name));
98994639
HS
247
248#endif /* USE_LOCALE_NUMERIC */
249}
250
251/*
252 * Set up for a new ctype locale.
253 */
254void
8772537c 255Perl_new_ctype(pTHX_ const char *newctype)
98994639
HS
256{
257#ifdef USE_LOCALE_CTYPE
0d071d52
KW
258
259 /* Called after all libc setlocale() calls affecting LC_CTYPE, to tell
260 * core Perl this and that 'newctype' is the name of the new locale.
261 *
262 * This function sets up the folding arrays for all 256 bytes, assuming
263 * that tofold() is tolc() since fold case is not a concept in POSIX,
264 *
265 * Any code changing the locale (outside this file) should use
266 * POSIX::setlocale, which calls this function. Therefore this function
267 * should be called directly only from this file and from
268 * POSIX::setlocale() */
269
27da23d5 270 dVAR;
68067e4e 271 UV i;
98994639 272
7918f24d
NC
273 PERL_ARGS_ASSERT_NEW_CTYPE;
274
215c5139
KW
275 /* We will replace any bad locale warning with 1) nothing if the new one is
276 * ok; or 2) a new warning for the bad new locale */
277 if (PL_warn_locale) {
278 SvREFCNT_dec_NN(PL_warn_locale);
279 PL_warn_locale = NULL;
280 }
281
c1284011 282 PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE);
31f05a37
KW
283
284 /* A UTF-8 locale gets standard rules. But note that code still has to
285 * handle this specially because of the three problematic code points */
286 if (PL_in_utf8_CTYPE_locale) {
287 Copy(PL_fold_latin1, PL_fold_locale, 256, U8);
288 }
289 else {
8c6180a9
KW
290 /* Assume enough space for every character being bad. 4 spaces each
291 * for the 94 printable characters that are output like "'x' "; and 5
292 * spaces each for "'\\' ", "'\t' ", and "'\n' "; plus a terminating
293 * NUL */
294 char bad_chars_list[ (94 * 4) + (3 * 5) + 1 ];
295
296 bool check_for_problems = ckWARN_d(WARN_LOCALE); /* No warnings means
297 no check */
298 bool multi_byte_locale = FALSE; /* Assume is a single-byte locale
299 to start */
300 unsigned int bad_count = 0; /* Count of bad characters */
301
baa60164
KW
302 for (i = 0; i < 256; i++) {
303 if (isUPPER_LC((U8) i))
304 PL_fold_locale[i] = (U8) toLOWER_LC((U8) i);
305 else if (isLOWER_LC((U8) i))
306 PL_fold_locale[i] = (U8) toUPPER_LC((U8) i);
307 else
308 PL_fold_locale[i] = (U8) i;
8c6180a9
KW
309
310 /* If checking for locale problems, see if the native ASCII-range
311 * printables plus \n and \t are in their expected categories in
312 * the new locale. If not, this could mean big trouble, upending
313 * Perl's and most programs' assumptions, like having a
314 * metacharacter with special meaning become a \w. Fortunately,
315 * it's very rare to find locales that aren't supersets of ASCII
316 * nowadays. It isn't a problem for most controls to be changed
317 * into something else; we check only \n and \t, though perhaps \r
318 * could be an issue as well. */
319 if (check_for_problems
320 && (isGRAPH_A(i) || isBLANK_A(i) || i == '\n'))
321 {
322 if ((isALPHANUMERIC_A(i) && ! isALPHANUMERIC_LC(i))
323 || (isPUNCT_A(i) && ! isPUNCT_LC(i))
324 || (isBLANK_A(i) && ! isBLANK_LC(i))
325 || (i == '\n' && ! isCNTRL_LC(i)))
326 {
327 if (bad_count) { /* Separate multiple entries with a
328 blank */
329 bad_chars_list[bad_count++] = ' ';
330 }
331 bad_chars_list[bad_count++] = '\'';
332 if (isPRINT_A(i)) {
333 bad_chars_list[bad_count++] = (char) i;
334 }
335 else {
336 bad_chars_list[bad_count++] = '\\';
337 if (i == '\n') {
338 bad_chars_list[bad_count++] = 'n';
339 }
340 else {
341 assert(i == '\t');
342 bad_chars_list[bad_count++] = 't';
343 }
344 }
345 bad_chars_list[bad_count++] = '\'';
346 bad_chars_list[bad_count] = '\0';
347 }
348 }
349 }
350
351#ifdef MB_CUR_MAX
352 /* We only handle single-byte locales (outside of UTF-8 ones; so if
353 * this locale requires than one byte, there are going to be
354 * problems. */
ba1a4362
KW
355 if (check_for_problems && MB_CUR_MAX > 1
356
357 /* Some platforms return MB_CUR_MAX > 1 for even the "C"
358 * locale. Just assume that the implementation for them (plus
359 * for POSIX) is correct and the > 1 value is spurious. (Since
360 * these are specially handled to never be considered UTF-8
361 * locales, as long as this is the only problem, everything
362 * should work fine */
363 && strNE(newctype, "C") && strNE(newctype, "POSIX"))
364 {
8c6180a9
KW
365 multi_byte_locale = TRUE;
366 }
367#endif
368
369 if (bad_count || multi_byte_locale) {
780fcc9f 370 PL_warn_locale = Perl_newSVpvf(aTHX_
8c6180a9 371 "Locale '%s' may not work well.%s%s%s\n",
780fcc9f 372 newctype,
8c6180a9
KW
373 (multi_byte_locale)
374 ? " Some characters in it are not recognized by"
375 " Perl."
376 : "",
377 (bad_count)
378 ? "\nThe following characters (and maybe others)"
379 " may not have the same meaning as the Perl"
380 " program expects:\n"
381 : "",
382 (bad_count)
383 ? bad_chars_list
384 : ""
385 );
780fcc9f
KW
386 /* If we are actually in the scope of the locale, output the
387 * message now. Otherwise we save it to be output at the first
388 * operation using this locale, if that actually happens. Most
389 * programs don't use locales, so they are immune to bad ones */
390 if (IN_LC(LC_CTYPE)) {
391
392 /* We have to save 'newctype' because the setlocale() just
393 * below may destroy it. The next setlocale() further down
394 * should restore it properly so that the intermediate change
395 * here is transparent to this function's caller */
396 const char * const badlocale = savepv(newctype);
397
398 setlocale(LC_CTYPE, "C");
399
400 /* The '0' below suppresses a bogus gcc compiler warning */
401 Perl_warner(aTHX_ packWARN(WARN_LOCALE), SvPVX(PL_warn_locale), 0);
402 setlocale(LC_CTYPE, badlocale);
c0f3a893 403 Safefree(badlocale);
780fcc9f
KW
404 SvREFCNT_dec_NN(PL_warn_locale);
405 PL_warn_locale = NULL;
406 }
baa60164 407 }
31f05a37 408 }
98994639
HS
409
410#endif /* USE_LOCALE_CTYPE */
7918f24d 411 PERL_ARGS_ASSERT_NEW_CTYPE;
8772537c 412 PERL_UNUSED_ARG(newctype);
96a5add6 413 PERL_UNUSED_CONTEXT;
98994639
HS
414}
415
98994639 416void
2726666d
KW
417Perl__warn_problematic_locale()
418{
419 dTHX;
420
421 /* Outputs the message in PL_warn_locale, and then NULLS it */
422
423#ifdef USE_LOCALE_CTYPE
424
425 if (PL_warn_locale) {
426 /*GCC_DIAG_IGNORE(-Wformat-security); Didn't work */
427 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE),
428 SvPVX(PL_warn_locale),
429 0 /* dummy to avoid compiler warning */ );
430 /* GCC_DIAG_RESTORE; */
431 SvREFCNT_dec_NN(PL_warn_locale);
432 PL_warn_locale = NULL;
433 }
434
435#endif
436
437}
438
439void
8772537c 440Perl_new_collate(pTHX_ const char *newcoll)
98994639
HS
441{
442#ifdef USE_LOCALE_COLLATE
0d071d52
KW
443
444 /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell
445 * core Perl this and that 'newcoll' is the name of the new locale.
446 *
447 * Any code changing the locale (outside this file) should use
448 * POSIX::setlocale, which calls this function. Therefore this function
449 * should be called directly only from this file and from
450 * POSIX::setlocale() */
451
98994639
HS
452 if (! newcoll) {
453 if (PL_collation_name) {
454 ++PL_collation_ix;
455 Safefree(PL_collation_name);
456 PL_collation_name = NULL;
457 }
458 PL_collation_standard = TRUE;
459 PL_collxfrm_base = 0;
460 PL_collxfrm_mult = 2;
461 return;
462 }
463
464 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
465 ++PL_collation_ix;
466 Safefree(PL_collation_name);
467 PL_collation_name = stdize_locale(savepv(newcoll));
a39edc4c 468 PL_collation_standard = isNAME_C_OR_POSIX(newcoll);
98994639
HS
469
470 {
471 /* 2: at most so many chars ('a', 'b'). */
472 /* 50: surely no system expands a char more. */
473#define XFRMBUFSIZE (2 * 50)
474 char xbuf[XFRMBUFSIZE];
8772537c
AL
475 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
476 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
477 const SSize_t mult = fb - fa;
e0601d3c 478 if (mult < 1 && !(fa == 0 && fb == 0))
ad49ad39
NC
479 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
480 (UV) fa, (UV) fb);
eb160463 481 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
98994639
HS
482 PL_collxfrm_mult = mult;
483 }
484 }
485
f2ce9e1c
JH
486#else
487 PERL_UNUSED_ARG(newcoll);
98994639
HS
488#endif /* USE_LOCALE_COLLATE */
489}
490
b385bb4d
KW
491#ifdef WIN32
492
493char *
494Perl_my_setlocale(pTHX_ int category, const char* locale)
495{
496 /* This, for Windows, emulates POSIX setlocale() behavior. There is no
497 * difference unless the input locale is "", which means on Windows to get
498 * the machine default, which is set via the computer's "Regional and
499 * Language Options" (or its current equivalent). In POSIX, it instead
500 * means to find the locale from the user's environment. This routine
501 * looks in the environment, and, if anything is found, uses that instead
502 * of going to the machine default. If there is no environment override,
503 * the machine default is used, as normal, by calling the real setlocale()
504 * with "". The POSIX behavior is to use the LC_ALL variable if set;
505 * otherwise to use the particular category's variable if set; otherwise to
506 * use the LANG variable. */
507
481465ea 508 bool override_LC_ALL = 0;
89f7b9aa
KW
509 char * result;
510
b385bb4d
KW
511 if (locale && strEQ(locale, "")) {
512# ifdef LC_ALL
513 locale = PerlEnv_getenv("LC_ALL");
514 if (! locale) {
515#endif
516 switch (category) {
517# ifdef LC_ALL
518 case LC_ALL:
481465ea 519 override_LC_ALL = TRUE;
b385bb4d
KW
520 break; /* We already know its variable isn't set */
521# endif
522# ifdef USE_LOCALE_TIME
523 case LC_TIME:
524 locale = PerlEnv_getenv("LC_TIME");
525 break;
526# endif
527# ifdef USE_LOCALE_CTYPE
528 case LC_CTYPE:
529 locale = PerlEnv_getenv("LC_CTYPE");
530 break;
531# endif
532# ifdef USE_LOCALE_COLLATE
533 case LC_COLLATE:
534 locale = PerlEnv_getenv("LC_COLLATE");
535 break;
536# endif
537# ifdef USE_LOCALE_MONETARY
538 case LC_MONETARY:
539 locale = PerlEnv_getenv("LC_MONETARY");
540 break;
541# endif
542# ifdef USE_LOCALE_NUMERIC
543 case LC_NUMERIC:
544 locale = PerlEnv_getenv("LC_NUMERIC");
545 break;
546# endif
547# ifdef USE_LOCALE_MESSAGES
548 case LC_MESSAGES:
549 locale = PerlEnv_getenv("LC_MESSAGES");
550 break;
551# endif
552 default:
553 /* This is a category, like PAPER_SIZE that we don't
554 * know about; and so can't provide a wrapper. */
555 break;
556 }
557 if (! locale) {
558 locale = PerlEnv_getenv("LANG");
481465ea 559 if (! locale) {
b385bb4d
KW
560 locale = "";
561 }
562 }
563# ifdef LC_ALL
564 }
565# endif
566 }
567
89f7b9aa
KW
568 result = setlocale(category, locale);
569
481465ea 570 if (! override_LC_ALL) {
89f7b9aa
KW
571 return result;
572 }
573
574 /* Here the input locale was LC_ALL, and we have set it to what is in the
481465ea
KW
575 * LANG variable or the system default if there is no LANG. But these have
576 * lower priority than the other LC_foo variables, so override it for each
577 * one that is set. (If they are set to "", it means to use the same thing
578 * we just set LC_ALL to, so can skip) */
89f7b9aa
KW
579# ifdef USE_LOCALE_TIME
580 result = PerlEnv_getenv("LC_TIME");
730252b2 581 if (result && strNE(result, "")) {
89f7b9aa
KW
582 setlocale(LC_TIME, result);
583 }
584# endif
585# ifdef USE_LOCALE_CTYPE
586 result = PerlEnv_getenv("LC_CTYPE");
730252b2 587 if (result && strNE(result, "")) {
89f7b9aa
KW
588 setlocale(LC_CTYPE, result);
589 }
590# endif
591# ifdef USE_LOCALE_COLLATE
592 result = PerlEnv_getenv("LC_COLLATE");
730252b2 593 if (result && strNE(result, "")) {
89f7b9aa
KW
594 setlocale(LC_COLLATE, result);
595 }
596# endif
597# ifdef USE_LOCALE_MONETARY
598 result = PerlEnv_getenv("LC_MONETARY");
730252b2 599 if (result && strNE(result, "")) {
89f7b9aa
KW
600 setlocale(LC_MONETARY, result);
601 }
602# endif
603# ifdef USE_LOCALE_NUMERIC
604 result = PerlEnv_getenv("LC_NUMERIC");
730252b2 605 if (result && strNE(result, "")) {
89f7b9aa
KW
606 setlocale(LC_NUMERIC, result);
607 }
608# endif
609# ifdef USE_LOCALE_MESSAGES
610 result = PerlEnv_getenv("LC_MESSAGES");
730252b2 611 if (result && strNE(result, "")) {
89f7b9aa
KW
612 setlocale(LC_MESSAGES, result);
613 }
614# endif
615
616 return setlocale(LC_ALL, NULL);
617
b385bb4d
KW
618}
619
620#endif
621
622
98994639
HS
623/*
624 * Initialize locale awareness.
625 */
626int
627Perl_init_i18nl10n(pTHX_ int printwarn)
628{
0e92a118
KW
629 /* printwarn is
630 *
631 * 0 if not to output warning when setup locale is bad
632 * 1 if to output warning based on value of PERL_BADLANG
633 * >1 if to output regardless of PERL_BADLANG
634 *
635 * returns
98994639 636 * 1 = set ok or not applicable,
0e92a118
KW
637 * 0 = fallback to a locale of lower priority
638 * -1 = fallback to all locales failed, not even to the C locale
98994639
HS
639 */
640
0e92a118
KW
641 int ok = 1;
642
98994639 643#if defined(USE_LOCALE)
98994639
HS
644#ifdef USE_LOCALE_CTYPE
645 char *curctype = NULL;
646#endif /* USE_LOCALE_CTYPE */
647#ifdef USE_LOCALE_COLLATE
648 char *curcoll = NULL;
649#endif /* USE_LOCALE_COLLATE */
650#ifdef USE_LOCALE_NUMERIC
651 char *curnum = NULL;
652#endif /* USE_LOCALE_NUMERIC */
653#ifdef __GLIBC__
7452cf6a 654 char * const language = PerlEnv_getenv("LANGUAGE");
98994639 655#endif
65ebb059 656
ccd65d51
KW
657 /* NULL uses the existing already set up locale */
658 const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT"))
659 ? NULL
660 : "";
c3fcd832
KW
661 const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */
662 unsigned int trial_locales_count;
7452cf6a
AL
663 char * const lc_all = PerlEnv_getenv("LC_ALL");
664 char * const lang = PerlEnv_getenv("LANG");
98994639 665 bool setlocale_failure = FALSE;
65ebb059
KW
666 unsigned int i;
667 char *p;
668 const bool locwarn = (printwarn > 1 ||
669 (printwarn &&
96e440d2
JH
670 (!(p = PerlEnv_getenv("PERL_BADLANG")) ||
671 grok_atou(p, NULL))));
0e92a118 672 bool done = FALSE;
6bce99ee
JH
673#ifdef WIN32
674 /* In some systems you can find out the system default locale
675 * and use that as the fallback locale. */
676# define SYSTEM_DEFAULT_LOCALE
677#endif
678#ifdef SYSTEM_DEFAULT_LOCALE
65ebb059 679 const char *system_default_locale = NULL;
6bce99ee 680#endif
98994639 681
0e92a118
KW
682#ifndef LOCALE_ENVIRON_REQUIRED
683 PERL_UNUSED_VAR(done);
684#else
98994639
HS
685
686 /*
687 * Ultrix setlocale(..., "") fails if there are no environment
688 * variables from which to get a locale name.
689 */
690
b3e384bf 691# ifdef LC_ALL
98994639 692 if (lang) {
b385bb4d 693 if (my_setlocale(LC_ALL, setlocale_init))
98994639
HS
694 done = TRUE;
695 else
696 setlocale_failure = TRUE;
697 }
698 if (!setlocale_failure) {
b3e384bf 699# ifdef USE_LOCALE_CTYPE
56279a21 700 Safefree(curctype);
98994639 701 if (! (curctype =
b385bb4d 702 my_setlocale(LC_CTYPE,
98994639 703 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
ccd65d51 704 ? setlocale_init : NULL)))
98994639
HS
705 setlocale_failure = TRUE;
706 else
707 curctype = savepv(curctype);
b3e384bf
KW
708# endif /* USE_LOCALE_CTYPE */
709# ifdef USE_LOCALE_COLLATE
56279a21 710 Safefree(curcoll);
98994639 711 if (! (curcoll =
b385bb4d 712 my_setlocale(LC_COLLATE,
98994639 713 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
ccd65d51 714 ? setlocale_init : NULL)))
98994639
HS
715 setlocale_failure = TRUE;
716 else
717 curcoll = savepv(curcoll);
b3e384bf
KW
718# endif /* USE_LOCALE_COLLATE */
719# ifdef USE_LOCALE_NUMERIC
56279a21 720 Safefree(curnum);
98994639 721 if (! (curnum =
b385bb4d 722 my_setlocale(LC_NUMERIC,
98994639 723 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
ccd65d51 724 ? setlocale_init : NULL)))
98994639
HS
725 setlocale_failure = TRUE;
726 else
727 curnum = savepv(curnum);
b3e384bf 728# endif /* USE_LOCALE_NUMERIC */
a782673d
KW
729# ifdef USE_LOCALE_MESSAGES
730 if (! my_setlocale(LC_MESSAGES,
731 (!done && (lang || PerlEnv_getenv("LC_MESSAGES")))
732 ? setlocale_init : NULL))
733 {
734 setlocale_failure = TRUE;
735 }
736# endif /* USE_LOCALE_MESSAGES */
c835d6be
KW
737# ifdef USE_LOCALE_MONETARY
738 if (! my_setlocale(LC_MONETARY,
739 (!done && (lang || PerlEnv_getenv("LC_MONETARY")))
740 ? setlocale_init : NULL))
741 {
742 setlocale_failure = TRUE;
743 }
744# endif /* USE_LOCALE_MONETARY */
98994639
HS
745 }
746
b3e384bf 747# endif /* LC_ALL */
98994639
HS
748
749#endif /* !LOCALE_ENVIRON_REQUIRED */
750
65ebb059
KW
751 /* We try each locale in the list until we get one that works, or exhaust
752 * the list */
c3fcd832
KW
753 trial_locales[0] = setlocale_init;
754 trial_locales_count = 1;
65ebb059
KW
755 for (i= 0; i < trial_locales_count; i++) {
756 const char * trial_locale = trial_locales[i];
757
758 if (i > 0) {
759
760 /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED
761 * when i==0, but I (khw) don't think that behavior makes much
762 * sense */
763 setlocale_failure = FALSE;
764
6bce99ee
JH
765#ifdef SYSTEM_DEFAULT_LOCALE
766# ifdef WIN32
65ebb059
KW
767 /* On Windows machines, an entry of "" after the 0th means to use
768 * the system default locale, which we now proceed to get. */
769 if (strEQ(trial_locale, "")) {
770 unsigned int j;
771
772 /* Note that this may change the locale, but we are going to do
773 * that anyway just below */
774 system_default_locale = setlocale(LC_ALL, "");
775
776 /* Skip if invalid or it's already on the list of locales to
777 * try */
778 if (! system_default_locale) {
779 goto next_iteration;
780 }
781 for (j = 0; j < trial_locales_count; j++) {
782 if (strEQ(system_default_locale, trial_locales[j])) {
783 goto next_iteration;
784 }
785 }
786
787 trial_locale = system_default_locale;
788 }
6bce99ee
JH
789# endif /* WIN32 */
790#endif /* SYSTEM_DEFAULT_LOCALE */
65ebb059
KW
791 }
792
98994639 793#ifdef LC_ALL
7cd8b568 794 if (! my_setlocale(LC_ALL, trial_locale)) {
49c85077 795 setlocale_failure = TRUE;
7cd8b568
KW
796 }
797 else {
798 /* Since LC_ALL succeeded, it should have changed all the other
799 * categories it can to its value; so we massage things so that the
800 * setlocales below just return their category's current values.
801 * This adequately handles the case in NetBSD where LC_COLLATE may
802 * not be defined for a locale, and setting it individually will
803 * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to
804 * the POSIX locale. */
805 trial_locale = NULL;
806 }
98994639
HS
807#endif /* LC_ALL */
808
49c85077 809 if (!setlocale_failure) {
98994639 810#ifdef USE_LOCALE_CTYPE
49c85077
KW
811 Safefree(curctype);
812 if (! (curctype = my_setlocale(LC_CTYPE, trial_locale)))
813 setlocale_failure = TRUE;
814 else
815 curctype = savepv(curctype);
98994639
HS
816#endif /* USE_LOCALE_CTYPE */
817#ifdef USE_LOCALE_COLLATE
49c85077
KW
818 Safefree(curcoll);
819 if (! (curcoll = my_setlocale(LC_COLLATE, trial_locale)))
820 setlocale_failure = TRUE;
821 else
822 curcoll = savepv(curcoll);
98994639
HS
823#endif /* USE_LOCALE_COLLATE */
824#ifdef USE_LOCALE_NUMERIC
49c85077
KW
825 Safefree(curnum);
826 if (! (curnum = my_setlocale(LC_NUMERIC, trial_locale)))
827 setlocale_failure = TRUE;
828 else
829 curnum = savepv(curnum);
98994639 830#endif /* USE_LOCALE_NUMERIC */
a782673d
KW
831#ifdef USE_LOCALE_MESSAGES
832 if (! (my_setlocale(LC_MESSAGES, trial_locale)))
833 setlocale_failure = TRUE;
834#endif /* USE_LOCALE_MESSAGES */
c835d6be
KW
835#ifdef USE_LOCALE_MONETARY
836 if (! (my_setlocale(LC_MONETARY, trial_locale)))
837 setlocale_failure = TRUE;
838#endif /* USE_LOCALE_MONETARY */
839
49c85077
KW
840 if (! setlocale_failure) { /* Success */
841 break;
842 }
65ebb059 843 }
98994639 844
49c85077
KW
845 /* Here, something failed; will need to try a fallback. */
846 ok = 0;
65ebb059 847
49c85077
KW
848 if (i == 0) {
849 unsigned int j;
98994639 850
65ebb059 851 if (locwarn) { /* Output failure info only on the first one */
98994639
HS
852#ifdef LC_ALL
853
49c85077
KW
854 PerlIO_printf(Perl_error_log,
855 "perl: warning: Setting locale failed.\n");
98994639
HS
856
857#else /* !LC_ALL */
858
49c85077
KW
859 PerlIO_printf(Perl_error_log,
860 "perl: warning: Setting locale failed for the categories:\n\t");
98994639 861#ifdef USE_LOCALE_CTYPE
49c85077
KW
862 if (! curctype)
863 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
98994639
HS
864#endif /* USE_LOCALE_CTYPE */
865#ifdef USE_LOCALE_COLLATE
49c85077
KW
866 if (! curcoll)
867 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
98994639
HS
868#endif /* USE_LOCALE_COLLATE */
869#ifdef USE_LOCALE_NUMERIC
49c85077
KW
870 if (! curnum)
871 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
98994639 872#endif /* USE_LOCALE_NUMERIC */
a782673d 873 PerlIO_printf(Perl_error_log, "and possibly others\n");
98994639
HS
874
875#endif /* LC_ALL */
876
49c85077
KW
877 PerlIO_printf(Perl_error_log,
878 "perl: warning: Please check that your locale settings:\n");
98994639
HS
879
880#ifdef __GLIBC__
49c85077
KW
881 PerlIO_printf(Perl_error_log,
882 "\tLANGUAGE = %c%s%c,\n",
883 language ? '"' : '(',
884 language ? language : "unset",
885 language ? '"' : ')');
98994639
HS
886#endif
887
49c85077
KW
888 PerlIO_printf(Perl_error_log,
889 "\tLC_ALL = %c%s%c,\n",
890 lc_all ? '"' : '(',
891 lc_all ? lc_all : "unset",
892 lc_all ? '"' : ')');
98994639
HS
893
894#if defined(USE_ENVIRON_ARRAY)
49c85077
KW
895 {
896 char **e;
897 for (e = environ; *e; e++) {
898 if (strnEQ(*e, "LC_", 3)
899 && strnNE(*e, "LC_ALL=", 7)
900 && (p = strchr(*e, '=')))
901 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
902 (int)(p - *e), *e, p + 1);
903 }
904 }
98994639 905#else
49c85077
KW
906 PerlIO_printf(Perl_error_log,
907 "\t(possibly more locale environment variables)\n");
98994639
HS
908#endif
909
49c85077
KW
910 PerlIO_printf(Perl_error_log,
911 "\tLANG = %c%s%c\n",
912 lang ? '"' : '(',
913 lang ? lang : "unset",
914 lang ? '"' : ')');
98994639 915
49c85077
KW
916 PerlIO_printf(Perl_error_log,
917 " are supported and installed on your system.\n");
918 }
98994639 919
65ebb059
KW
920 /* Calculate what fallback locales to try. We have avoided this
921 * until we have to, becuase failure is quite unlikely. This will
922 * usually change the upper bound of the loop we are in.
923 *
924 * Since the system's default way of setting the locale has not
925 * found one that works, We use Perl's defined ordering: LC_ALL,
926 * LANG, and the C locale. We don't try the same locale twice, so
927 * don't add to the list if already there. (On POSIX systems, the
928 * LC_ALL element will likely be a repeat of the 0th element "",
929 * but there's no harm done by doing it explicitly */
930 if (lc_all) {
931 for (j = 0; j < trial_locales_count; j++) {
932 if (strEQ(lc_all, trial_locales[j])) {
933 goto done_lc_all;
934 }
935 }
936 trial_locales[trial_locales_count++] = lc_all;
937 }
938 done_lc_all:
98994639 939
65ebb059
KW
940 if (lang) {
941 for (j = 0; j < trial_locales_count; j++) {
942 if (strEQ(lang, trial_locales[j])) {
943 goto done_lang;
944 }
945 }
946 trial_locales[trial_locales_count++] = lang;
947 }
948 done_lang:
949
950#if defined(WIN32) && defined(LC_ALL)
951 /* For Windows, we also try the system default locale before "C".
952 * (If there exists a Windows without LC_ALL we skip this because
953 * it gets too complicated. For those, the "C" is the next
954 * fallback possibility). The "" is the same as the 0th element of
955 * the array, but the code at the loop above knows to treat it
956 * differently when not the 0th */
957 trial_locales[trial_locales_count++] = "";
958#endif
959
960 for (j = 0; j < trial_locales_count; j++) {
961 if (strEQ("C", trial_locales[j])) {
962 goto done_C;
963 }
964 }
965 trial_locales[trial_locales_count++] = "C";
98994639 966
65ebb059
KW
967 done_C: ;
968 } /* end of first time through the loop */
98994639 969
65ebb059
KW
970#ifdef WIN32
971 next_iteration: ;
972#endif
973
974 } /* end of looping through the trial locales */
975
976 if (ok < 1) { /* If we tried to fallback */
977 const char* msg;
978 if (! setlocale_failure) { /* fallback succeeded */
979 msg = "Falling back to";
980 }
981 else { /* fallback failed */
98994639 982
65ebb059
KW
983 /* We dropped off the end of the loop, so have to decrement i to
984 * get back to the value the last time through */
985 i--;
98994639 986
65ebb059
KW
987 ok = -1;
988 msg = "Failed to fall back to";
989
990 /* To continue, we should use whatever values we've got */
98994639 991#ifdef USE_LOCALE_CTYPE
49c85077
KW
992 Safefree(curctype);
993 curctype = savepv(setlocale(LC_CTYPE, NULL));
98994639
HS
994#endif /* USE_LOCALE_CTYPE */
995#ifdef USE_LOCALE_COLLATE
49c85077
KW
996 Safefree(curcoll);
997 curcoll = savepv(setlocale(LC_COLLATE, NULL));
98994639
HS
998#endif /* USE_LOCALE_COLLATE */
999#ifdef USE_LOCALE_NUMERIC
49c85077
KW
1000 Safefree(curnum);
1001 curnum = savepv(setlocale(LC_NUMERIC, NULL));
98994639 1002#endif /* USE_LOCALE_NUMERIC */
65ebb059
KW
1003 }
1004
1005 if (locwarn) {
1006 const char * description;
1007 const char * name = "";
1008 if (strEQ(trial_locales[i], "C")) {
1009 description = "the standard locale";
1010 name = "C";
1011 }
6bce99ee 1012#ifdef SYSTEM_DEFAULT_LOCALE
65ebb059
KW
1013 else if (strEQ(trial_locales[i], "")) {
1014 description = "the system default locale";
1015 if (system_default_locale) {
1016 name = system_default_locale;
1017 }
1018 }
6bce99ee 1019#endif /* SYSTEM_DEFAULT_LOCALE */
65ebb059
KW
1020 else {
1021 description = "a fallback locale";
1022 name = trial_locales[i];
1023 }
1024 if (name && strNE(name, "")) {
1025 PerlIO_printf(Perl_error_log,
1026 "perl: warning: %s %s (\"%s\").\n", msg, description, name);
1027 }
1028 else {
1029 PerlIO_printf(Perl_error_log,
1030 "perl: warning: %s %s.\n", msg, description);
1031 }
1032 }
1033 } /* End of tried to fallback */
98994639
HS
1034
1035#ifdef USE_LOCALE_CTYPE
1036 new_ctype(curctype);
1037#endif /* USE_LOCALE_CTYPE */
1038
1039#ifdef USE_LOCALE_COLLATE
1040 new_collate(curcoll);
1041#endif /* USE_LOCALE_COLLATE */
1042
1043#ifdef USE_LOCALE_NUMERIC
1044 new_numeric(curnum);
1045#endif /* USE_LOCALE_NUMERIC */
b310b053 1046
8ef6e574 1047#if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE)
49c85077
KW
1048 /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE
1049 * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by
1050 * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the
1051 * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open
1052 * discipline. */
c1284011 1053 PL_utf8locale = _is_cur_LC_category_utf8(LC_CTYPE);
49c85077 1054
a05d7ebb 1055 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
fde18df1
JH
1056 This is an alternative to using the -C command line switch
1057 (the -C if present will override this). */
1058 {
dd374669 1059 const char *p = PerlEnv_getenv("PERL_UNICODE");
a05d7ebb 1060 PL_unicode = p ? parse_unicode_opts(&p) : 0;
5a22a2bb
NC
1061 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
1062 PL_utf8cache = -1;
b310b053 1063 }
ec71e770 1064#endif
b310b053 1065
98994639 1066#ifdef USE_LOCALE_CTYPE
43c5f42d 1067 Safefree(curctype);
98994639
HS
1068#endif /* USE_LOCALE_CTYPE */
1069#ifdef USE_LOCALE_COLLATE
43c5f42d 1070 Safefree(curcoll);
98994639
HS
1071#endif /* USE_LOCALE_COLLATE */
1072#ifdef USE_LOCALE_NUMERIC
43c5f42d 1073 Safefree(curnum);
98994639 1074#endif /* USE_LOCALE_NUMERIC */
8ef6e574 1075
f2ce9e1c
JH
1076#else /* !USE_LOCALE */
1077 PERL_UNUSED_ARG(printwarn);
8ef6e574
KW
1078#endif /* USE_LOCALE */
1079
98994639
HS
1080 return ok;
1081}
1082
49c85077 1083
98994639
HS
1084#ifdef USE_LOCALE_COLLATE
1085
1086/*
1087 * mem_collxfrm() is a bit like strxfrm() but with two important
1088 * differences. First, it handles embedded NULs. Second, it allocates
1089 * a bit more memory than needed for the transformed data itself.
1090 * The real transformed data begins at offset sizeof(collationix).
1091 * Please see sv_collxfrm() to see how this is used.
1092 */
166f8a29 1093
98994639
HS
1094char *
1095Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
1096{
1097 char *xbuf;
1098 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
1099
7918f24d
NC
1100 PERL_ARGS_ASSERT_MEM_COLLXFRM;
1101
98994639
HS
1102 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
1103 /* the +1 is for the terminating NUL. */
1104
1105 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
a02a5408 1106 Newx(xbuf, xAlloc, char);
98994639
HS
1107 if (! xbuf)
1108 goto bad;
1109
1110 *(U32*)xbuf = PL_collation_ix;
1111 xout = sizeof(PL_collation_ix);
1112 for (xin = 0; xin < len; ) {
224e8ef5 1113 Size_t xused;
98994639
HS
1114
1115 for (;;) {
1116 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
224e8ef5 1117 if (xused >= PERL_INT_MAX)
98994639 1118 goto bad;
eb160463 1119 if ((STRLEN)xused < xAlloc - xout)
98994639
HS
1120 break;
1121 xAlloc = (2 * xAlloc) + 1;
1122 Renew(xbuf, xAlloc, char);
1123 if (! xbuf)
1124 goto bad;
1125 }
1126
1127 xin += strlen(s + xin) + 1;
1128 xout += xused;
1129
1130 /* Embedded NULs are understood but silently skipped
1131 * because they make no sense in locale collation. */
1132 }
1133
1134 xbuf[xout] = '\0';
1135 *xlen = xout - sizeof(PL_collation_ix);
1136 return xbuf;
1137
1138 bad:
1139 Safefree(xbuf);
1140 *xlen = 0;
1141 return NULL;
1142}
1143
1144#endif /* USE_LOCALE_COLLATE */
1145
8ef6e574
KW
1146#ifdef USE_LOCALE
1147
c1284011
KW
1148bool
1149Perl__is_cur_LC_category_utf8(pTHX_ int category)
7d74bb61
KW
1150{
1151 /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
1152 * otherwise. 'category' may not be LC_ALL. If the platform doesn't have
119ee68b 1153 * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence
609548d2
KW
1154 * could give the wrong result. The result will very likely be correct for
1155 * languages that have commonly used non-ASCII characters, but for notably
1156 * English, it comes down to if the locale's name ends in something like
1157 * "UTF-8". It errs on the side of not being a UTF-8 locale. */
7d74bb61
KW
1158
1159 char *save_input_locale = NULL;
7d74bb61
KW
1160 STRLEN final_pos;
1161
8ef6e574 1162#ifdef LC_ALL
7d74bb61 1163 assert(category != LC_ALL);
8ef6e574 1164#endif
7d74bb61
KW
1165
1166 /* First dispose of the trivial cases */
b07fffd1 1167 save_input_locale = setlocale(category, NULL);
7d74bb61 1168 if (! save_input_locale) {
69014004
KW
1169 DEBUG_L(PerlIO_printf(Perl_debug_log,
1170 "Could not find current locale for category %d\n",
1171 category));
7d74bb61
KW
1172 return FALSE; /* XXX maybe should croak */
1173 }
b07fffd1 1174 save_input_locale = stdize_locale(savepv(save_input_locale));
a39edc4c 1175 if (isNAME_C_OR_POSIX(save_input_locale)) {
69014004
KW
1176 DEBUG_L(PerlIO_printf(Perl_debug_log,
1177 "Current locale for category %d is %s\n",
1178 category, save_input_locale));
b07fffd1 1179 Safefree(save_input_locale);
7d74bb61
KW
1180 return FALSE;
1181 }
1182
1d958db2
KW
1183#if defined(USE_LOCALE_CTYPE) \
1184 && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET)))
7d74bb61 1185
1d958db2 1186 { /* Next try nl_langinfo or MB_CUR_MAX if available */
7d74bb61
KW
1187
1188 char *save_ctype_locale = NULL;
119ee68b 1189 bool is_utf8;
7d74bb61 1190
119ee68b 1191 if (category != LC_CTYPE) { /* These work only on LC_CTYPE */
7d74bb61
KW
1192
1193 /* Get the current LC_CTYPE locale */
4f72bb37 1194 save_ctype_locale = setlocale(LC_CTYPE, NULL);
7d74bb61 1195 if (! save_ctype_locale) {
69014004
KW
1196 DEBUG_L(PerlIO_printf(Perl_debug_log,
1197 "Could not find current locale for LC_CTYPE\n"));
7d74bb61
KW
1198 goto cant_use_nllanginfo;
1199 }
4f72bb37 1200 save_ctype_locale = stdize_locale(savepv(save_ctype_locale));
7d74bb61
KW
1201
1202 /* If LC_CTYPE and the desired category use the same locale, this
1203 * means that finding the value for LC_CTYPE is the same as finding
1204 * the value for the desired category. Otherwise, switch LC_CTYPE
1205 * to the desired category's locale */
1206 if (strEQ(save_ctype_locale, save_input_locale)) {
1207 Safefree(save_ctype_locale);
1208 save_ctype_locale = NULL;
1209 }
1210 else if (! setlocale(LC_CTYPE, save_input_locale)) {
69014004
KW
1211 DEBUG_L(PerlIO_printf(Perl_debug_log,
1212 "Could not change LC_CTYPE locale to %s\n",
1213 save_input_locale));
7d74bb61
KW
1214 Safefree(save_ctype_locale);
1215 goto cant_use_nllanginfo;
1216 }
1217 }
1218
69014004
KW
1219 DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n",
1220 save_input_locale));
1221
7d74bb61 1222 /* Here the current LC_CTYPE is set to the locale of the category whose
1d958db2
KW
1223 * information is desired. This means that nl_langinfo() and MB_CUR_MAX
1224 * should give the correct results */
119ee68b 1225
1d958db2
KW
1226# if defined(HAS_NL_LANGINFO) && defined(CODESET)
1227 {
4f72bb37 1228 char *codeset = nl_langinfo(CODESET);
1d958db2 1229 if (codeset && strNE(codeset, "")) {
4f72bb37 1230 codeset = savepv(codeset);
119ee68b 1231
1d958db2
KW
1232 /* If we switched LC_CTYPE, switch back */
1233 if (save_ctype_locale) {
1234 setlocale(LC_CTYPE, save_ctype_locale);
1235 Safefree(save_ctype_locale);
1236 }
1237
1238 is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8"))
1239 || foldEQ(codeset, STR_WITH_LEN("UTF8"));
1240
69014004
KW
1241 DEBUG_L(PerlIO_printf(Perl_debug_log,
1242 "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n",
1243 codeset, is_utf8));
1d958db2
KW
1244 Safefree(codeset);
1245 Safefree(save_input_locale);
1246 return is_utf8;
1247 }
119ee68b
KW
1248 }
1249
1d958db2
KW
1250# endif
1251# ifdef MB_CUR_MAX
1252
1253 /* Here, either we don't have nl_langinfo, or it didn't return a
1254 * codeset. Try MB_CUR_MAX */
1255
119ee68b
KW
1256 /* Standard UTF-8 needs at least 4 bytes to represent the maximum
1257 * Unicode code point. Since UTF-8 is the only non-single byte
1258 * encoding we handle, we just say any such encoding is UTF-8, and if
1259 * turns out to be wrong, other things will fail */
1260 is_utf8 = MB_CUR_MAX >= 4;
1261
69014004
KW
1262 DEBUG_L(PerlIO_printf(Perl_debug_log,
1263 "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n",
1264 (int) MB_CUR_MAX, is_utf8));
1265
119ee68b
KW
1266 Safefree(save_input_locale);
1267
1268# ifdef HAS_MBTOWC
1269
1270 /* ... But, most system that have MB_CUR_MAX will also have mbtowc(),
1271 * since they are both in the C99 standard. We can feed a known byte
1272 * string to the latter function, and check that it gives the expected
1273 * result */
1274 if (is_utf8) {
1275 wchar_t wc;
856b881c 1276 PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */
69014004 1277 errno = 0;
f019f68f 1278 if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
119ee68b
KW
1279 != strlen(HYPHEN_UTF8)
1280 || wc != (wchar_t) 0x2010)
1281 {
1282 is_utf8 = FALSE;
abdcbdb8 1283 DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", (unsigned int)wc));
69014004
KW
1284 DEBUG_L(PerlIO_printf(Perl_debug_log,
1285 "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n",
1286 mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno));
119ee68b
KW
1287 }
1288 }
119ee68b
KW
1289# endif
1290
1d958db2
KW
1291 /* If we switched LC_CTYPE, switch back */
1292 if (save_ctype_locale) {
1293 setlocale(LC_CTYPE, save_ctype_locale);
1294 Safefree(save_ctype_locale);
119ee68b 1295 }
7d74bb61 1296
1d958db2 1297 return is_utf8;
119ee68b 1298# endif
7d74bb61 1299 }
119ee68b 1300
7d74bb61
KW
1301 cant_use_nllanginfo:
1302
0080c90a
KW
1303#else /* nl_langinfo should work if available, so don't bother compiling this
1304 fallback code. The final fallback of looking at the name is
1305 compiled, and will be executed if nl_langinfo fails */
7d74bb61 1306
97f4de96
KW
1307 /* nl_langinfo not available or failed somehow. Next try looking at the
1308 * currency symbol to see if it disambiguates things. Often that will be
1309 * in the native script, and if the symbol isn't in UTF-8, we know that the
1310 * locale isn't. If it is non-ASCII UTF-8, we infer that the locale is
609548d2
KW
1311 * too, as the odds of a non-UTF8 string being valid UTF-8 are quite small
1312 * */
fa9b773e
KW
1313
1314#ifdef HAS_LOCALECONV
fa9b773e 1315# ifdef USE_LOCALE_MONETARY
fa9b773e
KW
1316 {
1317 char *save_monetary_locale = NULL;
fa9b773e 1318 bool only_ascii = FALSE;
13542a67
KW
1319 bool is_utf8 = FALSE;
1320 struct lconv* lc;
fa9b773e 1321
97f4de96
KW
1322 /* Like above for LC_CTYPE, we first set LC_MONETARY to the locale of
1323 * the desired category, if it isn't that locale already */
1324
fa9b773e
KW
1325 if (category != LC_MONETARY) {
1326
4f72bb37 1327 save_monetary_locale = setlocale(LC_MONETARY, NULL);
fa9b773e 1328 if (! save_monetary_locale) {
69014004
KW
1329 DEBUG_L(PerlIO_printf(Perl_debug_log,
1330 "Could not find current locale for LC_MONETARY\n"));
fa9b773e
KW
1331 goto cant_use_monetary;
1332 }
4f72bb37 1333 save_monetary_locale = stdize_locale(savepv(save_monetary_locale));
fa9b773e 1334
13542a67
KW
1335 if (strEQ(save_monetary_locale, save_input_locale)) {
1336 Safefree(save_monetary_locale);
1337 save_monetary_locale = NULL;
1338 }
1339 else if (! setlocale(LC_MONETARY, save_input_locale)) {
59c234b4
KW
1340 DEBUG_L(PerlIO_printf(Perl_debug_log,
1341 "Could not change LC_MONETARY locale to %s\n",
1342 save_input_locale));
1343 Safefree(save_monetary_locale);
1344 goto cant_use_monetary;
fa9b773e
KW
1345 }
1346 }
1347
1348 /* Here the current LC_MONETARY is set to the locale of the category
1349 * whose information is desired. */
1350
13542a67
KW
1351 lc = localeconv();
1352 if (! lc
1353 || ! lc->currency_symbol
9f10db87 1354 || is_invariant_string((U8 *) lc->currency_symbol, 0))
13542a67
KW
1355 {
1356 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));
1357 only_ascii = TRUE;
1358 }
1359 else {
1360 is_utf8 = is_utf8_string((U8 *) lc->currency_symbol, 0);
fa9b773e
KW
1361 }
1362
1363 /* If we changed it, restore LC_MONETARY to its original locale */
1364 if (save_monetary_locale) {
1365 setlocale(LC_MONETARY, save_monetary_locale);
1366 Safefree(save_monetary_locale);
1367 }
1368
13542a67 1369 if (! only_ascii) {
fa9b773e 1370
59c234b4
KW
1371 /* It isn't a UTF-8 locale if the symbol is not legal UTF-8;
1372 * otherwise assume the locale is UTF-8 if and only if the symbol
1373 * is non-ascii UTF-8. */
1374 DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?Currency symbol for %s is UTF-8=%d\n",
1375 save_input_locale, is_utf8));
1376 Safefree(save_input_locale);
1377 return is_utf8;
13542a67 1378 }
fa9b773e
KW
1379 }
1380 cant_use_monetary:
1381
1382# endif /* USE_LOCALE_MONETARY */
1383#endif /* HAS_LOCALECONV */
1384
15f7e74e
KW
1385#if defined(HAS_STRFTIME) && defined(USE_LOCALE_TIME)
1386
1387/* Still haven't found a non-ASCII string to disambiguate UTF-8 or not. Try
1388 * the names of the months and weekdays, timezone, and am/pm indicator */
1389 {
1390 char *save_time_locale = NULL;
1391 int hour = 10;
1392 bool is_dst = FALSE;
1393 int dom = 1;
1394 int month = 0;
1395 int i;
1396 char * formatted_time;
1397
1398
1399 /* Like above for LC_MONETARY, we set LC_TIME to the locale of the
1400 * desired category, if it isn't that locale already */
1401
1402 if (category != LC_TIME) {
1403
1404 save_time_locale = setlocale(LC_TIME, NULL);
1405 if (! save_time_locale) {
1406 DEBUG_L(PerlIO_printf(Perl_debug_log,
1407 "Could not find current locale for LC_TIME\n"));
1408 goto cant_use_time;
1409 }
1410 save_time_locale = stdize_locale(savepv(save_time_locale));
1411
1412 if (strEQ(save_time_locale, save_input_locale)) {
1413 Safefree(save_time_locale);
1414 save_time_locale = NULL;
1415 }
1416 else if (! setlocale(LC_TIME, save_input_locale)) {
1417 DEBUG_L(PerlIO_printf(Perl_debug_log,
1418 "Could not change LC_TIME locale to %s\n",
1419 save_input_locale));
1420 Safefree(save_time_locale);
1421 goto cant_use_time;
1422 }
1423 }
1424
1425 /* Here the current LC_TIME is set to the locale of the category
1426 * whose information is desired. Look at all the days of the week and
9f10db87 1427 * month names, and the timezone and am/pm indicator for UTF-8 variant
15f7e74e
KW
1428 * characters. The first such a one found will tell us if the locale
1429 * is UTF-8 or not */
1430
1431 for (i = 0; i < 7 + 12; i++) { /* 7 days; 12 months */
1432 formatted_time = my_strftime("%A %B %Z %p",
1433 0, 0, hour, dom, month, 112, 0, 0, is_dst);
9f10db87 1434 if (! formatted_time || is_invariant_string((U8 *) formatted_time, 0)) {
15f7e74e
KW
1435
1436 /* Here, we didn't find a non-ASCII. Try the next time through
1437 * with the complemented dst and am/pm, and try with the next
1438 * weekday. After we have gotten all weekdays, try the next
1439 * month */
1440 is_dst = ! is_dst;
1441 hour = (hour + 12) % 24;
1442 dom++;
1443 if (i > 6) {
1444 month++;
1445 }
1446 continue;
1447 }
1448
1449 /* Here, we have a non-ASCII. Return TRUE is it is valid UTF8;
1450 * false otherwise. But first, restore LC_TIME to its original
1451 * locale if we changed it */
1452 if (save_time_locale) {
1453 setlocale(LC_TIME, save_time_locale);
1454 Safefree(save_time_locale);
1455 }
1456
1457 DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?time-related strings for %s are UTF-8=%d\n",
1458 save_input_locale,
1459 is_utf8_string((U8 *) formatted_time, 0)));
1460 Safefree(save_input_locale);
1461 return is_utf8_string((U8 *) formatted_time, 0);
1462 }
1463
1464 /* Falling off the end of the loop indicates all the names were just
1465 * ASCII. Go on to the next test. If we changed it, restore LC_TIME
1466 * to its original locale */
1467 if (save_time_locale) {
1468 setlocale(LC_TIME, save_time_locale);
1469 Safefree(save_time_locale);
1470 }
1471 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));
1472 }
1473 cant_use_time:
1474
1475#endif
1476
1477#if 0 && defined(USE_LOCALE_MESSAGES) && defined(HAS_SYS_ERRLIST)
855aeb93
JH
1478
1479/* This code is ifdefd out because it was found to not be necessary in testing
5857e934
KW
1480 * on our dromedary test machine, which has over 700 locales. There, this
1481 * added no value to looking at the currency symbol and the time strings. I
1482 * left it in so as to avoid rewriting it if real-world experience indicates
1483 * that dromedary is an outlier. Essentially, instead of returning abpve if we
855aeb93
JH
1484 * haven't found illegal utf8, we continue on and examine all the strerror()
1485 * messages on the platform for utf8ness. If all are ASCII, we still don't
1486 * know the answer; but otherwise we have a pretty good indication of the
5857e934
KW
1487 * utf8ness. The reason this doesn't help much is that the messages may not
1488 * have been translated into the locale. The currency symbol and time strings
1489 * are much more likely to have been translated. */
1490 {
855aeb93 1491 int e;
5857e934
KW
1492 bool is_utf8 = FALSE;
1493 bool non_ascii = FALSE;
855aeb93 1494 char *save_messages_locale = NULL;
5857e934 1495 const char * errmsg = NULL;
855aeb93 1496
5857e934
KW
1497 /* Like above, we set LC_MESSAGES to the locale of the desired
1498 * category, if it isn't that locale already */
855aeb93
JH
1499
1500 if (category != LC_MESSAGES) {
1501
5857e934 1502 save_messages_locale = setlocale(LC_MESSAGES, NULL);
855aeb93 1503 if (! save_messages_locale) {
5857e934
KW
1504 DEBUG_L(PerlIO_printf(Perl_debug_log,
1505 "Could not find current locale for LC_MESSAGES\n"));
855aeb93
JH
1506 goto cant_use_messages;
1507 }
5857e934 1508 save_messages_locale = stdize_locale(savepv(save_messages_locale));
855aeb93
JH
1509
1510 if (strEQ(save_messages_locale, save_input_locale)) {
5857e934
KW
1511 Safefree(save_messages_locale);
1512 save_messages_locale = NULL;
855aeb93
JH
1513 }
1514 else if (! setlocale(LC_MESSAGES, save_input_locale)) {
5857e934
KW
1515 DEBUG_L(PerlIO_printf(Perl_debug_log,
1516 "Could not change LC_MESSAGES locale to %s\n",
1517 save_input_locale));
855aeb93
JH
1518 Safefree(save_messages_locale);
1519 goto cant_use_messages;
1520 }
1521 }
1522
1523 /* Here the current LC_MESSAGES is set to the locale of the category
5857e934
KW
1524 * whose information is desired. Look through all the messages. We
1525 * can't use Strerror() here because it may expand to code that
1526 * segfaults in miniperl */
855aeb93 1527
5857e934
KW
1528 for (e = 0; e <= sys_nerr; e++) {
1529 errno = 0;
1530 errmsg = sys_errlist[e];
1531 if (errno || !errmsg) {
855aeb93
JH
1532 break;
1533 }
5857e934 1534 errmsg = savepv(errmsg);
9f10db87 1535 if (! is_invariant_string((U8 *) errmsg, 0)) {
5857e934
KW
1536 non_ascii = TRUE;
1537 is_utf8 = is_utf8_string((U8 *) errmsg, 0);
1538 break;
855aeb93
JH
1539 }
1540 }
5857e934 1541 Safefree(errmsg);
855aeb93
JH
1542
1543 /* And, if we changed it, restore LC_MESSAGES to its original locale */
1544 if (save_messages_locale) {
1545 setlocale(LC_MESSAGES, save_messages_locale);
1546 Safefree(save_messages_locale);
1547 }
1548
5857e934
KW
1549 if (non_ascii) {
1550
1551 /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid,
1552 * any non-ascii means it is one; otherwise we assume it isn't */
1553 DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?error messages for %s are UTF-8=%d\n",
1554 save_input_locale,
1555 is_utf8));
1556 Safefree(save_input_locale);
1557 return is_utf8;
1558 }
855aeb93 1559
5857e934 1560 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
1561 }
1562 cant_use_messages:
1563
1564#endif
fa9b773e 1565
0080c90a
KW
1566#endif /* the code that is compiled when no nl_langinfo */
1567
92c0a900
KW
1568#ifndef EBCDIC /* On os390, even if the name ends with "UTF-8', it isn't a
1569 UTF-8 locale */
97f4de96
KW
1570 /* As a last resort, look at the locale name to see if it matches
1571 * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the
1572 * return of setlocale(), is actually defined to be opaque, so we can't
1573 * really rely on the absence of various substrings in the name to indicate
1574 * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to
1575 * be a UTF-8 locale. Similarly for the other common names */
1576
1577 final_pos = strlen(save_input_locale) - 1;
1578 if (final_pos >= 3) {
1579 char *name = save_input_locale;
1580
1581 /* Find next 'U' or 'u' and look from there */
1582 while ((name += strcspn(name, "Uu") + 1)
1583 <= save_input_locale + final_pos - 2)
1584 {
305b8651
KW
1585 if (!isALPHA_FOLD_NE(*name, 't')
1586 || isALPHA_FOLD_NE(*(name + 1), 'f'))
97f4de96
KW
1587 {
1588 continue;
1589 }
1590 name += 2;
1591 if (*(name) == '-') {
1592 if ((name > save_input_locale + final_pos - 1)) {
1593 break;
1594 }
1595 name++;
1596 }
1597 if (*(name) == '8') {
97f4de96
KW
1598 DEBUG_L(PerlIO_printf(Perl_debug_log,
1599 "Locale %s ends with UTF-8 in name\n",
1600 save_input_locale));
00c54b9c 1601 Safefree(save_input_locale);
97f4de96
KW
1602 return TRUE;
1603 }
1604 }
1605 DEBUG_L(PerlIO_printf(Perl_debug_log,
1606 "Locale %s doesn't end with UTF-8 in name\n",
1607 save_input_locale));
1608 }
92c0a900 1609#endif
97f4de96
KW
1610
1611#ifdef WIN32
1612 /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
1613 if (final_pos >= 4
1614 && *(save_input_locale + final_pos - 0) == '1'
1615 && *(save_input_locale + final_pos - 1) == '0'
1616 && *(save_input_locale + final_pos - 2) == '0'
1617 && *(save_input_locale + final_pos - 3) == '5'
1618 && *(save_input_locale + final_pos - 4) == '6')
1619 {
1620 DEBUG_L(PerlIO_printf(Perl_debug_log,
1621 "Locale %s ends with 10056 in name, is UTF-8 locale\n",
1622 save_input_locale));
1623 Safefree(save_input_locale);
1624 return TRUE;
1625 }
1626#endif
1627
1628 /* Other common encodings are the ISO 8859 series, which aren't UTF-8. But
1629 * since we are about to return FALSE anyway, there is no point in doing
1630 * this extra work */
1631#if 0
1632 if (instr(save_input_locale, "8859")) {
1633 DEBUG_L(PerlIO_printf(Perl_debug_log,
1634 "Locale %s has 8859 in name, not UTF-8 locale\n",
1635 save_input_locale));
1636 Safefree(save_input_locale);
1637 return FALSE;
1638 }
1639#endif
1640
69014004
KW
1641 DEBUG_L(PerlIO_printf(Perl_debug_log,
1642 "Assuming locale %s is not a UTF-8 locale\n",
1643 save_input_locale));
fa9b773e 1644 Safefree(save_input_locale);
7d74bb61
KW
1645 return FALSE;
1646}
1647
8ef6e574 1648#endif
7d74bb61 1649
d6ded950
KW
1650
1651bool
1652Perl__is_in_locale_category(pTHX_ const bool compiling, const int category)
1653{
1a4f13e1 1654 dVAR;
d6ded950
KW
1655 /* Internal function which returns if we are in the scope of a pragma that
1656 * enables the locale category 'category'. 'compiling' should indicate if
1657 * this is during the compilation phase (TRUE) or not (FALSE). */
1658
1659 const COP * const cop = (compiling) ? &PL_compiling : PL_curcop;
1660
1661 SV *categories = cop_hints_fetch_pvs(cop, "locale", 0);
1662 if (! categories || categories == &PL_sv_placeholder) {
1663 return FALSE;
1664 }
1665
1666 /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get
1667 * a valid unsigned */
1668 assert(category >= -1);
1669 return cBOOL(SvUV(categories) & (1U << (category + 1)));
1670}
1671
2c6ee1a7
KW
1672char *
1673Perl_my_strerror(pTHX_ const int errnum) {
1674
1675 /* Uses C locale for the error text unless within scope of 'use locale' for
1676 * LC_MESSAGES */
1677
1678#ifdef USE_LOCALE_MESSAGES
8ee4b769 1679 if (! IN_LC(LC_MESSAGES)) {
2c6ee1a7 1680 char * save_locale = setlocale(LC_MESSAGES, NULL);
a39edc4c 1681 if (! isNAME_C_OR_POSIX(save_locale)) {
2c6ee1a7
KW
1682 char *errstr;
1683
1684 /* The next setlocale likely will zap this, so create a copy */
1685 save_locale = savepv(save_locale);
1686
1687 setlocale(LC_MESSAGES, "C");
1688
1689 /* This points to the static space in Strerror, with all its
1690 * limitations */
1691 errstr = Strerror(errnum);
1692
1693 setlocale(LC_MESSAGES, save_locale);
1694 Safefree(save_locale);
1695 return errstr;
1696 }
1697 }
1698#endif
1699
1700 return Strerror(errnum);
1701}
1702
66610fdd 1703/*
747c467a
KW
1704
1705=head1 Locale-related functions and macros
1706
1707=for apidoc sync_locale
1708
1709Changing the program's locale should be avoided by XS code. Nevertheless,
1710certain non-Perl libraries called from XS, such as C<Gtk> do so. When this
1711happens, Perl needs to be told that the locale has changed. Use this function
1712to do so, before returning to Perl.
1713
1714=cut
1715*/
1716
1717void
1718Perl_sync_locale(pTHX)
1719{
1720
1721#ifdef USE_LOCALE_CTYPE
1722 new_ctype(setlocale(LC_CTYPE, NULL));
1723#endif /* USE_LOCALE_CTYPE */
1724
1725#ifdef USE_LOCALE_COLLATE
1726 new_collate(setlocale(LC_COLLATE, NULL));
1727#endif
1728
1729#ifdef USE_LOCALE_NUMERIC
1730 set_numeric_local(); /* Switch from "C" to underlying LC_NUMERIC */
1731 new_numeric(setlocale(LC_NUMERIC, NULL));
1732#endif /* USE_LOCALE_NUMERIC */
1733
1734}
1735
1736
1737
1738/*
66610fdd
RGS
1739 * Local variables:
1740 * c-indentation-style: bsd
1741 * c-basic-offset: 4
14d04a33 1742 * indent-tabs-mode: nil
66610fdd
RGS
1743 * End:
1744 *
14d04a33 1745 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1746 */