Back to index

tetex-bin  3.0
textdomain.c
Go to the documentation of this file.
00001 /* Implementation of the textdomain(3) function.
00002    Copyright (C) 1995-1998, 2000-2003 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify it
00005    under the terms of the GNU Library General Public License as published
00006    by the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public
00015    License along with this program; if not, write to the Free Software
00016    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00017    USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022 
00023 #include <stdlib.h>
00024 #include <string.h>
00025 
00026 #ifdef _LIBC
00027 # include <libintl.h>
00028 #else
00029 # include "libgnuintl.h"
00030 #endif
00031 #include "gettextP.h"
00032 
00033 #ifdef _LIBC
00034 /* We have to handle multi-threaded applications.  */
00035 # include <bits/libc-lock.h>
00036 #else
00037 /* Provide dummy implementation if this is outside glibc.  */
00038 # define __libc_rwlock_define(CLASS, NAME)
00039 # define __libc_rwlock_wrlock(NAME)
00040 # define __libc_rwlock_unlock(NAME)
00041 #endif
00042 
00043 /* The internal variables in the standalone libintl.a must have different
00044    names than the internal variables in GNU libc, otherwise programs
00045    using libintl.a cannot be linked statically.  */
00046 #if !defined _LIBC
00047 # define _nl_default_default_domain libintl_nl_default_default_domain
00048 # define _nl_current_default_domain libintl_nl_current_default_domain
00049 #endif
00050 
00051 /* @@ end of prolog @@ */
00052 
00053 /* Name of the default text domain.  */
00054 extern const char _nl_default_default_domain[] attribute_hidden;
00055 
00056 /* Default text domain in which entries for gettext(3) are to be found.  */
00057 extern const char *_nl_current_default_domain attribute_hidden;
00058 
00059 
00060 /* Names for the libintl functions are a problem.  They must not clash
00061    with existing names and they should follow ANSI C.  But this source
00062    code is also used in GNU C Library where the names have a __
00063    prefix.  So we have to make a difference here.  */
00064 #ifdef _LIBC
00065 # define TEXTDOMAIN __textdomain
00066 # ifndef strdup
00067 #  define strdup(str) __strdup (str)
00068 # endif
00069 #else
00070 # define TEXTDOMAIN libintl_textdomain
00071 #endif
00072 
00073 /* Lock variable to protect the global data in the gettext implementation.  */
00074 __libc_rwlock_define (extern, _nl_state_lock attribute_hidden)
00075 
00076 /* Set the current default message catalog to DOMAINNAME.
00077    If DOMAINNAME is null, return the current default.
00078    If DOMAINNAME is "", reset to the default of "messages".  */
00079 char *
00080 TEXTDOMAIN (const char *domainname)
00081 {
00082   char *new_domain;
00083   char *old_domain;
00084 
00085   /* A NULL pointer requests the current setting.  */
00086   if (domainname == NULL)
00087     return (char *) _nl_current_default_domain;
00088 
00089   __libc_rwlock_wrlock (_nl_state_lock);
00090 
00091   old_domain = (char *) _nl_current_default_domain;
00092 
00093   /* If domain name is the null string set to default domain "messages".  */
00094   if (domainname[0] == '\0'
00095       || strcmp (domainname, _nl_default_default_domain) == 0)
00096     {
00097       _nl_current_default_domain = _nl_default_default_domain;
00098       new_domain = (char *) _nl_current_default_domain;
00099     }
00100   else if (strcmp (domainname, old_domain) == 0)
00101     /* This can happen and people will use it to signal that some
00102        environment variable changed.  */
00103     new_domain = old_domain;
00104   else
00105     {
00106       /* If the following malloc fails `_nl_current_default_domain'
00107         will be NULL.  This value will be returned and so signals we
00108         are out of core.  */
00109 #if defined _LIBC || defined HAVE_STRDUP
00110       new_domain = strdup (domainname);
00111 #else
00112       size_t len = strlen (domainname) + 1;
00113       new_domain = (char *) malloc (len);
00114       if (new_domain != NULL)
00115        memcpy (new_domain, domainname, len);
00116 #endif
00117 
00118       if (new_domain != NULL)
00119        _nl_current_default_domain = new_domain;
00120     }
00121 
00122   /* We use this possibility to signal a change of the loaded catalogs
00123      since this is most likely the case and there is no other easy we
00124      to do it.  Do it only when the call was successful.  */
00125   if (new_domain != NULL)
00126     {
00127       ++_nl_msg_cat_cntr;
00128 
00129       if (old_domain != new_domain && old_domain != _nl_default_default_domain)
00130        free (old_domain);
00131     }
00132 
00133   __libc_rwlock_unlock (_nl_state_lock);
00134 
00135   return new_domain;
00136 }
00137 
00138 #ifdef _LIBC
00139 /* Alias for function name in GNU C Library.  */
00140 weak_alias (__textdomain, textdomain);
00141 #endif