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-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
 */
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
*/
29
 
59
 
30
 
60
 
31
/*** capsule.c --- TDF capsule ADT.
61
/*** capsule.c --- TDF capsule ADT.
32
 *
62
 *
33
 ** Author: Steve Folkes <smf@hermes.mod.uk>
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
34
 *
64
 *
35
 *** Commentary:
65
 *** Commentary:
36
 *
66
 *
37
 * This file implements the TDF capsule routines used by the TDF linker.
67
 * This file implements the TDF capsule routines used by the TDF linker.
38
 *
68
 *
39
 *** Change Log:
69
 *** Change Log:
40
 * $Log: capsule.c,v $
70
 * $Log: capsule.c,v $
41
 * Revision 1.1.1.1  1998/01/17  15:57:18  release
71
 * Revision 1.1.1.1  1998/01/17  15:57:18  release
42
 * First version to be checked into rolling release.
72
 * First version to be checked into rolling release.
43
 *
73
 *
Line 95... Line 125...
95
typedef struct ShapeDataT {
125
typedef struct ShapeDataT {
96
    ShapeEntryP			entry;
126
    ShapeEntryP			entry;
97
    unsigned			num_ids;
127
    unsigned			num_ids;
98
    unsigned		       *id_maps;
128
    unsigned		       *id_maps;
99
} ShapeDataT, *ShapeDataP;
129
} ShapeDataT, *ShapeDataP;
100
 
130
 
101
typedef struct NameDataT {
131
typedef struct NameDataT {
102
    NameEntryP		       *names_vec;
132
    NameEntryP		       *names_vec;
103
    unsigned			num_names;
133
    unsigned			num_names;
104
} NameDataT, *NameDataP;
134
} NameDataT, *NameDataP;
105
 
135
 
Line 107... Line 137...
107
 
137
 
108
#define NUM_DEFAULT_UNIT_SETS	(12)
138
#define NUM_DEFAULT_UNIT_SETS	(12)
109
 
139
 
110
/*--------------------------------------------------------------------------*/
140
/*--------------------------------------------------------------------------*/
111
 
141
 
112
static CStringP capsule_default_unit_set_names [NUM_DEFAULT_UNIT_SETS] = {
142
static CStringP capsule_default_unit_set_names[NUM_DEFAULT_UNIT_SETS] = {
113
    "tld",
143
    "tld",
114
    "tld2",
144
    "tld2",
115
    "versions",
145
    "versions",
116
    "tokdec",
146
    "tokdec",
117
    "tokdef",
147
    "tokdef",
Line 121... Line 151...
121
    "diagdef",
151
    "diagdef",
122
    "dgcompunit",
152
    "dgcompunit",
123
    "tagdef",
153
    "tagdef",
124
    "linkinfo"
154
    "linkinfo"
125
};
155
};
126
static UnitSetT   capsule_default_unit_sets [NUM_DEFAULT_UNIT_SETS];
156
static UnitSetT   capsule_default_unit_sets[NUM_DEFAULT_UNIT_SETS];
127
static unsigned   capsule_num_unit_sets;
157
static unsigned   capsule_num_unit_sets;
128
static UnitSetP   capsule_unit_sets     = NIL (UnitSetP);
158
static UnitSetP   capsule_unit_sets     = NIL(UnitSetP);
129
static unsigned   capsule_tld_index     = 0;
159
static unsigned   capsule_tld_index     = 0;
130
static unsigned   capsule_tld2_index    = 1;
160
static unsigned   capsule_tld2_index    = 1;
131
static unsigned   capsule_unit_length;
161
static unsigned   capsule_unit_length;
132
static unsigned   capsule_unit_offset;
162
static unsigned   capsule_unit_offset;
133
static ExceptionP XX_capsule_error      = EXCEPTION ("error in TDF capsule");
163
static ExceptionP XX_capsule_error      = EXCEPTION("error in TDF capsule");
134
static unsigned   capsule_major_version = 0;
164
static unsigned   capsule_major_version = 0;
135
static unsigned   capsule_minor_version = 0;
165
static unsigned   capsule_minor_version = 0;
136
 
166
 
137
/*--------------------------------------------------------------------------*/
167
/*--------------------------------------------------------------------------*/
138
 
168
 
139
static void
169
static void
140
capsule_setup_defaults PROTO_Z ()
170
capsule_setup_defaults(void)
141
{
171
{
142
    if (capsule_unit_sets == NIL (UnitSetP)) {
172
    if (capsule_unit_sets == NIL(UnitSetP)) {
143
	unsigned i;
173
	unsigned i;
144
 
174
 
145
	for (i = 0; i < NUM_DEFAULT_UNIT_SETS; i ++) {
175
	for (i = 0; i < NUM_DEFAULT_UNIT_SETS; i++) {
146
	    nstring_copy_cstring (&(capsule_default_unit_sets [i].name),
176
	    nstring_copy_cstring(& (capsule_default_unit_sets[i].name),
147
				  capsule_default_unit_set_names [i]);
177
				  capsule_default_unit_set_names[i]);
148
	}
178
	}
149
	capsule_num_unit_sets = NUM_DEFAULT_UNIT_SETS;
179
	capsule_num_unit_sets = NUM_DEFAULT_UNIT_SETS;
150
	capsule_unit_sets     = capsule_default_unit_sets;
180
	capsule_unit_sets     = capsule_default_unit_sets;
151
    }
181
    }
152
}
182
}
153
 
183
 
154
static void
184
static void
155
capsule_setup PROTO_N ((units))
185
capsule_setup(UnitTableP  units)
156
	      PROTO_T (UnitTableP  units)
-
 
157
{
186
{
158
    static BoolT need_setup = TRUE;
187
    static BoolT need_setup = TRUE;
159
 
188
 
160
    if (need_setup) {
189
    if (need_setup) {
161
	unsigned i;
190
	unsigned i;
162
 
191
 
163
	capsule_setup_defaults ();
192
	capsule_setup_defaults();
164
	for (i = 0; i < capsule_num_unit_sets; i ++) {
193
	for (i = 0; i < capsule_num_unit_sets; i++) {
165
	    NStringP   name = &(capsule_unit_sets [i].name);
194
	    NStringP   name = & (capsule_unit_sets[i].name);
166
	    UnitEntryP entry;
195
	    UnitEntryP entry;
167
 
196
 
168
	    entry = unit_table_add (units, name, i);
197
	    entry = unit_table_add(units, name, i);
169
	    capsule_unit_sets [i].entry = entry;
198
	    capsule_unit_sets[i].entry = entry;
170
	    debug_info_u_name (name);
199
	    debug_info_u_name(name);
171
	}
200
	}
172
	need_setup = FALSE;
201
	need_setup = FALSE;
173
    }
202
    }
174
}
203
}
175
 
204
 
176
static BoolT
205
static BoolT
177
capsule_read_unit_set_name PROTO_N ((istream, dstring))
206
capsule_read_unit_set_name(IStreamP istream,				    DStringP dstring)
178
			   PROTO_T (IStreamP istream X
-
 
179
				    DStringP dstring)
-
 
180
{
207
{
181
    char c;
208
    char c;
182
 
209
 
183
    do {
210
    do {
184
	if (!istream_read_char (istream, &c)) {
211
	if (!istream_read_char(istream, &c)) {
185
	    return (FALSE);
212
	    return(FALSE);
186
	}
213
	}
187
    } while (syntax_is_white_space (c));
214
    } while (syntax_is_white_space(c));
188
    if (c != '"') {
215
    if (c != '"') {
189
	E_unit_set_expected_quote (istream);
216
	E_unit_set_expected_quote(istream);
190
	UNREACHED;
217
	UNREACHED;
191
    }
218
    }
192
    dstring_init (dstring);
219
    dstring_init(dstring);
193
    while (istream_read_char (istream, &c)) {
220
    while (istream_read_char(istream, &c)) {
194
	if (c == '"') {
221
	if (c == '"') {
195
	    return (TRUE);
222
	    return(TRUE);
196
	} else if (c == '\\') {
223
	} else if (c == '\\') {
197
	    switch (istream_read_escaped_char (istream, &c)) EXHAUSTIVE {
224
	    switch (istream_read_escaped_char(istream, &c))EXHAUSTIVE {
198
	      case ISTREAM_STAT_READ_CHAR:
225
	      case ISTREAM_STAT_READ_CHAR:
199
		dstring_append_char (dstring, c);
226
		dstring_append_char(dstring, c);
200
		break;
227
		break;
201
	      case ISTREAM_STAT_NO_CHAR:
228
	      case ISTREAM_STAT_NO_CHAR:
202
		break;
229
		break;
203
	      case ISTREAM_STAT_SYNTAX_ERROR:
230
	      case ISTREAM_STAT_SYNTAX_ERROR:
204
		E_unit_set_illegal_escape (istream);
231
		E_unit_set_illegal_escape(istream);
205
		UNREACHED;
232
		UNREACHED;
206
	    }
233
	    }
207
	} else {
234
	} else {
208
	    dstring_append_char (dstring, c);
235
	    dstring_append_char(dstring, c);
209
	}
236
	}
210
    }
237
    }
211
    E_unit_set_eof_in_name (istream);
238
    E_unit_set_eof_in_name(istream);
212
    UNREACHED;
239
    UNREACHED;
213
}
240
}
214
 
241
 
215
static void
242
static void
216
capsule_check_unit_sets PROTO_N ((istream))
243
capsule_check_unit_sets(IStreamP istream)
217
			PROTO_T (IStreamP istream)
-
 
218
{
244
{
219
    static BoolT    inited    = FALSE;
245
    static BoolT    inited    = FALSE;
220
    static NStringT tld;
246
    static NStringT tld;
221
    static NStringT tld2;
247
    static NStringT tld2;
222
    BoolT           tld_found = FALSE;
248
    BoolT           tld_found = FALSE;
223
    unsigned        i;
249
    unsigned        i;
224
 
250
 
225
    if (!inited) {
251
    if (!inited) {
226
	nstring_copy_cstring (&tld, "tld");
252
	nstring_copy_cstring(&tld, "tld");
227
	nstring_copy_cstring (&tld2, "tld2");
253
	nstring_copy_cstring(&tld2, "tld2");
228
	inited = TRUE;
254
	inited = TRUE;
229
    }
255
    }
230
    capsule_tld_index  = UINT_MAX;
256
    capsule_tld_index  = UINT_MAX;
231
    capsule_tld2_index = UINT_MAX;
257
    capsule_tld2_index = UINT_MAX;
232
    for (i = 0; i < capsule_num_unit_sets; i ++) {
258
    for (i = 0; i < capsule_num_unit_sets; i++) {
233
	NStringP name = &(capsule_unit_sets [i].name);
259
	NStringP name = & (capsule_unit_sets[i].name);
234
	unsigned j;
260
	unsigned j;
235
 
261
 
236
	for (j = 0; j < i; j ++) {
262
	for (j = 0; j < i; j++) {
237
	    if (nstring_equal (name, &(capsule_unit_sets [j].name))) {
263
	    if (nstring_equal(name, & (capsule_unit_sets[j].name))) {
238
		E_unit_set_duplicate_name (istream_name (istream), name);
264
		E_unit_set_duplicate_name(istream_name(istream), name);
239
		UNREACHED;
265
		UNREACHED;
240
	    }
266
	    }
241
	}
267
	}
242
	if (nstring_equal (name, &tld)) {
268
	if (nstring_equal(name, &tld)) {
243
	    capsule_tld_index = i;
269
	    capsule_tld_index = i;
244
	    tld_found         = TRUE;
270
	    tld_found         = TRUE;
245
	} else if (nstring_equal (name, &tld2)) {
271
	} else if (nstring_equal(name, &tld2)) {
246
	    capsule_tld2_index = i;
272
	    capsule_tld2_index = i;
247
	}
273
	}
248
    }
274
    }
249
    if (!tld_found) {
275
    if (!tld_found) {
250
	E_unit_set_no_tld_name (istream_name (istream));
276
	E_unit_set_no_tld_name(istream_name(istream));
251
	UNREACHED;
277
	UNREACHED;
252
    }
278
    }
253
}
279
}
254
 
280
 
255
static void
281
static void
256
capsule_read_unit_set_file_1 PROTO_N ((istream))
282
capsule_read_unit_set_file_1(IStreamP istream)
257
			     PROTO_T (IStreamP istream)
-
 
258
{
283
{
259
    UnitSetListEntryP  head          = NIL (UnitSetListEntryP);
284
    UnitSetListEntryP  head          = NIL(UnitSetListEntryP);
260
    UnitSetListEntryP *tail          = &head;
285
    UnitSetListEntryP *tail          = &head;
261
    unsigned           num_unit_sets = 0;
286
    unsigned           num_unit_sets = 0;
262
    UnitSetListEntryP  entry;
287
    UnitSetListEntryP  entry;
263
    UnitSetP           unit_sets;
288
    UnitSetP           unit_sets;
264
    unsigned           i;
289
    unsigned           i;
265
 
290
 
266
    for (;;) {
291
    for (;;) {
267
	DStringT dstring;
292
	DStringT dstring;
268
 
293
 
269
	if (!capsule_read_unit_set_name (istream, &dstring)) {
294
	if (!capsule_read_unit_set_name(istream, &dstring)) {
270
	    goto done;
295
	    goto done;
271
	}
296
	}
272
	entry       = ALLOCATE (UnitSetListEntryT);
297
	entry       = ALLOCATE(UnitSetListEntryT);
273
	entry->next = NIL (UnitSetListEntryP);
298
	entry->next = NIL(UnitSetListEntryP);
274
	dstring_to_nstring (&dstring, &(entry->name));
299
	dstring_to_nstring(&dstring, & (entry->name));
275
	*tail       = entry;
300
	*tail       = entry;
276
	tail        = &(entry->next);
301
	tail        = & (entry->next);
277
	dstring_destroy (&dstring);
302
	dstring_destroy(&dstring);
278
	num_unit_sets ++;
303
	num_unit_sets++;
279
    }
304
    }
280
  done:
305
  done:
281
    unit_sets = ALLOCATE_VECTOR (UnitSetT, num_unit_sets);
306
    unit_sets = ALLOCATE_VECTOR(UnitSetT, num_unit_sets);
282
    i         = 0;
307
    i         = 0;
283
    entry     = head;
308
    entry     = head;
284
    while (entry) {
309
    while (entry) {
285
	UnitSetListEntryP tmp = entry->next;
310
	UnitSetListEntryP tmp = entry->next;
286
 
311
 
287
	nstring_assign (&(unit_sets [i].name), &(entry->name));
312
	nstring_assign(& (unit_sets[i].name), & (entry->name));
288
	DEALLOCATE (entry);
313
	DEALLOCATE(entry);
289
	entry = tmp;
314
	entry = tmp;
290
	i ++;
315
	i++;
291
    }
316
    }
292
    capsule_num_unit_sets = num_unit_sets;
317
    capsule_num_unit_sets = num_unit_sets;
293
    capsule_unit_sets     = unit_sets;
318
    capsule_unit_sets     = unit_sets;
294
    capsule_check_unit_sets (istream);
319
    capsule_check_unit_sets(istream);
295
}
320
}
296
 
321
 
297
/*--------------------------------------------------------------------------*/
322
/*--------------------------------------------------------------------------*/
298
 
323
 
299
static TDFReaderP
324
static TDFReaderP
300
capsule_reader PROTO_N ((capsule))
325
capsule_reader(CapsuleP capsule)
301
	       PROTO_T (CapsuleP capsule)
-
 
302
{
326
{
303
    ASSERT (capsule->type == CT_INPUT);
327
    ASSERT(capsule->type == CT_INPUT);
304
    return (&(capsule->u.reader));
328
    return(& (capsule->u.reader));
305
}
329
}
306
 
330
 
307
static TDFWriterP
331
static TDFWriterP
308
capsule_writer PROTO_N ((capsule))
332
capsule_writer(CapsuleP capsule)
309
	       PROTO_T (CapsuleP capsule)
-
 
310
{
333
{
311
    ASSERT (capsule->type == CT_OUTPUT);
334
    ASSERT(capsule->type == CT_OUTPUT);
312
    return (&(capsule->u.writer));
335
    return(& (capsule->u.writer));
313
}
336
}
314
 
337
 
315
static NStringP
338
static NStringP
316
capsule_magic PROTO_Z ()
339
capsule_magic(void)
317
{
340
{
318
    static NStringT const_magic;
341
    static NStringT const_magic;
319
    static BoolT    inited = FALSE;
342
    static BoolT    inited = FALSE;
320
 
343
 
321
    if (!inited) {
344
    if (!inited) {
322
	nstring_copy_cstring (&const_magic, "TDFC");
345
	nstring_copy_cstring(&const_magic, "TDFC");
323
	inited = TRUE;
346
	inited = TRUE;
324
    }
347
    }
325
    return (&const_magic);
348
    return(&const_magic);
326
}
349
}
327
    
350
 
328
/*--------------------------------------------------------------------------*/
351
/*--------------------------------------------------------------------------*/
329
 
352
 
330
static void
353
static void
331
capsule_read_header PROTO_N ((capsule))
354
capsule_read_header(CapsuleP capsule)
332
    		    PROTO_T (CapsuleP capsule)
-
 
333
{
355
{
334
    TDFReaderP reader      = capsule_reader (capsule);
356
    TDFReaderP reader      = capsule_reader(capsule);
335
    NStringP   const_magic = capsule_magic ();
357
    NStringP   const_magic = capsule_magic();
336
    NStringT   magic;
358
    NStringT   magic;
337
    unsigned   major;
359
    unsigned   major;
338
    unsigned   minor;
360
    unsigned   minor;
339
 
361
 
340
    nstring_init_length (&magic, (unsigned) 4);
362
    nstring_init_length(&magic,(unsigned)4);
341
    tdf_read_bytes (reader, &magic);
363
    tdf_read_bytes(reader, &magic);
342
    if (!nstring_equal (&magic, const_magic)) {
364
    if (!nstring_equal(&magic, const_magic)) {
343
	E_capsule_bad_magic (capsule, &magic, const_magic);
365
	E_capsule_bad_magic(capsule, &magic, const_magic);
344
	THROW (XX_capsule_error);
366
	THROW(XX_capsule_error);
345
	UNREACHED;
367
	UNREACHED;
346
    }
368
    }
347
    nstring_destroy (&magic);
369
    nstring_destroy(&magic);
348
    major = tdf_read_int (reader);
370
    major = tdf_read_int(reader);
349
    minor = tdf_read_int (reader);
371
    minor = tdf_read_int(reader);
350
    debug_info_r_versions (major, minor);
372
    debug_info_r_versions(major, minor);
351
    if (major < 4) {
373
    if (major < 4) {
352
	E_capsule_bad_version (capsule, major);
374
	E_capsule_bad_version(capsule, major);
353
	THROW (XX_capsule_error);
375
	THROW(XX_capsule_error);
354
	UNREACHED;
376
	UNREACHED;
355
    } else if (capsule_major_version == 0) {
377
    } else if (capsule_major_version == 0) {
356
	capsule_major_version = major;
378
	capsule_major_version = major;
357
	capsule_minor_version = minor;
379
	capsule_minor_version = minor;
358
    } else if (capsule_major_version != major) {
380
    } else if (capsule_major_version != major) {
359
	E_capsule_version_mismatch (capsule, capsule_major_version, major);
381
	E_capsule_version_mismatch(capsule, capsule_major_version, major);
360
	THROW (XX_capsule_error);
382
	THROW(XX_capsule_error);
361
	UNREACHED;
383
	UNREACHED;
362
    } else if (capsule_minor_version < minor) {
384
    } else if (capsule_minor_version < minor) {
363
	capsule_minor_version = minor;
385
	capsule_minor_version = minor;
364
    }
386
    }
365
    tdf_read_align (reader);
387
    tdf_read_align(reader);
366
}
388
}
367
 
389
 
368
static UnitEntryP *
390
static UnitEntryP *
369
capsule_read_unit_set_names PROTO_N ((capsule, units, num_unit_sets_ref))
391
capsule_read_unit_set_names(CapsuleP   capsule,				     UnitTableP units, 
370
			    PROTO_T (CapsuleP   capsule X
-
 
371
				     UnitTableP units X
-
 
372
				     unsigned  *num_unit_sets_ref)
392
				     unsigned  *num_unit_sets_ref)
373
{
393
{
374
    TDFReaderP  reader        = capsule_reader (capsule);
394
    TDFReaderP  reader        = capsule_reader(capsule);
375
    unsigned    num_unit_sets = tdf_read_int (reader);
395
    unsigned    num_unit_sets = tdf_read_int(reader);
376
    UnitEntryP *units_vec     = ALLOCATE_VECTOR (UnitEntryP, num_unit_sets);
396
    UnitEntryP *units_vec     = ALLOCATE_VECTOR(UnitEntryP, num_unit_sets);
377
    UnitEntryP  tld_entry     = capsule_unit_sets [capsule_tld_index].entry;
397
    UnitEntryP  tld_entry     = capsule_unit_sets[capsule_tld_index].entry;
378
    UnitEntryP  tld2_entry    = ((capsule_tld2_index == UINT_MAX) ?
398
    UnitEntryP  tld2_entry    = ((capsule_tld2_index == UINT_MAX)?
379
				 NIL (UnitEntryP) :
399
				 NIL(UnitEntryP):
380
				 capsule_unit_sets [capsule_tld2_index].entry);
400
				 capsule_unit_sets[capsule_tld2_index].entry);
381
    BoolT       has_tld_unit  = FALSE;
401
    BoolT       has_tld_unit  = FALSE;
382
    unsigned    i;
402
    unsigned    i;
383
 
403
 
384
    debug_info_r_start_unit_decs (num_unit_sets);
404
    debug_info_r_start_unit_decs(num_unit_sets);
385
    for (i = 0; i < num_unit_sets; i ++) {
405
    for (i = 0; i < num_unit_sets; i++) {
386
	NStringT   nstring;
406
	NStringT   nstring;
387
	UnitEntryP entry;
407
	UnitEntryP entry;
388
 
408
 
389
	tdf_read_string (reader, &nstring);
409
	tdf_read_string(reader, &nstring);
390
	if ((entry = unit_table_get (units, &nstring)) != NIL (UnitEntryP)) {
410
	if ((entry = unit_table_get(units, &nstring)) != NIL(UnitEntryP)) {
391
	    unsigned order = unit_entry_order (entry);
411
	    unsigned order = unit_entry_order(entry);
392
	    unsigned j;
412
	    unsigned j;
393
 
413
 
394
	    for (j = 0; j < i; j ++) {
414
	    for (j = 0; j < i; j++) {
395
		if (entry == units_vec [j]) {
415
		if (entry == units_vec[j]) {
396
		    E_duplicate_unit_set_name (capsule, &nstring);
416
		    E_duplicate_unit_set_name(capsule, &nstring);
397
		    THROW (XX_capsule_error);
417
		    THROW(XX_capsule_error);
398
		    UNREACHED;
418
		    UNREACHED;
399
		} else if (order < unit_entry_order (units_vec [j])) {
419
		} else if (order < unit_entry_order(units_vec[j])) {
400
		    E_out_of_order_unit_set_name (capsule, &nstring);
420
		    E_out_of_order_unit_set_name(capsule, &nstring);
401
		    THROW (XX_capsule_error);
421
		    THROW(XX_capsule_error);
402
		    UNREACHED;
422
		    UNREACHED;
403
		}
423
		}
404
	    }
424
	    }
405
	    if (entry == tld2_entry) {
425
	    if (entry == tld2_entry) {
406
		E_tld2_unit_set_type_obsolete (capsule);
426
		E_tld2_unit_set_type_obsolete(capsule);
407
	    }
427
	    }
408
	    if ((entry == tld_entry) || (entry == tld2_entry)) {
428
	    if ((entry == tld_entry) || (entry == tld2_entry)) {
409
		if (has_tld_unit) {
429
		if (has_tld_unit) {
410
		    E_extra_tld_unit_set (capsule);
430
		    E_extra_tld_unit_set(capsule);
411
		    THROW (XX_capsule_error);
431
		    THROW(XX_capsule_error);
412
		    UNREACHED;
432
		    UNREACHED;
413
		}
433
		}
414
		has_tld_unit = TRUE;
434
		has_tld_unit = TRUE;
415
	    }
435
	    }
416
	    units_vec [i] = entry;
436
	    units_vec[i] = entry;
417
	} else {
437
	} else {
418
	    E_unknown_unit_set_name (capsule, &nstring);
438
	    E_unknown_unit_set_name(capsule, &nstring);
419
	    THROW (XX_capsule_error);
439
	    THROW(XX_capsule_error);
420
	    UNREACHED;
440
	    UNREACHED;
421
	}
441
	}
422
	debug_info_r_unit_dec (&nstring);
442
	debug_info_r_unit_dec(&nstring);
423
	nstring_destroy (&nstring);
443
	nstring_destroy(&nstring);
424
    }
444
    }
425
    if (!has_tld_unit) {
445
    if (!has_tld_unit) {
426
	E_missing_tld_unit_set (tdf_reader_name (reader));
446
	E_missing_tld_unit_set(tdf_reader_name(reader));
427
    }
447
    }
428
    *num_unit_sets_ref = num_unit_sets;
448
    *num_unit_sets_ref = num_unit_sets;
429
    return (units_vec);
449
    return(units_vec);
430
}
450
}
431
 
451
 
432
static ShapeDataP
452
static ShapeDataP
433
capsule_read_shapes PROTO_N ((capsule, shapes, num_shapes_ref))
453
capsule_read_shapes(CapsuleP    capsule,			     ShapeTableP shapes, 
434
		    PROTO_T (CapsuleP    capsule X
-
 
435
			     ShapeTableP shapes X
-
 
436
			     unsigned   *num_shapes_ref)
454
			     unsigned   *num_shapes_ref)
437
{
455
{
438
    TDFReaderP reader     = capsule_reader (capsule);
456
    TDFReaderP reader     = capsule_reader(capsule);
439
    unsigned   num_shapes = tdf_read_int (reader);
457
    unsigned   num_shapes = tdf_read_int(reader);
440
    ShapeDataP shapes_vec = ALLOCATE_VECTOR (ShapeDataT, num_shapes);
458
    ShapeDataP shapes_vec = ALLOCATE_VECTOR(ShapeDataT, num_shapes);
441
    unsigned   i;
459
    unsigned   i;
442
 
460
 
443
    debug_info_r_start_shapes (num_shapes);
461
    debug_info_r_start_shapes(num_shapes);
444
    for (i = 0; i < num_shapes; i ++) {
462
    for (i = 0; i < num_shapes; i++) {
445
	NStringT    nstring;
463
	NStringT    nstring;
446
	unsigned    num_ids;
464
	unsigned    num_ids;
447
	ShapeEntryP entry;
465
	ShapeEntryP entry;
448
	unsigned    j;
466
	unsigned    j;
449
 
467
 
450
	tdf_read_string (reader, &nstring);
468
	tdf_read_string(reader, &nstring);
451
	num_ids = tdf_read_int (reader);
469
	num_ids = tdf_read_int(reader);
452
	entry   = shape_table_add (shapes, &nstring);
470
	entry   = shape_table_add(shapes, &nstring);
453
	for (j = 0; j < i; j ++) {
471
	for (j = 0; j < i; j++) {
454
	    if (entry == shapes_vec [j].entry) {
472
	    if (entry == shapes_vec[j].entry) {
455
		E_duplicate_shape_name (capsule, &nstring);
473
		E_duplicate_shape_name(capsule, &nstring);
456
		THROW (XX_capsule_error);
474
		THROW(XX_capsule_error);
457
		UNREACHED;
475
		UNREACHED;
458
	    }
476
	    }
459
	}
477
	}
460
	debug_info_r_shape (&nstring, num_ids);
478
	debug_info_r_shape(&nstring, num_ids);
461
	nstring_destroy (&nstring);
479
	nstring_destroy(&nstring);
462
	shapes_vec [i].entry   = entry;
480
	shapes_vec[i].entry   = entry;
463
	shapes_vec [i].num_ids = num_ids;
481
	shapes_vec[i].num_ids = num_ids;
464
	shapes_vec [i].id_maps = ALLOCATE_VECTOR (unsigned, num_ids);
482
	shapes_vec[i].id_maps = ALLOCATE_VECTOR(unsigned, num_ids);
465
	for (j = 0; j < num_ids; j ++) {
483
	for (j = 0; j < num_ids; j++) {
466
	    shapes_vec [i].id_maps [j] = UINT_MAX;
484
	    shapes_vec[i].id_maps[j] = UINT_MAX;
467
	}
485
	}
468
    }
486
    }
469
    *num_shapes_ref = num_shapes;
487
    *num_shapes_ref = num_shapes;
470
    return (shapes_vec);
488
    return(shapes_vec);
471
}
489
}
472
 
490
 
473
static NameEntryP *
491
static NameEntryP *
474
capsule_read_external_names_1 PROTO_N ((capsule, shape, num_ref))
492
capsule_read_external_names_1(CapsuleP   capsule,				       ShapeDataP shape, 
475
			      PROTO_T (CapsuleP   capsule X
-
 
476
				       ShapeDataP shape X
-
 
477
				       unsigned  *num_ref)
493
				       unsigned  *num_ref)
478
{
494
{
479
    TDFReaderP  reader         = capsule_reader (capsule);
495
    TDFReaderP  reader         = capsule_reader(capsule);
480
    unsigned    num_this_shape = tdf_read_int (reader);
496
    unsigned    num_this_shape = tdf_read_int(reader);
481
    ShapeEntryP entry          = shape->entry;
497
    ShapeEntryP entry          = shape->entry;
482
    NStringP    key            = shape_entry_key (entry);
498
    NStringP    key            = shape_entry_key(entry);
483
    NameTableP  table          = shape_entry_name_table (entry);
499
    NameTableP  table          = shape_entry_name_table(entry);
484
    unsigned    num_ids        = shape->num_ids;
500
    unsigned    num_ids        = shape->num_ids;
485
    unsigned   *id_maps        = shape->id_maps;
501
    unsigned   *id_maps        = shape->id_maps;
486
    NameEntryP *names_vec      = ALLOCATE_VECTOR (NameEntryP, num_this_shape);
502
    NameEntryP *names_vec      = ALLOCATE_VECTOR(NameEntryP, num_this_shape);
487
    unsigned    i;
503
    unsigned    i;
488
 
504
 
489
    debug_info_r_start_shape_names (key, num_this_shape);
505
    debug_info_r_start_shape_names(key, num_this_shape);
490
    for (i = 0; i < num_this_shape; i ++) {
506
    for (i = 0; i < num_this_shape; i++) {
491
	unsigned   id = tdf_read_int (reader);
507
	unsigned   id = tdf_read_int(reader);
492
	NameKeyT   name;
508
	NameKeyT   name;
493
	NameEntryP name_entry;
509
	NameEntryP name_entry;
494
 
510
 
495
	tdf_read_name (reader, &name);
511
	tdf_read_name(reader, &name);
496
	if (id >= num_ids) {
512
	if (id >= num_ids) {
497
	    E_name_id_out_of_range (capsule, key, &name, id, num_ids);
513
	    E_name_id_out_of_range(capsule, key, &name, id, num_ids);
498
	    THROW (XX_capsule_error);
514
	    THROW(XX_capsule_error);
499
	    UNREACHED;
515
	    UNREACHED;
500
	}
516
	}
501
	name_entry = name_table_add (table, &name, entry);
517
	name_entry = name_table_add(table, &name, entry);
502
	if (id_maps [id] != UINT_MAX) {
518
	if (id_maps[id]!= UINT_MAX) {
503
	    E_name_id_used_multiple_times (capsule, key, &name, id);
519
	    E_name_id_used_multiple_times(capsule, key, &name, id);
504
	    THROW (XX_capsule_error);
520
	    THROW(XX_capsule_error);
505
	    UNREACHED;
521
	    UNREACHED;
506
	}
522
	}
507
	names_vec [i] = name_entry;
523
	names_vec[i] = name_entry;
508
	id_maps [id]  = name_entry_id (name_entry);
524
	id_maps[id] = name_entry_id(name_entry);
509
	debug_info_r_name (&name, id, id_maps [id],
525
	debug_info_r_name(&name, id, id_maps[id],
510
			   name_entry_key (name_entry));
526
			   name_entry_key(name_entry));
511
	name_key_destroy (&name);
527
	name_key_destroy(&name);
512
    }
528
    }
513
    *num_ref = num_this_shape;
529
    *num_ref = num_this_shape;
514
    return (names_vec);
530
    return(names_vec);
515
}
531
}
516
 
532
 
517
static NameDataP
533
static NameDataP
518
capsule_read_external_names PROTO_N ((capsule, num_shapes, shapes_vec))
534
capsule_read_external_names(CapsuleP   capsule,				     unsigned   num_shapes, 
519
			    PROTO_T (CapsuleP   capsule X
-
 
520
				     unsigned   num_shapes X
-
 
521
				     ShapeDataP shapes_vec)
535
				     ShapeDataP shapes_vec)
522
{
536
{
523
    TDFReaderP reader = capsule_reader (capsule);
537
    TDFReaderP reader = capsule_reader(capsule);
524
    NameDataT *names_vec_vec;
538
    NameDataT *names_vec_vec;
525
    unsigned   num_names;
539
    unsigned   num_names;
526
    unsigned   i;
540
    unsigned   i;
527
 
541
 
528
    if ((num_names = tdf_read_int (reader)) != num_shapes) {
542
    if ((num_names = tdf_read_int(reader)) != num_shapes) {
529
	E_shape_and_name_count_mismatch (capsule, num_shapes, num_names);
543
	E_shape_and_name_count_mismatch(capsule, num_shapes, num_names);
530
	THROW (XX_capsule_error);
544
	THROW(XX_capsule_error);
531
	UNREACHED;
545
	UNREACHED;
532
    }
546
    }
533
    debug_info_r_start_names (num_names);
547
    debug_info_r_start_names(num_names);
534
    names_vec_vec = ALLOCATE_VECTOR (NameDataT, num_names);
548
    names_vec_vec = ALLOCATE_VECTOR(NameDataT, num_names);
535
    for (i = 0; i < num_names; i ++) {
549
    for (i = 0; i < num_names; i++) {
536
	ShapeDataP  shape = &(shapes_vec [i]);
550
	ShapeDataP  shape = & (shapes_vec[i]);
537
	NameEntryP *names_vec;
551
	NameEntryP *names_vec;
538
	unsigned    num_this_shape;
552
	unsigned    num_this_shape;
539
 
553
 
540
	names_vec = capsule_read_external_names_1 (capsule, shape,
554
	names_vec = capsule_read_external_names_1(capsule, shape,
541
						   &num_this_shape);
555
						   &num_this_shape);
542
	names_vec_vec [i].names_vec = names_vec;
556
	names_vec_vec[i].names_vec = names_vec;
543
	names_vec_vec [i].num_names = num_this_shape;
557
	names_vec_vec[i].num_names = num_this_shape;
544
    }
558
    }
545
    return (names_vec_vec);
559
    return(names_vec_vec);
546
}
560
}
547
 
561
 
548
/*--------------------------------------------------------------------------*/
562
/*--------------------------------------------------------------------------*/
549
 
563
 
550
static unsigned
564
static unsigned
551
capsule_get_token_index PROTO_N ((shapes, num_shapes, shapes_vec))
565
capsule_get_token_index(ShapeTableP shapes,				 unsigned    num_shapes, 
552
			PROTO_T (ShapeTableP shapes X
-
 
553
				 unsigned    num_shapes X
-
 
554
				 ShapeDataP  shapes_vec)
566
				 ShapeDataP  shapes_vec)
555
{
567
{
556
    ShapeEntryP token_entry = shape_table_get_token_entry (shapes);
568
    ShapeEntryP token_entry = shape_table_get_token_entry(shapes);
557
    unsigned    i;
569
    unsigned    i;
558
 
570
 
559
    for (i = 0; i < num_shapes; i ++) {
571
    for (i = 0; i < num_shapes; i++) {
560
	if (shapes_vec [i].entry == token_entry) {
572
	if (shapes_vec[i].entry == token_entry) {
561
	    return (i);
573
	    return(i);
562
	}
574
	}
563
    }
575
    }
564
    return (UINT_MAX);
576
    return(UINT_MAX);
565
}
577
}
566
 
578
 
567
static unsigned
579
static unsigned
568
capsule_get_tag_index PROTO_N ((shapes, num_shapes, shapes_vec))
580
capsule_get_tag_index(ShapeTableP shapes,			       unsigned    num_shapes, 
569
		      PROTO_T (ShapeTableP shapes X
-
 
570
			       unsigned    num_shapes X
-
 
571
			       ShapeDataP  shapes_vec)
581
			       ShapeDataP  shapes_vec)
572
{
582
{
573
    ShapeEntryP tag_entry = shape_table_get_tag_entry (shapes);
583
    ShapeEntryP tag_entry = shape_table_get_tag_entry(shapes);
574
    unsigned    i;
584
    unsigned    i;
575
 
585
 
576
    for (i = 0; i < num_shapes; i ++) {
586
    for (i = 0; i < num_shapes; i++) {
577
	if (shapes_vec [i].entry == tag_entry) {
587
	if (shapes_vec[i].entry == tag_entry) {
578
	    return (i);
588
	    return(i);
579
	}
589
	}
580
    }
590
    }
581
    return (UINT_MAX);
591
    return(UINT_MAX);
582
}
592
}
583
 
593
 
584
static void
594
static void
585
capsule_read_usage PROTO_N ((capsule, entry, need_dec, no_mult, shape_key))
595
capsule_read_usage(CapsuleP  capsule,			    NameDataP entry, 
586
		   PROTO_T (CapsuleP  capsule X
-
 
587
			    NameDataP entry X
-
 
588
			    BoolT     need_dec X
596
			    BoolT     need_dec, 
589
			    BoolT     no_mult X
597
			    BoolT     no_mult, 
590
			    NStringP  shape_key)
598
			    NStringP  shape_key)
591
{
599
{
592
    TDFReaderP  reader    = capsule_reader (capsule);
600
    TDFReaderP  reader    = capsule_reader(capsule);
593
    unsigned    num_names = entry->num_names;
601
    unsigned    num_names = entry->num_names;
594
    NameEntryP *names_vec = entry->names_vec;
602
    NameEntryP *names_vec = entry->names_vec;
595
    unsigned    i;
603
    unsigned    i;
596
 
604
 
597
    debug_info_r_start_usages (shape_key, num_names);
605
    debug_info_r_start_usages(shape_key, num_names);
598
    for (i = 0; i < num_names; i ++) {
606
    for (i = 0; i < num_names; i++) {
599
	unsigned   use        = tdf_read_int (reader);
607
	unsigned   use        = tdf_read_int(reader);
600
	NameEntryP name_entry = names_vec [i];
608
	NameEntryP name_entry = names_vec[i];
601
	unsigned   name_use   = name_entry_get_use (name_entry);
609
	unsigned   name_use   = name_entry_get_use(name_entry);
602
	NameKeyP   key        = name_entry_key (name_entry);
610
	NameKeyP   key        = name_entry_key(name_entry);
603
 
611
 
604
	if (use & ~(U_USED | U_DECD | U_DEFD | U_MULT)) {
612
	if (use & ~(U_USED | U_DECD | U_DEFD | U_MULT)) {
605
	    E_bad_usage (capsule, shape_key, key, use);
613
	    E_bad_usage(capsule, shape_key, key, use);
606
	    THROW (XX_capsule_error);
614
	    THROW(XX_capsule_error);
607
	    UNREACHED;
615
	    UNREACHED;
608
	} else if (no_mult && (use & U_MULT)) {
616
	} else if (no_mult && (use & U_MULT)) {
609
	    E_illegally_multiply_defined (capsule, shape_key, key);
617
	    E_illegally_multiply_defined(capsule, shape_key, key);
610
	    THROW (XX_capsule_error);
618
	    THROW(XX_capsule_error);
611
	    UNREACHED;
619
	    UNREACHED;
612
	} else if (need_dec &&
620
	} else if (need_dec &&
613
		   (((use & (U_DEFD | U_DECD)) == U_DEFD) ||
621
		  (((use & (U_DEFD | U_DECD)) == U_DEFD) ||
614
		    ((use & (U_MULT | U_DECD)) == U_MULT))) {
622
		   ((use & (U_MULT | U_DECD)) == U_MULT))) {
615
	    E_defined_but_not_declared (capsule, shape_key, key);
623
	    E_defined_but_not_declared(capsule, shape_key, key);
616
	    THROW (XX_capsule_error);
624
	    THROW(XX_capsule_error);
617
	    UNREACHED;
625
	    UNREACHED;
618
	}
626
	}
619
	if ((use & U_DEFD) && (name_use & U_DEFD)) {
627
	if ((use & U_DEFD) && (name_use & U_DEFD)) {
620
	    CapsuleP definition = name_entry_get_definition (name_entry);
628
	    CapsuleP definition = name_entry_get_definition(name_entry);
621
	    CStringP prev_name  = capsule_name (definition);
629
	    CStringP prev_name  = capsule_name(definition);
622
 
630
 
623
	    E_multiply_defined (capsule, shape_key, key, prev_name);
631
	    E_multiply_defined(capsule, shape_key, key, prev_name);
624
	} else if ((use & U_MULT) && (name_use & U_MULT) &&
632
	} else if ((use & U_MULT) && (name_use & U_MULT) &&
625
		   (!(use & U_DEFD)) && (!(name_use & U_DEFD))) {
633
		  (!(use & U_DEFD)) && (!(name_use & U_DEFD))) {
626
	    name_entry_set_definition (name_entry, NIL (CapsuleP));
634
	    name_entry_set_definition(name_entry, NIL(CapsuleP));
627
	} else if ((use & U_DEFD) ||
635
	} else if ((use & U_DEFD) ||
628
		   ((use & U_MULT) && (!(name_use & (U_MULT | U_DEFD))))) {
636
		  ((use & U_MULT) && (!(name_use & (U_MULT | U_DEFD))))) {
629
	    name_entry_set_definition (name_entry, capsule);
637
	    name_entry_set_definition(name_entry, capsule);
630
	}
638
	}
631
	debug_info_r_usage (use, name_use, key);
639
	debug_info_r_usage(use, name_use, key);
632
	name_entry_merge_use (name_entry, use);
640
	name_entry_merge_use(name_entry, use);
633
    }
641
    }
634
}
642
}
635
 
643
 
636
static void
644
static void
637
capsule_read_tld_type_0_unit PROTO_N ((capsule, shapes, num_shapes, shapes_vec,
645
capsule_read_tld_type_0_unit(CapsuleP capsule, ShapeTableP shapes,
638
				       names_vec_vec))
-
 
639
			     PROTO_T (CapsuleP    capsule X
-
 
640
				      ShapeTableP shapes X
-
 
641
				      unsigned    num_shapes X
-
 
642
				      ShapeDataP  shapes_vec X
646
			     unsigned num_shapes, ShapeDataP shapes_vec,
643
				      NameDataP   names_vec_vec)
647
			     NameDataP names_vec_vec)
644
{
648
{
645
    unsigned i;
649
    unsigned i;
646
 
650
 
647
    i = capsule_get_token_index (shapes, num_shapes, shapes_vec);
651
    i = capsule_get_token_index(shapes, num_shapes, shapes_vec);
648
    if (i != UINT_MAX) {
652
    if (i != UINT_MAX) {
649
	NStringP key = shape_entry_key (shapes_vec [i].entry);
653
	NStringP key = shape_entry_key(shapes_vec[i].entry);
650
 
654
 
651
	capsule_read_usage (capsule, &(names_vec_vec [i]), FALSE, TRUE, key);
655
	capsule_read_usage(capsule, & (names_vec_vec[i]), FALSE, TRUE, key);
652
    }
656
    }
653
    i = capsule_get_tag_index (shapes, num_shapes, shapes_vec);
657
    i = capsule_get_tag_index(shapes, num_shapes, shapes_vec);
654
    if (i != UINT_MAX) {
658
    if (i != UINT_MAX) {
655
	NStringP key = shape_entry_key (shapes_vec [i].entry);
659
	NStringP key = shape_entry_key(shapes_vec[i].entry);
656
 
660
 
657
	capsule_read_usage (capsule, &(names_vec_vec [i]), TRUE, FALSE, key);
661
	capsule_read_usage(capsule, & (names_vec_vec[i]), TRUE, FALSE, key);
658
    }
662
    }
659
}
663
}
660
 
664
 
661
static void
665
static void
662
capsule_read_tld_type_1_unit PROTO_N ((capsule, shapes, num_shapes, shapes_vec,
666
capsule_read_tld_type_1_unit(CapsuleP capsule, ShapeTableP shapes,
663
				       names_vec_vec))
-
 
664
			     PROTO_T (CapsuleP    capsule X
-
 
665
				      ShapeTableP shapes X
-
 
666
				      unsigned    num_shapes X
-
 
667
				      ShapeDataP  shapes_vec X
667
			     unsigned num_shapes, ShapeDataP shapes_vec,
668
				      NameDataP   names_vec_vec)
668
			     NameDataP names_vec_vec)
669
{
669
{
670
    unsigned i;
670
    unsigned i;
671
    unsigned token = capsule_get_token_index (shapes, num_shapes, shapes_vec);
671
    unsigned token = capsule_get_token_index(shapes, num_shapes, shapes_vec);
672
    unsigned tag   = capsule_get_tag_index (shapes, num_shapes, shapes_vec);
672
    unsigned tag   = capsule_get_tag_index(shapes, num_shapes, shapes_vec);
673
 
673
 
674
    for (i = 0; i < num_shapes; i ++) {
674
    for (i = 0; i < num_shapes; i++) {
675
	NStringP key = shape_entry_key (shapes_vec [i].entry);
675
	NStringP key = shape_entry_key(shapes_vec[i].entry);
676
 
676
 
677
	capsule_read_usage (capsule, &(names_vec_vec [i]), i == tag,
677
	capsule_read_usage(capsule, & (names_vec_vec[i]), i == tag,
678
			    i == token, key);
678
			    i == token, key);
679
    }
679
    }
680
}
680
}
681
 
681
 
682
/*--------------------------------------------------------------------------*/
682
/*--------------------------------------------------------------------------*/
683
 
683
 
684
typedef void (*UnitTypeProcP)
684
typedef void(*UnitTypeProcP)
685
	PROTO_S ((CapsuleP, ShapeTableP, unsigned, ShapeDataP, NameDataP));
685
(CapsuleP, ShapeTableP, unsigned, ShapeDataP, NameDataP);
686
 
686
 
687
static UnitTypeProcP capsule_type_jump_table [] = {
687
static UnitTypeProcP capsule_type_jump_table[] = {
688
    capsule_read_tld_type_0_unit,
688
    capsule_read_tld_type_0_unit,
689
    capsule_read_tld_type_1_unit
689
    capsule_read_tld_type_1_unit
690
};
690
};
691
 
691
 
692
#define CAPSULE_TYPE_JUMP_TABLE_SIZE \
692
#define CAPSULE_TYPE_JUMP_TABLE_SIZE \
693
	((unsigned) (sizeof (capsule_type_jump_table) / \
693
	((unsigned)(sizeof(capsule_type_jump_table) / \
694
		     sizeof (UnitTypeProcP)))
694
		     sizeof(UnitTypeProcP)))
695
 
695
 
696
/*--------------------------------------------------------------------------*/
696
/*--------------------------------------------------------------------------*/
697
 
697
 
698
static void
698
static void
699
capsule_read_tld_unit_header PROTO_N ((capsule, unit_set))
699
capsule_read_tld_unit_header(CapsuleP capsule,				      NStringP unit_set)
700
			     PROTO_T (CapsuleP capsule X
-
 
701
				      NStringP unit_set)
-
 
702
{
700
{
703
    TDFReaderP reader = capsule_reader (capsule);
701
    TDFReaderP reader = capsule_reader(capsule);
704
 
702
 
705
    if (tdf_read_int (reader) != 1) {
703
    if (tdf_read_int(reader) != 1) {
706
	E_too_many_tld_units (capsule);
704
	E_too_many_tld_units(capsule);
707
	THROW (XX_capsule_error);
705
	THROW(XX_capsule_error);
708
	UNREACHED;
706
	UNREACHED;
709
    }
707
    }
710
    debug_info_r_start_units (unit_set, (unsigned) 1);
708
    debug_info_r_start_units(unit_set,(unsigned)1);
711
    debug_info_r_start_unit (unit_set, (unsigned) 1, (unsigned) 1);
709
    debug_info_r_start_unit(unit_set,(unsigned)1,(unsigned)1);
712
    if (tdf_read_int (reader) != 0) {
710
    if (tdf_read_int(reader) != 0) {
713
	E_too_many_tld_unit_counts (capsule);
711
	E_too_many_tld_unit_counts(capsule);
714
	THROW (XX_capsule_error);
712
	THROW(XX_capsule_error);
715
	UNREACHED;
713
	UNREACHED;
716
    }
714
    }
717
    debug_info_r_start_counts ((unsigned) 0);
715
    debug_info_r_start_counts((unsigned)0);
718
    if (tdf_read_int (reader) != 0) {
716
    if (tdf_read_int(reader) != 0) {
719
	E_too_many_tld_unit_mappings (capsule);
717
	E_too_many_tld_unit_mappings(capsule);
720
	THROW (XX_capsule_error);
718
	THROW(XX_capsule_error);
721
	UNREACHED;
719
	UNREACHED;
722
    }
720
    }
723
    debug_info_r_start_maps ((unsigned) 0);
721
    debug_info_r_start_maps((unsigned)0);
724
    capsule_unit_length = tdf_read_int (reader);
722
    capsule_unit_length = tdf_read_int(reader);
725
    debug_info_r_unit_body (capsule_unit_length);
723
    debug_info_r_unit_body(capsule_unit_length);
726
    tdf_read_align (reader);
724
    tdf_read_align(reader);
727
    capsule_unit_offset = tdf_reader_byte (reader);
725
    capsule_unit_offset = tdf_reader_byte(reader);
728
}
726
}
729
 
727
 
730
static void
728
static void
731
capsule_read_tld_unit_trailer PROTO_N ((capsule))
729
capsule_read_tld_unit_trailer(CapsuleP capsule)
732
			      PROTO_T (CapsuleP capsule)
-
 
733
{
730
{
734
    TDFReaderP reader  = capsule_reader (capsule);
731
    TDFReaderP reader  = capsule_reader(capsule);
735
    unsigned   offset  = tdf_reader_byte (reader);
732
    unsigned   offset  = tdf_reader_byte(reader);
736
    unsigned   correct = (capsule_unit_offset + capsule_unit_length);
733
    unsigned   correct = (capsule_unit_offset + capsule_unit_length);
737
 
734
 
738
    tdf_read_align (reader);
735
    tdf_read_align(reader);
739
    if (correct != offset) {
736
    if (correct != offset) {
740
	E_tld_unit_wrong_size (capsule, correct, offset);
737
	E_tld_unit_wrong_size(capsule, correct, offset);
741
	THROW (XX_capsule_error);
738
	THROW(XX_capsule_error);
742
	UNREACHED;
739
	UNREACHED;
743
    }
740
    }
744
}
741
}
745
 
742
 
746
static void
743
static void
747
capsule_read_tld2_units PROTO_N ((capsule, shapes, num_shapes, shapes_vec,
744
capsule_read_tld2_units(CapsuleP capsule, ShapeTableP shapes,
748
				  names_vec_vec))
-
 
749
			PROTO_T (CapsuleP    capsule X
-
 
750
				 ShapeTableP shapes X
-
 
751
				 unsigned    num_shapes X
-
 
752
				 ShapeDataP  shapes_vec X
745
			unsigned num_shapes, ShapeDataP shapes_vec,
753
				 NameDataP   names_vec_vec)
746
			NameDataP names_vec_vec)
754
{
747
{
755
    UnitEntryP tld2_entry = capsule_unit_sets [capsule_tld2_index].entry;
748
    UnitEntryP tld2_entry = capsule_unit_sets[capsule_tld2_index].entry;
756
    NStringP   key        = unit_entry_key (tld2_entry);
749
    NStringP   key        = unit_entry_key(tld2_entry);
757
 
750
 
758
    ASSERT (capsule_tld2_index != UINT_MAX);
751
    ASSERT(capsule_tld2_index != UINT_MAX);
759
    capsule_read_tld_unit_header (capsule, key);
752
    capsule_read_tld_unit_header(capsule, key);
760
    debug_info_r_tld_version ((unsigned) 0);
753
    debug_info_r_tld_version((unsigned)0);
761
    capsule_read_tld_type_0_unit (capsule, shapes, num_shapes, shapes_vec,
754
    capsule_read_tld_type_0_unit(capsule, shapes, num_shapes, shapes_vec,
762
				  names_vec_vec);
755
				  names_vec_vec);
763
    capsule_read_tld_unit_trailer (capsule);
756
    capsule_read_tld_unit_trailer(capsule);
764
}
757
}
765
 
758
 
766
static void
759
static void
767
capsule_read_tld_units PROTO_N ((capsule, shapes, num_shapes, shapes_vec,
760
capsule_read_tld_units(CapsuleP capsule, ShapeTableP shapes,
768
				 names_vec_vec))
-
 
769
		       PROTO_T (CapsuleP    capsule X
761
		       unsigned num_shapes, ShapeDataP shapes_vec,
770
				ShapeTableP shapes X
-
 
771
				unsigned    num_shapes X
-
 
772
				ShapeDataP  shapes_vec X
-
 
773
				NameDataP   names_vec_vec)
762
		       NameDataP names_vec_vec)
774
{
763
{
775
    TDFReaderP reader    = capsule_reader (capsule);
764
    TDFReaderP reader    = capsule_reader(capsule);
776
    UnitEntryP tld_entry = capsule_unit_sets [capsule_tld_index].entry;
765
    UnitEntryP tld_entry = capsule_unit_sets[capsule_tld_index].entry;
777
    NStringP   key       = unit_entry_key (tld_entry);
766
    NStringP   key       = unit_entry_key(tld_entry);
778
    unsigned   unit_type;
767
    unsigned   unit_type;
779
 
768
 
780
    capsule_read_tld_unit_header (capsule, key);
769
    capsule_read_tld_unit_header(capsule, key);
781
    unit_type = tdf_read_int (reader);
770
    unit_type = tdf_read_int(reader);
782
    if (unit_type >= CAPSULE_TYPE_JUMP_TABLE_SIZE) {
771
    if (unit_type >= CAPSULE_TYPE_JUMP_TABLE_SIZE) {
783
	E_unknown_tld_unit_type (capsule, unit_type);
772
	E_unknown_tld_unit_type(capsule, unit_type);
784
	THROW (XX_capsule_error);
773
	THROW(XX_capsule_error);
785
	UNREACHED;
774
	UNREACHED;
786
    }
775
    }
787
    debug_info_r_tld_version (unit_type);
776
    debug_info_r_tld_version(unit_type);
788
    (*(capsule_type_jump_table [unit_type])) (capsule, shapes, num_shapes,
777
   (*(capsule_type_jump_table[unit_type]))(capsule, shapes, num_shapes,
789
					      shapes_vec, names_vec_vec);
778
					      shapes_vec, names_vec_vec);
790
    capsule_read_tld_unit_trailer (capsule);
779
    capsule_read_tld_unit_trailer(capsule);
791
}
780
}
792
 
781
 
793
static MapEntryP *
782
static MapEntryP *
794
capsule_read_unit_counts PROTO_N ((capsule, num_shapes, shapes_vec, num_counts,
783
capsule_read_unit_counts(CapsuleP capsule, unsigned num_shapes,
795
				   unit_entry, unit, unit_num))
-
 
796
			 PROTO_T (CapsuleP   capsule X
-
 
797
				  unsigned   num_shapes X
-
 
798
				  ShapeDataP shapes_vec X
784
			 ShapeDataP shapes_vec, unsigned num_counts,
799
				  unsigned   num_counts X
-
 
800
				  UnitEntryP unit_entry X
785
			 UnitEntryP unit_entry, UnitP unit, unsigned unit_num)
801
				  UnitP      unit X
-
 
802
				  unsigned   unit_num)
-
 
803
{
786
{
804
    if ((num_counts != 0) && (num_counts != num_shapes)) {
787
    if ((num_counts != 0) && (num_counts != num_shapes)) {
805
	E_unit_count_num_mismatch (capsule, num_counts, num_shapes, unit_num,
788
	E_unit_count_num_mismatch(capsule, num_counts, num_shapes, unit_num,
806
				   unit_entry_key (unit_entry));
789
				   unit_entry_key(unit_entry));
807
	THROW (XX_capsule_error);
790
	THROW(XX_capsule_error);
808
	UNREACHED;
791
	UNREACHED;
809
    }
792
    }
810
    debug_info_r_start_counts (num_counts);
793
    debug_info_r_start_counts(num_counts);
811
    if (num_counts != 0) {
794
    if (num_counts != 0) {
812
	TDFReaderP reader  = capsule_reader (capsule);
795
	TDFReaderP reader  = capsule_reader(capsule);
813
	MapTableP  table   = unit_map_table (unit);
796
	MapTableP  table   = unit_map_table(unit);
814
	MapEntryP *entries = ALLOCATE_VECTOR (MapEntryP, num_counts);
797
	MapEntryP *entries = ALLOCATE_VECTOR(MapEntryP, num_counts);
815
	unsigned   i;
798
	unsigned   i;
816
 
799
 
817
	for (i = 0; i < num_counts; i ++) {
800
	for (i = 0; i < num_counts; i++) {
818
	    unsigned  count = tdf_read_int (reader);
801
	    unsigned  count = tdf_read_int(reader);
819
	    NStringP  key   = shape_entry_key (shapes_vec [i].entry);
802
	    NStringP  key   = shape_entry_key(shapes_vec[i].entry);
820
	    MapEntryP entry = map_table_add (table, key, count);
803
	    MapEntryP entry = map_table_add(table, key, count);
821
 
804
 
822
	    debug_info_r_count (count, key);
805
	    debug_info_r_count(count, key);
823
	    entries [i] = entry;
806
	    entries[i] = entry;
824
	}
807
	}
825
	return (entries);
808
	return(entries);
826
    } else {
809
    } else {
827
	return (NIL (MapEntryP *));
810
	return(NIL(MapEntryP *));
828
    }
811
    }
829
}
812
}
830
 
813
 
831
static void
814
static void
832
capsule_read_unit_maps PROTO_N ((capsule, num_counts, shapes_vec, unit_entry,
815
capsule_read_unit_maps(CapsuleP capsule, unsigned num_counts,
833
				 unit_num, entries))
-
 
834
		       PROTO_T (CapsuleP   capsule X
-
 
835
				unsigned   num_counts X
-
 
836
				ShapeDataP shapes_vec X
816
		       ShapeDataP shapes_vec, UnitEntryP unit_entry,
837
				UnitEntryP unit_entry X
-
 
838
				unsigned   unit_num X
-
 
839
				MapEntryP *entries)
817
		       unsigned unit_num, MapEntryP *entries)
840
{
818
{
841
    TDFReaderP reader          = capsule_reader (capsule);
819
    TDFReaderP reader          = capsule_reader(capsule);
842
    unsigned   num_link_shapes = tdf_read_int (reader);
820
    unsigned   num_link_shapes = tdf_read_int(reader);
843
    unsigned   i;
821
    unsigned   i;
844
 
822
 
845
    if (num_link_shapes != num_counts) {
823
    if (num_link_shapes != num_counts) {
846
	E_unit_mapping_num_mismatch (capsule, num_link_shapes, num_counts,
824
	E_unit_mapping_num_mismatch(capsule, num_link_shapes, num_counts,
847
				     unit_num, unit_entry_key (unit_entry));
825
				     unit_num, unit_entry_key(unit_entry));
848
	THROW (XX_capsule_error);
826
	THROW(XX_capsule_error);
849
	UNREACHED;
827
	UNREACHED;
850
    }
828
    }
851
    debug_info_r_start_maps (num_link_shapes);
829
    debug_info_r_start_maps(num_link_shapes);
852
    for (i = 0; i < num_link_shapes; i ++) {
830
    for (i = 0; i < num_link_shapes; i++) {
853
	unsigned    num_links   = tdf_read_int (reader);
831
	unsigned    num_links   = tdf_read_int(reader);
854
	ShapeEntryP shape_entry = (shapes_vec [i].entry);
832
	ShapeEntryP shape_entry = (shapes_vec[i].entry);
855
	NStringP    key         = shape_entry_key (shape_entry);
833
	NStringP    key         = shape_entry_key(shape_entry);
856
	unsigned    j;
834
	unsigned    j;
857
 
835
 
858
	map_entry_set_num_links (entries [i], num_links);
836
	map_entry_set_num_links(entries[i], num_links);
859
	debug_info_r_start_shape_maps (key, num_links);
837
	debug_info_r_start_shape_maps(key, num_links);
860
	for (j = 0; j < num_links; j ++) {
838
	for (j = 0; j < num_links; j++) {
861
	    unsigned  internal = tdf_read_int (reader);
839
	    unsigned  internal = tdf_read_int(reader);
862
	    unsigned  external = tdf_read_int (reader);
840
	    unsigned  external = tdf_read_int(reader);
863
	    unsigned  num_ids  = shapes_vec [i].num_ids;
841
	    unsigned  num_ids  = shapes_vec[i].num_ids;
864
	    unsigned *id_maps  = shapes_vec [i].id_maps;
842
	    unsigned *id_maps  = shapes_vec[i].id_maps;
865
 
843
 
866
	    if (external >= num_ids) {
844
	    if (external >= num_ids) {
867
		E_id_out_of_range (capsule, external, num_ids, key, unit_num,
845
		E_id_out_of_range(capsule, external, num_ids, key, unit_num,
868
				   unit_entry_key (unit_entry));
846
				   unit_entry_key(unit_entry));
869
		THROW (XX_capsule_error);
847
		THROW(XX_capsule_error);
870
		UNREACHED;
848
		UNREACHED;
871
	    }
849
	    }
872
	    if (id_maps [external] == UINT_MAX) {
850
	    if (id_maps[external] == UINT_MAX) {
873
		unsigned id = shape_entry_next_id (shape_entry);
851
		unsigned id = shape_entry_next_id(shape_entry);
874
 
852
 
875
		id_maps [external] = id;
853
		id_maps[external] = id;
876
	    }
854
	    }
877
	    debug_info_r_map (internal, external, id_maps [external]);
855
	    debug_info_r_map(internal, external, id_maps[external]);
878
	    external = id_maps [external];
856
	    external = id_maps[external];
879
	    map_entry_set_link (entries [i], j, internal, external);
857
	    map_entry_set_link(entries[i], j, internal, external);
880
	}
858
	}
881
    }
859
    }
882
}
860
}
883
 
861
 
884
static void
862
static void
885
capsule_read_unit PROTO_N ((capsule, num_shapes, shapes_vec, unit_entry,
863
capsule_read_unit(CapsuleP capsule, unsigned num_shapes, ShapeDataP shapes_vec,
886
			    unit_num))
-
 
887
		  PROTO_T (CapsuleP   capsule X
-
 
888
			   unsigned   num_shapes X
-
 
889
			   ShapeDataP shapes_vec X
-
 
890
			   UnitEntryP unit_entry X
864
		  UnitEntryP unit_entry, unsigned unit_num)
891
			   unsigned   unit_num)
-
 
892
{
865
{
893
    TDFReaderP reader     = capsule_reader (capsule);
866
    TDFReaderP reader     = capsule_reader(capsule);
894
    unsigned   num_counts = tdf_read_int (reader);
867
    unsigned   num_counts = tdf_read_int(reader);
895
    UnitP      unit       = unit_entry_add_unit (unit_entry, num_counts);
868
    UnitP      unit       = unit_entry_add_unit(unit_entry, num_counts);
896
    MapEntryP *entries;
869
    MapEntryP *entries;
897
    unsigned   size;
870
    unsigned   size;
898
    NStringT   nstring;
871
    NStringT   nstring;
899
 
872
 
900
    entries = capsule_read_unit_counts (capsule, num_shapes, shapes_vec,
873
    entries = capsule_read_unit_counts(capsule, num_shapes, shapes_vec,
901
					num_counts, unit_entry, unit,
874
					num_counts, unit_entry, unit,
902
					unit_num);
875
					unit_num);
903
    capsule_read_unit_maps (capsule, num_counts, shapes_vec, unit_entry,
876
    capsule_read_unit_maps(capsule, num_counts, shapes_vec, unit_entry,
904
			    unit_num, entries);
877
			    unit_num, entries);
905
    size = tdf_read_int (reader);
878
    size = tdf_read_int(reader);
906
    debug_info_r_unit_body (size);
879
    debug_info_r_unit_body(size);
907
    nstring_init_length (&nstring, size);
880
    nstring_init_length(&nstring, size);
908
    tdf_read_bytes (reader, &nstring);
881
    tdf_read_bytes(reader, &nstring);
909
    unit_set_contents (unit, &nstring);
882
    unit_set_contents(unit, &nstring);
910
    DEALLOCATE (entries);
883
    DEALLOCATE(entries);
911
}
884
}
912
 
885
 
913
static void
886
static void
914
capsule_read_units PROTO_N ((capsule, num_shapes, shapes_vec, unit_entry))
887
capsule_read_units(CapsuleP   capsule,			    unsigned   num_shapes, 
915
		   PROTO_T (CapsuleP   capsule X
-
 
916
			    unsigned   num_shapes X
-
 
917
			    ShapeDataP shapes_vec X
888
			    ShapeDataP shapes_vec, 
918
			    UnitEntryP unit_entry)
889
			    UnitEntryP unit_entry)
919
{
890
{
920
    TDFReaderP reader    = capsule_reader (capsule);
891
    TDFReaderP reader    = capsule_reader(capsule);
921
    unsigned   num_units = tdf_read_int (reader);
892
    unsigned   num_units = tdf_read_int(reader);
922
    unsigned   i;
893
    unsigned   i;
923
 
894
 
924
    debug_info_r_start_units (unit_entry_key (unit_entry), num_units);
895
    debug_info_r_start_units(unit_entry_key(unit_entry), num_units);
925
    for (i = 0; i < num_units; i ++) {
896
    for (i = 0; i < num_units; i++) {
926
	debug_info_r_start_unit (unit_entry_key (unit_entry), i + 1,
897
	debug_info_r_start_unit(unit_entry_key(unit_entry), i + 1,
927
				 num_units);
898
				 num_units);
928
	capsule_read_unit (capsule, num_shapes, shapes_vec, unit_entry, i);
899
	capsule_read_unit(capsule, num_shapes, shapes_vec, unit_entry, i);
929
    }
900
    }
930
}
901
}
931
 
902
 
932
static void
903
static void
933
capsule_read_unit_sets PROTO_N ((capsule, num_unit_sets, units_vec, shapes,
904
capsule_read_unit_sets(CapsuleP capsule, unsigned num_unit_sets,
934
				 num_shapes, shapes_vec, names_vec_vec))
-
 
935
		       PROTO_T (CapsuleP    capsule X
-
 
936
				unsigned    num_unit_sets X
-
 
937
				UnitEntryP *units_vec X
905
		       UnitEntryP *units_vec, ShapeTableP shapes,
938
				ShapeTableP shapes X
-
 
939
				unsigned    num_shapes X
-
 
940
				ShapeDataP  shapes_vec X
906
		       unsigned num_shapes, ShapeDataP shapes_vec,
941
				NameDataP   names_vec_vec)
907
		       NameDataP names_vec_vec)
942
{
908
{
943
    TDFReaderP reader       = capsule_reader (capsule);
909
    TDFReaderP reader       = capsule_reader(capsule);
944
    UnitEntryP tld_entry    = capsule_unit_sets [capsule_tld_index].entry;
910
    UnitEntryP tld_entry    = capsule_unit_sets[capsule_tld_index].entry;
945
    UnitEntryP tld2_entry   = ((capsule_tld2_index == UINT_MAX) ?
911
    UnitEntryP tld2_entry   = ((capsule_tld2_index == UINT_MAX)?
946
			       NIL (UnitEntryP) :
912
			       NIL(UnitEntryP):
947
			       capsule_unit_sets [capsule_tld2_index].entry);
913
			       capsule_unit_sets[capsule_tld2_index].entry);
948
    unsigned   num_units;
914
    unsigned   num_units;
949
    unsigned   i;
915
    unsigned   i;
950
 
916
 
951
    if ((num_units = tdf_read_int (reader)) != num_unit_sets) {
917
    if ((num_units = tdf_read_int(reader)) != num_unit_sets) {
952
	E_unit_set_count_mismatch (capsule, num_unit_sets, num_units);
918
	E_unit_set_count_mismatch(capsule, num_unit_sets, num_units);
953
	THROW (XX_capsule_error);
919
	THROW(XX_capsule_error);
954
	UNREACHED;
920
	UNREACHED;
955
    }
921
    }
956
    debug_info_r_start_unit_sets (num_units);
922
    debug_info_r_start_unit_sets(num_units);
957
    for (i = 0; i < num_units; i ++) {
923
    for (i = 0; i < num_units; i++) {
958
	if (units_vec [i] == tld_entry) {
924
	if (units_vec[i] == tld_entry) {
959
	    capsule_read_tld_units (capsule, shapes, num_shapes, shapes_vec,
925
	    capsule_read_tld_units(capsule, shapes, num_shapes, shapes_vec,
960
				    names_vec_vec);
926
				    names_vec_vec);
961
	} else if (units_vec [i] == tld2_entry) {
927
	} else if (units_vec[i] == tld2_entry) {
962
	    capsule_read_tld2_units (capsule, shapes, num_shapes, shapes_vec,
928
	    capsule_read_tld2_units(capsule, shapes, num_shapes, shapes_vec,
963
				     names_vec_vec);
929
				     names_vec_vec);
964
	} else {
930
	} else {
965
	    capsule_read_units (capsule, num_shapes, shapes_vec,
931
	    capsule_read_units(capsule, num_shapes, shapes_vec,
966
				units_vec [i]);
932
				units_vec[i]);
967
	}
933
	}
968
    }
934
    }
969
}
935
}
970
 
936
 
971
/*--------------------------------------------------------------------------*/
937
/*--------------------------------------------------------------------------*/
972
 
938
 
973
static void
939
static void
974
capsule_write_header PROTO_N ((capsule))
940
capsule_write_header(CapsuleP capsule)
975
    		     PROTO_T (CapsuleP capsule)
-
 
976
{
941
{
977
    TDFWriterP writer      = capsule_writer (capsule);
942
    TDFWriterP writer      = capsule_writer(capsule);
978
    NStringP   const_magic = capsule_magic ();
943
    NStringP   const_magic = capsule_magic();
979
 
944
 
980
    tdf_write_bytes (writer, const_magic);
945
    tdf_write_bytes(writer, const_magic);
981
    ASSERT (capsule_major_version >= 4);
946
    ASSERT(capsule_major_version >= 4);
982
    tdf_write_int (writer, capsule_major_version);
947
    tdf_write_int(writer, capsule_major_version);
983
    tdf_write_int (writer, capsule_minor_version);
948
    tdf_write_int(writer, capsule_minor_version);
984
    debug_info_w_versions (capsule_major_version, capsule_minor_version);
949
    debug_info_w_versions(capsule_major_version, capsule_minor_version);
985
    tdf_write_align (writer);
950
    tdf_write_align(writer);
986
}
951
}
987
 
952
 
988
/*--------------------------------------------------------------------------*/
953
/*--------------------------------------------------------------------------*/
989
 
954
 
990
void
955
void
991
capsule_read_unit_set_file PROTO_N ((name))
956
capsule_read_unit_set_file(CStringP name)
992
			   PROTO_T (CStringP name)
-
 
993
{
957
{
994
    IStreamT istream;
958
    IStreamT istream;
995
 
959
 
996
    ASSERT (capsule_unit_sets == NIL (UnitSetP));
960
    ASSERT(capsule_unit_sets == NIL(UnitSetP));
997
    if (!istream_open (&istream, name)) {
961
    if (!istream_open(&istream, name)) {
998
	E_cannot_open_unit_set_file (name);
962
	E_cannot_open_unit_set_file(name);
999
	UNREACHED;
963
	UNREACHED;
1000
    }
964
    }
1001
    capsule_read_unit_set_file_1 (&istream);
965
    capsule_read_unit_set_file_1(&istream);
1002
    istream_close (&istream);
966
    istream_close(&istream);
1003
}
967
}
1004
 
968
 
1005
CapsuleP
969
CapsuleP
1006
capsule_create_stream_input PROTO_N ((name))
970
capsule_create_stream_input(CStringP name)
1007
			    PROTO_T (CStringP name)
-
 
1008
{
971
{
1009
    CapsuleP capsule = ALLOCATE (CapsuleT);
972
    CapsuleP capsule = ALLOCATE(CapsuleT);
1010
 
973
 
1011
    capsule->type = CT_INPUT;
974
    capsule->type = CT_INPUT;
1012
    if (!tdf_reader_open (capsule_reader (capsule), name)) {
975
    if (!tdf_reader_open(capsule_reader(capsule), name)) {
1013
	DEALLOCATE (capsule);
976
	DEALLOCATE(capsule);
1014
	return (NIL (CapsuleP));
977
	return(NIL(CapsuleP));
1015
    }
978
    }
1016
    capsule->name     = name;
979
    capsule->name     = name;
1017
    capsule->complete = FALSE;
980
    capsule->complete = FALSE;
1018
    return (capsule);
981
    return(capsule);
1019
}
982
}
1020
 
983
 
1021
CapsuleP
984
CapsuleP
1022
capsule_create_string_input PROTO_N ((name, contents))
985
capsule_create_string_input(CStringP name,				     NStringP contents)
1023
			    PROTO_T (CStringP name X
-
 
1024
				     NStringP contents)
-
 
1025
{
986
{
1026
    CapsuleP capsule = ALLOCATE (CapsuleT);
987
    CapsuleP capsule = ALLOCATE(CapsuleT);
1027
 
988
 
1028
    capsule->type     = CT_INPUT;
989
    capsule->type     = CT_INPUT;
1029
    tdf_reader_open_string (capsule_reader (capsule), name, contents);
990
    tdf_reader_open_string(capsule_reader(capsule), name, contents);
1030
    capsule->name     = name;
991
    capsule->name     = name;
1031
    capsule->complete = FALSE;
992
    capsule->complete = FALSE;
1032
    return (capsule);
993
    return(capsule);
1033
}
994
}
1034
 
995
 
1035
CapsuleP
996
CapsuleP
1036
capsule_create_stream_output PROTO_N ((name))
997
capsule_create_stream_output(CStringP name)
1037
			     PROTO_T (CStringP name)
-
 
1038
{
998
{
1039
    CapsuleP capsule = ALLOCATE (CapsuleT);
999
    CapsuleP capsule = ALLOCATE(CapsuleT);
1040
 
1000
 
1041
    capsule->type = CT_OUTPUT;
1001
    capsule->type = CT_OUTPUT;
1042
    if (!tdf_writer_open (capsule_writer (capsule), name)) {
1002
    if (!tdf_writer_open(capsule_writer(capsule), name)) {
1043
	DEALLOCATE (capsule);
1003
	DEALLOCATE(capsule);
1044
	return (NIL (CapsuleP));
1004
	return(NIL(CapsuleP));
1045
    }
1005
    }
1046
    capsule->name = name;
1006
    capsule->name = name;
1047
    return (capsule);
1007
    return(capsule);
1048
}
1008
}
1049
 
1009
 
1050
CStringP
1010
CStringP
1051
capsule_name PROTO_N ((capsule))
1011
capsule_name(CapsuleP capsule)
1052
	     PROTO_T (CapsuleP capsule)
-
 
1053
{
1012
{
1054
    return (capsule->name);
1013
    return(capsule->name);
1055
}
1014
}
1056
 
1015
 
1057
unsigned
1016
unsigned
1058
capsule_byte PROTO_N ((capsule))
1017
capsule_byte(CapsuleP capsule)
1059
	     PROTO_T (CapsuleP capsule)
-
 
1060
{
1018
{
1061
    return (tdf_reader_byte (capsule_reader (capsule)));
1019
    return(tdf_reader_byte(capsule_reader(capsule)));
1062
}
1020
}
1063
 
1021
 
1064
void
1022
void
1065
capsule_read PROTO_N ((capsule, units, shapes))
1023
capsule_read(CapsuleP    capsule,		      UnitTableP  units, 
1066
	     PROTO_T (CapsuleP    capsule X
-
 
1067
		      UnitTableP  units X
-
 
1068
		      ShapeTableP shapes)
1024
		      ShapeTableP shapes)
1069
{
1025
{
1070
    ASSERT (capsule->type == CT_INPUT);
1026
    ASSERT(capsule->type == CT_INPUT);
1071
    capsule_setup (units);
1027
    capsule_setup(units);
1072
    HANDLE {
1028
    HANDLE {
1073
	UnitEntryP *units_vec;
1029
	UnitEntryP *units_vec;
1074
	unsigned    num_unit_sets;
1030
	unsigned    num_unit_sets;
1075
	ShapeDataP  shapes_vec;
1031
	ShapeDataP  shapes_vec;
1076
	unsigned    num_shapes;
1032
	unsigned    num_shapes;
1077
	NameDataP   names_vec_vec;
1033
	NameDataP   names_vec_vec;
1078
	unsigned    i;
1034
	unsigned    i;
1079
 
1035
 
1080
	debug_info_r_start_capsule (capsule_name (capsule));
1036
	debug_info_r_start_capsule(capsule_name(capsule));
1081
	capsule_read_header (capsule);
1037
	capsule_read_header(capsule);
1082
	units_vec     = capsule_read_unit_set_names (capsule, units,
1038
	units_vec     = capsule_read_unit_set_names(capsule, units,
1083
						     &num_unit_sets);
1039
						     &num_unit_sets);
1084
	shapes_vec    = capsule_read_shapes (capsule, shapes, &num_shapes);
1040
	shapes_vec    = capsule_read_shapes(capsule, shapes, &num_shapes);
1085
	names_vec_vec = capsule_read_external_names (capsule, num_shapes,
1041
	names_vec_vec = capsule_read_external_names(capsule, num_shapes,
1086
						     shapes_vec);
1042
						     shapes_vec);
1087
	capsule_read_unit_sets (capsule, num_unit_sets, units_vec, shapes,
1043
	capsule_read_unit_sets(capsule, num_unit_sets, units_vec, shapes,
1088
				num_shapes, shapes_vec, names_vec_vec);
1044
				num_shapes, shapes_vec, names_vec_vec);
1089
	tdf_read_eof (capsule_reader (capsule));
1045
	tdf_read_eof(capsule_reader(capsule));
1090
	debug_info_r_end_capsule ();
1046
	debug_info_r_end_capsule();
1091
	DEALLOCATE (units_vec);
1047
	DEALLOCATE(units_vec);
1092
	for (i = 0; i < num_shapes; i ++) {
1048
	for (i = 0; i < num_shapes; i++) {
1093
	    DEALLOCATE (shapes_vec [i].id_maps);
1049
	    DEALLOCATE(shapes_vec[i].id_maps);
1094
	    DEALLOCATE (names_vec_vec [i].names_vec);
1050
	    DEALLOCATE(names_vec_vec[i].names_vec);
1095
	}
1051
	}
1096
	DEALLOCATE (shapes_vec);
1052
	DEALLOCATE(shapes_vec);
1097
	DEALLOCATE (names_vec_vec);
1053
	DEALLOCATE(names_vec_vec);
1098
	capsule->complete = TRUE;
1054
	capsule->complete = TRUE;
1099
    } WITH {
1055
    } WITH {
1100
	ExceptionP exception = EXCEPTION_EXCEPTION ();
1056
	ExceptionP exception = EXCEPTION_EXCEPTION();
1101
 
1057
 
1102
	debug_info_r_abort_capsule ();
1058
	debug_info_r_abort_capsule();
1103
	if ((exception != XX_capsule_error) &&
1059
	if ((exception != XX_capsule_error) &&
1104
	    (exception != XX_tdf_read_error)) {
1060
	   (exception != XX_tdf_read_error)) {
1105
	    RETHROW ();
1061
	    RETHROW();
1106
	}
1062
	}
1107
    } END_HANDLE
1063
    } END_HANDLE
1108
}
1064
}
1109
 
1065
 
1110
void
1066
void
1111
capsule_store_contents PROTO_N ((capsule))
1067
capsule_store_contents(CapsuleP capsule)
1112
		       PROTO_T (CapsuleP capsule)
-
 
1113
{
1068
{
1114
    if (capsule->complete) {
1069
    if (capsule->complete) {
1115
	TDFReaderP reader = capsule_reader (capsule);
1070
	TDFReaderP reader = capsule_reader(capsule);
1116
	unsigned   length = tdf_reader_byte (reader);
1071
	unsigned   length = tdf_reader_byte(reader);
1117
 
1072
 
1118
	nstring_init_length (&(capsule->contents), length);
1073
	nstring_init_length(& (capsule->contents), length);
1119
	tdf_reader_rewind (reader);
1074
	tdf_reader_rewind(reader);
1120
	tdf_read_bytes (reader, &(capsule->contents));
1075
	tdf_read_bytes(reader, & (capsule->contents));
1121
	tdf_read_eof (reader);
1076
	tdf_read_eof(reader);
1122
    }
1077
    }
1123
}
1078
}
1124
 
1079
 
1125
NStringP
1080
NStringP
1126
capsule_contents PROTO_N ((capsule))
1081
capsule_contents(CapsuleP capsule)
1127
		 PROTO_T (CapsuleP capsule)
-
 
1128
{
1082
{
1129
    return (&(capsule->contents));
1083
    return(& (capsule->contents));
1130
}
1084
}
1131
 
1085
 
1132
void
1086
void
1133
capsule_set_index PROTO_N ((capsule, i))
1087
capsule_set_index(CapsuleP capsule,			   unsigned i)
1134
		  PROTO_T (CapsuleP capsule X
-
 
1135
			   unsigned i)
-
 
1136
{
1088
{
1137
    capsule->capsule_index = i;
1089
    capsule->capsule_index = i;
1138
}
1090
}
1139
 
1091
 
1140
unsigned
1092
unsigned
1141
capsule_get_index PROTO_N ((capsule))
1093
capsule_get_index(CapsuleP capsule)
1142
		  PROTO_T (CapsuleP capsule)
-
 
1143
{
1094
{
1144
    return (capsule->capsule_index);
1095
    return(capsule->capsule_index);
1145
}
1096
}
1146
 
1097
 
1147
void
1098
void
1148
capsule_write PROTO_N ((capsule, units, shapes))
1099
capsule_write(CapsuleP    capsule,		       UnitTableP  units, 
1149
	      PROTO_T (CapsuleP    capsule X
-
 
1150
		       UnitTableP  units X
-
 
1151
		       ShapeTableP shapes)
1100
		       ShapeTableP shapes)
1152
{
1101
{
1153
    TDFWriterP      writer     = capsule_writer (capsule);
1102
    TDFWriterP      writer     = capsule_writer(capsule);
1154
    UnitEntryP      tld_entry  = capsule_unit_sets [capsule_tld_index].entry;
1103
    UnitEntryP      tld_entry  = capsule_unit_sets[capsule_tld_index].entry;
1155
    unsigned        num_shapes = 0;
1104
    unsigned        num_shapes = 0;
1156
    UnitSetClosureT unit_set_closure;
1105
    UnitSetClosureT unit_set_closure;
1157
    unsigned        i;
1106
    unsigned        i;
1158
 
1107
 
1159
    debug_info_w_start_capsule (capsule_name (capsule));
1108
    debug_info_w_start_capsule(capsule_name(capsule));
1160
    capsule_write_header (capsule);
1109
    capsule_write_header(capsule);
1161
    unit_set_closure.num_unit_sets = 1;
1110
    unit_set_closure.num_unit_sets = 1;
1162
    unit_set_closure.shapes        = shapes;
1111
    unit_set_closure.shapes        = shapes;
1163
    unit_table_iter (units, unit_entry_do_count, (GenericP) &unit_set_closure);
1112
    unit_table_iter(units, unit_entry_do_count,(GenericP) &unit_set_closure);
1164
    debug_info_w_start_unit_decs (unit_set_closure.num_unit_sets);
1113
    debug_info_w_start_unit_decs(unit_set_closure.num_unit_sets);
1165
    tdf_write_int (writer, unit_set_closure.num_unit_sets);
1114
    tdf_write_int(writer, unit_set_closure.num_unit_sets);
1166
    for (i = 0; i < capsule_num_unit_sets; i ++) {
1115
    for (i = 0; i < capsule_num_unit_sets; i++) {
1167
	UnitEntryP entry = capsule_unit_sets [i].entry;
1116
	UnitEntryP entry = capsule_unit_sets[i].entry;
1168
 
1117
 
1169
	unit_entry_write_unit_set (entry, tld_entry, writer);
1118
	unit_entry_write_unit_set(entry, tld_entry, writer);
1170
    }
1119
    }
1171
    shape_table_iter (shapes, shape_entry_do_count, (GenericP) &num_shapes);
1120
    shape_table_iter(shapes, shape_entry_do_count,(GenericP) &num_shapes);
1172
    debug_info_w_start_shapes (num_shapes);
1121
    debug_info_w_start_shapes(num_shapes);
1173
    tdf_write_int (writer, num_shapes);
1122
    tdf_write_int(writer, num_shapes);
1174
    shape_table_iter (shapes, shape_entry_write_shape, (GenericP) writer);
1123
    shape_table_iter(shapes, shape_entry_write_shape,(GenericP)writer);
1175
    debug_info_w_start_names (num_shapes);
1124
    debug_info_w_start_names(num_shapes);
1176
    tdf_write_int (writer, num_shapes);
1125
    tdf_write_int(writer, num_shapes);
1177
    shape_table_iter (shapes, shape_entry_write_externs, (GenericP) writer);
1126
    shape_table_iter(shapes, shape_entry_write_externs,(GenericP)writer);
1178
    debug_info_w_start_unit_sets (unit_set_closure.num_unit_sets);
1127
    debug_info_w_start_unit_sets(unit_set_closure.num_unit_sets);
1179
    tdf_write_int (writer, unit_set_closure.num_unit_sets);
1128
    tdf_write_int(writer, unit_set_closure.num_unit_sets);
1180
    for (i = 0; i < capsule_num_unit_sets; i ++) {
1129
    for (i = 0; i < capsule_num_unit_sets; i++) {
1181
	UnitEntryP entry = capsule_unit_sets [i].entry;
1130
	UnitEntryP entry = capsule_unit_sets[i].entry;
1182
 
1131
 
1183
	if (entry == tld_entry) {
1132
	if (entry == tld_entry) {
1184
	    unit_entry_write_tld_unit (entry, shapes, writer);
1133
	    unit_entry_write_tld_unit(entry, shapes, writer);
1185
	} else {
1134
	} else {
1186
	    unit_entry_write_units (entry, shapes, num_shapes, writer);
1135
	    unit_entry_write_units(entry, shapes, num_shapes, writer);
1187
	}
1136
	}
1188
    }
1137
    }
1189
    debug_info_w_end_capsule ();
1138
    debug_info_w_end_capsule();
1190
}
1139
}
1191
 
1140
 
1192
void
1141
void
1193
capsule_close PROTO_N ((capsule))
1142
capsule_close(CapsuleP capsule)
1194
	      PROTO_T (CapsuleP capsule)
-
 
1195
{
1143
{
1196
    switch (capsule->type) EXHAUSTIVE {
1144
    switch (capsule->type)EXHAUSTIVE {
1197
      case CT_INPUT:
1145
      case CT_INPUT:
1198
	tdf_reader_close (capsule_reader (capsule));
1146
	tdf_reader_close(capsule_reader(capsule));
1199
	break;
1147
	break;
1200
      case CT_OUTPUT:
1148
      case CT_OUTPUT:
1201
	tdf_writer_close (capsule_writer (capsule));
1149
	tdf_writer_close(capsule_writer(capsule));
1202
	break;
1150
	break;
1203
    }
1151
    }
1204
}
1152
}
1205
 
1153
 
1206
unsigned
1154
unsigned
1207
capsule_get_major_version PROTO_Z ()
1155
capsule_get_major_version(void)
1208
{
1156
{
1209
    return (capsule_major_version);
1157
    return(capsule_major_version);
1210
}
1158
}
1211
 
1159
 
1212
void
1160
void
1213
capsule_set_major_version PROTO_N ((major))
1161
capsule_set_major_version(unsigned major)
1214
    			  PROTO_T (unsigned major)
-
 
1215
{
1162
{
1216
    capsule_major_version = major;
1163
    capsule_major_version = major;
1217
}
1164
}
1218
 
1165
 
1219
unsigned
1166
unsigned
1220
capsule_get_minor_version PROTO_Z ()
1167
capsule_get_minor_version(void)
1221
{
1168
{
1222
    return (capsule_minor_version);
1169
    return(capsule_minor_version);
1223
}
1170
}
1224

1171

1225
/*
1172
/*
1226
 * Local variables(smf):
1173
 * Local variables(smf):
1227
 * eval: (include::add-path-entry "../os-interface" "../library")
1174
 * eval: (include::add-path-entry "../os-interface" "../library")