Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
7 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
7 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:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 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;
7 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;
7 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
#if FS_STDARG
63
#include <stdarg.h>
64
#else
65
#include <varargs.h>
66
#endif
67
#include "types.h"
68
#include "file.h"
69
#include "pretty.h"
70
#include "utility.h"
71
 
72
 
73
/*
74
    PROGRAM NAME
75
 
76
    The program name is output in all error reports.
77
*/
78
 
7 7u83 79
char *progname = "disp";
2 7u83 80
 
81
 
82
/*
83
    EXIT STATUS
84
 
85
    This flag is set to 1 whenever an error occurs.  It is the value
86
    with which the program finally exits.
87
*/
88
 
7 7u83 89
int exit_status = EXIT_SUCCESS;
2 7u83 90
 
91
 
92
/*
93
    RECOVERY FLAG
94
 
95
    This flag controls whether an attempt is made to recover from
96
    non-fatal errors.  Anything after an error is likely to be of very
97
    little use.
98
*/
99
 
7 7u83 100
int recover = 0;
2 7u83 101
 
102
 
103
/*
104
    REPORT A FATAL ERROR
105
 
106
    An error is reported and the program aborts immediately.
107
*/
108
 
7 7u83 109
void
110
fatal_error(char *s, ...) /* VARARGS */
2 7u83 111
{
7 7u83 112
    va_list args;
2 7u83 113
#if FS_STDARG
7 7u83 114
    va_start(args, s);
2 7u83 115
#else
7 7u83 116
    char *s;
117
    va_start(args);
118
    s = va_arg(args, char *);
2 7u83 119
#endif
7 7u83 120
    if (progname)IGNORE fprintf(stderr, "%s: ", progname);
121
    IGNORE fprintf(stderr, "Error: ");
122
    IGNORE vfprintf(stderr, s, args);
123
    IGNORE fprintf(stderr, ".\n");
124
    va_end(args);
125
    exit(EXIT_FAILURE);
2 7u83 126
}
127
 
128
 
129
/*
130
    REPORT AN INPUT ERROR
131
 
132
    An error is reported together with the position within the input
133
    file where it occured, and the program either attempts to recover
134
    (if the recover flag is true) or outputs what it has read so far
135
    and then exits (otherwise).
136
*/
137
 
7 7u83 138
void
139
input_error(char *s, ...) /* VARARGS */
2 7u83 140
{
7 7u83 141
    va_list args;
142
    long b = here.byte;
2 7u83 143
#if FS_STDARG
7 7u83 144
    va_start(args, s);
2 7u83 145
#else
7 7u83 146
    char *s;
147
    va_start(args);
148
    s = va_arg(args, char *);
2 7u83 149
#endif
7 7u83 150
    if (progname)IGNORE fprintf(stderr, "%s: ", progname);
151
    IGNORE fprintf(stderr, "Error: ");
152
    IGNORE vfprintf(stderr, s, args);
153
    IGNORE fprintf(stderr, ", byte %ld, bit %d.\n", b, here.bit);
154
    va_end(args);
155
    exit_status = EXIT_FAILURE;
156
    if (!recover) {
157
	pretty_tree();
158
	exit(EXIT_FAILURE);
2 7u83 159
    }
7 7u83 160
    return;
2 7u83 161
}
162
 
163
 
164
/*
165
    ALLOCATE A SECTION OF MEMORY
166
 
167
    This routine allocates n bytes of memory.
168
*/
169
 
7 7u83 170
pointer
171
xalloc(int n)
2 7u83 172
{
7 7u83 173
    pointer ptr = (pointer)malloc((size_t)n);
174
    if (ptr == null)fatal_error("Memory allocation error");
175
    return(ptr);
2 7u83 176
}
177
 
178
 
179
/*
180
    REALLOCATE A SECTION OF MEMORY
181
 
182
    This routine reallocates n bytes of memory.
183
*/
184
 
7 7u83 185
pointer
186
xrealloc(pointer p, int n)
2 7u83 187
{
7 7u83 188
    pointer ptr;
189
    if (p == null) return(xalloc(n));
190
    ptr = (pointer)realloc(p,(size_t)n);
191
    if (ptr == null)fatal_error("Memory allocation error");
192
    return(ptr);
2 7u83 193
}