upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/process.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp49
1 files changed, 25 insertions, 24 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index 14aac92c3..c0070782f 100644
--- a/runtime/shims/process.cpp
+++ b/runtime/shims/process.cpp
@@ -41,29 +41,30 @@ std::atomic<bool> g_exited{false};
41std::atomic<int> g_exit_code{0}; 41std::atomic<int> g_exit_code{0};
42HANDLE g_done = nullptr; 42HANDLE g_done = nullptr;
43 43
44std::string quote(const std::string& a) { 44std::wstring quote(const std::wstring& a) {
45 if (!a.empty() && a.find_first_of(" \t\"") == std::string::npos) return a; 45 if (!a.empty() && a.find_first_of(L" \t\"") == std::wstring::npos) return a;
46 std::string out = "\""; 46 std::wstring out = L"\"";
47 size_t slashes = 0; // CommandLineToArgvW: backslashes are literal unless they precede a quote 47 size_t slashes = 0; // CommandLineToArgvW: backslashes are literal unless they precede a quote
48 for (char c : a) { 48 for (wchar_t c : a) {
49 if (c == '\\') { 49 if (c == L'\\') {
50 ++slashes; 50 ++slashes;
51 } else { 51 } else {
52 if (c == '"') out.append(slashes + 1, '\\'); 52 if (c == L'"') out.append(slashes + 1, L'\\');
53 slashes = 0; 53 slashes = 0;
54 } 54 }
55 out += c; 55 out += c;
56 } 56 }
57 return out.append(slashes, '\\') + "\""; // a trailing run is doubled before the closing quote 57 return out.append(slashes, L'\\') + L"\""; // a trailing run is doubled before the closing quote
58} 58}
59 59
60// The narrow arguments are ANSI code page text (main.cpp passes std::filesystem::path::string(), and the CRT's 60// The narrow arguments are ANSI code page text, like the CRT's narrow argv; under a UTF-8 activeCodePage CP_ACP is
61// narrow argv is ACP too); under a UTF-8 activeCodePage CP_ACP is UTF-8 anyway. 61// UTF-8 anyway. A character the code page has no mapping for becomes the code page's default character (or a
62std::wstring widen(const std::string& s) { 62// best-fit one), as in the CRT, instead of failing: std::filesystem::path::string() throws there (RT-84).
63 int n = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, nullptr, 0); 63std::string narrow(const std::wstring& w) {
64 std::wstring w(n > 0 ? n - 1 : 0, L'\0'); 64 int n = WideCharToMultiByte(CP_ACP, 0, w.c_str(), -1, nullptr, 0, nullptr, nullptr);
65 if (n > 1) MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, w.data(), n); 65 std::string s(n > 0 ? n - 1 : 0, '\0');
66 return w; 66 if (n > 1) WideCharToMultiByte(CP_ACP, 0, w.c_str(), -1, s.data(), n, nullptr, nullptr);
67 return s;
67} 68}
68 69
69[[noreturn]] void end_run(int code) { 70[[noreturn]] void end_run(int code) {
@@ -134,18 +135,18 @@ DWORD WINAPI game_thread(void* p) {
134 135
135} // namespace 136} // namespace
136 137
137void set_command_line(const std::string& program, const std::vector<std::string>& args) { 138void set_command_line(const std::wstring& program, const std::vector<std::wstring>& args) {
138 g_args.clear();
139 g_args.push_back(program);
140 g_args.insert(g_args.end(), args.begin(), args.end());
141 g_wargs.clear(); 139 g_wargs.clear();
142 g_cmdline.clear(); 140 g_wargs.push_back(program);
143 for (auto& a : g_args) { 141 g_wargs.insert(g_wargs.end(), args.begin(), args.end());
144 g_wargs.push_back(widen(a)); 142 g_args.clear();
145 if (!g_cmdline.empty()) g_cmdline += ' '; 143 g_wcmdline.clear();
146 g_cmdline += quote(a); 144 for (auto& a : g_wargs) {
145 g_args.push_back(narrow(a));
146 if (!g_wcmdline.empty()) g_wcmdline += L' ';
147 g_wcmdline += quote(a);
147 } 148 }
148 g_wcmdline = widen(g_cmdline); 149 g_cmdline = narrow(g_wcmdline);
149 g_argv.clear(); 150 g_argv.clear();
150 g_wargv.clear(); 151 g_wargv.clear();
151 for (auto& a : g_args) g_argv.push_back(a.data()); 152 for (auto& a : g_args) g_argv.push_back(a.data());