1 /*
2  *  error.c:		Error handling
3  *
4  *  Written by:		Stefan Frank
5  *			Ullrich Hafner
6  *
7  *  Credits:	Modelled after variable argument routines from Jef
8  *		Poskanzer's pbmplus package.
9  *
10  *  This file is part of FIASCO (Fractal Image And Sequence COdec)
11  *  Copyright (C) 1994-2000 Ullrich Hafner
12  */
13 
14 /*
15  *  $Date: 2000/03/20 21:29:59 $
16  *  $Author: hafner $
17  *  $Revision: 4.3 $
18  *  $State: Exp $
19  */
20 
21 #define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
22 #define _BSD_SOURCE 1   /* Make sure strdup() is in string.h */
23 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
24 #define _ERROR_C
25 
26 #include "config.h"
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 
31 #if STDC_HEADERS
32 #	include <stdarg.h>
33 #	define VA_START(args, lastarg) va_start(args, lastarg)
34 #else  /* not STDC_HEADERS */
35 #	include <varargs.h>
36 #	define VA_START(args, lastarg) va_start(args)
37 #endif /* not STDC_HEADERS */
38 #include <string.h>
39 
40 #if HAVE_SETJMP_H
41 #	include <setjmp.h>
42 #endif /* HAVE_SETJMP_H */
43 
44 #include "fiasco.h"
45 #include "binerror.h"
46 
47 /*****************************************************************************
48 
49 			     global variables
50 
51 *****************************************************************************/
52 
53 int   error_line = 0;
54 const char *error_file = NULL;
55 
56 /*****************************************************************************
57 
58 			     local variables
59 
60 *****************************************************************************/
61 
62 static const char *executable = "(name not initialized)";
63 
64 /*****************************************************************************
65 
66 			       public code
67 
68 *****************************************************************************/
69 
70 void
init_error_handling(const char * name)71 init_error_handling (const char *name)
72 /*
73  *  Initialize filename of executable.
74  *
75  *  No return value.
76  */
77 {
78    if (name)
79       executable = strdup (name);
80 }
81 
82 void
_error(const char * format,...)83 _error (const char *format, ...)
84 /*
85  *  Print error message and exit.
86  *
87  *  No return value.
88  */
89 {
90    va_list	args;
91 
92    VA_START (args, format);
93 
94    fprintf (stderr, "%s: %s: line %d:\nError: ",
95 	    executable, error_file, error_line);
96    vfprintf (stderr, format, args);
97    fputc ('\n', stderr);
98    va_end(args);
99 
100    exit (1);
101 }
102 
103 void
_file_error(const char * filename)104 _file_error (const char *filename)
105 /*
106  *  Print file error message and exit.
107  *
108  *  No return value.
109  */
110 {
111    fprintf (stderr, "%s: %s: line %d:\nError: ",
112 	    executable, error_file, error_line);
113    perror (filename);
114 
115    exit (2);
116 }
117 
118 void
_warning(const char * format,...)119 _warning (const char *format, ...)
120 /*
121  *  Issue a warning and continue execution.
122  *
123  *  No return value.
124  */
125 {
126    va_list args;
127 
128    VA_START (args, format);
129 
130    fprintf (stderr, "%s: %s: line %d:\nWarning: ",
131 	    executable, error_file, error_line);
132    vfprintf (stderr, format, args);
133    fputc ('\n', stderr);
134 
135    va_end (args);
136 }
137