Back to index

plt-scheme  4.2.1
cat-compat.c
Go to the documentation of this file.
00001 /* Compatibility code for gettext-using-catgets interface.
00002    Copyright (C) 1995 Free Software Foundation, Inc.
00003 
00004 The GNU C Library is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU Library General Public License as
00006 published by the Free Software Foundation; either version 2 of the
00007 License, or (at your option) any later version.
00008 
00009 The GNU C Library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 Library General Public License for more details.
00013 
00014 You should have received a copy of the GNU Library General Public
00015 License along with the GNU C Library; see the file COPYING.LIB.  If
00016 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017 Boston, MA 02111-1307, USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022 
00023 #include <stdio.h>
00024 
00025 #ifdef STDC_HEADERS
00026 # include <stdlib.h>
00027 # include <string.h>
00028 #else
00029 char *getenv ();
00030 # ifdef HAVE_MALLOC_H
00031 #  include <malloc.h>
00032 # endif
00033 #endif
00034 
00035 #ifdef HAVE_NL_TYPES_H
00036 # include <nl_types.h>
00037 #endif
00038 
00039 #include "libgettext.h"
00040 
00041 /* @@ end of prolog @@ */
00042 
00043 /* The catalog descriptor.  */
00044 static nl_catd catalog = (nl_catd) -1;
00045 
00046 /* Name of the default catalog.  */
00047 static const char default_catalog_name[] = "messages";
00048 
00049 /* Name of currently used catalog.  */
00050 static const char *catalog_name = default_catalog_name;
00051 
00052 /* Get ID for given string.  If not found return -1.  */
00053 static int msg_to_cat_id PARAMS ((const char *msg));
00054 
00055 /* Substitution for systems lacking this function in their C library.  */
00056 #if !_LIBC && !HAVE_STPCPY
00057 static char *stpcpy PARAMS ((char *dest, const char *src));
00058 #endif
00059 
00060 
00061 /* Set currently used domain/catalog.  */
00062 char *
00063 textdomain (domainname)
00064      const char *domainname;
00065 {
00066   nl_catd new_catalog;
00067   char *new_name;
00068   size_t new_name_len;
00069   char *lang;
00070 
00071 #if HAVE_SETLOCALE && HAVE_LC_MESSAGES && HAVE_SETLOCALE_NULL
00072   lang = setlocale (LC_MESSAGES, NULL);
00073 #else
00074   lang = getenv ("LC_ALL");
00075   if (lang == NULL || lang[0] == '\0')
00076     {
00077       lang = getenv ("LC_MESSAGES");
00078       if (lang == NULL || lang[0] == '\0')
00079        lang = getenv ("LANG");
00080     }
00081 #endif
00082   if (lang == NULL || lang[0] == '\0')
00083     lang = "C";
00084 
00085   /* See whether name of currently used domain is asked.  */
00086   if (domainname == NULL)
00087     return (char *) catalog_name;
00088 
00089   if (domainname[0] == '\0')
00090     domainname = default_catalog_name;
00091 
00092   /* Compute length of added path element.  */
00093   new_name_len = sizeof (LOCALEDIR) - 1 + 1 + strlen (lang)
00094                + sizeof ("/LC_MESSAGES/") - 1 + sizeof (PACKAGE) - 1
00095                + sizeof (".cat");
00096 
00097   new_name = (char *) malloc (new_name_len);
00098   if (new_name == NULL)
00099     return NULL;
00100 
00101   strcpy (new_name, PACKAGE);
00102   new_catalog = catopen (new_name, 0);
00103 
00104   if (new_catalog == (nl_catd) -1)
00105     {
00106       /* NLSPATH search didn't work, try absolute path */
00107       sprintf (new_name, "%s/%s/LC_MESSAGES/%s.cat", LOCALEDIR, lang,
00108               PACKAGE);
00109       new_catalog = catopen (new_name, 0);
00110 
00111       if (new_catalog == (nl_catd) -1)
00112        {
00113          free (new_name);
00114          return (char *) catalog_name;
00115        }
00116     }
00117 
00118   /* Close old catalog.  */
00119   if (catalog != (nl_catd) -1)
00120     catclose (catalog);
00121   if (catalog_name != default_catalog_name)
00122     free ((char *) catalog_name);
00123 
00124   catalog = new_catalog;
00125   catalog_name = new_name;
00126 
00127   return (char *) catalog_name;
00128 }
00129 
00130 char *
00131 bindtextdomain (domainname, dirname)
00132      const char *domainname;
00133      const char *dirname;
00134 {
00135 #if HAVE_SETENV || HAVE_PUTENV
00136   char *old_val, *new_val, *cp;
00137   size_t new_val_len;
00138 
00139   /* This does not make much sense here but to be compatible do it.  */
00140   if (domainname == NULL)
00141     return NULL;
00142 
00143   /* Compute length of added path element.  If we use setenv we don't need
00144      the first byts for NLSPATH=, but why complicate the code for this
00145      peanuts.  */
00146   new_val_len = sizeof ("NLSPATH=") - 1 + strlen (dirname)
00147               + sizeof ("/%L/LC_MESSAGES/%N.cat");
00148 
00149   old_val = getenv ("NLSPATH");
00150   if (old_val == NULL || old_val[0] == '\0')
00151     {
00152       old_val = NULL;
00153       new_val_len += 1 + sizeof (LOCALEDIR) - 1
00154                     + sizeof ("/%L/LC_MESSAGES/%N.cat");
00155     }
00156   else
00157     new_val_len += strlen (old_val);
00158 
00159   new_val = (char *) malloc (new_val_len);
00160   if (new_val == NULL)
00161     return NULL;
00162 
00163 # if HAVE_SETENV
00164   cp = new_val;
00165 # else
00166   cp = stpcpy (new_val, "NLSPATH=");
00167 # endif
00168 
00169   cp = stpcpy (cp, dirname);
00170   cp = stpcpy (cp, "/%L/LC_MESSAGES/%N.cat:");
00171 
00172   if (old_val == NULL)
00173     {
00174 # if __STDC__
00175       stpcpy (cp, LOCALEDIR "/%L/LC_MESSAGES/%N.cat");
00176 # else
00177 
00178       cp = stpcpy (cp, LOCALEDIR);
00179       stpcpy (cp, "/%L/LC_MESSAGES/%N.cat");
00180 # endif
00181     }
00182   else
00183     stpcpy (cp, old_val);
00184 
00185 # if HAVE_SETENV
00186   setenv ("NLSPATH", new_val, 1);
00187   free (new_val);
00188 # else
00189   putenv (new_val);
00190   /* Do *not* free the environment entry we just entered.  It is used
00191      from now on.   */
00192 # endif
00193 
00194 #endif
00195 
00196   return (char *) domainname;
00197 }
00198 
00199 #undef gettext
00200 char *
00201 gettext (msg)
00202      const char *msg;
00203 {
00204   int msgid;
00205 
00206   if (msg == NULL || catalog == (nl_catd) -1)
00207     return (char *) msg;
00208 
00209   /* Get the message from the catalog.  We always use set number 1.
00210      The message ID is computed by the function `msg_to_cat_id'
00211      which works on the table generated by `po-to-tbl'.  */
00212   msgid = msg_to_cat_id (msg);
00213   if (msgid == -1)
00214     return (char *) msg;
00215 
00216   return catgets (catalog, 1, msgid, (char *) msg);
00217 }
00218 
00219 /* Look through the table `_msg_tbl' which has `_msg_tbl_length' entries
00220    for the one equal to msg.  If it is found return the ID.  In case when
00221    the string is not found return -1.  */
00222 static int
00223 msg_to_cat_id (msg)
00224      const char *msg;
00225 {
00226   int cnt;
00227 
00228   for (cnt = 0; cnt < _msg_tbl_length; ++cnt)
00229     if (strcmp (msg, _msg_tbl[cnt]._msg) == 0)
00230       return _msg_tbl[cnt]._msg_number;
00231 
00232   return -1;
00233 }
00234 
00235 
00236 /* @@ begin of epilog @@ */
00237 
00238 /* We don't want libintl.a to depend on any other library.  So we
00239    avoid the non-standard function stpcpy.  In GNU C Library this
00240    function is available, though.  Also allow the symbol HAVE_STPCPY
00241    to be defined.  */
00242 #if !_LIBC && !HAVE_STPCPY
00243 static char *
00244 stpcpy (dest, src)
00245      char *dest;
00246      const char *src;
00247 {
00248   while ((*dest++ = *src++) != '\0')
00249     /* Do nothing. */ ;
00250   return dest - 1;
00251 }
00252 #endif