xref: /original-bsd/usr.bin/pascal/pc/pc.c (revision fbed46ce)
1 static	char sccsid[] = "@(#)pc.c 3.13 11/23/81";
2 
3 #include <stdio.h>
4 #include <signal.h>
5 #include <wait.h>
6 
7 /*
8  * Pc - front end for Pascal compiler.
9  */
10 char	*pc0 = "/usr/lib/pc0";
11 char	*pc1 = "/lib/f1";
12 char	*pc2 = "/usr/lib/pc2";
13 char	*c2 = "/lib/c2";
14 char	*pc3 = "/usr/lib/pc3";
15 char	*ld = "/bin/ld";
16 char	*as = "/bin/as";
17 char	*lpc = "-lpc";
18 char	*crt0 = "/lib/crt0.o";
19 char	*mcrt0 = "/lib/mcrt0.o";
20 char	*gcrt0 = "/usr/lib/gcrt0.o";
21 
22 char	*mktemp();
23 char	*tname[2];
24 char	*tfile[2];
25 
26 char	*setsuf(), *savestr();
27 
28 int	Jflag, Sflag, Oflag, Tlflag, cflag, gflag, pflag;
29 int	debug;
30 
31 #define	NARGS	512
32 int	ldargx = 3;
33 int	pc0argx = 3;
34 char	*pc0args[NARGS] =	{ "pc0", "-o", "XXX" };
35 char	*pc1args[3] =		{ "pc1", 0, };
36 char	*pc2args[2] =		{ "pc2", 0 };
37 char	*c2args[4] =		{ "c2", 0, 0, 0 };
38 int	pc3argx = 1;
39 #define	pc3args	pc0args
40 #define	ldargs	pc0args
41 /* char	*pc3args[NARGS] =	{ "pc3", 0 }; */
42 /* char	*ldargs[NARGS] =	{ "ld", "-X", "/lib/crt0.o", 0, }; */
43 int	asargx;
44 char	*asargs[6] =		{ "as", 0, };
45 
46 /*
47  * If the number of .p arguments (np) is 1, and the number of .o arguments
48  * (nxo) is 0, and we successfully create an ``a.out'', then we remove
49  * the one .ps .o file (onepso).
50  */
51 int	np, nxo;
52 char	*onepso;
53 int	errs;
54 
55 int	onintr();
56 
57 main(argc, argv)
58 	int argc;
59 	char **argv;
60 {
61 	register char *argp;
62 	register int i;
63 	int savargx;
64 	char *t, c;
65 	int j;
66 
67 	argc--, argv++;
68 	if (argc == 0) {
69 		execl("/bin/cat", "cat", "/usr/lib/how_pc");
70 		exit(1);
71 	}
72 	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
73 		signal(SIGINT, onintr);
74 		signal(SIGTERM, onintr);
75 	}
76 	for (i = 0; i < argc; i++) {
77 		argp = argv[i];
78 		if (argp[0] != '-')
79 			continue;
80 		switch (argp[1]) {
81 
82 		case 'd':
83 			if (argp[2] == 0)
84 				debug++;
85 			continue;
86 		case 'i':
87 			pc0args[pc0argx++] = "-i";
88 			while (i+1 < argc && argv[i+1][0] != '-' &&
89 			    getsuf(argv[i+1]) != 'p') {
90 				pc0args[pc0argx++] = argv[i+1];
91 				i++;
92 			}
93 			if (i+1 == argc) {
94 				fprintf(stderr, "pc: bad -i construction\n");
95 				exit(1);
96 			}
97 			continue;
98 		case 'o':
99 			i++;
100 			if (i == argc) {
101 				fprintf(stderr, "pc: -o must specify file\n");
102 				exit(1);
103 			}
104 			c = getsuf(argv[i]);
105 			if (c == 'o' || c == 'p' || c == 'c') {
106 				fprintf(stderr, "pc: -o would overwrite %s\n",
107 				    argv[i]);
108 				exit(1);
109 			}
110 			continue;
111 		case 'O':
112 			Oflag = 1;
113 			continue;
114 		case 'S':
115 			Sflag = 1;
116 			continue;
117 		case 'J':
118 			Jflag = 1;
119 			continue;
120 		case 'T':
121 			switch (argp[2]) {
122 
123 			case '0':
124 				pc0 = "/usr/src/cmd/pc0/a.out";
125 				if (argp[3] != '\0') {
126 					pc0 = &argp[3];
127 				}
128 				continue;
129 			case '1':
130 				pc1 = "/usr/src/cmd/pcc/pc1";
131 				if (argp[3] != '\0') {
132 					pc1 = &argp[3];
133 				}
134 				continue;
135 			case '2':
136 				pc2 = "/usr/src/cmd/pascal/pc2";
137 				if (argp[3] != '\0') {
138 					pc2 = &argp[3];
139 				}
140 				continue;
141 			case '3':
142 				pc3 = "/usr/src/cmd/pascal/pc3";
143 				if (argp[3] != '\0') {
144 					pc3 = &argp[3];
145 				}
146 				continue;
147 			case 'l':
148 				Tlflag = 1;
149 				lpc = "/usr/src/lib/libpc/libpc";
150 				if (argp[3] != '\0') {
151 					lpc = &argp[3];
152 				}
153 				continue;
154 			}
155 			continue;
156 		case 'c':
157 			cflag = 1;
158 			continue;
159 		case 'l':
160 			if (argp[2])
161 				continue;
162 			/* fall into ... */
163 		case 'b':
164 		case 'g':
165 		case 's':
166 		case 'w':
167 		case 'z':
168 		case 'C':
169 			pc0args[pc0argx++] = argp;
170 			if (argp[1] == 'g')
171 				gflag = 1;
172 			continue;
173 		case 't':
174 			fprintf(stderr, "pc: -t is default; -C for checking\n");
175 			continue;
176 		case 'p':
177 			if (argp[2] == 'g')
178 				crt0 = gcrt0;
179 			else
180 				crt0 = mcrt0;
181 			if (!Tlflag)
182 				lpc = "-lpc_p";
183 			pflag = 1;
184 			continue;
185 		}
186 	}
187 	if (gflag && Oflag) {
188 		fprintf(stderr, "pc: warning: -g overrides -O\n");
189 		Oflag = 0;
190 	}
191 	tname[0] = mktemp("/tmp/p0XXXXXX");
192 	tname[1] = mktemp("/tmp/p1XXXXXX");
193 	savargx = pc0argx;
194 	for (i = 0; i < argc; i++) {
195 		argp = argv[i];
196 		if (argp[0] == '-')
197 			continue;
198 		if (suffix(argp) == 's') {
199 			asargx = 1;
200 			if (Jflag)
201 				asargs[asargx++] = "-J";
202 			asargs[asargx++] = argp;
203 			asargs[asargx++] = "-o";
204 			tfile[1] = setsuf(argp, 'o');
205 			asargs[asargx++] = tfile[1];
206 			asargs[asargx] = 0;
207 			if (dosys(as, asargs, 0, 0))
208 				continue;
209 			tfile[1] = 0;
210 			continue;
211 		}
212 		if (suffix(argp) != 'p')
213 			continue;
214 		tfile[0] = tname[0];
215 		pc0args[2] = tfile[0];
216 		pc0argx = savargx;
217 		if (pflag)
218 			pc0args[pc0argx++] = "-p";
219 		pc0args[pc0argx++] = argp;
220 		pc0args[pc0argx] = 0;
221 		if (dosys(pc0, pc0args, 0, 0))
222 			continue;
223 		pc1args[1] = tfile[0];
224 		tfile[1] = tname[1];
225 		if (dosys(pc1, pc1args, 0, tfile[1]))
226 			continue;
227 		unlink(tfile[0]);
228 		tfile[0] = tname[0];
229 		if (Oflag) {
230 			if (dosys(c2, c2args, tfile[1], tfile[0]))
231 				continue;
232 			unlink(tfile[1]);
233 			tfile[1] = tfile[0];
234 			tfile[0] = tname[1];
235 		}
236 		if (Sflag)
237 			tfile[0] = setsuf(argp, 's');
238 		if (dosys(pc2, pc2args, tfile[1], tfile[0]))
239 			continue;
240 		unlink(tfile[1]);
241 		tfile[1] = 0;
242 		if (Sflag) {
243 			tfile[0] = 0;
244 			continue;
245 		}
246 		asargx = 1;
247 		if (Jflag)
248 			asargs[asargx++] = "-J";
249 		asargs[asargx++] = tfile[0];
250 		asargs[asargx++] = "-o";
251 		tfile[1] = setsuf(argp, 'o');
252 		asargs[asargx++] = tfile[1];
253 		asargs[asargx] = 0;
254 		if (dosys(as, asargs, 0, 0))
255 			continue;
256 		tfile[1] = 0;
257 		remove();
258 	}
259 	if (errs || cflag || Sflag)
260 		done();
261 /* char	*pc3args[NARGS] =	{ "pc3", 0 }; */
262 	pc3args[0] = "pc3";
263 	for (i = 0; i < argc; i++) {
264 		argp = argv[i];
265 		if (!strcmp(argp, "-o"))
266 			i++;
267 		if (argp[0] == '-')
268 			continue;
269 		switch (getsuf(argp)) {
270 
271 		case 'o':
272 			pc3args[pc3argx++] = argp;
273 			nxo++;
274 			continue;
275 		case 's':
276 		case 'p':
277 			onepso = pc3args[pc3argx++] =
278 			    savestr(setsuf(argp, 'o'));
279 			np++;
280 			continue;
281 		}
282 	}
283 	pc3args[pc3argx] = 0;
284 	if (dosys(pc3, pc3args, 0, 0))
285 		done();
286 /* char	*ldargs[NARGS] =	{ "ld", "-X", "/lib/crt0.o", 0, }; */
287 	ldargs[0] = "ld";
288 	ldargs[1] = "-X";
289 	ldargs[2] = crt0;
290 	for (i = 0; i < argc; i++) {
291 		argp = argv[i];
292 		if (argp[0] != '-') {
293 			switch (getsuf(argp)) {
294 
295 			case 'p':
296 			case 's':
297 				ldargs[ldargx] = savestr(setsuf(argp, 'o'));
298 				break;
299 			default:
300 				ldargs[ldargx] = argp;
301 				break;
302 			}
303 			if (getsuf(ldargs[ldargx]) == 'o')
304 			for (j = 0; j < ldargx; j++)
305 				if (!strcmp(ldargs[j], ldargs[ldargx]))
306 					goto duplicate;
307 			ldargx++;
308 duplicate:
309 			continue;
310 		}
311 		switch (argp[1]) {
312 
313 		case 'i':
314 			while (i+1 < argc && argv[i+1][0] != '-' &&
315 			    getsuf(argv[i+1]) != 'p')
316 				i++;
317 			continue;
318 		case 'd':
319 			if (argp[2] == 0)
320 				continue;
321 			ldargs[ldargx++] = argp;
322 			continue;
323 		case 'o':
324 			ldargs[ldargx++] = argp;
325 			i++;
326 			ldargs[ldargx++] = argv[i];
327 			continue;
328 		case 'l':
329 			if (argp[2])
330 				ldargs[ldargx++] = argp;
331 			continue;
332 		case 'c':
333 		case 'g':
334 		case 'w':
335 		case 'p':
336 		case 'S':
337 		case 'J':
338 		case 'T':
339 		case 'O':
340 		case 'C':
341 		case 'b':
342 		case 's':
343 		case 'z':
344 			continue;
345 		default:
346 			ldargs[ldargx++] = argp;
347 			continue;
348 		}
349 	}
350 	ldargs[ldargx++] = lpc;
351 	if (gflag)
352 		ldargs[ldargx++] = "-lg";
353 	if (pflag) {
354 		ldargs[ldargx++] = "-lm_p";
355 		ldargs[ldargx++] = "-lc_p";
356 	} else {
357 		ldargs[ldargx++] = "-lm";
358 		ldargs[ldargx++] = "-lc";
359 	}
360 	ldargs[ldargx] = 0;
361 	if (dosys(ld, ldargs, 0, 0)==0 && np == 1 && nxo == 0)
362 		unlink(onepso);
363 	done();
364 }
365 
366 dosys(cmd, argv, in, out)
367 	char *cmd, **argv, *in, *out;
368 {
369 	union wait status;
370 	int pid;
371 
372 	if (debug) {
373 		int i;
374 		printf("%s:", cmd);
375 		for (i = 0; argv[i]; i++)
376 			printf(" %s", argv[i]);
377 		if (in)
378 			printf(" <%s", in);
379 		if (out)
380 			printf(" >%s", out);
381 		printf("\n");
382 	}
383 	pid = vfork();
384 	if (pid < 0) {
385 		fprintf(stderr, "pc: No more processes\n");
386 		done();
387 	}
388 	if (pid == 0) {
389 		if (in) {
390 			close(0);
391 			if (open(in, 0) != 0) {
392 				perror(in);
393 				exit(1);
394 			}
395 		}
396 		if (out) {
397 			close(1);
398 			unlink(out);
399 			if (creat(out, 0666) != 1) {
400 				perror(out);
401 				exit(1);
402 			}
403 		}
404 		signal(SIGINT, SIG_DFL);
405 		execv(cmd, argv);
406 		perror(cmd);
407 		exit(1);
408 	}
409 	while (wait(&status) != pid)
410 		;
411 	if (WIFSIGNALED(status)) {
412 		if (status.w_termsig != SIGINT)
413 			fprintf(stderr, "Fatal error in %s\n", cmd);
414 		errs = 100;
415 		done();
416 		/*NOTREACHED*/
417 	}
418 	if (status.w_retcode) {
419 		errs = 1;
420 		remove();
421 	}
422 	return (status.w_retcode);
423 }
424 
425 done()
426 {
427 
428 	remove();
429 	exit(errs);
430 }
431 
432 remove()
433 {
434 
435 	if (tfile[0])
436 		unlink(tfile[0]);
437 	if (tfile[1])
438 		unlink(tfile[1]);
439 }
440 
441 onintr()
442 {
443 
444 	errs = 1;
445 	done();
446 }
447 
448 getsuf(cp)
449 	char *cp;
450 {
451 
452 	if (*cp == 0)
453 		return;
454 	while (cp[1])
455 		cp++;
456 	if (cp[-1] != '.')
457 		return (0);
458 	return (*cp);
459 }
460 
461 char *
462 setsuf(as, ch)
463 	char *as;
464 {
465 	register char *s, *s1;
466 
467 	s = s1 = savestr(as);
468 	while (*s)
469 		if (*s++ == '/')
470 			s1 = s;
471 	s[-1] = ch;
472 	return (s1);
473 }
474 
475 #define	NSAVETAB	512
476 char	*savetab;
477 int	saveleft;
478 
479 char *
480 savestr(cp)
481 	register char *cp;
482 {
483 	register int len;
484 
485 	len = strlen(cp) + 1;
486 	if (len > saveleft) {
487 		saveleft = NSAVETAB;
488 		if (len > saveleft)
489 			saveleft = len;
490 		savetab = (char *)malloc(saveleft);
491 		if (savetab == 0) {
492 			fprintf(stderr, "ran out of memory (savestr)\n");
493 			exit(1);
494 		}
495 	}
496 	strncpy(savetab, cp, len);
497 	cp = savetab;
498 	savetab += len;
499 	return (cp);
500 }
501 
502 suffix(cp)
503 	char *cp;
504 {
505 
506 	if (cp[0] == 0 || cp[1] == 0)
507 		return (0);
508 	while (cp[1])
509 		cp++;
510 	if (cp[-1] == '.')
511 		return (*cp);
512 	return (0);
513 }
514