#include        <stdio.h>
#include        "c.h"
#include        "expr.h"
#include        "gen.h"
#include        "cglbdec.h"

#define VERSION	"Version 0.2 October 1, 1987"
/*
 *68000 C compiler
 *
 *Copyright 1984, 1985, 1986 Matthew Brandt.
 *  all commercial rights reserved.
 *
 *This compiler is intended as an instructive tool for personal use. Any
 *use for profit without the written consent of the author is prohibited.
 *
 *This compiler may be distributed freely for non-commercial use as long
 *as this notice stays intact. Please forward any enhancements or questions
 *to:
 *
 *Matthew Brandt
 *Box 920337
 *Norcross, Ga 30092
 */

char            infile[20],
                listfile[20],
                outfile[20];
extern TABLE    tagtable;
int		mainflag;
extern int      total_errors;

main(argc,argv)
int     argc;
char    **argv;
{       while(--argc) {
                if( **++argv == '-')
                        options(*argv);
                else if( openfiles(*argv)) {
                        lineno = 0;
                        initsym();
                        getch();
                        getsym();
                        compile();
                        summary();
                        release_global();
                        closefiles();
                        }
                }
}

static int options(s)
char*s;
{
	if ( strcmp(s, "-n") == 0 || strcmp(s, "-N") == 0 )
	  Options.Optimize = !Options.Optimize;
	else if ( strcmp(s, "-l") == 0 || strcmp(s, "-L") == 0 )
	  Options.List = !Options.List;
}

static int     openfiles(s)
char *s;
{
        int     ofl;
	FILE 	*fdopen();

	printf("PDC Public Domain Compiler - %s\n", VERSION );
	printf("Copyright 1984, 1985, 1986 By Mathew Brandt.\n");
	printf("Amiga port by Jeff Lydiatt.\n");
	printf("Freely Distributable for non-commercial use.\n\n");
        strcpy(infile,s);
        strcpy(listfile,s);
        strcpy(outfile,s);
        makename(listfile,".lis");
        makename(outfile,".s");
        if( (input = fopen(infile,"r")) == 0) {
                printf(" cant open %s\n",infile);
                return 0;
                }
        ofl = creat(outfile,-1);
        if( ofl < 0 )
                {
                printf(" cant create %s\n",outfile);
                fclose(input);
                return 0;
                }
        if( (output = fdopen(ofl,"w")) == NULL) {
                printf(" cant open %s\n",outfile);
                fclose(input);
                return 0;
                }
	if ( Options.List )
           if( (list = fopen(listfile,"w")) == 0) {
                printf(" cant open %s\n",listfile);
                fclose(input);
                fclose(output);
                return 0;
                }
        return 1;
}

static makename(s,e)
char    *s, *e;
{       while(*s != 0 && *s != '.')
                ++s;
        while(*s++ = *e++);
}

static summary()
{   printf("\n -- %d errors found.",total_errors);
    if ( Options.List )
       fprintf(list,"\f\n *** global scope symbol table ***\n\n");
    list_table(&gsyms,0);
    if ( Options.List )
        fprintf(list,"\n *** structures and unions ***\n\n");
    list_table(&tagtable,0);
    fprintf(output,"\tEND\n");
}

static closefiles()
{       fclose(input);
        fclose(output);
	if (Options.List)
           fclose(list);
}

