Upload Tizen:Base source
[external/eglibc.git] / intl / dcigettext.c
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.
5
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.
10
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.
15
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
19    02111-1307 USA.  */
20
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.  */
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE    1
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31
32 #include <sys/types.h>
33
34 #ifdef __GNUC__
35 # define alloca __builtin_alloca
36 # define HAVE_ALLOCA 1
37 #else
38 # if defined HAVE_ALLOCA_H || defined _LIBC
39 #  include <alloca.h>
40 # else
41 #  ifdef _AIX
42  #pragma alloca
43 #  else
44 #   ifndef alloca
45 char *alloca ();
46 #   endif
47 #  endif
48 # endif
49 #endif
50
51 #include <errno.h>
52 #ifndef errno
53 extern int errno;
54 #endif
55 #ifndef __set_errno
56 # define __set_errno(val) errno = (val)
57 #endif
58
59 #include <stddef.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #if defined HAVE_UNISTD_H || defined _LIBC
64 # include <unistd.h>
65 #endif
66
67 #include <locale.h>
68
69 #if defined HAVE_SYS_PARAM_H || defined _LIBC
70 # include <sys/param.h>
71 #endif
72
73 #include "gettextP.h"
74 #include "plural-exp.h"
75 #ifdef _LIBC
76 # include <libintl.h>
77 #else
78 # include "libgnuintl.h"
79 #endif
80 #include "hash-string.h"
81
82 #ifdef _LIBC
83 # include <gnu/option-groups.h>
84 #endif
85
86 /* Thread safetyness.  */
87 #ifdef _LIBC
88 # include <bits/libc-lock.h>
89 #else
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)
97 #endif
98
99 /* Alignment of types.  */
100 #if defined __GNUC__ && __GNUC__ >= 2
101 # define alignof(TYPE) __alignof__ (TYPE)
102 #else
103 # define alignof(TYPE) \
104     ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2)
105 #endif
106
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.  */
110 #if !defined _LIBC
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
115 #endif
116
117 /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>.  */
118 #ifndef offsetof
119 # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
120 #endif
121
122 /* @@ end of prolog @@ */
123
124 #ifdef _LIBC
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
129 # ifndef stpcpy
130 #  define stpcpy __stpcpy
131 # endif
132 # define tfind __tfind
133 #else
134 # if !defined HAVE_GETCWD
135 char *getwd ();
136 #  define getcwd(buf, max) getwd (buf)
137 # else
138 char *getcwd ();
139 # endif
140 # ifndef HAVE_STPCPY
141 static char *stpcpy PARAMS ((char *dest, const char *src));
142 # endif
143 # ifndef HAVE_MEMPCPY
144 static void *mempcpy PARAMS ((void *dest, const void *src, size_t n));
145 # endif
146 #endif
147
148 /* Amount to increase buffer size by in each try.  */
149 #define PATH_INCR 32
150
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__)
156 # include <limits.h>
157 #endif
158
159 #ifndef _POSIX_PATH_MAX
160 # define _POSIX_PATH_MAX 255
161 #endif
162
163 #if !defined PATH_MAX && defined _PC_PATH_MAX
164 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
165 #endif
166
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>
170 #endif
171
172 #if !defined PATH_MAX && defined MAXPATHLEN
173 # define PATH_MAX MAXPATHLEN
174 #endif
175
176 #ifndef PATH_MAX
177 # define PATH_MAX _POSIX_PATH_MAX
178 #endif
179
180 /* Whether to support different locales in different threads.  */
181 #if defined _LIBC || HAVE_NL_LOCALE_NAME
182 # define HAVE_PER_THREAD_LOCALE
183 #endif
184
185 /* This is the type used for the search tree where known translations
186    are stored.  */
187 struct known_translation_t
188 {
189   /* Domain in which to search.  */
190   const char *domainname;
191
192   /* The category.  */
193   int category;
194
195 #ifdef HAVE_PER_THREAD_LOCALE
196   /* Name of the relevant locale category, or "" for the global locale.  */
197   const char *localename;
198 #endif
199
200   /* State of the catalog counter at the point the string was found.  */
201   int counter;
202
203   /* Catalog where the string was found.  */
204   struct loaded_l10nfile *domain;
205
206   /* And finally the translation.  */
207   const char *translation;
208   size_t translation_length;
209
210   /* Pointer to the string in question.  */
211   union
212     {
213       char appended[ZERO];  /* used if domain != NULL */
214       const char *ptr;      /* used if domain == NULL */
215     }
216   msgid;
217 };
218
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
222 # include <search.h>
223
224 static void *root;
225
226 # ifdef _LIBC
227 #  define tsearch __tsearch
228 # endif
229
230 /* Function to compare two entries in the table of known translations.  */
231 static int transcmp PARAMS ((const void *p1, const void *p2));
232 static int
233 transcmp (p1, p2)
234      const void *p1;
235      const void *p2;
236 {
237   const struct known_translation_t *s1;
238   const struct known_translation_t *s2;
239   int result;
240
241   s1 = (const struct known_translation_t *) p1;
242   s2 = (const struct known_translation_t *) p2;
243
244   result = strcmp (s1->domain != NULL ? s1->msgid.appended : s1->msgid.ptr,
245                    s2->domain != NULL ? s2->msgid.appended : s2->msgid.ptr);
246   if (result == 0)
247     {
248       result = strcmp (s1->domainname, s2->domainname);
249       if (result == 0)
250         {
251 #ifdef HAVE_PER_THREAD_LOCALE
252           result = strcmp (s1->localename, s2->localename);
253           if (result == 0)
254 #endif
255             /* We compare the category last (though this is the cheapest
256                operation) since it is hopefully always the same (namely
257                LC_MESSAGES).  */
258             result = s1->category - s2->category;
259         }
260     }
261
262   return result;
263 }
264 #endif
265
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";
269
270 /* Value used as the default domain for gettext(3).  */
271 const char *_nl_current_default_domain attribute_hidden
272      = _nl_default_default_domain;
273
274 /* Contains the default location of the message catalogs.  */
275
276 #ifdef _LIBC
277 extern const char _nl_default_dirname[];
278 libc_hidden_proto (_nl_default_dirname)
279 #endif
280 const char _nl_default_dirname[] = LOCALEDIR;
281 #ifdef _LIBC
282 libc_hidden_data_def (_nl_default_dirname)
283 #endif
284
285 /* List with bindings of specific domains created by bindtextdomain()
286    calls.  */
287 struct binding *_nl_domain_bindings;
288
289 /* Prototypes for local functions.  */
290 static char *plural_lookup PARAMS ((struct loaded_l10nfile *domain,
291                                     unsigned long int n,
292                                     const char *translation,
293                                     size_t translation_len))
294      internal_function;
295 static const char *guess_category_value PARAMS ((int category,
296                                                  const char *categoryname))
297      internal_function;
298 #ifdef _LIBC
299 # include "../locale/localeinfo.h"
300 # define category_to_name(category) \
301   _nl_category_names.str + _nl_category_name_idxs[category]
302 #else
303 static const char *category_to_name PARAMS ((int category)) internal_function;
304 #endif
305
306
307 /* For those loosing systems which don't have `alloca' we have to add
308    some additional code emulating it.  */
309 #ifdef HAVE_ALLOCA
310 /* Nothing has to be done.  */
311 # define freea(p) /* nothing */
312 # define ADD_BLOCK(list, address) /* nothing */
313 # define FREE_BLOCKS(list) /* nothing */
314 #else
315 struct block_list
316 {
317   void *address;
318   struct block_list *next;
319 };
320 # define ADD_BLOCK(list, addr)                                                \
321   do {                                                                        \
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         \
324        the list.  */                                                          \
325     if (newp != NULL) {                                                       \
326       newp->address = (addr);                                                 \
327       newp->next = (list);                                                    \
328       (list) = newp;                                                          \
329     }                                                                         \
330   } while (0)
331 # define FREE_BLOCKS(list)                                                    \
332   do {                                                                        \
333     while (list != NULL) {                                                    \
334       struct block_list *old = list;                                          \
335       list = list->next;                                                      \
336       free (old->address);                                                    \
337       free (old);                                                             \
338     }                                                                         \
339   } while (0)
340 # undef alloca
341 # define alloca(size) (malloc (size))
342 # define freea(p) free (p)
343 #endif  /* have alloca */
344
345
346 #ifdef _LIBC
347 /* List of blocks allocated for translations.  */
348 typedef struct transmem_list
349 {
350   struct transmem_list *next;
351   char data[ZERO];
352 } transmem_block_t;
353 static struct transmem_list *transmem_list;
354 #else
355 typedef unsigned char transmem_block_t;
356 #endif
357 #if defined _LIBC || HAVE_ICONV
358 static const char *get_output_charset PARAMS ((struct binding *domainbinding))
359      internal_function;
360 #endif
361
362
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.  */
367 #ifdef _LIBC
368 # define DCIGETTEXT __dcigettext
369 #else
370 # define DCIGETTEXT libintl_dcigettext
371 #endif
372
373 /* Lock variable to protect the global data in the gettext implementation.  */
374 #ifdef _LIBC
375 __libc_rwlock_define_initialized (, _nl_state_lock attribute_hidden)
376 #endif
377
378 /* Checking whether the binaries runs SUID must be done and glibc provides
379    easier methods therefore we make a difference here.  */
380 #ifdef _LIBC
381 # define ENABLE_SECURE __libc_enable_secure
382 # define DETERMINE_SECURE
383 #else
384 # ifndef HAVE_GETUID
385 #  define getuid() 0
386 # endif
387 # ifndef HAVE_GETGID
388 #  define getgid() 0
389 # endif
390 # ifndef HAVE_GETEUID
391 #  define geteuid() getuid()
392 # endif
393 # ifndef HAVE_GETEGID
394 #  define getegid() getgid()
395 # endif
396 static int enable_secure;
397 # define ENABLE_SECURE (enable_secure == 1)
398 # define DETERMINE_SECURE \
399   if (enable_secure == 0)                                                     \
400     {                                                                         \
401       if (getuid () != geteuid () || getgid () != getegid ())                 \
402         enable_secure = 1;                                                    \
403       else                                                                    \
404         enable_secure = -1;                                                   \
405     }
406 #endif
407
408 /* Get the function to evaluate the plural expression.  */
409 #include "plural-eval.c"
410
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.  */
414 char *
415 DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
416      const char *domainname;
417      const char *msgid1;
418      const char *msgid2;
419      int plural;
420      unsigned long int n;
421      int category;
422 {
423 #ifndef HAVE_ALLOCA
424   struct block_list *block_list = NULL;
425 #endif
426   struct loaded_l10nfile *domain;
427   struct binding *binding;
428   const char *categoryname;
429   const char *categoryvalue;
430   char *dirname, *xdomainname;
431   char *single_locale;
432   char *retval;
433   size_t retlen;
434   int saved_errno;
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;
440 # endif
441 #endif
442   size_t domainname_len;
443
444   /* If no real MSGID is given return NULL.  */
445   if (msgid1 == NULL)
446     return NULL;
447
448 #ifdef _LIBC
449   if (category < 0 || category >= __LC_LAST || category == LC_ALL)
450     /* Bogus.  */
451     return (plural == 0
452             ? (char *) msgid1
453             /* Use the Germanic plural rule.  */
454             : n == 1 ? (char *) msgid1 : (char *) msgid2);
455 #endif
456
457 #ifdef _LIBC
458 #if __OPTION_EGLIBC_LOCALE_CODE
459   __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
460   __libc_rwlock_rdlock (__libc_setlocale_lock);
461 #endif
462 #endif
463
464   __libc_rwlock_rdlock (_nl_state_lock);
465
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;
471
472 #if defined HAVE_TSEARCH || defined _LIBC
473   /* Try to find the translation among those which we found at
474      some time.  */
475   search.domain = NULL;
476   search.msgid.ptr = msgid1;
477   search.domainname = domainname;
478   search.category = category;
479 # ifdef HAVE_PER_THREAD_LOCALE
480 #  ifdef _LIBC
481 #   if __OPTION_EGLIBC_LOCALE_CODE
482   localename = strdupa (__current_locale_name (category));
483 #   else
484   localename = "C";
485 #   endif
486 #  endif
487   search.localename = localename;
488 # endif
489
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);
494
495   foundp = (struct known_translation_t **) tfind (&search, &root, transcmp);
496
497   __libc_rwlock_unlock (tree_lock);
498
499   if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr)
500     {
501       /* Now deal with plural.  */
502       if (plural)
503         retval = plural_lookup ((*foundp)->domain, n, (*foundp)->translation,
504                                 (*foundp)->translation_length);
505       else
506         retval = (char *) (*foundp)->translation;
507
508 # ifdef _LIBC
509 #if __OPTION_EGLIBC_LOCALE_CODE
510       __libc_rwlock_unlock (__libc_setlocale_lock);
511 #endif
512 # endif
513       __libc_rwlock_unlock (_nl_state_lock);
514       return retval;
515     }
516 #endif
517
518   /* Preserve the `errno' value.  */
519   saved_errno = errno;
520
521   /* See whether this is a SUID binary or not.  */
522   DETERMINE_SECURE;
523
524   /* First find matching binding.  */
525   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
526     {
527       int compare = strcmp (domainname, binding->domainname);
528       if (compare == 0)
529         /* We found it!  */
530         break;
531       if (compare < 0)
532         {
533           /* It is not in the list.  */
534           binding = NULL;
535           break;
536         }
537     }
538
539   if (binding == NULL)
540     dirname = (char *) _nl_default_dirname;
541   else if (binding->dirname[0] == '/')
542     dirname = binding->dirname;
543   else
544     {
545       /* We have a relative path.  Make it absolute now.  */
546       size_t dirname_len = strlen (binding->dirname) + 1;
547       size_t path_max;
548       char *ret;
549
550       path_max = (unsigned int) PATH_MAX;
551       path_max += 2;            /* The getcwd docs say to do this.  */
552
553       for (;;)
554         {
555           dirname = (char *) alloca (path_max + dirname_len);
556           ADD_BLOCK (block_list, dirname);
557
558           __set_errno (0);
559           ret = getcwd (dirname, path_max);
560           if (ret != NULL || errno != ERANGE)
561             break;
562
563           path_max += path_max / 2;
564           path_max += PATH_INCR;
565         }
566
567       if (ret == NULL)
568         goto no_translation;
569
570       stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
571     }
572
573   /* Now determine the symbolic name of CATEGORY and its value.  */
574   categoryname = category_to_name (category);
575   categoryvalue = guess_category_value (category, categoryname);
576
577   domainname_len = strlen (domainname);
578   xdomainname = (char *) alloca (strlen (categoryname)
579                                  + domainname_len + 5);
580   ADD_BLOCK (block_list, xdomainname);
581
582   stpcpy (mempcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
583                   domainname, domainname_len),
584           ".mo");
585
586   /* Creating working area.  */
587   single_locale = (char *) alloca (strlen (categoryvalue) + 1);
588   ADD_BLOCK (block_list, single_locale);
589
590
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.  */
593   while (1)
594     {
595       /* Make CATEGORYVALUE point to the next element of the list.  */
596       while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
597         ++categoryvalue;
598       if (categoryvalue[0] == '\0')
599         {
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
603              will take place.  */
604           single_locale[0] = 'C';
605           single_locale[1] = '\0';
606         }
607       else
608         {
609           char *cp = single_locale;
610           while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
611             *cp++ = *categoryvalue++;
612           *cp = '\0';
613
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.  */
618             continue;
619         }
620
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)
625         {
626         no_translation:
627           FREE_BLOCKS (block_list);
628 #if __OPTION_EGLIBC_LOCALE_CODE
629           __libc_rwlock_unlock (__libc_setlocale_lock);
630 #endif
631           __libc_rwlock_unlock (_nl_state_lock);
632           __set_errno (saved_errno);
633           return (plural == 0
634                   ? (char *) msgid1
635                   /* Use the Germanic plural rule.  */
636                   : n == 1 ? (char *) msgid1 : (char *) msgid2);
637         }
638
639
640       /* Find structure describing the message catalog matching the
641          DOMAINNAME and CATEGORY.  */
642       domain = _nl_find_domain (dirname, single_locale, xdomainname, binding);
643
644       if (domain != NULL)
645         {
646           retval = _nl_find_msg (domain, binding, msgid1, 1, &retlen);
647
648           if (retval == NULL)
649             {
650               int cnt;
651
652               for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
653                 {
654                   retval = _nl_find_msg (domain->successor[cnt], binding,
655                                          msgid1, 1, &retlen);
656
657                   if (retval != NULL)
658                     {
659                       domain = domain->successor[cnt];
660                       break;
661                     }
662                 }
663             }
664
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))
669             goto no_translation;
670
671           if (retval != NULL)
672             {
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
677               if (foundp == NULL)
678                 {
679                   /* Create a new entry and add it to the search tree.  */
680                   size_t msgid_len;
681                   size_t size;
682                   struct known_translation_t *newp;
683
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;
689 # endif
690                   newp = (struct known_translation_t *) malloc (size);
691                   if (newp != NULL)
692                     {
693                       char *new_domainname;
694 # ifdef HAVE_PER_THREAD_LOCALE
695                       char *new_localename;
696 # endif
697
698                       new_domainname =
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);
704 # endif
705                       newp->domainname = new_domainname;
706                       newp->category = category;
707 # ifdef HAVE_PER_THREAD_LOCALE
708                       newp->localename = new_localename;
709 # endif
710                       newp->counter = _nl_msg_cat_cntr;
711                       newp->domain = domain;
712                       newp->translation = retval;
713                       newp->translation_length = retlen;
714
715                       __libc_rwlock_wrlock (tree_lock);
716
717                       /* Insert the entry in the search tree.  */
718                       foundp = (struct known_translation_t **)
719                         tsearch (newp, &root, transcmp);
720
721                       __libc_rwlock_unlock (tree_lock);
722
723                       if (foundp == NULL
724                           || __builtin_expect (*foundp != newp, 0))
725                         /* The insert failed.  */
726                         free (newp);
727                     }
728                 }
729               else
730                 {
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;
736                 }
737 #endif
738               __set_errno (saved_errno);
739
740               /* Now deal with plural.  */
741               if (plural)
742                 retval = plural_lookup (domain, n, retval, retlen);
743
744 #if __OPTION_EGLIBC_LOCALE_CODE
745               __libc_rwlock_unlock (__libc_setlocale_lock);
746 #endif
747               __libc_rwlock_unlock (_nl_state_lock);
748               return retval;
749             }
750         }
751     }
752   /* NOTREACHED */
753 }
754
755
756 char *
757 internal_function
758 _nl_find_msg (domain_file, domainbinding, msgid, convert, lengthp)
759      struct loaded_l10nfile *domain_file;
760      struct binding *domainbinding;
761      const char *msgid;
762      int convert;
763      size_t *lengthp;
764 {
765   struct loaded_domain *domain;
766   nls_uint32 nstrings;
767   size_t act;
768   char *result;
769   size_t resultlen;
770
771   if (domain_file->decided <= 0)
772     _nl_load_domain (domain_file, domainbinding);
773
774   if (domain_file->data == NULL)
775     return NULL;
776
777   domain = (struct loaded_domain *) domain_file->data;
778
779   nstrings = domain->nstrings;
780
781   /* Locate the MSGID and its translation.  */
782   if (domain->hash_tab != NULL)
783     {
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));
789
790       while (1)
791         {
792           nls_uint32 nstr =
793             W (domain->must_swap_hash_tab, domain->hash_tab[idx]);
794
795           if (nstr == 0)
796             /* Hash table entry is empty.  */
797             return NULL;
798
799           nstr--;
800
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.  */
804           if (nstr < nstrings
805               ? W (domain->must_swap, domain->orig_tab[nstr].length) >= len
806                 && (strcmp (msgid,
807                             domain->data + W (domain->must_swap,
808                                               domain->orig_tab[nstr].offset))
809                     == 0)
810               : domain->orig_sysdep_tab[nstr - nstrings].length > len
811                 && (strcmp (msgid,
812                             domain->orig_sysdep_tab[nstr - nstrings].pointer)
813                     == 0))
814             {
815               act = nstr;
816               goto found;
817             }
818
819           if (idx >= domain->hash_size - incr)
820             idx -= domain->hash_size - incr;
821           else
822             idx += incr;
823         }
824       /* NOTREACHED */
825     }
826   else
827     {
828       /* Try the default method:  binary search in the sorted array of
829          messages.  */
830       size_t top, bottom;
831
832       bottom = 0;
833       top = nstrings;
834       while (bottom < top)
835         {
836           int cmp_val;
837
838           act = (bottom + top) / 2;
839           cmp_val = strcmp (msgid, (domain->data
840                                     + W (domain->must_swap,
841                                          domain->orig_tab[act].offset)));
842           if (cmp_val < 0)
843             top = act;
844           else if (cmp_val > 0)
845             bottom = act + 1;
846           else
847             goto found;
848         }
849       /* No translation was found.  */
850       return NULL;
851     }
852
853  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.  */
856   if (act < nstrings)
857     {
858       result = (char *)
859         (domain->data + W (domain->must_swap, domain->trans_tab[act].offset));
860       resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1;
861     }
862   else
863     {
864       result = (char *) domain->trans_sysdep_tab[act - nstrings].pointer;
865       resultlen = domain->trans_sysdep_tab[act - nstrings].length;
866     }
867
868 #if defined _LIBC || HAVE_ICONV
869   if (convert)
870     {
871       /* We are supposed to do a conversion.  */
872       const char *encoding = get_output_charset (domainbinding);
873
874       /* Protect against reallocation of the table.  */
875       __libc_rwlock_rdlock (domain->conversions_lock);
876
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;
881       size_t i;
882
883       for (i = nconversions; i > 0; )
884         {
885           i--;
886           if (strcmp (domain->conversions[i].encoding, encoding) == 0)
887             {
888               convd = &domain->conversions[i];
889               break;
890             }
891         }
892
893       __libc_rwlock_unlock (domain->conversions_lock);
894
895       if (convd == NULL)
896         {
897           /* We have to allocate a new conversions table.  */
898           __libc_rwlock_wrlock (domain->conversions_lock);
899           nconversions = domain->nconversions;
900
901           /* Maybe in the meantime somebody added the translation.
902              Recheck.  */
903           for (i = nconversions; i > 0; )
904             {
905               i--;
906               if (strcmp (domain->conversions[i].encoding, encoding) == 0)
907                 {
908                   convd = &domain->conversions[i];
909                   goto found_convd;
910                 }
911             }
912
913           /* Allocate a table for the converted translations for this
914              encoding.  */
915           struct converted_domain *new_conversions =
916             (struct converted_domain *)
917             realloc (domain->conversions,
918                      (nconversions + 1) * sizeof (struct converted_domain));
919
920           if (__builtin_expect (new_conversions == NULL, 0))
921             {
922               /* Nothing we can do, no more memory.  We cannot use the
923                  translation because it might be encoded incorrectly.  */
924             unlock_fail:
925               __libc_rwlock_unlock (domain->conversions_lock);
926               return (char *) -1;
927             }
928
929           domain->conversions = new_conversions;
930
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.  */
936             goto unlock_fail;
937
938           convd = &new_conversions[nconversions];
939           convd->encoding = encoding;
940
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.  */
946 # ifdef _LIBC
947           convd->conv = (__gconv_t) -1;
948 # else
949 #  if HAVE_ICONV
950           convd->conv = (iconv_t) -1;
951 #  endif
952 # endif
953           {
954             char *nullentry;
955             size_t nullentrylen;
956
957             /* Get the header entry.  This is a recursion, but it doesn't
958                reallocate domain->conversions because we pass convert = 0.  */
959             nullentry =
960               _nl_find_msg (domain_file, domainbinding, "", 0, &nullentrylen);
961
962             if (nullentry != NULL)
963               {
964                 const char *charsetstr;
965
966                 charsetstr = strstr (nullentry, "charset=");
967                 if (charsetstr != NULL)
968                   {
969                     size_t len;
970                     char *charset;
971                     const char *outcharset;
972
973                     charsetstr += strlen ("charset=");
974                     len = strcspn (charsetstr, " \t\n");
975
976                     charset = (char *) alloca (len + 1);
977 # if defined _LIBC || HAVE_MEMPCPY
978                     *((char *) mempcpy (charset, charsetstr, len)) = '\0';
979 # else
980                     memcpy (charset, charsetstr, len);
981                     charset[len] = '\0';
982 # endif
983
984                     outcharset = encoding;
985
986 # ifdef _LIBC
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,
991                                           GCONV_AVOID_NOCONV);
992                     if (__builtin_expect (r != __GCONV_OK, 0))
993                       {
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))
998                           {
999                             __libc_rwlock_unlock (domain->conversions_lock);
1000                             free ((char *) encoding);
1001                             return NULL;
1002                           }
1003
1004                         convd->conv = (__gconv_t) -1;
1005                       }
1006 # else
1007 #  if HAVE_ICONV
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)
1013                       {
1014                         char *tmp;
1015
1016                         len = strlen (outcharset);
1017                         tmp = (char *) alloca (len + 10 + 1);
1018                         memcpy (tmp, outcharset, len);
1019                         memcpy (tmp + len, "//TRANSLIT", 10 + 1);
1020                         outcharset = tmp;
1021
1022                         convd->conv = iconv_open (outcharset, charset);
1023
1024                         freea (outcharset);
1025                       }
1026                     else
1027 #   endif
1028                       convd->conv = iconv_open (outcharset, charset);
1029 #  endif
1030 # endif
1031
1032                     freea (charset);
1033                   }
1034               }
1035           }
1036           convd->conv_tab = NULL;
1037           /* Here domain->conversions is still == new_conversions.  */
1038           domain->nconversions++;
1039
1040         found_convd:
1041           __libc_rwlock_unlock (domain->conversions_lock);
1042         }
1043
1044       if (
1045 # ifdef _LIBC
1046           convd->conv != (__gconv_t) -1
1047 # else
1048 #  if HAVE_ICONV
1049           convd->conv != (iconv_t) -1
1050 #  endif
1051 # endif
1052           )
1053         {
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
1062              NULs.  */
1063
1064           if (__builtin_expect (convd->conv_tab == NULL, 0))
1065             {
1066               __libc_lock_lock (lock);
1067               if (convd->conv_tab == NULL)
1068                 {
1069                   convd->conv_tab
1070                     = calloc (nstrings + domain->n_sysdep_strings,
1071                               sizeof (char *));
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;
1076                 }
1077               __libc_lock_unlock (lock);
1078             }
1079
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.  */
1083             return (char *) -1;
1084
1085           if (convd->conv_tab[act] == NULL)
1086             {
1087               __libc_lock_lock (lock);
1088             not_translated_yet:;
1089
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;
1098
1099               const unsigned char *inbuf;
1100               unsigned char *outbuf;
1101               int malloc_count;
1102 # ifndef _LIBC
1103               transmem_block_t *transmem_list = NULL;
1104 # endif
1105
1106               inbuf = (const unsigned char *) result;
1107               outbuf = freemem + sizeof (size_t);
1108
1109               malloc_count = 0;
1110               while (1)
1111                 {
1112                   transmem_block_t *newmem;
1113 # ifdef _LIBC
1114                   size_t non_reversible;
1115                   int res;
1116
1117                   if (freemem_size < sizeof (size_t))
1118                     goto resize_freemem;
1119
1120                   res = __gconv (convd->conv,
1121                                  &inbuf, inbuf + resultlen,
1122                                  &outbuf,
1123                                  outbuf + freemem_size - sizeof (size_t),
1124                                  &non_reversible);
1125
1126                   if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT)
1127                     break;
1128
1129                   if (res != __GCONV_FULL_OUTPUT)
1130                     {
1131                       /* We should not use the translation at all, it
1132                          is incorrectly encoded.  */
1133                       __libc_lock_unlock (lock);
1134                       return NULL;
1135                     }
1136
1137                   inbuf = (const unsigned char *) result;
1138 # else
1139 #  if HAVE_ICONV
1140                   const char *inptr = (const char *) inbuf;
1141                   size_t inleft = resultlen;
1142                   char *outptr = (char *) outbuf;
1143                   size_t outleft;
1144
1145                   if (freemem_size < sizeof (size_t))
1146                     goto resize_freemem;
1147
1148                   outleft = freemem_size - sizeof (size_t);
1149                   if (iconv (convd->conv,
1150                              (ICONV_CONST char **) &inptr, &inleft,
1151                              &outptr, &outleft)
1152                       != (size_t) (-1))
1153                     {
1154                       outbuf = (unsigned char *) outptr;
1155                       break;
1156                     }
1157                   if (errno != E2BIG)
1158                     {
1159                       __libc_lock_unlock (lock);
1160                       return NULL;
1161                     }
1162 #  endif
1163 # endif
1164
1165                 resize_freemem:
1166                   /* We must allocate a new buffer or resize the old one.  */
1167                   if (malloc_count > 0)
1168                     {
1169                       ++malloc_count;
1170                       freemem_size = malloc_count * INITIAL_BLOCK_SIZE;
1171                       newmem = (transmem_block_t *) realloc (transmem_list,
1172                                                              freemem_size);
1173 # ifdef _LIBC
1174                       if (newmem != NULL)
1175                         transmem_list = transmem_list->next;
1176                       else
1177                         {
1178                           struct transmem_list *old = transmem_list;
1179
1180                           transmem_list = transmem_list->next;
1181                           free (old);
1182                         }
1183 # endif
1184                     }
1185                   else
1186                     {
1187                       malloc_count = 1;
1188                       freemem_size = INITIAL_BLOCK_SIZE;
1189                       newmem = (transmem_block_t *) malloc (freemem_size);
1190                     }
1191                   if (__builtin_expect (newmem == NULL, 0))
1192                     {
1193                       freemem = NULL;
1194                       freemem_size = 0;
1195                       __libc_lock_unlock (lock);
1196                       return (char *) -1;
1197                     }
1198
1199 # ifdef _LIBC
1200                   /* Add the block to the list of blocks we have to free
1201                      at some point.  */
1202                   newmem->next = transmem_list;
1203                   transmem_list = newmem;
1204
1205                   freemem = (unsigned char *) newmem->data;
1206                   freemem_size -= offsetof (struct transmem_list, data);
1207 # else
1208                   transmem_list = newmem;
1209                   freemem = newmem;
1210 # endif
1211
1212                   outbuf = freemem + sizeof (size_t);
1213                 }
1214
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;
1221               freemem = outbuf;
1222               freemem += freemem_size & (alignof (size_t) - 1);
1223               freemem_size = freemem_size & ~ (alignof (size_t) - 1);
1224
1225               __libc_lock_unlock (lock);
1226             }
1227
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];
1232         }
1233     }
1234
1235   /* The result string is converted.  */
1236
1237 #endif /* _LIBC || HAVE_ICONV */
1238
1239   *lengthp = resultlen;
1240   return result;
1241 }
1242
1243
1244 /* Look up a plural variant.  */
1245 static char *
1246 internal_function
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;
1252 {
1253   struct loaded_domain *domaindata = (struct loaded_domain *) domain->data;
1254   unsigned long int index;
1255   const char *p;
1256
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.  */
1261     index = 0;
1262
1263   /* Skip INDEX strings at TRANSLATION.  */
1264   p = translation;
1265   while (index-- > 0)
1266     {
1267 #ifdef _LIBC
1268       p = __rawmemchr (p, '\0');
1269 #else
1270       p = strchr (p, '\0');
1271 #endif
1272       /* And skip over the NUL byte.  */
1273       p++;
1274
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;
1280     }
1281   return (char *) p;
1282 }
1283
1284 #ifndef _LIBC
1285 /* Return string representation of locale CATEGORY.  */
1286 static const char *
1287 internal_function
1288 category_to_name (category)
1289      int category;
1290 {
1291   const char *retval;
1292
1293   switch (category)
1294   {
1295 #ifdef LC_COLLATE
1296   case LC_COLLATE:
1297     retval = "LC_COLLATE";
1298     break;
1299 #endif
1300 #ifdef LC_CTYPE
1301   case LC_CTYPE:
1302     retval = "LC_CTYPE";
1303     break;
1304 #endif
1305 #ifdef LC_MONETARY
1306   case LC_MONETARY:
1307     retval = "LC_MONETARY";
1308     break;
1309 #endif
1310 #ifdef LC_NUMERIC
1311   case LC_NUMERIC:
1312     retval = "LC_NUMERIC";
1313     break;
1314 #endif
1315 #ifdef LC_TIME
1316   case LC_TIME:
1317     retval = "LC_TIME";
1318     break;
1319 #endif
1320 #ifdef LC_MESSAGES
1321   case LC_MESSAGES:
1322     retval = "LC_MESSAGES";
1323     break;
1324 #endif
1325 #ifdef LC_RESPONSE
1326   case LC_RESPONSE:
1327     retval = "LC_RESPONSE";
1328     break;
1329 #endif
1330 #ifdef LC_ALL
1331   case LC_ALL:
1332     /* This might not make sense but is perhaps better than any other
1333        value.  */
1334     retval = "LC_ALL";
1335     break;
1336 #endif
1337   default:
1338     /* If you have a better idea for a default value let me know.  */
1339     retval = "LC_XXX";
1340   }
1341
1342   return retval;
1343 }
1344 #endif
1345
1346 /* Guess value of current locale from value of the environment variables.  */
1347 static const char *
1348 internal_function
1349 guess_category_value (category, categoryname)
1350      int category;
1351      const char *categoryname;
1352 {
1353   const char *language;
1354   const char *retval;
1355
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')
1361     language = NULL;
1362
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.  */
1366 #ifdef _LIBC
1367 # if __OPTION_EGLIBC_LOCALE_CODE
1368   retval = __current_locale_name (category);
1369 # else
1370   retval = "C";
1371 # endif
1372 #else
1373   retval = _nl_locale_name (category, categoryname);
1374 #endif
1375
1376   return language != NULL && strcmp (retval, "C") != 0 ? language : retval;
1377 }
1378
1379 #if defined _LIBC || HAVE_ICONV
1380 /* Returns the output charset.  */
1381 static const char *
1382 internal_function
1383 get_output_charset (domainbinding)
1384      struct binding *domainbinding;
1385 {
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;
1393   else
1394     {
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;
1400
1401       if (!output_charset_cached)
1402         {
1403           const char *value = getenv ("OUTPUT_CHARSET");
1404
1405           if (value != NULL && value[0] != '\0')
1406             {
1407               size_t len = strlen (value) + 1;
1408               char *value_copy = (char *) malloc (len);
1409
1410               if (value_copy != NULL)
1411                 memcpy (value_copy, value, len);
1412               output_charset_cache = value_copy;
1413             }
1414           output_charset_cached = 1;
1415         }
1416
1417       if (output_charset_cache != NULL)
1418         return output_charset_cache;
1419       else
1420         {
1421 # ifdef _LIBC
1422           return _NL_CURRENT (LC_CTYPE, CODESET);
1423 # else
1424 #  if HAVE_ICONV
1425           extern const char *locale_charset PARAMS ((void);
1426           return locale_charset ();
1427 #  endif
1428 # endif
1429         }
1430     }
1431 }
1432 #endif
1433
1434 /* @@ begin of epilog @@ */
1435
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
1439    to be defined.  */
1440 #if !_LIBC && !HAVE_STPCPY
1441 static char *
1442 stpcpy (dest, src)
1443      char *dest;
1444      const char *src;
1445 {
1446   while ((*dest++ = *src++) != '\0')
1447     /* Do nothing. */ ;
1448   return dest - 1;
1449 }
1450 #endif
1451
1452 #if !_LIBC && !HAVE_MEMPCPY
1453 static void *
1454 mempcpy (dest, src, n)
1455      void *dest;
1456      const void *src;
1457      size_t n;
1458 {
1459   return (void *) ((char *) memcpy (dest, src, n) + n);
1460 }
1461 #endif
1462
1463
1464 #ifdef _LIBC
1465 /* If we want to free all resources we have to do some work at
1466    program's end.  */
1467 libc_freeres_fn (free_mem)
1468 {
1469   void *old;
1470
1471   while (_nl_domain_bindings != NULL)
1472     {
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);
1479       free (oldp);
1480     }
1481
1482   if (_nl_current_default_domain != _nl_default_default_domain)
1483     /* Yes, again a pointer comparison.  */
1484     free ((char *) _nl_current_default_domain);
1485
1486   /* Remove the search tree with the known translations.  */
1487   __tdestroy (root, free);
1488   root = NULL;
1489
1490   while (transmem_list != NULL)
1491     {
1492       old = transmem_list;
1493       transmem_list = transmem_list->next;
1494       free (old);
1495     }
1496 }
1497 #endif