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