source: git/src/message.c @ 1422c39

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 1422c39 was ea816ec, checked in by Olly Betts <olly@…>, 24 years ago

HACKING: Updated RISC OS build instructions.

Removed support for really old language names ("fren", "germ", "ital",
etc) - use "fr", "de", "it", etc instead.

(RISC OS version): support a few extra accents in messages.

NEWS, TODO: Updated.

configure.in: Removed 6 superfluous checks for functions.

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

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