• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <locale.h>
2 #include <string.h>
3 #include <sys/mman.h>
4 #include <stdlib.h>
5 #include "locale_impl.h"
6 #include "libc.h"
7 #include "lock.h"
8 #include "fork_impl.h"
9 
10 #define malloc __libc_malloc
11 #define calloc undef
12 #define realloc undef
13 #define free undef
14 
__lctrans_impl(const char * msg,const struct __locale_map * lm)15 const char *__lctrans_impl(const char *msg, const struct __locale_map *lm)
16 {
17 	const char *trans = 0;
18 	if (lm) trans = __mo_lookup(lm->map, lm->map_size, msg);
19 	return trans ? trans : msg;
20 }
21 
22 #ifndef __LITEOS__
23 static const char envvars[][18] = {
24 	"LC_CTYPE",
25 	"LC_NUMERIC",
26 	"LC_TIME",
27 	"LC_COLLATE",
28 	"LC_MONETARY",
29 	"LC_MESSAGES",
30 	"LC_PAPER",
31 	"LC_NAME",
32 	"LC_ADDRESS",
33 	"LC_TELEPHONE",
34 	"LC_MEASUREMENT",
35 	"LC_IDENTIFICATION",
36 };
37 #else
38 static const char envvars[][12] = {
39 	"LC_CTYPE",
40 	"LC_NUMERIC",
41 	"LC_TIME",
42 	"LC_COLLATE",
43 	"LC_MONETARY",
44 	"LC_MESSAGES",
45 };
46 #endif
47 
48 volatile int __locale_lock[1];
49 volatile int *const __locale_lockptr = __locale_lock;
50 
51 #ifdef FEATURE_ICU_LOCALE
52 static const char *g_valid_locale_table[] = {"zh_CN", "zh_CN.UTF-8"};
53 static int g_locale_table_size = sizeof(g_valid_locale_table) / sizeof(g_valid_locale_table[0]);
54 #endif
55 
__get_locale(int cat,const char * val)56 const struct __locale_map *__get_locale(int cat, const char *val)
57 {
58 	static void *volatile loc_head;
59 	const struct __locale_map *p;
60 	struct __locale_map *new = 0;
61 	const char *path = 0, *z;
62 	char buf[256];
63 	size_t l, n;
64 
65 	if (!*val) {
66 		(val = getenv("LC_ALL")) && *val ||
67 		(val = getenv(envvars[cat])) && *val ||
68 		(val = getenv("LANG")) && *val ||
69 		(val = "C.UTF-8");
70 	}
71 
72 	/* Limit name length and forbid leading dot or any slashes. */
73 	for (n=0; n<LOCALE_NAME_MAX && val[n] && val[n]!='/'; n++);
74 	if (val[0]=='.' || val[n]) val = "C.UTF-8";
75 	int builtin = (val[0]=='C' && !val[1])
76 #ifndef __LITEOS__
77 		|| !strcmp(val, "POSIX")
78 		|| !strcmp(val, "en_US");
79 #else
80 		|| !strcmp(val, "C.UTF-8")
81 		|| !strcmp(val, "POSIX");
82 #endif
83 
84 	if (builtin) {
85 		if (cat == LC_CTYPE && val[1]=='.')
86 			return (void *)&__c_dot_utf8;
87 		return 0;
88 	}
89 
90 	for (p=loc_head; p; p=p->next)
91 		if (!strcmp(val, p->name)) return p;
92 
93 	if (!strcmp(val, "en_US.UTF-8") || !strcmp(val, "C.UTF-8")) {
94 		/* If no locale definition was found, make a locale map
95 		* object anyway to store the name, which is kept for the
96 		* sake of being able to do message translations at the
97 		* application level. */
98 		if (!new && (new = malloc(sizeof *new))) {
99 			new->map = __c_dot_utf8.map;
100 			new->map_size = __c_dot_utf8.map_size;
101 			memcpy(new->name, val, n);
102 			new->name[n] = 0;
103 			new->next = loc_head;
104 			new->flag = VALID;
105 			loc_head = new;
106 		}
107 	}
108 
109 	if (!libc.secure) path = getenv("MUSL_LOCPATH");
110 	/* FIXME: add a default path? */
111 
112 	if (path) for (; *path; path=z+!!*z) {
113 		z = __strchrnul(path, ':');
114 		l = z - path;
115 		if (l >= sizeof buf - n - 2) continue;
116 		memcpy(buf, path, l);
117 		buf[l] = '/';
118 		memcpy(buf+l+1, val, n);
119 		buf[l+1+n] = 0;
120 		size_t map_size;
121 		const void *map = __map_file(buf, &map_size);
122 		if (map) {
123 			new = malloc(sizeof *new);
124 			if (!new) {
125 				__munmap((void *)map, map_size);
126 				break;
127 			}
128 			new->map = map;
129 			new->map_size = map_size;
130 			memcpy(new->name, val, n);
131 			new->name[n] = 0;
132 			new->next = loc_head;
133 			new->flag = VALID;
134 			loc_head = new;
135 			break;
136 		}
137 	}
138 
139 	/* If no locale definition was found, make a locale map
140 	 * object anyway to store the name, which is kept for the
141 	 * sake of being able to do message translations at the
142 	 * application level. */
143 	if (!new && (new = malloc(sizeof *new))) {
144 		new->map = __c_dot_utf8.map;
145 		new->map_size = __c_dot_utf8.map_size;
146 		memcpy(new->name, val, n);
147 		new->name[n] = 0;
148 		new->next = loc_head;
149 		new->flag = INVALID;
150 		loc_head = new;
151 	}
152 
153 #ifdef FEATURE_ICU_LOCALE
154 	/* For locales with no definition, checks whether this locale setting
155 	 * is valid for icu. We use a table to record all the valid icu locale
156 	 * settings. */
157 	if (new->flag == INVALID) {
158 		for (int i = 0; i < g_locale_table_size; i++) {
159 			if (strcmp(g_valid_locale_table[i], val) == 0) {
160 				new->flag = ICU_VALID;
161 				break;
162 			}
163 		}
164 		/* Use ICU_VALID flag to indicate that other icu-related functions can use icu methods */
165 		if (new->flag == ICU_VALID) {
166 			/* ICU function: u_setDataDirectory, Load ICU data to memory */
167 			set_icu_directory();
168 		}
169 	}
170 #endif
171 
172 	/* For LC_CTYPE, never return a null pointer unless the
173 	 * requested name was "C" or "POSIX". */
174 	if (!new && cat == LC_CTYPE) new = (void *)&__c_dot_utf8;
175 
176 	return new;
177 }
178