Removed definition of GUILE_PTHREAD_COMPAT inside commentary
[bpt/guile.git] / libguile / backtrace.c
index 7e07a13..f3927d5 100644 (file)
@@ -1,5 +1,5 @@
 /* Printing of backtraces and error messages
- * Copyright (C) 1996,1997,1998 Free Software Foundation
+ * Copyright (C) 1996, 1997, 1998, 1999 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 author can be reached at djurfeldt@nada.kth.se
  * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
 
+/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
+   gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
+
+
 #include <stdio.h>
+#include <ctype.h>
+
 #include "_scm.h"
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
 #include "stacks.h"
 #include "srcprop.h"
-#include "genio.h"
 #include "struct.h"
 #include "strports.h"
 #include "throw.h"
 #include "fluids.h"
+#include "ports.h"
+#include "strings.h"
 
+#include "validate.h"
 #include "backtrace.h"
 
 /* {Error reporting and backtraces}
 
 SCM scm_the_last_stack_fluid;
 
-static void display_header SCM_P ((SCM source, SCM port));
 static void
-display_header (source, port)
-     SCM source;
-     SCM port;
+display_header (SCM source, SCM port)
 {
-  SCM fname = (SCM_NIMP (source) && SCM_MEMOIZEDP (source)
+  SCM fname = (SCM_MEMOIZEDP (source)
               ? scm_source_property (source, scm_sym_filename)
               : SCM_BOOL_F);
-  if (SCM_NIMP (fname) && SCM_STRINGP (fname))
+  if (SCM_STRINGP (fname))
     {
       scm_prin1 (fname, port, 0);
       scm_putc (':', port);
@@ -98,55 +108,22 @@ display_header (source, port)
 
 
 void
-scm_display_error_message (message, args, port)
-     SCM message;
-     SCM args;
-     SCM port;
+scm_display_error_message (SCM message, SCM args, SCM port)
 {
-  int writingp;
-  char *start;
-  char *p;
-  
-  if (SCM_IMP (message) || !SCM_ROSTRINGP (message) || SCM_IMP (args)
-      || !scm_list_p (args))
+  if (SCM_ROSTRINGP (message) && SCM_NFALSEP (scm_list_p (args)))
+    {
+      scm_simple_format (port, message, args);
+      scm_newline (port);
+    }
+  else
     {
       scm_prin1 (message, port, 0);
       scm_putc ('\n', port);
-      return;
     }
-
-  SCM_COERCE_SUBSTR (message);
-  start = SCM_ROCHARS (message);
-  for (p = start; *p != '\0'; ++p)
-    if (*p == '%')
-      {
-       if (SCM_IMP (args) || SCM_NCONSP (args))
-         continue;
-       
-       ++p;
-       if (*p == 's')
-         writingp = 0;
-       else if (*p == 'S')
-         writingp = 1;
-       else
-         continue;
-
-       scm_lfwrite (start, p - start - 1, port);
-       scm_prin1 (SCM_CAR (args), port, writingp);
-       args = SCM_CDR (args);
-       start = p + 1;
-      }
-  scm_lfwrite (start, p - start, port);
-  scm_putc ('\n', port);
 }
 
-static void display_expression SCM_P ((SCM frame, SCM pname, SCM source, SCM port));
 static void
-display_expression (frame, pname, source, port)
-     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);
@@ -154,16 +131,15 @@ display_expression (frame, pname, source, port)
   pstate->fancyp = 1;
   pstate->level = 2;
   pstate->length = 3;
-  if (SCM_NIMP (pname) && SCM_ROSTRINGP (pname))
+  if (SCM_ROSTRINGP (pname))
     {
-      if (SCM_NIMP (frame)
-         && SCM_FRAMEP (frame)
+      if (SCM_FRAMEP (frame)
          && SCM_FRAME_EVAL_ARGS_P (frame))
        scm_puts ("While evaluating arguments to ", port);
       else
        scm_puts ("In procedure ", port);
       scm_iprin1 (pname, port, pstate);
-      if (SCM_NIMP (source) && SCM_MEMOIZEDP (source))
+      if (SCM_MEMOIZEDP (source))
        {
          scm_puts (" in expression ", port);
          pstate->writingp = 1;
@@ -198,24 +174,22 @@ display_error_body (struct display_error_args *a)
   SCM prev_frame = SCM_BOOL_F;
 
   if (SCM_DEBUGGINGP
-      && SCM_NIMP (a->stack)
       && SCM_STACKP (a->stack)
       && SCM_STACK_LENGTH (a->stack) > 0)
     {
       current_frame = scm_stack_ref (a->stack, SCM_INUM0);
       source = SCM_FRAME_SOURCE (current_frame);
       prev_frame = SCM_FRAME_PREV (current_frame);
-      if (!(SCM_NIMP (source) && SCM_MEMOIZEDP (source))
+      if (!SCM_MEMOIZEDP (source)
          && prev_frame != SCM_BOOL_F)
        source = SCM_FRAME_SOURCE (prev_frame);
       if (SCM_FRAME_PROC_P (current_frame)
          && scm_procedure_p (SCM_FRAME_PROC (current_frame)) == SCM_BOOL_T)
        pname = scm_procedure_name (SCM_FRAME_PROC (current_frame));
     }
-  if (!(SCM_NIMP (pname) && SCM_ROSTRINGP (pname)))
+  if (!SCM_ROSTRINGP (pname))
     pname = a->subr;
-  if ((SCM_NIMP (pname) && SCM_ROSTRINGP (pname))
-      || (SCM_NIMP (source) && SCM_MEMOIZEDP (source)))
+  if (SCM_ROSTRINGP (pname) || SCM_MEMOIZEDP (source))
     {
       display_header (source, a->port);
       display_expression (current_frame, pname, source, a->port);
@@ -234,7 +208,7 @@ struct display_error_handler_data {
    Note that it is very important that this handler *doesn't* try to
    print more than the error tag, since the error very probably is
    caused by an erroneous print call-back routine.  If we would
-   tru to print all objects, we would enter an infinite loop. */
+   try to print all objects, we would enter an infinite loop. */
 static SCM
 display_error_handler (struct display_error_handler_data *data,
                       SCM tag, SCM args)
@@ -248,15 +222,10 @@ display_error_handler (struct display_error_handler_data *data,
   return SCM_UNSPECIFIED;
 }
 
-SCM_PROC(s_display_error, "display-error", 6, 0, 0, scm_display_error);
-SCM
-scm_display_error (stack, port, subr, message, args, rest)
-     SCM stack;
-     SCM port;
-     SCM subr;
-     SCM message;
-     SCM args;
-     SCM rest;
+SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
+           (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest),
+"")
+#define FUNC_NAME s_scm_display_error
 {
   struct display_error_args a;
   struct display_error_handler_data data;
@@ -273,47 +242,112 @@ scm_display_error (stack, port, subr, message, args, rest)
                      (scm_catch_handler_t) display_error_handler, &data);
   return SCM_UNSPECIFIED;
 }
+#undef FUNC_NAME
+
+typedef struct {
+  int level;
+  int length;
+} print_params_t;
+
+static int n_print_params = 9;
+static print_params_t default_print_params[] = {
+  { 4, 9 }, { 4, 3 },
+  { 3, 4 }, { 3, 3 },
+  { 2, 4 }, { 2, 3 },
+  { 1, 4 }, { 1, 3 }, { 1, 2 }
+};
+static print_params_t *print_params = default_print_params;
+
+#ifdef GUILE_DEBUG
+SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
+           (SCM params),
+"")
+#define FUNC_NAME s_scm_set_print_params_x
+{
+  int i;
+  int n;
+  SCM ls;
+  print_params_t *new_params;
+
+  SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
+  for (ls = params; SCM_NNULLP (ls); ls = SCM_CDR (ls))
+    SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
+               && SCM_INUMP (SCM_CAAR (ls))
+               && SCM_INUM (SCM_CAAR (ls)) >= 0
+               && SCM_INUMP (SCM_CADAR (ls))
+               && SCM_INUM (SCM_CADAR (ls)) >= 0,
+               params,
+               SCM_ARG2,
+               s_scm_set_print_params_x);
+  new_params = scm_must_malloc (n * sizeof (print_params_t),
+                               FUNC_NAME);
+  if (print_params != default_print_params)
+    scm_must_free (print_params);
+  print_params = new_params;
+  for (i = 0; i < n; ++i)
+    {
+      print_params[i].level = SCM_INUM (SCM_CAAR (params));
+      print_params[i].length = SCM_INUM (SCM_CADAR (params));
+      params = SCM_CDR (params);
+    }
+  n_print_params = n;
+  return SCM_UNSPECIFIED;
+}
+#undef FUNC_NAME
+#endif
 
-static void indent SCM_P ((int n, SCM port));
 static void
-indent (n, port)
-     int n;
-     SCM port;
+indent (int n, SCM port)
 {
   int i;
   for (i = 0; i < n; ++i)
     scm_putc (' ', port);
 }
 
-static void display_frame_expr SCM_P ((char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate));
 static void
-display_frame_expr (hdr, exp, tlr, indentation, sport, port, pstate)
-     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)
 {
-  if (SCM_NIMP (exp) && SCM_CONSP (exp))
+  SCM string;
+  int i = 0, n;
+  scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport);
+  do
     {
-      scm_iprlist (hdr, exp, tlr[0], port, pstate);
-      scm_puts (&tlr[1], port);
+      pstate->length = print_params[i].length;
+      ptob->seek (sport, 0, SEEK_SET);
+      if (SCM_CONSP (exp))
+       {
+         pstate->level = print_params[i].level - 1;
+         scm_iprlist (hdr, exp, tlr[0], sport, pstate);
+         scm_puts (&tlr[1], sport);
+       }
+      else
+       {
+         pstate->level = print_params[i].level;
+         scm_iprin1 (exp, sport, pstate);
+       }
+      ptob->flush (sport);
+      n = ptob->seek (sport, 0, SEEK_CUR);
+      ++i;
     }
-  else
-    scm_iprin1 (exp, port, pstate);
-  scm_putc ('\n', port);
+  while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
+  ptob->truncate (sport, n);
+  string = scm_strport_to_string (sport);
+  /* Remove control characters */
+  for (i = 0; i < n; ++i)
+    if (iscntrl (SCM_CHARS (string)[i]))
+      SCM_CHARS (string)[i] = ' ';
+  /* Truncate */
+  if (indentation + n > SCM_BACKTRACE_WIDTH)
+    {
+      n = SCM_BACKTRACE_WIDTH - indentation;
+      SCM_CHARS (string)[n - 1] = '$';
+    }
+      
+  scm_lfwrite (SCM_CHARS (string), n, port);
 }
 
-static void display_application SCM_P ((SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate));
 static void
-display_application (frame, indentation, sport, port, pstate)
-     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_NFALSEP (scm_procedure_p (proc))
@@ -329,43 +363,50 @@ display_application (frame, indentation, sport, port, pstate)
                      pstate);
 }
 
-SCM_PROC(s_display_application, "display-application", 1, 1, 0, scm_display_application);
-
-SCM
-scm_display_application (SCM frame, SCM port)
+SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0, 
+           (SCM frame, SCM port, SCM indent),
+"")
+#define FUNC_NAME s_scm_display_application
 {
+  SCM_VALIDATE_FRAME (1,frame);
   if (SCM_UNBNDP (port))
     port = scm_cur_outp;
+  else
+    SCM_VALIDATE_OPOUTPORT (2,port);
+  if (SCM_UNBNDP (indent))
+    indent = SCM_INUM0;
+  else
+    SCM_VALIDATE_INUM (3,indent);
+  
   if (SCM_FRAME_PROC_P (frame))
     /* Display an application. */
     {
-      SCM print_state;
+      SCM sport, print_state;
       scm_print_state *pstate;
       
+      /* Create a string port used for adaptation of printing parameters. */
+      sport = scm_mkstrport (SCM_INUM0,
+                            scm_make_string (SCM_MAKINUM (240),
+                                             SCM_UNDEFINED),
+                            SCM_OPN | SCM_WRTNG,
+                            FUNC_NAME);
+
       /* Create a print state for printing of frames. */
       print_state = scm_make_print_state ();
       pstate = SCM_PRINT_STATE (print_state);
       pstate->writingp = 1;
       pstate->fancyp = 1;
-      pstate->level = 2;
-      pstate->length = 9;
       
-      display_application (frame, 0, SCM_BOOL_F, port, pstate); /*fixme*/
+      display_application (frame, SCM_INUM (indent), sport, port, pstate);
       return SCM_BOOL_T;
     }
   else
     return SCM_BOOL_F;
 }
+#undef FUNC_NAME
 
-static void display_frame SCM_P ((SCM frame, int nfield, int indentation, SCM sport, SCM port, scm_print_state *pstate));
 static void
-display_frame (frame, nfield, indentation, sport, port, pstate)
-     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;
 
@@ -399,20 +440,21 @@ display_frame (frame, nfield, indentation, sport, port, pstate)
     /* Display a special form. */
     {
       SCM source = SCM_FRAME_SOURCE (frame);
-      SCM copy = (SCM_NIMP (source) && SCM_CONSP (source) 
+      SCM copy = (SCM_CONSP (source) 
                  ? scm_source_property (source, scm_sym_copy)
                  : SCM_BOOL_F);
-      SCM umcopy = (SCM_NIMP (source) && SCM_MEMOIZEDP (source)
+      SCM umcopy = (SCM_MEMOIZEDP (source)
                    ? scm_unmemoize (source)
                    : SCM_BOOL_F);
       display_frame_expr ("(",
-                         SCM_NIMP (copy) && SCM_CONSP (copy) ? copy : umcopy,
+                         SCM_CONSP (copy) ? copy : umcopy,
                          ")",
                          nfield + 1 + indentation,
                          sport,
                          port,
                          pstate);
     }
+  scm_putc ('\n', port);
 
   /* Announce missing frames? */
   if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
@@ -429,10 +471,9 @@ struct display_backtrace_args {
   SCM depth;
 };
 
-SCM_PROC(s_display_backtrace, "display-backtrace", 2, 2, 0, scm_display_backtrace);
-
 static SCM
-display_backtrace_body (struct display_backtrace_args *a)
+display_backtrace_body(struct display_backtrace_args *a)
+#define FUNC_NAME "display_backtrace_body"
 {
   int n_frames, beg, end, n, i, j;
   int nfield, indent_p, indentation;
@@ -442,11 +483,11 @@ display_backtrace_body (struct display_backtrace_args *a)
   a->port = SCM_COERCE_OUTPORT (a->port);
 
   /* Argument checking and extraction. */
-  SCM_ASSERT (SCM_NIMP (a->stack) && SCM_STACKP (a->stack),
+  SCM_ASSERT (SCM_STACKP (a->stack),
              a->stack,
              SCM_ARG1,
              s_display_backtrace);
-  SCM_ASSERT (SCM_NIMP (a->port) && SCM_OPOUTPORTP (a->port),
+  SCM_ASSERT (SCM_OPOUTPORTP (a->port),
              a->port,
              SCM_ARG2,
              s_display_backtrace);
@@ -485,15 +526,13 @@ display_backtrace_body (struct display_backtrace_args *a)
   sport = scm_mkstrport (SCM_INUM0,
                         scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED),
                         SCM_OPN | SCM_WRTNG,
-                        s_display_backtrace);
+                        FUNC_NAME);
 
   /* Create a print state for printing of frames. */
   print_state = scm_make_print_state ();
   pstate = SCM_PRINT_STATE (print_state);
   pstate->writingp = 1;
   pstate->fancyp = 1;
-  pstate->level = 2;
-  pstate->length = 3;
 
   /* First find out if it's reasonable to do indentation. */
   if (SCM_BACKWARDS_P)
@@ -522,8 +561,6 @@ display_backtrace_body (struct display_backtrace_args *a)
   for (i = 0; j > 0; ++i) j /= 10;
   nfield = i ? i : 1;
   
-  scm_puts ("Backtrace:\n", a->port);
-
   /* Print frames. */
   frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
   indentation = 1;
@@ -538,13 +575,12 @@ display_backtrace_body (struct display_backtrace_args *a)
 
   return SCM_UNSPECIFIED;
 }
+#undef FUNC_NAME
 
-SCM
-scm_display_backtrace (stack, port, first, depth)
-     SCM stack;
-     SCM port;
-     SCM first;
-     SCM depth;
+SCM_DEFINE (scm_display_backtrace, "display-backtrace", 2, 2, 0, 
+           (SCM stack, SCM port, SCM first, SCM depth),
+"")
+#define FUNC_NAME s_scm_display_backtrace
 {
   struct display_backtrace_args a;
   struct display_error_handler_data data;
@@ -559,17 +595,20 @@ scm_display_backtrace (stack, port, first, depth)
                      (scm_catch_handler_t) display_error_handler, &data);
   return SCM_UNSPECIFIED;
 }
+#undef FUNC_NAME
 
 SCM_VCELL (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
 
-SCM_PROC(s_backtrace, "backtrace", 0, 0, 0, scm_backtrace);
-SCM
-scm_backtrace ()
+SCM_DEFINE (scm_backtrace, "backtrace", 0, 0, 0, 
+           (),
+"")
+#define FUNC_NAME s_scm_backtrace
 {
   SCM the_last_stack = scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid));
   if (SCM_NFALSEP (the_last_stack))
     {
       scm_newline (scm_cur_outp);
+      scm_puts ("Backtrace:\n", scm_cur_outp);
       scm_display_backtrace (the_last_stack,
                             scm_cur_outp,
                             SCM_UNDEFINED,
@@ -591,6 +630,7 @@ scm_backtrace ()
     }
   return SCM_UNSPECIFIED;
 }
+#undef FUNC_NAME
 
 \f
 
@@ -602,3 +642,9 @@ scm_init_backtrace ()
 
 #include "backtrace.x"
 }
+
+/*
+  Local Variables:
+  c-file-style: "gnu"
+  End:
+*/