Changes from arch/CVS synchronization
[bpt/guile.git] / libguile / script.c
index f3d89dc..b024378 100644 (file)
@@ -1,46 +1,25 @@
-/* Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002 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)
- * any later version.
- * 
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * 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, 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.
- *
- * The exception is that, if you link the GUILE library with other files
- * to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the GUILE library code into it.
- *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
+/* 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
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This exception applies only to the code released by the
- * Free Software Foundation under the name GUILE.  If you copy
- * code from other Free Software Foundation releases into a copy of
- * GUILE, as the General Public License permits, the exception does
- * not apply to the code that you add in this way.  To avoid misleading
- * anyone as to the status of such modified files, you must delete
- * this exception notice from them.
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * 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.  */
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
 
 /* "script.c" argv tricks for `#!' scripts.
    Authors: Aubrey Jaffer and Jim Blandy */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
 
 #include <stdio.h>
 #include <errno.h>
@@ -141,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); */
@@ -153,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);
@@ -366,19 +348,23 @@ char *scm_usage_name = 0;
 void
 scm_shell_usage (int fatal, char *message)
 {
+  FILE  *fp = (fatal ? stderr : stdout);
+
   if (message)
-    fprintf (stderr, "%s\n", message);
+    fprintf (fp, "%s\n", message);
 
-  fprintf (stderr
+  fprintf (fp
            "Usage: %s OPTION ...\n"
            "Evaluate Scheme code, interactively or from a script.\n"
            "\n"
-           "  -s SCRIPT      load Scheme source code from FILE, and exit\n"
+           "  [-s] FILE      load Scheme source code from FILE, and exit\n"
            "  -c EXPR        evalute Scheme expression EXPR, and exit\n"
            "  --             stop scanning arguments; run interactively\n"
            "The above switches stop argument processing, and pass all\n"
            "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"
@@ -393,7 +379,11 @@ scm_shell_usage (int fatal, char *message)
           "                 which is a list of numbers like \"2,13,14\"\n"
            "  -h, --help     display this help and exit\n"
            "  -v, --version  display version information and exit\n"
-          "  \\              read arguments from following script lines\n",
+          "  \\              read arguments from following script lines\n"
+           "\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)
@@ -411,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.
@@ -424,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)
 {
@@ -434,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;
@@ -457,23 +465,23 @@ scm_compile_shell_switches (int argc, char **argv)
   
   for (i = 1; i < argc; i++)
     {
-      if (! strcmp (argv[i], "-s")) /* load script */
+      if ((! strcmp (argv[i], "-s")) || (argv[i][0] != '-')) /* load script */
        {
-         if (++i >= argc)
+         if ((argv[i][0] == '-') && (++i >= argc))
            scm_shell_usage (1, "missing argument to `-s' switch");
 
          /* 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_makfrom0str (argv[i]));
+             SCM_SETCAR (do_script, scm_from_locale_string (argv[i]));
              do_script = SCM_EOL;
            }
          else
            /* Construct an application of LOAD to the script name.  */
            tail = scm_cons (scm_cons2 (sym_load,
-                                       scm_makfrom0str (argv[i]),
+                                       scm_from_locale_string (argv[i]),
                                        SCM_EOL),
                               tail);
          argv0 = argv[i];
@@ -487,7 +495,7 @@ scm_compile_shell_switches (int argc, char **argv)
          if (++i >= argc)
            scm_shell_usage (1, "missing argument to `-c' switch");
          tail = scm_cons (scm_cons2 (sym_eval_string,
-                                     scm_makfrom0str (argv[i]),
+                                     scm_from_locale_string (argv[i]),
                                      SCM_EOL),
                           tail);
          i++;
@@ -505,17 +513,49 @@ scm_compile_shell_switches (int argc, char **argv)
        {
          if (++i < argc)
            tail = scm_cons (scm_cons2 (sym_load,
-                                       scm_makfrom0str (argv[i]),
+                                       scm_from_locale_string (argv[i]),
                                        SCM_EOL),
                             tail);
          else
            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_str2symbol (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");
        }
@@ -524,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),
@@ -561,7 +601,7 @@ scm_compile_shell_switches (int argc, char **argv)
              num = strtol (p, &end, 10);
              if (end - p > 0)
                {
-                 srfis = scm_cons (scm_long2num (num), srfis);
+                 srfis = scm_cons (scm_from_long (num), srfis);
                  if (*end)
                    {
                      if (*end == ',')
@@ -595,12 +635,12 @@ 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"
                  "There is no warranty, to the extent permitted by law.\n",
-                 SCM_STRING_CHARS (scm_version ()));
+                 scm_to_locale_string (scm_version ()));
          exit (0);
        }
 
@@ -613,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
@@ -621,10 +661,10 @@ scm_compile_shell_switches (int argc, char **argv)
   scm_set_program_arguments (argc ? argc - i : 0, argv + i, argv0);
   
   /* If the --emacs switch was set, now is when we process it.  */
-  scm_c_define ("use-emacs-interface", SCM_BOOL (use_emacs_interface));
+  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),
@@ -641,12 +681,17 @@ scm_compile_shell_switches (int argc, char **argv)
         quit.  */
       tail = scm_cons (scm_cons (sym_quit, SCM_EOL),
                       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);
+
+  /* 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.  */