1 /* Implementation of the internal dcigettext function.
2 Copyright (C) 1995-2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
22 This must come before <config.h> because <config.h> may include
23 <features.h>, and once <features.h> has been included, it's too late. */
25 # define _GNU_SOURCE 1
32 #include <sys/types.h>
35 # define alloca __builtin_alloca
36 # define HAVE_ALLOCA 1
38 # if defined HAVE_ALLOCA_H || defined _LIBC
56 # define __set_errno(val) errno = (val)
63 #if defined HAVE_UNISTD_H || defined _LIBC
69 #if defined HAVE_SYS_PARAM_H || defined _LIBC
70 # include <sys/param.h>
74 #include "plural-exp.h"
78 # include "libgnuintl.h"
80 #include "hash-string.h"
83 # include <gnu/option-groups.h>
86 /* Thread safetyness. */
88 # include <bits/libc-lock.h>
90 /* Provide dummy implementation if this is outside glibc. */
91 # define __libc_lock_define_initialized(CLASS, NAME)
92 # define __libc_lock_lock(NAME)
93 # define __libc_lock_unlock(NAME)
94 # define __libc_rwlock_define_initialized(CLASS, NAME)
95 # define __libc_rwlock_rdlock(NAME)
96 # define __libc_rwlock_unlock(NAME)
99 /* Alignment of types. */
100 #if defined __GNUC__ && __GNUC__ >= 2
101 # define alignof(TYPE) __alignof__ (TYPE)
103 # define alignof(TYPE) \
104 ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2)
107 /* The internal variables in the standalone libintl.a must have different
108 names than the internal variables in GNU libc, otherwise programs
109 using libintl.a cannot be linked statically. */
111 # define _nl_default_default_domain libintl_nl_default_default_domain
112 # define _nl_current_default_domain libintl_nl_current_default_domain
113 # define _nl_default_dirname libintl_nl_default_dirname
114 # define _nl_domain_bindings libintl_nl_domain_bindings
117 /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>. */
119 # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
122 /* @@ end of prolog @@ */
125 /* Rename the non ANSI C functions. This is required by the standard
126 because some ANSI C functions will require linking with this object
127 file and the name space must not be polluted. */
128 # define getcwd __getcwd
130 # define stpcpy __stpcpy
132 # define tfind __tfind
134 # if !defined HAVE_GETCWD
136 # define getcwd(buf, max) getwd (buf)
141 static char *stpcpy PARAMS ((char *dest, const char *src));
143 # ifndef HAVE_MEMPCPY
144 static void *mempcpy PARAMS ((void *dest, const void *src, size_t n));
148 /* Amount to increase buffer size by in each try. */
151 /* The following is from pathmax.h. */
152 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
153 PATH_MAX but might cause redefinition warnings when sys/param.h is
154 later included (as on MORE/BSD 4.3). */
155 #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__)
159 #ifndef _POSIX_PATH_MAX
160 # define _POSIX_PATH_MAX 255
163 #if !defined PATH_MAX && defined _PC_PATH_MAX
164 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
167 /* Don't include sys/param.h if it already has been. */
168 #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN
169 # include <sys/param.h>
172 #if !defined PATH_MAX && defined MAXPATHLEN
173 # define PATH_MAX MAXPATHLEN
177 # define PATH_MAX _POSIX_PATH_MAX
180 /* Whether to support different locales in different threads. */
181 #if defined _LIBC || HAVE_NL_LOCALE_NAME
182 # define HAVE_PER_THREAD_LOCALE
185 /* This is the type used for the search tree where known translations
187 struct known_translation_t
189 /* Domain in which to search. */
190 const char *domainname;
195 #ifdef HAVE_PER_THREAD_LOCALE
196 /* Name of the relevant locale category, or "" for the global locale. */
197 const char *localename;
200 /* State of the catalog counter at the point the string was found. */
203 /* Catalog where the string was found. */
204 struct loaded_l10nfile *domain;
206 /* And finally the translation. */
207 const char *translation;
208 size_t translation_length;
210 /* Pointer to the string in question. */
213 char appended[ZERO]; /* used if domain != NULL */
214 const char *ptr; /* used if domain == NULL */
219 /* Root of the search tree with known translations. We can use this
220 only if the system provides the `tsearch' function family. */
221 #if defined HAVE_TSEARCH || defined _LIBC
227 # define tsearch __tsearch
230 /* Function to compare two entries in the table of known translations. */
231 static int transcmp PARAMS ((const void *p1, const void *p2));
237 const struct known_translation_t *s1;
238 const struct known_translation_t *s2;
241 s1 = (const struct known_translation_t *) p1;
242 s2 = (const struct known_translation_t *) p2;
244 result = strcmp (s1->domain != NULL ? s1->msgid.appended : s1->msgid.ptr,
245 s2->domain != NULL ? s2->msgid.appended : s2->msgid.ptr);
248 result = strcmp (s1->domainname, s2->domainname);
251 #ifdef HAVE_PER_THREAD_LOCALE
252 result = strcmp (s1->localename, s2->localename);
255 /* We compare the category last (though this is the cheapest
256 operation) since it is hopefully always the same (namely
258 result = s1->category - s2->category;
266 /* Name of the default domain used for gettext(3) prior any call to
267 textdomain(3). The default value for this is "messages". */
268 const char _nl_default_default_domain[] attribute_hidden = "messages";
270 /* Value used as the default domain for gettext(3). */
271 const char *_nl_current_default_domain attribute_hidden
272 = _nl_default_default_domain;
274 /* Contains the default location of the message catalogs. */
277 extern const char _nl_default_dirname[];
278 libc_hidden_proto (_nl_default_dirname)
280 const char _nl_default_dirname[] = LOCALEDIR;
282 libc_hidden_data_def (_nl_default_dirname)
285 /* List with bindings of specific domains created by bindtextdomain()
287 struct binding *_nl_domain_bindings;
289 /* Prototypes for local functions. */
290 static char *plural_lookup PARAMS ((struct loaded_l10nfile *domain,
292 const char *translation,
293 size_t translation_len))
295 static const char *guess_category_value PARAMS ((int category,
296 const char *categoryname))
299 # include "../locale/localeinfo.h"
300 # define category_to_name(category) \
301 _nl_category_names.str + _nl_category_name_idxs[category]
303 static const char *category_to_name PARAMS ((int category)) internal_function;
307 /* For those loosing systems which don't have `alloca' we have to add
308 some additional code emulating it. */
310 /* Nothing has to be done. */
311 # define freea(p) /* nothing */
312 # define ADD_BLOCK(list, address) /* nothing */
313 # define FREE_BLOCKS(list) /* nothing */
318 struct block_list *next;
320 # define ADD_BLOCK(list, addr) \
322 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
323 /* If we cannot get a free block we cannot add the new element to \
325 if (newp != NULL) { \
326 newp->address = (addr); \
327 newp->next = (list); \
331 # define FREE_BLOCKS(list) \
333 while (list != NULL) { \
334 struct block_list *old = list; \
336 free (old->address); \
341 # define alloca(size) (malloc (size))
342 # define freea(p) free (p)
343 #endif /* have alloca */
347 /* List of blocks allocated for translations. */
348 typedef struct transmem_list
350 struct transmem_list *next;
353 static struct transmem_list *transmem_list;
355 typedef unsigned char transmem_block_t;
357 #if defined _LIBC || HAVE_ICONV
358 static const char *get_output_charset PARAMS ((struct binding *domainbinding))
363 /* Names for the libintl functions are a problem. They must not clash
364 with existing names and they should follow ANSI C. But this source
365 code is also used in GNU C Library where the names have a __
366 prefix. So we have to make a difference here. */
368 # define DCIGETTEXT __dcigettext
370 # define DCIGETTEXT libintl_dcigettext
373 /* Lock variable to protect the global data in the gettext implementation. */
375 __libc_rwlock_define_initialized (, _nl_state_lock attribute_hidden)
378 /* Checking whether the binaries runs SUID must be done and glibc provides
379 easier methods therefore we make a difference here. */
381 # define ENABLE_SECURE __libc_enable_secure
382 # define DETERMINE_SECURE
390 # ifndef HAVE_GETEUID
391 # define geteuid() getuid()
393 # ifndef HAVE_GETEGID
394 # define getegid() getgid()
396 static int enable_secure;
397 # define ENABLE_SECURE (enable_secure == 1)
398 # define DETERMINE_SECURE \
399 if (enable_secure == 0) \
401 if (getuid () != geteuid () || getgid () != getegid ()) \
404 enable_secure = -1; \
408 /* Get the function to evaluate the plural expression. */
409 #include "plural-eval.c"
411 /* Look up MSGID in the DOMAINNAME message catalog for the current
412 CATEGORY locale and, if PLURAL is nonzero, search over string
413 depending on the plural form determined by N. */
415 DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
416 const char *domainname;
424 struct block_list *block_list = NULL;
426 struct loaded_l10nfile *domain;
427 struct binding *binding;
428 const char *categoryname;
429 const char *categoryvalue;
430 char *dirname, *xdomainname;
435 #if defined HAVE_TSEARCH || defined _LIBC
436 struct known_translation_t search;
437 struct known_translation_t **foundp = NULL;
438 # ifdef HAVE_PER_THREAD_LOCALE
439 const char *localename;
442 size_t domainname_len;
444 /* If no real MSGID is given return NULL. */
449 if (category < 0 || category >= __LC_LAST || category == LC_ALL)
453 /* Use the Germanic plural rule. */
454 : n == 1 ? (char *) msgid1 : (char *) msgid2);
458 #if __OPTION_EGLIBC_LOCALE_CODE
459 __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
460 __libc_rwlock_rdlock (__libc_setlocale_lock);
464 __libc_rwlock_rdlock (_nl_state_lock);
466 /* If DOMAINNAME is NULL, we are interested in the default domain. If
467 CATEGORY is not LC_MESSAGES this might not make much sense but the
468 definition left this undefined. */
469 if (domainname == NULL)
470 domainname = _nl_current_default_domain;
472 #if defined HAVE_TSEARCH || defined _LIBC
473 /* Try to find the translation among those which we found at
475 search.domain = NULL;
476 search.msgid.ptr = msgid1;
477 search.domainname = domainname;
478 search.category = category;
479 # ifdef HAVE_PER_THREAD_LOCALE
481 # if __OPTION_EGLIBC_LOCALE_CODE
482 localename = strdupa (__current_locale_name (category));
487 search.localename = localename;
490 /* Since tfind/tsearch manage a balanced tree, concurrent tfind and
491 tsearch calls can be fatal. */
492 __libc_rwlock_define_initialized (static, tree_lock);
493 __libc_rwlock_rdlock (tree_lock);
495 foundp = (struct known_translation_t **) tfind (&search, &root, transcmp);
497 __libc_rwlock_unlock (tree_lock);
499 if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr)
501 /* Now deal with plural. */
503 retval = plural_lookup ((*foundp)->domain, n, (*foundp)->translation,
504 (*foundp)->translation_length);
506 retval = (char *) (*foundp)->translation;
509 #if __OPTION_EGLIBC_LOCALE_CODE
510 __libc_rwlock_unlock (__libc_setlocale_lock);
513 __libc_rwlock_unlock (_nl_state_lock);
518 /* Preserve the `errno' value. */
521 /* See whether this is a SUID binary or not. */
524 /* First find matching binding. */
525 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
527 int compare = strcmp (domainname, binding->domainname);
533 /* It is not in the list. */
540 dirname = (char *) _nl_default_dirname;
541 else if (binding->dirname[0] == '/')
542 dirname = binding->dirname;
545 /* We have a relative path. Make it absolute now. */
546 size_t dirname_len = strlen (binding->dirname) + 1;
550 path_max = (unsigned int) PATH_MAX;
551 path_max += 2; /* The getcwd docs say to do this. */
555 dirname = (char *) alloca (path_max + dirname_len);
556 ADD_BLOCK (block_list, dirname);
559 ret = getcwd (dirname, path_max);
560 if (ret != NULL || errno != ERANGE)
563 path_max += path_max / 2;
564 path_max += PATH_INCR;
570 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
573 /* Now determine the symbolic name of CATEGORY and its value. */
574 categoryname = category_to_name (category);
575 categoryvalue = guess_category_value (category, categoryname);
577 domainname_len = strlen (domainname);
578 xdomainname = (char *) alloca (strlen (categoryname)
579 + domainname_len + 5);
580 ADD_BLOCK (block_list, xdomainname);
582 stpcpy (mempcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
583 domainname, domainname_len),
586 /* Creating working area. */
587 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
588 ADD_BLOCK (block_list, single_locale);
591 /* Search for the given string. This is a loop because we perhaps
592 got an ordered list of languages to consider for the translation. */
595 /* Make CATEGORYVALUE point to the next element of the list. */
596 while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
598 if (categoryvalue[0] == '\0')
600 /* The whole contents of CATEGORYVALUE has been searched but
601 no valid entry has been found. We solve this situation
602 by implicitly appending a "C" entry, i.e. no translation
604 single_locale[0] = 'C';
605 single_locale[1] = '\0';
609 char *cp = single_locale;
610 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
611 *cp++ = *categoryvalue++;
614 /* When this is a SUID binary we must not allow accessing files
615 outside the dedicated directories. */
616 if (ENABLE_SECURE && strchr (single_locale, '/') != NULL)
617 /* Ingore this entry. */
621 /* If the current locale value is C (or POSIX) we don't load a
622 domain. Return the MSGID. */
623 if (strcmp (single_locale, "C") == 0
624 || strcmp (single_locale, "POSIX") == 0)
627 FREE_BLOCKS (block_list);
628 #if __OPTION_EGLIBC_LOCALE_CODE
629 __libc_rwlock_unlock (__libc_setlocale_lock);
631 __libc_rwlock_unlock (_nl_state_lock);
632 __set_errno (saved_errno);
635 /* Use the Germanic plural rule. */
636 : n == 1 ? (char *) msgid1 : (char *) msgid2);
640 /* Find structure describing the message catalog matching the
641 DOMAINNAME and CATEGORY. */
642 domain = _nl_find_domain (dirname, single_locale, xdomainname, binding);
646 retval = _nl_find_msg (domain, binding, msgid1, 1, &retlen);
652 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
654 retval = _nl_find_msg (domain->successor[cnt], binding,
659 domain = domain->successor[cnt];
665 /* Returning -1 means that some resource problem exists
666 (likely memory) and that the strings could not be
667 converted. Return the original strings. */
668 if (__builtin_expect (retval == (char *) -1, 0))
673 /* Found the translation of MSGID1 in domain DOMAIN:
674 starting at RETVAL, RETLEN bytes. */
675 FREE_BLOCKS (block_list);
676 #if defined HAVE_TSEARCH || defined _LIBC
679 /* Create a new entry and add it to the search tree. */
682 struct known_translation_t *newp;
684 msgid_len = strlen (msgid1) + 1;
685 size = offsetof (struct known_translation_t, msgid)
686 + msgid_len + domainname_len + 1;
687 # ifdef HAVE_PER_THREAD_LOCALE
688 size += strlen (localename) + 1;
690 newp = (struct known_translation_t *) malloc (size);
693 char *new_domainname;
694 # ifdef HAVE_PER_THREAD_LOCALE
695 char *new_localename;
699 mempcpy (newp->msgid.appended, msgid1, msgid_len);
700 memcpy (new_domainname, domainname, domainname_len + 1);
701 # ifdef HAVE_PER_THREAD_LOCALE
702 new_localename = new_domainname + domainname_len + 1;
703 strcpy (new_localename, localename);
705 newp->domainname = new_domainname;
706 newp->category = category;
707 # ifdef HAVE_PER_THREAD_LOCALE
708 newp->localename = new_localename;
710 newp->counter = _nl_msg_cat_cntr;
711 newp->domain = domain;
712 newp->translation = retval;
713 newp->translation_length = retlen;
715 __libc_rwlock_wrlock (tree_lock);
717 /* Insert the entry in the search tree. */
718 foundp = (struct known_translation_t **)
719 tsearch (newp, &root, transcmp);
721 __libc_rwlock_unlock (tree_lock);
724 || __builtin_expect (*foundp != newp, 0))
725 /* The insert failed. */
731 /* We can update the existing entry. */
732 (*foundp)->counter = _nl_msg_cat_cntr;
733 (*foundp)->domain = domain;
734 (*foundp)->translation = retval;
735 (*foundp)->translation_length = retlen;
738 __set_errno (saved_errno);
740 /* Now deal with plural. */
742 retval = plural_lookup (domain, n, retval, retlen);
744 #if __OPTION_EGLIBC_LOCALE_CODE
745 __libc_rwlock_unlock (__libc_setlocale_lock);
747 __libc_rwlock_unlock (_nl_state_lock);
758 _nl_find_msg (domain_file, domainbinding, msgid, convert, lengthp)
759 struct loaded_l10nfile *domain_file;
760 struct binding *domainbinding;
765 struct loaded_domain *domain;
771 if (domain_file->decided <= 0)
772 _nl_load_domain (domain_file, domainbinding);
774 if (domain_file->data == NULL)
777 domain = (struct loaded_domain *) domain_file->data;
779 nstrings = domain->nstrings;
781 /* Locate the MSGID and its translation. */
782 if (domain->hash_tab != NULL)
784 /* Use the hashing table. */
785 nls_uint32 len = strlen (msgid);
786 nls_uint32 hash_val = __hash_string (msgid);
787 nls_uint32 idx = hash_val % domain->hash_size;
788 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
793 W (domain->must_swap_hash_tab, domain->hash_tab[idx]);
796 /* Hash table entry is empty. */
801 /* Compare msgid with the original string at index nstr.
802 We compare the lengths with >=, not ==, because plural entries
803 are represented by strings with an embedded NUL. */
805 ? W (domain->must_swap, domain->orig_tab[nstr].length) >= len
807 domain->data + W (domain->must_swap,
808 domain->orig_tab[nstr].offset))
810 : domain->orig_sysdep_tab[nstr - nstrings].length > len
812 domain->orig_sysdep_tab[nstr - nstrings].pointer)
819 if (idx >= domain->hash_size - incr)
820 idx -= domain->hash_size - incr;
828 /* Try the default method: binary search in the sorted array of
838 act = (bottom + top) / 2;
839 cmp_val = strcmp (msgid, (domain->data
840 + W (domain->must_swap,
841 domain->orig_tab[act].offset)));
844 else if (cmp_val > 0)
849 /* No translation was found. */
854 /* The translation was found at index ACT. If we have to convert the
855 string to use a different character set, this is the time. */
859 (domain->data + W (domain->must_swap, domain->trans_tab[act].offset));
860 resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1;
864 result = (char *) domain->trans_sysdep_tab[act - nstrings].pointer;
865 resultlen = domain->trans_sysdep_tab[act - nstrings].length;
868 #if defined _LIBC || HAVE_ICONV
871 /* We are supposed to do a conversion. */
872 const char *encoding = get_output_charset (domainbinding);
874 /* Protect against reallocation of the table. */
875 __libc_rwlock_rdlock (domain->conversions_lock);
877 /* Search whether a table with converted translations for this
878 encoding has already been allocated. */
879 size_t nconversions = domain->nconversions;
880 struct converted_domain *convd = NULL;
883 for (i = nconversions; i > 0; )
886 if (strcmp (domain->conversions[i].encoding, encoding) == 0)
888 convd = &domain->conversions[i];
893 __libc_rwlock_unlock (domain->conversions_lock);
897 /* We have to allocate a new conversions table. */
898 __libc_rwlock_wrlock (domain->conversions_lock);
899 nconversions = domain->nconversions;
901 /* Maybe in the meantime somebody added the translation.
903 for (i = nconversions; i > 0; )
906 if (strcmp (domain->conversions[i].encoding, encoding) == 0)
908 convd = &domain->conversions[i];
913 /* Allocate a table for the converted translations for this
915 struct converted_domain *new_conversions =
916 (struct converted_domain *)
917 realloc (domain->conversions,
918 (nconversions + 1) * sizeof (struct converted_domain));
920 if (__builtin_expect (new_conversions == NULL, 0))
922 /* Nothing we can do, no more memory. We cannot use the
923 translation because it might be encoded incorrectly. */
925 __libc_rwlock_unlock (domain->conversions_lock);
929 domain->conversions = new_conversions;
931 /* Copy the 'encoding' string to permanent storage. */
932 encoding = strdup (encoding);
933 if (__builtin_expect (encoding == NULL, 0))
934 /* Nothing we can do, no more memory. We cannot use the
935 translation because it might be encoded incorrectly. */
938 convd = &new_conversions[nconversions];
939 convd->encoding = encoding;
941 /* Find out about the character set the file is encoded with.
942 This can be found (in textual form) in the entry "". If this
943 entry does not exist or if this does not contain the 'charset='
944 information, we will assume the charset matches the one the
945 current locale and we don't have to perform any conversion. */
947 convd->conv = (__gconv_t) -1;
950 convd->conv = (iconv_t) -1;
957 /* Get the header entry. This is a recursion, but it doesn't
958 reallocate domain->conversions because we pass convert = 0. */
960 _nl_find_msg (domain_file, domainbinding, "", 0, &nullentrylen);
962 if (nullentry != NULL)
964 const char *charsetstr;
966 charsetstr = strstr (nullentry, "charset=");
967 if (charsetstr != NULL)
971 const char *outcharset;
973 charsetstr += strlen ("charset=");
974 len = strcspn (charsetstr, " \t\n");
976 charset = (char *) alloca (len + 1);
977 # if defined _LIBC || HAVE_MEMPCPY
978 *((char *) mempcpy (charset, charsetstr, len)) = '\0';
980 memcpy (charset, charsetstr, len);
984 outcharset = encoding;
987 /* We always want to use transliteration. */
988 outcharset = norm_add_slashes (outcharset, "TRANSLIT");
989 charset = norm_add_slashes (charset, "");
990 int r = __gconv_open (outcharset, charset, &convd->conv,
992 if (__builtin_expect (r != __GCONV_OK, 0))
994 /* If the output encoding is the same there is
995 nothing to do. Otherwise do not use the
996 translation at all. */
997 if (__builtin_expect (r != __GCONV_NULCONV, 1))
999 __libc_rwlock_unlock (domain->conversions_lock);
1000 free ((char *) encoding);
1004 convd->conv = (__gconv_t) -1;
1008 /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5,
1009 we want to use transliteration. */
1010 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \
1011 || _LIBICONV_VERSION >= 0x0105
1012 if (strchr (outcharset, '/') == NULL)
1016 len = strlen (outcharset);
1017 tmp = (char *) alloca (len + 10 + 1);
1018 memcpy (tmp, outcharset, len);
1019 memcpy (tmp + len, "//TRANSLIT", 10 + 1);
1022 convd->conv = iconv_open (outcharset, charset);
1028 convd->conv = iconv_open (outcharset, charset);
1036 convd->conv_tab = NULL;
1037 /* Here domain->conversions is still == new_conversions. */
1038 domain->nconversions++;
1041 __libc_rwlock_unlock (domain->conversions_lock);
1046 convd->conv != (__gconv_t) -1
1049 convd->conv != (iconv_t) -1
1054 __libc_lock_define_initialized (static, lock)
1055 /* We are supposed to do a conversion. First allocate an
1056 appropriate table with the same structure as the table
1057 of translations in the file, where we can put the pointers
1058 to the converted strings in.
1059 There is a slight complication with plural entries. They
1060 are represented by consecutive NUL terminated strings. We
1061 handle this case by converting RESULTLEN bytes, including
1064 if (__builtin_expect (convd->conv_tab == NULL, 0))
1066 __libc_lock_lock (lock);
1067 if (convd->conv_tab == NULL)
1070 = calloc (nstrings + domain->n_sysdep_strings,
1072 if (convd->conv_tab != NULL)
1073 goto not_translated_yet;
1074 /* Mark that we didn't succeed allocating a table. */
1075 convd->conv_tab = (char **) -1;
1077 __libc_lock_unlock (lock);
1080 if (__builtin_expect (convd->conv_tab == (char **) -1, 0))
1081 /* Nothing we can do, no more memory. We cannot use the
1082 translation because it might be encoded incorrectly. */
1085 if (convd->conv_tab[act] == NULL)
1087 __libc_lock_lock (lock);
1088 not_translated_yet:;
1090 /* We haven't used this string so far, so it is not
1091 translated yet. Do this now. */
1092 /* We use a bit more efficient memory handling.
1093 We allocate always larger blocks which get used over
1094 time. This is faster than many small allocations. */
1095 # define INITIAL_BLOCK_SIZE 4080
1096 static unsigned char *freemem;
1097 static size_t freemem_size;
1099 const unsigned char *inbuf;
1100 unsigned char *outbuf;
1103 transmem_block_t *transmem_list = NULL;
1106 inbuf = (const unsigned char *) result;
1107 outbuf = freemem + sizeof (size_t);
1112 transmem_block_t *newmem;
1114 size_t non_reversible;
1117 if (freemem_size < sizeof (size_t))
1118 goto resize_freemem;
1120 res = __gconv (convd->conv,
1121 &inbuf, inbuf + resultlen,
1123 outbuf + freemem_size - sizeof (size_t),
1126 if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT)
1129 if (res != __GCONV_FULL_OUTPUT)
1131 /* We should not use the translation at all, it
1132 is incorrectly encoded. */
1133 __libc_lock_unlock (lock);
1137 inbuf = (const unsigned char *) result;
1140 const char *inptr = (const char *) inbuf;
1141 size_t inleft = resultlen;
1142 char *outptr = (char *) outbuf;
1145 if (freemem_size < sizeof (size_t))
1146 goto resize_freemem;
1148 outleft = freemem_size - sizeof (size_t);
1149 if (iconv (convd->conv,
1150 (ICONV_CONST char **) &inptr, &inleft,
1154 outbuf = (unsigned char *) outptr;
1159 __libc_lock_unlock (lock);
1166 /* We must allocate a new buffer or resize the old one. */
1167 if (malloc_count > 0)
1170 freemem_size = malloc_count * INITIAL_BLOCK_SIZE;
1171 newmem = (transmem_block_t *) realloc (transmem_list,
1175 transmem_list = transmem_list->next;
1178 struct transmem_list *old = transmem_list;
1180 transmem_list = transmem_list->next;
1188 freemem_size = INITIAL_BLOCK_SIZE;
1189 newmem = (transmem_block_t *) malloc (freemem_size);
1191 if (__builtin_expect (newmem == NULL, 0))
1195 __libc_lock_unlock (lock);
1200 /* Add the block to the list of blocks we have to free
1202 newmem->next = transmem_list;
1203 transmem_list = newmem;
1205 freemem = (unsigned char *) newmem->data;
1206 freemem_size -= offsetof (struct transmem_list, data);
1208 transmem_list = newmem;
1212 outbuf = freemem + sizeof (size_t);
1215 /* We have now in our buffer a converted string. Put this
1216 into the table of conversions. */
1217 *(size_t *) freemem = outbuf - freemem - sizeof (size_t);
1218 convd->conv_tab[act] = (char *) freemem;
1219 /* Shrink freemem, but keep it aligned. */
1220 freemem_size -= outbuf - freemem;
1222 freemem += freemem_size & (alignof (size_t) - 1);
1223 freemem_size = freemem_size & ~ (alignof (size_t) - 1);
1225 __libc_lock_unlock (lock);
1228 /* Now convd->conv_tab[act] contains the translation of all
1229 the plural variants. */
1230 result = convd->conv_tab[act] + sizeof (size_t);
1231 resultlen = *(size_t *) convd->conv_tab[act];
1235 /* The result string is converted. */
1237 #endif /* _LIBC || HAVE_ICONV */
1239 *lengthp = resultlen;
1244 /* Look up a plural variant. */
1247 plural_lookup (domain, n, translation, translation_len)
1248 struct loaded_l10nfile *domain;
1249 unsigned long int n;
1250 const char *translation;
1251 size_t translation_len;
1253 struct loaded_domain *domaindata = (struct loaded_domain *) domain->data;
1254 unsigned long int index;
1257 index = plural_eval (domaindata->plural, n);
1258 if (index >= domaindata->nplurals)
1259 /* This should never happen. It means the plural expression and the
1260 given maximum value do not match. */
1263 /* Skip INDEX strings at TRANSLATION. */
1268 p = __rawmemchr (p, '\0');
1270 p = strchr (p, '\0');
1272 /* And skip over the NUL byte. */
1275 if (p >= translation + translation_len)
1276 /* This should never happen. It means the plural expression
1277 evaluated to a value larger than the number of variants
1278 available for MSGID1. */
1279 return (char *) translation;
1285 /* Return string representation of locale CATEGORY. */
1288 category_to_name (category)
1297 retval = "LC_COLLATE";
1302 retval = "LC_CTYPE";
1307 retval = "LC_MONETARY";
1312 retval = "LC_NUMERIC";
1322 retval = "LC_MESSAGES";
1327 retval = "LC_RESPONSE";
1332 /* This might not make sense but is perhaps better than any other
1338 /* If you have a better idea for a default value let me know. */
1346 /* Guess value of current locale from value of the environment variables. */
1349 guess_category_value (category, categoryname)
1351 const char *categoryname;
1353 const char *language;
1356 /* The highest priority value is the `LANGUAGE' environment
1357 variable. But we don't use the value if the currently selected
1358 locale is the C locale. This is a GNU extension. */
1359 language = getenv ("LANGUAGE");
1360 if (language != NULL && language[0] == '\0')
1363 /* We have to proceed with the POSIX methods of looking to `LC_ALL',
1364 `LC_xxx', and `LANG'. On some systems this can be done by the
1365 `setlocale' function itself. */
1367 # if __OPTION_EGLIBC_LOCALE_CODE
1368 retval = __current_locale_name (category);
1373 retval = _nl_locale_name (category, categoryname);
1376 return language != NULL && strcmp (retval, "C") != 0 ? language : retval;
1379 #if defined _LIBC || HAVE_ICONV
1380 /* Returns the output charset. */
1383 get_output_charset (domainbinding)
1384 struct binding *domainbinding;
1386 /* The output charset should normally be determined by the locale. But
1387 sometimes the locale is not used or not correctly set up, so we provide
1388 a possibility for the user to override this: the OUTPUT_CHARSET
1389 environment variable. Moreover, the value specified through
1390 bind_textdomain_codeset overrides both. */
1391 if (domainbinding != NULL && domainbinding->codeset != NULL)
1392 return domainbinding->codeset;
1395 /* For speed reasons, we look at the value of OUTPUT_CHARSET only
1396 once. This is a user variable that is not supposed to change
1397 during a program run. */
1398 static char *output_charset_cache;
1399 static int output_charset_cached;
1401 if (!output_charset_cached)
1403 const char *value = getenv ("OUTPUT_CHARSET");
1405 if (value != NULL && value[0] != '\0')
1407 size_t len = strlen (value) + 1;
1408 char *value_copy = (char *) malloc (len);
1410 if (value_copy != NULL)
1411 memcpy (value_copy, value, len);
1412 output_charset_cache = value_copy;
1414 output_charset_cached = 1;
1417 if (output_charset_cache != NULL)
1418 return output_charset_cache;
1422 return _NL_CURRENT (LC_CTYPE, CODESET);
1425 extern const char *locale_charset PARAMS ((void);
1426 return locale_charset ();
1434 /* @@ begin of epilog @@ */
1436 /* We don't want libintl.a to depend on any other library. So we
1437 avoid the non-standard function stpcpy. In GNU C Library this
1438 function is available, though. Also allow the symbol HAVE_STPCPY
1440 #if !_LIBC && !HAVE_STPCPY
1446 while ((*dest++ = *src++) != '\0')
1452 #if !_LIBC && !HAVE_MEMPCPY
1454 mempcpy (dest, src, n)
1459 return (void *) ((char *) memcpy (dest, src, n) + n);
1465 /* If we want to free all resources we have to do some work at
1467 libc_freeres_fn (free_mem)
1471 while (_nl_domain_bindings != NULL)
1473 struct binding *oldp = _nl_domain_bindings;
1474 _nl_domain_bindings = _nl_domain_bindings->next;
1475 if (oldp->dirname != _nl_default_dirname)
1476 /* Yes, this is a pointer comparison. */
1477 free (oldp->dirname);
1478 free (oldp->codeset);
1482 if (_nl_current_default_domain != _nl_default_default_domain)
1483 /* Yes, again a pointer comparison. */
1484 free ((char *) _nl_current_default_domain);
1486 /* Remove the search tree with the known translations. */
1487 __tdestroy (root, free);
1490 while (transmem_list != NULL)
1492 old = transmem_list;
1493 transmem_list = transmem_list->next;