1 /* $NetBSD: tipout.c,v 1.13 2006/04/03 16:13:34 tls Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
36 static char sccsid
[] = "@(#)tipout.c 8.1 (Berkeley) 6/6/93";
38 __RCSID("$NetBSD: tipout.c,v 1.13 2006/04/03 16:13:34 tls Exp $");
45 * lower fork of tip -- handles passive side
46 * reading from the remote host
55 * TIPOUT wait state routine --
56 * sent by TIPIN when it wants to posses the remote host
62 (void)write(repdes
[1],&ccc
,1); /* We got the message */
63 (void)read(fildes
[0], &ccc
,1); /* Now wait for coprocess */
67 * Scripting command interpreter --
68 * accepts script file name over the pipe and acts accordingly
77 (void)read(fildes
[0], &c
, 1); /* We got the message */
78 while (c
!= '\n' && line
+ sizeof line
- pline
> 0) {
80 (void)read(fildes
[0], &c
, 1);
83 if (boolean(value(SCRIPT
)) && fscript
!= NULL
)
84 (void)fclose(fscript
);
86 setboolean(value(SCRIPT
), FALSE
);
89 if ((fscript
= fopen(line
, "a")) == NULL
)
93 setboolean(value(SCRIPT
), TRUE
);
96 (void)write(repdes
[1], &reply
, 1); /* Now coprocess waits for us */
101 intTERM(int dummy __unused
)
104 if (boolean(value(SCRIPT
)) && fscript
!= NULL
)
105 (void)fclose(fscript
);
113 setboolean(value(BEAUTIFY
), !boolean(value(BEAUTIFY
)));
117 * ****TIPOUT TIPOUT****
126 struct pollfd pfd
[2];
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*/
133 pfd
[0].fd
= attndes
[0];
134 pfd
[0].events
= POLLIN
;
136 pfd
[1].events
= POLLIN
|POLLHUP
;
138 for (omask
= 0;; (void)sigsetmask(omask
)) {
140 if (poll(pfd
, 2, -1) > 0) {
142 if (pfd
[0].revents
& POLLIN
)
143 if (read(attndes
[0], &ccc
, 1) > 0) {
146 intIOT(); /* TIPIN wants us to wait */
149 intEMT(); /* TIPIN wants us to script */
152 intSYS(); /* "Beautify" value toggle */
160 if (pfd
[1].revents
& (POLLIN
|POLLHUP
)) {
161 cnt
= read(FD
, buf
, BUFSIZ
);
163 /* lost carrier || EOF */
164 if ((cnt
< 0 && errno
== EIO
) || (cnt
== 0)) {
165 (void)sigblock(sigmask(SIGTERM
));
171 omask
= sigblock(SIGTERM
);
172 for (cp
= buf
; cp
< buf
+ cnt
; cp
++)
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
);
180 for (cp
= buf
; cp
< buf
+ cnt
; cp
++)
181 if ((*cp
>= ' ' && *cp
<= '~') ||
182 any(*cp
, value(EXCEPTIONS
)))
183 (void)putc(*cp
, fscript
);