1 /* dcgettext.c -- implemenatation of the dcgettext(3) function
2 Copyright (C) 1995 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 # ifdef 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
__P ((struct loaded_domain
*domain
, const char *msgid
));
155 static const char *category_to_name
__P((int category
));
156 static const char *guess_category_value
__P((int category
,
157 const char *categoryname
));
160 /* Names for the libintl functions are a problem. They must not clash
161 with existing names and they should follow ANSI C. But this source
162 code is also used in GNU C Library where the names have a __
163 prefix. So we have to make a difference here. */
165 # define DCGETTEXT __dcgettext
167 # define DCGETTEXT dcgettext__
170 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
173 DCGETTEXT (domainname
, msgid
, category
)
174 const char *domainname
;
178 struct loaded_domain
*domain
;
179 struct binding
*binding
;
180 const char *categoryname
;
181 const char *categoryvalue
;
182 char *dirname
, *xdomainname
;
186 /* If no real MSGID is given return NULL. */
190 /* If DOMAINNAME is NULL, we are interested in the default domain. If
191 CATEGORY is not LC_MESSAGES this might not make much sense but the
192 defintion left this undefined. */
193 if (domainname
== NULL
)
194 domainname
= _nl_current_default_domain
;
196 /* First find matching binding. */
197 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
199 int compare
= strcmp (domainname
, binding
->domainname
);
205 /* It is not in the list. */
212 dirname
= (char *) _nl_default_dirname
;
213 else if (binding
->dirname
[0] == '/')
214 dirname
= binding
->dirname
;
217 /* We have a relative path. Make it absolute now. */
218 size_t dirname_len
= strlen (binding
->dirname
) + 1;
222 path_max
= (unsigned) PATH_MAX
;
223 path_max
+= 2; /* The getcwd docs say to do this. */
225 dirname
= (char *) alloca (path_max
+ dirname_len
);
228 while ((ret
= getcwd (dirname
, path_max
)) == NULL
&& errno
== ERANGE
)
230 path_max
+= PATH_INCR
;
231 dirname
= (char *) alloca (path_max
+ dirname_len
);
236 /* We cannot get the current working directory. Don't signal an
237 error but simply return the default string. */
238 return (char *) msgid
;
240 /* We don't want libintl.a to depend on any other library. So
241 we avoid the non-standard function stpcpy. In GNU C Library
242 this function is available, though. Also allow the symbol
243 HAVE_STPCPY to be defined. */
244 #if defined _LIBC || defined HAVE_STPCPY
245 stpcpy (stpcpy (strchr (dirname
, '\0'), "/"), binding
->dirname
);
247 strcat (dirname
, "/");
248 strcat (dirname
, binding
->dirname
);
252 /* Now determine the symbolic name of CATEGORY and its value. */
253 categoryname
= category_to_name (category
);
254 categoryvalue
= guess_category_value (category
, categoryname
);
256 xdomainname
= (char *) alloca (strlen (categoryname
)
257 + strlen (domainname
) + 5);
258 /* We don't want libintl.a to depend on any other library. So we
259 avoid the non-standard function stpcpy. In GNU C Library this
260 function is available, though. Also allow the symbol HAVE_STPCPY
262 #if defined _LIBC || defined HAVE_STPCPY
263 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname
, categoryname
), "/"),
267 strcpy (xdomainname
, categoryname
);
268 strcat (xdomainname
, "/");
269 strcat (xdomainname
, domainname
);
270 strcat (xdomainname
, ".mo");
273 /* Creating working area. */
274 single_locale
= (char *) alloca (strlen (categoryvalue
) + 1);
277 /* Search for the given string. This is a loop because we perhaps
278 got an ordered list of languages to consider for th translation. */
281 /* Make CATEGORYVALUE point to the next element of the list. */
282 while (categoryvalue
[0] != '\0' && categoryvalue
[0] == ':')
284 if (categoryvalue
[0] == '\0')
286 /* The whole contents of CATEGORYVALUE has been searched but
287 no valid entry has been found. We solve this situation
288 by implicitely appending a "C" entry, i.e. no translation
290 single_locale
[0] = 'C';
291 single_locale
[1] = '\0';
295 char *cp
= single_locale
;
296 while (categoryvalue
[0] != '\0' && categoryvalue
[0] != ':')
297 *cp
++ = *categoryvalue
++;
301 /* If the current locale value is C (or POSIX) we don't load a
302 domain. Return the MSGID. */
303 if (strcmp (single_locale
, "C") == 0
304 || strcmp (single_locale
, "POSIX") == 0)
305 return (char *) msgid
;
308 /* Find structure describing the message catalog matching the
309 DOMAINNAME and CATEGORY. */
310 domain
= _nl_find_domain (dirname
, single_locale
, xdomainname
);
314 retval
= find_msg (domain
, msgid
);
320 for (cnt
= 6; cnt
>= 0 && retval
== NULL
; --cnt
)
321 if (domain
->successor
[cnt
] != NULL
)
323 retval
= find_msg (domain
->successor
[cnt
], msgid
);
325 if (domain
->successor
[cnt
]->data
== NULL
)
326 domain
->successor
[cnt
] = NULL
;
338 /* Alias for function name in GNU C Library. */
339 weak_alias (__dcgettext
, dcgettext
);
344 find_msg (domain
, msgid
)
345 struct loaded_domain
*domain
;
348 size_t top
, act
, bottom
;
350 if (domain
->decided
== 0)
351 _nl_load_domain (domain
);
353 if (domain
->data
== NULL
)
356 /* Locate the MSGID and its translation. */
357 if (domain
->hash_size
> 2 && domain
->hash_tab
!= NULL
)
359 /* Use the hashing table. */
360 nls_uint32 len
= strlen (msgid
);
361 nls_uint32 hash_val
= hash_string (msgid
);
362 nls_uint32 idx
= hash_val
% domain
->hash_size
;
363 nls_uint32 incr
= 1 + (hash_val
% (domain
->hash_size
- 2));
364 nls_uint32 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
367 /* Hash table entry is empty. */
370 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
372 domain
->data
+ W (domain
->must_swap
,
373 domain
->orig_tab
[nstr
- 1].offset
)) == 0)
374 return (char *) domain
->data
+ W (domain
->must_swap
,
375 domain
->trans_tab
[nstr
- 1].offset
);
379 if (idx
>= W (domain
->must_swap
, domain
->hash_size
) - incr
)
380 idx
-= W (domain
->must_swap
, domain
->hash_size
) - incr
;
384 nstr
= W (domain
->must_swap
, domain
->hash_tab
[idx
]);
386 /* Hash table entry is empty. */
389 if (W (domain
->must_swap
, domain
->orig_tab
[nstr
- 1].length
) == len
391 domain
->data
+ W (domain
->must_swap
,
392 domain
->orig_tab
[nstr
- 1].offset
))
394 return (char *) domain
->data
395 + W (domain
->must_swap
, domain
->trans_tab
[nstr
- 1].offset
);
400 /* Now we try the default method: binary search in the sorted
401 array of messages. */
403 top
= domain
->nstrings
;
408 act
= (bottom
+ top
) / 2;
409 cmp_val
= strcmp (msgid
, domain
->data
410 + W (domain
->must_swap
,
411 domain
->orig_tab
[act
].offset
));
414 else if (cmp_val
> 0)
420 /* If an translation is found return this. */
421 return bottom
>= top
? NULL
: (char *) domain
->data
422 + W (domain
->must_swap
,
423 domain
->trans_tab
[act
].offset
);
427 /* Return string representation of locale CATEGORY. */
428 static const char *category_to_name (category
)
437 retval
= "LC_COLLATE";
447 retval
= "LC_MONETARY";
452 retval
= "LC_NUMERIC";
462 retval
= "LC_MESSAGES";
467 retval
= "LC_RESPONSE";
472 /* This might not make sense but is perhaps better than any other
478 /* If you have a better idea for a default value let me know. */
485 /* Guess value of current locale from value of the environment variables. */
486 static const char *guess_category_value (category
, categoryname
)
488 const char *categoryname
;
492 /* The highest priority value is the `LANGUAGE' environment
493 variable. This is a GNU extension. */
494 retval
= getenv ("LANGUAGE");
495 if (retval
!= NULL
&& retval
[0] != '\0')
498 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
499 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
500 systems this can be done by the `setlocale' function itself. */
501 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
502 return setlocale (category
, NULL
);
504 /* Setting of LC_ALL overwrites all other. */
505 retval
= getenv ("LC_ALL");
506 if (retval
!= NULL
&& retval
[0] != '\0')
509 /* Next comes the name of the desired category. */
510 retval
= getenv (categoryname
);
511 if (retval
!= NULL
&& retval
[0] != '\0')
514 /* Last possibility is the LANG environment variable. */
515 retval
= getenv ("LANG");
516 if (retval
!= NULL
&& retval
[0] != '\0')
519 /* We use C as the default domain. POSIX says this is implementation