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