datadec.c 1.71 KB
Newer Older
dcw's avatar
dcw committed
1
2
3
4
5
6
7
8
9
10
/*
 *                      DATA DECLARATION BUILDER
 *                      ==== =========== =======
 *
 ******* Author:
 *
 *      Duncan White, Imperial College, London, England.
 *
 ******* Description:
 *
11
12
13
 *      This program builds C data declarations, construction functions
 *	and print functions from a series of HOPE/Miranda style recursive
 *	data declarations. (with hints on printing)
dcw's avatar
dcw committed
14
 *
15
 *      The output produced is placed in pair of files
dcw's avatar
dcw committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 *      ( x.c and x.h ) which together form a module provided the
 *      relevent data types.
 */

#include <dcw.h>
#include "struct.h"
#include "lexer.h"
#include "parser.h"


main( argc, argv ) int argc; char **argv;
{
        FILE		*infile, *cfile, *hfile;
        char		inname[256], tempname[256];
        declnlist	declns;
	int		len;
	char		*basename;

34
        ASSERT( argc==2 || argc==3, ("Usage: datadec outfile [infile]\n") );
dcw's avatar
dcw committed
35
36
37
38
39
40
41
42
43

	basename = argv[1];
	len = strlen( basename );
	if( !strcmp( basename+len-2, ".c" ) ) {
		basename[len-2] = '\0';
	}

        sprintf( tempname, "%s.c", basename );
        cfile = fopen( tempname, "w" );
44
        ASSERT( cfile != NULL, ("datadec: can't create '%s'\n",tempname) );
dcw's avatar
dcw committed
45
46
47

        sprintf( tempname, "%s.h", basename );
        hfile = fopen( tempname, "w" );
48
        ASSERT( hfile != NULL, ("datadec: can't create '%s'\n",tempname) );
dcw's avatar
dcw committed
49
50
51
52

        if( argc == 3 ) {
                lexfile = fopen( argv[2], "r" );
                ASSERT( lexfile != NULL,
53
			("datadec: can't open '%s'\n",argv[2]) );
dcw's avatar
dcw committed
54
55
56
57
        } else {
                lexfile = stdin;
        }
        ASSERT( parse_declns( &declns ),
58
		("datadec: can't parse input properly\n") );
dcw's avatar
dcw committed
59
60
61
62
63
        make_declns( declns, cfile, hfile, basename );
	fclose( cfile );
	fclose( hfile );
	exit(0);
}