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