main.c 3.46 KB
/*
 * Main, initialization, error routines
 */

#include "ic.h"
#include "y.tab.h"

/* Global variables */
struct decl     *errortype;     /* for use as dummy decl when error in parse */
struct decl     *errorvar;

struct id	*returnid;      /* fake return variable for functions. */

int verbose = FALSE;        /* verbose output flag */

void initreserved(void)
{
    enter(use, "use", 3);
    
    enter(bank, "bank", 4);
    enter(program, "program", 7);
    enter(instrument, "instrument", 10);    
    enter(sound, "sound", 5);    
    enter(wavetable, "wavetable", 9);    

    enter(volume, "volume", 6);    
    enter(pan, "pan", 3);    

    enter(keymap, "keymap", 6);
    enter(keyMin, "keyMin", 6);
    enter(keyMax, "keyMax", 6);
    enter(keyBase, "keyBase", 7);
    enter(velocityMin, "velocityMin", 11);
    enter(velocityMax, "velocityMax", 11);
    enter(detune, "detune", 6);

    enter(envelope, "envelope", 8);
    enter(attackTime, "attackTime", 10);
    enter(attackVolume, "attackVolume", 9);
    enter(decayTime, "decayTime", 9);
    enter(decayVolume, "decayVolume", 8);
    enter(releaseTime, "releaseTime", 11);
    enter(releaseVolume, "releaseVolume", 10);
}

void pline(void)
{
    fprintf(stdout, "Line %d: ", yylineno);
}

void yyerror(char *s) 
{ 
    pline(); 
    fprintf(stdout, "%s: ", s);
    fprintf(stdout, "Last token was %s\n", yytext);
}

/* Initialize some base types.  Set globals */
void inittypes(void)
{
}

void initoutput(void)
{
}

static char usage[] = "[-verbose] infile";

FILE    *bnkFile;
FILE    *tblFile;

main(int argc, char **argv)
{
    extern char         *optarg;
    extern int          optind;
    int                 errflg=0;
    int                 c;
    int	                return_val;
    char                *ofile;
    char                *bfname;
    char                *tfname;
    
    /*
     * process command line arguments
     */
    while ((c = getopt(argc, argv, "vo:")) != EOF) {        
        switch (c) {
            case 'v':
                verbose = TRUE;
                break;
            case 'o':
                ofile = optarg;
                break;
            case '?':
                errflg++;
                break;
        }
    }
    
    if (!freopen(argv[optind],"r", stdin)) {
        fprintf(stderr, "%s: could not open [%s] for input\n",argv[0], argv[optind]);
        exit(-1);
    }

    if (ofile == NULL){
        bnkFile       = fopen("tst.bnk", "w");
        tblFile       = fopen("tst.tbl", "w");
    } else {
        bfname = (char *) malloc(strlen(ofile)+5);
        tfname = (char *) malloc(strlen(ofile)+5);
        strcpy(bfname, ofile);
        strcpy(tfname, ofile);
        strcat(bfname, ".bnk");
        strcat(tfname, ".tbl");
        if ((bnkFile = fopen(bfname, "w")) == NULL){
            fprintf(stderr,"%s: could not open [%s] for output\n",argv[0], bfname);
            exit(-1);
        }
        if ((tblFile = fopen(tfname, "w")) == NULL){
            fprintf(stderr,"%s: could not open [%s] for output\n",argv[0], tfname);
            exit(-1);
        }
    }
    
    /*
     * init the world
     */
    initLex();
    initsymtab();
    initreserved();
    inittypes();
    initoutput();

    declInit();

    /*
     * parse the file
     */
    return_val = yyparse();

    /*
     * lay out the file image by taking a pass through the data and
     * marking the offsets
     */
    doLayOut();
    
    if (verbose) {
        doPrint();
    }

    
    doWrite(bnkFile);
    
    return(return_val);
}