Terminal emulator with all the bells and whistles https://www.enlightenment.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4069 lines
112 KiB

#include "private.h"
#include <Elementary.h>
7 years ago
#include <stdint.h>
#include "col.h"
#include "termio.h"
#include "termpty.h"
#include "termptydbl.h"
#include "termptyesc.h"
#include "termptyops.h"
#include "termptyext.h"
#if defined(ENABLE_TESTS)
#include "tytest.h"
#endif
#undef CRITICAL
#undef ERR
#undef WRN
#undef INF
#undef DBG
#define CRITICAL(...) EINA_LOG_DOM_CRIT(_termpty_log_dom, __VA_ARGS__)
#define ERR(...) EINA_LOG_DOM_ERR(_termpty_log_dom, __VA_ARGS__)
#define WRN(...) EINA_LOG_DOM_WARN(_termpty_log_dom, __VA_ARGS__)
#define INF(...) EINA_LOG_DOM_INFO(_termpty_log_dom, __VA_ARGS__)
#define DBG(...) EINA_LOG_DOM_DBG(_termpty_log_dom, __VA_ARGS__)
#define ST 0x9c // String Terminator
#define BEL 0x07 // Bell
#define ESC 033 // Escape
#define DEL 127
#define UTF8CC 0xc2
/* XXX: all handle_ functions return the number of bytes successfully read, 0
* if not enough bytes could be read
*/
static const char *const ASCII_CHARS_TABLE[] =
{
"NUL", // '\0'
"SOH", // '\001'
"STX", // '\002'
"ETX", // '\003'
"EOT", // '\004'
"ENQ", // '\005'
"ACK", // '\006'
"BEL", // '\007'
"BS", // '\010'
"HT", // '\011'
"LF", // '\012'
"VT", // '\013'
"FF", // '\014'
"CR" , // '\015'
"SO", // '\016'
"SI", // '\017'
"DLE", // '\020'
"DC1", // '\021'
"DC2", // '\022'
"DC3", // '\023'
"DC4", // '\024'
"NAK", // '\025'
"SYN", // '\026'
"ETB", // '\027'
"CAN", // '\030'
"EM", // '\031'
"SUB", // '\032'
"ESC", // '\033'
"FS", // '\034'
"GS", // '\035'
"RS", // '\036'
"US" // '\037'
};
static const char *
_safechar(unsigned int c)
{
static char _str[9];
// This should avoid 'BEL' and 'ESC' in particular, which would
// have side effects in the parent terminal (esp. ESC).
if (c < (sizeof(ASCII_CHARS_TABLE) / sizeof(ASCII_CHARS_TABLE[0])))
return ASCII_CHARS_TABLE[c];
if (c == DEL)
return "DEL";
// The rest should be safe (?)
snprintf(_str, 9, "%c", c);
_str[8] = '\0';
return _str;
}
static Eina_Bool
_cursor_is_within_margins(const Termpty *ty)
{
return !(
((ty->termstate.top_margin > 0)
&& (ty->cursor_state.cy < ty->termstate.top_margin))
|| ((ty->termstate.bottom_margin > 0)
&& (ty->cursor_state.cy >= ty->termstate.bottom_margin))
|| ((ty->termstate.left_margin > 0)
&& (ty->cursor_state.cx < ty->termstate.left_margin))
|| ((ty->termstate.right_margin > 0)
&& (ty->cursor_state.cx >= ty->termstate.right_margin))
);
}
enum csi_arg_error {
CSI_ARG_NO_VALUE = 1,
CSI_ARG_ERROR = 2
};
static int
_csi_arg_get(Termpty *ty, Eina_Unicode **ptr)
{
Eina_Unicode *b = *ptr;
int sum = 0;
if ((b == NULL) || (*b == '\0'))
{
*ptr = NULL;
return -CSI_ARG_NO_VALUE;
}
/* Skip potential '?', '>'.... */
while ((*b) && ( (*b) != ';' && ((*b) < '0' || (*b) > '9')))
b++;
if (*b == ';')
{
b++;
*ptr = b;
return -CSI_ARG_NO_VALUE;
}
if (*b == '\0')
{
*ptr = NULL;
return -CSI_ARG_NO_VALUE;
}
while ((*b >= '0') && (*b <= '9'))
{
7 years ago
if (sum > INT32_MAX/10 )
{
ERR("Invalid sequence: argument is too large");
ty->decoding_error = EINA_TRUE;
goto error;
}
sum *= 10;
sum += *b - '0';
b++;
}
if ((*b == ';') || (*b == ':'))
{
if (b[1])
b++;
*ptr = b;
}
else if (*b == '\0')
{
*ptr = NULL;
}
else
{
*ptr = b;
}
return sum;
error:
ty->decoding_error = EINA_TRUE;
*ptr = NULL;
return -CSI_ARG_ERROR;
}
static void
_tab_forward(Termpty *ty, int n)
{
int cx = ty->cursor_state.cx;
for (; n > 0; n--)
{
do
{
cx++;
}
while ((cx < ty->w) && (!TAB_TEST(ty, cx)));
}
ty->cursor_state.cx = cx;
TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
}
static void
_cursor_to_start_of_line(Termpty *ty)
{
ty->cursor_state.cx = ty->termstate.left_margin;
}
static void
_handle_cursor_control(Termpty *ty, const Eina_Unicode *cc)
{
switch (*cc)
{
case 0x07: // BEL '\a' (bell)
if (ty->cb.bell.func) ty->cb.bell.func(ty->cb.bell.data);
return;
case 0x08: // BS '\b' (backspace)
DBG("->BS");
ty->termstate.wrapnext = 0;
ty->cursor_state.cx--;
TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
return;
case 0x09: // HT '\t' (horizontal tab)
DBG("->HT");
_tab_forward(ty, 1);
return;
case 0x0a: // LF '\n' (new line)
case 0x0b: // VT '\v' (vertical tab)
case 0x0c: // FF '\f' (form feed)
DBG("->LF");
ty->termstate.wrapnext = 0;
if (ty->termstate.crlf)
_cursor_to_start_of_line(ty);
ty->cursor_state.cy++;
termpty_text_scroll_test(ty, EINA_TRUE);
return;
case 0x0d: // CR '\r' (carriage ret)
DBG("->CR");
if (ty->cursor_state.cx != 0)
{
ty->termstate.had_cr_x = ty->cursor_state.cx;
ty->termstate.had_cr_y = ty->cursor_state.cy;
ty->termstate.wrapnext = 0;
}
_cursor_to_start_of_line(ty);
return;
default:
return;
}
}
static void
_switch_to_alternative_screen(Termpty *ty, int mode)
{
// swap screen content now
if (mode != ty->altbuf)
termpty_screen_swap(ty);
}
static void
_move_cursor_to_origin(Termpty *ty)
{
if (ty->termstate.restrict_cursor)
{
ty->cursor_state.cx = ty->termstate.left_margin;
TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
ty->cursor_state.cy = ty->termstate.top_margin;
TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
}
else
{
ty->cursor_state.cx = 0;
ty->cursor_state.cy = 0;
}
}
static void
_handle_esc_csi_reset_mode(Termpty *ty, Eina_Unicode cc, Eina_Unicode *b,
const Eina_Unicode * const end)
{
int mode = 0, priv = 0, arg;
if (cc == 'h')
mode = 1;
if (*b == '?')
{
priv = 1;
b++;
}
if (priv) /* DEC Private Mode Reset (DECRST) */
{
while (b && b <= end)
{
arg = _csi_arg_get(ty, &b);
// complete-ish list here:
// http://ttssh2.sourceforge.jp/manual/en/about/ctrlseq.html
switch (arg)
{
case -CSI_ARG_ERROR:
return;
/* TODO: -CSI_ARG_NO_VALUE */
case 1:
ty->termstate.appcursor = mode;
break;
case 2:
DBG("DECANM - ANSI MODE - VT52");
ty->termstate.kbd_lock = mode;
break;
case 3: // 132 column mode… should we handle this?
#if defined(SUPPORT_80_132_COLUMNS)
if (ty->termstate.att.is_80_132_mode_allowed)
{
/* ONLY FOR TESTING PURPOSE FTM */
Evas_Object *wn;
int w, h;
wn = termio_win_get(ty->obj);
elm_win_size_step_get(wn, &w, &h);
evas_object_resize(wn,
4 +
(mode ? 132 : 80) * w,
4 + ty->h * h);
termpty_resize(ty, mode ? 132 : 80,
ty->h);
termpty_reset_state(ty);
termpty_clear_screen(ty,
TERMPTY_CLR_ALL);
}
#endif
break;
case 4:
DBG("scrolling mode (DECSCLM): %i (always fast mode)", mode);
break;
case 5:
ty->termstate.reverse = mode;
break;
case 6:
/* DECOM */
if (mode)
{
/* set, within margins */
ty->termstate.restrict_cursor = 1;
}
else
{
ty->termstate.restrict_cursor = 0;
}
_move_cursor_to_origin(ty);
DBG("DECOM: mode (%d): cursor is at 0,0"
" cursor limited to screen/start point"
" for line #'s depends on top margin",
mode);
break;
case 7:
DBG("DECAWM: set/reset wrap mode to %i", mode);
ty->termstate.wrap = mode;
break;
case 8:
ty->termstate.no_autorepeat = !mode;
DBG("DECARM - auto repeat %i", mode);
break;
case 9:
DBG("set mouse (X10) %i", mode);
if (mode) ty->mouse_mode = MOUSE_X10;
else ty->mouse_mode = MOUSE_OFF;
break;
case 12: // ignore
WRN("TODO: set blinking cursor to (stop?) %i or local echo (ignored)", mode);
break;
case 19: // never seen this - what to do?
WRN("TODO: set print extent to full screen");
ty->decoding_error = EINA_TRUE;
break;
case 20: // crfl==1 -> cur moves to col 0 on LF, FF or VT, ==0 -> mode is cr+lf
ty->termstate.crlf = mode;
break;
case 25:
ty->termstate.hide_cursor = !mode;
DBG("hide cursor: %d", !mode);
break;
case 30: // ignore
WRN("TODO: set scrollbar mapping %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 33: // ignore
WRN("TODO: Stop cursor blink %i", mode);
break;
case 34: // ignore
WRN("TODO: Underline cursor mode %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 35: // ignore
WRN("TODO: set shift keys %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 38: // ignore
WRN("TODO: switch to tek window %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 40:
DBG("Allow 80 -> 132 Mode %i", mode);
#if defined(SUPPORT_80_132_COLUMNS)
ty->termstate.att.is_80_132_mode_allowed = mode;
#endif
break;
case 45: // ignore
WRN("TODO: Reverse-wraparound Mode");
ty->decoding_error = EINA_TRUE;
break;
case 47:
_switch_to_alternative_screen(ty, mode);
break;
case 59: // ignore
WRN("TODO: kanji terminal mode %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 66:
WRN("TODO: app keypad mode %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 67:
ty->termstate.send_bs = mode;
DBG("backspace send bs not del = %i", mode);
break;
case 69:
ty->termstate.lr_margins = mode;
if (!mode)
{
ty->termstate.left_margin = 0;
ty->termstate.right_margin = 0;
}
break;
case 98:
DBG("DECARSM Set/Reset Auto Resize Mode: %d", mode);
break;
case 100:
DBG("DECAAM Set/Reset Auto Answerback Mode: %d", mode);
break;
case 101:
DBG("DECCANSM Set/Reset Conceal Answerback Message Mode: %d", mode);
break;
case 109:
DBG("DECCAPSLK Set/Reset Caps Lock Mode: %d", mode);
break;
case 1000:
if (mode) ty->mouse_mode = MOUSE_NORMAL;
else ty->mouse_mode = MOUSE_OFF;
DBG("set mouse (press+release only) to %i", mode);
break;
case 1001:
WRN("TODO: x11 mouse highlighting %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 1002:
if (mode) ty->mouse_mode = MOUSE_NORMAL_BTN_MOVE;
else ty->mouse_mode = MOUSE_OFF;
DBG("set mouse (press+release+motion while pressed) %i", mode);
break;
case 1003:
if (mode) ty->mouse_mode = MOUSE_NORMAL_ALL_MOVE;
else ty->mouse_mode = MOUSE_OFF;
DBG("set mouse (press+release+all motion) %i", mode);
break;
case 1004: // i dont know what focus repporting is?
WRN("TODO: enable focus reporting %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 1005:
if (mode) ty->mouse_ext = MOUSE_EXT_UTF8;
else ty->mouse_ext = MOUSE_EXT_NONE;
DBG("set mouse (xterm utf8 style) %i", mode);
break;
case 1006:
if (mode) ty->mouse_ext = MOUSE_EXT_SGR;
else ty->mouse_ext = MOUSE_EXT_NONE;
DBG("set mouse (xterm sgr style) %i", mode);
break;
case 1010: // ignore
WRN("TODO: set home on tty output %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 1012: // ignore
WRN("TODO: set home on tty input %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 1015:
if (mode) ty->mouse_ext = MOUSE_EXT_URXVT;
else ty->mouse_ext = MOUSE_EXT_NONE;
DBG("set mouse (rxvt-unicode style) %i", mode);
break;
case 1034: // ignore
/* libreadline6 emits it but it shouldn't.
See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=577012
*/
DBG("Ignored screen mode %i", arg);
ty->decoding_error = EINA_TRUE;
break;
case 1047:
if (!mode && ty->altbuf)
/* clear screen before switching back to normal */
termpty_clear_screen(ty, TERMPTY_CLR_ALL);
_switch_to_alternative_screen(ty, mode);
break;
case 1048:
termpty_cursor_copy(ty, mode);
break;
case 1049:
if (mode)
{
// switch to altbuf
termpty_cursor_copy(ty, mode);
_switch_to_alternative_screen(ty, mode);
if (ty->altbuf)
/* clear screen before switching back to normal */
termpty_clear_screen(ty, TERMPTY_CLR_ALL);
}
else
{
if (ty->altbuf)
/* clear screen before switching back to normal */
termpty_clear_screen(ty, TERMPTY_CLR_ALL);
_switch_to_alternative_screen(ty, mode);
termpty_cursor_copy(ty, mode);
}
break;
case 2004:
ty->bracketed_paste = mode;
break;
case 7727: // ignore
WRN("TODO: enable application escape mode %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 7786: // ignore
WRN("TODO: enable mouse wheel -> cursor key xlation %i", mode);
ty->decoding_error = EINA_TRUE;
break;
default:
WRN("Unhandled DEC Private Reset Mode arg %i", arg);
ty->decoding_error = EINA_TRUE;
break;
}
}
}
else /* Reset Mode (RM) */
{
while (b && b <= end)
{
arg = _csi_arg_get(ty, &b);
switch (arg)
{
case -CSI_ARG_ERROR:
return;
/* TODO: -CSI_ARG_NO_VALUE */
case 1:
ty->termstate.appcursor = mode;
break;
case 3:
WRN("CRM - Show Control Character Mode: %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 4:
DBG("set insert mode to %i", mode);
ty->termstate.insert = mode;
break;
case 34:
WRN("TODO: hebrew keyboard mapping: %i", mode);
ty->decoding_error = EINA_TRUE;
break;
case 36:
WRN("TODO: hebrew encoding mode: %i", mode);
ty->decoding_error = EINA_TRUE;
break;
default:
WRN("Unhandled ANSI Reset Mode arg %i", arg);
ty->decoding_error = EINA_TRUE;
}
}
}
}
static int
_csi_truecolor_arg_get(Termpty *ty, Eina_Unicode **ptr)
{
Eina_Unicode *b = *ptr;
int sum = 0;
char separator;
if ((b == NULL) || (*b == '\0'))
{
*ptr = NULL;
return -CSI_ARG_NO_VALUE;
}
/* by construction, shall be the same separator as the following ones */
separator = *(b-1);
if ((separator != ';') && (separator != ':'))
{
*ptr = NULL;
return -CSI_ARG_NO_VALUE;
}
if (*b == separator)
{
b++;
*ptr = b;
return -CSI_ARG_NO_VALUE;
}
if (*b == '\0')
{
*ptr = NULL;
return -CSI_ARG_NO_VALUE;
}
while ((*b >= '0') && (*b <= '9'))
{
if (sum > INT32_MAX/10 )
{
*ptr = NULL;
ERR("Invalid sequence: argument is too large");
ty->decoding_error = EINA_TRUE;
return -CSI_ARG_ERROR;
}
sum *= 10;
sum += *b - '0';
b++;
}
if (*b == separator)
{
b++;
*ptr = b;
}
else if (*b == '\0')
{
*ptr = NULL;
}
else
{
*ptr = b;
}
return sum;
}
static int
_approximate_truecolor_rgb(Termpty *ty, int r0, int g0, int b0)
{
int chosen_color = COL_DEF;
/* TODO: use the function in tests */
#if defined(ENABLE_FUZZING)
(void) ty;
(void) r0;
(void) g0;
(void) b0;
#else
int c;
int distance_min = INT_MAX;
Evas_Object *textgrid;
DBG("approximating r:%d g:%d b:%d", r0, g0, b0);
textgrid = termio_textgrid_get(ty->obj);
for (c = 0; c < 256; c++)
{
int r1 = 0, g1 = 0, b1 = 0, a1 = 0;
int delta_red_sq, delta_green_sq, delta_blue_sq, middle_red;
int distance;
evas_object_textgrid_palette_get(textgrid,
EVAS_TEXTGRID_PALETTE_EXTENDED,
c, &r1, &g1, &b1, &a1);
/* Compute the color distance
* XXX: this is inacurate but should give good enough results.
* See * https://en.wikipedia.org/wiki/Color_difference
*/
middle_red = (r0 + r1) / 2;
delta_red_sq = (r0 - r1) * (r0 - r1);
delta_green_sq = (g0 - g1) * (g0 - g1);
delta_blue_sq = (b0 - b1) * (b0 - b1);
distance = 2 * delta_red_sq
+ 4 * delta_green_sq
+ 3 * delta_blue_sq
+ ((middle_red) * (delta_red_sq - delta_blue_sq) / 256);
if (distance < distance_min)
{
distance_min = distance;
chosen_color = c;
}
}
#endif
return chosen_color;
}
static int
_handle_esc_csi_truecolor_rgb(Termpty *ty, Eina_Unicode **ptr)
{
int r, g, b;
Eina_Unicode *u = *ptr;
char separator;
if ((u == NULL) || (*u == '\0'))
{
return COL_DEF;
}
separator = *(u-1);
r = _csi_truecolor_arg_get(ty, ptr);
g = _csi_truecolor_arg_get(ty, ptr);
b = _csi_truecolor_arg_get(ty, ptr);
if ((r == -CSI_ARG_ERROR) ||
(g == -CSI_ARG_ERROR) ||
(b == -CSI_ARG_ERROR))
return COL_DEF;
if (separator == ':' && *ptr)
{
if (**ptr != ';')
{
/* then the first parameter was a color-space-id (ignored) */
r = g;
g = b;
b = _csi_truecolor_arg_get(ty, ptr);
/* Skip other parameters */
while ((*ptr) && (**ptr != ';'))
{
int arg = _csi_truecolor_arg_get(ty, ptr);
if (arg == -CSI_ARG_ERROR)
break;
}
}
if ((*ptr) && (**ptr == ';'))
{
*ptr = (*ptr) + 1;
}
}
if (r == -CSI_ARG_NO_VALUE)
r = 0;
if (g == -CSI_ARG_NO_VALUE)
g = 0;
if (b == -CSI_ARG_NO_VALUE)
b = 0;
return _approximate_truecolor_rgb(ty, r, g, b);
}
static int
_handle_esc_csi_truecolor_cmy(Termpty *ty, Eina_Unicode **ptr)
{
int r, g, b, c, m, y;
Eina_Unicode *u = *ptr;
char separator;
if ((u == NULL) || (*u == '\0'))
{
return COL_DEF;
}
separator = *(u-1);
/* Considering CMY stored as percents */
c = _csi_truecolor_arg_get(ty, ptr);
m = _csi_truecolor_arg_get(ty, ptr);
y = _csi_truecolor_arg_get(ty, ptr);
if ((c == -CSI_ARG_ERROR) ||
(m == -CSI_ARG_ERROR) ||
(y == -CSI_ARG_ERROR))
return COL_DEF;
if (separator == ':' && *ptr)
{
if (**ptr != ';')
{
/* then the first parameter was a color-space-id (ignored) */
c = m;
m = y;
y = _csi_truecolor_arg_get(ty, ptr);
/* Skip other parameters */
while ((*ptr) && (**ptr != ';'))
{
int arg = _csi_truecolor_arg_get(ty, ptr);
if (arg == -CSI_ARG_ERROR)
break;
}
}
if ((*ptr) && (**ptr == ';'))
{
*ptr = (*ptr) + 1;
}
}
if (c == -CSI_ARG_NO_VALUE)
c = 0;
if (m == -CSI_ARG_NO_VALUE)
m = 0;
if (y == -CSI_ARG_NO_VALUE)
y = 0;
r = 255 - ((c * 255) / 100);
g = 255 - ((m * 255) / 100);
b = 255 - ((y * 255) / 100);
return _approximate_truecolor_rgb(ty, r, g, b);
}
static int
_handle_esc_csi_truecolor_cmyk(Termpty *ty, Eina_Unicode **ptr)
{
int r, g, b, c, m, y, k;
Eina_Unicode *u = *ptr;
char separator;
if ((u == NULL) || (*u == '\0'))
{
return COL_DEF;
}
separator = *(u-1);
/* Considering CMYK stored as percents */
c = _csi_truecolor_arg_get(ty, ptr);
m = _csi_truecolor_arg_get(ty, ptr);
y = _csi_truecolor_arg_get(ty, ptr);
k = _csi_truecolor_arg_get(ty, ptr);
if ((c == -CSI_ARG_ERROR) ||
(m == -CSI_ARG_ERROR) ||
(y == -CSI_ARG_ERROR) ||
(k == -CSI_ARG_ERROR))
return COL_DEF;
if (separator == ':' && *ptr)
{
if (**ptr != ';')
{
/* then the first parameter was a color-space-id (ignored) */
c = m;
m = y;
y = k;
k = _csi_truecolor_arg_get(ty, ptr);
/* Skip other parameters */
while ((*ptr) && (**ptr !=