9 #include "locale_impl.h"
23 static void *volatile bindings;
25 static char *gettextdir(const char *domainname, size_t *dirlen)
28 for (p=bindings; p; p=p->next) {
29 if (!strcmp(p->domainname, domainname) && p->active) {
31 return (char *)p->dirname;
37 char *bindtextdomain(const char *domainname, const char *dirname)
39 static volatile int lock[1];
40 struct binding *p, *q;
42 if (!domainname) return 0;
43 if (!dirname) return gettextdir(domainname, &(size_t){0});
45 size_t domlen = strnlen(domainname, NAME_MAX+1);
46 size_t dirlen = strnlen(dirname, PATH_MAX);
47 if (domlen > NAME_MAX || dirlen >= PATH_MAX) {
54 for (p=bindings; p; p=p->next) {
55 if (!strcmp(p->domainname, domainname) &&
56 !strcmp(p->dirname, dirname)) {
62 p = calloc(sizeof *p + domlen + dirlen + 2, 1);
69 p->domainname = p->buf;
70 p->dirname = p->buf + domlen + 1;
71 memcpy(p->domainname, domainname, domlen+1);
72 memcpy(p->dirname, dirname, dirlen+1);
73 a_cas_p(&bindings, bindings, p);
76 a_store(&p->active, 1);
78 for (q=bindings; q; q=q->next) {
79 if (!strcmp(q->domainname, domainname) && q != p)
80 a_store(&q->active, 0);
85 return (char *)p->dirname;
88 static const char catnames[][12] = {
97 static const char catlens[] = { 8, 10, 7, 10, 11, 11 };
103 const char *plural_rule;
105 struct binding *binding;
106 const struct __locale_map *lm;
110 static char *dummy_gettextdomain()
115 weak_alias(dummy_gettextdomain, __gettextdomain);
117 char *dcngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n, int category)
119 static struct msgcat *volatile cats;
121 struct __locale_struct *loc = CURRENT_LOCALE;
122 const struct __locale_map *lm;
125 int old_errno = errno;
127 if ((unsigned)category >= LC_ALL) goto notrans;
129 if (!domainname) domainname = __gettextdomain();
131 domlen = strnlen(domainname, NAME_MAX+1);
132 if (domlen > NAME_MAX) goto notrans;
134 for (q=bindings; q; q=q->next)
135 if (!strcmp(q->domainname, domainname) && q->active)
137 if (!q) goto notrans;
139 lm = loc->cat[category];
143 return (char *) ((n == 1) ? msgid1 : msgid2);
146 for (p=cats; p; p=p->next)
147 if (p->binding == q && p->lm == lm && p->cat == category)
151 const char *dirname, *locname, *catname, *modname, *locp;
152 size_t dirlen, loclen, catlen, modlen, alt_modlen;
156 dirname = q->dirname;
158 catname = catnames[category];
161 loclen = strlen(locname);
162 catlen = catlens[category];
164 /* Logically split @mod suffix from locale name. */
165 modname = memchr(locname, '@', loclen);
166 if (!modname) modname = locname + loclen;
167 alt_modlen = modlen = loclen - (modname-locname);
168 loclen = modname-locname;
170 /* Drop .charset identifier; it is not used. */
171 const char *csp = memchr(locname, '.', loclen);
172 if (csp) loclen = csp-locname;
174 char name[dirlen+1 + loclen+modlen+1 + catlen+1 + domlen+3 + 1];
178 snprintf(name, sizeof name, "%s/%.*s%.*s/%s/%s.mo\0",
179 dirname, (int)loclen, locname,
180 (int)alt_modlen, modname, catname, domainname);
181 if (map = __map_file(name, &map_size)) break;
183 /* Try dropping @mod, _YY, then both. */
186 } else if ((locp = memchr(locname, '_', loclen))) {
187 loclen = locp-locname;
193 if (!map) goto notrans;
195 p = calloc(sizeof *p, 1);
197 __munmap((void *)map, map_size);
204 p->map_size = map_size;
206 const char *rule = "n!=1;";
207 unsigned long np = 2;
208 const char *r = __mo_lookup(p->map, p->map_size, "");
210 while (r && strncmp(r, "Plural-Forms:", 13)) {
216 while (isspace(*r)) r++;
217 if (!strncmp(r, "nplurals=", 9)) {
218 np = strtoul(r+9, &z, 10);
221 while (*r && *r != ';') r++;
224 while (isspace(*r)) r++;
225 if (!strncmp(r, "plural=", 7))
230 p->plural_rule = rule;
235 } while (a_cas_p(&cats, old_cats, p) != old_cats);
238 const char *trans = __mo_lookup(p->map, p->map_size, msgid1);
239 if (!trans) goto notrans;
241 /* Non-plural-processing gettext forms pass a null pointer as
242 * msgid2 to request that dcngettext suppress plural processing. */
244 if (msgid2 && p->nplurals) {
245 unsigned long plural = __pleval(p->plural_rule, n);
246 if (plural > p->nplurals) goto notrans;
248 size_t rem = p->map_size - (trans - (char *)p->map);
249 size_t l = strnlen(trans, rem);
256 return (char *)trans;
259 char *dcgettext(const char *domainname, const char *msgid, int category)
261 return dcngettext(domainname, msgid, 0, 1, category);
264 char *dngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n)
266 return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES);
269 char *dgettext(const char *domainname, const char *msgid)
271 return dcngettext(domainname, msgid, 0, 1, LC_MESSAGES);