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
/*** library.c --- TDF library ADT.
61
/*** library.c --- TDF library 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 library routines used by the TDF linker.
67
 * This file implements the TDF library routines used by the TDF linker.
38
 *
68
 *
39
 *** Change Log:
69
 *** Change Log:
40
 * $Log: library.c,v $
70
 * $Log: library.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
 *
44
 * Revision 1.5  1995/09/22  08:39:17  smf
74
 * Revision 1.5  1995/09/22  08:39:17  smf
45
 * Fixed problems with incomplete structures (to shut "tcc" up).
75
 * Fixed problems with incomplete structures (to shut "tcc" up).
46
 * Fixed some problems in "name-key.c" (no real problems, but rewritten to
76
 * Fixed some problems in "name-key.c" (no real problems, but rewritten to
47
 * reduce the warnings that were output by "tcc" and "gcc").
77
 * reduce the warnings that were output by "tcc" and "gcc").
48
 * Fixed bug CR95_354.tld-common-id-problem (library capsules could be loaded
78
 * Fixed bug CR95_354.tld-common-id-problem (library capsules could be loaded
Line 55... Line 85...
55
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
85
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
56
 * OSSG C Coding Standards.
86
 * OSSG C Coding Standards.
57
 *
87
 *
58
 * Revision 1.2  1994/08/23  09:40:08  smf
88
 * Revision 1.2  1994/08/23  09:40:08  smf
59
 * Fixed bug DR114:ids-too-long.
89
 * Fixed bug DR114:ids-too-long.
60
 *
90
 *
61
 * Revision 1.1.1.1  1994/07/25  16:03:31  smf
91
 * Revision 1.1.1.1  1994/07/25  16:03:31  smf
62
 * Initial import of TDF linker 3.5 non shared files.
92
 * Initial import of TDF linker 3.5 non shared files.
63
 *
93
 *
64
**/
94
**/
65
 
95
 
Line 76... Line 106...
76
 
106
 
77
#include "solve-cycles.h"
107
#include "solve-cycles.h"
78
 
108
 
79
/*--------------------------------------------------------------------------*/
109
/*--------------------------------------------------------------------------*/
80
 
110
 
81
static ExceptionP XX_library_error   = EXCEPTION ("error in TDF library");
111
static ExceptionP XX_library_error   = EXCEPTION("error in TDF library");
82
 
112
 
83
/*--------------------------------------------------------------------------*/
113
/*--------------------------------------------------------------------------*/
84
 
114
 
85
static TDFReaderP
115
static TDFReaderP
86
library_reader PROTO_N ((library))
116
library_reader(LibraryP library)
87
	       PROTO_T (LibraryP library)
-
 
88
{
117
{
89
    ASSERT (library->type == LT_INPUT);
118
    ASSERT(library->type == LT_INPUT);
90
    return (&(library->u.reader));
119
    return(& (library->u.reader));
91
}
120
}
92
 
121
 
93
static TDFWriterP
122
static TDFWriterP
94
library_writer PROTO_N ((library))
123
library_writer(LibraryP library)
95
	       PROTO_T (LibraryP library)
-
 
96
{
124
{
97
    ASSERT (library->type == LT_OUTPUT);
125
    ASSERT(library->type == LT_OUTPUT);
98
    return (&(library->u.writer));
126
    return(& (library->u.writer));
99
}
127
}
100
 
128
 
101
/*--------------------------------------------------------------------------*/
129
/*--------------------------------------------------------------------------*/
102
 
130
 
103
static void
131
static void
104
library_check_index_entry PROTO_N ((library, entry, need_dec, no_mult,
132
library_check_index_entry(LibraryP library, ShapeEntryP entry, BoolT need_dec,
105
				    shape_key, key, use, lib_capsule, table))
-
 
106
                          PROTO_T (LibraryP    library X
-
 
107
				   ShapeEntryP entry X
-
 
108
				   BoolT       need_dec X
-
 
109
				   BoolT       no_mult X
-
 
110
				   NStringP    shape_key X
133
			  BoolT no_mult, NStringP shape_key, NameKeyP key,
111
				   NameKeyP    key X
-
 
112
				   unsigned    use X
-
 
113
				   LibCapsuleP lib_capsule X
134
			  unsigned use, LibCapsuleP lib_capsule,
114
				   NameTableP  table)
135
			  NameTableP table)
115
{
136
{
116
    NameEntryP  name_entry  = name_table_add (table, key, entry);
137
    NameEntryP  name_entry  = name_table_add(table, key, entry);
117
    unsigned    name_use    = name_entry_get_use (name_entry);
138
    unsigned    name_use    = name_entry_get_use(name_entry);
118
 
139
 
119
    if (use & ~(U_USED | U_DECD | U_DEFD | U_MULT)) {
140
    if (use & ~(U_USED | U_DECD | U_DEFD | U_MULT)) {
120
	E_lib_bad_usage (library, shape_key, key, use);
141
	E_lib_bad_usage(library, shape_key, key, use);
121
	THROW (XX_library_error);
142
	THROW(XX_library_error);
122
	UNREACHED;
143
	UNREACHED;
123
    } else if (no_mult && (use & U_MULT)) {
144
    } else if (no_mult && (use & U_MULT)) {
124
	E_lib_illegally_mult_defined (library, shape_key, key);
145
	E_lib_illegally_mult_defined(library, shape_key, key);
125
	THROW (XX_library_error);
146
	THROW(XX_library_error);
126
	UNREACHED;
147
	UNREACHED;
127
    } else if (need_dec &&
148
    } else if (need_dec &&
128
	       (((use & (U_DEFD | U_DECD)) == U_DEFD) ||
149
	      (((use & (U_DEFD | U_DECD)) == U_DEFD) ||
129
		((use & (U_MULT | U_DECD)) == U_MULT))) {
150
		((use & (U_MULT | U_DECD)) == U_MULT))) {
130
	E_lib_defined_but_not_declared (library, shape_key, key);
151
	E_lib_defined_but_not_declared(library, shape_key, key);
131
	THROW (XX_library_error);
152
	THROW(XX_library_error);
132
	UNREACHED;
153
	UNREACHED;
133
    }
154
    }
134
    if ((use & U_DEFD) && (name_use & U_DEFD)) {
155
    if ((use & U_DEFD) && (name_use & U_DEFD)) {
135
	LibCapsuleP definition = name_entry_get_lib_definition (name_entry);
156
	LibCapsuleP definition = name_entry_get_lib_definition(name_entry);
136
 
157
 
137
	E_lib_multiply_defined (library, shape_key, key, definition);
158
	E_lib_multiply_defined(library, shape_key, key, definition);
138
    } else if ((use & U_MULT) && (name_use & U_MULT) &&
159
    } else if ((use & U_MULT) && (name_use & U_MULT) &&
139
	       (!(use & U_DEFD)) && (!(name_use & U_DEFD))) {
160
	      (!(use & U_DEFD)) && (!(name_use & U_DEFD))) {
140
	name_entry_set_lib_definition (name_entry, NIL (LibCapsuleP));
161
	name_entry_set_lib_definition(name_entry, NIL(LibCapsuleP));
141
    } else if ((use & U_DEFD) ||
162
    } else if ((use & U_DEFD) ||
142
	       ((use & U_MULT) && (!(name_use & (U_MULT | U_DEFD))))) {
163
	      ((use & U_MULT) && (!(name_use & (U_MULT | U_DEFD))))) {
143
	name_entry_set_lib_definition (name_entry, lib_capsule);
164
	name_entry_set_lib_definition(name_entry, lib_capsule);
144
    }
165
    }
145
    debug_info_r_index_entry (key, use, name_use, name_entry_key (name_entry),
166
    debug_info_r_index_entry(key, use, name_use, name_entry_key(name_entry),
146
			      lib_capsule_name (lib_capsule));
167
			      lib_capsule_name(lib_capsule));
147
    name_entry_merge_use (name_entry, use);
168
    name_entry_merge_use(name_entry, use);
148
}
169
}
149
 
170
 
150
static unsigned
171
static unsigned
151
library_read_version_0_capsules PROTO_N ((library))
172
library_read_version_0_capsules(LibraryP library)
152
				PROTO_T (LibraryP library)
-
 
153
{
173
{
154
    TDFReaderP reader       = library_reader (library);
174
    TDFReaderP reader       = library_reader(library);
155
    unsigned   num_capsules = tdf_read_int (reader);
175
    unsigned   num_capsules = tdf_read_int(reader);
156
    unsigned   i;
176
    unsigned   i;
157
 
177
 
158
    debug_info_r_start_capsules (num_capsules);
178
    debug_info_r_start_capsules(num_capsules);
159
    library->num_capsules = num_capsules;
179
    library->num_capsules = num_capsules;
160
    library->capsules     = ALLOCATE_VECTOR (LibCapsuleT, num_capsules);
180
    library->capsules     = ALLOCATE_VECTOR(LibCapsuleT, num_capsules);
161
    for (i = 0; i < num_capsules; i ++) {
181
    for (i = 0; i < num_capsules; i++) {
162
	NStringP contents = &(library->capsules [i].contents);
182
	NStringP contents = & (library->capsules[i].contents);
163
	NStringT nstring;
183
	NStringT nstring;
164
	unsigned length;
184
	unsigned length;
165
 
185
 
166
	tdf_read_string (reader, &nstring);
186
	tdf_read_string(reader, &nstring);
167
	if (nstring_contains (&nstring, '\0')) {
187
	if (nstring_contains(&nstring, '\0')) {
168
	    E_null_in_file_name (library, &nstring);
188
	    E_null_in_file_name(library, &nstring);
169
	    THROW (XX_library_error);
189
	    THROW(XX_library_error);
170
	    UNREACHED;
190
	    UNREACHED;
171
	}
191
	}
172
	library->capsules [i].name    = nstring_to_cstring (&nstring);
192
	library->capsules[i].name    = nstring_to_cstring(&nstring);
173
	library->capsules [i].library = library;
193
	library->capsules[i].library = library;
174
	library->capsules [i].loaded  = FALSE;
194
	library->capsules[i].loaded  = FALSE;
175
	length = tdf_read_int (reader);
195
	length = tdf_read_int(reader);
176
	nstring_init_length (contents, length);
196
	nstring_init_length(contents, length);
177
	tdf_read_bytes (reader, contents);
197
	tdf_read_bytes(reader, contents);
178
	debug_info_r_capsule (&nstring, length);
198
	debug_info_r_capsule(&nstring, length);
179
	nstring_destroy (&nstring);
199
	nstring_destroy(&nstring);
180
    }
200
    }
181
    return (num_capsules);
201
    return(num_capsules);
182
}
202
}
183
 
203
 
184
static void
204
static void
185
library_read_version_0 PROTO_N ((library, shapes))
205
library_read_version_0(LibraryP    library,				ShapeTableP shapes)
186
		       PROTO_T (LibraryP    library X
-
 
187
				ShapeTableP shapes)
-
 
188
{
206
{
189
    TDFReaderP  reader       = library_reader (library);
207
    TDFReaderP  reader       = library_reader(library);
190
    unsigned    num_capsules = library_read_version_0_capsules (library);
208
    unsigned    num_capsules = library_read_version_0_capsules(library);
191
    ShapeEntryP token_entry  = shape_table_get_token_entry (shapes);
209
    ShapeEntryP token_entry  = shape_table_get_token_entry(shapes);
192
    ShapeEntryP tag_entry    = shape_table_get_tag_entry (shapes);
210
    ShapeEntryP tag_entry    = shape_table_get_tag_entry(shapes);
193
    unsigned    num_shapes   = tdf_read_int (reader);
211
    unsigned    num_shapes   = tdf_read_int(reader);
194
    unsigned    i;
212
    unsigned    i;
195
 
213
 
196
    debug_info_r_start_index (num_shapes);
214
    debug_info_r_start_index(num_shapes);
197
    for (i = 0; i < num_shapes; i ++) {
215
    for (i = 0; i < num_shapes; i++) {
198
	NStringT    name;
216
	NStringT    name;
199
	ShapeEntryP entry;
217
	ShapeEntryP entry;
200
	BoolT       need_dec;
218
	BoolT       need_dec;
201
	BoolT       no_mult;
219
	BoolT       no_mult;
202
	NameTableP  table;
220
	NameTableP  table;
203
	unsigned    num_names;
221
	unsigned    num_names;
204
	unsigned    j;
222
	unsigned    j;
205
 
223
 
206
	tdf_read_string (reader, &name);
224
	tdf_read_string(reader, &name);
207
	entry     = shape_table_add (shapes, &name);
225
	entry     = shape_table_add(shapes, &name);
208
	need_dec  = (entry == tag_entry);
226
	need_dec  = (entry == tag_entry);
209
	no_mult   = (entry == token_entry);
227
	no_mult   = (entry == token_entry);
210
	table     = shape_entry_name_table (entry);
228
	table     = shape_entry_name_table(entry);
211
	num_names = tdf_read_int (reader);
229
	num_names = tdf_read_int(reader);
212
	debug_info_r_start_shape_index (&name, num_names);
230
	debug_info_r_start_shape_index(&name, num_names);
213
	for (j = 0; j < num_names; j ++) {
231
	for (j = 0; j < num_names; j++) {
214
	    NameKeyT    external_name;
232
	    NameKeyT    external_name;
215
	    unsigned    use;
233
	    unsigned    use;
216
	    unsigned    capsule_index;
234
	    unsigned    capsule_index;
217
	    LibCapsuleP lib_capsule;
235
	    LibCapsuleP lib_capsule;
218
 
236
 
219
	    tdf_read_name (reader, &external_name);
237
	    tdf_read_name(reader, &external_name);
220
	    use           = tdf_read_int (reader);
238
	    use           = tdf_read_int(reader);
221
	    capsule_index = tdf_read_int (reader);
239
	    capsule_index = tdf_read_int(reader);
222
	    if (capsule_index >= num_capsules) {
240
	    if (capsule_index >= num_capsules) {
223
		E_capsule_index_too_big (library, &name, &external_name,
241
		E_capsule_index_too_big(library, &name, &external_name,
224
					 capsule_index, num_capsules);
242
					 capsule_index, num_capsules);
225
		THROW (XX_library_error);
243
		THROW(XX_library_error);
226
		UNREACHED;
244
		UNREACHED;
227
	    }
245
	    }
228
	    lib_capsule = &(library->capsules [capsule_index]);
246
	    lib_capsule = & (library->capsules[capsule_index]);
229
	    library_check_index_entry (library, entry, need_dec, no_mult,
247
	    library_check_index_entry(library, entry, need_dec, no_mult,
230
				       &name, &external_name, use, lib_capsule,
248
				       &name, &external_name, use, lib_capsule,
231
				       table);
249
				       table);
232
	    name_key_destroy (&external_name);
250
	    name_key_destroy(&external_name);
233
	}
251
	}
234
	nstring_destroy (&name);
252
	nstring_destroy(&name);
235
    }
253
    }
236
    tdf_read_eof (reader);
254
    tdf_read_eof(reader);
237
}
255
}
238
 
256
 
239
static void
257
static void
240
library_extract_1 PROTO_N ((capsule, use_basename))
258
library_extract_1(LibCapsuleP capsule,			   BoolT       use_basename)
241
		  PROTO_T (LibCapsuleP capsule X
-
 
242
			   BoolT       use_basename)
-
 
243
{
259
{
244
    CStringP   old_name = lib_capsule_name (capsule);
260
    CStringP   old_name = lib_capsule_name(capsule);
245
    CStringP   name     = old_name;
261
    CStringP   name     = old_name;
246
    NStringP   contents = lib_capsule_contents (capsule);
262
    NStringP   contents = lib_capsule_contents(capsule);
247
    TDFWriterT writer;
263
    TDFWriterT writer;
248
 
264
 
249
    if (use_basename) {
265
    if (use_basename) {
250
	name = file_name_basename (name);
266
	name = file_name_basename(name);
251
    }
267
    }
252
    file_name_populate (name);
268
    file_name_populate(name);
253
    if (tdf_writer_open (&writer, name)) {
269
    if (tdf_writer_open(&writer, name)) {
254
	E_extracting_capsule (old_name, name);
270
	E_extracting_capsule(old_name, name);
255
	tdf_write_bytes (&writer, contents);
271
	tdf_write_bytes(&writer, contents);
256
	tdf_writer_close (&writer);
272
	tdf_writer_close(&writer);
257
    } else {
273
    } else {
258
	E_cannot_open_output_file (name);
274
	E_cannot_open_output_file(name);
259
    }
275
    }
260
    if (use_basename) {
276
    if (use_basename) {
261
	DEALLOCATE (name);
277
	DEALLOCATE(name);
262
    }
278
    }
263
}
279
}
264
 
280
 
265
/*--------------------------------------------------------------------------*/
281
/*--------------------------------------------------------------------------*/
266
 
282
 
267
typedef void (*LibTypeProcP)
283
typedef void(*LibTypeProcP)
268
	PROTO_S ((LibraryP, ShapeTableP));
284
(LibraryP, ShapeTableP);
269
 
285
 
270
static LibTypeProcP library_type_jump_table [] = {
286
static LibTypeProcP library_type_jump_table[] = {
271
    library_read_version_0
287
    library_read_version_0
272
};
288
};
273
 
289
 
274
#define LIBRARY_TYPE_JUMP_TABLE_SIZE \
290
#define LIBRARY_TYPE_JUMP_TABLE_SIZE \
275
	((unsigned) (sizeof (library_type_jump_table) / sizeof (LibTypeProcP)))
291
	((unsigned)(sizeof(library_type_jump_table) / sizeof(LibTypeProcP)))
276
 
292
 
277
/*--------------------------------------------------------------------------*/
293
/*--------------------------------------------------------------------------*/
278
 
294
 
279
static NStringP
295
static NStringP
280
library_magic PROTO_Z ()
296
library_magic(void)
281
{
297
{
282
    static NStringT const_magic;
298
    static NStringT const_magic;
283
    static BoolT    inited = FALSE;
299
    static BoolT    inited = FALSE;
284
 
300
 
285
    if (!inited) {
301
    if (!inited) {
286
	nstring_copy_cstring (&const_magic, "TDFL");
302
	nstring_copy_cstring(&const_magic, "TDFL");
287
	inited = TRUE;
303
	inited = TRUE;
288
    }
304
    }
289
    return (&const_magic);
305
    return(&const_magic);
290
}
306
}
291
 
307
 
292
/*--------------------------------------------------------------------------*/
308
/*--------------------------------------------------------------------------*/
293
 
309
 
294
static void
310
static void
295
library_read_header PROTO_N ((library))
311
library_read_header(LibraryP library)
296
    		    PROTO_T (LibraryP library)
-
 
297
{
312
{
298
    TDFReaderP reader        = library_reader (library);
313
    TDFReaderP reader        = library_reader(library);
299
    NStringP   const_magic   = library_magic ();
314
    NStringP   const_magic   = library_magic();
300
    unsigned   capsule_major = capsule_get_major_version ();
315
    unsigned   capsule_major = capsule_get_major_version();
301
    NStringT   magic;
316
    NStringT   magic;
302
    unsigned   major;
317
    unsigned   major;
303
    unsigned   minor;
318
    unsigned   minor;
304
 
319
 
305
    nstring_init_length (&magic, (unsigned) 4);
320
    nstring_init_length(&magic,(unsigned)4);
306
    tdf_read_bytes (reader, &magic);
321
    tdf_read_bytes(reader, &magic);
307
    if (!nstring_equal (&magic, const_magic)) {
322
    if (!nstring_equal(&magic, const_magic)) {
308
	E_library_bad_magic (library, &magic, const_magic);
323
	E_library_bad_magic(library, &magic, const_magic);
309
	THROW (XX_library_error);
324
	THROW(XX_library_error);
310
	UNREACHED;
325
	UNREACHED;
311
    }
326
    }
312
    nstring_destroy (&magic);
327
    nstring_destroy(&magic);
313
    major = tdf_read_int (reader);
328
    major = tdf_read_int(reader);
314
    minor = tdf_read_int (reader);
329
    minor = tdf_read_int(reader);
315
    debug_info_r_lib_versions (major, minor);
330
    debug_info_r_lib_versions(major, minor);
316
    if (major < 4) {
331
    if (major < 4) {
317
	E_library_bad_version (library, major);
332
	E_library_bad_version(library, major);
318
	THROW (XX_library_error);
333
	THROW(XX_library_error);
319
	UNREACHED;
334
	UNREACHED;
320
    } else if (capsule_major == 0) {
335
    } else if (capsule_major == 0) {
321
	capsule_set_major_version (major);
336
	capsule_set_major_version(major);
322
    } else if (capsule_major != major) {
337
    } else if (capsule_major != major) {
323
	E_library_version_mismatch (library, capsule_major, major);
338
	E_library_version_mismatch(library, capsule_major, major);
324
	THROW (XX_library_error);
339
	THROW(XX_library_error);
325
	UNREACHED;
340
	UNREACHED;
326
    }
341
    }
327
    library->major = major;
342
    library->major = major;
328
    library->minor = minor;
343
    library->minor = minor;
329
    tdf_read_align (reader);
344
    tdf_read_align(reader);
330
}
345
}
331
 
346
 
332
/*--------------------------------------------------------------------------*/
347
/*--------------------------------------------------------------------------*/
333
 
348
 
334
static void
349
static void
335
library_write_header PROTO_N ((library))
350
library_write_header(LibraryP library)
336
    		     PROTO_T (LibraryP library)
-
 
337
{
351
{
338
    TDFWriterP writer      = library_writer (library);
352
    TDFWriterP writer      = library_writer(library);
339
    NStringP   const_magic = library_magic ();
353
    NStringP   const_magic = library_magic();
340
    unsigned   major       = capsule_get_major_version ();
354
    unsigned   major       = capsule_get_major_version();
341
    unsigned   minor       = capsule_get_minor_version ();
355
    unsigned   minor       = capsule_get_minor_version();
342
 
356
 
343
    tdf_write_bytes (writer, const_magic);
357
    tdf_write_bytes(writer, const_magic);
344
    ASSERT (major >= 4);
358
    ASSERT(major >= 4);
345
    tdf_write_int (writer, major);
359
    tdf_write_int(writer, major);
346
    tdf_write_int (writer, minor);
360
    tdf_write_int(writer, minor);
347
    debug_info_w_lib_versions (major, minor);
361
    debug_info_w_lib_versions(major, minor);
348
    tdf_write_align (writer);
362
    tdf_write_align(writer);
349
}
363
}
350
 
364
 
351
 
365
 
352
/*--------------------------------------------------------------------------*/
366
/*--------------------------------------------------------------------------*/
353
 
367
 
354
CStringP
368
CStringP
355
lib_capsule_name PROTO_N ((capsule))
369
lib_capsule_name(LibCapsuleP capsule)
356
		 PROTO_T (LibCapsuleP capsule)
-
 
357
{
370
{
358
    return (capsule->name);
371
    return(capsule->name);
359
}
372
}
360
 
373
 
361
CStringP
374
CStringP
362
lib_capsule_full_name PROTO_N ((capsule))
375
lib_capsule_full_name(LibCapsuleP capsule)
363
		      PROTO_T (LibCapsuleP capsule)
-
 
364
{
376
{
365
    CStringP lib_name   = library_name (capsule->library);
377
    CStringP lib_name   = library_name(capsule->library);
366
    unsigned lib_length = cstring_length (lib_name);
378
    unsigned lib_length = cstring_length(lib_name);
367
    CStringP name       = lib_capsule_name (capsule);
379
    CStringP name       = lib_capsule_name(capsule);
368
    unsigned length     = cstring_length (name);
380
    unsigned length     = cstring_length(name);
369
    CStringP full_name  = ALLOCATE_VECTOR (char, lib_length + length + 3);
381
    CStringP full_name  = ALLOCATE_VECTOR(char, lib_length + length + 3);
370
    CStringP tmp        = full_name;
382
    CStringP tmp        = full_name;
371
 
383
 
372
    (void) memcpy ((GenericP) tmp, (GenericP) lib_name, (SizeT) lib_length);
384
   (void)memcpy((GenericP)tmp,(GenericP)lib_name,(SizeT)lib_length);
373
    tmp += lib_length;
385
    tmp += lib_length;
374
    *tmp = '(';
386
    *tmp = '(';
375
    tmp ++;
387
    tmp++;
376
    (void) memcpy ((GenericP) tmp, (GenericP) name, (SizeT) length);
388
   (void)memcpy((GenericP)tmp,(GenericP)name,(SizeT)length);
377
    tmp += length;
389
    tmp += length;
378
    *tmp = ')';
390
    *tmp = ')';
379
    tmp ++;
391
    tmp++;
380
    *tmp = '\0';
392
    *tmp = '\0';
381
    return (full_name);
393
    return(full_name);
382
}
394
}
383
 
395
 
384
NStringP
396
NStringP
385
lib_capsule_contents PROTO_N ((capsule))
397
lib_capsule_contents(LibCapsuleP capsule)
386
		     PROTO_T (LibCapsuleP capsule)
-
 
387
{
398
{
388
    return (&(capsule->contents));
399
    return(& (capsule->contents));
389
}
400
}
390
 
401
 
391
BoolT
402
BoolT
392
lib_capsule_is_loaded PROTO_N ((capsule))
403
lib_capsule_is_loaded(LibCapsuleP capsule)
393
    		      PROTO_T (LibCapsuleP capsule)
-
 
394
{
404
{
395
    return (capsule->loaded);
405
    return(capsule->loaded);
396
}
406
}
397
 
407
 
398
void
408
void
399
lib_capsule_loaded PROTO_N ((capsule))
409
lib_capsule_loaded(LibCapsuleP capsule)
400
    		   PROTO_T (LibCapsuleP capsule)
-
 
401
{
410
{
402
    capsule->loaded = TRUE;
411
    capsule->loaded = TRUE;
403
}
412
}
404
 
413
 
405
/*--------------------------------------------------------------------------*/
414
/*--------------------------------------------------------------------------*/
406
 
415
 
407
void
416
void
408
write_lib_capsule_full_name PROTO_N ((ostream, capsule))
417
write_lib_capsule_full_name(OStreamP    ostream,				     LibCapsuleP capsule)
409
			    PROTO_T (OStreamP    ostream X
-
 
410
				     LibCapsuleP capsule)
-
 
411
{
418
{
412
    write_cstring (ostream, library_name (capsule->library));
419
    write_cstring(ostream, library_name(capsule->library));
413
    write_char (ostream, '(');
420
    write_char(ostream, '(');
414
    write_cstring (ostream, lib_capsule_name (capsule));
421
    write_cstring(ostream, lib_capsule_name(capsule));
415
    write_char (ostream, ')');
422
    write_char(ostream, ')');
416
}
423
}
417
 
424
 
418
/*--------------------------------------------------------------------------*/
425
/*--------------------------------------------------------------------------*/
419
 
426
 
420
LibraryP
427
LibraryP
421
library_create_stream_input PROTO_N ((name))
428
library_create_stream_input(CStringP name)
422
			    PROTO_T (CStringP name)
-
 
423
{
429
{
424
    LibraryP library = ALLOCATE (LibraryT);
430
    LibraryP library = ALLOCATE(LibraryT);
425
 
431
 
426
    library->type = LT_INPUT;
432
    library->type = LT_INPUT;
427
    if (!tdf_reader_open (library_reader (library), name)) {
433
    if (!tdf_reader_open(library_reader(library), name)) {
428
	DEALLOCATE (library);
434
	DEALLOCATE(library);
429
	return (NIL (LibraryP));
435
	return(NIL(LibraryP));
430
    }
436
    }
431
    library->name     = name;
437
    library->name     = name;
432
    library->complete = FALSE;
438
    library->complete = FALSE;
433
    return (library);
439
    return(library);
434
}
440
}
435
 
441
 
436
LibraryP
442
LibraryP
437
library_create_stream_output PROTO_N ((name))
443
library_create_stream_output(CStringP name)
438
			     PROTO_T (CStringP name)
-
 
439
{
444
{
440
    LibraryP library = ALLOCATE (LibraryT);
445
    LibraryP library = ALLOCATE(LibraryT);
441
 
446
 
442
    library->type = LT_OUTPUT;
447
    library->type = LT_OUTPUT;
443
    if (!tdf_writer_open (library_writer (library), name)) {
448
    if (!tdf_writer_open(library_writer(library), name)) {
444
	DEALLOCATE (library);
449
	DEALLOCATE(library);
445
	return (NIL (LibraryP));
450
	return(NIL(LibraryP));
446
    }
451
    }
447
    library->name     = name;
452
    library->name     = name;
448
    library->complete = FALSE;
453
    library->complete = FALSE;
449
    return (library);
454
    return(library);
450
}
455
}
451
 
456
 
452
CStringP
457
CStringP
453
library_name PROTO_N ((library))
458
library_name(LibraryP library)
454
	     PROTO_T (LibraryP library)
-
 
455
{
459
{
456
    return (library->name);
460
    return(library->name);
457
}
461
}
458
 
462
 
459
unsigned
463
unsigned
460
library_num_capsules PROTO_N ((library))
464
library_num_capsules(LibraryP library)
461
		     PROTO_T (LibraryP library)
-
 
462
{
465
{
463
    return (library->num_capsules);
466
    return(library->num_capsules);
464
}
467
}
465
 
468
 
466
LibCapsuleP
469
LibCapsuleP
467
library_get_capsule PROTO_N ((library, capsule_index))
470
library_get_capsule(LibraryP library,			     unsigned capsule_index)
468
		    PROTO_T (LibraryP library X
-
 
469
			     unsigned capsule_index)
-
 
470
{
471
{
471
    ASSERT (capsule_index < library->num_capsules);
472
    ASSERT(capsule_index < library->num_capsules);
472
    return (&(library->capsules [capsule_index]));
473
    return(& (library->capsules[capsule_index]));
473
}
474
}
474
 
475
 
475
unsigned
476
unsigned
476
library_byte PROTO_N ((library))
477
library_byte(LibraryP library)
477
	     PROTO_T (LibraryP library)
-
 
478
{
478
{
479
    return (tdf_reader_byte (library_reader (library)));
479
    return(tdf_reader_byte(library_reader(library)));
480
}
480
}
481
 
481
 
482
void
482
void
483
library_content PROTO_N ((library, want_index, want_size, want_version))
483
library_content(LibraryP library,			 BoolT    want_index, 
484
		PROTO_T (LibraryP library X
-
 
485
			 BoolT    want_index X
-
 
486
			 BoolT    want_size X
484
			 BoolT    want_size, 
487
		         BoolT    want_version)
485
		         BoolT    want_version)
488
{
486
{
489
    ShapeTableP shapes = shape_table_create ();
487
    ShapeTableP shapes = shape_table_create();
490
 
488
 
491
    library_read (library, shapes);
489
    library_read(library, shapes);
492
    if (library->complete) {
490
    if (library->complete) {
493
	unsigned i;
491
	unsigned i;
494
 
492
 
495
	if (want_version) {
493
	if (want_version) {
496
	    write_char     (ostream_output, '[');
494
	    write_char    (ostream_output, '[');
497
	    write_unsigned (ostream_output, library->major);
495
	    write_unsigned(ostream_output, library->major);
498
	    write_cstring  (ostream_output, ", ");
496
	    write_cstring (ostream_output, ", ");
499
	    write_unsigned (ostream_output, library->minor);
497
	    write_unsigned(ostream_output, library->minor);
500
	    write_char     (ostream_output, ']');
498
	    write_char    (ostream_output, ']');
501
	    write_newline  (ostream_output);
499
	    write_newline (ostream_output);
502
	}
500
	}
503
	for (i = 0; i < library->num_capsules; i ++) {
501
	for (i = 0; i < library->num_capsules; i++) {
504
	    LibCapsuleP capsule = &(library->capsules [i]);
502
	    LibCapsuleP capsule = & (library->capsules[i]);
505
 
503
 
506
	    write_cstring (ostream_output, lib_capsule_name (capsule));
504
	    write_cstring(ostream_output, lib_capsule_name(capsule));
507
	    if (want_size) {
505
	    if (want_size) {
508
		NStringP body = lib_capsule_contents (capsule);
506
		NStringP body = lib_capsule_contents(capsule);
509
 
507
 
510
		write_cstring (ostream_output, " (");
508
		write_cstring(ostream_output, " (");
511
		write_unsigned (ostream_output, nstring_length (body));
509
		write_unsigned(ostream_output, nstring_length(body));
512
		write_char (ostream_output, ')');
510
		write_char(ostream_output, ')');
513
	    }
511
	    }
514
	    write_newline (ostream_output);
512
	    write_newline(ostream_output);
515
	}
513
	}
516
	if (want_index) {
514
	if (want_index) {
517
	    shape_table_iter (shapes, shape_entry_show_content,
515
	    shape_table_iter(shapes, shape_entry_show_content,
518
			      NIL (GenericP));
516
			      NIL(GenericP));
519
	}
517
	}
520
    }
518
    }
521
}
519
}
522
 
520
 
523
void
521
void
524
library_extract_all PROTO_N ((library, use_basename))
522
library_extract_all(LibraryP library,			     BoolT    use_basename)
525
		    PROTO_T (LibraryP library X
-
 
526
			     BoolT    use_basename)
-
 
527
{
523
{
528
    ShapeTableP shapes = shape_table_create ();
524
    ShapeTableP shapes = shape_table_create();
529
 
525
 
530
    library_read (library, shapes);
526
    library_read(library, shapes);
531
    if (library->complete) {
527
    if (library->complete) {
532
	unsigned i;
528
	unsigned i;
533
 
529
 
534
	for (i = 0; i < library->num_capsules; i ++) {
530
	for (i = 0; i < library->num_capsules; i++) {
535
	    LibCapsuleP capsule = &(library->capsules [i]);
531
	    LibCapsuleP capsule = & (library->capsules[i]);
536
 
532
 
537
	    library_extract_1 (capsule, use_basename);
533
	    library_extract_1(capsule, use_basename);
538
	}
534
	}
539
    }
535
    }
540
}
536
}
541
 
537
 
542
void
538
void
543
library_extract PROTO_N ((library, use_basename, match_basename, num_files,
539
library_extract(LibraryP library, BoolT use_basename, BoolT match_basename,
544
			  files))
-
 
545
		PROTO_T (LibraryP  library X
-
 
546
			 BoolT     use_basename X
-
 
547
			 BoolT     match_basename X
-
 
548
			 unsigned  num_files X
540
		unsigned num_files, CStringP *files)
549
			 CStringP *files)
-
 
550
{
541
{
551
    ShapeTableP shapes = shape_table_create ();
542
    ShapeTableP shapes = shape_table_create();
552
 
543
 
553
    library_read (library, shapes);
544
    library_read(library, shapes);
554
    if (library->complete) {
545
    if (library->complete) {
555
	unsigned i;
546
	unsigned i;
556
 
547
 
557
	for (i = 0; i < num_files; i ++) {
548
	for (i = 0; i < num_files; i++) {
558
	    BoolT    matched = FALSE;
549
	    BoolT    matched = FALSE;
559
	    unsigned j;
550
	    unsigned j;
560
 
551
 
561
	    for (j = 0; j < library->num_capsules; j ++) {
552
	    for (j = 0; j < library->num_capsules; j++) {
562
		LibCapsuleP capsule   = &(library->capsules [j]);
553
		LibCapsuleP capsule   = & (library->capsules[j]);
563
		CStringP    file_name = (files [i]);
554
		CStringP    file_name = (files[i]);
564
		CStringP    lib_name  = lib_capsule_name (capsule);
555
		CStringP    lib_name  = lib_capsule_name(capsule);
565
		CStringP    base_name = NIL (CStringP);
556
		CStringP    base_name = NIL(CStringP);
566
 
557
 
567
		if (match_basename) {
558
		if (match_basename) {
568
		    base_name = file_name_basename (lib_name);
559
		    base_name = file_name_basename(lib_name);
569
		}
560
		}
570
		if ((cstring_equal (file_name, lib_name)) ||
561
		if ((cstring_equal(file_name, lib_name)) ||
571
		    (match_basename && cstring_equal (file_name, base_name))) {
562
		   (match_basename && cstring_equal(file_name, base_name))) {
572
		    library_extract_1 (capsule, use_basename);
563
		    library_extract_1(capsule, use_basename);
573
		    matched = TRUE;
564
		    matched = TRUE;
574
		}
565
		}
575
		if (match_basename) {
566
		if (match_basename) {
576
		    DEALLOCATE (base_name);
567
		    DEALLOCATE(base_name);
577
		}
568
		}
578
	    }
569
	    }
579
	    if (!matched) {
570
	    if (!matched) {
580
		E_capsule_not_found (files [i], library_name (library));
571
		E_capsule_not_found(files[i], library_name(library));
581
	    }
572
	    }
582
	}
573
	}
583
    }
574
    }
584
}
575
}
585
 
576
 
586
void
577
void
587
library_read PROTO_N ((library, shapes))
578
library_read(LibraryP    library,		      ShapeTableP shapes)
588
	     PROTO_T (LibraryP    library X
-
 
589
		      ShapeTableP shapes)
-
 
590
{
579
{
591
    HANDLE {
580
    HANDLE {
592
	TDFReaderP reader = library_reader (library);
581
	TDFReaderP reader = library_reader(library);
593
	unsigned   library_type;
582
	unsigned   library_type;
594
 
583
 
595
	debug_info_r_start_library (library_name (library));
584
	debug_info_r_start_library(library_name(library));
596
	library_read_header (library);
585
	library_read_header(library);
597
	library_type = tdf_read_int (reader);
586
	library_type = tdf_read_int(reader);
598
	if (library_type >= LIBRARY_TYPE_JUMP_TABLE_SIZE) {
587
	if (library_type >= LIBRARY_TYPE_JUMP_TABLE_SIZE) {
599
	    E_lib_unknown_type (library, library_type);
588
	    E_lib_unknown_type(library, library_type);
600
	    THROW (XX_library_error);
589
	    THROW(XX_library_error);
601
	    UNREACHED;
590
	    UNREACHED;
602
	}
591
	}
603
	debug_info_r_library_version (library_type);
592
	debug_info_r_library_version(library_type);
604
	(*(library_type_jump_table [library_type])) (library, shapes);
593
	(*(library_type_jump_table[library_type]))(library, shapes);
605
	debug_info_r_end_library ();
594
	debug_info_r_end_library();
606
	library->complete = TRUE;
595
	library->complete = TRUE;
607
    } WITH {
596
    } WITH {
608
	ExceptionP exception = EXCEPTION_EXCEPTION ();
597
	ExceptionP exception = EXCEPTION_EXCEPTION();
609
 
598
 
610
	debug_info_r_abort_library ();
599
	debug_info_r_abort_library();
611
	if ((exception != XX_tdf_read_error) &&
600
	if ((exception != XX_tdf_read_error) &&
612
	    (exception != XX_library_error)) {
601
	   (exception != XX_library_error)) {
613
	    RETHROW ();
602
	    RETHROW();
614
	}
603
	}
615
    } END_HANDLE
604
    } END_HANDLE
616
}
605
}
617
 
606
 
618
void
607
void
619
library_write PROTO_N ((library, shapes, num_capsules, capsules))
608
library_write(LibraryP    library,		       ShapeTableP shapes, 
620
	      PROTO_T (LibraryP    library X
-
 
621
		       ShapeTableP shapes X
-
 
622
		       unsigned    num_capsules X
609
		       unsigned    num_capsules, 
623
		       CapsuleP   *capsules)
610
		       CapsuleP   *capsules)
624
{
611
{
625
    TDFWriterP writer     = library_writer (library);
612
    TDFWriterP writer     = library_writer(library);
626
    unsigned   num_shapes = 0;
613
    unsigned   num_shapes = 0;
627
    unsigned   i;
614
    unsigned   i;
628
 
615
 
629
    debug_info_w_start_library (library_name (library));
616
    debug_info_w_start_library(library_name(library));
630
    library_write_header (library);
617
    library_write_header(library);
631
    debug_info_w_library_version ((unsigned) 0);
618
    debug_info_w_library_version((unsigned)0);
632
    tdf_write_int (writer, (unsigned) 0);
619
    tdf_write_int(writer,(unsigned)0);
633
    debug_info_w_start_capsules (num_capsules);
620
    debug_info_w_start_capsules(num_capsules);
634
    tdf_write_int (writer, num_capsules);
621
    tdf_write_int(writer, num_capsules);
635
    for (i = 0; i < num_capsules; i ++) {
622
    for (i = 0; i < num_capsules; i++) {
636
	CapsuleP capsule  = capsules [i];
623
	CapsuleP capsule  = capsules[i];
637
	CStringP name     = capsule_name (capsule);
624
	CStringP name     = capsule_name(capsule);
638
	NStringP contents = capsule_contents (capsule);
625
	NStringP contents = capsule_contents(capsule);
639
	unsigned length   = nstring_length (contents);
626
	unsigned length   = nstring_length(contents);
640
	NStringT nstring;
627
	NStringT nstring;
641
 
628
 
642
	debug_info_w_capsule (name, length);
629
	debug_info_w_capsule(name, length);
643
	nstring_copy_cstring (&nstring, name);
630
	nstring_copy_cstring(&nstring, name);
644
	tdf_write_string (writer, &nstring);
631
	tdf_write_string(writer, &nstring);
645
	nstring_destroy (&nstring);
632
	nstring_destroy(&nstring);
646
	tdf_write_int (writer, length);
633
	tdf_write_int(writer, length);
647
	tdf_write_bytes (writer, contents);
634
	tdf_write_bytes(writer, contents);
648
    }
635
    }
649
    shape_table_iter (shapes, shape_entry_do_lib_count,
636
    shape_table_iter(shapes, shape_entry_do_lib_count,
650
		      (GenericP) &num_shapes);
637
		     (GenericP) &num_shapes);
651
    debug_info_w_start_index (num_shapes);
638
    debug_info_w_start_index(num_shapes);
652
    tdf_write_int (writer, num_shapes);
639
    tdf_write_int(writer, num_shapes);
653
    shape_table_iter (shapes, shape_entry_do_lib_write, (GenericP) writer);
640
    shape_table_iter(shapes, shape_entry_do_lib_write,(GenericP)writer);
654
    debug_info_w_end_library ();
641
    debug_info_w_end_library();
655
}
642
}
656
 
643
 
657
void
644
void
658
library_close PROTO_N ((library))
645
library_close(LibraryP library)
659
	      PROTO_T (LibraryP library)
-
 
660
{
646
{
661
    switch (library->type) EXHAUSTIVE {
647
    switch (library->type)EXHAUSTIVE {
662
      case CT_INPUT:
648
      case CT_INPUT:
663
	tdf_reader_close (library_reader (library));
649
	tdf_reader_close(library_reader(library));
664
	break;
650
	break;
665
      case CT_OUTPUT:
651
      case CT_OUTPUT:
666
	tdf_writer_close (library_writer (library));
652
	tdf_writer_close(library_writer(library));
667
	break;
653
	break;
668
    }
654
    }
669
}
655
}
670

656

671
/*
657
/*