[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 | #define CHARSET_BAD -1 |
---|
| 259 | #define CHARSET_USASCII 0 |
---|
| 260 | #define CHARSET_ISO_8859_1 1 |
---|
| 261 | #define CHARSET_DOSCP850 2 |
---|
| 262 | #define CHARSET_RISCOS31 3 |
---|
| 263 | static int default_charset( void ) { |
---|
| 264 | #ifdef ISO8859_1 |
---|
| 265 | return CHARSET_ISO_8859_1; |
---|
| 266 | #elif (OS==RISCOS) |
---|
[2ca296b] | 267 | /* RISCOS 3.1 and above CHARSET_RISCOS31 (ISO_8859_1 + extras in 128-159) |
---|
[647407d] | 268 | * FIXME: RISCOS < 3.1 is ISO_8859_1 */ |
---|
[2ca296b] | 269 | return CHARSET_RISCOS31; |
---|
[60f7018] | 270 | #elif (OS==MSDOS) |
---|
| 271 | return CHARSET_DOSCP850; |
---|
| 272 | #else |
---|
[647407d] | 273 | return CHARSET_ISO_8859_1; /* FIXME: Look at env var CHARSET ? */ |
---|
[60f7018] | 274 | #endif |
---|
| 275 | } |
---|
| 276 | |
---|
[48e4121] | 277 | #if (OS==MSDOS) |
---|
[db75e18] | 278 | static int |
---|
| 279 | xlate_dos_cp850(int unicode) |
---|
| 280 | { |
---|
[48e4121] | 281 | switch (unicode) { |
---|
[d41b1353] | 282 | #include "uni2dos.h" |
---|
[48e4121] | 283 | } |
---|
| 284 | return 0; |
---|
| 285 | } |
---|
| 286 | #endif |
---|
| 287 | |
---|
[db75e18] | 288 | static int |
---|
[55de792] | 289 | add_unicode(int charset, unsigned char *p, int value) |
---|
[db75e18] | 290 | { |
---|
[55de792] | 291 | #ifdef DEBUG |
---|
| 292 | fprintf(stderr, "add_unicode(%d, %p, %d)\n", charset, p, value); |
---|
| 293 | #endif |
---|
[4432f2e] | 294 | if (value == 0) return 0; |
---|
[48e4121] | 295 | switch (charset) { |
---|
[db75e18] | 296 | case CHARSET_USASCII: |
---|
[48e4121] | 297 | if (value < 128) { |
---|
| 298 | *p = value; |
---|
| 299 | return 1; |
---|
| 300 | } |
---|
| 301 | break; |
---|
[db75e18] | 302 | case CHARSET_ISO_8859_1: |
---|
[48e4121] | 303 | #if (OS==RISCOS) |
---|
[db75e18] | 304 | case CHARSET_RISCOS31: /* RISC OS 3.1 has a few extras in 128-159 */ |
---|
[48e4121] | 305 | #endif |
---|
| 306 | if (value < 256) { |
---|
| 307 | *p = value; |
---|
| 308 | return 1; |
---|
| 309 | } |
---|
| 310 | #if (OS==RISCOS) |
---|
[db75e18] | 311 | /* FIXME: if OS version >= 3.1 handle extras here */ |
---|
| 312 | /* RISC OS 3.1 (and later) extensions to ISO-8859-1: |
---|
| 313 | * \^y = \x86 |
---|
| 314 | * \^Y = \x85 |
---|
| 315 | * \^w = \x82 |
---|
| 316 | * \^W = \x81 |
---|
| 317 | * \oe = \x9b |
---|
| 318 | * \OE = \x9a |
---|
| 319 | */ |
---|
[48e4121] | 320 | #endif |
---|
| 321 | break; |
---|
| 322 | #if (OS==MSDOS) |
---|
[db75e18] | 323 | case CHARSET_DOSCP850: |
---|
[48e4121] | 324 | value = xlate_dos_cp850(value); |
---|
| 325 | if (value) { |
---|
| 326 | *p = value; |
---|
| 327 | return 1; |
---|
| 328 | } |
---|
| 329 | break; |
---|
| 330 | #endif |
---|
[4432f2e] | 331 | } |
---|
[f1a5201] | 332 | return 0; |
---|
[4432f2e] | 333 | } |
---|
| 334 | |
---|
[f2a6ce4] | 335 | /* fall back on looking in the current directory */ |
---|
| 336 | static const char *pth_cfg_files = ""; |
---|
| 337 | |
---|
[db75e18] | 338 | static int num_msgs = 0; |
---|
[55de792] | 339 | static char **msg_array = NULL; |
---|
[db75e18] | 340 | |
---|
| 341 | static void |
---|
| 342 | parse_msg_file(int charset_code) |
---|
| 343 | { |
---|
| 344 | FILE *fh; |
---|
| 345 | unsigned char header[20]; |
---|
[6d54808] | 346 | /*const*/ char *lang; |
---|
[4432f2e] | 347 | int i; |
---|
[db75e18] | 348 | unsigned len; |
---|
[55de792] | 349 | unsigned char *p; |
---|
| 350 | |
---|
| 351 | #ifdef DEBUG |
---|
| 352 | fprintf(stderr, "parse_msg_file(%d)\n", charset_code); |
---|
| 353 | #endif |
---|
[db75e18] | 354 | |
---|
| 355 | lang = getenv("SURVEXLANG"); |
---|
[55de792] | 356 | #ifdef DEBUG |
---|
| 357 | fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)"); |
---|
| 358 | #endif |
---|
| 359 | |
---|
[32232b2] | 360 | if (!lang || !*lang) { |
---|
| 361 | lang = getenv("LANG"); |
---|
| 362 | if (!lang || !*lang) lang = DEFAULTLANG; |
---|
| 363 | } |
---|
[55de792] | 364 | #ifdef DEBUG |
---|
| 365 | fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)"); |
---|
| 366 | #endif |
---|
[db75e18] | 367 | |
---|
| 368 | #if 1 |
---|
| 369 | /* backward compatibility - FIXME deprecate? */ |
---|
[d41b1353] | 370 | if (strcasecmp(lang, "engi") == 0) { |
---|
[db75e18] | 371 | lang = "en"; |
---|
[d41b1353] | 372 | } else if (strcasecmp(lang, "engu") == 0) { |
---|
[db75e18] | 373 | lang = "en-us"; |
---|
[d41b1353] | 374 | } else if (strcasecmp(lang, "fren") == 0) { |
---|
[db75e18] | 375 | lang = "fr"; |
---|
[d41b1353] | 376 | } else if (strcasecmp(lang, "germ") == 0) { |
---|
[db75e18] | 377 | lang = "de"; |
---|
[d41b1353] | 378 | } else if (strcasecmp(lang, "ital") == 0) { |
---|
[db75e18] | 379 | lang = "it"; |
---|
[d41b1353] | 380 | } else if (strcasecmp(lang, "span") == 0) { |
---|
[db75e18] | 381 | lang = "es"; |
---|
[d41b1353] | 382 | } else if (strcasecmp(lang, "cata") == 0) { |
---|
[db75e18] | 383 | lang = "ca"; |
---|
[d41b1353] | 384 | } else if (strcasecmp(lang, "port") == 0) { |
---|
[db75e18] | 385 | lang = "pt"; |
---|
[48e4121] | 386 | } |
---|
[db75e18] | 387 | #endif |
---|
[55de792] | 388 | #ifdef DEBUG |
---|
| 389 | fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)"); |
---|
| 390 | #endif |
---|
[db75e18] | 391 | |
---|
[647407d] | 392 | /* On Mandrake LANG defaults to C */ |
---|
| 393 | if (strcmp(lang, "C") == 0) lang = "en"; |
---|
| 394 | |
---|
[94156dd] | 395 | lang = osstrdup(lang); |
---|
[6d54808] | 396 | /* On my RedHat 6.1 Linux box, LANG defaults to en_US - be nice and |
---|
| 397 | * handle this... */ |
---|
| 398 | if (strchr(lang, '_')) { |
---|
| 399 | char *under = strchr(lang, '_'); |
---|
[b4ab19e] | 400 | *under++ = '-'; |
---|
| 401 | while (*under) { |
---|
| 402 | *under = tolower(*under); |
---|
| 403 | under++; |
---|
| 404 | } |
---|
[6d54808] | 405 | } |
---|
| 406 | |
---|
[f2a6ce4] | 407 | fh = fopenWithPthAndExt(pth_cfg_files, lang, EXT_SVX_MSG, "rb", NULL); |
---|
[db75e18] | 408 | |
---|
| 409 | if (!fh) { |
---|
| 410 | /* e.g. if 'en-COCKNEY' is unknown, see if we know 'en' */ |
---|
| 411 | if (strlen(lang) > 3 && lang[2] == '-') { |
---|
| 412 | char lang_generic[3]; |
---|
| 413 | lang_generic[0] = lang[0]; |
---|
| 414 | lang_generic[1] = lang[1]; |
---|
| 415 | lang_generic[2] = '\0'; |
---|
[f2a6ce4] | 416 | fh = fopenWithPthAndExt(pth_cfg_files, lang_generic, EXT_SVX_MSG, |
---|
| 417 | "rb", NULL); |
---|
[db75e18] | 418 | } |
---|
[48e4121] | 419 | } |
---|
[db75e18] | 420 | |
---|
| 421 | if (!fh) { |
---|
| 422 | /* no point extracting this error, as it won't get used if file opens */ |
---|
| 423 | fprintf(STDERR, "Can't open message file '%s' using path '%s'\n", |
---|
[f2a6ce4] | 424 | lang, pth_cfg_files); |
---|
[db75e18] | 425 | exit(EXIT_FAILURE); |
---|
[4432f2e] | 426 | } |
---|
[db75e18] | 427 | |
---|
| 428 | if (fread(header, 1, 20, fh) < 20 || |
---|
| 429 | memcmp(header, "Svx\nMsg\r\n\xfe\xff", 12) != 0) { |
---|
| 430 | /* no point extracting this error, as it won't get used if file opens */ |
---|
| 431 | fprintf(STDERR, "Problem with message file '%s'\n", lang); |
---|
| 432 | exit(EXIT_FAILURE); |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | if (header[12] != 0) { |
---|
| 436 | /* no point extracting this error, as it won't get used if file opens */ |
---|
| 437 | fprintf(STDERR, "I don't understand this message file version\n"); |
---|
| 438 | exit(EXIT_FAILURE); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | num_msgs = (header[14] << 8) | header[15]; |
---|
| 442 | |
---|
| 443 | len = 0; |
---|
| 444 | for (i = 16; i < 20; i++) len = (len << 8) | header[i]; |
---|
| 445 | |
---|
[55de792] | 446 | p = osmalloc(len); |
---|
| 447 | if (fread(p, 1, len, fh) < len) { |
---|
[db75e18] | 448 | /* no point extracting this error - it won't get used once file's read */ |
---|
| 449 | fprintf(STDERR, "Message file truncated?\n"); |
---|
| 450 | exit(EXIT_FAILURE); |
---|
| 451 | } |
---|
[a420b49] | 452 | fclose(fh); |
---|
[db75e18] | 453 | |
---|
[55de792] | 454 | #ifdef DEBUG |
---|
| 455 | fprintf(stderr, "lang = '%s', num_msgs = %d, len = %d\n", lang, num_msgs, len); |
---|
| 456 | #endif |
---|
| 457 | |
---|
| 458 | msg_array = osmalloc(sizeof(char *) * num_msgs); |
---|
| 459 | |
---|
[db75e18] | 460 | for (i = 0; i < num_msgs; i++) { |
---|
[55de792] | 461 | unsigned char *to = p; |
---|
[db75e18] | 462 | int ch; |
---|
[55de792] | 463 | msg_array[i] = (char *)p; |
---|
[db75e18] | 464 | while ((ch = *p++) != 0) { |
---|
| 465 | /* A byte in the range 0x80-0xbf or 0xf0-0xff isn't valid in |
---|
| 466 | * this state, (0xf0-0xfd mean values > 0xffff) so treat as |
---|
| 467 | * literal and try to resync so we cope better when fed |
---|
| 468 | * non-utf-8 data. Similarly we abandon a multibyte sequence |
---|
| 469 | * if we hit an invalid character. */ |
---|
| 470 | if (ch >= 0xc0 && ch < 0xf0) { |
---|
| 471 | int ch1 = *p; |
---|
| 472 | if ((ch1 & 0xc0) != 0x80) goto resync; |
---|
| 473 | |
---|
| 474 | if (ch < 0xe0) { |
---|
| 475 | /* 2 byte sequence */ |
---|
| 476 | ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f); |
---|
| 477 | p++; |
---|
| 478 | } else { |
---|
| 479 | /* 3 byte sequence */ |
---|
| 480 | int ch2 = p[1]; |
---|
| 481 | if ((ch2 & 0xc0) != 0x80) goto resync; |
---|
| 482 | ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6) | (ch2 & 0x3f); |
---|
| 483 | p += 2; |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | resync: |
---|
| 488 | |
---|
| 489 | if (ch < 127) { |
---|
| 490 | *to++ = (char)ch; |
---|
| 491 | } else { |
---|
| 492 | /* FIXME this rather assumes a 2 byte UTF-8 code never |
---|
| 493 | * transliterates to more than 2 characters */ |
---|
| 494 | to += add_unicode(charset_code, to, ch); |
---|
| 495 | } |
---|
| 496 | } |
---|
| 497 | *to++ = '\0'; |
---|
| 498 | } |
---|
[4432f2e] | 499 | } |
---|
| 500 | |
---|
[f2a6ce4] | 501 | const char * |
---|
| 502 | msg_cfgpth(void) |
---|
| 503 | { |
---|
| 504 | return pth_cfg_files; |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | void |
---|
| 508 | msg_init(const char *argv0) |
---|
[db75e18] | 509 | { |
---|
| 510 | char *p; |
---|
| 511 | |
---|
| 512 | #ifdef HAVE_SIGNAL |
---|
| 513 | init_signals(); |
---|
| 514 | #endif |
---|
| 515 | /* This code *should* be completely bomb-proof even if strcpy |
---|
| 516 | * generates a signal |
---|
| 517 | */ |
---|
| 518 | szAppNameCopy = argv0; /* FIXME... */ |
---|
| 519 | szAppNameCopy = osstrdup(argv0); |
---|
| 520 | |
---|
| 521 | /* Look for env. var. "SURVEXHOME" or the like */ |
---|
| 522 | p = getenv("SURVEXHOME"); |
---|
| 523 | if (p && *p) { |
---|
[f2a6ce4] | 524 | pth_cfg_files = osstrdup(p); |
---|
[18f4759] | 525 | #if (OS==UNIX) && defined(DATADIR) && defined(PACKAGE) |
---|
[a420b49] | 526 | } else { |
---|
| 527 | /* under Unix, we compile in the configured path */ |
---|
[18f4759] | 528 | pth_cfg_files = DATADIR "/" PACKAGE; |
---|
[a420b49] | 529 | #else |
---|
[db75e18] | 530 | } else if (argv0) { |
---|
| 531 | /* else try the path on argv[0] */ |
---|
[f2a6ce4] | 532 | pth_cfg_files = path_from_fnm(argv0); |
---|
[a420b49] | 533 | #endif |
---|
[db75e18] | 534 | } |
---|
| 535 | |
---|
| 536 | select_charset(default_charset()); |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | /* message may be overwritten by next call (but not in current implementation) */ |
---|
| 540 | extern const char * |
---|
| 541 | msg(int en) |
---|
| 542 | { |
---|
| 543 | static const char *szBadEn = "???"; |
---|
| 544 | |
---|
[55de792] | 545 | if (!msg_array) { |
---|
[a420b49] | 546 | if (en != 1) return szBadEn; |
---|
| 547 | /* this should be the only message which can be requested before |
---|
| 548 | * the message file is opened and read... */ |
---|
| 549 | return "Out of memory (couldn't find %ul bytes).\n"; |
---|
[db75e18] | 550 | } |
---|
| 551 | |
---|
| 552 | if (en < 0 || en >= num_msgs) return szBadEn; |
---|
| 553 | |
---|
[55de792] | 554 | return msg_array[en]; |
---|
[db75e18] | 555 | } |
---|
| 556 | |
---|
| 557 | /* returns persistent copy of message */ |
---|
| 558 | extern const char * |
---|
| 559 | msgPerm(int en) |
---|
| 560 | { |
---|
| 561 | return msg(en); |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | void |
---|
| 565 | v_report(int severity, const char *fnm, int line, int en, va_list ap) |
---|
| 566 | { |
---|
| 567 | if (fnm) { |
---|
| 568 | fputs(fnm, STDERR); |
---|
| 569 | if (line) fprintf(STDERR, ":%d", line); |
---|
| 570 | } else { |
---|
| 571 | fputs(szAppNameCopy, STDERR); |
---|
| 572 | } |
---|
| 573 | fputs(": ", STDERR); |
---|
| 574 | |
---|
| 575 | if (severity == 0) { |
---|
| 576 | fputs(msg(/*warning*/4), STDERR); |
---|
| 577 | fputs(": ", STDERR); |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | vfprintf(STDERR, msg(en), ap); |
---|
| 581 | fputnl(STDERR); |
---|
| 582 | |
---|
[647407d] | 583 | /* FIXME: allow "warnings are errors" and/or "errors are fatal" */ |
---|
[db75e18] | 584 | switch (severity) { |
---|
| 585 | case 0: |
---|
| 586 | cWarnings++; |
---|
| 587 | break; |
---|
| 588 | case 1: |
---|
| 589 | cErrors++; |
---|
| 590 | if (cErrors == 50) |
---|
| 591 | fatalerror_in_file(fnm, 0, /*Too many errors - giving up*/19); |
---|
| 592 | break; |
---|
| 593 | case 2: |
---|
| 594 | exit(EXIT_FAILURE); |
---|
| 595 | } |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | void |
---|
| 599 | warning(int en, ...) |
---|
| 600 | { |
---|
| 601 | va_list ap; |
---|
| 602 | va_start(ap, en); |
---|
| 603 | v_report(0, NULL, 0, en, ap); |
---|
| 604 | va_end(ap); |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | void |
---|
| 608 | error(int en, ...) |
---|
| 609 | { |
---|
| 610 | va_list ap; |
---|
| 611 | va_start(ap, en); |
---|
| 612 | v_report(1, NULL, 0, en, ap); |
---|
| 613 | va_end(ap); |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | void |
---|
| 617 | fatalerror(int en, ...) |
---|
| 618 | { |
---|
| 619 | va_list ap; |
---|
| 620 | va_start(ap, en); |
---|
| 621 | v_report(2, NULL, 0, en, ap); |
---|
| 622 | va_end(ap); |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | void |
---|
| 626 | warning_in_file(const char *fnm, int line, int en, ...) |
---|
| 627 | { |
---|
| 628 | va_list ap; |
---|
| 629 | va_start(ap, en); |
---|
| 630 | v_report(0, fnm, line, en, ap); |
---|
| 631 | va_end(ap); |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | void |
---|
| 635 | error_in_file(const char *fnm, int line, int en, ...) |
---|
| 636 | { |
---|
| 637 | va_list ap; |
---|
| 638 | va_start(ap, en); |
---|
| 639 | v_report(1, fnm, line, en, ap); |
---|
| 640 | va_end(ap); |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | void |
---|
| 644 | fatalerror_in_file(const char *fnm, int line, int en, ...) |
---|
| 645 | { |
---|
| 646 | va_list ap; |
---|
| 647 | va_start(ap, en); |
---|
| 648 | v_report(2, fnm, line, en, ap); |
---|
| 649 | va_end(ap); |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | /* Code to support switching character set at runtime (e.g. for a printer |
---|
| 653 | * driver to support different character sets on screen and on the printer) |
---|
| 654 | */ |
---|
| 655 | typedef struct charset_li { |
---|
| 656 | struct charset_li *next; |
---|
| 657 | int code; |
---|
[55de792] | 658 | char **msg_array; |
---|
[db75e18] | 659 | } charset_li; |
---|
| 660 | |
---|
| 661 | static charset_li *charset_head = NULL; |
---|
| 662 | |
---|
| 663 | static int charset = CHARSET_BAD; |
---|
| 664 | |
---|
| 665 | int |
---|
| 666 | select_charset(int charset_code) |
---|
| 667 | { |
---|
| 668 | int old_charset = charset; |
---|
| 669 | charset_li *p; |
---|
| 670 | |
---|
[55de792] | 671 | #ifdef DEBUG |
---|
| 672 | fprintf(stderr, "select_charset(%d), old charset = %d\n", charset_code, charset); |
---|
| 673 | #endif |
---|
| 674 | |
---|
[db75e18] | 675 | charset = charset_code; |
---|
| 676 | |
---|
| 677 | /* check if we've already parsed messages for new charset */ |
---|
| 678 | for (p = charset_head; p; p = p->next) { |
---|
[55de792] | 679 | #ifdef DEBUG |
---|
| 680 | printf("%p: code %d msg_array %p\n", p, p->code, p->msg_array); |
---|
| 681 | #endif |
---|
[db75e18] | 682 | if (p->code == charset) { |
---|
[55de792] | 683 | msg_array = p->msg_array; |
---|
[db75e18] | 684 | return old_charset; |
---|
| 685 | } |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | /* nope, got to reparse message file */ |
---|
| 689 | parse_msg_file(charset_code); |
---|
| 690 | |
---|
| 691 | /* add to list */ |
---|
| 692 | p = osnew(charset_li); |
---|
| 693 | p->code = charset; |
---|
[55de792] | 694 | p->msg_array = msg_array; |
---|
[db75e18] | 695 | p->next = charset_head; |
---|
| 696 | charset_head = p; |
---|
| 697 | |
---|
| 698 | return old_charset; |
---|
| 699 | } |
---|