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