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
28 #include "libguile/_scm.h"
29 #include "libguile/chars.h"
30 #include "libguile/continuations.h"
31 #include "libguile/smob.h"
32 #include "libguile/eval.h"
33 #include "libguile/macros.h"
34 #include "libguile/procprop.h"
35 #include "libguile/read.h"
36 #include "libguile/weaks.h"
37 #include "libguile/unif.h"
38 #include "libguile/alist.h"
39 #include "libguile/struct.h"
40 #include "libguile/objects.h"
41 #include "libguile/ports.h"
42 #include "libguile/root.h"
43 #include "libguile/strings.h"
44 #include "libguile/strports.h"
45 #include "libguile/vectors.h"
46 #include "libguile/lang.h"
47 #include "libguile/numbers.h"
49 #include "libguile/validate.h"
50 #include "libguile/print.h"
52 #include "libguile/private-options.h"
56 /* {Names of immediate symbols}
58 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
61 /* This table must agree with the list of flags in tags.h. */
62 static const char *iflagnames
[] =
71 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
74 /* Elisp nil value. This is its Scheme name; whenever it's printed in
75 * Elisp, it should appear as the symbol `nil'. */
79 SCM_SYMBOL (sym_reader
, "reader");
81 scm_t_option scm_print_opts
[] = {
82 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK (SCM_BOOL_F
),
83 "Hook for printing closures (should handle macros as well)." },
84 { SCM_OPTION_BOOLEAN
, "source", 0,
85 "Print closures with source." },
86 { SCM_OPTION_SCM
, "highlight-prefix", (unsigned long)SCM_BOOL_F
,
87 "The string to print before highlighted values." },
88 { SCM_OPTION_SCM
, "highlight-suffix", (unsigned long)SCM_BOOL_F
,
89 "The string to print after highlighted values." },
90 { SCM_OPTION_SCM
, "quote-keywordish-symbols", (unsigned long)SCM_BOOL_F
,
91 "How to print symbols that have a colon as their first or last character. "
92 "The value '#f' does not quote the colons; '#t' quotes them; "
93 "'reader' quotes them when the reader option 'keywords' is not '#f'."
99 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
101 "Option interface for the print options. Instead of using\n"
102 "this procedure directly, use the procedures\n"
103 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
104 "and @code{print-options}.")
105 #define FUNC_NAME s_scm_print_options
107 SCM ans
= scm_options (setting
,
115 /* {Printing of Scheme Objects}
118 /* Detection of circular references.
120 * Due to other constraints in the implementation, this code has bad
121 * time complexity (O (depth * N)), The printer code can be
122 * rewritten to be O(N).
124 #define PUSH_REF(pstate, obj) \
127 PSTATE_STACK_SET (pstate, pstate->top, obj); \
129 if (pstate->top == pstate->ceiling) \
130 grow_ref_stack (pstate); \
133 #define ENTER_NESTED_DATA(pstate, obj, label) \
136 register unsigned long i; \
137 for (i = 0; i < pstate->top; ++i) \
138 if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
140 if (pstate->fancyp) \
142 if (pstate->top - pstate->list_offset >= pstate->level) \
144 scm_putc ('#', port); \
148 PUSH_REF(pstate, obj); \
151 #define EXIT_NESTED_DATA(pstate) \
155 PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
159 SCM scm_print_state_vtable
= SCM_BOOL_F
;
160 static SCM print_state_pool
= SCM_EOL
;
161 scm_i_pthread_mutex_t print_state_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
163 #ifdef GUILE_DEBUG /* Used for debugging purposes */
165 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
167 "Return the current-pstate -- the car of the\n"
168 "@code{print_state_pool}. @code{current-pstate} is only\n"
169 "included in @code{--enable-guile-debug} builds.")
170 #define FUNC_NAME s_scm_current_pstate
172 if (!scm_is_null (print_state_pool
))
173 return SCM_CAR (print_state_pool
);
181 #define PSTATE_SIZE 50L
184 make_print_state (void)
187 = scm_make_struct (scm_print_state_vtable
, SCM_INUM0
, SCM_EOL
);
188 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
189 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
190 pstate
->ceiling
= SCM_SIMPLE_VECTOR_LENGTH (pstate
->ref_vect
);
191 pstate
->highlight_objects
= SCM_EOL
;
196 scm_make_print_state ()
198 SCM answer
= SCM_BOOL_F
;
200 /* First try to allocate a print state from the pool */
201 scm_i_pthread_mutex_lock (&print_state_mutex
);
202 if (!scm_is_null (print_state_pool
))
204 answer
= SCM_CAR (print_state_pool
);
205 print_state_pool
= SCM_CDR (print_state_pool
);
207 scm_i_pthread_mutex_unlock (&print_state_mutex
);
209 return scm_is_false (answer
) ? make_print_state () : answer
;
213 scm_free_print_state (SCM print_state
)
216 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
217 /* Cleanup before returning print state to pool.
218 * It is better to do it here. Doing it in scm_prin1
219 * would cost more since that function is called much more
223 pstate
->revealed
= 0;
224 pstate
->highlight_objects
= SCM_EOL
;
225 scm_i_pthread_mutex_lock (&print_state_mutex
);
226 handle
= scm_cons (print_state
, print_state_pool
);
227 print_state_pool
= handle
;
228 scm_i_pthread_mutex_unlock (&print_state_mutex
);
232 scm_i_port_with_print_state (SCM port
, SCM print_state
)
234 if (SCM_UNBNDP (print_state
))
236 if (SCM_PORT_WITH_PS_P (port
))
239 print_state
= scm_make_print_state ();
240 /* port does not need to be coerced since it doesn't have ps */
243 port
= SCM_COERCE_OUTPORT (port
);
244 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps
,
245 SCM_UNPACK (scm_cons (port
, print_state
)));
249 grow_ref_stack (scm_print_state
*pstate
)
251 SCM old_vect
= pstate
->ref_vect
;
252 size_t old_size
= SCM_SIMPLE_VECTOR_LENGTH (old_vect
);
253 size_t new_size
= 2 * pstate
->ceiling
;
254 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
257 for (i
= 0; i
!= old_size
; ++i
)
258 SCM_SIMPLE_VECTOR_SET (new_vect
, i
, SCM_SIMPLE_VECTOR_REF (old_vect
, i
));
260 pstate
->ref_vect
= new_vect
;
261 pstate
->ceiling
= new_size
;
264 #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
265 #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
268 print_circref (SCM port
, scm_print_state
*pstate
, SCM ref
)
271 long self
= pstate
->top
- 1;
273 if (scm_is_pair (PSTATE_STACK_REF (pstate
, i
)))
277 if (!scm_is_pair (PSTATE_STACK_REF (pstate
, i
-1))
278 || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate
, i
-1)),
279 SCM_CDR (PSTATE_STACK_REF (pstate
, i
))))
285 for (i
= pstate
->top
- 1; 1; --i
)
286 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), ref
))
288 scm_putc ('#', port
);
289 scm_intprint (i
- self
, 10, port
);
290 scm_putc ('#', port
);
293 /* Print the name of a symbol. */
296 quote_keywordish_symbol (const char *str
, size_t len
)
300 /* LEN is guaranteed to be > 0.
302 if (str
[0] != ':' && str
[len
-1] != ':')
305 option
= SCM_PRINT_KEYWORD_STYLE
;
306 if (scm_is_false (option
))
308 if (scm_is_eq (option
, sym_reader
))
309 return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE
));
314 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
316 /* This points to the first character that has not yet been written to the
319 /* This points to the character we're currently looking at. */
321 /* If the name contains weird characters, we'll escape them with
322 * backslashes and set this flag; it indicates that we should surround the
323 * name with "#{" and "}#". */
325 /* Backslashes are not sufficient to make a name weird, but if a name is
326 * weird because of other characters, backslahes need to be escaped too.
327 * The first time we see a backslash, we set maybe_weird, and mw_pos points
328 * to the backslash. Then if the name turns out to be weird, we re-process
329 * everything starting from mw_pos.
330 * We could instead make backslashes always weird. This is not necessary
331 * to ensure that the output is (read)-able, but it would make this code
332 * simpler and faster. */
336 if (len
== 0 || str
[0] == '\'' || str
[0] == '`' || str
[0] == ','
337 || quote_keywordish_symbol (str
, len
)
338 || (str
[0] == '.' && len
== 1)
339 || scm_is_true (scm_c_locale_stringn_to_number (str
, len
, 10)))
341 scm_lfwrite ("#{", 2, port
);
345 for (end
= pos
; end
< len
; ++end
)
348 #ifdef BRACKETS_AS_PARENS
357 case SCM_WHITE_SPACES
:
358 case SCM_LINE_INCREMENTORS
:
367 scm_lfwrite ("#{", 2, port
);
371 scm_lfwrite (str
+ pos
, end
- pos
, port
);
376 scm_lfwrite (buf
, 2, port
);
393 scm_lfwrite (str
+ pos
, end
- pos
, port
);
395 scm_lfwrite ("}#", 2, port
);
398 /* Print generally. Handles both write and display according to PSTATE.
400 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
401 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
403 static void iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
);
406 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
409 && scm_is_true (scm_memq (exp
, pstate
->highlight_objects
)))
411 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX
, port
);
412 iprin1 (exp
, port
, pstate
);
413 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX
, port
);
416 iprin1 (exp
, port
, pstate
);
420 iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
422 switch (SCM_ITAG3 (exp
))
424 case scm_tc3_closure
:
427 /* These tc3 tags should never occur in an immediate value. They are
428 * only used in cell types of non-immediates, i. e. the value returned
429 * by SCM_CELL_TYPE (exp) can use these tags.
431 scm_ipruk ("immediate", exp
, port
);
435 scm_intprint (SCM_I_INUM (exp
), 10, port
);
440 scm_t_wchar i
= SCM_CHAR (exp
);
443 if (SCM_WRITINGP (pstate
))
445 scm_puts ("#\\", port
);
446 name
= scm_i_charname (exp
);
448 scm_puts (name
, port
);
449 else if (uc_is_general_category_withtable (i
, UC_CATEGORY_MASK_L
453 | UC_CATEGORY_MASK_S
))
454 /* Print the character if is graphic character. */
457 /* Character is graphic. Print it. */
460 /* Character is graphic but unrepresentable in
461 this port's encoding. */
462 scm_intprint (i
, 8, port
);
465 /* Character is a non-graphical character. */
466 scm_intprint (i
, 8, port
);
471 else if (SCM_IFLAGP (exp
)
472 && ((size_t) SCM_IFLAGNUM (exp
) < (sizeof iflagnames
/ sizeof (char *))))
474 scm_puts (iflagnames
[SCM_IFLAGNUM (exp
)], port
);
476 else if (SCM_ISYMP (exp
))
478 scm_i_print_isym (exp
, port
);
480 else if (SCM_ILOCP (exp
))
482 scm_i_print_iloc (exp
, port
);
486 /* unknown immediate value */
487 scm_ipruk ("immediate", exp
, port
);
491 switch (SCM_TYP7 (exp
))
495 ENTER_NESTED_DATA (pstate
, exp
, circref
);
496 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
498 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
501 pwps
= scm_i_port_with_print_state (port
, pstate
->handle
);
502 pstate
->revealed
= 1;
503 scm_call_generic_2 (print
, exp
, pwps
);
508 scm_print_struct (exp
, port
, pstate
);
510 EXIT_NESTED_DATA (pstate
);
513 case scm_tcs_cons_imcar
:
514 case scm_tcs_cons_nimcar
:
515 ENTER_NESTED_DATA (pstate
, exp
, circref
);
516 scm_iprlist ("(", exp
, ')', port
, pstate
);
517 EXIT_NESTED_DATA (pstate
);
520 print_circref (port
, pstate
, exp
);
522 case scm_tcs_closures
:
523 if (scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE
))
524 || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE
,
527 SCM formals
= SCM_CLOSURE_FORMALS (exp
);
528 scm_puts ("#<procedure", port
);
529 scm_putc (' ', port
);
530 scm_iprin1 (scm_procedure_name (exp
), port
, pstate
);
531 scm_putc (' ', port
);
532 if (SCM_PRINT_SOURCE_P
)
534 SCM env
= SCM_ENV (exp
);
535 SCM xenv
= SCM_EXTEND_ENV (formals
, SCM_EOL
, env
);
536 SCM src
= scm_i_unmemocopy_body (SCM_CODE (exp
), xenv
);
537 ENTER_NESTED_DATA (pstate
, exp
, circref
);
538 scm_iprin1 (src
, port
, pstate
);
539 EXIT_NESTED_DATA (pstate
);
542 scm_iprin1 (formals
, port
, pstate
);
543 scm_putc ('>', port
);
547 switch SCM_TYP16 (exp
) {
549 scm_bigprint (exp
, port
, pstate
);
552 scm_print_real (exp
, port
, pstate
);
554 case scm_tc16_complex
:
555 scm_print_complex (exp
, port
, pstate
);
557 case scm_tc16_fraction
:
558 scm_i_print_fraction (exp
, port
, pstate
);
563 if (SCM_WRITINGP (pstate
))
566 static char const hex
[] = "0123456789abcdef";
570 scm_putc ('"', port
);
571 len
= scm_i_string_length (exp
);
572 for (i
= 0; i
< len
; ++i
)
574 scm_t_wchar ch
= scm_i_string_ref (exp
, i
);
577 if (ch
== ' ' || ch
== '\n')
582 else if (ch
== '"' || ch
== '\\')
584 scm_putc ('\\', port
);
585 scm_charprint (ch
, port
);
589 if (uc_is_general_category_withtable
591 UC_CATEGORY_MASK_L
| UC_CATEGORY_MASK_M
|
592 UC_CATEGORY_MASK_N
| UC_CATEGORY_MASK_P
|
595 /* Print the character since it is a graphic
598 SCM wstr
= scm_i_make_wide_string (1, &wbuf
);
604 buf
= u32_conv_to_encoding ("ISO-8859-1",
606 (scm_t_uint32
*) wbuf
,
607 1, NULL
, NULL
, &len
);
610 /* Character is graphic and representable in
611 this encoding. Print it. */
612 scm_lfwrite_str (wstr
, port
);
620 /* Character is graphic but unrepresentable in
621 this port's encoding or is not graphic. */
626 buf
[2] = hex
[ch
/ 16];
627 buf
[3] = hex
[ch
% 16];
628 scm_lfwrite (buf
, 4, port
);
630 else if (ch
<= 0xFFFF)
634 buf
[2] = hex
[(ch
& 0xF000) >> 12];
635 buf
[3] = hex
[(ch
& 0xF00) >> 8];
636 buf
[4] = hex
[(ch
& 0xF0) >> 4];
637 buf
[5] = hex
[(ch
& 0xF)];
638 scm_lfwrite (buf
, 6, port
);
641 else if (ch
> 0xFFFF)
645 buf
[2] = hex
[(ch
& 0xF00000) >> 20];
646 buf
[3] = hex
[(ch
& 0xF0000) >> 16];
647 buf
[4] = hex
[(ch
& 0xF000) >> 12];
648 buf
[5] = hex
[(ch
& 0xF00) >> 8];
649 buf
[6] = hex
[(ch
& 0xF0) >> 4];
650 buf
[7] = hex
[(ch
& 0xF)];
651 scm_lfwrite (buf
, 8, port
);
656 scm_putc ('"', port
);
657 scm_remember_upto_here_1 (exp
);
660 scm_lfwrite (scm_i_string_chars (exp
), scm_i_string_length (exp
),
662 scm_remember_upto_here_1 (exp
);
665 if (scm_i_symbol_is_interned (exp
))
667 scm_print_symbol_name (scm_i_symbol_chars (exp
),
668 scm_i_symbol_length (exp
), port
);
669 scm_remember_upto_here_1 (exp
);
673 scm_puts ("#<uninterned-symbol ", port
);
674 scm_print_symbol_name (scm_i_symbol_chars (exp
),
675 scm_i_symbol_length (exp
),
677 scm_putc (' ', port
);
678 scm_uintprint (SCM_UNPACK (exp
), 16, port
);
679 scm_putc ('>', port
);
682 case scm_tc7_variable
:
683 scm_i_variable_print (exp
, port
, pstate
);
686 ENTER_NESTED_DATA (pstate
, exp
, circref
);
687 if (SCM_IS_WHVEC (exp
))
688 scm_puts ("#wh(", port
);
690 scm_puts ("#w(", port
);
691 goto common_vector_printer
;
694 ENTER_NESTED_DATA (pstate
, exp
, circref
);
695 scm_puts ("#(", port
);
696 common_vector_printer
:
699 long last
= SCM_SIMPLE_VECTOR_LENGTH (exp
) - 1;
702 && SCM_SIMPLE_VECTOR_LENGTH (exp
) > pstate
->length
)
704 last
= pstate
->length
- 1;
707 for (i
= 0; i
< last
; ++i
)
710 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
711 scm_putc (' ', port
);
716 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
719 scm_puts (" ...", port
);
720 scm_putc (')', port
);
722 EXIT_NESTED_DATA (pstate
);
725 scm_puts (SCM_SUBR_GENERIC (exp
)
726 ? "#<primitive-generic "
727 : "#<primitive-procedure ",
729 scm_puts (scm_i_symbol_chars (SCM_SUBR_NAME (exp
)), port
);
730 scm_putc ('>', port
);
734 scm_puts ("#<procedure-with-setter", port
);
736 SCM name
= scm_procedure_name (exp
);
737 if (scm_is_true (name
))
739 scm_putc (' ', port
);
740 scm_display (name
, port
);
743 scm_putc ('>', port
);
747 register long i
= SCM_PTOBNUM (exp
);
749 && scm_ptobs
[i
].print
750 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
755 ENTER_NESTED_DATA (pstate
, exp
, circref
);
756 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
757 EXIT_NESTED_DATA (pstate
);
761 scm_ipruk ("type", exp
, port
);
766 /* Print states are necessary for circular reference safe printing.
767 * They are also expensive to allocate. Therefore print states are
768 * kept in a pool so that they can be reused.
771 /* The PORT argument can also be a print-state/port pair, which will
772 * then be used instead of allocating a new print state. This is
773 * useful for continuing a chain of print calls from Scheme. */
776 scm_prin1 (SCM exp
, SCM port
, int writingp
)
778 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
780 scm_print_state
*pstate
;
783 /* If PORT is a print-state/port pair, use that. Else create a new
786 if (SCM_PORT_WITH_PS_P (port
))
788 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
789 port
= SCM_PORT_WITH_PS_PORT (port
);
793 /* First try to allocate a print state from the pool */
794 scm_i_pthread_mutex_lock (&print_state_mutex
);
795 if (!scm_is_null (print_state_pool
))
797 handle
= print_state_pool
;
798 print_state_pool
= SCM_CDR (print_state_pool
);
800 scm_i_pthread_mutex_unlock (&print_state_mutex
);
801 if (scm_is_false (handle
))
802 handle
= scm_list_1 (make_print_state ());
803 pstate_scm
= SCM_CAR (handle
);
806 pstate
= SCM_PRINT_STATE (pstate_scm
);
807 old_writingp
= pstate
->writingp
;
808 pstate
->writingp
= writingp
;
809 scm_iprin1 (exp
, port
, pstate
);
810 pstate
->writingp
= old_writingp
;
812 /* Return print state to pool if it has been created above and
813 hasn't escaped to Scheme. */
815 if (scm_is_true (handle
) && !pstate
->revealed
)
817 scm_i_pthread_mutex_lock (&print_state_mutex
);
818 SCM_SETCDR (handle
, print_state_pool
);
819 print_state_pool
= handle
;
820 scm_i_pthread_mutex_unlock (&print_state_mutex
);
824 /* Print a character.
827 scm_charprint (scm_t_uint32 ch
, SCM port
)
830 SCM wstr
= scm_i_make_wide_string (1, &wbuf
);
833 scm_lfwrite_str (wstr
, port
);
840 scm_intprint (scm_t_intmax n
, int radix
, SCM port
)
842 char num_buf
[SCM_INTBUFLEN
];
843 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
847 scm_uintprint (scm_t_uintmax n
, int radix
, SCM port
)
849 char num_buf
[SCM_INTBUFLEN
];
850 scm_lfwrite (num_buf
, scm_iuint2str (n
, radix
, num_buf
), port
);
853 /* Print an object of unrecognized type.
857 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
859 scm_puts ("#<unknown-", port
);
860 scm_puts (hdr
, port
);
861 if (scm_in_heap_p (ptr
))
863 scm_puts (" (0x", port
);
864 scm_uintprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
865 scm_puts (" . 0x", port
);
866 scm_uintprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
867 scm_puts (") @", port
);
869 scm_puts (" 0x", port
);
870 scm_uintprint (SCM_UNPACK (ptr
), 16, port
);
871 scm_putc ('>', port
);
878 scm_iprlist (char *hdr
, SCM exp
, int tlr
, SCM port
, scm_print_state
*pstate
)
880 register SCM hare
, tortoise
;
881 long floor
= pstate
->top
- 2;
882 scm_puts (hdr
, port
);
887 /* Run a hare and tortoise so that total time complexity will be
888 O(depth * N) instead of O(N^2). */
889 hare
= SCM_CDR (exp
);
891 while (scm_is_pair (hare
))
893 if (scm_is_eq (hare
, tortoise
))
895 hare
= SCM_CDR (hare
);
896 if (!scm_is_pair (hare
))
898 hare
= SCM_CDR (hare
);
899 tortoise
= SCM_CDR (tortoise
);
902 /* No cdr cycles intrinsic to this list */
903 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
904 for (exp
= SCM_CDR (exp
); scm_is_pair (exp
); exp
= SCM_CDR (exp
))
908 for (i
= floor
; i
>= 0; --i
)
909 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
911 PUSH_REF (pstate
, exp
);
912 scm_putc (' ', port
);
914 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
916 if (!SCM_NULL_OR_NIL_P (exp
))
918 scm_puts (" . ", port
);
919 scm_iprin1 (exp
, port
, pstate
);
923 scm_putc (tlr
, port
);
924 pstate
->top
= floor
+ 2;
929 long n
= pstate
->length
;
931 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
932 exp
= SCM_CDR (exp
); --n
;
933 for (; scm_is_pair (exp
); exp
= SCM_CDR (exp
))
935 register unsigned long i
;
937 for (i
= 0; i
< pstate
->top
; ++i
)
938 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
944 scm_puts (" ...", port
);
950 PUSH_REF(pstate
, exp
);
951 ++pstate
->list_offset
;
952 scm_putc (' ', port
);
954 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
957 if (!SCM_NULL_OR_NIL_P (exp
))
959 scm_puts (" . ", port
);
960 scm_iprin1 (exp
, port
, pstate
);
963 pstate
->list_offset
-= pstate
->top
- floor
- 2;
967 pstate
->list_offset
-= pstate
->top
- floor
- 2;
970 scm_puts (" . ", port
);
971 print_circref (port
, pstate
, exp
);
978 scm_valid_oport_value_p (SCM val
)
980 return (SCM_OPOUTPORTP (val
)
981 || (SCM_PORT_WITH_PS_P (val
)
982 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
985 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
988 scm_write (SCM obj
, SCM port
)
990 if (SCM_UNBNDP (port
))
991 port
= scm_current_output_port ();
993 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
995 scm_prin1 (obj
, port
, 1);
1000 scm_close_port (port
);
1004 return SCM_UNSPECIFIED
;
1008 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
1011 scm_display (SCM obj
, SCM port
)
1013 if (SCM_UNBNDP (port
))
1014 port
= scm_current_output_port ();
1016 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
1018 scm_prin1 (obj
, port
, 0);
1023 scm_close_port (port
);
1027 return SCM_UNSPECIFIED
;
1031 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
1032 (SCM destination
, SCM message
, SCM args
),
1033 "Write @var{message} to @var{destination}, defaulting to\n"
1034 "the current output port.\n"
1035 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
1036 "@code{~S} (was @code{%S}) escapes. When printed,\n"
1037 "the escapes are replaced with corresponding members of\n"
1039 "@code{~A} formats using @code{display} and @code{~S} formats\n"
1040 "using @code{write}.\n"
1041 "If @var{destination} is @code{#t}, then use the current output\n"
1042 "port, if @var{destination} is @code{#f}, then return a string\n"
1043 "containing the formatted text. Does not add a trailing newline.")
1044 #define FUNC_NAME s_scm_simple_format
1046 SCM port
, answer
= SCM_UNSPECIFIED
;
1047 int fReturnString
= 0;
1053 if (scm_is_eq (destination
, SCM_BOOL_T
))
1055 destination
= port
= scm_current_output_port ();
1057 else if (scm_is_false (destination
))
1060 port
= scm_mkstrport (SCM_INUM0
,
1061 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
1062 SCM_OPN
| SCM_WRTNG
,
1068 SCM_VALIDATE_OPORT_VALUE (1, destination
);
1069 port
= SCM_COERCE_OUTPORT (destination
);
1071 SCM_VALIDATE_STRING (2, message
);
1072 SCM_VALIDATE_REST_ARGUMENT (args
);
1074 start
= scm_i_string_chars (message
);
1075 end
= start
+ scm_i_string_length (message
);
1076 for (p
= start
; p
!= end
; ++p
)
1091 scm_lfwrite (start
, p
- start
, port
);
1095 scm_lfwrite (start
, p
- start
- 1, port
);
1100 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
1101 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1106 if (!scm_is_pair (args
))
1107 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1108 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1110 scm_lfwrite (start
, p
- start
- 1, port
);
1111 /* we pass destination here */
1112 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1113 args
= SCM_CDR (args
);
1117 scm_lfwrite (start
, p
- start
, port
);
1118 if (!scm_is_eq (args
, SCM_EOL
))
1119 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1120 scm_list_1 (scm_length (args
)));
1123 answer
= scm_strport_to_string (destination
);
1125 return scm_return_first (answer
, message
);
1130 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1132 "Send a newline to @var{port}.\n"
1133 "If @var{port} is omitted, send to the current output port.")
1134 #define FUNC_NAME s_scm_newline
1136 if (SCM_UNBNDP (port
))
1137 port
= scm_current_output_port ();
1139 SCM_VALIDATE_OPORT_VALUE (1, port
);
1141 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1142 return SCM_UNSPECIFIED
;
1146 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1147 (SCM chr
, SCM port
),
1148 "Send character @var{chr} to @var{port}.")
1149 #define FUNC_NAME s_scm_write_char
1151 if (SCM_UNBNDP (port
))
1152 port
= scm_current_output_port ();
1154 SCM_VALIDATE_CHAR (1, chr
);
1155 SCM_VALIDATE_OPORT_VALUE (2, port
);
1157 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1162 scm_close_port (port
);
1166 return SCM_UNSPECIFIED
;
1172 /* Call back to Scheme code to do the printing of special objects
1173 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1174 * containing PORT and PSTATE. This object can be used as the port for
1175 * display/write etc to continue the current print chain. The REVEALED
1176 * field of PSTATE is set to true to indicate that the print state has
1177 * escaped to Scheme and thus has to be freed by the GC.
1180 scm_t_bits scm_tc16_port_with_ps
;
1182 /* Print exactly as the port itself would */
1185 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1187 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1188 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1192 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1194 pstate
->revealed
= 1;
1195 return scm_call_2 (proc
, exp
,
1196 scm_i_port_with_print_state (port
, pstate
->handle
));
1199 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 1, 1, 0,
1200 (SCM port
, SCM pstate
),
1201 "Create a new port which behaves like @var{port}, but with an\n"
1202 "included print state @var{pstate}. @var{pstate} is optional.\n"
1203 "If @var{pstate} isn't supplied and @var{port} already has\n"
1204 "a print state, the old print state is reused.")
1205 #define FUNC_NAME s_scm_port_with_print_state
1207 SCM_VALIDATE_OPORT_VALUE (1, port
);
1208 if (!SCM_UNBNDP (pstate
))
1209 SCM_VALIDATE_PRINTSTATE (2, pstate
);
1210 return scm_i_port_with_print_state (port
, pstate
);
1214 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1216 "Return the print state of the port @var{port}. If @var{port}\n"
1217 "has no associated print state, @code{#f} is returned.")
1218 #define FUNC_NAME s_scm_get_print_state
1220 if (SCM_PORT_WITH_PS_P (port
))
1221 return SCM_PORT_WITH_PS_PS (port
);
1222 if (SCM_OUTPUT_PORT_P (port
))
1224 SCM_WRONG_TYPE_ARG (1, port
);
1233 SCM vtable
, layout
, type
;
1235 scm_init_opts (scm_print_options
, scm_print_opts
);
1237 scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"),
1238 scm_from_locale_string ("{"),
1239 scm_from_locale_symbol ("highlight-suffix"),
1240 scm_from_locale_string ("}")));
1242 scm_gc_register_root (&print_state_pool
);
1243 scm_gc_register_root (&scm_print_state_vtable
);
1244 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1246 scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT
));
1247 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1248 scm_set_struct_vtable_name_x (type
, scm_from_locale_symbol ("print-state"));
1249 scm_print_state_vtable
= type
;
1251 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1252 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1253 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1254 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1256 #include "libguile/print.x"
1258 scm_print_opts
[SCM_PRINT_KEYWORD_STYLE_I
].val
= SCM_UNPACK (sym_reader
);