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
36 # include <langinfo.h>
41 #if defined(USE_LOCALE_NUMERIC) || defined(USE_LOCALE_COLLATE)
43 * Standardize the locale name from a string returned by 'setlocale'.
45 * The standard return value of setlocale() is either
46 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
47 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
48 * (the space-separated values represent the various sublocales,
49 * in some unspecified order)
51 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
52 * which is harmful for further use of the string in setlocale().
56 S_stdize_locale(pTHX_ char *locs)
58 const char * const s = strchr(locs, '=');
61 PERL_ARGS_ASSERT_STDIZE_LOCALE;
64 const char * const t = strchr(s, '.');
67 const char * const u = strchr(t, '\n');
68 if (u && (u[1] == 0)) {
69 const STRLEN len = u - s;
70 Move(s + 1, locs, len, char);
78 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
85 Perl_set_numeric_radix(pTHX)
87 #ifdef USE_LOCALE_NUMERIC
89 # ifdef HAS_LOCALECONV
90 const struct lconv* const lc = localeconv();
92 if (lc && lc->decimal_point) {
93 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
94 SvREFCNT_dec(PL_numeric_radix_sv);
95 PL_numeric_radix_sv = NULL;
98 if (PL_numeric_radix_sv)
99 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
101 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
105 PL_numeric_radix_sv = NULL;
106 # endif /* HAS_LOCALECONV */
107 #endif /* USE_LOCALE_NUMERIC */
111 * Set up for a new numeric locale.
114 Perl_new_numeric(pTHX_ const char *newnum)
116 #ifdef USE_LOCALE_NUMERIC
120 Safefree(PL_numeric_name);
121 PL_numeric_name = NULL;
122 PL_numeric_standard = TRUE;
123 PL_numeric_local = TRUE;
127 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
128 Safefree(PL_numeric_name);
129 PL_numeric_name = stdize_locale(savepv(newnum));
130 PL_numeric_standard = ((*newnum == 'C' && newnum[1] == '\0')
131 || strEQ(newnum, "POSIX"));
132 PL_numeric_local = TRUE;
136 #endif /* USE_LOCALE_NUMERIC */
140 Perl_set_numeric_standard(pTHX)
142 #ifdef USE_LOCALE_NUMERIC
145 if (! PL_numeric_standard) {
146 setlocale(LC_NUMERIC, "C");
147 PL_numeric_standard = TRUE;
148 PL_numeric_local = FALSE;
152 #endif /* USE_LOCALE_NUMERIC */
156 Perl_set_numeric_local(pTHX)
158 #ifdef USE_LOCALE_NUMERIC
161 if (! PL_numeric_local) {
162 setlocale(LC_NUMERIC, PL_numeric_name);
163 PL_numeric_standard = FALSE;
164 PL_numeric_local = TRUE;
168 #endif /* USE_LOCALE_NUMERIC */
172 * Set up for a new ctype locale.
175 Perl_new_ctype(pTHX_ const char *newctype)
177 #ifdef USE_LOCALE_CTYPE
181 PERL_ARGS_ASSERT_NEW_CTYPE;
183 for (i = 0; i < 256; i++) {
185 PL_fold_locale[i] = toLOWER_LC(i);
186 else if (isLOWER_LC(i))
187 PL_fold_locale[i] = toUPPER_LC(i);
189 PL_fold_locale[i] = i;
192 #endif /* USE_LOCALE_CTYPE */
193 PERL_ARGS_ASSERT_NEW_CTYPE;
194 PERL_UNUSED_ARG(newctype);
199 * Set up for a new collation locale.
202 Perl_new_collate(pTHX_ const char *newcoll)
204 #ifdef USE_LOCALE_COLLATE
208 if (PL_collation_name) {
210 Safefree(PL_collation_name);
211 PL_collation_name = NULL;
213 PL_collation_standard = TRUE;
214 PL_collxfrm_base = 0;
215 PL_collxfrm_mult = 2;
219 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
221 Safefree(PL_collation_name);
222 PL_collation_name = stdize_locale(savepv(newcoll));
223 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
224 || strEQ(newcoll, "POSIX"));
227 /* 2: at most so many chars ('a', 'b'). */
228 /* 50: surely no system expands a char more. */
229 #define XFRMBUFSIZE (2 * 50)
230 char xbuf[XFRMBUFSIZE];
231 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
232 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
233 const SSize_t mult = fb - fa;
235 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
237 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
238 PL_collxfrm_mult = mult;
242 #endif /* USE_LOCALE_COLLATE */
246 * Initialize locale awareness.
249 Perl_init_i18nl10n(pTHX_ int printwarn)
253 * 1 = set ok or not applicable,
254 * 0 = fallback to C locale,
255 * -1 = fallback to C locale failed
258 #if defined(USE_LOCALE)
261 #ifdef USE_LOCALE_CTYPE
262 char *curctype = NULL;
263 #endif /* USE_LOCALE_CTYPE */
264 #ifdef USE_LOCALE_COLLATE
265 char *curcoll = NULL;
266 #endif /* USE_LOCALE_COLLATE */
267 #ifdef USE_LOCALE_NUMERIC
269 #endif /* USE_LOCALE_NUMERIC */
271 char * const language = PerlEnv_getenv("LANGUAGE");
273 char * const lc_all = PerlEnv_getenv("LC_ALL");
274 char * const lang = PerlEnv_getenv("LANG");
275 bool setlocale_failure = FALSE;
277 #ifdef LOCALE_ENVIRON_REQUIRED
280 * Ultrix setlocale(..., "") fails if there are no environment
281 * variables from which to get a locale name.
288 if (setlocale(LC_ALL, ""))
291 setlocale_failure = TRUE;
293 if (!setlocale_failure) {
294 #ifdef USE_LOCALE_CTYPE
298 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
300 setlocale_failure = TRUE;
302 curctype = savepv(curctype);
303 #endif /* USE_LOCALE_CTYPE */
304 #ifdef USE_LOCALE_COLLATE
307 setlocale(LC_COLLATE,
308 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
310 setlocale_failure = TRUE;
312 curcoll = savepv(curcoll);
313 #endif /* USE_LOCALE_COLLATE */
314 #ifdef USE_LOCALE_NUMERIC
317 setlocale(LC_NUMERIC,
318 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
320 setlocale_failure = TRUE;
322 curnum = savepv(curnum);
323 #endif /* USE_LOCALE_NUMERIC */
328 #endif /* !LOCALE_ENVIRON_REQUIRED */
331 if (! setlocale(LC_ALL, ""))
332 setlocale_failure = TRUE;
335 if (!setlocale_failure) {
336 #ifdef USE_LOCALE_CTYPE
338 if (! (curctype = setlocale(LC_CTYPE, "")))
339 setlocale_failure = TRUE;
341 curctype = savepv(curctype);
342 #endif /* USE_LOCALE_CTYPE */
343 #ifdef USE_LOCALE_COLLATE
345 if (! (curcoll = setlocale(LC_COLLATE, "")))
346 setlocale_failure = TRUE;
348 curcoll = savepv(curcoll);
349 #endif /* USE_LOCALE_COLLATE */
350 #ifdef USE_LOCALE_NUMERIC
352 if (! (curnum = setlocale(LC_NUMERIC, "")))
353 setlocale_failure = TRUE;
355 curnum = savepv(curnum);
356 #endif /* USE_LOCALE_NUMERIC */
359 if (setlocale_failure) {
361 const bool locwarn = (printwarn > 1 ||
363 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
368 PerlIO_printf(Perl_error_log,
369 "perl: warning: Setting locale failed.\n");
373 PerlIO_printf(Perl_error_log,
374 "perl: warning: Setting locale failed for the categories:\n\t");
375 #ifdef USE_LOCALE_CTYPE
377 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
378 #endif /* USE_LOCALE_CTYPE */
379 #ifdef USE_LOCALE_COLLATE
381 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
382 #endif /* USE_LOCALE_COLLATE */
383 #ifdef USE_LOCALE_NUMERIC
385 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
386 #endif /* USE_LOCALE_NUMERIC */
387 PerlIO_printf(Perl_error_log, "\n");
391 PerlIO_printf(Perl_error_log,
392 "perl: warning: Please check that your locale settings:\n");
395 PerlIO_printf(Perl_error_log,
396 "\tLANGUAGE = %c%s%c,\n",
397 language ? '"' : '(',
398 language ? language : "unset",
399 language ? '"' : ')');
402 PerlIO_printf(Perl_error_log,
403 "\tLC_ALL = %c%s%c,\n",
405 lc_all ? lc_all : "unset",
408 #if defined(USE_ENVIRON_ARRAY)
411 for (e = environ; *e; e++) {
412 if (strnEQ(*e, "LC_", 3)
413 && strnNE(*e, "LC_ALL=", 7)
414 && (p = strchr(*e, '=')))
415 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
416 (int)(p - *e), *e, p + 1);
420 PerlIO_printf(Perl_error_log,
421 "\t(possibly more locale environment variables)\n");
424 PerlIO_printf(Perl_error_log,
427 lang ? lang : "unset",
430 PerlIO_printf(Perl_error_log,
431 " are supported and installed on your system.\n");
436 if (setlocale(LC_ALL, "C")) {
438 PerlIO_printf(Perl_error_log,
439 "perl: warning: Falling back to the standard locale (\"C\").\n");
444 PerlIO_printf(Perl_error_log,
445 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
452 #ifdef USE_LOCALE_CTYPE
453 || !(curctype || setlocale(LC_CTYPE, "C"))
454 #endif /* USE_LOCALE_CTYPE */
455 #ifdef USE_LOCALE_COLLATE
456 || !(curcoll || setlocale(LC_COLLATE, "C"))
457 #endif /* USE_LOCALE_COLLATE */
458 #ifdef USE_LOCALE_NUMERIC
459 || !(curnum || setlocale(LC_NUMERIC, "C"))
460 #endif /* USE_LOCALE_NUMERIC */
464 PerlIO_printf(Perl_error_log,
465 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
469 #endif /* ! LC_ALL */
471 #ifdef USE_LOCALE_CTYPE
473 curctype = savepv(setlocale(LC_CTYPE, NULL));
474 #endif /* USE_LOCALE_CTYPE */
475 #ifdef USE_LOCALE_COLLATE
477 curcoll = savepv(setlocale(LC_COLLATE, NULL));
478 #endif /* USE_LOCALE_COLLATE */
479 #ifdef USE_LOCALE_NUMERIC
481 curnum = savepv(setlocale(LC_NUMERIC, NULL));
482 #endif /* USE_LOCALE_NUMERIC */
486 #ifdef USE_LOCALE_CTYPE
488 #endif /* USE_LOCALE_CTYPE */
490 #ifdef USE_LOCALE_COLLATE
491 new_collate(curcoll);
492 #endif /* USE_LOCALE_COLLATE */
494 #ifdef USE_LOCALE_NUMERIC
496 #endif /* USE_LOCALE_NUMERIC */
500 #endif /* USE_LOCALE */
504 /* Set PL_utf8locale to TRUE if using PerlIO _and_
505 any of the following are true:
506 - nl_langinfo(CODESET) contains /^utf-?8/i
507 - $ENV{LC_ALL} contains /^utf-?8/i
508 - $ENV{LC_CTYPE} contains /^utf-?8/i
509 - $ENV{LANG} contains /^utf-?8/i
510 The LC_ALL, LC_CTYPE, LANG obey the usual override
511 hierarchy of locale environment variables. (LANGUAGE
512 affects only LC_MESSAGES only under glibc.) (If present,
513 it overrides LC_MESSAGES for GNU gettext, and it also
514 can have more than one locale, separated by spaces,
515 in case you need to know.)
516 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
517 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
518 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
520 bool utf8locale = FALSE;
521 char *codeset = NULL;
522 #if defined(HAS_NL_LANGINFO) && defined(CODESET)
523 codeset = nl_langinfo(CODESET);
526 utf8locale = (foldEQ(codeset, STR_WITH_LEN("UTF-8"))
527 || foldEQ(codeset, STR_WITH_LEN("UTF8") ));
528 #if defined(USE_LOCALE)
529 else { /* nl_langinfo(CODESET) is supposed to correctly
530 * interpret the locale environment variables,
531 * but just in case it fails, let's do this manually. */
533 utf8locale = (foldEQ(lang, STR_WITH_LEN("UTF-8"))
534 || foldEQ(lang, STR_WITH_LEN("UTF8") ));
535 #ifdef USE_LOCALE_CTYPE
537 utf8locale = (foldEQ(curctype, STR_WITH_LEN("UTF-8"))
538 || foldEQ(curctype, STR_WITH_LEN("UTF8") ));
541 utf8locale = (foldEQ(lc_all, STR_WITH_LEN("UTF-8"))
542 || foldEQ(lc_all, STR_WITH_LEN("UTF8") ));
544 #endif /* USE_LOCALE */
546 PL_utf8locale = TRUE;
548 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
549 This is an alternative to using the -C command line switch
550 (the -C if present will override this). */
552 const char *p = PerlEnv_getenv("PERL_UNICODE");
553 PL_unicode = p ? parse_unicode_opts(&p) : 0;
554 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
559 #ifdef USE_LOCALE_CTYPE
561 #endif /* USE_LOCALE_CTYPE */
562 #ifdef USE_LOCALE_COLLATE
564 #endif /* USE_LOCALE_COLLATE */
565 #ifdef USE_LOCALE_NUMERIC
567 #endif /* USE_LOCALE_NUMERIC */
571 #ifdef USE_LOCALE_COLLATE
574 * mem_collxfrm() is a bit like strxfrm() but with two important
575 * differences. First, it handles embedded NULs. Second, it allocates
576 * a bit more memory than needed for the transformed data itself.
577 * The real transformed data begins at offset sizeof(collationix).
578 * Please see sv_collxfrm() to see how this is used.
582 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
586 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
588 PERL_ARGS_ASSERT_MEM_COLLXFRM;
590 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
591 /* the +1 is for the terminating NUL. */
593 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
594 Newx(xbuf, xAlloc, char);
598 *(U32*)xbuf = PL_collation_ix;
599 xout = sizeof(PL_collation_ix);
600 for (xin = 0; xin < len; ) {
604 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
605 if (xused >= PERL_INT_MAX)
607 if ((STRLEN)xused < xAlloc - xout)
609 xAlloc = (2 * xAlloc) + 1;
610 Renew(xbuf, xAlloc, char);
615 xin += strlen(s + xin) + 1;
618 /* Embedded NULs are understood but silently skipped
619 * because they make no sense in locale collation. */
623 *xlen = xout - sizeof(PL_collation_ix);
632 #endif /* USE_LOCALE_COLLATE */
636 * c-indentation-style: bsd
638 * indent-tabs-mode: t
641 * ex: set ts=8 sts=4 sw=4 noet: