home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gettext-0.10.24-bin.lha / share / gettext / intl / dcgettext.c < prev    next >
C/C++ Source or Header  |  1996-10-12  |  16KB  |  597 lines

  1. /* dcgettext.c -- implementation of the dcgettext(3) function
  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 <sys/types.h>
  23.  
  24. #ifdef __GNUC__
  25. # define alloca __builtin_alloca
  26. # define HAVE_ALLOCA 1
  27. #else
  28. # if defined HAVE_ALLOCA_H || defined _LIBC
  29. #  include <alloca.h>
  30. # else
  31. #  ifdef _AIX
  32.  #pragma alloca
  33. #  else
  34. #   ifndef alloca
  35. char *alloca ();
  36. #   endif
  37. #  endif
  38. # endif
  39. #endif
  40.  
  41. #include <errno.h>
  42. #ifndef errno
  43. extern int errno;
  44. #endif
  45.  
  46. #if defined STDC_HEADERS || defined _LIBC
  47. # include <stdlib.h>
  48. #else
  49. char *getenv ();
  50. # ifdef HAVE_MALLOC_H
  51. #  include <malloc.h>
  52. # else
  53. void free ();
  54. # endif
  55. #endif
  56.  
  57. #if defined HAVE_STRING_H || defined _LIBC
  58. # ifndef _GNU_SOURCE
  59. #  define _GNU_SOURCE    1
  60. # endif
  61. # include <string.h>
  62. #else
  63. # include <strings.h>
  64. #endif
  65. #if !HAVE_STRCHR && !defined _LIBC
  66. # ifndef strchr
  67. #  define strchr index
  68. # endif
  69. #endif
  70.  
  71. #if defined HAVE_UNISTD_H || defined _LIBC
  72. # include <unistd.h>
  73. #endif
  74.  
  75. #include "gettext.h"
  76. #include "gettextP.h"
  77. #ifdef _LIBC
  78. # include <libintl.h>
  79. #else
  80. # include "libgettext.h"
  81. #endif
  82. #include "hash-string.h"
  83.  
  84. /* @@ end of prolog @@ */
  85.  
  86. #ifdef _LIBC
  87. /* Rename the non ANSI C functions.  This is required by the standard
  88.    because some ANSI C functions will require linking with this object
  89.    file and the name space must not be polluted.  */
  90. # define getcwd __getcwd
  91. # define stpcpy __stpcpy
  92. #else
  93. # if !defined HAVE_GETCWD
  94. char *getwd ();
  95. #  define getcwd(buf, max) getwd (buf)
  96. # else
  97. char *getcwd ();
  98. # endif
  99. # ifndef HAVE_STPCPY
  100. static char *stpcpy PARAMS ((char *dest, const char *src));
  101. # endif
  102. #endif
  103.  
  104. /* Amount to increase buffer size by in each try.  */
  105. #define PATH_INCR 32
  106.  
  107. /* The following is from pathmax.h.  */
  108. /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
  109.    PATH_MAX but might cause redefinition warnings when sys/param.h is
  110.    later included (as on MORE/BSD 4.3).  */
  111. #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
  112. # include <limits.h>
  113. #endif
  114.  
  115. #ifndef _POSIX_PATH_MAX
  116. # define _POSIX_PATH_MAX 255
  117. #endif
  118.  
  119. #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
  120. # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
  121. #endif
  122.  
  123. /* Don't include sys/param.h if it already has been.  */
  124. #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
  125. # include <sys/param.h>
  126. #endif
  127.  
  128. #if !defined(PATH_MAX) && defined(MAXPATHLEN)
  129. # define PATH_MAX MAXPATHLEN
  130. #endif
  131.  
  132. #ifndef PATH_MAX
  133. # define PATH_MAX _POSIX_PATH_MAX
  134. #endif
  135.  
  136. /* XPG3 defines the result of `setlocale (category, NULL)' as:
  137.    ``Directs `setlocale()' to query `category' and return the current
  138.      setting of `local'.''
  139.    However it does not specify the exact format.  And even worse: POSIX
  140.    defines this not at all.  So we can use this feature only on selected
  141.    system (e.g. those using GNU C Library).  */
  142. #ifdef _LIBC
  143. # define HAVE_LOCALE_NULL
  144. #endif
  145.  
  146. /* Name of the default domain used for gettext(3) prior any call to
  147.    textdomain(3).  The default value for this is "messages".  */
  148. const char _nl_default_default_domain[] = "messages";
  149.  
  150. /* Value used as the default domain for gettext(3).  */
  151. const char *_nl_current_default_domain = _nl_default_default_domain;
  152.  
  153. /* Contains the default location of the message catalogs.  */
  154. const char _nl_default_dirname[] = GNULOCALEDIR;
  155.  
  156. /* List with bindings of specific domains created by bindtextdomain()
  157.    calls.  */
  158. struct binding *_nl_domain_bindings;
  159.  
  160. /* Prototypes for local functions.  */
  161. static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
  162.                    const char *msgid));
  163. static const char *category_to_name PARAMS ((int category));
  164. static const char *guess_category_value PARAMS ((int category,
  165.                          const char *categoryname));
  166.  
  167.  
  168. /* For those loosing systems which don't have `alloca' we have to add
  169.    some additional code emulating it.  */
  170. #ifdef HAVE_ALLOCA
  171. /* Nothing has to be done.  */
  172. # define ADD_BLOCK(list, address) /* nothing */
  173. # define FREE_BLOCKS(list) /* nothing */
  174. #else
  175. struct block_list
  176. {
  177.   void *address;
  178.   struct block_list *next;
  179. };
  180. # define ADD_BLOCK(list, addr)                              \
  181.   do {                                          \
  182.     struct block_list *newp = (struct block_list *) malloc (sizeof (*newp));  \
  183.     /* If we cannot get a free block we cannot add the new element to          \
  184.        the list.  */                                  \
  185.     if (newp != NULL) {                                  \
  186.       newp->address = (addr);                              \
  187.       newp->next = (list);                              \
  188.       (list) = newp;                                  \
  189.     }                                          \
  190.   } while (0)
  191. # define FREE_BLOCKS(list)                              \
  192.   do {                                          \
  193.     while (list != NULL) {                              \
  194.       struct block_list *old = list;                          \
  195.       list = list->next;                              \
  196.       free (old);                                  \
  197.     }                                          \
  198.   } while (0)
  199. # undef alloca
  200. # define alloca(size) (malloc (size))
  201. #endif    /* have alloca */
  202.  
  203.  
  204. /* Names for the libintl functions are a problem.  They must not clash
  205.    with existing names and they should follow ANSI C.  But this source
  206.    code is also used in GNU C Library where the names have a __
  207.    prefix.  So we have to make a difference here.  */
  208. #ifdef _LIBC
  209. # define DCGETTEXT __dcgettext
  210. #else
  211. # define DCGETTEXT dcgettext__
  212. #endif
  213.  
  214. /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
  215.    locale.  */
  216. char *
  217. DCGETTEXT (domainname, msgid, category)
  218.      const char *domainname;
  219.      const char *msgid;
  220.      int category;
  221. {
  222. #ifndef HAVE_ALLOCA
  223.   struct block_list *block_list = NULL;
  224. #endif
  225.   struct loaded_l10nfile *domain;
  226.   struct binding *binding;
  227.   const char *categoryname;
  228.   const char *categoryvalue;
  229.   char *dirname, *xdomainname;
  230.   char *single_locale;
  231.   char *retval;
  232.   int saved_errno = errno;
  233.  
  234.   /* If no real MSGID is given return NULL.  */
  235.   if (msgid == NULL)
  236.     return NULL;
  237.  
  238.   /* If DOMAINNAME is NULL, we are interested in the default domain.  If
  239.      CATEGORY is not LC_MESSAGES this might not make much sense but the
  240.      defintion left this undefined.  */
  241.   if (domainname == NULL)
  242.     domainname = _nl_current_default_domain;
  243.  
  244.   /* First find matching binding.  */
  245.   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
  246.     {
  247.       int compare = strcmp (domainname, binding->domainname);
  248.       if (compare == 0)
  249.     /* We found it!  */
  250.     break;
  251.       if (compare < 0)
  252.     {
  253.       /* It is not in the list.  */
  254.       binding = NULL;
  255.       break;
  256.     }
  257.     }
  258.  
  259.   if (binding == NULL)
  260.     dirname = (char *) _nl_default_dirname;
  261.   else if (binding->dirname[0] == '/')
  262.     dirname = binding->dirname;
  263.   else
  264.     {
  265.       /* We have a relative path.  Make it absolute now.  */
  266.       size_t dirname_len = strlen (binding->dirname) + 1;
  267.       size_t path_max;
  268.       char *ret;
  269.  
  270.       path_max = (unsigned) PATH_MAX;
  271.       path_max += 2;        /* The getcwd docs say to do this.  */
  272.  
  273.       dirname = (char *) alloca (path_max + dirname_len);
  274.       ADD_BLOCK (block_list, dirname);
  275.  
  276.       errno = 0;
  277.       while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
  278.     {
  279.       path_max += PATH_INCR;
  280.       dirname = (char *) alloca (path_max + dirname_len);
  281.       ADD_BLOCK (block_list, dirname);
  282.       errno = 0;
  283.     }
  284.  
  285.       if (ret == NULL)
  286.     {
  287.       /* We cannot get the current working directory.  Don't signal an
  288.          error but simply return the default string.  */
  289.       FREE_BLOCKS (block_list);
  290.       errno = saved_errno;
  291.       return (char *) msgid;
  292.     }
  293.  
  294.       /* We don't want libintl.a to depend on any other library.  So
  295.      we avoid the non-standard function stpcpy.  In GNU C Library
  296.      this function is available, though.  Also allow the symbol
  297.      HAVE_STPCPY to be defined.  */
  298.       stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
  299.     }
  300.  
  301.   /* Now determine the symbolic name of CATEGORY and its value.  */
  302.   categoryname = category_to_name (category);
  303.   categoryvalue = guess_category_value (category, categoryname);
  304.  
  305.   xdomainname = (char *) alloca (strlen (categoryname)
  306.                  + strlen (domainname) + 5);
  307.   ADD_BLOCK (block_list, xdomainname);
  308.   /* We don't want libintl.a to depend on any other library.  So we
  309.      avoid the non-standard function stpcpy.  In GNU C Library this
  310.      function is available, though.  Also allow the symbol HAVE_STPCPY
  311.      to be defined.  */
  312.   stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
  313.           domainname),
  314.       ".mo");
  315.  
  316.   /* Creating working area.  */
  317.   single_locale = (char *) alloca (strlen (categoryvalue) + 1);
  318.   ADD_BLOCK (block_list, single_locale);
  319.  
  320.  
  321.   /* Search for the given string.  This is a loop because we perhaps
  322.      got an ordered list of languages to consider for th translation.  */
  323.   while (1)
  324.     {
  325.       /* Make CATEGORYVALUE point to the next element of the list.  */
  326.       while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
  327.     ++categoryvalue;
  328.       if (categoryvalue[0] == '\0')
  329.     {
  330.       /* The whole contents of CATEGORYVALUE has been searched but
  331.          no valid entry has been found.  We solve this situation
  332.          by implicitely appending a "C" entry, i.e. no translation
  333.          will take place.  */
  334.       single_locale[0] = 'C';
  335.       single_locale[1] = '\0';
  336.     }
  337.       else
  338.     {
  339.       char *cp = single_locale;
  340.       while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
  341.         *cp++ = *categoryvalue++;
  342.       *cp = '\0';
  343.     }
  344.  
  345.       /* If the current locale value is C (or POSIX) we don't load a
  346.      domain.  Return the MSGID.  */
  347.       if (strcmp (single_locale, "C") == 0
  348.       || strcmp (single_locale, "POSIX") == 0)
  349.     {
  350.       FREE_BLOCKS (block_list);
  351.       errno = saved_errno;
  352.       return (char *) msgid;
  353.     }
  354.  
  355.  
  356.       /* Find structure describing the message catalog matching the
  357.      DOMAINNAME and CATEGORY.  */
  358.       domain = _nl_find_domain (dirname, single_locale, xdomainname);
  359.  
  360.       if (domain != NULL)
  361.     {
  362.       retval = find_msg (domain, msgid);
  363.  
  364.       if (retval == NULL)
  365.         {
  366.           int cnt;
  367.  
  368.           for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
  369.         {
  370.           retval = find_msg (domain->successor[cnt], msgid);
  371.  
  372.           if (retval != NULL)
  373.             break;
  374.         }
  375.         }
  376.  
  377.       if (retval != NULL)
  378.         {
  379.           FREE_BLOCKS (block_list);
  380.           errno = saved_errno;
  381.           return retval;
  382.         }
  383.     }
  384.     }
  385.   /* NOTREACHED */
  386. }
  387.  
  388. #ifdef _LIBC
  389. /* Alias for function name in GNU C Library.  */
  390. weak_alias (__dcgettext, dcgettext);
  391. #endif
  392.  
  393.  
  394. static char *
  395. find_msg (domain_file, msgid)
  396.      struct loaded_l10nfile *domain_file;
  397.      const char *msgid;
  398. {
  399.   size_t top, act, bottom;
  400.   struct loaded_domain *domain;
  401.  
  402.   if (domain_file->decided == 0)
  403.     _nl_load_domain (domain_file);
  404.  
  405.   if (domain_file->data == NULL)
  406.     return NULL;
  407.  
  408.   domain = (struct loaded_domain *) domain_file->data;
  409.  
  410.   /* Locate the MSGID and its translation.  */
  411.   if (domain->hash_size > 2 && domain->hash_tab != NULL)
  412.     {
  413.       /* Use the hashing table.  */
  414.       nls_uint32 len = strlen (msgid);
  415.       nls_uint32 hash_val = hash_string (msgid);
  416.       nls_uint32 idx = hash_val % domain->hash_size;
  417.       nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
  418.       nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
  419.  
  420.       if (nstr == 0)
  421.     /* Hash table entry is empty.  */
  422.     return NULL;
  423.  
  424.       if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
  425.       && strcmp (msgid,
  426.              domain->data + W (domain->must_swap,
  427.                        domain->orig_tab[nstr - 1].offset)) == 0)
  428.     return (char *) domain->data + W (domain->must_swap,
  429.                       domain->trans_tab[nstr - 1].offset);
  430.  
  431.       while (1)
  432.     {
  433.       if (idx >= domain->hash_size - incr)
  434.         idx -= domain->hash_size - incr;
  435.       else
  436.         idx += incr;
  437.  
  438.       nstr = W (domain->must_swap, domain->hash_tab[idx]);
  439.       if (nstr == 0)
  440.         /* Hash table entry is empty.  */
  441.         return NULL;
  442.  
  443.       if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
  444.           && strcmp (msgid,
  445.              domain->data + W (domain->must_swap,
  446.                        domain->orig_tab[nstr - 1].offset))
  447.              == 0)
  448.         return (char *) domain->data
  449.           + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
  450.     }
  451.       /* NOTREACHED */
  452.     }
  453.  
  454.   /* Now we try the default method:  binary search in the sorted
  455.      array of messages.  */
  456.   bottom = 0;
  457.   top = domain->nstrings;
  458.   while (bottom < top)
  459.     {
  460.       int cmp_val;
  461.  
  462.       act = (bottom + top) / 2;
  463.       cmp_val = strcmp (msgid, domain->data
  464.                    + W (domain->must_swap,
  465.                     domain->orig_tab[act].offset));
  466.       if (cmp_val < 0)
  467.     top = act;
  468.       else if (cmp_val > 0)
  469.     bottom = act + 1;
  470.       else
  471.     break;
  472.     }
  473.  
  474.   /* If an translation is found return this.  */
  475.   return bottom >= top ? NULL : (char *) domain->data
  476.                                 + W (domain->must_swap,
  477.                      domain->trans_tab[act].offset);
  478. }
  479.  
  480.  
  481. /* Return string representation of locale CATEGORY.  */
  482. static const char *
  483. category_to_name (category)
  484.      int category;
  485. {
  486.   const char *retval;
  487.  
  488.   switch (category)
  489.   {
  490. #ifdef LC_COLLATE
  491.   case LC_COLLATE:
  492.     retval = "LC_COLLATE";
  493.     break;
  494. #endif
  495. #ifdef LC_CTYPE
  496.   case LC_CTYPE:
  497.     retval = "LC_CTYPE";
  498.     break;
  499. #endif
  500. #ifdef LC_MONETARY
  501.   case LC_MONETARY:
  502.     retval = "LC_MONETARY";
  503.     break;
  504. #endif
  505. #ifdef LC_NUMERIC
  506.   case LC_NUMERIC:
  507.     retval = "LC_NUMERIC";
  508.     break;
  509. #endif
  510. #ifdef LC_TIME
  511.   case LC_TIME:
  512.     retval = "LC_TIME";
  513.     break;
  514. #endif
  515. #ifdef LC_MESSAGES
  516.   case LC_MESSAGES:
  517.     retval = "LC_MESSAGES";
  518.     break;
  519. #endif
  520. #ifdef LC_RESPONSE
  521.   case LC_RESPONSE:
  522.     retval = "LC_RESPONSE";
  523.     break;
  524. #endif
  525. #ifdef LC_ALL
  526.   case LC_ALL:
  527.     /* This might not make sense but is perhaps better than any other
  528.        value.  */
  529.     retval = "LC_ALL";
  530.     break;
  531. #endif
  532.   default:
  533.     /* If you have a better idea for a default value let me know.  */
  534.     retval = "LC_XXX";
  535.   }
  536.  
  537.   return retval;
  538. }
  539.  
  540. /* Guess value of current locale from value of the environment variables.  */
  541. static const char *guess_category_value (category, categoryname)
  542.      int category;
  543.      const char *categoryname;
  544. {
  545.   const char *retval;
  546.  
  547.   /* The highest priority value is the `LANGUAGE' environment
  548.      variable.  This is a GNU extension.  */
  549.   retval = getenv ("LANGUAGE");
  550.   if (retval != NULL && retval[0] != '\0')
  551.     return retval;
  552.  
  553.   /* `LANGUAGE' is not set.  So we have to proceed with the POSIX
  554.      methods of looking to `LC_ALL', `LC_xxx', and `LANG'.  On some
  555.      systems this can be done by the `setlocale' function itself.  */
  556. #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
  557.   return setlocale (category, NULL);
  558. #else
  559.   /* Setting of LC_ALL overwrites all other.  */
  560.   retval = getenv ("LC_ALL");
  561.   if (retval != NULL && retval[0] != '\0')
  562.     return retval;
  563.  
  564.   /* Next comes the name of the desired category.  */
  565.   retval = getenv (categoryname);
  566.   if (retval != NULL && retval[0] != '\0')
  567.     return retval;
  568.  
  569.   /* Last possibility is the LANG environment variable.  */
  570.   retval = getenv ("LANG");
  571.   if (retval != NULL && retval[0] != '\0')
  572.     return retval;
  573.  
  574.   /* We use C as the default domain.  POSIX says this is implementation
  575.      defined.  */
  576.   return "C";
  577. #endif
  578. }
  579.  
  580. /* @@ begin of epilog @@ */
  581.  
  582. /* We don't want libintl.a to depend on any other library.  So we
  583.    avoid the non-standard function stpcpy.  In GNU C Library this
  584.    function is available, though.  Also allow the symbol HAVE_STPCPY
  585.    to be defined.  */
  586. #if !_LIBC && !HAVE_STPCPY
  587. static char *
  588. stpcpy (dest, src)
  589.      char *dest;
  590.      const char *src;
  591. {
  592.   while ((*dest++ = *src++) != '\0')
  593.     /* Do nothing. */ ;
  594.   return dest - 1;
  595. }
  596. #endif
  597.