i-bash/locale.c

480 lines
12 KiB
C
Raw Normal View History

1996-12-23 17:02:34 +00:00
/* locale.c - Miscellaneous internationalization functions. */
2004-07-27 13:29:18 +00:00
/* Copyright (C) 1996-2004 Free Software Foundation, Inc.
1996-12-23 17:02:34 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
2000-03-17 21:46:59 +00:00
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
1996-12-23 17:02:34 +00:00
#include "config.h"
#include "bashtypes.h"
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "bashintl.h"
#include "bashansi.h"
#include <stdio.h>
2001-11-13 17:56:06 +00:00
#include "chartypes.h"
1996-12-23 17:02:34 +00:00
#include "shell.h"
2002-07-17 14:10:11 +00:00
#include "input.h" /* For bash_input */
extern int dump_translatable_strings, dump_po_strings;
1996-12-23 17:02:34 +00:00
/* The current locale when the program begins */
static char *default_locale;
/* The current domain for textdomain(3). */
static char *default_domain;
static char *default_dir;
/* tracks the value of LC_ALL; used to override values for other locale
categories */
static char *lc_all;
2004-07-27 13:29:18 +00:00
/* tracks the value of LC_ALL; used to provide defaults for locale
categories */
static char *lang;
/* Called to reset all of the locale variables to their appropriate values
if (and only if) LC_ALL has not been assigned a value. */
static int reset_locale_vars __P((void));
static void locale_setblanks __P((void));
1996-12-23 17:02:34 +00:00
/* Set the value of default_locale and make the current locale the
system default locale. This should be called very early in main(). */
void
set_default_locale ()
{
#if defined (HAVE_SETLOCALE)
default_locale = setlocale (LC_ALL, "");
if (default_locale)
default_locale = savestring (default_locale);
#endif /* HAVE_SETLOCALE */
2004-07-27 13:29:18 +00:00
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
1996-12-23 17:02:34 +00:00
}
2004-07-27 13:29:18 +00:00
/* Set default values for LC_CTYPE, LC_COLLATE, LC_MESSAGES and LC_NUMERIC
if they are not specified in the environment, but LC_ALL is. This
1996-12-23 17:02:34 +00:00
should be called from main() after parsing the environment. */
void
set_default_locale_vars ()
{
char *val;
2004-07-27 13:29:18 +00:00
int r;
1996-12-23 17:02:34 +00:00
#if defined (HAVE_SETLOCALE)
2004-07-27 13:29:18 +00:00
# if defined (LC_CTYPE)
1996-12-23 17:02:34 +00:00
val = get_string_value ("LC_CTYPE");
if (val == 0 && lc_all && *lc_all)
2004-07-27 13:29:18 +00:00
{
setlocale (LC_CTYPE, lc_all);
locale_setblanks ();
}
# endif
1996-12-23 17:02:34 +00:00
# if defined (LC_COLLATE)
val = get_string_value ("LC_COLLATE");
if (val == 0 && lc_all && *lc_all)
setlocale (LC_COLLATE, lc_all);
# endif /* LC_COLLATE */
# if defined (LC_MESSAGES)
val = get_string_value ("LC_MESSAGES");
if (val == 0 && lc_all && *lc_all)
setlocale (LC_MESSAGES, lc_all);
# endif /* LC_MESSAGES */
2000-03-17 21:46:59 +00:00
# if defined (LC_NUMERIC)
val = get_string_value ("LC_NUMERIC");
if (val == 0 && lc_all && *lc_all)
setlocale (LC_NUMERIC, lc_all);
# endif /* LC_NUMERIC */
1996-12-23 17:02:34 +00:00
#endif /* HAVE_SETLOCALE */
val = get_string_value ("TEXTDOMAIN");
if (val && *val)
{
FREE (default_domain);
default_domain = savestring (val);
2004-07-27 13:29:18 +00:00
#if 0
/* Don't want to override the shell's textdomain as the default */
1996-12-23 17:02:34 +00:00
textdomain (default_domain);
2004-07-27 13:29:18 +00:00
#endif
1996-12-23 17:02:34 +00:00
}
val = get_string_value ("TEXTDOMAINDIR");
if (val && *val)
{
FREE (default_dir);
default_dir = savestring (val);
2004-07-27 13:29:18 +00:00
if (default_domain && *default_domain)
bindtextdomain (default_domain, default_dir);
1996-12-23 17:02:34 +00:00
}
}
/* Set one of the locale categories (specified by VAR) to VALUE. Returns 1
if successful, 0 otherwise. */
int
set_locale_var (var, value)
char *var, *value;
{
2004-07-27 13:29:18 +00:00
int r;
1996-12-23 17:02:34 +00:00
if (var[0] == 'T' && var[10] == 0) /* TEXTDOMAIN */
{
FREE (default_domain);
default_domain = value ? savestring (value) : (char *)NULL;
2004-07-27 13:29:18 +00:00
#if 0
/* Don't want to override the shell's textdomain as the default */
1996-12-23 17:02:34 +00:00
textdomain (default_domain);
2004-07-27 13:29:18 +00:00
#endif
1996-12-23 17:02:34 +00:00
return (1);
}
else if (var[0] == 'T') /* TEXTDOMAINDIR */
{
FREE (default_dir);
default_dir = value ? savestring (value) : (char *)NULL;
2004-07-27 13:29:18 +00:00
if (default_domain && *default_domain)
bindtextdomain (default_domain, default_dir);
1996-12-23 17:02:34 +00:00
return (1);
}
/* var[0] == 'L' && var[1] == 'C' && var[2] == '_' */
else if (var[3] == 'A') /* LC_ALL */
{
FREE (lc_all);
1997-09-22 20:22:27 +00:00
if (value)
lc_all = savestring (value);
else
{
2001-11-13 17:56:06 +00:00
lc_all = (char *)xmalloc (1);
1997-09-22 20:22:27 +00:00
lc_all[0] = '\0';
}
1996-12-23 17:02:34 +00:00
#if defined (HAVE_SETLOCALE)
2004-07-27 13:29:18 +00:00
r = *lc_all ? (setlocale (LC_ALL, lc_all) != 0) : reset_locale_vars ();
locale_setblanks ();
return r;
1996-12-23 17:02:34 +00:00
#else
return (1);
#endif
}
#if defined (HAVE_SETLOCALE)
else if (var[3] == 'C' && var[4] == 'T') /* LC_CTYPE */
{
2004-07-27 13:29:18 +00:00
# if defined (LC_CTYPE)
1996-12-23 17:02:34 +00:00
if (lc_all == 0 || *lc_all == '\0')
2004-07-27 13:29:18 +00:00
{
r = (setlocale (LC_CTYPE, get_locale_var ("LC_CTYPE")) != 0);
locale_setblanks ();
return r;
}
# endif
1996-12-23 17:02:34 +00:00
}
else if (var[3] == 'C' && var[4] == 'O') /* LC_COLLATE */
{
# if defined (LC_COLLATE)
if (lc_all == 0 || *lc_all == '\0')
2004-07-27 13:29:18 +00:00
return (setlocale (LC_COLLATE, get_locale_var ("LC_COLLATE")) != 0);
1996-12-23 17:02:34 +00:00
# endif /* LC_COLLATE */
}
else if (var[3] == 'M' && var[4] == 'E') /* LC_MESSAGES */
{
# if defined (LC_MESSAGES)
if (lc_all == 0 || *lc_all == '\0')
2004-07-27 13:29:18 +00:00
return (setlocale (LC_MESSAGES, get_locale_var ("LC_MESSAGES")) != 0);
1996-12-23 17:02:34 +00:00
# endif /* LC_MESSAGES */
}
2001-11-13 17:56:06 +00:00
else if (var[3] == 'N' && var[4] == 'U') /* LC_NUMERIC */
2000-03-17 21:46:59 +00:00
{
# if defined (LC_NUMERIC)
if (lc_all == 0 || *lc_all == '\0')
2004-07-27 13:29:18 +00:00
return (setlocale (LC_NUMERIC, get_locale_var ("LC_NUMERIC")) != 0);
2000-03-17 21:46:59 +00:00
# endif /* LC_NUMERIC */
}
1996-12-23 17:02:34 +00:00
#endif /* HAVE_SETLOCALE */
return (0);
}
2004-07-27 13:29:18 +00:00
/* Called when LANG is assigned a value. Tracks value in `lang'. Calls
reset_locale_vars() to reset any default values if LC_ALL is unset or
null. */
1996-12-23 17:02:34 +00:00
int
set_lang (var, value)
char *var, *value;
{
2004-07-27 13:29:18 +00:00
FREE (lang);
if (value)
lang = savestring (value);
else
{
lang = (char *)xmalloc (1);
lang[0] = '\0';
}
return ((lc_all == 0 || *lc_all == 0) ? reset_locale_vars () : 0);
1996-12-23 17:02:34 +00:00
}
2004-07-27 13:29:18 +00:00
/* Get the value of one of the locale variables (LC_MESSAGES, LC_CTYPE).
The precedence is as POSIX.2 specifies: LC_ALL has precedence over
the specific locale variables, and LANG, if set, is used as the default. */
1996-12-23 17:02:34 +00:00
char *
get_locale_var (var)
char *var;
{
char *locale;
locale = lc_all;
2004-07-27 13:29:18 +00:00
if (locale == 0 || *locale == 0)
1996-12-23 17:02:34 +00:00
locale = get_string_value (var);
2004-07-27 13:29:18 +00:00
if (locale == 0 || *locale == 0)
locale = lang;
if (locale == 0 || *locale == 0)
locale = default_locale; /* system-dependent; not really portable */
1996-12-23 17:02:34 +00:00
return (locale);
}
2004-07-27 13:29:18 +00:00
/* Called to reset all of the locale variables to their appropriate values
if (and only if) LC_ALL has not been assigned a value. DO NOT CALL THIS
IF LC_ALL HAS BEEN ASSIGNED A VALUE. */
static int
reset_locale_vars ()
{
#if defined (HAVE_SETLOCALE)
char *locale;
locale = lang;
if (locale == 0 || *locale == '\0')
locale = default_locale;
if (setlocale (LC_ALL, locale) == 0)
return 0;
# if defined (LC_CTYPE)
setlocale (LC_CTYPE, get_locale_var ("LC_CTYPE"));
# endif
# if defined (LC_COLLATE)
setlocale (LC_COLLATE, get_locale_var ("LC_COLLATE"));
# endif
# if defined (LC_MESSAGES)
setlocale (LC_MESSAGES, get_locale_var ("LC_MESSAGES"));
# endif
# if defined (LC_NUMERIC)
setlocale (LC_NUMERIC, get_locale_var ("LC_NUMERIC"));
# endif
locale_setblanks ();
#endif
return 1;
}
1996-12-23 17:02:34 +00:00
/* Translate the contents of STRING, a $"..." quoted string, according
to the current locale. In the `C' or `POSIX' locale, or if gettext()
is not available, the passed string is returned unchanged. The
length of the translated string is returned in LENP, if non-null. */
char *
localetrans (string, len, lenp)
char *string;
int len, *lenp;
{
char *locale, *t;
char *translated;
int tlen;
/* Don't try to translate null strings. */
if (string == 0 || *string == 0)
{
if (lenp)
2001-04-06 19:14:31 +00:00
*lenp = 0;
1996-12-23 17:02:34 +00:00
return ((char *)NULL);
}
locale = get_locale_var ("LC_MESSAGES");
/* If we don't have setlocale() or the current locale is `C' or `POSIX',
just return the string. If we don't have gettext(), there's no use
doing anything else. */
if (locale == 0 || locale[0] == '\0' ||
(locale[0] == 'C' && locale[1] == '\0') || STREQ (locale, "POSIX"))
{
2001-11-13 17:56:06 +00:00
t = (char *)xmalloc (len + 1);
1996-12-23 17:02:34 +00:00
strcpy (t, string);
if (lenp)
*lenp = len;
return (t);
}
/* Now try to translate it. */
2004-07-27 13:29:18 +00:00
if (default_domain && *default_domain)
translated = dgettext (default_domain, string);
else
translated = string;
1996-12-23 17:02:34 +00:00
if (translated == string) /* gettext returns its argument if untranslatable */
{
2001-11-13 17:56:06 +00:00
t = (char *)xmalloc (len + 1);
1996-12-23 17:02:34 +00:00
strcpy (t, string);
if (lenp)
*lenp = len;
}
else
{
tlen = strlen (translated);
2001-11-13 17:56:06 +00:00
t = (char *)xmalloc (tlen + 1);
1996-12-23 17:02:34 +00:00
strcpy (t, translated);
if (lenp)
*lenp = tlen;
}
return (t);
}
2002-07-17 14:10:11 +00:00
/* Change a bash string into a string suitable for inclusion in a `po' file.
This backslash-escapes `"' and `\' and changes newlines into \\\n"\n". */
char *
mk_msgstr (string, foundnlp)
char *string;
int *foundnlp;
{
register int c, len;
char *result, *r, *s;
for (len = 0, s = string; s && *s; s++)
{
len++;
if (*s == '"' || *s == '\\')
len++;
else if (*s == '\n')
len += 5;
}
r = result = (char *)xmalloc (len + 3);
*r++ = '"';
for (s = string; s && (c = *s); s++)
{
if (c == '\n') /* <NL> -> \n"<NL>" */
{
*r++ = '\\';
*r++ = 'n';
*r++ = '"';
*r++ = '\n';
*r++ = '"';
if (foundnlp)
*foundnlp = 1;
continue;
}
if (c == '"' || c == '\\')
*r++ = '\\';
*r++ = c;
}
*r++ = '"';
*r++ = '\0';
return result;
}
/* $"..." -- Translate the portion of STRING between START and END
according to current locale using gettext (if available) and return
the result. The caller will take care of leaving the quotes intact.
The string will be left without the leading `$' by the caller.
If translation is performed, the translated string will be double-quoted
by the caller. The length of the translated string is returned in LENP,
if non-null. */
char *
localeexpand (string, start, end, lineno, lenp)
char *string;
int start, end, lineno, *lenp;
{
int len, tlen, foundnl;
char *temp, *t, *t2;
temp = (char *)xmalloc (end - start + 1);
for (tlen = 0, len = start; len < end; )
temp[tlen++] = string[len++];
temp[tlen] = '\0';
/* If we're just dumping translatable strings, don't do anything with the
2004-07-27 13:29:18 +00:00
string itself, but if we're dumping in `po' file format, convert it into
a form more palatable to gettext(3) and friends by quoting `"' and `\'
with backslashes and converting <NL> into `\n"<NL>"'. If we find a
newline in TEMP, we first output a `msgid ""' line and then the
translated string; otherwise we output the `msgid' and translated
string all on one line. */
2002-07-17 14:10:11 +00:00
if (dump_translatable_strings)
{
if (dump_po_strings)
{
foundnl = 0;
t = mk_msgstr (temp, &foundnl);
t2 = foundnl ? "\"\"\n" : "";
printf ("#: %s:%d\nmsgid %s%s\nmsgstr \"\"\n",
yy_input_name (), lineno, t2, t);
free (t);
}
else
printf ("\"%s\"\n", temp);
if (lenp)
*lenp = tlen;
return (temp);
}
else if (*temp)
{
t = localetrans (temp, tlen, &len);
free (temp);
if (lenp)
*lenp = len;
return (t);
}
else
{
if (lenp)
*lenp = 0;
return (temp);
}
}
2004-07-27 13:29:18 +00:00
/* Set every character in the <blank> character class to be a shell break
character for the lexical analyzer when the locale changes. */
static void
locale_setblanks ()
{
int x;
for (x = 0; x < sh_syntabsiz; x++)
{
if (isblank (x))
sh_syntaxtab[x] |= CSHBRK;
else if (member (x, shell_break_chars))
sh_syntaxtab[x] |= CSHBRK;
else
sh_syntaxtab[x] &= ~CSHBRK;
}
}