diff --git a/contrib/win32/win32compat/console.c b/contrib/win32/win32compat/console.c index 44a99020b..c1b18ff2a 100644 --- a/contrib/win32/win32compat/console.c +++ b/contrib/win32/win32compat/console.c @@ -74,6 +74,7 @@ typedef struct _SCREEN_RECORD{ }SCREEN_RECORD,*PSCREEN_RECORD; PSCREEN_RECORD pSavedScreenRec = NULL; +int in_raw_mode = 0; /* ************************************************************ */ /* Function: ConInit */ @@ -135,6 +136,7 @@ int ConInit( DWORD OutputHandle, BOOL fSmartInit ) if (GetConsoleScreenBufferInfo(hOutputConsole, &csbi)) SavedViewRect = csbi.srWindow; + in_raw_mode = 1; return 0; } @@ -147,6 +149,7 @@ int ConUnInit( void ) { CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo; + in_raw_mode = 0; if ( hOutputConsole == NULL ) return 0; diff --git a/contrib/win32/win32compat/shell-host.c b/contrib/win32/win32compat/shell-host.c index 06bde1ece..1f2322b22 100644 --- a/contrib/win32/win32compat/shell-host.c +++ b/contrib/win32/win32compat/shell-host.c @@ -1069,15 +1069,15 @@ int start_with_pty(int ac, wchar_t **av) { /*TODO - pick this up from system32*/ cmd[0] = L'\0'; if (ac) - GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, PATH_MAX, L"cmd.exe")); + GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, L"cmd.exe")); ac--; av++; if (ac) - GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, PATH_MAX, L" /c")); + GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, L" /c")); while (ac) { - GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, PATH_MAX, L" ")); - GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, PATH_MAX, *av)); + GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, L" ")); + GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, *av)); ac--; av++; } @@ -1183,14 +1183,14 @@ int start_withno_pty(int ac, wchar_t **av) { /*TODO - pick this up from system32*/ cmd[0] = L'\0'; - GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, PATH_MAX, L"cmd.exe")); + GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, L"cmd.exe")); ac -= 2; av += 2; if (ac) - GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, PATH_MAX, L" /c")); + GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, L" /c")); while (ac) { - GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, PATH_MAX, L" ")); - GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, PATH_MAX, *av)); + GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, L" ")); + GOTO_CLEANUP_ON_ERR(wcscat_s(cmd, MAX_CMD_LEN, *av)); ac--; av++; } diff --git a/contrib/win32/win32compat/termio.c b/contrib/win32/win32compat/termio.c index 4ae3be65e..bc61f820d 100644 --- a/contrib/win32/win32compat/termio.c +++ b/contrib/win32/win32compat/termio.c @@ -6,6 +6,8 @@ #define TERM_IO_BUF_SIZE 2048 +extern int in_raw_mode; + struct io_status { DWORD to_transfer; DWORD transferred; @@ -129,25 +131,19 @@ static DWORD WINAPI WriteThread( DWORD dwSavedAttributes = ENABLE_PROCESSED_INPUT; debug3("TermWrite thread, io:%p", pio); - /* decide to call parsing engine or directly write to console - * doing the following trick to decide - - * if console in handle is set to process Ctrl+C, then it is likely - * serving a PTY enabled session - */ - GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwSavedAttributes); - if (dwSavedAttributes & ENABLE_PROCESSED_INPUT) { + if (in_raw_mode == 0) { /* convert stream to utf16 and dump on console */ pio->write_details.buf[write_status.to_transfer] = '\0'; wchar_t* t = utf8_to_utf16(pio->write_details.buf); WriteConsoleW(WINHANDLE(pio), t, wcslen(t), 0, 0); free(t); write_status.transferred = write_status.to_transfer; - } else { - - telProcessNetwork(pio->write_details.buf, write_status.to_transfer, &respbuf, &resplen); - /*TODO - respbuf is not null in some cases, this needs to be returned back via read stream*/ - write_status.transferred = write_status.to_transfer; - } + } else { + /* console mode */ + telProcessNetwork(pio->write_details.buf, write_status.to_transfer, &respbuf, &resplen); + /*TODO - respbuf is not null in some cases, this needs to be returned back via read stream*/ + write_status.transferred = write_status.to_transfer; + } if (0 == QueueUserAPC(WriteAPCProc, main_thread, (ULONG_PTR)pio)) { debug("TermWrite thread - ERROR QueueUserAPC failed %d, io:%p", GetLastError(), pio); diff --git a/sftp.c b/sftp.c index d1f3ffa47..d7d8cd66a 100644 --- a/sftp.c +++ b/sftp.c @@ -296,19 +296,16 @@ help(void) /* printf version to account for utf-8 input */ /* TODO - merge this with vfmprint */ static void printf_utf8(char *fmt, ... ) { - /* TODO - is 1024 sufficient */ - char buf[1024]; - wchar_t* wtmp; - va_list valist; - va_start(valist, fmt); + /* TODO - is 1024 sufficient */ + char buf[1024]; + int length = 0; + + va_list valist; + va_start(valist, fmt); + length = vsnprintf(buf, 1024, fmt, valist); + va_end(valist); - vsnprintf(buf, 1024, fmt, valist); - va_end(valist); - - if ((wtmp = utf8_to_utf16(buf)) == NULL) - fatal("unable to allocate memory"); - WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), wtmp, wcslen(wtmp), 0, 0); - free(wtmp); + write(STDOUT_FILENO, buf, length); } /* override mprintf */ @@ -931,11 +928,17 @@ do_ls_dir(struct sftp_conn *conn, const char *path, #ifdef WINDOWS /* cannot use printf_utf8 becuase of width specification */ /* printf_utf8 does not account for utf-16 based argument widths */ + char *p = NULL; wchar_t buf[1024]; wchar_t* wtmp = utf8_to_utf16(fname); swprintf(buf, 1024, L"%-*s", colspace, wtmp); - WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), buf, wcslen(buf), 0, 0); + + if ((p = utf16_to_utf8(buf)) == NULL) + continue; + + write(STDOUT_FILENO, p, strlen(p)); free(wtmp); + free(p); #else mprintf("%-*s", colspace, fname); #endif @@ -1025,11 +1028,17 @@ do_globbed_ls(struct sftp_conn *conn, const char *path, #ifdef WINDOWS /* cannot use printf_utf8 becuase of width specification */ /* printf_utf8 does not account for utf-16 based argument widths */ + char *p = NULL; wchar_t buf[1024]; wchar_t* wtmp = utf8_to_utf16(fname); swprintf(buf, 1024, L"%-*s", colspace, wtmp); - WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), buf, wcslen(buf), 0, 0); + + if ((p = utf16_to_utf8(buf)) == NULL) + continue; + + write(STDOUT_FILENO, p, strlen(p)); free(wtmp); + free(p); #else mprintf("%-*s", colspace, fname); #endif