Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
6 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
6 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
6 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
6 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "external.h"
63
#include "filename.h"
64
#include "list.h"
65
#include "execute.h"
66
#include "flags.h"
67
#include "startup.h"
68
#include "utility.h"
69
 
70
 
71
/*
6 7u83 72
 * THE STARTUP AND ENDUP FILES
73
 *
74
 * These variables give the names and file descriptors for the startup and
75
 * endup files, plus the command-line options to pass them to the producer.
76
 */
2 7u83 77
 
6 7u83 78
static FILE *startup_file = null, *endup_file = null;
79
static char *startup_name = null, *endup_name = null;
80
char *startup_opt = null, *endup_opt = null;
2 7u83 81
 
82
 
83
/*
6 7u83 84
 * ADD A MESSAGE TO THE STARTUP FILE
85
 *
86
 * This routine prints the message s to the tcc startup file.
87
 */
2 7u83 88
 
6 7u83 89
void
90
add_to_startup(char *s)
2 7u83 91
{
6 7u83 92
	if (startup_name == null) {
93
		startup_name = temp_name(temporary_dir, "ts");
94
		startup_opt = string_concat("-f", startup_name);
2 7u83 95
	}
6 7u83 96
	opt_startup = add_item(opt_startup, s);
97
	if (dry_run) {
98
		return;
99
	}
100
	if (startup_file == null) {
101
		startup_file = fopen(startup_name, "a");
102
		if (startup_file == null) {
103
			error(SERIOUS, "Can't open startup file, '%s'",
104
			      startup_name);
105
			return;
106
		}
107
		IGNORE fprintf(startup_file, "#line 1 \"%s\"\n", name_h_file);
108
	}
109
	IGNORE fputs(s, startup_file);
110
	return;
2 7u83 111
}
112
 
113
 
114
/*
6 7u83 115
 * ADD A MESSAGE TO THE ENDUP FILE
116
 *
117
 * This routine prints the message s to the tcc endup file.
118
 */
2 7u83 119
 
6 7u83 120
void
121
add_to_endup(char *s)
2 7u83 122
{
6 7u83 123
	if (endup_name == null) {
124
		endup_name = temp_name(temporary_dir, "te");
125
		startup_opt = string_concat("-e", endup_name);
2 7u83 126
	}
6 7u83 127
	opt_endup = add_item(opt_endup, s);
128
	if (dry_run) {
129
		return;
130
	}
131
	if (endup_file == null) {
132
		endup_file = fopen(endup_name, "a");
133
		if (endup_file == null) {
134
			error(SERIOUS, "Can't open endup file, '%s'",
135
			      endup_name);
136
			return;
137
		}
138
		IGNORE fprintf(endup_file, "#line 1 \"%s\"\n", name_E_file);
139
	}
140
	IGNORE fputs(s, endup_file);
141
	return;
2 7u83 142
}
143
 
144
 
145
/*
6 7u83 146
 * THE TOKEN DEFINITION FILE
147
 *
148
 * This file is used to hold TDF notation for the definition of the
149
 * command-line tokens.
150
 */
2 7u83 151
 
6 7u83 152
static FILE *tokdef_file = null;
153
char *tokdef_name = null;
2 7u83 154
 
155
 
156
/*
6 7u83 157
 * ADD A MESSAGE TO THE TOKEN DEFINITION FILE
158
 *
159
 * This routine prints the message s to the tcc token definition file.
160
 */
2 7u83 161
 
6 7u83 162
static void
163
add_to_tokdef(char *s)
2 7u83 164
{
6 7u83 165
	if (tokdef_name == null) {
166
		tokdef_name = temp_name(temporary_dir, "td");
2 7u83 167
	}
6 7u83 168
	if (dry_run) {
169
		return;
170
	}
171
	if (tokdef_file == null) {
172
		tokdef_file = fopen(tokdef_name, "a");
173
		if (tokdef_file == null) {
174
			error(SERIOUS, "Can't open token definition file, '%s'",
175
			      tokdef_name);
176
			return;
177
		}
178
		IGNORE fputs("( make_tokdec ~char variety )\n", tokdef_file);
179
		IGNORE fputs("( make_tokdec ~signed_int variety )\n\n",
180
			     tokdef_file);
181
	}
182
	IGNORE fputs(s, tokdef_file);
183
	return;
2 7u83 184
}
185
 
186
 
187
/*
6 7u83 188
 * CLOSE THE STARTUP AND ENDUP FILES
189
 *
190
 * This routine closes the startup and endup files.
191
 */
2 7u83 192
 
6 7u83 193
void
194
close_startup(void)
2 7u83 195
{
6 7u83 196
	if (startup_file) {
197
		IGNORE fclose(startup_file);
198
		startup_file = null;
199
	}
200
	if (endup_file) {
201
		IGNORE fclose(endup_file);
202
		endup_file = null;
203
	}
204
	if (tokdef_file) {
205
		IGNORE fclose(tokdef_file);
206
		tokdef_file = null;
207
	}
208
	return;
2 7u83 209
}
210
 
211
 
212
/*
6 7u83 213
 * CLEAN UP THE STARTUP AND ENDUP FILES
214
 *
215
 * This routine is called before the program terminates either to remove the
216
 * tcc startup and endup files or to move them if they are to be preserved.
217
 */
2 7u83 218
 
6 7u83 219
void
220
remove_startup(void)
2 7u83 221
{
6 7u83 222
	if (keeps[STARTUP_FILE]) {
223
		if (startup_name) {
224
			cmd_list(exec_move);
225
			cmd_string(startup_name);
226
			cmd_string(name_h_file);
227
			IGNORE execute(no_filename, no_filename);
228
		}
229
		if (endup_name) {
230
			cmd_list(exec_move);
231
			cmd_string(endup_name);
232
			cmd_string(name_E_file);
233
			IGNORE execute(no_filename, no_filename);
234
		}
235
		if (tokdef_name) {
236
			cmd_list(exec_move);
237
			cmd_string(tokdef_name);
238
			cmd_string(name_p_file);
239
			IGNORE execute(no_filename, no_filename);
240
		}
241
	} else {
242
		if (startup_name) {
243
			cmd_list(exec_remove);
244
			cmd_string(startup_name);
245
			IGNORE execute(no_filename, no_filename);
246
		}
247
		if (endup_name) {
248
			cmd_list(exec_remove);
249
			cmd_string(endup_name);
250
			IGNORE execute(no_filename, no_filename);
251
		}
252
		if (tokdef_name) {
253
			cmd_list(exec_remove);
254
			cmd_string(tokdef_name);
255
			IGNORE execute(no_filename, no_filename);
256
		}
2 7u83 257
	}
6 7u83 258
	return;
2 7u83 259
}
260
 
261
 
262
/*
6 7u83 263
 * DEAL WITH STARTUP PRAGMA OPTIONS
264
 *
265
 * This routine translates command-line compilation mode options into the
266
 * corresponding pragma statements.
267
 */
2 7u83 268
 
6 7u83 269
void
270
add_pragma(char *s)
2 7u83 271
{
6 7u83 272
	char *e;
273
	char *level = "warning";
274
	static char *start_scope = "#pragma TenDRA begin\n";
275
	if (start_scope) {
276
		add_to_startup(start_scope);
277
		start_scope = null;
278
	}
279
	e = strchr(s, '=');
280
	if (e) {
281
		level = e + 1;
282
		*e = 0;
283
	}
2 7u83 284
 
6 7u83 285
	/* Write option to startup file */
286
	add_to_startup("#pragma TenDRA option \"");
287
	add_to_startup(s);
288
	add_to_startup("\" ");
289
	add_to_startup(level);
290
	add_to_startup("\n");
291
	return;
2 7u83 292
}
293
 
294
 
295
/*
6 7u83 296
 * DEAL WITH STARTUP TOKEN OPTIONS
297
 *
298
 * This routine translates command-line token definition options into the
299
 * corresponding pragma statements.
300
 */
2 7u83 301
 
6 7u83 302
void
303
add_token(char *s)
2 7u83 304
{
6 7u83 305
	char *type = "int";
306
	char *defn = "1";
307
	char *e = strchr(s, '=');
308
	if (e) {
309
		defn = e + 1;
310
		*e = 0;
311
	}
2 7u83 312
 
6 7u83 313
	/* Write token description to startup file */
314
	add_to_startup("#pragma token EXP const : ");
315
	add_to_startup(type);
316
	add_to_startup(" : ");
317
	add_to_startup(s);
318
	add_to_startup(" #\n");
319
	add_to_startup("#pragma interface ");
320
	add_to_startup(s);
321
	add_to_startup("\n");
2 7u83 322
 
6 7u83 323
	/* Write definition to token definition file */
324
	add_to_tokdef("( make_tokdef ");
325
	add_to_tokdef(s);
326
	add_to_tokdef(" exp\n");
327
	add_to_tokdef("  ( make_int ~signed_int ");
328
	add_to_tokdef(defn);
329
	add_to_tokdef(" ) )\n\n");
330
	return;
2 7u83 331
}