10 #include <sys/ioctl.h>
16 #define MODE_CURSOR 0x01
17 #define MODE_WRAP 0x02
18 #define MODE_ORIGIN 0x04
19 #define MODE_AUTOCR 0x08
20 #define ATTR_BOLD 0x10
21 #define ATTR_ITALIC 0x20
23 #define ATTR_ALL (ATTR_BOLD | ATTR_ITALIC | ATTR_REV)
24 #define MODE_INSERT 0x100
25 #define MODE_WRAPREADY 0x200
26 #define MODE_CLR8 0x400 /* colours 0-7 */
28 #define CLR_MK(fg, bg) ((fg) | ((bg) << 10))
29 #define CLR_FG(c) ((c) & 0x3ff)
30 #define CLR_BG(c) (((c) >> 10) & 0x3ff)
31 #define CLR_M(c) ((c) & (CLR_B | CLR_I))
37 #define LIMIT(n, a, b) ((n) < (a) ? (a) : ((n) > (b) ? (b) : (n)))
38 #define BIT_SET(i, b, val) ((val) ? ((i) | (b)) : ((i) & ~(b)))
39 #define OFFSET(r, c) ((r) * pad_cols() + (c))
48 int *screen
; /* screen content */
49 int *hist
; /* scrolling history */
50 int *clr
; /* foreground/background color */
51 int *dirty
; /* changed rows in lazy mode */
52 struct term_state cur
, sav
; /* terminal saved state */
53 int fd
; /* terminal file descriptor */
54 int hrow
; /* the next history row in hist[] */
55 int hpos
; /* scrolling history; position */
56 int lazy
; /* lazy mode */
57 int pid
; /* pid of the terminal program */
58 int top
, bot
; /* terminal scrolling region */
60 int signal
; /* send SIGUSR1 and SIGUSR2 */
63 static struct term
*term
;
73 static int clrfg
= FGCOLOR
;
74 static int clrbg
= BGCOLOR
;
76 static unsigned int clr16
[16] = {
77 COLOR0
, COLOR1
, COLOR2
, COLOR3
, COLOR4
, COLOR5
, COLOR6
, COLOR7
,
78 COLOR8
, COLOR9
, COLORA
, COLORB
, COLORC
, COLORD
, COLORE
, COLORF
,
81 static int clrmap(int c
)
83 int g
= (c
- 232) * 10 + 8;
91 int ri
= (c
- 16) / 36 ;
92 int gi
= (c
- 16) % 36 / 6;
93 int bi
= (c
- 16) % 6;
94 int rc
= ri
? (ri
* 40 + 55) : 0;
95 int gc
= gi
? (gi
* 40 + 55) : 0;
96 int bc
= bi
? (bi
* 40 + 55) : 0;
97 return (rc
<< 16) | (gc
<< 8) | bc
;
99 return (g
<< 16) | (g
<< 8) | g
;
102 /* low level drawing and lazy updating */
104 static int color(void)
106 int c
= mode
& ATTR_REV
? CLR_MK(bg
, fg
) : CLR_MK(fg
, bg
);
107 if (mode
& ATTR_BOLD
)
109 if (mode
& ATTR_ITALIC
)
114 /* assumes visible && !lazy */
115 static void _draw_pos(int r
, int c
, int cursor
)
117 int rev
= cursor
&& mode
& MODE_CURSOR
;
118 int i
= OFFSET(r
, c
);
119 int fg
= rev
? CLR_BG(clr
[i
]) : CLR_FG(clr
[i
]);
120 int bg
= rev
? CLR_FG(clr
[i
]) : CLR_BG(clr
[i
]);
121 pad_put(screen
[i
], r
, c
, CLR_M(clr
[i
]) | clrmap(fg
), clrmap(bg
));
124 /* assumes visible && !lazy */
125 static void _draw_row(int r
)
127 int cbg
, cch
; /* current background and character */
128 int fbg
, fsc
= -1; /* filling background and start column */
130 /* call pad_fill() only once for blank columns with identical backgrounds */
131 for (i
= 0; i
< pad_cols(); i
++) {
132 cbg
= CLR_BG(clr
[OFFSET(r
, i
)]);
133 cch
= screen
[OFFSET(r
, i
)] ? screen
[OFFSET(r
, i
)] : ' ';
134 if (fsc
>= 0 && (cbg
!= fbg
|| cch
!= ' ')) {
135 pad_fill(r
, r
+ 1, fsc
, i
, clrmap(fbg
));
140 } else if (fsc
< 0) {
145 pad_fill(r
, r
+ 1, fsc
>= 0 ? fsc
: pad_cols(), -1, clrmap(cbg
));
148 static int candraw(int sr
, int er
)
152 for (i
= sr
; i
< er
; i
++)
154 return visible
&& !lazy
;
157 static void draw_rows(int sr
, int er
)
161 for (i
= sr
; i
< er
; i
++)
165 static void draw_cols(int r
, int sc
, int ec
)
168 if (candraw(r
, r
+ 1))
169 for (i
= sc
; i
< ec
; i
++)
173 static void draw_char(int ch
, int r
, int c
)
175 int i
= OFFSET(r
, c
);
178 if (candraw(r
, r
+ 1))
182 static void draw_cursor(int put
)
184 if (candraw(row
, row
+ 1))
185 _draw_pos(row
, col
, put
);
188 static void lazy_start(void)
190 memset(dirty
, 0, pad_rows() * sizeof(*dirty
));
194 static void lazy_flush(void)
197 if (!visible
|| !lazy
)
199 for (i
= 0; i
< pad_rows(); i
++)
203 _draw_pos(row
, col
, 1);
208 static void screen_reset(int i
, int n
)
211 candraw(i
/ pad_cols(), (i
+ n
) / pad_cols());
212 memset(screen
+ i
, 0, n
* sizeof(*screen
));
213 for (c
= 0; c
< n
; c
++)
214 clr
[i
+ c
] = CLR_MK(fg
, bg
);
217 static void screen_move(int dst
, int src
, int n
)
219 int srow
= (MIN(src
, dst
) + (n
> 0 ? 0 : n
)) / pad_cols();
220 int drow
= (MAX(src
, dst
) + (n
> 0 ? n
: 0)) / pad_cols();
222 memmove(screen
+ dst
, screen
+ src
, n
* sizeof(*screen
));
223 memmove(clr
+ dst
, clr
+ src
, n
* sizeof(*clr
));
226 /* terminal input buffering */
228 #define PTYLEN (1 << 16)
230 static char ptybuf
[PTYLEN
]; /* always emptied in term_read() */
231 static int ptylen
; /* buffer length */
232 static int ptycur
; /* current offset */
234 static int waitpty(int us
)
236 struct pollfd ufds
[1];
237 ufds
[0].fd
= term
->fd
;
238 ufds
[0].events
= POLLIN
;
239 return poll(ufds
, 1, us
) <= 0;
242 static int readpty(void)
246 return (unsigned char) ptybuf
[ptycur
++];
250 while ((nr
= read(term
->fd
, ptybuf
+ ptylen
, PTYLEN
- ptylen
)) > 0)
252 if (!ptylen
&& errno
== EAGAIN
&& !waitpty(100))
253 ptylen
= read(term
->fd
, ptybuf
, PTYLEN
);
255 return ptylen
> 0 ? (unsigned char) ptybuf
[0] : -1;
258 /* term interface functions */
260 static void term_zero(struct term
*term
)
262 memset(term
->screen
, 0, pad_rows() * pad_cols() * sizeof(term
->screen
[0]));
263 memset(term
->hist
, 0, NHIST
* pad_cols() * sizeof(term
->hist
[0]));
264 memset(term
->clr
, 0, pad_rows() * pad_cols() * sizeof(term
->clr
[0]));
265 memset(term
->dirty
, 0, pad_rows() * sizeof(term
->dirty
[0]));
266 memset(&term
->cur
, 0, sizeof(term
->cur
));
267 memset(&term
->sav
, 0, sizeof(term
->sav
));
280 struct term
*term_make(void)
282 struct term
*term
= malloc(sizeof(*term
));
283 term
->screen
= malloc(pad_rows() * pad_cols() * sizeof(term
->screen
[0]));
284 term
->hist
= malloc(NHIST
* pad_cols() * sizeof(term
->hist
[0]));
285 term
->clr
= malloc(pad_rows() * pad_cols() * sizeof(term
->clr
[0]));
286 term
->dirty
= malloc(pad_rows() * sizeof(term
->dirty
[0]));
291 void term_free(struct term
*term
)
300 int term_fd(struct term
*term
)
305 void term_send(int c
)
309 write(term
->fd
, &b
, 1);
312 static void term_sendstr(char *s
)
315 write(term
->fd
, s
, strlen(s
));
318 static void term_blank(void)
320 screen_reset(0, pad_rows() * pad_cols());
322 pad_fill(0, -1, 0, -1, clrmap(CLR_BG(color())));
325 static void ctlseq(void);
329 while (ptycur
< ptylen
) {
330 if (visible
&& !lazy
&& ptylen
- ptycur
> 15)
337 static void term_reset(void)
342 mode
= MODE_CURSOR
| MODE_WRAP
| MODE_CLR8
;
348 static int _openpty(int *master
, int *slave
)
353 if ((*master
= open("/dev/ptmx", O_RDWR
)) == -1)
355 if (ioctl(*master
, TIOCSPTLCK
, &unlock
) == -1)
357 if (ioctl(*master
, TIOCGPTN
, &ptyno
) == -1)
359 sprintf(name
, "/dev/pts/%d", ptyno
);
360 *slave
= open(name
, O_RDWR
| O_NOCTTY
);
364 static void tio_setsize(int fd
)
367 winp
.ws_col
= pad_cols();
368 winp
.ws_row
= pad_rows();
371 ioctl(fd
, TIOCSWINSZ
, &winp
);
374 static void tio_login(int fd
)
377 ioctl(fd
, TIOCSCTTY
, NULL
);
386 static void execvep(char *cmd
, char **argv
, char **envp
)
389 char *p
= getenv("PATH");
390 execve(cmd
, argv
, envp
);
393 while (*p
&& *p
!= ':')
397 execve(path
, argv
, envp
);
403 static void envcpy(char **d
, char **s
, int len
)
406 for (i
= 0; i
< len
- 1 && s
[i
]; i
++)
411 static void envset(char **d
, char *env
)
414 int len
= strchr(env
, '=') - env
;
415 for (i
= 0; d
[i
]; i
++) {
416 if (memcmp(d
[i
], env
, len
))
422 extern char **environ
;
423 void term_exec(char **args
, int swsig
)
427 if (_openpty(&master
, &slave
) == -1)
429 if ((term
->pid
= fork()) == -1)
432 char *envp
[256] = {NULL
};
434 snprintf(pgid
, sizeof(pgid
), "TERM_PGID=%d", getpid());
435 envcpy(envp
, environ
, LEN(envp
) - 3);
436 envset(envp
, "TERM=" TERM
);
437 envset(envp
, pad_fbdev());
442 execvep(args
[0], args
, envp
);
447 term
->rows
= pad_rows();
448 term
->cols
= pad_cols();
449 term
->signal
= swsig
;
450 fcntl(term
->fd
, F_SETFD
, fcntl(term
->fd
, F_GETFD
) | FD_CLOEXEC
);
451 fcntl(term
->fd
, F_SETFL
, fcntl(term
->fd
, F_GETFL
) | O_NONBLOCK
);
453 memset(term
->hist
, 0, NHIST
* pad_cols() * sizeof(term
->hist
[0]));
456 static void misc_save(struct term_state
*state
)
465 static void misc_load(struct term_state
*state
)
474 void term_save(struct term
*term
)
479 misc_save(&term
->cur
);
485 void term_hide(struct term
*term
)
487 if (term
->pid
> 0 && term
->signal
)
488 kill(-term
->pid
, SIGUSR1
);
491 void term_show(struct term
*term
)
493 if (term
->pid
> 0 && term
->signal
)
494 kill(-term
->pid
, SIGUSR2
);
497 void term_signal(struct term
*term
)
502 static void resizeupdate(int or, int oc
, int nr
, int nc
)
504 int dr
= row
>= nr
? row
- nr
+ 1 : 0;
505 int dst
= nc
<= oc
? 0 : nr
* nc
- 1;
506 while (dst
>= 0 && dst
< nr
* nc
) {
509 int src
= dr
+ r
< or && c
< oc
? (dr
+ r
) * oc
+ c
: -1;
510 term
->screen
[dst
] = src
>= 0 ? term
->screen
[src
] : 0;
511 term
->clr
[dst
] = src
>= 0 ? term
->clr
[src
] : color();
512 dst
= nc
<= oc
? dst
+ 1 : dst
- 1;
516 /* redraw the screen; if all is zero, update changed lines only */
517 void term_redraw(int all
)
520 if (term
->rows
!= pad_rows() || term
->cols
!= pad_cols()) {
521 tio_setsize(term
->fd
);
522 resizeupdate(term
->rows
, term
->cols
, pad_rows(), pad_cols());
523 if (bot
== term
->rows
)
525 term
->rows
= pad_rows();
526 term
->cols
= pad_cols();
527 top
= MIN(top
, term
->rows
);
528 bot
= MIN(bot
, term
->rows
);
529 row
= MIN(row
, term
->rows
- 1);
530 col
= MIN(col
, term
->cols
- 1);
533 pad_fill(pad_rows(), -1, 0, -1, clrbg
);
535 memset(dirty
, 1, pad_rows() * sizeof(*dirty
));
537 if (all
|| !term
->hpos
)
541 pad_fill(0, -1, 0, -1, 0);
545 void term_load(struct term
*t
, int flags
)
548 misc_load(&term
->cur
);
549 screen
= term
->screen
;
563 term_load(term
, visible
);
568 static int writeutf8(char *dst
, int c
)
572 *d
++ = c
> 0 ? c
: ' ';
576 *d
++ = 0xc0 | (c
>> 6);
577 *d
++ = 0x80 | (c
& 0x3f);
581 *d
++ = 0xe0 | (c
>> 12);
582 *d
++ = 0x80 | ((c
>> 6) & 0x3f);
583 *d
++ = 0x80 | (c
& 0x3f);
586 *d
++ = 0xf0 | (c
>> 18);
587 *d
++ = 0x80 | ((c
>> 12) & 0x3f);
588 *d
++ = 0x80 | ((c
>> 6) & 0x3f);
589 *d
++ = 0x80 | (c
& 0x3f);
593 void term_screenshot(char *path
)
596 int fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
598 for (i
= 0; i
< pad_rows(); i
++) {
600 for (j
= 0; j
< pad_cols(); j
++)
601 if (~screen
[OFFSET(i
, j
)] & DWCHAR
)
602 s
+= writeutf8(s
, screen
[OFFSET(i
, j
)]);
604 write(fd
, buf
, s
- buf
);
609 void term_colors(char *path
)
611 FILE *fp
= fopen(path
, "r");
614 fscanf(fp
, "%x %x", &clrfg
, &clrbg
);
615 for (i
= 0; i
< 16; i
++)
616 fscanf(fp
, "%x", &clr16
[i
]);
621 /* high-level drawing functions */
623 static void empty_rows(int sr
, int er
)
625 screen_reset(OFFSET(sr
, 0), (er
- sr
) * pad_cols());
628 static void blank_rows(int sr
, int er
)
635 #define HISTROW(pos) (term->hist + ((term->hrow + NHIST - (pos)) % NHIST) * pad_cols())
637 static void scrl_rows(int nr
)
640 for (i
= 0; i
< nr
; i
++) {
641 memcpy(HISTROW(0), screen
+ i
* pad_cols(),
642 pad_cols() * sizeof(screen
[0]));
643 term
->hrow
= (term
->hrow
+ 1) % NHIST
;
647 void term_scrl(int scrl
)
650 int hpos
= LIMIT(term
->hpos
+ scrl
, 0, NHIST
);
657 memset(dirty
, 1, pad_rows() * sizeof(*dirty
));
658 for (i
= 0; i
< pad_rows(); i
++) {
659 int off
= (i
- hpos
) * pad_cols();
660 int *_scr
= i
< hpos
? HISTROW(hpos
- i
) : term
->screen
+ off
;
661 int *_clr
= i
< hpos
? NULL
: term
->clr
+ off
;
662 for (j
= 0; j
< pad_cols(); j
++) {
663 int c
= _clr
? _clr
[j
] : CLR_MK(BG
, FG
);
664 pad_put(_scr
[j
], i
, j
, CLR_M(c
) | clrmap(CLR_FG(c
)), clrmap(CLR_BG(c
)));
669 static void scroll_screen(int sr
, int nr
, int n
)
674 screen_move(OFFSET(sr
+ n
, 0), OFFSET(sr
, 0), nr
* pad_cols());
676 empty_rows(sr
, sr
+ n
);
678 empty_rows(sr
+ nr
+ n
, sr
+ nr
);
679 draw_rows(MIN(sr
, sr
+ n
), MAX(sr
+ nr
, sr
+ nr
+ n
));
683 static void insert_lines(int n
)
685 int sr
= MAX(top
, row
);
686 int nr
= bot
- row
- n
;
688 scroll_screen(sr
, nr
, n
);
691 static void delete_lines(int n
)
693 int r
= MAX(top
, row
);
695 int nr
= bot
- r
- n
;
697 scroll_screen(sr
, nr
, -n
);
700 static int origin(void)
702 return mode
& MODE_ORIGIN
;
705 static void move_cursor(int r
, int c
)
709 t
= origin() ? top
: 0;
710 b
= origin() ? bot
: pad_rows();
711 row
= LIMIT(r
, t
, b
- 1);
712 col
= LIMIT(c
, 0, pad_cols() - 1);
714 mode
= BIT_SET(mode
, MODE_WRAPREADY
, 0);
717 static void set_region(int t
, int b
)
719 top
= LIMIT(t
- 1, 0, pad_rows() - 1);
720 bot
= LIMIT(b
? b
: pad_rows(), top
+ 1, pad_rows());
725 static void setattr(int m
)
727 if (!m
|| (m
/ 10) == 3)
748 mode
&= ~ATTR_ITALIC
;
755 fg
= m
> 37 ? FG
: m
- 30;
757 bg
= m
> 47 ? BG
: m
- 40;
765 static void kill_chars(int sc
, int ec
)
768 for (i
= sc
; i
< ec
; i
++)
769 draw_char(0, row
, i
);
773 static void move_chars(int sc
, int nc
, int n
)
776 screen_move(OFFSET(row
, sc
+ n
), OFFSET(row
, sc
), nc
);
778 screen_reset(OFFSET(row
, sc
), n
);
780 screen_reset(OFFSET(row
, pad_cols() + n
), -n
);
781 draw_cols(row
, MIN(sc
, sc
+ n
), pad_cols());
785 static void delete_chars(int n
)
788 int nc
= pad_cols() - sc
;
789 move_chars(sc
, nc
, -n
);
792 static void insert_chars(int n
)
794 int nc
= pad_cols() - col
- n
;
795 move_chars(col
, nc
, n
);
798 static void advance(int dr
, int dc
, int scrl
)
802 if (dr
&& r
>= bot
&& scrl
) {
804 int nr
= (bot
- top
) + n
;
806 scroll_screen(top
+ -n
, nr
, n
);
808 if (dr
&& r
< top
&& scrl
) {
810 int nr
= (bot
- top
) - n
;
812 scroll_screen(top
, nr
, n
);
814 r
= dr
? LIMIT(r
, top
, bot
- 1) : r
;
815 c
= LIMIT(c
, 0, pad_cols() - 1);
819 static void insertchar(int c
)
821 if (mode
& MODE_WRAPREADY
)
823 if (mode
& MODE_INSERT
)
825 draw_char(c
, row
, col
);
826 if (col
== pad_cols() - 1)
827 mode
= BIT_SET(mode
, MODE_WRAPREADY
, 1);
833 /* partial vt102 implementation */
835 static void escseq(void);
836 static void escseq_cs(void);
837 static void escseq_g0(void);
838 static void escseq_g1(void);
839 static void escseq_g2(void);
840 static void escseq_g3(void);
841 static void csiseq(void);
842 static void csiseq_da(int c
);
843 static void csiseq_dsr(int c
);
844 static void modeseq(int c
, int set
);
846 /* comments taken from: http://www.ivarch.com/programs/termvt102.shtml */
848 static int readutf8(int c
)
851 if (~c
& 0xc0) /* ASCII or invalid */
855 return ((c
& 0x1f) << 6) | (c1
& 0x3f);
858 return ((c
& 0x0f) << 12) | ((c1
& 0x3f) << 6) | (c2
& 0x3f);
861 return ((c
& 0x07) << 18) | ((c1
& 0x3f) << 12) | ((c2
& 0x3f) << 6) | (c3
& 0x3f);
865 #define unknown(ctl, c)
867 /* control sequences */
868 static void ctlseq(void)
872 case 0x09: /* HT horizontal tab to next tab stop */
873 advance(0, 8 - col
% 8, 0);
875 case 0x0a: /* LF line feed */
876 case 0x0b: /* VT line feed */
877 case 0x0c: /* FF line feed */
878 advance(1, (mode
& MODE_AUTOCR
) ? -col
: 0, 1);
880 case 0x08: /* BS backspace one column */
883 case 0x1b: /* ESC start escape sequence */
886 case 0x0d: /* CR carriage return */
889 case 0x9b: /* CSI equivalent to ESC [ */
892 case 0x00: /* NUL ignored */
893 case 0x07: /* BEL beep */
894 case 0x7f: /* DEL ignored */
896 case 0x05: /* ENQ trigger answerback message */
897 case 0x0e: /* SO activate G1 character set & newline */
898 case 0x0f: /* SI activate G0 character set */
899 case 0x11: /* XON resume transmission */
900 case 0x13: /* XOFF stop transmission, ignore characters */
901 case 0x18: /* CAN interrupt escape sequence */
902 case 0x1a: /* SUB interrupt escape sequence */
903 unknown("ctlseq", c
);
907 if (isdw(c
) && col
+ 1 == pad_cols() && ~mode
& MODE_WRAPREADY
)
912 insertchar(c
| DWCHAR
);
917 #define ESCM(c) (((c) & 0xf0) == 0x20)
918 #define ESCF(c) ((c) > 0x30 && (c) < 0x7f)
920 /* escape sequences */
921 static void escseq(void)
927 case '[': /* CSI control sequence introducer */
930 case '%': /* CS... escseq_cs table */
933 case '(': /* G0... escseq_g0 table */
936 case ')': /* G1... escseq_g1 table */
939 case '*': /* G2... escseq_g2 table */
942 case '+': /* G3... escseq_g3 table */
945 case '7': /* DECSC save state (position, charset, attributes) */
946 misc_save(&term
->sav
);
948 case '8': /* DECRC restore most recently saved state */
949 misc_load(&term
->sav
);
951 case 'M': /* RI reverse line feed */
954 case 'D': /* IND line feed */
957 case 'E': /* NEL newline */
960 case 'c': /* RIS reset */
963 case 'H': /* HTS set tab stop at current column */
964 case 'Z': /* DECID DEC private ID; return ESC [ ? 6 c (VT102) */
965 case '#': /* DECALN ("#8") DEC alignment test - fill screen with E's */
966 case '>': /* DECPNM set numeric keypad mode */
967 case '=': /* DECPAM set application keypad mode */
968 case 'N': /* SS2 select G2 charset for next char only */
969 case 'O': /* SS3 select G3 charset for next char only */
970 case 'P': /* DCS device control string (ended by ST) */
971 case 'X': /* SOS start of string */
972 case '^': /* PM privacy message (ended by ST) */
973 case '_': /* APC application program command (ended by ST) */
974 case '\\': /* ST string terminator */
975 case 'n': /* LS2 invoke G2 charset */
976 case 'o': /* LS3 invoke G3 charset */
977 case '|': /* LS3R invoke G3 charset as GR */
978 case '}': /* LS2R invoke G2 charset as GR */
979 case '~': /* LS1R invoke G1 charset as GR */
980 case ']': /* OSC operating system command */
981 case 'g': /* BEL alternate BEL */
983 unknown("escseq", c
);
988 static void escseq_cs(void)
992 case '@': /* CSDFL select default charset (ISO646/8859-1) */
993 case 'G': /* CSUTF8 select UTF-8 */
994 case '8': /* CSUTF8 select UTF-8 (obsolete) */
996 unknown("escseq_cs", c
);
1001 static void escseq_g0(void)
1005 case '8': /* G0DFL G0 charset = default mapping (ISO8859-1) */
1006 case '0': /* G0GFX G0 charset = VT100 graphics mapping */
1007 case 'U': /* G0ROM G0 charset = null mapping (straight to ROM) */
1008 case 'K': /* G0USR G0 charset = user defined mapping */
1009 case 'B': /* G0TXT G0 charset = ASCII mapping */
1011 unknown("escseq_g0", c
);
1016 static void escseq_g1(void)
1020 case '8': /* G1DFL G1 charset = default mapping (ISO8859-1) */
1021 case '0': /* G1GFX G1 charset = VT100 graphics mapping */
1022 case 'U': /* G1ROM G1 charset = null mapping (straight to ROM) */
1023 case 'K': /* G1USR G1 charset = user defined mapping */
1024 case 'B': /* G1TXT G1 charset = ASCII mapping */
1026 unknown("escseq_g1", c
);
1031 static void escseq_g2(void)
1035 case '8': /* G2DFL G2 charset = default mapping (ISO8859-1) */
1036 case '0': /* G2GFX G2 charset = VT100 graphics mapping */
1037 case 'U': /* G2ROM G2 charset = null mapping (straight to ROM) */
1038 case 'K': /* G2USR G2 charset = user defined mapping */
1040 unknown("escseq_g2", c
);
1045 static void escseq_g3(void)
1049 case '8': /* G3DFL G3 charset = default mapping (ISO8859-1) */
1050 case '0': /* G3GFX G3 charset = VT100 graphics mapping */
1051 case 'U': /* G3ROM G3 charset = null mapping (straight to ROM) */
1052 case 'K': /* G3USR G3 charset = user defined mapping */
1054 unknown("escseq_g3", c
);
1059 static int absrow(int r
)
1061 return origin() ? top
+ r
: r
;
1064 #define CSIP(c) (((c) & 0xf0) == 0x30)
1065 #define CSII(c) (((c) & 0xf0) == 0x20)
1066 #define CSIF(c) ((c) >= 0x40 && (c) < 0x80)
1068 #define MAXCSIARGS 32
1069 /* ECMA-48 CSI sequences */
1070 static void csiseq(void)
1072 int args
[MAXCSIARGS
+ 8] = {0};
1078 if (strchr("<=>?", c
)) {
1084 while (isdigit(c
)) {
1085 arg
= arg
* 10 + (c
- '0');
1096 case 'H': /* CUP move cursor to row, column */
1097 case 'f': /* HVP move cursor to row, column */
1098 move_cursor(absrow(MAX(0, args
[0] - 1)), MAX(0, args
[1] - 1));
1100 case 'J': /* ED erase display */
1103 kill_chars(col
, pad_cols());
1104 blank_rows(row
+ 1, pad_rows());
1107 kill_chars(0, col
+ 1);
1108 blank_rows(0, row
- 1);
1115 case 'A': /* CUU move cursor up */
1116 advance(-MAX(1, args
[0]), 0, 0);
1118 case 'e': /* VPR move cursor down */
1119 case 'B': /* CUD move cursor down */
1120 advance(MAX(1, args
[0]), 0, 0);
1122 case 'a': /* HPR move cursor right */
1123 case 'C': /* CUF move cursor right */
1124 advance(0, MAX(1, args
[0]), 0);
1126 case 'D': /* CUB move cursor left */
1127 advance(0, -MAX(1, args
[0]), 0);
1129 case 'K': /* EL erase line */
1132 kill_chars(col
, pad_cols());
1135 kill_chars(0, col
+ 1);
1138 kill_chars(0, pad_cols());
1142 case 'L': /* IL insert blank lines */
1143 if (row
>= top
&& row
< bot
)
1144 insert_lines(MAX(1, args
[0]));
1146 case 'M': /* DL delete lines */
1147 if (row
>= top
&& row
< bot
)
1148 delete_lines(MAX(1, args
[0]));
1150 case 'S': /* SU scroll up */
1151 i
= MAX(1, args
[0]);
1152 scroll_screen(i
, pad_rows() - i
, -i
);
1154 case 'T': /* SD scroll down */
1155 i
= MAX(1, args
[0]);
1156 scroll_screen(0, pad_rows() - i
, i
);
1158 case 'd': /* VPA move to row (current column) */
1159 move_cursor(absrow(MAX(1, args
[0]) - 1), col
);
1161 case 'm': /* SGR set graphic rendition */
1164 for (i
= 0; i
< n
; i
++) {
1165 if (args
[i
] == 38 && args
[i
+ 1] == 2) {
1167 fg
= (args
[i
+ 2] << 16) |
1168 (args
[i
+ 3] << 8) | args
[i
+ 4];
1172 if (args
[i
] == 38) {
1178 if (args
[i
] == 48 && args
[i
+ 1] == 2) {
1179 bg
= (args
[i
+ 2] << 16) |
1180 (args
[i
+ 3] << 8) | args
[i
+ 4];
1184 if (args
[i
] == 48) {
1191 if (mode
& MODE_CLR8
&& mode
& ATTR_BOLD
&& BRIGHTEN
)
1192 for (i
= 0; i
< 8; i
++)
1196 case 'r': /* DECSTBM set scrolling region to (top, bottom) rows */
1197 set_region(args
[0], args
[1]);
1199 case 'c': /* DA return ESC [ ? 6 c (VT102) */
1200 csiseq_da(priv
== '?' ? args
[0] | 0x80 : args
[0]);
1202 case 'h': /* SM set mode */
1203 for (i
= 0; i
< n
; i
++)
1204 modeseq(priv
== '?' ? args
[i
] | 0x80 : args
[i
], 1);
1207 case 'l': /* RM reset mode */
1208 for (i
= 0; i
< n
; i
++)
1209 modeseq(priv
== '?' ? args
[i
] | 0x80 : args
[i
], 0);
1212 case 'P': /* DCH delete characters on current line */
1213 delete_chars(LIMIT(args
[0], 1, pad_cols() - col
));
1215 case '@': /* ICH insert blank characters */
1216 insert_chars(LIMIT(args
[0], 1, pad_cols() - col
));
1218 case 'n': /* DSR device status report */
1219 csiseq_dsr(args
[0]);
1221 case 'G': /* CHA move cursor to column in current row */
1222 advance(0, MAX(0, args
[0] - 1) - col
, 0);
1224 case 'X': /* ECH erase characters on current line */
1225 kill_chars(col
, MIN(col
+ MAX(1, args
[0]), pad_cols()));
1227 case '[': /* IGN ignored control sequence */
1228 case 'E': /* CNL move cursor down and to column 1 */
1229 case 'F': /* CPL move cursor up and to column 1 */
1230 case 'g': /* TBC clear tab stop (CSI 3 g = clear all stops) */
1231 case 'q': /* DECLL set keyboard LEDs */
1232 case 's': /* CUPSV save cursor position */
1233 case 'u': /* CUPRS restore cursor position */
1234 case '`': /* HPA move cursor to column in current row */
1236 unknown("csiseq", c
);
1241 static void csiseq_da(int c
)
1245 term_sendstr("\x1b[?6c");
1248 /* we don't care much about cursor shape */
1249 /* printf("csiseq_da <0x%x>\n", c); */
1254 static void csiseq_dsr(int c
)
1256 char status
[1 << 5];
1259 term_sendstr("\x1b[0n");
1262 sprintf(status
, "\x1b[%d;%dR",
1263 (origin() ? row
- top
: row
) + 1, col
+ 1);
1264 term_sendstr(status
);
1267 unknown("csiseq_dsr", c
);
1272 /* ANSI/DEC specified modes for SM/RM ANSI Specified Modes */
1273 static void modeseq(int c
, int set
)
1276 case 0x87: /* DECAWM Auto Wrap */
1277 mode
= BIT_SET(mode
, MODE_WRAP
, set
);
1279 case 0x99: /* DECTCEM Cursor on (set); Cursor off (reset) */
1280 mode
= BIT_SET(mode
, MODE_CURSOR
, set
);
1282 case 0x86: /* DECOM Sets relative coordinates (set); Sets absolute coordinates (reset) */
1283 mode
= BIT_SET(mode
, MODE_ORIGIN
, set
);
1285 case 0x14: /* LNM Line Feed / New Line Mode */
1286 mode
= BIT_SET(mode
, MODE_AUTOCR
, set
);
1288 case 0x04: /* IRM insertion/replacement mode (always reset) */
1289 mode
= BIT_SET(mode
, MODE_INSERT
, set
);
1291 case 0x00: /* IGN error (ignored) */
1292 case 0x01: /* GATM guarded-area transfer mode (ignored) */
1293 case 0x02: /* KAM keyboard action mode (always reset) */
1294 case 0x03: /* CRM control representation mode (always reset) */
1295 case 0x05: /* SRTM status-reporting transfer mode */
1296 case 0x06: /* ERM erasure mode (always set) */
1297 case 0x07: /* VEM vertical editing mode (ignored) */
1298 case 0x08: /* BDSM bi-directional support mode */
1299 case 0x0a: /* HEM horizontal editing mode */
1300 case 0x0b: /* PUM positioning unit mode */
1301 case 0x0c: /* SRM send/receive mode (echo on/off) */
1302 case 0x0d: /* FEAM format effector action mode */
1303 case 0x0e: /* FETM format effector transfer mode */
1304 case 0x0f: /* MATM multiple area transfer mode */
1305 case 0x10: /* TTM transfer termination mode */
1306 case 0x11: /* SATM selected area transfer mode */
1307 case 0x12: /* TSM tabulation stop mode */
1308 case 0x13: /* EBM editing boundary mode */
1309 /* DEC Private Modes: "?NUM" -> (NUM | 0x80) */
1310 case 0x80: /* IGN error (ignored) */
1311 case 0x81: /* DECCKM cursorkeys application (set); cursorkeys normal (reset) */
1312 case 0x82: /* DECANM ANSI (set); VT52 (reset) */
1313 case 0x83: /* DECCOLM 132 columns (set); 80 columns (reset) */
1314 case 0x84: /* DECSCLM jump scroll (set); smooth scroll (reset) */
1315 case 0x85: /* DECSCNM reverse screen (set); normal screen (reset) */
1316 case 0x88: /* DECARM auto repeat */
1317 case 0x89: /* DECINLM interlace */
1318 case 0x92: /* DECPFF send FF to printer after print screen (set); no char after PS (reset) */
1319 case 0x93: /* DECPEX print screen: prints full screen (set); prints scroll region (reset) */
1321 unknown("modeseq", c
);