Skip to content
Snippets Groups Projects
datadec.c 2.4 KiB
Newer Older
dcw's avatar
dcw committed
/*
 *                      DATA DECLARATION BUILDER
 *                      ==== =========== =======
 *
 ******* Author:
 *
 *      Duncan White, Imperial College, London, England.
 *
 ******* Description:
 *
 *      This program builds C data declarations, construction functions
 *	and print functions from a series of HOPE/Miranda style recursive
dcw's avatar
dcw committed
 *	data declarations. (with optional hints on printing)
dcw's avatar
dcw committed
 *
dcw's avatar
dcw committed
 *      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
 */

#include <dcw.h>
#include "struct.h"
#include "lexer.h"
#include "parser.h"
dcw's avatar
dcw committed
#include "decs.h"
dcw's avatar
dcw committed


#define MUSTBE(b)	ASSERT(b,("Usage: datadec [-vpo] outfile [infile]\n"))
dcw's avatar
dcw committed

#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
main( argc, argv ) int argc; char **argv;
{
dcw's avatar
dcw committed
        FILE		*cfile, *hfile;
        char		tempname[256];
dcw's avatar
dcw committed
	char		*basename;
	char		*s;
dcw's avatar
dcw committed
        declnlist	declns;
	int		len;
dcw's avatar
dcw committed
	int		arg;
dcw's avatar
dcw committed

dcw's avatar
dcw committed
	arg = 1;
	NEED_ANOTHER_ARG;

	verbose = FALSE; print = FALSE; opt = FALSE;
dcw's avatar
dcw committed
	while( *(s=argv[arg]) == '-' )
dcw's avatar
dcw committed
	{
dcw's avatar
dcw committed
		for( s++; *s; s++ )
		{
			switch( *s )
			{
			case 'v':
				verbose = TRUE;
				break;
			case 'p':
				print = TRUE;
				break;
			case 'o':
				opt = TRUE;
				break;
dcw's avatar
dcw committed
			default:
				ABORT( ("datadec: illegal option -%c\n",*s) );
			}
		}
dcw's avatar
dcw committed
		arg++;
dcw's avatar
dcw committed
		NEED_ANOTHER_ARG;
dcw's avatar
dcw committed
	}
dcw's avatar
dcw committed

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

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

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

dcw's avatar
dcw committed
        if( IS_ANOTHER_ARG ) {
                lexfile = fopen( argv[arg], "r" );
dcw's avatar
dcw committed
                ASSERT( lexfile != NULL,
dcw's avatar
dcw committed
			("datadec: can't open '%s'\n",argv[arg]) );
		arg++;
dcw's avatar
dcw committed
        } else {
                lexfile = stdin;
        }
dcw's avatar
dcw committed

	REQUIRE_NO_MORE_ARGS;

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