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>
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)
59 if ((s = strchr(locs, '='))) {
63 if ((t = strchr(s, '.'))) {
66 if ((u = strchr(t, '\n'))) {
70 Move(s + 1, locs, len, char);
79 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
86 Perl_set_numeric_radix(pTHX)
88 #ifdef USE_LOCALE_NUMERIC
89 # ifdef HAS_LOCALECONV
93 if (lc && lc->decimal_point) {
94 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
95 SvREFCNT_dec(PL_numeric_radix_sv);
96 PL_numeric_radix_sv = Nullsv;
99 if (PL_numeric_radix_sv)
100 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
102 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
106 PL_numeric_radix_sv = Nullsv;
107 # endif /* HAS_LOCALECONV */
108 #endif /* USE_LOCALE_NUMERIC */
112 * Set up for a new numeric locale.
115 Perl_new_numeric(pTHX_ char *newnum)
117 #ifdef USE_LOCALE_NUMERIC
120 if (PL_numeric_name) {
121 Safefree(PL_numeric_name);
122 PL_numeric_name = NULL;
124 PL_numeric_standard = TRUE;
125 PL_numeric_local = TRUE;
129 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
130 Safefree(PL_numeric_name);
131 PL_numeric_name = stdize_locale(savepv(newnum));
132 PL_numeric_standard = ((*newnum == 'C' && newnum[1] == '\0')
133 || strEQ(newnum, "POSIX"));
134 PL_numeric_local = TRUE;
138 #endif /* USE_LOCALE_NUMERIC */
142 Perl_set_numeric_standard(pTHX)
144 #ifdef USE_LOCALE_NUMERIC
146 if (! PL_numeric_standard) {
147 setlocale(LC_NUMERIC, "C");
148 PL_numeric_standard = TRUE;
149 PL_numeric_local = FALSE;
153 #endif /* USE_LOCALE_NUMERIC */
157 Perl_set_numeric_local(pTHX)
159 #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_ char *newctype)
177 #ifdef USE_LOCALE_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 */
195 * Set up for a new collation locale.
198 Perl_new_collate(pTHX_ char *newcoll)
200 #ifdef USE_LOCALE_COLLATE
203 if (PL_collation_name) {
205 Safefree(PL_collation_name);
206 PL_collation_name = NULL;
208 PL_collation_standard = TRUE;
209 PL_collxfrm_base = 0;
210 PL_collxfrm_mult = 2;
214 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
216 Safefree(PL_collation_name);
217 PL_collation_name = stdize_locale(savepv(newcoll));
218 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
219 || strEQ(newcoll, "POSIX"));
222 /* 2: at most so many chars ('a', 'b'). */
223 /* 50: surely no system expands a char more. */
224 #define XFRMBUFSIZE (2 * 50)
225 char xbuf[XFRMBUFSIZE];
226 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
227 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
228 SSize_t mult = fb - fa;
230 Perl_croak(aTHX_ "strxfrm() gets absurd");
231 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
232 PL_collxfrm_mult = mult;
236 #endif /* USE_LOCALE_COLLATE */
240 * Initialize locale awareness.
243 Perl_init_i18nl10n(pTHX_ int printwarn)
247 * 1 = set ok or not applicable,
248 * 0 = fallback to C locale,
249 * -1 = fallback to C locale failed
252 #if defined(USE_LOCALE)
254 #ifdef USE_LOCALE_CTYPE
255 char *curctype = NULL;
256 #endif /* USE_LOCALE_CTYPE */
257 #ifdef USE_LOCALE_COLLATE
258 char *curcoll = NULL;
259 #endif /* USE_LOCALE_COLLATE */
260 #ifdef USE_LOCALE_NUMERIC
262 #endif /* USE_LOCALE_NUMERIC */
264 char *language = PerlEnv_getenv("LANGUAGE");
266 char *lc_all = PerlEnv_getenv("LC_ALL");
267 char *lang = PerlEnv_getenv("LANG");
268 bool setlocale_failure = FALSE;
270 #ifdef LOCALE_ENVIRON_REQUIRED
273 * Ultrix setlocale(..., "") fails if there are no environment
274 * variables from which to get a locale name.
281 if (setlocale(LC_ALL, ""))
284 setlocale_failure = TRUE;
286 if (!setlocale_failure) {
287 #ifdef USE_LOCALE_CTYPE
290 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
292 setlocale_failure = TRUE;
294 curctype = savepv(curctype);
295 #endif /* USE_LOCALE_CTYPE */
296 #ifdef USE_LOCALE_COLLATE
298 setlocale(LC_COLLATE,
299 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
301 setlocale_failure = TRUE;
303 curcoll = savepv(curcoll);
304 #endif /* USE_LOCALE_COLLATE */
305 #ifdef USE_LOCALE_NUMERIC
307 setlocale(LC_NUMERIC,
308 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
310 setlocale_failure = TRUE;
312 curnum = savepv(curnum);
313 #endif /* USE_LOCALE_NUMERIC */
318 #endif /* !LOCALE_ENVIRON_REQUIRED */
321 if (! setlocale(LC_ALL, ""))
322 setlocale_failure = TRUE;
325 if (!setlocale_failure) {
326 #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
333 if (! (curcoll = setlocale(LC_COLLATE, "")))
334 setlocale_failure = TRUE;
336 curcoll = savepv(curcoll);
337 #endif /* USE_LOCALE_COLLATE */
338 #ifdef USE_LOCALE_NUMERIC
339 if (! (curnum = setlocale(LC_NUMERIC, "")))
340 setlocale_failure = TRUE;
342 curnum = savepv(curnum);
343 #endif /* USE_LOCALE_NUMERIC */
346 if (setlocale_failure) {
348 bool locwarn = (printwarn > 1 ||
350 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
355 PerlIO_printf(Perl_error_log,
356 "perl: warning: Setting locale failed.\n");
360 PerlIO_printf(Perl_error_log,
361 "perl: warning: Setting locale failed for the categories:\n\t");
362 #ifdef USE_LOCALE_CTYPE
364 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
365 #endif /* USE_LOCALE_CTYPE */
366 #ifdef USE_LOCALE_COLLATE
368 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
369 #endif /* USE_LOCALE_COLLATE */
370 #ifdef USE_LOCALE_NUMERIC
372 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
373 #endif /* USE_LOCALE_NUMERIC */
374 PerlIO_printf(Perl_error_log, "\n");
378 PerlIO_printf(Perl_error_log,
379 "perl: warning: Please check that your locale settings:\n");
382 PerlIO_printf(Perl_error_log,
383 "\tLANGUAGE = %c%s%c,\n",
384 language ? '"' : '(',
385 language ? language : "unset",
386 language ? '"' : ')');
389 PerlIO_printf(Perl_error_log,
390 "\tLC_ALL = %c%s%c,\n",
392 lc_all ? lc_all : "unset",
395 #if defined(USE_ENVIRON_ARRAY)
398 for (e = environ; *e; e++) {
399 if (strnEQ(*e, "LC_", 3)
400 && strnNE(*e, "LC_ALL=", 7)
401 && (p = strchr(*e, '=')))
402 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
403 (int)(p - *e), *e, p + 1);
407 PerlIO_printf(Perl_error_log,
408 "\t(possibly more locale environment variables)\n");
411 PerlIO_printf(Perl_error_log,
414 lang ? lang : "unset",
417 PerlIO_printf(Perl_error_log,
418 " are supported and installed on your system.\n");
423 if (setlocale(LC_ALL, "C")) {
425 PerlIO_printf(Perl_error_log,
426 "perl: warning: Falling back to the standard locale (\"C\").\n");
431 PerlIO_printf(Perl_error_log,
432 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
439 #ifdef USE_LOCALE_CTYPE
440 || !(curctype || setlocale(LC_CTYPE, "C"))
441 #endif /* USE_LOCALE_CTYPE */
442 #ifdef USE_LOCALE_COLLATE
443 || !(curcoll || setlocale(LC_COLLATE, "C"))
444 #endif /* USE_LOCALE_COLLATE */
445 #ifdef USE_LOCALE_NUMERIC
446 || !(curnum || setlocale(LC_NUMERIC, "C"))
447 #endif /* USE_LOCALE_NUMERIC */
451 PerlIO_printf(Perl_error_log,
452 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
456 #endif /* ! LC_ALL */
458 #ifdef USE_LOCALE_CTYPE
459 curctype = savepv(setlocale(LC_CTYPE, Nullch));
460 #endif /* USE_LOCALE_CTYPE */
461 #ifdef USE_LOCALE_COLLATE
462 curcoll = savepv(setlocale(LC_COLLATE, Nullch));
463 #endif /* USE_LOCALE_COLLATE */
464 #ifdef USE_LOCALE_NUMERIC
465 curnum = savepv(setlocale(LC_NUMERIC, Nullch));
466 #endif /* USE_LOCALE_NUMERIC */
470 #ifdef USE_LOCALE_CTYPE
472 #endif /* USE_LOCALE_CTYPE */
474 #ifdef USE_LOCALE_COLLATE
475 new_collate(curcoll);
476 #endif /* USE_LOCALE_COLLATE */
478 #ifdef USE_LOCALE_NUMERIC
480 #endif /* USE_LOCALE_NUMERIC */
484 #endif /* USE_LOCALE */
488 /* Set PL_utf8locale to TRUE if using PerlIO _and_
489 any of the following are true:
490 - nl_langinfo(CODESET) contains /^utf-?8/i
491 - $ENV{LC_ALL} contains /^utf-?8/i
492 - $ENV{LC_CTYPE} contains /^utf-?8/i
493 - $ENV{LANG} contains /^utf-?8/i
494 The LC_ALL, LC_CTYPE, LANG obey the usual override
495 hierarchy of locale environment variables. (LANGUAGE
496 affects only LC_MESSAGES only under glibc.) (If present,
497 it overrides LC_MESSAGES for GNU gettext, and it also
498 can have more than one locale, separated by spaces,
499 in case you need to know.)
500 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
501 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
502 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
504 bool utf8locale = FALSE;
505 char *codeset = NULL;
506 #if defined(HAS_NL_LANGINFO) && defined(CODESET)
507 codeset = nl_langinfo(CODESET);
510 utf8locale = (ibcmp(codeset, "UTF-8", 5) == 0 ||
511 ibcmp(codeset, "UTF8", 4) == 0);
512 #if defined(USE_LOCALE)
513 else { /* nl_langinfo(CODESET) is supposed to correctly
514 * interpret the locale environment variables,
515 * but just in case it fails, let's do this manually. */
517 utf8locale = (ibcmp(lang, "UTF-8", 5) == 0 ||
518 ibcmp(lang, "UTF8", 4) == 0);
519 #ifdef USE_LOCALE_CTYPE
521 utf8locale = (ibcmp(curctype, "UTF-8", 5) == 0 ||
522 ibcmp(curctype, "UTF8", 4) == 0);
525 utf8locale = (ibcmp(lc_all, "UTF-8", 5) == 0 ||
526 ibcmp(lc_all, "UTF8", 4) == 0);
528 #endif /* USE_LOCALE */
530 PL_utf8locale = TRUE;
532 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
533 This is an alternative to using the -C command line switch
534 (the -C if present will override this). */
536 const char *p = PerlEnv_getenv("PERL_UNICODE");
537 PL_unicode = p ? parse_unicode_opts(&p) : 0;
541 #ifdef USE_LOCALE_CTYPE
542 if (curctype != NULL)
544 #endif /* USE_LOCALE_CTYPE */
545 #ifdef USE_LOCALE_COLLATE
548 #endif /* USE_LOCALE_COLLATE */
549 #ifdef USE_LOCALE_NUMERIC
552 #endif /* USE_LOCALE_NUMERIC */
556 /* Backwards compatibility. */
558 Perl_init_i18nl14n(pTHX_ int printwarn)
560 return init_i18nl10n(printwarn);
563 #ifdef USE_LOCALE_COLLATE
566 * mem_collxfrm() is a bit like strxfrm() but with two important
567 * differences. First, it handles embedded NULs. Second, it allocates
568 * a bit more memory than needed for the transformed data itself.
569 * The real transformed data begins at offset sizeof(collationix).
570 * Please see sv_collxfrm() to see how this is used.
574 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
577 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
579 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
580 /* the +1 is for the terminating NUL. */
582 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
583 New(171, xbuf, xAlloc, char);
587 *(U32*)xbuf = PL_collation_ix;
588 xout = sizeof(PL_collation_ix);
589 for (xin = 0; xin < len; ) {
593 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
596 if ((STRLEN)xused < xAlloc - xout)
598 xAlloc = (2 * xAlloc) + 1;
599 Renew(xbuf, xAlloc, char);
604 xin += strlen(s + xin) + 1;
607 /* Embedded NULs are understood but silently skipped
608 * because they make no sense in locale collation. */
612 *xlen = xout - sizeof(PL_collation_ix);
621 #endif /* USE_LOCALE_COLLATE */
625 * c-indentation-style: bsd
627 * indent-tabs-mode: t
630 * ex: set ts=8 sts=4 sw=4 noet: