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 * Standardize the locale name from a string returned by 'setlocale'.
31 * The standard return value of setlocale() is either
32 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
33 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
34 * (the space-separated values represent the various sublocales,
35 * in some unspecificed order)
37 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
38 * which is harmful for further use of the string in setlocale().
42 S_stdize_locale(pTHX_ char *locs)
47 if ((s = strchr(locs, '='))) {
51 if ((t = strchr(s, '.'))) {
54 if ((u = strchr(t, '\n'))) {
58 Move(s + 1, locs, len, char);
67 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
73 Perl_set_numeric_radix(pTHX)
75 #ifdef USE_LOCALE_NUMERIC
76 # ifdef HAS_LOCALECONV
80 if (lc && lc->decimal_point) {
81 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
82 SvREFCNT_dec(PL_numeric_radix_sv);
83 PL_numeric_radix_sv = Nullsv;
86 if (PL_numeric_radix_sv)
87 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
89 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
93 PL_numeric_radix_sv = Nullsv;
94 # endif /* HAS_LOCALECONV */
95 #endif /* USE_LOCALE_NUMERIC */
99 * Set up for a new numeric locale.
102 Perl_new_numeric(pTHX_ char *newnum)
104 #ifdef USE_LOCALE_NUMERIC
107 if (PL_numeric_name) {
108 Safefree(PL_numeric_name);
109 PL_numeric_name = NULL;
111 PL_numeric_standard = TRUE;
112 PL_numeric_local = TRUE;
116 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
117 Safefree(PL_numeric_name);
118 PL_numeric_name = stdize_locale(savepv(newnum));
119 PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
120 PL_numeric_local = TRUE;
124 #endif /* USE_LOCALE_NUMERIC */
128 Perl_set_numeric_standard(pTHX)
130 #ifdef USE_LOCALE_NUMERIC
132 if (! PL_numeric_standard) {
133 setlocale(LC_NUMERIC, "C");
134 PL_numeric_standard = TRUE;
135 PL_numeric_local = FALSE;
139 #endif /* USE_LOCALE_NUMERIC */
143 Perl_set_numeric_local(pTHX)
145 #ifdef USE_LOCALE_NUMERIC
147 if (! PL_numeric_local) {
148 setlocale(LC_NUMERIC, PL_numeric_name);
149 PL_numeric_standard = FALSE;
150 PL_numeric_local = TRUE;
154 #endif /* USE_LOCALE_NUMERIC */
158 * Set up for a new ctype locale.
161 Perl_new_ctype(pTHX_ char *newctype)
163 #ifdef USE_LOCALE_CTYPE
167 for (i = 0; i < 256; i++) {
169 PL_fold_locale[i] = toLOWER_LC(i);
170 else if (isLOWER_LC(i))
171 PL_fold_locale[i] = toUPPER_LC(i);
173 PL_fold_locale[i] = i;
176 #endif /* USE_LOCALE_CTYPE */
180 * Set up for a new collation locale.
183 Perl_new_collate(pTHX_ char *newcoll)
185 #ifdef USE_LOCALE_COLLATE
188 if (PL_collation_name) {
190 Safefree(PL_collation_name);
191 PL_collation_name = NULL;
193 PL_collation_standard = TRUE;
194 PL_collxfrm_base = 0;
195 PL_collxfrm_mult = 2;
199 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
201 Safefree(PL_collation_name);
202 PL_collation_name = stdize_locale(savepv(newcoll));
203 PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
206 /* 2: at most so many chars ('a', 'b'). */
207 /* 50: surely no system expands a char more. */
208 #define XFRMBUFSIZE (2 * 50)
209 char xbuf[XFRMBUFSIZE];
210 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
211 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
212 SSize_t mult = fb - fa;
214 Perl_croak(aTHX_ "strxfrm() gets absurd");
215 PL_collxfrm_base = (fa > mult) ? (fa - mult) : 0;
216 PL_collxfrm_mult = mult;
220 #endif /* USE_LOCALE_COLLATE */
224 * Initialize locale awareness.
227 Perl_init_i18nl10n(pTHX_ int printwarn)
231 * 1 = set ok or not applicable,
232 * 0 = fallback to C locale,
233 * -1 = fallback to C locale failed
236 #if defined(USE_LOCALE)
238 #ifdef USE_LOCALE_CTYPE
239 char *curctype = NULL;
240 #endif /* USE_LOCALE_CTYPE */
241 #ifdef USE_LOCALE_COLLATE
242 char *curcoll = NULL;
243 #endif /* USE_LOCALE_COLLATE */
244 #ifdef USE_LOCALE_NUMERIC
246 #endif /* USE_LOCALE_NUMERIC */
248 char *language = PerlEnv_getenv("LANGUAGE");
250 char *lc_all = PerlEnv_getenv("LC_ALL");
251 char *lang = PerlEnv_getenv("LANG");
252 bool setlocale_failure = FALSE;
254 #ifdef LOCALE_ENVIRON_REQUIRED
257 * Ultrix setlocale(..., "") fails if there are no environment
258 * variables from which to get a locale name.
265 if (setlocale(LC_ALL, ""))
268 setlocale_failure = TRUE;
270 if (!setlocale_failure) {
271 #ifdef USE_LOCALE_CTYPE
274 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
276 setlocale_failure = TRUE;
278 curctype = savepv(curctype);
279 #endif /* USE_LOCALE_CTYPE */
280 #ifdef USE_LOCALE_COLLATE
282 setlocale(LC_COLLATE,
283 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
285 setlocale_failure = TRUE;
287 curcoll = savepv(curcoll);
288 #endif /* USE_LOCALE_COLLATE */
289 #ifdef USE_LOCALE_NUMERIC
291 setlocale(LC_NUMERIC,
292 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
294 setlocale_failure = TRUE;
296 curnum = savepv(curnum);
297 #endif /* USE_LOCALE_NUMERIC */
302 #endif /* !LOCALE_ENVIRON_REQUIRED */
305 if (! setlocale(LC_ALL, ""))
306 setlocale_failure = TRUE;
309 if (!setlocale_failure) {
310 #ifdef USE_LOCALE_CTYPE
311 if (! (curctype = setlocale(LC_CTYPE, "")))
312 setlocale_failure = TRUE;
314 curctype = savepv(curctype);
315 #endif /* USE_LOCALE_CTYPE */
316 #ifdef USE_LOCALE_COLLATE
317 if (! (curcoll = setlocale(LC_COLLATE, "")))
318 setlocale_failure = TRUE;
320 curcoll = savepv(curcoll);
321 #endif /* USE_LOCALE_COLLATE */
322 #ifdef USE_LOCALE_NUMERIC
323 if (! (curnum = setlocale(LC_NUMERIC, "")))
324 setlocale_failure = TRUE;
326 curnum = savepv(curnum);
327 #endif /* USE_LOCALE_NUMERIC */
330 if (setlocale_failure) {
332 bool locwarn = (printwarn > 1 ||
334 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
339 PerlIO_printf(Perl_error_log,
340 "perl: warning: Setting locale failed.\n");
344 PerlIO_printf(Perl_error_log,
345 "perl: warning: Setting locale failed for the categories:\n\t");
346 #ifdef USE_LOCALE_CTYPE
348 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
349 #endif /* USE_LOCALE_CTYPE */
350 #ifdef USE_LOCALE_COLLATE
352 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
353 #endif /* USE_LOCALE_COLLATE */
354 #ifdef USE_LOCALE_NUMERIC
356 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
357 #endif /* USE_LOCALE_NUMERIC */
358 PerlIO_printf(Perl_error_log, "\n");
362 PerlIO_printf(Perl_error_log,
363 "perl: warning: Please check that your locale settings:\n");
366 PerlIO_printf(Perl_error_log,
367 "\tLANGUAGE = %c%s%c,\n",
368 language ? '"' : '(',
369 language ? language : "unset",
370 language ? '"' : ')');
373 PerlIO_printf(Perl_error_log,
374 "\tLC_ALL = %c%s%c,\n",
376 lc_all ? lc_all : "unset",
379 #if defined(USE_ENVIRON_ARRAY)
382 for (e = environ; *e; e++) {
383 if (strnEQ(*e, "LC_", 3)
384 && strnNE(*e, "LC_ALL=", 7)
385 && (p = strchr(*e, '=')))
386 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
387 (int)(p - *e), *e, p + 1);
391 PerlIO_printf(Perl_error_log,
392 "\t(possibly more locale environment variables)\n");
395 PerlIO_printf(Perl_error_log,
398 lang ? lang : "unset",
401 PerlIO_printf(Perl_error_log,
402 " are supported and installed on your system.\n");
407 if (setlocale(LC_ALL, "C")) {
409 PerlIO_printf(Perl_error_log,
410 "perl: warning: Falling back to the standard locale (\"C\").\n");
415 PerlIO_printf(Perl_error_log,
416 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
423 #ifdef USE_LOCALE_CTYPE
424 || !(curctype || setlocale(LC_CTYPE, "C"))
425 #endif /* USE_LOCALE_CTYPE */
426 #ifdef USE_LOCALE_COLLATE
427 || !(curcoll || setlocale(LC_COLLATE, "C"))
428 #endif /* USE_LOCALE_COLLATE */
429 #ifdef USE_LOCALE_NUMERIC
430 || !(curnum || setlocale(LC_NUMERIC, "C"))
431 #endif /* USE_LOCALE_NUMERIC */
435 PerlIO_printf(Perl_error_log,
436 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
440 #endif /* ! LC_ALL */
442 #ifdef USE_LOCALE_CTYPE
443 curctype = savepv(setlocale(LC_CTYPE, Nullch));
444 #endif /* USE_LOCALE_CTYPE */
445 #ifdef USE_LOCALE_COLLATE
446 curcoll = savepv(setlocale(LC_COLLATE, Nullch));
447 #endif /* USE_LOCALE_COLLATE */
448 #ifdef USE_LOCALE_NUMERIC
449 curnum = savepv(setlocale(LC_NUMERIC, Nullch));
450 #endif /* USE_LOCALE_NUMERIC */
454 #ifdef USE_LOCALE_CTYPE
456 #endif /* USE_LOCALE_CTYPE */
458 #ifdef USE_LOCALE_COLLATE
459 new_collate(curcoll);
460 #endif /* USE_LOCALE_COLLATE */
462 #ifdef USE_LOCALE_NUMERIC
464 #endif /* USE_LOCALE_NUMERIC */
467 #endif /* USE_LOCALE */
469 #ifdef USE_LOCALE_CTYPE
470 if (curctype != NULL)
472 #endif /* USE_LOCALE_CTYPE */
473 #ifdef USE_LOCALE_COLLATE
476 #endif /* USE_LOCALE_COLLATE */
477 #ifdef USE_LOCALE_NUMERIC
480 #endif /* USE_LOCALE_NUMERIC */
484 /* Backwards compatibility. */
486 Perl_init_i18nl14n(pTHX_ int printwarn)
488 return init_i18nl10n(printwarn);
491 #ifdef USE_LOCALE_COLLATE
494 * mem_collxfrm() is a bit like strxfrm() but with two important
495 * differences. First, it handles embedded NULs. Second, it allocates
496 * a bit more memory than needed for the transformed data itself.
497 * The real transformed data begins at offset sizeof(collationix).
498 * Please see sv_collxfrm() to see how this is used.
501 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
504 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
506 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
507 /* the +1 is for the terminating NUL. */
509 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
510 New(171, xbuf, xAlloc, char);
514 *(U32*)xbuf = PL_collation_ix;
515 xout = sizeof(PL_collation_ix);
516 for (xin = 0; xin < len; ) {
520 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
523 if (xused < xAlloc - xout)
525 xAlloc = (2 * xAlloc) + 1;
526 Renew(xbuf, xAlloc, char);
531 xin += strlen(s + xin) + 1;
534 /* Embedded NULs are understood but silently skipped
535 * because they make no sense in locale collation. */
539 *xlen = xout - sizeof(PL_collation_ix);
548 #endif /* USE_LOCALE_COLLATE */