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