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