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