This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
.locale.c: Better emulate POSIX locale setting on Windows
[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
b385bb4d
KW
334#ifdef WIN32
335
336char *
337Perl_my_setlocale(pTHX_ int category, const char* locale)
338{
339 /* This, for Windows, emulates POSIX setlocale() behavior. There is no
340 * difference unless the input locale is "", which means on Windows to get
341 * the machine default, which is set via the computer's "Regional and
342 * Language Options" (or its current equivalent). In POSIX, it instead
343 * means to find the locale from the user's environment. This routine
344 * looks in the environment, and, if anything is found, uses that instead
345 * of going to the machine default. If there is no environment override,
346 * the machine default is used, as normal, by calling the real setlocale()
347 * with "". The POSIX behavior is to use the LC_ALL variable if set;
348 * otherwise to use the particular category's variable if set; otherwise to
349 * use the LANG variable. */
350
89f7b9aa
KW
351 unsigned override_LANG = 0;
352 char * result;
353
b385bb4d
KW
354 if (locale && strEQ(locale, "")) {
355# ifdef LC_ALL
356 locale = PerlEnv_getenv("LC_ALL");
357 if (! locale) {
358#endif
359 switch (category) {
360# ifdef LC_ALL
361 case LC_ALL:
89f7b9aa 362 override_LANG++;
b385bb4d
KW
363 break; /* We already know its variable isn't set */
364# endif
365# ifdef USE_LOCALE_TIME
366 case LC_TIME:
367 locale = PerlEnv_getenv("LC_TIME");
368 break;
369# endif
370# ifdef USE_LOCALE_CTYPE
371 case LC_CTYPE:
372 locale = PerlEnv_getenv("LC_CTYPE");
373 break;
374# endif
375# ifdef USE_LOCALE_COLLATE
376 case LC_COLLATE:
377 locale = PerlEnv_getenv("LC_COLLATE");
378 break;
379# endif
380# ifdef USE_LOCALE_MONETARY
381 case LC_MONETARY:
382 locale = PerlEnv_getenv("LC_MONETARY");
383 break;
384# endif
385# ifdef USE_LOCALE_NUMERIC
386 case LC_NUMERIC:
387 locale = PerlEnv_getenv("LC_NUMERIC");
388 break;
389# endif
390# ifdef USE_LOCALE_MESSAGES
391 case LC_MESSAGES:
392 locale = PerlEnv_getenv("LC_MESSAGES");
393 break;
394# endif
395 default:
396 /* This is a category, like PAPER_SIZE that we don't
397 * know about; and so can't provide a wrapper. */
398 break;
399 }
400 if (! locale) {
401 locale = PerlEnv_getenv("LANG");
89f7b9aa
KW
402 if (locale) {
403 override_LANG++;
404 }
405 else {
b385bb4d
KW
406 locale = "";
407 }
408 }
409# ifdef LC_ALL
410 }
411# endif
412 }
413
89f7b9aa
KW
414 result = setlocale(category, locale);
415
416 if (override_LANG < 2) {
417 return result;
418 }
419
420 /* Here the input locale was LC_ALL, and we have set it to what is in the
421 * LANG variable. But LANG has lower priority than the other LC_foo
422 * variables, so override it for each one that is set. (If they are set to
423 * "", it means to use the same thing we just set LC_ALL to, so can skip)
424 * */
425# ifdef USE_LOCALE_TIME
426 result = PerlEnv_getenv("LC_TIME");
427 if (result and strNE(result, "")) {
428 setlocale(LC_TIME, result);
429 }
430# endif
431# ifdef USE_LOCALE_CTYPE
432 result = PerlEnv_getenv("LC_CTYPE");
433 if (result and strNE(result, "")) {
434 setlocale(LC_CTYPE, result);
435 }
436# endif
437# ifdef USE_LOCALE_COLLATE
438 result = PerlEnv_getenv("LC_COLLATE");
439 if (result and strNE(result, "")) {
440 setlocale(LC_COLLATE, result);
441 }
442# endif
443# ifdef USE_LOCALE_MONETARY
444 result = PerlEnv_getenv("LC_MONETARY");
445 if (result and strNE(result, "")) {
446 setlocale(LC_MONETARY, result);
447 }
448# endif
449# ifdef USE_LOCALE_NUMERIC
450 result = PerlEnv_getenv("LC_NUMERIC");
451 if (result and strNE(result, "")) {
452 setlocale(LC_NUMERIC, result);
453 }
454# endif
455# ifdef USE_LOCALE_MESSAGES
456 result = PerlEnv_getenv("LC_MESSAGES");
457 if (result and strNE(result, "")) {
458 setlocale(LC_MESSAGES, result);
459 }
460# endif
461
462 return setlocale(LC_ALL, NULL);
463
b385bb4d
KW
464}
465
466#endif
467
468
98994639
HS
469/*
470 * Initialize locale awareness.
471 */
472int
473Perl_init_i18nl10n(pTHX_ int printwarn)
474{
0e92a118
KW
475 /* printwarn is
476 *
477 * 0 if not to output warning when setup locale is bad
478 * 1 if to output warning based on value of PERL_BADLANG
479 * >1 if to output regardless of PERL_BADLANG
480 *
481 * returns
98994639 482 * 1 = set ok or not applicable,
0e92a118
KW
483 * 0 = fallback to a locale of lower priority
484 * -1 = fallback to all locales failed, not even to the C locale
98994639
HS
485 */
486
0e92a118
KW
487 int ok = 1;
488
98994639 489#if defined(USE_LOCALE)
97aff369 490 dVAR;
98994639
HS
491
492#ifdef USE_LOCALE_CTYPE
493 char *curctype = NULL;
494#endif /* USE_LOCALE_CTYPE */
495#ifdef USE_LOCALE_COLLATE
496 char *curcoll = NULL;
497#endif /* USE_LOCALE_COLLATE */
498#ifdef USE_LOCALE_NUMERIC
499 char *curnum = NULL;
500#endif /* USE_LOCALE_NUMERIC */
501#ifdef __GLIBC__
7452cf6a 502 char * const language = PerlEnv_getenv("LANGUAGE");
98994639 503#endif
65ebb059 504
ccd65d51
KW
505 /* NULL uses the existing already set up locale */
506 const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT"))
507 ? NULL
508 : "";
c3fcd832
KW
509 const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */
510 unsigned int trial_locales_count;
7452cf6a
AL
511 char * const lc_all = PerlEnv_getenv("LC_ALL");
512 char * const lang = PerlEnv_getenv("LANG");
98994639 513 bool setlocale_failure = FALSE;
65ebb059
KW
514 unsigned int i;
515 char *p;
516 const bool locwarn = (printwarn > 1 ||
517 (printwarn &&
518 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
0e92a118 519 bool done = FALSE;
65ebb059 520 const char *system_default_locale = NULL;
0e92a118 521
98994639 522
0e92a118
KW
523#ifndef LOCALE_ENVIRON_REQUIRED
524 PERL_UNUSED_VAR(done);
525#else
98994639
HS
526
527 /*
528 * Ultrix setlocale(..., "") fails if there are no environment
529 * variables from which to get a locale name.
530 */
531
b3e384bf 532# ifdef LC_ALL
98994639 533 if (lang) {
b385bb4d 534 if (my_setlocale(LC_ALL, setlocale_init))
98994639
HS
535 done = TRUE;
536 else
537 setlocale_failure = TRUE;
538 }
539 if (!setlocale_failure) {
b3e384bf 540# ifdef USE_LOCALE_CTYPE
56279a21 541 Safefree(curctype);
98994639 542 if (! (curctype =
b385bb4d 543 my_setlocale(LC_CTYPE,
98994639 544 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
ccd65d51 545 ? setlocale_init : NULL)))
98994639
HS
546 setlocale_failure = TRUE;
547 else
548 curctype = savepv(curctype);
b3e384bf
KW
549# endif /* USE_LOCALE_CTYPE */
550# ifdef USE_LOCALE_COLLATE
56279a21 551 Safefree(curcoll);
98994639 552 if (! (curcoll =
b385bb4d 553 my_setlocale(LC_COLLATE,
98994639 554 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
ccd65d51 555 ? setlocale_init : NULL)))
98994639
HS
556 setlocale_failure = TRUE;
557 else
558 curcoll = savepv(curcoll);
b3e384bf
KW
559# endif /* USE_LOCALE_COLLATE */
560# ifdef USE_LOCALE_NUMERIC
56279a21 561 Safefree(curnum);
98994639 562 if (! (curnum =
b385bb4d 563 my_setlocale(LC_NUMERIC,
98994639 564 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
ccd65d51 565 ? setlocale_init : NULL)))
98994639
HS
566 setlocale_failure = TRUE;
567 else
568 curnum = savepv(curnum);
b3e384bf 569# endif /* USE_LOCALE_NUMERIC */
a782673d
KW
570# ifdef USE_LOCALE_MESSAGES
571 if (! my_setlocale(LC_MESSAGES,
572 (!done && (lang || PerlEnv_getenv("LC_MESSAGES")))
573 ? setlocale_init : NULL))
574 {
575 setlocale_failure = TRUE;
576 }
577# endif /* USE_LOCALE_MESSAGES */
c835d6be
KW
578# ifdef USE_LOCALE_MONETARY
579 if (! my_setlocale(LC_MONETARY,
580 (!done && (lang || PerlEnv_getenv("LC_MONETARY")))
581 ? setlocale_init : NULL))
582 {
583 setlocale_failure = TRUE;
584 }
585# endif /* USE_LOCALE_MONETARY */
98994639
HS
586 }
587
b3e384bf 588# endif /* LC_ALL */
98994639
HS
589
590#endif /* !LOCALE_ENVIRON_REQUIRED */
591
65ebb059
KW
592 /* We try each locale in the list until we get one that works, or exhaust
593 * the list */
c3fcd832
KW
594 trial_locales[0] = setlocale_init;
595 trial_locales_count = 1;
65ebb059
KW
596 for (i= 0; i < trial_locales_count; i++) {
597 const char * trial_locale = trial_locales[i];
598
599 if (i > 0) {
600
601 /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED
602 * when i==0, but I (khw) don't think that behavior makes much
603 * sense */
604 setlocale_failure = FALSE;
605
606#ifdef WIN32
607
608 /* On Windows machines, an entry of "" after the 0th means to use
609 * the system default locale, which we now proceed to get. */
610 if (strEQ(trial_locale, "")) {
611 unsigned int j;
612
613 /* Note that this may change the locale, but we are going to do
614 * that anyway just below */
615 system_default_locale = setlocale(LC_ALL, "");
616
617 /* Skip if invalid or it's already on the list of locales to
618 * try */
619 if (! system_default_locale) {
620 goto next_iteration;
621 }
622 for (j = 0; j < trial_locales_count; j++) {
623 if (strEQ(system_default_locale, trial_locales[j])) {
624 goto next_iteration;
625 }
626 }
627
628 trial_locale = system_default_locale;
629 }
630#endif
631 }
632
98994639 633#ifdef LC_ALL
7cd8b568 634 if (! my_setlocale(LC_ALL, trial_locale)) {
49c85077 635 setlocale_failure = TRUE;
7cd8b568
KW
636 }
637 else {
638 /* Since LC_ALL succeeded, it should have changed all the other
639 * categories it can to its value; so we massage things so that the
640 * setlocales below just return their category's current values.
641 * This adequately handles the case in NetBSD where LC_COLLATE may
642 * not be defined for a locale, and setting it individually will
643 * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to
644 * the POSIX locale. */
645 trial_locale = NULL;
646 }
98994639
HS
647#endif /* LC_ALL */
648
49c85077 649 if (!setlocale_failure) {
98994639 650#ifdef USE_LOCALE_CTYPE
49c85077
KW
651 Safefree(curctype);
652 if (! (curctype = my_setlocale(LC_CTYPE, trial_locale)))
653 setlocale_failure = TRUE;
654 else
655 curctype = savepv(curctype);
98994639
HS
656#endif /* USE_LOCALE_CTYPE */
657#ifdef USE_LOCALE_COLLATE
49c85077
KW
658 Safefree(curcoll);
659 if (! (curcoll = my_setlocale(LC_COLLATE, trial_locale)))
660 setlocale_failure = TRUE;
661 else
662 curcoll = savepv(curcoll);
98994639
HS
663#endif /* USE_LOCALE_COLLATE */
664#ifdef USE_LOCALE_NUMERIC
49c85077
KW
665 Safefree(curnum);
666 if (! (curnum = my_setlocale(LC_NUMERIC, trial_locale)))
667 setlocale_failure = TRUE;
668 else
669 curnum = savepv(curnum);
98994639 670#endif /* USE_LOCALE_NUMERIC */
a782673d
KW
671#ifdef USE_LOCALE_MESSAGES
672 if (! (my_setlocale(LC_MESSAGES, trial_locale)))
673 setlocale_failure = TRUE;
674#endif /* USE_LOCALE_MESSAGES */
c835d6be
KW
675#ifdef USE_LOCALE_MONETARY
676 if (! (my_setlocale(LC_MONETARY, trial_locale)))
677 setlocale_failure = TRUE;
678#endif /* USE_LOCALE_MONETARY */
679
49c85077
KW
680 if (! setlocale_failure) { /* Success */
681 break;
682 }
65ebb059 683 }
98994639 684
49c85077
KW
685 /* Here, something failed; will need to try a fallback. */
686 ok = 0;
65ebb059 687
49c85077
KW
688 if (i == 0) {
689 unsigned int j;
98994639 690
65ebb059 691 if (locwarn) { /* Output failure info only on the first one */
98994639
HS
692#ifdef LC_ALL
693
49c85077
KW
694 PerlIO_printf(Perl_error_log,
695 "perl: warning: Setting locale failed.\n");
98994639
HS
696
697#else /* !LC_ALL */
698
49c85077
KW
699 PerlIO_printf(Perl_error_log,
700 "perl: warning: Setting locale failed for the categories:\n\t");
98994639 701#ifdef USE_LOCALE_CTYPE
49c85077
KW
702 if (! curctype)
703 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
98994639
HS
704#endif /* USE_LOCALE_CTYPE */
705#ifdef USE_LOCALE_COLLATE
49c85077
KW
706 if (! curcoll)
707 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
98994639
HS
708#endif /* USE_LOCALE_COLLATE */
709#ifdef USE_LOCALE_NUMERIC
49c85077
KW
710 if (! curnum)
711 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
98994639 712#endif /* USE_LOCALE_NUMERIC */
a782673d 713 PerlIO_printf(Perl_error_log, "and possibly others\n");
98994639
HS
714
715#endif /* LC_ALL */
716
49c85077
KW
717 PerlIO_printf(Perl_error_log,
718 "perl: warning: Please check that your locale settings:\n");
98994639
HS
719
720#ifdef __GLIBC__
49c85077
KW
721 PerlIO_printf(Perl_error_log,
722 "\tLANGUAGE = %c%s%c,\n",
723 language ? '"' : '(',
724 language ? language : "unset",
725 language ? '"' : ')');
98994639
HS
726#endif
727
49c85077
KW
728 PerlIO_printf(Perl_error_log,
729 "\tLC_ALL = %c%s%c,\n",
730 lc_all ? '"' : '(',
731 lc_all ? lc_all : "unset",
732 lc_all ? '"' : ')');
98994639
HS
733
734#if defined(USE_ENVIRON_ARRAY)
49c85077
KW
735 {
736 char **e;
737 for (e = environ; *e; e++) {
738 if (strnEQ(*e, "LC_", 3)
739 && strnNE(*e, "LC_ALL=", 7)
740 && (p = strchr(*e, '=')))
741 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
742 (int)(p - *e), *e, p + 1);
743 }
744 }
98994639 745#else
49c85077
KW
746 PerlIO_printf(Perl_error_log,
747 "\t(possibly more locale environment variables)\n");
98994639
HS
748#endif
749
49c85077
KW
750 PerlIO_printf(Perl_error_log,
751 "\tLANG = %c%s%c\n",
752 lang ? '"' : '(',
753 lang ? lang : "unset",
754 lang ? '"' : ')');
98994639 755
49c85077
KW
756 PerlIO_printf(Perl_error_log,
757 " are supported and installed on your system.\n");
758 }
98994639 759
65ebb059
KW
760 /* Calculate what fallback locales to try. We have avoided this
761 * until we have to, becuase failure is quite unlikely. This will
762 * usually change the upper bound of the loop we are in.
763 *
764 * Since the system's default way of setting the locale has not
765 * found one that works, We use Perl's defined ordering: LC_ALL,
766 * LANG, and the C locale. We don't try the same locale twice, so
767 * don't add to the list if already there. (On POSIX systems, the
768 * LC_ALL element will likely be a repeat of the 0th element "",
769 * but there's no harm done by doing it explicitly */
770 if (lc_all) {
771 for (j = 0; j < trial_locales_count; j++) {
772 if (strEQ(lc_all, trial_locales[j])) {
773 goto done_lc_all;
774 }
775 }
776 trial_locales[trial_locales_count++] = lc_all;
777 }
778 done_lc_all:
98994639 779
65ebb059
KW
780 if (lang) {
781 for (j = 0; j < trial_locales_count; j++) {
782 if (strEQ(lang, trial_locales[j])) {
783 goto done_lang;
784 }
785 }
786 trial_locales[trial_locales_count++] = lang;
787 }
788 done_lang:
789
790#if defined(WIN32) && defined(LC_ALL)
791 /* For Windows, we also try the system default locale before "C".
792 * (If there exists a Windows without LC_ALL we skip this because
793 * it gets too complicated. For those, the "C" is the next
794 * fallback possibility). The "" is the same as the 0th element of
795 * the array, but the code at the loop above knows to treat it
796 * differently when not the 0th */
797 trial_locales[trial_locales_count++] = "";
798#endif
799
800 for (j = 0; j < trial_locales_count; j++) {
801 if (strEQ("C", trial_locales[j])) {
802 goto done_C;
803 }
804 }
805 trial_locales[trial_locales_count++] = "C";
98994639 806
65ebb059
KW
807 done_C: ;
808 } /* end of first time through the loop */
98994639 809
65ebb059
KW
810#ifdef WIN32
811 next_iteration: ;
812#endif
813
814 } /* end of looping through the trial locales */
815
816 if (ok < 1) { /* If we tried to fallback */
817 const char* msg;
818 if (! setlocale_failure) { /* fallback succeeded */
819 msg = "Falling back to";
820 }
821 else { /* fallback failed */
98994639 822
65ebb059
KW
823 /* We dropped off the end of the loop, so have to decrement i to
824 * get back to the value the last time through */
825 i--;
98994639 826
65ebb059
KW
827 ok = -1;
828 msg = "Failed to fall back to";
829
830 /* To continue, we should use whatever values we've got */
98994639 831#ifdef USE_LOCALE_CTYPE
49c85077
KW
832 Safefree(curctype);
833 curctype = savepv(setlocale(LC_CTYPE, NULL));
98994639
HS
834#endif /* USE_LOCALE_CTYPE */
835#ifdef USE_LOCALE_COLLATE
49c85077
KW
836 Safefree(curcoll);
837 curcoll = savepv(setlocale(LC_COLLATE, NULL));
98994639
HS
838#endif /* USE_LOCALE_COLLATE */
839#ifdef USE_LOCALE_NUMERIC
49c85077
KW
840 Safefree(curnum);
841 curnum = savepv(setlocale(LC_NUMERIC, NULL));
98994639 842#endif /* USE_LOCALE_NUMERIC */
65ebb059
KW
843 }
844
845 if (locwarn) {
846 const char * description;
847 const char * name = "";
848 if (strEQ(trial_locales[i], "C")) {
849 description = "the standard locale";
850 name = "C";
851 }
852 else if (strEQ(trial_locales[i], "")) {
853 description = "the system default locale";
854 if (system_default_locale) {
855 name = system_default_locale;
856 }
857 }
858 else {
859 description = "a fallback locale";
860 name = trial_locales[i];
861 }
862 if (name && strNE(name, "")) {
863 PerlIO_printf(Perl_error_log,
864 "perl: warning: %s %s (\"%s\").\n", msg, description, name);
865 }
866 else {
867 PerlIO_printf(Perl_error_log,
868 "perl: warning: %s %s.\n", msg, description);
869 }
870 }
871 } /* End of tried to fallback */
98994639
HS
872
873#ifdef USE_LOCALE_CTYPE
874 new_ctype(curctype);
875#endif /* USE_LOCALE_CTYPE */
876
877#ifdef USE_LOCALE_COLLATE
878 new_collate(curcoll);
879#endif /* USE_LOCALE_COLLATE */
880
881#ifdef USE_LOCALE_NUMERIC
882 new_numeric(curnum);
883#endif /* USE_LOCALE_NUMERIC */
b310b053 884
8ef6e574 885#if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE)
49c85077
KW
886 /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE
887 * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by
888 * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the
889 * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open
890 * discipline. */
891 PL_utf8locale = is_cur_LC_category_utf8(LC_CTYPE);
892
a05d7ebb 893 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
fde18df1
JH
894 This is an alternative to using the -C command line switch
895 (the -C if present will override this). */
896 {
dd374669 897 const char *p = PerlEnv_getenv("PERL_UNICODE");
a05d7ebb 898 PL_unicode = p ? parse_unicode_opts(&p) : 0;
5a22a2bb
NC
899 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
900 PL_utf8cache = -1;
b310b053 901 }
ec71e770 902#endif
b310b053 903
98994639 904#ifdef USE_LOCALE_CTYPE
43c5f42d 905 Safefree(curctype);
98994639
HS
906#endif /* USE_LOCALE_CTYPE */
907#ifdef USE_LOCALE_COLLATE
43c5f42d 908 Safefree(curcoll);
98994639
HS
909#endif /* USE_LOCALE_COLLATE */
910#ifdef USE_LOCALE_NUMERIC
43c5f42d 911 Safefree(curnum);
98994639 912#endif /* USE_LOCALE_NUMERIC */
8ef6e574
KW
913
914#endif /* USE_LOCALE */
915
98994639
HS
916 return ok;
917}
918
49c85077 919
98994639
HS
920#ifdef USE_LOCALE_COLLATE
921
922/*
923 * mem_collxfrm() is a bit like strxfrm() but with two important
924 * differences. First, it handles embedded NULs. Second, it allocates
925 * a bit more memory than needed for the transformed data itself.
926 * The real transformed data begins at offset sizeof(collationix).
927 * Please see sv_collxfrm() to see how this is used.
928 */
166f8a29 929
98994639
HS
930char *
931Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
932{
97aff369 933 dVAR;
98994639
HS
934 char *xbuf;
935 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
936
7918f24d
NC
937 PERL_ARGS_ASSERT_MEM_COLLXFRM;
938
98994639
HS
939 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
940 /* the +1 is for the terminating NUL. */
941
942 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
a02a5408 943 Newx(xbuf, xAlloc, char);
98994639
HS
944 if (! xbuf)
945 goto bad;
946
947 *(U32*)xbuf = PL_collation_ix;
948 xout = sizeof(PL_collation_ix);
949 for (xin = 0; xin < len; ) {
224e8ef5 950 Size_t xused;
98994639
HS
951
952 for (;;) {
953 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
224e8ef5 954 if (xused >= PERL_INT_MAX)
98994639 955 goto bad;
eb160463 956 if ((STRLEN)xused < xAlloc - xout)
98994639
HS
957 break;
958 xAlloc = (2 * xAlloc) + 1;
959 Renew(xbuf, xAlloc, char);
960 if (! xbuf)
961 goto bad;
962 }
963
964 xin += strlen(s + xin) + 1;
965 xout += xused;
966
967 /* Embedded NULs are understood but silently skipped
968 * because they make no sense in locale collation. */
969 }
970
971 xbuf[xout] = '\0';
972 *xlen = xout - sizeof(PL_collation_ix);
973 return xbuf;
974
975 bad:
976 Safefree(xbuf);
977 *xlen = 0;
978 return NULL;
979}
980
981#endif /* USE_LOCALE_COLLATE */
982
8ef6e574
KW
983#ifdef USE_LOCALE
984
637917bb 985STATIC bool
7d74bb61
KW
986S_is_cur_LC_category_utf8(pTHX_ int category)
987{
988 /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
989 * otherwise. 'category' may not be LC_ALL. If the platform doesn't have
119ee68b
KW
990 * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence
991 * could give the wrong result. It errs on the side of not being a UTF-8
992 * locale. */
7d74bb61
KW
993
994 char *save_input_locale = NULL;
7d74bb61
KW
995 STRLEN final_pos;
996
8ef6e574 997#ifdef LC_ALL
7d74bb61 998 assert(category != LC_ALL);
8ef6e574 999#endif
7d74bb61
KW
1000
1001 /* First dispose of the trivial cases */
b07fffd1 1002 save_input_locale = setlocale(category, NULL);
7d74bb61 1003 if (! save_input_locale) {
69014004
KW
1004 DEBUG_L(PerlIO_printf(Perl_debug_log,
1005 "Could not find current locale for category %d\n",
1006 category));
7d74bb61
KW
1007 return FALSE; /* XXX maybe should croak */
1008 }
b07fffd1 1009 save_input_locale = stdize_locale(savepv(save_input_locale));
7d74bb61
KW
1010 if ((*save_input_locale == 'C' && save_input_locale[1] == '\0')
1011 || strEQ(save_input_locale, "POSIX"))
1012 {
69014004
KW
1013 DEBUG_L(PerlIO_printf(Perl_debug_log,
1014 "Current locale for category %d is %s\n",
1015 category, save_input_locale));
b07fffd1 1016 Safefree(save_input_locale);
7d74bb61
KW
1017 return FALSE;
1018 }
1019
1d958db2
KW
1020#if defined(USE_LOCALE_CTYPE) \
1021 && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET)))
7d74bb61 1022
1d958db2 1023 { /* Next try nl_langinfo or MB_CUR_MAX if available */
7d74bb61
KW
1024
1025 char *save_ctype_locale = NULL;
119ee68b 1026 bool is_utf8;
7d74bb61 1027
119ee68b 1028 if (category != LC_CTYPE) { /* These work only on LC_CTYPE */
7d74bb61
KW
1029
1030 /* Get the current LC_CTYPE locale */
1031 save_ctype_locale = stdize_locale(savepv(setlocale(LC_CTYPE, NULL)));
1032 if (! save_ctype_locale) {
69014004
KW
1033 DEBUG_L(PerlIO_printf(Perl_debug_log,
1034 "Could not find current locale for LC_CTYPE\n"));
7d74bb61
KW
1035 goto cant_use_nllanginfo;
1036 }
1037
1038 /* If LC_CTYPE and the desired category use the same locale, this
1039 * means that finding the value for LC_CTYPE is the same as finding
1040 * the value for the desired category. Otherwise, switch LC_CTYPE
1041 * to the desired category's locale */
1042 if (strEQ(save_ctype_locale, save_input_locale)) {
1043 Safefree(save_ctype_locale);
1044 save_ctype_locale = NULL;
1045 }
1046 else if (! setlocale(LC_CTYPE, save_input_locale)) {
69014004
KW
1047 DEBUG_L(PerlIO_printf(Perl_debug_log,
1048 "Could not change LC_CTYPE locale to %s\n",
1049 save_input_locale));
7d74bb61
KW
1050 Safefree(save_ctype_locale);
1051 goto cant_use_nllanginfo;
1052 }
1053 }
1054
69014004
KW
1055 DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n",
1056 save_input_locale));
1057
7d74bb61 1058 /* Here the current LC_CTYPE is set to the locale of the category whose
1d958db2
KW
1059 * information is desired. This means that nl_langinfo() and MB_CUR_MAX
1060 * should give the correct results */
119ee68b 1061
1d958db2
KW
1062# if defined(HAS_NL_LANGINFO) && defined(CODESET)
1063 {
1064 char *codeset = savepv(nl_langinfo(CODESET));
1065 if (codeset && strNE(codeset, "")) {
119ee68b 1066
1d958db2
KW
1067 /* If we switched LC_CTYPE, switch back */
1068 if (save_ctype_locale) {
1069 setlocale(LC_CTYPE, save_ctype_locale);
1070 Safefree(save_ctype_locale);
1071 }
1072
1073 is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8"))
1074 || foldEQ(codeset, STR_WITH_LEN("UTF8"));
1075
69014004
KW
1076 DEBUG_L(PerlIO_printf(Perl_debug_log,
1077 "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n",
1078 codeset, is_utf8));
1d958db2
KW
1079 Safefree(codeset);
1080 Safefree(save_input_locale);
1081 return is_utf8;
1082 }
119ee68b
KW
1083 }
1084
1d958db2
KW
1085# endif
1086# ifdef MB_CUR_MAX
1087
1088 /* Here, either we don't have nl_langinfo, or it didn't return a
1089 * codeset. Try MB_CUR_MAX */
1090
119ee68b
KW
1091 /* Standard UTF-8 needs at least 4 bytes to represent the maximum
1092 * Unicode code point. Since UTF-8 is the only non-single byte
1093 * encoding we handle, we just say any such encoding is UTF-8, and if
1094 * turns out to be wrong, other things will fail */
1095 is_utf8 = MB_CUR_MAX >= 4;
1096
69014004
KW
1097 DEBUG_L(PerlIO_printf(Perl_debug_log,
1098 "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n",
1099 (int) MB_CUR_MAX, is_utf8));
1100
119ee68b
KW
1101 Safefree(save_input_locale);
1102
1103# ifdef HAS_MBTOWC
1104
1105 /* ... But, most system that have MB_CUR_MAX will also have mbtowc(),
1106 * since they are both in the C99 standard. We can feed a known byte
1107 * string to the latter function, and check that it gives the expected
1108 * result */
1109 if (is_utf8) {
1110 wchar_t wc;
a6715020 1111 GCC_DIAG_IGNORE(-Wunused-result);
1d958db2 1112 (void) mbtowc(&wc, NULL, 0); /* Reset any shift state */
a6715020 1113 GCC_DIAG_RESTORE;
69014004 1114 errno = 0;
119ee68b
KW
1115 if (mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
1116 != strlen(HYPHEN_UTF8)
1117 || wc != (wchar_t) 0x2010)
1118 {
1119 is_utf8 = FALSE;
69014004
KW
1120 DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", wc));
1121 DEBUG_L(PerlIO_printf(Perl_debug_log,
1122 "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n",
1123 mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno));
119ee68b
KW
1124 }
1125 }
119ee68b
KW
1126# endif
1127
1d958db2
KW
1128 /* If we switched LC_CTYPE, switch back */
1129 if (save_ctype_locale) {
1130 setlocale(LC_CTYPE, save_ctype_locale);
1131 Safefree(save_ctype_locale);
119ee68b 1132 }
7d74bb61 1133
1d958db2 1134 return is_utf8;
119ee68b 1135# endif
7d74bb61 1136 }
119ee68b 1137
7d74bb61
KW
1138 cant_use_nllanginfo:
1139
1140#endif /* HAS_NL_LANGINFO etc */
1141
1142 /* nl_langinfo not available or failed somehow. Look at the locale name to
751e9426 1143 * see if it matches qr/UTF -? 8 /ix */
7d74bb61
KW
1144
1145 final_pos = strlen(save_input_locale) - 1;
751e9426
KW
1146 if (final_pos >= 3) {
1147 char *name = save_input_locale;
1148
1149 /* Find next 'U' or 'u' and look from there */
1150 while ((name += strcspn(name, "Uu") + 1)
1151 <= save_input_locale + final_pos - 2)
7d74bb61 1152 {
751e9426
KW
1153 if (toFOLD(*(name)) != 't'
1154 || toFOLD(*(name + 1)) != 'f')
1155 {
1156 continue;
1157 }
1158 name += 2;
1159 if (*(name) == '-') {
1160 if ((name > save_input_locale + final_pos - 1)) {
1161 break;
1162 }
1163 name++;
1164 }
1165 if (*(name) == '8') {
1166 Safefree(save_input_locale);
69014004
KW
1167 DEBUG_L(PerlIO_printf(Perl_debug_log,
1168 "Locale %s ends with UTF-8 in name\n",
1169 save_input_locale));
751e9426
KW
1170 return TRUE;
1171 }
7d74bb61 1172 }
69014004
KW
1173 DEBUG_L(PerlIO_printf(Perl_debug_log,
1174 "Locale %s doesn't end with UTF-8 in name\n",
1175 save_input_locale));
7d74bb61
KW
1176 }
1177
1178#ifdef WIN32
1179 /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
1180 if (final_pos >= 4
1181 && *(save_input_locale + final_pos - 0) == '1'
1182 && *(save_input_locale + final_pos - 1) == '0'
1183 && *(save_input_locale + final_pos - 2) == '0'
1184 && *(save_input_locale + final_pos - 3) == '5'
1185 && *(save_input_locale + final_pos - 4) == '6')
1186 {
1187 Safefree(save_input_locale);
69014004
KW
1188 DEBUG_L(PerlIO_printf(Perl_debug_log,
1189 "Locale %s ends with 10056 in name, is UTF-8 locale\n",
1190 save_input_locale));
7d74bb61
KW
1191 return TRUE;
1192 }
1193#endif
1194
fa9b773e
KW
1195 /* Other common encodings are the ISO 8859 series, which aren't UTF-8 */
1196 if (instr(save_input_locale, "8859")) {
1197 Safefree(save_input_locale);
69014004
KW
1198 DEBUG_L(PerlIO_printf(Perl_debug_log,
1199 "Locale %s has 8859 in name, not UTF-8 locale\n",
1200 save_input_locale));
fa9b773e
KW
1201 return FALSE;
1202 }
1203
1204#ifdef HAS_LOCALECONV
1205
1206# ifdef USE_LOCALE_MONETARY
1207
1208 /* Here, there is nothing in the locale name to indicate whether the locale
1209 * is UTF-8 or not. This "name", the return of setlocale(), is actually
1210 * defined to be opaque, so we can't really rely on the absence of various
1211 * substrings in the name to indicate its UTF-8ness. Look at the locale's
1212 * currency symbol. Often that will be in the native script, and if the
1213 * symbol isn't in UTF-8, we know that the locale isn't. If it is
1214 * non-ASCII UTF-8, we infer that the locale is too.
1215 * To do this, like above for LC_CTYPE, we first set LC_MONETARY to the
1216 * locale of the desired category, if it isn't that locale already */
1217
1218 {
1219 char *save_monetary_locale = NULL;
1220 bool illegal_utf8 = FALSE;
1221 bool only_ascii = FALSE;
1222 const struct lconv* const lc = localeconv();
1223
1224 if (category != LC_MONETARY) {
1225
1226 save_monetary_locale = stdize_locale(savepv(setlocale(LC_MONETARY,
1227 NULL)));
1228 if (! save_monetary_locale) {
69014004
KW
1229 DEBUG_L(PerlIO_printf(Perl_debug_log,
1230 "Could not find current locale for LC_MONETARY\n"));
fa9b773e
KW
1231 goto cant_use_monetary;
1232 }
1233
1234 if (strNE(save_monetary_locale, save_input_locale)) {
1235 if (! setlocale(LC_MONETARY, save_input_locale)) {
69014004
KW
1236 DEBUG_L(PerlIO_printf(Perl_debug_log,
1237 "Could not change LC_MONETARY locale to %s\n",
1238 save_input_locale));
fa9b773e
KW
1239 Safefree(save_monetary_locale);
1240 goto cant_use_monetary;
1241 }
1242 }
1243 }
1244
1245 /* Here the current LC_MONETARY is set to the locale of the category
1246 * whose information is desired. */
1247
1248 if (lc && lc->currency_symbol) {
1249 if (! is_utf8_string((U8 *) lc->currency_symbol, 0)) {
69014004
KW
1250 DEBUG_L(PerlIO_printf(Perl_debug_log,
1251 "Currency symbol for %s is not legal UTF-8\n",
1252 save_input_locale));
fa9b773e
KW
1253 illegal_utf8 = TRUE;
1254 }
1255 else if (is_ascii_string((U8 *) lc->currency_symbol, 0)) {
69014004 1256 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
1257 only_ascii = TRUE;
1258 }
1259 }
1260
1261 /* If we changed it, restore LC_MONETARY to its original locale */
1262 if (save_monetary_locale) {
1263 setlocale(LC_MONETARY, save_monetary_locale);
1264 Safefree(save_monetary_locale);
1265 }
1266
1267 Safefree(save_input_locale);
1268
1269 /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; otherwise
1270 * assume the locale is UTF-8 if and only if the symbol is non-ascii
1271 * UTF-8. (We can't really tell if the locale is UTF-8 or not if the
1272 * symbol is just a '$', so we err on the side of it not being UTF-8)
1273 * */
69014004
KW
1274 DEBUG_L(PerlIO_printf(Perl_debug_log, "\tis_utf8=%d\n", (illegal_utf8)
1275 ? FALSE
1276 : ! only_ascii));
fa9b773e
KW
1277 return (illegal_utf8)
1278 ? FALSE
1279 : ! only_ascii;
1280
1281 }
1282 cant_use_monetary:
1283
1284# endif /* USE_LOCALE_MONETARY */
1285#endif /* HAS_LOCALECONV */
1286
1287#if 0 && defined(HAS_STRERROR) && defined(USE_LOCALE_MESSAGES)
1288
1289/* This code is ifdefd out because it was found to not be necessary in testing
1290 * on our dromedary test machine, which has over 700 locales. There, looking
1291 * at just the currency symbol gave essentially the same results as doing this
1292 * extra work. Executing this also caused segfaults in miniperl. I left it in
1293 * so as to avoid rewriting it if real-world experience indicates that
1294 * dromedary is an outlier. Essentially, instead of returning abpve if we
1295 * haven't found illegal utf8, we continue on and examine all the strerror()
1296 * messages on the platform for utf8ness. If all are ASCII, we still don't
1297 * know the answer; but otherwise we have a pretty good indication of the
1298 * utf8ness. The reason this doesn't necessarily help much is that the
1299 * messages may not have been translated into the locale. The currency symbol
1300 * is much more likely to have been translated. The code below would need to
1301 * be altered somewhat to just be a continuation of testing the currency
1302 * symbol. */
1303 int e;
1304 unsigned int failures = 0, non_ascii = 0;
1305 char *save_messages_locale = NULL;
1306
1307 /* Like above for LC_CTYPE, we set LC_MESSAGES to the locale of the
1308 * desired category, if it isn't that locale already */
1309
1310 if (category != LC_MESSAGES) {
1311
1312 save_messages_locale = stdize_locale(savepv(setlocale(LC_MESSAGES,
1313 NULL)));
1314 if (! save_messages_locale) {
1315 goto cant_use_messages;
1316 }
1317
1318 if (strEQ(save_messages_locale, save_input_locale)) {
1319 Safefree(save_input_locale);
1320 }
1321 else if (! setlocale(LC_MESSAGES, save_input_locale)) {
1322 Safefree(save_messages_locale);
1323 goto cant_use_messages;
1324 }
1325 }
1326
1327 /* Here the current LC_MESSAGES is set to the locale of the category
1328 * whose information is desired. Look through all the messages */
1329
1330 for (e = 0;
1331#ifdef HAS_SYS_ERRLIST
1332 e <= sys_nerr
1333#endif
1334 ; e++)
1335 {
1336 const U8* const errmsg = (U8 *) Strerror(e) ;
1337 if (!errmsg)
1338 break;
1339 if (! is_utf8_string(errmsg, 0)) {
1340 failures++;
1341 break;
1342 }
1343 else if (! is_ascii_string(errmsg, 0)) {
1344 non_ascii++;
1345 }
1346 }
1347
1348 /* And, if we changed it, restore LC_MESSAGES to its original locale */
1349 if (save_messages_locale) {
1350 setlocale(LC_MESSAGES, save_messages_locale);
1351 Safefree(save_messages_locale);
1352 }
1353
1354 /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid,
1355 * any non-ascii means it is one; otherwise we assume it isn't */
1356 return (failures) ? FALSE : non_ascii;
1357
1358 }
1359 cant_use_messages:
1360
1361#endif
1362
69014004
KW
1363 DEBUG_L(PerlIO_printf(Perl_debug_log,
1364 "Assuming locale %s is not a UTF-8 locale\n",
1365 save_input_locale));
fa9b773e 1366 Safefree(save_input_locale);
7d74bb61
KW
1367 return FALSE;
1368}
1369
8ef6e574 1370#endif
7d74bb61 1371
66610fdd
RGS
1372/*
1373 * Local variables:
1374 * c-indentation-style: bsd
1375 * c-basic-offset: 4
14d04a33 1376 * indent-tabs-mode: nil
66610fdd
RGS
1377 * End:
1378 *
14d04a33 1379 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1380 */