1 /* dcgettext.c -- implementation of the dcgettext(3) function
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <sys/types.h>
27 # define alloca __builtin_alloca
29 # if defined HAVE_ALLOCA_H || defined _LIBC
47 #if defined STDC_HEADERS || defined _LIBC
58 #if defined HAVE_STRING_H || defined _LIBC
63 #if !HAVE_STRCHR && !defined _LIBC
69 #if defined HAVE_UNISTD_H || defined _LIBC
78 # include "libgettext.h"
80 #include "hash-string.h"
82 /* @@ end of prolog @@ */
85 /* Rename the non ANSI C functions. This is required by the standard
86 because some ANSI C functions will require linking with this object
87 file and the name space must not be polluted. */
88 # define getcwd __getcwd
89 # define stpcpy __stpcpy
91 # if !defined HAVE_GETCWD
93 # define getcwd(buf, max) getwd (buf)
99 /* Amount to increase buffer size by in each try. */
102 /* The following is from pathmax.h. */
103 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
104 PATH_MAX but might cause redefinition warnings when sys/param.h is
105 later included (as on MORE/BSD 4.3). */
106 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
110 #ifndef _POSIX_PATH_MAX
111 # define _POSIX_PATH_MAX 255
114 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
115 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
118 /* Don't include sys/param.h if it already has been. */
119 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
120 # include <sys/param.h>
123 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
124 # define PATH_MAX MAXPATHLEN
128 # define PATH_MAX _POSIX_PATH_MAX
131 /* XPG3 defines the result of `setlocale (category, NULL)' as:
132 ``Directs `setlocale()' to query `category' and return the current
133 setting of `local'.''
134 However it does not specify the exact format. And even worse: POSIX
135 defines this not at all. So we can use this feature only on selected
136 system (e.g. those using GNU C Library). */
138 # define HAVE_LOCALE_NULL
141 /* Name of the default domain used for gettext(3) prior any call to
142 textdomain(3). The default value for this is "messages". */
143 const char _nl_default_default_domain
[] = "messages";
145 /* Value used as the default domain for gettext(3). */
146 const char *_nl_current_default_domain
= _nl_default_default_domain
;
148 /* Contains the default location of the message catalogs. */
149 const char _nl_default_dirname
[] = GNULOCALEDIR
;
151 /* List with bindings of specific domains created by bindtextdomain()
153 struct binding
*_nl_domain_bindings
;
155 /* Prototypes for local functions. */
156 static char *find_msg
PARAMS ((struct loaded_l10nfile
*domain_file
,
158 static const char *category_to_name
PARAMS ((int category
));
159 static const char *guess_category_value
PARAMS ((int category
,
160 const char *categoryname
));
163 /* Names for the libintl functions are a problem. They must not clash
164 with existing names and they should follow ANSI C. But this source
165 code is also used in GNU C Library where the names have a __
166 prefix. So we have to make a difference here. */
168 # define DCGETTEXT __dcgettext
170 # define DCGETTEXT dcgettext__
173 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
176 DCGETTEXT (domainname
, msgid
, category
)
177 const char *domainname
;
181 struct loaded_l10nfile
*domain
;
182 struct binding
*binding
;
183 const char *categoryname
;
184 const char *categoryvalue
;
185 char *dirname
, *xdomainname
;
188 int saved_errno
= errno
;
190 /* If no real MSGID is given return NULL. */
194 /* If DOMAINNAME is NULL, we are interested in the default domain. If
195 CATEGORY is not LC_MESSAGES this might not make much sense but the
196 defintion left this undefined. */
197 if (domainname
== NULL
)
198 domainname
= _nl_current_default_domain
;
200 /* First find matching binding. */
201 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
203 int compare
= strcmp (domainname
, binding
->domainname
);
209 /* It is not in the list. */
216 dirname
= (char *) _nl_default_dirname
;
217 else if (binding
->dirname
[0] == '/')
218 dirname
= binding
->dirname
;
221 /* We have a relative path. Make it absolute now. */
222 size_t dirname_len
= strlen (binding
->dirname
) + 1;
226 path_max
= (unsigned) PATH_MAX
;
227 path_max
+= 2; /* The getcwd docs say to do this. */
229 dirname
= (char *) alloca (path_max
+ dirname_len
);
232 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
234 path_max
+= PATH_INCR
;
235 dirname
= (char *) alloca (path_max
+ dirname_len
);
241 /* We cannot get the current working directory. Don't signal an
242 error but simply return the default string. */
244 return (char *) msgid
;
247 /* We don't want libintl.a to depend on any other library. So
248 we avoid the non-standard function stpcpy. In GNU C Library
249 this function is available, though. Also allow the symbol
250 HAVE_STPCPY to be defined. */
251 #if defined _LIBC || defined HAVE_STPCPY
252 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
254 strcat (dirname
, "/");
255 strcat (dirname
, binding
->dirname
);
259 /* Now determine the symbolic name of CATEGORY and its value. */
260 categoryname
= category_to_name (category
);
261 categoryvalue
= guess_category_value (category
, categoryname
);
263 xdomainname
= (char *) alloca (strlen (categoryname
)
264 + strlen (domainname
) + 5);
265 /* We don't want libintl.a to depend on any other library. So we
266 avoid the non-standard function stpcpy. In GNU C Library this
267 function is available, though. Also allow the symbol HAVE_STPCPY
269 #if defined _LIBC || defined HAVE_STPCPY
270 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
274 strcpy (xdomainname
, categoryname
);
275 strcat (xdomainname
, "/");
276 strcat (xdomainname
, domainname
);
277 strcat (xdomainname
, ".mo");
280 /* Creating working area. */
281 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
284 /* Search for the given string. This is a loop because we perhaps
285 got an ordered list of languages to consider for th translation. */
288 /* Make CATEGORYVALUE point to the next element of the list. */
289 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
291 if (categoryvalue
[0] == '\0')
293 /* The whole contents of CATEGORYVALUE has been searched but
294 no valid entry has been found. We solve this situation
295 by implicitely appending a "C" entry, i.e. no translation
297 single_locale
[0] = 'C';
298 single_locale
[1] = '\0';
302 char *cp
= single_locale
;
303 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
304 *cp
++ = *categoryvalue
++;
308 /* If the current locale value is C (or POSIX) we don't load a
309 domain. Return the MSGID. */
310 if (strcmp (single_locale
, "C") == 0
311 || strcmp (single_locale
, "POSIX") == 0)
314 return (char *) msgid
;
318 /* Find structure describing the message catalog matching the
319 DOMAINNAME and CATEGORY. */
320 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
);
324 retval
= find_msg (domain
, msgid
);
330 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
332 retval
= find_msg (domain
->successor
[cnt
], msgid
);
350 /* Alias for function name in GNU C Library. */
351 weak_alias (__dcgettext
, dcgettext
);
356 find_msg (domain_file
, msgid
)
357 struct loaded_l10nfile
*domain_file
;
360 size_t top
, act
, bottom
;
361 struct loaded_domain
*domain
;
363 if (domain_file
->decided
== 0)
364 _nl_load_domain (domain_file
);
366 if (domain_file
->data
== NULL
)
369 domain
= (struct loaded_domain
*) domain_file
->data
;
371 /* Locate the MSGID and its translation. */
372 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
374 /* Use the hashing table. */
375 nls_uint32 len
= strlen (msgid
);
376 nls_uint32 hash_val
= hash_string (msgid
);
377 nls_uint32 idx
= hash_val
% domain
->hash_size
;
378 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
379 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
382 /* Hash table entry is empty. */
385 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
387 domain
->data
+ W (domain
->must_swap
,
388 domain
->orig_tab
[nstr
- 1].offset
)) == 0)
389 return (char *) domain
->data
+ W (domain
->must_swap
,
390 domain
->trans_tab
[nstr
- 1].offset
);
394 if (idx
>= domain
->hash_size
- incr
)
395 idx
-= domain
->hash_size
- incr
;
399 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
401 /* Hash table entry is empty. */
404 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
406 domain
->data
+ W (domain
->must_swap
,
407 domain
->orig_tab
[nstr
- 1].offset
))
409 return (char *) domain
->data
410 + W (domain
->must_swap
, domain
->trans_tab
[nstr
- 1].offset
);
415 /* Now we try the default method: binary search in the sorted
416 array of messages. */
418 top
= domain
->nstrings
;
423 act
= (bottom
+ top
) / 2;
424 cmp_val
= strcmp (msgid
, domain
->data
425 + W (domain
->must_swap
,
426 domain
->orig_tab
[act
].offset
));
429 else if (cmp_val
> 0)
435 /* If an translation is found return this. */
436 return bottom
>= top
? NULL
: (char *) domain
->data
437 + W (domain
->must_swap
,
438 domain
->trans_tab
[act
].offset
);
442 /* Return string representation of locale CATEGORY. */
444 category_to_name (category
)
453 retval
= "LC_COLLATE";
463 retval
= "LC_MONETARY";
468 retval
= "LC_NUMERIC";
478 retval
= "LC_MESSAGES";
483 retval
= "LC_RESPONSE";
488 /* This might not make sense but is perhaps better than any other
494 /* If you have a better idea for a default value let me know. */
501 /* Guess value of current locale from value of the environment variables. */
502 static const char *guess_category_value (category
, categoryname
)
504 const char *categoryname
;
508 /* The highest priority value is the `LANGUAGE' environment
509 variable. This is a GNU extension. */
510 retval
= getenv ("LANGUAGE");
511 if (retval
!= NULL
&& retval
[0] != '\0')
514 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
515 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
516 systems this can be done by the `setlocale' function itself. */
517 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
518 return setlocale (category
, NULL
);
520 /* Setting of LC_ALL overwrites all other. */
521 retval
= getenv ("LC_ALL");
522 if (retval
!= NULL
&& retval
[0] != '\0')
525 /* Next comes the name of the desired category. */
526 retval
= getenv (categoryname
);
527 if (retval
!= NULL
&& retval
[0] != '\0')
530 /* Last possibility is the LANG environment variable. */
531 retval
= getenv ("LANG");
532 if (retval
!= NULL
&& retval
[0] != '\0')
535 /* We use C as the default domain. POSIX says this is implementation