Annotation of src/usr.bin/tip/tipout.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: tipout.c,v 1.10 2003/06/03 02:56:18 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.
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.11 ! deraadt 37: static const char rcsid[] = "$OpenBSD: tipout.c,v 1.10 2003/06/03 02:56:18 millert Exp $";
1.1 deraadt 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: /*
51: * TIPOUT wait state routine --
52: * sent by TIPIN when it wants to posses the remote host
53: */
54: void
55: intIOT()
56: {
57:
58: write(repdes[1],&ccc,1);
59: read(fildes[0], &ccc,1);
60: longjmp(sigbuf, 1);
61: }
62:
63: /*
64: * Scripting command interpreter --
65: * accepts script file name over the pipe and acts accordingly
66: */
67: void
68: intEMT()
69: {
70: char c, line[256];
1.8 millert 71: char *pline = line;
1.1 deraadt 72: char reply;
73:
74: read(fildes[0], &c, 1);
1.5 millert 75: while (c != '\n' && pline - line < sizeof(line)) {
1.1 deraadt 76: *pline++ = c;
77: read(fildes[0], &c, 1);
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: }
93: write(repdes[1], &reply, 1);
94: longjmp(sigbuf, 1);
95: }
96:
97: void
1.11 ! deraadt 98: intTERM(int signo)
1.1 deraadt 99: {
100:
101: if (boolean(value(SCRIPT)) && fscript != NULL)
102: fclose(fscript);
1.11 ! deraadt 103: if (signo && tipin_pid)
! 104: kill(tipin_pid, signo);
1.1 deraadt 105: exit(0);
106: }
107:
108: void
109: intSYS()
110: {
111:
1.4 millert 112: setboolean(value(BEAUTIFY), !boolean(value(BEAUTIFY)));
1.1 deraadt 113: longjmp(sigbuf, 1);
114: }
115:
116: /*
117: * ****TIPOUT TIPOUT****
118: */
1.6 deraadt 119: void
1.1 deraadt 120: tipout()
121: {
122: char buf[BUFSIZ];
1.8 millert 123: char *cp;
124: int 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.1 deraadt 138: cnt = read(FD, buf, BUFSIZ);
139: if (cnt <= 0) {
140: /* lost carrier */
141: if (cnt < 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.7 millert 150: sigemptyset(&mask);
151: sigaddset(&mask, SIGEMT);
152: sigaddset(&mask, SIGTERM);
153: sigaddset(&mask, SIGIOT);
154: sigaddset(&mask, SIGSYS);
155: sigprocmask(SIG_BLOCK, &mask, NULL);
1.1 deraadt 156: for (cp = buf; cp < buf + cnt; cp++)
1.2 deraadt 157: *cp &= STRIP_PAR;
1.1 deraadt 158: write(1, buf, cnt);
159: if (boolean(value(SCRIPT)) && fscript != NULL) {
160: if (!boolean(value(BEAUTIFY))) {
161: fwrite(buf, 1, cnt, fscript);
162: continue;
163: }
164: for (cp = buf; cp < buf + cnt; cp++)
165: if ((*cp >= ' ' && *cp <= '~') ||
166: any(*cp, value(EXCEPTIONS)))
167: putc(*cp, fscript);
168: }
169: }
170: }