From d377f5b12f9d4baa08fa02c92f9f383a0a320d54 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 11 Nov 2026 09:02:54 -0800 Subject: [PATCH 1/4] wolfterm: implement CSI J and K erase modes The Windows console translator handles ED and EL modes as ECMA-48 does: an omitted mode is 0, mode 0 erases from the cursor to the end, mode 1 from the start through the cursor cell, and mode 2 the whole display or line. Mode 1J now clears the rows above the cursor too. - ED 0-2 take the display as the whole buffer, the rows CSI H addresses, so no old text is left where the cursor can go - Erased cells take the current attributes, so an old background colour does not survive the erase - wolfSSH_ClearScreen() wraps at the buffer width, not the maximum window width Issue: F-10556, F-12549 --- src/wolfterm.c | 101 +++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 53 deletions(-) diff --git a/src/wolfterm.c b/src/wolfterm.c index 786cee4d0..05560d6a3 100644 --- a/src/wolfterm.c +++ b/src/wolfterm.c @@ -100,7 +100,8 @@ static void wolfSSH_CursorMove(WOLFSSH_HANDLE h, word32 x, word32 y, byte stomp) } -/* clear screen (put ' ' chars) from (x1,y1) to (x2,y2) */ +/* clear screen (put ' ' chars in the current attributes) from (x1,y1) up to, + * not including, (x2,y2) */ static void wolfSSH_ClearScreen(WOLFSSH_HANDLE handle, word32 x1, word32 y1, word32 x2, word32 y2) { CONSOLE_SCREEN_BUFFER_INFO info = { 0 }; @@ -110,7 +111,7 @@ static void wolfSSH_ClearScreen(WOLFSSH_HANDLE handle, word32 x1, word32 y1, wor word32 maxX = 0; GetConsoleScreenBufferInfo(handle, &info); - maxX = info.dwMaximumWindowSize.X; + maxX = info.dwSize.X; /* fills wrap at the buffer width */ start.X = x1; start.Y = y1; @@ -127,6 +128,7 @@ static void wolfSSH_ClearScreen(WOLFSSH_HANDLE handle, word32 x1, word32 y1, wor } FillConsoleOutputCharacterA(handle, ' ', fill, start, &w); + FillConsoleOutputAttribute(handle, info.wAttributes, fill, start, &w); } @@ -533,78 +535,71 @@ static int wolfSSH_DoControlSeq(WOLFSSH* ssh, WOLFSSH_HANDLE handle, byte* buf, wolfSSH_CursorMove(handle, args[0], 0, 0); break; - case 'J': /* erase display */ - if (numArgs == 0) { - WLOG(WS_LOG_DEBUG, "CSI 'J' expects an argument"); - args[0] = 2; /* default to erase screen */ - } + case 'J': /* erase display; an omitted mode is 0 */ + { + CONSOLE_SCREEN_BUFFER_INFO info = { 0 }; + word32 x, y, end; + + GetConsoleScreenBufferInfo(handle, &info); + x = (word32)info.dwCursorPosition.X; + y = (word32)info.dwCursorPosition.Y; + /* The display is the whole buffer, as CSI H addresses buffer + * rows from row 0. */ + end = (word32)info.dwSize.Y; switch (args[0]) { - case 1: /* erase from start to cursor */ - { - CONSOLE_SCREEN_BUFFER_INFO info = { 0 }; - GetConsoleScreenBufferInfo(handle, &info); - wolfSSH_ClearScreen(handle, 0, info.dwCursorPosition.Y, - info.dwCursorPosition.X, info.dwCursorPosition.Y); - } + case 0: /* erase from cursor to end of buffer */ + wolfSSH_ClearScreen(handle, x, y, 0, end); break; - case 2: /* erase whole display */ - { - CONSOLE_SCREEN_BUFFER_INFO info = { 0 }; - GetConsoleScreenBufferInfo(handle, &info); - wolfSSH_ClearScreen(handle, 0, 0, info.dwSize.X, info.dwSize.Y); - } + case 1: /* erase from start of buffer through cursor */ + wolfSSH_ClearScreen(handle, 0, 0, x + 1, y); + break; + + case 2: /* erase whole buffer */ + wolfSSH_ClearScreen(handle, 0, 0, 0, end); break; case 3: /* erase whole display + */ - { - CONSOLE_SCREEN_BUFFER_INFO info = { 0 }; - GetConsoleScreenBufferInfo(handle, &info); - wolfSSH_ClearScreen(handle, 0, 0, info.dwMaximumWindowSize.X, + wolfSSH_ClearScreen(handle, 0, 0, + info.dwMaximumWindowSize.X, info.dwMaximumWindowSize.Y); wolfSSH_CursorMove(handle, 0, 0, 1); - } break; default: WLOG(WS_LOG_DEBUG, "Unexpected erase value %d", args[0]); } break; + } - case 'K': - if (numArgs == 0) { /* erase start of cursor to end of line */ - { - CONSOLE_SCREEN_BUFFER_INFO info = { 0 }; - GetConsoleScreenBufferInfo(handle, &info); - wolfSSH_ClearScreen(handle, info.dwCursorPosition.X, - info.dwCursorPosition.Y, info.dwMaximumWindowSize.X, - info.dwCursorPosition.Y); - } - break; - } - else { - switch (args[0]) { - case 1: /* erase start of line to cursor */ - { - CONSOLE_SCREEN_BUFFER_INFO info = { 0 }; - GetConsoleScreenBufferInfo(handle, &info); - wolfSSH_ClearScreen(handle, 0, info.dwCursorPosition.Y, - info.dwCursorPosition.X, info.dwCursorPosition.Y); - } + case 'K': /* erase line; an omitted mode is 0 */ + { + CONSOLE_SCREEN_BUFFER_INFO info = { 0 }; + word32 x, y; + + GetConsoleScreenBufferInfo(handle, &info); + x = (word32)info.dwCursorPosition.X; + y = (word32)info.dwCursorPosition.Y; + + switch (args[0]) { + case 0: /* erase from cursor to end of line */ + wolfSSH_ClearScreen(handle, x, y, info.dwSize.X, y); break; - case 2: /* erase whole line */ - { - CONSOLE_SCREEN_BUFFER_INFO info = { 0 }; - GetConsoleScreenBufferInfo(handle, &info); - wolfSSH_ClearScreen(handle, 0, info.dwCursorPosition.Y, - info.dwMaximumWindowSize.X, info.dwCursorPosition.Y); - } + case 1: /* erase from start of line through cursor */ + wolfSSH_ClearScreen(handle, 0, y, x + 1, y); break; - } + + case 2: /* erase whole line */ + wolfSSH_ClearScreen(handle, 0, y, info.dwSize.X, y); + break; + + default: + WLOG(WS_LOG_DEBUG, "Unexpected erase value %d", args[0]); } break; + } case 'm': /* set display attributes */ From 6c94370857ad6e350377f6facbc91260506f5382 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 11 Nov 2026 09:03:45 -0800 Subject: [PATCH 2/4] wolfterm: accumulate SGR attributes per sequence doDisplayAttributes() starts from the current console attributes, applies each SGR argument to that value in order, and sets the result once, so ESC[1;31m keeps the bold. SGR 0 resets to the default attributes mid-sequence. - SGR 25 (blink off) is a no-op; there is no blink to turn off - SGR 39 restores the default foreground colour and keeps the bold Issue: F-11663, F-15506 --- src/wolfterm.c | 58 ++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/wolfterm.c b/src/wolfterm.c index 05560d6a3..bc5e11f4f 100644 --- a/src/wolfterm.c +++ b/src/wolfterm.c @@ -147,133 +147,131 @@ static void doDisplayAttributes(WOLFSSH* ssh, WOLFSSH_HANDLE handle, word32* arg numArgs = 1; } + /* attributes accumulate across the arguments */ for (i = 0; i < numArgs; i++) { switch (args[i]) { case 0: /* reset all to defaults */ - SetConsoleTextAttribute(handle, ssh->defaultAttr); + atr = ssh->defaultAttr; break; case 1: /* set bold */ - SetConsoleTextAttribute(handle, (atr | FOREGROUND_INTENSITY)); + atr |= FOREGROUND_INTENSITY; break; case 2: /* set half-bright */ break; case 4: /* set undersocre */ - SetConsoleTextAttribute(handle, atr | BACKGROUND_INTENSITY); + atr |= BACKGROUND_INTENSITY; break; case 10: /* reset mapping and display control flag */ break; case 24: /* turn off underline */ - SetConsoleTextAttribute(handle, atr & ~BACKGROUND_INTENSITY); + atr &= ~BACKGROUND_INTENSITY; break; case 21: case 22: /* normal intensity */ atr &= ~BACKGROUND_INTENSITY; atr &= ~FOREGROUND_INTENSITY; - SetConsoleTextAttribute(handle, atr); break; - case 25: /* blink off */ - SetConsoleTextAttribute(handle, (atr | BACKGROUND_INTENSITY)); + case 25: /* blink off, blink is not supported */ break; case 30: /* set black foreground */ - SetConsoleTextAttribute(handle, (atr & ~(WS_MASK_RBGFG))); + atr &= ~WS_MASK_RBGFG; break; case 31: /* red foreground */ atr &= ~WS_MASK_RBGFG; - SetConsoleTextAttribute(handle, atr | FOREGROUND_RED); + atr |= FOREGROUND_RED; break; case 32: /* green foreground */ atr &= ~WS_MASK_RBGFG; - SetConsoleTextAttribute(handle, atr | FOREGROUND_GREEN); + atr |= FOREGROUND_GREEN; break; case 33: /* brown foreground */ atr &= ~WS_MASK_RBGFG; - SetConsoleTextAttribute(handle, atr | FOREGROUND_GREEN | - FOREGROUND_RED); + atr |= FOREGROUND_GREEN | FOREGROUND_RED; break; case 34: /* blue foreground */ atr &= ~WS_MASK_RBGFG; - SetConsoleTextAttribute(handle, atr | FOREGROUND_BLUE); + atr |= FOREGROUND_BLUE; break; case 35: /* magenta foreground */ atr &= ~WS_MASK_RBGFG; - SetConsoleTextAttribute(handle, (atr | FOREGROUND_RED | - FOREGROUND_BLUE)); + atr |= FOREGROUND_RED | FOREGROUND_BLUE; break; case 36: /* cyan foreground */ atr &= ~WS_MASK_RBGFG; - SetConsoleTextAttribute(handle, atr | FOREGROUND_BLUE | - FOREGROUND_GREEN); + atr |= FOREGROUND_BLUE | FOREGROUND_GREEN; break; case 37: /* white foreground */ - SetConsoleTextAttribute(handle, (atr | WS_MASK_RBGFG)); + atr |= WS_MASK_RBGFG; + break; + + case 39: /* default foreground, bold kept */ + atr &= ~WS_MASK_RBGFG; + atr |= (ssh->defaultAttr & WS_MASK_RBGFG); break; case 40: /* black background */ - SetConsoleTextAttribute(handle, atr &= ~WS_MASK_RBGBG); + atr &= ~WS_MASK_RBGBG; break; case 41: /* red background */ atr &= ~WS_MASK_RBGBG; - SetConsoleTextAttribute(handle, (atr | BACKGROUND_RED)); + atr |= BACKGROUND_RED; break; case 42: /* green background */ atr &= ~WS_MASK_RBGBG; - SetConsoleTextAttribute(handle, (atr | BACKGROUND_GREEN)); + atr |= BACKGROUND_GREEN; break; case 43: /* brown background */ atr &= ~WS_MASK_RBGBG; - SetConsoleTextAttribute(handle, (atr | BACKGROUND_RED | - BACKGROUND_GREEN)); + atr |= BACKGROUND_RED | BACKGROUND_GREEN; break; case 44: /* blue background */ atr &= ~WS_MASK_RBGBG; - SetConsoleTextAttribute(handle, (atr | BACKGROUND_BLUE)); + atr |= BACKGROUND_BLUE; break; case 45: /* magenta background */ atr &= ~WS_MASK_RBGBG; - SetConsoleTextAttribute(handle, (atr | BACKGROUND_RED | - BACKGROUND_BLUE)); + atr |= BACKGROUND_RED | BACKGROUND_BLUE; break; case 46: /* cyan background */ atr &= ~WS_MASK_RBGBG; - SetConsoleTextAttribute(handle, (atr | BACKGROUND_BLUE | - BACKGROUND_GREEN)); + atr |= BACKGROUND_BLUE | BACKGROUND_GREEN; break; case 47: /* white background */ - SetConsoleTextAttribute(handle, (atr | WS_MASK_RBGBG)); + atr |= WS_MASK_RBGBG; break; case 49: /* default background */ atr &= ~WS_MASK_BG; atr |= (ssh->defaultAttr & WS_MASK_BG); - SetConsoleTextAttribute(handle, atr); break; default: WLOG(WS_LOG_DEBUG, "Unknown display attribute %d", args[i]); } } + SetConsoleTextAttribute(handle, (WORD)atr); } From a84392074a781b2518134eaad24f950f33b0cd38 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 11 Nov 2026 09:07:06 -0800 Subject: [PATCH 3/4] tests: check Windows CSI erase and SGR results test_wolfSSH_ConvertConsole_csi() runs ED, EL and SGR sequences against an off-screen console buffer and reads back the cells and attributes. It returns early when no console is attached. - ED 0-2 run again with the window scrolled down, with the cursor inside and below it, and with 2J before and after CSI H - Erased cells are checked for the current attributes - SGR 39 is checked with and without bold Issue: F-10556, F-12549, F-11663, F-15506 --- tests/api.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) diff --git a/tests/api.c b/tests/api.c index 211b0c633..3f9459a45 100644 --- a/tests/api.c +++ b/tests/api.c @@ -9715,6 +9715,211 @@ static void test_wolfSSH_ConvertConsole(void) } +#ifdef USE_WINDOWS_API +/* Run seq on an 'x'-filled buffer; return the character at (x,y). A win + * is set after the cursor, so the cursor can sit outside it. */ +static char ConsoleCsiCellWin(HANDLE h, const char* seq, COORD cursor, + const SMALL_RECT* win, SHORT x, SHORT y) +{ + WOLFSSH_CTX* ctx; + WOLFSSH* ssh; + CONSOLE_SCREEN_BUFFER_INFO info; + COORD origin = { 0, 0 }; + COORD cell; + DWORD n = 0; + char c = 0; + + AssertTrue(GetConsoleScreenBufferInfo(h, &info)); + AssertTrue(FillConsoleOutputCharacterA(h, 'x', + (DWORD)info.dwSize.X * (DWORD)info.dwSize.Y, origin, &n)); + AssertTrue(SetConsoleCursorPosition(h, cursor)); + if (win != NULL) { + AssertTrue(SetConsoleWindowInfo(h, TRUE, win)); + } + + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL)); + AssertNotNull(ssh = wolfSSH_new(ctx)); + AssertIntEQ(wolfSSH_ConvertConsole(ssh, h, (byte*)seq, + (word32)WSTRLEN(seq)), WS_SUCCESS); + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + + cell.X = x; + cell.Y = y; + AssertTrue(ReadConsoleOutputCharacterA(h, &c, 1, cell, &n)); + return c; +} + +/* ConsoleCsiCellWin() with the cursor at (4,2) and the window unchanged. */ +static char ConsoleCsiCell(HANDLE h, const char* seq, SHORT x, SHORT y) +{ + COORD cursor = { 4, 2 }; + + return ConsoleCsiCellWin(h, seq, cursor, NULL, x, y); +} + +/* Run seq with the cursor at (4,2) on cells in BACKGROUND_GREEN, with cur as + * the current attributes; return the attributes at (x,y). */ +static WORD ConsoleEraseAttr(HANDLE h, const char* seq, WORD cur, + SHORT x, SHORT y) +{ + WOLFSSH_CTX* ctx; + WOLFSSH* ssh; + CONSOLE_SCREEN_BUFFER_INFO info; + COORD origin = { 0, 0 }; + COORD cursor = { 4, 2 }; + COORD cell; + DWORD n = 0; + WORD a = 0; + + AssertTrue(GetConsoleScreenBufferInfo(h, &info)); + AssertTrue(FillConsoleOutputAttribute(h, BACKGROUND_GREEN, + (DWORD)info.dwSize.X * (DWORD)info.dwSize.Y, origin, &n)); + AssertTrue(SetConsoleTextAttribute(h, cur)); + AssertTrue(SetConsoleCursorPosition(h, cursor)); + + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL)); + AssertNotNull(ssh = wolfSSH_new(ctx)); + AssertIntEQ(wolfSSH_ConvertConsole(ssh, h, (byte*)seq, + (word32)WSTRLEN(seq)), WS_SUCCESS); + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + + cell.X = x; + cell.Y = y; + AssertTrue(ReadConsoleOutputAttribute(h, &a, 1, cell, &n)); + return a; +} + +/* Run seq from attributes start; return the resulting attributes. */ +static WORD ConsoleSgr(HANDLE h, WORD start, const char* seq) +{ + WOLFSSH_CTX* ctx; + WOLFSSH* ssh; + CONSOLE_SCREEN_BUFFER_INFO info; + + AssertTrue(SetConsoleTextAttribute(h, start)); + AssertNotNull(ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL)); + AssertNotNull(ssh = wolfSSH_new(ctx)); + /* the first call records start as the default attributes */ + AssertIntEQ(wolfSSH_ConvertConsole(ssh, h, (byte*)seq, + (word32)WSTRLEN(seq)), WS_SUCCESS); + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + + AssertTrue(GetConsoleScreenBufferInfo(h, &info)); + return info.wAttributes; +} +#endif /* USE_WINDOWS_API */ + + +static void test_wolfSSH_ConvertConsole_csi(void) +{ +#ifdef USE_WINDOWS_API + HANDLE h; + SHORT w, wh; + COORD size; + SMALL_RECT win; + CONSOLE_SCREEN_BUFFER_INFO info; + const WORD def = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; + const WORD red = FOREGROUND_RED; + + h = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, + CONSOLE_TEXTMODE_BUFFER, NULL); + if (h == INVALID_HANDLE_VALUE) { + return; /* no console attached */ + } + AssertTrue(GetConsoleScreenBufferInfo(h, &info)); + w = info.dwSize.X; + + /* erase display: omitted and 0 erase cursor to end */ + AssertIntEQ(ConsoleCsiCell(h, "\x1b[J", 3, 2), 'x'); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[J", 4, 2), ' '); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[J", 0, 3), ' '); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[0J", 3, 2), 'x'); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[0J", 4, 2), ' '); + /* 1J erases the rows above and the line through the cursor */ + AssertIntEQ(ConsoleCsiCell(h, "\x1b[1J", 0, 0), ' '); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[1J", w - 1, 1), ' '); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[1J", 4, 2), ' '); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[1J", 5, 2), 'x'); + + /* erase line */ + AssertIntEQ(ConsoleCsiCell(h, "\x1b[K", 3, 2), 'x'); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[K", w - 1, 2), ' '); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[K", 0, 3), 'x'); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[0K", 4, 2), ' '); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[0K", 3, 2), 'x'); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[1K", 4, 2), ' '); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[1K", 5, 2), 'x'); + AssertIntEQ(ConsoleCsiCell(h, "\x1b[1K", w - 1, 1), 'x'); + + /* erased cells take the current attributes */ + AssertIntEQ(ConsoleEraseAttr(h, "\x1b[K", def, 3, 2), BACKGROUND_GREEN); + AssertIntEQ(ConsoleEraseAttr(h, "\x1b[K", def, w - 1, 2), def); + AssertIntEQ(ConsoleEraseAttr(h, "\x1b[2J", def, 0, 0), def); + + /* SGR arguments accumulate */ + AssertIntEQ(ConsoleSgr(h, def, "\x1b[1;31m"), red | FOREGROUND_INTENSITY); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[31;1m"), red | FOREGROUND_INTENSITY); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[44;33m"), + BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[31;0m"), def); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[25m"), def); + /* SGR 39 restores the foreground colour, not the bold */ + AssertIntEQ(ConsoleSgr(h, def, "\x1b[31;39m"), def); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[1;31;39m"), + def | FOREGROUND_INTENSITY); + + /* window scrolled down two rows: erase display clears the rows above + * it too, as CSI H can move the cursor there */ + wh = (SHORT)(info.srWindow.Bottom - info.srWindow.Top + 1); + size.X = w; + size.Y = (SHORT)(wh + 4); + win.Left = 0; + win.Top = 2; + win.Right = info.srWindow.Right; + win.Bottom = (SHORT)(wh + 1); + if (SetConsoleScreenBufferSize(h, size) + && SetConsoleWindowInfo(h, TRUE, &win) + && GetConsoleScreenBufferInfo(h, &info) + && info.srWindow.Top == 2) { + COORD cur = { 4, 3 }; + COORD out = { 4, 0 }; + + out.Y = (SHORT)(wh + 3); /* below the window */ + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[1J", cur, &win, w - 1, 1), + ' '); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[1J", cur, &win, 4, 3), ' '); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[1J", cur, &win, 5, 3), 'x'); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[0J", cur, &win, 3, 3), 'x'); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[0J", cur, &win, w - 1, + wh + 1), ' '); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[0J", cur, &win, 0, wh + 2), + ' '); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[2J", cur, &win, 0, 0), ' '); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[2J", cur, &win, w - 1, + wh + 1), ' '); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[2J", cur, &win, 0, wh + 2), + ' '); + /* 2J then H: the cursor must not land on old text */ + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[2J\x1b[H", cur, &win, 0, 0), + ' '); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[2J\x1b[H", cur, &win, 5, 1), + ' '); + /* cursor below the window: 2J still clears the visible rows */ + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[2J", out, &win, 0, 2), ' '); + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[2J", out, &win, 0, 3), ' '); + /* clear: H then 2J reaches the rows below the first screen */ + AssertIntEQ(ConsoleCsiCellWin(h, "\x1b[H\x1b[2J", out, &win, 0, + wh + 3), ' '); + } + + CloseHandle(h); +#endif /* USE_WINDOWS_API */ +} + + static void test_wolfSSH_ConstantCompare(void) { const byte a[] = { 0x01, 0x02, 0x03, 0x04 }; @@ -11602,6 +11807,7 @@ int wolfSSH_ApiTest(int argc, char** argv) test_wolfSSH_SetUsername(); test_wolfSSH_SetChannelType(); test_wolfSSH_ConvertConsole(); + test_wolfSSH_ConvertConsole_csi(); test_wolfSSH_DoModes(); test_wolfSSH_CTX_UsePrivateKey_buffer(); test_wolfSSH_CTX_UseCert_buffer(); From cc7d9a4feed2e567e1e3876d80a8846bccebe2d3 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 2 Dec 2026 10:39:10 -0800 Subject: [PATCH 4/4] wolfterm: map SGR 38 and 48 colours doDisplayAttributes() takes the 5;N or 2;R;G;B after SGR 38 or 48 as one colour and sets the nearest console colour, rather than reading each sub-parameter as an SGR code of its own. - Indexes 0-15 map directly; the 6x6x6 cube, the grey ramp and RGB go to the nearest of the 16 console colours - An incomplete colour is skipped and leaves the attributes alone - api-test checks indexed, RGB, background and incomplete colours --- src/wolfterm.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/api.c | 14 ++++++ 2 files changed, 129 insertions(+) diff --git a/src/wolfterm.c b/src/wolfterm.c index bc5e11f4f..5e4be6e6f 100644 --- a/src/wolfterm.c +++ b/src/wolfterm.c @@ -132,6 +132,99 @@ static void wolfSSH_ClearScreen(WOLFSSH_HANDLE handle, word32 x1, word32 y1, wor } +/* console foreground bits for xterm colour index 0-15 */ +static word32 sgrIndexColor(word32 n) +{ + word32 c = 0; + + if (n & 1) + c |= FOREGROUND_RED; + if (n & 2) + c |= FOREGROUND_GREEN; + if (n & 4) + c |= FOREGROUND_BLUE; + if (n & 8) + c |= FOREGROUND_INTENSITY; + return c; +} + + +/* nearest console foreground bits for an RGB colour, components 0-255 */ +static word32 sgrRgbColor(word32 r, word32 g, word32 b) +{ + word32 m = r; + word32 c = 0; + + if (g > m) + m = g; + if (b > m) + m = b; + if (m < 48) + return 0; + if (r * 2 > m) + c |= FOREGROUND_RED; + if (g * 2 > m) + c |= FOREGROUND_GREEN; + if (b * 2 > m) + c |= FOREGROUND_BLUE; + if (m > 230) + c |= FOREGROUND_INTENSITY; + return c; +} + + +/* Parse the 5;N or 2;R;G;B after the SGR 38 or 48 at args[*i], leaving *i + * on the last argument used. Returns 1 with the console foreground bits in + * color when the colour is complete. */ +static int sgrExtColor(const word32* args, word32 numArgs, word32* i, + word32* color) +{ + static const byte cube[6] = { 0, 95, 135, 175, 215, 255 }; + word32 n = *i + 1; + word32 v; + + if (n >= numArgs) + return 0; + *i = n; + + if (args[n] == 5) { + if (n + 1 >= numArgs) + return 0; + *i = n + 1; + v = args[n + 1]; + if (v < 16) { + *color = sgrIndexColor(v); + } + else if (v < 232) { + v -= 16; + *color = sgrRgbColor(cube[v / 36], cube[(v / 6) % 6], + cube[v % 6]); + } + else if (v < 256) { + v = 8 + 10 * (v - 232); + *color = sgrRgbColor(v, v, v); + } + else { + return 0; + } + return 1; + } + + if (args[n] == 2) { + if (n + 3 >= numArgs) { + *i = numArgs - 1; + return 0; + } + *i = n + 3; + *color = sgrRgbColor(min(args[n + 1], 255), min(args[n + 2], 255), + min(args[n + 3], 255)); + return 1; + } + + return 0; +} + + static void doDisplayAttributes(WOLFSSH* ssh, WOLFSSH_HANDLE handle, word32* args, word32 numArgs) { word32 i = 0; @@ -219,6 +312,17 @@ static void doDisplayAttributes(WOLFSSH* ssh, WOLFSSH_HANDLE handle, word32* arg atr |= WS_MASK_RBGFG; break; + case 38: /* extended foreground, 5;N or 2;R;G;B */ + { + word32 color = 0; + + if (sgrExtColor(args, numArgs, &i, &color)) { + atr &= ~WS_MASK_RBGFG; + atr |= color; + } + break; + } + case 39: /* default foreground, bold kept */ atr &= ~WS_MASK_RBGFG; atr |= (ssh->defaultAttr & WS_MASK_RBGFG); @@ -262,6 +366,17 @@ static void doDisplayAttributes(WOLFSSH* ssh, WOLFSSH_HANDLE handle, word32* arg atr |= WS_MASK_RBGBG; break; + case 48: /* extended background, 5;N or 2;R;G;B */ + { + word32 color = 0; + + if (sgrExtColor(args, numArgs, &i, &color)) { + atr &= ~WS_MASK_RBGBG; + atr |= color << 4; + } + break; + } + case 49: /* default background */ atr &= ~WS_MASK_BG; atr |= (ssh->defaultAttr & WS_MASK_BG); diff --git a/tests/api.c b/tests/api.c index 3f9459a45..b623318f1 100644 --- a/tests/api.c +++ b/tests/api.c @@ -9870,6 +9870,20 @@ static void test_wolfSSH_ConvertConsole_csi(void) AssertIntEQ(ConsoleSgr(h, def, "\x1b[31;39m"), def); AssertIntEQ(ConsoleSgr(h, def, "\x1b[1;31;39m"), def | FOREGROUND_INTENSITY); + /* 38 and 48 take their sub-parameters as a colour */ + AssertIntEQ(ConsoleSgr(h, def, "\x1b[38;5;1m"), red); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[38;5;196m"), + red | FOREGROUND_INTENSITY); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[1;38;5;2m"), + FOREGROUND_GREEN | FOREGROUND_INTENSITY); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[38;5;4;1m"), + FOREGROUND_BLUE | FOREGROUND_INTENSITY); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[32;38;2;0;0;0m"), 0); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[38;2;205;0;0m"), red); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[48;5;4m"), def | BACKGROUND_BLUE); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[48;5;244m"), + def | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); + AssertIntEQ(ConsoleSgr(h, def, "\x1b[31;38;5m"), red); /* window scrolled down two rows: erase display clears the rows above * it too, as CSI H can move the cursor there */