source: git/src/message.c @ cc9c8a0

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since cc9c8a0 was 6d54808, checked in by Olly Betts <olly@…>, 26 years ago

Handle LANG being en_US

git-svn-id: file:///home/survex-svn/survex/trunk@304 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 16.2 KB
Line 
1/* > message.c
2 * Fairly general purpose message and error routines
3 * Copyright (C) 1993-1998 Olly Betts
4 */
5
6/*#define DEBUG 1*/
7
8#ifdef HAVE_CONFIG_H
9# include <config.h>
10#endif
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <ctype.h>
16#include <limits.h>
17#include <errno.h>
18
19#include "whichos.h"
20#include "filename.h"
21#include "message.h"
22#include "osdepend.h"
23#include "filelist.h"
24#include "debug.h"
25
26#ifdef HAVE_SIGNAL
27# ifdef HAVE_SETJMP
28#  include <setjmp.h>
29static jmp_buf jmpbufSignal;
30#  include <signal.h>
31# else
32#  undef HAVE_SIGNAL
33# endif
34#endif
35
36/* This is the name of the default language -- set like this so folks can
37 * add (for eg) -DDEFAULTLANG="fr" to UFLG in the makefile
38 * FIXME - update wrt automake/autoconf
39 */
40#ifndef DEFAULTLANG
41# define DEFAULTLANG "en"
42#endif
43
44/* For funcs which want to be immune from messing around with different
45 * calling conventions */
46#ifndef CDECL
47# define CDECL
48#endif
49
50static int cWarnings = 0; /* keep track of how many warnings we've given */
51static int cErrors = 0;   /* and how many (non-fatal) errors */
52
53extern int error_summary(void) {
54   fprintf(STDERR, msg(/*There were %d warning(s) and %d non-fatal error(s).*/16),
55           cWarnings, cErrors);
56   fputnl(STDERR);
57   return (cErrors ? EXIT_FAILURE : EXIT_SUCCESS);
58}
59
60/* in case osmalloc() fails before szAppNameCopy is set up */
61const char *szAppNameCopy = "anonymous program";
62
63/* error code for failed osmalloc and osrealloc calls */
64static void
65outofmem(OSSIZE_T size)
66{
67   fatalerror(1/*Out of memory (couldn't find %lu bytes).*/, (unsigned long)size);
68}
69
70#ifdef TOMBSTONES
71#define TOMBSTONE_SIZE 16
72static char tombstone[TOMBSTONE_SIZE] = "012345\xfftombstone";
73#endif
74
75/* malloc with error catching if it fails. Also allows us to write special
76 * versions easily eg for DOS EMS or MS Windows.
77 */
78extern void FAR *
79osmalloc(OSSIZE_T size)
80{
81   void FAR *p;
82#ifdef TOMBSTONES
83   size += TOMBSTONE_SIZE * 2;
84   p = malloc(size);
85#else
86   p = xosmalloc(size);
87#endif
88   if (p == NULL) outofmem(size);
89#ifdef TOMBSTONES
90printf("osmalloc truep=%p truesize=%d\n",p,size);
91   memcpy(p, tombstone, TOMBSTONE_SIZE);
92   memcpy(p + size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE);
93   *(size_t *)p = size;
94   p += TOMBSTONE_SIZE;
95#endif
96   return p;
97}
98
99/* realloc with error catching if it fails. */
100extern void FAR *
101osrealloc(void *p, OSSIZE_T size)
102{
103   /* some pre-ANSI realloc implementations don't cope with a NULL pointer */
104   if (p == NULL) {
105      p = xosmalloc(size);
106   } else {
107#ifdef TOMBSTONES
108      int true_size;
109      size += TOMBSTONE_SIZE * 2;
110      p -= TOMBSTONE_SIZE;
111      true_size = *(size_t *)p;
112printf("osrealloc (in truep=%p truesize=%d)\n",p,true_size);
113      if (memcmp(p + sizeof(size_t), tombstone + sizeof(size_t),
114                 TOMBSTONE_SIZE - sizeof(size_t)) != 0) {
115         printf("start tombstone for block %p, size %d corrupted!",
116                p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2);     
117      }
118      if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone,
119                 TOMBSTONE_SIZE) != 0) {
120         printf("end tombstone for block %p, size %d corrupted!",
121                p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2);     
122      }
123      p = realloc(p, size);
124      if (p == NULL) outofmem(size);
125printf("osrealloc truep=%p truesize=%d\n",p,size);
126      memcpy(p, tombstone, TOMBSTONE_SIZE);
127      memcpy(p + size - TOMBSTONE_SIZE, tombstone, TOMBSTONE_SIZE);
128      *(size_t *)p = size;
129      p += TOMBSTONE_SIZE;
130#else
131      p = xosrealloc(p, size);
132#endif
133   }
134   if (p == NULL) outofmem(size);
135   return p;
136}
137
138extern void FAR *
139osstrdup(const char *str)
140{
141   char *p;
142   OSSIZE_T len;
143   len = strlen(str) + 1;
144   p = osmalloc(len);
145   memcpy(p, str, len);
146   return p;
147}
148
149/* osfree is usually just a macro in osalloc.h */
150#ifdef TOMBSTONES
151extern void
152osfree(void *p)
153{
154   int true_size;
155   if (!p) return;
156   p -= TOMBSTONE_SIZE;
157   true_size = *(size_t *)p;
158printf("osfree truep=%p truesize=%d\n",p,true_size);
159   if (memcmp(p + sizeof(size_t), tombstone + sizeof(size_t),
160              TOMBSTONE_SIZE - sizeof(size_t)) != 0) {
161      printf("start tombstone for block %p, size %d corrupted!",
162             p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2);     
163   }
164   if (memcmp(p + true_size - TOMBSTONE_SIZE, tombstone,
165              TOMBSTONE_SIZE) != 0) {
166      printf("end tombstone for block %p, size %d corrupted!",
167             p + TOMBSTONE_SIZE, true_size - TOMBSTONE_SIZE * 2);     
168   }
169   free(p);
170}
171#endif
172
173#ifdef HAVE_SIGNAL
174
175static int sigReceived;
176
177/* for systems not using autoconf, assume the signal handler returns void
178 * unless specified elsewhere */
179#ifndef RETSIGTYPE
180# define RETSIGTYPE void
181#endif
182
183static CDECL RETSIGTYPE FAR report_sig( int sig ) {
184   sigReceived = sig;
185   longjmp(jmpbufSignal, 1);
186}
187
188static void
189init_signals(void)
190{
191   int en;
192   if (!setjmp(jmpbufSignal)) {
193#if 0 /* FIXME: disable for now so we get a core dump */
194      signal(SIGABRT, report_sig); /* abnormal termination eg abort() */
195      signal(SIGFPE,  report_sig); /* arithmetic error eg /0 or overflow */
196      signal(SIGILL,  report_sig); /* illegal function image eg illegal instruction */
197      signal(SIGSEGV, report_sig); /* illegal storage access eg access outside memory limits */
198#endif
199      signal(SIGINT,  report_sig); /* interactive attention eg interrupt */
200      signal(SIGTERM, report_sig); /* termination request sent to program */
201# ifdef SIGSTAK /* only on RISC OS AFAIK */
202      signal(SIGSTAK, report_sig); /* stack overflow */
203# endif
204      return;
205   }
206
207   switch (sigReceived) {
208   case SIGABRT: en=90; break;
209   case SIGFPE:  en=91; break;
210   case SIGILL:  en=92; break;
211   case SIGINT:  en=93; break;
212   case SIGSEGV: en=94; break;
213   case SIGTERM: en=95; break;
214# ifdef SIGSTAK
215   case SIGSTAK: en=96; break;
216# endif
217   default:      en=97; break;
218   }
219   fputsnl(msg(en), STDERR);
220   if (errno >= 0) {
221# ifdef HAVE_STRERROR
222      fputsnl(strerror(errno), STDERR);
223# elif defined(HAVE_SYS_ERRLIST)
224      if (errno < sys_nerr) fputsnl(STDERR, sys_errlist[errno]);
225# elif defined(HAVE_PERROR)
226      perror(NULL); /* always goes to stderr */
227      /* if (arg!=NULL && *arg!='\0') fputs("<arg>: <err>\n",stderr); */
228      /* else fputs("<err>\n",stderr); */
229# else
230      fprintf(STDERR, "error code %d\n", errno);
231# endif
232   }
233   /* Any signals apart from SIGINT and SIGTERM suggest a bug */
234   if (sigReceived != SIGINT && sigReceived != SIGTERM)
235      fatalerror(/*Bug in program detected! Please report this to the authors*/11);
236
237   exit(EXIT_FAILURE);
238}
239#endif
240
241#define CHARSET_BAD       -1
242#define CHARSET_USASCII    0
243#define CHARSET_ISO_8859_1 1
244#define CHARSET_DOSCP850   2
245#define CHARSET_RISCOS31   3
246static int default_charset( void ) {
247#ifdef ISO8859_1
248   return CHARSET_ISO_8859_1;
249#elif (OS==RISCOS)
250/* RISCOS 3.1 and above CHARSET_RISCOS31 (ISO_8859_1 + extras in 128-159)
251 * RISCOS < 3.1 is ISO_8859_1 !HACK! */
252   return CHARSET_RISCOS31;
253#elif (OS==MSDOS)
254   return CHARSET_DOSCP850;
255#else
256   return CHARSET_ISO_8859_1; /* Look at env var CHARSET ? !HACK! */
257#endif
258}
259
260#if (OS==MSDOS)
261static int
262xlate_dos_cp850(int unicode)
263{
264   switch (unicode) {
265#include "uni2dos.h"
266   }
267   return 0;
268}
269#endif
270
271static int
272add_unicode(int charset, unsigned char *p, int value)
273{
274#ifdef DEBUG
275   fprintf(stderr, "add_unicode(%d, %p, %d)\n", charset, p, value);
276#endif
277   if (value == 0) return 0;
278   switch (charset) {
279   case CHARSET_USASCII:
280      if (value < 128) {
281         *p = value;
282         return 1;
283      }
284      break;
285   case CHARSET_ISO_8859_1:
286#if (OS==RISCOS)
287   case CHARSET_RISCOS31: /* RISC OS 3.1 has a few extras in 128-159 */
288#endif
289      if (value < 256) {
290         *p = value;
291         return 1;
292      }
293#if (OS==RISCOS)
294      /* FIXME: if OS version >= 3.1 handle extras here */
295      /* RISC OS 3.1 (and later) extensions to ISO-8859-1:
296       * \^y = \x86
297       * \^Y = \x85
298       * \^w = \x82
299       * \^W = \x81
300       * \oe = \x9b
301       * \OE = \x9a
302       */
303#endif
304      break;
305#if (OS==MSDOS)
306   case CHARSET_DOSCP850:
307      value = xlate_dos_cp850(value);
308      if (value) {
309         *p = value;
310         return 1;
311      }
312      break;
313#endif
314   }
315   return 0;
316}
317
318/* fall back on looking in the current directory */
319static const char *pth_cfg_files = "";
320
321static int num_msgs = 0;
322static char **msg_array = NULL;
323
324static void
325parse_msg_file(int charset_code)
326{
327   FILE *fh;
328   unsigned char header[20];
329   /*const*/ char *lang;
330   int i;
331   unsigned len;
332   unsigned char *p;
333   
334#ifdef DEBUG
335   fprintf(stderr, "parse_msg_file(%d)\n", charset_code);
336#endif
337
338   lang = getenv("SURVEXLANG");
339#ifdef DEBUG
340   fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)");
341#endif
342   
343   if (!lang || !*lang) {
344      lang = getenv("LANG");
345      if (!lang || !*lang) lang = DEFAULTLANG;
346   }
347#ifdef DEBUG
348   fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)");
349#endif
350
351#if 1
352   /* backward compatibility - FIXME deprecate? */
353   if (strcasecmp(lang, "engi") == 0) {
354      lang = "en";
355   } else if (strcasecmp(lang, "engu") == 0) {
356      lang = "en-us";
357   } else if (strcasecmp(lang, "fren") == 0) {
358      lang = "fr";
359   } else if (strcasecmp(lang, "germ") == 0) {
360      lang = "de";
361   } else if (strcasecmp(lang, "ital") == 0) {
362      lang = "it";
363   } else if (strcasecmp(lang, "span") == 0) {
364      lang = "es";
365   } else if (strcasecmp(lang, "cata") == 0) {
366      lang = "ca";
367   } else if (strcasecmp(lang, "port") == 0) {
368      lang = "pt";
369   }
370#endif
371#ifdef DEBUG
372   fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)");
373#endif
374
375   lang = strdup(lang);
376   /* On my RedHat 6.1 Linux box, LANG defaults to en_US - be nice and
377    * handle this... */
378   if (strchr(lang, '_')) {
379      char *under = strchr(lang, '_');
380      *under = '-';
381   }
382
383   fh = fopenWithPthAndExt(pth_cfg_files, lang, EXT_SVX_MSG, "rb", NULL);
384
385   if (!fh) {
386      /* e.g. if 'en-COCKNEY' is unknown, see if we know 'en' */
387      if (strlen(lang) > 3 && lang[2] == '-') {
388         char lang_generic[3];
389         lang_generic[0] = lang[0];
390         lang_generic[1] = lang[1];
391         lang_generic[2] = '\0';
392         fh = fopenWithPthAndExt(pth_cfg_files, lang_generic, EXT_SVX_MSG,
393                                 "rb", NULL);
394      }
395   }
396
397   if (!fh) {
398      /* no point extracting this error, as it won't get used if file opens */
399      fprintf(STDERR, "Can't open message file '%s' using path '%s'\n",
400              lang, pth_cfg_files);
401      exit(EXIT_FAILURE);
402   }
403
404   if (fread(header, 1, 20, fh) < 20 ||
405       memcmp(header, "Svx\nMsg\r\n\xfe\xff", 12) != 0) {
406      /* no point extracting this error, as it won't get used if file opens */
407      fprintf(STDERR, "Problem with message file '%s'\n", lang);
408      exit(EXIT_FAILURE);
409   }
410
411   if (header[12] != 0) {
412      /* no point extracting this error, as it won't get used if file opens */
413      fprintf(STDERR, "I don't understand this message file version\n");
414      exit(EXIT_FAILURE);
415   }
416
417   num_msgs = (header[14] << 8) | header[15];
418
419   len = 0;
420   for (i = 16; i < 20; i++) len = (len << 8) | header[i];
421
422   p = osmalloc(len);
423   if (fread(p, 1, len, fh) < len) {
424      /* no point extracting this error - it won't get used once file's read */
425      fprintf(STDERR, "Message file truncated?\n");
426      exit(EXIT_FAILURE);
427   }
428   fclose(fh);
429
430#ifdef DEBUG
431   fprintf(stderr, "lang = '%s', num_msgs = %d, len = %d\n", lang, num_msgs, len);
432#endif
433
434   msg_array = osmalloc(sizeof(char *) * num_msgs);
435
436   for (i = 0; i < num_msgs; i++) {
437      unsigned char *to = p;
438      int ch;
439      msg_array[i] = (char *)p;
440      while ((ch = *p++) != 0) {
441         /* A byte in the range 0x80-0xbf or 0xf0-0xff isn't valid in
442          * this state, (0xf0-0xfd mean values > 0xffff) so treat as
443          * literal and try to resync so we cope better when fed
444          * non-utf-8 data.  Similarly we abandon a multibyte sequence
445          * if we hit an invalid character. */
446         if (ch >= 0xc0 && ch < 0xf0) {
447            int ch1 = *p;
448            if ((ch1 & 0xc0) != 0x80) goto resync;
449               
450            if (ch < 0xe0) {
451               /* 2 byte sequence */
452               ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f);
453               p++;
454            } else {
455               /* 3 byte sequence */
456               int ch2 = p[1];
457               if ((ch2 & 0xc0) != 0x80) goto resync;
458               ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6) | (ch2 & 0x3f);
459               p += 2;
460            }
461         }
462           
463         resync:
464           
465         if (ch < 127) {
466            *to++ = (char)ch;
467         } else {
468            /* FIXME this rather assumes a 2 byte UTF-8 code never
469             * transliterates to more than 2 characters */
470            to += add_unicode(charset_code, to, ch);
471         }
472      }
473      *to++ = '\0';
474   }
475}
476
477const char *
478msg_cfgpth(void)
479{
480   return pth_cfg_files;
481}
482
483void
484msg_init(const char *argv0)
485{
486   char *p;
487
488#ifdef HAVE_SIGNAL
489   init_signals();
490#endif
491   /* This code *should* be completely bomb-proof even if strcpy
492    * generates a signal
493    */
494   szAppNameCopy = argv0; /* FIXME... */
495   szAppNameCopy = osstrdup(argv0);
496
497   /* Look for env. var. "SURVEXHOME" or the like */
498   p = getenv("SURVEXHOME");
499   if (p && *p) {
500      pth_cfg_files = osstrdup(p);
501#if (OS==UNIX) && defined(DATADIR) && defined(PACKAGE)
502   } else {
503      /* under Unix, we compile in the configured path */
504      pth_cfg_files = DATADIR "/" PACKAGE;
505#else
506   } else if (argv0) {
507      /* else try the path on argv[0] */
508      pth_cfg_files = path_from_fnm(argv0);
509#endif
510   }
511
512   select_charset(default_charset());
513}
514
515/* message may be overwritten by next call (but not in current implementation) */
516extern const char *
517msg(int en)
518{
519   static const char *szBadEn = "???";
520
521   if (!msg_array) {
522      if (en != 1) return szBadEn;
523      /* this should be the only message which can be requested before
524       * the message file is opened and read... */
525      return "Out of memory (couldn't find %ul bytes).\n";
526   }
527
528   if (en < 0 || en >= num_msgs) return szBadEn;
529
530   return msg_array[en];
531}
532
533/* returns persistent copy of message */
534extern const char *
535msgPerm(int en)
536{
537   return msg(en);
538}
539
540void
541v_report(int severity, const char *fnm, int line, int en, va_list ap)
542{
543   if (fnm) {
544      fputs(fnm, STDERR);
545      if (line) fprintf(STDERR, ":%d", line);
546   } else {
547      fputs(szAppNameCopy, STDERR);
548   }   
549   fputs(": ", STDERR);
550
551   if (severity == 0) {
552      fputs(msg(/*warning*/4), STDERR);
553      fputs(": ", STDERR);
554   }
555
556   vfprintf(STDERR, msg(en), ap);
557   fputnl(STDERR);
558   
559   /* FIXME allow "warnings are errors" and/or "errors are fatal" */
560   switch (severity) {
561    case 0:
562      cWarnings++;
563      break;
564    case 1:
565      cErrors++;
566      if (cErrors == 50)
567         fatalerror_in_file(fnm, 0, /*Too many errors - giving up*/19);
568      break;
569    case 2:
570      exit(EXIT_FAILURE);
571   }
572}
573
574void
575warning(int en, ...)
576{
577   va_list ap;
578   va_start(ap, en);
579   v_report(0, NULL, 0, en, ap);
580   va_end(ap);
581}
582
583void
584error(int en, ...)
585{
586   va_list ap;
587   va_start(ap, en);
588   v_report(1, NULL, 0, en, ap);
589   va_end(ap);
590}
591
592void
593fatalerror(int en, ...)
594{
595   va_list ap;
596   va_start(ap, en);
597   v_report(2, NULL, 0, en, ap);
598   va_end(ap);
599}
600
601void
602warning_in_file(const char *fnm, int line, int en, ...)
603{
604   va_list ap;
605   va_start(ap, en);
606   v_report(0, fnm, line, en, ap);
607   va_end(ap);
608}
609
610void
611error_in_file(const char *fnm, int line, int en, ...)
612{
613   va_list ap;
614   va_start(ap, en);
615   v_report(1, fnm, line, en, ap);
616   va_end(ap);
617}
618
619void
620fatalerror_in_file(const char *fnm, int line, int en, ...)
621{
622   va_list ap;
623   va_start(ap, en);
624   v_report(2, fnm, line, en, ap);
625   va_end(ap);
626}
627
628/* Code to support switching character set at runtime (e.g. for a printer
629 * driver to support different character sets on screen and on the printer)
630 */
631typedef struct charset_li {
632   struct charset_li *next;
633   int code;
634   char **msg_array;
635} charset_li;
636
637static charset_li *charset_head = NULL;
638
639static int charset = CHARSET_BAD;
640
641int
642select_charset(int charset_code)
643{
644   int old_charset = charset;
645   charset_li *p;
646
647#ifdef DEBUG
648   fprintf(stderr, "select_charset(%d), old charset = %d\n", charset_code, charset);
649#endif
650   
651   charset = charset_code;
652
653   /* check if we've already parsed messages for new charset */
654   for (p = charset_head; p; p = p->next) {
655#ifdef DEBUG
656      printf("%p: code %d msg_array %p\n", p, p->code, p->msg_array);
657#endif
658      if (p->code == charset) {
659         msg_array = p->msg_array;
660         return old_charset;
661      }
662   }
663
664   /* nope, got to reparse message file */
665   parse_msg_file(charset_code);
666
667   /* add to list */
668   p = osnew(charset_li);
669   p->code = charset;
670   p->msg_array = msg_array;
671   p->next = charset_head;
672   charset_head = p;
673
674   return old_charset;
675}
Note: See TracBrowser for help on using the repository browser.