ocaml/stdlib/headernt.c

197 lines
6.2 KiB
C
Raw Normal View History

/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1998 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "caml/mlvalues.h"
#include "caml/exec.h"
#ifndef __MINGW32__
#pragma comment(linker , "/subsystem:console")
#pragma comment(lib , "kernel32")
#ifdef _UCRT
#pragma comment(lib , "ucrt.lib")
#pragma comment(lib , "vcruntime.lib")
#endif
#endif
2017-09-21 02:21:07 -07:00
char * default_runtime_name = RUNTIME_NAME;
static
#if _MSC_VER >= 1200
__forceinline
#else
__inline
#endif
unsigned long read_size(const char * const ptr)
{
const unsigned char * const p = (const unsigned char * const) ptr;
return ((unsigned long) p[0] << 24) | ((unsigned long) p[1] << 16) |
((unsigned long) p[2] << 8) | p[3];
}
2017-09-21 02:21:07 -07:00
static __inline char * read_runtime_path(HANDLE h)
{
char buffer[TRAILER_SIZE];
2017-09-21 02:21:07 -07:00
static char runtime_path[MAX_PATH];
DWORD nread;
int num_sections, path_size, i;
long ofs;
if (SetFilePointer(h, -TRAILER_SIZE, NULL, FILE_END) == -1) return NULL;
if (! ReadFile(h, buffer, TRAILER_SIZE, &nread, NULL)) return NULL;
if (nread != TRAILER_SIZE) return NULL;
num_sections = read_size(buffer);
ofs = TRAILER_SIZE + num_sections * 8;
if (SetFilePointer(h, - ofs, NULL, FILE_END) == -1) return NULL;
path_size = 0;
for (i = 0; i < num_sections; i++) {
if (! ReadFile(h, buffer, 8, &nread, NULL) || nread != 8) return NULL;
if (buffer[0] == 'R' && buffer[1] == 'N' &&
buffer[2] == 'T' && buffer[3] == 'M') {
path_size = read_size(buffer + 4);
ofs += path_size;
} else if (path_size > 0)
ofs += read_size(buffer + 4);
}
if (path_size == 0) return default_runtime_name;
if (path_size >= MAX_PATH) return NULL;
if (SetFilePointer(h, -ofs, NULL, FILE_END) == -1) return NULL;
if (! ReadFile(h, runtime_path, path_size, &nread, NULL)) return NULL;
if (nread != path_size) return NULL;
runtime_path[path_size - 1] = 0;
return runtime_path;
}
static BOOL WINAPI ctrl_handler(DWORD event)
{
if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT)
return TRUE; /* pretend we've handled them */
else
return FALSE;
}
2017-09-21 02:21:07 -07:00
#if WINDOWS_UNICODE
#define CP CP_UTF8
#else
#define CP CP_ACP
2017-09-21 02:21:07 -07:00
#endif
2017-09-20 00:39:06 -07:00
static void write_console(HANDLE hOut, WCHAR *wstr)
{
DWORD consoleMode, numwritten, len;
static char str[MAX_PATH];
2017-08-12 13:24:41 -07:00
if (GetConsoleMode(hOut, &consoleMode) != 0) {
/* The output stream is a Console */
2017-09-20 00:39:06 -07:00
WriteConsole(hOut, wstr, wcslen(wstr), &numwritten, NULL);
} else { /* The output stream is redirected */
2017-08-12 13:24:41 -07:00
len =
WideCharToMultiByte(CP, 0, wstr, wcslen(wstr), str, sizeof(str),
NULL, NULL);
2017-09-20 00:39:06 -07:00
WriteFile(hOut, str, len, &numwritten, NULL);
}
}
Unicode support for the Windows runtime (#1200) * Add support code * Explicitly reference ANSI Windows APIs * Adapt Sys.is_directory * Adapt ocamlrun * Add Changes entry * Add testsuite * Adapt Unix.open_process{_in,_out,_full,}, Unix.create_process{_env,} * Adapt headernt.c * Adapt Pervasives.open_{in,out}, Filename.temp_file, etc. * Adapt Sys.file_exists * Adapt Sys.remove * Adapt Sys.chdir * Adapt Sys.getcwd * Adapt Sys.getenv * Adapt Sys.command * Adapt Sys.readdir * Adapt CPLUGINS * Remove use of FormatMessageA, CreateFileA * Adapt Unix.mkdir * Adapt Unix.openfile * Adapt Unix.readlink * Adapt Unix.rename * Adapt Unix.{LargeFile,}.{l,}stat * Adapt Unix.system * Adapt Unix.{open,read}dir * Adapt Unix.link * Adapt Unix.symlink * Adapt Unix.getcwd * Adapt Unix.rmdir * Adapt Unix.utimes * Adapt Unix.unlink * Adapt Unix.chdir * Adapt Unix.chmod * Adapt Unix.{execv,execve,execvp,execvpe} * Compile with -DUNICODE -D_UNICODE under Windows * Add configure-time switch, Config.windows_unicode * Adapt Unix.putenv * Re-implement Unix.environment using GetEnvironmentStrings() * Use Unicode-aware flexdll * Adapt Unix.environment * AppVeyor: bootstrap flexdll * Adapt tests/embedded/cmmain.c * Adapt tests/lib-dynlink-csharp/entry.c * Remove exec tests * Fixup * Pass -municode to MinGW compiler * Try to fix tests/embedded * Adapt Sys.rename * Correct Changes entry * Makefile.several: use $(O) and $(NATIVECODE_ONLY) * Display => skipped correctly for tests/win-unicode * Add missing casts to execv* calls It's not clear why these aren't necessary for with char, but they are necessary with wchar_t on GCC (but not MSVC). * Missing header in systhreads (Win32 only) * Revert "Pass -municode to MinGW compiler" This reverts commit a4ce7fb319c429068a5b9d1ab14a2cc3969c355f. * Revert "Try to fix tests/embedded" This reverts commit 5197d8922295b7b339b970ec3189374aa15de4b8. * Revert "Remove exec tests" This reverts commit 306ccef2e79eca5b38ecfa285b912c7bcf3e9f52. * Don't pass $(LDFLAGS) when build ocamlc.opt It's already included via CC anyway, and it causes Unicode problems for Winodws (because the linker options need to be prefixed "-link" to go via flexlink). * Use wmain on Windows for ocamlrun * Build Unicode applications on Windows * Use wmain in headernt.c * Minor correction to win-unicode Makefile * Switch submodule to FlexDLL 0.36 * Build ocamlyacc as an ANSI application * Revert "Fixup" This reverts commit 500bd6b575ffd6c5b71c6953e55d740f0b090185. * Fix casts for execvp/execve * Remove tabs from test code * Fix Changes entry * shell32.lib is no longer necessary * Free allocated string * Changes: signal breaking change * Disable exec_tests * Protect with CAML_INTERNALS
2017-09-18 08:41:29 -07:00
static __inline void __declspec(noreturn) run_runtime(wchar_t * runtime,
wchar_t * const cmdline)
{
Unicode support for the Windows runtime (#1200) * Add support code * Explicitly reference ANSI Windows APIs * Adapt Sys.is_directory * Adapt ocamlrun * Add Changes entry * Add testsuite * Adapt Unix.open_process{_in,_out,_full,}, Unix.create_process{_env,} * Adapt headernt.c * Adapt Pervasives.open_{in,out}, Filename.temp_file, etc. * Adapt Sys.file_exists * Adapt Sys.remove * Adapt Sys.chdir * Adapt Sys.getcwd * Adapt Sys.getenv * Adapt Sys.command * Adapt Sys.readdir * Adapt CPLUGINS * Remove use of FormatMessageA, CreateFileA * Adapt Unix.mkdir * Adapt Unix.openfile * Adapt Unix.readlink * Adapt Unix.rename * Adapt Unix.{LargeFile,}.{l,}stat * Adapt Unix.system * Adapt Unix.{open,read}dir * Adapt Unix.link * Adapt Unix.symlink * Adapt Unix.getcwd * Adapt Unix.rmdir * Adapt Unix.utimes * Adapt Unix.unlink * Adapt Unix.chdir * Adapt Unix.chmod * Adapt Unix.{execv,execve,execvp,execvpe} * Compile with -DUNICODE -D_UNICODE under Windows * Add configure-time switch, Config.windows_unicode * Adapt Unix.putenv * Re-implement Unix.environment using GetEnvironmentStrings() * Use Unicode-aware flexdll * Adapt Unix.environment * AppVeyor: bootstrap flexdll * Adapt tests/embedded/cmmain.c * Adapt tests/lib-dynlink-csharp/entry.c * Remove exec tests * Fixup * Pass -municode to MinGW compiler * Try to fix tests/embedded * Adapt Sys.rename * Correct Changes entry * Makefile.several: use $(O) and $(NATIVECODE_ONLY) * Display => skipped correctly for tests/win-unicode * Add missing casts to execv* calls It's not clear why these aren't necessary for with char, but they are necessary with wchar_t on GCC (but not MSVC). * Missing header in systhreads (Win32 only) * Revert "Pass -municode to MinGW compiler" This reverts commit a4ce7fb319c429068a5b9d1ab14a2cc3969c355f. * Revert "Try to fix tests/embedded" This reverts commit 5197d8922295b7b339b970ec3189374aa15de4b8. * Revert "Remove exec tests" This reverts commit 306ccef2e79eca5b38ecfa285b912c7bcf3e9f52. * Don't pass $(LDFLAGS) when build ocamlc.opt It's already included via CC anyway, and it causes Unicode problems for Winodws (because the linker options need to be prefixed "-link" to go via flexlink). * Use wmain on Windows for ocamlrun * Build Unicode applications on Windows * Use wmain in headernt.c * Minor correction to win-unicode Makefile * Switch submodule to FlexDLL 0.36 * Build ocamlyacc as an ANSI application * Revert "Fixup" This reverts commit 500bd6b575ffd6c5b71c6953e55d740f0b090185. * Fix casts for execvp/execve * Remove tabs from test code * Fix Changes entry * shell32.lib is no longer necessary * Free allocated string * Changes: signal breaking change * Disable exec_tests * Protect with CAML_INTERNALS
2017-09-18 08:41:29 -07:00
wchar_t path[MAX_PATH];
STARTUPINFO stinfo;
PROCESS_INFORMATION procinfo;
DWORD retcode;
2017-08-12 13:24:41 -07:00
if (SearchPath(NULL, runtime, L".exe", sizeof(path)/sizeof(wchar_t),
path, &runtime) == 0) {
HANDLE errh;
errh = GetStdHandle(STD_ERROR_HANDLE);
2017-09-20 00:39:06 -07:00
write_console(errh, L"Cannot exec ");
write_console(errh, runtime);
write_console(errh, L"\r\n");
ExitProcess(2);
#if _MSC_VER >= 1200
__assume(0); /* Not reached */
#endif
}
/* Need to ignore ctrl-C and ctrl-break, otherwise we'll die and take
the underlying OCaml program with us! */
SetConsoleCtrlHandler(ctrl_handler, TRUE);
stinfo.cb = sizeof(stinfo);
stinfo.lpReserved = NULL;
stinfo.lpDesktop = NULL;
stinfo.lpTitle = NULL;
stinfo.dwFlags = 0;
stinfo.cbReserved2 = 0;
stinfo.lpReserved2 = NULL;
if (!CreateProcess(path, cmdline, NULL, NULL, TRUE, 0, NULL, NULL,
&stinfo, &procinfo)) {
HANDLE errh;
errh = GetStdHandle(STD_ERROR_HANDLE);
2017-09-20 00:39:06 -07:00
write_console(errh, L"Cannot exec ");
write_console(errh, runtime);
write_console(errh, L"\r\n");
ExitProcess(2);
#if _MSC_VER >= 1200
__assume(0); /* Not reached */
#endif
}
CloseHandle(procinfo.hThread);
WaitForSingleObject(procinfo.hProcess , INFINITE);
GetExitCodeProcess(procinfo.hProcess , &retcode);
CloseHandle(procinfo.hProcess);
ExitProcess(retcode);
#if _MSC_VER >= 1200
__assume(0); /* Not reached */
#endif
}
Unicode support for the Windows runtime (#1200) * Add support code * Explicitly reference ANSI Windows APIs * Adapt Sys.is_directory * Adapt ocamlrun * Add Changes entry * Add testsuite * Adapt Unix.open_process{_in,_out,_full,}, Unix.create_process{_env,} * Adapt headernt.c * Adapt Pervasives.open_{in,out}, Filename.temp_file, etc. * Adapt Sys.file_exists * Adapt Sys.remove * Adapt Sys.chdir * Adapt Sys.getcwd * Adapt Sys.getenv * Adapt Sys.command * Adapt Sys.readdir * Adapt CPLUGINS * Remove use of FormatMessageA, CreateFileA * Adapt Unix.mkdir * Adapt Unix.openfile * Adapt Unix.readlink * Adapt Unix.rename * Adapt Unix.{LargeFile,}.{l,}stat * Adapt Unix.system * Adapt Unix.{open,read}dir * Adapt Unix.link * Adapt Unix.symlink * Adapt Unix.getcwd * Adapt Unix.rmdir * Adapt Unix.utimes * Adapt Unix.unlink * Adapt Unix.chdir * Adapt Unix.chmod * Adapt Unix.{execv,execve,execvp,execvpe} * Compile with -DUNICODE -D_UNICODE under Windows * Add configure-time switch, Config.windows_unicode * Adapt Unix.putenv * Re-implement Unix.environment using GetEnvironmentStrings() * Use Unicode-aware flexdll * Adapt Unix.environment * AppVeyor: bootstrap flexdll * Adapt tests/embedded/cmmain.c * Adapt tests/lib-dynlink-csharp/entry.c * Remove exec tests * Fixup * Pass -municode to MinGW compiler * Try to fix tests/embedded * Adapt Sys.rename * Correct Changes entry * Makefile.several: use $(O) and $(NATIVECODE_ONLY) * Display => skipped correctly for tests/win-unicode * Add missing casts to execv* calls It's not clear why these aren't necessary for with char, but they are necessary with wchar_t on GCC (but not MSVC). * Missing header in systhreads (Win32 only) * Revert "Pass -municode to MinGW compiler" This reverts commit a4ce7fb319c429068a5b9d1ab14a2cc3969c355f. * Revert "Try to fix tests/embedded" This reverts commit 5197d8922295b7b339b970ec3189374aa15de4b8. * Revert "Remove exec tests" This reverts commit 306ccef2e79eca5b38ecfa285b912c7bcf3e9f52. * Don't pass $(LDFLAGS) when build ocamlc.opt It's already included via CC anyway, and it causes Unicode problems for Winodws (because the linker options need to be prefixed "-link" to go via flexlink). * Use wmain on Windows for ocamlrun * Build Unicode applications on Windows * Use wmain in headernt.c * Minor correction to win-unicode Makefile * Switch submodule to FlexDLL 0.36 * Build ocamlyacc as an ANSI application * Revert "Fixup" This reverts commit 500bd6b575ffd6c5b71c6953e55d740f0b090185. * Fix casts for execvp/execve * Remove tabs from test code * Fix Changes entry * shell32.lib is no longer necessary * Free allocated string * Changes: signal breaking change * Disable exec_tests * Protect with CAML_INTERNALS
2017-09-18 08:41:29 -07:00
int wmain(void)
{
Unicode support for the Windows runtime (#1200) * Add support code * Explicitly reference ANSI Windows APIs * Adapt Sys.is_directory * Adapt ocamlrun * Add Changes entry * Add testsuite * Adapt Unix.open_process{_in,_out,_full,}, Unix.create_process{_env,} * Adapt headernt.c * Adapt Pervasives.open_{in,out}, Filename.temp_file, etc. * Adapt Sys.file_exists * Adapt Sys.remove * Adapt Sys.chdir * Adapt Sys.getcwd * Adapt Sys.getenv * Adapt Sys.command * Adapt Sys.readdir * Adapt CPLUGINS * Remove use of FormatMessageA, CreateFileA * Adapt Unix.mkdir * Adapt Unix.openfile * Adapt Unix.readlink * Adapt Unix.rename * Adapt Unix.{LargeFile,}.{l,}stat * Adapt Unix.system * Adapt Unix.{open,read}dir * Adapt Unix.link * Adapt Unix.symlink * Adapt Unix.getcwd * Adapt Unix.rmdir * Adapt Unix.utimes * Adapt Unix.unlink * Adapt Unix.chdir * Adapt Unix.chmod * Adapt Unix.{execv,execve,execvp,execvpe} * Compile with -DUNICODE -D_UNICODE under Windows * Add configure-time switch, Config.windows_unicode * Adapt Unix.putenv * Re-implement Unix.environment using GetEnvironmentStrings() * Use Unicode-aware flexdll * Adapt Unix.environment * AppVeyor: bootstrap flexdll * Adapt tests/embedded/cmmain.c * Adapt tests/lib-dynlink-csharp/entry.c * Remove exec tests * Fixup * Pass -municode to MinGW compiler * Try to fix tests/embedded * Adapt Sys.rename * Correct Changes entry * Makefile.several: use $(O) and $(NATIVECODE_ONLY) * Display => skipped correctly for tests/win-unicode * Add missing casts to execv* calls It's not clear why these aren't necessary for with char, but they are necessary with wchar_t on GCC (but not MSVC). * Missing header in systhreads (Win32 only) * Revert "Pass -municode to MinGW compiler" This reverts commit a4ce7fb319c429068a5b9d1ab14a2cc3969c355f. * Revert "Try to fix tests/embedded" This reverts commit 5197d8922295b7b339b970ec3189374aa15de4b8. * Revert "Remove exec tests" This reverts commit 306ccef2e79eca5b38ecfa285b912c7bcf3e9f52. * Don't pass $(LDFLAGS) when build ocamlc.opt It's already included via CC anyway, and it causes Unicode problems for Winodws (because the linker options need to be prefixed "-link" to go via flexlink). * Use wmain on Windows for ocamlrun * Build Unicode applications on Windows * Use wmain in headernt.c * Minor correction to win-unicode Makefile * Switch submodule to FlexDLL 0.36 * Build ocamlyacc as an ANSI application * Revert "Fixup" This reverts commit 500bd6b575ffd6c5b71c6953e55d740f0b090185. * Fix casts for execvp/execve * Remove tabs from test code * Fix Changes entry * shell32.lib is no longer necessary * Free allocated string * Changes: signal breaking change * Disable exec_tests * Protect with CAML_INTERNALS
2017-09-18 08:41:29 -07:00
wchar_t truename[MAX_PATH];
wchar_t * cmdline = GetCommandLine();
2017-09-21 02:21:07 -07:00
char * runtime_path;
wchar_t wruntime_path[MAX_PATH];
HANDLE h;
Unicode support for the Windows runtime (#1200) * Add support code * Explicitly reference ANSI Windows APIs * Adapt Sys.is_directory * Adapt ocamlrun * Add Changes entry * Add testsuite * Adapt Unix.open_process{_in,_out,_full,}, Unix.create_process{_env,} * Adapt headernt.c * Adapt Pervasives.open_{in,out}, Filename.temp_file, etc. * Adapt Sys.file_exists * Adapt Sys.remove * Adapt Sys.chdir * Adapt Sys.getcwd * Adapt Sys.getenv * Adapt Sys.command * Adapt Sys.readdir * Adapt CPLUGINS * Remove use of FormatMessageA, CreateFileA * Adapt Unix.mkdir * Adapt Unix.openfile * Adapt Unix.readlink * Adapt Unix.rename * Adapt Unix.{LargeFile,}.{l,}stat * Adapt Unix.system * Adapt Unix.{open,read}dir * Adapt Unix.link * Adapt Unix.symlink * Adapt Unix.getcwd * Adapt Unix.rmdir * Adapt Unix.utimes * Adapt Unix.unlink * Adapt Unix.chdir * Adapt Unix.chmod * Adapt Unix.{execv,execve,execvp,execvpe} * Compile with -DUNICODE -D_UNICODE under Windows * Add configure-time switch, Config.windows_unicode * Adapt Unix.putenv * Re-implement Unix.environment using GetEnvironmentStrings() * Use Unicode-aware flexdll * Adapt Unix.environment * AppVeyor: bootstrap flexdll * Adapt tests/embedded/cmmain.c * Adapt tests/lib-dynlink-csharp/entry.c * Remove exec tests * Fixup * Pass -municode to MinGW compiler * Try to fix tests/embedded * Adapt Sys.rename * Correct Changes entry * Makefile.several: use $(O) and $(NATIVECODE_ONLY) * Display => skipped correctly for tests/win-unicode * Add missing casts to execv* calls It's not clear why these aren't necessary for with char, but they are necessary with wchar_t on GCC (but not MSVC). * Missing header in systhreads (Win32 only) * Revert "Pass -municode to MinGW compiler" This reverts commit a4ce7fb319c429068a5b9d1ab14a2cc3969c355f. * Revert "Try to fix tests/embedded" This reverts commit 5197d8922295b7b339b970ec3189374aa15de4b8. * Revert "Remove exec tests" This reverts commit 306ccef2e79eca5b38ecfa285b912c7bcf3e9f52. * Don't pass $(LDFLAGS) when build ocamlc.opt It's already included via CC anyway, and it causes Unicode problems for Winodws (because the linker options need to be prefixed "-link" to go via flexlink). * Use wmain on Windows for ocamlrun * Build Unicode applications on Windows * Use wmain in headernt.c * Minor correction to win-unicode Makefile * Switch submodule to FlexDLL 0.36 * Build ocamlyacc as an ANSI application * Revert "Fixup" This reverts commit 500bd6b575ffd6c5b71c6953e55d740f0b090185. * Fix casts for execvp/execve * Remove tabs from test code * Fix Changes entry * shell32.lib is no longer necessary * Free allocated string * Changes: signal breaking change * Disable exec_tests * Protect with CAML_INTERNALS
2017-09-18 08:41:29 -07:00
GetModuleFileName(NULL, truename, sizeof(truename)/sizeof(wchar_t));
h = CreateFile(truename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE ||
(runtime_path = read_runtime_path(h)) == NULL) {
HANDLE errh;
errh = GetStdHandle(STD_ERROR_HANDLE);
2017-09-20 00:39:06 -07:00
write_console(errh, truename);
write_console(errh, L" not found or is not a bytecode executable file\r\n");
ExitProcess(2);
#if _MSC_VER >= 1200
__assume(0); /* Not reached */
#endif
}
CloseHandle(h);
2017-08-12 13:24:41 -07:00
MultiByteToWideChar(CP, 0, runtime_path, -1, wruntime_path,
sizeof(wruntime_path)/sizeof(wchar_t));
2017-09-21 02:21:07 -07:00
run_runtime(wruntime_path , cmdline);
#if _MSC_VER >= 1200
__assume(0); /* Not reached */
#endif
#ifdef __MINGW32__
return 0;
#endif
}