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.
28 #define PERL_IN_LOCALE_C
32 # include <langinfo.h>
37 #if defined(USE_LOCALE_NUMERIC) || defined(USE_LOCALE_COLLATE)
39 * Standardize the locale name from a string returned by 'setlocale'.
41 * The typical return value of setlocale() is either
42 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
43 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
44 * (the space-separated values represent the various sublocales,
45 * in some unspecified order). This is not handled by this function.
47 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
48 * which is harmful for further use of the string in setlocale(). This
49 * function removes the trailing new line and everything up through the '='
53 S_stdize_locale(pTHX_ char *locs)
55 const char * const s = strchr(locs, '=');
58 PERL_ARGS_ASSERT_STDIZE_LOCALE;
61 const char * const t = strchr(s, '.');
64 const char * const u = strchr(t, '\n');
65 if (u && (u[1] == 0)) {
66 const STRLEN len = u - s;
67 Move(s + 1, locs, len, char);
75 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
82 Perl_set_numeric_radix(pTHX)
84 #ifdef USE_LOCALE_NUMERIC
86 # ifdef HAS_LOCALECONV
87 const struct lconv* const lc = localeconv();
89 if (lc && lc->decimal_point) {
90 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
91 SvREFCNT_dec(PL_numeric_radix_sv);
92 PL_numeric_radix_sv = NULL;
95 if (PL_numeric_radix_sv)
96 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
98 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
102 PL_numeric_radix_sv = NULL;
103 # endif /* HAS_LOCALECONV */
104 #endif /* USE_LOCALE_NUMERIC */
108 * Set up for a new numeric locale.
111 Perl_new_numeric(pTHX_ const char *newnum)
113 #ifdef USE_LOCALE_NUMERIC
118 Safefree(PL_numeric_name);
119 PL_numeric_name = NULL;
120 PL_numeric_standard = TRUE;
121 PL_numeric_local = TRUE;
125 save_newnum = stdize_locale(savepv(newnum));
126 if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) {
127 Safefree(PL_numeric_name);
128 PL_numeric_name = save_newnum;
129 PL_numeric_standard = ((*save_newnum == 'C' && save_newnum[1] == '\0')
130 || strEQ(save_newnum, "POSIX"));
131 PL_numeric_local = TRUE;
135 Safefree(save_newnum);
138 #endif /* USE_LOCALE_NUMERIC */
142 Perl_set_numeric_standard(pTHX)
144 #ifdef USE_LOCALE_NUMERIC
147 if (! PL_numeric_standard) {
148 setlocale(LC_NUMERIC, "C");
149 PL_numeric_standard = TRUE;
150 PL_numeric_local = FALSE;
154 #endif /* USE_LOCALE_NUMERIC */
158 Perl_set_numeric_local(pTHX)
160 #ifdef USE_LOCALE_NUMERIC
163 if (! PL_numeric_local) {
164 setlocale(LC_NUMERIC, PL_numeric_name);
165 PL_numeric_standard = FALSE;
166 PL_numeric_local = TRUE;
170 #endif /* USE_LOCALE_NUMERIC */
174 * Set up for a new ctype locale.
177 Perl_new_ctype(pTHX_ const char *newctype)
179 #ifdef USE_LOCALE_CTYPE
183 PERL_ARGS_ASSERT_NEW_CTYPE;
185 for (i = 0; i < 256; i++) {
187 PL_fold_locale[i] = toLOWER_LC(i);
188 else if (isLOWER_LC(i))
189 PL_fold_locale[i] = toUPPER_LC(i);
191 PL_fold_locale[i] = i;
194 #endif /* USE_LOCALE_CTYPE */
195 PERL_ARGS_ASSERT_NEW_CTYPE;
196 PERL_UNUSED_ARG(newctype);
201 * Set up for a new collation locale.
204 Perl_new_collate(pTHX_ const char *newcoll)
206 #ifdef USE_LOCALE_COLLATE
210 if (PL_collation_name) {
212 Safefree(PL_collation_name);
213 PL_collation_name = NULL;
215 PL_collation_standard = TRUE;
216 PL_collxfrm_base = 0;
217 PL_collxfrm_mult = 2;
221 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
223 Safefree(PL_collation_name);
224 PL_collation_name = stdize_locale(savepv(newcoll));
225 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
226 || strEQ(newcoll, "POSIX"));
229 /* 2: at most so many chars ('a', 'b'). */
230 /* 50: surely no system expands a char more. */
231 #define XFRMBUFSIZE (2 * 50)
232 char xbuf[XFRMBUFSIZE];
233 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
234 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
235 const SSize_t mult = fb - fa;
236 if (mult < 1 && !(fa == 0 && fb == 0))
237 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
239 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
240 PL_collxfrm_mult = mult;
244 #endif /* USE_LOCALE_COLLATE */
248 * Initialize locale awareness.
251 Perl_init_i18nl10n(pTHX_ int printwarn)
255 * 1 = set ok or not applicable,
256 * 0 = fallback to C locale,
257 * -1 = fallback to C locale failed
260 #if defined(USE_LOCALE)
263 #ifdef USE_LOCALE_CTYPE
264 char *curctype = NULL;
265 #endif /* USE_LOCALE_CTYPE */
266 #ifdef USE_LOCALE_COLLATE
267 char *curcoll = NULL;
268 #endif /* USE_LOCALE_COLLATE */
269 #ifdef USE_LOCALE_NUMERIC
271 #endif /* USE_LOCALE_NUMERIC */
273 char * const language = PerlEnv_getenv("LANGUAGE");
275 char * const lc_all = PerlEnv_getenv("LC_ALL");
276 char * const lang = PerlEnv_getenv("LANG");
277 bool setlocale_failure = FALSE;
279 #ifdef LOCALE_ENVIRON_REQUIRED
282 * Ultrix setlocale(..., "") fails if there are no environment
283 * variables from which to get a locale name.
290 if (setlocale(LC_ALL, ""))
293 setlocale_failure = TRUE;
295 if (!setlocale_failure) {
296 # ifdef USE_LOCALE_CTYPE
300 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
302 setlocale_failure = TRUE;
304 curctype = savepv(curctype);
305 # endif /* USE_LOCALE_CTYPE */
306 # ifdef USE_LOCALE_COLLATE
309 setlocale(LC_COLLATE,
310 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
312 setlocale_failure = TRUE;
314 curcoll = savepv(curcoll);
315 # endif /* USE_LOCALE_COLLATE */
316 # ifdef USE_LOCALE_NUMERIC
319 setlocale(LC_NUMERIC,
320 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
322 setlocale_failure = TRUE;
324 curnum = savepv(curnum);
325 # endif /* USE_LOCALE_NUMERIC */
330 #endif /* !LOCALE_ENVIRON_REQUIRED */
333 if (! setlocale(LC_ALL, ""))
334 setlocale_failure = TRUE;
337 if (!setlocale_failure) {
338 #ifdef USE_LOCALE_CTYPE
340 if (! (curctype = setlocale(LC_CTYPE, "")))
341 setlocale_failure = TRUE;
343 curctype = savepv(curctype);
344 #endif /* USE_LOCALE_CTYPE */
345 #ifdef USE_LOCALE_COLLATE
347 if (! (curcoll = setlocale(LC_COLLATE, "")))
348 setlocale_failure = TRUE;
350 curcoll = savepv(curcoll);
351 #endif /* USE_LOCALE_COLLATE */
352 #ifdef USE_LOCALE_NUMERIC
354 if (! (curnum = setlocale(LC_NUMERIC, "")))
355 setlocale_failure = TRUE;
357 curnum = savepv(curnum);
358 #endif /* USE_LOCALE_NUMERIC */
361 if (setlocale_failure) {
363 const bool locwarn = (printwarn > 1 ||
365 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
370 PerlIO_printf(Perl_error_log,
371 "perl: warning: Setting locale failed.\n");
375 PerlIO_printf(Perl_error_log,
376 "perl: warning: Setting locale failed for the categories:\n\t");
377 #ifdef USE_LOCALE_CTYPE
379 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
380 #endif /* USE_LOCALE_CTYPE */
381 #ifdef USE_LOCALE_COLLATE
383 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
384 #endif /* USE_LOCALE_COLLATE */
385 #ifdef USE_LOCALE_NUMERIC
387 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
388 #endif /* USE_LOCALE_NUMERIC */
389 PerlIO_printf(Perl_error_log, "\n");
393 PerlIO_printf(Perl_error_log,
394 "perl: warning: Please check that your locale settings:\n");
397 PerlIO_printf(Perl_error_log,
398 "\tLANGUAGE = %c%s%c,\n",
399 language ? '"' : '(',
400 language ? language : "unset",
401 language ? '"' : ')');
404 PerlIO_printf(Perl_error_log,
405 "\tLC_ALL = %c%s%c,\n",
407 lc_all ? lc_all : "unset",
410 #if defined(USE_ENVIRON_ARRAY)
413 for (e = environ; *e; e++) {
414 if (strnEQ(*e, "LC_", 3)
415 && strnNE(*e, "LC_ALL=", 7)
416 && (p = strchr(*e, '=')))
417 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
418 (int)(p - *e), *e, p + 1);
422 PerlIO_printf(Perl_error_log,
423 "\t(possibly more locale environment variables)\n");
426 PerlIO_printf(Perl_error_log,
429 lang ? lang : "unset",
432 PerlIO_printf(Perl_error_log,
433 " are supported and installed on your system.\n");
438 if (setlocale(LC_ALL, "C")) {
440 PerlIO_printf(Perl_error_log,
441 "perl: warning: Falling back to the standard locale (\"C\").\n");
446 PerlIO_printf(Perl_error_log,
447 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
454 #ifdef USE_LOCALE_CTYPE
455 || !(curctype || setlocale(LC_CTYPE, "C"))
456 #endif /* USE_LOCALE_CTYPE */
457 #ifdef USE_LOCALE_COLLATE
458 || !(curcoll || setlocale(LC_COLLATE, "C"))
459 #endif /* USE_LOCALE_COLLATE */
460 #ifdef USE_LOCALE_NUMERIC
461 || !(curnum || setlocale(LC_NUMERIC, "C"))
462 #endif /* USE_LOCALE_NUMERIC */
466 PerlIO_printf(Perl_error_log,
467 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
471 #endif /* ! LC_ALL */
473 #ifdef USE_LOCALE_CTYPE
475 curctype = savepv(setlocale(LC_CTYPE, NULL));
476 #endif /* USE_LOCALE_CTYPE */
477 #ifdef USE_LOCALE_COLLATE
479 curcoll = savepv(setlocale(LC_COLLATE, NULL));
480 #endif /* USE_LOCALE_COLLATE */
481 #ifdef USE_LOCALE_NUMERIC
483 curnum = savepv(setlocale(LC_NUMERIC, NULL));
484 #endif /* USE_LOCALE_NUMERIC */
488 #ifdef USE_LOCALE_CTYPE
490 #endif /* USE_LOCALE_CTYPE */
492 #ifdef USE_LOCALE_COLLATE
493 new_collate(curcoll);
494 #endif /* USE_LOCALE_COLLATE */
496 #ifdef USE_LOCALE_NUMERIC
498 #endif /* USE_LOCALE_NUMERIC */
502 #endif /* USE_LOCALE */
506 /* Set PL_utf8locale to TRUE if using PerlIO _and_
507 any of the following are true:
508 - nl_langinfo(CODESET) contains /^utf-?8/i
509 - $ENV{LC_ALL} contains /^utf-?8/i
510 - $ENV{LC_CTYPE} contains /^utf-?8/i
511 - $ENV{LANG} contains /^utf-?8/i
512 The LC_ALL, LC_CTYPE, LANG obey the usual override
513 hierarchy of locale environment variables. (LANGUAGE
514 affects only LC_MESSAGES only under glibc.) (If present,
515 it overrides LC_MESSAGES for GNU gettext, and it also
516 can have more than one locale, separated by spaces,
517 in case you need to know.)
518 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
519 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
520 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
522 bool utf8locale = FALSE;
523 char *codeset = NULL;
524 #if defined(HAS_NL_LANGINFO) && defined(CODESET)
525 codeset = nl_langinfo(CODESET);
528 utf8locale = (foldEQ(codeset, STR_WITH_LEN("UTF-8"))
529 || foldEQ(codeset, STR_WITH_LEN("UTF8") ));
530 #if defined(USE_LOCALE)
531 else { /* nl_langinfo(CODESET) is supposed to correctly
532 * interpret the locale environment variables,
533 * but just in case it fails, let's do this manually. */
535 utf8locale = (foldEQ(lang, STR_WITH_LEN("UTF-8"))
536 || foldEQ(lang, STR_WITH_LEN("UTF8") ));
537 #ifdef USE_LOCALE_CTYPE
539 utf8locale = (foldEQ(curctype, STR_WITH_LEN("UTF-8"))
540 || foldEQ(curctype, STR_WITH_LEN("UTF8") ));
543 utf8locale = (foldEQ(lc_all, STR_WITH_LEN("UTF-8"))
544 || foldEQ(lc_all, STR_WITH_LEN("UTF8") ));
546 #endif /* USE_LOCALE */
548 PL_utf8locale = TRUE;
550 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
551 This is an alternative to using the -C command line switch
552 (the -C if present will override this). */
554 const char *p = PerlEnv_getenv("PERL_UNICODE");
555 PL_unicode = p ? parse_unicode_opts(&p) : 0;
556 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
561 #ifdef USE_LOCALE_CTYPE
563 #endif /* USE_LOCALE_CTYPE */
564 #ifdef USE_LOCALE_COLLATE
566 #endif /* USE_LOCALE_COLLATE */
567 #ifdef USE_LOCALE_NUMERIC
569 #endif /* USE_LOCALE_NUMERIC */
573 #ifdef USE_LOCALE_COLLATE
576 * mem_collxfrm() is a bit like strxfrm() but with two important
577 * differences. First, it handles embedded NULs. Second, it allocates
578 * a bit more memory than needed for the transformed data itself.
579 * The real transformed data begins at offset sizeof(collationix).
580 * Please see sv_collxfrm() to see how this is used.
584 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
588 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
590 PERL_ARGS_ASSERT_MEM_COLLXFRM;
592 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
593 /* the +1 is for the terminating NUL. */
595 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
596 Newx(xbuf, xAlloc, char);
600 *(U32*)xbuf = PL_collation_ix;
601 xout = sizeof(PL_collation_ix);
602 for (xin = 0; xin < len; ) {
606 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
607 if (xused >= PERL_INT_MAX)
609 if ((STRLEN)xused < xAlloc - xout)
611 xAlloc = (2 * xAlloc) + 1;
612 Renew(xbuf, xAlloc, char);
617 xin += strlen(s + xin) + 1;
620 /* Embedded NULs are understood but silently skipped
621 * because they make no sense in locale collation. */
625 *xlen = xout - sizeof(PL_collation_ix);
634 #endif /* USE_LOCALE_COLLATE */
638 * c-indentation-style: bsd
640 * indent-tabs-mode: nil
643 * ex: set ts=8 sts=4 sw=4 et: