[readline] Fix compilation on MinGW
[deliverable/binutils-gdb.git] / readline / nls.c
index f2d413d59d5a1d3a03217b598c226573c9693d2c..d2f67e0d3e1ac1a9f085d3e10fbc7af0c129d8e0 100644 (file)
@@ -1,24 +1,24 @@
 /* nls.c -- skeletal internationalization code. */
 
-/* Copyright (C) 1996 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2017 Free Software Foundation, Inc.
 
-   This file is part of the GNU Readline Library, a library for
-   reading lines of text with interactive input and history editing.
+   This file is part of the GNU Readline Library (Readline), a library
+   for reading lines of text with interactive input and history editing.      
 
-   The GNU Readline Library is free software; you can redistribute it
-   and/or modify it under the terms of the GNU General Public License
-   as published by the Free Software Foundation; either version 1, or
+   Readline is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
-   The GNU Readline Library 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
+   Readline 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 General Public License for more details.
 
-   The GNU General Public License is often shipped with GNU software, and
-   is generally kept in a file called COPYING or LICENSE.  If you do not
-   have a copy of the license, write to the Free Software Foundation,
-   675 Mass Ave, Cambridge, MA 02139, USA. */
+   You should have received a copy of the GNU General Public License
+   along with Readline.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
 #define READLINE_LIBRARY
 
 #if defined (HAVE_CONFIG_H)
@@ -27,6 +27,8 @@
 
 #include <sys/types.h>
 
+#include <stdio.h>
+
 #if defined (HAVE_UNISTD_H)
 #  include <unistd.h>
 #endif /* HAVE_UNISTD_H */
 #  include <locale.h>
 #endif
 
+#if defined (HAVE_LANGINFO_CODESET)
+#  include <langinfo.h>
+#endif
+
 #include <ctype.h>
 
 #include "rldefs.h"
+#include "readline.h"
+#include "rlshell.h"
+#include "rlprivate.h"
 
-extern int _rl_convert_meta_chars_to_ascii;
-extern int _rl_output_meta_chars;
-extern int _rl_meta_flag;
-
-/* Functions imported from shell.c */
-extern char *get_env_value ();
+static int utf8locale PARAMS((char *));
 
 #if !defined (HAVE_SETLOCALE)    
 /* A list of legal values for the LANG or LC_CTYPE environment variables.
@@ -70,27 +74,87 @@ static char *legal_lang_values[] =
  "iso88599",
  "iso885910",
  "koi8r",
- "koi8-r", 
+ "utf8",
   0
 };
 
-static char *normalize_codeset ();
-static char *find_codeset ();
+static char *normalize_codeset PARAMS((char *));
 #endif /* !HAVE_SETLOCALE */
 
+static char *find_codeset PARAMS((char *, size_t *));
+
+static char *_rl_get_locale_var PARAMS((const char *));
+
+static char *
+_rl_get_locale_var (const char *v)
+{
+  char *lspec;
+
+  lspec = sh_get_env_value ("LC_ALL");
+  if (lspec == 0 || *lspec == 0)
+    lspec = sh_get_env_value (v);
+  if (lspec == 0 || *lspec == 0)
+    lspec = sh_get_env_value ("LANG");
+
+  return lspec;
+}
+
+static int
+utf8locale (char *lspec)
+{
+  char *cp;
+  size_t len;
+
+#if HAVE_LANGINFO_CODESET
+  cp = nl_langinfo (CODESET);
+  return (STREQ (cp, "UTF-8") || STREQ (cp, "utf8"));
+#else
+  cp = find_codeset (lspec, &len);
+
+  if (cp == 0 || len < 4 || len > 5)
+    return 0;
+  return ((len == 5) ? strncmp (cp, "UTF-8", len) == 0 : strncmp (cp, "utf8", 4) == 0);
+#endif
+}
+
+/* Query the right environment variables and call setlocale() to initialize
+   the C library locale settings. */
+char *
+_rl_init_locale (void)
+{
+  char *ret, *lspec;
+
+  /* Set the LC_CTYPE locale category from environment variables. */
+  lspec = _rl_get_locale_var ("LC_CTYPE");
+  /* Since _rl_get_locale_var queries the right environment variables,
+     we query the current locale settings with setlocale(), and, if
+     that doesn't return anything, we set lspec to the empty string to
+     force the subsequent call to setlocale() to define the `native'
+     environment. */
+  if (lspec == 0 || *lspec == 0)
+    lspec = setlocale (LC_CTYPE, (char *)NULL);
+  if (lspec == 0)
+    lspec = "";
+  ret = setlocale (LC_CTYPE, lspec);   /* ok, since it does not change locale */
+
+  _rl_utf8locale = (ret && *ret) ? utf8locale (ret) : 0;
+
+  return ret;
+}
+
 /* Check for LC_ALL, LC_CTYPE, and LANG and use the first with a value
    to decide the defaults for 8-bit character input and output.  Returns
    1 if we set eight-bit mode. */
 int
-_rl_init_eightbit ()
+_rl_init_eightbit (void)
 {
 /* If we have setlocale(3), just check the current LC_CTYPE category
    value, and go into eight-bit mode if it's not C or POSIX. */
 #if defined (HAVE_SETLOCALE)
-  char *t;
+  char *lspec, *t;
+
+  t = _rl_init_locale ();      /* returns static pointer */
 
-  /* Set the LC_CTYPE locale category from environment variables. */
-  t = setlocale (LC_CTYPE, "");
   if (t && *t && (t[0] != 'C' || t[1]) && (STREQ (t, "POSIX") == 0))
     {
       _rl_meta_flag = 1;
@@ -108,9 +172,8 @@ _rl_init_eightbit ()
   /* We don't have setlocale.  Finesse it.  Check the environment for the
      appropriate variables and set eight-bit mode if they have the right
      values. */
-  lspec = get_env_value ("LC_ALL");
-  if (lspec == 0) lspec = get_env_value ("LC_CTYPE");
-  if (lspec == 0) lspec = get_env_value ("LANG");
+  lspec = _rl_get_locale_var ("LC_CTYPE");
+
   if (lspec == 0 || (t = normalize_codeset (lspec)) == 0)
     return (0);
   for (i = 0; t && legal_lang_values[i]; i++)
@@ -121,16 +184,17 @@ _rl_init_eightbit ()
        _rl_output_meta_chars = 1;
        break;
       }
-  free (t);
-  return (legal_lang_values[i] ? 1 : 0);
 
+  _rl_utf8locale = *t ? STREQ (t, "utf8") : 0;
+
+  xfree (t);
+  return (legal_lang_values[i] ? 1 : 0);
 #endif /* !HAVE_SETLOCALE */
 }
 
 #if !defined (HAVE_SETLOCALE)
 static char *
-normalize_codeset (codeset)
-     char *codeset;
+normalize_codeset (char *codeset)
 {
   size_t namelen, i;
   int len, all_digits;
@@ -144,10 +208,10 @@ normalize_codeset (codeset)
   all_digits = 1;
   for (len = 0, i = 0; i < namelen; i++)
     {
-      if (isalnum (codeset[i]))
+      if (ISALNUM ((unsigned char)codeset[i]))
        {
          len++;
-         all_digits &= isdigit (codeset[i]);
+         all_digits &= _rl_digit_p (codeset[i]);
        }
     }
 
@@ -165,20 +229,19 @@ normalize_codeset (codeset)
     }
 
   for (i = 0; i < namelen; i++)
-    if (isalpha (codeset[i]))
-      *wp++ = (isupper (codeset[i])) ? tolower (codeset[i]) : codeset[i];
-    else if (isdigit (codeset[i]))
+    if (ISALPHA ((unsigned char)codeset[i]))
+      *wp++ = _rl_to_lower (codeset[i]);
+    else if (_rl_digit_p (codeset[i]))
       *wp++ = codeset[i];
   *wp = '\0';
 
   return retval;
 }
+#endif /* !HAVE_SETLOCALE */
 
 /* Isolate codeset portion of locale specification. */
 static char *
-find_codeset (name, lenp)
-     char *name;
-     size_t *lenp;
+find_codeset (char *name, size_t *lenp)
 {
   char *cp, *language, *result;
 
@@ -225,4 +288,3 @@ find_codeset (name, lenp)
 
   return result;
 }
-#endif /* !HAVE_SETLOCALE */
This page took 0.028554 seconds and 4 git commands to generate.