source: git/src/message.c @ 0040767b

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 0040767b was dbb4e19, checked in by Olly Betts <olly@…>, 26 years ago

more changes

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

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