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
/*
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
#if FS_STDARG
33
#include <stdarg.h>
34
#else
35
#include <varargs.h>
36
#endif
37
#include "list.h"
38
#include "flags.h"
39
#include "main.h"
40
#include "suffix.h"
41
#include "utility.h"
42
 
43
 
44
/*
45
    ERROR VARIABLES
46
 
47
    The value exit_status gives the overall status of the program.  It
48
    can be EXIT_SUCCESS or EXIT_FAILURE.  The variable progname gives
49
    the name of the program, which is used in error reports.
50
*/
51
 
52
int exit_status = EXIT_SUCCESS ;
53
char *progname = PROGNAME_TCC ;
54
 
55
 
56
/*
57
    PRINT AN ERROR MESSAGE
58
 
59
    This routine prints an error message s (a printf-style string,
60
    which may be followed by any number of arguments) of severity e
61
    (see utility.h).
62
*/
63
 
64
void error
65
    PROTO_V ( ( int e, char *s, ... ) ) /* VARARGS */
66
{
67
    va_list args ;
68
    char *errtype = null ;
69
#if FS_STDARG
70
    va_start ( args, s ) ;
71
#else
72
    int e ;
73
    char *s ;
74
    va_start ( args ) ;
75
    e = va_arg ( args, int ) ;
76
    s = va_arg ( args, char * ) ;
77
#endif
78
    switch ( e ) {
79
	case FATAL : {
80
	    exit_status = EXIT_FAILURE ;
81
	    errtype = "Fatal" ;
82
	    break ;
83
	}
84
	case INTERNAL : {
85
	    exit_status = EXIT_FAILURE ;
86
	    errtype = "Internal" ;
87
	    break ;
88
	}
89
	case SERIOUS : {
90
	    exit_status = EXIT_FAILURE ;
91
	    errtype = "Error" ;
92
	    break ;
93
	}
94
	case OPTION : {
95
	    exit_status = EXIT_FAILURE ;
96
	    errtype = "Option interpreter" ;
97
	    break ;
98
	}
99
	case WARNING : {
100
	    if ( !warnings ) {
101
		va_end ( args ) ;
102
		return ;
103
	    }
104
	    errtype = "Warning" ;
105
	    break ;
106
	}
107
	case INFO : {
108
	    errtype = "Information" ;
109
	    break ;
110
	}
111
    }
112
    if ( checker ) progname = PROGNAME_TCHK ;
113
    IGNORE fprintf ( stderr, "%s: ", progname ) ;
114
    if ( errtype ) IGNORE fprintf ( stderr, "%s: ", errtype ) ;
115
    IGNORE vfprintf ( stderr, s, args ) ;
116
    IGNORE fprintf ( stderr, ".\n" ) ;
117
    va_end ( args ) ;
118
    if ( e == FATAL ) main_end () ;
119
    return ;
120
}
121
 
122
 
123
/*
124
    PRINT A COMMENT
125
 
126
    This routine prints the comment s (a printf-style string, which
127
    may be followed by any number of arguments) to the standard output.
128
*/
129
 
130
void comment
131
    PROTO_V ( ( int e, char *s, ... ) ) /* VARARGS */
132
{
133
    FILE *f ;
134
    va_list args ;
135
#if FS_STDARG
136
    va_start ( args, s ) ;
137
#else
138
    int e ;
139
    char *s ;
140
    va_start ( args ) ;
141
    e = va_arg ( args, int ) ;
142
    s = va_arg ( args, char * ) ;
143
#endif
144
    f = ( e ? stdout : stderr ) ;
145
    IGNORE fflush ( f ) ;
146
    IGNORE vfprintf ( f, s, args ) ;
147
    IGNORE fflush ( f ) ;
148
    va_end ( args ) ;
149
    return ;
150
}
151
 
152
 
153
/*
154
    ALLOCATE A BLOCK OF MEMORY
155
 
156
    This routine allocates a block of memory of size sz and returns
157
    the result.
158
*/
159
 
160
pointer xalloc
161
    PROTO_N ( ( sz ) )
162
    PROTO_T ( int sz )
163
{
164
    pointer p = ( pointer ) malloc ( ( size_t ) sz ) ;
165
    if ( p == null ) error ( FATAL, "Memory allocation error" ) ;
166
    return ( p ) ;
167
}
168
 
169
 
170
/*
171
    REALLOCATE A BLOCK OF MEMORY
172
 
173
    This routine reallocates the block of memory p to have size sz.
174
    xrealloc ( null, sz ) is equivalent to xalloc ( sz ).
175
 
176
*/
177
 
178
pointer xrealloc
179
    PROTO_N ( ( p, sz ) )
180
    PROTO_T ( pointer p X int sz )
181
{
182
    pointer q ;
183
    if ( p == null ) return ( xalloc ( sz ) ) ;
184
    q = ( pointer ) realloc ( p, ( size_t ) sz ) ;
185
    if ( q == null ) error ( FATAL, "Memory reallocation error" ) ;
186
    return ( q ) ;
187
}
188
 
189
 
190
/*
191
    ALLOCATE SPACE FOR A STRING
192
 
193
    This routine allocates n characters of memory for use in the string
194
    memory allocation routines.
195
*/
196
 
197
static char *string_alloc
198
    PROTO_N ( ( n ) )
199
    PROTO_T ( int n )
200
{
201
    char *r ;
202
    if ( n >= 1000 ) {
203
	/* Long strings are allocated space by alloc_nof */
204
	r = alloc_nof ( char, n ) ;
205
    } else {
206
	/* Short strings are allocated space from a buffer */
207
	static int no_free = 0 ;
208
	static char *free_chars = null ;
209
	if ( n >= no_free ) {
210
	    no_free = 4000 ;
211
	    free_chars = alloc_nof ( char, no_free ) ;
212
	}
213
	r = free_chars ;
214
	no_free -= n ;
215
	free_chars += n ;
216
    }
217
    return ( r ) ;
218
}
219
 
220
 
221
/*
222
    COPY A STRING
223
 
224
    This routine allocates space for a copy of the string s and copies
225
    the string into this space.  This copy is returned.
226
*/
227
 
228
char *string_copy
229
    PROTO_N ( ( s ) )
230
    PROTO_T ( char *s )
231
{
232
    int n = ( int ) strlen ( s ) ;
233
    char *r = string_alloc ( n + 1 ) ;
234
    IGNORE strcpy ( r, s ) ;
235
    return ( r ) ;
236
}
237
 
238
 
239
/*
240
    COPY TWO STRINGS
241
 
242
    This routine allocates space for a copy of the string s followed by
243
    a copy of the string t and concatenates the strings into this space.
244
    This copy is returned.
245
*/
246
 
247
char *string_concat
248
    PROTO_N ( ( s, t ) )
249
    PROTO_T ( char *s X char *t )
250
{
251
    int n = ( int ) strlen ( s ) ;
252
    int m = ( int ) strlen ( t ) ;
253
    char *r = string_alloc ( n + m + 1 ) ;
254
    IGNORE strcpy ( r, s ) ;
255
    IGNORE strcpy ( r + n, t ) ;
256
    return ( r ) ;
257
}
258
 
259
 
260
/*
261
    TEMPORARY WORK SPACE
262
 
263
    This variable gives a temporary work space of size buffer_size
264
    (see utility.h) which is used as a scratch work area.
265
*/
266
 
267
char *buffer ;