Subversion Repositories tendra.SVN

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "error.h"
33
#include "release.h"
34
 
35
 
36
/*
37
    ALLOW BOTH STDARG AND VARARGS
38
 
39
    The flag FS_STDARG decides which of stdarg.h and varargs.h is to be
40
    used for dealing with functions with a variable number of arguments.
41
*/
42
 
43
#if FS_STDARG
44
#include <stdarg.h>
45
#else
46
#include <varargs.h>
47
#endif
48
 
49
 
50
/*
51
    RELEASE VERSION
52
 
53
    This macro gives the release version, if known.
54
*/
55
 
56
#ifndef RELEASE
57
#define RELEASE		"unknown"
58
#endif
59
 
60
 
61
/*
62
    ERROR FLAGS AND VARIABLES
63
 
64
    These variables are used or set in the error routines.
65
*/
66
 
67
CONST char *progname = NULL ;
68
CONST char *progvers = NULL ;
69
int exit_status = EXIT_SUCCESS ;
70
int maximum_errors = 20 ;
71
static int number_errors = 0 ;
72
 
73
int crt_line_no = 1 ;
74
CONST char *crt_file_name = NULL ;
75
 
76
 
77
/*
78
    SET PROGRAM NAME
79
 
80
    This routine sets the program name to nm and the program version to
81
    vers.
82
*/
83
 
84
void set_progname
85
    PROTO_N ( ( nm, vers ) )
86
    PROTO_T ( CONST char *nm X CONST char *vers )
87
{
88
    char *r = strrchr ( nm, '/' ) ;
89
    progname = ( r ? r + 1 : nm ) ;
90
    progvers = vers ;
91
    return ;
92
}
93
 
94
 
95
/*
96
    PRINT VERSION NUMBER
97
 
98
    This routine prints the program name and version number.
99
*/
100
 
101
void report_version
102
    PROTO_Z ()
103
{
104
    CONST char *r = RELEASE ;
105
    CONST char *nm = progname ;
106
    CONST char *vers = progvers ;
107
    if ( nm == NULL ) nm = "unknown" ;
108
    if ( vers == NULL ) vers = "1.0" ;
109
    fprintf_v ( stderr, "%s: Version %s (Release %s)\n", nm, vers, r ) ;
110
    return ;
111
}
112
 
113
 
114
/*
115
    PRINT AN ERROR MESSAGE
116
 
117
    This routine prints an error message s with arguments args and severity
118
    e.  fn and ln give the error position.
119
*/
120
 
121
static void error_msg
122
    PROTO_N ( ( e, fn, ln, s, args ) )
123
    PROTO_T ( int e X CONST char *fn X int ln X CONST char *s X va_list args )
124
{
125
    if ( e != ERROR_NONE ) {
126
	if ( progname ) fprintf_v ( stderr, "%s: ", progname ) ;
127
	switch ( e ) {
128
	    case ERROR_WARNING : {
129
		fprintf_v ( stderr, "Warning: " ) ;
130
		break ;
131
	    }
132
	    case ERROR_FATAL : {
133
		fprintf_v ( stderr, "Fatal: " ) ;
134
		exit_status = EXIT_FAILURE ;
135
		number_errors++ ;
136
		break ;
137
	    }
138
	    default : {
139
		fprintf_v ( stderr, "Error: " ) ;
140
		exit_status = EXIT_FAILURE ;
141
		number_errors++ ;
142
		break ;
143
	    }
144
	}
145
	if ( fn ) {
146
	    fprintf_v ( stderr, "%s: ", fn ) ;
147
	    if ( ln != -1 ) fprintf_v ( stderr, "line %d: ", ln ) ;
148
	}
149
	vfprintf_v ( stderr, s, args ) ;
150
	fprintf_v ( stderr, ".\n" ) ;
151
	if ( e == ERROR_FATAL ) exit ( EXIT_FAILURE ) ;
152
	if ( number_errors >= maximum_errors && maximum_errors ) {
153
	    error ( ERROR_FATAL, "Too many errors (%d) - aborting",
154
		    number_errors ) ;
155
	}
156
    }
157
    return ;
158
}
159
 
160
 
161
/*
162
    PRINT AN ERROR AT CURRENT POSITION
163
 
164
    This routine prints the error message s of severity e at the current
165
    file position.  s is a printf format string whose arguments are passed
166
    as the optional procedure parameters.
167
*/
168
 
169
void error
170
    PROTO_V ( ( int e, CONST char *s, ... ) )
171
    /*VARARGS*/
172
{
173
    va_list args ;
174
#if FS_STDARG
175
    va_start ( args, s ) ;
176
#else
177
    int e ;
178
    CONST char *s ;
179
    va_start ( args ) ;
180
    e = va_arg ( args, int ) ;
181
    s = va_arg ( args, CONST char * ) ;
182
#endif
183
    error_msg ( e, crt_file_name, crt_line_no, s, args ) ;
184
    va_end ( args ) ;
185
    return ;
186
}
187
 
188
 
189
/*
190
    PRINT AN ERROR AT A GIVEN POSITION
191
 
192
    This routine prints the error message s of severity e at the file
193
    position given by fn and ln.  s is as above.
194
*/
195
 
196
void error_posn
197
    PROTO_V ( ( int e, CONST char *fn, int ln, CONST char *s, ... ) )
198
    /*VARARGS*/
199
{
200
    va_list args ;
201
#if FS_STDARG
202
    va_start ( args, s ) ;
203
#else
204
    int e ;
205
    CONST char *fn ;
206
    int ln ;
207
    CONST char *s ;
208
    va_start ( args ) ;
209
    e = va_arg ( args, int ) ;
210
    fn = va_arg ( args, CONST char * ) ;
211
    ln = va_arg ( args, int ) ;
212
    s = va_arg ( args, CONST char * ) ;
213
#endif
214
    error_msg ( e, fn, ln, s, args ) ;
215
    va_end ( args ) ;
216
    return ;
217
}
218
 
219
 
220
#ifdef DEBUG
221
 
222
/*
223
    PRINT AN ASSERTION
224
 
225
    This routine prints the assertion s which occurred at the location
226
    given by file and line.
227
*/
228
 
229
void assertion
230
    PROTO_N ( ( s, file, line ) )
231
    PROTO_T ( CONST char *s X CONST char *file X int line )
232
{
233
    if ( progname ) fprintf_v ( stderr, "%s: ", progname ) ;
234
    fprintf_v ( stderr, "Assertion: %s: line %d: '%s'.\n", file, line, s ) ;
235
    abort () ;
236
}
237
 
238
#endif