1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include "tip.h"
#include <limits.h>
/*
* tip
*
* lower fork of tip -- handles passive side
* reading from the remote host
*/
FILE *fscript;
static sigjmp_buf sigbuf;
/*
* TIPOUT wait state routine --
* sent by TIPIN when it wants to posses the remote host
*/
void
intIOT(void)
{
(void) write(repdes[1], &ccc, 1);
(void) read(fildes[0], &ccc, 1);
siglongjmp(sigbuf, 1);
}
/*
* Scripting command interpreter --
* accepts script file name over the pipe and acts accordingly
*/
void
intEMT(void)
{
char c, line[PATH_MAX];
char *pline = line;
char reply;
(void) read(fildes[0], &c, 1);
while (c != '\n' && line + sizeof (line) - pline > 1) {
*pline++ = c;
(void) read(fildes[0], &c, 1);
}
*pline = '\0';
if (boolean(value(SCRIPT)) && fscript != NULL)
(void) fclose(fscript);
if (pline == line) {
boolean(value(SCRIPT)) = FALSE;
reply = 'y';
} else {
if ((fscript = fopen(line, "a")) == NULL)
reply = 'n';
else {
reply = 'y';
boolean(value(SCRIPT)) = TRUE;
}
}
(void) write(repdes[1], &reply, 1);
siglongjmp(sigbuf, 1);
}
void
intTERM(void)
{
if (boolean(value(SCRIPT)) && fscript != NULL)
(void) fclose(fscript);
exit(0);
}
void
intSYS(void)
{
boolean(value(BEAUTIFY)) = !boolean(value(BEAUTIFY));
siglongjmp(sigbuf, 1);
}
/*
* ****TIPOUT TIPOUT****
*/
void
tipout(void)
{
char buf[BUFSIZ];
char *cp;
int cnt;
sigset_t omask, bmask, tmask;
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
/* attention from TIPIN */
(void) signal(SIGEMT, (sig_handler_t)intEMT);
/* time to go signal */
(void) signal(SIGTERM, (sig_handler_t)intTERM);
/* scripting going on signal */
(void) signal(SIGIOT, (sig_handler_t)intIOT);
/* for dial-ups */
(void) signal(SIGHUP, (sig_handler_t)intTERM);
/* beautify toggle */
(void) signal(SIGSYS, (sig_handler_t)intSYS);
(void) sigsetjmp(sigbuf, 1);
(void) sigemptyset(&omask);
(void) sigemptyset(&bmask);
(void) sigaddset(&bmask, SIGEMT);
(void) sigaddset(&bmask, SIGTERM);
(void) sigaddset(&bmask, SIGIOT);
(void) sigaddset(&bmask, SIGSYS);
(void) sigemptyset(&tmask);
(void) sigaddset(&tmask, SIGTERM);
for (;;) {
cnt = read(FD, buf, BUFSIZ);
if (cnt <= 0) {
/*
* If dialback is specified, ignore the hangup
* and clear the hangup condition on the device.
*/
if (cnt == 0 && DB) {
int fd;
DB = 0;
if ((fd = open(DV, O_RDWR)) >= 0) {
if (fd != FD)
(void) close(fd);
}
continue;
}
/* lost carrier */
if ((cnt < 0 && errno == EIO) ||
(cnt == 0)) {
(void) sigprocmask(SIG_BLOCK, &tmask, NULL);
intTERM();
/*NOTREACHED*/
}
} else {
(void) sigprocmask(SIG_BLOCK, &bmask, &omask);
if (!noparity)
for (cp = buf; cp < buf + cnt; cp++)
*cp &= 0177;
(void) write(1, buf, cnt);
if (boolean(value(SCRIPT)) && fscript != NULL) {
if (!boolean(value(BEAUTIFY))) {
(void) fwrite(buf, 1, cnt, fscript);
} else {
for (cp = buf; cp < buf + cnt; cp++)
if ((*cp >= ' ' && *cp <= '~')||
any(*cp, value(EXCEPTIONS)))
(void) putc(*cp,
fscript);
}
}
}
(void) sigprocmask(SIG_SETMASK, &omask, NULL);
}
}
|