1 /* Copyright (C) 1995-1999 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48 #include "libguile/_scm.h"
49 #include "libguile/chars.h"
50 #include "libguile/smob.h"
51 #include "libguile/eval.h"
52 #include "libguile/macros.h"
53 #include "libguile/procprop.h"
54 #include "libguile/read.h"
55 #include "libguile/weaks.h"
56 #include "libguile/unif.h"
57 #include "libguile/alist.h"
58 #include "libguile/struct.h"
59 #include "libguile/objects.h"
60 #include "libguile/ports.h"
61 #include "libguile/root.h"
62 #include "libguile/strings.h"
63 #include "libguile/strports.h"
64 #include "libguile/vectors.h"
66 #include "libguile/validate.h"
67 #include "libguile/print.h"
70 /* {Names of immediate symbols}
72 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
75 char *scm_isymnames
[] =
77 /* This table must agree with the declarations */
93 "#@literal-variable-ref",
94 "#@literal-variable-set!",
97 "#@call-with-current-continuation",
99 /* user visible ISYMS */
113 /* Multi-language support */
128 scm_option scm_print_opts
[] = {
129 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK(SCM_BOOL_F
),
130 "Hook for printing closures." },
131 { SCM_OPTION_BOOLEAN
, "source", 0,
132 "Print closures with source." }
135 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
138 #define FUNC_NAME s_scm_print_options
140 SCM ans
= scm_options (setting
,
149 /* {Printing of Scheme Objects}
152 /* Detection of circular references.
154 * Due to other constraints in the implementation, this code has bad
155 * time complexity (O (depth * N)), The printer code can be
156 * rewritten to be O(N).
158 #define PUSH_REF(pstate, obj) \
160 pstate->ref_stack[pstate->top++] = (obj); \
161 if (pstate->top == pstate->ceiling) \
162 grow_ref_stack (pstate); \
165 #define ENTER_NESTED_DATA(pstate, obj, label) \
167 register unsigned long i; \
168 for (i = 0; i < pstate->top; ++i) \
169 if (SCM_EQ_P (pstate->ref_stack[i], (obj))) \
171 if (pstate->fancyp) \
173 if (pstate->top - pstate->list_offset >= pstate->level) \
175 scm_putc ('#', port); \
179 PUSH_REF(pstate, obj); \
182 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
184 SCM scm_print_state_vtable
;
186 static SCM print_state_pool
;
188 #ifdef GUILE_DEBUG /* Used for debugging purposes */
190 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
192 "Return the current-pstate--the `cadr' of the print_state_pool.\n"
193 "`current-pstate' is only included in GUILE_DEBUG builds.")
194 #define FUNC_NAME s_scm_current_pstate
196 return SCM_CADR (print_state_pool
);
202 #define PSTATE_SIZE 50L
205 make_print_state (void)
207 SCM print_state
= scm_make_struct (SCM_CAR (print_state_pool
), /* pstate type */
210 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
211 pstate
->ref_vect
= scm_make_vector (SCM_MAKINUM (PSTATE_SIZE
),
213 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
214 pstate
->ceiling
= SCM_LENGTH (pstate
->ref_vect
);
219 scm_make_print_state ()
221 SCM answer
= SCM_BOOL_F
;
223 /* First try to allocate a print state from the pool */
225 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
227 answer
= SCM_CADR (print_state_pool
);
228 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
232 return SCM_FALSEP (answer
) ? make_print_state () : answer
;
236 scm_free_print_state (SCM print_state
)
239 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
240 /* Cleanup before returning print state to pool.
241 * It is better to do it here. Doing it in scm_prin1
242 * would cost more since that function is called much more
246 pstate
->revealed
= 0;
247 SCM_NEWCELL (handle
);
249 SCM_SETCAR (handle
, print_state
);
250 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
251 SCM_SETCDR (print_state_pool
, handle
);
256 grow_ref_stack (scm_print_state
*pstate
)
258 int new_size
= 2 * pstate
->ceiling
;
259 scm_vector_set_length_x (pstate
->ref_vect
, SCM_MAKINUM (new_size
));
260 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
261 pstate
->ceiling
= new_size
;
266 print_circref (SCM port
,scm_print_state
*pstate
,SCM ref
)
269 int self
= pstate
->top
- 1;
271 if (SCM_CONSP (pstate
->ref_stack
[i
]))
275 if (SCM_NCONSP (pstate
->ref_stack
[i
- 1])
276 || !SCM_EQ_P (SCM_CDR (pstate
->ref_stack
[i
- 1]),
277 pstate
->ref_stack
[i
]))
283 for (i
= pstate
->top
- 1; 1; --i
)
284 if (SCM_EQ_P (pstate
->ref_stack
[i
], ref
))
286 scm_putc ('#', port
);
287 scm_intprint (i
- self
, 10, port
);
288 scm_putc ('#', port
);
291 /* Print generally. Handles both write and display according to PSTATE.
293 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
294 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
297 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
300 switch (SCM_ITAG3 (exp
))
304 scm_intprint (SCM_INUM (exp
), 10, port
);
312 if (SCM_WRITINGP (pstate
))
314 scm_puts ("#\\", port
);
315 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
316 scm_puts (scm_charnames
[i
], port
);
317 else if (i
< 0 || i
> '\177')
318 scm_intprint (i
, 8, port
);
325 else if (SCM_IFLAGP (exp
)
326 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
327 scm_puts (SCM_ISYMCHARS (exp
), port
);
328 else if (SCM_ILOCP (exp
))
330 scm_puts ("#@", port
);
331 scm_intprint (SCM_IFRAME (exp
), 10, port
);
332 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
333 scm_intprint (SCM_IDIST (exp
), 10, port
);
340 scm_puts ("#@", port
);
341 exp
= SCM_GLOC_SYM (exp
);
345 scm_ipruk ("immediate", exp
, port
);
348 switch (SCM_TYP7 (exp
))
350 case scm_tcs_cons_gloc
:
352 if (SCM_STRUCT_VTABLE_DATA (exp
) [scm_vtable_index_vcell
] == 0)
354 ENTER_NESTED_DATA (pstate
, exp
, circref
);
355 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
357 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
361 scm_tc16_port_with_ps
,
362 SCM_UNPACK (scm_cons (port
, pstate
->handle
)));
363 scm_call_generic_2 (print
, exp
, pwps
);
368 scm_print_struct (exp
, port
, pstate
);
370 EXIT_NESTED_DATA (pstate
);
374 case scm_tcs_cons_imcar
:
375 case scm_tcs_cons_nimcar
:
376 ENTER_NESTED_DATA (pstate
, exp
, circref
);
377 scm_iprlist ("(", exp
, ')', port
, pstate
);
378 EXIT_NESTED_DATA (pstate
);
381 print_circref (port
, pstate
, exp
);
384 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
386 case scm_tcs_closures
:
387 /* The user supplied print closure procedure must handle
388 macro closures as well. */
389 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
390 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
394 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
396 /* Printing a macro. */
398 name
= scm_macro_name (exp
);
399 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
401 code
= env
= SCM_UNDEFINED
;
402 scm_puts ("#<primitive-", port
);
406 code
= SCM_CODE (SCM_CDR (exp
));
407 env
= SCM_ENV (SCM_CDR (exp
));
408 scm_puts ("#<", port
);
410 if (SCM_CELL_WORD_0 (exp
) & (3L << 16))
411 scm_puts ("macro", port
);
413 scm_puts ("syntax", port
);
414 if (SCM_CELL_WORD_0 (exp
) & (2L << 16))
415 scm_putc ('!', port
);
419 /* Printing a closure. */
420 name
= scm_procedure_name (exp
);
421 code
= SCM_CODE (exp
);
423 scm_puts ("#<procedure", port
);
425 if (SCM_ROSTRINGP (name
))
427 scm_putc (' ', port
);
428 scm_puts (SCM_ROCHARS (name
), port
);
430 if (!SCM_UNBNDP (code
))
432 if (SCM_PRINT_SOURCE_P
)
434 code
= scm_unmemocopy (code
,
435 SCM_EXTEND_ENV (SCM_CAR (code
),
438 ENTER_NESTED_DATA (pstate
, exp
, circref
);
439 scm_iprlist (" ", code
, '>', port
, pstate
);
440 EXIT_NESTED_DATA (pstate
);
444 if (SCM_TYP16 (exp
) != scm_tc16_macro
)
446 scm_putc (' ', port
);
447 scm_iprin1 (SCM_CAR (code
), port
, pstate
);
449 scm_putc ('>', port
);
453 scm_putc ('>', port
);
456 case scm_tc7_substring
:
458 if (SCM_WRITINGP (pstate
))
462 scm_putc ('"', port
);
463 for (i
= 0; i
< SCM_ROLENGTH (exp
); ++i
)
464 switch (SCM_ROCHARS (exp
)[i
])
468 scm_putc ('\\', port
);
470 scm_putc (SCM_ROCHARS (exp
)[i
], port
);
472 scm_putc ('"', port
);
476 scm_lfwrite (SCM_ROCHARS (exp
), (scm_sizet
) SCM_ROLENGTH (exp
),
479 case scm_tcs_symbols
:
489 len
= SCM_LENGTH (exp
);
490 str
= SCM_CHARS (exp
);
497 scm_lfwrite ("#{}#", 4, port
);
499 for (end
= pos
; end
< len
; ++end
)
502 #ifdef BRACKETS_AS_PARENS
510 case SCM_WHITE_SPACES
:
511 case SCM_LINE_INCREMENTORS
:
520 scm_lfwrite ("#{", 2, port
);
525 scm_lfwrite (str
+ pos
, end
- pos
, port
);
531 scm_lfwrite (buf
, 2, port
);
553 scm_lfwrite (str
+ pos
, end
- pos
, port
);
555 scm_lfwrite ("}#", 2, port
);
559 ENTER_NESTED_DATA (pstate
, exp
, circref
);
560 if (SCM_IS_WHVEC (exp
))
561 scm_puts ("#wh(", port
);
563 scm_puts ("#w(", port
);
564 goto common_vector_printer
;
567 ENTER_NESTED_DATA (pstate
, exp
, circref
);
568 scm_puts ("#(", port
);
569 common_vector_printer
:
572 int last
= SCM_LENGTH (exp
) - 1;
574 if (pstate
->fancyp
&& SCM_LENGTH (exp
) > pstate
->length
)
576 last
= pstate
->length
- 1;
579 for (i
= 0; i
< last
; ++i
)
582 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
583 scm_putc (' ', port
);
588 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
591 scm_puts (" ...", port
);
592 scm_putc (')', port
);
594 EXIT_NESTED_DATA (pstate
);
605 #ifdef HAVE_LONG_LONGS
608 scm_raprin1 (exp
, port
, pstate
);
612 scm_puts (SCM_SUBR_GENERIC (exp
) && *SCM_SUBR_GENERIC (exp
)
613 ? "#<primitive-generic "
614 : "#<primitive-procedure ",
616 scm_puts (SCM_CHARS (SCM_SNAME (exp
)), port
);
617 scm_putc ('>', port
);
622 SCM proc
= SCM_CCLO_SUBR (exp
);
623 if (SCM_EQ_P (proc
, scm_f_gsubr_apply
))
625 /* Print gsubrs as primitives */
626 SCM name
= scm_procedure_name (exp
);
627 scm_puts ("#<primitive-procedure", port
);
628 if (SCM_NFALSEP (name
))
630 scm_putc (' ', port
);
631 scm_puts (SCM_CHARS (name
), port
);
636 scm_puts ("#<compiled-closure ", port
);
637 scm_iprin1 (proc
, port
, pstate
);
639 scm_putc ('>', port
);
644 scm_puts ("#<procedure-with-setter", port
);
646 SCM name
= scm_procedure_name (exp
);
647 if (SCM_NFALSEP (name
))
649 scm_putc (' ', port
);
650 scm_puts (SCM_ROCHARS (name
), port
);
653 scm_putc ('>', port
);
656 scm_puts ("#<continuation ", port
);
657 scm_intprint (SCM_LENGTH (exp
), 10, port
);
658 scm_puts (" @ ", port
);
659 scm_intprint ((long) SCM_CHARS (exp
), 16, port
);
660 scm_putc ('>', port
);
664 register long i
= SCM_PTOBNUM (exp
);
666 && scm_ptobs
[i
].print
667 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
674 ENTER_NESTED_DATA (pstate
, exp
, circref
);
675 i
= SCM_SMOBNUM (exp
);
676 if (i
< scm_numsmob
&& scm_smobs
[i
].print
677 && (scm_smobs
[i
].print
) (exp
, port
, pstate
))
679 EXIT_NESTED_DATA (pstate
);
682 EXIT_NESTED_DATA (pstate
);
683 /* Macros have their print field set to NULL. They are
684 handled at the same place as closures in order to achieve
685 non-redundancy. Placing the condition here won't slow
686 down printing of other smobs. */
687 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
692 scm_ipruk ("type", exp
, port
);
697 /* Print states are necessary for circular reference safe printing.
698 * They are also expensive to allocate. Therefore print states are
699 * kept in a pool so that they can be reused.
702 /* The PORT argument can also be a print-state/port pair, which will
703 * then be used instead of allocating a new print state. This is
704 * useful for continuing a chain of print calls from Scheme. */
707 scm_prin1 (SCM exp
, SCM port
, int writingp
)
709 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
711 scm_print_state
*pstate
;
713 /* If PORT is a print-state/port pair, use that. Else create a new
716 if (SCM_PORT_WITH_PS_P (port
))
718 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
719 port
= SCM_PORT_WITH_PS_PORT (port
);
723 /* First try to allocate a print state from the pool */
725 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
727 handle
= SCM_CDR (print_state_pool
);
728 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
731 if (SCM_FALSEP (handle
))
732 handle
= scm_cons (make_print_state (), SCM_EOL
);
733 pstate_scm
= SCM_CAR (handle
);
736 pstate
= SCM_PRINT_STATE (pstate_scm
);
737 pstate
->writingp
= writingp
;
738 scm_iprin1 (exp
, port
, pstate
);
740 /* Return print state to pool if it has been created above and
741 hasn't escaped to Scheme. */
743 if (!SCM_FALSEP (handle
) && !pstate
->revealed
)
746 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
747 SCM_SETCDR (print_state_pool
, handle
);
757 scm_intprint (long n
, int radix
, SCM port
)
759 char num_buf
[SCM_INTBUFLEN
];
760 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
763 /* Print an object of unrecognized type.
767 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
769 scm_puts ("#<unknown-", port
);
770 scm_puts (hdr
, port
);
773 scm_puts (" (0x", port
);
774 scm_intprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
775 scm_puts (" . 0x", port
);
776 scm_intprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
777 scm_puts (") @", port
);
779 scm_puts (" 0x", port
);
780 scm_intprint (SCM_UNPACK (ptr
), 16, port
);
781 scm_putc ('>', port
);
789 scm_iprlist (char *hdr
,SCM exp
,int tlr
,SCM port
,scm_print_state
*pstate
)
791 register SCM hare
, tortoise
;
792 int floor
= pstate
->top
- 2;
793 scm_puts (hdr
, port
);
798 /* Run a hare and tortoise so that total time complexity will be
799 O(depth * N) instead of O(N^2). */
800 hare
= SCM_CDR (exp
);
802 while (SCM_ECONSP (hare
))
804 if (SCM_EQ_P (hare
, tortoise
))
806 hare
= SCM_CDR (hare
);
807 if (SCM_IMP (hare
) || SCM_NECONSP (hare
))
809 hare
= SCM_CDR (hare
);
810 tortoise
= SCM_CDR (tortoise
);
813 /* No cdr cycles intrinsic to this list */
814 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
816 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
820 if (SCM_NECONSP (exp
))
822 for (i
= floor
; i
>= 0; --i
)
823 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
825 PUSH_REF (pstate
, exp
);
826 scm_putc (' ', port
);
828 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
830 if (SCM_NNULLP (exp
))
832 scm_puts (" . ", port
);
833 scm_iprin1 (exp
, port
, pstate
);
837 scm_putc (tlr
, port
);
838 pstate
->top
= floor
+ 2;
843 int n
= pstate
->length
;
845 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
846 exp
= SCM_CDR (exp
); --n
;
847 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
849 register unsigned long i
;
851 if (SCM_NECONSP (exp
))
853 for (i
= 0; i
< pstate
->top
; ++i
)
854 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
860 scm_puts (" ...", port
);
866 PUSH_REF(pstate
, exp
);
867 ++pstate
->list_offset
;
868 scm_putc (' ', port
);
870 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
873 if (SCM_NNULLP (exp
))
875 scm_puts (" . ", port
);
876 scm_iprin1 (exp
, port
, pstate
);
879 pstate
->list_offset
-= pstate
->top
- floor
- 2;
883 pstate
->list_offset
-= pstate
->top
- floor
- 2;
886 scm_puts (" . ", port
);
887 print_circref (port
, pstate
, exp
);
894 scm_valid_oport_value_p (SCM val
)
896 return (SCM_OPOUTPORTP (val
)
897 || (SCM_PORT_WITH_PS_P (val
)
898 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
901 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
904 scm_write (SCM obj
, SCM port
)
906 if (SCM_UNBNDP (port
))
909 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
911 scm_prin1 (obj
, port
, 1);
915 scm_close_port (port
);
918 return SCM_UNSPECIFIED
;
922 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
925 scm_display (SCM obj
, SCM port
)
927 if (SCM_UNBNDP (port
))
930 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
932 scm_prin1 (obj
, port
, 0);
936 scm_close_port (port
);
939 return SCM_UNSPECIFIED
;
943 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
944 (SCM destination
, SCM message
, SCM args
),
945 "Write MESSAGE to DESTINATION, defaulting to `current-output-port'.\n"
946 "MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,\n"
947 "the escapes are replaced with corresponding members of ARGS:\n"
948 "~A formats using `display' and ~S formats using `write'.\n"
949 "If DESTINATION is #t, then use the `current-output-port',\n"
950 "if DESTINATION is #f, then return a string containing the formatted text.\n"
951 "Does not add a trailing newline.")
952 #define FUNC_NAME s_scm_simple_format
954 SCM answer
= SCM_UNSPECIFIED
;
955 int fReturnString
= 0;
960 if (SCM_EQ_P (destination
, SCM_BOOL_T
)) {
961 destination
= scm_cur_outp
;
962 } else if (SCM_FALSEP (destination
)) {
964 destination
= scm_mkstrport (SCM_INUM0
,
965 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
969 SCM_VALIDATE_OPORT_VALUE (1,destination
);
971 SCM_VALIDATE_STRING(2,message
);
972 SCM_VALIDATE_REST_ARGUMENT (args
);
974 start
= SCM_ROCHARS (message
);
975 for (p
= start
; *p
!= '\0'; ++p
)
978 if (SCM_IMP (args
) || SCM_NCONSP (args
))
989 scm_lfwrite (start
, p
- start
- 1, destination
);
990 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
991 args
= SCM_CDR (args
);
994 scm_lfwrite (start
, p
- start
, destination
);
997 answer
= scm_strport_to_string (destination
);
999 return scm_return_first(answer
,message
);
1004 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1006 "Send a newline to PORT.")
1007 #define FUNC_NAME s_scm_newline
1009 if (SCM_UNBNDP (port
))
1010 port
= scm_cur_outp
;
1012 SCM_VALIDATE_OPORT_VALUE (1,port
);
1014 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1015 return SCM_UNSPECIFIED
;
1019 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1020 (SCM chr
, SCM port
),
1021 "Send character CHR to PORT.")
1022 #define FUNC_NAME s_scm_write_char
1024 if (SCM_UNBNDP (port
))
1025 port
= scm_cur_outp
;
1027 SCM_VALIDATE_CHAR (1,chr
);
1028 SCM_VALIDATE_OPORT_VALUE (2,port
);
1030 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1034 scm_close_port (port
);
1037 return SCM_UNSPECIFIED
;
1043 /* Call back to Scheme code to do the printing of special objects
1044 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1045 * containing PORT and PSTATE. This object can be used as the port for
1046 * display/write etc to continue the current print chain. The REVEALED
1047 * field of PSTATE is set to true to indicate that the print state has
1048 * escaped to Scheme and thus has to be freed by the GC.
1051 long scm_tc16_port_with_ps
;
1053 /* Print exactly as the port itself would */
1056 print_port_with_ps (SCM obj
, SCM port
, scm_print_state
*pstate
)
1058 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1059 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1063 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1066 SCM pair
= scm_cons (port
, pstate
->handle
);
1067 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (pair
));
1068 pstate
->revealed
= 1;
1069 return scm_apply (proc
, exp
, scm_cons (pwps
, scm_listofnull
));
1072 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 2, 0, 0,
1073 (SCM port
, SCM pstate
),
1075 #define FUNC_NAME s_scm_port_with_print_state
1078 SCM_VALIDATE_OPORT_VALUE (1,port
);
1079 SCM_VALIDATE_PRINTSTATE (2,pstate
);
1080 port
= SCM_COERCE_OUTPORT (port
);
1081 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (scm_cons (port
, pstate
)));
1086 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1089 #define FUNC_NAME s_scm_get_print_state
1091 if (SCM_PORT_WITH_PS_P (port
))
1092 return SCM_PORT_WITH_PS_PS (port
);
1093 if (SCM_OUTPUT_PORT_P (port
))
1095 RETURN_SCM_WTA (1,port
);
1104 SCM vtable
, layout
, type
;
1106 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1107 vtable
= scm_make_vtable_vtable (scm_make_struct_layout (scm_nullstr
),
1110 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
1111 type
= scm_make_struct (vtable
, SCM_INUM0
, SCM_LIST1 (layout
));
1112 scm_set_struct_vtable_name_x (type
, SCM_CAR (scm_intern0 ("print-state")));
1113 print_state_pool
= scm_permanent_object (scm_cons (type
, SCM_EOL
));
1115 scm_print_state_vtable
= type
;
1117 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1118 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1119 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1120 scm_set_smob_print (scm_tc16_port_with_ps
, print_port_with_ps
);
1122 #include "libguile/print.x"