[fd5f670] | 1 | /* message.c |
---|
[db75e18] | 2 | * Fairly general purpose message and error routines |
---|
[9965b2b] | 3 | * Copyright (C) 1993-2003,2004,2005,2006,2007,2010 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 |
---|
[06b1227] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 | |
---|
[ecffd4b3] | 34 | #include "cmdline.h" |
---|
[60f7018] | 35 | #include "whichos.h" |
---|
[4432f2e] | 36 | #include "filename.h" |
---|
| 37 | #include "message.h" |
---|
[60f7018] | 38 | #include "osdepend.h" |
---|
| 39 | #include "filelist.h" |
---|
| 40 | #include "debug.h" |
---|
| 41 | |
---|
[a4140e9] | 42 | #ifdef AVEN |
---|
[cd620d5] | 43 | # include "aven.h" |
---|
[a4140e9] | 44 | #endif |
---|
| 45 | |
---|
[60f7018] | 46 | #ifdef HAVE_SIGNAL |
---|
[7d5f3c0] | 47 | # ifdef HAVE_SETJMP_H |
---|
[60f7018] | 48 | # include <setjmp.h> |
---|
| 49 | static jmp_buf jmpbufSignal; |
---|
| 50 | # include <signal.h> |
---|
| 51 | # else |
---|
| 52 | # undef HAVE_SIGNAL |
---|
| 53 | # endif |
---|
| 54 | #endif |
---|
| 55 | |
---|
[affaeee] | 56 | #if OS_WIN32 |
---|
[6d30f38] | 57 | # define WIN32_LEAN_AND_MEAN |
---|
[cd620d5] | 58 | # include <windows.h> |
---|
[affaeee] | 59 | #elif OS_UNIX |
---|
[b4fe9fb] | 60 | # include <sys/types.h> |
---|
| 61 | # include <sys/stat.h> |
---|
[2163157] | 62 | #endif |
---|
| 63 | |
---|
[60f7018] | 64 | /* For funcs which want to be immune from messing around with different |
---|
| 65 | * calling conventions */ |
---|
| 66 | #ifndef CDECL |
---|
[2ca296b] | 67 | # define CDECL |
---|
[60f7018] | 68 | #endif |
---|
| 69 | |
---|
[25ab06b] | 70 | int msg_warnings = 0; /* keep track of how many warnings we've given */ |
---|
| 71 | int msg_errors = 0; /* and how many (non-fatal) errors */ |
---|
[60f7018] | 72 | |
---|
[a4140e9] | 73 | /* in case osmalloc() fails before appname_copy is set up */ |
---|
| 74 | static const char *appname_copy = "anonymous program"; |
---|
[60f7018] | 75 | |
---|
[b88b171] | 76 | /* Path to use to look for executables (used by aven to find cavern). */ |
---|
| 77 | static const char *exe_pth = ""; |
---|
| 78 | |
---|
[60f7018] | 79 | /* error code for failed osmalloc and osrealloc calls */ |
---|
[db75e18] | 80 | static void |
---|
| 81 | outofmem(OSSIZE_T size) |
---|
| 82 | { |
---|
[bd1913f] | 83 | fatalerror(/*Out of memory (couldn't find %lu bytes).*/1, |
---|
[421b7d2] | 84 | (unsigned long)size); |
---|
[60f7018] | 85 | } |
---|
| 86 | |
---|
[a420b49] | 87 | #ifdef TOMBSTONES |
---|
| 88 | #define TOMBSTONE_SIZE 16 |
---|
[bd1913f] | 89 | static const char tombstone[TOMBSTONE_SIZE] = "012345\xfftombstone"; |
---|
[a420b49] | 90 | #endif |
---|
| 91 | |
---|
[60f7018] | 92 | /* malloc with error catching if it fails. Also allows us to write special |
---|
[2fdd67c] | 93 | * versions easily eg for MS Windows. |
---|
[60f7018] | 94 | */ |
---|
[9965b2b] | 95 | void * |
---|
[db75e18] | 96 | osmalloc(OSSIZE_T size) |
---|
| 97 | { |
---|
[9965b2b] | 98 | void *p; |
---|
[a420b49] | 99 | #ifdef TOMBSTONES |
---|
| 100 | size += TOMBSTONE_SIZE * 2; |
---|
| 101 | p = malloc(size); |
---|
| 102 | #else |
---|
[db75e18] | 103 | p = xosmalloc(size); |
---|
[a420b49] | 104 | #endif |
---|
[2ca296b] | 105 | if (p == NULL) outofmem(size); |
---|
[a420b49] | 106 | #ifdef TOMBSTONES |
---|
[bd1913f] | 107 | printf("osmalloc truep=%p truesize=%d\n", p, size); |
---|
[a420b49] | 108 | memcpy(p, tombstone, TOMBSTONE_SIZE); |
---|
| 109 | memcpy(p + size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE); |
---|
| 110 | *(size_t *)p = size; |
---|
| 111 | p += TOMBSTONE_SIZE; |
---|
| 112 | #endif |
---|
[60f7018] | 113 | return p; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /* realloc with error catching if it fails. */ |
---|
[9965b2b] | 117 | void * |
---|
[db75e18] | 118 | osrealloc(void *p, OSSIZE_T size) |
---|
| 119 | { |
---|
[a420b49] | 120 | /* some pre-ANSI realloc implementations don't cope with a NULL pointer */ |
---|
| 121 | if (p == NULL) { |
---|
| 122 | p = xosmalloc(size); |
---|
| 123 | } else { |
---|
| 124 | #ifdef TOMBSTONES |
---|
| 125 | int true_size; |
---|
| 126 | size += TOMBSTONE_SIZE * 2; |
---|
| 127 | p -= TOMBSTONE_SIZE; |
---|
| 128 | true_size = *(size_t *)p; |
---|
[bd1913f] | 129 | printf("osrealloc (in truep=%p truesize=%d)\n", p, true_size); |
---|
[a420b49] | 130 | if (memcmp(p + sizeof(size_t), tombstone + sizeof(size_t), |
---|
| 131 | TOMBSTONE_SIZE - sizeof(size_t)) != 0) { |
---|
| 132 | printf("start tombstone for block %p, size %d corrupted!", |
---|
[cb3d1e2] | 133 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
[a420b49] | 134 | } |
---|
| 135 | if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone, |
---|
| 136 | TOMBSTONE_SIZE) != 0) { |
---|
| 137 | printf("end tombstone for block %p, size %d corrupted!", |
---|
[cb3d1e2] | 138 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
[a420b49] | 139 | } |
---|
| 140 | p = realloc(p, size); |
---|
| 141 | if (p == NULL) outofmem(size); |
---|
[bd1913f] | 142 | printf("osrealloc truep=%p truesize=%d\n", p, size); |
---|
[a420b49] | 143 | memcpy(p, tombstone, TOMBSTONE_SIZE); |
---|
| 144 | memcpy(p + size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE); |
---|
| 145 | *(size_t *)p = size; |
---|
| 146 | p += TOMBSTONE_SIZE; |
---|
| 147 | #else |
---|
| 148 | p = xosrealloc(p, size); |
---|
| 149 | #endif |
---|
| 150 | } |
---|
[2ca296b] | 151 | if (p == NULL) outofmem(size); |
---|
[60f7018] | 152 | return p; |
---|
| 153 | } |
---|
| 154 | |
---|
[9965b2b] | 155 | char * |
---|
[db75e18] | 156 | osstrdup(const char *str) |
---|
| 157 | { |
---|
[60f7018] | 158 | char *p; |
---|
[db75e18] | 159 | OSSIZE_T len; |
---|
| 160 | len = strlen(str) + 1; |
---|
[0a3c5fa] | 161 | p = osmalloc(len); |
---|
[db75e18] | 162 | memcpy(p, str, len); |
---|
[60f7018] | 163 | return p; |
---|
| 164 | } |
---|
| 165 | |
---|
[a420b49] | 166 | /* osfree is usually just a macro in osalloc.h */ |
---|
| 167 | #ifdef TOMBSTONES |
---|
[bd1913f] | 168 | void |
---|
[a420b49] | 169 | osfree(void *p) |
---|
| 170 | { |
---|
| 171 | int true_size; |
---|
| 172 | if (!p) return; |
---|
| 173 | p -= TOMBSTONE_SIZE; |
---|
| 174 | true_size = *(size_t *)p; |
---|
[bd1913f] | 175 | printf("osfree truep=%p truesize=%d\n", p, true_size); |
---|
[a420b49] | 176 | if (memcmp(p + sizeof(size_t), tombstone + sizeof(size_t), |
---|
| 177 | TOMBSTONE_SIZE - sizeof(size_t)) != 0) { |
---|
| 178 | printf("start tombstone for block %p, size %d corrupted!", |
---|
[cb3d1e2] | 179 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
[a420b49] | 180 | } |
---|
| 181 | if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone, |
---|
| 182 | TOMBSTONE_SIZE) != 0) { |
---|
| 183 | printf("end tombstone for block %p, size %d corrupted!", |
---|
[cb3d1e2] | 184 | p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2); |
---|
[a420b49] | 185 | } |
---|
| 186 | free(p); |
---|
| 187 | } |
---|
| 188 | #endif |
---|
[60f7018] | 189 | |
---|
| 190 | #ifdef HAVE_SIGNAL |
---|
| 191 | |
---|
| 192 | static int sigReceived; |
---|
| 193 | |
---|
| 194 | /* for systems not using autoconf, assume the signal handler returns void |
---|
| 195 | * unless specified elsewhere */ |
---|
| 196 | #ifndef RETSIGTYPE |
---|
[2ca296b] | 197 | # define RETSIGTYPE void |
---|
[60f7018] | 198 | #endif |
---|
| 199 | |
---|
[9965b2b] | 200 | static CDECL RETSIGTYPE |
---|
[bd1913f] | 201 | report_sig(int sig) |
---|
| 202 | { |
---|
[db75e18] | 203 | sigReceived = sig; |
---|
[2ca296b] | 204 | longjmp(jmpbufSignal, 1); |
---|
[60f7018] | 205 | } |
---|
| 206 | |
---|
[db75e18] | 207 | static void |
---|
| 208 | init_signals(void) |
---|
| 209 | { |
---|
[60f7018] | 210 | int en; |
---|
| 211 | if (!setjmp(jmpbufSignal)) { |
---|
[db75e18] | 212 | signal(SIGABRT, report_sig); /* abnormal termination eg abort() */ |
---|
| 213 | signal(SIGFPE, report_sig); /* arithmetic error eg /0 or overflow */ |
---|
| 214 | signal(SIGILL, report_sig); /* illegal function image eg illegal instruction */ |
---|
| 215 | signal(SIGSEGV, report_sig); /* illegal storage access eg access outside memory limits */ |
---|
[60f7018] | 216 | # ifdef SIGSTAK /* only on RISC OS AFAIK */ |
---|
[db75e18] | 217 | signal(SIGSTAK, report_sig); /* stack overflow */ |
---|
[60f7018] | 218 | # endif |
---|
| 219 | return; |
---|
| 220 | } |
---|
[db75e18] | 221 | |
---|
[ec58e3d3] | 222 | /* Remove that signal handler to avoid the possibility of an infinite loop. |
---|
| 223 | */ |
---|
| 224 | signal(sigReceived, SIG_DFL); |
---|
| 225 | |
---|
[60f7018] | 226 | switch (sigReceived) { |
---|
[bd1913f] | 227 | case SIGABRT: en = /*Abnormal termination*/90; break; |
---|
| 228 | case SIGFPE: en = /*Arithmetic error*/91; break; |
---|
| 229 | case SIGILL: en = /*Illegal instruction*/92; break; |
---|
| 230 | case SIGSEGV: en = /*Bad memory access*/94; break; |
---|
[60f7018] | 231 | # ifdef SIGSTAK |
---|
[bd1913f] | 232 | case SIGSTAK: en = /*Stack overflow*/96; break; |
---|
[60f7018] | 233 | # endif |
---|
[bd1913f] | 234 | default: en = /*Unknown signal received*/97; break; |
---|
[60f7018] | 235 | } |
---|
[db75e18] | 236 | fputsnl(msg(en), STDERR); |
---|
[ea816ec] | 237 | |
---|
[c0a9908] | 238 | /* Any of the signals we catch indicates a bug */ |
---|
| 239 | fatalerror(/*Bug in program detected! Please report this to the authors*/11); |
---|
[db75e18] | 240 | |
---|
[60f7018] | 241 | exit(EXIT_FAILURE); |
---|
| 242 | } |
---|
| 243 | #endif |
---|
| 244 | |
---|
[bd1913f] | 245 | static int |
---|
| 246 | default_charset(void) |
---|
| 247 | { |
---|
[06b1227] | 248 | if (getenv("SURVEX_UTF8")) return CHARSET_UTF8; |
---|
[affaeee] | 249 | #if OS_WIN32 |
---|
[d5bd3a7] | 250 | # ifdef AVEN |
---|
[31ccf72] | 251 | # define CODEPAGE GetACP() |
---|
[d5bd3a7] | 252 | # else |
---|
[06b1227] | 253 | # define CODEPAGE GetConsoleOutputCP() |
---|
[31ccf72] | 254 | # endif |
---|
| 255 | switch (CODEPAGE) { |
---|
[2f85497] | 256 | case 0: return CHARSET_UTF8; |
---|
[dc639a8] | 257 | case 1252: return CHARSET_WINCP1252; |
---|
[d92d282] | 258 | case 1250: return CHARSET_WINCP1250; |
---|
[dc639a8] | 259 | case 850: return CHARSET_DOSCP850; |
---|
[d5bd3a7] | 260 | } |
---|
| 261 | return CHARSET_USASCII; |
---|
[affaeee] | 262 | #elif OS_UNIX |
---|
[e416af5] | 263 | #ifdef AVEN |
---|
| 264 | return CHARSET_UTF8; |
---|
| 265 | #else |
---|
[8c15864] | 266 | const char *p = getenv("LC_ALL"); |
---|
[31ccf72] | 267 | if (p == NULL || p[0] == '\0') { |
---|
| 268 | p = getenv("LC_CTYPE"); |
---|
| 269 | if (p == NULL || p[0] == '\0') { |
---|
| 270 | p = msg_lang; |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | if (p) { |
---|
| 275 | char *q = strchr(p, '.'); |
---|
| 276 | if (q) p = q + 1; |
---|
| 277 | } |
---|
| 278 | |
---|
[a4140e9] | 279 | if (p) { |
---|
[8c15864] | 280 | const char *chset = p; |
---|
[a4140e9] | 281 | size_t name_len; |
---|
| 282 | |
---|
| 283 | while (*p != '\0' && *p != '@') p++; |
---|
| 284 | |
---|
| 285 | name_len = p - chset; |
---|
| 286 | |
---|
| 287 | if (name_len) { |
---|
| 288 | int only_digit = 1; |
---|
| 289 | size_t cnt; |
---|
[421b7d2] | 290 | |
---|
[a4140e9] | 291 | for (cnt = 0; cnt < name_len; ++cnt) |
---|
[abd126e] | 292 | if (isalpha((unsigned char)chset[cnt])) { |
---|
[a4140e9] | 293 | only_digit = 0; |
---|
| 294 | break; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | if (only_digit) goto iso; |
---|
[421b7d2] | 298 | |
---|
[a4140e9] | 299 | switch (tolower(chset[0])) { |
---|
| 300 | case 'i': |
---|
| 301 | if (tolower(chset[1]) == 's' && tolower(chset[2]) == 'o') { |
---|
| 302 | chset += 3; |
---|
| 303 | iso: |
---|
| 304 | if (strncmp(chset, "8859", 4) == 0) { |
---|
| 305 | chset += 4; |
---|
[abd126e] | 306 | while (chset < p && *chset && !isdigit((unsigned char)*chset)) |
---|
| 307 | chset++; |
---|
[a4140e9] | 308 | switch (atoi(chset)) { |
---|
| 309 | case 1: return CHARSET_ISO_8859_1; |
---|
[f0a0b05] | 310 | case 2: return CHARSET_ISO_8859_2; |
---|
[f8e03f3] | 311 | case 15: return CHARSET_ISO_8859_15; |
---|
[421b7d2] | 312 | default: return CHARSET_USASCII; |
---|
[a4140e9] | 313 | } |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | break; |
---|
| 317 | case 'u': |
---|
| 318 | if (tolower(chset[1]) == 't' && tolower(chset[2]) == 'f') { |
---|
| 319 | chset += 3; |
---|
[abd126e] | 320 | while (chset < p && *chset && !isdigit((unsigned char)*chset)) |
---|
| 321 | chset++; |
---|
[a4140e9] | 322 | switch (atoi(chset)) { |
---|
| 323 | case 8: return CHARSET_UTF8; |
---|
| 324 | default: return CHARSET_USASCII; |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | return CHARSET_USASCII; |
---|
| 331 | #endif |
---|
| 332 | #else |
---|
[affaeee] | 333 | # error Do not know operating system! |
---|
[60f7018] | 334 | #endif |
---|
| 335 | } |
---|
| 336 | |
---|
[a4140e9] | 337 | /* It seems that Swedish and maybe some other scandanavian languages don't |
---|
| 338 | * transliterate ä to ae - but it seems there may be conflicting views |
---|
| 339 | * on this... |
---|
| 340 | */ |
---|
| 341 | #define umlaut_to_e() 1 |
---|
| 342 | |
---|
[1b92879] | 343 | /* values <= 127 already dealt with */ |
---|
[db75e18] | 344 | static int |
---|
[55de792] | 345 | add_unicode(int charset, unsigned char *p, int value) |
---|
[db75e18] | 346 | { |
---|
[55de792] | 347 | #ifdef DEBUG |
---|
| 348 | fprintf(stderr, "add_unicode(%d, %p, %d)\n", charset, p, value); |
---|
| 349 | #endif |
---|
[4432f2e] | 350 | if (value == 0) return 0; |
---|
[48e4121] | 351 | switch (charset) { |
---|
[db75e18] | 352 | case CHARSET_USASCII: |
---|
[6a4871e] | 353 | if (value < 0x80) { |
---|
[48e4121] | 354 | *p = value; |
---|
| 355 | return 1; |
---|
| 356 | } |
---|
| 357 | break; |
---|
[db75e18] | 358 | case CHARSET_ISO_8859_1: |
---|
[6a4871e] | 359 | if (value < 0x100) { |
---|
[48e4121] | 360 | *p = value; |
---|
| 361 | return 1; |
---|
| 362 | } |
---|
[ea816ec] | 363 | break; |
---|
[f0a0b05] | 364 | case CHARSET_ISO_8859_2: |
---|
| 365 | if (value >= 0xa0) { |
---|
| 366 | int v = 0; |
---|
| 367 | switch (value) { |
---|
| 368 | case 0xa0: case 0xa4: case 0xa7: case 0xa8: case 0xad: case 0xb0: |
---|
| 369 | case 0xb4: case 0xb8: case 0xc1: case 0xc2: case 0xc4: case 0xc7: |
---|
| 370 | case 0xc9: case 0xcb: case 0xcd: case 0xce: case 0xd3: case 0xd4: |
---|
| 371 | case 0xd6: case 0xd7: case 0xda: case 0xdc: case 0xdd: case 0xdf: |
---|
| 372 | case 0xe1: case 0xe2: case 0xe4: case 0xe7: case 0xe9: case 0xeb: |
---|
| 373 | case 0xed: case 0xee: case 0xf3: case 0xf4: case 0xf6: case 0xf7: |
---|
| 374 | case 0xfa: case 0xfc: case 0xfd: |
---|
| 375 | v = value; break; |
---|
| 376 | case 0x104: v = '\xa1'; break; |
---|
| 377 | case 0x2d8: v = '\xa2'; break; |
---|
| 378 | case 0x141: v = '\xa3'; break; |
---|
| 379 | case 0x13d: v = '\xa5'; break; |
---|
| 380 | case 0x15a: v = '\xa6'; break; |
---|
| 381 | case 0x160: v = '\xa9'; break; |
---|
[64d37a3] | 382 | case 0x15e: v = '\xaa'; break; /* Scedil */ |
---|
[f0a0b05] | 383 | case 0x164: v = '\xab'; break; |
---|
| 384 | case 0x179: v = '\xac'; break; |
---|
| 385 | case 0x17d: v = '\xae'; break; |
---|
| 386 | case 0x17b: v = '\xaf'; break; |
---|
| 387 | case 0x105: v = '\xb1'; break; |
---|
| 388 | case 0x2db: v = '\xb2'; break; |
---|
| 389 | case 0x142: v = '\xb3'; break; |
---|
| 390 | case 0x13e: v = '\xb5'; break; |
---|
| 391 | case 0x15b: v = '\xb6'; break; |
---|
| 392 | case 0x2c7: v = '\xb7'; break; |
---|
| 393 | case 0x161: v = '\xb9'; break; |
---|
[64d37a3] | 394 | case 0x15f: v = '\xba'; break; /* scedil */ |
---|
[f0a0b05] | 395 | case 0x165: v = '\xbb'; break; |
---|
| 396 | case 0x17a: v = '\xbc'; break; |
---|
| 397 | case 0x2dd: v = '\xbd'; break; |
---|
| 398 | case 0x17e: v = '\xbe'; break; |
---|
| 399 | case 0x17c: v = '\xbf'; break; |
---|
| 400 | case 0x154: v = '\xc0'; break; |
---|
| 401 | case 0x102: v = '\xc3'; break; |
---|
| 402 | case 0x139: v = '\xc5'; break; |
---|
| 403 | case 0x106: v = '\xc6'; break; |
---|
| 404 | case 0x10c: v = '\xc8'; break; |
---|
| 405 | case 0x118: v = '\xca'; break; |
---|
| 406 | case 0x11a: v = '\xcc'; break; |
---|
| 407 | case 0x10e: v = '\xcf'; break; |
---|
| 408 | case 0x110: v = '\xd0'; break; |
---|
| 409 | case 0x143: v = '\xd1'; break; |
---|
| 410 | case 0x147: v = '\xd2'; break; |
---|
| 411 | case 0x150: v = '\xd5'; break; |
---|
| 412 | case 0x158: v = '\xd8'; break; |
---|
| 413 | case 0x16e: v = '\xd9'; break; |
---|
| 414 | case 0x170: v = '\xdb'; break; |
---|
[64d37a3] | 415 | case 0x162: v = '\xde'; break; /* Ţ */ |
---|
[f0a0b05] | 416 | case 0x155: v = '\xe0'; break; |
---|
| 417 | case 0x103: v = '\xe3'; break; |
---|
| 418 | case 0x13a: v = '\xe5'; break; |
---|
| 419 | case 0x107: v = '\xe6'; break; |
---|
| 420 | case 0x10d: v = '\xe8'; break; |
---|
| 421 | case 0x119: v = '\xea'; break; |
---|
| 422 | case 0x11b: v = '\xec'; break; |
---|
| 423 | case 0x10f: v = '\xef'; break; |
---|
| 424 | case 0x111: v = '\xf0'; break; |
---|
| 425 | case 0x144: v = '\xf1'; break; |
---|
| 426 | case 0x148: v = '\xf2'; break; |
---|
| 427 | case 0x151: v = '\xf5'; break; |
---|
| 428 | case 0x159: v = '\xf8'; break; |
---|
| 429 | case 0x16f: v = '\xf9'; break; |
---|
| 430 | case 0x171: v = '\xfb'; break; |
---|
[64d37a3] | 431 | case 0x163: v = '\xfe'; break; /* tcedil */ |
---|
[f0a0b05] | 432 | case 0x2d9: v = '\xff'; break; |
---|
| 433 | } |
---|
[abd126e] | 434 | if (v == 0) break; |
---|
[f0a0b05] | 435 | value = v; |
---|
| 436 | } |
---|
| 437 | *p = value; |
---|
| 438 | return 1; |
---|
[f8e03f3] | 439 | case CHARSET_ISO_8859_15: |
---|
| 440 | switch (value) { |
---|
| 441 | case 0xa4: case 0xa6: case 0xb0: case 0xc4: |
---|
| 442 | case 0xd0: case 0xd4: case 0xd5: case 0xd6: |
---|
| 443 | goto donthave; |
---|
| 444 | case 0x152: value = 0xd4; break; /* Œ */ |
---|
| 445 | case 0x153: value = 0xd5; break; /* œ */ |
---|
| 446 | #if 0 |
---|
| 447 | case 0x0: value = 0xa4; break; /* euro */ |
---|
[f0a0b05] | 448 | #endif |
---|
| 449 | case 0x160: value = 0xa6; break; /* Scaron */ |
---|
| 450 | case 0x161: value = 0xb0; break; /* scaron */ |
---|
| 451 | case 0x17d: value = 0xc4; break; /* Zcaron */ |
---|
| 452 | case 0x17e: value = 0xd0; break; /* zcaron */ |
---|
| 453 | #if 0 |
---|
[f8e03f3] | 454 | case 0x0: value = 0xd6; break; /* Ydiersis */ |
---|
| 455 | #endif |
---|
| 456 | } |
---|
| 457 | if (value < 0x100) { |
---|
| 458 | *p = value; |
---|
| 459 | return 1; |
---|
| 460 | } |
---|
| 461 | donthave: |
---|
| 462 | break; |
---|
[affaeee] | 463 | #if OS_WIN32 |
---|
[f0a0b05] | 464 | case CHARSET_WINCP1250: |
---|
| 465 | /* MS Windows rough equivalent to ISO-8859-2 */ |
---|
| 466 | if (value >= 0x80) { |
---|
| 467 | int v = 0; |
---|
| 468 | switch (value) { |
---|
| 469 | case 0xa0: case 0xa4: case 0xa6: case 0xa7: case 0xa8: case 0xa9: |
---|
| 470 | case 0xab: case 0xac: case 0xad: case 0xae: case 0xb0: case 0xb1: |
---|
| 471 | case 0xb4: case 0xb5: case 0xb6: case 0xb7: case 0xb8: case 0xbb: |
---|
| 472 | case 0xc1: case 0xc2: case 0xc4: case 0xc7: case 0xc9: case 0xcb: |
---|
| 473 | case 0xcd: case 0xce: case 0xd3: case 0xd4: case 0xd6: case 0xd7: |
---|
| 474 | case 0xda: case 0xdc: case 0xdd: case 0xdf: case 0xe1: case 0xe2: |
---|
| 475 | case 0xe4: case 0xe7: case 0xe9: case 0xeb: case 0xed: case 0xee: |
---|
| 476 | case 0xf3: case 0xf4: case 0xf6: case 0xf7: case 0xfa: case 0xfc: |
---|
[b88b171] | 477 | case 0xfd: |
---|
[f0a0b05] | 478 | v = value; break; |
---|
| 479 | case 0x20ac: v = '\x80'; break; |
---|
| 480 | case 0x201a: v = '\x82'; break; |
---|
| 481 | case 0x201e: v = '\x84'; break; |
---|
| 482 | case 0x2026: v = '\x85'; break; |
---|
| 483 | case 0x2020: v = '\x86'; break; |
---|
| 484 | case 0x2021: v = '\x87'; break; |
---|
| 485 | case 0x2030: v = '\x89'; break; |
---|
| 486 | case 0x0160: v = '\x8a'; break; |
---|
| 487 | case 0x2039: v = '\x8b'; break; |
---|
| 488 | case 0x015a: v = '\x8c'; break; |
---|
| 489 | case 0x0164: v = '\x8d'; break; |
---|
| 490 | case 0x017d: v = '\x8e'; break; |
---|
| 491 | case 0x0179: v = '\x8f'; break; |
---|
| 492 | case 0x2018: v = '\x91'; break; |
---|
| 493 | case 0x2019: v = '\x92'; break; |
---|
| 494 | case 0x201c: v = '\x93'; break; |
---|
| 495 | case 0x201d: v = '\x94'; break; |
---|
| 496 | case 0x2022: v = '\x95'; break; |
---|
| 497 | case 0x2013: v = '\x96'; break; |
---|
| 498 | case 0x2014: v = '\x97'; break; |
---|
| 499 | case 0x2122: v = '\x99'; break; |
---|
| 500 | case 0x0161: v = '\x9a'; break; |
---|
| 501 | case 0x203a: v = '\x9b'; break; |
---|
| 502 | case 0x015b: v = '\x9c'; break; |
---|
| 503 | case 0x0165: v = '\x9d'; break; |
---|
| 504 | case 0x017e: v = '\x9e'; break; |
---|
| 505 | case 0x017a: v = '\x9f'; break; |
---|
| 506 | case 0x02c7: v = '\xa1'; break; |
---|
| 507 | case 0x02d8: v = '\xa2'; break; |
---|
| 508 | case 0x0141: v = '\xa3'; break; |
---|
| 509 | case 0x0104: v = '\xa5'; break; |
---|
[64d37a3] | 510 | case 0x015e: v = '\xaa'; break; /* Scedil */ |
---|
[f0a0b05] | 511 | case 0x017b: v = '\xaf'; break; |
---|
| 512 | case 0x02db: v = '\xb2'; break; |
---|
| 513 | case 0x0142: v = '\xb3'; break; |
---|
| 514 | case 0x0105: v = '\xb9'; break; |
---|
[64d37a3] | 515 | case 0x015f: v = '\xba'; break; /* scedil */ |
---|
[f0a0b05] | 516 | case 0x013d: v = '\xbc'; break; |
---|
| 517 | case 0x02dd: v = '\xbd'; break; |
---|
| 518 | case 0x013e: v = '\xbe'; break; |
---|
| 519 | case 0x017c: v = '\xbf'; break; |
---|
| 520 | case 0x0154: v = '\xc0'; break; |
---|
| 521 | case 0x0102: v = '\xc3'; break; |
---|
| 522 | case 0x0139: v = '\xc5'; break; |
---|
| 523 | case 0x0106: v = '\xc6'; break; |
---|
| 524 | case 0x010c: v = '\xc8'; break; |
---|
| 525 | case 0x0118: v = '\xca'; break; |
---|
| 526 | case 0x011a: v = '\xcc'; break; |
---|
| 527 | case 0x010e: v = '\xcf'; break; |
---|
| 528 | case 0x0110: v = '\xd0'; break; |
---|
| 529 | case 0x0143: v = '\xd1'; break; |
---|
| 530 | case 0x0147: v = '\xd2'; break; |
---|
| 531 | case 0x0150: v = '\xd5'; break; |
---|
| 532 | case 0x0158: v = '\xd8'; break; |
---|
| 533 | case 0x016e: v = '\xd9'; break; |
---|
| 534 | case 0x0170: v = '\xdb'; break; |
---|
[64d37a3] | 535 | case 0x0162: v = '\xde'; break; /* Ţ */ |
---|
[f0a0b05] | 536 | case 0x0155: v = '\xe0'; break; |
---|
| 537 | case 0x0103: v = '\xe3'; break; |
---|
| 538 | case 0x013a: v = '\xe5'; break; |
---|
| 539 | case 0x0107: v = '\xe6'; break; |
---|
| 540 | case 0x010d: v = '\xe8'; break; |
---|
| 541 | case 0x0119: v = '\xea'; break; |
---|
| 542 | case 0x011b: v = '\xec'; break; |
---|
| 543 | case 0x010f: v = '\xef'; break; |
---|
| 544 | case 0x0111: v = '\xf0'; break; |
---|
| 545 | case 0x0144: v = '\xf1'; break; |
---|
| 546 | case 0x0148: v = '\xf2'; break; |
---|
| 547 | case 0x0151: v = '\xf5'; break; |
---|
| 548 | case 0x0159: v = '\xf8'; break; |
---|
| 549 | case 0x016f: v = '\xf9'; break; |
---|
| 550 | case 0x0171: v = '\xfb'; break; |
---|
[64d37a3] | 551 | case 0x0163: v = '\xfe'; break; /* tcedil */ |
---|
[f0a0b05] | 552 | case 0x02d9: v = '\xff'; break; |
---|
| 553 | } |
---|
[abd126e] | 554 | if (v == 0) break; |
---|
[f0a0b05] | 555 | value = v; |
---|
| 556 | } |
---|
| 557 | *p = value; |
---|
| 558 | return 1; |
---|
[0af8e31] | 559 | case CHARSET_WINCP1252: |
---|
| 560 | /* MS Windows extensions to ISO-8859-1 */ |
---|
| 561 | switch (value) { |
---|
| 562 | case 0x152: value = 0x8c; break; /* Œ */ |
---|
| 563 | case 0x153: value = 0x9c; break; /* œ */ |
---|
| 564 | #if 0 |
---|
| 565 | /* there are a few other obscure ones we don't currently need */ |
---|
| 566 | #endif |
---|
| 567 | } |
---|
| 568 | if (value < 0x100) { |
---|
| 569 | *p = value; |
---|
| 570 | return 1; |
---|
| 571 | } |
---|
| 572 | break; |
---|
[e2ae719] | 573 | #endif |
---|
[affaeee] | 574 | #if OS_WIN32 |
---|
[1b92879] | 575 | case CHARSET_DOSCP850: { |
---|
| 576 | unsigned char uni2dostab[] = { |
---|
| 577 | 255, 173, 189, 156, 207, 190, 221, 245, |
---|
| 578 | 249, 184, 166, 174, 170, 240, 169, 238, |
---|
| 579 | 248, 241, 253, 252, 239, 230, 244, 250, |
---|
| 580 | 247, 251, 167, 175, 172, 171, 243, 168, |
---|
| 581 | 183, 181, 182, 199, 142, 143, 146, 128, |
---|
| 582 | 212, 144, 210, 211, 222, 214, 215, 216, |
---|
| 583 | 209, 165, 227, 224, 226, 229, 153, 158, |
---|
| 584 | 157, 235, 233, 234, 154, 237, 232, 225, |
---|
| 585 | 133, 160, 131, 198, 132, 134, 145, 135, |
---|
| 586 | 138, 130, 136, 137, 141, 161, 140, 139, |
---|
| 587 | 208, 164, 149, 162, 147, 228, 148, 246, |
---|
| 588 | 155, 151, 163, 150, 129, 236, 231, 152 |
---|
| 589 | }; |
---|
| 590 | if (value >= 160 && value < 256) { |
---|
| 591 | *p = (int)uni2dostab[value - 160]; |
---|
| 592 | return 1; |
---|
| 593 | } |
---|
[106af12] | 594 | #if 0 |
---|
| 595 | if (value == 305) { /* LATIN SMALL LETTER DOTLESS I */ |
---|
[1b92879] | 596 | *p = 213; |
---|
| 597 | return 1; |
---|
| 598 | } |
---|
[106af12] | 599 | if (value == 402) { /* LATIN SMALL LETTER F WITH HOOK */ |
---|
[1b92879] | 600 | *p = 159; |
---|
[48e4121] | 601 | return 1; |
---|
| 602 | } |
---|
[106af12] | 603 | #endif |
---|
[48e4121] | 604 | break; |
---|
[66f8e13] | 605 | } |
---|
[48e4121] | 606 | #endif |
---|
[4432f2e] | 607 | } |
---|
[a4140e9] | 608 | /* Transliterate characters we can't represent */ |
---|
| 609 | #ifdef DEBUG |
---|
| 610 | fprintf(stderr, "transliterate `%c' 0x%x\n", value, value); |
---|
| 611 | #endif |
---|
| 612 | switch (value) { |
---|
| 613 | case 160: |
---|
| 614 | *p = ' '; return 1; |
---|
| 615 | case 161 /* ¡ */: |
---|
| 616 | *p = '!'; return 1; |
---|
| 617 | case 171 /* « */: |
---|
| 618 | p[1] = *p = '<'; return 2; |
---|
| 619 | case 187 /* » */: |
---|
| 620 | p[1] = *p = '>'; return 2; |
---|
| 621 | case 191 /* ¿ */: |
---|
| 622 | *p = '?'; return 1; |
---|
| 623 | case 192 /* À */: case 193 /* Á */: case 194 /* Â */: case 195 /* Ã */: |
---|
| 624 | *p = 'A'; return 1; |
---|
| 625 | case 197 /* Å */: |
---|
| 626 | p[1] = *p = 'A'; return 2; |
---|
| 627 | case 196 /* Ä */: /* Ä */ |
---|
| 628 | *p = 'A'; |
---|
| 629 | if (!umlaut_to_e()) return 1; |
---|
| 630 | p[1] = 'E'; return 2; |
---|
| 631 | case 198 /* Æ */: |
---|
| 632 | *p = 'A'; p[1] = 'E'; return 2; |
---|
[f0a0b05] | 633 | case 199 /* Ç */: case 268: /* Č */ |
---|
[a4140e9] | 634 | *p = 'C'; return 1; |
---|
[f0a0b05] | 635 | case 270: /* Ď */ |
---|
| 636 | *p = 'D'; return 1; |
---|
[a4140e9] | 637 | case 200 /* È */: case 201 /* É */: case 202 /* Ê */: case 203 /* Ë */: |
---|
| 638 | *p = 'E'; return 1; |
---|
| 639 | case 204 /* Ì */: case 205 /* Í */: case 206 /* Î */: case 207 /* Ï */: |
---|
| 640 | *p = 'I'; return 1; |
---|
| 641 | case 208 /* Ð */: case 222 /* Þ */: |
---|
| 642 | *p = 'T'; p[1] = 'H'; return 2; |
---|
[f0a0b05] | 643 | case 315: /* Ĺ */ |
---|
[64d37a3] | 644 | case 317: /* Ľ */ |
---|
[f0a0b05] | 645 | *p = 'L'; return 1; |
---|
[a4140e9] | 646 | case 209 /* Ñ */: |
---|
| 647 | *p = 'N'; return 1; |
---|
| 648 | case 210 /* Ò */: case 211 /* Ó */: case 212 /* Ô */: case 213 /* Õ */: |
---|
| 649 | *p = 'O'; return 1; |
---|
| 650 | case 214 /* Ö */: /* Ö */ case 0x152: /* Œ */ |
---|
| 651 | *p = 'O'; p[1] = 'E'; return 2; |
---|
[f0a0b05] | 652 | case 352: /* Š */ |
---|
[64d37a3] | 653 | case 0x15e: /* Ş */ |
---|
[f0a0b05] | 654 | *p = 'S'; return 1; |
---|
[64d37a3] | 655 | case 0x162: /* Ţ */ |
---|
| 656 | case 0x164: /* Ť */ |
---|
| 657 | *p = 'T'; return 1; |
---|
[a4140e9] | 658 | case 217 /* Ù */: case 218 /* Ú */: case 219 /* Û */: |
---|
| 659 | *p = 'U'; return 1; |
---|
| 660 | case 220 /* Ü */: /* Ü */ |
---|
| 661 | *p = 'U'; p[1] = 'E'; return 2; |
---|
| 662 | case 221 /* Ý */: |
---|
| 663 | *p = 'Y'; return 1; |
---|
[f0a0b05] | 664 | case 381: /* Ž */ |
---|
| 665 | *p = 'Z'; return 1; |
---|
[a4140e9] | 666 | case 223 /* ß */: |
---|
| 667 | p[1] = *p = 's'; return 2; |
---|
| 668 | case 224 /* à */: case 225 /* á */: case 226 /* â */: case 227 /* ã */: |
---|
[64d37a3] | 669 | case 259: /* ă */ |
---|
[a4140e9] | 670 | *p = 'a'; return 1; |
---|
| 671 | case 228 /* ä */: /* ä */ case 230 /* æ */: |
---|
| 672 | *p = 'a'; p[1] = 'e'; return 2; |
---|
| 673 | case 229 /* å */: |
---|
| 674 | p[1] = *p = 'a'; return 2; |
---|
[f0a0b05] | 675 | case 231 /* ç */: case 269 /* č */: |
---|
[a4140e9] | 676 | *p = 'c'; return 1; |
---|
[f0a0b05] | 677 | case 271: /* ď */ |
---|
| 678 | *p = 'd'; return 1; |
---|
[a4140e9] | 679 | case 232 /* è */: case 233 /* é */: case 234 /* ê */: case 235 /* ë */: |
---|
[f0a0b05] | 680 | case 283 /* ě */: |
---|
[a4140e9] | 681 | *p = 'e'; return 1; |
---|
| 682 | case 236 /* ì */: case 237 /* í */: case 238 /* î */: case 239 /* ï */: |
---|
| 683 | *p = 'i'; return 1; |
---|
[f0a0b05] | 684 | case 316 /* ĺ */: |
---|
[64d37a3] | 685 | case 318 /* ľ */: |
---|
[f0a0b05] | 686 | *p = 'l'; return 1; |
---|
| 687 | case 241 /* ñ */: case 328 /* ň */: |
---|
[a4140e9] | 688 | *p = 'n'; return 1; |
---|
[f0a0b05] | 689 | case 345: /* ř */ |
---|
| 690 | *p = 'r'; return 1; |
---|
| 691 | case 353: /* š */ |
---|
[64d37a3] | 692 | case 0x15f: /* ş */ |
---|
[f0a0b05] | 693 | *p = 's'; return 1; |
---|
| 694 | case 357: /* ť */ |
---|
[64d37a3] | 695 | case 0x163: /* ţ */ |
---|
[f0a0b05] | 696 | *p = 't'; return 1; |
---|
[a4140e9] | 697 | case 240 /* ð */: case 254 /* þ */: |
---|
| 698 | *p = 't'; p[1] = 'h'; return 2; |
---|
[421b7d2] | 699 | case 242 /* ò */: case 243 /* ó */: case 244 /* ô */: case 245 /* õ */: |
---|
[a4140e9] | 700 | *p = 'o'; return 1; |
---|
| 701 | case 246 /* ö */: /* ö */ case 0x153: /* œ */ |
---|
| 702 | *p = 'o'; p[1] = 'e'; return 2; |
---|
| 703 | case 249 /* ù */: case 250 /* ú */: case 251 /* û */: |
---|
[f0a0b05] | 704 | case 367 /* ů */: |
---|
[a4140e9] | 705 | *p = 'u'; return 1; |
---|
| 706 | case 252 /* ü */: /* ü */ |
---|
| 707 | *p = 'u'; p[1] = 'e'; return 2; |
---|
| 708 | case 253 /* ý */: case 255 /* ÿ */: |
---|
| 709 | *p = 'y'; return 1; |
---|
[f0a0b05] | 710 | case 382: /* ž */ |
---|
| 711 | *p = 'z'; return 1; |
---|
[a4140e9] | 712 | } |
---|
| 713 | #ifdef DEBUG |
---|
| 714 | fprintf(stderr, "failed to transliterate\n"); |
---|
| 715 | #endif |
---|
[f1a5201] | 716 | return 0; |
---|
[4432f2e] | 717 | } |
---|
| 718 | |
---|
[affaeee] | 719 | #if OS_UNIX && defined DATADIR && defined PACKAGE |
---|
[fa42426] | 720 | /* Under Unix, we compile in the configured path */ |
---|
| 721 | static const char *pth_cfg_files = DATADIR "/" PACKAGE; |
---|
| 722 | #else |
---|
| 723 | /* On other platforms, we fall back on looking in the current directory */ |
---|
[f2a6ce4] | 724 | static const char *pth_cfg_files = ""; |
---|
[fa42426] | 725 | #endif |
---|
[f2a6ce4] | 726 | |
---|
[db75e18] | 727 | static int num_msgs = 0; |
---|
[55de792] | 728 | static char **msg_array = NULL; |
---|
[db75e18] | 729 | |
---|
[b83f907] | 730 | const char *msg_lang = NULL; |
---|
[c0a9908] | 731 | const char *msg_lang2 = NULL; |
---|
[b83f907] | 732 | |
---|
[d5bd3a7] | 733 | static char ** |
---|
| 734 | parse_msgs(int n, unsigned char *p, int charset_code) { |
---|
| 735 | int i; |
---|
| 736 | |
---|
| 737 | char **msgs = osmalloc(n * sizeof(char *)); |
---|
| 738 | |
---|
| 739 | for (i = 0; i < n; i++) { |
---|
| 740 | unsigned char *to = p; |
---|
| 741 | int ch; |
---|
| 742 | msgs[i] = (char *)p; |
---|
| 743 | |
---|
| 744 | /* If we want UTF8 anyway, we just need to find the start of each |
---|
| 745 | * message */ |
---|
| 746 | if (charset_code == CHARSET_UTF8) { |
---|
| 747 | p += strlen((char *)p) + 1; |
---|
| 748 | continue; |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | while ((ch = *p++) != 0) { |
---|
| 752 | /* A byte in the range 0x80-0xbf or 0xf0-0xff isn't valid in |
---|
| 753 | * this state, (0xf0-0xfd mean values > 0xffff) so treat as |
---|
| 754 | * literal and try to resync so we cope better when fed |
---|
| 755 | * non-utf-8 data. Similarly we abandon a multibyte sequence |
---|
| 756 | * if we hit an invalid character. */ |
---|
| 757 | if (ch >= 0xc0 && ch < 0xf0) { |
---|
| 758 | int ch1 = *p; |
---|
| 759 | if ((ch1 & 0xc0) != 0x80) goto resync; |
---|
| 760 | |
---|
| 761 | if (ch < 0xe0) { |
---|
| 762 | /* 2 byte sequence */ |
---|
| 763 | ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f); |
---|
| 764 | p++; |
---|
| 765 | } else { |
---|
| 766 | /* 3 byte sequence */ |
---|
| 767 | int ch2 = p[1]; |
---|
| 768 | if ((ch2 & 0xc0) != 0x80) goto resync; |
---|
| 769 | ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6) | (ch2 & 0x3f); |
---|
| 770 | p += 2; |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | |
---|
[421b7d2] | 774 | resync: |
---|
[d5bd3a7] | 775 | |
---|
| 776 | if (ch < 127) { |
---|
| 777 | *to++ = (char)ch; |
---|
| 778 | } else { |
---|
[421b7d2] | 779 | /* We assume an N byte UTF-8 code never transliterates to more |
---|
[d5bd3a7] | 780 | * than N characters (so we can't transliterate © to (C) or |
---|
| 781 | * ® to (R) for example) */ |
---|
| 782 | to += add_unicode(charset_code, to, ch); |
---|
| 783 | } |
---|
| 784 | } |
---|
| 785 | *to++ = '\0'; |
---|
| 786 | } |
---|
| 787 | return msgs; |
---|
| 788 | } |
---|
| 789 | |
---|
[c8f550b] | 790 | /* This is the name of the default language, which can be set like so: |
---|
| 791 | * ./configure --enable-defaultlang=fr |
---|
[0874cd6a] | 792 | */ |
---|
| 793 | #ifdef DEFAULTLANG |
---|
[d5bd3a7] | 794 | /* No point extracting these errors as they won't get used if file opens */ |
---|
[c8f550b] | 795 | # include "../lib/defaultlang.h" |
---|
[0874cd6a] | 796 | #else |
---|
| 797 | #define N_DONTEXTRACTMSGS 5 |
---|
| 798 | static unsigned char dontextractmsgs[] = |
---|
| 799 | "Can't open message file `%s' using path `%s'\0"/*1000*/ |
---|
| 800 | "Problem with message file `%s'\0"/*1001*/ |
---|
| 801 | "I don't understand this message file version\0"/*1002*/ |
---|
| 802 | "Message file truncated?\0"/*1003*/ |
---|
| 803 | "Out of memory (couldn't find %lu bytes).\0"/*1004*/; |
---|
| 804 | #endif |
---|
[d5bd3a7] | 805 | |
---|
| 806 | static char **dontextract = NULL; |
---|
| 807 | |
---|
[db75e18] | 808 | static void |
---|
| 809 | parse_msg_file(int charset_code) |
---|
| 810 | { |
---|
| 811 | FILE *fh; |
---|
| 812 | unsigned char header[20]; |
---|
[421b7d2] | 813 | int i; |
---|
[db75e18] | 814 | unsigned len; |
---|
[55de792] | 815 | unsigned char *p; |
---|
[b164c18] | 816 | char *fnm, *s; |
---|
[d5bd3a7] | 817 | int n; |
---|
[cb3d1e2] | 818 | |
---|
[55de792] | 819 | #ifdef DEBUG |
---|
| 820 | fprintf(stderr, "parse_msg_file(%d)\n", charset_code); |
---|
| 821 | #endif |
---|
[db75e18] | 822 | |
---|
[d5bd3a7] | 823 | /* sort out messages we need to print if we can't open the message file */ |
---|
| 824 | dontextract = parse_msgs(N_DONTEXTRACTMSGS, dontextractmsgs, charset_code); |
---|
| 825 | |
---|
[0a3c5fa] | 826 | fnm = osstrdup(msg_lang); |
---|
[b164c18] | 827 | /* trim off charset from stuff like "de_DE.iso8859_1" */ |
---|
| 828 | s = strchr(fnm, '.'); |
---|
| 829 | if (s) *s = '\0'; |
---|
[a2f9d5c] | 830 | |
---|
[b164c18] | 831 | fh = fopenWithPthAndExt(pth_cfg_files, fnm, EXT_SVX_MSG, "rb", NULL); |
---|
[db75e18] | 832 | |
---|
| 833 | if (!fh) { |
---|
[fd5f670] | 834 | /* e.g. if 'en_GB' is unknown, see if we know 'en' */ |
---|
| 835 | if (strlen(fnm) > 3 && fnm[2] == '_') { |
---|
[b164c18] | 836 | fnm[2] = '\0'; |
---|
| 837 | fh = fopenWithPthAndExt(pth_cfg_files, fnm, EXT_SVX_MSG, "rb", NULL); |
---|
[fd5f670] | 838 | if (!fh) fnm[2] = '_'; /* for error reporting */ |
---|
[db75e18] | 839 | } |
---|
[48e4121] | 840 | } |
---|
[db75e18] | 841 | |
---|
| 842 | if (!fh) { |
---|
[b07f165] | 843 | fatalerror(/*Can't open message file `%s' using path `%s'*/1000, |
---|
| 844 | fnm, pth_cfg_files); |
---|
[4432f2e] | 845 | } |
---|
[db75e18] | 846 | |
---|
| 847 | if (fread(header, 1, 20, fh) < 20 || |
---|
| 848 | memcmp(header, "Svx\nMsg\r\n\xfe\xff", 12) != 0) { |
---|
[b07f165] | 849 | fatalerror(/*Problem with message file `%s'*/1001, fnm); |
---|
[db75e18] | 850 | } |
---|
| 851 | |
---|
[b07f165] | 852 | if (header[12] != 0) |
---|
| 853 | fatalerror(/*I don't understand this message file version*/1002); |
---|
[db75e18] | 854 | |
---|
[d5bd3a7] | 855 | n = (header[14] << 8) | header[15]; |
---|
[db75e18] | 856 | |
---|
| 857 | len = 0; |
---|
| 858 | for (i = 16; i < 20; i++) len = (len << 8) | header[i]; |
---|
| 859 | |
---|
[0a3c5fa] | 860 | p = osmalloc(len); |
---|
[b07f165] | 861 | if (fread(p, 1, len, fh) < len) |
---|
| 862 | fatalerror(/*Message file truncated?*/1003); |
---|
[2163157] | 863 | |
---|
[a420b49] | 864 | fclose(fh); |
---|
[db75e18] | 865 | |
---|
[55de792] | 866 | #ifdef DEBUG |
---|
[d5bd3a7] | 867 | fprintf(stderr, "fnm = `%s', n = %d, len = %d\n", fnm, n, len); |
---|
[55de792] | 868 | #endif |
---|
[b164c18] | 869 | osfree(fnm); |
---|
[55de792] | 870 | |
---|
[d5bd3a7] | 871 | msg_array = parse_msgs(n, p, charset_code); |
---|
| 872 | num_msgs = n; |
---|
[4432f2e] | 873 | } |
---|
| 874 | |
---|
[f2a6ce4] | 875 | const char * |
---|
| 876 | msg_cfgpth(void) |
---|
| 877 | { |
---|
| 878 | return pth_cfg_files; |
---|
| 879 | } |
---|
| 880 | |
---|
[b88b171] | 881 | const char * |
---|
| 882 | msg_exepth(void) |
---|
| 883 | { |
---|
| 884 | return exe_pth; |
---|
| 885 | } |
---|
| 886 | |
---|
[bdfe97f] | 887 | const char * |
---|
| 888 | msg_appname(void) |
---|
| 889 | { |
---|
| 890 | return appname_copy; |
---|
| 891 | } |
---|
| 892 | |
---|
[f2a6ce4] | 893 | void |
---|
[bdfe97f] | 894 | msg_init(char * const *argv) |
---|
[db75e18] | 895 | { |
---|
| 896 | char *p; |
---|
[4c07c51] | 897 | SVX_ASSERT(argv); |
---|
[db75e18] | 898 | |
---|
| 899 | #ifdef HAVE_SIGNAL |
---|
| 900 | init_signals(); |
---|
| 901 | #endif |
---|
[bdfe97f] | 902 | /* Point to argv[0] itself so we report a more helpful error if the |
---|
| 903 | * code to work out the clean appname generates a signal */ |
---|
| 904 | appname_copy = argv[0]; |
---|
[affaeee] | 905 | #if OS_UNIX |
---|
[0a3c5fa] | 906 | /* use name as-is on Unix - programs run from path get name as supplied */ |
---|
[bdfe97f] | 907 | appname_copy = osstrdup(argv[0]); |
---|
[0a3c5fa] | 908 | #else |
---|
| 909 | /* use the lower-cased leafname on other platforms */ |
---|
[27b8b59] | 910 | p = leaf_from_fnm(argv[0]); |
---|
| 911 | appname_copy = p; |
---|
[4bfc8a7] | 912 | while (*p) { |
---|
| 913 | *p = tolower(*p); |
---|
[27b8b59] | 914 | ++p; |
---|
[4bfc8a7] | 915 | } |
---|
[0a3c5fa] | 916 | #endif |
---|
[db75e18] | 917 | |
---|
[5e14c5f] | 918 | /* shortcut --version so you can check the version number even when the |
---|
| 919 | * correct message file can't be found... */ |
---|
[bdfe97f] | 920 | if (argv[1] && strcmp(argv[1], "--version") == 0) { |
---|
| 921 | cmdline_version(); |
---|
| 922 | exit(0); |
---|
| 923 | } |
---|
[fa42426] | 924 | if (argv[0]) { |
---|
[b88b171] | 925 | exe_pth = path_from_fnm(argv[0]); |
---|
[0580c6a] | 926 | #ifdef MACOSX_BUNDLE |
---|
| 927 | /* If we're being built into a bundle, always look relative to |
---|
| 928 | * the path to the binary. */ |
---|
[b72f4b5] | 929 | #ifdef AVEN |
---|
| 930 | /* Aven is packaged as an application, so we must look inside there. */ |
---|
| 931 | pth_cfg_files = use_path(exe_pth, "../Resources"); |
---|
| 932 | #else |
---|
[b88b171] | 933 | pth_cfg_files = use_path(exe_pth, "share/survex"); |
---|
[b72f4b5] | 934 | #endif |
---|
[affaeee] | 935 | #elif OS_UNIX && defined DATADIR && defined PACKAGE |
---|
[30d0b94] | 936 | bool free_pth = fFalse; |
---|
| 937 | char *pth = getenv("srcdir"); |
---|
| 938 | if (!pth || !pth[0]) { |
---|
| 939 | pth = path_from_fnm(argv[0]); |
---|
| 940 | free_pth = fTrue; |
---|
| 941 | } |
---|
[fa42426] | 942 | if (pth[0]) { |
---|
[b4fe9fb] | 943 | struct stat buf; |
---|
[affaeee] | 944 | #if OS_UNIX_MACOSX |
---|
[0580c6a] | 945 | /* On MacOS X the programs may be installed anywhere, with the |
---|
| 946 | * share directory and the binaries in the same directory. */ |
---|
| 947 | p = use_path(pth, "share/survex/en.msg"); |
---|
[b4fe9fb] | 948 | if (lstat(p, &buf) == 0 && S_ISREG(buf.st_mode)) { |
---|
[0580c6a] | 949 | pth_cfg_files = use_path(pth, "share/survex"); |
---|
| 950 | goto macosx_got_msg; |
---|
[b4fe9fb] | 951 | } |
---|
| 952 | osfree(p); |
---|
| 953 | #endif |
---|
[0580c6a] | 954 | /* If we're run with an explicit path, check if "../lib/en.msg" |
---|
| 955 | * from the program's path exists, and if so look there for |
---|
| 956 | * support files - this allows us to test binaries in the build |
---|
| 957 | * tree easily. */ |
---|
[b4fe9fb] | 958 | p = use_path(pth, "../lib/en.msg"); |
---|
| 959 | if (lstat(p, &buf) == 0) { |
---|
[c282349] | 960 | #ifdef S_ISREG |
---|
[b4fe9fb] | 961 | /* POSIX way */ |
---|
| 962 | if (S_ISREG(buf.st_mode)) { |
---|
| 963 | pth_cfg_files = use_path(pth, "../lib"); |
---|
| 964 | } |
---|
| 965 | #else |
---|
| 966 | /* BSD way */ |
---|
| 967 | if ((buf.st_mode & S_IFMT) == S_IFREG) { |
---|
| 968 | pth_cfg_files = use_path(pth, "../lib"); |
---|
| 969 | } |
---|
| 970 | #endif |
---|
[fa42426] | 971 | } |
---|
[64d37a3] | 972 | #if defined(__GNUC__) && defined(__APPLE_CC__) |
---|
[0580c6a] | 973 | macosx_got_msg: |
---|
[64d37a3] | 974 | #endif |
---|
[5e14c5f] | 975 | osfree(p); |
---|
[fa42426] | 976 | } |
---|
[b88b171] | 977 | |
---|
[30d0b94] | 978 | if (free_pth) osfree(pth); |
---|
[affaeee] | 979 | #elif OS_WIN32 |
---|
[27b8b59] | 980 | DWORD len = 256; |
---|
| 981 | char *buf = NULL, *modname; |
---|
| 982 | while (1) { |
---|
| 983 | DWORD got; |
---|
| 984 | buf = osrealloc(buf, len); |
---|
| 985 | got = GetModuleFileName(NULL, buf, len); |
---|
| 986 | if (got < len) break; |
---|
| 987 | len += len; |
---|
| 988 | } |
---|
| 989 | modname = buf; |
---|
| 990 | /* Strange Win32 nastiness - strip prefix "\\?\" if present */ |
---|
| 991 | if (strncmp(modname, "\\\\?\\", 4) == 0) modname += 4; |
---|
| 992 | pth_cfg_files = path_from_fnm(modname); |
---|
| 993 | osfree(buf); |
---|
[a420b49] | 994 | #else |
---|
[fa42426] | 995 | /* Get the path to the support files from argv[0] */ |
---|
[bdfe97f] | 996 | pth_cfg_files = path_from_fnm(argv[0]); |
---|
[a420b49] | 997 | #endif |
---|
[db75e18] | 998 | } |
---|
| 999 | |
---|
[b164c18] | 1000 | msg_lang = getenv("SURVEXLANG"); |
---|
| 1001 | #ifdef DEBUG |
---|
[74044b7] | 1002 | fprintf(stderr, "msg_lang = %p (= \"%s\")\n", msg_lang, msg_lang?msg_lang:"(null)"); |
---|
[b164c18] | 1003 | #endif |
---|
| 1004 | |
---|
| 1005 | if (!msg_lang || !*msg_lang) { |
---|
[b88b171] | 1006 | msg_lang = getenv("LC_MESSAGES"); |
---|
[830d822] | 1007 | if (!msg_lang || !*msg_lang) { |
---|
| 1008 | msg_lang = getenv("LANG"); |
---|
| 1009 | /* Something (AutoCAD?) on Microsoft Windows sets LANG to a number. */ |
---|
| 1010 | if (msg_lang && !isalpha(msg_lang[0])) msg_lang = NULL; |
---|
| 1011 | } |
---|
[74044b7] | 1012 | if (!msg_lang || !*msg_lang) { |
---|
[affaeee] | 1013 | #if OS_WIN32 |
---|
[74044b7] | 1014 | LCID locid; |
---|
| 1015 | #endif |
---|
[37fca9b] | 1016 | #ifdef DEFAULTLANG |
---|
[d5bd3a7] | 1017 | msg_lang = STRING(DEFAULTLANG); |
---|
[37fca9b] | 1018 | #else |
---|
| 1019 | msg_lang = "en"; |
---|
| 1020 | #endif |
---|
[affaeee] | 1021 | #if OS_WIN32 |
---|
[74044b7] | 1022 | locid = GetUserDefaultLCID(); |
---|
| 1023 | if (locid) { |
---|
| 1024 | WORD langid = LANGIDFROMLCID(locid); |
---|
| 1025 | switch (PRIMARYLANGID(langid)) { |
---|
[761c5f9] | 1026 | /* older mingw compilers don't seem to supply this value */ |
---|
| 1027 | #ifndef LANG_CATALAN |
---|
| 1028 | # define LANG_CATALAN 0x03 |
---|
| 1029 | #endif |
---|
[74044b7] | 1030 | case LANG_CATALAN: |
---|
| 1031 | msg_lang = "ca"; |
---|
| 1032 | break; |
---|
[21c226e] | 1033 | case LANG_CHINESE: |
---|
| 1034 | msg_lang = "zh"; |
---|
| 1035 | break; |
---|
[74044b7] | 1036 | case LANG_ENGLISH: |
---|
| 1037 | if (SUBLANGID(langid) == SUBLANG_ENGLISH_US) |
---|
| 1038 | msg_lang = "en_US"; |
---|
| 1039 | else |
---|
| 1040 | msg_lang = "en"; |
---|
| 1041 | break; |
---|
| 1042 | case LANG_FRENCH: |
---|
| 1043 | msg_lang = "fr"; |
---|
| 1044 | break; |
---|
| 1045 | case LANG_GERMAN: |
---|
| 1046 | switch (SUBLANGID(langid)) { |
---|
| 1047 | case SUBLANG_GERMAN_SWISS: |
---|
| 1048 | msg_lang = "de_CH"; |
---|
| 1049 | break; |
---|
| 1050 | case SUBLANG_GERMAN: |
---|
| 1051 | msg_lang = "de_DE"; |
---|
| 1052 | break; |
---|
| 1053 | default: |
---|
| 1054 | msg_lang = "de"; |
---|
| 1055 | } |
---|
| 1056 | break; |
---|
| 1057 | case LANG_ITALIAN: |
---|
| 1058 | msg_lang = "it"; |
---|
| 1059 | break; |
---|
| 1060 | case LANG_PORTUGUESE: |
---|
| 1061 | if (SUBLANGID(langid) == SUBLANG_PORTUGUESE_BRAZILIAN) |
---|
| 1062 | msg_lang = "pt_BR"; |
---|
| 1063 | else |
---|
| 1064 | msg_lang = "pt"; |
---|
| 1065 | break; |
---|
[64d37a3] | 1066 | case LANG_ROMANIAN: |
---|
| 1067 | msg_lang = "ro"; |
---|
| 1068 | break; |
---|
[b6de07d] | 1069 | case LANG_SLOVAK: |
---|
| 1070 | msg_lang = "sk"; |
---|
| 1071 | break; |
---|
[74044b7] | 1072 | case LANG_SPANISH: |
---|
| 1073 | msg_lang = "es"; |
---|
| 1074 | break; |
---|
| 1075 | } |
---|
| 1076 | } |
---|
[84faf78b] | 1077 | #endif |
---|
[74044b7] | 1078 | } |
---|
[b164c18] | 1079 | } |
---|
| 1080 | #ifdef DEBUG |
---|
| 1081 | fprintf(stderr, "msg_lang = %p (= \"%s\")\n", msg_lang, msg_lang?msg_lang:"(null)"); |
---|
| 1082 | #endif |
---|
| 1083 | |
---|
| 1084 | /* On Mandrake LANG defaults to C */ |
---|
| 1085 | if (strcmp(msg_lang, "C") == 0) msg_lang = "en"; |
---|
| 1086 | |
---|
[0a3c5fa] | 1087 | msg_lang = osstrdup(msg_lang); |
---|
[b164c18] | 1088 | |
---|
| 1089 | /* Convert en-us to en_US, etc */ |
---|
| 1090 | p = strchr(msg_lang, '-'); |
---|
| 1091 | if (p) { |
---|
| 1092 | *p++ = '_'; |
---|
| 1093 | while (*p) { |
---|
| 1094 | *p = toupper(*p); |
---|
| 1095 | p++; |
---|
| 1096 | } |
---|
| 1097 | } |
---|
| 1098 | |
---|
| 1099 | p = strchr(msg_lang, '_'); |
---|
| 1100 | if (p) { |
---|
| 1101 | *p = '\0'; |
---|
[0a3c5fa] | 1102 | msg_lang2 = osstrdup(msg_lang); |
---|
[b164c18] | 1103 | *p = '_'; |
---|
| 1104 | } |
---|
| 1105 | |
---|
[efa5aea] | 1106 | int def_charset = default_charset(); |
---|
| 1107 | |
---|
[b164c18] | 1108 | #ifdef LC_MESSAGES |
---|
| 1109 | /* try to setlocale() appropriately too */ |
---|
| 1110 | if (!setlocale(LC_MESSAGES, msg_lang)) { |
---|
[efa5aea] | 1111 | if (!msg_lang2 || !setlocale(LC_MESSAGES, msg_lang2)) { |
---|
| 1112 | def_charset = CHARSET_ISO_8859_1; |
---|
| 1113 | } |
---|
[b164c18] | 1114 | } |
---|
| 1115 | #endif |
---|
| 1116 | |
---|
[efa5aea] | 1117 | select_charset(def_charset); |
---|
[db75e18] | 1118 | } |
---|
| 1119 | |
---|
[d5bd3a7] | 1120 | /* Message may be overwritten by next call |
---|
[bd1913f] | 1121 | * (but not in current implementation) */ |
---|
| 1122 | const char * |
---|
[db75e18] | 1123 | msg(int en) |
---|
| 1124 | { |
---|
[4c07c51] | 1125 | /* NB can't use SVX_ASSERT here! */ |
---|
[eee67ab] | 1126 | static char badbuf[256]; |
---|
[d5bd3a7] | 1127 | if (en >= 1000 && en < 1000 + N_DONTEXTRACTMSGS) |
---|
[b07f165] | 1128 | return dontextract[en - 1000]; |
---|
[55de792] | 1129 | if (!msg_array) { |
---|
[eee67ab] | 1130 | if (en != 1) { |
---|
[21c226e] | 1131 | sprintf(badbuf, "Message %d requested before msg_array initialised\n", |
---|
| 1132 | en); |
---|
[eee67ab] | 1133 | return badbuf; |
---|
| 1134 | } |
---|
[d5bd3a7] | 1135 | /* this should be the only other message which can be requested before |
---|
[a420b49] | 1136 | * the message file is opened and read... */ |
---|
[d5bd3a7] | 1137 | if (!dontextract) return "Out of memory (couldn't find %lu bytes)."; |
---|
[c40038a] | 1138 | return dontextract[(/*Out of memory (couldn't find %lu bytes).*/1004) |
---|
| 1139 | - 1000]; |
---|
[db75e18] | 1140 | } |
---|
| 1141 | |
---|
[eee67ab] | 1142 | if (en < 0 || en >= num_msgs) { |
---|
| 1143 | sprintf(badbuf, "Message %d out of range\n", en); |
---|
| 1144 | return badbuf; |
---|
| 1145 | } |
---|
[db75e18] | 1146 | |
---|
[90123e8] | 1147 | if (en == 0) { |
---|
[7993643] | 1148 | const char *p = msg_array[0]; |
---|
[90123e8] | 1149 | if (!*p) p = "(C)"; |
---|
| 1150 | return p; |
---|
| 1151 | } |
---|
| 1152 | |
---|
[55de792] | 1153 | return msg_array[en]; |
---|
[db75e18] | 1154 | } |
---|
| 1155 | |
---|
| 1156 | /* returns persistent copy of message */ |
---|
[bd1913f] | 1157 | const char * |
---|
[db75e18] | 1158 | msgPerm(int en) |
---|
| 1159 | { |
---|
| 1160 | return msg(en); |
---|
| 1161 | } |
---|
| 1162 | |
---|
| 1163 | void |
---|
| 1164 | v_report(int severity, const char *fnm, int line, int en, va_list ap) |
---|
| 1165 | { |
---|
[1f316f3] | 1166 | #ifdef AVEN |
---|
[2e18955] | 1167 | aven_v_report(severity, fnm, line, en, ap); |
---|
[2163157] | 1168 | #else |
---|
[db75e18] | 1169 | if (fnm) { |
---|
| 1170 | fputs(fnm, STDERR); |
---|
| 1171 | if (line) fprintf(STDERR, ":%d", line); |
---|
| 1172 | } else { |
---|
[a4140e9] | 1173 | fputs(appname_copy, STDERR); |
---|
[cb3d1e2] | 1174 | } |
---|
[db75e18] | 1175 | fputs(": ", STDERR); |
---|
| 1176 | |
---|
| 1177 | if (severity == 0) { |
---|
| 1178 | fputs(msg(/*warning*/4), STDERR); |
---|
| 1179 | fputs(": ", STDERR); |
---|
| 1180 | } |
---|
| 1181 | |
---|
| 1182 | vfprintf(STDERR, msg(en), ap); |
---|
| 1183 | fputnl(STDERR); |
---|
[1f316f3] | 1184 | #endif |
---|
[cb3d1e2] | 1185 | |
---|
[db75e18] | 1186 | switch (severity) { |
---|
| 1187 | case 0: |
---|
[25ab06b] | 1188 | msg_warnings++; |
---|
[db75e18] | 1189 | break; |
---|
| 1190 | case 1: |
---|
[25ab06b] | 1191 | msg_errors++; |
---|
| 1192 | if (msg_errors == 50) |
---|
[db75e18] | 1193 | fatalerror_in_file(fnm, 0, /*Too many errors - giving up*/19); |
---|
| 1194 | break; |
---|
| 1195 | case 2: |
---|
| 1196 | exit(EXIT_FAILURE); |
---|
| 1197 | } |
---|
| 1198 | } |
---|
| 1199 | |
---|
| 1200 | void |
---|
| 1201 | warning(int en, ...) |
---|
| 1202 | { |
---|
| 1203 | va_list ap; |
---|
| 1204 | va_start(ap, en); |
---|
| 1205 | v_report(0, NULL, 0, en, ap); |
---|
| 1206 | va_end(ap); |
---|
| 1207 | } |
---|
| 1208 | |
---|
| 1209 | void |
---|
| 1210 | error(int en, ...) |
---|
| 1211 | { |
---|
| 1212 | va_list ap; |
---|
| 1213 | va_start(ap, en); |
---|
| 1214 | v_report(1, NULL, 0, en, ap); |
---|
| 1215 | va_end(ap); |
---|
| 1216 | } |
---|
| 1217 | |
---|
| 1218 | void |
---|
| 1219 | fatalerror(int en, ...) |
---|
| 1220 | { |
---|
| 1221 | va_list ap; |
---|
| 1222 | va_start(ap, en); |
---|
| 1223 | v_report(2, NULL, 0, en, ap); |
---|
| 1224 | va_end(ap); |
---|
| 1225 | } |
---|
| 1226 | |
---|
| 1227 | void |
---|
| 1228 | warning_in_file(const char *fnm, int line, int en, ...) |
---|
| 1229 | { |
---|
| 1230 | va_list ap; |
---|
| 1231 | va_start(ap, en); |
---|
| 1232 | v_report(0, fnm, line, en, ap); |
---|
| 1233 | va_end(ap); |
---|
| 1234 | } |
---|
| 1235 | |
---|
| 1236 | void |
---|
| 1237 | error_in_file(const char *fnm, int line, int en, ...) |
---|
| 1238 | { |
---|
| 1239 | va_list ap; |
---|
| 1240 | va_start(ap, en); |
---|
| 1241 | v_report(1, fnm, line, en, ap); |
---|
| 1242 | va_end(ap); |
---|
| 1243 | } |
---|
| 1244 | |
---|
| 1245 | void |
---|
| 1246 | fatalerror_in_file(const char *fnm, int line, int en, ...) |
---|
| 1247 | { |
---|
| 1248 | va_list ap; |
---|
| 1249 | va_start(ap, en); |
---|
| 1250 | v_report(2, fnm, line, en, ap); |
---|
| 1251 | va_end(ap); |
---|
| 1252 | } |
---|
| 1253 | |
---|
| 1254 | /* Code to support switching character set at runtime (e.g. for a printer |
---|
| 1255 | * driver to support different character sets on screen and on the printer) |
---|
| 1256 | */ |
---|
| 1257 | typedef struct charset_li { |
---|
| 1258 | struct charset_li *next; |
---|
| 1259 | int code; |
---|
[55de792] | 1260 | char **msg_array; |
---|
[db75e18] | 1261 | } charset_li; |
---|
| 1262 | |
---|
| 1263 | static charset_li *charset_head = NULL; |
---|
| 1264 | |
---|
| 1265 | static int charset = CHARSET_BAD; |
---|
| 1266 | |
---|
| 1267 | int |
---|
| 1268 | select_charset(int charset_code) |
---|
| 1269 | { |
---|
| 1270 | int old_charset = charset; |
---|
| 1271 | charset_li *p; |
---|
| 1272 | |
---|
[55de792] | 1273 | #ifdef DEBUG |
---|
[bd1913f] | 1274 | fprintf(stderr, "select_charset(%d), old charset = %d\n", charset_code, |
---|
[421b7d2] | 1275 | charset); |
---|
[55de792] | 1276 | #endif |
---|
[cb3d1e2] | 1277 | |
---|
[db75e18] | 1278 | charset = charset_code; |
---|
| 1279 | |
---|
| 1280 | /* check if we've already parsed messages for new charset */ |
---|
| 1281 | for (p = charset_head; p; p = p->next) { |
---|
[55de792] | 1282 | #ifdef DEBUG |
---|
| 1283 | printf("%p: code %d msg_array %p\n", p, p->code, p->msg_array); |
---|
| 1284 | #endif |
---|
[db75e18] | 1285 | if (p->code == charset) { |
---|
[421b7d2] | 1286 | msg_array = p->msg_array; |
---|
| 1287 | return old_charset; |
---|
[db75e18] | 1288 | } |
---|
| 1289 | } |
---|
| 1290 | |
---|
| 1291 | /* nope, got to reparse message file */ |
---|
| 1292 | parse_msg_file(charset_code); |
---|
| 1293 | |
---|
| 1294 | /* add to list */ |
---|
| 1295 | p = osnew(charset_li); |
---|
| 1296 | p->code = charset; |
---|
[55de792] | 1297 | p->msg_array = msg_array; |
---|
[db75e18] | 1298 | p->next = charset_head; |
---|
| 1299 | charset_head = p; |
---|
| 1300 | |
---|
| 1301 | return old_charset; |
---|
| 1302 | } |
---|