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