1 | /* message.c |
---|
2 | * Fairly general purpose message and error routines |
---|
3 | * Copyright (C) 1993-2003,2004,2005,2006,2007,2010,2011,2012,2014 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 "cmdline.h" |
---|
35 | #include "whichos.h" |
---|
36 | #include "filename.h" |
---|
37 | #include "message.h" |
---|
38 | #include "osdepend.h" |
---|
39 | #include "filelist.h" |
---|
40 | #include "debug.h" |
---|
41 | |
---|
42 | #ifdef AVEN |
---|
43 | # include "aven.h" |
---|
44 | #endif |
---|
45 | |
---|
46 | #ifdef HAVE_SIGNAL |
---|
47 | # ifdef HAVE_SETJMP_H |
---|
48 | # include <setjmp.h> |
---|
49 | static jmp_buf jmpbufSignal; |
---|
50 | # include <signal.h> |
---|
51 | # else |
---|
52 | # undef HAVE_SIGNAL |
---|
53 | # endif |
---|
54 | #endif |
---|
55 | |
---|
56 | #if OS_WIN32 |
---|
57 | # define WIN32_LEAN_AND_MEAN |
---|
58 | # include <windows.h> |
---|
59 | #elif OS_UNIX |
---|
60 | # include <sys/types.h> |
---|
61 | # include <sys/stat.h> |
---|
62 | #endif |
---|
63 | |
---|
64 | /* For funcs which want to be immune from messing around with different |
---|
65 | * calling conventions */ |
---|
66 | #ifndef CDECL |
---|
67 | # define CDECL |
---|
68 | #endif |
---|
69 | |
---|
70 | int msg_warnings = 0; /* keep track of how many warnings we've given */ |
---|
71 | int msg_errors = 0; /* and how many (non-fatal) errors */ |
---|
72 | |
---|
73 | /* in case osmalloc() fails before appname_copy is set up */ |
---|
74 | static const char *appname_copy = "anonymous program"; |
---|
75 | |
---|
76 | /* Path to use to look for executables (used by aven to find cavern). */ |
---|
77 | static const char *exe_pth = ""; |
---|
78 | |
---|
79 | /* error code for failed osmalloc and osrealloc calls */ |
---|
80 | static void |
---|
81 | outofmem(OSSIZE_T size) |
---|
82 | { |
---|
83 | fatalerror(/*Out of memory (couldn’t find %lu bytes).*/1, |
---|
84 | (unsigned long)size); |
---|
85 | } |
---|
86 | |
---|
87 | #ifdef TOMBSTONES |
---|
88 | #define TOMBSTONE_SIZE 16 |
---|
89 | static const char tombstone[TOMBSTONE_SIZE] = "012345\xfftombstone"; |
---|
90 | #endif |
---|
91 | |
---|
92 | /* malloc with error catching if it fails. Also allows us to write special |
---|
93 | * versions easily eg for MS Windows. |
---|
94 | */ |
---|
95 | void * |
---|
96 | osmalloc(OSSIZE_T size) |
---|
97 | { |
---|
98 | void *p; |
---|
99 | #ifdef TOMBSTONES |
---|
100 | size += TOMBSTONE_SIZE * 2; |
---|
101 | p = malloc(size); |
---|
102 | #else |
---|
103 | p = xosmalloc(size); |
---|
104 | #endif |
---|
105 | if (p == NULL) outofmem(size); |
---|
106 | #ifdef TOMBSTONES |
---|
107 | printf("osmalloc truep=%p truesize=%d\n", p, size); |
---|
108 | memcpy(p, tombstone, TOMBSTONE_SIZE); |
---|
109 | memcpy(p + size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE); |
---|
110 | *(size_t *)p = size; |
---|
111 | p += TOMBSTONE_SIZE; |
---|
112 | #endif |
---|
113 | return p; |
---|
114 | } |
---|
115 | |
---|
116 | /* realloc with error catching if it fails. */ |
---|
117 | void * |
---|
118 | osrealloc(void *p, OSSIZE_T size) |
---|
119 | { |
---|
120 | /* some pre-ANSI realloc implementations don't cope with a NULL pointer */ |
---|
121 | if (p == NULL) { |
---|
122 | p = xosmalloc(size); |
---|
123 | } else { |
---|
124 | #ifdef TOMBSTONES |
---|
125 | int true_size; |
---|
126 | size += TOMBSTONE_SIZE * 2; |
---|
127 | p -= TOMBSTONE_SIZE; |
---|
128 | true_size = *(size_t *)p; |
---|
129 | printf("osrealloc (in truep=%p truesize=%d)\n", p, true_size); |
---|
130 | if (memcmp(p + sizeof(size_t), tombstone + sizeof(size_t), |
---|
131 | TOMBSTONE_SIZE - sizeof(size_t)) != 0) { |
---|
132 | printf("start tombstone for block %p, size %d corrupted!", |
---|
133 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
134 | } |
---|
135 | if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone, |
---|
136 | TOMBSTONE_SIZE) != 0) { |
---|
137 | printf("end tombstone for block %p, size %d corrupted!", |
---|
138 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
139 | } |
---|
140 | p = realloc(p, size); |
---|
141 | if (p == NULL) outofmem(size); |
---|
142 | printf("osrealloc truep=%p truesize=%d\n", p, size); |
---|
143 | memcpy(p, tombstone, TOMBSTONE_SIZE); |
---|
144 | memcpy(p + size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE); |
---|
145 | *(size_t *)p = size; |
---|
146 | p += TOMBSTONE_SIZE; |
---|
147 | #else |
---|
148 | p = xosrealloc(p, size); |
---|
149 | #endif |
---|
150 | } |
---|
151 | if (p == NULL) outofmem(size); |
---|
152 | return p; |
---|
153 | } |
---|
154 | |
---|
155 | char * |
---|
156 | osstrdup(const char *str) |
---|
157 | { |
---|
158 | char *p; |
---|
159 | OSSIZE_T len; |
---|
160 | len = strlen(str) + 1; |
---|
161 | p = osmalloc(len); |
---|
162 | memcpy(p, str, len); |
---|
163 | return p; |
---|
164 | } |
---|
165 | |
---|
166 | /* osfree is usually just a macro in osalloc.h */ |
---|
167 | #ifdef TOMBSTONES |
---|
168 | void |
---|
169 | osfree(void *p) |
---|
170 | { |
---|
171 | int true_size; |
---|
172 | if (!p) return; |
---|
173 | p -= TOMBSTONE_SIZE; |
---|
174 | true_size = *(size_t *)p; |
---|
175 | printf("osfree truep=%p truesize=%d\n", p, true_size); |
---|
176 | if (memcmp(p + sizeof(size_t), tombstone + sizeof(size_t), |
---|
177 | TOMBSTONE_SIZE - sizeof(size_t)) != 0) { |
---|
178 | printf("start tombstone for block %p, size %d corrupted!", |
---|
179 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
180 | } |
---|
181 | if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone, |
---|
182 | TOMBSTONE_SIZE) != 0) { |
---|
183 | printf("end tombstone for block %p, size %d corrupted!", |
---|
184 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
185 | } |
---|
186 | free(p); |
---|
187 | } |
---|
188 | #endif |
---|
189 | |
---|
190 | #ifdef HAVE_SIGNAL |
---|
191 | |
---|
192 | static int sigReceived; |
---|
193 | |
---|
194 | /* for systems not using autoconf, assume the signal handler returns void |
---|
195 | * unless specified elsewhere */ |
---|
196 | #ifndef RETSIGTYPE |
---|
197 | # define RETSIGTYPE void |
---|
198 | #endif |
---|
199 | |
---|
200 | static CDECL RETSIGTYPE |
---|
201 | report_sig(int sig) |
---|
202 | { |
---|
203 | sigReceived = sig; |
---|
204 | longjmp(jmpbufSignal, 1); |
---|
205 | } |
---|
206 | |
---|
207 | static void |
---|
208 | init_signals(void) |
---|
209 | { |
---|
210 | int en; |
---|
211 | if (!setjmp(jmpbufSignal)) { |
---|
212 | signal(SIGABRT, report_sig); /* abnormal termination eg abort() */ |
---|
213 | signal(SIGFPE, report_sig); /* arithmetic error eg /0 or overflow */ |
---|
214 | signal(SIGILL, report_sig); /* illegal function image eg illegal instruction */ |
---|
215 | signal(SIGSEGV, report_sig); /* illegal storage access eg access outside memory limits */ |
---|
216 | return; |
---|
217 | } |
---|
218 | |
---|
219 | /* Remove that signal handler to avoid the possibility of an infinite loop. |
---|
220 | */ |
---|
221 | signal(sigReceived, SIG_DFL); |
---|
222 | |
---|
223 | switch (sigReceived) { |
---|
224 | case SIGABRT: en = /*Abnormal termination*/90; break; |
---|
225 | case SIGFPE: en = /*Arithmetic error*/91; break; |
---|
226 | case SIGILL: en = /*Illegal instruction*/92; break; |
---|
227 | case SIGSEGV: en = /*Bad memory access*/94; break; |
---|
228 | default: en = /*Unknown signal received*/97; break; |
---|
229 | } |
---|
230 | fputsnl(msg(en), STDERR); |
---|
231 | |
---|
232 | /* Any of the signals we catch indicates a bug */ |
---|
233 | fatalerror(/*Bug in program detected! Please report this to the authors*/11); |
---|
234 | |
---|
235 | exit(EXIT_FAILURE); |
---|
236 | } |
---|
237 | #endif |
---|
238 | |
---|
239 | static int |
---|
240 | default_charset(void) |
---|
241 | { |
---|
242 | if (getenv("SURVEX_UTF8")) return CHARSET_UTF8; |
---|
243 | #if OS_WIN32 |
---|
244 | # ifdef AVEN |
---|
245 | # define CODEPAGE GetACP() |
---|
246 | # else |
---|
247 | # define CODEPAGE GetConsoleOutputCP() |
---|
248 | # endif |
---|
249 | switch (CODEPAGE) { |
---|
250 | case 0: return CHARSET_UTF8; |
---|
251 | case 1252: return CHARSET_WINCP1252; |
---|
252 | case 1250: return CHARSET_WINCP1250; |
---|
253 | case 850: return CHARSET_DOSCP850; |
---|
254 | } |
---|
255 | return CHARSET_USASCII; |
---|
256 | #elif OS_UNIX |
---|
257 | #ifdef AVEN |
---|
258 | return CHARSET_UTF8; |
---|
259 | #else |
---|
260 | const char *p = getenv("LC_ALL"); |
---|
261 | if (p == NULL || p[0] == '\0') { |
---|
262 | p = getenv("LC_CTYPE"); |
---|
263 | if (p == NULL || p[0] == '\0') { |
---|
264 | p = getenv("LANG"); |
---|
265 | /* Something (AutoCAD?) on Microsoft Windows sets LANG to a number. */ |
---|
266 | if (p == NULL || !isalpha((unsigned char)p[0])) |
---|
267 | p = msg_lang; |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | if (p) { |
---|
272 | char *q = strchr(p, '.'); |
---|
273 | if (q) p = q + 1; |
---|
274 | } |
---|
275 | |
---|
276 | if (p) { |
---|
277 | const char *chset = p; |
---|
278 | size_t name_len; |
---|
279 | |
---|
280 | while (*p != '\0' && *p != '@') p++; |
---|
281 | |
---|
282 | name_len = p - chset; |
---|
283 | |
---|
284 | if (name_len) { |
---|
285 | int only_digit = 1; |
---|
286 | size_t cnt; |
---|
287 | |
---|
288 | for (cnt = 0; cnt < name_len; ++cnt) |
---|
289 | if (isalpha((unsigned char)chset[cnt])) { |
---|
290 | only_digit = 0; |
---|
291 | break; |
---|
292 | } |
---|
293 | |
---|
294 | if (only_digit) goto iso; |
---|
295 | |
---|
296 | switch (tolower(chset[0])) { |
---|
297 | case 'i': |
---|
298 | if (tolower(chset[1]) == 's' && tolower(chset[2]) == 'o') { |
---|
299 | chset += 3; |
---|
300 | iso: |
---|
301 | if (strncmp(chset, "8859", 4) == 0) { |
---|
302 | chset += 4; |
---|
303 | while (chset < p && *chset && !isdigit((unsigned char)*chset)) |
---|
304 | chset++; |
---|
305 | switch (atoi(chset)) { |
---|
306 | case 1: return CHARSET_ISO_8859_1; |
---|
307 | case 2: return CHARSET_ISO_8859_2; |
---|
308 | case 15: return CHARSET_ISO_8859_15; |
---|
309 | default: return CHARSET_USASCII; |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|
313 | break; |
---|
314 | case 'u': |
---|
315 | if (tolower(chset[1]) == 't' && tolower(chset[2]) == 'f') { |
---|
316 | chset += 3; |
---|
317 | while (chset < p && *chset && !isdigit((unsigned char)*chset)) |
---|
318 | chset++; |
---|
319 | switch (atoi(chset)) { |
---|
320 | case 8: return CHARSET_UTF8; |
---|
321 | default: return CHARSET_USASCII; |
---|
322 | } |
---|
323 | } |
---|
324 | } |
---|
325 | } |
---|
326 | } |
---|
327 | return CHARSET_USASCII; |
---|
328 | #endif |
---|
329 | #else |
---|
330 | # error Do not know operating system! |
---|
331 | #endif |
---|
332 | } |
---|
333 | |
---|
334 | /* It seems that Swedish and maybe some other scandanavian languages don't |
---|
335 | * transliterate ä to ae - but it seems there may be conflicting views |
---|
336 | * on this... |
---|
337 | */ |
---|
338 | #define umlaut_to_e() 1 |
---|
339 | |
---|
340 | /* values <= 127 already dealt with */ |
---|
341 | static int |
---|
342 | add_unicode(int charset, unsigned char *p, int value) |
---|
343 | { |
---|
344 | #ifdef DEBUG |
---|
345 | fprintf(stderr, "add_unicode(%d, %p, %d)\n", charset, p, value); |
---|
346 | #endif |
---|
347 | if (value == 0) return 0; |
---|
348 | switch (charset) { |
---|
349 | case CHARSET_USASCII: |
---|
350 | if (value < 0x80) { |
---|
351 | *p = value; |
---|
352 | return 1; |
---|
353 | } |
---|
354 | break; |
---|
355 | case CHARSET_ISO_8859_1: |
---|
356 | if (value < 0x100) { |
---|
357 | *p = value; |
---|
358 | return 1; |
---|
359 | } |
---|
360 | break; |
---|
361 | case CHARSET_ISO_8859_2: |
---|
362 | if (value >= 0xa0) { |
---|
363 | int v = 0; |
---|
364 | switch (value) { |
---|
365 | case 0xa0: case 0xa4: case 0xa7: case 0xa8: case 0xad: case 0xb0: |
---|
366 | case 0xb4: case 0xb8: case 0xc1: case 0xc2: case 0xc4: case 0xc7: |
---|
367 | case 0xc9: case 0xcb: case 0xcd: case 0xce: case 0xd3: case 0xd4: |
---|
368 | case 0xd6: case 0xd7: case 0xda: case 0xdc: case 0xdd: case 0xdf: |
---|
369 | case 0xe1: case 0xe2: case 0xe4: case 0xe7: case 0xe9: case 0xeb: |
---|
370 | case 0xed: case 0xee: case 0xf3: case 0xf4: case 0xf6: case 0xf7: |
---|
371 | case 0xfa: case 0xfc: case 0xfd: |
---|
372 | v = value; break; |
---|
373 | case 0x104: v = '\xa1'; break; |
---|
374 | case 0x2d8: v = '\xa2'; break; |
---|
375 | case 0x141: v = '\xa3'; break; |
---|
376 | case 0x13d: v = '\xa5'; break; |
---|
377 | case 0x15a: v = '\xa6'; break; |
---|
378 | case 0x160: v = '\xa9'; break; |
---|
379 | case 0x15e: v = '\xaa'; break; /* Scedil */ |
---|
380 | case 0x164: v = '\xab'; break; |
---|
381 | case 0x179: v = '\xac'; break; |
---|
382 | case 0x17d: v = '\xae'; break; |
---|
383 | case 0x17b: v = '\xaf'; break; |
---|
384 | case 0x105: v = '\xb1'; break; |
---|
385 | case 0x2db: v = '\xb2'; break; |
---|
386 | case 0x142: v = '\xb3'; break; |
---|
387 | case 0x13e: v = '\xb5'; break; |
---|
388 | case 0x15b: v = '\xb6'; break; |
---|
389 | case 0x2c7: v = '\xb7'; break; |
---|
390 | case 0x161: v = '\xb9'; break; |
---|
391 | case 0x15f: v = '\xba'; break; /* scedil */ |
---|
392 | case 0x165: v = '\xbb'; break; |
---|
393 | case 0x17a: v = '\xbc'; break; |
---|
394 | case 0x2dd: v = '\xbd'; break; |
---|
395 | case 0x17e: v = '\xbe'; break; |
---|
396 | case 0x17c: v = '\xbf'; break; |
---|
397 | case 0x154: v = '\xc0'; break; |
---|
398 | case 0x102: v = '\xc3'; break; |
---|
399 | case 0x139: v = '\xc5'; break; |
---|
400 | case 0x106: v = '\xc6'; break; |
---|
401 | case 0x10c: v = '\xc8'; break; |
---|
402 | case 0x118: v = '\xca'; break; |
---|
403 | case 0x11a: v = '\xcc'; break; |
---|
404 | case 0x10e: v = '\xcf'; break; |
---|
405 | case 0x110: v = '\xd0'; break; |
---|
406 | case 0x143: v = '\xd1'; break; |
---|
407 | case 0x147: v = '\xd2'; break; |
---|
408 | case 0x150: v = '\xd5'; break; |
---|
409 | case 0x158: v = '\xd8'; break; |
---|
410 | case 0x16e: v = '\xd9'; break; |
---|
411 | case 0x170: v = '\xdb'; break; |
---|
412 | case 0x162: v = '\xde'; break; /* Ţ */ |
---|
413 | case 0x155: v = '\xe0'; break; |
---|
414 | case 0x103: v = '\xe3'; break; |
---|
415 | case 0x13a: v = '\xe5'; break; |
---|
416 | case 0x107: v = '\xe6'; break; |
---|
417 | case 0x10d: v = '\xe8'; break; |
---|
418 | case 0x119: v = '\xea'; break; |
---|
419 | case 0x11b: v = '\xec'; break; |
---|
420 | case 0x10f: v = '\xef'; break; |
---|
421 | case 0x111: v = '\xf0'; break; |
---|
422 | case 0x144: v = '\xf1'; break; |
---|
423 | case 0x148: v = '\xf2'; break; |
---|
424 | case 0x151: v = '\xf5'; break; |
---|
425 | case 0x159: v = '\xf8'; break; |
---|
426 | case 0x16f: v = '\xf9'; break; |
---|
427 | case 0x171: v = '\xfb'; break; |
---|
428 | case 0x163: v = '\xfe'; break; /* tcedil */ |
---|
429 | case 0x2d9: v = '\xff'; break; |
---|
430 | } |
---|
431 | if (v == 0) break; |
---|
432 | value = v; |
---|
433 | } |
---|
434 | *p = value; |
---|
435 | return 1; |
---|
436 | case CHARSET_ISO_8859_15: |
---|
437 | switch (value) { |
---|
438 | case 0xa4: case 0xa6: case 0xb0: case 0xc4: |
---|
439 | case 0xd0: case 0xd4: case 0xd5: case 0xd6: |
---|
440 | goto donthave; |
---|
441 | case 0x152: value = 0xd4; break; /* Œ */ |
---|
442 | case 0x153: value = 0xd5; break; /* œ */ |
---|
443 | #if 0 |
---|
444 | case 0x0: value = 0xa4; break; /* euro */ |
---|
445 | #endif |
---|
446 | case 0x160: value = 0xa6; break; /* Scaron */ |
---|
447 | case 0x161: value = 0xb0; break; /* scaron */ |
---|
448 | case 0x17d: value = 0xc4; break; /* Zcaron */ |
---|
449 | case 0x17e: value = 0xd0; break; /* zcaron */ |
---|
450 | #if 0 |
---|
451 | case 0x0: value = 0xd6; break; /* Ydiersis */ |
---|
452 | #endif |
---|
453 | } |
---|
454 | if (value < 0x100) { |
---|
455 | *p = value; |
---|
456 | return 1; |
---|
457 | } |
---|
458 | donthave: |
---|
459 | break; |
---|
460 | #if OS_WIN32 |
---|
461 | case CHARSET_WINCP1250: |
---|
462 | /* MS Windows rough equivalent to ISO-8859-2 */ |
---|
463 | if (value >= 0x80) { |
---|
464 | int v = 0; |
---|
465 | switch (value) { |
---|
466 | case 0xa0: case 0xa4: case 0xa6: case 0xa7: case 0xa8: case 0xa9: |
---|
467 | case 0xab: case 0xac: case 0xad: case 0xae: case 0xb0: case 0xb1: |
---|
468 | case 0xb4: case 0xb5: case 0xb6: case 0xb7: case 0xb8: case 0xbb: |
---|
469 | case 0xc1: case 0xc2: case 0xc4: case 0xc7: case 0xc9: case 0xcb: |
---|
470 | case 0xcd: case 0xce: case 0xd3: case 0xd4: case 0xd6: case 0xd7: |
---|
471 | case 0xda: case 0xdc: case 0xdd: case 0xdf: case 0xe1: case 0xe2: |
---|
472 | case 0xe4: case 0xe7: case 0xe9: case 0xeb: case 0xed: case 0xee: |
---|
473 | case 0xf3: case 0xf4: case 0xf6: case 0xf7: case 0xfa: case 0xfc: |
---|
474 | case 0xfd: |
---|
475 | v = value; break; |
---|
476 | case 0x20ac: v = '\x80'; break; |
---|
477 | case 0x201a: v = '\x82'; break; |
---|
478 | case 0x201e: v = '\x84'; break; |
---|
479 | case 0x2026: v = '\x85'; break; |
---|
480 | case 0x2020: v = '\x86'; break; |
---|
481 | case 0x2021: v = '\x87'; break; |
---|
482 | case 0x2030: v = '\x89'; break; |
---|
483 | case 0x0160: v = '\x8a'; break; |
---|
484 | case 0x2039: v = '\x8b'; break; |
---|
485 | case 0x015a: v = '\x8c'; break; |
---|
486 | case 0x0164: v = '\x8d'; break; |
---|
487 | case 0x017d: v = '\x8e'; break; |
---|
488 | case 0x0179: v = '\x8f'; break; |
---|
489 | case 0x2018: v = '\x91'; break; |
---|
490 | case 0x2019: v = '\x92'; break; |
---|
491 | case 0x201c: v = '\x93'; break; |
---|
492 | case 0x201d: v = '\x94'; break; |
---|
493 | case 0x2022: v = '\x95'; break; |
---|
494 | case 0x2013: v = '\x96'; break; |
---|
495 | case 0x2014: v = '\x97'; break; |
---|
496 | case 0x2122: v = '\x99'; break; |
---|
497 | case 0x0161: v = '\x9a'; break; |
---|
498 | case 0x203a: v = '\x9b'; break; |
---|
499 | case 0x015b: v = '\x9c'; break; |
---|
500 | case 0x0165: v = '\x9d'; break; |
---|
501 | case 0x017e: v = '\x9e'; break; |
---|
502 | case 0x017a: v = '\x9f'; break; |
---|
503 | case 0x02c7: v = '\xa1'; break; |
---|
504 | case 0x02d8: v = '\xa2'; break; |
---|
505 | case 0x0141: v = '\xa3'; break; |
---|
506 | case 0x0104: v = '\xa5'; break; |
---|
507 | case 0x015e: v = '\xaa'; break; /* Scedil */ |
---|
508 | case 0x017b: v = '\xaf'; break; |
---|
509 | case 0x02db: v = '\xb2'; break; |
---|
510 | case 0x0142: v = '\xb3'; break; |
---|
511 | case 0x0105: v = '\xb9'; break; |
---|
512 | case 0x015f: v = '\xba'; break; /* scedil */ |
---|
513 | case 0x013d: v = '\xbc'; break; |
---|
514 | case 0x02dd: v = '\xbd'; break; |
---|
515 | case 0x013e: v = '\xbe'; break; |
---|
516 | case 0x017c: v = '\xbf'; break; |
---|
517 | case 0x0154: v = '\xc0'; break; |
---|
518 | case 0x0102: v = '\xc3'; break; |
---|
519 | case 0x0139: v = '\xc5'; break; |
---|
520 | case 0x0106: v = '\xc6'; break; |
---|
521 | case 0x010c: v = '\xc8'; break; |
---|
522 | case 0x0118: v = '\xca'; break; |
---|
523 | case 0x011a: v = '\xcc'; break; |
---|
524 | case 0x010e: v = '\xcf'; break; |
---|
525 | case 0x0110: v = '\xd0'; break; |
---|
526 | case 0x0143: v = '\xd1'; break; |
---|
527 | case 0x0147: v = '\xd2'; break; |
---|
528 | case 0x0150: v = '\xd5'; break; |
---|
529 | case 0x0158: v = '\xd8'; break; |
---|
530 | case 0x016e: v = '\xd9'; break; |
---|
531 | case 0x0170: v = '\xdb'; break; |
---|
532 | case 0x0162: v = '\xde'; break; /* Ţ */ |
---|
533 | case 0x0155: v = '\xe0'; break; |
---|
534 | case 0x0103: v = '\xe3'; break; |
---|
535 | case 0x013a: v = '\xe5'; break; |
---|
536 | case 0x0107: v = '\xe6'; break; |
---|
537 | case 0x010d: v = '\xe8'; break; |
---|
538 | case 0x0119: v = '\xea'; break; |
---|
539 | case 0x011b: v = '\xec'; break; |
---|
540 | case 0x010f: v = '\xef'; break; |
---|
541 | case 0x0111: v = '\xf0'; break; |
---|
542 | case 0x0144: v = '\xf1'; break; |
---|
543 | case 0x0148: v = '\xf2'; break; |
---|
544 | case 0x0151: v = '\xf5'; break; |
---|
545 | case 0x0159: v = '\xf8'; break; |
---|
546 | case 0x016f: v = '\xf9'; break; |
---|
547 | case 0x0171: v = '\xfb'; break; |
---|
548 | case 0x0163: v = '\xfe'; break; /* tcedil */ |
---|
549 | case 0x02d9: v = '\xff'; break; |
---|
550 | } |
---|
551 | if (v == 0) break; |
---|
552 | value = v; |
---|
553 | } |
---|
554 | *p = value; |
---|
555 | return 1; |
---|
556 | case CHARSET_WINCP1252: |
---|
557 | /* MS Windows extensions to ISO-8859-1 */ |
---|
558 | switch (value) { |
---|
559 | case 0x2026: value = 0x85; break; /* hellip */ |
---|
560 | case 0x160: value = 0x8a; break; /* Scaron */ |
---|
561 | case 0x152: value = 0x8c; break; /* OElig */ |
---|
562 | case 0x17d: value = 0x8e; break; /* Zcaron */ |
---|
563 | case 0x161: value = 0x9a; break; /* scaron */ |
---|
564 | case 0x153: value = 0x9c; break; /* oelig */ |
---|
565 | case 0x17e: value = 0x9e; break; /* zcaron */ |
---|
566 | #if 0 |
---|
567 | /* there are a few other obscure ones we don't currently need */ |
---|
568 | #endif |
---|
569 | } |
---|
570 | if (value < 0x100) { |
---|
571 | *p = value; |
---|
572 | return 1; |
---|
573 | } |
---|
574 | break; |
---|
575 | #endif |
---|
576 | #if OS_WIN32 |
---|
577 | case CHARSET_DOSCP850: { |
---|
578 | unsigned char uni2dostab[] = { |
---|
579 | 255, 173, 189, 156, 207, 190, 221, 245, |
---|
580 | 249, 184, 166, 174, 170, 240, 169, 238, |
---|
581 | 248, 241, 253, 252, 239, 230, 244, 250, |
---|
582 | 247, 251, 167, 175, 172, 171, 243, 168, |
---|
583 | 183, 181, 182, 199, 142, 143, 146, 128, |
---|
584 | 212, 144, 210, 211, 222, 214, 215, 216, |
---|
585 | 209, 165, 227, 224, 226, 229, 153, 158, |
---|
586 | 157, 235, 233, 234, 154, 237, 232, 225, |
---|
587 | 133, 160, 131, 198, 132, 134, 145, 135, |
---|
588 | 138, 130, 136, 137, 141, 161, 140, 139, |
---|
589 | 208, 164, 149, 162, 147, 228, 148, 246, |
---|
590 | 155, 151, 163, 150, 129, 236, 231, 152 |
---|
591 | }; |
---|
592 | if (value >= 160 && value < 256) { |
---|
593 | *p = (int)uni2dostab[value - 160]; |
---|
594 | return 1; |
---|
595 | } |
---|
596 | #if 0 |
---|
597 | if (value == 305) { /* LATIN SMALL LETTER DOTLESS I */ |
---|
598 | *p = 213; |
---|
599 | return 1; |
---|
600 | } |
---|
601 | if (value == 402) { /* LATIN SMALL LETTER F WITH HOOK */ |
---|
602 | *p = 159; |
---|
603 | return 1; |
---|
604 | } |
---|
605 | #endif |
---|
606 | break; |
---|
607 | } |
---|
608 | #endif |
---|
609 | } |
---|
610 | /* Transliterate characters we can't represent */ |
---|
611 | #ifdef DEBUG |
---|
612 | fprintf(stderr, "transliterate “%c” 0x%x\n", value, value); |
---|
613 | #endif |
---|
614 | switch (value) { |
---|
615 | case 160: |
---|
616 | *p = ' '; return 1; |
---|
617 | case 161 /* ¡ */: |
---|
618 | *p = '!'; return 1; |
---|
619 | case 176 /* ° */: |
---|
620 | *p = 'd'; p[1] = 'g'; return 2; |
---|
621 | case 191 /* ¿ */: |
---|
622 | *p = '?'; return 1; |
---|
623 | case 192 /* À */: case 193 /* Á */: case 194 /* Â */: case 195 /* Ã */: |
---|
624 | *p = 'A'; return 1; |
---|
625 | case 197 /* Å */: |
---|
626 | p[1] = *p = 'A'; return 2; |
---|
627 | case 196 /* Ä */: /* Ä */ |
---|
628 | *p = 'A'; |
---|
629 | if (!umlaut_to_e()) return 1; |
---|
630 | p[1] = 'E'; return 2; |
---|
631 | case 198 /* Æ */: |
---|
632 | *p = 'A'; p[1] = 'E'; return 2; |
---|
633 | case 199 /* Ç */: case 268: /* Č */ |
---|
634 | *p = 'C'; return 1; |
---|
635 | case 270: /* Ď */ |
---|
636 | *p = 'D'; return 1; |
---|
637 | case 200 /* È */: case 201 /* É */: case 202 /* Ê */: case 203 /* Ë */: |
---|
638 | *p = 'E'; return 1; |
---|
639 | case 204 /* Ì */: case 205 /* Í */: case 206 /* Î */: case 207 /* Ï */: |
---|
640 | *p = 'I'; return 1; |
---|
641 | case 208 /* Ð */: case 222 /* Þ */: |
---|
642 | *p = 'T'; p[1] = 'H'; return 2; |
---|
643 | case 315: /* Ĺ */ |
---|
644 | case 317: /* Ľ */ |
---|
645 | *p = 'L'; return 1; |
---|
646 | case 209 /* Ñ */: |
---|
647 | *p = 'N'; return 1; |
---|
648 | case 210 /* Ò */: case 211 /* Ó */: case 212 /* Ô */: case 213 /* Õ */: |
---|
649 | *p = 'O'; return 1; |
---|
650 | case 214 /* Ö */: /* Ö */ case 0x152: /* Œ */ |
---|
651 | *p = 'O'; p[1] = 'E'; return 2; |
---|
652 | case 352: /* Š */ |
---|
653 | case 0x15e: /* Ş */ |
---|
654 | *p = 'S'; return 1; |
---|
655 | case 0x162: /* Ţ */ |
---|
656 | case 0x164: /* Ť */ |
---|
657 | *p = 'T'; return 1; |
---|
658 | case 217 /* Ù */: case 218 /* Ú */: case 219 /* Û */: |
---|
659 | *p = 'U'; return 1; |
---|
660 | case 220 /* Ü */: /* Ü */ |
---|
661 | *p = 'U'; p[1] = 'E'; return 2; |
---|
662 | case 221 /* Ý */: |
---|
663 | *p = 'Y'; return 1; |
---|
664 | case 381: /* Ž */ |
---|
665 | *p = 'Z'; return 1; |
---|
666 | case 223 /* ß */: |
---|
667 | p[1] = *p = 's'; return 2; |
---|
668 | case 224 /* à */: case 225 /* á */: case 226 /* â */: case 227 /* ã */: |
---|
669 | case 259: /* ă */ |
---|
670 | *p = 'a'; return 1; |
---|
671 | case 228 /* ä */: /* ä */ case 230 /* æ */: |
---|
672 | *p = 'a'; p[1] = 'e'; return 2; |
---|
673 | case 229 /* å */: |
---|
674 | p[1] = *p = 'a'; return 2; |
---|
675 | case 231 /* ç */: case 269 /* č */: |
---|
676 | *p = 'c'; return 1; |
---|
677 | case 271: /* ď */ |
---|
678 | *p = 'd'; return 1; |
---|
679 | case 232 /* è */: case 233 /* é */: case 234 /* ê */: case 235 /* ë */: |
---|
680 | case 283 /* ě */: |
---|
681 | *p = 'e'; return 1; |
---|
682 | case 236 /* ì */: case 237 /* í */: case 238 /* î */: case 239 /* ï */: |
---|
683 | *p = 'i'; return 1; |
---|
684 | case 316 /* ĺ */: |
---|
685 | case 318 /* ľ */: |
---|
686 | *p = 'l'; return 1; |
---|
687 | case 241 /* ñ */: case 328 /* ň */: |
---|
688 | *p = 'n'; return 1; |
---|
689 | case 345: /* ř */ |
---|
690 | *p = 'r'; return 1; |
---|
691 | case 353: /* š */ |
---|
692 | case 0x15f: /* ş */ |
---|
693 | *p = 's'; return 1; |
---|
694 | case 357: /* ť */ |
---|
695 | case 0x163: /* ţ */ |
---|
696 | *p = 't'; return 1; |
---|
697 | case 240 /* ð */: case 254 /* þ */: |
---|
698 | *p = 't'; p[1] = 'h'; return 2; |
---|
699 | case 242 /* ò */: case 243 /* ó */: case 244 /* ô */: case 245 /* õ */: |
---|
700 | *p = 'o'; return 1; |
---|
701 | case 246 /* ö */: /* ö */ case 0x153: /* œ */ |
---|
702 | *p = 'o'; p[1] = 'e'; return 2; |
---|
703 | case 249 /* ù */: case 250 /* ú */: case 251 /* û */: |
---|
704 | case 367 /* ů */: |
---|
705 | *p = 'u'; return 1; |
---|
706 | case 252 /* ü */: /* ü */ |
---|
707 | *p = 'u'; p[1] = 'e'; return 2; |
---|
708 | case 253 /* ý */: case 255 /* ÿ */: |
---|
709 | *p = 'y'; return 1; |
---|
710 | case 382: /* ž */ |
---|
711 | *p = 'z'; return 1; |
---|
712 | case 0x2019: /* ‘ */ |
---|
713 | *p = '\''; return 1; |
---|
714 | case 171: /* « */ case 187: /* » */ |
---|
715 | case 0x201c: /* “ */ case 0x201d: /* ” */ |
---|
716 | *p = '"'; return 1; |
---|
717 | case 0x2026: /* … */ |
---|
718 | *p = '.'; p[1] = '.'; p[2] = '.'; return 3; |
---|
719 | case 0x2192: /* → */ |
---|
720 | *p = '-'; p[1] = '>'; return 2; |
---|
721 | } |
---|
722 | #ifdef DEBUG |
---|
723 | fprintf(stderr, "failed to transliterate\n"); |
---|
724 | #endif |
---|
725 | return 0; |
---|
726 | } |
---|
727 | |
---|
728 | #if OS_UNIX && defined DATADIR && defined PACKAGE |
---|
729 | /* Under Unix, we compile in the configured path */ |
---|
730 | static const char *pth_cfg_files = DATADIR "/" PACKAGE; |
---|
731 | #else |
---|
732 | /* On other platforms, we fall back on looking in the current directory */ |
---|
733 | static const char *pth_cfg_files = ""; |
---|
734 | #endif |
---|
735 | |
---|
736 | static int num_msgs = 0; |
---|
737 | static char **msg_array = NULL; |
---|
738 | |
---|
739 | static bool msg_lang_explicit = fFalse; |
---|
740 | const char *msg_lang = NULL; |
---|
741 | const char *msg_lang2 = NULL; |
---|
742 | |
---|
743 | static char ** |
---|
744 | parse_msgs(int n, unsigned char *p, int charset_code) { |
---|
745 | int i; |
---|
746 | |
---|
747 | char **msgs = osmalloc(n * sizeof(char *)); |
---|
748 | |
---|
749 | for (i = 0; i < n; i++) { |
---|
750 | unsigned char *to = p; |
---|
751 | int ch; |
---|
752 | msgs[i] = (char *)p; |
---|
753 | |
---|
754 | /* If we want UTF8 anyway, we just need to find the start of each |
---|
755 | * message */ |
---|
756 | if (charset_code == CHARSET_UTF8) { |
---|
757 | p += strlen((char *)p) + 1; |
---|
758 | continue; |
---|
759 | } |
---|
760 | |
---|
761 | while ((ch = *p++) != 0) { |
---|
762 | /* A byte in the range 0x80-0xbf or 0xf0-0xff isn't valid in |
---|
763 | * this state, (0xf0-0xfd mean values > 0xffff) so treat as |
---|
764 | * literal and try to resync so we cope better when fed |
---|
765 | * non-utf-8 data. Similarly we abandon a multibyte sequence |
---|
766 | * if we hit an invalid character. */ |
---|
767 | if (ch >= 0xc0 && ch < 0xf0) { |
---|
768 | int ch1 = *p; |
---|
769 | if ((ch1 & 0xc0) != 0x80) goto resync; |
---|
770 | |
---|
771 | if (ch < 0xe0) { |
---|
772 | /* 2 byte sequence */ |
---|
773 | ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f); |
---|
774 | p++; |
---|
775 | } else { |
---|
776 | /* 3 byte sequence */ |
---|
777 | int ch2 = p[1]; |
---|
778 | if ((ch2 & 0xc0) != 0x80) goto resync; |
---|
779 | ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6) | (ch2 & 0x3f); |
---|
780 | p += 2; |
---|
781 | } |
---|
782 | } |
---|
783 | |
---|
784 | resync: |
---|
785 | |
---|
786 | if (ch < 127) { |
---|
787 | *to++ = (char)ch; |
---|
788 | } else { |
---|
789 | /* We assume an N byte UTF-8 code never transliterates to more |
---|
790 | * than N characters (so we can't transliterate © to (C) or |
---|
791 | * ® to (R) for example) */ |
---|
792 | to += add_unicode(charset_code, to, ch); |
---|
793 | } |
---|
794 | } |
---|
795 | *to++ = '\0'; |
---|
796 | } |
---|
797 | return msgs; |
---|
798 | } |
---|
799 | |
---|
800 | /* This is the name of the default language, which can be set like so: |
---|
801 | * ./configure --enable-defaultlang=fr |
---|
802 | */ |
---|
803 | #ifdef DEFAULTLANG |
---|
804 | /* No point extracting these errors as they won't get used if file opens */ |
---|
805 | # include "../lib/defaultlang.h" |
---|
806 | #else |
---|
807 | #define N_DONTEXTRACTMSGS 5 |
---|
808 | static unsigned char dontextractmsgs[] = |
---|
809 | "Can't open message file \"%s\" using path \"%s\"\0"/*1000*/ |
---|
810 | "Problem with message file \"%s\"\0"/*1001*/ |
---|
811 | "I don't understand this message file version\0"/*1002*/ |
---|
812 | "Message file truncated?\0"/*1003*/ |
---|
813 | "Out of memory (couldn't find %lu bytes).\0"/*1004*/; |
---|
814 | #endif |
---|
815 | |
---|
816 | static char **dontextract = NULL; |
---|
817 | |
---|
818 | static void |
---|
819 | parse_msg_file(int charset_code) |
---|
820 | { |
---|
821 | FILE *fh; |
---|
822 | unsigned char header[20]; |
---|
823 | int i; |
---|
824 | unsigned len; |
---|
825 | unsigned char *p; |
---|
826 | char *fnm, *s; |
---|
827 | int n; |
---|
828 | |
---|
829 | #ifdef DEBUG |
---|
830 | fprintf(stderr, "parse_msg_file(%d)\n", charset_code); |
---|
831 | #endif |
---|
832 | |
---|
833 | /* sort out messages we need to print if we can't open the message file */ |
---|
834 | dontextract = parse_msgs(N_DONTEXTRACTMSGS, dontextractmsgs, charset_code); |
---|
835 | |
---|
836 | fnm = osstrdup(msg_lang); |
---|
837 | /* trim off charset from stuff like "de_DE.iso8859_1" */ |
---|
838 | s = strchr(fnm, '.'); |
---|
839 | if (s) *s = '\0'; |
---|
840 | |
---|
841 | fh = fopenWithPthAndExt(pth_cfg_files, fnm, EXT_SVX_MSG, "rb", NULL); |
---|
842 | |
---|
843 | if (!fh && strlen(fnm) > 3 && fnm[2] == '_') { |
---|
844 | /* e.g. if 'en_GB' is unknown, see if we know 'en' */ |
---|
845 | fnm[2] = '\0'; |
---|
846 | fh = fopenWithPthAndExt(pth_cfg_files, fnm, EXT_SVX_MSG, "rb", NULL); |
---|
847 | if (!fh) fnm[2] = '_'; /* for error reporting */ |
---|
848 | } |
---|
849 | |
---|
850 | if (!fh && !msg_lang_explicit) { |
---|
851 | /* If msg_lang wasn't specified using environment variable SURVEX_LANG, |
---|
852 | * then default to 'en' if we don't find messages for language msg_lang. |
---|
853 | */ |
---|
854 | if (fnm[0] && fnm[1]) { |
---|
855 | strcpy(fnm, "en"); |
---|
856 | } else { |
---|
857 | osfree(fnm); |
---|
858 | fnm = osstrdup("en"); |
---|
859 | } |
---|
860 | fh = fopenWithPthAndExt(pth_cfg_files, fnm, EXT_SVX_MSG, "rb", NULL); |
---|
861 | } |
---|
862 | |
---|
863 | if (!fh) { |
---|
864 | fatalerror(/*Can't open message file “%s” using path “%s”*/1000, |
---|
865 | fnm, pth_cfg_files); |
---|
866 | } |
---|
867 | |
---|
868 | if (fread(header, 1, 20, fh) < 20 || |
---|
869 | memcmp(header, "Svx\nMsg\r\n\xfe\xff", 12) != 0) { |
---|
870 | fatalerror(/*Problem with message file “%s”*/1001, fnm); |
---|
871 | } |
---|
872 | |
---|
873 | if (header[12] != 0) |
---|
874 | fatalerror(/*I don't understand this message file version*/1002); |
---|
875 | |
---|
876 | n = (header[14] << 8) | header[15]; |
---|
877 | |
---|
878 | len = 0; |
---|
879 | for (i = 16; i < 20; i++) len = (len << 8) | header[i]; |
---|
880 | |
---|
881 | p = osmalloc(len); |
---|
882 | if (fread(p, 1, len, fh) < len) |
---|
883 | fatalerror(/*Message file truncated?*/1003); |
---|
884 | |
---|
885 | fclose(fh); |
---|
886 | |
---|
887 | #ifdef DEBUG |
---|
888 | fprintf(stderr, "fnm = “%s”, n = %d, len = %d\n", fnm, n, len); |
---|
889 | #endif |
---|
890 | osfree(fnm); |
---|
891 | |
---|
892 | msg_array = parse_msgs(n, p, charset_code); |
---|
893 | num_msgs = n; |
---|
894 | } |
---|
895 | |
---|
896 | const char * |
---|
897 | msg_cfgpth(void) |
---|
898 | { |
---|
899 | return pth_cfg_files; |
---|
900 | } |
---|
901 | |
---|
902 | const char * |
---|
903 | msg_exepth(void) |
---|
904 | { |
---|
905 | return exe_pth; |
---|
906 | } |
---|
907 | |
---|
908 | const char * |
---|
909 | msg_appname(void) |
---|
910 | { |
---|
911 | return appname_copy; |
---|
912 | } |
---|
913 | |
---|
914 | void |
---|
915 | msg_init(char * const *argv) |
---|
916 | { |
---|
917 | char *p; |
---|
918 | SVX_ASSERT(argv); |
---|
919 | |
---|
920 | /* Point to argv[0] itself so we report a more helpful error if the |
---|
921 | * code to work out the clean appname generates a signal */ |
---|
922 | appname_copy = argv[0]; |
---|
923 | #if OS_UNIX |
---|
924 | /* use name as-is on Unix - programs run from path get name as supplied */ |
---|
925 | appname_copy = osstrdup(argv[0]); |
---|
926 | #else |
---|
927 | /* use the lower-cased leafname on other platforms */ |
---|
928 | p = leaf_from_fnm(argv[0]); |
---|
929 | appname_copy = p; |
---|
930 | while (*p) { |
---|
931 | *p = tolower(*p); |
---|
932 | ++p; |
---|
933 | } |
---|
934 | #endif |
---|
935 | |
---|
936 | /* shortcut --version so you can check the version number even when the |
---|
937 | * correct message file can't be found... */ |
---|
938 | if (argv[1] && strcmp(argv[1], "--version") == 0) { |
---|
939 | cmdline_version(); |
---|
940 | exit(0); |
---|
941 | } |
---|
942 | if (argv[0]) { |
---|
943 | exe_pth = path_from_fnm(argv[0]); |
---|
944 | #ifdef MACOSX_BUNDLE |
---|
945 | /* If we're being built into a bundle, always look relative to |
---|
946 | * the path to the binary. */ |
---|
947 | #ifdef AVEN |
---|
948 | /* Aven is packaged as an application, so we must look inside there. */ |
---|
949 | pth_cfg_files = use_path(exe_pth, "../Resources"); |
---|
950 | #else |
---|
951 | pth_cfg_files = use_path(exe_pth, "share/survex"); |
---|
952 | #endif |
---|
953 | #elif OS_UNIX && defined DATADIR && defined PACKAGE |
---|
954 | bool free_pth = fFalse; |
---|
955 | char *pth = getenv("srcdir"); |
---|
956 | if (!pth || !pth[0]) { |
---|
957 | pth = path_from_fnm(argv[0]); |
---|
958 | free_pth = fTrue; |
---|
959 | } |
---|
960 | if (pth[0]) { |
---|
961 | struct stat buf; |
---|
962 | #if OS_UNIX_MACOSX |
---|
963 | /* On MacOS X the programs may be installed anywhere, with the |
---|
964 | * share directory and the binaries in the same directory. */ |
---|
965 | p = use_path(pth, "share/survex/en.msg"); |
---|
966 | if (lstat(p, &buf) == 0 && S_ISREG(buf.st_mode)) { |
---|
967 | pth_cfg_files = use_path(pth, "share/survex"); |
---|
968 | goto macosx_got_msg; |
---|
969 | } |
---|
970 | osfree(p); |
---|
971 | /* The cavern which aven runs is a hardlinked copy alongside |
---|
972 | * the aven binary. |
---|
973 | */ |
---|
974 | p = use_path(pth, "../Resources/en.msg"); |
---|
975 | if (lstat(p, &buf) == 0 && S_ISREG(buf.st_mode)) { |
---|
976 | pth_cfg_files = use_path(pth, "../Resources"); |
---|
977 | goto macosx_got_msg; |
---|
978 | } |
---|
979 | osfree(p); |
---|
980 | #endif |
---|
981 | /* If we're run with an explicit path, check if "../lib/en.msg" |
---|
982 | * from the program's path exists, and if so look there for |
---|
983 | * support files - this allows us to test binaries in the build |
---|
984 | * tree easily. */ |
---|
985 | p = use_path(pth, "../lib/en.msg"); |
---|
986 | if (lstat(p, &buf) == 0) { |
---|
987 | #ifdef S_ISREG |
---|
988 | /* POSIX way */ |
---|
989 | if (S_ISREG(buf.st_mode)) { |
---|
990 | pth_cfg_files = use_path(pth, "../lib"); |
---|
991 | } |
---|
992 | #else |
---|
993 | /* BSD way */ |
---|
994 | if ((buf.st_mode & S_IFMT) == S_IFREG) { |
---|
995 | pth_cfg_files = use_path(pth, "../lib"); |
---|
996 | } |
---|
997 | #endif |
---|
998 | } |
---|
999 | #if defined(__GNUC__) && defined(__APPLE_CC__) |
---|
1000 | macosx_got_msg: |
---|
1001 | #endif |
---|
1002 | osfree(p); |
---|
1003 | } |
---|
1004 | |
---|
1005 | if (free_pth) osfree(pth); |
---|
1006 | #elif OS_WIN32 |
---|
1007 | DWORD len = 256; |
---|
1008 | char *buf = NULL, *modname; |
---|
1009 | while (1) { |
---|
1010 | DWORD got; |
---|
1011 | buf = osrealloc(buf, len); |
---|
1012 | got = GetModuleFileName(NULL, buf, len); |
---|
1013 | if (got < len) break; |
---|
1014 | len += len; |
---|
1015 | } |
---|
1016 | modname = buf; |
---|
1017 | /* Strange Win32 nastiness - strip prefix "\\?\" if present */ |
---|
1018 | if (strncmp(modname, "\\\\?\\", 4) == 0) modname += 4; |
---|
1019 | pth_cfg_files = path_from_fnm(modname); |
---|
1020 | osfree(buf); |
---|
1021 | #else |
---|
1022 | /* Get the path to the support files from argv[0] */ |
---|
1023 | pth_cfg_files = path_from_fnm(argv[0]); |
---|
1024 | #endif |
---|
1025 | } |
---|
1026 | |
---|
1027 | msg_lang = getenv("SURVEXLANG"); |
---|
1028 | #ifdef DEBUG |
---|
1029 | fprintf(stderr, "msg_lang = %p (= \"%s\")\n", msg_lang, msg_lang?msg_lang:"(null)"); |
---|
1030 | #endif |
---|
1031 | |
---|
1032 | msg_lang_explicit = fTrue; |
---|
1033 | if (!msg_lang || !*msg_lang) { |
---|
1034 | msg_lang_explicit = fFalse; |
---|
1035 | msg_lang = getenv("LC_ALL"); |
---|
1036 | } |
---|
1037 | if (!msg_lang || !*msg_lang) { |
---|
1038 | msg_lang = getenv("LC_MESSAGES"); |
---|
1039 | if (!msg_lang || !*msg_lang) { |
---|
1040 | msg_lang = getenv("LANG"); |
---|
1041 | /* Something (AutoCAD?) on Microsoft Windows sets LANG to a number. */ |
---|
1042 | if (msg_lang && !isalpha(msg_lang[0])) msg_lang = NULL; |
---|
1043 | } |
---|
1044 | if (!msg_lang || !*msg_lang) { |
---|
1045 | #if OS_WIN32 |
---|
1046 | LCID locid; |
---|
1047 | #endif |
---|
1048 | #ifdef DEFAULTLANG |
---|
1049 | msg_lang = STRING(DEFAULTLANG); |
---|
1050 | #else |
---|
1051 | msg_lang = "en"; |
---|
1052 | #endif |
---|
1053 | #if OS_WIN32 |
---|
1054 | locid = GetUserDefaultLCID(); |
---|
1055 | if (locid) { |
---|
1056 | WORD langid = LANGIDFROMLCID(locid); |
---|
1057 | switch (PRIMARYLANGID(langid)) { |
---|
1058 | case LANG_BULGARIAN: |
---|
1059 | msg_lang = "bg"; |
---|
1060 | break; |
---|
1061 | /* older mingw compilers don't seem to supply this value */ |
---|
1062 | #ifndef LANG_CATALAN |
---|
1063 | # define LANG_CATALAN 0x03 |
---|
1064 | #endif |
---|
1065 | case LANG_CATALAN: |
---|
1066 | msg_lang = "ca"; |
---|
1067 | break; |
---|
1068 | case LANG_CHINESE: |
---|
1069 | msg_lang = "zh"; |
---|
1070 | break; |
---|
1071 | case LANG_ENGLISH: |
---|
1072 | if (SUBLANGID(langid) == SUBLANG_ENGLISH_US) |
---|
1073 | msg_lang = "en_US"; |
---|
1074 | else |
---|
1075 | msg_lang = "en"; |
---|
1076 | break; |
---|
1077 | case LANG_FRENCH: |
---|
1078 | msg_lang = "fr"; |
---|
1079 | break; |
---|
1080 | case LANG_GERMAN: |
---|
1081 | switch (SUBLANGID(langid)) { |
---|
1082 | case SUBLANG_GERMAN_SWISS: |
---|
1083 | msg_lang = "de_CH"; |
---|
1084 | break; |
---|
1085 | case SUBLANG_GERMAN: |
---|
1086 | msg_lang = "de_DE"; |
---|
1087 | break; |
---|
1088 | default: |
---|
1089 | msg_lang = "de"; |
---|
1090 | } |
---|
1091 | break; |
---|
1092 | case LANG_ITALIAN: |
---|
1093 | msg_lang = "it"; |
---|
1094 | break; |
---|
1095 | case LANG_PORTUGUESE: |
---|
1096 | if (SUBLANGID(langid) == SUBLANG_PORTUGUESE_BRAZILIAN) |
---|
1097 | msg_lang = "pt_BR"; |
---|
1098 | else |
---|
1099 | msg_lang = "pt"; |
---|
1100 | break; |
---|
1101 | case LANG_ROMANIAN: |
---|
1102 | msg_lang = "ro"; |
---|
1103 | break; |
---|
1104 | case LANG_SLOVAK: |
---|
1105 | msg_lang = "sk"; |
---|
1106 | break; |
---|
1107 | case LANG_SPANISH: |
---|
1108 | msg_lang = "es"; |
---|
1109 | break; |
---|
1110 | } |
---|
1111 | } |
---|
1112 | #endif |
---|
1113 | } |
---|
1114 | } |
---|
1115 | #ifdef DEBUG |
---|
1116 | fprintf(stderr, "msg_lang = %p (= \"%s\")\n", msg_lang, msg_lang?msg_lang:"(null)"); |
---|
1117 | #endif |
---|
1118 | |
---|
1119 | /* On Mandrake LANG defaults to C */ |
---|
1120 | if (strcmp(msg_lang, "C") == 0) msg_lang = "en"; |
---|
1121 | |
---|
1122 | msg_lang = osstrdup(msg_lang); |
---|
1123 | |
---|
1124 | /* Convert en-us to en_US, etc */ |
---|
1125 | p = strchr(msg_lang, '-'); |
---|
1126 | if (p) { |
---|
1127 | *p++ = '_'; |
---|
1128 | while (*p) { |
---|
1129 | *p = toupper(*p); |
---|
1130 | p++; |
---|
1131 | } |
---|
1132 | } |
---|
1133 | |
---|
1134 | p = strchr(msg_lang, '_'); |
---|
1135 | if (p) { |
---|
1136 | *p = '\0'; |
---|
1137 | msg_lang2 = osstrdup(msg_lang); |
---|
1138 | *p = '_'; |
---|
1139 | } |
---|
1140 | |
---|
1141 | #ifdef LC_MESSAGES |
---|
1142 | /* try to setlocale() appropriately too */ |
---|
1143 | if (!setlocale(LC_MESSAGES, msg_lang)) { |
---|
1144 | if (msg_lang2) { |
---|
1145 | (void)setlocale(LC_MESSAGES, msg_lang2); |
---|
1146 | } |
---|
1147 | } |
---|
1148 | #endif |
---|
1149 | |
---|
1150 | select_charset(default_charset()); |
---|
1151 | |
---|
1152 | #ifdef HAVE_SIGNAL |
---|
1153 | /* Initialise signal handlers only after the messages have been as we need |
---|
1154 | * the messages to usefully handle the signals. */ |
---|
1155 | init_signals(); |
---|
1156 | #endif |
---|
1157 | } |
---|
1158 | |
---|
1159 | /* Message may be overwritten by next call |
---|
1160 | * (but not in current implementation) */ |
---|
1161 | const char * |
---|
1162 | msg(int en) |
---|
1163 | { |
---|
1164 | /* NB can't use SVX_ASSERT here! */ |
---|
1165 | static char badbuf[256]; |
---|
1166 | if (dontextract && en >= 1000 && en < 1000 + N_DONTEXTRACTMSGS) |
---|
1167 | return dontextract[en - 1000]; |
---|
1168 | if (!msg_array) { |
---|
1169 | if (en != 1) { |
---|
1170 | sprintf(badbuf, "Message %d requested before fully initialised\n", en); |
---|
1171 | return badbuf; |
---|
1172 | } |
---|
1173 | /* this should be the only other message which can be requested before |
---|
1174 | * the message file is opened and read... */ |
---|
1175 | if (!dontextract) return "Out of memory (couldn't find %lu bytes)."; |
---|
1176 | return dontextract[(/*Out of memory (couldn't find %lu bytes).*/1004) |
---|
1177 | - 1000]; |
---|
1178 | } |
---|
1179 | |
---|
1180 | if (en < 0 || en >= num_msgs) { |
---|
1181 | sprintf(badbuf, "Message %d out of range\n", en); |
---|
1182 | return badbuf; |
---|
1183 | } |
---|
1184 | |
---|
1185 | if (en == 0) { |
---|
1186 | const char *p = msg_array[0]; |
---|
1187 | if (!*p) p = "(C)"; |
---|
1188 | return p; |
---|
1189 | } |
---|
1190 | |
---|
1191 | return msg_array[en]; |
---|
1192 | } |
---|
1193 | |
---|
1194 | /* returns persistent copy of message */ |
---|
1195 | const char * |
---|
1196 | msgPerm(int en) |
---|
1197 | { |
---|
1198 | return msg(en); |
---|
1199 | } |
---|
1200 | |
---|
1201 | void |
---|
1202 | v_report(int severity, const char *fnm, int line, int col, int en, va_list ap) |
---|
1203 | { |
---|
1204 | #ifdef AVEN |
---|
1205 | (void)col; |
---|
1206 | aven_v_report(severity, fnm, line, en, ap); |
---|
1207 | #else |
---|
1208 | if (fnm) { |
---|
1209 | fputs(fnm, STDERR); |
---|
1210 | if (line) fprintf(STDERR, ":%d", line); |
---|
1211 | if (col > 0) fprintf(STDERR, ":%d", col); |
---|
1212 | } else { |
---|
1213 | fputs(appname_copy, STDERR); |
---|
1214 | } |
---|
1215 | fputs(": ", STDERR); |
---|
1216 | |
---|
1217 | if (severity == 0) { |
---|
1218 | fputs(msg(/*warning*/4), STDERR); |
---|
1219 | fputs(": ", STDERR); |
---|
1220 | } |
---|
1221 | |
---|
1222 | vfprintf(STDERR, msg(en), ap); |
---|
1223 | fputnl(STDERR); |
---|
1224 | #endif |
---|
1225 | |
---|
1226 | switch (severity) { |
---|
1227 | case 0: |
---|
1228 | msg_warnings++; |
---|
1229 | break; |
---|
1230 | case 1: |
---|
1231 | msg_errors++; |
---|
1232 | if (msg_errors == 50) |
---|
1233 | fatalerror_in_file(fnm, 0, /*Too many errors - giving up*/19); |
---|
1234 | break; |
---|
1235 | case 2: |
---|
1236 | exit(EXIT_FAILURE); |
---|
1237 | } |
---|
1238 | } |
---|
1239 | |
---|
1240 | void |
---|
1241 | warning(int en, ...) |
---|
1242 | { |
---|
1243 | va_list ap; |
---|
1244 | va_start(ap, en); |
---|
1245 | v_report(0, NULL, 0, 0, en, ap); |
---|
1246 | va_end(ap); |
---|
1247 | } |
---|
1248 | |
---|
1249 | void |
---|
1250 | error(int en, ...) |
---|
1251 | { |
---|
1252 | va_list ap; |
---|
1253 | va_start(ap, en); |
---|
1254 | v_report(1, NULL, 0, 0, en, ap); |
---|
1255 | va_end(ap); |
---|
1256 | } |
---|
1257 | |
---|
1258 | void |
---|
1259 | fatalerror(int en, ...) |
---|
1260 | { |
---|
1261 | va_list ap; |
---|
1262 | va_start(ap, en); |
---|
1263 | v_report(2, NULL, 0, 0, en, ap); |
---|
1264 | va_end(ap); |
---|
1265 | } |
---|
1266 | |
---|
1267 | void |
---|
1268 | warning_in_file(const char *fnm, int line, int en, ...) |
---|
1269 | { |
---|
1270 | va_list ap; |
---|
1271 | va_start(ap, en); |
---|
1272 | v_report(0, fnm, line, 0, en, ap); |
---|
1273 | va_end(ap); |
---|
1274 | } |
---|
1275 | |
---|
1276 | void |
---|
1277 | error_in_file(const char *fnm, int line, int en, ...) |
---|
1278 | { |
---|
1279 | va_list ap; |
---|
1280 | va_start(ap, en); |
---|
1281 | v_report(1, fnm, line, 0, en, ap); |
---|
1282 | va_end(ap); |
---|
1283 | } |
---|
1284 | |
---|
1285 | void |
---|
1286 | fatalerror_in_file(const char *fnm, int line, int en, ...) |
---|
1287 | { |
---|
1288 | va_list ap; |
---|
1289 | va_start(ap, en); |
---|
1290 | v_report(2, fnm, line, 0, en, ap); |
---|
1291 | va_end(ap); |
---|
1292 | } |
---|
1293 | |
---|
1294 | /* Code to support switching character set at runtime (e.g. for a printer |
---|
1295 | * driver to support different character sets on screen and on the printer) |
---|
1296 | */ |
---|
1297 | typedef struct charset_li { |
---|
1298 | struct charset_li *next; |
---|
1299 | int code; |
---|
1300 | char **msg_array; |
---|
1301 | } charset_li; |
---|
1302 | |
---|
1303 | static charset_li *charset_head = NULL; |
---|
1304 | |
---|
1305 | static int charset = CHARSET_BAD; |
---|
1306 | |
---|
1307 | int |
---|
1308 | select_charset(int charset_code) |
---|
1309 | { |
---|
1310 | int old_charset = charset; |
---|
1311 | charset_li *p; |
---|
1312 | |
---|
1313 | #ifdef DEBUG |
---|
1314 | fprintf(stderr, "select_charset(%d), old charset = %d\n", charset_code, |
---|
1315 | charset); |
---|
1316 | #endif |
---|
1317 | |
---|
1318 | charset = charset_code; |
---|
1319 | |
---|
1320 | /* check if we've already parsed messages for new charset */ |
---|
1321 | for (p = charset_head; p; p = p->next) { |
---|
1322 | #ifdef DEBUG |
---|
1323 | printf("%p: code %d msg_array %p\n", p, p->code, p->msg_array); |
---|
1324 | #endif |
---|
1325 | if (p->code == charset) { |
---|
1326 | msg_array = p->msg_array; |
---|
1327 | return old_charset; |
---|
1328 | } |
---|
1329 | } |
---|
1330 | |
---|
1331 | /* nope, got to reparse message file */ |
---|
1332 | parse_msg_file(charset_code); |
---|
1333 | |
---|
1334 | /* add to list */ |
---|
1335 | p = osnew(charset_li); |
---|
1336 | p->code = charset; |
---|
1337 | p->msg_array = msg_array; |
---|
1338 | p->next = charset_head; |
---|
1339 | charset_head = p; |
---|
1340 | |
---|
1341 | return old_charset; |
---|
1342 | } |
---|