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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
/* $Id: tty.c,v 1.30 2008-06-20 06:36:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <ncurses.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "tmux.h"
struct tty_term *tty_find_term(char *, int,char **);
void tty_free_term(struct tty_term *);
void tty_fill_acs(struct tty *);
u_char tty_get_acs(struct tty *, u_char);
void tty_raw(struct tty *, const char *);
void tty_puts(struct tty *, const char *);
void tty_putc(struct tty *, char);
void tty_attributes(struct tty *, u_char, u_char);
char tty_translate(char);
TAILQ_HEAD(, tty_term) tty_terms = TAILQ_HEAD_INITIALIZER(tty_terms);
void
tty_init(struct tty *tty, char *path, char *term)
{
tty->path = xstrdup(path);
if (term == NULL)
tty->termname = xstrdup("unknown");
else
tty->termname = xstrdup(term);
}
int
tty_open(struct tty *tty, char **cause)
{
struct termios tio;
int what, mode;
tty->fd = open(tty->path, O_RDWR|O_NONBLOCK);
if (tty->fd == -1) {
xasprintf(cause, "%s: %s", tty->path, strerror(errno));
return (-1);
}
if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
fatal("fcntl");
if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl");
if ((tty->term = tty_find_term(tty->termname, tty->fd, cause)) == NULL)
goto error;
tty->in = buffer_create(BUFSIZ);
tty->out = buffer_create(BUFSIZ);
tty->attr = 0;
tty->colr = 0x70;
tty->flags = 0;
tty_keys_init(tty);
tty_fill_acs(tty);
if (tcgetattr(tty->fd, &tty->tio) != 0)
fatal("tcgetattr failed");
memcpy(&tio, &tty->tio, sizeof tio);
tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|IUCLC|ISTRIP);
tio.c_iflag |= IGNBRK;
tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET|OLCUC);
tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|ECHOPRT|ECHOKE|ECHOCTL|ISIG);
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
fatal("tcsetattr failed");
#ifdef TIOCFLUSH
what = 0;
if (ioctl(tty->fd, TIOCFLUSH, &what) != 0)
fatal("ioctl(TIOCFLUSH)");
#endif
if (enter_ca_mode != NULL)
tty_puts(tty, enter_ca_mode);
if (keypad_xmit != NULL)
tty_puts(tty, keypad_xmit);
if (ena_acs != NULL)
tty_puts(tty, ena_acs);
tty_puts(tty, clear_screen);
return (0);
error:
close(tty->fd);
tty->fd = -1;
return (-1);
}
void
tty_close(struct tty *tty)
{
struct winsize ws;
if (tty->fd == -1)
return;
/*
* Skip any writing if the fd is invalid. Things like ssh -t can
* easily leave us with a dead tty.
*/
if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1) {
if (errno != EBADF && errno != ENXIO)
fatal("ioctl(TIOCGWINSZ)");
} else {
if (tcsetattr(tty->fd, TCSANOW, &tty->tio) != 0)
fatal("tcsetattr failed");
tty_raw(tty, tparm(change_scroll_region, 0, ws.ws_row - 1));
if (keypad_local != NULL)
tty_raw(tty, keypad_local);
if (exit_ca_mode != NULL)
tty_raw(tty, exit_ca_mode);
tty_raw(tty, clear_screen);
if (cursor_normal != NULL)
tty_raw(tty, cursor_normal);
if (exit_attribute_mode != NULL)
tty_raw(tty, exit_attribute_mode);
}
tty_free_term(tty->term);
tty_keys_free(tty);
close(tty->fd);
tty->fd = -1;
buffer_destroy(tty->in);
buffer_destroy(tty->out);
}
void
tty_free(struct tty *tty)
{
tty_close(tty);
if (tty->path != NULL)
xfree(tty->path);
if (tty->termname != NULL)
xfree(tty->termname);
}
struct tty_term *
tty_find_term(char *name, int fd, char **cause)
{
struct tty_term *term;
int error;
TAILQ_FOREACH(term, &tty_terms, entry) {
if (strcmp(term->name, name) == 0) {
term->references++;
return (term);
}
}
term = xmalloc(sizeof *term);
term->name = xstrdup(name);
term->term = NULL;
term->references = 1;
TAILQ_INSERT_HEAD(&tty_terms, term, entry);
if (setupterm(name, fd, &error) != OK) {
switch (error) {
case 0:
xasprintf(cause, "can't use hardcopy terminal");
break;
case 1:
xasprintf(cause, "missing or unsuitable terminal");
break;
case 2:
xasprintf(cause, "can't find terminfo database");
break;
default:
xasprintf(cause, "unknown error");
break;
}
goto error;
}
term->term = cur_term;
if (clear_screen == NULL) {
xasprintf(cause, "clear_screen missing");
goto error;
}
if (cursor_down == NULL) {
xasprintf(cause, "cursor_down missing");
goto error;
}
if (carriage_return == NULL) {
xasprintf(cause, "carriage_return missing");
goto error;
}
if (cursor_left == NULL) {
xasprintf(cause, "cursor_left missing");
goto error;
}
if (parm_up_cursor == NULL && cursor_up == NULL) {
xasprintf(cause, "parm_up_cursor missing");
goto error;
}
if (parm_down_cursor == NULL && cursor_down == NULL) {
xasprintf(cause, "parm_down_cursor missing");
goto error;
}
if (parm_right_cursor == NULL && cursor_right == NULL) {
xasprintf(cause, "parm_right_cursor missing");
goto error;
}
if (parm_left_cursor == NULL && cursor_left == NULL) {
xasprintf(cause, "parm_left_cursor missing");
goto error;
}
if (cursor_address == NULL) {
xasprintf(cause, "cursor_address missing");
goto error;
}
if (parm_insert_line == NULL && insert_line == NULL) {
xasprintf(cause, "parm_insert_line missing");
goto error;
}
if (parm_delete_line == NULL && delete_line == NULL) {
xasprintf(cause, "parm_delete_line missing");
goto error;
}
if (parm_ich == NULL && insert_character == NULL &&
(enter_insert_mode == NULL || exit_insert_mode == NULL)) {
xasprintf(cause, "parm_ich missing");
goto error;
}
if (scroll_reverse == NULL) {
xasprintf(cause, "scroll_reverse missing");
goto error;
}
if (change_scroll_region == NULL) {
xasprintf(cause, "change_scroll_region missing");
goto error;
}
return (term);
error:
tty_free_term(term);
return (NULL);
}
void
tty_free_term(struct tty_term *term)
{
if (--term->references != 0)
return;
TAILQ_REMOVE(&tty_terms, term, entry);
#ifdef __FreeBSD___
/*
* XXX XXX XXX FIXME FIXME
* FreeBSD 6.2 crashes with a double-free somewhere under here.
*/
if (term->term != NULL)
del_curterm(term->term);
#endif
xfree(term->name);
xfree(term);
}
void
tty_fill_acs(struct tty *tty)
{
char *ptr;
memset(tty->acs, 0, sizeof tty->acs);
if (acs_chars == NULL || (strlen(acs_chars) % 2) != 0)
return;
for (ptr = acs_chars; *ptr != '\0'; ptr += 2)
tty->acs[(u_char) ptr[0]] = ptr[1];
}
u_char
tty_get_acs(struct tty *tty, u_char ch)
{
if (tty->acs[ch] != '\0')
return (tty->acs[ch]);
return (ch);
}
void
tty_raw(struct tty *tty, const char *s)
{
write(tty->fd, s, strlen(s));
}
void
tty_puts(struct tty *tty, const char *s)
{
buffer_write(tty->out, s, strlen(s));
}
void
tty_putc(struct tty *tty, char ch)
{
if (tty->attr & ATTR_CHARSET)
ch = tty_get_acs(tty, ch);
buffer_write8(tty->out, ch);
}
void
tty_set_title(struct tty *tty, const char *title)
{
tty_puts(tty, "\e]0;");
tty_puts(tty, title);
tty_putc(tty, '\007');
}
void
tty_vwrite(struct tty *tty, struct screen *s, int cmd, va_list ap)
{
char ch;
u_int i, ua, ub;
if (tty->flags & TTY_FREEZE)
return;
if (tty->term == NULL) /* XXX XXX */
return;
set_curterm(tty->term->term);
switch (cmd) {
case TTY_CHARACTER:
ch = va_arg(ap, int);
switch (ch) {
case '\n': /* LF */
tty_puts(tty, cursor_down);
break;
case '\r': /* CR */
tty_puts(tty, carriage_return);
break;
case '\007': /* BEL */
if (bell != NULL)
tty_puts(tty, bell);
break;
case '\010': /* BS */
tty_puts(tty, cursor_left);
break;
default:
tty_putc(tty, ch);
break;
}
break;
case TTY_CURSORUP:
ua = va_arg(ap, u_int);
if (parm_up_cursor != NULL)
tty_puts(tty, tparm(parm_up_cursor, ua));
else {
while (ua-- > 0)
tty_puts(tty, cursor_up);
}
break;
case TTY_CURSORDOWN:
ua = va_arg(ap, u_int);
if (parm_down_cursor != NULL)
tty_puts(tty, tparm(parm_down_cursor, ua));
else {
while (ua-- > 0)
tty_puts(tty, cursor_down);
}
break;
case TTY_CURSORRIGHT:
ua = va_arg(ap, u_int);
if (parm_right_cursor != NULL)
tty_puts(tty, tparm(parm_right_cursor, ua));
else {
while (ua-- > 0)
tty_puts(tty, cursor_right);
}
break;
case TTY_CURSORLEFT:
ua = va_arg(ap, u_int);
if (parm_left_cursor != NULL)
tty_puts(tty, tparm(parm_left_cursor, ua));
else {
while (ua-- > 0)
tty_puts(tty, cursor_left);
}
break;
case TTY_CURSORMOVE:
ua = va_arg(ap, u_int);
ub = va_arg(ap, u_int);
tty_puts(tty, tparm(cursor_address, ua, ub));
break;
case TTY_CLEARENDOFLINE:
if (clr_eol != NULL)
tty_puts(tty, clr_eol);
else {
tty_puts(tty, tparm(cursor_address, s->cy, s->cx));
for (i = s->cx; i < screen_size_x(s); i++)
tty_putc(tty, ' ');
tty_puts(tty, tparm(cursor_address, s->cy, s->cx));
}
break;
case TTY_CLEARSTARTOFLINE:
if (clr_bol != NULL)
tty_puts(tty, clr_bol);
else {
tty_puts(tty, tparm(cursor_address, s->cy, 0));
for (i = 0; i < s->cx + 1; i++)
tty_putc(tty, ' ');
tty_puts(tty, tparm(cursor_address, s->cy, s->cx));
}
break;
case TTY_CLEARLINE:
if (clr_eol != NULL) {
tty_puts(tty, tparm(cursor_address, s->cy, 0));
tty_puts(tty, clr_eol);
tty_puts(tty, tparm(cursor_address, s->cy, s->cx));
} else {
tty_puts(tty, tparm(cursor_address, s->cy, 0));
for (i = 0; i < screen_size_x(s); i++)
tty_putc(tty, ' ');
tty_puts(tty, tparm(cursor_address, s->cy, s->cx));
}
break;
case TTY_INSERTLINE:
ua = va_arg(ap, u_int);
if (parm_insert_line != NULL)
tty_puts(tty, tparm(parm_insert_line, ua));
else {
while (ua-- > 0)
tty_puts(tty, insert_line);
}
break;
case TTY_DELETELINE:
ua = va_arg(ap, u_int);
if (parm_delete_line != NULL)
tty_puts(tty, tparm(parm_delete_line, ua));
else {
while (ua-- > 0)
tty_puts(tty, delete_line);
}
break;
case TTY_INSERTCHARACTER:
ua = va_arg(ap, u_int);
if (parm_ich != NULL)
tty_puts(tty, tparm(parm_ich, ua));
else if (insert_character != NULL) {
while (ua-- > 0)
tty_puts(tty, insert_character);
} else if (enter_insert_mode != NULL) {
tty_puts(tty, enter_insert_mode);
while (ua-- > 0)
tty_putc(tty, ' ');
tty_puts(tty, exit_insert_mode);
tty_puts(tty, tparm(cursor_address, s->cy, s->cx));
}
break;
case TTY_DELETECHARACTER:
ua = va_arg(ap, u_int);
if (parm_dch != NULL)
tty_puts(tty, tparm(parm_dch, ua));
else if (delete_character != NULL) {
while (ua-- > 0)
tty_puts(tty, delete_character);
} else {
while (ua-- > 0)
tty_putc(tty, '\010');
}
break;
case TTY_CURSORON:
if (!(tty->flags & TTY_NOCURSOR) && cursor_normal != NULL)
tty_puts(tty, cursor_normal);
break;
case TTY_CURSOROFF:
if (cursor_invisible != NULL)
tty_puts(tty, cursor_invisible);
break;
case TTY_REVERSEINDEX:
tty_puts(tty, scroll_reverse);
break;
case TTY_SCROLLREGION:
ua = va_arg(ap, u_int);
ub = va_arg(ap, u_int);
tty_puts(tty, tparm(change_scroll_region, ua, ub));
break;
#if 0
case TTY_INSERTON:
if (enter_insert_mode != NULL)
tty_puts(tty, enter_insert_mode);
break;
case TTY_INSERTOFF:
if (exit_insert_mode != NULL)
tty_puts(tty, exit_insert_mode);
break;
case TTY_KCURSOROFF:
t = tigetstr("CE");
if (t != (char *) 0 && t != (char *) -1)
tty_puts(tty, t);
break;
case TTY_KCURSORON:
t = tigetstr("CS");
if (t != (char *) 0 && t != (char *) -1)
tty_puts(tty, t);
break;
case TTY_KKEYPADOFF:
if (keypad_local != NULL)
tty_puts(tty, keypad_local);
break;
case TTY_KKEYPADON:
if (keypad_xmit != NULL)
tty_puts(tty, keypad_xmit);
break;
#endif
case TTY_MOUSEOFF:
if (key_mouse != NULL)
tty_puts(tty, "\e[?1000l");
break;
case TTY_MOUSEON:
if (key_mouse != NULL)
tty_puts(tty, "\e[?1000h");
break;
case TTY_ATTRIBUTES:
ua = va_arg(ap, u_int);
ub = va_arg(ap, u_int);
tty_attributes(tty, ua, ub);
break;
}
}
void
tty_attributes(struct tty *tty, u_char attr, u_char colr)
{
u_char fg, bg;
if (attr == tty->attr && colr == tty->colr)
return;
/* If any bits are being cleared, reset everything. */
if (tty->attr & ~attr) {
if ((tty->attr & ATTR_CHARSET) &&
exit_alt_charset_mode != NULL)
tty_puts(tty, exit_alt_charset_mode);
tty_puts(tty, exit_attribute_mode);
tty->colr = 0x70;
tty->attr = 0;
}
/* Filter out bits already set. */
attr &= ~tty->attr;
tty->attr |= attr;
if ((attr & ATTR_BRIGHT) && enter_bold_mode != NULL)
tty_puts(tty, enter_bold_mode);
if ((attr & ATTR_DIM) && enter_dim_mode != NULL)
tty_puts(tty, enter_dim_mode);
if ((attr & ATTR_ITALICS) && enter_standout_mode != NULL)
tty_puts(tty, enter_standout_mode);
if ((attr & ATTR_UNDERSCORE) && enter_underline_mode != NULL)
tty_puts(tty, enter_underline_mode);
if ((attr & ATTR_BLINK) && enter_blink_mode != NULL)
tty_puts(tty, enter_blink_mode);
if ((attr & ATTR_REVERSE) && enter_reverse_mode != NULL)
tty_puts(tty, enter_reverse_mode);
if ((attr & ATTR_HIDDEN) && enter_secure_mode != NULL)
tty_puts(tty, enter_secure_mode);
if ((attr & ATTR_CHARSET) && enter_alt_charset_mode != NULL)
tty_puts(tty, enter_alt_charset_mode);
fg = (colr >> 4) & 0xf;
if (fg != ((tty->colr >> 4) & 0xf)) {
if (tigetflag("AX") == TRUE) {
if (fg == 7)
fg = 8;
} else {
if (fg == 8)
fg = 7;
}
if (fg == 8)
tty_puts(tty, "\e[39m");
else if (set_a_foreground != NULL)
tty_puts(tty, tparm(set_a_foreground, fg));
}
bg = colr & 0xf;
if (bg != (tty->colr & 0xf)) {
if (tigetflag("AX") == TRUE) {
if (bg == 0)
bg = 8;
} else {
if (bg == 8)
bg = 0;
}
if (bg == 8)
tty_puts(tty, "\e[49m");
else if (set_a_background != NULL)
tty_puts(tty, tparm(set_a_background, bg));
}
tty->colr = colr;
}
|