Applied patches to Git interpreter from Gargoyle source code
[projects/chimara/chimara.git] / interpreters / git / git_unix.c
index 04cfe60ec59f3c5ff18724a75dc8e3c45a37be73..2b9c6e342067b09dd9a5e0b641e853cdde25ee7d 100644 (file)
@@ -25,23 +25,47 @@ glkunix_argumentlist_t glkunix_arguments[] =
 #define CACHE_SIZE (256 * 1024L)
 #define UNDO_SIZE (512 * 1024L)
 
+int gHasInited = 0;
+
+#ifdef CHIMARA_EXTENSIONS
+
+void fatalError (const char * s)
+{
+       winid_t win;
+       if (!gHasInited)
+       {
+               win = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
+               glk_set_window(win);
+       }
+       /* pray that this goes somewhere reasonable... */
+       glk_put_string("\n*** fatal error: ");
+       glk_put_string((char*)s);
+       glk_put_string(" ***\n");
+       glk_exit();
+}
+
+#else
+
 void fatalError (const char * s)
 {
     fprintf (stderr, "*** fatal error: %s ***\n", s);
     exit (1);
 }
 
+#endif /* CHIMARA_EXTENSIONS */
+
 #ifdef USE_MMAP
 // Fast loader that uses some fancy Unix features.
 
 const char * gFilename = 0;
+char * gStartupError = 0;
 
 int glkunix_startup_code(glkunix_startup_t *data)
 {
     if (data->argc <= 1)
     {
-        printf ("usage: git gamefile.ulx\n");
-        return 0;
+        gStartupError = "No file given";
+        return 1;
     }
     gFilename = data->argv[1];
     return 1;
@@ -52,7 +76,10 @@ void glk_main ()
     int          file;
     struct stat  info;
     const char * ptr;
-    
+
+       if (gStartupError)
+               fatalError(gStartupError);
+
     file = open (gFilename, O_RDONLY);
     if (file < 0)
         goto error;
@@ -61,35 +88,35 @@ void glk_main ()
         goto error;
     
     if (info.st_size < 256)
-    {
-        fprintf (stderr, "This is too small to be a glulx file.\n");
-        exit (1);
-    }
+               fatalError("This is too small to be a glulx file.");
 
     ptr = mmap (NULL, info.st_size, PROT_READ, MAP_PRIVATE, file, 0);
     if (ptr == MAP_FAILED)
         goto error;
+
+       gHasInited = 1;
         
     git (ptr, info.st_size, CACHE_SIZE, UNDO_SIZE);
     munmap ((void*) ptr, info.st_size);
     return;
     
 error:
-    perror ("git");
-    exit (errno);
+       sprintf(errmsg, "git: %s", strerror(errno));
+    fatalError(errmsg);
 }
 
 #else
 // Generic loader that should work anywhere.
 
 strid_t gStream = 0;
+char * gStartupError = 0;
 
 int glkunix_startup_code(glkunix_startup_t *data)
 {
     if (data->argc <= 1)
     {
-        printf ("usage: git gamefile.ulx\n");
-        return 0;
+        gStartupError = "No file given";
+        return 1;
     }
     gStream = glkunix_stream_open_pathname ((char*) data->argv[1], 0, 0);
     return 1;
@@ -97,9 +124,14 @@ int glkunix_startup_code(glkunix_startup_t *data)
 
 void glk_main ()
 {
+       if (gStartupError)
+               fatalError(gStartupError);
+
     if (gStream == NULL)
         fatalError ("could not open game file");
 
+       gHasInited = 1;
+
     gitWithStream (gStream, CACHE_SIZE, UNDO_SIZE);
 }