* configure.in: check for hstrerror.
[bpt/guile.git] / libguile / print.c
index 511a41a..15ceaf9 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
+/*     Copyright (C) 1995-1999 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
 #include "genio.h"
 #include "smob.h"
 #include "eval.h"
+#include "macros.h"
 #include "procprop.h"
 #include "read.h"
 #include "weaks.h"
 #include "unif.h"
 #include "alist.h"
 #include "struct.h"
+#include "objects.h"
 
 #include "print.h"
 \f
@@ -94,7 +96,22 @@ char *scm_isymnames[] =
   "#<undefined>",
   "#<eof>",
   "()",
-  "#<unspecified>"
+  "#<unspecified>",
+  "#@dispatch",
+  "#@slot-ref",
+  "#@slot-set!",
+
+  /* Multi-language support */
+  
+  "#@nil-cond",
+  "#@nil-ify",
+  "#@t-ify",
+  "#@0-cond",
+  "#@0-ify",
+  "#@1-ify",
+  "#@bind",
+  
+  "#@delay"
 };
 
 scm_option scm_print_opts[] = {
@@ -203,29 +220,6 @@ scm_make_print_state ()
   return answer ? answer : make_print_state ();
 }
 
-static char s_print_state_printer[] = "print-state-printer";
-static SCM
-print_state_printer (obj, port)
-     SCM obj;
-     SCM port;
-{
-  /* This function can be made visible by means of struct-ref, so
-     we need to make sure that it gets what it wants. */
-  SCM_ASSERT (SCM_NIMP (obj) && SCM_PRINT_STATE_P (obj),
-             obj,
-             SCM_ARG1,
-             s_print_state_printer);
-  SCM_ASSERT (scm_valid_oport_value_p (port),
-             port,
-             SCM_ARG2,
-             s_print_state_printer);
-  port = SCM_COERCE_OUTPORT (port);
-  scm_puts ("#<print-state ", port);
-  scm_intprint (obj, 16, port);
-  scm_putc ('>', port);
-  return SCM_UNSPECIFIED;
-}
-
 void
 scm_free_print_state (print_state)
      SCM print_state;
@@ -292,7 +286,8 @@ print_circref (port, pstate, ref)
 
 /* Print generally.  Handles both write and display according to PSTATE.
  */
-
+SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write);
+SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
 
 void 
 scm_iprin1 (exp, port, pstate)
@@ -356,7 +351,21 @@ taloop:
          if (SCM_CDR (SCM_CAR (exp) - 1L) == 0)
            {
              ENTER_NESTED_DATA (pstate, exp, circref);
-             scm_print_struct (exp, port, pstate);
+             if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS)
+               {
+                 SCM pwps, print = pstate->writingp ? g_write : g_display;
+                 if (!print)
+                   goto print_struct;
+                 SCM_NEWSMOB (pwps,
+                              scm_tc16_port_with_ps,
+                              scm_cons (port, pstate->handle));
+                 scm_call_generic_2 (print, exp, pwps);
+               }
+             else
+               {
+               print_struct:
+                 scm_print_struct (exp, port, pstate);
+               }
              EXIT_NESTED_DATA (pstate);
              break;
            }
@@ -378,7 +387,7 @@ taloop:
             macro closures as well. */
          if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE))
              || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE,
-                                               exp, port, pstate)));
+                                               exp, port, pstate)))
          {
            SCM name, code, env;
            if (SCM_TYP16 (exp) == scm_tc16_macro)
@@ -591,23 +600,55 @@ taloop:
        case scm_tc7_fvect:
        case scm_tc7_dvect:
        case scm_tc7_cvect:
-#ifdef LONGLONGS
+#ifdef HAVE_LONG_LONGS
        case scm_tc7_llvect:
 #endif
          scm_raprin1 (exp, port, pstate);
          break;
        case scm_tcs_subrs:
-         scm_puts ("#<primitive-procedure ", port);
+         scm_puts (SCM_SUBR_GENERIC (exp) && *SCM_SUBR_GENERIC (exp)
+                   ? "#<primitive-generic "
+                   : "#<primitive-procedure ",
+                   port);
          scm_puts (SCM_CHARS (SCM_SNAME (exp)), port);
          scm_putc ('>', port);
          break;
 #ifdef CCLO
        case scm_tc7_cclo:
-         scm_puts ("#<compiled-closure ", port);
-         scm_iprin1 (SCM_CCLO_SUBR (exp), port, pstate);
-         scm_putc ('>', port);
+         {
+           SCM proc = SCM_CCLO_SUBR (exp);
+           if (proc == scm_f_gsubr_apply)
+             {
+               /* Print gsubrs as primitives */
+               SCM name = scm_procedure_name (exp);
+               scm_puts ("#<primitive-procedure", port);
+               if (SCM_NFALSEP (name))
+                 {
+                   scm_putc (' ', port);
+                   scm_puts (SCM_CHARS (name), port);
+                 }
+             }
+           else
+             {
+               scm_puts ("#<compiled-closure ", port);
+               scm_iprin1 (proc, port, pstate);
+             }
+           scm_putc ('>', port);
+         }
          break;
 #endif
+       case scm_tc7_pws:
+         scm_puts ("#<procedure-with-setter", port);
+         {
+           SCM name = scm_procedure_name (exp);
+           if (SCM_NFALSEP (name))
+             {
+               scm_putc (' ', port);
+               scm_puts (SCM_ROCHARS (name), port);
+             }
+         }
+         scm_putc ('>', port);
+         break;
        case scm_tc7_contin:
          scm_puts ("#<continuation ", port);
          scm_intprint (SCM_LENGTH (exp), 10, port);
@@ -672,10 +713,10 @@ scm_prin1 (exp, port, writingp)
   /* If PORT is a print-state/port pair, use that.  Else create a new
      print-state. */
 
-  if (SCM_NIMP (port) && SCM_CONSP (port))
+  if (SCM_NIMP (port) && SCM_PORT_WITH_PS_P (port))
     {
-      pstate_scm = SCM_CDR (port);
-      port = SCM_CAR (port);
+      pstate_scm = SCM_PORT_WITH_PS_PS (port);
+      port = SCM_PORT_WITH_PS_PORT (port);
     }
   else
     {
@@ -865,14 +906,11 @@ scm_valid_oport_value_p   (SCM val)
 {
   return (SCM_NIMP (val)
          && (SCM_OPOUTPORTP (val)
-             || (SCM_CONSP (val)
-                 && SCM_NIMP (SCM_CAR (val))
-                 && SCM_OPOUTPORTP (SCM_CAR (val))
-                 && SCM_NIMP (SCM_CDR (val))
-                 && SCM_PRINT_STATE_P (SCM_CDR (val)))));
+             || (SCM_PORT_WITH_PS_P (val)
+                 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val)))));
 }
 
-SCM_PROC(s_write, "write", 1, 1, 0, scm_write);
+/* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
 
 SCM 
 scm_write (obj, port)
@@ -881,8 +919,8 @@ scm_write (obj, port)
 {
   if (SCM_UNBNDP (port))
     port = scm_cur_outp;
-  else
-    SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
+
+  SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
 
   scm_prin1 (obj, port, 1);
 #ifdef HAVE_PIPE
@@ -895,7 +933,7 @@ scm_write (obj, port)
 }
 
 
-SCM_PROC(s_display, "display", 1, 1, 0, scm_display);
+/* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
 
 SCM 
 scm_display (obj, port)
@@ -904,8 +942,8 @@ scm_display (obj, port)
 {
   if (SCM_UNBNDP (port))
     port = scm_cur_outp;
-  else
-    SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
+
+  SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
 
   scm_prin1 (obj, port, 0);
 #ifdef HAVE_PIPE
@@ -925,19 +963,10 @@ scm_newline (port)
 {
   if (SCM_UNBNDP (port))
     port = scm_cur_outp;
-  else
-    SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG1, s_newline);
+
+  SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG1, s_newline);
 
   scm_putc ('\n', SCM_COERCE_OUTPORT (port));
-#ifdef HAVE_PIPE
-# ifdef EPIPE
-  if (EPIPE == errno)
-    scm_close_port (port);
-  else
-# endif
-#endif
-  if (port == scm_cur_outp)
-    scm_fflush (port);
   return SCM_UNSPECIFIED;
 }
 
@@ -950,8 +979,8 @@ scm_write_char (chr, port)
 {
   if (SCM_UNBNDP (port))
     port = scm_cur_outp;
-  else
-    SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write_char);
+
+  SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write_char);
 
   SCM_ASSERT (SCM_ICHRP (chr), chr, SCM_ARG1, s_write_char);
   scm_putc ((int) SCM_ICHR (chr), SCM_COERCE_OUTPORT (port));
@@ -967,20 +996,64 @@ scm_write_char (chr, port)
 \f
 
 /* Call back to Scheme code to do the printing of special objects
-(like structs).  SCM_PRINTER_APPLY applies PROC to EXP and a pair
-containing PORT and PSTATE.  This pair can be used as the port for
-display/write etc to continue the current print chain.  The REVEALED
-field of PSTATE is set to true to indicate that the print state has
-escaped to Scheme and thus has to be freed by the GC. */
+ * (like structs).  SCM_PRINTER_APPLY applies PROC to EXP and a smob
+ * containing PORT and PSTATE.  This object can be used as the port for
+ * display/write etc to continue the current print chain.  The REVEALED
+ * field of PSTATE is set to true to indicate that the print state has
+ * escaped to Scheme and thus has to be freed by the GC.
+ */
+
+long scm_tc16_port_with_ps;
+
+/* Print exactly as the port itself would */
+
+static int
+print_port_with_ps (SCM obj, SCM port, scm_print_state *pstate)
+{
+  obj = SCM_PORT_WITH_PS_PORT (obj);
+  return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate);
+}
 
 SCM
 scm_printer_apply (proc, exp, port, pstate)
      SCM proc, exp, port;
      scm_print_state *pstate;
 {
+  SCM pwps;
   SCM pair = scm_cons (port, pstate->handle);
+  SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, pair);
   pstate->revealed = 1;
-  return scm_apply (proc, exp, scm_cons (pair, scm_listofnull));
+  return scm_apply (proc, exp, scm_cons (pwps, scm_listofnull));
+}
+
+SCM_PROC (s_port_with_print_state, "port-with-print-state", 2, 0, 0, scm_port_with_print_state);
+
+SCM
+scm_port_with_print_state (SCM port, SCM pstate)
+{
+  SCM pwps;
+  SCM_ASSERT (scm_valid_oport_value_p (port),
+             port, SCM_ARG1, s_port_with_print_state);
+  SCM_ASSERT (SCM_NIMP (pstate) && SCM_PRINT_STATE_P (pstate),
+             pstate, SCM_ARG2, s_port_with_print_state);
+  port = SCM_COERCE_OUTPORT (port);
+  SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, scm_cons (port, pstate));
+  return pwps;
+}
+
+SCM_PROC (s_get_print_state, "get-print-state", 1, 0, 0, scm_get_print_state);
+
+SCM
+scm_get_print_state (SCM port)
+{
+  if (SCM_NIMP (port))
+    {
+      if (SCM_PORT_WITH_PS_P (port))
+       return SCM_PORT_WITH_PS_PS (port);
+      if (SCM_OUTPORTP (port))
+       return SCM_BOOL_F;
+    }
+  return scm_wta (port, (char *) SCM_ARG1, s_get_print_state);
 }
 
 \f
@@ -988,21 +1061,23 @@ scm_printer_apply (proc, exp, port, pstate)
 void
 scm_init_print ()
 {
-  SCM vtable, layout, printer, type;
+  SCM vtable, layout, type;
   
   scm_init_opts (scm_print_options, scm_print_opts, SCM_N_PRINT_OPTIONS);
   vtable = scm_make_vtable_vtable (scm_make_struct_layout (scm_nullstr),
                                   SCM_INUM0,
                                   SCM_EOL);
   layout = scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT));
-  printer = scm_make_subr_opt (s_print_state_printer,
-                              scm_tc7_subr_2,
-                              (SCM (*) ()) print_state_printer,
-                              0 /* Don't bind the name. */);
-  type = scm_make_struct (vtable, SCM_INUM0, SCM_LIST2 (layout, printer));
+  type = scm_make_struct (vtable, SCM_INUM0, SCM_LIST1 (layout));
+  scm_set_struct_vtable_name_x (type, SCM_CAR (scm_intern0 ("print-state")));
   print_state_pool = scm_permanent_object (scm_cons (type, SCM_EOL));
 
   scm_print_state_vtable = type;
 
+  /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
+  scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
+  scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr);
+  scm_set_smob_print (scm_tc16_port_with_ps, print_port_with_ps);
+  
 #include "print.x"
 }