diff --git a/include/locale.h b/include/locale.h index 86eda17d34d7..0c1a6bcc35b0 100644 --- a/include/locale.h +++ b/include/locale.h @@ -1,80 +1,81 @@ /* * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)locale.h 8.1 (Berkeley) 6/2/93 */ #ifndef _LOCALE_H_ #define _LOCALE_H_ struct lconv { char *decimal_point; char *thousands_sep; char *grouping; char *int_curr_symbol; char *currency_symbol; char *mon_decimal_point; char *mon_thousands_sep; char *mon_grouping; char *positive_sign; char *negative_sign; char int_frac_digits; char frac_digits; char p_cs_precedes; char p_sep_by_space; char n_cs_precedes; char n_sep_by_space; char p_sign_posn; char n_sign_posn; }; #ifndef NULL #define NULL 0 #endif #define LC_ALL 0 #define LC_COLLATE 1 #define LC_CTYPE 2 #define LC_MONETARY 3 #define LC_NUMERIC 4 #define LC_TIME 5 +#define LC_MESSAGES 6 -#define _LC_LAST 6 /* marks end */ +#define _LC_LAST 7 /* marks end */ #include __BEGIN_DECLS struct lconv *localeconv __P((void)); char *setlocale __P((int, const char *)); __END_DECLS #endif /* _LOCALE_H_ */ diff --git a/lib/libc/locale/setlocale.3 b/lib/libc/locale/setlocale.3 index 18db81120cd9..7494f2d06b96 100644 --- a/lib/libc/locale/setlocale.3 +++ b/lib/libc/locale/setlocale.3 @@ -1,330 +1,332 @@ .\" Copyright (c) 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" Donn Seeley at BSDI. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)setlocale.3 8.1 (Berkeley) 6/9/93 .\" .Dd June 9, 1993 .Dt SETLOCALE 3 .Os .Sh NAME .Nm setlocale , .Nm localeconv .Nd natural language formatting for C .Sh SYNOPSIS .Fd #include .Ft char * .Fn setlocale "int category" "const char *locale" .Ft struct lconv * .Fn localeconv "void" .Sh DESCRIPTION The .Fn setlocale function sets the C library's notion of natural language formatting style for particular sets of routines. Each such style is called a .Sq locale and is invoked using an appropriate name passed as a C string. The .Fn localeconv routine returns the current locale's parameters for formatting numbers. .Pp The .Fn setlocale function recognizes several categories of routines. These are the categories and the sets of routines they select: .Pp .Bl -tag -width LC_MONETARY .It Dv LC_ALL Set the entire locale generically. .It Dv LC_COLLATE Set a locale for string collation routines. This controls alphabetic ordering in .Fn strcoll and .Fn strxfrm . .It Dv LC_CTYPE Set a locale for the .Xr ctype 3 , .Xr mbrune 3 , .Xr multibyte 3 and .Xr rune 3 functions. This controls recognition of upper and lower case, alphabetic or non-alphabetic characters, and so on. The real work is done by the .Fn setrunelocale function. +.It Dv LC_MESSAGES +Set a locale for message catalogs. .It Dv LC_MONETARY Set a locale for formatting monetary values; this affects the .Fn localeconv function. .It Dv LC_NUMERIC Set a locale for formatting numbers. This controls the formatting of decimal points in input and output of floating point numbers in functions such as .Fn printf and .Fn scanf , as well as values returned by .Fn localeconv . .It Dv LC_TIME Set a locale for formatting dates and times using the .Fn strftime function. .El .Pp Only three locales are defined by default, the empty string .Li "\&""\|"" which denotes the native environment, and the .Li "\&""C"" and .LI "\&""POSIX"" locales, which denote the C language environment. A .Fa locale argument of .Dv NULL causes .Fn setlocale to return the current locale. By default, C programs start in the .Li "\&""C"" locale. The only function in the library that sets the locale is .Fn setlocale ; the locale is never changed as a side effect of some other routine. .Pp The .Fn localeconv function returns a pointer to a structure which provides parameters for formatting numbers, especially currency values: .Bd -literal -offset indent struct lconv { char *decimal_point; char *thousands_sep; char *grouping; char *int_curr_symbol; char *currency_symbol; char *mon_decimal_point; char *mon_thousands_sep; char *mon_grouping; char *positive_sign; char *negative_sign; char int_frac_digits; char frac_digits; char p_cs_precedes; char p_sep_by_space; char n_cs_precedes; char n_sep_by_space; char p_sign_posn; char n_sign_posn; }; .Ed .Pp The individual fields have the following meanings: .Pp .Bl -tag -width mon_decimal_point .It Fa decimal_point The decimal point character, except for currency values. .It Fa thousands_sep The separator between groups of digits before the decimal point, except for currency values. .It Fa grouping The sizes of the groups of digits, except for currency values. This is a pointer to a vector of integers, each of size .Va char , representing group size from low order digit groups to high order (right to left). The list may be terminated with 0 or .Dv CHAR_MAX . If the list is terminated with 0, the last group size before the 0 is repeated to account for all the digits. If the list is terminated with .Dv CHAR_MAX , no more grouping is performed. .It Fa int_curr_symbol The standardized international currency symbol. .It Fa currency_symbol The local currency symbol. .It Fa mon_decimal_point The decimal point character for currency values. .It Fa mon_thousands_sep The separator for digit groups in currency values. .It Fa mon_grouping Like .Fa grouping but for currency values. .It Fa positive_sign The character used to denote nonnegative currency values, usually the empty string. .It Fa negative_sign The character used to denote negative currency values, usually a minus sign. .It Fa int_frac_digits The number of digits after the decimal point in an international-style currency value. .It Fa frac_digits The number of digits after the decimal point in the local style for currency values. .It Fa p_cs_precedes 1 if the currency symbol precedes the currency value for nonnegative values, 0 if it follows. .It Fa p_sep_by_space 1 if a space is inserted between the currency symbol and the currency value for nonnegative values, 0 otherwise. .It Fa n_cs_precedes Like .Fa p_cs_precedes but for negative values. .It Fa n_sep_by_space Like .Fa p_sep_by_space but for negative values. .It Fa p_sign_posn The location of the .Fa positive_sign with respect to a nonnegative quantity and the .Fa currency_symbol , coded as follows: .Bl -tag -width 3n -compact .It Li 0 Parentheses around the entire string. .It Li 1 Before the string. .It Li 2 After the string. .It Li 3 Just before .Fa currency_symbol . .It Li 4 Just after .Fa currency_symbol . .El .It Fa n_sign_posn Like .Fa p_sign_posn but for negative currency values. .El .Pp Unless mentioned above, an empty string as a value for a field indicates a zero length result or a value that is not in the current locale. A .Dv CHAR_MAX result similarly denotes an unavailable value. .Sh "RETURN VALUES The .Fn setlocale function returns .Dv NULL and fails to change the locale if the given combination of .Fa category and .Fa locale makes no sense. The .Fn localeconv function returns a pointer to a static object which may be altered by later calls to .Fn setlocale or .Fn localeconv . .Sh FILES .Bl -tag -width /usr/share/locale/locale/category -compact .It Pa $PATH_LOCALE/ Ns Em locale/category .It Pa /usr/share/locale/ Ns Em locale/category locale file for the locale .Em locale and the category .Em category . .El .Sh "SEE ALSO .Xr colldef 1 , .Xr mklocale 1 , .Xr ctype 3 , .Xr mbrune 3 , .Xr multibyte 3 , .Xr rune 3 , .Xr strcoll 3 , .Xr strxfrm 3 , .Xr euc 4 , .Xr utf2 4 .Sh STANDARDS The .Fn setlocale and .Fn localeconv functions conform to .St -ansiC . .Sh HISTORY The .Fn setlocale and .Fn localeconv functions first appeared in .Bx 4.4 . .Sh BUGS The current implementation supports only the .Li "\&""C"" and .Li "\&""POSIX"" locales for all but the .Dv LC_COLLATE , .Dv LC_CTYPE , and .Dv LC_TIME categories. .Pp In spite of the gnarly currency support in .Fn localeconv , the standards don't include any functions for generalized currency formatting. .Pp Use of .Dv LC_MONETARY could lead to misleading results until we have a real time currency conversion function. .Dv LC_NUMERIC and .Dv LC_TIME are personal choices and should not be wrapped up with the other categories. diff --git a/lib/libc/locale/setlocale.c b/lib/libc/locale/setlocale.c index 4b86a813a2dc..ac8729dcbb33 100644 --- a/lib/libc/locale/setlocale.c +++ b/lib/libc/locale/setlocale.c @@ -1,320 +1,319 @@ /* * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Paul Borman at Krystal Technologies. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: setlocale.c,v 1.21 1997/04/07 08:54:35 ache Exp $ + * $Id: setlocale.c,v 1.22 1998/01/15 09:58:07 jb Exp $ */ #ifdef LIBC_RCS static const char rcsid[] = - "$Id: setlocale.c,v 1.21 1997/04/07 08:54:35 ache Exp $"; + "$Id: setlocale.c,v 1.22 1998/01/15 09:58:07 jb Exp $"; #endif #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93"; #endif /* LIBC_SCCS and not lint */ #include #include #include #include #include #include #include #include #include "collate.h" #include "setlocale.h" /* * Category names for getenv() */ static char *categories[_LC_LAST] = { "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", + "LC_MESSAGES", }; /* * Current locales for each category */ static char current_categories[_LC_LAST][ENCODING_LEN + 1] = { "C", "C", "C", "C", "C", "C", + "C", }; /* * The locales we are going to try and load */ static char new_categories[_LC_LAST][ENCODING_LEN + 1]; static char saved_categories[_LC_LAST][ENCODING_LEN + 1]; static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)]; static char *currentlocale __P((void)); static char *loadlocale __P((int)); static int stub_load_locale __P((const char *)); extern int __time_load_locale __P((const char *)); /* strftime.c */ #ifdef XPG4 extern int _xpg4_setrunelocale __P((char *)); #endif char * setlocale(category, locale) int category; const char *locale; { int i, j, len; char *env, *r; if (category < LC_ALL || category >= _LC_LAST) return (NULL); if (!locale) return (category != LC_ALL ? current_categories[category] : currentlocale()); /* * Default to the current locale for everything. */ for (i = 1; i < _LC_LAST; ++i) (void)strcpy(new_categories[i], current_categories[i]); /* * Now go fill up new_categories from the locale argument */ if (!*locale) { env = getenv(categories[category]); if (category != LC_ALL && (!env || !*env)) env = getenv(categories[LC_ALL]); if (!env || !*env) env = getenv("LANG"); if (!env || !*env) env = "C"; (void) strncpy(new_categories[category], env, ENCODING_LEN); new_categories[category][ENCODING_LEN] = '\0'; if (category == LC_ALL) { for (i = 1; i < _LC_LAST; ++i) { if (!(env = getenv(categories[i])) || !*env) env = new_categories[LC_ALL]; (void)strncpy(new_categories[i], env, ENCODING_LEN); new_categories[i][ENCODING_LEN] = '\0'; } } } else if (category != LC_ALL) { (void)strncpy(new_categories[category], locale, ENCODING_LEN); new_categories[category][ENCODING_LEN] = '\0'; } else { if ((r = strchr(locale, '/')) == NULL) { for (i = 1; i < _LC_LAST; ++i) { (void)strncpy(new_categories[i], locale, ENCODING_LEN); new_categories[i][ENCODING_LEN] = '\0'; } } else { for (i = 1; r[1] == '/'; ++r); if (!r[1]) return (NULL); /* Hmm, just slashes... */ do { len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale; (void)strncpy(new_categories[i], locale, len); new_categories[i][len] = '\0'; i++; locale = r; while (*locale == '/') ++locale; while (*++r && *r != '/'); } while (*locale); while (i < _LC_LAST) (void)strcpy(new_categories[i], new_categories[i-1]); } } if (category) return (loadlocale(category)); for (i = 1; i < _LC_LAST; ++i) { (void)strcpy(saved_categories[i], current_categories[i]); if (loadlocale(i) == NULL) { for (j = 1; j < i; j++) { (void)strcpy(new_categories[j], saved_categories[j]); /* XXX can fail too */ (void)loadlocale(j); } return (NULL); } } return (currentlocale()); } static char * currentlocale() { int i; (void)strcpy(current_locale_string, current_categories[1]); for (i = 2; i < _LC_LAST; ++i) if (strcmp(current_categories[1], current_categories[i])) { - (void) strcpy(current_locale_string, current_categories[1]); - (void) strcat(current_locale_string, "/"); - (void) strcat(current_locale_string, current_categories[2]); - (void) strcat(current_locale_string, "/"); - (void) strcat(current_locale_string, current_categories[3]); - (void) strcat(current_locale_string, "/"); - (void) strcat(current_locale_string, current_categories[4]); - (void) strcat(current_locale_string, "/"); - (void) strcat(current_locale_string, current_categories[5]); + for (i = 2; i < _LC_LAST; ++i) { + (void) strcat(current_locale_string, "/"); + (void) strcat(current_locale_string, current_categories[i]); + } break; } return (current_locale_string); } static char * loadlocale(category) int category; { char *ret; char *new = new_categories[category]; char *old = current_categories[category]; if (_PathLocale == NULL) { char *p = getenv("PATH_LOCALE"); if (p != NULL #ifndef __NETBSD_SYSCALLS && !issetugid() #endif ) { if (strlen(p) + 1/*"/"*/ + ENCODING_LEN + 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) return (NULL); _PathLocale = strdup(p); if (_PathLocale == NULL) return (NULL); } else _PathLocale = _PATH_LOCALE; } if (strcmp(new, old) == 0) return (old); if (category == LC_CTYPE) { #ifdef XPG4 ret = _xpg4_setrunelocale(new) ? NULL : new; #else ret = setrunelocale(new) ? NULL : new; #endif if (!ret) { #ifdef XPG4 (void)_xpg4_setrunelocale(old); #else (void)setrunelocale(old); #endif } else (void)strcpy(old, new); return (ret); } if (category == LC_COLLATE) { ret = (__collate_load_tables(new) < 0) ? NULL : new; if (!ret) (void)__collate_load_tables(old); else (void)strcpy(old, new); return (ret); } if (category == LC_TIME) { ret = (__time_load_locale(new) < 0) ? NULL : new; if (!ret) (void)__time_load_locale(old); else (void)strcpy(old, new); return (ret); } - if (category == LC_MONETARY || category == LC_NUMERIC) { + if (category == LC_MONETARY || + category == LC_MESSAGES || + category == LC_NUMERIC) { ret = stub_load_locale(new) ? NULL : new; if (!ret) (void)stub_load_locale(old); else (void)strcpy(old, new); return (ret); } /* Just in case...*/ return (NULL); } static int stub_load_locale(encoding) const char *encoding; { char name[PATH_MAX]; struct stat st; if (!encoding) return(1); /* * The "C" and "POSIX" locale are always here. */ if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX")) return(0); if (!_PathLocale) return(1); /* Range checking not needed, encoding has fixed size */ strcpy(name, _PathLocale); strcat(name, "/"); strcat(name, encoding); #if 0 /* * Some day we will actually look at this file. */ #endif return (stat(name, &st) != 0 || !S_ISDIR(st.st_mode)); }