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