]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - intl/dcigettext.c
[BZ #661]
[thirdparty/glibc.git] / intl / dcigettext.c
index d2b903c3f3f4ea7a1a68d1ea73516488cfca9761..8163064edc28d0c39d6b17527ff770fcab8d2795 100644 (file)
@@ -1,20 +1,21 @@
 /* Implementation of the internal dcigettext function.
-   Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
+   Copyright (C) 1995-2002,2003,2004,2005 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
 
 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
    This must come before <config.h> because <config.h> may include
 
 #include <sys/types.h>
 
-#if defined __GNUC__ && !defined C_ALLOCA
+#ifdef __GNUC__
 # define alloca __builtin_alloca
 # define HAVE_ALLOCA 1
 #else
-# if (defined HAVE_ALLOCA_H || defined _LIBC) && !defined C_ALLOCA
+# if defined HAVE_ALLOCA_H || defined _LIBC
 #  include <alloca.h>
 # else
 #  ifdef _AIX
@@ -54,42 +55,22 @@ extern int errno;
 # define __set_errno(val) errno = (val)
 #endif
 
-#if defined STDC_HEADERS || defined _LIBC
-# include <stddef.h>
-# include <stdlib.h>
-#else
-char *getenv ();
-# ifdef HAVE_MALLOC_H
-#  include <malloc.h>
-# else
-void free ();
-# endif
-#endif
-
-#if defined HAVE_STRING_H || defined _LIBC
-# include <string.h>
-#else
-# include <strings.h>
-#endif
-#if !HAVE_STRCHR && !defined _LIBC
-# ifndef strchr
-#  define strchr index
-# endif
-#endif
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
 
 #if defined HAVE_UNISTD_H || defined _LIBC
 # include <unistd.h>
 #endif
 
-#if defined HAVE_LOCALE_H || defined _LIBC
-# include <locale.h>
-#endif
+#include <locale.h>
 
 #if defined HAVE_SYS_PARAM_H || defined _LIBC
 # include <sys/param.h>
 #endif
 
 #include "gettextP.h"
+#include "plural-exp.h"
 #ifdef _LIBC
 # include <libintl.h>
 #else
@@ -122,10 +103,10 @@ void free ();
    names than the internal variables in GNU libc, otherwise programs
    using libintl.a cannot be linked statically.  */
 #if !defined _LIBC
-# define _nl_default_default_domain _nl_default_default_domain__
-# define _nl_current_default_domain _nl_current_default_domain__
-# define _nl_default_dirname _nl_default_dirname__
-# define _nl_domain_bindings _nl_domain_bindings__
+# define _nl_default_default_domain libintl_nl_default_default_domain
+# define _nl_current_default_domain libintl_nl_current_default_domain
+# define _nl_default_dirname libintl_nl_default_dirname
+# define _nl_domain_bindings libintl_nl_domain_bindings
 #endif
 
 /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>.  */
@@ -191,22 +172,12 @@ static void *mempcpy PARAMS ((void *dest, const void *src, size_t n));
 # define PATH_MAX _POSIX_PATH_MAX
 #endif
 
-/* XPG3 defines the result of `setlocale (category, NULL)' as:
-   ``Directs `setlocale()' to query `category' and return the current
-     setting of `local'.''
-   However it does not specify the exact format.  Neither do SUSV2 and
-   ISO C 99.  So we can use this feature only on selected systems (e.g.
-   those using GNU C Library).  */
-#ifdef _LIBC
-# define HAVE_LOCALE_NULL
-#endif
-
 /* This is the type used for the search tree where known translations
    are stored.  */
 struct known_translation_t
 {
   /* Domain in which to search.  */
-  char *domainname;
+  const char *domainname;
 
   /* The category.  */
   int category;
@@ -267,13 +238,22 @@ transcmp (p1, p2)
 
 /* Name of the default domain used for gettext(3) prior any call to
    textdomain(3).  The default value for this is "messages".  */
-const char _nl_default_default_domain[] = "messages";
+const char _nl_default_default_domain[] attribute_hidden = "messages";
 
 /* Value used as the default domain for gettext(3).  */
-const char *_nl_current_default_domain = _nl_default_default_domain;
+const char *_nl_current_default_domain attribute_hidden
+     = _nl_default_default_domain;
 
 /* Contains the default location of the message catalogs.  */
+
+#ifdef _LIBC
+extern const char _nl_default_dirname[];
+libc_hidden_proto (_nl_default_dirname)
+#endif
 const char _nl_default_dirname[] = LOCALEDIR;
+#ifdef _LIBC
+libc_hidden_data_def (_nl_default_dirname)
+#endif
 
 /* List with bindings of specific domains created by bindtextdomain()
    calls.  */
@@ -285,19 +265,22 @@ static char *plural_lookup PARAMS ((struct loaded_l10nfile *domain,
                                    const char *translation,
                                    size_t translation_len))
      internal_function;
-static unsigned long int plural_eval PARAMS ((struct expression *pexp,
-                                             unsigned long int n))
-     internal_function;
-static const char *category_to_name PARAMS ((int category)) internal_function;
 static const char *guess_category_value PARAMS ((int category,
                                                 const char *categoryname))
      internal_function;
+#ifdef _LIBC
+# include "../locale/localeinfo.h"
+# define category_to_name(category)    _nl_category_names[category]
+#else
+static const char *category_to_name PARAMS ((int category)) internal_function;
+#endif
 
 
 /* For those loosing systems which don't have `alloca' we have to add
    some additional code emulating it.  */
 #ifdef HAVE_ALLOCA
 /* Nothing has to be done.  */
+# define freea(p) /* nothing */
 # define ADD_BLOCK(list, address) /* nothing */
 # define FREE_BLOCKS(list) /* nothing */
 #else
@@ -322,11 +305,13 @@ struct block_list
     while (list != NULL) {                                                   \
       struct block_list *old = list;                                         \
       list = list->next;                                                     \
+      free (old->address);                                                   \
       free (old);                                                            \
     }                                                                        \
   } while (0)
 # undef alloca
 # define alloca(size) (malloc (size))
+# define freea(p) free (p)
 #endif /* have alloca */
 
 
@@ -350,11 +335,13 @@ typedef unsigned char transmem_block_t;
 #ifdef _LIBC
 # define DCIGETTEXT __dcigettext
 #else
-# define DCIGETTEXT dcigettext__
+# define DCIGETTEXT libintl_dcigettext
 #endif
 
 /* Lock variable to protect the global data in the gettext implementation.  */
-__libc_rwlock_define_initialized (, _nl_state_lock)
+#ifdef _LIBC
+__libc_rwlock_define_initialized (, _nl_state_lock attribute_hidden)
+#endif
 
 /* Checking whether the binaries runs SUID must be done and glibc provides
    easier methods therefore we make a difference here.  */
@@ -362,6 +349,18 @@ __libc_rwlock_define_initialized (, _nl_state_lock)
 # define ENABLE_SECURE __libc_enable_secure
 # define DETERMINE_SECURE
 #else
+# ifndef HAVE_GETUID
+#  define getuid() 0
+# endif
+# ifndef HAVE_GETGID
+#  define getgid() 0
+# endif
+# ifndef HAVE_GETEUID
+#  define geteuid() getuid()
+# endif
+# ifndef HAVE_GETEGID
+#  define getegid() getgid()
+# endif
 static int enable_secure;
 # define ENABLE_SECURE (enable_secure == 1)
 # define DETERMINE_SECURE \
@@ -374,6 +373,9 @@ static int enable_secure;
     }
 #endif
 
+/* Get the function to evaluate the plural expression.  */
+#include "plural-eval.c"
+
 /* Look up MSGID in the DOMAINNAME message catalog for the current
    CATEGORY locale and, if PLURAL is nonzero, search over string
    depending on the plural form determined by N.  */
@@ -409,6 +411,15 @@ DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
   if (msgid1 == NULL)
     return NULL;
 
+#ifdef _LIBC
+  if (category < 0 || category >= __LC_LAST || category == LC_ALL)
+    /* Bogus.  */
+    return (plural == 0
+           ? (char *) msgid1
+           /* Use the Germanic plural rule.  */
+           : n == 1 ? (char *) msgid1 : (char *) msgid2);
+#endif
+
   __libc_rwlock_rdlock (_nl_state_lock);
 
   /* If DOMAINNAME is NULL, we are interested in the default domain.  If
@@ -425,10 +436,19 @@ DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
   search = (struct known_translation_t *)
           alloca (offsetof (struct known_translation_t, msgid) + msgid_len);
   memcpy (search->msgid, msgid1, msgid_len);
-  search->domainname = (char *) domainname;
+  search->domainname = domainname;
   search->category = category;
 
+  /* Since tfind/tsearch manage a balanced tree, concurrent tfind and
+     tsearch calls can be fatal.  */
+  __libc_rwlock_define_initialized (static, tree_lock);
+  __libc_rwlock_rdlock (tree_lock);
+
   foundp = (struct known_translation_t **) tfind (search, &root, transcmp);
+
+  __libc_rwlock_unlock (tree_lock);
+
+  freea (search);
   if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr)
     {
       /* Now deal with plural.  */
@@ -478,16 +498,18 @@ DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
       path_max = (unsigned int) PATH_MAX;
       path_max += 2;           /* The getcwd docs say to do this.  */
 
-      dirname = (char *) alloca (path_max + dirname_len);
-      ADD_BLOCK (block_list, dirname);
-
-      __set_errno (0);
-      while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
+      for (;;)
        {
-         path_max += PATH_INCR;
          dirname = (char *) alloca (path_max + dirname_len);
          ADD_BLOCK (block_list, dirname);
+
          __set_errno (0);
+         ret = getcwd (dirname, path_max);
+         if (ret != NULL || errno != ERANGE)
+           break;
+
+         path_max += path_max / 2;
+         path_max += PATH_INCR;
        }
 
       if (ret == NULL)
@@ -599,7 +621,6 @@ DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
              /* Found the translation of MSGID1 in domain DOMAIN:
                 starting at RETVAL, RETLEN bytes.  */
              FREE_BLOCKS (block_list);
-             __set_errno (saved_errno);
 #if defined HAVE_TSEARCH || defined _LIBC
              if (foundp == NULL)
                {
@@ -611,18 +632,25 @@ DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
                            + msgid_len + domainname_len + 1);
                  if (newp != NULL)
                    {
-                     newp->domainname =
-                       mempcpy (newp->msgid, msgid1, msgid_len);
-                     memcpy (newp->domainname, domainname, domainname_len + 1);
+                     char *new_domainname;
+
+                     new_domainname = mempcpy (newp->msgid, msgid1, msgid_len);
+                     memcpy (new_domainname, domainname, domainname_len + 1);
+                     newp->domainname = new_domainname;
                      newp->category = category;
                      newp->counter = _nl_msg_cat_cntr;
                      newp->domain = domain;
                      newp->translation = retval;
                      newp->translation_length = retlen;
 
+                     __libc_rwlock_wrlock (tree_lock);
+
                      /* Insert the entry in the search tree.  */
                      foundp = (struct known_translation_t **)
                        tsearch (newp, &root, transcmp);
+
+                     __libc_rwlock_unlock (tree_lock);
+
                      if (foundp == NULL
                          || __builtin_expect (*foundp != newp, 0))
                        /* The insert failed.  */
@@ -638,6 +666,8 @@ DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
                  (*foundp)->translation_length = retlen;
                }
 #endif
+             __set_errno (saved_errno);
+
              /* Now deal with plural.  */
              if (plural)
                retval = plural_lookup (domain, n, retval, retlen);
@@ -660,11 +690,12 @@ _nl_find_msg (domain_file, domainbinding, msgid, lengthp)
      size_t *lengthp;
 {
   struct loaded_domain *domain;
+  nls_uint32 nstrings;
   size_t act;
   char *result;
   size_t resultlen;
 
-  if (domain_file->decided == 0)
+  if (domain_file->decided <= 0)
     _nl_load_domain (domain_file, domainbinding);
 
   if (domain_file->data == NULL)
@@ -672,33 +703,43 @@ _nl_find_msg (domain_file, domainbinding, msgid, lengthp)
 
   domain = (struct loaded_domain *) domain_file->data;
 
+  nstrings = domain->nstrings;
+
   /* Locate the MSGID and its translation.  */
-  if (domain->hash_size > 2 && domain->hash_tab != NULL)
+  if (domain->hash_tab != NULL)
     {
       /* Use the hashing table.  */
       nls_uint32 len = strlen (msgid);
-      nls_uint32 hash_val = hash_string (msgid);
+      nls_uint32 hash_val = __hash_string (msgid);
       nls_uint32 idx = hash_val % domain->hash_size;
       nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
 
       while (1)
        {
-         nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
+         nls_uint32 nstr =
+           W (domain->must_swap_hash_tab, domain->hash_tab[idx]);
 
          if (nstr == 0)
            /* Hash table entry is empty.  */
            return NULL;
 
-         /* Compare msgid with the original string at index nstr-1.
+         nstr--;
+
+         /* Compare msgid with the original string at index nstr.
             We compare the lengths with >=, not ==, because plural entries
             are represented by strings with an embedded NUL.  */
-         if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) >= len
-             && (strcmp (msgid,
-                         domain->data + W (domain->must_swap,
-                                           domain->orig_tab[nstr - 1].offset))
-                 == 0))
+         if (nstr < nstrings
+             ? W (domain->must_swap, domain->orig_tab[nstr].length) >= len
+               && (strcmp (msgid,
+                           domain->data + W (domain->must_swap,
+                                             domain->orig_tab[nstr].offset))
+                   == 0)
+             : domain->orig_sysdep_tab[nstr - nstrings].length > len
+               && (strcmp (msgid,
+                           domain->orig_sysdep_tab[nstr - nstrings].pointer)
+                   == 0))
            {
-             act = nstr - 1;
+             act = nstr;
              goto found;
            }
 
@@ -716,7 +757,7 @@ _nl_find_msg (domain_file, domainbinding, msgid, lengthp)
       size_t top, bottom;
 
       bottom = 0;
-      top = domain->nstrings;
+      top = nstrings;
       while (bottom < top)
        {
          int cmp_val;
@@ -739,9 +780,17 @@ _nl_find_msg (domain_file, domainbinding, msgid, lengthp)
  found:
   /* The translation was found at index ACT.  If we have to convert the
      string to use a different character set, this is the time.  */
-  result = ((char *) domain->data
-           + W (domain->must_swap, domain->trans_tab[act].offset));
-  resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1;
+  if (act < nstrings)
+    {
+      result = (char *)
+       (domain->data + W (domain->must_swap, domain->trans_tab[act].offset));
+      resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1;
+    }
+  else
+    {
+      result = (char *) domain->trans_sysdep_tab[act - nstrings].pointer;
+      resultlen = domain->trans_sysdep_tab[act - nstrings].length;
+    }
 
 #if defined _LIBC || HAVE_ICONV
   if (domain->codeset_cntr
@@ -774,8 +823,9 @@ _nl_find_msg (domain_file, domainbinding, msgid, lengthp)
         NULs.  */
 
       if (domain->conv_tab == NULL
-         && ((domain->conv_tab = (char **) calloc (domain->nstrings,
-                                                   sizeof (char *)))
+         && ((domain->conv_tab =
+                (char **) calloc (nstrings + domain->n_sysdep_strings,
+                                  sizeof (char *)))
              == NULL))
        /* Mark that we didn't succeed allocating a table.  */
        domain->conv_tab = (char **) -1;
@@ -834,7 +884,7 @@ _nl_find_msg (domain_file, domainbinding, msgid, lengthp)
                  goto converted;
                }
 
-             inbuf = result;
+             inbuf = (const unsigned char *) result;
 # else
 #  if HAVE_ICONV
              const char *inptr = (const char *) inbuf;
@@ -902,7 +952,7 @@ _nl_find_msg (domain_file, domainbinding, msgid, lengthp)
              newmem->next = transmem_list;
              transmem_list = newmem;
 
-             freemem = newmem->data;
+             freemem = (unsigned char *) newmem->data;
              freemem_size -= offsetof (struct transmem_list, data);
 # else
              transmem_list = newmem;
@@ -981,88 +1031,7 @@ plural_lookup (domain, n, translation, translation_len)
   return (char *) p;
 }
 
-
-/* Function to evaluate the plural expression and return an index value.  */
-static unsigned long int
-internal_function
-plural_eval (pexp, n)
-     struct expression *pexp;
-     unsigned long int n;
-{
-  switch (pexp->nargs)
-    {
-    case 0:
-      switch (pexp->operation)
-       {
-       case var:
-         return n;
-       case num:
-         return pexp->val.num;
-       default:
-         break;
-       }
-      /* NOTREACHED */
-      break;
-    case 1:
-      {
-       /* pexp->operation must be lnot.  */
-       unsigned long int arg = plural_eval (pexp->val.args[0], n);
-       return ! arg;
-      }
-    case 2:
-      {
-       unsigned long int leftarg = plural_eval (pexp->val.args[0], n);
-       if (pexp->operation == lor)
-         return leftarg || plural_eval (pexp->val.args[1], n);
-       else if (pexp->operation == land)
-         return leftarg && plural_eval (pexp->val.args[1], n);
-       else
-         {
-           unsigned long int rightarg = plural_eval (pexp->val.args[1], n);
-
-           switch (pexp->operation)
-             {
-             case mult:
-               return leftarg * rightarg;
-             case divide:
-               return leftarg / rightarg;
-             case module:
-               return leftarg % rightarg;
-             case plus:
-               return leftarg + rightarg;
-             case minus:
-               return leftarg - rightarg;
-             case less_than:
-               return leftarg < rightarg;
-             case greater_than:
-               return leftarg > rightarg;
-             case less_or_equal:
-               return leftarg <= rightarg;
-             case greater_or_equal:
-               return leftarg >= rightarg;
-             case equal:
-               return leftarg == rightarg;
-             case not_equal:
-               return leftarg != rightarg;
-             default:
-               break;
-             }
-         }
-       /* NOTREACHED */
-       break;
-      }
-    case 3:
-      {
-       /* pexp->operation must be qmop.  */
-       unsigned long int boolarg = plural_eval (pexp->val.args[0], n);
-       return plural_eval (pexp->val.args[boolarg ? 1 : 2], n);
-      }
-    }
-  /* NOTREACHED */
-  return 0;
-}
-
-
+#ifndef _LIBC
 /* Return string representation of locale CATEGORY.  */
 static const char *
 internal_function
@@ -1122,6 +1091,7 @@ category_to_name (category)
 
   return retval;
 }
+#endif
 
 /* Guess value of current locale from value of the environment variables.  */
 static const char *
@@ -1143,25 +1113,10 @@ guess_category_value (category, categoryname)
   /* We have to proceed with the POSIX methods of looking to `LC_ALL',
      `LC_xxx', and `LANG'.  On some systems this can be done by the
      `setlocale' function itself.  */
-#if defined _LIBC || (defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL)
-  retval = setlocale (category, NULL);
+#ifdef _LIBC
+  retval = __current_locale_name (category);
 #else
-  /* Setting of LC_ALL overwrites all other.  */
-  retval = getenv ("LC_ALL");
-  if (retval == NULL || retval[0] == '\0')
-    {
-      /* Next comes the name of the desired category.  */
-      retval = getenv (categoryname);
-      if (retval == NULL || retval[0] == '\0')
-       {
-         /* Last possibility is the LANG environment variable.  */
-         retval = getenv ("LANG");
-         if (retval == NULL || retval[0] == '\0')
-           /* We use C as the default domain.  POSIX says this is
-              implementation defined.  */
-           return "C";
-       }
-    }
+  retval = _nl_locale_name (category, categoryname);
 #endif
 
   return language != NULL && strcmp (retval, "C") != 0 ? language : retval;
@@ -1200,8 +1155,7 @@ mempcpy (dest, src, n)
 #ifdef _LIBC
 /* If we want to free all resources we have to do some work at
    program's end.  */
-static void __attribute__ ((unused))
-free_mem (void)
+libc_freeres_fn (free_mem)
 {
   void *old;
 
@@ -1231,6 +1185,4 @@ free_mem (void)
       free (old);
     }
 }
-
-text_set_element (__libc_subfreeres, free_mem);
 #endif