1 /* Copyright (C) 1995-1999,2000,2001 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. */
47 #include "libguile/_scm.h"
48 #include "libguile/chars.h"
49 #include "libguile/continuations.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 */
124 "#@call-with-values",
129 scm_t_option scm_print_opts
[] = {
130 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK (SCM_BOOL_F
),
131 "Hook for printing closures (should handle macros as well)." },
132 { SCM_OPTION_BOOLEAN
, "source", 0,
133 "Print closures with source." }
136 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
138 "Option interface for the print options. Instead of using\n"
139 "this procedure directly, use the procedures\n"
140 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
141 "and @code{print-options}.")
142 #define FUNC_NAME s_scm_print_options
144 SCM ans
= scm_options (setting
,
153 /* {Printing of Scheme Objects}
156 /* Detection of circular references.
158 * Due to other constraints in the implementation, this code has bad
159 * time complexity (O (depth * N)), The printer code can be
160 * rewritten to be O(N).
162 #define PUSH_REF(pstate, obj) \
164 pstate->ref_stack[pstate->top++] = (obj); \
165 if (pstate->top == pstate->ceiling) \
166 grow_ref_stack (pstate); \
169 #define ENTER_NESTED_DATA(pstate, obj, label) \
171 register unsigned long i; \
172 for (i = 0; i < pstate->top; ++i) \
173 if (SCM_EQ_P (pstate->ref_stack[i], (obj))) \
175 if (pstate->fancyp) \
177 if (pstate->top - pstate->list_offset >= pstate->level) \
179 scm_putc ('#', port); \
183 PUSH_REF(pstate, obj); \
186 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
188 SCM scm_print_state_vtable
= SCM_BOOL_F
;
189 static SCM print_state_pool
= SCM_EOL
;
191 #ifdef GUILE_DEBUG /* Used for debugging purposes */
193 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
195 "Return the current-pstate -- the car of the\n"
196 "@code{print_state_pool}. @code{current-pstate} is only\n"
197 "included in @code{--enable-guile-debug} builds.")
198 #define FUNC_NAME s_scm_current_pstate
200 if (!SCM_NULLP (print_state_pool
))
201 return SCM_CAR (print_state_pool
);
209 #define PSTATE_SIZE 50L
212 make_print_state (void)
215 = scm_make_struct (scm_print_state_vtable
, SCM_INUM0
, SCM_EOL
);
216 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
217 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
218 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
219 pstate
->ceiling
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
224 scm_make_print_state ()
226 SCM answer
= SCM_BOOL_F
;
228 /* First try to allocate a print state from the pool */
230 if (!SCM_NULLP (print_state_pool
))
232 answer
= SCM_CAR (print_state_pool
);
233 print_state_pool
= SCM_CDR (print_state_pool
);
237 return SCM_FALSEP (answer
) ? make_print_state () : answer
;
241 scm_free_print_state (SCM print_state
)
244 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
245 /* Cleanup before returning print state to pool.
246 * It is better to do it here. Doing it in scm_prin1
247 * would cost more since that function is called much more
251 pstate
->revealed
= 0;
253 handle
= scm_cons (print_state
, print_state_pool
);
254 print_state_pool
= handle
;
259 grow_ref_stack (scm_print_state
*pstate
)
261 unsigned long int old_size
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
262 SCM
*old_elts
= SCM_VELTS (pstate
->ref_vect
);
263 unsigned long int new_size
= 2 * pstate
->ceiling
;
264 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
265 SCM
*new_elts
= SCM_VELTS (new_vect
);
268 for (i
= 0; i
!= old_size
; ++i
)
269 new_elts
[i
] = old_elts
[i
];
271 pstate
->ref_vect
= new_vect
;
272 pstate
->ref_stack
= new_elts
;
273 pstate
->ceiling
= new_size
;
278 print_circref (SCM port
,scm_print_state
*pstate
,SCM ref
)
281 long self
= pstate
->top
- 1;
283 if (SCM_CONSP (pstate
->ref_stack
[i
]))
287 if (!SCM_CONSP (pstate
->ref_stack
[i
- 1])
288 || !SCM_EQ_P (SCM_CDR (pstate
->ref_stack
[i
- 1]),
289 pstate
->ref_stack
[i
]))
295 for (i
= pstate
->top
- 1; 1; --i
)
296 if (SCM_EQ_P (pstate
->ref_stack
[i
], ref
))
298 scm_putc ('#', port
);
299 scm_intprint (i
- self
, 10, port
);
300 scm_putc ('#', port
);
303 /* Print the name of a symbol. */
306 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
318 /* XXX - Lots of weird symbol names are missed, such as "12" or
322 scm_lfwrite ("#{}#", 4, port
);
323 else if (str
[0] == '#' || str
[0] == ':' || str
[len
-1] == ':')
325 scm_lfwrite ("#{", 2, port
);
329 for (end
= pos
; end
< len
; ++end
)
332 #ifdef BRACKETS_AS_PARENS
340 case SCM_WHITE_SPACES
:
341 case SCM_LINE_INCREMENTORS
:
350 scm_lfwrite ("#{", 2, port
);
355 scm_lfwrite (str
+ pos
, end
- pos
, port
);
361 scm_lfwrite (buf
, 2, port
);
383 scm_lfwrite (str
+ pos
, end
- pos
, port
);
385 scm_lfwrite ("}#", 2, port
);
388 /* Print generally. Handles both write and display according to PSTATE.
390 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
391 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
394 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
396 switch (SCM_ITAG3 (exp
))
398 case scm_tc3_closure
:
401 /* These tc3 tags should never occur in an immediate value. They are
402 * only used in cell types of non-immediates, i. e. the value returned
403 * by SCM_CELL_TYPE (exp) can use these tags.
405 scm_ipruk ("immediate", exp
, port
);
409 scm_intprint (SCM_INUM (exp
), 10, port
);
414 long i
= SCM_CHAR (exp
);
416 if (SCM_WRITINGP (pstate
))
418 scm_puts ("#\\", port
);
419 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
420 scm_puts (scm_charnames
[i
], port
);
422 else if (i
== '\177')
423 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
425 else if (i
< 0 || i
> '\177')
426 scm_intprint (i
, 8, port
);
433 else if (SCM_IFLAGP (exp
)
434 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
435 scm_puts (SCM_ISYMCHARS (exp
), port
);
436 else if (SCM_ILOCP (exp
))
438 scm_puts ("#@", port
);
439 scm_intprint ((long) SCM_IFRAME (exp
), 10, port
);
440 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
441 scm_intprint ((long) SCM_IDIST (exp
), 10, port
);
445 /* unknown immediate value */
446 scm_ipruk ("immediate", exp
, port
);
450 switch (SCM_TYP7 (exp
))
454 ENTER_NESTED_DATA (pstate
, exp
, circref
);
455 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
457 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
461 scm_tc16_port_with_ps
,
462 SCM_UNPACK (scm_cons (port
, pstate
->handle
)));
463 pstate
->revealed
= 1;
464 scm_call_generic_2 (print
, exp
, pwps
);
469 scm_print_struct (exp
, port
, pstate
);
471 EXIT_NESTED_DATA (pstate
);
474 case scm_tcs_cons_imcar
:
475 case scm_tcs_cons_nimcar
:
476 ENTER_NESTED_DATA (pstate
, exp
, circref
);
477 scm_iprlist ("(", exp
, ')', port
, pstate
);
478 EXIT_NESTED_DATA (pstate
);
481 print_circref (port
, pstate
, exp
);
483 case scm_tcs_closures
:
484 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
485 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
488 SCM formals
= SCM_CLOSURE_FORMALS (exp
);
489 scm_puts ("#<procedure", port
);
490 scm_putc (' ', port
);
491 scm_iprin1 (scm_procedure_name (exp
), port
, pstate
);
492 scm_putc (' ', port
);
493 if (SCM_PRINT_SOURCE_P
)
495 SCM env
= SCM_ENV (exp
);
496 SCM xenv
= SCM_EXTEND_ENV (formals
, SCM_EOL
, env
);
497 SCM src
= scm_unmemocopy (SCM_CODE (exp
), xenv
);
498 ENTER_NESTED_DATA (pstate
, exp
, circref
);
499 scm_iprin1 (src
, port
, pstate
);
500 EXIT_NESTED_DATA (pstate
);
503 scm_iprin1 (formals
, port
, pstate
);
504 scm_putc ('>', port
);
508 if (SCM_WRITINGP (pstate
))
512 scm_putc ('"', port
);
513 for (i
= 0; i
< SCM_STRING_LENGTH (exp
); ++i
)
514 switch (SCM_STRING_CHARS (exp
)[i
])
518 scm_putc ('\\', port
);
520 scm_putc (SCM_STRING_CHARS (exp
)[i
], port
);
522 scm_putc ('"', port
);
526 scm_lfwrite (SCM_STRING_CHARS (exp
), SCM_STRING_LENGTH (exp
), port
);
529 scm_print_symbol_name (SCM_SYMBOL_CHARS (exp
),
530 SCM_SYMBOL_LENGTH (exp
),
532 scm_remember_upto_here_1 (exp
);
534 case scm_tc7_variable
:
535 scm_i_variable_print (exp
, port
, pstate
);
538 ENTER_NESTED_DATA (pstate
, exp
, circref
);
539 if (SCM_IS_WHVEC (exp
))
540 scm_puts ("#wh(", port
);
542 scm_puts ("#w(", port
);
543 goto common_vector_printer
;
546 ENTER_NESTED_DATA (pstate
, exp
, circref
);
547 scm_puts ("#(", port
);
548 common_vector_printer
:
551 long last
= SCM_VECTOR_LENGTH (exp
) - 1;
553 if (pstate
->fancyp
&& SCM_VECTOR_LENGTH (exp
) > pstate
->length
)
555 last
= pstate
->length
- 1;
558 for (i
= 0; i
< last
; ++i
)
561 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
562 scm_putc (' ', port
);
567 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
570 scm_puts (" ...", port
);
571 scm_putc (')', port
);
573 EXIT_NESTED_DATA (pstate
);
584 #ifdef HAVE_LONG_LONGS
587 scm_raprin1 (exp
, port
, pstate
);
591 scm_puts (SCM_SUBR_GENERIC (exp
) && *SCM_SUBR_GENERIC (exp
)
592 ? "#<primitive-generic "
593 : "#<primitive-procedure ",
595 scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp
)), port
);
596 scm_putc ('>', port
);
601 SCM proc
= SCM_CCLO_SUBR (exp
);
602 if (SCM_EQ_P (proc
, scm_f_gsubr_apply
))
604 /* Print gsubrs as primitives */
605 SCM name
= scm_procedure_name (exp
);
606 scm_puts ("#<primitive-procedure", port
);
607 if (!SCM_FALSEP (name
))
609 scm_putc (' ', port
);
610 scm_puts (SCM_SYMBOL_CHARS (name
), port
);
615 scm_puts ("#<compiled-closure ", port
);
616 scm_iprin1 (proc
, port
, pstate
);
618 scm_putc ('>', port
);
623 scm_puts ("#<procedure-with-setter", port
);
625 SCM name
= scm_procedure_name (exp
);
626 if (!SCM_FALSEP (name
))
628 scm_putc (' ', port
);
629 scm_display (name
, port
);
632 scm_putc ('>', port
);
636 register long i
= SCM_PTOBNUM (exp
);
638 && scm_ptobs
[i
].print
639 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
644 ENTER_NESTED_DATA (pstate
, exp
, circref
);
645 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
646 EXIT_NESTED_DATA (pstate
);
650 scm_ipruk ("type", exp
, port
);
655 /* Print states are necessary for circular reference safe printing.
656 * They are also expensive to allocate. Therefore print states are
657 * kept in a pool so that they can be reused.
660 /* The PORT argument can also be a print-state/port pair, which will
661 * then be used instead of allocating a new print state. This is
662 * useful for continuing a chain of print calls from Scheme. */
665 scm_prin1 (SCM exp
, SCM port
, int writingp
)
667 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
669 scm_print_state
*pstate
;
671 /* If PORT is a print-state/port pair, use that. Else create a new
674 if (SCM_PORT_WITH_PS_P (port
))
676 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
677 port
= SCM_PORT_WITH_PS_PORT (port
);
681 /* First try to allocate a print state from the pool */
683 if (!SCM_NULLP (print_state_pool
))
685 handle
= print_state_pool
;
686 print_state_pool
= SCM_CDR (print_state_pool
);
689 if (SCM_FALSEP (handle
))
690 handle
= scm_list_1 (make_print_state ());
691 pstate_scm
= SCM_CAR (handle
);
694 pstate
= SCM_PRINT_STATE (pstate_scm
);
695 pstate
->writingp
= writingp
;
696 scm_iprin1 (exp
, port
, pstate
);
698 /* Return print state to pool if it has been created above and
699 hasn't escaped to Scheme. */
701 if (!SCM_FALSEP (handle
) && !pstate
->revealed
)
704 SCM_SETCDR (handle
, print_state_pool
);
705 print_state_pool
= handle
;
715 scm_intprint (long n
, int radix
, SCM port
)
717 char num_buf
[SCM_INTBUFLEN
];
718 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
721 /* Print an object of unrecognized type.
725 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
727 scm_puts ("#<unknown-", port
);
728 scm_puts (hdr
, port
);
731 scm_puts (" (0x", port
);
732 scm_intprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
733 scm_puts (" . 0x", port
);
734 scm_intprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
735 scm_puts (") @", port
);
737 scm_puts (" 0x", port
);
738 scm_intprint (SCM_UNPACK (ptr
), 16, port
);
739 scm_putc ('>', port
);
746 scm_iprlist (char *hdr
,SCM exp
,int tlr
,SCM port
,scm_print_state
*pstate
)
748 register SCM hare
, tortoise
;
749 long floor
= pstate
->top
- 2;
750 scm_puts (hdr
, port
);
755 /* Run a hare and tortoise so that total time complexity will be
756 O(depth * N) instead of O(N^2). */
757 hare
= SCM_CDR (exp
);
759 while (SCM_CONSP (hare
))
761 if (SCM_EQ_P (hare
, tortoise
))
763 hare
= SCM_CDR (hare
);
764 if (!SCM_CONSP (hare
))
766 hare
= SCM_CDR (hare
);
767 tortoise
= SCM_CDR (tortoise
);
770 /* No cdr cycles intrinsic to this list */
771 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
772 for (exp
= SCM_CDR (exp
); SCM_CONSP (exp
); exp
= SCM_CDR (exp
))
776 for (i
= floor
; i
>= 0; --i
)
777 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
779 PUSH_REF (pstate
, exp
);
780 scm_putc (' ', port
);
782 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
784 if (!SCM_NULLP (exp
))
786 scm_puts (" . ", port
);
787 scm_iprin1 (exp
, port
, pstate
);
791 scm_putc (tlr
, port
);
792 pstate
->top
= floor
+ 2;
797 long n
= pstate
->length
;
799 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
800 exp
= SCM_CDR (exp
); --n
;
801 for (; SCM_CONSP (exp
); exp
= SCM_CDR (exp
))
803 register unsigned long i
;
805 for (i
= 0; i
< pstate
->top
; ++i
)
806 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
812 scm_puts (" ...", port
);
818 PUSH_REF(pstate
, exp
);
819 ++pstate
->list_offset
;
820 scm_putc (' ', port
);
822 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
825 if (!SCM_NULLP (exp
))
827 scm_puts (" . ", port
);
828 scm_iprin1 (exp
, port
, pstate
);
831 pstate
->list_offset
-= pstate
->top
- floor
- 2;
835 pstate
->list_offset
-= pstate
->top
- floor
- 2;
838 scm_puts (" . ", port
);
839 print_circref (port
, pstate
, exp
);
846 scm_valid_oport_value_p (SCM val
)
848 return (SCM_OPOUTPORTP (val
)
849 || (SCM_PORT_WITH_PS_P (val
)
850 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
853 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
856 scm_write (SCM obj
, SCM port
)
858 if (SCM_UNBNDP (port
))
861 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
863 scm_prin1 (obj
, port
, 1);
867 scm_close_port (port
);
870 return SCM_UNSPECIFIED
;
874 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
877 scm_display (SCM obj
, SCM port
)
879 if (SCM_UNBNDP (port
))
882 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
884 scm_prin1 (obj
, port
, 0);
888 scm_close_port (port
);
891 return SCM_UNSPECIFIED
;
895 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
896 (SCM destination
, SCM message
, SCM args
),
897 "Write @var{message} to @var{destination}, defaulting to\n"
898 "the current output port.\n"
899 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
900 "@code{~S} (was @code{%S}) escapes. When printed,\n"
901 "the escapes are replaced with corresponding members of\n"
903 "@code{~A} formats using @code{display} and @code{~S} formats\n"
904 "using @code{write}.\n"
905 "If @var{destination} is @code{#t}, then use the current output\n"
906 "port, if @var{destination} is @code{#f}, then return a string\n"
907 "containing the formatted text. Does not add a trailing newline.")
908 #define FUNC_NAME s_scm_simple_format
910 SCM answer
= SCM_UNSPECIFIED
;
911 int fReturnString
= 0;
917 if (SCM_EQ_P (destination
, SCM_BOOL_T
))
919 destination
= scm_cur_outp
;
921 else if (SCM_FALSEP (destination
))
924 destination
= scm_mkstrport (SCM_INUM0
,
925 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
931 SCM_VALIDATE_OPORT_VALUE (1, destination
);
932 destination
= SCM_COERCE_OUTPORT (destination
);
934 SCM_VALIDATE_STRING (2, message
);
935 SCM_VALIDATE_REST_ARGUMENT (args
);
937 start
= SCM_STRING_CHARS (message
);
938 end
= start
+ SCM_STRING_LENGTH (message
);
939 for (p
= start
; p
!= end
; ++p
)
954 scm_lfwrite (start
, p
- start
, destination
);
958 scm_newline (destination
);
962 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
963 scm_list_1 (SCM_MAKE_CHAR (*p
)));
968 if (!SCM_CONSP (args
))
969 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
970 scm_list_1 (SCM_MAKE_CHAR (*p
)));
972 scm_lfwrite (start
, p
- start
- 1, destination
);
973 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
974 args
= SCM_CDR (args
);
978 scm_lfwrite (start
, p
- start
, destination
);
979 if (!SCM_EQ_P (args
, SCM_EOL
))
980 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
981 scm_list_1 (scm_length (args
)));
984 answer
= scm_strport_to_string (destination
);
986 return scm_return_first (answer
, message
);
991 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
993 "Send a newline to @var{port}.\n"
994 "If @var{port} is omitted, send to the current output port.")
995 #define FUNC_NAME s_scm_newline
997 if (SCM_UNBNDP (port
))
1000 SCM_VALIDATE_OPORT_VALUE (1,port
);
1002 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1003 return SCM_UNSPECIFIED
;
1007 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1008 (SCM chr
, SCM port
),
1009 "Send character @var{chr} to @var{port}.")
1010 #define FUNC_NAME s_scm_write_char
1012 if (SCM_UNBNDP (port
))
1013 port
= scm_cur_outp
;
1015 SCM_VALIDATE_CHAR (1,chr
);
1016 SCM_VALIDATE_OPORT_VALUE (2,port
);
1018 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1022 scm_close_port (port
);
1025 return SCM_UNSPECIFIED
;
1031 /* Call back to Scheme code to do the printing of special objects
1032 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1033 * containing PORT and PSTATE. This object can be used as the port for
1034 * display/write etc to continue the current print chain. The REVEALED
1035 * field of PSTATE is set to true to indicate that the print state has
1036 * escaped to Scheme and thus has to be freed by the GC.
1039 scm_t_bits scm_tc16_port_with_ps
;
1041 /* Print exactly as the port itself would */
1044 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1046 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1047 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1051 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1054 SCM pair
= scm_cons (port
, pstate
->handle
);
1055 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (pair
));
1056 pstate
->revealed
= 1;
1057 return scm_call_2 (proc
, exp
, pwps
);
1060 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 2, 0, 0,
1061 (SCM port
, SCM pstate
),
1062 "Create a new port which behaves like @var{port}, but with an\n"
1063 "included print state @var{pstate}.")
1064 #define FUNC_NAME s_scm_port_with_print_state
1067 SCM_VALIDATE_OPORT_VALUE (1,port
);
1068 SCM_VALIDATE_PRINTSTATE (2,pstate
);
1069 port
= SCM_COERCE_OUTPORT (port
);
1070 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (scm_cons (port
, pstate
)));
1075 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1077 "Return the print state of the port @var{port}. If @var{port}\n"
1078 "has no associated print state, @code{#f} is returned.")
1079 #define FUNC_NAME s_scm_get_print_state
1081 if (SCM_PORT_WITH_PS_P (port
))
1082 return SCM_PORT_WITH_PS_PS (port
);
1083 if (SCM_OUTPUT_PORT_P (port
))
1085 SCM_WRONG_TYPE_ARG (1, port
);
1094 SCM vtable
, layout
, type
;
1096 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1098 scm_gc_register_root (&print_state_pool
);
1099 scm_gc_register_root (&scm_print_state_vtable
);
1100 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1101 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
1102 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1103 scm_set_struct_vtable_name_x (type
, scm_str2symbol ("print-state"));
1104 scm_print_state_vtable
= type
;
1106 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1107 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1108 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1109 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1111 #ifndef SCM_MAGIC_SNARFER
1112 #include "libguile/print.x"