xref: /onnv-gate/usr/src/cmd/tip/tipout.c (revision 2590:3709c0dd648e)
10Sstevel@tonic-gate /*
2*2590Ssn199410  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
5549Smuffin 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
80Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
90Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
100Sstevel@tonic-gate  */
110Sstevel@tonic-gate 
12549Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
130Sstevel@tonic-gate 
140Sstevel@tonic-gate #include "tip.h"
15*2590Ssn199410 #include <limits.h>
16549Smuffin 
170Sstevel@tonic-gate /*
180Sstevel@tonic-gate  * tip
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * lower fork of tip -- handles passive side
210Sstevel@tonic-gate  *  reading from the remote host
220Sstevel@tonic-gate  */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate static	sigjmp_buf sigbuf;
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * TIPOUT wait state routine --
280Sstevel@tonic-gate  *   sent by TIPIN when it wants to posses the remote host
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate void
intIOT(void)31549Smuffin intIOT(void)
320Sstevel@tonic-gate {
330Sstevel@tonic-gate 
34549Smuffin 	(void) write(repdes[1], &ccc, 1);
35549Smuffin 	(void) read(fildes[0], &ccc, 1);
360Sstevel@tonic-gate 	siglongjmp(sigbuf, 1);
370Sstevel@tonic-gate }
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Scripting command interpreter --
410Sstevel@tonic-gate  *  accepts script file name over the pipe and acts accordingly
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate void
intEMT(void)44549Smuffin intEMT(void)
450Sstevel@tonic-gate {
46*2590Ssn199410 	char c, line[PATH_MAX];
47549Smuffin 	char *pline = line;
480Sstevel@tonic-gate 	char reply;
490Sstevel@tonic-gate 
50549Smuffin 	(void) read(fildes[0], &c, 1);
51*2590Ssn199410 	while (c != '\n' && line + sizeof (line) - pline > 1) {
520Sstevel@tonic-gate 		*pline++ = c;
53549Smuffin 		(void) read(fildes[0], &c, 1);
540Sstevel@tonic-gate 	}
550Sstevel@tonic-gate 	*pline = '\0';
560Sstevel@tonic-gate 	if (boolean(value(SCRIPT)) && fscript != NULL)
57549Smuffin 		(void) fclose(fscript);
580Sstevel@tonic-gate 	if (pline == line) {
590Sstevel@tonic-gate 		boolean(value(SCRIPT)) = FALSE;
600Sstevel@tonic-gate 		reply = 'y';
610Sstevel@tonic-gate 	} else {
620Sstevel@tonic-gate 		if ((fscript = fopen(line, "a")) == NULL)
630Sstevel@tonic-gate 			reply = 'n';
640Sstevel@tonic-gate 		else {
650Sstevel@tonic-gate 			reply = 'y';
660Sstevel@tonic-gate 			boolean(value(SCRIPT)) = TRUE;
670Sstevel@tonic-gate 		}
680Sstevel@tonic-gate 	}
69549Smuffin 	(void) write(repdes[1], &reply, 1);
700Sstevel@tonic-gate 	siglongjmp(sigbuf, 1);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate 
730Sstevel@tonic-gate void
intTERM(void)74549Smuffin intTERM(void)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	if (boolean(value(SCRIPT)) && fscript != NULL)
78549Smuffin 		(void) fclose(fscript);
790Sstevel@tonic-gate 	exit(0);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate void
intSYS(void)83549Smuffin intSYS(void)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	boolean(value(BEAUTIFY)) = !boolean(value(BEAUTIFY));
870Sstevel@tonic-gate 	siglongjmp(sigbuf, 1);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * ****TIPOUT   TIPOUT****
920Sstevel@tonic-gate  */
93549Smuffin void
tipout(void)94549Smuffin tipout(void)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	char buf[BUFSIZ];
97549Smuffin 	char *cp;
98549Smuffin 	int cnt;
990Sstevel@tonic-gate 	sigset_t omask, bmask, tmask;
1000Sstevel@tonic-gate 
101549Smuffin 	(void) signal(SIGINT, SIG_IGN);
102549Smuffin 	(void) signal(SIGQUIT, SIG_IGN);
103549Smuffin 	/* attention from TIPIN */
104549Smuffin 	(void) signal(SIGEMT, (sig_handler_t)intEMT);
105549Smuffin 	/* time to go signal */
106549Smuffin 	(void) signal(SIGTERM, (sig_handler_t)intTERM);
107549Smuffin 	/* scripting going on signal */
108549Smuffin 	(void) signal(SIGIOT, (sig_handler_t)intIOT);
109549Smuffin 	/* for dial-ups */
110549Smuffin 	(void) signal(SIGHUP, (sig_handler_t)intTERM);
111549Smuffin 	/* beautify toggle */
112549Smuffin 	(void) signal(SIGSYS, (sig_handler_t)intSYS);
1130Sstevel@tonic-gate 	(void) sigsetjmp(sigbuf, 1);
1140Sstevel@tonic-gate 
115549Smuffin 	(void) sigemptyset(&omask);
116549Smuffin 	(void) sigemptyset(&bmask);
117549Smuffin 	(void) sigaddset(&bmask, SIGEMT);
118549Smuffin 	(void) sigaddset(&bmask, SIGTERM);
119549Smuffin 	(void) sigaddset(&bmask, SIGIOT);
120549Smuffin 	(void) sigaddset(&bmask, SIGSYS);
121549Smuffin 	(void) sigemptyset(&tmask);
122549Smuffin 	(void) sigaddset(&tmask, SIGTERM);
1230Sstevel@tonic-gate 	for (;;) {
1240Sstevel@tonic-gate 		cnt = read(FD, buf, BUFSIZ);
1250Sstevel@tonic-gate 		if (cnt <= 0) {
1260Sstevel@tonic-gate 			/*
1270Sstevel@tonic-gate 			 * If dialback is specified, ignore the hangup
1280Sstevel@tonic-gate 			 * and clear the hangup condition on the device.
1290Sstevel@tonic-gate 			 */
1300Sstevel@tonic-gate 			if (cnt == 0 && DB) {
1310Sstevel@tonic-gate 				int fd;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 				DB = 0;
1340Sstevel@tonic-gate 				if ((fd = open(DV, O_RDWR)) >= 0) {
1350Sstevel@tonic-gate 					if (fd != FD)
136549Smuffin 						(void) close(fd);
1370Sstevel@tonic-gate 				}
1380Sstevel@tonic-gate 				continue;
1390Sstevel@tonic-gate 			}
1400Sstevel@tonic-gate 			/* lost carrier */
1410Sstevel@tonic-gate 			if ((cnt < 0 && errno == EIO) ||
1420Sstevel@tonic-gate 			    (cnt == 0)) {
1430Sstevel@tonic-gate 				(void) sigprocmask(SIG_BLOCK, &tmask, NULL);
1440Sstevel@tonic-gate 				intTERM();
1450Sstevel@tonic-gate 				/*NOTREACHED*/
1460Sstevel@tonic-gate 			}
1470Sstevel@tonic-gate 		} else {
1480Sstevel@tonic-gate 			(void) sigprocmask(SIG_BLOCK, &bmask, &omask);
1490Sstevel@tonic-gate 			if (!noparity)
1500Sstevel@tonic-gate 				for (cp = buf; cp < buf + cnt; cp++)
1510Sstevel@tonic-gate 					*cp &= 0177;
1520Sstevel@tonic-gate 
153549Smuffin 			(void) write(1, buf, cnt);
1540Sstevel@tonic-gate 			if (boolean(value(SCRIPT)) && fscript != NULL) {
1550Sstevel@tonic-gate 				if (!boolean(value(BEAUTIFY))) {
156549Smuffin 					(void) fwrite(buf, 1, cnt, fscript);
1570Sstevel@tonic-gate 				} else {
1580Sstevel@tonic-gate 					for (cp = buf; cp < buf + cnt; cp++)
1590Sstevel@tonic-gate 						if ((*cp >= ' ' && *cp <= '~')||
1600Sstevel@tonic-gate 						    any(*cp, value(EXCEPTIONS)))
161549Smuffin 							(void) putc(*cp,
162549Smuffin 							    fscript);
1630Sstevel@tonic-gate 				}
1640Sstevel@tonic-gate 			}
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &omask, NULL);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate }
169