Subversion Repositories tendra.SVN

Rev

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

Rev 5 Rev 6
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 37... Line 67...
37
#include "output.h"
67
#include "output.h"
38
#include "xalloc.h"
68
#include "xalloc.h"
39
 
69
 
40
 
70
 
41
/*
71
/*
42
    BITMASKS
72
 * BITMASKS
43
 
73
 *
44
    This array gives the masks for each bit value.
74
 * This array gives the masks for each bit value.
45
*/
75
 */
46
 
76
 
47
unsigned long bitmask [ CHAR_BIT ] ;
77
unsigned long bitmask[CHAR_BIT];
48
 
78
 
49
 
79
 
50
/*
80
/*
51
    INITIALISE MASKS
81
 * INITIALISE MASKS
52
 
82
 *
53
    This routine initialises the array of bitmasks.  This is done
83
 * This routine initialises the array of bitmasks.  This is done
54
    dynamically rather than statically in case CHAR_BIT is not 8.
84
 * dynamically rather than statically in case CHAR_BIT is not 8.
55
*/
85
 */
56
 
86
 
57
void init_bitmask
87
void
58
    PROTO_Z ()
88
init_bitmask(void)
59
{
89
{
60
    int i ;
90
    int i;
61
    for ( i = 0 ; i < CHAR_BIT ; i++ ) bitmask [i] = MASK ( i ) ;
91
    for (i = 0; i < CHAR_BIT; i++) {
-
 
92
	    bitmask[i] = MASK(i);
-
 
93
    }
62
    return ;
94
    return;
63
}
95
}
64
 
96
 
65
 
97
 
66
/*
98
/*
67
    INPUT FILE
99
 * INPUT FILE
68
 
100
 *
69
    These variables describe the input file.  There is a one byte input
101
 * These variables describe the input file.  There is a one byte input
70
    buffer.
102
 * buffer.
71
*/
103
 */
72
 
104
 
73
static FILE *input_file ;
105
static FILE *input_file;
74
static unsigned long input_buff = 0 ;
106
static unsigned long input_buff = 0;
75
static int input_bits = 0 ;
107
static int input_bits = 0;
76
 
108
 
77
 
109
 
78
/*
110
/*
79
    READ A NUMBER OF BITS
111
 * READ A NUMBER OF BITS
-
 
112
 *
-
 
113
 * This routine reads the next n bits from the input file.
-
 
114
 */
80
 
115
 
81
    This routine reads the next n bits from the input file.
-
 
82
*/
-
 
83
 
-
 
84
static unsigned long read_bits
116
static unsigned long
85
    PROTO_N ( ( n ) )
-
 
86
    PROTO_T ( int n )
117
read_bits(int n)
87
{
118
{
88
    int b = input_bits ;
119
    int b = input_bits;
89
    unsigned long m = input_buff ;
120
    unsigned long m = input_buff;
90
    unsigned long r = 0 ;
121
    unsigned long r = 0;
91
    while ( n ) {
122
    while (n) {
92
	if ( b == 0 ) {
123
	if (b == 0) {
93
	    /* Read next byte */
124
	    /* Read next byte */
94
	    int c = getc ( input_file ) ;
125
	    int c = getc(input_file);
95
	    if ( c == EOF ) {
126
	    if (c == EOF) {
96
		error ( ERROR_FATAL, "Premature end of file" ) ;
127
		error(ERROR_FATAL, "Premature end of file");
97
	    } else {
128
	    } else {
98
		m = ( ( ( unsigned long ) c ) & MASK ( CHAR_BIT ) ) ;
129
		m = (((unsigned long)c) & MASK(CHAR_BIT));
99
	    }
130
	    }
100
	    b = CHAR_BIT ;
131
	    b = CHAR_BIT;
101
	}
132
	}
102
	if ( n >= b ) {
133
	if (n >= b) {
103
	    r = ( r << b ) | m ;
134
	    r = (r << b) | m;
104
	    n -= b ;
135
	    n -= b;
105
	    b = 0 ;
136
	    b = 0;
106
	} else {
137
	} else {
107
	    int c = b - n ;
138
	    int c = b - n;
108
	    r = ( r << n ) | ( m >> c ) ;
139
	    r = (r << n) | (m >> c);
109
	    m &= bitmask [c] ;
140
	    m &= bitmask[c];
110
	    n = 0 ;
141
	    n = 0;
111
	    b = c ;
142
	    b = c;
112
	}
143
	}
113
    }
144
    }
114
    input_bits = b ;
145
    input_bits = b;
115
    input_buff = m ;
146
    input_buff = m;
116
    return ( r ) ;
147
    return(r);
117
}
148
}
118
 
149
 
119
 
150
 
120
/*
151
/*
121
    READ AN INTEGER
152
 * READ AN INTEGER
122
 
153
 *
123
    This routine reads an integer from the input file.  This is encoded
154
 * This routine reads an integer from the input file.  This is encoded
124
    as a sequence of octal digits with a marker bit for the end of the
155
 * as a sequence of octal digits with a marker bit for the end of the
125
    number.
156
 * number.
126
*/
157
 */
127
 
158
 
128
static unsigned long read_int
159
static unsigned long
129
    PROTO_Z ()
160
read_int(void)
130
{
161
{
131
    unsigned long r = 0, d ;
162
    unsigned long r = 0, d;
132
    while ( d = read_bits ( 4 ), ( d & 0x08 ) == 0 ) {
163
    while (d = read_bits(4), (d & 0x08) == 0) {
133
	r = ( r << 3 ) | d ;
164
	r = (r << 3) | d;
134
    }
165
    }
135
    r = ( r << 3 ) | ( d & 0x07 ) ;
166
    r = (r << 3) | (d & 0x07);
136
    return ( r ) ;
167
    return(r);
137
}
168
}
138
 
169
 
139
 
170
 
140
/*
171
/*
141
    READ A STRING
172
 * READ A STRING
142
 
173
 *
143
    This routine reads an integer from the input file.  This is encoded
174
 * This routine reads an integer from the input file.  This is encoded
144
    as the string length followed by the characters comprising the
175
 * as the string length followed by the characters comprising the
145
    string (8 bits each).
176
 * string (8 bits each).
146
*/
177
 */
147
 
178
 
148
static char *read_string
179
static char *
149
    PROTO_Z ()
180
read_string(void)
150
{
181
{
151
    unsigned long i, n = read_int () ;
182
    unsigned long i, n = read_int();
152
    char *s = xstr ( ( long ) ( n + 1 ) ) ;
183
    char *s = xstr((long)(n + 1));
153
    for ( i = 0 ; i < n ; i++ ) {
184
    for (i = 0; i < n; i++) {
154
	s [i] = ( char ) read_bits ( 8 ) ;
185
	s[i] = (char)read_bits(8);
155
    }
186
    }
156
    s [n] = 0 ;
187
    s[n] = 0;
157
    return ( s ) ;
188
    return(s);
158
}
189
}
159
 
190
 
160
 
191
 
161
/*
192
/*
162
    READ A FILENAME
193
 * READ A FILENAME
163
 
194
 *
164
    File names are just strings, however they are buffered to save
195
 * File names are just strings, however they are buffered to save
165
    space.
196
 * space.
166
*/
197
 */
167
 
198
 
168
static char *read_filename
199
static char *
169
    PROTO_Z ()
200
read_filename(void)
170
{
201
{
171
    char *s ;
202
    char *s;
172
    static char *last_filename = "????" ;
203
    static char *last_filename = "????";
173
    if ( read_bits ( 1 ) ) {
204
    if (read_bits(1)) {
174
	s = last_filename ;
205
	s = last_filename;
175
    } else {
206
    } else {
176
	s = read_string () ;
207
	s = read_string();
177
	last_filename = s ;
208
	last_filename = s;
178
    }
209
    }
179
    return ( s ) ;
210
    return(s);
180
}
211
}
181
 
212
 
182
 
213
 
183
/*
214
/*
184
    AUTOMATICALLY GENERATED DISK READING ROUTINES
215
 * AUTOMATICALLY GENERATED DISK READING ROUTINES
-
 
216
 *
-
 
217
 * The main disk reading routines are automatically generated.  The
-
 
218
 * various macros are used to customise these routines.
-
 
219
 */
185
 
220
 
186
    The main disk reading routines are automatically generated.  The
-
 
187
    various macros are used to customise these routines.
-
 
188
*/
-
 
189
 
-
 
190
#define READ_BITS( N )		( ( unsigned ) read_bits ( N ) )
221
#define READ_BITS(N)		((unsigned)read_bits(N))
191
#define READ_ALIAS()		( ( unsigned ) read_int () )
222
#define READ_ALIAS()		((unsigned)read_int())
192
#define READ_DIM()		( ( unsigned ) read_int () )
223
#define READ_DIM()		((unsigned)read_int())
193
#define READ_int()		( ( int ) read_int () )
224
#define READ_int()		((int)read_int())
194
#define READ_number()		( ( number ) read_int () )
225
#define READ_number()		((number)read_int())
195
#define READ_string()		read_string ()
226
#define READ_string()		read_string()
196
#define READ_name_string()	read_filename ()
227
#define READ_name_string()	read_filename()
197
#define READ_zero_int()		0
228
#define READ_zero_int()		0
198
 
229
 
199
#include "read_def.h"
230
#include "read_def.h"
200
 
231
 
201
 
232
 
202
/*
233
/*
203
    READ AN INPUT FILE
234
 * READ AN INPUT FILE
204
 
235
 *
205
    This routine reads an algebra from the input file nm.
236
 * This routine reads an algebra from the input file nm.
206
*/
237
 */
207
 
238
 
208
void read_file
239
void
209
    PROTO_N ( ( nm ) )
-
 
210
    PROTO_T ( char *nm )
240
read_file(char *nm)
211
{
241
{
212
    char *s ;
242
    char *s;
213
 
243
 
214
    /* Open file */
244
    /* Open file */
215
    crt_line_no = -1 ;
245
    crt_line_no = -1;
216
    crt_file_name = nm ;
246
    crt_file_name = nm;
217
    input_file = fopen ( nm, "rb" ) ;
247
    input_file = fopen(nm, "rb");
218
    if ( input_file == NULL ) {
248
    if (input_file == NULL) {
219
	error ( ERROR_SERIOUS, "Can't open input file, '%s'", nm ) ;
249
	error(ERROR_SERIOUS, "Can't open input file, '%s'", nm);
220
	return ;
250
	return;
221
    }
251
    }
222
    init_bitmask () ;
252
    init_bitmask();
223
    input_buff = 0 ;
253
    input_buff = 0;
224
    input_bits = 0 ;
254
    input_bits = 0;
225
 
255
 
226
    /* Confirm file header */
256
    /* Confirm file header */
227
    s = READ_string () ;
257
    s = READ_string();
228
    if ( !streq ( s, calculus_NAME ) ) {
258
    if (!streq(s, calculus_NAME)) {
229
	error ( ERROR_FATAL, "Invalid file header identifier" ) ;
259
	error(ERROR_FATAL, "Invalid file header identifier");
230
    }
260
    }
231
    s = READ_string () ;
261
    s = READ_string();
232
    if ( !streq ( s, calculus_VERSION ) ) {
262
    if (!streq(s, calculus_VERSION)) {
233
	error ( ERROR_FATAL, "Invalid file header version, '%s'", s ) ;
263
	error(ERROR_FATAL, "Invalid file header version, '%s'", s);
234
    }
264
    }
235
 
265
 
236
    /* Read the algebra */
266
    /* Read the algebra */
237
    algebra->name = READ_string () ;
267
    algebra->name = READ_string();
238
    algebra->major_no = READ_int () ;
268
    algebra->major_no = READ_int();
239
    algebra->minor_no = READ_int () ;
269
    algebra->minor_no = READ_int();
240
    algebra->types = READ_list_ptr_type () ;
270
    algebra->types = READ_list_ptr_type();
241
    clear_calculus_alias () ;
271
    clear_calculus_alias();
242
 
272
 
243
    /* Extract lists of primitives etc */
273
    /* Extract lists of primitives etc */
244
    LOOP_TYPE {
274
    LOOP_TYPE {
245
	TYPE_P t = CRT_TYPE ;
275
	TYPE_P t = CRT_TYPE;
246
	TYPE t0 = DEREF_type ( t ) ;
276
	TYPE t0 = DEREF_type(t);
247
	switch ( TAG_type ( t0 ) ) {
277
	switch (TAG_type(t0)) {
248
	    case type_primitive_tag : {
278
	    case type_primitive_tag: {
249
		PRIMITIVE_P p = DEREF_ptr ( type_primitive_prim ( t0 ) ) ;
279
		PRIMITIVE_P p = DEREF_ptr(type_primitive_prim(t0));
250
		CONS_ptr ( p, algebra->primitives, algebra->primitives ) ;
280
		CONS_ptr(p, algebra->primitives, algebra->primitives);
251
		break ;
281
		break;
252
	    }
282
	    }
253
	    case type_ident_tag : {
283
	    case type_ident_tag: {
254
		IDENTITY_P p = DEREF_ptr ( type_ident_id ( t0 ) ) ;
284
		IDENTITY_P p = DEREF_ptr(type_ident_id(t0));
255
		CONS_ptr ( p, algebra->identities, algebra->identities ) ;
285
		CONS_ptr(p, algebra->identities, algebra->identities);
256
		break ;
286
		break;
257
	    }
287
	    }
258
	    case type_enumeration_tag : {
288
	    case type_enumeration_tag: {
259
		ENUM_P p = DEREF_ptr ( type_enumeration_en ( t0 ) ) ;
289
		ENUM_P p = DEREF_ptr(type_enumeration_en(t0));
260
		CONS_ptr ( p, algebra->enumerations, algebra->enumerations ) ;
290
		CONS_ptr(p, algebra->enumerations, algebra->enumerations);
261
		break ;
291
		break;
262
	    }
292
	    }
263
	    case type_structure_tag : {
293
	    case type_structure_tag: {
264
		STRUCTURE_P p = DEREF_ptr ( type_structure_struc ( t0 ) ) ;
294
		STRUCTURE_P p = DEREF_ptr(type_structure_struc(t0));
265
		CONS_ptr ( p, algebra->structures, algebra->structures ) ;
295
		CONS_ptr(p, algebra->structures, algebra->structures);
266
		break ;
296
		break;
267
	    }
297
	    }
268
	    case type_onion_tag : {
298
	    case type_onion_tag: {
269
		UNION_P p = DEREF_ptr ( type_onion_un ( t0 ) ) ;
299
		UNION_P p = DEREF_ptr(type_onion_un(t0));
270
		CONS_ptr ( p, algebra->unions, algebra->unions ) ;
300
		CONS_ptr(p, algebra->unions, algebra->unions);
271
		break ;
301
		break;
272
	    }
302
	    }
273
	}
303
	}
274
    }
304
    }
275
 
305
 
276
    /* Close file */
306
    /* Close file */
277
    fclose_v ( input_file ) ;
307
    fclose_v(input_file);
278
    return ;
308
    return;
279
}
309
}