X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Flocalename.c;h=185f51580a1e8adfb32d97d78a95b1e26adb5da7;hb=23eecb48e39afd0d267d64d40ba6bf97aa865e13;hp=d2554fd96967924faf2f606336fcc3549cef6528;hpb=b2e2010c7c902235b5efb5bd3c6529f61b093aa4;p=gnulib.git diff --git a/lib/localename.c b/lib/localename.c index d2554fd96..185f51580 100644 --- a/lib/localename.c +++ b/lib/localename.c @@ -1,24 +1,22 @@ /* Determine name of the currently selected locale. - Copyright (C) 1995-2010 Free Software Foundation, Inc. + Copyright (C) 1995-2013 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU Library General Public License as published - by the Free Software Foundation; either version 2, or (at your option) - any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. - You should have received a copy of the GNU Library General Public - License along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - USA. */ + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ /* Written by Ulrich Drepper , 1995. */ -/* Win32 code written by Tor Lillqvist . */ -/* MacOS X code written by Bruno Haible . */ +/* Native Windows code written by Tor Lillqvist . */ +/* Mac OS X code written by Bruno Haible . */ #include @@ -36,7 +34,7 @@ #include #if HAVE_USELOCALE -/* MacOS X 10.5 defines the locale_t type in . */ +/* Mac OS X 10.5 defines the locale_t type in . */ # if defined __APPLE__ && defined __MACH__ # include # endif @@ -56,10 +54,10 @@ #endif #if defined _WIN32 || defined __WIN32__ -# define WIN32_NATIVE +# define WINDOWS_NATIVE #endif -#if defined WIN32_NATIVE || defined __CYGWIN__ /* WIN32 or Cygwin */ +#if defined WINDOWS_NATIVE || defined __CYGWIN__ /* Native Windows or Cygwin */ # define WIN32_LEAN_AND_MEAN # include /* List of language codes, sorted by value: @@ -1130,11 +1128,11 @@ #if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE -/* MacOS X 10.2 or newer */ +/* Mac OS X 10.2 or newer */ -/* Canonicalize a MacOS X locale name to a Unix locale name. +/* Canonicalize a Mac OS X locale name to a Unix locale name. NAME is a sufficiently large buffer. - On input, it contains the MacOS X locale name. + On input, it contains the Mac OS X locale name. On output, it contains the Unix locale name. */ # if !defined IN_LIBINTL static @@ -1147,9 +1145,9 @@ gl_locale_name_canonicalize (char *name) http://lists.apple.com/archives/carbon-dev/2005/Mar/msg00293.html */ /* Convert legacy (NeXTstep inherited) English names to Unix (ISO 639 and - ISO 3166) names. Prior to MacOS X 10.3, there is no API for doing this. + ISO 3166) names. Prior to Mac OS X 10.3, there is no API for doing this. Therefore we do it ourselves, using a table based on the results of the - MacOS X 10.3.8 function + Mac OS X 10.3.8 function CFLocaleCreateCanonicalLocaleIdentifierFromString(). */ typedef struct { const char legacy[21+1]; const char unixy[5+1]; } legacy_entry; @@ -1292,26 +1290,26 @@ gl_locale_name_canonicalize (char *name) typedef struct { const char langtag[7+1]; const char unixy[12+1]; } langtag_entry; static const langtag_entry langtag_table[] = { - /* MacOS X has "az-Arab", "az-Cyrl", "az-Latn". + /* Mac OS X has "az-Arab", "az-Cyrl", "az-Latn". The default script for az on Unix is Latin. */ { "az-Latn", "az" }, - /* MacOS X has "ga-dots". Does not yet exist on Unix. */ + /* Mac OS X has "ga-dots". Does not yet exist on Unix. */ { "ga-dots", "ga" }, - /* MacOS X has "kk-Cyrl". Does not yet exist on Unix. */ - /* MacOS X has "mn-Cyrl", "mn-Mong". + /* Mac OS X has "kk-Cyrl". Does not yet exist on Unix. */ + /* Mac OS X has "mn-Cyrl", "mn-Mong". The default script for mn on Unix is Cyrillic. */ { "mn-Cyrl", "mn" }, - /* MacOS X has "ms-Arab", "ms-Latn". + /* Mac OS X has "ms-Arab", "ms-Latn". The default script for ms on Unix is Latin. */ { "ms-Latn", "ms" }, - /* MacOS X has "tg-Cyrl". + /* Mac OS X has "tg-Cyrl". The default script for tg on Unix is Cyrillic. */ { "tg-Cyrl", "tg" }, - /* MacOS X has "tk-Cyrl". Does not yet exist on Unix. */ - /* MacOS X has "tt-Cyrl". + /* Mac OS X has "tk-Cyrl". Does not yet exist on Unix. */ + /* Mac OS X has "tt-Cyrl". The default script for tt on Unix is Cyrillic. */ { "tt-Cyrl", "tt" }, - /* MacOS X has "zh-Hans", "zh-Hant". + /* Mac OS X has "zh-Hans", "zh-Hant". Country codes are used to distinguish these on Unix. */ { "zh-Hans", "zh_CN" }, { "zh-Hant", "zh_TW" } @@ -1407,11 +1405,11 @@ gl_locale_name_canonicalize (char *name) #endif -#if defined WIN32_NATIVE || defined __CYGWIN__ /* WIN32 or Cygwin */ +#if defined WINDOWS_NATIVE || defined __CYGWIN__ /* Native Windows or Cygwin */ -/* Canonicalize a Win32 native locale name to a Unix locale name. +/* Canonicalize a Windows native locale name to a Unix locale name. NAME is a sufficiently large buffer. - On input, it contains the Win32 locale name. + On input, it contains the Windows locale name. On output, it contains the Unix locale name. */ # if !defined IN_LIBINTL static @@ -1467,9 +1465,9 @@ gl_locale_name_from_win32_LANGID (LANGID langid) } /* Internet Explorer has an LCID to RFC3066 name mapping stored in HKEY_CLASSES_ROOT\Mime\Database\Rfc1766. But we better don't use that - since IE's i18n subsystem is known to be inconsistent with the Win32 base - (e.g. they have different character conversion facilities that produce - different results). */ + since IE's i18n subsystem is known to be inconsistent with the native + Windows base (e.g. they have different character conversion facilities + that produce different results). */ /* Use our own table. */ { int primary, sub; @@ -2507,7 +2505,7 @@ gl_locale_name_from_win32_LCID (LCID lcid) #endif -#if HAVE_USELOCALE /* glibc or MacOS X */ +#if HAVE_USELOCALE /* glibc or Mac OS X */ /* Simple hash set of strings. We don't want to drag in lots of hash table code here. */ @@ -2607,7 +2605,7 @@ gl_locale_name_thread_unsafe (int category, const char *categoryname) locale_t thread_locale = uselocale (NULL); if (thread_locale != LC_GLOBAL_LOCALE) { -# if __GLIBC__ >= 2 +# if __GLIBC__ >= 2 && !defined __UCLIBC__ /* Work around an incorrect definition of the _NL_LOCALE_NAME macro in glibc < 2.12. See . */ @@ -2618,105 +2616,34 @@ gl_locale_name_thread_unsafe (int category, const char *categoryname) nl_langinfo (_NL_LOCALE_NAME (category)). */ name = thread_locale->__names[category]; return name; -# endif -# if defined __APPLE__ && defined __MACH__ /* MacOS X */ - /* The locale name is found deep in an undocumented data structure. - Since it's stored in a buffer of size 32 and newlocale() rejects - locale names of length > 31, we can assume that it is NUL terminated - in this buffer. But we need to make a copy of the locale name, of - indefinite extent. */ - struct _xlocale - { - int32_t __refcount; - void (*__free_extra)(void *); - __darwin_mbstate_t __mbs[10]; - int64_t __magic; - unsigned char __collate_load_error; - unsigned char __collate_substitute_nontrivial; - unsigned char _messages_using_locale; - unsigned char _monetary_using_locale; - unsigned char _numeric_using_locale; - unsigned char _time_using_locale; - unsigned char __mlocale_changed; - unsigned char __nlocale_changed; - unsigned char __numeric_fp_cvt; - struct __xlocale_st_collate *__lc_collate; - struct __xlocale_st_runelocale *__lc_ctype; - struct __xlocale_st_messages *__lc_messages; - struct __xlocale_st_monetary *__lc_monetary; - struct __xlocale_st_numeric *__lc_numeric; - struct _xlocale *__lc_numeric_loc; - struct __xlocale_st_time *__lc_time; - /* more */ - }; - struct __xlocale_st_collate - { - int32_t __refcount; - void (*__free_extra)(void *); - char __encoding[32]; - /* more */ - }; - struct __xlocale_st_runelocale - { - int32_t __refcount; - void (*__free_extra)(void *); - char __ctype_encoding[32]; - /* more */ - }; - struct __xlocale_st_messages - { - int32_t __refcount; - void (*__free_extra)(void *); - char *_messages_locale_buf; - /* more */ - }; - struct __xlocale_st_monetary - { - int32_t __refcount; - void (*__free_extra)(void *); - char *_monetary_locale_buf; - /* more */ - }; - struct __xlocale_st_numeric { - int32_t __refcount; - void (*__free_extra)(void *); - char *_numeric_locale_buf; - /* more */ - }; - struct __xlocale_st_time { - int32_t __refcount; - void (*__free_extra)(void *); - char *_time_locale_buf; - /* more */ - }; - struct _xlocale *tlp = (struct _xlocale *) thread_locale; +# elif defined __FreeBSD__ || (defined __APPLE__ && defined __MACH__) + /* FreeBSD, Mac OS X */ + int mask; + switch (category) { case LC_CTYPE: - return tlp->__lc_ctype->__ctype_encoding; + mask = LC_CTYPE_MASK; + break; case LC_NUMERIC: - return tlp->_numeric_using_locale - ? tlp->__lc_numeric->_numeric_locale_buf - : "C"; + mask = LC_NUMERIC_MASK; + break; case LC_TIME: - return tlp->_time_using_locale - ? tlp->__lc_time->_time_locale_buf - : "C"; + mask = LC_TIME_MASK; + break; case LC_COLLATE: - return !tlp->__collate_load_error - ? tlp->__lc_collate->__encoding - : "C"; + mask = LC_COLLATE_MASK; + break; case LC_MONETARY: - return tlp->_monetary_using_locale - ? tlp->__lc_monetary->_monetary_locale_buf - : "C"; + mask = LC_MONETARY_MASK; + break; case LC_MESSAGES: - return tlp->_messages_using_locale - ? tlp->__lc_messages->_messages_locale_buf - : "C"; + mask = LC_MESSAGES_MASK; + break; default: /* We shouldn't get here. */ return ""; } + return querylocale (mask, thread_locale); # endif } } @@ -2743,7 +2670,7 @@ gl_locale_name_thread (int category, const char *categoryname) However it does not specify the exact format. Neither do SUSV2 and ISO C 99. So we can use this feature only on selected systems (e.g. those using GNU C Library). */ -#if defined _LIBC || (defined __GLIBC__ && __GLIBC__ >= 2) +#if defined _LIBC || ((defined __GLIBC__ && __GLIBC__ >= 2) && !defined __UCLIBC__) # define HAVE_LOCALE_NULL #endif @@ -2758,8 +2685,8 @@ gl_locale_name_posix (int category, const char *categoryname) /* On other systems we ignore what setlocale reports and instead look at the environment variables directly. This is necessary 1. on systems which have a facility for customizing the default locale - (MacOS X, native Windows, Cygwin) and where the system's setlocale() - function ignores this default locale (MacOS X, Cygwin), in two cases: + (Mac OS X, native Windows, Cygwin) and where the system's setlocale() + function ignores this default locale (Mac OS X, Cygwin), in two cases: a. when the user missed to use the setlocale() override from libintl (for example by not including ), b. when setlocale supports only the "C" locale, such as on Cygwin @@ -2794,7 +2721,7 @@ gl_locale_name_environ (int category, const char *categoryname) if (retval != NULL && retval[0] != '\0') { #if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE - /* MacOS X 10.2 or newer. + /* Mac OS X 10.2 or newer. Ignore invalid LANG value set by the Terminal application. */ if (strcmp (retval, "UTF-8") != 0) #endif @@ -2821,10 +2748,10 @@ gl_locale_name_default (void) locale, customizing it for each location. POSIX:2001 does not require such a facility. - The systems with such a facility are MacOS X and Windows: They provide a + The systems with such a facility are Mac OS X and Windows: They provide a GUI that allows the user to choose a locale. - - On MacOS X, by default, none of LC_* or LANG are set. Starting with - MacOS X 10.4 or 10.5, LANG is set for processes launched by the + - On Mac OS X, by default, none of LC_* or LANG are set. Starting with + Mac OS X 10.4 or 10.5, LANG is set for processes launched by the 'Terminal' application (but sometimes to an incorrect value "UTF-8"). When no environment variable is set, setlocale (LC_ALL, "") uses the "C" locale. @@ -2840,7 +2767,7 @@ gl_locale_name_default (void) "C.UTF-8" locale, which operates in the same way as the "C" locale. */ -#if !(HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE || defined WIN32_NATIVE || defined __CYGWIN__) +#if !(HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE || defined WINDOWS_NATIVE || defined __CYGWIN__) /* The system does not have a way of setting the locale, other than the POSIX specified environment variables. We use C as default locale. */ @@ -2854,7 +2781,7 @@ gl_locale_name_default (void) codeset. */ # if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE - /* MacOS X 10.2 or newer */ + /* Mac OS X 10.2 or newer */ { /* Cache the locale name, since CoreFoundation calls are expensive. */ static const char *cached_localename; @@ -2862,24 +2789,25 @@ gl_locale_name_default (void) if (cached_localename == NULL) { char namebuf[256]; -# if HAVE_CFLOCALECOPYCURRENT /* MacOS X 10.3 or newer */ +# if HAVE_CFLOCALECOPYCURRENT /* Mac OS X 10.3 or newer */ CFLocaleRef locale = CFLocaleCopyCurrent (); CFStringRef name = CFLocaleGetIdentifier (locale); - if (CFStringGetCString (name, namebuf, sizeof(namebuf), + if (CFStringGetCString (name, namebuf, sizeof (namebuf), kCFStringEncodingASCII)) { gl_locale_name_canonicalize (namebuf); cached_localename = strdup (namebuf); } CFRelease (locale); -# elif HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.2 or newer */ +# elif HAVE_CFPREFERENCESCOPYAPPVALUE /* Mac OS X 10.2 or newer */ CFTypeRef value = CFPreferencesCopyAppValue (CFSTR ("AppleLocale"), kCFPreferencesCurrentApplication); if (value != NULL && CFGetTypeID (value) == CFStringGetTypeID () - && CFStringGetCString ((CFStringRef)value, namebuf, sizeof(namebuf), + && CFStringGetCString ((CFStringRef)value, + namebuf, sizeof (namebuf), kCFStringEncodingASCII)) { gl_locale_name_canonicalize (namebuf); @@ -2894,11 +2822,11 @@ gl_locale_name_default (void) # endif -# if defined WIN32_NATIVE || defined __CYGWIN__ /* WIN32 or Cygwin */ +# if defined WINDOWS_NATIVE || defined __CYGWIN__ /* Native Windows or Cygwin */ { LCID lcid; - /* Use native Win32 API locale ID. */ + /* Use native Windows API locale ID. */ lcid = GetThreadLocale (); return gl_locale_name_from_win32_LCID (lcid);