xref: /dflybsd-src/lib/libc/locale/xlocale_private.h (revision fef0a4be3c2d757f727c223fc6032f0898b816ad)
10d5acd74SJohn Marino /*-
20d5acd74SJohn Marino  * Copyright (c) 2011 The FreeBSD Foundation
30d5acd74SJohn Marino  * All rights reserved.
40d5acd74SJohn Marino  *
50d5acd74SJohn Marino  * This software was developed by David Chisnall under sponsorship from
60d5acd74SJohn Marino  * the FreeBSD Foundation.
70d5acd74SJohn Marino  *
80d5acd74SJohn Marino  * Redistribution and use in source and binary forms, with or without
90d5acd74SJohn Marino  * modification, are permitted provided that the following conditions
100d5acd74SJohn Marino  * are met:
110d5acd74SJohn Marino  * 1. Redistributions of source code must retain the above copyright
120d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer.
130d5acd74SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
140d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
150d5acd74SJohn Marino  *    documentation and/or other materials provided with the distribution.
160d5acd74SJohn Marino  *
170d5acd74SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
180d5acd74SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190d5acd74SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200d5acd74SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
210d5acd74SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
220d5acd74SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
230d5acd74SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
240d5acd74SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
250d5acd74SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
260d5acd74SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
270d5acd74SJohn Marino  * SUCH DAMAGE.
280d5acd74SJohn Marino  *
298720959fSzrj  * $FreeBSD: head/lib/libc/locale/xlocale_private.h 326193 2017-11-25 17:12:48Z pfg $
300d5acd74SJohn Marino  */
310d5acd74SJohn Marino 
32e3c51d6dSzrj #ifndef __LIBC
33e3c51d6dSzrj #error "Userland tools should not use this private header."
34e3c51d6dSzrj #endif
35e3c51d6dSzrj 
360d5acd74SJohn Marino #ifndef _XLOCALE_PRIVATE__H_
370d5acd74SJohn Marino #define _XLOCALE_PRIVATE__H_
380d5acd74SJohn Marino 
390d5acd74SJohn Marino #include <xlocale.h>
400d5acd74SJohn Marino #include <locale.h>
410d5acd74SJohn Marino #include <stdlib.h>
420d5acd74SJohn Marino #include <stdint.h>
430d5acd74SJohn Marino #include <sys/types.h>
440d5acd74SJohn Marino #include <machine/atomic.h>
45*3cfdabcfSzrj #include <machine/wchar.h>		/* for __mbstate_t, XXX use mbstate_t here? */
460d5acd74SJohn Marino #include "setlocale.h"
470d5acd74SJohn Marino 
488720959fSzrj /**
498720959fSzrj  * The XLC_ values are indexes into the components array.  They are defined in
508720959fSzrj  * the same order as the LC_ values in locale.h, but without the LC_ALL zero
518720959fSzrj  * value.  Translating from LC_X to XLC_X is done by subtracting one.
528720959fSzrj  *
538720959fSzrj  * Any reordering of this enum should ensure that these invariants are not
548720959fSzrj  * violated.
558720959fSzrj  */
560d5acd74SJohn Marino enum {
570d5acd74SJohn Marino 	XLC_COLLATE = 0,
580d5acd74SJohn Marino 	XLC_CTYPE,
590d5acd74SJohn Marino 	XLC_MONETARY,
600d5acd74SJohn Marino 	XLC_NUMERIC,
610d5acd74SJohn Marino 	XLC_TIME,
620d5acd74SJohn Marino 	XLC_MESSAGES,
630d5acd74SJohn Marino 	XLC_LAST
640d5acd74SJohn Marino };
650d5acd74SJohn Marino 
668720959fSzrj _Static_assert(XLC_LAST - XLC_COLLATE == 6, "XLC values should be contiguous");
678720959fSzrj _Static_assert(XLC_COLLATE == LC_COLLATE - 1,
688720959fSzrj                "XLC_COLLATE doesn't match the LC_COLLATE value.");
698720959fSzrj _Static_assert(XLC_CTYPE == LC_CTYPE - 1,
708720959fSzrj                "XLC_CTYPE doesn't match the LC_CTYPE value.");
718720959fSzrj _Static_assert(XLC_MONETARY == LC_MONETARY - 1,
728720959fSzrj                "XLC_MONETARY doesn't match the LC_MONETARY value.");
738720959fSzrj _Static_assert(XLC_NUMERIC == LC_NUMERIC - 1,
748720959fSzrj                "XLC_NUMERIC doesn't match the LC_NUMERIC value.");
758720959fSzrj _Static_assert(XLC_TIME == LC_TIME - 1,
768720959fSzrj                "XLC_TIME doesn't match the LC_TIME value.");
778720959fSzrj _Static_assert(XLC_MESSAGES == LC_MESSAGES - 1,
788720959fSzrj                "XLC_MESSAGES doesn't match the LC_MESSAGES value.");
790d5acd74SJohn Marino 
800d5acd74SJohn Marino /**
810d5acd74SJohn Marino  * Header used for objects that are reference counted.  Objects may optionally
820d5acd74SJohn Marino  * have a destructor associated, which is responsible for destroying the
830d5acd74SJohn Marino  * structure.  Global / static versions of the structure should have no
840d5acd74SJohn Marino  * destructor set - they can then have their reference counts manipulated as
850d5acd74SJohn Marino  * normal, but will not do anything with them.
860d5acd74SJohn Marino  *
870d5acd74SJohn Marino  * The header stores a retain count - objects are assumed to have a reference
880d5acd74SJohn Marino  * count of 1 when they are created, but the retain count is 0.  When the
890d5acd74SJohn Marino  * retain count is less than 0, they are freed.
900d5acd74SJohn Marino  */
910d5acd74SJohn Marino struct xlocale_refcounted {
920d5acd74SJohn Marino 	/** Number of references to this component.  */
930d5acd74SJohn Marino 	long retain_count;
940d5acd74SJohn Marino 	/** Function used to destroy this component, if one is required*/
950d5acd74SJohn Marino 	void(*destructor)(void*);
960d5acd74SJohn Marino };
970d5acd74SJohn Marino /**
980d5acd74SJohn Marino  * Header for a locale component.  All locale components must begin with this
990d5acd74SJohn Marino  * header.
1000d5acd74SJohn Marino  */
1010d5acd74SJohn Marino struct xlocale_component {
1020d5acd74SJohn Marino 	struct xlocale_refcounted header;
1030d5acd74SJohn Marino 	/** Name of the locale used for this component. */
1040d5acd74SJohn Marino 	char locale[ENCODING_LEN+1];
1050d5acd74SJohn Marino };
1060d5acd74SJohn Marino 
1070d5acd74SJohn Marino /**
1080d5acd74SJohn Marino  * xlocale structure, stores per-thread locale information.
1090d5acd74SJohn Marino  */
1100d5acd74SJohn Marino struct _xlocale {
1110d5acd74SJohn Marino 	struct xlocale_refcounted header;
1120d5acd74SJohn Marino 	/** Components for the locale.  */
1130d5acd74SJohn Marino 	struct xlocale_component *components[XLC_LAST];
1140d5acd74SJohn Marino 	/** Flag indicating if components[XLC_MONETARY] has changed since the
1150d5acd74SJohn Marino 	 * last call to localeconv_l() with this locale. */
1160d5acd74SJohn Marino 	int monetary_locale_changed;
1170d5acd74SJohn Marino 	/** Flag indicating whether this locale is actually using a locale for
1180d5acd74SJohn Marino 	 * LC_MONETARY (1), or if it should use the C default instead (0). */
1190d5acd74SJohn Marino 	int using_monetary_locale;
1200d5acd74SJohn Marino 	/** Flag indicating if components[XLC_NUMERIC] has changed since the
1210d5acd74SJohn Marino 	 * last call to localeconv_l() with this locale. */
1220d5acd74SJohn Marino 	int numeric_locale_changed;
1230d5acd74SJohn Marino 	/** Flag indicating whether this locale is actually using a locale for
1240d5acd74SJohn Marino 	 * LC_NUMERIC (1), or if it should use the C default instead (0). */
1250d5acd74SJohn Marino 	int using_numeric_locale;
1260d5acd74SJohn Marino 	/** Flag indicating whether this locale is actually using a locale for
1270d5acd74SJohn Marino 	 * LC_TIME (1), or if it should use the C default instead (0). */
1280d5acd74SJohn Marino 	int using_time_locale;
1290d5acd74SJohn Marino 	/** Flag indicating whether this locale is actually using a locale for
1300d5acd74SJohn Marino 	 * LC_MESSAGES (1), or if it should use the C default instead (0). */
1310d5acd74SJohn Marino 	int using_messages_locale;
1320d5acd74SJohn Marino 	/** The structure to be returned from localeconv_l() for this locale. */
1330d5acd74SJohn Marino 	struct lconv lconv;
1340d5acd74SJohn Marino 	/** Persistent state used by mblen() calls. */
1350d5acd74SJohn Marino 	__mbstate_t mblen;
1360d5acd74SJohn Marino 	/** Persistent state used by mbrlen() calls. */
1370d5acd74SJohn Marino 	__mbstate_t mbrlen;
1380d5acd74SJohn Marino 	/** Persistent state used by mbrtoc16() calls. */
1390d5acd74SJohn Marino 	__mbstate_t mbrtoc16;
1400d5acd74SJohn Marino 	/** Persistent state used by mbrtoc32() calls. */
1410d5acd74SJohn Marino 	__mbstate_t mbrtoc32;
1420d5acd74SJohn Marino 	/** Persistent state used by mbrtowc() calls. */
1430d5acd74SJohn Marino 	__mbstate_t mbrtowc;
1440d5acd74SJohn Marino 	/** Persistent state used by mbsnrtowcs() calls. */
1450d5acd74SJohn Marino 	__mbstate_t mbsnrtowcs;
1460d5acd74SJohn Marino 	/** Persistent state used by mbsrtowcs() calls. */
1470d5acd74SJohn Marino 	__mbstate_t mbsrtowcs;
1480d5acd74SJohn Marino 	/** Persistent state used by mbtowc() calls. */
1490d5acd74SJohn Marino 	__mbstate_t mbtowc;
1500d5acd74SJohn Marino 	/** Persistent state used by c16rtomb() calls. */
1510d5acd74SJohn Marino 	__mbstate_t c16rtomb;
1520d5acd74SJohn Marino 	/** Persistent state used by c32rtomb() calls. */
1530d5acd74SJohn Marino 	__mbstate_t c32rtomb;
1540d5acd74SJohn Marino 	/** Persistent state used by wcrtomb() calls. */
1550d5acd74SJohn Marino 	__mbstate_t wcrtomb;
1560d5acd74SJohn Marino 	/** Persistent state used by wcsnrtombs() calls. */
1570d5acd74SJohn Marino 	__mbstate_t wcsnrtombs;
1580d5acd74SJohn Marino 	/** Persistent state used by wcsrtombs() calls. */
1590d5acd74SJohn Marino 	__mbstate_t wcsrtombs;
1600d5acd74SJohn Marino 	/** Persistent state used by wctomb() calls. */
1610d5acd74SJohn Marino 	__mbstate_t wctomb;
1620d5acd74SJohn Marino 	/** Buffer used by nl_langinfo_l() */
1630d5acd74SJohn Marino 	char *csym;
1640d5acd74SJohn Marino };
1650d5acd74SJohn Marino 
1660d5acd74SJohn Marino /**
1670d5acd74SJohn Marino  * Increments the reference count of a reference-counted structure.
1680d5acd74SJohn Marino  */
1690d5acd74SJohn Marino __attribute__((unused)) static void*
xlocale_retain(void * val)1700d5acd74SJohn Marino xlocale_retain(void *val)
1710d5acd74SJohn Marino {
1720d5acd74SJohn Marino 	struct xlocale_refcounted *obj = val;
1730d5acd74SJohn Marino 	atomic_add_long(&(obj->retain_count), 1);
1740d5acd74SJohn Marino 	return (val);
1750d5acd74SJohn Marino }
1760d5acd74SJohn Marino /**
1770d5acd74SJohn Marino  * Decrements the reference count of a reference-counted structure, freeing it
1780d5acd74SJohn Marino  * if this is the last reference, calling its destructor if it has one.
1790d5acd74SJohn Marino  */
1800d5acd74SJohn Marino __attribute__((unused)) static void
xlocale_release(void * val)1810d5acd74SJohn Marino xlocale_release(void *val)
1820d5acd74SJohn Marino {
1830d5acd74SJohn Marino 	struct xlocale_refcounted *obj = val;
18444783347Szrj 	long count;
18544783347Szrj 
18644783347Szrj 	count = atomic_fetchadd_long(&(obj->retain_count), -1) - 1;
18744783347Szrj 	if (count < 0 && obj->destructor != NULL)
1880d5acd74SJohn Marino 		obj->destructor(obj);
1890d5acd74SJohn Marino }
1900d5acd74SJohn Marino 
1910d5acd74SJohn Marino /**
1920d5acd74SJohn Marino  * Load functions.  Each takes the name of a locale and a pointer to the data
1930d5acd74SJohn Marino  * to be initialised as arguments.  Two special values are allowed for the
1940d5acd74SJohn Marino  */
1950d5acd74SJohn Marino extern void* __collate_load(const char*, locale_t);
1960d5acd74SJohn Marino extern void* __ctype_load(const char*, locale_t);
1970d5acd74SJohn Marino extern void* __messages_load(const char*, locale_t);
1980d5acd74SJohn Marino extern void* __monetary_load(const char*, locale_t);
1990d5acd74SJohn Marino extern void* __numeric_load(const char*, locale_t);
2000d5acd74SJohn Marino extern void* __time_load(const char*, locale_t);
2010d5acd74SJohn Marino 
2020d5acd74SJohn Marino extern struct _xlocale __xlocale_global_locale;
2030d5acd74SJohn Marino extern struct _xlocale __xlocale_C_locale;
2040d5acd74SJohn Marino 
2050d5acd74SJohn Marino /**
2060d5acd74SJohn Marino  * Caches the rune table in TLS for fast access.
2070d5acd74SJohn Marino  */
2080d5acd74SJohn Marino void __set_thread_rune_locale(locale_t loc);
2090d5acd74SJohn Marino /**
2100d5acd74SJohn Marino  * Flag indicating whether a per-thread locale has been set.  If no per-thread
2110d5acd74SJohn Marino  * locale has ever been set, then we always use the global locale.
2120d5acd74SJohn Marino  */
2130d5acd74SJohn Marino extern int __has_thread_locale;
2140d5acd74SJohn Marino /**
2150d5acd74SJohn Marino  * The per-thread locale.  Avoids the need to use pthread lookup functions when
2160d5acd74SJohn Marino  * getting the per-thread locale.
2170d5acd74SJohn Marino  */
2180d5acd74SJohn Marino extern __thread locale_t __thread_locale;
2190d5acd74SJohn Marino 
2200d5acd74SJohn Marino /**
2210d5acd74SJohn Marino  * Returns the current locale for this thread, or the global locale if none is
2220d5acd74SJohn Marino  * set.  The caller does not have to free the locale.  The return value from
2230d5acd74SJohn Marino  * this call is not guaranteed to remain valid after the locale changes.  As
2240d5acd74SJohn Marino  * such, this should only be called within libc functions.
2250d5acd74SJohn Marino  */
__get_locale(void)2260d5acd74SJohn Marino static inline locale_t __get_locale(void)
2270d5acd74SJohn Marino {
2280d5acd74SJohn Marino 
2290d5acd74SJohn Marino 	if (!__has_thread_locale) {
2300d5acd74SJohn Marino 		return (&__xlocale_global_locale);
2310d5acd74SJohn Marino 	}
2320d5acd74SJohn Marino 	return (__thread_locale ? __thread_locale : &__xlocale_global_locale);
2330d5acd74SJohn Marino }
2340d5acd74SJohn Marino 
2350d5acd74SJohn Marino /**
2360d5acd74SJohn Marino  * Two magic values are allowed for locale_t objects.  NULL and -1.  This
2370d5acd74SJohn Marino  * function maps those to the real locales that they represent.
2380d5acd74SJohn Marino  */
get_real_locale(locale_t locale)2390d5acd74SJohn Marino static inline locale_t get_real_locale(locale_t locale)
2400d5acd74SJohn Marino {
2410d5acd74SJohn Marino 	switch ((intptr_t)locale) {
2420d5acd74SJohn Marino 		case 0: return (&__xlocale_C_locale);
2430d5acd74SJohn Marino 		case -1: return (&__xlocale_global_locale);
2440d5acd74SJohn Marino 		default: return (locale);
2450d5acd74SJohn Marino 	}
2460d5acd74SJohn Marino }
2470d5acd74SJohn Marino 
2480d5acd74SJohn Marino /**
2490d5acd74SJohn Marino  * Replace a placeholder locale with the real global or thread-local locale_t.
2500d5acd74SJohn Marino  */
2510d5acd74SJohn Marino #define FIX_LOCALE(l) (l = get_real_locale(l))
2520d5acd74SJohn Marino 
2530d5acd74SJohn Marino #endif
254