[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.16

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