* General: A lot of typo, texinfo markup and layout corrections.
[bpt/guile.git] / libguile / script.c
index 1b9de76..7c7c3f1 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
+/* Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001 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)
    gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
 
 #include <stdio.h>
+#include <errno.h>
 #include <ctype.h>
-#include "_scm.h"
-#include "gh.h"
-#include "load.h"
-#include "version.h"
 
-#include "script.h"
+#include "libguile/_scm.h"
+#include "libguile/gh.h"
+#include "libguile/load.h"
+#include "libguile/version.h"
+
+#include "libguile/validate.h"
+#include "libguile/script.h"
 
 #ifdef HAVE_STRING_H
 #include <string.h>
@@ -71,14 +74,14 @@ scm_cat_path (char *str1, const char *str2, long n)
     n = strlen (str2);
   if (str1)
     {
-      long len = strlen (str1);
-      str1 = (char *) realloc (str1, (scm_sizet) (len + n + 1));
+      size_t len = strlen (str1);
+      str1 = (char *) realloc (str1, (size_t) (len + n + 1));
       if (!str1)
        return 0L;
       strncat (str1 + len, str2, n);
       return str1;
     }
-  str1 = (char *) malloc ((scm_sizet) (n + 1));
+  str1 = (char *) malloc ((size_t) (n + 1));
   if (!str1)
     return 0L;
   str1[0] = 0;
@@ -168,6 +171,7 @@ scm_find_executable (const char *name)
 /* Read a \nnn-style escape.  We've just read the backslash.  */
 static int
 script_get_octal (FILE *f)
+#define FUNC_NAME "script_get_octal"
 {
   int i;
   int value = 0;
@@ -178,16 +182,17 @@ script_get_octal (FILE *f)
       if ('0' <= c && c <= '7')
        value = (value * 8) + (c - '0');
       else
-       scm_wta (SCM_UNDEFINED,
-                "malformed script: bad octal backslash escape",
-                "script argument parser");
+       SCM_MISC_ERROR ("malformed script: bad octal backslash escape",
+                       SCM_EOL);
     }
   return value;
 }
+#undef FUNC_NAME
 
 
 static int
 script_get_backslash (FILE *f)
+#define FUNC_NAME "script_get_backslash"
 {
   int c = getc (f);
 
@@ -211,28 +216,26 @@ script_get_backslash (FILE *f)
     case '4': case '5': case '6': case '7':
       ungetc (c, f);
       return script_get_octal (f);
-           
+
     case EOF:
-      scm_wta (SCM_UNDEFINED,
-              "malformed script: backslash followed by EOF",
-              "script argument parser");
+      SCM_MISC_ERROR ("malformed script: backslash followed by EOF", SCM_EOL);
       return 0; /* not reached? */
 
     default:
-      scm_wta (SCM_UNDEFINED,
-              "malformed script: bad backslash sequence",
-              "script argument parser");
+      SCM_MISC_ERROR ("malformed script: bad backslash sequence", SCM_EOL);
       return 0; /* not reached? */
     }
 }
+#undef FUNC_NAME
 
 
 static char *
 script_read_arg (FILE *f)
+#define FUNC_NAME "script_read_arg"
 {
-  int size = 7;
+  size_t size = 7;
   char *buf = malloc (size + 1);
-  int len = 0;
+  size_t len = 0;
 
   if (! buf)
     return 0;
@@ -275,13 +278,12 @@ script_read_arg (FILE *f)
 
        case '\t':
          free (buf);
-         scm_wta (SCM_UNDEFINED,
-                  "malformed script: TAB in meta-arguments",
-                  "argument parser");
+         SCM_MISC_ERROR ("malformed script: TAB in meta-arguments", SCM_EOL);
          return 0; /* not reached? */
        }
     }
 }
+#undef FUNC_NAME
 
 
 static int
@@ -379,8 +381,11 @@ 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"
+           "  --debug        start with debugging evaluator and backtraces\n"
           "  -q             inhibit loading of user init file\n"
            "  --emacs        enable Emacs protocol (experimental)\n"
+          "  --use-srfi=LS  load SRFI modules for the SRFIs in LS,\n"
+          "                 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",
@@ -399,6 +404,7 @@ SCM_SYMBOL (sym_begin, "begin");
 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");
 
 
 /* Given an array of command-line switches, return a Scheme expression
@@ -408,16 +414,7 @@ SCM_SYMBOL (sym_quit, "quit");
    probably agree.  I'd say I didn't feel comfortable doing that in
    the present system.  You'd say, well, fix the system so you are
    comfortable doing that.  I'd agree again.  *shrug*
-
-   We load the ice-9 system from here.  It might be nicer if the
-   libraries initialized from the inner_main function in guile.c (which
-   will be auto-generated eventually) could assume ice-9 were already
-   loaded.  Then again, it might be nice if ice-9 could assume that
-   certain libraries were already loaded.  The solution is to break up
-   ice-9 into modules which can be frozen and statically linked like any
-   other module.  Then all the modules can describe their dependencies in
-   the usual way, and the auto-generated inner_main will do the right
-   thing. */
+ */
 
 static char guile[] = "guile";
 
@@ -525,12 +522,57 @@ scm_compile_shell_switches (int argc, char **argv)
                           tail);
        }
 
+      else if (! strcmp (argv[i], "--debug")) /* debug eval + backtraces */
+       {
+         SCM_DEVAL_P = 1;
+         SCM_BACKTRACE_P = 1;
+         SCM_RECORD_POSITIONS_P = 1;
+         SCM_RESET_DEBUG_MODE;
+       }
+
       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 (! strncmp (argv[i], "--use-srfi=", 11)) /* load SRFIs */ 
+       {
+         SCM srfis = SCM_EOL;  /* List of requested SRFIs.  */
+         char * p = argv[i] + 11;
+         while (*p)
+           {
+             long num;
+             char * end;
+
+             num = strtol (p, &end, 10);
+             if (end - p > 0)
+               {
+                 srfis = scm_cons (scm_long2num (num), srfis);
+                 if (*end)
+                   {
+                     if (*end == ',')
+                       p = end + 1;
+                     else
+                       scm_shell_usage (1, "invalid SRFI specification");
+                   }
+                 else
+                   break;
+               }
+             else
+               scm_shell_usage (1, "invalid SRFI specification");
+           }
+         if (scm_ilength (srfis) <= 0)
+           scm_shell_usage (1, "invalid SRFI specification");
+         srfis = scm_reverse_x (srfis, SCM_UNDEFINED);
+         tail = scm_cons (scm_listify
+                          (sym_use_srfis,
+                           scm_listify (scm_sym_quote, 
+                                        srfis, SCM_UNDEFINED),
+                           SCM_UNDEFINED),
+                          tail);
+       }
+
       else if (! strcmp (argv[i], "-h")
               || ! strcmp (argv[i], "--help"))
        {
@@ -543,12 +585,12 @@ scm_compile_shell_switches (int argc, char **argv)
        {
          /* Print version number.  */
          printf ("Guile %s\n"
-                 "Copyright (c) 1995, 1996, 1997 Free Software Foundation\n"
+                 "Copyright (c) 1995, 1996, 1997, 2000 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_CHARS (scm_version ()));
+                 SCM_STRING_CHARS (scm_version ()));
          exit (0);
        }
 
@@ -569,10 +611,7 @@ 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 vcell = scm_sysintern0_no_module_lookup ("use-emacs-interface");
-    SCM_SETCDR (vcell, SCM_BOOL(use_emacs_interface));
-  }
+  scm_c_define ("use-emacs-interface", SCM_BOOL (use_emacs_interface));
 
   /* Handle the `-e' switch, if it was specified.  */
   if (!SCM_NULLP (entry_point))
@@ -635,14 +674,17 @@ scm_shell (int argc, char **argv)
       }
   }
 
-  exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc,argv))));
+  exit (scm_exit_status (scm_eval_x (scm_compile_shell_switches (argc, argv),
+                                    scm_current_module ())));
 }
 
 
 void
 scm_init_script ()
 {
-#include "script.x"
+#ifndef SCM_MAGIC_SNARFER
+#include "libguile/script.x"
+#endif
 }
 
 /*