Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / load.c
index 66e3cc4..86d7e53 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+/* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2004, 2006, 2008,
+ *   2009, 2010, 2011, 2012 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 License
@@ -87,7 +88,9 @@ SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
 #define FUNC_NAME s_scm_primitive_load
 {
   SCM hook = *scm_loc_load_hook;
+  SCM ret = SCM_UNSPECIFIED;
   char *encoding;
+
   SCM_VALIDATE_STRING (1, filename);
   if (scm_is_true (hook) && scm_is_false (scm_procedure_p (hook)))
     SCM_MISC_ERROR ("value of %load-hook is neither a procedure nor #f",
@@ -96,8 +99,10 @@ SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
   if (!scm_is_false (hook))
     scm_call_1 (hook, filename);
 
-  { /* scope */
-    SCM port = scm_open_file (filename, scm_from_locale_string ("r"));
+  {
+    SCM port;
+
+    port = scm_open_file (filename, scm_from_locale_string ("r"));
     scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
     scm_i_dynwind_current_load_port (port);
 
@@ -124,13 +129,13 @@ SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
        if (SCM_EOF_OBJECT_P (form))
          break;
 
-       scm_primitive_eval_x (form);
+       ret = scm_primitive_eval_x (form);
       }
 
     scm_dynwind_end ();
     scm_close_port (port);
   }
-  return SCM_UNSPECIFIED;
+  return ret;
 }
 #undef FUNC_NAME
 
@@ -415,8 +420,9 @@ scm_c_string_has_an_ext (char *str, size_t len, SCM extensions)
 
 /* Search PATH for a directory containing a file named FILENAME.
    The file must be readable, and not a directory.
-   If we find one, return its full filename; otherwise, return #f.
+   If we find one, return its full pathname; otherwise, return #f.
    If FILENAME is absolute, return it unchanged.
+   We also fill *stat_buf corresponding to the returned pathname.
    If given, EXTENSIONS is a list of strings; for each directory 
    in PATH, we search for FILENAME concatenated with each EXTENSION.  */
 static SCM
@@ -441,7 +447,7 @@ search_path (SCM path, SCM filename, SCM extensions, SCM require_exts,
   filename_len = strlen (filename_chars);
   scm_dynwind_free (filename_chars);
 
-  /* If FILENAME is absolute, return it unchanged.  */
+  /* If FILENAME is absolute and is still valid, return it unchanged.  */
 #ifdef __MINGW32__
   if (((filename_len >= 1) && 
        (filename_chars[0] == '/' || filename_chars[0] == '\\')) ||
@@ -453,14 +459,13 @@ search_path (SCM path, SCM filename, SCM extensions, SCM require_exts,
   if (filename_len >= 1 && filename_chars[0] == '/')
 #endif
     {
-      SCM res = filename;
-      if (scm_is_true (require_exts) &&
-          !scm_c_string_has_an_ext (filename_chars, filename_len,
+      if ((scm_is_false (require_exts) ||
+           scm_c_string_has_an_ext (filename_chars, filename_len,
                                     extensions))
-        res = SCM_BOOL_F;
-
-      scm_dynwind_end ();
-      return res;
+          && stat (filename_chars, stat_buf) == 0
+          && !(stat_buf->st_mode & S_IFDIR))
+        result = filename;
+      goto end;
     }
 
   /* If FILENAME has an extension, don't try to add EXTENSIONS to it.  */
@@ -479,8 +484,7 @@ search_path (SCM path, SCM filename, SCM extensions, SCM require_exts,
               {
                 /* This filename has an extension, but not one of the right
                    ones... */
-                scm_dynwind_end ();
-                return SCM_BOOL_F;
+                goto end;
               }
            /* This filename already has an extension, so cancel the
                list of extensions.  */
@@ -567,9 +571,9 @@ SCM_DEFINE (scm_search_path, "search-path", 2, 0, 1,
            "@var{filename}. The file must be readable, and not a directory.\n"
            "If we find one, return its full filename; otherwise, return\n"
            "@code{#f}.  If @var{filename} is absolute, return it unchanged.\n"
-           "If given, @var{extensions} is a list of strings; for each\n"
+           "If given, @var{rest} is a list of extension strings; for each\n"
            "directory in @var{path}, we search for @var{filename}\n"
-           "concatenated with each @var{extension}.")
+           "concatenated with each extension.")
 #define FUNC_NAME s_scm_search_path
 {
   SCM extensions, require_exts;
@@ -663,11 +667,11 @@ compiled_is_fresh (SCM full_filename, SCM compiled_filename,
   else
     {
       compiled_is_newer = 0;
-      scm_puts (";;; note: source file ", scm_current_error_port ());
+      scm_puts_unlocked (";;; note: source file ", scm_current_error_port ());
       scm_display (full_filename, scm_current_error_port ());
-      scm_puts ("\n;;;       newer than compiled ", scm_current_error_port ());
+      scm_puts_unlocked ("\n;;;       newer than compiled ", scm_current_error_port ());
       scm_display (compiled_filename, scm_current_error_port ());
-      scm_puts ("\n", scm_current_error_port ());
+      scm_puts_unlocked ("\n", scm_current_error_port ());
     }
 
   return compiled_is_newer;
@@ -682,10 +686,10 @@ SCM_SYMBOL (sym_auto_compilation_options, "%auto-compilation-options");
 static SCM
 do_try_auto_compile (void *data)
 {
-  SCM source = PTR2SCM (data);
+  SCM source = SCM_PACK_POINTER (data);
   SCM comp_mod, compile_file;
 
-  scm_puts (";;; compiling ", scm_current_error_port ());
+  scm_puts_unlocked (";;; compiling ", scm_current_error_port ());
   scm_display (source, scm_current_error_port ());
   scm_newline (scm_current_error_port ());
 
@@ -714,16 +718,16 @@ do_try_auto_compile (void *data)
       /* Assume `*current-warning-prefix*' has an appropriate value.  */
       res = scm_call_n (scm_variable_ref (compile_file), args, 5);
 
-      scm_puts (";;; compiled ", scm_current_error_port ());
+      scm_puts_unlocked (";;; compiled ", scm_current_error_port ());
       scm_display (res, scm_current_error_port ());
       scm_newline (scm_current_error_port ());
       return res;
     }
   else
     {
-      scm_puts (";;; it seems ", scm_current_error_port ());
+      scm_puts_unlocked (";;; it seems ", scm_current_error_port ());
       scm_display (source, scm_current_error_port ());
-      scm_puts ("\n;;; is part of the compiler; skipping auto-compilation\n",
+      scm_puts_unlocked ("\n;;; is part of the compiler; skipping auto-compilation\n",
                 scm_current_error_port ());
       return SCM_BOOL_F;
     }
@@ -732,24 +736,24 @@ do_try_auto_compile (void *data)
 static SCM
 auto_compile_catch_handler (void *data, SCM tag, SCM throw_args)
 {
-  SCM source = PTR2SCM (data);
+  SCM source = SCM_PACK_POINTER (data);
   SCM oport, lines;
 
   oport = scm_open_output_string ();
   scm_print_exception (oport, SCM_BOOL_F, tag, throw_args);
 
-  scm_puts (";;; WARNING: compilation of ", scm_current_error_port ());
-  scm_display (source, scm_current_error_port ());
-  scm_puts (" failed:\n", scm_current_error_port ());
+  scm_puts_unlocked (";;; WARNING: compilation of ", scm_current_warning_port ());
+  scm_display (source, scm_current_warning_port ());
+  scm_puts_unlocked (" failed:\n", scm_current_warning_port ());
 
   lines = scm_string_split (scm_get_output_string (oport),
                             SCM_MAKE_CHAR ('\n'));
   for (; scm_is_pair (lines); lines = scm_cdr (lines))
     if (scm_c_string_length (scm_car (lines)))
       {
-        scm_puts (";;; ", scm_current_error_port ());
-        scm_display (scm_car (lines), scm_current_error_port ());
-        scm_newline (scm_current_error_port ());
+        scm_puts_unlocked (";;; ", scm_current_warning_port ());
+        scm_display (scm_car (lines), scm_current_warning_port ());
+        scm_newline (scm_current_warning_port ());
       }
 
   scm_close_port (oport);
@@ -765,9 +769,9 @@ SCM_DEFINE (scm_sys_warn_auto_compilation_enabled, "%warn-auto-compilation-enabl
 
   if (!message_shown)
     {
-      scm_puts (";;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0\n"
+      scm_puts_unlocked (";;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0\n"
                 ";;;       or pass the --no-auto-compile argument to disable.\n",
-                scm_current_error_port ());
+                scm_current_warning_port ());
       message_shown = 1;
     }
 
@@ -784,9 +788,9 @@ scm_try_auto_compile (SCM source)
   scm_sys_warn_auto_compilation_enabled ();
   return scm_c_catch (SCM_BOOL_T,
                       do_try_auto_compile,
-                      SCM2PTR (source),
+                      SCM_UNPACK_POINTER (source),
                       auto_compile_catch_handler,
-                      SCM2PTR (source),
+                      SCM_UNPACK_POINTER (source),
                       NULL, NULL);
 }
 
@@ -933,9 +937,9 @@ SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 0, 0, 1,
       if (stat_ret == 0 && compiled_is_fresh (full_filename, fallback,
                                               &stat_source, &stat_compiled))
         {
-          scm_puts (";;; found fresh local cache at ", scm_current_error_port ());
-          scm_display (fallback, scm_current_error_port ());
-          scm_newline (scm_current_error_port ());
+          scm_puts_unlocked (";;; found fresh local cache at ", scm_current_warning_port ());
+          scm_display (fallback, scm_current_warning_port ());
+          scm_newline (scm_current_warning_port ());
           return scm_load_compiled_with_vm (fallback);
         }
     }
@@ -998,7 +1002,7 @@ init_build_info ()
 
   for (i = 0; i < (sizeof (info) / sizeof (info[0])); i++)
     {
-      SCM key = scm_from_locale_symbol (info[i].name);
+      SCM key = scm_from_utf8_symbol (info[i].name);
       SCM val = scm_from_locale_string (info[i].value);
       *loc = scm_acons (key, val, *loc);
     }