diff options
Diffstat (limited to 'newlib/libc/locale')
-rw-r--r-- | newlib/libc/locale/lctype.c | 112 | ||||
-rw-r--r-- | newlib/libc/locale/lctype.h | 5 | ||||
-rw-r--r-- | newlib/libc/locale/lmessages.c | 87 | ||||
-rw-r--r-- | newlib/libc/locale/lmessages.h | 5 | ||||
-rw-r--r-- | newlib/libc/locale/lmonetary.c | 138 | ||||
-rw-r--r-- | newlib/libc/locale/lmonetary.h | 5 | ||||
-rw-r--r-- | newlib/libc/locale/lnumeric.c | 94 | ||||
-rw-r--r-- | newlib/libc/locale/lnumeric.h | 5 | ||||
-rw-r--r-- | newlib/libc/locale/locale.c | 247 | ||||
-rw-r--r-- | newlib/libc/locale/nl_langinfo.c | 6 | ||||
-rw-r--r-- | newlib/libc/locale/setlocale.h | 81 | ||||
-rw-r--r-- | newlib/libc/locale/timelocal.c | 82 | ||||
-rw-r--r-- | newlib/libc/locale/timelocal.h | 5 |
13 files changed, 490 insertions, 382 deletions
diff --git a/newlib/libc/locale/lctype.c b/newlib/libc/locale/lctype.c index 28575dd08..a776ee09f 100644 --- a/newlib/libc/locale/lctype.c +++ b/newlib/libc/locale/lctype.c @@ -21,9 +21,6 @@ * SUCH DAMAGE. */ -#include <limits.h> -#include <string.h> -#include "lctype.h" #include "ldpart.h" #include "setlocale.h" @@ -53,59 +50,78 @@ static char *_ctype_locale_buf; static char _ctype_locale_buf[_CTYPE_BUF_SIZE]; #endif +/* NULL locale indicates global locale (called from setlocale) */ int -__ctype_load_locale(const char *name, void *f_wctomb, const char *charset, - int mb_cur_max) +__ctype_load_locale (struct _thr_locale_t *locale, const char *name, + void *f_wctomb, const char *charset, int mb_cur_max) { - int ret; + int ret; + struct lc_ctype_T ct; + char *bufp = NULL; #ifdef __CYGWIN__ - extern int __set_lc_ctype_from_win (const char *, - const struct lc_ctype_T *, - struct lc_ctype_T *, char **, - void *, const char *, int); - int old_ctype_using_locale = _ctype_using_locale; - _ctype_using_locale = 0; - ret = __set_lc_ctype_from_win (name, &_C_ctype_locale, &_ctype_locale, - &_ctype_locale_buf, f_wctomb, charset, - mb_cur_max); - /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ - if (ret < 0) - _ctype_using_locale = old_ctype_using_locale; - else - { - _ctype_using_locale = ret; - ret = 0; - } + extern int __set_lc_ctype_from_win (const char *, const struct lc_ctype_T *, + struct lc_ctype_T *, char **, void *, + const char *, int); + ret = __set_lc_ctype_from_win (name, &_C_ctype_locale, &ct, &bufp, + f_wctomb, charset, mb_cur_max); + /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ + if (ret >= 0) + { + struct lc_ctype_T *ctp = NULL; + + if (ret > 0) + { + ctp = (struct lc_ctype_T *) calloc (1, sizeof *ctp); + if (!ctp) + return -1; + memcpy (ctp, &ct, sizeof *ctp); + } + locale->ctype = ret == 0 ? NULL : ctp; + if (locale->ctype_buf) + free (locale->ctype_buf); + locale->ctype_buf = bufp; + ret = 0; + } #elif !defined (__HAVE_LOCALE_INFO_EXTENDED__) - if (!strcmp (name, "C")) - _ctype_using_locale = 0; - else - { - _ctype_locale.codeset = strcpy (_ctype_locale_buf, charset); - char *mbc = _ctype_locale_buf + _CTYPE_BUF_SIZE - 2; - mbc[0] = mb_cur_max; - mbc[1] = '\0'; - _ctype_locale.mb_cur_max = mbc; - _ctype_using_locale = 1; - } - ret = 0; + ret = 0; + if (!strcmp (name, "C")) + locale->ctype = NULL; + else + { + if (locale == __get_global_locale ()) + bufp = _ctype_locale_buf; + else + bufp = (char *) malloc (_CTYPE_BUF_SIZE); + if (*bufp) + { + _ctype_locale.codeset = strcpy (bufp, charset); + char *mbc = bufp + _CTYPE_BUF_SIZE - 2; + mbc[0] = mb_cur_max; + mbc[1] = '\0'; + _ctype_locale.mb_cur_max = mbc; + if (locale->ctype_buf && locale->ctype_buf != _ctype_locale_buf) + free (locale->ctype_buf); + locale->ctype_buf = bufp; + } + else + ret = -1; + } #else - ret = __part_load_locale(name, &_ctype_using_locale, - _ctype_locale_buf, "LC_CTYPE", - LCCTYPE_SIZE, LCCTYPE_SIZE, - (const char **)&_ctype_locale); - if (ret == 0 && _ctype_using_locale) - _ctype_locale.grouping = - __fix_locale_grouping_str(_ctype_locale.grouping); + ret = __part_load_locale(name, &_ctype_using_locale, + _ctype_locale_buf, "LC_CTYPE", + LCCTYPE_SIZE, LCCTYPE_SIZE, + (const char **)&_ctype_locale); + if (ret == 0 && _ctype_using_locale) + _ctype_locale.grouping = + __fix_locale_grouping_str(_ctype_locale.grouping); #endif - return ret; + return ret; } struct lc_ctype_T * -__get_current_ctype_locale(void) { - - return (_ctype_using_locale - ? &_ctype_locale - : (struct lc_ctype_T *)&_C_ctype_locale); +__get_current_ctype_locale (void) +{ + struct _thr_locale_t *cur_locale = __get_current_locale (); + return cur_locale->ctype ?: (struct lc_ctype_T *) &_C_ctype_locale; } diff --git a/newlib/libc/locale/lctype.h b/newlib/libc/locale/lctype.h index 663074f54..ead08c567 100644 --- a/newlib/libc/locale/lctype.h +++ b/newlib/libc/locale/lctype.h @@ -39,8 +39,9 @@ struct lc_ctype_T { #endif }; -struct lc_ctype_T *__get_current_ctype_locale(void); -int __ctype_load_locale(const char *, void *, const char *, int); +struct lc_ctype_T *__get_current_ctype_locale (void); +int __ctype_load_locale (struct _thr_locale_t *, const char *, void *, + const char *, int); __END_DECLS diff --git a/newlib/libc/locale/lmessages.c b/newlib/libc/locale/lmessages.c index 9fb1df4a9..7d9096e6c 100644 --- a/newlib/libc/locale/lmessages.c +++ b/newlib/libc/locale/lmessages.c @@ -28,7 +28,7 @@ #include <stddef.h> -#include "lmessages.h" +#include "setlocale.h" #include "ldpart.h" #define LCMESSAGES_SIZE_FULL (sizeof(struct lc_messages_T) / sizeof(char *)) @@ -53,57 +53,68 @@ static const struct lc_messages_T _C_messages_locale = { #endif }; +#ifndef __CYGWIN__ static struct lc_messages_T _messages_locale; static int _messages_using_locale; static char *_messages_locale_buf; +#endif int -__messages_load_locale (const char *name, void *f_wctomb, const char *charset) +__messages_load_locale (struct _thr_locale_t *locale, const char *name, + void *f_wctomb, const char *charset) { + int ret; + struct lc_messages_T me; + char *bufp = NULL; + #ifdef __CYGWIN__ - extern int __set_lc_messages_from_win (const char *, - const struct lc_messages_T *, - struct lc_messages_T *, char **, - void *, const char *); - int ret; + extern int __set_lc_messages_from_win (const char *, + const struct lc_messages_T *, + struct lc_messages_T *, char **, + void *, const char *); - int old_messages_using_locale = _messages_using_locale; - _messages_using_locale = 0; - ret = __set_lc_messages_from_win (name, &_C_messages_locale, - &_messages_locale, - &_messages_locale_buf, - f_wctomb, charset); - /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ - if (ret < 0) - _messages_using_locale = old_messages_using_locale; - else - { - _messages_using_locale = ret; - ret = 0; - } - return ret; + ret = __set_lc_messages_from_win (name, &_C_messages_locale, &me, &bufp, + f_wctomb, charset); + /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ + if (ret >= 0) + { + struct lc_messages_T *mep = NULL; + + if (ret > 0) + { + mep = (struct lc_messages_T *) calloc (1, sizeof *mep); + if (!mep) + return -1; + memcpy (mep, &me, sizeof *mep); + } + locale->messages = ret == 0 ? NULL : mep; + if (locale->messages_buf) + free (locale->messages_buf); + locale->messages_buf = bufp; + ret = 0; + } #else - /* - * Propose that we can have incomplete locale file (w/o "{yes,no}str"). - * Initialize them before loading. In case of complete locale, they'll - * be initialized to loaded value, otherwise they'll not be touched. - */ - _messages_locale.yesstr = empty; - _messages_locale.nostr = empty; + /* + * Propose that we can have incomplete locale file (w/o "{yes,no}str"). + * Initialize them before loading. In case of complete locale, they'll + * be initialized to loaded value, otherwise they'll not be touched. + */ + _messages_locale.yesstr = empty; + _messages_locale.nostr = empty; - return __part_load_locale(name, &_messages_using_locale, - _messages_locale_buf, "LC_MESSAGES", - LCMESSAGES_SIZE_FULL, LCMESSAGES_SIZE_MIN, - (const char **)&_messages_locale); + ret = __part_load_locale(name, &_messages_using_locale, + _messages_locale_buf, "LC_MESSAGES", + LCMESSAGES_SIZE_FULL, LCMESSAGES_SIZE_MIN, + (const char **)&_messages_locale); #endif + return ret; } struct lc_messages_T * -__get_current_messages_locale(void) { - - return (_messages_using_locale - ? &_messages_locale - : (struct lc_messages_T *)&_C_messages_locale); +__get_current_messages_locale (void) +{ + struct _thr_locale_t *cur_locale = __get_current_locale (); + return cur_locale->messages ?: (struct lc_messages_T *) &_C_messages_locale; } #ifdef LOCALE_DEBUG diff --git a/newlib/libc/locale/lmessages.h b/newlib/libc/locale/lmessages.h index 079895d5b..242a67ef9 100644 --- a/newlib/libc/locale/lmessages.h +++ b/newlib/libc/locale/lmessages.h @@ -49,8 +49,9 @@ struct lc_messages_T { #endif }; -struct lc_messages_T *__get_current_messages_locale(void); -int __messages_load_locale(const char *, void *, const char *); +struct lc_messages_T *__get_current_messages_locale (void); +int __messages_load_locale (struct _thr_locale_t *, const char *, void *, + const char *); __END_DECLS diff --git a/newlib/libc/locale/lmonetary.c b/newlib/libc/locale/lmonetary.c index 80c2d0442..344a6d81a 100644 --- a/newlib/libc/locale/lmonetary.c +++ b/newlib/libc/locale/lmonetary.c @@ -28,10 +28,9 @@ #include <limits.h> #include <stdlib.h> -#include "lmonetary.h" +#include "setlocale.h" #include "ldpart.h" -extern int __mlocale_changed; extern const char * __fix_locale_grouping_str(const char *); #define LCMONETARY_SIZE (sizeof(struct lc_monetary_T) / sizeof(char *)) @@ -75,11 +74,11 @@ static const struct lc_monetary_T _C_monetary_locale = { #endif }; +#ifndef __CYGWIN__ static struct lc_monetary_T _monetary_locale; static int _monetary_using_locale; static char *_monetary_locale_buf; -#ifndef __CYGWIN__ static char cnv(const char *str) { int i = strtol(str, NULL, 10); @@ -90,97 +89,66 @@ cnv(const char *str) { #endif int -__monetary_load_locale(const char *name , void *f_wctomb, const char *charset) +__monetary_load_locale (struct _thr_locale_t *locale, const char *name , + void *f_wctomb, const char *charset) { - int ret; + int ret; + struct lc_monetary_T mo; + char *bufp = NULL; #ifdef __CYGWIN__ - extern int __set_lc_monetary_from_win (const char *, - const struct lc_monetary_T *, - struct lc_monetary_T *, char **, - void *, const char *); - int old_monetary_using_locale = _monetary_using_locale; - _monetary_using_locale = 0; - ret = __set_lc_monetary_from_win (name, &_C_monetary_locale, - &_monetary_locale, - &_monetary_locale_buf, - f_wctomb, charset); - /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ - if (ret < 0) - _monetary_using_locale = old_monetary_using_locale; - else - { - _monetary_using_locale = ret; - __mlocale_changed = 1; - ret = 0; - } + extern int __set_lc_monetary_from_win (const char *, + const struct lc_monetary_T *, + struct lc_monetary_T *, char **, + void *, const char *); + ret = __set_lc_monetary_from_win (name, &_C_monetary_locale, &mo, &bufp, + f_wctomb, charset); + /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ + if (ret >= 0) + { + struct lc_monetary_T *mop = NULL; + + if (ret > 0) + { + mop = (struct lc_monetary_T *) calloc (1, sizeof *mop); + if (!mop) + return -1; + memcpy (mop, &mo, sizeof *mop); + } + locale->monetary = ret == 0 ? NULL : mop; + if (locale->monetary_buf) + free (locale->monetary_buf); + locale->monetary_buf = bufp; + ret = 0; + } #else - __mlocale_changed = 1; - ret = __part_load_locale(name, &_monetary_using_locale, - _monetary_locale_buf, "LC_MONETARY", - LCMONETARY_SIZE, LCMONETARY_SIZE, - (const char **)&_monetary_locale); - if (ret == 0 && _monetary_using_locale) { - _monetary_locale.mon_grouping = - __fix_locale_grouping_str(_monetary_locale.mon_grouping); + ret = __part_load_locale(name, &_monetary_using_locale, + _monetary_locale_buf, "LC_MONETARY", + LCMONETARY_SIZE, LCMONETARY_SIZE, + (const char **)&_monetary_locale); + if (ret == 0 && _monetary_using_locale) { + _monetary_locale.mon_grouping = + __fix_locale_grouping_str(_monetary_locale.mon_grouping); #define M_ASSIGN_CHAR(NAME) (((char *)_monetary_locale.NAME)[0] = \ - cnv(_monetary_locale.NAME)) + cnv(_monetary_locale.NAME)) - M_ASSIGN_CHAR(int_frac_digits); - M_ASSIGN_CHAR(frac_digits); - M_ASSIGN_CHAR(p_cs_precedes); - M_ASSIGN_CHAR(p_sep_by_space); - M_ASSIGN_CHAR(n_cs_precedes); - M_ASSIGN_CHAR(n_sep_by_space); - M_ASSIGN_CHAR(p_sign_posn); - M_ASSIGN_CHAR(n_sign_posn); - } + M_ASSIGN_CHAR(int_frac_digits); + M_ASSIGN_CHAR(frac_digits); + M_ASSIGN_CHAR(p_cs_precedes); + M_ASSIGN_CHAR(p_sep_by_space); + M_ASSIGN_CHAR(n_cs_precedes); + M_ASSIGN_CHAR(n_sep_by_space); + M_ASSIGN_CHAR(p_sign_posn); + M_ASSIGN_CHAR(n_sign_posn); + } #endif - return ret; + return ret; } struct lc_monetary_T * -__get_current_monetary_locale(void) { - - return (_monetary_using_locale - ? &_monetary_locale - : (struct lc_monetary_T *)&_C_monetary_locale); -} - -#ifdef LOCALE_DEBUG -void -monetdebug() { -printf( "int_curr_symbol = %s\n" - "currency_symbol = %s\n" - "mon_decimal_point = %s\n" - "mon_thousands_sep = %s\n" - "mon_grouping = %s\n" - "positive_sign = %s\n" - "negative_sign = %s\n" - "int_frac_digits = %d\n" - "frac_digits = %d\n" - "p_cs_precedes = %d\n" - "p_sep_by_space = %d\n" - "n_cs_precedes = %d\n" - "n_sep_by_space = %d\n" - "p_sign_posn = %d\n" - "n_sign_posn = %d\n", - _monetary_locale.int_curr_symbol, - _monetary_locale.currency_symbol, - _monetary_locale.mon_decimal_point, - _monetary_locale.mon_thousands_sep, - _monetary_locale.mon_grouping, - _monetary_locale.positive_sign, - _monetary_locale.negative_sign, - _monetary_locale.int_frac_digits[0], - _monetary_locale.frac_digits[0], - _monetary_locale.p_cs_precedes[0], - _monetary_locale.p_sep_by_space[0], - _monetary_locale.n_cs_precedes[0], - _monetary_locale.n_sep_by_space[0], - _monetary_locale.p_sign_posn[0], - _monetary_locale.n_sign_posn[0] -); +__get_current_monetary_locale (void) +{ + struct _thr_locale_t *cur_locale = __get_current_locale (); + return cur_locale->monetary ?: (struct lc_monetary_T *) &_C_monetary_locale; } -#endif /* LOCALE_DEBUG */ diff --git a/newlib/libc/locale/lmonetary.h b/newlib/libc/locale/lmonetary.h index 7aa21e298..98000d615 100644 --- a/newlib/libc/locale/lmonetary.h +++ b/newlib/libc/locale/lmonetary.h @@ -68,8 +68,9 @@ struct lc_monetary_T { #endif }; -struct lc_monetary_T *__get_current_monetary_locale(void); -int __monetary_load_locale(const char *, void *, const char *); +struct lc_monetary_T *__get_current_monetary_locale (void); +int __monetary_load_locale (struct _thr_locale_t *, const char *, void *, + const char *); __END_DECLS diff --git a/newlib/libc/locale/lnumeric.c b/newlib/libc/locale/lnumeric.c index ae2447066..f74c44612 100644 --- a/newlib/libc/locale/lnumeric.c +++ b/newlib/libc/locale/lnumeric.c @@ -24,11 +24,9 @@ * SUCH DAMAGE. */ -#include <limits.h> -#include "lnumeric.h" +#include "setlocale.h" #include "ldpart.h" -extern int __nlocale_changed; extern const char *__fix_locale_grouping_str(const char *); #define LCNUMERIC_SIZE (sizeof(struct lc_numeric_T) / sizeof(char *)) @@ -46,64 +44,60 @@ static const struct lc_numeric_T _C_numeric_locale = { #endif }; +#ifndef __CYGWIN__ static struct lc_numeric_T _numeric_locale; static int _numeric_using_locale; static char *_numeric_locale_buf; +#endif int -__numeric_load_locale(const char *name , void *f_wctomb, const char *charset) +__numeric_load_locale (struct _thr_locale_t *locale, const char *name , + void *f_wctomb, const char *charset) { - int ret; + int ret; + struct lc_numeric_T nm; + char *bufp = NULL; #ifdef __CYGWIN__ - extern int __set_lc_numeric_from_win (const char *, - const struct lc_numeric_T *, - struct lc_numeric_T *, char **, - void *, const char *); - int old_numeric_using_locale = _numeric_using_locale; - _numeric_using_locale = 0; - ret = __set_lc_numeric_from_win (name, &_C_numeric_locale, - &_numeric_locale, &_numeric_locale_buf, - f_wctomb, charset); - /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ - if (ret < 0) - _numeric_using_locale = old_numeric_using_locale; - else - { - _numeric_using_locale = ret; - __nlocale_changed = 1; - ret = 0; - } + extern int __set_lc_numeric_from_win (const char *, + const struct lc_numeric_T *, + struct lc_numeric_T *, char **, + void *, const char *); + ret = __set_lc_numeric_from_win (name, &_C_numeric_locale, &nm, &bufp, + f_wctomb, charset); + /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ + if (ret >= 0) + { + struct lc_numeric_T *nmp = NULL; + + if (ret > 0) + { + nmp = (struct lc_numeric_T *) calloc (1, sizeof *nmp); + if (!nmp) + return -1; + memcpy (nmp, &nm, sizeof *nmp); + } + locale->numeric = ret == 0 ? NULL : nmp; + if (locale->numeric_buf) + free (locale->numeric_buf); + locale->numeric_buf = bufp; + ret = 0; + } #else - __nlocale_changed = 1; - ret = __part_load_locale(name, &_numeric_using_locale, - _numeric_locale_buf, "LC_NUMERIC", - LCNUMERIC_SIZE, LCNUMERIC_SIZE, - (const char **)&_numeric_locale); - if (ret == 0 && _numeric_using_locale) - _numeric_locale.grouping = - __fix_locale_grouping_str(_numeric_locale.grouping); + ret = __part_load_locale(name, &_numeric_using_locale, + _numeric_locale_buf, "LC_NUMERIC", + LCNUMERIC_SIZE, LCNUMERIC_SIZE, + (const char **)&_numeric_locale); + if (ret == 0 && _numeric_using_locale) + _numeric_locale.grouping = + __fix_locale_grouping_str(_numeric_locale.grouping); #endif - return ret; + return ret; } struct lc_numeric_T * -__get_current_numeric_locale(void) { - - return (_numeric_using_locale - ? &_numeric_locale - : (struct lc_numeric_T *)&_C_numeric_locale); -} - -#ifdef LOCALE_DEBUG -void -numericdebug(void) { -printf( "decimal_point = %s\n" - "thousands_sep = %s\n" - "grouping = %s\n", - _numeric_locale.decimal_point, - _numeric_locale.thousands_sep, - _numeric_locale.grouping -); +__get_current_numeric_locale (void) +{ + struct _thr_locale_t *cur_locale = __get_current_locale (); + return cur_locale->numeric ?: (struct lc_numeric_T *) &_C_numeric_locale; } -#endif /* LOCALE_DEBUG */ diff --git a/newlib/libc/locale/lnumeric.h b/newlib/libc/locale/lnumeric.h index 2bd7d9745..8a41966b7 100644 --- a/newlib/libc/locale/lnumeric.h +++ b/newlib/libc/locale/lnumeric.h @@ -46,8 +46,9 @@ struct lc_numeric_T { #endif }; -struct lc_numeric_T *__get_current_numeric_locale(void); -int __numeric_load_locale(const char *, void *, const char *); +struct lc_numeric_T *__get_current_numeric_locale (void); +int __numeric_load_locale (struct _thr_locale_t *, const char *, void *, + const char *); __END_DECLS diff --git a/newlib/libc/locale/locale.c b/newlib/libc/locale/locale.c index 33b535941..83d83a5ed 100644 --- a/newlib/libc/locale/locale.c +++ b/newlib/libc/locale/locale.c @@ -172,30 +172,22 @@ No supporting OS subroutines are required. #include <newlib.h> #include <errno.h> -#include <locale.h> +#include <ctype.h> #include <string.h> #include <limits.h> #include <reent.h> #include <stdlib.h> #include <wchar.h> -#include "lmessages.h" -#include "lmonetary.h" -#include "lnumeric.h" -#include "lctype.h" -#include "timelocal.h" +#include "setlocale.h" #include "../stdlib/local.h" -#define _LC_LAST 7 -#define ENCODING_LEN 31 - -#ifdef __CYGWIN__ /* Cygwin starts with LC_CTYPE set to "C.UTF-8". */ +#ifdef __CYGWIN__ /* Has to be kept available as exported symbol for + backward compatibility. Set it in setlocale, but + otherwise ignore it. Applications compiled after + 2010 don't use it anymore. */ int __EXPORT __mb_cur_max = 6; -#else -int __EXPORT __mb_cur_max = 1; #endif -int __nlocale_changed = 0; -int __mlocale_changed = 0; char *_PathLocale = NULL; static @@ -234,44 +226,56 @@ static char *categories[_LC_LAST] = { */ char __default_locale[ENCODING_LEN + 1] = DEFAULT_LOCALE; -/* - * Current locales for each category - */ -static char current_categories[_LC_LAST][ENCODING_LEN + 1] = { - "C", - "C", -#ifdef __CYGWIN__ /* Cygwin starts with LC_CTYPE set to "C.UTF-8". */ - "C.UTF-8", +struct _thr_locale_t __global_locale = +{ + { "C", "C", DEFAULT_LOCALE, "C", "C", "C", "C", }, +#ifdef __CYGWIN__ + __utf8_wctomb, + __utf8_mbtowc, +#else + __ascii_wctomb, + __ascii_mbtowc, +#endif + NULL, + 0, +#ifndef __HAVE_LOCALE_INFO__ + "\1", + "ASCII", + "ASCII", #else - "C", + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, +#ifdef __CYGWIN__ + NULL, +#endif #endif - "C", - "C", - "C", - "C", }; /* - * The locales we are going to try and load + * The locales we are going to try and load. These are only temporary + * variables and only used in setlocale. */ 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)]; +/* Renamed from current_locale_string to make clear this is only the + *global* string for setlocale (LC_ALL, NULL). There's no equivalent + functionality for uselocale. */ +static char global_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)]; static char *currentlocale(void); static char *loadlocale(struct _reent *, int); static const char *__get_locale_env(struct _reent *, int); #endif /* _MB_CAPABLE */ -#ifdef __CYGWIN__ -static char lc_ctype_charset[ENCODING_LEN + 1] = "UTF-8"; -#else -static char lc_ctype_charset[ENCODING_LEN + 1] = "ASCII"; -#endif -static char lc_message_charset[ENCODING_LEN + 1] = "ASCII"; -static int lc_ctype_cjk_lang = 0; - char * _DEFUN(_setlocale_r, (p, category, locale), struct _reent *p _AND @@ -297,13 +301,13 @@ _DEFUN(_setlocale_r, (p, category, locale), } if (locale == NULL) - return category != LC_ALL ? current_categories[category] : currentlocale(); + return category != LC_ALL ? __global_locale.categories[category] : currentlocale(); /* * Default to the current locale for everything. */ for (i = 1; i < _LC_LAST; ++i) - strcpy (new_categories[i], current_categories[i]); + strcpy (new_categories[i], __global_locale.categories[i]); /* * Now go fill up new_categories from the locale argument @@ -395,7 +399,7 @@ _DEFUN(_setlocale_r, (p, category, locale), for (i = 1; i < _LC_LAST; ++i) { - strcpy (saved_categories[i], current_categories[i]); + strcpy (saved_categories[i], __global_locale.categories[i]); if (loadlocale (p, i) == NULL) { saverr = p->_errno; @@ -422,18 +426,18 @@ currentlocale() { int i; - (void)strcpy(current_locale_string, current_categories[1]); + (void)strcpy(global_locale_string, __global_locale.categories[1]); for (i = 2; i < _LC_LAST; ++i) - if (strcmp(current_categories[1], current_categories[i])) { + if (strcmp(__global_locale.categories[1], __global_locale.categories[i])) { for (i = 2; i < _LC_LAST; ++i) { - (void)strcat(current_locale_string, "/"); - (void)strcat(current_locale_string, - current_categories[i]); + (void)strcat(global_locale_string, "/"); + (void)strcat(global_locale_string, + __global_locale.categories[i]); } break; } - return (current_locale_string); + return (global_locale_string); } #endif /* _MB_CAPABLE */ @@ -441,10 +445,11 @@ currentlocale() #ifdef __CYGWIN__ extern void __set_charset_from_locale (const char *locale, char *charset); extern char *__set_locale_from_locale_alias (const char *, char *); -extern int __collate_load_locale (const char *, void *, const char *); +extern int __collate_load_locale (struct _thr_locale_t *, const char *, void *, + const char *); #endif /* __CYGWIN__ */ -extern void __set_ctype (const char *charset); +extern void __set_ctype (struct _reent *, const char *charset); static char * loadlocale(struct _reent *p, int category) @@ -455,7 +460,7 @@ loadlocale(struct _reent *p, int category) The string must be in one of the allowed locale strings, either one in POSIX-style, or one in the old newlib style to maintain backward compatibility. If the local string is correct, the charset - is extracted and stored in lc_ctype_charset or lc_message_charset + is extracted and stored in ctype_codeset or message_charset dependent on the cateogry. */ char *locale = NULL; char charset[ENCODING_LEN + 1]; @@ -468,8 +473,8 @@ loadlocale(struct _reent *p, int category) int cjknarrow = 0; /* Avoid doing everything twice if nothing has changed. */ - if (!strcmp (new_categories[category], current_categories[category])) - return current_categories[category]; + if (!strcmp (new_categories[category], __global_locale.categories[category])) + return __global_locale.categories[category]; #ifdef __CYGWIN__ /* This additional code handles the case that the incoming locale string @@ -852,49 +857,61 @@ restart: switch (category) { case LC_CTYPE: - strcpy (lc_ctype_charset, charset); - __mb_cur_max = mbc_max; +#ifndef __HAVE_LOCALE_INFO__ + strcpy (__global_locale.ctype_codeset, charset); + __global_locale.mb_cur_max[0] = mbc_max; +#endif +#ifdef __CYGWIN__ + __mb_cur_max = mbc_max; /* Only for backward compat */ +#endif __wctomb = l_wctomb; __mbtowc = l_mbtowc; - __set_ctype (charset); + __set_ctype (NULL, charset); /* Determine the width for the "CJK Ambiguous Width" category of characters. This is used in wcwidth(). Assume single width for single-byte charsets, and double width for multi-byte charsets other than UTF-8. For UTF-8, use double width for the East Asian languages ("ja", "ko", "zh"), and single width for everything else. Single width can also be forced with the "@cjknarrow" modifier. */ - lc_ctype_cjk_lang = !cjknarrow + __global_locale.cjk_lang = !cjknarrow && mbc_max > 1 && (charset[0] != 'U' || strncmp (locale, "ja", 2) == 0 || strncmp (locale, "ko", 2) == 0 || strncmp (locale, "zh", 2) == 0); #ifdef __HAVE_LOCALE_INFO__ - ret = __ctype_load_locale (locale, (void *) l_wctomb, charset, mbc_max); + ret = __ctype_load_locale (__get_global_locale (), locale, + (void *) l_wctomb, charset, mbc_max); #endif /* __HAVE_LOCALE_INFO__ */ break; case LC_MESSAGES: - strcpy (lc_message_charset, charset); #ifdef __HAVE_LOCALE_INFO__ - ret = __messages_load_locale (locale, (void *) l_wctomb, charset); + ret = __messages_load_locale (__get_global_locale (), locale, + (void *) l_wctomb, charset); if (!ret) +#else + strcpy (__global_locale.message_codeset, charset); #endif /* __HAVE_LOCALE_INFO__ */ break; #ifdef __HAVE_LOCALE_INFO__ #ifdef __CYGWIN__ /* Right now only Cygwin supports a __collate_load_locale function at all. */ case LC_COLLATE: - ret = __collate_load_locale (locale, (void *) l_mbtowc, charset); + ret = __collate_load_locale (__get_global_locale (), locale, + (void *) l_mbtowc, charset); break; #endif case LC_MONETARY: - ret = __monetary_load_locale (locale, (void *) l_wctomb, charset); + ret = __monetary_load_locale (__get_global_locale (), locale, + (void *) l_wctomb, charset); break; case LC_NUMERIC: - ret = __numeric_load_locale (locale, (void *) l_wctomb, charset); + ret = __numeric_load_locale (__get_global_locale (), locale, + (void *) l_wctomb, charset); break; case LC_TIME: - ret = __time_load_locale (locale, (void *) l_wctomb, charset); + ret = __time_load_locale (__get_global_locale (), locale, + (void *) l_wctomb, charset); break; #endif /* __HAVE_LOCALE_INFO__ */ default: @@ -904,7 +921,7 @@ restart: if (ret) FAIL; #endif /* __HAVE_LOCALE_INFO__ */ - return strcpy(current_categories[category], new_categories[category]); + return strcpy(__global_locale.categories[category], new_categories[category]); } static const char * @@ -934,88 +951,96 @@ __get_locale_env(struct _reent *p, int category) char * _DEFUN_VOID(__locale_charset) { -#if 0//def __HAVE_LOCALE_INFO__ - return __get_current_ctype_locale ()->codeset; +#ifdef __HAVE_LOCALE_INFO__ + return (char *) __get_current_ctype_locale ()->codeset; #else - return lc_ctype_charset; + return __global_locale.ctype_codeset; #endif } int _DEFUN_VOID(__locale_mb_cur_max) { -#if 0//def __HAVE_LOCALE_INFO__ +#ifdef __HAVE_LOCALE_INFO__ return __get_current_ctype_locale ()->mb_cur_max[0]; #else - return __mb_cur_max; + return __global_locale.mb_cur_max[0]; #endif } - char * _DEFUN_VOID(__locale_msgcharset) { #ifdef __HAVE_LOCALE_INFO__ return (char *) __get_current_messages_locale ()->codeset; #else - return lc_message_charset; + return (char *) __global_locale.message_codeset; #endif } int _DEFUN_VOID(__locale_cjk_lang) { - return lc_ctype_cjk_lang; +#ifdef __HAVE_LOCALE_INFO__ + return __get_current_locale ()->cjk_lang; +#else + return __global_locale.cjk_lang; +#endif } +#ifdef __HAVE_LOCALE_INFO__ +char * +_DEFUN_VOID(__locale_ctype_ptr) +{ + /* Only check if the current thread/reent has a locale. ctype_ptr is unused + in __global_locale, rather the global variable __ctype_ptr__ is used. */ + return __get_locale_r (_REENT) ? __get_locale_r (_REENT)->ctype_ptr + : __ctype_ptr__; +} + +#endif + struct lconv * _DEFUN(_localeconv_r, (data), struct _reent *data) { #ifdef __HAVE_LOCALE_INFO__ - if (__nlocale_changed) - { - struct lc_numeric_T *n = __get_current_numeric_locale (); - lconv.decimal_point = (char *) n->decimal_point; - lconv.thousands_sep = (char *) n->thousands_sep; - lconv.grouping = (char *) n->grouping; - __nlocale_changed = 0; - } - if (__mlocale_changed) - { - struct lc_monetary_T *m = __get_current_monetary_locale (); - lconv.int_curr_symbol = (char *) m->int_curr_symbol; - lconv.currency_symbol = (char *) m->currency_symbol; - lconv.mon_decimal_point = (char *) m->mon_decimal_point; - lconv.mon_thousands_sep = (char *) m->mon_thousands_sep; - lconv.mon_grouping = (char *) m->mon_grouping; - lconv.positive_sign = (char *) m->positive_sign; - lconv.negative_sign = (char *) m->negative_sign; - lconv.int_frac_digits = m->int_frac_digits[0]; - lconv.frac_digits = m->frac_digits[0]; - lconv.p_cs_precedes = m->p_cs_precedes[0]; - lconv.p_sep_by_space = m->p_sep_by_space[0]; - lconv.n_cs_precedes = m->n_cs_precedes[0]; - lconv.n_sep_by_space = m->n_sep_by_space[0]; - lconv.p_sign_posn = m->p_sign_posn[0]; - lconv.n_sign_posn = m->n_sign_posn[0]; + struct lc_numeric_T *n = __get_current_numeric_locale (); + struct lc_monetary_T *m = __get_current_monetary_locale (); + + lconv.decimal_point = (char *) n->decimal_point; + lconv.thousands_sep = (char *) n->thousands_sep; + lconv.grouping = (char *) n->grouping; + lconv.int_curr_symbol = (char *) m->int_curr_symbol; + lconv.currency_symbol = (char *) m->currency_symbol; + lconv.mon_decimal_point = (char *) m->mon_decimal_point; + lconv.mon_thousands_sep = (char *) m->mon_thousands_sep; + lconv.mon_grouping = (char *) m->mon_grouping; + lconv.positive_sign = (char *) m->positive_sign; + lconv.negative_sign = (char *) m->negative_sign; + lconv.int_frac_digits = m->int_frac_digits[0]; + lconv.frac_digits = m->frac_digits[0]; + lconv.p_cs_precedes = m->p_cs_precedes[0]; + lconv.p_sep_by_space = m->p_sep_by_space[0]; + lconv.n_cs_precedes = m->n_cs_precedes[0]; + lconv.n_sep_by_space = m->n_sep_by_space[0]; + lconv.p_sign_posn = m->p_sign_posn[0]; + lconv.n_sign_posn = m->n_sign_posn[0]; #ifdef __HAVE_LOCALE_INFO_EXTENDED__ - lconv.int_p_cs_precedes = m->int_p_cs_precedes[0]; - lconv.int_p_sep_by_space = m->int_p_sep_by_space[0]; - lconv.int_n_cs_precedes = m->int_n_cs_precedes[0]; - lconv.int_n_sep_by_space = m->int_n_sep_by_space[0]; - lconv.int_n_sign_posn = m->int_n_sign_posn[0]; - lconv.int_p_sign_posn = m->int_p_sign_posn[0]; + lconv.int_p_cs_precedes = m->int_p_cs_precedes[0]; + lconv.int_p_sep_by_space = m->int_p_sep_by_space[0]; + lconv.int_n_cs_precedes = m->int_n_cs_precedes[0]; + lconv.int_n_sep_by_space = m->int_n_sep_by_space[0]; + lconv.int_n_sign_posn = m->int_n_sign_posn[0]; + lconv.int_p_sign_posn = m->int_p_sign_posn[0]; #else /* !__HAVE_LOCALE_INFO_EXTENDED__ */ - lconv.int_p_cs_precedes = m->p_cs_precedes[0]; - lconv.int_p_sep_by_space = m->p_sep_by_space[0]; - lconv.int_n_cs_precedes = m->n_cs_precedes[0]; - lconv.int_n_sep_by_space = m->n_sep_by_space[0]; - lconv.int_n_sign_posn = m->n_sign_posn[0]; - lconv.int_p_sign_posn = m->p_sign_posn[0]; + lconv.int_p_cs_precedes = m->p_cs_precedes[0]; + lconv.int_p_sep_by_space = m->p_sep_by_space[0]; + lconv.int_n_cs_precedes = m->n_cs_precedes[0]; + lconv.int_n_sep_by_space = m->n_sep_by_space[0]; + lconv.int_n_sign_posn = m->n_sign_posn[0]; + lconv.int_p_sign_posn = m->p_sign_posn[0]; #endif /* !__HAVE_LOCALE_INFO_EXTENDED__ */ - __mlocale_changed = 0; - } #endif /* __HAVE_LOCALE_INFO__ */ return (struct lconv *) &lconv; } diff --git a/newlib/libc/locale/nl_langinfo.c b/newlib/libc/locale/nl_langinfo.c index 4b7e983f0..3acbc3a04 100644 --- a/newlib/libc/locale/nl_langinfo.c +++ b/newlib/libc/locale/nl_langinfo.c @@ -32,11 +32,7 @@ #include <stdlib.h> #include <string.h> -#include "lctype.h" -#include "timelocal.h" -#include "lnumeric.h" -#include "lmonetary.h" -#include "lmessages.h" +#include "setlocale.h" #ifndef __CYGWIN__ #define TRANSITION_PERIOD_HACK diff --git a/newlib/libc/locale/setlocale.h b/newlib/libc/locale/setlocale.h index 3eb769863..a4b55a6c7 100644 --- a/newlib/libc/locale/setlocale.h +++ b/newlib/libc/locale/setlocale.h @@ -29,8 +29,89 @@ #ifndef _SETLOCALE_H_ #define _SETLOCALE_H_ +#include <limits.h> +#include <string.h> +#include <stdlib.h> +#include <locale.h> +#include "lctype.h" +#include "lmessages.h" +#include "lnumeric.h" +#include "timelocal.h" +#include "lmonetary.h" + #define ENCODING_LEN 31 #define CATEGORY_LEN 11 +#define _LC_LAST 7 + +#ifdef __CYGWIN__ +struct lc_collate_T; +#endif + +struct _thr_locale_t +{ + char categories[_LC_LAST][ENCODING_LEN + 1]; + int (*__wctomb) (struct _reent *, char *, wchar_t, + const char *, mbstate_t *); + int (*__mbtowc) (struct _reent *, wchar_t *, const char *, + size_t, const char *, mbstate_t *); + char *ctype_ptr; /* Unused in __global_locale */ + int cjk_lang; +#ifndef __HAVE_LOCALE_INFO__ + char mb_cur_max[2]; + char ctype_codeset[ENCODING_LEN + 1]; + char message_codeset[ENCODING_LEN + 1]; +#else + struct lc_ctype_T *ctype; + char *ctype_buf; + struct lc_monetary_T *monetary; + char *monetary_buf; + struct lc_numeric_T *numeric; + char *numeric_buf; + struct lc_time_T *time; + char *time_buf; + struct lc_messages_T *messages; + char *messages_buf; +#ifdef __CYGWIN__ + struct lc_collate_T *collate; +#endif + /* Append more categories here. */ +#endif +}; + +#ifdef __cplusplus +extern "C" { +#endif + +extern struct _thr_locale_t __global_locale; + +/* In POSIX terms the global locale is the process-wide locale. Use this + function to always refer to the global locale. */ +_ELIDABLE_INLINE struct _thr_locale_t * +__get_global_locale () +{ + return &__global_locale; +} + +/* Per REENT locale. This is newlib-internal. */ +_ELIDABLE_INLINE struct _thr_locale_t * +__get_locale_r (struct _reent *r) +{ + return r->_locale; +} + +/* In POSIX terms the current locale is the locale used by all functions + using locale info without providing a locale as parameter (*_l functions). + The current locale is either the locale of the current thread, if the + thread called uselocale, or the global locale if not. */ +_ELIDABLE_INLINE struct _thr_locale_t * +__get_current_locale () +{ + return _REENT->_locale ?: &__global_locale; +} + +#ifdef __cplusplus +} +#endif extern char *_PathLocale; diff --git a/newlib/libc/locale/timelocal.c b/newlib/libc/locale/timelocal.c index ca2f79b50..eda776a5b 100644 --- a/newlib/libc/locale/timelocal.c +++ b/newlib/libc/locale/timelocal.c @@ -30,11 +30,7 @@ #include <stddef.h> #include "ldpart.h" -#include "timelocal.h" - -static struct lc_time_T _time_locale; -static int _time_using_locale; -static char *time_locale_buf; +#include "setlocale.h" #define LCTIME_SIZE (sizeof(struct lc_time_T) / sizeof(char *)) @@ -149,40 +145,56 @@ static const struct lc_time_T _C_time_locale = { #endif }; -struct lc_time_T * -__get_current_time_locale(void) { - return (_time_using_locale - ? &_time_locale - : (struct lc_time_T *)&_C_time_locale); -} +#ifndef __CYGWIN__ +static struct lc_time_T _time_locale; +static int _time_using_locale; +static char *time_locale_buf; +#endif int -__time_load_locale(const char *name, void *f_wctomb, const char *charset) { - - int ret; +__time_load_locale (struct _thr_locale_t *locale, const char *name, + void *f_wctomb, const char *charset) +{ + int ret; + struct lc_time_T ti; + char *bufp = NULL; #ifdef __CYGWIN__ - extern int __set_lc_time_from_win (const char *, - const struct lc_time_T *, - struct lc_time_T *, - char **, void *, const char *); - int old_time_using_locale = _time_using_locale; - _time_using_locale = 0; - ret = __set_lc_time_from_win (name, &_C_time_locale, &_time_locale, - &time_locale_buf, f_wctomb, charset); - /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ - if (ret < 0) - _time_using_locale = old_time_using_locale; - else - { - _time_using_locale = ret; - ret = 0; - } + extern int __set_lc_time_from_win (const char *, const struct lc_time_T *, + struct lc_time_T *, char **, void *, + const char *); + ret = __set_lc_time_from_win (name, &_C_time_locale, &ti, &bufp, + f_wctomb, charset); + /* ret == -1: error, ret == 0: C/POSIX, ret > 0: valid */ + if (ret >= 0) + { + struct lc_time_T *tip = NULL; + + if (ret > 0) + { + tip = (struct lc_time_T *) calloc (1, sizeof *tip); + if (!tip) + return -1; + memcpy (tip, &ti, sizeof *tip); + } + locale->time = ret == 0 ? NULL : tip; + if (locale->time_buf) + free (locale->time_buf); + locale->time_buf = bufp; + ret = 0; + } #else - ret = __part_load_locale(name, &_time_using_locale, - time_locale_buf, "LC_TIME", - LCTIME_SIZE, LCTIME_SIZE, - (const char **)&_time_locale); + ret = __part_load_locale(name, &_time_using_locale, + time_locale_buf, "LC_TIME", + LCTIME_SIZE, LCTIME_SIZE, + (const char **)&_time_locale); #endif - return (ret); + return (ret); +} + +struct lc_time_T * +__get_current_time_locale (void) +{ + struct _thr_locale_t *cur_locale = __get_current_locale (); + return cur_locale->time ?: (struct lc_time_T *) &_C_time_locale; } diff --git a/newlib/libc/locale/timelocal.h b/newlib/libc/locale/timelocal.h index a0c1ef7ef..6004ad88b 100644 --- a/newlib/libc/locale/timelocal.h +++ b/newlib/libc/locale/timelocal.h @@ -77,8 +77,9 @@ struct lc_time_T { #endif }; -struct lc_time_T *__get_current_time_locale(void); -int __time_load_locale(const char *, void *, const char *); +struct lc_time_T *__get_current_time_locale (void); +int __time_load_locale (struct _thr_locale_t *, const char *, void *, + const char *); __END_DECLS |