fix code to read from pipes to request only what it needs
[unix-history] / usr / src / usr.bin / pascal / px / int.c
CommitLineData
2917d4fc
KM
1/* Copyright (c) 1979 Regents of the University of California */
2
9d3b2962 3static char sccsid[] = "@(#)int.c 1.4 %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
KM
17#include "vars.h"
18#include "objfmt.h"
19
20main(ac,av)
21
9a92014d
KM
22 int ac;
23 char **av;
2917d4fc
KM
24
25{
9a92014d 26 register char *objprog, *file;
842d41c9 27 register long bytesread, bytestoread, block;
9a92014d
KM
28 register FILE *prog;
29 struct pxhdr pxhd;
30# define pipe 3
2917d4fc 31
9a92014d
KM
32 /*
33 * Initialize everything
34 */
35 _argc = ac;
36 _argv = av;
37 _nodump = FALSE;
2917d4fc 38\f
9a92014d
KM
39 /*
40 * Determine how PX was invoked, and how to process the program
41 */
842d41c9 42 if (_argv[0][0] == '-' && _argv[0][1] == 'o') {
9a92014d
KM
43 file = &_argv[0][2];
44 _mode = PIX;
842d41c9 45 } else if (_argc <= 1) {
9a92014d
KM
46 file = "obj";
47 _mode = PX;
842d41c9 48 } else if (_argv[1][0] != '-') {
9a92014d
KM
49 file = _argv[1];
50 _mode = PX;
842d41c9 51 } else if (_argv[1][1] == 0) {
9a92014d
KM
52 file = _argv[0];
53 _mode = PIPE;
54 _argc -= 1;
55 _argv[1] = _argv[0];
56 _argv = &_argv[1];
842d41c9 57 } else {
9a92014d
KM
58 fputs("Improper specification of object file to PX\n",stderr);
59 exit(1);
842d41c9 60 }
2917d4fc 61\f
9a92014d
KM
62 /*
63 * Process program header information
64 */
842d41c9 65 if (_mode == PIPE) {
9a92014d 66 read(pipe,&pxhd,sizeof(struct pxhdr));
842d41c9 67 } else {
9a92014d 68 prog = fopen(file,"r");
842d41c9 69 if (prog == NULL) {
9a92014d
KM
70 perror(file);
71 exit(1);
842d41c9 72 }
9a92014d
KM
73 fseek(prog,(long)(HEADER_BYTES-sizeof(struct pxhdr)),0);
74 fread(&pxhd,sizeof(struct pxhdr),1,prog);
842d41c9
KM
75 }
76 if (pxhd.maketime < createtime) {
9a92014d
KM
77 fprintf(stderr,"%s is obsolete and must be recompiled\n",file);
78 exit(1);
842d41c9
KM
79 }
80 if (pxhd.magicnum != MAGICNUM) {
bc0cb1f9 81 fprintf(stderr,"%s is not a Pascal interpreter file\n",file);
2917d4fc 82 exit(1);
842d41c9 83 }
2917d4fc 84\f
9a92014d
KM
85 /*
86 * Load program into memory
87 */
88 objprog = malloc((int)pxhd.objsize);
842d41c9
KM
89 if (_mode == PIPE) {
90 bytestoread = pxhd.objsize;
9a92014d 91 bytesread = 0;
842d41c9
KM
92 do {
93 block = read(pipe,(int)(objprog+bytesread),bytestoread);
94 if (block > 0) {
95 bytesread += block;
96 bytestoread -= block;
9a92014d 97 }
842d41c9
KM
98 } while (block > 0);
99 } else {
9a92014d
KM
100 bytesread = fread(objprog,1,(int)pxhd.objsize,prog);
101 fclose(prog);
102 if (_mode == PIX)
103 unlink(file);
842d41c9
KM
104 }
105 if (bytesread != pxhd.objsize) {
9a92014d
KM
106 fprintf(stderr,"Read error occurred while loading %s\n",file);
107 exit(1);
842d41c9 108 }
2917d4fc 109 if (_mode == PIX)
9a92014d
KM
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);
2917d4fc 139}