Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 43... Line 73...
43
    BUFFSIZE gives the size of the input buffer.  HI_MASK and LO_MASK
73
    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
74
    are bit masks used to extract bits 8-15 and 0-7 respectively from
45
    an integer.
75
    an integer.
46
*/
76
*/
47
 
77
 
48
#define BUFFSIZE	( ( size_t ) 1024 )
78
#define BUFFSIZE	((size_t)1024)
49
#define HI_MASK		( ( unsigned ) 0xff00 )
79
#define HI_MASK		((unsigned)0xff00)
50
#define LO_MASK		( ( unsigned ) 0x00ff )
80
#define LO_MASK		((unsigned)0x00ff)
51
 
81
 
52
 
82
 
53
/*
83
/*
54
    INPUT AND OUTPUT FILES AND BUFFERS
84
    INPUT AND OUTPUT FILES AND BUFFERS
55
 
85
 
56
    The input TDF capsule is read from tdf_file.  The pretty-printed
86
    The input TDF capsule is read from tdf_file.  The pretty-printed
57
    output is placed into pp_file.  The input uses the buffer
87
    output is placed into pp_file.  The input uses the buffer
58
    inbuffer.
88
    inbuffer.
59
*/
89
*/
60
 
90
 
61
FILE *pp_file ;
91
FILE *pp_file;
62
static FILE *tdf_file ;
92
static FILE *tdf_file;
63
static char inbuffer [ BUFFSIZE ] ;
93
static char inbuffer[BUFFSIZE];
64
static int ib_ptr, ib_size ;
94
static int ib_ptr, ib_size;
65
 
95
 
66
 
96
 
67
/*
97
/*
68
    POSITION IN INPUT FILE
98
    POSITION IN INPUT FILE
69
 
99
 
70
    The current position in the input file is recorded by means of a
100
    The current position in the input file is recorded by means of a
71
    place.
101
    place.
72
*/
102
*/
73
 
103
 
74
place here ;
104
place here;
75
 
105
 
76
 
106
 
77
/*
107
/*
78
    INPUT AND OUTPUT VARIABLES
108
    INPUT AND OUTPUT VARIABLES
79
 
109
 
Line 82... Line 112...
82
    value attained by column is recorded in maximum.  The last character
112
    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
113
    output is held in lastc.  The flag read_error is used to indicate
84
    that an error has occurred in reading the input file.
114
    that an error has occurred in reading the input file.
85
*/
115
*/
86
 
116
 
87
int printflag = 1 ;
117
int printflag = 1;
88
int column ;
118
int column;
89
int maximum ;
119
int maximum;
90
int lastc = 0 ;
120
int lastc = 0;
91
int read_error = 0 ;
121
int read_error = 0;
92
int dump = 0 ;
122
int dump = 0;
93
 
123
 
94
 
124
 
95
/*
125
/*
96
    OPEN FILES
126
    OPEN FILES
97
 
127
 
98
    The file name1 is opened for input and name2 for output.  If name2
128
    The file name1 is opened for input and name2 for output.  If name2
99
    is the null string, the standard output is used.
129
    is the null string, the standard output is used.
100
*/
130
*/
101
 
131
 
102
void open_files
132
void
103
    PROTO_N ( ( name1, name2 ) )
-
 
104
    PROTO_T ( char *name1 X char *name2 )
133
open_files(char *name1, char *name2)
105
{
134
{
106
    tdf_file = fopen ( name1, "rb" ) ;
135
    tdf_file = fopen(name1, "rb");
107
    if ( tdf_file == null ) fatal_error ( "Can't open %s", name1 ) ;
136
    if (tdf_file == null)fatal_error("Can't open %s", name1);
108
    if ( name2 == null ) dflag = 0 ;
137
    if (name2 == null)dflag = 0;
109
    if ( dflag ) {
138
    if (dflag) {
110
	pp_file = fopen ( name2, "w" ) ;
139
	pp_file = fopen(name2, "w");
111
	if ( pp_file == null ) fatal_error ( "Can't open %s", name2 ) ;
140
	if (pp_file == null)fatal_error("Can't open %s", name2);
112
    } else {
141
    } else {
113
	pp_file = stdout ;
142
	pp_file = stdout;
114
    }
143
    }
115
    here.byte = 0 ;
144
    here.byte = 0;
116
    here.bit = 0 ;
145
    here.bit = 0;
117
    ib_size = 0 ;
146
    ib_size = 0;
118
    return ;
147
    return;
119
}
148
}
120
 
149
 
121
 
150
 
122
/*
151
/*
123
    READ THE NEXT BYTE FROM THE INPUT FILE
152
    READ THE NEXT BYTE FROM THE INPUT FILE
124
 
153
 
125
    This routine reads the next byte from the input file, putting it
154
    This routine reads the next byte from the input file, putting it
126
    into the worksp field of here.
155
    into the worksp field of here.
127
*/
156
*/
128
 
157
 
129
static void next_byte
158
static void
130
    PROTO_Z ()
159
next_byte(void)
131
{
160
{
132
    if ( read_error ) {
161
    if (read_error) {
133
	here.worksp = 0xff ;
162
	here.worksp = 0xff;
134
    } else {
163
    } else {
135
	/* refill input buffer if required */
164
	/* refill input buffer if required */
136
	if ( ib_size == 0 ) {
165
	if (ib_size == 0) {
137
	    ib_ptr = 0 ;
166
	    ib_ptr = 0;
138
	    ib_size = ( int ) fread ( inbuffer, 1, BUFFSIZE, tdf_file ) ;
167
	    ib_size = (int)fread(inbuffer, 1, BUFFSIZE, tdf_file);
139
	    if ( ib_size == 0 ) {
168
	    if (ib_size == 0) {
140
		/* if we have reached the end of the file... */
169
		/* if we have reached the end of the file... */
141
		read_error = 1 ;
170
		read_error = 1;
142
		if ( !dump ) {
171
		if (!dump) {
143
		    out ( "<reading error>" ) ;
172
		    out("<reading error>");
144
		    input_error ( "Reading error" ) ;
173
		    input_error("Reading error");
145
		}
174
		}
146
	    }
175
	    }
147
	}
176
	}
148
	/* get the next character */
177
	/* get the next character */
149
	if ( read_error ) {
178
	if (read_error) {
150
	    here.worksp = 0xff ;
179
	    here.worksp = 0xff;
151
	} else {
180
	} else {
152
	    char c = inbuffer [ ib_ptr++ ] ;
181
	    char c = inbuffer[ib_ptr++];
153
	    here.worksp = ( unsigned ) ( c & 0xff ) ;
182
	    here.worksp = (unsigned)(c & 0xff);
154
	    ib_size-- ;
183
	    ib_size--;
155
	}
184
	}
156
    }
185
    }
157
    return ;
186
    return;
158
}
187
}
159
 
188
 
160
 
189
 
161
/*
190
/*
162
    FETCH THE NEXT n BITS FROM THE INPUT FILE
191
    FETCH THE NEXT n BITS FROM THE INPUT FILE
Line 164... Line 193...
164
    This routine reads the next n bits from the input file and returns
193
    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
194
    them as an integer.  n will rarely be as much as 8, so there is
166
    no problem with overflow.
195
    no problem with overflow.
167
*/
196
*/
168
 
197
 
169
long fetch
198
long
170
    PROTO_N ( ( n ) )
-
 
171
    PROTO_T ( int n )
199
fetch(int n)
172
{
200
{
173
    int m ;
201
    int m;
174
    unsigned a = 0 ;
202
    unsigned a = 0;
175
    while ( n ) {
203
    while (n) {
176
	/* read the next byte if necessary */
204
	/* read the next byte if necessary */
177
	if ( here.bit == 0 ) next_byte () ;
205
	if (here.bit == 0)next_byte();
178
	/* m = number of bits we need from this byte */
206
	/* m = number of bits we need from this byte */
179
	m = BYTESIZE - here.bit ;
207
	m = BYTESIZE - here.bit;
180
	if ( n < m ) m = n ;
208
	if (n < m)m = n;
181
	/* extract m bytes from here.worksp */
209
	/* extract m bytes from here.worksp */
182
	here.worksp <<= m ;
210
	here.worksp <<= m;
183
	a = ( a << m ) + ( ( here.worksp & HI_MASK ) >> BYTESIZE ) ;
211
	a = (a << m) + ((here.worksp & HI_MASK) >> BYTESIZE);
184
	here.worksp &= LO_MASK ;
212
	here.worksp &= LO_MASK;
185
	n -= m ;
213
	n -= m;
186
	here.bit += m ;
214
	here.bit += m;
187
	if ( here.bit == BYTESIZE ) {
215
	if (here.bit == BYTESIZE) {
188
	    here.bit = 0 ;
216
	    here.bit = 0;
189
	    here.byte++ ;
217
	    here.byte++;
190
	}
218
	}
191
    }
219
    }
192
    return ( ( long ) a ) ;
220
    return((long)a);
193
}
221
}
194
 
222
 
195
 
223
 
196
/*
224
/*
197
    ALIGN INPUT FILE TO AN 8 BIT BOUNDARY
225
    ALIGN INPUT FILE TO AN 8 BIT BOUNDARY
198
 
226
 
199
    Bits are read from the input file until it is on an 8 bit boundary.
227
    Bits are read from the input file until it is on an 8 bit boundary.
200
*/
228
*/
201
 
229
 
202
void byte_align
230
void
203
    PROTO_Z ()
231
byte_align(void)
204
{
232
{
205
    while ( !( here.bit == 0 || here.bit == BYTESIZE ) ) {
233
    while (!(here.bit == 0 || here.bit == BYTESIZE)) {
206
	IGNORE fetch ( 1 ) ;
234
	IGNORE fetch(1);
207
    }
235
    }
208
    return ;
236
    return;
209
}
237
}
210
 
238
 
211
 
239
 
212
/*
240
/*
213
    GO TO A GIVEN PLACE IN THE INPUT FILE
241
    GO TO A GIVEN PLACE IN THE INPUT FILE
214
 
242
 
215
    The current position is set to the position indicated by the place p.
243
    The current position is set to the position indicated by the place p.
216
*/
244
*/
217
 
245
 
218
void set_place
246
void
219
    PROTO_N ( ( p ) )
-
 
220
    PROTO_T ( place *p )
247
set_place(place *p)
221
{
248
{
222
    int s ;
249
    int s;
223
    here.byte = p->byte ;
250
    here.byte = p->byte;
224
    here.bit = 0 ;
251
    here.bit = 0;
225
    s = fseek ( tdf_file, here.byte, SEEK_SET ) ;
252
    s = fseek(tdf_file, here.byte, SEEK_SET);
226
    if ( s ) fatal_error ( "Internal file seek error" ) ;
253
    if (s)fatal_error("Internal file seek error");
227
    ib_size = 0 ;
254
    ib_size = 0;
228
    IGNORE fetch ( p->bit ) ;
255
    IGNORE fetch(p->bit);
229
    return ;
256
    return;
230
}
257
}
231
 
258
 
232
 
259
 
233
/*
260
/*
234
    SKIP THE NEXT n BITS IN THE INPUT FILE
261
    SKIP THE NEXT n BITS IN THE INPUT FILE
235
 
262
 
236
    If n is small, the next n bits are read but discarded.  Otherwise
263
    If n is small, the next n bits are read but discarded.  Otherwise
237
    set_place is used.
264
    set_place is used.
238
*/
265
*/
239
 
266
 
240
void skip_bits
267
void
241
    PROTO_N ( ( n ) )
-
 
242
    PROTO_T ( long n )
268
skip_bits(long n)
243
{
269
{
244
    if ( read_error ) return ;
270
    if (read_error) return;
245
    if ( n <= 4 * BYTESIZE ) {
271
    if (n <= 4 * BYTESIZE) {
246
	IGNORE fetch ( ( int ) n ) ;
272
	IGNORE fetch((int)n);
247
    } else {
273
    } else {
248
	place p ;
274
	place p;
249
	long b = posn ( here ) + n ;
275
	long b = posn(here) + n;
250
	p.byte = b / BYTESIZE ;
276
	p.byte = b / BYTESIZE;
251
	p.bit = ( int ) ( b % BYTESIZE ) ;
277
	p.bit = (int)(b % BYTESIZE);
252
	set_place ( &p ) ;
278
	set_place(&p);
253
    }
279
    }
254
    return ;
280
    return;
255
}
281
}
256
 
282
 
257
 
283
 
258
/*
284
/*
259
    ARRAY OF SPACES
285
    ARRAY OF SPACES
260
 
286
 
261
    The indentation spaces used during pretty-printing are stored in
287
    The indentation spaces used during pretty-printing are stored in
262
    an array.
288
    an array.
263
*/
289
*/
264
 
290
 
265
static char *spaces1 ;
291
static char *spaces1;
266
 
292
 
267
 
293
 
268
/*
294
/*
269
    INITIALIZE SPACES
295
    INITIALIZE SPACES
270
 
296
 
271
    This routine initializes the array of indentation spaces, spaces1.
297
    This routine initializes the array of indentation spaces, spaces1.
272
*/
298
*/
273
 
299
 
274
void init_spaces
300
void
275
    PROTO_N ( ( d ) )
-
 
276
    PROTO_T ( int d )
301
init_spaces(int d)
277
{
302
{
278
    int i, j ;
303
    int i, j;
279
    if ( helpflag ) {
304
    if (helpflag) {
280
	spaces1 = alloc_nof ( char, 5000 ) ;
305
	spaces1 = alloc_nof(char, 5000);
281
	/* every dth character should be a '.' */
306
	/* every dth character should be a '.' */
282
	for ( i = 0, j = 0 ; i < 5000 ; i++ ) {
307
	for (i = 0, j = 0; i < 5000; i++) {
283
	    if ( ++j == d ) {
308
	    if (++j == d) {
284
		spaces1 [i] = '.' ;
309
		spaces1[i] = '.';
285
		j = 0 ;
310
		j = 0;
286
	    } else {
311
	    } else {
287
		spaces1 [i] = ' ' ;
312
		spaces1[i] = ' ';
288
	    }
313
	    }
289
	}
314
	}
290
    }
315
    }
291
    return ;
316
    return;
292
}
317
}
293
 
318
 
294
 
319
 
295
/*
320
/*
296
    OUTPUT n SPACES
321
    OUTPUT n SPACES
297
 
322
 
298
    This routine outputs n indentation spaces, using the array spaces1
323
    This routine outputs n indentation spaces, using the array spaces1
299
    when appropriate.
324
    when appropriate.
300
*/
325
*/
301
 
326
 
302
void spaces
327
void
303
    PROTO_N ( ( n ) )
-
 
304
    PROTO_T ( int n )
328
spaces(int n)
305
{
329
{
306
    if ( n < 0 ) n = 0 ;
330
    if (n < 0)n = 0;
307
    if ( printflag ) {
331
    if (printflag) {
308
	if ( helpflag ) {
332
	if (helpflag) {
309
	    if ( n ) {
333
	    if (n) {
310
		 IGNORE fwrite ( spaces1, sizeof ( char ),
334
		 IGNORE fwrite(spaces1, sizeof(char),
311
				   ( size_t ) n, pp_file ) ;
335
				  (size_t)n, pp_file);
312
	    }
336
	    }
313
	} else {
337
	} else {
314
	    int i ;
338
	    int i;
315
	    int q = ( n / TBSIZE ) ;
339
	    int q = (n / TBSIZE);
316
	    int r = ( n % TBSIZE ) ;
340
	    int r = (n % TBSIZE);
317
	    for ( i = 0 ; i < q ; i++ ) IGNORE fputc ( TAB, pp_file ) ;
341
	    for (i = 0; i < q; i++)IGNORE fputc(TAB, pp_file);
318
	    for ( i = 0 ; i < r ; i++ ) IGNORE fputc ( SPACE, pp_file ) ;
342
	    for (i = 0; i < r; i++)IGNORE fputc(SPACE, pp_file);
319
	}
343
	}
320
    }
344
    }
321
    column = n ;
345
    column = n;
322
    lastc = ' ' ;
346
    lastc = ' ';
323
    return ;
347
    return;
324
}
348
}