• 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", "en_US.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 			if (strcmp(val, "en_US.UTF-8") == 0) {
105 				new->flag = ICU_VALID;
106 			} else {
107 				new->flag = VALID;
108 			}
109 			loc_head = new;
110 		}
111 	}
112 
113 	if (!libc.secure) path = getenv("MUSL_LOCPATH");
114 	/* FIXME: add a default path? */
115 
116 	if (path) for (; *path; path=z+!!*z) {
117 		z = __strchrnul(path, ':');
118 		l = z - path;
119 		if (l >= sizeof buf - n - 2) continue;
120 		memcpy(buf, path, l);
121 		buf[l] = '/';
122 		memcpy(buf+l+1, val, n);
123 		buf[l+1+n] = 0;
124 		size_t map_size;
125 		const void *map = __map_file(buf, &map_size);
126 		if (map) {
127 			new = malloc(sizeof *new);
128 			if (!new) {
129 				__munmap((void *)map, map_size);
130 				break;
131 			}
132 			new->map = map;
133 			new->map_size = map_size;
134 			memcpy(new->name, val, n);
135 			new->name[n] = 0;
136 			new->next = loc_head;
137 			new->flag = VALID;
138 			loc_head = new;
139 			break;
140 		}
141 	}
142 
143 	/* If no locale definition was found, make a locale map
144 	 * object anyway to store the name, which is kept for the
145 	 * sake of being able to do message translations at the
146 	 * application level. */
147 	if (!new && (new = malloc(sizeof *new))) {
148 		new->map = __c_dot_utf8.map;
149 		new->map_size = __c_dot_utf8.map_size;
150 		memcpy(new->name, val, n);
151 		new->name[n] = 0;
152 		new->next = loc_head;
153 		new->flag = INVALID;
154 		loc_head = new;
155 	}
156 
157 #ifdef FEATURE_ICU_LOCALE
158 	/* For locales with no definition, checks whether this locale setting
159 	 * is valid for icu. We use a table to record all the valid icu locale
160 	 * settings. */
161 	if (new->flag == INVALID) {
162 		for (int i = 0; i < g_locale_table_size; i++) {
163 			if (strcmp(g_valid_locale_table[i], val) == 0) {
164 				new->flag = ICU_VALID;
165 				break;
166 			}
167 		}
168 	}
169     /* Use ICU_VALID flag to indicate that other icu-related functions can use icu methods */
170 	if (new->flag == ICU_VALID) {
171 		/* ICU function: u_setDataDirectory, Load ICU data to memory */
172 		set_icu_directory();
173 	}
174 #endif
175 
176 	/* For LC_CTYPE, never return a null pointer unless the
177 	 * requested name was "C" or "POSIX". */
178 	if (!new && cat == LC_CTYPE) new = (void *)&__c_dot_utf8;
179 
180 	return new;
181 }
182