[project @ 2003-06-11 07:23:06 by simonpj]
[ghc-hetmet.git] / ghc / driver / ghci / ghci.c
index 723b767..bbf9dec 100644 (file)
@@ -1,9 +1,13 @@
 /*
  *
- * $Id: ghci.c,v 1.3 2001/08/02 01:01:46 sof Exp $
+ * $Id: ghci.c,v 1.7 2003/06/11 07:23:06 simonpj Exp $
  *
- * ghci wrapper - invokes ghc.exe with the added command-line
+ * ghci wrapper for Win32 only
+ * 
+ * This wrapper invokes ghc.exe with the added command-line
  *                option "--interactive".
+ * (On Unix this is done by the ghci.sh shell script, but
+ *  that does not work so well on Win32.)
  *
  * (c) The GHC Team 2001
  *
@@ -25,7 +29,7 @@
  *   * Compile it up (assuming the .ico file is in the same dir
  *     as the .rc file):
  *
- *         MSVC:    rc /I. ghci.rc /o ghci.res
+ *         MSVC:    rc /i. /fo ghci.res ghci.rc 
  *         mingw:   windres -o ghci.res -i ghci.rc -O coff
  *
  *   * Add the resulting .res file to the link line of the wrapper:
@@ -79,7 +83,7 @@ main(int argc, char** argv)
     return 1;
   }
   
-  new_argv = (char**)malloc(sizeof(char) * (argc + 1 + 1));
+  new_argv = (char**)malloc(sizeof(char*) * (argc + 1 + 1));
   if (new_argv == NULL) {
     errmsg("failed to start up ghc.exe");
     return 1;
@@ -95,12 +99,17 @@ main(int argc, char** argv)
   }
 
   for ( i=1; i < argc; i++ ) {
-    new_argv[i+1] = (char*)malloc(sizeof(char) * (strlen(argv[i] + 1)));
+    int len = strlen(argv[i]);
+    /* to avoid quoting issues, surround each option in double quotes */
+    new_argv[i+1] = (char*)malloc(sizeof(char) * (len + 3));
     if (new_argv[i+1] == NULL) {
       errmsg("failed to start up ghc.exe");
       return 1;
     } else {
-      strcpy(new_argv[i+1], argv[i]);
+      new_argv[i+1][0] = '"';
+      strcpy(1 + new_argv[i+1], argv[i]);
+      new_argv[i+1][len+1] = '"';
+      new_argv[i+1][len+2] = '\0';
     }
   }
   new_argv[i+1] = NULL;
@@ -113,8 +122,20 @@ main(int argc, char** argv)
      Cygwin gives me the right behaviour, but does it by
      implementing it in terms of spawnv(), so you pay
      the cost of having to create an extra process.
+     Plus, of course, we aren't allowed to use Cygwin here, because
+     GHC does not assume Cygwin.
      
-     ==> Just use spawnv().
+     ==> Just use spawnv(), which is provided by msvcrt.dll, the
+         Microsoft C runtime to which mingw delegates almost all
+        system calls
   */
+#if 0
+  fprintf(stderr, "Invoking ghc: ");
+  i=0;
+  while (new_argv[i] != NULL) {
+    fprintf(stderr, "%s ", new_argv[i++]);
+  }
+  fprintf(stderr, "\n"); fflush(stderr);
+#endif
   return _spawnv(_P_WAIT, binPath, new_argv);
 }