| 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. And, LC_MESSAGES is |
| 33 | * switched to the C locale for outputting the message unless within the scope |
| 34 | * of 'use locale'. |
| 35 | */ |
| 36 | |
| 37 | #include "EXTERN.h" |
| 38 | #define PERL_IN_LOCALE_C |
| 39 | #include "perl.h" |
| 40 | |
| 41 | #ifdef I_LANGINFO |
| 42 | # include <langinfo.h> |
| 43 | #endif |
| 44 | |
| 45 | #include "reentr.h" |
| 46 | |
| 47 | /* If the environment says to, we can output debugging information during |
| 48 | * initialization. This is done before option parsing, and before any thread |
| 49 | * creation, so can be a file-level static */ |
| 50 | #ifdef DEBUGGING |
| 51 | # ifdef PERL_GLOBAL_STRUCT |
| 52 | /* no global syms allowed */ |
| 53 | # define debug_initialization 0 |
| 54 | # define DEBUG_INITIALIZATION_set(v) |
| 55 | # else |
| 56 | static bool debug_initialization = FALSE; |
| 57 | # define DEBUG_INITIALIZATION_set(v) (debug_initialization = v) |
| 58 | # endif |
| 59 | #endif |
| 60 | |
| 61 | #ifdef USE_LOCALE |
| 62 | |
| 63 | /* |
| 64 | * Standardize the locale name from a string returned by 'setlocale', possibly |
| 65 | * modifying that string. |
| 66 | * |
| 67 | * The typical return value of setlocale() is either |
| 68 | * (1) "xx_YY" if the first argument of setlocale() is not LC_ALL |
| 69 | * (2) "xa_YY xb_YY ..." if the first argument of setlocale() is LC_ALL |
| 70 | * (the space-separated values represent the various sublocales, |
| 71 | * in some unspecified order). This is not handled by this function. |
| 72 | * |
| 73 | * In some platforms it has a form like "LC_SOMETHING=Lang_Country.866\n", |
| 74 | * which is harmful for further use of the string in setlocale(). This |
| 75 | * function removes the trailing new line and everything up through the '=' |
| 76 | * |
| 77 | */ |
| 78 | STATIC char * |
| 79 | S_stdize_locale(pTHX_ char *locs) |
| 80 | { |
| 81 | const char * const s = strchr(locs, '='); |
| 82 | bool okay = TRUE; |
| 83 | |
| 84 | PERL_ARGS_ASSERT_STDIZE_LOCALE; |
| 85 | |
| 86 | if (s) { |
| 87 | const char * const t = strchr(s, '.'); |
| 88 | okay = FALSE; |
| 89 | if (t) { |
| 90 | const char * const u = strchr(t, '\n'); |
| 91 | if (u && (u[1] == 0)) { |
| 92 | const STRLEN len = u - s; |
| 93 | Move(s + 1, locs, len, char); |
| 94 | locs[len] = 0; |
| 95 | okay = TRUE; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (!okay) |
| 101 | Perl_croak(aTHX_ "Can't fix broken locale name \"%s\"", locs); |
| 102 | |
| 103 | return locs; |
| 104 | } |
| 105 | |
| 106 | #endif |
| 107 | |
| 108 | void |
| 109 | Perl_set_numeric_radix(pTHX) |
| 110 | { |
| 111 | #ifdef USE_LOCALE_NUMERIC |
| 112 | # ifdef HAS_LOCALECONV |
| 113 | const struct lconv* const lc = localeconv(); |
| 114 | |
| 115 | if (lc && lc->decimal_point) { |
| 116 | if (lc->decimal_point[0] == '.' && lc->decimal_point[1] == 0) { |
| 117 | SvREFCNT_dec(PL_numeric_radix_sv); |
| 118 | PL_numeric_radix_sv = NULL; |
| 119 | } |
| 120 | else { |
| 121 | if (PL_numeric_radix_sv) |
| 122 | sv_setpv(PL_numeric_radix_sv, lc->decimal_point); |
| 123 | else |
| 124 | PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0); |
| 125 | if (! is_utf8_invariant_string((U8 *) lc->decimal_point, 0) |
| 126 | && is_utf8_string((U8 *) lc->decimal_point, 0) |
| 127 | && _is_cur_LC_category_utf8(LC_NUMERIC)) |
| 128 | { |
| 129 | SvUTF8_on(PL_numeric_radix_sv); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | PL_numeric_radix_sv = NULL; |
| 135 | |
| 136 | #ifdef DEBUGGING |
| 137 | if (DEBUG_L_TEST || debug_initialization) { |
| 138 | PerlIO_printf(Perl_debug_log, "Locale radix is '%s', ?UTF-8=%d\n", |
| 139 | (PL_numeric_radix_sv) |
| 140 | ? SvPVX(PL_numeric_radix_sv) |
| 141 | : "NULL", |
| 142 | (PL_numeric_radix_sv) |
| 143 | ? cBOOL(SvUTF8(PL_numeric_radix_sv)) |
| 144 | : 0); |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | # endif /* HAS_LOCALECONV */ |
| 149 | #endif /* USE_LOCALE_NUMERIC */ |
| 150 | } |
| 151 | |
| 152 | /* Is the C string input 'name' "C" or "POSIX"? If so, and 'name' is the |
| 153 | * return of setlocale(), then this is extremely likely to be the C or POSIX |
| 154 | * locale. However, the output of setlocale() is documented to be opaque, but |
| 155 | * the odds are extremely small that it would return these two strings for some |
| 156 | * other locale. Note that VMS in these two locales includes many non-ASCII |
| 157 | * characters as controls and punctuation (below are hex bytes): |
| 158 | * cntrl: 00-1F 7F 84-97 9B-9F |
| 159 | * 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 |
| 160 | * Oddly, none there are listed as alphas, though some represent alphabetics |
| 161 | * http://www.nntp.perl.org/group/perl.perl5.porters/2013/02/msg198753.html */ |
| 162 | #define isNAME_C_OR_POSIX(name) ((name) != NULL \ |
| 163 | && ((*(name) == 'C' && (*(name + 1)) == '\0') \ |
| 164 | || strEQ((name), "POSIX"))) |
| 165 | |
| 166 | void |
| 167 | Perl_new_numeric(pTHX_ const char *newnum) |
| 168 | { |
| 169 | #ifdef USE_LOCALE_NUMERIC |
| 170 | |
| 171 | /* Called after all libc setlocale() calls affecting LC_NUMERIC, to tell |
| 172 | * core Perl this and that 'newnum' is the name of the new locale. |
| 173 | * It installs this locale as the current underlying default. |
| 174 | * |
| 175 | * The default locale and the C locale can be toggled between by use of the |
| 176 | * set_numeric_local() and set_numeric_standard() functions, which should |
| 177 | * probably not be called directly, but only via macros like |
| 178 | * SET_NUMERIC_STANDARD() in perl.h. |
| 179 | * |
| 180 | * The toggling is necessary mainly so that a non-dot radix decimal point |
| 181 | * character can be output, while allowing internal calculations to use a |
| 182 | * dot. |
| 183 | * |
| 184 | * This sets several interpreter-level variables: |
| 185 | * PL_numeric_name The underlying locale's name: a copy of 'newnum' |
| 186 | * PL_numeric_local A boolean indicating if the toggled state is such |
| 187 | * that the current locale is the program's underlying |
| 188 | * locale |
| 189 | * PL_numeric_standard An int indicating if the toggled state is such |
| 190 | * that the current locale is the C locale. If non-zero, |
| 191 | * it is in C; if > 1, it means it may not be toggled away |
| 192 | * from C. |
| 193 | * Note that both of the last two variables can be true at the same time, |
| 194 | * if the underlying locale is C. (Toggling is a no-op under these |
| 195 | * circumstances.) |
| 196 | * |
| 197 | * Any code changing the locale (outside this file) should use |
| 198 | * POSIX::setlocale, which calls this function. Therefore this function |
| 199 | * should be called directly only from this file and from |
| 200 | * POSIX::setlocale() */ |
| 201 | |
| 202 | char *save_newnum; |
| 203 | |
| 204 | if (! newnum) { |
| 205 | Safefree(PL_numeric_name); |
| 206 | PL_numeric_name = NULL; |
| 207 | PL_numeric_standard = TRUE; |
| 208 | PL_numeric_local = TRUE; |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | save_newnum = stdize_locale(savepv(newnum)); |
| 213 | |
| 214 | PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum); |
| 215 | PL_numeric_local = TRUE; |
| 216 | |
| 217 | if (! PL_numeric_name || strNE(PL_numeric_name, save_newnum)) { |
| 218 | Safefree(PL_numeric_name); |
| 219 | PL_numeric_name = save_newnum; |
| 220 | } |
| 221 | else { |
| 222 | Safefree(save_newnum); |
| 223 | } |
| 224 | |
| 225 | /* Keep LC_NUMERIC in the C locale. This is for XS modules, so they don't |
| 226 | * have to worry about the radix being a non-dot. (Core operations that |
| 227 | * need the underlying locale change to it temporarily). */ |
| 228 | set_numeric_standard(); |
| 229 | |
| 230 | set_numeric_radix(); |
| 231 | |
| 232 | #else |
| 233 | PERL_UNUSED_ARG(newnum); |
| 234 | #endif /* USE_LOCALE_NUMERIC */ |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | Perl_set_numeric_standard(pTHX) |
| 239 | { |
| 240 | #ifdef USE_LOCALE_NUMERIC |
| 241 | /* Toggle the LC_NUMERIC locale to C. Most code should use the macros like |
| 242 | * SET_NUMERIC_STANDARD() in perl.h instead of calling this directly. The |
| 243 | * macro avoids calling this routine if toggling isn't necessary according |
| 244 | * to our records (which could be wrong if some XS code has changed the |
| 245 | * locale behind our back) */ |
| 246 | |
| 247 | setlocale(LC_NUMERIC, "C"); |
| 248 | PL_numeric_standard = TRUE; |
| 249 | PL_numeric_local = isNAME_C_OR_POSIX(PL_numeric_name); |
| 250 | set_numeric_radix(); |
| 251 | #ifdef DEBUGGING |
| 252 | if (DEBUG_L_TEST || debug_initialization) { |
| 253 | PerlIO_printf(Perl_debug_log, |
| 254 | "Underlying LC_NUMERIC locale now is C\n"); |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | #endif /* USE_LOCALE_NUMERIC */ |
| 259 | } |
| 260 | |
| 261 | void |
| 262 | Perl_set_numeric_local(pTHX) |
| 263 | { |
| 264 | #ifdef USE_LOCALE_NUMERIC |
| 265 | /* Toggle the LC_NUMERIC locale to the current underlying default. Most |
| 266 | * code should use the macros like SET_NUMERIC_LOCAL() in perl.h instead of |
| 267 | * calling this directly. The macro avoids calling this routine if |
| 268 | * toggling isn't necessary according to our records (which could be wrong |
| 269 | * if some XS code has changed the locale behind our back) */ |
| 270 | |
| 271 | setlocale(LC_NUMERIC, PL_numeric_name); |
| 272 | PL_numeric_standard = isNAME_C_OR_POSIX(PL_numeric_name); |
| 273 | PL_numeric_local = TRUE; |
| 274 | set_numeric_radix(); |
| 275 | #ifdef DEBUGGING |
| 276 | if (DEBUG_L_TEST || debug_initialization) { |
| 277 | PerlIO_printf(Perl_debug_log, |
| 278 | "Underlying LC_NUMERIC locale now is %s\n", |
| 279 | PL_numeric_name); |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | #endif /* USE_LOCALE_NUMERIC */ |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Set up for a new ctype locale. |
| 288 | */ |
| 289 | void |
| 290 | Perl_new_ctype(pTHX_ const char *newctype) |
| 291 | { |
| 292 | #ifdef USE_LOCALE_CTYPE |
| 293 | |
| 294 | /* Called after all libc setlocale() calls affecting LC_CTYPE, to tell |
| 295 | * core Perl this and that 'newctype' is the name of the new locale. |
| 296 | * |
| 297 | * This function sets up the folding arrays for all 256 bytes, assuming |
| 298 | * that tofold() is tolc() since fold case is not a concept in POSIX, |
| 299 | * |
| 300 | * Any code changing the locale (outside this file) should use |
| 301 | * POSIX::setlocale, which calls this function. Therefore this function |
| 302 | * should be called directly only from this file and from |
| 303 | * POSIX::setlocale() */ |
| 304 | |
| 305 | dVAR; |
| 306 | UV i; |
| 307 | |
| 308 | PERL_ARGS_ASSERT_NEW_CTYPE; |
| 309 | |
| 310 | /* We will replace any bad locale warning with 1) nothing if the new one is |
| 311 | * ok; or 2) a new warning for the bad new locale */ |
| 312 | if (PL_warn_locale) { |
| 313 | SvREFCNT_dec_NN(PL_warn_locale); |
| 314 | PL_warn_locale = NULL; |
| 315 | } |
| 316 | |
| 317 | PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE); |
| 318 | |
| 319 | /* A UTF-8 locale gets standard rules. But note that code still has to |
| 320 | * handle this specially because of the three problematic code points */ |
| 321 | if (PL_in_utf8_CTYPE_locale) { |
| 322 | Copy(PL_fold_latin1, PL_fold_locale, 256, U8); |
| 323 | } |
| 324 | else { |
| 325 | /* Assume enough space for every character being bad. 4 spaces each |
| 326 | * for the 94 printable characters that are output like "'x' "; and 5 |
| 327 | * spaces each for "'\\' ", "'\t' ", and "'\n' "; plus a terminating |
| 328 | * NUL */ |
| 329 | char bad_chars_list[ (94 * 4) + (3 * 5) + 1 ]; |
| 330 | |
| 331 | /* Don't check for problems if we are suppressing the warnings */ |
| 332 | bool check_for_problems = ckWARN_d(WARN_LOCALE) |
| 333 | || UNLIKELY(DEBUG_L_TEST); |
| 334 | bool multi_byte_locale = FALSE; /* Assume is a single-byte locale |
| 335 | to start */ |
| 336 | unsigned int bad_count = 0; /* Count of bad characters */ |
| 337 | |
| 338 | for (i = 0; i < 256; i++) { |
| 339 | if (isUPPER_LC((U8) i)) |
| 340 | PL_fold_locale[i] = (U8) toLOWER_LC((U8) i); |
| 341 | else if (isLOWER_LC((U8) i)) |
| 342 | PL_fold_locale[i] = (U8) toUPPER_LC((U8) i); |
| 343 | else |
| 344 | PL_fold_locale[i] = (U8) i; |
| 345 | |
| 346 | /* If checking for locale problems, see if the native ASCII-range |
| 347 | * printables plus \n and \t are in their expected categories in |
| 348 | * the new locale. If not, this could mean big trouble, upending |
| 349 | * Perl's and most programs' assumptions, like having a |
| 350 | * metacharacter with special meaning become a \w. Fortunately, |
| 351 | * it's very rare to find locales that aren't supersets of ASCII |
| 352 | * nowadays. It isn't a problem for most controls to be changed |
| 353 | * into something else; we check only \n and \t, though perhaps \r |
| 354 | * could be an issue as well. */ |
| 355 | if (check_for_problems |
| 356 | && (isGRAPH_A(i) || isBLANK_A(i) || i == '\n')) |
| 357 | { |
| 358 | if ((isALPHANUMERIC_A(i) && ! isALPHANUMERIC_LC(i)) |
| 359 | || (isPUNCT_A(i) && ! isPUNCT_LC(i)) |
| 360 | || (isBLANK_A(i) && ! isBLANK_LC(i)) |
| 361 | || (i == '\n' && ! isCNTRL_LC(i))) |
| 362 | { |
| 363 | if (bad_count) { /* Separate multiple entries with a |
| 364 | blank */ |
| 365 | bad_chars_list[bad_count++] = ' '; |
| 366 | } |
| 367 | bad_chars_list[bad_count++] = '\''; |
| 368 | if (isPRINT_A(i)) { |
| 369 | bad_chars_list[bad_count++] = (char) i; |
| 370 | } |
| 371 | else { |
| 372 | bad_chars_list[bad_count++] = '\\'; |
| 373 | if (i == '\n') { |
| 374 | bad_chars_list[bad_count++] = 'n'; |
| 375 | } |
| 376 | else { |
| 377 | assert(i == '\t'); |
| 378 | bad_chars_list[bad_count++] = 't'; |
| 379 | } |
| 380 | } |
| 381 | bad_chars_list[bad_count++] = '\''; |
| 382 | bad_chars_list[bad_count] = '\0'; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | #ifdef MB_CUR_MAX |
| 388 | /* We only handle single-byte locales (outside of UTF-8 ones; so if |
| 389 | * this locale requires more than one byte, there are going to be |
| 390 | * problems. */ |
| 391 | if (check_for_problems && MB_CUR_MAX > 1 |
| 392 | |
| 393 | /* Some platforms return MB_CUR_MAX > 1 for even the "C" |
| 394 | * locale. Just assume that the implementation for them (plus |
| 395 | * for POSIX) is correct and the > 1 value is spurious. (Since |
| 396 | * these are specially handled to never be considered UTF-8 |
| 397 | * locales, as long as this is the only problem, everything |
| 398 | * should work fine */ |
| 399 | && strNE(newctype, "C") && strNE(newctype, "POSIX")) |
| 400 | { |
| 401 | multi_byte_locale = TRUE; |
| 402 | } |
| 403 | #endif |
| 404 | |
| 405 | if (bad_count || multi_byte_locale) { |
| 406 | PL_warn_locale = Perl_newSVpvf(aTHX_ |
| 407 | "Locale '%s' may not work well.%s%s%s\n", |
| 408 | newctype, |
| 409 | (multi_byte_locale) |
| 410 | ? " Some characters in it are not recognized by" |
| 411 | " Perl." |
| 412 | : "", |
| 413 | (bad_count) |
| 414 | ? "\nThe following characters (and maybe others)" |
| 415 | " may not have the same meaning as the Perl" |
| 416 | " program expects:\n" |
| 417 | : "", |
| 418 | (bad_count) |
| 419 | ? bad_chars_list |
| 420 | : "" |
| 421 | ); |
| 422 | /* If we are actually in the scope of the locale or are debugging, |
| 423 | * output the message now. Otherwise we save it to be output at |
| 424 | * the first operation using this locale, if that actually happens. |
| 425 | * Most programs don't use locales, so they are immune to bad ones. |
| 426 | * */ |
| 427 | if (IN_LC(LC_CTYPE) || UNLIKELY(DEBUG_L_TEST)) { |
| 428 | |
| 429 | /* We have to save 'newctype' because the setlocale() just |
| 430 | * below may destroy it. The next setlocale() further down |
| 431 | * should restore it properly so that the intermediate change |
| 432 | * here is transparent to this function's caller */ |
| 433 | const char * const badlocale = savepv(newctype); |
| 434 | |
| 435 | setlocale(LC_CTYPE, "C"); |
| 436 | |
| 437 | /* The '0' below suppresses a bogus gcc compiler warning */ |
| 438 | Perl_warner(aTHX_ packWARN(WARN_LOCALE), SvPVX(PL_warn_locale), 0); |
| 439 | setlocale(LC_CTYPE, badlocale); |
| 440 | Safefree(badlocale); |
| 441 | SvREFCNT_dec_NN(PL_warn_locale); |
| 442 | PL_warn_locale = NULL; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | #endif /* USE_LOCALE_CTYPE */ |
| 448 | PERL_ARGS_ASSERT_NEW_CTYPE; |
| 449 | PERL_UNUSED_ARG(newctype); |
| 450 | PERL_UNUSED_CONTEXT; |
| 451 | } |
| 452 | |
| 453 | void |
| 454 | Perl__warn_problematic_locale() |
| 455 | { |
| 456 | |
| 457 | #ifdef USE_LOCALE_CTYPE |
| 458 | |
| 459 | dTHX; |
| 460 | |
| 461 | /* Internal-to-core function that outputs the message in PL_warn_locale, |
| 462 | * and then NULLS it. Should be called only through the macro |
| 463 | * _CHECK_AND_WARN_PROBLEMATIC_LOCALE */ |
| 464 | |
| 465 | if (PL_warn_locale) { |
| 466 | /*GCC_DIAG_IGNORE(-Wformat-security); Didn't work */ |
| 467 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), |
| 468 | SvPVX(PL_warn_locale), |
| 469 | 0 /* dummy to avoid compiler warning */ ); |
| 470 | /* GCC_DIAG_RESTORE; */ |
| 471 | SvREFCNT_dec_NN(PL_warn_locale); |
| 472 | PL_warn_locale = NULL; |
| 473 | } |
| 474 | |
| 475 | #endif |
| 476 | |
| 477 | } |
| 478 | |
| 479 | void |
| 480 | Perl_new_collate(pTHX_ const char *newcoll) |
| 481 | { |
| 482 | #ifdef USE_LOCALE_COLLATE |
| 483 | |
| 484 | /* Called after all libc setlocale() calls affecting LC_COLLATE, to tell |
| 485 | * core Perl this and that 'newcoll' is the name of the new locale. |
| 486 | * |
| 487 | * Any code changing the locale (outside this file) should use |
| 488 | * POSIX::setlocale, which calls this function. Therefore this function |
| 489 | * should be called directly only from this file and from |
| 490 | * POSIX::setlocale(). |
| 491 | * |
| 492 | * The design of locale collation is that every locale change is given an |
| 493 | * index 'PL_collation_ix'. The first time a string particpates in an |
| 494 | * operation that requires collation while locale collation is active, it |
| 495 | * is given PERL_MAGIC_collxfrm magic (via sv_collxfrm_flags()). That |
| 496 | * magic includes the collation index, and the transformation of the string |
| 497 | * by strxfrm(), q.v. That transformation is used when doing comparisons, |
| 498 | * instead of the string itself. If a string changes, the magic is |
| 499 | * cleared. The next time the locale changes, the index is incremented, |
| 500 | * and so we know during a comparison that the transformation is not |
| 501 | * necessarily still valid, and so is recomputed. Note that if the locale |
| 502 | * changes enough times, the index could wrap (a U32), and it is possible |
| 503 | * that a transformation would improperly be considered valid, leading to |
| 504 | * an unlikely bug */ |
| 505 | |
| 506 | if (! newcoll) { |
| 507 | if (PL_collation_name) { |
| 508 | ++PL_collation_ix; |
| 509 | Safefree(PL_collation_name); |
| 510 | PL_collation_name = NULL; |
| 511 | } |
| 512 | PL_collation_standard = TRUE; |
| 513 | is_standard_collation: |
| 514 | PL_collxfrm_base = 0; |
| 515 | PL_collxfrm_mult = 2; |
| 516 | PL_in_utf8_COLLATE_locale = FALSE; |
| 517 | PL_strxfrm_NUL_replacement = '\0'; |
| 518 | PL_strxfrm_max_cp = 0; |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | /* If this is not the same locale as currently, set the new one up */ |
| 523 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
| 524 | ++PL_collation_ix; |
| 525 | Safefree(PL_collation_name); |
| 526 | PL_collation_name = stdize_locale(savepv(newcoll)); |
| 527 | PL_collation_standard = isNAME_C_OR_POSIX(newcoll); |
| 528 | if (PL_collation_standard) { |
| 529 | goto is_standard_collation; |
| 530 | } |
| 531 | |
| 532 | PL_in_utf8_COLLATE_locale = _is_cur_LC_category_utf8(LC_COLLATE); |
| 533 | PL_strxfrm_NUL_replacement = '\0'; |
| 534 | PL_strxfrm_max_cp = 0; |
| 535 | |
| 536 | /* A locale collation definition includes primary, secondary, tertiary, |
| 537 | * etc. weights for each character. To sort, the primary weights are |
| 538 | * used, and only if they compare equal, then the secondary weights are |
| 539 | * used, and only if they compare equal, then the tertiary, etc. |
| 540 | * |
| 541 | * strxfrm() works by taking the input string, say ABC, and creating an |
| 542 | * output transformed string consisting of first the primary weights, |
| 543 | * A¹B¹C¹ followed by the secondary ones, A²B²C²; and then the |
| 544 | * tertiary, etc, yielding A¹B¹C¹ A²B²C² A³B³C³ .... Some characters |
| 545 | * may not have weights at every level. In our example, let's say B |
| 546 | * doesn't have a tertiary weight, and A doesn't have a secondary |
| 547 | * weight. The constructed string is then going to be |
| 548 | * A¹B¹C¹ B²C² A³C³ .... |
| 549 | * This has the desired effect that strcmp() will look at the secondary |
| 550 | * or tertiary weights only if the strings compare equal at all higher |
| 551 | * priority weights. The spaces shown here, like in |
| 552 | * "A¹B¹C¹ * A²B²C² " |
| 553 | * are not just for readability. In the general case, these must |
| 554 | * actually be bytes, which we will call here 'separator weights'; and |
| 555 | * they must be smaller than any other weight value, but since these |
| 556 | * are C strings, only the terminating one can be a NUL (some |
| 557 | * implementations may include a non-NUL separator weight just before |
| 558 | * the NUL). Implementations tend to reserve 01 for the separator |
| 559 | * weights. They are needed so that a shorter string's secondary |
| 560 | * weights won't be misconstrued as primary weights of a longer string, |
| 561 | * etc. By making them smaller than any other weight, the shorter |
| 562 | * string will sort first. (Actually, if all secondary weights are |
| 563 | * smaller than all primary ones, there is no need for a separator |
| 564 | * weight between those two levels, etc.) |
| 565 | * |
| 566 | * The length of the transformed string is roughly a linear function of |
| 567 | * the input string. It's not exactly linear because some characters |
| 568 | * don't have weights at all levels. When we call strxfrm() we have to |
| 569 | * allocate some memory to hold the transformed string. The |
| 570 | * calculations below try to find coefficients 'm' and 'b' for this |
| 571 | * locale so that m*x + b equals how much space we need, given the size |
| 572 | * of the input string in 'x'. If we calculate too small, we increase |
| 573 | * the size as needed, and call strxfrm() again, but it is better to |
| 574 | * get it right the first time to avoid wasted expensive string |
| 575 | * transformations. */ |
| 576 | |
| 577 | { |
| 578 | /* We use the string below to find how long the tranformation of it |
| 579 | * is. Almost all locales are supersets of ASCII, or at least the |
| 580 | * ASCII letters. We use all of them, half upper half lower, |
| 581 | * because if we used fewer, we might hit just the ones that are |
| 582 | * outliers in a particular locale. Most of the strings being |
| 583 | * collated will contain a preponderance of letters, and even if |
| 584 | * they are above-ASCII, they are likely to have the same number of |
| 585 | * weight levels as the ASCII ones. It turns out that digits tend |
| 586 | * to have fewer levels, and some punctuation has more, but those |
| 587 | * are relatively sparse in text, and khw believes this gives a |
| 588 | * reasonable result, but it could be changed if experience so |
| 589 | * dictates. */ |
| 590 | const char longer[] = "ABCDEFGHIJKLMnopqrstuvwxyz"; |
| 591 | char * x_longer; /* Transformed 'longer' */ |
| 592 | Size_t x_len_longer; /* Length of 'x_longer' */ |
| 593 | |
| 594 | char * x_shorter; /* We also transform a substring of 'longer' */ |
| 595 | Size_t x_len_shorter; |
| 596 | |
| 597 | /* _mem_collxfrm() is used get the transformation (though here we |
| 598 | * are interested only in its length). It is used because it has |
| 599 | * the intelligence to handle all cases, but to work, it needs some |
| 600 | * values of 'm' and 'b' to get it started. For the purposes of |
| 601 | * this calculation we use a very conservative estimate of 'm' and |
| 602 | * 'b'. This assumes a weight can be multiple bytes, enough to |
| 603 | * hold any UV on the platform, and there are 5 levels, 4 weight |
| 604 | * bytes, and a trailing NUL. */ |
| 605 | PL_collxfrm_base = 5; |
| 606 | PL_collxfrm_mult = 5 * sizeof(UV); |
| 607 | |
| 608 | /* Find out how long the transformation really is */ |
| 609 | x_longer = _mem_collxfrm(longer, |
| 610 | sizeof(longer) - 1, |
| 611 | &x_len_longer, |
| 612 | |
| 613 | /* We avoid converting to UTF-8 in the |
| 614 | * called function by telling it the |
| 615 | * string is in UTF-8 if the locale is a |
| 616 | * UTF-8 one. Since the string passed |
| 617 | * here is invariant under UTF-8, we can |
| 618 | * claim it's UTF-8 even though it isn't. |
| 619 | * */ |
| 620 | PL_in_utf8_COLLATE_locale); |
| 621 | Safefree(x_longer); |
| 622 | |
| 623 | /* Find out how long the transformation of a substring of 'longer' |
| 624 | * is. Together the lengths of these transformations are |
| 625 | * sufficient to calculate 'm' and 'b'. The substring is all of |
| 626 | * 'longer' except the first character. This minimizes the chances |
| 627 | * of being swayed by outliers */ |
| 628 | x_shorter = _mem_collxfrm(longer + 1, |
| 629 | sizeof(longer) - 2, |
| 630 | &x_len_shorter, |
| 631 | PL_in_utf8_COLLATE_locale); |
| 632 | Safefree(x_shorter); |
| 633 | |
| 634 | /* If the results are nonsensical for this simple test, the whole |
| 635 | * locale definition is suspect. Mark it so that locale collation |
| 636 | * is not active at all for it. XXX Should we warn? */ |
| 637 | if ( x_len_shorter == 0 |
| 638 | || x_len_longer == 0 |
| 639 | || x_len_shorter >= x_len_longer) |
| 640 | { |
| 641 | PL_collxfrm_mult = 0; |
| 642 | PL_collxfrm_base = 0; |
| 643 | } |
| 644 | else { |
| 645 | SSize_t base; /* Temporary */ |
| 646 | |
| 647 | /* We have both: m * strlen(longer) + b = x_len_longer |
| 648 | * m * strlen(shorter) + b = x_len_shorter; |
| 649 | * subtracting yields: |
| 650 | * m * (strlen(longer) - strlen(shorter)) |
| 651 | * = x_len_longer - x_len_shorter |
| 652 | * But we have set things up so that 'shorter' is 1 byte smaller |
| 653 | * than 'longer'. Hence: |
| 654 | * m = x_len_longer - x_len_shorter |
| 655 | * |
| 656 | * But if something went wrong, make sure the multiplier is at |
| 657 | * least 1. |
| 658 | */ |
| 659 | if (x_len_longer > x_len_shorter) { |
| 660 | PL_collxfrm_mult = (STRLEN) x_len_longer - x_len_shorter; |
| 661 | } |
| 662 | else { |
| 663 | PL_collxfrm_mult = 1; |
| 664 | } |
| 665 | |
| 666 | /* mx + b = len |
| 667 | * so: b = len - mx |
| 668 | * but in case something has gone wrong, make sure it is |
| 669 | * non-negative */ |
| 670 | base = x_len_longer - PL_collxfrm_mult * (sizeof(longer) - 1); |
| 671 | if (base < 0) { |
| 672 | base = 0; |
| 673 | } |
| 674 | |
| 675 | /* Add 1 for the trailing NUL */ |
| 676 | PL_collxfrm_base = base + 1; |
| 677 | } |
| 678 | |
| 679 | #ifdef DEBUGGING |
| 680 | if (DEBUG_L_TEST || debug_initialization) { |
| 681 | PerlIO_printf(Perl_debug_log, |
| 682 | "%s:%d: ?UTF-8 locale=%d; x_len_shorter=%zu, " |
| 683 | "x_len_longer=%zu," |
| 684 | " collate multipler=%zu, collate base=%zu\n", |
| 685 | __FILE__, __LINE__, |
| 686 | PL_in_utf8_COLLATE_locale, |
| 687 | x_len_shorter, x_len_longer, |
| 688 | PL_collxfrm_mult, PL_collxfrm_base); |
| 689 | } |
| 690 | #endif |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | #else |
| 695 | PERL_UNUSED_ARG(newcoll); |
| 696 | #endif /* USE_LOCALE_COLLATE */ |
| 697 | } |
| 698 | |
| 699 | #ifdef WIN32 |
| 700 | |
| 701 | char * |
| 702 | Perl_my_setlocale(pTHX_ int category, const char* locale) |
| 703 | { |
| 704 | /* This, for Windows, emulates POSIX setlocale() behavior. There is no |
| 705 | * difference unless the input locale is "", which means on Windows to get |
| 706 | * the machine default, which is set via the computer's "Regional and |
| 707 | * Language Options" (or its current equivalent). In POSIX, it instead |
| 708 | * means to find the locale from the user's environment. This routine |
| 709 | * looks in the environment, and, if anything is found, uses that instead |
| 710 | * of going to the machine default. If there is no environment override, |
| 711 | * the machine default is used, as normal, by calling the real setlocale() |
| 712 | * with "". The POSIX behavior is to use the LC_ALL variable if set; |
| 713 | * otherwise to use the particular category's variable if set; otherwise to |
| 714 | * use the LANG variable. */ |
| 715 | |
| 716 | bool override_LC_ALL = FALSE; |
| 717 | char * result; |
| 718 | |
| 719 | if (locale && strEQ(locale, "")) { |
| 720 | # ifdef LC_ALL |
| 721 | locale = PerlEnv_getenv("LC_ALL"); |
| 722 | if (! locale) { |
| 723 | #endif |
| 724 | switch (category) { |
| 725 | # ifdef LC_ALL |
| 726 | case LC_ALL: |
| 727 | override_LC_ALL = TRUE; |
| 728 | break; /* We already know its variable isn't set */ |
| 729 | # endif |
| 730 | # ifdef USE_LOCALE_TIME |
| 731 | case LC_TIME: |
| 732 | locale = PerlEnv_getenv("LC_TIME"); |
| 733 | break; |
| 734 | # endif |
| 735 | # ifdef USE_LOCALE_CTYPE |
| 736 | case LC_CTYPE: |
| 737 | locale = PerlEnv_getenv("LC_CTYPE"); |
| 738 | break; |
| 739 | # endif |
| 740 | # ifdef USE_LOCALE_COLLATE |
| 741 | case LC_COLLATE: |
| 742 | locale = PerlEnv_getenv("LC_COLLATE"); |
| 743 | break; |
| 744 | # endif |
| 745 | # ifdef USE_LOCALE_MONETARY |
| 746 | case LC_MONETARY: |
| 747 | locale = PerlEnv_getenv("LC_MONETARY"); |
| 748 | break; |
| 749 | # endif |
| 750 | # ifdef USE_LOCALE_NUMERIC |
| 751 | case LC_NUMERIC: |
| 752 | locale = PerlEnv_getenv("LC_NUMERIC"); |
| 753 | break; |
| 754 | # endif |
| 755 | # ifdef USE_LOCALE_MESSAGES |
| 756 | case LC_MESSAGES: |
| 757 | locale = PerlEnv_getenv("LC_MESSAGES"); |
| 758 | break; |
| 759 | # endif |
| 760 | default: |
| 761 | /* This is a category, like PAPER_SIZE that we don't |
| 762 | * know about; and so can't provide a wrapper. */ |
| 763 | break; |
| 764 | } |
| 765 | if (! locale) { |
| 766 | locale = PerlEnv_getenv("LANG"); |
| 767 | if (! locale) { |
| 768 | locale = ""; |
| 769 | } |
| 770 | } |
| 771 | # ifdef LC_ALL |
| 772 | } |
| 773 | # endif |
| 774 | } |
| 775 | |
| 776 | result = setlocale(category, locale); |
| 777 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", __FILE__, __LINE__, |
| 778 | _setlocale_debug_string(category, locale, result))); |
| 779 | |
| 780 | if (! override_LC_ALL) { |
| 781 | return result; |
| 782 | } |
| 783 | |
| 784 | /* Here the input category was LC_ALL, and we have set it to what is in the |
| 785 | * LANG variable or the system default if there is no LANG. But these have |
| 786 | * lower priority than the other LC_foo variables, so override it for each |
| 787 | * one that is set. (If they are set to "", it means to use the same thing |
| 788 | * we just set LC_ALL to, so can skip) */ |
| 789 | # ifdef USE_LOCALE_TIME |
| 790 | result = PerlEnv_getenv("LC_TIME"); |
| 791 | if (result && strNE(result, "")) { |
| 792 | setlocale(LC_TIME, result); |
| 793 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 794 | __FILE__, __LINE__, |
| 795 | _setlocale_debug_string(LC_TIME, result, "not captured"))); |
| 796 | } |
| 797 | # endif |
| 798 | # ifdef USE_LOCALE_CTYPE |
| 799 | result = PerlEnv_getenv("LC_CTYPE"); |
| 800 | if (result && strNE(result, "")) { |
| 801 | setlocale(LC_CTYPE, result); |
| 802 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 803 | __FILE__, __LINE__, |
| 804 | _setlocale_debug_string(LC_CTYPE, result, "not captured"))); |
| 805 | } |
| 806 | # endif |
| 807 | # ifdef USE_LOCALE_COLLATE |
| 808 | result = PerlEnv_getenv("LC_COLLATE"); |
| 809 | if (result && strNE(result, "")) { |
| 810 | setlocale(LC_COLLATE, result); |
| 811 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 812 | __FILE__, __LINE__, |
| 813 | _setlocale_debug_string(LC_COLLATE, result, "not captured"))); |
| 814 | } |
| 815 | # endif |
| 816 | # ifdef USE_LOCALE_MONETARY |
| 817 | result = PerlEnv_getenv("LC_MONETARY"); |
| 818 | if (result && strNE(result, "")) { |
| 819 | setlocale(LC_MONETARY, result); |
| 820 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 821 | __FILE__, __LINE__, |
| 822 | _setlocale_debug_string(LC_MONETARY, result, "not captured"))); |
| 823 | } |
| 824 | # endif |
| 825 | # ifdef USE_LOCALE_NUMERIC |
| 826 | result = PerlEnv_getenv("LC_NUMERIC"); |
| 827 | if (result && strNE(result, "")) { |
| 828 | setlocale(LC_NUMERIC, result); |
| 829 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 830 | __FILE__, __LINE__, |
| 831 | _setlocale_debug_string(LC_NUMERIC, result, "not captured"))); |
| 832 | } |
| 833 | # endif |
| 834 | # ifdef USE_LOCALE_MESSAGES |
| 835 | result = PerlEnv_getenv("LC_MESSAGES"); |
| 836 | if (result && strNE(result, "")) { |
| 837 | setlocale(LC_MESSAGES, result); |
| 838 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 839 | __FILE__, __LINE__, |
| 840 | _setlocale_debug_string(LC_MESSAGES, result, "not captured"))); |
| 841 | } |
| 842 | # endif |
| 843 | |
| 844 | result = setlocale(LC_ALL, NULL); |
| 845 | DEBUG_L(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", |
| 846 | __FILE__, __LINE__, |
| 847 | _setlocale_debug_string(LC_ALL, NULL, result))); |
| 848 | |
| 849 | return result; |
| 850 | } |
| 851 | |
| 852 | #endif |
| 853 | |
| 854 | |
| 855 | /* |
| 856 | * Initialize locale awareness. |
| 857 | */ |
| 858 | int |
| 859 | Perl_init_i18nl10n(pTHX_ int printwarn) |
| 860 | { |
| 861 | /* printwarn is |
| 862 | * |
| 863 | * 0 if not to output warning when setup locale is bad |
| 864 | * 1 if to output warning based on value of PERL_BADLANG |
| 865 | * >1 if to output regardless of PERL_BADLANG |
| 866 | * |
| 867 | * returns |
| 868 | * 1 = set ok or not applicable, |
| 869 | * 0 = fallback to a locale of lower priority |
| 870 | * -1 = fallback to all locales failed, not even to the C locale |
| 871 | * |
| 872 | * Under -DDEBUGGING, if the environment variable PERL_DEBUG_LOCALE_INIT is |
| 873 | * set, debugging information is output. |
| 874 | * |
| 875 | * This looks more complicated than it is, mainly due to the #ifdefs. |
| 876 | * |
| 877 | * We try to set LC_ALL to the value determined by the environment. If |
| 878 | * there is no LC_ALL on this platform, we try the individual categories we |
| 879 | * know about. If this works, we are done. |
| 880 | * |
| 881 | * But if it doesn't work, we have to do something else. We search the |
| 882 | * environment variables ourselves instead of relying on the system to do |
| 883 | * it. We look at, in order, LC_ALL, LANG, a system default locale (if we |
| 884 | * think there is one), and the ultimate fallback "C". This is all done in |
| 885 | * the same loop as above to avoid duplicating code, but it makes things |
| 886 | * more complex. After the original failure, we add the fallback |
| 887 | * possibilities to the list of locales to try, and iterate the loop |
| 888 | * through them all until one succeeds. |
| 889 | * |
| 890 | * On Ultrix, the locale MUST come from the environment, so there is |
| 891 | * preliminary code to set it. I (khw) am not sure that it is necessary, |
| 892 | * and that this couldn't be folded into the loop, but barring any real |
| 893 | * platforms to test on, it's staying as-is |
| 894 | * |
| 895 | * A slight complication is that in embedded Perls, the locale may already |
| 896 | * be set-up, and we don't want to get it from the normal environment |
| 897 | * variables. This is handled by having a special environment variable |
| 898 | * indicate we're in this situation. We simply set setlocale's 2nd |
| 899 | * parameter to be a NULL instead of "". That indicates to setlocale that |
| 900 | * it is not to change anything, but to return the current value, |
| 901 | * effectively initializing perl's db to what the locale already is. |
| 902 | * |
| 903 | * We play the same trick with NULL if a LC_ALL succeeds. We call |
| 904 | * setlocale() on the individual categores with NULL to get their existing |
| 905 | * values for our db, instead of trying to change them. |
| 906 | * */ |
| 907 | |
| 908 | int ok = 1; |
| 909 | |
| 910 | #if defined(USE_LOCALE) |
| 911 | #ifdef USE_LOCALE_CTYPE |
| 912 | char *curctype = NULL; |
| 913 | #endif /* USE_LOCALE_CTYPE */ |
| 914 | #ifdef USE_LOCALE_COLLATE |
| 915 | char *curcoll = NULL; |
| 916 | #endif /* USE_LOCALE_COLLATE */ |
| 917 | #ifdef USE_LOCALE_NUMERIC |
| 918 | char *curnum = NULL; |
| 919 | #endif /* USE_LOCALE_NUMERIC */ |
| 920 | #ifdef __GLIBC__ |
| 921 | const char * const language = savepv(PerlEnv_getenv("LANGUAGE")); |
| 922 | #endif |
| 923 | |
| 924 | /* NULL uses the existing already set up locale */ |
| 925 | const char * const setlocale_init = (PerlEnv_getenv("PERL_SKIP_LOCALE_INIT")) |
| 926 | ? NULL |
| 927 | : ""; |
| 928 | const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */ |
| 929 | unsigned int trial_locales_count; |
| 930 | const char * const lc_all = savepv(PerlEnv_getenv("LC_ALL")); |
| 931 | const char * const lang = savepv(PerlEnv_getenv("LANG")); |
| 932 | bool setlocale_failure = FALSE; |
| 933 | unsigned int i; |
| 934 | char *p; |
| 935 | |
| 936 | /* A later getenv() could zap this, so only use here */ |
| 937 | const char * const bad_lang_use_once = PerlEnv_getenv("PERL_BADLANG"); |
| 938 | |
| 939 | const bool locwarn = (printwarn > 1 |
| 940 | || (printwarn |
| 941 | && (! bad_lang_use_once |
| 942 | || ( |
| 943 | /* disallow with "" or "0" */ |
| 944 | *bad_lang_use_once |
| 945 | && strNE("0", bad_lang_use_once))))); |
| 946 | bool done = FALSE; |
| 947 | char * sl_result; /* return from setlocale() */ |
| 948 | char * locale_param; |
| 949 | #ifdef WIN32 |
| 950 | /* In some systems you can find out the system default locale |
| 951 | * and use that as the fallback locale. */ |
| 952 | # define SYSTEM_DEFAULT_LOCALE |
| 953 | #endif |
| 954 | #ifdef SYSTEM_DEFAULT_LOCALE |
| 955 | const char *system_default_locale = NULL; |
| 956 | #endif |
| 957 | |
| 958 | #ifdef DEBUGGING |
| 959 | DEBUG_INITIALIZATION_set((PerlEnv_getenv("PERL_DEBUG_LOCALE_INIT")) |
| 960 | ? TRUE |
| 961 | : FALSE); |
| 962 | # define DEBUG_LOCALE_INIT(category, locale, result) \ |
| 963 | STMT_START { \ |
| 964 | if (debug_initialization) { \ |
| 965 | PerlIO_printf(Perl_debug_log, \ |
| 966 | "%s:%d: %s\n", \ |
| 967 | __FILE__, __LINE__, \ |
| 968 | _setlocale_debug_string(category, \ |
| 969 | locale, \ |
| 970 | result)); \ |
| 971 | } \ |
| 972 | } STMT_END |
| 973 | #else |
| 974 | # define DEBUG_LOCALE_INIT(a,b,c) |
| 975 | #endif |
| 976 | |
| 977 | #ifndef LOCALE_ENVIRON_REQUIRED |
| 978 | PERL_UNUSED_VAR(done); |
| 979 | PERL_UNUSED_VAR(locale_param); |
| 980 | #else |
| 981 | |
| 982 | /* |
| 983 | * Ultrix setlocale(..., "") fails if there are no environment |
| 984 | * variables from which to get a locale name. |
| 985 | */ |
| 986 | |
| 987 | # ifdef LC_ALL |
| 988 | if (lang) { |
| 989 | sl_result = my_setlocale(LC_ALL, setlocale_init); |
| 990 | DEBUG_LOCALE_INIT(LC_ALL, setlocale_init, sl_result); |
| 991 | if (sl_result) |
| 992 | done = TRUE; |
| 993 | else |
| 994 | setlocale_failure = TRUE; |
| 995 | } |
| 996 | if (! setlocale_failure) { |
| 997 | # ifdef USE_LOCALE_CTYPE |
| 998 | locale_param = (! done && (lang || PerlEnv_getenv("LC_CTYPE"))) |
| 999 | ? setlocale_init |
| 1000 | : NULL; |
| 1001 | curctype = my_setlocale(LC_CTYPE, locale_param); |
| 1002 | DEBUG_LOCALE_INIT(LC_CTYPE, locale_param, sl_result); |
| 1003 | if (! curctype) |
| 1004 | setlocale_failure = TRUE; |
| 1005 | else |
| 1006 | curctype = savepv(curctype); |
| 1007 | # endif /* USE_LOCALE_CTYPE */ |
| 1008 | # ifdef USE_LOCALE_COLLATE |
| 1009 | locale_param = (! done && (lang || PerlEnv_getenv("LC_COLLATE"))) |
| 1010 | ? setlocale_init |
| 1011 | : NULL; |
| 1012 | curcoll = my_setlocale(LC_COLLATE, locale_param); |
| 1013 | DEBUG_LOCALE_INIT(LC_COLLATE, locale_param, sl_result); |
| 1014 | if (! curcoll) |
| 1015 | setlocale_failure = TRUE; |
| 1016 | else |
| 1017 | curcoll = savepv(curcoll); |
| 1018 | # endif /* USE_LOCALE_COLLATE */ |
| 1019 | # ifdef USE_LOCALE_NUMERIC |
| 1020 | locale_param = (! done && (lang || PerlEnv_getenv("LC_NUMERIC"))) |
| 1021 | ? setlocale_init |
| 1022 | : NULL; |
| 1023 | curnum = my_setlocale(LC_NUMERIC, locale_param); |
| 1024 | DEBUG_LOCALE_INIT(LC_NUMERIC, locale_param, sl_result); |
| 1025 | if (! curnum) |
| 1026 | setlocale_failure = TRUE; |
| 1027 | else |
| 1028 | curnum = savepv(curnum); |
| 1029 | # endif /* USE_LOCALE_NUMERIC */ |
| 1030 | # ifdef USE_LOCALE_MESSAGES |
| 1031 | locale_param = (! done && (lang || PerlEnv_getenv("LC_MESSAGES"))) |
| 1032 | ? setlocale_init |
| 1033 | : NULL; |
| 1034 | sl_result = my_setlocale(LC_MESSAGES, locale_param); |
| 1035 | DEBUG_LOCALE_INIT(LC_MESSAGES, locale_param, sl_result); |
| 1036 | if (! sl_result) { |
| 1037 | setlocale_failure = TRUE; |
| 1038 | } |
| 1039 | # endif /* USE_LOCALE_MESSAGES */ |
| 1040 | # ifdef USE_LOCALE_MONETARY |
| 1041 | locale_param = (! done && (lang || PerlEnv_getenv("LC_MONETARY"))) |
| 1042 | ? setlocale_init |
| 1043 | : NULL; |
| 1044 | sl_result = my_setlocale(LC_MONETARY, locale_param); |
| 1045 | DEBUG_LOCALE_INIT(LC_MONETARY, locale_param, sl_result); |
| 1046 | if (! sl_result) { |
| 1047 | setlocale_failure = TRUE; |
| 1048 | } |
| 1049 | # endif /* USE_LOCALE_MONETARY */ |
| 1050 | } |
| 1051 | |
| 1052 | # endif /* LC_ALL */ |
| 1053 | |
| 1054 | #endif /* !LOCALE_ENVIRON_REQUIRED */ |
| 1055 | |
| 1056 | /* We try each locale in the list until we get one that works, or exhaust |
| 1057 | * the list. Normally the loop is executed just once. But if setting the |
| 1058 | * locale fails, inside the loop we add fallback trials to the array and so |
| 1059 | * will execute the loop multiple times */ |
| 1060 | trial_locales[0] = setlocale_init; |
| 1061 | trial_locales_count = 1; |
| 1062 | for (i= 0; i < trial_locales_count; i++) { |
| 1063 | const char * trial_locale = trial_locales[i]; |
| 1064 | |
| 1065 | if (i > 0) { |
| 1066 | |
| 1067 | /* XXX This is to preserve old behavior for LOCALE_ENVIRON_REQUIRED |
| 1068 | * when i==0, but I (khw) don't think that behavior makes much |
| 1069 | * sense */ |
| 1070 | setlocale_failure = FALSE; |
| 1071 | |
| 1072 | #ifdef SYSTEM_DEFAULT_LOCALE |
| 1073 | # ifdef WIN32 |
| 1074 | /* On Windows machines, an entry of "" after the 0th means to use |
| 1075 | * the system default locale, which we now proceed to get. */ |
| 1076 | if (strEQ(trial_locale, "")) { |
| 1077 | unsigned int j; |
| 1078 | |
| 1079 | /* Note that this may change the locale, but we are going to do |
| 1080 | * that anyway just below */ |
| 1081 | system_default_locale = setlocale(LC_ALL, ""); |
| 1082 | DEBUG_LOCALE_INIT(LC_ALL, "", system_default_locale); |
| 1083 | |
| 1084 | /* Skip if invalid or it's already on the list of locales to |
| 1085 | * try */ |
| 1086 | if (! system_default_locale) { |
| 1087 | goto next_iteration; |
| 1088 | } |
| 1089 | for (j = 0; j < trial_locales_count; j++) { |
| 1090 | if (strEQ(system_default_locale, trial_locales[j])) { |
| 1091 | goto next_iteration; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | trial_locale = system_default_locale; |
| 1096 | } |
| 1097 | # endif /* WIN32 */ |
| 1098 | #endif /* SYSTEM_DEFAULT_LOCALE */ |
| 1099 | } |
| 1100 | |
| 1101 | #ifdef LC_ALL |
| 1102 | sl_result = my_setlocale(LC_ALL, trial_locale); |
| 1103 | DEBUG_LOCALE_INIT(LC_ALL, trial_locale, sl_result); |
| 1104 | if (! sl_result) { |
| 1105 | setlocale_failure = TRUE; |
| 1106 | } |
| 1107 | else { |
| 1108 | /* Since LC_ALL succeeded, it should have changed all the other |
| 1109 | * categories it can to its value; so we massage things so that the |
| 1110 | * setlocales below just return their category's current values. |
| 1111 | * This adequately handles the case in NetBSD where LC_COLLATE may |
| 1112 | * not be defined for a locale, and setting it individually will |
| 1113 | * fail, whereas setting LC_ALL suceeds, leaving LC_COLLATE set to |
| 1114 | * the POSIX locale. */ |
| 1115 | trial_locale = NULL; |
| 1116 | } |
| 1117 | #endif /* LC_ALL */ |
| 1118 | |
| 1119 | if (!setlocale_failure) { |
| 1120 | #ifdef USE_LOCALE_CTYPE |
| 1121 | Safefree(curctype); |
| 1122 | curctype = my_setlocale(LC_CTYPE, trial_locale); |
| 1123 | DEBUG_LOCALE_INIT(LC_CTYPE, trial_locale, curctype); |
| 1124 | if (! curctype) |
| 1125 | setlocale_failure = TRUE; |
| 1126 | else |
| 1127 | curctype = savepv(curctype); |
| 1128 | #endif /* USE_LOCALE_CTYPE */ |
| 1129 | #ifdef USE_LOCALE_COLLATE |
| 1130 | Safefree(curcoll); |
| 1131 | curcoll = my_setlocale(LC_COLLATE, trial_locale); |
| 1132 | DEBUG_LOCALE_INIT(LC_COLLATE, trial_locale, curcoll); |
| 1133 | if (! curcoll) |
| 1134 | setlocale_failure = TRUE; |
| 1135 | else |
| 1136 | curcoll = savepv(curcoll); |
| 1137 | #endif /* USE_LOCALE_COLLATE */ |
| 1138 | #ifdef USE_LOCALE_NUMERIC |
| 1139 | Safefree(curnum); |
| 1140 | curnum = my_setlocale(LC_NUMERIC, trial_locale); |
| 1141 | DEBUG_LOCALE_INIT(LC_NUMERIC, trial_locale, curnum); |
| 1142 | if (! curnum) |
| 1143 | setlocale_failure = TRUE; |
| 1144 | else |
| 1145 | curnum = savepv(curnum); |
| 1146 | #endif /* USE_LOCALE_NUMERIC */ |
| 1147 | #ifdef USE_LOCALE_MESSAGES |
| 1148 | sl_result = my_setlocale(LC_MESSAGES, trial_locale); |
| 1149 | DEBUG_LOCALE_INIT(LC_MESSAGES, trial_locale, sl_result); |
| 1150 | if (! (sl_result)) |
| 1151 | setlocale_failure = TRUE; |
| 1152 | #endif /* USE_LOCALE_MESSAGES */ |
| 1153 | #ifdef USE_LOCALE_MONETARY |
| 1154 | sl_result = my_setlocale(LC_MONETARY, trial_locale); |
| 1155 | DEBUG_LOCALE_INIT(LC_MONETARY, trial_locale, sl_result); |
| 1156 | if (! (sl_result)) |
| 1157 | setlocale_failure = TRUE; |
| 1158 | #endif /* USE_LOCALE_MONETARY */ |
| 1159 | |
| 1160 | if (! setlocale_failure) { /* Success */ |
| 1161 | break; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | /* Here, something failed; will need to try a fallback. */ |
| 1166 | ok = 0; |
| 1167 | |
| 1168 | if (i == 0) { |
| 1169 | unsigned int j; |
| 1170 | |
| 1171 | if (locwarn) { /* Output failure info only on the first one */ |
| 1172 | #ifdef LC_ALL |
| 1173 | |
| 1174 | PerlIO_printf(Perl_error_log, |
| 1175 | "perl: warning: Setting locale failed.\n"); |
| 1176 | |
| 1177 | #else /* !LC_ALL */ |
| 1178 | |
| 1179 | PerlIO_printf(Perl_error_log, |
| 1180 | "perl: warning: Setting locale failed for the categories:\n\t"); |
| 1181 | # ifdef USE_LOCALE_CTYPE |
| 1182 | if (! curctype) |
| 1183 | PerlIO_printf(Perl_error_log, "LC_CTYPE "); |
| 1184 | # endif /* USE_LOCALE_CTYPE */ |
| 1185 | # ifdef USE_LOCALE_COLLATE |
| 1186 | if (! curcoll) |
| 1187 | PerlIO_printf(Perl_error_log, "LC_COLLATE "); |
| 1188 | # endif /* USE_LOCALE_COLLATE */ |
| 1189 | # ifdef USE_LOCALE_NUMERIC |
| 1190 | if (! curnum) |
| 1191 | PerlIO_printf(Perl_error_log, "LC_NUMERIC "); |
| 1192 | # endif /* USE_LOCALE_NUMERIC */ |
| 1193 | PerlIO_printf(Perl_error_log, "and possibly others\n"); |
| 1194 | |
| 1195 | #endif /* LC_ALL */ |
| 1196 | |
| 1197 | PerlIO_printf(Perl_error_log, |
| 1198 | "perl: warning: Please check that your locale settings:\n"); |
| 1199 | |
| 1200 | #ifdef __GLIBC__ |
| 1201 | PerlIO_printf(Perl_error_log, |
| 1202 | "\tLANGUAGE = %c%s%c,\n", |
| 1203 | language ? '"' : '(', |
| 1204 | language ? language : "unset", |
| 1205 | language ? '"' : ')'); |
| 1206 | #endif |
| 1207 | |
| 1208 | PerlIO_printf(Perl_error_log, |
| 1209 | "\tLC_ALL = %c%s%c,\n", |
| 1210 | lc_all ? '"' : '(', |
| 1211 | lc_all ? lc_all : "unset", |
| 1212 | lc_all ? '"' : ')'); |
| 1213 | |
| 1214 | #if defined(USE_ENVIRON_ARRAY) |
| 1215 | { |
| 1216 | char **e; |
| 1217 | for (e = environ; *e; e++) { |
| 1218 | if (strEQs(*e, "LC_") |
| 1219 | && strNEs(*e, "LC_ALL=") |
| 1220 | && (p = strchr(*e, '='))) |
| 1221 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", |
| 1222 | (int)(p - *e), *e, p + 1); |
| 1223 | } |
| 1224 | } |
| 1225 | #else |
| 1226 | PerlIO_printf(Perl_error_log, |
| 1227 | "\t(possibly more locale environment variables)\n"); |
| 1228 | #endif |
| 1229 | |
| 1230 | PerlIO_printf(Perl_error_log, |
| 1231 | "\tLANG = %c%s%c\n", |
| 1232 | lang ? '"' : '(', |
| 1233 | lang ? lang : "unset", |
| 1234 | lang ? '"' : ')'); |
| 1235 | |
| 1236 | PerlIO_printf(Perl_error_log, |
| 1237 | " are supported and installed on your system.\n"); |
| 1238 | } |
| 1239 | |
| 1240 | /* Calculate what fallback locales to try. We have avoided this |
| 1241 | * until we have to, because failure is quite unlikely. This will |
| 1242 | * usually change the upper bound of the loop we are in. |
| 1243 | * |
| 1244 | * Since the system's default way of setting the locale has not |
| 1245 | * found one that works, We use Perl's defined ordering: LC_ALL, |
| 1246 | * LANG, and the C locale. We don't try the same locale twice, so |
| 1247 | * don't add to the list if already there. (On POSIX systems, the |
| 1248 | * LC_ALL element will likely be a repeat of the 0th element "", |
| 1249 | * but there's no harm done by doing it explicitly. |
| 1250 | * |
| 1251 | * Note that this tries the LC_ALL environment variable even on |
| 1252 | * systems which have no LC_ALL locale setting. This may or may |
| 1253 | * not have been originally intentional, but there's no real need |
| 1254 | * to change the behavior. */ |
| 1255 | if (lc_all) { |
| 1256 | for (j = 0; j < trial_locales_count; j++) { |
| 1257 | if (strEQ(lc_all, trial_locales[j])) { |
| 1258 | goto done_lc_all; |
| 1259 | } |
| 1260 | } |
| 1261 | trial_locales[trial_locales_count++] = lc_all; |
| 1262 | } |
| 1263 | done_lc_all: |
| 1264 | |
| 1265 | if (lang) { |
| 1266 | for (j = 0; j < trial_locales_count; j++) { |
| 1267 | if (strEQ(lang, trial_locales[j])) { |
| 1268 | goto done_lang; |
| 1269 | } |
| 1270 | } |
| 1271 | trial_locales[trial_locales_count++] = lang; |
| 1272 | } |
| 1273 | done_lang: |
| 1274 | |
| 1275 | #if defined(WIN32) && defined(LC_ALL) |
| 1276 | /* For Windows, we also try the system default locale before "C". |
| 1277 | * (If there exists a Windows without LC_ALL we skip this because |
| 1278 | * it gets too complicated. For those, the "C" is the next |
| 1279 | * fallback possibility). The "" is the same as the 0th element of |
| 1280 | * the array, but the code at the loop above knows to treat it |
| 1281 | * differently when not the 0th */ |
| 1282 | trial_locales[trial_locales_count++] = ""; |
| 1283 | #endif |
| 1284 | |
| 1285 | for (j = 0; j < trial_locales_count; j++) { |
| 1286 | if (strEQ("C", trial_locales[j])) { |
| 1287 | goto done_C; |
| 1288 | } |
| 1289 | } |
| 1290 | trial_locales[trial_locales_count++] = "C"; |
| 1291 | |
| 1292 | done_C: ; |
| 1293 | } /* end of first time through the loop */ |
| 1294 | |
| 1295 | #ifdef WIN32 |
| 1296 | next_iteration: ; |
| 1297 | #endif |
| 1298 | |
| 1299 | } /* end of looping through the trial locales */ |
| 1300 | |
| 1301 | if (ok < 1) { /* If we tried to fallback */ |
| 1302 | const char* msg; |
| 1303 | if (! setlocale_failure) { /* fallback succeeded */ |
| 1304 | msg = "Falling back to"; |
| 1305 | } |
| 1306 | else { /* fallback failed */ |
| 1307 | |
| 1308 | /* We dropped off the end of the loop, so have to decrement i to |
| 1309 | * get back to the value the last time through */ |
| 1310 | i--; |
| 1311 | |
| 1312 | ok = -1; |
| 1313 | msg = "Failed to fall back to"; |
| 1314 | |
| 1315 | /* To continue, we should use whatever values we've got */ |
| 1316 | #ifdef USE_LOCALE_CTYPE |
| 1317 | Safefree(curctype); |
| 1318 | curctype = savepv(setlocale(LC_CTYPE, NULL)); |
| 1319 | DEBUG_LOCALE_INIT(LC_CTYPE, NULL, curctype); |
| 1320 | #endif /* USE_LOCALE_CTYPE */ |
| 1321 | #ifdef USE_LOCALE_COLLATE |
| 1322 | Safefree(curcoll); |
| 1323 | curcoll = savepv(setlocale(LC_COLLATE, NULL)); |
| 1324 | DEBUG_LOCALE_INIT(LC_COLLATE, NULL, curcoll); |
| 1325 | #endif /* USE_LOCALE_COLLATE */ |
| 1326 | #ifdef USE_LOCALE_NUMERIC |
| 1327 | Safefree(curnum); |
| 1328 | curnum = savepv(setlocale(LC_NUMERIC, NULL)); |
| 1329 | DEBUG_LOCALE_INIT(LC_NUMERIC, NULL, curnum); |
| 1330 | #endif /* USE_LOCALE_NUMERIC */ |
| 1331 | } |
| 1332 | |
| 1333 | if (locwarn) { |
| 1334 | const char * description; |
| 1335 | const char * name = ""; |
| 1336 | if (strEQ(trial_locales[i], "C")) { |
| 1337 | description = "the standard locale"; |
| 1338 | name = "C"; |
| 1339 | } |
| 1340 | #ifdef SYSTEM_DEFAULT_LOCALE |
| 1341 | else if (strEQ(trial_locales[i], "")) { |
| 1342 | description = "the system default locale"; |
| 1343 | if (system_default_locale) { |
| 1344 | name = system_default_locale; |
| 1345 | } |
| 1346 | } |
| 1347 | #endif /* SYSTEM_DEFAULT_LOCALE */ |
| 1348 | else { |
| 1349 | description = "a fallback locale"; |
| 1350 | name = trial_locales[i]; |
| 1351 | } |
| 1352 | if (name && strNE(name, "")) { |
| 1353 | PerlIO_printf(Perl_error_log, |
| 1354 | "perl: warning: %s %s (\"%s\").\n", msg, description, name); |
| 1355 | } |
| 1356 | else { |
| 1357 | PerlIO_printf(Perl_error_log, |
| 1358 | "perl: warning: %s %s.\n", msg, description); |
| 1359 | } |
| 1360 | } |
| 1361 | } /* End of tried to fallback */ |
| 1362 | |
| 1363 | #ifdef USE_LOCALE_CTYPE |
| 1364 | new_ctype(curctype); |
| 1365 | #endif /* USE_LOCALE_CTYPE */ |
| 1366 | |
| 1367 | #ifdef USE_LOCALE_COLLATE |
| 1368 | new_collate(curcoll); |
| 1369 | #endif /* USE_LOCALE_COLLATE */ |
| 1370 | |
| 1371 | #ifdef USE_LOCALE_NUMERIC |
| 1372 | new_numeric(curnum); |
| 1373 | #endif /* USE_LOCALE_NUMERIC */ |
| 1374 | |
| 1375 | #if defined(USE_PERLIO) && defined(USE_LOCALE_CTYPE) |
| 1376 | /* Set PL_utf8locale to TRUE if using PerlIO _and_ the current LC_CTYPE |
| 1377 | * locale is UTF-8. If PL_utf8locale and PL_unicode (set by -C or by |
| 1378 | * $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the |
| 1379 | * PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open |
| 1380 | * discipline. */ |
| 1381 | PL_utf8locale = _is_cur_LC_category_utf8(LC_CTYPE); |
| 1382 | |
| 1383 | /* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO. |
| 1384 | This is an alternative to using the -C command line switch |
| 1385 | (the -C if present will override this). */ |
| 1386 | { |
| 1387 | const char *p = PerlEnv_getenv("PERL_UNICODE"); |
| 1388 | PL_unicode = p ? parse_unicode_opts(&p) : 0; |
| 1389 | if (PL_unicode & PERL_UNICODE_UTF8CACHEASSERT_FLAG) |
| 1390 | PL_utf8cache = -1; |
| 1391 | } |
| 1392 | #endif |
| 1393 | |
| 1394 | #ifdef USE_LOCALE_CTYPE |
| 1395 | Safefree(curctype); |
| 1396 | #endif /* USE_LOCALE_CTYPE */ |
| 1397 | #ifdef USE_LOCALE_COLLATE |
| 1398 | Safefree(curcoll); |
| 1399 | #endif /* USE_LOCALE_COLLATE */ |
| 1400 | #ifdef USE_LOCALE_NUMERIC |
| 1401 | Safefree(curnum); |
| 1402 | #endif /* USE_LOCALE_NUMERIC */ |
| 1403 | |
| 1404 | #ifdef __GLIBC__ |
| 1405 | Safefree(language); |
| 1406 | #endif |
| 1407 | |
| 1408 | Safefree(lc_all); |
| 1409 | Safefree(lang); |
| 1410 | |
| 1411 | #else /* !USE_LOCALE */ |
| 1412 | PERL_UNUSED_ARG(printwarn); |
| 1413 | #endif /* USE_LOCALE */ |
| 1414 | |
| 1415 | #ifdef DEBUGGING |
| 1416 | /* So won't continue to output stuff */ |
| 1417 | DEBUG_INITIALIZATION_set(FALSE); |
| 1418 | #endif |
| 1419 | |
| 1420 | return ok; |
| 1421 | } |
| 1422 | |
| 1423 | #ifdef USE_LOCALE_COLLATE |
| 1424 | |
| 1425 | char * |
| 1426 | Perl__mem_collxfrm(pTHX_ const char *input_string, |
| 1427 | STRLEN len, /* Length of 'input_string' */ |
| 1428 | STRLEN *xlen, /* Set to length of returned string |
| 1429 | (not including the collation index |
| 1430 | prefix) */ |
| 1431 | bool utf8 /* Is the input in UTF-8? */ |
| 1432 | ) |
| 1433 | { |
| 1434 | |
| 1435 | /* _mem_collxfrm() is a bit like strxfrm() but with two important |
| 1436 | * differences. First, it handles embedded NULs. Second, it allocates a bit |
| 1437 | * more memory than needed for the transformed data itself. The real |
| 1438 | * transformed data begins at offset COLLXFRM_HDR_LEN. *xlen is set to |
| 1439 | * the length of that, and doesn't include the collation index size. |
| 1440 | * Please see sv_collxfrm() to see how this is used. */ |
| 1441 | |
| 1442 | #define COLLXFRM_HDR_LEN sizeof(PL_collation_ix) |
| 1443 | |
| 1444 | char * s = (char *) input_string; |
| 1445 | STRLEN s_strlen = strlen(input_string); |
| 1446 | char *xbuf = NULL; |
| 1447 | STRLEN xAlloc; /* xalloc is a reserved word in VC */ |
| 1448 | STRLEN length_in_chars; |
| 1449 | bool first_time = TRUE; /* Cleared after first loop iteration */ |
| 1450 | |
| 1451 | PERL_ARGS_ASSERT__MEM_COLLXFRM; |
| 1452 | |
| 1453 | /* Must be NUL-terminated */ |
| 1454 | assert(*(input_string + len) == '\0'); |
| 1455 | |
| 1456 | /* If this locale has defective collation, skip */ |
| 1457 | if (PL_collxfrm_base == 0 && PL_collxfrm_mult == 0) { |
| 1458 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1459 | "_mem_collxfrm: locale's collation is defective\n")); |
| 1460 | goto bad; |
| 1461 | } |
| 1462 | |
| 1463 | /* Replace any embedded NULs with the control that sorts before any others. |
| 1464 | * This will give as good as possible results on strings that don't |
| 1465 | * otherwise contain that character, but otherwise there may be |
| 1466 | * less-than-perfect results with that character and NUL. This is |
| 1467 | * unavoidable unless we replace strxfrm with our own implementation. */ |
| 1468 | if (s_strlen < len) { /* Only execute if there is an embedded NUL */ |
| 1469 | char * e = s + len; |
| 1470 | char * sans_nuls; |
| 1471 | STRLEN sans_nuls_len; |
| 1472 | STRLEN sans_nuls_pos; |
| 1473 | int try_non_controls; |
| 1474 | char this_replacement_char[] = "?\0"; /* Room for a two-byte string, |
| 1475 | making sure 2nd byte is NUL. |
| 1476 | */ |
| 1477 | STRLEN this_replacement_len; |
| 1478 | |
| 1479 | /* If we don't know what non-NUL control character sorts lowest for |
| 1480 | * this locale, find it */ |
| 1481 | if (PL_strxfrm_NUL_replacement == '\0') { |
| 1482 | int j; |
| 1483 | char * cur_min_x = NULL; /* The min_char's xfrm, (except it also |
| 1484 | includes the collation index |
| 1485 | prefixed. */ |
| 1486 | |
| 1487 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, "Looking to replace NUL\n")); |
| 1488 | |
| 1489 | /* Unlikely, but it may be that no control will work to replace |
| 1490 | * NUL, in which case we instead look for any character. Controls |
| 1491 | * are preferred because collation order is, in general, context |
| 1492 | * sensitive, with adjoining characters affecting the order, and |
| 1493 | * controls are less likely to have such interactions, allowing the |
| 1494 | * NUL-replacement to stand on its own. (Another way to look at it |
| 1495 | * is to imagine what would happen if the NUL were replaced by a |
| 1496 | * combining character; it wouldn't work out all that well.) */ |
| 1497 | for (try_non_controls = 0; |
| 1498 | try_non_controls < 2; |
| 1499 | try_non_controls++) |
| 1500 | { |
| 1501 | /* Look through all legal code points (NUL isn't) */ |
| 1502 | for (j = 1; j < 256; j++) { |
| 1503 | char * x; /* j's xfrm plus collation index */ |
| 1504 | STRLEN x_len; /* length of 'x' */ |
| 1505 | STRLEN trial_len = 1; |
| 1506 | |
| 1507 | /* Create a 1 byte string of the current code point */ |
| 1508 | char cur_source[] = { (char) j, '\0' }; |
| 1509 | |
| 1510 | if (! try_non_controls && (PL_in_utf8_COLLATE_locale) |
| 1511 | ? ! isCNTRL_L1(j) |
| 1512 | : ! isCNTRL_LC(j)) |
| 1513 | { |
| 1514 | continue; |
| 1515 | } |
| 1516 | |
| 1517 | /* Then transform it */ |
| 1518 | x = _mem_collxfrm(cur_source, trial_len, &x_len, |
| 1519 | 0 /* The string is not in UTF-8 */); |
| 1520 | |
| 1521 | /* Ignore any character that didn't successfully transform. |
| 1522 | * */ |
| 1523 | if (! x) { |
| 1524 | continue; |
| 1525 | } |
| 1526 | |
| 1527 | /* If this character's transformation is lower than |
| 1528 | * the current lowest, this one becomes the lowest */ |
| 1529 | if ( cur_min_x == NULL |
| 1530 | || strLT(x + COLLXFRM_HDR_LEN, |
| 1531 | cur_min_x + COLLXFRM_HDR_LEN)) |
| 1532 | { |
| 1533 | PL_strxfrm_NUL_replacement = j; |
| 1534 | cur_min_x = x; |
| 1535 | } |
| 1536 | else { |
| 1537 | Safefree(x); |
| 1538 | } |
| 1539 | } /* end of loop through all 255 characters */ |
| 1540 | |
| 1541 | /* Stop looking if found */ |
| 1542 | if (cur_min_x) { |
| 1543 | break; |
| 1544 | } |
| 1545 | |
| 1546 | /* Unlikely, but possible, if there aren't any controls that |
| 1547 | * work in the locale, repeat the loop, looking for any |
| 1548 | * character that works */ |
| 1549 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1550 | "_mem_collxfrm: No control worked. Trying non-controls\n")); |
| 1551 | } /* End of loop to try first the controls, then any char */ |
| 1552 | |
| 1553 | if (! cur_min_x) { |
| 1554 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1555 | "_mem_collxfrm: Couldn't find any character to replace" |
| 1556 | " embedded NULs in locale %s with", PL_collation_name)); |
| 1557 | goto bad; |
| 1558 | } |
| 1559 | |
| 1560 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1561 | "_mem_collxfrm: Replacing embedded NULs in locale %s with " |
| 1562 | "0x%02X\n", PL_collation_name, PL_strxfrm_NUL_replacement)); |
| 1563 | |
| 1564 | Safefree(cur_min_x); |
| 1565 | } /* End of determining the character that is to replace NULs */ |
| 1566 | |
| 1567 | /* If the replacement is variant under UTF-8, it must match the |
| 1568 | * UTF8-ness as the original */ |
| 1569 | if ( ! UVCHR_IS_INVARIANT(PL_strxfrm_NUL_replacement) && utf8) { |
| 1570 | this_replacement_char[0] = |
| 1571 | UTF8_EIGHT_BIT_HI(PL_strxfrm_NUL_replacement); |
| 1572 | this_replacement_char[1] = |
| 1573 | UTF8_EIGHT_BIT_LO(PL_strxfrm_NUL_replacement); |
| 1574 | this_replacement_len = 2; |
| 1575 | } |
| 1576 | else { |
| 1577 | this_replacement_char[0] = PL_strxfrm_NUL_replacement; |
| 1578 | /* this_replacement_char[1] = '\0' was done at initialization */ |
| 1579 | this_replacement_len = 1; |
| 1580 | } |
| 1581 | |
| 1582 | /* The worst case length for the replaced string would be if every |
| 1583 | * character in it is NUL. Multiply that by the length of each |
| 1584 | * replacement, and allow for a trailing NUL */ |
| 1585 | sans_nuls_len = (len * this_replacement_len) + 1; |
| 1586 | Newx(sans_nuls, sans_nuls_len, char); |
| 1587 | *sans_nuls = '\0'; |
| 1588 | sans_nuls_pos = 0; |
| 1589 | |
| 1590 | /* Replace each NUL with the lowest collating control. Loop until have |
| 1591 | * exhausted all the NULs */ |
| 1592 | while (s + s_strlen < e) { |
| 1593 | sans_nuls_pos = my_strlcat(sans_nuls + sans_nuls_pos, |
| 1594 | s, |
| 1595 | sans_nuls_len); |
| 1596 | |
| 1597 | /* Do the actual replacement */ |
| 1598 | sans_nuls_pos = my_strlcat(sans_nuls + sans_nuls_pos, |
| 1599 | this_replacement_char, |
| 1600 | sans_nuls_len); |
| 1601 | |
| 1602 | /* Move past the input NUL */ |
| 1603 | s += s_strlen + 1; |
| 1604 | s_strlen = strlen(s); |
| 1605 | } |
| 1606 | |
| 1607 | /* And add anything that trails the final NUL */ |
| 1608 | my_strlcat(sans_nuls + sans_nuls_pos, s, sans_nuls_len); |
| 1609 | |
| 1610 | /* Switch so below we transform this modified string */ |
| 1611 | s = sans_nuls; |
| 1612 | len = strlen(s); |
| 1613 | } /* End of replacing NULs */ |
| 1614 | |
| 1615 | /* Make sure the UTF8ness of the string and locale match */ |
| 1616 | if (utf8 != PL_in_utf8_COLLATE_locale) { |
| 1617 | const char * const t = s; /* Temporary so we can later find where the |
| 1618 | input was */ |
| 1619 | |
| 1620 | /* Here they don't match. Change the string's to be what the locale is |
| 1621 | * expecting */ |
| 1622 | |
| 1623 | if (! utf8) { /* locale is UTF-8, but input isn't; upgrade the input */ |
| 1624 | s = (char *) bytes_to_utf8((const U8 *) s, &len); |
| 1625 | utf8 = TRUE; |
| 1626 | } |
| 1627 | else { /* locale is not UTF-8; but input is; downgrade the input */ |
| 1628 | |
| 1629 | s = (char *) bytes_from_utf8((const U8 *) s, &len, &utf8); |
| 1630 | |
| 1631 | /* If the downgrade was successful we are done, but if the input |
| 1632 | * contains things that require UTF-8 to represent, have to do |
| 1633 | * damage control ... */ |
| 1634 | if (UNLIKELY(utf8)) { |
| 1635 | |
| 1636 | /* What we do is construct a non-UTF-8 string with |
| 1637 | * 1) the characters representable by a single byte converted |
| 1638 | * to be so (if necessary); |
| 1639 | * 2) and the rest converted to collate the same as the |
| 1640 | * highest collating representable character. That makes |
| 1641 | * them collate at the end. This is similar to how we |
| 1642 | * handle embedded NULs, but we use the highest collating |
| 1643 | * code point instead of the smallest. Like the NUL case, |
| 1644 | * this isn't perfect, but is the best we can reasonably |
| 1645 | * do. Every above-255 code point will sort the same as |
| 1646 | * the highest-sorting 0-255 code point. If that code |
| 1647 | * point can combine in a sequence with some other code |
| 1648 | * points for weight calculations, us changing something to |
| 1649 | * be it can adversely affect the results. But in most |
| 1650 | * cases, it should work reasonably. And note that this is |
| 1651 | * really an illegal situation: using code points above 255 |
| 1652 | * on a locale where only 0-255 are valid. If two strings |
| 1653 | * sort entirely equal, then the sort order for the |
| 1654 | * above-255 code points will be in code point order. */ |
| 1655 | |
| 1656 | utf8 = FALSE; |
| 1657 | |
| 1658 | /* If we haven't calculated the code point with the maximum |
| 1659 | * collating order for this locale, do so now */ |
| 1660 | if (! PL_strxfrm_max_cp) { |
| 1661 | int j; |
| 1662 | |
| 1663 | /* The current transformed string that collates the |
| 1664 | * highest (except it also includes the prefixed collation |
| 1665 | * index. */ |
| 1666 | char * cur_max_x = NULL; |
| 1667 | |
| 1668 | /* Look through all legal code points (NUL isn't) */ |
| 1669 | for (j = 1; j < 256; j++) { |
| 1670 | char * x; |
| 1671 | STRLEN x_len; |
| 1672 | |
| 1673 | /* Create a 1-char string of the current code point. */ |
| 1674 | char cur_source[] = { (char) j, '\0' }; |
| 1675 | |
| 1676 | /* Then transform it */ |
| 1677 | x = _mem_collxfrm(cur_source, 1, &x_len, FALSE); |
| 1678 | |
| 1679 | /* If something went wrong (which it shouldn't), just |
| 1680 | * ignore this code point */ |
| 1681 | if (! x) { |
| 1682 | continue; |
| 1683 | } |
| 1684 | |
| 1685 | /* If this character's transformation is higher than |
| 1686 | * the current highest, this one becomes the highest */ |
| 1687 | if ( cur_max_x == NULL |
| 1688 | || strGT(x + COLLXFRM_HDR_LEN, |
| 1689 | cur_max_x + COLLXFRM_HDR_LEN)) |
| 1690 | { |
| 1691 | PL_strxfrm_max_cp = j; |
| 1692 | cur_max_x = x; |
| 1693 | } |
| 1694 | else { |
| 1695 | Safefree(x); |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | if (! cur_max_x) { |
| 1700 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1701 | "_mem_collxfrm: Couldn't find any character to" |
| 1702 | " replace above-Latin1 chars in locale %s with", |
| 1703 | PL_collation_name)); |
| 1704 | goto bad; |
| 1705 | } |
| 1706 | |
| 1707 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1708 | "_mem_collxfrm: highest 1-byte collating character" |
| 1709 | " in locale %s is 0x%02X\n", |
| 1710 | PL_collation_name, |
| 1711 | PL_strxfrm_max_cp)); |
| 1712 | |
| 1713 | Safefree(cur_max_x); |
| 1714 | } |
| 1715 | |
| 1716 | /* Here we know which legal code point collates the highest. |
| 1717 | * We are ready to construct the non-UTF-8 string. The length |
| 1718 | * will be at least 1 byte smaller than the input string |
| 1719 | * (because we changed at least one 2-byte character into a |
| 1720 | * single byte), but that is eaten up by the trailing NUL */ |
| 1721 | Newx(s, len, char); |
| 1722 | |
| 1723 | { |
| 1724 | STRLEN i; |
| 1725 | STRLEN d= 0; |
| 1726 | |
| 1727 | for (i = 0; i < len; i+= UTF8SKIP(t + i)) { |
| 1728 | U8 cur_char = t[i]; |
| 1729 | if (UTF8_IS_INVARIANT(cur_char)) { |
| 1730 | s[d++] = cur_char; |
| 1731 | } |
| 1732 | else if (UTF8_IS_DOWNGRADEABLE_START(cur_char)) { |
| 1733 | s[d++] = EIGHT_BIT_UTF8_TO_NATIVE(cur_char, t[i+1]); |
| 1734 | } |
| 1735 | else { /* Replace illegal cp with highest collating |
| 1736 | one */ |
| 1737 | s[d++] = PL_strxfrm_max_cp; |
| 1738 | } |
| 1739 | } |
| 1740 | s[d++] = '\0'; |
| 1741 | Renew(s, d, char); /* Free up unused space */ |
| 1742 | } |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | /* Here, we have constructed a modified version of the input. It could |
| 1747 | * be that we already had a modified copy before we did this version. |
| 1748 | * If so, that copy is no longer needed */ |
| 1749 | if (t != input_string) { |
| 1750 | Safefree(t); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | length_in_chars = (utf8) |
| 1755 | ? utf8_length((U8 *) s, (U8 *) s + len) |
| 1756 | : len; |
| 1757 | |
| 1758 | /* The first element in the output is the collation id, used by |
| 1759 | * sv_collxfrm(); then comes the space for the transformed string. The |
| 1760 | * equation should give us a good estimate as to how much is needed */ |
| 1761 | xAlloc = COLLXFRM_HDR_LEN |
| 1762 | + PL_collxfrm_base |
| 1763 | + (PL_collxfrm_mult * length_in_chars); |
| 1764 | Newx(xbuf, xAlloc, char); |
| 1765 | if (UNLIKELY(! xbuf)) { |
| 1766 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1767 | "_mem_collxfrm: Couldn't malloc %zu bytes\n", xAlloc)); |
| 1768 | goto bad; |
| 1769 | } |
| 1770 | |
| 1771 | /* Store the collation id */ |
| 1772 | *(U32*)xbuf = PL_collation_ix; |
| 1773 | |
| 1774 | /* Then the transformation of the input. We loop until successful, or we |
| 1775 | * give up */ |
| 1776 | for (;;) { |
| 1777 | |
| 1778 | *xlen = strxfrm(xbuf + COLLXFRM_HDR_LEN, s, xAlloc - COLLXFRM_HDR_LEN); |
| 1779 | |
| 1780 | /* If the transformed string occupies less space than we told strxfrm() |
| 1781 | * was available, it means it successfully transformed the whole |
| 1782 | * string. */ |
| 1783 | if (*xlen < xAlloc - COLLXFRM_HDR_LEN) { |
| 1784 | |
| 1785 | /* Some systems include a trailing NUL in the returned length. |
| 1786 | * Ignore it, using a loop in case multiple trailing NULs are |
| 1787 | * returned. */ |
| 1788 | while ( (*xlen) > 0 |
| 1789 | && *(xbuf + COLLXFRM_HDR_LEN + (*xlen) - 1) == '\0') |
| 1790 | { |
| 1791 | (*xlen)--; |
| 1792 | } |
| 1793 | |
| 1794 | /* If the first try didn't get it, it means our prediction was low. |
| 1795 | * Modify the coefficients so that we predict a larger value in any |
| 1796 | * future transformations */ |
| 1797 | if (! first_time) { |
| 1798 | STRLEN needed = *xlen + 1; /* +1 For trailing NUL */ |
| 1799 | STRLEN computed_guess = PL_collxfrm_base |
| 1800 | + (PL_collxfrm_mult * length_in_chars); |
| 1801 | |
| 1802 | /* On zero-length input, just keep current slope instead of |
| 1803 | * dividing by 0 */ |
| 1804 | const STRLEN new_m = (length_in_chars != 0) |
| 1805 | ? needed / length_in_chars |
| 1806 | : PL_collxfrm_mult; |
| 1807 | |
| 1808 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
| 1809 | "%s: %d: initial size of %zu bytes for a length " |
| 1810 | "%zu string was insufficient, %zu needed\n", |
| 1811 | __FILE__, __LINE__, |
| 1812 | computed_guess, length_in_chars, needed)); |
| 1813 | |
| 1814 | /* If slope increased, use it, but discard this result for |
| 1815 | * length 1 strings, as we can't be sure that it's a real slope |
| 1816 | * change */ |
| 1817 | if (length_in_chars > 1 && new_m > PL_collxfrm_mult) { |
| 1818 | #ifdef DEBUGGING |
| 1819 | STRLEN old_m = PL_collxfrm_mult; |
| 1820 | STRLEN old_b = PL_collxfrm_base; |
| 1821 | #endif |
| 1822 | PL_collxfrm_mult = new_m; |
| 1823 | PL_collxfrm_base = 1; /* +1 For trailing NUL */ |
| 1824 | computed_guess = PL_collxfrm_base |
| 1825 | + (PL_collxfrm_mult * length_in_chars); |
| 1826 | if (computed_guess < needed) { |
| 1827 | PL_collxfrm_base += needed - computed_guess; |
| 1828 | } |
| 1829 | |
| 1830 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
| 1831 | "%s: %d: slope is now %zu; was %zu, base " |
| 1832 | "is now %zu; was %zu\n", |
| 1833 | __FILE__, __LINE__, |
| 1834 | PL_collxfrm_mult, old_m, |
| 1835 | PL_collxfrm_base, old_b)); |
| 1836 | } |
| 1837 | else { /* Slope didn't change, but 'b' did */ |
| 1838 | const STRLEN new_b = needed |
| 1839 | - computed_guess |
| 1840 | + PL_collxfrm_base; |
| 1841 | DEBUG_Lv(PerlIO_printf(Perl_debug_log, |
| 1842 | "%s: %d: base is now %zu; was %zu\n", |
| 1843 | __FILE__, __LINE__, |
| 1844 | new_b, PL_collxfrm_base)); |
| 1845 | PL_collxfrm_base = new_b; |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | break; |
| 1850 | } |
| 1851 | |
| 1852 | if (UNLIKELY(*xlen >= PERL_INT_MAX)) { |
| 1853 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1854 | "_mem_collxfrm: Needed %zu bytes, max permissible is %u\n", |
| 1855 | *xlen, PERL_INT_MAX)); |
| 1856 | goto bad; |
| 1857 | } |
| 1858 | |
| 1859 | /* A well-behaved strxfrm() returns exactly how much space it needs |
| 1860 | * (usually not including the trailing NUL) when it fails due to not |
| 1861 | * enough space being provided. Assume that this is the case unless |
| 1862 | * it's been proven otherwise */ |
| 1863 | if (LIKELY(PL_strxfrm_is_behaved) && first_time) { |
| 1864 | xAlloc = *xlen + COLLXFRM_HDR_LEN + 1; |
| 1865 | } |
| 1866 | else { /* Here, either: |
| 1867 | * 1) The strxfrm() has previously shown bad behavior; or |
| 1868 | * 2) It isn't the first time through the loop, which means |
| 1869 | * that the strxfrm() is now showing bad behavior, because |
| 1870 | * we gave it what it said was needed in the previous |
| 1871 | * iteration, and it came back saying it needed still more. |
| 1872 | * (Many versions of cygwin fit this. When the buffer size |
| 1873 | * isn't sufficient, they return the input size instead of |
| 1874 | * how much is needed.) |
| 1875 | * Increase the buffer size by a fixed percentage and try again. |
| 1876 | * */ |
| 1877 | xAlloc += (xAlloc / 4) + 1; |
| 1878 | PL_strxfrm_is_behaved = FALSE; |
| 1879 | |
| 1880 | #ifdef DEBUGGING |
| 1881 | if (DEBUG_Lv_TEST || debug_initialization) { |
| 1882 | PerlIO_printf(Perl_debug_log, |
| 1883 | "_mem_collxfrm required more space than previously calculated" |
| 1884 | " for locale %s, trying again with new guess=%d+%zu\n", |
| 1885 | PL_collation_name, (int) COLLXFRM_HDR_LEN, |
| 1886 | xAlloc - COLLXFRM_HDR_LEN); |
| 1887 | } |
| 1888 | #endif |
| 1889 | } |
| 1890 | |
| 1891 | Renew(xbuf, xAlloc, char); |
| 1892 | if (UNLIKELY(! xbuf)) { |
| 1893 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 1894 | "_mem_collxfrm: Couldn't realloc %zu bytes\n", xAlloc)); |
| 1895 | goto bad; |
| 1896 | } |
| 1897 | |
| 1898 | first_time = FALSE; |
| 1899 | } |
| 1900 | |
| 1901 | |
| 1902 | #ifdef DEBUGGING |
| 1903 | if (DEBUG_Lv_TEST || debug_initialization) { |
| 1904 | Size_t i; |
| 1905 | |
| 1906 | print_collxfrm_input_and_return(s, s + len, xlen, utf8); |
| 1907 | PerlIO_printf(Perl_debug_log, "Its xfrm is:"); |
| 1908 | for (i = COLLXFRM_HDR_LEN; i < *xlen + COLLXFRM_HDR_LEN; i++) { |
| 1909 | PerlIO_printf(Perl_debug_log, " %02x", (U8) xbuf[i]); |
| 1910 | } |
| 1911 | PerlIO_printf(Perl_debug_log, "\n"); |
| 1912 | } |
| 1913 | #endif |
| 1914 | |
| 1915 | /* Free up unneeded space; retain ehough for trailing NUL */ |
| 1916 | Renew(xbuf, COLLXFRM_HDR_LEN + *xlen + 1, char); |
| 1917 | |
| 1918 | if (s != input_string) { |
| 1919 | Safefree(s); |
| 1920 | } |
| 1921 | |
| 1922 | return xbuf; |
| 1923 | |
| 1924 | bad: |
| 1925 | Safefree(xbuf); |
| 1926 | if (s != input_string) { |
| 1927 | Safefree(s); |
| 1928 | } |
| 1929 | *xlen = 0; |
| 1930 | #ifdef DEBUGGING |
| 1931 | if (DEBUG_Lv_TEST || debug_initialization) { |
| 1932 | print_collxfrm_input_and_return(s, s + len, NULL, utf8); |
| 1933 | } |
| 1934 | #endif |
| 1935 | return NULL; |
| 1936 | } |
| 1937 | |
| 1938 | #ifdef DEBUGGING |
| 1939 | |
| 1940 | STATIC void |
| 1941 | S_print_collxfrm_input_and_return(pTHX_ |
| 1942 | const char * const s, |
| 1943 | const char * const e, |
| 1944 | const STRLEN * const xlen, |
| 1945 | const bool is_utf8) |
| 1946 | { |
| 1947 | const char * t = s; |
| 1948 | bool prev_was_printable = TRUE; |
| 1949 | bool first_time = TRUE; |
| 1950 | |
| 1951 | PERL_ARGS_ASSERT_PRINT_COLLXFRM_INPUT_AND_RETURN; |
| 1952 | |
| 1953 | PerlIO_printf(Perl_debug_log, "_mem_collxfrm[%u]: returning ", |
| 1954 | PL_collation_ix); |
| 1955 | if (xlen) { |
| 1956 | PerlIO_printf(Perl_debug_log, "%" UVuf, (UV) *xlen); |
| 1957 | } |
| 1958 | else { |
| 1959 | PerlIO_printf(Perl_debug_log, "NULL"); |
| 1960 | } |
| 1961 | PerlIO_printf(Perl_debug_log, " for locale '%s', string='", |
| 1962 | PL_collation_name); |
| 1963 | |
| 1964 | while (t < e) { |
| 1965 | UV cp = (is_utf8) |
| 1966 | ? utf8_to_uvchr_buf((U8 *) t, e, NULL) |
| 1967 | : * (U8 *) t; |
| 1968 | if (isPRINT(cp)) { |
| 1969 | if (! prev_was_printable) { |
| 1970 | PerlIO_printf(Perl_debug_log, " "); |
| 1971 | } |
| 1972 | PerlIO_printf(Perl_debug_log, "%c", (U8) cp); |
| 1973 | prev_was_printable = TRUE; |
| 1974 | } |
| 1975 | else { |
| 1976 | if (! first_time) { |
| 1977 | PerlIO_printf(Perl_debug_log, " "); |
| 1978 | } |
| 1979 | PerlIO_printf(Perl_debug_log, "%02" UVXf, cp); |
| 1980 | prev_was_printable = FALSE; |
| 1981 | } |
| 1982 | t += (is_utf8) ? UTF8SKIP(t) : 1; |
| 1983 | first_time = FALSE; |
| 1984 | } |
| 1985 | |
| 1986 | PerlIO_printf(Perl_debug_log, "'\n"); |
| 1987 | } |
| 1988 | |
| 1989 | #endif /* #ifdef DEBUGGING */ |
| 1990 | |
| 1991 | #endif /* USE_LOCALE_COLLATE */ |
| 1992 | |
| 1993 | #ifdef USE_LOCALE |
| 1994 | |
| 1995 | bool |
| 1996 | Perl__is_cur_LC_category_utf8(pTHX_ int category) |
| 1997 | { |
| 1998 | /* Returns TRUE if the current locale for 'category' is UTF-8; FALSE |
| 1999 | * otherwise. 'category' may not be LC_ALL. If the platform doesn't have |
| 2000 | * nl_langinfo(), nor MB_CUR_MAX, this employs a heuristic, which hence |
| 2001 | * could give the wrong result. The result will very likely be correct for |
| 2002 | * languages that have commonly used non-ASCII characters, but for notably |
| 2003 | * English, it comes down to if the locale's name ends in something like |
| 2004 | * "UTF-8". It errs on the side of not being a UTF-8 locale. */ |
| 2005 | |
| 2006 | char *save_input_locale = NULL; |
| 2007 | STRLEN final_pos; |
| 2008 | |
| 2009 | #ifdef LC_ALL |
| 2010 | assert(category != LC_ALL); |
| 2011 | #endif |
| 2012 | |
| 2013 | /* First dispose of the trivial cases */ |
| 2014 | save_input_locale = setlocale(category, NULL); |
| 2015 | if (! save_input_locale) { |
| 2016 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2017 | "Could not find current locale for category %d\n", |
| 2018 | category)); |
| 2019 | return FALSE; /* XXX maybe should croak */ |
| 2020 | } |
| 2021 | save_input_locale = stdize_locale(savepv(save_input_locale)); |
| 2022 | if (isNAME_C_OR_POSIX(save_input_locale)) { |
| 2023 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2024 | "Current locale for category %d is %s\n", |
| 2025 | category, save_input_locale)); |
| 2026 | Safefree(save_input_locale); |
| 2027 | return FALSE; |
| 2028 | } |
| 2029 | |
| 2030 | #if defined(USE_LOCALE_CTYPE) \ |
| 2031 | && (defined(MB_CUR_MAX) || (defined(HAS_NL_LANGINFO) && defined(CODESET))) |
| 2032 | |
| 2033 | { /* Next try nl_langinfo or MB_CUR_MAX if available */ |
| 2034 | |
| 2035 | char *save_ctype_locale = NULL; |
| 2036 | bool is_utf8; |
| 2037 | |
| 2038 | if (category != LC_CTYPE) { /* These work only on LC_CTYPE */ |
| 2039 | |
| 2040 | /* Get the current LC_CTYPE locale */ |
| 2041 | save_ctype_locale = setlocale(LC_CTYPE, NULL); |
| 2042 | if (! save_ctype_locale) { |
| 2043 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2044 | "Could not find current locale for LC_CTYPE\n")); |
| 2045 | goto cant_use_nllanginfo; |
| 2046 | } |
| 2047 | save_ctype_locale = stdize_locale(savepv(save_ctype_locale)); |
| 2048 | |
| 2049 | /* If LC_CTYPE and the desired category use the same locale, this |
| 2050 | * means that finding the value for LC_CTYPE is the same as finding |
| 2051 | * the value for the desired category. Otherwise, switch LC_CTYPE |
| 2052 | * to the desired category's locale */ |
| 2053 | if (strEQ(save_ctype_locale, save_input_locale)) { |
| 2054 | Safefree(save_ctype_locale); |
| 2055 | save_ctype_locale = NULL; |
| 2056 | } |
| 2057 | else if (! setlocale(LC_CTYPE, save_input_locale)) { |
| 2058 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2059 | "Could not change LC_CTYPE locale to %s\n", |
| 2060 | save_input_locale)); |
| 2061 | Safefree(save_ctype_locale); |
| 2062 | goto cant_use_nllanginfo; |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | DEBUG_L(PerlIO_printf(Perl_debug_log, "Current LC_CTYPE locale=%s\n", |
| 2067 | save_input_locale)); |
| 2068 | |
| 2069 | /* Here the current LC_CTYPE is set to the locale of the category whose |
| 2070 | * information is desired. This means that nl_langinfo() and MB_CUR_MAX |
| 2071 | * should give the correct results */ |
| 2072 | |
| 2073 | # if defined(HAS_NL_LANGINFO) && defined(CODESET) |
| 2074 | { |
| 2075 | char *codeset = nl_langinfo(CODESET); |
| 2076 | if (codeset && strNE(codeset, "")) { |
| 2077 | codeset = savepv(codeset); |
| 2078 | |
| 2079 | /* If we switched LC_CTYPE, switch back */ |
| 2080 | if (save_ctype_locale) { |
| 2081 | setlocale(LC_CTYPE, save_ctype_locale); |
| 2082 | Safefree(save_ctype_locale); |
| 2083 | } |
| 2084 | |
| 2085 | is_utf8 = foldEQ(codeset, STR_WITH_LEN("UTF-8")) |
| 2086 | || foldEQ(codeset, STR_WITH_LEN("UTF8")); |
| 2087 | |
| 2088 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2089 | "\tnllanginfo returned CODESET '%s'; ?UTF8 locale=%d\n", |
| 2090 | codeset, is_utf8)); |
| 2091 | Safefree(codeset); |
| 2092 | Safefree(save_input_locale); |
| 2093 | return is_utf8; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | # endif |
| 2098 | # ifdef MB_CUR_MAX |
| 2099 | |
| 2100 | /* Here, either we don't have nl_langinfo, or it didn't return a |
| 2101 | * codeset. Try MB_CUR_MAX */ |
| 2102 | |
| 2103 | /* Standard UTF-8 needs at least 4 bytes to represent the maximum |
| 2104 | * Unicode code point. Since UTF-8 is the only non-single byte |
| 2105 | * encoding we handle, we just say any such encoding is UTF-8, and if |
| 2106 | * turns out to be wrong, other things will fail */ |
| 2107 | is_utf8 = MB_CUR_MAX >= 4; |
| 2108 | |
| 2109 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2110 | "\tMB_CUR_MAX=%d; ?UTF8 locale=%d\n", |
| 2111 | (int) MB_CUR_MAX, is_utf8)); |
| 2112 | |
| 2113 | Safefree(save_input_locale); |
| 2114 | |
| 2115 | # ifdef HAS_MBTOWC |
| 2116 | |
| 2117 | /* ... But, most system that have MB_CUR_MAX will also have mbtowc(), |
| 2118 | * since they are both in the C99 standard. We can feed a known byte |
| 2119 | * string to the latter function, and check that it gives the expected |
| 2120 | * result */ |
| 2121 | if (is_utf8) { |
| 2122 | wchar_t wc; |
| 2123 | PERL_UNUSED_RESULT(mbtowc(&wc, NULL, 0));/* Reset any shift state */ |
| 2124 | errno = 0; |
| 2125 | if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)) |
| 2126 | != strlen(HYPHEN_UTF8) |
| 2127 | || wc != (wchar_t) 0x2010) |
| 2128 | { |
| 2129 | is_utf8 = FALSE; |
| 2130 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\thyphen=U+%x\n", (unsigned int)wc)); |
| 2131 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2132 | "\treturn from mbtowc=%d; errno=%d; ?UTF8 locale=0\n", |
| 2133 | mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8)), errno)); |
| 2134 | } |
| 2135 | } |
| 2136 | # endif |
| 2137 | |
| 2138 | /* If we switched LC_CTYPE, switch back */ |
| 2139 | if (save_ctype_locale) { |
| 2140 | setlocale(LC_CTYPE, save_ctype_locale); |
| 2141 | Safefree(save_ctype_locale); |
| 2142 | } |
| 2143 | |
| 2144 | return is_utf8; |
| 2145 | # endif |
| 2146 | } |
| 2147 | |
| 2148 | cant_use_nllanginfo: |
| 2149 | |
| 2150 | #else /* nl_langinfo should work if available, so don't bother compiling this |
| 2151 | fallback code. The final fallback of looking at the name is |
| 2152 | compiled, and will be executed if nl_langinfo fails */ |
| 2153 | |
| 2154 | /* nl_langinfo not available or failed somehow. Next try looking at the |
| 2155 | * currency symbol to see if it disambiguates things. Often that will be |
| 2156 | * in the native script, and if the symbol isn't in UTF-8, we know that the |
| 2157 | * locale isn't. If it is non-ASCII UTF-8, we infer that the locale is |
| 2158 | * too, as the odds of a non-UTF8 string being valid UTF-8 are quite small |
| 2159 | * */ |
| 2160 | |
| 2161 | #ifdef HAS_LOCALECONV |
| 2162 | # ifdef USE_LOCALE_MONETARY |
| 2163 | { |
| 2164 | char *save_monetary_locale = NULL; |
| 2165 | bool only_ascii = FALSE; |
| 2166 | bool is_utf8 = FALSE; |
| 2167 | struct lconv* lc; |
| 2168 | |
| 2169 | /* Like above for LC_CTYPE, we first set LC_MONETARY to the locale of |
| 2170 | * the desired category, if it isn't that locale already */ |
| 2171 | |
| 2172 | if (category != LC_MONETARY) { |
| 2173 | |
| 2174 | save_monetary_locale = setlocale(LC_MONETARY, NULL); |
| 2175 | if (! save_monetary_locale) { |
| 2176 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2177 | "Could not find current locale for LC_MONETARY\n")); |
| 2178 | goto cant_use_monetary; |
| 2179 | } |
| 2180 | save_monetary_locale = stdize_locale(savepv(save_monetary_locale)); |
| 2181 | |
| 2182 | if (strEQ(save_monetary_locale, save_input_locale)) { |
| 2183 | Safefree(save_monetary_locale); |
| 2184 | save_monetary_locale = NULL; |
| 2185 | } |
| 2186 | else if (! setlocale(LC_MONETARY, save_input_locale)) { |
| 2187 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2188 | "Could not change LC_MONETARY locale to %s\n", |
| 2189 | save_input_locale)); |
| 2190 | Safefree(save_monetary_locale); |
| 2191 | goto cant_use_monetary; |
| 2192 | } |
| 2193 | } |
| 2194 | |
| 2195 | /* Here the current LC_MONETARY is set to the locale of the category |
| 2196 | * whose information is desired. */ |
| 2197 | |
| 2198 | lc = localeconv(); |
| 2199 | if (! lc |
| 2200 | || ! lc->currency_symbol |
| 2201 | || is_utf8_invariant_string((U8 *) lc->currency_symbol, 0)) |
| 2202 | { |
| 2203 | 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)); |
| 2204 | only_ascii = TRUE; |
| 2205 | } |
| 2206 | else { |
| 2207 | is_utf8 = is_utf8_string((U8 *) lc->currency_symbol, 0); |
| 2208 | } |
| 2209 | |
| 2210 | /* If we changed it, restore LC_MONETARY to its original locale */ |
| 2211 | if (save_monetary_locale) { |
| 2212 | setlocale(LC_MONETARY, save_monetary_locale); |
| 2213 | Safefree(save_monetary_locale); |
| 2214 | } |
| 2215 | |
| 2216 | if (! only_ascii) { |
| 2217 | |
| 2218 | /* It isn't a UTF-8 locale if the symbol is not legal UTF-8; |
| 2219 | * otherwise assume the locale is UTF-8 if and only if the symbol |
| 2220 | * is non-ascii UTF-8. */ |
| 2221 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?Currency symbol for %s is UTF-8=%d\n", |
| 2222 | save_input_locale, is_utf8)); |
| 2223 | Safefree(save_input_locale); |
| 2224 | return is_utf8; |
| 2225 | } |
| 2226 | } |
| 2227 | cant_use_monetary: |
| 2228 | |
| 2229 | # endif /* USE_LOCALE_MONETARY */ |
| 2230 | #endif /* HAS_LOCALECONV */ |
| 2231 | |
| 2232 | #if defined(HAS_STRFTIME) && defined(USE_LOCALE_TIME) |
| 2233 | |
| 2234 | /* Still haven't found a non-ASCII string to disambiguate UTF-8 or not. Try |
| 2235 | * the names of the months and weekdays, timezone, and am/pm indicator */ |
| 2236 | { |
| 2237 | char *save_time_locale = NULL; |
| 2238 | int hour = 10; |
| 2239 | bool is_dst = FALSE; |
| 2240 | int dom = 1; |
| 2241 | int month = 0; |
| 2242 | int i; |
| 2243 | char * formatted_time; |
| 2244 | |
| 2245 | |
| 2246 | /* Like above for LC_MONETARY, we set LC_TIME to the locale of the |
| 2247 | * desired category, if it isn't that locale already */ |
| 2248 | |
| 2249 | if (category != LC_TIME) { |
| 2250 | |
| 2251 | save_time_locale = setlocale(LC_TIME, NULL); |
| 2252 | if (! save_time_locale) { |
| 2253 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2254 | "Could not find current locale for LC_TIME\n")); |
| 2255 | goto cant_use_time; |
| 2256 | } |
| 2257 | save_time_locale = stdize_locale(savepv(save_time_locale)); |
| 2258 | |
| 2259 | if (strEQ(save_time_locale, save_input_locale)) { |
| 2260 | Safefree(save_time_locale); |
| 2261 | save_time_locale = NULL; |
| 2262 | } |
| 2263 | else if (! setlocale(LC_TIME, save_input_locale)) { |
| 2264 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2265 | "Could not change LC_TIME locale to %s\n", |
| 2266 | save_input_locale)); |
| 2267 | Safefree(save_time_locale); |
| 2268 | goto cant_use_time; |
| 2269 | } |
| 2270 | } |
| 2271 | |
| 2272 | /* Here the current LC_TIME is set to the locale of the category |
| 2273 | * whose information is desired. Look at all the days of the week and |
| 2274 | * month names, and the timezone and am/pm indicator for UTF-8 variant |
| 2275 | * characters. The first such a one found will tell us if the locale |
| 2276 | * is UTF-8 or not */ |
| 2277 | |
| 2278 | for (i = 0; i < 7 + 12; i++) { /* 7 days; 12 months */ |
| 2279 | formatted_time = my_strftime("%A %B %Z %p", |
| 2280 | 0, 0, hour, dom, month, 112, 0, 0, is_dst); |
| 2281 | if ( ! formatted_time |
| 2282 | || is_utf8_invariant_string((U8 *) formatted_time, 0)) |
| 2283 | { |
| 2284 | |
| 2285 | /* Here, we didn't find a non-ASCII. Try the next time through |
| 2286 | * with the complemented dst and am/pm, and try with the next |
| 2287 | * weekday. After we have gotten all weekdays, try the next |
| 2288 | * month */ |
| 2289 | is_dst = ! is_dst; |
| 2290 | hour = (hour + 12) % 24; |
| 2291 | dom++; |
| 2292 | if (i > 6) { |
| 2293 | month++; |
| 2294 | } |
| 2295 | continue; |
| 2296 | } |
| 2297 | |
| 2298 | /* Here, we have a non-ASCII. Return TRUE is it is valid UTF8; |
| 2299 | * false otherwise. But first, restore LC_TIME to its original |
| 2300 | * locale if we changed it */ |
| 2301 | if (save_time_locale) { |
| 2302 | setlocale(LC_TIME, save_time_locale); |
| 2303 | Safefree(save_time_locale); |
| 2304 | } |
| 2305 | |
| 2306 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?time-related strings for %s are UTF-8=%d\n", |
| 2307 | save_input_locale, |
| 2308 | is_utf8_string((U8 *) formatted_time, 0))); |
| 2309 | Safefree(save_input_locale); |
| 2310 | return is_utf8_string((U8 *) formatted_time, 0); |
| 2311 | } |
| 2312 | |
| 2313 | /* Falling off the end of the loop indicates all the names were just |
| 2314 | * ASCII. Go on to the next test. If we changed it, restore LC_TIME |
| 2315 | * to its original locale */ |
| 2316 | if (save_time_locale) { |
| 2317 | setlocale(LC_TIME, save_time_locale); |
| 2318 | Safefree(save_time_locale); |
| 2319 | } |
| 2320 | 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)); |
| 2321 | } |
| 2322 | cant_use_time: |
| 2323 | |
| 2324 | #endif |
| 2325 | |
| 2326 | #if 0 && defined(USE_LOCALE_MESSAGES) && defined(HAS_SYS_ERRLIST) |
| 2327 | |
| 2328 | /* This code is ifdefd out because it was found to not be necessary in testing |
| 2329 | * on our dromedary test machine, which has over 700 locales. There, this |
| 2330 | * added no value to looking at the currency symbol and the time strings. I |
| 2331 | * left it in so as to avoid rewriting it if real-world experience indicates |
| 2332 | * that dromedary is an outlier. Essentially, instead of returning abpve if we |
| 2333 | * haven't found illegal utf8, we continue on and examine all the strerror() |
| 2334 | * messages on the platform for utf8ness. If all are ASCII, we still don't |
| 2335 | * know the answer; but otherwise we have a pretty good indication of the |
| 2336 | * utf8ness. The reason this doesn't help much is that the messages may not |
| 2337 | * have been translated into the locale. The currency symbol and time strings |
| 2338 | * are much more likely to have been translated. */ |
| 2339 | { |
| 2340 | int e; |
| 2341 | bool is_utf8 = FALSE; |
| 2342 | bool non_ascii = FALSE; |
| 2343 | char *save_messages_locale = NULL; |
| 2344 | const char * errmsg = NULL; |
| 2345 | |
| 2346 | /* Like above, we set LC_MESSAGES to the locale of the desired |
| 2347 | * category, if it isn't that locale already */ |
| 2348 | |
| 2349 | if (category != LC_MESSAGES) { |
| 2350 | |
| 2351 | save_messages_locale = setlocale(LC_MESSAGES, NULL); |
| 2352 | if (! save_messages_locale) { |
| 2353 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2354 | "Could not find current locale for LC_MESSAGES\n")); |
| 2355 | goto cant_use_messages; |
| 2356 | } |
| 2357 | save_messages_locale = stdize_locale(savepv(save_messages_locale)); |
| 2358 | |
| 2359 | if (strEQ(save_messages_locale, save_input_locale)) { |
| 2360 | Safefree(save_messages_locale); |
| 2361 | save_messages_locale = NULL; |
| 2362 | } |
| 2363 | else if (! setlocale(LC_MESSAGES, save_input_locale)) { |
| 2364 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2365 | "Could not change LC_MESSAGES locale to %s\n", |
| 2366 | save_input_locale)); |
| 2367 | Safefree(save_messages_locale); |
| 2368 | goto cant_use_messages; |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | /* Here the current LC_MESSAGES is set to the locale of the category |
| 2373 | * whose information is desired. Look through all the messages. We |
| 2374 | * can't use Strerror() here because it may expand to code that |
| 2375 | * segfaults in miniperl */ |
| 2376 | |
| 2377 | for (e = 0; e <= sys_nerr; e++) { |
| 2378 | errno = 0; |
| 2379 | errmsg = sys_errlist[e]; |
| 2380 | if (errno || !errmsg) { |
| 2381 | break; |
| 2382 | } |
| 2383 | errmsg = savepv(errmsg); |
| 2384 | if (! is_utf8_invariant_string((U8 *) errmsg, 0)) { |
| 2385 | non_ascii = TRUE; |
| 2386 | is_utf8 = is_utf8_string((U8 *) errmsg, 0); |
| 2387 | break; |
| 2388 | } |
| 2389 | } |
| 2390 | Safefree(errmsg); |
| 2391 | |
| 2392 | /* And, if we changed it, restore LC_MESSAGES to its original locale */ |
| 2393 | if (save_messages_locale) { |
| 2394 | setlocale(LC_MESSAGES, save_messages_locale); |
| 2395 | Safefree(save_messages_locale); |
| 2396 | } |
| 2397 | |
| 2398 | if (non_ascii) { |
| 2399 | |
| 2400 | /* Any non-UTF-8 message means not a UTF-8 locale; if all are valid, |
| 2401 | * any non-ascii means it is one; otherwise we assume it isn't */ |
| 2402 | DEBUG_L(PerlIO_printf(Perl_debug_log, "\t?error messages for %s are UTF-8=%d\n", |
| 2403 | save_input_locale, |
| 2404 | is_utf8)); |
| 2405 | Safefree(save_input_locale); |
| 2406 | return is_utf8; |
| 2407 | } |
| 2408 | |
| 2409 | 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)); |
| 2410 | } |
| 2411 | cant_use_messages: |
| 2412 | |
| 2413 | #endif |
| 2414 | |
| 2415 | #endif /* the code that is compiled when no nl_langinfo */ |
| 2416 | |
| 2417 | #ifndef EBCDIC /* On os390, even if the name ends with "UTF-8', it isn't a |
| 2418 | UTF-8 locale */ |
| 2419 | /* As a last resort, look at the locale name to see if it matches |
| 2420 | * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the |
| 2421 | * return of setlocale(), is actually defined to be opaque, so we can't |
| 2422 | * really rely on the absence of various substrings in the name to indicate |
| 2423 | * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to |
| 2424 | * be a UTF-8 locale. Similarly for the other common names */ |
| 2425 | |
| 2426 | final_pos = strlen(save_input_locale) - 1; |
| 2427 | if (final_pos >= 3) { |
| 2428 | char *name = save_input_locale; |
| 2429 | |
| 2430 | /* Find next 'U' or 'u' and look from there */ |
| 2431 | while ((name += strcspn(name, "Uu") + 1) |
| 2432 | <= save_input_locale + final_pos - 2) |
| 2433 | { |
| 2434 | if (!isALPHA_FOLD_NE(*name, 't') |
| 2435 | || isALPHA_FOLD_NE(*(name + 1), 'f')) |
| 2436 | { |
| 2437 | continue; |
| 2438 | } |
| 2439 | name += 2; |
| 2440 | if (*(name) == '-') { |
| 2441 | if ((name > save_input_locale + final_pos - 1)) { |
| 2442 | break; |
| 2443 | } |
| 2444 | name++; |
| 2445 | } |
| 2446 | if (*(name) == '8') { |
| 2447 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2448 | "Locale %s ends with UTF-8 in name\n", |
| 2449 | save_input_locale)); |
| 2450 | Safefree(save_input_locale); |
| 2451 | return TRUE; |
| 2452 | } |
| 2453 | } |
| 2454 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2455 | "Locale %s doesn't end with UTF-8 in name\n", |
| 2456 | save_input_locale)); |
| 2457 | } |
| 2458 | #endif |
| 2459 | |
| 2460 | #ifdef WIN32 |
| 2461 | /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */ |
| 2462 | if (final_pos >= 4 |
| 2463 | && *(save_input_locale + final_pos - 0) == '1' |
| 2464 | && *(save_input_locale + final_pos - 1) == '0' |
| 2465 | && *(save_input_locale + final_pos - 2) == '0' |
| 2466 | && *(save_input_locale + final_pos - 3) == '5' |
| 2467 | && *(save_input_locale + final_pos - 4) == '6') |
| 2468 | { |
| 2469 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2470 | "Locale %s ends with 10056 in name, is UTF-8 locale\n", |
| 2471 | save_input_locale)); |
| 2472 | Safefree(save_input_locale); |
| 2473 | return TRUE; |
| 2474 | } |
| 2475 | #endif |
| 2476 | |
| 2477 | /* Other common encodings are the ISO 8859 series, which aren't UTF-8. But |
| 2478 | * since we are about to return FALSE anyway, there is no point in doing |
| 2479 | * this extra work */ |
| 2480 | #if 0 |
| 2481 | if (instr(save_input_locale, "8859")) { |
| 2482 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2483 | "Locale %s has 8859 in name, not UTF-8 locale\n", |
| 2484 | save_input_locale)); |
| 2485 | Safefree(save_input_locale); |
| 2486 | return FALSE; |
| 2487 | } |
| 2488 | #endif |
| 2489 | |
| 2490 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2491 | "Assuming locale %s is not a UTF-8 locale\n", |
| 2492 | save_input_locale)); |
| 2493 | Safefree(save_input_locale); |
| 2494 | return FALSE; |
| 2495 | } |
| 2496 | |
| 2497 | #endif |
| 2498 | |
| 2499 | |
| 2500 | bool |
| 2501 | Perl__is_in_locale_category(pTHX_ const bool compiling, const int category) |
| 2502 | { |
| 2503 | dVAR; |
| 2504 | /* Internal function which returns if we are in the scope of a pragma that |
| 2505 | * enables the locale category 'category'. 'compiling' should indicate if |
| 2506 | * this is during the compilation phase (TRUE) or not (FALSE). */ |
| 2507 | |
| 2508 | const COP * const cop = (compiling) ? &PL_compiling : PL_curcop; |
| 2509 | |
| 2510 | SV *categories = cop_hints_fetch_pvs(cop, "locale", 0); |
| 2511 | if (! categories || categories == &PL_sv_placeholder) { |
| 2512 | return FALSE; |
| 2513 | } |
| 2514 | |
| 2515 | /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get |
| 2516 | * a valid unsigned */ |
| 2517 | assert(category >= -1); |
| 2518 | return cBOOL(SvUV(categories) & (1U << (category + 1))); |
| 2519 | } |
| 2520 | |
| 2521 | char * |
| 2522 | Perl_my_strerror(pTHX_ const int errnum) |
| 2523 | { |
| 2524 | /* Returns a mortalized copy of the text of the error message associated |
| 2525 | * with 'errnum'. It uses the current locale's text unless the platform |
| 2526 | * doesn't have the LC_MESSAGES category or we are not being called from |
| 2527 | * within the scope of 'use locale'. In the former case, it uses whatever |
| 2528 | * strerror returns; in the latter case it uses the text from the C locale. |
| 2529 | * |
| 2530 | * The function just calls strerror(), but temporarily switches, if needed, |
| 2531 | * to the C locale */ |
| 2532 | |
| 2533 | char *errstr; |
| 2534 | |
| 2535 | #ifdef USE_LOCALE_MESSAGES /* If platform doesn't have messages category, we |
| 2536 | don't do any switching to the C locale; we just |
| 2537 | use whatever strerror() returns */ |
| 2538 | const bool within_locale_scope = IN_LC(LC_MESSAGES); |
| 2539 | |
| 2540 | dVAR; |
| 2541 | |
| 2542 | # ifdef USE_THREAD_SAFE_LOCALE |
| 2543 | locale_t save_locale = NULL; |
| 2544 | # else |
| 2545 | char * save_locale = NULL; |
| 2546 | bool locale_is_C = FALSE; |
| 2547 | |
| 2548 | /* We have a critical section to prevent another thread from changing the |
| 2549 | * locale out from under us (or zapping the buffer returned from |
| 2550 | * setlocale() ) */ |
| 2551 | LOCALE_LOCK; |
| 2552 | |
| 2553 | # endif |
| 2554 | |
| 2555 | if (! within_locale_scope) { |
| 2556 | errno = 0; |
| 2557 | |
| 2558 | # ifdef USE_THREAD_SAFE_LOCALE /* Use the thread-safe locale functions */ |
| 2559 | |
| 2560 | save_locale = uselocale(PL_C_locale_obj); |
| 2561 | if (! save_locale) { |
| 2562 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2563 | "uselocale failed, errno=%d\n", errno)); |
| 2564 | } |
| 2565 | |
| 2566 | # else /* Not thread-safe build */ |
| 2567 | |
| 2568 | save_locale = setlocale(LC_MESSAGES, NULL); |
| 2569 | if (! save_locale) { |
| 2570 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2571 | "setlocale failed, errno=%d\n", errno)); |
| 2572 | } |
| 2573 | else { |
| 2574 | locale_is_C = isNAME_C_OR_POSIX(save_locale); |
| 2575 | |
| 2576 | /* Switch to the C locale if not already in it */ |
| 2577 | if (! locale_is_C) { |
| 2578 | |
| 2579 | /* The setlocale() just below likely will zap 'save_locale', so |
| 2580 | * create a copy. */ |
| 2581 | save_locale = savepv(save_locale); |
| 2582 | setlocale(LC_MESSAGES, "C"); |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | # endif |
| 2587 | |
| 2588 | } /* end of ! within_locale_scope */ |
| 2589 | |
| 2590 | #endif |
| 2591 | |
| 2592 | errstr = Strerror(errnum); |
| 2593 | if (errstr) { |
| 2594 | errstr = savepv(errstr); |
| 2595 | SAVEFREEPV(errstr); |
| 2596 | } |
| 2597 | |
| 2598 | #ifdef USE_LOCALE_MESSAGES |
| 2599 | |
| 2600 | if (! within_locale_scope) { |
| 2601 | errno = 0; |
| 2602 | |
| 2603 | # ifdef USE_THREAD_SAFE_LOCALE |
| 2604 | |
| 2605 | if (save_locale && ! uselocale(save_locale)) { |
| 2606 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2607 | "uselocale restore failed, errno=%d\n", errno)); |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | # else |
| 2612 | |
| 2613 | if (save_locale && ! locale_is_C) { |
| 2614 | if (! setlocale(LC_MESSAGES, save_locale)) { |
| 2615 | DEBUG_L(PerlIO_printf(Perl_debug_log, |
| 2616 | "setlocale restore failed, errno=%d\n", errno)); |
| 2617 | } |
| 2618 | Safefree(save_locale); |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | LOCALE_UNLOCK; |
| 2623 | |
| 2624 | # endif |
| 2625 | #endif |
| 2626 | |
| 2627 | return errstr; |
| 2628 | } |
| 2629 | |
| 2630 | /* |
| 2631 | |
| 2632 | =head1 Locale-related functions and macros |
| 2633 | |
| 2634 | =for apidoc sync_locale |
| 2635 | |
| 2636 | Changing the program's locale should be avoided by XS code. Nevertheless, |
| 2637 | certain non-Perl libraries called from XS, such as C<Gtk> do so. When this |
| 2638 | happens, Perl needs to be told that the locale has changed. Use this function |
| 2639 | to do so, before returning to Perl. |
| 2640 | |
| 2641 | =cut |
| 2642 | */ |
| 2643 | |
| 2644 | void |
| 2645 | Perl_sync_locale(pTHX) |
| 2646 | { |
| 2647 | |
| 2648 | #ifdef USE_LOCALE_CTYPE |
| 2649 | new_ctype(setlocale(LC_CTYPE, NULL)); |
| 2650 | #endif /* USE_LOCALE_CTYPE */ |
| 2651 | |
| 2652 | #ifdef USE_LOCALE_COLLATE |
| 2653 | new_collate(setlocale(LC_COLLATE, NULL)); |
| 2654 | #endif |
| 2655 | |
| 2656 | #ifdef USE_LOCALE_NUMERIC |
| 2657 | set_numeric_local(); /* Switch from "C" to underlying LC_NUMERIC */ |
| 2658 | new_numeric(setlocale(LC_NUMERIC, NULL)); |
| 2659 | #endif /* USE_LOCALE_NUMERIC */ |
| 2660 | |
| 2661 | } |
| 2662 | |
| 2663 | #if defined(DEBUGGING) && defined(USE_LOCALE) |
| 2664 | |
| 2665 | char * |
| 2666 | Perl__setlocale_debug_string(const int category, /* category number, |
| 2667 | like LC_ALL */ |
| 2668 | const char* const locale, /* locale name */ |
| 2669 | |
| 2670 | /* return value from setlocale() when attempting to |
| 2671 | * set 'category' to 'locale' */ |
| 2672 | const char* const retval) |
| 2673 | { |
| 2674 | /* Returns a pointer to a NUL-terminated string in static storage with |
| 2675 | * added text about the info passed in. This is not thread safe and will |
| 2676 | * be overwritten by the next call, so this should be used just to |
| 2677 | * formulate a string to immediately print or savepv() on. */ |
| 2678 | |
| 2679 | /* initialise to a non-null value to keep it out of BSS and so keep |
| 2680 | * -DPERL_GLOBAL_STRUCT_PRIVATE happy */ |
| 2681 | static char ret[128] = "If you can read this, thank your buggy C" |
| 2682 | " library strlcpy(), and change your hints file" |
| 2683 | " to undef it"; |
| 2684 | my_strlcpy(ret, "setlocale(", sizeof(ret)); |
| 2685 | |
| 2686 | switch (category) { |
| 2687 | default: |
| 2688 | my_snprintf(ret, sizeof(ret), "%s? %d", ret, category); |
| 2689 | break; |
| 2690 | # ifdef LC_ALL |
| 2691 | case LC_ALL: |
| 2692 | my_strlcat(ret, "LC_ALL", sizeof(ret)); |
| 2693 | break; |
| 2694 | # endif |
| 2695 | # ifdef LC_CTYPE |
| 2696 | case LC_CTYPE: |
| 2697 | my_strlcat(ret, "LC_CTYPE", sizeof(ret)); |
| 2698 | break; |
| 2699 | # endif |
| 2700 | # ifdef LC_NUMERIC |
| 2701 | case LC_NUMERIC: |
| 2702 | my_strlcat(ret, "LC_NUMERIC", sizeof(ret)); |
| 2703 | break; |
| 2704 | # endif |
| 2705 | # ifdef LC_COLLATE |
| 2706 | case LC_COLLATE: |
| 2707 | my_strlcat(ret, "LC_COLLATE", sizeof(ret)); |
| 2708 | break; |
| 2709 | # endif |
| 2710 | # ifdef LC_TIME |
| 2711 | case LC_TIME: |
| 2712 | my_strlcat(ret, "LC_TIME", sizeof(ret)); |
| 2713 | break; |
| 2714 | # endif |
| 2715 | # ifdef LC_MONETARY |
| 2716 | case LC_MONETARY: |
| 2717 | my_strlcat(ret, "LC_MONETARY", sizeof(ret)); |
| 2718 | break; |
| 2719 | # endif |
| 2720 | # ifdef LC_MESSAGES |
| 2721 | case LC_MESSAGES: |
| 2722 | my_strlcat(ret, "LC_MESSAGES", sizeof(ret)); |
| 2723 | break; |
| 2724 | # endif |
| 2725 | } |
| 2726 | |
| 2727 | my_strlcat(ret, ", ", sizeof(ret)); |
| 2728 | |
| 2729 | if (locale) { |
| 2730 | my_strlcat(ret, "\"", sizeof(ret)); |
| 2731 | my_strlcat(ret, locale, sizeof(ret)); |
| 2732 | my_strlcat(ret, "\"", sizeof(ret)); |
| 2733 | } |
| 2734 | else { |
| 2735 | my_strlcat(ret, "NULL", sizeof(ret)); |
| 2736 | } |
| 2737 | |
| 2738 | my_strlcat(ret, ") returned ", sizeof(ret)); |
| 2739 | |
| 2740 | if (retval) { |
| 2741 | my_strlcat(ret, "\"", sizeof(ret)); |
| 2742 | my_strlcat(ret, retval, sizeof(ret)); |
| 2743 | my_strlcat(ret, "\"", sizeof(ret)); |
| 2744 | } |
| 2745 | else { |
| 2746 | my_strlcat(ret, "NULL", sizeof(ret)); |
| 2747 | } |
| 2748 | |
| 2749 | assert(strlen(ret) < sizeof(ret)); |
| 2750 | |
| 2751 | return ret; |
| 2752 | } |
| 2753 | |
| 2754 | #endif |
| 2755 | |
| 2756 | |
| 2757 | /* |
| 2758 | * ex: set ts=8 sts=4 sw=4 et: |
| 2759 | */ |