1996-02-22 04:53:13 -08:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1996-02-22 04:53:13 -08:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1998-04-14 07:48:34 -07:00
|
|
|
/* Copyright 1998 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1996-02-22 04:53:13 -08:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
2000-03-09 01:05:19 -08:00
|
|
|
#define STRICT
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
|
|
|
#include <windows.h>
|
2014-12-27 06:41:49 -08:00
|
|
|
#include "caml/mlvalues.h"
|
|
|
|
#include "caml/exec.h"
|
1996-02-22 04:53:13 -08:00
|
|
|
|
2002-06-07 02:49:45 -07:00
|
|
|
#ifndef __MINGW32__
|
2000-03-09 01:05:19 -08:00
|
|
|
#pragma comment(linker , "/entry:headerentry")
|
|
|
|
#pragma comment(linker , "/subsystem:console")
|
|
|
|
#pragma comment(lib , "kernel32")
|
2002-06-07 02:49:45 -07:00
|
|
|
#endif
|
2000-03-09 01:05:19 -08:00
|
|
|
|
2011-03-17 09:18:05 -07:00
|
|
|
char * default_runtime_name = RUNTIME_NAME;
|
1998-06-01 07:53:28 -07:00
|
|
|
|
2000-03-09 01:05:19 -08:00
|
|
|
static
|
|
|
|
#if _MSC_VER >= 1200
|
|
|
|
__forceinline
|
|
|
|
#else
|
|
|
|
__inline
|
|
|
|
#endif
|
|
|
|
unsigned long read_size(const char * const ptr)
|
1998-04-14 07:48:34 -07:00
|
|
|
{
|
2000-03-09 01:05:19 -08:00
|
|
|
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];
|
1998-04-14 07:48:34 -07:00
|
|
|
}
|
|
|
|
|
2000-03-09 01:05:19 -08:00
|
|
|
static __inline char * read_runtime_path(HANDLE h)
|
1998-04-14 07:48:34 -07:00
|
|
|
{
|
|
|
|
char buffer[TRAILER_SIZE];
|
1998-08-14 02:57:43 -07:00
|
|
|
static char runtime_path[MAX_PATH];
|
1998-04-14 07:48:34 -07:00
|
|
|
DWORD nread;
|
2000-03-16 05:35:20 -08:00
|
|
|
int num_sections, path_size, i;
|
|
|
|
long ofs;
|
1998-04-14 07:48:34 -07:00
|
|
|
|
1998-06-01 07:53:28 -07:00
|
|
|
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;
|
2000-03-16 05:35:20 -08:00
|
|
|
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;
|
1998-06-01 07:53:28 -07:00
|
|
|
return runtime_path;
|
1998-04-14 07:48:34 -07:00
|
|
|
}
|
|
|
|
|
2001-11-27 04:58:19 -08:00
|
|
|
static BOOL WINAPI ctrl_handler(DWORD event)
|
|
|
|
{
|
|
|
|
if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT)
|
2002-07-23 07:12:03 -07:00
|
|
|
return TRUE; /* pretend we've handled them */
|
2001-11-27 04:58:19 -08:00
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-03-09 01:05:19 -08:00
|
|
|
#define msg_and_length(msg) msg , (sizeof(msg) - 1)
|
|
|
|
|
|
|
|
static __inline void __declspec(noreturn) run_runtime(char * runtime,
|
|
|
|
char * const cmdline)
|
1998-04-14 07:48:34 -07:00
|
|
|
{
|
2000-03-09 01:05:19 -08:00
|
|
|
char path[MAX_PATH];
|
|
|
|
STARTUPINFO stinfo;
|
|
|
|
PROCESS_INFORMATION procinfo;
|
|
|
|
DWORD retcode;
|
|
|
|
if (SearchPath(NULL, runtime, ".exe", MAX_PATH, path, &runtime) == 0) {
|
|
|
|
HANDLE errh;
|
|
|
|
DWORD numwritten;
|
|
|
|
errh = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
WriteFile(errh, msg_and_length("Cannot exec "), &numwritten, NULL);
|
|
|
|
WriteFile(errh, runtime, strlen(runtime), &numwritten, NULL);
|
|
|
|
WriteFile(errh, msg_and_length("\r\n"), &numwritten, NULL);
|
|
|
|
ExitProcess(2);
|
|
|
|
#if _MSC_VER >= 1200
|
|
|
|
__assume(0); /* Not reached */
|
|
|
|
#endif
|
|
|
|
}
|
2001-11-27 04:58:19 -08:00
|
|
|
/* Need to ignore ctrl-C and ctrl-break, otherwise we'll die and take
|
|
|
|
the underlying OCaml program with us! */
|
|
|
|
SetConsoleCtrlHandler(ctrl_handler, TRUE);
|
|
|
|
|
2000-03-09 01:05:19 -08:00
|
|
|
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;
|
|
|
|
DWORD numwritten;
|
|
|
|
errh = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
WriteFile(errh, msg_and_length("Cannot exec "), &numwritten, NULL);
|
|
|
|
WriteFile(errh, runtime, strlen(runtime), &numwritten, NULL);
|
|
|
|
WriteFile(errh, msg_and_length("\r\n"), &numwritten, NULL);
|
|
|
|
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
|
1998-04-14 07:48:34 -07:00
|
|
|
}
|
1996-02-22 04:53:13 -08:00
|
|
|
|
2002-06-07 02:49:45 -07:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
int main()
|
|
|
|
#else
|
2000-03-09 01:05:19 -08:00
|
|
|
void __declspec(noreturn) __cdecl headerentry()
|
2002-06-07 02:49:45 -07:00
|
|
|
#endif
|
1996-02-22 04:53:13 -08:00
|
|
|
{
|
1998-05-12 07:07:30 -07:00
|
|
|
char truename[MAX_PATH];
|
1998-02-27 06:07:09 -08:00
|
|
|
char * cmdline = GetCommandLine();
|
1998-06-01 07:53:28 -07:00
|
|
|
char * runtime_path;
|
1998-05-11 11:42:59 -07:00
|
|
|
HANDLE h;
|
1998-04-14 07:48:34 -07:00
|
|
|
|
1998-05-11 11:42:59 -07:00
|
|
|
GetModuleFileName(NULL, truename, sizeof(truename));
|
1998-12-02 06:39:27 -08:00
|
|
|
h = CreateFile(truename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL, OPEN_EXISTING, 0, NULL);
|
1998-05-11 11:42:59 -07:00
|
|
|
if (h == INVALID_HANDLE_VALUE ||
|
1998-06-01 07:53:28 -07:00
|
|
|
(runtime_path = read_runtime_path(h)) == NULL) {
|
2000-03-09 01:05:19 -08:00
|
|
|
HANDLE errh;
|
|
|
|
DWORD numwritten;
|
|
|
|
errh = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
WriteFile(errh, truename, strlen(truename), &numwritten, NULL);
|
2013-03-09 14:38:52 -08:00
|
|
|
WriteFile(errh, msg_and_length(" not found or is not a bytecode"
|
|
|
|
" executable file\r\n"),
|
2000-03-09 01:05:19 -08:00
|
|
|
&numwritten, NULL);
|
|
|
|
ExitProcess(2);
|
|
|
|
#if _MSC_VER >= 1200
|
|
|
|
__assume(0); /* Not reached */
|
|
|
|
#endif
|
1998-04-14 07:48:34 -07:00
|
|
|
}
|
1998-05-11 11:42:59 -07:00
|
|
|
CloseHandle(h);
|
2000-03-09 01:05:19 -08:00
|
|
|
run_runtime(runtime_path , cmdline);
|
|
|
|
#if _MSC_VER >= 1200
|
|
|
|
__assume(0); /* Not reached */
|
|
|
|
#endif
|
2002-06-18 06:01:53 -07:00
|
|
|
#ifdef __MINGW32__
|
2002-06-07 02:49:45 -07:00
|
|
|
return 0;
|
|
|
|
#endif
|
1996-02-22 04:53:13 -08:00
|
|
|
}
|