1*f7cc78ecSespie /* Compatibility code for gettext-using-catgets interface.
2*f7cc78ecSespie Copyright (C) 1995, 1997 Free Software Foundation, Inc.
3*f7cc78ecSespie
4*f7cc78ecSespie This program is free software; you can redistribute it and/or modify
5*f7cc78ecSespie it under the terms of the GNU General Public License as published by
6*f7cc78ecSespie the Free Software Foundation; either version 2, or (at your option)
7*f7cc78ecSespie any later version.
8*f7cc78ecSespie
9*f7cc78ecSespie This program is distributed in the hope that it will be useful,
10*f7cc78ecSespie but WITHOUT ANY WARRANTY; without even the implied warranty of
11*f7cc78ecSespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*f7cc78ecSespie GNU General Public License for more details.
13*f7cc78ecSespie
14*f7cc78ecSespie You should have received a copy of the GNU General Public License
15*f7cc78ecSespie along with this program; if not, write to the Free Software Foundation,
16*f7cc78ecSespie Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17*f7cc78ecSespie
18*f7cc78ecSespie #ifdef HAVE_CONFIG_H
19*f7cc78ecSespie # include <config.h>
20*f7cc78ecSespie #endif
21*f7cc78ecSespie
22*f7cc78ecSespie #include <stdio.h>
23*f7cc78ecSespie
24*f7cc78ecSespie #ifdef STDC_HEADERS
25*f7cc78ecSespie # include <stdlib.h>
26*f7cc78ecSespie # include <string.h>
27*f7cc78ecSespie #else
28*f7cc78ecSespie char *getenv ();
29*f7cc78ecSespie # ifdef HAVE_MALLOC_H
30*f7cc78ecSespie # include <malloc.h>
31*f7cc78ecSespie # endif
32*f7cc78ecSespie #endif
33*f7cc78ecSespie
34*f7cc78ecSespie #ifdef HAVE_NL_TYPES_H
35*f7cc78ecSespie # include <nl_types.h>
36*f7cc78ecSespie #endif
37*f7cc78ecSespie
38*f7cc78ecSespie #include "libgettext.h"
39*f7cc78ecSespie
40*f7cc78ecSespie /* @@ end of prolog @@ */
41*f7cc78ecSespie
42*f7cc78ecSespie /* XPG3 defines the result of `setlocale (category, NULL)' as:
43*f7cc78ecSespie ``Directs `setlocale()' to query `category' and return the current
44*f7cc78ecSespie setting of `local'.''
45*f7cc78ecSespie However it does not specify the exact format. And even worse: POSIX
46*f7cc78ecSespie defines this not at all. So we can use this feature only on selected
47*f7cc78ecSespie system (e.g. those using GNU C Library). */
48*f7cc78ecSespie #ifdef _LIBC
49*f7cc78ecSespie # define HAVE_LOCALE_NULL
50*f7cc78ecSespie #endif
51*f7cc78ecSespie
52*f7cc78ecSespie /* The catalog descriptor. */
53*f7cc78ecSespie static nl_catd catalog = (nl_catd) -1;
54*f7cc78ecSespie
55*f7cc78ecSespie /* Name of the default catalog. */
56*f7cc78ecSespie static const char default_catalog_name[] = "messages";
57*f7cc78ecSespie
58*f7cc78ecSespie /* Name of currently used catalog. */
59*f7cc78ecSespie static const char *catalog_name = default_catalog_name;
60*f7cc78ecSespie
61*f7cc78ecSespie /* Get ID for given string. If not found return -1. */
62*f7cc78ecSespie static int msg_to_cat_id PARAMS ((const char *msg));
63*f7cc78ecSespie
64*f7cc78ecSespie /* Substitution for systems lacking this function in their C library. */
65*f7cc78ecSespie #if !_LIBC && !HAVE_STPCPY
66*f7cc78ecSespie static char *stpcpy PARAMS ((char *dest, const char *src));
67*f7cc78ecSespie #endif
68*f7cc78ecSespie
69*f7cc78ecSespie
70*f7cc78ecSespie /* Set currently used domain/catalog. */
71*f7cc78ecSespie char *
textdomain(domainname)72*f7cc78ecSespie textdomain (domainname)
73*f7cc78ecSespie const char *domainname;
74*f7cc78ecSespie {
75*f7cc78ecSespie nl_catd new_catalog;
76*f7cc78ecSespie char *new_name;
77*f7cc78ecSespie size_t new_name_len;
78*f7cc78ecSespie char *lang;
79*f7cc78ecSespie
80*f7cc78ecSespie #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES \
81*f7cc78ecSespie && defined HAVE_LOCALE_NULL
82*f7cc78ecSespie lang = setlocale (LC_MESSAGES, NULL);
83*f7cc78ecSespie #else
84*f7cc78ecSespie lang = getenv ("LC_ALL");
85*f7cc78ecSespie if (lang == NULL || lang[0] == '\0')
86*f7cc78ecSespie {
87*f7cc78ecSespie lang = getenv ("LC_MESSAGES");
88*f7cc78ecSespie if (lang == NULL || lang[0] == '\0')
89*f7cc78ecSespie lang = getenv ("LANG");
90*f7cc78ecSespie }
91*f7cc78ecSespie #endif
92*f7cc78ecSespie if (lang == NULL || lang[0] == '\0')
93*f7cc78ecSespie lang = "C";
94*f7cc78ecSespie
95*f7cc78ecSespie /* See whether name of currently used domain is asked. */
96*f7cc78ecSespie if (domainname == NULL)
97*f7cc78ecSespie return (char *) catalog_name;
98*f7cc78ecSespie
99*f7cc78ecSespie if (domainname[0] == '\0')
100*f7cc78ecSespie domainname = default_catalog_name;
101*f7cc78ecSespie
102*f7cc78ecSespie /* Compute length of added path element. */
103*f7cc78ecSespie new_name_len = sizeof (LOCALEDIR) - 1 + 1 + strlen (lang)
104*f7cc78ecSespie + sizeof ("/LC_MESSAGES/") - 1 + sizeof (PACKAGE) - 1
105*f7cc78ecSespie + sizeof (".cat");
106*f7cc78ecSespie
107*f7cc78ecSespie new_name = (char *) malloc (new_name_len);
108*f7cc78ecSespie if (new_name == NULL)
109*f7cc78ecSespie return NULL;
110*f7cc78ecSespie
111*f7cc78ecSespie strcpy (new_name, PACKAGE);
112*f7cc78ecSespie new_catalog = catopen (new_name, 0);
113*f7cc78ecSespie
114*f7cc78ecSespie if (new_catalog == (nl_catd) -1)
115*f7cc78ecSespie {
116*f7cc78ecSespie /* NLSPATH search didn't work, try absolute path */
117*f7cc78ecSespie sprintf (new_name, "%s/%s/LC_MESSAGES/%s.cat", LOCALEDIR, lang,
118*f7cc78ecSespie PACKAGE);
119*f7cc78ecSespie new_catalog = catopen (new_name, 0);
120*f7cc78ecSespie
121*f7cc78ecSespie if (new_catalog == (nl_catd) -1)
122*f7cc78ecSespie {
123*f7cc78ecSespie free (new_name);
124*f7cc78ecSespie return (char *) catalog_name;
125*f7cc78ecSespie }
126*f7cc78ecSespie }
127*f7cc78ecSespie
128*f7cc78ecSespie /* Close old catalog. */
129*f7cc78ecSespie if (catalog != (nl_catd) -1)
130*f7cc78ecSespie catclose (catalog);
131*f7cc78ecSespie if (catalog_name != default_catalog_name)
132*f7cc78ecSespie free ((char *) catalog_name);
133*f7cc78ecSespie
134*f7cc78ecSespie catalog = new_catalog;
135*f7cc78ecSespie catalog_name = new_name;
136*f7cc78ecSespie
137*f7cc78ecSespie return (char *) catalog_name;
138*f7cc78ecSespie }
139*f7cc78ecSespie
140*f7cc78ecSespie char *
bindtextdomain(domainname,dirname)141*f7cc78ecSespie bindtextdomain (domainname, dirname)
142*f7cc78ecSespie const char *domainname;
143*f7cc78ecSespie const char *dirname;
144*f7cc78ecSespie {
145*f7cc78ecSespie #if HAVE_SETENV || HAVE_PUTENV
146*f7cc78ecSespie char *old_val, *new_val, *cp;
147*f7cc78ecSespie size_t new_val_len;
148*f7cc78ecSespie
149*f7cc78ecSespie /* This does not make much sense here but to be compatible do it. */
150*f7cc78ecSespie if (domainname == NULL)
151*f7cc78ecSespie return NULL;
152*f7cc78ecSespie
153*f7cc78ecSespie /* Compute length of added path element. If we use setenv we don't need
154*f7cc78ecSespie the first byts for NLSPATH=, but why complicate the code for this
155*f7cc78ecSespie peanuts. */
156*f7cc78ecSespie new_val_len = sizeof ("NLSPATH=") - 1 + strlen (dirname)
157*f7cc78ecSespie + sizeof ("/%L/LC_MESSAGES/%N.cat");
158*f7cc78ecSespie
159*f7cc78ecSespie old_val = getenv ("NLSPATH");
160*f7cc78ecSespie if (old_val == NULL || old_val[0] == '\0')
161*f7cc78ecSespie {
162*f7cc78ecSespie old_val = NULL;
163*f7cc78ecSespie new_val_len += 1 + sizeof (LOCALEDIR) - 1
164*f7cc78ecSespie + sizeof ("/%L/LC_MESSAGES/%N.cat");
165*f7cc78ecSespie }
166*f7cc78ecSespie else
167*f7cc78ecSespie new_val_len += strlen (old_val);
168*f7cc78ecSespie
169*f7cc78ecSespie new_val = (char *) malloc (new_val_len);
170*f7cc78ecSespie if (new_val == NULL)
171*f7cc78ecSespie return NULL;
172*f7cc78ecSespie
173*f7cc78ecSespie # if HAVE_SETENV
174*f7cc78ecSespie cp = new_val;
175*f7cc78ecSespie # else
176*f7cc78ecSespie cp = stpcpy (new_val, "NLSPATH=");
177*f7cc78ecSespie # endif
178*f7cc78ecSespie
179*f7cc78ecSespie cp = stpcpy (cp, dirname);
180*f7cc78ecSespie cp = stpcpy (cp, "/%L/LC_MESSAGES/%N.cat:");
181*f7cc78ecSespie
182*f7cc78ecSespie if (old_val == NULL)
183*f7cc78ecSespie {
184*f7cc78ecSespie # if __STDC__
185*f7cc78ecSespie stpcpy (cp, LOCALEDIR "/%L/LC_MESSAGES/%N.cat");
186*f7cc78ecSespie # else
187*f7cc78ecSespie
188*f7cc78ecSespie cp = stpcpy (cp, LOCALEDIR);
189*f7cc78ecSespie stpcpy (cp, "/%L/LC_MESSAGES/%N.cat");
190*f7cc78ecSespie # endif
191*f7cc78ecSespie }
192*f7cc78ecSespie else
193*f7cc78ecSespie stpcpy (cp, old_val);
194*f7cc78ecSespie
195*f7cc78ecSespie # if HAVE_SETENV
196*f7cc78ecSespie setenv ("NLSPATH", new_val, 1);
197*f7cc78ecSespie free (new_val);
198*f7cc78ecSespie # else
199*f7cc78ecSespie putenv (new_val);
200*f7cc78ecSespie /* Do *not* free the environment entry we just entered. It is used
201*f7cc78ecSespie from now on. */
202*f7cc78ecSespie # endif
203*f7cc78ecSespie
204*f7cc78ecSespie #endif
205*f7cc78ecSespie
206*f7cc78ecSespie return (char *) domainname;
207*f7cc78ecSespie }
208*f7cc78ecSespie
209*f7cc78ecSespie #undef gettext
210*f7cc78ecSespie char *
gettext(msg)211*f7cc78ecSespie gettext (msg)
212*f7cc78ecSespie const char *msg;
213*f7cc78ecSespie {
214*f7cc78ecSespie int msgid;
215*f7cc78ecSespie
216*f7cc78ecSespie if (msg == NULL || catalog == (nl_catd) -1)
217*f7cc78ecSespie return (char *) msg;
218*f7cc78ecSespie
219*f7cc78ecSespie /* Get the message from the catalog. We always use set number 1.
220*f7cc78ecSespie The message ID is computed by the function `msg_to_cat_id'
221*f7cc78ecSespie which works on the table generated by `po-to-tbl'. */
222*f7cc78ecSespie msgid = msg_to_cat_id (msg);
223*f7cc78ecSespie if (msgid == -1)
224*f7cc78ecSespie return (char *) msg;
225*f7cc78ecSespie
226*f7cc78ecSespie return catgets (catalog, 1, msgid, (char *) msg);
227*f7cc78ecSespie }
228*f7cc78ecSespie
229*f7cc78ecSespie /* Look through the table `_msg_tbl' which has `_msg_tbl_length' entries
230*f7cc78ecSespie for the one equal to msg. If it is found return the ID. In case when
231*f7cc78ecSespie the string is not found return -1. */
232*f7cc78ecSespie static int
msg_to_cat_id(msg)233*f7cc78ecSespie msg_to_cat_id (msg)
234*f7cc78ecSespie const char *msg;
235*f7cc78ecSespie {
236*f7cc78ecSespie int cnt;
237*f7cc78ecSespie
238*f7cc78ecSespie for (cnt = 0; cnt < _msg_tbl_length; ++cnt)
239*f7cc78ecSespie if (strcmp (msg, _msg_tbl[cnt]._msg) == 0)
240*f7cc78ecSespie return _msg_tbl[cnt]._msg_number;
241*f7cc78ecSespie
242*f7cc78ecSespie return -1;
243*f7cc78ecSespie }
244*f7cc78ecSespie
245*f7cc78ecSespie
246*f7cc78ecSespie /* @@ begin of epilog @@ */
247*f7cc78ecSespie
248*f7cc78ecSespie /* We don't want libintl.a to depend on any other library. So we
249*f7cc78ecSespie avoid the non-standard function stpcpy. In GNU C Library this
250*f7cc78ecSespie function is available, though. Also allow the symbol HAVE_STPCPY
251*f7cc78ecSespie to be defined. */
252*f7cc78ecSespie #if !_LIBC && !HAVE_STPCPY
253*f7cc78ecSespie static char *
stpcpy(dest,src)254*f7cc78ecSespie stpcpy (dest, src)
255*f7cc78ecSespie char *dest;
256*f7cc78ecSespie const char *src;
257*f7cc78ecSespie {
258*f7cc78ecSespie while ((*dest++ = *src++) != '\0')
259*f7cc78ecSespie /* Do nothing. */ ;
260*f7cc78ecSespie return dest - 1;
261*f7cc78ecSespie }
262*f7cc78ecSespie #endif
263