[project @ 1999-12-03 14:38:39 by sewardj]
[ghc-hetmet.git] / ghc / interpreter / machdep.c
index 56089a7..cbe9d54 100644 (file)
@@ -13,8 +13,8 @@
  * included in the distribution.
  *
  * $RCSfile: machdep.c,v $
- * $Revision: 1.8 $
- * $Date: 1999/10/15 21:40:52 $
+ * $Revision: 1.16 $
+ * $Date: 1999/12/03 14:38:39 $
  * ------------------------------------------------------------------------*/
 
 #ifdef HAVE_SIGNAL_H
@@ -100,6 +100,9 @@ extern unsigned _stklen = 8000;         /* Allocate an 8k stack segment    */
 #ifdef HAVE_UNIX_H
 #include <unix.h>
 #endif
+#if SYMANTEC_C
+int allow_break_count = 0;
+#endif
 
 /* --------------------------------------------------------------------------
  * Prototypes for registry reading
@@ -113,7 +116,7 @@ extern unsigned _stklen = 8000;         /* Allocate an 8k stack segment    */
 #endif
 
 #define HugsRoot ("SOFTWARE\\Haskell\\Hugs\\" HUGS_VERSION "\\")
-#define ProjectRoot ("SOFTWARE\\Haskell\\Hugs\\Projects\\")
+#define ProjectRoot ("SOFTWARE\\Haskell\\Projects\\")
 
 static Bool   local createKey      Args((HKEY, String, PHKEY, REGSAM));
 static Bool   local queryValue     Args((HKEY, String, String, LPDWORD, LPBYTE, DWORD));
@@ -218,7 +221,7 @@ String f; {
     return (0 == access(f,4));
 #elif defined HAVE_SYS_STAT_H || defined HAVE_STAT_H
     struct stat scbuf;
-    //fprintf(stderr, "readable: %s\n", f );
+    /* fprintf(stderr, "readable: %s\n", f ); */
     return (  !stat(f,&scbuf) 
            && (scbuf.st_mode & S_IREAD) /* readable     */
            && (scbuf.st_mode & S_IFREG) /* regular file */
@@ -253,18 +256,21 @@ static Bool   local tryEndings    Args((String));
 # define SLASH                   '\\'
 # define isSLASH(c)              ((c)=='\\' || (c)=='/')
 # define PATHSEP                 ';'
+# define PATHSEP_STR             ";"
 # define DLL_ENDING              ".dll"
 #elif MAC_FILENAMES
 # define SLASH                   ':'
 # define isSLASH(c)              ((c)==SLASH)
 # define PATHSEP                 ';'
+# define PATHSEP_STR             ";"
 /* Mac PEF (Preferred Executable Format) file */
 # define DLL_ENDING              ".pef" 
 #else
 # define SLASH                   '/'
 # define isSLASH(c)              ((c)==SLASH)
 # define PATHSEP                 ':'
-# define DLL_ENDING              ".o"
+# define PATHSEP_STR             ":"
+# define DLL_ENDING              ".u_o"
 #endif
 
 static String local hugsdir() {     /* directory containing lib/Prelude.hs */
@@ -279,7 +285,7 @@ static String local hugsdir() {     /* directory containing lib/Prelude.hs */
         }
     }
     return dir;
-#elif HAVE_GETMODULEFILENAME && !DOS
+#elif HAVE_GETMODULEFILENAME && !DOS && !__CYGWIN32__
     /* On Windows, we can find the binary we're running and it's
      * conventional to put the libraries in the same place.
      */
@@ -290,7 +296,8 @@ static String local hugsdir() {     /* directory containing lib/Prelude.hs */
         if (dir[0] == '\0') { /* GetModuleFileName must have failed */
             return HUGSDIR;
         }
-        if (slash = strrchr(dir,SLASH)) { /* truncate after directory name */
+        slash = strrchr(dir,SLASH);
+        if (slash) { /* truncate after directory name */
             *slash = '\0';
         }
     }
@@ -306,7 +313,7 @@ static String local hugsdir() {     /* directory containing lib/Prelude.hs */
 }
 
 #if HSCRIPT    
-static String local hscriptDir() {  /* directory containing ?? what Daan?  */
+static String local hscriptDir() {  /* Directory containing hscript.dll           */
     static char dir[FILENAME_MAX+1] = "";
     if (dir[0] == '\0') { /* not initialised yet */
         String s = readRegString(HKEY_LOCAL_MACHINE,HScriptRoot,"InstallDir","");
@@ -376,9 +383,9 @@ String s; {                     /* a pathname in some appropriate manner.  */
 }
 
 #if HSCRIPT
-static String endings[] = { "", ".hi", ".hs", ".lhs", ".hsx", ".hash", 0 };
+static String endings[] = { "", ".u_hi", ".hs", ".lhs", ".hsx", ".hash", 0 };
 #else
-static String endings[] = { "", ".hi", ".hs", ".lhs", 0 };
+static String endings[] = { "", ".u_hi", ".hs", ".lhs", 0 };
 #endif
 static char   searchBuf[FILENAME_MAX+1];
 static Int    searchPos;
@@ -600,33 +607,40 @@ String path; {
  * New path handling stuff for the Combined System (tm)
  * ------------------------------------------------------------------------*/
 
-#define N_DEFAULT_LIBDIR 1000
-char defaultLibDir[N_DEFAULT_LIBDIR];
+char installDir[N_INSTALLDIR];
 
-/* Assumes that getcwd()++argv[0] is the absolute path to the
-   executable.  Basically wrong.
+/* Sets installDir to $STGHUGSDIR, and ensures there is a trailing
+   slash at the end.
 */
-void setDefaultLibDir ( String argv_0 )
+void setInstallDir ( String argv_0 )
 {
-   int i;
-   if (argv_0[0] != SLASH) {
-      if (!getcwd(defaultLibDir,N_DEFAULT_LIBDIR-strlen(argv_0)-10)) {
-         ERRMSG(0) "Can't get current working directory"
-         EEND;
-      }
-      i = strlen(defaultLibDir);
-      defaultLibDir[i++] = SLASH;
-   } else {
-      i = 0;
+   int   i;
+   char* r = getenv("STGHUGSDIR");
+   if (!r) {
+      fprintf(stderr, 
+          "%s: installation error: environment variable STGHUGSDIR is not set.\n",
+          argv_0 );
+      fprintf(stderr, 
+          "%s: pls set it to be the directory where STGHugs98 is installed.\n\n",
+          argv_0 );
+      exit(2);
+
    }
-   strcpy(&defaultLibDir[i],argv_0);
-   i += strlen(argv_0);
-   while (defaultLibDir[i] != SLASH) i--;
-   i++;
-   strcpy(&defaultLibDir[i], "lib");
-   /* fprintf ( stderr, "default lib dir = %s\n", defaultLibDir ); */
+
+   if (strlen(r) > N_INSTALLDIR-30 ) {
+      fprintf(stderr, 
+          "%s: environment variable STGHUGSDIR is suspiciously long; pls remedy\n\n",
+          argv_0 );
+      exit(2);
+   }
+
+   strcpy ( installDir, r );
+   i = strlen(installDir);
+   if (installDir[i-1] != SLASH) installDir[i++] = SLASH;
+   installDir[i] = 0;
 }
 
+
 Bool findFilesForModule ( 
         String  modName,
         String* path,
@@ -649,22 +663,35 @@ Bool findFilesForModule (
    Int    nPath;
    Bool   literate;
    String peStart, peEnd;
-   String augdPath;       /* .:defaultLibDir:hugsPath */
+   String augdPath;       /* .:hugsPath:installDir/GhcPrel:installDir/lib */
 
    *path = *sExt = NULL;
    *sAvail = *iAvail = *oAvail = FALSE;
    *sSize  = *iSize  = *oSize  = 0;
 
-   augdPath = malloc(4+strlen(defaultLibDir)+strlen(hugsPath));
+   augdPath = malloc( 2*(10+3+strlen(installDir)) 
+                      +strlen(hugsPath) +10/*paranoia*/);
    if (!augdPath)
       internal("moduleNameToFileNames: malloc failed(2)");
-   augdPath[0] = '.';
-   augdPath[1] = PATHSEP;
-   augdPath[2] = 0;
-   strcat ( augdPath, defaultLibDir );
-   augdPath[2+strlen(defaultLibDir)] = PATHSEP;
-   augdPath[3+strlen(defaultLibDir)] = 0;
-   strcat(augdPath,hugsPath);
+
+   augdPath[0] = 0;
+   strcat(augdPath, ".");
+   strcat(augdPath, PATHSEP_STR);
+
+   strcat(augdPath, hugsPath);
+   strcat(augdPath, PATHSEP_STR);
+
+   if (combined) {
+      strcat(augdPath, installDir);
+      strcat(augdPath, "GhcPrel");
+      strcat(augdPath, PATHSEP_STR);
+   }
+
+   strcat(augdPath, installDir);
+   strcat(augdPath, "lib");
+   strcat(augdPath, PATHSEP_STR);
+
+   /* fprintf ( stderr, "augdpath = `%s'\n", augdPath ); */
 
    peEnd = augdPath-1;
    while (1) {
@@ -705,7 +732,7 @@ Bool findFilesForModule (
          getFileInfo(searchBuf, oTime, oSize);
       }
 
-      strcpy(searchBuf+nPath, ".hi");
+      strcpy(searchBuf+nPath, ".u_hi");
       if (readable(searchBuf)) {
          *iAvail = TRUE;
          getFileInfo(searchBuf, iTime, iSize);
@@ -1064,7 +1091,61 @@ Int readTerminalChar() {                /* read character from terminal    */
     if (terminalEchoReqd) {
         return getchar();
     } else {
-        Int c = getch();
+#if IS_WIN32 && !HUGS_FOR_WINDOWS && !__BORLANDC__
+       /* When reading a character from the console/terminal, we want
+        * to operate in 'raw' mode (to use old UNIX tty parlance) and have
+        * it return when a character is available and _not_ wait until
+        * the next time the user hits carriage return. On Windows platforms,
+        * this _can_ be done by reading directly from the console, using
+        * getch().  However, this doesn't sit well with programming
+        * environments such as Emacs which allow you to create sub-processes
+        * running Hugs, and then communicate with the running interpreter
+        * through its standard input and output handles. If you use getch()
+        * in that setting, you end up trying to read the (unused) console
+        * of the editor itself, through which not a lot of characters is
+        * bound to come out, since the editor communicates input to Hugs
+        * via the standard input handle.
+        *
+        * To avoid this rather unfortunate situation, we use the Win32
+        * console API and re-jig the input properties of the standard
+        * input handle before trying to read a character using stdio's
+        * getchar().
+        * 
+        * The 'cost' of this solution is that it is Win32 specific and
+        * won't work with Windows 3.1 + it is kind of ugly and verbose
+        * to have to futz around with the console properties on a
+        * per-char basis. Both of these disadvantages aren't in my
+        * opinion fatal.
+        *
+        * -- sof 5/99
+        */
+        Int c;
+       DWORD mo;
+       HANDLE hIn;
+       /* I don't quite understand why, but if the FILE*'s underlying file
+          descriptor is in text mode, we seem to lose the first carriage
+          return.
+        */
+       setmode(fileno(stdin), _O_BINARY);
+       hIn = GetStdHandle(STD_INPUT_HANDLE);
+       GetConsoleMode(hIn, &mo);
+       SetConsoleMode(hIn, mo & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
+       /* 
+        * On Win9x, the first time you change the mode (as above) a
+        * raw '\n' is inserted.  Since enter maps to a raw '\r', and we
+        * map this (below) to '\n', we can just ignore all *raw* '\n's.
+        */
+       do {
+         c = getc(stdin);
+       } while (c == '\n');
+       /* Same as it ever was - revert back state of stdin. */
+       SetConsoleMode(hIn, mo);
+       setmode(fileno(stdin), _O_TEXT);
+#else
+       Int c = getch();
+#endif
         return c=='\r' ? '\n' : c;      /* slight paranoia about CR-LF    */
     }
 }
@@ -1121,8 +1202,21 @@ static sigHandler(panic) {              /* exit in a panic, on receipt of  */
 }
 #endif /* !DONT_PANIC */
 
+#if IS_WIN32
+BOOL WINAPI consoleHandler(DWORD dwCtrlType) {
+    switch (dwCtrlType) {              /* Allows Hugs to be terminated    */
+       case CTRL_CLOSE_EVENT :         /* from the window's close menu.   */
+           ExitProcess(0);
+    }
+    return FALSE;
+}
+#endif
 static Void local installHandlers() { /* Install handlers for all fatal    */ 
                                       /* signals except SIGINT and SIGBREAK*/
+#if IS_WIN32
+    SetConsoleCtrlHandler(consoleHandler,TRUE);
+#endif
 #if !DONT_PANIC && !DOS
 # ifdef SIGABRT
     signal(SIGABRT,panic);
@@ -1173,7 +1267,7 @@ String nm; {                            /* or just line may be zero        */
         String ec = editorCmd;
         String rd = NULL;               /* Set to nonnull to redo ...      */
 
-        for (; n>0 && *he && *he!=' '; n--)
+       for (; n>0 && *he && *he!=' ' && *he!='%'; n--)
             *ec++ = *he++;              /* Copy editor name to buffer      */
                                         /* assuming filename ends at space */
 
@@ -1428,6 +1522,20 @@ Int    val; {
 #endif /* USE_REGISTRY */
 
 /* --------------------------------------------------------------------------
+ * Things to do with the argv/argc and the env
+ * ------------------------------------------------------------------------*/
+
+int nh_argc ( void )
+{
+  return prog_argc;
+}
+
+int nh_argvb ( int argno, int offset )
+{
+  return (int)(prog_argv[argno][offset]);
+}
+
+/* --------------------------------------------------------------------------
  * Machine dependent control:
  * ------------------------------------------------------------------------*/