This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
s/sv_setpv(sv,"")/sv_setpvn(sv,"",0)/
[perl5.git] / locale.c
CommitLineData
98994639
HS
1/* locale.c
2 *
4bb101f2 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
770526c1 4 * 2000, 2001, 2002, 2003, 2005, by Larry Wall and others
98994639
HS
5 *
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.
8 *
9 */
10
11/*
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!
19 */
20
166f8a29
DM
21/* utility functions for handling locale-specific stuff like what
22 * character represents the decimal point.
23 */
24
98994639
HS
25#include "EXTERN.h"
26#define PERL_IN_LOCALE_C
27#include "perl.h"
28
29#ifdef I_LOCALE
30# include <locale.h>
31#endif
32
b310b053
JH
33#ifdef I_LANGINFO
34# include <langinfo.h>
35#endif
36
a4af207c
JH
37#include "reentr.h"
38
27da23d5 39#if defined(USE_LOCALE_NUMERIC) || defined(USE_LOCALE_COLLATE)
98994639
HS
40/*
41 * Standardize the locale name from a string returned by 'setlocale'.
42 *
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)
48 *
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().
51 *
52 */
53STATIC char *
54S_stdize_locale(pTHX_ char *locs)
55{
56 char *s;
57 bool okay = TRUE;
58
59 if ((s = strchr(locs, '='))) {
60 char *t;
61
62 okay = FALSE;
63 if ((t = strchr(s, '.'))) {
64 char *u;
65
66 if ((u = strchr(t, '\n'))) {
67
68 if (u[1] == 0) {
69 STRLEN len = u - s;
70 Move(s + 1, locs, len, char);
71 locs[len] = 0;
72 okay = TRUE;
73 }
74 }
75 }
76 }
77
78 if (!okay)
79 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
80
81 return locs;
82}
27da23d5 83#endif
98994639
HS
84
85void
86Perl_set_numeric_radix(pTHX)
87{
88#ifdef USE_LOCALE_NUMERIC
89# ifdef HAS_LOCALECONV
90 struct lconv* lc;
91
92 lc = 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;
97 }
98 else {
99 if (PL_numeric_radix_sv)
100 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
101 else
102 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
103 }
104 }
105 else
106 PL_numeric_radix_sv = Nullsv;
107# endif /* HAS_LOCALECONV */
108#endif /* USE_LOCALE_NUMERIC */
109}
110
111/*
112 * Set up for a new numeric locale.
113 */
114void
115Perl_new_numeric(pTHX_ char *newnum)
116{
117#ifdef USE_LOCALE_NUMERIC
118
119 if (! newnum) {
120 if (PL_numeric_name) {
121 Safefree(PL_numeric_name);
122 PL_numeric_name = NULL;
123 }
124 PL_numeric_standard = TRUE;
125 PL_numeric_local = TRUE;
126 return;
127 }
128
129 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
130 Safefree(PL_numeric_name);
131 PL_numeric_name = stdize_locale(savepv(newnum));
770526c1
NC
132 PL_numeric_standard = ((*newnum == 'C' && newnum[1] == '\0')
133 || strEQ(newnum, "POSIX"));
98994639
HS
134 PL_numeric_local = TRUE;
135 set_numeric_radix();
136 }
137
138#endif /* USE_LOCALE_NUMERIC */
139}
140
141void
142Perl_set_numeric_standard(pTHX)
143{
144#ifdef USE_LOCALE_NUMERIC
145
146 if (! PL_numeric_standard) {
147 setlocale(LC_NUMERIC, "C");
148 PL_numeric_standard = TRUE;
149 PL_numeric_local = FALSE;
150 set_numeric_radix();
151 }
152
153#endif /* USE_LOCALE_NUMERIC */
154}
155
156void
157Perl_set_numeric_local(pTHX)
158{
159#ifdef USE_LOCALE_NUMERIC
160
161 if (! PL_numeric_local) {
162 setlocale(LC_NUMERIC, PL_numeric_name);
163 PL_numeric_standard = FALSE;
164 PL_numeric_local = TRUE;
165 set_numeric_radix();
166 }
167
168#endif /* USE_LOCALE_NUMERIC */
169}
170
171/*
172 * Set up for a new ctype locale.
173 */
174void
175Perl_new_ctype(pTHX_ char *newctype)
176{
177#ifdef USE_LOCALE_CTYPE
27da23d5 178 dVAR;
98994639
HS
179 int i;
180
181 for (i = 0; i < 256; i++) {
182 if (isUPPER_LC(i))
183 PL_fold_locale[i] = toLOWER_LC(i);
184 else if (isLOWER_LC(i))
185 PL_fold_locale[i] = toUPPER_LC(i);
186 else
187 PL_fold_locale[i] = i;
188 }
189
190#endif /* USE_LOCALE_CTYPE */
dd374669 191 (void)newctype;
98994639
HS
192}
193
194/*
195 * Set up for a new collation locale.
196 */
197void
198Perl_new_collate(pTHX_ char *newcoll)
199{
200#ifdef USE_LOCALE_COLLATE
201
202 if (! newcoll) {
203 if (PL_collation_name) {
204 ++PL_collation_ix;
205 Safefree(PL_collation_name);
206 PL_collation_name = NULL;
207 }
208 PL_collation_standard = TRUE;
209 PL_collxfrm_base = 0;
210 PL_collxfrm_mult = 2;
211 return;
212 }
213
214 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
215 ++PL_collation_ix;
216 Safefree(PL_collation_name);
217 PL_collation_name = stdize_locale(savepv(newcoll));
770526c1
NC
218 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
219 || strEQ(newcoll, "POSIX"));
98994639
HS
220
221 {
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;
229 if (mult < 1)
230 Perl_croak(aTHX_ "strxfrm() gets absurd");
eb160463 231 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
98994639
HS
232 PL_collxfrm_mult = mult;
233 }
234 }
235
236#endif /* USE_LOCALE_COLLATE */
237}
238
239/*
240 * Initialize locale awareness.
241 */
242int
243Perl_init_i18nl10n(pTHX_ int printwarn)
244{
245 int ok = 1;
246 /* returns
247 * 1 = set ok or not applicable,
248 * 0 = fallback to C locale,
249 * -1 = fallback to C locale failed
250 */
251
252#if defined(USE_LOCALE)
253
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
261 char *curnum = NULL;
262#endif /* USE_LOCALE_NUMERIC */
263#ifdef __GLIBC__
264 char *language = PerlEnv_getenv("LANGUAGE");
265#endif
266 char *lc_all = PerlEnv_getenv("LC_ALL");
267 char *lang = PerlEnv_getenv("LANG");
268 bool setlocale_failure = FALSE;
269
270#ifdef LOCALE_ENVIRON_REQUIRED
271
272 /*
273 * Ultrix setlocale(..., "") fails if there are no environment
274 * variables from which to get a locale name.
275 */
276
277 bool done = FALSE;
278
279#ifdef LC_ALL
280 if (lang) {
281 if (setlocale(LC_ALL, ""))
282 done = TRUE;
283 else
284 setlocale_failure = TRUE;
285 }
286 if (!setlocale_failure) {
287#ifdef USE_LOCALE_CTYPE
288 if (! (curctype =
289 setlocale(LC_CTYPE,
290 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
291 ? "" : Nullch)))
292 setlocale_failure = TRUE;
293 else
294 curctype = savepv(curctype);
295#endif /* USE_LOCALE_CTYPE */
296#ifdef USE_LOCALE_COLLATE
297 if (! (curcoll =
298 setlocale(LC_COLLATE,
299 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
300 ? "" : Nullch)))
301 setlocale_failure = TRUE;
302 else
303 curcoll = savepv(curcoll);
304#endif /* USE_LOCALE_COLLATE */
305#ifdef USE_LOCALE_NUMERIC
306 if (! (curnum =
307 setlocale(LC_NUMERIC,
308 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
309 ? "" : Nullch)))
310 setlocale_failure = TRUE;
311 else
312 curnum = savepv(curnum);
313#endif /* USE_LOCALE_NUMERIC */
314 }
315
316#endif /* LC_ALL */
317
318#endif /* !LOCALE_ENVIRON_REQUIRED */
319
320#ifdef LC_ALL
321 if (! setlocale(LC_ALL, ""))
322 setlocale_failure = TRUE;
323#endif /* LC_ALL */
324
325 if (!setlocale_failure) {
326#ifdef USE_LOCALE_CTYPE
327 if (! (curctype = setlocale(LC_CTYPE, "")))
328 setlocale_failure = TRUE;
329 else
330 curctype = savepv(curctype);
331#endif /* USE_LOCALE_CTYPE */
332#ifdef USE_LOCALE_COLLATE
333 if (! (curcoll = setlocale(LC_COLLATE, "")))
334 setlocale_failure = TRUE;
335 else
336 curcoll = savepv(curcoll);
337#endif /* USE_LOCALE_COLLATE */
338#ifdef USE_LOCALE_NUMERIC
339 if (! (curnum = setlocale(LC_NUMERIC, "")))
340 setlocale_failure = TRUE;
341 else
342 curnum = savepv(curnum);
343#endif /* USE_LOCALE_NUMERIC */
344 }
345
346 if (setlocale_failure) {
347 char *p;
348 bool locwarn = (printwarn > 1 ||
349 (printwarn &&
350 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
351
352 if (locwarn) {
353#ifdef LC_ALL
354
355 PerlIO_printf(Perl_error_log,
356 "perl: warning: Setting locale failed.\n");
357
358#else /* !LC_ALL */
359
360 PerlIO_printf(Perl_error_log,
361 "perl: warning: Setting locale failed for the categories:\n\t");
362#ifdef USE_LOCALE_CTYPE
363 if (! curctype)
364 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
365#endif /* USE_LOCALE_CTYPE */
366#ifdef USE_LOCALE_COLLATE
367 if (! curcoll)
368 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
369#endif /* USE_LOCALE_COLLATE */
370#ifdef USE_LOCALE_NUMERIC
371 if (! curnum)
372 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
373#endif /* USE_LOCALE_NUMERIC */
374 PerlIO_printf(Perl_error_log, "\n");
375
376#endif /* LC_ALL */
377
378 PerlIO_printf(Perl_error_log,
379 "perl: warning: Please check that your locale settings:\n");
380
381#ifdef __GLIBC__
382 PerlIO_printf(Perl_error_log,
383 "\tLANGUAGE = %c%s%c,\n",
384 language ? '"' : '(',
385 language ? language : "unset",
386 language ? '"' : ')');
387#endif
388
389 PerlIO_printf(Perl_error_log,
390 "\tLC_ALL = %c%s%c,\n",
391 lc_all ? '"' : '(',
392 lc_all ? lc_all : "unset",
393 lc_all ? '"' : ')');
394
395#if defined(USE_ENVIRON_ARRAY)
396 {
397 char **e;
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);
404 }
405 }
406#else
407 PerlIO_printf(Perl_error_log,
408 "\t(possibly more locale environment variables)\n");
409#endif
410
411 PerlIO_printf(Perl_error_log,
412 "\tLANG = %c%s%c\n",
413 lang ? '"' : '(',
414 lang ? lang : "unset",
415 lang ? '"' : ')');
416
417 PerlIO_printf(Perl_error_log,
418 " are supported and installed on your system.\n");
419 }
420
421#ifdef LC_ALL
422
423 if (setlocale(LC_ALL, "C")) {
424 if (locwarn)
425 PerlIO_printf(Perl_error_log,
426 "perl: warning: Falling back to the standard locale (\"C\").\n");
427 ok = 0;
428 }
429 else {
430 if (locwarn)
431 PerlIO_printf(Perl_error_log,
432 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
433 ok = -1;
434 }
435
436#else /* ! LC_ALL */
437
438 if (0
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 */
448 )
449 {
450 if (locwarn)
451 PerlIO_printf(Perl_error_log,
452 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
453 ok = -1;
454 }
455
456#endif /* ! LC_ALL */
457
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 */
467 }
468 else {
469
470#ifdef USE_LOCALE_CTYPE
471 new_ctype(curctype);
472#endif /* USE_LOCALE_CTYPE */
473
474#ifdef USE_LOCALE_COLLATE
475 new_collate(curcoll);
476#endif /* USE_LOCALE_COLLATE */
477
478#ifdef USE_LOCALE_NUMERIC
479 new_numeric(curnum);
480#endif /* USE_LOCALE_NUMERIC */
b310b053 481
98994639
HS
482 }
483
484#endif /* USE_LOCALE */
485
ec71e770 486#ifdef USE_PERLIO
b310b053 487 {
fde18df1 488 /* Set PL_utf8locale to TRUE if using PerlIO _and_
ec71e770 489 any of the following are true:
085a54d9 490 - nl_langinfo(CODESET) contains /^utf-?8/i
61de9fb5 491 - $ENV{LC_ALL} contains /^utf-?8/i
085a54d9 492 - $ENV{LC_CTYPE} contains /^utf-?8/i
61de9fb5
JH
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.)
8aa8f774 500 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
a05d7ebb 501 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
fde18df1 502 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
085a54d9 503 */
fde18df1 504 bool utf8locale = FALSE;
b310b053
JH
505 char *codeset = NULL;
506#if defined(HAS_NL_LANGINFO) && defined(CODESET)
507 codeset = nl_langinfo(CODESET);
508#endif
61de9fb5 509 if (codeset)
fde18df1
JH
510 utf8locale = (ibcmp(codeset, "UTF-8", 5) == 0 ||
511 ibcmp(codeset, "UTF8", 4) == 0);
077a72a9 512#if defined(USE_LOCALE)
61de9fb5
JH
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. */
516 if (lang)
fde18df1
JH
517 utf8locale = (ibcmp(lang, "UTF-8", 5) == 0 ||
518 ibcmp(lang, "UTF8", 4) == 0);
b310b053 519#ifdef USE_LOCALE_CTYPE
61de9fb5 520 if (curctype)
fde18df1
JH
521 utf8locale = (ibcmp(curctype, "UTF-8", 5) == 0 ||
522 ibcmp(curctype, "UTF8", 4) == 0);
b310b053 523#endif
61de9fb5 524 if (lc_all)
fde18df1
JH
525 utf8locale = (ibcmp(lc_all, "UTF-8", 5) == 0 ||
526 ibcmp(lc_all, "UTF8", 4) == 0);
61de9fb5 527 }
fde18df1
JH
528#endif /* USE_LOCALE */
529 if (utf8locale)
530 PL_utf8locale = TRUE;
531 }
a05d7ebb 532 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
fde18df1
JH
533 This is an alternative to using the -C command line switch
534 (the -C if present will override this). */
535 {
dd374669 536 const char *p = PerlEnv_getenv("PERL_UNICODE");
a05d7ebb 537 PL_unicode = p ? parse_unicode_opts(&p) : 0;
b310b053 538 }
ec71e770 539#endif
b310b053 540
98994639
HS
541#ifdef USE_LOCALE_CTYPE
542 if (curctype != NULL)
543 Safefree(curctype);
544#endif /* USE_LOCALE_CTYPE */
545#ifdef USE_LOCALE_COLLATE
546 if (curcoll != NULL)
547 Safefree(curcoll);
548#endif /* USE_LOCALE_COLLATE */
549#ifdef USE_LOCALE_NUMERIC
550 if (curnum != NULL)
551 Safefree(curnum);
552#endif /* USE_LOCALE_NUMERIC */
553 return ok;
554}
555
556/* Backwards compatibility. */
557int
558Perl_init_i18nl14n(pTHX_ int printwarn)
559{
560 return init_i18nl10n(printwarn);
561}
562
563#ifdef USE_LOCALE_COLLATE
564
565/*
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.
571 */
166f8a29 572
98994639
HS
573char *
574Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
575{
576 char *xbuf;
577 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
578
579 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
580 /* the +1 is for the terminating NUL. */
581
582 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
583 New(171, xbuf, xAlloc, char);
584 if (! xbuf)
585 goto bad;
586
587 *(U32*)xbuf = PL_collation_ix;
588 xout = sizeof(PL_collation_ix);
589 for (xin = 0; xin < len; ) {
590 SSize_t xused;
591
592 for (;;) {
593 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
594 if (xused == -1)
595 goto bad;
eb160463 596 if ((STRLEN)xused < xAlloc - xout)
98994639
HS
597 break;
598 xAlloc = (2 * xAlloc) + 1;
599 Renew(xbuf, xAlloc, char);
600 if (! xbuf)
601 goto bad;
602 }
603
604 xin += strlen(s + xin) + 1;
605 xout += xused;
606
607 /* Embedded NULs are understood but silently skipped
608 * because they make no sense in locale collation. */
609 }
610
611 xbuf[xout] = '\0';
612 *xlen = xout - sizeof(PL_collation_ix);
613 return xbuf;
614
615 bad:
616 Safefree(xbuf);
617 *xlen = 0;
618 return NULL;
619}
620
621#endif /* USE_LOCALE_COLLATE */
622