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