Imported from ../bash-3.1.tar.gz.
[platform/upstream/bash.git] / locale.c
index 1805b2f..48e44ec 100644 (file)
--- a/locale.c
+++ b/locale.c
@@ -1,6 +1,6 @@
 /* locale.c - Miscellaneous internationalization functions. */
 
-/* Copyright (C) 1996 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2004 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -47,6 +47,16 @@ static char *default_dir;
    categories */
 static char *lc_all;
 
+/* tracks the value of LC_ALL; used to provide defaults for locale
+   categories */
+static char *lang;
+
+/* Called to reset all of the locale variables to their appropriate values
+   if (and only if) LC_ALL has not been assigned a value. */
+static int reset_locale_vars __P((void));
+
+static void locale_setblanks __P((void));
+
 /* Set the value of default_locale and make the current locale the
    system default locale.  This should be called very early in main(). */
 void
@@ -57,20 +67,29 @@ set_default_locale ()
   if (default_locale)
     default_locale = savestring (default_locale);
 #endif /* HAVE_SETLOCALE */
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
 }
 
-/* Set default values for LC_CTYPE, LC_COLLATE, and LC_MESSAGES if they
-   are not specified in the environment, but LANG or LC_ALL is.  This
+/* Set default values for LC_CTYPE, LC_COLLATE, LC_MESSAGES, LC_NUMERIC and
+   LC_TIME if they are not specified in the environment, but LC_ALL is.  This
    should be called from main() after parsing the environment. */
 void
 set_default_locale_vars ()
 {
   char *val;
+  int r;
 
 #if defined (HAVE_SETLOCALE)
+
+#  if defined (LC_CTYPE)
   val = get_string_value ("LC_CTYPE");
   if (val == 0 && lc_all && *lc_all)
-    setlocale (LC_CTYPE, lc_all);
+    {
+      setlocale (LC_CTYPE, lc_all);
+      locale_setblanks ();
+    }
+#  endif
 
 #  if defined (LC_COLLATE)
   val = get_string_value ("LC_COLLATE");
@@ -90,6 +109,12 @@ set_default_locale_vars ()
     setlocale (LC_NUMERIC, lc_all);
 #  endif /* LC_NUMERIC */
 
+#  if defined (LC_TIME)
+  val = get_string_value ("LC_TIME");
+  if (val == 0 && lc_all && *lc_all)
+    setlocale (LC_TIME, lc_all);
+#  endif /* LC_TIME */
+
 #endif /* HAVE_SETLOCALE */
 
   val = get_string_value ("TEXTDOMAIN");
@@ -97,7 +122,10 @@ set_default_locale_vars ()
     {
       FREE (default_domain);
       default_domain = savestring (val);
+#if 0
+      /* Don't want to override the shell's textdomain as the default */
       textdomain (default_domain);
+#endif
     }
 
   val = get_string_value ("TEXTDOMAINDIR");
@@ -105,7 +133,8 @@ set_default_locale_vars ()
     {
       FREE (default_dir);
       default_dir = savestring (val);
-      bindtextdomain (default_domain, default_dir);
+      if (default_domain && *default_domain)
+       bindtextdomain (default_domain, default_dir);
     }
 }
 
@@ -115,18 +144,24 @@ int
 set_locale_var (var, value)
      char *var, *value;
 {
+  int r;
+
   if (var[0] == 'T' && var[10] == 0)           /* TEXTDOMAIN */
     {
       FREE (default_domain);
       default_domain = value ? savestring (value) : (char *)NULL;
+#if 0
+      /* Don't want to override the shell's textdomain as the default */
       textdomain (default_domain);
+#endif
       return (1);
     }
   else if (var[0] == 'T')                      /* TEXTDOMAINDIR */
     {
       FREE (default_dir);
       default_dir = value ? savestring (value) : (char *)NULL;
-      bindtextdomain (default_domain, default_dir);
+      if (default_domain && *default_domain)
+       bindtextdomain (default_domain, default_dir);
       return (1);
     }
 
@@ -137,15 +172,15 @@ set_locale_var (var, value)
       FREE (lc_all);
       if (value)
        lc_all = savestring (value);
-      else if (default_locale)
-       lc_all = savestring (default_locale);
       else
        {
          lc_all = (char *)xmalloc (1);
          lc_all[0] = '\0';
        }
 #if defined (HAVE_SETLOCALE)
-      return (setlocale (LC_ALL, lc_all) != 0);
+      r = *lc_all ? (setlocale (LC_ALL, lc_all) != 0) : reset_locale_vars ();
+      locale_setblanks ();
+      return r;
 #else
       return (1);
 #endif
@@ -154,46 +189,71 @@ set_locale_var (var, value)
 #if defined (HAVE_SETLOCALE)
   else if (var[3] == 'C' && var[4] == 'T')     /* LC_CTYPE */
     {
+#  if defined (LC_CTYPE)
       if (lc_all == 0 || *lc_all == '\0')
-       return (setlocale (LC_CTYPE, value ? value : "") != 0);
+       {
+         r = (setlocale (LC_CTYPE, get_locale_var ("LC_CTYPE")) != 0);
+         locale_setblanks ();
+         return r;
+       }
+#  endif
     }
   else if (var[3] == 'C' && var[4] == 'O')     /* LC_COLLATE */
     {
 #  if defined (LC_COLLATE)
       if (lc_all == 0 || *lc_all == '\0')
-       return (setlocale (LC_COLLATE, value ? value : "") != 0);
+       return (setlocale (LC_COLLATE, get_locale_var ("LC_COLLATE")) != 0);
 #  endif /* LC_COLLATE */
     }
   else if (var[3] == 'M' && var[4] == 'E')     /* LC_MESSAGES */
     {
 #  if defined (LC_MESSAGES)
       if (lc_all == 0 || *lc_all == '\0')
-       return (setlocale (LC_MESSAGES, value ? value : "") != 0);
+       return (setlocale (LC_MESSAGES, get_locale_var ("LC_MESSAGES")) != 0);
 #  endif /* LC_MESSAGES */
     }
   else if (var[3] == 'N' && var[4] == 'U')     /* LC_NUMERIC */
     {
 #  if defined (LC_NUMERIC)
       if (lc_all == 0 || *lc_all == '\0')
-       return (setlocale (LC_NUMERIC, value ? value : "") != 0);
+       return (setlocale (LC_NUMERIC, get_locale_var ("LC_NUMERIC")) != 0);
 #  endif /* LC_NUMERIC */
     }
+  else if (var[3] == 'T' && var[4] == 'I')     /* LC_TIME */
+    {
+#  if defined (LC_TIME)
+      if (lc_all == 0 || *lc_all == '\0')
+       return (setlocale (LC_TIME, get_locale_var ("LC_TIME")) != 0);
+#  endif /* LC_TIME */
+    }
 #endif /* HAVE_SETLOCALE */
+  
 
   return (0);
 }
 
-/* Called when LANG is assigned a value.  Sets LC_ALL category with
-   setlocale(3) if that has not already been set.  Doesn't change any
-   shell variables. */
+/* Called when LANG is assigned a value.  Tracks value in `lang'.  Calls
+   reset_locale_vars() to reset any default values if LC_ALL is unset or
+   null. */
 int
 set_lang (var, value)
      char *var, *value;
 {
-  return ((lc_all == 0 || *lc_all == 0) ? setlocale (LC_ALL, value?value:"") != NULL : 0);
+  FREE (lang);
+  if (value)
+    lang = savestring (value);
+  else
+    {
+      lang = (char *)xmalloc (1);
+      lang[0] = '\0';
+    }
+    
+  return ((lc_all == 0 || *lc_all == 0) ? reset_locale_vars () : 0);
 }
 
-/* Get the value of one of the locale variables (LC_MESSAGES, LC_CTYPE) */
+/* Get the value of one of the locale variables (LC_MESSAGES, LC_CTYPE).
+   The precedence is as POSIX.2 specifies:  LC_ALL has precedence over
+   the specific locale variables, and LANG, if set, is used as the default. */
 char *
 get_locale_var (var)
      char *var;
@@ -202,14 +262,53 @@ get_locale_var (var)
 
   locale = lc_all;
 
-  if (locale == 0)
+  if (locale == 0 || *locale == 0)
     locale = get_string_value (var);
-  if (locale == 0)
-    locale = default_locale;
+  if (locale == 0 || *locale == 0)
+    locale = lang;
+  if (locale == 0 || *locale == 0)
+    locale = default_locale;   /* system-dependent; not really portable */
 
   return (locale);
 }
 
+/* Called to reset all of the locale variables to their appropriate values
+   if (and only if) LC_ALL has not been assigned a value.  DO NOT CALL THIS
+   IF LC_ALL HAS BEEN ASSIGNED A VALUE. */
+static int
+reset_locale_vars ()
+{
+#if defined (HAVE_SETLOCALE)
+  char *locale;
+
+  locale = lang;
+  if (locale == 0 || *locale == '\0')
+    locale = default_locale;
+  if (setlocale (LC_ALL, locale) == 0)
+    return 0;
+
+#  if defined (LC_CTYPE)
+  setlocale (LC_CTYPE, get_locale_var ("LC_CTYPE"));
+#  endif
+#  if defined (LC_COLLATE)
+  setlocale (LC_COLLATE, get_locale_var ("LC_COLLATE"));
+#  endif
+#  if defined (LC_MESSAGES)
+  setlocale (LC_MESSAGES, get_locale_var ("LC_MESSAGES"));
+#  endif
+#  if defined (LC_NUMERIC)
+  setlocale (LC_NUMERIC, get_locale_var ("LC_NUMERIC"));
+#  endif
+#  if defined (LC_TIME)
+  setlocale (LC_TIME, get_locale_var ("LC_TIME"));
+#  endif
+
+  locale_setblanks ();  
+
+#endif
+  return 1;
+}
+
 /* Translate the contents of STRING, a $"..." quoted string, according
    to the current locale.  In the `C' or `POSIX' locale, or if gettext()
    is not available, the passed string is returned unchanged.  The
@@ -220,10 +319,8 @@ localetrans (string, len, lenp)
      int len, *lenp;
 {
   char *locale, *t;
-#if defined (HAVE_GETTEXT)
   char *translated;
   int tlen;
-#endif
 
   /* Don't try to translate null strings. */
   if (string == 0 || *string == 0)
@@ -238,10 +335,8 @@ localetrans (string, len, lenp)
   /* If we don't have setlocale() or the current locale is `C' or `POSIX',
      just return the string.  If we don't have gettext(), there's no use
      doing anything else. */
-#if defined (HAVE_GETTEXT)
   if (locale == 0 || locale[0] == '\0' ||
       (locale[0] == 'C' && locale[1] == '\0') || STREQ (locale, "POSIX"))
-#endif
     {
       t = (char *)xmalloc (len + 1);
       strcpy (t, string);
@@ -250,9 +345,12 @@ localetrans (string, len, lenp)
       return (t);
     }
 
-#if defined (HAVE_GETTEXT)
   /* Now try to translate it. */
-  translated = gettext (string);
+  if (default_domain && *default_domain)
+    translated = dgettext (default_domain, string);
+  else
+    translated = string;
+
   if (translated == string)    /* gettext returns its argument if untranslatable */
     {
       t = (char *)xmalloc (len + 1);
@@ -269,7 +367,6 @@ localetrans (string, len, lenp)
        *lenp = tlen;
     }
   return (t);
-#endif /* HAVE_GETTEXT */
 }
 
 /* Change a bash string into a string suitable for inclusion in a `po' file.
@@ -339,11 +436,12 @@ localeexpand (string, start, end, lineno, lenp)
   temp[tlen] = '\0';
 
   /* If we're just dumping translatable strings, don't do anything with the
-     string itself, but if we're dumping in `po' file format, convert it into a form more palatable to gettext(3)
-     and friends by quoting `"' and `\' with backslashes and converting <NL>
-     into `\n"<NL>"'.  If we find a newline in TEMP, we first output a
-     `msgid ""' line and then the translated string; otherwise we output the
-     `msgid' and translated string all on one line. */
+     string itself, but if we're dumping in `po' file format, convert it into
+     a form more palatable to gettext(3) and friends by quoting `"' and `\'
+     with backslashes and converting <NL> into `\n"<NL>"'.  If we find a
+     newline in TEMP, we first output a `msgid ""' line and then the
+     translated string; otherwise we output the `msgid' and translated
+     string all on one line. */
   if (dump_translatable_strings)
     {
       if (dump_po_strings)
@@ -378,3 +476,21 @@ localeexpand (string, start, end, lineno, lenp)
       return (temp);
     }
 }
+
+/* Set every character in the <blank> character class to be a shell break
+   character for the lexical analyzer when the locale changes. */
+static void
+locale_setblanks ()
+{
+  int x;
+
+  for (x = 0; x < sh_syntabsiz; x++)
+    {
+      if (isblank (x))
+       sh_syntaxtab[x] |= CSHBRK;
+      else if (member (x, shell_break_chars))
+       sh_syntaxtab[x] |= CSHBRK;
+      else
+       sh_syntaxtab[x] &= ~CSHBRK;
+    }
+}