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
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
40 #define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
42 static int has_default = 0;
46 C0_SOH, C0_STX, C0_ETX, C0_EOT, C0_ENQ, C0_ACK, C0_BEL,
47 C0_BS , C0_HT , C0_LF , C0_VT , C0_FF , C0_CR , C0_SO , C0_SI ,
48 C0_DLE, C0_DC1, C0_DC2, D0_DC3, C0_DC4, C0_NAK, C0_SYN, C0_ETB,
49 C0_CAN, C0_EM , C0_SUB, C0_ESC, C0_IS4, C0_IS3, C0_IS2, C0_IS1,
54 C1_41 , C1_BPH, C1_NBH, C1_44 , C1_NEL, C1_SSA, C1_ESA,
55 C1_HTS, C1_HTJ, C1_VTS, C1_PLD, C1_PLU, C1_RI , C1_SS2, C1_SS3,
56 C1_DCS, C1_PU1, C1_PU2, C1_STS, C1_CCH, C1_MW , C1_SPA, C1_EPA,
57 C1_SOS, C1_59 , C1_SCI, C1_CSI, CS_ST , C1_OSC, C1_PM , C1_APC,
62 CSI_CUU, CSI_CUD, CSI_CUF, CSI_CUB, CSI_CNL, CSI_CPL, CSI_CHA,
63 CSI_CUP, CSI_CHT, CSI_ED , CSI_EL , CSI_IL , CSI_DL , CSI_EF , CSI_EA ,
64 CSI_DCH, CSI_SEE, CSI_CPR, CSI_SU , CSI_SD , CSI_NP , CSI_PP , CSI_CTC,
65 CSI_ECH, CSI_CVT, CSI_CBT, CSI_SRS, CSI_PTX, CSI_SDS, CSI_SIMD, CSI_5F,
66 CSI_HPA, CSI_HPR, CSI_REP, CSI_DA , CSI_VPA, CSI_VPR, CSI_HVP, CSI_TBC,
67 CSI_SM , CSI_MC , CSI_HPB, CSI_VPB, CSI_RM , CSI_SGR, CSI_DSR, CSI_DAQ,
68 CSI_70 , CSI_71 , CSI_72 , CSI_73 , CSI_74 , CSI_75 , CSI_76 , CSI_77 ,
69 CSI_78 , CSI_79 , CSI_7A , CSI_7B , CSI_7C , CSI_7D , CSI_7E , CSI_7F
78 static char const * const keytable[KEY_MAX+1] = {
84 [KEY_BACKSPACE] = "\177",
89 [KEY_PPAGE] = "\e[5~",
90 [KEY_NPAGE] = "\e[6~",
91 [KEY_SUSPEND] = "\x1A", /* Ctrl+Z gets mapped to this */
97 [KEY_F(6)] = "\e[17~",
98 [KEY_F(7)] = "\e[18~",
99 [KEY_F(8)] = "\e[19~",
100 [KEY_F(9)] = "\e[20~",
101 [KEY_F(10)] = "\e[21~",
104 static void mtty_row_set(mtty_row_t *row, int start, int len, uint16_t attr)
107 wmemset(row->text + start, 0, len);
108 for (int i = start; i < len + start; i++) {
113 static void mtty_row_roll(mtty_row_t *start, mtty_row_t *end, int count)
122 mtty_row_t *buf = alloca(count * sizeof(mtty_row_t));
124 memcpy(buf, start, count * sizeof(mtty_row_t));
125 memmove(start, start + count, (n - count) * sizeof(mtty_row_t));
126 memcpy(end - count, buf, count * sizeof(mtty_row_t));
127 for (mtty_row_t *row = start; row < end; row++) {
133 static void clamp_cursor_to_bounds(madtty_t *rt)
135 if (rt->curs_row < rt->lines) {
136 rt->curs_row = rt->lines;
138 if (rt->curs_row >= rt->lines + rt->rows) {
139 rt->curs_row = rt->lines + rt->rows - 1;
142 if (rt->curs_col < 0) {
145 if (rt->curs_col >= rt->cols) {
146 rt->curs_col = rt->cols - 1;
150 static void cursor_line_down(madtty_t *rt)
153 if (rt->curs_row < rt->scroll_bot)
156 rt->curs_row = rt->scroll_bot - 1;
157 mtty_row_roll(rt->scroll_top, rt->scroll_bot, 1);
158 mtty_row_set(rt->curs_row, 0, rt->cols, 0);
161 __attribute__((const))
162 static uint16_t build_attrs(unsigned curattrs)
164 return ((curattrs & ~A_COLOR) | COLOR_PAIR(curattrs & 0xff))
165 >> NCURSES_ATTR_SHIFT;
168 static void new_escape_sequence(madtty_t *rt)
175 static void cancel_escape_sequence(madtty_t *rt)
182 static bool is_valid_csi_ender(int c)
184 return (c >= 'a' && c <= 'z')
185 || (c >= 'A' && c <= 'Z')
186 || (c == '@' || c == '`');
189 /* interprets a 'set attribute' (SGR) CSI escape sequence */
190 static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
195 /* special case: reset attributes */
196 rt->curattrs = A_NORMAL;
200 for (i = 0; i < pcount; i++) {
202 #define CASE(x, op) case x: op; break
203 CASE(0, rt->curattrs = A_NORMAL);
204 CASE(1, rt->curattrs |= A_BOLD);
205 CASE(4, rt->curattrs |= A_UNDERLINE);
206 CASE(5, rt->curattrs |= A_BLINK);
207 CASE(6, rt->curattrs |= A_BLINK);
208 CASE(7, rt->curattrs |= A_REVERSE);
209 CASE(8, rt->curattrs |= A_INVIS);
210 CASE(22, rt->curattrs &= ~A_BOLD);
211 CASE(24, rt->curattrs &= ~A_UNDERLINE);
212 CASE(25, rt->curattrs &= ~A_BLINK);
213 CASE(27, rt->curattrs &= ~A_REVERSE);
214 CASE(28, rt->curattrs &= ~A_INVIS);
216 case 30 ... 37: /* fg */
218 rt->curattrs &= ~0xf0;
219 rt->curattrs |= (param[i] + 1 - 30) << 4;
221 rt->curattrs &= ~070;
222 rt->curattrs |= (7 - (param[i] - 30)) << 3;
227 rt->curattrs &= has_default ? ~0xf0 : ~070;
230 case 40 ... 47: /* bg */
232 rt->curattrs &= ~0x0f;
233 rt->curattrs |= (param[i] + 1 - 40);
235 rt->curattrs &= ~007;
236 rt->curattrs |= (param[i] - 40);
241 rt->curattrs &= has_default ? ~0x0f : ~007;
250 /* interprets an 'erase display' (ED) escape sequence */
251 static void interpret_csi_ED(madtty_t *rt, int param[], int pcount)
253 mtty_row_t *row, *start, *end;
254 attr_t attr = build_attrs(rt->curattrs);
257 if (pcount && param[0] == 2) {
259 end = rt->lines + rt->rows;
261 if (pcount && param[0] == 1) {
264 mtty_row_set(rt->curs_row, 0, rt->curs_col + 1, attr);
266 mtty_row_set(rt->curs_row, rt->curs_col,
267 rt->cols - rt->curs_col, attr);
268 start = rt->curs_row + 1;
269 end = rt->lines + rt->rows;
272 for (row = start; row < end; row++) {
273 mtty_row_set(row, 0, rt->cols, attr);
277 /* interprets a 'move cursor' (CUP) escape sequence */
278 static void interpret_csi_CUP(madtty_t *rt, int param[], int pcount)
282 rt->curs_row = rt->lines;
287 return; /* malformed */
290 rt->curs_row = rt->lines + param[0] - 1; /* convert from 1-based to 0-based */
291 rt->curs_col = param[1] - 1; /* convert from 1-based to 0-based */
293 clamp_cursor_to_bounds(rt);
296 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
297 * CPL, CHA, HPR, VPA, VPR, HPA */
298 static void interpret_csi_C(madtty_t *rt, char verb, int param[], int pcount)
300 int n = (pcount && param[0] > 0) ? param[0] : 1;
303 case 'A': rt->curs_row -= n; break;
304 case 'B': case 'e': rt->curs_row += n; break;
305 case 'C': case 'a': rt->curs_col += n; break;
306 case 'D': rt->curs_col -= n; break;
307 case 'E': rt->curs_row += n; rt->curs_col = 0; break;
308 case 'F': rt->curs_row -= n; rt->curs_col = 0; break;
309 case 'G': case '`': rt->curs_col = param[0] - 1; break;
310 case 'd': rt->curs_row = rt->lines + param[0] - 1; break;
313 clamp_cursor_to_bounds(rt);
316 /* Interpret the 'erase line' escape sequence */
317 static void interpret_csi_EL(madtty_t *rt, int param[], int pcount)
319 attr_t attr = build_attrs(rt->curattrs);
321 switch (pcount ? param[0] : 0) {
323 mtty_row_set(rt->curs_row, 0, rt->curs_col + 1, attr);
326 mtty_row_set(rt->curs_row, 0, rt->cols, attr);
329 mtty_row_set(rt->curs_row, rt->curs_col, rt->cols - rt->curs_col,
335 /* Interpret the 'insert blanks' sequence (ICH) */
336 static void interpret_csi_ICH(madtty_t *rt, int param[], int pcount)
338 mtty_row_t *row = rt->curs_row;
339 int n = (pcount && param[0] > 0) ? param[0] : 1;
342 if (rt->curs_col + n > rt->cols) {
343 n = rt->cols - rt->curs_col;
346 for (i = rt->cols - 1; i >= rt->curs_col + n; i--) {
347 row->text[i] = row->text[i - n];
348 row->attr[i] = row->attr[i - n];
351 mtty_row_set(row, rt->curs_col, n, build_attrs(rt->curattrs));
354 /* Interpret the 'delete chars' sequence (DCH) */
355 static void interpret_csi_DCH(madtty_t *rt, int param[], int pcount)
357 mtty_row_t *row = rt->curs_row;
358 int n = (pcount && param[0] > 0) ? param[0] : 1;
361 if (rt->curs_col + n > rt->cols) {
362 n = rt->cols - rt->curs_col;
365 for (i = rt->curs_col; i < rt->cols - n; i++) {
366 row->text[i] = row->text[i + n];
367 row->attr[i] = row->attr[i + n];
370 mtty_row_set(row, rt->cols - n, n, build_attrs(rt->curattrs));
373 /* Interpret a 'scroll reverse' (SR) */
374 static void interpret_csi_SR(madtty_t *rt)
376 mtty_row_roll(rt->scroll_top, rt->scroll_bot, -1);
377 mtty_row_set(rt->scroll_top, 0, rt->cols, build_attrs(rt->curattrs));
380 /* Interpret an 'insert line' sequence (IL) */
381 static void interpret_csi_IL(madtty_t *rt, int param[], int pcount)
383 int n = (pcount && param[0] > 0) ? param[0] : 1;
385 if (rt->curs_row + n >= rt->scroll_bot) {
386 for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
387 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
390 mtty_row_roll(rt->curs_row, rt->scroll_bot, -n);
391 for (mtty_row_t *row = rt->curs_row; row < rt->curs_row + n; row++) {
392 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
397 /* Interpret a 'delete line' sequence (DL) */
398 static void interpret_csi_DL(madtty_t *rt, int param[], int pcount)
400 int n = (pcount && param[0] > 0) ? param[0] : 1;
402 if (rt->curs_row + n >= rt->scroll_bot) {
403 for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
404 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
407 mtty_row_roll(rt->curs_row, rt->scroll_bot, n);
408 for (mtty_row_t *row = rt->scroll_bot - n; row < rt->scroll_bot; row++) {
409 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
414 /* Interpret an 'erase characters' (ECH) sequence */
415 static void interpret_csi_ECH(madtty_t *rt, int param[], int pcount)
417 int n = (pcount && param[0] > 0) ? param[0] : 1;
419 if (rt->curs_col + n < rt->cols) {
420 n = rt->cols - rt->curs_col;
422 mtty_row_set(rt->curs_row, rt->curs_col, n, build_attrs(rt->curattrs));
425 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
426 static void interpret_csi_DECSTBM(madtty_t *rt, int param[], int pcount)
428 int new_top, new_bot;
432 rt->scroll_top = rt->lines;
433 rt->scroll_bot = rt->lines + rt->rows;
436 return; /* malformed */
439 new_top = param[0] - 1;
442 /* clamp to bounds */
445 if (new_top >= rt->rows)
446 new_top = rt->rows - 1;
449 if (new_bot >= rt->rows)
452 /* check for range validity */
453 if (new_top < new_bot) {
454 rt->scroll_top = rt->lines + new_top;
455 rt->scroll_bot = rt->lines + new_bot;
461 static void es_interpret_csi(madtty_t *rt)
463 static int csiparam[MAX_CSI_ES_PARAMS];
465 const char *p = rt->ebuf + 1;
466 char verb = rt->ebuf[rt->elen - 1];
468 p += rt->ebuf[1] == '?'; /* CSI private mode */
470 /* parse numeric parameters */
471 while (isdigit((unsigned char)*p) || *p == ';') {
473 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
474 csiparam[param_count++] = 0;
476 if (param_count == 0) csiparam[param_count++] = 0;
477 csiparam[param_count - 1] *= 10;
478 csiparam[param_count - 1] += *p - '0';
484 if (rt->ebuf[1] == '?') {
487 if (csiparam[0] == 25)
492 if (csiparam[0] == 25)
498 /* delegate handling depending on command character (verb) */
501 if (param_count == 1 && csiparam[0] == 4) /* insert mode */
505 if (param_count == 1 && csiparam[0] == 4) /* replace mode */
508 case 'm': /* it's a 'set attribute' sequence */
509 interpret_csi_SGR(rt, csiparam, param_count); break;
510 case 'J': /* it's an 'erase display' sequence */
511 interpret_csi_ED(rt, csiparam, param_count); break;
512 case 'H': case 'f': /* it's a 'move cursor' sequence */
513 interpret_csi_CUP(rt, csiparam, param_count); break;
514 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
515 case 'e': case 'a': case 'd': case '`':
516 /* it is a 'relative move' */
517 interpret_csi_C(rt, verb, csiparam, param_count); break;
518 case 'K': /* erase line */
519 interpret_csi_EL(rt, csiparam, param_count); break;
520 case '@': /* insert characters */
521 interpret_csi_ICH(rt, csiparam, param_count); break;
522 case 'P': /* delete characters */
523 interpret_csi_DCH(rt, csiparam, param_count); break;
524 case 'L': /* insert lines */
525 interpret_csi_IL(rt, csiparam, param_count); break;
526 case 'M': /* delete lines */
527 interpret_csi_DL(rt, csiparam, param_count); break;
528 case 'X': /* erase chars */
529 interpret_csi_ECH(rt, csiparam, param_count); break;
530 case 'r': /* set scrolling region */
531 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
532 case 's': /* save cursor location */
533 rt->curs_srow = rt->curs_row - rt->lines;
534 rt->curs_scol = rt->curs_col;
536 case 'u': /* restore cursor location */
537 rt->curs_row = rt->lines + rt->curs_srow;
538 rt->curs_col = rt->curs_scol;
539 clamp_cursor_to_bounds(rt);
546 static void try_interpret_escape_seq(madtty_t *rt)
548 char lastchar = rt->ebuf[rt->elen-1];
555 interpret_csi_SR(rt);
556 cancel_escape_sequence(rt);
565 case ']': /* xterm thing */
566 if (lastchar == '\a')
574 if (is_valid_csi_ender(lastchar)) {
575 es_interpret_csi(rt);
576 cancel_escape_sequence(rt);
582 if (rt->elen + 1 >= (int)sizeof(rt->ebuf)) {
586 fprintf(stderr, "cancelled: \\033");
587 for (i = 0; i < rt->elen; i++) {
589 if (isprint(c) && c >= ' ') {
592 fprintf(stderr, "\\%03o", c);
597 cancel_escape_sequence(rt);
601 static void madtty_process_nonprinting(madtty_t *rt, wchar_t wc)
605 new_escape_sequence(rt);
609 /* do nothing for now... maybe a visual bell would be nice? */
613 if (rt->curs_col > 0)
617 case C0_HT: /* tab */
618 rt->curs_col = (rt->curs_col + 8) & ~7;
619 if (rt->curs_col >= rt->cols)
620 rt->curs_col = rt->cols - 1;
630 cursor_line_down(rt);
633 case C0_SO: /* shift out - acs */
634 rt->graphmode = true;
636 case C0_SI: /* shift in - acs */
637 rt->graphmode = false;
642 void madtty_putc(madtty_t *rt, wchar_t wc)
646 if (!rt->seen_input) {
648 kill(-rt->childpid, SIGWINCH);
652 if (wc == '\n' || (wc >= ' ' && isprint(wc))) {
655 fprintf(stderr, "\\%03o", wc);
660 assert (rt->elen + 1 < (int)sizeof(rt->ebuf));
661 rt->ebuf[rt->elen] = wc;
662 rt->ebuf[++rt->elen] = '\0';
663 try_interpret_escape_seq(rt);
664 } else if (IS_CONTROL(wc)) {
665 madtty_process_nonprinting(rt, wc);
670 // vt100 special graphics and line drawing
671 // 5f-7e standard vt100
672 // 40-5e rxvt extension for extra curses acs chars
673 static uint16_t vt100_0[62] = { // 41 .. 7e
674 0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47 hi mr. snowman!
675 0, 0, 0, 0, 0, 0, 0, 0, // 48-4f
676 0, 0, 0, 0, 0, 0, 0, 0, // 50-57
677 0, 0, 0, 0, 0, 0, 0, 0x0020, // 58-5f
678 0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1, // 60-67
679 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, // 68-6f
680 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, // 70-77
681 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, // 78-7e
684 if (wc >= 0x41 && wc <= 0x7e && vt100_0[wc - 0x41]) {
685 wc = vt100_0[wc - 0x41];
689 width = wcwidth(wc) ?: 1;
692 if (width == 2 && rt->curs_col == rt->cols - 1) {
695 tmp->text[rt->curs_col] = 0;
696 tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
700 if (rt->curs_col >= rt->cols) {
702 cursor_line_down(rt);
709 wmemmove(tmp->text + rt->curs_col + width, tmp->text + rt->curs_col,
710 (rt->cols - rt->curs_col - width));
711 memmove(tmp->attr + rt->curs_col + width, tmp->attr + rt->curs_col,
712 (rt->cols - rt->curs_col - width) * sizeof(tmp->attr[0]));
715 tmp->text[rt->curs_col] = wc;
716 tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
719 tmp->text[rt->curs_col] = 0;
720 tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
726 int madtty_process(madtty_t *rt)
735 res = read(rt->pty, rt->rbuf + rt->rlen, sizeof(rt->rbuf) - rt->rlen);
740 while (pos < rt->rlen) {
744 len = (ssize_t)mbrtowc(&wc, rt->rbuf + pos, rt->rlen - pos, &rt->ps);
747 memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
756 pos += len ? len : 1;
761 memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
765 madtty_t *madtty_create(int rows, int cols)
770 if (rows <= 0 || cols <= 0)
773 rt = (madtty_t*)calloc(sizeof(madtty_t), 1);
777 /* record dimensions */
781 /* default mode is replace */
784 /* create the cell matrix */
785 rt->lines = (mtty_row_t*)calloc(sizeof(mtty_row_t), rt->rows);
786 for (i = 0; i < rt->rows; i++) {
787 rt->lines[i].text = (wchar_t *)calloc(sizeof(wchar_t), rt->cols);
788 rt->lines[i].attr = (uint16_t *)calloc(sizeof(uint16_t), rt->cols);
791 rt->pty = -1; /* no pty for now */
793 /* initialization of other public fields */
794 rt->curs_row = rt->lines;
796 rt->curattrs = A_NORMAL; /* white text over black background */
798 /* initial scrolling area is the whole window */
799 rt->scroll_top = rt->lines;
800 rt->scroll_bot = rt->lines + rt->rows;
805 void madtty_resize(madtty_t *rt, int rows, int cols)
807 struct winsize ws = { .ws_row = rows, .ws_col = cols };
808 mtty_row_t *lines = rt->lines;
810 if (rows <= 0 || cols <= 0)
813 if (rt->rows != rows) {
814 while (rt->rows > rows) {
815 free(lines[rt->rows - 1].text);
816 free(lines[rt->rows - 1].attr);
820 lines = realloc(lines, sizeof(mtty_row_t) * rows);
823 if (rt->cols != cols) {
824 for (int row = 0; row < rt->rows; row++) {
825 lines[row].text = realloc(lines[row].text, sizeof(wchar_t) * cols);
826 lines[row].attr = realloc(lines[row].attr, sizeof(uint16_t) * cols);
828 mtty_row_set(lines + row, rt->cols, cols - rt->cols, 0);
833 while (rt->rows < rows) {
834 lines[rt->rows].text = (wchar_t *)calloc(sizeof(wchar_t), cols);
835 lines[rt->rows].attr = (uint16_t *)calloc(sizeof(uint16_t), cols);
839 rt->curs_row += lines - rt->lines;
840 rt->scroll_top += lines - rt->lines;
841 rt->scroll_bot += lines - rt->lines;
842 if (rt->scroll_bot > lines + rt->rows)
843 rt->scroll_bot = lines + rt->rows;
845 clamp_cursor_to_bounds(rt);
846 ioctl(rt->pty, TIOCSWINSZ, &ws);
847 kill(-rt->childpid, SIGWINCH);
850 void madtty_destroy(madtty_t *rt)
856 for (i = 0; i < rt->rows; i++) {
857 free(rt->lines[i].text);
858 free(rt->lines[i].attr);
864 void madtty_draw(madtty_t *rt, WINDOW *win, int srow, int scol)
867 for (int i = 0; i < rt->rows; i++) {
868 mtty_row_t *row = rt->lines + i;
874 wmove(win, srow + i, scol);
875 for (int j = 0; j < rt->cols; j++) {
876 if (!j || row->attr[j] != row->attr[j - 1])
877 wattrset(win, (attr_t)row->attr[j] << NCURSES_ATTR_SHIFT);
878 if (row->text[j] >= 128) {
879 char buf[MB_CUR_MAX + 1];
882 len = wcrtomb(buf, row->text[j], NULL);
883 waddnstr(win, buf, len);
884 if (wcwidth(row->text[j]) > 1)
887 waddch(win, row->text[j] > ' ' ? row->text[j] : ' ');
893 wmove(win, srow + rt->curs_row - rt->lines, scol + rt->curs_col);
894 curs_set(!rt->curshid);
897 /******************************************************/
899 pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[])
904 ws.ws_row = rt->rows;
905 ws.ws_col = rt->cols;
906 ws.ws_xpixel = ws.ws_ypixel = 0;
908 pid = forkpty(&rt->pty, NULL, NULL, &ws);
914 setenv("TERM", "rxvt", 1);
915 execv(path, (char *const*)argv);
916 fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]);
920 return rt->childpid = pid;
923 void madtty_keypress(madtty_t *rt, int keycode)
925 char c = (char)keycode;
929 if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) {
930 buf = keytable[keycode];
931 len = strlen(keytable[keycode]);
938 int res = write(rt->pty, buf, len);
939 if (res < 0 && errno != EAGAIN && errno != EINTR)
947 void madtty_initialize(void)
949 setlocale(LC_ALL, "");
954 nodelay(stdscr, TRUE);
955 keypad(stdscr, TRUE);
960 use_default_colors();
961 assume_default_colors(-1, -1);
964 for (int bg = -1; bg < 8; bg++) {
965 for (int fg = -1; fg < 8; fg++) {
966 init_pair((fg + 1) * 16 + bg + 1, fg, bg);
970 for (int bg = 0; bg < 8; bg++) {
971 for (int fg = 0; fg < 8; fg++) {
972 init_pair((7 - fg) * 8 + bg, fg, bg);