source: git/src/message.c @ 02f8178

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 02f8178 was 25ab06b, checked in by Olly Betts <olly@…>, 24 years ago

Prefer balanced quotes (`...') to unbalanced ('...') in messages.

cavern: file reading errors now treated as fatal; unattached survey error
now fatal; if there are errors, don't produce output files; bug fix: buffer
overrun in showline(); removed PRINT_NAME_PTRS debug stuff.

survex: syntax errors in command line arguments now fatal.

Turned off aven building for now.

3dtodxf renamed to cad3d to reflect choice of output formats.

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

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