1 /* Implementation of the internal dcigettext function.
2 Copyright (C) 1995-2014 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)
64 #if defined HAVE_UNISTD_H || defined _LIBC
71 /* Guess whether integer division by zero raises signal SIGFPE.
72 Set to 1 only if you know for sure. In case of doubt, set to 0. */
73 # if defined __alpha__ || defined __arm__ || defined __i386__ \
74 || defined __m68k__ || defined __s390__
75 # define INTDIV0_RAISES_SIGFPE 1
77 # define INTDIV0_RAISES_SIGFPE 0
80 #if !INTDIV0_RAISES_SIGFPE
84 #if defined HAVE_SYS_PARAM_H || defined _LIBC
85 # include <sys/param.h>
89 # include "localcharset.h"
93 #include "plural-exp.h"
100 # include "libgnuintl.h"
102 #include "hash-string.h"
104 /* Handle multi-threaded applications. */
106 # include <bits/libc-lock.h>
107 # define gl_rwlock_define_initialized __libc_rwlock_define_initialized
108 # define gl_rwlock_rdlock __libc_rwlock_rdlock
109 # define gl_rwlock_wrlock __libc_rwlock_wrlock
110 # define gl_rwlock_unlock __libc_rwlock_unlock
115 /* Alignment of types. */
116 #if defined __GNUC__ && __GNUC__ >= 2
117 # define alignof(TYPE) __alignof__ (TYPE)
119 # define alignof(TYPE) \
120 ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2)
123 /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */
125 # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
128 /* @@ end of prolog @@ */
131 /* Rename the non ANSI C functions. This is required by the standard
132 because some ANSI C functions will require linking with this object
133 file and the name space must not be polluted. */
134 # define getcwd __getcwd
136 # define stpcpy __stpcpy
138 # define tfind __tfind
140 # if !defined HAVE_GETCWD
142 # define getcwd(buf, max) getwd (buf)
145 # define getcwd(buf, max) (getcwd) (buf, max, 0)
151 static char *stpcpy (char *dest
, const char *src
);
153 # ifndef HAVE_MEMPCPY
154 static void *mempcpy (void *dest
, const void *src
, size_t n
);
158 /* Use a replacement if the system does not provide the `tsearch' function
160 #if defined HAVE_TSEARCH || defined _LIBC
163 # define tsearch libintl_tsearch
164 # define tfind libintl_tfind
165 # define tdelete libintl_tdelete
166 # define twalk libintl_twalk
167 # include "tsearch.h"
171 # define tsearch __tsearch
174 /* Amount to increase buffer size by in each try. */
177 /* The following is from pathmax.h. */
178 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
179 PATH_MAX but might cause redefinition warnings when sys/param.h is
180 later included (as on MORE/BSD 4.3). */
181 #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__)
185 #ifndef _POSIX_PATH_MAX
186 # define _POSIX_PATH_MAX 255
189 #if !defined PATH_MAX && defined _PC_PATH_MAX
190 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
193 /* Don't include sys/param.h if it already has been. */
194 #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN
195 # include <sys/param.h>
198 #if !defined PATH_MAX && defined MAXPATHLEN
199 # define PATH_MAX MAXPATHLEN
203 # define PATH_MAX _POSIX_PATH_MAX
207 ISSLASH(C) tests whether C is a directory separator character.
208 IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
209 it may be concatenated to a directory pathname.
210 IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
212 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
213 /* Win32, Cygwin, OS/2, DOS */
214 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
215 # define HAS_DEVICE(P) \
216 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
218 # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
219 # define IS_PATH_WITH_DIR(P) \
220 (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
223 # define ISSLASH(C) ((C) == '/')
224 # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
225 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
228 /* Whether to support different locales in different threads. */
229 #if defined _LIBC || HAVE_USELOCALE || defined IN_LIBGLOCALE
230 # define HAVE_PER_THREAD_LOCALE
233 /* This is the type used for the search tree where known translations
235 struct known_translation_t
237 /* Domain in which to search. */
238 const char *domainname
;
243 #ifdef HAVE_PER_THREAD_LOCALE
244 /* Name of the relevant locale category, or "" for the global locale. */
245 const char *localename
;
249 /* The character encoding. */
250 const char *encoding
;
253 /* State of the catalog counter at the point the string was found. */
256 /* Catalog where the string was found. */
257 struct loaded_l10nfile
*domain
;
259 /* And finally the translation. */
260 const char *translation
;
261 size_t translation_length
;
263 /* Pointer to the string in question. */
266 char appended
[ZERO
]; /* used if domain != NULL */
267 const char *ptr
; /* used if domain == NULL */
272 gl_rwlock_define_initialized (static, tree_lock
)
274 /* Root of the search tree with known translations. */
277 /* Function to compare two entries in the table of known translations. */
279 transcmp (const void *p1
, const void *p2
)
281 const struct known_translation_t
*s1
;
282 const struct known_translation_t
*s2
;
285 s1
= (const struct known_translation_t
*) p1
;
286 s2
= (const struct known_translation_t
*) p2
;
288 result
= strcmp (s1
->domain
!= NULL
? s1
->msgid
.appended
: s1
->msgid
.ptr
,
289 s2
->domain
!= NULL
? s2
->msgid
.appended
: s2
->msgid
.ptr
);
292 result
= strcmp (s1
->domainname
, s2
->domainname
);
295 #ifdef HAVE_PER_THREAD_LOCALE
296 result
= strcmp (s1
->localename
, s2
->localename
);
301 result
= strcmp (s1
->encoding
, s2
->encoding
);
304 /* We compare the category last (though this is the cheapest
305 operation) since it is hopefully always the same (namely
307 result
= s1
->category
- s2
->category
;
315 /* Name of the default domain used for gettext(3) prior any call to
316 textdomain(3). The default value for this is "messages". */
317 const char _nl_default_default_domain
[] attribute_hidden
= "messages";
319 #ifndef IN_LIBGLOCALE
320 /* Value used as the default domain for gettext(3). */
321 const char *_nl_current_default_domain attribute_hidden
322 = _nl_default_default_domain
;
325 /* Contains the default location of the message catalogs. */
327 extern const char _nl_default_dirname
[];
330 extern const char _nl_default_dirname
[];
331 libc_hidden_proto (_nl_default_dirname
)
333 const char _nl_default_dirname
[] = LOCALEDIR
;
335 libc_hidden_data_def (_nl_default_dirname
)
339 #ifndef IN_LIBGLOCALE
340 /* List with bindings of specific domains created by bindtextdomain()
342 struct binding
*_nl_domain_bindings
;
345 /* Prototypes for local functions. */
346 static char *plural_lookup (struct loaded_l10nfile
*domain
,
348 const char *translation
, size_t translation_len
)
352 static const char *guess_category_value (int category
,
353 const char *categoryname
,
354 const char *localename
)
357 static const char *guess_category_value (int category
,
358 const char *categoryname
)
363 # include "../locale/localeinfo.h"
364 # define category_to_name(category) \
365 _nl_category_names.str + _nl_category_name_idxs[category]
367 static const char *category_to_name (int category
) internal_function
;
369 #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE
370 static const char *get_output_charset (struct binding
*domainbinding
)
375 /* For those losing systems which don't have `alloca' we have to add
376 some additional code emulating it. */
378 /* Nothing has to be done. */
379 # define freea(p) /* nothing */
380 # define ADD_BLOCK(list, address) /* nothing */
381 # define FREE_BLOCKS(list) /* nothing */
386 struct block_list
*next
;
388 # define ADD_BLOCK(list, addr) \
390 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
391 /* If we cannot get a free block we cannot add the new element to \
393 if (newp != NULL) { \
394 newp->address = (addr); \
395 newp->next = (list); \
399 # define FREE_BLOCKS(list) \
401 while (list != NULL) { \
402 struct block_list *old = list; \
404 free (old->address); \
409 # define alloca(size) (malloc (size))
410 # define freea(p) free (p)
411 #endif /* have alloca */
415 /* List of blocks allocated for translations. */
416 typedef struct transmem_list
418 struct transmem_list
*next
;
421 static struct transmem_list
*transmem_list
;
423 typedef unsigned char transmem_block_t
;
427 /* Names for the libintl functions are a problem. They must not clash
428 with existing names and they should follow ANSI C. But this source
429 code is also used in GNU C Library where the names have a __
430 prefix. So we have to make a difference here. */
432 # define DCIGETTEXT __dcigettext
434 # define DCIGETTEXT libintl_dcigettext
437 /* Lock variable to protect the global data in the gettext implementation. */
438 gl_rwlock_define_initialized (, _nl_state_lock attribute_hidden
)
440 /* Checking whether the binaries runs SUID must be done and glibc provides
441 easier methods therefore we make a difference here. */
443 # define ENABLE_SECURE __libc_enable_secure
444 # define DETERMINE_SECURE
452 # ifndef HAVE_GETEUID
453 # define geteuid() getuid()
455 # ifndef HAVE_GETEGID
456 # define getegid() getgid()
458 static int enable_secure
;
459 # define ENABLE_SECURE (enable_secure == 1)
460 # define DETERMINE_SECURE \
461 if (enable_secure == 0) \
463 if (getuid () != geteuid () || getgid () != getegid ()) \
466 enable_secure = -1; \
470 /* Get the function to evaluate the plural expression. */
471 #include "eval-plural.h"
473 /* Look up MSGID in the DOMAINNAME message catalog for the current
474 CATEGORY locale and, if PLURAL is nonzero, search over string
475 depending on the plural form determined by N. */
478 gl_dcigettext (const char *domainname
,
479 const char *msgid1
, const char *msgid2
,
480 int plural
, unsigned long int n
,
482 const char *localename
, const char *encoding
)
485 DCIGETTEXT (const char *domainname
, const char *msgid1
, const char *msgid2
,
486 int plural
, unsigned long int n
, int category
)
490 struct block_list
*block_list
= NULL
;
492 struct loaded_l10nfile
*domain
;
493 struct binding
*binding
;
494 const char *categoryname
;
495 const char *categoryvalue
;
502 struct known_translation_t search
;
503 struct known_translation_t
**foundp
= NULL
;
504 #if defined HAVE_PER_THREAD_LOCALE && !defined IN_LIBGLOCALE
505 const char *localename
;
507 size_t domainname_len
;
509 /* If no real MSGID is given return NULL. */
514 if (category
< 0 || category
>= __LC_LAST
|| category
== LC_ALL
)
518 /* Use the Germanic plural rule. */
519 : n
== 1 ? (char *) msgid1
: (char *) msgid2
);
522 /* Preserve the `errno' value. */
526 __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden
)
527 __libc_rwlock_rdlock (__libc_setlocale_lock
);
530 gl_rwlock_rdlock (_nl_state_lock
);
532 /* If DOMAINNAME is NULL, we are interested in the default domain. If
533 CATEGORY is not LC_MESSAGES this might not make much sense but the
534 definition left this undefined. */
535 if (domainname
== NULL
)
536 domainname
= _nl_current_default_domain
;
538 /* OS/2 specific: backward compatibility with older libintl versions */
539 #ifdef LC_MESSAGES_COMPAT
540 if (category
== LC_MESSAGES_COMPAT
)
541 category
= LC_MESSAGES
;
544 /* Try to find the translation among those which we found at
546 search
.domain
= NULL
;
547 search
.msgid
.ptr
= msgid1
;
548 search
.domainname
= domainname
;
549 search
.category
= category
;
550 #ifdef HAVE_PER_THREAD_LOCALE
551 # ifndef IN_LIBGLOCALE
553 localename
= strdupa (__current_locale_name (category
));
555 categoryname
= category_to_name (category
);
556 # define CATEGORYNAME_INITIALIZED
557 localename
= _nl_locale_name_thread_unsafe (category
, categoryname
);
558 if (localename
== NULL
)
562 search
.localename
= localename
;
563 # ifdef IN_LIBGLOCALE
564 search
.encoding
= encoding
;
567 /* Since tfind/tsearch manage a balanced tree, concurrent tfind and
568 tsearch calls can be fatal. */
569 gl_rwlock_rdlock (tree_lock
);
571 foundp
= (struct known_translation_t
**) tfind (&search
, &root
, transcmp
);
573 gl_rwlock_unlock (tree_lock
);
575 if (foundp
!= NULL
&& (*foundp
)->counter
== _nl_msg_cat_cntr
)
577 /* Now deal with plural. */
579 retval
= plural_lookup ((*foundp
)->domain
, n
, (*foundp
)->translation
,
580 (*foundp
)->translation_length
);
582 retval
= (char *) (*foundp
)->translation
;
584 gl_rwlock_unlock (_nl_state_lock
);
586 __libc_rwlock_unlock (__libc_setlocale_lock
);
588 __set_errno (saved_errno
);
593 /* See whether this is a SUID binary or not. */
596 /* First find matching binding. */
598 /* We can use a trivial binding, since _nl_find_msg will ignore it anyway,
599 and _nl_load_domain and _nl_find_domain just pass it through. */
601 dirname
= bindtextdomain (domainname
, NULL
);
603 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
605 int compare
= strcmp (domainname
, binding
->domainname
);
611 /* It is not in the list. */
618 dirname
= _nl_default_dirname
;
621 dirname
= binding
->dirname
;
623 if (!IS_ABSOLUTE_PATH (dirname
))
625 /* We have a relative path. Make it absolute now. */
626 size_t dirname_len
= strlen (dirname
) + 1;
628 char *resolved_dirname
;
631 path_max
= (unsigned int) PATH_MAX
;
632 path_max
+= 2; /* The getcwd docs say to do this. */
636 resolved_dirname
= (char *) alloca (path_max
+ dirname_len
);
637 ADD_BLOCK (block_list
, tmp_dirname
);
640 ret
= getcwd (resolved_dirname
, path_max
);
641 if (ret
!= NULL
|| errno
!= ERANGE
)
644 path_max
+= path_max
/ 2;
645 path_max
+= PATH_INCR
;
649 /* We cannot get the current working directory. Don't signal an
650 error but simply return the default string. */
651 goto return_untranslated
;
653 stpcpy (stpcpy (strchr (resolved_dirname
, '\0'), "/"), dirname
);
654 dirname
= resolved_dirname
;
656 #ifndef IN_LIBGLOCALE
660 /* Now determine the symbolic name of CATEGORY and its value. */
661 #ifndef CATEGORYNAME_INITIALIZED
662 categoryname
= category_to_name (category
);
665 categoryvalue
= guess_category_value (category
, categoryname
, localename
);
667 categoryvalue
= guess_category_value (category
, categoryname
);
670 domainname_len
= strlen (domainname
);
671 xdomainname
= (char *) alloca (strlen (categoryname
)
672 + domainname_len
+ 5);
673 ADD_BLOCK (block_list
, xdomainname
);
675 stpcpy ((char *) mempcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
676 domainname
, domainname_len
),
679 /* Creating working area. */
680 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
681 ADD_BLOCK (block_list
, single_locale
);
684 /* Search for the given string. This is a loop because we perhaps
685 got an ordered list of languages to consider for the translation. */
688 /* Make CATEGORYVALUE point to the next element of the list. */
689 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
691 if (categoryvalue
[0] == '\0')
693 /* The whole contents of CATEGORYVALUE has been searched but
694 no valid entry has been found. We solve this situation
695 by implicitly appending a "C" entry, i.e. no translation
697 single_locale
[0] = 'C';
698 single_locale
[1] = '\0';
702 char *cp
= single_locale
;
703 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
704 *cp
++ = *categoryvalue
++;
707 /* When this is a SUID binary we must not allow accessing files
708 outside the dedicated directories. */
709 if (ENABLE_SECURE
&& IS_PATH_WITH_DIR (single_locale
))
710 /* Ingore this entry. */
714 /* If the current locale value is C (or POSIX) we don't load a
715 domain. Return the MSGID. */
716 if (strcmp (single_locale
, "C") == 0
717 || strcmp (single_locale
, "POSIX") == 0)
720 /* Find structure describing the message catalog matching the
721 DOMAINNAME and CATEGORY. */
722 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
, binding
);
726 #if defined IN_LIBGLOCALE
727 retval
= _nl_find_msg (domain
, binding
, encoding
, msgid1
, &retlen
);
729 retval
= _nl_find_msg (domain
, binding
, msgid1
, 1, &retlen
);
736 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
738 #if defined IN_LIBGLOCALE
739 retval
= _nl_find_msg (domain
->successor
[cnt
], binding
,
740 encoding
, msgid1
, &retlen
);
742 retval
= _nl_find_msg (domain
->successor
[cnt
], binding
,
746 /* Resource problems are not fatal, instead we return no
748 if (__builtin_expect (retval
== (char *) -1, 0))
749 goto return_untranslated
;
753 domain
= domain
->successor
[cnt
];
759 /* Returning -1 means that some resource problem exists
760 (likely memory) and that the strings could not be
761 converted. Return the original strings. */
762 if (__builtin_expect (retval
== (char *) -1, 0))
767 /* Found the translation of MSGID1 in domain DOMAIN:
768 starting at RETVAL, RETLEN bytes. */
769 FREE_BLOCKS (block_list
);
772 /* Create a new entry and add it to the search tree. */
775 struct known_translation_t
*newp
;
777 msgid_len
= strlen (msgid1
) + 1;
778 size
= offsetof (struct known_translation_t
, msgid
)
779 + msgid_len
+ domainname_len
+ 1;
780 #ifdef HAVE_PER_THREAD_LOCALE
781 size
+= strlen (localename
) + 1;
783 newp
= (struct known_translation_t
*) malloc (size
);
786 char *new_domainname
;
787 #ifdef HAVE_PER_THREAD_LOCALE
788 char *new_localename
;
792 (char *) mempcpy (newp
->msgid
.appended
, msgid1
,
794 memcpy (new_domainname
, domainname
, domainname_len
+ 1);
795 #ifdef HAVE_PER_THREAD_LOCALE
796 new_localename
= new_domainname
+ domainname_len
+ 1;
797 strcpy (new_localename
, localename
);
799 newp
->domainname
= new_domainname
;
800 newp
->category
= category
;
801 #ifdef HAVE_PER_THREAD_LOCALE
802 newp
->localename
= new_localename
;
805 newp
->encoding
= encoding
;
807 newp
->counter
= _nl_msg_cat_cntr
;
808 newp
->domain
= domain
;
809 newp
->translation
= retval
;
810 newp
->translation_length
= retlen
;
812 gl_rwlock_wrlock (tree_lock
);
814 /* Insert the entry in the search tree. */
815 foundp
= (struct known_translation_t
**)
816 tsearch (newp
, &root
, transcmp
);
818 gl_rwlock_unlock (tree_lock
);
821 || __builtin_expect (*foundp
!= newp
, 0))
822 /* The insert failed. */
828 /* We can update the existing entry. */
829 (*foundp
)->counter
= _nl_msg_cat_cntr
;
830 (*foundp
)->domain
= domain
;
831 (*foundp
)->translation
= retval
;
832 (*foundp
)->translation_length
= retlen
;
835 __set_errno (saved_errno
);
837 /* Now deal with plural. */
839 retval
= plural_lookup (domain
, n
, retval
, retlen
);
841 gl_rwlock_unlock (_nl_state_lock
);
843 __libc_rwlock_unlock (__libc_setlocale_lock
);
851 /* Return the untranslated MSGID. */
852 FREE_BLOCKS (block_list
);
853 gl_rwlock_unlock (_nl_state_lock
);
855 __libc_rwlock_unlock (__libc_setlocale_lock
);
860 extern void _nl_log_untranslated (const char *logfilename
,
861 const char *domainname
,
862 const char *msgid1
, const char *msgid2
,
864 const char *logfilename
= getenv ("GETTEXT_LOG_UNTRANSLATED");
866 if (logfilename
!= NULL
&& logfilename
[0] != '\0')
867 _nl_log_untranslated (logfilename
, domainname
, msgid1
, msgid2
, plural
);
870 __set_errno (saved_errno
);
873 /* Use the Germanic plural rule. */
874 : n
== 1 ? (char *) msgid1
: (char *) msgid2
);
878 /* Look up the translation of msgid within DOMAIN_FILE and DOMAINBINDING.
879 Return it if found. Return NULL if not found or in case of a conversion
880 failure (problem in the particular message catalog). Return (char *) -1
881 in case of a memory allocation failure during conversion (only if
882 ENCODING != NULL resp. CONVERT == true). */
886 _nl_find_msg (struct loaded_l10nfile
*domain_file
,
887 struct binding
*domainbinding
, const char *encoding
,
891 _nl_find_msg (struct loaded_l10nfile
*domain_file
,
892 struct binding
*domainbinding
,
893 const char *msgid
, int convert
,
897 struct loaded_domain
*domain
;
903 if (domain_file
->decided
<= 0)
904 _nl_load_domain (domain_file
, domainbinding
);
906 if (domain_file
->data
== NULL
)
909 domain
= (struct loaded_domain
*) domain_file
->data
;
911 nstrings
= domain
->nstrings
;
913 /* Locate the MSGID and its translation. */
914 if (domain
->hash_tab
!= NULL
)
916 /* Use the hashing table. */
917 nls_uint32 len
= strlen (msgid
);
918 nls_uint32 hash_val
= __hash_string (msgid
);
919 nls_uint32 idx
= hash_val
% domain
->hash_size
;
920 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
925 W (domain
->must_swap_hash_tab
, domain
->hash_tab
[idx
]);
928 /* Hash table entry is empty. */
933 /* Compare msgid with the original string at index nstr.
934 We compare the lengths with >=, not ==, because plural entries
935 are represented by strings with an embedded NUL. */
937 ? W (domain
->must_swap
, domain
->orig_tab
[nstr
].length
) >= len
939 domain
->data
+ W (domain
->must_swap
,
940 domain
->orig_tab
[nstr
].offset
))
942 : domain
->orig_sysdep_tab
[nstr
- nstrings
].length
> len
944 domain
->orig_sysdep_tab
[nstr
- nstrings
].pointer
)
951 if (idx
>= domain
->hash_size
- incr
)
952 idx
-= domain
->hash_size
- incr
;
960 /* Try the default method: binary search in the sorted array of
970 act
= (bottom
+ top
) / 2;
971 cmp_val
= strcmp (msgid
, (domain
->data
972 + W (domain
->must_swap
,
973 domain
->orig_tab
[act
].offset
)));
976 else if (cmp_val
> 0)
981 /* No translation was found. */
986 /* The translation was found at index ACT. If we have to convert the
987 string to use a different character set, this is the time. */
991 (domain
->data
+ W (domain
->must_swap
, domain
->trans_tab
[act
].offset
));
992 resultlen
= W (domain
->must_swap
, domain
->trans_tab
[act
].length
) + 1;
996 result
= (char *) domain
->trans_sysdep_tab
[act
- nstrings
].pointer
;
997 resultlen
= domain
->trans_sysdep_tab
[act
- nstrings
].length
;
1000 #if defined _LIBC || HAVE_ICONV
1001 # ifdef IN_LIBGLOCALE
1002 if (encoding
!= NULL
)
1007 /* We are supposed to do a conversion. */
1008 # ifndef IN_LIBGLOCALE
1009 const char *encoding
= get_output_charset (domainbinding
);
1011 size_t nconversions
;
1012 struct converted_domain
*convd
;
1015 /* Protect against reallocation of the table. */
1016 gl_rwlock_rdlock (domain
->conversions_lock
);
1018 /* Search whether a table with converted translations for this
1019 encoding has already been allocated. */
1020 nconversions
= domain
->nconversions
;
1023 for (i
= nconversions
; i
> 0; )
1026 if (strcmp (domain
->conversions
[i
].encoding
, encoding
) == 0)
1028 convd
= &domain
->conversions
[i
];
1033 gl_rwlock_unlock (domain
->conversions_lock
);
1037 /* We have to allocate a new conversions table. */
1038 gl_rwlock_wrlock (domain
->conversions_lock
);
1039 nconversions
= domain
->nconversions
;
1041 /* Maybe in the meantime somebody added the translation.
1043 for (i
= nconversions
; i
> 0; )
1046 if (strcmp (domain
->conversions
[i
].encoding
, encoding
) == 0)
1048 convd
= &domain
->conversions
[i
];
1054 /* Allocate a table for the converted translations for this
1056 struct converted_domain
*new_conversions
=
1057 (struct converted_domain
*)
1058 (domain
->conversions
!= NULL
1059 ? realloc (domain
->conversions
,
1060 (nconversions
+ 1) * sizeof (struct converted_domain
))
1061 : malloc ((nconversions
+ 1) * sizeof (struct converted_domain
)));
1063 if (__builtin_expect (new_conversions
== NULL
, 0))
1065 /* Nothing we can do, no more memory. We cannot use the
1066 translation because it might be encoded incorrectly. */
1068 gl_rwlock_unlock (domain
->conversions_lock
);
1072 domain
->conversions
= new_conversions
;
1074 /* Copy the 'encoding' string to permanent storage. */
1075 encoding
= strdup (encoding
);
1076 if (__builtin_expect (encoding
== NULL
, 0))
1077 /* Nothing we can do, no more memory. We cannot use the
1078 translation because it might be encoded incorrectly. */
1081 convd
= &new_conversions
[nconversions
];
1082 convd
->encoding
= encoding
;
1084 /* Find out about the character set the file is encoded with.
1085 This can be found (in textual form) in the entry "". If this
1086 entry does not exist or if this does not contain the 'charset='
1087 information, we will assume the charset matches the one the
1088 current locale and we don't have to perform any conversion. */
1090 convd
->conv
= (__gconv_t
) -1;
1093 convd
->conv
= (iconv_t
) -1;
1098 size_t nullentrylen
;
1100 /* Get the header entry. This is a recursion, but it doesn't
1101 reallocate domain->conversions because we pass
1102 encoding = NULL or convert = 0, respectively. */
1104 # ifdef IN_LIBGLOCALE
1105 _nl_find_msg (domain_file
, domainbinding
, NULL
, "",
1108 _nl_find_msg (domain_file
, domainbinding
, "", 0, &nullentrylen
);
1111 /* Resource problems are fatal. If we continue onwards we will
1112 only attempt to calloc a new conv_tab and fail later. */
1113 if (__builtin_expect (nullentry
== (char *) -1, 0))
1116 if (nullentry
!= NULL
)
1118 const char *charsetstr
;
1120 charsetstr
= strstr (nullentry
, "charset=");
1121 if (charsetstr
!= NULL
)
1125 const char *outcharset
;
1127 charsetstr
+= strlen ("charset=");
1128 len
= strcspn (charsetstr
, " \t\n");
1130 charset
= (char *) alloca (len
+ 1);
1131 # if defined _LIBC || HAVE_MEMPCPY
1132 *((char *) mempcpy (charset
, charsetstr
, len
)) = '\0';
1134 memcpy (charset
, charsetstr
, len
);
1135 charset
[len
] = '\0';
1138 outcharset
= encoding
;
1141 /* We always want to use transliteration. */
1142 outcharset
= norm_add_slashes (outcharset
, "TRANSLIT");
1143 charset
= norm_add_slashes (charset
, "");
1144 int r
= __gconv_open (outcharset
, charset
, &convd
->conv
,
1145 GCONV_AVOID_NOCONV
);
1146 if (__builtin_expect (r
!= __GCONV_OK
, 0))
1148 /* If the output encoding is the same there is
1149 nothing to do. Otherwise do not use the
1150 translation at all. */
1151 if (__builtin_expect (r
!= __GCONV_NULCONV
, 1))
1153 gl_rwlock_unlock (domain
->conversions_lock
);
1154 free ((char *) encoding
);
1158 convd
->conv
= (__gconv_t
) -1;
1162 /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5,
1163 we want to use transliteration. */
1164 # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \
1165 && !defined __UCLIBC__) \
1166 || _LIBICONV_VERSION >= 0x0105
1167 if (strchr (outcharset
, '/') == NULL
)
1171 len
= strlen (outcharset
);
1172 tmp
= (char *) alloca (len
+ 10 + 1);
1173 memcpy (tmp
, outcharset
, len
);
1174 memcpy (tmp
+ len
, "//TRANSLIT", 10 + 1);
1177 convd
->conv
= iconv_open (outcharset
, charset
);
1183 convd
->conv
= iconv_open (outcharset
, charset
);
1191 convd
->conv_tab
= NULL
;
1192 /* Here domain->conversions is still == new_conversions. */
1193 domain
->nconversions
++;
1197 gl_rwlock_unlock (domain
->conversions_lock
);
1202 convd
->conv
!= (__gconv_t
) -1
1205 convd
->conv
!= (iconv_t
) -1
1210 /* We are supposed to do a conversion. First allocate an
1211 appropriate table with the same structure as the table
1212 of translations in the file, where we can put the pointers
1213 to the converted strings in.
1214 There is a slight complication with plural entries. They
1215 are represented by consecutive NUL terminated strings. We
1216 handle this case by converting RESULTLEN bytes, including
1219 /* This lock primarily protects the memory management variables
1220 freemem, freemem_size. It also protects write accesses to
1221 convd->conv_tab. It's not worth using a separate lock (such
1222 as domain->conversions_lock) for this purpose, because when
1223 modifying convd->conv_tab, we also need to lock freemem,
1224 freemem_size for most of the time. */
1225 __libc_lock_define_initialized (static, lock
)
1227 if (__builtin_expect (convd
->conv_tab
== NULL
, 0))
1229 __libc_lock_lock (lock
);
1230 if (convd
->conv_tab
== NULL
)
1233 (char **) calloc (nstrings
+ domain
->n_sysdep_strings
,
1235 if (convd
->conv_tab
!= NULL
)
1236 goto not_translated_yet
;
1237 /* Mark that we didn't succeed allocating a table. */
1238 convd
->conv_tab
= (char **) -1;
1240 __libc_lock_unlock (lock
);
1243 if (__builtin_expect (convd
->conv_tab
== (char **) -1, 0))
1244 /* Nothing we can do, no more memory. We cannot use the
1245 translation because it might be encoded incorrectly. */
1248 if (convd
->conv_tab
[act
] == NULL
)
1250 /* We haven't used this string so far, so it is not
1251 translated yet. Do this now. */
1252 /* We use a bit more efficient memory handling.
1253 We allocate always larger blocks which get used over
1254 time. This is faster than many small allocations. */
1255 # define INITIAL_BLOCK_SIZE 4080
1256 static unsigned char *freemem
;
1257 static size_t freemem_size
;
1259 const unsigned char *inbuf
;
1260 unsigned char *outbuf
;
1263 transmem_block_t
*transmem_list
;
1266 __libc_lock_lock (lock
);
1269 inbuf
= (const unsigned char *) result
;
1270 outbuf
= freemem
+ sizeof (size_t);
1272 transmem_list
= NULL
;
1278 transmem_block_t
*newmem
;
1280 size_t non_reversible
;
1283 if (freemem_size
< sizeof (size_t))
1284 goto resize_freemem
;
1286 res
= __gconv (convd
->conv
,
1287 &inbuf
, inbuf
+ resultlen
,
1289 outbuf
+ freemem_size
- sizeof (size_t),
1292 if (res
== __GCONV_OK
|| res
== __GCONV_EMPTY_INPUT
)
1295 if (res
!= __GCONV_FULL_OUTPUT
)
1297 /* We should not use the translation at all, it
1298 is incorrectly encoded. */
1299 __libc_lock_unlock (lock
);
1303 inbuf
= (const unsigned char *) result
;
1306 const char *inptr
= (const char *) inbuf
;
1307 size_t inleft
= resultlen
;
1308 char *outptr
= (char *) outbuf
;
1311 if (freemem_size
< sizeof (size_t))
1312 goto resize_freemem
;
1314 outleft
= freemem_size
- sizeof (size_t);
1315 if (iconv (convd
->conv
,
1316 (ICONV_CONST
char **) &inptr
, &inleft
,
1320 outbuf
= (unsigned char *) outptr
;
1325 __libc_lock_unlock (lock
);
1332 /* We must allocate a new buffer or resize the old one. */
1333 if (malloc_count
> 0)
1336 freemem_size
= malloc_count
* INITIAL_BLOCK_SIZE
;
1337 newmem
= (transmem_block_t
*) realloc (transmem_list
,
1341 transmem_list
= newmem
;
1344 struct transmem_list
*old
= transmem_list
;
1346 transmem_list
= transmem_list
->next
;
1354 freemem_size
= INITIAL_BLOCK_SIZE
;
1355 newmem
= (transmem_block_t
*) malloc (freemem_size
);
1359 /* Add the block to the list of blocks we have to free
1361 newmem
->next
= transmem_list
;
1362 transmem_list
= newmem
;
1364 /* Fall through and return -1. */
1367 if (__builtin_expect (newmem
== NULL
, 0))
1371 __libc_lock_unlock (lock
);
1376 freemem
= (unsigned char *) newmem
->data
;
1377 freemem_size
-= offsetof (struct transmem_list
, data
);
1379 transmem_list
= newmem
;
1383 outbuf
= freemem
+ sizeof (size_t);
1386 /* We have now in our buffer a converted string. Put this
1387 into the table of conversions. */
1388 *(size_t *) freemem
= outbuf
- freemem
- sizeof (size_t);
1389 convd
->conv_tab
[act
] = (char *) freemem
;
1390 /* Shrink freemem, but keep it aligned. */
1391 freemem_size
-= outbuf
- freemem
;
1393 freemem
+= freemem_size
& (alignof (size_t) - 1);
1394 freemem_size
= freemem_size
& ~ (alignof (size_t) - 1);
1396 __libc_lock_unlock (lock
);
1399 /* Now convd->conv_tab[act] contains the translation of all
1400 the plural variants. */
1401 result
= convd
->conv_tab
[act
] + sizeof (size_t);
1402 resultlen
= *(size_t *) convd
->conv_tab
[act
];
1406 /* The result string is converted. */
1408 #endif /* _LIBC || HAVE_ICONV */
1410 *lengthp
= resultlen
;
1415 /* Look up a plural variant. */
1418 plural_lookup (struct loaded_l10nfile
*domain
, unsigned long int n
,
1419 const char *translation
, size_t translation_len
)
1421 struct loaded_domain
*domaindata
= (struct loaded_domain
*) domain
->data
;
1422 unsigned long int index
;
1425 index
= plural_eval (domaindata
->plural
, n
);
1426 if (index
>= domaindata
->nplurals
)
1427 /* This should never happen. It means the plural expression and the
1428 given maximum value do not match. */
1431 /* Skip INDEX strings at TRANSLATION. */
1436 p
= __rawmemchr (p
, '\0');
1438 p
= strchr (p
, '\0');
1440 /* And skip over the NUL byte. */
1443 if (p
>= translation
+ translation_len
)
1444 /* This should never happen. It means the plural expression
1445 evaluated to a value larger than the number of variants
1446 available for MSGID1. */
1447 return (char *) translation
;
1453 /* Return string representation of locale CATEGORY. */
1456 category_to_name (int category
)
1464 retval
= "LC_COLLATE";
1469 retval
= "LC_CTYPE";
1474 retval
= "LC_MONETARY";
1479 retval
= "LC_NUMERIC";
1489 retval
= "LC_MESSAGES";
1494 retval
= "LC_RESPONSE";
1499 /* This might not make sense but is perhaps better than any other
1505 /* If you have a better idea for a default value let me know. */
1513 /* Guess value of current locale from value of the environment variables
1514 or system-dependent defaults. */
1517 #ifdef IN_LIBGLOCALE
1518 guess_category_value (int category
, const char *categoryname
,
1522 guess_category_value (int category
, const char *categoryname
)
1525 const char *language
;
1526 #ifndef IN_LIBGLOCALE
1529 const char *language_default
;
1530 int locale_defaulted
;
1534 /* We use the settings in the following order:
1535 1. The value of the environment variable 'LANGUAGE'. This is a GNU
1536 extension. Its value can be a colon-separated list of locale names.
1537 2. The value of the environment variable 'LC_ALL', 'LC_xxx', or 'LANG'.
1538 More precisely, the first among these that is set to a non-empty value.
1539 This is how POSIX specifies it. The value is a single locale name.
1540 3. A system-dependent preference list of languages. Its value can be a
1541 colon-separated list of locale names.
1542 4. A system-dependent default locale name.
1544 - System-dependent settings can be overridden by environment variables.
1545 - If the system provides both a list of languages and a default locale,
1546 the former is used. */
1548 #ifndef IN_LIBGLOCALE
1549 /* Fetch the locale name, through the POSIX method of looking to `LC_ALL',
1550 `LC_xxx', and `LANG'. On some systems this can be done by the
1551 `setlocale' function itself. */
1553 locale
= __current_locale_name (category
);
1555 locale_defaulted
= 0;
1557 locale
= _nl_locale_name_thread_unsafe (category
, categoryname
);
1561 locale
= _nl_locale_name_posix (category
, categoryname
);
1564 locale
= _nl_locale_name_default ();
1565 locale_defaulted
= 1;
1571 /* Ignore LANGUAGE and its system-dependent analogon if the locale is set
1573 1. "C" locale usually uses the ASCII encoding, and most international
1574 messages use non-ASCII characters. These characters get displayed
1575 as question marks (if using glibc's iconv()) or as invalid 8-bit
1576 characters (because other iconv()s refuse to convert most non-ASCII
1577 characters to ASCII). In any case, the output is ugly.
1578 2. The precise output of some programs in the "C" locale is specified
1579 by POSIX and should not depend on environment variables like
1580 "LANGUAGE" or system-dependent information. We allow such programs
1581 to use gettext(). */
1582 if (strcmp (locale
, "C") == 0)
1585 /* The highest priority value is the value of the 'LANGUAGE' environment
1587 language
= getenv ("LANGUAGE");
1588 if (language
!= NULL
&& language
[0] != '\0')
1590 #if !defined IN_LIBGLOCALE && !defined _LIBC
1591 /* The next priority value is the locale name, if not defaulted. */
1592 if (locale_defaulted
)
1594 /* The next priority value is the default language preferences list. */
1595 language_default
= _nl_language_preferences_default ();
1596 if (language_default
!= NULL
)
1597 return language_default
;
1599 /* The least priority value is the locale name, if defaulted. */
1604 #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE
1605 /* Returns the output charset. */
1608 get_output_charset (struct binding
*domainbinding
)
1610 /* The output charset should normally be determined by the locale. But
1611 sometimes the locale is not used or not correctly set up, so we provide
1612 a possibility for the user to override this: the OUTPUT_CHARSET
1613 environment variable. Moreover, the value specified through
1614 bind_textdomain_codeset overrides both. */
1615 if (domainbinding
!= NULL
&& domainbinding
->codeset
!= NULL
)
1616 return domainbinding
->codeset
;
1619 /* For speed reasons, we look at the value of OUTPUT_CHARSET only
1620 once. This is a user variable that is not supposed to change
1621 during a program run. */
1622 static char *output_charset_cache
;
1623 static int output_charset_cached
;
1625 if (!output_charset_cached
)
1627 const char *value
= getenv ("OUTPUT_CHARSET");
1629 if (value
!= NULL
&& value
[0] != '\0')
1631 size_t len
= strlen (value
) + 1;
1632 char *value_copy
= (char *) malloc (len
);
1634 if (value_copy
!= NULL
)
1635 memcpy (value_copy
, value
, len
);
1636 output_charset_cache
= value_copy
;
1638 output_charset_cached
= 1;
1641 if (output_charset_cache
!= NULL
)
1642 return output_charset_cache
;
1646 return _NL_CURRENT (LC_CTYPE
, CODESET
);
1649 return locale_charset ();
1657 /* @@ begin of epilog @@ */
1659 /* We don't want libintl.a to depend on any other library. So we
1660 avoid the non-standard function stpcpy. In GNU C Library this
1661 function is available, though. Also allow the symbol HAVE_STPCPY
1663 #if !_LIBC && !HAVE_STPCPY
1665 stpcpy (char *dest
, const char *src
)
1667 while ((*dest
++ = *src
++) != '\0')
1673 #if !_LIBC && !HAVE_MEMPCPY
1675 mempcpy (void *dest
, const void *src
, size_t n
)
1677 return (void *) ((char *) memcpy (dest
, src
, n
) + n
);
1681 #if !_LIBC && !HAVE_TSEARCH
1682 # include "tsearch.c"
1687 /* If we want to free all resources we have to do some work at
1689 libc_freeres_fn (free_mem
)
1693 while (_nl_domain_bindings
!= NULL
)
1695 struct binding
*oldp
= _nl_domain_bindings
;
1696 _nl_domain_bindings
= _nl_domain_bindings
->next
;
1697 if (oldp
->dirname
!= _nl_default_dirname
)
1698 /* Yes, this is a pointer comparison. */
1699 free (oldp
->dirname
);
1700 free (oldp
->codeset
);
1704 if (_nl_current_default_domain
!= _nl_default_default_domain
)
1705 /* Yes, again a pointer comparison. */
1706 free ((char *) _nl_current_default_domain
);
1708 /* Remove the search tree with the known translations. */
1709 __tdestroy (root
, free
);
1712 while (transmem_list
!= NULL
)
1714 old
= transmem_list
;
1715 transmem_list
= transmem_list
->next
;