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 */
130 Perl_new_numeric(pTHX_ const char *newnum)
132 #ifdef USE_LOCALE_NUMERIC
134 /* Called after all libc setlocale() calls affecting LC_NUMERIC, to tell
135 * core Perl this and that 'newnum' is the name of the new locale.
136 * It installs this locale as the current underlying default.
138 * The default locale and the C locale can be toggled between by use of the
139 * set_numeric_local() and set_numeric_standard() functions, which should
140 * probably not be called directly, but only via macros like
141 * SET_NUMERIC_STANDARD() in perl.h.
143 * The toggling is necessary mainly so that a non-dot radix decimal point
144 * character can be output, while allowing internal calculations to use a
147 * This sets several interpreter-level variables:
148 * PL_numeric_name The default locale's name: a copy of 'newnum'
149 * PL_numeric_local A boolean indicating if the toggled state is such
150 * that the current locale is the program's underlying
152 * PL_numeric_standard An int indicating if the toggled state is such
153 * that the current locale is the C locale. If non-zero,
154 * it is in C; if > 1, it means it may not be toggled away
156 * Note that both of the last two variables can be true at the same time,
157 * if the underlying locale is C. (Toggling is a no-op under these
160 * Any code changing the locale (outside this file) should use
161 * POSIX::setlocale, which calls this function. Therefore this function
162 * should be called directly only from this file and from
163 * POSIX::setlocale() */
168 Safefree(PL_numeric_name);
169 PL_numeric_name = NULL;
170 PL_numeric_standard = TRUE;
171 PL_numeric_local = TRUE;
175 save_newnum = stdize_locale(savepv(newnum));
176 if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) {
177 Safefree(PL_numeric_name);
178 PL_numeric_name = save_newnum;
181 PL_numeric_standard = ((*save_newnum == 'C' && save_newnum[1] == '\0')
182 || strEQ(save_newnum, "POSIX"));
183 PL_numeric_local = TRUE;
185 /* Keep LC_NUMERIC in the C locale. This is for XS modules, so they don't
186 * have to worry about the radix being a non-dot. (Core operations that
187 * need the underlying locale change to it temporarily). */
188 set_numeric_standard();
192 #endif /* USE_LOCALE_NUMERIC */
196 Perl_set_numeric_standard(pTHX)
198 #ifdef USE_LOCALE_NUMERIC
199 /* Toggle the LC_NUMERIC locale to C, if not already there. Probably
200 * should use the macros like SET_NUMERIC_STANDARD() in perl.h instead of
201 * calling this directly. */
203 if (_NOT_IN_NUMERIC_STANDARD) {
204 setlocale(LC_NUMERIC, "C");
205 PL_numeric_standard = TRUE;
206 PL_numeric_local = FALSE;
209 DEBUG_L(PerlIO_printf(Perl_debug_log,
210 "Underlying LC_NUMERIC locale now is C\n"));
212 #endif /* USE_LOCALE_NUMERIC */
216 Perl_set_numeric_local(pTHX)
218 #ifdef USE_LOCALE_NUMERIC
219 /* Toggle the LC_NUMERIC locale to the current underlying default, if not
220 * already there. Probably should use the macros like SET_NUMERIC_LOCAL()
221 * in perl.h instead of calling this directly. */
223 if (_NOT_IN_NUMERIC_LOCAL) {
224 setlocale(LC_NUMERIC, PL_numeric_name);
225 PL_numeric_standard = FALSE;
226 PL_numeric_local = TRUE;
229 DEBUG_L(PerlIO_printf(Perl_debug_log,
230 "Underlying LC_NUMERIC locale now is %s\n",
233 #endif /* USE_LOCALE_NUMERIC */
237 * Set up for a new ctype locale.
240 Perl_new_ctype(pTHX_ const char *newctype)
242 #ifdef USE_LOCALE_CTYPE
244 /* Called after all libc setlocale() calls affecting LC_CTYPE, to tell
245 * core Perl this and that 'newctype' is the name of the new locale.
247 * This function sets up the folding arrays for all 256 bytes, assuming
248 * that tofold() is tolc() since fold case is not a concept in POSIX,
250 * Any code changing the locale (outside this file) should use
251 * POSIX::setlocale, which calls this function. Therefore this function
252 * should be called directly only from this file and from
253 * POSIX::setlocale() */
258 PERL_ARGS_ASSERT_NEW_CTYPE;
260 PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE);
262 /* A UTF-8 locale gets standard rules. But note that code still has to
263 * handle this specially because of the three problematic code points */
264 if (PL_in_utf8_CTYPE_locale) {
265 Copy(PL_fold_latin1, PL_fold_locale, 256, U8);
268 for (i = 0; i < 256; i++) {
269 if (isUPPER_LC((U8) i))
270 PL_fold_locale[i] = (U8) toLOWER_LC((U8) i);
271 else if (isLOWER_LC((U8) i))
272 PL_fold_locale[i] = (U8) toUPPER_LC((U8) i);
274 PL_fold_locale[i] = (U8) i;
278 #endif /* USE_LOCALE_CTYPE */
279 PERL_ARGS_ASSERT_NEW_CTYPE;
280 PERL_UNUSED_ARG(newctype);
285 Perl_new_collate(pTHX_ const char *newcoll)
287 #ifdef USE_LOCALE_COLLATE
289 /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell
290 * core Perl this and that 'newcoll' is the name of the new locale.
292 * Any code changing the locale (outside this file) should use
293 * POSIX::setlocale, which calls this function. Therefore this function
294 * should be called directly only from this file and from
295 * POSIX::setlocale() */
298 if (PL_collation_name) {
300 Safefree(PL_collation_name);
301 PL_collation_name = NULL;
303 PL_collation_standard = TRUE;
304 PL_collxfrm_base = 0;
305 PL_collxfrm_mult = 2;
309 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
311 Safefree(PL_collation_name);
312 PL_collation_name = stdize_locale(savepv(newcoll));
313 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
314 || strEQ(newcoll, "POSIX"));
317 /* 2: at most so many chars ('a', 'b'). */
318 /* 50: surely no system expands a char more. */
319 #define XFRMBUFSIZE (2 * 50)
320 char xbuf[XFRMBUFSIZE];
321 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
322 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
323 const SSize_t mult = fb - fa;
324 if (mult < 1 && !(fa == 0 && fb == 0))
325 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
327 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
328 PL_collxfrm_mult = mult;
332 #endif /* USE_LOCALE_COLLATE */
338 Perl_my_setlocale(pTHX_ int category, const char* locale)
340 /* This, for Windows, emulates POSIX setlocale() behavior. There is no
341 * difference unless the input locale is "", which means on Windows to get
342 * the machine default, which is set via the computer's "Regional and
343 * Language Options" (or its current equivalent). In POSIX, it instead
344 * means to find the locale from the user's environment. This routine
345 * looks in the environment, and, if anything is found, uses that instead
346 * of going to the machine default. If there is no environment override,
347 * the machine default is used, as normal, by calling the real setlocale()
348 * with "". The POSIX behavior is to use the LC_ALL variable if set;
349 * otherwise to use the particular category's variable if set; otherwise to
350 * use the LANG variable. */
352 bool override_LC_ALL = 0;
355 if (locale && strEQ(locale, "")) {
357 locale = PerlEnv_getenv("LC_ALL");
363 override_LC_ALL = TRUE;
364 break; /* We already know its variable isn't set */
366 # ifdef USE_LOCALE_TIME
368 locale = PerlEnv_getenv("LC_TIME");
371 # ifdef USE_LOCALE_CTYPE
373 locale = PerlEnv_getenv("LC_CTYPE");
376 # ifdef USE_LOCALE_COLLATE
378 locale = PerlEnv_getenv("LC_COLLATE");
381 # ifdef USE_LOCALE_MONETARY
383 locale = PerlEnv_getenv("LC_MONETARY");
386 # ifdef USE_LOCALE_NUMERIC
388 locale = PerlEnv_getenv("LC_NUMERIC");
391 # ifdef USE_LOCALE_MESSAGES
393 locale = PerlEnv_getenv("LC_MESSAGES");
397 /* This is a category, like PAPER_SIZE that we don't
398 * know about; and so can't provide a wrapper. */
402 locale = PerlEnv_getenv("LANG");
412 result = setlocale(category, locale);
414 if (! override_LC_ALL) {
418 /* Here the input locale was LC_ALL, and we have set it to what is in the
419 * LANG variable or the system default if there is no LANG. But these have
420 * lower priority than the other LC_foo variables, so override it for each
421 * one that is set. (If they are set to "", it means to use the same thing
422 * we just set LC_ALL to, so can skip) */
423 # ifdef USE_LOCALE_TIME
424 result = PerlEnv_getenv("LC_TIME");
425 if (result && strNE(result, "")) {
426 setlocale(LC_TIME, result);
429 # ifdef USE_LOCALE_CTYPE
430 result = PerlEnv_getenv("LC_CTYPE");
431 if (result && strNE(result, "")) {
432 setlocale(LC_CTYPE, result);
435 # ifdef USE_LOCALE_COLLATE
436 result = PerlEnv_getenv("LC_COLLATE");
437 if (result && strNE(result, "")) {
438 setlocale(LC_COLLATE, result);
441 # ifdef USE_LOCALE_MONETARY
442 result = PerlEnv_getenv("LC_MONETARY");
443 if (result && strNE(result, "")) {
444 setlocale(LC_MONETARY, result);
447 # ifdef USE_LOCALE_NUMERIC
448 result = PerlEnv_getenv("LC_NUMERIC");
449 if (result && strNE(result, "")) {
450 setlocale(LC_NUMERIC, result);
453 # ifdef USE_LOCALE_MESSAGES
454 result = PerlEnv_getenv("LC_MESSAGES");
455 if (result && strNE(result, "")) {
456 setlocale(LC_MESSAGES, result);
460 return setlocale(LC_ALL, NULL);
468 * Initialize locale awareness.
471 Perl_init_i18nl10n(pTHX_ int printwarn)
475 * 0 if not to output warning when setup locale is bad
476 * 1 if to output warning based on value of PERL_BADLANG
477 * >1 if to output regardless of PERL_BADLANG
480 * 1 = set ok or not applicable,
481 * 0 = fallback to a locale of lower priority
482 * -1 = fallback to all locales failed, not even to the C locale
487 #if defined(USE_LOCALE)
488 #ifdef USE_LOCALE_CTYPE
489 char *curctype = NULL;
490 #endif /* USE_LOCALE_CTYPE */
491 #ifdef USE_LOCALE_COLLATE
492 char *curcoll = NULL;
493 #endif /* USE_LOCALE_COLLATE */
494 #ifdef USE_LOCALE_NUMERIC
496 #endif /* USE_LOCALE_NUMERIC */
498 char * const language = PerlEnv_getenv("LANGUAGE");
501 /* NULL uses the existing already set up locale */
502 const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT"))
505 const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */
506 unsigned int trial_locales_count;
507 char * const lc_all = PerlEnv_getenv("LC_ALL");
508 char * const lang = PerlEnv_getenv("LANG");
509 bool setlocale_failure = FALSE;
512 const bool locwarn = (printwarn > 1 ||
514 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
517 /* In some systems you can find out the system default locale
518 * and use that as the fallback locale. */
519 # define SYSTEM_DEFAULT_LOCALE
521 #ifdef SYSTEM_DEFAULT_LOCALE
522 const char *system_default_locale = NULL;
525 #ifndef LOCALE_ENVIRON_REQUIRED
526 PERL_UNUSED_VAR(done);
530 * Ultrix setlocale(..., "") fails if there are no environment
531 * variables from which to get a locale name.
536 if (my_setlocale(LC_ALL, setlocale_init))
539 setlocale_failure = TRUE;
541 if (!setlocale_failure) {
542 # ifdef USE_LOCALE_CTYPE
545 my_setlocale(LC_CTYPE,
546 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
547 ? setlocale_init : NULL)))
548 setlocale_failure = TRUE;
550 curctype = savepv(curctype);
551 # endif /* USE_LOCALE_CTYPE */
552 # ifdef USE_LOCALE_COLLATE
555 my_setlocale(LC_COLLATE,
556 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
557 ? setlocale_init : NULL)))
558 setlocale_failure = TRUE;
560 curcoll = savepv(curcoll);
561 # endif /* USE_LOCALE_COLLATE */
562 # ifdef USE_LOCALE_NUMERIC
565 my_setlocale(LC_NUMERIC,
566 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
567 ? setlocale_init : NULL)))
568 setlocale_failure = TRUE;
570 curnum = savepv(curnum);
571 # endif /* USE_LOCALE_NUMERIC */
572 # ifdef USE_LOCALE_MESSAGES
573 if (! my_setlocale(LC_MESSAGES,
574 (!done && (lang || PerlEnv_getenv("LC_MESSAGES")))
575 ? setlocale_init : NULL))
577 setlocale_failure = TRUE;
579 # endif /* USE_LOCALE_MESSAGES */
580 # ifdef USE_LOCALE_MONETARY
581 if (! my_setlocale(LC_MONETARY,
582 (!done && (lang || PerlEnv_getenv("LC_MONETARY")))
583 ? setlocale_init : NULL))
585 setlocale_failure = TRUE;
587 # endif /* USE_LOCALE_MONETARY */
592 #endif /* !LOCALE_ENVIRON_REQUIRED */
594 /* We try each locale in the list until we get one that works, or exhaust
596 trial_locales[0] = setlocale_init;
597 trial_locales_count = 1;
598 for (i= 0; i < trial_locales_count; i++) {
599 const char * trial_locale = trial_locales[i];
603 /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED
604 * when i==0, but I (khw) don't think that behavior makes much
606 setlocale_failure = FALSE;
608 #ifdef SYSTEM_DEFAULT_LOCALE
610 /* On Windows machines, an entry of "" after the 0th means to use
611 * the system default locale, which we now proceed to get. */
612 if (strEQ(trial_locale, "")) {
615 /* Note that this may change the locale, but we are going to do
616 * that anyway just below */
617 system_default_locale = setlocale(LC_ALL, "");
619 /* Skip if invalid or it's already on the list of locales to
621 if (! system_default_locale) {
624 for (j = 0; j < trial_locales_count; j++) {
625 if (strEQ(system_default_locale, trial_locales[j])) {
630 trial_locale = system_default_locale;
633 #endif /* SYSTEM_DEFAULT_LOCALE */
637 if (! my_setlocale(LC_ALL, trial_locale)) {
638 setlocale_failure = TRUE;
641 /* Since LC_ALL succeeded, it should have changed all the other
642 * categories it can to its value; so we massage things so that the
643 * setlocales below just return their category's current values.
644 * This adequately handles the case in NetBSD where LC_COLLATE may
645 * not be defined for a locale, and setting it individually will
646 * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to
647 * the POSIX locale. */
652 if (!setlocale_failure) {
653 #ifdef USE_LOCALE_CTYPE
655 if (! (curctype = my_setlocale(LC_CTYPE, trial_locale)))
656 setlocale_failure = TRUE;
658 curctype = savepv(curctype);
659 #endif /* USE_LOCALE_CTYPE */
660 #ifdef USE_LOCALE_COLLATE
662 if (! (curcoll = my_setlocale(LC_COLLATE, trial_locale)))
663 setlocale_failure = TRUE;
665 curcoll = savepv(curcoll);
666 #endif /* USE_LOCALE_COLLATE */
667 #ifdef USE_LOCALE_NUMERIC
669 if (! (curnum = my_setlocale(LC_NUMERIC, trial_locale)))
670 setlocale_failure = TRUE;
672 curnum = savepv(curnum);
673 #endif /* USE_LOCALE_NUMERIC */
674 #ifdef USE_LOCALE_MESSAGES
675 if (! (my_setlocale(LC_MESSAGES, trial_locale)))
676 setlocale_failure = TRUE;
677 #endif /* USE_LOCALE_MESSAGES */
678 #ifdef USE_LOCALE_MONETARY
679 if (! (my_setlocale(LC_MONETARY, trial_locale)))
680 setlocale_failure = TRUE;
681 #endif /* USE_LOCALE_MONETARY */
683 if (! setlocale_failure) { /* Success */
688 /* Here, something failed; will need to try a fallback. */
694 if (locwarn) { /* Output failure info only on the first one */
697 PerlIO_printf(Perl_error_log,
698 "perl: warning: Setting locale failed.\n");
702 PerlIO_printf(Perl_error_log,
703 "perl: warning: Setting locale failed for the categories:\n\t");
704 #ifdef USE_LOCALE_CTYPE
706 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
707 #endif /* USE_LOCALE_CTYPE */
708 #ifdef USE_LOCALE_COLLATE
710 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
711 #endif /* USE_LOCALE_COLLATE */
712 #ifdef USE_LOCALE_NUMERIC
714 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
715 #endif /* USE_LOCALE_NUMERIC */
716 PerlIO_printf(Perl_error_log, "and possibly others\n");
720 PerlIO_printf(Perl_error_log,
721 "perl: warning: Please check that your locale settings:\n");
724 PerlIO_printf(Perl_error_log,
725 "\tLANGUAGE = %c%s%c,\n",
726 language ? '"' : '(',
727 language ? language : "unset",
728 language ? '"' : ')');
731 PerlIO_printf(Perl_error_log,
732 "\tLC_ALL = %c%s%c,\n",
734 lc_all ? lc_all : "unset",
737 #if defined(USE_ENVIRON_ARRAY)
740 for (e = environ; *e; e++) {
741 if (strnEQ(*e, "LC_", 3)
742 && strnNE(*e, "LC_ALL=", 7)
743 && (p = strchr(*e, '=')))
744 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
745 (int)(p - *e), *e, p + 1);
749 PerlIO_printf(Perl_error_log,
750 "\t(possibly more locale environment variables)\n");
753 PerlIO_printf(Perl_error_log,
756 lang ? lang : "unset",
759 PerlIO_printf(Perl_error_log,
760 " are supported and installed on your system.\n");
763 /* Calculate what fallback locales to try. We have avoided this
764 * until we have to, becuase failure is quite unlikely. This will
765 * usually change the upper bound of the loop we are in.
767 * Since the system's default way of setting the locale has not
768 * found one that works, We use Perl's defined ordering: LC_ALL,
769 * LANG, and the C locale. We don't try the same locale twice, so
770 * don't add to the list if already there. (On POSIX systems, the
771 * LC_ALL element will likely be a repeat of the 0th element "",
772 * but there's no harm done by doing it explicitly */
774 for (j = 0; j < trial_locales_count; j++) {
775 if (strEQ(lc_all, trial_locales[j])) {
779 trial_locales[trial_locales_count++] = lc_all;
784 for (j = 0; j < trial_locales_count; j++) {
785 if (strEQ(lang, trial_locales[j])) {
789 trial_locales[trial_locales_count++] = lang;
793 #if defined(WIN32) && defined(LC_ALL)
794 /* For Windows, we also try the system default locale before "C".
795 * (If there exists a Windows without LC_ALL we skip this because
796 * it gets too complicated. For those, the "C" is the next
797 * fallback possibility). The "" is the same as the 0th element of
798 * the array, but the code at the loop above knows to treat it
799 * differently when not the 0th */
800 trial_locales[trial_locales_count++] = "";
803 for (j = 0; j < trial_locales_count; j++) {
804 if (strEQ("C", trial_locales[j])) {
808 trial_locales[trial_locales_count++] = "C";
811 } /* end of first time through the loop */
817 } /* end of looping through the trial locales */
819 if (ok < 1) { /* If we tried to fallback */
821 if (! setlocale_failure) { /* fallback succeeded */
822 msg = "Falling back to";
824 else { /* fallback failed */
826 /* We dropped off the end of the loop, so have to decrement i to
827 * get back to the value the last time through */
831 msg = "Failed to fall back to";
833 /* To continue, we should use whatever values we've got */
834 #ifdef USE_LOCALE_CTYPE
836 curctype = savepv(setlocale(LC_CTYPE, NULL));
837 #endif /* USE_LOCALE_CTYPE */
838 #ifdef USE_LOCALE_COLLATE
840 curcoll = savepv(setlocale(LC_COLLATE, NULL));
841 #endif /* USE_LOCALE_COLLATE */
842 #ifdef USE_LOCALE_NUMERIC
844 curnum = savepv(setlocale(LC_NUMERIC, NULL));
845 #endif /* USE_LOCALE_NUMERIC */
849 const char * description;
850 const char * name = "";
851 if (strEQ(trial_locales[i], "C")) {
852 description = "the standard locale";
855 #ifdef SYSTEM_DEFAULT_LOCALE
856 else if (strEQ(trial_locales[i], "")) {
857 description = "the system default locale";
858 if (system_default_locale) {
859 name = system_default_locale;
862 #endif /* SYSTEM_DEFAULT_LOCALE */
864 description = "a fallback locale";
865 name = trial_locales[i];
867 if (name && strNE(name, "")) {
868 PerlIO_printf(Perl_error_log,
869 "perl: warning: %s %s (\"%s\").\n", msg, description, name);
872 PerlIO_printf(Perl_error_log,
873 "perl: warning: %s %s.\n", msg, description);
876 } /* End of tried to fallback */
878 #ifdef USE_LOCALE_CTYPE
880 #endif /* USE_LOCALE_CTYPE */
882 #ifdef USE_LOCALE_COLLATE
883 new_collate(curcoll);
884 #endif /* USE_LOCALE_COLLATE */
886 #ifdef USE_LOCALE_NUMERIC
888 #endif /* USE_LOCALE_NUMERIC */
890 #if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE)
891 /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE
892 * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by
893 * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the
894 * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open
896 PL_utf8locale = _is_cur_LC_category_utf8(LC_CTYPE);
898 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
899 This is an alternative to using the -C command line switch
900 (the -C if present will override this). */
902 const char *p = PerlEnv_getenv("PERL_UNICODE");
903 PL_unicode = p ? parse_unicode_opts(&p) : 0;
904 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
909 #ifdef USE_LOCALE_CTYPE
911 #endif /* USE_LOCALE_CTYPE */
912 #ifdef USE_LOCALE_COLLATE
914 #endif /* USE_LOCALE_COLLATE */
915 #ifdef USE_LOCALE_NUMERIC
917 #endif /* USE_LOCALE_NUMERIC */
919 #endif /* USE_LOCALE */
925 #ifdef USE_LOCALE_COLLATE
928 * mem_collxfrm() is a bit like strxfrm() but with two important
929 * differences. First, it handles embedded NULs. Second, it allocates
930 * a bit more memory than needed for the transformed data itself.
931 * The real transformed data begins at offset sizeof(collationix).
932 * Please see sv_collxfrm() to see how this is used.
936 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
939 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
941 PERL_ARGS_ASSERT_MEM_COLLXFRM;
943 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
944 /* the +1 is for the terminating NUL. */
946 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
947 Newx(xbuf, xAlloc, char);
951 *(U32*)xbuf = PL_collation_ix;
952 xout = sizeof(PL_collation_ix);
953 for (xin = 0; xin < len; ) {
957 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
958 if (xused >= PERL_INT_MAX)
960 if ((STRLEN)xused < xAlloc - xout)
962 xAlloc = (2 * xAlloc) + 1;
963 Renew(xbuf, xAlloc, char);
968 xin += strlen(s + xin) + 1;
971 /* Embedded NULs are understood but silently skipped
972 * because they make no sense in locale collation. */
976 *xlen = xout - sizeof(PL_collation_ix);
985 #endif /* USE_LOCALE_COLLATE */
990 Perl__is_cur_LC_category_utf8(pTHX_ int category)
992 /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
993 * otherwise. 'category' may not be LC_ALL. If the platform doesn't have
994 * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence
995 * could give the wrong result. It errs on the side of not being a UTF-8
998 char *save_input_locale = NULL;
1002 assert(category != LC_ALL);
1005 /* First dispose of the trivial cases */
1006 save_input_locale = setlocale(category, NULL);
1007 if (! save_input_locale) {
1008 DEBUG_L(PerlIO_printf(Perl_debug_log,
1009 "Could not find current locale for category %d\n",
1011 return FALSE; /* XXX maybe should croak */
1013 save_input_locale = stdize_locale(savepv(save_input_locale));
1014 if ((*save_input_locale == 'C' && save_input_locale[1] == '\0')
1015 || strEQ(save_input_locale, "POSIX"))
1017 DEBUG_L(PerlIO_printf(Perl_debug_log,
1018 "Current locale for category %d is %s\n",
1019 category, save_input_locale));
1020 Safefree(save_input_locale);
1024 #if defined(USE_LOCALE_CTYPE) \
1025 && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET)))
1027 { /* Next try nl_langinfo or MB_CUR_MAX if available */
1029 char *save_ctype_locale = NULL;
1032 if (category != LC_CTYPE) { /* These work only on LC_CTYPE */
1034 /* Get the current LC_CTYPE locale */
1035 save_ctype_locale = stdize_locale(savepv(setlocale(LC_CTYPE, NULL)));
1036 if (! save_ctype_locale) {
1037 DEBUG_L(PerlIO_printf(Perl_debug_log,
1038 "Could not find current locale for LC_CTYPE\n"));
1039 goto cant_use_nllanginfo;
1042 /* If LC_CTYPE and the desired category use the same locale, this
1043 * means that finding the value for LC_CTYPE is the same as finding
1044 * the value for the desired category. Otherwise, switch LC_CTYPE
1045 * to the desired category's locale */
1046 if (strEQ(save_ctype_locale, save_input_locale)) {
1047 Safefree(save_ctype_locale);
1048 save_ctype_locale = NULL;
1050 else if (! setlocale(LC_CTYPE, save_input_locale)) {
1051 DEBUG_L(PerlIO_printf(Perl_debug_log,
1052 "Could not change LC_CTYPE locale to %s\n",
1053 save_input_locale));
1054 Safefree(save_ctype_locale);
1055 goto cant_use_nllanginfo;
1059 DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n",
1060 save_input_locale));
1062 /* Here the current LC_CTYPE is set to the locale of the category whose
1063 * information is desired. This means that nl_langinfo() and MB_CUR_MAX
1064 * should give the correct results */
1066 # if defined(HAS_NL_LANGINFO) && defined(CODESET)
1068 char *codeset = savepv(nl_langinfo(CODESET));
1069 if (codeset && strNE(codeset, "")) {
1071 /* If we switched LC_CTYPE, switch back */
1072 if (save_ctype_locale) {
1073 setlocale(LC_CTYPE, save_ctype_locale);
1074 Safefree(save_ctype_locale);
1077 is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8"))
1078 || foldEQ(codeset, STR_WITH_LEN("UTF8"));
1080 DEBUG_L(PerlIO_printf(Perl_debug_log,
1081 "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n",
1084 Safefree(save_input_locale);
1093 /* Here, either we don't have nl_langinfo, or it didn't return a
1094 * codeset. Try MB_CUR_MAX */
1096 /* Standard UTF-8 needs at least 4 bytes to represent the maximum
1097 * Unicode code point. Since UTF-8 is the only non-single byte
1098 * encoding we handle, we just say any such encoding is UTF-8, and if
1099 * turns out to be wrong, other things will fail */
1100 is_utf8 = MB_CUR_MAX >= 4;
1102 DEBUG_L(PerlIO_printf(Perl_debug_log,
1103 "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n",
1104 (int) MB_CUR_MAX, is_utf8));
1106 Safefree(save_input_locale);
1110 /* ... But, most system that have MB_CUR_MAX will also have mbtowc(),
1111 * since they are both in the C99 standard. We can feed a known byte
1112 * string to the latter function, and check that it gives the expected
1116 GCC_DIAG_IGNORE(-Wunused-result);
1117 (void) mbtowc(&wc, NULL, 0); /* Reset any shift state */
1120 if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
1121 != strlen(HYPHEN_UTF8)
1122 || wc != (wchar_t) 0x2010)
1125 DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", wc));
1126 DEBUG_L(PerlIO_printf(Perl_debug_log,
1127 "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n",
1128 mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno));
1133 /* If we switched LC_CTYPE, switch back */
1134 if (save_ctype_locale) {
1135 setlocale(LC_CTYPE, save_ctype_locale);
1136 Safefree(save_ctype_locale);
1143 cant_use_nllanginfo:
1145 #endif /* HAS_NL_LANGINFO etc */
1147 /* nl_langinfo not available or failed somehow. Look at the locale name to
1148 * see if it matches qr/UTF -? 8 /ix */
1150 final_pos = strlen(save_input_locale) - 1;
1151 if (final_pos >= 3) {
1152 char *name = save_input_locale;
1154 /* Find next 'U' or 'u' and look from there */
1155 while ((name += strcspn(name, "Uu") + 1)
1156 <= save_input_locale + final_pos - 2)
1158 if (toFOLD(*(name)) != 't'
1159 || toFOLD(*(name + 1)) != 'f')
1164 if (*(name) == '-') {
1165 if ((name > save_input_locale + final_pos - 1)) {
1170 if (*(name) == '8') {
1171 Safefree(save_input_locale);
1172 DEBUG_L(PerlIO_printf(Perl_debug_log,
1173 "Locale %s ends with UTF-8 in name\n",
1174 save_input_locale));
1178 DEBUG_L(PerlIO_printf(Perl_debug_log,
1179 "Locale %s doesn't end with UTF-8 in name\n",
1180 save_input_locale));
1184 /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
1186 && *(save_input_locale + final_pos - 0) == '1'
1187 && *(save_input_locale + final_pos - 1) == '0'
1188 && *(save_input_locale + final_pos - 2) == '0'
1189 && *(save_input_locale + final_pos - 3) == '5'
1190 && *(save_input_locale + final_pos - 4) == '6')
1192 DEBUG_L(PerlIO_printf(Perl_debug_log,
1193 "Locale %s ends with 10056 in name, is UTF-8 locale\n",
1194 save_input_locale));
1195 Safefree(save_input_locale);
1200 /* Other common encodings are the ISO 8859 series, which aren't UTF-8 */
1201 if (instr(save_input_locale, "8859")) {
1202 DEBUG_L(PerlIO_printf(Perl_debug_log,
1203 "Locale %s has 8859 in name, not UTF-8 locale\n",
1204 save_input_locale));
1205 Safefree(save_input_locale);
1209 #ifdef HAS_LOCALECONV
1211 # ifdef USE_LOCALE_MONETARY
1213 /* Here, there is nothing in the locale name to indicate whether the locale
1214 * is UTF-8 or not. This "name", the return of setlocale(), is actually
1215 * defined to be opaque, so we can't really rely on the absence of various
1216 * substrings in the name to indicate its UTF-8ness. Look at the locale's
1217 * currency symbol. Often that will be in the native script, and if the
1218 * symbol isn't in UTF-8, we know that the locale isn't. If it is
1219 * non-ASCII UTF-8, we infer that the locale is too.
1220 * To do this, like above for LC_CTYPE, we first set LC_MONETARY to the
1221 * locale of the desired category, if it isn't that locale already */
1224 char *save_monetary_locale = NULL;
1225 bool illegal_utf8 = FALSE;
1226 bool only_ascii = FALSE;
1227 const struct lconv* const lc = localeconv();
1229 if (category != LC_MONETARY) {
1231 save_monetary_locale = stdize_locale(savepv(setlocale(LC_MONETARY,
1233 if (! save_monetary_locale) {
1234 DEBUG_L(PerlIO_printf(Perl_debug_log,
1235 "Could not find current locale for LC_MONETARY\n"));
1236 goto cant_use_monetary;
1239 if (strNE(save_monetary_locale, save_input_locale)) {
1240 if (! setlocale(LC_MONETARY, save_input_locale)) {
1241 DEBUG_L(PerlIO_printf(Perl_debug_log,
1242 "Could not change LC_MONETARY locale to %s\n",
1243 save_input_locale));
1244 Safefree(save_monetary_locale);
1245 goto cant_use_monetary;
1250 /* Here the current LC_MONETARY is set to the locale of the category
1251 * whose information is desired. */
1253 if (lc && lc->currency_symbol) {
1254 if (! is_utf8_string((U8 *) lc->currency_symbol, 0)) {
1255 DEBUG_L(PerlIO_printf(Perl_debug_log,
1256 "Currency symbol for %s is not legal UTF-8\n",
1257 save_input_locale));
1258 illegal_utf8 = TRUE;
1260 else if (is_ascii_string((U8 *) lc->currency_symbol, 0)) {
1261 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));
1266 /* If we changed it, restore LC_MONETARY to its original locale */
1267 if (save_monetary_locale) {
1268 setlocale(LC_MONETARY, save_monetary_locale);
1269 Safefree(save_monetary_locale);
1272 Safefree(save_input_locale);
1274 /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; otherwise
1275 * assume the locale is UTF-8 if and only if the symbol is non-ascii
1276 * UTF-8. (We can't really tell if the locale is UTF-8 or not if the
1277 * symbol is just a '$', so we err on the side of it not being UTF-8)
1279 DEBUG_L(PerlIO_printf(Perl_debug_log, "\tis_utf8=%d\n", (illegal_utf8)
1282 return (illegal_utf8)
1289 # endif /* USE_LOCALE_MONETARY */
1290 #endif /* HAS_LOCALECONV */
1292 #if 0 && defined(HAS_STRERROR) && defined(USE_LOCALE_MESSAGES)
1294 /* This code is ifdefd out because it was found to not be necessary in testing
1295 * on our dromedary test machine, which has over 700 locales. There, looking
1296 * at just the currency symbol gave essentially the same results as doing this
1297 * extra work. Executing this also caused segfaults in miniperl. I left it in
1298 * so as to avoid rewriting it if real-world experience indicates that
1299 * dromedary is an outlier. Essentially, instead of returning abpve if we
1300 * haven't found illegal utf8, we continue on and examine all the strerror()
1301 * messages on the platform for utf8ness. If all are ASCII, we still don't
1302 * know the answer; but otherwise we have a pretty good indication of the
1303 * utf8ness. The reason this doesn't necessarily help much is that the
1304 * messages may not have been translated into the locale. The currency symbol
1305 * is much more likely to have been translated. The code below would need to
1306 * be altered somewhat to just be a continuation of testing the currency
1309 unsigned int failures = 0, non_ascii = 0;
1310 char *save_messages_locale = NULL;
1312 /* Like above for LC_CTYPE, we set LC_MESSAGES to the locale of the
1313 * desired category, if it isn't that locale already */
1315 if (category != LC_MESSAGES) {
1317 save_messages_locale = stdize_locale(savepv(setlocale(LC_MESSAGES,
1319 if (! save_messages_locale) {
1320 goto cant_use_messages;
1323 if (strEQ(save_messages_locale, save_input_locale)) {
1324 Safefree(save_input_locale);
1326 else if (! setlocale(LC_MESSAGES, save_input_locale)) {
1327 Safefree(save_messages_locale);
1328 goto cant_use_messages;
1332 /* Here the current LC_MESSAGES is set to the locale of the category
1333 * whose information is desired. Look through all the messages */
1336 #ifdef HAS_SYS_ERRLIST
1341 const U8* const errmsg = (U8 *) Strerror(e) ;
1344 if (! is_utf8_string(errmsg, 0)) {
1348 else if (! is_ascii_string(errmsg, 0)) {
1353 /* And, if we changed it, restore LC_MESSAGES to its original locale */
1354 if (save_messages_locale) {
1355 setlocale(LC_MESSAGES, save_messages_locale);
1356 Safefree(save_messages_locale);
1359 /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid,
1360 * any non-ascii means it is one; otherwise we assume it isn't */
1361 return (failures) ? FALSE : non_ascii;
1368 DEBUG_L(PerlIO_printf(Perl_debug_log,
1369 "Assuming locale %s is not a UTF-8 locale\n",
1370 save_input_locale));
1371 Safefree(save_input_locale);
1379 Perl__is_in_locale_category(pTHX_ const bool compiling, const int category)
1382 /* Internal function which returns if we are in the scope of a pragma that
1383 * enables the locale category 'category'. 'compiling' should indicate if
1384 * this is during the compilation phase (TRUE) or not (FALSE). */
1386 const COP * const cop = (compiling) ? &PL_compiling : PL_curcop;
1388 SV *categories = cop_hints_fetch_pvs(cop, "locale", 0);
1389 if (! categories || categories == &PL_sv_placeholder) {
1393 /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get
1394 * a valid unsigned */
1395 assert(category >= -1);
1396 return cBOOL(SvUV(categories) & (1U << (category + 1)));
1400 Perl_my_strerror(pTHX_ const int errnum) {
1402 /* Uses C locale for the error text unless within scope of 'use locale' for
1405 #ifdef USE_LOCALE_MESSAGES
1406 if (! IN_LC(LC_MESSAGES)) {
1407 char * save_locale = setlocale(LC_MESSAGES, NULL);
1408 if (! ((*save_locale == 'C' && save_locale[1] == '\0')
1409 || strEQ(save_locale, "POSIX")))
1413 /* The next setlocale likely will zap this, so create a copy */
1414 save_locale = savepv(save_locale);
1416 setlocale(LC_MESSAGES, "C");
1418 /* This points to the static space in Strerror, with all its
1420 errstr = Strerror(errnum);
1422 setlocale(LC_MESSAGES, save_locale);
1423 Safefree(save_locale);
1429 return Strerror(errnum);
1434 * c-indentation-style: bsd
1436 * indent-tabs-mode: nil
1439 * ex: set ts=8 sts=4 sw=4 et: