Code cleansing
authorPierre Habouzit <madcoder@debian.org>
Sat, 10 Nov 2007 14:39:00 +0000 (15:39 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sat, 10 Nov 2007 14:39:51 +0000 (15:39 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
demo/boxshell.c
madtty/madtty.c
madtty/madtty.h

index 76af914..505f6b9 100644 (file)
@@ -56,7 +56,7 @@ static int is_expired(struct timeval now, struct timeval expiry)
 int main(void)
 {
     madtty_t *rt;
 int main(void)
 {
     madtty_t *rt;
-    int dirty = 0;
+    int dirty = 0, pty;
     struct timeval next;
 
     signal(SIGCHLD, handler);
     struct timeval next;
 
     signal(SIGCHLD, handler);
@@ -72,7 +72,7 @@ int main(void)
         const char *path = getenv("SHELL") ?: "/bin/sh";
         const char *args[] = { path, "--login", NULL};
 
         const char *path = getenv("SHELL") ?: "/bin/sh";
         const char *args[] = { path, "--login", NULL};
 
-        madtty_forkpty(rt, path, args);
+        madtty_forkpty(rt, path, args, &pty);
     }
 
     /* keep reading keypresses from the user and passing them to the terminal;
     }
 
     /* keep reading keypresses from the user and passing them to the terminal;
@@ -85,10 +85,10 @@ int main(void)
 
         FD_ZERO(&rfds);
         FD_SET(0, &rfds);
 
         FD_ZERO(&rfds);
         FD_SET(0, &rfds);
-        FD_SET(rt->pty, &rfds);
+        FD_SET(pty, &rfds);
 
 
-        if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) {
-            if (FD_ISSET(rt->pty, &rfds)) {
+        if (select(pty + 1, &rfds, NULL, NULL, &tv) > 0) {
+            if (FD_ISSET(pty, &rfds)) {
                 madtty_process(rt);
                 dirty = 1;
             }
                 madtty_process(rt);
                 dirty = 1;
             }
index 8a3d26f..ce21d32 100644 (file)
@@ -69,11 +69,41 @@ enum {
     CSI_78 , CSI_79 , CSI_7A , CSI_7B , CSI_7C , CSI_7D , CSI_7E , CSI_7F
 };
 
     CSI_78 , CSI_79 , CSI_7A , CSI_7B , CSI_7C , CSI_7D , CSI_7E , CSI_7F
 };
 
-struct mtty_row_t {
+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;
     wchar_t  *text;
     uint16_t *attr;
     unsigned dirty : 1;
-};
+} t_row_t;
 
 static char const * const keytable[KEY_MAX+1] = {
     ['\n']          = "\r",
 
 static char const * const keytable[KEY_MAX+1] = {
     ['\n']          = "\r",
@@ -101,7 +131,7 @@ static char const * const keytable[KEY_MAX+1] = {
     [KEY_F(10)]     = "\e[21~",
 };
 
     [KEY_F(10)]     = "\e[21~",
 };
 
-static void mtty_row_set(mtty_row_t *row, int start, int len, uint16_t attr)
+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);
 {
     row->dirty = true;
     wmemset(row->text + start, 0, len);
@@ -110,7 +140,7 @@ static void mtty_row_set(mtty_row_t *row, int start, int len, uint16_t attr)
     }
 }
 
     }
 }
 
-static void mtty_row_roll(mtty_row_t *start, mtty_row_t *end, int count)
+static void t_row_roll(t_row_t *start, t_row_t *end, int count)
 {
     int n = end - start;
 
 {
     int n = end - start;
 
@@ -119,43 +149,43 @@ static void mtty_row_roll(mtty_row_t *start, mtty_row_t *end, int count)
         count += n;
 
     if (count) {
         count += n;
 
     if (count) {
-        mtty_row_t *buf = alloca(count * sizeof(mtty_row_t));
+        t_row_t *buf = alloca(count * sizeof(t_row_t));
 
 
-        memcpy(buf, start, count * sizeof(mtty_row_t));
-        memmove(start, start + count, (n - count) * sizeof(mtty_row_t));
-        memcpy(end - count, buf, count * sizeof(mtty_row_t));
-        for (mtty_row_t *row = start; row < end; row++) {
+        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;
         }
     }
 }
 
             row->dirty = true;
         }
     }
 }
 
-static void clamp_cursor_to_bounds(madtty_t *rt)
+static void clamp_cursor_to_bounds(madtty_t *t)
 {
 {
-    if (rt->curs_row < rt->lines) {
-        rt->curs_row = rt->lines;
+    if (t->curs_row < t->lines) {
+        t->curs_row = t->lines;
     }
     }
-    if (rt->curs_row >= rt->lines + rt->rows) {
-        rt->curs_row = rt->lines + rt->rows - 1;
+    if (t->curs_row >= t->lines + t->rows) {
+        t->curs_row = t->lines + t->rows - 1;
     }
 
     }
 
-    if (rt->curs_col < 0) {
-        rt->curs_col = 0;
+    if (t->curs_col < 0) {
+        t->curs_col = 0;
     }
     }
-    if (rt->curs_col >= rt->cols) {
-        rt->curs_col = rt->cols - 1;
+    if (t->curs_col >= t->cols) {
+        t->curs_col = t->cols - 1;
     }
 }
 
     }
 }
 
-static void cursor_line_down(madtty_t *rt)
+static void cursor_line_down(madtty_t *t)
 {
 {
-    rt->curs_row++;
-    if (rt->curs_row < rt->scroll_bot)
+    t->curs_row++;
+    if (t->curs_row < t->scroll_bot)
         return;
 
         return;
 
-    rt->curs_row = rt->scroll_bot - 1;
-    mtty_row_roll(rt->scroll_top, rt->scroll_bot, 1);
-    mtty_row_set(rt->curs_row, 0, rt->cols, 0);
+    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);
 }
 
 __attribute__((const))
 }
 
 __attribute__((const))
@@ -165,18 +195,18 @@ static uint16_t build_attrs(unsigned curattrs)
         >> NCURSES_ATTR_SHIFT;
 }
 
         >> NCURSES_ATTR_SHIFT;
 }
 
-static void new_escape_sequence(madtty_t *rt)
+static void new_escape_sequence(madtty_t *t)
 {
 {
-    rt->escaped = true;
-    rt->elen    = 0;
-    rt->ebuf[0] = '\0';
+    t->escaped = true;
+    t->elen    = 0;
+    t->ebuf[0] = '\0';
 }
 
 }
 
-static void cancel_escape_sequence(madtty_t *rt)
+static void cancel_escape_sequence(madtty_t *t)
 {
 {
-    rt->escaped = false;
-    rt->elen    = 0;
-    rt->ebuf[0] = '\0';
+    t->escaped = false;
+    t->elen    = 0;
+    t->ebuf[0] = '\0';
 }
 
 static bool is_valid_csi_ender(int c)
 }
 
 static bool is_valid_csi_ender(int c)
@@ -187,58 +217,58 @@ static bool is_valid_csi_ender(int c)
 }
 
 /* interprets a 'set attribute' (SGR) CSI escape sequence */
 }
 
 /* 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 */
 {
     int i;
 
     if (pcount == 0) {
         /* special case: reset attributes */
-        rt->curattrs = A_NORMAL;
+        t->curattrs = A_NORMAL;
         return;
     }
 
     for (i = 0; i < pcount; i++) {
         switch (param[i]) {
 #define CASE(x, op)  case x: op; break
         return;
     }
 
     for (i = 0; i < pcount; i++) {
         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(6,  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(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) {
 
           case 30 ... 37: /* fg */
             if (has_default) {
-                rt->curattrs &= ~0xf0;
-                rt->curattrs |= (param[i] + 1 - 30) << 4;
+                t->curattrs &= ~0xf0;
+                t->curattrs |= (param[i] + 1 - 30) << 4;
             } else {
             } else {
-                rt->curattrs &= ~070;
-                rt->curattrs |= (7 - (param[i] - 30)) << 3;
+                t->curattrs &= ~070;
+                t->curattrs |= (7 - (param[i] - 30)) << 3;
             }
             break;
 
           case 39:
             }
             break;
 
           case 39:
-            rt->curattrs &= has_default ? ~0xf0 : ~070;
+            t->curattrs &= has_default ? ~0xf0 : ~070;
             break;
 
           case 40 ... 47: /* bg */
             if (has_default) {
             break;
 
           case 40 ... 47: /* bg */
             if (has_default) {
-                rt->curattrs &= ~0x0f;
-                rt->curattrs |= (param[i] + 1 - 40);
+                t->curattrs &= ~0x0f;
+                t->curattrs |= (param[i] + 1 - 40);
             } else {
             } else {
-                rt->curattrs &= ~007;
-                rt->curattrs |= (param[i] - 40);
+                t->curattrs &= ~007;
+                t->curattrs |= (param[i] - 40);
             }
             break;
 
           case 49:
             }
             break;
 
           case 49:
-            rt->curattrs &= has_default ? ~0x0f : ~007;
+            t->curattrs &= has_default ? ~0x0f : ~007;
             break;
 
           default:
             break;
 
           default:
@@ -248,189 +278,190 @@ static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
 }
 
 /* interprets an 'erase display' (ED) escape sequence */
 }
 
 /* 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)
 {
 {
-    mtty_row_t *row, *start, *end;
-    attr_t attr = build_attrs(rt->curattrs);
+    t_row_t *row, *start, *end;
+    attr_t attr = build_attrs(t->curattrs);
 
     /* decide range */
     if (pcount && param[0] == 2) {
 
     /* decide range */
     if (pcount && param[0] == 2) {
-        start = rt->lines;
-        end   = rt->lines + rt->rows;
+        start = t->lines;
+        end   = t->lines + t->rows;
     } else
     if (pcount && param[0] == 1) {
     } else
     if (pcount && param[0] == 1) {
-        start = rt->lines;
-        end   = rt->curs_row;
-        mtty_row_set(rt->curs_row, 0, rt->curs_col + 1, attr);
+        start = t->lines;
+        end   = t->curs_row;
+        t_row_set(t->curs_row, 0, t->curs_col + 1, attr);
     } else {
     } else {
-        mtty_row_set(rt->curs_row, rt->curs_col,
-                     rt->cols - rt->curs_col, attr);
-        start = rt->curs_row + 1;
-        end   = rt->lines + rt->rows;
+        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++) {
     }
 
     for (row = start; row < end; row++) {
-        mtty_row_set(row, 0, rt->cols, attr);
+        t_row_set(row, 0, t->cols, attr);
     }
 }
 
 /* interprets a 'move cursor' (CUP) escape sequence */
     }
 }
 
 /* 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 */
 {
     if (pcount == 0) {
         /* special case */
-        rt->curs_row = rt->lines;
-        rt->curs_col = 0;
+        t->curs_row = t->lines;
+        t->curs_col = 0;
         return;
     } else
     if (pcount < 2) {
         return;  /* malformed */
     }
 
         return;
     } else
     if (pcount < 2) {
         return;  /* malformed */
     }
 
-    rt->curs_row = rt->lines + 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 */
 }
 
 /* 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) {
 {
     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  = rt->lines + 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 */
 }
 
 /* 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)
 {
 {
-    attr_t attr = build_attrs(rt->curattrs);
+    attr_t attr = build_attrs(t->curattrs);
 
     switch (pcount ? param[0] : 0) {
       case 1:
 
     switch (pcount ? param[0] : 0) {
       case 1:
-        mtty_row_set(rt->curs_row, 0, rt->curs_col + 1, attr);
+        t_row_set(t->curs_row, 0, t->curs_col + 1, attr);
         break;
       case 2:
         break;
       case 2:
-        mtty_row_set(rt->curs_row, 0, rt->cols, attr);
+        t_row_set(t->curs_row, 0, t->cols, attr);
         break;
       default:
         break;
       default:
-        mtty_row_set(rt->curs_row, rt->curs_col, rt->cols - rt->curs_col,
+        t_row_set(t->curs_row, t->curs_col, t->cols - t->curs_col,
                      attr);
         break;
     }
 }
 
 /* Interpret the 'insert blanks' sequence (ICH) */
                      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)
 {
 {
-    mtty_row_t *row = rt->curs_row;
+    t_row_t *row = t->curs_row;
     int n = (pcount && param[0] > 0) ? param[0] : 1;
     int i;
 
     int n = (pcount && param[0] > 0) ? param[0] : 1;
     int i;
 
-    if (rt->curs_col + n > rt->cols) {
-        n = rt->cols - rt->curs_col;
+    if (t->curs_col + n > t->cols) {
+        n = t->cols - t->curs_col;
     }
 
     }
 
-    for (i = rt->cols - 1; i >= rt->curs_col + n; i--) {
+    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];
     }
 
         row->text[i] = row->text[i - n];
         row->attr[i] = row->attr[i - n];
     }
 
-    mtty_row_set(row, rt->curs_col, n, build_attrs(rt->curattrs));
+    t_row_set(row, t->curs_col, n, build_attrs(t->curattrs));
 }
 
 /* Interpret the 'delete chars' sequence (DCH) */
 }
 
 /* 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)
 {
 {
-    mtty_row_t *row = rt->curs_row;
+    t_row_t *row = t->curs_row;
     int n = (pcount && param[0] > 0) ? param[0] : 1;
     int i;
 
     int n = (pcount && param[0] > 0) ? param[0] : 1;
     int i;
 
-    if (rt->curs_col + n > rt->cols) {
-        n = rt->cols - rt->curs_col;
+    if (t->curs_col + n > t->cols) {
+        n = t->cols - t->curs_col;
     }
 
     }
 
-    for (i = rt->curs_col; i < rt->cols - n; i++) {
+    for (i = t->curs_col; i < t->cols - n; i++) {
         row->text[i] = row->text[i + n];
         row->attr[i] = row->attr[i + n];
     }
 
         row->text[i] = row->text[i + n];
         row->attr[i] = row->attr[i + n];
     }
 
-    mtty_row_set(row, rt->cols - n, n, build_attrs(rt->curattrs));
+    t_row_set(row, t->cols - n, n, build_attrs(t->curattrs));
 }
 
 /* Interpret a 'scroll reverse' (SR) */
 }
 
 /* Interpret a 'scroll reverse' (SR) */
-static void interpret_csi_SR(madtty_t *rt)
+static void interpret_csi_SR(madtty_t *t)
 {
 {
-    mtty_row_roll(rt->scroll_top, rt->scroll_bot, -1);
-    mtty_row_set(rt->scroll_top, 0, rt->cols, build_attrs(rt->curattrs));
+    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) */
 }
 
 /* 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 n = (pcount && param[0] > 0) ? param[0] : 1;
 
-    if (rt->curs_row + n >= rt->scroll_bot) {
-        for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
-            mtty_row_set(row, 0, rt->cols, build_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 {
         }
     } else {
-        mtty_row_roll(rt->curs_row, rt->scroll_bot, -n);
-        for (mtty_row_t *row = rt->curs_row; row < rt->curs_row + n; row++) {
-            mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
+        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) */
         }
     }
 }
 
 /* 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 n = (pcount && param[0] > 0) ? param[0] : 1;
 
-    if (rt->curs_row + n >= rt->scroll_bot) {
-        for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
-            mtty_row_set(row, 0, rt->cols, build_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 {
         }
     } else {
-        mtty_row_roll(rt->curs_row, rt->scroll_bot, n);
-        for (mtty_row_t *row = rt->scroll_bot - n; row < rt->scroll_bot; row++) {
-            mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
+        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 */
         }
     }
 }
 
 /* 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 n = (pcount && param[0] > 0) ? param[0] : 1;
 
-    if (rt->curs_col + n < rt->cols) {
-        n = rt->cols - rt->curs_col;
+    if (t->curs_col + n < t->cols) {
+        n = t->cols - t->curs_col;
     }
     }
-    mtty_row_set(rt->curs_row, rt->curs_col, n, build_attrs(rt->curattrs));
+    t_row_set(t->curs_row, t->curs_col, n, build_attrs(t->curattrs));
 }
 
 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
 }
 
 /* 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 new_top, new_bot;
 
     switch (pcount) {
       case 0:
 {
     int new_top, new_bot;
 
     switch (pcount) {
       case 0:
-        rt->scroll_top = rt->lines;
-        rt->scroll_bot = rt->lines + rt->rows;
+        t->scroll_top = t->lines;
+        t->scroll_bot = t->lines + t->rows;
         break;
       default:
         return; /* malformed */
         break;
       default:
         return; /* malformed */
@@ -442,35 +473,36 @@ static void interpret_csi_DECSTBM(madtty_t *rt, int param[], int pcount)
         /* clamp to bounds */
         if (new_top < 0)
             new_top = 0;
         /* clamp to bounds */
         if (new_top < 0)
             new_top = 0;
-        if (new_top >= rt->rows)
-            new_top = rt->rows - 1;
+        if (new_top >= t->rows)
+            new_top = t->rows - 1;
         if (new_bot < 0)
             new_bot = 0;
         if (new_bot < 0)
             new_bot = 0;
-        if (new_bot >= rt->rows)
-            new_bot = rt->rows;
+        if (new_bot >= t->rows)
+            new_bot = t->rows;
 
         /* check for range validity */
         if (new_top < new_bot) {
 
         /* check for range validity */
         if (new_top < new_bot) {
-            rt->scroll_top = rt->lines + new_top;
-            rt->scroll_bot = rt->lines + new_bot;
+            t->scroll_top = t->lines + new_top;
+            t->scroll_bot = t->lines + new_bot;
         }
         break;
     }
 }
 
         }
         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;
     int param_count = 0;
-    const char *p = rt->ebuf + 1;
-    char verb = rt->ebuf[rt->elen - 1];
+    const char *p = t->ebuf + 1;
+    char verb = t->ebuf[t->elen - 1];
 
 
-    p += rt->ebuf[1] == '?'; /* CSI private mode */
+    p += t->ebuf[1] == '?'; /* CSI private mode */
 
     /* parse numeric parameters */
     while (isdigit((unsigned char)*p) || *p == ';') {
         if (*p == ';') {
 
     /* 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;
             csiparam[param_count++] = 0;
         } else {
             if (param_count == 0) csiparam[param_count++] = 0;
@@ -481,16 +513,16 @@ static void es_interpret_csi(madtty_t *rt)
         p++;
     }
 
         p++;
     }
 
-    if (rt->ebuf[1] == '?') {
+    if (t->ebuf[1] == '?') {
         switch (verb) {
           case 'l':
             if (csiparam[0] == 25)
         switch (verb) {
           case 'l':
             if (csiparam[0] == 25)
-                rt->curshid = true;
+                t->curshid = true;
             break;
 
           case 'h':
             if (csiparam[0] == 25)
             break;
 
           case 'h':
             if (csiparam[0] == 25)
-                rt->curshid = false;
+                t->curshid = false;
             break;
         }
     }
             break;
         }
     }
@@ -499,66 +531,66 @@ static void es_interpret_csi(madtty_t *rt)
     switch (verb) {
       case 'h':
         if (param_count == 1 && csiparam[0] == 4) /* insert mode */
     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 */
         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 */
         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 */
       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 */
       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' */
       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 */
       case 'K': /* erase line */
-        interpret_csi_EL(rt, csiparam, param_count); break;
+        interpret_csi_EL(t, csiparam, param_count); break;
       case '@': /* insert characters */
       case '@': /* insert characters */
-        interpret_csi_ICH(rt, csiparam, param_count); break;
+        interpret_csi_ICH(t, csiparam, param_count); break;
       case 'P': /* delete characters */
       case 'P': /* delete characters */
-        interpret_csi_DCH(rt, csiparam, param_count); break;
+        interpret_csi_DCH(t, csiparam, param_count); break;
       case 'L': /* insert lines */
       case 'L': /* insert lines */
-        interpret_csi_IL(rt, csiparam, param_count); break;
+        interpret_csi_IL(t, csiparam, param_count); break;
       case 'M': /* delete lines */
       case 'M': /* delete lines */
-        interpret_csi_DL(rt, csiparam, param_count); break;
+        interpret_csi_DL(t, csiparam, param_count); break;
       case 'X': /* erase chars */
       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 */
       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 */
       case 's': /* save cursor location */
-        rt->curs_srow = rt->curs_row - rt->lines;
-        rt->curs_scol = rt->curs_col;
+        t->curs_srow = t->curs_row - t->lines;
+        t->curs_scol = t->curs_col;
         break;
       case 'u': /* restore cursor location */
         break;
       case 'u': /* restore cursor location */
-        rt->curs_row = rt->lines + rt->curs_srow;
-        rt->curs_col = rt->curs_scol;
-        clamp_cursor_to_bounds(rt);
+        t->curs_row = t->lines + t->curs_srow;
+        t->curs_col = t->curs_scol;
+        clamp_cursor_to_bounds(t);
         break;
       default:
         break;
     }
 }
 
         break;
       default:
         break;
     }
 }
 
-static void try_interpret_escape_seq(madtty_t *rt)
+static void try_interpret_escape_seq(madtty_t *t)
 {
 {
-    char lastchar  = rt->ebuf[rt->elen-1];
+    char lastchar  = t->ebuf[t->elen-1];
 
 
-    switch (*rt->ebuf) {
+    switch (*t->ebuf) {
       case '\0':
         return;
 
       case 'M':
       case '\0':
         return;
 
       case 'M':
-        interpret_csi_SR(rt);
-        cancel_escape_sequence(rt);
+        interpret_csi_SR(t);
+        cancel_escape_sequence(t);
         return;
 
       case '(':
       case ')':
         return;
 
       case '(':
       case ')':
-        if (rt->elen == 2)
+        if (t->elen == 2)
             goto cancel;
         break;
 
             goto cancel;
         break;
 
@@ -572,37 +604,24 @@ static void try_interpret_escape_seq(madtty_t *rt)
 
       case '[':
         if (is_valid_csi_ender(lastchar)) {
 
       case '[':
         if (is_valid_csi_ender(lastchar)) {
-            es_interpret_csi(rt);
-            cancel_escape_sequence(rt);
+            es_interpret_csi(t);
+            cancel_escape_sequence(t);
             return;
         }
         break;
     }
 
             return;
         }
         break;
     }
 
-    if (rt->elen + 1 >= (int)sizeof(rt->ebuf)) {
+    if (t->elen + 1 >= (int)sizeof(t->ebuf)) {
 cancel:
 cancel:
-#if 0
-        int i;
-        fprintf(stderr, "cancelled: \\033");
-        for (i = 0; i < rt->elen; i++) {
-            int c = rt->ebuf[i];
-            if (isprint(c) && c >= ' ') {
-                fputc(c, stderr);
-            } else {
-                fprintf(stderr, "\\%03o", c);
-            }
-        }
-        fputc('\n', stderr);
-#endif
-        cancel_escape_sequence(rt);
+        cancel_escape_sequence(t);
     }
 }
 
     }
 }
 
-static void madtty_process_nonprinting(madtty_t *rt, wchar_t wc)
+static void madtty_process_nonprinting(madtty_t *t, wchar_t wc)
 {
     switch (wc) {
       case C0_ESC:
 {
     switch (wc) {
       case C0_ESC:
-        new_escape_sequence(rt);
+        new_escape_sequence(t);
         break;
 
       case C0_BEL:
         break;
 
       case C0_BEL:
@@ -610,77 +629,69 @@ static void madtty_process_nonprinting(madtty_t *rt, wchar_t wc)
         break;
 
       case C0_BS:
         break;
 
       case C0_BS:
-        if (rt->curs_col > 0)
-            rt->curs_col--;
+        if (t->curs_col > 0)
+            t->curs_col--;
         break;
 
       case C0_HT: /* tab */
         break;
 
       case C0_HT: /* tab */
-        rt->curs_col = (rt->curs_col + 8) & ~7;
-        if (rt->curs_col >= rt->cols)
-            rt->curs_col = rt->cols - 1;
+        t->curs_col = (t->curs_col + 8) & ~7;
+        if (t->curs_col >= t->cols)
+            t->curs_col = t->cols - 1;
         break;
 
       case C0_CR:
         break;
 
       case C0_CR:
-        rt->curs_col = 0;
+        t->curs_col = 0;
         break;
 
       case C0_VT:
       case C0_FF:
       case C0_LF:
         break;
 
       case C0_VT:
       case C0_FF:
       case C0_LF:
-        cursor_line_down(rt);
+        cursor_line_down(t);
         break;
 
       case C0_SO:              /* shift out - acs */
         break;
 
       case C0_SO:              /* shift out - acs */
-        rt->graphmode = true;
+        t->graphmode = true;
         break;
       case C0_SI:              /* shift in - acs */
         break;
       case C0_SI:              /* shift in - acs */
-        rt->graphmode = false;
+        t->graphmode = false;
         break;
     }
 }
 
         break;
     }
 }
 
-void madtty_putc(madtty_t *rt, wchar_t wc)
+// vt100 special graphics and line drawing
+// 5f-7e standard vt100
+// 40-5e rxvt extension for extra curses acs chars
+static uint16_t const vt100_0[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
+};
+
+void madtty_putc(madtty_t *t, wchar_t wc)
 {
     int width = 0;
 
 {
     int width = 0;
 
-    if (!rt->seen_input) {
-        rt->seen_input = 1;
-        kill(-rt->childpid, SIGWINCH);
-    }
-
-#if 0
-    if (wc == '\n' || (wc >= ' ' && isprint(wc))) {
-        fputc(wc, stderr);
-    } else {
-        fprintf(stderr, "\\%03o", wc);
+    if (!t->seen_input) {
+        t->seen_input = 1;
+        kill(-t->childpid, SIGWINCH);
     }
     }
-#endif
 
 
-    if (rt->escaped) {
-        assert (rt->elen + 1 < (int)sizeof(rt->ebuf));
-        rt->ebuf[rt->elen]   = wc;
-        rt->ebuf[++rt->elen] = '\0';
-        try_interpret_escape_seq(rt);
+    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)) {
     } else if (IS_CONTROL(wc)) {
-        madtty_process_nonprinting(rt, wc);
+        madtty_process_nonprinting(t, wc);
     } else {
     } else {
-        mtty_row_t *tmp;
-
-        if (rt->graphmode) {
-            // vt100 special graphics and line drawing
-            // 5f-7e standard vt100
-            // 40-5e rxvt extension for extra curses acs chars
-            static uint16_t vt100_0[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
-            };
+        t_row_t *tmp;
 
 
+        if (t->graphmode) {
             if (wc >= 0x41 && wc <= 0x7e && vt100_0[wc - 0x41]) {
                 wc = vt100_0[wc - 0x41];
             }
             if (wc >= 0x41 && wc <= 0x7e && vt100_0[wc - 0x41]) {
                 wc = vt100_0[wc - 0x41];
             }
@@ -689,190 +700,190 @@ void madtty_putc(madtty_t *rt, wchar_t wc)
             width = wcwidth(wc) ?: 1;
         }
 
             width = wcwidth(wc) ?: 1;
         }
 
-        if (width == 2 && rt->curs_col == rt->cols - 1) {
-            tmp = rt->curs_row;
+        if (width == 2 && t->curs_col == t->cols - 1) {
+            tmp = t->curs_row;
             tmp->dirty = true;
             tmp->dirty = true;
-            tmp->text[rt->curs_col] = 0;
-            tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
-            rt->curs_col++;
+            tmp->text[t->curs_col] = 0;
+            tmp->attr[t->curs_col] = build_attrs(t->curattrs);
+            t->curs_col++;
         }
 
         }
 
-        if (rt->curs_col >= rt->cols) {
-            rt->curs_col = 0;
-            cursor_line_down(rt);
+        if (t->curs_col >= t->cols) {
+            t->curs_col = 0;
+            cursor_line_down(t);
         }
 
         }
 
-        tmp = rt->curs_row;
+        tmp = t->curs_row;
         tmp->dirty = true;
 
         tmp->dirty = true;
 
-        if (rt->insert) {
-            wmemmove(tmp->text + rt->curs_col + width, tmp->text + rt->curs_col,
-                     (rt->cols - rt->curs_col - width));
-            memmove(tmp->attr + rt->curs_col + width, tmp->attr + rt->curs_col,
-                    (rt->cols - rt->curs_col - width) * sizeof(tmp->attr[0]));
+        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]));
         }
 
         }
 
-        tmp->text[rt->curs_col] = wc;
-        tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
-        rt->curs_col++;
+        tmp->text[t->curs_col] = wc;
+        tmp->attr[t->curs_col] = build_attrs(t->curattrs);
+        t->curs_col++;
         if (width == 2) {
         if (width == 2) {
-            tmp->text[rt->curs_col] = 0;
-            tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
-            rt->curs_col++;
+            tmp->text[t->curs_col] = 0;
+            tmp->attr[t->curs_col] = build_attrs(t->curattrs);
+            t->curs_col++;
         }
     }
 }
 
         }
     }
 }
 
-int madtty_process(madtty_t *rt)
+int madtty_process(madtty_t *t)
 {
     int res, pos = 0;
 
 {
     int res, pos = 0;
 
-    if (rt->pty < 0) {
+    if (t->pty < 0) {
         errno = EINVAL;
         return -1;
     }
 
         errno = EINVAL;
         return -1;
     }
 
-    res = read(rt->pty, rt->rbuf + rt->rlen, sizeof(rt->rbuf) - rt->rlen);
+    res = read(t->pty, t->rbuf + t->rlen, sizeof(t->rbuf) - t->rlen);
     if (res < 0)
         return -1;
 
     if (res < 0)
         return -1;
 
-    rt->rlen += res;
-    while (pos < rt->rlen) {
+    t->rlen += res;
+    while (pos < t->rlen) {
         wchar_t wc;
         ssize_t len;
 
         wchar_t wc;
         ssize_t len;
 
-        len = (ssize_t)mbrtowc(&wc, rt->rbuf + pos, rt->rlen - pos, &rt->ps);
+        len = (ssize_t)mbrtowc(&wc, t->rbuf + pos, t->rlen - pos, &t->ps);
         if (len == -2) {
         if (len == -2) {
-            rt->rlen -= pos;
-            memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
+            t->rlen -= pos;
+            memmove(t->rbuf, t->rbuf + pos, t->rlen);
             return 0;
         }
 
         if (len == -1) {
             len = 1;
             return 0;
         }
 
         if (len == -1) {
             len = 1;
-            wc  = rt->rbuf[pos];
+            wc  = t->rbuf[pos];
         }
 
         pos += len ? len : 1;
         }
 
         pos += len ? len : 1;
-        madtty_putc(rt, wc);
+        madtty_putc(t, wc);
     }
 
     }
 
-    rt->rlen -= pos;
-    memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
+    t->rlen -= pos;
+    memmove(t->rbuf, t->rbuf + pos, t->rlen);
     return 0;
 }
 
 madtty_t *madtty_create(int rows, int cols)
 {
     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;
 
     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 */
         return NULL;
 
     /* record dimensions */
-    rt->rows = rows;
-    rt->cols = cols;
+    t->rows = rows;
+    t->cols = cols;
 
     /* default mode is replace */
 
     /* default mode is replace */
-    rt->insert = false;
+    t->insert = false;
 
     /* create the cell matrix */
 
     /* create the cell matrix */
-    rt->lines = (mtty_row_t*)calloc(sizeof(mtty_row_t), rt->rows);
-    for (i = 0; i < rt->rows; i++) {
-        rt->lines[i].text = (wchar_t *)calloc(sizeof(wchar_t), rt->cols);
-        rt->lines[i].attr = (uint16_t *)calloc(sizeof(uint16_t), 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);
     }
 
     }
 
-    rt->pty = -1;  /* no pty for now */
+    t->pty = -1;  /* no pty for now */
 
     /* initialization of other public fields */
 
     /* initialization of other public fields */
-    rt->curs_row = rt->lines;
-    rt->curs_col = 0;
-    rt->curattrs = A_NORMAL;  /* white text over black background */
+    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 */
 
     /* initial scrolling area is the whole window */
-    rt->scroll_top = rt->lines;
-    rt->scroll_bot = rt->lines + rt->rows;
+    t->scroll_top = t->lines;
+    t->scroll_bot = t->lines + t->rows;
 
 
-    return rt;
+    return t;
 }
 
 }
 
-void madtty_resize(madtty_t *rt, int rows, int cols)
+void madtty_resize(madtty_t *t, int rows, int cols)
 {
     struct winsize ws = { .ws_row = rows, .ws_col = cols };
 {
     struct winsize ws = { .ws_row = rows, .ws_col = cols };
-    mtty_row_t *lines = rt->lines;
+    t_row_t *lines = t->lines;
 
     if (rows <= 0 || cols <= 0)
         return;
 
 
     if (rows <= 0 || cols <= 0)
         return;
 
-    if (rt->rows != rows) {
-        while (rt->rows > rows) {
-            free(lines[rt->rows - 1].text);
-            free(lines[rt->rows - 1].attr);
-            rt->rows--;
+    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(mtty_row_t) * rows);
+        lines = realloc(lines, sizeof(t_row_t) * rows);
     }
 
     }
 
-    if (rt->cols != cols) {
-        for (int row = 0; row < rt->rows; row++) {
+    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);
             lines[row].text = realloc(lines[row].text, sizeof(wchar_t) * cols);
             lines[row].attr = realloc(lines[row].attr, sizeof(uint16_t) * cols);
-            if (rt->cols < cols)
-                mtty_row_set(lines + row, rt->cols, cols - rt->cols, 0);
+            if (t->cols < cols)
+                t_row_set(lines + row, t->cols, cols - t->cols, 0);
         }
         }
-        rt->cols = cols;
+        t->cols = cols;
     }
 
     }
 
-    while (rt->rows < rows) {
-        lines[rt->rows].text = (wchar_t *)calloc(sizeof(wchar_t), cols);
-        lines[rt->rows].attr = (uint16_t *)calloc(sizeof(uint16_t), cols);
-        rt->rows++;
+    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->rows++;
     }
 
     }
 
-    rt->curs_row   += lines - rt->lines;
-    rt->scroll_top += lines - rt->lines;
-    rt->scroll_bot += lines - rt->lines;
-    if (rt->scroll_bot > lines + rt->rows)
-        rt->scroll_bot = lines + rt->rows;
-    rt->lines = lines;
-    clamp_cursor_to_bounds(rt);
-    ioctl(rt->pty, TIOCSWINSZ, &ws);
-    kill(-rt->childpid, SIGWINCH);
+    t->curs_row   += lines - t->lines;
+    t->scroll_top += lines - t->lines;
+    t->scroll_bot += lines - t->lines;
+    if (t->scroll_bot > lines + t->rows)
+        t->scroll_bot = lines + t->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;
 {
     int i;
-    if (!rt)
+    if (!t)
         return;
 
         return;
 
-    for (i = 0; i < rt->rows; i++) {
-        free(rt->lines[i].text);
-        free(rt->lines[i].attr);
+    for (i = 0; i < t->rows; i++) {
+        free(t->lines[i].text);
+        free(t->lines[i].attr);
     }
     }
-    free(rt->lines);
-    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)
 {
     curs_set(0);
 {
     curs_set(0);
-    for (int i = 0; i < rt->rows; i++) {
-        mtty_row_t *row = rt->lines + i;
+    for (int i = 0; i < t->rows; i++) {
+        t_row_t *row = t->lines + i;
 
 
         if (!row->dirty)
             continue;
 
         wmove(win, srow + i, scol);
 
 
         if (!row->dirty)
             continue;
 
         wmove(win, srow + i, scol);
-        for (int j = 0; j < rt->cols; j++) {
+        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) {
             if (!j || row->attr[j] != row->attr[j - 1])
                 wattrset(win, (attr_t)row->attr[j] << NCURSES_ATTR_SHIFT);
             if (row->text[j] >= 128) {
@@ -890,37 +901,44 @@ void madtty_draw(madtty_t *rt, WINDOW *win, int srow, int scol)
         row->dirty = false;
     }
 
         row->dirty = false;
     }
 
-    wmove(win, srow + rt->curs_row - rt->lines, scol + rt->curs_col);
-    curs_set(!rt->curshid);
+    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;
 
 {
     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;
 
     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", "rxvt", 1);
     if (pid < 0)
         return -1;
 
     if (pid == 0) {
         setsid();
         setenv("TERM", "rxvt", 1);
-        execv(path, (char *const*)argv);
+        execv(p, (char *const*)argv);
         fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]);
         exit(1);
     }
 
         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_getpty(madtty_t *t)
+{
+    return t->pty;
 }
 
 }
 
-void madtty_keypress(madtty_t *rt, int keycode)
+void madtty_keypress(madtty_t *t, int keycode)
 {
     char c = (char)keycode;
     const char *buf;
 {
     char c = (char)keycode;
     const char *buf;
@@ -935,7 +953,7 @@ void madtty_keypress(madtty_t *rt, int keycode)
     }
 
     while (len > 0) {
     }
 
     while (len > 0) {
-        int res = write(rt->pty, buf, len);
+        int res = write(t->pty, buf, len);
         if (res < 0 && errno != EAGAIN && errno != EINTR)
             return;
 
         if (res < 0 && errno != EAGAIN && errno != EINTR)
             return;
 
index edecde9..cd9e7d4 100644 (file)
 #include <unistd.h>
 #include <wchar.h>
 
 #include <unistd.h>
 #include <wchar.h>
 
-#define MAX_CSI_ES_PARAMS 32
-
-typedef struct mtty_row_t mtty_row_t;
-
-typedef struct {
-    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;
-
-    mtty_row_t *lines;
-    mtty_row_t *scroll_top;
-    mtty_row_t *scroll_bot;
-
-    /* cursor */
-    unsigned curattrs;
-    mtty_row_t *curs_row;
-    int curs_col;
-    int curs_srow, curs_scol;
-
-    /* buffers and parsing state */
-    mbstate_t ps;
-    char rbuf[BUFSIZ];
-    char ebuf[BUFSIZ];
-    int  rlen, elen;
-} madtty_t;
-
 void madtty_initialize(void);
 
 void madtty_initialize(void);
 
-madtty_t *madtty_create(int rows, int cols);
-void madtty_resize(madtty_t *rt, int rows, int cols);
-void madtty_destroy(madtty_t *rt);
-pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[]);
+typedef struct madtty_t madtty_t;
 
 
-int madtty_process(madtty_t *rt);
-void madtty_keypress(madtty_t *rt, int keycode);
-void madtty_draw(madtty_t *rt, WINDOW *win, int startrow, int startcol);
+madtty_t *madtty_create(int rows, int cols);
+void madtty_resize(madtty_t *, int rows, int cols);
+void madtty_destroy(madtty_t *);
+pid_t madtty_forkpty(madtty_t *, const char *, const char *argv[], int *pty);
+int madtty_getpty(madtty_t *);
+
+int madtty_process(madtty_t *);
+void madtty_keypress(madtty_t *, int keycode);
+void madtty_draw(madtty_t *, WINDOW *win, int startrow, int startcol);
 
 
 #endif /* MADTTY_MADTTY_H */
 
 
 #endif /* MADTTY_MADTTY_H */