Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
6 7u83 1
/*
2
 * Copyright (c) 2002-2006 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
/*
32
    		 Crown Copyright (c) 1997
33
 
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:-
42
 
43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
45
 
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;
49
 
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;
53
 
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
/**********************************************************************
62
$Author: release $
63
$Date: 1998/02/04 10:46:41 $
64
$Revision: 1.2 $
65
$Log: units.c,v $
66
 * Revision 1.2  1998/02/04  10:46:41  release
67
 * Changes during testing.
68
 *
69
 * Revision 1.1.1.1  1998/01/17  15:57:25  release
70
 * First version to be checked into rolling release.
71
 *
72
 * Revision 1.4  1995/09/05  16:27:54  currie
73
 * include files + 2r lex
74
 *
75
 * Revision 1.3  1995/09/04  10:29:24  currie
76
 * tld units instead of tld2
77
 *
78
 * Revision 1.2  1995/08/21  16:00:59  currie
79
 * put <stdlib> at top of headers
80
 *
81
 * Revision 1.1  1995/04/07  14:29:07  currie
82
 * Initial revision
83
 *
84
 * Revision 1.1  1995/04/07  14:29:07  currie
85
 * Initial revision
86
 *
87
 * Revision 1.2  1994/07/21  10:36:40  currie
88
 * Added banner
89
 *
90
***********************************************************************/
91
#include "config.h"
92
#include "util.h"
93
#include "readstreams.h"
94
#include "streams.h"
95
#include "units.h"
96
#include "encodings.h"
97
#include "enc_nos.h"
98
#include "includes.h"
99
#include "errors.h"
100
#include "defs.h"
101
#include "syntax.h"
102
 
103
 
104
FILE *out_file;
105
Unit units[NO_OF_UNITS];
106
int current_Unit;
107
int capsule_names[NO_OF_ENTITIES];
108
TDF lk_externs[NO_OF_ENTITIES];
109
char *ent_names[NO_OF_ENTITIES] = {
110
	"token",
111
	"tag",
112
	"alignment"
113
};
114
char *unit_names[NO_OF_UNITS] = {
115
	"tld",
116
	"versions",
117
	"tokdec",
118
	"tokdef",
119
	"aldef",
120
	"tagdec",
121
	"tagdef"
122
};
123
 
124
int line_no_tok = -1;
125
 
126
 
127
int
128
next_label(void)
129
{
130
	return(units[current_Unit].no_labs++);
131
}
132
 
133
 
134
int
135
next_unit_name(int ent)
136
{
137
	return((units[current_Unit].no_entity)[ent]++);
138
}
139
 
140
 
141
Name
142
next_name(int ent)
143
{
144
	Name ans;
145
	ans.unit_no = current_Unit;
146
	ans.unit_name = next_unit_name(ent);
147
	return ans;
148
}
149
 
150
 
151
int
152
next_capsule_name(int ent)
153
{
154
	return(capsule_names[ent]++);
155
}
156
 
157
 
158
int
159
capsule_name(Name *n, int ent)
160
{
161
	Unit *nl_unit = &units[n->unit_no];
162
	Link *nl_links = (nl_unit->links)[ent];
163
	while (nl_links != (Link *)0) {
164
		if (n->unit_name == nl_links->unit_name)
165
			return nl_links->capsule_name;
166
		nl_links = nl_links->next;
167
	}
168
	Assert(n->unit_name < (nl_unit->no_entity)[ent]);
169
	nl_links = MALLOC(Link);
170
	nl_links->capsule_name = next_capsule_name(ent);
171
	nl_links->unit_name = n->unit_name;
172
	nl_links->next = (nl_unit->links)[ent];
173
	(nl_unit->links)[ent] = nl_links;
174
	return nl_links->capsule_name;
175
}
176
 
177
 
178
int
179
cname_to_lname(int c_name, int ent)
180
{
181
	Link *links = (units[current_Unit].links)[ent];
182
	while (links != (Link *)0) {
183
		if (c_name == links->capsule_name) {
184
			return links->unit_name;
185
		}
186
		links = links->next;
187
	}
188
	links = MALLOC(Link);
189
	links->capsule_name = c_name;
190
	links->unit_name = next_unit_name(ent);
191
	links->next = (units[current_Unit].links)[ent];
192
	(units[current_Unit].links)[ent] = links;
193
	return links->unit_name;
194
}
195
 
196
 
197
int
198
non_local(Name *n, int ent)
199
{
200
	return cname_to_lname(capsule_name(n, ent),ent);
201
}
202
 
203
 
204
void
205
make_tag(Name *n)
206
{
207
	int loc_name = (current_Unit == n->unit_no) ? n->unit_name :
208
	    non_local(n, tag_ent);
209
	Assert(loc_name < (units[current_Unit].no_entity)[tag_ent]);
210
	o_make_tag(out_tdfint32(UL(loc_name)));
211
}
212
 
213
 
214
void
215
make_tok(Name *n)
216
{
217
	int loc_name = (current_Unit == n->unit_no) ? n->unit_name :
218
	    non_local(n, tok_ent);
219
	Assert(loc_name < (units[current_Unit].no_entity)[tok_ent]);
220
	o_make_tok(out_tdfint32(UL(loc_name)));
221
}
222
 
223
 
224
void
225
make_al_tag(Name *n)
226
{
227
	int loc_name = (current_Unit == n->unit_no) ? n->unit_name :
228
	    non_local(n, al_tag_ent);
229
	Assert(loc_name < (units[current_Unit].no_entity)[al_tag_ent]);
230
	o_make_al_tag(out_tdfint32(UL(loc_name)));
231
}
232
 
233
 
234
long
235
local_name(Name *n, int ent)
236
{
237
	int loc_name = (current_Unit == n->unit_no) ? n->unit_name :
238
	    non_local(n, ent);
239
	Assert(loc_name < (units[current_Unit].no_entity)[ent]);
240
	return loc_name;
241
}
242
 
243
 
244
void
245
FILENAME(void)
246
{
247
	o_make_filename(
248
	  o_make_nat(out_tdfint32(UL(0))),
249
	  out_tdfstring_bytes("?", 8, 1),
250
	  out_tdfstring_bytes(file_name, 8, UI(strlen(file_name)))
251
	);
252
}
253
 
254
 
255
void
256
init_units(void)
257
{
258
	int i,j;
259
	if (MAJOR_NO == 4 && MINOR_NO == 1) {
260
		MINOR_NO = 0;
261
	}
262
	for (i = 0; i < NO_OF_UNITS; i++) {
263
		Unit *u = &units[i];
264
		INIT_TDF(&u->tdf);
265
		u->no_labs = 0;
266
		u->present = (i == version_unit || i == tld2_unit);
267
		for (j = 0; j < NO_OF_ENTITIES; j++) {
268
			(u->no_entity)[j] = 0;
269
			(u->links)[j] = (Link *)0;
270
		}
271
	}
272
}
273
 
274
 
275
static void
276
make_links(Link *l)
277
{
278
	o_make_links(
279
		     { while (l!= (Link *)0) {
280
		     LIST_ELEM(
281
			       o_make_link(out_tdfint32(UL(l->unit_name)),
282
					   out_tdfint32(UL(l->capsule_name))
283
					  )
284
			      );
285
		     l = l->next;
286
		     }
287
		     }
288
		    );
289
}
290
 
291
 
292
static void
293
props(int unit_no)
294
{
295
  Unit * u = units+unit_no;
296
  TDF temp;
297
  NEW_STREAM(&temp,
298
  switch (unit_no) {
299
    case version_unit:
300
    	o_make_versions(
301
    	   { LIST_ELEM(o_make_version(out_tdfint32(MAJOR_NO),
302
    				      out_tdfint32(MINOR_NO)
303
    				    )
304
    		      );
305
    	   }
306
       );
307
     	break;
308
    case tokdec_unit:
309
    	o_make_tokdecs({ append_TDF(&u->tdf, 1);
310
    			 current_TDF->no = u->tdf.no;
311
    			}
312
    		      );
313
        break;
314
    case tokdef_unit:
315
    	o_make_tokdefs(out_tdfint32(UL(u->no_labs)),
316
			{ append_TDF(&u->tdf, 1);
317
    			 current_TDF->no = u->tdf.no;
318
    			}
319
    		      );
320
    	break;
321
    case tagdec_unit:
322
    	o_make_tagdecs(out_tdfint32(UL(u->no_labs)),
323
			{ append_TDF(&u->tdf, 1);
324
    			 current_TDF->no = u->tdf.no;
325
    			}
326
    		      );
327
    	break;
328
    case tagdef_unit:
329
    	o_make_tagdefs(out_tdfint32(UL(u->no_labs)),
330
			{ append_TDF(&u->tdf, 1);
331
    			 current_TDF->no = u->tdf.no;
332
    			}
333
    		      );
334
    	break;
335
    case al_tagdef_unit:
336
    	o_make_al_tagdefs(out_tdfint32(UL(u->no_labs)),
337
			{ append_TDF(&u->tdf, 1);
338
    			 current_TDF->no = u->tdf.no;
339
    			}
340
    		      );
341
    	break;
342
    default: fail("Don't know unit type");
343
  }
344
);
345
 append_bytestream(&temp,1);
346
}
347
 
348
 
349
void make_unit
350
(int unit_no)
351
{
352
	Unit * u = units+unit_no;
353
	int i;
354
	o_make_unit(
355
	  if (unit_no != tld2_unit)
356
	  { for (i=0; i<NO_OF_ENTITIES; i++) {
357
	   	LIST_ELEM(out_tdfint32(UL(u->no_entity[i])));
358
	     }
359
	  },
360
	  if (unit_no != tld2_unit)
361
	  { for (i=0; i<NO_OF_ENTITIES; i++) {
362
	   	LIST_ELEM(make_links(u->links[i]));
363
	     }
364
	  },
365
	  if (unit_no != tld2_unit)
366
	  	{ props(unit_no); }
367
	  else { append_bytestream(&u->tdf, 1);	}
368
	);
369
}
370
 
371
 
372
static int
373
get_byte(void)
374
{
375
	unsigned int x = 0;
376
	int i;
377
	for (i = 7; i >= 0; i--) {
378
		unsigned int y = get_bit();
379
		if (y > 1) {
380
			return -1;
381
		}
382
		x += (y << i);
383
	}
384
	return (int)x;
385
}
386
 
387
 
388
void
389
make_tdf_file(TDF *s, FILE *file)
390
{
391
  	Instream ins;
392
  	Chunk *ch = s->first;
393
  	int offst = 0;
394
  	int x;
395
	curin = &ins;
396
	curin->ch = ch;
397
	curin->byte_pos = 0;
398
	curin->bit_pos = 0;
399
 
400
	while (ch != (Chunk *)0) {
401
	   offst += ch->offst;
402
	   offst &= 7;
403
	   if (ch->next == (Chunk *)0 || ch->next->aligned) {
404
	   	if (offst != 0) {
405
	   	   ch->offst += (unsigned char)(8 - offst);
406
	   	   if (ch->offst >= 8) {
407
			   ch->usage += 1;
408
			   ch->offst -= 8;
409
		   }
410
	   	   offst=0;
411
	   	}
412
	   }
413
	   ch = ch->next;
414
	}
415
 
416
    	while ((x = get_byte()) >= 0) {
417
    		IGNORE fputc(x, file);
418
    	}
419
}