-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvterm.c
More file actions
360 lines (284 loc) · 8.07 KB
/
Copy pathvterm.c
File metadata and controls
360 lines (284 loc) · 8.07 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <signal.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>
#ifdef __linux__ // forkpty() in linux
#include <pty.h>
#include <utmp.h>
#endif
#ifdef __FreeBSD__ // forkpty() in freebsd
#include <sys/ioctl.h>
#include <libutil.h>
#endif
#if defined(__APPLE__) && defined(__MACH__) // forkpty() in osx
#include <util.h>
#else
#include <term.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include "vterm.h"
#include "vterm_private.h"
#include "vterm_write.h"
#include "vterm_exec.h"
#include "vterm_buffer.h"
#include "vterm_csi.h"
#include "color_cache.h"
#include "mouse_driver.h"
#include "macros.h"
#include "utlist.h"
/*
as a shared library, it's necssary for all the terminals to be able to
share color palette and mouse state information because these are
shared resources.
*/
color_cache_t *color_cache = NULL;
void
_vterm_set_guest_env(vterm_t *vterm);
void
_vterm_set_host_env(vterm_t *vterm);
vterm_t*
vterm_alloc(void)
{
vterm_t *vterm;
vterm = (vterm_t*)calloc(1, sizeof(vterm_t));
return vterm;
}
vterm_t*
vterm_init(vterm_t *vterm, uint16_t width, uint16_t height, uint32_t flags)
{
pid_t child_pid = 0;
int master_fd;
struct winsize ws = {.ws_xpixel = 0, .ws_ypixel = 0};
char *pos = NULL;
int retval;
int fd_flags;
// xterm emulation is the default if none specified
if((flags & VTERM_TERM_MASK) == 0) flags |= VTERM_FLAG_XTERM_256;
#ifdef NOCURSES
flags = flags | VTERM_FLAG_NOCURSES;
#endif
#ifdef NOUTF8
flags = flags | VTERM_FLAG_NOUTF8;
#endif
if(height <= 0 || width <= 0) return NULL;
// a new instance
if(vterm == NULL)
vterm = (vterm_t*)calloc(1, sizeof(vterm_t));
// seed the active descriptor cache. vterm_desc_idx defaults to 0
// (VTERM_BUF_STANDARD) from calloc; the inline array address is stable
// for the lifetime of the vterm_t.
vterm->v_desc_active = &vterm->vterm_desc[VTERM_BUF_STANDARD];
// allocate a the buffer (a matrix of cells)
vterm_buffer_alloc(vterm, VTERM_BUF_STANDARD, width, height);
/*
allocate the scrollback buffer to 4x height and erase the buffer
with all blanks.
*/
vterm_buffer_alloc(vterm, VTERM_BUF_HISTORY, width, height * 4);
vterm_erase(vterm, VTERM_BUF_HISTORY, '-');
// initializes the color cache or updates the ref count
color_cache_init();
// default active colors
// uses ncurses macros even if we aren't using ncurses.
// it is just a bit mask/shift operation.
if(flags & VTERM_FLAG_NOCURSES)
{
// todo: gracefully handle NOCURSES
}
else
{
vterm->vterm_desc[0].curattr = 0;
}
// initialize all cells with defaults
vterm_erase(vterm, VTERM_BUF_STANDARD, 0);
if(flags & VTERM_FLAG_DUMP)
{
// setup the base filepath for the dump file
vterm->debug_filepath = (char*)calloc(PATH_MAX + 1,sizeof(char));
if( getcwd(vterm->debug_filepath, PATH_MAX) == NULL )
{
fprintf(stderr,"ERROR: Failed to getcwd()\n");
pos = 0;
}
else
{
pos = vterm->debug_filepath;
pos += strlen(vterm->debug_filepath);
if (pos[0] != '/')
{
pos[0] = '/';
pos++;
}
}
}
vterm->flags = flags;
memset(&ws, 0, sizeof(ws));
ws.ws_row = height;
ws.ws_col = width;
/*
rxvt has a crazy escape sequence for resetting the terminal.
others typically use \Ec
*/
if(flags & VTERM_FLAG_RXVT)
{
vterm->rs1_reset = interpret_csi_RS1_rxvt;
}
if(flags & VTERM_FLAG_NOPTY)
{ // skip all the child process and fd stuff.
}
else
{
child_pid = forkpty(&master_fd, NULL, NULL, &ws);
vterm->pty_fd = master_fd;
if(child_pid < 0)
{
vterm_destroy(vterm);
exit(EXIT_FAILURE);
}
if(child_pid == 0)
{
signal(SIGINT, SIG_DFL);
_vterm_set_guest_env(vterm);
retval = vterm_exec_binary(vterm);
if(retval == -1) exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}
vterm->child_pid = child_pid;
_vterm_set_host_env(vterm);
if(flags & VTERM_FLAG_DUMP)
{
sprintf(pos, "vterm-%d.dump", child_pid);
vterm->debug_fd = open(vterm->debug_filepath,
O_CREAT | O_TRUNC | O_WRONLY, S_IWUSR | S_IRUSR);
}
if(ttyname_r(master_fd,vterm->ttyname,sizeof(vterm->ttyname) - 1) != 0)
{
snprintf(vterm->ttyname,sizeof(vterm->ttyname) - 1, "vterm");
}
}
/* status flags (O_NONBLOCK), not FD flags (FD_CLOEXEC). Was F_SETFD,
which silently no-op'd the nonblock request and left the master
blocking. Nonblocking + write-all in _vterm_write_pty keeps paste
and keystroke writes from hanging the host while still completing. */
fd_flags = fcntl(vterm->pty_fd, F_GETFL);
if(fd_flags != -1)
fcntl(vterm->pty_fd, F_SETFL, fd_flags | O_NONBLOCK);
use_extended_names(TRUE);
vterm->write = vterm_write_keymap;
vterm->read_buf = (char *)calloc(MAX_PIPE_READ + 10, sizeof(char));
return vterm;
}
void
vterm_destroy(vterm_t *vterm)
{
int i;
if(vterm == NULL) return;
color_cache_free_pairs(vterm);
vterm_free_mapped_colors(vterm);
color_cache_release();
// unload the mouse driver on exit
mouse_driver_free(vterm);
// todo: do something more elegant in the future
for(i = 0; i < 3; i++)
{
vterm_buffer_dealloc(vterm, i);
}
free(vterm->read_buf);
free(vterm->title);
free(vterm);
vterm = NULL;
return;
}
pid_t
vterm_get_pid(vterm_t *vterm)
{
if(vterm == NULL) return -1;
return vterm->child_pid;
}
int
vterm_get_pty_fd(vterm_t *vterm)
{
if(vterm == NULL) return -1;
return vterm->pty_fd;
}
const char*
vterm_get_ttyname(vterm_t *vterm)
{
if(vterm == NULL) return NULL;
return (const char*)vterm->ttyname;
}
void
_vterm_set_host_env(vterm_t *vterm)
{
int term_colors = 0;
int broken_bits = 0;
term_colors = tigetnum("colors");
broken_bits = tigetnum("ncv");
// bad value passed. fallback to normal xterm mode
if(term_colors < 16)
{
vterm->flags &= ~VTERM_FLAG_XTERM_256;
vterm->flags |= VTERM_FLAG_XTERM;
}
// probably not safe to use 16-colors
if(broken_bits > 0)
{
if((term_colors < 16) && (broken_bits & 0x10))
{
vterm->flags |= VTERM_FLAG_C8;
vterm->flags &= ~VTERM_FLAG_C16;
}
}
return;
}
void
_vterm_set_guest_env(vterm_t *vterm)
{
int term_colors = 0;
term_colors = tigetnum("colors");
if(vterm->flags & VTERM_FLAG_RXVT)
{
setenv("TERM", "rxvt", 1);
setenv("COLORTERM", "rxvt", 1);
}
if(vterm->flags & VTERM_FLAG_XTERM)
{
setenv("TERM", "xterm", 1);
if(vterm->flags & VTERM_FLAG_TRUECOLOR)
setenv("COLORTERM", "truecolor", 1);
else
setenv("COLORTERM", "xterm", 1);
}
if(vterm->flags & VTERM_FLAG_XTERM_256)
{
setenv("TERM", "xterm", 1);
#ifndef __FreeBSD__
if(term_colors == 16)
setenv("TERM", "xterm-16color", 1);
if(term_colors > 16)
// setenv("TERM", "xterm-direct", 1);
setenv("TERM", "xterm-256color", 1);
#endif
if(vterm->flags & VTERM_FLAG_TRUECOLOR)
setenv("COLORTERM", "truecolor", 1);
else
setenv("COLORTERM", "xterm", 1);
}
if(vterm->flags & VTERM_FLAG_LINUX)
{
setenv("TERM", "linux", 1);
unsetenv("COLORTERM");
}
if(vterm->flags & VTERM_FLAG_VT100)
{
setenv("TERM", "vt100", 1);
unsetenv("COLORTERM");
}
return;
}