Repository navigation
Expand file tree
/
Copy pathShellCheckRunner.cpp
More file actions
191 lines (162 loc) · 6.5 KB
/
Copy pathShellCheckRunner.cpp
File metadata and controls
191 lines (162 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "ShellCheckRunner.h"
#include <shlwapi.h>
#include <thread>
#include <utility>
ShellCheckRunner::ShellCheckRunner() = default;
ShellCheckRunner::~ShellCheckRunner() = default;
static bool fileExists(const std::wstring& path) {
if (path.empty()) return false;
DWORD attr = ::GetFileAttributesW(path.c_str());
return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY);
}
std::wstring ShellCheckRunner::resolveExecutablePath(const std::wstring& configuredPath,
const std::wstring& pluginDir) const {
if (fileExists(configuredPath)) {
return configuredPath;
}
if (!pluginDir.empty()) {
std::wstring localExe = pluginDir;
if (localExe.back() != L'\\' && localExe.back() != L'/') {
localExe.push_back(L'\\');
}
localExe += L"shellcheck.exe";
if (fileExists(localExe)) {
return localExe;
}
}
wchar_t foundPath[MAX_PATH] = {0};
if (::SearchPathW(nullptr, L"shellcheck.exe", nullptr, MAX_PATH, foundPath, nullptr) > 0) {
return std::wstring(foundPath);
}
return std::wstring();
}
static bool executeShellCheckSync(const std::wstring& exePath,
const std::string& scriptUtf8,
const std::wstring& shellDialect,
std::vector<ShellCheckIssue>& outIssues,
std::wstring& outError) {
SECURITY_ATTRIBUTES sa{};
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = nullptr;
HANDLE hStdinRead = nullptr, hStdinWrite = nullptr;
HANDLE hStdoutRead = nullptr, hStdoutWrite = nullptr;
HANDLE hStderrRead = nullptr, hStderrWrite = nullptr;
if (!::CreatePipe(&hStdinRead, &hStdinWrite, &sa, 0) ||
!::CreatePipe(&hStdoutRead, &hStdoutWrite, &sa, 0) ||
!::CreatePipe(&hStderrRead, &hStderrWrite, &sa, 0)) {
outError = L"Falha ao criar pipes para executar o shellcheck.exe.";
if (hStdinRead) ::CloseHandle(hStdinRead);
if (hStdinWrite) ::CloseHandle(hStdinWrite);
if (hStdoutRead) ::CloseHandle(hStdoutRead);
if (hStdoutWrite) ::CloseHandle(hStdoutWrite);
if (hStderrRead) ::CloseHandle(hStderrRead);
if (hStderrWrite) ::CloseHandle(hStderrWrite);
return false;
}
::SetHandleInformation(hStdinWrite, HANDLE_FLAG_INHERIT, 0);
::SetHandleInformation(hStdoutRead, HANDLE_FLAG_INHERIT, 0);
::SetHandleInformation(hStderrRead, HANDLE_FLAG_INHERIT, 0);
std::wstring cmdLine = L"\"" + exePath + L"\" -f json1";
if (!shellDialect.empty() && shellDialect != L"auto") {
cmdLine += L" --shell=" + shellDialect;
}
cmdLine += L" -";
STARTUPINFOW si{};
si.cb = sizeof(STARTUPINFOW);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdInput = hStdinRead;
si.hStdOutput = hStdoutWrite;
si.hStdError = hStderrWrite;
PROCESS_INFORMATION pi{};
std::vector<wchar_t> mutableCmd(cmdLine.begin(), cmdLine.end());
mutableCmd.push_back(L'\0');
BOOL created = ::CreateProcessW(
nullptr,
mutableCmd.data(),
nullptr,
nullptr,
TRUE,
CREATE_NO_WINDOW,
nullptr,
nullptr,
&si,
&pi
);
::CloseHandle(hStdinRead);
::CloseHandle(hStdoutWrite);
::CloseHandle(hStderrWrite);
if (!created) {
::CloseHandle(hStdinWrite);
::CloseHandle(hStdoutRead);
::CloseHandle(hStderrRead);
outError = L"Não foi possível iniciar o shellcheck.exe.";
return false;
}
// Write stdin on a separate thread to prevent pipe buffer deadlocks on large scripts
std::thread writerThread([hStdinWrite, &scriptUtf8]() {
size_t offset = 0;
while (offset < scriptUtf8.size()) {
DWORD chunk = static_cast<DWORD>(
(scriptUtf8.size() - offset > 65536) ? 65536 : (scriptUtf8.size() - offset));
DWORD written = 0;
if (!::WriteFile(hStdinWrite, scriptUtf8.data() + offset, chunk, &written, nullptr) || written == 0) {
break;
}
offset += written;
}
::CloseHandle(hStdinWrite);
});
std::string stdoutData;
std::string stderrData;
char buffer[4096];
DWORD bytesRead = 0;
while (::ReadFile(hStdoutRead, buffer, sizeof(buffer), &bytesRead, nullptr) && bytesRead > 0) {
stdoutData.append(buffer, bytesRead);
}
while (::ReadFile(hStderrRead, buffer, sizeof(buffer), &bytesRead, nullptr) && bytesRead > 0) {
stderrData.append(buffer, bytesRead);
}
writerThread.join();
::WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitCode = 0;
::GetExitCodeProcess(pi.hProcess, &exitCode);
::CloseHandle(hStdoutRead);
::CloseHandle(hStderrRead);
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
// ShellCheck exit codes: 0 = no issues, 1 = issues found, 2+ = fatal error
if (stdoutData.empty() && exitCode >= 2) {
int len = ::MultiByteToWideChar(CP_UTF8, 0, stderrData.data(), static_cast<int>(stderrData.size()), nullptr, 0);
if (len > 0) {
outError.resize(len);
::MultiByteToWideChar(CP_UTF8, 0, stderrData.data(), static_cast<int>(stderrData.size()), &outError[0], len);
} else {
outError = L"ShellCheck retornou erro de execução.";
}
return false;
}
std::string parseErr;
if (!parseShellCheckJson1(stdoutData, outIssues, parseErr)) {
outError = L"Falha ao interpretar saída JSON do ShellCheck.";
return false;
}
return true;
}
uint64_t ShellCheckRunner::runAsync(HWND notifyHwnd,
const std::wstring& exePath,
const std::string& scriptUtf8,
const std::wstring& shellDialect) {
uint64_t gen = generation_.fetch_add(1) + 1;
std::thread([this, notifyHwnd, exePath, scriptUtf8, shellDialect, gen]() {
auto* result = new ShellCheckResult();
result->generation = gen;
result->success = executeShellCheckSync(exePath, scriptUtf8, shellDialect, result->issues, result->errorMessage);
if (!::IsWindow(notifyHwnd) ||
!::PostMessageW(notifyHwnd, WM_APP_SHELLCHECK_DONE, 0, reinterpret_cast<LPARAM>(result))) {
delete result;
}
}).detach();
return gen;
}