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