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 # define __set_errno(val) errno = (val)
53 #if defined STDC_HEADERS || defined _LIBC
64 #if defined HAVE_STRING_H || defined _LIBC
66 # define _GNU_SOURCE 1
72 #if !HAVE_STRCHR && !defined _LIBC
78 #if defined HAVE_UNISTD_H || defined _LIBC
87 # include "libgettext.h"
89 #include "hash-string.h"
91 /* @@ end of prolog @@ */
94 /* Rename the non ANSI C functions. This is required by the standard
95 because some ANSI C functions will require linking with this object
96 file and the name space must not be polluted. */
97 # define getcwd __getcwd
98 # define stpcpy __stpcpy
100 # if !defined HAVE_GETCWD
102 # define getcwd(buf, max) getwd (buf)
107 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
111 /* Amount to increase buffer size by in each try. */
114 /* The following is from pathmax.h. */
115 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
116 PATH_MAX but might cause redefinition warnings when sys/param.h is
117 later included (as on MORE/BSD 4.3). */
118 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
122 #ifndef _POSIX_PATH_MAX
123 # define _POSIX_PATH_MAX 255
126 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
127 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
130 /* Don't include sys/param.h if it already has been. */
131 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
132 # include <sys/param.h>
135 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
136 # define PATH_MAX MAXPATHLEN
140 # define PATH_MAX _POSIX_PATH_MAX
143 /* XPG3 defines the result of `setlocale (category, NULL)' as:
144 ``Directs `setlocale()' to query `category' and return the current
145 setting of `local'.''
146 However it does not specify the exact format. And even worse: POSIX
147 defines this not at all. So we can use this feature only on selected
148 system (e.g. those using GNU C Library). */
150 # define HAVE_LOCALE_NULL
153 /* Name of the default domain used for gettext(3) prior any call to
154 textdomain(3). The default value for this is "messages". */
155 const char _nl_default_default_domain
[] = "messages";
157 /* Value used as the default domain for gettext(3). */
158 const char *_nl_current_default_domain
= _nl_default_default_domain
;
160 /* Contains the default location of the message catalogs. */
161 const char _nl_default_dirname
[] = GNULOCALEDIR
;
163 /* List with bindings of specific domains created by bindtextdomain()
165 struct binding
*_nl_domain_bindings
;
167 /* Prototypes for local functions. */
168 static char *find_msg
PARAMS ((struct loaded_l10nfile
*domain_file
,
170 static const char *category_to_name
PARAMS ((int category
));
171 static const char *guess_category_value
PARAMS ((int category
,
172 const char *categoryname
));
175 /* For those loosing systems which don't have `alloca' we have to add
176 some additional code emulating it. */
178 /* Nothing has to be done. */
179 # define ADD_BLOCK(list, address) /* nothing */
180 # define FREE_BLOCKS(list) /* nothing */
185 struct block_list
*next
;
187 # define ADD_BLOCK(list, addr) \
189 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
190 /* If we cannot get a free block we cannot add the new element to \
192 if (newp != NULL) { \
193 newp->address = (addr); \
194 newp->next = (list); \
198 # define FREE_BLOCKS(list) \
200 while (list != NULL) { \
201 struct block_list *old = list; \
207 # define alloca(size) (malloc (size))
208 #endif /* have alloca */
211 /* Names for the libintl functions are a problem. They must not clash
212 with existing names and they should follow ANSI C. But this source
213 code is also used in GNU C Library where the names have a __
214 prefix. So we have to make a difference here. */
216 # define DCGETTEXT __dcgettext
218 # define DCGETTEXT dcgettext__
221 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
224 DCGETTEXT (domainname
, msgid
, category
)
225 const char *domainname
;
230 struct block_list
*block_list
= NULL
;
232 struct loaded_l10nfile
*domain
;
233 struct binding
*binding
;
234 const char *categoryname
;
235 const char *categoryvalue
;
236 char *dirname
, *xdomainname
;
239 int saved_errno
= errno
;
241 /* If no real MSGID is given return NULL. */
245 /* If DOMAINNAME is NULL, we are interested in the default domain. If
246 CATEGORY is not LC_MESSAGES this might not make much sense but the
247 defintion left this undefined. */
248 if (domainname
== NULL
)
249 domainname
= _nl_current_default_domain
;
251 /* First find matching binding. */
252 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
254 int compare
= strcmp (domainname
, binding
->domainname
);
260 /* It is not in the list. */
267 dirname
= (char *) _nl_default_dirname
;
268 else if (binding
->dirname
[0] == '/')
269 dirname
= binding
->dirname
;
272 /* We have a relative path. Make it absolute now. */
273 size_t dirname_len
= strlen (binding
->dirname
) + 1;
277 path_max
= (unsigned) PATH_MAX
;
278 path_max
+= 2; /* The getcwd docs say to do this. */
280 dirname
= (char *) alloca (path_max
+ dirname_len
);
281 ADD_BLOCK (block_list
, dirname
);
284 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
286 path_max
+= PATH_INCR
;
287 dirname
= (char *) alloca (path_max
+ dirname_len
);
288 ADD_BLOCK (block_list
, dirname
);
294 /* We cannot get the current working directory. Don't signal an
295 error but simply return the default string. */
296 FREE_BLOCKS (block_list
);
297 __set_errno (saved_errno
);
298 return (char *) msgid
;
301 /* We don't want libintl.a to depend on any other library. So
302 we avoid the non-standard function stpcpy. In GNU C Library
303 this function is available, though. Also allow the symbol
304 HAVE_STPCPY to be defined. */
305 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
308 /* Now determine the symbolic name of CATEGORY and its value. */
309 categoryname
= category_to_name (category
);
310 categoryvalue
= guess_category_value (category
, categoryname
);
312 xdomainname
= (char *) alloca (strlen (categoryname
)
313 + strlen (domainname
) + 5);
314 ADD_BLOCK (block_list
, xdomainname
);
315 /* We don't want libintl.a to depend on any other library. So we
316 avoid the non-standard function stpcpy. In GNU C Library this
317 function is available, though. Also allow the symbol HAVE_STPCPY
319 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
323 /* Creating working area. */
324 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
325 ADD_BLOCK (block_list
, single_locale
);
328 /* Search for the given string. This is a loop because we perhaps
329 got an ordered list of languages to consider for th translation. */
332 /* Make CATEGORYVALUE point to the next element of the list. */
333 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
335 if (categoryvalue
[0] == '\0')
337 /* The whole contents of CATEGORYVALUE has been searched but
338 no valid entry has been found. We solve this situation
339 by implicitely appending a "C" entry, i.e. no translation
341 single_locale
[0] = 'C';
342 single_locale
[1] = '\0';
346 char *cp
= single_locale
;
347 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
348 *cp
++ = *categoryvalue
++;
352 /* If the current locale value is C (or POSIX) we don't load a
353 domain. Return the MSGID. */
354 if (strcmp (single_locale
, "C") == 0
355 || strcmp (single_locale
, "POSIX") == 0)
357 FREE_BLOCKS (block_list
);
358 __set_errno (saved_errno
);
359 return (char *) msgid
;
363 /* Find structure describing the message catalog matching the
364 DOMAINNAME and CATEGORY. */
365 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
);
369 retval
= find_msg (domain
, msgid
);
375 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
377 retval
= find_msg (domain
->successor
[cnt
], msgid
);
386 FREE_BLOCKS (block_list
);
387 __set_errno (saved_errno
);
396 /* Alias for function name in GNU C Library. */
397 weak_alias (__dcgettext
, dcgettext
);
402 find_msg (domain_file
, msgid
)
403 struct loaded_l10nfile
*domain_file
;
406 size_t top
, act
, bottom
;
407 struct loaded_domain
*domain
;
409 if (domain_file
->decided
== 0)
410 _nl_load_domain (domain_file
);
412 if (domain_file
->data
== NULL
)
415 domain
= (struct loaded_domain
*) domain_file
->data
;
417 /* Locate the MSGID and its translation. */
418 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
420 /* Use the hashing table. */
421 nls_uint32 len
= strlen (msgid
);
422 nls_uint32 hash_val
= hash_string (msgid
);
423 nls_uint32 idx
= hash_val
% domain
->hash_size
;
424 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
425 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
428 /* Hash table entry is empty. */
431 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
433 domain
->data
+ W (domain
->must_swap
,
434 domain
->orig_tab
[nstr
- 1].offset
)) == 0)
435 return (char *) domain
->data
+ W (domain
->must_swap
,
436 domain
->trans_tab
[nstr
- 1].offset
);
440 if (idx
>= domain
->hash_size
- incr
)
441 idx
-= domain
->hash_size
- incr
;
445 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
447 /* Hash table entry is empty. */
450 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
452 domain
->data
+ W (domain
->must_swap
,
453 domain
->orig_tab
[nstr
- 1].offset
))
455 return (char *) domain
->data
456 + W (domain
->must_swap
, domain
->trans_tab
[nstr
- 1].offset
);
461 /* Now we try the default method: binary search in the sorted
462 array of messages. */
464 top
= domain
->nstrings
;
469 act
= (bottom
+ top
) / 2;
470 cmp_val
= strcmp (msgid
, domain
->data
471 + W (domain
->must_swap
,
472 domain
->orig_tab
[act
].offset
));
475 else if (cmp_val
> 0)
481 /* If an translation is found return this. */
482 return bottom
>= top
? NULL
: (char *) domain
->data
483 + W (domain
->must_swap
,
484 domain
->trans_tab
[act
].offset
);
488 /* Return string representation of locale CATEGORY. */
490 category_to_name (category
)
499 retval
= "LC_COLLATE";
509 retval
= "LC_MONETARY";
514 retval
= "LC_NUMERIC";
524 retval
= "LC_MESSAGES";
529 retval
= "LC_RESPONSE";
534 /* This might not make sense but is perhaps better than any other
540 /* If you have a better idea for a default value let me know. */
547 /* Guess value of current locale from value of the environment variables. */
548 static const char *guess_category_value (category
, categoryname
)
550 const char *categoryname
;
554 /* The highest priority value is the `LANGUAGE' environment
555 variable. This is a GNU extension. */
556 retval
= getenv ("LANGUAGE");
557 if (retval
!= NULL
&& retval
[0] != '\0')
560 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
561 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
562 systems this can be done by the `setlocale' function itself. */
563 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
564 return setlocale (category
, NULL
);
566 /* Setting of LC_ALL overwrites all other. */
567 retval
= getenv ("LC_ALL");
568 if (retval
!= NULL
&& retval
[0] != '\0')
571 /* Next comes the name of the desired category. */
572 retval
= getenv (categoryname
);
573 if (retval
!= NULL
&& retval
[0] != '\0')
576 /* Last possibility is the LANG environment variable. */
577 retval
= getenv ("LANG");
578 if (retval
!= NULL
&& retval
[0] != '\0')
581 /* We use C as the default domain. POSIX says this is implementation
587 /* @@ begin of epilog @@ */
589 /* We don't want libintl.a to depend on any other library. So we
590 avoid the non-standard function stpcpy. In GNU C Library this
591 function is available, though. Also allow the symbol HAVE_STPCPY
593 #if !_LIBC && !HAVE_STPCPY
599 while ((*dest
++ = *src
++) != '\0')