[project @ 2001-07-30 10:40:36 by rrt]
[ghc-hetmet.git] / ghc / driver / ghci / ghci.c
1 /*
2  *
3  * $Id: ghci.c,v 1.1 2001/07/30 10:40:36 rrt Exp $
4  *
5  * ghci wrapper - invokes ghc.exe with the added command-line
6  *                option "--interactive".
7  *
8  * (c) The GHC Team 2001
9  *
10  * ghc.exe is searched for using the 'normal' search rules
11  * for DLLs / EXEs (i.e., first in the same dir as this wrapper,
12  * then system dirs, then PATH).
13  *
14  * To compile:
15  *
16  *   MSVC:    cl /o ghci.exe /c ghciwrap.c
17  *   mingw:   gcc -mno-cygwin -o ghci.exe ghciwrap.c
18  *
19  * If you want to associate your own icon with the wrapper,
20  * here's how to do it:
21  *
22  *   * Create a one-line .rc file, ghci.rc (say), containing
23  *          0 ICON "hsicon.ico"
24  *     (subst the string literal for the name of your icon file).
25  *   * Compile it up (assuming the .ico file is in the same dir
26  *     as the .rc file):
27  *
28  *         MSVC:    rc /I. ghci.rc /o ghci.res
29  *         mingw:   windres -o ghci.res -o ghci.rc -O coff
30  *
31  *   * Add the resulting .res file to the link line of the wrapper:
32  *
33  *     MSVC:    cl /o ghci.exe /c ghciwrap.c ghci.res
34  *     mingw:   gcc -mno-cygwin -o ghci.exe ghciwrap.c ghci.res
35  *
36  */
37
38 #include <windows.h>
39 #include <stdio.h>
40 #include <process.h>
41 #include <malloc.h>
42
43 #define BINARY_NAME "ghc.exe"
44 #define IACTIVE_OPTION "--interactive"
45
46 #define errmsg(msg) fprintf(stderr, msg "\n"); fflush(stderr)
47
48 int
49 main(int argc, char** argv)
50 {
51   TCHAR  binPath[FILENAME_MAX+1];
52   DWORD  dwSize = FILENAME_MAX;
53   DWORD  dwRes;
54   TCHAR* szEnd;
55   char** new_argv;
56   int    i;
57
58   /* Locate the binary we want to start up */
59   dwRes = 
60     SearchPath(NULL,
61                BINARY_NAME,
62                NULL,
63                dwSize,
64                binPath,
65                &szEnd);
66                
67   if (dwRes == 0) {            
68     errmsg("Unable to locate ghc.exe");
69     return 1;
70   }
71   
72   new_argv = (char**)malloc(sizeof(char) * (argc + 1 + 1));
73   if (new_argv == NULL) {
74     errmsg("failed to start up ghc.exe");
75     return 1;
76   }
77   new_argv[0] = binPath;
78
79   new_argv[1] = (char*)malloc(sizeof(char) * (strlen(IACTIVE_OPTION) + 1));
80   if (new_argv[1]) {
81     strcpy(new_argv[1], IACTIVE_OPTION);
82   } else {
83     errmsg("failed to start up ghc.exe");
84     return 1;
85   }
86
87   for ( i=1; i < argc; i++ ) {
88     new_argv[i+1] = (char*)malloc(sizeof(char) * (strlen(argv[i] + 1)));
89     if (new_argv[i+1] == NULL) {
90       errmsg("failed to start up ghc.exe");
91       return 1;
92     } else {
93       strcpy(new_argv[i+1], argv[i]);
94     }
95   }
96   new_argv[i+1] = NULL;
97   
98   /* I was hoping to be able to use execv() here, but
99      the MS implementation of said function doesn't appear to
100      be quite right (the 'parent' app seems to exit without
101      waiting, which is not a spec-fulfilling thing to do).
102      
103      Cygwin gives me the right behaviour, but does it by
104      implementing it in terms of spawnv(), so you pay
105      the cost of having to create an extra process.
106      
107      ==> Just use spawnv().
108   */
109   return _spawnv(_P_WAIT, binPath, new_argv);
110 }