1 /* dcgettext.c -- implementation of the dcgettext(3) function
2 Copyright (C) 1995 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
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include <sys/types.h>
25 # define alloca __builtin_alloca
27 # if defined HAVE_ALLOCA_H || defined _LIBC
45 #if defined STDC_HEADERS || defined _LIBC
56 #if defined HAVE_STRING_H || defined _LIBC
61 #if !HAVE_STRCHR && !defined _LIBC
67 #if defined HAVE_UNISTD_H || defined _LIBC
76 # include "libgettext.h"
78 #include "hash-string.h"
80 /* @@ end of prolog @@ */
83 /* Rename the non ANSI C functions. This is required by the standard
84 because some ANSI C functions will require linking with this object
85 file and the name space must not be polluted. */
86 # define getcwd __getcwd
87 # define stpcpy __stpcpy
90 #if !defined HAVE_GETCWD && !defined _LIBC
92 # define getcwd(buf, max) getwd (buf)
97 /* Amount to increase buffer size by in each try. */
100 /* The following is from pathmax.h. */
101 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
102 PATH_MAX but might cause redefinition warnings when sys/param.h is
103 later included (as on MORE/BSD 4.3). */
104 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
108 #ifndef _POSIX_PATH_MAX
109 # define _POSIX_PATH_MAX 255
112 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
113 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
116 /* Don't include sys/param.h if it already has been. */
117 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
118 # include <sys/param.h>
121 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
122 # define PATH_MAX MAXPATHLEN
126 # define PATH_MAX _POSIX_PATH_MAX
129 /* XPG3 defines the result of `setlocale (category, NULL)' as:
130 ``Directs `setlocale()' to query `category' and return the current
131 setting of `local'.''
132 However it does not specify the exact format. And even worse: POSIX
133 defines this not at all. So we can use this feature only on selected
134 system (e.g. those using GNU C Library). */
136 # define HAVE_LOCALE_NULL
139 /* Name of the default domain used for gettext(3) prior any call to
140 textdomain(3). The default value for this is "messages". */
141 const char _nl_default_default_domain
[] = "messages";
143 /* Value used as the default domain for gettext(3). */
144 const char *_nl_current_default_domain
= _nl_default_default_domain
;
146 /* Contains the default location of the message catalogs. */
147 const char _nl_default_dirname
[] = GNULOCALEDIR
;
149 /* List with bindings of specific domains created by bindtextdomain()
151 struct binding
*_nl_domain_bindings
;
153 /* Prototypes for local functions. */
154 static char *find_msg
PARAMS ((struct loaded_domain
*domain
,
156 static const char *category_to_name
PARAMS ((int category
));
157 static const char *guess_category_value
PARAMS ((int category
,
158 const char *categoryname
));
161 /* Names for the libintl functions are a problem. They must not clash
162 with existing names and they should follow ANSI C. But this source
163 code is also used in GNU C Library where the names have a __
164 prefix. So we have to make a difference here. */
166 # define DCGETTEXT __dcgettext
168 # define DCGETTEXT dcgettext__
171 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
174 DCGETTEXT (domainname
, msgid
, category
)
175 const char *domainname
;
179 struct loaded_domain
*domain
;
180 struct binding
*binding
;
181 const char *categoryname
;
182 const char *categoryvalue
;
183 char *dirname
, *xdomainname
;
186 int saved_errno
= errno
;
188 /* If no real MSGID is given return NULL. */
192 /* If DOMAINNAME is NULL, we are interested in the default domain. If
193 CATEGORY is not LC_MESSAGES this might not make much sense but the
194 defintion left this undefined. */
195 if (domainname
== NULL
)
196 domainname
= _nl_current_default_domain
;
198 /* First find matching binding. */
199 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
201 int compare
= strcmp (domainname
, binding
->domainname
);
207 /* It is not in the list. */
214 dirname
= (char *) _nl_default_dirname
;
215 else if (binding
->dirname
[0] == '/')
216 dirname
= binding
->dirname
;
219 /* We have a relative path. Make it absolute now. */
220 size_t dirname_len
= strlen (binding
->dirname
) + 1;
224 path_max
= (unsigned) PATH_MAX
;
225 path_max
+= 2; /* The getcwd docs say to do this. */
227 dirname
= (char *) alloca (path_max
+ dirname_len
);
230 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
232 path_max
+= PATH_INCR
;
233 dirname
= (char *) alloca (path_max
+ dirname_len
);
239 /* We cannot get the current working directory. Don't signal an
240 error but simply return the default string. */
242 return (char *) msgid
;
245 /* We don't want libintl.a to depend on any other library. So
246 we avoid the non-standard function stpcpy. In GNU C Library
247 this function is available, though. Also allow the symbol
248 HAVE_STPCPY to be defined. */
249 #if defined _LIBC || defined HAVE_STPCPY
250 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
252 strcat (dirname
, "/");
253 strcat (dirname
, binding
->dirname
);
257 /* Now determine the symbolic name of CATEGORY and its value. */
258 categoryname
= category_to_name (category
);
259 categoryvalue
= guess_category_value (category
, categoryname
);
261 xdomainname
= (char *) alloca (strlen (categoryname
)
262 + strlen (domainname
) + 5);
263 /* We don't want libintl.a to depend on any other library. So we
264 avoid the non-standard function stpcpy. In GNU C Library this
265 function is available, though. Also allow the symbol HAVE_STPCPY
267 #if defined _LIBC || defined HAVE_STPCPY
268 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
272 strcpy (xdomainname
, categoryname
);
273 strcat (xdomainname
, "/");
274 strcat (xdomainname
, domainname
);
275 strcat (xdomainname
, ".mo");
278 /* Creating working area. */
279 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
282 /* Search for the given string. This is a loop because we perhaps
283 got an ordered list of languages to consider for th translation. */
286 /* Make CATEGORYVALUE point to the next element of the list. */
287 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
289 if (categoryvalue
[0] == '\0')
291 /* The whole contents of CATEGORYVALUE has been searched but
292 no valid entry has been found. We solve this situation
293 by implicitely appending a "C" entry, i.e. no translation
295 single_locale
[0] = 'C';
296 single_locale
[1] = '\0';
300 char *cp
= single_locale
;
301 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
302 *cp
++ = *categoryvalue
++;
306 /* If the current locale value is C (or POSIX) we don't load a
307 domain. Return the MSGID. */
308 if (strcmp (single_locale
, "C") == 0
309 || strcmp (single_locale
, "POSIX") == 0)
312 return (char *) msgid
;
316 /* Find structure describing the message catalog matching the
317 DOMAINNAME and CATEGORY. */
318 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
);
322 retval
= find_msg (domain
, msgid
);
328 for (cnt
= 0; domain
->successor
[cnt
] != NULL
; ++cnt
)
330 retval
= find_msg (domain
->successor
[cnt
], msgid
);
348 /* Alias for function name in GNU C Library. */
349 weak_alias (__dcgettext
, dcgettext
);
354 find_msg (domain
, msgid
)
355 struct loaded_domain
*domain
;
358 size_t top
, act
, bottom
;
360 if (domain
->decided
== 0)
361 _nl_load_domain (domain
);
363 if (domain
->data
== NULL
)
366 /* Locate the MSGID and its translation. */
367 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
369 /* Use the hashing table. */
370 nls_uint32 len
= strlen (msgid
);
371 nls_uint32 hash_val
= hash_string (msgid
);
372 nls_uint32 idx
= hash_val
% domain
->hash_size
;
373 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
374 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
377 /* Hash table entry is empty. */
380 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
382 domain
->data
+ W (domain
->must_swap
,
383 domain
->orig_tab
[nstr
- 1].offset
)) == 0)
384 return (char *) domain
->data
+ W (domain
->must_swap
,
385 domain
->trans_tab
[nstr
- 1].offset
);
389 if (idx
>= domain
->hash_size
- incr
)
390 idx
-= domain
->hash_size
- incr
;
394 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
396 /* Hash table entry is empty. */
399 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
401 domain
->data
+ W (domain
->must_swap
,
402 domain
->orig_tab
[nstr
- 1].offset
))
404 return (char *) domain
->data
405 + W (domain
->must_swap
, domain
->trans_tab
[nstr
- 1].offset
);
410 /* Now we try the default method: binary search in the sorted
411 array of messages. */
413 top
= domain
->nstrings
;
418 act
= (bottom
+ top
) / 2;
419 cmp_val
= strcmp (msgid
, domain
->data
420 + W (domain
->must_swap
,
421 domain
->orig_tab
[act
].offset
));
424 else if (cmp_val
> 0)
430 /* If an translation is found return this. */
431 return bottom
>= top
? NULL
: (char *) domain
->data
432 + W (domain
->must_swap
,
433 domain
->trans_tab
[act
].offset
);
437 /* Return string representation of locale CATEGORY. */
438 static const char *category_to_name (category
)
447 retval
= "LC_COLLATE";
457 retval
= "LC_MONETARY";
462 retval
= "LC_NUMERIC";
472 retval
= "LC_MESSAGES";
477 retval
= "LC_RESPONSE";
482 /* This might not make sense but is perhaps better than any other
488 /* If you have a better idea for a default value let me know. */
495 /* Guess value of current locale from value of the environment variables. */
496 static const char *guess_category_value (category
, categoryname
)
498 const char *categoryname
;
502 /* The highest priority value is the `LANGUAGE' environment
503 variable. This is a GNU extension. */
504 retval
= getenv ("LANGUAGE");
505 if (retval
!= NULL
&& retval
[0] != '\0')
508 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
509 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
510 systems this can be done by the `setlocale' function itself. */
511 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
512 return setlocale (category
, NULL
);
514 /* Setting of LC_ALL overwrites all other. */
515 retval
= getenv ("LC_ALL");
516 if (retval
!= NULL
&& retval
[0] != '\0')
519 /* Next comes the name of the desired category. */
520 retval
= getenv (categoryname
);
521 if (retval
!= NULL
&& retval
[0] != '\0')
524 /* Last possibility is the LANG environment variable. */
525 retval
= getenv ("LANG");
526 if (retval
!= NULL
&& retval
[0] != '\0')
529 /* We use C as the default domain. POSIX says this is implementation