1 /* dcgettext.c -- implementation of the dcgettext(3) function
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however. The master source lives in /gd/gnu/lib.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
26 #include <sys/types.h>
29 # define alloca __builtin_alloca
30 # define HAVE_ALLOCA 1
32 # if defined HAVE_ALLOCA_H || defined _LIBC
50 #if defined STDC_HEADERS || defined _LIBC
61 #if defined HAVE_STRING_H || defined _LIBC
63 # define _GNU_SOURCE 1
69 #if !HAVE_STRCHR && !defined _LIBC
75 #if defined HAVE_UNISTD_H || defined _LIBC
84 # include "libgettext.h"
86 #include "hash-string.h"
88 /* @@ end of prolog @@ */
91 /* Rename the non ANSI C functions. This is required by the standard
92 because some ANSI C functions will require linking with this object
93 file and the name space must not be polluted. */
94 # define getcwd __getcwd
95 # define stpcpy __stpcpy
97 # if !defined HAVE_GETCWD
99 # define getcwd(buf, max) getwd (buf)
104 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
108 /* Amount to increase buffer size by in each try. */
111 /* The following is from pathmax.h. */
112 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
113 PATH_MAX but might cause redefinition warnings when sys/param.h is
114 later included (as on MORE/BSD 4.3). */
115 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
119 #ifndef _POSIX_PATH_MAX
120 # define _POSIX_PATH_MAX 255
123 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
124 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
127 /* Don't include sys/param.h if it already has been. */
128 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
129 # include <sys/param.h>
132 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
133 # define PATH_MAX MAXPATHLEN
137 # define PATH_MAX _POSIX_PATH_MAX
140 /* XPG3 defines the result of `setlocale (category, NULL)' as:
141 ``Directs `setlocale()' to query `category' and return the current
142 setting of `local'.''
143 However it does not specify the exact format. And even worse: POSIX
144 defines this not at all. So we can use this feature only on selected
145 system (e.g. those using GNU C Library). */
147 # define HAVE_LOCALE_NULL
150 /* Name of the default domain used for gettext(3) prior any call to
151 textdomain(3). The default value for this is "messages". */
152 const char _nl_default_default_domain
[] = "messages";
154 /* Value used as the default domain for gettext(3). */
155 const char *_nl_current_default_domain
= _nl_default_default_domain
;
157 /* Contains the default location of the message catalogs. */
158 const char _nl_default_dirname
[] = GNULOCALEDIR
;
160 /* List with bindings of specific domains created by bindtextdomain()
162 struct binding
*_nl_domain_bindings
;
164 /* Prototypes for local functions. */
165 static char *find_msg
PARAMS ((struct loaded_l10nfile
*domain_file
,
167 static const char *category_to_name
PARAMS ((int category
));
168 static const char *guess_category_value
PARAMS ((int category
,
169 const char *categoryname
));
172 /* For those loosing systems which don't have `alloca' we have to add
173 some additional code emulating it. */
175 /* Nothing has to be done. */
176 # define ADD_BLOCK(list, address) /* nothing */
177 # define FREE_BLOCKS(list) /* nothing */
182 struct block_list
*next
;
184 # define ADD_BLOCK(list, addr) \
186 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
187 /* If we cannot get a free block we cannot add the new element to \
189 if (newp != NULL) { \
190 newp->address = (addr); \
191 newp->next = (list); \
195 # define FREE_BLOCKS(list) \
197 while (list != NULL) { \
198 struct block_list *old = list; \
204 # define alloca(size) (malloc (size))
205 #endif /* have alloca */
208 /* Names for the libintl functions are a problem. They must not clash
209 with existing names and they should follow ANSI C. But this source
210 code is also used in GNU C Library where the names have a __
211 prefix. So we have to make a difference here. */
213 # define DCGETTEXT __dcgettext
215 # define DCGETTEXT dcgettext__
218 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
221 DCGETTEXT (domainname
, msgid
, category
)
222 const char *domainname
;
227 struct block_list
*block_list
= NULL
;
229 struct loaded_l10nfile
*domain
;
230 struct binding
*binding
;
231 const char *categoryname
;
232 const char *categoryvalue
;
233 char *dirname
, *xdomainname
;
236 int saved_errno
= errno
;
238 /* If no real MSGID is given return NULL. */
242 /* If DOMAINNAME is NULL, we are interested in the default domain. If
243 CATEGORY is not LC_MESSAGES this might not make much sense but the
244 defintion left this undefined. */
245 if (domainname
== NULL
)
246 domainname
= _nl_current_default_domain
;
248 /* First find matching binding. */
249 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
251 int compare
= strcmp (domainname
, binding
->domainname
);
257 /* It is not in the list. */
264 dirname
= (char *) _nl_default_dirname
;
265 else if (binding
->dirname
[0] == '/')
266 dirname
= binding
->dirname
;
269 /* We have a relative path. Make it absolute now. */
270 size_t dirname_len
= strlen (binding
->dirname
) + 1;
274 path_max
= (unsigned) PATH_MAX
;
275 path_max
+= 2; /* The getcwd docs say to do this. */
277 dirname
= (char *) alloca (path_max
+ dirname_len
);
278 ADD_BLOCK (block_list
, dirname
);
281 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
283 path_max
+= PATH_INCR
;
284 dirname
= (char *) alloca (path_max
+ dirname_len
);
285 ADD_BLOCK (block_list
, dirname
);
291 /* We cannot get the current working directory. Don't signal an
292 error but simply return the default string. */
293 FREE_BLOCKS (block_list
);
295 return (char *) msgid
;
298 /* We don't want libintl.a to depend on any other library. So
299 we avoid the non-standard function stpcpy. In GNU C Library
300 this function is available, though. Also allow the symbol
301 HAVE_STPCPY to be defined. */
302 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
305 /* Now determine the symbolic name of CATEGORY and its value. */
306 categoryname
= category_to_name (category
);
307 categoryvalue
= guess_category_value (category
, categoryname
);
309 xdomainname
= (char *) alloca (strlen (categoryname
)
310 + strlen (domainname
) + 5);
311 ADD_BLOCK (block_list
, xdomainname
);
312 /* We don't want libintl.a to depend on any other library. So we
313 avoid the non-standard function stpcpy. In GNU C Library this
314 function is available, though. Also allow the symbol HAVE_STPCPY
316 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
320 /* Creating working area. */
321 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
322 ADD_BLOCK (block_list
, single_locale
);
325 /* Search for the given string. This is a loop because we perhaps
326 got an ordered list of languages to consider for th translation. */
329 /* Make CATEGORYVALUE point to the next element of the list. */
330 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
332 if (categoryvalue
[0] == '\0')
334 /* The whole contents of CATEGORYVALUE has been searched but
335 no valid entry has been found. We solve this situation
336 by implicitely appending a "C" entry, i.e. no translation
338 single_locale
[0] = 'C';
339 single_locale
[1] = '\0';
343 char *cp
= single_locale
;
344 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
345 *cp
++ = *categoryvalue
++;
349 /* If the current locale value is C (or POSIX) we don't load a
350 domain. Return the MSGID. */
351 if (strcmp (single_locale
, "C") == 0
352 || strcmp (single_locale
, "POSIX") == 0)
354 FREE_BLOCKS (block_list
);
356 return (char *) msgid
;
360 /* Find structure describing the message catalog matching the
361 DOMAINNAME and CATEGORY. */
362 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
);
366 retval
= find_msg (domain
, msgid
);
372 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
374 retval
= find_msg (domain
->successor
[cnt
], msgid
);
383 FREE_BLOCKS (block_list
);
393 /* Alias for function name in GNU C Library. */
394 weak_alias (__dcgettext
, dcgettext
);
399 find_msg (domain_file
, msgid
)
400 struct loaded_l10nfile
*domain_file
;
403 size_t top
, act
, bottom
;
404 struct loaded_domain
*domain
;
406 if (domain_file
->decided
== 0)
407 _nl_load_domain (domain_file
);
409 if (domain_file
->data
== NULL
)
412 domain
= (struct loaded_domain
*) domain_file
->data
;
414 /* Locate the MSGID and its translation. */
415 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
417 /* Use the hashing table. */
418 nls_uint32 len
= strlen (msgid
);
419 nls_uint32 hash_val
= hash_string (msgid
);
420 nls_uint32 idx
= hash_val
% domain
->hash_size
;
421 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
422 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
425 /* Hash table entry is empty. */
428 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
430 domain
->data
+ W (domain
->must_swap
,
431 domain
->orig_tab
[nstr
- 1].offset
)) == 0)
432 return (char *) domain
->data
+ W (domain
->must_swap
,
433 domain
->trans_tab
[nstr
- 1].offset
);
437 if (idx
>= domain
->hash_size
- incr
)
438 idx
-= domain
->hash_size
- incr
;
442 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
444 /* Hash table entry is empty. */
447 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
449 domain
->data
+ W (domain
->must_swap
,
450 domain
->orig_tab
[nstr
- 1].offset
))
452 return (char *) domain
->data
453 + W (domain
->must_swap
, domain
->trans_tab
[nstr
- 1].offset
);
458 /* Now we try the default method: binary search in the sorted
459 array of messages. */
461 top
= domain
->nstrings
;
466 act
= (bottom
+ top
) / 2;
467 cmp_val
= strcmp (msgid
, domain
->data
468 + W (domain
->must_swap
,
469 domain
->orig_tab
[act
].offset
));
472 else if (cmp_val
> 0)
478 /* If an translation is found return this. */
479 return bottom
>= top
? NULL
: (char *) domain
->data
480 + W (domain
->must_swap
,
481 domain
->trans_tab
[act
].offset
);
485 /* Return string representation of locale CATEGORY. */
487 category_to_name (category
)
496 retval
= "LC_COLLATE";
506 retval
= "LC_MONETARY";
511 retval
= "LC_NUMERIC";
521 retval
= "LC_MESSAGES";
526 retval
= "LC_RESPONSE";
531 /* This might not make sense but is perhaps better than any other
537 /* If you have a better idea for a default value let me know. */
544 /* Guess value of current locale from value of the environment variables. */
545 static const char *guess_category_value (category
, categoryname
)
547 const char *categoryname
;
551 /* The highest priority value is the `LANGUAGE' environment
552 variable. This is a GNU extension. */
553 retval
= getenv ("LANGUAGE");
554 if (retval
!= NULL
&& retval
[0] != '\0')
557 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
558 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
559 systems this can be done by the `setlocale' function itself. */
560 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
561 return setlocale (category
, NULL
);
563 /* Setting of LC_ALL overwrites all other. */
564 retval
= getenv ("LC_ALL");
565 if (retval
!= NULL
&& retval
[0] != '\0')
568 /* Next comes the name of the desired category. */
569 retval
= getenv (categoryname
);
570 if (retval
!= NULL
&& retval
[0] != '\0')
573 /* Last possibility is the LANG environment variable. */
574 retval
= getenv ("LANG");
575 if (retval
!= NULL
&& retval
[0] != '\0')
578 /* We use C as the default domain. POSIX says this is implementation
584 /* @@ begin of epilog @@ */
586 /* We don't want libintl.a to depend on any other library. So we
587 avoid the non-standard function stpcpy. In GNU C Library this
588 function is available, though. Also allow the symbol HAVE_STPCPY
590 #if !_LIBC && !HAVE_STPCPY
596 while ((*dest
++ = *src
++) != '\0')