1996-02-22 04:53:13 -08:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
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 */
|
1996-02-22 04:53:13 -08:00
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1998-04-14 07:48:34 -07:00
|
|
|
#include <string.h>
|
1998-02-27 06:07:09 -08:00
|
|
|
#include <wtypes.h>
|
|
|
|
#include <winbase.h>
|
1996-02-22 04:53:13 -08:00
|
|
|
#include <process.h>
|
1998-04-14 07:48:34 -07:00
|
|
|
#include "../byterun/exec.h"
|
1996-02-22 04:53:13 -08:00
|
|
|
|
1998-06-01 07:53:28 -07:00
|
|
|
char * default_runtime_name = "ocamlrun";
|
|
|
|
|
1998-04-14 07:48:34 -07:00
|
|
|
static unsigned long read_size(char * ptr)
|
|
|
|
{
|
|
|
|
unsigned char * p = (unsigned char *) ptr;
|
|
|
|
return ((unsigned long) p[0] << 24) + ((unsigned long) p[1] << 16) +
|
|
|
|
((unsigned long) p[2] << 8) + p[3];
|
|
|
|
}
|
|
|
|
|
1998-06-01 07:53:28 -07:00
|
|
|
static char * read_runtime_path(HANDLE h)
|
1998-04-14 07:48:34 -07:00
|
|
|
{
|
|
|
|
char buffer[TRAILER_SIZE];
|
1998-06-01 07:53:28 -07:00
|
|
|
char runtime_path[MAX_PATH];
|
1998-04-14 07:48:34 -07:00
|
|
|
DWORD nread;
|
|
|
|
struct exec_trailer tr;
|
|
|
|
long size;
|
|
|
|
|
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;
|
1998-04-14 07:48:34 -07:00
|
|
|
tr.path_size = read_size(buffer);
|
|
|
|
tr.code_size = read_size(buffer + 4);
|
|
|
|
tr.prim_size = read_size(buffer + 8);
|
|
|
|
tr.data_size = read_size(buffer + 12);
|
|
|
|
tr.symbol_size = read_size(buffer + 16);
|
|
|
|
tr.debug_size = read_size(buffer + 20);
|
1998-06-01 07:53:28 -07:00
|
|
|
if (tr.path_size >= MAX_PATH) return NULL;
|
|
|
|
if (tr.path_size == 0) return default_runtime_path;
|
1998-04-14 07:48:34 -07:00
|
|
|
size = tr.path_size + tr.code_size + tr.prim_size +
|
|
|
|
tr.data_size + tr.symbol_size + tr.debug_size + TRAILER_SIZE;
|
1998-06-01 07:53:28 -07:00
|
|
|
if (SetFilePointer(h, -size, NULL, FILE_END) == -1) return NULL;
|
|
|
|
if (! ReadFile(h, runtime_path, tr.path_size, &nread, NULL)) return NULL;
|
|
|
|
if (nread != tr.path_size) return NULL;
|
1998-04-14 07:48:34 -07:00
|
|
|
runtime_path[tr.path_size - 1] = 0;
|
1998-06-01 07:53:28 -07:00
|
|
|
return runtime_path;
|
1998-04-14 07:48:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void errwrite(char * msg)
|
|
|
|
{
|
|
|
|
DWORD numwritten;
|
|
|
|
WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, strlen(msg),
|
|
|
|
&numwritten, NULL);
|
|
|
|
}
|
1996-02-22 04:53:13 -08:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
int main(int argc, char ** argv)
|
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;
|
|
|
|
int retcode;
|
1998-04-14 07:48:34 -07:00
|
|
|
|
1998-05-11 11:42:59 -07:00
|
|
|
GetModuleFileName(NULL, truename, sizeof(truename));
|
1998-04-14 07:48:34 -07:00
|
|
|
h = CreateFile(truename, GENERIC_READ, 0, 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) {
|
1998-04-14 07:48:34 -07:00
|
|
|
errwrite(truename);
|
|
|
|
errwrite(" not found or is not a bytecode executable file\r\n");
|
|
|
|
return 2;
|
|
|
|
}
|
1998-05-11 11:42:59 -07:00
|
|
|
CloseHandle(h);
|
1998-04-14 07:48:34 -07:00
|
|
|
retcode = spawnlp(P_WAIT, runtime_path, cmdline, NULL);
|
1996-02-22 04:53:13 -08:00
|
|
|
/* We use P_WAIT instead of P_OVERLAY here because under NT,
|
|
|
|
P_OVERLAY returns to the command interpreter, displaying the prompt
|
|
|
|
before executing the command. */
|
|
|
|
if (retcode == -1) {
|
1998-04-14 07:48:34 -07:00
|
|
|
errwrite("Cannot exec ");
|
|
|
|
errwrite(runtime_path);
|
|
|
|
errwrite("\r\n");
|
1996-02-22 04:53:13 -08:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return retcode;
|
|
|
|
}
|
1998-02-27 06:07:09 -08:00
|
|
|
|
|
|
|
/* Prevent VC++ from linking its own _setargv function, which
|
|
|
|
performs command-line processing (we don't need it) */
|
|
|
|
|
|
|
|
static void _setargv() { }
|
1998-04-14 07:48:34 -07:00
|
|
|
|