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