1 /* Copyright (C) 1995-1999,2000,2001, 2002, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 #include "libguile/_scm.h"
30 #include "libguile/chars.h"
31 #include "libguile/continuations.h"
32 #include "libguile/smob.h"
33 #include "libguile/eval.h"
34 #include "libguile/macros.h"
35 #include "libguile/procprop.h"
36 #include "libguile/read.h"
37 #include "libguile/weaks.h"
38 #include "libguile/programs.h"
39 #include "libguile/unif.h"
40 #include "libguile/alist.h"
41 #include "libguile/struct.h"
42 #include "libguile/objects.h"
43 #include "libguile/ports.h"
44 #include "libguile/root.h"
45 #include "libguile/strings.h"
46 #include "libguile/strports.h"
47 #include "libguile/vectors.h"
48 #include "libguile/lang.h"
49 #include "libguile/numbers.h"
51 #include "libguile/validate.h"
52 #include "libguile/print.h"
54 #include "libguile/private-options.h"
58 /* {Names of immediate symbols}
60 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
63 /* This table must agree with the list of flags in tags.h. */
64 static const char *iflagnames
[] =
73 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
76 /* Elisp nil value. This is its Scheme name; whenever it's printed in
77 * Elisp, it should appear as the symbol `nil'. */
81 SCM_SYMBOL (sym_reader
, "reader");
83 scm_t_option scm_print_opts
[] = {
84 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK (SCM_BOOL_F
),
85 "Hook for printing closures (should handle macros as well)." },
86 { SCM_OPTION_BOOLEAN
, "source", 0,
87 "Print closures with source." },
88 { SCM_OPTION_SCM
, "highlight-prefix", (unsigned long)SCM_BOOL_F
,
89 "The string to print before highlighted values." },
90 { SCM_OPTION_SCM
, "highlight-suffix", (unsigned long)SCM_BOOL_F
,
91 "The string to print after highlighted values." },
92 { SCM_OPTION_SCM
, "quote-keywordish-symbols", (unsigned long)SCM_BOOL_F
,
93 "How to print symbols that have a colon as their first or last character. "
94 "The value '#f' does not quote the colons; '#t' quotes them; "
95 "'reader' quotes them when the reader option 'keywords' is not '#f'."
101 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
103 "Option interface for the print options. Instead of using\n"
104 "this procedure directly, use the procedures\n"
105 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
106 "and @code{print-options}.")
107 #define FUNC_NAME s_scm_print_options
109 SCM ans
= scm_options (setting
,
117 /* {Printing of Scheme Objects}
120 /* Detection of circular references.
122 * Due to other constraints in the implementation, this code has bad
123 * time complexity (O (depth * N)), The printer code can be
124 * rewritten to be O(N).
126 #define PUSH_REF(pstate, obj) \
129 PSTATE_STACK_SET (pstate, pstate->top, obj); \
131 if (pstate->top == pstate->ceiling) \
132 grow_ref_stack (pstate); \
135 #define ENTER_NESTED_DATA(pstate, obj, label) \
138 register unsigned long i; \
139 for (i = 0; i < pstate->top; ++i) \
140 if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
142 if (pstate->fancyp) \
144 if (pstate->top - pstate->list_offset >= pstate->level) \
146 scm_putc ('#', port); \
150 PUSH_REF(pstate, obj); \
153 #define EXIT_NESTED_DATA(pstate) \
157 PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
161 SCM scm_print_state_vtable
= SCM_BOOL_F
;
162 static SCM print_state_pool
= SCM_EOL
;
163 scm_i_pthread_mutex_t print_state_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
165 #ifdef GUILE_DEBUG /* Used for debugging purposes */
167 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
169 "Return the current-pstate -- the car of the\n"
170 "@code{print_state_pool}. @code{current-pstate} is only\n"
171 "included in @code{--enable-guile-debug} builds.")
172 #define FUNC_NAME s_scm_current_pstate
174 if (!scm_is_null (print_state_pool
))
175 return SCM_CAR (print_state_pool
);
183 #define PSTATE_SIZE 50L
186 make_print_state (void)
189 = scm_make_struct (scm_print_state_vtable
, SCM_INUM0
, SCM_EOL
);
190 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
191 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
192 pstate
->ceiling
= SCM_SIMPLE_VECTOR_LENGTH (pstate
->ref_vect
);
193 pstate
->highlight_objects
= SCM_EOL
;
198 scm_make_print_state ()
200 SCM answer
= SCM_BOOL_F
;
202 /* First try to allocate a print state from the pool */
203 scm_i_pthread_mutex_lock (&print_state_mutex
);
204 if (!scm_is_null (print_state_pool
))
206 answer
= SCM_CAR (print_state_pool
);
207 print_state_pool
= SCM_CDR (print_state_pool
);
209 scm_i_pthread_mutex_unlock (&print_state_mutex
);
211 return scm_is_false (answer
) ? make_print_state () : answer
;
215 scm_free_print_state (SCM print_state
)
218 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
219 /* Cleanup before returning print state to pool.
220 * It is better to do it here. Doing it in scm_prin1
221 * would cost more since that function is called much more
225 pstate
->revealed
= 0;
226 pstate
->highlight_objects
= SCM_EOL
;
227 scm_i_pthread_mutex_lock (&print_state_mutex
);
228 handle
= scm_cons (print_state
, print_state_pool
);
229 print_state_pool
= handle
;
230 scm_i_pthread_mutex_unlock (&print_state_mutex
);
234 scm_i_port_with_print_state (SCM port
, SCM print_state
)
236 if (SCM_UNBNDP (print_state
))
238 if (SCM_PORT_WITH_PS_P (port
))
241 print_state
= scm_make_print_state ();
242 /* port does not need to be coerced since it doesn't have ps */
245 port
= SCM_COERCE_OUTPORT (port
);
246 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps
,
247 SCM_UNPACK (scm_cons (port
, print_state
)));
251 grow_ref_stack (scm_print_state
*pstate
)
253 SCM old_vect
= pstate
->ref_vect
;
254 size_t old_size
= SCM_SIMPLE_VECTOR_LENGTH (old_vect
);
255 size_t new_size
= 2 * pstate
->ceiling
;
256 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
259 for (i
= 0; i
!= old_size
; ++i
)
260 SCM_SIMPLE_VECTOR_SET (new_vect
, i
, SCM_SIMPLE_VECTOR_REF (old_vect
, i
));
262 pstate
->ref_vect
= new_vect
;
263 pstate
->ceiling
= new_size
;
266 #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
267 #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
270 print_circref (SCM port
, scm_print_state
*pstate
, SCM ref
)
273 long self
= pstate
->top
- 1;
275 if (scm_is_pair (PSTATE_STACK_REF (pstate
, i
)))
279 if (!scm_is_pair (PSTATE_STACK_REF (pstate
, i
-1))
280 || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate
, i
-1)),
281 SCM_CDR (PSTATE_STACK_REF (pstate
, i
))))
287 for (i
= pstate
->top
- 1; 1; --i
)
288 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), ref
))
290 scm_putc ('#', port
);
291 scm_intprint (i
- self
, 10, port
);
292 scm_putc ('#', port
);
295 /* Print the name of a symbol. */
298 quote_keywordish_symbol (SCM symbol
)
302 if (scm_i_symbol_ref (symbol
, 0) != ':'
303 && scm_i_symbol_ref (symbol
, scm_i_symbol_length (symbol
) - 1) != ':')
306 option
= SCM_PRINT_KEYWORD_STYLE
;
307 if (scm_is_false (option
))
309 if (scm_is_eq (option
, sym_reader
))
310 return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE
));
315 scm_i_print_symbol_name (SCM str
, SCM port
)
317 /* This points to the first character that has not yet been written to the
320 /* This points to the character we're currently looking at. */
322 /* If the name contains weird characters, we'll escape them with
323 * backslashes and set this flag; it indicates that we should surround the
324 * name with "#{" and "}#". */
326 /* Backslashes are not sufficient to make a name weird, but if a name is
327 * weird because of other characters, backslahes need to be escaped too.
328 * The first time we see a backslash, we set maybe_weird, and mw_pos points
329 * to the backslash. Then if the name turns out to be weird, we re-process
330 * everything starting from mw_pos.
331 * We could instead make backslashes always weird. This is not necessary
332 * to ensure that the output is (read)-able, but it would make this code
333 * simpler and faster. */
336 size_t len
= scm_i_symbol_length (str
);
337 scm_t_wchar str0
= scm_i_symbol_ref (str
, 0);
339 if (len
== 0 || str0
== '\'' || str0
== '`' || str0
== ','
340 || quote_keywordish_symbol (str
)
341 || (str0
== '.' && len
== 1)
342 || scm_is_true (scm_i_string_to_number (scm_symbol_to_string (str
), 10)))
344 scm_lfwrite ("#{", 2, port
);
348 for (end
= pos
; end
< len
; ++end
)
349 switch (scm_i_symbol_ref (str
, end
))
351 #ifdef BRACKETS_AS_PARENS
360 case SCM_WHITE_SPACES
:
361 case SCM_LINE_INCREMENTORS
:
370 scm_lfwrite ("#{", 2, port
);
374 scm_lfwrite_substr (scm_symbol_to_string (str
), pos
, end
, port
);
378 buf
[1] = (char) (unsigned char) scm_i_symbol_ref (str
, end
);
379 scm_lfwrite (buf
, 2, port
);
396 scm_lfwrite_substr (scm_symbol_to_string (str
), pos
, end
, port
);
398 scm_lfwrite ("}#", 2, port
);
402 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
404 SCM symbol
= scm_from_locale_symboln (str
, len
);
405 return scm_i_print_symbol_name (symbol
, port
);
408 /* Print generally. Handles both write and display according to PSTATE.
410 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
411 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
413 static void iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
);
416 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
419 && scm_is_true (scm_memq (exp
, pstate
->highlight_objects
)))
421 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX
, port
);
422 iprin1 (exp
, port
, pstate
);
423 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX
, port
);
426 iprin1 (exp
, port
, pstate
);
430 iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
432 switch (SCM_ITAG3 (exp
))
434 case scm_tc3_closure
:
437 /* These tc3 tags should never occur in an immediate value. They are
438 * only used in cell types of non-immediates, i. e. the value returned
439 * by SCM_CELL_TYPE (exp) can use these tags.
441 scm_ipruk ("immediate", exp
, port
);
445 scm_intprint (SCM_I_INUM (exp
), 10, port
);
450 scm_t_wchar i
= SCM_CHAR (exp
);
453 if (SCM_WRITINGP (pstate
))
455 scm_puts ("#\\", port
);
456 name
= scm_i_charname (exp
);
458 scm_puts (name
, port
);
459 else if (uc_is_general_category_withtable (i
, UC_CATEGORY_MASK_L
463 | UC_CATEGORY_MASK_S
))
464 /* Print the character if is graphic character. */
467 SCM wstr
= scm_i_make_wide_string (1, &wbuf
);
472 enc
= scm_i_get_port_encoding (port
);
474 if (enc
== NULL
&& i
<= 0xFF)
476 /* Character is graphic and Latin-1. Print it */
477 scm_lfwrite_str (wstr
, port
);
481 buf
= u32_conv_to_encoding (enc
,
483 (scm_t_uint32
*) wbuf
,
489 /* Character is graphic. Print it. */
490 scm_lfwrite_str (wstr
, port
);
494 /* Character is graphic but unrepresentable in
495 this port's encoding. */
496 scm_intprint (i
, 8, port
);
500 /* Character is a non-graphical character. */
501 scm_intprint (i
, 8, port
);
504 scm_i_charprint (i
, port
);
506 else if (SCM_IFLAGP (exp
)
507 && ((size_t) SCM_IFLAGNUM (exp
) < (sizeof iflagnames
/ sizeof (char *))))
509 scm_puts (iflagnames
[SCM_IFLAGNUM (exp
)], port
);
511 else if (SCM_ISYMP (exp
))
513 scm_i_print_isym (exp
, port
);
515 else if (SCM_ILOCP (exp
))
517 scm_i_print_iloc (exp
, port
);
521 /* unknown immediate value */
522 scm_ipruk ("immediate", exp
, port
);
526 switch (SCM_TYP7 (exp
))
530 ENTER_NESTED_DATA (pstate
, exp
, circref
);
531 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
533 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
536 pwps
= scm_i_port_with_print_state (port
, pstate
->handle
);
537 pstate
->revealed
= 1;
538 scm_call_generic_2 (print
, exp
, pwps
);
543 scm_print_struct (exp
, port
, pstate
);
545 EXIT_NESTED_DATA (pstate
);
548 case scm_tcs_cons_imcar
:
549 case scm_tcs_cons_nimcar
:
550 ENTER_NESTED_DATA (pstate
, exp
, circref
);
551 scm_iprlist ("(", exp
, ')', port
, pstate
);
552 EXIT_NESTED_DATA (pstate
);
555 print_circref (port
, pstate
, exp
);
557 case scm_tcs_closures
:
558 if (scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE
))
559 || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE
,
562 SCM formals
= SCM_CLOSURE_FORMALS (exp
);
563 scm_puts ("#<procedure", port
);
564 scm_putc (' ', port
);
565 scm_iprin1 (scm_procedure_name (exp
), port
, pstate
);
566 scm_putc (' ', port
);
567 if (SCM_PRINT_SOURCE_P
)
569 SCM env
= SCM_ENV (exp
);
570 SCM xenv
= SCM_EXTEND_ENV (formals
, SCM_EOL
, env
);
571 SCM src
= scm_i_unmemocopy_body (SCM_CODE (exp
), xenv
);
572 ENTER_NESTED_DATA (pstate
, exp
, circref
);
573 scm_iprin1 (src
, port
, pstate
);
574 EXIT_NESTED_DATA (pstate
);
577 scm_iprin1 (formals
, port
, pstate
);
578 scm_putc ('>', port
);
582 switch SCM_TYP16 (exp
) {
584 scm_bigprint (exp
, port
, pstate
);
587 scm_print_real (exp
, port
, pstate
);
589 case scm_tc16_complex
:
590 scm_print_complex (exp
, port
, pstate
);
592 case scm_tc16_fraction
:
593 scm_i_print_fraction (exp
, port
, pstate
);
598 if (SCM_WRITINGP (pstate
))
601 static char const hex
[] = "0123456789abcdef";
605 scm_putc ('"', port
);
606 len
= scm_i_string_length (exp
);
607 for (i
= 0; i
< len
; ++i
)
609 scm_t_wchar ch
= scm_i_string_ref (exp
, i
);
612 if (ch
== ' ' || ch
== '\n')
617 else if (ch
== '"' || ch
== '\\')
619 scm_putc ('\\', port
);
620 scm_i_charprint (ch
, port
);
624 if (uc_is_general_category_withtable
626 UC_CATEGORY_MASK_L
| UC_CATEGORY_MASK_M
|
627 UC_CATEGORY_MASK_N
| UC_CATEGORY_MASK_P
|
630 /* Print the character since it is a graphic
633 SCM wstr
= scm_i_make_wide_string (1, &wbuf
);
637 if (scm_i_get_port_encoding (port
))
639 wstr
= scm_i_make_wide_string (1, &wbuf
);
641 buf
= u32_conv_to_encoding (scm_i_get_port_encoding (port
),
643 (scm_t_uint32
*) wbuf
,
649 /* Character is graphic and representable in
650 this encoding. Print it. */
651 scm_lfwrite_str (wstr
, port
);
666 /* Character is graphic but unrepresentable in
667 this port's encoding or is not graphic. */
672 buf
[2] = hex
[ch
/ 16];
673 buf
[3] = hex
[ch
% 16];
674 scm_lfwrite (buf
, 4, port
);
676 else if (ch
<= 0xFFFF)
680 buf
[2] = hex
[(ch
& 0xF000) >> 12];
681 buf
[3] = hex
[(ch
& 0xF00) >> 8];
682 buf
[4] = hex
[(ch
& 0xF0) >> 4];
683 buf
[5] = hex
[(ch
& 0xF)];
684 scm_lfwrite (buf
, 6, port
);
687 else if (ch
> 0xFFFF)
691 buf
[2] = hex
[(ch
& 0xF00000) >> 20];
692 buf
[3] = hex
[(ch
& 0xF0000) >> 16];
693 buf
[4] = hex
[(ch
& 0xF000) >> 12];
694 buf
[5] = hex
[(ch
& 0xF00) >> 8];
695 buf
[6] = hex
[(ch
& 0xF0) >> 4];
696 buf
[7] = hex
[(ch
& 0xF)];
697 scm_lfwrite (buf
, 8, port
);
702 scm_putc ('"', port
);
703 scm_remember_upto_here_1 (exp
);
706 scm_lfwrite_str (exp
, port
);
707 scm_remember_upto_here_1 (exp
);
710 if (scm_i_symbol_is_interned (exp
))
712 scm_i_print_symbol_name (exp
, port
);
713 scm_remember_upto_here_1 (exp
);
717 scm_puts ("#<uninterned-symbol ", port
);
718 scm_i_print_symbol_name (exp
, port
);
719 scm_putc (' ', port
);
720 scm_uintprint (SCM_UNPACK (exp
), 16, port
);
721 scm_putc ('>', port
);
724 case scm_tc7_variable
:
725 scm_i_variable_print (exp
, port
, pstate
);
727 case scm_tc7_program
:
728 scm_i_program_print (exp
, port
, pstate
);
731 ENTER_NESTED_DATA (pstate
, exp
, circref
);
732 if (SCM_IS_WHVEC (exp
))
733 scm_puts ("#wh(", port
);
735 scm_puts ("#w(", port
);
736 goto common_vector_printer
;
739 ENTER_NESTED_DATA (pstate
, exp
, circref
);
740 scm_puts ("#(", port
);
741 common_vector_printer
:
744 long last
= SCM_SIMPLE_VECTOR_LENGTH (exp
) - 1;
747 && SCM_SIMPLE_VECTOR_LENGTH (exp
) > pstate
->length
)
749 last
= pstate
->length
- 1;
752 for (i
= 0; i
< last
; ++i
)
755 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
756 scm_putc (' ', port
);
761 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
764 scm_puts (" ...", port
);
765 scm_putc (')', port
);
767 EXIT_NESTED_DATA (pstate
);
771 SCM name
= scm_symbol_to_string (SCM_SUBR_NAME (exp
));
772 scm_puts (SCM_SUBR_GENERIC (exp
)
773 ? "#<primitive-generic "
774 : "#<primitive-procedure ",
776 scm_lfwrite_str (name
, port
);
777 scm_putc ('>', port
);
781 scm_puts ("#<procedure-with-setter", port
);
783 SCM name
= scm_procedure_name (exp
);
784 if (scm_is_true (name
))
786 scm_putc (' ', port
);
787 scm_display (name
, port
);
790 scm_putc ('>', port
);
794 register long i
= SCM_PTOBNUM (exp
);
796 && scm_ptobs
[i
].print
797 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
802 ENTER_NESTED_DATA (pstate
, exp
, circref
);
803 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
804 EXIT_NESTED_DATA (pstate
);
808 scm_ipruk ("type", exp
, port
);
813 /* Print states are necessary for circular reference safe printing.
814 * They are also expensive to allocate. Therefore print states are
815 * kept in a pool so that they can be reused.
818 /* The PORT argument can also be a print-state/port pair, which will
819 * then be used instead of allocating a new print state. This is
820 * useful for continuing a chain of print calls from Scheme. */
823 scm_prin1 (SCM exp
, SCM port
, int writingp
)
825 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
827 scm_print_state
*pstate
;
830 /* If PORT is a print-state/port pair, use that. Else create a new
833 if (SCM_PORT_WITH_PS_P (port
))
835 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
836 port
= SCM_PORT_WITH_PS_PORT (port
);
840 /* First try to allocate a print state from the pool */
841 scm_i_pthread_mutex_lock (&print_state_mutex
);
842 if (!scm_is_null (print_state_pool
))
844 handle
= print_state_pool
;
845 print_state_pool
= SCM_CDR (print_state_pool
);
847 scm_i_pthread_mutex_unlock (&print_state_mutex
);
848 if (scm_is_false (handle
))
849 handle
= scm_list_1 (make_print_state ());
850 pstate_scm
= SCM_CAR (handle
);
853 pstate
= SCM_PRINT_STATE (pstate_scm
);
854 old_writingp
= pstate
->writingp
;
855 pstate
->writingp
= writingp
;
856 scm_iprin1 (exp
, port
, pstate
);
857 pstate
->writingp
= old_writingp
;
859 /* Return print state to pool if it has been created above and
860 hasn't escaped to Scheme. */
862 if (scm_is_true (handle
) && !pstate
->revealed
)
864 scm_i_pthread_mutex_lock (&print_state_mutex
);
865 SCM_SETCDR (handle
, print_state_pool
);
866 print_state_pool
= handle
;
867 scm_i_pthread_mutex_unlock (&print_state_mutex
);
871 /* Print a character.
874 scm_i_charprint (scm_t_wchar ch
, SCM port
)
877 SCM wstr
= scm_i_make_wide_string (1, &wbuf
);
880 scm_lfwrite_str (wstr
, port
);
887 scm_intprint (scm_t_intmax n
, int radix
, SCM port
)
889 char num_buf
[SCM_INTBUFLEN
];
890 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
894 scm_uintprint (scm_t_uintmax n
, int radix
, SCM port
)
896 char num_buf
[SCM_INTBUFLEN
];
897 scm_lfwrite (num_buf
, scm_iuint2str (n
, radix
, num_buf
), port
);
900 /* Print an object of unrecognized type.
904 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
906 scm_puts ("#<unknown-", port
);
907 scm_puts (hdr
, port
);
908 if (scm_in_heap_p (ptr
))
910 scm_puts (" (0x", port
);
911 scm_uintprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
912 scm_puts (" . 0x", port
);
913 scm_uintprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
914 scm_puts (") @", port
);
916 scm_puts (" 0x", port
);
917 scm_uintprint (SCM_UNPACK (ptr
), 16, port
);
918 scm_putc ('>', port
);
925 scm_iprlist (char *hdr
, SCM exp
, int tlr
, SCM port
, scm_print_state
*pstate
)
927 register SCM hare
, tortoise
;
928 long floor
= pstate
->top
- 2;
929 scm_puts (hdr
, port
);
934 /* Run a hare and tortoise so that total time complexity will be
935 O(depth * N) instead of O(N^2). */
936 hare
= SCM_CDR (exp
);
938 while (scm_is_pair (hare
))
940 if (scm_is_eq (hare
, tortoise
))
942 hare
= SCM_CDR (hare
);
943 if (!scm_is_pair (hare
))
945 hare
= SCM_CDR (hare
);
946 tortoise
= SCM_CDR (tortoise
);
949 /* No cdr cycles intrinsic to this list */
950 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
951 for (exp
= SCM_CDR (exp
); scm_is_pair (exp
); exp
= SCM_CDR (exp
))
955 for (i
= floor
; i
>= 0; --i
)
956 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
958 PUSH_REF (pstate
, exp
);
959 scm_putc (' ', port
);
961 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
963 if (!SCM_NULL_OR_NIL_P (exp
))
965 scm_puts (" . ", port
);
966 scm_iprin1 (exp
, port
, pstate
);
970 scm_putc (tlr
, port
);
971 pstate
->top
= floor
+ 2;
976 long n
= pstate
->length
;
978 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
979 exp
= SCM_CDR (exp
); --n
;
980 for (; scm_is_pair (exp
); exp
= SCM_CDR (exp
))
982 register unsigned long i
;
984 for (i
= 0; i
< pstate
->top
; ++i
)
985 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
991 scm_puts (" ...", port
);
997 PUSH_REF(pstate
, exp
);
998 ++pstate
->list_offset
;
999 scm_putc (' ', port
);
1001 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
1004 if (!SCM_NULL_OR_NIL_P (exp
))
1006 scm_puts (" . ", port
);
1007 scm_iprin1 (exp
, port
, pstate
);
1010 pstate
->list_offset
-= pstate
->top
- floor
- 2;
1014 pstate
->list_offset
-= pstate
->top
- floor
- 2;
1017 scm_puts (" . ", port
);
1018 print_circref (port
, pstate
, exp
);
1025 scm_valid_oport_value_p (SCM val
)
1027 return (SCM_OPOUTPORTP (val
)
1028 || (SCM_PORT_WITH_PS_P (val
)
1029 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
1032 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
1035 scm_write (SCM obj
, SCM port
)
1037 if (SCM_UNBNDP (port
))
1038 port
= scm_current_output_port ();
1040 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
1042 scm_prin1 (obj
, port
, 1);
1047 scm_close_port (port
);
1051 return SCM_UNSPECIFIED
;
1055 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
1058 scm_display (SCM obj
, SCM port
)
1060 if (SCM_UNBNDP (port
))
1061 port
= scm_current_output_port ();
1063 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
1065 scm_prin1 (obj
, port
, 0);
1070 scm_close_port (port
);
1074 return SCM_UNSPECIFIED
;
1078 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
1079 (SCM destination
, SCM message
, SCM args
),
1080 "Write @var{message} to @var{destination}, defaulting to\n"
1081 "the current output port.\n"
1082 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
1083 "@code{~S} (was @code{%S}) escapes. When printed,\n"
1084 "the escapes are replaced with corresponding members of\n"
1086 "@code{~A} formats using @code{display} and @code{~S} formats\n"
1087 "using @code{write}.\n"
1088 "If @var{destination} is @code{#t}, then use the current output\n"
1089 "port, if @var{destination} is @code{#f}, then return a string\n"
1090 "containing the formatted text. Does not add a trailing newline.")
1091 #define FUNC_NAME s_scm_simple_format
1093 SCM port
, answer
= SCM_UNSPECIFIED
;
1094 int fReturnString
= 0;
1096 size_t start
, p
, end
;
1098 if (scm_is_eq (destination
, SCM_BOOL_T
))
1100 destination
= port
= scm_current_output_port ();
1102 else if (scm_is_false (destination
))
1105 port
= scm_mkstrport (SCM_INUM0
,
1106 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
1107 SCM_OPN
| SCM_WRTNG
,
1113 SCM_VALIDATE_OPORT_VALUE (1, destination
);
1114 port
= SCM_COERCE_OUTPORT (destination
);
1116 SCM_VALIDATE_STRING (2, message
);
1117 SCM_VALIDATE_REST_ARGUMENT (args
);
1121 end
= scm_i_string_length (message
);
1122 for (p
= start
; p
!= end
; ++p
)
1123 if (scm_i_string_ref (message
, p
) == '~')
1128 switch (scm_i_string_ref (message
, p
))
1137 scm_lfwrite_substr (message
, start
, p
, port
);
1141 scm_lfwrite_substr (message
, start
, p
- 1, port
);
1146 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
1147 scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message
, p
))));
1152 if (!scm_is_pair (args
))
1153 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1154 scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message
, p
))));
1156 scm_lfwrite_substr (message
, start
, p
- 1, port
);
1157 /* we pass destination here */
1158 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1159 args
= SCM_CDR (args
);
1163 scm_lfwrite_substr (message
, start
, p
, port
);
1164 if (!scm_is_eq (args
, SCM_EOL
))
1165 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1166 scm_list_1 (scm_length (args
)));
1169 answer
= scm_strport_to_string (destination
);
1171 return scm_return_first (answer
, message
);
1176 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1178 "Send a newline to @var{port}.\n"
1179 "If @var{port} is omitted, send to the current output port.")
1180 #define FUNC_NAME s_scm_newline
1182 if (SCM_UNBNDP (port
))
1183 port
= scm_current_output_port ();
1185 SCM_VALIDATE_OPORT_VALUE (1, port
);
1187 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1188 return SCM_UNSPECIFIED
;
1192 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1193 (SCM chr
, SCM port
),
1194 "Send character @var{chr} to @var{port}.")
1195 #define FUNC_NAME s_scm_write_char
1197 if (SCM_UNBNDP (port
))
1198 port
= scm_current_output_port ();
1200 SCM_VALIDATE_CHAR (1, chr
);
1201 SCM_VALIDATE_OPORT_VALUE (2, port
);
1203 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1208 scm_close_port (port
);
1212 return SCM_UNSPECIFIED
;
1218 /* Call back to Scheme code to do the printing of special objects
1219 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1220 * containing PORT and PSTATE. This object can be used as the port for
1221 * display/write etc to continue the current print chain. The REVEALED
1222 * field of PSTATE is set to true to indicate that the print state has
1223 * escaped to Scheme and thus has to be freed by the GC.
1226 scm_t_bits scm_tc16_port_with_ps
;
1228 /* Print exactly as the port itself would */
1231 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1233 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1234 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1238 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1240 pstate
->revealed
= 1;
1241 return scm_call_2 (proc
, exp
,
1242 scm_i_port_with_print_state (port
, pstate
->handle
));
1245 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 1, 1, 0,
1246 (SCM port
, SCM pstate
),
1247 "Create a new port which behaves like @var{port}, but with an\n"
1248 "included print state @var{pstate}. @var{pstate} is optional.\n"
1249 "If @var{pstate} isn't supplied and @var{port} already has\n"
1250 "a print state, the old print state is reused.")
1251 #define FUNC_NAME s_scm_port_with_print_state
1253 SCM_VALIDATE_OPORT_VALUE (1, port
);
1254 if (!SCM_UNBNDP (pstate
))
1255 SCM_VALIDATE_PRINTSTATE (2, pstate
);
1256 return scm_i_port_with_print_state (port
, pstate
);
1260 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1262 "Return the print state of the port @var{port}. If @var{port}\n"
1263 "has no associated print state, @code{#f} is returned.")
1264 #define FUNC_NAME s_scm_get_print_state
1266 if (SCM_PORT_WITH_PS_P (port
))
1267 return SCM_PORT_WITH_PS_PS (port
);
1268 if (SCM_OUTPUT_PORT_P (port
))
1270 SCM_WRONG_TYPE_ARG (1, port
);
1279 SCM vtable
, layout
, type
;
1281 scm_init_opts (scm_print_options
, scm_print_opts
);
1283 scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"),
1284 scm_from_locale_string ("{"),
1285 scm_from_locale_symbol ("highlight-suffix"),
1286 scm_from_locale_string ("}")));
1288 scm_gc_register_root (&print_state_pool
);
1289 scm_gc_register_root (&scm_print_state_vtable
);
1290 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1292 scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT
));
1293 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1294 scm_set_struct_vtable_name_x (type
, scm_from_locale_symbol ("print-state"));
1295 scm_print_state_vtable
= type
;
1297 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1298 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1299 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1300 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1302 #include "libguile/print.x"
1304 scm_print_opts
[SCM_PRINT_KEYWORD_STYLE_I
].val
= SCM_UNPACK (sym_reader
);