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
97 # ifdef HAS_LOCALECONV
98 const struct lconv* const lc = localeconv();
100 if (lc && lc->decimal_point) {
101 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
102 SvREFCNT_dec(PL_numeric_radix_sv);
103 PL_numeric_radix_sv = NULL;
106 if (PL_numeric_radix_sv)
107 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
109 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
110 if (! is_ascii_string((U8 *) lc->decimal_point, 0)
111 && is_utf8_string((U8 *) lc->decimal_point, 0)
112 && is_cur_LC_category_utf8(LC_NUMERIC))
114 SvUTF8_on(PL_numeric_radix_sv);
119 PL_numeric_radix_sv = NULL;
121 DEBUG_L(PerlIO_printf(Perl_debug_log, "Locale radix is %s\n",
122 (PL_numeric_radix_sv)
126 # endif /* HAS_LOCALECONV */
127 #endif /* USE_LOCALE_NUMERIC */
131 Perl_new_numeric(pTHX_ const char *newnum)
133 #ifdef USE_LOCALE_NUMERIC
135 /* Called after all libc setlocale() calls affecting LC_NUMERIC, to tell
136 * core Perl this and that 'newnum' is the name of the new locale.
137 * It installs this locale as the current underlying default.
139 * The default locale and the C locale can be toggled between by use of the
140 * set_numeric_local() and set_numeric_standard() functions, which should
141 * probably not be called directly, but only via macros like
142 * SET_NUMERIC_STANDARD() in perl.h.
144 * The toggling is necessary mainly so that a non-dot radix decimal point
145 * character can be output, while allowing internal calculations to use a
148 * This sets several interpreter-level variables:
149 * PL_numeric_name The default locale's name: a copy of 'newnum'
150 * PL_numeric_local A boolean indicating if the toggled state is such
151 * that the current locale is the default locale
152 * PL_numeric_standard A boolean indicating if the toggled state is such
153 * that the current locale is the C locale
154 * Note that both of the last two variables can be true at the same time,
155 * if the underlying locale is C. (Toggling is a no-op under these
158 * Any code changing the locale (outside this file) should use
159 * POSIX::setlocale, which calls this function. Therefore this function
160 * should be called directly only from this file and from
161 * POSIX::setlocale() */
167 Safefree(PL_numeric_name);
168 PL_numeric_name = NULL;
169 PL_numeric_standard = TRUE;
170 PL_numeric_local = TRUE;
174 save_newnum = stdize_locale(savepv(newnum));
175 if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) {
176 Safefree(PL_numeric_name);
177 PL_numeric_name = save_newnum;
180 PL_numeric_standard = ((*save_newnum == 'C' && save_newnum[1] == '\0')
181 || strEQ(save_newnum, "POSIX"));
182 PL_numeric_local = TRUE;
185 #endif /* USE_LOCALE_NUMERIC */
189 Perl_set_numeric_standard(pTHX)
191 #ifdef USE_LOCALE_NUMERIC
194 /* Toggle the LC_NUMERIC locale to C, if not already there. Probably
195 * should use the macros like SET_NUMERIC_STANDARD() in perl.h instead of
196 * calling this directly. */
198 if (! PL_numeric_standard) {
199 setlocale(LC_NUMERIC, "C");
200 PL_numeric_standard = TRUE;
201 PL_numeric_local = FALSE;
204 DEBUG_L(PerlIO_printf(Perl_debug_log,
205 "Underlying LC_NUMERIC locale now is C\n"));
207 #endif /* USE_LOCALE_NUMERIC */
211 Perl_set_numeric_local(pTHX)
213 #ifdef USE_LOCALE_NUMERIC
216 /* Toggle the LC_NUMERIC locale to the current underlying default, if not
217 * already there. Probably should use the macros like SET_NUMERIC_LOCAL()
218 * in perl.h instead of calling this directly. */
220 if (! PL_numeric_local) {
221 setlocale(LC_NUMERIC, PL_numeric_name);
222 PL_numeric_standard = FALSE;
223 PL_numeric_local = TRUE;
226 DEBUG_L(PerlIO_printf(Perl_debug_log,
227 "Underlying LC_NUMERIC locale now is %s\n",
230 #endif /* USE_LOCALE_NUMERIC */
234 * Set up for a new ctype locale.
237 Perl_new_ctype(pTHX_ const char *newctype)
239 #ifdef USE_LOCALE_CTYPE
241 /* Called after all libc setlocale() calls affecting LC_CTYPE, to tell
242 * core Perl this and that 'newctype' is the name of the new locale.
244 * This function sets up the folding arrays for all 256 bytes, assuming
245 * that tofold() is tolc() since fold case is not a concept in POSIX,
247 * Any code changing the locale (outside this file) should use
248 * POSIX::setlocale, which calls this function. Therefore this function
249 * should be called directly only from this file and from
250 * POSIX::setlocale() */
255 PERL_ARGS_ASSERT_NEW_CTYPE;
257 PL_in_utf8_CTYPE_locale = is_cur_LC_category_utf8(LC_CTYPE);
259 /* A UTF-8 locale gets standard rules. But note that code still has to
260 * handle this specially because of the three problematic code points */
261 if (PL_in_utf8_CTYPE_locale) {
262 Copy(PL_fold_latin1, PL_fold_locale, 256, U8);
265 for (i = 0; i < 256; i++) {
266 if (isUPPER_LC((U8) i))
267 PL_fold_locale[i] = (U8) toLOWER_LC((U8) i);
268 else if (isLOWER_LC((U8) i))
269 PL_fold_locale[i] = (U8) toUPPER_LC((U8) i);
271 PL_fold_locale[i] = (U8) i;
275 #endif /* USE_LOCALE_CTYPE */
276 PERL_ARGS_ASSERT_NEW_CTYPE;
277 PERL_UNUSED_ARG(newctype);
282 Perl_new_collate(pTHX_ const char *newcoll)
284 #ifdef USE_LOCALE_COLLATE
286 /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell
287 * core Perl this and that 'newcoll' is the name of the new locale.
289 * Any code changing the locale (outside this file) should use
290 * POSIX::setlocale, which calls this function. Therefore this function
291 * should be called directly only from this file and from
292 * POSIX::setlocale() */
297 if (PL_collation_name) {
299 Safefree(PL_collation_name);
300 PL_collation_name = NULL;
302 PL_collation_standard = TRUE;
303 PL_collxfrm_base = 0;
304 PL_collxfrm_mult = 2;
308 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
310 Safefree(PL_collation_name);
311 PL_collation_name = stdize_locale(savepv(newcoll));
312 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
313 || strEQ(newcoll, "POSIX"));
316 /* 2: at most so many chars ('a', 'b'). */
317 /* 50: surely no system expands a char more. */
318 #define XFRMBUFSIZE (2 * 50)
319 char xbuf[XFRMBUFSIZE];
320 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
321 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
322 const SSize_t mult = fb - fa;
323 if (mult < 1 && !(fa == 0 && fb == 0))
324 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
326 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
327 PL_collxfrm_mult = mult;
331 #endif /* USE_LOCALE_COLLATE */
337 Perl_my_setlocale(pTHX_ int category, const char* locale)
339 /* This, for Windows, emulates POSIX setlocale() behavior. There is no
340 * difference unless the input locale is "", which means on Windows to get
341 * the machine default, which is set via the computer's "Regional and
342 * Language Options" (or its current equivalent). In POSIX, it instead
343 * means to find the locale from the user's environment. This routine
344 * looks in the environment, and, if anything is found, uses that instead
345 * of going to the machine default. If there is no environment override,
346 * the machine default is used, as normal, by calling the real setlocale()
347 * with "". The POSIX behavior is to use the LC_ALL variable if set;
348 * otherwise to use the particular category's variable if set; otherwise to
349 * use the LANG variable. */
351 bool override_LC_ALL = 0;
354 if (locale && strEQ(locale, "")) {
356 locale = PerlEnv_getenv("LC_ALL");
362 override_LC_ALL = TRUE;
363 break; /* We already know its variable isn't set */
365 # ifdef USE_LOCALE_TIME
367 locale = PerlEnv_getenv("LC_TIME");
370 # ifdef USE_LOCALE_CTYPE
372 locale = PerlEnv_getenv("LC_CTYPE");
375 # ifdef USE_LOCALE_COLLATE
377 locale = PerlEnv_getenv("LC_COLLATE");
380 # ifdef USE_LOCALE_MONETARY
382 locale = PerlEnv_getenv("LC_MONETARY");
385 # ifdef USE_LOCALE_NUMERIC
387 locale = PerlEnv_getenv("LC_NUMERIC");
390 # ifdef USE_LOCALE_MESSAGES
392 locale = PerlEnv_getenv("LC_MESSAGES");
396 /* This is a category, like PAPER_SIZE that we don't
397 * know about; and so can't provide a wrapper. */
401 locale = PerlEnv_getenv("LANG");
411 result = setlocale(category, locale);
413 if (! override_LC_ALL) {
417 /* Here the input locale was LC_ALL, and we have set it to what is in the
418 * LANG variable or the system default if there is no LANG. But these have
419 * lower priority than the other LC_foo variables, so override it for each
420 * one that is set. (If they are set to "", it means to use the same thing
421 * we just set LC_ALL to, so can skip) */
422 # ifdef USE_LOCALE_TIME
423 result = PerlEnv_getenv("LC_TIME");
424 if (result && strNE(result, "")) {
425 setlocale(LC_TIME, result);
428 # ifdef USE_LOCALE_CTYPE
429 result = PerlEnv_getenv("LC_CTYPE");
430 if (result && strNE(result, "")) {
431 setlocale(LC_CTYPE, result);
434 # ifdef USE_LOCALE_COLLATE
435 result = PerlEnv_getenv("LC_COLLATE");
436 if (result && strNE(result, "")) {
437 setlocale(LC_COLLATE, result);
440 # ifdef USE_LOCALE_MONETARY
441 result = PerlEnv_getenv("LC_MONETARY");
442 if (result && strNE(result, "")) {
443 setlocale(LC_MONETARY, result);
446 # ifdef USE_LOCALE_NUMERIC
447 result = PerlEnv_getenv("LC_NUMERIC");
448 if (result && strNE(result, "")) {
449 setlocale(LC_NUMERIC, result);
452 # ifdef USE_LOCALE_MESSAGES
453 result = PerlEnv_getenv("LC_MESSAGES");
454 if (result && strNE(result, "")) {
455 setlocale(LC_MESSAGES, result);
459 return setlocale(LC_ALL, NULL);
467 * Initialize locale awareness.
470 Perl_init_i18nl10n(pTHX_ int printwarn)
474 * 0 if not to output warning when setup locale is bad
475 * 1 if to output warning based on value of PERL_BADLANG
476 * >1 if to output regardless of PERL_BADLANG
479 * 1 = set ok or not applicable,
480 * 0 = fallback to a locale of lower priority
481 * -1 = fallback to all locales failed, not even to the C locale
486 #if defined(USE_LOCALE)
489 #ifdef USE_LOCALE_CTYPE
490 char *curctype = NULL;
491 #endif /* USE_LOCALE_CTYPE */
492 #ifdef USE_LOCALE_COLLATE
493 char *curcoll = NULL;
494 #endif /* USE_LOCALE_COLLATE */
495 #ifdef USE_LOCALE_NUMERIC
497 #endif /* USE_LOCALE_NUMERIC */
499 char * const language = PerlEnv_getenv("LANGUAGE");
502 /* NULL uses the existing already set up locale */
503 const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT"))
506 const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */
507 unsigned int trial_locales_count;
508 char * const lc_all = PerlEnv_getenv("LC_ALL");
509 char * const lang = PerlEnv_getenv("LANG");
510 bool setlocale_failure = FALSE;
513 const bool locwarn = (printwarn > 1 ||
515 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
518 /* In some systems you can find out the system default locale
519 * and use that as the fallback locale. */
520 # define SYSTEM_DEFAULT_LOCALE
522 #ifdef SYSTEM_DEFAULT_LOCALE
523 const char *system_default_locale = NULL;
526 #ifndef LOCALE_ENVIRON_REQUIRED
527 PERL_UNUSED_VAR(done);
531 * Ultrix setlocale(..., "") fails if there are no environment
532 * variables from which to get a locale name.
537 if (my_setlocale(LC_ALL, setlocale_init))
540 setlocale_failure = TRUE;
542 if (!setlocale_failure) {
543 # ifdef USE_LOCALE_CTYPE
546 my_setlocale(LC_CTYPE,
547 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
548 ? setlocale_init : NULL)))
549 setlocale_failure = TRUE;
551 curctype = savepv(curctype);
552 # endif /* USE_LOCALE_CTYPE */
553 # ifdef USE_LOCALE_COLLATE
556 my_setlocale(LC_COLLATE,
557 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
558 ? setlocale_init : NULL)))
559 setlocale_failure = TRUE;
561 curcoll = savepv(curcoll);
562 # endif /* USE_LOCALE_COLLATE */
563 # ifdef USE_LOCALE_NUMERIC
566 my_setlocale(LC_NUMERIC,
567 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
568 ? setlocale_init : NULL)))
569 setlocale_failure = TRUE;
571 curnum = savepv(curnum);
572 # endif /* USE_LOCALE_NUMERIC */
573 # ifdef USE_LOCALE_MESSAGES
574 if (! my_setlocale(LC_MESSAGES,
575 (!done && (lang || PerlEnv_getenv("LC_MESSAGES")))
576 ? setlocale_init : NULL))
578 setlocale_failure = TRUE;
580 # endif /* USE_LOCALE_MESSAGES */
581 # ifdef USE_LOCALE_MONETARY
582 if (! my_setlocale(LC_MONETARY,
583 (!done && (lang || PerlEnv_getenv("LC_MONETARY")))
584 ? setlocale_init : NULL))
586 setlocale_failure = TRUE;
588 # endif /* USE_LOCALE_MONETARY */
593 #endif /* !LOCALE_ENVIRON_REQUIRED */
595 /* We try each locale in the list until we get one that works, or exhaust
597 trial_locales[0] = setlocale_init;
598 trial_locales_count = 1;
599 for (i= 0; i < trial_locales_count; i++) {
600 const char * trial_locale = trial_locales[i];
604 /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED
605 * when i==0, but I (khw) don't think that behavior makes much
607 setlocale_failure = FALSE;
609 #ifdef SYSTEM_DEFAULT_LOCALE
611 /* On Windows machines, an entry of "" after the 0th means to use
612 * the system default locale, which we now proceed to get. */
613 if (strEQ(trial_locale, "")) {
616 /* Note that this may change the locale, but we are going to do
617 * that anyway just below */
618 system_default_locale = setlocale(LC_ALL, "");
620 /* Skip if invalid or it's already on the list of locales to
622 if (! system_default_locale) {
625 for (j = 0; j < trial_locales_count; j++) {
626 if (strEQ(system_default_locale, trial_locales[j])) {
631 trial_locale = system_default_locale;
634 #endif /* SYSTEM_DEFAULT_LOCALE */
638 if (! my_setlocale(LC_ALL, trial_locale)) {
639 setlocale_failure = TRUE;
642 /* Since LC_ALL succeeded, it should have changed all the other
643 * categories it can to its value; so we massage things so that the
644 * setlocales below just return their category's current values.
645 * This adequately handles the case in NetBSD where LC_COLLATE may
646 * not be defined for a locale, and setting it individually will
647 * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to
648 * the POSIX locale. */
653 if (!setlocale_failure) {
654 #ifdef USE_LOCALE_CTYPE
656 if (! (curctype = my_setlocale(LC_CTYPE, trial_locale)))
657 setlocale_failure = TRUE;
659 curctype = savepv(curctype);
660 #endif /* USE_LOCALE_CTYPE */
661 #ifdef USE_LOCALE_COLLATE
663 if (! (curcoll = my_setlocale(LC_COLLATE, trial_locale)))
664 setlocale_failure = TRUE;
666 curcoll = savepv(curcoll);
667 #endif /* USE_LOCALE_COLLATE */
668 #ifdef USE_LOCALE_NUMERIC
670 if (! (curnum = my_setlocale(LC_NUMERIC, trial_locale)))
671 setlocale_failure = TRUE;
673 curnum = savepv(curnum);
674 #endif /* USE_LOCALE_NUMERIC */
675 #ifdef USE_LOCALE_MESSAGES
676 if (! (my_setlocale(LC_MESSAGES, trial_locale)))
677 setlocale_failure = TRUE;
678 #endif /* USE_LOCALE_MESSAGES */
679 #ifdef USE_LOCALE_MONETARY
680 if (! (my_setlocale(LC_MONETARY, trial_locale)))
681 setlocale_failure = TRUE;
682 #endif /* USE_LOCALE_MONETARY */
684 if (! setlocale_failure) { /* Success */
689 /* Here, something failed; will need to try a fallback. */
695 if (locwarn) { /* Output failure info only on the first one */
698 PerlIO_printf(Perl_error_log,
699 "perl: warning: Setting locale failed.\n");
703 PerlIO_printf(Perl_error_log,
704 "perl: warning: Setting locale failed for the categories:\n\t");
705 #ifdef USE_LOCALE_CTYPE
707 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
708 #endif /* USE_LOCALE_CTYPE */
709 #ifdef USE_LOCALE_COLLATE
711 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
712 #endif /* USE_LOCALE_COLLATE */
713 #ifdef USE_LOCALE_NUMERIC
715 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
716 #endif /* USE_LOCALE_NUMERIC */
717 PerlIO_printf(Perl_error_log, "and possibly others\n");
721 PerlIO_printf(Perl_error_log,
722 "perl: warning: Please check that your locale settings:\n");
725 PerlIO_printf(Perl_error_log,
726 "\tLANGUAGE = %c%s%c,\n",
727 language ? '"' : '(',
728 language ? language : "unset",
729 language ? '"' : ')');
732 PerlIO_printf(Perl_error_log,
733 "\tLC_ALL = %c%s%c,\n",
735 lc_all ? lc_all : "unset",
738 #if defined(USE_ENVIRON_ARRAY)
741 for (e = environ; *e; e++) {
742 if (strnEQ(*e, "LC_", 3)
743 && strnNE(*e, "LC_ALL=", 7)
744 && (p = strchr(*e, '=')))
745 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
746 (int)(p - *e), *e, p + 1);
750 PerlIO_printf(Perl_error_log,
751 "\t(possibly more locale environment variables)\n");
754 PerlIO_printf(Perl_error_log,
757 lang ? lang : "unset",
760 PerlIO_printf(Perl_error_log,
761 " are supported and installed on your system.\n");
764 /* Calculate what fallback locales to try. We have avoided this
765 * until we have to, becuase failure is quite unlikely. This will
766 * usually change the upper bound of the loop we are in.
768 * Since the system's default way of setting the locale has not
769 * found one that works, We use Perl's defined ordering: LC_ALL,
770 * LANG, and the C locale. We don't try the same locale twice, so
771 * don't add to the list if already there. (On POSIX systems, the
772 * LC_ALL element will likely be a repeat of the 0th element "",
773 * but there's no harm done by doing it explicitly */
775 for (j = 0; j < trial_locales_count; j++) {
776 if (strEQ(lc_all, trial_locales[j])) {
780 trial_locales[trial_locales_count++] = lc_all;
785 for (j = 0; j < trial_locales_count; j++) {
786 if (strEQ(lang, trial_locales[j])) {
790 trial_locales[trial_locales_count++] = lang;
794 #if defined(WIN32) && defined(LC_ALL)
795 /* For Windows, we also try the system default locale before "C".
796 * (If there exists a Windows without LC_ALL we skip this because
797 * it gets too complicated. For those, the "C" is the next
798 * fallback possibility). The "" is the same as the 0th element of
799 * the array, but the code at the loop above knows to treat it
800 * differently when not the 0th */
801 trial_locales[trial_locales_count++] = "";
804 for (j = 0; j < trial_locales_count; j++) {
805 if (strEQ("C", trial_locales[j])) {
809 trial_locales[trial_locales_count++] = "C";
812 } /* end of first time through the loop */
818 } /* end of looping through the trial locales */
820 if (ok < 1) { /* If we tried to fallback */
822 if (! setlocale_failure) { /* fallback succeeded */
823 msg = "Falling back to";
825 else { /* fallback failed */
827 /* We dropped off the end of the loop, so have to decrement i to
828 * get back to the value the last time through */
832 msg = "Failed to fall back to";
834 /* To continue, we should use whatever values we've got */
835 #ifdef USE_LOCALE_CTYPE
837 curctype = savepv(setlocale(LC_CTYPE, NULL));
838 #endif /* USE_LOCALE_CTYPE */
839 #ifdef USE_LOCALE_COLLATE
841 curcoll = savepv(setlocale(LC_COLLATE, NULL));
842 #endif /* USE_LOCALE_COLLATE */
843 #ifdef USE_LOCALE_NUMERIC
845 curnum = savepv(setlocale(LC_NUMERIC, NULL));
846 #endif /* USE_LOCALE_NUMERIC */
850 const char * description;
851 const char * name = "";
852 if (strEQ(trial_locales[i], "C")) {
853 description = "the standard locale";
856 #ifdef SYSTEM_DEFAULT_LOCALE
857 else if (strEQ(trial_locales[i], "")) {
858 description = "the system default locale";
859 if (system_default_locale) {
860 name = system_default_locale;
863 #endif /* SYSTEM_DEFAULT_LOCALE */
865 description = "a fallback locale";
866 name = trial_locales[i];
868 if (name && strNE(name, "")) {
869 PerlIO_printf(Perl_error_log,
870 "perl: warning: %s %s (\"%s\").\n", msg, description, name);
873 PerlIO_printf(Perl_error_log,
874 "perl: warning: %s %s.\n", msg, description);
877 } /* End of tried to fallback */
879 #ifdef USE_LOCALE_CTYPE
881 #endif /* USE_LOCALE_CTYPE */
883 #ifdef USE_LOCALE_COLLATE
884 new_collate(curcoll);
885 #endif /* USE_LOCALE_COLLATE */
887 #ifdef USE_LOCALE_NUMERIC
889 #endif /* USE_LOCALE_NUMERIC */
891 #if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE)
892 /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE
893 * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by
894 * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the
895 * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open
897 PL_utf8locale = is_cur_LC_category_utf8(LC_CTYPE);
899 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
900 This is an alternative to using the -C command line switch
901 (the -C if present will override this). */
903 const char *p = PerlEnv_getenv("PERL_UNICODE");
904 PL_unicode = p ? parse_unicode_opts(&p) : 0;
905 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
910 #ifdef USE_LOCALE_CTYPE
912 #endif /* USE_LOCALE_CTYPE */
913 #ifdef USE_LOCALE_COLLATE
915 #endif /* USE_LOCALE_COLLATE */
916 #ifdef USE_LOCALE_NUMERIC
918 #endif /* USE_LOCALE_NUMERIC */
920 #endif /* USE_LOCALE */
926 #ifdef USE_LOCALE_COLLATE
929 * mem_collxfrm() is a bit like strxfrm() but with two important
930 * differences. First, it handles embedded NULs. Second, it allocates
931 * a bit more memory than needed for the transformed data itself.
932 * The real transformed data begins at offset sizeof(collationix).
933 * Please see sv_collxfrm() to see how this is used.
937 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
941 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
943 PERL_ARGS_ASSERT_MEM_COLLXFRM;
945 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
946 /* the +1 is for the terminating NUL. */
948 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
949 Newx(xbuf, xAlloc, char);
953 *(U32*)xbuf = PL_collation_ix;
954 xout = sizeof(PL_collation_ix);
955 for (xin = 0; xin < len; ) {
959 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
960 if (xused >= PERL_INT_MAX)
962 if ((STRLEN)xused < xAlloc - xout)
964 xAlloc = (2 * xAlloc) + 1;
965 Renew(xbuf, xAlloc, char);
970 xin += strlen(s + xin) + 1;
973 /* Embedded NULs are understood but silently skipped
974 * because they make no sense in locale collation. */
978 *xlen = xout - sizeof(PL_collation_ix);
987 #endif /* USE_LOCALE_COLLATE */
992 S_is_cur_LC_category_utf8(pTHX_ int category)
994 /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
995 * otherwise. 'category' may not be LC_ALL. If the platform doesn't have
996 * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence
997 * could give the wrong result. It errs on the side of not being a UTF-8
1000 char *save_input_locale = NULL;
1004 assert(category != LC_ALL);
1007 /* First dispose of the trivial cases */
1008 save_input_locale = setlocale(category, NULL);
1009 if (! save_input_locale) {
1010 DEBUG_L(PerlIO_printf(Perl_debug_log,
1011 "Could not find current locale for category %d\n",
1013 return FALSE; /* XXX maybe should croak */
1015 save_input_locale = stdize_locale(savepv(save_input_locale));
1016 if ((*save_input_locale == 'C' && save_input_locale[1] == '\0')
1017 || strEQ(save_input_locale, "POSIX"))
1019 DEBUG_L(PerlIO_printf(Perl_debug_log,
1020 "Current locale for category %d is %s\n",
1021 category, save_input_locale));
1022 Safefree(save_input_locale);
1026 #if defined(USE_LOCALE_CTYPE) \
1027 && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET)))
1029 { /* Next try nl_langinfo or MB_CUR_MAX if available */
1031 char *save_ctype_locale = NULL;
1034 if (category != LC_CTYPE) { /* These work only on LC_CTYPE */
1036 /* Get the current LC_CTYPE locale */
1037 save_ctype_locale = stdize_locale(savepv(setlocale(LC_CTYPE, NULL)));
1038 if (! save_ctype_locale) {
1039 DEBUG_L(PerlIO_printf(Perl_debug_log,
1040 "Could not find current locale for LC_CTYPE\n"));
1041 goto cant_use_nllanginfo;
1044 /* If LC_CTYPE and the desired category use the same locale, this
1045 * means that finding the value for LC_CTYPE is the same as finding
1046 * the value for the desired category. Otherwise, switch LC_CTYPE
1047 * to the desired category's locale */
1048 if (strEQ(save_ctype_locale, save_input_locale)) {
1049 Safefree(save_ctype_locale);
1050 save_ctype_locale = NULL;
1052 else if (! setlocale(LC_CTYPE, save_input_locale)) {
1053 DEBUG_L(PerlIO_printf(Perl_debug_log,
1054 "Could not change LC_CTYPE locale to %s\n",
1055 save_input_locale));
1056 Safefree(save_ctype_locale);
1057 goto cant_use_nllanginfo;
1061 DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n",
1062 save_input_locale));
1064 /* Here the current LC_CTYPE is set to the locale of the category whose
1065 * information is desired. This means that nl_langinfo() and MB_CUR_MAX
1066 * should give the correct results */
1068 # if defined(HAS_NL_LANGINFO) && defined(CODESET)
1070 char *codeset = savepv(nl_langinfo(CODESET));
1071 if (codeset && strNE(codeset, "")) {
1073 /* If we switched LC_CTYPE, switch back */
1074 if (save_ctype_locale) {
1075 setlocale(LC_CTYPE, save_ctype_locale);
1076 Safefree(save_ctype_locale);
1079 is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8"))
1080 || foldEQ(codeset, STR_WITH_LEN("UTF8"));
1082 DEBUG_L(PerlIO_printf(Perl_debug_log,
1083 "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n",
1086 Safefree(save_input_locale);
1095 /* Here, either we don't have nl_langinfo, or it didn't return a
1096 * codeset. Try MB_CUR_MAX */
1098 /* Standard UTF-8 needs at least 4 bytes to represent the maximum
1099 * Unicode code point. Since UTF-8 is the only non-single byte
1100 * encoding we handle, we just say any such encoding is UTF-8, and if
1101 * turns out to be wrong, other things will fail */
1102 is_utf8 = MB_CUR_MAX >= 4;
1104 DEBUG_L(PerlIO_printf(Perl_debug_log,
1105 "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n",
1106 (int) MB_CUR_MAX, is_utf8));
1108 Safefree(save_input_locale);
1112 /* ... But, most system that have MB_CUR_MAX will also have mbtowc(),
1113 * since they are both in the C99 standard. We can feed a known byte
1114 * string to the latter function, and check that it gives the expected
1118 GCC_DIAG_IGNORE(-Wunused-result);
1119 (void) mbtowc(&wc, NULL, 0); /* Reset any shift state */
1122 if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
1123 != strlen(HYPHEN_UTF8)
1124 || wc != (wchar_t) 0x2010)
1127 DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", wc));
1128 DEBUG_L(PerlIO_printf(Perl_debug_log,
1129 "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n",
1130 mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno));
1135 /* If we switched LC_CTYPE, switch back */
1136 if (save_ctype_locale) {
1137 setlocale(LC_CTYPE, save_ctype_locale);
1138 Safefree(save_ctype_locale);
1145 cant_use_nllanginfo:
1147 #endif /* HAS_NL_LANGINFO etc */
1149 /* nl_langinfo not available or failed somehow. Look at the locale name to
1150 * see if it matches qr/UTF -? 8 /ix */
1152 final_pos = strlen(save_input_locale) - 1;
1153 if (final_pos >= 3) {
1154 char *name = save_input_locale;
1156 /* Find next 'U' or 'u' and look from there */
1157 while ((name += strcspn(name, "Uu") + 1)
1158 <= save_input_locale + final_pos - 2)
1160 if (toFOLD(*(name)) != 't'
1161 || toFOLD(*(name + 1)) != 'f')
1166 if (*(name) == '-') {
1167 if ((name > save_input_locale + final_pos - 1)) {
1172 if (*(name) == '8') {
1173 Safefree(save_input_locale);
1174 DEBUG_L(PerlIO_printf(Perl_debug_log,
1175 "Locale %s ends with UTF-8 in name\n",
1176 save_input_locale));
1180 DEBUG_L(PerlIO_printf(Perl_debug_log,
1181 "Locale %s doesn't end with UTF-8 in name\n",
1182 save_input_locale));
1186 /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
1188 && *(save_input_locale + final_pos - 0) == '1'
1189 && *(save_input_locale + final_pos - 1) == '0'
1190 && *(save_input_locale + final_pos - 2) == '0'
1191 && *(save_input_locale + final_pos - 3) == '5'
1192 && *(save_input_locale + final_pos - 4) == '6')
1194 DEBUG_L(PerlIO_printf(Perl_debug_log,
1195 "Locale %s ends with 10056 in name, is UTF-8 locale\n",
1196 save_input_locale));
1197 Safefree(save_input_locale);
1202 /* Other common encodings are the ISO 8859 series, which aren't UTF-8 */
1203 if (instr(save_input_locale, "8859")) {
1204 DEBUG_L(PerlIO_printf(Perl_debug_log,
1205 "Locale %s has 8859 in name, not UTF-8 locale\n",
1206 save_input_locale));
1207 Safefree(save_input_locale);
1211 #ifdef HAS_LOCALECONV
1213 # ifdef USE_LOCALE_MONETARY
1215 /* Here, there is nothing in the locale name to indicate whether the locale
1216 * is UTF-8 or not. This "name", the return of setlocale(), is actually
1217 * defined to be opaque, so we can't really rely on the absence of various
1218 * substrings in the name to indicate its UTF-8ness. Look at the locale's
1219 * currency symbol. Often that will be in the native script, and if the
1220 * symbol isn't in UTF-8, we know that the locale isn't. If it is
1221 * non-ASCII UTF-8, we infer that the locale is too.
1222 * To do this, like above for LC_CTYPE, we first set LC_MONETARY to the
1223 * locale of the desired category, if it isn't that locale already */
1226 char *save_monetary_locale = NULL;
1227 bool illegal_utf8 = FALSE;
1228 bool only_ascii = FALSE;
1229 const struct lconv* const lc = localeconv();
1231 if (category != LC_MONETARY) {
1233 save_monetary_locale = stdize_locale(savepv(setlocale(LC_MONETARY,
1235 if (! save_monetary_locale) {
1236 DEBUG_L(PerlIO_printf(Perl_debug_log,
1237 "Could not find current locale for LC_MONETARY\n"));
1238 goto cant_use_monetary;
1241 if (strNE(save_monetary_locale, save_input_locale)) {
1242 if (! setlocale(LC_MONETARY, save_input_locale)) {
1243 DEBUG_L(PerlIO_printf(Perl_debug_log,
1244 "Could not change LC_MONETARY locale to %s\n",
1245 save_input_locale));
1246 Safefree(save_monetary_locale);
1247 goto cant_use_monetary;
1252 /* Here the current LC_MONETARY is set to the locale of the category
1253 * whose information is desired. */
1255 if (lc && lc->currency_symbol) {
1256 if (! is_utf8_string((U8 *) lc->currency_symbol, 0)) {
1257 DEBUG_L(PerlIO_printf(Perl_debug_log,
1258 "Currency symbol for %s is not legal UTF-8\n",
1259 save_input_locale));
1260 illegal_utf8 = TRUE;
1262 else if (is_ascii_string((U8 *) lc->currency_symbol, 0)) {
1263 DEBUG_L(PerlIO_printf(Perl_debug_log, "Currency symbol for %s contains only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale));
1268 /* If we changed it, restore LC_MONETARY to its original locale */
1269 if (save_monetary_locale) {
1270 setlocale(LC_MONETARY, save_monetary_locale);
1271 Safefree(save_monetary_locale);
1274 Safefree(save_input_locale);
1276 /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; otherwise
1277 * assume the locale is UTF-8 if and only if the symbol is non-ascii
1278 * UTF-8. (We can't really tell if the locale is UTF-8 or not if the
1279 * symbol is just a '$', so we err on the side of it not being UTF-8)
1281 DEBUG_L(PerlIO_printf(Perl_debug_log, "\tis_utf8=%d\n", (illegal_utf8)
1284 return (illegal_utf8)
1291 # endif /* USE_LOCALE_MONETARY */
1292 #endif /* HAS_LOCALECONV */
1294 #if 0 && defined(HAS_STRERROR) && defined(USE_LOCALE_MESSAGES)
1296 /* This code is ifdefd out because it was found to not be necessary in testing
1297 * on our dromedary test machine, which has over 700 locales. There, looking
1298 * at just the currency symbol gave essentially the same results as doing this
1299 * extra work. Executing this also caused segfaults in miniperl. I left it in
1300 * so as to avoid rewriting it if real-world experience indicates that
1301 * dromedary is an outlier. Essentially, instead of returning abpve if we
1302 * haven't found illegal utf8, we continue on and examine all the strerror()
1303 * messages on the platform for utf8ness. If all are ASCII, we still don't
1304 * know the answer; but otherwise we have a pretty good indication of the
1305 * utf8ness. The reason this doesn't necessarily help much is that the
1306 * messages may not have been translated into the locale. The currency symbol
1307 * is much more likely to have been translated. The code below would need to
1308 * be altered somewhat to just be a continuation of testing the currency
1311 unsigned int failures = 0, non_ascii = 0;
1312 char *save_messages_locale = NULL;
1314 /* Like above for LC_CTYPE, we set LC_MESSAGES to the locale of the
1315 * desired category, if it isn't that locale already */
1317 if (category != LC_MESSAGES) {
1319 save_messages_locale = stdize_locale(savepv(setlocale(LC_MESSAGES,
1321 if (! save_messages_locale) {
1322 goto cant_use_messages;
1325 if (strEQ(save_messages_locale, save_input_locale)) {
1326 Safefree(save_input_locale);
1328 else if (! setlocale(LC_MESSAGES, save_input_locale)) {
1329 Safefree(save_messages_locale);
1330 goto cant_use_messages;
1334 /* Here the current LC_MESSAGES is set to the locale of the category
1335 * whose information is desired. Look through all the messages */
1338 #ifdef HAS_SYS_ERRLIST
1343 const U8* const errmsg = (U8 *) Strerror(e) ;
1346 if (! is_utf8_string(errmsg, 0)) {
1350 else if (! is_ascii_string(errmsg, 0)) {
1355 /* And, if we changed it, restore LC_MESSAGES to its original locale */
1356 if (save_messages_locale) {
1357 setlocale(LC_MESSAGES, save_messages_locale);
1358 Safefree(save_messages_locale);
1361 /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid,
1362 * any non-ascii means it is one; otherwise we assume it isn't */
1363 return (failures) ? FALSE : non_ascii;
1370 DEBUG_L(PerlIO_printf(Perl_debug_log,
1371 "Assuming locale %s is not a UTF-8 locale\n",
1372 save_input_locale));
1373 Safefree(save_input_locale);
1381 * c-indentation-style: bsd
1383 * indent-tabs-mode: nil
1386 * ex: set ts=8 sts=4 sw=4 et: