-
Notifications
You must be signed in to change notification settings - Fork 8
/
libintl.c
303 lines (253 loc) · 7.15 KB
/
libintl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright (C) 2008 Tor Lillqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; see the file COPYING.LIB.txt. If
* not, write to the Free Software Foundation, Inc., 51 Franklin
* Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#else
# define _POSIX_C_SOURCE 200809L
# include <stddef.h>
# if !STUB_ONLY
# include <dlfcn.h>
# endif
typedef void* HMODULE;
#endif
#ifdef _MSC_VER
# define strdup(s) _strdup (s)
#endif
#include <stdlib.h>
#include <string.h>
#define G_INTL_COMPILATION
#include "libintl.h"
int _nl_msg_cat_cntr; /* So that configury thinks it is GNU
* gettext
*/
static char * (*p_gettext) (const char *msgid);
static char * (*p_dgettext) (const char *domainname,
const char *msgid);
static char * (*p_dcgettext) (const char *domainname,
const char *msgid,
int category);
static char * (*p_ngettext) (const char *msgid1,
const char *msgid2,
unsigned long int n);
static char * (*p_dngettext) (const char *domainname,
const char *msgid1,
const char *msgid2,
unsigned long int n);
static char * (*p_dcngettext) (const char *domainname,
const char *msgid1,
const char *msgid2,
unsigned long int n,
int category);
static char * (*p_textdomain) (const char *domainname);
static char * (*p_bindtextdomain) (const char *domainname,
const char *dirname);
static char * (*p_bind_textdomain_codeset) (const char *domainname,
const char *codeset);
static int
use_intl_dll (HMODULE dll)
{
#if !STUB_ONLY
# ifdef _WIN32
# define LOOKUP(fn) p_##fn = (void *) GetProcAddress (dll, #fn); if (p_##fn == NULL) return 0
# else
# define LOOKUP(fn) p_##fn = (void *) dlsym (dll, #fn); if (p_##fn == NULL) return 0
# endif /* _WIN32 */
LOOKUP (gettext);
LOOKUP (dgettext);
LOOKUP (dcgettext);
LOOKUP (ngettext);
LOOKUP (dngettext);
LOOKUP (dcngettext);
LOOKUP (textdomain);
LOOKUP (bindtextdomain);
LOOKUP (bind_textdomain_codeset);
#undef LOOKUP
#endif /* !STUB_ONLY */
return 1;
}
static char *current_domain = NULL;
#define DUMMY(fn, parlist, retval) \
static char * \
dummy_##fn parlist \
{ \
return (char *) (retval); \
}
DUMMY (gettext,
(const char *msgid),
msgid)
DUMMY (dgettext,
(const char *domainname,
const char *msgid),
msgid)
DUMMY (dcgettext,
(const char *domainname,
const char *msgid,
int category),
msgid)
DUMMY (ngettext,
(const char *msgid1,
const char *msgid2,
unsigned long int n),
n == 1 ? msgid1 : msgid2)
DUMMY (dngettext,
(const char *domainname,
const char *msgid1,
const char *msgid2,
unsigned long int n),
n == 1 ? msgid1 : msgid2)
DUMMY (dcngettext,
(const char *domainname,
const char *msgid1,
const char *msgid2,
unsigned long int n,
int category),
n == 1 ? msgid1 : msgid2)
/* GLib requires that textdomain(NULL) returns "messages"
* if textdomain() hasn't been called earlier.
*/
DUMMY (textdomain,
(const char *domainname),
(domainname ?
(free (current_domain), current_domain = strdup (domainname)) :
(current_domain ?
current_domain :
(current_domain = strdup ("messages")))))
/* bindtextdomain() should return the current dirname for the domain,
* after possibly changing it. I don't think software usually checks
* the return value, though, so just return a dummy string now. This
* is the dummy implementation after all, so it hardly matters?
*/
DUMMY (bindtextdomain,
(const char *domainname,
const char *dirname),
"/dummy")
/* bind_textdomain_codeset() should return the corrent codeset for the
* domain after possibly changing it. Again, this is the dummy
* implementation, so just return the codeset argument.
*/
DUMMY (bind_textdomain_codeset,
(const char *domainname,
const char *codeset),
codeset)
#undef DUMMY
static void
use_dummy (void)
{
#define USE_DUMMY(fn) p_##fn = dummy_##fn
USE_DUMMY (gettext);
USE_DUMMY (dgettext);
USE_DUMMY (dcgettext);
USE_DUMMY (ngettext);
USE_DUMMY (dngettext);
USE_DUMMY (dcngettext);
USE_DUMMY (textdomain);
USE_DUMMY (bindtextdomain);
USE_DUMMY (bind_textdomain_codeset);
#undef USE_DUMMY
}
static void
setup (void)
{
static int beenhere = 0;
if (!beenhere)
{
#if !STUB_ONLY
#if defined(_WIN64)
/* On 64-bit Windows we have let libtool choose the default name
* for the DLL, as we don't need the intl.dll name for backward
* compatibility
*/
HMODULE intl_dll = LoadLibrary ("libintl-8.dll");
# elif defined( _WIN32)
HMODULE intl_dll = LoadLibrary ("intl.dll");
# elif defined(__APPLE__) && defined(__MACH__)
HMODULE intl_dll = dlopen ("libintl.dylib", RTLD_LAZY);
# else
HMODULE intl_dll = dlopen ("libintl.so", RTLD_LAZY);
# endif
#else /* !STUB_ONLY */
HMODULE intl_dll = NULL;
#endif /* STUB_ONLY */
if (intl_dll != NULL &&
use_intl_dll (intl_dll))
;
else
use_dummy ();
beenhere = 1;
}
}
void
_proxy_libintl_deinit (void)
{
if (current_domain != NULL)
{
free (current_domain);
current_domain = NULL;
}
}
#define IMPLEMENT(fn, parlist, parlist2) \
char * \
g_libintl_ ## fn parlist \
{ \
setup (); \
return p_##fn parlist2; \
}
IMPLEMENT (gettext,
(const char *msgid),
(msgid))
IMPLEMENT (dgettext,
(const char *domainname,
const char *msgid),
(domainname, msgid))
IMPLEMENT (dcgettext,
(const char *domainname,
const char *msgid,
int category),
(domainname, msgid, category))
IMPLEMENT (ngettext,
(const char *msgid1,
const char *msgid2,
unsigned long int n),
(msgid1, msgid2, n))
IMPLEMENT (dngettext,
(const char *domainname,
const char *msgid1,
const char *msgid2,
unsigned long int n),
(domainname, msgid1, msgid2, n))
IMPLEMENT (dcngettext,
(const char *domainname,
const char *msgid1,
const char *msgid2,
unsigned long int n,
int category),
(domainname, msgid1, msgid2, n, category))
IMPLEMENT (textdomain,
(const char *domainname),
(domainname))
IMPLEMENT (bindtextdomain,
(const char *domainname,
const char *dirname),
(domainname, dirname))
IMPLEMENT (bind_textdomain_codeset,
(const char *domainname,
const char *codeset),
(domainname, codeset))
#undef IMPLEMENT