1 /* nls.c -- skeletal internationalization code. */
3 /* Copyright (C) 1996 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
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 /* Check for LC_ALL, LC_CTYPE, and LANG and use the first with a value
79 to decide the defaults for 8-bit character input and output. Returns
80 1 if we set eight-bit mode. */
84 /* If we have setlocale(3), just check the current LC_CTYPE category
85 value, and go into eight-bit mode if it's not C or POSIX. */
86 #if defined (HAVE_SETLOCALE)
89 /* Set the LC_CTYPE locale category from environment variables. */
90 t = setlocale (LC_CTYPE, "");
91 if (t && *t && (t[0] != 'C' || t[1]) && (STREQ (t, "POSIX") == 0))
94 _rl_convert_meta_chars_to_ascii = 0;
95 _rl_output_meta_chars = 1;
101 #else /* !HAVE_SETLOCALE */
105 /* We don't have setlocale. Finesse it. Check the environment for the
106 appropriate variables and set eight-bit mode if they have the right
108 lspec = sh_get_env_value ("LC_ALL");
109 if (lspec == 0) lspec = sh_get_env_value ("LC_CTYPE");
110 if (lspec == 0) lspec = sh_get_env_value ("LANG");
111 if (lspec == 0 || (t = normalize_codeset (lspec)) == 0)
113 for (i = 0; t && legal_lang_values[i]; i++)
114 if (STREQ (t, legal_lang_values[i]))
117 _rl_convert_meta_chars_to_ascii = 0;
118 _rl_output_meta_chars = 1;
122 return (legal_lang_values[i] ? 1 : 0);
124 #endif /* !HAVE_SETLOCALE */
127 #if !defined (HAVE_SETLOCALE)
129 normalize_codeset (codeset)
136 codeset = find_codeset (codeset, &namelen);
142 for (len = 0, i = 0; i < namelen; i++)
144 if (ISALNUM ((unsigned char)codeset[i]))
147 all_digits &= _rl_digit_p (codeset[i]);
151 retval = (char *)malloc ((all_digits ? 3 : 0) + len + 1);
156 /* Add `iso' to beginning of an all-digit codeset */
164 for (i = 0; i < namelen; i++)
165 if (ISALPHA ((unsigned char)codeset[i]))
166 *wp++ = _rl_to_lower (codeset[i]);
167 else if (_rl_digit_p (codeset[i]))
174 /* Isolate codeset portion of locale specification. */
176 find_codeset (name, lenp)
180 char *cp, *language, *result;
182 cp = language = name;
185 while (*cp && *cp != '_' && *cp != '@' && *cp != '+' && *cp != ',')
188 /* This does not make sense: language has to be specified. As
189 an exception we allow the variable to contain only the codeset
190 name. Perhaps there are funny codeset names. */
193 *lenp = strlen (language);
198 /* Next is the territory. */
202 while (*cp && *cp != '.' && *cp != '@' && *cp != '+' && *cp != ',' && *cp != '_');
204 /* Now, finally, is the codeset. */
209 while (*cp && *cp != '@');
218 *lenp = strlen (language);
225 #endif /* !HAVE_SETLOCALE */