home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / intl / cat-compat.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  6KB  |  208 lines

  1. /* Compatibility code for gettext-using-catgets interface.
  2.    Copyright (C) 1995 Free Software Foundation, Inc.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library 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 GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22.  
  23. #include <stdlib.h>
  24.  
  25. #ifdef HAVE_NL_TYPES_H
  26. # include <nl_types.h>
  27. #endif
  28.  
  29. #include "libgettext.h"
  30.  
  31. /* @@ end of prolog @@ */
  32.  
  33. extern char *xstrdup __P ((const char *));
  34.  
  35. /* The catalog descriptor.  */
  36. static nl_catd catalog = (nl_catd) -1;
  37.  
  38. /* Name of the default catalog.  */
  39. static char default_catalog_name[] = "messages";
  40.  
  41. /* Name of currently used catalog.  */
  42. static char *catalog_name = default_catalog_name;
  43.  
  44. /* Get ID for given string.  If not found return -1.  */
  45. static int msg_to_cat_id (const char *msg);
  46.  
  47.  
  48. /* Set currently used domain/catalog.  */
  49. char *
  50. textdomain (domainname)
  51.      const char *domainname;
  52. {
  53.   nl_catd new_catalog;
  54.   char *new_name;
  55.   size_t new_name_len;
  56.   char *lang;
  57.  
  58. #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES
  59.   lang = setlocale (LC_MESSAGES, NULL);
  60. #else
  61.   lang = getenv ("LC_MESSAGES");
  62.   if (lang == NULL || lang[0] == '\0')
  63.     lang = getenv ("LANG");
  64. #endif
  65.   if (lang == NULL || lang[0] == '\0')
  66.     lang = "C";
  67.  
  68.   /* See whether name of currently used domain is asked.  */
  69.   if (domainname == NULL)
  70.     return catalog_name;
  71.  
  72.   if (domainname[0] == '\0')
  73.     domainname = default_catalog_name;
  74.  
  75.   /* Compute length of added path element.  */
  76.   new_name_len = sizeof (DEF_MSG_DOM_DIR) - 1 + 1 + strlen (lang)
  77.          + sizeof ("/LC_MESSAGES/") - 1 + sizeof (PACKAGE) - 1
  78.          + sizeof (".cat");
  79.  
  80.   new_name = (char *) xmalloc (new_name_len);
  81.  
  82.   sprintf (new_name, "%s/%s/LC_MESSAGES/%s.cat", DEF_MSG_DOM_DIR, lang,
  83.        PACKAGE);
  84.  
  85.   new_catalog = catopen (new_name, 0);
  86.   if (new_catalog == (nl_catd) -1)
  87.     {
  88.       /* The system seems not to understand an absolute file name as
  89.      argument to catopen.  Try now with the established NLSPATH.  */
  90.       stpcpy (new_name, PACKAGE);
  91.  
  92.       new_catalog = catopen (new_name, 0);
  93.       if (new_catalog == (nl_catd) -1)
  94.     {
  95.       free (new_name);
  96.       return catalog_name;
  97.     }
  98.     }
  99.  
  100.   /* Close old catalog.  */
  101.   if (catalog != (nl_catd) -1)
  102.     catclose (catalog);
  103.   if (catalog_name != default_catalog_name)
  104.     free (catalog_name);
  105.  
  106.   catalog = new_catalog;
  107.   catalog_name = new_name;
  108.  
  109.   return catalog_name;
  110. }
  111.  
  112. char *
  113. bindtextdomain (domainname, dirname)
  114.      const char *domainname;
  115.      const char *dirname;
  116. {
  117. #if defined HAVE_SETENV || defined HAVE_PUTENV
  118.   char *old_val = getenv ("NLSPATH");
  119.   char *new_val;
  120.   size_t new_val_len;
  121.  
  122.   /* This does not make much sense here but to be compatible do it.  */
  123.   if (domainname == NULL)
  124.     return NULL;
  125.  
  126.   /* Compute length of added path element.  If we use setenv we don't need
  127.      the first byts for NLSPATH=, but why complicate the code for this
  128.      peanuts.  */
  129.   new_val_len = sizeof ("NLSPATH=") - 1 + sizeof (DEF_MSG_DOM_DIR) - 1
  130.         + sizeof ("/%L/LC_MESSAGES/") - 1 + sizeof (PACKAGE) - 1
  131.         + sizeof (".cat");
  132.  
  133.   if (old_val != NULL && old_val[0] != '\0')
  134.     new_val_len += strlen (old_val);
  135.  
  136.   new_val = (char *) xmalloc (new_val_len);
  137.  
  138. # ifdef HAVE_SETENV
  139. #  if __STDC__
  140.   stpcpy (stpcpy (stpcpy (new_val,
  141.               DEF_MSG_DOM_DIR "/%L/LC_MESSAGES/" PACKAGE ".cat"),
  142. #  else  
  143.   stpcpy (stpcpy (stpcpy (stpcpy (stpcpy (stpcpy DEF_MSG_DOM_DIR),
  144.                       "/%L/LC_MESSAGES/"),
  145.                   PACKAGE),
  146.               ".cat"),
  147. #  endif
  148.             old_val != NULL && old_val[0] != '\0' ? ":" : ""),
  149.       old_val != NULL && old_val[0] != '\0' ? old_val : "");
  150.  
  151.   setenv ("NLSPATH", new_val, 1);
  152. # else
  153. #  if __STDC__
  154.   stpcpy (stpcpy (stpcpy (new_val,
  155.               "NLSPATH=" DEF_MSG_DOM_DIR "/%L/LC_MESSAGES/"
  156.               PACKAGE ".cat"),
  157. #  else  
  158.   stpcpy (stpcpy (stpcpy (stpcpy (stpcpy (stpcpy (stpcpy (new_val, "NLSPATH="),
  159.                           DEF_MSG_DOM_DIR),
  160.                       "/%L/LC_MESSAGES/"),
  161.                   PACKAGE),
  162.               ".cat"),
  163. #  endif
  164.             old_val != NULL && old_val[0] != '\0' ? ":" : ""),
  165.       old_val != NULL && old_val[0] != '\0' ? old_val : "");
  166.  
  167.   putenv (new_val);
  168. # endif
  169. #endif
  170.  
  171.   return (char *) domainname;
  172. }
  173.  
  174. #undef gettext
  175. char *
  176. gettext (msg)
  177.      const char *msg;
  178. {
  179.   int msgid;
  180.  
  181.   if (msg == NULL || catalog == (nl_catd) -1)
  182.     return (char *) msg;
  183.  
  184.   /* Get the message from the catalog.  We always use set number 1.
  185.      The message ID is computed by the function `msg_to_cat_id'
  186.      which works on the table generated by `po-to-tbl'.  */
  187.   msgid = msg_to_cat_id (msg);
  188.   if (msgid == -1)
  189.     return (char *) msg;
  190.  
  191.   return catgets (catalog, 1, msgid, (char *) msg);
  192. }
  193.  
  194. /* Look through the table `_msg_tbl' which has `_msg_tbl_length' entries
  195.    for the one equal to msg.  If it is found return the ID.  In case when
  196.    the string is not found return -1.  */
  197. static int
  198. msg_to_cat_id (const char *msg)
  199. {
  200.   int cnt;
  201.  
  202.   for (cnt = 0; cnt < _msg_tbl_length; ++cnt)
  203.     if (strcmp (msg, _msg_tbl[cnt]._msg) == 0)
  204.       return _msg_tbl[cnt]._msg_number;
  205.  
  206.   return -1;
  207. }
  208.