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