| 1 | /* locale.c |
| 2 | * |
| 3 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
| 4 | * 2002, 2003, 2005, 2006, 2007, 2008 by Larry Wall and others |
| 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 | /* |
| 12 | * A Elbereth Gilthoniel, |
| 13 | * silivren penna míriel |
| 14 | * o menel aglar elenath! |
| 15 | * Na-chaered palan-díriel |
| 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"] |
| 21 | */ |
| 22 | |
| 23 | /* utility functions for handling locale-specific stuff like what |
| 24 | * character represents the decimal point. |
| 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. |
| 33 | */ |
| 34 | |
| 35 | #include "EXTERN.h" |
| 36 | #define PERL_IN_LOCALE_C |
| 37 | #include "perl.h" |
| 38 | |
| 39 | #ifdef I_LANGINFO |
| 40 | # include <langinfo.h> |
| 41 | #endif |
| 42 | |
| 43 | #include "reentr.h" |
| 44 | |
| 45 | #ifdef USE_LOCALE |
| 46 | |
| 47 | /* |
| 48 | * Standardize the locale name from a string returned by 'setlocale', possibly |
| 49 | * modifying that string. |
| 50 | * |
| 51 | * The typical return value of setlocale() is either |
| 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, |
| 55 | * in some unspecified order). This is not handled by this function. |
| 56 | * |
| 57 | * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n", |
| 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 '=' |
| 60 | * |
| 61 | */ |
| 62 | STATIC char * |
| 63 | S_stdize_locale(pTHX_ char *locs) |
| 64 | { |
| 65 | const char * const s = strchr(locs, '='); |
| 66 | bool okay = TRUE; |
| 67 | |
| 68 | PERL_ARGS_ASSERT_STDIZE_LOCALE; |
| 69 | |
| 70 | if (s) { |
| 71 | const char * const t = strchr(s, '.'); |
| 72 | okay = FALSE; |
| 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; |
| 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 | |
| 90 | #endif |
| 91 | |
| 92 | void |
| 93 | Perl_set_numeric_radix(pTHX) |
| 94 | { |
| 95 | #ifdef USE_LOCALE_NUMERIC |
| 96 | # ifdef HAS_LOCALECONV |
| 97 | const struct lconv* const lc = localeconv(); |
| 98 | |
| 99 | if (lc && lc->decimal_point) { |
| 100 | if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) { |
| 101 | SvREFCNT_dec(PL_numeric_radix_sv); |
| 102 | PL_numeric_radix_sv = NULL; |
| 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); |
| 109 | if (! is_invariant_string((U8 *) lc->decimal_point, 0) |
| 110 | && is_utf8_string((U8 *) lc->decimal_point, 0) |
| 111 | && _is_cur_LC_category_utf8(LC_NUMERIC)) |
| 112 | { |
| 113 | SvUTF8_on(PL_numeric_radix_sv); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | else |
| 118 | PL_numeric_radix_sv = NULL; |
| 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 | |
| 125 | # endif /* HAS_LOCALECONV */ |
| 126 | #endif /* USE_LOCALE_NUMERIC */ |
| 127 | } |
| 128 | |
| 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 */ |
| 139 | #define isNAME_C_OR_POSIX(name) ((name) != NULL \ |
| 140 | && ((*(name) == 'C' && (*(name + 1)) == '\0') \ |
| 141 | || strEQ((name), "POSIX"))) |
| 142 | |
| 143 | void |
| 144 | Perl_new_numeric(pTHX_ const char *newnum) |
| 145 | { |
| 146 | #ifdef USE_LOCALE_NUMERIC |
| 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 underlying locale's name: a copy of 'newnum' |
| 163 | * PL_numeric_local A boolean indicating if the toggled state is such |
| 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. |
| 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 | |
| 179 | char *save_newnum; |
| 180 | |
| 181 | if (! newnum) { |
| 182 | Safefree(PL_numeric_name); |
| 183 | PL_numeric_name = NULL; |
| 184 | PL_numeric_standard = TRUE; |
| 185 | PL_numeric_local = TRUE; |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | save_newnum = stdize_locale(savepv(newnum)); |
| 190 | if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) { |
| 191 | Safefree(PL_numeric_name); |
| 192 | PL_numeric_name = save_newnum; |
| 193 | } |
| 194 | |
| 195 | PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum); |
| 196 | PL_numeric_local = TRUE; |
| 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 | |
| 203 | set_numeric_radix(); |
| 204 | |
| 205 | #else |
| 206 | PERL_UNUSED_ARG(newnum); |
| 207 | #endif /* USE_LOCALE_NUMERIC */ |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | Perl_set_numeric_standard(pTHX) |
| 212 | { |
| 213 | #ifdef USE_LOCALE_NUMERIC |
| 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) */ |
| 219 | |
| 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(); |
| 224 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 225 | "Underlying LC_NUMERIC locale now is C\n")); |
| 226 | |
| 227 | #endif /* USE_LOCALE_NUMERIC */ |
| 228 | } |
| 229 | |
| 230 | void |
| 231 | Perl_set_numeric_local(pTHX) |
| 232 | { |
| 233 | #ifdef USE_LOCALE_NUMERIC |
| 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) */ |
| 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(); |
| 244 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 245 | "Underlying LC_NUMERIC locale now is %s\n", |
| 246 | PL_numeric_name)); |
| 247 | |
| 248 | #endif /* USE_LOCALE_NUMERIC */ |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Set up for a new ctype locale. |
| 253 | */ |
| 254 | void |
| 255 | Perl_new_ctype(pTHX_ const char *newctype) |
| 256 | { |
| 257 | #ifdef USE_LOCALE_CTYPE |
| 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 | |
| 270 | dVAR; |
| 271 | UV i; |
| 272 | |
| 273 | PERL_ARGS_ASSERT_NEW_CTYPE; |
| 274 | |
| 275 | /* We will replace any bad locale warning with 1) nothing if the new one is |
| 276 | * ok; or 2) a new warning for the bad new locale */ |
| 277 | if (PL_warn_locale) { |
| 278 | SvREFCNT_dec_NN(PL_warn_locale); |
| 279 | PL_warn_locale = NULL; |
| 280 | } |
| 281 | |
| 282 | PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE); |
| 283 | |
| 284 | /* A UTF-8 locale gets standard rules. But note that code still has to |
| 285 | * handle this specially because of the three problematic code points */ |
| 286 | if (PL_in_utf8_CTYPE_locale) { |
| 287 | Copy(PL_fold_latin1, PL_fold_locale, 256, U8); |
| 288 | } |
| 289 | else { |
| 290 | /* Assume enough space for every character being bad. 4 spaces each |
| 291 | * for the 94 printable characters that are output like "'x' "; and 5 |
| 292 | * spaces each for "'\\' ", "'\t' ", and "'\n' "; plus a terminating |
| 293 | * NUL */ |
| 294 | char bad_chars_list[ (94 * 4) + (3 * 5) + 1 ]; |
| 295 | |
| 296 | bool check_for_problems = ckWARN_d(WARN_LOCALE); /* No warnings means |
| 297 | no check */ |
| 298 | bool multi_byte_locale = FALSE; /* Assume is a single-byte locale |
| 299 | to start */ |
| 300 | unsigned int bad_count = 0; /* Count of bad characters */ |
| 301 | |
| 302 | for (i = 0; i < 256; i++) { |
| 303 | if (isUPPER_LC((U8) i)) |
| 304 | PL_fold_locale[i] = (U8) toLOWER_LC((U8) i); |
| 305 | else if (isLOWER_LC((U8) i)) |
| 306 | PL_fold_locale[i] = (U8) toUPPER_LC((U8) i); |
| 307 | else |
| 308 | PL_fold_locale[i] = (U8) i; |
| 309 | |
| 310 | /* If checking for locale problems, see if the native ASCII-range |
| 311 | * printables plus \n and \t are in their expected categories in |
| 312 | * the new locale. If not, this could mean big trouble, upending |
| 313 | * Perl's and most programs' assumptions, like having a |
| 314 | * metacharacter with special meaning become a \w. Fortunately, |
| 315 | * it's very rare to find locales that aren't supersets of ASCII |
| 316 | * nowadays. It isn't a problem for most controls to be changed |
| 317 | * into something else; we check only \n and \t, though perhaps \r |
| 318 | * could be an issue as well. */ |
| 319 | if (check_for_problems |
| 320 | && (isGRAPH_A(i) || isBLANK_A(i) || i == '\n')) |
| 321 | { |
| 322 | if ((isALPHANUMERIC_A(i) && ! isALPHANUMERIC_LC(i)) |
| 323 | || (isPUNCT_A(i) && ! isPUNCT_LC(i)) |
| 324 | || (isBLANK_A(i) && ! isBLANK_LC(i)) |
| 325 | || (i == '\n' && ! isCNTRL_LC(i))) |
| 326 | { |
| 327 | if (bad_count) { /* Separate multiple entries with a |
| 328 | blank */ |
| 329 | bad_chars_list[bad_count++] = ' '; |
| 330 | } |
| 331 | bad_chars_list[bad_count++] = '\''; |
| 332 | if (isPRINT_A(i)) { |
| 333 | bad_chars_list[bad_count++] = (char) i; |
| 334 | } |
| 335 | else { |
| 336 | bad_chars_list[bad_count++] = '\\'; |
| 337 | if (i == '\n') { |
| 338 | bad_chars_list[bad_count++] = 'n'; |
| 339 | } |
| 340 | else { |
| 341 | assert(i == '\t'); |
| 342 | bad_chars_list[bad_count++] = 't'; |
| 343 | } |
| 344 | } |
| 345 | bad_chars_list[bad_count++] = '\''; |
| 346 | bad_chars_list[bad_count] = '\0'; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | #ifdef MB_CUR_MAX |
| 352 | /* We only handle single-byte locales (outside of UTF-8 ones; so if |
| 353 | * this locale requires than one byte, there are going to be |
| 354 | * problems. */ |
| 355 | if (check_for_problems && MB_CUR_MAX > 1 |
| 356 | |
| 357 | /* Some platforms return MB_CUR_MAX > 1 for even the "C" |
| 358 | * locale. Just assume that the implementation for them (plus |
| 359 | * for POSIX) is correct and the > 1 value is spurious. (Since |
| 360 | * these are specially handled to never be considered UTF-8 |
| 361 | * locales, as long as this is the only problem, everything |
| 362 | * should work fine */ |
| 363 | && strNE(newctype, "C") && strNE(newctype, "POSIX")) |
| 364 | { |
| 365 | multi_byte_locale = TRUE; |
| 366 | } |
| 367 | #endif |
| 368 | |
| 369 | if (bad_count || multi_byte_locale) { |
| 370 | PL_warn_locale = Perl_newSVpvf(aTHX_ |
| 371 | "Locale '%s' may not work well.%s%s%s\n", |
| 372 | newctype, |
| 373 | (multi_byte_locale) |
| 374 | ? " Some characters in it are not recognized by" |
| 375 | " Perl." |
| 376 | : "", |
| 377 | (bad_count) |
| 378 | ? "\nThe following characters (and maybe others)" |
| 379 | " may not have the same meaning as the Perl" |
| 380 | " program expects:\n" |
| 381 | : "", |
| 382 | (bad_count) |
| 383 | ? bad_chars_list |
| 384 | : "" |
| 385 | ); |
| 386 | /* If we are actually in the scope of the locale, output the |
| 387 | * message now. Otherwise we save it to be output at the first |
| 388 | * operation using this locale, if that actually happens. Most |
| 389 | * programs don't use locales, so they are immune to bad ones */ |
| 390 | if (IN_LC(LC_CTYPE)) { |
| 391 | |
| 392 | /* We have to save 'newctype' because the setlocale() just |
| 393 | * below may destroy it. The next setlocale() further down |
| 394 | * should restore it properly so that the intermediate change |
| 395 | * here is transparent to this function's caller */ |
| 396 | const char * const badlocale = savepv(newctype); |
| 397 | |
| 398 | setlocale(LC_CTYPE, "C"); |
| 399 | |
| 400 | /* The '0' below suppresses a bogus gcc compiler warning */ |
| 401 | Perl_warner(aTHX_ packWARN(WARN_LOCALE), SvPVX(PL_warn_locale), 0); |
| 402 | setlocale(LC_CTYPE, badlocale); |
| 403 | Safefree(badlocale); |
| 404 | SvREFCNT_dec_NN(PL_warn_locale); |
| 405 | PL_warn_locale = NULL; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | #endif /* USE_LOCALE_CTYPE */ |
| 411 | PERL_ARGS_ASSERT_NEW_CTYPE; |
| 412 | PERL_UNUSED_ARG(newctype); |
| 413 | PERL_UNUSED_CONTEXT; |
| 414 | } |
| 415 | |
| 416 | void |
| 417 | Perl__warn_problematic_locale() |
| 418 | { |
| 419 | |
| 420 | #ifdef USE_LOCALE_CTYPE |
| 421 | |
| 422 | dTHX; |
| 423 | |
| 424 | /* Internal-to-core function that outputs the message in PL_warn_locale, |
| 425 | * and then NULLS it. Should be called only through the macro |
| 426 | * _CHECK_AND_WARN_PROBLEMATIC_LOCALE */ |
| 427 | |
| 428 | if (PL_warn_locale) { |
| 429 | /*GCC_DIAG_IGNORE(-Wformat-security); Didn't work */ |
| 430 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), |
| 431 | SvPVX(PL_warn_locale), |
| 432 | 0 /* dummy to avoid compiler warning */ ); |
| 433 | /* GCC_DIAG_RESTORE; */ |
| 434 | SvREFCNT_dec_NN(PL_warn_locale); |
| 435 | PL_warn_locale = NULL; |
| 436 | } |
| 437 | |
| 438 | #endif |
| 439 | |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | Perl_new_collate(pTHX_ const char *newcoll) |
| 444 | { |
| 445 | #ifdef USE_LOCALE_COLLATE |
| 446 | |
| 447 | /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell |
| 448 | * core Perl this and that 'newcoll' is the name of the new locale. |
| 449 | * |
| 450 | * Any code changing the locale (outside this file) should use |
| 451 | * POSIX::setlocale, which calls this function. Therefore this function |
| 452 | * should be called directly only from this file and from |
| 453 | * POSIX::setlocale() */ |
| 454 | |
| 455 | if (! newcoll) { |
| 456 | if (PL_collation_name) { |
| 457 | ++PL_collation_ix; |
| 458 | Safefree(PL_collation_name); |
| 459 | PL_collation_name = NULL; |
| 460 | } |
| 461 | PL_collation_standard = TRUE; |
| 462 | PL_collxfrm_base = 0; |
| 463 | PL_collxfrm_mult = 2; |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
| 468 | ++PL_collation_ix; |
| 469 | Safefree(PL_collation_name); |
| 470 | PL_collation_name = stdize_locale(savepv(newcoll)); |
| 471 | PL_collation_standard = isNAME_C_OR_POSIX(newcoll); |
| 472 | |
| 473 | { |
| 474 | /* 2: at most so many chars ('a', 'b'). */ |
| 475 | /* 50: surely no system expands a char more. */ |
| 476 | #define XFRMBUFSIZE (2 * 50) |
| 477 | char xbuf[XFRMBUFSIZE]; |
| 478 | const Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); |
| 479 | const Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); |
| 480 | const SSize_t mult = fb - fa; |
| 481 | if (mult < 1 && !(fa == 0 && fb == 0)) |
| 482 | Perl_croak(aTHX_ "panic: strxfrm() gets absurd - a => %"UVuf", ab => %"UVuf, |
| 483 | (UV) fa, (UV) fb); |
| 484 | PL_collxfrm_base = (fa > (Size_t)mult) ? (fa - mult) : 0; |
| 485 | PL_collxfrm_mult = mult; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | #else |
| 490 | PERL_UNUSED_ARG(newcoll); |
| 491 | #endif /* USE_LOCALE_COLLATE */ |
| 492 | } |
| 493 | |
| 494 | #ifdef WIN32 |
| 495 | |
| 496 | char * |
| 497 | Perl_my_setlocale(pTHX_ int category, const char* locale) |
| 498 | { |
| 499 | /* This, for Windows, emulates POSIX setlocale() behavior. There is no |
| 500 | * difference unless the input locale is "", which means on Windows to get |
| 501 | * the machine default, which is set via the computer's "Regional and |
| 502 | * Language Options" (or its current equivalent). In POSIX, it instead |
| 503 | * means to find the locale from the user's environment. This routine |
| 504 | * looks in the environment, and, if anything is found, uses that instead |
| 505 | * of going to the machine default. If there is no environment override, |
| 506 | * the machine default is used, as normal, by calling the real setlocale() |
| 507 | * with "". The POSIX behavior is to use the LC_ALL variable if set; |
| 508 | * otherwise to use the particular category's variable if set; otherwise to |
| 509 | * use the LANG variable. */ |
| 510 | |
| 511 | bool override_LC_ALL = FALSE; |
| 512 | char * result; |
| 513 | |
| 514 | if (locale && strEQ(locale, "")) { |
| 515 | # ifdef LC_ALL |
| 516 | locale = PerlEnv_getenv("LC_ALL"); |
| 517 | if (! locale) { |
| 518 | #endif |
| 519 | switch (category) { |
| 520 | # ifdef LC_ALL |
| 521 | case LC_ALL: |
| 522 | override_LC_ALL = TRUE; |
| 523 | break; /* We already know its variable isn't set */ |
| 524 | # endif |
| 525 | # ifdef USE_LOCALE_TIME |
| 526 | case LC_TIME: |
| 527 | locale = PerlEnv_getenv("LC_TIME"); |
| 528 | break; |
| 529 | # endif |
| 530 | # ifdef USE_LOCALE_CTYPE |
| 531 | case LC_CTYPE: |
| 532 | locale = PerlEnv_getenv("LC_CTYPE"); |
| 533 | break; |
| 534 | # endif |
| 535 | # ifdef USE_LOCALE_COLLATE |
| 536 | case LC_COLLATE: |
| 537 | locale = PerlEnv_getenv("LC_COLLATE"); |
| 538 | break; |
| 539 | # endif |
| 540 | # ifdef USE_LOCALE_MONETARY |
| 541 | case LC_MONETARY: |
| 542 | locale = PerlEnv_getenv("LC_MONETARY"); |
| 543 | break; |
| 544 | # endif |
| 545 | # ifdef USE_LOCALE_NUMERIC |
| 546 | case LC_NUMERIC: |
| 547 | locale = PerlEnv_getenv("LC_NUMERIC"); |
| 548 | break; |
| 549 | # endif |
| 550 | # ifdef USE_LOCALE_MESSAGES |
| 551 | case LC_MESSAGES: |
| 552 | locale = PerlEnv_getenv("LC_MESSAGES"); |
| 553 | break; |
| 554 | # endif |
| 555 | default: |
| 556 | /* This is a category, like PAPER_SIZE that we don't |
| 557 | * know about; and so can't provide a wrapper. */ |
| 558 | break; |
| 559 | } |
| 560 | if (! locale) { |
| 561 | locale = PerlEnv_getenv("LANG"); |
| 562 | if (! locale) { |
| 563 | locale = ""; |
| 564 | } |
| 565 | } |
| 566 | # ifdef LC_ALL |
| 567 | } |
| 568 | # endif |
| 569 | } |
| 570 | |
| 571 | result = setlocale(category, locale); |
| 572 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", __FILE__, __LINE__, |
| 573 | _setlocale_debug_string(category, locale, result))); |
| 574 | |
| 575 | if (! override_LC_ALL) { |
| 576 | return result; |
| 577 | } |
| 578 | |
| 579 | /* Here the input category was LC_ALL, and we have set it to what is in the |
| 580 | * LANG variable or the system default if there is no LANG. But these have |
| 581 | * lower priority than the other LC_foo variables, so override it for each |
| 582 | * one that is set. (If they are set to "", it means to use the same thing |
| 583 | * we just set LC_ALL to, so can skip) */ |
| 584 | # ifdef USE_LOCALE_TIME |
| 585 | result = PerlEnv_getenv("LC_TIME"); |
| 586 | if (result && strNE(result, "")) { |
| 587 | setlocale(LC_TIME, result); |
| 588 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 589 | __FILE__, __LINE__, |
| 590 | _setlocale_debug_string(LC_TIME, result, "not captured"))); |
| 591 | } |
| 592 | # endif |
| 593 | # ifdef USE_LOCALE_CTYPE |
| 594 | result = PerlEnv_getenv("LC_CTYPE"); |
| 595 | if (result && strNE(result, "")) { |
| 596 | setlocale(LC_CTYPE, result); |
| 597 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 598 | __FILE__, __LINE__, |
| 599 | _setlocale_debug_string(LC_CTYPE, result, "not captured"))); |
| 600 | } |
| 601 | # endif |
| 602 | # ifdef USE_LOCALE_COLLATE |
| 603 | result = PerlEnv_getenv("LC_COLLATE"); |
| 604 | if (result && strNE(result, "")) { |
| 605 | setlocale(LC_COLLATE, result); |
| 606 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 607 | __FILE__, __LINE__, |
| 608 | _setlocale_debug_string(LC_COLLATE, result, "not captured"))); |
| 609 | } |
| 610 | # endif |
| 611 | # ifdef USE_LOCALE_MONETARY |
| 612 | result = PerlEnv_getenv("LC_MONETARY"); |
| 613 | if (result && strNE(result, "")) { |
| 614 | setlocale(LC_MONETARY, result); |
| 615 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 616 | __FILE__, __LINE__, |
| 617 | _setlocale_debug_string(LC_MONETARY, result, "not captured"))); |
| 618 | } |
| 619 | # endif |
| 620 | # ifdef USE_LOCALE_NUMERIC |
| 621 | result = PerlEnv_getenv("LC_NUMERIC"); |
| 622 | if (result && strNE(result, "")) { |
| 623 | setlocale(LC_NUMERIC, result); |
| 624 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 625 | __FILE__, __LINE__, |
| 626 | _setlocale_debug_string(LC_NUMERIC, result, "not captured"))); |
| 627 | } |
| 628 | # endif |
| 629 | # ifdef USE_LOCALE_MESSAGES |
| 630 | result = PerlEnv_getenv("LC_MESSAGES"); |
| 631 | if (result && strNE(result, "")) { |
| 632 | setlocale(LC_MESSAGES, result); |
| 633 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 634 | __FILE__, __LINE__, |
| 635 | _setlocale_debug_string(LC_MESSAGES, result, "not captured"))); |
| 636 | } |
| 637 | # endif |
| 638 | |
| 639 | result = setlocale(LC_ALL, NULL); |
| 640 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 641 | __FILE__, __LINE__, |
| 642 | _setlocale_debug_string(LC_ALL, NULL, result))); |
| 643 | |
| 644 | return result; |
| 645 | } |
| 646 | |
| 647 | #endif |
| 648 | |
| 649 | |
| 650 | /* |
| 651 | * Initialize locale awareness. |
| 652 | */ |
| 653 | int |
| 654 | Perl_init_i18nl10n(pTHX_ int printwarn) |
| 655 | { |
| 656 | /* printwarn is |
| 657 | * |
| 658 | * 0 if not to output warning when setup locale is bad |
| 659 | * 1 if to output warning based on value of PERL_BADLANG |
| 660 | * >1 if to output regardless of PERL_BADLANG |
| 661 | * |
| 662 | * returns |
| 663 | * 1 = set ok or not applicable, |
| 664 | * 0 = fallback to a locale of lower priority |
| 665 | * -1 = fallback to all locales failed, not even to the C locale |
| 666 | * |
| 667 | * Under -DDEBUGGING, if the environment variable PERL_DEBUG_LOCALE_INIT is |
| 668 | * set, debugging information is output. |
| 669 | * |
| 670 | * This looks more complicated than it is, mainly due to the #ifdefs. |
| 671 | * |
| 672 | * We try to set LC_ALL to the value determined by the environment. If |
| 673 | * there is no LC_ALL on this platform, we try the individual categories we |
| 674 | * know about. If this works, we are done. |
| 675 | * |
| 676 | * But if it doesn't work, we have to do something else. We search the |
| 677 | * environment variables ourselves instead of relying on the system to do |
| 678 | * it. We look at, in order, LC_ALL, LANG, a system default locale (if we |
| 679 | * think there is one), and the ultimate fallback "C". This is all done in |
| 680 | * the same loop as above to avoid duplicating code, but it makes things |
| 681 | * more complex. After the original failure, we add the fallback |
| 682 | * possibilities to the list of locales to try, and iterate the loop |
| 683 | * through them all until one succeeds. |
| 684 | * |
| 685 | * On Ultrix, the locale MUST come from the environment, so there is |
| 686 | * preliminary code to set it. I (khw) am not sure that it is necessary, |
| 687 | * and that this couldn't be folded into the loop, but barring any real |
| 688 | * platforms to test on, it's staying as-is |
| 689 | * |
| 690 | * A slight complication is that in embedded Perls, the locale may already |
| 691 | * be set-up, and we don't want to get it from the normal environment |
| 692 | * variables. This is handled by having a special environment variable |
| 693 | * indicate we're in this situation. We simply set setlocale's 2nd |
| 694 | * parameter to be a NULL instead of "". That indicates to setlocale that |
| 695 | * it is not to change anything, but to return the current value, |
| 696 | * effectively initializing perl's db to what the locale already is. |
| 697 | * |
| 698 | * We play the same trick with NULL if a LC_ALL succeeds. We call |
| 699 | * setlocale() on the individual categores with NULL to get their existing |
| 700 | * values for our db, instead of trying to change them. |
| 701 | * */ |
| 702 | |
| 703 | int ok = 1; |
| 704 | |
| 705 | #if defined(USE_LOCALE) |
| 706 | #ifdef USE_LOCALE_CTYPE |
| 707 | char *curctype = NULL; |
| 708 | #endif /* USE_LOCALE_CTYPE */ |
| 709 | #ifdef USE_LOCALE_COLLATE |
| 710 | char *curcoll = NULL; |
| 711 | #endif /* USE_LOCALE_COLLATE */ |
| 712 | #ifdef USE_LOCALE_NUMERIC |
| 713 | char *curnum = NULL; |
| 714 | #endif /* USE_LOCALE_NUMERIC */ |
| 715 | #ifdef __GLIBC__ |
| 716 | const char * const language = savepv(PerlEnv_getenv("LANGUAGE")); |
| 717 | #endif |
| 718 | |
| 719 | /* NULL uses the existing already set up locale */ |
| 720 | const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT")) |
| 721 | ? NULL |
| 722 | : ""; |
| 723 | #ifdef DEBUGGING |
| 724 | const bool debug = (PerlEnv_getenv("PERL_DEBUG_LOCALE_INIT")) |
| 725 | ? TRUE |
| 726 | : FALSE; |
| 727 | # define DEBUG_LOCALE_INIT(category, locale, result) \ |
| 728 | STMT_START { \ |
| 729 | if (debug) { \ |
| 730 | PerlIO_printf(Perl_debug_log, \ |
| 731 | "%s:%d: %s\n", \ |
| 732 | __FILE__, __LINE__, \ |
| 733 | _setlocale_debug_string(category, \ |
| 734 | locale, \ |
| 735 | result)); \ |
| 736 | } \ |
| 737 | } STMT_END |
| 738 | #else |
| 739 | # define DEBUG_LOCALE_INIT(a,b,c) |
| 740 | #endif |
| 741 | const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */ |
| 742 | unsigned int trial_locales_count; |
| 743 | const char * const lc_all = savepv(PerlEnv_getenv("LC_ALL")); |
| 744 | const char * const lang = savepv(PerlEnv_getenv("LANG")); |
| 745 | bool setlocale_failure = FALSE; |
| 746 | unsigned int i; |
| 747 | char *p; |
| 748 | |
| 749 | /* A later getenv() could zap this, so only use here */ |
| 750 | const char * const bad_lang_use_once = PerlEnv_getenv("PERL_BADLANG"); |
| 751 | |
| 752 | const bool locwarn = (printwarn > 1 |
| 753 | || (printwarn |
| 754 | && (! bad_lang_use_once |
| 755 | || ( |
| 756 | /* disallow with "" or "0" */ |
| 757 | *bad_lang_use_once |
| 758 | && strNE("0", bad_lang_use_once))))); |
| 759 | bool done = FALSE; |
| 760 | char * sl_result; /* return from setlocale() */ |
| 761 | char * locale_param; |
| 762 | #ifdef WIN32 |
| 763 | /* In some systems you can find out the system default locale |
| 764 | * and use that as the fallback locale. */ |
| 765 | # define SYSTEM_DEFAULT_LOCALE |
| 766 | #endif |
| 767 | #ifdef SYSTEM_DEFAULT_LOCALE |
| 768 | const char *system_default_locale = NULL; |
| 769 | #endif |
| 770 | |
| 771 | #ifndef LOCALE_ENVIRON_REQUIRED |
| 772 | PERL_UNUSED_VAR(done); |
| 773 | PERL_UNUSED_VAR(locale_param); |
| 774 | #else |
| 775 | |
| 776 | /* |
| 777 | * Ultrix setlocale(..., "") fails if there are no environment |
| 778 | * variables from which to get a locale name. |
| 779 | */ |
| 780 | |
| 781 | # ifdef LC_ALL |
| 782 | if (lang) { |
| 783 | sl_result = my_setlocale(LC_ALL, setlocale_init); |
| 784 | DEBUG_LOCALE_INIT(LC_ALL, setlocale_init, sl_result); |
| 785 | if (sl_result) |
| 786 | done = TRUE; |
| 787 | else |
| 788 | setlocale_failure = TRUE; |
| 789 | } |
| 790 | if (! setlocale_failure) { |
| 791 | # ifdef USE_LOCALE_CTYPE |
| 792 | locale_param = (! done && (lang || PerlEnv_getenv("LC_CTYPE"))) |
| 793 | ? setlocale_init |
| 794 | : NULL; |
| 795 | curctype = my_setlocale(LC_CTYPE, locale_param); |
| 796 | DEBUG_LOCALE_INIT(LC_CTYPE, locale_param, sl_result); |
| 797 | if (! curctype) |
| 798 | setlocale_failure = TRUE; |
| 799 | else |
| 800 | curctype = savepv(curctype); |
| 801 | # endif /* USE_LOCALE_CTYPE */ |
| 802 | # ifdef USE_LOCALE_COLLATE |
| 803 | locale_param = (! done && (lang || PerlEnv_getenv("LC_COLLATE"))) |
| 804 | ? setlocale_init |
| 805 | : NULL; |
| 806 | curcoll = my_setlocale(LC_COLLATE, locale_param); |
| 807 | DEBUG_LOCALE_INIT(LC_COLLATE, locale_param, sl_result); |
| 808 | if (! curcoll) |
| 809 | setlocale_failure = TRUE; |
| 810 | else |
| 811 | curcoll = savepv(curcoll); |
| 812 | # endif /* USE_LOCALE_COLLATE */ |
| 813 | # ifdef USE_LOCALE_NUMERIC |
| 814 | locale_param = (! done && (lang || PerlEnv_getenv("LC_NUMERIC"))) |
| 815 | ? setlocale_init |
| 816 | : NULL; |
| 817 | curnum = my_setlocale(LC_NUMERIC, locale_param); |
| 818 | DEBUG_LOCALE_INIT(LC_NUMERIC, locale_param, sl_result); |
| 819 | if (! curnum) |
| 820 | setlocale_failure = TRUE; |
| 821 | else |
| 822 | curnum = savepv(curnum); |
| 823 | # endif /* USE_LOCALE_NUMERIC */ |
| 824 | # ifdef USE_LOCALE_MESSAGES |
| 825 | locale_param = (! done && (lang || PerlEnv_getenv("LC_MESSAGES"))) |
| 826 | ? setlocale_init |
| 827 | : NULL; |
| 828 | sl_result = my_setlocale(LC_MESSAGES, locale_param); |
| 829 | DEBUG_LOCALE_INIT(LC_MESSAGES, locale_param, sl_result); |
| 830 | if (! sl_result) |
| 831 | setlocale_failure = TRUE; |
| 832 | } |
| 833 | # endif /* USE_LOCALE_MESSAGES */ |
| 834 | # ifdef USE_LOCALE_MONETARY |
| 835 | locale_param = (! done && (lang || PerlEnv_getenv("LC_MONETARY"))) |
| 836 | ? setlocale_init |
| 837 | : NULL; |
| 838 | sl_result = my_setlocale(LC_MONETARY, locale_param); |
| 839 | DEBUG_LOCALE_INIT(LC_MONETARY, locale_param, sl_result); |
| 840 | if (! sl_result) { |
| 841 | setlocale_failure = TRUE; |
| 842 | } |
| 843 | # endif /* USE_LOCALE_MONETARY */ |
| 844 | } |
| 845 | |
| 846 | # endif /* LC_ALL */ |
| 847 | |
| 848 | #endif /* !LOCALE_ENVIRON_REQUIRED */ |
| 849 | |
| 850 | /* We try each locale in the list until we get one that works, or exhaust |
| 851 | * the list. Normally the loop is executed just once. But if setting the |
| 852 | * locale fails, inside the loop we add fallback trials to the array and so |
| 853 | * will execute the loop multiple times */ |
| 854 | trial_locales[0] = setlocale_init; |
| 855 | trial_locales_count = 1; |
| 856 | for (i= 0; i < trial_locales_count; i++) { |
| 857 | const char * trial_locale = trial_locales[i]; |
| 858 | |
| 859 | if (i > 0) { |
| 860 | |
| 861 | /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED |
| 862 | * when i==0, but I (khw) don't think that behavior makes much |
| 863 | * sense */ |
| 864 | setlocale_failure = FALSE; |
| 865 | |
| 866 | #ifdef SYSTEM_DEFAULT_LOCALE |
| 867 | # ifdef WIN32 |
| 868 | /* On Windows machines, an entry of "" after the 0th means to use |
| 869 | * the system default locale, which we now proceed to get. */ |
| 870 | if (strEQ(trial_locale, "")) { |
| 871 | unsigned int j; |
| 872 | |
| 873 | /* Note that this may change the locale, but we are going to do |
| 874 | * that anyway just below */ |
| 875 | system_default_locale = setlocale(LC_ALL, ""); |
| 876 | DEBUG_LOCALE_INIT(LC_ALL, "", system_default_locale); |
| 877 | |
| 878 | /* Skip if invalid or it's already on the list of locales to |
| 879 | * try */ |
| 880 | if (! system_default_locale) { |
| 881 | goto next_iteration; |
| 882 | } |
| 883 | for (j = 0; j < trial_locales_count; j++) { |
| 884 | if (strEQ(system_default_locale, trial_locales[j])) { |
| 885 | goto next_iteration; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | trial_locale = system_default_locale; |
| 890 | } |
| 891 | # endif /* WIN32 */ |
| 892 | #endif /* SYSTEM_DEFAULT_LOCALE */ |
| 893 | } |
| 894 | |
| 895 | #ifdef LC_ALL |
| 896 | sl_result = my_setlocale(LC_ALL, trial_locale); |
| 897 | DEBUG_LOCALE_INIT(LC_ALL, trial_locale, sl_result); |
| 898 | if (! sl_result) { |
| 899 | setlocale_failure = TRUE; |
| 900 | } |
| 901 | else { |
| 902 | /* Since LC_ALL succeeded, it should have changed all the other |
| 903 | * categories it can to its value; so we massage things so that the |
| 904 | * setlocales below just return their category's current values. |
| 905 | * This adequately handles the case in NetBSD where LC_COLLATE may |
| 906 | * not be defined for a locale, and setting it individually will |
| 907 | * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to |
| 908 | * the POSIX locale. */ |
| 909 | trial_locale = NULL; |
| 910 | } |
| 911 | #endif /* LC_ALL */ |
| 912 | |
| 913 | if (!setlocale_failure) { |
| 914 | #ifdef USE_LOCALE_CTYPE |
| 915 | Safefree(curctype); |
| 916 | curctype = my_setlocale(LC_CTYPE, trial_locale); |
| 917 | DEBUG_LOCALE_INIT(LC_CTYPE, trial_locale, curctype); |
| 918 | if (! curctype) |
| 919 | setlocale_failure = TRUE; |
| 920 | else |
| 921 | curctype = savepv(curctype); |
| 922 | #endif /* USE_LOCALE_CTYPE */ |
| 923 | #ifdef USE_LOCALE_COLLATE |
| 924 | Safefree(curcoll); |
| 925 | curcoll = my_setlocale(LC_COLLATE, trial_locale); |
| 926 | DEBUG_LOCALE_INIT(LC_COLLATE, trial_locale, curcoll); |
| 927 | if (! curcoll) |
| 928 | setlocale_failure = TRUE; |
| 929 | else |
| 930 | curcoll = savepv(curcoll); |
| 931 | #endif /* USE_LOCALE_COLLATE */ |
| 932 | #ifdef USE_LOCALE_NUMERIC |
| 933 | Safefree(curnum); |
| 934 | curnum = my_setlocale(LC_NUMERIC, trial_locale); |
| 935 | DEBUG_LOCALE_INIT(LC_NUMERIC, trial_locale, curnum); |
| 936 | if (! curnum) |
| 937 | setlocale_failure = TRUE; |
| 938 | else |
| 939 | curnum = savepv(curnum); |
| 940 | #endif /* USE_LOCALE_NUMERIC */ |
| 941 | #ifdef USE_LOCALE_MESSAGES |
| 942 | sl_result = my_setlocale(LC_MESSAGES, trial_locale); |
| 943 | DEBUG_LOCALE_INIT(LC_MESSAGES, trial_locale, sl_result); |
| 944 | if (! (sl_result)) |
| 945 | setlocale_failure = TRUE; |
| 946 | #endif /* USE_LOCALE_MESSAGES */ |
| 947 | #ifdef USE_LOCALE_MONETARY |
| 948 | sl_result = my_setlocale(LC_MONETARY, trial_locale); |
| 949 | DEBUG_LOCALE_INIT(LC_MONETARY, trial_locale, sl_result); |
| 950 | if (! (sl_result)) |
| 951 | setlocale_failure = TRUE; |
| 952 | #endif /* USE_LOCALE_MONETARY */ |
| 953 | |
| 954 | if (! setlocale_failure) { /* Success */ |
| 955 | break; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | /* Here, something failed; will need to try a fallback. */ |
| 960 | ok = 0; |
| 961 | |
| 962 | if (i == 0) { |
| 963 | unsigned int j; |
| 964 | |
| 965 | if (locwarn) { /* Output failure info only on the first one */ |
| 966 | #ifdef LC_ALL |
| 967 | |
| 968 | PerlIO_printf(Perl_error_log, |
| 969 | "perl: warning: Setting locale failed.\n"); |
| 970 | |
| 971 | #else /* !LC_ALL */ |
| 972 | |
| 973 | PerlIO_printf(Perl_error_log, |
| 974 | "perl: warning: Setting locale failed for the categories:\n\t"); |
| 975 | # ifdef USE_LOCALE_CTYPE |
| 976 | if (! curctype) |
| 977 | PerlIO_printf(Perl_error_log, "LC_CTYPE "); |
| 978 | # endif /* USE_LOCALE_CTYPE */ |
| 979 | # ifdef USE_LOCALE_COLLATE |
| 980 | if (! curcoll) |
| 981 | PerlIO_printf(Perl_error_log, "LC_COLLATE "); |
| 982 | # endif /* USE_LOCALE_COLLATE */ |
| 983 | # ifdef USE_LOCALE_NUMERIC |
| 984 | if (! curnum) |
| 985 | PerlIO_printf(Perl_error_log, "LC_NUMERIC "); |
| 986 | # endif /* USE_LOCALE_NUMERIC */ |
| 987 | PerlIO_printf(Perl_error_log, "and possibly others\n"); |
| 988 | |
| 989 | #endif /* LC_ALL */ |
| 990 | |
| 991 | PerlIO_printf(Perl_error_log, |
| 992 | "perl: warning: Please check that your locale settings:\n"); |
| 993 | |
| 994 | #ifdef __GLIBC__ |
| 995 | PerlIO_printf(Perl_error_log, |
| 996 | "\tLANGUAGE = %c%s%c,\n", |
| 997 | language ? '"' : '(', |
| 998 | language ? language : "unset", |
| 999 | language ? '"' : ')'); |
| 1000 | #endif |
| 1001 | |
| 1002 | PerlIO_printf(Perl_error_log, |
| 1003 | "\tLC_ALL = %c%s%c,\n", |
| 1004 | lc_all ? '"' : '(', |
| 1005 | lc_all ? lc_all : "unset", |
| 1006 | lc_all ? '"' : ')'); |
| 1007 | |
| 1008 | #if defined(USE_ENVIRON_ARRAY) |
| 1009 | { |
| 1010 | char **e; |
| 1011 | for (e = environ; *e; e++) { |
| 1012 | if (strnEQ(*e, "LC_", 3) |
| 1013 | && strnNE(*e, "LC_ALL=", 7) |
| 1014 | && (p = strchr(*e, '='))) |
| 1015 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", |
| 1016 | (int)(p - *e), *e, p + 1); |
| 1017 | } |
| 1018 | } |
| 1019 | #else |
| 1020 | PerlIO_printf(Perl_error_log, |
| 1021 | "\t(possibly more locale environment variables)\n"); |
| 1022 | #endif |
| 1023 | |
| 1024 | PerlIO_printf(Perl_error_log, |
| 1025 | "\tLANG = %c%s%c\n", |
| 1026 | lang ? '"' : '(', |
| 1027 | lang ? lang : "unset", |
| 1028 | lang ? '"' : ')'); |
| 1029 | |
| 1030 | PerlIO_printf(Perl_error_log, |
| 1031 | " are supported and installed on your system.\n"); |
| 1032 | } |
| 1033 | |
| 1034 | /* Calculate what fallback locales to try. We have avoided this |
| 1035 | * until we have to, because failure is quite unlikely. This will |
| 1036 | * usually change the upper bound of the loop we are in. |
| 1037 | * |
| 1038 | * Since the system's default way of setting the locale has not |
| 1039 | * found one that works, We use Perl's defined ordering: LC_ALL, |
| 1040 | * LANG, and the C locale. We don't try the same locale twice, so |
| 1041 | * don't add to the list if already there. (On POSIX systems, the |
| 1042 | * LC_ALL element will likely be a repeat of the 0th element "", |
| 1043 | * but there's no harm done by doing it explicitly. |
| 1044 | * |
| 1045 | * Note that this tries the LC_ALL environment variable even on |
| 1046 | * systems which have no LC_ALL locale setting. This may or may |
| 1047 | * not have been originally intentional, but there's no real need |
| 1048 | * to change the behavior. */ |
| 1049 | if (lc_all) { |
| 1050 | for (j = 0; j < trial_locales_count; j++) { |
| 1051 | if (strEQ(lc_all, trial_locales[j])) { |
| 1052 | goto done_lc_all; |
| 1053 | } |
| 1054 | } |
| 1055 | trial_locales[trial_locales_count++] = lc_all; |
| 1056 | } |
| 1057 | done_lc_all: |
| 1058 | |
| 1059 | if (lang) { |
| 1060 | for (j = 0; j < trial_locales_count; j++) { |
| 1061 | if (strEQ(lang, trial_locales[j])) { |
| 1062 | goto done_lang; |
| 1063 | } |
| 1064 | } |
| 1065 | trial_locales[trial_locales_count++] = lang; |
| 1066 | } |
| 1067 | done_lang: |
| 1068 | |
| 1069 | #if defined(WIN32) && defined(LC_ALL) |
| 1070 | /* For Windows, we also try the system default locale before "C". |
| 1071 | * (If there exists a Windows without LC_ALL we skip this because |
| 1072 | * it gets too complicated. For those, the "C" is the next |
| 1073 | * fallback possibility). The "" is the same as the 0th element of |
| 1074 | * the array, but the code at the loop above knows to treat it |
| 1075 | * differently when not the 0th */ |
| 1076 | trial_locales[trial_locales_count++] = ""; |
| 1077 | #endif |
| 1078 | |
| 1079 | for (j = 0; j < trial_locales_count; j++) { |
| 1080 | if (strEQ("C", trial_locales[j])) { |
| 1081 | goto done_C; |
| 1082 | } |
| 1083 | } |
| 1084 | trial_locales[trial_locales_count++] = "C"; |
| 1085 | |
| 1086 | done_C: ; |
| 1087 | } /* end of first time through the loop */ |
| 1088 | |
| 1089 | #ifdef WIN32 |
| 1090 | next_iteration: ; |
| 1091 | #endif |
| 1092 | |
| 1093 | } /* end of looping through the trial locales */ |
| 1094 | |
| 1095 | if (ok < 1) { /* If we tried to fallback */ |
| 1096 | const char* msg; |
| 1097 | if (! setlocale_failure) { /* fallback succeeded */ |
| 1098 | msg = "Falling back to"; |
| 1099 | } |
| 1100 | else { /* fallback failed */ |
| 1101 | |
| 1102 | /* We dropped off the end of the loop, so have to decrement i to |
| 1103 | * get back to the value the last time through */ |
| 1104 | i--; |
| 1105 | |
| 1106 | ok = -1; |
| 1107 | msg = "Failed to fall back to"; |
| 1108 | |
| 1109 | /* To continue, we should use whatever values we've got */ |
| 1110 | #ifdef USE_LOCALE_CTYPE |
| 1111 | Safefree(curctype); |
| 1112 | curctype = savepv(setlocale(LC_CTYPE, NULL)); |
| 1113 | DEBUG_LOCALE_INIT(LC_CTYPE, NULL, curctype); |
| 1114 | #endif /* USE_LOCALE_CTYPE */ |
| 1115 | #ifdef USE_LOCALE_COLLATE |
| 1116 | Safefree(curcoll); |
| 1117 | curcoll = savepv(setlocale(LC_COLLATE, NULL)); |
| 1118 | DEBUG_LOCALE_INIT(LC_COLLATE, NULL, curcoll); |
| 1119 | #endif /* USE_LOCALE_COLLATE */ |
| 1120 | #ifdef USE_LOCALE_NUMERIC |
| 1121 | Safefree(curnum); |
| 1122 | curnum = savepv(setlocale(LC_NUMERIC, NULL)); |
| 1123 | DEBUG_LOCALE_INIT(LC_NUMERIC, NULL, curnum); |
| 1124 | #endif /* USE_LOCALE_NUMERIC */ |
| 1125 | } |
| 1126 | |
| 1127 | if (locwarn) { |
| 1128 | const char * description; |
| 1129 | const char * name = ""; |
| 1130 | if (strEQ(trial_locales[i], "C")) { |
| 1131 | description = "the standard locale"; |
| 1132 | name = "C"; |
| 1133 | } |
| 1134 | #ifdef SYSTEM_DEFAULT_LOCALE |
| 1135 | else if (strEQ(trial_locales[i], "")) { |
| 1136 | description = "the system default locale"; |
| 1137 | if (system_default_locale) { |
| 1138 | name = system_default_locale; |
| 1139 | } |
| 1140 | } |
| 1141 | #endif /* SYSTEM_DEFAULT_LOCALE */ |
| 1142 | else { |
| 1143 | description = "a fallback locale"; |
| 1144 | name = trial_locales[i]; |
| 1145 | } |
| 1146 | if (name && strNE(name, "")) { |
| 1147 | PerlIO_printf(Perl_error_log, |
| 1148 | "perl: warning: %s %s (\"%s\").\n", msg, description, name); |
| 1149 | } |
| 1150 | else { |
| 1151 | PerlIO_printf(Perl_error_log, |
| 1152 | "perl: warning: %s %s.\n", msg, description); |
| 1153 | } |
| 1154 | } |
| 1155 | } /* End of tried to fallback */ |
| 1156 | |
| 1157 | #ifdef USE_LOCALE_CTYPE |
| 1158 | new_ctype(curctype); |
| 1159 | #endif /* USE_LOCALE_CTYPE */ |
| 1160 | |
| 1161 | #ifdef USE_LOCALE_COLLATE |
| 1162 | new_collate(curcoll); |
| 1163 | #endif /* USE_LOCALE_COLLATE */ |
| 1164 | |
| 1165 | #ifdef USE_LOCALE_NUMERIC |
| 1166 | new_numeric(curnum); |
| 1167 | #endif /* USE_LOCALE_NUMERIC */ |
| 1168 | |
| 1169 | #if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE) |
| 1170 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE |
| 1171 | * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by |
| 1172 | * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the |
| 1173 | * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open |
| 1174 | * discipline. */ |
| 1175 | PL_utf8locale = _is_cur_LC_category_utf8(LC_CTYPE); |
| 1176 | |
| 1177 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
| 1178 | This is an alternative to using the -C command line switch |
| 1179 | (the -C if present will override this). */ |
| 1180 | { |
| 1181 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
| 1182 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
| 1183 | if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG) |
| 1184 | PL_utf8cache = -1; |
| 1185 | } |
| 1186 | #endif |
| 1187 | |
| 1188 | #ifdef USE_LOCALE_CTYPE |
| 1189 | Safefree(curctype); |
| 1190 | #endif /* USE_LOCALE_CTYPE */ |
| 1191 | #ifdef USE_LOCALE_COLLATE |
| 1192 | Safefree(curcoll); |
| 1193 | #endif /* USE_LOCALE_COLLATE */ |
| 1194 | #ifdef USE_LOCALE_NUMERIC |
| 1195 | Safefree(curnum); |
| 1196 | #endif /* USE_LOCALE_NUMERIC */ |
| 1197 | |
| 1198 | #ifdef __GLIBC__ |
| 1199 | Safefree(language); |
| 1200 | #endif |
| 1201 | |
| 1202 | Safefree(lc_all); |
| 1203 | Safefree(lang); |
| 1204 | |
| 1205 | #else /* !USE_LOCALE */ |
| 1206 | PERL_UNUSED_ARG(printwarn); |
| 1207 | #endif /* USE_LOCALE */ |
| 1208 | |
| 1209 | return ok; |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | #ifdef USE_LOCALE_COLLATE |
| 1214 | |
| 1215 | /* |
| 1216 | * mem_collxfrm() is a bit like strxfrm() but with two important |
| 1217 | * differences. First, it handles embedded NULs. Second, it allocates |
| 1218 | * a bit more memory than needed for the transformed data itself. |
| 1219 | * The real transformed data begins at offset sizeof(collationix). |
| 1220 | * Please see sv_collxfrm() to see how this is used. |
| 1221 | */ |
| 1222 | |
| 1223 | char * |
| 1224 | Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen) |
| 1225 | { |
| 1226 | char *xbuf; |
| 1227 | STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */ |
| 1228 | |
| 1229 | PERL_ARGS_ASSERT_MEM_COLLXFRM; |
| 1230 | |
| 1231 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ |
| 1232 | /* the +1 is for the terminating NUL. */ |
| 1233 | |
| 1234 | xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1; |
| 1235 | Newx(xbuf, xAlloc, char); |
| 1236 | if (! xbuf) |
| 1237 | goto bad; |
| 1238 | |
| 1239 | *(U32*)xbuf = PL_collation_ix; |
| 1240 | xout = sizeof(PL_collation_ix); |
| 1241 | for (xin = 0; xin < len; ) { |
| 1242 | Size_t xused; |
| 1243 | |
| 1244 | for (;;) { |
| 1245 | xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout); |
| 1246 | if (xused >= PERL_INT_MAX) |
| 1247 | goto bad; |
| 1248 | if ((STRLEN)xused < xAlloc - xout) |
| 1249 | break; |
| 1250 | xAlloc = (2 * xAlloc) + 1; |
| 1251 | Renew(xbuf, xAlloc, char); |
| 1252 | if (! xbuf) |
| 1253 | goto bad; |
| 1254 | } |
| 1255 | |
| 1256 | xin += strlen(s + xin) + 1; |
| 1257 | xout += xused; |
| 1258 | |
| 1259 | /* Embedded NULs are understood but silently skipped |
| 1260 | * because they make no sense in locale collation. */ |
| 1261 | } |
| 1262 | |
| 1263 | xbuf[xout] = '\0'; |
| 1264 | *xlen = xout - sizeof(PL_collation_ix); |
| 1265 | return xbuf; |
| 1266 | |
| 1267 | bad: |
| 1268 | Safefree(xbuf); |
| 1269 | *xlen = 0; |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | |
| 1273 | #endif /* USE_LOCALE_COLLATE */ |
| 1274 | |
| 1275 | #ifdef USE_LOCALE |
| 1276 | |
| 1277 | bool |
| 1278 | Perl__is_cur_LC_category_utf8(pTHX_ int category) |
| 1279 | { |
| 1280 | /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE |
| 1281 | * otherwise. 'category' may not be LC_ALL. If the platform doesn't have |
| 1282 | * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence |
| 1283 | * could give the wrong result. The result will very likely be correct for |
| 1284 | * languages that have commonly used non-ASCII characters, but for notably |
| 1285 | * English, it comes down to if the locale's name ends in something like |
| 1286 | * "UTF-8". It errs on the side of not being a UTF-8 locale. */ |
| 1287 | |
| 1288 | char *save_input_locale = NULL; |
| 1289 | STRLEN final_pos; |
| 1290 | |
| 1291 | #ifdef LC_ALL |
| 1292 | assert(category != LC_ALL); |
| 1293 | #endif |
| 1294 | |
| 1295 | /* First dispose of the trivial cases */ |
| 1296 | save_input_locale = setlocale(category, NULL); |
| 1297 | if (! save_input_locale) { |
| 1298 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1299 | "Could not find current locale for category %d\n", |
| 1300 | category)); |
| 1301 | return FALSE; /* XXX maybe should croak */ |
| 1302 | } |
| 1303 | save_input_locale = stdize_locale(savepv(save_input_locale)); |
| 1304 | if (isNAME_C_OR_POSIX(save_input_locale)) { |
| 1305 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1306 | "Current locale for category %d is %s\n", |
| 1307 | category, save_input_locale)); |
| 1308 | Safefree(save_input_locale); |
| 1309 | return FALSE; |
| 1310 | } |
| 1311 | |
| 1312 | #if defined(USE_LOCALE_CTYPE) \ |
| 1313 | && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET))) |
| 1314 | |
| 1315 | { /* Next try nl_langinfo or MB_CUR_MAX if available */ |
| 1316 | |
| 1317 | char *save_ctype_locale = NULL; |
| 1318 | bool is_utf8; |
| 1319 | |
| 1320 | if (category != LC_CTYPE) { /* These work only on LC_CTYPE */ |
| 1321 | |
| 1322 | /* Get the current LC_CTYPE locale */ |
| 1323 | save_ctype_locale = setlocale(LC_CTYPE, NULL); |
| 1324 | if (! save_ctype_locale) { |
| 1325 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1326 | "Could not find current locale for LC_CTYPE\n")); |
| 1327 | goto cant_use_nllanginfo; |
| 1328 | } |
| 1329 | save_ctype_locale = stdize_locale(savepv(save_ctype_locale)); |
| 1330 | |
| 1331 | /* If LC_CTYPE and the desired category use the same locale, this |
| 1332 | * means that finding the value for LC_CTYPE is the same as finding |
| 1333 | * the value for the desired category. Otherwise, switch LC_CTYPE |
| 1334 | * to the desired category's locale */ |
| 1335 | if (strEQ(save_ctype_locale, save_input_locale)) { |
| 1336 | Safefree(save_ctype_locale); |
| 1337 | save_ctype_locale = NULL; |
| 1338 | } |
| 1339 | else if (! setlocale(LC_CTYPE, save_input_locale)) { |
| 1340 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1341 | "Could not change LC_CTYPE locale to %s\n", |
| 1342 | save_input_locale)); |
| 1343 | Safefree(save_ctype_locale); |
| 1344 | goto cant_use_nllanginfo; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n", |
| 1349 | save_input_locale)); |
| 1350 | |
| 1351 | /* Here the current LC_CTYPE is set to the locale of the category whose |
| 1352 | * information is desired. This means that nl_langinfo() and MB_CUR_MAX |
| 1353 | * should give the correct results */ |
| 1354 | |
| 1355 | # if defined(HAS_NL_LANGINFO) && defined(CODESET) |
| 1356 | { |
| 1357 | char *codeset = nl_langinfo(CODESET); |
| 1358 | if (codeset && strNE(codeset, "")) { |
| 1359 | codeset = savepv(codeset); |
| 1360 | |
| 1361 | /* If we switched LC_CTYPE, switch back */ |
| 1362 | if (save_ctype_locale) { |
| 1363 | setlocale(LC_CTYPE, save_ctype_locale); |
| 1364 | Safefree(save_ctype_locale); |
| 1365 | } |
| 1366 | |
| 1367 | is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8")) |
| 1368 | || foldEQ(codeset, STR_WITH_LEN("UTF8")); |
| 1369 | |
| 1370 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1371 | "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n", |
| 1372 | codeset, is_utf8)); |
| 1373 | Safefree(codeset); |
| 1374 | Safefree(save_input_locale); |
| 1375 | return is_utf8; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | # endif |
| 1380 | # ifdef MB_CUR_MAX |
| 1381 | |
| 1382 | /* Here, either we don't have nl_langinfo, or it didn't return a |
| 1383 | * codeset. Try MB_CUR_MAX */ |
| 1384 | |
| 1385 | /* Standard UTF-8 needs at least 4 bytes to represent the maximum |
| 1386 | * Unicode code point. Since UTF-8 is the only non-single byte |
| 1387 | * encoding we handle, we just say any such encoding is UTF-8, and if |
| 1388 | * turns out to be wrong, other things will fail */ |
| 1389 | is_utf8 = MB_CUR_MAX >= 4; |
| 1390 | |
| 1391 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1392 | "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n", |
| 1393 | (int) MB_CUR_MAX, is_utf8)); |
| 1394 | |
| 1395 | Safefree(save_input_locale); |
| 1396 | |
| 1397 | # ifdef HAS_MBTOWC |
| 1398 | |
| 1399 | /* ... But, most system that have MB_CUR_MAX will also have mbtowc(), |
| 1400 | * since they are both in the C99 standard. We can feed a known byte |
| 1401 | * string to the latter function, and check that it gives the expected |
| 1402 | * result */ |
| 1403 | if (is_utf8) { |
| 1404 | wchar_t wc; |
| 1405 | PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */ |
| 1406 | errno = 0; |
| 1407 | if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)) |
| 1408 | != strlen(HYPHEN_UTF8) |
| 1409 | || wc != (wchar_t) 0x2010) |
| 1410 | { |
| 1411 | is_utf8 = FALSE; |
| 1412 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", (unsigned int)wc)); |
| 1413 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1414 | "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n", |
| 1415 | mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno)); |
| 1416 | } |
| 1417 | } |
| 1418 | # endif |
| 1419 | |
| 1420 | /* If we switched LC_CTYPE, switch back */ |
| 1421 | if (save_ctype_locale) { |
| 1422 | setlocale(LC_CTYPE, save_ctype_locale); |
| 1423 | Safefree(save_ctype_locale); |
| 1424 | } |
| 1425 | |
| 1426 | return is_utf8; |
| 1427 | # endif |
| 1428 | } |
| 1429 | |
| 1430 | cant_use_nllanginfo: |
| 1431 | |
| 1432 | #else /* nl_langinfo should work if available, so don't bother compiling this |
| 1433 | fallback code. The final fallback of looking at the name is |
| 1434 | compiled, and will be executed if nl_langinfo fails */ |
| 1435 | |
| 1436 | /* nl_langinfo not available or failed somehow. Next try looking at the |
| 1437 | * currency symbol to see if it disambiguates things. Often that will be |
| 1438 | * in the native script, and if the symbol isn't in UTF-8, we know that the |
| 1439 | * locale isn't. If it is non-ASCII UTF-8, we infer that the locale is |
| 1440 | * too, as the odds of a non-UTF8 string being valid UTF-8 are quite small |
| 1441 | * */ |
| 1442 | |
| 1443 | #ifdef HAS_LOCALECONV |
| 1444 | # ifdef USE_LOCALE_MONETARY |
| 1445 | { |
| 1446 | char *save_monetary_locale = NULL; |
| 1447 | bool only_ascii = FALSE; |
| 1448 | bool is_utf8 = FALSE; |
| 1449 | struct lconv* lc; |
| 1450 | |
| 1451 | /* Like above for LC_CTYPE, we first set LC_MONETARY to the locale of |
| 1452 | * the desired category, if it isn't that locale already */ |
| 1453 | |
| 1454 | if (category != LC_MONETARY) { |
| 1455 | |
| 1456 | save_monetary_locale = setlocale(LC_MONETARY, NULL); |
| 1457 | if (! save_monetary_locale) { |
| 1458 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1459 | "Could not find current locale for LC_MONETARY\n")); |
| 1460 | goto cant_use_monetary; |
| 1461 | } |
| 1462 | save_monetary_locale = stdize_locale(savepv(save_monetary_locale)); |
| 1463 | |
| 1464 | if (strEQ(save_monetary_locale, save_input_locale)) { |
| 1465 | Safefree(save_monetary_locale); |
| 1466 | save_monetary_locale = NULL; |
| 1467 | } |
| 1468 | else if (! setlocale(LC_MONETARY, save_input_locale)) { |
| 1469 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1470 | "Could not change LC_MONETARY locale to %s\n", |
| 1471 | save_input_locale)); |
| 1472 | Safefree(save_monetary_locale); |
| 1473 | goto cant_use_monetary; |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | /* Here the current LC_MONETARY is set to the locale of the category |
| 1478 | * whose information is desired. */ |
| 1479 | |
| 1480 | lc = localeconv(); |
| 1481 | if (! lc |
| 1482 | || ! lc->currency_symbol |
| 1483 | || is_invariant_string((U8 *) lc->currency_symbol, 0)) |
| 1484 | { |
| 1485 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Couldn't get currency symbol for %s, or contains only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale)); |
| 1486 | only_ascii = TRUE; |
| 1487 | } |
| 1488 | else { |
| 1489 | is_utf8 = is_utf8_string((U8 *) lc->currency_symbol, 0); |
| 1490 | } |
| 1491 | |
| 1492 | /* If we changed it, restore LC_MONETARY to its original locale */ |
| 1493 | if (save_monetary_locale) { |
| 1494 | setlocale(LC_MONETARY, save_monetary_locale); |
| 1495 | Safefree(save_monetary_locale); |
| 1496 | } |
| 1497 | |
| 1498 | if (! only_ascii) { |
| 1499 | |
| 1500 | /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; |
| 1501 | * otherwise assume the locale is UTF-8 if and only if the symbol |
| 1502 | * is non-ascii UTF-8. */ |
| 1503 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?Currency symbol for %s is UTF-8=%d\n", |
| 1504 | save_input_locale, is_utf8)); |
| 1505 | Safefree(save_input_locale); |
| 1506 | return is_utf8; |
| 1507 | } |
| 1508 | } |
| 1509 | cant_use_monetary: |
| 1510 | |
| 1511 | # endif /* USE_LOCALE_MONETARY */ |
| 1512 | #endif /* HAS_LOCALECONV */ |
| 1513 | |
| 1514 | #if defined(HAS_STRFTIME) && defined(USE_LOCALE_TIME) |
| 1515 | |
| 1516 | /* Still haven't found a non-ASCII string to disambiguate UTF-8 or not. Try |
| 1517 | * the names of the months and weekdays, timezone, and am/pm indicator */ |
| 1518 | { |
| 1519 | char *save_time_locale = NULL; |
| 1520 | int hour = 10; |
| 1521 | bool is_dst = FALSE; |
| 1522 | int dom = 1; |
| 1523 | int month = 0; |
| 1524 | int i; |
| 1525 | char * formatted_time; |
| 1526 | |
| 1527 | |
| 1528 | /* Like above for LC_MONETARY, we set LC_TIME to the locale of the |
| 1529 | * desired category, if it isn't that locale already */ |
| 1530 | |
| 1531 | if (category != LC_TIME) { |
| 1532 | |
| 1533 | save_time_locale = setlocale(LC_TIME, NULL); |
| 1534 | if (! save_time_locale) { |
| 1535 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1536 | "Could not find current locale for LC_TIME\n")); |
| 1537 | goto cant_use_time; |
| 1538 | } |
| 1539 | save_time_locale = stdize_locale(savepv(save_time_locale)); |
| 1540 | |
| 1541 | if (strEQ(save_time_locale, save_input_locale)) { |
| 1542 | Safefree(save_time_locale); |
| 1543 | save_time_locale = NULL; |
| 1544 | } |
| 1545 | else if (! setlocale(LC_TIME, save_input_locale)) { |
| 1546 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1547 | "Could not change LC_TIME locale to %s\n", |
| 1548 | save_input_locale)); |
| 1549 | Safefree(save_time_locale); |
| 1550 | goto cant_use_time; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | /* Here the current LC_TIME is set to the locale of the category |
| 1555 | * whose information is desired. Look at all the days of the week and |
| 1556 | * month names, and the timezone and am/pm indicator for UTF-8 variant |
| 1557 | * characters. The first such a one found will tell us if the locale |
| 1558 | * is UTF-8 or not */ |
| 1559 | |
| 1560 | for (i = 0; i < 7 + 12; i++) { /* 7 days; 12 months */ |
| 1561 | formatted_time = my_strftime("%A %B %Z %p", |
| 1562 | 0, 0, hour, dom, month, 112, 0, 0, is_dst); |
| 1563 | if (! formatted_time || is_invariant_string((U8 *) formatted_time, 0)) { |
| 1564 | |
| 1565 | /* Here, we didn't find a non-ASCII. Try the next time through |
| 1566 | * with the complemented dst and am/pm, and try with the next |
| 1567 | * weekday. After we have gotten all weekdays, try the next |
| 1568 | * month */ |
| 1569 | is_dst = ! is_dst; |
| 1570 | hour = (hour + 12) % 24; |
| 1571 | dom++; |
| 1572 | if (i > 6) { |
| 1573 | month++; |
| 1574 | } |
| 1575 | continue; |
| 1576 | } |
| 1577 | |
| 1578 | /* Here, we have a non-ASCII. Return TRUE is it is valid UTF8; |
| 1579 | * false otherwise. But first, restore LC_TIME to its original |
| 1580 | * locale if we changed it */ |
| 1581 | if (save_time_locale) { |
| 1582 | setlocale(LC_TIME, save_time_locale); |
| 1583 | Safefree(save_time_locale); |
| 1584 | } |
| 1585 | |
| 1586 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?time-related strings for %s are UTF-8=%d\n", |
| 1587 | save_input_locale, |
| 1588 | is_utf8_string((U8 *) formatted_time, 0))); |
| 1589 | Safefree(save_input_locale); |
| 1590 | return is_utf8_string((U8 *) formatted_time, 0); |
| 1591 | } |
| 1592 | |
| 1593 | /* Falling off the end of the loop indicates all the names were just |
| 1594 | * ASCII. Go on to the next test. If we changed it, restore LC_TIME |
| 1595 | * to its original locale */ |
| 1596 | if (save_time_locale) { |
| 1597 | setlocale(LC_TIME, save_time_locale); |
| 1598 | Safefree(save_time_locale); |
| 1599 | } |
| 1600 | DEBUG_L(PerlIO_printf(Perl_debug_log, "All time-related words for %s contain only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale)); |
| 1601 | } |
| 1602 | cant_use_time: |
| 1603 | |
| 1604 | #endif |
| 1605 | |
| 1606 | #if 0 && defined(USE_LOCALE_MESSAGES) && defined(HAS_SYS_ERRLIST) |
| 1607 | |
| 1608 | /* This code is ifdefd out because it was found to not be necessary in testing |
| 1609 | * on our dromedary test machine, which has over 700 locales. There, this |
| 1610 | * added no value to looking at the currency symbol and the time strings. I |
| 1611 | * left it in so as to avoid rewriting it if real-world experience indicates |
| 1612 | * that dromedary is an outlier. Essentially, instead of returning abpve if we |
| 1613 | * haven't found illegal utf8, we continue on and examine all the strerror() |
| 1614 | * messages on the platform for utf8ness. If all are ASCII, we still don't |
| 1615 | * know the answer; but otherwise we have a pretty good indication of the |
| 1616 | * utf8ness. The reason this doesn't help much is that the messages may not |
| 1617 | * have been translated into the locale. The currency symbol and time strings |
| 1618 | * are much more likely to have been translated. */ |
| 1619 | { |
| 1620 | int e; |
| 1621 | bool is_utf8 = FALSE; |
| 1622 | bool non_ascii = FALSE; |
| 1623 | char *save_messages_locale = NULL; |
| 1624 | const char * errmsg = NULL; |
| 1625 | |
| 1626 | /* Like above, we set LC_MESSAGES to the locale of the desired |
| 1627 | * category, if it isn't that locale already */ |
| 1628 | |
| 1629 | if (category != LC_MESSAGES) { |
| 1630 | |
| 1631 | save_messages_locale = setlocale(LC_MESSAGES, NULL); |
| 1632 | if (! save_messages_locale) { |
| 1633 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1634 | "Could not find current locale for LC_MESSAGES\n")); |
| 1635 | goto cant_use_messages; |
| 1636 | } |
| 1637 | save_messages_locale = stdize_locale(savepv(save_messages_locale)); |
| 1638 | |
| 1639 | if (strEQ(save_messages_locale, save_input_locale)) { |
| 1640 | Safefree(save_messages_locale); |
| 1641 | save_messages_locale = NULL; |
| 1642 | } |
| 1643 | else if (! setlocale(LC_MESSAGES, save_input_locale)) { |
| 1644 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1645 | "Could not change LC_MESSAGES locale to %s\n", |
| 1646 | save_input_locale)); |
| 1647 | Safefree(save_messages_locale); |
| 1648 | goto cant_use_messages; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | /* Here the current LC_MESSAGES is set to the locale of the category |
| 1653 | * whose information is desired. Look through all the messages. We |
| 1654 | * can't use Strerror() here because it may expand to code that |
| 1655 | * segfaults in miniperl */ |
| 1656 | |
| 1657 | for (e = 0; e <= sys_nerr; e++) { |
| 1658 | errno = 0; |
| 1659 | errmsg = sys_errlist[e]; |
| 1660 | if (errno || !errmsg) { |
| 1661 | break; |
| 1662 | } |
| 1663 | errmsg = savepv(errmsg); |
| 1664 | if (! is_invariant_string((U8 *) errmsg, 0)) { |
| 1665 | non_ascii = TRUE; |
| 1666 | is_utf8 = is_utf8_string((U8 *) errmsg, 0); |
| 1667 | break; |
| 1668 | } |
| 1669 | } |
| 1670 | Safefree(errmsg); |
| 1671 | |
| 1672 | /* And, if we changed it, restore LC_MESSAGES to its original locale */ |
| 1673 | if (save_messages_locale) { |
| 1674 | setlocale(LC_MESSAGES, save_messages_locale); |
| 1675 | Safefree(save_messages_locale); |
| 1676 | } |
| 1677 | |
| 1678 | if (non_ascii) { |
| 1679 | |
| 1680 | /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid, |
| 1681 | * any non-ascii means it is one; otherwise we assume it isn't */ |
| 1682 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?error messages for %s are UTF-8=%d\n", |
| 1683 | save_input_locale, |
| 1684 | is_utf8)); |
| 1685 | Safefree(save_input_locale); |
| 1686 | return is_utf8; |
| 1687 | } |
| 1688 | |
| 1689 | DEBUG_L(PerlIO_printf(Perl_debug_log, "All error messages for %s contain only ASCII; can't use for determining if UTF-8 locale\n", save_input_locale)); |
| 1690 | } |
| 1691 | cant_use_messages: |
| 1692 | |
| 1693 | #endif |
| 1694 | |
| 1695 | #endif /* the code that is compiled when no nl_langinfo */ |
| 1696 | |
| 1697 | #ifndef EBCDIC /* On os390, even if the name ends with "UTF-8', it isn't a |
| 1698 | UTF-8 locale */ |
| 1699 | /* As a last resort, look at the locale name to see if it matches |
| 1700 | * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the |
| 1701 | * return of setlocale(), is actually defined to be opaque, so we can't |
| 1702 | * really rely on the absence of various substrings in the name to indicate |
| 1703 | * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to |
| 1704 | * be a UTF-8 locale. Similarly for the other common names */ |
| 1705 | |
| 1706 | final_pos = strlen(save_input_locale) - 1; |
| 1707 | if (final_pos >= 3) { |
| 1708 | char *name = save_input_locale; |
| 1709 | |
| 1710 | /* Find next 'U' or 'u' and look from there */ |
| 1711 | while ((name += strcspn(name, "Uu") + 1) |
| 1712 | <= save_input_locale + final_pos - 2) |
| 1713 | { |
| 1714 | if (!isALPHA_FOLD_NE(*name, 't') |
| 1715 | || isALPHA_FOLD_NE(*(name + 1), 'f')) |
| 1716 | { |
| 1717 | continue; |
| 1718 | } |
| 1719 | name += 2; |
| 1720 | if (*(name) == '-') { |
| 1721 | if ((name > save_input_locale + final_pos - 1)) { |
| 1722 | break; |
| 1723 | } |
| 1724 | name++; |
| 1725 | } |
| 1726 | if (*(name) == '8') { |
| 1727 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1728 | "Locale %s ends with UTF-8 in name\n", |
| 1729 | save_input_locale)); |
| 1730 | Safefree(save_input_locale); |
| 1731 | return TRUE; |
| 1732 | } |
| 1733 | } |
| 1734 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1735 | "Locale %s doesn't end with UTF-8 in name\n", |
| 1736 | save_input_locale)); |
| 1737 | } |
| 1738 | #endif |
| 1739 | |
| 1740 | #ifdef WIN32 |
| 1741 | /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */ |
| 1742 | if (final_pos >= 4 |
| 1743 | && *(save_input_locale + final_pos - 0) == '1' |
| 1744 | && *(save_input_locale + final_pos - 1) == '0' |
| 1745 | && *(save_input_locale + final_pos - 2) == '0' |
| 1746 | && *(save_input_locale + final_pos - 3) == '5' |
| 1747 | && *(save_input_locale + final_pos - 4) == '6') |
| 1748 | { |
| 1749 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1750 | "Locale %s ends with 10056 in name, is UTF-8 locale\n", |
| 1751 | save_input_locale)); |
| 1752 | Safefree(save_input_locale); |
| 1753 | return TRUE; |
| 1754 | } |
| 1755 | #endif |
| 1756 | |
| 1757 | /* Other common encodings are the ISO 8859 series, which aren't UTF-8. But |
| 1758 | * since we are about to return FALSE anyway, there is no point in doing |
| 1759 | * this extra work */ |
| 1760 | #if 0 |
| 1761 | if (instr(save_input_locale, "8859")) { |
| 1762 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1763 | "Locale %s has 8859 in name, not UTF-8 locale\n", |
| 1764 | save_input_locale)); |
| 1765 | Safefree(save_input_locale); |
| 1766 | return FALSE; |
| 1767 | } |
| 1768 | #endif |
| 1769 | |
| 1770 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1771 | "Assuming locale %s is not a UTF-8 locale\n", |
| 1772 | save_input_locale)); |
| 1773 | Safefree(save_input_locale); |
| 1774 | return FALSE; |
| 1775 | } |
| 1776 | |
| 1777 | #endif |
| 1778 | |
| 1779 | |
| 1780 | bool |
| 1781 | Perl__is_in_locale_category(pTHX_ const bool compiling, const int category) |
| 1782 | { |
| 1783 | dVAR; |
| 1784 | /* Internal function which returns if we are in the scope of a pragma that |
| 1785 | * enables the locale category 'category'. 'compiling' should indicate if |
| 1786 | * this is during the compilation phase (TRUE) or not (FALSE). */ |
| 1787 | |
| 1788 | const COP * const cop = (compiling) ? &PL_compiling : PL_curcop; |
| 1789 | |
| 1790 | SV *categories = cop_hints_fetch_pvs(cop, "locale", 0); |
| 1791 | if (! categories || categories == &PL_sv_placeholder) { |
| 1792 | return FALSE; |
| 1793 | } |
| 1794 | |
| 1795 | /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get |
| 1796 | * a valid unsigned */ |
| 1797 | assert(category >= -1); |
| 1798 | return cBOOL(SvUV(categories) & (1U << (category + 1))); |
| 1799 | } |
| 1800 | |
| 1801 | char * |
| 1802 | Perl_my_strerror(pTHX_ const int errnum) { |
| 1803 | |
| 1804 | /* Uses C locale for the error text unless within scope of 'use locale' for |
| 1805 | * LC_MESSAGES */ |
| 1806 | |
| 1807 | #ifdef USE_LOCALE_MESSAGES |
| 1808 | if (! IN_LC(LC_MESSAGES)) { |
| 1809 | char * save_locale = setlocale(LC_MESSAGES, NULL); |
| 1810 | if (! isNAME_C_OR_POSIX(save_locale)) { |
| 1811 | char *errstr; |
| 1812 | |
| 1813 | /* The next setlocale likely will zap this, so create a copy */ |
| 1814 | save_locale = savepv(save_locale); |
| 1815 | |
| 1816 | setlocale(LC_MESSAGES, "C"); |
| 1817 | |
| 1818 | /* This points to the static space in Strerror, with all its |
| 1819 | * limitations */ |
| 1820 | errstr = Strerror(errnum); |
| 1821 | |
| 1822 | setlocale(LC_MESSAGES, save_locale); |
| 1823 | Safefree(save_locale); |
| 1824 | return errstr; |
| 1825 | } |
| 1826 | } |
| 1827 | #endif |
| 1828 | |
| 1829 | return Strerror(errnum); |
| 1830 | } |
| 1831 | |
| 1832 | /* |
| 1833 | |
| 1834 | =head1 Locale-related functions and macros |
| 1835 | |
| 1836 | =for apidoc sync_locale |
| 1837 | |
| 1838 | Changing the program's locale should be avoided by XS code. Nevertheless, |
| 1839 | certain non-Perl libraries called from XS, such as C<Gtk> do so. When this |
| 1840 | happens, Perl needs to be told that the locale has changed. Use this function |
| 1841 | to do so, before returning to Perl. |
| 1842 | |
| 1843 | =cut |
| 1844 | */ |
| 1845 | |
| 1846 | void |
| 1847 | Perl_sync_locale(pTHX) |
| 1848 | { |
| 1849 | |
| 1850 | #ifdef USE_LOCALE_CTYPE |
| 1851 | new_ctype(setlocale(LC_CTYPE, NULL)); |
| 1852 | #endif /* USE_LOCALE_CTYPE */ |
| 1853 | |
| 1854 | #ifdef USE_LOCALE_COLLATE |
| 1855 | new_collate(setlocale(LC_COLLATE, NULL)); |
| 1856 | #endif |
| 1857 | |
| 1858 | #ifdef USE_LOCALE_NUMERIC |
| 1859 | set_numeric_local(); /* Switch from "C" to underlying LC_NUMERIC */ |
| 1860 | new_numeric(setlocale(LC_NUMERIC, NULL)); |
| 1861 | #endif /* USE_LOCALE_NUMERIC */ |
| 1862 | |
| 1863 | } |
| 1864 | |
| 1865 | #if defined(DEBUGGING) && defined(USE_LOCALE) |
| 1866 | |
| 1867 | char * |
| 1868 | Perl__setlocale_debug_string(const int category, /* category number, |
| 1869 | like LC_ALL */ |
| 1870 | const char* const locale, /* locale name */ |
| 1871 | |
| 1872 | /* return value from setlocale() when attempting to |
| 1873 | * set 'category' to 'locale' */ |
| 1874 | const char* const retval) |
| 1875 | { |
| 1876 | /* Returns a pointer to a NUL-terminated string in static storage with |
| 1877 | * added text about the info passed in. This is not thread safe and will |
| 1878 | * be overwritten by the next call, so this should be used just to |
| 1879 | * formulate a string to immediately print or savepv() on. */ |
| 1880 | |
| 1881 | static char ret[128] = ""; |
| 1882 | |
| 1883 | my_strlcpy(ret, "setlocale(", sizeof(ret)); |
| 1884 | |
| 1885 | switch (category) { |
| 1886 | default: |
| 1887 | my_snprintf(ret, sizeof(ret), "%s? %d", ret, category); |
| 1888 | break; |
| 1889 | # ifdef LC_ALL |
| 1890 | case LC_ALL: |
| 1891 | my_strlcat(ret, "LC_ALL", sizeof(ret)); |
| 1892 | break; |
| 1893 | # endif |
| 1894 | # ifdef LC_CTYPE |
| 1895 | case LC_CTYPE: |
| 1896 | my_strlcat(ret, "LC_CTYPE", sizeof(ret)); |
| 1897 | break; |
| 1898 | # endif |
| 1899 | # ifdef LC_NUMERIC |
| 1900 | case LC_NUMERIC: |
| 1901 | my_strlcat(ret, "LC_NUMERIC", sizeof(ret)); |
| 1902 | break; |
| 1903 | # endif |
| 1904 | # ifdef LC_COLLATE |
| 1905 | case LC_COLLATE: |
| 1906 | my_strlcat(ret, "LC_COLLATE", sizeof(ret)); |
| 1907 | break; |
| 1908 | # endif |
| 1909 | # ifdef LC_TIME |
| 1910 | case LC_TIME: |
| 1911 | my_strlcat(ret, "LC_TIME", sizeof(ret)); |
| 1912 | break; |
| 1913 | # endif |
| 1914 | # ifdef LC_MONETARY |
| 1915 | case LC_MONETARY: |
| 1916 | my_strlcat(ret, "LC_MONETARY", sizeof(ret)); |
| 1917 | break; |
| 1918 | # endif |
| 1919 | # ifdef LC_MESSAGES |
| 1920 | case LC_MESSAGES: |
| 1921 | my_strlcat(ret, "LC_MESSAGES", sizeof(ret)); |
| 1922 | break; |
| 1923 | # endif |
| 1924 | } |
| 1925 | |
| 1926 | my_strlcat(ret, ", ", sizeof(ret)); |
| 1927 | |
| 1928 | if (locale) { |
| 1929 | my_strlcat(ret, "\"", sizeof(ret)); |
| 1930 | my_strlcat(ret, locale, sizeof(ret)); |
| 1931 | my_strlcat(ret, "\"", sizeof(ret)); |
| 1932 | } |
| 1933 | else { |
| 1934 | my_strlcat(ret, "NULL", sizeof(ret)); |
| 1935 | } |
| 1936 | |
| 1937 | my_strlcat(ret, ") returned ", sizeof(ret)); |
| 1938 | |
| 1939 | if (retval) { |
| 1940 | my_strlcat(ret, "\"", sizeof(ret)); |
| 1941 | my_strlcat(ret, retval, sizeof(ret)); |
| 1942 | my_strlcat(ret, "\"", sizeof(ret)); |
| 1943 | } |
| 1944 | else { |
| 1945 | my_strlcat(ret, "NULL", sizeof(ret)); |
| 1946 | } |
| 1947 | |
| 1948 | assert(strlen(ret) < sizeof(ret)); |
| 1949 | |
| 1950 | return ret; |
| 1951 | } |
| 1952 | |
| 1953 | #endif |
| 1954 | |
| 1955 | |
| 1956 | /* |
| 1957 | * ex: set ts=8 sts=4 sw=4 et: |
| 1958 | */ |