xref: /original-bsd/usr.bin/tip/tipout.c (revision d919d844)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)tipout.c	5.2 (Berkeley) 09/02/88";
20 #endif /* not lint */
21 
22 #include "tip.h"
23 /*
24  * tip
25  *
26  * lower fork of tip -- handles passive side
27  *  reading from the remote host
28  */
29 
30 static	jmp_buf sigbuf;
31 
32 /*
33  * TIPOUT wait state routine --
34  *   sent by TIPIN when it wants to posses the remote host
35  */
36 intIOT()
37 {
38 
39 	write(repdes[1],&ccc,1);
40 	read(fildes[0], &ccc,1);
41 	longjmp(sigbuf, 1);
42 }
43 
44 /*
45  * Scripting command interpreter --
46  *  accepts script file name over the pipe and acts accordingly
47  */
48 intEMT()
49 {
50 	char c, line[256];
51 	register char *pline = line;
52 	char reply;
53 
54 	read(fildes[0], &c, 1);
55 	while (c != '\n') {
56 		*pline++ = c;
57 		read(fildes[0], &c, 1);
58 	}
59 	*pline = '\0';
60 	if (boolean(value(SCRIPT)) && fscript != NULL)
61 		fclose(fscript);
62 	if (pline == line) {
63 		boolean(value(SCRIPT)) = FALSE;
64 		reply = 'y';
65 	} else {
66 		if ((fscript = fopen(line, "a")) == NULL)
67 			reply = 'n';
68 		else {
69 			reply = 'y';
70 			boolean(value(SCRIPT)) = TRUE;
71 		}
72 	}
73 	write(repdes[1], &reply, 1);
74 	longjmp(sigbuf, 1);
75 }
76 
77 intTERM()
78 {
79 
80 	if (boolean(value(SCRIPT)) && fscript != NULL)
81 		fclose(fscript);
82 	exit(0);
83 }
84 
85 intSYS()
86 {
87 
88 	boolean(value(BEAUTIFY)) = !boolean(value(BEAUTIFY));
89 	longjmp(sigbuf, 1);
90 }
91 
92 /*
93  * ****TIPOUT   TIPOUT****
94  */
95 tipout()
96 {
97 	char buf[BUFSIZ];
98 	register char *cp;
99 	register int cnt;
100 	extern int errno;
101 	int omask;
102 
103 	signal(SIGINT, SIG_IGN);
104 	signal(SIGQUIT, SIG_IGN);
105 	signal(SIGEMT, intEMT);		/* attention from TIPIN */
106 	signal(SIGTERM, intTERM);	/* time to go signal */
107 	signal(SIGIOT, intIOT);		/* scripting going on signal */
108 	signal(SIGHUP, intTERM);	/* for dial-ups */
109 	signal(SIGSYS, intSYS);		/* beautify toggle */
110 	(void) setjmp(sigbuf);
111 	for (omask = 0;; sigsetmask(omask)) {
112 		cnt = read(FD, buf, BUFSIZ);
113 		if (cnt <= 0) {
114 			/* lost carrier */
115 			if (cnt < 0 && errno == EIO) {
116 				sigblock(sigmask(SIGTERM));
117 				intTERM();
118 				/*NOTREACHED*/
119 			}
120 			continue;
121 		}
122 #define	ALLSIGS	sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS)
123 		omask = sigblock(ALLSIGS);
124 		for (cp = buf; cp < buf + cnt; cp++)
125 			*cp &= 0177;
126 		write(1, buf, cnt);
127 		if (boolean(value(SCRIPT)) && fscript != NULL) {
128 			if (!boolean(value(BEAUTIFY))) {
129 				fwrite(buf, 1, cnt, fscript);
130 				continue;
131 			}
132 			for (cp = buf; cp < buf + cnt; cp++)
133 				if ((*cp >= ' ' && *cp <= '~') ||
134 				    any(*cp, value(EXCEPTIONS)))
135 					putc(*cp, fscript);
136 		}
137 	}
138 }
139