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