Subversion Repositories tendra.SVN

Rev

Rev 6 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
6 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
6 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
6 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
6 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "error.h"
63
#include "release.h"
64
 
65
 
66
/*
67
    ALLOW BOTH STDARG AND VARARGS
68
 
69
    The flag FS_STDARG decides which of stdarg.h and varargs.h is to be
70
    used for dealing with functions with a variable number of arguments.
71
*/
72
 
73
#if FS_STDARG
74
#include <stdarg.h>
75
#else
76
#include <varargs.h>
77
#endif
78
 
79
 
80
/*
81
    RELEASE VERSION
82
 
83
    This macro gives the release version, if known.
84
*/
85
 
86
#ifndef RELEASE
87
#define RELEASE		"unknown"
88
#endif
89
 
90
 
91
/*
92
    ERROR FLAGS AND VARIABLES
93
 
94
    These variables are used or set in the error routines.
95
*/
96
 
6 7u83 97
CONST char *progname = NULL;
98
CONST char *progvers = NULL;
99
int exit_status = EXIT_SUCCESS;
100
int maximum_errors = 20;
101
static int number_errors = 0;
2 7u83 102
 
6 7u83 103
int crt_line_no = 1;
104
CONST char *crt_file_name = NULL;
2 7u83 105
 
106
 
107
/*
108
    SET PROGRAM NAME
109
 
110
    This routine sets the program name to nm and the program version to
111
    vers.
112
*/
113
 
6 7u83 114
void
115
set_progname(CONST char *nm, CONST char *vers)
2 7u83 116
{
6 7u83 117
    char *r = strrchr(nm, '/');
118
    progname = (r ? r + 1 : nm);
119
    progvers = vers;
120
    return;
2 7u83 121
}
122
 
123
 
124
/*
125
    PRINT VERSION NUMBER
126
 
127
    This routine prints the program name and version number.
128
*/
129
 
6 7u83 130
void
131
report_version(void)
2 7u83 132
{
6 7u83 133
    CONST char *r = RELEASE;
134
    CONST char *nm = progname;
135
    CONST char *vers = progvers;
136
    if (nm == NULL) nm = "unknown";
137
    if (vers == NULL) vers = "1.0";
138
    fprintf_v(stderr, "%s: Version %s (Release %s)\n", nm, vers, r);
139
    return;
2 7u83 140
}
141
 
142
 
143
/*
144
    PRINT AN ERROR MESSAGE
145
 
146
    This routine prints an error message s with arguments args and severity
147
    e.  fn and ln give the error position.
148
*/
149
 
6 7u83 150
static void
151
error_msg(int e, CONST char *fn, int ln, CONST char *s, va_list args)
2 7u83 152
{
6 7u83 153
    if (e != ERROR_NONE) {
154
	if (progname) fprintf_v(stderr, "%s: ", progname);
155
	switch (e) {
156
	    case ERROR_WARNING: {
157
		fprintf_v(stderr, "Warning: ");
158
		break;
2 7u83 159
	    }
6 7u83 160
	    case ERROR_FATAL: {
161
		fprintf_v(stderr, "Fatal: ");
162
		exit_status = EXIT_FAILURE;
163
		number_errors++;
164
		break;
2 7u83 165
	    }
166
	    default : {
6 7u83 167
		fprintf_v(stderr, "Error: ");
168
		exit_status = EXIT_FAILURE;
169
		number_errors++;
170
		break;
2 7u83 171
	    }
172
	}
6 7u83 173
	if (fn) {
174
	    fprintf_v(stderr, "%s: ", fn);
175
	    if (ln != -1) fprintf_v(stderr, "line %d: ", ln);
2 7u83 176
	}
6 7u83 177
	vfprintf_v(stderr, s, args);
178
	fprintf_v(stderr, ".\n");
179
	if (e == ERROR_FATAL) exit(EXIT_FAILURE);
180
	if (number_errors >= maximum_errors && maximum_errors) {
181
	    error(ERROR_FATAL, "Too many errors (%d) - aborting",
182
		    number_errors);
2 7u83 183
	}
184
    }
6 7u83 185
    return;
2 7u83 186
}
187
 
188
 
189
/*
190
    PRINT AN ERROR AT CURRENT POSITION
191
 
192
    This routine prints the error message s of severity e at the current
193
    file position.  s is a printf format string whose arguments are passed
194
    as the optional procedure parameters.
195
*/
196
 
6 7u83 197
void
198
error(int e, CONST char *s, ...)
2 7u83 199
    /*VARARGS*/
200
{
6 7u83 201
    va_list args;
2 7u83 202
#if FS_STDARG
6 7u83 203
    va_start(args, s);
2 7u83 204
#else
6 7u83 205
    int e;
206
    CONST char *s;
207
    va_start(args);
208
    e = va_arg(args, int);
209
    s = va_arg(args, CONST char *);
2 7u83 210
#endif
6 7u83 211
    error_msg(e, crt_file_name, crt_line_no, s, args);
212
    va_end(args);
213
    return;
2 7u83 214
}
215
 
216
 
217
/*
218
    PRINT AN ERROR AT A GIVEN POSITION
219
 
220
    This routine prints the error message s of severity e at the file
221
    position given by fn and ln.  s is as above.
222
*/
223
 
6 7u83 224
void
225
error_posn(int e, CONST char *fn, int ln, CONST char *s, ...)
2 7u83 226
    /*VARARGS*/
227
{
6 7u83 228
    va_list args;
2 7u83 229
#if FS_STDARG
6 7u83 230
    va_start(args, s);
2 7u83 231
#else
6 7u83 232
    int e;
233
    CONST char *fn;
234
    int ln;
235
    CONST char *s;
236
    va_start(args);
237
    e = va_arg(args, int);
238
    fn = va_arg(args, CONST char *);
239
    ln = va_arg(args, int);
240
    s = va_arg(args, CONST char *);
2 7u83 241
#endif
6 7u83 242
    error_msg(e, fn, ln, s, args);
243
    va_end(args);
244
    return;
2 7u83 245
}
246
 
247
 
248
#ifdef DEBUG
249
 
250
/*
251
    PRINT AN ASSERTION
252
 
253
    This routine prints the assertion s which occurred at the location
254
    given by file and line.
255
*/
256
 
6 7u83 257
void
258
assertion(CONST char *s, CONST char *file, int line)
2 7u83 259
{
6 7u83 260
    if (progname) fprintf_v(stderr, "%s: ", progname);
261
    fprintf_v(stderr, "Assertion: %s: line %d: '%s'.\n", file, line, s);
262
    abort();
2 7u83 263
}
264
 
265
#endif