Rocco Rutte:
[apps/madmutt.git] / intl / l10nflist.c
index 8e2605b..0501b97 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
+/* Copyright (C) 1995-1999, 2000-2004 Free Software Foundation, Inc.
    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
 
    This program is free software; you can redistribute it and/or modify it
@@ -13,7 +13,7 @@
 
    You should have received a copy of the GNU Library General Public
    License along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    USA.  */
 
 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
 #endif
 
 #include <string.h>
-#if !HAVE_STRCHR && !defined _LIBC
-# ifndef strchr
-#  define strchr index
-# endif
-#endif
 
 #if defined _LIBC || defined HAVE_ARGZ_H
 # include <argz.h>
 # endif
 #else
 # ifndef HAVE_STPCPY
-static char *stpcpy PARAMS ((char *dest, const char *src));
+static char *stpcpy (char *dest, const char *src);
 # endif
 #endif
 
+/* Pathname support.
+   ISSLASH(C)           tests whether C is a directory separator character.
+   IS_ABSOLUTE_PATH(P)  tests whether P is an absolute path.  If it is not,
+                        it may be concatenated to a directory pathname.
+ */
+#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
+  /* Win32, Cygwin, OS/2, DOS */
+# define ISSLASH(C) ((C) == '/' || (C) == '\\')
+# define HAS_DEVICE(P) \
+    ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
+     && (P)[1] == ':')
+# define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
+#else
+  /* Unix */
+# define ISSLASH(C) ((C) == '/')
+# define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
+#endif
+
 /* Define function which are usually not available.  */
 
 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
 /* Returns the number of strings in ARGZ.  */
-static size_t argz_count__ PARAMS ((const char *argz, size_t len));
-
 static size_t
-argz_count__ (argz, len)
-     const char *argz;
-     size_t len;
+argz_count__ (const char *argz, size_t len)
 {
   size_t count = 0;
   while (len > 0)
@@ -90,18 +99,17 @@ argz_count__ (argz, len)
 }
 # undef __argz_count
 # define __argz_count(argz, len) argz_count__ (argz, len)
+#else
+# ifdef _LIBC
+#  define __argz_count(argz, len) INTUSE(__argz_count) (argz, len)
+# endif
 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
 
 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
    except the last into the character SEP.  */
-static void argz_stringify__ PARAMS ((char *argz, size_t len, int sep));
-
 static void
-argz_stringify__ (argz, len, sep)
-     char *argz;
-     size_t len;
-     int sep;
+argz_stringify__ (char *argz, size_t len, int sep)
 {
   while (len > 0)
     {
@@ -114,17 +122,16 @@ argz_stringify__ (argz, len, sep)
 }
 # undef __argz_stringify
 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
+#else
+# ifdef _LIBC
+#  define __argz_stringify(argz, len, sep) \
+  INTUSE(__argz_stringify) (argz, len, sep)
+# endif
 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
 
 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
-static char *argz_next__ PARAMS ((char *argz, size_t argz_len,
-                                 const char *entry));
-
 static char *
-argz_next__ (argz, argz_len, entry)
-     char *argz;
-     size_t argz_len;
-     const char *entry;
+argz_next__ (char *argz, size_t argz_len, const char *entry)
 {
   if (entry)
     {
@@ -145,11 +152,8 @@ argz_next__ (argz, argz_len, entry)
 
 
 /* Return number of bits set in X.  */
-static int pop PARAMS ((int x));
-
 static inline int
-pop (x)
-     int x;
+pop (int x)
 {
   /* We assume that no more than 16 bits are used.  */
   x = ((x & ~0x5555) >> 1) + (x & 0x5555);
@@ -162,31 +166,27 @@ pop (x)
 
 \f
 struct loaded_l10nfile *
-_nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
-                   territory, codeset, normalized_codeset, modifier, special,
-                   sponsor, revision, filename, do_allocate)
-     struct loaded_l10nfile **l10nfile_list;
-     const char *dirlist;
-     size_t dirlist_len;
-     int mask;
-     const char *language;
-     const char *territory;
-     const char *codeset;
-     const char *normalized_codeset;
-     const char *modifier;
-     const char *special;
-     const char *sponsor;
-     const char *revision;
-     const char *filename;
-     int do_allocate;
+_nl_make_l10nflist (struct loaded_l10nfile **l10nfile_list,
+                   const char *dirlist, size_t dirlist_len,
+                   int mask, const char *language, const char *territory,
+                   const char *codeset, const char *normalized_codeset,
+                   const char *modifier, const char *special,
+                   const char *sponsor, const char *revision,
+                   const char *filename, int do_allocate)
 {
   char *abs_filename;
-  struct loaded_l10nfile *last = NULL;
+  struct loaded_l10nfile **lastp;
   struct loaded_l10nfile *retval;
   char *cp;
+  size_t dirlist_count;
   size_t entries;
   int cnt;
 
+  /* If LANGUAGE contains an absolute directory specification, we ignore
+     DIRLIST.  */
+  if (IS_ABSOLUTE_PATH (language))
+    dirlist_len = 0;
+
   /* Allocate room for the full file name.  */
   abs_filename = (char *) malloc (dirlist_len
                                  + strlen (language)
@@ -204,7 +204,7 @@ _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
                                  + (((mask & CEN_SPONSOR) != 0
                                      || (mask & CEN_REVISION) != 0)
                                     ? (1 + ((mask & CEN_SPONSOR) != 0
-                                            ? strlen (sponsor) + 1 : 0)
+                                            ? strlen (sponsor) : 0)
                                        + ((mask & CEN_REVISION) != 0
                                           ? strlen (revision) + 1 : 0)) : 0)
                                  + 1 + strlen (filename) + 1);
@@ -212,14 +212,16 @@ _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
   if (abs_filename == NULL)
     return NULL;
 
-  retval = NULL;
-  last = NULL;
-
   /* Construct file name.  */
-  memcpy (abs_filename, dirlist, dirlist_len);
-  __argz_stringify (abs_filename, dirlist_len, PATH_SEPARATOR);
-  cp = abs_filename + (dirlist_len - 1);
-  *cp++ = '/';
+  cp = abs_filename;
+  if (dirlist_len > 0)
+    {
+      memcpy (cp, dirlist, dirlist_len);
+      __argz_stringify (cp, dirlist_len, PATH_SEPARATOR);
+      cp += dirlist_len;
+      cp[-1] = '/';
+    }
+
   cp = stpcpy (cp, language);
 
   if ((mask & TERRITORY) != 0)
@@ -266,7 +268,7 @@ _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
 
   /* Look in list of already loaded domains whether it is already
      available.  */
-  last = NULL;
+  lastp = l10nfile_list;
   for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
     if (retval->filename != NULL)
       {
@@ -281,7 +283,7 @@ _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
            break;
          }
 
-       last = retval;
+       lastp = &retval->next;
       }
 
   if (retval != NULL || do_allocate == 0)
@@ -290,48 +292,66 @@ _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
       return retval;
     }
 
-  retval = (struct loaded_l10nfile *)
-    malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
-                               * (1 << pop (mask))
-                               * sizeof (struct loaded_l10nfile *)));
+  dirlist_count = (dirlist_len > 0 ? __argz_count (dirlist, dirlist_len) : 1);
+
+  /* Allocate a new loaded_l10nfile.  */
+  retval =
+    (struct loaded_l10nfile *)
+    malloc (sizeof (*retval)
+           + (((dirlist_count << pop (mask)) + (dirlist_count > 1 ? 1 : 0))
+              * sizeof (struct loaded_l10nfile *)));
   if (retval == NULL)
     return NULL;
 
   retval->filename = abs_filename;
-  retval->decided = (__argz_count (dirlist, dirlist_len) != 1
+
+  /* We set retval->data to NULL here; it is filled in later.
+     Setting retval->decided to 1 here means that retval does not
+     correspond to a real file (dirlist_count > 1) or is not worth
+     looking up (if an unnormalized codeset was specified).  */
+  retval->decided = (dirlist_count > 1
                     || ((mask & XPG_CODESET) != 0
                         && (mask & XPG_NORM_CODESET) != 0));
   retval->data = NULL;
 
-  if (last == NULL)
-    {
-      retval->next = *l10nfile_list;
-      *l10nfile_list = retval;
-    }
-  else
-    {
-      retval->next = last->next;
-      last->next = retval;
-    }
+  retval->next = *lastp;
+  *lastp = retval;
 
   entries = 0;
-  /* If the DIRLIST is a real list the RETVAL entry corresponds not to
-     a real file.  So we have to use the DIRLIST separation mechanism
-     of the inner loop.  */
-  cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
-  for (; cnt >= 0; --cnt)
+  /* Recurse to fill the inheritance list of RETVAL.
+     If the DIRLIST is a real list (i.e. DIRLIST_COUNT > 1), the RETVAL
+     entry does not correspond to a real file; retval->filename contains
+     colons.  In this case we loop across all elements of DIRLIST and
+     across all bit patterns dominated by MASK.
+     If the DIRLIST is a single directory or entirely redundant (i.e.
+     DIRLIST_COUNT == 1), we loop across all bit patterns dominated by
+     MASK, excluding MASK itself.
+     In either case, we loop down from MASK to 0.  This has the effect
+     that the extra bits in the locale name are dropped in this order:
+     first the modifier, then the territory, then the codeset, then the
+     normalized_codeset.  */
+  for (cnt = dirlist_count > 1 ? mask : mask - 1; cnt >= 0; --cnt)
     if ((cnt & ~mask) == 0
        && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
        && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
       {
-       /* Iterate over all elements of the DIRLIST.  */
-       char *dir = NULL;
-
-       while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
-              != NULL)
+       if (dirlist_count > 1)
+         {
+           /* Iterate over all elements of the DIRLIST.  */
+           char *dir = NULL;
+
+           while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
+                  != NULL)
+             retval->successor[entries++]
+               = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1,
+                                     cnt, language, territory, codeset,
+                                     normalized_codeset, modifier, special,
+                                     sponsor, revision, filename, 1);
+         }
+       else
          retval->successor[entries++]
-           = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
-                                 language, territory, codeset,
+           = _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len,
+                                 cnt, language, territory, codeset,
                                  normalized_codeset, modifier, special,
                                  sponsor, revision, filename, 1);
       }
@@ -345,9 +365,7 @@ _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
    names.  The return value is dynamically allocated and has to be
    freed by the caller.  */
 const char *
-_nl_normalize_codeset (codeset, name_len)
-     const char *codeset;
-     size_t name_len;
+_nl_normalize_codeset (const char *codeset, size_t name_len)
 {
   int len = 0;
   int only_digit = 1;
@@ -394,9 +412,7 @@ _nl_normalize_codeset (codeset, name_len)
    to be defined.  */
 #if !_LIBC && !HAVE_STPCPY
 static char *
-stpcpy (dest, src)
-     char *dest;
-     const char *src;
+stpcpy (char *dest, const char *src)
 {
   while ((*dest++ = *src++) != '\0')
     /* Do nothing. */ ;