3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License (LGPL) as published by the Free Software Foundation.
7 Please refer to the COPYING file for more information.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 Copyright © 2004 Bruno T. C. de Oliveira
19 Copyright © 2006 Pierre Habouzit
32 #include <sys/types.h>
36 #define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
40 C0_SOH, C0_STX, C0_ETX, C0_EOT, C0_ENQ, C0_ACK, C0_BEL,
41 C0_BS , C0_HT , C0_LF , C0_VT , C0_FF , C0_CR , C0_SO , C0_SI ,
42 C0_DLE, C0_DC1, C0_DC2, D0_DC3, C0_DC4, C0_NAK, C0_SYN, C0_ETB,
43 C0_CAN, C0_EM , C0_SUB, C0_ESC, C0_IS4, C0_IS3, C0_IS2, C0_IS1,
48 C1_41 , C1_BPH, C1_NBH, C1_44 , C1_NEL, C1_SSA, C1_ESA,
49 C1_HTS, C1_HTJ, C1_VTS, C1_PLD, C1_PLU, C1_RI , C1_SS2, C1_SS3,
50 C1_DCS, C1_PU1, C1_PU2, C1_STS, C1_CCH, C1_MW , C1_SPA, C1_EPA,
51 C1_SOS, C1_59 , C1_SCI, C1_CSI, CS_ST , C1_OSC, C1_PM , C1_APC,
56 CSI_CUU, CSI_CUD, CSI_CUF, CSI_CUB, CSI_CNL, CSI_CPL, CSI_CHA,
57 CSI_CUP, CSI_CHT, CSI_ED , CSI_EL , CSI_IL , CSI_DL , CSI_EF , CSI_EA ,
58 CSI_DCH, CSI_SEE, CSI_CPR, CSI_SU , CSI_SD , CSI_NP , CSI_PP , CSI_CTC,
59 CSI_ECH, CSI_CVT, CSI_CBT, CSI_SRS, CSI_PTX, CSI_SDS, CSI_SIMD, CSI_5F,
60 CSI_HPA, CSI_HPR, CSI_REP, CSI_DA , CSI_VPA, CSI_VPR, CSI_HVP, CSI_TBC,
61 CSI_SM , CSI_MC , CSI_HPB, CSI_VPB, CSI_RM , CSI_SGR, CSI_DSR, CSI_DAQ,
62 CSI_70 , CSI_71 , CSI_72 , CSI_73 , CSI_74 , CSI_75 , CSI_76 , CSI_77 ,
63 CSI_78 , CSI_79 , CSI_7A , CSI_7B , CSI_7C , CSI_7D , CSI_7E , CSI_7F
72 static char const * const keytable[KEY_MAX+1] = {
78 [KEY_BACKSPACE] = "\177",
83 [KEY_PPAGE] = "\e[5~",
84 [KEY_NPAGE] = "\e[6~",
85 [KEY_SUSPEND] = "\x1A", /* Ctrl+Z gets mapped to this */
91 [KEY_F(6)] = "\e[17~",
92 [KEY_F(7)] = "\e[18~",
93 [KEY_F(8)] = "\e[19~",
94 [KEY_F(9)] = "\e[20~",
95 [KEY_F(10)] = "\e[21~",
98 static void mtty_row_set(mtty_row_t *row, int start, int len, uint16_t attr)
101 wmemset(row->text + start, 0, len);
102 for (int i = start; i < len + start; i++) {
107 static void mtty_row_roll(mtty_row_t *start, mtty_row_t *end, int count)
116 mtty_row_t *buf = alloca(count * sizeof(mtty_row_t));
118 memcpy(buf, start, count * sizeof(mtty_row_t));
119 memmove(start, start + count, (n - count) * sizeof(mtty_row_t));
120 memcpy(end - count, buf, count * sizeof(mtty_row_t));
121 for (mtty_row_t *row = start; row < end; row++) {
127 static void clamp_cursor_to_bounds(madtty_t *rt)
129 if (rt->curs_row < rt->lines) {
130 rt->curs_row = rt->lines;
132 if (rt->curs_row >= rt->lines + rt->rows) {
133 rt->curs_row = rt->lines + rt->rows - 1;
136 if (rt->curs_col < 0) {
139 if (rt->curs_col >= rt->cols) {
140 rt->curs_col = rt->cols - 1;
144 static void cursor_line_down(madtty_t *rt)
147 if (rt->curs_row < rt->scroll_bot)
150 rt->curs_row = rt->scroll_bot - 1;
151 mtty_row_roll(rt->scroll_top, rt->scroll_bot, 1);
152 mtty_row_set(rt->curs_row, 0, rt->cols, 0);
155 __attribute__((const))
156 static uint16_t build_attrs(unsigned curattrs)
158 return ((curattrs & ~A_COLOR) | COLOR_PAIR(curattrs & 0xff))
159 >> NCURSES_ATTR_SHIFT;
162 static void new_escape_sequence(madtty_t *rt)
169 static void cancel_escape_sequence(madtty_t *rt)
176 static bool is_valid_csi_ender(int c)
178 return (c >= 'a' && c <= 'z')
179 || (c >= 'A' && c <= 'Z')
180 || (c == '@' || c == '`');
183 /* interprets a 'set attribute' (SGR) CSI escape sequence */
184 static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
189 /* special case: reset attributes */
190 rt->curattrs = A_NORMAL;
194 for (i = 0; i < pcount; i++) {
196 #define CASE(x, op) case x: op; break
197 CASE(0, rt->curattrs = A_NORMAL);
198 CASE(1, rt->curattrs |= A_BOLD);
199 CASE(4, rt->curattrs |= A_UNDERLINE);
200 CASE(5, rt->curattrs |= A_BLINK);
201 CASE(6, rt->curattrs |= A_BLINK);
202 CASE(7, rt->curattrs |= A_REVERSE);
203 CASE(8, rt->curattrs |= A_INVIS);
204 CASE(22, rt->curattrs &= ~A_BOLD);
205 CASE(24, rt->curattrs &= ~A_UNDERLINE);
206 CASE(25, rt->curattrs &= ~A_BLINK);
207 CASE(27, rt->curattrs &= ~A_REVERSE);
208 CASE(28, rt->curattrs &= ~A_INVIS);
211 rt->curattrs &= ~0xf0;
212 rt->curattrs |= (param[i] - 29) << 4;
216 rt->curattrs &= ~0xf0;
220 rt->curattrs &= ~0x0f;
221 rt->curattrs |= (param[i] - 39);
225 rt->curattrs &= ~0x0f;
234 /* interprets an 'erase display' (ED) escape sequence */
235 static void interpret_csi_ED(madtty_t *rt, int param[], int pcount)
237 mtty_row_t *row, *start, *end;
238 attr_t attr = build_attrs(rt->curattrs);
241 if (pcount && param[0] == 2) {
243 end = rt->lines + rt->rows;
245 if (pcount && param[0] == 1) {
248 mtty_row_set(rt->curs_row, 0, rt->curs_col + 1, attr);
250 mtty_row_set(rt->curs_row, rt->curs_col,
251 rt->cols - rt->curs_col, attr);
252 start = rt->curs_row + 1;
253 end = rt->lines + rt->rows;
256 for (row = start; row < end; row++) {
257 mtty_row_set(row, 0, rt->cols, attr);
261 /* interprets a 'move cursor' (CUP) escape sequence */
262 static void interpret_csi_CUP(madtty_t *rt, int param[], int pcount)
266 rt->curs_row = rt->lines;
271 return; /* malformed */
274 rt->curs_row = rt->lines + param[0] - 1; /* convert from 1-based to 0-based */
275 rt->curs_col = param[1] - 1; /* convert from 1-based to 0-based */
277 clamp_cursor_to_bounds(rt);
280 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
281 * CPL, CHA, HPR, VPA, VPR, HPA */
282 static void interpret_csi_C(madtty_t *rt, char verb, int param[], int pcount)
284 int n = (pcount && param[0] > 0) ? param[0] : 1;
287 case 'A': rt->curs_row -= n; break;
288 case 'B': case 'e': rt->curs_row += n; break;
289 case 'C': case 'a': rt->curs_col += n; break;
290 case 'D': rt->curs_col -= n; break;
291 case 'E': rt->curs_row += n; rt->curs_col = 0; break;
292 case 'F': rt->curs_row -= n; rt->curs_col = 0; break;
293 case 'G': case '`': rt->curs_col = param[0] - 1; break;
294 case 'd': rt->curs_row = rt->lines + param[0] - 1; break;
297 clamp_cursor_to_bounds(rt);
300 /* Interpret the 'erase line' escape sequence */
301 static void interpret_csi_EL(madtty_t *rt, int param[], int pcount)
303 attr_t attr = build_attrs(rt->curattrs);
305 switch (pcount ? param[0] : 0) {
307 mtty_row_set(rt->curs_row, 0, rt->curs_col + 1, attr);
310 mtty_row_set(rt->curs_row, 0, rt->cols, attr);
313 mtty_row_set(rt->curs_row, rt->curs_col, rt->cols - rt->curs_col,
319 /* Interpret the 'insert blanks' sequence (ICH) */
320 static void interpret_csi_ICH(madtty_t *rt, int param[], int pcount)
322 mtty_row_t *row = rt->curs_row;
323 int n = (pcount && param[0] > 0) ? param[0] : 1;
326 if (rt->curs_col + n > rt->cols) {
327 n = rt->cols - rt->curs_col;
330 for (i = rt->cols - 1; i >= rt->curs_col + n; i--) {
331 row->text[i] = row->text[i - n];
332 row->attr[i] = row->attr[i - n];
335 mtty_row_set(row, rt->curs_col, n, build_attrs(rt->curattrs));
338 /* Interpret the 'delete chars' sequence (DCH) */
339 static void interpret_csi_DCH(madtty_t *rt, int param[], int pcount)
341 mtty_row_t *row = rt->curs_row;
342 int n = (pcount && param[0] > 0) ? param[0] : 1;
345 if (rt->curs_col + n > rt->cols) {
346 n = rt->cols - rt->curs_col;
349 for (i = rt->curs_col; i < rt->cols - n; i++) {
350 row->text[i] = row->text[i + n];
351 row->attr[i] = row->attr[i + n];
354 mtty_row_set(row, rt->cols - n, n, build_attrs(rt->curattrs));
357 /* Interpret a 'scroll reverse' (SR) */
358 static void interpret_csi_SR(madtty_t *rt)
360 mtty_row_roll(rt->scroll_top, rt->scroll_bot, -1);
361 mtty_row_set(rt->scroll_top, 0, rt->cols, build_attrs(rt->curattrs));
364 /* Interpret an 'insert line' sequence (IL) */
365 static void interpret_csi_IL(madtty_t *rt, int param[], int pcount)
367 int n = (pcount && param[0] > 0) ? param[0] : 1;
369 if (rt->curs_row + n >= rt->scroll_bot) {
370 for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
371 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
374 mtty_row_roll(rt->curs_row, rt->scroll_bot, -n);
375 for (mtty_row_t *row = rt->curs_row; row < rt->curs_row + n; row++) {
376 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
381 /* Interpret a 'delete line' sequence (DL) */
382 static void interpret_csi_DL(madtty_t *rt, int param[], int pcount)
384 int n = (pcount && param[0] > 0) ? param[0] : 1;
386 if (rt->curs_row + n >= rt->scroll_bot) {
387 for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
388 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
391 mtty_row_roll(rt->curs_row, rt->scroll_bot, n);
392 for (mtty_row_t *row = rt->scroll_bot - n; row < rt->scroll_bot; row++) {
393 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
398 /* Interpret an 'erase characters' (ECH) sequence */
399 static void interpret_csi_ECH(madtty_t *rt, int param[], int pcount)
401 int n = (pcount && param[0] > 0) ? param[0] : 1;
403 if (rt->curs_col + n < rt->cols) {
404 n = rt->cols - rt->curs_col;
406 mtty_row_set(rt->curs_row, rt->curs_col, n, build_attrs(rt->curattrs));
409 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
410 static void interpret_csi_DECSTBM(madtty_t *rt, int param[], int pcount)
412 int new_top, new_bot;
416 rt->scroll_top = rt->lines;
417 rt->scroll_bot = rt->lines + rt->rows;
420 return; /* malformed */
423 new_top = param[0] - 1;
426 /* clamp to bounds */
429 if (new_top >= rt->rows)
430 new_top = rt->rows - 1;
433 if (new_bot >= rt->rows)
436 /* check for range validity */
437 if (new_top < new_bot) {
438 rt->scroll_top = rt->lines + new_top;
439 rt->scroll_bot = rt->lines + new_bot;
445 static void es_interpret_csi(madtty_t *rt)
447 static int csiparam[MAX_CSI_ES_PARAMS];
449 const char *p = rt->ebuf + 1;
450 char verb = rt->ebuf[rt->elen - 1];
452 p += rt->ebuf[1] == '?'; /* CSI private mode */
454 /* parse numeric parameters */
455 while (isdigit((unsigned char)*p) || *p == ';') {
457 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
458 csiparam[param_count++] = 0;
460 if (param_count == 0) csiparam[param_count++] = 0;
461 csiparam[param_count - 1] *= 10;
462 csiparam[param_count - 1] += *p - '0';
468 if (rt->ebuf[1] == '?') {
471 if (csiparam[0] == 25)
476 if (csiparam[0] == 25)
482 /* delegate handling depending on command character (verb) */
485 if (param_count == 1 && csiparam[0] == 4) /* insert mode */
489 if (param_count == 1 && csiparam[0] == 4) /* replace mode */
492 case 'm': /* it's a 'set attribute' sequence */
493 interpret_csi_SGR(rt, csiparam, param_count); break;
494 case 'J': /* it's an 'erase display' sequence */
495 interpret_csi_ED(rt, csiparam, param_count); break;
496 case 'H': case 'f': /* it's a 'move cursor' sequence */
497 interpret_csi_CUP(rt, csiparam, param_count); break;
498 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
499 case 'e': case 'a': case 'd': case '`':
500 /* it is a 'relative move' */
501 interpret_csi_C(rt, verb, csiparam, param_count); break;
502 case 'K': /* erase line */
503 interpret_csi_EL(rt, csiparam, param_count); break;
504 case '@': /* insert characters */
505 interpret_csi_ICH(rt, csiparam, param_count); break;
506 case 'P': /* delete characters */
507 interpret_csi_DCH(rt, csiparam, param_count); break;
508 case 'L': /* insert lines */
509 interpret_csi_IL(rt, csiparam, param_count); break;
510 case 'M': /* delete lines */
511 interpret_csi_DL(rt, csiparam, param_count); break;
512 case 'X': /* erase chars */
513 interpret_csi_ECH(rt, csiparam, param_count); break;
514 case 'r': /* set scrolling region */
515 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
516 case 's': /* save cursor location */
517 rt->curs_srow = rt->curs_row - rt->lines;
518 rt->curs_scol = rt->curs_col;
520 case 'u': /* restore cursor location */
521 rt->curs_row = rt->lines + rt->curs_srow;
522 rt->curs_col = rt->curs_scol;
523 clamp_cursor_to_bounds(rt);
530 static void try_interpret_escape_seq(madtty_t *rt)
532 char lastchar = rt->ebuf[rt->elen-1];
539 interpret_csi_SR(rt);
540 cancel_escape_sequence(rt);
549 case ']': /* xterm thing */
550 if (lastchar == '\a')
558 if (is_valid_csi_ender(lastchar)) {
559 es_interpret_csi(rt);
560 cancel_escape_sequence(rt);
566 if (rt->elen + 1 >= (int)sizeof(rt->ebuf)) {
570 fprintf(stderr, "cancelled: \\033");
571 for (i = 0; i < rt->elen; i++) {
573 if (isprint(c) && c >= ' ') {
576 fprintf(stderr, "\\%03o", c);
581 cancel_escape_sequence(rt);
585 static void madtty_process_nonprinting(madtty_t *rt, wchar_t wc)
589 new_escape_sequence(rt);
593 /* do nothing for now... maybe a visual bell would be nice? */
597 if (rt->curs_col > 0)
601 case C0_HT: /* tab */
602 rt->curs_col = (rt->curs_col + 8) & ~7;
603 if (rt->curs_col >= rt->cols)
604 rt->curs_col = rt->cols - 1;
614 cursor_line_down(rt);
617 case C0_SO: /* shift out - acs */
618 rt->graphmode = true;
620 case C0_SI: /* shift in - acs */
621 rt->graphmode = false;
626 void madtty_putc(madtty_t *rt, wchar_t wc)
628 if (!rt->seen_input) {
630 kill(-rt->childpid, SIGWINCH);
634 if (wc == '\n' || (wc >= ' ' && isprint(wc))) {
637 fprintf(stderr, "\\%03o", wc);
642 assert (rt->elen + 1 < (int)sizeof(rt->ebuf));
643 rt->ebuf[rt->elen] = wc;
644 rt->ebuf[++rt->elen] = '\0';
645 try_interpret_escape_seq(rt);
646 } else if (IS_CONTROL(wc)) {
647 madtty_process_nonprinting(rt, wc);
652 // vt100 special graphics and line drawing
653 // 5f-7e standard vt100
654 // 40-5e rxvt extension for extra curses acs chars
655 static uint16_t vt100_0[62] = { // 41 .. 7e
656 0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47 hi mr. snowman!
657 0, 0, 0, 0, 0, 0, 0, 0, // 48-4f
658 0, 0, 0, 0, 0, 0, 0, 0, // 50-57
659 0, 0, 0, 0, 0, 0, 0, 0x0020, // 58-5f
660 0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1, // 60-67
661 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, // 68-6f
662 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, // 70-77
663 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, // 78-7e
666 if (wc >= 0x41 && wc <= 0x7e && vt100_0[wc - 0x41]) {
667 wc = vt100_0[wc - 0x41];
668 // width = 1; // vt100 line drawing characters are always single-width
672 if (rt->curs_col >= rt->cols) {
674 cursor_line_down(rt);
680 wmemmove(tmp->text + rt->curs_col + 1, tmp->text + rt->curs_col,
681 (rt->cols - rt->curs_col - 1));
682 memmove(tmp->attr + rt->curs_col + 1, tmp->attr + rt->curs_col,
683 (rt->cols - rt->curs_col - 1) * sizeof(tmp->attr[0]));
686 tmp->text[rt->curs_col] = wc;
687 tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
693 int madtty_process(madtty_t *rt)
702 res = read(rt->pty, rt->rbuf + rt->rlen, sizeof(rt->rbuf) - rt->rlen);
707 while (pos < rt->rlen) {
711 len = (ssize_t)mbrtowc(&wc, rt->rbuf + pos, rt->rlen - pos, &rt->ps);
714 memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
723 pos += len ? len : 1;
728 memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
732 madtty_t *madtty_create(int rows, int cols)
737 if (rows <= 0 || cols <= 0)
740 rt = (madtty_t*)calloc(sizeof(madtty_t), 1);
744 /* record dimensions */
748 /* default mode is replace */
751 /* create the cell matrix */
752 rt->lines = (mtty_row_t*)calloc(sizeof(mtty_row_t), rt->rows);
753 for (i = 0; i < rt->rows; i++) {
754 rt->lines[i].text = (wchar_t *)calloc(sizeof(wchar_t), rt->cols);
755 rt->lines[i].attr = (uint16_t *)calloc(sizeof(uint16_t), rt->cols);
758 rt->pty = -1; /* no pty for now */
760 /* initialization of other public fields */
761 rt->curs_row = rt->lines;
763 rt->curattrs = A_NORMAL; /* white text over black background */
765 /* initial scrolling area is the whole window */
766 rt->scroll_top = rt->lines;
767 rt->scroll_bot = rt->lines + rt->rows;
772 void madtty_destroy(madtty_t *rt)
778 for (i = 0; i < rt->rows; i++) {
779 free(rt->lines[i].text);
780 free(rt->lines[i].attr);
786 void madtty_draw(madtty_t *rt, WINDOW *win, int srow, int scol)
789 for (int i = 0; i < rt->rows; i++) {
790 mtty_row_t *row = rt->lines + i;
796 wmove(win, srow + i, scol);
797 for (int j = 0; j < rt->cols; j++) {
798 if (!j || row->attr[j] != row->attr[j - 1])
799 wattrset(win, (attr_t)row->attr[j] << NCURSES_ATTR_SHIFT);
800 if (row->text[j] >= 128) {
801 char buf[MB_CUR_MAX + 1];
804 len = wcrtomb(buf, row->text[j], NULL);
805 waddnstr(win, buf, len);
807 waddch(win, row->text[j] > ' ' ? row->text[j] : ' ');
813 wmove(win, srow + rt->curs_row - rt->lines, scol + rt->curs_col);
814 curs_set(!rt->curshid);
817 /******************************************************/
819 pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[])
824 ws.ws_row = rt->rows;
825 ws.ws_col = rt->cols;
826 ws.ws_xpixel = ws.ws_ypixel = 0;
828 pid = forkpty(&rt->pty, NULL, NULL, &ws);
834 setenv("TERM", "rxvt", 1);
835 execv(path, (char *const*)argv);
836 fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]);
840 return rt->childpid = pid;
843 void madtty_keypress(madtty_t *rt, int keycode)
845 char c = (char)keycode;
850 if (keycode == KEY_F(1)) {
851 #define MIN(a, b) ((a < (b)) ? a : (b))
852 kill(-rt->childpid, SIGWINCH);
853 rt->scroll_bot = MIN(rt->scroll_bot, rt->lines + rt->rows);
854 rt->curs_row = MIN(rt->curs_row, rt->lines + rt->rows);
855 printf(stderr, "%d\n", rt->rows);
859 if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) {
860 buf = keytable[keycode];
861 len = strlen(keytable[keycode]);
868 int res = write(rt->pty, buf, len);
869 if (res < 0 && errno != EAGAIN && errno != EINTR)
877 void madtty_initialize(void)
879 setlocale(LC_ALL, "");
882 use_default_colors();
885 nodelay(stdscr, TRUE);
886 keypad(stdscr, TRUE);
888 for (int i = -1; i < 8; i++) {
889 for (int j = -1; j < 8; j++) {
890 init_pair((i + 1) * 16 + j + 1, i, j);