showterm 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,312 +0,0 @@
1
- /*
2
- * Copyright (c) 2000 Satoru Takabayashi <satoru@namazu.org>
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions
7
- * are met:
8
- * 1. Redistributions of source code must retain the above copyright
9
- * notice, this list of conditions and the following disclaimer.
10
- * 2. Redistributions in binary form must reproduce the above copyright
11
- * notice, this list of conditions and the following disclaimer in the
12
- * documentation and/or other materials provided with the distribution.
13
- * 3. All advertising materials mentioning features or use of this software
14
- * must display the following acknowledgement:
15
- * This product includes software developed by the University of
16
- * California, Berkeley and its contributors.
17
- * 4. Neither the name of the University nor the names of its contributors
18
- * may be used to endorse or promote products derived from this software
19
- * without specific prior written permission.
20
- *
21
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
- * SUCH DAMAGE.
32
- */
33
-
34
- #include <stdio.h>
35
- #include <stdlib.h>
36
- #include <assert.h>
37
- #include <unistd.h>
38
- #include <termios.h>
39
- #include <sys/time.h>
40
- #include <string.h>
41
-
42
- #include "ttyrec.h"
43
- #include "io.h"
44
-
45
- typedef double (*WaitFunc) (struct timeval prev,
46
- struct timeval cur,
47
- double speed);
48
- typedef int (*ReadFunc) (FILE *fp, Header *h, char **buf);
49
- typedef void (*WriteFunc) (char *buf, int len);
50
- typedef void (*ProcessFunc) (FILE *fp, double speed,
51
- ReadFunc read_func, WaitFunc wait_func);
52
-
53
- struct timeval
54
- timeval_diff (struct timeval tv1, struct timeval tv2)
55
- {
56
- struct timeval diff;
57
-
58
- diff.tv_sec = tv2.tv_sec - tv1.tv_sec;
59
- diff.tv_usec = tv2.tv_usec - tv1.tv_usec;
60
- if (diff.tv_usec < 0) {
61
- diff.tv_sec--;
62
- diff.tv_usec += 1000000;
63
- }
64
-
65
- return diff;
66
- }
67
-
68
- struct timeval
69
- timeval_div (struct timeval tv1, double n)
70
- {
71
- double x = ((double)tv1.tv_sec + (double)tv1.tv_usec / 1000000.0) / n;
72
- struct timeval div;
73
-
74
- div.tv_sec = (int)x;
75
- div.tv_usec = (x - (int)x) * 1000000;
76
-
77
- return div;
78
- }
79
-
80
- double
81
- ttywait (struct timeval prev, struct timeval cur, double speed)
82
- {
83
- static struct timeval drift = {0, 0};
84
- struct timeval start;
85
- struct timeval diff = timeval_diff(prev, cur);
86
- fd_set readfs;
87
-
88
- gettimeofday(&start, NULL);
89
-
90
- assert(speed != 0);
91
- diff = timeval_diff(drift, timeval_div(diff, speed));
92
- if (diff.tv_sec < 0) {
93
- diff.tv_sec = diff.tv_usec = 0;
94
- }
95
-
96
- FD_SET(STDIN_FILENO, &readfs);
97
- /*
98
- * We use select() for sleeping with subsecond precision.
99
- * select() is also used to wait user's input from a keyboard.
100
- *
101
- * Save "diff" since select(2) may overwrite it to {0, 0}.
102
- */
103
- struct timeval orig_diff = diff;
104
- select(1, &readfs, NULL, NULL, &diff);
105
- diff = orig_diff; /* Restore the original diff value. */
106
- if (FD_ISSET(0, &readfs)) { /* a user hits a character? */
107
- char c;
108
- read(STDIN_FILENO, &c, 1); /* drain the character */
109
- switch (c) {
110
- case '+':
111
- case 'f':
112
- speed *= 2;
113
- break;
114
- case '-':
115
- case 's':
116
- speed /= 2;
117
- break;
118
- case '1':
119
- speed = 1.0;
120
- break;
121
- }
122
- drift.tv_sec = drift.tv_usec = 0;
123
- } else {
124
- struct timeval stop;
125
- gettimeofday(&stop, NULL);
126
- /* Hack to accumulate the drift */
127
- if (diff.tv_sec == 0 && diff.tv_usec == 0) {
128
- diff = timeval_diff(drift, diff); // diff = 0 - drift.
129
- }
130
- drift = timeval_diff(diff, timeval_diff(start, stop));
131
- }
132
- return speed;
133
- }
134
-
135
- double
136
- ttynowait (struct timeval prev, struct timeval cur, double speed)
137
- {
138
- /* do nothing */
139
- return 0; /* Speed isn't important. */
140
- }
141
-
142
- int
143
- ttyread (FILE *fp, Header *h, char **buf)
144
- {
145
- if (read_header(fp, h) == 0) {
146
- return 0;
147
- }
148
-
149
- *buf = malloc(h->len);
150
- if (*buf == NULL) {
151
- perror("malloc");
152
- }
153
-
154
- if (fread(*buf, 1, h->len, fp) == 0) {
155
- perror("fread");
156
- }
157
- return 1;
158
- }
159
-
160
- int
161
- ttypread (FILE *fp, Header *h, char **buf)
162
- {
163
- /*
164
- * Read persistently just like tail -f.
165
- */
166
- while (ttyread(fp, h, buf) == 0) {
167
- struct timeval w = {0, 250000};
168
- select(0, NULL, NULL, NULL, &w);
169
- clearerr(fp);
170
- }
171
- return 1;
172
- }
173
-
174
- void
175
- ttywrite (char *buf, int len)
176
- {
177
- fwrite(buf, 1, len, stdout);
178
- }
179
-
180
- void
181
- ttynowrite (char *buf, int len)
182
- {
183
- /* do nothing */
184
- }
185
-
186
- void
187
- ttyplay (FILE *fp, double speed, ReadFunc read_func,
188
- WriteFunc write_func, WaitFunc wait_func)
189
- {
190
- int first_time = 1;
191
- struct timeval prev;
192
-
193
- setbuf(stdout, NULL);
194
- setbuf(fp, NULL);
195
-
196
- while (1) {
197
- char *buf;
198
- Header h;
199
-
200
- if (read_func(fp, &h, &buf) == 0) {
201
- break;
202
- }
203
-
204
- if (!first_time) {
205
- speed = wait_func(prev, h.tv, speed);
206
- }
207
- first_time = 0;
208
-
209
- write_func(buf, h.len);
210
- prev = h.tv;
211
- free(buf);
212
- }
213
- }
214
-
215
- void
216
- ttyskipall (FILE *fp)
217
- {
218
- /*
219
- * Skip all records.
220
- */
221
- ttyplay(fp, 0, ttyread, ttynowrite, ttynowait);
222
- }
223
-
224
- void ttyplayback (FILE *fp, double speed,
225
- ReadFunc read_func, WaitFunc wait_func)
226
- {
227
- ttyplay(fp, speed, ttyread, ttywrite, wait_func);
228
- }
229
-
230
- void ttypeek (FILE *fp, double speed,
231
- ReadFunc read_func, WaitFunc wait_func)
232
- {
233
- ttyskipall(fp);
234
- ttyplay(fp, speed, ttypread, ttywrite, ttynowait);
235
- }
236
-
237
-
238
- void
239
- usage (void)
240
- {
241
- printf("Usage: ttyplay [OPTION] [FILE]\n");
242
- printf(" -s SPEED Set speed to SPEED [1.0]\n");
243
- printf(" -n No wait mode\n");
244
- printf(" -p Peek another person's ttyrecord\n");
245
- exit(EXIT_FAILURE);
246
- }
247
-
248
- /*
249
- * We do some tricks so that select(2) properly works on
250
- * STDIN_FILENO in ttywait().
251
- */
252
- FILE *
253
- input_from_stdin (void)
254
- {
255
- FILE *fp;
256
- int fd = edup(STDIN_FILENO);
257
- edup2(STDOUT_FILENO, STDIN_FILENO);
258
- return efdopen(fd, "r");
259
- }
260
-
261
- int
262
- main (int argc, char **argv)
263
- {
264
- double speed = 1.0;
265
- ReadFunc read_func = ttyread;
266
- WaitFunc wait_func = ttywait;
267
- ProcessFunc process = ttyplayback;
268
- FILE *input = NULL;
269
- struct termios old, new;
270
-
271
- set_progname(argv[0]);
272
- while (1) {
273
- int ch = getopt(argc, argv, "s:np");
274
- if (ch == EOF) {
275
- break;
276
- }
277
- switch (ch) {
278
- case 's':
279
- if (optarg == NULL) {
280
- perror("-s option requires an argument");
281
- exit(EXIT_FAILURE);
282
- }
283
- sscanf(optarg, "%lf", &speed);
284
- break;
285
- case 'n':
286
- wait_func = ttynowait;
287
- break;
288
- case 'p':
289
- process = ttypeek;
290
- break;
291
- default:
292
- usage();
293
- }
294
- }
295
-
296
- if (optind < argc) {
297
- input = efopen(argv[optind], "r");
298
- } else {
299
- input = input_from_stdin();
300
- }
301
- assert(input != NULL);
302
-
303
- tcgetattr(0, &old); /* Get current terminal state */
304
- new = old; /* Make a copy */
305
- new.c_lflag &= ~(ICANON | ECHO | ECHONL); /* unbuffered, no echo */
306
- tcsetattr(0, TCSANOW, &new); /* Make it current */
307
-
308
- process(input, speed, read_func, wait_func);
309
- tcsetattr(0, TCSANOW, &old); /* Return terminal state */
310
-
311
- return 0;
312
- }
@@ -1,76 +0,0 @@
1
- .\"
2
- .\" This manual page is written by NAKANO Takeo <nakano@webmasters.gr.jp>
3
- .\"
4
- .TH TTYREC 1
5
- .SH NAME
6
- ttyrec \- a tty recorder
7
- .SH SYNOPSIS
8
- .br
9
- .B ttyrec
10
- .I "[\-a][\-u] [file]"
11
- .br
12
- .SH DESCRIPTION
13
- .B Ttyrec
14
- is a tty recorder.
15
- It is a derivative of
16
- .BR script (1)
17
- command for recording timing information with microsecond accuracy as well.
18
- It can record emacs -nw, vi, lynx, or any programs running on tty.
19
- .PP
20
- .B Ttyrec
21
- invokes a shell and records the session until the shell exits.
22
- Recorded data can be played back with
23
- .BR ttyplay (1).
24
- If the argument
25
- .I file
26
- is given, the session will be recorded in that file.
27
- Otherwise,
28
- .I ttyrecord
29
- is used as default.
30
- .SH OPTIONS
31
- .TP
32
- .B \-a
33
- Append the output to
34
- .I file
35
- or
36
- .IR ttyrecord ,
37
- rather than overwriting it.
38
- .TP
39
- .B \-u
40
- With this option,
41
- .B ttyrec
42
- automatically calls
43
- .BR uudecode (1)
44
- and saves its output when uuencoded data appear on the session.
45
- It allow you to transfer files from remote host.
46
- You can call
47
- .B ttyrec
48
- with this option, login to the remote host
49
- and invoke
50
- .BR uuencode (1)
51
- on it for the file you want to transfer.
52
- .TP
53
- .BI \-e " command"
54
- Invoke
55
- .I command
56
- when ttyrec starts.
57
-
58
-
59
- .SH ENVIRONMENT
60
- .TP
61
- .I SHELL
62
- If the variable
63
- .I SHELL
64
- exists, the shell forked by
65
- .B ttyrec
66
- will be that shell.
67
- If it's not set,
68
- the Bourne shell is assumed.
69
- (Most shells set this variable automatically).
70
- .SH "SEE ALSO"
71
- .BR script (1),
72
- .BR ttyplay (1),
73
- .BR ttytime (1),
74
- .BR uuencode (1),
75
- .BR uudecode (1)
76
-
@@ -1,483 +0,0 @@
1
- /*
2
- * Copyright (c) 1980 Regents of the University of California.
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions
7
- * are met:
8
- * 1. Redistributions of source code must retain the above copyright
9
- * notice, this list of conditions and the following disclaimer.
10
- * 2. Redistributions in binary form must reproduce the above copyright
11
- * notice, this list of conditions and the following disclaimer in the
12
- * documentation and/or other materials provided with the distribution.
13
- * 3. All advertising materials mentioning features or use of this software
14
- * must display the following acknowledgement:
15
- * This product includes software developed by the University of
16
- * California, Berkeley and its contributors.
17
- * 4. Neither the name of the University nor the names of its contributors
18
- * may be used to endorse or promote products derived from this software
19
- * without specific prior written permission.
20
- *
21
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
- * SUCH DAMAGE.
32
- */
33
-
34
- /* 1999-02-22 Arkadiusz Mi�kiewicz <misiek@misiek.eu.org>
35
- * - added Native Language Support
36
- */
37
-
38
- /* 2000-12-27 Satoru Takabayashi <satoru@namazu.org>
39
- * - modify `script' to create `ttyrec'.
40
- */
41
-
42
- /*
43
- * script
44
- */
45
- #include <sys/types.h>
46
- #include <sys/stat.h>
47
- #include <termios.h>
48
- #include <sys/ioctl.h>
49
- #include <sys/time.h>
50
- #include <sys/file.h>
51
- #include <sys/signal.h>
52
- #include <stdio.h>
53
- #include <time.h>
54
- #include <unistd.h>
55
- #include <string.h>
56
- #include <stdlib.h>
57
-
58
- #if defined(SVR4)
59
- #include <fcntl.h>
60
- #include <stropts.h>
61
- #endif /* SVR4 */
62
-
63
- #include <sys/time.h>
64
- #include "ttyrec.h"
65
- #include "io.h"
66
-
67
- #define HAVE_inet_aton
68
- #define HAVE_scsi_h
69
- #define HAVE_kd_h
70
-
71
- #define _(FOO) FOO
72
-
73
- #ifdef HAVE_openpty
74
- #include <libutil.h>
75
- #endif
76
-
77
- #if defined(SVR4) && !defined(CDEL)
78
- #if defined(_POSIX_VDISABLE)
79
- #define CDEL _POSIX_VDISABLE
80
- #elif defined(CDISABLE)
81
- #define CDEL CDISABLE
82
- #else /* not _POSIX_VISIBLE && not CDISABLE */
83
- #define CDEL 255
84
- #endif /* not _POSIX_VISIBLE && not CDISABLE */
85
- #endif /* SVR4 && ! CDEL */
86
-
87
- void done(void);
88
- void fail(void);
89
- void fixtty(void);
90
- void getmaster(void);
91
- void getslave(void);
92
- void doinput(void);
93
- void dooutput(void);
94
- void doshell(const char*);
95
-
96
- char *shell;
97
- FILE *fscript;
98
- int master;
99
- int slave;
100
- int child;
101
- int subchild;
102
- char *fname;
103
-
104
- struct termios tt;
105
- struct winsize win;
106
- int lb;
107
- int l;
108
- #if !defined(SVR4)
109
- #ifndef HAVE_openpty
110
- char line[] = "/dev/ptyXX";
111
- #endif
112
- #endif /* !SVR4 */
113
- int aflg;
114
- int uflg;
115
-
116
- int
117
- main(argc, argv)
118
- int argc;
119
- char *argv[];
120
- {
121
- extern int optind;
122
- int ch;
123
- void finish();
124
- char *getenv();
125
- char *command = NULL;
126
-
127
- while ((ch = getopt(argc, argv, "aue:h?")) != EOF)
128
- switch((char)ch) {
129
- case 'a':
130
- aflg++;
131
- break;
132
- case 'u':
133
- uflg++;
134
- break;
135
- case 'e':
136
- command = strdup(optarg);
137
- break;
138
- case 'h':
139
- case '?':
140
- default:
141
- fprintf(stderr, _("usage: ttyrec [-u] [-e command] [-a] [file]\n"));
142
- exit(1);
143
- }
144
- argc -= optind;
145
- argv += optind;
146
-
147
- if (argc > 0)
148
- fname = argv[0];
149
- else
150
- fname = "ttyrecord";
151
- if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
152
- perror(fname);
153
- fail();
154
- }
155
- setbuf(fscript, NULL);
156
-
157
- shell = getenv("SHELL");
158
- if (shell == NULL)
159
- shell = "/bin/sh";
160
-
161
- getmaster();
162
- fixtty();
163
-
164
- (void) signal(SIGCHLD, finish);
165
- child = fork();
166
- if (child < 0) {
167
- perror("fork");
168
- fail();
169
- }
170
- if (child == 0) {
171
- subchild = child = fork();
172
- if (child < 0) {
173
- perror("fork");
174
- fail();
175
- }
176
- if (child)
177
- dooutput();
178
- else
179
- doshell(command);
180
- }
181
- doinput();
182
-
183
- return 0;
184
- }
185
-
186
- void
187
- doinput()
188
- {
189
- register int cc;
190
- char ibuf[BUFSIZ];
191
-
192
- (void) fclose(fscript);
193
- #ifdef HAVE_openpty
194
- (void) close(slave);
195
- #endif
196
- while ((cc = read(0, ibuf, BUFSIZ)) > 0)
197
- (void) write(master, ibuf, cc);
198
- done();
199
- }
200
-
201
- #include <sys/wait.h>
202
-
203
- void
204
- finish()
205
- {
206
- #if defined(SVR4)
207
- int status;
208
- #else /* !SVR4 */
209
- union wait status;
210
- #endif /* !SVR4 */
211
- register int pid;
212
- register int die = 0;
213
-
214
- while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0)
215
- if (pid == child)
216
- die = 1;
217
-
218
- if (die)
219
- done();
220
- }
221
-
222
- struct linebuf {
223
- char str[BUFSIZ + 1]; /* + 1 for an additional NULL character.*/
224
- int len;
225
- };
226
-
227
-
228
- void
229
- check_line (const char *line)
230
- {
231
- static int uuencode_mode = 0;
232
- static FILE *uudecode;
233
-
234
- if (uuencode_mode == 1) {
235
- fprintf(uudecode, "%s", line);
236
- if (strcmp(line, "end\n") == 0) {
237
- pclose(uudecode);
238
- uuencode_mode = 0;
239
- }
240
- } else {
241
- int dummy; char dummy2[BUFSIZ];
242
- if (sscanf(line, "begin %o %s", &dummy, dummy2) == 2) {
243
- /*
244
- * uuencode line found!
245
- */
246
- uudecode = popen("uudecode", "w");
247
- fprintf(uudecode, "%s", line);
248
- uuencode_mode = 1;
249
- }
250
- }
251
- }
252
-
253
- void
254
- check_output(const char *str, int len)
255
- {
256
- static struct linebuf lbuf = {"", 0};
257
- int i;
258
-
259
- for (i = 0; i < len; i++) {
260
- if (lbuf.len < BUFSIZ) {
261
- lbuf.str[lbuf.len] = str[i];
262
- if (lbuf.str[lbuf.len] == '\r') {
263
- lbuf.str[lbuf.len] = '\n';
264
- }
265
- lbuf.len++;
266
- if (lbuf.str[lbuf.len - 1] == '\n') {
267
- if (lbuf.len > 1) { /* skip a blank line. */
268
- lbuf.str[lbuf.len] = '\0';
269
- check_line(lbuf.str);
270
- }
271
- lbuf.len = 0;
272
- }
273
- } else {/* buffer overflow */
274
- lbuf.len = 0;
275
- }
276
- }
277
- }
278
-
279
- void
280
- dooutput()
281
- {
282
- int cc;
283
- char obuf[BUFSIZ];
284
-
285
- setbuf(stdout, NULL);
286
- (void) close(0);
287
- #ifdef HAVE_openpty
288
- (void) close(slave);
289
- #endif
290
- for (;;) {
291
- Header h;
292
-
293
- cc = read(master, obuf, BUFSIZ);
294
- if (cc <= 0)
295
- break;
296
- if (uflg)
297
- check_output(obuf, cc);
298
- h.len = cc;
299
- gettimeofday(&h.tv, NULL);
300
- (void) write(1, obuf, cc);
301
- (void) write_header(fscript, &h);
302
- (void) fwrite(obuf, 1, cc, fscript);
303
- }
304
- done();
305
- }
306
-
307
- void
308
- doshell(const char* command)
309
- {
310
- /***
311
- int t;
312
-
313
- t = open(_PATH_TTY, O_RDWR);
314
- if (t >= 0) {
315
- (void) ioctl(t, TIOCNOTTY, (char *)0);
316
- (void) close(t);
317
- }
318
- ***/
319
- getslave();
320
- (void) close(master);
321
- (void) fclose(fscript);
322
- (void) dup2(slave, 0);
323
- (void) dup2(slave, 1);
324
- (void) dup2(slave, 2);
325
- (void) close(slave);
326
-
327
- if (!command) {
328
- execl(shell, strrchr(shell, '/') + 1, "-i", 0);
329
- } else {
330
- execl(shell, strrchr(shell, '/') + 1, "-c", command, 0);
331
- }
332
- perror(shell);
333
- fail();
334
- }
335
-
336
- void
337
- fixtty()
338
- {
339
- struct termios rtt;
340
-
341
- rtt = tt;
342
- #if defined(SVR4)
343
- rtt.c_iflag = 0;
344
- rtt.c_lflag &= ~(ISIG|ICANON|XCASE|ECHO|ECHOE|ECHOK|ECHONL);
345
- rtt.c_oflag = OPOST;
346
- rtt.c_cc[VINTR] = CDEL;
347
- rtt.c_cc[VQUIT] = CDEL;
348
- rtt.c_cc[VERASE] = CDEL;
349
- rtt.c_cc[VKILL] = CDEL;
350
- rtt.c_cc[VEOF] = 1;
351
- rtt.c_cc[VEOL] = 0;
352
- #else /* !SVR4 */
353
- cfmakeraw(&rtt);
354
- rtt.c_lflag &= ~ECHO;
355
- #endif /* !SVR4 */
356
- (void) tcsetattr(0, TCSAFLUSH, &rtt);
357
- }
358
-
359
- void
360
- fail()
361
- {
362
-
363
- (void) kill(0, SIGTERM);
364
- done();
365
- }
366
-
367
- void
368
- done()
369
- {
370
- if (subchild) {
371
- (void) fclose(fscript);
372
- (void) close(master);
373
- } else {
374
- (void) tcsetattr(0, TCSAFLUSH, &tt);
375
- }
376
- exit(0);
377
- }
378
-
379
- void
380
- getmaster()
381
- {
382
- #if defined(SVR4)
383
- (void) tcgetattr(0, &tt);
384
- (void) ioctl(0, TIOCGWINSZ, (char *)&win);
385
- if ((master = open("/dev/ptmx", O_RDWR)) < 0) {
386
- perror("open(\"/dev/ptmx\", O_RDWR)");
387
- fail();
388
- }
389
- #else /* !SVR4 */
390
- #ifdef HAVE_openpty
391
- (void) tcgetattr(0, &tt);
392
- (void) ioctl(0, TIOCGWINSZ, (char *)&win);
393
- if (openpty(&master, &slave, NULL, &tt, &win) < 0) {
394
- fprintf(stderr, _("openpty failed\n"));
395
- fail();
396
- }
397
- #else
398
- #ifdef HAVE_getpt
399
- if ((master = getpt()) < 0) {
400
- perror("getpt()");
401
- fail();
402
- }
403
- #else
404
- char *pty, *bank, *cp;
405
- struct stat stb;
406
-
407
- pty = &line[strlen("/dev/ptyp")];
408
- for (bank = "pqrs"; *bank; bank++) {
409
- line[strlen("/dev/pty")] = *bank;
410
- *pty = '0';
411
- if (stat(line, &stb) < 0)
412
- break;
413
- for (cp = "0123456789abcdef"; *cp; cp++) {
414
- *pty = *cp;
415
- master = open(line, O_RDWR);
416
- if (master >= 0) {
417
- char *tp = &line[strlen("/dev/")];
418
- int ok;
419
-
420
- /* verify slave side is usable */
421
- *tp = 't';
422
- ok = access(line, R_OK|W_OK) == 0;
423
- *tp = 'p';
424
- if (ok) {
425
- (void) tcgetattr(0, &tt);
426
- (void) ioctl(0, TIOCGWINSZ,
427
- (char *)&win);
428
- return;
429
- }
430
- (void) close(master);
431
- }
432
- }
433
- }
434
- fprintf(stderr, _("Out of pty's\n"));
435
- fail();
436
- #endif /* not HAVE_getpt */
437
- #endif /* not HAVE_openpty */
438
- #endif /* !SVR4 */
439
- }
440
-
441
- void
442
- getslave()
443
- {
444
- #if defined(SVR4)
445
- (void) setsid();
446
- grantpt( master);
447
- unlockpt(master);
448
- if ((slave = open((const char *)ptsname(master), O_RDWR)) < 0) {
449
- perror("open(fd, O_RDWR)");
450
- fail();
451
- }
452
- if (isastream(slave)) {
453
- if (ioctl(slave, I_PUSH, "ptem") < 0) {
454
- perror("ioctl(fd, I_PUSH, ptem)");
455
- fail();
456
- }
457
- if (ioctl(slave, I_PUSH, "ldterm") < 0) {
458
- perror("ioctl(fd, I_PUSH, ldterm)");
459
- fail();
460
- }
461
- #ifndef _HPUX_SOURCE
462
- if (ioctl(slave, I_PUSH, "ttcompat") < 0) {
463
- perror("ioctl(fd, I_PUSH, ttcompat)");
464
- fail();
465
- }
466
- #endif
467
- (void) ioctl(0, TIOCGWINSZ, (char *)&win);
468
- }
469
- #else /* !SVR4 */
470
- #ifndef HAVE_openpty
471
- line[strlen("/dev/")] = 't';
472
- slave = open(line, O_RDWR);
473
- if (slave < 0) {
474
- perror(line);
475
- fail();
476
- }
477
- (void) tcsetattr(slave, TCSAFLUSH, &tt);
478
- (void) ioctl(slave, TIOCSWINSZ, (char *)&win);
479
- #endif
480
- (void) setsid();
481
- (void) ioctl(slave, TIOCSCTTY, 0);
482
- #endif /* SVR4 */
483
- }