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