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 "read.h"
63
#include "calculus.h"
64
#include "check.h"
65
#include "code.h"
66
#include "common.h"
67
#include "disk.h"
68
#include "error.h"
69
#include "lex.h"
70
#include "output.h"
71
#include "pretty.h"
72
#include "print.h"
73
#include "template.h"
74
#include "token.h"
75
#include "write.h"
76
#include "xalloc.h"
77
 
78
 
79
/*
6 7u83 80
 * ACTIONS
81
 *
82
 * These macros are used to describe the various actions which the
83
 * program can perform.
84
 */
2 7u83 85
 
86
#define ACTION_C		0
87
#define ACTION_TOKEN		1
88
#define ACTION_DISK		2
89
#define ACTION_PRETTY		3
90
#define ACTION_PRINT		4
91
#define ACTION_WRITE		5
92
#define ACTION_LIST		6
93
#define ACTION_TEMPL		7
94
 
95
 
96
/*
6 7u83 97
 * LIST ACTION
98
 *
99
 * This is the trivial action which just prints a list of all the types
100
 * in the algebra.
101
 */
2 7u83 102
 
6 7u83 103
static void
104
list_action(char *nm)
2 7u83 105
{
6 7u83 106
    if (streq(nm, ".")) {
107
	output_file = stdout;
2 7u83 108
    } else {
6 7u83 109
	output_file = fopen(nm, "w");
110
	if (output_file == NULL) {
111
	    error(ERROR_SERIOUS, "Can't open output file, '%s'", nm);
112
	    return;
2 7u83 113
	}
114
    }
6 7u83 115
    LOOP_TYPE output("%TT ;\n", CRT_TYPE);
116
    flush_output();
117
    if (output_file != stdout) {
118
	    fclose_v(output_file);
119
    }
120
    return;
2 7u83 121
}
122
 
123
 
124
/*
6 7u83 125
 * MAIN ROUTINE
126
 *
127
 * This is the main routine.  It processes the command-line options,
128
 * reads the input file, and writes the output files.
129
 */
2 7u83 130
 
6 7u83 131
int
132
main(int argc, char **argv)
2 7u83 133
{
6 7u83 134
    int a;
135
    int text = 1;
136
    int no_args = 0;
137
    int last_arg = 0;
138
    int need_alg = 1;
139
    char *in = NULL;
140
    char *alg = NULL;
141
    int act = ACTION_C;
2 7u83 142
 
143
    /* Scan arguments */
6 7u83 144
    set_progname(argv[0], "1.2");
145
    for (a = 1; a < argc; a++) {
146
	char *arg = argv[a];
147
	if (arg[0]!= '-') {
148
	    last_arg = a;
149
	    no_args++;
2 7u83 150
	}
151
    }
6 7u83 152
    if (no_args == 1) {
153
	    last_arg = 0;
154
    }
2 7u83 155
 
156
    /* Process arguments */
6 7u83 157
    for (a = 1; a < argc; a++) {
158
	char *arg = argv[a];
159
	if (arg[0] == '-') {
160
	    int known;
161
	    if (arg[1] && arg[2]) {
2 7u83 162
		/* Multi-character options */
6 7u83 163
		known = 0;
164
		switch (arg[1]) {
165
		    case 'A': {
2 7u83 166
			/* Output algebra name */
6 7u83 167
			alg = arg + 2;
168
			known = 1;
169
			break;
2 7u83 170
		    }
6 7u83 171
		    case 'E': {
2 7u83 172
			/* File containing extra types */
6 7u83 173
			if (need_alg) {
174
				new_algebra();
175
			}
176
			process_file(arg + 2, 0);
177
			need_alg = 0;
178
			known = 1;
179
			break;
2 7u83 180
		    }
6 7u83 181
		    case 'T': {
2 7u83 182
			/* Template file */
6 7u83 183
			if (act == ACTION_TOKEN) {
184
				token_cond = 1;
185
			}
186
			in = arg + 2;
187
			act = ACTION_TEMPL;
188
			known = 1;
189
			break;
2 7u83 190
		    }
191
		}
192
	    } else {
193
		/* Single character options */
6 7u83 194
		known = 1;
195
		switch (arg[1]) {
2 7u83 196
		    /* Input flags */
6 7u83 197
		    case 'r': text = 0; break;
198
		    case 'i': text = 1; break;
2 7u83 199
 
200
		    /* Output flags */
6 7u83 201
		    case 'c': act = ACTION_C; break;
202
		    case 'd': act = ACTION_DISK; break;
203
		    case 'l': act = ACTION_LIST; break;
204
		    case 'o': act = ACTION_PRETTY; break;
205
		    case 'p': act = ACTION_PRINT; break;
206
		    case 't': act = ACTION_TOKEN; break;
207
		    case 'w': act = ACTION_WRITE; break;
2 7u83 208
 
209
		    /* Output options */
6 7u83 210
		    case 'a': extra_asserts = 1; break;
211
		    case 'e': extra_headers = 1; break;
212
		    case 'm': map_proto = 0; break;
213
		    case 'n': const_tokens = 0; break;
214
		    case 'x': allow_vec = 0; break;
215
		    case 'z': allow_stack = 0; break;
2 7u83 216
 
217
		    /* Other options */
6 7u83 218
		    case 'q': verbose_output = 0; break;
219
		    case 'v': report_version(); break;
220
		    default : known = 0; break;
2 7u83 221
		}
222
	    }
6 7u83 223
	    if (!known) {
224
		error(ERROR_WARNING, "Unknown option, '%s'", arg);
2 7u83 225
	    }
6 7u83 226
	} else if (a != last_arg) {
227
	    if (need_alg) {
228
		    new_algebra();
229
	    }
230
	    if (text) {
231
		process_file(arg, 1);
2 7u83 232
	    } else {
6 7u83 233
		read_file(arg);
2 7u83 234
	    }
6 7u83 235
	    check_types();
236
	    check_names(0);
237
	    need_alg = 1;
2 7u83 238
	}
239
    }
6 7u83 240
    if (no_args == 0) {
241
	    error(ERROR_FATAL, "Not enough arguments");
242
    }
243
    if (!need_alg) {
244
	    error(ERROR_SERIOUS, "Badly placed -E option");
245
    }
2 7u83 246
 
247
    /* Look up output algebra */
6 7u83 248
    if (alg) {
249
	ALGEBRA_DEFN *al = find_algebra(alg);
250
	if (al == NULL) {
251
	    error(ERROR_SERIOUS, "Algebra %s not defined", alg);
2 7u83 252
	} else {
6 7u83 253
	    algebra = al;
2 7u83 254
	}
255
    }
256
 
257
    /* Generate output */
6 7u83 258
    if (exit_status == EXIT_SUCCESS) {
259
	char *out = (last_arg ? argv[last_arg]: ".");
260
	switch (act) {
261
	    case ACTION_C: main_action_c(out); break;
262
	    case ACTION_TOKEN: main_action_tok(out); break;
263
	    case ACTION_DISK: disk_action(out); break;
264
	    case ACTION_PRETTY: pretty_file(out); break;
265
	    case ACTION_PRINT: print_action(out); break;
266
	    case ACTION_WRITE: write_file(out); break;
267
	    case ACTION_LIST: list_action(out); break;
268
	    case ACTION_TEMPL: template_file(in, out); break;
2 7u83 269
	}
270
    } else {
6 7u83 271
	error(ERROR_FATAL, "No output generated due to previous errors");
2 7u83 272
    }
6 7u83 273
    return(exit_status);
2 7u83 274
}