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