root / intl / textdomain.c @ 22
History | View | Annotate | Download (4.4 kB)
| 1 | 1 | hiro | /* Implementation of the textdomain(3) function.
|
|---|---|---|---|
| 2 | 1 | hiro | Copyright (C) 1995-1998, 2000-2003 Free Software Foundation, Inc. |
| 3 | 1 | hiro | |
| 4 | 1 | hiro | This program is free software; you can redistribute it and/or modify it |
| 5 | 1 | hiro | under the terms of the GNU Library General Public License as published |
| 6 | 1 | hiro | by the Free Software Foundation; either version 2, or (at your option) |
| 7 | 1 | hiro | any later version. |
| 8 | 1 | hiro | |
| 9 | 1 | hiro | This program is distributed in the hope that it will be useful, |
| 10 | 1 | hiro | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | 1 | hiro | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | 1 | hiro | Library General Public License for more details. |
| 13 | 1 | hiro | |
| 14 | 1 | hiro | You should have received a copy of the GNU Library General Public |
| 15 | 1 | hiro | License along with this program; if not, write to the Free Software |
| 16 | 1 | hiro | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | 1 | hiro | USA. */ |
| 18 | 1 | hiro | |
| 19 | 1 | hiro | #ifdef HAVE_CONFIG_H
|
| 20 | 1 | hiro | # include <config.h> |
| 21 | 1 | hiro | #endif
|
| 22 | 1 | hiro | |
| 23 | 1 | hiro | #include <stdlib.h> |
| 24 | 1 | hiro | #include <string.h> |
| 25 | 1 | hiro | |
| 26 | 1 | hiro | #ifdef _LIBC
|
| 27 | 1 | hiro | # include <libintl.h> |
| 28 | 1 | hiro | #else
|
| 29 | 1 | hiro | # include "libgnuintl.h" |
| 30 | 1 | hiro | #endif
|
| 31 | 1 | hiro | #include "gettextP.h" |
| 32 | 1 | hiro | |
| 33 | 1 | hiro | #ifdef _LIBC
|
| 34 | 1 | hiro | /* We have to handle multi-threaded applications. */
|
| 35 | 1 | hiro | # include <bits/libc-lock.h> |
| 36 | 1 | hiro | #else
|
| 37 | 1 | hiro | /* Provide dummy implementation if this is outside glibc. */
|
| 38 | 1 | hiro | # define __libc_rwlock_define(CLASS, NAME)
|
| 39 | 1 | hiro | # define __libc_rwlock_wrlock(NAME)
|
| 40 | 1 | hiro | # define __libc_rwlock_unlock(NAME)
|
| 41 | 1 | hiro | #endif
|
| 42 | 1 | hiro | |
| 43 | 1 | hiro | /* The internal variables in the standalone libintl.a must have different
|
| 44 | 1 | hiro | names than the internal variables in GNU libc, otherwise programs |
| 45 | 1 | hiro | using libintl.a cannot be linked statically. */ |
| 46 | 1 | hiro | #if !defined _LIBC
|
| 47 | 1 | hiro | # define _nl_default_default_domain libintl_nl_default_default_domain
|
| 48 | 1 | hiro | # define _nl_current_default_domain libintl_nl_current_default_domain
|
| 49 | 1 | hiro | #endif
|
| 50 | 1 | hiro | |
| 51 | 1 | hiro | /* @@ end of prolog @@ */
|
| 52 | 1 | hiro | |
| 53 | 1 | hiro | /* Name of the default text domain. */
|
| 54 | 1 | hiro | extern const char _nl_default_default_domain[] attribute_hidden; |
| 55 | 1 | hiro | |
| 56 | 1 | hiro | /* Default text domain in which entries for gettext(3) are to be found. */
|
| 57 | 1 | hiro | extern const char *_nl_current_default_domain attribute_hidden; |
| 58 | 1 | hiro | |
| 59 | 1 | hiro | |
| 60 | 1 | hiro | /* Names for the libintl functions are a problem. They must not clash
|
| 61 | 1 | hiro | with existing names and they should follow ANSI C. But this source |
| 62 | 1 | hiro | code is also used in GNU C Library where the names have a __ |
| 63 | 1 | hiro | prefix. So we have to make a difference here. */ |
| 64 | 1 | hiro | #ifdef _LIBC
|
| 65 | 1 | hiro | # define TEXTDOMAIN __textdomain
|
| 66 | 1 | hiro | # ifndef strdup
|
| 67 | 1 | hiro | # define strdup(str) __strdup (str)
|
| 68 | 1 | hiro | # endif
|
| 69 | 1 | hiro | #else
|
| 70 | 1 | hiro | # define TEXTDOMAIN libintl_textdomain
|
| 71 | 1 | hiro | #endif
|
| 72 | 1 | hiro | |
| 73 | 1 | hiro | /* Lock variable to protect the global data in the gettext implementation. */
|
| 74 | 1 | hiro | __libc_rwlock_define (extern, _nl_state_lock attribute_hidden)
|
| 75 | 1 | hiro | |
| 76 | 1 | hiro | /* Set the current default message catalog to DOMAINNAME.
|
| 77 | 1 | hiro | If DOMAINNAME is null, return the current default. |
| 78 | 1 | hiro | If DOMAINNAME is "", reset to the default of "messages". */ |
| 79 | 1 | hiro | char *
|
| 80 | 1 | hiro | TEXTDOMAIN (const char *domainname) |
| 81 | 1 | hiro | {
|
| 82 | 1 | hiro | char *new_domain;
|
| 83 | 1 | hiro | char *old_domain;
|
| 84 | 1 | hiro | |
| 85 | 1 | hiro | /* A NULL pointer requests the current setting. */
|
| 86 | 1 | hiro | if (domainname == NULL) |
| 87 | 1 | hiro | return (char *) _nl_current_default_domain; |
| 88 | 1 | hiro | |
| 89 | 1 | hiro | __libc_rwlock_wrlock (_nl_state_lock); |
| 90 | 1 | hiro | |
| 91 | 1 | hiro | old_domain = (char *) _nl_current_default_domain;
|
| 92 | 1 | hiro | |
| 93 | 1 | hiro | /* If domain name is the null string set to default domain "messages". */
|
| 94 | 1 | hiro | if (domainname[0] == '\0' |
| 95 | 1 | hiro | || strcmp (domainname, _nl_default_default_domain) == 0)
|
| 96 | 1 | hiro | {
|
| 97 | 1 | hiro | _nl_current_default_domain = _nl_default_default_domain; |
| 98 | 1 | hiro | new_domain = (char *) _nl_current_default_domain;
|
| 99 | 1 | hiro | } |
| 100 | 1 | hiro | else if (strcmp (domainname, old_domain) == 0) |
| 101 | 1 | hiro | /* This can happen and people will use it to signal that some
|
| 102 | 1 | hiro | environment variable changed. */ |
| 103 | 1 | hiro | new_domain = old_domain; |
| 104 | 1 | hiro | else
|
| 105 | 1 | hiro | {
|
| 106 | 1 | hiro | /* If the following malloc fails `_nl_current_default_domain'
|
| 107 | 1 | hiro | will be NULL. This value will be returned and so signals we |
| 108 | 1 | hiro | are out of core. */ |
| 109 | 1 | hiro | #if defined _LIBC || defined HAVE_STRDUP
|
| 110 | 1 | hiro | new_domain = strdup (domainname); |
| 111 | 1 | hiro | #else
|
| 112 | 1 | hiro | size_t len = strlen (domainname) + 1;
|
| 113 | 1 | hiro | new_domain = (char *) malloc (len);
|
| 114 | 1 | hiro | if (new_domain != NULL) |
| 115 | 1 | hiro | memcpy (new_domain, domainname, len); |
| 116 | 1 | hiro | #endif
|
| 117 | 1 | hiro | |
| 118 | 1 | hiro | if (new_domain != NULL) |
| 119 | 1 | hiro | _nl_current_default_domain = new_domain; |
| 120 | 1 | hiro | } |
| 121 | 1 | hiro | |
| 122 | 1 | hiro | /* We use this possibility to signal a change of the loaded catalogs
|
| 123 | 1 | hiro | since this is most likely the case and there is no other easy we |
| 124 | 1 | hiro | to do it. Do it only when the call was successful. */ |
| 125 | 1 | hiro | if (new_domain != NULL) |
| 126 | 1 | hiro | {
|
| 127 | 1 | hiro | ++_nl_msg_cat_cntr; |
| 128 | 1 | hiro | |
| 129 | 1 | hiro | if (old_domain != new_domain && old_domain != _nl_default_default_domain)
|
| 130 | 1 | hiro | free (old_domain); |
| 131 | 1 | hiro | } |
| 132 | 1 | hiro | |
| 133 | 1 | hiro | __libc_rwlock_unlock (_nl_state_lock); |
| 134 | 1 | hiro | |
| 135 | 1 | hiro | return new_domain;
|
| 136 | 1 | hiro | } |
| 137 | 1 | hiro | |
| 138 | 1 | hiro | #ifdef _LIBC
|
| 139 | 1 | hiro | /* Alias for function name in GNU C Library. */
|
| 140 | 1 | hiro | weak_alias (__textdomain, textdomain); |
| 141 | 1 | hiro | #endif |