xref: /original-bsd/usr.bin/pascal/px/int.c (revision 1f3a482a)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)int.c 1.4 06/21/81";
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>
16 #include	"whoami.h"
17 #include	"vars.h"
18 #include	"objfmt.h"
19 
20 main(ac,av)
21 
22 	int	ac;
23 	char	**av;
24 
25 {
26 	register char *objprog, *file;
27 	register long bytesread, bytestoread, block;
28 	register FILE *prog;
29 	struct	 pxhdr pxhd;
30 #	define	 pipe 3
31 
32 	/*
33 	 * Initialize everything
34 	 */
35 	_argc = ac;
36 	_argv = av;
37 	_nodump = FALSE;
38 
39 	/*
40 	 * Determine how PX was invoked, and how to process the program
41 	 */
42 	if (_argv[0][0] == '-' && _argv[0][1] == 'o') {
43 		file = &_argv[0][2];
44 		_mode = PIX;
45 	} else if (_argc <= 1) {
46 		file = "obj";
47 		_mode = PX;
48 	} else if (_argv[1][0] != '-') {
49 		file = _argv[1];
50 		_mode = PX;
51 	} else if (_argv[1][1] == 0) {
52 		file = _argv[0];
53 		_mode = PIPE;
54 		_argc -= 1;
55 		_argv[1] = _argv[0];
56 		_argv = &_argv[1];
57 	} else {
58 		fputs("Improper specification of object file to PX\n",stderr);
59 		exit(1);
60 	}
61 
62 	/*
63 	 * Process program header information
64 	 */
65 	if (_mode == PIPE) {
66 		read(pipe,&pxhd,sizeof(struct pxhdr));
67 	} else {
68 		prog = fopen(file,"r");
69 		if (prog == NULL) {
70 			perror(file);
71 			exit(1);
72 		}
73 		fseek(prog,(long)(HEADER_BYTES-sizeof(struct pxhdr)),0);
74 		fread(&pxhd,sizeof(struct pxhdr),1,prog);
75 	}
76 	if (pxhd.maketime < createtime) {
77 		fprintf(stderr,"%s is obsolete and must be recompiled\n",file);
78 		exit(1);
79 	}
80 	if (pxhd.magicnum != MAGICNUM) {
81 		fprintf(stderr,"%s is not a Pascal interpreter file\n",file);
82 		exit(1);
83 	}
84 
85 	/*
86 	 * Load program into memory
87 	 */
88 	objprog = malloc((int)pxhd.objsize);
89 	if (_mode == PIPE) {
90 		bytestoread = pxhd.objsize;
91 		bytesread = 0;
92 		do	{
93 			block = read(pipe,(int)(objprog+bytesread),bytestoread);
94 			if (block > 0) {
95 				bytesread += block;
96 				bytestoread -= block;
97 			}
98 		} while (block > 0);
99 	} else {
100 		bytesread = fread(objprog,1,(int)pxhd.objsize,prog);
101 		fclose(prog);
102 		if (_mode == PIX)
103 			unlink(file);
104 	}
105 	if (bytesread != pxhd.objsize) {
106 		fprintf(stderr,"Read error occurred while loading %s\n",file);
107 		exit(1);
108 	}
109 	if (_mode == PIX)
110 		fputs("Execution begins...\n",stderr);
111 	/*
112 	 * set interpreter to catch expected signals and begin interpretation
113 	 */
114 	signal(SIGILL,syserr);
115 	signal(SIGBUS,syserr);
116 	signal(SIGSYS,syserr);
117 	if (signal(SIGINT,SIG_IGN) != SIG_IGN)
118 		signal(SIGINT,intr);
119 	signal(SIGSEGV,memsize);
120 	signal(SIGFPE,except);
121 	signal(SIGTRAP,liberr);
122 	/*
123 	 * do it
124 	 */
125 	interpreter(objprog);
126 	/*
127 	 * reset signals, deallocate memory, and exit normally
128 	 */
129 	signal(SIGINT,SIG_IGN);
130 	signal(SIGSEGV,SIG_DFL);
131 	signal(SIGFPE,SIG_DFL);
132 	signal(SIGTRAP,SIG_DFL);
133 	signal(SIGILL,SIG_DFL);
134 	signal(SIGBUS,SIG_DFL);
135 	signal(SIGSYS,SIG_DFL);
136 	PFLUSH();
137 	/* pfree(objprog); */
138 	psexit(0);
139 }
140