/* VIEWER  03/19/92             */
/* Programmed by Terry Newton   */
/* This program runs Binary and */
/* Basic programs, read text or */
/* display bitmap graphic files */
/* (normal and compressed)      */
/* Requires...                  */
/*   ACECIO.CCC    Functions    */
/*   CMP6D40.OBJ   Compress ML  */
/*   GR7P6F00.OBJ  GR15 ML      */
/*   BRUN0590.OBJ  Bin Load ML  */
/*   ENGINE.OBJ    Ace Runtime  */

int  check1,reentry,basic;
int  cr0,cr1,cr2,cr3,cr4,grmode;
int  key,err,esc,i,n,dircnt,func;
int  halfpnt,reread,ftype,a,b,c,d,e;
int  menucnt,pagenum,gr7,picmode;
int  drvnum,esc3,blksfree,exitflg;
int  lpos,llen,wpos;
int  check2,flen,scrval;
int  eof,size,bufptr,len;
char word[80],darr[129*12],fext[4];
char path[80],fspec[80],inpstr[80];
char *tmptr,*start,*temp,*ptr;
char *scrbuf,*filebuf;

main() $(
 if(peek(0x6F00)>0)check1=0;
 if(check1!=12345||check2!=12345) $(
  drvnum=peek(0x0021);
  strcpy(path,"D1:");
  poke(path+1,drvnum+48);
  reentry=0;
 $) else reentry=1;
/* set Sparta RUNLOC */
 if(peek(dpeek(10)+28)==128||
    peek(0x0700)==0x53)
  dpoke(dpeek(10)+61,0x3635);
 if(dpeek(0xBFFA)==0xA000)basic=1;
 else basic=0;
 poke(0x6F00,0);scrval=0;
 scrbuf=0x7000;filebuf=0x7400;
 check1=0;check2=0;exitflg=0;
 if(reentry==0) $(
  readdsk();pagenum=0;
 $) else reentry=0;
 while(exitflg==0) $(
  pickfile();
  if(func==1)readdsk();
  if(func==2)loadfile();
  if(func==3)exitflg=1;
 $)
 check1=12345;check2=12345;
 printf("↰");poke(752,0);exit();
$)
/* set colors */
setcols() $(
 poke(708,cr0);poke(709,cr1);
 poke(710,cr2);poke(711,cr3);
 poke(712,cr4);
$)
/* text mode */
textmode() $(
 graphics(0);poke(82,2);
 poke(752,1);printf("↰");
 cr0=0;cr1=12;cr2=0;
 cr3=0;cr4=0;setcols();
$)
/* error!!! */
errtrap() $(
 err=abs(err);closeall();
 textmode();position(2,5);
 printf("Error #%d\n",err);
 func=999;getkb();
$)
/* no attract getkey */
getkb() $(
 while(peek(764)==255)poke(77,0);
 key=getkey();
$)
/* read directory into array */
readdsk() $(
 blksfree=0;dircnt=0;
 scrval=0;pagenum=0;
 textmode();position(2,10);
 strcpy(fspec,path);
 strcat(fspec,"*.*");
 printf("Reading %s\n",fspec);
 err=open(1,6,0,fspec);
 if(err<0) errtrap();
 else $(
  dircnt=0;esc=0;
  while(esc==0) $(
   err=cgets(inpstr,1);
   if(err>0) $(
    temp=strlen(inpstr);
    if(temp>=17) $(
     move(inpstr+1,fspec,12);
     poke(fspec+12,0);
     if(strcmp(fspec+9,"DIR")==0) $(
      poke(fspec,'.');
      strcpy(fspec+9,"   ");
     $)
     temp=dircnt*12;temp+=darr;
     move(fspec,temp,12);dircnt++;
    $) else $(
     move(inpstr,fspec,4);
     blksfree=val(fspec);
    $)
   $)
   else esc=1;
   err=0;
  $)
 $)
 close(1);
$)
/* File Selection Menu */
pickfile() $(
 textmode();
 if(scrval)
  move(scrbuf,dpeek(88),960);
 else $(
  temp=pagenum*36;
  if(dircnt<temp+37)
   menucnt=dircnt-temp;
  else menucnt=36;
  printf("%s %d FILES %d FREE",path,dircnt,blksfree);
  halfpnt=(menucnt+1)/2;position(2,2);
  if(menucnt>0) $(
   for(i=0;i<halfpnt&&i<menucnt;i++) $(
    temp=i+65;prtname();
   $)
   position(2,2);
   for(i=halfpnt;i<menucnt;i++) $(
    temp=i+65;if(temp>90)temp=i+22;
    printf("→→→→→→→→→→→→→→→→→→→");
    prtname();
   $)
  $)
  position(2,22);
  printf(" ESC  ");
  if(strlen(path)==3) printf("Disk");
  else printf("Main");
  printf("   RET  DOS  ");
  if(dircnt>36) printf(" SPC  Page");
  if(basic)printf("\n(BASIC ON)↑");
  printf("\n");poke(764,255);
  move(dpeek(88),scrbuf,960);scrval=1;
 $)
 func=0;while(func==0) $(
  getkb();key=toupper(key);
  if(key=='␛') $(
   func=999;err=128;
   while(err>2) $(
    if(strlen(path)==3) $(
     printf("↰↓↓Select Drive (1-8)");
     drvnum=0;
     while(drvnum<1||drvnum>8) $(
      drvnum=getkey()-48;
     $)
    $)
    strcpy(path,"D1:");
    poke(path+1,drvnum+48);
    readdsk();pagenum=0;key=0;
   $)
  $)
  if(key==32&&dircnt>36) $(
   func=999;pagenum++;scrval=0;
   if(pagenum*36>dircnt)pagenum=0;
  $)
  if(key==155)func=3;
/* Determine File to Load */
  temp=999;
  if(key>='A'&&key<='Z')temp=key-65;
  if(key>='0'&&key<='9')temp=key-22;
  if(temp<menucnt&&temp>=0) $(
   temp+=36*pagenum;
   temp*=12;tmptr=temp+darr+1;
/* build fspec */
   move(tmptr+8,fext,3);
   poke(fext+3,0);
   move(tmptr,fspec,8);
   poke(fspec+8,0);
   for(i=0;i<8;i++) $(
    if(peek(fspec+i)==32) poke(fspec+i,0);
   $)
   if(peek(fext)!=32) $(
    for(i=0;i<3;i++) $(
     if(peek(fext+i)==32) poke(fext+i,0);
    $)
    strcat(fspec,".");
    strcat(fspec,fext);
   $)
   strcpy(inpstr,fspec);
   strcpy(fspec,path);
   strcat(fspec,inpstr);
   i=strlen(fspec);
   for(;i<80;i++)poke(fspec+i,0);
/* subdirectory? */
   if(peek(tmptr-1)!='.')func=2;
   else $(
    strcat(fspec,">");
    strcpy(path,fspec);func=1;
   $)
  $)
 $)
$)
/* print filename */
prtname() $(
 printf(" %c ",temp+128);
 tmptr=i;tmptr+=pagenum*36;
 tmptr*=12;tmptr+=darr;
 move(tmptr,fspec,12);
 poke(fspec+12,0);
 printf("%s\n",fspec);
$)
/* Determine file type and load */
loadfile() $(
 err=open(1,4,0,fspec);
 if(err<0)errtrap();
 else $(
  a=cgetc(1);b=cgetc(1);close(1);
  picmode=0;if(strcmp(fext,"MIC")==0)
   $( picmode=1;loadima(); $)
  else if(a==255&&b==255)binload();
  else if(a==0&&b==0)basload();
  else if(a>0&&a<32)loadima();
  else disptext();
 $)
$)
/* binary load */
binload() $(
 if(strcmp(fext,"COM")==0)
  poke(0x0589,1);
 else poke(0x0589,0);
 textmode();position(6,4);
 printf("Loading %s\n\n",fspec);
 poke(752,0);brun(fspec);scrval=0;
$)
/* basic load */
basload() $(
 if(basic) $(
  poke(752,0);
  printf("↰↓↓↓RUN\34%s\34\n",fspec);
  printf("↑↑↑");poke(764,12);basrun();
 $) else disptext();
$)
/* load image file */
loadima() $(
 len=0;open(1,4,0,fspec);
 gr7=0;grmode=8;cr0=0;cr1=12;
 cr2=0;cr3=0;cr4=0;
 if(picmode==0) $(
  grmode=cgetc(1)&15;
  if(grmode==15) $(grmode=8;gr7=1;$)
  cr0=cgetc(1);cr1=cgetc(1);
  cr2=cgetc(1);cr3=cgetc(1);
  cr4=cgetc(1);
 $)
 if(grmode>6) $(
  graphics(grmode+16);setcols();
  start=dpeek(0x0058);
  flen=7680;if(grmode==7)flen=3840;
  if(cr3!=254)bgets(start,flen,1);
  else if(basic==0)
   decode(start,flen,1);
  if(gr7) $(
   graphics(8+48);gr7plus();setcols();
  $)
  clear(filebuf,500);
  len=bgets(filebuf,500,1);
  close(1);getkb();
  if(key=='┼') $(
   graphics(grmode+32);
   if(gr7)gr7plus();setcols();
   printf("↰*** SAVE IMAGE ***\n");
   printf("1) Normal  2) Bitmap  ");
   key=getkey();
   if(key=='1'&&basic)key=0;
   if(key=='1'||key=='2') $(
    printf("%c\n",key);picmode=key-49;
    printf("Filename >");gets(fspec);
    if(picmode)normalize(fspec,"MIC");
    else normalize(fspec,"PIC");
    err=open(1,8,0,fspec);
    if(err<0)errtrap; else $(
     graphics(grmode+48);setcols();
     if(gr7)gr7plus();
     if(picmode)bputs(start,flen,1);
     else $(
      if(gr7)cputc(15,1);
      else cputc(grmode,1);
      cputc(cr0,1);cputc(cr1,1);
      cputc(cr2,1);cputc(254,1);
      cputc(cr4,1);
      encode(start,flen,1);
      if(len>1)bputs(filebuf,len,1);
     $)
    $)
   $)
  $)
 $)
 close(1);
$)
/* ML Links */
encode()  asm 0x6D40;
decode()  asm 0x6D43;
brun()    asm 0x0590;
basrun()  asm 0xA000;
gr7plus() asm 0x6F01;
/* display text file */
disptext() $(
 poke(82,0);printf("↰");
 llen=40;esc=0;lpos=0;wpos=0;
 open(1,4,0,fspec);
 size=bgets(filebuf,2048,1);
 if(size<2048)eof=1;else eof=0;
 bufptr=0;err=0;
 while(esc==0) $(
  temp=*(filebuf+bufptr);bufptr++;
  if(bufptr>=size) $(
   if(eof==0) $(
    bufptr=0;
    size=bgets(filebuf,2048,1);
    if(size<2048)eof=1;else eof=0;
   $) else err=128;
  $)
  if(err>1)esc=1;else $(
   poke(word+wpos,temp);wpos++;
   if(temp==32||wpos>78||temp==155) $(
    if(lpos+wpos>llen) $(
     if(lpos<llen)printf("\n");
     lpos=0;
    $)
    d=0;while(d<wpos) $(
     c=peek(word+d);
     printf("␛%c",c);d++;
    $)
    lpos=lpos+wpos;wpos=0;
    if(temp==155)lpos=0;
    key=peek(764);if(key<255) $(
     poke(764,255);
     if(key==28)esc=2;  /* ESC */
     if(key==33) $(     /* SPC */
      getkb();if(key=='␛')esc=2;
     $)
    $)
   $)
  $)
 $)
 close(1);if(esc==1) $(
  d=0;while(d<wpos) $(
   c=peek(word+d);
   printf("␛%c",c);d++;
  $)
  getkb();
 $)
$)