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 45... Line 75...
45
    Some strings are output directly.  Others are built up gradually
75
    Some strings are output directly.  Others are built up gradually
46
    in this buffer.  length gives the length of the current string.
76
    in this buffer.  length gives the length of the current string.
47
    page_length gives the size of the buffer.
77
    page_length gives the size of the buffer.
48
*/
78
*/
49
 
79
 
50
int length ;
80
int length;
51
static int page_length ;
81
static int page_length;
52
static char *page = null ;
82
static char *page = null;
53
 
83
 
54
 
84
 
55
/*
85
/*
56
    TOP LEVEL WORD AND CURRENT WORD POINTER
86
    TOP LEVEL WORD AND CURRENT WORD POINTER
57
 
87
 
58
    The top of the TDF tree is given by word1.  The current position in
88
    The top of the TDF tree is given by word1.  The current position in
59
    the tree is given by word_ptr.
89
    the tree is given by word_ptr.
60
*/
90
*/
61
 
91
 
62
word word1 = { SIMPLE, 0, null, null, null } ;
92
word word1 = { SIMPLE, 0, null, null, null };
63
word *word_ptr ;
93
word *word_ptr;
64
 
94
 
65
 
95
 
66
/*
96
/*
67
    SET UP INITIAL PAGE AND WORDS
97
    SET UP INITIAL PAGE AND WORDS
68
 
98
 
69
    The page buffer is initialized and the current word pointer is set
99
    The page buffer is initialized and the current word pointer is set
70
    to the top-level word.
100
    to the top-level word.
71
*/
101
*/
72
 
102
 
73
void initialize_tree
103
void
74
    PROTO_Z ()
104
initialize_tree(void)
75
{
105
{
76
    if ( page == null ) {
106
    if (page == null) {
77
	page = alloc_nof ( char, 10000 ) ;
107
	page = alloc_nof(char, 10000);
78
	page_length = 10000 ;
108
	page_length = 10000;
79
    }
109
    }
80
    word_ptr = &word1 ;
110
    word_ptr = &word1;
81
    length = 0 ;
111
    length = 0;
82
    return ;
112
    return;
83
}
113
}
84
 
114
 
85
 
115
 
86
/*
116
/*
87
    START A NEW WORD
117
    START A NEW WORD
88
 
118
 
89
    A new word of layout type c is created.
119
    A new word of layout type c is created.
90
*/
120
*/
91
 
121
 
92
word *new_word
122
word *
93
    PROTO_N ( ( c ) )
-
 
94
    PROTO_T ( int c )
123
new_word(int c)
95
{
124
{
96
#define BLOCK 100
125
#define BLOCK 100
97
    static word *wblock ;
126
    static word *wblock;
98
    static int block_count = BLOCK ;
127
    static int block_count = BLOCK;
99
 
128
 
100
    word *new_ptr ;
129
    word *new_ptr;
101
    if ( printflag ) {
130
    if (printflag) {
102
 
131
 
103
	if ( block_count == BLOCK ) {
132
	if (block_count == BLOCK) {
104
	    /* Allocate space if required */
133
	    /* Allocate space if required */
105
	    wblock = alloc_nof ( word, BLOCK ) ;
134
	    wblock = alloc_nof(word, BLOCK);
106
	    block_count = 0 ;
135
	    block_count = 0;
107
	}
136
	}
108
 
137
 
109
	/* set up fields of new word */
138
	/* set up fields of new word */
110
	new_ptr = wblock + ( block_count++ ) ;
139
	new_ptr = wblock + (block_count++);
111
	if ( word_ptr->type != SIMPLE && word_ptr->son == null ) {
140
	if (word_ptr->type != SIMPLE && word_ptr->son == null) {
112
	    word_ptr->son = new_ptr ;
141
	    word_ptr->son = new_ptr;
113
	} else {
142
	} else {
114
	    word_ptr->bro = new_ptr ;
143
	    word_ptr->bro = new_ptr;
115
	}
144
	}
116
	word_ptr = new_ptr ;
145
	word_ptr = new_ptr;
117
	word_ptr->type = ( char ) c ;
146
	word_ptr->type = (char)c;
118
	if ( length ) {
147
	if (length) {
119
	    word_ptr->text = page ;
148
	    word_ptr->text = page;
120
	    word_ptr->length = length ;
149
	    word_ptr->length = length;
121
	    page += ( length + 1 ) ;
150
	    page += (length + 1);
122
	    page_length -= ( length + 1 ) ;
151
	    page_length -= (length + 1);
123
	    if ( page_length < 100 ) {
152
	    if (page_length < 100) {
124
		page = alloc_nof ( char, 10000 ) ;
153
		page = alloc_nof(char, 10000);
125
		page_length = 10000 ;
154
		page_length = 10000;
126
	    }
155
	    }
127
	    length = 0 ;
156
	    length = 0;
128
	} else {
157
	} else {
129
	    word_ptr->text = "" ;
158
	    word_ptr->text = "";
130
	    word_ptr->length = 0 ;
159
	    word_ptr->length = 0;
131
	}
160
	}
132
	word_ptr->son = null ;
161
	word_ptr->son = null;
133
	word_ptr->bro = null ;
162
	word_ptr->bro = null;
134
    }
163
    }
135
    return ( word_ptr ) ;
164
    return(word_ptr);
136
}
165
}
137
 
166
 
138
 
167
 
139
/*
168
/*
140
    OUTPUT A SINGLE CHARACTER INTO INTERNAL MEMORY
169
    OUTPUT A SINGLE CHARACTER INTO INTERNAL MEMORY
141
 
170
 
142
    The character c is appended to the internal memory buffer.
171
    The character c is appended to the internal memory buffer.
143
*/
172
*/
144
 
173
 
145
void out_char
174
void
146
    PROTO_N ( ( c ) )
-
 
147
    PROTO_T ( int c )
175
out_char(int c)
148
{
176
{
149
    if ( printflag ) {
177
    if (printflag) {
150
	page [ length ] = ( char ) c ;
178
	page[length] = (char)c;
151
	length++ ;
179
	length++;
152
	page [ length ] = 0 ;
180
	page[length] = 0;
153
    }
181
    }
154
    return ;
182
    return;
155
}
183
}
156
 
184
 
157
 
185
 
158
/*
186
/*
159
    OUTPUT A STRING INTO INTERNAL MEMORY
187
    OUTPUT A STRING INTO INTERNAL MEMORY
160
 
188
 
161
    The string str is appended to the internal memory buffer.
189
    The string str is appended to the internal memory buffer.
162
*/
190
*/
163
 
191
 
164
void out_string
192
void
165
    PROTO_N ( ( str ) )
-
 
166
    PROTO_T ( char *str )
193
out_string(char *str)
167
{
194
{
168
    if ( printflag ) {
195
    if (printflag) {
169
	IGNORE strcpy ( page + length, str ) ;
196
	IGNORE strcpy(page + length, str);
170
	length += ( int ) strlen ( str ) ;
197
	length += (int)strlen(str);
171
    }
198
    }
172
    return ;
199
    return;
173
}
200
}
174
 
201
 
175
 
202
 
176
/*
203
/*
177
    MAKE A STRING INTO A WORD
204
    MAKE A STRING INTO A WORD
178
 
205
 
179
    This routine creates a simple word from the string str, appending it
206
    This routine creates a simple word from the string str, appending it
180
    to any text in the internal memory buffer.
207
    to any text in the internal memory buffer.
181
*/
208
*/
182
 
209
 
183
void out
210
void
184
    PROTO_N ( ( str ) )
-
 
185
    PROTO_T ( char *str )
211
out(char *str)
186
{
212
{
187
    if ( printflag ) {
213
    if (printflag) {
188
	if ( length ) {
214
	if (length) {
189
	    out_string ( str ) ;
215
	    out_string(str);
190
	    IGNORE new_word ( SIMPLE ) ;
216
	    IGNORE new_word(SIMPLE);
191
	} else {
217
	} else {
192
	    word *ptr = new_word ( SIMPLE ) ;
218
	    word *ptr = new_word(SIMPLE);
193
	    ptr->text = str ;
219
	    ptr->text = str;
194
	    ptr->length = ( int ) strlen ( str ) ;
220
	    ptr->length = (int)strlen(str);
195
	}
221
	}
196
    }
222
    }
197
    return ;
223
    return;
198
}
224
}
199
 
225
 
200
 
226
 
201
/*
227
/*
202
    MAKE AN INTEGER INTO A WORD
228
    MAKE AN INTEGER INTO A WORD
203
 
229
 
204
    This routine creates a simple word from an integer.
230
    This routine creates a simple word from an integer.
205
*/
231
*/
206
 
232
 
207
void out_int
233
void
208
    PROTO_N ( ( n ) )
-
 
209
    PROTO_T ( long n )
234
out_int(long n)
210
{
235
{
211
    if ( printflag ) {
236
    if (printflag) {
212
	/* Note that the input is cast to an unsigned int */
237
	/* Note that the input is cast to an unsigned int */
213
	unsigned long m = ( unsigned long ) n, dig, power = 1 ;
238
	unsigned long m = (unsigned long)n, dig, power = 1;
214
 
239
 
215
	/* Get the highest power of 10 dividing m */
240
	/* Get the highest power of 10 dividing m */
216
	while ( ( m / power ) >= 10 ) power *= 10 ;
241
	while ((m / power) >= 10)power *= 10;
217
 
242
 
218
	/* Now output digits of m */
243
	/* Now output digits of m */
219
	while ( power != 0 ) {
244
	while (power != 0) {
220
	    dig = ( m / power ) ;
245
	    dig = (m / power);
221
	    m -= dig * power ;
246
	    m -= dig * power;
222
	    power = ( power / 10 ) ;
247
	    power = (power / 10);
223
	    out_char ( charact ( dig ) ) ;
248
	    out_char(charact(dig));
224
	}
249
	}
225
	/* Make this into a simple word */
250
	/* Make this into a simple word */
226
	IGNORE new_word ( SIMPLE ) ;
251
	IGNORE new_word(SIMPLE);
227
    }
252
    }
228
    return ;
253
    return;
229
}
254
}
230
 
255
 
231
 
256
 
232
/*
257
/*
233
    MAKE A STRING OF OCTAL DIGITS INTO A WORD
258
    MAKE A STRING OF OCTAL DIGITS INTO A WORD
Line 236... Line 261...
236
    and a sign.  If the string of octal digits corresponds to a
261
    and a sign.  If the string of octal digits corresponds to a
237
    number of at most 32 bits, out_int is used, otherwise the string
262
    number of at most 32 bits, out_int is used, otherwise the string
238
    is output directly.
263
    is output directly.
239
*/
264
*/
240
 
265
 
241
void out_signed
266
void
242
    PROTO_N ( ( n, sn ) )
-
 
243
    PROTO_T ( char *n X int sn )
267
out_signed(char *n, int sn)
244
{
268
{
245
    if ( printflag ) {
269
    if (printflag) {
246
	/* Calculate the number of binary digits in n */
270
	/* Calculate the number of binary digits in n */
247
	int a = digit ( *n ), d ;
271
	int a = digit(*n), d;
248
	if ( a & 4 ) {
272
	if (a & 4) {
249
	    d = 3 * tdf_int_digits ;
273
	    d = 3 * tdf_int_digits;
250
	} else {
274
	} else {
251
	    if ( a & 2 ) {
275
	    if (a & 2) {
252
		d = 3 * tdf_int_digits - 1 ;
276
		d = 3 * tdf_int_digits - 1;
253
	    } else {
277
	    } else {
254
		d = 3 * tdf_int_digits - 2 ;
278
		d = 3 * tdf_int_digits - 2;
255
	    }
279
	    }
256
	}
280
	}
257
	if ( d <= BYTESIZE * ( int ) sizeof ( long ) ) {
281
	if (d <= BYTESIZE *(int)sizeof(long)) {
258
	    /* If n will fit into a long work out its value */
282
	    /* If n will fit into a long work out its value */
259
	    char *s ;
283
	    char *s;
260
	    long t = 0 ;
284
	    long t = 0;
261
	    for ( s = n ; *s ; s++ ) t = 8 * t + digit ( *s ) ;
285
	    for (s = n; *s; s++)t = 8 * t + digit(*s);
262
	    if ( sn && t ) out_char ( '-' ) ;
286
	    if (sn && t)out_char('-');
263
	    out_int ( t ) ;
287
	    out_int(t);
264
	} else {
288
	} else {
265
	    /* Otherwise output n as a string of octal digits */
289
	    /* Otherwise output n as a string of octal digits */
266
	    word *w ;
290
	    word *w;
267
	    out_string ( "octal" ) ;
291
	    out_string("octal");
268
	    w = new_word ( HORIZ_BRACKETS ) ;
292
	    w = new_word(HORIZ_BRACKETS);
269
	    out_string ( sn ? "-0" : "0" ) ;
293
	    out_string(sn ? "-0" : "0");
270
	    out ( n ) ;
294
	    out(n);
271
	    end_word ( w ) ;
295
	    end_word(w);
272
	}
296
	}
273
    }
297
    }
274
    return ;
298
    return;
275
}
299
}
276
 
300
 
277
 
301
 
278
/*
302
/*
279
    MAKE A UNIQUE INTO A WORD
303
    MAKE A UNIQUE INTO A WORD
280
 
304
 
281
    The unique u is output.
305
    The unique u is output.
282
*/
306
*/
283
 
307
 
284
void out_unique
308
void
285
    PROTO_N ( ( u ) )
-
 
286
    PROTO_T ( unique u )
309
out_unique(unique u)
287
{
310
{
288
    word *w ;
311
    word *w;
289
    out_string ( "unique" ) ;
312
    out_string("unique");
290
    w = new_word ( HORIZ_BRACKETS ) ;
313
    w = new_word(HORIZ_BRACKETS);
291
    while ( *u ) {
314
    while (*u) {
292
	out ( *u ) ;
315
	out(*u);
293
	u++ ;
316
	u++;
294
    }
317
    }
295
    end_word ( w ) ;
318
    end_word(w);
296
    return ;
319
    return;
297
}
320
}
298
 
321
 
299
 
322
 
300
/*
323
/*
301
    UTILITY FORMATTING ROUTINE
324
    UTILITY FORMATTING ROUTINE
Line 303... Line 326...
303
    This routine is used to format a function and its arguments.  The
326
    This routine is used to format a function and its arguments.  The
304
    function name is given by func, the layout type by c, and the
327
    function name is given by func, the layout type by c, and the
305
    arguments by the decode string args.
328
    arguments by the decode string args.
306
*/
329
*/
307
 
330
 
308
void format
331
void
309
    PROTO_N ( ( c, func, args ) )
-
 
310
    PROTO_T ( int c X char *func X char *args )
332
format(int c, char *func, char *args)
311
{
333
{
312
    if ( printflag ) {
334
    if (printflag) {
313
	word *ptr ;
335
	word *ptr;
314
	if ( length ) {
336
	if (length) {
315
	    out_string ( func ) ;
337
	    out_string(func);
316
	    ptr = new_word ( c ) ;
338
	    ptr = new_word(c);
317
	} else {
339
	} else {
318
	    ptr = new_word ( c ) ;
340
	    ptr = new_word(c);
319
	    ptr->text = func ;
341
	    ptr->text = func;
320
	    ptr->length = ( int ) strlen ( func ) ;
342
	    ptr->length = (int)strlen(func);
321
	}
343
	}
322
	decode ( args ) ;
344
	decode(args);
323
	end_word ( ptr ) ;
345
	end_word(ptr);
324
    } else {
346
    } else {
325
	decode ( args ) ;
347
	decode(args);
326
    }
348
    }
327
    return ;
349
    return;
328
}
350
}