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