
#include "ass.h"

int line_no;
int indx;
int lc;
int list;
int symbtab_len=0;
int prspc;
char line[MAXLEN],oldline[MAXLEN];
FILE *outfile,*listfile;
SYMBTYPE symbtab[MAXSYMB];

main(argc,argv)
int argc;
char *argv[];
{
    FILE *mustopen();
    register FILE *infile;
    register i;
    int start,stop;
    char *s;

    if (argc<2 || argc>3)
    {
        fputs("usage: ass filename [filename]\n",stderr);
        exit(1);
    }
    if (argc == 3)
    {
        s=argv[2];
        while (*s)
            *(s++)=toupper(*s);
        listfile=fopen(argv[2],"w");
        if (listfile == NULL)
        {
            fprintf(stderr,"ass: can't open %s\n",argv[2]);
            exit(1);
        }
        list=TRUE;
    }
    else
        list=FALSE;
    infile=mustopen(argv[1],TRUE);
    outfile=mustopen(argv[1],FALSE);
    line_no=1;
    lc=0;
    while (1)
    {
        if (!fgets(oldline,MAXLEN,infile))
        {
            fputs("missing '.end.'-statement",stderr);
            exit(1);
        }
        convline(oldline);
        if ((start=pass1()) != -1)
            break;
        line_no++;
    }
    sortsymb();
    if (lc&1)
        lc++;
    putoct(0,lc);
    putoct(0,start);
    for (i=0; i<symbtab_len; i++)
        if (symbtab[i].globl && symbtab[i].def)
        {
            putstr((symbtab[i].rel ? '@' : 0),symbtab[i].name);
            putoct(0,symbtab[i].val);
        }
    putstr('~',"");
    rewind(infile);
    line_no=1;
    lc=0;
    if (list)
        fprintf(listfile,"\017\n");
    while (1)
    {
        if (!fgets(oldline,MAXLEN,infile))
        {
            fputs("missing '.end'-statement",stderr);
            exit(1);
        }    
        if (list)
        {
            fprintf(listfile,"%4d: %06o- ",line_no,lc);
            prspc=27;
        }
        convline(oldline);
        stop=pass2();
        if (list)
        {
            while (prspc-- > 0)
                putc(' ',listfile);
            fputs(oldline,listfile);
            putc('\n',listfile);
        }
        if (stop)
            break;
        line_no++;
    }
    if (lc&1)
        putbyte(0);
    if (list)
    {
        fprintf(listfile,"\n\nSymbol Table:\n\n");
        for (i=0; i<symbtab_len; i++)
        {
            fprintf(listfile,"%-8.8s %06o",symbtab[i].name,
                   (symbtab[i].def ? symbtab[i].val : 0));
            if (symbtab[i].rel)
                putc('R',listfile);
            else
                putc(' ',listfile);
            if (symbtab[i].globl)
                putc('G',listfile);
            else
                putc(' ',listfile);
            if (i%4 == 3)
                putc('\n',listfile);
            else
                fprintf(listfile,"        ");
        }
        putc('\n',listfile);
    }
}

convline(s)
register char *s;
{
    register i;
    
    i=0;
    while (*s && *s!='\n')
        line[i++]=tolower(*(s++));
    *s='\0';
    line[i]='\0';
}

FILE *mustopen(s,in)
register char *s;
register in;
{
    char name[80];
    register char *t;
    FILE *fil;
    
    if ((t=rindex(s,'.')) != NULL)
    {
        while (*t && *t!='\\')
            t++;
        if (*t!='\\')
        {
            fputs("ass: suffix is added by program\n",stderr);
            exit(1);
        }
    }
    strcpy(name,s);
    strcat(name,(in ? ".s" : ".o"));
    fil=fopen(name,(in ? "r" : "w"));
    if (fil == NULL)
    {
        fprintf(stderr,"ass: can't open %s\n",name);
        exit(1);
    }
    return fil;
}

sortsymb()
{
    extern symbtab_len;
    extern SYMBTYPE symbtab[];
    
    register i,j;
    SYMBTYPE temp;
    
    for (i=0; i<symbtab_len-1; i++)
        for (j=i; j<symbtab_len; j++)
            if (strcmp(symbtab[j].name,symbtab[i].name) < 0)
            {
                temp=symbtab[j];
                symbtab[j]=symbtab[i];
                symbtab[i]=temp;
            }
}
