The patch move _CurrentRuneLocale global variable (which hold ctype
support) to locale_t.

- remove the global variable
- move initialisation to struct _locale_t
- add locale_t parameter to several internal functions
- use _current_locale() for get the current locale when using iswalpha(3)
  and co.
- pass LC_GLOBAL_LOCALE to configure ctype with setlocale(3).

Please note this patch is the bigger of the serie, as it combines
removing the global _CurrentRuneLocale variable with use a dynamic
locale state (_current_locale()).

If need, I could split it.
-- 
Sebastien Marie


Index: b/lib/libc/locale/runetable.c
===================================================================
--- a/lib/libc/locale/runetable.c       2015-07-08 09:15:00.593677755 +0200
+++ b/lib/libc/locale/runetable.c       2015-07-08 09:30:32.330646182 +0200
@@ -270,5 +270,3 @@
     },
     NULL
 };
-
-_RuneLocale *_CurrentRuneLocale = &_DefaultRuneLocale;
Index: b/lib/libc/locale/xlocale.c
===================================================================
--- a/lib/libc/locale/xlocale.c 2015-07-08 09:30:11.430429064 +0200
+++ b/lib/libc/locale/xlocale.c 2015-07-08 11:38:50.581670856 +0200
@@ -17,6 +17,7 @@
 
 #include <sys/types.h>
 
+#include "rune.h"
 #include "thread_private.h"
 #include "xlocale_private.h"
 
@@ -25,7 +26,8 @@
  */
 struct _locale_t _lc_global_locale = {
        XLOCALE_INSTALLED,
-       {"C", "C", "C", "C", "C", "C", "C"}
+       {"C", "C", "C", "C", "C", "C", "C"},
+       &_DefaultRuneLocale
 };
 
 
Index: b/lib/libc/locale/xlocale_private.h
===================================================================
--- a/lib/libc/locale/xlocale_private.h 2015-07-08 09:30:11.430429064 +0200
+++ b/lib/libc/locale/xlocale_private.h 2015-07-08 11:39:01.331759641 +0200
@@ -20,9 +20,12 @@
 #include <sys/cdefs.h>
 #include <locale.h>
 
+#include "runetype.h"
+
 struct _locale_t {
        int     flags;
        char    categories[_LC_LAST][32];
+       _RuneLocale             *lc_ctype;
 };
 
 #define XLOCALE_INSTALLED    (1 << 0)
Index: b/lib/libc/locale/___runetype_mb.c
===================================================================
--- a/lib/libc/locale/___runetype_mb.c  2015-07-08 09:15:00.593677755 +0200
+++ b/lib/libc/locale/___runetype_mb.c  2015-07-08 09:30:32.340645321 +0200
@@ -36,9 +36,10 @@
 #include <wctype.h>
 #include "rune.h"
 #include "rune_local.h"
+#include "xlocale_private.h"
 
 _RuneType
-___runetype_mb(wint_t c)
+___runetype_mb(wint_t c, locale_t locale)
 {
        rune_t c0;
        uint32_t x;
@@ -49,7 +50,7 @@
                return (0U);
 
        c0 = (rune_t)c; /* XXX assumes wint_t = int */
-       rr = &_CurrentRuneLocale->rl_runetype_ext;
+       rr = &locale->lc_ctype->rl_runetype_ext;
        base = rr->rr_rune_ranges;
        for (x = rr->rr_nranges; x; x >>= 1) {
                re = base + (x >> 1);
Index: b/lib/libc/locale/iswctype.c
===================================================================
--- a/lib/libc/locale/iswctype.c        2015-07-08 09:15:00.593677755 +0200
+++ b/lib/libc/locale/iswctype.c        2015-07-08 11:42:27.553495230 +0200
@@ -40,132 +40,136 @@
 #include <ctype.h>
 #include <errno.h>
 #include <string.h>
+
 #include "rune.h"
 #include "runetype.h"
 #include "rune_local.h"
+#include "xlocale_private.h"
 #include "_wctrans_local.h"
 
-static __inline _RuneType __runetype_w(wint_t);
-static __inline int __isctype_w(wint_t, _RuneType);
-static __inline wint_t __toupper_w(wint_t);
-static __inline wint_t __tolower_w(wint_t);
+static __inline _RuneType __runetype_w(wint_t, locale_t);
+static __inline int __isctype_w(wint_t, _RuneType, locale_t);
+static __inline wint_t __toupper_w(wint_t, locale_t);
+static __inline wint_t __tolower_w(wint_t, locale_t);
 
 static __inline _RuneType
-__runetype_w(wint_t c)
+__runetype_w(wint_t c, locale_t locale)
 {
-       _RuneLocale *rl = _CurrentRuneLocale;
+       _RuneLocale *rl = locale->lc_ctype;
 
        return (_RUNE_ISCACHED(c) ?
-               rl->rl_runetype[c] : ___runetype_mb(c));
+               rl->rl_runetype[c] : ___runetype_mb(c, locale));
 }
 
 static __inline int
-__isctype_w(wint_t c, _RuneType f)
+__isctype_w(wint_t c, _RuneType f, locale_t locale)
 {
-       return (!!(__runetype_w(c) & f));
+       return (!!(__runetype_w(c, locale) & f));
 }
 
 static __inline wint_t
-__toupper_w(wint_t c)
+__toupper_w(wint_t c, locale_t locale)
 {
-       return (_towctrans(c, _wctrans_upper(_CurrentRuneLocale)));
+       return (_towctrans(c, _wctrans_upper(locale->lc_ctype)));
 }
 
 static __inline wint_t
-__tolower_w(wint_t c)
+__tolower_w(wint_t c, locale_t locale)
 {
-       return (_towctrans(c, _wctrans_lower(_CurrentRuneLocale)));
+       return (_towctrans(c, _wctrans_lower(locale->lc_ctype)));
 }
 
 int
 iswalnum(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_A|_CTYPE_D));
+       return (__isctype_w((c), _CTYPE_A|_CTYPE_D, _current_locale()));
 }
 
 int
 iswalpha(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_A));
+       return (__isctype_w((c), _CTYPE_A, _current_locale()));
 }
 
 int
 iswblank(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_B));
+       return (__isctype_w((c), _CTYPE_B, _current_locale()));
 }
 
 int
 iswcntrl(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_C));
+       return (__isctype_w((c), _CTYPE_C, _current_locale()));
 }
 
 int
 iswdigit(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_D));
+       return (__isctype_w((c), _CTYPE_D, _current_locale()));
 }
 
 int
 iswgraph(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_G));
+       return (__isctype_w((c), _CTYPE_G, _current_locale()));
 }
 
 int
 iswlower(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_L));
+       return (__isctype_w((c), _CTYPE_L, _current_locale()));
 }
 
 int
 iswprint(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_R));
+       return (__isctype_w((c), _CTYPE_R, _current_locale()));
 }
 
 int
 iswpunct(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_P));
+       return (__isctype_w((c), _CTYPE_P, _current_locale()));
 }
 
 int
 iswspace(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_S));
+       return (__isctype_w((c), _CTYPE_S, _current_locale()));
 }
 
 int
 iswupper(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_U));
+       return (__isctype_w((c), _CTYPE_U, _current_locale()));
 }
 
 int
 iswxdigit(wint_t c)
 {
-       return (__isctype_w((c), _CTYPE_X));
+       return (__isctype_w((c), _CTYPE_X, _current_locale()));
 }
 
 wint_t
 towupper(wint_t c)
 {
-       return (__toupper_w(c));
+       return (__toupper_w(c, _current_locale()));
 }
 
 wint_t
 towlower(wint_t c)
 {
-       return (__tolower_w(c));
+       return (__tolower_w(c, _current_locale()));
 }
 
 int
 wcwidth(wchar_t c)
 {
-       if (__isctype_w((c), _CTYPE_R))
-               return (((unsigned)__runetype_w(c) & _CTYPE_SWM) >> _CTYPE_SWS);
+       locale_t locale = _current_locale();
+       if (__isctype_w((c), _CTYPE_R, locale))
+               return (((unsigned)__runetype_w(c, locale)
+                        & _CTYPE_SWM) >> _CTYPE_SWS);
        return -1;
 }
 
@@ -173,7 +177,7 @@
 wctrans(const char *charclass)
 {
        int i;
-       _RuneLocale *rl = _CurrentRuneLocale;
+       _RuneLocale *rl = _current_locale()->lc_ctype;
 
        if (rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_name==NULL)
                _wctrans_init(rl);
@@ -199,7 +203,7 @@
 wctype(const char *property)
 {
        int i;
-       _RuneLocale *rl = _CurrentRuneLocale;
+       _RuneLocale *rl = _current_locale()->lc_ctype;
 
        for (i=0; i<_WCTYPE_NINDEXES; i++)
                if (!strcmp(rl->rl_wctype[i].te_name, property))
@@ -218,5 +222,6 @@
                return 0;
        }
 
-       return (__isctype_w(c, ((_WCTypeEntry *)charclass)->te_mask));
+       return (__isctype_w(c, ((_WCTypeEntry *)charclass)->te_mask,
+                           _current_locale()));
 }
Index: b/lib/libc/locale/multibyte_citrus.c
===================================================================
--- a/lib/libc/locale/multibyte_citrus.c        2015-07-08 09:15:00.593677755 
+0200
+++ b/lib/libc/locale/multibyte_citrus.c        2015-07-08 09:30:32.340645321 
+0200
@@ -35,6 +35,7 @@
 #include "citrus_ctype.h"
 #include "rune.h"
 #include "multibyte.h"
+#include "xlocale_private.h"
 
 int
 mbsinit(const mbstate_t *ps)
@@ -47,7 +48,7 @@
 
        rl = _ps_to_runelocale(ps);
        if (rl == NULL)
-               rl = _CurrentRuneLocale;
+               rl = _current_locale()->lc_ctype;
        cc = rl->rl_citrus_ctype;
        return (*cc->cc_ops->co_mbsinit)(ps);
 }
@@ -60,7 +61,7 @@
 
        if (ps == NULL)
                ps = &mbs;
-       cc = _CurrentRuneLocale->rl_citrus_ctype;
+       cc = _current_locale()->lc_ctype->rl_citrus_ctype;
        return (*cc->cc_ops->co_mbrtowc)(pwc, s, n, _ps_to_private(ps));
 }
 
@@ -83,7 +84,7 @@
 
        if (ps == NULL)
                ps = &mbs;
-       cc = _CurrentRuneLocale->rl_citrus_ctype;
+       cc = _current_locale()->lc_ctype->rl_citrus_ctype;
        return (*cc->cc_ops->co_mbsnrtowcs)(dst, src, nmc, len,
            _ps_to_private(ps));
 }
@@ -96,7 +97,7 @@
 
        if (ps == NULL)
                ps = &mbs;
-       cc = _CurrentRuneLocale->rl_citrus_ctype;
+       cc = _current_locale()->lc_ctype->rl_citrus_ctype;
        return (*cc->cc_ops->co_wcrtomb)(s, wc, _ps_to_private(ps));
 }
 
@@ -119,7 +120,7 @@
 
        if (ps == NULL)
                ps = &mbs;
-       cc = _CurrentRuneLocale->rl_citrus_ctype;
+       cc = _current_locale()->lc_ctype->rl_citrus_ctype;
        return (*cc->cc_ops->co_wcsnrtombs)(dst, src, nwc, len,
            _ps_to_private(ps));
 }
Index: b/lib/libc/locale/nl_langinfo.c
===================================================================
--- a/lib/libc/locale/nl_langinfo.c     2015-07-08 09:15:00.593677755 +0200
+++ b/lib/libc/locale/nl_langinfo.c     2015-07-08 11:38:48.371645285 +0200
@@ -8,13 +8,16 @@
 #include <locale.h>
 #include <nl_types.h>
 #include <langinfo.h>
+
 #include "rune.h"
 #include "runetype.h"
+#include "xlocale_private.h"
 
 char *
 nl_langinfo(nl_item item)
 {
        const char *s;
+       struct _locale_t * loc = _current_locale();
 
        switch (item) {
        case D_T_FMT:
@@ -101,7 +104,7 @@
                s = "";
                break;
        case CODESET:
-               s = _CurrentRuneLocale->rl_codeset;
+               s = loc->lc_ctype->rl_codeset;
                if (!s)
                        s = "";
                break;
Index: b/lib/libc/locale/rune.h
===================================================================
--- a/lib/libc/locale/rune.h    2015-07-08 09:15:00.593677755 +0200
+++ b/lib/libc/locale/rune.h    2015-07-08 09:30:32.350645229 +0200
@@ -75,6 +75,5 @@
  */
 extern size_t __mb_len_max_runtime;
 extern _RuneLocale _DefaultRuneLocale;
-extern _RuneLocale *_CurrentRuneLocale;
 
 #endif /*! _RUNE_H_ */
Index: b/lib/libc/locale/runeglue.c
===================================================================
--- a/lib/libc/locale/runeglue.c        2015-07-08 09:15:00.593677755 +0200
+++ b/lib/libc/locale/runeglue.c        2015-07-08 11:38:50.581670856 +0200
@@ -45,6 +45,7 @@
 #include "rune.h"
 #include "rune_local.h"
 #include "ctype_private.h"
+#include "xlocale_private.h"
 
 #if EOF != -1
 #error "EOF != -1"
@@ -114,18 +115,18 @@
 }
 
 void
-__install_currentrunelocale_ctype()
+__install_currentrunelocale_ctype(locale_t locale)
 {
-       if (_CurrentRuneLocale->rl_tabs != NULL) {
+       if (locale->lc_ctype->rl_tabs != NULL) {
                /* LINTED const cast */
                _ctype_ = (const unsigned char *)
-                   &(_CurrentRuneLocale->rl_tabs->ctype_tab);
+                   &(locale->lc_ctype->rl_tabs->ctype_tab);
                /* LINTED const cast */
                _toupper_tab_ = (const short *)
-                   &(_CurrentRuneLocale->rl_tabs->toupper_tab);
+                   &(locale->lc_ctype->rl_tabs->toupper_tab);
                /* LINTED const cast */
                _tolower_tab_ = (const short *)
-                   &(_CurrentRuneLocale->rl_tabs->tolower_tab);
+                   &(locale->lc_ctype->rl_tabs->tolower_tab);
        } else {
                _ctype_ = _C_ctype_;
                _toupper_tab_ = _C_toupper_;
Index: b/lib/libc/locale/setrunelocale.c
===================================================================
--- a/lib/libc/locale/setrunelocale.c   2015-07-08 09:15:00.593677755 +0200
+++ b/lib/libc/locale/setrunelocale.c   2015-07-08 11:38:51.361675324 +0200
@@ -102,6 +102,7 @@
 #include "citrus_ctype.h"
 #include "rune.h"
 #include "rune_local.h"
+#include "xlocale_private.h"
 
 struct localetable {
        char path[PATH_MAX];
@@ -171,7 +172,7 @@
 }
 
 int
-_xpg4_setrunelocale(const char *locname)
+_xpg4_setrunelocale(const char *locname, locale_t locale)
 {
        char path[PATH_MAX];
        _RuneLocale *rl;
@@ -205,7 +206,7 @@
                return ENOENT;
 
 found:
-       _CurrentRuneLocale = rl;
+       locale->lc_ctype = rl;
        __mb_cur_max = rl->rl_citrus_ctype->cc_mb_cur_max;
 
        return 0;
Index: b/lib/libc/locale/rune_local.h
===================================================================
--- a/lib/libc/locale/rune_local.h      2015-07-08 09:15:00.593677755 +0200
+++ b/lib/libc/locale/rune_local.h      2015-07-08 11:38:49.001660007 +0200
@@ -30,20 +30,22 @@
 #ifndef _RUNE_LOCAL_H_
 #define _RUNE_LOCAL_H_
 
+#include <locale.h>
+
 /* rune.c */
 extern _RuneLocale *_Read_RuneMagi(FILE *fp);
 extern void _NukeRune(_RuneLocale *);
 
 /* setrunelocale.c */
-extern int _xpg4_setrunelocale(const char *);
+extern int _xpg4_setrunelocale(const char *, locale_t);
 extern _RuneLocale *_findrunelocale(const char *);
 extern int _newrunelocale(const char *);
 
 /* runeglue.c */
 extern int __make_ctype_tabs(_RuneLocale *);
-extern void __install_currentrunelocale_ctype(void);
+extern void __install_currentrunelocale_ctype(locale_t);
 
 /* ___runetype_mb.c */
-extern _RuneType ___runetype_mb(wint_t);
+extern _RuneType ___runetype_mb(wint_t, locale_t);
 
 #endif
Index: b/lib/libc/locale/setlocale.c
===================================================================
--- a/lib/libc/locale/setlocale.c       2015-07-08 09:30:11.430429064 +0200
+++ b/lib/libc/locale/setlocale.c       2015-07-08 11:38:49.001660007 +0200
@@ -188,8 +188,8 @@
 {
        switch (category) {
        case LC_CTYPE:
-               (void)_xpg4_setrunelocale("C");
-               __install_currentrunelocale_ctype();
+               (void)_xpg4_setrunelocale("C", LC_GLOBAL_LOCALE);
+               __install_currentrunelocale_ctype(LC_GLOBAL_LOCALE);
                break;
        case LC_MESSAGES:
        case LC_COLLATE:
@@ -244,9 +244,9 @@
 
        switch (category) {
        case LC_CTYPE:
-               if (_xpg4_setrunelocale(locname))
+               if (_xpg4_setrunelocale(locname, LC_GLOBAL_LOCALE))
                        return -1;
-               __install_currentrunelocale_ctype();
+               __install_currentrunelocale_ctype(LC_GLOBAL_LOCALE);
                break;
 
        case LC_MESSAGES:

Reply via email to