Regenerated: autoconf -m /gd/gnu/autoconf sysdeps/unix/configure.in
[glibc/history.git] / intl / dcgettext.c
blob91025be89ba00e336483f593164653e81d74ec96
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)
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 # ifdef 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 __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. */
164 #ifdef _LIBC
165 # define DCGETTEXT __dcgettext
166 #else
167 # define DCGETTEXT dcgettext__
168 #endif
170 /* Look up MSGID in the DOMAINNAME message catalog for the current CATEGORY
171 locale. */
172 char *
173 DCGETTEXT (domainname, msgid, category)
174 const char *domainname;
175 const char *msgid;
176 int category;
178 struct loaded_domain *domain;
179 struct binding *binding;
180 const char *categoryname;
181 const char *categoryvalue;
182 char *dirname, *xdomainname;
183 char *single_locale;
184 char *retval;
186 /* If no real MSGID is given return NULL. */
187 if (msgid == NULL)
188 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);
200 if (compare == 0)
201 /* We found it! */
202 break;
203 if (compare < 0)
205 /* It is not in the list. */
206 binding = NULL;
207 break;
211 if (binding == NULL)
212 dirname = (char *) _nl_default_dirname;
213 else if (binding->dirname[0] == '/')
214 dirname = binding->dirname;
215 else
217 /* We have a relative path. Make it absolute now. */
218 size_t dirname_len = strlen (binding->dirname) + 1;
219 size_t path_max;
220 char *ret;
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);
227 errno = 0;
228 while ((ret = getcwd (dirname, path_max)) == NULL && errno == ERANGE)
230 path_max += PATH_INCR;
231 dirname = (char *) alloca (path_max + dirname_len);
232 errno = 0;
235 if (ret == NULL)
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);
246 #else
247 strcat (dirname, "/");
248 strcat (dirname, binding->dirname);
249 #endif
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
261 to be defined. */
262 #if defined _LIBC || defined HAVE_STPCPY
263 stpcpy (stpcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
264 domainname),
265 ".mo");
266 #else
267 strcpy (xdomainname, categoryname);
268 strcat (xdomainname, "/");
269 strcat (xdomainname, domainname);
270 strcat (xdomainname, ".mo");
271 #endif
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. */
279 while (1)
281 /* Make CATEGORYVALUE point to the next element of the list. */
282 while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
283 ++categoryvalue;
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
289 will take place. */
290 single_locale[0] = 'C';
291 single_locale[1] = '\0';
293 else
295 char *cp = single_locale;
296 while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
297 *cp++ = *categoryvalue++;
298 *cp = '\0';
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);
312 if (domain != NULL)
314 retval = find_msg (domain, msgid);
316 if (retval == NULL)
318 int cnt;
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;
330 if (retval != NULL)
331 return retval;
334 /* NOTREACHED */
337 #ifdef _LIBC
338 /* Alias for function name in GNU C Library. */
339 weak_alias (__dcgettext, dcgettext);
340 #endif
343 static char *
344 find_msg (domain, msgid)
345 struct loaded_domain *domain;
346 const char *msgid;
348 size_t top, act, bottom;
350 if (domain->decided == 0)
351 _nl_load_domain (domain);
353 if (domain->data == NULL)
354 return 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]);
366 if (nstr == 0)
367 /* Hash table entry is empty. */
368 return NULL;
370 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
371 && strcmp (msgid,
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);
377 while (1)
379 if (idx >= W (domain->must_swap, domain->hash_size) - incr)
380 idx -= W (domain->must_swap, domain->hash_size) - incr;
381 else
382 idx += incr;
384 nstr = W (domain->must_swap, domain->hash_tab[idx]);
385 if (nstr == 0)
386 /* Hash table entry is empty. */
387 return NULL;
389 if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) == len
390 && strcmp (msgid,
391 domain->data + W (domain->must_swap,
392 domain->orig_tab[nstr - 1].offset))
393 == 0)
394 return (char *) domain->data
395 + W (domain->must_swap, domain->trans_tab[nstr - 1].offset);
397 /* NOTREACHED */
400 /* Now we try the default method: binary search in the sorted
401 array of messages. */
402 bottom = 0;
403 top = domain->nstrings;
404 while (bottom < top)
406 int cmp_val;
408 act = (bottom + top) / 2;
409 cmp_val = strcmp (msgid, domain->data
410 + W (domain->must_swap,
411 domain->orig_tab[act].offset));
412 if (cmp_val < 0)
413 top = act;
414 else if (cmp_val > 0)
415 bottom = act + 1;
416 else
417 break;
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)
429 int category;
431 const char *retval;
433 switch (category)
435 #ifdef LC_COLLATE
436 case LC_COLLATE:
437 retval = "LC_COLLATE";
438 break;
439 #endif
440 #ifdef LC_CTYPE
441 case LC_CTYPE:
442 retval = "LC_CTYPE";
443 break;
444 #endif
445 #ifdef LC_MONETARY
446 case LC_MONETARY:
447 retval = "LC_MONETARY";
448 break;
449 #endif
450 #ifdef LC_NUMERIC
451 case LC_NUMERIC:
452 retval = "LC_NUMERIC";
453 break;
454 #endif
455 #ifdef LC_TIME
456 case LC_TIME:
457 retval = "LC_TIME";
458 break;
459 #endif
460 #ifdef LC_MESSAGES
461 case LC_MESSAGES:
462 retval = "LC_MESSAGES";
463 break;
464 #endif
465 #ifdef LC_RESPONSE
466 case LC_RESPONSE:
467 retval = "LC_RESPONSE";
468 break;
469 #endif
470 #ifdef LC_ALL
471 case LC_ALL:
472 /* This might not make sense but is perhaps better than any other
473 value. */
474 retval = "LC_ALL";
475 break;
476 #endif
477 default:
478 /* If you have a better idea for a default value let me know. */
479 retval = "LC_XXX";
482 return retval;
485 /* Guess value of current locale from value of the environment variables. */
486 static const char *guess_category_value (category, categoryname)
487 int category;
488 const char *categoryname;
490 const char *retval;
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')
496 return retval;
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);
503 #else
504 /* Setting of LC_ALL overwrites all other. */
505 retval = getenv ("LC_ALL");
506 if (retval != NULL && retval[0] != '\0')
507 return retval;
509 /* Next comes the name of the desired category. */
510 retval = getenv (categoryname);
511 if (retval != NULL && retval[0] != '\0')
512 return retval;
514 /* Last possibility is the LANG environment variable. */
515 retval = getenv ("LANG");
516 if (retval != NULL && retval[0] != '\0')
517 return retval;
519 /* We use C as the default domain. POSIX says this is implementation
520 defined. */
521 return "C";
522 #endif