1 /* Copyright (C) 1995-1999,2000,2001, 2002, 2003, 2004, 2006, 2008, 2009, 2010, 2011 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/control.h"
34 #include "libguile/eval.h"
35 #include "libguile/macros.h"
36 #include "libguile/procprop.h"
37 #include "libguile/read.h"
38 #include "libguile/weaks.h"
39 #include "libguile/programs.h"
40 #include "libguile/alist.h"
41 #include "libguile/struct.h"
42 #include "libguile/ports.h"
43 #include "libguile/root.h"
44 #include "libguile/strings.h"
45 #include "libguile/strports.h"
46 #include "libguile/vectors.h"
47 #include "libguile/numbers.h"
48 #include "libguile/vm.h"
50 #include "libguile/validate.h"
51 #include "libguile/print.h"
53 #include "libguile/private-options.h"
57 /* Character printers. */
59 static int display_character (scm_t_wchar
, SCM
,
60 scm_t_string_failed_conversion_handler
);
61 static void write_character (scm_t_wchar
, SCM
, int);
65 /* {Names of immediate symbols}
67 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
70 /* This table must agree with the list of flags in tags.h. */
71 static const char *iflagnames
[] =
74 "#nil", /* Elisp nil value. Should print from elisp as symbol `nil'. */
75 "#<XXX UNUSED LISP FALSE -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
78 "#<XXX UNUSED BOOLEAN 0 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
79 "#<XXX UNUSED BOOLEAN 1 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
80 "#<XXX UNUSED BOOLEAN 2 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
85 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
89 SCM_SYMBOL (sym_reader
, "reader");
91 scm_t_option scm_print_opts
[] = {
92 { SCM_OPTION_SCM
, "highlight-prefix", (scm_t_bits
)SCM_BOOL_F
,
93 "The string to print before highlighted values." },
94 { SCM_OPTION_SCM
, "highlight-suffix", (scm_t_bits
)SCM_BOOL_F
,
95 "The string to print after highlighted values." },
96 { SCM_OPTION_SCM
, "quote-keywordish-symbols", (scm_t_bits
)SCM_BOOL_F
,
97 "How to print symbols that have a colon as their first or last character. "
98 "The value '#f' does not quote the colons; '#t' quotes them; "
99 "'reader' quotes them when the reader option 'keywords' is not '#f'."
104 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
106 "Option interface for the print options. Instead of using\n"
107 "this procedure directly, use the procedures\n"
108 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
109 "and @code{print-options}.")
110 #define FUNC_NAME s_scm_print_options
112 SCM ans
= scm_options (setting
,
120 /* {Printing of Scheme Objects}
123 /* Detection of circular references.
125 * Due to other constraints in the implementation, this code has bad
126 * time complexity (O (depth * N)), The printer code can be
127 * rewritten to be O(N).
129 #define PUSH_REF(pstate, obj) \
132 PSTATE_STACK_SET (pstate, pstate->top, obj); \
134 if (pstate->top == pstate->ceiling) \
135 grow_ref_stack (pstate); \
138 #define ENTER_NESTED_DATA(pstate, obj, label) \
141 register unsigned long i; \
142 for (i = 0; i < pstate->top; ++i) \
143 if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
145 if (pstate->fancyp) \
147 if (pstate->top - pstate->list_offset >= pstate->level) \
149 scm_putc ('#', port); \
153 PUSH_REF(pstate, obj); \
156 #define EXIT_NESTED_DATA(pstate) \
160 PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
164 SCM scm_print_state_vtable
= SCM_BOOL_F
;
165 static SCM print_state_pool
= SCM_EOL
;
166 scm_i_pthread_mutex_t print_state_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
168 #ifdef GUILE_DEBUG /* Used for debugging purposes */
170 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
172 "Return the current-pstate -- the car of the\n"
173 "@code{print_state_pool}. @code{current-pstate} is only\n"
174 "included in @code{--enable-guile-debug} builds.")
175 #define FUNC_NAME s_scm_current_pstate
177 if (!scm_is_null (print_state_pool
))
178 return SCM_CAR (print_state_pool
);
186 #define PSTATE_SIZE 50L
189 make_print_state (void)
192 = scm_make_struct (scm_print_state_vtable
, SCM_INUM0
, SCM_EOL
);
193 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
194 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
195 pstate
->ceiling
= SCM_SIMPLE_VECTOR_LENGTH (pstate
->ref_vect
);
196 pstate
->highlight_objects
= SCM_EOL
;
201 scm_make_print_state ()
203 SCM answer
= SCM_BOOL_F
;
205 /* First try to allocate a print state from the pool */
206 scm_i_pthread_mutex_lock (&print_state_mutex
);
207 if (!scm_is_null (print_state_pool
))
209 answer
= SCM_CAR (print_state_pool
);
210 print_state_pool
= SCM_CDR (print_state_pool
);
212 scm_i_pthread_mutex_unlock (&print_state_mutex
);
214 return scm_is_false (answer
) ? make_print_state () : answer
;
218 scm_free_print_state (SCM print_state
)
221 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
222 /* Cleanup before returning print state to pool.
223 * It is better to do it here. Doing it in scm_prin1
224 * would cost more since that function is called much more
228 pstate
->revealed
= 0;
229 pstate
->highlight_objects
= SCM_EOL
;
230 scm_i_pthread_mutex_lock (&print_state_mutex
);
231 handle
= scm_cons (print_state
, print_state_pool
);
232 print_state_pool
= handle
;
233 scm_i_pthread_mutex_unlock (&print_state_mutex
);
237 scm_i_port_with_print_state (SCM port
, SCM print_state
)
239 if (SCM_UNBNDP (print_state
))
241 if (SCM_PORT_WITH_PS_P (port
))
244 print_state
= scm_make_print_state ();
245 /* port does not need to be coerced since it doesn't have ps */
248 port
= SCM_COERCE_OUTPORT (port
);
249 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps
,
250 SCM_UNPACK (scm_cons (port
, print_state
)));
254 grow_ref_stack (scm_print_state
*pstate
)
256 SCM old_vect
= pstate
->ref_vect
;
257 size_t old_size
= SCM_SIMPLE_VECTOR_LENGTH (old_vect
);
258 size_t new_size
= 2 * pstate
->ceiling
;
259 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
262 for (i
= 0; i
!= old_size
; ++i
)
263 SCM_SIMPLE_VECTOR_SET (new_vect
, i
, SCM_SIMPLE_VECTOR_REF (old_vect
, i
));
265 pstate
->ref_vect
= new_vect
;
266 pstate
->ceiling
= new_size
;
269 #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
270 #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
273 print_circref (SCM port
, scm_print_state
*pstate
, SCM ref
)
276 long self
= pstate
->top
- 1;
278 if (scm_is_pair (PSTATE_STACK_REF (pstate
, i
)))
282 if (!scm_is_pair (PSTATE_STACK_REF (pstate
, i
-1))
283 || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate
, i
-1)),
284 SCM_CDR (PSTATE_STACK_REF (pstate
, i
))))
290 for (i
= pstate
->top
- 1; 1; --i
)
291 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), ref
))
293 scm_putc ('#', port
);
294 scm_intprint (i
- self
, 10, port
);
295 scm_putc ('#', port
);
298 /* Print the name of a symbol. */
301 quote_keywordish_symbol (SCM symbol
)
305 if (scm_i_symbol_ref (symbol
, 0) != ':'
306 && scm_i_symbol_ref (symbol
, scm_i_symbol_length (symbol
) - 1) != ':')
309 option
= SCM_PRINT_KEYWORD_STYLE
;
310 if (scm_is_false (option
))
312 if (scm_is_eq (option
, sym_reader
))
313 return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE
));
318 scm_i_print_symbol_name (SCM str
, SCM port
)
320 /* This points to the first character that has not yet been written to the
323 /* This points to the character we're currently looking at. */
325 /* If the name contains weird characters, we'll escape them with
326 * backslashes and set this flag; it indicates that we should surround the
327 * name with "#{" and "}#". */
329 /* Backslashes are not sufficient to make a name weird, but if a name is
330 * weird because of other characters, backslahes need to be escaped too.
331 * The first time we see a backslash, we set maybe_weird, and mw_pos points
332 * to the backslash. Then if the name turns out to be weird, we re-process
333 * everything starting from mw_pos.
334 * We could instead make backslashes always weird. This is not necessary
335 * to ensure that the output is (read)-able, but it would make this code
336 * simpler and faster. */
339 size_t len
= scm_i_symbol_length (str
);
340 scm_t_wchar str0
= scm_i_symbol_ref (str
, 0);
342 if (len
== 0 || str0
== '\'' || str0
== '`' || str0
== ','
343 || quote_keywordish_symbol (str
)
344 || (str0
== '.' && len
== 1)
345 || scm_is_true (scm_i_string_to_number (scm_symbol_to_string (str
), 10)))
347 scm_lfwrite ("#{", 2, port
);
351 for (end
= pos
; end
< len
; ++end
)
352 switch (scm_i_symbol_ref (str
, end
))
354 #ifdef BRACKETS_AS_PARENS
363 case SCM_WHITE_SPACES
:
364 case SCM_LINE_INCREMENTORS
:
373 scm_lfwrite ("#{", 2, port
);
377 scm_lfwrite_substr (scm_symbol_to_string (str
), pos
, end
, port
);
381 buf
[1] = (char) (unsigned char) scm_i_symbol_ref (str
, end
);
382 scm_lfwrite (buf
, 2, port
);
399 scm_lfwrite_substr (scm_symbol_to_string (str
), pos
, end
, port
);
401 scm_lfwrite ("}#", 2, port
);
405 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
407 SCM symbol
= scm_from_locale_symboln (str
, len
);
408 scm_i_print_symbol_name (symbol
, port
);
411 /* Print generally. Handles both write and display according to PSTATE.
413 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
414 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
416 static void iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
);
419 /* Print a character as an octal or hex escape. */
420 #define PRINT_CHAR_ESCAPE(i, port) \
423 if (!SCM_R6RS_ESCAPES_P) \
424 scm_intprint (i, 8, port); \
427 scm_puts ("x", port); \
428 scm_intprint (i, 16, port); \
435 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
438 && scm_is_true (scm_memq (exp
, pstate
->highlight_objects
)))
440 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX
, port
);
441 iprin1 (exp
, port
, pstate
);
442 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX
, port
);
445 iprin1 (exp
, port
, pstate
);
449 iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
451 switch (SCM_ITAG3 (exp
))
455 /* These tc3 tags should never occur in an immediate value. They are
456 * only used in cell types of non-immediates, i. e. the value returned
457 * by SCM_CELL_TYPE (exp) can use these tags.
459 scm_ipruk ("immediate", exp
, port
);
463 scm_intprint (SCM_I_INUM (exp
), 10, port
);
468 if (SCM_WRITINGP (pstate
))
469 write_character (SCM_CHAR (exp
), port
, 0);
472 if (!display_character (SCM_CHAR (exp
), port
,
473 scm_i_get_conversion_strategy (port
)))
474 scm_encoding_error (__func__
, errno
,
475 "cannot convert to output locale",
476 "UTF-32", scm_i_get_port_encoding (port
),
477 scm_string (scm_list_1 (exp
)));
480 else if (SCM_IFLAGP (exp
)
481 && ((size_t) SCM_IFLAGNUM (exp
) < (sizeof iflagnames
/ sizeof (char *))))
483 scm_puts (iflagnames
[SCM_IFLAGNUM (exp
)], port
);
487 /* unknown immediate value */
488 scm_ipruk ("immediate", exp
, port
);
492 switch (SCM_TYP7 (exp
))
496 ENTER_NESTED_DATA (pstate
, exp
, circref
);
497 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
499 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
502 pwps
= scm_i_port_with_print_state (port
, pstate
->handle
);
503 pstate
->revealed
= 1;
504 scm_call_generic_2 (print
, exp
, pwps
);
509 scm_print_struct (exp
, port
, pstate
);
511 EXIT_NESTED_DATA (pstate
);
514 case scm_tcs_cons_imcar
:
515 case scm_tcs_cons_nimcar
:
516 ENTER_NESTED_DATA (pstate
, exp
, circref
);
517 scm_iprlist ("(", exp
, ')', port
, pstate
);
518 EXIT_NESTED_DATA (pstate
);
521 print_circref (port
, pstate
, exp
);
524 switch SCM_TYP16 (exp
) {
526 scm_bigprint (exp
, port
, pstate
);
529 scm_print_real (exp
, port
, pstate
);
531 case scm_tc16_complex
:
532 scm_print_complex (exp
, port
, pstate
);
534 case scm_tc16_fraction
:
535 scm_i_print_fraction (exp
, port
, pstate
);
540 if (SCM_WRITINGP (pstate
))
544 scm_putc ('"', port
);
545 len
= scm_i_string_length (exp
);
546 for (i
= 0; i
< len
; ++i
)
547 write_character (scm_i_string_ref (exp
, i
), port
, 1);
549 scm_putc ('"', port
);
550 scm_remember_upto_here_1 (exp
);
553 scm_lfwrite_str (exp
, port
);
554 scm_remember_upto_here_1 (exp
);
557 if (scm_i_symbol_is_interned (exp
))
559 scm_i_print_symbol_name (exp
, port
);
560 scm_remember_upto_here_1 (exp
);
564 scm_puts ("#<uninterned-symbol ", port
);
565 scm_i_print_symbol_name (exp
, port
);
566 scm_putc (' ', port
);
567 scm_uintprint (SCM_UNPACK (exp
), 16, port
);
568 scm_putc ('>', port
);
571 case scm_tc7_variable
:
572 scm_i_variable_print (exp
, port
, pstate
);
574 case scm_tc7_program
:
575 scm_i_program_print (exp
, port
, pstate
);
577 case scm_tc7_pointer
:
578 scm_i_pointer_print (exp
, port
, pstate
);
580 case scm_tc7_hashtable
:
581 scm_i_hashtable_print (exp
, port
, pstate
);
584 scm_i_fluid_print (exp
, port
, pstate
);
586 case scm_tc7_dynamic_state
:
587 scm_i_dynamic_state_print (exp
, port
, pstate
);
590 scm_i_frame_print (exp
, port
, pstate
);
592 case scm_tc7_objcode
:
593 scm_i_objcode_print (exp
, port
, pstate
);
596 scm_i_vm_print (exp
, port
, pstate
);
598 case scm_tc7_vm_cont
:
599 scm_i_vm_cont_print (exp
, port
, pstate
);
602 scm_i_prompt_print (exp
, port
, pstate
);
604 case scm_tc7_with_fluids
:
605 scm_i_with_fluids_print (exp
, port
, pstate
);
608 ENTER_NESTED_DATA (pstate
, exp
, circref
);
609 if (SCM_IS_WHVEC (exp
))
610 scm_puts ("#wh(", port
);
612 scm_puts ("#w(", port
);
613 goto common_vector_printer
;
615 case scm_tc7_bytevector
:
616 scm_i_print_bytevector (exp
, port
, pstate
);
619 ENTER_NESTED_DATA (pstate
, exp
, circref
);
620 scm_puts ("#(", port
);
621 common_vector_printer
:
624 long last
= SCM_SIMPLE_VECTOR_LENGTH (exp
) - 1;
627 && SCM_SIMPLE_VECTOR_LENGTH (exp
) > pstate
->length
)
629 last
= pstate
->length
- 1;
632 if (SCM_I_WVECTP (exp
))
634 /* Elements of weak vectors may not be accessed via the
635 `SIMPLE_VECTOR_REF ()' macro. */
636 for (i
= 0; i
< last
; ++i
)
638 scm_iprin1 (scm_c_vector_ref (exp
, i
),
640 scm_putc (' ', port
);
645 for (i
= 0; i
< last
; ++i
)
647 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
648 scm_putc (' ', port
);
655 scm_iprin1 (scm_c_vector_ref (exp
, i
), port
, pstate
);
658 scm_puts (" ...", port
);
659 scm_putc (')', port
);
661 EXIT_NESTED_DATA (pstate
);
665 register long i
= SCM_PTOBNUM (exp
);
667 && scm_ptobs
[i
].print
668 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
673 ENTER_NESTED_DATA (pstate
, exp
, circref
);
674 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
675 EXIT_NESTED_DATA (pstate
);
678 /* case scm_tcs_closures: */
680 scm_ipruk ("type", exp
, port
);
685 /* Print states are necessary for circular reference safe printing.
686 * They are also expensive to allocate. Therefore print states are
687 * kept in a pool so that they can be reused.
690 /* The PORT argument can also be a print-state/port pair, which will
691 * then be used instead of allocating a new print state. This is
692 * useful for continuing a chain of print calls from Scheme. */
695 scm_prin1 (SCM exp
, SCM port
, int writingp
)
697 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
699 scm_print_state
*pstate
;
702 /* If PORT is a print-state/port pair, use that. Else create a new
705 if (SCM_PORT_WITH_PS_P (port
))
707 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
708 port
= SCM_PORT_WITH_PS_PORT (port
);
712 /* First try to allocate a print state from the pool */
713 scm_i_pthread_mutex_lock (&print_state_mutex
);
714 if (!scm_is_null (print_state_pool
))
716 handle
= print_state_pool
;
717 print_state_pool
= SCM_CDR (print_state_pool
);
719 scm_i_pthread_mutex_unlock (&print_state_mutex
);
720 if (scm_is_false (handle
))
721 handle
= scm_list_1 (make_print_state ());
722 pstate_scm
= SCM_CAR (handle
);
725 pstate
= SCM_PRINT_STATE (pstate_scm
);
726 old_writingp
= pstate
->writingp
;
727 pstate
->writingp
= writingp
;
728 scm_iprin1 (exp
, port
, pstate
);
729 pstate
->writingp
= old_writingp
;
731 /* Return print state to pool if it has been created above and
732 hasn't escaped to Scheme. */
734 if (scm_is_true (handle
) && !pstate
->revealed
)
736 scm_i_pthread_mutex_lock (&print_state_mutex
);
737 SCM_SETCDR (handle
, print_state_pool
);
738 print_state_pool
= handle
;
739 scm_i_pthread_mutex_unlock (&print_state_mutex
);
743 /* Attempt to display CH to PORT according to STRATEGY. Return non-zero
744 if CH was successfully displayed, zero otherwise (e.g., if it was not
745 representable in PORT's encoding.) */
747 display_character (scm_t_wchar ch
, SCM port
,
748 scm_t_string_failed_conversion_handler strategy
)
751 const char *encoding
;
753 encoding
= scm_i_get_port_encoding (port
);
754 if (encoding
== NULL
)
767 char locale_encoded
[8 * sizeof (ch
)], *result
;
769 len
= sizeof (locale_encoded
);
770 result
= u32_conv_to_encoding (encoding
, strategy
,
771 (scm_t_uint32
*) &ch
, 1,
772 NULL
, locale_encoded
, &len
);
775 /* CH is graphic; print it. */
777 if (strategy
== SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE
)
779 /* Apply the same escaping syntax as in `write_character'. */
780 if (SCM_R6RS_ESCAPES_P
)
782 /* LOCALE_ENCODED is large enough to store an R6RS
783 `\xNNNN;' escape sequence. However, libunistring
784 up to 0.9.3 (included) always returns a
785 heap-allocated RESULT. */
786 if (SCM_UNLIKELY (result
!= locale_encoded
))
787 result
= scm_realloc (result
, len
* 7);
789 scm_i_unistring_escapes_to_r6rs_escapes (result
, &len
);
792 scm_i_unistring_escapes_to_guile_escapes (result
, &len
);
795 scm_lfwrite (result
, len
, port
);
798 if (SCM_UNLIKELY (result
!= locale_encoded
))
808 /* Attempt to pretty-print CH, a combining character, to PORT. Return
809 zero upon failure, non-zero otherwise. The idea is to print CH above
810 a dotted circle to make it more visible. */
812 write_combining_character (scm_t_wchar ch
, SCM port
)
815 const char *encoding
;
817 encoding
= scm_i_get_port_encoding (port
);
818 if (encoding
!= NULL
)
821 char locale_encoded
[sizeof (str
)], *result
;
824 str
[0] = SCM_CODEPOINT_DOTTED_CIRCLE
;
827 len
= sizeof (locale_encoded
);
828 result
= u32_conv_to_encoding (encoding
, iconveh_error
,
829 (scm_t_uint32
*) str
, 2,
830 NULL
, locale_encoded
, &len
);
833 scm_lfwrite (result
, len
, port
);
835 if (SCM_UNLIKELY (result
!= locale_encoded
))
839 /* Can't write the result to PORT. */
843 /* PORT is Latin-1-encoded and can't display the fancy things. */
849 /* Write CH to PORT, escaping it if it's non-graphic or not
850 representable in PORT's encoding. If STRING_ESCAPES_P is true and CH
851 needs to be escaped, it is escaped using the in-string escape syntax;
852 otherwise the character escape syntax is used. */
854 write_character (scm_t_wchar ch
, SCM port
, int string_escapes_p
)
858 if (string_escapes_p
)
860 /* Check if CH deserves special treatment. */
861 if (ch
== '"' || ch
== '\\')
863 scm_putc ('\\', port
);
867 else if (ch
== ' ' || ch
== '\n')
875 scm_puts ("#\\", port
);
877 if (uc_combining_class (ch
) != UC_CCC_NR
)
878 /* Character is a combining character, so attempt to
880 printed
= write_combining_character (ch
, port
);
884 && uc_is_general_category_withtable (ch
,
890 /* CH is graphic; attempt to display it. */
891 printed
= display_character (ch
, port
, iconveh_error
);
895 /* CH isn't graphic or cannot be represented in PORT's
898 if (string_escapes_p
)
900 /* Represent CH using the in-string escape syntax. */
902 static const char hex
[] = "0123456789abcdef";
903 static const char escapes
[7] = "abtnvfr";
906 if (ch
>= 0x07 && ch
<= 0x0D && ch
!= 0x0A)
908 /* Use special escapes for some C0 controls. */
910 buf
[1] = escapes
[ch
- 0x07];
911 scm_lfwrite (buf
, 2, port
);
913 else if (!SCM_R6RS_ESCAPES_P
)
919 buf
[2] = hex
[ch
/ 16];
920 buf
[3] = hex
[ch
% 16];
921 scm_lfwrite (buf
, 4, port
);
923 else if (ch
<= 0xFFFF)
927 buf
[2] = hex
[(ch
& 0xF000) >> 12];
928 buf
[3] = hex
[(ch
& 0xF00) >> 8];
929 buf
[4] = hex
[(ch
& 0xF0) >> 4];
930 buf
[5] = hex
[(ch
& 0xF)];
931 scm_lfwrite (buf
, 6, port
);
933 else if (ch
> 0xFFFF)
937 buf
[2] = hex
[(ch
& 0xF00000) >> 20];
938 buf
[3] = hex
[(ch
& 0xF0000) >> 16];
939 buf
[4] = hex
[(ch
& 0xF000) >> 12];
940 buf
[5] = hex
[(ch
& 0xF00) >> 8];
941 buf
[6] = hex
[(ch
& 0xF0) >> 4];
942 buf
[7] = hex
[(ch
& 0xF)];
943 scm_lfwrite (buf
, 8, port
);
948 /* Print an R6RS variable-length hex escape: "\xNNNN;". */
949 scm_t_wchar ch2
= ch
;
959 buf
[i
] = hex
[ch2
& 0xF];
966 scm_lfwrite (buf
+ i
, 9 - i
, port
);
971 /* Represent CH using the character escape syntax. */
974 name
= scm_i_charname (SCM_MAKE_CHAR (ch
));
976 scm_puts (name
, port
);
978 PRINT_CHAR_ESCAPE (ch
, port
);
987 scm_intprint (scm_t_intmax n
, int radix
, SCM port
)
989 char num_buf
[SCM_INTBUFLEN
];
990 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
994 scm_uintprint (scm_t_uintmax n
, int radix
, SCM port
)
996 char num_buf
[SCM_INTBUFLEN
];
997 scm_lfwrite (num_buf
, scm_iuint2str (n
, radix
, num_buf
), port
);
1000 /* Print an object of unrecognized type.
1004 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
1006 scm_puts ("#<unknown-", port
);
1007 scm_puts (hdr
, port
);
1008 if (1) /* (scm_in_heap_p (ptr)) */ /* FIXME */
1010 scm_puts (" (0x", port
);
1011 scm_uintprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
1012 scm_puts (" . 0x", port
);
1013 scm_uintprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
1014 scm_puts (") @", port
);
1016 scm_puts (" 0x", port
);
1017 scm_uintprint (SCM_UNPACK (ptr
), 16, port
);
1018 scm_putc ('>', port
);
1025 scm_iprlist (char *hdr
, SCM exp
, int tlr
, SCM port
, scm_print_state
*pstate
)
1027 register SCM hare
, tortoise
;
1028 long floor
= pstate
->top
- 2;
1029 scm_puts (hdr
, port
);
1032 goto fancy_printing
;
1034 /* Run a hare and tortoise so that total time complexity will be
1035 O(depth * N) instead of O(N^2). */
1036 hare
= SCM_CDR (exp
);
1038 while (scm_is_pair (hare
))
1040 if (scm_is_eq (hare
, tortoise
))
1041 goto fancy_printing
;
1042 hare
= SCM_CDR (hare
);
1043 if (!scm_is_pair (hare
))
1045 hare
= SCM_CDR (hare
);
1046 tortoise
= SCM_CDR (tortoise
);
1049 /* No cdr cycles intrinsic to this list */
1050 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
1051 for (exp
= SCM_CDR (exp
); scm_is_pair (exp
); exp
= SCM_CDR (exp
))
1055 for (i
= floor
; i
>= 0; --i
)
1056 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
1058 PUSH_REF (pstate
, exp
);
1059 scm_putc (' ', port
);
1061 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
1063 if (!SCM_NULL_OR_NIL_P (exp
))
1065 scm_puts (" . ", port
);
1066 scm_iprin1 (exp
, port
, pstate
);
1070 scm_putc (tlr
, port
);
1071 pstate
->top
= floor
+ 2;
1076 long n
= pstate
->length
;
1078 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
1079 exp
= SCM_CDR (exp
); --n
;
1080 for (; scm_is_pair (exp
); exp
= SCM_CDR (exp
))
1082 register unsigned long i
;
1084 for (i
= 0; i
< pstate
->top
; ++i
)
1085 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
1091 scm_puts (" ...", port
);
1097 PUSH_REF(pstate
, exp
);
1098 ++pstate
->list_offset
;
1099 scm_putc (' ', port
);
1101 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
1104 if (!SCM_NULL_OR_NIL_P (exp
))
1106 scm_puts (" . ", port
);
1107 scm_iprin1 (exp
, port
, pstate
);
1110 pstate
->list_offset
-= pstate
->top
- floor
- 2;
1114 pstate
->list_offset
-= pstate
->top
- floor
- 2;
1117 scm_puts (" . ", port
);
1118 print_circref (port
, pstate
, exp
);
1125 scm_valid_oport_value_p (SCM val
)
1127 return (SCM_OPOUTPORTP (val
)
1128 || (SCM_PORT_WITH_PS_P (val
)
1129 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
1132 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
1135 scm_write (SCM obj
, SCM port
)
1137 if (SCM_UNBNDP (port
))
1138 port
= scm_current_output_port ();
1140 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
1142 scm_prin1 (obj
, port
, 1);
1143 return SCM_UNSPECIFIED
;
1147 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
1150 scm_display (SCM obj
, SCM port
)
1152 if (SCM_UNBNDP (port
))
1153 port
= scm_current_output_port ();
1155 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
1157 scm_prin1 (obj
, port
, 0);
1158 return SCM_UNSPECIFIED
;
1162 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
1163 (SCM destination
, SCM message
, SCM args
),
1164 "Write @var{message} to @var{destination}, defaulting to\n"
1165 "the current output port.\n"
1166 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
1167 "@code{~S} (was @code{%S}) escapes. When printed,\n"
1168 "the escapes are replaced with corresponding members of\n"
1170 "@code{~A} formats using @code{display} and @code{~S} formats\n"
1171 "using @code{write}.\n"
1172 "If @var{destination} is @code{#t}, then use the current output\n"
1173 "port, if @var{destination} is @code{#f}, then return a string\n"
1174 "containing the formatted text. Does not add a trailing newline.")
1175 #define FUNC_NAME s_scm_simple_format
1177 SCM port
, answer
= SCM_UNSPECIFIED
;
1178 int fReturnString
= 0;
1180 size_t start
, p
, end
;
1182 if (scm_is_eq (destination
, SCM_BOOL_T
))
1184 destination
= port
= scm_current_output_port ();
1186 else if (scm_is_false (destination
))
1189 port
= scm_mkstrport (SCM_INUM0
,
1190 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
1191 SCM_OPN
| SCM_WRTNG
,
1197 SCM_VALIDATE_OPORT_VALUE (1, destination
);
1198 port
= SCM_COERCE_OUTPORT (destination
);
1200 SCM_VALIDATE_STRING (2, message
);
1201 SCM_VALIDATE_REST_ARGUMENT (args
);
1205 end
= scm_i_string_length (message
);
1206 for (p
= start
; p
!= end
; ++p
)
1207 if (scm_i_string_ref (message
, p
) == '~')
1212 switch (scm_i_string_ref (message
, p
))
1221 scm_lfwrite_substr (message
, start
, p
, port
);
1225 scm_lfwrite_substr (message
, start
, p
- 1, port
);
1230 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
1231 scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message
, p
))));
1236 if (!scm_is_pair (args
))
1237 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1238 scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message
, p
))));
1240 scm_lfwrite_substr (message
, start
, p
- 1, port
);
1241 /* we pass destination here */
1242 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1243 args
= SCM_CDR (args
);
1247 scm_lfwrite_substr (message
, start
, p
, port
);
1248 if (!scm_is_eq (args
, SCM_EOL
))
1249 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1250 scm_list_1 (scm_length (args
)));
1253 answer
= scm_strport_to_string (destination
);
1255 return scm_return_first (answer
, message
);
1260 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1262 "Send a newline to @var{port}.\n"
1263 "If @var{port} is omitted, send to the current output port.")
1264 #define FUNC_NAME s_scm_newline
1266 if (SCM_UNBNDP (port
))
1267 port
= scm_current_output_port ();
1269 SCM_VALIDATE_OPORT_VALUE (1, port
);
1271 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1272 return SCM_UNSPECIFIED
;
1276 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1277 (SCM chr
, SCM port
),
1278 "Send character @var{chr} to @var{port}.")
1279 #define FUNC_NAME s_scm_write_char
1281 if (SCM_UNBNDP (port
))
1282 port
= scm_current_output_port ();
1284 SCM_VALIDATE_CHAR (1, chr
);
1285 SCM_VALIDATE_OPORT_VALUE (2, port
);
1287 port
= SCM_COERCE_OUTPORT (port
);
1288 if (!display_character (SCM_CHAR (chr
), port
,
1289 scm_i_get_conversion_strategy (port
)))
1290 scm_encoding_error (__func__
, errno
,
1291 "cannot convert to output locale",
1292 "UTF-32", scm_i_get_port_encoding (port
),
1293 scm_string (scm_list_1 (chr
)));
1295 return SCM_UNSPECIFIED
;
1301 /* Call back to Scheme code to do the printing of special objects
1302 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1303 * containing PORT and PSTATE. This object can be used as the port for
1304 * display/write etc to continue the current print chain. The REVEALED
1305 * field of PSTATE is set to true to indicate that the print state has
1306 * escaped to Scheme and thus has to be freed by the GC.
1309 scm_t_bits scm_tc16_port_with_ps
;
1311 /* Print exactly as the port itself would */
1314 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1316 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1317 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1321 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1323 pstate
->revealed
= 1;
1324 return scm_call_2 (proc
, exp
,
1325 scm_i_port_with_print_state (port
, pstate
->handle
));
1328 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 1, 1, 0,
1329 (SCM port
, SCM pstate
),
1330 "Create a new port which behaves like @var{port}, but with an\n"
1331 "included print state @var{pstate}. @var{pstate} is optional.\n"
1332 "If @var{pstate} isn't supplied and @var{port} already has\n"
1333 "a print state, the old print state is reused.")
1334 #define FUNC_NAME s_scm_port_with_print_state
1336 SCM_VALIDATE_OPORT_VALUE (1, port
);
1337 if (!SCM_UNBNDP (pstate
))
1338 SCM_VALIDATE_PRINTSTATE (2, pstate
);
1339 return scm_i_port_with_print_state (port
, pstate
);
1343 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1345 "Return the print state of the port @var{port}. If @var{port}\n"
1346 "has no associated print state, @code{#f} is returned.")
1347 #define FUNC_NAME s_scm_get_print_state
1349 if (SCM_PORT_WITH_PS_P (port
))
1350 return SCM_PORT_WITH_PS_PS (port
);
1351 if (SCM_OUTPUT_PORT_P (port
))
1353 SCM_WRONG_TYPE_ARG (1, port
);
1362 SCM vtable
, layout
, type
;
1364 scm_init_opts (scm_print_options
, scm_print_opts
);
1366 scm_print_options (scm_list_4 (scm_from_latin1_symbol ("highlight-prefix"),
1367 scm_from_locale_string ("{"),
1368 scm_from_latin1_symbol ("highlight-suffix"),
1369 scm_from_locale_string ("}")));
1371 scm_gc_register_root (&print_state_pool
);
1372 scm_gc_register_root (&scm_print_state_vtable
);
1373 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1375 scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT
));
1376 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1377 scm_set_struct_vtable_name_x (type
, scm_from_latin1_symbol ("print-state"));
1378 scm_print_state_vtable
= type
;
1380 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1381 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1382 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1384 #include "libguile/print.x"
1386 scm_print_opts
[SCM_PRINT_KEYWORD_STYLE_I
].val
= SCM_UNPACK (sym_reader
);