1 #include <libintl.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <limits.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
8 #include <ctype.h>
9 #include "locale_impl.h"
10 #include "atomic.h"
11 #include "pleval.h"
12 #include "lock.h"
13 #include "fork_impl.h"
14
15 #define malloc __libc_malloc
16 #define calloc __libc_calloc
17 #define realloc undef
18 #define free undef
19
20 struct binding {
21 struct binding *next;
22 int dirlen;
23 volatile int active;
24 char *domainname;
25 char *dirname;
26 char buf[];
27 };
28
29 static void *volatile bindings;
30
gettextdir(const char * domainname,size_t * dirlen)31 static char *gettextdir(const char *domainname, size_t *dirlen)
32 {
33 struct binding *p;
34 for (p=bindings; p; p=p->next) {
35 if (!strcmp(p->domainname, domainname) && p->active) {
36 *dirlen = p->dirlen;
37 return (char *)p->dirname;
38 }
39 }
40 return 0;
41 }
42
43 static volatile int lock[1];
44 volatile int *const __gettext_lockptr = lock;
45
bindtextdomain(const char * domainname,const char * dirname)46 char *bindtextdomain(const char *domainname, const char *dirname)
47 {
48 struct binding *p, *q;
49
50 if (!domainname) return 0;
51 if (!dirname) return gettextdir(domainname, &(size_t){0});
52
53 size_t domlen = strnlen(domainname, NAME_MAX+1);
54 size_t dirlen = strnlen(dirname, PATH_MAX);
55 if (domlen > NAME_MAX || dirlen >= PATH_MAX) {
56 errno = EINVAL;
57 return 0;
58 }
59
60 LOCK(lock);
61
62 for (p=bindings; p; p=p->next) {
63 if (!strcmp(p->domainname, domainname) &&
64 !strcmp(p->dirname, dirname)) {
65 break;
66 }
67 }
68
69 if (!p) {
70 p = calloc(sizeof *p + domlen + dirlen + 2, 1);
71 if (!p) {
72 UNLOCK(lock);
73 return 0;
74 }
75 p->next = bindings;
76 p->dirlen = dirlen;
77 p->domainname = p->buf;
78 p->dirname = p->buf + domlen + 1;
79 memcpy(p->domainname, domainname, domlen+1);
80 memcpy(p->dirname, dirname, dirlen+1);
81 a_cas_p(&bindings, bindings, p);
82 }
83
84 a_store(&p->active, 1);
85
86 for (q=bindings; q; q=q->next) {
87 if (!strcmp(q->domainname, domainname) && q != p)
88 a_store(&q->active, 0);
89 }
90
91 UNLOCK(lock);
92
93 return (char *)p->dirname;
94 }
95
96 #ifndef __LITEOS__
97 static const char catnames[][18] = {
98 "LC_CTYPE",
99 "LC_NUMERIC",
100 "LC_TIME",
101 "LC_COLLATE",
102 "LC_MONETARY",
103 "LC_MESSAGES",
104 "LC_PAPER",
105 "LC_NAME",
106 "LC_ADDRESS",
107 "LC_TELEPHONE",
108 "LC_MEASUREMENT",
109 "LC_IDENTIFICATION",
110 };
111
112 static const char catlens[] = { 8, 10, 7, 10, 11, 11, 8, 7, 10, 12, 14, 17 };
113 #else
114 static const char catnames[][12] = {
115 "LC_CTYPE",
116 "LC_NUMERIC",
117 "LC_TIME",
118 "LC_COLLATE",
119 "LC_MONETARY",
120 "LC_MESSAGES",
121 };
122
123 static const char catlens[] = { 8, 10, 7, 10, 11, 11 };
124 #endif
125
126 struct msgcat {
127 struct msgcat *next;
128 const void *map;
129 size_t map_size;
130 const char *plural_rule;
131 int nplurals;
132 struct binding *binding;
133 const struct __locale_map *lm;
134 int cat;
135 };
136
dummy_gettextdomain()137 static char *dummy_gettextdomain()
138 {
139 return "messages";
140 }
141
142 weak_alias(dummy_gettextdomain, __gettextdomain);
143
dcngettext(const char * domainname,const char * msgid1,const char * msgid2,unsigned long int n,int category)144 char *dcngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n, int category)
145 {
146 static struct msgcat *volatile cats;
147 struct msgcat *p;
148 struct __locale_struct *loc = CURRENT_LOCALE;
149 const struct __locale_map *lm;
150 size_t domlen;
151 struct binding *q;
152 int old_errno = errno;
153
154 /* match gnu gettext behaviour */
155 if (!msgid1) goto notrans;
156
157 if ((unsigned)category >= LC_ALL) goto notrans;
158
159 if (!domainname) domainname = __gettextdomain();
160
161 domlen = strnlen(domainname, NAME_MAX+1);
162 if (domlen > NAME_MAX) goto notrans;
163
164 for (q=bindings; q; q=q->next)
165 if (!strcmp(q->domainname, domainname) && q->active)
166 break;
167 if (!q) goto notrans;
168
169 lm = loc->cat[category];
170 if (!lm) {
171 notrans:
172 errno = old_errno;
173 return (char *) ((n == 1) ? msgid1 : msgid2);
174 }
175
176 for (p=cats; p; p=p->next)
177 if (p->binding == q && p->lm == lm && p->cat == category)
178 break;
179
180 if (!p) {
181 const char *dirname, *locname, *catname, *modname, *locp;
182 size_t dirlen, loclen, catlen, modlen, alt_modlen;
183 void *old_cats;
184 size_t map_size;
185
186 dirname = q->dirname;
187 locname = lm->name;
188 catname = catnames[category];
189
190 dirlen = q->dirlen;
191 loclen = strlen(locname);
192 catlen = catlens[category];
193
194 /* Logically split @mod suffix from locale name. */
195 modname = memchr(locname, '@', loclen);
196 if (!modname) modname = locname + loclen;
197 alt_modlen = modlen = loclen - (modname-locname);
198 loclen = modname-locname;
199
200 /* Drop .charset identifier; it is not used. */
201 const char *csp = memchr(locname, '.', loclen);
202 if (csp) loclen = csp-locname;
203
204 char name[dirlen+1 + loclen+modlen+1 + catlen+1 + domlen+3 + 1];
205 const void *map;
206
207 for (;;) {
208 snprintf(name, sizeof name, "%s/%.*s%.*s/%s/%s.mo\0",
209 dirname, (int)loclen, locname,
210 (int)alt_modlen, modname, catname, domainname);
211 if (map = __map_file(name, &map_size)) break;
212
213 /* Try dropping @mod, _YY, then both. */
214 if (alt_modlen) {
215 alt_modlen = 0;
216 } else if ((locp = memchr(locname, '_', loclen))) {
217 loclen = locp-locname;
218 alt_modlen = modlen;
219 } else {
220 break;
221 }
222 }
223 if (!map) goto notrans;
224
225 p = calloc(sizeof *p, 1);
226 if (!p) {
227 __munmap((void *)map, map_size);
228 goto notrans;
229 }
230 p->cat = category;
231 p->binding = q;
232 p->lm = lm;
233 p->map = map;
234 p->map_size = map_size;
235
236 const char *rule = "n!=1;";
237 unsigned long np = 2;
238 const char *r = __mo_lookup(p->map, p->map_size, "");
239 char *z;
240 while (r && strncmp(r, "Plural-Forms:", 13)) {
241 z = strchr(r, '\n');
242 r = z ? z+1 : 0;
243 }
244 if (r) {
245 r += 13;
246 while (isspace(*r)) r++;
247 if (!strncmp(r, "nplurals=", 9)) {
248 np = strtoul(r+9, &z, 10);
249 r = z;
250 }
251 while (*r && *r != ';') r++;
252 if (*r) {
253 r++;
254 while (isspace(*r)) r++;
255 if (!strncmp(r, "plural=", 7))
256 rule = r+7;
257 }
258 }
259 p->nplurals = np;
260 p->plural_rule = rule;
261
262 do {
263 old_cats = cats;
264 p->next = old_cats;
265 } while (a_cas_p(&cats, old_cats, p) != old_cats);
266 }
267
268 const char *trans = __mo_lookup(p->map, p->map_size, msgid1);
269 if (!trans) goto notrans;
270
271 /* Non-plural-processing gettext forms pass a null pointer as
272 * msgid2 to request that dcngettext suppress plural processing. */
273
274 if (msgid2 && p->nplurals) {
275 unsigned long plural = __pleval(p->plural_rule, n);
276 if (plural > p->nplurals) goto notrans;
277 while (plural--) {
278 size_t rem = p->map_size - (trans - (char *)p->map);
279 size_t l = strnlen(trans, rem);
280 if (l+1 >= rem)
281 goto notrans;
282 trans += l+1;
283 }
284 }
285 errno = old_errno;
286 return (char *)trans;
287 }
288
dcgettext(const char * domainname,const char * msgid,int category)289 char *dcgettext(const char *domainname, const char *msgid, int category)
290 {
291 return dcngettext(domainname, msgid, 0, 1, category);
292 }
293
dngettext(const char * domainname,const char * msgid1,const char * msgid2,unsigned long int n)294 char *dngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n)
295 {
296 return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES);
297 }
298
dgettext(const char * domainname,const char * msgid)299 char *dgettext(const char *domainname, const char *msgid)
300 {
301 return dcngettext(domainname, msgid, 0, 1, LC_MESSAGES);
302 }
303