-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptty.c
156 lines (132 loc) · 2.67 KB
/
ptty.c
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
#include <sys/wait.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <poll.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#if defined(__linux)
#include <pty.h>
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#include <util.h>
#elif defined(__FreeBSD__) || defined(__DragonFly__)
#include <libutil.h>
#endif
void
die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
exit(EXIT_FAILURE);
}
void
usage(void)
{
fputs("ptty [-C] [-c cols] [-r rows] cmd\n", stderr);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
struct winsize ws = {.ws_row = 25, .ws_col = 80, 0, 0};
int ch;
bool closeflag = false;
while ((ch = getopt(argc, argv, "c:r:Ch")) != -1) {
switch (ch) {
case 'c': /* cols */
ws.ws_col = strtoimax(optarg, NULL, 10);
if (errno != 0)
die("strtoimax: %s", optarg);
break;
case 'r': /* lines */
ws.ws_row = strtoimax(optarg, NULL, 10);
if (errno != 0)
die("strtoimax: %s", optarg);
break;
case 'C':
closeflag = true;
break;
case 'h':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage();
int mfd;
pid_t child = forkpty(&mfd, NULL, NULL, &ws);
switch (child) {
case -1:
die("forkpty");
case 0: /* child */
execvp(argv[0], argv);
die("exec");
}
/* parent */
if (closeflag && close(mfd) == -1)
die("close:");
int pfds = 2;
struct pollfd pfd[2] = {
{ STDIN_FILENO, POLLIN, 0},
{ mfd, POLLIN, 0}
};
for (;;) {
char buf[BUFSIZ];
ssize_t n;
int r;
if ((r = poll(pfd, pfds, -1)) == -1)
die("poll:");
if (pfd[0].revents & POLLIN) {
if ((n = read(STDIN_FILENO, buf, sizeof buf)) == -1)
die("read:");
if (n == 0) {
pfd[0].fd = -1;
if (close(mfd) == -1)
die("close:");
break;
}
if (write(mfd, buf, n) == -1)
die("write:");
}
if (pfd[1].revents & POLLIN) {
if ((n = read(mfd, buf, sizeof(buf)-1)) == -1)
die("read:");
if (n == 0) break;
buf[n] = '\0';
/* handle cursor position request */
if (strcmp("\033[6n", buf) == 0) {
dprintf(mfd, "\033[25;1R");
continue;
}
if (write(STDOUT_FILENO, buf, n) == -1)
die("write:");
}
if (pfd[0].revents & POLLHUP) {
pfd[0].fd = -1;
if (close(mfd) == -1)
die("close:");
break;
}
if (pfd[1].revents & POLLHUP)
break;
}
int status;
if (waitpid(child, &status, 0) != child)
die("waitpid:");
return WEXITSTATUS(status);
}