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