summaryrefslogtreecommitdiff
path: root/nls.c
diff options
context:
space:
mode:
Diffstat (limited to 'nls.c')
-rw-r--r--nls.c96
1 files changed, 75 insertions, 21 deletions
diff --git a/nls.c b/nls.c
index d2f67e0..5c6a13b 100644
--- a/nls.c
+++ b/nls.c
@@ -1,6 +1,6 @@
/* nls.c -- skeletal internationalization code. */
-/* Copyright (C) 1996-2017 Free Software Foundation, Inc.
+/* Copyright (C) 1996-2022 Free Software Foundation, Inc.
This file is part of the GNU Readline Library (Readline), a library
for reading lines of text with interactive input and history editing.
@@ -53,8 +53,12 @@
#include "readline.h"
#include "rlshell.h"
#include "rlprivate.h"
+#include "xmalloc.h"
-static int utf8locale PARAMS((char *));
+static int utf8locale (char *);
+
+#define RL_DEFAULT_LOCALE "C"
+static char *_rl_current_locale = 0;
#if !defined (HAVE_SETLOCALE)
/* A list of legal values for the LANG or LC_CTYPE environment variables.
@@ -78,12 +82,12 @@ static char *legal_lang_values[] =
0
};
-static char *normalize_codeset PARAMS((char *));
+static char *normalize_codeset (char *);
#endif /* !HAVE_SETLOCALE */
-static char *find_codeset PARAMS((char *, size_t *));
+static char *find_codeset (char *, size_t *);
-static char *_rl_get_locale_var PARAMS((const char *));
+static char *_rl_get_locale_var (const char *);
static char *
_rl_get_locale_var (const char *v)
@@ -131,50 +135,61 @@ _rl_init_locale (void)
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 defined (HAVE_SETLOCALE)
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 */
+#else
+ ret = (lspec == 0 || *lspec == 0) ? RL_DEFAULT_LOCALE : lspec;
+#endif
_rl_utf8locale = (ret && *ret) ? utf8locale (ret) : 0;
+ _rl_current_locale = savestring (ret);
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 (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. */
+ value (passed as LOCALESTR), and go into eight-bit mode if it's not "C"
+ or "POSIX". If FORCE is non-zero, we reset the locale variables to values
+ appropriate for the C locale if the locale is "C" or "POSIX". FORCE is 0
+ when this is called from _rl_init_eightbit, since we're modifying the
+ default initial values and don't need to change anything else. If we
+ don't have setlocale(3), we check the codeset portion of LOCALESTR against
+ a set of known values and go into eight-bit mode if it matches one of those.
+ Returns 1 if we set eight-bit (multibyte) mode. */
+static int
+_rl_set_localevars (char *localestr, int force)
+{
#if defined (HAVE_SETLOCALE)
- char *lspec, *t;
-
- t = _rl_init_locale (); /* returns static pointer */
-
- if (t && *t && (t[0] != 'C' || t[1]) && (STREQ (t, "POSIX") == 0))
+ if (localestr && *localestr && (localestr[0] != 'C' || localestr[1]) && (STREQ (localestr, "POSIX") == 0))
{
_rl_meta_flag = 1;
_rl_convert_meta_chars_to_ascii = 0;
_rl_output_meta_chars = 1;
return (1);
}
+ else if (force)
+ {
+ /* Default "C" locale settings. */
+ _rl_meta_flag = 0;
+ _rl_convert_meta_chars_to_ascii = 1;
+ _rl_output_meta_chars = 0;
+ return (0);
+ }
else
return (0);
#else /* !HAVE_SETLOCALE */
- char *lspec, *t;
+ char *t;
int i;
/* 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 = _rl_get_locale_var ("LC_CTYPE");
-
- if (lspec == 0 || (t = normalize_codeset (lspec)) == 0)
+ if (localestr == 0 || (t = normalize_codeset (localestr)) == 0)
return (0);
for (i = 0; t && legal_lang_values[i]; i++)
if (STREQ (t, legal_lang_values[i]))
@@ -185,6 +200,14 @@ _rl_init_eightbit (void)
break;
}
+ if (force && legal_lang_values[i] == 0) /* didn't find it */
+ {
+ /* Default "C" locale settings. */
+ _rl_meta_flag = 0;
+ _rl_convert_meta_chars_to_ascii = 1;
+ _rl_output_meta_chars = 0;
+ }
+
_rl_utf8locale = *t ? STREQ (t, "utf8") : 0;
xfree (t);
@@ -192,6 +215,21 @@ _rl_init_eightbit (void)
#endif /* !HAVE_SETLOCALE */
}
+/* 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 (void)
+{
+ char *t, *ol;
+
+ ol = _rl_current_locale;
+ t = _rl_init_locale (); /* resets _rl_current_locale, returns static pointer */
+ xfree (ol);
+
+ return (_rl_set_localevars (t, 0));
+}
+
#if !defined (HAVE_SETLOCALE)
static char *
normalize_codeset (char *codeset)
@@ -288,3 +326,19 @@ find_codeset (char *name, size_t *lenp)
return result;
}
+
+void
+_rl_reset_locale (void)
+{
+ char *ol, *nl;
+
+ /* This should not be NULL; _rl_init_eightbit sets it on the first call to
+ readline() or rl_initialize(). */
+ ol = _rl_current_locale;
+ nl = _rl_init_locale (); /* resets _rl_current_locale */
+
+ if ((ol == 0 && nl) || (ol && nl && (STREQ (ol, nl) == 0)))
+ (void)_rl_set_localevars (nl, 1);
+
+ xfree (ol);
+}