source: git/src/message.c @ 89a6a1e

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey svn-merged-from-1.0
Last change on this file since 89a6a1e was 27b8b59, checked in by Olly Betts <olly@…>, 23 years ago

Sync with 1.0 branch.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@2208 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 47.4 KB
Line 
1/* message.c
2 * Fairly general purpose message and error routines
3 * Copyright (C) 1993-2002 Olly Betts
4 *
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.
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
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20/*#define DEBUG 1*/
21
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <ctype.h>
30#include <limits.h>
31#include <errno.h>
32#include <locale.h>
33
34#include "whichos.h"
35#include "filename.h"
36#include "message.h"
37#include "osdepend.h"
38#include "filelist.h"
39#include "debug.h"
40
41#ifdef AVEN
42# include "aven.h"
43#endif
44
45#ifdef HAVE_SIGNAL
46# ifdef HAVE_SETJMP_H
47#  include <setjmp.h>
48static jmp_buf jmpbufSignal;
49#  include <signal.h>
50# else
51#  undef HAVE_SIGNAL
52# endif
53#endif
54
55#if (OS==WIN32)
56# include <windows.h>
57#elif (OS==MSDOS)
58#include <dos.h>
59# ifdef __DJGPP__
60#  include <dpmi.h>
61#  include <go32.h>
62#  include <sys/movedata.h>
63# endif
64#elif (OS==RISCOS)
65# include "oslib/wimpreadsy.h"
66# include "oslib/territory.h"
67#endif
68
69/* For funcs which want to be immune from messing around with different
70 * calling conventions */
71#ifndef CDECL
72# define CDECL
73#endif
74
75int msg_warnings = 0; /* keep track of how many warnings we've given */
76int msg_errors = 0;   /* and how many (non-fatal) errors */
77
78/* in case osmalloc() fails before appname_copy is set up */
79static const char *appname_copy = "anonymous program";
80
81/* error code for failed osmalloc and osrealloc calls */
82static void
83outofmem(OSSIZE_T size)
84{
85   fatalerror(/*Out of memory (couldn't find %lu bytes).*/1,
86              (unsigned long)size);
87}
88
89#ifdef TOMBSTONES
90#define TOMBSTONE_SIZE 16
91static const char tombstone[TOMBSTONE_SIZE] = "012345\xfftombstone";
92#endif
93
94/* malloc with error catching if it fails. Also allows us to write special
95 * versions easily eg for DOS EMS or MS Windows.
96 */
97void FAR *
98osmalloc(OSSIZE_T size)
99{
100   void FAR *p;
101#ifdef TOMBSTONES
102   size += TOMBSTONE_SIZE * 2;
103   p = malloc(size);
104#else
105   p = xosmalloc(size);
106#endif
107   if (p == NULL) outofmem(size);
108#ifdef TOMBSTONES
109   printf("osmalloc truep=%p truesize=%d\n", p, size);
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
115   return p;
116}
117
118/* realloc with error catching if it fails. */
119void FAR *
120osrealloc(void *p, OSSIZE_T size)
121{
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;
131      printf("osrealloc (in truep=%p truesize=%d)\n", p, true_size);
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!",
135                p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2);
136      }
137      if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone,
138                 TOMBSTONE_SIZE) != 0) {
139         printf("end tombstone for block %p, size %d corrupted!",
140                p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2);
141      }
142      p = realloc(p, size);
143      if (p == NULL) outofmem(size);
144      printf("osrealloc truep=%p truesize=%d\n", p, size);
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   }
153   if (p == NULL) outofmem(size);
154   return p;
155}
156
157char FAR *
158osstrdup(const char *str)
159{
160   char *p;
161   OSSIZE_T len;
162   len = strlen(str) + 1;
163   p = osmalloc(len);
164   memcpy(p, str, len);
165   return p;
166}
167
168/* osfree is usually just a macro in osalloc.h */
169#ifdef TOMBSTONES
170void
171osfree(void *p)
172{
173   int true_size;
174   if (!p) return;
175   p -= TOMBSTONE_SIZE;
176   true_size = *(size_t *)p;
177   printf("osfree truep=%p truesize=%d\n", p, true_size);
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!",
181             p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2);
182   }
183   if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone,
184              TOMBSTONE_SIZE) != 0) {
185      printf("end tombstone for block %p, size %d corrupted!",
186             p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2);
187   }
188   free(p);
189}
190#endif
191
192#ifdef HAVE_SIGNAL
193
194static int sigReceived;
195
196/* for systems not using autoconf, assume the signal handler returns void
197 * unless specified elsewhere */
198#ifndef RETSIGTYPE
199# define RETSIGTYPE void
200#endif
201
202static CDECL RETSIGTYPE FAR
203report_sig(int sig)
204{
205   sigReceived = sig;
206   longjmp(jmpbufSignal, 1);
207}
208
209static void
210init_signals(void)
211{
212   int en;
213   if (!setjmp(jmpbufSignal)) {
214#if 0 /* disable these to get a core dump */
215      signal(SIGABRT, report_sig); /* abnormal termination eg abort() */
216      signal(SIGFPE,  report_sig); /* arithmetic error eg /0 or overflow */
217      signal(SIGILL,  report_sig); /* illegal function image eg illegal instruction */
218      signal(SIGSEGV, report_sig); /* illegal storage access eg access outside memory limits */
219#endif
220# ifdef SIGSTAK /* only on RISC OS AFAIK */
221      signal(SIGSTAK, report_sig); /* stack overflow */
222# endif
223      return;
224   }
225
226   switch (sigReceived) {
227      case SIGABRT: en = /*Abnormal termination*/90; break;
228      case SIGFPE:  en = /*Arithmetic error*/91; break;
229      case SIGILL:  en = /*Illegal instruction*/92; break;
230      case SIGSEGV: en = /*Bad memory access*/94; break;
231# ifdef SIGSTAK
232      case SIGSTAK: en = /*Stack overflow*/96; break;
233# endif
234      default:      en = /*Unknown signal received*/97; break;
235   }
236   fputsnl(msg(en), STDERR);
237
238   /* Any of the signals we catch indicates a bug */
239   fatalerror(/*Bug in program detected! Please report this to the authors*/11);
240
241   exit(EXIT_FAILURE);
242}
243#endif
244
245static int
246default_charset(void)
247{
248#if (OS==RISCOS)
249   /* RISCOS 3.1 and above CHARSET_RISCOS31 (ISO_8859_1 + extras in 128-159)
250    * RISCOS < 3.1 is ISO_8859_1 */
251   int version;
252   if (xwimpreadsysinfo_version(&version) != NULL) {
253      /* RISC OS 2 or some error (don't care which) */
254      return CHARSET_ISO_8859_1;
255   }
256
257   /* oddly wimp_VERSION_RO3 is RISC OS 3.1 */
258   if (version < wimp_VERSION_RO3) return CHARSET_ISO_8859_1;
259
260   return CHARSET_RISCOS31;
261#elif (OS==MSDOS)
262#ifdef __DJGPP__
263   __dpmi_regs r;
264   r.x.ax = 0x6501;
265   r.x.bx = 0xffff;
266   r.x.dx = 0xffff;
267   /* Use DJGPP's transfer buffer (which is at least 2K) */
268   r.x.es = __tb >> 4;
269   r.x.di = __tb & 0x0f;
270   r.x.cx = 2048;
271   /* bit 1 is the carry flag */
272   if (__dpmi_int(0x21, &r) != -1 && !(r.x.flags & 1)) {
273      unsigned short p;
274      dosmemget(__tb + 5, 2, &p);
275#else
276   union REGS r;
277   struct SREGS s = { 0 };
278
279   unsigned char buf[48];
280   r.x.ax = 0x6501;
281   r.x.bx = 0xffff;
282   r.x.dx = 0xffff;
283   s.es = FP_SEG(buf);
284   r.x.di = FP_OFF(buf);
285   r.x.cx = 48;
286   intdosx(&r, &r, &s);
287   if (!r.x.cflag) {
288      unsigned short p = buf[5] | (buf[6] << 8);
289#endif
290      if (p == 437) return CHARSET_DOSCP437;
291      if (p == 850) return CHARSET_DOSCP850;
292      if (p == 912) return CHARSET_ISO_8859_2;
293   }
294   return CHARSET_USASCII;
295#elif (OS==WIN32)
296# ifdef AVEN
297#  define CODEPAGE GetACP()
298# else
299#  define CODEPAGE GetConsoleOutputCP()
300# endif
301   switch (CODEPAGE) {
302    case 1252: return CHARSET_WINCP1252;
303    case 850: return CHARSET_DOSCP850;
304   }
305   return CHARSET_USASCII;
306#elif (OS==UNIX)
307#if defined(XCAVEROT) || defined(AVEN)
308   return CHARSET_ISO_8859_1;
309#else
310   const char *p = getenv("LC_ALL");
311   if (p == NULL || p[0] == '\0') {
312      p = getenv("LC_CTYPE");
313      if (p == NULL || p[0] == '\0') {
314         p = msg_lang;
315      }
316   }
317
318   if (p) {
319      char *q = strchr(p, '.');
320      if (q) p = q + 1;
321   }
322
323   if (p) {
324      const char *chset = p;
325      size_t name_len;
326
327      while (*p != '\0' && *p != '@') p++;
328
329      name_len = p - chset;
330
331      if (name_len) {
332         int only_digit = 1;
333         size_t cnt;
334
335         for (cnt = 0; cnt < name_len; ++cnt)
336            if (isalpha((unsigned char)chset[cnt])) {
337               only_digit = 0;
338               break;
339            }
340
341         if (only_digit) goto iso;
342
343         switch (tolower(chset[0])) {
344          case 'i':
345            if (tolower(chset[1]) == 's' && tolower(chset[2]) == 'o') {
346               chset += 3;
347               iso:
348               if (strncmp(chset, "8859", 4) == 0) {
349                  chset += 4;
350                  while (chset < p && *chset && !isdigit((unsigned char)*chset))
351                     chset++;
352                  switch (atoi(chset)) {
353                   case 1: return CHARSET_ISO_8859_1;
354                   case 2: return CHARSET_ISO_8859_2;
355                   case 15: return CHARSET_ISO_8859_15;
356                   default: return CHARSET_USASCII;
357                  }
358               }
359            }
360            break;
361          case 'u':
362            if (tolower(chset[1]) == 't' && tolower(chset[2]) == 'f') {
363               chset += 3;
364               while (chset < p && *chset && !isdigit((unsigned char)*chset))
365                  chset++;
366               switch (atoi(chset)) {
367                case 8: return CHARSET_UTF8;
368                default: return CHARSET_USASCII;
369               }
370            }
371         }
372      }
373   }
374   return CHARSET_USASCII;
375#endif
376#else
377# error Do not know operating system 'OS'
378#endif
379}
380
381/* It seems that Swedish and maybe some other scandanavian languages don't
382 * transliterate &auml; to ae - but it seems there may be conflicting views
383 * on this...
384 */
385#define umlaut_to_e() 1
386
387/* values <= 127 already dealt with */
388static int
389add_unicode(int charset, unsigned char *p, int value)
390{
391#ifdef DEBUG
392   fprintf(stderr, "add_unicode(%d, %p, %d)\n", charset, p, value);
393#endif
394   if (value == 0) return 0;
395   switch (charset) {
396   case CHARSET_USASCII:
397      if (value < 0x80) {
398         *p = value;
399         return 1;
400      }
401      break;
402   case CHARSET_ISO_8859_1:
403      if (value < 0x100) {
404         *p = value;
405         return 1;
406      }
407      break;
408   case CHARSET_ISO_8859_2:
409      if (value >= 0xa0) {
410         int v = 0;
411         switch (value) {
412            case 0xa0: case 0xa4: case 0xa7: case 0xa8: case 0xad: case 0xb0:
413            case 0xb4: case 0xb8: case 0xc1: case 0xc2: case 0xc4: case 0xc7:
414            case 0xc9: case 0xcb: case 0xcd: case 0xce: case 0xd3: case 0xd4:
415            case 0xd6: case 0xd7: case 0xda: case 0xdc: case 0xdd: case 0xdf:
416            case 0xe1: case 0xe2: case 0xe4: case 0xe7: case 0xe9: case 0xeb:
417            case 0xed: case 0xee: case 0xf3: case 0xf4: case 0xf6: case 0xf7:
418            case 0xfa: case 0xfc: case 0xfd:
419               v = value; break;
420            case 0x104: v = '\xa1'; break;
421            case 0x2d8: v = '\xa2'; break;
422            case 0x141: v = '\xa3'; break;
423            case 0x13d: v = '\xa5'; break;
424            case 0x15a: v = '\xa6'; break;
425            case 0x160: v = '\xa9'; break;
426            case 0x15e: v = '\xaa'; break;
427            case 0x164: v = '\xab'; break;
428            case 0x179: v = '\xac'; break;
429            case 0x17d: v = '\xae'; break;
430            case 0x17b: v = '\xaf'; break;
431            case 0x105: v = '\xb1'; break;
432            case 0x2db: v = '\xb2'; break;
433            case 0x142: v = '\xb3'; break;
434            case 0x13e: v = '\xb5'; break;
435            case 0x15b: v = '\xb6'; break;
436            case 0x2c7: v = '\xb7'; break;
437            case 0x161: v = '\xb9'; break;
438            case 0x15f: v = '\xba'; break;
439            case 0x165: v = '\xbb'; break;
440            case 0x17a: v = '\xbc'; break;
441            case 0x2dd: v = '\xbd'; break;
442            case 0x17e: v = '\xbe'; break;
443            case 0x17c: v = '\xbf'; break;
444            case 0x154: v = '\xc0'; break;
445            case 0x102: v = '\xc3'; break;
446            case 0x139: v = '\xc5'; break;
447            case 0x106: v = '\xc6'; break;
448            case 0x10c: v = '\xc8'; break;
449            case 0x118: v = '\xca'; break;
450            case 0x11a: v = '\xcc'; break;
451            case 0x10e: v = '\xcf'; break;
452            case 0x110: v = '\xd0'; break;
453            case 0x143: v = '\xd1'; break;
454            case 0x147: v = '\xd2'; break;
455            case 0x150: v = '\xd5'; break;
456            case 0x158: v = '\xd8'; break;
457            case 0x16e: v = '\xd9'; break;
458            case 0x170: v = '\xdb'; break;
459            case 0x162: v = '\xde'; break;
460            case 0x155: v = '\xe0'; break;
461            case 0x103: v = '\xe3'; break;
462            case 0x13a: v = '\xe5'; break;
463            case 0x107: v = '\xe6'; break;
464            case 0x10d: v = '\xe8'; break;
465            case 0x119: v = '\xea'; break;
466            case 0x11b: v = '\xec'; break;
467            case 0x10f: v = '\xef'; break;
468            case 0x111: v = '\xf0'; break;
469            case 0x144: v = '\xf1'; break;
470            case 0x148: v = '\xf2'; break;
471            case 0x151: v = '\xf5'; break;
472            case 0x159: v = '\xf8'; break;
473            case 0x16f: v = '\xf9'; break;
474            case 0x171: v = '\xfb'; break;
475            case 0x163: v = '\xfe'; break;
476            case 0x2d9: v = '\xff'; break;
477         }
478         if (v == 0) break;
479         value = v;
480      }
481      *p = value;
482      return 1;
483   case CHARSET_ISO_8859_15:
484      switch (value) {
485       case 0xa4: case 0xa6: case 0xb0: case 0xc4:
486       case 0xd0: case 0xd4: case 0xd5: case 0xd6:
487         goto donthave;
488       case 0x152: value = 0xd4; break; /* &OElig; */
489       case 0x153: value = 0xd5; break; /* &oelig; */
490#if 0
491       case 0x0: value = 0xa4; break; /* euro */
492#endif
493       case 0x160: value = 0xa6; break; /* Scaron */
494       case 0x161: value = 0xb0; break; /* scaron */
495       case 0x17d: value = 0xc4; break; /* Zcaron */
496       case 0x17e: value = 0xd0; break; /* zcaron */
497#if 0
498       case 0x0: value = 0xd6; break; /* Ydiersis */
499#endif
500      }
501      if (value < 0x100) {
502         *p = value;
503         return 1;
504      }
505      donthave:
506      break;
507#if (OS==RISCOS)
508   case CHARSET_RISCOS31:
509      /* RISC OS 3.1 (and later) extensions to ISO-8859-1 */
510      switch (value) {
511       case 0x152: value = 0x9a; break; /* &OElig; */
512       case 0x153: value = 0x9b; break; /* &oelig; */
513#if 0
514       case 0x174: value = 0x81; break; /* &Wcirc; */
515       case 0x175: value = 0x82; break; /* &wcirc; */
516       case 0x176: value = 0x85; break; /* &Ycirc; */
517       case 0x177: value = 0x86; break; /* &ycirc; */
518#endif
519      }
520      if (value < 0x100) {
521         *p = value;
522         return 1;
523      }
524      break;
525#elif (OS==WIN32)
526   case CHARSET_WINCP1250:
527      /* MS Windows rough equivalent to ISO-8859-2 */
528      if (value >= 0x80) {
529         int v = 0;
530         switch (value) {
531            case 0xa0: case 0xa4: case 0xa6: case 0xa7: case 0xa8: case 0xa9:
532            case 0xab: case 0xac: case 0xad: case 0xae: case 0xb0: case 0xb1:
533            case 0xb4: case 0xb5: case 0xb6: case 0xb7: case 0xb8: case 0xbb:
534            case 0xc1: case 0xc2: case 0xc4: case 0xc7: case 0xc9: case 0xcb:
535            case 0xcd: case 0xce: case 0xd3: case 0xd4: case 0xd6: case 0xd7:
536            case 0xda: case 0xdc: case 0xdd: case 0xdf: case 0xe1: case 0xe2:
537            case 0xe4: case 0xe7: case 0xe9: case 0xeb: case 0xed: case 0xee:
538            case 0xf3: case 0xf4: case 0xf6: case 0xf7: case 0xfa: case 0xfc:
539            case 0xfd:
540               v = value; break;
541            case 0x20ac: v = '\x80'; break;
542            case 0x201a: v = '\x82'; break;
543            case 0x201e: v = '\x84'; break;
544            case 0x2026: v = '\x85'; break;
545            case 0x2020: v = '\x86'; break;
546            case 0x2021: v = '\x87'; break;
547            case 0x2030: v = '\x89'; break;
548            case 0x0160: v = '\x8a'; break;
549            case 0x2039: v = '\x8b'; break;
550            case 0x015a: v = '\x8c'; break;
551            case 0x0164: v = '\x8d'; break;
552            case 0x017d: v = '\x8e'; break;
553            case 0x0179: v = '\x8f'; break;
554            case 0x2018: v = '\x91'; break;
555            case 0x2019: v = '\x92'; break;
556            case 0x201c: v = '\x93'; break;
557            case 0x201d: v = '\x94'; break;
558            case 0x2022: v = '\x95'; break;
559            case 0x2013: v = '\x96'; break;
560            case 0x2014: v = '\x97'; break;
561            case 0x2122: v = '\x99'; break;
562            case 0x0161: v = '\x9a'; break;
563            case 0x203a: v = '\x9b'; break;
564            case 0x015b: v = '\x9c'; break;
565            case 0x0165: v = '\x9d'; break;
566            case 0x017e: v = '\x9e'; break;
567            case 0x017a: v = '\x9f'; break;
568            case 0x02c7: v = '\xa1'; break;
569            case 0x02d8: v = '\xa2'; break;
570            case 0x0141: v = '\xa3'; break;
571            case 0x0104: v = '\xa5'; break;
572            case 0x015e: v = '\xaa'; break;
573            case 0x017b: v = '\xaf'; break;
574            case 0x02db: v = '\xb2'; break;
575            case 0x0142: v = '\xb3'; break;
576            case 0x0105: v = '\xb9'; break;
577            case 0x015f: v = '\xba'; break;
578            case 0x013d: v = '\xbc'; break;
579            case 0x02dd: v = '\xbd'; break;
580            case 0x013e: v = '\xbe'; break;
581            case 0x017c: v = '\xbf'; break;
582            case 0x0154: v = '\xc0'; break;
583            case 0x0102: v = '\xc3'; break;
584            case 0x0139: v = '\xc5'; break;
585            case 0x0106: v = '\xc6'; break;
586            case 0x010c: v = '\xc8'; break;
587            case 0x0118: v = '\xca'; break;
588            case 0x011a: v = '\xcc'; break;
589            case 0x010e: v = '\xcf'; break;
590            case 0x0110: v = '\xd0'; break;
591            case 0x0143: v = '\xd1'; break;
592            case 0x0147: v = '\xd2'; break;
593            case 0x0150: v = '\xd5'; break;
594            case 0x0158: v = '\xd8'; break;
595            case 0x016e: v = '\xd9'; break;
596            case 0x0170: v = '\xdb'; break;
597            case 0x0162: v = '\xde'; break;
598            case 0x0155: v = '\xe0'; break;
599            case 0x0103: v = '\xe3'; break;
600            case 0x013a: v = '\xe5'; break;
601            case 0x0107: v = '\xe6'; break;
602            case 0x010d: v = '\xe8'; break;
603            case 0x0119: v = '\xea'; break;
604            case 0x011b: v = '\xec'; break;
605            case 0x010f: v = '\xef'; break;
606            case 0x0111: v = '\xf0'; break;
607            case 0x0144: v = '\xf1'; break;
608            case 0x0148: v = '\xf2'; break;
609            case 0x0151: v = '\xf5'; break;
610            case 0x0159: v = '\xf8'; break;
611            case 0x016f: v = '\xf9'; break;
612            case 0x0171: v = '\xfb'; break;
613            case 0x0163: v = '\xfe'; break;
614            case 0x02d9: v = '\xff'; break;
615         }
616         if (v == 0) break;
617         value = v;
618      }
619      *p = value;
620      return 1;
621   case CHARSET_WINCP1252:
622      /* MS Windows extensions to ISO-8859-1 */
623      switch (value) {
624       case 0x152: value = 0x8c; break; /* &OElig; */
625       case 0x153: value = 0x9c; break; /* &oelig; */
626#if 0
627      /* there are a few other obscure ones we don't currently need */
628#endif
629      }
630      if (value < 0x100) {
631         *p = value;
632         return 1;
633      }
634      break;
635#endif
636#if (OS==MSDOS)
637   case CHARSET_DOSCP437: {
638      unsigned char uni2dostab[] = {
639          255, 173, 155, 156,   0, 157,   0,   0,
640            0,   0, 166, 174, 170,   0,   0,   0,
641          248, 241, 253,   0,   0, 230,   0, 250,
642            0,   0, 167, 175, 172, 171,   0, 168,
643            0,   0,   0,   0, 142, 143, 146, 128,
644            0, 144,   0,   0,   0,   0,   0,   0,
645            0, 165,   0,   0,   0,   0, 153,   0,
646            0,   0,   0,   0, 154,   0,   0, 225,
647          133, 160, 131,   0, 132, 134, 145, 135,
648          138, 130, 136, 137, 141, 161, 140, 139,
649            0, 164, 149, 162, 147,   0, 148, 246,
650            0, 151, 163, 150, 129,   0,   0, 152,
651      };
652      if (value >= 160 && value < 256) {
653         int ch = (int)uni2dostab[value - 160];
654         if (!ch) break;
655         *p = ch;
656         return 1;
657      }
658#if 0
659      switch (value) {
660          case 8359: *p = 158; return 1; /* PESETA SIGN */
661          case 402: *p = 159; return 1; /* LATIN SMALL LETTER F WITH HOOK */
662          case 8976: *p = 169; return 1; /* REVERSED NOT SIGN */
663          case 945: *p = 224; return 1; /* GREEK SMALL LETTER ALPHA */
664          case 915: *p = 226; return 1; /* GREEK CAPITAL LETTER GAMMA */
665          case 960: *p = 227; return 1; /* GREEK SMALL LETTER PI */
666          case 931: *p = 228; return 1; /* GREEK CAPITAL LETTER SIGMA */
667          case 963: *p = 229; return 1; /* GREEK SMALL LETTER SIGMA */
668          case 964: *p = 231; return 1; /* GREEK SMALL LETTER TAU */
669          case 934: *p = 232; return 1; /* GREEK CAPITAL LETTER PHI */
670          case 920: *p = 233; return 1; /* GREEK CAPITAL LETTER THETA */
671          case 937: *p = 234; return 1; /* GREEK CAPITAL LETTER OMEGA */
672          case 948: *p = 235; return 1; /* GREEK SMALL LETTER DELTA */
673          case 8734: *p = 236; return 1; /* INFINITY */
674          case 966: *p = 237; return 1; /* GREEK SMALL LETTER PHI */
675          case 949: *p = 238; return 1; /* GREEK SMALL LETTER EPSILON */
676          case 8745: *p = 239; return 1; /* INTERSECTION */
677          case 8801: *p = 240; return 1; /* IDENTICAL TO */
678          case 8805: *p = 242; return 1; /* GREATER-THAN OR EQUAL TO */
679          case 8804: *p = 243; return 1; /* LESS-THAN OR EQUAL TO */
680          case 8992: *p = 244; return 1; /* TOP HALF INTEGRAL */
681          case 8993: *p = 245; return 1; /* BOTTOM HALF INTEGRAL */
682          case 8776: *p = 247; return 1; /* ALMOST EQUAL TO */
683          case 8729: *p = 249; return 1; /* BULLET OPERATOR */
684          case 8730: *p = 251; return 1; /* SQUARE ROOT */
685          case 8319: *p = 252; return 1; /* SUPERSCRIPT LATIN SMALL LETTER N */
686          case 9632: *p = 254; return 1; /* BLACK SQUARE */
687      }
688#endif
689      break;
690   }
691#endif
692#if (OS==MSDOS || OS==WIN32)
693   case CHARSET_DOSCP850: {
694      unsigned char uni2dostab[] = {
695         255, 173, 189, 156, 207, 190, 221, 245,
696         249, 184, 166, 174, 170, 240, 169, 238,
697         248, 241, 253, 252, 239, 230, 244, 250,
698         247, 251, 167, 175, 172, 171, 243, 168,
699         183, 181, 182, 199, 142, 143, 146, 128,
700         212, 144, 210, 211, 222, 214, 215, 216,
701         209, 165, 227, 224, 226, 229, 153, 158,
702         157, 235, 233, 234, 154, 237, 232, 225,
703         133, 160, 131, 198, 132, 134, 145, 135,
704         138, 130, 136, 137, 141, 161, 140, 139,
705         208, 164, 149, 162, 147, 228, 148, 246,
706         155, 151, 163, 150, 129, 236, 231, 152
707      };
708      if (value >= 160 && value < 256) {
709         *p = (int)uni2dostab[value - 160];
710         return 1;
711      }
712#if 0
713      if (value == 305) { /* LATIN SMALL LETTER DOTLESS I */
714         *p = 213;
715         return 1;
716      }
717      if (value == 402) { /* LATIN SMALL LETTER F WITH HOOK */
718         *p = 159;
719         return 1;
720      }
721#endif
722      break;
723   }
724#endif
725   }
726   /* Transliterate characters we can't represent */
727#ifdef DEBUG
728   fprintf(stderr, "transliterate `%c' 0x%x\n", value, value);
729#endif
730   switch (value) {
731    case 160:
732      *p = ' '; return 1;
733    case 161 /* ¡ */:
734      *p = '!'; return 1;
735    case 171 /* « */:
736      p[1] = *p = '<'; return 2;
737    case 187 /* » */:
738      p[1] = *p = '>'; return 2;
739    case 191 /* ¿ */:
740      *p = '?'; return 1;
741    case 192 /* À */: case 193 /* Á */: case 194 /* Â */: case 195 /* Ã */:
742      *p = 'A'; return 1;
743    case 197 /* Å */:
744      p[1] = *p = 'A'; return 2;
745    case 196 /* Ä */: /* &Auml; */
746      *p = 'A';
747      if (!umlaut_to_e()) return 1;
748      p[1] = 'E'; return 2;
749    case 198 /* Æ */:
750      *p = 'A'; p[1] = 'E'; return 2;
751    case 199 /* Ç */: case 268: /* &Ccaron; */
752      *p = 'C'; return 1;
753    case 270: /* &Dcaron; */
754      *p = 'D'; return 1;
755    case 200 /* È */: case 201 /* É */: case 202 /* Ê */: case 203 /* Ë */:
756      *p = 'E'; return 1;
757    case 204 /* Ì */: case 205 /* Í */: case 206 /* Î */: case 207 /* Ï */:
758      *p = 'I'; return 1;
759    case 208 /* Ð */: case 222 /* Þ */:
760      *p = 'T'; p[1] = 'H'; return 2;
761    case 315: /* &Lacute; */
762      *p = 'L'; return 1;
763    case 209 /* Ñ */:
764      *p = 'N'; return 1;
765    case 210 /* Ò */: case 211 /* Ó */: case 212 /* Ô */: case 213 /* Õ */:
766      *p = 'O'; return 1;
767    case 214 /* Ö */: /* &Ouml; */ case 0x152: /* &OElig; */
768      *p = 'O'; p[1] = 'E'; return 2;
769    case 352: /* &Scaron; */
770      *p = 'S'; return 1;
771    case 217 /* Ù */: case 218 /* Ú */: case 219 /* Û */:
772      *p = 'U'; return 1;
773    case 220 /* Ü */: /* &Uuml; */
774      *p = 'U'; p[1] = 'E'; return 2;
775    case 221 /* Ý */:
776      *p = 'Y'; return 1;
777    case 381: /* &Zcaron; */
778      *p = 'Z'; return 1;
779    case 223 /* ß */:
780      p[1] = *p = 's'; return 2;
781    case 224 /* à */: case 225 /* á */: case 226 /* â */: case 227 /* ã */:
782      *p = 'a'; return 1;
783    case 228 /* ä */: /* &auml; */ case 230 /* æ */:
784      *p = 'a'; p[1] = 'e'; return 2;
785    case 229 /* å */:
786      p[1] = *p = 'a'; return 2;
787    case 231 /* ç */: case 269 /* &ccaron; */:
788      *p = 'c'; return 1;
789    case 271: /* &dcaron; */
790      *p = 'd'; return 1;
791    case 232 /* è */: case 233 /* é */: case 234 /* ê */: case 235 /* ë */:
792    case 283 /* &ecaron; */:
793      *p = 'e'; return 1;
794    case 236 /* ì */: case 237 /* í */: case 238 /* î */: case 239 /* ï */:
795      *p = 'i'; return 1;
796    case 316 /* &lacute; */:
797      *p = 'l'; return 1;
798    case 241 /* ñ */: case 328 /* &ncaron; */:
799      *p = 'n'; return 1;
800    case 345: /* &rcaron; */
801      *p = 'r'; return 1;
802    case 353: /* &scaron; */
803      *p = 's'; return 1;
804    case 357: /* &tcaron; */
805      *p = 't'; return 1;
806    case 240 /* ð */: case 254 /* þ */:
807      *p = 't'; p[1] = 'h'; return 2;
808    case 242 /* ò */: case 243 /* ó */: case 244 /* ô */: case 245 /* õ */:
809      *p = 'o'; return 1;
810    case 246 /* ö */: /* &ouml; */ case 0x153: /* &oelig; */
811      *p = 'o'; p[1] = 'e'; return 2;
812    case 249 /* ù */: case 250 /* ú */: case 251 /* û */:
813    case 367 /* &uring; */:
814      *p = 'u'; return 1;
815    case 252 /* ü */: /* &uuml; */
816      *p = 'u'; p[1] = 'e'; return 2;
817    case 253 /* ý */: case 255 /* ÿ */:
818      *p = 'y'; return 1;
819    case 382: /* &zcaron; */
820      *p = 'z'; return 1;
821   }
822#ifdef DEBUG
823   fprintf(stderr, "failed to transliterate\n");
824#endif
825   return 0;
826}
827
828#if (OS==UNIX) && defined(DATADIR) && defined(PACKAGE)
829/* Under Unix, we compile in the configured path */
830static const char *pth_cfg_files = DATADIR "/" PACKAGE;
831#else
832/* On other platforms, we fall back on looking in the current directory */
833static const char *pth_cfg_files = "";
834#endif
835
836static int num_msgs = 0;
837static char **msg_array = NULL;
838
839const char *msg_lang = NULL;
840const char *msg_lang2 = NULL;
841
842static char **
843parse_msgs(int n, unsigned char *p, int charset_code) {
844   int i;
845
846   char **msgs = osmalloc(n * sizeof(char *));
847
848   for (i = 0; i < n; i++) {
849      unsigned char *to = p;
850      int ch;
851      msgs[i] = (char *)p;
852
853      /* If we want UTF8 anyway, we just need to find the start of each
854       * message */
855      if (charset_code == CHARSET_UTF8) {
856         p += strlen((char *)p) + 1;
857         continue;
858      }
859
860      while ((ch = *p++) != 0) {
861         /* A byte in the range 0x80-0xbf or 0xf0-0xff isn't valid in
862          * this state, (0xf0-0xfd mean values > 0xffff) so treat as
863          * literal and try to resync so we cope better when fed
864          * non-utf-8 data.  Similarly we abandon a multibyte sequence
865          * if we hit an invalid character. */
866         if (ch >= 0xc0 && ch < 0xf0) {
867            int ch1 = *p;
868            if ((ch1 & 0xc0) != 0x80) goto resync;
869
870            if (ch < 0xe0) {
871               /* 2 byte sequence */
872               ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f);
873               p++;
874            } else {
875               /* 3 byte sequence */
876               int ch2 = p[1];
877               if ((ch2 & 0xc0) != 0x80) goto resync;
878               ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6) | (ch2 & 0x3f);
879               p += 2;
880            }
881         }
882
883         resync:
884
885         if (ch < 127) {
886            *to++ = (char)ch;
887         } else {
888            /* We assume an N byte UTF-8 code never transliterates to more
889             * than N characters (so we can't transliterate © to (C) or
890             * ® to (R) for example) */
891            to += add_unicode(charset_code, to, ch);
892         }
893      }
894      *to++ = '\0';
895   }
896   return msgs;
897}
898
899/* This is the name of the default language, which can be set like so:
900 * ./configure --enable-defaultlang=fr
901 */
902#ifdef DEFAULTLANG
903/* No point extracting these errors as they won't get used if file opens */
904# include "../lib/defaultlang.h"
905#else
906#define N_DONTEXTRACTMSGS 5
907static unsigned char dontextractmsgs[] =
908   "Can't open message file `%s' using path `%s'\0"/*1000*/
909   "Problem with message file `%s'\0"/*1001*/
910   "I don't understand this message file version\0"/*1002*/
911   "Message file truncated?\0"/*1003*/
912   "Out of memory (couldn't find %lu bytes).\0"/*1004*/;
913#endif
914
915static char **dontextract = NULL;
916
917static void
918parse_msg_file(int charset_code)
919{
920   FILE *fh;
921   unsigned char header[20];
922   int i;
923   unsigned len;
924   unsigned char *p;
925   char *fnm, *s;
926   int n;
927
928#ifdef DEBUG
929   fprintf(stderr, "parse_msg_file(%d)\n", charset_code);
930#endif
931
932   /* sort out messages we need to print if we can't open the message file */
933   dontextract = parse_msgs(N_DONTEXTRACTMSGS, dontextractmsgs, charset_code);
934
935   fnm = osstrdup(msg_lang);
936   /* trim off charset from stuff like "de_DE.iso8859_1" */
937   s = strchr(fnm, '.');
938   if (s) *s = '\0';
939
940   fh = fopenWithPthAndExt(pth_cfg_files, fnm, EXT_SVX_MSG, "rb", NULL);
941
942   if (!fh) {
943      /* e.g. if 'en_GB' is unknown, see if we know 'en' */
944      if (strlen(fnm) > 3 && fnm[2] == '_') {
945         fnm[2] = '\0';
946         fh = fopenWithPthAndExt(pth_cfg_files, fnm, EXT_SVX_MSG, "rb", NULL);
947         if (!fh) fnm[2] = '_'; /* for error reporting */
948      }
949   }
950
951   if (!fh) {
952      fatalerror(/*Can't open message file `%s' using path `%s'*/1000,
953                 fnm, pth_cfg_files);
954   }
955
956   if (fread(header, 1, 20, fh) < 20 ||
957       memcmp(header, "Svx\nMsg\r\n\xfe\xff", 12) != 0) {
958      fatalerror(/*Problem with message file `%s'*/1001, fnm);
959   }
960
961   if (header[12] != 0)
962      fatalerror(/*I don't understand this message file version*/1002);
963
964   n = (header[14] << 8) | header[15];
965
966   len = 0;
967   for (i = 16; i < 20; i++) len = (len << 8) | header[i];
968
969   p = osmalloc(len);
970   if (fread(p, 1, len, fh) < len)
971      fatalerror(/*Message file truncated?*/1003);
972
973   fclose(fh);
974
975#ifdef DEBUG
976   fprintf(stderr, "fnm = `%s', n = %d, len = %d\n", fnm, n, len);
977#endif
978   osfree(fnm);
979
980   msg_array = parse_msgs(n, p, charset_code);
981   num_msgs = n;
982}
983
984const char *
985msg_cfgpth(void)
986{
987   return pth_cfg_files;
988}
989
990const char *
991msg_appname(void)
992{
993   return appname_copy;
994}
995
996void
997msg_init(char * const *argv)
998{
999   char *p;
1000   SVX_ASSERT(argv);
1001
1002#ifdef HAVE_SIGNAL
1003   init_signals();
1004#endif
1005   /* Point to argv[0] itself so we report a more helpful error if the
1006    * code to work out the clean appname generates a signal */
1007   appname_copy = argv[0];
1008#if (OS == UNIX)
1009   /* use name as-is on Unix - programs run from path get name as supplied */
1010   appname_copy = osstrdup(argv[0]);
1011#else
1012   /* use the lower-cased leafname on other platforms */
1013   p = leaf_from_fnm(argv[0]);
1014   appname_copy = p;
1015   while (*p) {
1016      *p = tolower(*p);
1017      ++p;
1018   }
1019#endif
1020
1021   /* shortcut --version so you can check the version number even when the
1022    * correct message file can't be found... */
1023   if (argv[1] && strcmp(argv[1], "--version") == 0) {
1024      cmdline_version();
1025      exit(0);
1026   }
1027
1028   if (argv[0]) {
1029#if (OS==UNIX) && defined(DATADIR) && defined(PACKAGE)
1030      bool free_pth = fFalse;
1031      char *pth = getenv("srcdir");
1032      if (!pth || !pth[0]) {
1033         pth = path_from_fnm(argv[0]);
1034         free_pth = fTrue;
1035      }
1036      if (pth[0]) {
1037         /* If we're run with an explicit path, check if "../lib/en.msg"
1038          * from the program's path exists, and if so look there for
1039          * support files - this allows us to test binaries in the build
1040          * tree easily. */
1041         /* May also be useful on MacOS X where the programs may be
1042          * installed anywhere... */
1043         char *p = use_path(pth, "../lib/en.msg");
1044         FILE *f = fopen(p, "r");
1045         if (f) {
1046            close(f);
1047            pth_cfg_files = use_path(pth, "../lib");
1048         }
1049         osfree(p);
1050      }
1051      if (free_pth) osfree(pth);
1052#elif (OS==WIN32)
1053      DWORD len = 256;
1054      char *buf = NULL, *modname;
1055      while (1) {
1056          DWORD got;
1057          buf = osrealloc(buf, len);
1058          got = GetModuleFileName(NULL, buf, len);
1059          if (got < len) break;
1060          len += len;
1061      }
1062      modname = buf;
1063      /* Strange Win32 nastiness - strip prefix "\\?\" if present */
1064      if (strncmp(modname, "\\\\?\\", 4) == 0) modname += 4;
1065      pth_cfg_files = path_from_fnm(modname);
1066      osfree(buf);
1067#else
1068      /* Get the path to the support files from argv[0] */
1069      pth_cfg_files = path_from_fnm(argv[0]);
1070#endif
1071   }
1072
1073   msg_lang = getenv("SURVEXLANG");
1074#ifdef DEBUG
1075   fprintf(stderr, "msg_lang = %p (= \"%s\")\n", msg_lang, msg_lang?msg_lang:"(null)");
1076#endif
1077
1078   if (!msg_lang || !*msg_lang) {
1079      msg_lang = getenv("LANG");
1080      if (!msg_lang || !*msg_lang) {
1081#if (OS==WIN32)
1082         LCID locid;
1083#elif (OS==RISCOS)
1084         territory_t t;
1085#endif
1086#ifdef DEFAULTLANG
1087         msg_lang = STRING(DEFAULTLANG);
1088#else
1089         msg_lang = "en";
1090#endif
1091#if (OS==WIN32)
1092         locid = GetUserDefaultLCID();
1093         if (locid) {
1094            WORD langid = LANGIDFROMLCID(locid);
1095            switch (PRIMARYLANGID(langid)) {
1096/* older mingw compilers don't seem to supply this value */
1097#ifndef LANG_CATALAN
1098# define LANG_CATALAN 0x03
1099#endif
1100             case LANG_CATALAN:
1101               msg_lang = "ca";
1102               break;
1103             case LANG_CHINESE:
1104               msg_lang = "zh";
1105               break;
1106             case LANG_ENGLISH:
1107               if (SUBLANGID(langid) == SUBLANG_ENGLISH_US)
1108                  msg_lang = "en_US";
1109               else
1110                  msg_lang = "en";
1111               break;
1112             case LANG_FRENCH:
1113               msg_lang = "fr";
1114               break;
1115             case LANG_GERMAN:
1116               switch (SUBLANGID(langid)) {
1117                case SUBLANG_GERMAN_SWISS:
1118                  msg_lang = "de_CH";
1119                  break;
1120                case SUBLANG_GERMAN:
1121                  msg_lang = "de_DE";
1122                  break;
1123                default:
1124                  msg_lang = "de";
1125               }
1126               break;
1127             case LANG_ITALIAN:
1128               msg_lang = "it";
1129               break;
1130             case LANG_PORTUGUESE:
1131               if (SUBLANGID(langid) == SUBLANG_PORTUGUESE_BRAZILIAN)
1132                  msg_lang = "pt_BR";
1133               else
1134                  msg_lang = "pt";
1135               break;
1136             case LANG_SLOVAK:
1137               msg_lang = "sk";
1138               break;
1139             case LANG_SPANISH:
1140               msg_lang = "es";
1141               break;
1142            }
1143         }
1144#elif (OS==RISCOS)
1145         if (!xterritory_number(&t)) switch (t) {
1146          case 1: /* UK */
1147          case 2: /* Master */
1148          case 3: /* Compact */
1149          case 17: /* Canada1 */
1150          case 19: /* Canada */
1151          case 22: /* Ireland */
1152            msg_lang = "en";
1153            break;
1154          case 4: /* Italy */
1155            msg_lang = "it";
1156            break;
1157          case 5: /* Spain (or ca) */
1158          case 27: /* Mexico */
1159          case 28: /* LatinAm (or pt_BR) */
1160            msg_lang = "es";
1161            break;
1162          case 6: /* France */
1163          case 18: /* Canada2 */
1164            msg_lang = "fr";
1165            break;
1166          case 7: /* Germany */
1167            msg_lang = "de_DE";
1168            break;
1169          case 8: /* Portugal */
1170            msg_lang = "pt";
1171            break;
1172          case 23: /* Hong Kong */
1173            msg_lang = "zh";
1174            break;
1175          case 48: /* USA */
1176            msg_lang = "en_US";
1177            break;
1178#if 0
1179          case 9: /* Esperanto */
1180          case 10: /* Greece */
1181          case 11: /* Sweden */
1182          case 12: /* Finland */
1183          case 13: /* Unused */
1184          case 14: /* Denmark */
1185          case 15: /* Norway */
1186          case 16: /* Iceland */
1187          case 20: /* Turkey */
1188          case 21: /* Arabic */
1189          case 24: /* Russia */
1190          case 25: /* Russia2 */
1191          case 26: /* Israel */
1192#endif
1193         }
1194#elif (OS==MSDOS)
1195           {
1196              int country_code;
1197# ifdef __DJGPP__
1198              __dpmi_regs r;
1199              r.x.ax = 0x6501;
1200              r.x.bx = 0xffff;
1201              r.x.dx = 0xffff;
1202              /* Use DJGPP's transfer buffer (which is at least 2K) */
1203              r.x.es = __tb >> 4;
1204              r.x.di = __tb & 0x0f;
1205              r.x.cx = 2048;
1206              /* bit 1 is the carry flag */
1207              if (__dpmi_int(0x21, &r) != -1 && !(r.x.flags & 1)) {
1208                 unsigned short p;
1209                 dosmemget(__tb + 3, 2, &p);
1210                 country_code = p;
1211# else
1212              union REGS r;
1213              r.x.ax = 0x3800; /* get current country info */
1214              r.x.dx = 0;
1215              intdos(&r, &r);
1216              if (!r.x.cflag) {
1217                 country_code = r.x.bx;
1218# endif
1219                 /* List of country codes taken from:
1220                  * http://www.delorie.com/djgpp/doc/rbinter/it/00/14.html */
1221                 /* The mappings here are guesses at best in most cases.
1222                  * In a lot of cases we pick a language because we have
1223                  * a translation in it, rather than because it's the most
1224                  * widely used or understood in that country. */
1225                 /* Improvements welcome */
1226                 switch (country_code) {
1227                     case 1: /* United States */
1228                     case 670: /* Saipan / N. Mariana Island */
1229                     case 671: /* Guam */
1230                     case 680: /* Palau */
1231                     case 684: /* American Samoa */
1232                     case 691: /* Micronesia */
1233                     case 692: /* Marshall Islands */
1234                         msg_lang = "en_US";
1235                         break;
1236                     case 4: /* Canada (English) */
1237                     case 27: /* South Africa */
1238                     case 44: /* United Kingdom */
1239                     case 61: /* International English / Australia */
1240                     case 64: /* New Zealand */
1241                     case 99: /* Asia (English) */
1242                     case 220: /* Gambia */
1243                     case 231: /* Liberia */
1244                     case 232: /* Sierra Leone */
1245                     case 233: /* Ghana */
1246                     case 254: /* Kenya */
1247                     case 256: /* Uganda */
1248                     case 260: /* Zambia */
1249                     case 263: /* Zimbabwe */
1250                     case 264: /* Namibia */
1251                     case 267: /* Botswana */
1252                     case 268: /* Swaziland */
1253                     case 290: /* St. Helena */
1254                     case 297: /* Aruba */
1255                     case 350: /* Gibraltar */
1256                     case 353: /* Ireland */
1257                     case 356: /* Malta */
1258                     case 500: /* Falkland Islands */
1259                     case 501: /* Belize */
1260                     case 592: /* Guyana */
1261                     case 672: /* Norfolk Island (Australia) / Christmas Island/Cocos Islands / Antartica */
1262                     case 673: /* Brunei Darussalam */
1263                     case 674: /* Nauru */
1264                     case 675: /* Papua New Guinea */
1265                     case 676: /* Tonga Islands */
1266                     case 677: /* Solomon Islands */
1267                     case 679: /* Fiji */
1268                     case 682: /* Cook Islands */
1269                     case 683: /* Niue */
1270                     case 685: /* Western Samoa */
1271                     case 686: /* Kiribati */
1272                         /* I believe only some of these are English speaking... */
1273                     case 809: /* Antigua and Barbuda / Anguilla / Bahamas / Barbados / Bermuda
1274                                  British Virgin Islands / Cayman Islands / Dominica
1275                                  Dominican Republic / Grenada / Jamaica / Montserra
1276                                  St. Kitts and Nevis / St. Lucia / St. Vincent and Grenadines
1277                                  Trinidad and Tobago / Turks and Caicos */
1278                         msg_lang = "en";
1279                         break;
1280                     case 2: /* Canadian-French */
1281                     case 32: /* Belgium */ /* maybe */
1282                     case 33: /* France */
1283                     case 213: /* Algeria */
1284                     case 216: /* Tunisia */
1285                     case 221: /* Senegal */
1286                     case 223: /* Mali */
1287                     case 225: /* Ivory Coast */
1288                     case 226: /* Burkina Faso */
1289                     case 227: /* Niger */
1290                     case 228: /* Togo */
1291                     case 229: /* Benin */
1292                     case 230: /* Mauritius */
1293                     case 235: /* Chad */
1294                     case 236: /* Central African Republic */
1295                     case 237: /* Cameroon */
1296                     case 241: /* Gabon */
1297                     case 242: /* Congo */
1298                     case 250: /* Rwhanda */
1299                     case 253: /* Djibouti */
1300                     case 257: /* Burundi */
1301                     case 261: /* Madagascar */
1302                     case 262: /* Reunion Island */
1303                     case 269: /* Comoros */
1304                     case 270: /* Mayotte */
1305                     case 352: /* Luxembourg (or de or ...) */
1306                     case 508: /* St. Pierre and Miquelon */
1307                     case 509: /* Haiti */
1308                     case 590: /* Guadeloupe */
1309                     case 594: /* French Guiana */
1310                     case 596: /* Martinique / French Antilles */
1311                     case 678: /* Vanuatu */
1312                     case 681: /* Wallis & Futuna */
1313                     case 687: /* New Caledonia */
1314                     case 689: /* French Polynesia */
1315                     case 961: /* Lebanon */
1316                         msg_lang = "fr";
1317                         break;
1318                     case 3: /* Latin America */
1319                     case 34: /* Spain */
1320                     case 51: /* Peru */
1321                     case 52: /* Mexico */
1322                     case 53: /* Cuba */
1323                     case 54: /* Argentina */
1324                     case 56: /* Chile */
1325                     case 57: /* Columbia */
1326                     case 58: /* Venezuela */
1327                     case 63: /* Philippines */
1328                     case 240: /* Equatorial Guinea */
1329                     case 502: /* Guatemala */
1330                     case 503: /* El Salvador */
1331                     case 504: /* Honduras */
1332                     case 505: /* Nicraragua */
1333                     case 506: /* Costa Rica */
1334                     case 507: /* Panama */
1335                     case 591: /* Bolivia */
1336                     case 593: /* Ecuador */
1337                     case 595: /* Paraguay */
1338                     case 598: /* Uruguay */
1339                         msg_lang = "es";
1340                         break;
1341                     case 39: /* Italy / San Marino / Vatican City */
1342                         msg_lang = "it";
1343                         break;
1344                     case 41: /* Switzerland / Liechtenstein */ /* or fr or ... */
1345                         msg_lang = "de_CH";
1346                         break;
1347                     case 43: /* Austria (DR DOS 5.0) */
1348                         msg_lang = "de";
1349                         break;
1350                     case 49: /* Germany */
1351                         msg_lang = "de_DE";
1352                         break;
1353                     case 55: /* Brazil (not supported by DR DOS 5.0) */
1354                         msg_lang = "pt_BR";
1355                         break;
1356                     case 238: /* Cape Verde Islands */
1357                     case 244: /* Angola */
1358                     case 245: /* Guinea-Bissau */
1359                     case 259: /* Mozambique */
1360                     case 351: /* Portugal */
1361                         msg_lang = "pt";
1362                         break;
1363                     case 42: /* Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0) */
1364                     case 421: /* Czech Republic / Tjekia (PC DOS 7+) */
1365                     case 422: /* Slovakia (reported as 421 due to a bug in COUNTRY.SYS) */
1366                         msg_lang = "sk";
1367                         break;
1368                     case 65: /* Singapore */
1369                     case 86: /* China (MS-DOS 5.0+) */
1370                     case 88: /* Taiwan (MS-DOS 5.0+) */
1371                     case 852: /* Hong Kong */
1372                     case 853: /* Macao */
1373                     case 886: /* Taiwan (MS-DOS 6.22+) */
1374                         msg_lang = "zh";
1375                         break;
1376#if 0
1377                     case 7: /* Russia */
1378                     case 20: /* Egypt */
1379                     case 30: /* Greece */
1380                     case 31: /* Netherlands */
1381                     case 35: /* Bulgaria??? */
1382                     case 36: /* Hungary (not supported by DR DOS 5.0) */
1383                     case 38: /* Yugoslavia (not supported by DR DOS 5.0) -- obsolete */
1384                     case 40: /* Romania */
1385                     case 45: /* Denmark */
1386                     case 46: /* Sweden */
1387                     case 47: /* Norway */
1388                     case 48: /* Poland (not supported by DR DOS 5.0) */
1389                     case 60: /* Malaysia */
1390                     case 62: /* Indonesia / East Timor */
1391                     case 66: /* Thailand (or Taiwan??? ) */
1392                     case 81: /* Japan (DR DOS 5.0, MS-DOS 5.0+) */
1393                     case 82: /* South Korea (DR DOS 5.0) */
1394                     case 84: /* Vietnam */
1395                     case 90: /* Turkey (MS-DOS 5.0+) */
1396                     case 91: /* India */
1397                     case 92: /* Pakistan */
1398                     case 93: /* Afghanistan */
1399                     case 94: /* Sri Lanka */
1400                     case 98: /* Iran */
1401                     case 102: /* ??? (Hebrew MS-DOS 5.0) */
1402                     case 112: /* Belarus */
1403                     case 200: /* Thailand (PC DOS 6.1+) (reported as 01due to a bug in PC DOS COUNTRY.SYS) */
1404                     case 212: /* Morocco */
1405                     case 218: /* Libya */
1406                     case 222: /* Maruitania */
1407                     case 224: /* African Guinea */
1408                     case 234: /* Nigeria */
1409                     case 239: /* Sao Tome and Principe */
1410                     case 243: /* Zaire */
1411                     case 246: /* Diego Garcia */
1412                     case 247: /* Ascension Isle */
1413                     case 248: /* Seychelles */
1414                     case 249: /* Sudan */
1415                     case 251: /* Ethiopia */
1416                     case 252: /* Somalia */
1417                     case 255: /* Tanzania */
1418                     case 265: /* Malawi */
1419                     case 266: /* Lesotho */
1420                     case 298: /* Faroe Islands */
1421                     case 299: /* Greenland */
1422                     case 354: /* Iceland */
1423                     case 355: /* Albania */
1424                     case 357: /* Cyprus */
1425                     case 358: /* Finland */
1426                     case 359: /* Bulgaria */
1427                     case 370: /* Lithuania (reported as 372 due to a bug in MS-DOS COUNTRY.SYS) */
1428                     case 371: /* Latvia (reported as 372 due to a bug in MS-DOS COUNTRY.SYS) */
1429                     case 372: /* Estonia */
1430                     case 373: /* Moldova */
1431                     case 375: /* ??? (MS-DOS 7.10 / Windows98) */
1432                     case 380: /* Ukraine */
1433                     case 381: /* Serbia / Montenegro */
1434                     case 384: /* Croatia */
1435                     case 385: /* Croatia (PC DOS 7+) */
1436                     case 386: /* Slovenia */
1437                     case 387: /* Bosnia-Herzegovina (Latin) */
1438                     case 388: /* Bosnia-Herzegovina (Cyrillic) (PC DOS 7+) (reported as 381 due to a bug in PC DOS COUNTRY.SYS) */
1439                     case 389: /* FYR Macedonia */
1440                     case 597: /* Suriname (nl) */
1441                     case 599: /* Netherland Antilles (nl) */
1442                     case 666: /* Russia??? (PTS-DOS 6.51 KEYB) */
1443                     case 667: /* Poland??? (PTS-DOS 6.51 KEYB) */
1444                     case 668: /* Poland??? (Slavic??? ) (PTS-DOS 6.51 KEYB) */
1445                     case 688: /* Tuvalu */
1446                     case 690: /* Tokealu */
1447                     case 711: /* ??? (currency = EA$, code pages 437,737,850,852,855,857) */
1448                     case 785: /* Arabic (Middle East/Saudi Arabia/etc.) */
1449                     case 804: /* Ukraine */
1450                     case 850: /* North Korea */
1451                     case 855: /* Cambodia */
1452                     case 856: /* Laos */
1453                     case 880: /* Bangladesh */
1454                     case 960: /* Maldives */
1455                     case 962: /* Jordan */
1456                     case 963: /* Syria / Syrian Arab Republic */
1457                     case 964: /* Iraq */
1458                     case 965: /* Kuwait */
1459                     case 966: /* Saudi Arabia */
1460                     case 967: /* Yemen */
1461                     case 968: /* Oman */
1462                     case 969: /* Yemen??? (Arabic MS-DOS 5.0) */
1463                     case 971: /* United Arab Emirates */
1464                     case 972: /* Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+) */
1465                     case 973: /* Bahrain */
1466                     case 974: /* Qatar */
1467                     case 975: /* Bhutan */
1468                     case 976: /* Mongolia */
1469                     case 977: /* Nepal */
1470                     case 995: /* Myanmar (Burma) */
1471#endif
1472                 }
1473              }
1474           }
1475#endif
1476      }
1477   }
1478#ifdef DEBUG
1479   fprintf(stderr, "msg_lang = %p (= \"%s\")\n", msg_lang, msg_lang?msg_lang:"(null)");
1480#endif
1481
1482   /* On Mandrake LANG defaults to C */
1483   if (strcmp(msg_lang, "C") == 0) msg_lang = "en";
1484
1485   msg_lang = osstrdup(msg_lang);
1486
1487   /* Convert en-us to en_US, etc */
1488   p = strchr(msg_lang, '-');
1489   if (p) {
1490      *p++ = '_';
1491      while (*p) {
1492         *p = toupper(*p);
1493         p++;
1494      }
1495   }
1496
1497   p = strchr(msg_lang, '_');
1498   if (p) {
1499      *p = '\0';
1500      msg_lang2 = osstrdup(msg_lang);
1501      *p = '_';
1502   }
1503
1504#ifdef LC_MESSAGES
1505   /* try to setlocale() appropriately too */
1506   if (!setlocale(LC_MESSAGES, msg_lang)) {
1507      if (msg_lang2) setlocale(LC_MESSAGES, msg_lang2);
1508   }
1509#endif
1510
1511   select_charset(default_charset());
1512}
1513
1514/* Message may be overwritten by next call
1515 * (but not in current implementation) */
1516const char *
1517msg(int en)
1518{
1519   /* NB can't use SVX_ASSERT here! */
1520   static char badbuf[256];
1521   if (en >= 1000 && en < 1000 + N_DONTEXTRACTMSGS)
1522      return dontextract[en - 1000];
1523   if (!msg_array) {
1524      if (en != 1)  {
1525         sprintf(badbuf, "Message %d requested before msg_array initialised\n",
1526                 en);
1527         return badbuf;
1528      }
1529      /* this should be the only other message which can be requested before
1530       * the message file is opened and read... */
1531      if (!dontextract) return "Out of memory (couldn't find %lu bytes).";
1532      return dontextract[(/*Out of memory (couldn't find %lu bytes).*/1004)
1533                         - 1000];
1534   }
1535
1536   if (en < 0 || en >= num_msgs) {
1537      sprintf(badbuf, "Message %d out of range\n", en);
1538      return badbuf;
1539   }
1540
1541   if (en == 0) {
1542      const char *p = msg_array[0];
1543      if (!*p) p = "(C)";
1544      return p;
1545   }
1546
1547   return msg_array[en];
1548}
1549
1550/* returns persistent copy of message */
1551const char *
1552msgPerm(int en)
1553{
1554   return msg(en);
1555}
1556
1557void
1558v_report(int severity, const char *fnm, int line, int en, va_list ap)
1559{
1560#ifdef AVEN
1561   aven_v_report(severity, fnm, line, en, ap);
1562#else
1563   if (fnm) {
1564      fputs(fnm, STDERR);
1565      if (line) fprintf(STDERR, ":%d", line);
1566   } else {
1567      fputs(appname_copy, STDERR);
1568   }
1569   fputs(": ", STDERR);
1570
1571   if (severity == 0) {
1572      fputs(msg(/*warning*/4), STDERR);
1573      fputs(": ", STDERR);
1574   }
1575
1576   vfprintf(STDERR, msg(en), ap);
1577   fputnl(STDERR);
1578#endif
1579
1580   switch (severity) {
1581    case 0:
1582      msg_warnings++;
1583      break;
1584    case 1:
1585      msg_errors++;
1586      if (msg_errors == 50)
1587         fatalerror_in_file(fnm, 0, /*Too many errors - giving up*/19);
1588      break;
1589    case 2:
1590      exit(EXIT_FAILURE);
1591   }
1592}
1593
1594void
1595warning(int en, ...)
1596{
1597   va_list ap;
1598   va_start(ap, en);
1599   v_report(0, NULL, 0, en, ap);
1600   va_end(ap);
1601}
1602
1603void
1604error(int en, ...)
1605{
1606   va_list ap;
1607   va_start(ap, en);
1608   v_report(1, NULL, 0, en, ap);
1609   va_end(ap);
1610}
1611
1612void
1613fatalerror(int en, ...)
1614{
1615   va_list ap;
1616   va_start(ap, en);
1617   v_report(2, NULL, 0, en, ap);
1618   va_end(ap);
1619}
1620
1621void
1622warning_in_file(const char *fnm, int line, int en, ...)
1623{
1624   va_list ap;
1625   va_start(ap, en);
1626   v_report(0, fnm, line, en, ap);
1627   va_end(ap);
1628}
1629
1630void
1631error_in_file(const char *fnm, int line, int en, ...)
1632{
1633   va_list ap;
1634   va_start(ap, en);
1635   v_report(1, fnm, line, en, ap);
1636   va_end(ap);
1637}
1638
1639void
1640fatalerror_in_file(const char *fnm, int line, int en, ...)
1641{
1642   va_list ap;
1643   va_start(ap, en);
1644   v_report(2, fnm, line, en, ap);
1645   va_end(ap);
1646}
1647
1648/* Code to support switching character set at runtime (e.g. for a printer
1649 * driver to support different character sets on screen and on the printer)
1650 */
1651typedef struct charset_li {
1652   struct charset_li *next;
1653   int code;
1654   char **msg_array;
1655} charset_li;
1656
1657static charset_li *charset_head = NULL;
1658
1659static int charset = CHARSET_BAD;
1660
1661int
1662select_charset(int charset_code)
1663{
1664   int old_charset = charset;
1665   charset_li *p;
1666
1667#ifdef DEBUG
1668   fprintf(stderr, "select_charset(%d), old charset = %d\n", charset_code,
1669           charset);
1670#endif
1671
1672   charset = charset_code;
1673
1674   /* check if we've already parsed messages for new charset */
1675   for (p = charset_head; p; p = p->next) {
1676#ifdef DEBUG
1677      printf("%p: code %d msg_array %p\n", p, p->code, p->msg_array);
1678#endif
1679      if (p->code == charset) {
1680         msg_array = p->msg_array;
1681         return old_charset;
1682      }
1683   }
1684
1685   /* nope, got to reparse message file */
1686   parse_msg_file(charset_code);
1687
1688   /* add to list */
1689   p = osnew(charset_li);
1690   p->code = charset;
1691   p->msg_array = msg_array;
1692   p->next = charset_head;
1693   charset_head = p;
1694
1695   return old_charset;
1696}
Note: See TracBrowser for help on using the repository browser.