Home | History | Annotate | Line # | Download | only in tip
tipout.c revision 1.11
      1 /*	$NetBSD: tipout.c,v 1.11 2006/04/03 04:53:58 christos 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)tipout.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: tipout.c,v 1.11 2006/04/03 04:53:58 christos Exp $");
     38 #endif /* not lint */
     39 
     40 #include "tip.h"
     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 void	intEMT(int);
     51 void	intIOT(int);
     52 void	intSYS(int);
     53 void	intTERM(int);
     54 
     55 /*
     56  * TIPOUT wait state routine --
     57  *   sent by TIPIN when it wants to posses the remote host
     58  */
     59 void
     60 /*ARGSUSED*/
     61 intIOT(int dummy)
     62 {
     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  */
     73 void
     74 /*ARGSUSED*/
     75 intEMT(int dummy)
     76 {
     77 	char c, line[256];
     78 	char *pline = line;
     79 	char reply;
     80 
     81 	read(fildes[0], &c, 1);
     82 	while (c != '\n' && line + sizeof line - pline > 0) {
     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) {
     90 		setboolean(value(SCRIPT), FALSE);
     91 		reply = 'y';
     92 	} else {
     93 		if ((fscript = fopen(line, "a")) == NULL)
     94 			reply = 'n';
     95 		else {
     96 			reply = 'y';
     97 			setboolean(value(SCRIPT), TRUE);
     98 		}
     99 	}
    100 	write(repdes[1], &reply, 1);
    101 	longjmp(sigbuf, 1);
    102 }
    103 
    104 void
    105 /*ARGSUSED*/
    106 intTERM(int dummy)
    107 {
    108 
    109 	if (boolean(value(SCRIPT)) && fscript != NULL)
    110 		fclose(fscript);
    111 	exit(0);
    112 }
    113 
    114 void
    115 /*ARGSUSED*/
    116 intSYS(int dummy)
    117 {
    118 
    119 	setboolean(value(BEAUTIFY), !boolean(value(BEAUTIFY)));
    120 	longjmp(sigbuf, 1);
    121 }
    122 
    123 /*
    124  * ****TIPOUT   TIPOUT****
    125  */
    126 void
    127 tipout(void)
    128 {
    129 	char buf[BUFSIZ];
    130 	char *cp;
    131 	int cnt;
    132 	int omask;
    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);
    142 	for (omask = 0;; sigsetmask(omask)) {
    143 		cnt = read(FD, buf, BUFSIZ);
    144 		if (cnt <= 0) {
    145 			/* lost carrier || EOF */
    146 			if ((cnt < 0 && errno == EIO) || (cnt == 0)) {
    147 				sigblock(sigmask(SIGTERM));
    148 				intTERM(0);
    149 				/*NOTREACHED*/
    150 			}
    151 			continue;
    152 		}
    153 #define	ALLSIGS	sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS)
    154 		omask = sigblock(ALLSIGS);
    155 		for (cp = buf; cp < buf + cnt; cp++)
    156 			*cp &= STRIP_PAR;
    157 		write(1, buf, (size_t)cnt);
    158 		if (boolean(value(SCRIPT)) && fscript != NULL) {
    159 			if (!boolean(value(BEAUTIFY))) {
    160 				fwrite(buf, 1, (size_t)cnt, fscript);
    161 				continue;
    162 			}
    163 			for (cp = buf; cp < buf + cnt; cp++)
    164 				if ((*cp >= ' ' && *cp <= '~') ||
    165 				    any(*cp, value(EXCEPTIONS)))
    166 					putc(*cp, fscript);
    167 		}
    168 	}
    169 }
    170