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