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
Line 
1/* > message.c
2 * Fairly general purpose message and error routines
3 * Copyright (C) 1993-1998 Olly Betts
4 */
5
6#ifdef HAVE_CONFIG_H
7# include <config.h>
8#endif
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"
18#include "filename.h"
19#include "message.h"
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
35 * add (for eg) -DDEFAULTLANG="fr" to UFLG in the makefile
36 * FIXME - update wrt automake/autoconf
37 */
38#ifndef DEFAULTLANG
39# define DEFAULTLANG "en"
40#endif
41
42/* For funcs which want to be immune from messing around with different
43 * calling conventions */
44#ifndef CDECL
45# define CDECL
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) {
52   fprintf(STDERR, msg(/*There were %d warning(s) and %d non-fatal error(s).*/16),
53           cWarnings, cErrors);
54   fputnl(STDERR);
55   return (cErrors ? EXIT_FAILURE : EXIT_SUCCESS);
56}
57
58/* in case osmalloc() fails before szAppNameCopy is set up */
59const char *szAppNameCopy = "anonymous program";
60
61/* error code for failed osmalloc and osrealloc calls */
62static void
63outofmem(OSSIZE_T size)
64{
65   fatalerror(1/*Out of memory (couldn't find %lu bytes).*/, (unsigned long)size);
66}
67
68#ifdef TOMBSTONES
69#define TOMBSTONE_SIZE 16
70static char tombstone[TOMBSTONE_SIZE] = "012345\xfftombstone";
71#endif
72
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 */
76extern void FAR *
77osmalloc(OSSIZE_T size)
78{
79   void FAR *p;
80#ifdef TOMBSTONES
81   size += TOMBSTONE_SIZE * 2;
82   p = malloc(size);
83#else
84   p = xosmalloc(size);
85#endif
86   if (p == NULL) outofmem(size);
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
94   return p;
95}
96
97/* realloc with error catching if it fails. */
98extern void FAR *
99osrealloc(void *p, OSSIZE_T size)
100{
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   }
132   if (p == NULL) outofmem(size);
133   return p;
134}
135
136extern void FAR *
137osstrdup(const char *str)
138{
139   char *p;
140   OSSIZE_T len;
141   len = strlen(str) + 1;
142   p = osmalloc(len);
143   memcpy(p, str, len);
144   return p;
145}
146
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
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
178# define RETSIGTYPE void
179#endif
180
181static CDECL RETSIGTYPE FAR report_sig( int sig ) {
182   sigReceived = sig;
183   longjmp(jmpbufSignal, 1);
184}
185
186static void
187init_signals(void)
188{
189   int en;
190   if (!setjmp(jmpbufSignal)) {
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 */
199# ifdef SIGSTAK /* only on RISC OS AFAIK */
200      signal(SIGSTAK, report_sig); /* stack overflow */
201# endif
202      return;
203   }
204
205   switch (sigReceived) {
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;
212# ifdef SIGSTAK
213   case SIGSTAK: en=96; break;
214# endif
215   default:      en=97; break;
216   }
217   fputsnl(msg(en), STDERR);
218   if (errno >= 0) {
219# ifdef HAVE_STRERROR
220      fputsnl(strerror(errno), STDERR);
221# elif defined(HAVE_SYS_ERRLIST)
222      if (errno < sys_nerr) fputsnl(STDERR, sys_errlist[errno]);
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
228      fprintf(STDERR, "error code %d\n", errno);
229# endif
230   }
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
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)
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;
251#elif (OS==MSDOS)
252   return CHARSET_DOSCP850;
253#else
254   return CHARSET_ISO_8859_1; /* Look at env var CHARSET ? !HACK! */
255#endif
256}
257
258#if (OS==MSDOS)
259static int
260xlate_dos_cp850(int unicode)
261{
262   switch (unicode) {
263#include "uni2dos.h"
264   }
265   return 0;
266}
267#endif
268
269static int
270add_unicode(int charset, char *p, int value)
271{
272   if (value == 0) return 0;
273   switch (charset) {
274   case CHARSET_USASCII:
275      if (value < 128) {
276         *p = value;
277         return 1;
278      }
279      break;
280   case CHARSET_ISO_8859_1:
281#if (OS==RISCOS)
282   case CHARSET_RISCOS31: /* RISC OS 3.1 has a few extras in 128-159 */
283#endif
284      if (value < 256) {
285         *p = value;
286         return 1;
287      }
288#if (OS==RISCOS)
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       */
298#endif
299      break;
300#if (OS==MSDOS)
301   case CHARSET_DOSCP850:
302      value = xlate_dos_cp850(value);
303      if (value) {
304         *p = value;
305         return 1;
306      }
307      break;
308#endif
309   }
310   return 0;
311}
312
313/* fall back on looking in the current directory */
314static const char *pth_cfg_files = "";
315
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;
325   int i;
326   unsigned len;
327   char *p = (char *)msg_blk;
328
329   lang = getenv("SURVEXLANG");
330   if (!lang || !*lang) lang = DEFAULTLANG;
331
332#if 1
333   /* backward compatibility - FIXME deprecate? */
334   if (strcasecmp(lang, "engi") == 0) {
335      lang = "en";
336   } else if (strcasecmp(lang, "engu") == 0) {
337      lang = "en-us";
338   } else if (strcasecmp(lang, "fren") == 0) {
339      lang = "fr";
340   } else if (strcasecmp(lang, "germ") == 0) {
341      lang = "de";
342   } else if (strcasecmp(lang, "ital") == 0) {
343      lang = "it";
344   } else if (strcasecmp(lang, "span") == 0) {
345      lang = "es";
346   } else if (strcasecmp(lang, "cata") == 0) {
347      lang = "ca";
348   } else if (strcasecmp(lang, "port") == 0) {
349      lang = "pt";
350   }
351#endif
352
353   fh = fopenWithPthAndExt(pth_cfg_files, lang, EXT_SVX_MSG, "rb", NULL);
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';
362         fh = fopenWithPthAndExt(pth_cfg_files, lang_generic, EXT_SVX_MSG,
363                                 "rb", NULL);
364      }
365   }
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",
370              lang, pth_cfg_files);
371      exit(EXIT_FAILURE);
372   }
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   }
398   fclose(fh);
399
400   p = (char *)msg_blk;
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   }
440}
441
442const char *
443msg_cfgpth(void)
444{
445   return pth_cfg_files;
446}
447
448void
449msg_init(const char *argv0)
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) {
465      pth_cfg_files = osstrdup(p);
466#if (OS==UNIX) && defined(SURVEXHOME)
467   } else {
468      /* under Unix, we compile in the configured path */
469      pth_cfg_files = SURVEXHOME;
470#else
471   } else if (argv0) {
472      /* else try the path on argv[0] */
473      pth_cfg_files = path_from_fnm(argv0);
474#endif
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) {
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";
493   }
494
495   if (en < 0 || en >= num_msgs) return szBadEn;
496
497   p = (char *)msg_blk;
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.