home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gettext-0.10.24-bin.lha / share / gettext / intl / cat-compat.c < prev    next >
C/C++ Source or Header  |  1996-10-12  |  6KB  |  253 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., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA.  */
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22.  
  23. #include <stdio.h>
  24.  
  25. #ifdef STDC_HEADERS
  26. # include <stdlib.h>
  27. # include <string.h>
  28. #else
  29. char *getenv ();
  30. # ifdef HAVE_MALLOC_H
  31. #  include <malloc.h>
  32. # endif
  33. #endif
  34.  
  35. #ifdef HAVE_NL_TYPES_H
  36. # include <nl_types.h>
  37. #endif
  38.  
  39. #include "libgettext.h"
  40.  
  41. /* @@ end of prolog @@ */
  42.  
  43. /* The catalog descriptor.  */
  44. static nl_catd catalog = (nl_catd) -1;
  45.  
  46. /* Name of the default catalog.  */
  47. static const char default_catalog_name[] = "messages";
  48.  
  49. /* Name of currently used catalog.  */
  50. static const char *catalog_name = default_catalog_name;
  51.  
  52. /* Get ID for given string.  If not found return -1.  */
  53. static int msg_to_cat_id PARAMS ((const char *msg));
  54.  
  55. /* Substitution for systems lacking this function in their C library.  */
  56. #if !_LIBC && !HAVE_STPCPY
  57. static char *stpcpy PARAMS ((char *dest, const char *src));
  58. #endif
  59.  
  60.  
  61. /* Set currently used domain/catalog.  */
  62. char *
  63. textdomain (domainname)
  64.      const char *domainname;
  65. {
  66.   nl_catd new_catalog;
  67.   char *new_name;
  68.   size_t new_name_len;
  69.   char *lang;
  70.  
  71. #if HAVE_SETLOCALE && HAVE_LC_MESSAGES && HAVE_SETLOCALE_NULL
  72.   lang = setlocale (LC_MESSAGES, NULL);
  73. #else
  74.   lang = getenv ("LC_ALL");
  75.   if (lang == NULL || lang[0] == '\0')
  76.     {
  77.       lang = getenv ("LC_MESSAGES");
  78.       if (lang == NULL || lang[0] == '\0')
  79.     lang = getenv ("LANG");
  80.     }
  81. #endif
  82.   if (lang == NULL || lang[0] == '\0')
  83.     lang = "C";
  84.  
  85.   /* See whether name of currently used domain is asked.  */
  86.   if (domainname == NULL)
  87.     return (char *) catalog_name;
  88.  
  89.   if (domainname[0] == '\0')
  90.     domainname = default_catalog_name;
  91.  
  92.   /* Compute length of added path element.  */
  93.   new_name_len = sizeof (LOCALEDIR) - 1 + 1 + strlen (lang)
  94.          + sizeof ("/LC_MESSAGES/") - 1 + sizeof (PACKAGE) - 1
  95.          + sizeof (".cat");
  96.  
  97.   new_name = (char *) malloc (new_name_len);
  98.   if (new_name == NULL)
  99.     return NULL;
  100.  
  101.   strcpy (new_name, PACKAGE);
  102.   new_catalog = catopen (new_name, 0);
  103.  
  104.   if (new_catalog == (nl_catd) -1)
  105.     {
  106.       /* NLSPATH search didn't work, try absolute path */
  107.       sprintf (new_name, "%s/%s/LC_MESSAGES/%s.cat", LOCALEDIR, lang,
  108.            PACKAGE);
  109.       new_catalog = catopen (new_name, 0);
  110.  
  111.       if (new_catalog == (nl_catd) -1)
  112.     {
  113.       free (new_name);
  114.       return (char *) catalog_name;
  115.     }
  116.     }
  117.  
  118.   /* Close old catalog.  */
  119.   if (catalog != (nl_catd) -1)
  120.     catclose (catalog);
  121.   if (catalog_name != default_catalog_name)
  122.     free ((char *) catalog_name);
  123.  
  124.   catalog = new_catalog;
  125.   catalog_name = new_name;
  126.  
  127.   return (char *) catalog_name;
  128. }
  129.  
  130. char *
  131. bindtextdomain (domainname, dirname)
  132.      const char *domainname;
  133.      const char *dirname;
  134. {
  135. #if HAVE_SETENV || HAVE_PUTENV
  136.   char *old_val, *new_val, *cp;
  137.   size_t new_val_len;
  138.  
  139.   /* This does not make much sense here but to be compatible do it.  */
  140.   if (domainname == NULL)
  141.     return NULL;
  142.  
  143.   /* Compute length of added path element.  If we use setenv we don't need
  144.      the first byts for NLSPATH=, but why complicate the code for this
  145.      peanuts.  */
  146.   new_val_len = sizeof ("NLSPATH=") - 1 + strlen (dirname)
  147.         + sizeof ("/%L/LC_MESSAGES/%N.cat");
  148.  
  149.   old_val = getenv ("NLSPATH");
  150.   if (old_val == NULL || old_val[0] == '\0')
  151.     {
  152.       old_val = NULL;
  153.       new_val_len += 1 + sizeof (LOCALEDIR) - 1
  154.                  + sizeof ("/%L/LC_MESSAGES/%N.cat");
  155.     }
  156.   else
  157.     new_val_len += strlen (old_val);
  158.  
  159.   new_val = (char *) malloc (new_val_len);
  160.   if (new_val == NULL)
  161.     return NULL;
  162.  
  163. # if HAVE_SETENV
  164.   cp = new_val;
  165. # else
  166.   cp = stpcpy (new_val, "NLSPATH=");
  167. # endif
  168.  
  169.   cp = stpcpy (cp, dirname);
  170.   cp = stpcpy (cp, "/%L/LC_MESSAGES/%N.cat:");
  171.  
  172.   if (old_val == NULL)
  173.     {
  174. # if __STDC__
  175.       stpcpy (cp, LOCALEDIR "/%L/LC_MESSAGES/%N.cat");
  176. # else
  177.  
  178.       cp = stpcpy (cp, LOCALEDIR);
  179.       stpcpy (cp, "/%L/LC_MESSAGES/%N.cat");
  180. # endif
  181.     }
  182.   else
  183.     stpcpy (cp, old_val);
  184.  
  185. # if HAVE_SETENV
  186.   setenv ("NLSPATH", new_val, 1);
  187.   free (new_val);
  188. # else
  189.   putenv (new_val);
  190.   /* Do *not* free the environment entry we just entered.  It is used
  191.      from now on.   */
  192. # endif
  193.  
  194. #endif
  195.  
  196.   return (char *) domainname;
  197. }
  198.  
  199. #undef gettext
  200. char *
  201. gettext (msg)
  202.      const char *msg;
  203. {
  204.   int msgid;
  205.  
  206.   if (msg == NULL || catalog == (nl_catd) -1)
  207.     return (char *) msg;
  208.  
  209.   /* Get the message from the catalog.  We always use set number 1.
  210.      The message ID is computed by the function `msg_to_cat_id'
  211.      which works on the table generated by `po-to-tbl'.  */
  212.   msgid = msg_to_cat_id (msg);
  213.   if (msgid == -1)
  214.     return (char *) msg;
  215.  
  216.   return catgets (catalog, 1, msgid, (char *) msg);
  217. }
  218.  
  219. /* Look through the table `_msg_tbl' which has `_msg_tbl_length' entries
  220.    for the one equal to msg.  If it is found return the ID.  In case when
  221.    the string is not found return -1.  */
  222. static int
  223. msg_to_cat_id (msg)
  224.      const char *msg;
  225. {
  226.   int cnt;
  227.  
  228.   for (cnt = 0; cnt < _msg_tbl_length; ++cnt)
  229.     if (strcmp (msg, _msg_tbl[cnt]._msg) == 0)
  230.       return _msg_tbl[cnt]._msg_number;
  231.  
  232.   return -1;
  233. }
  234.  
  235.  
  236. /* @@ begin of epilog @@ */
  237.  
  238. /* We don't want libintl.a to depend on any other library.  So we
  239.    avoid the non-standard function stpcpy.  In GNU C Library this
  240.    function is available, though.  Also allow the symbol HAVE_STPCPY
  241.    to be defined.  */
  242. #if !_LIBC && !HAVE_STPCPY
  243. static char *
  244. stpcpy (dest, src)
  245.      char *dest;
  246.      const char *src;
  247. {
  248.   while ((*dest++ = *src++) != '\0')
  249.     /* Do nothing. */ ;
  250.   return dest - 1;
  251. }
  252. #endif
  253.