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