datadec.c 2.15 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
 *      This program builds C data declarations, construction functions
 *	and print functions from a series of HOPE/Miranda style recursive
dcw's avatar
dcw committed
13
 *	data declarations. (with optional hints on printing)
dcw's avatar
dcw committed
14
 *
dcw's avatar
dcw committed
15
16
 *      The output produced is placed in pair of files (eg. x.c and x.h )
 *	which together form a module provided the relevent data types.
dcw's avatar
dcw committed
17
18
19
20
21
22
23
24
 */

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


dcw's avatar
dcw committed
25
26
27
28
29
30
31
#define MUSTBE(b)	ASSERT(b,("Usage: datadec [-v] outfile [infile]\n"))

#define NEED_ANOTHER_ARG	MUSTBE( IS_ANOTHER_ARG )
#define REQUIRE_NO_MORE_ARGS	MUSTBE( argc == arg )
#define IS_ANOTHER_ARG		(argc > arg)


dcw's avatar
dcw committed
32
33
main( argc, argv ) int argc; char **argv;
{
dcw's avatar
dcw committed
34
35
        FILE		*cfile, *hfile;
        char		tempname[256];
dcw's avatar
dcw committed
36
37
38
        declnlist	declns;
	int		len;
	char		*basename;
dcw's avatar
dcw committed
39
40
	BOOL		verbose;
	int		arg;
dcw's avatar
dcw committed
41

dcw's avatar
dcw committed
42
43
44
45
46
47
48
49
50
	arg = 1;
	NEED_ANOTHER_ARG;

	verbose = FALSE;
	if( strcmp( argv[arg], "-v" )==0 )
	{
		arg++;
		verbose = TRUE;
	}
dcw's avatar
dcw committed
51

dcw's avatar
dcw committed
52
53
54
	NEED_ANOTHER_ARG;

	basename = argv[arg++];
dcw's avatar
dcw committed
55
	len = strlen( basename );
dcw's avatar
dcw committed
56
57
	if( !strcmp( basename+len-2, ".c" ) )
	{
dcw's avatar
dcw committed
58
59
60
61
62
		basename[len-2] = '\0';
	}

        sprintf( tempname, "%s.c", basename );
        cfile = fopen( tempname, "w" );
63
        ASSERT( cfile != NULL, ("datadec: can't create '%s'\n",tempname) );
dcw's avatar
dcw committed
64
65
66

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

dcw's avatar
dcw committed
69
70
        if( IS_ANOTHER_ARG ) {
                lexfile = fopen( argv[arg], "r" );
dcw's avatar
dcw committed
71
                ASSERT( lexfile != NULL,
dcw's avatar
dcw committed
72
73
			("datadec: can't open '%s'\n",argv[arg]) );
		arg++;
dcw's avatar
dcw committed
74
75
76
        } else {
                lexfile = stdin;
        }
dcw's avatar
dcw committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

	REQUIRE_NO_MORE_ARGS;

        if( parse_declns( &declns ) )
	{
		if( verbose )
		{
			printf( "parsed declns: they are:\n\n" );
			print_declnlist( declns );
		}
		make_declns( declns, cfile, hfile, basename );
	} else
	{
		fprintf( stderr, "datadec: can't parse input properly\n" );
	}
dcw's avatar
dcw committed
92
93
94
95
	fclose( cfile );
	fclose( hfile );
	exit(0);
}