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
))
317 scm_intprint (SCM_INUM (exp
), 10, port
);
325 if (SCM_WRITINGP (pstate
))
327 scm_puts ("#\\", port
);
328 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
329 scm_puts (scm_charnames
[i
], port
);
331 else if (i
== '\177')
332 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
334 else if (i
< 0 || i
> '\177')
335 scm_intprint (i
, 8, port
);
342 else if (SCM_IFLAGP (exp
)
343 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
344 scm_puts (SCM_ISYMCHARS (exp
), port
);
345 else if (SCM_ILOCP (exp
))
347 scm_puts ("#@", port
);
348 scm_intprint (SCM_IFRAME (exp
), 10, port
);
349 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
350 scm_intprint (SCM_IDIST (exp
), 10, port
);
357 scm_puts ("#@", port
);
358 exp
= SCM_GLOC_SYM (exp
);
362 scm_ipruk ("immediate", exp
, port
);
365 switch (SCM_TYP7 (exp
))
367 case scm_tcs_cons_gloc
:
369 if (SCM_STRUCT_VTABLE_DATA (exp
) [scm_vtable_index_vcell
] == 0)
371 ENTER_NESTED_DATA (pstate
, exp
, circref
);
372 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
374 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
378 scm_tc16_port_with_ps
,
379 SCM_UNPACK (scm_cons (port
, pstate
->handle
)));
380 scm_call_generic_2 (print
, exp
, pwps
);
385 scm_print_struct (exp
, port
, pstate
);
387 EXIT_NESTED_DATA (pstate
);
391 case scm_tcs_cons_imcar
:
392 case scm_tcs_cons_nimcar
:
393 ENTER_NESTED_DATA (pstate
, exp
, circref
);
394 scm_iprlist ("(", exp
, ')', port
, pstate
);
395 EXIT_NESTED_DATA (pstate
);
398 print_circref (port
, pstate
, exp
);
401 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
403 case scm_tcs_closures
:
404 /* The user supplied print closure procedure must handle
405 macro closures as well. */
406 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
407 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
411 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
413 /* Printing a macro. */
415 name
= scm_macro_name (exp
);
416 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
418 code
= env
= SCM_UNDEFINED
;
419 scm_puts ("#<primitive-", port
);
423 code
= SCM_CODE (SCM_CDR (exp
));
424 env
= SCM_ENV (SCM_CDR (exp
));
425 scm_puts ("#<", port
);
427 if (SCM_CELL_WORD_0 (exp
) & (3L << 16))
428 scm_puts ("macro", port
);
430 scm_puts ("syntax", port
);
431 if (SCM_CELL_WORD_0 (exp
) & (2L << 16))
432 scm_putc ('!', port
);
436 /* Printing a closure. */
437 name
= scm_procedure_name (exp
);
438 code
= SCM_CODE (exp
);
440 scm_puts ("#<procedure", port
);
442 if (SCM_ROSTRINGP (name
))
444 scm_putc (' ', port
);
445 scm_puts (SCM_ROCHARS (name
), port
);
447 if (!SCM_UNBNDP (code
))
449 if (SCM_PRINT_SOURCE_P
)
451 code
= scm_unmemocopy (code
,
452 SCM_EXTEND_ENV (SCM_CAR (code
),
455 ENTER_NESTED_DATA (pstate
, exp
, circref
);
456 scm_iprlist (" ", code
, '>', port
, pstate
);
457 EXIT_NESTED_DATA (pstate
);
461 if (SCM_TYP16 (exp
) != scm_tc16_macro
)
463 scm_putc (' ', port
);
464 scm_iprin1 (SCM_CAR (code
), port
, pstate
);
466 scm_putc ('>', port
);
470 scm_putc ('>', port
);
473 case scm_tc7_substring
:
475 if (SCM_WRITINGP (pstate
))
479 scm_putc ('"', port
);
480 for (i
= 0; i
< SCM_STRING_LENGTH (exp
); ++i
)
481 switch (SCM_ROCHARS (exp
)[i
])
485 scm_putc ('\\', port
);
487 scm_putc (SCM_ROCHARS (exp
)[i
], port
);
489 scm_putc ('"', port
);
493 scm_lfwrite (SCM_ROCHARS (exp
), (scm_sizet
) SCM_STRING_LENGTH (exp
),
506 len
= SCM_SYMBOL_LENGTH (exp
);
507 str
= SCM_SYMBOL_CHARS (exp
);
514 scm_lfwrite ("#{}#", 4, port
);
516 for (end
= pos
; end
< len
; ++end
)
519 #ifdef BRACKETS_AS_PARENS
527 case SCM_WHITE_SPACES
:
528 case SCM_LINE_INCREMENTORS
:
537 scm_lfwrite ("#{", 2, port
);
542 scm_lfwrite (str
+ pos
, end
- pos
, port
);
548 scm_lfwrite (buf
, 2, port
);
570 scm_lfwrite (str
+ pos
, end
- pos
, port
);
572 scm_lfwrite ("}#", 2, port
);
576 ENTER_NESTED_DATA (pstate
, exp
, circref
);
577 if (SCM_IS_WHVEC (exp
))
578 scm_puts ("#wh(", port
);
580 scm_puts ("#w(", port
);
581 goto common_vector_printer
;
584 ENTER_NESTED_DATA (pstate
, exp
, circref
);
585 scm_puts ("#(", port
);
586 common_vector_printer
:
589 int last
= SCM_VECTOR_LENGTH (exp
) - 1;
591 if (pstate
->fancyp
&& SCM_VECTOR_LENGTH (exp
) > pstate
->length
)
593 last
= pstate
->length
- 1;
596 for (i
= 0; i
< last
; ++i
)
599 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
600 scm_putc (' ', port
);
605 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
608 scm_puts (" ...", port
);
609 scm_putc (')', port
);
611 EXIT_NESTED_DATA (pstate
);
622 #ifdef HAVE_LONG_LONGS
625 scm_raprin1 (exp
, port
, pstate
);
629 scm_puts (SCM_SUBR_GENERIC (exp
) && *SCM_SUBR_GENERIC (exp
)
630 ? "#<primitive-generic "
631 : "#<primitive-procedure ",
633 scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp
)), port
);
634 scm_putc ('>', port
);
639 SCM proc
= SCM_CCLO_SUBR (exp
);
640 if (SCM_EQ_P (proc
, scm_f_gsubr_apply
))
642 /* Print gsubrs as primitives */
643 SCM name
= scm_procedure_name (exp
);
644 scm_puts ("#<primitive-procedure", port
);
645 if (SCM_NFALSEP (name
))
647 scm_putc (' ', port
);
648 scm_puts (SCM_SYMBOL_CHARS (name
), port
);
653 scm_puts ("#<compiled-closure ", port
);
654 scm_iprin1 (proc
, port
, pstate
);
656 scm_putc ('>', port
);
661 scm_puts ("#<procedure-with-setter", port
);
663 SCM name
= scm_procedure_name (exp
);
664 if (SCM_NFALSEP (name
))
666 scm_putc (' ', port
);
667 scm_puts (SCM_ROCHARS (name
), port
);
670 scm_putc ('>', port
);
673 scm_puts ("#<continuation ", port
);
674 scm_intprint (SCM_CONTINUATION_LENGTH (exp
), 10, port
);
675 scm_puts (" @ ", port
);
676 scm_intprint ((long) SCM_CONTREGS (exp
), 16, port
);
677 scm_putc ('>', port
);
681 register long i
= SCM_PTOBNUM (exp
);
683 && scm_ptobs
[i
].print
684 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
691 ENTER_NESTED_DATA (pstate
, exp
, circref
);
692 i
= SCM_SMOBNUM (exp
);
693 if (i
< scm_numsmob
&& scm_smobs
[i
].print
694 && (scm_smobs
[i
].print
) (exp
, port
, pstate
))
696 EXIT_NESTED_DATA (pstate
);
699 EXIT_NESTED_DATA (pstate
);
700 /* Macros have their print field set to NULL. They are
701 handled at the same place as closures in order to achieve
702 non-redundancy. Placing the condition here won't slow
703 down printing of other smobs. */
704 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
709 scm_ipruk ("type", exp
, port
);
714 /* Print states are necessary for circular reference safe printing.
715 * They are also expensive to allocate. Therefore print states are
716 * kept in a pool so that they can be reused.
719 /* The PORT argument can also be a print-state/port pair, which will
720 * then be used instead of allocating a new print state. This is
721 * useful for continuing a chain of print calls from Scheme. */
724 scm_prin1 (SCM exp
, SCM port
, int writingp
)
726 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
728 scm_print_state
*pstate
;
730 /* If PORT is a print-state/port pair, use that. Else create a new
733 if (SCM_PORT_WITH_PS_P (port
))
735 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
736 port
= SCM_PORT_WITH_PS_PORT (port
);
740 /* First try to allocate a print state from the pool */
742 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
744 handle
= SCM_CDR (print_state_pool
);
745 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
748 if (SCM_FALSEP (handle
))
749 handle
= scm_cons (make_print_state (), SCM_EOL
);
750 pstate_scm
= SCM_CAR (handle
);
753 pstate
= SCM_PRINT_STATE (pstate_scm
);
754 pstate
->writingp
= writingp
;
755 scm_iprin1 (exp
, port
, pstate
);
757 /* Return print state to pool if it has been created above and
758 hasn't escaped to Scheme. */
760 if (!SCM_FALSEP (handle
) && !pstate
->revealed
)
763 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
764 SCM_SETCDR (print_state_pool
, handle
);
774 scm_intprint (long n
, int radix
, SCM port
)
776 char num_buf
[SCM_INTBUFLEN
];
777 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
780 /* Print an object of unrecognized type.
784 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
786 scm_puts ("#<unknown-", port
);
787 scm_puts (hdr
, port
);
790 scm_puts (" (0x", port
);
791 scm_intprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
792 scm_puts (" . 0x", port
);
793 scm_intprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
794 scm_puts (") @", port
);
796 scm_puts (" 0x", port
);
797 scm_intprint (SCM_UNPACK (ptr
), 16, port
);
798 scm_putc ('>', port
);
806 scm_iprlist (char *hdr
,SCM exp
,int tlr
,SCM port
,scm_print_state
*pstate
)
808 register SCM hare
, tortoise
;
809 int floor
= pstate
->top
- 2;
810 scm_puts (hdr
, port
);
815 /* Run a hare and tortoise so that total time complexity will be
816 O(depth * N) instead of O(N^2). */
817 hare
= SCM_CDR (exp
);
819 while (SCM_ECONSP (hare
))
821 if (SCM_EQ_P (hare
, tortoise
))
823 hare
= SCM_CDR (hare
);
824 if (SCM_IMP (hare
) || SCM_NECONSP (hare
))
826 hare
= SCM_CDR (hare
);
827 tortoise
= SCM_CDR (tortoise
);
830 /* No cdr cycles intrinsic to this list */
831 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
833 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
837 if (SCM_NECONSP (exp
))
839 for (i
= floor
; i
>= 0; --i
)
840 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
842 PUSH_REF (pstate
, exp
);
843 scm_putc (' ', port
);
845 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
847 if (SCM_NNULLP (exp
))
849 scm_puts (" . ", port
);
850 scm_iprin1 (exp
, port
, pstate
);
854 scm_putc (tlr
, port
);
855 pstate
->top
= floor
+ 2;
860 int n
= pstate
->length
;
862 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
863 exp
= SCM_CDR (exp
); --n
;
864 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
866 register unsigned long i
;
868 if (SCM_NECONSP (exp
))
870 for (i
= 0; i
< pstate
->top
; ++i
)
871 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
877 scm_puts (" ...", port
);
883 PUSH_REF(pstate
, exp
);
884 ++pstate
->list_offset
;
885 scm_putc (' ', port
);
887 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
890 if (SCM_NNULLP (exp
))
892 scm_puts (" . ", port
);
893 scm_iprin1 (exp
, port
, pstate
);
896 pstate
->list_offset
-= pstate
->top
- floor
- 2;
900 pstate
->list_offset
-= pstate
->top
- floor
- 2;
903 scm_puts (" . ", port
);
904 print_circref (port
, pstate
, exp
);
911 scm_valid_oport_value_p (SCM val
)
913 return (SCM_OPOUTPORTP (val
)
914 || (SCM_PORT_WITH_PS_P (val
)
915 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
918 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
921 scm_write (SCM obj
, SCM port
)
923 if (SCM_UNBNDP (port
))
926 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
928 scm_prin1 (obj
, port
, 1);
932 scm_close_port (port
);
935 return SCM_UNSPECIFIED
;
939 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
942 scm_display (SCM obj
, SCM port
)
944 if (SCM_UNBNDP (port
))
947 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
949 scm_prin1 (obj
, port
, 0);
953 scm_close_port (port
);
956 return SCM_UNSPECIFIED
;
960 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
961 (SCM destination
, SCM message
, SCM args
),
962 "Write MESSAGE to DESTINATION, defaulting to `current-output-port'.\n"
963 "MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,\n"
964 "the escapes are replaced with corresponding members of ARGS:\n"
965 "~A formats using `display' and ~S formats using `write'.\n"
966 "If DESTINATION is #t, then use the `current-output-port',\n"
967 "if DESTINATION is #f, then return a string containing the formatted text.\n"
968 "Does not add a trailing newline.")
969 #define FUNC_NAME s_scm_simple_format
971 SCM answer
= SCM_UNSPECIFIED
;
972 int fReturnString
= 0;
977 if (SCM_EQ_P (destination
, SCM_BOOL_T
))
979 destination
= scm_cur_outp
;
981 else if (SCM_FALSEP (destination
))
984 destination
= scm_mkstrport (SCM_INUM0
,
985 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
991 SCM_VALIDATE_OPORT_VALUE (1, destination
);
992 destination
= SCM_COERCE_OUTPORT (destination
);
994 SCM_VALIDATE_STRING (2, message
);
995 SCM_VALIDATE_REST_ARGUMENT (args
);
997 start
= SCM_ROCHARS (message
);
998 for (p
= start
; *p
!= '\0'; ++p
)
1001 if (SCM_IMP (args
) || SCM_NCONSP (args
))
1005 if (*p
== 'A' || *p
== 'a')
1007 else if (*p
== 'S' || *p
== 's')
1012 scm_lfwrite (start
, p
- start
- 1, destination
);
1013 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1014 args
= SCM_CDR (args
);
1017 scm_lfwrite (start
, p
- start
, destination
);
1020 answer
= scm_strport_to_string (destination
);
1022 return scm_return_first (answer
, message
);
1027 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1029 "Send a newline to PORT.")
1030 #define FUNC_NAME s_scm_newline
1032 if (SCM_UNBNDP (port
))
1033 port
= scm_cur_outp
;
1035 SCM_VALIDATE_OPORT_VALUE (1,port
);
1037 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1038 return SCM_UNSPECIFIED
;
1042 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1043 (SCM chr
, SCM port
),
1044 "Send character CHR to PORT.")
1045 #define FUNC_NAME s_scm_write_char
1047 if (SCM_UNBNDP (port
))
1048 port
= scm_cur_outp
;
1050 SCM_VALIDATE_CHAR (1,chr
);
1051 SCM_VALIDATE_OPORT_VALUE (2,port
);
1053 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1057 scm_close_port (port
);
1060 return SCM_UNSPECIFIED
;
1066 /* Call back to Scheme code to do the printing of special objects
1067 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1068 * containing PORT and PSTATE. This object can be used as the port for
1069 * display/write etc to continue the current print chain. The REVEALED
1070 * field of PSTATE is set to true to indicate that the print state has
1071 * escaped to Scheme and thus has to be freed by the GC.
1074 long scm_tc16_port_with_ps
;
1076 /* Print exactly as the port itself would */
1079 print_port_with_ps (SCM obj
, SCM port
, scm_print_state
*pstate
)
1081 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1082 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1086 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1089 SCM pair
= scm_cons (port
, pstate
->handle
);
1090 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (pair
));
1091 pstate
->revealed
= 1;
1092 return scm_apply (proc
, exp
, scm_cons (pwps
, scm_listofnull
));
1095 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 2, 0, 0,
1096 (SCM port
, SCM pstate
),
1098 #define FUNC_NAME s_scm_port_with_print_state
1101 SCM_VALIDATE_OPORT_VALUE (1,port
);
1102 SCM_VALIDATE_PRINTSTATE (2,pstate
);
1103 port
= SCM_COERCE_OUTPORT (port
);
1104 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, SCM_UNPACK (scm_cons (port
, pstate
)));
1109 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1112 #define FUNC_NAME s_scm_get_print_state
1114 if (SCM_PORT_WITH_PS_P (port
))
1115 return SCM_PORT_WITH_PS_PS (port
);
1116 if (SCM_OUTPUT_PORT_P (port
))
1118 RETURN_SCM_WTA (1,port
);
1127 SCM vtable
, layout
, type
;
1129 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1130 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1131 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
1132 type
= scm_make_struct (vtable
, SCM_INUM0
, SCM_LIST1 (layout
));
1133 scm_set_struct_vtable_name_x (type
, SCM_CAR (scm_intern0 ("print-state")));
1134 print_state_pool
= scm_permanent_object (scm_cons (type
, SCM_EOL
));
1136 scm_print_state_vtable
= type
;
1138 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1139 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1140 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1141 scm_set_smob_print (scm_tc16_port_with_ps
, print_port_with_ps
);
1143 #include "libguile/print.x"