Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / load.c
index 4b51a0b..155bdbd 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004, 2006, 2008 Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -18,7 +18,7 @@
 
 \f
 
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
@@ -42,6 +42,7 @@
 
 #include "libguile/validate.h"
 #include "libguile/load.h"
+#include "libguile/fluids.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
    Applied to the full name of the file.  */
 static SCM *scm_loc_load_hook;
 
+/* The current reader (a fluid).  */
+static SCM the_reader = SCM_BOOL_F;
+
+
 SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0, 
            (SCM filename),
            "Load the file named @var{filename} and evaluate its contents in\n"
@@ -83,18 +88,28 @@ SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
 
   { /* scope */
     SCM port = scm_open_file (filename, scm_from_locale_string ("r"));
-    scm_frame_begin (SCM_F_FRAME_REWINDABLE);
-    scm_i_frame_current_load_port (port);
+    scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
+    scm_i_dynwind_current_load_port (port);
 
     while (1)
       {
-       SCM form = scm_read (port);
+       SCM reader, form;
+
+       /* Lookup and use the current reader to read the next
+          expression. */
+       reader = scm_fluid_ref (the_reader);
+       if (reader == SCM_BOOL_F)
+         form = scm_read (port);
+       else
+         form = scm_call_1 (reader, port);
+
        if (SCM_EOF_OBJECT_P (form))
          break;
+
        scm_primitive_eval_x (form);
       }
 
-    scm_frame_end ();
+    scm_dynwind_end ();
     scm_close_port (port);
   }
   return SCM_UNSPECIFIED;
@@ -301,11 +316,11 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
   if (SCM_UNBNDP (extensions))
     extensions = SCM_EOL;
 
-  scm_frame_begin (0);
+  scm_dynwind_begin (0);
 
   filename_chars = scm_to_locale_string (filename);
   filename_len = strlen (filename_chars);
-  scm_frame_free (filename_chars);
+  scm_dynwind_free (filename_chars);
 
   /* If FILENAME is absolute, return it unchanged.  */
 #ifdef __MINGW32__
@@ -319,7 +334,7 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
   if (filename_len >= 1 && filename_chars[0] == '/')
 #endif
     {
-      scm_frame_end ();
+      scm_dynwind_end ();
       return filename;
     }
 
@@ -356,7 +371,7 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
 
   buf.buf_len = 512;
   buf.buf = scm_malloc (buf.buf_len);
-  scm_frame_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
+  scm_dynwind_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY);
 
   /* Try every path element.
    */
@@ -409,7 +424,7 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
     scm_wrong_type_arg_msg (NULL, 0, path, "proper list");
 
  end:
-  scm_frame_end ();
+  scm_dynwind_end ();
   return result;
 }
 #undef FUNC_NAME
@@ -501,6 +516,10 @@ scm_init_load ()
                                                  scm_nullstr)));
   scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
 
+  the_reader = scm_make_fluid ();
+  scm_fluid_set_x (the_reader, SCM_BOOL_F);
+  scm_c_define("current-reader", the_reader);
+
   init_build_info ();
 
 #include "libguile/load.x"