This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add an "always update" parameter to regen_lib's open_new().
[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.
25 */
26
98994639
HS
27#include "EXTERN.h"
28#define PERL_IN_LOCALE_C
29#include "perl.h"
30
b310b053
JH
31#ifdef I_LANGINFO
32# include <langinfo.h>
33#endif
34
a4af207c
JH
35#include "reentr.h"
36
98994639
HS
37/*
38 * Standardize the locale name from a string returned by 'setlocale'.
39 *
0ef2a2b2 40 * The typical return value of setlocale() is either
98994639
HS
41 * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL
42 * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL
43 * (the space-separated values represent the various sublocales,
0ef2a2b2 44 * in some unspecified order). This is not handled by this function.
98994639
HS
45 *
46 * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n",
0ef2a2b2
KW
47 * which is harmful for further use of the string in setlocale(). This
48 * function removes the trailing new line and everything up through the '='
98994639
HS
49 *
50 */
51STATIC char *
52S_stdize_locale(pTHX_ char *locs)
53{
7452cf6a 54 const char * const s = strchr(locs, '=');
98994639
HS
55 bool okay = TRUE;
56
7918f24d
NC
57 PERL_ARGS_ASSERT_STDIZE_LOCALE;
58
8772537c
AL
59 if (s) {
60 const char * const t = strchr(s, '.');
98994639 61 okay = FALSE;
8772537c
AL
62 if (t) {
63 const char * const u = strchr(t, '\n');
64 if (u && (u[1] == 0)) {
65 const STRLEN len = u - s;
66 Move(s + 1, locs, len, char);
67 locs[len] = 0;
68 okay = TRUE;
98994639
HS
69 }
70 }
71 }
72
73 if (!okay)
74 Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs);
75
76 return locs;
77}
78
79void
80Perl_set_numeric_radix(pTHX)
81{
82#ifdef USE_LOCALE_NUMERIC
97aff369 83 dVAR;
98994639 84# ifdef HAS_LOCALECONV
7452cf6a 85 const struct lconv* const lc = localeconv();
98994639 86
98994639
HS
87 if (lc && lc->decimal_point) {
88 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
89 SvREFCNT_dec(PL_numeric_radix_sv);
a0714e2c 90 PL_numeric_radix_sv = NULL;
98994639
HS
91 }
92 else {
93 if (PL_numeric_radix_sv)
94 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
95 else
96 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
97 }
98 }
99 else
a0714e2c 100 PL_numeric_radix_sv = NULL;
98994639
HS
101# endif /* HAS_LOCALECONV */
102#endif /* USE_LOCALE_NUMERIC */
103}
104
105/*
106 * Set up for a new numeric locale.
107 */
108void
8772537c 109Perl_new_numeric(pTHX_ const char *newnum)
98994639
HS
110{
111#ifdef USE_LOCALE_NUMERIC
b03f34cf 112 char *save_newnum;
97aff369 113 dVAR;
98994639
HS
114
115 if (! newnum) {
43c5f42d
NC
116 Safefree(PL_numeric_name);
117 PL_numeric_name = NULL;
98994639
HS
118 PL_numeric_standard = TRUE;
119 PL_numeric_local = TRUE;
120 return;
121 }
122
b03f34cf
KW
123 save_newnum = stdize_locale(savepv(newnum));
124 if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) {
98994639 125 Safefree(PL_numeric_name);
b03f34cf
KW
126 PL_numeric_name = save_newnum;
127 PL_numeric_standard = ((*save_newnum == 'C' && save_newnum[1] == '\0')
128 || strEQ(save_newnum, "POSIX"));
98994639
HS
129 PL_numeric_local = TRUE;
130 set_numeric_radix();
131 }
b03f34cf
KW
132 else {
133 Safefree(save_newnum);
134 }
98994639
HS
135
136#endif /* USE_LOCALE_NUMERIC */
137}
138
139void
140Perl_set_numeric_standard(pTHX)
141{
142#ifdef USE_LOCALE_NUMERIC
97aff369 143 dVAR;
98994639
HS
144
145 if (! PL_numeric_standard) {
146 setlocale(LC_NUMERIC, "C");
147 PL_numeric_standard = TRUE;
148 PL_numeric_local = FALSE;
149 set_numeric_radix();
150 }
151
152#endif /* USE_LOCALE_NUMERIC */
153}
154
155void
156Perl_set_numeric_local(pTHX)
157{
158#ifdef USE_LOCALE_NUMERIC
97aff369 159 dVAR;
98994639
HS
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
8772537c 175Perl_new_ctype(pTHX_ const char *newctype)
98994639
HS
176{
177#ifdef USE_LOCALE_CTYPE
27da23d5 178 dVAR;
98994639
HS
179 int i;
180
7918f24d
NC
181 PERL_ARGS_ASSERT_NEW_CTYPE;
182
98994639
HS
183 for (i = 0; i < 256; i++) {
184 if (isUPPER_LC(i))
185 PL_fold_locale[i] = toLOWER_LC(i);
186 else if (isLOWER_LC(i))
187 PL_fold_locale[i] = toUPPER_LC(i);
188 else
189 PL_fold_locale[i] = i;
190 }
191
192#endif /* USE_LOCALE_CTYPE */
7918f24d 193 PERL_ARGS_ASSERT_NEW_CTYPE;
8772537c 194 PERL_UNUSED_ARG(newctype);
96a5add6 195 PERL_UNUSED_CONTEXT;
98994639
HS
196}
197
198/*
199 * Set up for a new collation locale.
200 */
201void
8772537c 202Perl_new_collate(pTHX_ const char *newcoll)
98994639
HS
203{
204#ifdef USE_LOCALE_COLLATE
97aff369 205 dVAR;
98994639
HS
206
207 if (! newcoll) {
208 if (PL_collation_name) {
209 ++PL_collation_ix;
210 Safefree(PL_collation_name);
211 PL_collation_name = NULL;
212 }
213 PL_collation_standard = TRUE;
214 PL_collxfrm_base = 0;
215 PL_collxfrm_mult = 2;
216 return;
217 }
218
219 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
220 ++PL_collation_ix;
221 Safefree(PL_collation_name);
222 PL_collation_name = stdize_locale(savepv(newcoll));
770526c1
NC
223 PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
224 || strEQ(newcoll, "POSIX"));
98994639
HS
225
226 {
227 /* 2: at most so many chars ('a', 'b'). */
228 /* 50: surely no system expands a char more. */
229#define XFRMBUFSIZE (2 * 50)
230 char xbuf[XFRMBUFSIZE];
8772537c
AL
231 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
232 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
233 const SSize_t mult = fb - fa;
e0601d3c 234 if (mult < 1 && !(fa == 0 && fb == 0))
ad49ad39
NC
235 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
236 (UV) fa, (UV) fb);
eb160463 237 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
98994639
HS
238 PL_collxfrm_mult = mult;
239 }
240 }
241
242#endif /* USE_LOCALE_COLLATE */
243}
244
245/*
246 * Initialize locale awareness.
247 */
248int
249Perl_init_i18nl10n(pTHX_ int printwarn)
250{
251 int ok = 1;
252 /* returns
253 * 1 = set ok or not applicable,
254 * 0 = fallback to C locale,
255 * -1 = fallback to C locale failed
256 */
257
258#if defined(USE_LOCALE)
97aff369 259 dVAR;
98994639
HS
260
261#ifdef USE_LOCALE_CTYPE
262 char *curctype = NULL;
263#endif /* USE_LOCALE_CTYPE */
264#ifdef USE_LOCALE_COLLATE
265 char *curcoll = NULL;
266#endif /* USE_LOCALE_COLLATE */
267#ifdef USE_LOCALE_NUMERIC
268 char *curnum = NULL;
269#endif /* USE_LOCALE_NUMERIC */
270#ifdef __GLIBC__
7452cf6a 271 char * const language = PerlEnv_getenv("LANGUAGE");
98994639 272#endif
7452cf6a
AL
273 char * const lc_all = PerlEnv_getenv("LC_ALL");
274 char * const lang = PerlEnv_getenv("LANG");
98994639
HS
275 bool setlocale_failure = FALSE;
276
277#ifdef LOCALE_ENVIRON_REQUIRED
278
279 /*
280 * Ultrix setlocale(..., "") fails if there are no environment
281 * variables from which to get a locale name.
282 */
283
284 bool done = FALSE;
285
b3e384bf 286# ifdef LC_ALL
98994639
HS
287 if (lang) {
288 if (setlocale(LC_ALL, ""))
289 done = TRUE;
290 else
291 setlocale_failure = TRUE;
292 }
293 if (!setlocale_failure) {
b3e384bf 294# ifdef USE_LOCALE_CTYPE
56279a21 295 Safefree(curctype);
98994639
HS
296 if (! (curctype =
297 setlocale(LC_CTYPE,
298 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
bd61b366 299 ? "" : NULL)))
98994639
HS
300 setlocale_failure = TRUE;
301 else
302 curctype = savepv(curctype);
b3e384bf
KW
303# endif /* USE_LOCALE_CTYPE */
304# ifdef USE_LOCALE_COLLATE
56279a21 305 Safefree(curcoll);
98994639
HS
306 if (! (curcoll =
307 setlocale(LC_COLLATE,
308 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
bd61b366 309 ? "" : NULL)))
98994639
HS
310 setlocale_failure = TRUE;
311 else
312 curcoll = savepv(curcoll);
b3e384bf
KW
313# endif /* USE_LOCALE_COLLATE */
314# ifdef USE_LOCALE_NUMERIC
56279a21 315 Safefree(curnum);
98994639
HS
316 if (! (curnum =
317 setlocale(LC_NUMERIC,
318 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
bd61b366 319 ? "" : NULL)))
98994639
HS
320 setlocale_failure = TRUE;
321 else
322 curnum = savepv(curnum);
b3e384bf 323# endif /* USE_LOCALE_NUMERIC */
98994639
HS
324 }
325
b3e384bf 326# endif /* LC_ALL */
98994639
HS
327
328#endif /* !LOCALE_ENVIRON_REQUIRED */
329
330#ifdef LC_ALL
331 if (! setlocale(LC_ALL, ""))
332 setlocale_failure = TRUE;
333#endif /* LC_ALL */
334
335 if (!setlocale_failure) {
336#ifdef USE_LOCALE_CTYPE
6cb43dbf 337 Safefree(curctype);
98994639
HS
338 if (! (curctype = setlocale(LC_CTYPE, "")))
339 setlocale_failure = TRUE;
340 else
341 curctype = savepv(curctype);
342#endif /* USE_LOCALE_CTYPE */
343#ifdef USE_LOCALE_COLLATE
6cb43dbf 344 Safefree(curcoll);
98994639
HS
345 if (! (curcoll = setlocale(LC_COLLATE, "")))
346 setlocale_failure = TRUE;
347 else
348 curcoll = savepv(curcoll);
349#endif /* USE_LOCALE_COLLATE */
350#ifdef USE_LOCALE_NUMERIC
6cb43dbf 351 Safefree(curnum);
98994639
HS
352 if (! (curnum = setlocale(LC_NUMERIC, "")))
353 setlocale_failure = TRUE;
354 else
355 curnum = savepv(curnum);
356#endif /* USE_LOCALE_NUMERIC */
357 }
358
359 if (setlocale_failure) {
360 char *p;
0bd48802 361 const bool locwarn = (printwarn > 1 ||
98994639
HS
362 (printwarn &&
363 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
364
365 if (locwarn) {
366#ifdef LC_ALL
367
368 PerlIO_printf(Perl_error_log,
369 "perl: warning: Setting locale failed.\n");
370
371#else /* !LC_ALL */
372
373 PerlIO_printf(Perl_error_log,
374 "perl: warning: Setting locale failed for the categories:\n\t");
375#ifdef USE_LOCALE_CTYPE
376 if (! curctype)
377 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
378#endif /* USE_LOCALE_CTYPE */
379#ifdef USE_LOCALE_COLLATE
380 if (! curcoll)
381 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
382#endif /* USE_LOCALE_COLLATE */
383#ifdef USE_LOCALE_NUMERIC
384 if (! curnum)
385 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
386#endif /* USE_LOCALE_NUMERIC */
387 PerlIO_printf(Perl_error_log, "\n");
388
389#endif /* LC_ALL */
390
391 PerlIO_printf(Perl_error_log,
392 "perl: warning: Please check that your locale settings:\n");
393
394#ifdef __GLIBC__
395 PerlIO_printf(Perl_error_log,
396 "\tLANGUAGE = %c%s%c,\n",
397 language ? '"' : '(',
398 language ? language : "unset",
399 language ? '"' : ')');
400#endif
401
402 PerlIO_printf(Perl_error_log,
403 "\tLC_ALL = %c%s%c,\n",
404 lc_all ? '"' : '(',
405 lc_all ? lc_all : "unset",
406 lc_all ? '"' : ')');
407
408#if defined(USE_ENVIRON_ARRAY)
409 {
410 char **e;
411 for (e = environ; *e; e++) {
412 if (strnEQ(*e, "LC_", 3)
413 && strnNE(*e, "LC_ALL=", 7)
414 && (p = strchr(*e, '=')))
415 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
416 (int)(p - *e), *e, p + 1);
417 }
418 }
419#else
420 PerlIO_printf(Perl_error_log,
421 "\t(possibly more locale environment variables)\n");
422#endif
423
424 PerlIO_printf(Perl_error_log,
425 "\tLANG = %c%s%c\n",
426 lang ? '"' : '(',
427 lang ? lang : "unset",
428 lang ? '"' : ')');
429
430 PerlIO_printf(Perl_error_log,
431 " are supported and installed on your system.\n");
432 }
433
434#ifdef LC_ALL
435
436 if (setlocale(LC_ALL, "C")) {
437 if (locwarn)
438 PerlIO_printf(Perl_error_log,
439 "perl: warning: Falling back to the standard locale (\"C\").\n");
440 ok = 0;
441 }
442 else {
443 if (locwarn)
444 PerlIO_printf(Perl_error_log,
445 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
446 ok = -1;
447 }
448
449#else /* ! LC_ALL */
450
451 if (0
452#ifdef USE_LOCALE_CTYPE
453 || !(curctype || setlocale(LC_CTYPE, "C"))
454#endif /* USE_LOCALE_CTYPE */
455#ifdef USE_LOCALE_COLLATE
456 || !(curcoll || setlocale(LC_COLLATE, "C"))
457#endif /* USE_LOCALE_COLLATE */
458#ifdef USE_LOCALE_NUMERIC
459 || !(curnum || setlocale(LC_NUMERIC, "C"))
460#endif /* USE_LOCALE_NUMERIC */
461 )
462 {
463 if (locwarn)
464 PerlIO_printf(Perl_error_log,
465 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
466 ok = -1;
467 }
468
469#endif /* ! LC_ALL */
470
471#ifdef USE_LOCALE_CTYPE
56279a21 472 Safefree(curctype);
bd61b366 473 curctype = savepv(setlocale(LC_CTYPE, NULL));
98994639
HS
474#endif /* USE_LOCALE_CTYPE */
475#ifdef USE_LOCALE_COLLATE
56279a21 476 Safefree(curcoll);
bd61b366 477 curcoll = savepv(setlocale(LC_COLLATE, NULL));
98994639
HS
478#endif /* USE_LOCALE_COLLATE */
479#ifdef USE_LOCALE_NUMERIC
56279a21 480 Safefree(curnum);
bd61b366 481 curnum = savepv(setlocale(LC_NUMERIC, NULL));
98994639
HS
482#endif /* USE_LOCALE_NUMERIC */
483 }
484 else {
485
486#ifdef USE_LOCALE_CTYPE
487 new_ctype(curctype);
488#endif /* USE_LOCALE_CTYPE */
489
490#ifdef USE_LOCALE_COLLATE
491 new_collate(curcoll);
492#endif /* USE_LOCALE_COLLATE */
493
494#ifdef USE_LOCALE_NUMERIC
495 new_numeric(curnum);
496#endif /* USE_LOCALE_NUMERIC */
b310b053 497
98994639
HS
498 }
499
500#endif /* USE_LOCALE */
501
ec71e770 502#ifdef USE_PERLIO
b310b053 503 {
fde18df1 504 /* Set PL_utf8locale to TRUE if using PerlIO _and_
7d74bb61 505 the current LC_CTYPE locale is UTF-8.
8aa8f774 506 If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
a05d7ebb 507 are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
fde18df1 508 on STDIN, STDOUT, STDERR, _and_ the default open discipline.
085a54d9 509 */
7d74bb61 510 PL_utf8locale = is_cur_LC_category_utf8(LC_CTYPE);
fde18df1 511 }
a05d7ebb 512 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
fde18df1
JH
513 This is an alternative to using the -C command line switch
514 (the -C if present will override this). */
515 {
dd374669 516 const char *p = PerlEnv_getenv("PERL_UNICODE");
a05d7ebb 517 PL_unicode = p ? parse_unicode_opts(&p) : 0;
5a22a2bb
NC
518 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
519 PL_utf8cache = -1;
b310b053 520 }
ec71e770 521#endif
b310b053 522
98994639 523#ifdef USE_LOCALE_CTYPE
43c5f42d 524 Safefree(curctype);
98994639
HS
525#endif /* USE_LOCALE_CTYPE */
526#ifdef USE_LOCALE_COLLATE
43c5f42d 527 Safefree(curcoll);
98994639
HS
528#endif /* USE_LOCALE_COLLATE */
529#ifdef USE_LOCALE_NUMERIC
43c5f42d 530 Safefree(curnum);
98994639
HS
531#endif /* USE_LOCALE_NUMERIC */
532 return ok;
533}
534
98994639
HS
535#ifdef USE_LOCALE_COLLATE
536
537/*
538 * mem_collxfrm() is a bit like strxfrm() but with two important
539 * differences. First, it handles embedded NULs. Second, it allocates
540 * a bit more memory than needed for the transformed data itself.
541 * The real transformed data begins at offset sizeof(collationix).
542 * Please see sv_collxfrm() to see how this is used.
543 */
166f8a29 544
98994639
HS
545char *
546Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
547{
97aff369 548 dVAR;
98994639
HS
549 char *xbuf;
550 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
551
7918f24d
NC
552 PERL_ARGS_ASSERT_MEM_COLLXFRM;
553
98994639
HS
554 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
555 /* the +1 is for the terminating NUL. */
556
557 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
a02a5408 558 Newx(xbuf, xAlloc, char);
98994639
HS
559 if (! xbuf)
560 goto bad;
561
562 *(U32*)xbuf = PL_collation_ix;
563 xout = sizeof(PL_collation_ix);
564 for (xin = 0; xin < len; ) {
224e8ef5 565 Size_t xused;
98994639
HS
566
567 for (;;) {
568 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
224e8ef5 569 if (xused >= PERL_INT_MAX)
98994639 570 goto bad;
eb160463 571 if ((STRLEN)xused < xAlloc - xout)
98994639
HS
572 break;
573 xAlloc = (2 * xAlloc) + 1;
574 Renew(xbuf, xAlloc, char);
575 if (! xbuf)
576 goto bad;
577 }
578
579 xin += strlen(s + xin) + 1;
580 xout += xused;
581
582 /* Embedded NULs are understood but silently skipped
583 * because they make no sense in locale collation. */
584 }
585
586 xbuf[xout] = '\0';
587 *xlen = xout - sizeof(PL_collation_ix);
588 return xbuf;
589
590 bad:
591 Safefree(xbuf);
592 *xlen = 0;
593 return NULL;
594}
595
596#endif /* USE_LOCALE_COLLATE */
597
7d74bb61
KW
598bool
599S_is_cur_LC_category_utf8(pTHX_ int category)
600{
601 /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
602 * otherwise. 'category' may not be LC_ALL. If the platform doesn't have
603 * nl_langinfo(), this employs a heuristic, which hence could give the
604 * wrong result. It errs on the side of not being a UTF-8 locale. */
605
606 char *save_input_locale = NULL;
607 int has_hyphen;
608 STRLEN final_pos;
609
610 assert(category != LC_ALL);
611
612 /* First dispose of the trivial cases */
613 save_input_locale = stdize_locale(setlocale(category, NULL));
614 if (! save_input_locale) {
615 return FALSE; /* XXX maybe should croak */
616 }
617 if ((*save_input_locale == 'C' && save_input_locale[1] == '\0')
618 || strEQ(save_input_locale, "POSIX"))
619 {
620 return FALSE;
621 }
622
623 save_input_locale = savepv(save_input_locale);
624
625#if defined(HAS_NL_LANGINFO) && defined(CODESET) && defined(USE_LOCALE_CTYPE)
626
627 { /* Next try nl_langinfo if available */
628
629 char *save_ctype_locale = NULL;
630 char *codeset = NULL;
631
632 if (category != LC_CTYPE) { /* nl_langinfo works only on LC_CTYPE */
633
634 /* Get the current LC_CTYPE locale */
635 save_ctype_locale = stdize_locale(savepv(setlocale(LC_CTYPE, NULL)));
636 if (! save_ctype_locale) {
637 goto cant_use_nllanginfo;
638 }
639
640 /* If LC_CTYPE and the desired category use the same locale, this
641 * means that finding the value for LC_CTYPE is the same as finding
642 * the value for the desired category. Otherwise, switch LC_CTYPE
643 * to the desired category's locale */
644 if (strEQ(save_ctype_locale, save_input_locale)) {
645 Safefree(save_ctype_locale);
646 save_ctype_locale = NULL;
647 }
648 else if (! setlocale(LC_CTYPE, save_input_locale)) {
649 Safefree(save_ctype_locale);
650 goto cant_use_nllanginfo;
651 }
652 }
653
654 /* Here the current LC_CTYPE is set to the locale of the category whose
655 * information is desired. This means that nl_langinfo() should give
656 * the correct results */
657 codeset = savepv(nl_langinfo(CODESET));
658 if (codeset) {
659 bool is_utf8;
660
661 /* If we switched LC_CTYPE, switch back */
662 if (save_ctype_locale) {
663 setlocale(LC_CTYPE, save_ctype_locale);
664 Safefree(save_ctype_locale);
665 }
666
667 is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8"))
668 || foldEQ(codeset, STR_WITH_LEN("UTF8"));
669
670 Safefree(codeset);
671 Safefree(save_input_locale);
672 return is_utf8;
673 }
674
675 }
676 cant_use_nllanginfo:
677
678#endif /* HAS_NL_LANGINFO etc */
679
680 /* nl_langinfo not available or failed somehow. Look at the locale name to
681 * see if it matches qr/UTF -? 8 $ /ix */
682
683 final_pos = strlen(save_input_locale) - 1;
684 if (final_pos >= 3
685 && *(save_input_locale + final_pos) == '8')
686 {
687 has_hyphen = *(save_input_locale + final_pos - 1 ) == '-';
688 if ((! has_hyphen || final_pos >= 4)
689 && toFOLD(*(save_input_locale + final_pos - has_hyphen - 1)) == 'f'
690 && toFOLD(*(save_input_locale + final_pos - has_hyphen - 2)) == 't'
691 && toFOLD(*(save_input_locale + final_pos - has_hyphen - 3)) == 'u')
692 {
693 Safefree(save_input_locale);
694 return TRUE;
695 }
696 }
697
698#ifdef WIN32
699 /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
700 if (final_pos >= 4
701 && *(save_input_locale + final_pos - 0) == '1'
702 && *(save_input_locale + final_pos - 1) == '0'
703 && *(save_input_locale + final_pos - 2) == '0'
704 && *(save_input_locale + final_pos - 3) == '5'
705 && *(save_input_locale + final_pos - 4) == '6')
706 {
707 Safefree(save_input_locale);
708 return TRUE;
709 }
710#endif
711
fa9b773e
KW
712 /* Other common encodings are the ISO 8859 series, which aren't UTF-8 */
713 if (instr(save_input_locale, "8859")) {
714 Safefree(save_input_locale);
715 return FALSE;
716 }
717
718#ifdef HAS_LOCALECONV
719
720# ifdef USE_LOCALE_MONETARY
721
722 /* Here, there is nothing in the locale name to indicate whether the locale
723 * is UTF-8 or not. This "name", the return of setlocale(), is actually
724 * defined to be opaque, so we can't really rely on the absence of various
725 * substrings in the name to indicate its UTF-8ness. Look at the locale's
726 * currency symbol. Often that will be in the native script, and if the
727 * symbol isn't in UTF-8, we know that the locale isn't. If it is
728 * non-ASCII UTF-8, we infer that the locale is too.
729 * To do this, like above for LC_CTYPE, we first set LC_MONETARY to the
730 * locale of the desired category, if it isn't that locale already */
731
732 {
733 char *save_monetary_locale = NULL;
734 bool illegal_utf8 = FALSE;
735 bool only_ascii = FALSE;
736 const struct lconv* const lc = localeconv();
737
738 if (category != LC_MONETARY) {
739
740 save_monetary_locale = stdize_locale(savepv(setlocale(LC_MONETARY,
741 NULL)));
742 if (! save_monetary_locale) {
743 goto cant_use_monetary;
744 }
745
746 if (strNE(save_monetary_locale, save_input_locale)) {
747 if (! setlocale(LC_MONETARY, save_input_locale)) {
748 Safefree(save_monetary_locale);
749 goto cant_use_monetary;
750 }
751 }
752 }
753
754 /* Here the current LC_MONETARY is set to the locale of the category
755 * whose information is desired. */
756
757 if (lc && lc->currency_symbol) {
758 if (! is_utf8_string((U8 *) lc->currency_symbol, 0)) {
759 illegal_utf8 = TRUE;
760 }
761 else if (is_ascii_string((U8 *) lc->currency_symbol, 0)) {
762 only_ascii = TRUE;
763 }
764 }
765
766 /* If we changed it, restore LC_MONETARY to its original locale */
767 if (save_monetary_locale) {
768 setlocale(LC_MONETARY, save_monetary_locale);
769 Safefree(save_monetary_locale);
770 }
771
772 Safefree(save_input_locale);
773
774 /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; otherwise
775 * assume the locale is UTF-8 if and only if the symbol is non-ascii
776 * UTF-8. (We can't really tell if the locale is UTF-8 or not if the
777 * symbol is just a '$', so we err on the side of it not being UTF-8)
778 * */
779 return (illegal_utf8)
780 ? FALSE
781 : ! only_ascii;
782
783 }
784 cant_use_monetary:
785
786# endif /* USE_LOCALE_MONETARY */
787#endif /* HAS_LOCALECONV */
788
789#if 0 && defined(HAS_STRERROR) && defined(USE_LOCALE_MESSAGES)
790
791/* This code is ifdefd out because it was found to not be necessary in testing
792 * on our dromedary test machine, which has over 700 locales. There, looking
793 * at just the currency symbol gave essentially the same results as doing this
794 * extra work. Executing this also caused segfaults in miniperl. I left it in
795 * so as to avoid rewriting it if real-world experience indicates that
796 * dromedary is an outlier. Essentially, instead of returning abpve if we
797 * haven't found illegal utf8, we continue on and examine all the strerror()
798 * messages on the platform for utf8ness. If all are ASCII, we still don't
799 * know the answer; but otherwise we have a pretty good indication of the
800 * utf8ness. The reason this doesn't necessarily help much is that the
801 * messages may not have been translated into the locale. The currency symbol
802 * is much more likely to have been translated. The code below would need to
803 * be altered somewhat to just be a continuation of testing the currency
804 * symbol. */
805 int e;
806 unsigned int failures = 0, non_ascii = 0;
807 char *save_messages_locale = NULL;
808
809 /* Like above for LC_CTYPE, we set LC_MESSAGES to the locale of the
810 * desired category, if it isn't that locale already */
811
812 if (category != LC_MESSAGES) {
813
814 save_messages_locale = stdize_locale(savepv(setlocale(LC_MESSAGES,
815 NULL)));
816 if (! save_messages_locale) {
817 goto cant_use_messages;
818 }
819
820 if (strEQ(save_messages_locale, save_input_locale)) {
821 Safefree(save_input_locale);
822 }
823 else if (! setlocale(LC_MESSAGES, save_input_locale)) {
824 Safefree(save_messages_locale);
825 goto cant_use_messages;
826 }
827 }
828
829 /* Here the current LC_MESSAGES is set to the locale of the category
830 * whose information is desired. Look through all the messages */
831
832 for (e = 0;
833#ifdef HAS_SYS_ERRLIST
834 e <= sys_nerr
835#endif
836 ; e++)
837 {
838 const U8* const errmsg = (U8 *) Strerror(e) ;
839 if (!errmsg)
840 break;
841 if (! is_utf8_string(errmsg, 0)) {
842 failures++;
843 break;
844 }
845 else if (! is_ascii_string(errmsg, 0)) {
846 non_ascii++;
847 }
848 }
849
850 /* And, if we changed it, restore LC_MESSAGES to its original locale */
851 if (save_messages_locale) {
852 setlocale(LC_MESSAGES, save_messages_locale);
853 Safefree(save_messages_locale);
854 }
855
856 /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid,
857 * any non-ascii means it is one; otherwise we assume it isn't */
858 return (failures) ? FALSE : non_ascii;
859
860 }
861 cant_use_messages:
862
863#endif
864
865 Safefree(save_input_locale);
7d74bb61
KW
866 return FALSE;
867}
868
869
870
66610fdd
RGS
871/*
872 * Local variables:
873 * c-indentation-style: bsd
874 * c-basic-offset: 4
14d04a33 875 * indent-tabs-mode: nil
66610fdd
RGS
876 * End:
877 *
14d04a33 878 * ex: set ts=8 sts=4 sw=4 et:
37442d52 879 */