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)
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
[] =
80 static char *normalize_codeset
PARAMS((char *));
81 #endif /* !HAVE_SETLOCALE */
83 static char *find_codeset
PARAMS((char *, size_t *));
85 static char *_rl_get_locale_var
PARAMS((const char *));
88 _rl_get_locale_var (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");
109 #if HAVE_LANGINFO_CODESET
110 cp
= nl_langinfo (CODESET
);
111 return (STREQ (cp
, "UTF-8") || STREQ (cp
, "utf8"));
113 cp
= find_codeset (lspec
, &len
);
115 if (cp
== 0 || len
< 4 || len
> 5)
117 return ((len
== 5) ? strncmp (cp
, "UTF-8", len
) == 0 : strncmp (cp
, "utf8", 4) == 0);
121 /* Check for LC_ALL, LC_CTYPE, and LANG and use the first with a value
122 to decide the defaults for 8-bit character input and output. Returns
123 1 if we set eight-bit mode. */
127 /* If we have setlocale(3), just check the current LC_CTYPE category
128 value, and go into eight-bit mode if it's not C or POSIX. */
129 #if defined (HAVE_SETLOCALE)
132 /* Set the LC_CTYPE locale category from environment variables. */
133 lspec
= _rl_get_locale_var ("LC_CTYPE");
134 /* Since _rl_get_locale_var queries the right environment variables,
135 we query the current locale settings with setlocale(), and, if
136 that doesn't return anything, we set lspec to the empty string to
137 force the subsequent call to setlocale() to define the `native'
139 if (lspec
== 0 || *lspec
== 0)
140 lspec
= setlocale (LC_CTYPE
, (char *)NULL
);
143 t
= setlocale (LC_CTYPE
, lspec
);
146 _rl_utf8locale
= utf8locale (t
);
148 if (t
&& *t
&& (t
[0] != 'C' || t
[1]) && (STREQ (t
, "POSIX") == 0))
151 _rl_convert_meta_chars_to_ascii
= 0;
152 _rl_output_meta_chars
= 1;
158 #else /* !HAVE_SETLOCALE */
162 /* We don't have setlocale. Finesse it. Check the environment for the
163 appropriate variables and set eight-bit mode if they have the right
165 lspec
= _rl_get_locale_var ("LC_CTYPE");
167 if (lspec
== 0 || (t
= normalize_codeset (lspec
)) == 0)
169 for (i
= 0; t
&& legal_lang_values
[i
]; i
++)
170 if (STREQ (t
, legal_lang_values
[i
]))
173 _rl_convert_meta_chars_to_ascii
= 0;
174 _rl_output_meta_chars
= 1;
178 return (legal_lang_values
[i
] ? 1 : 0);
180 #endif /* !HAVE_SETLOCALE */
183 #if !defined (HAVE_SETLOCALE)
185 normalize_codeset (codeset
)
192 codeset
= find_codeset (codeset
, &namelen
);
198 for (len
= 0, i
= 0; i
< namelen
; i
++)
200 if (ISALNUM ((unsigned char)codeset
[i
]))
203 all_digits
&= _rl_digit_p (codeset
[i
]);
207 retval
= (char *)malloc ((all_digits
? 3 : 0) + len
+ 1);
212 /* Add `iso' to beginning of an all-digit codeset */
220 for (i
= 0; i
< namelen
; i
++)
221 if (ISALPHA ((unsigned char)codeset
[i
]))
222 *wp
++ = _rl_to_lower (codeset
[i
]);
223 else if (_rl_digit_p (codeset
[i
]))
229 #endif /* !HAVE_SETLOCALE */
231 /* Isolate codeset portion of locale specification. */
233 find_codeset (name
, lenp
)
237 char *cp
, *language
, *result
;
239 cp
= language
= name
;
242 while (*cp
&& *cp
!= '_' && *cp
!= '@' && *cp
!= '+' && *cp
!= ',')
245 /* This does not make sense: language has to be specified. As
246 an exception we allow the variable to contain only the codeset
247 name. Perhaps there are funny codeset names. */
250 *lenp
= strlen (language
);
255 /* Next is the territory. */
259 while (*cp
&& *cp
!= '.' && *cp
!= '@' && *cp
!= '+' && *cp
!= ',' && *cp
!= '_');
261 /* Now, finally, is the codeset. */
266 while (*cp
&& *cp
!= '@');
275 *lenp
= strlen (language
);