3 * $Id: ghci.c,v 1.9 2005/04/22 17:15:51 sof Exp $
5 * ghci wrapper for Win32 only
7 * This wrapper invokes ghc.exe with the added command-line
8 * option "--interactive".
9 * (On Unix this is done by the ghci.sh shell script, but
10 * that does not work so well on Win32.)
12 * (c) The GHC Team 2001
14 * ghc.exe is searched for using the 'normal' search rules
15 * for DLLs / EXEs (i.e., first in the same dir as this wrapper,
16 * then system dirs, then PATH).
20 * MSVC: cl /o ghci.exe /c ghciwrap.c
21 * mingw: gcc -mno-cygwin -o ghci.exe ghciwrap.c
23 * If you want to associate your own icon with the wrapper,
24 * here's how to do it:
26 * * Create a one-line .rc file, ghci.rc (say), containing
28 * (subst the string literal for the name of your icon file).
29 * * Compile it up (assuming the .ico file is in the same dir
32 * MSVC: rc /i. /fo ghci.res ghci.rc
33 * mingw: windres -o ghci.res -i ghci.rc -O coff
35 * * Add the resulting .res file to the link line of the wrapper:
37 * MSVC: cl /o ghci.exe /c ghciwrap.c ghci.res
38 * mingw: gcc -mno-cygwin -o ghci.exe ghciwrap.c ghci.res
50 #define BINARY_NAME "ghc.exe"
51 #define IACTIVE_OPTION "--interactive"
53 #define errmsg(msg) fprintf(stderr, msg "\n"); fflush(stderr)
54 #define errmsg1(msg,val) fprintf(stderr, msg "\n",val); fflush(stderr)
57 main(int argc, char** argv)
59 TCHAR binPath[FILENAME_MAX+1];
60 TCHAR binPathShort[MAX_PATH+1];
61 DWORD dwSize = FILENAME_MAX;
66 unsigned int cmdline_len = 0;
71 PROCESS_INFORMATION pi;
73 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
74 ZeroMemory(&si, sizeof(STARTUPINFO));
75 si.cb = sizeof(STARTUPINFO);
77 /* Locate the binary we want to start up */
78 if ( !SearchPath(NULL,
84 errmsg("Unable to locate ghc.exe");
89 /* Turn the path into short form - LFN form causes problems
90 when passed in argv[0]. */
91 if ( !(GetShortPathName(binPath, binPathShort, dwSize)) ) {
92 errmsg("Unable to locate ghc.exe");
96 /* Compute length of the flattened 'argv', including extra IACTIVE_OPTION (and spaces!) */
97 cmdline_len += 1 + strlen(IACTIVE_OPTION);
99 /* Note: play it safe and quote all argv strings */
100 cmdline_len += 1 + strlen(argv[i]) + 2;
102 new_cmdline = (char*)malloc(sizeof(char) * (cmdline_len + 1));
104 errmsg("failed to start up ghc.exe; insufficient memory");
108 strcpy(new_cmdline, " " IACTIVE_OPTION);
109 ptr = new_cmdline + strlen(" " IACTIVE_OPTION);
110 for(i=1;i<argc;i++) {
121 /* Note: Used to use _spawnv(_P_WAIT, ...) here, but it suffered
122 from the parent intercepting console events such as Ctrl-C,
123 which it shouldn't. Installing an ignore-all console handler
124 didn't do the trick either.
126 Irrespective of this issue, using CreateProcess() is preferable,
127 as it makes this wrapper work on both mingw and cygwin.
130 fprintf(stderr, "Invoking ghc: %s %s\n", binPathShort, new_cmdline); fflush(stderr);
132 if (!CreateProcess(binPathShort,
137 0, /* dwCreationFlags */
138 NULL, /* lpEnvironment */
139 NULL, /* lpCurrentDirectory */
140 &si, /* lpStartupInfo */
142 errmsg1("Unable to start ghc.exe (error code: %lu)", GetLastError());
145 /* Disable handling of console events in the parent by dropping its
146 * connection to the console. This has the (minor) downside of not being
147 * able to subsequently emit any error messages to the console.
151 switch (WaitForSingleObject(pi.hProcess, INFINITE) ) {
156 /* in the event we get any hard errors, bring the child to a halt. */
157 TerminateProcess(pi.hProcess,1);