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 02111-1307 USA. */
18 #include "intlconfig.h"
20 #include <sys/types.h>
23 # define alloca __builtin_alloca
24 # define HAVE_ALLOCA 1
26 # if defined HAVE_ALLOCA_H || defined _LIBC
44 # define __set_errno(val) errno = (val)
47 #if defined STDC_HEADERS || defined _LIBC
58 #if defined HAVE_STRING_H || defined _LIBC
60 # define _GNU_SOURCE 1
66 #if !HAVE_STRCHR && !defined _LIBC
72 #if defined HAVE_UNISTD_H || defined _LIBC
81 # include "libgettext.h"
83 #include "hash-string.h"
85 /* @@ end of prolog @@ */
88 /* Rename the non ANSI C functions. This is required by the standard
89 because some ANSI C functions will require linking with this object
90 file and the name space must not be polluted. */
91 # define getcwd __getcwd
93 # define stpcpy __stpcpy
96 # if !defined HAVE_GETCWD
98 # define getcwd(buf, max) getwd (buf)
103 static char *stpcpy PARAMS ((char *dest, const char *src));
107 /* Amount to increase buffer size by in each try. */
110 /* The following is from pathmax.h. */
111 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
112 PATH_MAX but might cause redefinition warnings when sys/param.h is
113 later included (as on MORE/BSD 4.3). */
114 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
118 #ifndef _POSIX_PATH_MAX
119 # define _POSIX_PATH_MAX 255
122 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
123 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
126 /* Don't include sys/param.h if it already has been. */
127 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
128 # include <sys/param.h>
131 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
132 # define PATH_MAX MAXPATHLEN
136 # define PATH_MAX _POSIX_PATH_MAX
139 /* XPG3 defines the result of `setlocale (category, NULL)' as:
140 ``Directs `setlocale()' to query `category' and return the current
141 setting of `local'.''
142 However it does not specify the exact format. And even worse: POSIX
143 defines this not at all. So we can use this feature only on selected
144 system (e.g. those using GNU C Library). */
146 # define HAVE_LOCALE_NULL
149 /* Name of the default domain used for gettext(3) prior any call to
150 textdomain(3). The default value for this is "messages". */
151 const char _nl_default_default_domain[] = "messages";
153 /* Value used as the default domain for gettext(3). */
154 const char *_nl_current_default_domain = _nl_default_default_domain;
156 /* Contains the default location of the message catalogs. */
157 const char _nl_default_dirname[] = GNULOCALEDIR;
159 /* List with bindings of specific domains created by bindtextdomain()
161 struct binding *_nl_domain_bindings;
163 /* Prototypes for local functions. */
164 static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
165 const char *msgid)) internal_function;
166 static const char *category_to_name PARAMS ((int category)) internal_function;
167 static const char *guess_category_value PARAMS ((int category,
168 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] == '/' || binding->dirname[1] == ':' ) /*FRANCO für win32*/
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);
294 __set_errno (saved_errno);
295 return (char *) msgid;
298 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
301 /* Now determine the symbolic name of CATEGORY and its value. */
302 categoryname = category_to_name (category);
303 categoryvalue = guess_category_value (category, categoryname);
305 xdomainname = (char *) alloca (strlen (categoryname)
306 + strlen (domainname) + 5);
307 ADD_BLOCK (block_list, xdomainname);
309 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
313 /* Creating working area. */
314 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
315 ADD_BLOCK (block_list, single_locale);
318 /* Search for the given string. This is a loop because we perhaps
319 got an ordered list of languages to consider for th translation. */
322 /* Make CATEGORYVALUE point to the next element of the list. */
323 while (categoryvalue[0] != '\0' && categoryvalue[0] == ';') /* FRANCO changed : to ; */
325 if (categoryvalue[0] == '\0')
327 /* The whole contents of CATEGORYVALUE has been searched but
328 no valid entry has been found. We solve this situation
329 by implicitly appending a "C" entry, i.e. no translation
331 single_locale[0] = 'C';
332 single_locale[1] = '\0';
336 char *cp = single_locale;
337 while (categoryvalue[0] != '\0' && categoryvalue[0] != ';')/* FRANCO changed : to ; */
338 *cp++ = *categoryvalue++;
342 /* If the current locale value is C (or POSIX) we don't load a
343 domain. Return the MSGID. */
344 if (strcmp (single_locale, "C") == 0
345 || strcmp (single_locale, "POSIX") == 0)
347 FREE_BLOCKS (block_list);
348 __set_errno (saved_errno);
349 return (char *) msgid;
353 /* Find structure describing the message catalog matching the
354 DOMAINNAME and CATEGORY. */
355 domain = _nl_find_domain (dirname, single_locale, xdomainname);
359 retval = find_msg (domain, msgid);
365 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
367 retval = find_msg (domain->successor[cnt], msgid);
376 FREE_BLOCKS (block_list);
377 __set_errno (saved_errno);
386 /* Alias for function name in GNU C Library. */
387 weak_alias (__dcgettext, dcgettext);
393 find_msg (domain_file, msgid)
394 struct loaded_l10nfile *domain_file;
397 size_t top, act, bottom;
398 struct loaded_domain *domain;
400 if (domain_file->decided == 0)
401 _nl_load_domain (domain_file);
403 if (domain_file->data == NULL)
406 domain = (struct loaded_domain *) domain_file->data;
408 /* Locate the MSGID and its translation. */
409 if (domain->hash_size > 2 && domain->hash_tab != NULL)
411 /* Use the hashing table. */
412 nls_uint32 len = strlen (msgid);
413 nls_uint32 hash_val = hash_string (msgid);
414 nls_uint32 idx = hash_val % domain->hash_size;
415 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
416 nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
419 /* Hash table entry is empty. */
422 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
424 domain->data + W (domain->must_swap,
425 domain->orig_tab[nstr - 1].offset)) == 0)
426 return (char *) domain->data + W (domain->must_swap,
427 domain->trans_tab[nstr - 1].offset);
431 if (idx >= domain->hash_size - incr)
432 idx -= domain->hash_size - incr;
436 nstr = W (domain->must_swap, domain->hash_tab[idx]);
438 /* Hash table entry is empty. */
441 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
443 domain->data + W (domain->must_swap,
444 domain->orig_tab[nstr - 1].offset))
446 return (char *) domain->data
447 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
452 /* Now we try the default method: binary search in the sorted
453 array of messages. */
455 top = domain->nstrings;
460 act = (bottom + top) / 2;
461 cmp_val = strcmp (msgid, domain->data
462 + W (domain->must_swap,
463 domain->orig_tab[act].offset));
466 else if (cmp_val > 0)
472 /* If an translation is found return this. */
473 return bottom >= top ? NULL : (char *) domain->data
474 + W (domain->must_swap,
475 domain->trans_tab[act].offset);
479 /* Return string representation of locale CATEGORY. */
482 category_to_name (category)
491 retval = "LC_COLLATE";
501 retval = "LC_MONETARY";
506 retval = "LC_NUMERIC";
516 retval = "LC_MESSAGES";
521 retval = "LC_RESPONSE";
526 /* This might not make sense but is perhaps better than any other
532 /* If you have a better idea for a default value let me know. */
539 /* Guess value of current locale from value of the environment variables. */
542 guess_category_value (category, categoryname)
544 const char *categoryname;
548 /* The highest priority value is the `LANGUAGE' environment
549 variable. This is a GNU extension. */
550 retval = getenv ("LANGUAGE");
551 if (retval != NULL && retval[0] != '\0')
554 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
555 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
556 systems this can be done by the `setlocale' function itself. */
557 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
558 return setlocale (category, NULL);
560 /* Setting of LC_ALL overwrites all other. */
561 retval = getenv ("LC_ALL");
562 if (retval != NULL && retval[0] != '\0')
565 /* Next comes the name of the desired category. */
566 retval = getenv (categoryname);
567 if (retval != NULL && retval[0] != '\0')
570 /* Last possibility is the LANG environment variable. */
571 retval = getenv ("LANG");
572 if (retval != NULL && retval[0] != '\0')
575 /* We use C as the default domain. POSIX says this is implementation
581 /* @@ begin of epilog @@ */
583 /* We don't want libintl.a to depend on any other library. So we
584 avoid the non-standard function stpcpy. In GNU C Library this
585 function is available, though. Also allow the symbol HAVE_STPCPY
587 #if !_LIBC && !HAVE_STPCPY
593 while ((*dest++ = *src++) != '\0')
601 /* If we want to free all resources we have to do some work at
603 static void __attribute__ ((unused))
606 struct binding *runp;
608 for (runp = _nl_domain_bindings; runp != NULL; runp = runp->next)
610 free (runp->domainname);
611 if (runp->dirname != _nl_default_dirname)
612 /* Yes, this is a pointer comparison. */
613 free (runp->dirname);
616 if (_nl_current_default_domain != _nl_default_default_domain)
617 /* Yes, again a pointer comparison. */
618 free ((char *) _nl_current_default_domain);
621 text_set_element (__libc_subfreeres, free_mem);