1 /* locale.c - Miscellaneous internationalization functions. */
3 /* Copyright (C) 1996-2009,2012,2016 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #include "bashtypes.h"
25 #if defined (HAVE_UNISTD_H)
29 #if HAVE_LANGINFO_CODESET
30 # include <langinfo.h>
36 #include "chartypes.h"
40 #include "input.h" /* For bash_input */
46 int locale_utf8locale
;
47 int locale_mb_cur_max
; /* value of MB_CUR_MAX for current locale (LC_CTYPE) */
48 int locale_shiftstates
;
50 extern int dump_translatable_strings
, dump_po_strings
;
52 /* The current locale when the program begins */
53 static char *default_locale
;
55 /* The current domain for textdomain(3). */
56 static char *default_domain
;
57 static char *default_dir
;
59 /* tracks the value of LC_ALL; used to override values for other locale
63 /* tracks the value of LC_ALL; used to provide defaults for locale
67 /* Called to reset all of the locale variables to their appropriate values
68 if (and only if) LC_ALL has not been assigned a value. */
69 static int reset_locale_vars
__P((void));
71 static void locale_setblanks
__P((void));
72 static int locale_isutf8
__P((char *));
74 /* Set the value of default_locale and make the current locale the
75 system default locale. This should be called very early in main(). */
79 #if defined (HAVE_SETLOCALE)
80 default_locale
= setlocale (LC_ALL
, "");
82 default_locale
= savestring (default_locale
);
83 #endif /* HAVE_SETLOCALE */
84 bindtextdomain (PACKAGE
, LOCALEDIR
);
87 locale_mb_cur_max
= MB_CUR_MAX
;
88 locale_utf8locale
= locale_isutf8 (default_locale
);
89 locale_shiftstates
= mblen ((char *)NULL
, 0);
92 /* Set default values for LC_CTYPE, LC_COLLATE, LC_MESSAGES, LC_NUMERIC and
93 LC_TIME if they are not specified in the environment, but LC_ALL is. This
94 should be called from main() after parsing the environment. */
96 set_default_locale_vars ()
100 #if defined (HAVE_SETLOCALE)
102 # if defined (LC_CTYPE)
103 val
= get_string_value ("LC_CTYPE");
104 if (val
== 0 && lc_all
&& *lc_all
)
106 setlocale (LC_CTYPE
, lc_all
);
108 locale_mb_cur_max
= MB_CUR_MAX
;
109 locale_utf8locale
= locale_isutf8 (lc_all
);
110 locale_shiftstates
= mblen ((char *)NULL
, 0);
115 # if defined (LC_COLLATE)
116 val
= get_string_value ("LC_COLLATE");
117 if (val
== 0 && lc_all
&& *lc_all
)
118 setlocale (LC_COLLATE
, lc_all
);
119 # endif /* LC_COLLATE */
121 # if defined (LC_MESSAGES)
122 val
= get_string_value ("LC_MESSAGES");
123 if (val
== 0 && lc_all
&& *lc_all
)
124 setlocale (LC_MESSAGES
, lc_all
);
125 # endif /* LC_MESSAGES */
127 # if defined (LC_NUMERIC)
128 val
= get_string_value ("LC_NUMERIC");
129 if (val
== 0 && lc_all
&& *lc_all
)
130 setlocale (LC_NUMERIC
, lc_all
);
131 # endif /* LC_NUMERIC */
133 # if defined (LC_TIME)
134 val
= get_string_value ("LC_TIME");
135 if (val
== 0 && lc_all
&& *lc_all
)
136 setlocale (LC_TIME
, lc_all
);
137 # endif /* LC_TIME */
139 #endif /* HAVE_SETLOCALE */
141 val
= get_string_value ("TEXTDOMAIN");
144 FREE (default_domain
);
145 default_domain
= savestring (val
);
146 if (default_dir
&& *default_dir
)
147 bindtextdomain (default_domain
, default_dir
);
150 val
= get_string_value ("TEXTDOMAINDIR");
154 default_dir
= savestring (val
);
155 if (default_domain
&& *default_domain
)
156 bindtextdomain (default_domain
, default_dir
);
160 /* Set one of the locale categories (specified by VAR) to VALUE. Returns 1
161 if successful, 0 otherwise. */
163 set_locale_var (var
, value
)
171 if (var
[0] == 'T' && var
[10] == 0) /* TEXTDOMAIN */
173 FREE (default_domain
);
174 default_domain
= value
? savestring (value
) : (char *)NULL
;
175 if (default_dir
&& *default_dir
)
176 bindtextdomain (default_domain
, default_dir
);
179 else if (var
[0] == 'T') /* TEXTDOMAINDIR */
182 default_dir
= value
? savestring (value
) : (char *)NULL
;
183 if (default_domain
&& *default_domain
)
184 bindtextdomain (default_domain
, default_dir
);
188 /* var[0] == 'L' && var[1] == 'C' && var[2] == '_' */
190 else if (var
[3] == 'A') /* LC_ALL */
194 lc_all
= savestring (value
);
197 lc_all
= (char *)xmalloc (1);
200 #if defined (HAVE_SETLOCALE)
201 r
= *lc_all
? ((x
= setlocale (LC_ALL
, lc_all
)) != 0) : reset_locale_vars ();
205 internal_warning(_("setlocale: LC_ALL: cannot change locale (%s)"), lc_all
);
207 internal_warning(_("setlocale: LC_ALL: cannot change locale (%s): %s"), lc_all
, strerror (errno
));
210 locale_mb_cur_max
= MB_CUR_MAX
;
211 /* if LC_ALL == "", reset_locale_vars has already called this */
213 locale_utf8locale
= locale_isutf8 (lc_all
);
214 locale_shiftstates
= mblen ((char *)NULL
, 0);
222 #if defined (HAVE_SETLOCALE)
223 else if (var
[3] == 'C' && var
[4] == 'T') /* LC_CTYPE */
225 # if defined (LC_CTYPE)
226 if (lc_all
== 0 || *lc_all
== '\0')
228 x
= setlocale (LC_CTYPE
, get_locale_var ("LC_CTYPE"));
230 locale_mb_cur_max
= MB_CUR_MAX
;
231 /* if setlocale() returns NULL, the locale is not changed */
233 locale_utf8locale
= locale_isutf8 (x
);
234 locale_shiftstates
= mblen ((char *)NULL
, 0);
239 else if (var
[3] == 'C' && var
[4] == 'O') /* LC_COLLATE */
241 # if defined (LC_COLLATE)
242 if (lc_all
== 0 || *lc_all
== '\0')
243 x
= setlocale (LC_COLLATE
, get_locale_var ("LC_COLLATE"));
244 # endif /* LC_COLLATE */
246 else if (var
[3] == 'M' && var
[4] == 'E') /* LC_MESSAGES */
248 # if defined (LC_MESSAGES)
249 if (lc_all
== 0 || *lc_all
== '\0')
250 x
= setlocale (LC_MESSAGES
, get_locale_var ("LC_MESSAGES"));
251 # endif /* LC_MESSAGES */
253 else if (var
[3] == 'N' && var
[4] == 'U') /* LC_NUMERIC */
255 # if defined (LC_NUMERIC)
256 if (lc_all
== 0 || *lc_all
== '\0')
257 x
= setlocale (LC_NUMERIC
, get_locale_var ("LC_NUMERIC"));
258 # endif /* LC_NUMERIC */
260 else if (var
[3] == 'T' && var
[4] == 'I') /* LC_TIME */
262 # if defined (LC_TIME)
263 if (lc_all
== 0 || *lc_all
== '\0')
264 x
= setlocale (LC_TIME
, get_locale_var ("LC_TIME"));
265 # endif /* LC_TIME */
267 #endif /* HAVE_SETLOCALE */
272 internal_warning(_("setlocale: %s: cannot change locale (%s)"), var
, get_locale_var (var
));
274 internal_warning(_("setlocale: %s: cannot change locale (%s): %s"), var
, get_locale_var (var
), strerror (errno
));
280 /* Called when LANG is assigned a value. Tracks value in `lang'. Calls
281 reset_locale_vars() to reset any default values if LC_ALL is unset or
284 set_lang (var
, value
)
289 lang
= savestring (value
);
292 lang
= (char *)xmalloc (1);
296 return ((lc_all
== 0 || *lc_all
== 0) ? reset_locale_vars () : 0);
299 /* Set default values for LANG and LC_ALL. Default values for all other
300 locale-related variables depend on these. */
306 v
= get_string_value ("LC_ALL");
307 set_locale_var ("LC_ALL", v
);
309 v
= get_string_value ("LANG");
310 set_lang ("LANG", v
);
313 /* Get the value of one of the locale variables (LC_MESSAGES, LC_CTYPE).
314 The precedence is as POSIX.2 specifies: LC_ALL has precedence over
315 the specific locale variables, and LANG, if set, is used as the default. */
324 if (locale
== 0 || *locale
== 0)
325 locale
= get_string_value (var
); /* XXX - no mem leak */
326 if (locale
== 0 || *locale
== 0)
328 if (locale
== 0 || *locale
== 0)
330 locale
= default_locale
; /* system-dependent; not really portable. should it be "C"? */
337 /* Called to reset all of the locale variables to their appropriate values
338 if (and only if) LC_ALL has not been assigned a value. DO NOT CALL THIS
339 IF LC_ALL HAS BEEN ASSIGNED A VALUE. */
344 #if defined (HAVE_SETLOCALE)
345 if (lang
== 0 || *lang
== '\0')
346 maybe_make_export_env (); /* trust that this will change environment for setlocale */
347 if (setlocale (LC_ALL
, lang
? lang
: "") == 0)
351 # if defined (LC_CTYPE)
352 x
= setlocale (LC_CTYPE
, get_locale_var ("LC_CTYPE"));
354 # if defined (LC_COLLATE)
355 t
= setlocale (LC_COLLATE
, get_locale_var ("LC_COLLATE"));
357 # if defined (LC_MESSAGES)
358 t
= setlocale (LC_MESSAGES
, get_locale_var ("LC_MESSAGES"));
360 # if defined (LC_NUMERIC)
361 t
= setlocale (LC_NUMERIC
, get_locale_var ("LC_NUMERIC"));
363 # if defined (LC_TIME)
364 t
= setlocale (LC_TIME
, get_locale_var ("LC_TIME"));
368 locale_mb_cur_max
= MB_CUR_MAX
;
370 locale_utf8locale
= locale_isutf8 (x
);
371 locale_shiftstates
= mblen ((char *)NULL
, 0);
377 /* Translate the contents of STRING, a $"..." quoted string, according
378 to the current locale. In the `C' or `POSIX' locale, or if gettext()
379 is not available, the passed string is returned unchanged. The
380 length of the translated string is returned in LENP, if non-null. */
382 localetrans (string
, len
, lenp
)
390 /* Don't try to translate null strings. */
391 if (string
== 0 || *string
== 0)
395 return ((char *)NULL
);
398 locale
= get_locale_var ("LC_MESSAGES");
400 /* If we don't have setlocale() or the current locale is `C' or `POSIX',
401 just return the string. If we don't have gettext(), there's no use
402 doing anything else. */
403 if (locale
== 0 || locale
[0] == '\0' ||
404 (locale
[0] == 'C' && locale
[1] == '\0') || STREQ (locale
, "POSIX"))
406 t
= (char *)xmalloc (len
+ 1);
413 /* Now try to translate it. */
414 if (default_domain
&& *default_domain
)
415 translated
= dgettext (default_domain
, string
);
419 if (translated
== string
) /* gettext returns its argument if untranslatable */
421 t
= (char *)xmalloc (len
+ 1);
428 tlen
= strlen (translated
);
429 t
= (char *)xmalloc (tlen
+ 1);
430 strcpy (t
, translated
);
437 /* Change a bash string into a string suitable for inclusion in a `po' file.
438 This backslash-escapes `"' and `\' and changes newlines into \\\n"\n". */
440 mk_msgstr (string
, foundnlp
)
445 char *result
, *r
, *s
;
447 for (len
= 0, s
= string
; s
&& *s
; s
++)
450 if (*s
== '"' || *s
== '\\')
456 r
= result
= (char *)xmalloc (len
+ 3);
459 for (s
= string
; s
&& (c
= *s
); s
++)
461 if (c
== '\n') /* <NL> -> \n"<NL>" */
472 if (c
== '"' || c
== '\\')
483 /* $"..." -- Translate the portion of STRING between START and END
484 according to current locale using gettext (if available) and return
485 the result. The caller will take care of leaving the quotes intact.
486 The string will be left without the leading `$' by the caller.
487 If translation is performed, the translated string will be double-quoted
488 by the caller. The length of the translated string is returned in LENP,
491 localeexpand (string
, start
, end
, lineno
, lenp
)
493 int start
, end
, lineno
, *lenp
;
495 int len
, tlen
, foundnl
;
498 temp
= (char *)xmalloc (end
- start
+ 1);
499 for (tlen
= 0, len
= start
; len
< end
; )
500 temp
[tlen
++] = string
[len
++];
503 /* If we're just dumping translatable strings, don't do anything with the
504 string itself, but if we're dumping in `po' file format, convert it into
505 a form more palatable to gettext(3) and friends by quoting `"' and `\'
506 with backslashes and converting <NL> into `\n"<NL>"'. If we find a
507 newline in TEMP, we first output a `msgid ""' line and then the
508 translated string; otherwise we output the `msgid' and translated
509 string all on one line. */
510 if (dump_translatable_strings
)
515 t
= mk_msgstr (temp
, &foundnl
);
516 t2
= foundnl
? "\"\"\n" : "";
518 printf ("#: %s:%d\nmsgid %s%s\nmsgstr \"\"\n",
519 yy_input_name (), lineno
, t2
, t
);
523 printf ("\"%s\"\n", temp
);
531 t
= localetrans (temp
, tlen
, &len
);
545 /* Set every character in the <blank> character class to be a shell break
546 character for the lexical analyzer when the locale changes. */
552 for (x
= 0; x
< sh_syntabsiz
; x
++)
554 if (isblank ((unsigned char)x
))
555 sh_syntaxtab
[x
] |= CSHBRK
|CBLANK
;
556 else if (member (x
, shell_break_chars
))
558 sh_syntaxtab
[x
] |= CSHBRK
;
559 sh_syntaxtab
[x
] &= ~CBLANK
;
562 sh_syntaxtab
[x
] &= ~(CSHBRK
|CBLANK
);
566 /* Parse a locale specification
567 language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
568 and return TRUE if the codeset is UTF-8 or utf8 */
570 locale_isutf8 (lspec
)
575 #if HAVE_LANGINFO_CODESET
576 cp
= nl_langinfo (CODESET
);
577 return (STREQ (cp
, "UTF-8") || STREQ (cp
, "utf8"));
578 #elif HAVE_LOCALE_CHARSET
579 cp
= locale_charset ();
580 return (STREQ (cp
, "UTF-8") || STREQ (cp
, "utf8"));
583 for (cp
= lspec
; *cp
&& *cp
!= '@' && *cp
!= '+' && *cp
!= ','; cp
++)
587 for (encoding
= ++cp
; *cp
&& *cp
!= '@' && *cp
!= '+' && *cp
!= ','; cp
++)
589 /* The encoding (codeset) is the substring between encoding and cp */
590 if ((cp
- encoding
== 5 && STREQN (encoding
, "UTF-8", 5)) ||
591 (cp
- encoding
== 4 && STREQN (encoding
, "utf8", 4)))
601 #if defined (HAVE_LOCALECONV)
608 return (lv
&& lv
->decimal_point
&& lv
->decimal_point
[0]) ? lv
->decimal_point
[0] : '.';
611 # undef locale_decpoint