This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.c: Prepare for rearrangement of code blocks
[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
96# ifdef HAS_LOCALECONV
7452cf6a 97 const struct lconv* const lc = localeconv();
98994639 98
98994639
HS
99 if (lc && lc->decimal_point) {
100 if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) {
101 SvREFCNT_dec(PL_numeric_radix_sv);
a0714e2c 102 PL_numeric_radix_sv = NULL;
98994639
HS
103 }
104 else {
105 if (PL_numeric_radix_sv)
106 sv_setpv(PL_numeric_radix_sv, lc->decimal_point);
107 else
108 PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
28acfe03
KW
109 if (! is_ascii_string((U8 *) lc->decimal_point, 0)
110 && is_utf8_string((U8 *) lc->decimal_point, 0)
c1284011 111 && _is_cur_LC_category_utf8(LC_NUMERIC))
28acfe03
KW
112 {
113 SvUTF8_on(PL_numeric_radix_sv);
114 }
98994639
HS
115 }
116 }
117 else
a0714e2c 118 PL_numeric_radix_sv = NULL;
69014004
KW
119
120 DEBUG_L(PerlIO_printf(Perl_debug_log, "Locale radix is %s\n",
121 (PL_numeric_radix_sv)
122 ? lc->decimal_point
123 : "NULL"));
124
98994639
HS
125# endif /* HAS_LOCALECONV */
126#endif /* USE_LOCALE_NUMERIC */
127}
128
a39edc4c
KW
129/* Is the C string input 'name' "C" or "POSIX"? If so, and 'name' is the
130 * return of setlocale(), then this is extremely likely to be the C or POSIX
131 * locale. However, the output of setlocale() is documented to be opaque, but
132 * the odds are extremely small that it would return these two strings for some
133 * other locale. Note that VMS in these two locales includes many non-ASCII
134 * characters as controls and punctuation (below are hex bytes):
135 * cntrl: 00-1F 7F 84-97 9B-9F
136 * punct: 21-2F 3A-40 5B-60 7B-7E A1-A3 A5 A7-AB B0-B3 B5-B7 B9-BD BF-CF D1-DD DF-EF F1-FD
137 * Oddly, none there are listed as alphas, though some represent alphabetics
138 * http://www.nntp.perl.org/group/perl.perl5.porters/2013/02/msg198753.html */
98b630b3
KW
139#define isNAME_C_OR_POSIX(name) ((name) != NULL \
140 && ((*(name) == 'C' && (*(name + 1)) == '\0') \
a39edc4c
KW
141 || strEQ((name), "POSIX")))
142
98994639 143void
8772537c 144Perl_new_numeric(pTHX_ const char *newnum)
98994639
HS
145{
146#ifdef USE_LOCALE_NUMERIC
0d071d52
KW
147
148 /* Called after all libc setlocale() calls affecting LC_NUMERIC, to tell
149 * core Perl this and that 'newnum' is the name of the new locale.
150 * It installs this locale as the current underlying default.
151 *
152 * The default locale and the C locale can be toggled between by use of the
153 * set_numeric_local() and set_numeric_standard() functions, which should
154 * probably not be called directly, but only via macros like
155 * SET_NUMERIC_STANDARD() in perl.h.
156 *
157 * The toggling is necessary mainly so that a non-dot radix decimal point
158 * character can be output, while allowing internal calculations to use a
159 * dot.
160 *
161 * This sets several interpreter-level variables:
162 * PL_numeric_name The default locale's name: a copy of 'newnum'
163 * PL_numeric_local A boolean indicating if the toggled state is such
7738054c
KW
164 * that the current locale is the program's underlying
165 * locale
166 * PL_numeric_standard An int indicating if the toggled state is such
167 * that the current locale is the C locale. If non-zero,
168 * it is in C; if > 1, it means it may not be toggled away
169 * from C.
0d071d52
KW
170 * Note that both of the last two variables can be true at the same time,
171 * if the underlying locale is C. (Toggling is a no-op under these
172 * circumstances.)
173 *
174 * Any code changing the locale (outside this file) should use
175 * POSIX::setlocale, which calls this function. Therefore this function
176 * should be called directly only from this file and from
177 * POSIX::setlocale() */
178
b03f34cf 179 char *save_newnum;
98994639
HS
180
181 if (! newnum) {
43c5f42d
NC
182 Safefree(PL_numeric_name);
183 PL_numeric_name = NULL;
98994639
HS
184 PL_numeric_standard = TRUE;
185 PL_numeric_local = TRUE;
186 return;
187 }
188
b03f34cf
KW
189 save_newnum = stdize_locale(savepv(newnum));
190 if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) {
98994639 191 Safefree(PL_numeric_name);
b03f34cf 192 PL_numeric_name = save_newnum;
b03f34cf 193 }
98994639 194
a39edc4c 195 PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum);
e19f01cb 196 PL_numeric_local = TRUE;
4c28b29c
KW
197
198 /* Keep LC_NUMERIC in the C locale. This is for XS modules, so they don't
199 * have to worry about the radix being a non-dot. (Core operations that
200 * need the underlying locale change to it temporarily). */
201 set_numeric_standard();
202
e19f01cb 203 set_numeric_radix();
6959d69d 204
f2ce9e1c
JH
205#else
206 PERL_UNUSED_ARG(newnum);
98994639
HS
207#endif /* USE_LOCALE_NUMERIC */
208}
209
210void
211Perl_set_numeric_standard(pTHX)
212{
213#ifdef USE_LOCALE_NUMERIC
28c1bf33
KW
214 /* Toggle the LC_NUMERIC locale to C. Most code should use the macros like
215 * SET_NUMERIC_STANDARD() in perl.h instead of calling this directly. The
216 * macro avoids calling this routine if toggling isn't necessary according
217 * to our records (which could be wrong if some XS code has changed the
218 * locale behind our back) */
0d071d52 219
a9b8c0d8
KW
220 setlocale(LC_NUMERIC, "C");
221 PL_numeric_standard = TRUE;
222 PL_numeric_local = isNAME_C_OR_POSIX(PL_numeric_name);
223 set_numeric_radix();
69014004
KW
224 DEBUG_L(PerlIO_printf(Perl_debug_log,
225 "Underlying LC_NUMERIC locale now is C\n"));
98994639
HS
226
227#endif /* USE_LOCALE_NUMERIC */
228}
229
230void
231Perl_set_numeric_local(pTHX)
232{
233#ifdef USE_LOCALE_NUMERIC
28c1bf33
KW
234 /* Toggle the LC_NUMERIC locale to the current underlying default. Most
235 * code should use the macros like SET_NUMERIC_LOCAL() in perl.h instead of
236 * calling this directly. The macro avoids calling this routine if
237 * toggling isn't necessary according to our records (which could be wrong
238 * if some XS code has changed the locale behind our back) */
a9b8c0d8
KW
239
240 setlocale(LC_NUMERIC, PL_numeric_name);
241 PL_numeric_standard = isNAME_C_OR_POSIX(PL_numeric_name);
242 PL_numeric_local = TRUE;
243 set_numeric_radix();
69014004
KW
244 DEBUG_L(PerlIO_printf(Perl_debug_log,
245 "Underlying LC_NUMERIC locale now is %s\n",
246 PL_numeric_name));
98994639
HS
247
248#endif /* USE_LOCALE_NUMERIC */
249}
250
251/*
252 * Set up for a new ctype locale.
253 */
254void
8772537c 255Perl_new_ctype(pTHX_ const char *newctype)
98994639
HS
256{
257#ifdef USE_LOCALE_CTYPE
0d071d52
KW
258
259 /* Called after all libc setlocale() calls affecting LC_CTYPE, to tell
260 * core Perl this and that 'newctype' is the name of the new locale.
261 *
262 * This function sets up the folding arrays for all 256 bytes, assuming
263 * that tofold() is tolc() since fold case is not a concept in POSIX,
264 *
265 * Any code changing the locale (outside this file) should use
266 * POSIX::setlocale, which calls this function. Therefore this function
267 * should be called directly only from this file and from
268 * POSIX::setlocale() */
269
27da23d5 270 dVAR;
68067e4e 271 UV i;
98994639 272
7918f24d
NC
273 PERL_ARGS_ASSERT_NEW_CTYPE;
274
c1284011 275 PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE);
31f05a37
KW
276
277 /* A UTF-8 locale gets standard rules. But note that code still has to
278 * handle this specially because of the three problematic code points */
279 if (PL_in_utf8_CTYPE_locale) {
280 Copy(PL_fold_latin1, PL_fold_locale, 256, U8);
281 }
282 else {
baa60164
KW
283 for (i = 0; i < 256; i++) {
284 if (isUPPER_LC((U8) i))
285 PL_fold_locale[i] = (U8) toLOWER_LC((U8) i);
286 else if (isLOWER_LC((U8) i))
287 PL_fold_locale[i] = (U8) toUPPER_LC((U8) i);
288 else
289 PL_fold_locale[i] = (U8) i;
290 }
31f05a37 291 }
98994639
HS
292
293#endif /* USE_LOCALE_CTYPE */
7918f24d 294 PERL_ARGS_ASSERT_NEW_CTYPE;
8772537c 295 PERL_UNUSED_ARG(newctype);
96a5add6 296 PERL_UNUSED_CONTEXT;
98994639
HS
297}
298
98994639 299void
8772537c 300Perl_new_collate(pTHX_ const char *newcoll)
98994639
HS
301{
302#ifdef USE_LOCALE_COLLATE
0d071d52
KW
303
304 /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell
305 * core Perl this and that 'newcoll' is the name of the new locale.
306 *
307 * Any code changing the locale (outside this file) should use
308 * POSIX::setlocale, which calls this function. Therefore this function
309 * should be called directly only from this file and from
310 * POSIX::setlocale() */
311
98994639
HS
312 if (! newcoll) {
313 if (PL_collation_name) {
314 ++PL_collation_ix;
315 Safefree(PL_collation_name);
316 PL_collation_name = NULL;
317 }
318 PL_collation_standard = TRUE;
319 PL_collxfrm_base = 0;
320 PL_collxfrm_mult = 2;
321 return;
322 }
323
324 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
325 ++PL_collation_ix;
326 Safefree(PL_collation_name);
327 PL_collation_name = stdize_locale(savepv(newcoll));
a39edc4c 328 PL_collation_standard = isNAME_C_OR_POSIX(newcoll);
98994639
HS
329
330 {
331 /* 2: at most so many chars ('a', 'b'). */
332 /* 50: surely no system expands a char more. */
333#define XFRMBUFSIZE (2 * 50)
334 char xbuf[XFRMBUFSIZE];
8772537c
AL
335 const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
336 const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
337 const SSize_t mult = fb - fa;
e0601d3c 338 if (mult < 1 && !(fa == 0 && fb == 0))
ad49ad39
NC
339 Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf,
340 (UV) fa, (UV) fb);
eb160463 341 PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0;
98994639
HS
342 PL_collxfrm_mult = mult;
343 }
344 }
345
f2ce9e1c
JH
346#else
347 PERL_UNUSED_ARG(newcoll);
98994639
HS
348#endif /* USE_LOCALE_COLLATE */
349}
350
b385bb4d
KW
351#ifdef WIN32
352
353char *
354Perl_my_setlocale(pTHX_ int category, const char* locale)
355{
356 /* This, for Windows, emulates POSIX setlocale() behavior. There is no
357 * difference unless the input locale is "", which means on Windows to get
358 * the machine default, which is set via the computer's "Regional and
359 * Language Options" (or its current equivalent). In POSIX, it instead
360 * means to find the locale from the user's environment. This routine
361 * looks in the environment, and, if anything is found, uses that instead
362 * of going to the machine default. If there is no environment override,
363 * the machine default is used, as normal, by calling the real setlocale()
364 * with "". The POSIX behavior is to use the LC_ALL variable if set;
365 * otherwise to use the particular category's variable if set; otherwise to
366 * use the LANG variable. */
367
481465ea 368 bool override_LC_ALL = 0;
89f7b9aa
KW
369 char * result;
370
b385bb4d
KW
371 if (locale && strEQ(locale, "")) {
372# ifdef LC_ALL
373 locale = PerlEnv_getenv("LC_ALL");
374 if (! locale) {
375#endif
376 switch (category) {
377# ifdef LC_ALL
378 case LC_ALL:
481465ea 379 override_LC_ALL = TRUE;
b385bb4d
KW
380 break; /* We already know its variable isn't set */
381# endif
382# ifdef USE_LOCALE_TIME
383 case LC_TIME:
384 locale = PerlEnv_getenv("LC_TIME");
385 break;
386# endif
387# ifdef USE_LOCALE_CTYPE
388 case LC_CTYPE:
389 locale = PerlEnv_getenv("LC_CTYPE");
390 break;
391# endif
392# ifdef USE_LOCALE_COLLATE
393 case LC_COLLATE:
394 locale = PerlEnv_getenv("LC_COLLATE");
395 break;
396# endif
397# ifdef USE_LOCALE_MONETARY
398 case LC_MONETARY:
399 locale = PerlEnv_getenv("LC_MONETARY");
400 break;
401# endif
402# ifdef USE_LOCALE_NUMERIC
403 case LC_NUMERIC:
404 locale = PerlEnv_getenv("LC_NUMERIC");
405 break;
406# endif
407# ifdef USE_LOCALE_MESSAGES
408 case LC_MESSAGES:
409 locale = PerlEnv_getenv("LC_MESSAGES");
410 break;
411# endif
412 default:
413 /* This is a category, like PAPER_SIZE that we don't
414 * know about; and so can't provide a wrapper. */
415 break;
416 }
417 if (! locale) {
418 locale = PerlEnv_getenv("LANG");
481465ea 419 if (! locale) {
b385bb4d
KW
420 locale = "";
421 }
422 }
423# ifdef LC_ALL
424 }
425# endif
426 }
427
89f7b9aa
KW
428 result = setlocale(category, locale);
429
481465ea 430 if (! override_LC_ALL) {
89f7b9aa
KW
431 return result;
432 }
433
434 /* Here the input locale was LC_ALL, and we have set it to what is in the
481465ea
KW
435 * LANG variable or the system default if there is no LANG. But these have
436 * lower priority than the other LC_foo variables, so override it for each
437 * one that is set. (If they are set to "", it means to use the same thing
438 * we just set LC_ALL to, so can skip) */
89f7b9aa
KW
439# ifdef USE_LOCALE_TIME
440 result = PerlEnv_getenv("LC_TIME");
730252b2 441 if (result && strNE(result, "")) {
89f7b9aa
KW
442 setlocale(LC_TIME, result);
443 }
444# endif
445# ifdef USE_LOCALE_CTYPE
446 result = PerlEnv_getenv("LC_CTYPE");
730252b2 447 if (result && strNE(result, "")) {
89f7b9aa
KW
448 setlocale(LC_CTYPE, result);
449 }
450# endif
451# ifdef USE_LOCALE_COLLATE
452 result = PerlEnv_getenv("LC_COLLATE");
730252b2 453 if (result && strNE(result, "")) {
89f7b9aa
KW
454 setlocale(LC_COLLATE, result);
455 }
456# endif
457# ifdef USE_LOCALE_MONETARY
458 result = PerlEnv_getenv("LC_MONETARY");
730252b2 459 if (result && strNE(result, "")) {
89f7b9aa
KW
460 setlocale(LC_MONETARY, result);
461 }
462# endif
463# ifdef USE_LOCALE_NUMERIC
464 result = PerlEnv_getenv("LC_NUMERIC");
730252b2 465 if (result && strNE(result, "")) {
89f7b9aa
KW
466 setlocale(LC_NUMERIC, result);
467 }
468# endif
469# ifdef USE_LOCALE_MESSAGES
470 result = PerlEnv_getenv("LC_MESSAGES");
730252b2 471 if (result && strNE(result, "")) {
89f7b9aa
KW
472 setlocale(LC_MESSAGES, result);
473 }
474# endif
475
476 return setlocale(LC_ALL, NULL);
477
b385bb4d
KW
478}
479
480#endif
481
482
98994639
HS
483/*
484 * Initialize locale awareness.
485 */
486int
487Perl_init_i18nl10n(pTHX_ int printwarn)
488{
0e92a118
KW
489 /* printwarn is
490 *
491 * 0 if not to output warning when setup locale is bad
492 * 1 if to output warning based on value of PERL_BADLANG
493 * >1 if to output regardless of PERL_BADLANG
494 *
495 * returns
98994639 496 * 1 = set ok or not applicable,
0e92a118
KW
497 * 0 = fallback to a locale of lower priority
498 * -1 = fallback to all locales failed, not even to the C locale
98994639
HS
499 */
500
0e92a118
KW
501 int ok = 1;
502
98994639 503#if defined(USE_LOCALE)
98994639
HS
504#ifdef USE_LOCALE_CTYPE
505 char *curctype = NULL;
506#endif /* USE_LOCALE_CTYPE */
507#ifdef USE_LOCALE_COLLATE
508 char *curcoll = NULL;
509#endif /* USE_LOCALE_COLLATE */
510#ifdef USE_LOCALE_NUMERIC
511 char *curnum = NULL;
512#endif /* USE_LOCALE_NUMERIC */
513#ifdef __GLIBC__
7452cf6a 514 char * const language = PerlEnv_getenv("LANGUAGE");
98994639 515#endif
65ebb059 516
ccd65d51
KW
517 /* NULL uses the existing already set up locale */
518 const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT"))
519 ? NULL
520 : "";
c3fcd832
KW
521 const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */
522 unsigned int trial_locales_count;
7452cf6a
AL
523 char * const lc_all = PerlEnv_getenv("LC_ALL");
524 char * const lang = PerlEnv_getenv("LANG");
98994639 525 bool setlocale_failure = FALSE;
65ebb059
KW
526 unsigned int i;
527 char *p;
528 const bool locwarn = (printwarn > 1 ||
529 (printwarn &&
530 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
0e92a118 531 bool done = FALSE;
6bce99ee
JH
532#ifdef WIN32
533 /* In some systems you can find out the system default locale
534 * and use that as the fallback locale. */
535# define SYSTEM_DEFAULT_LOCALE
536#endif
537#ifdef SYSTEM_DEFAULT_LOCALE
65ebb059 538 const char *system_default_locale = NULL;
6bce99ee 539#endif
98994639 540
0e92a118
KW
541#ifndef LOCALE_ENVIRON_REQUIRED
542 PERL_UNUSED_VAR(done);
543#else
98994639
HS
544
545 /*
546 * Ultrix setlocale(..., "") fails if there are no environment
547 * variables from which to get a locale name.
548 */
549
b3e384bf 550# ifdef LC_ALL
98994639 551 if (lang) {
b385bb4d 552 if (my_setlocale(LC_ALL, setlocale_init))
98994639
HS
553 done = TRUE;
554 else
555 setlocale_failure = TRUE;
556 }
557 if (!setlocale_failure) {
b3e384bf 558# ifdef USE_LOCALE_CTYPE
56279a21 559 Safefree(curctype);
98994639 560 if (! (curctype =
b385bb4d 561 my_setlocale(LC_CTYPE,
98994639 562 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
ccd65d51 563 ? setlocale_init : NULL)))
98994639
HS
564 setlocale_failure = TRUE;
565 else
566 curctype = savepv(curctype);
b3e384bf
KW
567# endif /* USE_LOCALE_CTYPE */
568# ifdef USE_LOCALE_COLLATE
56279a21 569 Safefree(curcoll);
98994639 570 if (! (curcoll =
b385bb4d 571 my_setlocale(LC_COLLATE,
98994639 572 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
ccd65d51 573 ? setlocale_init : NULL)))
98994639
HS
574 setlocale_failure = TRUE;
575 else
576 curcoll = savepv(curcoll);
b3e384bf
KW
577# endif /* USE_LOCALE_COLLATE */
578# ifdef USE_LOCALE_NUMERIC
56279a21 579 Safefree(curnum);
98994639 580 if (! (curnum =
b385bb4d 581 my_setlocale(LC_NUMERIC,
98994639 582 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
ccd65d51 583 ? setlocale_init : NULL)))
98994639
HS
584 setlocale_failure = TRUE;
585 else
586 curnum = savepv(curnum);
b3e384bf 587# endif /* USE_LOCALE_NUMERIC */
a782673d
KW
588# ifdef USE_LOCALE_MESSAGES
589 if (! my_setlocale(LC_MESSAGES,
590 (!done && (lang || PerlEnv_getenv("LC_MESSAGES")))
591 ? setlocale_init : NULL))
592 {
593 setlocale_failure = TRUE;
594 }
595# endif /* USE_LOCALE_MESSAGES */
c835d6be
KW
596# ifdef USE_LOCALE_MONETARY
597 if (! my_setlocale(LC_MONETARY,
598 (!done && (lang || PerlEnv_getenv("LC_MONETARY")))
599 ? setlocale_init : NULL))
600 {
601 setlocale_failure = TRUE;
602 }
603# endif /* USE_LOCALE_MONETARY */
98994639
HS
604 }
605
b3e384bf 606# endif /* LC_ALL */
98994639
HS
607
608#endif /* !LOCALE_ENVIRON_REQUIRED */
609
65ebb059
KW
610 /* We try each locale in the list until we get one that works, or exhaust
611 * the list */
c3fcd832
KW
612 trial_locales[0] = setlocale_init;
613 trial_locales_count = 1;
65ebb059
KW
614 for (i= 0; i < trial_locales_count; i++) {
615 const char * trial_locale = trial_locales[i];
616
617 if (i > 0) {
618
619 /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED
620 * when i==0, but I (khw) don't think that behavior makes much
621 * sense */
622 setlocale_failure = FALSE;
623
6bce99ee
JH
624#ifdef SYSTEM_DEFAULT_LOCALE
625# ifdef WIN32
65ebb059
KW
626 /* On Windows machines, an entry of "" after the 0th means to use
627 * the system default locale, which we now proceed to get. */
628 if (strEQ(trial_locale, "")) {
629 unsigned int j;
630
631 /* Note that this may change the locale, but we are going to do
632 * that anyway just below */
633 system_default_locale = setlocale(LC_ALL, "");
634
635 /* Skip if invalid or it's already on the list of locales to
636 * try */
637 if (! system_default_locale) {
638 goto next_iteration;
639 }
640 for (j = 0; j < trial_locales_count; j++) {
641 if (strEQ(system_default_locale, trial_locales[j])) {
642 goto next_iteration;
643 }
644 }
645
646 trial_locale = system_default_locale;
647 }
6bce99ee
JH
648# endif /* WIN32 */
649#endif /* SYSTEM_DEFAULT_LOCALE */
65ebb059
KW
650 }
651
98994639 652#ifdef LC_ALL
7cd8b568 653 if (! my_setlocale(LC_ALL, trial_locale)) {
49c85077 654 setlocale_failure = TRUE;
7cd8b568
KW
655 }
656 else {
657 /* Since LC_ALL succeeded, it should have changed all the other
658 * categories it can to its value; so we massage things so that the
659 * setlocales below just return their category's current values.
660 * This adequately handles the case in NetBSD where LC_COLLATE may
661 * not be defined for a locale, and setting it individually will
662 * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to
663 * the POSIX locale. */
664 trial_locale = NULL;
665 }
98994639
HS
666#endif /* LC_ALL */
667
49c85077 668 if (!setlocale_failure) {
98994639 669#ifdef USE_LOCALE_CTYPE
49c85077
KW
670 Safefree(curctype);
671 if (! (curctype = my_setlocale(LC_CTYPE, trial_locale)))
672 setlocale_failure = TRUE;
673 else
674 curctype = savepv(curctype);
98994639
HS
675#endif /* USE_LOCALE_CTYPE */
676#ifdef USE_LOCALE_COLLATE
49c85077
KW
677 Safefree(curcoll);
678 if (! (curcoll = my_setlocale(LC_COLLATE, trial_locale)))
679 setlocale_failure = TRUE;
680 else
681 curcoll = savepv(curcoll);
98994639
HS
682#endif /* USE_LOCALE_COLLATE */
683#ifdef USE_LOCALE_NUMERIC
49c85077
KW
684 Safefree(curnum);
685 if (! (curnum = my_setlocale(LC_NUMERIC, trial_locale)))
686 setlocale_failure = TRUE;
687 else
688 curnum = savepv(curnum);
98994639 689#endif /* USE_LOCALE_NUMERIC */
a782673d
KW
690#ifdef USE_LOCALE_MESSAGES
691 if (! (my_setlocale(LC_MESSAGES, trial_locale)))
692 setlocale_failure = TRUE;
693#endif /* USE_LOCALE_MESSAGES */
c835d6be
KW
694#ifdef USE_LOCALE_MONETARY
695 if (! (my_setlocale(LC_MONETARY, trial_locale)))
696 setlocale_failure = TRUE;
697#endif /* USE_LOCALE_MONETARY */
698
49c85077
KW
699 if (! setlocale_failure) { /* Success */
700 break;
701 }
65ebb059 702 }
98994639 703
49c85077
KW
704 /* Here, something failed; will need to try a fallback. */
705 ok = 0;
65ebb059 706
49c85077
KW
707 if (i == 0) {
708 unsigned int j;
98994639 709
65ebb059 710 if (locwarn) { /* Output failure info only on the first one */
98994639
HS
711#ifdef LC_ALL
712
49c85077
KW
713 PerlIO_printf(Perl_error_log,
714 "perl: warning: Setting locale failed.\n");
98994639
HS
715
716#else /* !LC_ALL */
717
49c85077
KW
718 PerlIO_printf(Perl_error_log,
719 "perl: warning: Setting locale failed for the categories:\n\t");
98994639 720#ifdef USE_LOCALE_CTYPE
49c85077
KW
721 if (! curctype)
722 PerlIO_printf(Perl_error_log, "LC_CTYPE ");
98994639
HS
723#endif /* USE_LOCALE_CTYPE */
724#ifdef USE_LOCALE_COLLATE
49c85077
KW
725 if (! curcoll)
726 PerlIO_printf(Perl_error_log, "LC_COLLATE ");
98994639
HS
727#endif /* USE_LOCALE_COLLATE */
728#ifdef USE_LOCALE_NUMERIC
49c85077
KW
729 if (! curnum)
730 PerlIO_printf(Perl_error_log, "LC_NUMERIC ");
98994639 731#endif /* USE_LOCALE_NUMERIC */
a782673d 732 PerlIO_printf(Perl_error_log, "and possibly others\n");
98994639
HS
733
734#endif /* LC_ALL */
735
49c85077
KW
736 PerlIO_printf(Perl_error_log,
737 "perl: warning: Please check that your locale settings:\n");
98994639
HS
738
739#ifdef __GLIBC__
49c85077
KW
740 PerlIO_printf(Perl_error_log,
741 "\tLANGUAGE = %c%s%c,\n",
742 language ? '"' : '(',
743 language ? language : "unset",
744 language ? '"' : ')');
98994639
HS
745#endif
746
49c85077
KW
747 PerlIO_printf(Perl_error_log,
748 "\tLC_ALL = %c%s%c,\n",
749 lc_all ? '"' : '(',
750 lc_all ? lc_all : "unset",
751 lc_all ? '"' : ')');
98994639
HS
752
753#if defined(USE_ENVIRON_ARRAY)
49c85077
KW
754 {
755 char **e;
756 for (e = environ; *e; e++) {
757 if (strnEQ(*e, "LC_", 3)
758 && strnNE(*e, "LC_ALL=", 7)
759 && (p = strchr(*e, '=')))
760 PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n",
761 (int)(p - *e), *e, p + 1);
762 }
763 }
98994639 764#else
49c85077
KW
765 PerlIO_printf(Perl_error_log,
766 "\t(possibly more locale environment variables)\n");
98994639
HS
767#endif
768
49c85077
KW
769 PerlIO_printf(Perl_error_log,
770 "\tLANG = %c%s%c\n",
771 lang ? '"' : '(',
772 lang ? lang : "unset",
773 lang ? '"' : ')');
98994639 774
49c85077
KW
775 PerlIO_printf(Perl_error_log,
776 " are supported and installed on your system.\n");
777 }
98994639 778
65ebb059
KW
779 /* Calculate what fallback locales to try. We have avoided this
780 * until we have to, becuase failure is quite unlikely. This will
781 * usually change the upper bound of the loop we are in.
782 *
783 * Since the system's default way of setting the locale has not
784 * found one that works, We use Perl's defined ordering: LC_ALL,
785 * LANG, and the C locale. We don't try the same locale twice, so
786 * don't add to the list if already there. (On POSIX systems, the
787 * LC_ALL element will likely be a repeat of the 0th element "",
788 * but there's no harm done by doing it explicitly */
789 if (lc_all) {
790 for (j = 0; j < trial_locales_count; j++) {
791 if (strEQ(lc_all, trial_locales[j])) {
792 goto done_lc_all;
793 }
794 }
795 trial_locales[trial_locales_count++] = lc_all;
796 }
797 done_lc_all:
98994639 798
65ebb059
KW
799 if (lang) {
800 for (j = 0; j < trial_locales_count; j++) {
801 if (strEQ(lang, trial_locales[j])) {
802 goto done_lang;
803 }
804 }
805 trial_locales[trial_locales_count++] = lang;
806 }
807 done_lang:
808
809#if defined(WIN32) && defined(LC_ALL)
810 /* For Windows, we also try the system default locale before "C".
811 * (If there exists a Windows without LC_ALL we skip this because
812 * it gets too complicated. For those, the "C" is the next
813 * fallback possibility). The "" is the same as the 0th element of
814 * the array, but the code at the loop above knows to treat it
815 * differently when not the 0th */
816 trial_locales[trial_locales_count++] = "";
817#endif
818
819 for (j = 0; j < trial_locales_count; j++) {
820 if (strEQ("C", trial_locales[j])) {
821 goto done_C;
822 }
823 }
824 trial_locales[trial_locales_count++] = "C";
98994639 825
65ebb059
KW
826 done_C: ;
827 } /* end of first time through the loop */
98994639 828
65ebb059
KW
829#ifdef WIN32
830 next_iteration: ;
831#endif
832
833 } /* end of looping through the trial locales */
834
835 if (ok < 1) { /* If we tried to fallback */
836 const char* msg;
837 if (! setlocale_failure) { /* fallback succeeded */
838 msg = "Falling back to";
839 }
840 else { /* fallback failed */
98994639 841
65ebb059
KW
842 /* We dropped off the end of the loop, so have to decrement i to
843 * get back to the value the last time through */
844 i--;
98994639 845
65ebb059
KW
846 ok = -1;
847 msg = "Failed to fall back to";
848
849 /* To continue, we should use whatever values we've got */
98994639 850#ifdef USE_LOCALE_CTYPE
49c85077
KW
851 Safefree(curctype);
852 curctype = savepv(setlocale(LC_CTYPE, NULL));
98994639
HS
853#endif /* USE_LOCALE_CTYPE */
854#ifdef USE_LOCALE_COLLATE
49c85077
KW
855 Safefree(curcoll);
856 curcoll = savepv(setlocale(LC_COLLATE, NULL));
98994639
HS
857#endif /* USE_LOCALE_COLLATE */
858#ifdef USE_LOCALE_NUMERIC
49c85077
KW
859 Safefree(curnum);
860 curnum = savepv(setlocale(LC_NUMERIC, NULL));
98994639 861#endif /* USE_LOCALE_NUMERIC */
65ebb059
KW
862 }
863
864 if (locwarn) {
865 const char * description;
866 const char * name = "";
867 if (strEQ(trial_locales[i], "C")) {
868 description = "the standard locale";
869 name = "C";
870 }
6bce99ee 871#ifdef SYSTEM_DEFAULT_LOCALE
65ebb059
KW
872 else if (strEQ(trial_locales[i], "")) {
873 description = "the system default locale";
874 if (system_default_locale) {
875 name = system_default_locale;
876 }
877 }
6bce99ee 878#endif /* SYSTEM_DEFAULT_LOCALE */
65ebb059
KW
879 else {
880 description = "a fallback locale";
881 name = trial_locales[i];
882 }
883 if (name && strNE(name, "")) {
884 PerlIO_printf(Perl_error_log,
885 "perl: warning: %s %s (\"%s\").\n", msg, description, name);
886 }
887 else {
888 PerlIO_printf(Perl_error_log,
889 "perl: warning: %s %s.\n", msg, description);
890 }
891 }
892 } /* End of tried to fallback */
98994639
HS
893
894#ifdef USE_LOCALE_CTYPE
895 new_ctype(curctype);
896#endif /* USE_LOCALE_CTYPE */
897
898#ifdef USE_LOCALE_COLLATE
899 new_collate(curcoll);
900#endif /* USE_LOCALE_COLLATE */
901
902#ifdef USE_LOCALE_NUMERIC
903 new_numeric(curnum);
904#endif /* USE_LOCALE_NUMERIC */
b310b053 905
8ef6e574 906#if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE)
49c85077
KW
907 /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE
908 * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by
909 * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the
910 * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open
911 * discipline. */
c1284011 912 PL_utf8locale = _is_cur_LC_category_utf8(LC_CTYPE);
49c85077 913
a05d7ebb 914 /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
fde18df1
JH
915 This is an alternative to using the -C command line switch
916 (the -C if present will override this). */
917 {
dd374669 918 const char *p = PerlEnv_getenv("PERL_UNICODE");
a05d7ebb 919 PL_unicode = p ? parse_unicode_opts(&p) : 0;
5a22a2bb
NC
920 if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG)
921 PL_utf8cache = -1;
b310b053 922 }
ec71e770 923#endif
b310b053 924
98994639 925#ifdef USE_LOCALE_CTYPE
43c5f42d 926 Safefree(curctype);
98994639
HS
927#endif /* USE_LOCALE_CTYPE */
928#ifdef USE_LOCALE_COLLATE
43c5f42d 929 Safefree(curcoll);
98994639
HS
930#endif /* USE_LOCALE_COLLATE */
931#ifdef USE_LOCALE_NUMERIC
43c5f42d 932 Safefree(curnum);
98994639 933#endif /* USE_LOCALE_NUMERIC */
8ef6e574 934
f2ce9e1c
JH
935#else /* !USE_LOCALE */
936 PERL_UNUSED_ARG(printwarn);
8ef6e574
KW
937#endif /* USE_LOCALE */
938
98994639
HS
939 return ok;
940}
941
49c85077 942
98994639
HS
943#ifdef USE_LOCALE_COLLATE
944
945/*
946 * mem_collxfrm() is a bit like strxfrm() but with two important
947 * differences. First, it handles embedded NULs. Second, it allocates
948 * a bit more memory than needed for the transformed data itself.
949 * The real transformed data begins at offset sizeof(collationix).
950 * Please see sv_collxfrm() to see how this is used.
951 */
166f8a29 952
98994639
HS
953char *
954Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
955{
956 char *xbuf;
957 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
958
7918f24d
NC
959 PERL_ARGS_ASSERT_MEM_COLLXFRM;
960
98994639
HS
961 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
962 /* the +1 is for the terminating NUL. */
963
964 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
a02a5408 965 Newx(xbuf, xAlloc, char);
98994639
HS
966 if (! xbuf)
967 goto bad;
968
969 *(U32*)xbuf = PL_collation_ix;
970 xout = sizeof(PL_collation_ix);
971 for (xin = 0; xin < len; ) {
224e8ef5 972 Size_t xused;
98994639
HS
973
974 for (;;) {
975 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
224e8ef5 976 if (xused >= PERL_INT_MAX)
98994639 977 goto bad;
eb160463 978 if ((STRLEN)xused < xAlloc - xout)
98994639
HS
979 break;
980 xAlloc = (2 * xAlloc) + 1;
981 Renew(xbuf, xAlloc, char);
982 if (! xbuf)
983 goto bad;
984 }
985
986 xin += strlen(s + xin) + 1;
987 xout += xused;
988
989 /* Embedded NULs are understood but silently skipped
990 * because they make no sense in locale collation. */
991 }
992
993 xbuf[xout] = '\0';
994 *xlen = xout - sizeof(PL_collation_ix);
995 return xbuf;
996
997 bad:
998 Safefree(xbuf);
999 *xlen = 0;
1000 return NULL;
1001}
1002
1003#endif /* USE_LOCALE_COLLATE */
1004
8ef6e574
KW
1005#ifdef USE_LOCALE
1006
c1284011
KW
1007bool
1008Perl__is_cur_LC_category_utf8(pTHX_ int category)
7d74bb61
KW
1009{
1010 /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
1011 * otherwise. 'category' may not be LC_ALL. If the platform doesn't have
119ee68b
KW
1012 * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence
1013 * could give the wrong result. It errs on the side of not being a UTF-8
1014 * locale. */
7d74bb61
KW
1015
1016 char *save_input_locale = NULL;
7d74bb61
KW
1017 STRLEN final_pos;
1018
8ef6e574 1019#ifdef LC_ALL
7d74bb61 1020 assert(category != LC_ALL);
8ef6e574 1021#endif
7d74bb61
KW
1022
1023 /* First dispose of the trivial cases */
b07fffd1 1024 save_input_locale = setlocale(category, NULL);
7d74bb61 1025 if (! save_input_locale) {
69014004
KW
1026 DEBUG_L(PerlIO_printf(Perl_debug_log,
1027 "Could not find current locale for category %d\n",
1028 category));
7d74bb61
KW
1029 return FALSE; /* XXX maybe should croak */
1030 }
b07fffd1 1031 save_input_locale = stdize_locale(savepv(save_input_locale));
a39edc4c 1032 if (isNAME_C_OR_POSIX(save_input_locale)) {
69014004
KW
1033 DEBUG_L(PerlIO_printf(Perl_debug_log,
1034 "Current locale for category %d is %s\n",
1035 category, save_input_locale));
b07fffd1 1036 Safefree(save_input_locale);
7d74bb61
KW
1037 return FALSE;
1038 }
1039
1d958db2
KW
1040#if defined(USE_LOCALE_CTYPE) \
1041 && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET)))
7d74bb61 1042
1d958db2 1043 { /* Next try nl_langinfo or MB_CUR_MAX if available */
7d74bb61
KW
1044
1045 char *save_ctype_locale = NULL;
119ee68b 1046 bool is_utf8;
7d74bb61 1047
119ee68b 1048 if (category != LC_CTYPE) { /* These work only on LC_CTYPE */
7d74bb61
KW
1049
1050 /* Get the current LC_CTYPE locale */
1051 save_ctype_locale = stdize_locale(savepv(setlocale(LC_CTYPE, NULL)));
1052 if (! save_ctype_locale) {
69014004
KW
1053 DEBUG_L(PerlIO_printf(Perl_debug_log,
1054 "Could not find current locale for LC_CTYPE\n"));
7d74bb61
KW
1055 goto cant_use_nllanginfo;
1056 }
1057
1058 /* If LC_CTYPE and the desired category use the same locale, this
1059 * means that finding the value for LC_CTYPE is the same as finding
1060 * the value for the desired category. Otherwise, switch LC_CTYPE
1061 * to the desired category's locale */
1062 if (strEQ(save_ctype_locale, save_input_locale)) {
1063 Safefree(save_ctype_locale);
1064 save_ctype_locale = NULL;
1065 }
1066 else if (! setlocale(LC_CTYPE, save_input_locale)) {
69014004
KW
1067 DEBUG_L(PerlIO_printf(Perl_debug_log,
1068 "Could not change LC_CTYPE locale to %s\n",
1069 save_input_locale));
7d74bb61
KW
1070 Safefree(save_ctype_locale);
1071 goto cant_use_nllanginfo;
1072 }
1073 }
1074
69014004
KW
1075 DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n",
1076 save_input_locale));
1077
7d74bb61 1078 /* Here the current LC_CTYPE is set to the locale of the category whose
1d958db2
KW
1079 * information is desired. This means that nl_langinfo() and MB_CUR_MAX
1080 * should give the correct results */
119ee68b 1081
1d958db2
KW
1082# if defined(HAS_NL_LANGINFO) && defined(CODESET)
1083 {
1084 char *codeset = savepv(nl_langinfo(CODESET));
1085 if (codeset && strNE(codeset, "")) {
119ee68b 1086
1d958db2
KW
1087 /* If we switched LC_CTYPE, switch back */
1088 if (save_ctype_locale) {
1089 setlocale(LC_CTYPE, save_ctype_locale);
1090 Safefree(save_ctype_locale);
1091 }
1092
1093 is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8"))
1094 || foldEQ(codeset, STR_WITH_LEN("UTF8"));
1095
69014004
KW
1096 DEBUG_L(PerlIO_printf(Perl_debug_log,
1097 "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n",
1098 codeset, is_utf8));
1d958db2
KW
1099 Safefree(codeset);
1100 Safefree(save_input_locale);
1101 return is_utf8;
1102 }
0ac78434 1103 Safefree(codeset);
119ee68b
KW
1104 }
1105
1d958db2
KW
1106# endif
1107# ifdef MB_CUR_MAX
1108
1109 /* Here, either we don't have nl_langinfo, or it didn't return a
1110 * codeset. Try MB_CUR_MAX */
1111
119ee68b
KW
1112 /* Standard UTF-8 needs at least 4 bytes to represent the maximum
1113 * Unicode code point. Since UTF-8 is the only non-single byte
1114 * encoding we handle, we just say any such encoding is UTF-8, and if
1115 * turns out to be wrong, other things will fail */
1116 is_utf8 = MB_CUR_MAX >= 4;
1117
69014004
KW
1118 DEBUG_L(PerlIO_printf(Perl_debug_log,
1119 "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n",
1120 (int) MB_CUR_MAX, is_utf8));
1121
119ee68b
KW
1122 Safefree(save_input_locale);
1123
1124# ifdef HAS_MBTOWC
1125
1126 /* ... But, most system that have MB_CUR_MAX will also have mbtowc(),
1127 * since they are both in the C99 standard. We can feed a known byte
1128 * string to the latter function, and check that it gives the expected
1129 * result */
1130 if (is_utf8) {
1131 wchar_t wc;
a6715020 1132 GCC_DIAG_IGNORE(-Wunused-result);
1d958db2 1133 (void) mbtowc(&wc, NULL, 0); /* Reset any shift state */
a6715020 1134 GCC_DIAG_RESTORE;
69014004 1135 errno = 0;
f019f68f 1136 if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
119ee68b
KW
1137 != strlen(HYPHEN_UTF8)
1138 || wc != (wchar_t) 0x2010)
1139 {
1140 is_utf8 = FALSE;
69014004
KW
1141 DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", wc));
1142 DEBUG_L(PerlIO_printf(Perl_debug_log,
1143 "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n",
1144 mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno));
119ee68b
KW
1145 }
1146 }
119ee68b
KW
1147# endif
1148
1d958db2
KW
1149 /* If we switched LC_CTYPE, switch back */
1150 if (save_ctype_locale) {
1151 setlocale(LC_CTYPE, save_ctype_locale);
1152 Safefree(save_ctype_locale);
119ee68b 1153 }
7d74bb61 1154
1d958db2 1155 return is_utf8;
119ee68b 1156# endif
7d74bb61 1157 }
119ee68b 1158
7d74bb61
KW
1159 cant_use_nllanginfo:
1160
1161#endif /* HAS_NL_LANGINFO etc */
1162
1163 /* nl_langinfo not available or failed somehow. Look at the locale name to
751e9426 1164 * see if it matches qr/UTF -? 8 /ix */
7d74bb61
KW
1165
1166 final_pos = strlen(save_input_locale) - 1;
751e9426
KW
1167 if (final_pos >= 3) {
1168 char *name = save_input_locale;
1169
1170 /* Find next 'U' or 'u' and look from there */
1171 while ((name += strcspn(name, "Uu") + 1)
1172 <= save_input_locale + final_pos - 2)
7d74bb61 1173 {
751e9426
KW
1174 if (toFOLD(*(name)) != 't'
1175 || toFOLD(*(name + 1)) != 'f')
1176 {
1177 continue;
1178 }
1179 name += 2;
1180 if (*(name) == '-') {
1181 if ((name > save_input_locale + final_pos - 1)) {
1182 break;
1183 }
1184 name++;
1185 }
1186 if (*(name) == '8') {
1187 Safefree(save_input_locale);
69014004
KW
1188 DEBUG_L(PerlIO_printf(Perl_debug_log,
1189 "Locale %s ends with UTF-8 in name\n",
1190 save_input_locale));
751e9426
KW
1191 return TRUE;
1192 }
7d74bb61 1193 }
69014004
KW
1194 DEBUG_L(PerlIO_printf(Perl_debug_log,
1195 "Locale %s doesn't end with UTF-8 in name\n",
1196 save_input_locale));
7d74bb61
KW
1197 }
1198
1199#ifdef WIN32
1200 /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
1201 if (final_pos >= 4
1202 && *(save_input_locale + final_pos - 0) == '1'
1203 && *(save_input_locale + final_pos - 1) == '0'
1204 && *(save_input_locale + final_pos - 2) == '0'
1205 && *(save_input_locale + final_pos - 3) == '5'
1206 && *(save_input_locale + final_pos - 4) == '6')
1207 {
69014004
KW
1208 DEBUG_L(PerlIO_printf(Perl_debug_log,
1209 "Locale %s ends with 10056 in name, is UTF-8 locale\n",
1210 save_input_locale));
d37662c0 1211 Safefree(save_input_locale);
7d74bb61
KW
1212 return TRUE;
1213 }
1214#endif
1215
fa9b773e
KW
1216 /* Other common encodings are the ISO 8859 series, which aren't UTF-8 */
1217 if (instr(save_input_locale, "8859")) {
69014004
KW
1218 DEBUG_L(PerlIO_printf(Perl_debug_log,
1219 "Locale %s has 8859 in name, not UTF-8 locale\n",
1220 save_input_locale));
d37662c0 1221 Safefree(save_input_locale);
fa9b773e
KW
1222 return FALSE;
1223 }
1224
1225#ifdef HAS_LOCALECONV
1226
1227# ifdef USE_LOCALE_MONETARY
1228
1229 /* Here, there is nothing in the locale name to indicate whether the locale
1230 * is UTF-8 or not. This "name", the return of setlocale(), is actually
1231 * defined to be opaque, so we can't really rely on the absence of various
1232 * substrings in the name to indicate its UTF-8ness. Look at the locale's
1233 * currency symbol. Often that will be in the native script, and if the
1234 * symbol isn't in UTF-8, we know that the locale isn't. If it is
1235 * non-ASCII UTF-8, we infer that the locale is too.
1236 * To do this, like above for LC_CTYPE, we first set LC_MONETARY to the
1237 * locale of the desired category, if it isn't that locale already */
1238
1239 {
1240 char *save_monetary_locale = NULL;
1241 bool illegal_utf8 = FALSE;
1242 bool only_ascii = FALSE;
1243 const struct lconv* const lc = localeconv();
1244
1245 if (category != LC_MONETARY) {
1246
1247 save_monetary_locale = stdize_locale(savepv(setlocale(LC_MONETARY,
1248 NULL)));
1249 if (! save_monetary_locale) {
69014004
KW
1250 DEBUG_L(PerlIO_printf(Perl_debug_log,
1251 "Could not find current locale for LC_MONETARY\n"));
fa9b773e
KW
1252 goto cant_use_monetary;
1253 }
1254
1255 if (strNE(save_monetary_locale, save_input_locale)) {
1256 if (! setlocale(LC_MONETARY, save_input_locale)) {
69014004
KW
1257 DEBUG_L(PerlIO_printf(Perl_debug_log,
1258 "Could not change LC_MONETARY locale to %s\n",
1259 save_input_locale));
fa9b773e
KW
1260 Safefree(save_monetary_locale);
1261 goto cant_use_monetary;
1262 }
1263 }
1264 }
1265
1266 /* Here the current LC_MONETARY is set to the locale of the category
1267 * whose information is desired. */
1268
1269 if (lc && lc->currency_symbol) {
1270 if (! is_utf8_string((U8 *) lc->currency_symbol, 0)) {
69014004
KW
1271 DEBUG_L(PerlIO_printf(Perl_debug_log,
1272 "Currency symbol for %s is not legal UTF-8\n",
1273 save_input_locale));
fa9b773e
KW
1274 illegal_utf8 = TRUE;
1275 }
1276 else if (is_ascii_string((U8 *) lc->currency_symbol, 0)) {
69014004 1277 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
1278 only_ascii = TRUE;
1279 }
1280 }
1281
1282 /* If we changed it, restore LC_MONETARY to its original locale */
1283 if (save_monetary_locale) {
1284 setlocale(LC_MONETARY, save_monetary_locale);
1285 Safefree(save_monetary_locale);
1286 }
1287
ce7f1f15
KW
1288 if (only_ascii) {
1289 goto cant_use_monetary;
1290 }
1291
fa9b773e
KW
1292 Safefree(save_input_locale);
1293
1294 /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; otherwise
1295 * assume the locale is UTF-8 if and only if the symbol is non-ascii
1296 * UTF-8. (We can't really tell if the locale is UTF-8 or not if the
1297 * symbol is just a '$', so we err on the side of it not being UTF-8)
1298 * */
ce7f1f15
KW
1299 DEBUG_L(PerlIO_printf(Perl_debug_log, "\tis_utf8=%d\n", ! illegal_utf8));
1300 return ! illegal_utf8;
fa9b773e
KW
1301 }
1302 cant_use_monetary:
1303
1304# endif /* USE_LOCALE_MONETARY */
1305#endif /* HAS_LOCALECONV */
1306
855aeb93
JH
1307#if 0 && defined(HAS_STRERROR) && defined(USE_LOCALE_MESSAGES)
1308
1309/* This code is ifdefd out because it was found to not be necessary in testing
1310 * on our dromedary test machine, which has over 700 locales. There, looking
1311 * at just the currency symbol gave essentially the same results as doing this
1312 * extra work. Executing this also caused segfaults in miniperl. I left it in
1313 * so as to avoid rewriting it if real-world experience indicates that
1314 * dromedary is an outlier. Essentially, instead of returning abpve if we
1315 * haven't found illegal utf8, we continue on and examine all the strerror()
1316 * messages on the platform for utf8ness. If all are ASCII, we still don't
1317 * know the answer; but otherwise we have a pretty good indication of the
1318 * utf8ness. The reason this doesn't necessarily help much is that the
1319 * messages may not have been translated into the locale. The currency symbol
1320 * is much more likely to have been translated. The code below would need to
1321 * be altered somewhat to just be a continuation of testing the currency
1322 * symbol. */
1323 int e;
1324 unsigned int failures = 0, non_ascii = 0;
1325 char *save_messages_locale = NULL;
1326
1327 /* Like above for LC_CTYPE, we set LC_MESSAGES to the locale of the
1328 * desired category, if it isn't that locale already */
1329
1330 if (category != LC_MESSAGES) {
1331
1332 save_messages_locale = stdize_locale(savepv(setlocale(LC_MESSAGES,
1333 NULL)));
1334 if (! save_messages_locale) {
1335 goto cant_use_messages;
1336 }
1337
1338 if (strEQ(save_messages_locale, save_input_locale)) {
1339 Safefree(save_input_locale);
1340 }
1341 else if (! setlocale(LC_MESSAGES, save_input_locale)) {
1342 Safefree(save_messages_locale);
1343 goto cant_use_messages;
1344 }
1345 }
1346
1347 /* Here the current LC_MESSAGES is set to the locale of the category
1348 * whose information is desired. Look through all the messages */
1349
1350 for (e = 0;
1351#ifdef HAS_SYS_ERRLIST
1352 e <= sys_nerr
1353#endif
1354 ; e++)
1355 {
1356 const U8* const errmsg = (U8 *) Strerror(e) ;
1357 if (!errmsg)
1358 break;
1359 if (! is_utf8_string(errmsg, 0)) {
1360 failures++;
1361 break;
1362 }
1363 else if (! is_ascii_string(errmsg, 0)) {
1364 non_ascii++;
1365 }
1366 }
1367
1368 /* And, if we changed it, restore LC_MESSAGES to its original locale */
1369 if (save_messages_locale) {
1370 setlocale(LC_MESSAGES, save_messages_locale);
1371 Safefree(save_messages_locale);
1372 }
1373
1374 /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid,
1375 * any non-ascii means it is one; otherwise we assume it isn't */
1376 return (failures) ? FALSE : non_ascii;
1377
1378 }
1379 cant_use_messages:
1380
1381#endif
fa9b773e 1382
69014004
KW
1383 DEBUG_L(PerlIO_printf(Perl_debug_log,
1384 "Assuming locale %s is not a UTF-8 locale\n",
1385 save_input_locale));
fa9b773e 1386 Safefree(save_input_locale);
7d74bb61
KW
1387 return FALSE;
1388}
1389
8ef6e574 1390#endif
7d74bb61 1391
d6ded950
KW
1392
1393bool
1394Perl__is_in_locale_category(pTHX_ const bool compiling, const int category)
1395{
1a4f13e1 1396 dVAR;
d6ded950
KW
1397 /* Internal function which returns if we are in the scope of a pragma that
1398 * enables the locale category 'category'. 'compiling' should indicate if
1399 * this is during the compilation phase (TRUE) or not (FALSE). */
1400
1401 const COP * const cop = (compiling) ? &PL_compiling : PL_curcop;
1402
1403 SV *categories = cop_hints_fetch_pvs(cop, "locale", 0);
1404 if (! categories || categories == &PL_sv_placeholder) {
1405 return FALSE;
1406 }
1407
1408 /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get
1409 * a valid unsigned */
1410 assert(category >= -1);
1411 return cBOOL(SvUV(categories) & (1U << (category + 1)));
1412}
1413
2c6ee1a7
KW
1414char *
1415Perl_my_strerror(pTHX_ const int errnum) {
1416
1417 /* Uses C locale for the error text unless within scope of 'use locale' for
1418 * LC_MESSAGES */
1419
1420#ifdef USE_LOCALE_MESSAGES
8ee4b769 1421 if (! IN_LC(LC_MESSAGES)) {
2c6ee1a7 1422 char * save_locale = setlocale(LC_MESSAGES, NULL);
a39edc4c 1423 if (! isNAME_C_OR_POSIX(save_locale)) {
2c6ee1a7
KW
1424 char *errstr;
1425
1426 /* The next setlocale likely will zap this, so create a copy */
1427 save_locale = savepv(save_locale);
1428
1429 setlocale(LC_MESSAGES, "C");
1430
1431 /* This points to the static space in Strerror, with all its
1432 * limitations */
1433 errstr = Strerror(errnum);
1434
1435 setlocale(LC_MESSAGES, save_locale);
1436 Safefree(save_locale);
1437 return errstr;
1438 }
1439 }
1440#endif
1441
1442 return Strerror(errnum);
1443}
1444
66610fdd
RGS
1445/*
1446 * Local variables:
1447 * c-indentation-style: bsd
1448 * c-basic-offset: 4
14d04a33 1449 * indent-tabs-mode: nil
66610fdd
RGS
1450 * End:
1451 *
14d04a33 1452 * ex: set ts=8 sts=4 sw=4 et:
37442d52 1453 */