1 /* Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48 #include "libguile/_scm.h"
49 #include "libguile/chars.h"
50 #include "libguile/continuations.h"
51 #include "libguile/smob.h"
52 #include "libguile/eval.h"
53 #include "libguile/macros.h"
54 #include "libguile/procprop.h"
55 #include "libguile/read.h"
56 #include "libguile/weaks.h"
57 #include "libguile/unif.h"
58 #include "libguile/alist.h"
59 #include "libguile/struct.h"
60 #include "libguile/objects.h"
61 #include "libguile/ports.h"
62 #include "libguile/root.h"
63 #include "libguile/strings.h"
64 #include "libguile/strports.h"
65 #include "libguile/vectors.h"
67 #include "libguile/validate.h"
68 #include "libguile/print.h"
71 /* {Names of immediate symbols}
73 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
76 char *scm_isymnames
[] =
78 /* This table must agree with the declarations */
94 "#@literal-variable-ref",
95 "#@literal-variable-set!",
98 "#@call-with-current-continuation",
100 /* user visible ISYMS */
114 /* Multi-language support */
129 scm_option scm_print_opts
[] = {
130 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK(SCM_BOOL_F
),
131 "Hook for printing closures." },
132 { SCM_OPTION_BOOLEAN
, "source", 0,
133 "Print closures with source." }
136 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
139 #define FUNC_NAME s_scm_print_options
141 SCM ans
= scm_options (setting
,
150 /* {Printing of Scheme Objects}
153 /* Detection of circular references.
155 * Due to other constraints in the implementation, this code has bad
156 * time complexity (O (depth * N)), The printer code can be
157 * rewritten to be O(N).
159 #define PUSH_REF(pstate, obj) \
161 pstate->ref_stack[pstate->top++] = (obj); \
162 if (pstate->top == pstate->ceiling) \
163 grow_ref_stack (pstate); \
166 #define ENTER_NESTED_DATA(pstate, obj, label) \
168 register unsigned long i; \
169 for (i = 0; i < pstate->top; ++i) \
170 if (SCM_EQ_P (pstate->ref_stack[i], (obj))) \
172 if (pstate->fancyp) \
174 if (pstate->top - pstate->list_offset >= pstate->level) \
176 scm_putc ('#', port); \
180 PUSH_REF(pstate, obj); \
183 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
185 SCM scm_print_state_vtable
;
187 static SCM print_state_pool
;
189 #ifdef GUILE_DEBUG /* Used for debugging purposes */
191 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
193 "Return the current-pstate--the `cadr' of the print_state_pool.\n"
194 "`current-pstate' is only included in GUILE_DEBUG builds.")
195 #define FUNC_NAME s_scm_current_pstate
197 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
198 return SCM_CADR (print_state_pool
);
206 #define PSTATE_SIZE 50L
209 make_print_state (void)
211 SCM print_state
= scm_make_struct (SCM_CAR (print_state_pool
), /* pstate type */
214 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
215 pstate
->ref_vect
= scm_make_vector (SCM_MAKINUM (PSTATE_SIZE
),
217 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
218 pstate
->ceiling
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
223 scm_make_print_state ()
225 SCM answer
= SCM_BOOL_F
;
227 /* First try to allocate a print state from the pool */
229 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
231 answer
= SCM_CADR (print_state_pool
);
232 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
236 return SCM_FALSEP (answer
) ? make_print_state () : answer
;
240 scm_free_print_state (SCM print_state
)
243 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
244 /* Cleanup before returning print state to pool.
245 * It is better to do it here. Doing it in scm_prin1
246 * would cost more since that function is called much more
250 pstate
->revealed
= 0;
251 SCM_NEWCELL (handle
);
253 SCM_SETCAR (handle
, print_state
);
254 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
255 SCM_SETCDR (print_state_pool
, handle
);
260 grow_ref_stack (scm_print_state
*pstate
)
262 unsigned long int old_size
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
263 SCM
*old_elts
= SCM_VELTS (pstate
->ref_vect
);
264 unsigned long int new_size
= 2 * pstate
->ceiling
;
265 SCM new_vect
= scm_make_vector (SCM_MAKINUM (new_size
), SCM_UNDEFINED
);
266 SCM
*new_elts
= SCM_VELTS (new_vect
);
269 for (i
= 0; i
!= old_size
; ++i
)
270 new_elts
[i
] = old_elts
[i
];
272 pstate
->ref_vect
= new_vect
;
273 pstate
->ref_stack
= new_elts
;
274 pstate
->ceiling
= new_size
;
279 print_circref (SCM port
,scm_print_state
*pstate
,SCM ref
)
282 int self
= pstate
->top
- 1;
284 if (SCM_CONSP (pstate
->ref_stack
[i
]))
288 if (SCM_NCONSP (pstate
->ref_stack
[i
- 1])
289 || !SCM_EQ_P (SCM_CDR (pstate
->ref_stack
[i
- 1]),
290 pstate
->ref_stack
[i
]))
296 for (i
= pstate
->top
- 1; 1; --i
)
297 if (SCM_EQ_P (pstate
->ref_stack
[i
], ref
))
299 scm_putc ('#', port
);
300 scm_intprint (i
- self
, 10, port
);
301 scm_putc ('#', port
);
304 /* Print generally. Handles both write and display according to PSTATE.
306 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
307 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
310 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
313 switch (SCM_ITAG3 (exp
))
315 case scm_tc3_closure
:
318 /* These tc3 tags should never occur in an immediate value. They are
319 * only used in cell types of non-immediates, i. e. the value returned
320 * by SCM_CELL_TYPE (exp) can use these tags.
322 scm_ipruk ("immediate", exp
, port
);
326 scm_intprint (SCM_INUM (exp
), 10, port
);
331 long i
= SCM_CHAR (exp
);
333 if (SCM_WRITINGP (pstate
))
335 scm_puts ("#\\", port
);
336 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
337 scm_puts (scm_charnames
[i
], port
);
339 else if (i
== '\177')
340 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
342 else if (i
< 0 || i
> '\177')
343 scm_intprint (i
, 8, port
);
350 else if (SCM_IFLAGP (exp
)
351 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
352 scm_puts (SCM_ISYMCHARS (exp
), port
);
353 else if (SCM_ILOCP (exp
))
355 scm_puts ("#@", port
);
356 scm_intprint (SCM_IFRAME (exp
), 10, port
);
357 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
358 scm_intprint (SCM_IDIST (exp
), 10, port
);
362 /* unknown immediate value */
363 scm_ipruk ("immediate", exp
, port
);
366 case scm_tc3_cons_gloc
:
368 scm_puts ("#@", port
);
369 exp
= SCM_GLOC_SYM (exp
);
372 switch (SCM_TYP7 (exp
))
374 case scm_tcs_cons_gloc
:
376 if (SCM_STRUCT_VTABLE_DATA (exp
) [scm_vtable_index_vcell
] == 0)
378 ENTER_NESTED_DATA (pstate
, exp
, circref
);
379 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
381 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
385 scm_tc16_port_with_ps
,
386 SCM_UNPACK (scm_cons (port
, pstate
->handle
)));
387 scm_call_generic_2 (print
, exp
, pwps
);
392 scm_print_struct (exp
, port
, pstate
);
394 EXIT_NESTED_DATA (pstate
);
398 case scm_tcs_cons_imcar
:
399 case scm_tcs_cons_nimcar
:
400 ENTER_NESTED_DATA (pstate
, exp
, circref
);
401 scm_iprlist ("(", exp
, ')', port
, pstate
);
402 EXIT_NESTED_DATA (pstate
);
405 print_circref (port
, pstate
, exp
);
408 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
410 case scm_tcs_closures
:
411 /* The user supplied print closure procedure must handle
412 macro closures as well. */
413 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
414 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
418 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
420 /* Printing a macro. */
422 name
= scm_macro_name (exp
);
423 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
425 code
= env
= SCM_UNDEFINED
;
426 scm_puts ("#<primitive-", port
);
430 code
= SCM_CODE (SCM_CDR (exp
));
431 env
= SCM_ENV (SCM_CDR (exp
));
432 scm_puts ("#<", port
);
434 if (SCM_CELL_WORD_0 (exp
) & (3L << 16))
435 scm_puts ("macro", port
);
437 scm_puts ("syntax", port
);
438 if (SCM_CELL_WORD_0 (exp
) & (2L << 16))
439 scm_putc ('!', port
);
443 /* Printing a closure. */
444 name
= scm_procedure_name (exp
);
445 code
= SCM_CODE (exp
);
447 scm_puts ("#<procedure", port
);
449 if (SCM_SYMBOLP (name
))
451 scm_putc (' ', port
);
452 scm_lfwrite (SCM_SYMBOL_CHARS (name
), SCM_SYMBOL_LENGTH (name
), port
);
454 else if (SCM_STRINGP (name
))
456 scm_putc (' ', port
);
457 scm_lfwrite (SCM_STRING_CHARS (name
), SCM_STRING_LENGTH (name
), port
);
459 if (!SCM_UNBNDP (code
))
461 if (SCM_PRINT_SOURCE_P
)
463 code
= scm_unmemocopy (code
,
464 SCM_EXTEND_ENV (SCM_CAR (code
),
467 ENTER_NESTED_DATA (pstate
, exp
, circref
);
468 scm_iprlist (" ", code
, '>', port
, pstate
);
469 EXIT_NESTED_DATA (pstate
);
473 if (SCM_TYP16 (exp
) != scm_tc16_macro
)
475 scm_putc (' ', port
);
476 scm_iprin1 (SCM_CAR (code
), port
, pstate
);
478 scm_putc ('>', port
);
482 scm_putc ('>', port
);
485 case scm_tc7_substring
:
487 if (SCM_WRITINGP (pstate
))
491 scm_putc ('"', port
);
492 for (i
= 0; i
< SCM_STRING_LENGTH (exp
); ++i
)
493 switch (SCM_STRING_CHARS (exp
)[i
])
497 scm_putc ('\\', port
);
499 scm_putc (SCM_STRING_CHARS (exp
)[i
], port
);
501 scm_putc ('"', port
);
505 scm_lfwrite (SCM_STRING_CHARS (exp
), SCM_STRING_LENGTH (exp
), port
);
517 len
= SCM_SYMBOL_LENGTH (exp
);
518 str
= SCM_SYMBOL_CHARS (exp
);
524 scm_lfwrite ("#{}#", 4, port
);
526 for (end
= pos
; end
< len
; ++end
)
529 #ifdef BRACKETS_AS_PARENS
537 case SCM_WHITE_SPACES
:
538 case SCM_LINE_INCREMENTORS
:
547 scm_lfwrite ("#{", 2, port
);
552 scm_lfwrite (str
+ pos
, end
- pos
, port
);
558 scm_lfwrite (buf
, 2, port
);
580 scm_lfwrite (str
+ pos
, end
- pos
, port
);
583 scm_lfwrite ("}#", 2, port
);
587 ENTER_NESTED_DATA (pstate
, exp
, circref
);
588 if (SCM_IS_WHVEC (exp
))
589 scm_puts ("#wh(", port
);
591 scm_puts ("#w(", port
);
592 goto common_vector_printer
;
595 ENTER_NESTED_DATA (pstate
, exp
, circref
);
596 scm_puts ("#(", port
);
597 common_vector_printer
:
600 int last
= SCM_VECTOR_LENGTH (exp
) - 1;
602 if (pstate
->fancyp
&& SCM_VECTOR_LENGTH (exp
) > pstate
->length
)
604 last
= pstate
->length
- 1;
607 for (i
= 0; i
< last
; ++i
)
610 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
611 scm_putc (' ', port
);
616 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
619 scm_puts (" ...", port
);
620 scm_putc (')', port
);
622 EXIT_NESTED_DATA (pstate
);
633 #ifdef HAVE_LONG_LONGS
636 scm_raprin1 (exp
, port
, pstate
);
640 scm_puts (SCM_SUBR_GENERIC (exp
) && *SCM_SUBR_GENERIC (exp
)
641 ? "#<primitive-generic "
642 : "#<primitive-procedure ",
644 scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp
)), port
);
645 scm_putc ('>', port
);
650 SCM proc
= SCM_CCLO_SUBR (exp
);
651 if (SCM_EQ_P (proc
, scm_f_gsubr_apply
))
653 /* Print gsubrs as primitives */
654 SCM name
= scm_procedure_name (exp
);
655 scm_puts ("#<primitive-procedure", port
);
656 if (SCM_NFALSEP (name
))
658 scm_putc (' ', port
);
659 scm_puts (SCM_SYMBOL_CHARS (name
), port
);
664 scm_puts ("#<compiled-closure ", port
);
665 scm_iprin1 (proc
, port
, pstate
);
667 scm_putc ('>', port
);
672 scm_puts ("#<procedure-with-setter", port
);
674 SCM name
= scm_procedure_name (exp
);
675 if (SCM_NFALSEP (name
))
677 scm_putc (' ', port
);
678 scm_display (name
, port
);
681 scm_putc ('>', port
);
685 register long i
= SCM_PTOBNUM (exp
);
687 && scm_ptobs
[i
].print
688 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
695 ENTER_NESTED_DATA (pstate
, exp
, circref
);
696 i
= SCM_SMOBNUM (exp
);
697 if (i
< scm_numsmob
&& scm_smobs
[i
].print
698 && (scm_smobs
[i
].print
) (exp
, port
, pstate
))
700 EXIT_NESTED_DATA (pstate
);
703 EXIT_NESTED_DATA (pstate
);
704 /* Macros have their print field set to NULL. They are
705 handled at the same place as closures in order to achieve
706 non-redundancy. Placing the condition here won't slow
707 down printing of other smobs. */
708 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
713 scm_ipruk ("type", exp
, port
);
718 /* Print states are necessary for circular reference safe printing.
719 * They are also expensive to allocate. Therefore print states are
720 * kept in a pool so that they can be reused.
723 /* The PORT argument can also be a print-state/port pair, which will
724 * then be used instead of allocating a new print state. This is
725 * useful for continuing a chain of print calls from Scheme. */
728 scm_prin1 (SCM exp
, SCM port
, int writingp
)
730 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
732 scm_print_state
*pstate
;
734 /* If PORT is a print-state/port pair, use that. Else create a new
737 if (SCM_PORT_WITH_PS_P (port
))
739 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
740 port
= SCM_PORT_WITH_PS_PORT (port
);
744 /* First try to allocate a print state from the pool */
746 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
748 handle
= SCM_CDR (print_state_pool
);
749 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
752 if (SCM_FALSEP (handle
))
753 handle
= scm_cons (make_print_state (), SCM_EOL
);
754 pstate_scm
= SCM_CAR (handle
);
757 pstate
= SCM_PRINT_STATE (pstate_scm
);
758 pstate
->writingp
= writingp
;
759 scm_iprin1 (exp
, port
, pstate
);
761 /* Return print state to pool if it has been created above and
762 hasn't escaped to Scheme. */
764 if (!SCM_FALSEP (handle
) && !pstate
->revealed
)
767 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
768 SCM_SETCDR (print_state_pool
, handle
);
778 scm_intprint (long n
, int radix
, SCM port
)
780 char num_buf
[SCM_INTBUFLEN
];
781 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
784 /* Print an object of unrecognized type.
788 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
790 scm_puts ("#<unknown-", port
);
791 scm_puts (hdr
, port
);
794 scm_puts (" (0x", port
);
795 scm_intprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
796 scm_puts (" . 0x", port
);
797 scm_intprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
798 scm_puts (") @", port
);
800 scm_puts (" 0x", port
);
801 scm_intprint (SCM_UNPACK (ptr
), 16, port
);
802 scm_putc ('>', port
);
810 scm_iprlist (char *hdr
,SCM exp
,int tlr
,SCM port
,scm_print_state
*pstate
)
812 register SCM hare
, tortoise
;
813 int floor
= pstate
->top
- 2;
814 scm_puts (hdr
, port
);
819 /* Run a hare and tortoise so that total time complexity will be
820 O(depth * N) instead of O(N^2). */
821 hare
= SCM_CDR (exp
);
823 while (SCM_ECONSP (hare
))
825 if (SCM_EQ_P (hare
, tortoise
))
827 hare
= SCM_CDR (hare
);
828 if (SCM_IMP (hare
) || SCM_NECONSP (hare
))
830 hare
= SCM_CDR (hare
);
831 tortoise
= SCM_CDR (tortoise
);
834 /* No cdr cycles intrinsic to this list */
835 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
837 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
841 if (SCM_NECONSP (exp
))
843 for (i
= floor
; i
>= 0; --i
)
844 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
846 PUSH_REF (pstate
, exp
);
847 scm_putc (' ', port
);
849 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
851 if (SCM_NNULLP (exp
))
853 scm_puts (" . ", port
);
854 scm_iprin1 (exp
, port
, pstate
);
858 scm_putc (tlr
, port
);
859 pstate
->top
= floor
+ 2;
864 int n
= pstate
->length
;
866 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
867 exp
= SCM_CDR (exp
); --n
;
868 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
870 register unsigned long i
;
872 if (SCM_NECONSP (exp
))
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 MESSAGE to DESTINATION, defaulting to `current-output-port'.\n"
967 "MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,\n"
968 "the escapes are replaced with corresponding members of ARGS:\n"
969 "~A formats using `display' and ~S formats using `write'.\n"
970 "If DESTINATION is #t, then use the `current-output-port',\n"
971 "if DESTINATION is #f, then return a string containing the formatted text.\n"
972 "Does not add a trailing newline.")
973 #define FUNC_NAME s_scm_simple_format
975 SCM answer
= SCM_UNSPECIFIED
;
976 int fReturnString
= 0;
982 if (SCM_EQ_P (destination
, SCM_BOOL_T
))
984 destination
= scm_cur_outp
;
986 else if (SCM_FALSEP (destination
))
989 destination
= scm_mkstrport (SCM_INUM0
,
990 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
996 SCM_VALIDATE_OPORT_VALUE (1, destination
);
997 destination
= SCM_COERCE_OUTPORT (destination
);
999 SCM_VALIDATE_STRING (2, message
);
1000 SCM_VALIDATE_REST_ARGUMENT (args
);
1002 start
= SCM_STRING_CHARS (message
);
1003 end
= start
+ SCM_STRING_LENGTH (message
);
1004 for (p
= start
; p
!= end
; ++p
)
1007 if (!SCM_CONSP (args
))
1013 if (*p
== 'A' || *p
== 'a')
1015 else if (*p
== 'S' || *p
== 's')
1020 scm_lfwrite (start
, p
- start
- 1, destination
);
1021 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1022 args
= SCM_CDR (args
);
1025 scm_lfwrite (start
, p
- start
, destination
);
1028 answer
= scm_strport_to_string (destination
);
1030 return scm_return_first (answer
, message
);
1035 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1037 "Send a newline to PORT.")
1038 #define FUNC_NAME s_scm_newline
1040 if (SCM_UNBNDP (port
))
1041 port
= scm_cur_outp
;
1043 SCM_VALIDATE_OPORT_VALUE (1,port
);
1045 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1046 return SCM_UNSPECIFIED
;
1050 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1051 (SCM chr
, SCM port
),
1052 "Send character CHR to PORT.")
1053 #define FUNC_NAME s_scm_write_char
1055 if (SCM_UNBNDP (port
))
1056 port
= scm_cur_outp
;
1058 SCM_VALIDATE_CHAR (1,chr
);
1059 SCM_VALIDATE_OPORT_VALUE (2,port
);
1061 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1065 scm_close_port (port
);
1068 return SCM_UNSPECIFIED
;
1074 /* Call back to Scheme code to do the printing of special objects
1075 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1076 * containing PORT and PSTATE. This object can be used as the port for
1077 * display/write etc to continue the current print chain. The REVEALED
1078 * field of PSTATE is set to true to indicate that the print state has
1079 * escaped to Scheme and thus has to be freed by the GC.
1082 long scm_tc16_port_with_ps
;
1084 /* Print exactly as the port itself would */
1087 print_port_with_ps (SCM obj
, SCM port
, scm_print_state
*pstate
)
1089 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1090 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1094 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1097 SCM pair
= scm_cons (port
, pstate
->handle
);
1098 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (pair
));
1099 pstate
->revealed
= 1;
1100 return scm_apply (proc
, exp
, scm_cons (pwps
, scm_listofnull
));
1103 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 2, 0, 0,
1104 (SCM port
, SCM pstate
),
1106 #define FUNC_NAME s_scm_port_with_print_state
1109 SCM_VALIDATE_OPORT_VALUE (1,port
);
1110 SCM_VALIDATE_PRINTSTATE (2,pstate
);
1111 port
= SCM_COERCE_OUTPORT (port
);
1112 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (scm_cons (port
, pstate
)));
1117 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1120 #define FUNC_NAME s_scm_get_print_state
1122 if (SCM_PORT_WITH_PS_P (port
))
1123 return SCM_PORT_WITH_PS_PS (port
);
1124 if (SCM_OUTPUT_PORT_P (port
))
1126 RETURN_SCM_WTA (1,port
);
1135 SCM vtable
, layout
, type
;
1137 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1138 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1139 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
1140 type
= scm_make_struct (vtable
, SCM_INUM0
, SCM_LIST1 (layout
));
1141 scm_set_struct_vtable_name_x (type
, scm_str2symbol ("print-state"));
1142 print_state_pool
= scm_permanent_object (scm_cons (type
, SCM_EOL
));
1144 scm_print_state_vtable
= type
;
1146 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1147 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1148 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1149 scm_set_smob_print (scm_tc16_port_with_ps
, print_port_with_ps
);
1151 #ifndef SCM_MAGIC_SNARFER
1152 #include "libguile/print.x"