Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / load.c
index 3cc48b9..155bdbd 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995,1996,1998,1999,2000,2001 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
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 
 \f
 
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
 #include "libguile/modules.h"
 #include "libguile/lang.h"
 #include "libguile/chars.h"
-#include "libguile/strop.h"
+#include "libguile/srfi-13.h"
 
 #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;
 
-static void
-swap_port (void *data)
-{
-  SCM *save_port = data, tmp = scm_cur_loadp;
-  scm_cur_loadp = *save_port;
-  *save_port = tmp;
-}
+/* The current reader (a fluid).  */
+static SCM the_reader = SCM_BOOL_F;
 
-static SCM
-load (void *data)
-{
-  SCM port = SCM_PACK (data);
-  while (1)
-    {
-      SCM form = scm_read (port);
-      if (SCM_EOF_OBJECT_P (form))
-       break;
-      scm_primitive_eval_x (form);
-    }
-  return SCM_UNSPECIFIED;
-}
 
 SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0, 
            (SCM filename),
@@ -104,14 +87,29 @@ SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
     scm_call_1 (hook, filename);
 
   { /* scope */
-    SCM port, save_port;
-    port = scm_open_file (filename, scm_mem2string ("r", sizeof (char)));
-    save_port = port;
-    scm_internal_dynamic_wind (swap_port,
-                              load,
-                              swap_port,
-                              (void *) SCM_UNPACK (port),
-                              &save_port);
+    SCM port = scm_open_file (filename, scm_from_locale_string ("r"));
+    scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
+    scm_i_dynwind_current_load_port (port);
+
+    while (1)
+      {
+       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_dynwind_end ();
     scm_close_port (port);
   }
   return SCM_UNSPECIFIED;
@@ -121,7 +119,7 @@ SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
 SCM
 scm_c_primitive_load (const char *filename)
 {
-  return scm_primitive_load (scm_makfrom0str (filename));
+  return scm_primitive_load (scm_from_locale_string (filename));
 }
 
 \f
@@ -134,7 +132,7 @@ SCM_DEFINE (scm_sys_package_data_dir, "%package-data-dir", 0, 0, 0,
            "@samp{/usr/local/share/guile}.")
 #define FUNC_NAME s_scm_sys_package_data_dir
 {
-  return scm_makfrom0str (SCM_PKGDATA_DIR);
+  return scm_from_locale_string (SCM_PKGDATA_DIR);
 }
 #undef FUNC_NAME
 #endif /* SCM_PKGDATA_DIR */
@@ -146,7 +144,7 @@ SCM_DEFINE (scm_sys_library_dir, "%library-dir", 0,0,0,
            "E.g., may return \"/usr/share/guile/1.3.5\".")
 #define FUNC_NAME s_scm_sys_library_dir
 {
-  return scm_makfrom0str(SCM_LIBRARY_DIR);
+  return scm_from_locale_string (SCM_LIBRARY_DIR);
 }
 #undef FUNC_NAME
 #endif /* SCM_LIBRARY_DIR */
@@ -158,7 +156,7 @@ SCM_DEFINE (scm_sys_site_dir, "%site-dir", 0,0,0,
            "E.g., may return \"/usr/share/guile/site\".")
 #define FUNC_NAME s_scm_sys_site_dir
 {
-  return scm_makfrom0str(SCM_SITE_DIR);
+  return scm_from_locale_string (SCM_SITE_DIR);
 }
 #undef FUNC_NAME
 #endif /* SCM_SITE_DIR */
@@ -204,16 +202,18 @@ SCM_DEFINE (scm_parse_path, "parse-path", 1, 1, 0,
 void
 scm_init_load_path ()
 {
+  char *env;
   SCM path = SCM_EOL;
 
 #ifdef SCM_LIBRARY_DIR
-  path = scm_list_3 (scm_makfrom0str (SCM_SITE_DIR),
-                    scm_makfrom0str (SCM_LIBRARY_DIR),
-                    scm_makfrom0str (SCM_PKGDATA_DIR));
+  path = scm_list_3 (scm_from_locale_string (SCM_SITE_DIR),
+                    scm_from_locale_string (SCM_LIBRARY_DIR),
+                    scm_from_locale_string (SCM_PKGDATA_DIR));
 #endif /* SCM_LIBRARY_DIR */
 
-  path = scm_parse_path (scm_from_locale_string (getenv ("GUILE_LOAD_PATH")),
-                        path);
+  env = getenv ("GUILE_LOAD_PATH");
+  if (env)
+    path = scm_parse_path (scm_from_locale_string (env), path);
 
   *scm_loc_load_path = path;
 }
@@ -240,7 +240,6 @@ stringbuf_grow (struct stringbuf *buf)
 {
   size_t ptroff = buf->ptr - buf->buf;
   buf->buf_len *= 2; 
-  // fprintf (stderr, "growing to %u\n", buf->buf_len);
   buf->buf = scm_realloc (buf->buf, buf->buf_len);
   buf->ptr = buf->buf + ptroff;
 }
@@ -317,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__
@@ -335,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;
     }
 
@@ -367,16 +366,16 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
 
   /* This simplifies the loop below a bit.
    */
-  if (SCM_NULLP (extensions))
+  if (scm_is_null (extensions))
     extensions = scm_listofnullstr;
 
   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.
    */
-  for (; SCM_CONSP (path); path = SCM_CDR (path))
+  for (; scm_is_pair (path); path = SCM_CDR (path))
     {
       SCM dir = SCM_CAR (path);
       SCM exts;
@@ -388,9 +387,9 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
       /* Concatenate the path name and the filename. */
       
 #ifdef __MINGW32__
-      if (buf.ptr > buf.buf && buf.ptr[-1] != '/' && buf.ptr[-1] != '\\')
+      if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/') && (buf.ptr[-1] != '\\'))
 #else
-      if (buf.ptr > buf.buf >= 1 && buf.ptr[-1] != '/')
+      if ((buf.ptr > buf.buf) && (buf.ptr[-1] != '/'))
 #endif
        stringbuf_cat (&buf, "/");
 
@@ -398,7 +397,7 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
       sans_ext_len = buf.ptr - buf.buf;
 
       /* Try every extension. */
-      for (exts = extensions; SCM_CONSP (exts); exts = SCM_CDR (exts))
+      for (exts = extensions; scm_is_pair (exts); exts = SCM_CDR (exts))
        {
          SCM ext = SCM_CAR (exts);
          struct stat mode;
@@ -409,8 +408,6 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 1, 0,
          /* If the file exists at all, we should return it.  If the
             file is inaccessible, then that's an error.  */
 
-         // fprintf (stderr, "trying: %s\n", buf.buf);
-
          if (stat (buf.buf, &mode) == 0
              && ! (mode.st_mode & S_IFDIR))
            {
@@ -427,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
@@ -471,24 +468,11 @@ SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
 {
   SCM full_filename;
 
-  SCM_VALIDATE_STRING (1, filename);
-
   full_filename = scm_sys_search_load_path (filename);
 
   if (scm_is_false (full_filename))
-    {
-      int absolute = (SCM_STRING_LENGTH (filename) >= 1
-#ifdef __MINGW32__
-                     && (SCM_STRING_CHARS (filename)[0] == '/' || 
-                         SCM_STRING_CHARS (filename)[0] == '\\'));
-#else
-                     && SCM_STRING_CHARS (filename)[0] == '/');
-#endif
-      SCM_MISC_ERROR ((absolute
-                      ? "Unable to load file ~S"
-                      : "Unable to find file ~S in load path"),
-                     scm_list_1 (filename));
-    }
+    SCM_MISC_ERROR ("Unable to find file ~S in load path",
+                   scm_list_1 (filename));
 
   return scm_primitive_load (full_filename);
 }
@@ -497,7 +481,7 @@ SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 0, 0,
 SCM
 scm_c_primitive_load_path (const char *filename)
 {
-  return scm_primitive_load_path (scm_makfrom0str (filename));
+  return scm_primitive_load_path (scm_from_locale_string (filename));
 }
 
 \f
@@ -513,12 +497,13 @@ init_build_info ()
   unsigned long i;
 
   for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
-    *loc = scm_acons (scm_str2symbol (info[i].name),
-                     scm_makfrom0str (info[i].value),
-                     *loc);
+    {
+      SCM key = scm_from_locale_symbol (info[i].name);
+      SCM val = scm_from_locale_string (info[i].value);
+      *loc = scm_acons (key, val, *loc);
+    }
 }
 
-
 \f
 void
 scm_init_load ()
@@ -527,10 +512,14 @@ scm_init_load ()
   scm_loc_load_path = SCM_VARIABLE_LOC (scm_c_define ("%load-path", SCM_EOL));
   scm_loc_load_extensions
     = SCM_VARIABLE_LOC (scm_c_define ("%load-extensions",
-                                   scm_list_2 (scm_makfrom0str (".scm"),
-                                               scm_nullstr)));
+                                     scm_list_2 (scm_from_locale_string (".scm"),
+                                                 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"