1 | /* > message.c |
---|
2 | * Fairly general purpose message and error routines |
---|
3 | * Copyright (C) 1993-2001 Olly Betts |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
18 | */ |
---|
19 | |
---|
20 | /*#define DEBUG 1*/ |
---|
21 | |
---|
22 | #ifdef HAVE_CONFIG_H |
---|
23 | # include <config.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <stdlib.h> |
---|
28 | #include <string.h> |
---|
29 | #include <ctype.h> |
---|
30 | #include <limits.h> |
---|
31 | #include <errno.h> |
---|
32 | #include <locale.h> |
---|
33 | |
---|
34 | #include "whichos.h" |
---|
35 | #include "filename.h" |
---|
36 | #include "message.h" |
---|
37 | #include "osdepend.h" |
---|
38 | #include "filelist.h" |
---|
39 | #include "debug.h" |
---|
40 | |
---|
41 | #ifdef HAVE_SIGNAL |
---|
42 | # ifdef HAVE_SETJMP |
---|
43 | # include <setjmp.h> |
---|
44 | static jmp_buf jmpbufSignal; |
---|
45 | # include <signal.h> |
---|
46 | # else |
---|
47 | # undef HAVE_SIGNAL |
---|
48 | # endif |
---|
49 | #endif |
---|
50 | |
---|
51 | #if (OS==WIN32) |
---|
52 | #include <windows.h> |
---|
53 | #endif |
---|
54 | |
---|
55 | /* This is the name of the default language. Add -DDEFAULTLANG to CFLAGS |
---|
56 | * e.g. with `CFLAGS="-DDEFAULTLANG=fr" ./configure' |
---|
57 | */ |
---|
58 | #ifndef DEFAULTLANG |
---|
59 | # define DEFAULTLANG "en" |
---|
60 | #endif |
---|
61 | |
---|
62 | /* For funcs which want to be immune from messing around with different |
---|
63 | * calling conventions */ |
---|
64 | #ifndef CDECL |
---|
65 | # define CDECL |
---|
66 | #endif |
---|
67 | |
---|
68 | int msg_warnings = 0; /* keep track of how many warnings we've given */ |
---|
69 | int msg_errors = 0; /* and how many (non-fatal) errors */ |
---|
70 | |
---|
71 | /* in case osmalloc() fails before szAppNameCopy is set up */ |
---|
72 | static const char *szAppNameCopy = "anonymous program"; |
---|
73 | |
---|
74 | /* error code for failed osmalloc and osrealloc calls */ |
---|
75 | static void |
---|
76 | outofmem(OSSIZE_T size) |
---|
77 | { |
---|
78 | fatalerror(/*Out of memory (couldn't find %lu bytes).*/1, |
---|
79 | (unsigned long)size); |
---|
80 | } |
---|
81 | |
---|
82 | #ifdef TOMBSTONES |
---|
83 | #define TOMBSTONE_SIZE 16 |
---|
84 | static const char tombstone[TOMBSTONE_SIZE] = "012345\xfftombstone"; |
---|
85 | #endif |
---|
86 | |
---|
87 | /* malloc with error catching if it fails. Also allows us to write special |
---|
88 | * versions easily eg for DOS EMS or MS Windows. |
---|
89 | */ |
---|
90 | void FAR * |
---|
91 | osmalloc(OSSIZE_T size) |
---|
92 | { |
---|
93 | void FAR *p; |
---|
94 | #ifdef TOMBSTONES |
---|
95 | size += TOMBSTONE_SIZE * 2; |
---|
96 | p = malloc(size); |
---|
97 | #else |
---|
98 | p = xosmalloc(size); |
---|
99 | #endif |
---|
100 | if (p == NULL) outofmem(size); |
---|
101 | #ifdef TOMBSTONES |
---|
102 | printf("osmalloc truep=%p truesize=%d\n", p, size); |
---|
103 | memcpy(p, tombstone, TOMBSTONE_SIZE); |
---|
104 | memcpy(p + size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE); |
---|
105 | *(size_t *)p = size; |
---|
106 | p += TOMBSTONE_SIZE; |
---|
107 | #endif |
---|
108 | return p; |
---|
109 | } |
---|
110 | |
---|
111 | /* realloc with error catching if it fails. */ |
---|
112 | void FAR * |
---|
113 | osrealloc(void *p, OSSIZE_T size) |
---|
114 | { |
---|
115 | /* some pre-ANSI realloc implementations don't cope with a NULL pointer */ |
---|
116 | if (p == NULL) { |
---|
117 | p = xosmalloc(size); |
---|
118 | } else { |
---|
119 | #ifdef TOMBSTONES |
---|
120 | int true_size; |
---|
121 | size += TOMBSTONE_SIZE * 2; |
---|
122 | p -= TOMBSTONE_SIZE; |
---|
123 | true_size = *(size_t *)p; |
---|
124 | printf("osrealloc (in truep=%p truesize=%d)\n", p, true_size); |
---|
125 | if (memcmp(p + sizeof(size_t), tombstone + sizeof(size_t), |
---|
126 | TOMBSTONE_SIZE - sizeof(size_t)) != 0) { |
---|
127 | printf("start tombstone for block %p, size %d corrupted!", |
---|
128 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
129 | } |
---|
130 | if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone, |
---|
131 | TOMBSTONE_SIZE) != 0) { |
---|
132 | printf("end tombstone for block %p, size %d corrupted!", |
---|
133 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
134 | } |
---|
135 | p = realloc(p, size); |
---|
136 | if (p == NULL) outofmem(size); |
---|
137 | printf("osrealloc truep=%p truesize=%d\n", p, size); |
---|
138 | memcpy(p, tombstone, TOMBSTONE_SIZE); |
---|
139 | memcpy(p + size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE); |
---|
140 | *(size_t *)p = size; |
---|
141 | p += TOMBSTONE_SIZE; |
---|
142 | #else |
---|
143 | p = xosrealloc(p, size); |
---|
144 | #endif |
---|
145 | } |
---|
146 | if (p == NULL) outofmem(size); |
---|
147 | return p; |
---|
148 | } |
---|
149 | |
---|
150 | char FAR * |
---|
151 | osstrdup(const char *str) |
---|
152 | { |
---|
153 | char *p; |
---|
154 | OSSIZE_T len; |
---|
155 | len = strlen(str) + 1; |
---|
156 | p = osmalloc(len); |
---|
157 | memcpy(p, str, len); |
---|
158 | return p; |
---|
159 | } |
---|
160 | |
---|
161 | /* osfree is usually just a macro in osalloc.h */ |
---|
162 | #ifdef TOMBSTONES |
---|
163 | void |
---|
164 | osfree(void *p) |
---|
165 | { |
---|
166 | int true_size; |
---|
167 | if (!p) return; |
---|
168 | p -= TOMBSTONE_SIZE; |
---|
169 | true_size = *(size_t *)p; |
---|
170 | printf("osfree truep=%p truesize=%d\n", p, true_size); |
---|
171 | if (memcmp(p + sizeof(size_t), tombstone + sizeof(size_t), |
---|
172 | TOMBSTONE_SIZE - sizeof(size_t)) != 0) { |
---|
173 | printf("start tombstone for block %p, size %d corrupted!", |
---|
174 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
175 | } |
---|
176 | if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone, |
---|
177 | TOMBSTONE_SIZE) != 0) { |
---|
178 | printf("end tombstone for block %p, size %d corrupted!", |
---|
179 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
180 | } |
---|
181 | free(p); |
---|
182 | } |
---|
183 | #endif |
---|
184 | |
---|
185 | #ifdef HAVE_SIGNAL |
---|
186 | |
---|
187 | static int sigReceived; |
---|
188 | |
---|
189 | /* for systems not using autoconf, assume the signal handler returns void |
---|
190 | * unless specified elsewhere */ |
---|
191 | #ifndef RETSIGTYPE |
---|
192 | # define RETSIGTYPE void |
---|
193 | #endif |
---|
194 | |
---|
195 | static CDECL RETSIGTYPE FAR |
---|
196 | report_sig(int sig) |
---|
197 | { |
---|
198 | sigReceived = sig; |
---|
199 | longjmp(jmpbufSignal, 1); |
---|
200 | } |
---|
201 | |
---|
202 | static void |
---|
203 | init_signals(void) |
---|
204 | { |
---|
205 | int en; |
---|
206 | if (!setjmp(jmpbufSignal)) { |
---|
207 | #if 1 /* disable these to get a core dump */ |
---|
208 | signal(SIGABRT, report_sig); /* abnormal termination eg abort() */ |
---|
209 | signal(SIGFPE, report_sig); /* arithmetic error eg /0 or overflow */ |
---|
210 | signal(SIGILL, report_sig); /* illegal function image eg illegal instruction */ |
---|
211 | signal(SIGSEGV, report_sig); /* illegal storage access eg access outside memory limits */ |
---|
212 | #endif |
---|
213 | # ifdef SIGSTAK /* only on RISC OS AFAIK */ |
---|
214 | signal(SIGSTAK, report_sig); /* stack overflow */ |
---|
215 | # endif |
---|
216 | return; |
---|
217 | } |
---|
218 | |
---|
219 | switch (sigReceived) { |
---|
220 | case SIGABRT: en = /*Abnormal termination*/90; break; |
---|
221 | case SIGFPE: en = /*Arithmetic error*/91; break; |
---|
222 | case SIGILL: en = /*Illegal instruction*/92; break; |
---|
223 | case SIGSEGV: en = /*Bad memory access*/94; break; |
---|
224 | # ifdef SIGSTAK |
---|
225 | case SIGSTAK: en = /*Stack overflow*/96; break; |
---|
226 | # endif |
---|
227 | default: en = /*Unknown signal received*/97; break; |
---|
228 | } |
---|
229 | fputsnl(msg(en), STDERR); |
---|
230 | |
---|
231 | /* Any of the signals we catch indicates a bug */ |
---|
232 | fatalerror(/*Bug in program detected! Please report this to the authors*/11); |
---|
233 | |
---|
234 | exit(EXIT_FAILURE); |
---|
235 | } |
---|
236 | #endif |
---|
237 | |
---|
238 | static int |
---|
239 | default_charset(void) |
---|
240 | { |
---|
241 | #if (OS==RISCOS) |
---|
242 | /* RISCOS 3.1 and above CHARSET_RISCOS31 (ISO_8859_1 + extras in 128-159) |
---|
243 | * RISCOS < 3.1 is ISO_8859_1 */ |
---|
244 | |
---|
245 | if (xwimpreadsysinfo_version(&version) != NULL) { |
---|
246 | /* RISC OS 2 or some error (don't care which) */ |
---|
247 | return CHARSET_ISO_8859_1; |
---|
248 | } |
---|
249 | |
---|
250 | /* oddly wimp_VERSION_RO3 is RISC OS 3.1 */ |
---|
251 | if (version < wimp_VERSION_RO3) return CHARSET_ISO_8859_1; |
---|
252 | |
---|
253 | return CHARSET_RISCOS31; |
---|
254 | #elif (OS==MSDOS) |
---|
255 | return CHARSET_DOSCP850; |
---|
256 | #else |
---|
257 | /* FIXME: assume ISO_8859_1 for now */ |
---|
258 | return CHARSET_ISO_8859_1; |
---|
259 | #endif |
---|
260 | } |
---|
261 | |
---|
262 | #if (OS==MSDOS) |
---|
263 | static int |
---|
264 | xlate_dos_cp850(int unicode) |
---|
265 | { |
---|
266 | switch (unicode) { |
---|
267 | #include "uni2dos.h" |
---|
268 | } |
---|
269 | return 0; |
---|
270 | } |
---|
271 | #endif |
---|
272 | |
---|
273 | static int |
---|
274 | add_unicode(int charset, unsigned char *p, int value) |
---|
275 | { |
---|
276 | #ifdef DEBUG |
---|
277 | fprintf(stderr, "add_unicode(%d, %p, %d)\n", charset, p, value); |
---|
278 | #endif |
---|
279 | if (value == 0) return 0; |
---|
280 | switch (charset) { |
---|
281 | case CHARSET_USASCII: |
---|
282 | if (value < 0x80) { |
---|
283 | *p = value; |
---|
284 | return 1; |
---|
285 | } |
---|
286 | break; |
---|
287 | case CHARSET_ISO_8859_1: |
---|
288 | if (value < 0x100) { |
---|
289 | *p = value; |
---|
290 | return 1; |
---|
291 | } |
---|
292 | break; |
---|
293 | #if (OS==RISCOS) |
---|
294 | case CHARSET_RISCOS31: |
---|
295 | /* RISC OS 3.1 (and later) extensions to ISO-8859-1 */ |
---|
296 | switch (value) { |
---|
297 | case 0x152: value = 0x9a; break; /* Œ */ |
---|
298 | case 0x153: value = 0x9b; break; /* œ */ |
---|
299 | case 0x174: value = 0x81; break; /* Ŵ */ |
---|
300 | case 0x175: value = 0x82; break; /* ŵ */ |
---|
301 | case 0x176: value = 0x85; break; /* Ŷ */ |
---|
302 | case 0x177: value = 0x86; break; /* ŷ */ |
---|
303 | } |
---|
304 | if (value < 0x100) { |
---|
305 | *p = value; |
---|
306 | return 1; |
---|
307 | } |
---|
308 | break; |
---|
309 | #endif |
---|
310 | #if (OS==MSDOS) |
---|
311 | case CHARSET_DOSCP850: |
---|
312 | value = xlate_dos_cp850(value); |
---|
313 | if (value) { |
---|
314 | *p = value; |
---|
315 | return 1; |
---|
316 | } |
---|
317 | break; |
---|
318 | #endif |
---|
319 | } |
---|
320 | return 0; |
---|
321 | } |
---|
322 | |
---|
323 | /* fall back on looking in the current directory */ |
---|
324 | static const char *pth_cfg_files = ""; |
---|
325 | |
---|
326 | static int num_msgs = 0; |
---|
327 | static char **msg_array = NULL; |
---|
328 | |
---|
329 | const char *msg_lang = NULL; |
---|
330 | const char *msg_lang2 = NULL; |
---|
331 | |
---|
332 | static void |
---|
333 | parse_msg_file(int charset_code) |
---|
334 | { |
---|
335 | FILE *fh; |
---|
336 | unsigned char header[20]; |
---|
337 | int i; |
---|
338 | unsigned len; |
---|
339 | unsigned char *p; |
---|
340 | char *fnm, *s; |
---|
341 | |
---|
342 | #ifdef DEBUG |
---|
343 | fprintf(stderr, "parse_msg_file(%d)\n", charset_code); |
---|
344 | #endif |
---|
345 | |
---|
346 | fnm = osstrdup(msg_lang); |
---|
347 | /* trim off charset from stuff like "de_DE.iso8859_1" */ |
---|
348 | s = strchr(fnm, '.'); |
---|
349 | if (s) *s = '\0'; |
---|
350 | |
---|
351 | fh = fopenWithPthAndExt(pth_cfg_files, fnm, EXT_SVX_MSG, "rb", NULL); |
---|
352 | |
---|
353 | if (!fh) { |
---|
354 | /* e.g. if 'en-COCKNEY' is unknown, see if we know 'en' */ |
---|
355 | if (strlen(fnm) > 3 && fnm[2] == '-') { |
---|
356 | fnm[2] = '\0'; |
---|
357 | fh = fopenWithPthAndExt(pth_cfg_files, fnm, EXT_SVX_MSG, "rb", NULL); |
---|
358 | if (!fh) fnm[2] = '-'; /* for error reporting */ |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | if (!fh) { |
---|
363 | fatalerror(/*Can't open message file `%s' using path `%s'*/1000, |
---|
364 | fnm, pth_cfg_files); |
---|
365 | } |
---|
366 | |
---|
367 | if (fread(header, 1, 20, fh) < 20 || |
---|
368 | memcmp(header, "Svx\nMsg\r\n\xfe\xff", 12) != 0) { |
---|
369 | fatalerror(/*Problem with message file `%s'*/1001, fnm); |
---|
370 | } |
---|
371 | |
---|
372 | if (header[12] != 0) |
---|
373 | fatalerror(/*I don't understand this message file version*/1002); |
---|
374 | |
---|
375 | num_msgs = (header[14] << 8) | header[15]; |
---|
376 | |
---|
377 | len = 0; |
---|
378 | for (i = 16; i < 20; i++) len = (len << 8) | header[i]; |
---|
379 | |
---|
380 | p = osmalloc(len); |
---|
381 | if (fread(p, 1, len, fh) < len) |
---|
382 | fatalerror(/*Message file truncated?*/1003); |
---|
383 | |
---|
384 | fclose(fh); |
---|
385 | |
---|
386 | #ifdef DEBUG |
---|
387 | fprintf(stderr, "fnm = `%s', num_msgs = %d, len = %d\n", fnm, num_msgs, len); |
---|
388 | #endif |
---|
389 | osfree(fnm); |
---|
390 | |
---|
391 | msg_array = osmalloc(sizeof(char *) * num_msgs); |
---|
392 | |
---|
393 | for (i = 0; i < num_msgs; i++) { |
---|
394 | unsigned char *to = p; |
---|
395 | int ch; |
---|
396 | msg_array[i] = (char *)p; |
---|
397 | |
---|
398 | /* If we want UTF8 anyway, we just need to find the start of each |
---|
399 | * message */ |
---|
400 | if (charset_code == CHARSET_UTF8) { |
---|
401 | p += strlen((char *)p) + 1; |
---|
402 | continue; |
---|
403 | } |
---|
404 | |
---|
405 | while ((ch = *p++) != 0) { |
---|
406 | /* A byte in the range 0x80-0xbf or 0xf0-0xff isn't valid in |
---|
407 | * this state, (0xf0-0xfd mean values > 0xffff) so treat as |
---|
408 | * literal and try to resync so we cope better when fed |
---|
409 | * non-utf-8 data. Similarly we abandon a multibyte sequence |
---|
410 | * if we hit an invalid character. */ |
---|
411 | if (ch >= 0xc0 && ch < 0xf0) { |
---|
412 | int ch1 = *p; |
---|
413 | if ((ch1 & 0xc0) != 0x80) goto resync; |
---|
414 | |
---|
415 | if (ch < 0xe0) { |
---|
416 | /* 2 byte sequence */ |
---|
417 | ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f); |
---|
418 | p++; |
---|
419 | } else { |
---|
420 | /* 3 byte sequence */ |
---|
421 | int ch2 = p[1]; |
---|
422 | if ((ch2 & 0xc0) != 0x80) goto resync; |
---|
423 | ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6) | (ch2 & 0x3f); |
---|
424 | p += 2; |
---|
425 | } |
---|
426 | } |
---|
427 | |
---|
428 | resync: |
---|
429 | |
---|
430 | if (ch < 127) { |
---|
431 | *to++ = (char)ch; |
---|
432 | } else { |
---|
433 | /* FIXME: this rather assumes a 2 byte UTF-8 code never |
---|
434 | * transliterates to more than 2 characters */ |
---|
435 | to += add_unicode(charset_code, to, ch); |
---|
436 | } |
---|
437 | } |
---|
438 | *to++ = '\0'; |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | const char * |
---|
443 | msg_cfgpth(void) |
---|
444 | { |
---|
445 | return pth_cfg_files; |
---|
446 | } |
---|
447 | |
---|
448 | void |
---|
449 | msg_init(const char *argv0) |
---|
450 | { |
---|
451 | char *p; |
---|
452 | |
---|
453 | #ifdef HAVE_SIGNAL |
---|
454 | init_signals(); |
---|
455 | #endif |
---|
456 | /* Point to argv0 itself so we report a more helpful error if the code to work |
---|
457 | * out the clean appname generates a signal */ |
---|
458 | szAppNameCopy = argv0; |
---|
459 | #if (OS == UNIX) |
---|
460 | /* use name as-is on Unix - programs run from path get name as supplied */ |
---|
461 | szAppNameCopy = osstrdup(argv0); |
---|
462 | #else |
---|
463 | /* use the lower-cased leafname on other platforms */ |
---|
464 | szAppNameCopy = p = leaf_from_fnm(argv0); |
---|
465 | while (*p) { |
---|
466 | *p = tolower(*p); |
---|
467 | p++; |
---|
468 | } |
---|
469 | #endif |
---|
470 | |
---|
471 | /* Look for env. var. "SURVEXHOME" or the like */ |
---|
472 | p = getenv("SURVEXHOME"); |
---|
473 | if (p && *p) { |
---|
474 | pth_cfg_files = osstrdup(p); |
---|
475 | #if (OS==UNIX) && defined(DATADIR) && defined(PACKAGE) |
---|
476 | } else { |
---|
477 | /* under Unix, we compile in the configured path */ |
---|
478 | pth_cfg_files = DATADIR "/" PACKAGE; |
---|
479 | #else |
---|
480 | } else if (argv0) { |
---|
481 | /* else try the path on argv[0] */ |
---|
482 | pth_cfg_files = path_from_fnm(argv0); |
---|
483 | #endif |
---|
484 | } |
---|
485 | |
---|
486 | msg_lang = getenv("SURVEXLANG"); |
---|
487 | #ifdef DEBUG |
---|
488 | fprintf(stderr, "msg_lang = %p (= \"%s\")\n", msg_lang, msg_lang?msg_lang:"(null)"); |
---|
489 | #endif |
---|
490 | |
---|
491 | if (!msg_lang || !*msg_lang) { |
---|
492 | msg_lang = getenv("LANG"); |
---|
493 | if (!msg_lang || !*msg_lang) { |
---|
494 | #if (OS==WIN32) |
---|
495 | LCID locid; |
---|
496 | #endif |
---|
497 | msg_lang = DEFAULTLANG; |
---|
498 | #if (OS==WIN32) |
---|
499 | locid = GetUserDefaultLCID(); |
---|
500 | if (locid) { |
---|
501 | WORD langid = LANGIDFROMLCID(locid); |
---|
502 | switch (PRIMARYLANGID(langid)) { |
---|
503 | case LANG_CATALAN: |
---|
504 | msg_lang = "ca"; |
---|
505 | break; |
---|
506 | case LANG_ENGLISH: |
---|
507 | if (SUBLANGID(langid) == SUBLANG_ENGLISH_US) |
---|
508 | msg_lang = "en_US"; |
---|
509 | else |
---|
510 | msg_lang = "en"; |
---|
511 | break; |
---|
512 | case LANG_FRENCH: |
---|
513 | msg_lang = "fr"; |
---|
514 | break; |
---|
515 | case LANG_GERMAN: |
---|
516 | switch (SUBLANGID(langid)) { |
---|
517 | case SUBLANG_GERMAN_SWISS: |
---|
518 | msg_lang = "de_CH"; |
---|
519 | break; |
---|
520 | case SUBLANG_GERMAN: |
---|
521 | msg_lang = "de_DE"; |
---|
522 | break; |
---|
523 | default: |
---|
524 | msg_lang = "de"; |
---|
525 | } |
---|
526 | break; |
---|
527 | case LANG_ITALIAN: |
---|
528 | msg_lang = "it"; |
---|
529 | break; |
---|
530 | case LANG_PORTUGUESE: |
---|
531 | if (SUBLANGID(langid) == SUBLANG_PORTUGUESE_BRAZILIAN) |
---|
532 | msg_lang = "pt_BR"; |
---|
533 | else |
---|
534 | msg_lang = "pt"; |
---|
535 | break; |
---|
536 | case LANG_SPANISH: |
---|
537 | msg_lang = "es"; |
---|
538 | break; |
---|
539 | } |
---|
540 | } |
---|
541 | #endif |
---|
542 | } |
---|
543 | } |
---|
544 | #ifdef DEBUG |
---|
545 | fprintf(stderr, "msg_lang = %p (= \"%s\")\n", msg_lang, msg_lang?msg_lang:"(null)"); |
---|
546 | #endif |
---|
547 | |
---|
548 | /* On Mandrake LANG defaults to C */ |
---|
549 | if (strcmp(msg_lang, "C") == 0) msg_lang = "en"; |
---|
550 | |
---|
551 | msg_lang = osstrdup(msg_lang); |
---|
552 | |
---|
553 | /* Convert en-us to en_US, etc */ |
---|
554 | p = strchr(msg_lang, '-'); |
---|
555 | if (p) { |
---|
556 | *p++ = '_'; |
---|
557 | while (*p) { |
---|
558 | *p = toupper(*p); |
---|
559 | p++; |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | p = strchr(msg_lang, '_'); |
---|
564 | if (p) { |
---|
565 | *p = '\0'; |
---|
566 | msg_lang2 = osstrdup(msg_lang); |
---|
567 | *p = '_'; |
---|
568 | } |
---|
569 | |
---|
570 | #ifdef LC_MESSAGES |
---|
571 | /* try to setlocale() appropriately too */ |
---|
572 | if (!setlocale(LC_MESSAGES, msg_lang)) { |
---|
573 | if (msg_lang2) setlocale(LC_MESSAGES, msg_lang2); |
---|
574 | } |
---|
575 | #endif |
---|
576 | |
---|
577 | select_charset(default_charset()); |
---|
578 | } |
---|
579 | |
---|
580 | /* no point extracting these errors as they won't get used if file opens */ |
---|
581 | /* FIXME: if DEFAULTLANG != "en" translate these... */ |
---|
582 | static const char *dontextract[] = { |
---|
583 | "Can't open message file `%s' using path `%s'", /*1000*/ |
---|
584 | "Problem with message file `%s'", /*1001*/ |
---|
585 | "I don't understand this message file version", /*1002*/ |
---|
586 | "Message file truncated?" /*1003*/ |
---|
587 | }; |
---|
588 | |
---|
589 | /* message may be overwritten by next call |
---|
590 | * (but not in current implementation) */ |
---|
591 | const char * |
---|
592 | msg(int en) |
---|
593 | { |
---|
594 | /* NB can't use ASSERT here! */ |
---|
595 | static char badbuf[256]; |
---|
596 | if (en >= 1000 && en < 1000 + (int)(sizeof(dontextract)/sizeof(char*))) |
---|
597 | return dontextract[en - 1000]; |
---|
598 | if (!msg_array) { |
---|
599 | if (en != 1) { |
---|
600 | sprintf(badbuf, "Message %d requested before msg_array initialised\n", en); |
---|
601 | return badbuf; |
---|
602 | } |
---|
603 | /* this should be the only message which can be requested before |
---|
604 | * the message file is opened and read... */ |
---|
605 | return "Out of memory (couldn't find %ul bytes).\n"; |
---|
606 | } |
---|
607 | |
---|
608 | if (en < 0 || en >= num_msgs) { |
---|
609 | sprintf(badbuf, "Message %d out of range\n", en); |
---|
610 | return badbuf; |
---|
611 | } |
---|
612 | |
---|
613 | return msg_array[en]; |
---|
614 | } |
---|
615 | |
---|
616 | /* returns persistent copy of message */ |
---|
617 | const char * |
---|
618 | msgPerm(int en) |
---|
619 | { |
---|
620 | return msg(en); |
---|
621 | } |
---|
622 | |
---|
623 | void |
---|
624 | v_report(int severity, const char *fnm, int line, int en, va_list ap) |
---|
625 | { |
---|
626 | #ifdef AVEN |
---|
627 | extern void aven_v_report(int severity, const char *fnm, int line, int en, |
---|
628 | va_list ap); |
---|
629 | aven_v_report(severity, fnm, line, en, ap); |
---|
630 | #else |
---|
631 | if (fnm) { |
---|
632 | fputs(fnm, STDERR); |
---|
633 | if (line) fprintf(STDERR, ":%d", line); |
---|
634 | } else { |
---|
635 | fputs(szAppNameCopy, STDERR); |
---|
636 | } |
---|
637 | fputs(": ", STDERR); |
---|
638 | |
---|
639 | if (severity == 0) { |
---|
640 | fputs(msg(/*warning*/4), STDERR); |
---|
641 | fputs(": ", STDERR); |
---|
642 | } |
---|
643 | |
---|
644 | vfprintf(STDERR, msg(en), ap); |
---|
645 | fputnl(STDERR); |
---|
646 | #endif |
---|
647 | |
---|
648 | switch (severity) { |
---|
649 | case 0: |
---|
650 | msg_warnings++; |
---|
651 | break; |
---|
652 | case 1: |
---|
653 | msg_errors++; |
---|
654 | if (msg_errors == 50) |
---|
655 | fatalerror_in_file(fnm, 0, /*Too many errors - giving up*/19); |
---|
656 | break; |
---|
657 | case 2: |
---|
658 | exit(EXIT_FAILURE); |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | void |
---|
663 | warning(int en, ...) |
---|
664 | { |
---|
665 | va_list ap; |
---|
666 | va_start(ap, en); |
---|
667 | v_report(0, NULL, 0, en, ap); |
---|
668 | va_end(ap); |
---|
669 | } |
---|
670 | |
---|
671 | void |
---|
672 | error(int en, ...) |
---|
673 | { |
---|
674 | va_list ap; |
---|
675 | va_start(ap, en); |
---|
676 | v_report(1, NULL, 0, en, ap); |
---|
677 | va_end(ap); |
---|
678 | } |
---|
679 | |
---|
680 | void |
---|
681 | fatalerror(int en, ...) |
---|
682 | { |
---|
683 | va_list ap; |
---|
684 | va_start(ap, en); |
---|
685 | v_report(2, NULL, 0, en, ap); |
---|
686 | va_end(ap); |
---|
687 | } |
---|
688 | |
---|
689 | void |
---|
690 | warning_in_file(const char *fnm, int line, int en, ...) |
---|
691 | { |
---|
692 | va_list ap; |
---|
693 | va_start(ap, en); |
---|
694 | v_report(0, fnm, line, en, ap); |
---|
695 | va_end(ap); |
---|
696 | } |
---|
697 | |
---|
698 | void |
---|
699 | error_in_file(const char *fnm, int line, int en, ...) |
---|
700 | { |
---|
701 | va_list ap; |
---|
702 | va_start(ap, en); |
---|
703 | v_report(1, fnm, line, en, ap); |
---|
704 | va_end(ap); |
---|
705 | } |
---|
706 | |
---|
707 | void |
---|
708 | fatalerror_in_file(const char *fnm, int line, int en, ...) |
---|
709 | { |
---|
710 | va_list ap; |
---|
711 | va_start(ap, en); |
---|
712 | v_report(2, fnm, line, en, ap); |
---|
713 | va_end(ap); |
---|
714 | } |
---|
715 | |
---|
716 | /* Code to support switching character set at runtime (e.g. for a printer |
---|
717 | * driver to support different character sets on screen and on the printer) |
---|
718 | */ |
---|
719 | typedef struct charset_li { |
---|
720 | struct charset_li *next; |
---|
721 | int code; |
---|
722 | char **msg_array; |
---|
723 | } charset_li; |
---|
724 | |
---|
725 | static charset_li *charset_head = NULL; |
---|
726 | |
---|
727 | static int charset = CHARSET_BAD; |
---|
728 | |
---|
729 | int |
---|
730 | select_charset(int charset_code) |
---|
731 | { |
---|
732 | int old_charset = charset; |
---|
733 | charset_li *p; |
---|
734 | |
---|
735 | #ifdef DEBUG |
---|
736 | fprintf(stderr, "select_charset(%d), old charset = %d\n", charset_code, |
---|
737 | charset); |
---|
738 | #endif |
---|
739 | |
---|
740 | charset = charset_code; |
---|
741 | |
---|
742 | /* check if we've already parsed messages for new charset */ |
---|
743 | for (p = charset_head; p; p = p->next) { |
---|
744 | #ifdef DEBUG |
---|
745 | printf("%p: code %d msg_array %p\n", p, p->code, p->msg_array); |
---|
746 | #endif |
---|
747 | if (p->code == charset) { |
---|
748 | msg_array = p->msg_array; |
---|
749 | return old_charset; |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | /* nope, got to reparse message file */ |
---|
754 | parse_msg_file(charset_code); |
---|
755 | |
---|
756 | /* add to list */ |
---|
757 | p = osnew(charset_li); |
---|
758 | p->code = charset; |
---|
759 | p->msg_array = msg_array; |
---|
760 | p->next = charset_head; |
---|
761 | charset_head = p; |
---|
762 | |
---|
763 | return old_charset; |
---|
764 | } |
---|