On Sun, Jul 12, 2015 at 03:56:21PM +0200, Sebastien Marie wrote:
> _ctype_ is used in ctype.h header in order to define inline functions
> for determining character class: isalnum(3), isalpha(3), ...
>
> The character class comes from lc_ctype.
>
> Switch _ctype_ from global variable to function in order to retrieve the
> current locale state (global or per-thread).
>
Here an updated patch.
It includes changes in libstdc++ (as _ctype_ is directly used here too),
and I reformat the patch to follow style(9) a bit more.
--
Sebastien Marie
Index: b/include/ctype.h
===================================================================
--- a/include/ctype.h 2015-07-14 08:48:56.004595151 +0200
+++ b/include/ctype.h 2015-07-14 08:54:13.956819666 +0200
@@ -53,7 +53,7 @@
__BEGIN_DECLS
-extern const char *_ctype_;
+extern const char *_ctype_(void);
extern const short *_tolower_tab_;
extern const short *_toupper_tab_;
@@ -90,57 +90,68 @@
__only_inline int isalnum(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] & (_U|_L|_N)));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & (_U|_L|_N)));
}
__only_inline int isalpha(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] & (_U|_L)));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & (_U|_L)));
}
__only_inline int iscntrl(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] & _C));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & _C));
}
__only_inline int isdigit(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] & _N));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & _N));
}
__only_inline int isgraph(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] &
(_P|_U|_L|_N)));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & (_P|_U|_L|_N)));
}
__only_inline int islower(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] & _L));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & _L));
}
__only_inline int isprint(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] &
(_P|_U|_L|_N|_B)));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & (_P|_U|_L|_N|_B)));
}
__only_inline int ispunct(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] & _P));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & _P));
}
__only_inline int isspace(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] & _S));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & _S));
}
__only_inline int isupper(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] & _U));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & _U));
}
__only_inline int isxdigit(int _c)
{
- return (_c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)_c] & (_N|_X)));
+ return (_c == -1 ?
+ 0 : ((_ctype_() + 1)[(unsigned char)_c] & (_N|_X)));
}
__only_inline int tolower(int _c)
Index: b/lib/libc/gen/ctype_.c
===================================================================
--- a/lib/libc/gen/ctype_.c 2015-07-14 08:48:56.004595151 +0200
+++ b/lib/libc/gen/ctype_.c 2015-07-14 08:49:17.404801180 +0200
@@ -72,5 +72,3 @@
0, 0, 0, 0, 0, 0, 0, 0, /* F0 */
0, 0, 0, 0, 0, 0, 0, 0 /* F8 */
};
-
-const char *_ctype_ = _C_ctype_;
Index: b/lib/libc/gen/isctype.c
===================================================================
--- a/lib/libc/gen/isctype.c 2015-07-14 08:48:56.004595151 +0200
+++ b/lib/libc/gen/isctype.c 2015-07-14 08:56:22.407746015 +0200
@@ -41,14 +41,16 @@
int
isalnum(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_U|_L|_N)));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & (_U|_L|_N)));
}
#undef isalpha
int
isalpha(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_U|_L)));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & (_U|_L)));
}
#undef isblank
@@ -62,63 +64,72 @@
int
iscntrl(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _C));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & _C));
}
#undef isdigit
int
isdigit(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _N));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & _N));
}
#undef isgraph
int
isgraph(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] &
(_P|_U|_L|_N)));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & (_P|_U|_L|_N)));
}
#undef islower
int
islower(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _L));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & _L));
}
#undef isprint
int
isprint(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] &
(_P|_U|_L|_N|_B)));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & (_P|_U|_L|_N|_B)));
}
#undef ispunct
int
ispunct(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _P));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & _P));
}
#undef isspace
int
isspace(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _S));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & _S));
}
#undef isupper
int
isupper(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _U));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & _U));
}
#undef isxdigit
int
isxdigit(int c)
{
- return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_N|_X)));
+ return (c == EOF ?
+ 0 : ((_ctype_() + 1)[(unsigned char)c] & (_N|_X)));
}
#undef isascii
Index: b/lib/libc/locale/runeglue.c
===================================================================
--- a/lib/libc/locale/runeglue.c 2015-07-14 08:48:56.004595151 +0200
+++ b/lib/libc/locale/runeglue.c 2015-07-14 08:53:03.986380137 +0200
@@ -119,16 +119,12 @@
{
if (locale->lc_ctype->rl_tabs != NULL) {
/* LINTED const cast */
- _ctype_ = (const unsigned char *)
- &(locale->lc_ctype->rl_tabs->ctype_tab);
- /* LINTED const cast */
_toupper_tab_ = (const short *)
&(locale->lc_ctype->rl_tabs->toupper_tab);
/* LINTED const cast */
_tolower_tab_ = (const short *)
&(locale->lc_ctype->rl_tabs->tolower_tab);
} else {
- _ctype_ = _C_ctype_;
_toupper_tab_ = _C_toupper_;
_tolower_tab_ = _C_tolower_;
}
Index: b/lib/libc/locale/xlocale.c
===================================================================
--- a/lib/libc/locale/xlocale.c 2015-07-14 08:48:56.004595151 +0200
+++ b/lib/libc/locale/xlocale.c 2015-07-14 08:53:03.986380137 +0200
@@ -73,3 +73,16 @@
} else
return (NULL);
}
+
+
+/* exported via ctype.h */
+const char *
+_ctype_(void)
+{
+ locale_t loc = _current_locale();
+
+ if (loc->lc_ctype->rl_tabs != NULL)
+ return (loc->lc_ctype->rl_tabs->ctype_tab);
+ else
+ return (_C_ctype_);
+}
Index: b/gnu/gcc/libstdc++-v3/config/os/bsd/openbsd/ctype_inline.h
===================================================================
--- a/gnu/gcc/libstdc++-v3/config/os/bsd/openbsd/ctype_inline.h 2015-07-14
08:48:43.714461377 +0200
+++ b/gnu/gcc/libstdc++-v3/config/os/bsd/openbsd/ctype_inline.h 2015-07-14
08:49:17.414799893 +0200
@@ -44,14 +44,14 @@
bool
ctype<char>::
is(mask __m, char __c) const
- { return (_M_table ? _M_table : _ctype_ + 1)[(unsigned char)(__c)] & __m; }
+ { return (_M_table ? _M_table : _ctype_() + 1)[(unsigned char)(__c)] & __m; }
const char*
ctype<char>::
is(const char* __low, const char* __high, mask* __vec) const
{
while (__low < __high)
- *__vec++ = (_M_table ? _M_table : _ctype_ + 1)[*__low++];
+ *__vec++ = (_M_table ? _M_table : _ctype_() + 1)[*__low++];
return __high;
}
Index: b/gnu/gcc/libstdc++-v3/config/os/bsd/openbsd/ctype_noninline.h
===================================================================
--- a/gnu/gcc/libstdc++-v3/config/os/bsd/openbsd/ctype_noninline.h
2015-07-14 08:48:43.714461377 +0200
+++ b/gnu/gcc/libstdc++-v3/config/os/bsd/openbsd/ctype_noninline.h
2015-07-14 08:49:17.424800004 +0200
@@ -46,7 +46,7 @@
size_t __refs)
: facet(__refs), _M_del(__table != 0 && __del),
_M_toupper(NULL), _M_tolower(NULL),
- _M_table(__table ? __table : _ctype_ + 1)
+ _M_table(__table ? __table : _ctype_() + 1)
{
memset(_M_widen, 0, sizeof(_M_widen));
_M_widen_ok = 0;
@@ -57,7 +57,7 @@
ctype<char>::ctype(const mask* __table, bool __del, size_t __refs)
: facet(__refs), _M_del(__table != 0 && __del),
_M_toupper(NULL), _M_tolower(NULL),
- _M_table(__table ? __table : _ctype_ + 1)
+ _M_table(__table ? __table : _ctype_() + 1)
{
memset(_M_widen, 0, sizeof(_M_widen));
_M_widen_ok = 0;