9 #include "locale_impl.h"
14 #define malloc __libc_malloc
15 #define calloc __libc_calloc
28 static void *volatile bindings;
30 static char *gettextdir(const char *domainname, size_t *dirlen)
33 for (p=bindings; p; p=p->next) {
34 if (!strcmp(p->domainname, domainname) && p->active) {
36 return (char *)p->dirname;
42 char *bindtextdomain(const char *domainname, const char *dirname)
44 static volatile int lock[1];
45 struct binding *p, *q;
47 if (!domainname) return 0;
48 if (!dirname) return gettextdir(domainname, &(size_t){0});
50 size_t domlen = strnlen(domainname, NAME_MAX+1);
51 size_t dirlen = strnlen(dirname, PATH_MAX);
52 if (domlen > NAME_MAX || dirlen >= PATH_MAX) {
59 for (p=bindings; p; p=p->next) {
60 if (!strcmp(p->domainname, domainname) &&
61 !strcmp(p->dirname, dirname)) {
67 p = calloc(sizeof *p + domlen + dirlen + 2, 1);
74 p->domainname = p->buf;
75 p->dirname = p->buf + domlen + 1;
76 memcpy(p->domainname, domainname, domlen+1);
77 memcpy(p->dirname, dirname, dirlen+1);
78 a_cas_p(&bindings, bindings, p);
81 a_store(&p->active, 1);
83 for (q=bindings; q; q=q->next) {
84 if (!strcmp(q->domainname, domainname) && q != p)
85 a_store(&q->active, 0);
90 return (char *)p->dirname;
93 static const char catnames[][12] = {
102 static const char catlens[] = { 8, 10, 7, 10, 11, 11 };
108 const char *plural_rule;
110 struct binding *binding;
111 const struct __locale_map *lm;
115 static char *dummy_gettextdomain()
120 weak_alias(dummy_gettextdomain, __gettextdomain);
122 char *dcngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n, int category)
124 static struct msgcat *volatile cats;
126 struct __locale_struct *loc = CURRENT_LOCALE;
127 const struct __locale_map *lm;
130 int old_errno = errno;
132 if ((unsigned)category >= LC_ALL) goto notrans;
134 if (!domainname) domainname = __gettextdomain();
136 domlen = strnlen(domainname, NAME_MAX+1);
137 if (domlen > NAME_MAX) goto notrans;
139 for (q=bindings; q; q=q->next)
140 if (!strcmp(q->domainname, domainname) && q->active)
142 if (!q) goto notrans;
144 lm = loc->cat[category];
148 return (char *) ((n == 1) ? msgid1 : msgid2);
151 for (p=cats; p; p=p->next)
152 if (p->binding == q && p->lm == lm && p->cat == category)
156 const char *dirname, *locname, *catname, *modname, *locp;
157 size_t dirlen, loclen, catlen, modlen, alt_modlen;
161 dirname = q->dirname;
163 catname = catnames[category];
166 loclen = strlen(locname);
167 catlen = catlens[category];
169 /* Logically split @mod suffix from locale name. */
170 modname = memchr(locname, '@', loclen);
171 if (!modname) modname = locname + loclen;
172 alt_modlen = modlen = loclen - (modname-locname);
173 loclen = modname-locname;
175 /* Drop .charset identifier; it is not used. */
176 const char *csp = memchr(locname, '.', loclen);
177 if (csp) loclen = csp-locname;
179 char name[dirlen+1 + loclen+modlen+1 + catlen+1 + domlen+3 + 1];
183 snprintf(name, sizeof name, "%s/%.*s%.*s/%s/%s.mo\0",
184 dirname, (int)loclen, locname,
185 (int)alt_modlen, modname, catname, domainname);
186 if (map = __map_file(name, &map_size)) break;
188 /* Try dropping @mod, _YY, then both. */
191 } else if ((locp = memchr(locname, '_', loclen))) {
192 loclen = locp-locname;
198 if (!map) goto notrans;
200 p = calloc(sizeof *p, 1);
202 __munmap((void *)map, map_size);
209 p->map_size = map_size;
211 const char *rule = "n!=1;";
212 unsigned long np = 2;
213 const char *r = __mo_lookup(p->map, p->map_size, "");
215 while (r && strncmp(r, "Plural-Forms:", 13)) {
221 while (isspace(*r)) r++;
222 if (!strncmp(r, "nplurals=", 9)) {
223 np = strtoul(r+9, &z, 10);
226 while (*r && *r != ';') r++;
229 while (isspace(*r)) r++;
230 if (!strncmp(r, "plural=", 7))
235 p->plural_rule = rule;
240 } while (a_cas_p(&cats, old_cats, p) != old_cats);
243 const char *trans = __mo_lookup(p->map, p->map_size, msgid1);
244 if (!trans) goto notrans;
246 /* Non-plural-processing gettext forms pass a null pointer as
247 * msgid2 to request that dcngettext suppress plural processing. */
249 if (msgid2 && p->nplurals) {
250 unsigned long plural = __pleval(p->plural_rule, n);
251 if (plural > p->nplurals) goto notrans;
253 size_t rem = p->map_size - (trans - (char *)p->map);
254 size_t l = strnlen(trans, rem);
261 return (char *)trans;
264 char *dcgettext(const char *domainname, const char *msgid, int category)
266 return dcngettext(domainname, msgid, 0, 1, category);
269 char *dngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n)
271 return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES);
274 char *dgettext(const char *domainname, const char *msgid)
276 return dcngettext(domainname, msgid, 0, 1, LC_MESSAGES);