tipout.c revision 11370
194380Sdfr/*
294380Sdfr * Copyright (c) 1983, 1993
394380Sdfr *	The Regents of the University of California.  All rights reserved.
494380Sdfr *
594380Sdfr * Redistribution and use in source and binary forms, with or without
6194919Sjhb * modification, are permitted provided that the following conditions
794380Sdfr * are met:
894380Sdfr * 1. Redistributions of source code must retain the above copyright
9104739Speter *    notice, this list of conditions and the following disclaimer.
10104739Speter * 2. Redistributions in binary form must reproduce the above copyright
1194380Sdfr *    notice, this list of conditions and the following disclaimer in the
1294380Sdfr *    documentation and/or other materials provided with the distribution.
1394380Sdfr * 3. All advertising materials mentioning features or use of this software
14100385Speter *    must display the following acknowledgement:
15174383Sjhb *	This product includes software developed by the University of
16119332Speter *	California, Berkeley and its contributors.
17119332Speter * 4. Neither the name of the University nor the names of its contributors
1894380Sdfr *    may be used to endorse or promote products derived from this software
1994380Sdfr *    without specific prior written permission.
2094380Sdfr *
21151721Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22151721Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23151721Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24151721Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25151721Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26151721Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27104739Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28104739Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29104739Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30104739Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31104739Speter * SUCH DAMAGE.
32104739Speter */
33171214Speter
34171214Speter#ifndef lint
35171214Speterstatic char sccsid[] = "@(#)tipout.c	8.1 (Berkeley) 6/6/93";
36171214Speter#endif /* not lint */
37171214Speter
38171214Speter#include "tip.h"
39194919Sjhb/*
40194919Sjhb * tip
41194919Sjhb *
42194919Sjhb * lower fork of tip -- handles passive side
43194919Sjhb *  reading from the remote host
44194919Sjhb */
4594380Sdfr
46119332Speterstatic	jmp_buf sigbuf;
47193235Srwatson
48193235Srwatson/*
49193235Srwatson * TIPOUT wait state routine --
50193235Srwatson *   sent by TIPIN when it wants to posses the remote host
51193235Srwatson */
52193235Srwatsonvoid
53193235SrwatsonintIOT()
54193235Srwatson{
55193235Srwatson
56193235Srwatson	write(repdes[1],&ccc,1);
57193235Srwatson	read(fildes[0], &ccc,1);
58193235Srwatson	longjmp(sigbuf, 1);
59193235Srwatson}
60193235Srwatson
61193235Srwatson/*
62193235Srwatson * Scripting command interpreter --
63193235Srwatson *  accepts script file name over the pipe and acts accordingly
64193235Srwatson */
65194392Sjhbvoid
66193235SrwatsonintEMT()
67193235Srwatson{
68193235Srwatson	char c, line[256];
69193235Srwatson	register char *pline = line;
70193235Srwatson	char reply;
71193235Srwatson
72193235Srwatson	read(fildes[0], &c, 1);
73193235Srwatson	while (c != '\n') {
74193235Srwatson		*pline++ = c;
75193235Srwatson		read(fildes[0], &c, 1);
76193235Srwatson	}
77193235Srwatson	*pline = '\0';
78193235Srwatson	if (boolean(value(SCRIPT)) && fscript != NULL)
79193235Srwatson		fclose(fscript);
80193235Srwatson	if (pline == line) {
81193235Srwatson		boolean(value(SCRIPT)) = FALSE;
82193235Srwatson		reply = 'y';
83193235Srwatson	} else {
84193235Srwatson		if ((fscript = fopen(line, "a")) == NULL)
85193235Srwatson			reply = 'n';
86193235Srwatson		else {
87193235Srwatson			reply = 'y';
88193235Srwatson			boolean(value(SCRIPT)) = TRUE;
89193235Srwatson		}
90193235Srwatson	}
91193235Srwatson	write(repdes[1], &reply, 1);
92193235Srwatson	longjmp(sigbuf, 1);
93193235Srwatson}
94193235Srwatson
95193235Srwatsonvoid
96193235SrwatsonintTERM()
97193235Srwatson{
98193235Srwatson
99193235Srwatson	if (boolean(value(SCRIPT)) && fscript != NULL)
100193235Srwatson		fclose(fscript);
101193235Srwatson	exit(0);
102193235Srwatson}
103193235Srwatson
104193235Srwatsonvoid
105193235SrwatsonintSYS()
106193235Srwatson{
107193235Srwatson
108193235Srwatson	boolean(value(BEAUTIFY)) = !boolean(value(BEAUTIFY));
109193235Srwatson	longjmp(sigbuf, 1);
110193235Srwatson}
111193235Srwatson
112193235Srwatson/*
113193235Srwatson * ****TIPOUT   TIPOUT****
114193235Srwatson */
115193235Srwatsontipout()
116193235Srwatson{
117193235Srwatson	char buf[BUFSIZ];
118193235Srwatson	register char *cp;
119193235Srwatson	register int cnt;
120193235Srwatson	extern int errno;
121193235Srwatson	int omask;
122193235Srwatson
123193235Srwatson	signal(SIGINT, SIG_IGN);
124193235Srwatson	signal(SIGQUIT, SIG_IGN);
125193235Srwatson	signal(SIGEMT, intEMT);		/* attention from TIPIN */
126193235Srwatson	signal(SIGTERM, intTERM);	/* time to go signal */
127193235Srwatson	signal(SIGIOT, intIOT);		/* scripting going on signal */
128193235Srwatson	signal(SIGHUP, intTERM);	/* for dial-ups */
129193235Srwatson	signal(SIGSYS, intSYS);		/* beautify toggle */
130193235Srwatson	(void) setjmp(sigbuf);
131193235Srwatson	for (omask = 0;; sigsetmask(omask)) {
132193235Srwatson		cnt = read(FD, buf, BUFSIZ);
133193235Srwatson		if (cnt <= 0) {
134193235Srwatson			/* lost carrier */
135193235Srwatson			if (cnt < 0 && errno == EIO) {
136193235Srwatson				sigblock(sigmask(SIGTERM));
137193235Srwatson				intTERM();
138193235Srwatson				/*NOTREACHED*/
139193235Srwatson			} else if (cnt == 0 && errno == ENOENT) {
140193235Srwatson				if (getppid() != 1)
141193235Srwatson					kill(getppid(),SIGUSR1);
142193235Srwatson				sigblock(sigmask(SIGTERM));
143193235Srwatson				intTERM();
144193235Srwatson				/*NOTREACHED*/
145193235Srwatson			} else if (cnt < 0) {
146193235Srwatson				if (getppid() != 1)
147193235Srwatson					kill(getppid(),SIGUSR1);
148193235Srwatson				sigblock(sigmask(SIGTERM));
149193235Srwatson				intTERM();
150193235Srwatson				/*NOTREACHED*/
151193235Srwatson			}
152193235Srwatson			continue;
153193235Srwatson		}
154193235Srwatson#define	ALLSIGS	sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS)
155193235Srwatson		omask = sigblock(ALLSIGS);
156193235Srwatson		for (cp = buf; cp < buf + cnt; cp++)
157193235Srwatson			*cp &= 0177;
158193235Srwatson		write(1, buf, cnt);
159193235Srwatson		if (boolean(value(SCRIPT)) && fscript != NULL) {
160193235Srwatson			if (!boolean(value(BEAUTIFY))) {
161193235Srwatson				fwrite(buf, 1, cnt, fscript);
162193235Srwatson				continue;
163193235Srwatson			}
164193235Srwatson			for (cp = buf; cp < buf + cnt; cp++)
165193235Srwatson				if ((*cp >= ' ' && *cp <= '~') ||
166193235Srwatson				    any(*cp, value(EXCEPTIONS)))
167193235Srwatson					putc(*cp, fscript);
168193235Srwatson		}
169193235Srwatson	}
170193235Srwatson}
171193235Srwatson