-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage.c
425 lines (374 loc) · 9.79 KB
/
language.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* language.c
* (c) 2002 Mikulas Patocka
* This file is a part of the Links program, released under GPL.
*/
#include "links.h"
struct translation {
const char *name;
};
struct translation_desc {
const struct translation *t;
};
unsigned char dummyarray[T__N_TEXTS];
#include "language.inc"
static unsigned char **translation_array[N_LANGUAGES][N_CODEPAGES];
int current_language;
static int current_lang_charset;
void init_trans(void)
{
int i, j;
for (i = 0; i < N_LANGUAGES; i++)
for (j = 0; j < N_CODEPAGES; j++)
translation_array[i][j] = NULL;
set_language(-1);
}
void shutdown_trans(void)
{
int i, j, k;
for (i = 0; i < N_LANGUAGES; i++)
for (j = 0; j < N_CODEPAGES; j++) if (translation_array[i][j]) {
for (k = 0; k < T__N_TEXTS; k++) {
unsigned char *txt = translation_array[i][j][k];
if (txt &&
txt != cast_uchar translations[i].t[k].name &&
txt != cast_uchar translation_english[k].name)
mem_free(txt);
}
mem_free(translation_array[i][j]);
}
}
int get_language_from_lang(unsigned char *lang)
{
unsigned char *p;
int i;
lang = stracpy(lang);
lang[strcspn(cast_const_char lang, ".@")] = 0;
if (!casestrcmp(lang, cast_uchar "nn_NO"))
strcpy(cast_char lang, "no");
for (p = lang; *p; p++) {
if (*p >= 'A' && *p <= 'Z')
*p += 'a' - 'A';
if (*p == '_')
*p = '-';
}
search_again:
for (i = 0; i < n_languages(); i++) {
p = cast_uchar translations[i].t[T__ACCEPT_LANGUAGE].name;
if (!p)
continue;
p = stracpy(p);
p[strcspn(cast_const_char p, ",;")] = 0;
if (!casestrcmp(lang, p)) {
mem_free(p);
mem_free(lang);
return i;
}
mem_free(p);
}
if ((p = cast_uchar strchr(cast_const_char lang, '-'))) {
*p = 0;
goto search_again;
}
mem_free(lang);
return -1;
}
int get_default_language(void)
{
static int default_language = -1;
unsigned char *lang;
if (default_language >= 0)
return default_language;
default_language = os_default_language();
if (default_language >= 0)
return default_language;
lang = cast_uchar getenv("LANG");
if (lang) {
default_language = get_language_from_lang(lang);
if (default_language >= 0)
return default_language;
}
default_language = get_language_from_lang(cast_uchar "en");
if (default_language < 0)
internal_error("default language 'english' not found");
return default_language;
}
int get_current_language(void)
{
if (current_language >= 0)
return current_language;
return get_default_language();
}
int get_default_charset(void)
{
static int default_charset = -1;
unsigned char *lang, *p;
int i;
if (default_charset >= 0)
return default_charset;
i = os_default_charset();
if (i >= 0)
goto ret_i;
lang = cast_uchar getenv("LC_CTYPE");
if (!lang)
lang = cast_uchar getenv("LANG");
if (!lang) {
i = 0;
goto ret_i;
}
if ((p = cast_uchar strchr(cast_const_char lang, '.'))) {
p++;
} else if ((i = get_cp_index(lang)) >= 0) {
goto ret_i;
} else {
if (strlen(cast_const_char lang) > 5 && !casestrcmp(cast_uchar (strchr(cast_const_char lang, 0) - 5), cast_uchar "@euro")) {
p = cast_uchar "ISO-8859-15";
} else {
int def_lang = get_language_from_lang(lang);
if (def_lang < 0) {
i = 0;
goto ret_i;
}
p = cast_uchar translations[def_lang].t[T__DEFAULT_CHAR_SET].name;
if (!p)
p = cast_uchar "";
}
}
i = get_cp_index(p);
if (i < 0)
i = 0;
ret_i:
#ifndef ENABLE_UTF8
if (!F && i == utf8_table)
i = 0;
#endif
default_charset = i;
return i;
}
int get_commandline_charset(void)
{
return dump_codepage == -1 ? get_default_charset() : dump_codepage;
}
static inline int get_text_index(unsigned char *text)
{
unsigned char * volatile dmv = dummyarray;
unsigned char * dm = dmv;
if (((my_uintptr_t)text >= (my_uintptr_t)dm && (my_uintptr_t)text < (my_uintptr_t)(dm + T__N_TEXTS)))
return (int)(text - dm);
return -1;
}
unsigned char *get_text_translation(unsigned char *text, struct terminal *term)
{
unsigned char **current_tra;
unsigned char *trn;
int charset;
int language_idx = get_current_language();
int idx;
if (!term) charset = 0;
else charset = term_charset(term);
idx = get_text_index(text);
if (idx < 0) return text;
if ((current_tra = translation_array[language_idx][charset])) {
unsigned char *tt;
if ((trn = current_tra[idx])) return trn;
tr:
if (!(tt = cast_uchar translations[language_idx].t[idx].name)) {
trn = cast_uchar translation_english[idx].name;
} else {
struct document_options l_opt;
memset(&l_opt, 0, sizeof(l_opt));
l_opt.plain = 0;
l_opt.cp = charset;
trn = convert(current_lang_charset, charset, tt, &l_opt);
if (!strcmp(cast_const_char trn, cast_const_char tt)) {
mem_free(trn);
trn = tt;
}
}
current_tra[idx] = trn;
} else {
if (current_lang_charset && charset != current_lang_charset) {
current_tra = translation_array[language_idx][charset] = mem_alloc(sizeof (unsigned char *) * T__N_TEXTS);
memset(current_tra, 0, sizeof (unsigned char *) * T__N_TEXTS);
goto tr;
}
if (!(trn = cast_uchar translations[language_idx].t[idx].name)) {
trn = cast_uchar translation_english[idx].name;
}
}
return trn;
}
unsigned char *get_english_translation(unsigned char *text)
{
int idx = get_text_index(text);
if (idx < 0) return text;
return cast_uchar translation_english[idx].name;
}
int n_languages(void)
{
return N_LANGUAGES;
}
unsigned char *language_name(int l)
{
if (l == -1) return cast_uchar "default";
return cast_uchar translations[l].t[T__LANGUAGE].name;
}
void set_language(int l)
{
int i;
unsigned char *cp;
current_language = l;
l = get_current_language();
cp = cast_uchar translations[l].t[T__CHAR_SET].name;
i = get_cp_index(cp);
if (i == -1) {
internal_error("Unknown charset for language %s.", translations[l].t[T__LANGUAGE].name);
i = 0;
}
current_lang_charset = i;
}
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE) && defined(LC_MESSAGES)
#define USE_OLD_LOCALE
#endif
#if defined(HAVE_FREELOCALE) && defined(HAVE_NEWLOCALE) && defined(HAVE_STRERROR_L)
#define USE_NEW_LOCALE
#endif
static unsigned char *strerror_alloc_internal(int err, void *loc_p)
{
unsigned char *strerror_buf, *e;
unsigned size = 32;
larger_buf:
strerror_buf = mem_alloc(size);
if (!loc_p)
e = cast_uchar strerror(err);
else
#ifdef USE_NEW_LOCALE
e = cast_uchar strerror_l(err, *(locale_t *)loc_p);
#else
e = NULL;
#endif
if (!e)
e = cast_uchar "Unknown error";
if (strlen(cast_const_char e) >= size) {
size *= 2;
if (!size) overalloc();
mem_free(strerror_buf);
goto larger_buf;
}
strcpy(cast_char strerror_buf, cast_const_char e);
return strerror_buf;
}
#ifdef USE_OLD_LOCALE
#ifdef USE_NEW_LOCALE
static unsigned char *strerror_try_locale(int err, unsigned char *lc, int charset_from, int charset_to)
{
locale_t nl;
unsigned char *str, *str_converted;
nl = newlocale(LC_CTYPE_MASK | LC_MESSAGES_MASK, cast_const_char lc, (locale_t)0);
if (nl == (locale_t)0)
return NULL;
str = strerror_alloc_internal(err, &nl);
freelocale(nl);
str_converted = convert(charset_from, charset_to, str, NULL);
mem_free(str);
return str_converted;
}
#else
static unsigned char *strerror_try_locale(int err, unsigned char *lc, int charset_from, int charset_to)
{
unsigned char *prev_type;
unsigned char *prev_msg;
unsigned char *str;
unsigned char *str_converted = NULL;
prev_type = cast_uchar setlocale(LC_CTYPE, NULL);
if (!prev_type)
goto err0;
prev_type = stracpy(prev_type);
prev_msg = cast_uchar setlocale(LC_MESSAGES, NULL);
if (!prev_msg)
goto err1;
prev_msg = stracpy(prev_msg);
if (!setlocale(LC_CTYPE, cast_const_char lc))
goto err2;
if (!setlocale(LC_MESSAGES, cast_const_char lc))
goto err2;
str = strerror_alloc_internal(err, NULL);
str_converted = convert(charset_from, charset_to, str, NULL);
mem_free(str);
err2:
setlocale(LC_MESSAGES, cast_const_char prev_msg);
mem_free(prev_msg);
err1:
setlocale(LC_CTYPE, cast_const_char prev_type);
mem_free(prev_type);
err0:
return str_converted;
}
#endif
static unsigned char *strerror_try_charset(int err, unsigned char *lc, unsigned char *charset_from, int charset_to)
{
unsigned char *full, *ret;
int idx;
idx = get_cp_index(charset_from);
if (idx < 0)
return NULL;
full = stracpy(lc);
add_to_strn(&full, cast_uchar ".");
add_to_strn(&full, charset_from);
ret = strerror_try_locale(err, full, idx, charset_to);
mem_free(full);
return ret;
}
static unsigned char *strerror_try_language(int err, unsigned char *lc, unsigned char *lc_charset, int charset_to)
{
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 5
return strerror_try_charset(err, lc, cast_uchar "UTF-8", charset_to);
#else
unsigned char *result;
if (charset_to == utf8_table) {
result = strerror_try_charset(err, lc, cast_uchar "UTF-8", charset_to);
if (result)
return result;
result = strerror_try_charset(err, lc, lc_charset, charset_to);
if (result)
return result;
} else {
result = strerror_try_charset(err, lc, lc_charset, charset_to);
if (result)
return result;
result = strerror_try_charset(err, lc, cast_uchar "UTF-8", charset_to);
if (result)
return result;
}
return NULL;
#endif
}
unsigned char *strerror_alloc(int err, struct terminal *term)
{
int charset_to;
unsigned char *lc_str, *lc_charset;
if (term) {
charset_to = term_charset(term);
} else {
charset_to = 0;
}
lc_str = get_text_translation(TEXT_(T__LOCALE_CODE), term);
lc_charset = get_text_translation(TEXT_(T__DEFAULT_CHAR_SET), term);
while (*lc_str) {
int l = (int)strcspn(cast_const_char lc_str, ",");
unsigned char *lc = memacpy(lc_str, l);
unsigned char *result;
result = strerror_try_language(err, lc, lc_charset, charset_to);
mem_free(lc);
if (result)
return result;
lc_str += l;
lc_str += *lc_str == ',';
}
return strerror_alloc_internal(err, NULL);
}
#else
unsigned char *strerror_alloc(int err, struct terminal *term)
{
return strerror_alloc_internal(err, NULL);
}
#endif