1 // Copyright (c) 2015 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "../include/winpty.h"
31 #include "../shared/DebugClient.h"
33 static std::vector<unsigned char> filterContent(
34 const std::vector<unsigned char> &content) {
35 std::vector<unsigned char> result;
36 auto it = content.begin();
37 const auto itEnd = content.end();
40 // Filter out carriage returns. Sometimes the output starts with
41 // a single CR; other times, it has multiple CRs.
43 } else if (*it == '\x1b' && (it + 1) < itEnd && *(it + 1) == '[') {
44 // Filter out escape sequences. They have no interior letters and
45 // end with a single letter.
47 while (it < itEnd && !isalpha(*it)) {
52 // Let everything else through.
53 result.push_back(*it);
60 // Read bytes from the non-overlapped file handle until the file is closed or
61 // until an I/O error occurs.
62 static std::vector<unsigned char> readAll(HANDLE handle) {
63 unsigned char buf[1024];
64 std::vector<unsigned char> result;
67 BOOL ret = ReadFile(handle, buf, sizeof(buf), &amount, nullptr);
68 if (!ret || amount == 0) {
71 result.insert(result.end(), buf, buf + amount);
76 static void parentTest() {
77 wchar_t program[1024];
78 wchar_t cmdline[1024];
79 GetModuleFileNameW(nullptr, program, 1024);
82 // XXX: We'd like to use swprintf, which is part of C99 and takes a
83 // size_t maxlen argument. MinGW-w64 has this function, as does MSVC.
84 // The old MinGW doesn't, though -- instead, it apparently provides an
85 // swprintf taking no maxlen argument. This *might* be a regression?
86 // (There is also no swnprintf, but that function is obsolescent with a
87 // correct swprintf, and it isn't in POSIX or ISO C.)
89 // Visual C++ 6 also provided this non-conformant swprintf, and I'm
90 // guessing MSVCRT.DLL does too. (My impression is that the old MinGW
91 // prefers to rely on MSVCRT.DLL for convenience?)
93 // I could compile differently for old MinGW, but what if it fixes its
94 // function later? Instead, use a workaround. It's starting to make
95 // sense to drop MinGW support in favor of MinGW-w64. This is too
98 // grepbait: OLD-MINGW / WINPTY_TARGET_MSYS1
100 wcscat(cmdline, L"\"");
101 wcscat(cmdline, program);
102 wcscat(cmdline, L"\" CHILD");
104 // swnprintf(cmdline, sizeof(cmdline) / sizeof(cmdline[0]),
105 // L"\"%ls\" CHILD", program);
107 auto agentCfg = winpty_config_new(0, nullptr);
108 assert(agentCfg != nullptr);
109 auto pty = winpty_open(agentCfg, nullptr);
110 assert(pty != nullptr);
111 winpty_config_free(agentCfg);
113 HANDLE conin = CreateFileW(
114 winpty_conin_name(pty),
115 GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
116 HANDLE conout = CreateFileW(
117 winpty_conout_name(pty),
118 GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
119 assert(conin != INVALID_HANDLE_VALUE);
120 assert(conout != INVALID_HANDLE_VALUE);
122 auto spawnCfg = winpty_spawn_config_new(
123 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN, program, cmdline,
124 nullptr, nullptr, nullptr);
125 assert(spawnCfg != nullptr);
126 HANDLE process = nullptr;
127 BOOL spawnSuccess = winpty_spawn(
128 pty, spawnCfg, &process, nullptr, nullptr, nullptr);
129 assert(spawnSuccess && process != nullptr);
131 auto content = readAll(conout);
132 content = filterContent(content);
134 std::vector<unsigned char> expectedContent = {
135 'H', 'I', '\n', 'X', 'Y', '\n'
138 assert(GetExitCodeProcess(process, &exitCode) && exitCode == 42);
139 CloseHandle(process);
142 assert(content == expectedContent);
146 static void childTest() {
151 int main(int argc, char *argv[]) {