Subversion Repositories tendra.SVN

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "read.h"
33
#include "calculus.h"
34
#include "check.h"
35
#include "code.h"
36
#include "common.h"
37
#include "disk.h"
38
#include "error.h"
39
#include "lex.h"
40
#include "output.h"
41
#include "pretty.h"
42
#include "print.h"
43
#include "template.h"
44
#include "token.h"
45
#include "write.h"
46
#include "xalloc.h"
47
 
48
 
49
/*
50
    ACTIONS
51
 
52
    These macros are used to describe the various actions which the
53
    program can perform.
54
*/
55
 
56
#define ACTION_C		0
57
#define ACTION_TOKEN		1
58
#define ACTION_DISK		2
59
#define ACTION_PRETTY		3
60
#define ACTION_PRINT		4
61
#define ACTION_WRITE		5
62
#define ACTION_LIST		6
63
#define ACTION_TEMPL		7
64
 
65
 
66
/*
67
    LIST ACTION
68
 
69
    This is the trivial action which just prints a list of all the types
70
    in the algebra.
71
*/
72
 
73
static void list_action
74
    PROTO_N ( ( nm ) )
75
    PROTO_T ( char *nm )
76
{
77
    if ( streq ( nm, "." ) ) {
78
	output_file = stdout ;
79
    } else {
80
	output_file = fopen ( nm, "w" ) ;
81
	if ( output_file == NULL ) {
82
	    error ( ERROR_SERIOUS, "Can't open output file, '%s'", nm ) ;
83
	    return ;
84
	}
85
    }
86
    LOOP_TYPE output ( "%TT ;\n", CRT_TYPE ) ;
87
    flush_output () ;
88
    if ( output_file != stdout ) fclose_v ( output_file ) ;
89
    return ;
90
}
91
 
92
 
93
/*
94
    MAIN ROUTINE
95
 
96
    This is the main routine.  It processes the command-line options,
97
    reads the input file, and writes the output files.
98
*/
99
 
100
int main
101
    PROTO_N ( ( argc, argv ) )
102
    PROTO_T ( int argc X char **argv )
103
{
104
    int a ;
105
    int text = 1 ;
106
    int no_args = 0 ;
107
    int last_arg = 0 ;
108
    int need_alg = 1 ;
109
    char *in = NULL ;
110
    char *alg = NULL ;
111
    int act = ACTION_C ;
112
 
113
    /* Scan arguments */
114
    set_progname ( argv [0], "1.2" ) ;
115
    for ( a = 1 ; a < argc ; a++ ) {
116
	char *arg = argv [a] ;
117
	if ( arg [0] != '-' ) {
118
	    last_arg = a ;
119
	    no_args++ ;
120
	}
121
    }
122
    if ( no_args == 1 ) last_arg = 0 ;
123
 
124
    /* Process arguments */
125
    for ( a = 1 ; a < argc ; a++ ) {
126
	char *arg = argv [a] ;
127
	if ( arg [0] == '-' ) {
128
	    int known ;
129
	    if ( arg [1] && arg [2] ) {
130
		/* Multi-character options */
131
		known = 0 ;
132
		switch ( arg [1] ) {
133
		    case 'A' : {
134
			/* Output algebra name */
135
			alg = arg + 2 ;
136
			known = 1 ;
137
			break ;
138
		    }
139
		    case 'E' : {
140
			/* File containing extra types */
141
			if ( need_alg ) new_algebra () ;
142
			process_file ( arg + 2, 0 ) ;
143
			need_alg = 0 ;
144
			known = 1 ;
145
			break ;
146
		    }
147
		    case 'T' : {
148
			/* Template file */
149
			if ( act == ACTION_TOKEN ) token_cond = 1 ;
150
			in = arg + 2 ;
151
			act = ACTION_TEMPL ;
152
			known = 1 ;
153
			break ;
154
		    }
155
		}
156
	    } else {
157
		/* Single character options */
158
		known = 1 ;
159
		switch ( arg [1] ) {
160
		    /* Input flags */
161
		    case 'r' : text = 0 ; break ;
162
		    case 'i' : text = 1 ; break ;
163
 
164
		    /* Output flags */
165
		    case 'c' : act = ACTION_C ; break ;
166
		    case 'd' : act = ACTION_DISK ; break ;
167
		    case 'l' : act = ACTION_LIST ; break ;
168
		    case 'o' : act = ACTION_PRETTY ; break ;
169
		    case 'p' : act = ACTION_PRINT ; break ;
170
		    case 't' : act = ACTION_TOKEN ; break ;
171
		    case 'w' : act = ACTION_WRITE ; break ;
172
 
173
		    /* Output options */
174
		    case 'a' : extra_asserts = 1 ; break ;
175
		    case 'e' : extra_headers = 1 ; break ;
176
		    case 'm' : map_proto = 0 ; break ;
177
		    case 'n' : const_tokens = 0 ; break ;
178
		    case 'x' : allow_vec = 0 ; break ;
179
		    case 'z' : allow_stack = 0 ; break ;
180
 
181
		    /* Other options */
182
		    case 'q' : verbose_output = 0 ; break ;
183
		    case 'v' : report_version () ; break ;
184
		    default : known = 0 ; break ;
185
		}
186
	    }
187
	    if ( !known ) {
188
		error ( ERROR_WARNING, "Unknown option, '%s'", arg ) ;
189
	    }
190
	} else if ( a != last_arg ) {
191
	    if ( need_alg ) new_algebra () ;
192
	    if ( text ) {
193
		process_file ( arg, 1 ) ;
194
	    } else {
195
		read_file ( arg ) ;
196
	    }
197
	    check_types () ;
198
	    check_names ( 0 ) ;
199
	    need_alg = 1 ;
200
	}
201
    }
202
    if ( no_args == 0 ) error ( ERROR_FATAL, "Not enough arguments" ) ;
203
    if ( !need_alg ) error ( ERROR_SERIOUS, "Badly placed -E option" ) ;
204
 
205
    /* Look up output algebra */
206
    if ( alg ) {
207
	ALGEBRA_DEFN *al = find_algebra ( alg ) ;
208
	if ( al == NULL ) {
209
	    error ( ERROR_SERIOUS, "Algebra %s not defined", alg ) ;
210
	} else {
211
	    algebra = al ;
212
	}
213
    }
214
 
215
    /* Generate output */
216
    if ( exit_status == EXIT_SUCCESS ) {
217
	char *out = ( last_arg ? argv [ last_arg ] : "." ) ;
218
	switch ( act ) {
219
	    case ACTION_C : main_action_c ( out ) ; break ;
220
	    case ACTION_TOKEN : main_action_tok ( out ) ; break ;
221
	    case ACTION_DISK : disk_action ( out ) ; break ;
222
	    case ACTION_PRETTY : pretty_file ( out ) ; break ;
223
	    case ACTION_PRINT : print_action ( out ) ; break ;
224
	    case ACTION_WRITE : write_file ( out ) ; break ;
225
	    case ACTION_LIST : list_action ( out ) ; break ;
226
	    case ACTION_TEMPL : template_file ( in, out ) ; break ;
227
	}
228
    } else {
229
	error ( ERROR_FATAL, "No output generated due to previous errors" ) ;
230
    }
231
    return ( exit_status ) ;
232
}