3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2005, 2006, 2007, 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!
21 /* utility functions for handling locale-specific stuff like what
22 * character represents the decimal point.
26 #define PERL_IN_LOCALE_C
34 # include <langinfo.h>
39 #if defined(USE_LOCALE_NUMERIC) || defined(USE_LOCALE_COLLATE)
41 * Standardize the locale name from a string returned by 'setlocale'.
43 * The standard return value of setlocale() is either
44 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
45 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
46 * (the space-separated values represent the various sublocales,
47 * in some unspecificed order)
49 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
50 * which is harmful for further use of the string in setlocale().
54 S_stdize_locale(pTHX_ char *locs)
56 const char * const s = strchr(locs, '=');
59 PERL_ARGS_ASSERT_STDIZE_LOCALE;
62 const char * const t = strchr(s, '.');
65 const char * const u = strchr(t, '\n');
66 if (u && (u[1] == 0)) {
67 const STRLEN len = u - s;
68 Move(s + 1, locs, len, char);
76 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
83 Perl_set_numeric_radix(pTHX)
85 #ifdef USE_LOCALE_NUMERIC
87 # ifdef HAS_LOCALECONV
88 const struct lconv* const lc = localeconv();
90 if (lc && lc->decimal_point) {
91 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
92 SvREFCNT_dec(PL_numeric_radix_sv);
93 PL_numeric_radix_sv = NULL;
96 if (PL_numeric_radix_sv)
97 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
99 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
103 PL_numeric_radix_sv = NULL;
104 # endif /* HAS_LOCALECONV */
105 #endif /* USE_LOCALE_NUMERIC */
109 * Set up for a new numeric locale.
112 Perl_new_numeric(pTHX_ const char *newnum)
114 #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 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
126 Safefree(PL_numeric_name);
127 PL_numeric_name = stdize_locale(savepv(newnum));
128 PL_numeric_standard = ((*newnum == 'C' && newnum[1] == '\0')
129 || strEQ(newnum, "POSIX"));
130 PL_numeric_local = TRUE;
134 #endif /* USE_LOCALE_NUMERIC */
138 Perl_set_numeric_standard(pTHX)
140 #ifdef USE_LOCALE_NUMERIC
143 if (! PL_numeric_standard) {
144 setlocale(LC_NUMERIC, "C");
145 PL_numeric_standard = TRUE;
146 PL_numeric_local = FALSE;
150 #endif /* USE_LOCALE_NUMERIC */
154 Perl_set_numeric_local(pTHX)
156 #ifdef USE_LOCALE_NUMERIC
159 if (! PL_numeric_local) {
160 setlocale(LC_NUMERIC, PL_numeric_name);
161 PL_numeric_standard = FALSE;
162 PL_numeric_local = TRUE;
166 #endif /* USE_LOCALE_NUMERIC */
170 * Set up for a new ctype locale.
173 Perl_new_ctype(pTHX_ const char *newctype)
175 #ifdef USE_LOCALE_CTYPE
179 PERL_ARGS_ASSERT_NEW_CTYPE;
181 for (i = 0; i < 256; i++) {
183 PL_fold_locale[i] = toLOWER_LC(i);
184 else if (isLOWER_LC(i))
185 PL_fold_locale[i] = toUPPER_LC(i);
187 PL_fold_locale[i] = i;
190 #endif /* USE_LOCALE_CTYPE */
191 PERL_ARGS_ASSERT_NEW_CTYPE;
192 PERL_UNUSED_ARG(newctype);
197 * Set up for a new collation locale.
200 Perl_new_collate(pTHX_ const char *newcoll)
202 #ifdef USE_LOCALE_COLLATE
206 if (PL_collation_name) {
208 Safefree(PL_collation_name);
209 PL_collation_name = NULL;
211 PL_collation_standard = TRUE;
212 PL_collxfrm_base = 0;
213 PL_collxfrm_mult = 2;
217 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
219 Safefree(PL_collation_name);
220 PL_collation_name = stdize_locale(savepv(newcoll));
221 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
222 || strEQ(newcoll, "POSIX"));
225 /* 2: at most so many chars ('a', 'b'). */
226 /* 50: surely no system expands a char more. */
227 #define XFRMBUFSIZE (2 * 50)
228 char xbuf[XFRMBUFSIZE];
229 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
230 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
231 const SSize_t mult = fb - fa;
233 Perl_croak(aTHX_ "strxfrm() gets absurd");
234 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
235 PL_collxfrm_mult = mult;
239 #endif /* USE_LOCALE_COLLATE */
243 * Initialize locale awareness.
246 Perl_init_i18nl10n(pTHX_ int printwarn)
250 * 1 = set ok or not applicable,
251 * 0 = fallback to C locale,
252 * -1 = fallback to C locale failed
255 #if defined(USE_LOCALE)
258 #ifdef USE_LOCALE_CTYPE
259 char *curctype = NULL;
260 #endif /* USE_LOCALE_CTYPE */
261 #ifdef USE_LOCALE_COLLATE
262 char *curcoll = NULL;
263 #endif /* USE_LOCALE_COLLATE */
264 #ifdef USE_LOCALE_NUMERIC
266 #endif /* USE_LOCALE_NUMERIC */
268 char * const language = PerlEnv_getenv("LANGUAGE");
270 char * const lc_all = PerlEnv_getenv("LC_ALL");
271 char * const lang = PerlEnv_getenv("LANG");
272 bool setlocale_failure = FALSE;
274 #ifdef LOCALE_ENVIRON_REQUIRED
277 * Ultrix setlocale(..., "") fails if there are no environment
278 * variables from which to get a locale name.
285 if (setlocale(LC_ALL, ""))
288 setlocale_failure = TRUE;
290 if (!setlocale_failure) {
291 #ifdef USE_LOCALE_CTYPE
295 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
297 setlocale_failure = TRUE;
299 curctype = savepv(curctype);
300 #endif /* USE_LOCALE_CTYPE */
301 #ifdef USE_LOCALE_COLLATE
304 setlocale(LC_COLLATE,
305 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
307 setlocale_failure = TRUE;
309 curcoll = savepv(curcoll);
310 #endif /* USE_LOCALE_COLLATE */
311 #ifdef USE_LOCALE_NUMERIC
314 setlocale(LC_NUMERIC,
315 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
317 setlocale_failure = TRUE;
319 curnum = savepv(curnum);
320 #endif /* USE_LOCALE_NUMERIC */
325 #endif /* !LOCALE_ENVIRON_REQUIRED */
328 if (! setlocale(LC_ALL, ""))
329 setlocale_failure = TRUE;
332 if (!setlocale_failure) {
333 #ifdef USE_LOCALE_CTYPE
335 if (! (curctype = setlocale(LC_CTYPE, "")))
336 setlocale_failure = TRUE;
338 curctype = savepv(curctype);
339 #endif /* USE_LOCALE_CTYPE */
340 #ifdef USE_LOCALE_COLLATE
342 if (! (curcoll = setlocale(LC_COLLATE, "")))
343 setlocale_failure = TRUE;
345 curcoll = savepv(curcoll);
346 #endif /* USE_LOCALE_COLLATE */
347 #ifdef USE_LOCALE_NUMERIC
349 if (! (curnum = setlocale(LC_NUMERIC, "")))
350 setlocale_failure = TRUE;
352 curnum = savepv(curnum);
353 #endif /* USE_LOCALE_NUMERIC */
356 if (setlocale_failure) {
358 const bool locwarn = (printwarn > 1 ||
360 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
365 PerlIO_printf(Perl_error_log,
366 "perl: warning: Setting locale failed.\n");
370 PerlIO_printf(Perl_error_log,
371 "perl: warning: Setting locale failed for the categories:\n\t");
372 #ifdef USE_LOCALE_CTYPE
374 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
375 #endif /* USE_LOCALE_CTYPE */
376 #ifdef USE_LOCALE_COLLATE
378 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
379 #endif /* USE_LOCALE_COLLATE */
380 #ifdef USE_LOCALE_NUMERIC
382 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
383 #endif /* USE_LOCALE_NUMERIC */
384 PerlIO_printf(Perl_error_log, "\n");
388 PerlIO_printf(Perl_error_log,
389 "perl: warning: Please check that your locale settings:\n");
392 PerlIO_printf(Perl_error_log,
393 "\tLANGUAGE = %c%s%c,\n",
394 language ? '"' : '(',
395 language ? language : "unset",
396 language ? '"' : ')');
399 PerlIO_printf(Perl_error_log,
400 "\tLC_ALL = %c%s%c,\n",
402 lc_all ? lc_all : "unset",
405 #if defined(USE_ENVIRON_ARRAY)
408 for (e = environ; *e; e++) {
409 if (strnEQ(*e, "LC_", 3)
410 && strnNE(*e, "LC_ALL=", 7)
411 && (p = strchr(*e, '=')))
412 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
413 (int)(p - *e), *e, p + 1);
417 PerlIO_printf(Perl_error_log,
418 "\t(possibly more locale environment variables)\n");
421 PerlIO_printf(Perl_error_log,
424 lang ? lang : "unset",
427 PerlIO_printf(Perl_error_log,
428 " are supported and installed on your system.\n");
433 if (setlocale(LC_ALL, "C")) {
435 PerlIO_printf(Perl_error_log,
436 "perl: warning: Falling back to the standard locale (\"C\").\n");
441 PerlIO_printf(Perl_error_log,
442 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
449 #ifdef USE_LOCALE_CTYPE
450 || !(curctype || setlocale(LC_CTYPE, "C"))
451 #endif /* USE_LOCALE_CTYPE */
452 #ifdef USE_LOCALE_COLLATE
453 || !(curcoll || setlocale(LC_COLLATE, "C"))
454 #endif /* USE_LOCALE_COLLATE */
455 #ifdef USE_LOCALE_NUMERIC
456 || !(curnum || setlocale(LC_NUMERIC, "C"))
457 #endif /* USE_LOCALE_NUMERIC */
461 PerlIO_printf(Perl_error_log,
462 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
466 #endif /* ! LC_ALL */
468 #ifdef USE_LOCALE_CTYPE
470 curctype = savepv(setlocale(LC_CTYPE, NULL));
471 #endif /* USE_LOCALE_CTYPE */
472 #ifdef USE_LOCALE_COLLATE
474 curcoll = savepv(setlocale(LC_COLLATE, NULL));
475 #endif /* USE_LOCALE_COLLATE */
476 #ifdef USE_LOCALE_NUMERIC
478 curnum = savepv(setlocale(LC_NUMERIC, NULL));
479 #endif /* USE_LOCALE_NUMERIC */
483 #ifdef USE_LOCALE_CTYPE
485 #endif /* USE_LOCALE_CTYPE */
487 #ifdef USE_LOCALE_COLLATE
488 new_collate(curcoll);
489 #endif /* USE_LOCALE_COLLATE */
491 #ifdef USE_LOCALE_NUMERIC
493 #endif /* USE_LOCALE_NUMERIC */
497 #endif /* USE_LOCALE */
501 /* Set PL_utf8locale to TRUE if using PerlIO _and_
502 any of the following are true:
503 - nl_langinfo(CODESET) contains /^utf-?8/i
504 - $ENV{LC_ALL} contains /^utf-?8/i
505 - $ENV{LC_CTYPE} contains /^utf-?8/i
506 - $ENV{LANG} contains /^utf-?8/i
507 The LC_ALL, LC_CTYPE, LANG obey the usual override
508 hierarchy of locale environment variables. (LANGUAGE
509 affects only LC_MESSAGES only under glibc.) (If present,
510 it overrides LC_MESSAGES for GNU gettext, and it also
511 can have more than one locale, separated by spaces,
512 in case you need to know.)
513 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
514 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
515 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
517 bool utf8locale = FALSE;
518 char *codeset = NULL;
519 #if defined(HAS_NL_LANGINFO) && defined(CODESET)
520 codeset = nl_langinfo(CODESET);
523 utf8locale = (Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF-8")) == 0 ||
524 Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF8") ) == 0);
525 #if defined(USE_LOCALE)
526 else { /* nl_langinfo(CODESET) is supposed to correctly
527 * interpret the locale environment variables,
528 * but just in case it fails, let's do this manually. */
530 utf8locale = (Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF-8")) == 0 ||
531 Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF8") ) == 0);
532 #ifdef USE_LOCALE_CTYPE
534 utf8locale = (Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF-8")) == 0 ||
535 Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF8") ) == 0);
538 utf8locale = (Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF-8")) == 0 ||
539 Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF8") ) == 0);
541 #endif /* USE_LOCALE */
543 PL_utf8locale = TRUE;
545 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
546 This is an alternative to using the -C command line switch
547 (the -C if present will override this). */
549 const char *p = PerlEnv_getenv("PERL_UNICODE");
550 PL_unicode = p ? parse_unicode_opts(&p) : 0;
551 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
556 #ifdef USE_LOCALE_CTYPE
558 #endif /* USE_LOCALE_CTYPE */
559 #ifdef USE_LOCALE_COLLATE
561 #endif /* USE_LOCALE_COLLATE */
562 #ifdef USE_LOCALE_NUMERIC
564 #endif /* USE_LOCALE_NUMERIC */
568 #ifdef USE_LOCALE_COLLATE
571 * mem_collxfrm() is a bit like strxfrm() but with two important
572 * differences. First, it handles embedded NULs. Second, it allocates
573 * a bit more memory than needed for the transformed data itself.
574 * The real transformed data begins at offset sizeof(collationix).
575 * Please see sv_collxfrm() to see how this is used.
579 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
583 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
585 PERL_ARGS_ASSERT_MEM_COLLXFRM;
587 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
588 /* the +1 is for the terminating NUL. */
590 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
591 Newx(xbuf, xAlloc, char);
595 *(U32*)xbuf = PL_collation_ix;
596 xout = sizeof(PL_collation_ix);
597 for (xin = 0; xin < len; ) {
601 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
602 if (xused >= PERL_INT_MAX)
604 if ((STRLEN)xused < xAlloc - xout)
606 xAlloc = (2 * xAlloc) + 1;
607 Renew(xbuf, xAlloc, char);
612 xin += strlen(s + xin) + 1;
615 /* Embedded NULs are understood but silently skipped
616 * because they make no sense in locale collation. */
620 *xlen = xout - sizeof(PL_collation_ix);
629 #endif /* USE_LOCALE_COLLATE */
633 * c-indentation-style: bsd
635 * indent-tabs-mode: t
638 * ex: set ts=8 sts=4 sw=4 noet: