-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunver.cpp
More file actions
208 lines (187 loc) · 7.19 KB
/
Copy pathrunver.cpp
File metadata and controls
208 lines (187 loc) · 7.19 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <windows.h>
#include <fcntl.h>
#include <filesystem>
#include <io.h>
#include <iostream>
#include <set>
#include <string>
#include <vector>
namespace fs = std::filesystem;
static std::wstring lower(std::wstring s) {
for (wchar_t& c : s) c = towlower(c);
return s;
}
static std::wstring getenv_w(const wchar_t* name, const wchar_t* fallback) {
wchar_t buf[32768];
DWORD n = GetEnvironmentVariableW(name, buf, 32768);
if (n == 0 || n >= 32768) return fallback;
return std::wstring(buf, n);
}
static std::wstring expand_env(const std::wstring& s) {
wchar_t buf[32768];
DWORD n = ExpandEnvironmentStringsW(s.c_str(), buf, 32768);
if (n == 0 || n > 32768) return s;
return std::wstring(buf);
}
static std::wstring trim(const std::wstring& s, const wchar_t* junk = L" \t\"") {
size_t a = s.find_first_not_of(junk);
size_t b = s.find_last_not_of(junk);
if (a == std::wstring::npos) return L"";
return s.substr(a, b - a + 1);
}
static std::vector<std::wstring> split(const std::wstring& s, wchar_t sep) {
std::vector<std::wstring> out;
size_t start = 0;
while (start <= s.size()) {
size_t pos = s.find(sep, start);
if (pos == std::wstring::npos) { out.push_back(s.substr(start)); break; }
out.push_back(s.substr(start, pos - start));
start = pos + 1;
}
return out;
}
// returns the path with the real on-disk name casing, or empty when it is not a file
static std::wstring real_file(const std::wstring& p) {
WIN32_FIND_DATAW fd;
HANDLE h = FindFirstFileW(p.c_str(), &fd);
if (h == INVALID_HANDLE_VALUE) return L"";
FindClose(h);
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return L"";
return (fs::path(p).parent_path() / fd.cFileName).wstring();
}
// every match for the name in the exact order cmd would try them: current directory, then each PATH directory, PATHEXT order within a directory
static std::vector<std::wstring> find_candidates(const std::wstring& name) {
std::vector<std::wstring> exts;
for (const std::wstring& e : split(getenv_w(L"PATHEXT", L".COM;.EXE;.BAT;.CMD"), L';')) {
std::wstring ext = trim(e);
if (!ext.empty() && ext[0] == L'.') exts.push_back(ext);
}
bool has_ext = false;
std::wstring name_ext = lower(fs::path(name).extension().wstring());
for (const std::wstring& e : exts) {
if (lower(e) == name_ext) has_ext = true;
}
std::vector<std::wstring> dirs;
dirs.push_back(fs::current_path().wstring());
for (const std::wstring& raw : split(getenv_w(L"PATH", L""), L';')) {
std::wstring d = expand_env(trim(raw));
if (!d.empty()) dirs.push_back(d);
}
std::vector<std::wstring> found;
std::set<std::wstring> seen_dirs;
for (const std::wstring& d : dirs) {
std::wstring norm = lower(d);
while (norm.size() > 3 && (norm.back() == L'\\' || norm.back() == L'/')) norm.pop_back();
if (!seen_dirs.insert(norm).second) continue;
std::wstring base = (fs::path(d) / name).wstring();
if (has_ext) {
std::wstring r = real_file(base);
if (!r.empty()) found.push_back(r);
continue;
}
for (const std::wstring& e : exts) {
std::wstring r = real_file(base + e);
if (!r.empty()) found.push_back(r);
}
}
return found;
}
// Windows command line quoting rules: wrap in quotes when needed, double backslashes that precede a quote, escape embedded quotes
static std::wstring quote_arg(const std::wstring& a) {
if (!a.empty() && a.find_first_of(L" \t\"") == std::wstring::npos) return a;
std::wstring out = L"\"";
size_t backslashes = 0;
for (wchar_t c : a) {
if (c == L'\\') { backslashes++; continue; }
if (c == L'"') {
out.append(backslashes * 2 + 1, L'\\');
out += c;
} else {
out.append(backslashes, L'\\');
out += c;
}
backslashes = 0;
}
out.append(backslashes * 2, L'\\');
out += L"\"";
return out;
}
static int run(const std::wstring& exe, const std::vector<std::wstring>& args) {
std::wstring ext = lower(fs::path(exe).extension().wstring());
bool script = (ext == L".bat" || ext == L".cmd");
std::wstring cmdline;
std::wstring app;
if (script) {
// batch files are not executables, cmd has to interpret them
app = getenv_w(L"COMSPEC", L"cmd.exe");
cmdline = quote_arg(app) + L" /c \"" + quote_arg(exe);
for (const std::wstring& a : args) cmdline += L" " + quote_arg(a);
cmdline += L"\"";
} else {
app = exe;
cmdline = quote_arg(exe);
for (const std::wstring& a : args) cmdline += L" " + quote_arg(a);
}
// let Ctrl+C reach the child instead of killing the launcher before the child is done
SetConsoleCtrlHandler(nullptr, TRUE);
STARTUPINFOW si = {};
si.cb = sizeof(si);
PROCESS_INFORMATION pi = {};
std::vector<wchar_t> buf(cmdline.begin(), cmdline.end());
buf.push_back(0);
if (!CreateProcessW(app.c_str(), buf.data(), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi)) {
std::wcerr << L"runver: failed to start " << exe << L" (error " << GetLastError() << L")\n";
return 127;
}
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD code = 0;
GetExitCodeProcess(pi.hProcess, &code);
CloseHandle(pi.hProcess);
return (int)code;
}
static void print_help() {
std::wcout <<
L"runver - run the Nth program of a given name found on PATH\n"
L"\n"
L"usage: runver <n> <program> [args...]\n"
L" runver list <program>\n"
L"\n"
L" <n> 1-based index in cmd's search order, so 1 is what typing\n"
L" the bare name would run, 2 the first shadowed one, and so on\n"
L" list show every match with its index, in search order\n"
L"\n"
L"example: runver list python\n"
L" runver 2 python -c \"import sys; print(sys.version)\"\n";
}
int wmain(int argc, wchar_t* argv[]) {
_setmode(_fileno(stdout), _O_U8TEXT);
_setmode(_fileno(stderr), _O_U8TEXT);
if (argc < 3) {
print_help();
return argc == 2 && (std::wstring(argv[1]) == L"--help" || std::wstring(argv[1]) == L"-h") ? 0 : 2;
}
std::wstring mode = argv[1];
std::wstring program = argv[2];
std::vector<std::wstring> found = find_candidates(program);
if (mode == L"list") {
if (found.empty()) {
std::wcerr << L"runver: no match for " << program << L"\n";
return 1;
}
for (size_t i = 0; i < found.size(); i++) std::wcout << (i + 1) << L" " << found[i] << L"\n";
return 0;
}
wchar_t* end = nullptr;
long n = wcstol(mode.c_str(), &end, 10);
if (*end != 0 || n < 1) {
std::wcerr << L"runver: first argument must be a positive number or 'list'\n";
return 2;
}
if ((size_t)n > found.size()) {
std::wcerr << L"runver: only " << found.size() << L" match(es) for " << program << L", use 'runver list " << program << L"'\n";
return 1;
}
std::vector<std::wstring> args(argv + 3, argv + argc);
return run(found[n - 1], args);
}