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