3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 * 2002, 2003, 2005, 2006, 2007, 2008 by Larry Wall and others
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.
12 * A Elbereth Gilthoniel,
13 * silivren penna míriel
14 * o menel aglar elenath!
15 * Na-chaered palan-díriel
16 * o galadhremmin ennorath,
17 * Fanuilos, le linnathon
18 * nef aear, si nef aearon!
20 * [p.238 of _The Lord of the Rings_, II/i: "Many Meetings"]
23 /* utility functions for handling locale-specific stuff like what
24 * character represents the decimal point.
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.
36 #define PERL_IN_LOCALE_C
40 # include <langinfo.h>
48 * Standardize the locale name from a string returned by 'setlocale', possibly
49 * modifying that string.
51 * The typical return value of setlocale() is either
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,
55 * in some unspecified order). This is not handled by this function.
57 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
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 '='
63 S_stdize_locale(pTHX_ char *locs)
65 const char * const s = strchr(locs, '=');
68 PERL_ARGS_ASSERT_STDIZE_LOCALE;
71 const char * const t = strchr(s, '.');
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);
85 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
93 Perl_set_numeric_radix(pTHX)
95 #ifdef USE_LOCALE_NUMERIC
96 # ifdef HAS_LOCALECONV
97 const struct lconv* const lc = localeconv();
99 if (lc && lc->decimal_point) {
100 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
101 SvREFCNT_dec(PL_numeric_radix_sv);
102 PL_numeric_radix_sv = NULL;
105 if (PL_numeric_radix_sv)
106 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
108 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
109 if (! is_ascii_string((U8 *) lc->decimal_point, 0)
110 && is_utf8_string((U8 *) lc->decimal_point, 0)
111 && _is_cur_LC_category_utf8(LC_NUMERIC))
113 SvUTF8_on(PL_numeric_radix_sv);
118 PL_numeric_radix_sv = NULL;
120 DEBUG_L(PerlIO_printf(Perl_debug_log, "Locale radix is %s\n",
121 (PL_numeric_radix_sv)
125 # endif /* HAS_LOCALECONV */
126 #endif /* USE_LOCALE_NUMERIC */
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 */
139 #define isNAME_C_OR_POSIX(name) ((name) != NULL \
140 && ((*(name) == 'C' && (*(name + 1)) == '\0') \
141 || strEQ((name), "POSIX")))
144 Perl_new_numeric(pTHX_ const char *newnum)
146 #ifdef USE_LOCALE_NUMERIC
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.
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.
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
161 * This sets several interpreter-level variables:
162 * PL_numeric_name The underlying locale's name: a copy of 'newnum'
163 * PL_numeric_local A boolean indicating if the toggled state is such
164 * that the current locale is the program's underlying
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
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
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() */
182 Safefree(PL_numeric_name);
183 PL_numeric_name = NULL;
184 PL_numeric_standard = TRUE;
185 PL_numeric_local = TRUE;
189 save_newnum = stdize_locale(savepv(newnum));
190 if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) {
191 Safefree(PL_numeric_name);
192 PL_numeric_name = save_newnum;
195 PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum);
196 PL_numeric_local = TRUE;
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();
206 PERL_UNUSED_ARG(newnum);
207 #endif /* USE_LOCALE_NUMERIC */
211 Perl_set_numeric_standard(pTHX)
213 #ifdef USE_LOCALE_NUMERIC
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) */
220 setlocale(LC_NUMERIC, "C");
221 PL_numeric_standard = TRUE;
222 PL_numeric_local = isNAME_C_OR_POSIX(PL_numeric_name);
224 DEBUG_L(PerlIO_printf(Perl_debug_log,
225 "Underlying LC_NUMERIC locale now is C\n"));
227 #endif /* USE_LOCALE_NUMERIC */
231 Perl_set_numeric_local(pTHX)
233 #ifdef USE_LOCALE_NUMERIC
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) */
240 setlocale(LC_NUMERIC, PL_numeric_name);
241 PL_numeric_standard = isNAME_C_OR_POSIX(PL_numeric_name);
242 PL_numeric_local = TRUE;
244 DEBUG_L(PerlIO_printf(Perl_debug_log,
245 "Underlying LC_NUMERIC locale now is %s\n",
248 #endif /* USE_LOCALE_NUMERIC */
252 * Set up for a new ctype locale.
255 Perl_new_ctype(pTHX_ const char *newctype)
257 #ifdef USE_LOCALE_CTYPE
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.
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,
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() */
273 PERL_ARGS_ASSERT_NEW_CTYPE;
275 PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE);
277 /* A UTF-8 locale gets standard rules. But note that code still has to
278 * handle this specially because of the three problematic code points */
279 if (PL_in_utf8_CTYPE_locale) {
280 Copy(PL_fold_latin1, PL_fold_locale, 256, U8);
283 /* Assume enough space for every character being bad. 4 spaces each
284 * for the 94 printable characters that are output like "'x' "; and 5
285 * spaces each for "'\\' ", "'\t' ", and "'\n' "; plus a terminating
287 char bad_chars_list[ (94 * 4) + (3 * 5) + 1 ];
289 bool check_for_problems = ckWARN_d(WARN_LOCALE); /* No warnings means
291 bool multi_byte_locale = FALSE; /* Assume is a single-byte locale
293 unsigned int bad_count = 0; /* Count of bad characters */
295 for (i = 0; i < 256; i++) {
296 if (isUPPER_LC((U8) i))
297 PL_fold_locale[i] = (U8) toLOWER_LC((U8) i);
298 else if (isLOWER_LC((U8) i))
299 PL_fold_locale[i] = (U8) toUPPER_LC((U8) i);
301 PL_fold_locale[i] = (U8) i;
303 /* If checking for locale problems, see if the native ASCII-range
304 * printables plus \n and \t are in their expected categories in
305 * the new locale. If not, this could mean big trouble, upending
306 * Perl's and most programs' assumptions, like having a
307 * metacharacter with special meaning become a \w. Fortunately,
308 * it's very rare to find locales that aren't supersets of ASCII
309 * nowadays. It isn't a problem for most controls to be changed
310 * into something else; we check only \n and \t, though perhaps \r
311 * could be an issue as well. */
312 if (check_for_problems
313 && (isGRAPH_A(i) || isBLANK_A(i) || i == '\n'))
315 if ((isALPHANUMERIC_A(i) && ! isALPHANUMERIC_LC(i))
316 || (isPUNCT_A(i) && ! isPUNCT_LC(i))
317 || (isBLANK_A(i) && ! isBLANK_LC(i))
318 || (i == '\n' && ! isCNTRL_LC(i)))
320 if (bad_count) { /* Separate multiple entries with a
322 bad_chars_list[bad_count++] = ' ';
324 bad_chars_list[bad_count++] = '\'';
326 bad_chars_list[bad_count++] = (char) i;
329 bad_chars_list[bad_count++] = '\\';
331 bad_chars_list[bad_count++] = 'n';
335 bad_chars_list[bad_count++] = 't';
338 bad_chars_list[bad_count++] = '\'';
339 bad_chars_list[bad_count] = '\0';
345 /* We only handle single-byte locales (outside of UTF-8 ones; so if
346 * this locale requires than one byte, there are going to be
348 if (check_for_problems && MB_CUR_MAX > 1) {
349 multi_byte_locale = TRUE;
353 if (bad_count || multi_byte_locale) {
354 setlocale(LC_CTYPE, "C");
355 Perl_warner(aTHX_ packWARN(WARN_LOCALE),
356 "Locale '%s' may not work well.%s%s%s\n",
359 ? " Some characters in it are not recognized by"
363 ? "\nThe following characters (and maybe others)"
364 " may not have the same meaning as the Perl"
365 " program expects:\n"
371 setlocale(LC_CTYPE, newctype);
375 #endif /* USE_LOCALE_CTYPE */
376 PERL_ARGS_ASSERT_NEW_CTYPE;
377 PERL_UNUSED_ARG(newctype);
382 Perl_new_collate(pTHX_ const char *newcoll)
384 #ifdef USE_LOCALE_COLLATE
386 /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell
387 * core Perl this and that 'newcoll' is the name of the new locale.
389 * Any code changing the locale (outside this file) should use
390 * POSIX::setlocale, which calls this function. Therefore this function
391 * should be called directly only from this file and from
392 * POSIX::setlocale() */
395 if (PL_collation_name) {
397 Safefree(PL_collation_name);
398 PL_collation_name = NULL;
400 PL_collation_standard = TRUE;
401 PL_collxfrm_base = 0;
402 PL_collxfrm_mult = 2;
406 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
408 Safefree(PL_collation_name);
409 PL_collation_name = stdize_locale(savepv(newcoll));
410 PL_collation_standard = isNAME_C_OR_POSIX(newcoll);
413 /* 2: at most so many chars ('a', 'b'). */
414 /* 50: surely no system expands a char more. */
415 #define XFRMBUFSIZE (2 * 50)
416 char xbuf[XFRMBUFSIZE];
417 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
418 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
419 const SSize_t mult = fb - fa;
420 if (mult < 1 && !(fa == 0 && fb == 0))
421 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
423 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
424 PL_collxfrm_mult = mult;
429 PERL_UNUSED_ARG(newcoll);
430 #endif /* USE_LOCALE_COLLATE */
436 Perl_my_setlocale(pTHX_ int category, const char* locale)
438 /* This, for Windows, emulates POSIX setlocale() behavior. There is no
439 * difference unless the input locale is "", which means on Windows to get
440 * the machine default, which is set via the computer's "Regional and
441 * Language Options" (or its current equivalent). In POSIX, it instead
442 * means to find the locale from the user's environment. This routine
443 * looks in the environment, and, if anything is found, uses that instead
444 * of going to the machine default. If there is no environment override,
445 * the machine default is used, as normal, by calling the real setlocale()
446 * with "". The POSIX behavior is to use the LC_ALL variable if set;
447 * otherwise to use the particular category's variable if set; otherwise to
448 * use the LANG variable. */
450 bool override_LC_ALL = 0;
453 if (locale && strEQ(locale, "")) {
455 locale = PerlEnv_getenv("LC_ALL");
461 override_LC_ALL = TRUE;
462 break; /* We already know its variable isn't set */
464 # ifdef USE_LOCALE_TIME
466 locale = PerlEnv_getenv("LC_TIME");
469 # ifdef USE_LOCALE_CTYPE
471 locale = PerlEnv_getenv("LC_CTYPE");
474 # ifdef USE_LOCALE_COLLATE
476 locale = PerlEnv_getenv("LC_COLLATE");
479 # ifdef USE_LOCALE_MONETARY
481 locale = PerlEnv_getenv("LC_MONETARY");
484 # ifdef USE_LOCALE_NUMERIC
486 locale = PerlEnv_getenv("LC_NUMERIC");
489 # ifdef USE_LOCALE_MESSAGES
491 locale = PerlEnv_getenv("LC_MESSAGES");
495 /* This is a category, like PAPER_SIZE that we don't
496 * know about; and so can't provide a wrapper. */
500 locale = PerlEnv_getenv("LANG");
510 result = setlocale(category, locale);
512 if (! override_LC_ALL) {
516 /* Here the input locale was LC_ALL, and we have set it to what is in the
517 * LANG variable or the system default if there is no LANG. But these have
518 * lower priority than the other LC_foo variables, so override it for each
519 * one that is set. (If they are set to "", it means to use the same thing
520 * we just set LC_ALL to, so can skip) */
521 # ifdef USE_LOCALE_TIME
522 result = PerlEnv_getenv("LC_TIME");
523 if (result && strNE(result, "")) {
524 setlocale(LC_TIME, result);
527 # ifdef USE_LOCALE_CTYPE
528 result = PerlEnv_getenv("LC_CTYPE");
529 if (result && strNE(result, "")) {
530 setlocale(LC_CTYPE, result);
533 # ifdef USE_LOCALE_COLLATE
534 result = PerlEnv_getenv("LC_COLLATE");
535 if (result && strNE(result, "")) {
536 setlocale(LC_COLLATE, result);
539 # ifdef USE_LOCALE_MONETARY
540 result = PerlEnv_getenv("LC_MONETARY");
541 if (result && strNE(result, "")) {
542 setlocale(LC_MONETARY, result);
545 # ifdef USE_LOCALE_NUMERIC
546 result = PerlEnv_getenv("LC_NUMERIC");
547 if (result && strNE(result, "")) {
548 setlocale(LC_NUMERIC, result);
551 # ifdef USE_LOCALE_MESSAGES
552 result = PerlEnv_getenv("LC_MESSAGES");
553 if (result && strNE(result, "")) {
554 setlocale(LC_MESSAGES, result);
558 return setlocale(LC_ALL, NULL);
566 * Initialize locale awareness.
569 Perl_init_i18nl10n(pTHX_ int printwarn)
573 * 0 if not to output warning when setup locale is bad
574 * 1 if to output warning based on value of PERL_BADLANG
575 * >1 if to output regardless of PERL_BADLANG
578 * 1 = set ok or not applicable,
579 * 0 = fallback to a locale of lower priority
580 * -1 = fallback to all locales failed, not even to the C locale
585 #if defined(USE_LOCALE)
586 #ifdef USE_LOCALE_CTYPE
587 char *curctype = NULL;
588 #endif /* USE_LOCALE_CTYPE */
589 #ifdef USE_LOCALE_COLLATE
590 char *curcoll = NULL;
591 #endif /* USE_LOCALE_COLLATE */
592 #ifdef USE_LOCALE_NUMERIC
594 #endif /* USE_LOCALE_NUMERIC */
596 char * const language = PerlEnv_getenv("LANGUAGE");
599 /* NULL uses the existing already set up locale */
600 const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT"))
603 const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */
604 unsigned int trial_locales_count;
605 char * const lc_all = PerlEnv_getenv("LC_ALL");
606 char * const lang = PerlEnv_getenv("LANG");
607 bool setlocale_failure = FALSE;
610 const bool locwarn = (printwarn > 1 ||
612 (!(p = PerlEnv_getenv("PERL_BADLANG")) ||
613 grok_atou(p, NULL))));
616 /* In some systems you can find out the system default locale
617 * and use that as the fallback locale. */
618 # define SYSTEM_DEFAULT_LOCALE
620 #ifdef SYSTEM_DEFAULT_LOCALE
621 const char *system_default_locale = NULL;
624 #ifndef LOCALE_ENVIRON_REQUIRED
625 PERL_UNUSED_VAR(done);
629 * Ultrix setlocale(..., "") fails if there are no environment
630 * variables from which to get a locale name.
635 if (my_setlocale(LC_ALL, setlocale_init))
638 setlocale_failure = TRUE;
640 if (!setlocale_failure) {
641 # ifdef USE_LOCALE_CTYPE
644 my_setlocale(LC_CTYPE,
645 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
646 ? setlocale_init : NULL)))
647 setlocale_failure = TRUE;
649 curctype = savepv(curctype);
650 # endif /* USE_LOCALE_CTYPE */
651 # ifdef USE_LOCALE_COLLATE
654 my_setlocale(LC_COLLATE,
655 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
656 ? setlocale_init : NULL)))
657 setlocale_failure = TRUE;
659 curcoll = savepv(curcoll);
660 # endif /* USE_LOCALE_COLLATE */
661 # ifdef USE_LOCALE_NUMERIC
664 my_setlocale(LC_NUMERIC,
665 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
666 ? setlocale_init : NULL)))
667 setlocale_failure = TRUE;
669 curnum = savepv(curnum);
670 # endif /* USE_LOCALE_NUMERIC */
671 # ifdef USE_LOCALE_MESSAGES
672 if (! my_setlocale(LC_MESSAGES,
673 (!done && (lang || PerlEnv_getenv("LC_MESSAGES")))
674 ? setlocale_init : NULL))
676 setlocale_failure = TRUE;
678 # endif /* USE_LOCALE_MESSAGES */
679 # ifdef USE_LOCALE_MONETARY
680 if (! my_setlocale(LC_MONETARY,
681 (!done && (lang || PerlEnv_getenv("LC_MONETARY")))
682 ? setlocale_init : NULL))
684 setlocale_failure = TRUE;
686 # endif /* USE_LOCALE_MONETARY */
691 #endif /* !LOCALE_ENVIRON_REQUIRED */
693 /* We try each locale in the list until we get one that works, or exhaust
695 trial_locales[0] = setlocale_init;
696 trial_locales_count = 1;
697 for (i= 0; i < trial_locales_count; i++) {
698 const char * trial_locale = trial_locales[i];
702 /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED
703 * when i==0, but I (khw) don't think that behavior makes much
705 setlocale_failure = FALSE;
707 #ifdef SYSTEM_DEFAULT_LOCALE
709 /* On Windows machines, an entry of "" after the 0th means to use
710 * the system default locale, which we now proceed to get. */
711 if (strEQ(trial_locale, "")) {
714 /* Note that this may change the locale, but we are going to do
715 * that anyway just below */
716 system_default_locale = setlocale(LC_ALL, "");
718 /* Skip if invalid or it's already on the list of locales to
720 if (! system_default_locale) {
723 for (j = 0; j < trial_locales_count; j++) {
724 if (strEQ(system_default_locale, trial_locales[j])) {
729 trial_locale = system_default_locale;
732 #endif /* SYSTEM_DEFAULT_LOCALE */
736 if (! my_setlocale(LC_ALL, trial_locale)) {
737 setlocale_failure = TRUE;
740 /* Since LC_ALL succeeded, it should have changed all the other
741 * categories it can to its value; so we massage things so that the
742 * setlocales below just return their category's current values.
743 * This adequately handles the case in NetBSD where LC_COLLATE may
744 * not be defined for a locale, and setting it individually will
745 * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to
746 * the POSIX locale. */
751 if (!setlocale_failure) {
752 #ifdef USE_LOCALE_CTYPE
754 if (! (curctype = my_setlocale(LC_CTYPE, trial_locale)))
755 setlocale_failure = TRUE;
757 curctype = savepv(curctype);
758 #endif /* USE_LOCALE_CTYPE */
759 #ifdef USE_LOCALE_COLLATE
761 if (! (curcoll = my_setlocale(LC_COLLATE, trial_locale)))
762 setlocale_failure = TRUE;
764 curcoll = savepv(curcoll);
765 #endif /* USE_LOCALE_COLLATE */
766 #ifdef USE_LOCALE_NUMERIC
768 if (! (curnum = my_setlocale(LC_NUMERIC, trial_locale)))
769 setlocale_failure = TRUE;
771 curnum = savepv(curnum);
772 #endif /* USE_LOCALE_NUMERIC */
773 #ifdef USE_LOCALE_MESSAGES
774 if (! (my_setlocale(LC_MESSAGES, trial_locale)))
775 setlocale_failure = TRUE;
776 #endif /* USE_LOCALE_MESSAGES */
777 #ifdef USE_LOCALE_MONETARY
778 if (! (my_setlocale(LC_MONETARY, trial_locale)))
779 setlocale_failure = TRUE;
780 #endif /* USE_LOCALE_MONETARY */
782 if (! setlocale_failure) { /* Success */
787 /* Here, something failed; will need to try a fallback. */
793 if (locwarn) { /* Output failure info only on the first one */
796 PerlIO_printf(Perl_error_log,
797 "perl: warning: Setting locale failed.\n");
801 PerlIO_printf(Perl_error_log,
802 "perl: warning: Setting locale failed for the categories:\n\t");
803 #ifdef USE_LOCALE_CTYPE
805 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
806 #endif /* USE_LOCALE_CTYPE */
807 #ifdef USE_LOCALE_COLLATE
809 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
810 #endif /* USE_LOCALE_COLLATE */
811 #ifdef USE_LOCALE_NUMERIC
813 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
814 #endif /* USE_LOCALE_NUMERIC */
815 PerlIO_printf(Perl_error_log, "and possibly others\n");
819 PerlIO_printf(Perl_error_log,
820 "perl: warning: Please check that your locale settings:\n");
823 PerlIO_printf(Perl_error_log,
824 "\tLANGUAGE = %c%s%c,\n",
825 language ? '"' : '(',
826 language ? language : "unset",
827 language ? '"' : ')');
830 PerlIO_printf(Perl_error_log,
831 "\tLC_ALL = %c%s%c,\n",
833 lc_all ? lc_all : "unset",
836 #if defined(USE_ENVIRON_ARRAY)
839 for (e = environ; *e; e++) {
840 if (strnEQ(*e, "LC_", 3)
841 && strnNE(*e, "LC_ALL=", 7)
842 && (p = strchr(*e, '=')))
843 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
844 (int)(p - *e), *e, p + 1);
848 PerlIO_printf(Perl_error_log,
849 "\t(possibly more locale environment variables)\n");
852 PerlIO_printf(Perl_error_log,
855 lang ? lang : "unset",
858 PerlIO_printf(Perl_error_log,
859 " are supported and installed on your system.\n");
862 /* Calculate what fallback locales to try. We have avoided this
863 * until we have to, becuase failure is quite unlikely. This will
864 * usually change the upper bound of the loop we are in.
866 * Since the system's default way of setting the locale has not
867 * found one that works, We use Perl's defined ordering: LC_ALL,
868 * LANG, and the C locale. We don't try the same locale twice, so
869 * don't add to the list if already there. (On POSIX systems, the
870 * LC_ALL element will likely be a repeat of the 0th element "",
871 * but there's no harm done by doing it explicitly */
873 for (j = 0; j < trial_locales_count; j++) {
874 if (strEQ(lc_all, trial_locales[j])) {
878 trial_locales[trial_locales_count++] = lc_all;
883 for (j = 0; j < trial_locales_count; j++) {
884 if (strEQ(lang, trial_locales[j])) {
888 trial_locales[trial_locales_count++] = lang;
892 #if defined(WIN32) && defined(LC_ALL)
893 /* For Windows, we also try the system default locale before "C".
894 * (If there exists a Windows without LC_ALL we skip this because
895 * it gets too complicated. For those, the "C" is the next
896 * fallback possibility). The "" is the same as the 0th element of
897 * the array, but the code at the loop above knows to treat it
898 * differently when not the 0th */
899 trial_locales[trial_locales_count++] = "";
902 for (j = 0; j < trial_locales_count; j++) {
903 if (strEQ("C", trial_locales[j])) {
907 trial_locales[trial_locales_count++] = "C";
910 } /* end of first time through the loop */
916 } /* end of looping through the trial locales */
918 if (ok < 1) { /* If we tried to fallback */
920 if (! setlocale_failure) { /* fallback succeeded */
921 msg = "Falling back to";
923 else { /* fallback failed */
925 /* We dropped off the end of the loop, so have to decrement i to
926 * get back to the value the last time through */
930 msg = "Failed to fall back to";
932 /* To continue, we should use whatever values we've got */
933 #ifdef USE_LOCALE_CTYPE
935 curctype = savepv(setlocale(LC_CTYPE, NULL));
936 #endif /* USE_LOCALE_CTYPE */
937 #ifdef USE_LOCALE_COLLATE
939 curcoll = savepv(setlocale(LC_COLLATE, NULL));
940 #endif /* USE_LOCALE_COLLATE */
941 #ifdef USE_LOCALE_NUMERIC
943 curnum = savepv(setlocale(LC_NUMERIC, NULL));
944 #endif /* USE_LOCALE_NUMERIC */
948 const char * description;
949 const char * name = "";
950 if (strEQ(trial_locales[i], "C")) {
951 description = "the standard locale";
954 #ifdef SYSTEM_DEFAULT_LOCALE
955 else if (strEQ(trial_locales[i], "")) {
956 description = "the system default locale";
957 if (system_default_locale) {
958 name = system_default_locale;
961 #endif /* SYSTEM_DEFAULT_LOCALE */
963 description = "a fallback locale";
964 name = trial_locales[i];
966 if (name && strNE(name, "")) {
967 PerlIO_printf(Perl_error_log,
968 "perl: warning: %s %s (\"%s\").\n", msg, description, name);
971 PerlIO_printf(Perl_error_log,
972 "perl: warning: %s %s.\n", msg, description);
975 } /* End of tried to fallback */
977 #ifdef USE_LOCALE_CTYPE
979 #endif /* USE_LOCALE_CTYPE */
981 #ifdef USE_LOCALE_COLLATE
982 new_collate(curcoll);
983 #endif /* USE_LOCALE_COLLATE */
985 #ifdef USE_LOCALE_NUMERIC
987 #endif /* USE_LOCALE_NUMERIC */
989 #if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE)
990 /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE
991 * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by
992 * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the
993 * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open
995 PL_utf8locale = _is_cur_LC_category_utf8(LC_CTYPE);
997 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
998 This is an alternative to using the -C command line switch
999 (the -C if present will override this). */
1001 const char *p = PerlEnv_getenv("PERL_UNICODE");
1002 PL_unicode = p ? parse_unicode_opts(&p) : 0;
1003 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
1008 #ifdef USE_LOCALE_CTYPE
1010 #endif /* USE_LOCALE_CTYPE */
1011 #ifdef USE_LOCALE_COLLATE
1013 #endif /* USE_LOCALE_COLLATE */
1014 #ifdef USE_LOCALE_NUMERIC
1016 #endif /* USE_LOCALE_NUMERIC */
1018 #else /* !USE_LOCALE */
1019 PERL_UNUSED_ARG(printwarn);
1020 #endif /* USE_LOCALE */
1026 #ifdef USE_LOCALE_COLLATE
1029 * mem_collxfrm() is a bit like strxfrm() but with two important
1030 * differences. First, it handles embedded NULs. Second, it allocates
1031 * a bit more memory than needed for the transformed data itself.
1032 * The real transformed data begins at offset sizeof(collationix).
1033 * Please see sv_collxfrm() to see how this is used.
1037 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
1040 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
1042 PERL_ARGS_ASSERT_MEM_COLLXFRM;
1044 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
1045 /* the +1 is for the terminating NUL. */
1047 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
1048 Newx(xbuf, xAlloc, char);
1052 *(U32*)xbuf = PL_collation_ix;
1053 xout = sizeof(PL_collation_ix);
1054 for (xin = 0; xin < len; ) {
1058 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
1059 if (xused >= PERL_INT_MAX)
1061 if ((STRLEN)xused < xAlloc - xout)
1063 xAlloc = (2 * xAlloc) + 1;
1064 Renew(xbuf, xAlloc, char);
1069 xin += strlen(s + xin) + 1;
1072 /* Embedded NULs are understood but silently skipped
1073 * because they make no sense in locale collation. */
1077 *xlen = xout - sizeof(PL_collation_ix);
1086 #endif /* USE_LOCALE_COLLATE */
1091 Perl__is_cur_LC_category_utf8(pTHX_ int category)
1093 /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
1094 * otherwise. 'category' may not be LC_ALL. If the platform doesn't have
1095 * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence
1096 * could give the wrong result. The result will very likely be correct for
1097 * languages that have commonly used non-ASCII characters, but for notably
1098 * English, it comes down to if the locale's name ends in something like
1099 * "UTF-8". It errs on the side of not being a UTF-8 locale. */
1101 char *save_input_locale = NULL;
1105 assert(category != LC_ALL);
1108 /* First dispose of the trivial cases */
1109 save_input_locale = setlocale(category, NULL);
1110 if (! save_input_locale) {
1111 DEBUG_L(PerlIO_printf(Perl_debug_log,
1112 "Could not find current locale for category %d\n",
1114 return FALSE; /* XXX maybe should croak */
1116 save_input_locale = stdize_locale(savepv(save_input_locale));
1117 if (isNAME_C_OR_POSIX(save_input_locale)) {
1118 DEBUG_L(PerlIO_printf(Perl_debug_log,
1119 "Current locale for category %d is %s\n",
1120 category, save_input_locale));
1121 Safefree(save_input_locale);
1125 #if defined(USE_LOCALE_CTYPE) \
1126 && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET)))
1128 { /* Next try nl_langinfo or MB_CUR_MAX if available */
1130 char *save_ctype_locale = NULL;
1133 if (category != LC_CTYPE) { /* These work only on LC_CTYPE */
1135 /* Get the current LC_CTYPE locale */
1136 save_ctype_locale = setlocale(LC_CTYPE, NULL);
1137 if (! save_ctype_locale) {
1138 DEBUG_L(PerlIO_printf(Perl_debug_log,
1139 "Could not find current locale for LC_CTYPE\n"));
1140 goto cant_use_nllanginfo;
1142 save_ctype_locale = stdize_locale(savepv(save_ctype_locale));
1144 /* If LC_CTYPE and the desired category use the same locale, this
1145 * means that finding the value for LC_CTYPE is the same as finding
1146 * the value for the desired category. Otherwise, switch LC_CTYPE
1147 * to the desired category's locale */
1148 if (strEQ(save_ctype_locale, save_input_locale)) {
1149 Safefree(save_ctype_locale);
1150 save_ctype_locale = NULL;
1152 else if (! setlocale(LC_CTYPE, save_input_locale)) {
1153 DEBUG_L(PerlIO_printf(Perl_debug_log,
1154 "Could not change LC_CTYPE locale to %s\n",
1155 save_input_locale));
1156 Safefree(save_ctype_locale);
1157 goto cant_use_nllanginfo;
1161 DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n",
1162 save_input_locale));
1164 /* Here the current LC_CTYPE is set to the locale of the category whose
1165 * information is desired. This means that nl_langinfo() and MB_CUR_MAX
1166 * should give the correct results */
1168 # if defined(HAS_NL_LANGINFO) && defined(CODESET)
1170 char *codeset = nl_langinfo(CODESET);
1171 if (codeset && strNE(codeset, "")) {
1172 codeset = savepv(codeset);
1174 /* If we switched LC_CTYPE, switch back */
1175 if (save_ctype_locale) {
1176 setlocale(LC_CTYPE, save_ctype_locale);
1177 Safefree(save_ctype_locale);
1180 is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8"))
1181 || foldEQ(codeset, STR_WITH_LEN("UTF8"));
1183 DEBUG_L(PerlIO_printf(Perl_debug_log,
1184 "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n",
1187 Safefree(save_input_locale);
1195 /* Here, either we don't have nl_langinfo, or it didn't return a
1196 * codeset. Try MB_CUR_MAX */
1198 /* Standard UTF-8 needs at least 4 bytes to represent the maximum
1199 * Unicode code point. Since UTF-8 is the only non-single byte
1200 * encoding we handle, we just say any such encoding is UTF-8, and if
1201 * turns out to be wrong, other things will fail */
1202 is_utf8 = MB_CUR_MAX >= 4;
1204 DEBUG_L(PerlIO_printf(Perl_debug_log,
1205 "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n",
1206 (int) MB_CUR_MAX, is_utf8));
1208 Safefree(save_input_locale);
1212 /* ... But, most system that have MB_CUR_MAX will also have mbtowc(),
1213 * since they are both in the C99 standard. We can feed a known byte
1214 * string to the latter function, and check that it gives the expected
1218 PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */
1220 if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
1221 != strlen(HYPHEN_UTF8)
1222 || wc != (wchar_t) 0x2010)
1225 DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", (unsigned int)wc));
1226 DEBUG_L(PerlIO_printf(Perl_debug_log,
1227 "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n",
1228 mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno));
1233 /* If we switched LC_CTYPE, switch back */
1234 if (save_ctype_locale) {
1235 setlocale(LC_CTYPE, save_ctype_locale);
1236 Safefree(save_ctype_locale);
1243 cant_use_nllanginfo:
1245 #else /* nl_langinfo should work if available, so don't bother compiling this
1246 fallback code. The final fallback of looking at the name is
1247 compiled, and will be executed if nl_langinfo fails */
1249 /* nl_langinfo not available or failed somehow. Next try looking at the
1250 * currency symbol to see if it disambiguates things. Often that will be
1251 * in the native script, and if the symbol isn't in UTF-8, we know that the
1252 * locale isn't. If it is non-ASCII UTF-8, we infer that the locale is
1253 * too, as the odds of a non-UTF8 string being valid UTF-8 are quite small
1256 #ifdef HAS_LOCALECONV
1257 # ifdef USE_LOCALE_MONETARY
1259 char *save_monetary_locale = NULL;
1260 bool only_ascii = FALSE;
1261 bool is_utf8 = FALSE;
1264 /* Like above for LC_CTYPE, we first set LC_MONETARY to the locale of
1265 * the desired category, if it isn't that locale already */
1267 if (category != LC_MONETARY) {
1269 save_monetary_locale = setlocale(LC_MONETARY, NULL);
1270 if (! save_monetary_locale) {
1271 DEBUG_L(PerlIO_printf(Perl_debug_log,
1272 "Could not find current locale for LC_MONETARY\n"));
1273 goto cant_use_monetary;
1275 save_monetary_locale = stdize_locale(savepv(save_monetary_locale));
1277 if (strEQ(save_monetary_locale, save_input_locale)) {
1278 Safefree(save_monetary_locale);
1279 save_monetary_locale = NULL;
1281 else if (! setlocale(LC_MONETARY, save_input_locale)) {
1282 DEBUG_L(PerlIO_printf(Perl_debug_log,
1283 "Could not change LC_MONETARY locale to %s\n",
1284 save_input_locale));
1285 Safefree(save_monetary_locale);
1286 goto cant_use_monetary;
1290 /* Here the current LC_MONETARY is set to the locale of the category
1291 * whose information is desired. */
1295 || ! lc->currency_symbol
1296 || is_ascii_string((U8 *) lc->currency_symbol, 0))
1298 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));
1302 is_utf8 = is_utf8_string((U8 *) lc->currency_symbol, 0);
1305 /* If we changed it, restore LC_MONETARY to its original locale */
1306 if (save_monetary_locale) {
1307 setlocale(LC_MONETARY, save_monetary_locale);
1308 Safefree(save_monetary_locale);
1313 /* It isn't a UTF-8 locale if the symbol is not legal UTF-8;
1314 * otherwise assume the locale is UTF-8 if and only if the symbol
1315 * is non-ascii UTF-8. */
1316 DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?Currency symbol for %s is UTF-8=%d\n",
1317 save_input_locale, is_utf8));
1318 Safefree(save_input_locale);
1324 # endif /* USE_LOCALE_MONETARY */
1325 #endif /* HAS_LOCALECONV */
1327 #if defined(HAS_STRFTIME) && defined(USE_LOCALE_TIME)
1329 /* Still haven't found a non-ASCII string to disambiguate UTF-8 or not. Try
1330 * the names of the months and weekdays, timezone, and am/pm indicator */
1332 char *save_time_locale = NULL;
1334 bool is_dst = FALSE;
1338 char * formatted_time;
1341 /* Like above for LC_MONETARY, we set LC_TIME to the locale of the
1342 * desired category, if it isn't that locale already */
1344 if (category != LC_TIME) {
1346 save_time_locale = setlocale(LC_TIME, NULL);
1347 if (! save_time_locale) {
1348 DEBUG_L(PerlIO_printf(Perl_debug_log,
1349 "Could not find current locale for LC_TIME\n"));
1352 save_time_locale = stdize_locale(savepv(save_time_locale));
1354 if (strEQ(save_time_locale, save_input_locale)) {
1355 Safefree(save_time_locale);
1356 save_time_locale = NULL;
1358 else if (! setlocale(LC_TIME, save_input_locale)) {
1359 DEBUG_L(PerlIO_printf(Perl_debug_log,
1360 "Could not change LC_TIME locale to %s\n",
1361 save_input_locale));
1362 Safefree(save_time_locale);
1367 /* Here the current LC_TIME is set to the locale of the category
1368 * whose information is desired. Look at all the days of the week and
1369 * month names, and the timezone and am/pm indicator for non-ASCII
1370 * characters. The first such a one found will tell us if the locale
1371 * is UTF-8 or not */
1373 for (i = 0; i < 7 + 12; i++) { /* 7 days; 12 months */
1374 formatted_time = my_strftime("%A %B %Z %p",
1375 0, 0, hour, dom, month, 112, 0, 0, is_dst);
1376 if (! formatted_time || is_ascii_string((U8 *) formatted_time, 0)) {
1378 /* Here, we didn't find a non-ASCII. Try the next time through
1379 * with the complemented dst and am/pm, and try with the next
1380 * weekday. After we have gotten all weekdays, try the next
1383 hour = (hour + 12) % 24;
1391 /* Here, we have a non-ASCII. Return TRUE is it is valid UTF8;
1392 * false otherwise. But first, restore LC_TIME to its original
1393 * locale if we changed it */
1394 if (save_time_locale) {
1395 setlocale(LC_TIME, save_time_locale);
1396 Safefree(save_time_locale);
1399 DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?time-related strings for %s are UTF-8=%d\n",
1401 is_utf8_string((U8 *) formatted_time, 0)));
1402 Safefree(save_input_locale);
1403 return is_utf8_string((U8 *) formatted_time, 0);
1406 /* Falling off the end of the loop indicates all the names were just
1407 * ASCII. Go on to the next test. If we changed it, restore LC_TIME
1408 * to its original locale */
1409 if (save_time_locale) {
1410 setlocale(LC_TIME, save_time_locale);
1411 Safefree(save_time_locale);
1413 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));
1419 #if 0 && defined(USE_LOCALE_MESSAGES) && defined(HAS_SYS_ERRLIST)
1421 /* This code is ifdefd out because it was found to not be necessary in testing
1422 * on our dromedary test machine, which has over 700 locales. There, this
1423 * added no value to looking at the currency symbol and the time strings. I
1424 * left it in so as to avoid rewriting it if real-world experience indicates
1425 * that dromedary is an outlier. Essentially, instead of returning abpve if we
1426 * haven't found illegal utf8, we continue on and examine all the strerror()
1427 * messages on the platform for utf8ness. If all are ASCII, we still don't
1428 * know the answer; but otherwise we have a pretty good indication of the
1429 * utf8ness. The reason this doesn't help much is that the messages may not
1430 * have been translated into the locale. The currency symbol and time strings
1431 * are much more likely to have been translated. */
1434 bool is_utf8 = FALSE;
1435 bool non_ascii = FALSE;
1436 char *save_messages_locale = NULL;
1437 const char * errmsg = NULL;
1439 /* Like above, we set LC_MESSAGES to the locale of the desired
1440 * category, if it isn't that locale already */
1442 if (category != LC_MESSAGES) {
1444 save_messages_locale = setlocale(LC_MESSAGES, NULL);
1445 if (! save_messages_locale) {
1446 DEBUG_L(PerlIO_printf(Perl_debug_log,
1447 "Could not find current locale for LC_MESSAGES\n"));
1448 goto cant_use_messages;
1450 save_messages_locale = stdize_locale(savepv(save_messages_locale));
1452 if (strEQ(save_messages_locale, save_input_locale)) {
1453 Safefree(save_messages_locale);
1454 save_messages_locale = NULL;
1456 else if (! setlocale(LC_MESSAGES, save_input_locale)) {
1457 DEBUG_L(PerlIO_printf(Perl_debug_log,
1458 "Could not change LC_MESSAGES locale to %s\n",
1459 save_input_locale));
1460 Safefree(save_messages_locale);
1461 goto cant_use_messages;
1465 /* Here the current LC_MESSAGES is set to the locale of the category
1466 * whose information is desired. Look through all the messages. We
1467 * can't use Strerror() here because it may expand to code that
1468 * segfaults in miniperl */
1470 for (e = 0; e <= sys_nerr; e++) {
1472 errmsg = sys_errlist[e];
1473 if (errno || !errmsg) {
1476 errmsg = savepv(errmsg);
1477 if (! is_ascii_string((U8 *) errmsg, 0)) {
1479 is_utf8 = is_utf8_string((U8 *) errmsg, 0);
1485 /* And, if we changed it, restore LC_MESSAGES to its original locale */
1486 if (save_messages_locale) {
1487 setlocale(LC_MESSAGES, save_messages_locale);
1488 Safefree(save_messages_locale);
1493 /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid,
1494 * any non-ascii means it is one; otherwise we assume it isn't */
1495 DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?error messages for %s are UTF-8=%d\n",
1498 Safefree(save_input_locale);
1502 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));
1508 #endif /* the code that is compiled when no nl_langinfo */
1510 #ifndef EBCDIC /* On os390, even if the name ends with "UTF-8', it isn't a
1512 /* As a last resort, look at the locale name to see if it matches
1513 * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the
1514 * return of setlocale(), is actually defined to be opaque, so we can't
1515 * really rely on the absence of various substrings in the name to indicate
1516 * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to
1517 * be a UTF-8 locale. Similarly for the other common names */
1519 final_pos = strlen(save_input_locale) - 1;
1520 if (final_pos >= 3) {
1521 char *name = save_input_locale;
1523 /* Find next 'U' or 'u' and look from there */
1524 while ((name += strcspn(name, "Uu") + 1)
1525 <= save_input_locale + final_pos - 2)
1527 if (!isALPHA_FOLD_NE(*name, 't')
1528 || isALPHA_FOLD_NE(*(name + 1), 'f'))
1533 if (*(name) == '-') {
1534 if ((name > save_input_locale + final_pos - 1)) {
1539 if (*(name) == '8') {
1540 DEBUG_L(PerlIO_printf(Perl_debug_log,
1541 "Locale %s ends with UTF-8 in name\n",
1542 save_input_locale));
1543 Safefree(save_input_locale);
1547 DEBUG_L(PerlIO_printf(Perl_debug_log,
1548 "Locale %s doesn't end with UTF-8 in name\n",
1549 save_input_locale));
1554 /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
1556 && *(save_input_locale + final_pos - 0) == '1'
1557 && *(save_input_locale + final_pos - 1) == '0'
1558 && *(save_input_locale + final_pos - 2) == '0'
1559 && *(save_input_locale + final_pos - 3) == '5'
1560 && *(save_input_locale + final_pos - 4) == '6')
1562 DEBUG_L(PerlIO_printf(Perl_debug_log,
1563 "Locale %s ends with 10056 in name, is UTF-8 locale\n",
1564 save_input_locale));
1565 Safefree(save_input_locale);
1570 /* Other common encodings are the ISO 8859 series, which aren't UTF-8. But
1571 * since we are about to return FALSE anyway, there is no point in doing
1572 * this extra work */
1574 if (instr(save_input_locale, "8859")) {
1575 DEBUG_L(PerlIO_printf(Perl_debug_log,
1576 "Locale %s has 8859 in name, not UTF-8 locale\n",
1577 save_input_locale));
1578 Safefree(save_input_locale);
1583 DEBUG_L(PerlIO_printf(Perl_debug_log,
1584 "Assuming locale %s is not a UTF-8 locale\n",
1585 save_input_locale));
1586 Safefree(save_input_locale);
1594 Perl__is_in_locale_category(pTHX_ const bool compiling, const int category)
1597 /* Internal function which returns if we are in the scope of a pragma that
1598 * enables the locale category 'category'. 'compiling' should indicate if
1599 * this is during the compilation phase (TRUE) or not (FALSE). */
1601 const COP * const cop = (compiling) ? &PL_compiling : PL_curcop;
1603 SV *categories = cop_hints_fetch_pvs(cop, "locale", 0);
1604 if (! categories || categories == &PL_sv_placeholder) {
1608 /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get
1609 * a valid unsigned */
1610 assert(category >= -1);
1611 return cBOOL(SvUV(categories) & (1U << (category + 1)));
1615 Perl_my_strerror(pTHX_ const int errnum) {
1617 /* Uses C locale for the error text unless within scope of 'use locale' for
1620 #ifdef USE_LOCALE_MESSAGES
1621 if (! IN_LC(LC_MESSAGES)) {
1622 char * save_locale = setlocale(LC_MESSAGES, NULL);
1623 if (! isNAME_C_OR_POSIX(save_locale)) {
1626 /* The next setlocale likely will zap this, so create a copy */
1627 save_locale = savepv(save_locale);
1629 setlocale(LC_MESSAGES, "C");
1631 /* This points to the static space in Strerror, with all its
1633 errstr = Strerror(errnum);
1635 setlocale(LC_MESSAGES, save_locale);
1636 Safefree(save_locale);
1642 return Strerror(errnum);
1647 =head1 Locale-related functions and macros
1649 =for apidoc sync_locale
1651 Changing the program's locale should be avoided by XS code. Nevertheless,
1652 certain non-Perl libraries called from XS, such as C<Gtk> do so. When this
1653 happens, Perl needs to be told that the locale has changed. Use this function
1654 to do so, before returning to Perl.
1660 Perl_sync_locale(pTHX)
1663 #ifdef USE_LOCALE_CTYPE
1664 new_ctype(setlocale(LC_CTYPE, NULL));
1665 #endif /* USE_LOCALE_CTYPE */
1667 #ifdef USE_LOCALE_COLLATE
1668 new_collate(setlocale(LC_COLLATE, NULL));
1671 #ifdef USE_LOCALE_NUMERIC
1672 set_numeric_local(); /* Switch from "C" to underlying LC_NUMERIC */
1673 new_numeric(setlocale(LC_NUMERIC, NULL));
1674 #endif /* USE_LOCALE_NUMERIC */
1682 * c-indentation-style: bsd
1684 * indent-tabs-mode: nil
1687 * ex: set ts=8 sts=4 sw=4 et: