| 1 | #ifndef _LOCALE_IMPL_H |
| 2 | #define _LOCALE_IMPL_H |
| 3 | |
| 4 | #include <locale.h> |
| 5 | #include <stdlib.h> |
| 6 | #include "libc.h" |
| 7 | #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) |
| 8 | #include "pthread_impl.h" |
| 9 | #endif |
| 10 | |
| 11 | #define LOCALE_NAME_MAX 23 |
| 12 | |
| 13 | struct __locale_map { |
| 14 | 	const void *map; |
| 15 | 	size_t map_size; |
| 16 | 	char name[LOCALE_NAME_MAX+1]; |
| 17 | 	const struct __locale_map *next; |
| 18 | }; |
| 19 | |
| 20 | #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) |
| 21 | extern hidden volatile int __locale_lock[1]; |
| 22 | #endif |
| 23 | |
| 24 | extern hidden const struct __locale_map __c_dot_utf8; |
| 25 | extern hidden const struct __locale_struct __c_locale; |
| 26 | extern hidden const struct __locale_struct __c_dot_utf8_locale; |
| 27 | |
| 28 | hidden const struct __locale_map *__get_locale(int, const char *); |
| 29 | hidden const char *__mo_lookup(const void *, size_t, const char *); |
| 30 | hidden const char *__lctrans(const char *, const struct __locale_map *); |
| 31 | #ifdef __wasilibc_unmodified_upstream |
| 32 | hidden const char *__lctrans_cur(const char *); |
| 33 | #else |
| 34 | // We make this visible in the wasi-libc build because |
| 35 | // libwasi-emulated-signal.so needs to import it from libc.so. If we ever |
| 36 | // decide to merge libwasi-emulated-signal.so into libc.so, this will no longer |
| 37 | // be necessary. |
| 38 | const char *__lctrans_cur(const char *); |
| 39 | #endif |
| 40 | hidden const char *__lctrans_impl(const char *, const struct __locale_map *); |
| 41 | hidden int __loc_is_allocated(locale_t); |
| 42 | hidden char *__gettextdomain(void); |
| 43 | |
| 44 | #define LOC_MAP_FAILED ((const struct __locale_map *)-1) |
| 45 | |
| 46 | #define LCTRANS(msg, lc, loc) __lctrans(msg, (loc)->cat[(lc)]) |
| 47 | #define LCTRANS_CUR(msg) __lctrans_cur(msg) |
| 48 | |
| 49 | #define C_LOCALE ((locale_t)&__c_locale) |
| 50 | #define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale) |
| 51 | |
| 52 | #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) |
| 53 | #define CURRENT_LOCALE (__pthread_self()->locale) |
| 54 | |
| 55 | #define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE]) |
| 56 | #else |
| 57 | // If we haven't set up the current_local field yet, do so. Then return an |
| 58 | // lvalue for the current_locale field. |
| 59 | #define CURRENT_LOCALE \ |
| 60 | (*({ \ |
| 61 | if (!libc.current_locale) { \ |
| 62 | libc.current_locale = &libc.global_locale; \ |
| 63 | } \ |
| 64 | &libc.current_locale; \ |
| 65 | })) |
| 66 | |
| 67 | #define CURRENT_UTF8 (!!CURRENT_LOCALE->cat[LC_CTYPE]) |
| 68 | #endif |
| 69 | |
| 70 | #undef MB_CUR_MAX |
| 71 | #define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1) |
| 72 | |
| 73 | #endif |