1 /* Convenience header for conditional use of GNU <libintl.h>.
2    Copyright (C) 1995-1998, 2000-2002, 2004-2006, 2009-2011 Free Software Foundation, Inc.
3 
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 3 of the License, or
7    (at your option) any later version.
8 
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.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16 
17 #ifndef _LIBGETTEXT_H
18 #define _LIBGETTEXT_H 1
19 
20 /* NLS can be disabled through the configure --disable-nls option.  */
21 #if ENABLE_NLS
22 
23 /* Get declarations of GNU message catalog functions.  */
24 # include <libintl.h>
25 
26 /* You can set the DEFAULT_TEXT_DOMAIN macro to specify the domain used by
27    the gettext() and ngettext() macros.  This is an alternative to calling
28    textdomain(), and is useful for libraries.  */
29 # ifdef DEFAULT_TEXT_DOMAIN
30 #  undef gettext
31 #  define gettext(Msgid) \
32      dgettext (DEFAULT_TEXT_DOMAIN, Msgid)
33 #  undef ngettext
34 #  define ngettext(Msgid1, Msgid2, N) \
35      dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N)
36 # endif
37 
38 #else
39 
40 /* Solaris /usr/include/locale.h includes /usr/include/libintl.h, which
41    chokes if dcgettext is defined as a macro.  So include it now, to make
42    later inclusions of <locale.h> a NOP.  We don't include <libintl.h>
43    as well because people using <stdio.h> will not include <libintl.h>,
44    and also including <libintl.h> would fail on SunOS 4, whereas <locale.h>
45    is OK.  */
46 #if defined(__sun)
47 # include <locale.h>
48 #endif
49 
50 /* Many header files from the libstdc++ coming with g++ 3.3 or newer include
51    <libintl.h>, which chokes if dcgettext is defined as a macro.  So include
52    it now, to make later inclusions of <libintl.h> a NOP.  */
53 #if defined(__cplusplus) && defined(__GNUG__) && (__GNUC__ >= 3)
54 # include <cstdlib>
55 # if (__GLIBC__ >= 2 && !defined __UCLIBC__) || _GLIBCXX_HAVE_LIBINTL_H
56 #  include <libintl.h>
57 # endif
58 #endif
59 
60 /* Disabled NLS.
61    The casts to 'const char *' serve the purpose of producing warnings
62    for invalid uses of the value returned from these functions.
63    On pre-ANSI systems without 'const', the config.h file is supposed to
64    contain "#define const".  */
65 # undef gettext
66 # define gettext(Msgid) ((const char *) (Msgid))
67 # undef dgettext
68 # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
69 # undef dcgettext
70 # define dcgettext(Domainname, Msgid, Category) \
71     ((void) (Category), dgettext (Domainname, Msgid))
72 # undef ngettext
73 # define ngettext(Msgid1, Msgid2, N) \
74     ((N) == 1 \
75      ? ((void) (Msgid2), (const char *) (Msgid1)) \
76      : ((void) (Msgid1), (const char *) (Msgid2)))
77 # undef dngettext
78 # define dngettext(Domainname, Msgid1, Msgid2, N) \
79     ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
80 # undef dcngettext
81 # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
82     ((void) (Category), dngettext (Domainname, Msgid1, Msgid2, N))
83 # undef textdomain
84 # define textdomain(Domainname) ((const char *) (Domainname))
85 # undef bindtextdomain
86 # define bindtextdomain(Domainname, Dirname) \
87     ((void) (Domainname), (const char *) (Dirname))
88 # undef bind_textdomain_codeset
89 # define bind_textdomain_codeset(Domainname, Codeset) \
90     ((void) (Domainname), (const char *) (Codeset))
91 
92 #endif
93 
94 /* Prefer gnulib's setlocale override over libintl's setlocale override.  */
95 #ifdef GNULIB_defined_setlocale
96 # undef setlocale
97 # define setlocale rpl_setlocale
98 #endif
99 
100 /* A pseudo function call that serves as a marker for the automated
101    extraction of messages, but does not call gettext().  The run-time
102    translation is done at a different place in the code.
103    The argument, String, should be a literal string.  Concatenated strings
104    and other string expressions won't work.
105    The macro's expansion is not parenthesized, so that it is suitable as
106    initializer for static 'char[]' or 'const char[]' variables.  */
107 #define gettext_noop(String) String
108 
109 /* The separator between msgctxt and msgid in a .mo file.  */
110 #define GETTEXT_CONTEXT_GLUE "\004"
111 
112 /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
113    MSGID.  MSGCTXT and MSGID must be string literals.  MSGCTXT should be
114    short and rarely need to change.
115    The letter 'p' stands for 'particular' or 'special'.  */
116 #ifdef DEFAULT_TEXT_DOMAIN
117 # define pgettext(Msgctxt, Msgid) \
118    pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
119 #else
120 # define pgettext(Msgctxt, Msgid) \
121    pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
122 #endif
123 #define dpgettext(Domainname, Msgctxt, Msgid) \
124   pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
125 #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
126   pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
127 #ifdef DEFAULT_TEXT_DOMAIN
128 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
129    npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
130 #else
131 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
132    npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
133 #endif
134 #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
135   npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
136 #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
137   npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
138 
139 #ifdef __GNUC__
140 __inline
141 #else
142 #ifdef __cplusplus
143 inline
144 #endif
145 #endif
146 static const char *
pgettext_aux(const char * domain,const char * msg_ctxt_id,const char * msgid,int category)147 pgettext_aux (const char *domain,
148               const char *msg_ctxt_id, const char *msgid,
149               int category)
150 {
151   const char *translation = dcgettext (domain, msg_ctxt_id, category);
152   if (translation == msg_ctxt_id)
153     return msgid;
154   else
155     return translation;
156 }
157 
158 #ifdef __GNUC__
159 __inline
160 #else
161 #ifdef __cplusplus
162 inline
163 #endif
164 #endif
165 static const char *
npgettext_aux(const char * domain,const char * msg_ctxt_id,const char * msgid,const char * msgid_plural,unsigned long int n,int category)166 npgettext_aux (const char *domain,
167                const char *msg_ctxt_id, const char *msgid,
168                const char *msgid_plural, unsigned long int n,
169                int category)
170 {
171   const char *translation =
172     dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
173   if (translation == msg_ctxt_id || translation == msgid_plural)
174     return (n == 1 ? msgid : msgid_plural);
175   else
176     return translation;
177 }
178 
179 /* The same thing extended for non-constant arguments.  Here MSGCTXT and MSGID
180    can be arbitrary expressions.  But for string literals these macros are
181    less efficient than those above.  */
182 
183 #include <string.h>
184 
185 #if (((__GNUC__ >= 3 || __GNUG__ >= 2) && !defined __STRICT_ANSI__) \
186      /* || __STDC_VERSION__ >= 199901L */ )
187 # define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 1
188 #else
189 # define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 0
190 #endif
191 
192 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
193 #include <stdlib.h>
194 #endif
195 
196 #define pgettext_expr(Msgctxt, Msgid) \
197   dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
198 #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
199   dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
200 
201 #ifdef __GNUC__
202 __inline
203 #else
204 #ifdef __cplusplus
205 inline
206 #endif
207 #endif
208 static const char *
dcpgettext_expr(const char * domain,const char * msgctxt,const char * msgid,int category)209 dcpgettext_expr (const char *domain,
210                  const char *msgctxt, const char *msgid,
211                  int category)
212 {
213   size_t msgctxt_len = strlen (msgctxt) + 1;
214   size_t msgid_len = strlen (msgid) + 1;
215   const char *translation;
216 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
217   char msg_ctxt_id[msgctxt_len + msgid_len];
218 #else
219   char buf[1024];
220   char *msg_ctxt_id =
221     (msgctxt_len + msgid_len <= sizeof (buf)
222      ? buf
223      : (char *) malloc (msgctxt_len + msgid_len));
224   if (msg_ctxt_id != NULL)
225 #endif
226     {
227       memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
228       msg_ctxt_id[msgctxt_len - 1] = '\004';
229       memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
230       translation = dcgettext (domain, msg_ctxt_id, category);
231 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
232       if (msg_ctxt_id != buf)
233         free (msg_ctxt_id);
234 #endif
235       if (translation != msg_ctxt_id)
236         return translation;
237     }
238   return msgid;
239 }
240 
241 #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
242   dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
243 #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
244   dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
245 
246 #ifdef __GNUC__
247 __inline
248 #else
249 #ifdef __cplusplus
250 inline
251 #endif
252 #endif
253 static const char *
dcnpgettext_expr(const char * domain,const char * msgctxt,const char * msgid,const char * msgid_plural,unsigned long int n,int category)254 dcnpgettext_expr (const char *domain,
255                   const char *msgctxt, const char *msgid,
256                   const char *msgid_plural, unsigned long int n,
257                   int category)
258 {
259   size_t msgctxt_len = strlen (msgctxt) + 1;
260   size_t msgid_len = strlen (msgid) + 1;
261   const char *translation;
262 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
263   char msg_ctxt_id[msgctxt_len + msgid_len];
264 #else
265   char buf[1024];
266   char *msg_ctxt_id =
267     (msgctxt_len + msgid_len <= sizeof (buf)
268      ? buf
269      : (char *) malloc (msgctxt_len + msgid_len));
270   if (msg_ctxt_id != NULL)
271 #endif
272     {
273       memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
274       msg_ctxt_id[msgctxt_len - 1] = '\004';
275       memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
276       translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
277 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
278       if (msg_ctxt_id != buf)
279         free (msg_ctxt_id);
280 #endif
281       if (!(translation == msg_ctxt_id || translation == msgid_plural))
282         return translation;
283     }
284   return (n == 1 ? msgid : msgid_plural);
285 }
286 
287 #endif /* _LIBGETTEXT_H */
288