From e137865ab2acf914cadd3447df2d11ea8e41e7a1 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Wed, 6 Mar 2024 09:01:48 -0700 Subject: [PATCH 1/3] use windows terminal parsing for VT --- examples/client/client.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index 51f8d2361..8d683a8b5 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -367,6 +367,20 @@ static THREAD_RET readPeer(void* in) FD_SET(fd, &errSet); #ifdef USE_WINDOWS_API + if (args->rawMode == 0) { + DWORD wrd; + + if (GetConsoleMode(stdoutHandle, &wrd) == FALSE) { + err_sys("Unable to get stdout handle"); + } + + /* depend on the terminal to process VT characters */ + wrd |= (ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT); + if (SetConsoleMode(stdoutHandle, wrd) == FALSE) { + err_sys("Unable to set console mode"); + } + } + /* set handle to use for window resize */ wc_LockMutex(&args->lock); wolfSSH_SetTerminalResizeCtx(args->ssh, stdoutHandle); @@ -446,29 +460,21 @@ static THREAD_RET readPeer(void* in) } } else { + #ifdef USE_WINDOWS_API + DWORD writtn = 0; + #endif buf[bufSz - 1] = '\0'; #ifdef USE_WINDOWS_API - if (args->rawMode == 0) { - ret = wolfSSH_ConvertConsole(args->ssh, stdoutHandle, buf, - ret); - if (ret != WS_SUCCESS && ret != WS_WANT_READ) { - err_sys("issue with print out"); - } - if (ret == WS_WANT_READ) { - ret = 0; - } - } - else { - printf("%s", buf); - WFFLUSH(stdout); + if (WriteFile(stdoutHandle, buf, bufSz, &writtn, NULL) == FALSE) { + err_sys("Failed to write to stdout handle"); } #else if (write(STDOUT_FILENO, buf, ret) < 0) { perror("write to stdout error "); } - WFFLUSH(stdout); #endif + WFFLUSH(stdout); } if (wolfSSH_stream_peek(args->ssh, buf, bufSz) <= 0) { bytes = 0; /* read it all */ From 9c7edce64419ccca1b63d1d15232149c139237a3 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Wed, 6 Mar 2024 09:06:28 -0700 Subject: [PATCH 2/3] use windows terminal parsing for VT wolfssh.c --- apps/wolfssh/wolfssh.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/apps/wolfssh/wolfssh.c b/apps/wolfssh/wolfssh.c index 5e216cbe7..6a45ca6d5 100644 --- a/apps/wolfssh/wolfssh.c +++ b/apps/wolfssh/wolfssh.c @@ -404,6 +404,20 @@ static THREAD_RET readPeer(void* in) FD_SET(fd, &errSet); #ifdef USE_WINDOWS_API + if (args->rawMode == 0) { + DWORD wrd; + + if (GetConsoleMode(stdoutHandle, &wrd) == FALSE) { + err_sys("Unable to get stdout handle"); + } + + /* depend on the terminal to process VT characters */ + wrd |= (ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT); + if (SetConsoleMode(stdoutHandle, wrd) == FALSE) { + err_sys("Unable to set console mode"); + } + } + /* set handle to use for window resize */ wc_LockMutex(&args->lock); wolfSSH_SetTerminalResizeCtx(args->ssh, stdoutHandle); @@ -482,22 +496,14 @@ static THREAD_RET readPeer(void* in) } } else { + #ifdef USE_WINDOWS_API + DWORD writtn = 0; + #endif buf[bufSz - 1] = '\0'; #ifdef USE_WINDOWS_API - if (args->rawMode == 0) { - ret = wolfSSH_ConvertConsole(args->ssh, stdoutHandle, buf, - ret); - if (ret != WS_SUCCESS && ret != WS_WANT_READ) { - err_sys("issue with print out"); - } - if (ret == WS_WANT_READ) { - ret = 0; - } - } - else { - printf("%s", buf); - fflush(stdout); + if (WriteFile(stdoutHandle, buf, bufSz, &writtn, NULL) == FALSE) { + err_sys("Failed to write to stdout handle"); } #else if (write(STDOUT_FILENO, buf, ret) < 0) { From 5d3f8776ed518cc1697d1c13fe46efd9afe73f97 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Wed, 6 Mar 2024 09:49:42 -0700 Subject: [PATCH 3/3] add macro guard on windows version for VT --- apps/wolfssh/wolfssh.c | 8 +++++++- examples/client/client.c | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/apps/wolfssh/wolfssh.c b/apps/wolfssh/wolfssh.c index 6a45ca6d5..d3b601786 100644 --- a/apps/wolfssh/wolfssh.c +++ b/apps/wolfssh/wolfssh.c @@ -412,7 +412,13 @@ static THREAD_RET readPeer(void* in) } /* depend on the terminal to process VT characters */ - wrd |= (ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT); + #ifndef _WIN32_WINNT_WIN10 + /* support for virtual terminal processing was introduced in windows 10 */ + #define _WIN32_WINNT_WIN10 0x0A00 + #endif + #if defined(WINVER) && (WINVER >= _WIN32_WINNT_WIN10) + wrd |= (ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT); + #endif if (SetConsoleMode(stdoutHandle, wrd) == FALSE) { err_sys("Unable to set console mode"); } diff --git a/examples/client/client.c b/examples/client/client.c index 8d683a8b5..974c321f7 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -375,7 +375,13 @@ static THREAD_RET readPeer(void* in) } /* depend on the terminal to process VT characters */ - wrd |= (ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT); + #ifndef _WIN32_WINNT_WIN10 + /* support for virtual terminal processing was introduced in windows 10 */ + #define _WIN32_WINNT_WIN10 0x0A00 + #endif + #if defined(WINVER) && (WINVER >= _WIN32_WINNT_WIN10) + wrd |= (ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT); + #endif if (SetConsoleMode(stdoutHandle, wrd) == FALSE) { err_sys("Unable to set console mode"); }