first working version.
[unix-history] / usr / src / usr.bin / tip / tipout.c
CommitLineData
05862919 1#ifndef lint
e2326c44 2static char sccsid[] = "@(#)tipout.c 4.9 (Berkeley) %G%";
05862919
SL
3#endif
4
eed8c72e
BJ
5#include "tip.h"
6/*
7 * tip
8 *
9 * lower fork of tip -- handles passive side
10 * reading from the remote host
11 */
12
05862919 13static jmp_buf sigbuf;
6b46907f 14
eed8c72e
BJ
15/*
16 * TIPOUT wait state routine --
17 * sent by TIPIN when it wants to posses the remote host
18 */
19intIOT()
20{
05862919 21
eed8c72e
BJ
22 write(repdes[1],&ccc,1);
23 read(fildes[0], &ccc,1);
05862919 24 longjmp(sigbuf, 1);
eed8c72e
BJ
25}
26
27/*
28 * Scripting command interpreter --
29 * accepts script file name over the pipe and acts accordingly
30 */
31intEMT()
32{
33 char c, line[256];
34 register char *pline = line;
35 char reply;
36
eed8c72e 37 read(fildes[0], &c, 1);
3f48242d 38 while (c != '\n') {
eed8c72e
BJ
39 *pline++ = c;
40 read(fildes[0], &c, 1);
41 }
42 *pline = '\0';
43 if (boolean(value(SCRIPT)) && fscript != NULL)
44 fclose(fscript);
45 if (pline == line) {
46 boolean(value(SCRIPT)) = FALSE;
47 reply = 'y';
48 } else {
49 if ((fscript = fopen(line, "a")) == NULL)
50 reply = 'n';
51 else {
52 reply = 'y';
53 boolean(value(SCRIPT)) = TRUE;
54 }
55 }
56 write(repdes[1], &reply, 1);
05862919 57 longjmp(sigbuf, 1);
eed8c72e
BJ
58}
59
60intTERM()
61{
05862919 62
eed8c72e
BJ
63 if (boolean(value(SCRIPT)) && fscript != NULL)
64 fclose(fscript);
65 exit(0);
66}
67
68intSYS()
69{
05862919 70
eed8c72e 71 boolean(value(BEAUTIFY)) = !boolean(value(BEAUTIFY));
05862919 72 longjmp(sigbuf, 1);
eed8c72e
BJ
73}
74
75/*
76 * ****TIPOUT TIPOUT****
77 */
78tipout()
79{
c731dacc
BS
80 char buf[BUFSIZ];
81 register char *cp;
82 register int cnt;
e2326c44 83 extern int errno;
05862919 84 int omask;
c731dacc 85
eed8c72e
BJ
86 signal(SIGINT, SIG_IGN);
87 signal(SIGQUIT, SIG_IGN);
88 signal(SIGEMT, intEMT); /* attention from TIPIN */
89 signal(SIGTERM, intTERM); /* time to go signal */
90 signal(SIGIOT, intIOT); /* scripting going on signal */
91 signal(SIGHUP, intTERM); /* for dial-ups */
92 signal(SIGSYS, intSYS); /* beautify toggle */
05862919
SL
93 (void) setjmp(sigbuf);
94 for (omask = 0;; sigsetmask(omask)) {
95 cnt = read(FD, buf, BUFSIZ);
e2326c44
SL
96 if (cnt <= 0) {
97 /* lost carrier */
98 if (cnt < 0 && errno == EIO) {
05862919 99#define mask(s) (1 << ((s) - 1))
e2326c44
SL
100 sigblock(mask(SIGTERM));
101 intTERM();
102 /*NOTREACHED*/
103 }
104 continue;
105 }
05862919
SL
106#define ALLSIGS mask(SIGEMT)|mask(SIGTERM)|mask(SIGIOT)|mask(SIGSYS)
107 omask = sigblock(ALLSIGS);
c731dacc
BS
108 for (cp = buf; cp < buf + cnt; cp++)
109 *cp &= 0177;
110 write(1, buf, cnt);
eed8c72e 111 if (boolean(value(SCRIPT)) && fscript != NULL) {
c731dacc 112 if (!boolean(value(BEAUTIFY))) {
3463e9c6 113 fwrite(buf, 1, cnt, fscript);
c731dacc
BS
114 continue;
115 }
05862919
SL
116 for (cp = buf; cp < buf + cnt; cp++)
117 if ((*cp >= ' ' && *cp <= '~') ||
118 any(*cp, value(EXCEPTIONS)))
119 putc(*cp, fscript);
eed8c72e
BJ
120 }
121 }
122}