* arbiters.c, async.c, regex-posix.c: Use new smob interface.
[bpt/guile.git] / libguile / script.c
index ddeeedf..bf59da9 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2, or (at your option)
@@ -11,7 +11,8 @@
  * 
  * You should have received a copy of the GNU General Public License
  * along with this software; see the file COPYING.  If not, write to
- * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
  *
  * As a special exception, the Free Software Foundation gives permission
  * for additional uses of the text contained in its release of GUILE.
  *
  * If you write modifications of your own for GUILE, it is your choice
  * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.  
- */
+ * If you do not wish that, delete this exception notice.  */
 
 /* "script.c" argv tricks for `#!' scripts.
-   Author: Aubrey Jaffer */
+   Authors: Aubrey Jaffer and Jim Blandy */
 
 #include <stdio.h>
 #include <ctype.h>
 #include "_scm.h"
 #include "gh.h"
 #include "load.h"
+#include "version.h"
 
 #include "script.h"
 
-#ifdef __IBMC__
-#include <io.h>
-#endif /* def __IBMC__ */
-
-#ifdef linux
-#include <unistd.h>            /* for X_OK define */
-#endif /* def linux */
-#ifdef __svr4__
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>            /* for X_OK define */
-#else
-#ifdef __sgi__
-#include <unistd.h>            /* for X_OK define */
-#endif /* def __sgi__ */
-#endif /* def __svr4__ */
-#ifdef hpux
-#define const
-/**/
 #endif
 
 /* Concatentate str2 onto str1 at position n and return concatenated
@@ -145,12 +131,8 @@ scm_sep_init_try (path, sep, initname)
 #define X_OK 1
 #endif /* ndef X_OK */
 
-#ifdef unix
-#include <stdio.h>
-
 char *
-scm_find_executable (name)
-     const char *name;
+scm_find_executable (const char *name)
 {
   char tbuf[MAXPATHLEN];
   int i = 0;
@@ -180,159 +162,6 @@ scm_find_executable (name)
   fclose (f);
   return scm_cat_path (0L, name, 0L);
 }
-#endif /* unix */
-
-#ifdef MSDOS
-
-#define DEFAULT_PATH "C:\\DOS"
-#define PATH_DELIMITER ';'
-#define ABSOLUTE_FILENAME_P(fname) ((fname[0] == '\\') \
-                                    || (fname[0] && (fname[1] == ':')))
-
-char *
-dld_find_executable (file)
-     const char *file;
-{
-  /* fprintf(stderr, "dld_find_executable %s -> %s\n", file, scm_cat_path(0L, file, 0L)); fflush(stderr); */
-  return scm_cat_path (0L, file, 0L);
-}
-#endif /* def MSDOS */
-
-#if 0
-/* This code was originally borrowed from SCM; Guile sees things
-   differently.  */
-
-/* Given dld_find_executable()'s best guess for the pathname of this
-   executable, find (and verify the existence of) initname in the
-   implementation-vicinity of this program.  Returns a newly allocated
-   string if successful, 0 if not */
-
-char *
-scm_find_impl_file (exec_path, generic_name, initname, sep)
-     char *exec_path;
-     const char *generic_name, *initname, *sep;
-{
-  char *sepptr = strrchr (exec_path, sep[0]);
-  char *extptr = exec_path + strlen (exec_path);
-  char *path = 0;
-  /* fprintf(stderr, "dld_find_e %s\n", exec_path); fflush(stderr); */
-  if (sepptr)
-    {
-      long sepind = sepptr - exec_path + 1L;
-
-      /* In case exec_path is in the source directory, look first in
-         exec_path's directory. */
-      path = scm_cat_path (0L, exec_path, sepind - 1L);
-      path = scm_sep_init_try (path, sep, initname);
-      if (path)
-       return path;
-
-#ifdef MSDOS
-      if (!strcmp (extptr - 4, ".exe") || !strcmp (extptr - 4, ".com") ||
-         !strcmp (extptr - 4, ".EXE") || !strcmp (extptr - 4, ".COM"))
-       extptr = extptr - 4;
-#endif /* def MSDOS */
-
-      if (generic_name &&
-         !strncmp (exec_path + sepind, generic_name, extptr - exec_path))
-       generic_name = 0;
-
-      /* If exec_path is in directory "exe" or "bin": */
-      path = scm_cat_path (0L, exec_path, sepind - 1L);
-      sepptr = path + sepind - 4;
-      if (!strcmp (sepptr, "exe") || !strcmp (sepptr, "bin") ||
-         !strcmp (sepptr, "EXE") || !strcmp (sepptr, "BIN"))
-       {
-         char *peer;
-
-         /* Look for initname in peer directory "lib". */
-         if (path)
-           {
-             strncpy (sepptr, "lib", 3);
-             path = scm_sep_init_try (path, sep, initname);
-             if (path)
-               return path;
-           }
-
-         /* Look for initname in peer directories "lib" and "src" in
-            subdirectory with the name of the executable (sans any type
-            extension like .EXE). */
-         for (peer = "lib"; !0; peer = "src")
-           {
-             path = scm_cat_path (0L, exec_path, extptr - exec_path + 0L);
-             if (path)
-               {
-                 strncpy (path + sepind - 4, peer, 3);
-                 path[extptr - exec_path] = 0;
-                 path = scm_sep_init_try (path, sep, initname);
-                 if (path)
-                   return path;
-               }
-             if (!strcmp (peer, "src"))
-               break;
-           }
-
-         if (generic_name)
-           {
-
-             /* Look for initname in peer directories "lib" and "src" in
-                subdirectory with the generic name. */
-             for (peer = "lib"; !0; peer = "src")
-               {
-                 path = scm_cat_path (0L, exec_path, sepind);
-                 if (path)
-                   {
-                     strncpy (path + sepind - 4, "lib", 3);
-                     path = scm_cat_path (path, generic_name, 0L);
-                     path = scm_sep_init_try (path, sep, initname);
-                     if (path)
-                       return path;
-                   }
-                 if (!strcmp (peer, "src"))
-                   break;
-               }
-           }
-       }
-
-#ifdef MSDOS
-      if (strlen (extptr))
-       {
-         /* If exec_path has type extension, look in a subdirectory with
-            the name of the executable sans the executable file's type
-            extension. */
-         path = scm_cat_path (0L, exec_path, extptr - exec_path + 0L);
-         path = scm_sep_init_try (path, sep, initname);
-         if (path)
-           return path;
-
-         if (generic_name)
-           {
-
-             /* Also look in generic_name subdirectory. */
-             path = scm_cat_path (0L, exec_path, sepind);
-             if (path)
-               path = scm_cat_path (path, generic_name, 0L);
-             path = scm_sep_init_try (path, sep, initname);
-             if (path)
-               return path;
-           }
-       }
-#endif /* def MSDOS */
-    }
-  else
-    {
-
-      /* We don't have a parse-able exec_path.  The only path to try is
-         just initname. */
-      path = scm_cat_path (0L, initname, 0L);
-      if (path)
-       path = scm_try_path (path);
-      if (path)
-       return path;
-    }
-  return 0L;
-}
-#endif /* 0 */
 
 
 /* Read a \nnn-style escape.  We've just read the backslash.  */
@@ -556,6 +385,7 @@ scm_shell_usage (int fatal, char *message)
            "  -e FUNCTION    after reading script, apply FUNCTION to\n"
            "                 command line arguments\n"
            "  -ds            do -s script at this point\n"
+          "  -q             inhibit loading of user init file\n"
            "  --emacs        enable Emacs protocol (experimental)\n"
            "  -h, --help     display this help and exit\n"
            "  -v, --version  display version information and exit\n"
@@ -563,7 +393,7 @@ scm_shell_usage (int fatal, char *message)
            scm_usage_name);
 
   if (fatal)
-    exit (1);
+    exit (fatal);
 }
 
 
@@ -577,12 +407,6 @@ SCM_SYMBOL (sym_top_repl, "top-repl");
 SCM_SYMBOL (sym_quit, "quit");
 
 
-/* The boot code "ice-9/boot-9" is only loaded by
-   SCM_COMPILE_SHELL_SWITCHES when this is false.  The unexec code
-   uses this, to keep ice_9 from being loaded into dumped guile
-   executables.  */
-int scm_ice_9_already_loaded = 0;
-
 /* Given an array of command-line switches, return a Scheme expression
    to carry out the actions specified by the switches.
 
@@ -601,6 +425,8 @@ int scm_ice_9_already_loaded = 0;
    the usual way, and the auto-generated inner_main will do the right
    thing. */
 
+static char guile[] = "guile";
+
 SCM
 scm_compile_shell_switches (int argc, char **argv)
 {
@@ -612,12 +438,14 @@ scm_compile_shell_switches (int argc, char **argv)
                                   the "-ds" switch.  */
   SCM entry_point = SCM_EOL;   /* for -e switch */
   int interactive = 1;         /* Should we go interactive when done? */
+  int inhibit_user_init = 0;   /* Don't load user init file */
   int use_emacs_interface = 0;
   int i;
-  char *argv0;
+  char *argv0 = guile;
 
   if (argc > 0)
     {
+      argv0 = argv[0];
       scm_usage_name = strrchr (argv[0], '/');
       if (! scm_usage_name)
        scm_usage_name = argv[0];
@@ -625,8 +453,7 @@ scm_compile_shell_switches (int argc, char **argv)
        scm_usage_name++;
     }
   if (! scm_usage_name)
-    scm_usage_name = "guile";
-  argv0 = scm_usage_name;
+    scm_usage_name = guile;
   
   for (i = 1; i < argc; i++)
     {
@@ -707,6 +534,9 @@ scm_compile_shell_switches (int argc, char **argv)
       else if (! strcmp (argv[i], "--emacs")) /* use emacs protocol */ 
        use_emacs_interface = 1;
 
+      else if (! strcmp (argv[i], "-q")) /* don't load user init */ 
+       inhibit_user_init = 1;
+
       else if (! strcmp (argv[i], "-h")
               || ! strcmp (argv[i], "--help"))
        {
@@ -719,12 +549,12 @@ scm_compile_shell_switches (int argc, char **argv)
        {
          /* Print version number.  */
          printf ("Guile %s\n"
-                 "Copyright (c) 1995, 1996 Free Software Foundation\n"
+                 "Copyright (c) 1995, 1996, 1997 Free Software Foundation\n"
                  "Guile may be distributed under the terms of the GNU General Public Licence;\n"
                  "certain other uses are permitted as well.  For details, see the file\n"
                  "`COPYING', which is included in the Guile distribution.\n"
                  "There is no warranty, to the extent permitted by law.\n",
-                 GUILE_VERSION);
+                 SCM_CHARS (scm_version ()));
          exit (0);
        }
 
@@ -742,11 +572,13 @@ scm_compile_shell_switches (int argc, char **argv)
 
   /* Make any remaining arguments available to the
      script/command/whatever.  */
-  scm_set_program_arguments (argc - i, argv + i, argv0);
+  scm_set_program_arguments (argc ? argc - i : 0, argv + i, argv0);
   
   /* If the --emacs switch was set, now is when we process it.  */
-  scm_sysintern ("use-emacs-interface",
-                (use_emacs_interface) ? SCM_BOOL_T : SCM_BOOL_F);
+  {
+    SCM vcell = scm_sysintern0_no_module_lookup ("use-emacs-interface");
+    SCM_SETCDR (vcell, use_emacs_interface ? SCM_BOOL_T : SCM_BOOL_F);
+  }
 
   /* Handle the `-e' switch, if it was specified.  */
   if (entry_point != SCM_EOL)
@@ -755,11 +587,9 @@ scm_compile_shell_switches (int argc, char **argv)
                                SCM_EOL),
                       tail);
 
-  /* If we didn't end with a -c or a -s, load the user's customization
-     file, and start the repl.  */
+  /* If we didn't end with a -c or a -s, start the repl.  */
   if (interactive)
     {
-      tail = scm_cons (scm_cons (sym_load_user_init, SCM_EOL), tail);
       tail = scm_cons (scm_cons (sym_top_repl, SCM_EOL), tail);
     }
   else
@@ -767,25 +597,23 @@ scm_compile_shell_switches (int argc, char **argv)
       /* After doing all the other actions prescribed by the command line,
         quit.  */
       tail = scm_cons (scm_cons (sym_quit, SCM_EOL),
-                  tail);
+                      tail);
+      /* Allow asyncs (signal handlers etc.) to be run.  */
+      scm_mask_ints = 0;
+    }
+
+  /* After the following line, actions will be added to the front. */
+  tail = scm_reverse_x (tail, SCM_UNDEFINED);
+  
+  /* If we didn't end with a -c or a -s and didn't supply a -q, load
+     the user's customization file.  */
+  if (interactive && !inhibit_user_init)
+    {
+      tail = scm_cons (scm_cons (sym_load_user_init, SCM_EOL), tail);
     }
-  {
-    /* We want a path only containing directories from SCHEME_LOAD_PATH,
-       SCM_SITE_DIR and SCM_LIBRARY_DIR when searching for the site init
-       file, so we do this before loading Ice-9.  */
-    SCM init_path = scm_sys_search_load_path (scm_makfrom0str ("init.scm"));
-
-    /* Load Ice-9.  */
-    if (!scm_ice_9_already_loaded)
-      scm_primitive_load_path (scm_makfrom0str ("ice-9/boot-9.scm"));
-
-    /* Load the init.scm file.  */
-    if (SCM_NFALSEP (init_path))
-      scm_primitive_load (init_path);
-  }
 
   {
-    SCM val = scm_cons (sym_begin, scm_list_reverse_x (tail, SCM_UNDEFINED));
+    SCM val = scm_cons (sym_begin, tail);
 
 #if 0
     scm_write (val, SCM_UNDEFINED);