| 1 | # |
| 2 | /* |
| 3 | * px - interpreter for Berkeley Pascal |
| 4 | * Version 2.0 Winter 1979 |
| 5 | * |
| 6 | * Original version for the PDP 11/70 authored by: |
| 7 | * Bill Joy, Charles Haley, Ken Thompson |
| 8 | * |
| 9 | * Rewritten for VAX 11/780 by Kirk McKusick |
| 10 | */ |
| 11 | |
| 12 | #include "stdio.h" |
| 13 | #include "signal.h" |
| 14 | #include "h00vars.h" |
| 15 | |
| 16 | main(ac,av) |
| 17 | |
| 18 | long ac; |
| 19 | char **av; |
| 20 | |
| 21 | { |
| 22 | extern intr(); |
| 23 | extern memsize(); |
| 24 | extern except(); |
| 25 | extern char *malloc(); |
| 26 | extern char _sibuf[], _sobuf[]; |
| 27 | long stats[8]; |
| 28 | short unsigned magicnum; |
| 29 | long size; |
| 30 | register long bytesread, block; |
| 31 | register char *objprog; |
| 32 | register FILE *prog; |
| 33 | #define pipe 3 |
| 34 | #define pipesize 4096 |
| 35 | |
| 36 | /* |
| 37 | * Initialize everything |
| 38 | */ |
| 39 | argc = ac; |
| 40 | argv = av; |
| 41 | stcnt = 0; |
| 42 | stlim = 500000; |
| 43 | llimit = 0x7fffffff; /* set to unlimited */ |
| 44 | nodump = 0; |
| 45 | bufopt = 1; /* default to line buffering */ |
| 46 | \f |
| 47 | /* |
| 48 | * Determine how PX was invoked, and how to process the program |
| 49 | */ |
| 50 | if (argv[0][0] == '-' && argv[0][1] == 'o') |
| 51 | { |
| 52 | file = &argv[0][2]; |
| 53 | mode = PIX; |
| 54 | } |
| 55 | else if (argc <= 1) |
| 56 | { |
| 57 | file = "obj"; |
| 58 | mode = PX; |
| 59 | } |
| 60 | else if (argv[1][0] != '-') |
| 61 | { |
| 62 | file = argv[1]; |
| 63 | mode = PX; |
| 64 | } |
| 65 | else if (argv[1][1] == 0) |
| 66 | { |
| 67 | file = argv[0]; |
| 68 | mode = PIPE; |
| 69 | argc -= 1; |
| 70 | argv[1] = argv[0]; |
| 71 | argv = &argv[1]; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | fputs("Improper specification of object file to PX\n",stderr); |
| 76 | exit(1); |
| 77 | } |
| 78 | \f |
| 79 | /* |
| 80 | * Process program header information |
| 81 | */ |
| 82 | if (mode == PIPE) |
| 83 | { |
| 84 | read(pipe,(char *)(&size),4); |
| 85 | read(pipe,(char *)(&magicnum),2); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | prog = fopen(file,"r"); |
| 90 | if (prog == NULL) |
| 91 | { |
| 92 | perror(file); |
| 93 | exit(1); |
| 94 | } |
| 95 | fstat(fileno(prog),&stats[0]); |
| 96 | size = stats[4]; |
| 97 | fread((char *)(&magicnum),2,1,prog); |
| 98 | if (magicnum == 0407) |
| 99 | { |
| 100 | fseek(prog,1024,0); |
| 101 | fread((char *)(&magicnum),2,1,prog); |
| 102 | size -= 1024; |
| 103 | } |
| 104 | } |
| 105 | size -=2; |
| 106 | if (magicnum == 0404) |
| 107 | /* maintain compatability with 11/70 */ |
| 108 | addrsze = 8; |
| 109 | else if (magicnum == 0403) |
| 110 | /* normal case */ |
| 111 | addrsze = 4; |
| 112 | else |
| 113 | { |
| 114 | fprintf(stderr,"%s is not a Pascal program\n",file); |
| 115 | exit(1); |
| 116 | } |
| 117 | \f |
| 118 | /* |
| 119 | * Load program into memory |
| 120 | */ |
| 121 | objprog = malloc(size); |
| 122 | if (mode == PIPE) |
| 123 | { |
| 124 | bytesread = 0; |
| 125 | do |
| 126 | { |
| 127 | block = read(pipe,objprog+bytesread,pipesize); |
| 128 | bytesread += block; |
| 129 | } |
| 130 | while (block); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | bytesread = fread(objprog,1,size,prog); |
| 135 | fclose(prog); |
| 136 | if (mode == PIX) |
| 137 | unlink(file); |
| 138 | } |
| 139 | if (bytesread != size) |
| 140 | { |
| 141 | fprintf(stderr,"Read error occurred while loading %s\n",file); |
| 142 | exit(1); |
| 143 | } |
| 144 | setbuf(stdin,&_sibuf[0]); |
| 145 | setbuf(stdout,&_sobuf[0]); |
| 146 | if (mode == PIX) |
| 147 | fputs("Execution begins...\n",stderr); |
| 148 | /* |
| 149 | * set interpreter to catch expected signals and begin interpretation |
| 150 | */ |
| 151 | signal(SIGINT,intr); |
| 152 | signal(SIGSEGV,memsize); |
| 153 | signal(SIGFPE,except); |
| 154 | #ifdef profile |
| 155 | interpret(objprog,1); |
| 156 | #else |
| 157 | interpret(objprog,0); |
| 158 | #endif |
| 159 | /* |
| 160 | * reset signals, deallocate memory, and exit normally |
| 161 | */ |
| 162 | signal(SIGINT,SIG_IGN); |
| 163 | signal(SIGSEGV,SIG_DFL); |
| 164 | signal(SIGFPE,SIG_DFL); |
| 165 | pflush(); |
| 166 | /* |
| 167 | pfree(objprog); |
| 168 | */ |
| 169 | psexit(0); |
| 170 | } |