*** empty log message ***
[bpt/guile.git] / libguile / backtrace.c
index 39d7563..ab96ba5 100644 (file)
@@ -1,49 +1,24 @@
 /* Printing of backtraces and error messages
- * Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation
+ * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003 Free Software Foundation
  *
- * 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 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 program is distributed in the hope that it will be useful,
+ * 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 General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser 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.
- *
- * 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.
- *
- * 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.
- *
- * The author can be reached at djurfeldt@nada.kth.se
- * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
-
+ * 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
+ */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
 
 #include <stdio.h>
 #include <ctype.h>
 #include "libguile/fluids.h"
 #include "libguile/ports.h"
 #include "libguile/strings.h"
+#include "libguile/dynwind.h"
 
 #include "libguile/validate.h"
+#include "libguile/lang.h"
 #include "libguile/backtrace.h"
 #include "libguile/filesys.h"
 
 /* {Error reporting and backtraces}
- * (A first approximation.)
  *
  * Note that these functions shouldn't generate errors themselves.
  */
 
-#ifndef SCM_RECKLESS
+/* Print parameters for error messages. */
+
+#define DISPLAY_ERROR_MESSAGE_MAX_LEVEL   7
+#define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
+
+/* Print parameters for failing expressions in error messages.
+ * (See also `print_params' below for backtrace print parameters.)
+ */
+
+#define DISPLAY_EXPRESSION_MAX_LEVEL      2
+#define DISPLAY_EXPRESSION_MAX_LENGTH     3
+
 #undef SCM_ASSERT
 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
        if (!(_cond)) \
           return SCM_BOOL_F;
-#endif
 
 SCM scm_the_last_stack_fluid_var;
 
@@ -117,30 +103,79 @@ display_header (SCM source, SCM port)
 }
 
 
+struct display_error_message_data {
+  SCM message;
+  SCM args;
+  SCM port;
+  scm_print_state *pstate;
+  int old_fancyp;
+  int old_level;
+  int old_length;
+};
+
+static SCM
+display_error_message (struct display_error_message_data *d)
+{
+  if (SCM_STRINGP (d->message) && !SCM_FALSEP (scm_list_p (d->args)))
+    scm_simple_format (d->port, d->message, d->args);
+  else
+    scm_display (d->message, d->port);
+  scm_newline (d->port);
+  return SCM_UNSPECIFIED;
+}
+
+static void
+before_display_error_message (struct display_error_message_data *d)
+{
+  scm_print_state *pstate = d->pstate;
+  d->old_fancyp = pstate->fancyp;
+  d->old_level  = pstate->level;
+  d->old_length = pstate->length;
+  pstate->fancyp = 1;
+  pstate->level  = DISPLAY_ERROR_MESSAGE_MAX_LEVEL;
+  pstate->length = DISPLAY_ERROR_MESSAGE_MAX_LENGTH;
+}
+
+static void
+after_display_error_message (struct display_error_message_data *d)
+{
+  scm_print_state *pstate = d->pstate;
+  pstate->fancyp = d->old_fancyp;
+  pstate->level  = d->old_level;
+  pstate->length = d->old_length;
+}
+
 void
 scm_display_error_message (SCM message, SCM args, SCM port)
 {
-  if (SCM_STRINGP (message) && !SCM_FALSEP (scm_list_p (args)))
-    {
-      scm_simple_format (port, message, args);
-      scm_newline (port);
-    }
-  else
-    {
-      scm_display (message, port);
-      scm_newline (port);
-    }
+  struct display_error_message_data d;
+  SCM print_state;
+  scm_print_state *pstate;
+
+  port = scm_i_port_with_print_state (port, SCM_UNDEFINED);
+  print_state = SCM_PORT_WITH_PS_PS (port);
+  pstate = SCM_PRINT_STATE (print_state);
+  
+  d.message = message;
+  d.args = args;
+  d.port = port;
+  d.pstate = pstate;
+  scm_internal_dynamic_wind ((scm_t_guard) before_display_error_message,
+                            (scm_t_inner) display_error_message,
+                            (scm_t_guard) after_display_error_message,
+                            &d,
+                            &d);
 }
 
 static void
-display_expression (SCM frame,SCM pname,SCM source,SCM port)
+display_expression (SCM frame, SCM pname, SCM source, SCM port)
 {
   SCM print_state = scm_make_print_state ();
   scm_print_state *pstate = SCM_PRINT_STATE (print_state);
   pstate->writingp = 0;
   pstate->fancyp = 1;
-  pstate->level = 2;
-  pstate->length = 3;
+  pstate->level  = DISPLAY_EXPRESSION_MAX_LEVEL;
+  pstate->length = DISPLAY_EXPRESSION_MAX_LENGTH;
   if (SCM_SYMBOLP (pname) || SCM_STRINGP (pname))
     {
       if (SCM_FRAMEP (frame)
@@ -183,7 +218,7 @@ display_error_body (struct display_error_args *a)
   SCM prev_frame = SCM_BOOL_F;
   SCM pname = a->subr;
 
-  if (SCM_DEBUGGINGP
+  if (scm_debug_mode_p
       && SCM_STACKP (a->stack)
       && SCM_STACK_LENGTH (a->stack) > 0)
     {
@@ -258,7 +293,7 @@ SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
            (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest),
            "Display an error message to the output port @var{port}.\n"
            "@var{stack} is the saved stack for the error, @var{subr} is\n"
-           "the name of the procedure in which the error occured and\n"
+           "the name of the procedure in which the error occurred and\n"
            "@var{message} is the actual error message, which may contain\n"
            "formatting instructions. These will format the arguments in\n"
            "the list @var{args} accordingly.  @var{rest} is currently\n"
@@ -302,7 +337,7 @@ SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
   print_params_t *new_params;
 
   SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
-  for (ls = params; !SCM_NULLP (ls); ls = SCM_CDR (ls))
+  for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls))
     SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
                && SCM_INUMP (SCM_CAAR (ls))
                && SCM_INUM (SCM_CAAR (ls)) >= 0
@@ -311,10 +346,9 @@ SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
                params,
                SCM_ARG2,
                s_scm_set_print_params_x);
-  new_params = scm_must_malloc (n * sizeof (print_params_t),
-                               FUNC_NAME);
+  new_params = scm_malloc (n * sizeof (print_params_t));
   if (print_params != default_print_params)
-    scm_must_free (print_params);
+    free (print_params);
   print_params = new_params;
   for (i = 0; i < n; ++i)
     {
@@ -337,7 +371,7 @@ indent (int n, SCM port)
 }
 
 static void
-display_frame_expr (char *hdr,SCM exp,char *tlr,int indentation,SCM sport,SCM port,scm_print_state *pstate)
+display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate)
 {
   SCM string;
   int i = 0, n;
@@ -379,7 +413,7 @@ display_frame_expr (char *hdr,SCM exp,char *tlr,int indentation,SCM sport,SCM po
 }
 
 static void
-display_application (SCM frame,int indentation,SCM sport,SCM port,scm_print_state *pstate)
+display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate)
 {
   SCM proc = SCM_FRAME_PROC (frame);
   SCM name = (!SCM_FALSEP (scm_procedure_p (proc))
@@ -402,15 +436,15 @@ SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
            "output.")
 #define FUNC_NAME s_scm_display_application
 {
-  SCM_VALIDATE_FRAME (1,frame);
+  SCM_VALIDATE_FRAME (1, frame);
   if (SCM_UNBNDP (port))
     port = scm_cur_outp;
   else
-    SCM_VALIDATE_OPOUTPORT (2,port);
+    SCM_VALIDATE_OPOUTPORT (2, port);
   if (SCM_UNBNDP (indent))
     indent = SCM_INUM0;
   else
-    SCM_VALIDATE_INUM (3,indent);
+    SCM_VALIDATE_INUM (3, indent);
   
   if (SCM_FRAME_PROC_P (frame))
     /* Display an application. */
@@ -487,7 +521,7 @@ display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
 
   display_backtrace_get_file_line (frame, &file, &line);
 
-  if (SCM_EQ_P (SCM_SHOW_FILE_NAME, sym_base))
+  if (SCM_EQ_P (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
     {
       if (SCM_FALSEP (file))
        {
@@ -499,8 +533,12 @@ display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
       else
        {
          pstate -> writingp = 0;
+#ifdef HAVE_POSIX
          scm_iprin1 (SCM_STRINGP (file) ? scm_basename (file, SCM_UNDEFINED) : file,
                      port, pstate);
+#else
+         scm_iprin1 (file, port, pstate);
+#endif
          pstate -> writingp = 1;
        }
 
@@ -522,7 +560,7 @@ display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
 }
 
 static void
-display_frame (SCM frame,int nfield,int indentation,SCM sport,SCM port,scm_print_state *pstate)
+display_frame (SCM frame, int nfield, int indentation, SCM sport, SCM port, scm_print_state *pstate)
 {
   int n, i, j;
 
@@ -534,7 +572,7 @@ display_frame (SCM frame,int nfield,int indentation,SCM sport,SCM port,scm_print
     }
 
   /* display file name and line number */
-  if (!SCM_FALSEP (SCM_SHOW_FILE_NAME))
+  if (!SCM_FALSEP (SCM_PACK (SCM_SHOW_FILE_NAME)))
     display_backtrace_file_and_line (frame, port, pstate);
 
   /* Check size of frame number. */
@@ -684,7 +722,7 @@ display_backtrace_body (struct display_backtrace_args *a)
   last_file = SCM_UNDEFINED;
   for (i = 0; i < n; ++i)
     {
-      if (!SCM_EQ_P (SCM_SHOW_FILE_NAME, sym_base))
+      if (!SCM_EQ_P (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
        display_backtrace_file (frame, &last_file, a->port, pstate);
 
       display_frame (frame, nfield, indentation, sport, a->port, pstate);
@@ -769,9 +807,7 @@ scm_init_backtrace ()
   SCM f = scm_make_fluid ();
   scm_the_last_stack_fluid_var = scm_c_define ("the-last-stack", f);
 
-#ifndef SCM_MAGIC_SNARFER
 #include "libguile/backtrace.x"
-#endif
 }
 
 /*