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