Copyright © 2006 Pierre Habouzit
*/
+#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <pty.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/ioctl.h>
+#include <sys/types.h>
#include "madtty.h"
+#define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
+
+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,
+};
+
+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,
+};
+
+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 mtty_row_t {
+ wchar_t *text;
+ uint16_t *attr;
+ unsigned dirty : 1;
+};
+
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_BACKSPACE] = "\177",
[KEY_HOME] = "\e[1~",
[KEY_IC] = "\e[2~",
[KEY_DC] = "\e[3~",
static void mtty_row_set(mtty_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;
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++) {
+ row->dirty = true;
+ }
}
}
mtty_row_set(rt->curs_row, 0, rt->cols, 0);
}
-static void cursor_line_up(madtty_t *rt)
-{
- rt->curs_row--;
- if (rt->curs_row >= rt->scroll_top)
- return;
-
- /* 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);
-}
-
__attribute__((const))
static uint16_t build_attrs(unsigned curattrs)
{
>> NCURSES_ATTR_SHIFT;
}
-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++;
-}
-
-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';
+ rt->elen = 0;
+ rt->ebuf[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;
- }
+ rt->elen = 0;
+ rt->ebuf[0] = '\0';
}
static bool is_valid_csi_ender(int c)
/* 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;
+ attr_t attr = build_attrs(rt->curattrs);
- switch (cmd) {
+ switch (pcount ? param[0] : 0) {
case 1:
- start = 0;
- len = rt->curs_col + 1;
+ mtty_row_set(rt->curs_row, 0, rt->curs_col + 1, attr);
break;
case 2:
- start = 0;
- len = rt->cols;
+ mtty_row_set(rt->curs_row, 0, rt->cols, attr);
break;
default:
- start = rt->curs_col;
- len = rt->cols - start;
+ mtty_row_set(rt->curs_row, rt->curs_col, rt->cols - rt->curs_col,
+ attr);
break;
}
-
- mtty_row_set(rt->curs_row, start, len, build_attrs(rt->curattrs));
}
/* Interpret the 'insert blanks' sequence (ICH) */
mtty_row_set(row, rt->cols - n, n, build_attrs(rt->curattrs));
}
+/* Interpret a 'scroll reverse' (SR) */
+static void interpret_csi_SR(madtty_t *rt)
+{
+ mtty_row_roll(rt->scroll_top, rt->scroll_bot, -1);
+ mtty_row_set(rt->scroll_top, 0, rt->cols, build_attrs(rt->curattrs));
+}
+
/* Interpret an 'insert line' sequence (IL) */
static void interpret_csi_IL(madtty_t *rt, int param[], int pcount)
{
mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
}
}
-
}
/* Interpret a 'delete line' sequence (DL) */
{
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];
+ const char *p = rt->ebuf + 1;
+ char verb = rt->ebuf[rt->elen - 1];
- if (!strncmp(rt->esbuf, "[?", 2)) { /* private-mode CSI, ignore */
- return;
- }
+ p += rt->ebuf[1] == '?'; /* CSI private mode */
/* parse numeric parameters */
while (isdigit((unsigned char)*p) || *p == ';') {
p++;
}
+ if (rt->ebuf[1] == '?') {
+ switch (verb) {
+ case 'l':
+ if (csiparam[0] == 25)
+ rt->curshid = true;
+ break;
+
+ case 'h':
+ if (csiparam[0] == 25)
+ rt->curshid = false;
+ break;
+ }
+ }
+
/* delegate handling depending on command character (verb) */
switch (verb) {
case 'h':
static void try_interpret_escape_seq(madtty_t *rt)
{
- char firstchar = rt->esbuf[0];
- char lastchar = rt->esbuf[rt->esbuf_len-1];
+ char lastchar = rt->ebuf[rt->elen-1];
- if (!firstchar)
- return; /* too early to do anything */
+ switch (*rt->ebuf) {
+ case '\0':
+ return;
- /* interpret ESC-M as reverse line-feed */
- if (firstchar == 'M') {
- cursor_line_up(rt);
+ case 'M':
+ interpret_csi_SR(rt);
cancel_escape_sequence(rt);
return;
+
+ case '(':
+ case ')':
+ if (rt->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(rt);
+ cancel_escape_sequence(rt);
+ return;
+ }
+ break;
}
- if (firstchar != '[' && firstchar != ']') {
- /* unrecognized escape sequence. Let's forget about it. */
+ if (rt->elen + 1 >= (int)sizeof(rt->ebuf)) {
+ int i;
+cancel:
+#if 0
+ 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);
- 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 */
+static void madtty_process_nonprinting(madtty_t *rt, wchar_t wc)
+{
+ switch (wc) {
+ case C0_ESC:
+ new_escape_sequence(rt);
+ 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 (rt->curs_col > 0)
+ rt->curs_col--;
+ 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;
+ break;
+
+ case C0_CR:
+ rt->curs_col = 0;
+ break;
+
+ case C0_VT:
+ case C0_FF:
+ case C0_LF:
+ cursor_line_down(rt);
+ break;
+
+ case C0_SO: /* shift out - acs */
+ rt->graphmode = true;
+ break;
+ case C0_SI: /* shift in - acs */
+ rt->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);
+void madtty_putc(madtty_t *rt, wchar_t wc)
+{
+ 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);
+ }
+#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);
+ } else if (IS_CONTROL(wc)) {
+ madtty_process_nonprinting(rt, wc);
+ } 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
+ };
+
+ if (wc >= 0x41 && wc <= 0x7e && vt100_0[wc - 0x41]) {
+ wc = vt100_0[wc - 0x41];
+ // width = 1; // vt100 line drawing characters are always single-width
+ }
+ }
+
+ 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] = wc;
+ tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
+ tmp->dirty = true;
+ rt->curs_col++;
+ }
}
int madtty_process(madtty_t *rt)
return -1;
}
- res = read(rt->pty, rt->rbuf + rt->rbuf_len,
- sizeof(rt->rbuf) - rt->rbuf_len);
+ res = read(rt->pty, rt->rbuf + rt->rlen, sizeof(rt->rbuf) - rt->rlen);
if (res < 0)
return -1;
- rt->rbuf_len += res;
- while (pos < rt->rbuf_len) {
+ rt->rlen += res;
+ while (pos < rt->rlen) {
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;
+ len = (ssize_t)mbrtowc(&wc, rt->rbuf + pos, rt->rlen - pos, &rt->ps);
+ if (len == -2) {
+ rt->rlen -= pos;
+ memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
+ return 0;
}
- pos += len ? len : 1;
-
- if (wc < ' ') {
- handle_control_char(rt, wc);
- continue;
+ if (len == -1) {
+ len = 1;
+ wc = rt->rbuf[pos];
}
- 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);
- }
+ pos += len ? len : 1;
+ madtty_putc(rt, wc);
}
- rt->rbuf_len -= pos;
- memmove(rt->rbuf, rt->rbuf + pos, rt->rbuf_len);
+ rt->rlen -= pos;
+ memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
return 0;
}
void madtty_draw(madtty_t *rt, WINDOW *win, int srow, int scol)
{
- int i, j;
-
- for (i = 0; i < rt->rows; i++) {
+ curs_set(0);
+ for (int 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 (!row->dirty)
+ continue;
+
+ wmove(win, srow + i, scol);
+ for (int 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) {
waddch(win, row->text[j] > ' ' ? row->text[j] : ' ');
}
}
+ row->dirty = false;
}
wmove(win, srow + rt->curs_row - rt->lines, scol + rt->curs_col);
+ curs_set(!rt->curshid);
}
/******************************************************/
if (pid == 0) {
setsid();
-
- setenv("TERM", "linux", 1);
+ setenv("TERM", "rxvt", 1);
execv(path, (char *const*)argv);
fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]);
exit(1);
return rt->childpid = pid;
}
-static 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;
-}
-
void madtty_keypress(madtty_t *rt, int keycode)
{
char c = (char)keycode;
buf = keytable[keycode];
len = strlen(keytable[keycode]);
} else {
+#if 0
+ if (keycode == KEY_F(3)) {
+ kill(-rt->childpid, SIGWINCH);
+ return;
+ }
+#endif
buf = &c;
len = 1;
}
while (len > 0) {
- int res = madtty_write(rt, buf, len);
- if (res < 0)
+ int res = write(rt->pty, buf, len);
+ if (res < 0 && errno != EAGAIN && errno != EINTR)
return;
buf += res;
{
setlocale(LC_ALL, "");
initscr();
- noecho();
start_color();
use_default_colors();
+ noecho();
raw();
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);