1 /* Copyright (C) 1995-1999,2000,2001, 2002, 2003 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library 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 GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "libguile/_scm.h"
24 #include "libguile/chars.h"
25 #include "libguile/continuations.h"
26 #include "libguile/smob.h"
27 #include "libguile/eval.h"
28 #include "libguile/macros.h"
29 #include "libguile/procprop.h"
30 #include "libguile/read.h"
31 #include "libguile/weaks.h"
32 #include "libguile/unif.h"
33 #include "libguile/alist.h"
34 #include "libguile/struct.h"
35 #include "libguile/objects.h"
36 #include "libguile/ports.h"
37 #include "libguile/root.h"
38 #include "libguile/strings.h"
39 #include "libguile/strports.h"
40 #include "libguile/vectors.h"
41 #include "libguile/lang.h"
43 #include "libguile/validate.h"
44 #include "libguile/print.h"
47 /* {Names of immediate symbols}
49 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
52 char *scm_isymnames
[] =
54 /* This table must agree with the list of SCM_IM_ constants in tags.h */
70 "#@literal-variable-ref",
71 "#@literal-variable-set!",
74 "#@call-with-current-continuation",
76 /* user visible ISYMS */
90 /* Multi-language support */
101 /* Elisp nil value. This is its Scheme name; whenever it's printed
102 in Elisp, it should appear as the symbol `nil'. */
107 scm_t_option scm_print_opts
[] = {
108 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK (SCM_BOOL_F
),
109 "Hook for printing closures (should handle macros as well)." },
110 { SCM_OPTION_BOOLEAN
, "source", 0,
111 "Print closures with source." }
114 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
116 "Option interface for the print options. Instead of using\n"
117 "this procedure directly, use the procedures\n"
118 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
119 "and @code{print-options}.")
120 #define FUNC_NAME s_scm_print_options
122 SCM ans
= scm_options (setting
,
131 /* {Printing of Scheme Objects}
134 /* Detection of circular references.
136 * Due to other constraints in the implementation, this code has bad
137 * time complexity (O (depth * N)), The printer code can be
138 * rewritten to be O(N).
140 #define PUSH_REF(pstate, obj) \
142 pstate->ref_stack[pstate->top++] = (obj); \
143 if (pstate->top == pstate->ceiling) \
144 grow_ref_stack (pstate); \
147 #define ENTER_NESTED_DATA(pstate, obj, label) \
149 register unsigned long i; \
150 for (i = 0; i < pstate->top; ++i) \
151 if (SCM_EQ_P (pstate->ref_stack[i], (obj))) \
153 if (pstate->fancyp) \
155 if (pstate->top - pstate->list_offset >= pstate->level) \
157 scm_putc ('#', port); \
161 PUSH_REF(pstate, obj); \
164 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
166 SCM scm_print_state_vtable
= SCM_BOOL_F
;
167 static SCM print_state_pool
= SCM_EOL
;
169 #ifdef GUILE_DEBUG /* Used for debugging purposes */
171 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
173 "Return the current-pstate -- the car of the\n"
174 "@code{print_state_pool}. @code{current-pstate} is only\n"
175 "included in @code{--enable-guile-debug} builds.")
176 #define FUNC_NAME s_scm_current_pstate
178 if (!SCM_NULLP (print_state_pool
))
179 return SCM_CAR (print_state_pool
);
187 #define PSTATE_SIZE 50L
190 make_print_state (void)
193 = scm_make_struct (scm_print_state_vtable
, SCM_INUM0
, SCM_EOL
);
194 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
195 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
196 pstate
->ref_stack
= SCM_WRITABLE_VELTS (pstate
->ref_vect
);
197 pstate
->ceiling
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
202 scm_make_print_state ()
204 SCM answer
= SCM_BOOL_F
;
206 /* First try to allocate a print state from the pool */
208 if (!SCM_NULLP (print_state_pool
))
210 answer
= SCM_CAR (print_state_pool
);
211 print_state_pool
= SCM_CDR (print_state_pool
);
215 return SCM_FALSEP (answer
) ? make_print_state () : answer
;
219 scm_free_print_state (SCM print_state
)
222 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
223 /* Cleanup before returning print state to pool.
224 * It is better to do it here. Doing it in scm_prin1
225 * would cost more since that function is called much more
229 pstate
->revealed
= 0;
231 handle
= scm_cons (print_state
, print_state_pool
);
232 print_state_pool
= handle
;
237 grow_ref_stack (scm_print_state
*pstate
)
239 unsigned long int old_size
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
240 SCM
const *old_elts
= SCM_VELTS (pstate
->ref_vect
);
241 unsigned long int new_size
= 2 * pstate
->ceiling
;
242 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
245 for (i
= 0; i
!= old_size
; ++i
)
246 SCM_VECTOR_SET (new_vect
, i
, old_elts
[i
]);
248 pstate
->ref_vect
= new_vect
;
249 pstate
->ref_stack
= SCM_WRITABLE_VELTS(new_vect
);
250 pstate
->ceiling
= new_size
;
255 print_circref (SCM port
, scm_print_state
*pstate
, SCM ref
)
258 long self
= pstate
->top
- 1;
260 if (SCM_CONSP (pstate
->ref_stack
[i
]))
264 if (!SCM_CONSP (pstate
->ref_stack
[i
- 1])
265 || !SCM_EQ_P (SCM_CDR (pstate
->ref_stack
[i
- 1]),
266 pstate
->ref_stack
[i
]))
272 for (i
= pstate
->top
- 1; 1; --i
)
273 if (SCM_EQ_P (pstate
->ref_stack
[i
], ref
))
275 scm_putc ('#', port
);
276 scm_intprint (i
- self
, 10, port
);
277 scm_putc ('#', port
);
280 /* Print the name of a symbol. */
283 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
295 /* XXX - Lots of weird symbol names are missed, such as "12" or
299 scm_lfwrite ("#{}#", 4, port
);
300 else if (str
[0] == '#' || str
[0] == ':' || str
[len
-1] == ':')
302 scm_lfwrite ("#{", 2, port
);
306 for (end
= pos
; end
< len
; ++end
)
309 #ifdef BRACKETS_AS_PARENS
317 case SCM_WHITE_SPACES
:
318 case SCM_LINE_INCREMENTORS
:
327 scm_lfwrite ("#{", 2, port
);
332 scm_lfwrite (str
+ pos
, end
- pos
, port
);
338 scm_lfwrite (buf
, 2, port
);
360 scm_lfwrite (str
+ pos
, end
- pos
, port
);
362 scm_lfwrite ("}#", 2, port
);
365 /* Print generally. Handles both write and display according to PSTATE.
367 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
368 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
371 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
373 switch (SCM_ITAG3 (exp
))
375 case scm_tc3_closure
:
378 /* These tc3 tags should never occur in an immediate value. They are
379 * only used in cell types of non-immediates, i. e. the value returned
380 * by SCM_CELL_TYPE (exp) can use these tags.
382 scm_ipruk ("immediate", exp
, port
);
386 scm_intprint (SCM_INUM (exp
), 10, port
);
391 long i
= SCM_CHAR (exp
);
393 if (SCM_WRITINGP (pstate
))
395 scm_puts ("#\\", port
);
396 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
397 scm_puts (scm_charnames
[i
], port
);
399 else if (i
== '\177')
400 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
402 else if (i
< 0 || i
> '\177')
403 scm_intprint (i
, 8, port
);
410 else if (SCM_IFLAGP (exp
)
411 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
412 scm_puts (SCM_ISYMCHARS (exp
), port
);
413 else if (SCM_ILOCP (exp
))
415 scm_puts ("#@", port
);
416 scm_intprint ((long) SCM_IFRAME (exp
), 10, port
);
417 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
418 scm_intprint ((long) SCM_IDIST (exp
), 10, port
);
422 /* unknown immediate value */
423 scm_ipruk ("immediate", exp
, port
);
427 switch (SCM_TYP7 (exp
))
431 ENTER_NESTED_DATA (pstate
, exp
, circref
);
432 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
434 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
438 scm_tc16_port_with_ps
,
439 SCM_UNPACK (scm_cons (port
, pstate
->handle
)));
440 pstate
->revealed
= 1;
441 scm_call_generic_2 (print
, exp
, pwps
);
446 scm_print_struct (exp
, port
, pstate
);
448 EXIT_NESTED_DATA (pstate
);
451 case scm_tcs_cons_imcar
:
452 case scm_tcs_cons_nimcar
:
453 ENTER_NESTED_DATA (pstate
, exp
, circref
);
454 scm_iprlist ("(", exp
, ')', port
, pstate
);
455 EXIT_NESTED_DATA (pstate
);
458 print_circref (port
, pstate
, exp
);
460 case scm_tcs_closures
:
461 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
462 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
465 SCM formals
= SCM_CLOSURE_FORMALS (exp
);
466 scm_puts ("#<procedure", port
);
467 scm_putc (' ', port
);
468 scm_iprin1 (scm_procedure_name (exp
), port
, pstate
);
469 scm_putc (' ', port
);
470 if (SCM_PRINT_SOURCE_P
)
472 SCM env
= SCM_ENV (exp
);
473 SCM xenv
= SCM_EXTEND_ENV (formals
, SCM_EOL
, env
);
474 SCM src
= scm_unmemocopy (SCM_CODE (exp
), xenv
);
475 ENTER_NESTED_DATA (pstate
, exp
, circref
);
476 scm_iprin1 (src
, port
, pstate
);
477 EXIT_NESTED_DATA (pstate
);
480 scm_iprin1 (formals
, port
, pstate
);
481 scm_putc ('>', port
);
485 if (SCM_WRITINGP (pstate
))
489 scm_putc ('"', port
);
490 for (i
= 0; i
< SCM_STRING_LENGTH (exp
); ++i
)
491 switch (SCM_STRING_CHARS (exp
)[i
])
495 scm_putc ('\\', port
);
497 scm_putc (SCM_STRING_CHARS (exp
)[i
], port
);
499 scm_putc ('"', port
);
503 scm_lfwrite (SCM_STRING_CHARS (exp
), SCM_STRING_LENGTH (exp
), port
);
506 if (SCM_SYMBOL_INTERNED_P (exp
))
508 scm_print_symbol_name (SCM_SYMBOL_CHARS (exp
),
509 SCM_SYMBOL_LENGTH (exp
),
511 scm_remember_upto_here_1 (exp
);
515 scm_puts ("#<uninterned-symbol ", port
);
516 scm_print_symbol_name (SCM_SYMBOL_CHARS (exp
),
517 SCM_SYMBOL_LENGTH (exp
),
519 scm_putc (' ', port
);
520 scm_intprint ((long)exp
, 16, port
);
521 scm_putc ('>', port
);
524 case scm_tc7_variable
:
525 scm_i_variable_print (exp
, port
, pstate
);
528 ENTER_NESTED_DATA (pstate
, exp
, circref
);
529 if (SCM_IS_WHVEC (exp
))
530 scm_puts ("#wh(", port
);
532 scm_puts ("#w(", port
);
533 goto common_vector_printer
;
536 ENTER_NESTED_DATA (pstate
, exp
, circref
);
537 scm_puts ("#(", port
);
538 common_vector_printer
:
541 long last
= SCM_VECTOR_LENGTH (exp
) - 1;
543 if (pstate
->fancyp
&& SCM_VECTOR_LENGTH (exp
) > pstate
->length
)
545 last
= pstate
->length
- 1;
548 for (i
= 0; i
< last
; ++i
)
551 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
552 scm_putc (' ', port
);
557 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
560 scm_puts (" ...", port
);
561 scm_putc (')', port
);
563 EXIT_NESTED_DATA (pstate
);
574 #if SCM_SIZEOF_LONG_LONG != 0
577 scm_raprin1 (exp
, port
, pstate
);
581 scm_puts (SCM_SUBR_GENERIC (exp
)
582 ? "#<primitive-generic "
583 : "#<primitive-procedure ",
585 scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp
)), port
);
586 scm_putc ('>', port
);
591 SCM proc
= SCM_CCLO_SUBR (exp
);
592 if (SCM_EQ_P (proc
, scm_f_gsubr_apply
))
594 /* Print gsubrs as primitives */
595 SCM name
= scm_procedure_name (exp
);
596 scm_puts ("#<primitive-procedure", port
);
597 if (!SCM_FALSEP (name
))
599 scm_putc (' ', port
);
600 scm_puts (SCM_SYMBOL_CHARS (name
), port
);
605 scm_puts ("#<compiled-closure ", port
);
606 scm_iprin1 (proc
, port
, pstate
);
608 scm_putc ('>', port
);
613 scm_puts ("#<procedure-with-setter", port
);
615 SCM name
= scm_procedure_name (exp
);
616 if (!SCM_FALSEP (name
))
618 scm_putc (' ', port
);
619 scm_display (name
, port
);
622 scm_putc ('>', port
);
626 register long i
= SCM_PTOBNUM (exp
);
628 && scm_ptobs
[i
].print
629 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
634 ENTER_NESTED_DATA (pstate
, exp
, circref
);
635 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
636 EXIT_NESTED_DATA (pstate
);
640 scm_ipruk ("type", exp
, port
);
645 /* Print states are necessary for circular reference safe printing.
646 * They are also expensive to allocate. Therefore print states are
647 * kept in a pool so that they can be reused.
650 /* The PORT argument can also be a print-state/port pair, which will
651 * then be used instead of allocating a new print state. This is
652 * useful for continuing a chain of print calls from Scheme. */
655 scm_prin1 (SCM exp
, SCM port
, int writingp
)
657 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
659 scm_print_state
*pstate
;
662 /* If PORT is a print-state/port pair, use that. Else create a new
665 if (SCM_PORT_WITH_PS_P (port
))
667 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
668 port
= SCM_PORT_WITH_PS_PORT (port
);
672 /* First try to allocate a print state from the pool */
674 if (!SCM_NULLP (print_state_pool
))
676 handle
= print_state_pool
;
677 print_state_pool
= SCM_CDR (print_state_pool
);
680 if (SCM_FALSEP (handle
))
681 handle
= scm_list_1 (make_print_state ());
682 pstate_scm
= SCM_CAR (handle
);
685 pstate
= SCM_PRINT_STATE (pstate_scm
);
686 old_writingp
= pstate
->writingp
;
687 pstate
->writingp
= writingp
;
688 scm_iprin1 (exp
, port
, pstate
);
689 pstate
->writingp
= old_writingp
;
691 /* Return print state to pool if it has been created above and
692 hasn't escaped to Scheme. */
694 if (!SCM_FALSEP (handle
) && !pstate
->revealed
)
697 SCM_SETCDR (handle
, print_state_pool
);
698 print_state_pool
= handle
;
708 scm_intprint (long n
, int radix
, SCM port
)
710 char num_buf
[SCM_INTBUFLEN
];
711 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
714 /* Print an object of unrecognized type.
718 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
720 scm_puts ("#<unknown-", port
);
721 scm_puts (hdr
, port
);
722 if (scm_in_heap_p (ptr
))
724 scm_puts (" (0x", port
);
725 scm_intprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
726 scm_puts (" . 0x", port
);
727 scm_intprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
728 scm_puts (") @", port
);
730 scm_puts (" 0x", port
);
731 scm_intprint (SCM_UNPACK (ptr
), 16, port
);
732 scm_putc ('>', port
);
739 scm_iprlist (char *hdr
, SCM exp
, int tlr
, SCM port
, scm_print_state
*pstate
)
741 register SCM hare
, tortoise
;
742 long floor
= pstate
->top
- 2;
743 scm_puts (hdr
, port
);
748 /* Run a hare and tortoise so that total time complexity will be
749 O(depth * N) instead of O(N^2). */
750 hare
= SCM_CDR (exp
);
752 while (SCM_CONSP (hare
))
754 if (SCM_EQ_P (hare
, tortoise
))
756 hare
= SCM_CDR (hare
);
757 if (!SCM_CONSP (hare
))
759 hare
= SCM_CDR (hare
);
760 tortoise
= SCM_CDR (tortoise
);
763 /* No cdr cycles intrinsic to this list */
764 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
765 for (exp
= SCM_CDR (exp
); SCM_CONSP (exp
); exp
= SCM_CDR (exp
))
769 for (i
= floor
; i
>= 0; --i
)
770 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
772 PUSH_REF (pstate
, exp
);
773 scm_putc (' ', port
);
775 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
777 if (!SCM_NULL_OR_NIL_P (exp
))
779 scm_puts (" . ", port
);
780 scm_iprin1 (exp
, port
, pstate
);
784 scm_putc (tlr
, port
);
785 pstate
->top
= floor
+ 2;
790 long n
= pstate
->length
;
792 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
793 exp
= SCM_CDR (exp
); --n
;
794 for (; SCM_CONSP (exp
); exp
= SCM_CDR (exp
))
796 register unsigned long i
;
798 for (i
= 0; i
< pstate
->top
; ++i
)
799 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
805 scm_puts (" ...", port
);
811 PUSH_REF(pstate
, exp
);
812 ++pstate
->list_offset
;
813 scm_putc (' ', port
);
815 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
818 if (!SCM_NULL_OR_NIL_P (exp
))
820 scm_puts (" . ", port
);
821 scm_iprin1 (exp
, port
, pstate
);
824 pstate
->list_offset
-= pstate
->top
- floor
- 2;
828 pstate
->list_offset
-= pstate
->top
- floor
- 2;
831 scm_puts (" . ", port
);
832 print_circref (port
, pstate
, exp
);
839 scm_valid_oport_value_p (SCM val
)
841 return (SCM_OPOUTPORTP (val
)
842 || (SCM_PORT_WITH_PS_P (val
)
843 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
846 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
849 scm_write (SCM obj
, SCM port
)
851 if (SCM_UNBNDP (port
))
854 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
856 scm_prin1 (obj
, port
, 1);
860 scm_close_port (port
);
863 return SCM_UNSPECIFIED
;
867 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
870 scm_display (SCM obj
, SCM port
)
872 if (SCM_UNBNDP (port
))
875 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
877 scm_prin1 (obj
, port
, 0);
881 scm_close_port (port
);
884 return SCM_UNSPECIFIED
;
888 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
889 (SCM destination
, SCM message
, SCM args
),
890 "Write @var{message} to @var{destination}, defaulting to\n"
891 "the current output port.\n"
892 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
893 "@code{~S} (was @code{%S}) escapes. When printed,\n"
894 "the escapes are replaced with corresponding members of\n"
896 "@code{~A} formats using @code{display} and @code{~S} formats\n"
897 "using @code{write}.\n"
898 "If @var{destination} is @code{#t}, then use the current output\n"
899 "port, if @var{destination} is @code{#f}, then return a string\n"
900 "containing the formatted text. Does not add a trailing newline.")
901 #define FUNC_NAME s_scm_simple_format
903 SCM answer
= SCM_UNSPECIFIED
;
904 int fReturnString
= 0;
910 if (SCM_EQ_P (destination
, SCM_BOOL_T
))
912 destination
= scm_cur_outp
;
914 else if (SCM_FALSEP (destination
))
917 destination
= scm_mkstrport (SCM_INUM0
,
918 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
924 SCM_VALIDATE_OPORT_VALUE (1, destination
);
925 destination
= SCM_COERCE_OUTPORT (destination
);
927 SCM_VALIDATE_STRING (2, message
);
928 SCM_VALIDATE_REST_ARGUMENT (args
);
930 start
= SCM_STRING_CHARS (message
);
931 end
= start
+ SCM_STRING_LENGTH (message
);
932 for (p
= start
; p
!= end
; ++p
)
947 scm_lfwrite (start
, p
- start
, destination
);
951 scm_lfwrite (start
, p
- start
- 1, destination
);
952 scm_newline (destination
);
956 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
957 scm_list_1 (SCM_MAKE_CHAR (*p
)));
962 if (!SCM_CONSP (args
))
963 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
964 scm_list_1 (SCM_MAKE_CHAR (*p
)));
966 scm_lfwrite (start
, p
- start
- 1, destination
);
967 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
968 args
= SCM_CDR (args
);
972 scm_lfwrite (start
, p
- start
, destination
);
973 if (!SCM_EQ_P (args
, SCM_EOL
))
974 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
975 scm_list_1 (scm_length (args
)));
978 answer
= scm_strport_to_string (destination
);
980 return scm_return_first (answer
, message
);
985 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
987 "Send a newline to @var{port}.\n"
988 "If @var{port} is omitted, send to the current output port.")
989 #define FUNC_NAME s_scm_newline
991 if (SCM_UNBNDP (port
))
994 SCM_VALIDATE_OPORT_VALUE (1, port
);
996 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
997 return SCM_UNSPECIFIED
;
1001 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1002 (SCM chr
, SCM port
),
1003 "Send character @var{chr} to @var{port}.")
1004 #define FUNC_NAME s_scm_write_char
1006 if (SCM_UNBNDP (port
))
1007 port
= scm_cur_outp
;
1009 SCM_VALIDATE_CHAR (1, chr
);
1010 SCM_VALIDATE_OPORT_VALUE (2, port
);
1012 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1016 scm_close_port (port
);
1019 return SCM_UNSPECIFIED
;
1025 /* Call back to Scheme code to do the printing of special objects
1026 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1027 * containing PORT and PSTATE. This object can be used as the port for
1028 * display/write etc to continue the current print chain. The REVEALED
1029 * field of PSTATE is set to true to indicate that the print state has
1030 * escaped to Scheme and thus has to be freed by the GC.
1033 scm_t_bits scm_tc16_port_with_ps
;
1035 /* Print exactly as the port itself would */
1038 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1040 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1041 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1045 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1048 SCM pair
= scm_cons (port
, pstate
->handle
);
1049 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (pair
));
1050 pstate
->revealed
= 1;
1051 return scm_call_2 (proc
, exp
, pwps
);
1054 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 2, 0, 0,
1055 (SCM port
, SCM pstate
),
1056 "Create a new port which behaves like @var{port}, but with an\n"
1057 "included print state @var{pstate}.")
1058 #define FUNC_NAME s_scm_port_with_print_state
1061 SCM_VALIDATE_OPORT_VALUE (1, port
);
1062 SCM_VALIDATE_PRINTSTATE (2, pstate
);
1063 port
= SCM_COERCE_OUTPORT (port
);
1064 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (scm_cons (port
, pstate
)));
1069 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1071 "Return the print state of the port @var{port}. If @var{port}\n"
1072 "has no associated print state, @code{#f} is returned.")
1073 #define FUNC_NAME s_scm_get_print_state
1075 if (SCM_PORT_WITH_PS_P (port
))
1076 return SCM_PORT_WITH_PS_PS (port
);
1077 if (SCM_OUTPUT_PORT_P (port
))
1079 SCM_WRONG_TYPE_ARG (1, port
);
1088 SCM vtable
, layout
, type
;
1090 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1092 scm_gc_register_root (&print_state_pool
);
1093 scm_gc_register_root (&scm_print_state_vtable
);
1094 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1095 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
1096 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1097 scm_set_struct_vtable_name_x (type
, scm_str2symbol ("print-state"));
1098 scm_print_state_vtable
= type
;
1100 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1101 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1102 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1103 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1105 #include "libguile/print.x"