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