Adapt GDB integration to newest patches
[bpt/guile.git] / libguile / i18n.c
index b22b332..f0e3443 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+/* Copyright (C) 2006-2014 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
@@ -40,7 +40,7 @@
 #include <unicase.h>
 #include <unistr.h>
 
-#if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L)
+#if defined HAVE_NEWLOCALE && defined HAVE_STRCOLL_L && defined HAVE_USELOCALE
 /* The GNU thread-aware locale API is documented in ``Thread-Aware Locale
    Model, a Proposal'', by Ulrich Drepper:
 
 
 #include "libguile/posix.h"  /* for `scm_i_locale_mutex' */
 
-#ifdef HAVE_LANGINFO_H
-# include <langinfo.h>
-#endif
-#ifdef HAVE_NL_TYPES_H
-# include <nl_types.h>
-#endif
-#ifndef HAVE_NL_ITEM
-/* Cygwin has <langinfo.h> but lacks <nl_types.h> and `nl_item'.  */
-typedef int nl_item;
-#endif
+/* Use Gnulib's header, which also provides `nl_item' & co.  */
+#include <langinfo.h>
 
 #ifndef HAVE_SETLOCALE
 static inline char *
@@ -196,22 +188,11 @@ typedef struct scm_locale
   int   category_mask;
 } *scm_t_locale;
 
-
-/* Free the resources used by LOCALE.  */
-static inline void
-scm_i_locale_free (scm_t_locale locale)
-{
-  free (locale->locale_name);
-  locale->locale_name = NULL;
-}
-
 #else /* USE_GNU_LOCALE_API */
 
 /* Alias for glibc's locale type.  */
 typedef locale_t scm_t_locale;
 
-#define scm_i_locale_free freelocale
-
 #endif /* USE_GNU_LOCALE_API */
 
 
@@ -234,7 +215,7 @@ SCM_GLOBAL_VARIABLE (scm_global_locale, "%global-locale");
 #define SCM_VALIDATE_OPTIONAL_LOCALE_COPY(_pos, _arg, _c_locale)       \
   do                                                                   \
     {                                                                  \
-      if (!scm_is_eq ((_arg), SCM_UNDEFINED))                           \
+      if (!SCM_UNBNDP (_arg))                                          \
        SCM_VALIDATE_LOCALE_COPY (_pos, _arg, _c_locale);               \
       else                                                             \
        (_c_locale) = NULL;                                             \
@@ -244,16 +225,21 @@ SCM_GLOBAL_VARIABLE (scm_global_locale, "%global-locale");
 
 SCM_SMOB (scm_tc16_locale_smob_type, "locale", 0);
 
+#ifdef USE_GNU_LOCALE_API
+
 SCM_SMOB_FREE (scm_tc16_locale_smob_type, smob_locale_free, locale)
 {
   scm_t_locale c_locale;
 
   c_locale = (scm_t_locale) SCM_SMOB_DATA (locale);
-  scm_i_locale_free (c_locale);
+  if (c_locale)
+    freelocale (c_locale);
 
   return 0;
 }
 
+#endif /* USE_GNU_LOCALE_API */
+
 
 static void inline scm_locale_error (const char *, int) SCM_NORETURN;
 
@@ -400,7 +386,7 @@ install_locale (scm_t_locale locale)
         account.  */
       category_mask |= locale->category_mask;
 
-      if (locale->base_locale != SCM_UNDEFINED)
+      if (!SCM_UNBNDP (locale->base_locale))
        locale = (scm_t_locale) SCM_SMOB_DATA (locale->base_locale);
       else
        locale = NULL;
@@ -501,7 +487,6 @@ get_current_locale (SCM *result)
 
   c_locale = scm_gc_malloc (sizeof (* c_locale), "locale");
 
-
   lock_locale_mutex ();
 
   c_locale->category_mask = LC_ALL_MASK;
@@ -509,20 +494,16 @@ get_current_locale (SCM *result)
 
   current_locale = setlocale (LC_ALL, NULL);
   if (current_locale != NULL)
-    {
-      c_locale->locale_name = strdup (current_locale);
-      if (c_locale->locale_name == NULL)
-       err = ENOMEM;
-    }
+    c_locale->locale_name = scm_gc_strdup (current_locale, "locale");
   else
     err = EINVAL;
 
   unlock_locale_mutex ();
 
-  if (err)
-    scm_gc_free (c_locale, sizeof (* c_locale), "locale");
-  else
+  if (err == 0)
     SCM_NEWSMOB (*result, scm_tc16_locale_smob_type, c_locale);
+  else
+    *result = SCM_BOOL_F;
 
   return err;
 }
@@ -657,6 +638,7 @@ SCM_DEFINE (scm_make_locale, "make-locale", 2, 1, 0,
   c_locale = newlocale (c_category_mask, c_locale_name, c_base_locale);
 
   free (c_locale_name);
+  c_locale_name = NULL;
 
   if (c_locale == (locale_t) 0)
     {
@@ -672,7 +654,9 @@ SCM_DEFINE (scm_make_locale, "make-locale", 2, 1, 0,
   c_locale = scm_gc_malloc (sizeof (* c_locale), "locale");
 
   c_locale->category_mask = c_category_mask;
-  c_locale->locale_name = c_locale_name;
+  c_locale->locale_name = scm_gc_strdup (c_locale_name, "locale");
+  free (c_locale_name);
+  c_locale_name = NULL;
 
   if (scm_is_eq (base_locale, SCM_VARIABLE_REF (scm_global_locale)))
     {
@@ -701,6 +685,8 @@ SCM_DEFINE (scm_make_locale, "make-locale", 2, 1, 0,
       }
   }
 
+  /* silence gcc's unused variable warning */
+  (void) c_base_locale;
 #endif
 
   return locale;
@@ -1479,6 +1465,14 @@ SCM_DEFINE (scm_locale_string_to_inexact, "locale-string->inexact",
    Note: We don't use Gnulib's `nl_langinfo' module because it's currently not
    as complete as the compatibility hacks in `i18n.scm'.  */
 
+static char *
+copy_string_or_null (const char *s)
+{
+  if (s == NULL)
+    return NULL;
+  else
+    return strdup (s);
+}
 
 SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
            (SCM item, SCM locale),
@@ -1490,14 +1484,11 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
            "Reference Manual}).")
 #define FUNC_NAME s_scm_nl_langinfo
 {
-#ifdef HAVE_NL_LANGINFO
   SCM result;
   nl_item c_item;
   char *c_result;
   scm_t_locale c_locale;
-#ifdef HAVE_LANGINFO_CODESET
   char *codeset;
-#endif
 
   SCM_VALIDATE_INT_COPY (2, item, c_item);
   SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale);
@@ -1513,10 +1504,8 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
   if (c_locale != NULL)
     {
 #ifdef USE_GNU_LOCALE_API
-      c_result = nl_langinfo_l (c_item, c_locale);
-#ifdef HAVE_LANGINFO_CODESET
-      codeset = nl_langinfo_l (CODESET, c_locale);
-#endif /* HAVE_LANGINFO_CODESET */
+      c_result = copy_string_or_null (nl_langinfo_l (c_item, c_locale));
+      codeset = copy_string_or_null (nl_langinfo_l (CODESET, c_locale));
 #else /* !USE_GNU_LOCALE_API */
       /* We can't use `RUN_IN_LOCALE_SECTION ()' here because the locale
         mutex is already taken.  */
@@ -1540,10 +1529,8 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
        scm_locale_error (FUNC_NAME, lsec_err);
       else
        {
-         c_result = nl_langinfo (c_item);
-#ifdef HAVE_LANGINFO_CODESET
-          codeset = nl_langinfo (CODESET);
-#endif /* HAVE_LANGINFO_CODESET */
+         c_result = copy_string_or_null (nl_langinfo (c_item));
+          codeset = copy_string_or_null (nl_langinfo (CODESET));
 
          restore_locale_settings (&lsec_prev_locale);
          free_locale_settings (&lsec_prev_locale);
@@ -1552,13 +1539,10 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
     }
   else
     {
-      c_result = nl_langinfo (c_item);
-#ifdef HAVE_LANGINFO_CODESET
-      codeset = nl_langinfo (CODESET);
-#endif /* HAVE_LANGINFO_CODESET */
+      c_result = copy_string_or_null (nl_langinfo (c_item));
+      codeset = copy_string_or_null (nl_langinfo (CODESET));
     }
 
-  c_result = strdup (c_result);
   unlock_locale_mutex ();
 
   if (c_result == NULL)
@@ -1601,9 +1585,13 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
          }
 #endif
 
-#if (defined FRAC_DIGITS) && (defined INT_FRAC_DIGITS)
+#if defined FRAC_DIGITS || defined INT_FRAC_DIGITS
+#ifdef FRAC_DIGITS
        case FRAC_DIGITS:
+#endif
+#ifdef INT_FRAC_DIGITS
        case INT_FRAC_DIGITS:
+#endif
          /* This is to be interpreted as a single integer.  */
          if (*c_result == CHAR_MAX)
            /* Unspecified.  */
@@ -1615,12 +1603,18 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
          break;
 #endif
 
-#if (defined P_CS_PRECEDES) && (defined INT_N_CS_PRECEDES)
+#if defined P_CS_PRECEDES || defined N_CS_PRECEDES ||  \
+  defined INT_P_CS_PRECEDES || defined INT_N_CS_PRECEDES || \
+  defined P_SEP_BY_SPACE || defined N_SEP_BY_SPACE
+#ifdef P_CS_PRECEDES
        case P_CS_PRECEDES:
        case N_CS_PRECEDES:
+#endif
+#ifdef INT_N_CS_PRECEDES
        case INT_P_CS_PRECEDES:
        case INT_N_CS_PRECEDES:
-#if (defined P_SEP_BY_SPACE) && (defined N_SEP_BY_SPACE)
+#endif
+#ifdef P_SEP_BY_SPACE
        case P_SEP_BY_SPACE:
        case N_SEP_BY_SPACE:
 #endif
@@ -1631,11 +1625,16 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
          break;
 #endif
 
-#if (defined P_SIGN_POSN) && (defined INT_N_SIGN_POSN)
+#if defined P_SIGN_POSN || defined N_SIGN_POSN || \
+  defined INT_P_SIGN_POSN || defined INT_N_SIGN_POSN
+#ifdef P_SIGN_POSN
        case P_SIGN_POSN:
        case N_SIGN_POSN:
+#endif
+#ifdef INT_P_SIGN_POSN
        case INT_P_SIGN_POSN:
        case INT_N_SIGN_POSN:
+#endif
          /* See `(libc) Sign of Money Amount' for the interpretation of the
             return value here.  */
          switch (*c_result)
@@ -1663,30 +1662,22 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
            default:
              result = scm_from_latin1_symbol ("unspecified");
            }
+         free (c_result);
          break;
 #endif
 
        default:
-#ifdef HAVE_LANGINFO_CODESET
           result = scm_from_stringn (c_result, strlen (c_result),
                                      codeset,
                                      SCM_FAILED_CONVERSION_QUESTION_MARK);
-#else /* !HAVE_LANGINFO_CODESET */
-          /* This may be incorrectly encoded if the locale differs
-             from the c_locale.  */
-          result = scm_from_locale_string (c_result);
-#endif /* !HAVE_LANGINFO_CODESET */
           free (c_result);
        }
     }
 
-  return result;
-#else
-  scm_syserror_msg (FUNC_NAME, "`nl-langinfo' not supported on your system",
-                   SCM_EOL, ENOSYS);
+  if (codeset != NULL)
+    free (codeset);
 
-  return SCM_BOOL_F;
-#endif
+  return result;
 }
 #undef FUNC_NAME
 
@@ -1694,8 +1685,6 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0,
 static inline void
 define_langinfo_items (void)
 {
-#ifdef HAVE_LANGINFO_H
-
 #define DEFINE_NLITEM_CONSTANT(_item)          \
   scm_c_define (# _item, scm_from_int (_item))
 
@@ -1755,13 +1744,23 @@ define_langinfo_items (void)
   DEFINE_NLITEM_CONSTANT (T_FMT);   /* Time format for strftime.  */
   DEFINE_NLITEM_CONSTANT (T_FMT_AMPM);/* 12-hour time format for strftime.  */
 
+#ifdef ERA
   DEFINE_NLITEM_CONSTANT (ERA);                /* Alternate era.  */
+#endif
+#ifdef ERA_D_FMT
   DEFINE_NLITEM_CONSTANT (ERA_D_FMT);  /* Date in alternate era format.  */
+#endif
+#ifdef ERA_D_T_FMT
   DEFINE_NLITEM_CONSTANT (ERA_D_T_FMT);        /* Date and time in alternate era
                                           format.  */
+#endif
+#ifdef ERA_T_FMT
   DEFINE_NLITEM_CONSTANT (ERA_T_FMT);  /* Time in alternate era format.  */
+#endif
 
+#ifdef ALT_DIGITS
   DEFINE_NLITEM_CONSTANT (ALT_DIGITS); /* Alternate symbols for digits.  */
+#endif
   DEFINE_NLITEM_CONSTANT (RADIXCHAR);
   DEFINE_NLITEM_CONSTANT (THOUSEP);
 
@@ -1850,8 +1849,6 @@ define_langinfo_items (void)
 #endif
 
 #undef DEFINE_NLITEM_CONSTANT
-
-#endif /* HAVE_NL_TYPES_H */
 }
 
 \f
@@ -1860,10 +1857,8 @@ scm_init_i18n ()
 {
   SCM global_locale_smob;
 
-#ifdef HAVE_NL_LANGINFO
   scm_add_feature ("nl-langinfo");
   define_langinfo_items ();
-#endif
 
 #include "libguile/i18n.x"