term: handle invalid values for scrolling region
[fbpad.git] / term.c
blob6f5460e89bfabcb4af1c680ef078ef6b76493132
1 #include <ctype.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <poll.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/ioctl.h>
10 #include <termios.h>
11 #include <unistd.h>
12 #include "config.h"
13 #include "pad.h"
14 #include "util.h"
15 #include "term.h"
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
23 #define ATTR_REV 0x20
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;
31 static int *screen;
32 static char *fgs, *bgs;
33 static int row, col;
34 static char fg, bg;
35 static int top, bot;
36 static int mode;
37 static int visible;
39 #define MAXLINES (1 << 8)
40 static int dirty[MAXLINES];
41 static int lazy;
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;
57 int i = OFFSET(r, c);
58 char fg = rev ? bgs[i] : fgs[i];
59 char bg = rev ? fgs[i] : bgs[i];
60 if (visible)
61 pad_put(screen[i], r, c, fg, bg);
64 static void _draw_row(int r)
66 int i;
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())
71 _draw_pos(r, i, 0);
75 static void draw_rows(int sr, int er)
77 int i;
78 if (!visible)
79 return;
80 for (i = sr; i < er; i++) {
81 if (lazy)
82 dirty[i] = 1;
83 else
84 _draw_row(i);
88 static void draw_cols(int r, int sc, int ec)
90 int i;
91 if (!visible)
92 return;
93 if (lazy)
94 dirty[r] = 1;
95 else
96 for (i = sc; i < ec; i++)
97 _draw_pos(r, i, 0);
100 static void draw_char(int ch, int r, int c)
102 int i = OFFSET(r, c);
103 screen[i] = ch;
104 fgs[i] = fgcolor();
105 bgs[i] = bgcolor();
106 if (!visible)
107 return;
108 if (lazy)
109 dirty[r] = 1;
110 else
111 _draw_pos(r, c, 0);
114 static void draw_cursor(int put)
116 if (!visible)
117 return;
118 if (lazy)
119 dirty[row] = 1;
120 else
121 _draw_pos(row, col, put);
124 static void lazy_clean(void)
126 if (visible)
127 memset(dirty, 0, sizeof(dirty));
130 static void lazy_flush(void)
132 int i;
133 if (!visible)
134 return;
135 _draw_pos(row, col, 0);
136 for (i = 0; i < pad_rows(); i++)
137 if (dirty[i])
138 _draw_row(i);
139 lazy_clean();
140 _draw_pos(row, col, 1);
143 static void term_redraw(void)
145 int i;
146 for (i = 0; i < pad_rows(); i++)
147 _draw_row(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];
158 static int ptylen;
159 static int ptycur;
160 static void waitpty(void)
162 struct pollfd ufds[1];
163 ufds[0].fd = term->fd;
164 ufds[0].events = POLLIN;
165 poll(ufds, 1, 100);
168 static int readpty(void)
170 if (ptycur < ptylen)
171 return ptybuf[ptycur++];
172 if (!term->fd)
173 return -1;
174 ptylen = read(term->fd, ptybuf, PTYBUFSIZE);
175 if (ptylen == -1 && errno == EAGAIN) {
176 waitpty();
177 ptylen = read(term->fd, ptybuf, PTYBUFSIZE);
179 if (ptylen > 0) {
180 ptycur = 1;
181 return ptybuf[0];
183 return -1;
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)
207 empty_rows(sr, er);
208 draw_rows(sr, er);
209 draw_cursor(1);
212 static void scroll_screen(int sr, int nr, int n)
214 draw_cursor(0);
215 screen_move(OFFSET(sr + n, 0), OFFSET(sr, 0), nr * pad_cols());
216 if (n > 0)
217 empty_rows(sr, sr + n);
218 else
219 empty_rows(sr + nr + n, sr + nr);
220 draw_rows(MIN(sr, sr + n), MAX(sr + nr, sr + nr + n));
221 draw_cursor(1);
224 static void insert_lines(int n)
226 int sr = MAX(top, row);
227 int nr = bot - row - n;
228 if (nr > 0)
229 scroll_screen(sr, nr, n);
232 static void delete_lines(int n)
234 int r = MAX(top, row);
235 int sr = r + n;
236 int nr = bot - r - n;
237 if (nr > 0)
238 scroll_screen(sr, nr, -n);
241 static void move_cursor(int r, int c)
243 int t, b;
244 draw_cursor(0);
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));
249 draw_cursor(1);
250 mode = BIT_SET(mode, MODE_WRAPREADY, 0);
253 static void advance(int dr, int dc, int scrl)
255 int r = row + dr;
256 int c = col + dc;
257 if (r >= bot && scrl) {
258 int n = bot - r - 1;
259 int nr = (bot - top) + n;
260 scroll_screen(top + -n, nr, n);
262 if (r < top && scrl) {
263 int n = top - r;
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));
269 move_cursor(r, c);
272 static void insertchar(int c)
274 int wrapready;
275 if (mode & MODE_WRAPREADY)
276 advance(1, -col, 1);
277 draw_char(c, row, col);
278 wrapready = col == pad_cols() - 1;
279 advance(0, 1, 1);
280 if (wrapready)
281 mode = BIT_SET(mode, MODE_WRAPREADY, 1);
284 void term_send(int c)
286 char b = c;
287 if (term->fd)
288 write(term->fd, &b, 1);
291 static void term_sendstr(char *s)
293 if (term->fd)
294 write(term->fd, s, strlen(s));
297 static void setattr(int m)
299 switch (m) {
300 case 0:
301 fg = FGCOLOR;
302 bg = BGCOLOR;
303 mode &= ~ATTR_ALL;
304 break;
305 case 1:
306 mode |= ATTR_BOLD;
307 break;
308 case 7:
309 mode |= ATTR_REV;
310 break;
311 case 22:
312 mode &= ~ATTR_BOLD;
313 break;
314 case 27:
315 mode &= ~ATTR_REV;
316 break;
317 default:
318 if ((m / 10) == 3)
319 fg = m > 37 ? FGCOLOR : m - 30;
320 if ((m / 10) == 4)
321 bg = m > 47 ? BGCOLOR : m - 40;
325 static void kill_chars(int sc, int ec)
327 int i;
328 for (i = sc; i < ec; i++)
329 draw_char(' ', row, i);
330 draw_cursor(1);
333 static void move_chars(int sc, int nc, int n)
335 draw_cursor(0);
336 screen_move(OFFSET(row, sc + n), OFFSET(row, sc), nc);
337 if (n > 0)
338 screen_reset(OFFSET(row, sc), n);
339 else
340 screen_reset(OFFSET(row, pad_cols() + n), -n);
341 draw_cols(row, MIN(sc, sc + n), pad_cols());
342 draw_cursor(1);
345 static void delete_chars(int n)
347 int sc = col + 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());
361 if (visible) {
362 pad_blank(bgcolor());
363 lazy_clean();
367 static void ctlseq(void);
368 void term_read(void)
370 ctlseq();
371 while (ptycur < ptylen) {
372 if (ptylen - ptycur > 15)
373 lazy = 1;
374 ctlseq();
376 if (lazy)
377 lazy_flush();
378 lazy = 0;
381 static void term_reset(void)
383 row = col = 0;
384 top = 0;
385 bot = pad_rows();
386 mode = MODE_DEFAULT;
387 setattr(0);
388 term_blank();
391 static int _openpty(int *master, int *slave)
393 int unlock = 0;
394 int ptyno = 0;
395 char name[20];
396 if ((*master = open("/dev/ptmx", O_RDWR)) == -1)
397 return -1;
398 if (ioctl(*master, TIOCSPTLCK, &unlock) == -1)
399 return -1;
400 if (ioctl(*master, TIOCGPTN, &ptyno) == -1)
401 return -1;
402 sprintf(name, "/dev/pts/%d", ptyno);
403 *slave = open(name, O_RDWR | O_NOCTTY);
404 return 0;
407 static void _login(int fd)
409 static char env[] = "TERM=linux";
410 struct winsize winp;
411 winp.ws_col = pad_cols();
412 winp.ws_row = pad_rows();
413 winp.ws_xpixel = 0;
414 winp.ws_ypixel = 0;
415 setsid();
416 ioctl(fd, TIOCSCTTY, NULL);
417 ioctl(fd, TIOCSWINSZ, &winp);
418 dup2(fd, 0);
419 dup2(fd, 1);
420 dup2(fd, 2);
421 if (fd > 2)
422 close(fd);
423 putenv(env);
426 void term_exec(char *cmd)
428 int master, slave;
429 memset(term, 0, sizeof(*term));
430 if (_openpty(&master, &slave) == -1)
431 return;
432 if ((term->pid = fork()) == -1)
433 return;
434 if (!term->pid) {
435 _login(slave);
436 close(master);
437 execlp(cmd, cmd, NULL);
438 exit(1);
440 close(slave);
441 term->fd = master;
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);
444 term_reset();
447 static void misc_save(struct term_state *state)
449 state->row = row;
450 state->col = col;
451 state->fg = fg;
452 state->bg = bg;
453 state->mode = mode;
456 static void misc_load(struct term_state *state)
458 row = state->row;
459 col = state->col;
460 fg = state->fg;
461 bg = state->bg;
462 mode = state->mode;
465 void term_save(struct term *term)
467 visible = 0;
468 misc_save(&term->cur);
469 term->top = top;
470 term->bot = bot;
473 void term_load(struct term *t, int flags)
475 term = t;
476 misc_load(&term->cur);
477 screen = term->screen;
478 fgs = term->fgs;
479 bgs = term->bgs;
480 visible = flags;
481 top = term->top;
482 bot = term->bot;
483 if (flags == TERM_REDRAW) {
484 if (term->fd)
485 term_redraw();
486 else
487 pad_blank(0);
491 void term_end(void)
493 if (term->fd)
494 close(term->fd);
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()));
503 if (origin())
504 move_cursor(top, 0);
507 static int writeutf8(char *dst, int c)
509 char *d = dst;
510 int l;
511 if (!(c & ~0x7f)) {
512 *d++ = c ? c : ' ';
513 return 1;
515 if (c > 0xffff) {
516 *d++ = 0xf0 | (c >> 18);
517 l = 3;
518 } else if (c > 0x7ff) {
519 *d++ = 0xe0 | (c >> 12);
520 l = 2;
521 } else if (c > 0x7f) {
522 *d++ = 0xc0 | (c >> 6);
523 l = 1;
525 while (l--)
526 *d++ = 0x80 | ((c >> (l * 6)) & 0x3f);
527 return d - dst;
530 void term_screenshot(void)
532 char buf[1 << 11];
533 int fd = open(SCREENSHOT, O_CREAT | O_TRUNC | O_WRONLY, 0600);
534 int i, j;
535 for (i = 0; i < pad_rows(); i++) {
536 char *s = buf;
537 for (j = 0; j < pad_cols(); j++)
538 s += writeutf8(s, screen[OFFSET(i, j)]);
539 *s++ = '\n';
540 write(fd, buf, s - buf);
542 close(fd);
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)
562 int result;
563 int l = 1;
564 if (~c & 0xc0)
565 return c;
566 while (l < 6 && c & (0x40 >> l))
567 l++;
568 result = (0x3f >> l) & c;
569 while (l--)
570 result = (result << 6) | (readpty() & 0x3f);
571 return result;
574 #define unknown(ctl, c)
576 /* control sequences */
577 static void ctlseq(void)
579 int c = readpty();
580 switch (c) {
581 case 0x09: /* HT horizontal tab to next tab stop */
582 advance(0, 8 - col % 8, 0);
583 break;
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);
588 break;
589 case 0x08: /* BS backspace one column */
590 advance(0, -1, 0);
591 break;
592 case 0x1b: /* ESC start escape sequence */
593 escseq();
594 break;
595 case 0x0d: /* CR carriage return */
596 advance(0, -col, 0);
597 break;
598 case 0x00: /* NUL ignored */
599 case 0x07: /* BEL beep */
600 case 0x7f: /* DEL ignored */
601 break;
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);
611 break;
612 default:
613 insertchar(readutf8(c));
614 break;
618 #define ESCM(c) (((c) & 0xf0) == 0x20)
619 #define ESCF(c) ((c) > 0x30 && (c) < 0x7f)
621 /* escape sequences */
622 static void escseq(void)
624 int c = readpty();
625 while (ESCM(c))
626 c = readpty();
627 switch (c) {
628 case '[': /* CSI control sequence introducer */
629 csiseq();
630 break;
631 case '%': /* CS... escseq_cs table */
632 escseq_cs();
633 break;
634 case '(': /* G0... escseq_g0 table */
635 escseq_g0();
636 break;
637 case ')': /* G1... escseq_g1 table */
638 escseq_g1();
639 break;
640 case '*': /* G2... escseq_g2 table */
641 escseq_g2();
642 break;
643 case '+': /* G3... escseq_g3 table */
644 escseq_g3();
645 break;
646 case '7': /* DECSC save state (position, charset, attributes) */
647 misc_save(&term->sav);
648 break;
649 case '8': /* DECRC restore most recently saved state */
650 misc_load(&term->sav);
651 break;
652 case 'M': /* RI reverse line feed */
653 advance(-1, 0, 1);
654 break;
655 case 'D': /* IND line feed */
656 advance(1, 0, 1);
657 break;
658 case 'E': /* NEL newline */
659 advance(1, -col, 1);
660 break;
661 case 'c': /* RIS reset */
662 term_reset();
663 break;
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 */
683 default:
684 unknown("escseq", c);
685 break;
689 static void escseq_cs(void)
691 int c = readpty();
692 switch (c) {
693 case '@': /* CSDFL select default charset (ISO646/8859-1) */
694 case 'G': /* CSUTF8 select UTF-8 */
695 case '8': /* CSUTF8 select UTF-8 (obsolete) */
696 default:
697 unknown("escseq_cs", c);
698 break;
702 static void escseq_g0(void)
704 int c = readpty();
705 switch (c) {
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 */
711 default:
712 unknown("escseq_g0", c);
713 break;
717 static void escseq_g1(void)
719 int c = readpty();
720 switch (c) {
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 */
726 default:
727 unknown("escseq_g1", c);
728 break;
732 static void escseq_g2(void)
734 int c = readpty();
735 switch (c) {
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 */
740 default:
741 unknown("escseq_g2", c);
742 break;
746 static void escseq_g3(void)
748 int c = readpty();
749 switch (c) {
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 */
754 default:
755 unknown("escseq_g3", c);
756 break;
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};
774 int i;
775 int n = 0;
776 int c = readpty();
777 int inter = 0;
778 int priv = 0;
780 if (strchr("<=>?", c)) {
781 priv = c;
782 c = readpty();
784 while (CSIP(c)) {
785 int arg = 0;
786 while (isdigit(c)) {
787 arg = arg * 10 + (c - '0');
788 c = readpty();
790 if (c == ';')
791 c = readpty();
792 args[n] = arg;
793 n = n < ARRAY_SIZE(args) ? n + 1 : 0;
795 while (CSII(c)) {
796 inter = c;
797 c = readpty();
799 switch (c) {
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));
803 break;
804 case 'J': /* ED erase display */
805 switch (args[0]) {
806 case 0:
807 kill_chars(col, pad_cols());
808 blank_rows(row + 1, pad_rows());
809 break;
810 case 1:
811 kill_chars(0, col + 1);
812 blank_rows(0, row - 1);
813 break;
814 case 2:
815 term_blank();
816 break;
818 break;
819 case 'A': /* CUU move cursor up */
820 advance(-MAX(1, args[0]), 0, 0);
821 break;
822 case 'e': /* VPR move cursor down */
823 case 'B': /* CUD move cursor down */
824 advance(MAX(1, args[0]), 0, 0);
825 break;
826 case 'a': /* HPR move cursor right */
827 case 'C': /* CUF move cursor right */
828 advance(0, MAX(1, args[0]), 0);
829 break;
830 case 'D': /* CUB move cursor left */
831 advance(0, -MAX(1, args[0]), 0);
832 break;
833 case 'K': /* EL erase line */
834 switch (args[0]) {
835 case 0:
836 kill_chars(col, pad_cols());
837 break;
838 case 1:
839 kill_chars(0, col + 1);
840 break;
841 case 2:
842 kill_chars(0, pad_cols());
843 break;
845 break;
846 case 'L': /* IL insert blank lines */
847 if (row >= top && row < bot)
848 insert_lines(MAX(1, args[0]));
849 break;
850 case 'M': /* DL delete lines */
851 if (row >= top && row < bot)
852 delete_lines(MAX(1, args[0]));
853 break;
854 case 'd': /* VPA move to row (current column) */
855 move_cursor(absrow(MAX(1, args[0]) - 1), col);
856 break;
857 case 'm': /* SGR set graphic rendition */
858 if (!n)
859 setattr(0);
860 for (i = 0; i < n; i++)
861 setattr(args[i]);
862 break;
863 case 'r': /* DECSTBM set scrolling region to (top, bottom) rows */
864 set_region(args[0], args[1]);
865 break;
866 case 'c': /* DA return ESC [ ? 6 c (VT102) */
867 csiseq_da(priv == '?' ? args[0] | 0x80 : args[0]);
868 break;
869 case 'h': /* SM set mode */
870 for (i = 0; i < n; i++)
871 modeseq(priv == '?' ? args[i] | 0x80 : args[i], 1);
872 break;
873 case 'l': /* RM reset mode */
874 for (i = 0; i < n; i++)
875 modeseq(priv == '?' ? args[i] | 0x80 : args[i], 0);
876 break;
877 case 'P': /* DCH delete characters on current line */
878 delete_chars(MIN(MAX(1, args[0]), pad_cols() - col));
879 break;
880 case '@': /* ICH insert blank characters */
881 insert_chars(MAX(1, args[0]));
882 break;
883 case 'n': /* DSR device status report */
884 csiseq_dsr(args[0]);
885 break;
886 case 'G': /* CHA move cursor to column in current row */
887 advance(0, MAX(0, args[0] - 1) - col, 0);
888 break;
889 case 'X': /* ECH erase characters on current line */
890 kill_chars(col, MIN(col + MAX(1, args[0]), pad_cols()));
891 break;
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 */
900 default:
901 unknown("csiseq", c);
902 break;
906 static void csiseq_da(int c)
908 switch (c) {
909 case 0x00:
910 term_sendstr("\x1b[?6c");
911 break;
912 default:
913 /* we don't care much about cursor shape */
914 /* printf("csiseq_da <0x%x>\n", c); */
915 break;
919 static void csiseq_dsr(int c)
921 char status[1 << 5];
922 switch (c) {
923 case 0x05:
924 term_sendstr("\x1b[0n");
925 break;
926 case 0x06:
927 snprintf(status, sizeof(status), "\x1b[%d;%dR",
928 (origin() ? row - top : row) + 1, col + 1);
929 term_sendstr(status);
930 break;
931 default:
932 unknown("csiseq_dsr", c);
933 break;
937 /* ANSI/DEC specified modes for SM/RM ANSI Specified Modes */
938 static void modeseq(int c, int set)
940 switch (c) {
941 case 0x87: /* DECAWM Auto Wrap */
942 mode = BIT_SET(mode, MODE_WRAP, set);
943 break;
944 case 0x99: /* DECTCEM Cursor on (set); Cursor off (reset) */
945 mode = BIT_SET(mode, MODE_CURSOR, set);
946 break;
947 case 0x86: /* DECOM Sets relative coordinates (set); Sets absolute coordinates (reset) */
948 mode = BIT_SET(mode, MODE_ORIGIN, set);
949 break;
950 case 0x14: /* LNM Line Feed / New Line Mode */
951 mode = BIT_SET(mode, MODE_AUTOCR, set);
952 break;
953 case 0x04: /* IRM insertion/replacement mode (always reset) */
954 break;
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) */
983 default:
984 unknown("modeseq", c);
985 break;