1 /* $NetBSD: tipout.c,v 1.15 2011/09/06 18:33:01 joerg 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 #include <poll.h>
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)tipout.c 8.1 (Berkeley) 6/6/93";
37 #endif
38 __RCSID("$NetBSD: tipout.c,v 1.15 2011/09/06 18:33:01 joerg Exp $");
39 #endif /* not lint */
40
41 #include "tip.h"
42 /*
43 * tip
44 *
45 * lower fork of tip -- handles passive side
46 * reading from the remote host
47 */
48
49 void intEMT(void);
50 void intIOT(void);
51 void intSYS(void);
52 __dead static void intTERM(int);
53
54 /*
55 * TIPOUT wait state routine --
56 * sent by TIPIN when it wants to posses the remote host
57 */
58 void
intIOT(void)59 intIOT(void)
60 {
61
62 (void)write(repdes[1],&ccc,1); /* We got the message */
63 (void)read(fildes[0], &ccc,1); /* Now wait for coprocess */
64 }
65
66 /*
67 * Scripting command interpreter --
68 * accepts script file name over the pipe and acts accordingly
69 */
70 void
intEMT(void)71 intEMT(void)
72 {
73 char c, line[256];
74 char *pline = line;
75 char reply;
76
77 (void)read(fildes[0], &c, 1); /* We got the message */
78 while (c != '\n' && line + sizeof line - pline > 0) {
79 *pline++ = c;
80 (void)read(fildes[0], &c, 1);
81 }
82 *pline = '\0';
83 if (boolean(value(SCRIPT)) && fscript != NULL)
84 (void)fclose(fscript);
85 if (pline == line) {
86 setboolean(value(SCRIPT), FALSE);
87 reply = 'y';
88 } else {
89 if ((fscript = fopen(line, "a")) == NULL)
90 reply = 'n';
91 else {
92 reply = 'y';
93 setboolean(value(SCRIPT), TRUE);
94 }
95 }
96 (void)write(repdes[1], &reply, 1); /* Now coprocess waits for us */
97 }
98
99 static void
100 /*ARGSUSED*/
intTERM(int dummy __unused)101 intTERM(int dummy __unused)
102 {
103
104 if (boolean(value(SCRIPT)) && fscript != NULL)
105 (void)fclose(fscript);
106 exit(0);
107 }
108
109 void
intSYS(void)110 intSYS(void)
111 {
112
113 setboolean(value(BEAUTIFY), !boolean(value(BEAUTIFY)));
114 }
115
116 /*
117 * ****TIPOUT TIPOUT****
118 */
119 void
tipout(void)120 tipout(void)
121 {
122 char buf[BUFSIZ];
123 char *cp;
124 int cnt;
125 int omask;
126 struct pollfd pfd[2];
127
128 (void)signal(SIGINT, SIG_IGN);
129 (void)signal(SIGQUIT, SIG_IGN);
130 (void)signal(SIGHUP, intTERM); /* for dial-ups */
131 (void)signal(SIGTERM, intTERM); /* time to go signal*/
132
133 pfd[0].fd = attndes[0];
134 pfd[0].events = POLLIN;
135 pfd[1].fd = FD;
136 pfd[1].events = POLLIN|POLLHUP;
137
138 for (omask = 0;; (void)sigsetmask(omask)) {
139
140 if (poll(pfd, 2, -1) > 0) {
141
142 if (pfd[0].revents & POLLIN)
143 if (read(attndes[0], &ccc, 1) > 0) {
144 switch(ccc) {
145 case 'W':
146 intIOT(); /* TIPIN wants us to wait */
147 break;
148 case 'S':
149 intEMT(); /* TIPIN wants us to script */
150 break;
151 case 'B':
152 intSYS(); /* "Beautify" value toggle */
153 break;
154 default:
155 break;
156 }
157 }
158 }
159
160 if (pfd[1].revents & (POLLIN|POLLHUP)) {
161 cnt = read(FD, buf, BUFSIZ);
162 if (cnt <= 0) {
163 /* lost carrier || EOF */
164 if ((cnt < 0 && errno == EIO) || (cnt == 0)) {
165 (void)sigblock(sigmask(SIGTERM));
166 intTERM(0);
167 /*NOTREACHED*/
168 }
169 continue;
170 }
171 omask = sigblock(SIGTERM);
172 for (cp = buf; cp < buf + cnt; cp++)
173 *cp &= STRIP_PAR;
174 (void)write(1, buf, (size_t)cnt);
175 if (boolean(value(SCRIPT)) && fscript != NULL) {
176 if (!boolean(value(BEAUTIFY))) {
177 (void)fwrite(buf, 1, (size_t)cnt, fscript);
178 continue;
179 }
180 for (cp = buf; cp < buf + cnt; cp++)
181 if ((*cp >= ' ' && *cp <= '~') ||
182 any(*cp, value(EXCEPTIONS)))
183 (void)putc(*cp, fscript);
184 }
185 }
186 }
187 }
188