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 1, 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 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_UNISTD_H)
32 #endif /* HAVE_UNISTD_H */
34 #if defined (HAVE_STDLIB_H)
37 # include "ansi_stdlib.h"
38 #endif /* HAVE_STDLIB_H */
40 #if defined (HAVE_LOCALE_H)
48 extern int _rl_convert_meta_chars_to_ascii;
49 extern int _rl_output_meta_chars;
50 extern int _rl_meta_flag;
52 /* Functions imported from shell.c */
53 extern char *get_env_value ();
55 #if !defined (HAVE_SETLOCALE)
56 /* A list of legal values for the LANG or LC_CTYPE environment variables.
57 If a locale name in this list is the value for the LC_ALL, LC_CTYPE,
58 or LANG environment variable (using the first of those with a value),
59 readline eight-bit mode is enabled. */
60 static char *legal_lang_values[] =
76 static char *normalize_codeset ();
77 static char *find_codeset ();
78 #endif /* !HAVE_SETLOCALE */
80 /* Check for LC_ALL, LC_CTYPE, and LANG and use the first with a value
81 to decide the defaults for 8-bit character input and output. Returns
82 1 if we set eight-bit mode. */
86 /* If we have setlocale(3), just check the current LC_CTYPE category
87 value, and go into eight-bit mode if it's not C or POSIX. */
88 #if defined (HAVE_SETLOCALE)
91 /* Set the LC_CTYPE locale category from environment variables. */
92 t = setlocale (LC_CTYPE, "");
93 if (t && *t && (t[0] != 'C' || t[1]) && (STREQ (t, "POSIX") == 0))
96 _rl_convert_meta_chars_to_ascii = 0;
97 _rl_output_meta_chars = 1;
103 #else /* !HAVE_SETLOCALE */
107 /* We don't have setlocale. Finesse it. Check the environment for the
108 appropriate variables and set eight-bit mode if they have the right
110 lspec = get_env_value ("LC_ALL");
111 if (lspec == 0) lspec = get_env_value ("LC_CTYPE");
112 if (lspec == 0) lspec = get_env_value ("LANG");
113 if (lspec == 0 || (t = normalize_codeset (lspec)) == 0)
115 for (i = 0; t && legal_lang_values[i]; i++)
116 if (STREQ (t, legal_lang_values[i]))
119 _rl_convert_meta_chars_to_ascii = 0;
120 _rl_output_meta_chars = 1;
124 return (legal_lang_values[i] ? 1 : 0);
126 #endif /* !HAVE_SETLOCALE */
129 #if !defined (HAVE_SETLOCALE)
131 normalize_codeset (codeset)
138 codeset = find_codeset (codeset, &namelen);
144 for (len = 0, i = 0; i < namelen; i++)
146 if (isalnum (codeset[i]))
149 all_digits &= isdigit (codeset[i]);
153 retval = (char *)malloc ((all_digits ? 3 : 0) + len + 1);
158 /* Add `iso' to beginning of an all-digit codeset */
166 for (i = 0; i < namelen; i++)
167 if (isalpha (codeset[i]))
168 *wp++ = (isupper (codeset[i])) ? tolower (codeset[i]) : codeset[i];
169 else if (isdigit (codeset[i]))
176 /* Isolate codeset portion of locale specification. */
178 find_codeset (name, lenp)
182 char *cp, *language, *result;
184 cp = language = name;
187 while (*cp && *cp != '_' && *cp != '@' && *cp != '+' && *cp != ',')
190 /* This does not make sense: language has to be specified. As
191 an exception we allow the variable to contain only the codeset
192 name. Perhaps there are funny codeset names. */
195 *lenp = strlen (language);
200 /* Next is the territory. */
204 while (*cp && *cp != '.' && *cp != '@' && *cp != '+' && *cp != ',' && *cp != '_');
206 /* Now, finally, is the codeset. */
211 while (*cp && *cp != '@');
220 *lenp = strlen (language);
227 #endif /* !HAVE_SETLOCALE */