xref: /original-bsd/usr.bin/pascal/pix/pix.c (revision b4971bb3)
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 copyright[] =
10 "@(#) Copyright (c) 1980, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)pix.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 /*
19  * pix - pi then px
20  *
21  * Bill Joy UCB August 26, 1977
22  */
23 
24 #include "whoami.h"
25 #include "objfmt.h"
26 #include "config.h"
27 #define	ERRS	1
28 
29 char	*name;
30 
31 int	onintr();
32 
33 #define	ETXTBSY	26
34 
35 main(argc, argv)
36 	int argc;
37 	char *argv[];
38 {
39 	register char **av;
40 	register int ac;
41 	int i, io, pid, status;
42 	extern errno;
43 
44 	do
45 		io = open("/dev/null", 0);
46 	while (io >= 0 && io < 3);
47 	for (io = 3; io < 15; io++)
48 		close(io);
49 	if ((signal(2, 1) & 01) == 0)
50 		signal(2, onintr);
51 	for (ac = 1; ac < argc; ac++)
52 		if (dotted(argv[ac], 'p')) {
53 			ac++;
54 			break;
55 		}
56 	name = "-o/tmp/pixaXXXXX" + 2;
57 	mktemp(name);
58 	for (;;) {
59 		io = creat(name, 0400);
60 		if (io > 0)
61 			break;
62 		if (name[8] == 'z') {
63 			perror(name);
64 			exit(1);
65 		}
66 		name[8]++;
67 	}
68 	pid = fork();
69 	if (pid == -1) {
70 		write(2, "No more processes\n", 18);
71 		onintr();
72 	}
73 	if (pid == 0) {
74 		if (io != 3) {
75 			write(2, "Impossible error in pix\n", 24);
76 			onintr();
77 		}
78 		argv[ac] = 0;
79 		argv[0] = name - 2;
80 		do
81 			execv(pi_comp, argv);
82 		while (errno == ETXTBSY);
83 		write(2, "Can't find pi\n", 14);
84 		onintr();
85 	}
86 	close(io);
87 	do
88 		i = wait(&status);
89 	while (i != pid && i != -1);
90 	if (i == -1 || (status & 0377))
91 		onintr();
92 	if (status != 0) {
93 		if ((status >> 8) == ERRS)
94 			write(2, "Execution suppressed due to compilation errors\n", 47);
95 		onintr();
96 	}
97 	ac--;
98 	argv[ac] = name;
99 	ac--;
100 	argv[ac] = "pix";
101 	argv[argc] = 0;
102 	do
103 		execv(px_debug, &argv[ac]);
104 	while (errno == ETXTBSY);
105 	write(2, "Can't find px\n", 14);
106 	onintr();
107 }
108 
109 dotted(cp, ch)
110 	char *cp, ch;
111 {
112 	register int i;
113 
114 	i = strlen(cp);
115 	return (i > 1 && cp[i - 2] == '.' && cp[i - 1] == ch);
116 }
117 
118 onintr()
119 {
120 
121 	signal(2, 1);
122 	unlink(name);
123 	exit(1);
124 }
125