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