24c30c7860dcbf85cbf4d472d59714667809616e
[platform/upstream/bash.git] / locale.c
1 /* locale.c - Miscellaneous internationalization functions. */
2
3 /* Copyright (C) 1996 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
7    Bash is free software; you can redistribute it and/or modify it under
8    the terms of the GNU General Public License as published by the Free
9    Software Foundation; either version 2, or (at your option) any later
10    version.
11
12    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13    WARRANTY; without even the implied warranty of MERCHANTABILITY or
14    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15    for more details.
16
17    You should have received a copy of the GNU General Public License along
18    with Bash; see the file COPYING.  If not, write to the Free Software
19    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "config.h"
22
23 #include "bashtypes.h"
24
25 #if defined (HAVE_UNISTD_H)
26 #  include <unistd.h>
27 #endif
28
29 #include "bashintl.h"
30 #include "bashansi.h"
31 #include <stdio.h>
32 #include <ctype.h>
33
34 #include "shell.h"
35
36 /* The current locale when the program begins */
37 static char *default_locale;
38
39 /* The current domain for textdomain(3). */
40 static char *default_domain;
41 static char *default_dir;
42
43 /* tracks the value of LC_ALL; used to override values for other locale
44    categories */
45 static char *lc_all;
46
47 /* Set the value of default_locale and make the current locale the
48    system default locale.  This should be called very early in main(). */
49 void
50 set_default_locale ()
51 {
52 #if defined (HAVE_SETLOCALE)
53   default_locale = setlocale (LC_ALL, "");
54   if (default_locale)
55     default_locale = savestring (default_locale);
56 #endif /* HAVE_SETLOCALE */
57 }
58
59 /* Set default values for LC_CTYPE, LC_COLLATE, and LC_MESSAGES if they
60    are not specified in the environment, but LANG or LC_ALL is.  This
61    should be called from main() after parsing the environment. */
62 void
63 set_default_locale_vars ()
64 {
65   char *val;
66
67 #if defined (HAVE_SETLOCALE)
68   val = get_string_value ("LC_CTYPE");
69   if (val == 0 && lc_all && *lc_all)
70     setlocale (LC_CTYPE, lc_all);
71
72 #  if defined (LC_COLLATE)
73   val = get_string_value ("LC_COLLATE");
74   if (val == 0 && lc_all && *lc_all)
75     setlocale (LC_COLLATE, lc_all);
76 #  endif /* LC_COLLATE */
77
78 #  if defined (LC_MESSAGES)
79   val = get_string_value ("LC_MESSAGES");
80   if (val == 0 && lc_all && *lc_all)
81     setlocale (LC_MESSAGES, lc_all);
82 #  endif /* LC_MESSAGES */
83
84 #endif /* HAVE_SETLOCALE */
85
86   val = get_string_value ("TEXTDOMAIN");
87   if (val && *val)
88     {
89       FREE (default_domain);
90       default_domain = savestring (val);
91       textdomain (default_domain);
92     }
93
94   val = get_string_value ("TEXTDOMAINDIR");
95   if (val && *val)
96     {
97       FREE (default_dir);
98       default_dir = savestring (val);
99       bindtextdomain (default_domain, default_dir);
100     }
101 }
102
103 /* Set one of the locale categories (specified by VAR) to VALUE.  Returns 1
104   if successful, 0 otherwise. */
105 int
106 set_locale_var (var, value)
107      char *var, *value;
108 {
109   if (var[0] == 'T' && var[10] == 0)            /* TEXTDOMAIN */
110     {
111       FREE (default_domain);
112       default_domain = value ? savestring (value) : (char *)NULL;
113       textdomain (default_domain);
114       return (1);
115     }
116   else if (var[0] == 'T')                       /* TEXTDOMAINDIR */
117     {
118       FREE (default_dir);
119       default_dir = value ? savestring (value) : (char *)NULL;
120       bindtextdomain (default_domain, default_dir);
121       return (1);
122     }
123
124   /* var[0] == 'L' && var[1] == 'C' && var[2] == '_' */
125
126   else if (var[3] == 'A')                       /* LC_ALL */
127     {
128       FREE (lc_all);
129       if (value)
130         lc_all = savestring (value);
131       else if (default_locale)
132         lc_all = savestring (default_locale);
133       else
134         {
135           lc_all = xmalloc (1);
136           lc_all[0] = '\0';
137         }
138 #if defined (HAVE_SETLOCALE)
139       return (setlocale (LC_ALL, value) != 0);
140 #else
141       return (1);
142 #endif
143     }
144
145 #if defined (HAVE_SETLOCALE)
146   else if (var[3] == 'C' && var[4] == 'T')      /* LC_CTYPE */
147     {
148       if (lc_all == 0 || *lc_all == '\0')
149         return (setlocale (LC_CTYPE, value) != 0);
150     }
151   else if (var[3] == 'C' && var[4] == 'O')      /* LC_COLLATE */
152     {
153 #  if defined (LC_COLLATE)
154       if (lc_all == 0 || *lc_all == '\0')
155         return (setlocale (LC_COLLATE, value) != 0);
156 #  endif /* LC_COLLATE */
157     }
158   else if (var[3] == 'M' && var[4] == 'E')      /* LC_MESSAGES */
159     {
160 #  if defined (LC_MESSAGES)
161       if (lc_all == 0 || *lc_all == '\0')
162         return (setlocale (LC_MESSAGES, value) != 0);
163 #  endif /* LC_MESSAGES */
164     }
165 #endif /* HAVE_SETLOCALE */
166
167   return (0);
168 }
169
170 #if 0
171 /* Called when LANG is assigned a value.  Sets LC_ALL if that has not
172    already been set. */
173 #else
174 /* This no longer does anything; we rely on the C library for correct
175    behavior. */
176 #endif
177 int
178 set_lang (var, value)
179      char *var, *value;
180 {
181 #if 0
182   return ((lc_all == 0) ? set_locale_var ("LC_ALL", value) : 0);
183 #else
184   return 0;
185 #endif
186 }
187
188 /* Get the value of one of the locale variables (LC_MESSAGES, LC_CTYPE) */
189 char *
190 get_locale_var (var)
191      char *var;
192 {
193   char *locale;
194
195   locale = lc_all;
196
197   if (locale == 0)
198     locale = get_string_value (var);
199   if (locale == 0)
200     locale = default_locale;
201
202   return (locale);
203 }
204
205 /* Translate the contents of STRING, a $"..." quoted string, according
206    to the current locale.  In the `C' or `POSIX' locale, or if gettext()
207    is not available, the passed string is returned unchanged.  The
208    length of the translated string is returned in LENP, if non-null. */
209 char *
210 localetrans (string, len, lenp)
211      char *string;
212      int len, *lenp;
213 {
214   char *locale, *t;
215 #if defined (HAVE_GETTEXT)
216   char *translated;
217   int tlen;
218 #endif
219
220   /* Don't try to translate null strings. */
221   if (string == 0 || *string == 0)
222     {
223       if (lenp)
224         *lenp = 0;
225       return ((char *)NULL);
226     }
227
228   t = xmalloc (len + 1);
229
230   locale = get_locale_var ("LC_MESSAGES");
231
232   /* If we don't have setlocale() or the current locale is `C' or `POSIX',
233      just return the string.  If we don't have gettext(), there's no use
234      doing anything else. */
235 #if defined (HAVE_GETTEXT)
236   if (locale == 0 || locale[0] == '\0' ||
237       (locale[0] == 'C' && locale[1] == '\0') || STREQ (locale, "POSIX"))
238 #endif
239     {
240       strcpy (t, string);
241       if (lenp)
242         *lenp = len;
243       return (t);
244     }
245
246 #if defined (HAVE_GETTEXT)
247   /* Now try to translate it. */
248   translated = gettext (string);
249   if (translated == string)     /* gettext returns its argument if untranslatable */
250     {
251       strcpy (t, string);
252       if (lenp)
253         *lenp = len;
254     }
255   else
256     {
257       free (t);
258       tlen = strlen (translated);
259       t = xmalloc (tlen + 1);
260       strcpy (t, translated);
261       if (lenp)
262         *lenp = tlen;
263     }
264   return (t);
265 #endif /* HAVE_GETTEXT */
266 }