This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Document that $ENV{PATH} may not contain relative directories under -T
[perl5.git] / locale.c
1 /*    locale.c
2  *
3  *    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4  *    2000, 2001, 2002, 2003, 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 = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
131         PL_numeric_local = TRUE;
132         set_numeric_radix();
133     }
134
135 #endif /* USE_LOCALE_NUMERIC */
136 }
137
138 void
139 Perl_set_numeric_standard(pTHX)
140 {
141 #ifdef USE_LOCALE_NUMERIC
142
143     if (! PL_numeric_standard) {
144         setlocale(LC_NUMERIC, "C");
145         PL_numeric_standard = TRUE;
146         PL_numeric_local = FALSE;
147         set_numeric_radix();
148     }
149
150 #endif /* USE_LOCALE_NUMERIC */
151 }
152
153 void
154 Perl_set_numeric_local(pTHX)
155 {
156 #ifdef USE_LOCALE_NUMERIC
157
158     if (! PL_numeric_local) {
159         setlocale(LC_NUMERIC, PL_numeric_name);
160         PL_numeric_standard = FALSE;
161         PL_numeric_local = TRUE;
162         set_numeric_radix();
163     }
164
165 #endif /* USE_LOCALE_NUMERIC */
166 }
167
168 /*
169  * Set up for a new ctype locale.
170  */
171 void
172 Perl_new_ctype(pTHX_ char *newctype)
173 {
174 #ifdef USE_LOCALE_CTYPE
175
176     int i;
177
178     for (i = 0; i < 256; i++) {
179         if (isUPPER_LC(i))
180             PL_fold_locale[i] = toLOWER_LC(i);
181         else if (isLOWER_LC(i))
182             PL_fold_locale[i] = toUPPER_LC(i);
183         else
184             PL_fold_locale[i] = i;
185     }
186
187 #endif /* USE_LOCALE_CTYPE */
188 }
189
190 /*
191  * Set up for a new collation locale.
192  */
193 void
194 Perl_new_collate(pTHX_ char *newcoll)
195 {
196 #ifdef USE_LOCALE_COLLATE
197
198     if (! newcoll) {
199         if (PL_collation_name) {
200             ++PL_collation_ix;
201             Safefree(PL_collation_name);
202             PL_collation_name = NULL;
203         }
204         PL_collation_standard = TRUE;
205         PL_collxfrm_base = 0;
206         PL_collxfrm_mult = 2;
207         return;
208     }
209
210     if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
211         ++PL_collation_ix;
212         Safefree(PL_collation_name);
213         PL_collation_name = stdize_locale(savepv(newcoll));
214         PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
215
216         {
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           Size_t fa = strxfrm(xbuf, "a",  XFRMBUFSIZE);
222           Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
223           SSize_t mult = fb - fa;
224           if (mult < 1)
225               Perl_croak(aTHX_ "strxfrm() gets absurd");
226           PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
227           PL_collxfrm_mult = mult;
228         }
229     }
230
231 #endif /* USE_LOCALE_COLLATE */
232 }
233
234 /*
235  * Initialize locale awareness.
236  */
237 int
238 Perl_init_i18nl10n(pTHX_ int printwarn)
239 {
240     int ok = 1;
241     /* returns
242      *    1 = set ok or not applicable,
243      *    0 = fallback to C locale,
244      *   -1 = fallback to C locale failed
245      */
246
247 #if defined(USE_LOCALE)
248
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
256     char *curnum     = NULL;
257 #endif /* USE_LOCALE_NUMERIC */
258 #ifdef __GLIBC__
259     char *language   = PerlEnv_getenv("LANGUAGE");
260 #endif
261     char *lc_all     = PerlEnv_getenv("LC_ALL");
262     char *lang       = PerlEnv_getenv("LANG");
263     bool setlocale_failure = FALSE;
264
265 #ifdef LOCALE_ENVIRON_REQUIRED
266
267     /*
268      * Ultrix setlocale(..., "") fails if there are no environment
269      * variables from which to get a locale name.
270      */
271
272     bool done = FALSE;
273
274 #ifdef LC_ALL
275     if (lang) {
276         if (setlocale(LC_ALL, ""))
277             done = TRUE;
278         else
279             setlocale_failure = TRUE;
280     }
281     if (!setlocale_failure) {
282 #ifdef USE_LOCALE_CTYPE
283         if (! (curctype =
284                setlocale(LC_CTYPE,
285                          (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
286                                     ? "" : Nullch)))
287             setlocale_failure = TRUE;
288         else
289             curctype = savepv(curctype);
290 #endif /* USE_LOCALE_CTYPE */
291 #ifdef USE_LOCALE_COLLATE
292         if (! (curcoll =
293                setlocale(LC_COLLATE,
294                          (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
295                                    ? "" : Nullch)))
296             setlocale_failure = TRUE;
297         else
298             curcoll = savepv(curcoll);
299 #endif /* USE_LOCALE_COLLATE */
300 #ifdef USE_LOCALE_NUMERIC
301         if (! (curnum =
302                setlocale(LC_NUMERIC,
303                          (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
304                                   ? "" : Nullch)))
305             setlocale_failure = TRUE;
306         else
307             curnum = savepv(curnum);
308 #endif /* USE_LOCALE_NUMERIC */
309     }
310
311 #endif /* LC_ALL */
312
313 #endif /* !LOCALE_ENVIRON_REQUIRED */
314
315 #ifdef LC_ALL
316     if (! setlocale(LC_ALL, ""))
317         setlocale_failure = TRUE;
318 #endif /* LC_ALL */
319
320     if (!setlocale_failure) {
321 #ifdef USE_LOCALE_CTYPE
322         if (! (curctype = setlocale(LC_CTYPE, "")))
323             setlocale_failure = TRUE;
324         else
325             curctype = savepv(curctype);
326 #endif /* USE_LOCALE_CTYPE */
327 #ifdef USE_LOCALE_COLLATE
328         if (! (curcoll = setlocale(LC_COLLATE, "")))
329             setlocale_failure = TRUE;
330         else
331             curcoll = savepv(curcoll);
332 #endif /* USE_LOCALE_COLLATE */
333 #ifdef USE_LOCALE_NUMERIC
334         if (! (curnum = setlocale(LC_NUMERIC, "")))
335             setlocale_failure = TRUE;
336         else
337             curnum = savepv(curnum);
338 #endif /* USE_LOCALE_NUMERIC */
339     }
340
341     if (setlocale_failure) {
342         char *p;
343         bool locwarn = (printwarn > 1 ||
344                         (printwarn &&
345                          (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
346
347         if (locwarn) {
348 #ifdef LC_ALL
349
350             PerlIO_printf(Perl_error_log,
351                "perl: warning: Setting locale failed.\n");
352
353 #else /* !LC_ALL */
354
355             PerlIO_printf(Perl_error_log,
356                "perl: warning: Setting locale failed for the categories:\n\t");
357 #ifdef USE_LOCALE_CTYPE
358             if (! curctype)
359                 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
360 #endif /* USE_LOCALE_CTYPE */
361 #ifdef USE_LOCALE_COLLATE
362             if (! curcoll)
363                 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
364 #endif /* USE_LOCALE_COLLATE */
365 #ifdef USE_LOCALE_NUMERIC
366             if (! curnum)
367                 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
368 #endif /* USE_LOCALE_NUMERIC */
369             PerlIO_printf(Perl_error_log, "\n");
370
371 #endif /* LC_ALL */
372
373             PerlIO_printf(Perl_error_log,
374                 "perl: warning: Please check that your locale settings:\n");
375
376 #ifdef __GLIBC__
377             PerlIO_printf(Perl_error_log,
378                           "\tLANGUAGE = %c%s%c,\n",
379                           language ? '"' : '(',
380                           language ? language : "unset",
381                           language ? '"' : ')');
382 #endif
383
384             PerlIO_printf(Perl_error_log,
385                           "\tLC_ALL = %c%s%c,\n",
386                           lc_all ? '"' : '(',
387                           lc_all ? lc_all : "unset",
388                           lc_all ? '"' : ')');
389
390 #if defined(USE_ENVIRON_ARRAY)
391             {
392               char **e;
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);
399               }
400             }
401 #else
402             PerlIO_printf(Perl_error_log,
403                           "\t(possibly more locale environment variables)\n");
404 #endif
405
406             PerlIO_printf(Perl_error_log,
407                           "\tLANG = %c%s%c\n",
408                           lang ? '"' : '(',
409                           lang ? lang : "unset",
410                           lang ? '"' : ')');
411
412             PerlIO_printf(Perl_error_log,
413                           "    are supported and installed on your system.\n");
414         }
415
416 #ifdef LC_ALL
417
418         if (setlocale(LC_ALL, "C")) {
419             if (locwarn)
420                 PerlIO_printf(Perl_error_log,
421       "perl: warning: Falling back to the standard locale (\"C\").\n");
422             ok = 0;
423         }
424         else {
425             if (locwarn)
426                 PerlIO_printf(Perl_error_log,
427       "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
428             ok = -1;
429         }
430
431 #else /* ! LC_ALL */
432
433         if (0
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 */
443             )
444         {
445             if (locwarn)
446                 PerlIO_printf(Perl_error_log,
447       "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
448             ok = -1;
449         }
450
451 #endif /* ! LC_ALL */
452
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 */
462     }
463     else {
464
465 #ifdef USE_LOCALE_CTYPE
466     new_ctype(curctype);
467 #endif /* USE_LOCALE_CTYPE */
468
469 #ifdef USE_LOCALE_COLLATE
470     new_collate(curcoll);
471 #endif /* USE_LOCALE_COLLATE */
472
473 #ifdef USE_LOCALE_NUMERIC
474     new_numeric(curnum);
475 #endif /* USE_LOCALE_NUMERIC */
476
477     }
478
479 #endif /* USE_LOCALE */
480
481 #ifdef USE_PERLIO
482     {
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.
498       */
499          bool utf8locale = FALSE;
500          char *codeset = NULL;
501 #if defined(HAS_NL_LANGINFO) && defined(CODESET)
502          codeset = nl_langinfo(CODESET);
503 #endif
504          if (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. */ 
511               if (lang)
512                    utf8locale = (ibcmp(lang,     "UTF-8", 5) == 0 ||
513                                  ibcmp(lang,     "UTF8",  4) == 0);
514 #ifdef USE_LOCALE_CTYPE
515               if (curctype)
516                    utf8locale = (ibcmp(curctype,     "UTF-8", 5) == 0 ||
517                                  ibcmp(curctype,     "UTF8",  4) == 0);
518 #endif
519               if (lc_all)
520                    utf8locale = (ibcmp(lc_all,   "UTF-8", 5) == 0 ||
521                                  ibcmp(lc_all,   "UTF8",  4) == 0);
522          }
523 #endif /* USE_LOCALE */
524          if (utf8locale)
525               PL_utf8locale = TRUE;
526     }
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). */
530     {
531          char *p = PerlEnv_getenv("PERL_UNICODE");
532          PL_unicode = p ? parse_unicode_opts(&p) : 0;
533     }
534 #endif
535
536 #ifdef USE_LOCALE_CTYPE
537     if (curctype != NULL)
538         Safefree(curctype);
539 #endif /* USE_LOCALE_CTYPE */
540 #ifdef USE_LOCALE_COLLATE
541     if (curcoll != NULL)
542         Safefree(curcoll);
543 #endif /* USE_LOCALE_COLLATE */
544 #ifdef USE_LOCALE_NUMERIC
545     if (curnum != NULL)
546         Safefree(curnum);
547 #endif /* USE_LOCALE_NUMERIC */
548     return ok;
549 }
550
551 /* Backwards compatibility. */
552 int
553 Perl_init_i18nl14n(pTHX_ int printwarn)
554 {
555     return init_i18nl10n(printwarn);
556 }
557
558 #ifdef USE_LOCALE_COLLATE
559
560 /*
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.
566  */
567
568 char *
569 Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
570 {
571     char *xbuf;
572     STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
573
574     /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
575     /* the +1 is for the terminating NUL. */
576
577     xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
578     New(171, xbuf, xAlloc, char);
579     if (! xbuf)
580         goto bad;
581
582     *(U32*)xbuf = PL_collation_ix;
583     xout = sizeof(PL_collation_ix);
584     for (xin = 0; xin < len; ) {
585         SSize_t xused;
586
587         for (;;) {
588             xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
589             if (xused == -1)
590                 goto bad;
591             if ((STRLEN)xused < xAlloc - xout)
592                 break;
593             xAlloc = (2 * xAlloc) + 1;
594             Renew(xbuf, xAlloc, char);
595             if (! xbuf)
596                 goto bad;
597         }
598
599         xin += strlen(s + xin) + 1;
600         xout += xused;
601
602         /* Embedded NULs are understood but silently skipped
603          * because they make no sense in locale collation. */
604     }
605
606     xbuf[xout] = '\0';
607     *xlen = xout - sizeof(PL_collation_ix);
608     return xbuf;
609
610   bad:
611     Safefree(xbuf);
612     *xlen = 0;
613     return NULL;
614 }
615
616 #endif /* USE_LOCALE_COLLATE */
617