forked from zelch/TemuTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpty.c
247 lines (221 loc) · 4.59 KB
/
pty.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pty.h>
#include <termios.h>
#include <glib.h>
#include <glib/gslist.h>
#include "pty.h"
/* This is taken from gnome-pty-helper, and ever so slightly modified. */
static struct termios*
init_term_with_defaults(struct termios* term)
{
/*
* openpty assumes POSIX termios so this should be portable.
* Don't change this to a structure init - POSIX doesn't say
* anything about field order.
*/
memset(term, 0, sizeof(struct termios));
term->c_iflag = 0
#ifdef BRKINT
| BRKINT
#endif
#ifdef ICRNL
| ICRNL
#endif
#ifdef IMAXBEL
| IMAXBEL
#endif
#if 0 /* MODIFICATION -- No flow control */
#ifdef IXON
| IXON
#endif
#ifdef IXANY
| IXANY
#endif
#endif
;
term->c_oflag = 0
#ifdef OPOST
| OPOST
#endif
#ifdef ONLCR
| ONLCR
#endif
#ifdef NL0
| NL0
#endif
#ifdef CR0
| CR0
#endif
#ifdef TAB0
| TAB0
#endif
#ifdef BS0
| BS0
#endif
#ifdef VT0
| VT0
#endif
#ifdef FF0
| FF0
#endif
;
term->c_cflag = 0
#ifdef CREAD
| CREAD
#endif
#ifdef CS8
| CS8
#endif
#ifdef HUPCL
| HUPCL
#endif
;
#ifdef EXTB
cfsetispeed(term, EXTB);
cfsetospeed(term, EXTB);
#else
# ifdef B38400
cfsetispeed(term, B38400);
cfsetospeed(term, B38400);
# else
# ifdef B9600
cfsetispeed(term, B9600);
cfsetospeed(term, B9600);
# endif
# endif
#endif /* EXTB */
term->c_lflag = 0
#ifdef ECHO
| ECHO
#endif
#ifdef ICANON
| ICANON
#endif
#ifdef ISIG
| ISIG
#endif
#ifdef IEXTEN
| IEXTEN
#endif
#ifdef ECHOE
| ECHOE
#endif
#ifdef ECHOKE
| ECHOKE
#endif
#ifdef ECHOK
| ECHOK
#endif
#ifdef ECHOCTL
| ECHOCTL
#endif
;
#ifdef N_TTY
/* should really be a check for c_line, but maybe this is good enough */
term->c_line = N_TTY;
#endif
/* These two may overlap so set them first */
/* That setup means, that read() will be blocked until */
/* at least 1 symbol will be read. */
term->c_cc[VMIN] = 1;
term->c_cc[VTIME] = 0;
/*
* Now set the characters. This is of course a religious matter
* but we use the defaults, with erase bound to the key gnome-terminal
* maps.
*
* These are the ones set by "stty sane".
*/
term->c_cc[VINTR] = 'C'-64;
term->c_cc[VQUIT] = '\\'-64;
term->c_cc[VERASE] = 127;
term->c_cc[VKILL] = 'U'-64;
term->c_cc[VEOF] = 'D'-64;
#ifdef VSWTC
term->c_cc[VSWTC] = 255;
#endif
term->c_cc[VSTART] = 'Q'-64;
term->c_cc[VSTOP] = 'S'-64;
term->c_cc[VSUSP] = 'Z'-64;
term->c_cc[VEOL] = 255;
/*
* Extended stuff.
*/
#ifdef VREPRINT
term->c_cc[VREPRINT] = 'R'-64;
#endif
#ifdef VSTATUS
term->c_cc[VSTATUS] = 'T'-64;
#endif
#ifdef VDISCARD
term->c_cc[VDISCARD] = 'O'-64;
#endif
#ifdef VWERASE
term->c_cc[VWERASE] = 'W'-64;
#endif
#ifdef VLNEXT
term->c_cc[VLNEXT] = 'V'-64;
#endif
#ifdef VDSUSP
term->c_cc[VDSUSP] = 'Y'-64;
#endif
#ifdef VEOL2
term->c_cc[VEOL2] = 255;
#endif
return term;
}
static GSList *pty_fds;
TemuPty *temu_pty_new_execve(
GIOFunc data_func, GIOFunc err_func, gpointer data,
const char *path, char *const argv[], char *const envp[]
)
{
TemuPty *pty;
pid_t pid;
int master;
GIOFlags flags;
struct termios ti;
GSList *fds;
init_term_with_defaults(&ti);
pid = forkpty(&master, NULL, &ti, NULL);
if (pid == -1)
return NULL;
if (!pid) {
for (fds = pty_fds; fds; fds = g_slist_next(fds))
close(GPOINTER_TO_INT(fds->data));
execve(path, argv, envp);
exit(127);
}
pty = g_new(TemuPty, 1);
pty_fds = g_slist_prepend(pty_fds, GINT_TO_POINTER(master));
/* Whoever decided to screw with the data by _DEFAULT_ loses. */
pty->master = g_io_channel_unix_new(master);
g_io_channel_set_encoding(pty->master, NULL, NULL);
g_io_channel_set_buffered(pty->master, FALSE);
flags = g_io_channel_get_flags(pty->master) | G_IO_FLAG_NONBLOCK;
g_io_channel_set_flags(pty->master, flags, NULL);
pty->data_watch = g_io_add_watch_full(pty->master, G_PRIORITY_HIGH, G_IO_IN|G_IO_PRI, data_func, data, NULL);
pty->err_watch = g_io_add_watch(pty->master, G_IO_ERR|G_IO_HUP, err_func, data);
return pty;
}
void temu_pty_destroy(TemuPty *pty)
{
pty_fds = g_slist_remove(pty_fds, GINT_TO_POINTER(g_io_channel_unix_get_fd(pty->master)));
g_source_remove(pty->data_watch);
g_source_remove(pty->err_watch);
g_io_channel_shutdown(pty->master, TRUE, NULL);
g_io_channel_unref(pty->master);
g_free(pty);
}
int temu_pty_set_size(TemuPty *pty, gint cols, gint rows)
{
struct winsize size;
memset(&size, 0, sizeof(size));
size.ws_col = cols;
size.ws_row = rows;
return ioctl(g_io_channel_unix_get_fd(pty->master), TIOCSWINSZ, &size);
}