Changes from arch/CVS synchronization
[bpt/guile.git] / libguile / script.c
index 5b62d65..b024378 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+/* Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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 as published by the Free Software Foundation; either
@@ -11,7 +11,7 @@
  *
  * 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
  */
 
 /* "script.c" argv tricks for `#!' scripts.
@@ -120,7 +120,7 @@ char *
 scm_find_executable (const char *name)
 {
   char tbuf[MAXPATHLEN];
-  int i = 0;
+  int i = 0, c;
   FILE *f;
 
   /* fprintf(stderr, "s_f_e checking access %s ->%d\n", name, access(name, X_OK)); fflush(stderr); */
@@ -132,16 +132,19 @@ scm_find_executable (const char *name)
   if ((fgetc (f) == '#') && (fgetc (f) == '!'))
     {
       while (1)
-       switch (tbuf[i++] = fgetc (f))
+       switch (c = fgetc (f))
          {
          case /*WHITE_SPACES */ ' ':
          case '\t':
          case '\r':
          case '\f':
          case EOF:
-           tbuf[--i] = 0;
+           tbuf[i] = 0;
            fclose (f);
            return scm_cat_path (0L, tbuf, 0L);
+         default:
+           tbuf[i++] = c;
+           break;
          }
     }
   fclose (f);
@@ -361,6 +364,7 @@ scm_shell_usage (int fatal, char *message)
            "remaining arguments as the value of (command-line).\n"
            "If FILE begins with `-' the -s switch is mandatory.\n"
            "\n"
+           "  -L DIRECTORY   add DIRECTORY to the front of the module load path\n"
            "  -l FILE        load Scheme source code from FILE\n"
            "  -e FUNCTION    after reading script, apply FUNCTION to\n"
            "                 command line arguments\n"
@@ -377,7 +381,9 @@ scm_shell_usage (int fatal, char *message)
            "  -v, --version  display version information and exit\n"
           "  \\              read arguments from following script lines\n"
            "\n"
-          "Please report bugs to bug-guile@gnu.org\n",
+          "Please report bugs to bug-guile@gnu.org.  (Note that you must\n"
+          "be subscribed to this list first, in order to successfully send\n"
+          "a report to it).\n",
            scm_usage_name);
 
   if (fatal)
@@ -395,7 +401,12 @@ SCM_SYMBOL (sym_load_user_init, "load-user-init");
 SCM_SYMBOL (sym_top_repl, "top-repl");
 SCM_SYMBOL (sym_quit, "quit");
 SCM_SYMBOL (sym_use_srfis, "use-srfis");
-
+SCM_SYMBOL (sym_load_path, "%load-path");
+SCM_SYMBOL (sym_set_x, "set!");
+SCM_SYMBOL (sym_cons, "cons");
+SCM_SYMBOL (sym_at, "@");
+SCM_SYMBOL (sym_atat, "@@");
+SCM_SYMBOL (sym_main, "main");
 
 /* Given an array of command-line switches, return a Scheme expression
    to carry out the actions specified by the switches.
@@ -408,6 +419,18 @@ SCM_SYMBOL (sym_use_srfis, "use-srfis");
 
 static char guile[] = "guile";
 
+static int
+all_symbols (SCM list)
+{
+  while (scm_is_pair (list))
+    {
+      if (!scm_is_symbol (SCM_CAR (list)))
+       return 0;
+      list = SCM_CDR (list);
+    }
+  return 1;
+}
+
 SCM
 scm_compile_shell_switches (int argc, char **argv)
 {
@@ -418,6 +441,7 @@ scm_compile_shell_switches (int argc, char **argv)
                                   the "load" command, in case we get
                                   the "-ds" switch.  */
   SCM entry_point = SCM_EOL;   /* for -e switch */
+  SCM user_load_path = SCM_EOL; /* for -L 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;
@@ -449,7 +473,7 @@ scm_compile_shell_switches (int argc, char **argv)
          /* If we specified the -ds option, do_script points to the
             cdr of an expression like (load #f); we replace the car
             (i.e., the #f) with the script name.  */
-         if (!SCM_NULLP (do_script))
+         if (!scm_is_null (do_script))
            {
              SCM_SETCAR (do_script, scm_from_locale_string (argv[i]));
              do_script = SCM_EOL;
@@ -496,10 +520,42 @@ scm_compile_shell_switches (int argc, char **argv)
            scm_shell_usage (1, "missing argument to `-l' switch");
        }         
 
+      else if (! strcmp (argv[i], "-L")) /* add to %load-path */
+       {
+         if (++i < argc)
+           user_load_path =
+             scm_cons (scm_list_3 (sym_set_x, 
+                                   sym_load_path, 
+                                   scm_list_3 (sym_cons,
+                                               scm_from_locale_string (argv[i]),
+                                               sym_load_path)),
+                       user_load_path);
+         else
+           scm_shell_usage (1, "missing argument to `-L' switch");
+       }         
+
       else if (! strcmp (argv[i], "-e")) /* entry point */
        {
          if (++i < argc)
-           entry_point = scm_c_read_string (argv[i]);
+           {
+             SCM port 
+               = scm_open_input_string (scm_from_locale_string (argv[i]));
+             SCM arg1 = scm_read (port);
+             SCM arg2 = scm_read (port);
+
+             /* Recognize syntax of certain versions of Guile 1.4 and
+                transform to (@ MODULE-NAME FUNC).
+              */
+             if (scm_is_false (scm_eof_object_p (arg2)))
+               entry_point = scm_list_3 (sym_at, arg1, arg2);
+             else if (scm_is_pair (arg1)
+                      && !(scm_is_eq (SCM_CAR (arg1), sym_at)
+                           || scm_is_eq (SCM_CAR (arg1), sym_atat))
+                      && all_symbols (arg1))
+               entry_point = scm_list_3 (sym_at, arg1, sym_main);
+             else
+               entry_point = arg1;
+           }
          else
            scm_shell_usage (1, "missing argument to `-e' switch");
        }
@@ -508,7 +564,7 @@ scm_compile_shell_switches (int argc, char **argv)
        {
          /* We put a dummy "load" expression, and let the -s put the
              filename in.  */
-         if (!SCM_NULLP (do_script))
+         if (!scm_is_null (do_script))
            scm_shell_usage (1, "the -ds switch may only be specified once");
          do_script = scm_cons (SCM_BOOL_F, SCM_EOL);
          tail = scm_cons (scm_cons (sym_load, do_script),
@@ -579,7 +635,7 @@ scm_compile_shell_switches (int argc, char **argv)
        {
          /* Print version number.  */
          printf ("Guile %s\n"
-                 "Copyright (c) 1995, 1996, 1997, 2000, 2001, 2002 Free Software Foundation\n"
+                 "Copyright (c) 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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"
@@ -597,7 +653,7 @@ scm_compile_shell_switches (int argc, char **argv)
     }
 
   /* Check to make sure the -ds got a -s. */
-  if (!SCM_NULLP (do_script))
+  if (!scm_is_null (do_script))
     scm_shell_usage (1, "the `-ds' switch requires the use of `-s' as well");
 
   /* Make any remaining arguments available to the
@@ -608,7 +664,7 @@ scm_compile_shell_switches (int argc, char **argv)
   scm_c_define ("use-emacs-interface", scm_from_bool (use_emacs_interface));
 
   /* Handle the `-e' switch, if it was specified.  */
-  if (!SCM_NULLP (entry_point))
+  if (!scm_is_null (entry_point))
     tail = scm_cons (scm_cons2 (entry_point,
                                scm_cons (sym_command_line, SCM_EOL),
                                SCM_EOL),
@@ -629,6 +685,13 @@ scm_compile_shell_switches (int argc, char **argv)
 
   /* After the following line, actions will be added to the front. */
   tail = scm_reverse_x (tail, SCM_UNDEFINED);
+
+  /* add the user-specified load path here, so it won't be in effect
+     during the loading of the user's customization file. */
+  if(!scm_is_null(user_load_path)) 
+    {
+      tail = scm_append_x( scm_cons2(user_load_path, tail, SCM_EOL) );
+    }
   
   /* If we didn't end with a -c or a -s and didn't supply a -q, load
      the user's customization file.  */