xref: /original-bsd/usr.bin/pascal/src/yymain.c (revision deff14a8)
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.2 (Berkeley) 05/24/94";
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 	if (lseek(fout[0], (off_t)0, 0) == -1)
119 		perror("copyfile: lseek"), panic("copyfile");
120 	while ((c = read(fout[0], &fout[3], 512)) > 0) {
121 		if (write(1, &fout[3], c) != c) {
122 			perror(firstname);
123 			pexit(ERRS);
124 		}
125 	}
126 }
127 #endif
128 
129 
130 #ifdef PI
131 #ifdef OBJ
132 
133 static
134 struct exec magichdr;
135 
136 magic()
137 {
138 
139 	short		buf[HEADER_BYTES / sizeof ( short )];
140 	unsigned	*ubuf = (unsigned *) buf;
141 	register int	hf, i;
142 
143 	hf = open(px_header,0);
144 	if (hf >= 0 && read(hf, (char *) buf, HEADER_BYTES) > sizeof(struct exec)) {
145 		magichdr.a_magic = ubuf[0];
146 		magichdr.a_text = ubuf[1];
147 		magichdr.a_data = ubuf[2];
148 		magichdr.a_bss = ubuf[3];
149 		magichdr.a_syms = ubuf[4];
150 		magichdr.a_entry = ubuf[5];
151 		magichdr.a_trsize = ubuf[6];
152 		magichdr.a_drsize = ubuf[7];
153 		for (i = 0; i < HEADER_BYTES / sizeof ( short ); i++)
154 			word(buf[i]);
155 	}
156 	(void) close(hf);
157 }
158 #endif OBJ
159 
160 #ifdef OBJ
161 magic2()
162 {
163 	struct pxhdr pxhd;
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 	if (lseek(ofil, (off_t)0, 0) == -1)
176 		perror("magic2: lseek1"), panic("magic2");
177 	write(ofil, (char *) (&magichdr), sizeof(struct exec));
178 	if (lseek(ofil, (off_t)(HEADER_BYTES - sizeof(pxhd)), 0) == -1)
179 		perror("magic2: lseek2"), panic("magic2");
180 	write(ofil, (char *) (&pxhd), sizeof (pxhd));
181 }
182 #endif OBJ
183 #endif
184 
185 #ifdef PXP
186 writef(i, cp)
187 {
188 
189 	write(i, cp, strlen(cp));
190 }
191 #endif
192