source: git/src/message.c @ 3e25a0d

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 3e25a0d was 55de792, checked in by Olly Betts <olly@…>, 26 years ago

fixed handling of messages with accents

git-svn-id: file:///home/survex-svn/survex/trunk@181 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#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) lang = DEFAULTLANG;
344#ifdef DEBUG
345   fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)");
346#endif
347
348#if 1
349   /* backward compatibility - FIXME deprecate? */
350   if (strcasecmp(lang, "engi") == 0) {
351      lang = "en";
352   } else if (strcasecmp(lang, "engu") == 0) {
353      lang = "en-us";
354   } else if (strcasecmp(lang, "fren") == 0) {
355      lang = "fr";
356   } else if (strcasecmp(lang, "germ") == 0) {
357      lang = "de";
358   } else if (strcasecmp(lang, "ital") == 0) {
359      lang = "it";
360   } else if (strcasecmp(lang, "span") == 0) {
361      lang = "es";
362   } else if (strcasecmp(lang, "cata") == 0) {
363      lang = "ca";
364   } else if (strcasecmp(lang, "port") == 0) {
365      lang = "pt";
366   }
367#endif
368#ifdef DEBUG
369   fprintf(stderr, "lang = %p (= \"%s\")\n", lang, lang?lang:"(null)");
370#endif
371
372   fh = fopenWithPthAndExt(pth_cfg_files, lang, EXT_SVX_MSG, "rb", NULL);
373
374   if (!fh) {
375      /* e.g. if 'en-COCKNEY' is unknown, see if we know 'en' */
376      if (strlen(lang) > 3 && lang[2] == '-') {
377         char lang_generic[3];
378         lang_generic[0] = lang[0];
379         lang_generic[1] = lang[1];
380         lang_generic[2] = '\0';
381         fh = fopenWithPthAndExt(pth_cfg_files, lang_generic, EXT_SVX_MSG,
382                                 "rb", NULL);
383      }
384   }
385
386   if (!fh) {
387      /* no point extracting this error, as it won't get used if file opens */
388      fprintf(STDERR, "Can't open message file '%s' using path '%s'\n",
389              lang, pth_cfg_files);
390      exit(EXIT_FAILURE);
391   }
392
393   if (fread(header, 1, 20, fh) < 20 ||
394       memcmp(header, "Svx\nMsg\r\n\xfe\xff", 12) != 0) {
395      /* no point extracting this error, as it won't get used if file opens */
396      fprintf(STDERR, "Problem with message file '%s'\n", lang);
397      exit(EXIT_FAILURE);
398   }
399
400   if (header[12] != 0) {
401      /* no point extracting this error, as it won't get used if file opens */
402      fprintf(STDERR, "I don't understand this message file version\n");
403      exit(EXIT_FAILURE);
404   }
405
406   num_msgs = (header[14] << 8) | header[15];
407
408   len = 0;
409   for (i = 16; i < 20; i++) len = (len << 8) | header[i];
410
411   p = osmalloc(len);
412   if (fread(p, 1, len, fh) < len) {
413      /* no point extracting this error - it won't get used once file's read */
414      fprintf(STDERR, "Message file truncated?\n");
415      exit(EXIT_FAILURE);
416   }
417   fclose(fh);
418
419#ifdef DEBUG
420   fprintf(stderr, "lang = '%s', num_msgs = %d, len = %d\n", lang, num_msgs, len);
421#endif
422
423   msg_array = osmalloc(sizeof(char *) * num_msgs);
424
425   for (i = 0; i < num_msgs; i++) {
426      unsigned char *to = p;
427      int ch;
428      msg_array[i] = (char *)p;
429      while ((ch = *p++) != 0) {
430         /* A byte in the range 0x80-0xbf or 0xf0-0xff isn't valid in
431          * this state, (0xf0-0xfd mean values > 0xffff) so treat as
432          * literal and try to resync so we cope better when fed
433          * non-utf-8 data.  Similarly we abandon a multibyte sequence
434          * if we hit an invalid character. */
435         if (ch >= 0xc0 && ch < 0xf0) {
436            int ch1 = *p;
437            if ((ch1 & 0xc0) != 0x80) goto resync;
438               
439            if (ch < 0xe0) {
440               /* 2 byte sequence */
441               ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f);
442               p++;
443            } else {
444               /* 3 byte sequence */
445               int ch2 = p[1];
446               if ((ch2 & 0xc0) != 0x80) goto resync;
447               ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6) | (ch2 & 0x3f);
448               p += 2;
449            }
450         }
451           
452         resync:
453           
454         if (ch < 127) {
455            *to++ = (char)ch;
456         } else {
457            /* FIXME this rather assumes a 2 byte UTF-8 code never
458             * transliterates to more than 2 characters */
459            to += add_unicode(charset_code, to, ch);
460         }
461      }
462      *to++ = '\0';
463   }
464}
465
466const char *
467msg_cfgpth(void)
468{
469   return pth_cfg_files;
470}
471
472void
473msg_init(const char *argv0)
474{
475   char *p;
476
477#ifdef HAVE_SIGNAL
478   init_signals();
479#endif
480   /* This code *should* be completely bomb-proof even if strcpy
481    * generates a signal
482    */
483   szAppNameCopy = argv0; /* FIXME... */
484   szAppNameCopy = osstrdup(argv0);
485
486   /* Look for env. var. "SURVEXHOME" or the like */
487   p = getenv("SURVEXHOME");
488   if (p && *p) {
489      pth_cfg_files = osstrdup(p);
490#if (OS==UNIX) && defined(SURVEXHOME)
491   } else {
492      /* under Unix, we compile in the configured path */
493      pth_cfg_files = SURVEXHOME;
494#else
495   } else if (argv0) {
496      /* else try the path on argv[0] */
497      pth_cfg_files = path_from_fnm(argv0);
498#endif
499   }
500
501   select_charset(default_charset());
502}
503
504/* message may be overwritten by next call (but not in current implementation) */
505extern const char *
506msg(int en)
507{
508   static const char *szBadEn = "???";
509
510   if (!msg_array) {
511      if (en != 1) return szBadEn;
512      /* this should be the only message which can be requested before
513       * the message file is opened and read... */
514      return "Out of memory (couldn't find %ul bytes).\n";
515   }
516
517   if (en < 0 || en >= num_msgs) return szBadEn;
518
519   return msg_array[en];
520}
521
522/* returns persistent copy of message */
523extern const char *
524msgPerm(int en)
525{
526   return msg(en);
527}
528
529void
530v_report(int severity, const char *fnm, int line, int en, va_list ap)
531{
532   if (fnm) {
533      fputs(fnm, STDERR);
534      if (line) fprintf(STDERR, ":%d", line);
535   } else {
536      fputs(szAppNameCopy, STDERR);
537   }   
538   fputs(": ", STDERR);
539
540   if (severity == 0) {
541      fputs(msg(/*warning*/4), STDERR);
542      fputs(": ", STDERR);
543   }
544
545   vfprintf(STDERR, msg(en), ap);
546   fputnl(STDERR);
547   
548   /* FIXME allow "warnings are errors" and/or "errors are fatal" */
549   switch (severity) {
550    case 0:
551      cWarnings++;
552      break;
553    case 1:
554      cErrors++;
555      if (cErrors == 50)
556         fatalerror_in_file(fnm, 0, /*Too many errors - giving up*/19);
557      break;
558    case 2:
559      exit(EXIT_FAILURE);
560   }
561}
562
563void
564warning(int en, ...)
565{
566   va_list ap;
567   va_start(ap, en);
568   v_report(0, NULL, 0, en, ap);
569   va_end(ap);
570}
571
572void
573error(int en, ...)
574{
575   va_list ap;
576   va_start(ap, en);
577   v_report(1, NULL, 0, en, ap);
578   va_end(ap);
579}
580
581void
582fatalerror(int en, ...)
583{
584   va_list ap;
585   va_start(ap, en);
586   v_report(2, NULL, 0, en, ap);
587   va_end(ap);
588}
589
590void
591warning_in_file(const char *fnm, int line, int en, ...)
592{
593   va_list ap;
594   va_start(ap, en);
595   v_report(0, fnm, line, en, ap);
596   va_end(ap);
597}
598
599void
600error_in_file(const char *fnm, int line, int en, ...)
601{
602   va_list ap;
603   va_start(ap, en);
604   v_report(1, fnm, line, en, ap);
605   va_end(ap);
606}
607
608void
609fatalerror_in_file(const char *fnm, int line, int en, ...)
610{
611   va_list ap;
612   va_start(ap, en);
613   v_report(2, fnm, line, en, ap);
614   va_end(ap);
615}
616
617/* Code to support switching character set at runtime (e.g. for a printer
618 * driver to support different character sets on screen and on the printer)
619 */
620typedef struct charset_li {
621   struct charset_li *next;
622   int code;
623   char **msg_array;
624} charset_li;
625
626static charset_li *charset_head = NULL;
627
628static int charset = CHARSET_BAD;
629
630int
631select_charset(int charset_code)
632{
633   int old_charset = charset;
634   charset_li *p;
635
636#ifdef DEBUG
637   fprintf(stderr, "select_charset(%d), old charset = %d\n", charset_code, charset);
638#endif
639   
640   charset = charset_code;
641
642   /* check if we've already parsed messages for new charset */
643   for (p = charset_head; p; p = p->next) {
644#ifdef DEBUG
645      printf("%p: code %d msg_array %p\n", p, p->code, p->msg_array);
646#endif
647      if (p->code == charset) {
648         msg_array = p->msg_array;
649         return old_charset;
650      }
651   }
652
653   /* nope, got to reparse message file */
654   parse_msg_file(charset_code);
655
656   /* add to list */
657   p = osnew(charset_li);
658   p->code = charset;
659   p->msg_array = msg_array;
660   p->next = charset_head;
661   charset_head = p;
662
663   return old_charset;
664}
Note: See TracBrowser for help on using the repository browser.