3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2005, 2006, 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, '=');
60 const char * const t = strchr(s, '.');
63 const char * const u = strchr(t, '\n');
64 if (u && (u[1] == 0)) {
65 const STRLEN len = u - s;
66 Move(s + 1, locs, len, char);
74 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
81 Perl_set_numeric_radix(pTHX)
83 #ifdef USE_LOCALE_NUMERIC
85 # ifdef HAS_LOCALECONV
86 const struct lconv* const lc = localeconv();
88 if (lc && lc->decimal_point) {
89 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
90 SvREFCNT_dec(PL_numeric_radix_sv);
91 PL_numeric_radix_sv = NULL;
94 if (PL_numeric_radix_sv)
95 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
97 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
101 PL_numeric_radix_sv = NULL;
102 # endif /* HAS_LOCALECONV */
103 #endif /* USE_LOCALE_NUMERIC */
107 * Set up for a new numeric locale.
110 Perl_new_numeric(pTHX_ const char *newnum)
112 #ifdef USE_LOCALE_NUMERIC
116 Safefree(PL_numeric_name);
117 PL_numeric_name = NULL;
118 PL_numeric_standard = TRUE;
119 PL_numeric_local = TRUE;
123 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
124 Safefree(PL_numeric_name);
125 PL_numeric_name = stdize_locale(savepv(newnum));
126 PL_numeric_standard = ((*newnum == 'C' && newnum[1] == '\0')
127 || strEQ(newnum, "POSIX"));
128 PL_numeric_local = TRUE;
132 #endif /* USE_LOCALE_NUMERIC */
136 Perl_set_numeric_standard(pTHX)
138 #ifdef USE_LOCALE_NUMERIC
141 if (! PL_numeric_standard) {
142 setlocale(LC_NUMERIC, "C");
143 PL_numeric_standard = TRUE;
144 PL_numeric_local = FALSE;
148 #endif /* USE_LOCALE_NUMERIC */
152 Perl_set_numeric_local(pTHX)
154 #ifdef USE_LOCALE_NUMERIC
157 if (! PL_numeric_local) {
158 setlocale(LC_NUMERIC, PL_numeric_name);
159 PL_numeric_standard = FALSE;
160 PL_numeric_local = TRUE;
164 #endif /* USE_LOCALE_NUMERIC */
168 * Set up for a new ctype locale.
171 Perl_new_ctype(pTHX_ const char *newctype)
173 #ifdef USE_LOCALE_CTYPE
177 for (i = 0; i < 256; i++) {
179 PL_fold_locale[i] = toLOWER_LC(i);
180 else if (isLOWER_LC(i))
181 PL_fold_locale[i] = toUPPER_LC(i);
183 PL_fold_locale[i] = i;
186 #endif /* USE_LOCALE_CTYPE */
187 PERL_UNUSED_ARG(newctype);
192 * Set up for a new collation locale.
195 Perl_new_collate(pTHX_ const char *newcoll)
197 #ifdef USE_LOCALE_COLLATE
201 if (PL_collation_name) {
203 Safefree(PL_collation_name);
204 PL_collation_name = NULL;
206 PL_collation_standard = TRUE;
207 PL_collxfrm_base = 0;
208 PL_collxfrm_mult = 2;
212 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
214 Safefree(PL_collation_name);
215 PL_collation_name = stdize_locale(savepv(newcoll));
216 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
217 || strEQ(newcoll, "POSIX"));
220 /* 2: at most so many chars ('a', 'b'). */
221 /* 50: surely no system expands a char more. */
222 #define XFRMBUFSIZE (2 * 50)
223 char xbuf[XFRMBUFSIZE];
224 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
225 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
226 const SSize_t mult = fb - fa;
228 Perl_croak(aTHX_ "strxfrm() gets absurd");
229 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
230 PL_collxfrm_mult = mult;
234 #endif /* USE_LOCALE_COLLATE */
238 * Initialize locale awareness.
241 Perl_init_i18nl10n(pTHX_ int printwarn)
245 * 1 = set ok or not applicable,
246 * 0 = fallback to C locale,
247 * -1 = fallback to C locale failed
250 #if defined(USE_LOCALE)
253 #ifdef USE_LOCALE_CTYPE
254 char *curctype = NULL;
255 #endif /* USE_LOCALE_CTYPE */
256 #ifdef USE_LOCALE_COLLATE
257 char *curcoll = NULL;
258 #endif /* USE_LOCALE_COLLATE */
259 #ifdef USE_LOCALE_NUMERIC
261 #endif /* USE_LOCALE_NUMERIC */
263 char * const language = PerlEnv_getenv("LANGUAGE");
265 char * const lc_all = PerlEnv_getenv("LC_ALL");
266 char * const lang = PerlEnv_getenv("LANG");
267 bool setlocale_failure = FALSE;
269 #ifdef LOCALE_ENVIRON_REQUIRED
272 * Ultrix setlocale(..., "") fails if there are no environment
273 * variables from which to get a locale name.
280 if (setlocale(LC_ALL, ""))
283 setlocale_failure = TRUE;
285 if (!setlocale_failure) {
286 #ifdef USE_LOCALE_CTYPE
289 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
291 setlocale_failure = TRUE;
293 curctype = savepv(curctype);
294 #endif /* USE_LOCALE_CTYPE */
295 #ifdef USE_LOCALE_COLLATE
297 setlocale(LC_COLLATE,
298 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
300 setlocale_failure = TRUE;
302 curcoll = savepv(curcoll);
303 #endif /* USE_LOCALE_COLLATE */
304 #ifdef USE_LOCALE_NUMERIC
306 setlocale(LC_NUMERIC,
307 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
309 setlocale_failure = TRUE;
311 curnum = savepv(curnum);
312 #endif /* USE_LOCALE_NUMERIC */
317 #endif /* !LOCALE_ENVIRON_REQUIRED */
320 if (! setlocale(LC_ALL, ""))
321 setlocale_failure = TRUE;
324 if (!setlocale_failure) {
325 #ifdef USE_LOCALE_CTYPE
327 if (! (curctype = setlocale(LC_CTYPE, "")))
328 setlocale_failure = TRUE;
330 curctype = savepv(curctype);
331 #endif /* USE_LOCALE_CTYPE */
332 #ifdef USE_LOCALE_COLLATE
334 if (! (curcoll = setlocale(LC_COLLATE, "")))
335 setlocale_failure = TRUE;
337 curcoll = savepv(curcoll);
338 #endif /* USE_LOCALE_COLLATE */
339 #ifdef USE_LOCALE_NUMERIC
341 if (! (curnum = setlocale(LC_NUMERIC, "")))
342 setlocale_failure = TRUE;
344 curnum = savepv(curnum);
345 #endif /* USE_LOCALE_NUMERIC */
348 if (setlocale_failure) {
350 const bool locwarn = (printwarn > 1 ||
352 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
357 PerlIO_printf(Perl_error_log,
358 "perl: warning: Setting locale failed.\n");
362 PerlIO_printf(Perl_error_log,
363 "perl: warning: Setting locale failed for the categories:\n\t");
364 #ifdef USE_LOCALE_CTYPE
366 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
367 #endif /* USE_LOCALE_CTYPE */
368 #ifdef USE_LOCALE_COLLATE
370 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
371 #endif /* USE_LOCALE_COLLATE */
372 #ifdef USE_LOCALE_NUMERIC
374 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
375 #endif /* USE_LOCALE_NUMERIC */
376 PerlIO_printf(Perl_error_log, "\n");
380 PerlIO_printf(Perl_error_log,
381 "perl: warning: Please check that your locale settings:\n");
384 PerlIO_printf(Perl_error_log,
385 "\tLANGUAGE = %c%s%c,\n",
386 language ? '"' : '(',
387 language ? language : "unset",
388 language ? '"' : ')');
391 PerlIO_printf(Perl_error_log,
392 "\tLC_ALL = %c%s%c,\n",
394 lc_all ? lc_all : "unset",
397 #if defined(USE_ENVIRON_ARRAY)
400 for (e = environ; *e; e++) {
401 if (strnEQ(*e, "LC_", 3)
402 && strnNE(*e, "LC_ALL=", 7)
403 && (p = strchr(*e, '=')))
404 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
405 (int)(p - *e), *e, p + 1);
409 PerlIO_printf(Perl_error_log,
410 "\t(possibly more locale environment variables)\n");
413 PerlIO_printf(Perl_error_log,
416 lang ? lang : "unset",
419 PerlIO_printf(Perl_error_log,
420 " are supported and installed on your system.\n");
425 if (setlocale(LC_ALL, "C")) {
427 PerlIO_printf(Perl_error_log,
428 "perl: warning: Falling back to the standard locale (\"C\").\n");
433 PerlIO_printf(Perl_error_log,
434 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
441 #ifdef USE_LOCALE_CTYPE
442 || !(curctype || setlocale(LC_CTYPE, "C"))
443 #endif /* USE_LOCALE_CTYPE */
444 #ifdef USE_LOCALE_COLLATE
445 || !(curcoll || setlocale(LC_COLLATE, "C"))
446 #endif /* USE_LOCALE_COLLATE */
447 #ifdef USE_LOCALE_NUMERIC
448 || !(curnum || setlocale(LC_NUMERIC, "C"))
449 #endif /* USE_LOCALE_NUMERIC */
453 PerlIO_printf(Perl_error_log,
454 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
458 #endif /* ! LC_ALL */
460 #ifdef USE_LOCALE_CTYPE
461 curctype = savepv(setlocale(LC_CTYPE, NULL));
462 #endif /* USE_LOCALE_CTYPE */
463 #ifdef USE_LOCALE_COLLATE
464 curcoll = savepv(setlocale(LC_COLLATE, NULL));
465 #endif /* USE_LOCALE_COLLATE */
466 #ifdef USE_LOCALE_NUMERIC
467 curnum = savepv(setlocale(LC_NUMERIC, NULL));
468 #endif /* USE_LOCALE_NUMERIC */
472 #ifdef USE_LOCALE_CTYPE
474 #endif /* USE_LOCALE_CTYPE */
476 #ifdef USE_LOCALE_COLLATE
477 new_collate(curcoll);
478 #endif /* USE_LOCALE_COLLATE */
480 #ifdef USE_LOCALE_NUMERIC
482 #endif /* USE_LOCALE_NUMERIC */
486 #endif /* USE_LOCALE */
490 /* Set PL_utf8locale to TRUE if using PerlIO _and_
491 any of the following are true:
492 - nl_langinfo(CODESET) contains /^utf-?8/i
493 - $ENV{LC_ALL} contains /^utf-?8/i
494 - $ENV{LC_CTYPE} contains /^utf-?8/i
495 - $ENV{LANG} contains /^utf-?8/i
496 The LC_ALL, LC_CTYPE, LANG obey the usual override
497 hierarchy of locale environment variables. (LANGUAGE
498 affects only LC_MESSAGES only under glibc.) (If present,
499 it overrides LC_MESSAGES for GNU gettext, and it also
500 can have more than one locale, separated by spaces,
501 in case you need to know.)
502 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
503 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
504 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
506 bool utf8locale = FALSE;
507 char *codeset = NULL;
508 #if defined(HAS_NL_LANGINFO) && defined(CODESET)
509 codeset = nl_langinfo(CODESET);
512 utf8locale = (Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF-8")) == 0 ||
513 Perl_ibcmp(aTHX_ codeset, STR_WITH_LEN("UTF8") ) == 0);
514 #if defined(USE_LOCALE)
515 else { /* nl_langinfo(CODESET) is supposed to correctly
516 * interpret the locale environment variables,
517 * but just in case it fails, let's do this manually. */
519 utf8locale = (Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF-8")) == 0 ||
520 Perl_ibcmp(aTHX_ lang, STR_WITH_LEN("UTF8") ) == 0);
521 #ifdef USE_LOCALE_CTYPE
523 utf8locale = (Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF-8")) == 0 ||
524 Perl_ibcmp(aTHX_ curctype, STR_WITH_LEN("UTF8") ) == 0);
527 utf8locale = (Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF-8")) == 0 ||
528 Perl_ibcmp(aTHX_ lc_all, STR_WITH_LEN("UTF8") ) == 0);
530 #endif /* USE_LOCALE */
532 PL_utf8locale = TRUE;
534 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
535 This is an alternative to using the -C command line switch
536 (the -C if present will override this). */
538 const char *p = PerlEnv_getenv("PERL_UNICODE");
539 PL_unicode = p ? parse_unicode_opts(&p) : 0;
543 #ifdef USE_LOCALE_CTYPE
545 #endif /* USE_LOCALE_CTYPE */
546 #ifdef USE_LOCALE_COLLATE
548 #endif /* USE_LOCALE_COLLATE */
549 #ifdef USE_LOCALE_NUMERIC
551 #endif /* USE_LOCALE_NUMERIC */
555 #ifdef USE_LOCALE_COLLATE
558 * mem_collxfrm() is a bit like strxfrm() but with two important
559 * differences. First, it handles embedded NULs. Second, it allocates
560 * a bit more memory than needed for the transformed data itself.
561 * The real transformed data begins at offset sizeof(collationix).
562 * Please see sv_collxfrm() to see how this is used.
566 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
570 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
572 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
573 /* the +1 is for the terminating NUL. */
575 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
576 Newx(xbuf, xAlloc, char);
580 *(U32*)xbuf = PL_collation_ix;
581 xout = sizeof(PL_collation_ix);
582 for (xin = 0; xin < len; ) {
586 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
589 if ((STRLEN)xused < xAlloc - xout)
591 xAlloc = (2 * xAlloc) + 1;
592 Renew(xbuf, xAlloc, char);
597 xin += strlen(s + xin) + 1;
600 /* Embedded NULs are understood but silently skipped
601 * because they make no sense in locale collation. */
605 *xlen = xout - sizeof(PL_collation_ix);
614 #endif /* USE_LOCALE_COLLATE */
618 * c-indentation-style: bsd
620 * indent-tabs-mode: t
623 * ex: set ts=8 sts=4 sw=4 noet: