[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 | |
---|
[32232b2] | 343 | if (!lang || !*lang) { |
---|
| 344 | lang = getenv("LANG"); |
---|
| 345 | if (!lang || !*lang) lang = DEFAULTLANG; |
---|
| 346 | } |
---|
[55de792] | 347 | #ifdef DEBUG |
---|
| 348 | fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)"); |
---|
| 349 | #endif |
---|
[db75e18] | 350 | |
---|
| 351 | #if 1 |
---|
| 352 | /* backward compatibility - FIXME deprecate? */ |
---|
[d41b1353] | 353 | if (strcasecmp(lang, "engi") == 0) { |
---|
[db75e18] | 354 | lang = "en"; |
---|
[d41b1353] | 355 | } else if (strcasecmp(lang, "engu") == 0) { |
---|
[db75e18] | 356 | lang = "en-us"; |
---|
[d41b1353] | 357 | } else if (strcasecmp(lang, "fren") == 0) { |
---|
[db75e18] | 358 | lang = "fr"; |
---|
[d41b1353] | 359 | } else if (strcasecmp(lang, "germ") == 0) { |
---|
[db75e18] | 360 | lang = "de"; |
---|
[d41b1353] | 361 | } else if (strcasecmp(lang, "ital") == 0) { |
---|
[db75e18] | 362 | lang = "it"; |
---|
[d41b1353] | 363 | } else if (strcasecmp(lang, "span") == 0) { |
---|
[db75e18] | 364 | lang = "es"; |
---|
[d41b1353] | 365 | } else if (strcasecmp(lang, "cata") == 0) { |
---|
[db75e18] | 366 | lang = "ca"; |
---|
[d41b1353] | 367 | } else if (strcasecmp(lang, "port") == 0) { |
---|
[db75e18] | 368 | lang = "pt"; |
---|
[48e4121] | 369 | } |
---|
[db75e18] | 370 | #endif |
---|
[55de792] | 371 | #ifdef DEBUG |
---|
| 372 | fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)"); |
---|
| 373 | #endif |
---|
[db75e18] | 374 | |
---|
[f2a6ce4] | 375 | fh = fopenWithPthAndExt(pth_cfg_files, lang, EXT_SVX_MSG, "rb", NULL); |
---|
[db75e18] | 376 | |
---|
| 377 | if (!fh) { |
---|
| 378 | /* e.g. if 'en-COCKNEY' is unknown, see if we know 'en' */ |
---|
| 379 | if (strlen(lang) > 3 && lang[2] == '-') { |
---|
| 380 | char lang_generic[3]; |
---|
| 381 | lang_generic[0] = lang[0]; |
---|
| 382 | lang_generic[1] = lang[1]; |
---|
| 383 | lang_generic[2] = '\0'; |
---|
[f2a6ce4] | 384 | fh = fopenWithPthAndExt(pth_cfg_files, lang_generic, EXT_SVX_MSG, |
---|
| 385 | "rb", NULL); |
---|
[db75e18] | 386 | } |
---|
[48e4121] | 387 | } |
---|
[db75e18] | 388 | |
---|
| 389 | if (!fh) { |
---|
| 390 | /* no point extracting this error, as it won't get used if file opens */ |
---|
| 391 | fprintf(STDERR, "Can't open message file '%s' using path '%s'\n", |
---|
[f2a6ce4] | 392 | lang, pth_cfg_files); |
---|
[db75e18] | 393 | exit(EXIT_FAILURE); |
---|
[4432f2e] | 394 | } |
---|
[db75e18] | 395 | |
---|
| 396 | if (fread(header, 1, 20, fh) < 20 || |
---|
| 397 | memcmp(header, "Svx\nMsg\r\n\xfe\xff", 12) != 0) { |
---|
| 398 | /* no point extracting this error, as it won't get used if file opens */ |
---|
| 399 | fprintf(STDERR, "Problem with message file '%s'\n", lang); |
---|
| 400 | exit(EXIT_FAILURE); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | if (header[12] != 0) { |
---|
| 404 | /* no point extracting this error, as it won't get used if file opens */ |
---|
| 405 | fprintf(STDERR, "I don't understand this message file version\n"); |
---|
| 406 | exit(EXIT_FAILURE); |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | num_msgs = (header[14] << 8) | header[15]; |
---|
| 410 | |
---|
| 411 | len = 0; |
---|
| 412 | for (i = 16; i < 20; i++) len = (len << 8) | header[i]; |
---|
| 413 | |
---|
[55de792] | 414 | p = osmalloc(len); |
---|
| 415 | if (fread(p, 1, len, fh) < len) { |
---|
[db75e18] | 416 | /* no point extracting this error - it won't get used once file's read */ |
---|
| 417 | fprintf(STDERR, "Message file truncated?\n"); |
---|
| 418 | exit(EXIT_FAILURE); |
---|
| 419 | } |
---|
[a420b49] | 420 | fclose(fh); |
---|
[db75e18] | 421 | |
---|
[55de792] | 422 | #ifdef DEBUG |
---|
| 423 | fprintf(stderr, "lang = '%s', num_msgs = %d, len = %d\n", lang, num_msgs, len); |
---|
| 424 | #endif |
---|
| 425 | |
---|
| 426 | msg_array = osmalloc(sizeof(char *) * num_msgs); |
---|
| 427 | |
---|
[db75e18] | 428 | for (i = 0; i < num_msgs; i++) { |
---|
[55de792] | 429 | unsigned char *to = p; |
---|
[db75e18] | 430 | int ch; |
---|
[55de792] | 431 | msg_array[i] = (char *)p; |
---|
[db75e18] | 432 | while ((ch = *p++) != 0) { |
---|
| 433 | /* A byte in the range 0x80-0xbf or 0xf0-0xff isn't valid in |
---|
| 434 | * this state, (0xf0-0xfd mean values > 0xffff) so treat as |
---|
| 435 | * literal and try to resync so we cope better when fed |
---|
| 436 | * non-utf-8 data. Similarly we abandon a multibyte sequence |
---|
| 437 | * if we hit an invalid character. */ |
---|
| 438 | if (ch >= 0xc0 && ch < 0xf0) { |
---|
| 439 | int ch1 = *p; |
---|
| 440 | if ((ch1 & 0xc0) != 0x80) goto resync; |
---|
| 441 | |
---|
| 442 | if (ch < 0xe0) { |
---|
| 443 | /* 2 byte sequence */ |
---|
| 444 | ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f); |
---|
| 445 | p++; |
---|
| 446 | } else { |
---|
| 447 | /* 3 byte sequence */ |
---|
| 448 | int ch2 = p[1]; |
---|
| 449 | if ((ch2 & 0xc0) != 0x80) goto resync; |
---|
| 450 | ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6) | (ch2 & 0x3f); |
---|
| 451 | p += 2; |
---|
| 452 | } |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | resync: |
---|
| 456 | |
---|
| 457 | if (ch < 127) { |
---|
| 458 | *to++ = (char)ch; |
---|
| 459 | } else { |
---|
| 460 | /* FIXME this rather assumes a 2 byte UTF-8 code never |
---|
| 461 | * transliterates to more than 2 characters */ |
---|
| 462 | to += add_unicode(charset_code, to, ch); |
---|
| 463 | } |
---|
| 464 | } |
---|
| 465 | *to++ = '\0'; |
---|
| 466 | } |
---|
[4432f2e] | 467 | } |
---|
| 468 | |
---|
[f2a6ce4] | 469 | const char * |
---|
| 470 | msg_cfgpth(void) |
---|
| 471 | { |
---|
| 472 | return pth_cfg_files; |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | void |
---|
| 476 | msg_init(const char *argv0) |
---|
[db75e18] | 477 | { |
---|
| 478 | char *p; |
---|
| 479 | |
---|
| 480 | #ifdef HAVE_SIGNAL |
---|
| 481 | init_signals(); |
---|
| 482 | #endif |
---|
| 483 | /* This code *should* be completely bomb-proof even if strcpy |
---|
| 484 | * generates a signal |
---|
| 485 | */ |
---|
| 486 | szAppNameCopy = argv0; /* FIXME... */ |
---|
| 487 | szAppNameCopy = osstrdup(argv0); |
---|
| 488 | |
---|
| 489 | /* Look for env. var. "SURVEXHOME" or the like */ |
---|
| 490 | p = getenv("SURVEXHOME"); |
---|
| 491 | if (p && *p) { |
---|
[f2a6ce4] | 492 | pth_cfg_files = osstrdup(p); |
---|
[18f4759] | 493 | #if (OS==UNIX) && defined(DATADIR) && defined(PACKAGE) |
---|
[a420b49] | 494 | } else { |
---|
| 495 | /* under Unix, we compile in the configured path */ |
---|
[18f4759] | 496 | pth_cfg_files = DATADIR "/" PACKAGE; |
---|
[a420b49] | 497 | #else |
---|
[db75e18] | 498 | } else if (argv0) { |
---|
| 499 | /* else try the path on argv[0] */ |
---|
[f2a6ce4] | 500 | pth_cfg_files = path_from_fnm(argv0); |
---|
[a420b49] | 501 | #endif |
---|
[db75e18] | 502 | } |
---|
| 503 | |
---|
| 504 | select_charset(default_charset()); |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | /* message may be overwritten by next call (but not in current implementation) */ |
---|
| 508 | extern const char * |
---|
| 509 | msg(int en) |
---|
| 510 | { |
---|
| 511 | static const char *szBadEn = "???"; |
---|
| 512 | |
---|
[55de792] | 513 | if (!msg_array) { |
---|
[a420b49] | 514 | if (en != 1) return szBadEn; |
---|
| 515 | /* this should be the only message which can be requested before |
---|
| 516 | * the message file is opened and read... */ |
---|
| 517 | return "Out of memory (couldn't find %ul bytes).\n"; |
---|
[db75e18] | 518 | } |
---|
| 519 | |
---|
| 520 | if (en < 0 || en >= num_msgs) return szBadEn; |
---|
| 521 | |
---|
[55de792] | 522 | return msg_array[en]; |
---|
[db75e18] | 523 | } |
---|
| 524 | |
---|
| 525 | /* returns persistent copy of message */ |
---|
| 526 | extern const char * |
---|
| 527 | msgPerm(int en) |
---|
| 528 | { |
---|
| 529 | return msg(en); |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | void |
---|
| 533 | v_report(int severity, const char *fnm, int line, int en, va_list ap) |
---|
| 534 | { |
---|
| 535 | if (fnm) { |
---|
| 536 | fputs(fnm, STDERR); |
---|
| 537 | if (line) fprintf(STDERR, ":%d", line); |
---|
| 538 | } else { |
---|
| 539 | fputs(szAppNameCopy, STDERR); |
---|
| 540 | } |
---|
| 541 | fputs(": ", STDERR); |
---|
| 542 | |
---|
| 543 | if (severity == 0) { |
---|
| 544 | fputs(msg(/*warning*/4), STDERR); |
---|
| 545 | fputs(": ", STDERR); |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | vfprintf(STDERR, msg(en), ap); |
---|
| 549 | fputnl(STDERR); |
---|
| 550 | |
---|
| 551 | /* FIXME allow "warnings are errors" and/or "errors are fatal" */ |
---|
| 552 | switch (severity) { |
---|
| 553 | case 0: |
---|
| 554 | cWarnings++; |
---|
| 555 | break; |
---|
| 556 | case 1: |
---|
| 557 | cErrors++; |
---|
| 558 | if (cErrors == 50) |
---|
| 559 | fatalerror_in_file(fnm, 0, /*Too many errors - giving up*/19); |
---|
| 560 | break; |
---|
| 561 | case 2: |
---|
| 562 | exit(EXIT_FAILURE); |
---|
| 563 | } |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | void |
---|
| 567 | warning(int en, ...) |
---|
| 568 | { |
---|
| 569 | va_list ap; |
---|
| 570 | va_start(ap, en); |
---|
| 571 | v_report(0, NULL, 0, en, ap); |
---|
| 572 | va_end(ap); |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | void |
---|
| 576 | error(int en, ...) |
---|
| 577 | { |
---|
| 578 | va_list ap; |
---|
| 579 | va_start(ap, en); |
---|
| 580 | v_report(1, NULL, 0, en, ap); |
---|
| 581 | va_end(ap); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | void |
---|
| 585 | fatalerror(int en, ...) |
---|
| 586 | { |
---|
| 587 | va_list ap; |
---|
| 588 | va_start(ap, en); |
---|
| 589 | v_report(2, NULL, 0, en, ap); |
---|
| 590 | va_end(ap); |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | void |
---|
| 594 | warning_in_file(const char *fnm, int line, int en, ...) |
---|
| 595 | { |
---|
| 596 | va_list ap; |
---|
| 597 | va_start(ap, en); |
---|
| 598 | v_report(0, fnm, line, en, ap); |
---|
| 599 | va_end(ap); |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | void |
---|
| 603 | error_in_file(const char *fnm, int line, int en, ...) |
---|
| 604 | { |
---|
| 605 | va_list ap; |
---|
| 606 | va_start(ap, en); |
---|
| 607 | v_report(1, fnm, line, en, ap); |
---|
| 608 | va_end(ap); |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | void |
---|
| 612 | fatalerror_in_file(const char *fnm, int line, int en, ...) |
---|
| 613 | { |
---|
| 614 | va_list ap; |
---|
| 615 | va_start(ap, en); |
---|
| 616 | v_report(2, fnm, line, en, ap); |
---|
| 617 | va_end(ap); |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | /* Code to support switching character set at runtime (e.g. for a printer |
---|
| 621 | * driver to support different character sets on screen and on the printer) |
---|
| 622 | */ |
---|
| 623 | typedef struct charset_li { |
---|
| 624 | struct charset_li *next; |
---|
| 625 | int code; |
---|
[55de792] | 626 | char **msg_array; |
---|
[db75e18] | 627 | } charset_li; |
---|
| 628 | |
---|
| 629 | static charset_li *charset_head = NULL; |
---|
| 630 | |
---|
| 631 | static int charset = CHARSET_BAD; |
---|
| 632 | |
---|
| 633 | int |
---|
| 634 | select_charset(int charset_code) |
---|
| 635 | { |
---|
| 636 | int old_charset = charset; |
---|
| 637 | charset_li *p; |
---|
| 638 | |
---|
[55de792] | 639 | #ifdef DEBUG |
---|
| 640 | fprintf(stderr, "select_charset(%d), old charset = %d\n", charset_code, charset); |
---|
| 641 | #endif |
---|
| 642 | |
---|
[db75e18] | 643 | charset = charset_code; |
---|
| 644 | |
---|
| 645 | /* check if we've already parsed messages for new charset */ |
---|
| 646 | for (p = charset_head; p; p = p->next) { |
---|
[55de792] | 647 | #ifdef DEBUG |
---|
| 648 | printf("%p: code %d msg_array %p\n", p, p->code, p->msg_array); |
---|
| 649 | #endif |
---|
[db75e18] | 650 | if (p->code == charset) { |
---|
[55de792] | 651 | msg_array = p->msg_array; |
---|
[db75e18] | 652 | return old_charset; |
---|
| 653 | } |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | /* nope, got to reparse message file */ |
---|
| 657 | parse_msg_file(charset_code); |
---|
| 658 | |
---|
| 659 | /* add to list */ |
---|
| 660 | p = osnew(charset_li); |
---|
| 661 | p->code = charset; |
---|
[55de792] | 662 | p->msg_array = msg_array; |
---|
[db75e18] | 663 | p->next = charset_head; |
---|
| 664 | charset_head = p; |
---|
| 665 | |
---|
| 666 | return old_charset; |
---|
| 667 | } |
---|