xref: /csrg-svn/usr.bin/pascal/pxp/yymain.c (revision 22245)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)yymain.c	5.1 (Berkeley) 06/05/85";
15 #endif not lint
16 
17 /*
18  * pi - Pascal interpreter code translator
19  *
20  * Charles Haley, Bill Joy UCB
21  * Version 1.2 November 1978
22  *
23  *
24  * pxp - Pascal execution profiler
25  *
26  * Bill Joy UCB
27  * Version 1.2 November 1978
28  */
29 
30 #include "whoami.h"
31 #include "0.h"
32 #include "yy.h"
33 
34 int	line = 1;
35 
36 /*
37  * Yymain initializes each of the utility
38  * clusters and then starts the processing
39  * by calling yyparse.
40  */
41 yymain()
42 {
43 
44 	/*
45 	 * Initialize the scanner
46 	 */
47 #ifdef PXP
48 	if (bracket == 0) {
49 #endif
50 		if (getline() == -1) {
51 			Perror(filename, "No lines in file");
52 			pexit(NOSTART);
53 		}
54 #ifdef PXP
55 	} else
56 		yyline = 0;
57 #endif
58 
59 #ifdef PI
60 	magic();
61 
62 #endif
63 	/*
64 	 * Initialize the clusters
65 	 *
66 	initstring();
67 	 */
68 	inithash();
69 	inittree();
70 #ifdef PI
71 	initnl();
72 #endif
73 
74 	/*
75 	 * Process the input
76 	 */
77 	yyparse();
78 #ifdef PI
79 	magic2();
80 #ifdef DEBUG
81 	dumpnl(0);
82 #endif
83 #endif
84 #ifdef PXP
85 	prttab();
86 	if (onefile) {
87 		extern int outcol;
88 
89 		if (outcol)
90 			putchar('\n');
91 		flush();
92 		if (eflg) {
93 			writef(2, "File not rewritten because of errors\n");
94 			pexit(ERRS);
95 		}
96 		signal(1, 1);
97 		signal(2, 1);
98 		copyfile();
99 	}
100 #endif
101 	pexit(eflg ? ERRS : AOK);
102 }
103 
104 #ifdef PXP
105 copyfile()
106 {
107 	register int c;
108 	char buf[BUFSIZ];
109 
110 	if (freopen(stdoutn, "r", stdin) == NULL) {
111 		perror(stdoutn);
112 		pexit(ERRS);
113 	}
114 	if (freopen(firstname, "w", stdout) == NULL) {
115 		perror(firstname);
116 		pexit(ERRS);
117 	}
118 	while ((c = getchar()) > 0)
119 		putchar(c);
120 	if (ferror(stdout))
121 		perror(stdout);
122 }
123 #endif
124 
125 static
126 struct {
127 	int		magic;
128 	unsigned	txt_size;
129 	unsigned	data_size;
130 	unsigned	bss_size;
131 	unsigned	syms_size;
132 	unsigned	entry_point;
133 	unsigned	tr_size;
134 	unsigned	dr_size;
135 } header;
136 
137 #ifdef PI
138 magic()
139 {
140 
141     /*
142      *	this is the size of /usr/lib/npxheader
143      */
144 #define	HEAD_BYTES	1024
145 	short		buf[HEAD_BYTES / sizeof ( short )];
146 	unsigned	*ubuf = buf;
147 	register int	hf, i;
148 
149 	hf = open("/usr/lib/npx_header", 0);
150 	if (hf >= 0 && read(hf, buf, HEAD_BYTES) > sizeof header) {
151 		header.magic = ubuf[0];
152 		header.txt_size = ubuf[1];
153 		header.data_size = ubuf[2];
154 		header.bss_size = ubuf[3];
155 		header.syms_size = ubuf[4];
156 		header.entry_point = ubuf[5];
157 		header.tr_size = ubuf[6];
158 		header.dr_size = ubuf[7];
159 		for (i = 0; i < HEAD_BYTES / sizeof ( short ); i++)
160 			word(buf[i]);
161 	}
162 	close(hf);
163 	word(0404);
164 }
165 
166 magic2()
167 {
168 	short i;
169 
170 	if  (header.magic != 0407)
171 		panic ( "magic2" );
172 	pflush();
173 	lseek(ofil, 0l, 0);
174 	header.data_size = ( unsigned ) lc - header.txt_size;
175 	header.data_size =- sizeof header;
176 	write(ofil, &header, sizeof header);
177 	lseek(ofil, ( long ) ( HEAD_BYTES - sizeof ( short ) ) , 0);
178 	i = ( ( unsigned ) lc) - HEAD_BYTES;
179 	write(ofil, &i, 2);
180 }
181 #endif
182 
183 #ifdef PXP
184 writef(i, cp)
185 {
186 
187 	write(i, cp, strlen(cp));
188 }
189 #endif
190