X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/6c3320363f6cd734c66a25852aac87e4f2538215..b8a2649a502a2760c6bc90d32f71a03c232eee81:/locale.c?ds=sidebyside diff --git a/locale.c b/locale.c index 0029c80..5d6566f 100644 --- a/locale.c +++ b/locale.c @@ -60,7 +60,7 @@ /* If the environment says to, we can output debugging information during * initialization. This is done before option parsing, and before any thread * creation, so can be a file-level static */ -#if ! defined(DEBUGGING) || defined(PERL_GLOBAL_STRUCT) +#if ! defined(DEBUGGING) # define debug_initialization 0 # define DEBUG_INITIALIZATION_set(v) #else @@ -200,6 +200,12 @@ const int categories[] = { # ifdef USE_LOCALE_TELEPHONE LC_TELEPHONE, # endif +# ifdef USE_LOCALE_SYNTAX + LC_SYNTAX, +# endif +# ifdef USE_LOCALE_TOD + LC_TOD, +# endif # ifdef LC_ALL LC_ALL, # endif @@ -245,6 +251,12 @@ const char * const category_names[] = { # ifdef USE_LOCALE_TELEPHONE "LC_TELEPHONE", # endif +# ifdef USE_LOCALE_SYNTAX + "LC_SYNTAX", +# endif +# ifdef USE_LOCALE_TOD + "LC_TOD", +# endif # ifdef LC_ALL "LC_ALL", # endif @@ -384,8 +396,20 @@ S_category_name(const int category) # else # define _DUMMY_TELEPHONE _DUMMY_PAPER # endif +# ifdef USE_LOCALE_SYNTAX +# define LC_SYNTAX_INDEX _DUMMY_TELEPHONE + 1 +# define _DUMMY_SYNTAX LC_SYNTAX_INDEX +# else +# define _DUMMY_SYNTAX _DUMMY_TELEPHONE +# endif +# ifdef USE_LOCALE_TOD +# define LC_TOD_INDEX _DUMMY_SYNTAX + 1 +# define _DUMMY_TOD LC_TOD_INDEX +# else +# define _DUMMY_TOD _DUMMY_SYNTAX +# endif # ifdef LC_ALL -# define LC_ALL_INDEX _DUMMY_TELEPHONE + 1 +# define LC_ALL_INDEX _DUMMY_TOD + 1 # endif #endif /* ifdef USE_LOCALE */ @@ -402,6 +426,7 @@ S_category_name(const int category) * known at compile time; "do_setlocale_r", not known until run time */ # define do_setlocale_c(cat, locale) my_setlocale(cat, locale) # define do_setlocale_r(cat, locale) my_setlocale(cat, locale) +# define FIX_GLIBC_LC_MESSAGES_BUG(i) #else /* Below uses POSIX 2008 */ @@ -415,6 +440,22 @@ S_category_name(const int category) emulate_setlocale(cat, locale, cat ## _INDEX, TRUE) # define do_setlocale_r(cat, locale) emulate_setlocale(cat, locale, 0, FALSE) +# if ! defined(__GLIBC__) || ! defined(USE_LOCALE_MESSAGES) + +# define FIX_GLIBC_LC_MESSAGES_BUG(i) + +# else /* Invalidate glibc cache of loaded translations, see [perl #134264] */ + +# include +# define FIX_GLIBC_LC_MESSAGES_BUG(i) \ + STMT_START { \ + if ((i) == LC_MESSAGES_INDEX) { \ + textdomain(textdomain(NULL)); \ + } \ + } STMT_END + +# endif + /* A third array, parallel to the ones above to map from category to its * equivalent mask */ const int category_masks[] = { @@ -451,6 +492,12 @@ const int category_masks[] = { # ifdef USE_LOCALE_TELEPHONE LC_TELEPHONE_MASK, # endif +# ifdef USE_LOCALE_SYNTAX + LC_SYNTAX_MASK, +# endif +# ifdef USE_LOCALE_TOD + LC_TOD_MASK, +# endif /* LC_ALL can't be turned off by a Configure * option, and in Posix 2008, should always be * here, so compile it in unconditionally. @@ -774,16 +821,6 @@ S_emulate_setlocale(const int category, if (! default_name || strEQ(default_name, "")) { default_name = "C"; } - else if (PL_scopestack_ix != 0) { - /* To minimize other threads messing with the environment, - * we copy the variable, making it a temporary. But this - * doesn't work upon program initialization before any - * scopes are created, and at this time, there's nothing - * else going on that would interfere. So skip the copy - * in that case */ - default_name = savepv(default_name); - SAVEFREEPV(default_name); - } if (category != LC_ALL) { const char * const name = PerlEnv_getenv(category_names[index]); @@ -818,22 +855,19 @@ S_emulate_setlocale(const int category, for (i = 0; i < LC_ALL_INDEX; i++) { const char * const env_override - = savepv(PerlEnv_getenv(category_names[i])); + = PerlEnv_getenv(category_names[i]); const char * this_locale = ( env_override && strNE(env_override, "")) ? env_override : default_name; if (! emulate_setlocale(categories[i], this_locale, i, TRUE)) { - Safefree(env_override); return NULL; } if (strNE(this_locale, default_name)) { did_override = TRUE; } - - Safefree(env_override); } /* If all the categories are the same, we can set LC_ALL to @@ -1158,6 +1192,8 @@ S_emulate_setlocale(const int category, Safefree(PL_curlocales[i]); PL_curlocales[i] = savepv(locale); } + + FIX_GLIBC_LC_MESSAGES_BUG(LC_MESSAGES_INDEX); } else { @@ -1172,6 +1208,8 @@ S_emulate_setlocale(const int category, /* Then update the category's record */ Safefree(PL_curlocales[index]); PL_curlocales[index] = savepv(locale); + + FIX_GLIBC_LC_MESSAGES_BUG(index); } # endif @@ -1550,7 +1588,6 @@ S_new_ctype(pTHX_ const char *newctype) * this function should be called directly only from this file and from * POSIX::setlocale() */ - dVAR; unsigned int i; /* Don't check for problems if we are suppressing the warnings */ @@ -1705,13 +1742,13 @@ S_new_ctype(pTHX_ const char *newctype) "isxdigit('%s') unexpectedly is %d\n", name, cBOOL(isxdigit(i)))); } - if (UNLIKELY(tolower(i) != (int) toLOWER_A(i))) { + if (UNLIKELY(tolower(i) != (int) toLOWER_A(i))) { is_bad = TRUE; DEBUG_L(PerlIO_printf(Perl_debug_log, "tolower('%s')=0x%x instead of the expected 0x%x\n", name, tolower(i), (int) toLOWER_A(i))); } - if (UNLIKELY(toupper(i) != (int) toUPPER_A(i))) { + if (UNLIKELY(toupper(i) != (int) toUPPER_A(i))) { is_bad = TRUE; DEBUG_L(PerlIO_printf(Perl_debug_log, "toupper('%s')=0x%x instead of the expected 0x%x\n", @@ -2218,9 +2255,9 @@ S_win32_setlocale(pTHX_ int category, const char* locale) result = PerlEnv_getenv(category_names[i]); if (result && strNE(result, "")) { #ifdef USE_WSETLOCALE - S_wrap_wsetlocale(aTHX_ categories[i], locale); + S_wrap_wsetlocale(aTHX_ categories[i], result); #else - setlocale(categories[i], locale); + setlocale(categories[i], result); #endif DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s:%d: %s\n", __FILE__, __LINE__, @@ -2244,7 +2281,7 @@ S_win32_setlocale(pTHX_ int category, const char* locale) /* -=head1 Locale-related functions and macros +=for apidoc_section Locales =for apidoc Perl_setlocale @@ -3278,7 +3315,6 @@ Perl_init_i18nl10n(pTHX_ int printwarn) * values for our db, instead of trying to change them. * */ - dVAR; int ok = 1; @@ -3289,7 +3325,7 @@ Perl_init_i18nl10n(pTHX_ int printwarn) #else /* USE_LOCALE */ # ifdef __GLIBC__ - const char * const language = savepv(PerlEnv_getenv("LANGUAGE")); + const char * const language = PerlEnv_getenv("LANGUAGE"); # endif @@ -3299,8 +3335,8 @@ Perl_init_i18nl10n(pTHX_ int printwarn) : ""; const char* trial_locales[5]; /* 5 = 1 each for "", LC_ALL, LANG, "", C */ unsigned int trial_locales_count; - const char * const lc_all = savepv(PerlEnv_getenv("LC_ALL")); - const char * const lang = savepv(PerlEnv_getenv("LANG")); + const char * const lc_all = PerlEnv_getenv("LC_ALL"); + const char * const lang = PerlEnv_getenv("LANG"); bool setlocale_failure = FALSE; unsigned int i; @@ -3430,6 +3466,20 @@ Perl_init_i18nl10n(pTHX_ int printwarn) assert(category_masks[LC_TELEPHONE_INDEX] == LC_TELEPHONE_MASK); # endif # endif +# ifdef USE_LOCALE_SYNTAX + assert(categories[LC_SYNTAX_INDEX] == LC_SYNTAX); + assert(strEQ(category_names[LC_SYNTAX_INDEX], "LC_SYNTAX")); +# ifdef USE_POSIX_2008_LOCALE + assert(category_masks[LC_SYNTAX_INDEX] == LC_SYNTAX_MASK); +# endif +# endif +# ifdef USE_LOCALE_TOD + assert(categories[LC_TOD_INDEX] == LC_TOD); + assert(strEQ(category_names[LC_TOD_INDEX], "LC_TOD")); +# ifdef USE_POSIX_2008_LOCALE + assert(category_masks[LC_TOD_INDEX] == LC_TOD_MASK); +# endif +# endif # ifdef LC_ALL assert(categories[LC_ALL_INDEX] == LC_ALL); assert(strEQ(category_names[LC_ALL_INDEX], "LC_ALL")); @@ -3440,6 +3490,18 @@ Perl_init_i18nl10n(pTHX_ int printwarn) # endif # endif /* DEBUGGING */ + /* Initialize the per-thread mbrFOO() state variables. See POSIX.xs for + * why these particular incantations are used. */ +#ifdef HAS_MBRLEN + memzero(&PL_mbrlen_ps, sizeof(PL_mbrlen_ps)); +#endif +#ifdef HAS_MBRTOWC + memzero(&PL_mbrtowc_ps, sizeof(PL_mbrtowc_ps)); +#endif +#ifdef HAS_WCTOMBR + wcrtomb(NULL, L'\0', &PL_wcrtomb_ps); +#endif + /* Initialize the cache of the program's UTF-8ness for the always known * locales C and POSIX */ my_strlcpy(PL_locale_utf8ness, C_and_POSIX_utf8ness, @@ -3876,15 +3938,6 @@ Perl_init_i18nl10n(pTHX_ int printwarn) } # endif -# ifdef __GLIBC__ - - Safefree(language); - -# endif - - Safefree(lc_all); - Safefree(lang); - #endif /* USE_LOCALE */ #ifdef DEBUGGING @@ -4537,7 +4590,7 @@ S_switch_category_locale_to_template(pTHX_ const int switch_category, const int Safefree(restore_to_locale); DEBUG_Lv(PerlIO_printf(Perl_debug_log, "%s locale unchanged as %s\n", - category_name(switch_category), restore_to_locale)); + category_name(switch_category), template_locale)); return NULL; } @@ -5194,7 +5247,6 @@ Perl__is_cur_LC_category_utf8(pTHX_ int category) bool Perl__is_in_locale_category(pTHX_ const bool compiling, const int category) { - dVAR; /* Internal function which returns if we are in the scope of a pragma that * enables the locale category 'category'. 'compiling' should indicate if * this is during the compilation phase (TRUE) or not (FALSE). */ @@ -5225,7 +5277,6 @@ Perl_my_strerror(pTHX_ const int errnum) * to the C locale */ char *errstr; - dVAR; #ifndef USE_LOCALE_MESSAGES @@ -5253,9 +5304,9 @@ Perl_my_strerror(pTHX_ const int errnum) Safefree(save_locale); } -# elif defined(HAS_POSIX_2008_LOCALE) \ - && defined(HAS_STRERROR_L) \ - && defined(HAS_DUPLOCALE) +# elif defined(USE_POSIX_2008_LOCALE) \ + && defined(HAS_STRERROR_L) \ + && defined(HAS_DUPLOCALE) /* This function is also trivial if we don't have to worry about thread * safety and have strerror_l(), as it handles the switch of locales so we @@ -5587,11 +5638,7 @@ S_setlocale_debug_string(const int category, /* category number, * be overwritten by the next call, so this should be used just to * formulate a string to immediately print or savepv() on. */ - /* initialise to a non-null value to keep it out of BSS and so keep - * -DPERL_GLOBAL_STRUCT_PRIVATE happy */ - static char ret[256] = "If you can read this, thank your buggy C" - " library strlcpy(), and change your hints file" - " to undef it"; + static char ret[256]; my_strlcpy(ret, "setlocale(", sizeof(ret)); my_strlcat(ret, category_name(category), sizeof(ret)); @@ -5666,7 +5713,6 @@ Perl_thread_locale_term() # ifndef WIN32 { /* Free up */ - dVAR; locale_t cur_obj = uselocale(LC_GLOBAL_LOCALE); if (cur_obj != LC_GLOBAL_LOCALE && cur_obj != PL_C_locale_obj) { freelocale(cur_obj);