Thu May 23 00:01:10 1996 Ulrich Drepper <drepper@cygnus.com>
[glibc/history.git] / intl / dcgettext.c
bloba18be16631ace6b1032cf86a7415154f3f2882f0
1 /* dcgettext.c -- implementation of the dcgettext(3) function
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <sys/types.h>
26 #ifdef __GNUC__
27 # define alloca __builtin_alloca
28 #else
29 # if defined HAVE_ALLOCA_H || defined _LIBC
30 # include <alloca.h>
31 # else
32 # ifdef _AIX
33 #pragma alloca
34 # else
35 # ifndef alloca
36 char *alloca ();
37 # endif
38 # endif
39 # endif
40 #endif
42 #include <errno.h>
43 #ifndef errno
44 extern int errno;
45 #endif
47 #if defined STDC_HEADERS || defined _LIBC
48 # include <stdlib.h>
49 #else
50 char *getenv ();
51 # ifdef HAVE_MALLOC_H
52 # include <malloc.h>
53 # else
54 void free ();
55 # endif
56 #endif
58 #if defined HAVE_STRING_H || defined _LIBC
59 # include <string.h>
60 #else
61 # include <strings.h>
62 #endif
63 #if !HAVE_STRCHR && !defined _LIBC
64 # ifndef strchr
65 # define strchr index
66 # endif
67 #endif
69 #if defined HAVE_UNISTD_H || defined _LIBC
70 # include <unistd.h>
71 #endif
73 #include "gettext.h"
74 #include "gettextP.h"
75 #ifdef _LIBC
76 # include <libintl.h>
77 #else
78 # include "libgettext.h"
79 #endif
80 #include "hash-string.h"
82 /* @@ end of prolog @@ */
84 #ifdef _LIBC
85 /* Rename the non ANSI C functions. This is required by the standard
86 because some ANSI C functions will require linking with this object
87 file and the name space must not be polluted. */
88 # define getcwd __getcwd
89 # define stpcpy __stpcpy
90 #else
91 # if !defined HAVE_GETCWD
92 char *getwd ();
93 # define getcwd(buf, max) getwd (buf)
94 # else
95 char *getcwd ();
96 # endif
97 #endif
99 /* Amount to increase buffer size by in each try. */
100 #define PATH_INCR 32
102 /* The following is from pathmax.h. */
103 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
104 PATH_MAX but might cause redefinition warnings when sys/param.h is
105 later included (as on MORE/BSD 4.3). */
106 #if defined(_POSIX_VERSION) || (defined(HAVE_LIMITS_H) && !defined(__GNUC__))
107 # include <limits.h>
108 #endif
110 #ifndef _POSIX_PATH_MAX
111 # define _POSIX_PATH_MAX 255
112 #endif
114 #if !defined(PATH_MAX) && defined(_PC_PATH_MAX)
115 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
116 #endif
118 /* Don't include sys/param.h if it already has been. */
119 #if defined(HAVE_SYS_PARAM_H) && !defined(PATH_MAX) && !defined(MAXPATHLEN)
120 # include <sys/param.h>
121 #endif
123 #if !defined(PATH_MAX) && defined(MAXPATHLEN)
124 # define PATH_MAX MAXPATHLEN
125 #endif
127 #ifndef PATH_MAX
128 # define PATH_MAX _POSIX_PATH_MAX
129 #endif
131 /* XPG3 defines the result of `setlocale (category, NULL)' as:
132 ``Directs `setlocale()' to query `category' and return the current
133 setting of `local'.''
134 However it does not specify the exact format. And even worse: POSIX
135 defines this not at all. So we can use this feature only on selected
136 system (e.g. those using GNU C Library). */
137 #ifdef _LIBC
138 # define HAVE_LOCALE_NULL
139 #endif
141 /* Name of the default domain used for gettext(3) prior any call to
142 textdomain(3). The default value for this is "messages". */
143 const char _nl_default_default_domain[] = "messages";
145 /* Value used as the default domain for gettext(3). */
146 const char *_nl_current_default_domain = _nl_default_default_domain;
148 /* Contains the default location of the message catalogs. */
149 const char _nl_default_dirname[] = GNULOCALEDIR;
151 /* List with bindings of specific domains created by bindtextdomain()
152 calls. */
153 struct binding *_nl_domain_bindings;
155 /* Prototypes for local functions. */
156 static char *find_msg PARAMS ((struct loaded_l10nfile *domain_file,
157 const char *msgid));
158 static const char *category_to_name PARAMS ((int category));
159 static const char *guess_category_value PARAMS ((int category,
160 const char *categoryname));
163 /* Names for the libintl functions are a problem. They must not clash
164 with existing names and they should follow ANSI C. But this source
165 code is also used in GNU C Library where the names have a __
166 prefix. So we have to make a difference here. */
167 #ifdef _LIBC
168 # define DCGETTEXT __dcgettext
169 #else
170 # define DCGETTEXT dcgettext__
171 #endif
173 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
174 locale. */
175 char *
176 DCGETTEXT (domainname, msgid, category)
177 const char *domainname;
178 const char *msgid;
179 int category;
181 struct loaded_l10nfile *domain;
182 struct binding *binding;
183 const char *categoryname;
184 const char *categoryvalue;
185 char *dirname, *xdomainname;
186 char *single_locale;
187 char *retval;
188 int saved_errno = errno;
190 /* If no real MSGID is given return NULL. */
191 if (msgid == NULL)
192 return NULL;
194 /* If DOMAINNAME is NULL, we are interested in the default domain. If
195 CATEGORY is not LC_MESSAGES this might not make much sense but the
196 defintion left this undefined. */
197 if (domainname == NULL)
198 domainname = _nl_current_default_domain;
200 /* First find matching binding. */
201 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
203 int compare = strcmp (domainname, binding->domainname);
204 if (compare == 0)
205 /* We found it! */
206 break;
207 if (compare < 0)
209 /* It is not in the list. */
210 binding = NULL;
211 break;
215 if (binding == NULL)
216 dirname = (char *) _nl_default_dirname;
217 else if (binding->dirname[0] == '/')
218 dirname = binding->dirname;
219 else
221 /* We have a relative path. Make it absolute now. */
222 size_t dirname_len = strlen (binding->dirname) + 1;
223 size_t path_max;
224 char *ret;
226 path_max = (unsigned) PATH_MAX;
227 path_max += 2; /* The getcwd docs say to do this. */
229 dirname = (char *) alloca (path_max + dirname_len);
231 errno = 0;
232 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
234 path_max += PATH_INCR;
235 dirname = (char *) alloca (path_max + dirname_len);
236 errno = 0;
239 if (ret == NULL)
241 /* We cannot get the current working directory. Don't signal an
242 error but simply return the default string. */
243 errno = saved_errno;
244 return (char *) msgid;
247 /* We don't want libintl.a to depend on any other library. So
248 we avoid the non-standard function stpcpy. In GNU C Library
249 this function is available, though. Also allow the symbol
250 HAVE_STPCPY to be defined. */
251 #if defined _LIBC || defined HAVE_STPCPY
252 stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
253 #else
254 strcat (dirname, "/");
255 strcat (dirname, binding->dirname);
256 #endif
259 /* Now determine the symbolic name of CATEGORY and its value. */
260 categoryname = category_to_name (category);
261 categoryvalue = guess_category_value (category, categoryname);
263 xdomainname = (char *) alloca (strlen (categoryname)
264 + strlen (domainname) + 5);
265 /* We don't want libintl.a to depend on any other library. So we
266 avoid the non-standard function stpcpy. In GNU C Library this
267 function is available, though. Also allow the symbol HAVE_STPCPY
268 to be defined. */
269 #if defined _LIBC || defined HAVE_STPCPY
270 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
271 domainname),
272 ".mo");
273 #else
274 strcpy (xdomainname, categoryname);
275 strcat (xdomainname, "/");
276 strcat (xdomainname, domainname);
277 strcat (xdomainname, ".mo");
278 #endif
280 /* Creating working area. */
281 single_locale = (char *) alloca (strlen (categoryvalue) + 1);
284 /* Search for the given string. This is a loop because we perhaps
285 got an ordered list of languages to consider for th translation. */
286 while (1)
288 /* Make CATEGORYVALUE point to the next element of the list. */
289 while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
290 ++categoryvalue;
291 if (categoryvalue[0] == '\0')
293 /* The whole contents of CATEGORYVALUE has been searched but
294 no valid entry has been found. We solve this situation
295 by implicitely appending a "C" entry, i.e. no translation
296 will take place. */
297 single_locale[0] = 'C';
298 single_locale[1] = '\0';
300 else
302 char *cp = single_locale;
303 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
304 *cp++ = *categoryvalue++;
305 *cp = '\0';
308 /* If the current locale value is C (or POSIX) we don't load a
309 domain. Return the MSGID. */
310 if (strcmp (single_locale, "C") == 0
311 || strcmp (single_locale, "POSIX") == 0)
313 errno = saved_errno;
314 return (char *) msgid;
318 /* Find structure describing the message catalog matching the
319 DOMAINNAME and CATEGORY. */
320 domain = _nl_find_domain (dirname, single_locale, xdomainname);
322 if (domain != NULL)
324 retval = find_msg (domain, msgid);
326 if (retval == NULL)
328 int cnt;
330 for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
332 retval = find_msg (domain->successor[cnt], msgid);
334 if (retval != NULL)
335 break;
339 if (retval != NULL)
341 errno = saved_errno;
342 return retval;
346 /* NOTREACHED */
349 #ifdef _LIBC
350 /* Alias for function name in GNU C Library. */
351 weak_alias (__dcgettext, dcgettext);
352 #endif
355 static char *
356 find_msg (domain_file, msgid)
357 struct loaded_l10nfile *domain_file;
358 const char *msgid;
360 size_t top, act, bottom;
361 struct loaded_domain *domain;
363 if (domain_file->decided == 0)
364 _nl_load_domain (domain_file);
366 if (domain_file->data == NULL)
367 return NULL;
369 domain = (struct loaded_domain *) domain_file->data;
371 /* Locate the MSGID and its translation. */
372 if (domain->hash_size > 2 && domain->hash_tab != NULL)
374 /* Use the hashing table. */
375 nls_uint32 len = strlen (msgid);
376 nls_uint32 hash_val = hash_string (msgid);
377 nls_uint32 idx = hash_val % domain->hash_size;
378 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
379 nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
381 if (nstr == 0)
382 /* Hash table entry is empty. */
383 return NULL;
385 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
386 && strcmp (msgid,
387 domain->data + W (domain->must_swap,
388 domain->orig_tab[nstr - 1].offset)) == 0)
389 return (char *) domain->data + W (domain->must_swap,
390 domain->trans_tab[nstr - 1].offset);
392 while (1)
394 if (idx >= domain->hash_size - incr)
395 idx -= domain->hash_size - incr;
396 else
397 idx += incr;
399 nstr = W (domain->must_swap, domain->hash_tab[idx]);
400 if (nstr == 0)
401 /* Hash table entry is empty. */
402 return NULL;
404 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
405 && strcmp (msgid,
406 domain->data + W (domain->must_swap,
407 domain->orig_tab[nstr - 1].offset))
408 == 0)
409 return (char *) domain->data
410 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
412 /* NOTREACHED */
415 /* Now we try the default method: binary search in the sorted
416 array of messages. */
417 bottom = 0;
418 top = domain->nstrings;
419 while (bottom < top)
421 int cmp_val;
423 act = (bottom + top) / 2;
424 cmp_val = strcmp (msgid, domain->data
425 + W (domain->must_swap,
426 domain->orig_tab[act].offset));
427 if (cmp_val < 0)
428 top = act;
429 else if (cmp_val > 0)
430 bottom = act + 1;
431 else
432 break;
435 /* If an translation is found return this. */
436 return bottom >= top ? NULL : (char *) domain->data
437 + W (domain->must_swap,
438 domain->trans_tab[act].offset);
442 /* Return string representation of locale CATEGORY. */
443 static const char *
444 category_to_name (category)
445 int category;
447 const char *retval;
449 switch (category)
451 #ifdef LC_COLLATE
452 case LC_COLLATE:
453 retval = "LC_COLLATE";
454 break;
455 #endif
456 #ifdef LC_CTYPE
457 case LC_CTYPE:
458 retval = "LC_CTYPE";
459 break;
460 #endif
461 #ifdef LC_MONETARY
462 case LC_MONETARY:
463 retval = "LC_MONETARY";
464 break;
465 #endif
466 #ifdef LC_NUMERIC
467 case LC_NUMERIC:
468 retval = "LC_NUMERIC";
469 break;
470 #endif
471 #ifdef LC_TIME
472 case LC_TIME:
473 retval = "LC_TIME";
474 break;
475 #endif
476 #ifdef LC_MESSAGES
477 case LC_MESSAGES:
478 retval = "LC_MESSAGES";
479 break;
480 #endif
481 #ifdef LC_RESPONSE
482 case LC_RESPONSE:
483 retval = "LC_RESPONSE";
484 break;
485 #endif
486 #ifdef LC_ALL
487 case LC_ALL:
488 /* This might not make sense but is perhaps better than any other
489 value. */
490 retval = "LC_ALL";
491 break;
492 #endif
493 default:
494 /* If you have a better idea for a default value let me know. */
495 retval = "LC_XXX";
498 return retval;
501 /* Guess value of current locale from value of the environment variables. */
502 static const char *guess_category_value (category, categoryname)
503 int category;
504 const char *categoryname;
506 const char *retval;
508 /* The highest priority value is the `LANGUAGE' environment
509 variable. This is a GNU extension. */
510 retval = getenv ("LANGUAGE");
511 if (retval != NULL && retval[0] != '\0')
512 return retval;
514 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
515 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
516 systems this can be done by the `setlocale' function itself. */
517 #if defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL
518 return setlocale (category, NULL);
519 #else
520 /* Setting of LC_ALL overwrites all other. */
521 retval = getenv ("LC_ALL");
522 if (retval != NULL && retval[0] != '\0')
523 return retval;
525 /* Next comes the name of the desired category. */
526 retval = getenv (categoryname);
527 if (retval != NULL && retval[0] != '\0')
528 return retval;
530 /* Last possibility is the LANG environment variable. */
531 retval = getenv ("LANG");
532 if (retval != NULL && retval[0] != '\0')
533 return retval;
535 /* We use C as the default domain. POSIX says this is implementation
536 defined. */
537 return "C";
538 #endif