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

1.20    ! nicm        1: /*     $OpenBSD: tipout.c,v 1.19 2009/10/27 23:59:45 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: #include "tip.h"
1.13      deraadt    34:
1.1       deraadt    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:
1.14      moritz     44: static void    intIOT(int);
                     45: static void    intEMT(int);
                     46: static void    intTERM(int);
                     47: static void    intSYS(int);
                     48:
1.1       deraadt    49: /*
                     50:  * TIPOUT wait state routine --
                     51:  *   sent by TIPIN when it wants to posses the remote host
                     52:  */
1.15      deraadt    53: /*ARGSUSED*/
1.14      moritz     54: static void
1.13      deraadt    55: intIOT(int signo)
1.1       deraadt    56: {
1.20    ! nicm       57:        write(tipin_fd, &ccc, 1);
        !            58:        read(tipin_fd, &ccc, 1);
1.1       deraadt    59:        longjmp(sigbuf, 1);
                     60: }
                     61:
                     62: /*
                     63:  * Scripting command interpreter --
                     64:  *  accepts script file name over the pipe and acts accordingly
                     65:  */
1.15      deraadt    66: /*ARGSUSED*/
1.14      moritz     67: static void
1.13      deraadt    68: intEMT(int signo)
1.1       deraadt    69: {
                     70:        char c, line[256];
1.8       millert    71:        char *pline = line;
1.1       deraadt    72:        char reply;
                     73:
1.20    ! nicm       74:        read(tipin_fd, &c, 1);
1.5       millert    75:        while (c != '\n' && pline - line < sizeof(line)) {
1.1       deraadt    76:                *pline++ = c;
1.20    ! nicm       77:                read(tipin_fd, &c, 1);
1.1       deraadt    78:        }
                     79:        *pline = '\0';
                     80:        if (boolean(value(SCRIPT)) && fscript != NULL)
                     81:                fclose(fscript);
                     82:        if (pline == line) {
1.4       millert    83:                setboolean(value(SCRIPT), FALSE);
1.1       deraadt    84:                reply = 'y';
                     85:        } else {
                     86:                if ((fscript = fopen(line, "a")) == NULL)
                     87:                        reply = 'n';
                     88:                else {
                     89:                        reply = 'y';
1.4       millert    90:                        setboolean(value(SCRIPT), TRUE);
1.1       deraadt    91:                }
                     92:        }
1.20    ! nicm       93:        write(tipin_fd, &reply, 1);
1.1       deraadt    94:        longjmp(sigbuf, 1);
                     95: }
                     96:
1.14      moritz     97: static void
1.11      deraadt    98: intTERM(int signo)
1.1       deraadt    99: {
                    100:        if (boolean(value(SCRIPT)) && fscript != NULL)
                    101:                fclose(fscript);
1.11      deraadt   102:        if (signo && tipin_pid)
                    103:                kill(tipin_pid, signo);
1.1       deraadt   104:        exit(0);
                    105: }
                    106:
1.15      deraadt   107: /*ARGSUSED*/
1.14      moritz    108: static void
1.13      deraadt   109: intSYS(int signo)
1.1       deraadt   110: {
1.4       millert   111:        setboolean(value(BEAUTIFY), !boolean(value(BEAUTIFY)));
1.1       deraadt   112:        longjmp(sigbuf, 1);
                    113: }
                    114:
                    115: /*
                    116:  * ****TIPOUT   TIPOUT****
                    117:  */
1.6       deraadt   118: void
1.13      deraadt   119: tipout(void)
1.1       deraadt   120: {
                    121:        char buf[BUFSIZ];
1.8       millert   122:        char *cp;
1.17      deraadt   123:        ssize_t scnt;
1.16      deraadt   124:        size_t cnt;
1.7       millert   125:        sigset_t mask, omask;
1.1       deraadt   126:
                    127:        signal(SIGINT, SIG_IGN);
                    128:        signal(SIGQUIT, SIG_IGN);
                    129:        signal(SIGEMT, intEMT);         /* attention from TIPIN */
                    130:        signal(SIGTERM, intTERM);       /* time to go signal */
                    131:        signal(SIGIOT, intIOT);         /* scripting going on signal */
                    132:        signal(SIGHUP, intTERM);        /* for dial-ups */
                    133:        signal(SIGSYS, intSYS);         /* beautify toggle */
                    134:        (void) setjmp(sigbuf);
1.7       millert   135:        sigprocmask(SIG_BLOCK, NULL, &omask);
                    136:        for (;;) {
                    137:                sigprocmask(SIG_SETMASK, &omask, NULL);
1.17      deraadt   138:                scnt = read(FD, buf, BUFSIZ);
                    139:                if (scnt <= 0) {
1.1       deraadt   140:                        /* lost carrier */
1.18      jason     141:                        if (scnt == 0 || (scnt < 0 && errno == EIO)) {
1.7       millert   142:                                sigemptyset(&mask);
                    143:                                sigaddset(&mask, SIGTERM);
                    144:                                sigprocmask(SIG_BLOCK, &mask, NULL);
1.11      deraadt   145:                                intTERM(0);
1.1       deraadt   146:                                /*NOTREACHED*/
                    147:                        }
                    148:                        continue;
                    149:                }
1.17      deraadt   150:                cnt = scnt;
1.7       millert   151:                sigemptyset(&mask);
                    152:                sigaddset(&mask, SIGEMT);
                    153:                sigaddset(&mask, SIGTERM);
                    154:                sigaddset(&mask, SIGIOT);
                    155:                sigaddset(&mask, SIGSYS);
                    156:                sigprocmask(SIG_BLOCK, &mask, NULL);
1.1       deraadt   157:                for (cp = buf; cp < buf + cnt; cp++)
1.2       deraadt   158:                        *cp &= STRIP_PAR;
1.12      deraadt   159:                write(STDOUT_FILENO, buf, cnt);
1.1       deraadt   160:                if (boolean(value(SCRIPT)) && fscript != NULL) {
                    161:                        if (!boolean(value(BEAUTIFY))) {
                    162:                                fwrite(buf, 1, cnt, fscript);
                    163:                                continue;
                    164:                        }
                    165:                        for (cp = buf; cp < buf + cnt; cp++)
                    166:                                if ((*cp >= ' ' && *cp <= '~') ||
                    167:                                    any(*cp, value(EXCEPTIONS)))
                    168:                                        putc(*cp, fscript);
                    169:                }
                    170:        }
                    171: }