/********************************************************/
/*							*/
/*		Q/C Compiler Version 3.2		*/
/*			(Part 9)			*/
/*							*/
/*     Copyright (c) 1984 Quality Computer Systems	*/
/*							*/
/*			11/24/84			*/
/********************************************************/

/* NOTE: This module becomes part of the free space after it
 *		is used if you link Q/C using PLINK-II
 */
#include "stdio.h"
#include "cstddef.h"
#include "cglbdecl.c"

/* Initialization code */
init()
{
    qprintf("%s%s%s\n", signon, version, copyright);
    minsym = nsym;
    minlit = litpsize;
}

/* Get command line options */
getoptions(argc, argv)
    int             argc;
    char           *argv[];

{
    register char  *p;
    char           *index(), *strcat(), *strcpy();
    static          rename = FALSE;

    if (argc < 2)		/* must have one input file */
	usagerr();
    nextfil = argv + 1;
    while (--argc)
    {
	if (**++argv == '-')
	    break;		/* stop at options */
	++ninfils;
    }
    if (ninfils == 0)
	usagerr();
    getfilename(*nextfil, infil, "C");
    while (argc--)
    {				/* look for options */
	p = *argv++;
	if (*p == '-')
	{
	    while (*++p)
	    {
		switch (chlower(*p))
		{
		case 'c':	/* commented asm listing */
		    fullist = TRUE;
		    continue;
		case 'd':	/* output to console (debug) */
		    output = stderr;
		    continue;
		case 'i':	/* toggle initialization of */
		    initflag = !initflag;	/* arrays>INITSIZE */
		    continue;
		case 'l':	/* generate a library */
		    libflag = TRUE;
		    continue;
		case 'o':	/* rename output file */
		    rename = 1;
		    continue;
		case 'r':	/* toggle redirection flag */
		    redirect = !redirect;
		    continue;
		case 's':	/* exclude shell in ROM code */
		    romflag = TRUE;
		    lptr = p + 1;	/* look for stk addr */
		    spaddr = gethex();
		    break;
		case 't':	/* generate trace messages */
		    trace.is_on = trace.enabled = TRUE;
		    continue;
		case 'v':	/* toggle verbose/terse */
		    verbose = !verbose;
		    continue;
		default:
		    usagerr();
		}
		break;		/* break out of -S switch */
	    }
	} else if (rename == 1)
	{			/* should be new output file name */
	    if (p[1] == ':' && p[2] == '\0')
	    {
		strcpy(outfil, p);	/* build drive:infil.defext */
		strcat(outfil, infil[1] == ':' ? infil + 2 : infil);
		*(index(outfil, '.') + 1) = '\0';
		strcat(outfil, defext);
	    } else
		getfilename(p, outfil, defext);
	    if (streq(infil, outfil))
	    {
		puts("Output file name same as input");
		exit(1);
	    }
	    rename = 2;		/* remember we did it */
	} else
	    usagerr();
    }
    if (!rename && output == 0)
    {				/* use infil.defext as output */
	strcpy(outfil, infil);
	*(index(outfil, '.') + 1) = '\0';	/* end name after '.' */
	strcat(outfil, defext);	/* add default extension */
    } else if (rename == 1)	/* new file name not found */
	usagerr();
}

usagerr()
{
#if Z80
    puts("Usage:cc infile ... -cdilortv -sxxxx outfile");
#else
    puts("Usage:cc infile ... -acdilmortv -sxxxx outfile");
#endif
    exit(1);
}

/* Obtain compiler table and buffer space from free space */
getspace()
{
    char           *sbrk();
    struct st      *j;

    peepbuf = sbrk(PEEPBUFSIZE);
    line = sbrk(LINESIZE);
    mline = sbrk(LINESIZE);
    pcasetab =
	    casetab =
	    (struct case_table *)
	    sbrk(ncases * sizeof(struct case_table));
    pswq =
	    swq =
	    (struct swq *) sbrk(nswq * sizeof(struct swq));
    typelist =
	    (struct typeinfo *)
	    sbrk(n_types * sizeof(struct typeinfo));
    inbuf = sbrk(iobufsize);
    outbuf = sbrk(iobufsize);
    freemem =
	    memtab =
	    (struct memtab *) sbrk(nmem * sizeof(struct memtab));
    plitpool =
	    litpool =
	    sbrk(litpsize);
    symtab =
	    (struct st *) sbrk(nsym * sizeof(struct st));
    if ((int) symtab == -1 || (int) litpool == -1)
    {
	puts("Not enough table space");
	exit(1);
    }
    for (j = symtab; j < symtab + nsym; ++j)
	j->st_decl = DECL_VIRGIN;
}

/* end of CC9.C */
