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