1 /* Implementation of the dcgettext(3) function.
2 Copyright (C) 1995, 1996, 1997, 1998 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include <sys/types.h>
25 # define alloca __builtin_alloca
26 # define HAVE_ALLOCA 1
28 # if defined HAVE_ALLOCA_H || defined _LIBC
46 # define __set_errno(val) errno = (val)
49 #if defined STDC_HEADERS || defined _LIBC
60 #if defined HAVE_STRING_H || defined _LIBC
62 # define _GNU_SOURCE 1
68 #if !HAVE_STRCHR && !defined _LIBC
74 #if defined HAVE_UNISTD_H || defined _LIBC
83 # include "libgettext.h"
85 #include "hash-string.h"
87 /* @@ end of prolog @@ */
90 /* Rename the non ANSI C functions. This is required by the standard
91 because some ANSI C functions will require linking with this object
92 file and the name space must not be polluted. */
93 # define getcwd __getcwd
95 # define stpcpy __stpcpy
98 # if !defined HAVE_GETCWD
100 # define getcwd(buf, max) getwd (buf)
105 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
109 /* Amount to increase buffer size by in each try. */
112 /* The following is from pathmax.h. */
113 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
114 PATH_MAX but might cause redefinition warnings when sys/param.h is
115 later included (as on MORE/BSD 4.3). */
116 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
120 #ifndef _POSIX_PATH_MAX
121 # define _POSIX_PATH_MAX 255
124 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
125 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
128 /* Don't include sys/param.h if it already has been. */
129 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
130 # include <sys/param.h>
133 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
134 # define PATH_MAX MAXPATHLEN
138 # define PATH_MAX _POSIX_PATH_MAX
141 /* XPG3 defines the result of `setlocale (category, NULL)' as:
142 ``Directs `setlocale()' to query `category' and return the current
143 setting of `local'.''
144 However it does not specify the exact format. And even worse: POSIX
145 defines this not at all. So we can use this feature only on selected
146 system (e.g. those using GNU C Library). */
148 # define HAVE_LOCALE_NULL
151 /* Name of the default domain used for gettext(3) prior any call to
152 textdomain(3). The default value for this is "messages". */
153 const char _nl_default_default_domain
[] = "messages";
155 /* Value used as the default domain for gettext(3). */
156 const char *_nl_current_default_domain
= _nl_default_default_domain
;
158 /* Contains the default location of the message catalogs. */
159 const char _nl_default_dirname
[] = GNULOCALEDIR
;
161 /* List with bindings of specific domains created by bindtextdomain()
163 struct binding
*_nl_domain_bindings
;
165 /* Prototypes for local functions. */
166 static char *find_msg
PARAMS ((struct loaded_l10nfile
*domain_file
,
167 const char *msgid
)) internal_function
;
168 static const char *category_to_name
PARAMS ((int category
)) internal_function
;
169 static const char *guess_category_value
PARAMS ((int category
,
170 const char *categoryname
))
174 /* For those loosing systems which don't have `alloca' we have to add
175 some additional code emulating it. */
177 /* Nothing has to be done. */
178 # define ADD_BLOCK(list, address) /* nothing */
179 # define FREE_BLOCKS(list) /* nothing */
184 struct block_list
*next
;
186 # define ADD_BLOCK(list, addr) \
188 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
189 /* If we cannot get a free block we cannot add the new element to \
191 if (newp != NULL) { \
192 newp->address = (addr); \
193 newp->next = (list); \
197 # define FREE_BLOCKS(list) \
199 while (list != NULL) { \
200 struct block_list *old = list; \
206 # define alloca(size) (malloc (size))
207 #endif /* have alloca */
210 /* Names for the libintl functions are a problem. They must not clash
211 with existing names and they should follow ANSI C. But this source
212 code is also used in GNU C Library where the names have a __
213 prefix. So we have to make a difference here. */
215 # define DCGETTEXT __dcgettext
217 # define DCGETTEXT dcgettext__
220 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
223 DCGETTEXT (domainname
, msgid
, category
)
224 const char *domainname
;
229 struct block_list
*block_list
= NULL
;
231 struct loaded_l10nfile
*domain
;
232 struct binding
*binding
;
233 const char *categoryname
;
234 const char *categoryvalue
;
235 char *dirname
, *xdomainname
;
238 int saved_errno
= errno
;
240 /* If no real MSGID is given return NULL. */
244 /* If DOMAINNAME is NULL, we are interested in the default domain. If
245 CATEGORY is not LC_MESSAGES this might not make much sense but the
246 defintion left this undefined. */
247 if (domainname
== NULL
)
248 domainname
= _nl_current_default_domain
;
250 /* First find matching binding. */
251 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
253 int compare
= strcmp (domainname
, binding
->domainname
);
259 /* It is not in the list. */
266 dirname
= (char *) _nl_default_dirname
;
267 else if (binding
->dirname
[0] == '/')
268 dirname
= binding
->dirname
;
271 /* We have a relative path. Make it absolute now. */
272 size_t dirname_len
= strlen (binding
->dirname
) + 1;
276 path_max
= (unsigned) PATH_MAX
;
277 path_max
+= 2; /* The getcwd docs say to do this. */
279 dirname
= (char *) alloca (path_max
+ dirname_len
);
280 ADD_BLOCK (block_list
, dirname
);
283 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
285 path_max
+= PATH_INCR
;
286 dirname
= (char *) alloca (path_max
+ dirname_len
);
287 ADD_BLOCK (block_list
, dirname
);
293 /* We cannot get the current working directory. Don't signal an
294 error but simply return the default string. */
295 FREE_BLOCKS (block_list
);
296 __set_errno (saved_errno
);
297 return (char *) msgid
;
300 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
303 /* Now determine the symbolic name of CATEGORY and its value. */
304 categoryname
= category_to_name (category
);
305 categoryvalue
= guess_category_value (category
, categoryname
);
307 xdomainname
= (char *) alloca (strlen (categoryname
)
308 + strlen (domainname
) + 5);
309 ADD_BLOCK (block_list
, xdomainname
);
311 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
315 /* Creating working area. */
316 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
317 ADD_BLOCK (block_list
, single_locale
);
320 /* Search for the given string. This is a loop because we perhaps
321 got an ordered list of languages to consider for th translation. */
324 /* Make CATEGORYVALUE point to the next element of the list. */
325 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
327 if (categoryvalue
[0] == '\0')
329 /* The whole contents of CATEGORYVALUE has been searched but
330 no valid entry has been found. We solve this situation
331 by implicitly appending a "C" entry, i.e. no translation
333 single_locale
[0] = 'C';
334 single_locale
[1] = '\0';
338 char *cp
= single_locale
;
339 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
340 *cp
++ = *categoryvalue
++;
344 /* If the current locale value is C (or POSIX) we don't load a
345 domain. Return the MSGID. */
346 if (strcmp (single_locale
, "C") == 0
347 || strcmp (single_locale
, "POSIX") == 0)
349 FREE_BLOCKS (block_list
);
350 __set_errno (saved_errno
);
351 return (char *) msgid
;
355 /* Find structure describing the message catalog matching the
356 DOMAINNAME and CATEGORY. */
357 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
);
361 retval
= find_msg (domain
, msgid
);
367 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
369 retval
= find_msg (domain
->successor
[cnt
], msgid
);
378 FREE_BLOCKS (block_list
);
379 __set_errno (saved_errno
);
388 /* Alias for function name in GNU C Library. */
389 weak_alias (__dcgettext
, dcgettext
);
395 find_msg (domain_file
, msgid
)
396 struct loaded_l10nfile
*domain_file
;
399 size_t top
, act
, bottom
;
400 struct loaded_domain
*domain
;
402 if (domain_file
->decided
== 0)
403 _nl_load_domain (domain_file
);
405 if (domain_file
->data
== NULL
)
408 domain
= (struct loaded_domain
*) domain_file
->data
;
410 /* Locate the MSGID and its translation. */
411 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
413 /* Use the hashing table. */
414 nls_uint32 len
= strlen (msgid
);
415 nls_uint32 hash_val
= hash_string (msgid
);
416 nls_uint32 idx
= hash_val
% domain
->hash_size
;
417 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
418 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
421 /* Hash table entry is empty. */
424 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
426 domain
->data
+ W (domain
->must_swap
,
427 domain
->orig_tab
[nstr
- 1].offset
)) == 0)
428 return (char *) domain
->data
+ W (domain
->must_swap
,
429 domain
->trans_tab
[nstr
- 1].offset
);
433 if (idx
>= domain
->hash_size
- incr
)
434 idx
-= domain
->hash_size
- incr
;
438 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
440 /* Hash table entry is empty. */
443 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
445 domain
->data
+ W (domain
->must_swap
,
446 domain
->orig_tab
[nstr
- 1].offset
))
448 return (char *) domain
->data
449 + W (domain
->must_swap
, domain
->trans_tab
[nstr
- 1].offset
);
454 /* Now we try the default method: binary search in the sorted
455 array of messages. */
457 top
= domain
->nstrings
;
462 act
= (bottom
+ top
) / 2;
463 cmp_val
= strcmp (msgid
, domain
->data
464 + W (domain
->must_swap
,
465 domain
->orig_tab
[act
].offset
));
468 else if (cmp_val
> 0)
474 /* If an translation is found return this. */
475 return bottom
>= top
? NULL
: (char *) domain
->data
476 + W (domain
->must_swap
,
477 domain
->trans_tab
[act
].offset
);
481 /* Return string representation of locale CATEGORY. */
484 category_to_name (category
)
493 retval
= "LC_COLLATE";
503 retval
= "LC_MONETARY";
508 retval
= "LC_NUMERIC";
518 retval
= "LC_MESSAGES";
523 retval
= "LC_RESPONSE";
528 /* This might not make sense but is perhaps better than any other
534 /* If you have a better idea for a default value let me know. */
541 /* Guess value of current locale from value of the environment variables. */
544 guess_category_value (category
, categoryname
)
546 const char *categoryname
;
550 /* The highest priority value is the `LANGUAGE' environment
551 variable. This is a GNU extension. */
552 retval
= getenv ("LANGUAGE");
553 if (retval
!= NULL
&& retval
[0] != '\0')
556 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
557 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
558 systems this can be done by the `setlocale' function itself. */
559 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
560 return setlocale (category
, NULL
);
562 /* Setting of LC_ALL overwrites all other. */
563 retval
= getenv ("LC_ALL");
564 if (retval
!= NULL
&& retval
[0] != '\0')
567 /* Next comes the name of the desired category. */
568 retval
= getenv (categoryname
);
569 if (retval
!= NULL
&& retval
[0] != '\0')
572 /* Last possibility is the LANG environment variable. */
573 retval
= getenv ("LANG");
574 if (retval
!= NULL
&& retval
[0] != '\0')
577 /* We use C as the default domain. POSIX says this is implementation
583 /* @@ begin of epilog @@ */
585 /* We don't want libintl.a to depend on any other library. So we
586 avoid the non-standard function stpcpy. In GNU C Library this
587 function is available, though. Also allow the symbol HAVE_STPCPY
589 #if !_LIBC && !HAVE_STPCPY
595 while ((*dest
++ = *src
++) != '\0')
603 /* If we want to free all resources we have to do some work at
605 static void __attribute__ ((unused
))
608 struct binding
*runp
;
610 for (runp
= _nl_domain_bindings
; runp
!= NULL
; runp
= runp
->next
)
612 free (runp
->domainname
);
613 if (runp
->dirname
!= _nl_default_dirname
)
614 /* Yes, this is a pointer comparison. */
615 free (runp
->dirname
);
618 if (_nl_current_default_domain
!= _nl_default_default_domain
)
619 /* Yes, again a pointer comparison. */
620 free ((char *) _nl_current_default_domain
);
623 text_set_element (__libc_subfreeres
, free_mem
);