3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2005, 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>
40 * Standardize the locale name from a string returned by 'setlocale'.
42 * The standard return value of setlocale() is either
43 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
44 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
45 * (the space-separated values represent the various sublocales,
46 * in some unspecificed order)
48 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
49 * which is harmful for further use of the string in setlocale().
53 S_stdize_locale(pTHX_ char *locs)
58 if ((s = strchr(locs, '='))) {
62 if ((t = strchr(s, '.'))) {
65 if ((u = strchr(t, '\n'))) {
69 Move(s + 1, locs, len, char);
78 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
84 Perl_set_numeric_radix(pTHX)
86 #ifdef USE_LOCALE_NUMERIC
87 # ifdef HAS_LOCALECONV
91 if (lc && lc->decimal_point) {
92 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
93 SvREFCNT_dec(PL_numeric_radix_sv);
94 PL_numeric_radix_sv = Nullsv;
97 if (PL_numeric_radix_sv)
98 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
100 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
104 PL_numeric_radix_sv = Nullsv;
105 # endif /* HAS_LOCALECONV */
106 #endif /* USE_LOCALE_NUMERIC */
110 * Set up for a new numeric locale.
113 Perl_new_numeric(pTHX_ char *newnum)
115 #ifdef USE_LOCALE_NUMERIC
118 if (PL_numeric_name) {
119 Safefree(PL_numeric_name);
120 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
144 if (! PL_numeric_standard) {
145 setlocale(LC_NUMERIC, "C");
146 PL_numeric_standard = TRUE;
147 PL_numeric_local = FALSE;
151 #endif /* USE_LOCALE_NUMERIC */
155 Perl_set_numeric_local(pTHX)
157 #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_ char *newctype)
175 #ifdef USE_LOCALE_CTYPE
179 for (i = 0; i < 256; i++) {
181 PL_fold_locale[i] = toLOWER_LC(i);
182 else if (isLOWER_LC(i))
183 PL_fold_locale[i] = toUPPER_LC(i);
185 PL_fold_locale[i] = i;
188 #endif /* USE_LOCALE_CTYPE */
193 * Set up for a new collation locale.
196 Perl_new_collate(pTHX_ char *newcoll)
198 #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 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
225 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
226 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)
252 #ifdef USE_LOCALE_CTYPE
253 char *curctype = NULL;
254 #endif /* USE_LOCALE_CTYPE */
255 #ifdef USE_LOCALE_COLLATE
256 char *curcoll = NULL;
257 #endif /* USE_LOCALE_COLLATE */
258 #ifdef USE_LOCALE_NUMERIC
260 #endif /* USE_LOCALE_NUMERIC */
262 char *language = PerlEnv_getenv("LANGUAGE");
264 char *lc_all = PerlEnv_getenv("LC_ALL");
265 char *lang = PerlEnv_getenv("LANG");
266 bool setlocale_failure = FALSE;
268 #ifdef LOCALE_ENVIRON_REQUIRED
271 * Ultrix setlocale(..., "") fails if there are no environment
272 * variables from which to get a locale name.
279 if (setlocale(LC_ALL, ""))
282 setlocale_failure = TRUE;
284 if (!setlocale_failure) {
285 #ifdef USE_LOCALE_CTYPE
288 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
290 setlocale_failure = TRUE;
292 curctype = savepv(curctype);
293 #endif /* USE_LOCALE_CTYPE */
294 #ifdef USE_LOCALE_COLLATE
296 setlocale(LC_COLLATE,
297 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
299 setlocale_failure = TRUE;
301 curcoll = savepv(curcoll);
302 #endif /* USE_LOCALE_COLLATE */
303 #ifdef USE_LOCALE_NUMERIC
305 setlocale(LC_NUMERIC,
306 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
308 setlocale_failure = TRUE;
310 curnum = savepv(curnum);
311 #endif /* USE_LOCALE_NUMERIC */
316 #endif /* !LOCALE_ENVIRON_REQUIRED */
319 if (! setlocale(LC_ALL, ""))
320 setlocale_failure = TRUE;
323 if (!setlocale_failure) {
324 #ifdef USE_LOCALE_CTYPE
325 if (! (curctype = setlocale(LC_CTYPE, "")))
326 setlocale_failure = TRUE;
328 curctype = savepv(curctype);
329 #endif /* USE_LOCALE_CTYPE */
330 #ifdef USE_LOCALE_COLLATE
331 if (! (curcoll = setlocale(LC_COLLATE, "")))
332 setlocale_failure = TRUE;
334 curcoll = savepv(curcoll);
335 #endif /* USE_LOCALE_COLLATE */
336 #ifdef USE_LOCALE_NUMERIC
337 if (! (curnum = setlocale(LC_NUMERIC, "")))
338 setlocale_failure = TRUE;
340 curnum = savepv(curnum);
341 #endif /* USE_LOCALE_NUMERIC */
344 if (setlocale_failure) {
346 bool locwarn = (printwarn > 1 ||
348 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
353 PerlIO_printf(Perl_error_log,
354 "perl: warning: Setting locale failed.\n");
358 PerlIO_printf(Perl_error_log,
359 "perl: warning: Setting locale failed for the categories:\n\t");
360 #ifdef USE_LOCALE_CTYPE
362 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
363 #endif /* USE_LOCALE_CTYPE */
364 #ifdef USE_LOCALE_COLLATE
366 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
367 #endif /* USE_LOCALE_COLLATE */
368 #ifdef USE_LOCALE_NUMERIC
370 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
371 #endif /* USE_LOCALE_NUMERIC */
372 PerlIO_printf(Perl_error_log, "\n");
376 PerlIO_printf(Perl_error_log,
377 "perl: warning: Please check that your locale settings:\n");
380 PerlIO_printf(Perl_error_log,
381 "\tLANGUAGE = %c%s%c,\n",
382 language ? '"' : '(',
383 language ? language : "unset",
384 language ? '"' : ')');
387 PerlIO_printf(Perl_error_log,
388 "\tLC_ALL = %c%s%c,\n",
390 lc_all ? lc_all : "unset",
393 #if defined(USE_ENVIRON_ARRAY)
396 for (e = environ; *e; e++) {
397 if (strnEQ(*e, "LC_", 3)
398 && strnNE(*e, "LC_ALL=", 7)
399 && (p = strchr(*e, '=')))
400 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
401 (int)(p - *e), *e, p + 1);
405 PerlIO_printf(Perl_error_log,
406 "\t(possibly more locale environment variables)\n");
409 PerlIO_printf(Perl_error_log,
412 lang ? lang : "unset",
415 PerlIO_printf(Perl_error_log,
416 " are supported and installed on your system.\n");
421 if (setlocale(LC_ALL, "C")) {
423 PerlIO_printf(Perl_error_log,
424 "perl: warning: Falling back to the standard locale (\"C\").\n");
429 PerlIO_printf(Perl_error_log,
430 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
437 #ifdef USE_LOCALE_CTYPE
438 || !(curctype || setlocale(LC_CTYPE, "C"))
439 #endif /* USE_LOCALE_CTYPE */
440 #ifdef USE_LOCALE_COLLATE
441 || !(curcoll || setlocale(LC_COLLATE, "C"))
442 #endif /* USE_LOCALE_COLLATE */
443 #ifdef USE_LOCALE_NUMERIC
444 || !(curnum || setlocale(LC_NUMERIC, "C"))
445 #endif /* USE_LOCALE_NUMERIC */
449 PerlIO_printf(Perl_error_log,
450 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
454 #endif /* ! LC_ALL */
456 #ifdef USE_LOCALE_CTYPE
457 curctype = savepv(setlocale(LC_CTYPE, Nullch));
458 #endif /* USE_LOCALE_CTYPE */
459 #ifdef USE_LOCALE_COLLATE
460 curcoll = savepv(setlocale(LC_COLLATE, Nullch));
461 #endif /* USE_LOCALE_COLLATE */
462 #ifdef USE_LOCALE_NUMERIC
463 curnum = savepv(setlocale(LC_NUMERIC, Nullch));
464 #endif /* USE_LOCALE_NUMERIC */
468 #ifdef USE_LOCALE_CTYPE
470 #endif /* USE_LOCALE_CTYPE */
472 #ifdef USE_LOCALE_COLLATE
473 new_collate(curcoll);
474 #endif /* USE_LOCALE_COLLATE */
476 #ifdef USE_LOCALE_NUMERIC
478 #endif /* USE_LOCALE_NUMERIC */
482 #endif /* USE_LOCALE */
486 /* Set PL_utf8locale to TRUE if using PerlIO _and_
487 any of the following are true:
488 - nl_langinfo(CODESET) contains /^utf-?8/i
489 - $ENV{LC_ALL} contains /^utf-?8/i
490 - $ENV{LC_CTYPE} contains /^utf-?8/i
491 - $ENV{LANG} contains /^utf-?8/i
492 The LC_ALL, LC_CTYPE, LANG obey the usual override
493 hierarchy of locale environment variables. (LANGUAGE
494 affects only LC_MESSAGES only under glibc.) (If present,
495 it overrides LC_MESSAGES for GNU gettext, and it also
496 can have more than one locale, separated by spaces,
497 in case you need to know.)
498 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
499 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
500 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
502 bool utf8locale = FALSE;
503 char *codeset = NULL;
504 #if defined(HAS_NL_LANGINFO) && defined(CODESET)
505 codeset = nl_langinfo(CODESET);
508 utf8locale = (ibcmp(codeset, "UTF-8", 5) == 0 ||
509 ibcmp(codeset, "UTF8", 4) == 0);
510 #if defined(USE_LOCALE)
511 else { /* nl_langinfo(CODESET) is supposed to correctly
512 * interpret the locale environment variables,
513 * but just in case it fails, let's do this manually. */
515 utf8locale = (ibcmp(lang, "UTF-8", 5) == 0 ||
516 ibcmp(lang, "UTF8", 4) == 0);
517 #ifdef USE_LOCALE_CTYPE
519 utf8locale = (ibcmp(curctype, "UTF-8", 5) == 0 ||
520 ibcmp(curctype, "UTF8", 4) == 0);
523 utf8locale = (ibcmp(lc_all, "UTF-8", 5) == 0 ||
524 ibcmp(lc_all, "UTF8", 4) == 0);
526 #endif /* USE_LOCALE */
528 PL_utf8locale = TRUE;
530 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
531 This is an alternative to using the -C command line switch
532 (the -C if present will override this). */
534 const char *p = PerlEnv_getenv("PERL_UNICODE");
535 PL_unicode = p ? parse_unicode_opts(&p) : 0;
539 #ifdef USE_LOCALE_CTYPE
540 if (curctype != NULL)
542 #endif /* USE_LOCALE_CTYPE */
543 #ifdef USE_LOCALE_COLLATE
546 #endif /* USE_LOCALE_COLLATE */
547 #ifdef USE_LOCALE_NUMERIC
550 #endif /* USE_LOCALE_NUMERIC */
554 /* Backwards compatibility. */
556 Perl_init_i18nl14n(pTHX_ int printwarn)
558 return init_i18nl10n(printwarn);
561 #ifdef USE_LOCALE_COLLATE
564 * mem_collxfrm() is a bit like strxfrm() but with two important
565 * differences. First, it handles embedded NULs. Second, it allocates
566 * a bit more memory than needed for the transformed data itself.
567 * The real transformed data begins at offset sizeof(collationix).
568 * Please see sv_collxfrm() to see how this is used.
572 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
575 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
577 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
578 /* the +1 is for the terminating NUL. */
580 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
581 New(171, xbuf, xAlloc, char);
585 *(U32*)xbuf = PL_collation_ix;
586 xout = sizeof(PL_collation_ix);
587 for (xin = 0; xin < len; ) {
591 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
594 if ((STRLEN)xused < xAlloc - xout)
596 xAlloc = (2 * xAlloc) + 1;
597 Renew(xbuf, xAlloc, char);
602 xin += strlen(s + xin) + 1;
605 /* Embedded NULs are understood but silently skipped
606 * because they make no sense in locale collation. */
610 *xlen = xout - sizeof(PL_collation_ix);
619 #endif /* USE_LOCALE_COLLATE */