]>
Commit | Line | Data |
---|---|---|
abbffdf9 | 1 | /* Implementation of the internal dcigettext function. |
dff8da6b | 2 | Copyright (C) 1995-2024 Free Software Foundation, Inc. |
abbffdf9 | 3 | |
6d248857 WN |
4 | This program is free software: you can redistribute it and/or modify |
5 | it under the terms of the GNU Lesser General Public License as published by | |
6 | the Free Software Foundation; either version 2.1 of the License, or | |
7 | (at your option) any later version. | |
abbffdf9 | 8 | |
6d248857 | 9 | This program is distributed in the hope that it will be useful, |
abbffdf9 | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
6d248857 WN |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU Lesser General Public License for more details. | |
abbffdf9 | 13 | |
6d248857 | 14 | You should have received a copy of the GNU Lesser General Public License |
5a82c748 | 15 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
abbffdf9 | 16 | |
0a55a284 UD |
17 | /* Tell glibc's <string.h> to provide a prototype for mempcpy(). |
18 | This must come before <config.h> because <config.h> may include | |
19 | <features.h>, and once <features.h> has been included, it's too late. */ | |
20 | #ifndef _GNU_SOURCE | |
21 | # define _GNU_SOURCE 1 | |
22 | #endif | |
23 | ||
abbffdf9 UD |
24 | #ifdef HAVE_CONFIG_H |
25 | # include <config.h> | |
26 | #endif | |
27 | ||
28 | #include <sys/types.h> | |
29 | ||
0555fcce | 30 | #ifdef __GNUC__ |
abbffdf9 UD |
31 | # define alloca __builtin_alloca |
32 | # define HAVE_ALLOCA 1 | |
33 | #else | |
6d248857 WN |
34 | # ifdef _MSC_VER |
35 | # include <malloc.h> | |
36 | # define alloca _alloca | |
abbffdf9 | 37 | # else |
6d248857 WN |
38 | # if defined HAVE_ALLOCA_H || defined _LIBC |
39 | # include <alloca.h> | |
abbffdf9 | 40 | # else |
6d248857 WN |
41 | # ifdef _AIX |
42 | #pragma alloca | |
43 | # else | |
44 | # ifndef alloca | |
abbffdf9 | 45 | char *alloca (); |
6d248857 | 46 | # endif |
abbffdf9 UD |
47 | # endif |
48 | # endif | |
49 | # endif | |
50 | #endif | |
51 | ||
52 | #include <errno.h> | |
53 | #ifndef errno | |
54 | extern int errno; | |
55 | #endif | |
56 | #ifndef __set_errno | |
57 | # define __set_errno(val) errno = (val) | |
58 | #endif | |
59 | ||
0555fcce UD |
60 | #include <stddef.h> |
61 | #include <stdlib.h> | |
9695dd0c | 62 | #include <stdio.h> |
0555fcce | 63 | #include <string.h> |
abbffdf9 UD |
64 | |
65 | #if defined HAVE_UNISTD_H || defined _LIBC | |
66 | # include <unistd.h> | |
67 | #endif | |
68 | ||
0555fcce | 69 | #include <locale.h> |
abbffdf9 | 70 | |
6d248857 WN |
71 | #ifdef _LIBC |
72 | /* Guess whether integer division by zero raises signal SIGFPE. | |
73 | Set to 1 only if you know for sure. In case of doubt, set to 0. */ | |
74 | # if defined __alpha__ || defined __arm__ || defined __i386__ \ | |
75 | || defined __m68k__ || defined __s390__ | |
76 | # define INTDIV0_RAISES_SIGFPE 1 | |
77 | # else | |
78 | # define INTDIV0_RAISES_SIGFPE 0 | |
79 | # endif | |
80 | #endif | |
81 | #if !INTDIV0_RAISES_SIGFPE | |
82 | # include <signal.h> | |
83 | #endif | |
84 | ||
abbffdf9 UD |
85 | #if defined HAVE_SYS_PARAM_H || defined _LIBC |
86 | # include <sys/param.h> | |
87 | #endif | |
88 | ||
6d248857 WN |
89 | #if !defined _LIBC |
90 | # include "localcharset.h" | |
91 | #endif | |
92 | ||
abbffdf9 | 93 | #include "gettextP.h" |
0555fcce | 94 | #include "plural-exp.h" |
abbffdf9 UD |
95 | #ifdef _LIBC |
96 | # include <libintl.h> | |
97 | #else | |
6d248857 WN |
98 | # ifdef IN_LIBGLOCALE |
99 | # include <libintl.h> | |
100 | # endif | |
4a4d50f3 | 101 | # include "libgnuintl.h" |
abbffdf9 UD |
102 | #endif |
103 | #include "hash-string.h" | |
104 | ||
6d248857 | 105 | /* Handle multi-threaded applications. */ |
abbffdf9 | 106 | #ifdef _LIBC |
ec999b8e | 107 | # include <libc-lock.h> |
6d248857 WN |
108 | # define gl_rwlock_define_initialized __libc_rwlock_define_initialized |
109 | # define gl_rwlock_rdlock __libc_rwlock_rdlock | |
110 | # define gl_rwlock_wrlock __libc_rwlock_wrlock | |
111 | # define gl_rwlock_unlock __libc_rwlock_unlock | |
0ed99ce4 | 112 | #else |
6d248857 | 113 | # include "lock.h" |
abbffdf9 UD |
114 | #endif |
115 | ||
45eca4d1 UD |
116 | /* Alignment of types. */ |
117 | #if defined __GNUC__ && __GNUC__ >= 2 | |
118 | # define alignof(TYPE) __alignof__ (TYPE) | |
119 | #else | |
120 | # define alignof(TYPE) \ | |
121 | ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2) | |
122 | #endif | |
123 | ||
4a4d50f3 UD |
124 | /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */ |
125 | #ifndef offsetof | |
126 | # define offsetof(type,ident) ((size_t)&(((type*)0)->ident)) | |
127 | #endif | |
128 | ||
abbffdf9 UD |
129 | /* @@ end of prolog @@ */ |
130 | ||
131 | #ifdef _LIBC | |
132 | /* Rename the non ANSI C functions. This is required by the standard | |
133 | because some ANSI C functions will require linking with this object | |
134 | file and the name space must not be polluted. */ | |
ae65d4f3 | 135 | # define strdup __strdup |
abbffdf9 UD |
136 | # define getcwd __getcwd |
137 | # ifndef stpcpy | |
138 | # define stpcpy __stpcpy | |
139 | # endif | |
4aebaa6b | 140 | # define tfind __tfind |
abbffdf9 UD |
141 | #else |
142 | # if !defined HAVE_GETCWD | |
143 | char *getwd (); | |
144 | # define getcwd(buf, max) getwd (buf) | |
145 | # else | |
6d248857 WN |
146 | # if VMS |
147 | # define getcwd(buf, max) (getcwd) (buf, max, 0) | |
148 | # else | |
abbffdf9 | 149 | char *getcwd (); |
6d248857 | 150 | # endif |
abbffdf9 UD |
151 | # endif |
152 | # ifndef HAVE_STPCPY | |
6d248857 | 153 | static char *stpcpy (char *dest, const char *src); |
abbffdf9 UD |
154 | # endif |
155 | # ifndef HAVE_MEMPCPY | |
6d248857 | 156 | static void *mempcpy (void *dest, const void *src, size_t n); |
abbffdf9 UD |
157 | # endif |
158 | #endif | |
159 | ||
6d248857 WN |
160 | /* Use a replacement if the system does not provide the `tsearch' function |
161 | family. */ | |
162 | #if defined HAVE_TSEARCH || defined _LIBC | |
163 | # include <search.h> | |
164 | #else | |
165 | # define tsearch libintl_tsearch | |
166 | # define tfind libintl_tfind | |
167 | # define tdelete libintl_tdelete | |
168 | # define twalk libintl_twalk | |
169 | # include "tsearch.h" | |
170 | #endif | |
171 | ||
172 | #ifdef _LIBC | |
173 | # define tsearch __tsearch | |
174 | #endif | |
175 | ||
abbffdf9 UD |
176 | /* Amount to increase buffer size by in each try. */ |
177 | #define PATH_INCR 32 | |
178 | ||
179 | /* The following is from pathmax.h. */ | |
180 | /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define | |
181 | PATH_MAX but might cause redefinition warnings when sys/param.h is | |
182 | later included (as on MORE/BSD 4.3). */ | |
183 | #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__) | |
184 | # include <limits.h> | |
185 | #endif | |
186 | ||
187 | #ifndef _POSIX_PATH_MAX | |
188 | # define _POSIX_PATH_MAX 255 | |
189 | #endif | |
190 | ||
191 | #if !defined PATH_MAX && defined _PC_PATH_MAX | |
d9525977 | 192 | # define PATH_MAX (__pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : __pathconf ("/", _PC_PATH_MAX)) |
abbffdf9 UD |
193 | #endif |
194 | ||
195 | /* Don't include sys/param.h if it already has been. */ | |
196 | #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN | |
197 | # include <sys/param.h> | |
198 | #endif | |
199 | ||
200 | #if !defined PATH_MAX && defined MAXPATHLEN | |
201 | # define PATH_MAX MAXPATHLEN | |
202 | #endif | |
203 | ||
204 | #ifndef PATH_MAX | |
205 | # define PATH_MAX _POSIX_PATH_MAX | |
206 | #endif | |
207 | ||
6d248857 WN |
208 | /* Pathname support. |
209 | ISSLASH(C) tests whether C is a directory separator character. | |
210 | IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not, | |
211 | it may be concatenated to a directory pathname. | |
212 | IS_PATH_WITH_DIR(P) tests whether P contains a directory specification. | |
213 | */ | |
214 | #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__ | |
215 | /* Win32, Cygwin, OS/2, DOS */ | |
216 | # define ISSLASH(C) ((C) == '/' || (C) == '\\') | |
217 | # define HAS_DEVICE(P) \ | |
218 | ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \ | |
219 | && (P)[1] == ':') | |
220 | # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P)) | |
221 | # define IS_PATH_WITH_DIR(P) \ | |
222 | (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P)) | |
223 | #else | |
224 | /* Unix */ | |
225 | # define ISSLASH(C) ((C) == '/') | |
226 | # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0]) | |
227 | # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL) | |
228 | #endif | |
229 | ||
8406a53a | 230 | /* Whether to support different locales in different threads. */ |
6d248857 | 231 | #if defined _LIBC || HAVE_USELOCALE || defined IN_LIBGLOCALE |
8406a53a UD |
232 | # define HAVE_PER_THREAD_LOCALE |
233 | #endif | |
234 | ||
abbffdf9 UD |
235 | /* This is the type used for the search tree where known translations |
236 | are stored. */ | |
237 | struct known_translation_t | |
238 | { | |
239 | /* Domain in which to search. */ | |
edac4240 | 240 | const char *domainname; |
abbffdf9 UD |
241 | |
242 | /* The category. */ | |
243 | int category; | |
244 | ||
8406a53a UD |
245 | #ifdef HAVE_PER_THREAD_LOCALE |
246 | /* Name of the relevant locale category, or "" for the global locale. */ | |
247 | const char *localename; | |
248 | #endif | |
249 | ||
6d248857 WN |
250 | #ifdef IN_LIBGLOCALE |
251 | /* The character encoding. */ | |
252 | const char *encoding; | |
253 | #endif | |
254 | ||
abbffdf9 UD |
255 | /* State of the catalog counter at the point the string was found. */ |
256 | int counter; | |
257 | ||
f8f900ec UD |
258 | /* Catalog where the string was found. */ |
259 | struct loaded_l10nfile *domain; | |
260 | ||
abbffdf9 UD |
261 | /* And finally the translation. */ |
262 | const char *translation; | |
f8f900ec | 263 | size_t translation_length; |
abbffdf9 UD |
264 | |
265 | /* Pointer to the string in question. */ | |
80b286ed UD |
266 | union |
267 | { | |
268 | char appended[ZERO]; /* used if domain != NULL */ | |
269 | const char *ptr; /* used if domain == NULL */ | |
270 | } | |
271 | msgid; | |
abbffdf9 UD |
272 | }; |
273 | ||
6d248857 | 274 | gl_rwlock_define_initialized (static, tree_lock) |
abbffdf9 | 275 | |
6d248857 | 276 | /* Root of the search tree with known translations. */ |
abbffdf9 UD |
277 | static void *root; |
278 | ||
abbffdf9 UD |
279 | /* Function to compare two entries in the table of known translations. */ |
280 | static int | |
6d248857 | 281 | transcmp (const void *p1, const void *p2) |
abbffdf9 | 282 | { |
ad5b53b1 UD |
283 | const struct known_translation_t *s1; |
284 | const struct known_translation_t *s2; | |
abbffdf9 UD |
285 | int result; |
286 | ||
ad5b53b1 UD |
287 | s1 = (const struct known_translation_t *) p1; |
288 | s2 = (const struct known_translation_t *) p2; | |
289 | ||
80b286ed UD |
290 | result = strcmp (s1->domain != NULL ? s1->msgid.appended : s1->msgid.ptr, |
291 | s2->domain != NULL ? s2->msgid.appended : s2->msgid.ptr); | |
abbffdf9 UD |
292 | if (result == 0) |
293 | { | |
f8f900ec | 294 | result = strcmp (s1->domainname, s2->domainname); |
abbffdf9 | 295 | if (result == 0) |
8406a53a UD |
296 | { |
297 | #ifdef HAVE_PER_THREAD_LOCALE | |
298 | result = strcmp (s1->localename, s2->localename); | |
299 | if (result == 0) | |
300 | #endif | |
6d248857 WN |
301 | { |
302 | #ifdef IN_LIBGLOCALE | |
303 | result = strcmp (s1->encoding, s2->encoding); | |
304 | if (result == 0) | |
305 | #endif | |
306 | /* We compare the category last (though this is the cheapest | |
307 | operation) since it is hopefully always the same (namely | |
308 | LC_MESSAGES). */ | |
309 | result = s1->category - s2->category; | |
310 | } | |
8406a53a | 311 | } |
abbffdf9 UD |
312 | } |
313 | ||
314 | return result; | |
315 | } | |
abbffdf9 UD |
316 | |
317 | /* Name of the default domain used for gettext(3) prior any call to | |
318 | textdomain(3). The default value for this is "messages". */ | |
418f1701 | 319 | const char _nl_default_default_domain[] attribute_hidden = "messages"; |
abbffdf9 | 320 | |
6d248857 | 321 | #ifndef IN_LIBGLOCALE |
abbffdf9 | 322 | /* Value used as the default domain for gettext(3). */ |
418f1701 UD |
323 | const char *_nl_current_default_domain attribute_hidden |
324 | = _nl_default_default_domain; | |
6d248857 | 325 | #endif |
abbffdf9 UD |
326 | |
327 | /* Contains the default location of the message catalogs. */ | |
6d248857 WN |
328 | #if defined __EMX__ |
329 | extern const char _nl_default_dirname[]; | |
330 | #else | |
331 | # ifdef _LIBC | |
9cfe5381 RM |
332 | extern const char _nl_default_dirname[]; |
333 | libc_hidden_proto (_nl_default_dirname) | |
6d248857 | 334 | # endif |
4a4d50f3 | 335 | const char _nl_default_dirname[] = LOCALEDIR; |
6d248857 | 336 | # ifdef _LIBC |
9cfe5381 | 337 | libc_hidden_data_def (_nl_default_dirname) |
6d248857 | 338 | # endif |
9cfe5381 | 339 | #endif |
abbffdf9 | 340 | |
6d248857 | 341 | #ifndef IN_LIBGLOCALE |
abbffdf9 UD |
342 | /* List with bindings of specific domains created by bindtextdomain() |
343 | calls. */ | |
344 | struct binding *_nl_domain_bindings; | |
6d248857 | 345 | #endif |
abbffdf9 UD |
346 | |
347 | /* Prototypes for local functions. */ | |
6d248857 WN |
348 | static char *plural_lookup (struct loaded_l10nfile *domain, |
349 | unsigned long int n, | |
d7ccc6c9 | 350 | const char *translation, size_t translation_len); |
6d248857 WN |
351 | |
352 | #ifdef IN_LIBGLOCALE | |
353 | static const char *guess_category_value (int category, | |
354 | const char *categoryname, | |
d7ccc6c9 | 355 | const char *localename); |
6d248857 WN |
356 | #else |
357 | static const char *guess_category_value (int category, | |
d7ccc6c9 | 358 | const char *categoryname); |
6d248857 WN |
359 | #endif |
360 | ||
2b71beea RM |
361 | #ifdef _LIBC |
362 | # include "../locale/localeinfo.h" | |
de18a706 | 363 | # define category_to_name(category) _nl_category_names_get (category) |
2b71beea | 364 | #else |
d7ccc6c9 | 365 | static const char *category_to_name (int category); |
6d248857 WN |
366 | #endif |
367 | #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE | |
d7ccc6c9 | 368 | static const char *get_output_charset (struct binding *domainbinding); |
2b71beea | 369 | #endif |
abbffdf9 UD |
370 | |
371 | ||
6d248857 | 372 | /* For those losing systems which don't have `alloca' we have to add |
abbffdf9 UD |
373 | some additional code emulating it. */ |
374 | #ifdef HAVE_ALLOCA | |
375 | /* Nothing has to be done. */ | |
0f131646 | 376 | # define freea(p) /* nothing */ |
abbffdf9 UD |
377 | # define ADD_BLOCK(list, address) /* nothing */ |
378 | # define FREE_BLOCKS(list) /* nothing */ | |
379 | #else | |
380 | struct block_list | |
381 | { | |
382 | void *address; | |
383 | struct block_list *next; | |
384 | }; | |
385 | # define ADD_BLOCK(list, addr) \ | |
386 | do { \ | |
387 | struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \ | |
388 | /* If we cannot get a free block we cannot add the new element to \ | |
389 | the list. */ \ | |
390 | if (newp != NULL) { \ | |
391 | newp->address = (addr); \ | |
392 | newp->next = (list); \ | |
393 | (list) = newp; \ | |
394 | } \ | |
395 | } while (0) | |
396 | # define FREE_BLOCKS(list) \ | |
397 | do { \ | |
398 | while (list != NULL) { \ | |
399 | struct block_list *old = list; \ | |
400 | list = list->next; \ | |
0f131646 | 401 | free (old->address); \ |
abbffdf9 UD |
402 | free (old); \ |
403 | } \ | |
404 | } while (0) | |
405 | # undef alloca | |
406 | # define alloca(size) (malloc (size)) | |
0f131646 | 407 | # define freea(p) free (p) |
abbffdf9 UD |
408 | #endif /* have alloca */ |
409 | ||
410 | ||
f8f900ec | 411 | #ifdef _LIBC |
7f455351 | 412 | /* List of blocks allocated for translations. */ |
f8f900ec | 413 | typedef struct transmem_list |
7f455351 UD |
414 | { |
415 | struct transmem_list *next; | |
4a4d50f3 | 416 | char data[ZERO]; |
f8f900ec UD |
417 | } transmem_block_t; |
418 | static struct transmem_list *transmem_list; | |
419 | #else | |
e686e1a1 | 420 | typedef unsigned char transmem_block_t; |
f8f900ec | 421 | #endif |
7f455351 UD |
422 | |
423 | ||
abbffdf9 UD |
424 | /* Names for the libintl functions are a problem. They must not clash |
425 | with existing names and they should follow ANSI C. But this source | |
426 | code is also used in GNU C Library where the names have a __ | |
427 | prefix. So we have to make a difference here. */ | |
428 | #ifdef _LIBC | |
429 | # define DCIGETTEXT __dcigettext | |
430 | #else | |
0f131646 | 431 | # define DCIGETTEXT libintl_dcigettext |
abbffdf9 UD |
432 | #endif |
433 | ||
0ed99ce4 | 434 | /* Lock variable to protect the global data in the gettext implementation. */ |
6d248857 | 435 | gl_rwlock_define_initialized (, _nl_state_lock attribute_hidden) |
0ed99ce4 | 436 | |
abbffdf9 UD |
437 | /* Checking whether the binaries runs SUID must be done and glibc provides |
438 | easier methods therefore we make a difference here. */ | |
439 | #ifdef _LIBC | |
440 | # define ENABLE_SECURE __libc_enable_secure | |
441 | # define DETERMINE_SECURE | |
442 | #else | |
0f131646 RM |
443 | # ifndef HAVE_GETUID |
444 | # define getuid() 0 | |
445 | # endif | |
446 | # ifndef HAVE_GETGID | |
447 | # define getgid() 0 | |
448 | # endif | |
449 | # ifndef HAVE_GETEUID | |
450 | # define geteuid() getuid() | |
451 | # endif | |
452 | # ifndef HAVE_GETEGID | |
453 | # define getegid() getgid() | |
454 | # endif | |
abbffdf9 UD |
455 | static int enable_secure; |
456 | # define ENABLE_SECURE (enable_secure == 1) | |
457 | # define DETERMINE_SECURE \ | |
458 | if (enable_secure == 0) \ | |
459 | { \ | |
460 | if (getuid () != geteuid () || getgid () != getegid ()) \ | |
461 | enable_secure = 1; \ | |
462 | else \ | |
463 | enable_secure = -1; \ | |
464 | } | |
465 | #endif | |
466 | ||
0555fcce | 467 | /* Get the function to evaluate the plural expression. */ |
6d248857 | 468 | #include "eval-plural.h" |
0555fcce | 469 | |
abbffdf9 UD |
470 | /* Look up MSGID in the DOMAINNAME message catalog for the current |
471 | CATEGORY locale and, if PLURAL is nonzero, search over string | |
472 | depending on the plural form determined by N. */ | |
6d248857 | 473 | #ifdef IN_LIBGLOCALE |
abbffdf9 | 474 | char * |
6d248857 WN |
475 | gl_dcigettext (const char *domainname, |
476 | const char *msgid1, const char *msgid2, | |
477 | int plural, unsigned long int n, | |
478 | int category, | |
479 | const char *localename, const char *encoding) | |
480 | #else | |
481 | char * | |
482 | DCIGETTEXT (const char *domainname, const char *msgid1, const char *msgid2, | |
483 | int plural, unsigned long int n, int category) | |
484 | #endif | |
abbffdf9 UD |
485 | { |
486 | #ifndef HAVE_ALLOCA | |
487 | struct block_list *block_list = NULL; | |
488 | #endif | |
489 | struct loaded_l10nfile *domain; | |
490 | struct binding *binding; | |
491 | const char *categoryname; | |
492 | const char *categoryvalue; | |
6d248857 | 493 | const char *dirname; |
9695dd0c | 494 | char *xdirname = NULL; |
6d248857 | 495 | char *xdomainname; |
abbffdf9 UD |
496 | char *single_locale; |
497 | char *retval; | |
f8f900ec | 498 | size_t retlen; |
abbffdf9 | 499 | int saved_errno; |
80b286ed | 500 | struct known_translation_t search; |
abbffdf9 | 501 | struct known_translation_t **foundp = NULL; |
6d248857 | 502 | #if defined HAVE_PER_THREAD_LOCALE && !defined IN_LIBGLOCALE |
8406a53a | 503 | const char *localename; |
abbffdf9 UD |
504 | #endif |
505 | size_t domainname_len; | |
506 | ||
507 | /* If no real MSGID is given return NULL. */ | |
508 | if (msgid1 == NULL) | |
509 | return NULL; | |
510 | ||
2b71beea RM |
511 | #ifdef _LIBC |
512 | if (category < 0 || category >= __LC_LAST || category == LC_ALL) | |
513 | /* Bogus. */ | |
514 | return (plural == 0 | |
515 | ? (char *) msgid1 | |
516 | /* Use the Germanic plural rule. */ | |
517 | : n == 1 ? (char *) msgid1 : (char *) msgid2); | |
518 | #endif | |
519 | ||
6d248857 WN |
520 | /* Preserve the `errno' value. */ |
521 | saved_errno = errno; | |
522 | ||
9a69db29 UD |
523 | #ifdef _LIBC |
524 | __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden) | |
525 | __libc_rwlock_rdlock (__libc_setlocale_lock); | |
526 | #endif | |
527 | ||
6d248857 | 528 | gl_rwlock_rdlock (_nl_state_lock); |
0ed99ce4 | 529 | |
17c389fc UD |
530 | /* If DOMAINNAME is NULL, we are interested in the default domain. If |
531 | CATEGORY is not LC_MESSAGES this might not make much sense but the | |
532 | definition left this undefined. */ | |
533 | if (domainname == NULL) | |
534 | domainname = _nl_current_default_domain; | |
535 | ||
6d248857 WN |
536 | /* OS/2 specific: backward compatibility with older libintl versions */ |
537 | #ifdef LC_MESSAGES_COMPAT | |
538 | if (category == LC_MESSAGES_COMPAT) | |
539 | category = LC_MESSAGES; | |
540 | #endif | |
541 | ||
f8f900ec UD |
542 | /* Try to find the translation among those which we found at |
543 | some time. */ | |
80b286ed UD |
544 | search.domain = NULL; |
545 | search.msgid.ptr = msgid1; | |
546 | search.domainname = domainname; | |
547 | search.category = category; | |
6d248857 WN |
548 | #ifdef HAVE_PER_THREAD_LOCALE |
549 | # ifndef IN_LIBGLOCALE | |
8406a53a | 550 | # ifdef _LIBC |
37e9dc81 | 551 | localename = __current_locale_name (category); |
6d248857 WN |
552 | # else |
553 | categoryname = category_to_name (category); | |
554 | # define CATEGORYNAME_INITIALIZED | |
555 | localename = _nl_locale_name_thread_unsafe (category, categoryname); | |
556 | if (localename == NULL) | |
557 | localename = ""; | |
8406a53a | 558 | # endif |
6d248857 | 559 | # endif |
80b286ed | 560 | search.localename = localename; |
6d248857 WN |
561 | # ifdef IN_LIBGLOCALE |
562 | search.encoding = encoding; | |
8406a53a | 563 | # endif |
f8f900ec | 564 | |
ce7265c7 UD |
565 | /* Since tfind/tsearch manage a balanced tree, concurrent tfind and |
566 | tsearch calls can be fatal. */ | |
6d248857 | 567 | gl_rwlock_rdlock (tree_lock); |
ce7265c7 | 568 | |
80b286ed | 569 | foundp = (struct known_translation_t **) tfind (&search, &root, transcmp); |
ce7265c7 | 570 | |
6d248857 | 571 | gl_rwlock_unlock (tree_lock); |
ce7265c7 | 572 | |
f8f900ec | 573 | if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr) |
abbffdf9 | 574 | { |
f8f900ec UD |
575 | /* Now deal with plural. */ |
576 | if (plural) | |
577 | retval = plural_lookup ((*foundp)->domain, n, (*foundp)->translation, | |
578 | (*foundp)->translation_length); | |
579 | else | |
580 | retval = (char *) (*foundp)->translation; | |
581 | ||
6d248857 | 582 | gl_rwlock_unlock (_nl_state_lock); |
9a69db29 UD |
583 | # ifdef _LIBC |
584 | __libc_rwlock_unlock (__libc_setlocale_lock); | |
585 | # endif | |
6d248857 | 586 | __set_errno (saved_errno); |
f8f900ec | 587 | return retval; |
abbffdf9 UD |
588 | } |
589 | #endif | |
590 | ||
abbffdf9 UD |
591 | /* See whether this is a SUID binary or not. */ |
592 | DETERMINE_SECURE; | |
593 | ||
abbffdf9 | 594 | /* First find matching binding. */ |
6d248857 WN |
595 | #ifdef IN_LIBGLOCALE |
596 | /* We can use a trivial binding, since _nl_find_msg will ignore it anyway, | |
597 | and _nl_load_domain and _nl_find_domain just pass it through. */ | |
598 | binding = NULL; | |
599 | dirname = bindtextdomain (domainname, NULL); | |
600 | #else | |
abbffdf9 UD |
601 | for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next) |
602 | { | |
603 | int compare = strcmp (domainname, binding->domainname); | |
604 | if (compare == 0) | |
605 | /* We found it! */ | |
606 | break; | |
607 | if (compare < 0) | |
608 | { | |
609 | /* It is not in the list. */ | |
610 | binding = NULL; | |
611 | break; | |
612 | } | |
613 | } | |
614 | ||
615 | if (binding == NULL) | |
6d248857 | 616 | dirname = _nl_default_dirname; |
abbffdf9 UD |
617 | else |
618 | { | |
6d248857 WN |
619 | dirname = binding->dirname; |
620 | #endif | |
621 | if (!IS_ABSOLUTE_PATH (dirname)) | |
622 | { | |
623 | /* We have a relative path. Make it absolute now. */ | |
9695dd0c FW |
624 | char *cwd = getcwd (NULL, 0); |
625 | if (cwd == NULL) | |
626 | /* We cannot get the current working directory. Don't | |
627 | signal an error but simply return the default | |
628 | string. */ | |
6d248857 | 629 | goto return_untranslated; |
9695dd0c FW |
630 | int ret = __asprintf (&xdirname, "%s/%s", cwd, dirname); |
631 | free (cwd); | |
632 | if (ret < 0) | |
8c1aafc1 | 633 | goto return_untranslated; |
9695dd0c | 634 | dirname = xdirname; |
6d248857 WN |
635 | } |
636 | #ifndef IN_LIBGLOCALE | |
abbffdf9 | 637 | } |
6d248857 | 638 | #endif |
abbffdf9 UD |
639 | |
640 | /* Now determine the symbolic name of CATEGORY and its value. */ | |
6d248857 | 641 | #ifndef CATEGORYNAME_INITIALIZED |
abbffdf9 | 642 | categoryname = category_to_name (category); |
6d248857 WN |
643 | #endif |
644 | #ifdef IN_LIBGLOCALE | |
645 | categoryvalue = guess_category_value (category, categoryname, localename); | |
646 | #else | |
abbffdf9 | 647 | categoryvalue = guess_category_value (category, categoryname); |
6d248857 | 648 | #endif |
abbffdf9 UD |
649 | |
650 | domainname_len = strlen (domainname); | |
651 | xdomainname = (char *) alloca (strlen (categoryname) | |
652 | + domainname_len + 5); | |
653 | ADD_BLOCK (block_list, xdomainname); | |
654 | ||
6d248857 WN |
655 | stpcpy ((char *) mempcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"), |
656 | domainname, domainname_len), | |
abbffdf9 UD |
657 | ".mo"); |
658 | ||
659 | /* Creating working area. */ | |
660 | single_locale = (char *) alloca (strlen (categoryvalue) + 1); | |
661 | ADD_BLOCK (block_list, single_locale); | |
662 | ||
663 | ||
664 | /* Search for the given string. This is a loop because we perhaps | |
665 | got an ordered list of languages to consider for the translation. */ | |
666 | while (1) | |
667 | { | |
668 | /* Make CATEGORYVALUE point to the next element of the list. */ | |
669 | while (categoryvalue[0] != '\0' && categoryvalue[0] == ':') | |
670 | ++categoryvalue; | |
671 | if (categoryvalue[0] == '\0') | |
672 | { | |
673 | /* The whole contents of CATEGORYVALUE has been searched but | |
674 | no valid entry has been found. We solve this situation | |
675 | by implicitly appending a "C" entry, i.e. no translation | |
676 | will take place. */ | |
677 | single_locale[0] = 'C'; | |
678 | single_locale[1] = '\0'; | |
679 | } | |
680 | else | |
681 | { | |
682 | char *cp = single_locale; | |
683 | while (categoryvalue[0] != '\0' && categoryvalue[0] != ':') | |
684 | *cp++ = *categoryvalue++; | |
685 | *cp = '\0'; | |
686 | ||
687 | /* When this is a SUID binary we must not allow accessing files | |
688 | outside the dedicated directories. */ | |
6d248857 | 689 | if (ENABLE_SECURE && IS_PATH_WITH_DIR (single_locale)) |
abbffdf9 UD |
690 | /* Ingore this entry. */ |
691 | continue; | |
692 | } | |
693 | ||
d0aefec4 BH |
694 | /* If the current locale value is "C" or "C.<encoding>" or "POSIX", |
695 | we don't load a domain. Return the MSGID. */ | |
696 | if ((single_locale[0] == 'C' | |
697 | && (single_locale[1] == '\0' || single_locale[1] == '.')) | |
abbffdf9 | 698 | || strcmp (single_locale, "POSIX") == 0) |
6d248857 | 699 | break; |
abbffdf9 UD |
700 | |
701 | /* Find structure describing the message catalog matching the | |
702 | DOMAINNAME and CATEGORY. */ | |
17c389fc | 703 | domain = _nl_find_domain (dirname, single_locale, xdomainname, binding); |
abbffdf9 UD |
704 | |
705 | if (domain != NULL) | |
706 | { | |
6d248857 WN |
707 | #if defined IN_LIBGLOCALE |
708 | retval = _nl_find_msg (domain, binding, encoding, msgid1, &retlen); | |
709 | #else | |
4e82c610 | 710 | retval = _nl_find_msg (domain, binding, msgid1, 1, &retlen); |
6d248857 | 711 | #endif |
abbffdf9 UD |
712 | |
713 | if (retval == NULL) | |
714 | { | |
715 | int cnt; | |
716 | ||
717 | for (cnt = 0; domain->successor[cnt] != NULL; ++cnt) | |
718 | { | |
6d248857 WN |
719 | #if defined IN_LIBGLOCALE |
720 | retval = _nl_find_msg (domain->successor[cnt], binding, | |
721 | encoding, msgid1, &retlen); | |
722 | #else | |
c44a663d | 723 | retval = _nl_find_msg (domain->successor[cnt], binding, |
4e82c610 | 724 | msgid1, 1, &retlen); |
6d248857 | 725 | #endif |
abbffdf9 | 726 | |
7a44c18f CD |
727 | /* Resource problems are not fatal, instead we return no |
728 | translation. */ | |
6d248857 WN |
729 | if (__builtin_expect (retval == (char *) -1, 0)) |
730 | goto return_untranslated; | |
7a44c18f | 731 | |
abbffdf9 | 732 | if (retval != NULL) |
f8f900ec UD |
733 | { |
734 | domain = domain->successor[cnt]; | |
735 | break; | |
736 | } | |
abbffdf9 UD |
737 | } |
738 | } | |
739 | ||
4259230f UD |
740 | /* Returning -1 means that some resource problem exists |
741 | (likely memory) and that the strings could not be | |
742 | converted. Return the original strings. */ | |
6d248857 WN |
743 | if (__builtin_expect (retval == (char *) -1, 0)) |
744 | break; | |
4259230f | 745 | |
abbffdf9 UD |
746 | if (retval != NULL) |
747 | { | |
f8f900ec UD |
748 | /* Found the translation of MSGID1 in domain DOMAIN: |
749 | starting at RETVAL, RETLEN bytes. */ | |
9695dd0c | 750 | free (xdirname); |
abbffdf9 | 751 | FREE_BLOCKS (block_list); |
abbffdf9 UD |
752 | if (foundp == NULL) |
753 | { | |
754 | /* Create a new entry and add it to the search tree. */ | |
80b286ed | 755 | size_t msgid_len; |
8406a53a | 756 | size_t size; |
abbffdf9 UD |
757 | struct known_translation_t *newp; |
758 | ||
80b286ed | 759 | msgid_len = strlen (msgid1) + 1; |
8406a53a UD |
760 | size = offsetof (struct known_translation_t, msgid) |
761 | + msgid_len + domainname_len + 1; | |
6d248857 | 762 | #ifdef HAVE_PER_THREAD_LOCALE |
8406a53a | 763 | size += strlen (localename) + 1; |
6d248857 | 764 | #endif |
8406a53a | 765 | newp = (struct known_translation_t *) malloc (size); |
abbffdf9 UD |
766 | if (newp != NULL) |
767 | { | |
edac4240 | 768 | char *new_domainname; |
6d248857 | 769 | #ifdef HAVE_PER_THREAD_LOCALE |
8406a53a | 770 | char *new_localename; |
6d248857 | 771 | #endif |
edac4240 | 772 | |
80b286ed | 773 | new_domainname = |
6d248857 WN |
774 | (char *) mempcpy (newp->msgid.appended, msgid1, |
775 | msgid_len); | |
edac4240 | 776 | memcpy (new_domainname, domainname, domainname_len + 1); |
6d248857 | 777 | #ifdef HAVE_PER_THREAD_LOCALE |
8406a53a UD |
778 | new_localename = new_domainname + domainname_len + 1; |
779 | strcpy (new_localename, localename); | |
6d248857 | 780 | #endif |
edac4240 | 781 | newp->domainname = new_domainname; |
abbffdf9 | 782 | newp->category = category; |
6d248857 | 783 | #ifdef HAVE_PER_THREAD_LOCALE |
8406a53a | 784 | newp->localename = new_localename; |
6d248857 WN |
785 | #endif |
786 | #ifdef IN_LIBGLOCALE | |
787 | newp->encoding = encoding; | |
788 | #endif | |
abbffdf9 | 789 | newp->counter = _nl_msg_cat_cntr; |
f8f900ec | 790 | newp->domain = domain; |
abbffdf9 | 791 | newp->translation = retval; |
f8f900ec | 792 | newp->translation_length = retlen; |
abbffdf9 | 793 | |
6d248857 | 794 | gl_rwlock_wrlock (tree_lock); |
ce7265c7 | 795 | |
abbffdf9 UD |
796 | /* Insert the entry in the search tree. */ |
797 | foundp = (struct known_translation_t **) | |
798 | tsearch (newp, &root, transcmp); | |
ce7265c7 | 799 | |
6d248857 | 800 | gl_rwlock_unlock (tree_lock); |
ce7265c7 | 801 | |
d89d0afa UD |
802 | if (foundp == NULL |
803 | || __builtin_expect (*foundp != newp, 0)) | |
abbffdf9 UD |
804 | /* The insert failed. */ |
805 | free (newp); | |
806 | } | |
807 | } | |
808 | else | |
809 | { | |
810 | /* We can update the existing entry. */ | |
811 | (*foundp)->counter = _nl_msg_cat_cntr; | |
f8f900ec | 812 | (*foundp)->domain = domain; |
abbffdf9 | 813 | (*foundp)->translation = retval; |
f8f900ec | 814 | (*foundp)->translation_length = retlen; |
abbffdf9 | 815 | } |
6d248857 | 816 | |
7f4dce05 RM |
817 | __set_errno (saved_errno); |
818 | ||
f8f900ec UD |
819 | /* Now deal with plural. */ |
820 | if (plural) | |
821 | retval = plural_lookup (domain, n, retval, retlen); | |
822 | ||
6d248857 WN |
823 | gl_rwlock_unlock (_nl_state_lock); |
824 | #ifdef _LIBC | |
9a69db29 | 825 | __libc_rwlock_unlock (__libc_setlocale_lock); |
6d248857 | 826 | #endif |
abbffdf9 UD |
827 | return retval; |
828 | } | |
829 | } | |
830 | } | |
6d248857 WN |
831 | |
832 | return_untranslated: | |
833 | /* Return the untranslated MSGID. */ | |
9695dd0c | 834 | free (xdirname); |
6d248857 WN |
835 | FREE_BLOCKS (block_list); |
836 | gl_rwlock_unlock (_nl_state_lock); | |
837 | #ifdef _LIBC | |
838 | __libc_rwlock_unlock (__libc_setlocale_lock); | |
839 | #endif | |
840 | #ifndef _LIBC | |
841 | if (!ENABLE_SECURE) | |
842 | { | |
843 | extern void _nl_log_untranslated (const char *logfilename, | |
844 | const char *domainname, | |
845 | const char *msgid1, const char *msgid2, | |
846 | int plural); | |
847 | const char *logfilename = getenv ("GETTEXT_LOG_UNTRANSLATED"); | |
848 | ||
849 | if (logfilename != NULL && logfilename[0] != '\0') | |
850 | _nl_log_untranslated (logfilename, domainname, msgid1, msgid2, plural); | |
851 | } | |
852 | #endif | |
853 | __set_errno (saved_errno); | |
854 | return (plural == 0 | |
855 | ? (char *) msgid1 | |
856 | /* Use the Germanic plural rule. */ | |
857 | : n == 1 ? (char *) msgid1 : (char *) msgid2); | |
abbffdf9 UD |
858 | } |
859 | ||
860 | ||
6d248857 WN |
861 | /* Look up the translation of msgid within DOMAIN_FILE and DOMAINBINDING. |
862 | Return it if found. Return NULL if not found or in case of a conversion | |
863 | failure (problem in the particular message catalog). Return (char *) -1 | |
864 | in case of a memory allocation failure during conversion (only if | |
865 | ENCODING != NULL resp. CONVERT == true). */ | |
abbffdf9 | 866 | char * |
6d248857 WN |
867 | #ifdef IN_LIBGLOCALE |
868 | _nl_find_msg (struct loaded_l10nfile *domain_file, | |
869 | struct binding *domainbinding, const char *encoding, | |
870 | const char *msgid, | |
871 | size_t *lengthp) | |
872 | #else | |
873 | _nl_find_msg (struct loaded_l10nfile *domain_file, | |
874 | struct binding *domainbinding, | |
875 | const char *msgid, int convert, | |
876 | size_t *lengthp) | |
877 | #endif | |
abbffdf9 | 878 | { |
354e6102 | 879 | struct loaded_domain *domain; |
3172f58f | 880 | nls_uint32 nstrings; |
0a55a284 UD |
881 | size_t act; |
882 | char *result; | |
f8f900ec | 883 | size_t resultlen; |
abbffdf9 | 884 | |
ce7265c7 | 885 | if (domain_file->decided <= 0) |
c44a663d | 886 | _nl_load_domain (domain_file, domainbinding); |
abbffdf9 UD |
887 | |
888 | if (domain_file->data == NULL) | |
889 | return NULL; | |
890 | ||
354e6102 | 891 | domain = (struct loaded_domain *) domain_file->data; |
abbffdf9 | 892 | |
3172f58f UD |
893 | nstrings = domain->nstrings; |
894 | ||
abbffdf9 | 895 | /* Locate the MSGID and its translation. */ |
3172f58f | 896 | if (domain->hash_tab != NULL) |
abbffdf9 UD |
897 | { |
898 | /* Use the hashing table. */ | |
899 | nls_uint32 len = strlen (msgid); | |
dd9423a6 | 900 | nls_uint32 hash_val = __hash_string (msgid); |
abbffdf9 UD |
901 | nls_uint32 idx = hash_val % domain->hash_size; |
902 | nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2)); | |
abbffdf9 | 903 | |
abbffdf9 UD |
904 | while (1) |
905 | { | |
3172f58f UD |
906 | nls_uint32 nstr = |
907 | W (domain->must_swap_hash_tab, domain->hash_tab[idx]); | |
4a4d50f3 UD |
908 | |
909 | if (nstr == 0) | |
910 | /* Hash table entry is empty. */ | |
911 | return NULL; | |
912 | ||
3172f58f UD |
913 | nstr--; |
914 | ||
915 | /* Compare msgid with the original string at index nstr. | |
f8f900ec UD |
916 | We compare the lengths with >=, not ==, because plural entries |
917 | are represented by strings with an embedded NUL. */ | |
3172f58f UD |
918 | if (nstr < nstrings |
919 | ? W (domain->must_swap, domain->orig_tab[nstr].length) >= len | |
920 | && (strcmp (msgid, | |
921 | domain->data + W (domain->must_swap, | |
922 | domain->orig_tab[nstr].offset)) | |
923 | == 0) | |
924 | : domain->orig_sysdep_tab[nstr - nstrings].length > len | |
925 | && (strcmp (msgid, | |
926 | domain->orig_sysdep_tab[nstr - nstrings].pointer) | |
927 | == 0)) | |
f8f900ec | 928 | { |
3172f58f | 929 | act = nstr; |
f8f900ec UD |
930 | goto found; |
931 | } | |
932 | ||
abbffdf9 UD |
933 | if (idx >= domain->hash_size - incr) |
934 | idx -= domain->hash_size - incr; | |
935 | else | |
936 | idx += incr; | |
abbffdf9 UD |
937 | } |
938 | /* NOTREACHED */ | |
939 | } | |
0a55a284 UD |
940 | else |
941 | { | |
942 | /* Try the default method: binary search in the sorted array of | |
943 | messages. */ | |
944 | size_t top, bottom; | |
945 | ||
946 | bottom = 0; | |
3172f58f | 947 | top = nstrings; |
0a55a284 UD |
948 | while (bottom < top) |
949 | { | |
950 | int cmp_val; | |
951 | ||
952 | act = (bottom + top) / 2; | |
953 | cmp_val = strcmp (msgid, (domain->data | |
954 | + W (domain->must_swap, | |
955 | domain->orig_tab[act].offset))); | |
956 | if (cmp_val < 0) | |
957 | top = act; | |
958 | else if (cmp_val > 0) | |
959 | bottom = act + 1; | |
960 | else | |
961 | goto found; | |
962 | } | |
963 | /* No translation was found. */ | |
964 | return NULL; | |
965 | } | |
abbffdf9 | 966 | |
0a55a284 UD |
967 | found: |
968 | /* The translation was found at index ACT. If we have to convert the | |
969 | string to use a different character set, this is the time. */ | |
3172f58f UD |
970 | if (act < nstrings) |
971 | { | |
972 | result = (char *) | |
973 | (domain->data + W (domain->must_swap, domain->trans_tab[act].offset)); | |
974 | resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1; | |
975 | } | |
976 | else | |
977 | { | |
978 | result = (char *) domain->trans_sysdep_tab[act - nstrings].pointer; | |
979 | resultlen = domain->trans_sysdep_tab[act - nstrings].length; | |
980 | } | |
0a55a284 UD |
981 | |
982 | #if defined _LIBC || HAVE_ICONV | |
6d248857 WN |
983 | # ifdef IN_LIBGLOCALE |
984 | if (encoding != NULL) | |
985 | # else | |
4e82c610 | 986 | if (convert) |
6d248857 | 987 | # endif |
c44a663d | 988 | { |
4e82c610 | 989 | /* We are supposed to do a conversion. */ |
6d248857 | 990 | # ifndef IN_LIBGLOCALE |
4e82c610 | 991 | const char *encoding = get_output_charset (domainbinding); |
6d248857 WN |
992 | # endif |
993 | size_t nconversions; | |
994 | struct converted_domain *convd; | |
995 | size_t i; | |
4e82c610 | 996 | |
408cfca0 | 997 | /* Protect against reallocation of the table. */ |
6d248857 | 998 | gl_rwlock_rdlock (domain->conversions_lock); |
408cfca0 | 999 | |
4e82c610 UD |
1000 | /* Search whether a table with converted translations for this |
1001 | encoding has already been allocated. */ | |
6d248857 WN |
1002 | nconversions = domain->nconversions; |
1003 | convd = NULL; | |
4e82c610 UD |
1004 | |
1005 | for (i = nconversions; i > 0; ) | |
1006 | { | |
1007 | i--; | |
1008 | if (strcmp (domain->conversions[i].encoding, encoding) == 0) | |
1009 | { | |
1010 | convd = &domain->conversions[i]; | |
1011 | break; | |
1012 | } | |
1013 | } | |
0ecb606c | 1014 | |
6d248857 | 1015 | gl_rwlock_unlock (domain->conversions_lock); |
408cfca0 | 1016 | |
4e82c610 UD |
1017 | if (convd == NULL) |
1018 | { | |
408cfca0 | 1019 | /* We have to allocate a new conversions table. */ |
6d248857 | 1020 | gl_rwlock_wrlock (domain->conversions_lock); |
2ecc7d93 | 1021 | nconversions = domain->nconversions; |
408cfca0 UD |
1022 | |
1023 | /* Maybe in the meantime somebody added the translation. | |
1024 | Recheck. */ | |
1025 | for (i = nconversions; i > 0; ) | |
1026 | { | |
1027 | i--; | |
1028 | if (strcmp (domain->conversions[i].encoding, encoding) == 0) | |
1029 | { | |
1030 | convd = &domain->conversions[i]; | |
1031 | goto found_convd; | |
1032 | } | |
1033 | } | |
1034 | ||
6d248857 WN |
1035 | { |
1036 | /* Allocate a table for the converted translations for this | |
1037 | encoding. */ | |
1038 | struct converted_domain *new_conversions = | |
1039 | (struct converted_domain *) | |
1040 | (domain->conversions != NULL | |
1041 | ? realloc (domain->conversions, | |
1042 | (nconversions + 1) * sizeof (struct converted_domain)) | |
1043 | : malloc ((nconversions + 1) * sizeof (struct converted_domain))); | |
1044 | ||
1045 | if (__builtin_expect (new_conversions == NULL, 0)) | |
1046 | { | |
1047 | /* Nothing we can do, no more memory. We cannot use the | |
1048 | translation because it might be encoded incorrectly. */ | |
1049 | unlock_fail: | |
1050 | gl_rwlock_unlock (domain->conversions_lock); | |
1051 | return (char *) -1; | |
1052 | } | |
4e82c610 | 1053 | |
6d248857 WN |
1054 | domain->conversions = new_conversions; |
1055 | ||
1056 | /* Copy the 'encoding' string to permanent storage. */ | |
1057 | encoding = strdup (encoding); | |
1058 | if (__builtin_expect (encoding == NULL, 0)) | |
408cfca0 UD |
1059 | /* Nothing we can do, no more memory. We cannot use the |
1060 | translation because it might be encoded incorrectly. */ | |
6d248857 | 1061 | goto unlock_fail; |
4e82c610 | 1062 | |
6d248857 WN |
1063 | convd = &new_conversions[nconversions]; |
1064 | convd->encoding = encoding; | |
4e82c610 | 1065 | |
6d248857 WN |
1066 | /* Find out about the character set the file is encoded with. |
1067 | This can be found (in textual form) in the entry "". If this | |
1068 | entry does not exist or if this does not contain the 'charset=' | |
1069 | information, we will assume the charset matches the one the | |
1070 | current locale and we don't have to perform any conversion. */ | |
0ecb606c | 1071 | # ifdef _LIBC |
6d248857 | 1072 | convd->conv = (__gconv_t) -1; |
0ecb606c JJ |
1073 | # else |
1074 | # if HAVE_ICONV | |
6d248857 | 1075 | convd->conv = (iconv_t) -1; |
0ecb606c JJ |
1076 | # endif |
1077 | # endif | |
6d248857 WN |
1078 | { |
1079 | char *nullentry; | |
1080 | size_t nullentrylen; | |
1081 | ||
1082 | /* Get the header entry. This is a recursion, but it doesn't | |
1083 | reallocate domain->conversions because we pass | |
1084 | encoding = NULL or convert = 0, respectively. */ | |
1085 | nullentry = | |
1086 | # ifdef IN_LIBGLOCALE | |
1087 | _nl_find_msg (domain_file, domainbinding, NULL, "", | |
1088 | &nullentrylen); | |
1089 | # else | |
1090 | _nl_find_msg (domain_file, domainbinding, "", 0, &nullentrylen); | |
1091 | # endif | |
4e82c610 | 1092 | |
6d248857 WN |
1093 | /* Resource problems are fatal. If we continue onwards we will |
1094 | only attempt to calloc a new conv_tab and fail later. */ | |
1095 | if (__builtin_expect (nullentry == (char *) -1, 0)) | |
1096 | return (char *) -1; | |
7a44c18f | 1097 | |
6d248857 WN |
1098 | if (nullentry != NULL) |
1099 | { | |
1100 | const char *charsetstr; | |
4e82c610 | 1101 | |
6d248857 WN |
1102 | charsetstr = strstr (nullentry, "charset="); |
1103 | if (charsetstr != NULL) | |
1104 | { | |
1105 | size_t len; | |
1106 | char *charset; | |
1107 | const char *outcharset; | |
4e82c610 | 1108 | |
6d248857 WN |
1109 | charsetstr += strlen ("charset="); |
1110 | len = strcspn (charsetstr, " \t\n"); | |
4e82c610 | 1111 | |
6d248857 | 1112 | charset = (char *) alloca (len + 1); |
4e82c610 | 1113 | # if defined _LIBC || HAVE_MEMPCPY |
6d248857 | 1114 | *((char *) mempcpy (charset, charsetstr, len)) = '\0'; |
4e82c610 | 1115 | # else |
6d248857 WN |
1116 | memcpy (charset, charsetstr, len); |
1117 | charset[len] = '\0'; | |
4e82c610 UD |
1118 | # endif |
1119 | ||
6d248857 | 1120 | outcharset = encoding; |
4e82c610 UD |
1121 | |
1122 | # ifdef _LIBC | |
91927b7c | 1123 | |
7d4ec75e AS |
1124 | struct gconv_spec conv_spec; |
1125 | ||
1126 | __gconv_create_spec (&conv_spec, charset, outcharset); | |
1127 | ||
1128 | /* We always want to use transliteration. */ | |
1129 | conv_spec.translit = true; | |
1130 | ||
91927b7c AS |
1131 | int r = __gconv_open (&conv_spec, &convd->conv, |
1132 | GCONV_AVOID_NOCONV); | |
7d4ec75e AS |
1133 | |
1134 | __gconv_destroy_spec (&conv_spec); | |
1135 | ||
6d248857 WN |
1136 | if (__builtin_expect (r != __GCONV_OK, 0)) |
1137 | { | |
1138 | /* If the output encoding is the same there is | |
1139 | nothing to do. Otherwise do not use the | |
1140 | translation at all. */ | |
1141 | if (__builtin_expect (r != __GCONV_NULCONV, 1)) | |
1142 | { | |
1143 | gl_rwlock_unlock (domain->conversions_lock); | |
1144 | free ((char *) encoding); | |
1145 | return NULL; | |
1146 | } | |
1147 | ||
1148 | convd->conv = (__gconv_t) -1; | |
1149 | } | |
4e82c610 UD |
1150 | # else |
1151 | # if HAVE_ICONV | |
6d248857 WN |
1152 | /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5, |
1153 | we want to use transliteration. */ | |
1154 | # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \ | |
1155 | && !defined __UCLIBC__) \ | |
4e82c610 | 1156 | || _LIBICONV_VERSION >= 0x0105 |
6d248857 WN |
1157 | if (strchr (outcharset, '/') == NULL) |
1158 | { | |
1159 | char *tmp; | |
4e82c610 | 1160 | |
6d248857 WN |
1161 | len = strlen (outcharset); |
1162 | tmp = (char *) alloca (len + 10 + 1); | |
1163 | memcpy (tmp, outcharset, len); | |
1164 | memcpy (tmp + len, "//TRANSLIT", 10 + 1); | |
1165 | outcharset = tmp; | |
4e82c610 | 1166 | |
6d248857 | 1167 | convd->conv = iconv_open (outcharset, charset); |
4e82c610 | 1168 | |
6d248857 WN |
1169 | freea (outcharset); |
1170 | } | |
1171 | else | |
4e82c610 | 1172 | # endif |
6d248857 | 1173 | convd->conv = iconv_open (outcharset, charset); |
4e82c610 UD |
1174 | # endif |
1175 | # endif | |
1176 | ||
6d248857 WN |
1177 | freea (charset); |
1178 | } | |
1179 | } | |
1180 | } | |
1181 | convd->conv_tab = NULL; | |
1182 | /* Here domain->conversions is still == new_conversions. */ | |
1183 | domain->nconversions++; | |
4e82c610 | 1184 | } |
408cfca0 UD |
1185 | |
1186 | found_convd: | |
6d248857 | 1187 | gl_rwlock_unlock (domain->conversions_lock); |
4e82c610 UD |
1188 | } |
1189 | ||
1190 | if ( | |
1191 | # ifdef _LIBC | |
1192 | convd->conv != (__gconv_t) -1 | |
1193 | # else | |
1194 | # if HAVE_ICONV | |
1195 | convd->conv != (iconv_t) -1 | |
1196 | # endif | |
1197 | # endif | |
1198 | ) | |
0a55a284 | 1199 | { |
4e82c610 UD |
1200 | /* We are supposed to do a conversion. First allocate an |
1201 | appropriate table with the same structure as the table | |
1202 | of translations in the file, where we can put the pointers | |
1203 | to the converted strings in. | |
1204 | There is a slight complication with plural entries. They | |
1205 | are represented by consecutive NUL terminated strings. We | |
1206 | handle this case by converting RESULTLEN bytes, including | |
1207 | NULs. */ | |
1208 | ||
6d248857 WN |
1209 | /* This lock primarily protects the memory management variables |
1210 | freemem, freemem_size. It also protects write accesses to | |
1211 | convd->conv_tab. It's not worth using a separate lock (such | |
1212 | as domain->conversions_lock) for this purpose, because when | |
1213 | modifying convd->conv_tab, we also need to lock freemem, | |
1214 | freemem_size for most of the time. */ | |
1215 | __libc_lock_define_initialized (static, lock) | |
1216 | ||
1217 | if (__builtin_expect (convd->conv_tab == NULL, 0)) | |
2ecc7d93 UD |
1218 | { |
1219 | __libc_lock_lock (lock); | |
1220 | if (convd->conv_tab == NULL) | |
1221 | { | |
6d248857 WN |
1222 | convd->conv_tab = |
1223 | (char **) calloc (nstrings + domain->n_sysdep_strings, | |
1224 | sizeof (char *)); | |
2ecc7d93 UD |
1225 | if (convd->conv_tab != NULL) |
1226 | goto not_translated_yet; | |
1227 | /* Mark that we didn't succeed allocating a table. */ | |
1228 | convd->conv_tab = (char **) -1; | |
1229 | } | |
1230 | __libc_lock_unlock (lock); | |
1231 | } | |
4e82c610 | 1232 | |
6d248857 | 1233 | if (__builtin_expect (convd->conv_tab == (char **) -1, 0)) |
4259230f UD |
1234 | /* Nothing we can do, no more memory. We cannot use the |
1235 | translation because it might be encoded incorrectly. */ | |
1236 | return (char *) -1; | |
4e82c610 UD |
1237 | |
1238 | if (convd->conv_tab[act] == NULL) | |
1239 | { | |
1240 | /* We haven't used this string so far, so it is not | |
1241 | translated yet. Do this now. */ | |
1242 | /* We use a bit more efficient memory handling. | |
1243 | We allocate always larger blocks which get used over | |
1244 | time. This is faster than many small allocations. */ | |
f8f900ec | 1245 | # define INITIAL_BLOCK_SIZE 4080 |
4e82c610 UD |
1246 | static unsigned char *freemem; |
1247 | static size_t freemem_size; | |
0a55a284 | 1248 | |
4e82c610 UD |
1249 | const unsigned char *inbuf; |
1250 | unsigned char *outbuf; | |
1251 | int malloc_count; | |
f8f900ec | 1252 | # ifndef _LIBC |
6d248857 | 1253 | transmem_block_t *transmem_list; |
f8f900ec | 1254 | # endif |
0a55a284 | 1255 | |
6d248857 WN |
1256 | __libc_lock_lock (lock); |
1257 | not_translated_yet: | |
1258 | ||
4e82c610 UD |
1259 | inbuf = (const unsigned char *) result; |
1260 | outbuf = freemem + sizeof (size_t); | |
6d248857 WN |
1261 | # ifndef _LIBC |
1262 | transmem_list = NULL; | |
1263 | # endif | |
45eca4d1 | 1264 | |
4e82c610 UD |
1265 | malloc_count = 0; |
1266 | while (1) | |
1267 | { | |
1268 | transmem_block_t *newmem; | |
0a55a284 | 1269 | # ifdef _LIBC |
4e82c610 UD |
1270 | size_t non_reversible; |
1271 | int res; | |
17c389fc | 1272 | |
4e82c610 UD |
1273 | if (freemem_size < sizeof (size_t)) |
1274 | goto resize_freemem; | |
45eca4d1 | 1275 | |
4e82c610 UD |
1276 | res = __gconv (convd->conv, |
1277 | &inbuf, inbuf + resultlen, | |
1278 | &outbuf, | |
1279 | outbuf + freemem_size - sizeof (size_t), | |
1280 | &non_reversible); | |
17c389fc | 1281 | |
4e82c610 UD |
1282 | if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT) |
1283 | break; | |
17c389fc | 1284 | |
4e82c610 UD |
1285 | if (res != __GCONV_FULL_OUTPUT) |
1286 | { | |
4259230f UD |
1287 | /* We should not use the translation at all, it |
1288 | is incorrectly encoded. */ | |
4e82c610 | 1289 | __libc_lock_unlock (lock); |
4259230f | 1290 | return NULL; |
4e82c610 | 1291 | } |
17c389fc | 1292 | |
4e82c610 | 1293 | inbuf = (const unsigned char *) result; |
0a55a284 UD |
1294 | # else |
1295 | # if HAVE_ICONV | |
4e82c610 UD |
1296 | const char *inptr = (const char *) inbuf; |
1297 | size_t inleft = resultlen; | |
1298 | char *outptr = (char *) outbuf; | |
1299 | size_t outleft; | |
1300 | ||
1301 | if (freemem_size < sizeof (size_t)) | |
1302 | goto resize_freemem; | |
1303 | ||
1304 | outleft = freemem_size - sizeof (size_t); | |
1305 | if (iconv (convd->conv, | |
1306 | (ICONV_CONST char **) &inptr, &inleft, | |
1307 | &outptr, &outleft) | |
1308 | != (size_t) (-1)) | |
1309 | { | |
1310 | outbuf = (unsigned char *) outptr; | |
1311 | break; | |
1312 | } | |
1313 | if (errno != E2BIG) | |
1314 | { | |
1315 | __libc_lock_unlock (lock); | |
4259230f | 1316 | return NULL; |
4e82c610 | 1317 | } |
17c389fc UD |
1318 | # endif |
1319 | # endif | |
0a55a284 | 1320 | |
4e82c610 UD |
1321 | resize_freemem: |
1322 | /* We must allocate a new buffer or resize the old one. */ | |
1323 | if (malloc_count > 0) | |
1324 | { | |
1325 | ++malloc_count; | |
1326 | freemem_size = malloc_count * INITIAL_BLOCK_SIZE; | |
1327 | newmem = (transmem_block_t *) realloc (transmem_list, | |
1328 | freemem_size); | |
f8f900ec | 1329 | # ifdef _LIBC |
4e82c610 | 1330 | if (newmem != NULL) |
006dd861 | 1331 | transmem_list = newmem; |
4e82c610 UD |
1332 | else |
1333 | { | |
1334 | struct transmem_list *old = transmem_list; | |
1335 | ||
1336 | transmem_list = transmem_list->next; | |
1337 | free (old); | |
1338 | } | |
1339 | # endif | |
1340 | } | |
f8f900ec UD |
1341 | else |
1342 | { | |
4e82c610 UD |
1343 | malloc_count = 1; |
1344 | freemem_size = INITIAL_BLOCK_SIZE; | |
1345 | newmem = (transmem_block_t *) malloc (freemem_size); | |
006dd861 | 1346 | # ifdef _LIBC |
7a44c18f CD |
1347 | if (newmem != NULL) |
1348 | { | |
1349 | /* Add the block to the list of blocks we have to free | |
1350 | at some point. */ | |
1351 | newmem->next = transmem_list; | |
1352 | transmem_list = newmem; | |
1353 | } | |
1354 | /* Fall through and return -1. */ | |
006dd861 | 1355 | # endif |
4e82c610 | 1356 | } |
6d248857 | 1357 | if (__builtin_expect (newmem == NULL, 0)) |
4e82c610 UD |
1358 | { |
1359 | freemem = NULL; | |
1360 | freemem_size = 0; | |
1361 | __libc_lock_unlock (lock); | |
4259230f | 1362 | return (char *) -1; |
f8f900ec | 1363 | } |
0a55a284 | 1364 | |
7f455351 | 1365 | # ifdef _LIBC |
4e82c610 UD |
1366 | freemem = (unsigned char *) newmem->data; |
1367 | freemem_size -= offsetof (struct transmem_list, data); | |
f8f900ec | 1368 | # else |
4e82c610 UD |
1369 | transmem_list = newmem; |
1370 | freemem = newmem; | |
7f455351 UD |
1371 | # endif |
1372 | ||
4e82c610 UD |
1373 | outbuf = freemem + sizeof (size_t); |
1374 | } | |
1375 | ||
1376 | /* We have now in our buffer a converted string. Put this | |
1377 | into the table of conversions. */ | |
1378 | *(size_t *) freemem = outbuf - freemem - sizeof (size_t); | |
1379 | convd->conv_tab[act] = (char *) freemem; | |
1380 | /* Shrink freemem, but keep it aligned. */ | |
1381 | freemem_size -= outbuf - freemem; | |
1382 | freemem = outbuf; | |
1383 | freemem += freemem_size & (alignof (size_t) - 1); | |
1384 | freemem_size = freemem_size & ~ (alignof (size_t) - 1); | |
1385 | ||
1386 | __libc_lock_unlock (lock); | |
0a55a284 | 1387 | } |
0a55a284 | 1388 | |
4e82c610 UD |
1389 | /* Now convd->conv_tab[act] contains the translation of all |
1390 | the plural variants. */ | |
1391 | result = convd->conv_tab[act] + sizeof (size_t); | |
1392 | resultlen = *(size_t *) convd->conv_tab[act]; | |
0a55a284 | 1393 | } |
0a55a284 UD |
1394 | } |
1395 | ||
0a55a284 UD |
1396 | /* The result string is converted. */ |
1397 | ||
1398 | #endif /* _LIBC || HAVE_ICONV */ | |
1399 | ||
f8f900ec UD |
1400 | *lengthp = resultlen; |
1401 | return result; | |
1402 | } | |
1403 | ||
1404 | ||
1405 | /* Look up a plural variant. */ | |
1406 | static char * | |
6d248857 WN |
1407 | plural_lookup (struct loaded_l10nfile *domain, unsigned long int n, |
1408 | const char *translation, size_t translation_len) | |
f8f900ec UD |
1409 | { |
1410 | struct loaded_domain *domaindata = (struct loaded_domain *) domain->data; | |
1411 | unsigned long int index; | |
1412 | const char *p; | |
1413 | ||
1414 | index = plural_eval (domaindata->plural, n); | |
1415 | if (index >= domaindata->nplurals) | |
1416 | /* This should never happen. It means the plural expression and the | |
1417 | given maximum value do not match. */ | |
1418 | index = 0; | |
1419 | ||
1420 | /* Skip INDEX strings at TRANSLATION. */ | |
1421 | p = translation; | |
0a55a284 UD |
1422 | while (index-- > 0) |
1423 | { | |
f8f900ec | 1424 | p = strchr (p, '\0'); |
0a55a284 | 1425 | /* And skip over the NUL byte. */ |
f8f900ec | 1426 | p++; |
abbffdf9 | 1427 | |
f8f900ec UD |
1428 | if (p >= translation + translation_len) |
1429 | /* This should never happen. It means the plural expression | |
1430 | evaluated to a value larger than the number of variants | |
1431 | available for MSGID1. */ | |
1432 | return (char *) translation; | |
1433 | } | |
1434 | return (char *) p; | |
abbffdf9 UD |
1435 | } |
1436 | ||
2b71beea | 1437 | #ifndef _LIBC |
abbffdf9 UD |
1438 | /* Return string representation of locale CATEGORY. */ |
1439 | static const char * | |
6d248857 | 1440 | category_to_name (int category) |
abbffdf9 UD |
1441 | { |
1442 | const char *retval; | |
1443 | ||
1444 | switch (category) | |
1445 | { | |
1446 | #ifdef LC_COLLATE | |
1447 | case LC_COLLATE: | |
1448 | retval = "LC_COLLATE"; | |
1449 | break; | |
1450 | #endif | |
1451 | #ifdef LC_CTYPE | |
1452 | case LC_CTYPE: | |
1453 | retval = "LC_CTYPE"; | |
1454 | break; | |
1455 | #endif | |
1456 | #ifdef LC_MONETARY | |
1457 | case LC_MONETARY: | |
1458 | retval = "LC_MONETARY"; | |
1459 | break; | |
1460 | #endif | |
1461 | #ifdef LC_NUMERIC | |
1462 | case LC_NUMERIC: | |
1463 | retval = "LC_NUMERIC"; | |
1464 | break; | |
1465 | #endif | |
1466 | #ifdef LC_TIME | |
1467 | case LC_TIME: | |
1468 | retval = "LC_TIME"; | |
1469 | break; | |
1470 | #endif | |
1471 | #ifdef LC_MESSAGES | |
1472 | case LC_MESSAGES: | |
1473 | retval = "LC_MESSAGES"; | |
1474 | break; | |
1475 | #endif | |
1476 | #ifdef LC_RESPONSE | |
1477 | case LC_RESPONSE: | |
1478 | retval = "LC_RESPONSE"; | |
1479 | break; | |
1480 | #endif | |
1481 | #ifdef LC_ALL | |
1482 | case LC_ALL: | |
1483 | /* This might not make sense but is perhaps better than any other | |
1484 | value. */ | |
1485 | retval = "LC_ALL"; | |
1486 | break; | |
1487 | #endif | |
1488 | default: | |
1489 | /* If you have a better idea for a default value let me know. */ | |
1490 | retval = "LC_XXX"; | |
1491 | } | |
1492 | ||
1493 | return retval; | |
1494 | } | |
2b71beea | 1495 | #endif |
abbffdf9 | 1496 | |
6d248857 WN |
1497 | /* Guess value of current locale from value of the environment variables |
1498 | or system-dependent defaults. */ | |
abbffdf9 | 1499 | static const char * |
6d248857 WN |
1500 | #ifdef IN_LIBGLOCALE |
1501 | guess_category_value (int category, const char *categoryname, | |
1502 | const char *locale) | |
1503 | ||
1504 | #else | |
1505 | guess_category_value (int category, const char *categoryname) | |
1506 | #endif | |
abbffdf9 | 1507 | { |
3859d27d | 1508 | const char *language; |
6d248857 WN |
1509 | #ifndef IN_LIBGLOCALE |
1510 | const char *locale; | |
1511 | # ifndef _LIBC | |
1512 | const char *language_default; | |
1513 | int locale_defaulted; | |
1514 | # endif | |
1515 | #endif | |
3859d27d | 1516 | |
6d248857 WN |
1517 | /* We use the settings in the following order: |
1518 | 1. The value of the environment variable 'LANGUAGE'. This is a GNU | |
1519 | extension. Its value can be a colon-separated list of locale names. | |
1520 | 2. The value of the environment variable 'LC_ALL', 'LC_xxx', or 'LANG'. | |
1521 | More precisely, the first among these that is set to a non-empty value. | |
1522 | This is how POSIX specifies it. The value is a single locale name. | |
1523 | 3. A system-dependent preference list of languages. Its value can be a | |
1524 | colon-separated list of locale names. | |
1525 | 4. A system-dependent default locale name. | |
1526 | This way: | |
1527 | - System-dependent settings can be overridden by environment variables. | |
1528 | - If the system provides both a list of languages and a default locale, | |
1529 | the former is used. */ | |
1530 | ||
1531 | #ifndef IN_LIBGLOCALE | |
1532 | /* Fetch the locale name, through the POSIX method of looking to `LC_ALL', | |
3859d27d UD |
1533 | `LC_xxx', and `LANG'. On some systems this can be done by the |
1534 | `setlocale' function itself. */ | |
6d248857 WN |
1535 | # ifdef _LIBC |
1536 | locale = __current_locale_name (category); | |
1537 | # else | |
1538 | locale_defaulted = 0; | |
1539 | # if HAVE_USELOCALE | |
1540 | locale = _nl_locale_name_thread_unsafe (category, categoryname); | |
1541 | if (locale == NULL) | |
1542 | # endif | |
1543 | { | |
1544 | locale = _nl_locale_name_posix (category, categoryname); | |
1545 | if (locale == NULL) | |
1546 | { | |
1547 | locale = _nl_locale_name_default (); | |
1548 | locale_defaulted = 1; | |
1549 | } | |
1550 | } | |
1551 | # endif | |
abbffdf9 | 1552 | #endif |
3859d27d | 1553 | |
6d248857 WN |
1554 | /* Ignore LANGUAGE and its system-dependent analogon if the locale is set |
1555 | to "C" because | |
1556 | 1. "C" locale usually uses the ASCII encoding, and most international | |
1557 | messages use non-ASCII characters. These characters get displayed | |
1558 | as question marks (if using glibc's iconv()) or as invalid 8-bit | |
1559 | characters (because other iconv()s refuse to convert most non-ASCII | |
1560 | characters to ASCII). In any case, the output is ugly. | |
1561 | 2. The precise output of some programs in the "C" locale is specified | |
1562 | by POSIX and should not depend on environment variables like | |
1563 | "LANGUAGE" or system-dependent information. We allow such programs | |
2897b231 BH |
1564 | to use gettext(). |
1565 | Ignore LANGUAGE and its system-dependent analogon also if the locale is | |
1566 | set to "C.UTF-8" or, more generally, to "C.<encoding>", because that's | |
1567 | the by-design behaviour for glibc, see | |
1568 | <https://sourceware.org/glibc/wiki/Proposals/C.UTF-8>. */ | |
1569 | if (locale[0] == 'C' && (locale[1] == '\0' || locale[1] == '.')) | |
6d248857 WN |
1570 | return locale; |
1571 | ||
1572 | /* The highest priority value is the value of the 'LANGUAGE' environment | |
1573 | variable. */ | |
1574 | language = getenv ("LANGUAGE"); | |
1575 | if (language != NULL && language[0] != '\0') | |
1576 | return language; | |
1577 | #if !defined IN_LIBGLOCALE && !defined _LIBC | |
1578 | /* The next priority value is the locale name, if not defaulted. */ | |
1579 | if (locale_defaulted) | |
1580 | { | |
1581 | /* The next priority value is the default language preferences list. */ | |
1582 | language_default = _nl_language_preferences_default (); | |
1583 | if (language_default != NULL) | |
1584 | return language_default; | |
1585 | } | |
1586 | /* The least priority value is the locale name, if defaulted. */ | |
1587 | #endif | |
1588 | return locale; | |
abbffdf9 UD |
1589 | } |
1590 | ||
6d248857 | 1591 | #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE |
4e82c610 UD |
1592 | /* Returns the output charset. */ |
1593 | static const char * | |
6d248857 | 1594 | get_output_charset (struct binding *domainbinding) |
4e82c610 UD |
1595 | { |
1596 | /* The output charset should normally be determined by the locale. But | |
1597 | sometimes the locale is not used or not correctly set up, so we provide | |
1598 | a possibility for the user to override this: the OUTPUT_CHARSET | |
1599 | environment variable. Moreover, the value specified through | |
1600 | bind_textdomain_codeset overrides both. */ | |
1601 | if (domainbinding != NULL && domainbinding->codeset != NULL) | |
1602 | return domainbinding->codeset; | |
1603 | else | |
1604 | { | |
1605 | /* For speed reasons, we look at the value of OUTPUT_CHARSET only | |
1606 | once. This is a user variable that is not supposed to change | |
1607 | during a program run. */ | |
1608 | static char *output_charset_cache; | |
1609 | static int output_charset_cached; | |
1610 | ||
1611 | if (!output_charset_cached) | |
1612 | { | |
1613 | const char *value = getenv ("OUTPUT_CHARSET"); | |
1614 | ||
1615 | if (value != NULL && value[0] != '\0') | |
1616 | { | |
1617 | size_t len = strlen (value) + 1; | |
1618 | char *value_copy = (char *) malloc (len); | |
1619 | ||
1620 | if (value_copy != NULL) | |
1621 | memcpy (value_copy, value, len); | |
1622 | output_charset_cache = value_copy; | |
1623 | } | |
1624 | output_charset_cached = 1; | |
1625 | } | |
1626 | ||
1627 | if (output_charset_cache != NULL) | |
1628 | return output_charset_cache; | |
1629 | else | |
1630 | { | |
1631 | # ifdef _LIBC | |
1632 | return _NL_CURRENT (LC_CTYPE, CODESET); | |
1633 | # else | |
1634 | # if HAVE_ICONV | |
4e82c610 UD |
1635 | return locale_charset (); |
1636 | # endif | |
1637 | # endif | |
1638 | } | |
1639 | } | |
1640 | } | |
1641 | #endif | |
1642 | ||
abbffdf9 UD |
1643 | /* @@ begin of epilog @@ */ |
1644 | ||
1645 | /* We don't want libintl.a to depend on any other library. So we | |
1646 | avoid the non-standard function stpcpy. In GNU C Library this | |
1647 | function is available, though. Also allow the symbol HAVE_STPCPY | |
1648 | to be defined. */ | |
1649 | #if !_LIBC && !HAVE_STPCPY | |
1650 | static char * | |
6d248857 | 1651 | stpcpy (char *dest, const char *src) |
abbffdf9 UD |
1652 | { |
1653 | while ((*dest++ = *src++) != '\0') | |
1654 | /* Do nothing. */ ; | |
1655 | return dest - 1; | |
1656 | } | |
1657 | #endif | |
1658 | ||
1659 | #if !_LIBC && !HAVE_MEMPCPY | |
1660 | static void * | |
6d248857 | 1661 | mempcpy (void *dest, const void *src, size_t n) |
abbffdf9 | 1662 | { |
b98f4e05 | 1663 | return (void *) ((char *) memcpy (dest, src, n) + n); |
abbffdf9 UD |
1664 | } |
1665 | #endif | |
1666 | ||
6d248857 WN |
1667 | #if !_LIBC && !HAVE_TSEARCH |
1668 | # include "tsearch.c" | |
1669 | #endif | |
1670 | ||
abbffdf9 UD |
1671 | |
1672 | #ifdef _LIBC | |
1673 | /* If we want to free all resources we have to do some work at | |
1674 | program's end. */ | |
88677348 AZN |
1675 | void |
1676 | __intl_freemem (void) | |
abbffdf9 | 1677 | { |
7f455351 | 1678 | void *old; |
abbffdf9 | 1679 | |
55e2d5c7 | 1680 | while (_nl_domain_bindings != NULL) |
17c389fc | 1681 | { |
55e2d5c7 UD |
1682 | struct binding *oldp = _nl_domain_bindings; |
1683 | _nl_domain_bindings = _nl_domain_bindings->next; | |
9cfe5381 | 1684 | if (oldp->dirname != _nl_default_dirname) |
17c389fc | 1685 | /* Yes, this is a pointer comparison. */ |
55e2d5c7 UD |
1686 | free (oldp->dirname); |
1687 | free (oldp->codeset); | |
1688 | free (oldp); | |
17c389fc | 1689 | } |
abbffdf9 UD |
1690 | |
1691 | if (_nl_current_default_domain != _nl_default_default_domain) | |
1692 | /* Yes, again a pointer comparison. */ | |
1693 | free ((char *) _nl_current_default_domain); | |
1694 | ||
17c389fc | 1695 | /* Remove the search tree with the known translations. */ |
abbffdf9 | 1696 | __tdestroy (root, free); |
55e2d5c7 | 1697 | root = NULL; |
7f455351 UD |
1698 | |
1699 | while (transmem_list != NULL) | |
1700 | { | |
1701 | old = transmem_list; | |
1702 | transmem_list = transmem_list->next; | |
1703 | free (old); | |
1704 | } | |
abbffdf9 | 1705 | } |
abbffdf9 | 1706 | #endif |