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