1 /* Implementation of the internal dcigettext function.
2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
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.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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. */
21 # define _GNU_SOURCE 1
28 #include <sys/types.h>
31 # define alloca __builtin_alloca
32 # define HAVE_ALLOCA 1
36 # define alloca _alloca
38 # if defined HAVE_ALLOCA_H || defined _LIBC
57 # define __set_errno(val) errno = (val)
65 #if defined HAVE_UNISTD_H || defined _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
78 # define INTDIV0_RAISES_SIGFPE 0
81 #if !INTDIV0_RAISES_SIGFPE
85 #if defined HAVE_SYS_PARAM_H || defined _LIBC
86 # include <sys/param.h>
90 # include "localcharset.h"
94 #include "plural-exp.h"
101 # include "libgnuintl.h"
103 #include "hash-string.h"
105 /* Handle multi-threaded applications. */
107 # include <libc-lock.h>
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
116 /* Alignment of types. */
117 #if defined __GNUC__ && __GNUC__ >= 2
118 # define alignof(TYPE) __alignof__ (TYPE)
120 # define alignof(TYPE) \
121 ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2)
124 /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */
126 # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
129 /* @@ end of prolog @@ */
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. */
135 # define strdup __strdup
136 # define getcwd __getcwd
138 # define stpcpy __stpcpy
140 # define tfind __tfind
142 # if !defined HAVE_GETCWD
144 # define getcwd(buf, max) getwd (buf)
147 # define getcwd(buf, max) (getcwd) (buf, max, 0)
153 static char *stpcpy (char *dest
, const char *src
);
155 # ifndef HAVE_MEMPCPY
156 static void *mempcpy (void *dest
, const void *src
, size_t n
);
160 /* Use a replacement if the system does not provide the `tsearch' function
162 #if defined HAVE_TSEARCH || defined _LIBC
165 # define tsearch libintl_tsearch
166 # define tfind libintl_tfind
167 # define tdelete libintl_tdelete
168 # define twalk libintl_twalk
169 # include "tsearch.h"
173 # define tsearch __tsearch
176 /* Amount to increase buffer size by in each try. */
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__)
187 #ifndef _POSIX_PATH_MAX
188 # define _POSIX_PATH_MAX 255
191 #if !defined PATH_MAX && defined _PC_PATH_MAX
192 # define PATH_MAX (__pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : __pathconf ("/", _PC_PATH_MAX))
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>
200 #if !defined PATH_MAX && defined MAXPATHLEN
201 # define PATH_MAX MAXPATHLEN
205 # define PATH_MAX _POSIX_PATH_MAX
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.
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')) \
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))
225 # define ISSLASH(C) ((C) == '/')
226 # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
227 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
230 /* Whether to support different locales in different threads. */
231 #if defined _LIBC || HAVE_USELOCALE || defined IN_LIBGLOCALE
232 # define HAVE_PER_THREAD_LOCALE
235 /* This is the type used for the search tree where known translations
237 struct known_translation_t
239 /* Domain in which to search. */
240 const char *domainname
;
245 #ifdef HAVE_PER_THREAD_LOCALE
246 /* Name of the relevant locale category, or "" for the global locale. */
247 const char *localename
;
251 /* The character encoding. */
252 const char *encoding
;
255 /* State of the catalog counter at the point the string was found. */
258 /* Catalog where the string was found. */
259 struct loaded_l10nfile
*domain
;
261 /* And finally the translation. */
262 const char *translation
;
263 size_t translation_length
;
265 /* Pointer to the string in question. */
268 char appended
[ZERO
]; /* used if domain != NULL */
269 const char *ptr
; /* used if domain == NULL */
274 gl_rwlock_define_initialized (static, tree_lock
)
276 /* Root of the search tree with known translations. */
279 /* Function to compare two entries in the table of known translations. */
281 transcmp (const void *p1
, const void *p2
)
283 const struct known_translation_t
*s1
;
284 const struct known_translation_t
*s2
;
287 s1
= (const struct known_translation_t
*) p1
;
288 s2
= (const struct known_translation_t
*) p2
;
290 result
= strcmp (s1
->domain
!= NULL
? s1
->msgid
.appended
: s1
->msgid
.ptr
,
291 s2
->domain
!= NULL
? s2
->msgid
.appended
: s2
->msgid
.ptr
);
294 result
= strcmp (s1
->domainname
, s2
->domainname
);
297 #ifdef HAVE_PER_THREAD_LOCALE
298 result
= strcmp (s1
->localename
, s2
->localename
);
303 result
= strcmp (s1
->encoding
, s2
->encoding
);
306 /* We compare the category last (though this is the cheapest
307 operation) since it is hopefully always the same (namely
309 result
= s1
->category
- s2
->category
;
317 /* Name of the default domain used for gettext(3) prior any call to
318 textdomain(3). The default value for this is "messages". */
319 const char _nl_default_default_domain
[] attribute_hidden
= "messages";
321 #ifndef IN_LIBGLOCALE
322 /* Value used as the default domain for gettext(3). */
323 const char *_nl_current_default_domain attribute_hidden
324 = _nl_default_default_domain
;
327 /* Contains the default location of the message catalogs. */
329 extern const char _nl_default_dirname
[];
332 extern const char _nl_default_dirname
[];
333 libc_hidden_proto (_nl_default_dirname
)
335 const char _nl_default_dirname
[] = LOCALEDIR
;
337 libc_hidden_data_def (_nl_default_dirname
)
341 #ifndef IN_LIBGLOCALE
342 /* List with bindings of specific domains created by bindtextdomain()
344 struct binding
*_nl_domain_bindings
;
347 /* Prototypes for local functions. */
348 static char *plural_lookup (struct loaded_l10nfile
*domain
,
350 const char *translation
, size_t translation_len
);
353 static const char *guess_category_value (int category
,
354 const char *categoryname
,
355 const char *localename
);
357 static const char *guess_category_value (int category
,
358 const char *categoryname
);
362 # include "../locale/localeinfo.h"
363 # define category_to_name(category) \
364 _nl_category_names.str + _nl_category_name_idxs[category]
366 static const char *category_to_name (int category
);
368 #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE
369 static const char *get_output_charset (struct binding
*domainbinding
);
373 /* For those losing systems which don't have `alloca' we have to add
374 some additional code emulating it. */
376 /* Nothing has to be done. */
377 # define freea(p) /* nothing */
378 # define ADD_BLOCK(list, address) /* nothing */
379 # define FREE_BLOCKS(list) /* nothing */
384 struct block_list
*next
;
386 # define ADD_BLOCK(list, addr) \
388 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
389 /* If we cannot get a free block we cannot add the new element to \
391 if (newp != NULL) { \
392 newp->address = (addr); \
393 newp->next = (list); \
397 # define FREE_BLOCKS(list) \
399 while (list != NULL) { \
400 struct block_list *old = list; \
402 free (old->address); \
407 # define alloca(size) (malloc (size))
408 # define freea(p) free (p)
409 #endif /* have alloca */
413 /* List of blocks allocated for translations. */
414 typedef struct transmem_list
416 struct transmem_list
*next
;
419 static struct transmem_list
*transmem_list
;
421 typedef unsigned char transmem_block_t
;
425 /* Names for the libintl functions are a problem. They must not clash
426 with existing names and they should follow ANSI C. But this source
427 code is also used in GNU C Library where the names have a __
428 prefix. So we have to make a difference here. */
430 # define DCIGETTEXT __dcigettext
432 # define DCIGETTEXT libintl_dcigettext
435 /* Lock variable to protect the global data in the gettext implementation. */
436 gl_rwlock_define_initialized (, _nl_state_lock attribute_hidden
)
438 /* Checking whether the binaries runs SUID must be done and glibc provides
439 easier methods therefore we make a difference here. */
441 # define ENABLE_SECURE __libc_enable_secure
442 # define DETERMINE_SECURE
450 # ifndef HAVE_GETEUID
451 # define geteuid() getuid()
453 # ifndef HAVE_GETEGID
454 # define getegid() getgid()
456 static int enable_secure
;
457 # define ENABLE_SECURE (enable_secure == 1)
458 # define DETERMINE_SECURE \
459 if (enable_secure == 0) \
461 if (getuid () != geteuid () || getgid () != getegid ()) \
464 enable_secure = -1; \
468 /* Get the function to evaluate the plural expression. */
469 #include "eval-plural.h"
471 /* Look up MSGID in the DOMAINNAME message catalog for the current
472 CATEGORY locale and, if PLURAL is nonzero, search over string
473 depending on the plural form determined by N. */
476 gl_dcigettext (const char *domainname
,
477 const char *msgid1
, const char *msgid2
,
478 int plural
, unsigned long int n
,
480 const char *localename
, const char *encoding
)
483 DCIGETTEXT (const char *domainname
, const char *msgid1
, const char *msgid2
,
484 int plural
, unsigned long int n
, int category
)
488 struct block_list
*block_list
= NULL
;
490 struct loaded_l10nfile
*domain
;
491 struct binding
*binding
;
492 const char *categoryname
;
493 const char *categoryvalue
;
495 char *xdirname
= NULL
;
501 struct known_translation_t search
;
502 struct known_translation_t
**foundp
= NULL
;
503 #if defined HAVE_PER_THREAD_LOCALE && !defined IN_LIBGLOCALE
504 const char *localename
;
506 size_t domainname_len
;
508 /* If no real MSGID is given return NULL. */
513 if (category
< 0 || category
>= __LC_LAST
|| category
== LC_ALL
)
517 /* Use the Germanic plural rule. */
518 : n
== 1 ? (char *) msgid1
: (char *) msgid2
);
521 /* Preserve the `errno' value. */
525 __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden
)
526 __libc_rwlock_rdlock (__libc_setlocale_lock
);
529 gl_rwlock_rdlock (_nl_state_lock
);
531 /* If DOMAINNAME is NULL, we are interested in the default domain. If
532 CATEGORY is not LC_MESSAGES this might not make much sense but the
533 definition left this undefined. */
534 if (domainname
== NULL
)
535 domainname
= _nl_current_default_domain
;
537 /* OS/2 specific: backward compatibility with older libintl versions */
538 #ifdef LC_MESSAGES_COMPAT
539 if (category
== LC_MESSAGES_COMPAT
)
540 category
= LC_MESSAGES
;
543 /* Try to find the translation among those which we found at
545 search
.domain
= NULL
;
546 search
.msgid
.ptr
= msgid1
;
547 search
.domainname
= domainname
;
548 search
.category
= category
;
549 #ifdef HAVE_PER_THREAD_LOCALE
550 # ifndef IN_LIBGLOCALE
552 localename
= __current_locale_name (category
);
554 categoryname
= category_to_name (category
);
555 # define CATEGORYNAME_INITIALIZED
556 localename
= _nl_locale_name_thread_unsafe (category
, categoryname
);
557 if (localename
== NULL
)
561 search
.localename
= localename
;
562 # ifdef IN_LIBGLOCALE
563 search
.encoding
= encoding
;
566 /* Since tfind/tsearch manage a balanced tree, concurrent tfind and
567 tsearch calls can be fatal. */
568 gl_rwlock_rdlock (tree_lock
);
570 foundp
= (struct known_translation_t
**) tfind (&search
, &root
, transcmp
);
572 gl_rwlock_unlock (tree_lock
);
574 if (foundp
!= NULL
&& (*foundp
)->counter
== _nl_msg_cat_cntr
)
576 /* Now deal with plural. */
578 retval
= plural_lookup ((*foundp
)->domain
, n
, (*foundp
)->translation
,
579 (*foundp
)->translation_length
);
581 retval
= (char *) (*foundp
)->translation
;
583 gl_rwlock_unlock (_nl_state_lock
);
585 __libc_rwlock_unlock (__libc_setlocale_lock
);
587 __set_errno (saved_errno
);
592 /* See whether this is a SUID binary or not. */
595 /* First find matching binding. */
597 /* We can use a trivial binding, since _nl_find_msg will ignore it anyway,
598 and _nl_load_domain and _nl_find_domain just pass it through. */
600 dirname
= bindtextdomain (domainname
, NULL
);
602 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
604 int compare
= strcmp (domainname
, binding
->domainname
);
610 /* It is not in the list. */
617 dirname
= _nl_default_dirname
;
620 dirname
= binding
->dirname
;
622 if (!IS_ABSOLUTE_PATH (dirname
))
624 /* We have a relative path. Make it absolute now. */
625 char *cwd
= getcwd (NULL
, 0);
627 /* We cannot get the current working directory. Don't
628 signal an error but simply return the default
630 goto return_untranslated
;
631 int ret
= __asprintf (&xdirname
, "%s/%s", cwd
, dirname
);
637 #ifndef IN_LIBGLOCALE
641 /* Now determine the symbolic name of CATEGORY and its value. */
642 #ifndef CATEGORYNAME_INITIALIZED
643 categoryname
= category_to_name (category
);
646 categoryvalue
= guess_category_value (category
, categoryname
, localename
);
648 categoryvalue
= guess_category_value (category
, categoryname
);
651 domainname_len
= strlen (domainname
);
652 xdomainname
= (char *) alloca (strlen (categoryname
)
653 + domainname_len
+ 5);
654 ADD_BLOCK (block_list
, xdomainname
);
656 stpcpy ((char *) mempcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
657 domainname
, domainname_len
),
660 /* Creating working area. */
661 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
662 ADD_BLOCK (block_list
, single_locale
);
665 /* Search for the given string. This is a loop because we perhaps
666 got an ordered list of languages to consider for the translation. */
669 /* Make CATEGORYVALUE point to the next element of the list. */
670 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
672 if (categoryvalue
[0] == '\0')
674 /* The whole contents of CATEGORYVALUE has been searched but
675 no valid entry has been found. We solve this situation
676 by implicitly appending a "C" entry, i.e. no translation
678 single_locale
[0] = 'C';
679 single_locale
[1] = '\0';
683 char *cp
= single_locale
;
684 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
685 *cp
++ = *categoryvalue
++;
688 /* When this is a SUID binary we must not allow accessing files
689 outside the dedicated directories. */
690 if (ENABLE_SECURE
&& IS_PATH_WITH_DIR (single_locale
))
691 /* Ingore this entry. */
695 /* If the current locale value is C (or POSIX) we don't load a
696 domain. Return the MSGID. */
697 if (strcmp (single_locale
, "C") == 0
698 || strcmp (single_locale
, "POSIX") == 0)
701 /* Find structure describing the message catalog matching the
702 DOMAINNAME and CATEGORY. */
703 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
, binding
);
707 #if defined IN_LIBGLOCALE
708 retval
= _nl_find_msg (domain
, binding
, encoding
, msgid1
, &retlen
);
710 retval
= _nl_find_msg (domain
, binding
, msgid1
, 1, &retlen
);
717 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
719 #if defined IN_LIBGLOCALE
720 retval
= _nl_find_msg (domain
->successor
[cnt
], binding
,
721 encoding
, msgid1
, &retlen
);
723 retval
= _nl_find_msg (domain
->successor
[cnt
], binding
,
727 /* Resource problems are not fatal, instead we return no
729 if (__builtin_expect (retval
== (char *) -1, 0))
730 goto return_untranslated
;
734 domain
= domain
->successor
[cnt
];
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. */
743 if (__builtin_expect (retval
== (char *) -1, 0))
748 /* Found the translation of MSGID1 in domain DOMAIN:
749 starting at RETVAL, RETLEN bytes. */
751 FREE_BLOCKS (block_list
);
754 /* Create a new entry and add it to the search tree. */
757 struct known_translation_t
*newp
;
759 msgid_len
= strlen (msgid1
) + 1;
760 size
= offsetof (struct known_translation_t
, msgid
)
761 + msgid_len
+ domainname_len
+ 1;
762 #ifdef HAVE_PER_THREAD_LOCALE
763 size
+= strlen (localename
) + 1;
765 newp
= (struct known_translation_t
*) malloc (size
);
768 char *new_domainname
;
769 #ifdef HAVE_PER_THREAD_LOCALE
770 char *new_localename
;
774 (char *) mempcpy (newp
->msgid
.appended
, msgid1
,
776 memcpy (new_domainname
, domainname
, domainname_len
+ 1);
777 #ifdef HAVE_PER_THREAD_LOCALE
778 new_localename
= new_domainname
+ domainname_len
+ 1;
779 strcpy (new_localename
, localename
);
781 newp
->domainname
= new_domainname
;
782 newp
->category
= category
;
783 #ifdef HAVE_PER_THREAD_LOCALE
784 newp
->localename
= new_localename
;
787 newp
->encoding
= encoding
;
789 newp
->counter
= _nl_msg_cat_cntr
;
790 newp
->domain
= domain
;
791 newp
->translation
= retval
;
792 newp
->translation_length
= retlen
;
794 gl_rwlock_wrlock (tree_lock
);
796 /* Insert the entry in the search tree. */
797 foundp
= (struct known_translation_t
**)
798 tsearch (newp
, &root
, transcmp
);
800 gl_rwlock_unlock (tree_lock
);
803 || __builtin_expect (*foundp
!= newp
, 0))
804 /* The insert failed. */
810 /* We can update the existing entry. */
811 (*foundp
)->counter
= _nl_msg_cat_cntr
;
812 (*foundp
)->domain
= domain
;
813 (*foundp
)->translation
= retval
;
814 (*foundp
)->translation_length
= retlen
;
817 __set_errno (saved_errno
);
819 /* Now deal with plural. */
821 retval
= plural_lookup (domain
, n
, retval
, retlen
);
823 gl_rwlock_unlock (_nl_state_lock
);
825 __libc_rwlock_unlock (__libc_setlocale_lock
);
833 /* Return the untranslated MSGID. */
835 FREE_BLOCKS (block_list
);
836 gl_rwlock_unlock (_nl_state_lock
);
838 __libc_rwlock_unlock (__libc_setlocale_lock
);
843 extern void _nl_log_untranslated (const char *logfilename
,
844 const char *domainname
,
845 const char *msgid1
, const char *msgid2
,
847 const char *logfilename
= getenv ("GETTEXT_LOG_UNTRANSLATED");
849 if (logfilename
!= NULL
&& logfilename
[0] != '\0')
850 _nl_log_untranslated (logfilename
, domainname
, msgid1
, msgid2
, plural
);
853 __set_errno (saved_errno
);
856 /* Use the Germanic plural rule. */
857 : n
== 1 ? (char *) msgid1
: (char *) msgid2
);
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). */
868 _nl_find_msg (struct loaded_l10nfile
*domain_file
,
869 struct binding
*domainbinding
, const char *encoding
,
873 _nl_find_msg (struct loaded_l10nfile
*domain_file
,
874 struct binding
*domainbinding
,
875 const char *msgid
, int convert
,
879 struct loaded_domain
*domain
;
885 if (domain_file
->decided
<= 0)
886 _nl_load_domain (domain_file
, domainbinding
);
888 if (domain_file
->data
== NULL
)
891 domain
= (struct loaded_domain
*) domain_file
->data
;
893 nstrings
= domain
->nstrings
;
895 /* Locate the MSGID and its translation. */
896 if (domain
->hash_tab
!= NULL
)
898 /* Use the hashing table. */
899 nls_uint32 len
= strlen (msgid
);
900 nls_uint32 hash_val
= __hash_string (msgid
);
901 nls_uint32 idx
= hash_val
% domain
->hash_size
;
902 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
907 W (domain
->must_swap_hash_tab
, domain
->hash_tab
[idx
]);
910 /* Hash table entry is empty. */
915 /* Compare msgid with the original string at index nstr.
916 We compare the lengths with >=, not ==, because plural entries
917 are represented by strings with an embedded NUL. */
919 ? W (domain
->must_swap
, domain
->orig_tab
[nstr
].length
) >= len
921 domain
->data
+ W (domain
->must_swap
,
922 domain
->orig_tab
[nstr
].offset
))
924 : domain
->orig_sysdep_tab
[nstr
- nstrings
].length
> len
926 domain
->orig_sysdep_tab
[nstr
- nstrings
].pointer
)
933 if (idx
>= domain
->hash_size
- incr
)
934 idx
-= domain
->hash_size
- incr
;
942 /* Try the default method: binary search in the sorted array of
952 act
= (bottom
+ top
) / 2;
953 cmp_val
= strcmp (msgid
, (domain
->data
954 + W (domain
->must_swap
,
955 domain
->orig_tab
[act
].offset
)));
958 else if (cmp_val
> 0)
963 /* No translation was 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. */
973 (domain
->data
+ W (domain
->must_swap
, domain
->trans_tab
[act
].offset
));
974 resultlen
= W (domain
->must_swap
, domain
->trans_tab
[act
].length
) + 1;
978 result
= (char *) domain
->trans_sysdep_tab
[act
- nstrings
].pointer
;
979 resultlen
= domain
->trans_sysdep_tab
[act
- nstrings
].length
;
982 #if defined _LIBC || HAVE_ICONV
983 # ifdef IN_LIBGLOCALE
984 if (encoding
!= NULL
)
989 /* We are supposed to do a conversion. */
990 # ifndef IN_LIBGLOCALE
991 const char *encoding
= get_output_charset (domainbinding
);
994 struct converted_domain
*convd
;
997 /* Protect against reallocation of the table. */
998 gl_rwlock_rdlock (domain
->conversions_lock
);
1000 /* Search whether a table with converted translations for this
1001 encoding has already been allocated. */
1002 nconversions
= domain
->nconversions
;
1005 for (i
= nconversions
; i
> 0; )
1008 if (strcmp (domain
->conversions
[i
].encoding
, encoding
) == 0)
1010 convd
= &domain
->conversions
[i
];
1015 gl_rwlock_unlock (domain
->conversions_lock
);
1019 /* We have to allocate a new conversions table. */
1020 gl_rwlock_wrlock (domain
->conversions_lock
);
1021 nconversions
= domain
->nconversions
;
1023 /* Maybe in the meantime somebody added the translation.
1025 for (i
= nconversions
; i
> 0; )
1028 if (strcmp (domain
->conversions
[i
].encoding
, encoding
) == 0)
1030 convd
= &domain
->conversions
[i
];
1036 /* Allocate a table for the converted translations for this
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
)));
1045 if (__builtin_expect (new_conversions
== NULL
, 0))
1047 /* Nothing we can do, no more memory. We cannot use the
1048 translation because it might be encoded incorrectly. */
1050 gl_rwlock_unlock (domain
->conversions_lock
);
1054 domain
->conversions
= new_conversions
;
1056 /* Copy the 'encoding' string to permanent storage. */
1057 encoding
= strdup (encoding
);
1058 if (__builtin_expect (encoding
== NULL
, 0))
1059 /* Nothing we can do, no more memory. We cannot use the
1060 translation because it might be encoded incorrectly. */
1063 convd
= &new_conversions
[nconversions
];
1064 convd
->encoding
= encoding
;
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. */
1072 convd
->conv
= (__gconv_t
) -1;
1075 convd
->conv
= (iconv_t
) -1;
1080 size_t nullentrylen
;
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. */
1086 # ifdef IN_LIBGLOCALE
1087 _nl_find_msg (domain_file
, domainbinding
, NULL
, "",
1090 _nl_find_msg (domain_file
, domainbinding
, "", 0, &nullentrylen
);
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))
1098 if (nullentry
!= NULL
)
1100 const char *charsetstr
;
1102 charsetstr
= strstr (nullentry
, "charset=");
1103 if (charsetstr
!= NULL
)
1107 const char *outcharset
;
1109 charsetstr
+= strlen ("charset=");
1110 len
= strcspn (charsetstr
, " \t\n");
1112 charset
= (char *) alloca (len
+ 1);
1113 # if defined _LIBC || HAVE_MEMPCPY
1114 *((char *) mempcpy (charset
, charsetstr
, len
)) = '\0';
1116 memcpy (charset
, charsetstr
, len
);
1117 charset
[len
] = '\0';
1120 outcharset
= encoding
;
1123 /* We always want to use transliteration. */
1124 outcharset
= norm_add_slashes (outcharset
, "TRANSLIT");
1125 charset
= norm_add_slashes (charset
, "");
1126 int r
= __gconv_open (outcharset
, charset
, &convd
->conv
,
1127 GCONV_AVOID_NOCONV
);
1128 if (__builtin_expect (r
!= __GCONV_OK
, 0))
1130 /* If the output encoding is the same there is
1131 nothing to do. Otherwise do not use the
1132 translation at all. */
1133 if (__builtin_expect (r
!= __GCONV_NULCONV
, 1))
1135 gl_rwlock_unlock (domain
->conversions_lock
);
1136 free ((char *) encoding
);
1140 convd
->conv
= (__gconv_t
) -1;
1144 /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5,
1145 we want to use transliteration. */
1146 # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \
1147 && !defined __UCLIBC__) \
1148 || _LIBICONV_VERSION >= 0x0105
1149 if (strchr (outcharset
, '/') == NULL
)
1153 len
= strlen (outcharset
);
1154 tmp
= (char *) alloca (len
+ 10 + 1);
1155 memcpy (tmp
, outcharset
, len
);
1156 memcpy (tmp
+ len
, "//TRANSLIT", 10 + 1);
1159 convd
->conv
= iconv_open (outcharset
, charset
);
1165 convd
->conv
= iconv_open (outcharset
, charset
);
1173 convd
->conv_tab
= NULL
;
1174 /* Here domain->conversions is still == new_conversions. */
1175 domain
->nconversions
++;
1179 gl_rwlock_unlock (domain
->conversions_lock
);
1184 convd
->conv
!= (__gconv_t
) -1
1187 convd
->conv
!= (iconv_t
) -1
1192 /* We are supposed to do a conversion. First allocate an
1193 appropriate table with the same structure as the table
1194 of translations in the file, where we can put the pointers
1195 to the converted strings in.
1196 There is a slight complication with plural entries. They
1197 are represented by consecutive NUL terminated strings. We
1198 handle this case by converting RESULTLEN bytes, including
1201 /* This lock primarily protects the memory management variables
1202 freemem, freemem_size. It also protects write accesses to
1203 convd->conv_tab. It's not worth using a separate lock (such
1204 as domain->conversions_lock) for this purpose, because when
1205 modifying convd->conv_tab, we also need to lock freemem,
1206 freemem_size for most of the time. */
1207 __libc_lock_define_initialized (static, lock
)
1209 if (__builtin_expect (convd
->conv_tab
== NULL
, 0))
1211 __libc_lock_lock (lock
);
1212 if (convd
->conv_tab
== NULL
)
1215 (char **) calloc (nstrings
+ domain
->n_sysdep_strings
,
1217 if (convd
->conv_tab
!= NULL
)
1218 goto not_translated_yet
;
1219 /* Mark that we didn't succeed allocating a table. */
1220 convd
->conv_tab
= (char **) -1;
1222 __libc_lock_unlock (lock
);
1225 if (__builtin_expect (convd
->conv_tab
== (char **) -1, 0))
1226 /* Nothing we can do, no more memory. We cannot use the
1227 translation because it might be encoded incorrectly. */
1230 if (convd
->conv_tab
[act
] == NULL
)
1232 /* We haven't used this string so far, so it is not
1233 translated yet. Do this now. */
1234 /* We use a bit more efficient memory handling.
1235 We allocate always larger blocks which get used over
1236 time. This is faster than many small allocations. */
1237 # define INITIAL_BLOCK_SIZE 4080
1238 static unsigned char *freemem
;
1239 static size_t freemem_size
;
1241 const unsigned char *inbuf
;
1242 unsigned char *outbuf
;
1245 transmem_block_t
*transmem_list
;
1248 __libc_lock_lock (lock
);
1251 inbuf
= (const unsigned char *) result
;
1252 outbuf
= freemem
+ sizeof (size_t);
1254 transmem_list
= NULL
;
1260 transmem_block_t
*newmem
;
1262 size_t non_reversible
;
1265 if (freemem_size
< sizeof (size_t))
1266 goto resize_freemem
;
1268 res
= __gconv (convd
->conv
,
1269 &inbuf
, inbuf
+ resultlen
,
1271 outbuf
+ freemem_size
- sizeof (size_t),
1274 if (res
== __GCONV_OK
|| res
== __GCONV_EMPTY_INPUT
)
1277 if (res
!= __GCONV_FULL_OUTPUT
)
1279 /* We should not use the translation at all, it
1280 is incorrectly encoded. */
1281 __libc_lock_unlock (lock
);
1285 inbuf
= (const unsigned char *) result
;
1288 const char *inptr
= (const char *) inbuf
;
1289 size_t inleft
= resultlen
;
1290 char *outptr
= (char *) outbuf
;
1293 if (freemem_size
< sizeof (size_t))
1294 goto resize_freemem
;
1296 outleft
= freemem_size
- sizeof (size_t);
1297 if (iconv (convd
->conv
,
1298 (ICONV_CONST
char **) &inptr
, &inleft
,
1302 outbuf
= (unsigned char *) outptr
;
1307 __libc_lock_unlock (lock
);
1314 /* We must allocate a new buffer or resize the old one. */
1315 if (malloc_count
> 0)
1318 freemem_size
= malloc_count
* INITIAL_BLOCK_SIZE
;
1319 newmem
= (transmem_block_t
*) realloc (transmem_list
,
1323 transmem_list
= newmem
;
1326 struct transmem_list
*old
= transmem_list
;
1328 transmem_list
= transmem_list
->next
;
1336 freemem_size
= INITIAL_BLOCK_SIZE
;
1337 newmem
= (transmem_block_t
*) malloc (freemem_size
);
1341 /* Add the block to the list of blocks we have to free
1343 newmem
->next
= transmem_list
;
1344 transmem_list
= newmem
;
1346 /* Fall through and return -1. */
1349 if (__builtin_expect (newmem
== NULL
, 0))
1353 __libc_lock_unlock (lock
);
1358 freemem
= (unsigned char *) newmem
->data
;
1359 freemem_size
-= offsetof (struct transmem_list
, data
);
1361 transmem_list
= newmem
;
1365 outbuf
= freemem
+ sizeof (size_t);
1368 /* We have now in our buffer a converted string. Put this
1369 into the table of conversions. */
1370 *(size_t *) freemem
= outbuf
- freemem
- sizeof (size_t);
1371 convd
->conv_tab
[act
] = (char *) freemem
;
1372 /* Shrink freemem, but keep it aligned. */
1373 freemem_size
-= outbuf
- freemem
;
1375 freemem
+= freemem_size
& (alignof (size_t) - 1);
1376 freemem_size
= freemem_size
& ~ (alignof (size_t) - 1);
1378 __libc_lock_unlock (lock
);
1381 /* Now convd->conv_tab[act] contains the translation of all
1382 the plural variants. */
1383 result
= convd
->conv_tab
[act
] + sizeof (size_t);
1384 resultlen
= *(size_t *) convd
->conv_tab
[act
];
1388 /* The result string is converted. */
1390 #endif /* _LIBC || HAVE_ICONV */
1392 *lengthp
= resultlen
;
1397 /* Look up a plural variant. */
1399 plural_lookup (struct loaded_l10nfile
*domain
, unsigned long int n
,
1400 const char *translation
, size_t translation_len
)
1402 struct loaded_domain
*domaindata
= (struct loaded_domain
*) domain
->data
;
1403 unsigned long int index
;
1406 index
= plural_eval (domaindata
->plural
, n
);
1407 if (index
>= domaindata
->nplurals
)
1408 /* This should never happen. It means the plural expression and the
1409 given maximum value do not match. */
1412 /* Skip INDEX strings at TRANSLATION. */
1417 p
= __rawmemchr (p
, '\0');
1419 p
= strchr (p
, '\0');
1421 /* And skip over the NUL byte. */
1424 if (p
>= translation
+ translation_len
)
1425 /* This should never happen. It means the plural expression
1426 evaluated to a value larger than the number of variants
1427 available for MSGID1. */
1428 return (char *) translation
;
1434 /* Return string representation of locale CATEGORY. */
1436 category_to_name (int category
)
1444 retval
= "LC_COLLATE";
1449 retval
= "LC_CTYPE";
1454 retval
= "LC_MONETARY";
1459 retval
= "LC_NUMERIC";
1469 retval
= "LC_MESSAGES";
1474 retval
= "LC_RESPONSE";
1479 /* This might not make sense but is perhaps better than any other
1485 /* If you have a better idea for a default value let me know. */
1493 /* Guess value of current locale from value of the environment variables
1494 or system-dependent defaults. */
1496 #ifdef IN_LIBGLOCALE
1497 guess_category_value (int category
, const char *categoryname
,
1501 guess_category_value (int category
, const char *categoryname
)
1504 const char *language
;
1505 #ifndef IN_LIBGLOCALE
1508 const char *language_default
;
1509 int locale_defaulted
;
1513 /* We use the settings in the following order:
1514 1. The value of the environment variable 'LANGUAGE'. This is a GNU
1515 extension. Its value can be a colon-separated list of locale names.
1516 2. The value of the environment variable 'LC_ALL', 'LC_xxx', or 'LANG'.
1517 More precisely, the first among these that is set to a non-empty value.
1518 This is how POSIX specifies it. The value is a single locale name.
1519 3. A system-dependent preference list of languages. Its value can be a
1520 colon-separated list of locale names.
1521 4. A system-dependent default locale name.
1523 - System-dependent settings can be overridden by environment variables.
1524 - If the system provides both a list of languages and a default locale,
1525 the former is used. */
1527 #ifndef IN_LIBGLOCALE
1528 /* Fetch the locale name, through the POSIX method of looking to `LC_ALL',
1529 `LC_xxx', and `LANG'. On some systems this can be done by the
1530 `setlocale' function itself. */
1532 locale
= __current_locale_name (category
);
1534 locale_defaulted
= 0;
1536 locale
= _nl_locale_name_thread_unsafe (category
, categoryname
);
1540 locale
= _nl_locale_name_posix (category
, categoryname
);
1543 locale
= _nl_locale_name_default ();
1544 locale_defaulted
= 1;
1550 /* Ignore LANGUAGE and its system-dependent analogon if the locale is set
1552 1. "C" locale usually uses the ASCII encoding, and most international
1553 messages use non-ASCII characters. These characters get displayed
1554 as question marks (if using glibc's iconv()) or as invalid 8-bit
1555 characters (because other iconv()s refuse to convert most non-ASCII
1556 characters to ASCII). In any case, the output is ugly.
1557 2. The precise output of some programs in the "C" locale is specified
1558 by POSIX and should not depend on environment variables like
1559 "LANGUAGE" or system-dependent information. We allow such programs
1560 to use gettext(). */
1561 if (strcmp (locale
, "C") == 0)
1564 /* The highest priority value is the value of the 'LANGUAGE' environment
1566 language
= getenv ("LANGUAGE");
1567 if (language
!= NULL
&& language
[0] != '\0')
1569 #if !defined IN_LIBGLOCALE && !defined _LIBC
1570 /* The next priority value is the locale name, if not defaulted. */
1571 if (locale_defaulted
)
1573 /* The next priority value is the default language preferences list. */
1574 language_default
= _nl_language_preferences_default ();
1575 if (language_default
!= NULL
)
1576 return language_default
;
1578 /* The least priority value is the locale name, if defaulted. */
1583 #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE
1584 /* Returns the output charset. */
1586 get_output_charset (struct binding
*domainbinding
)
1588 /* The output charset should normally be determined by the locale. But
1589 sometimes the locale is not used or not correctly set up, so we provide
1590 a possibility for the user to override this: the OUTPUT_CHARSET
1591 environment variable. Moreover, the value specified through
1592 bind_textdomain_codeset overrides both. */
1593 if (domainbinding
!= NULL
&& domainbinding
->codeset
!= NULL
)
1594 return domainbinding
->codeset
;
1597 /* For speed reasons, we look at the value of OUTPUT_CHARSET only
1598 once. This is a user variable that is not supposed to change
1599 during a program run. */
1600 static char *output_charset_cache
;
1601 static int output_charset_cached
;
1603 if (!output_charset_cached
)
1605 const char *value
= getenv ("OUTPUT_CHARSET");
1607 if (value
!= NULL
&& value
[0] != '\0')
1609 size_t len
= strlen (value
) + 1;
1610 char *value_copy
= (char *) malloc (len
);
1612 if (value_copy
!= NULL
)
1613 memcpy (value_copy
, value
, len
);
1614 output_charset_cache
= value_copy
;
1616 output_charset_cached
= 1;
1619 if (output_charset_cache
!= NULL
)
1620 return output_charset_cache
;
1624 return _NL_CURRENT (LC_CTYPE
, CODESET
);
1627 return locale_charset ();
1635 /* @@ begin of epilog @@ */
1637 /* We don't want libintl.a to depend on any other library. So we
1638 avoid the non-standard function stpcpy. In GNU C Library this
1639 function is available, though. Also allow the symbol HAVE_STPCPY
1641 #if !_LIBC && !HAVE_STPCPY
1643 stpcpy (char *dest
, const char *src
)
1645 while ((*dest
++ = *src
++) != '\0')
1651 #if !_LIBC && !HAVE_MEMPCPY
1653 mempcpy (void *dest
, const void *src
, size_t n
)
1655 return (void *) ((char *) memcpy (dest
, src
, n
) + n
);
1659 #if !_LIBC && !HAVE_TSEARCH
1660 # include "tsearch.c"
1665 /* If we want to free all resources we have to do some work at
1667 libc_freeres_fn (free_mem
)
1671 while (_nl_domain_bindings
!= NULL
)
1673 struct binding
*oldp
= _nl_domain_bindings
;
1674 _nl_domain_bindings
= _nl_domain_bindings
->next
;
1675 if (oldp
->dirname
!= _nl_default_dirname
)
1676 /* Yes, this is a pointer comparison. */
1677 free (oldp
->dirname
);
1678 free (oldp
->codeset
);
1682 if (_nl_current_default_domain
!= _nl_default_default_domain
)
1683 /* Yes, again a pointer comparison. */
1684 free ((char *) _nl_current_default_domain
);
1686 /* Remove the search tree with the known translations. */
1687 __tdestroy (root
, free
);
1690 while (transmem_list
!= NULL
)
1692 old
= transmem_list
;
1693 transmem_list
= transmem_list
->next
;