Perl_set_numeric_radix(pTHX)
{
#ifdef USE_LOCALE_NUMERIC
- dVAR;
# ifdef HAS_LOCALECONV
const struct lconv* const lc = localeconv();
PL_numeric_radix_sv = newSVpv(lc->decimal_point, 0);
if (! is_ascii_string((U8 *) lc->decimal_point, 0)
&& is_utf8_string((U8 *) lc->decimal_point, 0)
- && is_cur_LC_category_utf8(LC_NUMERIC))
+ && _is_cur_LC_category_utf8(LC_NUMERIC))
{
SvUTF8_on(PL_numeric_radix_sv);
}
#endif /* USE_LOCALE_NUMERIC */
}
+/* Is the C string input 'name' "C" or "POSIX"? If so, and 'name' is the
+ * return of setlocale(), then this is extremely likely to be the C or POSIX
+ * locale. However, the output of setlocale() is documented to be opaque, but
+ * the odds are extremely small that it would return these two strings for some
+ * other locale. Note that VMS in these two locales includes many non-ASCII
+ * characters as controls and punctuation (below are hex bytes):
+ * cntrl: 00-1F 7F 84-97 9B-9F
+ * 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
+ * Oddly, none there are listed as alphas, though some represent alphabetics
+ * http://www.nntp.perl.org/group/perl.perl5.porters/2013/02/msg198753.html */
+#define isNAME_C_OR_POSIX(name) ((name) != NULL \
+ && ((*(name) == 'C' && (*(name + 1)) == '\0') \
+ || strEQ((name), "POSIX")))
+
void
Perl_new_numeric(pTHX_ const char *newnum)
{
* This sets several interpreter-level variables:
* PL_numeric_name The default locale's name: a copy of 'newnum'
* PL_numeric_local A boolean indicating if the toggled state is such
- * that the current locale is the default locale
- * PL_numeric_standard A boolean indicating if the toggled state is such
- * that the current locale is the C locale
+ * that the current locale is the program's underlying
+ * locale
+ * PL_numeric_standard An int indicating if the toggled state is such
+ * that the current locale is the C locale. If non-zero,
+ * it is in C; if > 1, it means it may not be toggled away
+ * from C.
* Note that both of the last two variables can be true at the same time,
* if the underlying locale is C. (Toggling is a no-op under these
* circumstances.)
* POSIX::setlocale() */
char *save_newnum;
- dVAR;
if (! newnum) {
Safefree(PL_numeric_name);
PL_numeric_name = save_newnum;
}
- PL_numeric_standard = ((*save_newnum == 'C' && save_newnum[1] == '\0')
- || strEQ(save_newnum, "POSIX"));
+ PL_numeric_standard = isNAME_C_OR_POSIX(save_newnum);
PL_numeric_local = TRUE;
+
+ /* Keep LC_NUMERIC in the C locale. This is for XS modules, so they don't
+ * have to worry about the radix being a non-dot. (Core operations that
+ * need the underlying locale change to it temporarily). */
+ set_numeric_standard();
+
set_numeric_radix();
+#else
+ PERL_UNUSED_ARG(newnum);
#endif /* USE_LOCALE_NUMERIC */
}
Perl_set_numeric_standard(pTHX)
{
#ifdef USE_LOCALE_NUMERIC
- dVAR;
-
- /* Toggle the LC_NUMERIC locale to C, if not already there. Probably
- * should use the macros like SET_NUMERIC_STANDARD() in perl.h instead of
- * calling this directly. */
-
- if (! PL_numeric_standard) {
- setlocale(LC_NUMERIC, "C");
- PL_numeric_standard = TRUE;
- PL_numeric_local = FALSE;
- set_numeric_radix();
- }
+ /* Toggle the LC_NUMERIC locale to C. Most code should use the macros like
+ * SET_NUMERIC_STANDARD() in perl.h instead of calling this directly. The
+ * macro avoids calling this routine if toggling isn't necessary according
+ * to our records (which could be wrong if some XS code has changed the
+ * locale behind our back) */
+
+ setlocale(LC_NUMERIC, "C");
+ PL_numeric_standard = TRUE;
+ PL_numeric_local = isNAME_C_OR_POSIX(PL_numeric_name);
+ set_numeric_radix();
DEBUG_L(PerlIO_printf(Perl_debug_log,
"Underlying LC_NUMERIC locale now is C\n"));
Perl_set_numeric_local(pTHX)
{
#ifdef USE_LOCALE_NUMERIC
- dVAR;
-
- /* Toggle the LC_NUMERIC locale to the current underlying default, if not
- * already there. Probably should use the macros like SET_NUMERIC_LOCAL()
- * in perl.h instead of calling this directly. */
-
- if (! PL_numeric_local) {
- setlocale(LC_NUMERIC, PL_numeric_name);
- PL_numeric_standard = FALSE;
- PL_numeric_local = TRUE;
- set_numeric_radix();
- }
+ /* Toggle the LC_NUMERIC locale to the current underlying default. Most
+ * code should use the macros like SET_NUMERIC_LOCAL() in perl.h instead of
+ * calling this directly. The macro avoids calling this routine if
+ * toggling isn't necessary according to our records (which could be wrong
+ * if some XS code has changed the locale behind our back) */
+
+ setlocale(LC_NUMERIC, PL_numeric_name);
+ PL_numeric_standard = isNAME_C_OR_POSIX(PL_numeric_name);
+ PL_numeric_local = TRUE;
+ set_numeric_radix();
DEBUG_L(PerlIO_printf(Perl_debug_log,
"Underlying LC_NUMERIC locale now is %s\n",
PL_numeric_name));
PERL_ARGS_ASSERT_NEW_CTYPE;
- PL_in_utf8_CTYPE_locale = is_cur_LC_category_utf8(LC_CTYPE);
+ PL_in_utf8_CTYPE_locale = _is_cur_LC_category_utf8(LC_CTYPE);
/* A UTF-8 locale gets standard rules. But note that code still has to
* handle this specially because of the three problematic code points */
* should be called directly only from this file and from
* POSIX::setlocale() */
- dVAR;
-
if (! newcoll) {
if (PL_collation_name) {
++PL_collation_ix;
++PL_collation_ix;
Safefree(PL_collation_name);
PL_collation_name = stdize_locale(savepv(newcoll));
- PL_collation_standard = ((*newcoll == 'C' && newcoll[1] == '\0')
- || strEQ(newcoll, "POSIX"));
+ PL_collation_standard = isNAME_C_OR_POSIX(newcoll);
{
/* 2: at most so many chars ('a', 'b'). */
}
}
+#else
+ PERL_UNUSED_ARG(newcoll);
#endif /* USE_LOCALE_COLLATE */
}
int ok = 1;
#if defined(USE_LOCALE)
- dVAR;
-
#ifdef USE_LOCALE_CTYPE
char *curctype = NULL;
#endif /* USE_LOCALE_CTYPE */
(printwarn &&
(!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))));
bool done = FALSE;
+#ifdef WIN32
+ /* In some systems you can find out the system default locale
+ * and use that as the fallback locale. */
+# define SYSTEM_DEFAULT_LOCALE
+#endif
+#ifdef SYSTEM_DEFAULT_LOCALE
const char *system_default_locale = NULL;
-
+#endif
#ifndef LOCALE_ENVIRON_REQUIRED
PERL_UNUSED_VAR(done);
* sense */
setlocale_failure = FALSE;
-#ifdef WIN32
-
+#ifdef SYSTEM_DEFAULT_LOCALE
+# ifdef WIN32
/* On Windows machines, an entry of "" after the 0th means to use
* the system default locale, which we now proceed to get. */
if (strEQ(trial_locale, "")) {
trial_locale = system_default_locale;
}
-#endif
+# endif /* WIN32 */
+#endif /* SYSTEM_DEFAULT_LOCALE */
}
#ifdef LC_ALL
description = "the standard locale";
name = "C";
}
+#ifdef SYSTEM_DEFAULT_LOCALE
else if (strEQ(trial_locales[i], "")) {
description = "the system default locale";
if (system_default_locale) {
name = system_default_locale;
}
}
+#endif /* SYSTEM_DEFAULT_LOCALE */
else {
description = "a fallback locale";
name = trial_locales[i];
* $ENV{PERL_UNICODE}) are true, perl.c:S_parse_body() will turn on the
* PerlIO :utf8 layer on STDIN, STDOUT, STDERR, _and_ the default open
* discipline. */
- PL_utf8locale = is_cur_LC_category_utf8(LC_CTYPE);
+ PL_utf8locale = _is_cur_LC_category_utf8(LC_CTYPE);
/* Set PL_unicode to $ENV{PERL_UNICODE} if using PerlIO.
This is an alternative to using the -C command line switch
Safefree(curnum);
#endif /* USE_LOCALE_NUMERIC */
+#else /* !USE_LOCALE */
+ PERL_UNUSED_ARG(printwarn);
#endif /* USE_LOCALE */
return ok;
char *
Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
{
- dVAR;
char *xbuf;
STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
#ifdef USE_LOCALE
-STATIC bool
-S_is_cur_LC_category_utf8(pTHX_ int category)
+bool
+Perl__is_cur_LC_category_utf8(pTHX_ int category)
{
/* Returns TRUE if the current locale for 'category' is UTF-8; FALSE
* otherwise. 'category' may not be LC_ALL. If the platform doesn't have
return FALSE; /* XXX maybe should croak */
}
save_input_locale = stdize_locale(savepv(save_input_locale));
- if ((*save_input_locale == 'C' && save_input_locale[1] == '\0')
- || strEQ(save_input_locale, "POSIX"))
- {
+ if (isNAME_C_OR_POSIX(save_input_locale)) {
DEBUG_L(PerlIO_printf(Perl_debug_log,
"Current locale for category %d is %s\n",
category, save_input_locale));
if (category != LC_CTYPE) { /* These work only on LC_CTYPE */
/* Get the current LC_CTYPE locale */
- save_ctype_locale = stdize_locale(savepv(setlocale(LC_CTYPE, NULL)));
+ save_ctype_locale = setlocale(LC_CTYPE, NULL);
if (! save_ctype_locale) {
DEBUG_L(PerlIO_printf(Perl_debug_log,
"Could not find current locale for LC_CTYPE\n"));
goto cant_use_nllanginfo;
}
+ save_ctype_locale = stdize_locale(savepv(save_ctype_locale));
/* If LC_CTYPE and the desired category use the same locale, this
* means that finding the value for LC_CTYPE is the same as finding
# if defined(HAS_NL_LANGINFO) && defined(CODESET)
{
- char *codeset = savepv(nl_langinfo(CODESET));
+ char *codeset = nl_langinfo(CODESET);
if (codeset && strNE(codeset, "")) {
+ codeset = savepv(codeset);
/* If we switched LC_CTYPE, switch back */
if (save_ctype_locale) {
(void) mbtowc(&wc, NULL, 0); /* Reset any shift state */
GCC_DIAG_RESTORE;
errno = 0;
- if (mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
+ if ((size_t)mbtowc(&wc, HYPHEN_UTF8, strlen(HYPHEN_UTF8))
!= strlen(HYPHEN_UTF8)
|| wc != (wchar_t) 0x2010)
{
cant_use_nllanginfo:
-#endif /* HAS_NL_LANGINFO etc */
-
- /* nl_langinfo not available or failed somehow. Look at the locale name to
- * see if it matches qr/UTF -? 8 /ix */
-
- final_pos = strlen(save_input_locale) - 1;
- if (final_pos >= 3) {
- char *name = save_input_locale;
-
- /* Find next 'U' or 'u' and look from there */
- while ((name += strcspn(name, "Uu") + 1)
- <= save_input_locale + final_pos - 2)
- {
- if (toFOLD(*(name)) != 't'
- || toFOLD(*(name + 1)) != 'f')
- {
- continue;
- }
- name += 2;
- if (*(name) == '-') {
- if ((name > save_input_locale + final_pos - 1)) {
- break;
- }
- name++;
- }
- if (*(name) == '8') {
- Safefree(save_input_locale);
- DEBUG_L(PerlIO_printf(Perl_debug_log,
- "Locale %s ends with UTF-8 in name\n",
- save_input_locale));
- return TRUE;
- }
- }
- DEBUG_L(PerlIO_printf(Perl_debug_log,
- "Locale %s doesn't end with UTF-8 in name\n",
- save_input_locale));
- }
-
-#ifdef WIN32
- /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
- if (final_pos >= 4
- && *(save_input_locale + final_pos - 0) == '1'
- && *(save_input_locale + final_pos - 1) == '0'
- && *(save_input_locale + final_pos - 2) == '0'
- && *(save_input_locale + final_pos - 3) == '5'
- && *(save_input_locale + final_pos - 4) == '6')
- {
- Safefree(save_input_locale);
- DEBUG_L(PerlIO_printf(Perl_debug_log,
- "Locale %s ends with 10056 in name, is UTF-8 locale\n",
- save_input_locale));
- return TRUE;
- }
-#endif
+#else /* nl_langinfo should work if available, so don't bother compiling this
+ fallback code. The final fallback of looking at the name is
+ compiled, and will be executed if nl_langinfo fails */
- /* Other common encodings are the ISO 8859 series, which aren't UTF-8 */
- if (instr(save_input_locale, "8859")) {
- Safefree(save_input_locale);
- DEBUG_L(PerlIO_printf(Perl_debug_log,
- "Locale %s has 8859 in name, not UTF-8 locale\n",
- save_input_locale));
- return FALSE;
- }
+ /* nl_langinfo not available or failed somehow. Next try looking at the
+ * currency symbol to see if it disambiguates things. Often that will be
+ * in the native script, and if the symbol isn't in UTF-8, we know that the
+ * locale isn't. If it is non-ASCII UTF-8, we infer that the locale is
+ * too. */
#ifdef HAS_LOCALECONV
-
# ifdef USE_LOCALE_MONETARY
-
- /* Here, there is nothing in the locale name to indicate whether the locale
- * is UTF-8 or not. This "name", the return of setlocale(), is actually
- * defined to be opaque, so we can't really rely on the absence of various
- * substrings in the name to indicate its UTF-8ness. Look at the locale's
- * currency symbol. Often that will be in the native script, and if the
- * symbol isn't in UTF-8, we know that the locale isn't. If it is
- * non-ASCII UTF-8, we infer that the locale is too.
- * To do this, like above for LC_CTYPE, we first set LC_MONETARY to the
- * locale of the desired category, if it isn't that locale already */
-
{
char *save_monetary_locale = NULL;
bool illegal_utf8 = FALSE;
bool only_ascii = FALSE;
const struct lconv* const lc = localeconv();
+ /* Like above for LC_CTYPE, we first set LC_MONETARY to the locale of
+ * the desired category, if it isn't that locale already */
+
if (category != LC_MONETARY) {
- save_monetary_locale = stdize_locale(savepv(setlocale(LC_MONETARY,
- NULL)));
+ save_monetary_locale = setlocale(LC_MONETARY, NULL);
if (! save_monetary_locale) {
DEBUG_L(PerlIO_printf(Perl_debug_log,
"Could not find current locale for LC_MONETARY\n"));
goto cant_use_monetary;
}
+ save_monetary_locale = stdize_locale(savepv(save_monetary_locale));
if (strNE(save_monetary_locale, save_input_locale)) {
if (! setlocale(LC_MONETARY, save_input_locale)) {
Safefree(save_monetary_locale);
}
+ if (only_ascii) {
+ goto cant_use_monetary;
+ }
+
Safefree(save_input_locale);
/* It isn't a UTF-8 locale if the symbol is not legal UTF-8; otherwise
* UTF-8. (We can't really tell if the locale is UTF-8 or not if the
* symbol is just a '$', so we err on the side of it not being UTF-8)
* */
- DEBUG_L(PerlIO_printf(Perl_debug_log, "\tis_utf8=%d\n", (illegal_utf8)
- ? FALSE
- : ! only_ascii));
- return (illegal_utf8)
- ? FALSE
- : ! only_ascii;
-
+ DEBUG_L(PerlIO_printf(Perl_debug_log, "\tis_utf8=%d\n", ! illegal_utf8));
+ return ! illegal_utf8;
}
cant_use_monetary:
#endif
+#endif /* the code that is compiled when no nl_langinfo */
+
+ /* As a last resort, look at the locale name to see if it matches
+ * qr/UTF -? * 8 /ix, or some other common locale names. This "name", the
+ * return of setlocale(), is actually defined to be opaque, so we can't
+ * really rely on the absence of various substrings in the name to indicate
+ * its UTF-8ness, but if it has UTF8 in the name, it is extremely likely to
+ * be a UTF-8 locale. Similarly for the other common names */
+
+ final_pos = strlen(save_input_locale) - 1;
+ if (final_pos >= 3) {
+ char *name = save_input_locale;
+
+ /* Find next 'U' or 'u' and look from there */
+ while ((name += strcspn(name, "Uu") + 1)
+ <= save_input_locale + final_pos - 2)
+ {
+ if (toFOLD(*(name)) != 't'
+ || toFOLD(*(name + 1)) != 'f')
+ {
+ continue;
+ }
+ name += 2;
+ if (*(name) == '-') {
+ if ((name > save_input_locale + final_pos - 1)) {
+ break;
+ }
+ name++;
+ }
+ if (*(name) == '8') {
+ Safefree(save_input_locale);
+ DEBUG_L(PerlIO_printf(Perl_debug_log,
+ "Locale %s ends with UTF-8 in name\n",
+ save_input_locale));
+ return TRUE;
+ }
+ }
+ DEBUG_L(PerlIO_printf(Perl_debug_log,
+ "Locale %s doesn't end with UTF-8 in name\n",
+ save_input_locale));
+ }
+
+#ifdef WIN32
+ /* http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756.aspx */
+ if (final_pos >= 4
+ && *(save_input_locale + final_pos - 0) == '1'
+ && *(save_input_locale + final_pos - 1) == '0'
+ && *(save_input_locale + final_pos - 2) == '0'
+ && *(save_input_locale + final_pos - 3) == '5'
+ && *(save_input_locale + final_pos - 4) == '6')
+ {
+ DEBUG_L(PerlIO_printf(Perl_debug_log,
+ "Locale %s ends with 10056 in name, is UTF-8 locale\n",
+ save_input_locale));
+ Safefree(save_input_locale);
+ return TRUE;
+ }
+#endif
+
+ /* Other common encodings are the ISO 8859 series, which aren't UTF-8. But
+ * since we are about to return FALSE anyway, there is no point in doing
+ * this extra work */
+#if 0
+ if (instr(save_input_locale, "8859")) {
+ DEBUG_L(PerlIO_printf(Perl_debug_log,
+ "Locale %s has 8859 in name, not UTF-8 locale\n",
+ save_input_locale));
+ Safefree(save_input_locale);
+ return FALSE;
+ }
+#endif
+
DEBUG_L(PerlIO_printf(Perl_debug_log,
"Assuming locale %s is not a UTF-8 locale\n",
save_input_locale));
#endif
+
+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). */
+
+ const COP * const cop = (compiling) ? &PL_compiling : PL_curcop;
+
+ SV *categories = cop_hints_fetch_pvs(cop, "locale", 0);
+ if (! categories || categories == &PL_sv_placeholder) {
+ return FALSE;
+ }
+
+ /* The pseudo-category 'not_characters' is -1, so just add 1 to each to get
+ * a valid unsigned */
+ assert(category >= -1);
+ return cBOOL(SvUV(categories) & (1U << (category + 1)));
+}
+
+char *
+Perl_my_strerror(pTHX_ const int errnum) {
+
+ /* Uses C locale for the error text unless within scope of 'use locale' for
+ * LC_MESSAGES */
+
+#ifdef USE_LOCALE_MESSAGES
+ if (! IN_LC(LC_MESSAGES)) {
+ char * save_locale = setlocale(LC_MESSAGES, NULL);
+ if (! isNAME_C_OR_POSIX(save_locale)) {
+ char *errstr;
+
+ /* The next setlocale likely will zap this, so create a copy */
+ save_locale = savepv(save_locale);
+
+ setlocale(LC_MESSAGES, "C");
+
+ /* This points to the static space in Strerror, with all its
+ * limitations */
+ errstr = Strerror(errnum);
+
+ setlocale(LC_MESSAGES, save_locale);
+ Safefree(save_locale);
+ return errstr;
+ }
+ }
+#endif
+
+ return Strerror(errnum);
+}
+
/*
* Local variables:
* c-indentation-style: bsd