source: git/src/message.c @ 970ac64

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 970ac64 was f2a6ce4, checked in by Olly Betts <olly@…>, 26 years ago

tidied up interface

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

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