xref: /freebsd/usr.bin/tip/tip/tipout.c (revision 9207b4cff7b8d483f4dd3c62266c2b58819eb7f9)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)tipout.c	8.1 (Berkeley) 6/6/93";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41 
42 #include "tip.h"
43 #include <errno.h>
44 #include <unistd.h>
45 /*
46  * tip
47  *
48  * lower fork of tip -- handles passive side
49  *  reading from the remote host
50  */
51 
52 static	jmp_buf sigbuf;
53 
54 /*
55  * TIPOUT wait state routine --
56  *   sent by TIPIN when it wants to posses the remote host
57  */
58 void
59 intIOT()
60 {
61 
62 	write(repdes[1],&ccc,1);
63 	read(fildes[0], &ccc,1);
64 	longjmp(sigbuf, 1);
65 }
66 
67 /*
68  * Scripting command interpreter --
69  *  accepts script file name over the pipe and acts accordingly
70  */
71 void
72 intEMT()
73 {
74 	char c, line[256];
75 	register char *pline = line;
76 	char reply;
77 
78 	read(fildes[0], &c, 1);
79 	while (c != '\n' && pline - line < sizeof(line)) {
80 		*pline++ = c;
81 		read(fildes[0], &c, 1);
82 	}
83 	*pline = '\0';
84 	if (boolean(value(SCRIPT)) && fscript != NULL)
85 		fclose(fscript);
86 	if (pline == line) {
87 		boolean(value(SCRIPT)) = FALSE;
88 		reply = 'y';
89 	} else {
90 		if ((fscript = fopen(line, "a")) == NULL)
91 			reply = 'n';
92 		else {
93 			reply = 'y';
94 			boolean(value(SCRIPT)) = TRUE;
95 		}
96 	}
97 	write(repdes[1], &reply, 1);
98 	longjmp(sigbuf, 1);
99 }
100 
101 void
102 intTERM()
103 {
104 
105 	if (boolean(value(SCRIPT)) && fscript != NULL)
106 		fclose(fscript);
107 	exit(0);
108 }
109 
110 void
111 intSYS()
112 {
113 
114 	boolean(value(BEAUTIFY)) = !boolean(value(BEAUTIFY));
115 	longjmp(sigbuf, 1);
116 }
117 
118 /*
119  * ****TIPOUT   TIPOUT****
120  */
121 void
122 tipout()
123 {
124 	char buf[BUFSIZ];
125 	register char *cp;
126 	register int cnt;
127 	int omask;
128 
129 	signal(SIGINT, SIG_IGN);
130 	signal(SIGQUIT, SIG_IGN);
131 	signal(SIGEMT, intEMT);		/* attention from TIPIN */
132 	signal(SIGTERM, intTERM);	/* time to go signal */
133 	signal(SIGIOT, intIOT);		/* scripting going on signal */
134 	signal(SIGHUP, intTERM);	/* for dial-ups */
135 	signal(SIGSYS, intSYS);		/* beautify toggle */
136 	(void) setjmp(sigbuf);
137 	for (omask = 0;; sigsetmask(omask)) {
138 		cnt = read(FD, buf, BUFSIZ);
139 		if (cnt <= 0) {
140 			/* lost carrier */
141 			if (cnt < 0 && errno == EIO) {
142 				sigblock(sigmask(SIGTERM));
143 				intTERM();
144 				/*NOTREACHED*/
145 			} else if (cnt == 0 && errno == ENOENT) {
146 				if (getppid() != 1)
147 					kill(getppid(),SIGUSR1);
148 				sigblock(sigmask(SIGTERM));
149 				intTERM();
150 				/*NOTREACHED*/
151 			} else if (cnt < 0) {
152 				if (getppid() != 1)
153 					kill(getppid(),SIGUSR1);
154 				sigblock(sigmask(SIGTERM));
155 				intTERM();
156 				/*NOTREACHED*/
157 			}
158 			continue;
159 		}
160 #define	ALLSIGS	sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS)
161 		omask = sigblock(ALLSIGS);
162 		for (cp = buf; cp < buf + cnt; cp++)
163 			*cp &= 0177;
164 		if (write(STDOUT_FILENO, buf, cnt) < 0)
165 			exit(1);
166 		if (boolean(value(SCRIPT)) && fscript != NULL) {
167 			if (!boolean(value(BEAUTIFY))) {
168 				fwrite(buf, 1, cnt, fscript);
169 				continue;
170 			}
171 			for (cp = buf; cp < buf + cnt; cp++)
172 				if ((*cp >= ' ' && *cp <= '~') ||
173 				    any(*cp, value(EXCEPTIONS)))
174 					putc(*cp, fscript);
175 		}
176 	}
177 }
178