1 /* Implementation of the dcgettext(3) function
2 Copyright (C) 1995, 1996, 1997 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
94 # 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
,
166 static const char *category_to_name
PARAMS ((int category
));
167 static const char *guess_category_value
PARAMS ((int category
,
168 const char *categoryname
));
171 /* For those loosing systems which don't have `alloca' we have to add
172 some additional code emulating it. */
174 /* Nothing has to be done. */
175 # define ADD_BLOCK(list, address) /* nothing */
176 # define FREE_BLOCKS(list) /* nothing */
181 struct block_list
*next
;
183 # define ADD_BLOCK(list, addr) \
185 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
186 /* If we cannot get a free block we cannot add the new element to \
188 if (newp != NULL) { \
189 newp->address = (addr); \
190 newp->next = (list); \
194 # define FREE_BLOCKS(list) \
196 while (list != NULL) { \
197 struct block_list *old = list; \
203 # define alloca(size) (malloc (size))
204 #endif /* have alloca */
207 /* Names for the libintl functions are a problem. They must not clash
208 with existing names and they should follow ANSI C. But this source
209 code is also used in GNU C Library where the names have a __
210 prefix. So we have to make a difference here. */
212 # define DCGETTEXT __dcgettext
214 # define DCGETTEXT dcgettext__
217 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
220 DCGETTEXT (domainname
, msgid
, category
)
221 const char *domainname
;
226 struct block_list
*block_list
= NULL
;
228 struct loaded_l10nfile
*domain
;
229 struct binding
*binding
;
230 const char *categoryname
;
231 const char *categoryvalue
;
232 char *dirname
, *xdomainname
;
235 int saved_errno
= errno
;
237 /* If no real MSGID is given return NULL. */
241 /* If DOMAINNAME is NULL, we are interested in the default domain. If
242 CATEGORY is not LC_MESSAGES this might not make much sense but the
243 defintion left this undefined. */
244 if (domainname
== NULL
)
245 domainname
= _nl_current_default_domain
;
247 /* First find matching binding. */
248 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
250 int compare
= strcmp (domainname
, binding
->domainname
);
256 /* It is not in the list. */
263 dirname
= (char *) _nl_default_dirname
;
264 else if (binding
->dirname
[0] == '/')
265 dirname
= binding
->dirname
;
268 /* We have a relative path. Make it absolute now. */
269 size_t dirname_len
= strlen (binding
->dirname
) + 1;
273 path_max
= (unsigned) PATH_MAX
;
274 path_max
+= 2; /* The getcwd docs say to do this. */
276 dirname
= (char *) alloca (path_max
+ dirname_len
);
277 ADD_BLOCK (block_list
, dirname
);
280 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
282 path_max
+= PATH_INCR
;
283 dirname
= (char *) alloca (path_max
+ dirname_len
);
284 ADD_BLOCK (block_list
, dirname
);
290 /* We cannot get the current working directory. Don't signal an
291 error but simply return the default string. */
292 FREE_BLOCKS (block_list
);
293 __set_errno (saved_errno
);
294 return (char *) msgid
;
297 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
300 /* Now determine the symbolic name of CATEGORY and its value. */
301 categoryname
= category_to_name (category
);
302 categoryvalue
= guess_category_value (category
, categoryname
);
304 xdomainname
= (char *) alloca (strlen (categoryname
)
305 + strlen (domainname
) + 5);
306 ADD_BLOCK (block_list
, xdomainname
);
308 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
312 /* Creating working area. */
313 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
314 ADD_BLOCK (block_list
, single_locale
);
317 /* Search for the given string. This is a loop because we perhaps
318 got an ordered list of languages to consider for th translation. */
321 /* Make CATEGORYVALUE point to the next element of the list. */
322 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
324 if (categoryvalue
[0] == '\0')
326 /* The whole contents of CATEGORYVALUE has been searched but
327 no valid entry has been found. We solve this situation
328 by implicitly appending a "C" entry, i.e. no translation
330 single_locale
[0] = 'C';
331 single_locale
[1] = '\0';
335 char *cp
= single_locale
;
336 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
337 *cp
++ = *categoryvalue
++;
341 /* If the current locale value is C (or POSIX) we don't load a
342 domain. Return the MSGID. */
343 if (strcmp (single_locale
, "C") == 0
344 || strcmp (single_locale
, "POSIX") == 0)
346 FREE_BLOCKS (block_list
);
347 __set_errno (saved_errno
);
348 return (char *) msgid
;
352 /* Find structure describing the message catalog matching the
353 DOMAINNAME and CATEGORY. */
354 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
);
358 retval
= find_msg (domain
, msgid
);
364 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
366 retval
= find_msg (domain
->successor
[cnt
], msgid
);
375 FREE_BLOCKS (block_list
);
376 __set_errno (saved_errno
);
385 /* Alias for function name in GNU C Library. */
386 weak_alias (__dcgettext
, dcgettext
);
391 find_msg (domain_file
, msgid
)
392 struct loaded_l10nfile
*domain_file
;
395 size_t top
, act
, bottom
;
396 struct loaded_domain
*domain
;
398 if (domain_file
->decided
== 0)
399 _nl_load_domain (domain_file
);
401 if (domain_file
->data
== NULL
)
404 domain
= (struct loaded_domain
*) domain_file
->data
;
406 /* Locate the MSGID and its translation. */
407 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
409 /* Use the hashing table. */
410 nls_uint32 len
= strlen (msgid
);
411 nls_uint32 hash_val
= hash_string (msgid
);
412 nls_uint32 idx
= hash_val
% domain
->hash_size
;
413 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
414 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
417 /* Hash table entry is empty. */
420 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
422 domain
->data
+ W (domain
->must_swap
,
423 domain
->orig_tab
[nstr
- 1].offset
)) == 0)
424 return (char *) domain
->data
+ W (domain
->must_swap
,
425 domain
->trans_tab
[nstr
- 1].offset
);
429 if (idx
>= domain
->hash_size
- incr
)
430 idx
-= domain
->hash_size
- incr
;
434 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
436 /* Hash table entry is empty. */
439 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
441 domain
->data
+ W (domain
->must_swap
,
442 domain
->orig_tab
[nstr
- 1].offset
))
444 return (char *) domain
->data
445 + W (domain
->must_swap
, domain
->trans_tab
[nstr
- 1].offset
);
450 /* Now we try the default method: binary search in the sorted
451 array of messages. */
453 top
= domain
->nstrings
;
458 act
= (bottom
+ top
) / 2;
459 cmp_val
= strcmp (msgid
, domain
->data
460 + W (domain
->must_swap
,
461 domain
->orig_tab
[act
].offset
));
464 else if (cmp_val
> 0)
470 /* If an translation is found return this. */
471 return bottom
>= top
? NULL
: (char *) domain
->data
472 + W (domain
->must_swap
,
473 domain
->trans_tab
[act
].offset
);
477 /* Return string representation of locale CATEGORY. */
479 category_to_name (category
)
488 retval
= "LC_COLLATE";
498 retval
= "LC_MONETARY";
503 retval
= "LC_NUMERIC";
513 retval
= "LC_MESSAGES";
518 retval
= "LC_RESPONSE";
523 /* This might not make sense but is perhaps better than any other
529 /* If you have a better idea for a default value let me know. */
536 /* Guess value of current locale from value of the environment variables. */
538 guess_category_value (category
, categoryname
)
540 const char *categoryname
;
544 /* The highest priority value is the `LANGUAGE' environment
545 variable. This is a GNU extension. */
546 retval
= getenv ("LANGUAGE");
547 if (retval
!= NULL
&& retval
[0] != '\0')
550 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
551 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
552 systems this can be done by the `setlocale' function itself. */
553 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
554 return setlocale (category
, NULL
);
556 /* Setting of LC_ALL overwrites all other. */
557 retval
= getenv ("LC_ALL");
558 if (retval
!= NULL
&& retval
[0] != '\0')
561 /* Next comes the name of the desired category. */
562 retval
= getenv (categoryname
);
563 if (retval
!= NULL
&& retval
[0] != '\0')
566 /* Last possibility is the LANG environment variable. */
567 retval
= getenv ("LANG");
568 if (retval
!= NULL
&& retval
[0] != '\0')
571 /* We use C as the default domain. POSIX says this is implementation
577 /* @@ begin of epilog @@ */
579 /* We don't want libintl.a to depend on any other library. So we
580 avoid the non-standard function stpcpy. In GNU C Library this
581 function is available, though. Also allow the symbol HAVE_STPCPY
583 #if !_LIBC && !HAVE_STPCPY
589 while ((*dest
++ = *src
++) != '\0')