a41723a483eb9ee0275b53ee95943b6a07b107a1
[ghc-hetmet.git] / ghc / driver / ghci / ghci.c
1 /*
2  *
3  * $Id: ghci.c,v 1.9 2005/04/22 17:15:51 sof Exp $
4  *
5  * ghci wrapper for Win32 only
6  * 
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.)
11  *
12  * (c) The GHC Team 2001
13  *
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).
17  *
18  * To compile:
19  *
20  *   MSVC:    cl /o ghci.exe /c ghciwrap.c
21  *   mingw:   gcc -mno-cygwin -o ghci.exe ghciwrap.c
22  *
23  * If you want to associate your own icon with the wrapper,
24  * here's how to do it:
25  *
26  *   * Create a one-line .rc file, ghci.rc (say), containing
27  *          0 ICON "hsicon.ico"
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
30  *     as the .rc file):
31  *
32  *         MSVC:    rc /i. /fo ghci.res ghci.rc 
33  *         mingw:   windres -o ghci.res -i ghci.rc -O coff
34  *
35  *   * Add the resulting .res file to the link line of the wrapper:
36  *
37  *     MSVC:    cl /o ghci.exe /c ghciwrap.c ghci.res
38  *     mingw:   gcc -mno-cygwin -o ghci.exe ghciwrap.c ghci.res
39  *
40  */
41
42 #include <windows.h>
43 #include <stdio.h>
44 #include <process.h>
45 #include <malloc.h>
46 #include <stdlib.h>
47 #include <signal.h>
48 #include <io.h>
49
50 #define BINARY_NAME "ghc.exe"
51 #define IACTIVE_OPTION "--interactive"
52
53 #define errmsg(msg) fprintf(stderr, msg "\n"); fflush(stderr)
54 #define errmsg1(msg,val) fprintf(stderr, msg "\n",val); fflush(stderr)
55
56 int
57 main(int argc, char** argv)
58 {
59   TCHAR  binPath[FILENAME_MAX+1];
60   TCHAR  binPathShort[MAX_PATH+1];
61   DWORD  dwSize = FILENAME_MAX;
62   TCHAR* szEnd;
63   int    i;
64   char*  new_cmdline;
65   char   *ptr, *src;
66   unsigned int cmdline_len = 0;
67   char **pp;
68   LPTSTR pp1;
69
70   STARTUPINFO si;
71   PROCESS_INFORMATION pi;
72   
73   ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
74   ZeroMemory(&si, sizeof(STARTUPINFO));
75   si.cb = sizeof(STARTUPINFO);
76
77   /* Locate the binary we want to start up */
78   if ( !SearchPath(NULL,
79                    BINARY_NAME,
80                    NULL,
81                    dwSize,
82                    (char*)binPath,
83                    &szEnd) ) {
84     errmsg("Unable to locate ghc.exe");
85     return 1;
86   }
87   
88   dwSize = MAX_PATH;
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");
93     return 1;
94   }
95   
96   /* Compute length of the flattened 'argv', including extra IACTIVE_OPTION (and spaces!) */
97   cmdline_len += 1 + strlen(IACTIVE_OPTION);
98   for(i=1;i<argc;i++) {
99       /* Note: play it safe and quote all argv strings */
100       cmdline_len += 1 + strlen(argv[i]) + 2;
101   }
102   new_cmdline = (char*)malloc(sizeof(char) * (cmdline_len + 1));
103   if (!new_cmdline) {
104       errmsg("failed to start up ghc.exe; insufficient memory");
105       return 1;
106   }
107   
108   strcpy(new_cmdline, " " IACTIVE_OPTION);
109   ptr = new_cmdline + strlen(" " IACTIVE_OPTION);
110   for(i=1;i<argc;i++) {
111       *ptr++ = ' ';
112       *ptr++ = '"';
113       src = argv[i];
114       while(*src) {
115           *ptr++ = *src++;
116       }
117       *ptr++ = '"';
118   }
119   *ptr = '\0';
120   
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.
125      
126      Irrespective of this issue, using CreateProcess() is preferable,
127      as it makes this wrapper work on both mingw and cygwin.
128   */
129 #if 0
130   fprintf(stderr, "Invoking ghc: %s %s\n", binPathShort, new_cmdline); fflush(stderr);
131 #endif
132   if (!CreateProcess(binPathShort,
133                      new_cmdline,
134                      NULL,
135                      NULL,
136                      TRUE,
137                      0, /* dwCreationFlags */
138                      NULL, /* lpEnvironment */
139                      NULL, /* lpCurrentDirectory */
140                      &si,  /* lpStartupInfo */
141                      &pi) ) {
142       errmsg1("Unable to start ghc.exe (error code: %lu)", GetLastError());
143       return 1;
144   }
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.
148    */
149   FreeConsole();
150
151   switch (WaitForSingleObject(pi.hProcess, INFINITE) ) {
152   case WAIT_OBJECT_0:
153       return 0;
154   case WAIT_ABANDONED:
155   case WAIT_FAILED:
156       /* in the event we get any hard errors, bring the child to a halt. */
157       TerminateProcess(pi.hProcess,1);
158       return 1;
159   default:
160       return 1;
161   }
162 }