1 /* nls.c -- skeletal internationalization code. */
3 /* Copyright (C) 1996-2017 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
34 #endif /* HAVE_UNISTD_H */
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
42 #if defined (HAVE_LOCALE_H)
46 #if defined (HAVE_LANGINFO_CODESET)
47 # include <langinfo.h>
55 #include "rlprivate.h"
57 static int utf8locale PARAMS((char *));
59 #if !defined (HAVE_SETLOCALE)
60 /* A list of legal values for the LANG or LC_CTYPE environment variables.
61 If a locale name in this list is the value for the LC_ALL, LC_CTYPE,
62 or LANG environment variable (using the first of those with a value),
63 readline eight-bit mode is enabled. */
64 static char *legal_lang_values[] =
81 static char *normalize_codeset PARAMS((char *));
82 #endif /* !HAVE_SETLOCALE */
84 static char *find_codeset PARAMS((char *, size_t *));
86 static char *_rl_get_locale_var PARAMS((const char *));
89 _rl_get_locale_var (const char *v)
93 lspec = sh_get_env_value ("LC_ALL");
94 if (lspec == 0 || *lspec == 0)
95 lspec = sh_get_env_value (v);
96 if (lspec == 0 || *lspec == 0)
97 lspec = sh_get_env_value ("LANG");
103 utf8locale (char *lspec)
108 #if HAVE_LANGINFO_CODESET
109 cp = nl_langinfo (CODESET);
110 return (STREQ (cp, "UTF-8") || STREQ (cp, "utf8"));
112 cp = find_codeset (lspec, &len);
114 if (cp == 0 || len < 4 || len > 5)
116 return ((len == 5) ? strncmp (cp, "UTF-8", len) == 0 : strncmp (cp, "utf8", 4) == 0);
120 /* Query the right environment variables and call setlocale() to initialize
121 the C library locale settings. */
123 _rl_init_locale (void)
127 /* Set the LC_CTYPE locale category from environment variables. */
128 lspec = _rl_get_locale_var ("LC_CTYPE");
129 /* Since _rl_get_locale_var queries the right environment variables,
130 we query the current locale settings with setlocale(), and, if
131 that doesn't return anything, we set lspec to the empty string to
132 force the subsequent call to setlocale() to define the `native'
134 if (lspec == 0 || *lspec == 0)
135 lspec = setlocale (LC_CTYPE, (char *)NULL);
138 ret = setlocale (LC_CTYPE, lspec); /* ok, since it does not change locale */
140 _rl_utf8locale = (ret && *ret) ? utf8locale (ret) : 0;
145 /* Check for LC_ALL, LC_CTYPE, and LANG and use the first with a value
146 to decide the defaults for 8-bit character input and output. Returns
147 1 if we set eight-bit mode. */
149 _rl_init_eightbit (void)
151 /* If we have setlocale(3), just check the current LC_CTYPE category
152 value, and go into eight-bit mode if it's not C or POSIX. */
153 #if defined (HAVE_SETLOCALE)
156 t = _rl_init_locale (); /* returns static pointer */
158 if (t && *t && (t[0] != 'C' || t[1]) && (STREQ (t, "POSIX") == 0))
161 _rl_convert_meta_chars_to_ascii = 0;
162 _rl_output_meta_chars = 1;
168 #else /* !HAVE_SETLOCALE */
172 /* We don't have setlocale. Finesse it. Check the environment for the
173 appropriate variables and set eight-bit mode if they have the right
175 lspec = _rl_get_locale_var ("LC_CTYPE");
177 if (lspec == 0 || (t = normalize_codeset (lspec)) == 0)
179 for (i = 0; t && legal_lang_values[i]; i++)
180 if (STREQ (t, legal_lang_values[i]))
183 _rl_convert_meta_chars_to_ascii = 0;
184 _rl_output_meta_chars = 1;
188 _rl_utf8locale = *t ? STREQ (t, "utf8") : 0;
191 return (legal_lang_values[i] ? 1 : 0);
192 #endif /* !HAVE_SETLOCALE */
195 #if !defined (HAVE_SETLOCALE)
197 normalize_codeset (char *codeset)
203 codeset = find_codeset (codeset, &namelen);
209 for (len = 0, i = 0; i < namelen; i++)
211 if (ISALNUM ((unsigned char)codeset[i]))
214 all_digits &= _rl_digit_p (codeset[i]);
218 retval = (char *)malloc ((all_digits ? 3 : 0) + len + 1);
223 /* Add `iso' to beginning of an all-digit codeset */
231 for (i = 0; i < namelen; i++)
232 if (ISALPHA ((unsigned char)codeset[i]))
233 *wp++ = _rl_to_lower (codeset[i]);
234 else if (_rl_digit_p (codeset[i]))
240 #endif /* !HAVE_SETLOCALE */
242 /* Isolate codeset portion of locale specification. */
244 find_codeset (char *name, size_t *lenp)
246 char *cp, *language, *result;
248 cp = language = name;
251 while (*cp && *cp != '_' && *cp != '@' && *cp != '+' && *cp != ',')
254 /* This does not make sense: language has to be specified. As
255 an exception we allow the variable to contain only the codeset
256 name. Perhaps there are funny codeset names. */
259 *lenp = strlen (language);
264 /* Next is the territory. */
268 while (*cp && *cp != '.' && *cp != '@' && *cp != '+' && *cp != ',' && *cp != '_');
270 /* Now, finally, is the codeset. */
275 while (*cp && *cp != '@');
284 *lenp = strlen (language);