tipout.c revision 1.10 1 /* $NetBSD: tipout.c,v 1.10 2006/04/03 00:51:13 perry Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)tipout.c 8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: tipout.c,v 1.10 2006/04/03 00:51:13 perry Exp $");
38 #endif /* not lint */
39
40 #include "tip.h"
41 /*
42 * tip
43 *
44 * lower fork of tip -- handles passive side
45 * reading from the remote host
46 */
47
48 static jmp_buf sigbuf;
49
50 void intEMT(int);
51 void intIOT(int);
52 void intSYS(int);
53 void intTERM(int);
54
55 /*
56 * TIPOUT wait state routine --
57 * sent by TIPIN when it wants to posses the remote host
58 */
59 void
60 intIOT(int dummy)
61 {
62
63 write(repdes[1],&ccc,1);
64 read(fildes[0], &ccc,1);
65 longjmp(sigbuf, 1);
66 }
67
68 /*
69 * Scripting command interpreter --
70 * accepts script file name over the pipe and acts accordingly
71 */
72 void
73 intEMT(int dummy)
74 {
75 char c, line[256];
76 char *pline = line;
77 char reply;
78
79 read(fildes[0], &c, 1);
80 while (c != '\n' && line + sizeof line - pline > 0) {
81 *pline++ = c;
82 read(fildes[0], &c, 1);
83 }
84 *pline = '\0';
85 if (boolean(value(SCRIPT)) && fscript != NULL)
86 fclose(fscript);
87 if (pline == line) {
88 setboolean(value(SCRIPT), FALSE);
89 reply = 'y';
90 } else {
91 if ((fscript = fopen(line, "a")) == NULL)
92 reply = 'n';
93 else {
94 reply = 'y';
95 setboolean(value(SCRIPT), TRUE);
96 }
97 }
98 write(repdes[1], &reply, 1);
99 longjmp(sigbuf, 1);
100 }
101
102 void
103 intTERM(int dummy)
104 {
105
106 if (boolean(value(SCRIPT)) && fscript != NULL)
107 fclose(fscript);
108 exit(0);
109 }
110
111 void
112 intSYS(int dummy)
113 {
114
115 setboolean(value(BEAUTIFY), !boolean(value(BEAUTIFY)));
116 longjmp(sigbuf, 1);
117 }
118
119 /*
120 * ****TIPOUT TIPOUT****
121 */
122 void
123 tipout(void)
124 {
125 char buf[BUFSIZ];
126 char *cp;
127 int cnt;
128 int omask;
129
130 signal(SIGINT, SIG_IGN);
131 signal(SIGQUIT, SIG_IGN);
132 signal(SIGEMT, intEMT); /* attention from TIPIN */
133 signal(SIGTERM, intTERM); /* time to go signal */
134 signal(SIGIOT, intIOT); /* scripting going on signal */
135 signal(SIGHUP, intTERM); /* for dial-ups */
136 signal(SIGSYS, intSYS); /* beautify toggle */
137 (void) setjmp(sigbuf);
138 for (omask = 0;; sigsetmask(omask)) {
139 cnt = read(FD, buf, BUFSIZ);
140 if (cnt <= 0) {
141 /* lost carrier || EOF */
142 if ((cnt < 0 && errno == EIO) || (cnt == 0)) {
143 sigblock(sigmask(SIGTERM));
144 intTERM(0);
145 /*NOTREACHED*/
146 }
147 continue;
148 }
149 #define ALLSIGS sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS)
150 omask = sigblock(ALLSIGS);
151 for (cp = buf; cp < buf + cnt; cp++)
152 *cp &= STRIP_PAR;
153 write(1, buf, cnt);
154 if (boolean(value(SCRIPT)) && fscript != NULL) {
155 if (!boolean(value(BEAUTIFY))) {
156 fwrite(buf, 1, cnt, fscript);
157 continue;
158 }
159 for (cp = buf; cp < buf + cnt; cp++)
160 if ((*cp >= ' ' && *cp <= '~') ||
161 any(*cp, value(EXCEPTIONS)))
162 putc(*cp, fscript);
163 }
164 }
165 }
166