3 * Copyright (c) 2001-2002, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * A Elbereth Gilthoniel,
12 * silivren penna míriel
13 * o menel aglar elenath!
14 * Na-chaered palan-díriel
15 * o galadhremmin ennorath,
16 * Fanuilos, le linnathon
17 * nef aear, si nef aearon!
21 #define PERL_IN_LOCALE_C
29 # include <langinfo.h>
35 * Standardize the locale name from a string returned by 'setlocale'.
37 * The standard return value of setlocale() is either
38 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
39 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
40 * (the space-separated values represent the various sublocales,
41 * in some unspecificed order)
43 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
44 * which is harmful for further use of the string in setlocale().
48 S_stdize_locale(pTHX_ char *locs)
53 if ((s = strchr(locs, '='))) {
57 if ((t = strchr(s, '.'))) {
60 if ((u = strchr(t, '\n'))) {
64 Move(s + 1, locs, len, char);
73 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
79 Perl_set_numeric_radix(pTHX)
81 #ifdef USE_LOCALE_NUMERIC
82 # ifdef HAS_LOCALECONV
86 if (lc && lc->decimal_point) {
87 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
88 SvREFCNT_dec(PL_numeric_radix_sv);
89 PL_numeric_radix_sv = Nullsv;
92 if (PL_numeric_radix_sv)
93 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
95 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
99 PL_numeric_radix_sv = Nullsv;
100 # endif /* HAS_LOCALECONV */
101 #endif /* USE_LOCALE_NUMERIC */
105 * Set up for a new numeric locale.
108 Perl_new_numeric(pTHX_ char *newnum)
110 #ifdef USE_LOCALE_NUMERIC
113 if (PL_numeric_name) {
114 Safefree(PL_numeric_name);
115 PL_numeric_name = NULL;
117 PL_numeric_standard = TRUE;
118 PL_numeric_local = TRUE;
122 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
123 Safefree(PL_numeric_name);
124 PL_numeric_name = stdize_locale(savepv(newnum));
125 PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
126 PL_numeric_local = TRUE;
130 #endif /* USE_LOCALE_NUMERIC */
134 Perl_set_numeric_standard(pTHX)
136 #ifdef USE_LOCALE_NUMERIC
138 if (! PL_numeric_standard) {
139 setlocale(LC_NUMERIC, "C");
140 PL_numeric_standard = TRUE;
141 PL_numeric_local = FALSE;
145 #endif /* USE_LOCALE_NUMERIC */
149 Perl_set_numeric_local(pTHX)
151 #ifdef USE_LOCALE_NUMERIC
153 if (! PL_numeric_local) {
154 setlocale(LC_NUMERIC, PL_numeric_name);
155 PL_numeric_standard = FALSE;
156 PL_numeric_local = TRUE;
160 #endif /* USE_LOCALE_NUMERIC */
164 * Set up for a new ctype locale.
167 Perl_new_ctype(pTHX_ char *newctype)
169 #ifdef USE_LOCALE_CTYPE
173 for (i = 0; i < 256; i++) {
175 PL_fold_locale[i] = toLOWER_LC(i);
176 else if (isLOWER_LC(i))
177 PL_fold_locale[i] = toUPPER_LC(i);
179 PL_fold_locale[i] = i;
182 #endif /* USE_LOCALE_CTYPE */
186 * Set up for a new collation locale.
189 Perl_new_collate(pTHX_ char *newcoll)
191 #ifdef USE_LOCALE_COLLATE
194 if (PL_collation_name) {
196 Safefree(PL_collation_name);
197 PL_collation_name = NULL;
199 PL_collation_standard = TRUE;
200 PL_collxfrm_base = 0;
201 PL_collxfrm_mult = 2;
205 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
207 Safefree(PL_collation_name);
208 PL_collation_name = stdize_locale(savepv(newcoll));
209 PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
212 /* 2: at most so many chars ('a', 'b'). */
213 /* 50: surely no system expands a char more. */
214 #define XFRMBUFSIZE (2 * 50)
215 char xbuf[XFRMBUFSIZE];
216 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
217 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
218 SSize_t mult = fb - fa;
220 Perl_croak(aTHX_ "strxfrm() gets absurd");
221 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
222 PL_collxfrm_mult = mult;
226 #endif /* USE_LOCALE_COLLATE */
230 * Initialize locale awareness.
233 Perl_init_i18nl10n(pTHX_ int printwarn)
237 * 1 = set ok or not applicable,
238 * 0 = fallback to C locale,
239 * -1 = fallback to C locale failed
242 #if defined(USE_LOCALE)
244 #ifdef USE_LOCALE_CTYPE
245 char *curctype = NULL;
246 #endif /* USE_LOCALE_CTYPE */
247 #ifdef USE_LOCALE_COLLATE
248 char *curcoll = NULL;
249 #endif /* USE_LOCALE_COLLATE */
250 #ifdef USE_LOCALE_NUMERIC
252 #endif /* USE_LOCALE_NUMERIC */
254 char *language = PerlEnv_getenv("LANGUAGE");
256 char *lc_all = PerlEnv_getenv("LC_ALL");
257 char *lang = PerlEnv_getenv("LANG");
258 bool setlocale_failure = FALSE;
260 #ifdef LOCALE_ENVIRON_REQUIRED
263 * Ultrix setlocale(..., "") fails if there are no environment
264 * variables from which to get a locale name.
271 if (setlocale(LC_ALL, ""))
274 setlocale_failure = TRUE;
276 if (!setlocale_failure) {
277 #ifdef USE_LOCALE_CTYPE
280 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
282 setlocale_failure = TRUE;
284 curctype = savepv(curctype);
285 #endif /* USE_LOCALE_CTYPE */
286 #ifdef USE_LOCALE_COLLATE
288 setlocale(LC_COLLATE,
289 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
291 setlocale_failure = TRUE;
293 curcoll = savepv(curcoll);
294 #endif /* USE_LOCALE_COLLATE */
295 #ifdef USE_LOCALE_NUMERIC
297 setlocale(LC_NUMERIC,
298 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
300 setlocale_failure = TRUE;
302 curnum = savepv(curnum);
303 #endif /* USE_LOCALE_NUMERIC */
308 #endif /* !LOCALE_ENVIRON_REQUIRED */
311 if (! setlocale(LC_ALL, ""))
312 setlocale_failure = TRUE;
315 if (!setlocale_failure) {
316 #ifdef USE_LOCALE_CTYPE
317 if (! (curctype = setlocale(LC_CTYPE, "")))
318 setlocale_failure = TRUE;
320 curctype = savepv(curctype);
321 #endif /* USE_LOCALE_CTYPE */
322 #ifdef USE_LOCALE_COLLATE
323 if (! (curcoll = setlocale(LC_COLLATE, "")))
324 setlocale_failure = TRUE;
326 curcoll = savepv(curcoll);
327 #endif /* USE_LOCALE_COLLATE */
328 #ifdef USE_LOCALE_NUMERIC
329 if (! (curnum = setlocale(LC_NUMERIC, "")))
330 setlocale_failure = TRUE;
332 curnum = savepv(curnum);
333 #endif /* USE_LOCALE_NUMERIC */
336 if (setlocale_failure) {
338 bool locwarn = (printwarn > 1 ||
340 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
345 PerlIO_printf(Perl_error_log,
346 "perl: warning: Setting locale failed.\n");
350 PerlIO_printf(Perl_error_log,
351 "perl: warning: Setting locale failed for the categories:\n\t");
352 #ifdef USE_LOCALE_CTYPE
354 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
355 #endif /* USE_LOCALE_CTYPE */
356 #ifdef USE_LOCALE_COLLATE
358 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
359 #endif /* USE_LOCALE_COLLATE */
360 #ifdef USE_LOCALE_NUMERIC
362 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
363 #endif /* USE_LOCALE_NUMERIC */
364 PerlIO_printf(Perl_error_log, "\n");
368 PerlIO_printf(Perl_error_log,
369 "perl: warning: Please check that your locale settings:\n");
372 PerlIO_printf(Perl_error_log,
373 "\tLANGUAGE = %c%s%c,\n",
374 language ? '"' : '(',
375 language ? language : "unset",
376 language ? '"' : ')');
379 PerlIO_printf(Perl_error_log,
380 "\tLC_ALL = %c%s%c,\n",
382 lc_all ? lc_all : "unset",
385 #if defined(USE_ENVIRON_ARRAY)
388 for (e = environ; *e; e++) {
389 if (strnEQ(*e, "LC_", 3)
390 && strnNE(*e, "LC_ALL=", 7)
391 && (p = strchr(*e, '=')))
392 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
393 (int)(p - *e), *e, p + 1);
397 PerlIO_printf(Perl_error_log,
398 "\t(possibly more locale environment variables)\n");
401 PerlIO_printf(Perl_error_log,
404 lang ? lang : "unset",
407 PerlIO_printf(Perl_error_log,
408 " are supported and installed on your system.\n");
413 if (setlocale(LC_ALL, "C")) {
415 PerlIO_printf(Perl_error_log,
416 "perl: warning: Falling back to the standard locale (\"C\").\n");
421 PerlIO_printf(Perl_error_log,
422 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
429 #ifdef USE_LOCALE_CTYPE
430 || !(curctype || setlocale(LC_CTYPE, "C"))
431 #endif /* USE_LOCALE_CTYPE */
432 #ifdef USE_LOCALE_COLLATE
433 || !(curcoll || setlocale(LC_COLLATE, "C"))
434 #endif /* USE_LOCALE_COLLATE */
435 #ifdef USE_LOCALE_NUMERIC
436 || !(curnum || setlocale(LC_NUMERIC, "C"))
437 #endif /* USE_LOCALE_NUMERIC */
441 PerlIO_printf(Perl_error_log,
442 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
446 #endif /* ! LC_ALL */
448 #ifdef USE_LOCALE_CTYPE
449 curctype = savepv(setlocale(LC_CTYPE, Nullch));
450 #endif /* USE_LOCALE_CTYPE */
451 #ifdef USE_LOCALE_COLLATE
452 curcoll = savepv(setlocale(LC_COLLATE, Nullch));
453 #endif /* USE_LOCALE_COLLATE */
454 #ifdef USE_LOCALE_NUMERIC
455 curnum = savepv(setlocale(LC_NUMERIC, Nullch));
456 #endif /* USE_LOCALE_NUMERIC */
460 #ifdef USE_LOCALE_CTYPE
462 #endif /* USE_LOCALE_CTYPE */
464 #ifdef USE_LOCALE_COLLATE
465 new_collate(curcoll);
466 #endif /* USE_LOCALE_COLLATE */
468 #ifdef USE_LOCALE_NUMERIC
470 #endif /* USE_LOCALE_NUMERIC */
474 #endif /* USE_LOCALE */
478 /* Set PL_wantutf8 to TRUE if using PerlIO _and_
479 any of the following are true:
480 - nl_langinfo(CODESET) contains /^utf-?8/i
481 - $ENV{LANGUAGE} contains /^utf-?8/i (only if using glibc)
482 - $ENV{LC_CALL} contains /^utf-?8/i
483 - $ENV{LC_CTYPE} contains /^utf-?8/i
484 - $ENV{LANG} contains /^utf-?8/i
485 If PL_wantutf8 is true, perl.c:S_parse_body()
486 will turn on the PerlIO :utf8 discipline on STDIN, STDOUT,
487 STDERR, _and_ the default open discipline.
489 bool wantutf8 = FALSE;
490 char *codeset = NULL;
491 #if defined(HAS_NL_LANGINFO) && defined(CODESET)
492 codeset = nl_langinfo(CODESET);
495 (ibcmp(codeset, "UTF-8", 5) == 0 ||
496 ibcmp(codeset, "UTF8", 4) == 0))
498 #if defined(USE_LOCALE)
500 if (!wantutf8 && language &&
501 (ibcmp(language, "UTF-8", 5) == 0 ||
502 ibcmp(language, "UTF8", 4) == 0))
505 if (!wantutf8 && lc_all &&
506 (ibcmp(lc_all, "UTF-8", 5) == 0 ||
507 ibcmp(lc_all, "UTF8", 4) == 0))
509 #ifdef USE_LOCALE_CTYPE
510 if (!wantutf8 && curctype &&
511 (ibcmp(curctype, "UTF-8", 5) == 0 ||
512 ibcmp(curctype, "UTF8", 4) == 0))
515 if (!wantutf8 && lang &&
516 (ibcmp(lang, "UTF-8", 5) == 0 ||
517 ibcmp(lang, "UTF8", 4) == 0))
519 #endif /* USE_LOCALE */
525 #ifdef USE_LOCALE_CTYPE
526 if (curctype != NULL)
528 #endif /* USE_LOCALE_CTYPE */
529 #ifdef USE_LOCALE_COLLATE
532 #endif /* USE_LOCALE_COLLATE */
533 #ifdef USE_LOCALE_NUMERIC
536 #endif /* USE_LOCALE_NUMERIC */
540 /* Backwards compatibility. */
542 Perl_init_i18nl14n(pTHX_ int printwarn)
544 return init_i18nl10n(printwarn);
547 #ifdef USE_LOCALE_COLLATE
550 * mem_collxfrm() is a bit like strxfrm() but with two important
551 * differences. First, it handles embedded NULs. Second, it allocates
552 * a bit more memory than needed for the transformed data itself.
553 * The real transformed data begins at offset sizeof(collationix).
554 * Please see sv_collxfrm() to see how this is used.
557 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
560 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
562 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
563 /* the +1 is for the terminating NUL. */
565 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
566 New(171, xbuf, xAlloc, char);
570 *(U32*)xbuf = PL_collation_ix;
571 xout = sizeof(PL_collation_ix);
572 for (xin = 0; xin < len; ) {
576 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
579 if ((STRLEN)xused < xAlloc - xout)
581 xAlloc = (2 * xAlloc) + 1;
582 Renew(xbuf, xAlloc, char);
587 xin += strlen(s + xin) + 1;
590 /* Embedded NULs are understood but silently skipped
591 * because they make no sense in locale collation. */
595 *xlen = xout - sizeof(PL_collation_ix);
604 #endif /* USE_LOCALE_COLLATE */