Mon Mar 18 22:54:32 1996 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[glibc.git] / intl / dcgettext.c
blob7562f69ab490b36da65d8473508771f111452929
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)
7 any later version.
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. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <sys/types.h>
24 #ifdef __GNUC__
25 # define alloca __builtin_alloca
26 #else
27 # if defined HAVE_ALLOCA_H || defined _LIBC
28 # include <alloca.h>
29 # else
30 # ifdef _AIX
31 #pragma alloca
32 # else
33 # ifndef alloca
34 char *alloca ();
35 # endif
36 # endif
37 # endif
38 #endif
40 #include <errno.h>
41 #ifndef errno
42 extern int errno;
43 #endif
45 #if defined STDC_HEADERS || defined _LIBC
46 # include <stdlib.h>
47 #else
48 char *getenv ();
49 # ifdef HAVE_MALLOC_H
50 # include <malloc.h>
51 # else
52 void free ();
53 # endif
54 #endif
56 #if defined HAVE_STRING_H || defined _LIBC
57 # include <string.h>
58 #else
59 # include <strings.h>
60 #endif
61 #if !HAVE_STRCHR && !defined _LIBC
62 # ifndef strchr
63 # define strchr index
64 # endif
65 #endif
67 #if defined HAVE_UNISTD_H || defined _LIBC
68 # include <unistd.h>
69 #endif
71 #include "gettext.h"
72 #include "gettextP.h"
73 #ifdef _LIBC
74 # include <libintl.h>
75 #else
76 # include "libgettext.h"
77 #endif
78 #include "hash-string.h"
80 /* @@ end of prolog @@ */
82 #ifdef _LIBC
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
88 #endif
90 #if !defined HAVE_GETCWD && !defined _LIBC
91 char *getwd ();
92 # define getcwd(buf, max) getwd (buf)
93 #else
94 char *getcwd ();
95 #endif
97 /* Amount to increase buffer size by in each try. */
98 #define PATH_INCR 32
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__))
105 # include <limits.h>
106 #endif
108 #ifndef _POSIX_PATH_MAX
109 # define _POSIX_PATH_MAX 255
110 #endif
112 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
113 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
114 #endif
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>
119 #endif
121 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
122 # define PATH_MAX MAXPATHLEN
123 #endif
125 #ifndef PATH_MAX
126 # define PATH_MAX _POSIX_PATH_MAX
127 #endif
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). */
135 #ifdef _LIBC
136 # define HAVE_LOCALE_NULL
137 #endif
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()
150 calls. */
151 struct binding *_nl_domain_bindings;
153 /* Prototypes for local functions. */
154 static char *find_msg PARAMS ((struct loaded_domain *domain,
155 const char *msgid));
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. */
165 #ifdef _LIBC
166 # define DCGETTEXT __dcgettext
167 #else
168 # define DCGETTEXT dcgettext__
169 #endif
171 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
172 locale. */
173 char *
174 DCGETTEXT (domainname, msgid, category)
175 const char *domainname;
176 const char *msgid;
177 int category;
179 struct loaded_domain *domain;
180 struct binding *binding;
181 const char *categoryname;
182 const char *categoryvalue;
183 char *dirname, *xdomainname;
184 char *single_locale;
185 char *retval;
186 int saved_errno = errno;
188 /* If no real MSGID is given return NULL. */
189 if (msgid == NULL)
190 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);
202 if (compare == 0)
203 /* We found it! */
204 break;
205 if (compare < 0)
207 /* It is not in the list. */
208 binding = NULL;
209 break;
213 if (binding == NULL)
214 dirname = (char *) _nl_default_dirname;
215 else if (binding->dirname[0] == '/')
216 dirname = binding->dirname;
217 else
219 /* We have a relative path. Make it absolute now. */
220 size_t dirname_len = strlen (binding->dirname) + 1;
221 size_t path_max;
222 char *ret;
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);
229 errno = 0;
230 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
232 path_max += PATH_INCR;
233 dirname = (char *) alloca (path_max + dirname_len);
234 errno = 0;
237 if (ret == NULL)
239 /* We cannot get the current working directory. Don't signal an
240 error but simply return the default string. */
241 errno = saved_errno;
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);
251 #else
252 strcat (dirname, "/");
253 strcat (dirname, binding->dirname);
254 #endif
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
266 to be defined. */
267 #if defined _LIBC || defined HAVE_STPCPY
268 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
269 domainname),
270 ".mo");
271 #else
272 strcpy (xdomainname, categoryname);
273 strcat (xdomainname, "/");
274 strcat (xdomainname, domainname);
275 strcat (xdomainname, ".mo");
276 #endif
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. */
284 while (1)
286 /* Make CATEGORYVALUE point to the next element of the list. */
287 while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
288 ++categoryvalue;
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
294 will take place. */
295 single_locale[0] = 'C';
296 single_locale[1] = '\0';
298 else
300 char *cp = single_locale;
301 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
302 *cp++ = *categoryvalue++;
303 *cp = '\0';
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)
311 errno = saved_errno;
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);
320 if (domain != NULL)
322 retval = find_msg (domain, msgid);
324 if (retval == NULL)
326 int cnt;
328 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
330 retval = find_msg (domain->successor[cnt], msgid);
332 if (retval != NULL)
333 break;
337 if (retval != NULL)
339 errno = saved_errno;
340 return retval;
344 /* NOTREACHED */
347 #ifdef _LIBC
348 /* Alias for function name in GNU C Library. */
349 weak_alias (__dcgettext, dcgettext);
350 #endif
353 static char *
354 find_msg (domain, msgid)
355 struct loaded_domain *domain;
356 const char *msgid;
358 size_t top, act, bottom;
360 if (domain->decided == 0)
361 _nl_load_domain (domain);
363 if (domain->data == NULL)
364 return 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]);
376 if (nstr == 0)
377 /* Hash table entry is empty. */
378 return NULL;
380 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
381 && strcmp (msgid,
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);
387 while (1)
389 if (idx >= domain->hash_size - incr)
390 idx -= domain->hash_size - incr;
391 else
392 idx += incr;
394 nstr = W (domain->must_swap, domain->hash_tab[idx]);
395 if (nstr == 0)
396 /* Hash table entry is empty. */
397 return NULL;
399 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
400 && strcmp (msgid,
401 domain->data + W (domain->must_swap,
402 domain->orig_tab[nstr - 1].offset))
403 == 0)
404 return (char *) domain->data
405 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
407 /* NOTREACHED */
410 /* Now we try the default method: binary search in the sorted
411 array of messages. */
412 bottom = 0;
413 top = domain->nstrings;
414 while (bottom < top)
416 int cmp_val;
418 act = (bottom + top) / 2;
419 cmp_val = strcmp (msgid, domain->data
420 + W (domain->must_swap,
421 domain->orig_tab[act].offset));
422 if (cmp_val < 0)
423 top = act;
424 else if (cmp_val > 0)
425 bottom = act + 1;
426 else
427 break;
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)
439 int category;
441 const char *retval;
443 switch (category)
445 #ifdef LC_COLLATE
446 case LC_COLLATE:
447 retval = "LC_COLLATE";
448 break;
449 #endif
450 #ifdef LC_CTYPE
451 case LC_CTYPE:
452 retval = "LC_CTYPE";
453 break;
454 #endif
455 #ifdef LC_MONETARY
456 case LC_MONETARY:
457 retval = "LC_MONETARY";
458 break;
459 #endif
460 #ifdef LC_NUMERIC
461 case LC_NUMERIC:
462 retval = "LC_NUMERIC";
463 break;
464 #endif
465 #ifdef LC_TIME
466 case LC_TIME:
467 retval = "LC_TIME";
468 break;
469 #endif
470 #ifdef LC_MESSAGES
471 case LC_MESSAGES:
472 retval = "LC_MESSAGES";
473 break;
474 #endif
475 #ifdef LC_RESPONSE
476 case LC_RESPONSE:
477 retval = "LC_RESPONSE";
478 break;
479 #endif
480 #ifdef LC_ALL
481 case LC_ALL:
482 /* This might not make sense but is perhaps better than any other
483 value. */
484 retval = "LC_ALL";
485 break;
486 #endif
487 default:
488 /* If you have a better idea for a default value let me know. */
489 retval = "LC_XXX";
492 return retval;
495 /* Guess value of current locale from value of the environment variables. */
496 static const char *guess_category_value (category, categoryname)
497 int category;
498 const char *categoryname;
500 const char *retval;
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')
506 return retval;
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);
513 #else
514 /* Setting of LC_ALL overwrites all other. */
515 retval = getenv ("LC_ALL");
516 if (retval != NULL && retval[0] != '\0')
517 return retval;
519 /* Next comes the name of the desired category. */
520 retval = getenv (categoryname);
521 if (retval != NULL && retval[0] != '\0')
522 return retval;
524 /* Last possibility is the LANG environment variable. */
525 retval = getenv ("LANG");
526 if (retval != NULL && retval[0] != '\0')
527 return retval;
529 /* We use C as the default domain. POSIX says this is implementation
530 defined. */
531 return "C";
532 #endif