Debian Codegraph @

lib/gettext.h

raw
1 /* Convenience header for conditional use of GNU <libintl.h>.
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #ifndef _LIBGETTEXT_H
18 #define _LIBGETTEXT_H 1
19
20
21 /* NLS can be disabled through the configure --disable-nls option
22 or through "#define ENABLE NLS 0" before including this file. */
23 #if defined ENABLE_NLS && 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 && !defined __UCLIBC__) || _GLIBCXX_HAVE_LIBINTL_H
58 # include <libintl.h>
59 # endif
60 # endif
61
62 /* Disabled NLS. */
63 # if defined __GNUC__ && !defined __clang__ && !defined __cplusplus
64 /* Use inline functions, to avoid warnings
65 warning: format not a string literal and no format arguments
66 that don't occur with enabled NLS. */
67 /* The return type 'const char *' serves the purpose of producing warnings
68 for invalid uses of the value returned from these functions. */
69 # if __GNUC__ >= 9
70 # pragma GCC diagnostic push
71 # pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
72 # endif
73 # if __GNUC__ + (__GNUC_MINOR__ >= 2) > 4
74 __attribute__ ((__always_inline__, __gnu_inline__))
75 # else
76 __attribute__ ((__always_inline__))
77 # endif
78 extern inline
79 # if !defined(__sun)
80 const
81 # endif
82 char *
83 gettext (const char *msgid)
84 {
85 return msgid;
86 }
87 # if __GNUC__ + (__GNUC_MINOR__ >= 2) > 4
88 __attribute__ ((__always_inline__, __gnu_inline__))
89 # else
90 __attribute__ ((__always_inline__))
91 # endif
92 extern inline
93 # if !defined(__sun)
94 const
95 # endif
96 char *
97 dgettext (const char *domain, const char *msgid)
98 {
99 (void) domain;
100 return msgid;
101 }
102 # if __GNUC__ + (__GNUC_MINOR__ >= 2) > 4
103 __attribute__ ((__always_inline__, __gnu_inline__))
104 # else
105 __attribute__ ((__always_inline__))
106 # endif
107 extern inline
108 # if !defined(__sun)
109 const
110 # endif
111 char *
112 dcgettext (const char *domain, const char *msgid, int category)
113 {
114 (void) domain;
115 (void) category;
116 return msgid;
117 }
118 # if __GNUC__ >= 9
119 # pragma GCC diagnostic pop
120 # endif
121 # else
122 /* The casts to 'const char *' serve the purpose of producing warnings
123 for invalid uses of the value returned from these functions. */
124 # undef gettext
125 # define gettext(Msgid) ((const char *) (Msgid))
126 # undef dgettext
127 # define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
128 # undef dcgettext
129 # define dcgettext(Domainname, Msgid, Category) \
130 ((void) (Category), dgettext (Domainname, Msgid))
131 # endif
132 # undef ngettext
133 # define ngettext(Msgid1, Msgid2, N) \
134 ((N) == 1 \
135 ? ((void) (Msgid2), (const char *) (Msgid1)) \
136 : ((void) (Msgid1), (const char *) (Msgid2)))
137 # undef dngettext
138 # define dngettext(Domainname, Msgid1, Msgid2, N) \
139 ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
140 # undef dcngettext
141 # define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
142 ((void) (Category), dngettext (Domainname, Msgid1, Msgid2, N))
143 # undef textdomain
144 # define textdomain(Domainname) ((const char *) (Domainname))
145 # undef bindtextdomain
146 # define bindtextdomain(Domainname, Dirname) \
147 ((void) (Domainname), (const char *) (Dirname))
148 # undef bind_textdomain_codeset
149 # define bind_textdomain_codeset(Domainname, Codeset) \
150 ((void) (Domainname), (const char *) (Codeset))
151
152 #endif
153
154
155 /* Prefer gnulib's setlocale override over libintl's setlocale override. */
156 #ifdef GNULIB_defined_setlocale
157 # undef setlocale
158 # define setlocale rpl_setlocale
159 #endif
160
161
162 /* A pseudo function call that serves as a marker for the automated
163 extraction of messages, but does not call gettext(). The run-time
164 translation is done at a different place in the code.
165 The argument, String, should be a literal string. Concatenated strings
166 and other string expressions won't work.
167 The macro's expansion is not parenthesized, so that it is suitable as
168 initializer for static 'char[]' or 'const char[]' variables. */
169 #define gettext_noop(String) String
170
171
172 /* The separator between msgctxt and msgid in a .mo file. */
173 #define GETTEXT_CONTEXT_GLUE "\004"
174
175 /* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
176 MSGID. MSGCTXT and MSGID must be string literals. MSGCTXT should be
177 short and rarely need to change.
178 The letter 'p' stands for 'particular' or 'special'. */
179
180 #include <locale.h> /* for LC_MESSAGES */
181
182 #ifdef DEFAULT_TEXT_DOMAIN
183 # define pgettext(Msgctxt, Msgid) \
184 pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
185 #else
186 # define pgettext(Msgctxt, Msgid) \
187 pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
188 #endif
189 #define dpgettext(Domainname, Msgctxt, Msgid) \
190 pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
191 #define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
192 pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
193 #ifdef DEFAULT_TEXT_DOMAIN
194 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
195 npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
196 #else
197 # define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
198 npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
199 #endif
200 #define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
201 npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
202 #define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
203 npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
204
205 #if defined __GNUC__ || defined __clang__
206 __inline
207 #else
208 #ifdef __cplusplus
209 inline
210 #endif
211 #endif
212 static const char *
213 pgettext_aux (const char *domain,
214 const char *msg_ctxt_id, const char *msgid,
215 int category)
216 {
217 const char *translation = dcgettext (domain, msg_ctxt_id, category);
218 if (translation == msg_ctxt_id)
219 return msgid;
220 else
221 return translation;
222 }
223
224 #if defined __GNUC__ || defined __clang__
225 __inline
226 #else
227 #ifdef __cplusplus
228 inline
229 #endif
230 #endif
231 static const char *
232 npgettext_aux (const char *domain,
233 const char *msg_ctxt_id, const char *msgid,
234 const char *msgid_plural, unsigned long int n,
235 int category)
236 {
237 const char *translation =
238 dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
239 if (translation == msg_ctxt_id || translation == msgid_plural)
240 return (n == 1 ? msgid : msgid_plural);
241 else
242 return translation;
243 }
244
245
246 /* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID
247 can be arbitrary expressions. But for string literals these macros are
248 less efficient than those above. */
249
250 #include <string.h> /* for memcpy */
251
252 /* GNULIB_NO_VLA can be defined to disable use of VLAs even if supported.
253 This relates to the -Wvla and -Wvla-larger-than warnings, enabled in
254 the default GCC many warnings set. This allows programs to disable use
255 of VLAs, which may be unintended, or may be awkward to support portably,
256 or may have security implications due to non-deterministic stack usage. */
257
258 #if (!defined GNULIB_NO_VLA \
259 && defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__ \
260 && !defined __STDC_NO_VLA__)
261 # define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 1
262 #else
263 # define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS 0
264 #endif
265
266 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
267 # include <stdlib.h> /* for malloc, free */
268 #endif
269
270 #define pgettext_expr(Msgctxt, Msgid) \
271 dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
272 #define dpgettext_expr(Domainname, Msgctxt, Msgid) \
273 dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
274
275 #if defined __GNUC__ || defined __clang__
276 __inline
277 #else
278 #ifdef __cplusplus
279 inline
280 #endif
281 #endif
282 static const char *
283 dcpgettext_expr (const char *domain,
284 const char *msgctxt, const char *msgid,
285 int category)
286 {
287 size_t msgctxt_len = strlen (msgctxt) + 1;
288 size_t msgid_len = strlen (msgid) + 1;
289 const char *translation;
290 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
291 char msg_ctxt_id[msgctxt_len + msgid_len];
292 #else
293 char buf[1024];
294 char *msg_ctxt_id =
295 (msgctxt_len + msgid_len <= sizeof (buf)
296 ? buf
297 : (char *) malloc (msgctxt_len + msgid_len));
298 if (msg_ctxt_id != NULL)
299 #endif
300 {
301 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
302 msg_ctxt_id[msgctxt_len - 1] = '\004';
303 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
304 translation = dcgettext (domain, msg_ctxt_id, category);
305 int found_translation = (translation != msg_ctxt_id);
306 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
307 if (msg_ctxt_id != buf)
308 free (msg_ctxt_id);
309 #endif
310 if (found_translation)
311 return translation;
312 }
313 return msgid;
314 }
315
316 #define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
317 dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
318 #define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
319 dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
320
321 #if defined __GNUC__ || defined __clang__
322 __inline
323 #else
324 #ifdef __cplusplus
325 inline
326 #endif
327 #endif
328 static const char *
329 dcnpgettext_expr (const char *domain,
330 const char *msgctxt, const char *msgid,
331 const char *msgid_plural, unsigned long int n,
332 int category)
333 {
334 size_t msgctxt_len = strlen (msgctxt) + 1;
335 size_t msgid_len = strlen (msgid) + 1;
336 const char *translation;
337 #if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
338 char msg_ctxt_id[msgctxt_len + msgid_len];
339 #else
340 char buf[1024];
341 char *msg_ctxt_id =
342 (msgctxt_len + msgid_len <= sizeof (buf)
343 ? buf
344 : (char *) malloc (msgctxt_len + msgid_len));
345 if (msg_ctxt_id != NULL)
346 #endif
347 {
348 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
349 msg_ctxt_id[msgctxt_len - 1] = '\004';
350 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
351 translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
352 int found_translation = !(translation == msg_ctxt_id || translation == msgid_plural);
353 #if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
354 if (msg_ctxt_id != buf)
355 free (msg_ctxt_id);
356 #endif
357 if (found_translation)
358 return translation;
359 }
360 return (n == 1 ? msgid : msgid_plural);
361 }
362
363
364 #endif /* _LIBGETTEXT_H */