xref: /original-bsd/usr.bin/pascal/src/yymain.c (revision 04dd0305)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)yymain.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "whoami.h"
13 #include "0.h"
14 #include "tree_ty.h"	/* must be included for yy.h */
15 #include "yy.h"
16 #include <a.out.h>
17 #include "objfmt.h"
18 #include <signal.h>
19 #include "config.h"
20 
21 /*
22  * Yymain initializes each of the utility
23  * clusters and then starts the processing
24  * by calling yyparse.
25  */
26 yymain()
27 {
28 
29 #ifdef OBJ
30 /*
31  * initialize symbol table temp files
32  */
33 	startnlfile();
34 #endif
35 	/*
36 	 * Initialize the scanner
37 	 */
38 #ifdef PXP
39 	if (bracket == 0) {
40 #endif
41 		if (getline() == -1) {
42 			Perror(filename, "No lines in file");
43 			pexit(NOSTART);
44 		}
45 #ifdef PXP
46 	} else
47 		yyline = 0;
48 #endif
49 
50 #ifdef PI
51 #   ifdef OBJ
52 	magic();
53 #   endif OBJ
54 #endif
55 	line = 1;
56 	errpfx = 'E';
57 	/*
58 	 * Initialize the clusters
59 	 *
60 	initstring();
61 	 */
62 	inithash();
63 	inittree();
64 #ifdef PI
65 	initnl();
66 #endif
67 
68 	/*
69 	 * Process the input
70 	 */
71 	yyparse();
72 #ifdef PI
73 #   ifdef OBJ
74 
75 	/*
76 	 * save outermost block of namelist
77 	 */
78 	savenl(NLNIL);
79 
80 	magic2();
81 #   endif OBJ
82 #   ifdef DEBUG
83 	dumpnl(NLNIL);
84 #   endif
85 #endif
86 
87 #ifdef PXP
88 	prttab();
89 	if (onefile) {
90 		extern int outcol;
91 
92 		if (outcol)
93 			pchr('\n');
94 		flush();
95 		if (eflg) {
96 			writef(2, "File not rewritten because of errors\n");
97 			pexit(ERRS);
98 		}
99 		(void) signal(SIGHUP, SIG_IGN);
100 		(void) signal(SIGINT, SIG_IGN);
101 		copyfile();
102 	}
103 #endif
104 	pexit(eflg ? ERRS : AOK);
105 }
106 
107 #ifdef PXP
108 copyfile()
109 {
110 	extern int fout[];
111 	register int c;
112 
113 	(void) close(1);
114 	if (creat(firstname, 0644) != 1) {
115 		perror(firstname);
116 		pexit(ERRS);
117 	}
118 	(void) lseek(fout[0], 0l, 0);
119 	while ((c = read(fout[0], &fout[3], 512)) > 0) {
120 		if (write(1, &fout[3], c) != c) {
121 			perror(firstname);
122 			pexit(ERRS);
123 		}
124 	}
125 }
126 #endif
127 
128 
129 #ifdef PI
130 #ifdef OBJ
131 
132 static
133 struct exec magichdr;
134 
135 magic()
136 {
137 
138 	short		buf[HEADER_BYTES / sizeof ( short )];
139 	unsigned	*ubuf = (unsigned *) buf;
140 	register int	hf, i;
141 
142 	hf = open(px_header,0);
143 	if (hf >= 0 && read(hf, (char *) buf, HEADER_BYTES) > sizeof(struct exec)) {
144 		magichdr.a_magic = ubuf[0];
145 		magichdr.a_text = ubuf[1];
146 		magichdr.a_data = ubuf[2];
147 		magichdr.a_bss = ubuf[3];
148 		magichdr.a_syms = ubuf[4];
149 		magichdr.a_entry = ubuf[5];
150 		magichdr.a_trsize = ubuf[6];
151 		magichdr.a_drsize = ubuf[7];
152 		for (i = 0; i < HEADER_BYTES / sizeof ( short ); i++)
153 			word(buf[i]);
154 	}
155 	(void) close(hf);
156 }
157 #endif OBJ
158 
159 #ifdef OBJ
160 magic2()
161 {
162 	struct pxhdr pxhd;
163 	extern long lseek();
164 
165 	if  (magichdr.a_magic != 0407)
166 		panic ( "magic2" );
167 	pflush();
168 	magichdr.a_data = ( unsigned ) lc - magichdr.a_text;
169 	magichdr.a_data -= sizeof (struct exec);
170 	pxhd.objsize = ( ( unsigned ) lc) - HEADER_BYTES;
171 	pxhd.symtabsize = nlhdrsize();
172 	magichdr.a_data += pxhd.symtabsize;
173 	(void) time((long *) (&pxhd.maketime));
174 	pxhd.magicnum = MAGICNUM;
175 	(void) lseek(ofil, 0l, 0);
176 	write(ofil, (char *) (&magichdr), sizeof(struct exec));
177 	(void) lseek(ofil, ( long ) ( HEADER_BYTES - sizeof ( pxhd ) ) , 0);
178 	write(ofil, (char *) (&pxhd), sizeof (pxhd));
179 }
180 #endif OBJ
181 #endif
182 
183 #ifdef PXP
184 writef(i, cp)
185 {
186 
187 	write(i, cp, strlen(cp));
188 }
189 #endif
190