Fix compilation on NetBSD.
[apps/madtty.git] / madtty / madtty.c
index e6d9dde..317712c 100644 (file)
     Copyright © 2006 Pierre Habouzit
  */
 
+#define _GNU_SOURCE
+#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <locale.h>
-#include <pty.h>
+#include <langinfo.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.h>
-
+#include <sys/types.h>
+#include <termios.h>
+#include <wchar.h>
+#ifdef __linux__
+# include <pty.h>
+#elif defined(__FreeBSD__)
+# include <libutil.h>
+#elif defined(__OpenBSD__) || defined(__NetBSD__)
+# include <util.h>
+#endif
 #include "madtty.h"
 
-static void clamp_cursor_to_bounds(madtty_t *rt)
-{
-    if (rt->curs_row < 0) {
-        rt->curs_row = 0;
-    }
-    if (rt->curs_col < 0) {
-        rt->curs_col = 0;
-    }
+#ifndef NCURSES_ATTR_SHIFT
+# define NCURSES_ATTR_SHIFT 8
+#endif
 
-    if (rt->curs_row >= rt->rows) {
-        rt->curs_row = rt->rows - 1;
-    }
-    if (rt->curs_col >= rt->cols) {
-        rt->curs_col = rt->cols - 1;
-    }
-}
+#define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
 
-static void cursor_line_down(madtty_t *rt)
-{
-    int i;
+static int has_default, is_utf8;
 
-    rt->curs_row++;
-    if (rt->curs_row <= rt->scrollbottom)
-        return;
+enum {
+    C0_NUL = 0x00,
+            C0_SOH, C0_STX, C0_ETX, C0_EOT, C0_ENQ, C0_ACK, C0_BEL,
+    C0_BS , C0_HT , C0_LF , C0_VT , C0_FF , C0_CR , C0_SO , C0_SI ,
+    C0_DLE, C0_DC1, C0_DC2, D0_DC3, C0_DC4, C0_NAK, C0_SYN, C0_ETB,
+    C0_CAN, C0_EM , C0_SUB, C0_ESC, C0_IS4, C0_IS3, C0_IS2, C0_IS1,
+};
 
-    /* must scroll the scrolling region up by 1 line, and put cursor on
-     * last line of it */
-    rt->curs_row = rt->scrollbottom;
+enum {
+    C1_40 = 0x40,
+            C1_41 , C1_BPH, C1_NBH, C1_44 , C1_NEL, C1_SSA, C1_ESA,
+    C1_HTS, C1_HTJ, C1_VTS, C1_PLD, C1_PLU, C1_RI , C1_SS2, C1_SS3,
+    C1_DCS, C1_PU1, C1_PU2, C1_STS, C1_CCH, C1_MW , C1_SPA, C1_EPA,
+    C1_SOS, C1_59 , C1_SCI, C1_CSI, CS_ST , C1_OSC, C1_PM , C1_APC,
+};
 
-    for (i = rt->scrolltop; i < rt->scrollbottom; i++) {
-        memcpy(rt->cells[i], rt->cells[i+1], sizeof(RoteCell) * rt->cols);
-    }
+enum {
+    CSI_ICH = 0x40,
+             CSI_CUU, CSI_CUD, CSI_CUF, CSI_CUB, CSI_CNL, CSI_CPL, CSI_CHA,
+    CSI_CUP, CSI_CHT, CSI_ED , CSI_EL , CSI_IL , CSI_DL , CSI_EF , CSI_EA ,
+    CSI_DCH, CSI_SEE, CSI_CPR, CSI_SU , CSI_SD , CSI_NP , CSI_PP , CSI_CTC,
+    CSI_ECH, CSI_CVT, CSI_CBT, CSI_SRS, CSI_PTX, CSI_SDS, CSI_SIMD, CSI_5F,
+    CSI_HPA, CSI_HPR, CSI_REP, CSI_DA , CSI_VPA, CSI_VPR, CSI_HVP, CSI_TBC,
+    CSI_SM , CSI_MC , CSI_HPB, CSI_VPB, CSI_RM , CSI_SGR, CSI_DSR, CSI_DAQ,
+    CSI_70 , CSI_71 , CSI_72 , CSI_73 , CSI_74 , CSI_75 , CSI_76 , CSI_77 ,
+    CSI_78 , CSI_79 , CSI_7A , CSI_7B , CSI_7C , CSI_7D , CSI_7E , CSI_7F
+};
+
+struct madtty_t {
+    int   pty;
+    pid_t childpid;
+
+    /* flags */
+    unsigned seen_input : 1;
+    unsigned insert     : 1;
+    unsigned escaped    : 1;
+    unsigned graphmode  : 1;
+    unsigned curshid    : 1;
+
+    /* geometry */
+    int rows, cols;
+    unsigned curattrs;
+
+    struct t_row_t *lines;
+    struct t_row_t *scroll_top;
+    struct t_row_t *scroll_bot;
+
+    /* cursor */
+    struct t_row_t *curs_row;
+    int curs_col, curs_srow, curs_scol;
+
+    /* buffers and parsing state */
+    mbstate_t ps;
+    char rbuf[BUFSIZ];
+    char ebuf[BUFSIZ];
+    int  rlen, elen;
+};
+
+typedef struct t_row_t {
+    wchar_t  *text;
+    uint16_t *attr;
+    unsigned dirty : 1;
+} t_row_t;
 
-    /* clear last row of the scrolling region */
-    for (i = 0; i < rt->cols; i++) {
-        rt->cells[rt->scrollbottom][i].s[0]  = 0x20;
-        rt->cells[rt->scrollbottom][i].len   = 1;
-        rt->cells[rt->scrollbottom][i].attrs = A_NORMAL;
+static char const * const keytable[KEY_MAX+1] = {
+    ['\n']          = "\r",
+    [KEY_UP]        = "\e[A",
+    [KEY_DOWN]      = "\e[B",
+    [KEY_RIGHT]     = "\e[C",
+    [KEY_LEFT]      = "\e[D",
+    [KEY_BACKSPACE] = "\177",
+    [KEY_HOME]      = "\e[1~",
+    [KEY_IC]        = "\e[2~",
+    [KEY_DC]        = "\e[3~",
+    [KEY_END]       = "\e[4~",
+    [KEY_PPAGE]     = "\e[5~",
+    [KEY_NPAGE]     = "\e[6~",
+    [KEY_SUSPEND]   = "\x1A",  /* Ctrl+Z gets mapped to this */
+    [KEY_F(1)]      = "\e[11~",
+    [KEY_F(2)]      = "\e[12~",
+    [KEY_F(3)]      = "\e[13~",
+    [KEY_F(4)]      = "\e[14~",
+    [KEY_F(5)]      = "\e[15~",
+    [KEY_F(6)]      = "\e[17~",
+    [KEY_F(7)]      = "\e[18~",
+    [KEY_F(8)]      = "\e[19~",
+    [KEY_F(9)]      = "\e[20~",
+    [KEY_F(10)]     = "\e[21~",
+    [KEY_F(11)]     = "\e[23~",
+    [KEY_F(12)]     = "\e[24~",
+    [KEY_F(13)]     = "\e[25~",
+    [KEY_F(14)]     = "\e[26~",
+    [KEY_F(15)]     = "\e[28~",
+    [KEY_F(16)]     = "\e[29~",
+    [KEY_F(17)]     = "\e[31~",
+    [KEY_F(18)]     = "\e[32~",
+    [KEY_F(19)]     = "\e[33~",
+    [KEY_F(20)]     = "\e[34~",
+};
+
+static void t_row_set(t_row_t *row, int start, int len, uint16_t attr)
+{
+    row->dirty = true;
+    wmemset(row->text + start, 0, len);
+    for (int i = start; i < len + start; i++) {
+        row->attr[i] = attr;
     }
 }
 
-static void cursor_line_up(madtty_t *rt)
+static void t_row_roll(t_row_t *start, t_row_t *end, int count)
 {
-    int i;
-
-    rt->curs_row--;
-    if (rt->curs_row >= rt->scrolltop)
-        return;
+    int n = end - start;
 
-    /* must scroll the scrolling region up by 1 line, and put cursor on
-     * first line of it */
-    rt->curs_row = rt->scrolltop;
+    count %= n;
+    if (count < 0)
+        count += n;
 
-    for (i = rt->scrollbottom; i > rt->scrolltop; i--) {
-        memcpy(rt->cells[i], rt->cells[i-1], sizeof(RoteCell) * rt->cols);
-    }
+    if (count) {
+        t_row_t *buf = alloca(count * sizeof(t_row_t));
 
-    /* clear first row of the scrolling region */
-    for (i = 0; i < rt->cols; i++) {
-        rt->cells[rt->scrolltop][i].s[0]  = 0x20;
-        rt->cells[rt->scrolltop][i].len   = 1;
-        rt->cells[rt->scrolltop][i].attrs = A_NORMAL;
+        memcpy(buf, start, count * sizeof(t_row_t));
+        memmove(start, start + count, (n - count) * sizeof(t_row_t));
+        memcpy(end - count, buf, count * sizeof(t_row_t));
+        for (t_row_t *row = start; row < end; row++) {
+            row->dirty = true;
+        }
     }
 }
 
-static void put_normal_char(madtty_t *rt, const char *s, int len)
+static void clamp_cursor_to_bounds(madtty_t *t)
 {
-    if (rt->curs_col >= rt->cols) {
-        rt->curs_col = 0;
-        cursor_line_down(rt);
+    if (t->curs_row < t->lines) {
+        t->curs_row = t->lines;
     }
-
-    if (rt->insert) {
-        int i;
-
-        for (i = rt->cols - 1; i >= rt->curs_col+1; i--) {
-            rt->cells[rt->curs_row][i] = rt->cells[rt->curs_row][i-1];
-        }
+    if (t->curs_row >= t->lines + t->rows) {
+        t->curs_row = t->lines + t->rows - 1;
     }
 
-    memcpy(rt->cells[rt->curs_row][rt->curs_col].s, s, len);
-    rt->cells[rt->curs_row][rt->curs_col].len   = len;
-    rt->cells[rt->curs_row][rt->curs_col].attrs = rt->curattrs;
-    rt->curs_col++;
+    if (t->curs_col < 0) {
+        t->curs_col = 0;
+    }
+    if (t->curs_col >= t->cols) {
+        t->curs_col = t->cols - 1;
+    }
 }
 
-static void put_graphmode_char(madtty_t *rt, int c)
+static void cursor_line_down(madtty_t *t)
 {
-    char nc;
-    /* do some very pitiful translation to regular ascii chars */
-    switch (c) {
-      case 'j': case 'k': case 'l': case 'm': case 'n': case 't':
-      case 'u': case 'v': case 'w':
-        nc = '+'; break;
-      case 'x':
-        nc = '|'; break;
-      default:
-        nc = '%';
-    }
+    t->curs_row++;
+    if (t->curs_row < t->scroll_bot)
+        return;
 
-    put_normal_char(rt, &nc, 1);
+    t->curs_row = t->scroll_bot - 1;
+    t_row_roll(t->scroll_top, t->scroll_bot, 1);
+    t_row_set(t->curs_row, 0, t->cols, 0);
 }
 
-static void new_escape_sequence(madtty_t *rt)
+__attribute__((const))
+static uint16_t build_attrs(unsigned curattrs)
 {
-    rt->escaped = true;
-    rt->esbuf_len = 0;
-    rt->esbuf[0] = '\0';
+    return ((curattrs & ~A_COLOR) | COLOR_PAIR(curattrs & 0xff))
+        >> NCURSES_ATTR_SHIFT;
 }
 
-static void cancel_escape_sequence(madtty_t *rt)
+static void new_escape_sequence(madtty_t *t)
 {
-    rt->escaped = false;
-    rt->esbuf_len = 0;
-    rt->esbuf[0] = '\0';
+    t->escaped = true;
+    t->elen    = 0;
+    t->ebuf[0] = '\0';
 }
 
-static void handle_control_char(madtty_t *rt, int c)
+static void cancel_escape_sequence(madtty_t *t)
 {
-    switch (c) {
-      case '\r':  /* carriage return */
-        rt->curs_col = 0;
-        break;
-
-      case '\n':  /* line feed */
-        rt->curs_col = 0;
-        cursor_line_down(rt);
-        break;
-
-      case '\b': /* backspace */
-        if (rt->curs_col > 0)
-            rt->curs_col--;
-        break;
-
-      case '\t': /* tab */
-        rt->curs_col = (rt->curs_col + 8) & ~7;
-        clamp_cursor_to_bounds(rt);
-        break;
-
-      case '\x1b': /* begin escape sequence (aborting previous one if any) */
-        new_escape_sequence(rt);
-        break;
-
-      case '\x0e': /* enter graphical character mode */
-        rt->graphmode = true;
-        break;
-
-      case '\x0f': /* exit graphical character mode */
-        rt->graphmode = false;
-        break;
-
-      case '\x9b': /* CSI character. Equivalent to ESC [ */
-        new_escape_sequence(rt);
-        rt->esbuf[rt->esbuf_len++] = '[';
-        break;
-
-      case '\x18':
-      case '\x1a': /* these interrupt escape sequences */
-        cancel_escape_sequence(rt);
-        break;
-
-      case '\a': /* bell */
-        /* do nothing for now... maybe a visual bell would be nice? */
-        break;
-    }
+    t->escaped = false;
+    t->elen    = 0;
+    t->ebuf[0] = '\0';
 }
 
 static bool is_valid_csi_ender(int c)
@@ -206,69 +236,58 @@ static bool is_valid_csi_ender(int c)
 }
 
 /* interprets a 'set attribute' (SGR) CSI escape sequence */
-static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_SGR(madtty_t *t, int param[], int pcount)
 {
     int i;
 
     if (pcount == 0) {
         /* special case: reset attributes */
-        rt->curattrs = A_NORMAL;
+        t->curattrs = A_NORMAL;
         return;
     }
 
     for (i = 0; i < pcount; i++) {
-
-        // From http://vt100.net/docs/vt510-rm/SGR table 5-16
-        // 0   All attributes off
-        // 1   Bold
-        // 4   Underline
-        // 5   Blinking
-        // 7   Negative image
-        // 8   Invisible image
-        // 10  The ASCII character set is the current 7-bit
-        //     display character set (default) - SCO Console only.
-        // 11  Map Hex 00-7F of the PC character set codes
-        //     to the current 7-bit display character set
-        //     - SCO Console only.
-        // 12  Map Hex 80-FF of the current character set to
-        //     the current 7-bit display character set - SCO
-        //     Console only.
-        // 22  Bold off
-        // 24  Underline off
-        // 25  Blinking off
-        // 27  Negative image off
-        // 28  Invisible image off
-
         switch (param[i]) {
 #define CASE(x, op)  case x: op; break
-            CASE(0,  rt->curattrs = A_NORMAL);
-            CASE(1,  rt->curattrs |= A_BOLD);
-            CASE(4,  rt->curattrs |= A_UNDERLINE);
-            CASE(5,  rt->curattrs |= A_BLINK);
-            CASE(7,  rt->curattrs |= A_REVERSE);
-            CASE(8,  rt->curattrs |= A_INVIS);
-            CASE(22, rt->curattrs &= ~A_BOLD);
-            CASE(24, rt->curattrs &= ~A_UNDERLINE);
-            CASE(25, rt->curattrs &= ~A_BLINK);
-            CASE(27, rt->curattrs &= ~A_REVERSE);
-            CASE(28, rt->curattrs &= ~A_INVIS);
-
-          case 30 ... 37:
-            rt->curattrs &= ~070;
-            rt->curattrs |= (param[i] - 30) << 3;
+            CASE(0,  t->curattrs = A_NORMAL);
+            CASE(1,  t->curattrs |= A_BOLD);
+            CASE(4,  t->curattrs |= A_UNDERLINE);
+            CASE(5,  t->curattrs |= A_BLINK);
+            CASE(6,  t->curattrs |= A_BLINK);
+            CASE(7,  t->curattrs |= A_REVERSE);
+            CASE(8,  t->curattrs |= A_INVIS);
+            CASE(22, t->curattrs &= ~A_BOLD);
+            CASE(24, t->curattrs &= ~A_UNDERLINE);
+            CASE(25, t->curattrs &= ~A_BLINK);
+            CASE(27, t->curattrs &= ~A_REVERSE);
+            CASE(28, t->curattrs &= ~A_INVIS);
+
+          case 30 ... 37: /* fg */
+            if (has_default) {
+                t->curattrs &= ~0xf0;
+                t->curattrs |= (param[i] + 1 - 30) << 4;
+            } else {
+                t->curattrs &= ~070;
+                t->curattrs |= (7 - (param[i] - 30)) << 3;
+            }
             break;
 
-          case 40 ... 47:
-            rt->curattrs &= ~007;
-            rt->curattrs |= (param[i] - 40);
+          case 39:
+            t->curattrs &= has_default ? ~0xf0 : ~070;
             break;
 
-          case 39:
-            rt->curattrs &= ~070;
+          case 40 ... 47: /* bg */
+            if (has_default) {
+                t->curattrs &= ~0x0f;
+                t->curattrs |= (param[i] + 1 - 40);
+            } else {
+                t->curattrs &= ~007;
+                t->curattrs |= (param[i] - 40);
+            }
             break;
 
           case 49:
-            rt->curattrs &= ~007;
+            t->curattrs &= has_default ? ~0x0f : ~007;
             break;
 
           default:
@@ -278,234 +297,231 @@ static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
 }
 
 /* interprets an 'erase display' (ED) escape sequence */
-static void interpret_csi_ED(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_ED(madtty_t *t, int param[], int pcount)
 {
-    int r, c;
-    int start_row, start_col, end_row, end_col;
+    t_row_t *row, *start, *end;
+    attr_t attr = build_attrs(t->curattrs);
 
     /* decide range */
     if (pcount && param[0] == 2) {
-        start_row = 0;
-        start_col = 0;
-        end_row = rt->rows - 1;
-        end_col = rt->cols - 1;
+        start = t->lines;
+        end   = t->lines + t->rows;
     } else
     if (pcount && param[0] == 1) {
-        start_row = 0;
-        start_col = 0;
-        end_row = rt->curs_row;
-        end_col = rt->curs_col;
+        start = t->lines;
+        end   = t->curs_row;
+        t_row_set(t->curs_row, 0, t->curs_col + 1, attr);
     } else {
-        start_row = rt->curs_row;
-        start_col = rt->curs_col;
-        end_row = rt->rows - 1;
-        end_col = rt->cols - 1;
-    }
-
-    /* clean range */
-    for (r = start_row; r <= end_row; r++) {
-        for (c = (r == start_row ? start_col : 0);
-             c <= (r == end_row ? end_col : rt->cols - 1);
-             c++)
-        {
-            rt->cells[r][c].s[0]  = 0x20;
-            rt->cells[r][c].len   = 1;
-            rt->cells[r][c].attrs = rt->curattrs;
-        }
+        t_row_set(t->curs_row, t->curs_col,
+                     t->cols - t->curs_col, attr);
+        start = t->curs_row + 1;
+        end   = t->lines + t->rows;
+    }
+
+    for (row = start; row < end; row++) {
+        t_row_set(row, 0, t->cols, attr);
     }
 }
 
 /* interprets a 'move cursor' (CUP) escape sequence */
-static void interpret_csi_CUP(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_CUP(madtty_t *t, int param[], int pcount)
 {
     if (pcount == 0) {
         /* special case */
-        rt->curs_row = rt->curs_col = 0;
+        t->curs_row = t->lines;
+        t->curs_col = 0;
         return;
     } else
     if (pcount < 2) {
         return;  /* malformed */
     }
 
-    rt->curs_row = param[0] - 1;  /* convert from 1-based to 0-based */
-    rt->curs_col = param[1] - 1;  /* convert from 1-based to 0-based */
+    t->curs_row = t->lines + param[0] - 1;
+    t->curs_col = param[1] - 1;
 
-    clamp_cursor_to_bounds(rt);
+    clamp_cursor_to_bounds(t);
 }
 
 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
  * CPL, CHA, HPR, VPA, VPR, HPA */
-static void interpret_csi_C(madtty_t *rt, char verb, int param[], int pcount)
+static void
+interpret_csi_C(madtty_t *t, char verb, int param[], int pcount)
 {
     int n = (pcount && param[0] > 0) ? param[0] : 1;
 
     switch (verb) {
-      case 'A':           rt->curs_row -= n; break;
-      case 'B': case 'e': rt->curs_row += n; break;
-      case 'C': case 'a': rt->curs_col += n; break;
-      case 'D':           rt->curs_col -= n; break;
-      case 'E':           rt->curs_row += n; rt->curs_col = 0; break;
-      case 'F':           rt->curs_row -= n; rt->curs_col = 0; break;
-      case 'G': case '`': rt->curs_col  = param[0] - 1; break;
-      case 'd':           rt->curs_row  = param[0] - 1; break;
+      case 'A':           t->curs_row -= n; break;
+      case 'B': case 'e': t->curs_row += n; break;
+      case 'C': case 'a': t->curs_col += n; break;
+      case 'D':           t->curs_col -= n; break;
+      case 'E':           t->curs_row += n; t->curs_col = 0; break;
+      case 'F':           t->curs_row -= n; t->curs_col = 0; break;
+      case 'G': case '`': t->curs_col  = param[0] - 1; break;
+      case 'd':           t->curs_row  = t->lines + param[0] - 1; break;
     }
 
-    clamp_cursor_to_bounds(rt);
+    clamp_cursor_to_bounds(t);
 }
 
 /* Interpret the 'erase line' escape sequence */
-static void interpret_csi_EL(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_EL(madtty_t *t, int param[], int pcount)
 {
-    int erase_start, erase_end, i;
-    int cmd = pcount ? param[0] : 0;
-
-    switch (cmd) {
-      case 1:  erase_start = 0;        erase_end = rt->curs_col;     break;
-      case 2:  erase_start = 0;        erase_end = rt->cols - 1; break;
-      default: erase_start = rt->curs_col; erase_end = rt->cols - 1; break;
-    }
+    attr_t attr = build_attrs(t->curattrs);
 
-    for (i = erase_start; i <= erase_end; i++) {
-        rt->cells[rt->curs_row][i].s[0]  = 0x20;
-        rt->cells[rt->curs_row][i].len   = 1;
-        rt->cells[rt->curs_row][i].attrs = rt->curattrs;
+    switch (pcount ? param[0] : 0) {
+      case 1:
+        t_row_set(t->curs_row, 0, t->curs_col + 1, attr);
+        break;
+      case 2:
+        t_row_set(t->curs_row, 0, t->cols, attr);
+        break;
+      default:
+        t_row_set(t->curs_row, t->curs_col, t->cols - t->curs_col,
+                     attr);
+        break;
     }
 }
 
 /* Interpret the 'insert blanks' sequence (ICH) */
-static void interpret_csi_ICH(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_ICH(madtty_t *t, int param[], int pcount)
 {
+    t_row_t *row = t->curs_row;
     int n = (pcount && param[0] > 0) ? param[0] : 1;
     int i;
 
-    for (i = rt->cols - 1; i >= rt->curs_col + n; i--) {
-        rt->cells[rt->curs_row][i] = rt->cells[rt->curs_row][i - n];
+    if (t->curs_col + n > t->cols) {
+        n = t->cols - t->curs_col;
     }
 
-    for (i = rt->curs_col; i < rt->curs_col + n; i++) {
-        rt->cells[rt->curs_row][i].s[0]  = 0x20;
-        rt->cells[rt->curs_row][i].len   = 1;
-        rt->cells[rt->curs_row][i].attrs = rt->curattrs;
+    for (i = t->cols - 1; i >= t->curs_col + n; i--) {
+        row->text[i] = row->text[i - n];
+        row->attr[i] = row->attr[i - n];
     }
+
+    t_row_set(row, t->curs_col, n, build_attrs(t->curattrs));
 }
 
 /* Interpret the 'delete chars' sequence (DCH) */
-static void interpret_csi_DCH(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_DCH(madtty_t *t, int param[], int pcount)
 {
+    t_row_t *row = t->curs_row;
     int n = (pcount && param[0] > 0) ? param[0] : 1;
     int i;
 
-    for (i = rt->curs_col; i < rt->cols; i++) {
-        if (i + n < rt->cols) {
-            rt->cells[rt->curs_row][i] = rt->cells[rt->curs_row][i + n];
-        } else {
-            rt->cells[rt->curs_row][i].s[0]  = 0x20;
-            rt->cells[rt->curs_row][i].len   = 1;
-            rt->cells[rt->curs_row][i].attrs = rt->curattrs;
-        }
+    if (t->curs_col + n > t->cols) {
+        n = t->cols - t->curs_col;
+    }
+
+    for (i = t->curs_col; i < t->cols - n; i++) {
+        row->text[i] = row->text[i + n];
+        row->attr[i] = row->attr[i + n];
     }
+
+    t_row_set(row, t->cols - n, n, build_attrs(t->curattrs));
+}
+
+/* Interpret a 'scroll reverse' (SR) */
+static void interpret_csi_SR(madtty_t *t)
+{
+    t_row_roll(t->scroll_top, t->scroll_bot, -1);
+    t_row_set(t->scroll_top, 0, t->cols, build_attrs(t->curattrs));
 }
 
 /* Interpret an 'insert line' sequence (IL) */
-static void interpret_csi_IL(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_IL(madtty_t *t, int param[], int pcount)
 {
     int n = (pcount && param[0] > 0) ? param[0] : 1;
-    int i, j;
 
-    for (i = rt->scrollbottom; i >= rt->curs_row + n; i--) {
-        memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols);
-    }
-
-    for (i = rt->curs_row; i < rt->curs_row + n && i <= rt->scrollbottom; i++) {
-        for (j = 0; j < rt->cols; j++) {
-            rt->cells[i][j].s[0]  = 0x20;
-            rt->cells[i][j].len   = 1;
-            rt->cells[i][j].attrs = rt->curattrs;
+    if (t->curs_row + n >= t->scroll_bot) {
+        for (t_row_t *row = t->curs_row; row < t->scroll_bot; row++) {
+            t_row_set(row, 0, t->cols, build_attrs(t->curattrs));
+        }
+    } else {
+        t_row_roll(t->curs_row, t->scroll_bot, -n);
+        for (t_row_t *row = t->curs_row; row < t->curs_row + n; row++) {
+            t_row_set(row, 0, t->cols, build_attrs(t->curattrs));
         }
     }
-
 }
 
 /* Interpret a 'delete line' sequence (DL) */
-static void interpret_csi_DL(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_DL(madtty_t *t, int param[], int pcount)
 {
     int n = (pcount && param[0] > 0) ? param[0] : 1;
-    int i, j;
 
-    for (i = rt->curs_row; i <= rt->scrollbottom; i++) {
-        if (i + n <= rt->scrollbottom) {
-            memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols);
-        } else {
-            for (j = 0; j < rt->cols; j++) {
-                rt->cells[i][j].s[0]  = 0x20;
-                rt->cells[i][j].len   = 1;
-                rt->cells[i][j].attrs = rt->curattrs;
-            }
+    if (t->curs_row + n >= t->scroll_bot) {
+        for (t_row_t *row = t->curs_row; row < t->scroll_bot; row++) {
+            t_row_set(row, 0, t->cols, build_attrs(t->curattrs));
+        }
+    } else {
+        t_row_roll(t->curs_row, t->scroll_bot, n);
+        for (t_row_t *row = t->scroll_bot - n; row < t->scroll_bot; row++) {
+            t_row_set(row, 0, t->cols, build_attrs(t->curattrs));
         }
     }
 }
 
 /* Interpret an 'erase characters' (ECH) sequence */
-static void interpret_csi_ECH(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_ECH(madtty_t *t, int param[], int pcount)
 {
     int n = (pcount && param[0] > 0) ? param[0] : 1;
-    int i;
 
-    for (i = rt->curs_col; i < rt->curs_col + n && i < rt->cols; i++) {
-        rt->cells[rt->curs_row][i].s[0]  = 0x20;
-        rt->cells[rt->curs_row][i].len   = 1;
-        rt->cells[rt->curs_row][i].attrs = rt->curattrs;
+    if (t->curs_col + n < t->cols) {
+        n = t->cols - t->curs_col;
     }
+    t_row_set(t->curs_row, t->curs_col, n, build_attrs(t->curattrs));
 }
 
 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
-static void interpret_csi_DECSTBM(madtty_t *rt, int param[], int pcount)
+static void interpret_csi_DECSTBM(madtty_t *t, int param[], int pcount)
 {
-    int newtop, newbottom;
+    int new_top, new_bot;
 
-    if (!pcount) {
-        newtop = 0;
-        newbottom = rt->rows - 1;
-    } else
-    if (pcount < 2) {
+    switch (pcount) {
+      case 0:
+        t->scroll_top = t->lines;
+        t->scroll_bot = t->lines + t->rows;
+        break;
+      default:
         return; /* malformed */
-    }
-
-    newtop = param[0] - 1;
-    newbottom = param[1] - 1;
 
-    /* clamp to bounds */
-    if (newtop < 0)
-        newtop = 0;
-    if (newtop >= rt->rows)
-        newtop = rt->rows - 1;
-    if (newbottom < 0)
-        newbottom = 0;
-    if (newbottom >= rt->rows)
-        newbottom = rt->rows - 1;
-
-    /* check for range validity */
-    if (newtop > newbottom)
-        return;
-    rt->scrolltop = newtop;
-    rt->scrollbottom = newbottom;
+      case 2:
+        new_top = param[0] - 1;
+        new_bot = param[1];
+
+        /* clamp to bounds */
+        if (new_top < 0)
+            new_top = 0;
+        if (new_top >= t->rows)
+            new_top = t->rows - 1;
+        if (new_bot < 0)
+            new_bot = 0;
+        if (new_bot >= t->rows)
+            new_bot = t->rows;
+
+        /* check for range validity */
+        if (new_top < new_bot) {
+            t->scroll_top = t->lines + new_top;
+            t->scroll_bot = t->lines + new_bot;
+        }
+        break;
+    }
 }
 
-static void es_interpret_csi(madtty_t *rt)
+static void es_interpret_csi(madtty_t *t)
 {
-    static int csiparam[MAX_CSI_ES_PARAMS];
+    static int csiparam[BUFSIZ];
     int param_count = 0;
-    const char *p = rt->esbuf + 1;
-    char verb = rt->esbuf[rt->esbuf_len - 1];
+    const char *p = t->ebuf + 1;
+    char verb = t->ebuf[t->elen - 1];
 
-    if (!strncmp(rt->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
-        return;
-    }
+    p += t->ebuf[1] == '?'; /* CSI private mode */
 
     /* parse numeric parameters */
     while (isdigit((unsigned char)*p) || *p == ';') {
         if (*p == ';') {
-            if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
+            if (param_count >= (int)sizeof(csiparam))
+                return; /* too long! */
             csiparam[param_count++] = 0;
         } else {
             if (param_count == 0) csiparam[param_count++] = 0;
@@ -516,277 +532,492 @@ static void es_interpret_csi(madtty_t *rt)
         p++;
     }
 
+    if (t->ebuf[1] == '?') {
+        switch (verb) {
+          case 'l':
+            if (csiparam[0] == 25)
+                t->curshid = true;
+            break;
+
+          case 'h':
+            if (csiparam[0] == 25)
+                t->curshid = false;
+            break;
+        }
+    }
+
     /* delegate handling depending on command character (verb) */
     switch (verb) {
       case 'h':
         if (param_count == 1 && csiparam[0] == 4) /* insert mode */
-            rt->insert = true;
+            t->insert = true;
         break;
       case 'l':
         if (param_count == 1 && csiparam[0] == 4) /* replace mode */
-            rt->insert = false;
+            t->insert = false;
         break;
       case 'm': /* it's a 'set attribute' sequence */
-        interpret_csi_SGR(rt, csiparam, param_count); break;
+        interpret_csi_SGR(t, csiparam, param_count); break;
       case 'J': /* it's an 'erase display' sequence */
-        interpret_csi_ED(rt, csiparam, param_count); break;
+        interpret_csi_ED(t, csiparam, param_count); break;
       case 'H': case 'f': /* it's a 'move cursor' sequence */
-        interpret_csi_CUP(rt, csiparam, param_count); break;
+        interpret_csi_CUP(t, csiparam, param_count); break;
       case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
       case 'e': case 'a': case 'd': case '`':
         /* it is a 'relative move' */
-        interpret_csi_C(rt, verb, csiparam, param_count); break;
+        interpret_csi_C(t, verb, csiparam, param_count); break;
       case 'K': /* erase line */
-        interpret_csi_EL(rt, csiparam, param_count); break;
+        interpret_csi_EL(t, csiparam, param_count); break;
       case '@': /* insert characters */
-        interpret_csi_ICH(rt, csiparam, param_count); break;
+        interpret_csi_ICH(t, csiparam, param_count); break;
       case 'P': /* delete characters */
-        interpret_csi_DCH(rt, csiparam, param_count); break;
+        interpret_csi_DCH(t, csiparam, param_count); break;
       case 'L': /* insert lines */
-        interpret_csi_IL(rt, csiparam, param_count); break;
+        interpret_csi_IL(t, csiparam, param_count); break;
       case 'M': /* delete lines */
-        interpret_csi_DL(rt, csiparam, param_count); break;
+        interpret_csi_DL(t, csiparam, param_count); break;
       case 'X': /* erase chars */
-        interpret_csi_ECH(rt, csiparam, param_count); break;
+        interpret_csi_ECH(t, csiparam, param_count); break;
       case 'r': /* set scrolling region */
-        interpret_csi_DECSTBM(rt, csiparam, param_count); break;
+        interpret_csi_DECSTBM(t, csiparam, param_count); break;
       case 's': /* save cursor location */
-        rt->curs_srow = rt->curs_col;
-        rt->curs_scol = rt->curs_row;
+        t->curs_srow = t->curs_row - t->lines;
+        t->curs_scol = t->curs_col;
         break;
       case 'u': /* restore cursor location */
-        rt->curs_col = rt->curs_srow;
-        rt->curs_row = rt->curs_scol;
+        t->curs_row = t->lines + t->curs_srow;
+        t->curs_col = t->curs_scol;
+        clamp_cursor_to_bounds(t);
         break;
       default:
         break;
     }
 }
 
-static void try_interpret_escape_seq(madtty_t *rt)
+static void try_interpret_escape_seq(madtty_t *t)
 {
-    char firstchar = rt->esbuf[0];
-    char lastchar  = rt->esbuf[rt->esbuf_len-1];
+    char lastchar  = t->ebuf[t->elen-1];
 
-    if (!firstchar)
-        return;  /* too early to do anything */
+    switch (*t->ebuf) {
+      case '\0':
+        return;
 
-    /* interpret ESC-M as reverse line-feed */
-    if (firstchar == 'M') {
-        cursor_line_up(rt);
-        cancel_escape_sequence(rt);
+      case 'M':
+        interpret_csi_SR(t);
+        cancel_escape_sequence(t);
         return;
+
+      case '(':
+      case ')':
+        if (t->elen == 2)
+            goto cancel;
+        break;
+
+      case ']': /* xterm thing */
+        if (lastchar == '\a')
+            goto cancel;
+        break;
+
+      default:
+        goto cancel;
+
+      case '[':
+        if (is_valid_csi_ender(lastchar)) {
+            es_interpret_csi(t);
+            cancel_escape_sequence(t);
+            return;
+        }
+        break;
     }
 
-    if (firstchar != '[' && firstchar != ']') {
-        /* unrecognized escape sequence. Let's forget about it. */
-        cancel_escape_sequence(rt);
-        return;
+    if (t->elen + 1 >= (int)sizeof(t->ebuf)) {
+cancel:
+#ifndef NDEBUG
+        fprintf(stderr, "cancelled: \\033");
+        for (int i = 0; i < (int)t->elen; i++) {
+            if (isprint(t->ebuf[i])) {
+                fputc(t->ebuf[i], stderr);
+            } else {
+                fprintf(stderr, "\\%03o", t->ebuf[i]);
+            }
+        }
+        fputc('\n', stderr);
+#endif
+        cancel_escape_sequence(t);
     }
+}
 
-    if (firstchar == '[' && is_valid_csi_ender(lastchar)) {
-        es_interpret_csi(rt);
-        cancel_escape_sequence(rt);
-    } else if (firstchar == ']' && lastchar == '\a') {
-        /* we have an xterm escape sequence: interpret it */
+static void madtty_process_nonprinting(madtty_t *t, wchar_t wc)
+{
+    switch (wc) {
+      case C0_ESC:
+        new_escape_sequence(t);
+        break;
 
-        /* es_interpret_xterm_es(rt);     -- TODO!*/
-        cancel_escape_sequence(rt);
+      case C0_BEL:
+        /* do nothing for now... maybe a visual bell would be nice? */
+        break;
+
+      case C0_BS:
+        if (t->curs_col > 0)
+            t->curs_col--;
+        break;
+
+      case C0_HT: /* tab */
+        t->curs_col = (t->curs_col + 8) & ~7;
+        if (t->curs_col >= t->cols)
+            t->curs_col = t->cols - 1;
+        break;
+
+      case C0_CR:
+        t->curs_col = 0;
+        break;
+
+      case C0_VT:
+      case C0_FF:
+      case C0_LF:
+        cursor_line_down(t);
+        break;
+
+      case C0_SO:              /* shift out - acs */
+        t->graphmode = true;
+        break;
+      case C0_SI:              /* shift in - acs */
+        t->graphmode = false;
+        break;
     }
+}
 
-    /* if the escape sequence took up all available space and could
-     * not yet be parsed, abort it */
-    if (rt->esbuf_len + 1 >= ESEQ_BUF_SIZE)
-        cancel_escape_sequence(rt);
+static void is_utf8_locale(void)
+{
+    const char *cset = nl_langinfo(CODESET) ?: "ANSI_X3.4-1968";
+    is_utf8 = !strcmp(cset, "UTF-8");
 }
 
-int madtty_inject(madtty_t *rt, const char *data, int len)
+// vt100 special graphics and line drawing
+// 5f-7e standard vt100
+// 40-5e rxvt extension for extra curses acs chars
+static uint16_t const vt100_utf8[62] = { // 41 .. 7e
+            0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47 hi mr. snowman!
+         0,      0,      0,      0,      0,      0,      0,      0, // 48-4f
+         0,      0,      0,      0,      0,      0,      0,      0, // 50-57
+         0,      0,      0,      0,      0,      0,      0, 0x0020, // 58-5f
+    0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1, // 60-67
+    0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, // 68-6f
+    0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, // 70-77
+    0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7,         // 78-7e
+};
+
+static uint32_t vt100[62];
+
+void madtty_init_vt100_graphics(void)
 {
-    int pos;
+    vt100['l' - 0x41] = ACS_ULCORNER;
+    vt100['m' - 0x41] = ACS_LLCORNER;
+    vt100['k' - 0x41] = ACS_URCORNER;
+    vt100['j' - 0x41] = ACS_LRCORNER;
+    vt100['u' - 0x41] = ACS_RTEE;
+    vt100['t' - 0x41] = ACS_LTEE;
+    vt100['v' - 0x41] = ACS_TTEE;
+    vt100['w' - 0x41] = ACS_BTEE;
+    vt100['q' - 0x41] = ACS_HLINE;
+    vt100['x' - 0x41] = ACS_VLINE;
+    vt100['n' - 0x41] = ACS_PLUS;
+    vt100['o' - 0x41] = ACS_S1;
+    vt100['s' - 0x41] = ACS_S9;
+    vt100['`' - 0x41] = ACS_DIAMOND;
+    vt100['a' - 0x41] = ACS_CKBOARD;
+    vt100['f' - 0x41] = ACS_DEGREE;
+    vt100['g' - 0x41] = ACS_PLMINUS;
+    vt100['~' - 0x41] = ACS_BULLET;
+    vt100[',' - 0x41] = ACS_LARROW;
+    vt100['+' - 0x41] = ACS_RARROW;
+    vt100['.' - 0x41] = ACS_DARROW;
+    vt100['-' - 0x41] = ACS_UARROW;
+    vt100['h' - 0x41] = ACS_BOARD;
+    vt100['i' - 0x41] = ACS_LANTERN;
+    vt100['0' - 0x41] = ACS_BLOCK;
+    /* these defaults were invented for ncurses */
+    vt100['p' - 0x41] = ACS_S3;
+    vt100['r' - 0x41] = ACS_S7;
+    vt100['y' - 0x41] = ACS_LEQUAL;
+    vt100['z' - 0x41] = ACS_GEQUAL;
+    vt100['{' - 0x41] = ACS_PI;
+    vt100['|' - 0x41] = ACS_NEQUAL;
+    vt100['}' - 0x41] = ACS_STERLING;
+    is_utf8_locale();
+}
 
-    for (pos = 0; pos < len; pos++) {
-        if ((unsigned char)data[pos] <= 31) {
-            handle_control_char(rt, data[pos]);
-            continue;
-        }
+static void madtty_putc(madtty_t *t, wchar_t wc)
+{
+    int width = 0;
 
-        if (rt->escaped && rt->esbuf_len < ESEQ_BUF_SIZE) {
-            /* append character to ongoing escape sequence */
-            rt->esbuf[rt->esbuf_len] = data[pos];
-            rt->esbuf[++rt->esbuf_len] = 0;
+    if (!t->seen_input) {
+        t->seen_input = 1;
+        kill(-t->childpid, SIGWINCH);
+    }
+
+    if (t->escaped) {
+        assert (t->elen + 1 < (int)sizeof(t->ebuf));
+        t->ebuf[t->elen]   = wc;
+        t->ebuf[++t->elen] = '\0';
+        try_interpret_escape_seq(t);
+    } else if (IS_CONTROL(wc)) {
+        madtty_process_nonprinting(t, wc);
+    } else {
+        t_row_t *tmp;
 
-            try_interpret_escape_seq(rt);
-        } else
-        if (rt->graphmode) {
-            put_graphmode_char(rt, data[pos]);
+        if (t->graphmode) {
+            if (wc >= 0x41 && wc <= 0x7e) {
+                wchar_t gc = is_utf8 ? vt100_utf8[wc - 0x41] : vt100[wc - 0x41];
+                if (gc)
+                    wc = gc;
+            }
+            width = 1;
         } else {
-            static int8_t const lens[5] = { 1, -1, 2, 3, 4 };
-            int bsf = __builtin_clz(~((unsigned char)data[pos] << 24));
+            width = wcwidth(wc) ?: 1;
+        }
+
+        if (width == 2 && t->curs_col == t->cols - 1) {
+            tmp = t->curs_row;
+            tmp->dirty = true;
+            tmp->text[t->curs_col] = 0;
+            tmp->attr[t->curs_col] = build_attrs(t->curattrs);
+            t->curs_col++;
+        }
+
+        if (t->curs_col >= t->cols) {
+            t->curs_col = 0;
+            cursor_line_down(t);
+        }
+
+        tmp = t->curs_row;
+        tmp->dirty = true;
+
+        if (t->insert) {
+            wmemmove(tmp->text + t->curs_col + width, tmp->text + t->curs_col,
+                     (t->cols - t->curs_col - width));
+            memmove(tmp->attr + t->curs_col + width, tmp->attr + t->curs_col,
+                    (t->cols - t->curs_col - width) * sizeof(tmp->attr[0]));
+        }
 
-            if (pos + lens[bsf] > len)
-                return pos;
+        tmp->text[t->curs_col] = wc;
+        tmp->attr[t->curs_col] = build_attrs(t->curattrs);
+        t->curs_col++;
+        if (width == 2) {
+            tmp->text[t->curs_col] = 0;
+            tmp->attr[t->curs_col] = build_attrs(t->curattrs);
+            t->curs_col++;
+        }
+    }
+}
+
+int madtty_process(madtty_t *t)
+{
+    int res, pos = 0;
+
+    if (t->pty < 0) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    res = read(t->pty, t->rbuf + t->rlen, sizeof(t->rbuf) - t->rlen);
+    if (res < 0)
+        return -1;
+
+    t->rlen += res;
+    while (pos < t->rlen) {
+        wchar_t wc;
+        ssize_t len;
 
-            put_normal_char(rt, data + pos, lens[bsf]);
-            pos += lens[bsf] - 1;
+        len = (ssize_t)mbrtowc(&wc, t->rbuf + pos, t->rlen - pos, &t->ps);
+        if (len == -2) {
+            t->rlen -= pos;
+            memmove(t->rbuf, t->rbuf + pos, t->rlen);
+            return 0;
         }
+
+        if (len == -1) {
+            len = 1;
+            wc  = t->rbuf[pos];
+        }
+
+        pos += len ? len : 1;
+        madtty_putc(t, wc);
     }
 
-    return len;
+    t->rlen -= pos;
+    memmove(t->rbuf, t->rbuf + pos, t->rlen);
+    return 0;
 }
 
 madtty_t *madtty_create(int rows, int cols)
 {
-    madtty_t *rt;
+    madtty_t *t;
     int i;
 
     if (rows <= 0 || cols <= 0)
         return NULL;
 
-    rt = (madtty_t*)calloc(sizeof(madtty_t), 1);
-    if (!rt)
+    t = (madtty_t*)calloc(sizeof(madtty_t), 1);
+    if (!t)
         return NULL;
 
     /* record dimensions */
-    rt->rows = rows;
-    rt->cols = cols;
+    t->rows = rows;
+    t->cols = cols;
 
     /* default mode is replace */
-    rt->insert = false; 
+    t->insert = false;
 
     /* create the cell matrix */
-    rt->cells = (RoteCell**)calloc(sizeof(RoteCell*), rt->rows);
-    for (i = 0; i < rt->rows; i++) {
-        rt->cells[i] = (RoteCell*)calloc(sizeof(RoteCell), rt->cols);
+    t->lines = (t_row_t*)calloc(sizeof(t_row_t), t->rows);
+    for (i = 0; i < t->rows; i++) {
+        t->lines[i].text = (wchar_t *)calloc(sizeof(wchar_t), t->cols);
+        t->lines[i].attr = (uint16_t *)calloc(sizeof(uint16_t), t->cols);
     }
 
-    /* initialization of other public fields */
-    rt->curs_row = rt->curs_col = 0;
-    rt->curattrs = A_NORMAL;  /* white text over black background */
+    t->pty = -1;  /* no pty for now */
 
-    rt->pty = -1;  /* no pty for now */
+    /* initialization of other public fields */
+    t->curs_row = t->lines;
+    t->curs_col = 0;
+    t->curattrs = A_NORMAL;  /* white text over black background */
 
     /* initial scrolling area is the whole window */
-    rt->scrolltop = 0;
-    rt->scrollbottom = rt->rows - 1;
+    t->scroll_top = t->lines;
+    t->scroll_bot = t->lines + t->rows;
+
+    return t;
+}
+
+void madtty_resize(madtty_t *t, int rows, int cols)
+{
+    struct winsize ws = { .ws_row = rows, .ws_col = cols };
+    t_row_t *lines = t->lines;
+
+    if (rows <= 0 || cols <= 0)
+        return;
+
+    if (t->rows != rows) {
+        while (t->rows > rows) {
+            free(lines[t->rows - 1].text);
+            free(lines[t->rows - 1].attr);
+            t->rows--;
+        }
+
+        lines = realloc(lines, sizeof(t_row_t) * rows);
+    }
+
+    if (t->cols != cols) {
+        for (int row = 0; row < t->rows; row++) {
+            lines[row].text = realloc(lines[row].text, sizeof(wchar_t) * cols);
+            lines[row].attr = realloc(lines[row].attr, sizeof(uint16_t) * cols);
+            if (t->cols < cols)
+                t_row_set(lines + row, t->cols, cols - t->cols, 0);
+            else
+                lines[row].dirty = true;
+        }
+        t->cols = cols;
+    }
+
+    while (t->rows < rows) {
+        lines[t->rows].text = (wchar_t *)calloc(sizeof(wchar_t), cols);
+        lines[t->rows].attr = (uint16_t *)calloc(sizeof(uint16_t), cols);
+        t_row_set(lines + t->rows, 0, t->cols, 0);
+        t->rows++;
+    }
 
-    return rt;
+    t->curs_row   += lines - t->lines;
+    t->scroll_top = lines;
+    t->scroll_bot = lines + rows;
+    t->lines = lines;
+    clamp_cursor_to_bounds(t);
+    ioctl(t->pty, TIOCSWINSZ, &ws);
+    kill(-t->childpid, SIGWINCH);
 }
 
-void madtty_destroy(madtty_t *rt)
+void madtty_destroy(madtty_t *t)
 {
     int i;
-    if (!rt)
+    if (!t)
         return;
 
-    for (i = 0; i < rt->rows; i++) {
-        free(rt->cells[i]);
+    for (i = 0; i < t->rows; i++) {
+        free(t->lines[i].text);
+        free(t->lines[i].attr);
     }
-    free(rt->cells);
-    free(rt);
+    free(t->lines);
+    free(t);
 }
 
-void madtty_draw(madtty_t *rt, WINDOW *win, int srow, int scol)
+void madtty_draw(madtty_t *t, WINDOW *win, int srow, int scol)
 {
-    int i, j;
+    curs_set(0);
+    for (int i = 0; i < t->rows; i++) {
+        t_row_t *row = t->lines + i;
+
+
+        if (!row->dirty)
+            continue;
 
-    for (i = 0; i < rt->rows; i++) {
         wmove(win, srow + i, scol);
-        for (j = 0; j < rt->cols; j++) {
-            wattrset(win, (rt->cells[i][j].attrs & ~077) | COLOR_PAIR(rt->cells[i][j].attrs & 077));
-            if (rt->cells[i][j].len && rt->cells[i][j].s[0] >= ' ') {
-                waddnstr(win, rt->cells[i][j].s, rt->cells[i][j].len);
+        for (int j = 0; j < t->cols; j++) {
+            if (!j || row->attr[j] != row->attr[j - 1])
+                wattrset(win, (attr_t)row->attr[j] << NCURSES_ATTR_SHIFT);
+            if (row->text[j] >= 128) {
+                char buf[MB_CUR_MAX + 1];
+                int len;
+
+                len = wcrtomb(buf, row->text[j], NULL);
+                waddnstr(win, buf, len);
+                if (wcwidth(row->text[j]) > 1)
+                    j++;
             } else {
-                waddch(win, ' ');
+                waddch(win, row->text[j] > ' ' ? row->text[j] : ' ');
             }
         }
+        row->dirty = false;
     }
 
-    wmove(win, srow + rt->curs_row, scol + rt->curs_col);
+    wmove(win, srow + t->curs_row - t->lines, scol + t->curs_col);
+    curs_set(!t->curshid);
 }
 
 /******************************************************/
 
-pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[])
+pid_t madtty_forkpty(madtty_t *t, const char *p, const char *argv[], int *pty)
 {
     struct winsize ws;
     pid_t pid;
 
-    ws.ws_row    = rt->rows;
-    ws.ws_col    = rt->cols;
+    ws.ws_row    = t->rows;
+    ws.ws_col    = t->cols;
     ws.ws_xpixel = ws.ws_ypixel = 0;
 
-    pid = forkpty(&rt->pty, NULL, NULL, &ws);
+    pid = forkpty(&t->pty, NULL, NULL, &ws);
     if (pid < 0)
         return -1;
 
     if (pid == 0) {
         setsid();
-
-        setenv("TERM", "linux", 1);
-        execv(path, (char *const*)argv);
+        setenv("TERM", "rxvt", 1);
+        execv(p, (char *const*)argv);
         fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]);
         exit(1);
     }
 
-    return rt->childpid = pid;
+    if (pty)
+        *pty = t->pty;
+    return t->childpid = pid;
 }
 
-int madtty_read(madtty_t *rt, char *buf, int buflen)
+int madtty_getpty(madtty_t *t)
 {
-    if (rt->pty < 0) {
-        errno = EINVAL;
-        return -1;
-    }
-
-    return read(rt->pty, buf, buflen);
+    return t->pty;
 }
 
-int madtty_write(madtty_t *rt, const char *data, int len)
-{
-    int res;
-
-    if (rt->pty < 0) {
-        errno = EINVAL;
-        return -1;
-    }
-
-again:
-    res = write(rt->pty, data, len);
-    if (res < 0) {
-        if (errno == EINTR || errno == EAGAIN)
-            goto again;
-    }
-
-    return res;
-}
-
-static char const * const keytable[KEY_MAX+1] = {
-    ['\n']          = "\r",
-    [KEY_UP]        = "\e[A",
-    [KEY_DOWN]      = "\e[B",
-    [KEY_RIGHT]     = "\e[C",
-    [KEY_LEFT]      = "\e[D",
-    [KEY_BACKSPACE] = "\b",
-    [KEY_HOME]      = "\e[1~",
-    [KEY_IC]        = "\e[2~",
-    [KEY_DC]        = "\e[3~",
-    [KEY_END]       = "\e[4~",
-    [KEY_PPAGE]     = "\e[5~",
-    [KEY_NPAGE]     = "\e[6~",
-    [KEY_SUSPEND]   = "\x1A",  /* Ctrl+Z gets mapped to this */
-    [KEY_F(1)]      = "\e[[A",
-    [KEY_F(2)]      = "\e[[B",
-    [KEY_F(3)]      = "\e[[C",
-    [KEY_F(4)]      = "\e[[D",
-    [KEY_F(5)]      = "\e[[E",
-    [KEY_F(6)]      = "\e[17~",
-    [KEY_F(7)]      = "\e[18~",
-    [KEY_F(8)]      = "\e[19~",
-    [KEY_F(9)]      = "\e[20~",
-    [KEY_F(10)]     = "\e[21~",
-};
-
-void madtty_keypress(madtty_t *rt, int keycode)
+void madtty_keypress(madtty_t *t, int keycode)
 {
     char c = (char)keycode;
     const char *buf;
@@ -801,8 +1032,8 @@ void madtty_keypress(madtty_t *rt, int keycode)
     }
 
     while (len > 0) {
-        int res = madtty_write(rt, buf, len);
-        if (res < 0)
+        int res = write(t->pty, buf, len);
+        if (res < 0 && errno != EAGAIN && errno != EINTR)
             return;
 
         buf += res;
@@ -810,16 +1041,45 @@ void madtty_keypress(madtty_t *rt, int keycode)
     }
 }
 
-void madtty_initialize(void)
+void madtty_init_colors(void)
 {
-    setlocale(LC_ALL, "");
-    initscr();
-    noecho();
-    start_color();
-    raw();
-    nodelay(stdscr, TRUE);
-    keypad(stdscr, TRUE);
+    if (COLOR_PAIRS > 64) {
+        use_default_colors();
+        has_default = 1;
+
+        for (int bg = -1; bg < 8; bg++) {
+            for (int fg = -1; fg < 8; fg++) {
+                init_pair((fg + 1) * 16 + bg + 1, fg, bg);
+            }
+        }
+    } else {
+        int use_default = use_default_colors() == OK;
+        for (int bg = 0; bg < 8; bg++) {
+            for (int fg = 0; fg < 8; fg++) {
+                if (use_default) {
+                    init_pair((7 - fg) * 8 + bg,
+                              fg == COLOR_WHITE ? -1 : fg,
+                              bg == COLOR_BLACK ? -1 : bg);
+                } else {
+                    init_pair((7 - fg) * 8 + bg, fg, bg);
+                }
+            }
+        }
+    }
+}
 
-    for (int i = 0; i < 8 * 8; i++)
-        init_pair(i, i >> 3, i & 7);
+int madtty_color_pair(int fg, int bg)
+{
+    if (has_default) {
+        if (fg < -1)
+            fg = -1;
+        if (bg < -1)
+            bg = -1;
+        return COLOR_PAIR((fg + 1) * 16 + bg + 1);
+    }
+    if (fg < 0)
+        fg = COLOR_WHITE;
+    if (bg < 0)
+        bg = COLOR_BLACK;
+    return COLOR_PAIR((7 - fg) * 8 + bg);
 }