fad520197c41b83023342e15935d40b798787ae0
[platform/upstream/bash.git] / lib / readline / nls.c
1 /* nls.c -- skeletal internationalization code. */
2
3 /* Copyright (C) 1996 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library, a library for
6    reading lines of text with interactive input and history editing.
7
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.
12
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.
17
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
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #if defined (HAVE_UNISTD_H)
29 #  include <unistd.h>
30 #endif /* HAVE_UNISTD_H */
31
32 #if defined (HAVE_STDLIB_H)
33 #  include <stdlib.h>
34 #else
35 #  include "ansi_stdlib.h"
36 #endif /* HAVE_STDLIB_H */
37
38 #if defined (HAVE_LOCALE_H)
39 #  include <locale.h>
40 #endif
41
42 #include <ctype.h>
43
44 #include "rldefs.h"
45
46 extern int _rl_convert_meta_chars_to_ascii;
47 extern int _rl_output_meta_chars;
48 extern int _rl_meta_flag;
49     
50 /* A list of legal values for the LANG or LC_CTYPE environment variables.
51    If a locale name in this list is the value for the LC_ALL, LC_CTYPE,
52    or LANG environment variable (using the first of those with a value),
53    readline eight-bit mode is enabled. */
54 static char *legal_lang_values[] =
55 {
56  "iso88591",
57  "iso88592",
58  "iso88593",
59  "iso88594",
60  "iso88595",
61  "iso88596",
62  "iso88597",
63  "iso88598",
64  "iso88599",
65  "iso885910",
66  "koi8r",   
67   0
68 };
69
70 static char *normalize_codeset ();
71 static char *find_codeset ();
72
73 /* Check for LC_ALL, LC_CTYPE, and LANG and use the first with a value
74    to decide the defaults for 8-bit character input and output.  Returns
75    1 if we set eight-bit mode. */
76 int
77 _rl_init_eightbit ()
78 {
79   char *lspec, *t;
80   int i;
81
82   lspec = getenv ("LC_ALL");
83   if (lspec == 0) lspec = getenv ("LC_CTYPE");
84   if (lspec == 0) lspec = getenv ("LANG");
85   if (lspec == 0 || (t = normalize_codeset (lspec)) == 0)
86     return (0);
87   for (i = 0; t && legal_lang_values[i]; i++)
88     if (STREQ (t, legal_lang_values[i]))
89       {
90         _rl_meta_flag = 1;
91         _rl_convert_meta_chars_to_ascii = 0;
92         _rl_output_meta_chars = 1;
93 #if defined (HAVE_SETLOCALE)
94         setlocale (LC_CTYPE, lspec);
95 #endif
96         break;
97       }
98   free (t);
99   return (legal_lang_values[i] ? 1 : 0);
100 }
101
102 static char *
103 normalize_codeset (codeset)
104      char *codeset;
105 {
106   size_t namelen, i;
107   int len, all_digits;
108   char *wp, *retval;
109
110   codeset = find_codeset (codeset, &namelen);
111
112   if (codeset == 0)
113     return (codeset);
114
115   all_digits = 1;
116   for (len = 0, i = 0; i < namelen; i++)
117     {
118       if (isalnum (codeset[i]))
119         {
120           len++;
121           all_digits &= isdigit (codeset[i]);
122         }
123     }
124
125   retval = (char *)malloc ((all_digits ? 3 : 0) + len + 1);
126   if (retval == 0)
127     return ((char *)0);
128
129   wp = retval;
130   /* Add `iso' to beginning of an all-digit codeset */
131   if (all_digits)
132     {
133       *wp++ = 'i';
134       *wp++ = 's';
135       *wp++ = 'o';
136     }
137
138   for (i = 0; i < namelen; i++)
139     if (isalpha (codeset[i]))
140       *wp++ = (isupper (codeset[i])) ? tolower (codeset[i]) : codeset[i];
141     else if (isdigit (codeset[i]))
142       *wp++ = codeset[i];
143   *wp = '\0';
144
145   return retval;
146 }
147
148 /* Isolate codeset portion of locale specification. */
149 static char *
150 find_codeset (name, lenp)
151      char *name;
152      size_t *lenp;
153 {
154   char *cp, *language, *result;
155
156   cp = language = name;
157   result = (char *)0;
158
159   while (*cp && *cp != '_' && *cp != '@' && *cp != '+' && *cp != ',')
160     cp++;
161
162   /* This does not make sense: language has to be specified.  As
163      an exception we allow the variable to contain only the codeset
164      name.  Perhaps there are funny codeset names.  */
165   if (language == cp) 
166     {
167       *lenp = strlen (language);
168       result = language;
169     }
170   else
171     {
172       /* Next is the territory. */
173       if (*cp == '_')
174         do
175           ++cp;
176         while (*cp && *cp != '.' && *cp != '@' && *cp != '+' && *cp != ',' && *cp != '_');
177
178       /* Now, finally, is the codeset. */
179       result = cp;
180       if (*cp == '.')
181         do
182           ++cp;
183         while (*cp && *cp != '@');
184
185       if (cp - result > 2)
186         {
187           result++;
188           *lenp = cp - result;
189         }
190       else
191         {
192           *lenp = strlen (language);
193           result = language;
194         }
195     }
196
197   return result;
198 }