1 /* Compatibility code for gettext-using-catgets interface.
2 Copyright (C) 1995 Free Software Foundation, Inc.
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.
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.
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. */
35 #ifdef HAVE_NL_TYPES_H
36 # include <nl_types.h>
39 #include "libgettext.h"
41 /* @@ end of prolog @@ */
43 /* The catalog descriptor. */
44 static nl_catd catalog = (nl_catd) -1;
46 /* Name of the default catalog. */
47 static const char default_catalog_name[] = "messages";
49 /* Name of currently used catalog. */
50 static const char *catalog_name = default_catalog_name;
52 /* Get ID for given string. If not found return -1. */
53 static int msg_to_cat_id __P ((const char *msg));
55 /* Substitution for systems lacking this function in their C library. */
56 #if !_LIBC && !HAVE_STPCPY
57 static char *stpcpy __P ((char *dest, const char *src));
61 /* Set currently used domain/catalog. */
63 textdomain (domainname)
64 const char *domainname;
71 #if HAVE_SETLOCALE && HAVE_LC_MESSAGES && HAVE_SETLOCALE_NULL
72 lang = setlocale (LC_MESSAGES, NULL);
74 lang = getenv ("LC_ALL");
75 if (lang == NULL || lang[0] == '\0')
77 lang = getenv ("LC_MESSAGES");
78 if (lang == NULL || lang[0] == '\0')
79 lang = getenv ("LANG");
82 if (lang == NULL || lang[0] == '\0')
85 /* See whether name of currently used domain is asked. */
86 if (domainname == NULL)
87 return (char *) catalog_name;
89 if (domainname[0] == '\0')
90 domainname = default_catalog_name;
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
97 new_name = (char *) malloc (new_name_len);
101 strcpy (new_name, PACKAGE);
102 new_catalog = catopen (new_name, 0);
104 if (new_catalog == (nl_catd) -1)
106 /* NLSPATH search didn't work, try absolute path */
107 sprintf (new_name, "%s/%s/LC_MESSAGES/%s.cat", LOCALEDIR, lang,
109 new_catalog = catopen (new_name, 0);
111 if (new_catalog == (nl_catd) -1)
114 return (char *) catalog_name;
118 /* Close old catalog. */
119 if (catalog != (nl_catd) -1)
121 if (catalog_name != default_catalog_name)
122 free ((char *) catalog_name);
124 catalog = new_catalog;
125 catalog_name = new_name;
127 return (char *) catalog_name;
131 bindtextdomain (domainname, dirname)
132 const char *domainname;
135 #if HAVE_SETENV || HAVE_PUTENV
136 char *old_val, *new_val, *cp;
139 /* This does not make much sense here but to be compatible do it. */
140 if (domainname == NULL)
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
146 new_val_len = sizeof ("NLSPATH=") - 1 + strlen (dirname)
147 + sizeof ("/%L/LC_MESSAGES/%N.cat");
149 old_val = getenv ("NLSPATH");
150 if (old_val == NULL || old_val[0] == '\0')
153 new_val_len += 1 + sizeof (LOCALEDIR) - 1
154 + sizeof ("/%L/LC_MESSAGES/%N.cat");
157 new_val_len += strlen (old_val);
159 new_val = (char *) malloc (new_val_len);
166 cp = stpcpy (new_val, "NLSPATH=");
169 cp = stpcpy (cp, dirname);
170 cp = stpcpy (cp, "/%L/LC_MESSAGES/%N.cat:");
175 stpcpy (cp, LOCALEDIR "/%L/LC_MESSAGES/%N.cat");
178 cp = stpcpy (cp, LOCALEDIR);
179 stpcpy (cp, "/%L/LC_MESSAGES/%N.cat");
183 stpcpy (cp, old_val);
186 setenv ("NLSPATH", new_val, 1);
190 /* Do *not* free the environment entry we just entered. It is used
196 return (char *) domainname;
206 if (msg == NULL || catalog == (nl_catd) -1)
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);
216 return catgets (catalog, 1, msgid, (char *) msg);
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. */
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;
236 /* @@ begin of epilog @@ */
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
242 #if !_LIBC && !HAVE_STPCPY
248 while ((*dest++ = *src++) != '\0')