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