home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / px / int.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  3KB  |  159 lines

  1. /* (c) 1979 Regents of the University of California */
  2. #
  3. /*
  4.  * px - interpreter for Berkeley Pascal
  5.  * Version 1.0 August 1977
  6.  *
  7.  * Bill Joy, Charles Haley, Ken Thompson
  8.  */
  9.  
  10. #include "0x.h"
  11. #include "opcode.h"
  12. #include "E.h"
  13.  
  14. int    display[20]    { display };
  15.  
  16. int    onintr();
  17.  
  18. main(ac, av)
  19.     int ac;
  20.     char *av[];
  21. {
  22.     register char *cp;
  23.     register int bytes, rmdr;
  24.     int size, *bp, i, of;
  25.  
  26.     i = signal(2, 1);
  27.     argc = ac - 1, argv = av + 1;
  28.     randim = 1./randm;
  29.     setmem();
  30.     if (av[0][0] == '-' && av[0][1] == 'o') {
  31.         av[0] =+ 2;
  32.         file = av[0];
  33.         argv--, argc++;
  34.         discard++;
  35.     } else if (argc == 0)
  36.         file = *--argv = "obj", argc++;
  37.     else if (argv[0][0] == '-' && argv[0][1] == 0) {
  38.         argv[0][0] = 0;
  39.         file = 0;
  40.         argv[0] = argv[-1];
  41.     } else
  42.         file = *argv;
  43.     if (file) {
  44.         cp = file;
  45.         of = open(cp, 0);
  46.         if (discard)
  47.             unlink(cp);
  48.     } else
  49.         of = 3;
  50.     if ((i & 01) == 0)
  51.         signal(2, onintr);
  52.     if (of < 0) {
  53. oops:
  54.         perror(cp);
  55.         exit(1);
  56.     }
  57.     if (file) {
  58.         fstat(of, display);
  59.         size = display[5];
  60.     } else
  61.         if (read(of, &size, 2) != 2) {
  62.             ferror("Improper argument");
  63.             exit(1);
  64.         }
  65.     if (size == 0) {
  66.         ferror("File is empty");
  67.         exit(1);
  68.     }
  69.     if (file) {
  70.         read(of, &i, 2);
  71.         if (i == 0407) {
  72.             size =- 1024;
  73.             seek(of, 1024, 0);
  74.         } else
  75.             seek(of, 0, 0);
  76.     }
  77.     bp = cp = alloc(size);
  78.     if (cp == -1) {
  79.         ferror("Too large");
  80.         exit(1);
  81.     }
  82.     rmdr = size;
  83.     while (rmdr != 0) {
  84.         i = (rmdr > 0 && rmdr < 512) ? rmdr : 512;
  85.         bytes = read(of, cp, i);
  86.         if (bytes <= 0) {
  87.             ferror("Unexpected end-of-file");
  88.             exit(1);
  89.         }
  90.         rmdr =- bytes;
  91.         cp =+ bytes;
  92.     }
  93.     if (read(of, cp, 1) == 1) {
  94.         ferror("Expected end-of-file");
  95.         exit(1);
  96.     }
  97.     close(of);
  98.     if (file == 0)
  99.         wait(&i);
  100.     if (*bp++ != 0404) {
  101.         ferror("Not a Pascal object file");
  102.         exit(1);
  103.     }
  104.     if (discard && bp[(bp[0] == O_PXPBUF ? bp[5] + 8 : bp[1]) / 2 + 1] != O_NODUMP)
  105.         write(2, "Execution begins...\n", 20);
  106.     interpret(bp, size);
  107. }
  108.  
  109. Perror(file, mesg)
  110.     char *file, *mesg;
  111. {
  112.     extern int errno;
  113.     extern char *sys_errlist[];
  114.  
  115.     errno = 0;
  116.     sys_errlist[0] = mesg;
  117.     perror(file);
  118. }
  119.  
  120. /*
  121.  * Initialization of random number "constants"
  122.  */
  123. long    seed    7774755.;
  124. double    randa    62605.;
  125. double    randc    113218009.;
  126. double    randm    536870912.;
  127.  
  128. /*
  129.  * Routine to put a string on the current
  130.  * pascal output given a pointer to the string
  131.  */
  132. puts(str)
  133.     char *str;
  134. {
  135.     register char *cp;
  136.  
  137.     cp = str;
  138.     while (*cp)
  139.         pputch(*cp++);
  140. }
  141.  
  142. ferror(cp)
  143.     char *cp;
  144. {
  145.  
  146.     Perror(file, cp);
  147. }
  148.  
  149. onintr()
  150. {
  151.     extern int draino[];
  152.  
  153.     if (dp == 0)
  154.         exit(1);
  155.     draino[0] = 512;
  156.     draino[1] = &draino[2];
  157.     error(EINTR);
  158. }
  159.