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