17 #define MODE_CURSOR 0x01
18 #define MODE_WRAP 0x02
19 #define MODE_ORIGIN 0x04
20 #define MODE_AUTOCR 0x08
21 #define MODE_DEFAULT (MODE_CURSOR | MODE_WRAP)
22 #define ATTR_BOLD 0x10
24 #define ATTR_ALL (ATTR_BOLD | ATTR_REV)
25 #define MODE_WRAPREADY 0x80
27 #define BIT_SET(i, b, val) ((val) ? ((i) | (b)) : ((i) & ~(b)))
28 #define OFFSET(r, c) ((r) * pad_cols() + (c))
30 static struct term
*term
;
32 static char *fgs
, *bgs
;
39 #define MAXLINES (1 << 8)
40 static int dirty
[MAXLINES
];
43 static char fgcolor(void)
45 int c
= mode
& ATTR_REV
? bg
: fg
;
46 return mode
& ATTR_BOLD
? c
| 0x08 : c
;
49 static char bgcolor(void)
51 return mode
& ATTR_REV
? fg
: bg
;
54 static void _draw_pos(int r
, int c
, int cursor
)
56 int rev
= cursor
&& mode
& MODE_CURSOR
;
58 char fg
= rev
? bgs
[i
] : fgs
[i
];
59 char bg
= rev
? fgs
[i
] : bgs
[i
];
61 pad_put(screen
[i
], r
, c
, fg
, bg
);
64 static void _draw_row(int r
)
67 pad_blankrow(r
, bgcolor());
68 for (i
= 0; i
< pad_cols(); i
++) {
69 int c
= screen
[OFFSET(r
, i
)];
70 if ((c
&& c
!= ' ') || bgs
[OFFSET(r
, i
)] != bgcolor())
75 static void draw_rows(int sr
, int er
)
80 for (i
= sr
; i
< er
; i
++) {
88 static void draw_cols(int r
, int sc
, int ec
)
96 for (i
= sc
; i
< ec
; i
++)
100 static void draw_char(int ch
, int r
, int c
)
102 int i
= OFFSET(r
, c
);
114 static void draw_cursor(int put
)
121 _draw_pos(row
, col
, put
);
124 static void lazy_clean(void)
127 memset(dirty
, 0, sizeof(dirty
));
130 static void lazy_flush(void)
135 _draw_pos(row
, col
, 0);
136 for (i
= 0; i
< pad_rows(); i
++)
140 _draw_pos(row
, col
, 1);
143 static void term_redraw(void)
146 for (i
= 0; i
< pad_rows(); i
++)
148 _draw_pos(row
, col
, 1);
151 static int origin(void)
153 return mode
& MODE_ORIGIN
;
156 #define PTYBUFSIZE (1 << 13)
157 static char ptybuf
[PTYBUFSIZE
];
160 static void waitpty(void)
162 struct pollfd ufds
[1];
163 ufds
[0].fd
= term
->fd
;
164 ufds
[0].events
= POLLIN
;
168 static int readpty(void)
171 return ptybuf
[ptycur
++];
174 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
175 if (ptylen
== -1 && errno
== EAGAIN
) {
177 ptylen
= read(term
->fd
, ptybuf
, PTYBUFSIZE
);
186 static void screen_move(int dst
, int src
, int n
)
188 memmove(screen
+ dst
, screen
+ src
, n
* sizeof(*screen
));
189 memmove(fgs
+ dst
, fgs
+ src
, n
);
190 memmove(bgs
+ dst
, bgs
+ src
, n
);
193 static void screen_reset(int i
, int n
)
195 memset(screen
+ i
, 0, n
* sizeof(*screen
));
196 memset(fgs
+ i
, fg
, n
);
197 memset(bgs
+ i
, bg
, n
);
200 static void empty_rows(int sr
, int er
)
202 screen_reset(OFFSET(sr
, 0), (er
- sr
) * pad_cols());
205 static void blank_rows(int sr
, int er
)
212 static void scroll_screen(int sr
, int nr
, int n
)
215 screen_move(OFFSET(sr
+ n
, 0), OFFSET(sr
, 0), nr
* pad_cols());
217 empty_rows(sr
, sr
+ n
);
219 empty_rows(sr
+ nr
+ n
, sr
+ nr
);
220 draw_rows(MIN(sr
, sr
+ n
), MAX(sr
+ nr
, sr
+ nr
+ n
));
224 static void insert_lines(int n
)
226 int sr
= MAX(top
, row
);
227 int nr
= bot
- row
- n
;
229 scroll_screen(sr
, nr
, n
);
232 static void delete_lines(int n
)
234 int r
= MAX(top
, row
);
236 int nr
= bot
- r
- n
;
238 scroll_screen(sr
, nr
, -n
);
241 static void move_cursor(int r
, int c
)
245 t
= origin() ? top
: 0;
246 b
= origin() ? bot
: pad_rows();
247 row
= MAX(t
, MIN(r
, b
- 1));
248 col
= MAX(0, MIN(c
, pad_cols() - 1));
250 mode
= BIT_SET(mode
, MODE_WRAPREADY
, 0);
253 static void advance(int dr
, int dc
, int scrl
)
257 if (r
>= bot
&& scrl
) {
259 int nr
= (bot
- top
) + n
;
260 scroll_screen(top
+ -n
, nr
, n
);
262 if (r
< top
&& scrl
) {
264 int nr
= (bot
- top
) - n
;
265 scroll_screen(top
, nr
, n
);
267 r
= MIN(bot
- 1, MAX(top
, r
));
268 c
= MIN(pad_cols() - 1, MAX(0, c
));
272 static void insertchar(int c
)
275 if (mode
& MODE_WRAPREADY
)
277 draw_char(c
, row
, col
);
278 wrapready
= col
== pad_cols() - 1;
281 mode
= BIT_SET(mode
, MODE_WRAPREADY
, 1);
284 void term_send(int c
)
288 write(term
->fd
, &b
, 1);
291 static void term_sendstr(char *s
)
294 write(term
->fd
, s
, strlen(s
));
297 static void setattr(int m
)
319 fg
= m
> 37 ? FGCOLOR
: m
- 30;
321 bg
= m
> 47 ? BGCOLOR
: m
- 40;
325 static void kill_chars(int sc
, int ec
)
328 for (i
= sc
; i
< ec
; i
++)
329 draw_char(' ', row
, i
);
333 static void move_chars(int sc
, int nc
, int n
)
336 screen_move(OFFSET(row
, sc
+ n
), OFFSET(row
, sc
), nc
);
338 screen_reset(OFFSET(row
, sc
), n
);
340 screen_reset(OFFSET(row
, pad_cols() + n
), -n
);
341 draw_cols(row
, MIN(sc
, sc
+ n
), pad_cols());
345 static void delete_chars(int n
)
348 int nc
= pad_cols() - sc
;
349 move_chars(sc
, nc
, -n
);
352 static void insert_chars(int n
)
354 int nc
= pad_cols() - col
- n
;
355 move_chars(col
, nc
, n
);
358 static void term_blank(void)
360 screen_reset(0, pad_rows() * pad_cols());
362 pad_blank(bgcolor());
367 static void ctlseq(void);
371 while (ptycur
< ptylen
) {
372 if (ptylen
- ptycur
> 15)
381 static void term_reset(void)
391 static int _openpty(int *master
, int *slave
)
396 if ((*master
= open("/dev/ptmx", O_RDWR
)) == -1)
398 if (ioctl(*master
, TIOCSPTLCK
, &unlock
) == -1)
400 if (ioctl(*master
, TIOCGPTN
, &ptyno
) == -1)
402 sprintf(name
, "/dev/pts/%d", ptyno
);
403 *slave
= open(name
, O_RDWR
| O_NOCTTY
);
407 static void _login(int fd
)
409 static char env
[] = "TERM=linux";
411 winp
.ws_col
= pad_cols();
412 winp
.ws_row
= pad_rows();
416 ioctl(fd
, TIOCSCTTY
, NULL
);
417 ioctl(fd
, TIOCSWINSZ
, &winp
);
426 void term_exec(char *cmd
)
429 memset(term
, 0, sizeof(*term
));
430 if (_openpty(&master
, &slave
) == -1)
432 if ((term
->pid
= fork()) == -1)
437 execlp(cmd
, cmd
, NULL
);
442 fcntl(term
->fd
, F_SETFD
, fcntl(term
->fd
, F_GETFD
) | FD_CLOEXEC
);
443 fcntl(term
->fd
, F_SETFL
, fcntl(term
->fd
, F_GETFL
) | O_NONBLOCK
);
447 static void misc_save(struct term_state
*state
)
456 static void misc_load(struct term_state
*state
)
465 void term_save(struct term
*term
)
468 misc_save(&term
->cur
);
473 void term_load(struct term
*t
, int flags
)
476 misc_load(&term
->cur
);
477 screen
= term
->screen
;
483 if (flags
== TERM_REDRAW
) {
495 memset(term
, 0, sizeof(*term
));
496 term_load(term
, visible
? TERM_REDRAW
: TERM_HIDDEN
);
499 static void set_region(int t
, int b
)
501 top
= MIN(pad_rows() - 1, MAX(0, t
- 1));
502 bot
= MIN(pad_rows(), MAX(top
+ 1, b
? b
: pad_rows()));
507 static int writeutf8(char *dst
, int c
)
516 *d
++ = 0xf0 | (c
>> 18);
518 } else if (c
> 0x7ff) {
519 *d
++ = 0xe0 | (c
>> 12);
521 } else if (c
> 0x7f) {
522 *d
++ = 0xc0 | (c
>> 6);
526 *d
++ = 0x80 | ((c
>> (l
* 6)) & 0x3f);
530 void term_screenshot(void)
533 int fd
= open(SCREENSHOT
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
535 for (i
= 0; i
< pad_rows(); i
++) {
537 for (j
= 0; j
< pad_cols(); j
++)
538 s
+= writeutf8(s
, screen
[OFFSET(i
, j
)]);
540 write(fd
, buf
, s
- buf
);
545 /* partial vt102 implementation */
547 static void escseq(void);
548 static void escseq_cs(void);
549 static void escseq_g0(void);
550 static void escseq_g1(void);
551 static void escseq_g2(void);
552 static void escseq_g3(void);
553 static void csiseq(void);
554 static void csiseq_da(int c
);
555 static void csiseq_dsr(int c
);
556 static void modeseq(int c
, int set
);
558 /* comments taken from: http://www.ivarch.com/programs/termvt102.shtml */
560 static int readutf8(int c
)
566 while (l
< 6 && c
& (0x40 >> l
))
568 result
= (0x3f >> l
) & c
;
570 result
= (result
<< 6) | (readpty() & 0x3f);
574 #define unknown(ctl, c)
576 /* control sequences */
577 static void ctlseq(void)
581 case 0x09: /* HT horizontal tab to next tab stop */
582 advance(0, 8 - col
% 8, 0);
584 case 0x0a: /* LF line feed */
585 case 0x0b: /* VT line feed */
586 case 0x0c: /* FF line feed */
587 advance(1, (mode
& MODE_AUTOCR
) ? -col
: 0, 1);
589 case 0x08: /* BS backspace one column */
592 case 0x1b: /* ESC start escape sequence */
595 case 0x0d: /* CR carriage return */
598 case 0x00: /* NUL ignored */
599 case 0x07: /* BEL beep */
600 case 0x7f: /* DEL ignored */
602 case 0x05: /* ENQ trigger answerback message */
603 case 0x0e: /* SO activate G1 character set & newline */
604 case 0x0f: /* SI activate G0 character set */
605 case 0x11: /* XON resume transmission */
606 case 0x13: /* XOFF stop transmission, ignore characters */
607 case 0x18: /* CAN interrupt escape sequence */
608 case 0x1a: /* SUB interrupt escape sequence */
609 case 0x9b: /* CSI equivalent to ESC [ */
610 unknown("ctlseq", c
);
613 insertchar(readutf8(c
));
618 #define ESCM(c) (((c) & 0xf0) == 0x20)
619 #define ESCF(c) ((c) > 0x30 && (c) < 0x7f)
621 /* escape sequences */
622 static void escseq(void)
628 case '[': /* CSI control sequence introducer */
631 case '%': /* CS... escseq_cs table */
634 case '(': /* G0... escseq_g0 table */
637 case ')': /* G1... escseq_g1 table */
640 case '*': /* G2... escseq_g2 table */
643 case '+': /* G3... escseq_g3 table */
646 case '7': /* DECSC save state (position, charset, attributes) */
647 misc_save(&term
->sav
);
649 case '8': /* DECRC restore most recently saved state */
650 misc_load(&term
->sav
);
652 case 'M': /* RI reverse line feed */
655 case 'D': /* IND line feed */
658 case 'E': /* NEL newline */
661 case 'c': /* RIS reset */
664 case 'H': /* HTS set tab stop at current column */
665 case 'Z': /* DECID DEC private ID; return ESC [ ? 6 c (VT102) */
666 case '#': /* DECALN ("#8") DEC alignment test - fill screen with E's */
667 case '>': /* DECPNM set numeric keypad mode */
668 case '=': /* DECPAM set application keypad mode */
669 case 'N': /* SS2 select G2 charset for next char only */
670 case 'O': /* SS3 select G3 charset for next char only */
671 case 'P': /* DCS device control string (ended by ST) */
672 case 'X': /* SOS start of string */
673 case '^': /* PM privacy message (ended by ST) */
674 case '_': /* APC application program command (ended by ST) */
675 case '\\': /* ST string terminator */
676 case 'n': /* LS2 invoke G2 charset */
677 case 'o': /* LS3 invoke G3 charset */
678 case '|': /* LS3R invoke G3 charset as GR */
679 case '}': /* LS2R invoke G2 charset as GR */
680 case '~': /* LS1R invoke G1 charset as GR */
681 case ']': /* OSC operating system command */
682 case 'g': /* BEL alternate BEL */
684 unknown("escseq", c
);
689 static void escseq_cs(void)
693 case '@': /* CSDFL select default charset (ISO646/8859-1) */
694 case 'G': /* CSUTF8 select UTF-8 */
695 case '8': /* CSUTF8 select UTF-8 (obsolete) */
697 unknown("escseq_cs", c
);
702 static void escseq_g0(void)
706 case '8': /* G0DFL G0 charset = default mapping (ISO8859-1) */
707 case '0': /* G0GFX G0 charset = VT100 graphics mapping */
708 case 'U': /* G0ROM G0 charset = null mapping (straight to ROM) */
709 case 'K': /* G0USR G0 charset = user defined mapping */
710 case 'B': /* G0TXT G0 charset = ASCII mapping */
712 unknown("escseq_g0", c
);
717 static void escseq_g1(void)
721 case '8': /* G1DFL G1 charset = default mapping (ISO8859-1) */
722 case '0': /* G1GFX G1 charset = VT100 graphics mapping */
723 case 'U': /* G1ROM G1 charset = null mapping (straight to ROM) */
724 case 'K': /* G1USR G1 charset = user defined mapping */
725 case 'B': /* G1TXT G1 charset = ASCII mapping */
727 unknown("escseq_g1", c
);
732 static void escseq_g2(void)
736 case '8': /* G2DFL G2 charset = default mapping (ISO8859-1) */
737 case '0': /* G2GFX G2 charset = VT100 graphics mapping */
738 case 'U': /* G2ROM G2 charset = null mapping (straight to ROM) */
739 case 'K': /* G2USR G2 charset = user defined mapping */
741 unknown("escseq_g2", c
);
746 static void escseq_g3(void)
750 case '8': /* G3DFL G3 charset = default mapping (ISO8859-1) */
751 case '0': /* G3GFX G3 charset = VT100 graphics mapping */
752 case 'U': /* G3ROM G3 charset = null mapping (straight to ROM) */
753 case 'K': /* G3USR G3 charset = user defined mapping */
755 unknown("escseq_g3", c
);
760 static int absrow(int r
)
762 return origin() ? top
+ r
: r
;
765 #define CSIP(c) (((c) & 0xf0) == 0x30)
766 #define CSII(c) (((c) & 0xf0) == 0x20)
767 #define CSIF(c) ((c) >= 0x40 && (c) < 0x80)
769 #define MAXCSIARGS 32
770 /* ECMA-48 CSI sequences */
771 static void csiseq(void)
773 int args
[MAXCSIARGS
] = {0};
780 if (strchr("<=>?", c
)) {
787 arg
= arg
* 10 + (c
- '0');
793 n
= n
< ARRAY_SIZE(args
) ? n
+ 1 : 0;
800 case 'H': /* CUP move cursor to row, column */
801 case 'f': /* HVP move cursor to row, column */
802 move_cursor(absrow(MAX(0, args
[0] - 1)), MAX(0, args
[1] - 1));
804 case 'J': /* ED erase display */
807 kill_chars(col
, pad_cols());
808 blank_rows(row
+ 1, pad_rows());
811 kill_chars(0, col
+ 1);
812 blank_rows(0, row
- 1);
819 case 'A': /* CUU move cursor up */
820 advance(-MAX(1, args
[0]), 0, 0);
822 case 'e': /* VPR move cursor down */
823 case 'B': /* CUD move cursor down */
824 advance(MAX(1, args
[0]), 0, 0);
826 case 'a': /* HPR move cursor right */
827 case 'C': /* CUF move cursor right */
828 advance(0, MAX(1, args
[0]), 0);
830 case 'D': /* CUB move cursor left */
831 advance(0, -MAX(1, args
[0]), 0);
833 case 'K': /* EL erase line */
836 kill_chars(col
, pad_cols());
839 kill_chars(0, col
+ 1);
842 kill_chars(0, pad_cols());
846 case 'L': /* IL insert blank lines */
847 if (row
>= top
&& row
< bot
)
848 insert_lines(MAX(1, args
[0]));
850 case 'M': /* DL delete lines */
851 if (row
>= top
&& row
< bot
)
852 delete_lines(MAX(1, args
[0]));
854 case 'd': /* VPA move to row (current column) */
855 move_cursor(absrow(MAX(1, args
[0]) - 1), col
);
857 case 'm': /* SGR set graphic rendition */
860 for (i
= 0; i
< n
; i
++)
863 case 'r': /* DECSTBM set scrolling region to (top, bottom) rows */
864 set_region(args
[0], args
[1]);
866 case 'c': /* DA return ESC [ ? 6 c (VT102) */
867 csiseq_da(priv
== '?' ? args
[0] | 0x80 : args
[0]);
869 case 'h': /* SM set mode */
870 for (i
= 0; i
< n
; i
++)
871 modeseq(priv
== '?' ? args
[i
] | 0x80 : args
[i
], 1);
873 case 'l': /* RM reset mode */
874 for (i
= 0; i
< n
; i
++)
875 modeseq(priv
== '?' ? args
[i
] | 0x80 : args
[i
], 0);
877 case 'P': /* DCH delete characters on current line */
878 delete_chars(MIN(MAX(1, args
[0]), pad_cols() - col
));
880 case '@': /* ICH insert blank characters */
881 insert_chars(MAX(1, args
[0]));
883 case 'n': /* DSR device status report */
886 case 'G': /* CHA move cursor to column in current row */
887 advance(0, MAX(0, args
[0] - 1) - col
, 0);
889 case 'X': /* ECH erase characters on current line */
890 kill_chars(col
, MIN(col
+ MAX(1, args
[0]), pad_cols()));
892 case '[': /* IGN ignored control sequence */
893 case 'E': /* CNL move cursor down and to column 1 */
894 case 'F': /* CPL move cursor up and to column 1 */
895 case 'g': /* TBC clear tab stop (CSI 3 g = clear all stops) */
896 case 'q': /* DECLL set keyboard LEDs */
897 case 's': /* CUPSV save cursor position */
898 case 'u': /* CUPRS restore cursor position */
899 case '`': /* HPA move cursor to column in current row */
901 unknown("csiseq", c
);
906 static void csiseq_da(int c
)
910 term_sendstr("\x1b[?6c");
913 /* we don't care much about cursor shape */
914 /* printf("csiseq_da <0x%x>\n", c); */
919 static void csiseq_dsr(int c
)
924 term_sendstr("\x1b[0n");
927 snprintf(status
, sizeof(status
), "\x1b[%d;%dR",
928 (origin() ? row
- top
: row
) + 1, col
+ 1);
929 term_sendstr(status
);
932 unknown("csiseq_dsr", c
);
937 /* ANSI/DEC specified modes for SM/RM ANSI Specified Modes */
938 static void modeseq(int c
, int set
)
941 case 0x87: /* DECAWM Auto Wrap */
942 mode
= BIT_SET(mode
, MODE_WRAP
, set
);
944 case 0x99: /* DECTCEM Cursor on (set); Cursor off (reset) */
945 mode
= BIT_SET(mode
, MODE_CURSOR
, set
);
947 case 0x86: /* DECOM Sets relative coordinates (set); Sets absolute coordinates (reset) */
948 mode
= BIT_SET(mode
, MODE_ORIGIN
, set
);
950 case 0x14: /* LNM Line Feed / New Line Mode */
951 mode
= BIT_SET(mode
, MODE_AUTOCR
, set
);
953 case 0x04: /* IRM insertion/replacement mode (always reset) */
955 case 0x00: /* IGN Error (Ignored) */
956 case 0x01: /* GATM guarded-area transfer mode (ignored) */
957 case 0x02: /* KAM keyboard action mode (always reset) */
958 case 0x03: /* CRM control representation mode (always reset) */
959 case 0x05: /* SRTM status-reporting transfer mode */
960 case 0x06: /* ERM erasure mode (always set) */
961 case 0x07: /* VEM vertical editing mode (ignored) */
962 case 0x0a: /* HEM horizontal editing mode */
963 case 0x0b: /* PUM positioning unit mode */
964 case 0x0c: /* SRM send/receive mode (echo on/off) */
965 case 0x0d: /* FEAM format effector action mode */
966 case 0x0e: /* FETM format effector transfer mode */
967 case 0x0f: /* MATM multiple area transfer mode */
968 case 0x10: /* TTM transfer termination mode */
969 case 0x11: /* SATM selected area transfer mode */
970 case 0x12: /* TSM tabulation stop mode */
971 case 0x13: /* EBM editing boundary mode */
972 /* DEC Private Modes: "?NUM" -> (NUM | 0x80) */
973 case 0x80: /* IGN Error (Ignored) */
974 case 0x81: /* DECCKM Cursorkeys application (set); Cursorkeys normal (reset) */
975 case 0x82: /* DECANM ANSI (set); VT52 (reset) */
976 case 0x83: /* DECCOLM 132 columns (set); 80 columns (reset) */
977 case 0x84: /* DECSCLM Jump scroll (set); Smooth scroll (reset) */
978 case 0x85: /* DECSCNM Reverse screen (set); Normal screen (reset) */
979 case 0x88: /* DECARM Auto Repeat */
980 case 0x89: /* DECINLM Interlace */
981 case 0x92: /* DECPFF Send FF to printer after print screen (set); No char after PS (reset) */
982 case 0x93: /* DECPEX Print screen: prints full screen (set); prints scroll region (reset) */
984 unknown("modeseq", c
);