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
31 # if defined HAVE_ALLOCA_H || defined _LIBC
49 #if defined STDC_HEADERS || defined _LIBC
60 #if defined HAVE_STRING_H || defined _LIBC
65 #if !HAVE_STRCHR && !defined _LIBC
71 #if defined HAVE_UNISTD_H || defined _LIBC
80 # include "libgettext.h"
82 #include "hash-string.h"
84 /* @@ end of prolog @@ */
87 /* Rename the non ANSI C functions. This is required by the standard
88 because some ANSI C functions will require linking with this object
89 file and the name space must not be polluted. */
90 # define getcwd __getcwd
91 # define stpcpy __stpcpy
93 # if !defined HAVE_GETCWD
95 # define getcwd(buf, max) getwd (buf)
101 /* Amount to increase buffer size by in each try. */
104 /* The following is from pathmax.h. */
105 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
106 PATH_MAX but might cause redefinition warnings when sys/param.h is
107 later included (as on MORE/BSD 4.3). */
108 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
112 #ifndef _POSIX_PATH_MAX
113 # define _POSIX_PATH_MAX 255
116 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
117 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
120 /* Don't include sys/param.h if it already has been. */
121 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
122 # include <sys/param.h>
125 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
126 # define PATH_MAX MAXPATHLEN
130 # define PATH_MAX _POSIX_PATH_MAX
133 /* XPG3 defines the result of `setlocale (category, NULL)' as:
134 ``Directs `setlocale()' to query `category' and return the current
135 setting of `local'.''
136 However it does not specify the exact format. And even worse: POSIX
137 defines this not at all. So we can use this feature only on selected
138 system (e.g. those using GNU C Library). */
140 # define HAVE_LOCALE_NULL
143 /* Name of the default domain used for gettext(3) prior any call to
144 textdomain(3). The default value for this is "messages". */
145 const char _nl_default_default_domain
[] = "messages";
147 /* Value used as the default domain for gettext(3). */
148 const char *_nl_current_default_domain
= _nl_default_default_domain
;
150 /* Contains the default location of the message catalogs. */
151 const char _nl_default_dirname
[] = GNULOCALEDIR
;
153 /* List with bindings of specific domains created by bindtextdomain()
155 struct binding
*_nl_domain_bindings
;
157 /* Prototypes for local functions. */
158 static char *find_msg
PARAMS ((struct loaded_l10nfile
*domain_file
,
160 static const char *category_to_name
PARAMS ((int category
));
161 static const char *guess_category_value
PARAMS ((int category
,
162 const char *categoryname
));
166 #define HAVE_ALLOCA 1
169 /* For those loosing systems which don't have `alloca' we have to add
170 some additional code emulating it. */
172 /* Nothing has to be done. */
173 # define ADD_BLOCK(list, address) /* nothing */
174 # define FREE_BLOCKS(list) /* nothing */
179 struct block_list
*next
;
181 # define ADD_BLOCK(list, addr) \
183 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
184 /* If we cannot get a free block we cannot add the new element to \
186 if (newp != NULL) { \
187 newp->address = (addr); \
188 newp->next = (list); \
192 # define FREE_BLOCKS(list) \
194 while (list != NULL) { \
195 struct block_list *old = list; \
201 # define alloca(size) (malloc (size))
202 #endif /* have alloca */
205 /* Names for the libintl functions are a problem. They must not clash
206 with existing names and they should follow ANSI C. But this source
207 code is also used in GNU C Library where the names have a __
208 prefix. So we have to make a difference here. */
210 # define DCGETTEXT __dcgettext
212 # define DCGETTEXT dcgettext__
215 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
218 DCGETTEXT (domainname
, msgid
, category
)
219 const char *domainname
;
224 struct block_list
*alloca_list
= NULL
;
226 struct loaded_l10nfile
*domain
;
227 struct binding
*binding
;
228 const char *categoryname
;
229 const char *categoryvalue
;
230 char *dirname
, *xdomainname
;
233 int saved_errno
= errno
;
235 /* If no real MSGID is given return NULL. */
239 /* If DOMAINNAME is NULL, we are interested in the default domain. If
240 CATEGORY is not LC_MESSAGES this might not make much sense but the
241 defintion left this undefined. */
242 if (domainname
== NULL
)
243 domainname
= _nl_current_default_domain
;
245 /* First find matching binding. */
246 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
248 int compare
= strcmp (domainname
, binding
->domainname
);
254 /* It is not in the list. */
261 dirname
= (char *) _nl_default_dirname
;
262 else if (binding
->dirname
[0] == '/')
263 dirname
= binding
->dirname
;
266 /* We have a relative path. Make it absolute now. */
267 size_t dirname_len
= strlen (binding
->dirname
) + 1;
271 path_max
= (unsigned) PATH_MAX
;
272 path_max
+= 2; /* The getcwd docs say to do this. */
274 dirname
= (char *) alloca (path_max
+ dirname_len
);
275 ADD_BLOCK (block_list
, dirname
);
278 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
280 path_max
+= PATH_INCR
;
281 dirname
= (char *) alloca (path_max
+ dirname_len
);
282 ADD_BLOCK (block_list
, dirname
);
288 /* We cannot get the current working directory. Don't signal an
289 error but simply return the default string. */
290 FREE_BLOCKS (block_list
);
292 return (char *) msgid
;
295 /* We don't want libintl.a to depend on any other library. So
296 we avoid the non-standard function stpcpy. In GNU C Library
297 this function is available, though. Also allow the symbol
298 HAVE_STPCPY to be defined. */
299 #if defined _LIBC || defined HAVE_STPCPY
300 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
302 strcat (dirname
, "/");
303 strcat (dirname
, binding
->dirname
);
307 /* Now determine the symbolic name of CATEGORY and its value. */
308 categoryname
= category_to_name (category
);
309 categoryvalue
= guess_category_value (category
, categoryname
);
311 xdomainname
= (char *) alloca (strlen (categoryname
)
312 + strlen (domainname
) + 5);
313 ADD_BLOCK (block_list
, xdomainname
);
314 /* We don't want libintl.a to depend on any other library. So we
315 avoid the non-standard function stpcpy. In GNU C Library this
316 function is available, though. Also allow the symbol HAVE_STPCPY
318 #if defined _LIBC || defined HAVE_STPCPY
319 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
323 strcpy (xdomainname
, categoryname
);
324 strcat (xdomainname
, "/");
325 strcat (xdomainname
, domainname
);
326 strcat (xdomainname
, ".mo");
329 /* Creating working area. */
330 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
331 ADD_BLOCK (block_list
, single_locale
);
334 /* Search for the given string. This is a loop because we perhaps
335 got an ordered list of languages to consider for th translation. */
338 /* Make CATEGORYVALUE point to the next element of the list. */
339 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
341 if (categoryvalue
[0] == '\0')
343 /* The whole contents of CATEGORYVALUE has been searched but
344 no valid entry has been found. We solve this situation
345 by implicitely appending a "C" entry, i.e. no translation
347 single_locale
[0] = 'C';
348 single_locale
[1] = '\0';
352 char *cp
= single_locale
;
353 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
354 *cp
++ = *categoryvalue
++;
358 /* If the current locale value is C (or POSIX) we don't load a
359 domain. Return the MSGID. */
360 if (strcmp (single_locale
, "C") == 0
361 || strcmp (single_locale
, "POSIX") == 0)
363 FREE_BLOCKS (block_list
);
365 return (char *) msgid
;
369 /* Find structure describing the message catalog matching the
370 DOMAINNAME and CATEGORY. */
371 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
);
375 retval
= find_msg (domain
, msgid
);
381 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
383 retval
= find_msg (domain
->successor
[cnt
], msgid
);
392 FREE_BLOCKS (block_list
);
402 /* Alias for function name in GNU C Library. */
403 weak_alias (__dcgettext
, dcgettext
);
408 find_msg (domain_file
, msgid
)
409 struct loaded_l10nfile
*domain_file
;
412 size_t top
, act
, bottom
;
413 struct loaded_domain
*domain
;
415 if (domain_file
->decided
== 0)
416 _nl_load_domain (domain_file
);
418 if (domain_file
->data
== NULL
)
421 domain
= (struct loaded_domain
*) domain_file
->data
;
423 /* Locate the MSGID and its translation. */
424 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
426 /* Use the hashing table. */
427 nls_uint32 len
= strlen (msgid
);
428 nls_uint32 hash_val
= hash_string (msgid
);
429 nls_uint32 idx
= hash_val
% domain
->hash_size
;
430 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
431 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
434 /* Hash table entry is empty. */
437 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
439 domain
->data
+ W (domain
->must_swap
,
440 domain
->orig_tab
[nstr
- 1].offset
)) == 0)
441 return (char *) domain
->data
+ W (domain
->must_swap
,
442 domain
->trans_tab
[nstr
- 1].offset
);
446 if (idx
>= domain
->hash_size
- incr
)
447 idx
-= domain
->hash_size
- incr
;
451 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
453 /* Hash table entry is empty. */
456 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
458 domain
->data
+ W (domain
->must_swap
,
459 domain
->orig_tab
[nstr
- 1].offset
))
461 return (char *) domain
->data
462 + W (domain
->must_swap
, domain
->trans_tab
[nstr
- 1].offset
);
467 /* Now we try the default method: binary search in the sorted
468 array of messages. */
470 top
= domain
->nstrings
;
475 act
= (bottom
+ top
) / 2;
476 cmp_val
= strcmp (msgid
, domain
->data
477 + W (domain
->must_swap
,
478 domain
->orig_tab
[act
].offset
));
481 else if (cmp_val
> 0)
487 /* If an translation is found return this. */
488 return bottom
>= top
? NULL
: (char *) domain
->data
489 + W (domain
->must_swap
,
490 domain
->trans_tab
[act
].offset
);
494 /* Return string representation of locale CATEGORY. */
496 category_to_name (category
)
505 retval
= "LC_COLLATE";
515 retval
= "LC_MONETARY";
520 retval
= "LC_NUMERIC";
530 retval
= "LC_MESSAGES";
535 retval
= "LC_RESPONSE";
540 /* This might not make sense but is perhaps better than any other
546 /* If you have a better idea for a default value let me know. */
553 /* Guess value of current locale from value of the environment variables. */
554 static const char *guess_category_value (category
, categoryname
)
556 const char *categoryname
;
560 /* The highest priority value is the `LANGUAGE' environment
561 variable. This is a GNU extension. */
562 retval
= getenv ("LANGUAGE");
563 if (retval
!= NULL
&& retval
[0] != '\0')
566 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
567 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
568 systems this can be done by the `setlocale' function itself. */
569 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
570 return setlocale (category
, NULL
);
572 /* Setting of LC_ALL overwrites all other. */
573 retval
= getenv ("LC_ALL");
574 if (retval
!= NULL
&& retval
[0] != '\0')
577 /* Next comes the name of the desired category. */
578 retval
= getenv (categoryname
);
579 if (retval
!= NULL
&& retval
[0] != '\0')
582 /* Last possibility is the LANG environment variable. */
583 retval
= getenv ("LANG");
584 if (retval
!= NULL
&& retval
[0] != '\0')
587 /* We use C as the default domain. POSIX says this is implementation