Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/tendra5/src/tools/disp/file.c – Rev 2

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | 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 "types.h"
33
#include "ascii.h"
34
#include "file.h"
35
#include "pretty.h"
36
#include "tree.h"
37
#include "utility.h"
38
 
39
 
40
/*
41
    MACRO DEFINITIONS
42
 
43
    BUFFSIZE gives the size of the input buffer.  HI_MASK and LO_MASK
44
    are bit masks used to extract bits 8-15 and 0-7 respectively from
45
    an integer.
46
*/
47
 
48
#define BUFFSIZE	( ( size_t ) 1024 )
49
#define HI_MASK		( ( unsigned ) 0xff00 )
50
#define LO_MASK		( ( unsigned ) 0x00ff )
51
 
52
 
53
/*
54
    INPUT AND OUTPUT FILES AND BUFFERS
55
 
56
    The input TDF capsule is read from tdf_file.  The pretty-printed
57
    output is placed into pp_file.  The input uses the buffer
58
    inbuffer.
59
*/
60
 
61
FILE *pp_file ;
62
static FILE *tdf_file ;
63
static char inbuffer [ BUFFSIZE ] ;
64
static int ib_ptr, ib_size ;
65
 
66
 
67
/*
68
    POSITION IN INPUT FILE
69
 
70
    The current position in the input file is recorded by means of a
71
    place.
72
*/
73
 
74
place here ;
75
 
76
 
77
/*
78
    INPUT AND OUTPUT VARIABLES
79
 
80
    printflag is used to switch the printing on or off.  The present
81
    column number in the output file is given by column.  The maximum
82
    value attained by column is recorded in maximum.  The last character
83
    output is held in lastc.  The flag read_error is used to indicate
84
    that an error has occurred in reading the input file.
85
*/
86
 
87
int printflag = 1 ;
88
int column ;
89
int maximum ;
90
int lastc = 0 ;
91
int read_error = 0 ;
92
int dump = 0 ;
93
 
94
 
95
/*
96
    OPEN FILES
97
 
98
    The file name1 is opened for input and name2 for output.  If name2
99
    is the null string, the standard output is used.
100
*/
101
 
102
void open_files
103
    PROTO_N ( ( name1, name2 ) )
104
    PROTO_T ( char *name1 X char *name2 )
105
{
106
    tdf_file = fopen ( name1, "rb" ) ;
107
    if ( tdf_file == null ) fatal_error ( "Can't open %s", name1 ) ;
108
    if ( name2 == null ) dflag = 0 ;
109
    if ( dflag ) {
110
	pp_file = fopen ( name2, "w" ) ;
111
	if ( pp_file == null ) fatal_error ( "Can't open %s", name2 ) ;
112
    } else {
113
	pp_file = stdout ;
114
    }
115
    here.byte = 0 ;
116
    here.bit = 0 ;
117
    ib_size = 0 ;
118
    return ;
119
}
120
 
121
 
122
/*
123
    READ THE NEXT BYTE FROM THE INPUT FILE
124
 
125
    This routine reads the next byte from the input file, putting it
126
    into the worksp field of here.
127
*/
128
 
129
static void next_byte
130
    PROTO_Z ()
131
{
132
    if ( read_error ) {
133
	here.worksp = 0xff ;
134
    } else {
135
	/* refill input buffer if required */
136
	if ( ib_size == 0 ) {
137
	    ib_ptr = 0 ;
138
	    ib_size = ( int ) fread ( inbuffer, 1, BUFFSIZE, tdf_file ) ;
139
	    if ( ib_size == 0 ) {
140
		/* if we have reached the end of the file... */
141
		read_error = 1 ;
142
		if ( !dump ) {
143
		    out ( "<reading error>" ) ;
144
		    input_error ( "Reading error" ) ;
145
		}
146
	    }
147
	}
148
	/* get the next character */
149
	if ( read_error ) {
150
	    here.worksp = 0xff ;
151
	} else {
152
	    char c = inbuffer [ ib_ptr++ ] ;
153
	    here.worksp = ( unsigned ) ( c & 0xff ) ;
154
	    ib_size-- ;
155
	}
156
    }
157
    return ;
158
}
159
 
160
 
161
/*
162
    FETCH THE NEXT n BITS FROM THE INPUT FILE
163
 
164
    This routine reads the next n bits from the input file and returns
165
    them as an integer.  n will rarely be as much as 8, so there is
166
    no problem with overflow.
167
*/
168
 
169
long fetch
170
    PROTO_N ( ( n ) )
171
    PROTO_T ( int n )
172
{
173
    int m ;
174
    unsigned a = 0 ;
175
    while ( n ) {
176
	/* read the next byte if necessary */
177
	if ( here.bit == 0 ) next_byte () ;
178
	/* m = number of bits we need from this byte */
179
	m = BYTESIZE - here.bit ;
180
	if ( n < m ) m = n ;
181
	/* extract m bytes from here.worksp */
182
	here.worksp <<= m ;
183
	a = ( a << m ) + ( ( here.worksp & HI_MASK ) >> BYTESIZE ) ;
184
	here.worksp &= LO_MASK ;
185
	n -= m ;
186
	here.bit += m ;
187
	if ( here.bit == BYTESIZE ) {
188
	    here.bit = 0 ;
189
	    here.byte++ ;
190
	}
191
    }
192
    return ( ( long ) a ) ;
193
}
194
 
195
 
196
/*
197
    ALIGN INPUT FILE TO AN 8 BIT BOUNDARY
198
 
199
    Bits are read from the input file until it is on an 8 bit boundary.
200
*/
201
 
202
void byte_align
203
    PROTO_Z ()
204
{
205
    while ( !( here.bit == 0 || here.bit == BYTESIZE ) ) {
206
	IGNORE fetch ( 1 ) ;
207
    }
208
    return ;
209
}
210
 
211
 
212
/*
213
    GO TO A GIVEN PLACE IN THE INPUT FILE
214
 
215
    The current position is set to the position indicated by the place p.
216
*/
217
 
218
void set_place
219
    PROTO_N ( ( p ) )
220
    PROTO_T ( place *p )
221
{
222
    int s ;
223
    here.byte = p->byte ;
224
    here.bit = 0 ;
225
    s = fseek ( tdf_file, here.byte, SEEK_SET ) ;
226
    if ( s ) fatal_error ( "Internal file seek error" ) ;
227
    ib_size = 0 ;
228
    IGNORE fetch ( p->bit ) ;
229
    return ;
230
}
231
 
232
 
233
/*
234
    SKIP THE NEXT n BITS IN THE INPUT FILE
235
 
236
    If n is small, the next n bits are read but discarded.  Otherwise
237
    set_place is used.
238
*/
239
 
240
void skip_bits
241
    PROTO_N ( ( n ) )
242
    PROTO_T ( long n )
243
{
244
    if ( read_error ) return ;
245
    if ( n <= 4 * BYTESIZE ) {
246
	IGNORE fetch ( ( int ) n ) ;
247
    } else {
248
	place p ;
249
	long b = posn ( here ) + n ;
250
	p.byte = b / BYTESIZE ;
251
	p.bit = ( int ) ( b % BYTESIZE ) ;
252
	set_place ( &p ) ;
253
    }
254
    return ;
255
}
256
 
257
 
258
/*
259
    ARRAY OF SPACES
260
 
261
    The indentation spaces used during pretty-printing are stored in
262
    an array.
263
*/
264
 
265
static char *spaces1 ;
266
 
267
 
268
/*
269
    INITIALIZE SPACES
270
 
271
    This routine initializes the array of indentation spaces, spaces1.
272
*/
273
 
274
void init_spaces
275
    PROTO_N ( ( d ) )
276
    PROTO_T ( int d )
277
{
278
    int i, j ;
279
    if ( helpflag ) {
280
	spaces1 = alloc_nof ( char, 5000 ) ;
281
	/* every dth character should be a '.' */
282
	for ( i = 0, j = 0 ; i < 5000 ; i++ ) {
283
	    if ( ++j == d ) {
284
		spaces1 [i] = '.' ;
285
		j = 0 ;
286
	    } else {
287
		spaces1 [i] = ' ' ;
288
	    }
289
	}
290
    }
291
    return ;
292
}
293
 
294
 
295
/*
296
    OUTPUT n SPACES
297
 
298
    This routine outputs n indentation spaces, using the array spaces1
299
    when appropriate.
300
*/
301
 
302
void spaces
303
    PROTO_N ( ( n ) )
304
    PROTO_T ( int n )
305
{
306
    if ( n < 0 ) n = 0 ;
307
    if ( printflag ) {
308
	if ( helpflag ) {
309
	    if ( n ) {
310
		 IGNORE fwrite ( spaces1, sizeof ( char ),
311
				   ( size_t ) n, pp_file ) ;
312
	    }
313
	} else {
314
	    int i ;
315
	    int q = ( n / TBSIZE ) ;
316
	    int r = ( n % TBSIZE ) ;
317
	    for ( i = 0 ; i < q ; i++ ) IGNORE fputc ( TAB, pp_file ) ;
318
	    for ( i = 0 ; i < r ; i++ ) IGNORE fputc ( SPACE, pp_file ) ;
319
	}
320
    }
321
    column = n ;
322
    lastc = ' ' ;
323
    return ;
324
}