1 /* Copyright (C) 1995-1999 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 */
63 #include "scm_validate.h"
67 /* {Names of immediate symbols}
69 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
72 char *scm_isymnames
[] =
74 /* This table must agree with the declarations */
90 "#@literal-variable-ref",
91 "#@literal-variable-set!",
94 "#@call-with-current-continuation",
96 /* user visible ISYMS */
110 /* Multi-language support */
125 scm_option scm_print_opts
[] = {
126 { SCM_OPTION_SCM
, "closure-hook", SCM_BOOL_F
,
127 "Hook for printing closures." },
128 { SCM_OPTION_BOOLEAN
, "source", 0,
129 "Print closures with source." }
132 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
135 #define FUNC_NAME s_scm_print_options
137 SCM ans
= scm_options (setting
,
146 /* {Printing of Scheme Objects}
149 /* Detection of circular references.
151 * Due to other constraints in the implementation, this code has bad
152 * time complexity (O (depth * N)), The printer code will be
153 * completely rewritten before next release of Guile. The new code
156 #define PUSH_REF(pstate, obj) \
158 pstate->ref_stack[pstate->top++] = (obj); \
159 if (pstate->top == pstate->ceiling) \
160 grow_ref_stack (pstate); \
163 #define ENTER_NESTED_DATA(pstate, obj, label) \
165 register unsigned long i; \
166 for (i = 0; i < pstate->top; ++i) \
167 if (pstate->ref_stack[i] == (obj)) \
169 if (pstate->fancyp) \
171 if (pstate->top - pstate->list_offset >= pstate->level) \
173 scm_putc ('#', port); \
177 PUSH_REF(pstate, obj); \
180 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
182 SCM scm_print_state_vtable
;
184 static SCM print_state_pool
;
186 #ifdef GUILE_DEBUG /* Used for debugging purposes */
188 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
191 #define FUNC_NAME s_scm_current_pstate
193 return SCM_CADR (print_state_pool
);
199 #define PSTATE_SIZE 50L
202 make_print_state (void)
204 SCM print_state
= scm_make_struct (SCM_CAR (print_state_pool
), /* pstate type */
207 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
208 pstate
->ref_vect
= scm_make_vector (SCM_MAKINUM (PSTATE_SIZE
),
210 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
211 pstate
->ceiling
= SCM_LENGTH (pstate
->ref_vect
);
216 scm_make_print_state ()
220 /* First try to allocate a print state from the pool */
222 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
224 answer
= SCM_CADR (print_state_pool
);
225 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
229 return answer
? answer
: make_print_state ();
233 scm_free_print_state (SCM print_state
)
236 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
237 /* Cleanup before returning print state to pool.
238 * It is better to do it here. Doing it in scm_prin1
239 * would cost more since that function is called much more
243 pstate
->revealed
= 0;
244 SCM_NEWCELL (handle
);
246 SCM_SETCAR (handle
, print_state
);
247 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
248 SCM_SETCDR (print_state_pool
, handle
);
253 grow_ref_stack (scm_print_state
*pstate
)
255 int new_size
= 2 * pstate
->ceiling
;
256 scm_vector_set_length_x (pstate
->ref_vect
, SCM_MAKINUM (new_size
));
257 pstate
->ref_stack
= SCM_VELTS (pstate
->ref_vect
);
258 pstate
->ceiling
= new_size
;
263 print_circref (SCM port
,scm_print_state
*pstate
,SCM ref
)
266 int self
= pstate
->top
- 1;
268 if (SCM_CONSP (pstate
->ref_stack
[i
]))
272 if (SCM_NCONSP (pstate
->ref_stack
[i
- 1])
273 || SCM_CDR (pstate
->ref_stack
[i
- 1]) != pstate
->ref_stack
[i
])
279 for (i
= pstate
->top
- 1; 1; --i
)
280 if (pstate
->ref_stack
[i
] == ref
)
282 scm_putc ('#', port
);
283 scm_intprint (i
- self
, 10, port
);
284 scm_putc ('#', port
);
287 /* Print generally. Handles both write and display according to PSTATE.
289 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
290 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
293 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
296 switch (7 & (int) exp
)
300 scm_intprint (SCM_INUM (exp
), 10, port
);
308 if (SCM_WRITINGP (pstate
))
310 scm_puts ("#\\", port
);
311 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
312 scm_puts (scm_charnames
[i
], port
);
313 else if (i
< 0 || i
> '\177')
314 scm_intprint (i
, 8, port
);
321 else if (SCM_IFLAGP (exp
)
322 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
323 scm_puts (SCM_ISYMCHARS (exp
), port
);
324 else if (SCM_ILOCP (exp
))
326 scm_puts ("#@", port
);
327 scm_intprint ((long) SCM_IFRAME (exp
), 10, port
);
328 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
329 scm_intprint ((long) SCM_IDIST (exp
), 10, port
);
336 scm_puts ("#@", port
);
337 exp
= SCM_CAR (exp
- 1);
341 scm_ipruk ("immediate", exp
, port
);
344 switch (SCM_TYP7 (exp
))
346 case scm_tcs_cons_gloc
:
348 if (SCM_CDR (SCM_CAR (exp
) - 1L) == 0)
350 ENTER_NESTED_DATA (pstate
, exp
, circref
);
351 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
353 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
357 scm_tc16_port_with_ps
,
358 scm_cons (port
, pstate
->handle
));
359 scm_call_generic_2 (print
, exp
, pwps
);
364 scm_print_struct (exp
, port
, pstate
);
366 EXIT_NESTED_DATA (pstate
);
370 case scm_tcs_cons_imcar
:
371 case scm_tcs_cons_nimcar
:
372 ENTER_NESTED_DATA (pstate
, exp
, circref
);
373 scm_iprlist ("(", exp
, ')', port
, pstate
);
374 EXIT_NESTED_DATA (pstate
);
377 print_circref (port
, pstate
, exp
);
380 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
382 case scm_tcs_closures
:
383 /* The user supplied print closure procedure must handle
384 macro closures as well. */
385 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
386 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
390 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
392 /* Printing a macro. */
394 name
= scm_macro_name (exp
);
395 if (!SCM_CLOSUREP (SCM_CDR (exp
)))
398 scm_puts ("#<primitive-", port
);
402 code
= SCM_CODE (SCM_CDR (exp
));
403 env
= SCM_ENV (SCM_CDR (exp
));
404 scm_puts ("#<", port
);
406 if (SCM_CAR (exp
) & (3L << 16))
407 scm_puts ("macro", port
);
409 scm_puts ("syntax", port
);
410 if (SCM_CAR (exp
) & (2L << 16))
411 scm_putc ('!', port
);
415 /* Printing a closure. */
416 name
= scm_procedure_name (exp
);
417 code
= SCM_CODE (exp
);
419 scm_puts ("#<procedure", port
);
421 if (SCM_ROSTRINGP (name
))
423 scm_putc (' ', port
);
424 scm_puts (SCM_ROCHARS (name
), port
);
428 if (SCM_PRINT_SOURCE_P
)
430 code
= scm_unmemocopy (code
,
431 SCM_EXTEND_ENV (SCM_CAR (code
),
434 ENTER_NESTED_DATA (pstate
, exp
, circref
);
435 scm_iprlist (" ", code
, '>', port
, pstate
);
436 EXIT_NESTED_DATA (pstate
);
440 if (SCM_TYP16 (exp
) != scm_tc16_macro
)
442 scm_putc (' ', port
);
443 scm_iprin1 (SCM_CAR (code
), port
, pstate
);
445 scm_putc ('>', port
);
449 scm_putc ('>', port
);
452 case scm_tc7_substring
:
454 if (SCM_WRITINGP (pstate
))
458 scm_putc ('"', port
);
459 for (i
= 0; i
< SCM_ROLENGTH (exp
); ++i
)
460 switch (SCM_ROCHARS (exp
)[i
])
464 scm_putc ('\\', port
);
466 scm_putc (SCM_ROCHARS (exp
)[i
], port
);
468 scm_putc ('"', port
);
472 scm_lfwrite (SCM_ROCHARS (exp
), (scm_sizet
) SCM_ROLENGTH (exp
),
475 case scm_tcs_symbols
:
485 len
= SCM_LENGTH (exp
);
486 str
= SCM_CHARS (exp
);
493 scm_lfwrite ("#{}#", 4, port
);
495 for (end
= pos
; end
< len
; ++end
)
498 #ifdef BRACKETS_AS_PARENS
506 case SCM_WHITE_SPACES
:
507 case SCM_LINE_INCREMENTORS
:
516 scm_lfwrite ("#{", 2, port
);
521 scm_lfwrite (str
+ pos
, end
- pos
, port
);
527 scm_lfwrite (buf
, 2, port
);
549 scm_lfwrite (str
+ pos
, end
- pos
, port
);
551 scm_lfwrite ("}#", 2, port
);
555 ENTER_NESTED_DATA (pstate
, exp
, circref
);
556 if (SCM_IS_WHVEC (exp
))
557 scm_puts ("#wh(", port
);
559 scm_puts ("#w(", port
);
560 goto common_vector_printer
;
563 ENTER_NESTED_DATA (pstate
, exp
, circref
);
564 scm_puts ("#(", port
);
565 common_vector_printer
:
568 int last
= SCM_LENGTH (exp
) - 1;
570 if (pstate
->fancyp
&& SCM_LENGTH (exp
) > pstate
->length
)
572 last
= pstate
->length
- 1;
575 for (i
= 0; i
< last
; ++i
)
578 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
579 scm_putc (' ', port
);
584 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
587 scm_puts (" ...", port
);
588 scm_putc (')', port
);
590 EXIT_NESTED_DATA (pstate
);
601 #ifdef HAVE_LONG_LONGS
604 scm_raprin1 (exp
, port
, pstate
);
608 scm_puts (SCM_SUBR_GENERIC (exp
) && *SCM_SUBR_GENERIC (exp
)
609 ? "#<primitive-generic "
610 : "#<primitive-procedure ",
612 scm_puts (SCM_CHARS (SCM_SNAME (exp
)), port
);
613 scm_putc ('>', port
);
618 SCM proc
= SCM_CCLO_SUBR (exp
);
619 if (proc
== scm_f_gsubr_apply
)
621 /* Print gsubrs as primitives */
622 SCM name
= scm_procedure_name (exp
);
623 scm_puts ("#<primitive-procedure", port
);
624 if (SCM_NFALSEP (name
))
626 scm_putc (' ', port
);
627 scm_puts (SCM_CHARS (name
), port
);
632 scm_puts ("#<compiled-closure ", port
);
633 scm_iprin1 (proc
, port
, pstate
);
635 scm_putc ('>', port
);
640 scm_puts ("#<procedure-with-setter", port
);
642 SCM name
= scm_procedure_name (exp
);
643 if (SCM_NFALSEP (name
))
645 scm_putc (' ', port
);
646 scm_puts (SCM_ROCHARS (name
), port
);
649 scm_putc ('>', port
);
652 scm_puts ("#<continuation ", port
);
653 scm_intprint (SCM_LENGTH (exp
), 10, port
);
654 scm_puts (" @ ", port
);
655 scm_intprint ((long) SCM_CHARS (exp
), 16, port
);
656 scm_putc ('>', port
);
660 register long i
= SCM_PTOBNUM (exp
);
662 && scm_ptobs
[i
].print
663 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
670 ENTER_NESTED_DATA (pstate
, exp
, circref
);
671 i
= SCM_SMOBNUM (exp
);
672 if (i
< scm_numsmob
&& scm_smobs
[i
].print
673 && (scm_smobs
[i
].print
) (exp
, port
, pstate
))
675 EXIT_NESTED_DATA (pstate
);
678 EXIT_NESTED_DATA (pstate
);
679 /* Macros have their print field set to NULL. They are
680 handled at the same place as closures in order to achieve
681 non-redundancy. Placing the condition here won't slow
682 down printing of other smobs. */
683 if (SCM_TYP16 (exp
) == scm_tc16_macro
)
688 scm_ipruk ("type", exp
, port
);
693 /* Print states are necessary for circular reference safe printing.
694 * They are also expensive to allocate. Therefore print states are
695 * kept in a pool so that they can be reused.
698 /* The PORT argument can also be a print-state/port pair, which will
699 * then be used instead of allocating a new print state. This is
700 * useful for continuing a chain of print calls from Scheme. */
703 scm_prin1 (SCM exp
, SCM port
, int writingp
)
705 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
707 scm_print_state
*pstate
;
709 /* If PORT is a print-state/port pair, use that. Else create a new
712 if (SCM_PORT_WITH_PS_P (port
))
714 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
715 port
= SCM_PORT_WITH_PS_PORT (port
);
719 /* First try to allocate a print state from the pool */
721 if (SCM_NNULLP (SCM_CDR (print_state_pool
)))
723 handle
= SCM_CDR (print_state_pool
);
724 SCM_SETCDR (print_state_pool
, SCM_CDDR (print_state_pool
));
727 if (handle
== SCM_BOOL_F
)
728 handle
= scm_cons (make_print_state (), SCM_EOL
);
729 pstate_scm
= SCM_CAR (handle
);
732 pstate
= SCM_PRINT_STATE (pstate_scm
);
733 pstate
->writingp
= writingp
;
734 scm_iprin1 (exp
, port
, pstate
);
736 /* Return print state to pool if it has been created above and
737 hasn't escaped to Scheme. */
739 if (handle
!= SCM_BOOL_F
&& !pstate
->revealed
)
742 SCM_SETCDR (handle
, SCM_CDR (print_state_pool
));
743 SCM_SETCDR (print_state_pool
, handle
);
753 scm_intprint (long n
, int radix
, SCM port
)
755 char num_buf
[SCM_INTBUFLEN
];
756 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
759 /* Print an object of unrecognized type.
763 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
765 scm_puts ("#<unknown-", port
);
766 scm_puts (hdr
, port
);
769 scm_puts (" (0x", port
);
770 scm_intprint (SCM_CAR (ptr
), 16, port
);
771 scm_puts (" . 0x", port
);
772 scm_intprint (SCM_CDR (ptr
), 16, port
);
773 scm_puts (") @", port
);
775 scm_puts (" 0x", port
);
776 scm_intprint (ptr
, 16, port
);
777 scm_putc ('>', port
);
785 scm_iprlist (char *hdr
,SCM exp
,int tlr
,SCM port
,scm_print_state
*pstate
)
787 register SCM hare
, tortoise
;
788 int floor
= pstate
->top
- 2;
789 scm_puts (hdr
, port
);
794 /* Run a hare and tortoise so that total time complexity will be
795 O(depth * N) instead of O(N^2). */
796 hare
= SCM_CDR (exp
);
798 while (SCM_ECONSP (hare
))
800 if (hare
== tortoise
)
802 hare
= SCM_CDR (hare
);
803 if (SCM_IMP (hare
) || SCM_NECONSP (hare
))
805 hare
= SCM_CDR (hare
);
806 tortoise
= SCM_CDR (tortoise
);
809 /* No cdr cycles intrinsic to this list */
810 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
812 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
816 if (SCM_NECONSP (exp
))
818 for (i
= floor
; i
>= 0; --i
)
819 if (pstate
->ref_stack
[i
] == exp
)
821 PUSH_REF (pstate
, exp
);
822 scm_putc (' ', port
);
824 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
826 if (SCM_NNULLP (exp
))
828 scm_puts (" . ", port
);
829 scm_iprin1 (exp
, port
, pstate
);
833 scm_putc (tlr
, port
);
834 pstate
->top
= floor
+ 2;
839 int n
= pstate
->length
;
841 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
842 exp
= SCM_CDR (exp
); --n
;
843 for (; SCM_NIMP (exp
); exp
= SCM_CDR (exp
))
845 register unsigned long i
;
847 if (SCM_NECONSP (exp
))
849 for (i
= 0; i
< pstate
->top
; ++i
)
850 if (pstate
->ref_stack
[i
] == exp
)
856 scm_puts (" ...", port
);
862 PUSH_REF(pstate
, exp
);
863 ++pstate
->list_offset
;
864 scm_putc (' ', port
);
866 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
869 if (SCM_NNULLP (exp
))
871 scm_puts (" . ", port
);
872 scm_iprin1 (exp
, port
, pstate
);
875 pstate
->list_offset
-= pstate
->top
- floor
- 2;
879 pstate
->list_offset
-= pstate
->top
- floor
- 2;
882 scm_puts (" . ", port
);
883 print_circref (port
, pstate
, exp
);
890 scm_valid_oport_value_p (SCM val
)
892 return (SCM_OPOUTPORTP (val
)
893 || (SCM_PORT_WITH_PS_P (val
)
894 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
897 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
900 scm_write (SCM obj
, SCM port
)
902 if (SCM_UNBNDP (port
))
905 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
907 scm_prin1 (obj
, port
, 1);
911 scm_close_port (port
);
914 return SCM_UNSPECIFIED
;
918 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
921 scm_display (SCM obj
, SCM port
)
923 if (SCM_UNBNDP (port
))
926 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
928 scm_prin1 (obj
, port
, 0);
932 scm_close_port (port
);
935 return SCM_UNSPECIFIED
;
939 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
940 (SCM destination
, SCM message
, SCM args
),
941 "Write MESSAGE to DESTINATION, defaulting to `current-output-port'.
942 MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,
943 the escapes are replaced with corresponding members of ARGS:
944 ~A formats using `display' and ~S formats using `write'.
945 If DESTINATION is #t, then use the `current-output-port',
946 if DESTINATION is #f, then return a string containing the formatted text.
947 Does not add a trailing newline.")
948 #define FUNC_NAME s_scm_simple_format
950 SCM answer
= SCM_UNSPECIFIED
;
951 int fReturnString
= 0;
956 if (SCM_BOOL_T
== destination
) {
957 destination
= scm_cur_outp
;
958 } else if (SCM_BOOL_F
== destination
) {
960 destination
= scm_mkstrport (SCM_INUM0
,
961 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
965 SCM_VALIDATE_OPORT_VALUE (1,destination
);
967 SCM_VALIDATE_STRING(2,message
);
968 SCM_VALIDATE_LIST(3,args
);
970 SCM_COERCE_SUBSTR (message
);
971 start
= SCM_ROCHARS (message
);
972 for (p
= start
; *p
!= '\0'; ++p
)
975 if (SCM_IMP (args
) || SCM_NCONSP (args
))
986 scm_lfwrite (start
, p
- start
- 1, destination
);
987 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
988 args
= SCM_CDR (args
);
991 scm_lfwrite (start
, p
- start
, destination
);
994 answer
= scm_strport_to_string (destination
);
1001 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1004 #define FUNC_NAME s_scm_newline
1006 if (SCM_UNBNDP (port
))
1007 port
= scm_cur_outp
;
1009 SCM_VALIDATE_OPORT_VALUE (1,port
);
1011 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1012 return SCM_UNSPECIFIED
;
1016 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1017 (SCM chr
, SCM port
),
1019 #define FUNC_NAME s_scm_write_char
1021 if (SCM_UNBNDP (port
))
1022 port
= scm_cur_outp
;
1024 SCM_VALIDATE_ICHR (1,chr
);
1025 SCM_VALIDATE_OPORT_VALUE (2,port
);
1027 scm_putc ((int) SCM_ICHR (chr
), SCM_COERCE_OUTPORT (port
));
1031 scm_close_port (port
);
1034 return SCM_UNSPECIFIED
;
1040 /* Call back to Scheme code to do the printing of special objects
1041 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1042 * containing PORT and PSTATE. This object can be used as the port for
1043 * display/write etc to continue the current print chain. The REVEALED
1044 * field of PSTATE is set to true to indicate that the print state has
1045 * escaped to Scheme and thus has to be freed by the GC.
1048 long scm_tc16_port_with_ps
;
1050 /* Print exactly as the port itself would */
1053 print_port_with_ps (SCM obj
, SCM port
, scm_print_state
*pstate
)
1055 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1056 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1060 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1063 SCM pair
= scm_cons (port
, pstate
->handle
);
1064 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, pair
);
1065 pstate
->revealed
= 1;
1066 return scm_apply (proc
, exp
, scm_cons (pwps
, scm_listofnull
));
1069 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 2, 0, 0,
1070 (SCM port
, SCM pstate
),
1072 #define FUNC_NAME s_scm_port_with_print_state
1075 SCM_VALIDATE_OPORT_VALUE (1,port
);
1076 SCM_VALIDATE_PRINTSTATE (2,pstate
);
1077 port
= SCM_COERCE_OUTPORT (port
);
1078 SCM_NEWSMOB (pwps
, scm_tc16_port_with_ps
, scm_cons (port
, pstate
));
1083 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1086 #define FUNC_NAME s_scm_get_print_state
1088 if (SCM_PORT_WITH_PS_P (port
))
1089 return SCM_PORT_WITH_PS_PS (port
);
1090 if (SCM_OUTPORTP (port
))
1092 RETURN_SCM_WTA (1,port
);
1101 SCM vtable
, layout
, type
;
1103 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1104 vtable
= scm_make_vtable_vtable (scm_make_struct_layout (scm_nullstr
),
1107 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
1108 type
= scm_make_struct (vtable
, SCM_INUM0
, SCM_LIST1 (layout
));
1109 scm_set_struct_vtable_name_x (type
, SCM_CAR (scm_intern0 ("print-state")));
1110 print_state_pool
= scm_permanent_object (scm_cons (type
, SCM_EOL
));
1112 scm_print_state_vtable
= type
;
1114 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1115 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1116 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1117 scm_set_smob_print (scm_tc16_port_with_ps
, print_port_with_ps
);