synchronize to version 2
[unix-history] / usr / src / usr.bin / pascal / px / int.c
CommitLineData
2917d4fc
KM
1/* Copyright (c) 1979 Regents of the University of California */
2
9b844fb6 3static char sccsid[] = "@(#)int.c 2.1 %G%";
2917d4fc
KM
4
5/*
6 * px - interpreter for Berkeley Pascal
7 * Version 3.0 Winter 1979
8 *
9 * Original version for the PDP 11/70 authored by:
10 * Bill Joy, Charles Haley, Ken Thompson
11 *
12 * Rewritten for VAX 11/780 by Kirk McKusick
13 */
14
15#include <signal.h>
9a92014d 16#include "whoami.h"
2917d4fc 17#include "vars.h"
8fe8ed0d 18#include "libpc.h"
2917d4fc
KM
19#include "objfmt.h"
20
b4466be6
ML
21/*
22 * New stuff for pdx
23 */
24
25extern char *end;
26extern loopaddr();
27union progcntr *pcaddrp; /* address of interpreter frame address */
28
2917d4fc
KM
29main(ac,av)
30
9a92014d
KM
31 int ac;
32 char **av;
2917d4fc
KM
33
34{
9a92014d 35 register char *objprog, *file;
e111a523 36 char *name;
842d41c9 37 register long bytesread, bytestoread, block;
9a92014d
KM
38 register FILE *prog;
39 struct pxhdr pxhd;
40# define pipe 3
2917d4fc 41
9a92014d
KM
42 /*
43 * Initialize everything
44 */
45 _argc = ac;
46 _argv = av;
47 _nodump = FALSE;
2917d4fc 48\f
9a92014d
KM
49 /*
50 * Determine how PX was invoked, and how to process the program
51 */
b4466be6
ML
52 file = _argv[1];
53 if (!strcmp(_argv[0], "pdx")) {
54 _mode = PDX;
55 _argv += 2; _argc -= 2;
e111a523 56 name = _argv[0];
b4466be6 57 } else if (!strcmp(_argv[0], "pix")) {
9a92014d 58 _mode = PIX;
b4466be6 59 _argv++; _argc--;
e111a523 60 name = _argv[0];
b4466be6 61 } else if (!strcmp(_argv[0], "pipe")) {
9a92014d 62 _mode = PIPE;
b4466be6
ML
63 file = "PIPE";
64 _argv++; _argc--;
e111a523 65 name = _argv[0];
842d41c9 66 } else {
b4466be6
ML
67 _mode = PX;
68 if (_argc <= 1)
69 file = "obj";
e111a523 70 name = file;
842d41c9 71 }
2917d4fc 72\f
fa68e687
KM
73 /*
74 * kludge to check for old style objs.
75 */
76 if (_mode == PX && !strcmp(file, "-")) {
77 fprintf(stderr, "%s is obsolete and must be recompiled\n",
78 _argv[0]);
79 exit(1);
80 }
9a92014d
KM
81 /*
82 * Process program header information
83 */
842d41c9 84 if (_mode == PIPE) {
9a92014d 85 read(pipe,&pxhd,sizeof(struct pxhdr));
842d41c9 86 } else {
9a92014d 87 prog = fopen(file,"r");
842d41c9 88 if (prog == NULL) {
9a92014d
KM
89 perror(file);
90 exit(1);
842d41c9 91 }
9a92014d 92 fread(&pxhd,sizeof(struct pxhdr),1,prog);
b4466be6
ML
93 if (pxhd.magicnum != MAGICNUM) {
94 fseek(prog,(long)(HEADER_BYTES-sizeof(struct pxhdr)),0);
95 fread(&pxhd,sizeof(struct pxhdr),1,prog);
96 }
842d41c9
KM
97 }
98 if (pxhd.magicnum != MAGICNUM) {
e111a523 99 fprintf(stderr,"%s is not a Pascal interpreter file\n",name);
2917d4fc 100 exit(1);
842d41c9 101 }
b4466be6 102 if (pxhd.maketime < createtime) {
e111a523 103 fprintf(stderr,"%s is obsolete and must be recompiled\n",name);
b4466be6
ML
104 exit(1);
105 }
2917d4fc 106\f
9a92014d
KM
107 /*
108 * Load program into memory
109 */
110 objprog = malloc((int)pxhd.objsize);
842d41c9
KM
111 if (_mode == PIPE) {
112 bytestoread = pxhd.objsize;
9a92014d 113 bytesread = 0;
842d41c9
KM
114 do {
115 block = read(pipe,(int)(objprog+bytesread),bytestoread);
116 if (block > 0) {
117 bytesread += block;
118 bytestoread -= block;
9a92014d 119 }
842d41c9
KM
120 } while (block > 0);
121 } else {
9a92014d
KM
122 bytesread = fread(objprog,1,(int)pxhd.objsize,prog);
123 fclose(prog);
842d41c9
KM
124 }
125 if (bytesread != pxhd.objsize) {
9a92014d
KM
126 fprintf(stderr,"Read error occurred while loading %s\n",file);
127 exit(1);
842d41c9 128 }
2917d4fc 129 if (_mode == PIX)
9a92014d
KM
130 fputs("Execution begins...\n",stderr);
131 /*
132 * set interpreter to catch expected signals and begin interpretation
133 */
134 signal(SIGILL,syserr);
135 signal(SIGBUS,syserr);
136 signal(SIGSYS,syserr);
137 if (signal(SIGINT,SIG_IGN) != SIG_IGN)
138 signal(SIGINT,intr);
139 signal(SIGSEGV,memsize);
8fe8ed0d 140 signal(SIGFPE,EXCEPT);
9a92014d 141 signal(SIGTRAP,liberr);
b4466be6
ML
142
143 /*
144 * See if we're being watched by the debugger, if so set a trap.
145 */
146 if (_mode == PDX || (_mode == PIX && pxhd.symtabsize > 0)) {
147 inittrap(&_display, &_dp, objprog, &pcaddrp, loopaddr);
148 }
149
9a92014d
KM
150 /*
151 * do it
152 */
153 interpreter(objprog);
154 /*
155 * reset signals, deallocate memory, and exit normally
156 */
157 signal(SIGINT,SIG_IGN);
158 signal(SIGSEGV,SIG_DFL);
159 signal(SIGFPE,SIG_DFL);
160 signal(SIGTRAP,SIG_DFL);
161 signal(SIGILL,SIG_DFL);
162 signal(SIGBUS,SIG_DFL);
163 signal(SIGSYS,SIG_DFL);
164 PFLUSH();
9a92014d 165 psexit(0);
2917d4fc 166}
b4466be6
ML
167
168/*
169 * Generate an IOT trap to tell the debugger that the object code
170 * has been read in. Parameters are there for debugger to look at,
171 * not the procedure.
172 */
173
174static inittrap(dispaddr, dpaddr, endaddr, pcaddrp, loopaddrp)
175union disply *dispaddr;
176struct disp *dpaddr;
177char *endaddr;
178union progcntr **pcaddrp;
179char **loopaddrp;
180{
181 kill(getpid(), SIGIOT);
182}