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