xref: /original-bsd/usr.bin/tip/tipout.c (revision 1f3a482a)
1 /*	tipout.c	4.2	81/06/16	*/
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 }
71 
72 /*
73  * ****TIPOUT   TIPOUT****
74  */
75 tipout()
76 {
77 	signal(SIGINT, SIG_IGN);
78 	signal(SIGQUIT, SIG_IGN);
79 	signal(SIGEMT, intEMT);		/* attention from TIPIN */
80 	signal(SIGTERM, intTERM);	/* time to go signal */
81 	signal(SIGIOT, intIOT);		/* scripting going on signal */
82 	signal(SIGHUP, intTERM);	/* for dial-ups */
83 	signal(SIGSYS, intSYS);		/* beautify toggle */
84 
85 	while(1) {
86 		do {
87 			intflag = 0;
88 			read(FD,&ch,1);
89 			ch &= 0177;
90 		} while(intflag);
91 		write(1, &ch, 1);
92 		if (boolean(value(SCRIPT)) && fscript != NULL) {
93 			if (boolean(value(BEAUTIFY)) && ch < 040 &&
94 			    !any(ch, value(EXCEPTIONS)))
95 					continue;
96 			putc(ch, fscript);
97 		}
98 	}
99 }
100