Back to index

nagios-plugins  1.4.16
gettext.h
Go to the documentation of this file.
00001 /* Convenience header for conditional use of GNU <libintl.h>.
00002    Copyright (C) 1995-1998, 2000-2002, 2004-2006, 2009-2010 Free Software
00003    Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License along
00016    with this program; if not, write to the Free Software Foundation,
00017    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00018 
00019 #ifndef _LIBGETTEXT_H
00020 #define _LIBGETTEXT_H 1
00021 
00022 /* NLS can be disabled through the configure --disable-nls option.  */
00023 #if ENABLE_NLS
00024 
00025 /* Get declarations of GNU message catalog functions.  */
00026 # include <libintl.h>
00027 
00028 /* You can set the DEFAULT_TEXT_DOMAIN macro to specify the domain used by
00029    the gettext() and ngettext() macros.  This is an alternative to calling
00030    textdomain(), and is useful for libraries.  */
00031 # ifdef DEFAULT_TEXT_DOMAIN
00032 #  undef gettext
00033 #  define gettext(Msgid) \
00034      dgettext (DEFAULT_TEXT_DOMAIN, Msgid)
00035 #  undef ngettext
00036 #  define ngettext(Msgid1, Msgid2, N) \
00037      dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N)
00038 # endif
00039 
00040 #else
00041 
00042 /* Solaris /usr/include/locale.h includes /usr/include/libintl.h, which
00043    chokes if dcgettext is defined as a macro.  So include it now, to make
00044    later inclusions of <locale.h> a NOP.  We don't include <libintl.h>
00045    as well because people using "gettext.h" will not include <libintl.h>,
00046    and also including <libintl.h> would fail on SunOS 4, whereas <locale.h>
00047    is OK.  */
00048 #if defined(__sun)
00049 # include <locale.h>
00050 #endif
00051 
00052 /* Many header files from the libstdc++ coming with g++ 3.3 or newer include
00053    <libintl.h>, which chokes if dcgettext is defined as a macro.  So include
00054    it now, to make later inclusions of <libintl.h> a NOP.  */
00055 #if defined(__cplusplus) && defined(__GNUG__) && (__GNUC__ >= 3)
00056 # include <cstdlib>
00057 # if (__GLIBC__ >= 2) || _GLIBCXX_HAVE_LIBINTL_H
00058 #  include <libintl.h>
00059 # endif
00060 #endif
00061 
00062 /* Disabled NLS.
00063    The casts to 'const char *' serve the purpose of producing warnings
00064    for invalid uses of the value returned from these functions.
00065    On pre-ANSI systems without 'const', the config.h file is supposed to
00066    contain "#define const".  */
00067 # undef gettext
00068 # define gettext(Msgid) ((const char *) (Msgid))
00069 # undef dgettext
00070 # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
00071 # undef dcgettext
00072 # define dcgettext(Domainname, Msgid, Category) \
00073     ((void) (Category), dgettext (Domainname, Msgid))
00074 # undef ngettext
00075 # define ngettext(Msgid1, Msgid2, N) \
00076     ((N) == 1 \
00077      ? ((void) (Msgid2), (const char *) (Msgid1)) \
00078      : ((void) (Msgid1), (const char *) (Msgid2)))
00079 # undef dngettext
00080 # define dngettext(Domainname, Msgid1, Msgid2, N) \
00081     ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
00082 # undef dcngettext
00083 # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
00084     ((void) (Category), dngettext (Domainname, Msgid1, Msgid2, N))
00085 # undef textdomain
00086 # define textdomain(Domainname) ((const char *) (Domainname))
00087 # undef bindtextdomain
00088 # define bindtextdomain(Domainname, Dirname) \
00089     ((void) (Domainname), (const char *) (Dirname))
00090 # undef bind_textdomain_codeset
00091 # define bind_textdomain_codeset(Domainname, Codeset) \
00092     ((void) (Domainname), (const char *) (Codeset))
00093 
00094 #endif
00095 
00096 /* A pseudo function call that serves as a marker for the automated
00097    extraction of messages, but does not call gettext().  The run-time
00098    translation is done at a different place in the code.
00099    The argument, String, should be a literal string.  Concatenated strings
00100    and other string expressions won't work.
00101    The macro's expansion is not parenthesized, so that it is suitable as
00102    initializer for static 'char[]' or 'const char[]' variables.  */
00103 #define gettext_noop(String) String
00104 
00105 /* The separator between msgctxt and msgid in a .mo file.  */
00106 #define GETTEXT_CONTEXT_GLUE "\004"
00107 
00108 /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
00109    MSGID.  MSGCTXT and MSGID must be string literals.  MSGCTXT should be
00110    short and rarely need to change.
00111    The letter 'p' stands for 'particular' or 'special'.  */
00112 #ifdef DEFAULT_TEXT_DOMAIN
00113 # define pgettext(Msgctxt, Msgid) \
00114    pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
00115 #else
00116 # define pgettext(Msgctxt, Msgid) \
00117    pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
00118 #endif
00119 #define dpgettext(Domainname, Msgctxt, Msgid) \
00120   pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
00121 #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
00122   pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
00123 #ifdef DEFAULT_TEXT_DOMAIN
00124 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
00125    npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
00126 #else
00127 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
00128    npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
00129 #endif
00130 #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
00131   npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
00132 #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
00133   npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
00134 
00135 #ifdef __GNUC__
00136 __inline
00137 #else
00138 #ifdef __cplusplus
00139 inline
00140 #endif
00141 #endif
00142 static const char *
00143 pgettext_aux (const char *domain,
00144               const char *msg_ctxt_id, const char *msgid,
00145               int category)
00146 {
00147   const char *translation = dcgettext (domain, msg_ctxt_id, category);
00148   if (translation == msg_ctxt_id)
00149     return msgid;
00150   else
00151     return translation;
00152 }
00153 
00154 #ifdef __GNUC__
00155 __inline
00156 #else
00157 #ifdef __cplusplus
00158 inline
00159 #endif
00160 #endif
00161 static const char *
00162 npgettext_aux (const char *domain,
00163                const char *msg_ctxt_id, const char *msgid,
00164                const char *msgid_plural, unsigned long int n,
00165                int category)
00166 {
00167   const char *translation =
00168     dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
00169   if (translation == msg_ctxt_id || translation == msgid_plural)
00170     return (n == 1 ? msgid : msgid_plural);
00171   else
00172     return translation;
00173 }
00174 
00175 /* The same thing extended for non-constant arguments.  Here MSGCTXT and MSGID
00176    can be arbitrary expressions.  But for string literals these macros are
00177    less efficient than those above.  */
00178 
00179 #include <string.h>
00180 
00181 #define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \
00182   (((__GNUC__ >= 3 || __GNUG__ >= 2) && !__STRICT_ANSI__) \
00183    /* || __STDC_VERSION__ >= 199901L */ )
00184 
00185 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00186 #include <stdlib.h>
00187 #endif
00188 
00189 #define pgettext_expr(Msgctxt, Msgid) \
00190   dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
00191 #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
00192   dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
00193 
00194 #ifdef __GNUC__
00195 __inline
00196 #else
00197 #ifdef __cplusplus
00198 inline
00199 #endif
00200 #endif
00201 static const char *
00202 dcpgettext_expr (const char *domain,
00203                  const char *msgctxt, const char *msgid,
00204                  int category)
00205 {
00206   size_t msgctxt_len = strlen (msgctxt) + 1;
00207   size_t msgid_len = strlen (msgid) + 1;
00208   const char *translation;
00209 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00210   char msg_ctxt_id[msgctxt_len + msgid_len];
00211 #else
00212   char buf[1024];
00213   char *msg_ctxt_id =
00214     (msgctxt_len + msgid_len <= sizeof (buf)
00215      ? buf
00216      : (char *) malloc (msgctxt_len + msgid_len));
00217   if (msg_ctxt_id != NULL)
00218 #endif
00219     {
00220       memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
00221       msg_ctxt_id[msgctxt_len - 1] = '\004';
00222       memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
00223       translation = dcgettext (domain, msg_ctxt_id, category);
00224 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00225       if (msg_ctxt_id != buf)
00226         free (msg_ctxt_id);
00227 #endif
00228       if (translation != msg_ctxt_id)
00229         return translation;
00230     }
00231   return msgid;
00232 }
00233 
00234 #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
00235   dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
00236 #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
00237   dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
00238 
00239 #ifdef __GNUC__
00240 __inline
00241 #else
00242 #ifdef __cplusplus
00243 inline
00244 #endif
00245 #endif
00246 static const char *
00247 dcnpgettext_expr (const char *domain,
00248                   const char *msgctxt, const char *msgid,
00249                   const char *msgid_plural, unsigned long int n,
00250                   int category)
00251 {
00252   size_t msgctxt_len = strlen (msgctxt) + 1;
00253   size_t msgid_len = strlen (msgid) + 1;
00254   const char *translation;
00255 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00256   char msg_ctxt_id[msgctxt_len + msgid_len];
00257 #else
00258   char buf[1024];
00259   char *msg_ctxt_id =
00260     (msgctxt_len + msgid_len <= sizeof (buf)
00261      ? buf
00262      : (char *) malloc (msgctxt_len + msgid_len));
00263   if (msg_ctxt_id != NULL)
00264 #endif
00265     {
00266       memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
00267       msg_ctxt_id[msgctxt_len - 1] = '\004';
00268       memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
00269       translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
00270 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
00271       if (msg_ctxt_id != buf)
00272         free (msg_ctxt_id);
00273 #endif
00274       if (!(translation == msg_ctxt_id || translation == msgid_plural))
00275         return translation;
00276     }
00277   return (n == 1 ? msgid : msgid_plural);
00278 }
00279 
00280 #endif /* _LIBGETTEXT_H */