/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by Mike Parker.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>

#include <vax/mtpr.h>

char *malloc();
char *realloc();
char *copyofstr();
char *copyofblk();
int free();
#define NEW(type) ((type *) malloc(sizeof(type)))
#define OLD(x) free((char *) x)

#include "../sys/rciext/kaioctl.h"
#include "../sys/rciext/kamsgs.h"

#include "getmapplace.h"

char *sbrk();

char device[] = "/dev/ka1";
char datafn[] = "/u1/mouse/.term/tvi950";
struct stat dfnstb;
char *data;
int cpuno = 1;
int kafd;
struct blkdesc desc;
char bootmsg[1024];
extern char etext;
extern char edata;
extern char end;

int Xint;

int slavemain();

getpfn()
{
 register int p0br; /* r11 */

 p0br = P0BR;
 asm("mfpr r11,r11");
 Xint = ((int *)p0br)[Xint>>9];
}

main()
{
 int fd;
 int dpgs;
 int pgsiz;

 fd = open(datafn,O_RDONLY,0);
 if (fd < 0)
  { perror(datafn);
    exit(1);
  }
 fstat(fd,&dfnstb);
 if ((dfnstb.st_mode&S_IFMT) != S_IFREG)
  { fprintf(stderr,"%s: not a plain file\n",datafn);
    exit(1);
  }
 pgsiz = getpagesize();
 data = sbrk(pgsiz+(int)dfnstb.st_size+pgsiz);
 if (data == (char *)-1)
  { perror("sbrk");
    exit(1);
  }
 data = (char *) (((((int)data)+pgsiz-1)/pgsiz)*pgsiz);
 data += 4;
 dpgs = ((((int)dfnstb.st_size)+4+pgsiz-1)/pgsiz) * pgsiz;
 if (read(fd,data,(int)dfnstb.st_size) != (int)dfnstb.st_size)
  { fprintf(stderr,"%s: can't read it all\n",datafn);
    exit(1);
  }
 close(fd);
 kafd = open(device,O_RDWR,0);
 if (kafd < 0)
  { perror(device);
    exit(1);
  }
 sendclear(kafd);
 sendtext(kafd);
 senddata(kafd);
 doexport(kafd,data-4,dpgs);
 ((int *)data)[-1] = 0;
 sendexec(kafd,slavemain);
 while (((int *)data)[-1] == 0) ;
 exit(0);
}

slavemain()
{
 fwrite(data,1,(int)dfnstb.st_size,stdout);
 fflush(stdout);
 ((int *)data)[-1] = 1;
}
