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;
252 SCM_NEWCELL (handle
);
254 SCM_SET_CELL_WORD_0 (handle
, print_state
);
255 SCM_SET_CELL_WORD_1 (handle
, print_state_pool
);
256 print_state_pool
= handle
;
261 grow_ref_stack (scm_print_state
*pstate
)
263 unsigned long int old_size
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
264 SCM
*old_elts
= SCM_VELTS (pstate
->ref_vect
);
265 unsigned long int new_size
= 2 * pstate
->ceiling
;
266 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
267 SCM
*new_elts
= SCM_VELTS (new_vect
);
270 for (i
= 0; i
!= old_size
; ++i
)
271 new_elts
[i
] = old_elts
[i
];
273 pstate
->ref_vect
= new_vect
;
274 pstate
->ref_stack
= new_elts
;
275 pstate
->ceiling
= new_size
;
280 print_circref (SCM port
,scm_print_state
*pstate
,SCM ref
)
283 long self
= pstate
->top
- 1;
285 if (SCM_CONSP (pstate
->ref_stack
[i
]))
289 if (!SCM_CONSP (pstate
->ref_stack
[i
- 1])
290 || !SCM_EQ_P (SCM_CDR (pstate
->ref_stack
[i
- 1]),
291 pstate
->ref_stack
[i
]))
297 for (i
= pstate
->top
- 1; 1; --i
)
298 if (SCM_EQ_P (pstate
->ref_stack
[i
], ref
))
300 scm_putc ('#', port
);
301 scm_intprint (i
- self
, 10, port
);
302 scm_putc ('#', port
);
305 /* Print the name of a symbol. */
308 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
320 /* XXX - Lots of weird symbol names are missed, such as "12" or
324 scm_lfwrite ("#{}#", 4, port
);
325 else if (str
[0] == '#' || str
[0] == ':' || str
[len
-1] == ':')
327 scm_lfwrite ("#{", 2, port
);
331 for (end
= pos
; end
< len
; ++end
)
334 #ifdef BRACKETS_AS_PARENS
342 case SCM_WHITE_SPACES
:
343 case SCM_LINE_INCREMENTORS
:
352 scm_lfwrite ("#{", 2, port
);
357 scm_lfwrite (str
+ pos
, end
- pos
, port
);
363 scm_lfwrite (buf
, 2, port
);
385 scm_lfwrite (str
+ pos
, end
- pos
, port
);
387 scm_lfwrite ("}#", 2, port
);
390 /* Print generally. Handles both write and display according to PSTATE.
392 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
393 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
396 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
398 switch (SCM_ITAG3 (exp
))
400 case scm_tc3_closure
:
403 /* These tc3 tags should never occur in an immediate value. They are
404 * only used in cell types of non-immediates, i. e. the value returned
405 * by SCM_CELL_TYPE (exp) can use these tags.
407 scm_ipruk ("immediate", exp
, port
);
411 scm_intprint (SCM_INUM (exp
), 10, port
);
416 long i
= SCM_CHAR (exp
);
418 if (SCM_WRITINGP (pstate
))
420 scm_puts ("#\\", port
);
421 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
422 scm_puts (scm_charnames
[i
], port
);
424 else if (i
== '\177')
425 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
427 else if (i
< 0 || i
> '\177')
428 scm_intprint (i
, 8, port
);
435 else if (SCM_IFLAGP (exp
)
436 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
437 scm_puts (SCM_ISYMCHARS (exp
), port
);
438 else if (SCM_ILOCP (exp
))
440 scm_puts ("#@", port
);
441 scm_intprint ((long) SCM_IFRAME (exp
), 10, port
);
442 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
443 scm_intprint ((long) SCM_IDIST (exp
), 10, port
);
447 /* unknown immediate value */
448 scm_ipruk ("immediate", exp
, port
);
452 switch (SCM_TYP7 (exp
))
456 ENTER_NESTED_DATA (pstate
, exp
, circref
);
457 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
459 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
463 scm_tc16_port_with_ps
,
464 SCM_UNPACK (scm_cons (port
, pstate
->handle
)));
465 pstate
->revealed
= 1;
466 scm_call_generic_2 (print
, exp
, pwps
);
471 scm_print_struct (exp
, port
, pstate
);
473 EXIT_NESTED_DATA (pstate
);
476 case scm_tcs_cons_imcar
:
477 case scm_tcs_cons_nimcar
:
478 ENTER_NESTED_DATA (pstate
, exp
, circref
);
479 scm_iprlist ("(", exp
, ')', port
, pstate
);
480 EXIT_NESTED_DATA (pstate
);
483 print_circref (port
, pstate
, exp
);
485 case scm_tcs_closures
:
486 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
487 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
490 SCM formals
= SCM_CLOSURE_FORMALS (exp
);
491 scm_puts ("#<procedure", port
);
492 scm_putc (' ', port
);
493 scm_iprin1 (scm_procedure_name (exp
), port
, pstate
);
494 scm_putc (' ', port
);
495 if (SCM_PRINT_SOURCE_P
)
497 SCM env
= SCM_ENV (exp
);
498 SCM xenv
= SCM_EXTEND_ENV (formals
, SCM_EOL
, env
);
499 SCM src
= scm_unmemocopy (SCM_CODE (exp
), xenv
);
500 ENTER_NESTED_DATA (pstate
, exp
, circref
);
501 scm_iprin1 (src
, port
, pstate
);
502 EXIT_NESTED_DATA (pstate
);
505 scm_iprin1 (formals
, port
, pstate
);
506 scm_putc ('>', port
);
510 if (SCM_WRITINGP (pstate
))
514 scm_putc ('"', port
);
515 for (i
= 0; i
< SCM_STRING_LENGTH (exp
); ++i
)
516 switch (SCM_STRING_CHARS (exp
)[i
])
520 scm_putc ('\\', port
);
522 scm_putc (SCM_STRING_CHARS (exp
)[i
], port
);
524 scm_putc ('"', port
);
528 scm_lfwrite (SCM_STRING_CHARS (exp
), SCM_STRING_LENGTH (exp
), port
);
531 scm_print_symbol_name (SCM_SYMBOL_CHARS (exp
),
532 SCM_SYMBOL_LENGTH (exp
),
534 scm_remember_upto_here_1 (exp
);
536 case scm_tc7_variable
:
537 scm_i_variable_print (exp
, port
, pstate
);
540 ENTER_NESTED_DATA (pstate
, exp
, circref
);
541 if (SCM_IS_WHVEC (exp
))
542 scm_puts ("#wh(", port
);
544 scm_puts ("#w(", port
);
545 goto common_vector_printer
;
548 ENTER_NESTED_DATA (pstate
, exp
, circref
);
549 scm_puts ("#(", port
);
550 common_vector_printer
:
553 long last
= SCM_VECTOR_LENGTH (exp
) - 1;
555 if (pstate
->fancyp
&& SCM_VECTOR_LENGTH (exp
) > pstate
->length
)
557 last
= pstate
->length
- 1;
560 for (i
= 0; i
< last
; ++i
)
563 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
564 scm_putc (' ', port
);
569 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
572 scm_puts (" ...", port
);
573 scm_putc (')', port
);
575 EXIT_NESTED_DATA (pstate
);
586 #ifdef HAVE_LONG_LONGS
589 scm_raprin1 (exp
, port
, pstate
);
593 scm_puts (SCM_SUBR_GENERIC (exp
) && *SCM_SUBR_GENERIC (exp
)
594 ? "#<primitive-generic "
595 : "#<primitive-procedure ",
597 scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp
)), port
);
598 scm_putc ('>', port
);
603 SCM proc
= SCM_CCLO_SUBR (exp
);
604 if (SCM_EQ_P (proc
, scm_f_gsubr_apply
))
606 /* Print gsubrs as primitives */
607 SCM name
= scm_procedure_name (exp
);
608 scm_puts ("#<primitive-procedure", port
);
609 if (!SCM_FALSEP (name
))
611 scm_putc (' ', port
);
612 scm_puts (SCM_SYMBOL_CHARS (name
), port
);
617 scm_puts ("#<compiled-closure ", port
);
618 scm_iprin1 (proc
, port
, pstate
);
620 scm_putc ('>', port
);
625 scm_puts ("#<procedure-with-setter", port
);
627 SCM name
= scm_procedure_name (exp
);
628 if (!SCM_FALSEP (name
))
630 scm_putc (' ', port
);
631 scm_display (name
, port
);
634 scm_putc ('>', port
);
638 register long i
= SCM_PTOBNUM (exp
);
640 && scm_ptobs
[i
].print
641 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
646 ENTER_NESTED_DATA (pstate
, exp
, circref
);
647 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
648 EXIT_NESTED_DATA (pstate
);
652 scm_ipruk ("type", exp
, port
);
657 /* Print states are necessary for circular reference safe printing.
658 * They are also expensive to allocate. Therefore print states are
659 * kept in a pool so that they can be reused.
662 /* The PORT argument can also be a print-state/port pair, which will
663 * then be used instead of allocating a new print state. This is
664 * useful for continuing a chain of print calls from Scheme. */
667 scm_prin1 (SCM exp
, SCM port
, int writingp
)
669 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
671 scm_print_state
*pstate
;
673 /* If PORT is a print-state/port pair, use that. Else create a new
676 if (SCM_PORT_WITH_PS_P (port
))
678 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
679 port
= SCM_PORT_WITH_PS_PORT (port
);
683 /* First try to allocate a print state from the pool */
685 if (!SCM_NULLP (print_state_pool
))
687 handle
= print_state_pool
;
688 print_state_pool
= SCM_CDR (print_state_pool
);
691 if (SCM_FALSEP (handle
))
692 handle
= scm_list_1 (make_print_state ());
693 pstate_scm
= SCM_CAR (handle
);
696 pstate
= SCM_PRINT_STATE (pstate_scm
);
697 pstate
->writingp
= writingp
;
698 scm_iprin1 (exp
, port
, pstate
);
700 /* Return print state to pool if it has been created above and
701 hasn't escaped to Scheme. */
703 if (!SCM_FALSEP (handle
) && !pstate
->revealed
)
706 SCM_SETCDR (handle
, print_state_pool
);
707 print_state_pool
= handle
;
717 scm_intprint (long n
, int radix
, SCM port
)
719 char num_buf
[SCM_INTBUFLEN
];
720 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
723 /* Print an object of unrecognized type.
727 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
729 scm_puts ("#<unknown-", port
);
730 scm_puts (hdr
, port
);
733 scm_puts (" (0x", port
);
734 scm_intprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
735 scm_puts (" . 0x", port
);
736 scm_intprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
737 scm_puts (") @", port
);
739 scm_puts (" 0x", port
);
740 scm_intprint (SCM_UNPACK (ptr
), 16, port
);
741 scm_putc ('>', port
);
748 scm_iprlist (char *hdr
,SCM exp
,int tlr
,SCM port
,scm_print_state
*pstate
)
750 register SCM hare
, tortoise
;
751 long floor
= pstate
->top
- 2;
752 scm_puts (hdr
, port
);
757 /* Run a hare and tortoise so that total time complexity will be
758 O(depth * N) instead of O(N^2). */
759 hare
= SCM_CDR (exp
);
761 while (SCM_CONSP (hare
))
763 if (SCM_EQ_P (hare
, tortoise
))
765 hare
= SCM_CDR (hare
);
766 if (!SCM_CONSP (hare
))
768 hare
= SCM_CDR (hare
);
769 tortoise
= SCM_CDR (tortoise
);
772 /* No cdr cycles intrinsic to this list */
773 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
774 for (exp
= SCM_CDR (exp
); SCM_CONSP (exp
); exp
= SCM_CDR (exp
))
778 for (i
= floor
; i
>= 0; --i
)
779 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
781 PUSH_REF (pstate
, exp
);
782 scm_putc (' ', port
);
784 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
786 if (!SCM_NULLP (exp
))
788 scm_puts (" . ", port
);
789 scm_iprin1 (exp
, port
, pstate
);
793 scm_putc (tlr
, port
);
794 pstate
->top
= floor
+ 2;
799 long n
= pstate
->length
;
801 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
802 exp
= SCM_CDR (exp
); --n
;
803 for (; SCM_CONSP (exp
); exp
= SCM_CDR (exp
))
805 register unsigned long i
;
807 for (i
= 0; i
< pstate
->top
; ++i
)
808 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
814 scm_puts (" ...", port
);
820 PUSH_REF(pstate
, exp
);
821 ++pstate
->list_offset
;
822 scm_putc (' ', port
);
824 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
827 if (!SCM_NULLP (exp
))
829 scm_puts (" . ", port
);
830 scm_iprin1 (exp
, port
, pstate
);
833 pstate
->list_offset
-= pstate
->top
- floor
- 2;
837 pstate
->list_offset
-= pstate
->top
- floor
- 2;
840 scm_puts (" . ", port
);
841 print_circref (port
, pstate
, exp
);
848 scm_valid_oport_value_p (SCM val
)
850 return (SCM_OPOUTPORTP (val
)
851 || (SCM_PORT_WITH_PS_P (val
)
852 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
855 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
858 scm_write (SCM obj
, SCM port
)
860 if (SCM_UNBNDP (port
))
863 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
865 scm_prin1 (obj
, port
, 1);
869 scm_close_port (port
);
872 return SCM_UNSPECIFIED
;
876 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
879 scm_display (SCM obj
, SCM port
)
881 if (SCM_UNBNDP (port
))
884 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
886 scm_prin1 (obj
, port
, 0);
890 scm_close_port (port
);
893 return SCM_UNSPECIFIED
;
897 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
898 (SCM destination
, SCM message
, SCM args
),
899 "Write @var{message} to @var{destination}, defaulting to\n"
900 "the current output port.\n"
901 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
902 "@code{~S} (was @code{%S}) escapes. When printed,\n"
903 "the escapes are replaced with corresponding members of\n"
905 "@code{~A} formats using @code{display} and @code{~S} formats\n"
906 "using @code{write}.\n"
907 "If @var{destination} is @code{#t}, then use the current output\n"
908 "port, if @var{destination} is @code{#f}, then return a string\n"
909 "containing the formatted text. Does not add a trailing newline.")
910 #define FUNC_NAME s_scm_simple_format
912 SCM answer
= SCM_UNSPECIFIED
;
913 int fReturnString
= 0;
919 if (SCM_EQ_P (destination
, SCM_BOOL_T
))
921 destination
= scm_cur_outp
;
923 else if (SCM_FALSEP (destination
))
926 destination
= scm_mkstrport (SCM_INUM0
,
927 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
933 SCM_VALIDATE_OPORT_VALUE (1, destination
);
934 destination
= SCM_COERCE_OUTPORT (destination
);
936 SCM_VALIDATE_STRING (2, message
);
937 SCM_VALIDATE_REST_ARGUMENT (args
);
939 start
= SCM_STRING_CHARS (message
);
940 end
= start
+ SCM_STRING_LENGTH (message
);
941 for (p
= start
; p
!= end
; ++p
)
956 scm_lfwrite (start
, p
- start
, destination
);
960 scm_newline (destination
);
964 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
965 scm_list_1 (SCM_MAKE_CHAR (*p
)));
970 if (!SCM_CONSP (args
))
971 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
972 scm_list_1 (SCM_MAKE_CHAR (*p
)));
974 scm_lfwrite (start
, p
- start
- 1, destination
);
975 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
976 args
= SCM_CDR (args
);
980 scm_lfwrite (start
, p
- start
, destination
);
981 if (!SCM_EQ_P (args
, SCM_EOL
))
982 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
983 scm_list_1 (scm_length (args
)));
986 answer
= scm_strport_to_string (destination
);
988 return scm_return_first (answer
, message
);
993 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
995 "Send a newline to @var{port}.")
996 #define FUNC_NAME s_scm_newline
998 if (SCM_UNBNDP (port
))
1001 SCM_VALIDATE_OPORT_VALUE (1,port
);
1003 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1004 return SCM_UNSPECIFIED
;
1008 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1009 (SCM chr
, SCM port
),
1010 "Send character @var{chr} to @var{port}.")
1011 #define FUNC_NAME s_scm_write_char
1013 if (SCM_UNBNDP (port
))
1014 port
= scm_cur_outp
;
1016 SCM_VALIDATE_CHAR (1,chr
);
1017 SCM_VALIDATE_OPORT_VALUE (2,port
);
1019 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1023 scm_close_port (port
);
1026 return SCM_UNSPECIFIED
;
1032 /* Call back to Scheme code to do the printing of special objects
1033 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1034 * containing PORT and PSTATE. This object can be used as the port for
1035 * display/write etc to continue the current print chain. The REVEALED
1036 * field of PSTATE is set to true to indicate that the print state has
1037 * escaped to Scheme and thus has to be freed by the GC.
1040 scm_t_bits scm_tc16_port_with_ps
;
1042 /* Print exactly as the port itself would */
1045 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1047 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1048 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1052 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1055 SCM pair
= scm_cons (port
, pstate
->handle
);
1056 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (pair
));
1057 pstate
->revealed
= 1;
1058 return scm_call_2 (proc
, exp
, pwps
);
1061 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 2, 0, 0,
1062 (SCM port
, SCM pstate
),
1063 "Create a new port which behaves like @var{port}, but with an\n"
1064 "included print state @var{pstate}.")
1065 #define FUNC_NAME s_scm_port_with_print_state
1068 SCM_VALIDATE_OPORT_VALUE (1,port
);
1069 SCM_VALIDATE_PRINTSTATE (2,pstate
);
1070 port
= SCM_COERCE_OUTPORT (port
);
1071 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (scm_cons (port
, pstate
)));
1076 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1078 "Return the print state of the port @var{port}. If @var{port}\n"
1079 "has no associated print state, @code{#f} is returned.")
1080 #define FUNC_NAME s_scm_get_print_state
1082 if (SCM_PORT_WITH_PS_P (port
))
1083 return SCM_PORT_WITH_PS_PS (port
);
1084 if (SCM_OUTPUT_PORT_P (port
))
1086 SCM_WRONG_TYPE_ARG (1, port
);
1095 SCM vtable
, layout
, type
;
1097 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1099 scm_gc_register_root (&print_state_pool
);
1100 scm_gc_register_root (&scm_print_state_vtable
);
1101 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1102 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
1103 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1104 scm_set_struct_vtable_name_x (type
, scm_str2symbol ("print-state"));
1105 scm_print_state_vtable
= type
;
1107 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1108 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1109 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1110 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1112 #ifndef SCM_MAGIC_SNARFER
1113 #include "libguile/print.x"