upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/process_cmdline_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/process_cmdline_test.cpp')
-rw-r--r--runtime/test/process_cmdline_test.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp
index 8f35a8ef7..01f1b38a5 100644
--- a/runtime/test/process_cmdline_test.cpp
+++ b/runtime/test/process_cmdline_test.cpp
@@ -112,6 +112,66 @@ void check_winmain(const std::wstring& program, const std::vector<std::wstring>&
112 } 112 }
113 } 113 }
114} 114}
115
116// msvcrt.dll's __getmainargs/__wgetmainargs (what a MinGW or old-MSVC CRT startup calls) hand out the game's argv and
117// the process environment. The wide environment (_wenviron) does not exist until a wide environment function has run,
118// and the narrow table is reallocated by _putenv, so both must be read at the call, not remembered by
119// set_command_line (RT-136). `name=value` must be found in `env`, whatever was set before or after set_command_line.
120template <typename C, typename Fn>
121bool env_has(Fn getmainargs, const C* entry) {
122 int argc = 0;
123 C** argv = nullptr;
124 C** env = nullptr;
125 if (getmainargs(&argc, &argv, &env, 0, nullptr) != 0 || argc < 1 || !argv || !argv[0] || !env) return false;
126 for (C** e = env; *e; ++e) {
127 if constexpr (sizeof(C) == 1) {
128 if (std::strcmp(reinterpret_cast<const char*>(*e), reinterpret_cast<const char*>(entry)) == 0) return true;
129 } else {
130 if (std::wcscmp(reinterpret_cast<const wchar_t*>(*e), reinterpret_cast<const wchar_t*>(entry)) == 0)
131 return true;
132 }
133 }
134 return false;
135}
136
137void check_getmainargs_environment() {
138 using Narrow = int(__cdecl*)(int*, char***, char***, int, void*);
139 using Wide = int(__cdecl*)(int*, wchar_t***, wchar_t***, int, void*);
140 const auto narrow_fn = reinterpret_cast<Narrow>(rt::shims::find("msvcrt.dll", "__getmainargs"));
141 const auto wide_fn = reinterpret_cast<Wide>(rt::shims::find("msvcrt.dll", "__wgetmainargs"));
142 if (!narrow_fn || !wide_fn) {
143 std::fprintf(stderr, "FAIL msvcrt.dll!__getmainargs/__wgetmainargs are not shimmed\n");
144 ++failures;
145 return;
146 }
147 // First use of the wide environment in this process: set_command_line runs before anything made _wenviron.
148 // main() hosts start with only the narrow table; if a toolchain change made _wenviron exist, the first-call
149 // checks below no longer prove the creation path, so say so rather than pass silently.
150 if (_wenviron) std::printf("note: _wenviron already exists at startup; its lazy creation is not exercised\n");
151 rt::process::set_command_line(L"game.exe", {L"-x"});
152 if (_putenv_s("LEONIDA_RT136_A", "before") != 0 || // narrow only; the wide table is built on first use
153 !env_has<wchar_t>(wide_fn, L"LEONIDA_RT136_A=before")) {
154 std::fprintf(stderr, "FAIL __wgetmainargs: no environment (or one without LEONIDA_RT136_A) at the first call\n");
155 ++failures;
156 }
157 if (!env_has<char>(narrow_fn, "LEONIDA_RT136_A=before")) {
158 std::fprintf(stderr, "FAIL __getmainargs: no environment (or one without LEONIDA_RT136_A)\n");
159 ++failures;
160 }
161 // A variable set after set_command_line reallocates the CRT's table; a pointer kept from before would dangle.
162 if (_putenv_s("LEONIDA_RT136_B", "after") != 0 || _wputenv_s(L"LEONIDA_RT136_C", L"after") != 0) {
163 std::fprintf(stderr, "FAIL _putenv_s/_wputenv_s did not set the test variables\n");
164 ++failures;
165 }
166 if (!env_has<wchar_t>(wide_fn, L"LEONIDA_RT136_B=after") || !env_has<wchar_t>(wide_fn, L"LEONIDA_RT136_C=after")) {
167 std::fprintf(stderr, "FAIL __wgetmainargs: an environment variable set after set_command_line is missing\n");
168 ++failures;
169 }
170 if (!env_has<char>(narrow_fn, "LEONIDA_RT136_B=after") || !env_has<char>(narrow_fn, "LEONIDA_RT136_C=after")) {
171 std::fprintf(stderr, "FAIL __getmainargs: an environment variable set after set_command_line is missing\n");
172 ++failures;
173 }
174}
115} // namespace 175} // namespace
116 176
117int main() { 177int main() {
@@ -126,6 +186,7 @@ int main() {
126 check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); 186 check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page");
127 187
128 check_import_names(); 188 check_import_names();
189 check_getmainargs_environment();
129 check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name"); 190 check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name");
130 check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space"); 191 check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space");
131 check_winmain(L"game.exe", {}, L"", "no arguments"); 192 check_winmain(L"game.exe", {}, L"", "no arguments");