home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / id-utils-3.2-src.tgz / tar.out / fsf / id-utils / intl / localealias.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  9KB  |  375 lines

  1. /* localealias.c -- handle aliases for locale names
  2.    Copyright (C) 1995, 1996 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21.  
  22. #include <ctype.h>
  23. #include <stdio.h>
  24. #include <sys/types.h>
  25.  
  26. #ifdef __GNUC__
  27. # define alloca __builtin_alloca
  28. # define HAVE_ALLOCA 1
  29. #else
  30. # if defined HAVE_ALLOCA_H || defined _LIBC
  31. #  include <alloca.h>
  32. # else
  33. #  ifdef _AIX
  34.  #pragma alloca
  35. #  else
  36. #   ifndef alloca
  37. char *alloca ();
  38. #   endif
  39. #  endif
  40. # endif
  41. #endif
  42.  
  43. #if defined STDC_HEADERS || defined _LIBC
  44. # include <stdlib.h>
  45. #else
  46. char *getenv ();
  47. # ifdef HAVE_MALLOC_H
  48. #  include <malloc.h>
  49. # else
  50. void free ();
  51. # endif
  52. #endif
  53.  
  54. #if defined HAVE_STRING_H || defined _LIBC
  55. # ifndef _GNU_SOURCE
  56. #  define _GNU_SOURCE    1
  57. # endif
  58. # include <string.h>
  59. #else
  60. # include <strings.h>
  61. #endif
  62. #if !HAVE_STRCHR && !defined _LIBC
  63. # ifndef strchr
  64. #  define strchr index
  65. # endif
  66. #endif
  67.  
  68. #include "gettext.h"
  69. #include "gettextP.h"
  70.  
  71. /* @@ end of prolog @@ */
  72.  
  73. #ifdef _LIBC
  74. /* Rename the non ANSI C functions.  This is required by the standard
  75.    because some ANSI C functions will require linking with this object
  76.    file and the name space must not be polluted.  */
  77. # define strcasecmp __strcasecmp
  78. #endif
  79.  
  80.  
  81. /* For those loosing systems which don't have `alloca' we have to add
  82.    some additional code emulating it.  */
  83. #ifdef HAVE_ALLOCA
  84. /* Nothing has to be done.  */
  85. # define ADD_BLOCK(list, address) /* nothing */
  86. # define FREE_BLOCKS(list) /* nothing */
  87. #else
  88. struct block_list
  89. {
  90.   void *address;
  91.   struct block_list *next;
  92. };
  93. # define ADD_BLOCK(list, addr)                              \
  94.   do {                                          \
  95.     struct block_list *newp = (struct block_list *) malloc (sizeof (*newp));  \
  96.     /* If we cannot get a free block we cannot add the new element to          \
  97.        the list.  */                                  \
  98.     if (newp != NULL) {                                  \
  99.       newp->address = (addr);                              \
  100.       newp->next = (list);                              \
  101.       (list) = newp;                                  \
  102.     }                                          \
  103.   } while (0)
  104. # define FREE_BLOCKS(list)                              \
  105.   do {                                          \
  106.     while (list != NULL) {                              \
  107.       struct block_list *old = list;                          \
  108.       list = list->next;                              \
  109.       free (old);                                  \
  110.     }                                          \
  111.   } while (0)
  112. # undef alloca
  113. # define alloca(size) (malloc (size))
  114. #endif    /* have alloca */
  115.  
  116.  
  117. struct alias_map
  118. {
  119.   const char *alias;
  120.   const char *value;
  121. };
  122.  
  123.  
  124. static struct alias_map *map;
  125. static size_t nmap = 0;
  126. static size_t maxmap = 0;
  127.  
  128.  
  129. /* Prototypes for local functions.  */
  130. static size_t read_alias_file PARAMS ((const char *fname, int fname_len));
  131. static void extend_alias_table PARAMS ((void));
  132. static int alias_compare PARAMS ((const struct alias_map *map1,
  133.                   const struct alias_map *map2));
  134.  
  135.  
  136. const char *
  137. _nl_expand_alias (name)
  138.     const char *name;
  139. {
  140.   static const char *locale_alias_path = LOCALE_ALIAS_PATH;
  141.   struct alias_map *retval;
  142.   size_t added;
  143.  
  144.   do
  145.     {
  146.       struct alias_map item;
  147.  
  148.       item.alias = name;
  149.  
  150.       if (nmap > 0)
  151.     retval = (struct alias_map *) bsearch (&item, map, nmap,
  152.                            sizeof (struct alias_map),
  153.                            (int (*) PARAMS ((const void *,
  154.                                  const void *))
  155.                         ) alias_compare);
  156.       else
  157.     retval = NULL;
  158.  
  159.       /* We really found an alias.  Return the value.  */
  160.       if (retval != NULL)
  161.     return retval->value;
  162.  
  163.       /* Perhaps we can find another alias file.  */
  164.       added = 0;
  165.       while (added == 0 && locale_alias_path[0] != '\0')
  166.     {
  167.       const char *start;
  168.  
  169.       while (locale_alias_path[0] == ':')
  170.         ++locale_alias_path;
  171.       start = locale_alias_path;
  172.  
  173.       while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
  174.         ++locale_alias_path;
  175.  
  176.       if (start < locale_alias_path)
  177.         added = read_alias_file (start, locale_alias_path - start);
  178.     }
  179.     }
  180.   while (added != 0);
  181.  
  182.   return NULL;
  183. }
  184.  
  185.  
  186. static size_t
  187. read_alias_file (fname, fname_len)
  188.      const char *fname;
  189.      int fname_len;
  190. {
  191. #ifndef HAVE_ALLOCA
  192.   struct block_list *block_list = NULL;
  193. #endif
  194.   FILE *fp;
  195.   char *full_fname;
  196.   size_t added;
  197.   static const char aliasfile[] = "/locale.alias";
  198.  
  199.   full_fname = (char *) alloca (fname_len + sizeof aliasfile);
  200.   ADD_BLOCK (block_list, full_fname);
  201.   memcpy (full_fname, fname, fname_len);
  202.   memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
  203.  
  204.   fp = fopen (full_fname, "r");
  205.   if (fp == NULL)
  206.     {
  207.       FREE_BLOCKS (block_list);
  208.       return 0;
  209.     }
  210.  
  211.   added = 0;
  212.   while (!feof (fp))
  213.     {
  214.       /* It is a reasonable approach to use a fix buffer here because
  215.      a) we are only interested in the first two fields
  216.      b) these fields must be usable as file names and so must not
  217.         be that long
  218.        */
  219.       char buf[BUFSIZ];
  220.       char *alias;
  221.       char *value;
  222.       char *cp;
  223.  
  224.       if (fgets (buf, BUFSIZ, fp) == NULL)
  225.     /* EOF reached.  */
  226.     break;
  227.  
  228.       cp = buf;
  229.       /* Ignore leading white space.  */
  230.       while (isspace (cp[0]))
  231.     ++cp;
  232.  
  233.       /* A leading '#' signals a comment line.  */
  234.       if (cp[0] != '\0' && cp[0] != '#')
  235.     {
  236.       alias = cp++;
  237.       while (cp[0] != '\0' && !isspace (cp[0]))
  238.         ++cp;
  239.       /* Terminate alias name.  */
  240.       if (cp[0] != '\0')
  241.         *cp++ = '\0';
  242.  
  243.       /* Now look for the beginning of the value.  */
  244.       while (isspace (cp[0]))
  245.         ++cp;
  246.  
  247.       if (cp[0] != '\0')
  248.         {
  249.           char *tp;
  250.           size_t len;
  251.  
  252.           value = cp++;
  253.           while (cp[0] != '\0' && !isspace (cp[0]))
  254.         ++cp;
  255.           /* Terminate value.  */
  256.           if (cp[0] == '\n')
  257.         {
  258.           /* This has to be done to make the following test
  259.              for the end of line possible.  We are looking for
  260.              the terminating '\n' which do not overwrite here.  */
  261.           *cp++ = '\0';
  262.           *cp = '\n';
  263.         }
  264.           else if (cp[0] != '\0')
  265.         *cp++ = '\0';
  266.  
  267.           if (nmap >= maxmap)
  268.         extend_alias_table ();
  269.  
  270.           /* We cannot depend on strdup available in the libc.  Sigh!  */
  271.           len = strlen (alias) + 1;
  272.           tp = (char *) malloc (len);
  273.           if (tp == NULL)
  274.         {
  275.           FREE_BLOCKS (block_list);
  276.           return added;
  277.         }
  278.           memcpy (tp, alias, len);
  279.           map[nmap].alias = tp;
  280.  
  281.           len = strlen (value) + 1;
  282.           tp = (char *) malloc (len);
  283.           if (tp == NULL)
  284.         {
  285.           FREE_BLOCKS (block_list);
  286.           return added;
  287.         }
  288.           memcpy (tp, value, len);
  289.           map[nmap].value = tp;
  290.  
  291.           ++nmap;
  292.           ++added;
  293.         }
  294.     }
  295.  
  296.       /* Possibily not the whole line fitted into the buffer.  Ignore
  297.      the rest of the line.  */
  298.       while (strchr (cp, '\n') == NULL)
  299.     {
  300.       cp = buf;
  301.       if (fgets (buf, BUFSIZ, fp) == NULL)
  302.         /* Make sure the inner loop will be left.  The outer loop
  303.            will exit at the `feof' test.  */
  304.         *cp = '\n';
  305.     }
  306.     }
  307.  
  308.   /* Should we test for ferror()?  I think we have to silently ignore
  309.      errors.  --drepper  */
  310.   fclose (fp);
  311.  
  312.   if (added > 0)
  313.     qsort (map, nmap, sizeof (struct alias_map),
  314.        (int (*) PARAMS ((const void *, const void *))) alias_compare);
  315.  
  316.   FREE_BLOCKS (block_list);
  317.   return added;
  318. }
  319.  
  320.  
  321. static void
  322. extend_alias_table ()
  323. {
  324.   size_t new_size;
  325.   struct alias_map *new_map;
  326.  
  327.   new_size = maxmap == 0 ? 100 : 2 * maxmap;
  328.   new_map = (struct alias_map *) malloc (new_size
  329.                      * sizeof (struct alias_map));
  330.   if (new_map == NULL)
  331.     /* Simply don't extend: we don't have any more core.  */
  332.     return;
  333.  
  334.   memcpy (new_map, map, nmap * sizeof (struct alias_map));
  335.  
  336.   if (maxmap != 0)
  337.     free (map);
  338.  
  339.   map = new_map;
  340.   maxmap = new_size;
  341. }
  342.  
  343.  
  344. static int
  345. alias_compare (map1, map2)
  346.      const struct alias_map *map1;
  347.      const struct alias_map *map2;
  348. {
  349. #if defined _LIBC || defined HAVE_STRCASECMP
  350.   return strcasecmp (map1->alias, map2->alias);
  351. #else
  352.   const unsigned char *p1 = (const unsigned char *) map1->alias;
  353.   const unsigned char *p2 = (const unsigned char *) map2->alias;
  354.   unsigned char c1, c2;
  355.  
  356.   if (p1 == p2)
  357.     return 0;
  358.  
  359.   do
  360.     {
  361.       /* I know this seems to be odd but the tolower() function in
  362.      some systems libc cannot handle nonalpha characters.  */
  363.       c1 = isupper (*p1) ? tolower (*p1) : *p1;
  364.       c2 = isupper (*p2) ? tolower (*p2) : *p2;
  365.       if (c1 == '\0')
  366.     break;
  367.       ++p1;
  368.       ++p2;
  369.     }
  370.   while (c1 == c2);
  371.  
  372.   return c1 - c2;
  373. #endif
  374. }
  375.