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