[BACK]Return to tipout.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tip

Annotation of src/usr.bin/tip/tipout.c, Revision 1.8

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