xref: /csrg-svn/usr.bin/tip/tipout.c (revision 62315)
119804Sdist /*
2*62315Sbostic  * Copyright (c) 1983, 1993
3*62315Sbostic  *	The Regents of the University of California.  All rights reserved.
435464Sbostic  *
542770Sbostic  * %sccs.include.redist.c%
619804Sdist  */
719804Sdist 
813280Ssam #ifndef lint
9*62315Sbostic static char sccsid[] = "@(#)tipout.c	8.1 (Berkeley) 06/06/93";
1035464Sbostic #endif /* not lint */
1113280Ssam 
123698Sroot #include "tip.h"
133698Sroot /*
143698Sroot  * tip
153698Sroot  *
163698Sroot  * lower fork of tip -- handles passive side
173698Sroot  *  reading from the remote host
183698Sroot  */
193698Sroot 
2013280Ssam static	jmp_buf sigbuf;
2113140Sralph 
223698Sroot /*
233698Sroot  * TIPOUT wait state routine --
243698Sroot  *   sent by TIPIN when it wants to posses the remote host
253698Sroot  */
2646864Sbostic void
intIOT()273698Sroot intIOT()
283698Sroot {
2913280Ssam 
303698Sroot 	write(repdes[1],&ccc,1);
313698Sroot 	read(fildes[0], &ccc,1);
3213280Ssam 	longjmp(sigbuf, 1);
333698Sroot }
343698Sroot 
353698Sroot /*
363698Sroot  * Scripting command interpreter --
373698Sroot  *  accepts script file name over the pipe and acts accordingly
383698Sroot  */
3946864Sbostic void
intEMT()403698Sroot intEMT()
413698Sroot {
423698Sroot 	char c, line[256];
433698Sroot 	register char *pline = line;
443698Sroot 	char reply;
453698Sroot 
463698Sroot 	read(fildes[0], &c, 1);
475137Ssam 	while (c != '\n') {
483698Sroot 		*pline++ = c;
493698Sroot 		read(fildes[0], &c, 1);
503698Sroot 	}
513698Sroot 	*pline = '\0';
523698Sroot 	if (boolean(value(SCRIPT)) && fscript != NULL)
533698Sroot 		fclose(fscript);
543698Sroot 	if (pline == line) {
553698Sroot 		boolean(value(SCRIPT)) = FALSE;
563698Sroot 		reply = 'y';
573698Sroot 	} else {
583698Sroot 		if ((fscript = fopen(line, "a")) == NULL)
593698Sroot 			reply = 'n';
603698Sroot 		else {
613698Sroot 			reply = 'y';
623698Sroot 			boolean(value(SCRIPT)) = TRUE;
633698Sroot 		}
643698Sroot 	}
653698Sroot 	write(repdes[1], &reply, 1);
6613280Ssam 	longjmp(sigbuf, 1);
673698Sroot }
683698Sroot 
6946864Sbostic void
intTERM()703698Sroot intTERM()
713698Sroot {
7213280Ssam 
733698Sroot 	if (boolean(value(SCRIPT)) && fscript != NULL)
743698Sroot 		fclose(fscript);
753698Sroot 	exit(0);
763698Sroot }
773698Sroot 
7846864Sbostic void
intSYS()793698Sroot intSYS()
803698Sroot {
8113280Ssam 
823698Sroot 	boolean(value(BEAUTIFY)) = !boolean(value(BEAUTIFY));
8313280Ssam 	longjmp(sigbuf, 1);
843698Sroot }
853698Sroot 
863698Sroot /*
873698Sroot  * ****TIPOUT   TIPOUT****
883698Sroot  */
tipout()893698Sroot tipout()
903698Sroot {
915258Sshannon 	char buf[BUFSIZ];
925258Sshannon 	register char *cp;
935258Sshannon 	register int cnt;
9415189Ssam 	extern int errno;
9513280Ssam 	int omask;
965258Sshannon 
973698Sroot 	signal(SIGINT, SIG_IGN);
983698Sroot 	signal(SIGQUIT, SIG_IGN);
993698Sroot 	signal(SIGEMT, intEMT);		/* attention from TIPIN */
1003698Sroot 	signal(SIGTERM, intTERM);	/* time to go signal */
1013698Sroot 	signal(SIGIOT, intIOT);		/* scripting going on signal */
1023698Sroot 	signal(SIGHUP, intTERM);	/* for dial-ups */
1033698Sroot 	signal(SIGSYS, intSYS);		/* beautify toggle */
10413280Ssam 	(void) setjmp(sigbuf);
10513280Ssam 	for (omask = 0;; sigsetmask(omask)) {
10613280Ssam 		cnt = read(FD, buf, BUFSIZ);
10715189Ssam 		if (cnt <= 0) {
10815189Ssam 			/* lost carrier */
10915189Ssam 			if (cnt < 0 && errno == EIO) {
11017185Sralph 				sigblock(sigmask(SIGTERM));
11115189Ssam 				intTERM();
11215189Ssam 				/*NOTREACHED*/
11315189Ssam 			}
1145258Sshannon 			continue;
11515189Ssam 		}
11617185Sralph #define	ALLSIGS	sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS)
11713280Ssam 		omask = sigblock(ALLSIGS);
1185258Sshannon 		for (cp = buf; cp < buf + cnt; cp++)
1195258Sshannon 			*cp &= 0177;
1205258Sshannon 		write(1, buf, cnt);
1213698Sroot 		if (boolean(value(SCRIPT)) && fscript != NULL) {
1225258Sshannon 			if (!boolean(value(BEAUTIFY))) {
1235333Sshannon 				fwrite(buf, 1, cnt, fscript);
1245258Sshannon 				continue;
1255258Sshannon 			}
12613280Ssam 			for (cp = buf; cp < buf + cnt; cp++)
12713280Ssam 				if ((*cp >= ' ' && *cp <= '~') ||
12813280Ssam 				    any(*cp, value(EXCEPTIONS)))
12913280Ssam 					putc(*cp, fscript);
1303698Sroot 		}
1313698Sroot 	}
1323698Sroot }
133