1 /* Compatibility code for gettext-using-catgets interface.
2 Copyright (C) 1995, 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
34 #ifdef HAVE_NL_TYPES_H
35 # include <nl_types.h>
38 #include "libgettext.h"
40 /* @@ end of prolog @@ */
42 /* XPG3 defines the result of `setlocale (category, NULL)' as:
43 ``Directs `setlocale()' to query `category' and return the current
45 However it does not specify the exact format. And even worse: POSIX
46 defines this not at all. So we can use this feature only on selected
47 system (e.g. those using GNU C Library). */
49 # define HAVE_LOCALE_NULL
52 /* The catalog descriptor. */
53 static nl_catd catalog
= (nl_catd
) -1;
55 /* Name of the default catalog. */
56 static const char default_catalog_name
[] = "messages";
58 /* Name of currently used catalog. */
59 static const char *catalog_name
= default_catalog_name
;
61 /* Get ID for given string. If not found return -1. */
62 static int msg_to_cat_id
PARAMS ((const char *msg
));
64 /* Substitution for systems lacking this function in their C library. */
65 #if !_LIBC && !HAVE_STPCPY
66 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
70 /* Set currently used domain/catalog. */
72 textdomain (domainname
)
73 const char *domainname
;
80 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES \
81 && defined HAVE_LOCALE_NULL
82 lang
= setlocale (LC_MESSAGES
, NULL
);
84 lang
= getenv ("LC_ALL");
85 if (lang
== NULL
|| lang
[0] == '\0')
87 lang
= getenv ("LC_MESSAGES");
88 if (lang
== NULL
|| lang
[0] == '\0')
89 lang
= getenv ("LANG");
92 if (lang
== NULL
|| lang
[0] == '\0')
95 /* See whether name of currently used domain is asked. */
96 if (domainname
== NULL
)
97 return (char *) catalog_name
;
99 if (domainname
[0] == '\0')
100 domainname
= default_catalog_name
;
102 /* Compute length of added path element. */
103 new_name_len
= sizeof (LOCALEDIR
) - 1 + 1 + strlen (lang
)
104 + sizeof ("/LC_MESSAGES/") - 1 + sizeof (PACKAGE
) - 1
107 new_name
= (char *) malloc (new_name_len
);
108 if (new_name
== NULL
)
111 strcpy (new_name
, PACKAGE
);
112 new_catalog
= catopen (new_name
, 0);
114 if (new_catalog
== (nl_catd
) -1)
116 /* NLSPATH search didn't work, try absolute path */
117 sprintf (new_name
, "%s/%s/LC_MESSAGES/%s.cat", LOCALEDIR
, lang
,
119 new_catalog
= catopen (new_name
, 0);
121 if (new_catalog
== (nl_catd
) -1)
124 return (char *) catalog_name
;
128 /* Close old catalog. */
129 if (catalog
!= (nl_catd
) -1)
131 if (catalog_name
!= default_catalog_name
)
132 free ((char *) catalog_name
);
134 catalog
= new_catalog
;
135 catalog_name
= new_name
;
137 return (char *) catalog_name
;
141 bindtextdomain (domainname
, dirname
)
142 const char *domainname
;
145 #if HAVE_SETENV || HAVE_PUTENV
146 char *old_val
, *new_val
, *cp
;
149 /* This does not make much sense here but to be compatible do it. */
150 if (domainname
== NULL
)
153 /* Compute length of added path element. If we use setenv we don't need
154 the first byts for NLSPATH=, but why complicate the code for this
156 new_val_len
= sizeof ("NLSPATH=") - 1 + strlen (dirname
)
157 + sizeof ("/%L/LC_MESSAGES/%N.cat");
159 old_val
= getenv ("NLSPATH");
160 if (old_val
== NULL
|| old_val
[0] == '\0')
163 new_val_len
+= 1 + sizeof (LOCALEDIR
) - 1
164 + sizeof ("/%L/LC_MESSAGES/%N.cat");
167 new_val_len
+= strlen (old_val
);
169 new_val
= (char *) malloc (new_val_len
);
176 cp
= stpcpy (new_val
, "NLSPATH=");
179 cp
= stpcpy (cp
, dirname
);
180 cp
= stpcpy (cp
, "/%L/LC_MESSAGES/%N.cat:");
185 stpcpy (cp
, LOCALEDIR
"/%L/LC_MESSAGES/%N.cat");
188 cp
= stpcpy (cp
, LOCALEDIR
);
189 stpcpy (cp
, "/%L/LC_MESSAGES/%N.cat");
193 stpcpy (cp
, old_val
);
196 setenv ("NLSPATH", new_val
, 1);
200 /* Do *not* free the environment entry we just entered. It is used
206 return (char *) domainname
;
216 if (msg
== NULL
|| catalog
== (nl_catd
) -1)
219 /* Get the message from the catalog. We always use set number 1.
220 The message ID is computed by the function `msg_to_cat_id'
221 which works on the table generated by `po-to-tbl'. */
222 msgid
= msg_to_cat_id (msg
);
226 return catgets (catalog
, 1, msgid
, (char *) msg
);
229 /* Look through the table `_msg_tbl' which has `_msg_tbl_length' entries
230 for the one equal to msg. If it is found return the ID. In case when
231 the string is not found return -1. */
238 for (cnt
= 0; cnt
< _msg_tbl_length
; ++cnt
)
239 if (strcmp (msg
, _msg_tbl
[cnt
]._msg
) == 0)
240 return _msg_tbl
[cnt
]._msg_number
;
246 /* @@ begin of epilog @@ */
248 /* We don't want libintl.a to depend on any other library. So we
249 avoid the non-standard function stpcpy. In GNU C Library this
250 function is available, though. Also allow the symbol HAVE_STPCPY
252 #if !_LIBC && !HAVE_STPCPY
258 while ((*dest
++ = *src
++) != '\0')