frexpl: Update regarding AIX.
[gnulib.git] / lib / localename.c
index 1310cb5..ea646c5 100644 (file)
@@ -1,5 +1,5 @@
 /* Determine name of the currently selected locale.
-   Copyright (C) 1995-1999, 2000-2009 Free Software Foundation, Inc.
+   Copyright (C) 1995-2010 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
 # include "localename.h"
 #endif
 
+#include <limits.h>
+#include <stddef.h>
 #include <stdlib.h>
 #include <locale.h>
+#include <string.h>
+
+#if HAVE_USELOCALE
+/* MacOS X 10.5 defines the locale_t type in <xlocale.h>.  */
+# if defined __APPLE__ && defined __MACH__
+#  include <xlocale.h>
+# endif
+# include <langinfo.h>
+# if !defined IN_LIBINTL
+#  include "glthread/lock.h"
+# endif
+#endif
 
 #if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE
-# include <string.h>
 # include <CoreFoundation/CFString.h>
 # if HAVE_CFLOCALECOPYCURRENT
 #  include <CoreFoundation/CFLocale.h>
@@ -46,7 +59,7 @@
 # define WIN32_NATIVE
 #endif
 
-#ifdef WIN32_NATIVE
+#if defined WIN32_NATIVE || defined __CYGWIN__ /* WIN32 or Cygwin */
 # define WIN32_LEAN_AND_MEAN
 # include <windows.h>
 /* List of language codes, sorted by value:
@@ -1394,7 +1407,7 @@ gl_locale_name_canonicalize (char *name)
 #endif
 
 
-#ifdef WIN32_NATIVE
+#if defined WIN32_NATIVE || defined __CYGWIN__ /* WIN32 or Cygwin */
 
 /* Canonicalize a Win32 native locale name to a Unix locale name.
    NAME is a sufficiently large buffer.
@@ -2494,6 +2507,264 @@ gl_locale_name_from_win32_LCID (LCID lcid)
 #endif
 
 
+#if HAVE_USELOCALE /* glibc or MacOS X */
+
+/* Simple hash set of strings.  We don't want to drag in lots of hash table
+   code here.  */
+
+# define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
+
+/* A hash function for NUL-terminated char* strings using
+   the method described by Bruno Haible.
+   See http://www.haible.de/bruno/hashfunc.html.  */
+static size_t
+string_hash (const void *x)
+{
+  const char *s = (const char *) x;
+  size_t h = 0;
+
+  for (; *s; s++)
+    h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));
+
+  return h;
+}
+
+/* A hash table of fixed size.  Multiple threads can access it read-only
+   simultaneously, but only one thread can insert into it at the same time.  */
+
+/* A node in a hash bucket collision list.  */
+struct hash_node
+  {
+    struct hash_node * volatile next;
+    char contents[100]; /* has variable size */
+  };
+
+# define HASH_TABLE_SIZE 257
+static struct hash_node * volatile struniq_hash_table[HASH_TABLE_SIZE]
+  /* = { NULL, ..., NULL } */;
+
+/* This lock protects the struniq_hash_table against multiple simultaneous
+   insertions.  */
+gl_lock_define_initialized(static, struniq_lock)
+
+/* Store a copy of the given string in a string pool with indefinite extent.
+   Return a pointer to this copy.  */
+static const char *
+struniq (const char *string)
+{
+  size_t hashcode = string_hash (string);
+  size_t slot = hashcode % HASH_TABLE_SIZE;
+  size_t size;
+  struct hash_node *new_node;
+  struct hash_node *p;
+  for (p = struniq_hash_table[slot]; p != NULL; p = p->next)
+    if (strcmp (p->contents, string) == 0)
+      return p->contents;
+  size = strlen (string) + 1;
+  new_node =
+    (struct hash_node *)
+    malloc (offsetof (struct hash_node, contents[0]) + size);
+  if (new_node == NULL)
+    /* Out of memory.  Return a statically allocated string.  */
+    return "C";
+  memcpy (new_node->contents, string, size);
+  /* Lock while inserting new_node.  */
+  gl_lock_lock (struniq_lock);
+  /* Check whether another thread already added the string while we were
+     waiting on the lock.  */
+  for (p = struniq_hash_table[slot]; p != NULL; p = p->next)
+    if (strcmp (p->contents, string) == 0)
+      {
+        free (new_node);
+        new_node = p;
+        goto done;
+      }
+  /* Really insert new_node into the hash table.  Fill new_node entirely first,
+     because other threads may be iterating over the linked list.  */
+  new_node->next = struniq_hash_table[slot];
+  struniq_hash_table[slot] = new_node;
+ done:
+  /* Unlock after new_node is inserted.  */
+  gl_lock_unlock (struniq_lock);
+  return new_node->contents;
+}
+
+#endif
+
+
+#if defined IN_LIBINTL || HAVE_USELOCALE
+
+/* Like gl_locale_name_thread, except that the result is not in storage of
+   indefinite extent.  */
+# if !defined IN_LIBINTL
+static
+# endif
+const char *
+gl_locale_name_thread_unsafe (int category, const char *categoryname)
+{
+# if HAVE_USELOCALE
+  {
+    locale_t thread_locale = uselocale (NULL);
+    if (thread_locale != LC_GLOBAL_LOCALE)
+      {
+#  if __GLIBC__ >= 2
+        /* Work around an incorrect definition of the _NL_LOCALE_NAME macro in
+           glibc < 2.12.
+           See <http://sourceware.org/bugzilla/show_bug.cgi?id=10968>.  */
+        const char *name =
+          nl_langinfo (_NL_ITEM ((category), _NL_ITEM_INDEX (-1)));
+        if (name[0] == '\0')
+          /* Fallback code for glibc < 2.4, which did not implement
+             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_part1_v0 /* used in MacOS X 10.5 */
+          {
+            int32_t __refcount;
+            void (*__free_extra)(void *);
+            __darwin_mbstate_t __mbs[10];
+            int64_t __magic;
+          };
+        struct _xlocale_part1_v1 /* used in MacOS X >= 10.6.0 */
+          {
+            int32_t __refcount;
+            void (*__free_extra)(void *);
+            __darwin_mbstate_t __mbs[10];
+            /*pthread_lock_t*/ int __lock;
+            int64_t __magic;
+          };
+        struct _xlocale_part2
+          {
+            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_part2 *tlp;
+        if (((struct _xlocale_part1_v0 *) thread_locale)->__magic
+            == 0x786C6F63616C6530LL)
+          /* MacOS X 10.5 */
+          tlp =
+            (struct _xlocale_part2 *)
+            &((struct _xlocale_part1_v0 *) thread_locale)->__magic;
+        else if (((struct _xlocale_part1_v1 *) thread_locale)->__magic
+                 == 0x786C6F63616C6530LL)
+          /* MacOS X >= 10.6.0 */
+          tlp =
+            (struct _xlocale_part2 *)
+            &((struct _xlocale_part1_v1 *) thread_locale)->__magic;
+        else
+          /* Unsupported version of MacOS X: The internals of 'struct _xlocale'
+             have changed again.  */
+          return "";
+        switch (category)
+          {
+          case LC_CTYPE:
+            return tlp->__lc_ctype->__ctype_encoding;
+          case LC_NUMERIC:
+            return tlp->_numeric_using_locale
+                   ? tlp->__lc_numeric->_numeric_locale_buf
+                   : "C";
+          case LC_TIME:
+            return tlp->_time_using_locale
+                   ? tlp->__lc_time->_time_locale_buf
+                   : "C";
+          case LC_COLLATE:
+            return !tlp->__collate_load_error
+                   ? tlp->__lc_collate->__encoding
+                   : "C";
+          case LC_MONETARY:
+            return tlp->_monetary_using_locale
+                   ? tlp->__lc_monetary->_monetary_locale_buf
+                   : "C";
+          case LC_MESSAGES:
+            return tlp->_messages_using_locale
+                   ? tlp->__lc_messages->_messages_locale_buf
+                   : "C";
+          default: /* We shouldn't get here.  */
+            return "";
+          }
+#  endif
+      }
+  }
+# endif
+  return NULL;
+}
+
+#endif
+
+const char *
+gl_locale_name_thread (int category, const char *categoryname)
+{
+#if HAVE_USELOCALE
+  const char *name = gl_locale_name_thread_unsafe (category, categoryname);
+  if (name != NULL)
+    return struniq (name);
+#endif
+  return NULL;
+}
+
 /* XPG3 defines the result of 'setlocale (category, NULL)' as:
    "Directs 'setlocale()' to query 'category' and return the current
     setting of 'local'."
@@ -2504,12 +2775,6 @@ gl_locale_name_from_win32_LCID (LCID lcid)
 # define HAVE_LOCALE_NULL
 #endif
 
-/* Determine the current locale's name, and canonicalize it into XPG syntax
-     language[_territory][.codeset][@modifier]
-   The codeset part in the result is not reliable; the locale_charset()
-   should be used for codeset information instead.
-   The result must not be freed; it is statically allocated.  */
-
 const char *
 gl_locale_name_posix (int category, const char *categoryname)
 {
@@ -2518,6 +2783,30 @@ gl_locale_name_posix (int category, const char *categoryname)
 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
   return setlocale (category, NULL);
 #else
+  /* 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:
+          a. when the user missed to use the setlocale() override from libintl
+             (for example by not including <libintl.h>),
+          b. when setlocale supports only the "C" locale, such as on Cygwin
+             1.5.x.  In this case even the override from libintl cannot help.
+       2. on all systems where setlocale supports only the "C" locale.  */
+  /* Strictly speaking, it is a POSIX violation to look at the environment
+     variables regardless whether setlocale has been called or not.  POSIX
+     says:
+         "For C-language programs, the POSIX locale shall be the
+          default locale when the setlocale() function is not called."
+     But we assume that all programs that use internationalized APIs call
+     setlocale (LC_ALL, "").  */
+  return gl_locale_name_environ (category, categoryname);
+#endif
+}
+
+const char *
+gl_locale_name_environ (int category, const char *categoryname)
+{
   const char *retval;
 
   /* Setting of LC_ALL overrides all other.  */
@@ -2531,10 +2820,21 @@ gl_locale_name_posix (int category, const char *categoryname)
   /* Last possibility is the LANG environment variable.  */
   retval = getenv ("LANG");
   if (retval != NULL && retval[0] != '\0')
-    return retval;
+    {
+#if HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE
+      /* MacOS X 10.2 or newer.
+         Ignore invalid LANG value set by the Terminal application.  */
+      if (strcmp (retval, "UTF-8") != 0)
+#endif
+#if defined __CYGWIN__
+      /* Cygwin.
+         Ignore dummy LANG value set by ~/.profile.  */
+      if (strcmp (retval, "C.UTF-8") != 0)
+#endif
+        return retval;
+    }
 
   return NULL;
-#endif
 }
 
 const char *
@@ -2547,9 +2847,28 @@ gl_locale_name_default (void)
       implementation-defined locale.  Some implementations may provide
       facilities for local installation administrators to set the default
       locale, customizing it for each location.  POSIX:2001 does not require
-      such a facility.  */
+      such a facility.
 
-#if !(HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE || defined(WIN32_NATIVE))
+     The systems with such a facility are MacOS 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
+         'Terminal' application (but sometimes to an incorrect value "UTF-8").
+         When no environment variable is set, setlocale (LC_ALL, "") uses the
+         "C" locale.
+       - On native Windows, by default, none of LC_* or LANG are set.
+         When no environment variable is set, setlocale (LC_ALL, "") uses the
+         locale chosen by the user.
+       - On Cygwin 1.5.x, by default, none of LC_* or LANG are set.
+         When no environment variable is set, setlocale (LC_ALL, "") uses the
+         "C" locale.
+       - On Cygwin 1.7, by default, LANG is set to "C.UTF-8" when the default
+         ~/.profile is executed.
+         When no environment variable is set, setlocale (LC_ALL, "") uses the
+         "C.UTF-8" locale, which operates in the same way as the "C" locale.
+  */
+
+#if !(HAVE_CFLOCALECOPYCURRENT || HAVE_CFPREFERENCESCOPYAPPVALUE || defined WIN32_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.  */
@@ -2575,7 +2894,7 @@ gl_locale_name_default (void)
         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);
@@ -2588,7 +2907,8 @@ gl_locale_name_default (void)
                                      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);
@@ -2603,7 +2923,7 @@ gl_locale_name_default (void)
 
 # endif
 
-# if defined(WIN32_NATIVE) /* WIN32, not Cygwin */
+# if defined WIN32_NATIVE || defined __CYGWIN__ /* WIN32 or Cygwin */
   {
     LCID lcid;
 
@@ -2616,11 +2936,21 @@ gl_locale_name_default (void)
 #endif
 }
 
+/* Determine the current locale's name, and canonicalize it into XPG syntax
+     language[_territory][.codeset][@modifier]
+   The codeset part in the result is not reliable; the locale_charset()
+   should be used for codeset information instead.
+   The result must not be freed; it is statically allocated.  */
+
 const char *
 gl_locale_name (int category, const char *categoryname)
 {
   const char *retval;
 
+  retval = gl_locale_name_thread (category, categoryname);
+  if (retval != NULL)
+    return retval;
+
   retval = gl_locale_name_posix (category, categoryname);
   if (retval != NULL)
     return retval;