This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/test.pl: Add option to runperl to suppress STDERR
[perl5.git] / locale.c
CommitLineData
98994639
HS
1/* locale.c
2 *
1129b882
NC
3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 * 2002, 2003, 2005, 2006, 2007, 2008 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/*
4ac71550 12 * A Elbereth Gilthoniel,
cdad3b53 13 * silivren penna míriel
4ac71550 14 * o menel aglar elenath!
cdad3b53 15 * Na-chaered palan-díriel
4ac71550
TC
16 * o galadhremmin ennorath,
17 * Fanuilos, le linnathon
18 * nef aear, si nef aearon!
19 *
20 * [p.238 of _The Lord of the Rings_, II/i: "Many Meetings"]
98994639
HS
21 */
22
166f8a29
DM
23/* utility functions for handling locale-specific stuff like what
24 * character represents the decimal point.
0d071d52
KW
25 *
26 * All C programs have an underlying locale. Perl generally doesn't pay any
27 * attention to it except within the scope of a 'use locale'. For most
28 * categories, it accomplishes this by just using different operations if it is
29 * in such scope than if not. However, various libc functions called by Perl
30 * are affected by the LC_NUMERIC category, so there are macros in perl.h that
31 * are used to toggle between the current locale and the C locale depending on
32 * the desired behavior of those functions at the moment.
166f8a29
DM
33 */
34
98994639
HS
35#include "EXTERN.h"
36#define PERL_IN_LOCALE_C
37#include "perl.h"
38
b310b053
JH
39#ifdef I_LANGINFO
40# include <langinfo.h>
41#endif
42
a4af207c
JH
43#include "reentr.h"
44
8ef6e574
KW
45#ifdef USE_LOCALE
46
98994639 47/*
0d071d52
KW
48 * Standardize the locale name from a string returned by 'setlocale', possibly
49 * modifying that string.
98994639 50 *
0ef2a2b2 51 * The typical return value of setlocale() is either
98994639
HS
52 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
53 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
54 * (the space-separated values represent the various sublocales,
0ef2a2b2 55 * in some unspecified order). This is not handled by this function.
98994639
HS
56 *
57 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
0ef2a2b2
KW
58 * which is harmful for further use of the string in setlocale(). This
59 * function removes the trailing new line and everything up through the '='
98994639
HS
60 *
61 */
62STATIC char *
63S_stdize_locale(pTHX_ char *locs)
64{
7452cf6a 65 const char * const s = strchr(locs, '=');
98994639
HS
66 bool okay = TRUE;
67
7918f24d
NC
68 PERL_ARGS_ASSERT_STDIZE_LOCALE;
69
8772537c
AL
70 if (s) {
71 const char * const t = strchr(s, '.');
98994639 72 okay = FALSE;
8772537c
AL
73 if (t) {
74 const char * const u = strchr(t, '\n');
75 if (u && (u[1] == 0)) {
76 const STRLEN len = u - s;
77 Move(s + 1, locs, len, char);
78 locs[len] = 0;
79 okay = TRUE;
98994639
HS
80 }
81 }
82 }
83
84 if (!okay)
85 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
86
87 return locs;
88}
89
8ef6e574
KW
90#endif
91
98994639
HS
92void
93Perl_set_numeric_radix(pTHX)
94{
95#ifdef USE_LOCALE_NUMERIC
97aff369 96 dVAR;
98994639 97# ifdef HAS_LOCALECONV
7452cf6a 98 const struct lconv* const lc = localeconv();
98994639 99
98994639
HS
100 if (lc && lc->decimal_point) {
101 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
102 SvREFCNT_dec(PL_numeric_radix_sv);
a0714e2c 103 PL_numeric_radix_sv = NULL;
98994639
HS
104 }
105 else {
106 if (PL_numeric_radix_sv)
107 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
108 else
109 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
28acfe03
KW
110 if (! is_ascii_string((U8 *) lc->decimal_point, 0)
111 && is_utf8_string((U8 *) lc->decimal_point, 0)
112 && is_cur_LC_category_utf8(LC_NUMERIC))
113 {
114 SvUTF8_on(PL_numeric_radix_sv);
115 }
98994639
HS
116 }
117 }
118 else
a0714e2c 119 PL_numeric_radix_sv = NULL;
69014004
KW
120
121 DEBUG_L(PerlIO_printf(Perl_debug_log, "Locale radix is %s\n",
122 (PL_numeric_radix_sv)
123 ? lc->decimal_point
124 : "NULL"));
125
98994639
HS
126# endif /* HAS_LOCALECONV */
127#endif /* USE_LOCALE_NUMERIC */
128}
129
98994639 130void
8772537c 131Perl_new_numeric(pTHX_ const char *newnum)
98994639
HS
132{
133#ifdef USE_LOCALE_NUMERIC
0d071d52
KW
134
135 /* Called after all libc setlocale() calls affecting LC_NUMERIC, to tell
136 * core Perl this and that 'newnum' is the name of the new locale.
137 * It installs this locale as the current underlying default.
138 *
139 * The default locale and the C locale can be toggled between by use of the
140 * set_numeric_local() and set_numeric_standard() functions, which should
141 * probably not be called directly, but only via macros like
142 * SET_NUMERIC_STANDARD() in perl.h.
143 *
144 * The toggling is necessary mainly so that a non-dot radix decimal point
145 * character can be output, while allowing internal calculations to use a
146 * dot.
147 *
148 * This sets several interpreter-level variables:
149 * PL_numeric_name The default locale's name: a copy of 'newnum'
150 * PL_numeric_local A boolean indicating if the toggled state is such
151 * that the current locale is the default locale
152 * PL_numeric_standard A boolean indicating if the toggled state is such
153 * that the current locale is the C locale
154 * Note that both of the last two variables can be true at the same time,
155 * if the underlying locale is C. (Toggling is a no-op under these
156 * circumstances.)
157 *
158 * Any code changing the locale (outside this file) should use
159 * POSIX::setlocale, which calls this function. Therefore this function
160 * should be called directly only from this file and from
161 * POSIX::setlocale() */
162
b03f34cf 163 char *save_newnum;
97aff369 164 dVAR;
98994639
HS
165
166 if (! newnum) {
43c5f42d
NC
167 Safefree(PL_numeric_name);
168 PL_numeric_name = NULL;
98994639
HS
169 PL_numeric_standard = TRUE;
170 PL_numeric_local = TRUE;
171 return;
172 }
173
b03f34cf
KW
174 save_newnum = stdize_locale(savepv(newnum));
175 if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) {
98994639 176 Safefree(PL_numeric_name);
b03f34cf 177 PL_numeric_name = save_newnum;
b03f34cf 178 }
98994639 179
e19f01cb
KW
180 PL_numeric_standard = ((*save_newnum == 'C' && save_newnum[1] == '\0')
181 || strEQ(save_newnum, "POSIX"));
182 PL_numeric_local = TRUE;
183 set_numeric_radix();
6959d69d 184
98994639
HS
185#endif /* USE_LOCALE_NUMERIC */
186}
187
188void
189Perl_set_numeric_standard(pTHX)
190{
191#ifdef USE_LOCALE_NUMERIC
97aff369 192 dVAR;
98994639 193
0d071d52
KW
194 /* Toggle the LC_NUMERIC locale to C, if not already there. Probably
195 * should use the macros like SET_NUMERIC_STANDARD() in perl.h instead of
196 * calling this directly. */
197
98994639
HS
198 if (! PL_numeric_standard) {
199 setlocale(LC_NUMERIC, "C");
200 PL_numeric_standard = TRUE;
201 PL_numeric_local = FALSE;
202 set_numeric_radix();
203 }
69014004
KW
204 DEBUG_L(PerlIO_printf(Perl_debug_log,
205 "Underlying LC_NUMERIC locale now is C\n"));
98994639
HS
206
207#endif /* USE_LOCALE_NUMERIC */
208}
209
210void
211Perl_set_numeric_local(pTHX)
212{
213#ifdef USE_LOCALE_NUMERIC
97aff369 214 dVAR;
98994639 215
0d071d52
KW
216 /* Toggle the LC_NUMERIC locale to the current underlying default, if not
217 * already there. Probably should use the macros like SET_NUMERIC_LOCAL()
218 * in perl.h instead of calling this directly. */
219
98994639
HS
220 if (! PL_numeric_local) {
221 setlocale(LC_NUMERIC, PL_numeric_name);
222 PL_numeric_standard = FALSE;
223 PL_numeric_local = TRUE;
224 set_numeric_radix();
225 }
69014004
KW
226 DEBUG_L(PerlIO_printf(Perl_debug_log,
227 "Underlying LC_NUMERIC locale now is %s\n",
228 PL_numeric_name));
98994639
HS
229
230#endif /* USE_LOCALE_NUMERIC */
231}
232
233/*
234 * Set up for a new ctype locale.
235 */
236void
8772537c 237Perl_new_ctype(pTHX_ const char *newctype)
98994639
HS
238{
239#ifdef USE_LOCALE_CTYPE
0d071d52
KW
240
241 /* Called after all libc setlocale() calls affecting LC_CTYPE, to tell
242 * core Perl this and that 'newctype' is the name of the new locale.
243 *
244 * This function sets up the folding arrays for all 256 bytes, assuming
245 * that tofold() is tolc() since fold case is not a concept in POSIX,
246 *
247 * Any code changing the locale (outside this file) should use
248 * POSIX::setlocale, which calls this function. Therefore this function
249 * should be called directly only from this file and from
250 * POSIX::setlocale() */
251
27da23d5 252 dVAR;
68067e4e 253 UV i;
98994639 254
7918f24d
NC
255 PERL_ARGS_ASSERT_NEW_CTYPE;
256
31f05a37
KW
257 PL_in_utf8_CTYPE_locale = is_cur_LC_category_utf8(LC_CTYPE);
258
259 /* A UTF-8 locale gets standard rules. But note that code still has to
260 * handle this specially because of the three problematic code points */
261 if (PL_in_utf8_CTYPE_locale) {
262 Copy(PL_fold_latin1, PL_fold_locale, 256, U8);
263 }
264 else {
baa60164
KW
265 for (i = 0; i < 256; i++) {
266 if (isUPPER_LC((U8) i))
267 PL_fold_locale[i] = (U8) toLOWER_LC((U8) i);
268 else if (isLOWER_LC((U8) i))
269 PL_fold_locale[i] = (U8) toUPPER_LC((U8) i);
270 else
271 PL_fold_locale[i] = (U8) i;
272 }
31f05a37 273 }
98994639
HS
274
275#endif /* USE_LOCALE_CTYPE */
7918f24d 276 PERL_ARGS_ASSERT_NEW_CTYPE;
8772537c 277 PERL_UNUSED_ARG(newctype);
96a5add6 278 PERL_UNUSED_CONTEXT;
98994639
HS
279}
280
98994639 281void
8772537c 282Perl_new_collate(pTHX_ const char *newcoll)
98994639
HS
283{
284#ifdef USE_LOCALE_COLLATE
0d071d52
KW
285
286 /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell
287 * core Perl this and that 'newcoll' is the name of the new locale.
288 *
289 * Any code changing the locale (outside this file) should use
290 * POSIX::setlocale, which calls this function. Therefore this function
291 * should be called directly only from this file and from
292 * POSIX::setlocale() */
293
97aff369 294 dVAR;
98994639
HS
295
296 if (! newcoll) {
297 if (PL_collation_name) {
298 ++PL_collation_ix;
299 Safefree(PL_collation_name);
300 PL_collation_name = NULL;
301 }
302 PL_collation_standard = TRUE;
303 PL_collxfrm_base = 0;
304 PL_collxfrm_mult = 2;
305 return;
306 }
307
308 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
309 ++PL_collation_ix;
310 Safefree(PL_collation_name);
311 PL_collation_name = stdize_locale(savepv(newcoll));
770526c1
NC
312 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
313 || strEQ(newcoll, "POSIX"));
98994639
HS
314
315 {
316 /* 2: at most so many chars ('a', 'b'). */
317 /* 50: surely no system expands a char more. */
318#define XFRMBUFSIZE (2 * 50)
319 char xbuf[XFRMBUFSIZE];
8772537c
AL
320 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
321 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
322 const SSize_t mult = fb - fa;
e0601d3c 323 if (mult < 1 && !(fa == 0 && fb == 0))
ad49ad39
NC
324 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
325 (UV) fa, (UV) fb);
eb160463 326 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
98994639
HS
327 PL_collxfrm_mult = mult;
328 }
329 }
330
331#endif /* USE_LOCALE_COLLATE */
332}
333
334/*
335 * Initialize locale awareness.
336 */
337int
338Perl_init_i18nl10n(pTHX_ int printwarn)
339{
340 int ok = 1;
341 /* returns
342 * 1 = set ok or not applicable,
343 * 0 = fallback to C locale,
344 * -1 = fallback to C locale failed
345 */
346
347#if defined(USE_LOCALE)
97aff369 348 dVAR;
98994639
HS
349
350#ifdef USE_LOCALE_CTYPE
351 char *curctype = NULL;
352#endif /* USE_LOCALE_CTYPE */
353#ifdef USE_LOCALE_COLLATE
354 char *curcoll = NULL;
355#endif /* USE_LOCALE_COLLATE */
356#ifdef USE_LOCALE_NUMERIC
357 char *curnum = NULL;
358#endif /* USE_LOCALE_NUMERIC */
359#ifdef __GLIBC__
7452cf6a 360 char * const language = PerlEnv_getenv("LANGUAGE");
98994639 361#endif
ccd65d51
KW
362 /* NULL uses the existing already set up locale */
363 const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT"))
364 ? NULL
365 : "";
7452cf6a
AL
366 char * const lc_all = PerlEnv_getenv("LC_ALL");
367 char * const lang = PerlEnv_getenv("LANG");
98994639
HS
368 bool setlocale_failure = FALSE;
369
370#ifdef LOCALE_ENVIRON_REQUIRED
371
372 /*
373 * Ultrix setlocale(..., "") fails if there are no environment
374 * variables from which to get a locale name.
375 */
376
377 bool done = FALSE;
378
b3e384bf 379# ifdef LC_ALL
98994639 380 if (lang) {
ccd65d51 381 if (setlocale(LC_ALL, setlocale_init))
98994639
HS
382 done = TRUE;
383 else
384 setlocale_failure = TRUE;
385 }
386 if (!setlocale_failure) {
b3e384bf 387# ifdef USE_LOCALE_CTYPE
56279a21 388 Safefree(curctype);
98994639
HS
389 if (! (curctype =
390 setlocale(LC_CTYPE,
391 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
ccd65d51 392 ? setlocale_init : NULL)))
98994639
HS
393 setlocale_failure = TRUE;
394 else
395 curctype = savepv(curctype);
b3e384bf
KW
396# endif /* USE_LOCALE_CTYPE */
397# ifdef USE_LOCALE_COLLATE
56279a21 398 Safefree(curcoll);
98994639
HS
399 if (! (curcoll =
400 setlocale(LC_COLLATE,
401 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
ccd65d51 402 ? setlocale_init : NULL)))
98994639
HS
403 setlocale_failure = TRUE;
404 else
405 curcoll = savepv(curcoll);
b3e384bf
KW
406# endif /* USE_LOCALE_COLLATE */
407# ifdef USE_LOCALE_NUMERIC
56279a21 408 Safefree(curnum);
98994639
HS
409 if (! (curnum =
410 setlocale(LC_NUMERIC,
411 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
ccd65d51 412 ? setlocale_init : NULL)))
98994639
HS
413 setlocale_failure = TRUE;
414 else
415 curnum = savepv(curnum);
b3e384bf 416# endif /* USE_LOCALE_NUMERIC */
98994639
HS
417 }
418
b3e384bf 419# endif /* LC_ALL */
98994639
HS
420
421#endif /* !LOCALE_ENVIRON_REQUIRED */
422
423#ifdef LC_ALL
ccd65d51 424 if (! setlocale(LC_ALL, setlocale_init))
98994639
HS
425 setlocale_failure = TRUE;
426#endif /* LC_ALL */
427
428 if (!setlocale_failure) {
429#ifdef USE_LOCALE_CTYPE
6cb43dbf 430 Safefree(curctype);
ccd65d51 431 if (! (curctype = setlocale(LC_CTYPE, setlocale_init)))
98994639
HS
432 setlocale_failure = TRUE;
433 else
434 curctype = savepv(curctype);
435#endif /* USE_LOCALE_CTYPE */
436#ifdef USE_LOCALE_COLLATE
6cb43dbf 437 Safefree(curcoll);
ccd65d51 438 if (! (curcoll = setlocale(LC_COLLATE, setlocale_init)))
98994639
HS
439 setlocale_failure = TRUE;
440 else
441 curcoll = savepv(curcoll);
442#endif /* USE_LOCALE_COLLATE */
443#ifdef USE_LOCALE_NUMERIC
6cb43dbf 444 Safefree(curnum);
ccd65d51 445 if (! (curnum = setlocale(LC_NUMERIC, setlocale_init)))
98994639
HS
446 setlocale_failure = TRUE;
447 else
448 curnum = savepv(curnum);
449#endif /* USE_LOCALE_NUMERIC */
450 }
451
452 if (setlocale_failure) {
453 char *p;
0bd48802 454 const bool locwarn = (printwarn > 1 ||
98994639
HS
455 (printwarn &&
456 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
457
458 if (locwarn) {
459#ifdef LC_ALL
460
461 PerlIO_printf(Perl_error_log,
462 "perl: warning: Setting locale failed.\n");
463
464#else /* !LC_ALL */
465
466 PerlIO_printf(Perl_error_log,
467 "perl: warning: Setting locale failed for the categories:\n\t");
468#ifdef USE_LOCALE_CTYPE
469 if (! curctype)
470 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
471#endif /* USE_LOCALE_CTYPE */
472#ifdef USE_LOCALE_COLLATE
473 if (! curcoll)
474 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
475#endif /* USE_LOCALE_COLLATE */
476#ifdef USE_LOCALE_NUMERIC
477 if (! curnum)
478 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
479#endif /* USE_LOCALE_NUMERIC */
480 PerlIO_printf(Perl_error_log, "\n");
481
482#endif /* LC_ALL */
483
484 PerlIO_printf(Perl_error_log,
485 "perl: warning: Please check that your locale settings:\n");
486
487#ifdef __GLIBC__
488 PerlIO_printf(Perl_error_log,
489 "\tLANGUAGE = %c%s%c,\n",
490 language ? '"' : '(',
491 language ? language : "unset",
492 language ? '"' : ')');
493#endif
494
495 PerlIO_printf(Perl_error_log,
496 "\tLC_ALL = %c%s%c,\n",
497 lc_all ? '"' : '(',
498 lc_all ? lc_all : "unset",
499 lc_all ? '"' : ')');
500
501#if defined(USE_ENVIRON_ARRAY)
502 {
503 char **e;
504 for (e = environ; *e; e++) {
505 if (strnEQ(*e, "LC_", 3)
506 && strnNE(*e, "LC_ALL=", 7)
507 && (p = strchr(*e, '=')))
508 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
509 (int)(p - *e), *e, p + 1);
510 }
511 }
512#else
513 PerlIO_printf(Perl_error_log,
514 "\t(possibly more locale environment variables)\n");
515#endif
516
517 PerlIO_printf(Perl_error_log,
518 "\tLANG = %c%s%c\n",
519 lang ? '"' : '(',
520 lang ? lang : "unset",
521 lang ? '"' : ')');
522
523 PerlIO_printf(Perl_error_log,
524 " are supported and installed on your system.\n");
525 }
526
527#ifdef LC_ALL
528
529 if (setlocale(LC_ALL, "C")) {
530 if (locwarn)
531 PerlIO_printf(Perl_error_log,
532 "perl: warning: Falling back to the standard locale (\"C\").\n");
533 ok = 0;
534 }
535 else {
536 if (locwarn)
537 PerlIO_printf(Perl_error_log,
538 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
539 ok = -1;
540 }
541
542#else /* ! LC_ALL */
543
544 if (0
545#ifdef USE_LOCALE_CTYPE
546 || !(curctype || setlocale(LC_CTYPE, "C"))
547#endif /* USE_LOCALE_CTYPE */
548#ifdef USE_LOCALE_COLLATE
549 || !(curcoll || setlocale(LC_COLLATE, "C"))
550#endif /* USE_LOCALE_COLLATE */
551#ifdef USE_LOCALE_NUMERIC
552 || !(curnum || setlocale(LC_NUMERIC, "C"))
553#endif /* USE_LOCALE_NUMERIC */
554 )
555 {
556 if (locwarn)
557 PerlIO_printf(Perl_error_log,
558 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
559 ok = -1;
560 }
561
562#endif /* ! LC_ALL */
563
564#ifdef USE_LOCALE_CTYPE
56279a21 565 Safefree(curctype);
bd61b366 566 curctype = savepv(setlocale(LC_CTYPE, NULL));
98994639
HS
567#endif /* USE_LOCALE_CTYPE */
568#ifdef USE_LOCALE_COLLATE
56279a21 569 Safefree(curcoll);
bd61b366 570 curcoll = savepv(setlocale(LC_COLLATE, NULL));
98994639
HS
571#endif /* USE_LOCALE_COLLATE */
572#ifdef USE_LOCALE_NUMERIC
56279a21 573 Safefree(curnum);
bd61b366 574 curnum = savepv(setlocale(LC_NUMERIC, NULL));
98994639
HS
575#endif /* USE_LOCALE_NUMERIC */
576 }
577 else {
578
579#ifdef USE_LOCALE_CTYPE
580 new_ctype(curctype);
581#endif /* USE_LOCALE_CTYPE */
582
583#ifdef USE_LOCALE_COLLATE
584 new_collate(curcoll);
585#endif /* USE_LOCALE_COLLATE */
586
587#ifdef USE_LOCALE_NUMERIC
588 new_numeric(curnum);
589#endif /* USE_LOCALE_NUMERIC */
b310b053 590
98994639
HS
591 }
592
8ef6e574 593#if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE)
b310b053 594 {
fde18df1 595 /* Set PL_utf8locale to TRUE if using PerlIO _and_
7d74bb61 596 the current LC_CTYPE locale is UTF-8.
8aa8f774 597 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
a05d7ebb 598 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
fde18df1 599 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
085a54d9 600 */
7d74bb61 601 PL_utf8locale = is_cur_LC_category_utf8(LC_CTYPE);
fde18df1 602 }
a05d7ebb 603 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
fde18df1
JH
604 This is an alternative to using the -C command line switch
605 (the -C if present will override this). */
606 {
dd374669 607 const char *p = PerlEnv_getenv("PERL_UNICODE");
a05d7ebb 608 PL_unicode = p ? parse_unicode_opts(&p) : 0;
5a22a2bb
NC
609 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
610 PL_utf8cache = -1;
b310b053 611 }
ec71e770 612#endif
b310b053 613
98994639 614#ifdef USE_LOCALE_CTYPE
43c5f42d 615 Safefree(curctype);
98994639
HS
616#endif /* USE_LOCALE_CTYPE */
617#ifdef USE_LOCALE_COLLATE
43c5f42d 618 Safefree(curcoll);
98994639
HS
619#endif /* USE_LOCALE_COLLATE */
620#ifdef USE_LOCALE_NUMERIC
43c5f42d 621 Safefree(curnum);
98994639 622#endif /* USE_LOCALE_NUMERIC */
8ef6e574
KW
623
624#endif /* USE_LOCALE */
625
98994639
HS
626 return ok;
627}
628
98994639
HS
629#ifdef USE_LOCALE_COLLATE
630
631/*
632 * mem_collxfrm() is a bit like strxfrm() but with two important
633 * differences. First, it handles embedded NULs. Second, it allocates
634 * a bit more memory than needed for the transformed data itself.
635 * The real transformed data begins at offset sizeof(collationix).
636 * Please see sv_collxfrm() to see how this is used.
637 */
166f8a29 638
98994639
HS
639char *
640Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
641{
97aff369 642 dVAR;
98994639
HS
643 char *xbuf;
644 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
645
7918f24d
NC
646 PERL_ARGS_ASSERT_MEM_COLLXFRM;
647
98994639
HS
648 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
649 /* the +1 is for the terminating NUL. */
650
651 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
a02a5408 652 Newx(xbuf, xAlloc, char);
98994639
HS
653 if (! xbuf)
654 goto bad;
655
656 *(U32*)xbuf = PL_collation_ix;
657 xout = sizeof(PL_collation_ix);
658 for (xin = 0; xin < len; ) {
224e8ef5 659 Size_t xused;
98994639
HS
660
661 for (;;) {
662 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
224e8ef5 663 if (xused >= PERL_INT_MAX)
98994639 664 goto bad;
eb160463 665 if ((STRLEN)xused < xAlloc - xout)
98994639
HS
666 break;
667 xAlloc = (2 * xAlloc) + 1;
668 Renew(xbuf, xAlloc, char);
669 if (! xbuf)
670 goto bad;
671 }
672
673 xin += strlen(s + xin) + 1;
674 xout += xused;
675
676 /* Embedded NULs are understood but silently skipped
677 * because they make no sense in locale collation. */
678 }
679
680 xbuf[xout] = '\0';
681 *xlen = xout - sizeof(PL_collation_ix);
682 return xbuf;
683
684 bad:
685 Safefree(xbuf);
686 *xlen = 0;
687 return NULL;
688}
689
690#endif /* USE_LOCALE_COLLATE */
691
8ef6e574
KW
692#ifdef USE_LOCALE
693
637917bb 694STATIC bool
7d74bb61
KW
695S_is_cur_LC_category_utf8(pTHX_ int category)
696{
697 /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
698 * otherwise. 'category' may not be LC_ALL. If the platform doesn't have
119ee68b
KW
699 * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence
700 * could give the wrong result. It errs on the side of not being a UTF-8
701 * locale. */
7d74bb61
KW
702
703 char *save_input_locale = NULL;
7d74bb61
KW
704 STRLEN final_pos;
705
8ef6e574 706#ifdef LC_ALL
7d74bb61 707 assert(category != LC_ALL);
8ef6e574 708#endif
7d74bb61
KW
709
710 /* First dispose of the trivial cases */
b07fffd1 711 save_input_locale = setlocale(category, NULL);
7d74bb61 712 if (! save_input_locale) {
69014004
KW
713 DEBUG_L(PerlIO_printf(Perl_debug_log,
714 "Could not find current locale for category %d\n",
715 category));
7d74bb61
KW
716 return FALSE; /* XXX maybe should croak */
717 }
b07fffd1 718 save_input_locale = stdize_locale(savepv(save_input_locale));
7d74bb61
KW
719 if ((*save_input_locale == 'C' && save_input_locale[1] == '\0')
720 || strEQ(save_input_locale, "POSIX"))
721 {
69014004
KW
722 DEBUG_L(PerlIO_printf(Perl_debug_log,
723 "Current locale for category %d is %s\n",
724 category, save_input_locale));
b07fffd1 725 Safefree(save_input_locale);
7d74bb61
KW
726 return FALSE;
727 }
728
1d958db2
KW
729#if defined(USE_LOCALE_CTYPE) \
730 && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET)))
7d74bb61 731
1d958db2 732 { /* Next try nl_langinfo or MB_CUR_MAX if available */
7d74bb61
KW
733
734 char *save_ctype_locale = NULL;
119ee68b 735 bool is_utf8;
7d74bb61 736
119ee68b 737 if (category != LC_CTYPE) { /* These work only on LC_CTYPE */
7d74bb61
KW
738
739 /* Get the current LC_CTYPE locale */
740 save_ctype_locale = stdize_locale(savepv(setlocale(LC_CTYPE, NULL)));
741 if (! save_ctype_locale) {
69014004
KW
742 DEBUG_L(PerlIO_printf(Perl_debug_log,
743 "Could not find current locale for LC_CTYPE\n"));
7d74bb61
KW
744 goto cant_use_nllanginfo;
745 }
746
747 /* If LC_CTYPE and the desired category use the same locale, this
748 * means that finding the value for LC_CTYPE is the same as finding
749 * the value for the desired category. Otherwise, switch LC_CTYPE
750 * to the desired category's locale */
751 if (strEQ(save_ctype_locale, save_input_locale)) {
752 Safefree(save_ctype_locale);
753 save_ctype_locale = NULL;
754 }
755 else if (! setlocale(LC_CTYPE, save_input_locale)) {
69014004
KW
756 DEBUG_L(PerlIO_printf(Perl_debug_log,
757 "Could not change LC_CTYPE locale to %s\n",
758 save_input_locale));
7d74bb61
KW
759 Safefree(save_ctype_locale);
760 goto cant_use_nllanginfo;
761 }
762 }
763
69014004
KW
764 DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n",
765 save_input_locale));
766
7d74bb61 767 /* Here the current LC_CTYPE is set to the locale of the category whose
1d958db2
KW
768 * information is desired. This means that nl_langinfo() and MB_CUR_MAX
769 * should give the correct results */
119ee68b 770
1d958db2
KW
771# if defined(HAS_NL_LANGINFO) && defined(CODESET)
772 {
773 char *codeset = savepv(nl_langinfo(CODESET));
774 if (codeset && strNE(codeset, "")) {
119ee68b 775
1d958db2
KW
776 /* If we switched LC_CTYPE, switch back */
777 if (save_ctype_locale) {
778 setlocale(LC_CTYPE, save_ctype_locale);
779 Safefree(save_ctype_locale);
780 }
781
782 is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8"))
783 || foldEQ(codeset, STR_WITH_LEN("UTF8"));
784
69014004
KW
785 DEBUG_L(PerlIO_printf(Perl_debug_log,
786 "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n",
787 codeset, is_utf8));
1d958db2
KW
788 Safefree(codeset);
789 Safefree(save_input_locale);
790 return is_utf8;
791 }
119ee68b
KW
792 }
793
1d958db2
KW
794# endif
795# ifdef MB_CUR_MAX
796
797 /* Here, either we don't have nl_langinfo, or it didn't return a
798 * codeset. Try MB_CUR_MAX */
799
119ee68b
KW
800 /* Standard UTF-8 needs at least 4 bytes to represent the maximum
801 * Unicode code point. Since UTF-8 is the only non-single byte
802 * encoding we handle, we just say any such encoding is UTF-8, and if
803 * turns out to be wrong, other things will fail */
804 is_utf8 = MB_CUR_MAX >= 4;
805
69014004
KW
806 DEBUG_L(PerlIO_printf(Perl_debug_log,
807 "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n",
808 (int) MB_CUR_MAX, is_utf8));
809
119ee68b
KW
810 Safefree(save_input_locale);
811
812# ifdef HAS_MBTOWC
813
814 /* ... But, most system that have MB_CUR_MAX will also have mbtowc(),
815 * since they are both in the C99 standard. We can feed a known byte
816 * string to the latter function, and check that it gives the expected
817 * result */
818 if (is_utf8) {
819 wchar_t wc;
a6715020 820 GCC_DIAG_IGNORE(-Wunused-result);
1d958db2 821 (void) mbtowc(&wc, NULL, 0); /* Reset any shift state */
a6715020 822 GCC_DIAG_RESTORE;
69014004 823 errno = 0;
119ee68b
KW
824 if (mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
825 != strlen(HYPHEN_UTF8)
826 || wc != (wchar_t) 0x2010)
827 {
828 is_utf8 = FALSE;
69014004
KW
829 DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", wc));
830 DEBUG_L(PerlIO_printf(Perl_debug_log,
831 "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n",
832 mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno));
119ee68b
KW
833 }
834 }
119ee68b
KW
835# endif
836
1d958db2
KW
837 /* If we switched LC_CTYPE, switch back */
838 if (save_ctype_locale) {
839 setlocale(LC_CTYPE, save_ctype_locale);
840 Safefree(save_ctype_locale);
119ee68b 841 }
7d74bb61 842
1d958db2 843 return is_utf8;
119ee68b 844# endif
7d74bb61 845 }
119ee68b 846
7d74bb61
KW
847 cant_use_nllanginfo:
848
849#endif /* HAS_NL_LANGINFO etc */
850
851 /* nl_langinfo not available or failed somehow. Look at the locale name to
751e9426 852 * see if it matches qr/UTF -? 8 /ix */
7d74bb61
KW
853
854 final_pos = strlen(save_input_locale) - 1;
751e9426
KW
855 if (final_pos >= 3) {
856 char *name = save_input_locale;
857
858 /* Find next 'U' or 'u' and look from there */
859 while ((name += strcspn(name, "Uu") + 1)
860 <= save_input_locale + final_pos - 2)
7d74bb61 861 {
751e9426
KW
862 if (toFOLD(*(name)) != 't'
863 || toFOLD(*(name + 1)) != 'f')
864 {
865 continue;
866 }
867 name += 2;
868 if (*(name) == '-') {
869 if ((name > save_input_locale + final_pos - 1)) {
870 break;
871 }
872 name++;
873 }
874 if (*(name) == '8') {
875 Safefree(save_input_locale);
69014004
KW
876 DEBUG_L(PerlIO_printf(Perl_debug_log,
877 "Locale %s ends with UTF-8 in name\n",
878 save_input_locale));
751e9426
KW
879 return TRUE;
880 }
7d74bb61 881 }
69014004
KW
882 DEBUG_L(PerlIO_printf(Perl_debug_log,
883 "Locale %s doesn't end with UTF-8 in name\n",
884 save_input_locale));
7d74bb61
KW
885 }
886
887#ifdef WIN32
888 /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
889 if (final_pos >= 4
890 && *(save_input_locale + final_pos - 0) == '1'
891 && *(save_input_locale + final_pos - 1) == '0'
892 && *(save_input_locale + final_pos - 2) == '0'
893 && *(save_input_locale + final_pos - 3) == '5'
894 && *(save_input_locale + final_pos - 4) == '6')
895 {
896 Safefree(save_input_locale);
69014004
KW
897 DEBUG_L(PerlIO_printf(Perl_debug_log,
898 "Locale %s ends with 10056 in name, is UTF-8 locale\n",
899 save_input_locale));
7d74bb61
KW
900 return TRUE;
901 }
902#endif
903
fa9b773e
KW
904 /* Other common encodings are the ISO 8859 series, which aren't UTF-8 */
905 if (instr(save_input_locale, "8859")) {
906 Safefree(save_input_locale);
69014004
KW
907 DEBUG_L(PerlIO_printf(Perl_debug_log,
908 "Locale %s has 8859 in name, not UTF-8 locale\n",
909 save_input_locale));
fa9b773e
KW
910 return FALSE;
911 }
912
913#ifdef HAS_LOCALECONV
914
915# ifdef USE_LOCALE_MONETARY
916
917 /* Here, there is nothing in the locale name to indicate whether the locale
918 * is UTF-8 or not. This "name", the return of setlocale(), is actually
919 * defined to be opaque, so we can't really rely on the absence of various
920 * substrings in the name to indicate its UTF-8ness. Look at the locale's
921 * currency symbol. Often that will be in the native script, and if the
922 * symbol isn't in UTF-8, we know that the locale isn't. If it is
923 * non-ASCII UTF-8, we infer that the locale is too.
924 * To do this, like above for LC_CTYPE, we first set LC_MONETARY to the
925 * locale of the desired category, if it isn't that locale already */
926
927 {
928 char *save_monetary_locale = NULL;
929 bool illegal_utf8 = FALSE;
930 bool only_ascii = FALSE;
931 const struct lconv* const lc = localeconv();
932
933 if (category != LC_MONETARY) {
934
935 save_monetary_locale = stdize_locale(savepv(setlocale(LC_MONETARY,
936 NULL)));
937 if (! save_monetary_locale) {
69014004
KW
938 DEBUG_L(PerlIO_printf(Perl_debug_log,
939 "Could not find current locale for LC_MONETARY\n"));
fa9b773e
KW
940 goto cant_use_monetary;
941 }
942
943 if (strNE(save_monetary_locale, save_input_locale)) {
944 if (! setlocale(LC_MONETARY, save_input_locale)) {
69014004
KW
945 DEBUG_L(PerlIO_printf(Perl_debug_log,
946 "Could not change LC_MONETARY locale to %s\n",
947 save_input_locale));
fa9b773e
KW
948 Safefree(save_monetary_locale);
949 goto cant_use_monetary;
950 }
951 }
952 }
953
954 /* Here the current LC_MONETARY is set to the locale of the category
955 * whose information is desired. */
956
957 if (lc && lc->currency_symbol) {
958 if (! is_utf8_string((U8 *) lc->currency_symbol, 0)) {
69014004
KW
959 DEBUG_L(PerlIO_printf(Perl_debug_log,
960 "Currency symbol for %s is not legal UTF-8\n",
961 save_input_locale));
fa9b773e
KW
962 illegal_utf8 = TRUE;
963 }
964 else if (is_ascii_string((U8 *) lc->currency_symbol, 0)) {
69014004 965 DEBUG_L(PerlIO_printf(Perl_debug_log, "Currency symbol for %s contains only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale));
fa9b773e
KW
966 only_ascii = TRUE;
967 }
968 }
969
970 /* If we changed it, restore LC_MONETARY to its original locale */
971 if (save_monetary_locale) {
972 setlocale(LC_MONETARY, save_monetary_locale);
973 Safefree(save_monetary_locale);
974 }
975
976 Safefree(save_input_locale);
977
978 /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; otherwise
979 * assume the locale is UTF-8 if and only if the symbol is non-ascii
980 * UTF-8. (We can't really tell if the locale is UTF-8 or not if the
981 * symbol is just a '$', so we err on the side of it not being UTF-8)
982 * */
69014004
KW
983 DEBUG_L(PerlIO_printf(Perl_debug_log, "\tis_utf8=%d\n", (illegal_utf8)
984 ? FALSE
985 : ! only_ascii));
fa9b773e
KW
986 return (illegal_utf8)
987 ? FALSE
988 : ! only_ascii;
989
990 }
991 cant_use_monetary:
992
993# endif /* USE_LOCALE_MONETARY */
994#endif /* HAS_LOCALECONV */
995
996#if 0 && defined(HAS_STRERROR) && defined(USE_LOCALE_MESSAGES)
997
998/* This code is ifdefd out because it was found to not be necessary in testing
999 * on our dromedary test machine, which has over 700 locales. There, looking
1000 * at just the currency symbol gave essentially the same results as doing this
1001 * extra work. Executing this also caused segfaults in miniperl. I left it in
1002 * so as to avoid rewriting it if real-world experience indicates that
1003 * dromedary is an outlier. Essentially, instead of returning abpve if we
1004 * haven't found illegal utf8, we continue on and examine all the strerror()
1005 * messages on the platform for utf8ness. If all are ASCII, we still don't
1006 * know the answer; but otherwise we have a pretty good indication of the
1007 * utf8ness. The reason this doesn't necessarily help much is that the
1008 * messages may not have been translated into the locale. The currency symbol
1009 * is much more likely to have been translated. The code below would need to
1010 * be altered somewhat to just be a continuation of testing the currency
1011 * symbol. */
1012 int e;
1013 unsigned int failures = 0, non_ascii = 0;
1014 char *save_messages_locale = NULL;
1015
1016 /* Like above for LC_CTYPE, we set LC_MESSAGES to the locale of the
1017 * desired category, if it isn't that locale already */
1018
1019 if (category != LC_MESSAGES) {
1020
1021 save_messages_locale = stdize_locale(savepv(setlocale(LC_MESSAGES,
1022 NULL)));
1023 if (! save_messages_locale) {
1024 goto cant_use_messages;
1025 }
1026
1027 if (strEQ(save_messages_locale, save_input_locale)) {
1028 Safefree(save_input_locale);
1029 }
1030 else if (! setlocale(LC_MESSAGES, save_input_locale)) {
1031 Safefree(save_messages_locale);
1032 goto cant_use_messages;
1033 }
1034 }
1035
1036 /* Here the current LC_MESSAGES is set to the locale of the category
1037 * whose information is desired. Look through all the messages */
1038
1039 for (e = 0;
1040#ifdef HAS_SYS_ERRLIST
1041 e <= sys_nerr
1042#endif
1043 ; e++)
1044 {
1045 const U8* const errmsg = (U8 *) Strerror(e) ;
1046 if (!errmsg)
1047 break;
1048 if (! is_utf8_string(errmsg, 0)) {
1049 failures++;
1050 break;
1051 }
1052 else if (! is_ascii_string(errmsg, 0)) {
1053 non_ascii++;
1054 }
1055 }
1056
1057 /* And, if we changed it, restore LC_MESSAGES to its original locale */
1058 if (save_messages_locale) {
1059 setlocale(LC_MESSAGES, save_messages_locale);
1060 Safefree(save_messages_locale);
1061 }
1062
1063 /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid,
1064 * any non-ascii means it is one; otherwise we assume it isn't */
1065 return (failures) ? FALSE : non_ascii;
1066
1067 }
1068 cant_use_messages:
1069
1070#endif
1071
69014004
KW
1072 DEBUG_L(PerlIO_printf(Perl_debug_log,
1073 "Assuming locale %s is not a UTF-8 locale\n",
1074 save_input_locale));
fa9b773e 1075 Safefree(save_input_locale);
7d74bb61
KW
1076 return FALSE;
1077}
1078
8ef6e574 1079#endif
7d74bb61 1080
66610fdd
RGS
1081/*
1082 * Local variables:
1083 * c-indentation-style: bsd
1084 * c-basic-offset: 4
14d04a33 1085 * indent-tabs-mode: nil
66610fdd
RGS
1086 * End:
1087 *
14d04a33 1088 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1089 */