xref: /freebsd/usr.bin/tip/tip/tipout.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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 "tip.h"
36 
37 /*
38  * tip
39  *
40  * lower fork of tip -- handles passive side
41  *  reading from the remote host
42  */
43 
44 static	jmp_buf sigbuf;
45 
46 static void	intIOT(int);
47 static void	intEMT(int);
48 static void	intTERM(int);
49 static void	intSYS(int);
50 
51 /*
52  * TIPOUT wait state routine --
53  *   sent by TIPIN when it wants to posses the remote host
54  */
55 /*ARGSUSED*/
56 static void
57 intIOT(int signo)
58 {
59 	write(repdes[1],&ccc,1);
60 	read(fildes[0], &ccc,1);
61 	longjmp(sigbuf, 1);
62 }
63 
64 /*
65  * Scripting command interpreter --
66  *  accepts script file name over the pipe and acts accordingly
67  */
68 /*ARGSUSED*/
69 static void
70 intEMT(int signo)
71 {
72 	char c, line[256];
73 	char *pline = line;
74 	char reply;
75 
76 	read(fildes[0], &c, 1);
77 	while (c != '\n' && (size_t)(pline - line) < sizeof(line)) {
78 		*pline++ = c;
79 		read(fildes[0], &c, 1);
80 	}
81 	*pline = '\0';
82 	if (boolean(value(SCRIPT)) && fscript != NULL)
83 		fclose(fscript);
84 	if (pline == line) {
85 		setboolean(value(SCRIPT), FALSE);
86 		reply = 'y';
87 	} else {
88 		if ((fscript = fopen(line, "a")) == NULL)
89 			reply = 'n';
90 		else {
91 			reply = 'y';
92 			setboolean(value(SCRIPT), TRUE);
93 		}
94 	}
95 	write(repdes[1], &reply, 1);
96 	longjmp(sigbuf, 1);
97 }
98 
99 static void
100 intTERM(int signo)
101 {
102 	if (boolean(value(SCRIPT)) && fscript != NULL)
103 		fclose(fscript);
104 	if (signo && tipin_pid)
105 		kill(tipin_pid, signo);
106 	exit(0);
107 }
108 
109 /*ARGSUSED*/
110 static void
111 intSYS(int signo)
112 {
113 	setboolean(value(BEAUTIFY), !boolean(value(BEAUTIFY)));
114 	longjmp(sigbuf, 1);
115 }
116 
117 /*
118  * ****TIPOUT   TIPOUT****
119  */
120 void
121 tipout(void)
122 {
123 	char buf[BUFSIZ];
124 	char *cp;
125 	ssize_t scnt;
126 	size_t cnt;
127 	sigset_t mask, 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 	sigprocmask(SIG_BLOCK, NULL, &omask);
138 	for (;;) {
139 		sigprocmask(SIG_SETMASK, &omask, NULL);
140 		scnt = read(FD, buf, BUFSIZ);
141 		if (scnt <= 0) {
142 			/* lost carrier */
143 			if (scnt == 0 ||
144 			    (scnt < 0 && (errno == EIO || errno == ENXIO))) {
145 				sigemptyset(&mask);
146 				sigaddset(&mask, SIGTERM);
147 				sigprocmask(SIG_BLOCK, &mask, NULL);
148 				intTERM(SIGHUP);
149 				/*NOTREACHED*/
150 			}
151 			continue;
152 		}
153 		cnt = scnt;
154 		sigemptyset(&mask);
155 		sigaddset(&mask, SIGEMT);
156 		sigaddset(&mask, SIGTERM);
157 		sigaddset(&mask, SIGIOT);
158 		sigaddset(&mask, SIGSYS);
159 		sigprocmask(SIG_BLOCK, &mask, NULL);
160 		for (cp = buf; cp < buf + cnt; cp++)
161 			*cp &= STRIP_PAR;
162 		write(STDOUT_FILENO, buf, cnt);
163 		if (boolean(value(SCRIPT)) && fscript != NULL) {
164 			if (!boolean(value(BEAUTIFY))) {
165 				fwrite(buf, 1, cnt, fscript);
166 			} else {
167 				for (cp = buf; cp < buf + cnt; cp++)
168 					if ((*cp >= ' ' && *cp <= '~') ||
169 					    any(*cp, value(EXCEPTIONS)))
170 						putc(*cp, fscript);
171 			}
172 			for (cp = buf; cp < buf + cnt; cp++) {
173 				if (!isgraph(*cp)) {
174 					fflush(fscript);
175 					break;
176 				}
177 			}
178 		}
179 	}
180 }
181