Subversion Repositories tendra.SVN

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

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