1 /* nls.c -- skeletal internationalization code. */
3 /* Copyright (C) 1996-2009 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)
51 #include "rlprivate.h"
53 #if !defined (HAVE_SETLOCALE)
54 /* A list of legal values for the LANG or LC_CTYPE environment variables.
55 If a locale name in this list is the value for the LC_ALL, LC_CTYPE,
56 or LANG environment variable (using the first of those with a value),
57 readline eight-bit mode is enabled. */
58 static char *legal_lang_values[] =
74 static char *normalize_codeset PARAMS((char *));
75 static char *find_codeset PARAMS((char *, size_t *));
76 #endif /* !HAVE_SETLOCALE */
78 static char *_rl_get_locale_var PARAMS((const char *));
81 _rl_get_locale_var (v)
86 lspec = sh_get_env_value ("LC_ALL");
87 if (lspec == 0 || *lspec == 0)
88 lspec = sh_get_env_value (v);
89 if (lspec == 0 || *lspec == 0)
90 lspec = sh_get_env_value ("LANG");
95 /* Check for LC_ALL, LC_CTYPE, and LANG and use the first with a value
96 to decide the defaults for 8-bit character input and output. Returns
97 1 if we set eight-bit mode. */
101 /* If we have setlocale(3), just check the current LC_CTYPE category
102 value, and go into eight-bit mode if it's not C or POSIX. */
103 #if defined (HAVE_SETLOCALE)
106 /* Set the LC_CTYPE locale category from environment variables. */
107 lspec = _rl_get_locale_var ("LC_CTYPE");
108 /* Since _rl_get_locale_var queries the right environment variables,
109 we query the current locale settings with setlocale(), and, if
110 that doesn't return anything, we set lspec to the empty string to
111 force the subsequent call to setlocale() to define the `native'
113 if (lspec == 0 || *lspec == 0)
114 lspec = setlocale (LC_CTYPE, (char *)NULL);
117 t = setlocale (LC_CTYPE, lspec);
119 if (t && *t && (t[0] != 'C' || t[1]) && (STREQ (t, "POSIX") == 0))
122 _rl_convert_meta_chars_to_ascii = 0;
123 _rl_output_meta_chars = 1;
129 #else /* !HAVE_SETLOCALE */
133 /* We don't have setlocale. Finesse it. Check the environment for the
134 appropriate variables and set eight-bit mode if they have the right
136 lspec = _rl_get_locale_var ("LC_CTYPE");
138 if (lspec == 0 || (t = normalize_codeset (lspec)) == 0)
140 for (i = 0; t && legal_lang_values[i]; i++)
141 if (STREQ (t, legal_lang_values[i]))
144 _rl_convert_meta_chars_to_ascii = 0;
145 _rl_output_meta_chars = 1;
149 return (legal_lang_values[i] ? 1 : 0);
151 #endif /* !HAVE_SETLOCALE */
154 #if !defined (HAVE_SETLOCALE)
156 normalize_codeset (codeset)
163 codeset = find_codeset (codeset, &namelen);
169 for (len = 0, i = 0; i < namelen; i++)
171 if (ISALNUM ((unsigned char)codeset[i]))
174 all_digits &= _rl_digit_p (codeset[i]);
178 retval = (char *)malloc ((all_digits ? 3 : 0) + len + 1);
183 /* Add `iso' to beginning of an all-digit codeset */
191 for (i = 0; i < namelen; i++)
192 if (ISALPHA ((unsigned char)codeset[i]))
193 *wp++ = _rl_to_lower (codeset[i]);
194 else if (_rl_digit_p (codeset[i]))
201 /* Isolate codeset portion of locale specification. */
203 find_codeset (name, lenp)
207 char *cp, *language, *result;
209 cp = language = name;
212 while (*cp && *cp != '_' && *cp != '@' && *cp != '+' && *cp != ',')
215 /* This does not make sense: language has to be specified. As
216 an exception we allow the variable to contain only the codeset
217 name. Perhaps there are funny codeset names. */
220 *lenp = strlen (language);
225 /* Next is the territory. */
229 while (*cp && *cp != '.' && *cp != '@' && *cp != '+' && *cp != ',' && *cp != '_');
231 /* Now, finally, is the codeset. */
236 while (*cp && *cp != '@');
245 *lenp = strlen (language);
252 #endif /* !HAVE_SETLOCALE */