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)
56 const char *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
84 # ifdef HAS_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 = Nullsv;
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 = Nullsv;
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
115 if (PL_numeric_name) {
116 Safefree(PL_numeric_name);
117 PL_numeric_name = NULL;
119 PL_numeric_standard = TRUE;
120 PL_numeric_local = TRUE;
124 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
125 Safefree(PL_numeric_name);
126 PL_numeric_name = stdize_locale(savepv(newnum));
127 PL_numeric_standard = ((*newnum == 'C' && newnum[1] == '\0')
128 || strEQ(newnum, "POSIX"));
129 PL_numeric_local = TRUE;
133 #endif /* USE_LOCALE_NUMERIC */
137 Perl_set_numeric_standard(pTHX)
139 #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
156 if (! PL_numeric_local) {
157 setlocale(LC_NUMERIC, PL_numeric_name);
158 PL_numeric_standard = FALSE;
159 PL_numeric_local = TRUE;
163 #endif /* USE_LOCALE_NUMERIC */
167 * Set up for a new ctype locale.
170 Perl_new_ctype(pTHX_ const char *newctype)
172 #ifdef USE_LOCALE_CTYPE
176 for (i = 0; i < 256; i++) {
178 PL_fold_locale[i] = toLOWER_LC(i);
179 else if (isLOWER_LC(i))
180 PL_fold_locale[i] = toUPPER_LC(i);
182 PL_fold_locale[i] = i;
185 #endif /* USE_LOCALE_CTYPE */
186 PERL_UNUSED_ARG(newctype);
190 * Set up for a new collation locale.
193 Perl_new_collate(pTHX_ const char *newcoll)
195 #ifdef USE_LOCALE_COLLATE
198 if (PL_collation_name) {
200 Safefree(PL_collation_name);
201 PL_collation_name = NULL;
203 PL_collation_standard = TRUE;
204 PL_collxfrm_base = 0;
205 PL_collxfrm_mult = 2;
209 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
211 Safefree(PL_collation_name);
212 PL_collation_name = stdize_locale(savepv(newcoll));
213 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
214 || strEQ(newcoll, "POSIX"));
217 /* 2: at most so many chars ('a', 'b'). */
218 /* 50: surely no system expands a char more. */
219 #define XFRMBUFSIZE (2 * 50)
220 char xbuf[XFRMBUFSIZE];
221 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
222 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
223 const SSize_t mult = fb - fa;
225 Perl_croak(aTHX_ "strxfrm() gets absurd");
226 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
227 PL_collxfrm_mult = mult;
231 #endif /* USE_LOCALE_COLLATE */
235 * Initialize locale awareness.
238 Perl_init_i18nl10n(pTHX_ int printwarn)
242 * 1 = set ok or not applicable,
243 * 0 = fallback to C locale,
244 * -1 = fallback to C locale failed
247 #if defined(USE_LOCALE)
249 #ifdef USE_LOCALE_CTYPE
250 char *curctype = NULL;
251 #endif /* USE_LOCALE_CTYPE */
252 #ifdef USE_LOCALE_COLLATE
253 char *curcoll = NULL;
254 #endif /* USE_LOCALE_COLLATE */
255 #ifdef USE_LOCALE_NUMERIC
257 #endif /* USE_LOCALE_NUMERIC */
259 char *language = PerlEnv_getenv("LANGUAGE");
261 char *lc_all = PerlEnv_getenv("LC_ALL");
262 char *lang = PerlEnv_getenv("LANG");
263 bool setlocale_failure = FALSE;
265 #ifdef LOCALE_ENVIRON_REQUIRED
268 * Ultrix setlocale(..., "") fails if there are no environment
269 * variables from which to get a locale name.
276 if (setlocale(LC_ALL, ""))
279 setlocale_failure = TRUE;
281 if (!setlocale_failure) {
282 #ifdef USE_LOCALE_CTYPE
285 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
287 setlocale_failure = TRUE;
289 curctype = savepv(curctype);
290 #endif /* USE_LOCALE_CTYPE */
291 #ifdef USE_LOCALE_COLLATE
293 setlocale(LC_COLLATE,
294 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
296 setlocale_failure = TRUE;
298 curcoll = savepv(curcoll);
299 #endif /* USE_LOCALE_COLLATE */
300 #ifdef USE_LOCALE_NUMERIC
302 setlocale(LC_NUMERIC,
303 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
305 setlocale_failure = TRUE;
307 curnum = savepv(curnum);
308 #endif /* USE_LOCALE_NUMERIC */
313 #endif /* !LOCALE_ENVIRON_REQUIRED */
316 if (! setlocale(LC_ALL, ""))
317 setlocale_failure = TRUE;
320 if (!setlocale_failure) {
321 #ifdef USE_LOCALE_CTYPE
322 if (! (curctype = setlocale(LC_CTYPE, "")))
323 setlocale_failure = TRUE;
325 curctype = savepv(curctype);
326 #endif /* USE_LOCALE_CTYPE */
327 #ifdef USE_LOCALE_COLLATE
328 if (! (curcoll = setlocale(LC_COLLATE, "")))
329 setlocale_failure = TRUE;
331 curcoll = savepv(curcoll);
332 #endif /* USE_LOCALE_COLLATE */
333 #ifdef USE_LOCALE_NUMERIC
334 if (! (curnum = setlocale(LC_NUMERIC, "")))
335 setlocale_failure = TRUE;
337 curnum = savepv(curnum);
338 #endif /* USE_LOCALE_NUMERIC */
341 if (setlocale_failure) {
343 bool locwarn = (printwarn > 1 ||
345 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
350 PerlIO_printf(Perl_error_log,
351 "perl: warning: Setting locale failed.\n");
355 PerlIO_printf(Perl_error_log,
356 "perl: warning: Setting locale failed for the categories:\n\t");
357 #ifdef USE_LOCALE_CTYPE
359 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
360 #endif /* USE_LOCALE_CTYPE */
361 #ifdef USE_LOCALE_COLLATE
363 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
364 #endif /* USE_LOCALE_COLLATE */
365 #ifdef USE_LOCALE_NUMERIC
367 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
368 #endif /* USE_LOCALE_NUMERIC */
369 PerlIO_printf(Perl_error_log, "\n");
373 PerlIO_printf(Perl_error_log,
374 "perl: warning: Please check that your locale settings:\n");
377 PerlIO_printf(Perl_error_log,
378 "\tLANGUAGE = %c%s%c,\n",
379 language ? '"' : '(',
380 language ? language : "unset",
381 language ? '"' : ')');
384 PerlIO_printf(Perl_error_log,
385 "\tLC_ALL = %c%s%c,\n",
387 lc_all ? lc_all : "unset",
390 #if defined(USE_ENVIRON_ARRAY)
393 for (e = environ; *e; e++) {
394 if (strnEQ(*e, "LC_", 3)
395 && strnNE(*e, "LC_ALL=", 7)
396 && (p = strchr(*e, '=')))
397 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
398 (int)(p - *e), *e, p + 1);
402 PerlIO_printf(Perl_error_log,
403 "\t(possibly more locale environment variables)\n");
406 PerlIO_printf(Perl_error_log,
409 lang ? lang : "unset",
412 PerlIO_printf(Perl_error_log,
413 " are supported and installed on your system.\n");
418 if (setlocale(LC_ALL, "C")) {
420 PerlIO_printf(Perl_error_log,
421 "perl: warning: Falling back to the standard locale (\"C\").\n");
426 PerlIO_printf(Perl_error_log,
427 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
434 #ifdef USE_LOCALE_CTYPE
435 || !(curctype || setlocale(LC_CTYPE, "C"))
436 #endif /* USE_LOCALE_CTYPE */
437 #ifdef USE_LOCALE_COLLATE
438 || !(curcoll || setlocale(LC_COLLATE, "C"))
439 #endif /* USE_LOCALE_COLLATE */
440 #ifdef USE_LOCALE_NUMERIC
441 || !(curnum || setlocale(LC_NUMERIC, "C"))
442 #endif /* USE_LOCALE_NUMERIC */
446 PerlIO_printf(Perl_error_log,
447 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
451 #endif /* ! LC_ALL */
453 #ifdef USE_LOCALE_CTYPE
454 curctype = savepv(setlocale(LC_CTYPE, Nullch));
455 #endif /* USE_LOCALE_CTYPE */
456 #ifdef USE_LOCALE_COLLATE
457 curcoll = savepv(setlocale(LC_COLLATE, Nullch));
458 #endif /* USE_LOCALE_COLLATE */
459 #ifdef USE_LOCALE_NUMERIC
460 curnum = savepv(setlocale(LC_NUMERIC, Nullch));
461 #endif /* USE_LOCALE_NUMERIC */
465 #ifdef USE_LOCALE_CTYPE
467 #endif /* USE_LOCALE_CTYPE */
469 #ifdef USE_LOCALE_COLLATE
470 new_collate(curcoll);
471 #endif /* USE_LOCALE_COLLATE */
473 #ifdef USE_LOCALE_NUMERIC
475 #endif /* USE_LOCALE_NUMERIC */
479 #endif /* USE_LOCALE */
483 /* Set PL_utf8locale to TRUE if using PerlIO _and_
484 any of the following are true:
485 - nl_langinfo(CODESET) contains /^utf-?8/i
486 - $ENV{LC_ALL} contains /^utf-?8/i
487 - $ENV{LC_CTYPE} contains /^utf-?8/i
488 - $ENV{LANG} contains /^utf-?8/i
489 The LC_ALL, LC_CTYPE, LANG obey the usual override
490 hierarchy of locale environment variables. (LANGUAGE
491 affects only LC_MESSAGES only under glibc.) (If present,
492 it overrides LC_MESSAGES for GNU gettext, and it also
493 can have more than one locale, separated by spaces,
494 in case you need to know.)
495 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
496 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
497 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
499 bool utf8locale = FALSE;
500 char *codeset = NULL;
501 #if defined(HAS_NL_LANGINFO) && defined(CODESET)
502 codeset = nl_langinfo(CODESET);
505 utf8locale = (ibcmp(codeset, "UTF-8", 5) == 0 ||
506 ibcmp(codeset, "UTF8", 4) == 0);
507 #if defined(USE_LOCALE)
508 else { /* nl_langinfo(CODESET) is supposed to correctly
509 * interpret the locale environment variables,
510 * but just in case it fails, let's do this manually. */
512 utf8locale = (ibcmp(lang, "UTF-8", 5) == 0 ||
513 ibcmp(lang, "UTF8", 4) == 0);
514 #ifdef USE_LOCALE_CTYPE
516 utf8locale = (ibcmp(curctype, "UTF-8", 5) == 0 ||
517 ibcmp(curctype, "UTF8", 4) == 0);
520 utf8locale = (ibcmp(lc_all, "UTF-8", 5) == 0 ||
521 ibcmp(lc_all, "UTF8", 4) == 0);
523 #endif /* USE_LOCALE */
525 PL_utf8locale = TRUE;
527 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
528 This is an alternative to using the -C command line switch
529 (the -C if present will override this). */
531 const char *p = PerlEnv_getenv("PERL_UNICODE");
532 PL_unicode = p ? parse_unicode_opts(&p) : 0;
536 #ifdef USE_LOCALE_CTYPE
537 if (curctype != NULL)
539 #endif /* USE_LOCALE_CTYPE */
540 #ifdef USE_LOCALE_COLLATE
543 #endif /* USE_LOCALE_COLLATE */
544 #ifdef USE_LOCALE_NUMERIC
547 #endif /* USE_LOCALE_NUMERIC */
551 /* Backwards compatibility. */
553 Perl_init_i18nl14n(pTHX_ int printwarn)
555 return init_i18nl10n(printwarn);
558 #ifdef USE_LOCALE_COLLATE
561 * mem_collxfrm() is a bit like strxfrm() but with two important
562 * differences. First, it handles embedded NULs. Second, it allocates
563 * a bit more memory than needed for the transformed data itself.
564 * The real transformed data begins at offset sizeof(collationix).
565 * Please see sv_collxfrm() to see how this is used.
569 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
572 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
574 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
575 /* the +1 is for the terminating NUL. */
577 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
578 New(171, xbuf, xAlloc, char);
582 *(U32*)xbuf = PL_collation_ix;
583 xout = sizeof(PL_collation_ix);
584 for (xin = 0; xin < len; ) {
588 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
591 if ((STRLEN)xused < xAlloc - xout)
593 xAlloc = (2 * xAlloc) + 1;
594 Renew(xbuf, xAlloc, char);
599 xin += strlen(s + xin) + 1;
602 /* Embedded NULs are understood but silently skipped
603 * because they make no sense in locale collation. */
607 *xlen = xout - sizeof(PL_collation_ix);
616 #endif /* USE_LOCALE_COLLATE */
620 * c-indentation-style: bsd
622 * indent-tabs-mode: t
625 * ex: set ts=8 sts=4 sw=4 noet: