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