Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | 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 "release.h"
33
#include "types.h"
34
#include "read_types.h"
35
#include "analyser.h"
36
#include "check.h"
37
#include "de_types.h"
38
#include "de_capsule.h"
39
#include "enc_cap.h"
40
#include "eval.h"
41
#include "file.h"
42
#include "help.h"
43
#include "node.h"
44
#include "read_cap.h"
45
#include "shape.h"
46
#include "tdf.h"
47
#include "table.h"
48
#include "utility.h"
49
#include "write.h"
50
 
51
 
52
/*
53
    PROGRAM VERSION
54
 
55
    The program name and version number are given.
56
*/
57
 
58
char *progname = "tnc" ;
59
static char *version = "Version: 1.9" ;
60
 
61
 
62
/*
63
    PROCESS AN OUTPUT OPTION
64
 
65
    The show option corresponding to arg is set to t.
66
*/
67
 
68
static boolean output_option
69
    PROTO_N ( ( arg, t ) )
70
    PROTO_T ( char *arg X boolean t )
71
{
72
    boolean *p = null ;
73
    if ( streq ( arg, "tokdecs" ) ) {
74
	p = &show_tokdecs ;
75
    } else if ( streq ( arg, "tokdefs" ) ) {
76
	p = &show_tokdefs ;
77
    } else if ( streq ( arg, "aldecs" ) ) {
78
	p = &show_aldecs ;
79
    } else if ( streq ( arg, "aldefs" ) ) {
80
	p = &show_aldefs ;
81
    } else if ( streq ( arg, "tagdecs" ) ) {
82
	p = &show_tagdecs ;
83
    } else if ( streq ( arg, "tagdefs" ) ) {
84
	p = &show_tagdefs ;
85
    }
86
    if ( p == null ) return ( 0 ) ;
87
    if ( t ) {
88
	show_tokdecs = 0 ;
89
	show_tokdefs = 0 ;
90
	show_aldefs = 0 ;
91
	show_tagdecs = 0 ;
92
	show_tagdefs = 0 ;
93
    }
94
    *p = t ;
95
    return ( 1 ) ;
96
}
97
 
98
 
99
/*
100
    MAIN ROUTINE
101
 
102
    This is the main routine.  It processes the command line arguments
103
    and calls the appropriate input and output routines.
104
*/
105
 
106
int main
107
    PROTO_N ( ( argc, argv ) )
108
    PROTO_T ( int argc X char **argv )
109
{
110
    int a ;
111
    int status = 0 ;
112
    boolean expand = 0 ;
113
    boolean evaluate = 0 ;
114
    boolean lib_input = 0 ;
115
    boolean output_next = 0 ;
116
    void ( *input_fn ) PROTO_S ( ( void ) ) ;
117
    void ( *output_fn ) PROTO_S ( ( void ) ) ;
118
 
119
    /* Default action : read text, encode TDF capsule */
120
    input_fn = read_capsule ;
121
    output_fn = enc_capsule ;
122
    text_input = 1 ;
123
    text_output = 0 ;
124
 
125
    /* Initialize internal tables */
126
    output = stdout ;
127
    init_tables () ;
128
    init_constructs () ;
129
 
130
    /* Scan arguments */
131
    for ( a = 1 ; a < argc ; a++ ) {
132
	char *arg = argv [a] ;
133
	if ( output_next ) {
134
	    open_output ( arg ) ;
135
	    output_next = 0 ;
136
	} else if ( *arg == '-' ) {
137
	    boolean known = 0 ;
138
	    switch ( arg [1] ) {
139
		case 'h' : {
140
		    /* Help option */
141
		    if ( streq ( arg, "-help" ) ) {
142
			if ( status ) warning ( "Too many arguments" ) ;
143
			a++ ;
144
			if ( a == argc ) {
145
			    help ( "all" ) ;
146
			} else {
147
			    while ( a < argc ) {
148
				help ( argv [a] ) ;
149
				a++ ;
150
			    }
151
			}
152
			exit ( exit_status ) ;
153
		    }
154
		    break ;
155
		}
156
		case 'd' : {
157
		    if ( arg [2] == 0 || streq ( arg, "-decode" ) ) {
158
			/* Decode mode */
159
			input_fn = de_capsule ;
160
			text_input = 0 ;
161
			known = 1 ;
162
		    }
163
		    break ;
164
		}
165
		case 'e' : {
166
		    if ( arg [2] == 0 || streq ( arg, "-encode" ) ) {
167
			/* Encode mode */
168
			output_fn = enc_capsule ;
169
			text_output = 0 ;
170
			known = 1 ;
171
		    } else if ( streq ( arg, "-eval" ) ) {
172
			evaluate = 1 ;
173
			known = 1 ;
174
		    } else if ( streq ( arg, "-expand" ) ) {
175
			expand = 1 ;
176
			known = 1 ;
177
		    }
178
		    break ;
179
		}
180
		case 'r' : {
181
		    if ( arg [2] == 0 || streq ( arg, "-read" ) ) {
182
			/* Read mode */
183
			input_fn = read_capsule ;
184
			text_input = 1 ;
185
			known = 1 ;
186
		    }
187
		    break ;
188
		}
189
		case 'w' : {
190
		    if ( arg [2] == 0 || streq ( arg, "-write" ) ) {
191
			/* Write mode */
192
			output_fn = print_capsule ;
193
			text_output = 1 ;
194
			known = 1 ;
195
		    }
196
		    break ;
197
		}
198
		case 'p' : {
199
		    if ( arg [2] == 0 || streq ( arg, "-print" ) ) {
200
			/* Pretty printer mode */
201
			input_fn = de_capsule ;
202
			output_fn = print_capsule ;
203
			text_input = 0 ;
204
			text_output = 1 ;
205
			known = 1 ;
206
		    }
207
		    break ;
208
		}
209
		case 't' : {
210
		    if ( arg [2] == 0 || streq ( arg, "-tsimp" ) ) {
211
			/* Expand token definitions */
212
			evaluate = 1 ;
213
			expand = 1 ;
214
			known = 1 ;
215
		    }
216
		    break ;
217
		}
218
		case 'c' : {
219
		    if ( arg [2] == 0 || streq ( arg, "-check" ) ) {
220
			/* Switch on shape checking */
221
			init_shapes () ;
222
			do_check = 1 ;
223
			known = 1 ;
224
		    } else if ( streq ( arg, "-cv" ) ) {
225
			init_shapes () ;
226
			do_check = 1 ;
227
			print_shapes = 1 ;
228
			known = 1 ;
229
		    }
230
		    break ;
231
		}
232
		case 'l' : {
233
		    if ( arg [2] == 0 || streq ( arg, "-lib" ) ) {
234
			lib_input = 1 ;
235
			known = 1 ;
236
		    }
237
		    break ;
238
		}
239
		case 'f' : {
240
		    if ( arg [2] == 0 || streq ( arg, "-func" ) ) {
241
			/* Check on form of input and output */
242
			func_input = 1 ;
243
			func_output = 1 ;
244
			known = 1 ;
245
		    } else if ( streq ( arg, "-func_out" ) ) {
246
			func_output = 1 ;
247
			known = 1 ;
248
		    } else if ( streq ( arg, "-func_in" ) ) {
249
			func_input = 1 ;
250
			known = 1 ;
251
		    }
252
		    break ;
253
		}
254
		case 'n' : {
255
		    if ( strncmp ( arg, "-no_", 4 ) == 0 ) {
256
			known = output_option ( arg + 4, 0 ) ;
257
		    }
258
		    break ;
259
		}
260
		case 'o' : {
261
		    if ( arg [2] == 0 ) {
262
			output_next = 1 ;
263
			known = 1 ;
264
		    } else if ( strncmp ( arg, "-only_", 6 ) == 0 ) {
265
			known = output_option ( arg + 6, 1 ) ;
266
		    }
267
		    break ;
268
		}
269
		case 'q' : {
270
		    if ( arg [2] == 0 ) {
271
			dont_check = 1 ;
272
			known = 1 ;
273
		    }
274
		    break ;
275
		}
276
		case 'u' : {
277
		    if ( arg [2] == 0 || streq ( arg, "-unsorted" ) ) {
278
			order_names = 0 ;
279
			known = 1 ;
280
		    }
281
		    break ;
282
		}
283
		case 'v' : {
284
		    if ( arg [2] == 0 || streq ( arg, "-version" ) ) {
285
			char *vn = version ;
286
			char *rn = RELEASE ;
287
			int v1 = VERSION_major ;
288
			int v2 = VERSION_minor ;
289
			IGNORE fprintf ( stderr, "%s: %s", progname, vn ) ;
290
			IGNORE fprintf ( stderr, " (TDF %d.%d)", v1, v2 ) ;
291
			IGNORE fprintf ( stderr, " (release %s)\n", rn ) ;
292
			known = 1 ;
293
		    }
294
		    break ;
295
		}
296
		case 'I' : {
297
		    add_directory ( arg + 2 ) ;
298
		    known = 1 ;
299
		    break ;
300
		}
301
		case 'L' : {
302
		    local_prefix = arg + 2 ;
303
		    known = 1 ;
304
		    break ;
305
		}
306
		case 'V' : {
307
		    if ( arg [2] == 0 ) {
308
			verbose = 1 ;
309
			known = 1 ;
310
		    }
311
		    break ;
312
		}
313
	    }
314
	    if ( !known ) warning ( "Unknown option, %s", arg ) ;
315
 
316
	} else {
317
	    /* Initialize input and output files */
318
	    if ( status == 0 ) {
319
		open_input ( arg, 0 ) ;
320
	    } else if ( status == 1 ) {
321
		open_output ( arg ) ;
322
	    } else {
323
		warning ( "Too many arguments" ) ;
324
	    }
325
	    status++ ;
326
	}
327
    }
328
 
329
    /* Check on library input */
330
    if ( lib_input && input_fn == de_capsule ) input_fn = de_library ;
331
 
332
    /* Perform the appropriate actions */
333
    if ( status == 0 ) fatal_error ( "Not enough arguments" ) ;
334
    ( *input_fn ) () ;
335
    if ( exit_status == EXIT_SUCCESS || text_output ) {
336
	if ( expand ) expand_all () ;
337
	if ( evaluate ) eval_all () ;
338
	sort_all () ;
339
	( *output_fn ) () ;
340
    }
341
    return ( exit_status ) ;
342
}