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