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