winpty/misc/TestUtil.cc

159 lines
4.7 KiB
C++
Raw Normal View History

2015-08-23 03:56:24 +00:00
// This file is included into test programs using #include
#include <windows.h>
2015-10-10 08:30:58 +00:00
#include <assert.h>
2015-08-23 03:56:24 +00:00
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <vector>
#include <string>
2015-08-23 03:56:24 +00:00
#include "../shared/DebugClient.h"
// Launch this test program again, in a new console that we will destroy.
static void startChildProcess(const wchar_t *args) {
2015-08-23 03:56:24 +00:00
wchar_t program[1024];
wchar_t cmdline[1024];
GetModuleFileNameW(NULL, program, 1024);
swprintf(cmdline, L"\"%ls\" %ls", program, args);
2015-08-23 03:56:24 +00:00
STARTUPINFOW sui;
PROCESS_INFORMATION pi;
memset(&sui, 0, sizeof(sui));
memset(&pi, 0, sizeof(pi));
sui.cb = sizeof(sui);
CreateProcessW(program, cmdline,
NULL, NULL,
/*bInheritHandles=*/FALSE,
/*dwCreationFlags=*/CREATE_NEW_CONSOLE,
NULL, NULL,
&sui, &pi);
}
Rewrite resize logic, introduce "direct mode", and tweak scroll scraping. * Unfreeze the console while changing the buffer size. Changing the buffer size hangs conhost.exe. See: - https://github.com/rprichard/winpty/issues/31 - https://wpdev.uservoice.com/forums/266908-command-prompt/suggestions/9941292-conhost-exe-hangs-in-win10-if-setconsolescreenbuff * Detect buffer size changes and switch to a "direct mode". Direct mode makes no attempt to track incremental console changes. Instead, the content of the current console window is printed. This mode is intended for full-screen apps that resize the console. * Reopen CONOUT$, which detects apps that change the active screen buffer. Fixes https://github.com/rprichard/winpty/issues/34. * In the scroll scraping (scrollingScrapeOutput), consider a line changed if the new content is truncated relative to the content previously output. Previously, we only compared against the line-buffer up to the current console width. e.g. If this: |C:\Program| turns into: |C:\Prog| |ram | we previously left |C:\Program| in the line-buffer for the first line and did not re-output the first line. We *should* reoutput the first line at this point so that, if the line scrolls upward, and the terminal is later expanded, we will have output an "Erase in Line" CSI command to clear the obscured "ram" text. We need to update the line-buffer for the sake of Windows 10 combined with terminals like xterm and putty. On such a terminal, if the terminal later widened, Windows 10 will restore the console to the first state. At that point, we need to reoutput the line, because xterm and putty do not save and restore truncated line content extending past the current terminal width.
2015-09-29 07:32:49 +00:00
static void setBufferSize(HANDLE conout, int x, int y) {
COORD size = { static_cast<SHORT>(x), static_cast<SHORT>(y) };
2015-08-23 03:56:24 +00:00
BOOL success = SetConsoleScreenBufferSize(conout, size);
trace("setBufferSize: (%d,%d), result=%d", x, y, success);
}
Rewrite resize logic, introduce "direct mode", and tweak scroll scraping. * Unfreeze the console while changing the buffer size. Changing the buffer size hangs conhost.exe. See: - https://github.com/rprichard/winpty/issues/31 - https://wpdev.uservoice.com/forums/266908-command-prompt/suggestions/9941292-conhost-exe-hangs-in-win10-if-setconsolescreenbuff * Detect buffer size changes and switch to a "direct mode". Direct mode makes no attempt to track incremental console changes. Instead, the content of the current console window is printed. This mode is intended for full-screen apps that resize the console. * Reopen CONOUT$, which detects apps that change the active screen buffer. Fixes https://github.com/rprichard/winpty/issues/34. * In the scroll scraping (scrollingScrapeOutput), consider a line changed if the new content is truncated relative to the content previously output. Previously, we only compared against the line-buffer up to the current console width. e.g. If this: |C:\Program| turns into: |C:\Prog| |ram | we previously left |C:\Program| in the line-buffer for the first line and did not re-output the first line. We *should* reoutput the first line at this point so that, if the line scrolls upward, and the terminal is later expanded, we will have output an "Erase in Line" CSI command to clear the obscured "ram" text. We need to update the line-buffer for the sake of Windows 10 combined with terminals like xterm and putty. On such a terminal, if the terminal later widened, Windows 10 will restore the console to the first state. At that point, we need to reoutput the line, because xterm and putty do not save and restore truncated line content extending past the current terminal width.
2015-09-29 07:32:49 +00:00
static void setWindowPos(HANDLE conout, int x, int y, int w, int h) {
SMALL_RECT r = {
static_cast<SHORT>(x), static_cast<SHORT>(y),
static_cast<SHORT>(x + w - 1),
static_cast<SHORT>(y + h - 1)
};
2015-08-23 03:56:24 +00:00
BOOL success = SetConsoleWindowInfo(conout, /*bAbsolute=*/TRUE, &r);
trace("setWindowPos: (%d,%d,%d,%d), result=%d", x, y, w, h, success);
}
Rewrite resize logic, introduce "direct mode", and tweak scroll scraping. * Unfreeze the console while changing the buffer size. Changing the buffer size hangs conhost.exe. See: - https://github.com/rprichard/winpty/issues/31 - https://wpdev.uservoice.com/forums/266908-command-prompt/suggestions/9941292-conhost-exe-hangs-in-win10-if-setconsolescreenbuff * Detect buffer size changes and switch to a "direct mode". Direct mode makes no attempt to track incremental console changes. Instead, the content of the current console window is printed. This mode is intended for full-screen apps that resize the console. * Reopen CONOUT$, which detects apps that change the active screen buffer. Fixes https://github.com/rprichard/winpty/issues/34. * In the scroll scraping (scrollingScrapeOutput), consider a line changed if the new content is truncated relative to the content previously output. Previously, we only compared against the line-buffer up to the current console width. e.g. If this: |C:\Program| turns into: |C:\Prog| |ram | we previously left |C:\Program| in the line-buffer for the first line and did not re-output the first line. We *should* reoutput the first line at this point so that, if the line scrolls upward, and the terminal is later expanded, we will have output an "Erase in Line" CSI command to clear the obscured "ram" text. We need to update the line-buffer for the sake of Windows 10 combined with terminals like xterm and putty. On such a terminal, if the terminal later widened, Windows 10 will restore the console to the first state. At that point, we need to reoutput the line, because xterm and putty do not save and restore truncated line content extending past the current terminal width.
2015-09-29 07:32:49 +00:00
static void setCursorPos(HANDLE conout, int x, int y) {
COORD coord = { static_cast<SHORT>(x), static_cast<SHORT>(y) };
SetConsoleCursorPosition(conout, coord);
}
Rewrite resize logic, introduce "direct mode", and tweak scroll scraping. * Unfreeze the console while changing the buffer size. Changing the buffer size hangs conhost.exe. See: - https://github.com/rprichard/winpty/issues/31 - https://wpdev.uservoice.com/forums/266908-command-prompt/suggestions/9941292-conhost-exe-hangs-in-win10-if-setconsolescreenbuff * Detect buffer size changes and switch to a "direct mode". Direct mode makes no attempt to track incremental console changes. Instead, the content of the current console window is printed. This mode is intended for full-screen apps that resize the console. * Reopen CONOUT$, which detects apps that change the active screen buffer. Fixes https://github.com/rprichard/winpty/issues/34. * In the scroll scraping (scrollingScrapeOutput), consider a line changed if the new content is truncated relative to the content previously output. Previously, we only compared against the line-buffer up to the current console width. e.g. If this: |C:\Program| turns into: |C:\Prog| |ram | we previously left |C:\Program| in the line-buffer for the first line and did not re-output the first line. We *should* reoutput the first line at this point so that, if the line scrolls upward, and the terminal is later expanded, we will have output an "Erase in Line" CSI command to clear the obscured "ram" text. We need to update the line-buffer for the sake of Windows 10 combined with terminals like xterm and putty. On such a terminal, if the terminal later widened, Windows 10 will restore the console to the first state. At that point, we need to reoutput the line, because xterm and putty do not save and restore truncated line content extending past the current terminal width.
2015-09-29 07:32:49 +00:00
static void setBufferSize(int x, int y) {
setBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), x, y);
}
static void setWindowPos(int x, int y, int w, int h) {
setWindowPos(GetStdHandle(STD_OUTPUT_HANDLE), x, y, w, h);
}
static void setCursorPos(int x, int y) {
setCursorPos(GetStdHandle(STD_OUTPUT_HANDLE), x, y);
}
2015-08-23 03:56:24 +00:00
static void countDown(int sec) {
for (int i = sec; i > 0; --i) {
printf("%d.. ", i);
fflush(stdout);
Sleep(1000);
}
printf("\n");
}
Rewrite resize logic, introduce "direct mode", and tweak scroll scraping. * Unfreeze the console while changing the buffer size. Changing the buffer size hangs conhost.exe. See: - https://github.com/rprichard/winpty/issues/31 - https://wpdev.uservoice.com/forums/266908-command-prompt/suggestions/9941292-conhost-exe-hangs-in-win10-if-setconsolescreenbuff * Detect buffer size changes and switch to a "direct mode". Direct mode makes no attempt to track incremental console changes. Instead, the content of the current console window is printed. This mode is intended for full-screen apps that resize the console. * Reopen CONOUT$, which detects apps that change the active screen buffer. Fixes https://github.com/rprichard/winpty/issues/34. * In the scroll scraping (scrollingScrapeOutput), consider a line changed if the new content is truncated relative to the content previously output. Previously, we only compared against the line-buffer up to the current console width. e.g. If this: |C:\Program| turns into: |C:\Prog| |ram | we previously left |C:\Program| in the line-buffer for the first line and did not re-output the first line. We *should* reoutput the first line at this point so that, if the line scrolls upward, and the terminal is later expanded, we will have output an "Erase in Line" CSI command to clear the obscured "ram" text. We need to update the line-buffer for the sake of Windows 10 combined with terminals like xterm and putty. On such a terminal, if the terminal later widened, Windows 10 will restore the console to the first state. At that point, we need to reoutput the line, because xterm and putty do not save and restore truncated line content extending past the current terminal width.
2015-09-29 07:32:49 +00:00
static void writeBox(int x, int y, int w, int h, char ch, int attributes=7) {
2015-08-23 03:56:24 +00:00
CHAR_INFO info = { 0 };
info.Char.AsciiChar = ch;
info.Attributes = attributes;
std::vector<CHAR_INFO> buf(w * h, info);
HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD bufSize = { static_cast<SHORT>(w), static_cast<SHORT>(h) };
2015-08-23 03:56:24 +00:00
COORD bufCoord = { 0, 0 };
SMALL_RECT writeRegion = {
static_cast<SHORT>(x),
static_cast<SHORT>(y),
static_cast<SHORT>(x + w - 1),
static_cast<SHORT>(y + h - 1)
};
2015-08-23 03:56:24 +00:00
WriteConsoleOutputA(conout, buf.data(), bufSize, bufCoord, &writeRegion);
}
static void setChar(int x, int y, char ch, int attributes=7) {
Rewrite resize logic, introduce "direct mode", and tweak scroll scraping. * Unfreeze the console while changing the buffer size. Changing the buffer size hangs conhost.exe. See: - https://github.com/rprichard/winpty/issues/31 - https://wpdev.uservoice.com/forums/266908-command-prompt/suggestions/9941292-conhost-exe-hangs-in-win10-if-setconsolescreenbuff * Detect buffer size changes and switch to a "direct mode". Direct mode makes no attempt to track incremental console changes. Instead, the content of the current console window is printed. This mode is intended for full-screen apps that resize the console. * Reopen CONOUT$, which detects apps that change the active screen buffer. Fixes https://github.com/rprichard/winpty/issues/34. * In the scroll scraping (scrollingScrapeOutput), consider a line changed if the new content is truncated relative to the content previously output. Previously, we only compared against the line-buffer up to the current console width. e.g. If this: |C:\Program| turns into: |C:\Prog| |ram | we previously left |C:\Program| in the line-buffer for the first line and did not re-output the first line. We *should* reoutput the first line at this point so that, if the line scrolls upward, and the terminal is later expanded, we will have output an "Erase in Line" CSI command to clear the obscured "ram" text. We need to update the line-buffer for the sake of Windows 10 combined with terminals like xterm and putty. On such a terminal, if the terminal later widened, Windows 10 will restore the console to the first state. At that point, we need to reoutput the line, because xterm and putty do not save and restore truncated line content extending past the current terminal width.
2015-09-29 07:32:49 +00:00
writeBox(x, y, 1, 1, ch, attributes);
}
static void fillChar(int x, int y, int repeat, char ch) {
COORD coord = { static_cast<SHORT>(x), static_cast<SHORT>(y) };
Rewrite resize logic, introduce "direct mode", and tweak scroll scraping. * Unfreeze the console while changing the buffer size. Changing the buffer size hangs conhost.exe. See: - https://github.com/rprichard/winpty/issues/31 - https://wpdev.uservoice.com/forums/266908-command-prompt/suggestions/9941292-conhost-exe-hangs-in-win10-if-setconsolescreenbuff * Detect buffer size changes and switch to a "direct mode". Direct mode makes no attempt to track incremental console changes. Instead, the content of the current console window is printed. This mode is intended for full-screen apps that resize the console. * Reopen CONOUT$, which detects apps that change the active screen buffer. Fixes https://github.com/rprichard/winpty/issues/34. * In the scroll scraping (scrollingScrapeOutput), consider a line changed if the new content is truncated relative to the content previously output. Previously, we only compared against the line-buffer up to the current console width. e.g. If this: |C:\Program| turns into: |C:\Prog| |ram | we previously left |C:\Program| in the line-buffer for the first line and did not re-output the first line. We *should* reoutput the first line at this point so that, if the line scrolls upward, and the terminal is later expanded, we will have output an "Erase in Line" CSI command to clear the obscured "ram" text. We need to update the line-buffer for the sake of Windows 10 combined with terminals like xterm and putty. On such a terminal, if the terminal later widened, Windows 10 will restore the console to the first state. At that point, we need to reoutput the line, because xterm and putty do not save and restore truncated line content extending past the current terminal width.
2015-09-29 07:32:49 +00:00
DWORD actual = 0;
FillConsoleOutputCharacterA(
GetStdHandle(STD_OUTPUT_HANDLE),
ch, repeat, coord, &actual);
2015-08-23 03:56:24 +00:00
}
static void repeatChar(int count, char ch) {
for (int i = 0; i < count; ++i) {
putchar(ch);
}
fflush(stdout);
}
// I don't know why, but wprintf fails to print this face name,
// " ゴシック" (aka MS Gothic). It helps to use wprintf instead of printf, and
// it helps to call `setlocale(LC_ALL, "")`, but the Japanese symbols are
// ultimately converted to `?` symbols, even though MS Gothic is able to
// display its own name, and the current code page is 932 (Shift-JIS).
2015-10-20 04:32:08 +00:00
static void cvfprintf(HANDLE conout, const wchar_t *fmt, va_list ap) {
wchar_t buffer[256];
vswprintf(buffer, 256 - 1, fmt, ap);
buffer[255] = L'\0';
DWORD actual = 0;
2015-10-20 04:32:08 +00:00
if (!WriteConsoleW(conout, buffer, wcslen(buffer), &actual, NULL)) {
wprintf(L"WriteConsoleW call failed!\n");
}
}
2015-10-20 04:32:08 +00:00
static void cfprintf(HANDLE conout, const wchar_t *fmt, ...) {
va_list ap;
va_start(ap, fmt);
cvfprintf(conout, fmt, ap);
va_end(ap);
}
static void cprintf(const wchar_t *fmt, ...) {
va_list ap;
va_start(ap, fmt);
2015-10-20 04:32:08 +00:00
cvfprintf(GetStdHandle(STD_OUTPUT_HANDLE), fmt, ap);
va_end(ap);
}
2015-10-20 04:32:08 +00:00
static std::string narrowString(const std::wstring &input)
{
int mblen = WideCharToMultiByte(
CP_UTF8, 0,
input.data(), input.size(),
NULL, 0, NULL, NULL);
if (mblen <= 0) {
return std::string();
}
std::vector<char> tmp(mblen);
int mblen2 = WideCharToMultiByte(
CP_UTF8, 0,
input.data(), input.size(),
tmp.data(), tmp.size(),
NULL, NULL);
assert(mblen2 == mblen);
return std::string(tmp.data(), tmp.size());
}