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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
49 #include "libguile/_scm.h"
50 #include "libguile/chars.h"
51 #include "libguile/continuations.h"
52 #include "libguile/smob.h"
53 #include "libguile/eval.h"
54 #include "libguile/macros.h"
55 #include "libguile/procprop.h"
56 #include "libguile/read.h"
57 #include "libguile/weaks.h"
58 #include "libguile/unif.h"
59 #include "libguile/alist.h"
60 #include "libguile/struct.h"
61 #include "libguile/objects.h"
62 #include "libguile/ports.h"
63 #include "libguile/root.h"
64 #include "libguile/strings.h"
65 #include "libguile/strports.h"
66 #include "libguile/vectors.h"
68 #include "libguile/validate.h"
69 #include "libguile/print.h"
72 /* {Names of immediate symbols}
74 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
77 char *scm_isymnames
[] =
79 /* This table must agree with the declarations */
95 "#@literal-variable-ref",
96 "#@literal-variable-set!",
99 "#@call-with-current-continuation",
101 /* user visible ISYMS */
115 /* Multi-language support */
130 scm_option scm_print_opts
[] = {
131 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK(SCM_BOOL_F
),
132 "Hook for printing closures." },
133 { SCM_OPTION_BOOLEAN
, "source", 0,
134 "Print closures with source." }
137 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
139 "Option interface for the print options. Instead of using\n"
140 "this procedure directly, use the procedures\n"
141 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
142 "and @code{print-options}.")
143 #define FUNC_NAME s_scm_print_options
145 SCM ans
= scm_options (setting
,
154 /* {Printing of Scheme Objects}
157 /* Detection of circular references.
159 * Due to other constraints in the implementation, this code has bad
160 * time complexity (O (depth * N)), The printer code can be
161 * rewritten to be O(N).
163 #define PUSH_REF(pstate, obj) \
165 pstate->ref_stack[pstate->top++] = (obj); \
166 if (pstate->top == pstate->ceiling) \
167 grow_ref_stack (pstate); \
170 #define ENTER_NESTED_DATA(pstate, obj, label) \
172 register unsigned long i; \
173 for (i = 0; i < pstate->top; ++i) \
174 if (SCM_EQ_P (pstate->ref_stack[i], (obj))) \
176 if (pstate->fancyp) \
178 if (pstate->top - pstate->list_offset >= pstate->level) \
180 scm_putc ('#', port); \
184 PUSH_REF(pstate, obj); \
187 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
189 SCM scm_print_state_vtable
;
191 static SCM print_state_pool
;
193 #ifdef GUILE_DEBUG /* Used for debugging purposes */
195 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
197 "Return the current-pstate -- the cadr of the\n"
198 "@code{print_state_pool}. @code{current-pstate} is only\n"
199 "included in @code{--enable-guile-debug} builds.")
200 #define FUNC_NAME s_scm_current_pstate
202 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
203 return SCM_CADR (print_state_pool
);
211 #define PSTATE_SIZE 50L
214 make_print_state (void)
216 SCM print_state
= scm_make_struct (SCM_CAR (print_state_pool
), /* pstate type */
219 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
220 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
221 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
222 pstate
->ceiling
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
227 scm_make_print_state ()
229 SCM answer
= SCM_BOOL_F
;
231 /* First try to allocate a print state from the pool */
233 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
235 answer
= SCM_CADR (print_state_pool
);
236 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
240 return SCM_FALSEP (answer
) ? make_print_state () : answer
;
244 scm_free_print_state (SCM print_state
)
247 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
248 /* Cleanup before returning print state to pool.
249 * It is better to do it here. Doing it in scm_prin1
250 * would cost more since that function is called much more
254 pstate
->revealed
= 0;
255 SCM_NEWCELL (handle
);
257 SCM_SET_CELL_WORD_0 (handle
, print_state
);
258 SCM_SET_CELL_WORD_1 (handle
, SCM_CDR (print_state_pool
));
259 SCM_SETCDR (print_state_pool
, handle
);
264 grow_ref_stack (scm_print_state
*pstate
)
266 unsigned long int old_size
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
267 SCM
*old_elts
= SCM_VELTS (pstate
->ref_vect
);
268 unsigned long int new_size
= 2 * pstate
->ceiling
;
269 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
270 SCM
*new_elts
= SCM_VELTS (new_vect
);
273 for (i
= 0; i
!= old_size
; ++i
)
274 new_elts
[i
] = old_elts
[i
];
276 pstate
->ref_vect
= new_vect
;
277 pstate
->ref_stack
= new_elts
;
278 pstate
->ceiling
= new_size
;
283 print_circref (SCM port
,scm_print_state
*pstate
,SCM ref
)
286 int self
= pstate
->top
- 1;
288 if (SCM_CONSP (pstate
->ref_stack
[i
]))
292 if (SCM_NCONSP (pstate
->ref_stack
[i
- 1])
293 || !SCM_EQ_P (SCM_CDR (pstate
->ref_stack
[i
- 1]),
294 pstate
->ref_stack
[i
]))
300 for (i
= pstate
->top
- 1; 1; --i
)
301 if (SCM_EQ_P (pstate
->ref_stack
[i
], ref
))
303 scm_putc ('#', port
);
304 scm_intprint (i
- self
, 10, port
);
305 scm_putc ('#', port
);
308 /* Print generally. Handles both write and display according to PSTATE.
310 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
311 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
314 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
317 switch (SCM_ITAG3 (exp
))
319 case scm_tc3_closure
:
322 /* These tc3 tags should never occur in an immediate value. They are
323 * only used in cell types of non-immediates, i. e. the value returned
324 * by SCM_CELL_TYPE (exp) can use these tags.
326 scm_ipruk ("immediate", exp
, port
);
330 scm_intprint (SCM_INUM (exp
), 10, port
);
335 long i
= SCM_CHAR (exp
);
337 if (SCM_WRITINGP (pstate
))
339 scm_puts ("#\\", port
);
340 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
341 scm_puts (scm_charnames
[i
], port
);
343 else if (i
== '\177')
344 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
346 else if (i
< 0 || i
> '\177')
347 scm_intprint (i
, 8, port
);
354 else if (SCM_IFLAGP (exp
)
355 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
356 scm_puts (SCM_ISYMCHARS (exp
), port
);
357 else if (SCM_ILOCP (exp
))
359 scm_puts ("#@", port
);
360 scm_intprint (SCM_IFRAME (exp
), 10, port
);
361 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
362 scm_intprint (SCM_IDIST (exp
), 10, port
);
366 /* unknown immediate value */
367 scm_ipruk ("immediate", exp
, port
);
370 case scm_tc3_cons_gloc
:
372 scm_puts ("#@", port
);
373 exp
= SCM_GLOC_SYM (exp
);
376 switch (SCM_TYP7 (exp
))
378 case scm_tcs_cons_gloc
:
380 if (SCM_STRUCT_VTABLE_DATA (exp
) [scm_vtable_index_vcell
] == 0)
382 ENTER_NESTED_DATA (pstate
, exp
, circref
);
383 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
385 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
389 scm_tc16_port_with_ps
,
390 SCM_UNPACK (scm_cons (port
, pstate
->handle
)));
391 scm_call_generic_2 (print
, exp
, pwps
);
396 scm_print_struct (exp
, port
, pstate
);
398 EXIT_NESTED_DATA (pstate
);
402 case scm_tcs_cons_imcar
:
403 case scm_tcs_cons_nimcar
:
404 ENTER_NESTED_DATA (pstate
, exp
, circref
);
405 scm_iprlist ("(", exp
, ')', port
, pstate
);
406 EXIT_NESTED_DATA (pstate
);
409 print_circref (port
, pstate
, exp
);
412 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
414 case scm_tcs_closures
:
415 /* The user supplied print closure procedure must handle
416 macro closures as well. */
417 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
418 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
422 if (SCM_MACROP (exp
))
424 /* Printing a macro. */
426 name
= scm_macro_name (exp
);
427 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
429 code
= env
= SCM_UNDEFINED
;
430 scm_puts ("#<primitive-", port
);
434 code
= SCM_CODE (SCM_CDR (exp
));
435 env
= SCM_ENV (SCM_CDR (exp
));
436 scm_puts ("#<", port
);
438 if (SCM_CELL_WORD_0 (exp
) & (3L << 16))
439 scm_puts ("macro", port
);
441 scm_puts ("syntax", port
);
442 if (SCM_CELL_WORD_0 (exp
) & (2L << 16))
443 scm_putc ('!', port
);
447 /* Printing a closure. */
448 name
= scm_procedure_name (exp
);
449 code
= SCM_CODE (exp
);
451 scm_puts ("#<procedure", port
);
453 if (SCM_SYMBOLP (name
))
455 scm_putc (' ', port
);
456 scm_lfwrite (SCM_SYMBOL_CHARS (name
), SCM_SYMBOL_LENGTH (name
), port
);
458 else if (SCM_STRINGP (name
))
460 scm_putc (' ', port
);
461 scm_lfwrite (SCM_STRING_CHARS (name
), SCM_STRING_LENGTH (name
), port
);
463 if (!SCM_UNBNDP (code
))
465 if (SCM_PRINT_SOURCE_P
)
467 code
= scm_unmemocopy (code
,
468 SCM_EXTEND_ENV (SCM_CAR (code
),
471 ENTER_NESTED_DATA (pstate
, exp
, circref
);
472 scm_iprlist (" ", code
, '>', port
, pstate
);
473 EXIT_NESTED_DATA (pstate
);
477 if (SCM_TYP16 (exp
) != scm_tc16_macro
)
479 scm_putc (' ', port
);
480 scm_iprin1 (SCM_CAR (code
), port
, pstate
);
482 scm_putc ('>', port
);
486 scm_putc ('>', port
);
489 case scm_tc7_substring
:
491 if (SCM_WRITINGP (pstate
))
495 scm_putc ('"', port
);
496 for (i
= 0; i
< SCM_STRING_LENGTH (exp
); ++i
)
497 switch (SCM_STRING_CHARS (exp
)[i
])
501 scm_putc ('\\', port
);
503 scm_putc (SCM_STRING_CHARS (exp
)[i
], port
);
505 scm_putc ('"', port
);
509 scm_lfwrite (SCM_STRING_CHARS (exp
), SCM_STRING_LENGTH (exp
), port
);
521 len
= SCM_SYMBOL_LENGTH (exp
);
522 str
= SCM_SYMBOL_CHARS (exp
);
528 scm_lfwrite ("#{}#", 4, port
);
530 for (end
= pos
; end
< len
; ++end
)
533 #ifdef BRACKETS_AS_PARENS
541 case SCM_WHITE_SPACES
:
542 case SCM_LINE_INCREMENTORS
:
551 scm_lfwrite ("#{", 2, port
);
556 scm_lfwrite (str
+ pos
, end
- pos
, port
);
562 scm_lfwrite (buf
, 2, port
);
584 scm_lfwrite (str
+ pos
, end
- pos
, port
);
585 scm_remember_upto_here_1 (exp
);
587 scm_lfwrite ("}#", 2, port
);
591 ENTER_NESTED_DATA (pstate
, exp
, circref
);
592 if (SCM_IS_WHVEC (exp
))
593 scm_puts ("#wh(", port
);
595 scm_puts ("#w(", port
);
596 goto common_vector_printer
;
599 ENTER_NESTED_DATA (pstate
, exp
, circref
);
600 scm_puts ("#(", port
);
601 common_vector_printer
:
604 int last
= SCM_VECTOR_LENGTH (exp
) - 1;
606 if (pstate
->fancyp
&& SCM_VECTOR_LENGTH (exp
) > pstate
->length
)
608 last
= pstate
->length
- 1;
611 for (i
= 0; i
< last
; ++i
)
614 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
615 scm_putc (' ', port
);
620 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
623 scm_puts (" ...", port
);
624 scm_putc (')', port
);
626 EXIT_NESTED_DATA (pstate
);
637 #ifdef HAVE_LONG_LONGS
640 scm_raprin1 (exp
, port
, pstate
);
644 scm_puts (SCM_SUBR_GENERIC (exp
) && *SCM_SUBR_GENERIC (exp
)
645 ? "#<primitive-generic "
646 : "#<primitive-procedure ",
648 scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp
)), port
);
649 scm_putc ('>', port
);
654 SCM proc
= SCM_CCLO_SUBR (exp
);
655 if (SCM_EQ_P (proc
, scm_f_gsubr_apply
))
657 /* Print gsubrs as primitives */
658 SCM name
= scm_procedure_name (exp
);
659 scm_puts ("#<primitive-procedure", port
);
660 if (SCM_NFALSEP (name
))
662 scm_putc (' ', port
);
663 scm_puts (SCM_SYMBOL_CHARS (name
), port
);
668 scm_puts ("#<compiled-closure ", port
);
669 scm_iprin1 (proc
, port
, pstate
);
671 scm_putc ('>', port
);
676 scm_puts ("#<procedure-with-setter", port
);
678 SCM name
= scm_procedure_name (exp
);
679 if (SCM_NFALSEP (name
))
681 scm_putc (' ', port
);
682 scm_display (name
, port
);
685 scm_putc ('>', port
);
689 register long i
= SCM_PTOBNUM (exp
);
691 && scm_ptobs
[i
].print
692 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
699 ENTER_NESTED_DATA (pstate
, exp
, circref
);
700 i
= SCM_SMOBNUM (exp
);
701 if (i
< scm_numsmob
&& scm_smobs
[i
].print
702 && (scm_smobs
[i
].print
) (exp
, port
, pstate
))
704 EXIT_NESTED_DATA (pstate
);
707 EXIT_NESTED_DATA (pstate
);
708 /* Macros have their print field set to NULL. They are
709 handled at the same place as closures in order to achieve
710 non-redundancy. Placing the condition here won't slow
711 down printing of other smobs. */
712 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
717 scm_ipruk ("type", exp
, port
);
722 /* Print states are necessary for circular reference safe printing.
723 * They are also expensive to allocate. Therefore print states are
724 * kept in a pool so that they can be reused.
727 /* The PORT argument can also be a print-state/port pair, which will
728 * then be used instead of allocating a new print state. This is
729 * useful for continuing a chain of print calls from Scheme. */
732 scm_prin1 (SCM exp
, SCM port
, int writingp
)
734 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
736 scm_print_state
*pstate
;
738 /* If PORT is a print-state/port pair, use that. Else create a new
741 if (SCM_PORT_WITH_PS_P (port
))
743 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
744 port
= SCM_PORT_WITH_PS_PORT (port
);
748 /* First try to allocate a print state from the pool */
750 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
752 handle
= SCM_CDR (print_state_pool
);
753 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
756 if (SCM_FALSEP (handle
))
757 handle
= scm_cons (make_print_state (), SCM_EOL
);
758 pstate_scm
= SCM_CAR (handle
);
761 pstate
= SCM_PRINT_STATE (pstate_scm
);
762 pstate
->writingp
= writingp
;
763 scm_iprin1 (exp
, port
, pstate
);
765 /* Return print state to pool if it has been created above and
766 hasn't escaped to Scheme. */
768 if (!SCM_FALSEP (handle
) && !pstate
->revealed
)
771 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
772 SCM_SETCDR (print_state_pool
, handle
);
782 scm_intprint (long n
, int radix
, SCM port
)
784 char num_buf
[SCM_INTBUFLEN
];
785 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
788 /* Print an object of unrecognized type.
792 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
794 scm_puts ("#<unknown-", port
);
795 scm_puts (hdr
, port
);
798 scm_puts (" (0x", port
);
799 scm_intprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
800 scm_puts (" . 0x", port
);
801 scm_intprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
802 scm_puts (") @", port
);
804 scm_puts (" 0x", port
);
805 scm_intprint (SCM_UNPACK (ptr
), 16, port
);
806 scm_putc ('>', port
);
810 /* Print a list. The list may be either a list of ordinary data, or it may be
811 a list that represents code. Lists that represent code may contain gloc
815 scm_iprlist (char *hdr
,SCM exp
,int tlr
,SCM port
,scm_print_state
*pstate
)
817 register SCM hare
, tortoise
;
818 int floor
= pstate
->top
- 2;
819 scm_puts (hdr
, port
);
824 /* Run a hare and tortoise so that total time complexity will be
825 O(depth * N) instead of O(N^2). */
826 hare
= SCM_CDR (exp
);
828 while (SCM_ECONSP (hare
))
830 if (SCM_EQ_P (hare
, tortoise
))
832 hare
= SCM_CDR (hare
);
833 if (SCM_IMP (hare
) || SCM_NECONSP (hare
))
835 hare
= SCM_CDR (hare
);
836 tortoise
= SCM_CDR (tortoise
);
839 /* No cdr cycles intrinsic to this list */
840 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
841 for (exp
= SCM_CDR (exp
); SCM_ECONSP (exp
); exp
= SCM_CDR (exp
))
845 for (i
= floor
; i
>= 0; --i
)
846 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
848 PUSH_REF (pstate
, exp
);
849 scm_putc (' ', port
);
851 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
853 if (!SCM_NULLP (exp
))
855 scm_puts (" . ", port
);
856 scm_iprin1 (exp
, port
, pstate
);
860 scm_putc (tlr
, port
);
861 pstate
->top
= floor
+ 2;
866 int n
= pstate
->length
;
868 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
869 exp
= SCM_CDR (exp
); --n
;
870 for (; SCM_ECONSP (exp
); exp
= SCM_CDR (exp
))
872 register unsigned long i
;
874 for (i
= 0; i
< pstate
->top
; ++i
)
875 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
881 scm_puts (" ...", port
);
887 PUSH_REF(pstate
, exp
);
888 ++pstate
->list_offset
;
889 scm_putc (' ', port
);
891 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
894 if (SCM_NNULLP (exp
))
896 scm_puts (" . ", port
);
897 scm_iprin1 (exp
, port
, pstate
);
900 pstate
->list_offset
-= pstate
->top
- floor
- 2;
904 pstate
->list_offset
-= pstate
->top
- floor
- 2;
907 scm_puts (" . ", port
);
908 print_circref (port
, pstate
, exp
);
915 scm_valid_oport_value_p (SCM val
)
917 return (SCM_OPOUTPORTP (val
)
918 || (SCM_PORT_WITH_PS_P (val
)
919 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
922 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
925 scm_write (SCM obj
, SCM port
)
927 if (SCM_UNBNDP (port
))
930 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
932 scm_prin1 (obj
, port
, 1);
936 scm_close_port (port
);
939 return SCM_UNSPECIFIED
;
943 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
946 scm_display (SCM obj
, SCM port
)
948 if (SCM_UNBNDP (port
))
951 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
953 scm_prin1 (obj
, port
, 0);
957 scm_close_port (port
);
960 return SCM_UNSPECIFIED
;
964 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
965 (SCM destination
, SCM message
, SCM args
),
966 "Write @var{message} to @var{destination}, defaulting to\n"
967 "the current output port.\n"
968 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
969 "@code{~S} (was @code{%S}) escapes. When printed,\n"
970 "the escapes are replaced with corresponding members of\n"
972 "@code{~A} formats using @code{display} and @code{~S} formats\n"
973 "using @code{write}.\n"
974 "If @var{destination} is @code{#t}, then use the current output\n"
975 "port, if @var{destination} is @code{#f}, then return a string\n"
976 "containing the formatted text. Does not add a trailing newline.")
977 #define FUNC_NAME s_scm_simple_format
979 SCM answer
= SCM_UNSPECIFIED
;
980 int fReturnString
= 0;
986 if (SCM_EQ_P (destination
, SCM_BOOL_T
))
988 destination
= scm_cur_outp
;
990 else if (SCM_FALSEP (destination
))
993 destination
= scm_mkstrport (SCM_INUM0
,
994 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
1000 SCM_VALIDATE_OPORT_VALUE (1, destination
);
1001 destination
= SCM_COERCE_OUTPORT (destination
);
1003 SCM_VALIDATE_STRING (2, message
);
1004 SCM_VALIDATE_REST_ARGUMENT (args
);
1006 start
= SCM_STRING_CHARS (message
);
1007 end
= start
+ SCM_STRING_LENGTH (message
);
1008 for (p
= start
; p
!= end
; ++p
)
1011 if (!SCM_CONSP (args
))
1017 if (*p
== 'A' || *p
== 'a')
1019 else if (*p
== 'S' || *p
== 's')
1024 scm_lfwrite (start
, p
- start
- 1, destination
);
1025 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1026 args
= SCM_CDR (args
);
1029 scm_lfwrite (start
, p
- start
, destination
);
1032 answer
= scm_strport_to_string (destination
);
1034 return scm_return_first (answer
, message
);
1039 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1041 "Send a newline to @var{port}.")
1042 #define FUNC_NAME s_scm_newline
1044 if (SCM_UNBNDP (port
))
1045 port
= scm_cur_outp
;
1047 SCM_VALIDATE_OPORT_VALUE (1,port
);
1049 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1050 return SCM_UNSPECIFIED
;
1054 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1055 (SCM chr
, SCM port
),
1056 "Send character @var{chr} to @var{port}.")
1057 #define FUNC_NAME s_scm_write_char
1059 if (SCM_UNBNDP (port
))
1060 port
= scm_cur_outp
;
1062 SCM_VALIDATE_CHAR (1,chr
);
1063 SCM_VALIDATE_OPORT_VALUE (2,port
);
1065 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1069 scm_close_port (port
);
1072 return SCM_UNSPECIFIED
;
1078 /* Call back to Scheme code to do the printing of special objects
1079 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1080 * containing PORT and PSTATE. This object can be used as the port for
1081 * display/write etc to continue the current print chain. The REVEALED
1082 * field of PSTATE is set to true to indicate that the print state has
1083 * escaped to Scheme and thus has to be freed by the GC.
1086 scm_bits_t scm_tc16_port_with_ps
;
1088 /* Print exactly as the port itself would */
1091 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1093 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1094 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1098 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1101 SCM pair
= scm_cons (port
, pstate
->handle
);
1102 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (pair
));
1103 pstate
->revealed
= 1;
1104 return scm_apply (proc
, exp
, scm_cons (pwps
, scm_listofnull
));
1107 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 2, 0, 0,
1108 (SCM port
, SCM pstate
),
1109 "Create a new port which behaves like @var{port}, but with an\n"
1110 "included print state @var{pstate}.")
1111 #define FUNC_NAME s_scm_port_with_print_state
1114 SCM_VALIDATE_OPORT_VALUE (1,port
);
1115 SCM_VALIDATE_PRINTSTATE (2,pstate
);
1116 port
= SCM_COERCE_OUTPORT (port
);
1117 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (scm_cons (port
, pstate
)));
1122 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1124 "Return the print state of the port @var{port}. If @var{port}\n"
1125 "has no associated print state, @code{#f} is returned.")
1126 #define FUNC_NAME s_scm_get_print_state
1128 if (SCM_PORT_WITH_PS_P (port
))
1129 return SCM_PORT_WITH_PS_PS (port
);
1130 if (SCM_OUTPUT_PORT_P (port
))
1132 SCM_WRONG_TYPE_ARG (1, port
);
1141 SCM vtable
, layout
, type
;
1143 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1144 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1145 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
1146 type
= scm_make_struct (vtable
, SCM_INUM0
, SCM_LIST1 (layout
));
1147 scm_set_struct_vtable_name_x (type
, scm_str2symbol ("print-state"));
1148 print_state_pool
= scm_permanent_object (scm_cons (type
, SCM_EOL
));
1150 scm_print_state_vtable
= type
;
1152 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1153 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1154 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1155 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1157 #ifndef SCM_MAGIC_SNARFER
1158 #include "libguile/print.x"