Annotation of src/usr.bin/tip/tipout.c, Revision 1.6
1.6 ! deraadt 1: /* $OpenBSD: tipout.c,v 1.5 1997/08/23 00:03:46 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.6 ! deraadt 41: static char rcsid[] = "$OpenBSD: tipout.c,v 1.5 1997/08/23 00:03:46 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];
75: register char *pline = line;
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];
125: register char *cp;
126: register int cnt;
127: extern int errno;
128: int omask;
129:
130: signal(SIGINT, SIG_IGN);
131: signal(SIGQUIT, SIG_IGN);
132: signal(SIGEMT, intEMT); /* attention from TIPIN */
133: signal(SIGTERM, intTERM); /* time to go signal */
134: signal(SIGIOT, intIOT); /* scripting going on signal */
135: signal(SIGHUP, intTERM); /* for dial-ups */
136: signal(SIGSYS, intSYS); /* beautify toggle */
137: (void) setjmp(sigbuf);
138: for (omask = 0;; sigsetmask(omask)) {
139: cnt = read(FD, buf, BUFSIZ);
140: if (cnt <= 0) {
141: /* lost carrier */
142: if (cnt < 0 && errno == EIO) {
143: sigblock(sigmask(SIGTERM));
144: intTERM();
145: /*NOTREACHED*/
146: }
147: continue;
148: }
149: #define ALLSIGS sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS)
150: omask = sigblock(ALLSIGS);
151: for (cp = buf; cp < buf + cnt; cp++)
1.2 deraadt 152: *cp &= STRIP_PAR;
1.1 deraadt 153: write(1, buf, cnt);
154: if (boolean(value(SCRIPT)) && fscript != NULL) {
155: if (!boolean(value(BEAUTIFY))) {
156: fwrite(buf, 1, cnt, fscript);
157: continue;
158: }
159: for (cp = buf; cp < buf + cnt; cp++)
160: if ((*cp >= ' ' && *cp <= '~') ||
161: any(*cp, value(EXCEPTIONS)))
162: putc(*cp, fscript);
163: }
164: }
165: }