xref: /original-bsd/usr.bin/tip/tipout.c (revision 72e3688a)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)tipout.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "tip.h"
13 /*
14  * tip
15  *
16  * lower fork of tip -- handles passive side
17  *  reading from the remote host
18  */
19 
20 static	jmp_buf sigbuf;
21 
22 /*
23  * TIPOUT wait state routine --
24  *   sent by TIPIN when it wants to posses the remote host
25  */
26 void
27 intIOT()
28 {
29 
30 	write(repdes[1],&ccc,1);
31 	read(fildes[0], &ccc,1);
32 	longjmp(sigbuf, 1);
33 }
34 
35 /*
36  * Scripting command interpreter --
37  *  accepts script file name over the pipe and acts accordingly
38  */
39 void
40 intEMT()
41 {
42 	char c, line[256];
43 	register char *pline = line;
44 	char reply;
45 
46 	read(fildes[0], &c, 1);
47 	while (c != '\n') {
48 		*pline++ = c;
49 		read(fildes[0], &c, 1);
50 	}
51 	*pline = '\0';
52 	if (boolean(value(SCRIPT)) && fscript != NULL)
53 		fclose(fscript);
54 	if (pline == line) {
55 		boolean(value(SCRIPT)) = FALSE;
56 		reply = 'y';
57 	} else {
58 		if ((fscript = fopen(line, "a")) == NULL)
59 			reply = 'n';
60 		else {
61 			reply = 'y';
62 			boolean(value(SCRIPT)) = TRUE;
63 		}
64 	}
65 	write(repdes[1], &reply, 1);
66 	longjmp(sigbuf, 1);
67 }
68 
69 void
70 intTERM()
71 {
72 
73 	if (boolean(value(SCRIPT)) && fscript != NULL)
74 		fclose(fscript);
75 	exit(0);
76 }
77 
78 void
79 intSYS()
80 {
81 
82 	boolean(value(BEAUTIFY)) = !boolean(value(BEAUTIFY));
83 	longjmp(sigbuf, 1);
84 }
85 
86 /*
87  * ****TIPOUT   TIPOUT****
88  */
89 tipout()
90 {
91 	char buf[BUFSIZ];
92 	register char *cp;
93 	register int cnt;
94 	extern int errno;
95 	int omask;
96 
97 	signal(SIGINT, SIG_IGN);
98 	signal(SIGQUIT, SIG_IGN);
99 	signal(SIGEMT, intEMT);		/* attention from TIPIN */
100 	signal(SIGTERM, intTERM);	/* time to go signal */
101 	signal(SIGIOT, intIOT);		/* scripting going on signal */
102 	signal(SIGHUP, intTERM);	/* for dial-ups */
103 	signal(SIGSYS, intSYS);		/* beautify toggle */
104 	(void) setjmp(sigbuf);
105 	for (omask = 0;; sigsetmask(omask)) {
106 		cnt = read(FD, buf, BUFSIZ);
107 		if (cnt <= 0) {
108 			/* lost carrier */
109 			if (cnt < 0 && errno == EIO) {
110 				sigblock(sigmask(SIGTERM));
111 				intTERM();
112 				/*NOTREACHED*/
113 			}
114 			continue;
115 		}
116 #define	ALLSIGS	sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS)
117 		omask = sigblock(ALLSIGS);
118 		for (cp = buf; cp < buf + cnt; cp++)
119 			*cp &= 0177;
120 		write(1, buf, cnt);
121 		if (boolean(value(SCRIPT)) && fscript != NULL) {
122 			if (!boolean(value(BEAUTIFY))) {
123 				fwrite(buf, 1, cnt, fscript);
124 				continue;
125 			}
126 			for (cp = buf; cp < buf + cnt; cp++)
127 				if ((*cp >= ' ' && *cp <= '~') ||
128 				    any(*cp, value(EXCEPTIONS)))
129 					putc(*cp, fscript);
130 		}
131 	}
132 }
133