X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=madtty%2Fmadtty.c;h=2b5fae213552f82447280421cd5d560b2c305330;hb=f38bb36465858ae6af3776175010e8c09e70f980;hp=af959ce8763523936f751d2d62ec1cc1b58ecb17;hpb=714e9fd2445997a69dec855e59a2a93cb991f3dd;p=apps%2Fmadtty.git diff --git a/madtty/madtty.c b/madtty/madtty.c index af959ce..2b5fae2 100644 --- a/madtty/madtty.c +++ b/madtty/madtty.c @@ -19,322 +19,820 @@ Copyright © 2006 Pierre Habouzit */ +#include #include -#include #include +#include +#include #include +#include #include #include #include "madtty.h" -#include "roteprivate.h" -RoteTerm *rote_vt_create(int rows, int cols) +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~", +}; + +static void mtty_row_set(mtty_row_t *row, int start, int len, uint16_t attr) { - RoteTerm *rt; - int i, j; + wmemset(row->text + start, 0, len); + for (int i = start; i < len + start; i++) { + row->attr[i] = attr; + } +} - if (rows <= 0 || cols <= 0) - return NULL; +__attribute__((noinline)) +static void mtty_row_roll(mtty_row_t *start, mtty_row_t *end, int count) +{ + int n = end - start; - rt = (RoteTerm*)calloc(sizeof(RoteTerm), 1); - if (!rt) - return NULL; + count %= n; + if (count < 0) + count += n; - /* record dimensions */ - rt->rows = rows; - rt->cols = cols; + if (count) { + mtty_row_t *buf = alloca(count * sizeof(mtty_row_t)); - /* default mode is replace */ - rt->insert = false; + 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)); + } +} - /* create the cell matrix */ - rt->cells = (RoteCell**) malloc(sizeof(RoteCell*) * rt->rows); - for (i = 0; i < rt->rows; i++) { - /* create row */ - rt->cells[i] = (RoteCell*) malloc(sizeof(RoteCell) * rt->cols); +static void clamp_cursor_to_bounds(madtty_t *rt) +{ + if (rt->curs_row < rt->lines) { + rt->curs_row = rt->lines; + } + if (rt->curs_row >= rt->lines + rt->rows) { + rt->curs_row = rt->lines + rt->rows - 1; + } - /* fill row with spaces */ - for (j = 0; j < rt->cols; j++) { - rt->cells[i][j].ch = 0x20; /* a space */ - rt->cells[i][j].attr = 0x70; /* white text, black background */ - } + if (rt->curs_col < 0) { + rt->curs_col = 0; + } + if (rt->curs_col >= rt->cols) { + rt->curs_col = rt->cols - 1; } +} - /* allocate dirtiness array */ - rt->line_dirty = (bool*)calloc(sizeof(bool), rt->rows); +static void cursor_line_down(madtty_t *rt) +{ + rt->curs_row++; + if (rt->curs_row < rt->scroll_bot) + return; - /* initialization of other public fields */ - rt->crow = rt->ccol = 0; - rt->curattr = 0x70; /* white text over black background */ + 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); +} - /* allocate private data */ - rt->pd = (RoteTermPrivate*)calloc(sizeof(RoteTermPrivate), 1); +static void cursor_line_up(madtty_t *rt) +{ + rt->curs_row--; + if (rt->curs_row >= rt->scroll_top) + return; - rt->pty = -1; /* no pty for now */ + /* must scroll the scrolling region up by 1 line, and put cursor on + * first line of it */ + rt->curs_row = rt->scroll_top; + mtty_row_roll(rt->scroll_top, rt->scroll_bot, -1); + mtty_row_set(rt->curs_row, 0, rt->cols, 0); +} - /* initial scrolling area is the whole window */ - rt->pd->scrolltop = 0; - rt->pd->scrollbottom = rt->rows - 1; +static uint16_t build_attrs(unsigned curattrs) +{ + return ((curattrs & ~A_COLOR) | COLOR_PAIR(curattrs & 0xff)) + >> NCURSES_ATTR_SHIFT; +} - return rt; +static void put_normal_char(madtty_t *rt, wchar_t c) +{ + mtty_row_t *tmp; + + if (rt->curs_col >= rt->cols) { + rt->curs_col = 0; + cursor_line_down(rt); + } + + tmp = rt->curs_row; + + if (rt->insert) { + wmemmove(tmp->text + rt->curs_col + 1, tmp->text + rt->curs_col, + (rt->cols - rt->curs_col - 1)); + memmove(tmp->attr + rt->curs_col + 1, tmp->attr + rt->curs_col, + (rt->cols - rt->curs_col - 1) * sizeof(tmp->attr[0])); + } + + tmp->text[rt->curs_col] = c; + tmp->attr[rt->curs_col] = build_attrs(rt->curattrs); + rt->curs_col++; } -void rote_vt_destroy(RoteTerm *rt) +static void put_graphmode_char(madtty_t *rt, int c) +{ + 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 = '%'; + } + + put_normal_char(rt, nc); +} + +static void new_escape_sequence(madtty_t *rt) +{ + rt->escaped = true; + rt->esbuf_len = 0; + rt->esbuf[0] = '\0'; +} + +static void cancel_escape_sequence(madtty_t *rt) +{ + rt->escaped = false; + rt->esbuf_len = 0; + rt->esbuf[0] = '\0'; +} + +static void handle_control_char(madtty_t *rt, int c) +{ + 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; + } +} + +static bool is_valid_csi_ender(int c) +{ + return (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c == '@' || c == '`'); +} + +/* interprets a 'set attribute' (SGR) CSI escape sequence */ +static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount) { int i; - if (!rt) + + if (pcount == 0) { + /* special case: reset attributes */ + rt->curattrs = A_NORMAL; return; + } - free(rt->pd); - free(rt->line_dirty); - for (i = 0; i < rt->rows; i++) { - free(rt->cells[i]); + 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 30 ... 37: + rt->curattrs &= ~0xf0; + rt->curattrs |= (param[i] - 29) << 4; + break; + + case 39: + rt->curattrs &= ~0xf0; + break; + + case 40 ... 47: + rt->curattrs &= ~0x0f; + rt->curattrs |= (param[i] - 39); + break; + + case 49: + rt->curattrs &= ~0x0f; + break; + + default: + break; + } } - free(rt->cells); - free(rt); } -#ifdef USE_NCURSES +/* interprets an 'erase display' (ED) escape sequence */ +static void interpret_csi_ED(madtty_t *rt, int param[], int pcount) +{ + int r; + int start_row, start_col, end_row, end_col; + + /* decide range */ + if (pcount && param[0] == 2) { + start_row = 0; + start_col = 0; + end_row = rt->rows - 1; + end_col = rt->cols - 1; + } else + if (pcount && param[0] == 1) { + start_row = 0; + start_col = 0; + end_row = rt->curs_row - rt->lines; + end_col = rt->curs_col; + } else { + start_row = rt->curs_row - rt->lines; + start_col = rt->curs_col; + end_row = rt->rows - 1; + end_col = rt->cols - 1; + } -static void default_cur_set_attr(WINDOW *win, unsigned char attr) + /* clean range */ + mtty_row_set(rt->lines + start_row, start_col, rt->cols - start_col, + build_attrs(rt->curattrs)); + for (r = start_row + 1; r < end_row; r++) { + mtty_row_set(rt->lines + r, 0, rt->cols, build_attrs(rt->curattrs)); + } + mtty_row_set(rt->lines + end_row, 0, end_col + 1, + build_attrs(rt->curattrs)); +} + +/* interprets a 'move cursor' (CUP) escape sequence */ +static void interpret_csi_CUP(madtty_t *rt, int param[], int pcount) { - int cp = ROTE_ATTR_BG(attr) * 8 + 7 - ROTE_ATTR_FG(attr); - if (!cp) wattrset(win, A_NORMAL); - else wattrset(win, COLOR_PAIR(cp)); + if (pcount == 0) { + /* special case */ + rt->curs_row = rt->lines; + rt->curs_col = 0; + 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 */ - if (ROTE_ATTR_BOLD(attr)) wattron(win, A_BOLD); - if (ROTE_ATTR_BLINK(attr)) wattron(win, A_BLINK); + clamp_cursor_to_bounds(rt); } -static inline unsigned char ensure_printable(unsigned char ch) +/* 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) { - return ch >= 32 ? ch : 32; + 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; + } + + clamp_cursor_to_bounds(rt); } -void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol, - void (*cur_set_attr)(WINDOW*,unsigned char)) { +/* Interpret the 'erase line' escape sequence */ +static void interpret_csi_EL(madtty_t *rt, int param[], int pcount) +{ + int start, len; + int cmd = pcount ? param[0] : 0; + + switch (cmd) { + case 1: + start = 0; + len = rt->curs_col + 1; + break; + case 2: + start = 0; + len = rt->cols; + break; + default: + start = rt->curs_col; + len = rt->cols - start; + break; + } - int i, j; + mtty_row_set(rt->curs_row, start, len, build_attrs(rt->curattrs)); +} - if (!cur_set_attr) - cur_set_attr = default_cur_set_attr; +/* Interpret the 'insert blanks' sequence (ICH) */ +static void interpret_csi_ICH(madtty_t *rt, int param[], int pcount) +{ + mtty_row_t *row = rt->curs_row; + int n = (pcount && param[0] > 0) ? param[0] : 1; + int i; - for (i = 0; i < rt->rows; i++) { - wmove(win, srow + i, scol); - for (j = 0; j < rt->cols; j++) { - (*cur_set_attr)(win, rt->cells[i][j].attr); - waddch(win, ensure_printable(rt->cells[i][j].ch)); - } + for (i = rt->cols - 1; i >= rt->curs_col + n; i--) { + row->text[i] = row->text[i - n]; + row->attr[i] = row->attr[i - n]; } - wmove(win, srow + rt->crow, scol + rt->ccol); + mtty_row_set(row, rt->curs_col, n, build_attrs(rt->curattrs)); } -#endif +/* Interpret the 'delete chars' sequence (DCH) */ +static void interpret_csi_DCH(madtty_t *rt, int param[], int pcount) +{ + mtty_row_t *row = rt->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) { + row->text[i] = row->text[i + n]; + row->attr[i] = row->attr[i + n]; + } else { + row->text[i] = 0; + row->attr[i] = build_attrs(rt->curattrs); + } + } +} -#define PTYCHAR1 "pqrstuvwxyz" -#define PTYCHAR2 "0123456789abcdef" - -/* allocate one pty/tty pair */ -static int get_pty(char *tty_str) -{ - int fd; - char ptydev[] = "/dev/pty??"; - char ttydev[] = "/dev/tty??"; - int len = strlen(ttydev); - const char *c1, *c2; - - for (c1 = PTYCHAR1; *c1; c1++) { - ptydev[len-2] = ttydev[len-2] = *c1; - for (c2 = PTYCHAR2; *c2; c2++) { - ptydev[len-1] = ttydev[len-1] = *c2; - if ((fd = open(ptydev, O_RDWR)) >= 0) { - if (access(ttydev, R_OK|W_OK) == 0) { - strcpy(tty_str, ttydev); - return fd; - } - close(fd); - } - } - } - return -1; -} - -static int -run_process(const char *path, const char **argv, int *fd_ptr, int *pid_ptr) -{ - int pty_fd, pid, i, nb_fds; - char tty_name[32]; - struct winsize ws; +/* Interpret an 'insert line' sequence (IL) */ +static void interpret_csi_IL(madtty_t *rt, int param[], int pcount) +{ + int n = (pcount && param[0] > 0) ? param[0] : 1; - pty_fd = get_pty(tty_name); - if (pty_fd < 0) - return -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)); + } + } 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)); + } + } - fcntl(pty_fd, F_SETFL, O_NONBLOCK); +} - /* set dummy screen size */ - ws.ws_col = 80; - ws.ws_row = 25; - ws.ws_xpixel = ws.ws_col; - ws.ws_ypixel = ws.ws_row; - ioctl(pty_fd, TIOCSWINSZ, &ws); +/* Interpret a 'delete line' sequence (DL) */ +static void interpret_csi_DL(madtty_t *rt, int param[], int pcount) +{ + int n = (pcount && param[0] > 0) ? param[0] : 1; - pid = fork(); - if (pid < 0) - return -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)); + } + } else { + mtty_row_roll(rt->curs_row, rt->scroll_bot, -n); - if (pid == 0) { - /* child process */ - nb_fds = getdtablesize(); - for (i = 0; i < nb_fds; i++) - close(i); - /* open pseudo tty for standard i/o */ - open(tty_name, O_RDWR); - dup(0); - dup(0); + 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)); + } + } +} - setsid(); +/* Interpret an 'erase characters' (ECH) sequence */ +static void interpret_csi_ECH(madtty_t *rt, int param[], int pcount) +{ + int n = (pcount && param[0] > 0) ? param[0] : 1; - setenv("TERM", "linux", 1); - execv(path, (char *const*)argv); - fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]); - exit(1); + if (rt->curs_col + n < rt->cols) { + n = rt->cols - rt->curs_col; } - /* return file info */ - *fd_ptr = pty_fd; - *pid_ptr = pid; - return 0; + mtty_row_set(rt->curs_row, rt->curs_col, n, build_attrs(rt->curattrs)); } -pid_t rote_vt_forkpty(RoteTerm *rt, const char *path, const char *argv[]) +/* Interpret a 'set scrolling region' (DECSTBM) sequence */ +static void interpret_csi_DECSTBM(madtty_t *rt, int param[], int pcount) { - struct winsize ws; + int new_top, new_bot; + + switch (pcount) { + case 0: + rt->scroll_top = rt->lines; + rt->scroll_bot = rt->lines + rt->rows; + break; + default: + return; /* malformed */ + + case 2: + new_top = param[0] - 1; + new_bot = param[1] - 1; + + /* clamp to bounds */ + if (new_top < 0) + new_top = 0; + if (new_top >= rt->rows) + new_top = rt->rows - 1; + if (new_bot < 0) + new_bot = 0; + if (new_bot >= rt->rows) + new_bot = rt->rows; + + /* check for range validity */ + if (new_top < new_bot) { + rt->scroll_top = rt->lines + new_top; + rt->scroll_bot = rt->lines + new_bot; + } + break; + } +} - ws.ws_row = rt->rows; - ws.ws_col = rt->cols; - ws.ws_xpixel = ws.ws_ypixel = 0; +static void es_interpret_csi(madtty_t *rt) +{ + static int csiparam[MAX_CSI_ES_PARAMS]; + int param_count = 0; + const char *p = rt->esbuf + 1; + char verb = rt->esbuf[rt->esbuf_len - 1]; - if (run_process(path, argv, &rt->pty, &rt->childpid)) { - return -1; + if (!strncmp(rt->esbuf, "[?", 2)) { /* private-mode CSI, ignore */ + return; + } + + /* parse numeric parameters */ + while (isdigit((unsigned char)*p) || *p == ';') { + if (*p == ';') { + if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */ + csiparam[param_count++] = 0; + } else { + if (param_count == 0) csiparam[param_count++] = 0; + csiparam[param_count - 1] *= 10; + csiparam[param_count - 1] += *p - '0'; + } + + p++; } - ioctl(rt->pty, TIOCSWINSZ, &ws); - return rt->childpid; + /* delegate handling depending on command character (verb) */ + switch (verb) { + case 'h': + if (param_count == 1 && csiparam[0] == 4) /* insert mode */ + rt->insert = true; + break; + case 'l': + if (param_count == 1 && csiparam[0] == 4) /* replace mode */ + rt->insert = false; + break; + case 'm': /* it's a 'set attribute' sequence */ + interpret_csi_SGR(rt, csiparam, param_count); break; + case 'J': /* it's an 'erase display' sequence */ + interpret_csi_ED(rt, csiparam, param_count); break; + case 'H': case 'f': /* it's a 'move cursor' sequence */ + interpret_csi_CUP(rt, 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; + case 'K': /* erase line */ + interpret_csi_EL(rt, csiparam, param_count); break; + case '@': /* insert characters */ + interpret_csi_ICH(rt, csiparam, param_count); break; + case 'P': /* delete characters */ + interpret_csi_DCH(rt, csiparam, param_count); break; + case 'L': /* insert lines */ + interpret_csi_IL(rt, csiparam, param_count); break; + case 'M': /* delete lines */ + interpret_csi_DL(rt, csiparam, param_count); break; + case 'X': /* erase chars */ + interpret_csi_ECH(rt, csiparam, param_count); break; + case 'r': /* set scrolling region */ + interpret_csi_DECSTBM(rt, csiparam, param_count); break; + case 's': /* save cursor location */ + rt->curs_srow = rt->curs_row - rt->lines; + rt->curs_scol = rt->curs_col; + 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); + break; + default: + break; + } } -void rote_vt_forsake_child(RoteTerm *rt) +static void try_interpret_escape_seq(madtty_t *rt) { - if (rt->pty >= 0) - close(rt->pty); - rt->pty = -1; - rt->childpid = 0; + char firstchar = rt->esbuf[0]; + char lastchar = rt->esbuf[rt->esbuf_len-1]; + + if (!firstchar) + return; /* too early to do anything */ + + /* interpret ESC-M as reverse line-feed */ + if (firstchar == 'M') { + cursor_line_up(rt); + cancel_escape_sequence(rt); + return; + } + + if (firstchar != '[' && firstchar != ']') { + /* unrecognized escape sequence. Let's forget about it. */ + cancel_escape_sequence(rt); + return; + } + + 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 */ + + /* es_interpret_xterm_es(rt); -- TODO!*/ + cancel_escape_sequence(rt); + } + + /* 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); } -int rote_vt_read(RoteTerm *rt, char *buf, int buflen) +int madtty_process(madtty_t *rt) { + int res, pos = 0; + if (rt->pty < 0) { errno = EINVAL; return -1; } - return read(rt->pty, buf, buflen); -} + res = read(rt->pty, rt->rbuf + rt->rbuf_len, + sizeof(rt->rbuf) - rt->rbuf_len); + if (res < 0) + return -1; -void rote_vt_write(RoteTerm *rt, const char *data, int len) -{ - if (rt->pty < 0) { - /* no pty, so just inject the data plain and simple */ - rote_vt_inject(rt, data, len); - return; - } + rt->rbuf_len += res; + while (pos < rt->rbuf_len) { + wchar_t wc; + ssize_t len; + + len = (ssize_t)mbrtowc(&wc, rt->rbuf + pos, rt->rbuf_len - pos, + &rt->ps); + if (len < 0) { + rt->rbuf_len -= pos; + memmove(rt->rbuf, rt->rbuf + pos, rt->rbuf_len); + return len == -2 ? 0 : -1; + } - /* write data to pty. Keep calling write() until we have written - * everything. */ - while (len > 0) { - int byteswritten = write(rt->pty, data, len); - if (byteswritten < 0) { - /* very ugly way to inform the error. Improvements welcome! */ - static char errormsg[] = "\n(ROTE: pty write() error)\n"; - rote_vt_inject(rt, errormsg, strlen(errormsg)); - return; + pos += len ? len : 1; + + if (wc < ' ') { + handle_control_char(rt, wc); + continue; } - data += byteswritten; - len -= byteswritten; + if (rt->escaped && rt->esbuf_len < ESEQ_BUF_SIZE) { + /* append character to ongoing escape sequence */ + rt->esbuf[rt->esbuf_len] = wc; + rt->esbuf[++rt->esbuf_len] = 0; + + try_interpret_escape_seq(rt); + } else + if (rt->graphmode) { + put_graphmode_char(rt, wc); + } else { + put_normal_char(rt, wc); + } } + + rt->rbuf_len -= pos; + memmove(rt->rbuf, rt->rbuf + pos, rt->rbuf_len); + return 0; } -void *rote_vt_take_snapshot(RoteTerm *rt) +madtty_t *madtty_create(int rows, int cols) { - const int bytes_per_row = sizeof(RoteCell) * rt->cols; - void *buf = malloc(bytes_per_row * rt->rows); - void *ptr = buf; + madtty_t *rt; int i; - for (i = 0; i < rt->rows; i++, ptr += bytes_per_row) - memcpy(ptr, rt->cells[i], bytes_per_row); + if (rows <= 0 || cols <= 0) + return NULL; + + rt = (madtty_t*)calloc(sizeof(madtty_t), 1); + if (!rt) + return NULL; + + /* record dimensions */ + rt->rows = rows; + rt->cols = cols; + + /* default mode is replace */ + rt->insert = false; + + /* 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); + } + + rt->pty = -1; /* no pty for now */ + + /* initialization of other public fields */ + rt->curs_row = rt->lines; + rt->curs_col = 0; + rt->curattrs = A_NORMAL; /* white text over black background */ + + /* initial scrolling area is the whole window */ + rt->scroll_top = rt->lines; + rt->scroll_bot = rt->lines + rt->rows; - return buf; + return rt; } -void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf) +void madtty_destroy(madtty_t *rt) { - const int bytes_per_row = sizeof(RoteCell) * rt->cols; - int i; + if (!rt) + return; - for (i = 0; i < rt->rows; i++, snapbuf += bytes_per_row) { - rt->line_dirty[i] = true; - memcpy(rt->cells[i], snapbuf, bytes_per_row); + for (i = 0; i < rt->rows; i++) { + free(rt->lines[i].text); + free(rt->lines[i].attr); } + free(rt->lines); + free(rt); } -static const char *keytable[KEY_MAX+1]; +void madtty_draw(madtty_t *rt, WINDOW *win, int srow, int scol) +{ + int i, j; -static void keytable_init() + for (i = 0; i < rt->rows; i++) { + mtty_row_t *row = rt->lines + i; + wmove(win, srow + i, scol); + + for (j = 0; j < rt->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); + } else { + waddch(win, row->text[j] > ' ' ? row->text[j] : ' '); + } + } + } + + wmove(win, srow + rt->curs_row - rt->lines, scol + rt->curs_col); +} + +/******************************************************/ + +pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[]) { - memset(keytable, 0, KEY_MAX+1 * sizeof(const char*)); + struct winsize ws; + pid_t pid; + + ws.ws_row = rt->rows; + ws.ws_col = rt->cols; + ws.ws_xpixel = ws.ws_ypixel = 0; + + pid = forkpty(&rt->pty, NULL, NULL, &ws); + if (pid < 0) + return -1; + + if (pid == 0) { + setsid(); - keytable['\n'] = "\r"; - keytable[KEY_UP] = "\e[A"; - keytable[KEY_DOWN] = "\e[B"; - keytable[KEY_RIGHT] = "\e[C"; - keytable[KEY_LEFT] = "\e[D"; - keytable[KEY_BACKSPACE] = "\b"; - keytable[KEY_HOME] = "\e[1~"; - keytable[KEY_IC] = "\e[2~"; - keytable[KEY_DC] = "\e[3~"; - keytable[KEY_END] = "\e[4~"; - keytable[KEY_PPAGE] = "\e[5~"; - keytable[KEY_NPAGE] = "\e[6~"; - keytable[KEY_SUSPEND] = "\x1A"; /* Ctrl+Z gets mapped to this */ - keytable[KEY_F(1)] = "\e[[A"; - keytable[KEY_F(2)] = "\e[[B"; - keytable[KEY_F(3)] = "\e[[C"; - keytable[KEY_F(4)] = "\e[[D"; - keytable[KEY_F(5)] = "\e[[E"; - keytable[KEY_F(6)] = "\e[17~"; - keytable[KEY_F(7)] = "\e[18~"; - keytable[KEY_F(8)] = "\e[19~"; - keytable[KEY_F(9)] = "\e[20~"; - keytable[KEY_F(10)] = "\e[21~"; + setenv("TERM", "linux", 1); + execv(path, (char *const*)argv); + fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]); + exit(1); + } + + return rt->childpid = pid; } -void rote_vt_keypress(RoteTerm *rt, int keycode) +static int madtty_write(madtty_t *rt, const char *data, int len) { - char c = (char) keycode; + int res; - if (keytable['\n'] == NULL) - keytable_init(); + 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; +} + +void madtty_keypress(madtty_t *rt, int keycode) +{ + char c = (char)keycode; + const char *buf; + int len; if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) { - rote_vt_write(rt, keytable[keycode], strlen(keytable[keycode])); + buf = keytable[keycode]; + len = strlen(keytable[keycode]); } else { - rote_vt_write(rt, &c, 1); /* not special, just write it */ + buf = &c; + len = 1; + } + + while (len > 0) { + int res = madtty_write(rt, buf, len); + if (res < 0) + return; + + buf += res; + len -= res; } } +void madtty_initialize(void) +{ + setlocale(LC_ALL, ""); + initscr(); + noecho(); + start_color(); + use_default_colors(); + raw(); + nodelay(stdscr, TRUE); + keypad(stdscr, TRUE); + + for (int i = -1; i < 8; i++) { + for (int j = -1; j < 8; j++) { + init_pair((i + 1) * 16 + j + 1, i, j); + } + } +}