1 /* Copyright (C) 1995-1999,2000,2001, 2002, 2003, 2004, 2006, 2008 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
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library 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 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 02110-1301 USA
26 #include "libguile/_scm.h"
27 #include "libguile/chars.h"
28 #include "libguile/continuations.h"
29 #include "libguile/smob.h"
30 #include "libguile/eval.h"
31 #include "libguile/macros.h"
32 #include "libguile/procprop.h"
33 #include "libguile/read.h"
34 #include "libguile/weaks.h"
35 #include "libguile/unif.h"
36 #include "libguile/alist.h"
37 #include "libguile/struct.h"
38 #include "libguile/objects.h"
39 #include "libguile/ports.h"
40 #include "libguile/root.h"
41 #include "libguile/strings.h"
42 #include "libguile/strports.h"
43 #include "libguile/vectors.h"
44 #include "libguile/lang.h"
45 #include "libguile/numbers.h"
47 #include "libguile/validate.h"
48 #include "libguile/print.h"
50 #include "libguile/private-options.h"
54 /* {Names of immediate symbols}
56 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
59 /* This table must agree with the list of flags in tags.h. */
60 static const char *iflagnames
[] =
69 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
72 /* Elisp nil value. This is its Scheme name; whenever it's printed in
73 * Elisp, it should appear as the symbol `nil'. */
77 SCM_SYMBOL (sym_reader
, "reader");
79 scm_t_option scm_print_opts
[] = {
80 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK (SCM_BOOL_F
),
81 "Hook for printing closures (should handle macros as well)." },
82 { SCM_OPTION_BOOLEAN
, "source", 0,
83 "Print closures with source." },
84 { SCM_OPTION_SCM
, "highlight-prefix", (unsigned long)SCM_BOOL_F
,
85 "The string to print before highlighted values." },
86 { SCM_OPTION_SCM
, "highlight-suffix", (unsigned long)SCM_BOOL_F
,
87 "The string to print after highlighted values." },
88 { SCM_OPTION_SCM
, "quote-keywordish-symbols", (unsigned long)SCM_BOOL_F
,
89 "How to print symbols that have a colon as their first or last character. "
90 "The value '#f' does not quote the colons; '#t' quotes them; "
91 "'reader' quotes them when the reader option 'keywords' is not '#f'."
97 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
99 "Option interface for the print options. Instead of using\n"
100 "this procedure directly, use the procedures\n"
101 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
102 "and @code{print-options}.")
103 #define FUNC_NAME s_scm_print_options
105 SCM ans
= scm_options (setting
,
113 /* {Printing of Scheme Objects}
116 /* Detection of circular references.
118 * Due to other constraints in the implementation, this code has bad
119 * time complexity (O (depth * N)), The printer code can be
120 * rewritten to be O(N).
122 #define PUSH_REF(pstate, obj) \
125 PSTATE_STACK_SET (pstate, pstate->top, obj); \
127 if (pstate->top == pstate->ceiling) \
128 grow_ref_stack (pstate); \
131 #define ENTER_NESTED_DATA(pstate, obj, label) \
134 register unsigned long i; \
135 for (i = 0; i < pstate->top; ++i) \
136 if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
138 if (pstate->fancyp) \
140 if (pstate->top - pstate->list_offset >= pstate->level) \
142 scm_putc ('#', port); \
146 PUSH_REF(pstate, obj); \
149 #define EXIT_NESTED_DATA(pstate) \
153 PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
157 SCM scm_print_state_vtable
= SCM_BOOL_F
;
158 static SCM print_state_pool
= SCM_EOL
;
159 scm_i_pthread_mutex_t print_state_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
161 #ifdef GUILE_DEBUG /* Used for debugging purposes */
163 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
165 "Return the current-pstate -- the car of the\n"
166 "@code{print_state_pool}. @code{current-pstate} is only\n"
167 "included in @code{--enable-guile-debug} builds.")
168 #define FUNC_NAME s_scm_current_pstate
170 if (!scm_is_null (print_state_pool
))
171 return SCM_CAR (print_state_pool
);
179 #define PSTATE_SIZE 50L
182 make_print_state (void)
185 = scm_make_struct (scm_print_state_vtable
, SCM_INUM0
, SCM_EOL
);
186 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
187 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
188 pstate
->ceiling
= SCM_SIMPLE_VECTOR_LENGTH (pstate
->ref_vect
);
189 pstate
->highlight_objects
= SCM_EOL
;
194 scm_make_print_state ()
196 SCM answer
= SCM_BOOL_F
;
198 /* First try to allocate a print state from the pool */
199 scm_i_pthread_mutex_lock (&print_state_mutex
);
200 if (!scm_is_null (print_state_pool
))
202 answer
= SCM_CAR (print_state_pool
);
203 print_state_pool
= SCM_CDR (print_state_pool
);
205 scm_i_pthread_mutex_unlock (&print_state_mutex
);
207 return scm_is_false (answer
) ? make_print_state () : answer
;
211 scm_free_print_state (SCM print_state
)
214 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
215 /* Cleanup before returning print state to pool.
216 * It is better to do it here. Doing it in scm_prin1
217 * would cost more since that function is called much more
221 pstate
->revealed
= 0;
222 pstate
->highlight_objects
= SCM_EOL
;
223 scm_i_pthread_mutex_lock (&print_state_mutex
);
224 handle
= scm_cons (print_state
, print_state_pool
);
225 print_state_pool
= handle
;
226 scm_i_pthread_mutex_unlock (&print_state_mutex
);
230 scm_i_port_with_print_state (SCM port
, SCM print_state
)
232 if (SCM_UNBNDP (print_state
))
234 if (SCM_PORT_WITH_PS_P (port
))
237 print_state
= scm_make_print_state ();
238 /* port does not need to be coerced since it doesn't have ps */
241 port
= SCM_COERCE_OUTPORT (port
);
242 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps
,
243 SCM_UNPACK (scm_cons (port
, print_state
)));
247 grow_ref_stack (scm_print_state
*pstate
)
249 SCM old_vect
= pstate
->ref_vect
;
250 size_t old_size
= SCM_SIMPLE_VECTOR_LENGTH (old_vect
);
251 size_t new_size
= 2 * pstate
->ceiling
;
252 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
255 for (i
= 0; i
!= old_size
; ++i
)
256 SCM_SIMPLE_VECTOR_SET (new_vect
, i
, SCM_SIMPLE_VECTOR_REF (old_vect
, i
));
258 pstate
->ref_vect
= new_vect
;
259 pstate
->ceiling
= new_size
;
262 #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
263 #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
266 print_circref (SCM port
, scm_print_state
*pstate
, SCM ref
)
269 long self
= pstate
->top
- 1;
271 if (scm_is_pair (PSTATE_STACK_REF (pstate
, i
)))
275 if (!scm_is_pair (PSTATE_STACK_REF (pstate
, i
-1))
276 || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate
, i
-1)),
277 SCM_CDR (PSTATE_STACK_REF (pstate
, i
))))
283 for (i
= pstate
->top
- 1; 1; --i
)
284 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), ref
))
286 scm_putc ('#', port
);
287 scm_intprint (i
- self
, 10, port
);
288 scm_putc ('#', port
);
291 /* Print the name of a symbol. */
294 quote_keywordish_symbol (const char *str
, size_t len
)
298 /* LEN is guaranteed to be > 0.
300 if (str
[0] != ':' && str
[len
-1] != ':')
303 option
= SCM_PRINT_KEYWORD_STYLE
;
304 if (scm_is_false (option
))
306 if (scm_is_eq (option
, sym_reader
))
307 return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE
));
312 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
314 /* This points to the first character that has not yet been written to the
317 /* This points to the character we're currently looking at. */
319 /* If the name contains weird characters, we'll escape them with
320 * backslashes and set this flag; it indicates that we should surround the
321 * name with "#{" and "}#". */
323 /* Backslashes are not sufficient to make a name weird, but if a name is
324 * weird because of other characters, backslahes need to be escaped too.
325 * The first time we see a backslash, we set maybe_weird, and mw_pos points
326 * to the backslash. Then if the name turns out to be weird, we re-process
327 * everything starting from mw_pos.
328 * We could instead make backslashes always weird. This is not necessary
329 * to ensure that the output is (read)-able, but it would make this code
330 * simpler and faster. */
334 if (len
== 0 || str
[0] == '\'' || str
[0] == '`' || str
[0] == ','
335 || quote_keywordish_symbol (str
, len
)
336 || (str
[0] == '.' && len
== 1)
337 || scm_is_true (scm_c_locale_stringn_to_number (str
, len
, 10)))
339 scm_lfwrite ("#{", 2, port
);
343 for (end
= pos
; end
< len
; ++end
)
346 #ifdef BRACKETS_AS_PARENS
355 case SCM_WHITE_SPACES
:
356 case SCM_LINE_INCREMENTORS
:
365 scm_lfwrite ("#{", 2, port
);
369 scm_lfwrite (str
+ pos
, end
- pos
, port
);
374 scm_lfwrite (buf
, 2, port
);
391 scm_lfwrite (str
+ pos
, end
- pos
, port
);
393 scm_lfwrite ("}#", 2, port
);
396 /* Print generally. Handles both write and display according to PSTATE.
398 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
399 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
401 static void iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
);
404 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
407 && scm_is_true (scm_memq (exp
, pstate
->highlight_objects
)))
409 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX
, port
);
410 iprin1 (exp
, port
, pstate
);
411 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX
, port
);
414 iprin1 (exp
, port
, pstate
);
418 iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
420 switch (SCM_ITAG3 (exp
))
422 case scm_tc3_closure
:
425 /* These tc3 tags should never occur in an immediate value. They are
426 * only used in cell types of non-immediates, i. e. the value returned
427 * by SCM_CELL_TYPE (exp) can use these tags.
429 scm_ipruk ("immediate", exp
, port
);
433 scm_intprint (SCM_I_INUM (exp
), 10, port
);
438 long i
= SCM_CHAR (exp
);
440 if (SCM_WRITINGP (pstate
))
442 scm_puts ("#\\", port
);
443 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
444 scm_puts (scm_charnames
[i
], port
);
446 else if (i
== '\177')
447 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
449 else if (i
< 0 || i
> '\177')
450 scm_intprint (i
, 8, port
);
457 else if (SCM_IFLAGP (exp
)
458 && ((size_t) SCM_IFLAGNUM (exp
) < (sizeof iflagnames
/ sizeof (char *))))
460 scm_puts (iflagnames
[SCM_IFLAGNUM (exp
)], port
);
462 else if (SCM_ISYMP (exp
))
464 scm_i_print_isym (exp
, port
);
466 else if (SCM_ILOCP (exp
))
468 scm_i_print_iloc (exp
, port
);
472 /* unknown immediate value */
473 scm_ipruk ("immediate", exp
, port
);
477 switch (SCM_TYP7 (exp
))
481 ENTER_NESTED_DATA (pstate
, exp
, circref
);
482 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
484 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
487 pwps
= scm_i_port_with_print_state (port
, pstate
->handle
);
488 pstate
->revealed
= 1;
489 scm_call_generic_2 (print
, exp
, pwps
);
494 scm_print_struct (exp
, port
, pstate
);
496 EXIT_NESTED_DATA (pstate
);
499 case scm_tcs_cons_imcar
:
500 case scm_tcs_cons_nimcar
:
501 ENTER_NESTED_DATA (pstate
, exp
, circref
);
502 scm_iprlist ("(", exp
, ')', port
, pstate
);
503 EXIT_NESTED_DATA (pstate
);
506 print_circref (port
, pstate
, exp
);
508 case scm_tcs_closures
:
509 if (scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE
))
510 || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE
,
513 SCM formals
= SCM_CLOSURE_FORMALS (exp
);
514 scm_puts ("#<procedure", port
);
515 scm_putc (' ', port
);
516 scm_iprin1 (scm_procedure_name (exp
), port
, pstate
);
517 scm_putc (' ', port
);
518 if (SCM_PRINT_SOURCE_P
)
520 SCM env
= SCM_ENV (exp
);
521 SCM xenv
= SCM_EXTEND_ENV (formals
, SCM_EOL
, env
);
522 SCM src
= scm_i_unmemocopy_body (SCM_CODE (exp
), xenv
);
523 ENTER_NESTED_DATA (pstate
, exp
, circref
);
524 scm_iprin1 (src
, port
, pstate
);
525 EXIT_NESTED_DATA (pstate
);
528 scm_iprin1 (formals
, port
, pstate
);
529 scm_putc ('>', port
);
533 switch SCM_TYP16 (exp
) {
535 scm_bigprint (exp
, port
, pstate
);
538 scm_print_real (exp
, port
, pstate
);
540 case scm_tc16_complex
:
541 scm_print_complex (exp
, port
, pstate
);
543 case scm_tc16_fraction
:
544 scm_i_print_fraction (exp
, port
, pstate
);
549 if (SCM_WRITINGP (pstate
))
554 scm_putc ('"', port
);
555 len
= scm_i_string_length (exp
);
556 data
= scm_i_string_chars (exp
);
557 for (i
= 0, j
= 0; i
< len
; ++i
)
559 unsigned char ch
= data
[i
];
560 if ((ch
< 32 && ch
!= '\n') || (127 <= ch
&& ch
< 148))
562 static char const hex
[]="0123456789abcdef";
565 scm_lfwrite (data
+j
, i
-j
, port
);
568 buf
[2] = hex
[ch
/ 16];
569 buf
[3] = hex
[ch
% 16];
570 scm_lfwrite (buf
, 4, port
);
571 data
= scm_i_string_chars (exp
);
574 else if (ch
== '"' || ch
== '\\')
576 scm_lfwrite (data
+j
, i
-j
, port
);
577 scm_putc ('\\', port
);
578 data
= scm_i_string_chars (exp
);
582 scm_lfwrite (data
+j
, i
-j
, port
);
583 scm_putc ('"', port
);
584 scm_remember_upto_here_1 (exp
);
587 scm_lfwrite (scm_i_string_chars (exp
), scm_i_string_length (exp
),
589 scm_remember_upto_here_1 (exp
);
592 if (scm_i_symbol_is_interned (exp
))
594 scm_print_symbol_name (scm_i_symbol_chars (exp
),
595 scm_i_symbol_length (exp
),
597 scm_remember_upto_here_1 (exp
);
601 scm_puts ("#<uninterned-symbol ", port
);
602 scm_print_symbol_name (scm_i_symbol_chars (exp
),
603 scm_i_symbol_length (exp
),
605 scm_putc (' ', port
);
606 scm_uintprint (SCM_UNPACK (exp
), 16, port
);
607 scm_putc ('>', port
);
610 case scm_tc7_variable
:
611 scm_i_variable_print (exp
, port
, pstate
);
614 ENTER_NESTED_DATA (pstate
, exp
, circref
);
615 if (SCM_IS_WHVEC (exp
))
616 scm_puts ("#wh(", port
);
618 scm_puts ("#w(", port
);
619 goto common_vector_printer
;
622 ENTER_NESTED_DATA (pstate
, exp
, circref
);
623 scm_puts ("#(", port
);
624 common_vector_printer
:
627 long last
= SCM_SIMPLE_VECTOR_LENGTH (exp
) - 1;
630 && SCM_SIMPLE_VECTOR_LENGTH (exp
) > pstate
->length
)
632 last
= pstate
->length
- 1;
635 for (i
= 0; i
< last
; ++i
)
638 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
639 scm_putc (' ', port
);
644 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
647 scm_puts (" ...", port
);
648 scm_putc (')', port
);
650 EXIT_NESTED_DATA (pstate
);
653 scm_puts (SCM_SUBR_GENERIC (exp
)
654 ? "#<primitive-generic "
655 : "#<primitive-procedure ",
657 scm_puts (scm_i_symbol_chars (SCM_SNAME (exp
)), port
);
658 scm_putc ('>', port
);
663 SCM proc
= SCM_CCLO_SUBR (exp
);
664 if (scm_is_eq (proc
, scm_f_gsubr_apply
))
666 /* Print gsubrs as primitives */
667 SCM name
= scm_procedure_name (exp
);
668 scm_puts ("#<primitive-procedure", port
);
669 if (scm_is_true (name
))
671 scm_putc (' ', port
);
672 scm_puts (scm_i_symbol_chars (name
), port
);
677 scm_puts ("#<compiled-closure ", port
);
678 scm_iprin1 (proc
, port
, pstate
);
680 scm_putc ('>', port
);
685 scm_puts ("#<procedure-with-setter", port
);
687 SCM name
= scm_procedure_name (exp
);
688 if (scm_is_true (name
))
690 scm_putc (' ', port
);
691 scm_display (name
, port
);
694 scm_putc ('>', port
);
698 register long i
= SCM_PTOBNUM (exp
);
700 && scm_ptobs
[i
].print
701 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
706 ENTER_NESTED_DATA (pstate
, exp
, circref
);
707 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
708 EXIT_NESTED_DATA (pstate
);
712 scm_ipruk ("type", exp
, port
);
717 /* Print states are necessary for circular reference safe printing.
718 * They are also expensive to allocate. Therefore print states are
719 * kept in a pool so that they can be reused.
722 /* The PORT argument can also be a print-state/port pair, which will
723 * then be used instead of allocating a new print state. This is
724 * useful for continuing a chain of print calls from Scheme. */
727 scm_prin1 (SCM exp
, SCM port
, int writingp
)
729 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
731 scm_print_state
*pstate
;
734 /* If PORT is a print-state/port pair, use that. Else create a new
737 if (SCM_PORT_WITH_PS_P (port
))
739 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
740 port
= SCM_PORT_WITH_PS_PORT (port
);
744 /* First try to allocate a print state from the pool */
745 scm_i_pthread_mutex_lock (&print_state_mutex
);
746 if (!scm_is_null (print_state_pool
))
748 handle
= print_state_pool
;
749 print_state_pool
= SCM_CDR (print_state_pool
);
751 scm_i_pthread_mutex_unlock (&print_state_mutex
);
752 if (scm_is_false (handle
))
753 handle
= scm_list_1 (make_print_state ());
754 pstate_scm
= SCM_CAR (handle
);
757 pstate
= SCM_PRINT_STATE (pstate_scm
);
758 old_writingp
= pstate
->writingp
;
759 pstate
->writingp
= writingp
;
760 scm_iprin1 (exp
, port
, pstate
);
761 pstate
->writingp
= old_writingp
;
763 /* Return print state to pool if it has been created above and
764 hasn't escaped to Scheme. */
766 if (scm_is_true (handle
) && !pstate
->revealed
)
768 scm_i_pthread_mutex_lock (&print_state_mutex
);
769 SCM_SETCDR (handle
, print_state_pool
);
770 print_state_pool
= handle
;
771 scm_i_pthread_mutex_unlock (&print_state_mutex
);
780 scm_intprint (scm_t_intmax n
, int radix
, SCM port
)
782 char num_buf
[SCM_INTBUFLEN
];
783 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
787 scm_uintprint (scm_t_uintmax n
, int radix
, SCM port
)
789 char num_buf
[SCM_INTBUFLEN
];
790 scm_lfwrite (num_buf
, scm_iuint2str (n
, radix
, num_buf
), port
);
793 /* Print an object of unrecognized type.
797 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
799 scm_puts ("#<unknown-", port
);
800 scm_puts (hdr
, port
);
801 if (scm_in_heap_p (ptr
))
803 scm_puts (" (0x", port
);
804 scm_uintprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
805 scm_puts (" . 0x", port
);
806 scm_uintprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
807 scm_puts (") @", port
);
809 scm_puts (" 0x", port
);
810 scm_uintprint (SCM_UNPACK (ptr
), 16, port
);
811 scm_putc ('>', port
);
818 scm_iprlist (char *hdr
, SCM exp
, int tlr
, SCM port
, scm_print_state
*pstate
)
820 register SCM hare
, tortoise
;
821 long floor
= pstate
->top
- 2;
822 scm_puts (hdr
, port
);
827 /* Run a hare and tortoise so that total time complexity will be
828 O(depth * N) instead of O(N^2). */
829 hare
= SCM_CDR (exp
);
831 while (scm_is_pair (hare
))
833 if (scm_is_eq (hare
, tortoise
))
835 hare
= SCM_CDR (hare
);
836 if (!scm_is_pair (hare
))
838 hare
= SCM_CDR (hare
);
839 tortoise
= SCM_CDR (tortoise
);
842 /* No cdr cycles intrinsic to this list */
843 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
844 for (exp
= SCM_CDR (exp
); scm_is_pair (exp
); exp
= SCM_CDR (exp
))
848 for (i
= floor
; i
>= 0; --i
)
849 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
851 PUSH_REF (pstate
, exp
);
852 scm_putc (' ', port
);
854 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
856 if (!SCM_NULL_OR_NIL_P (exp
))
858 scm_puts (" . ", port
);
859 scm_iprin1 (exp
, port
, pstate
);
863 scm_putc (tlr
, port
);
864 pstate
->top
= floor
+ 2;
869 long n
= pstate
->length
;
871 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
872 exp
= SCM_CDR (exp
); --n
;
873 for (; scm_is_pair (exp
); exp
= SCM_CDR (exp
))
875 register unsigned long i
;
877 for (i
= 0; i
< pstate
->top
; ++i
)
878 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
884 scm_puts (" ...", port
);
890 PUSH_REF(pstate
, exp
);
891 ++pstate
->list_offset
;
892 scm_putc (' ', port
);
894 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
897 if (!SCM_NULL_OR_NIL_P (exp
))
899 scm_puts (" . ", port
);
900 scm_iprin1 (exp
, port
, pstate
);
903 pstate
->list_offset
-= pstate
->top
- floor
- 2;
907 pstate
->list_offset
-= pstate
->top
- floor
- 2;
910 scm_puts (" . ", port
);
911 print_circref (port
, pstate
, exp
);
918 scm_valid_oport_value_p (SCM val
)
920 return (SCM_OPOUTPORTP (val
)
921 || (SCM_PORT_WITH_PS_P (val
)
922 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
925 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
928 scm_write (SCM obj
, SCM port
)
930 if (SCM_UNBNDP (port
))
931 port
= scm_current_output_port ();
933 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
935 scm_prin1 (obj
, port
, 1);
940 scm_close_port (port
);
944 return SCM_UNSPECIFIED
;
948 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
951 scm_display (SCM obj
, SCM port
)
953 if (SCM_UNBNDP (port
))
954 port
= scm_current_output_port ();
956 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
958 scm_prin1 (obj
, port
, 0);
963 scm_close_port (port
);
967 return SCM_UNSPECIFIED
;
971 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
972 (SCM destination
, SCM message
, SCM args
),
973 "Write @var{message} to @var{destination}, defaulting to\n"
974 "the current output port.\n"
975 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
976 "@code{~S} (was @code{%S}) escapes. When printed,\n"
977 "the escapes are replaced with corresponding members of\n"
979 "@code{~A} formats using @code{display} and @code{~S} formats\n"
980 "using @code{write}.\n"
981 "If @var{destination} is @code{#t}, then use the current output\n"
982 "port, if @var{destination} is @code{#f}, then return a string\n"
983 "containing the formatted text. Does not add a trailing newline.")
984 #define FUNC_NAME s_scm_simple_format
986 SCM port
, answer
= SCM_UNSPECIFIED
;
987 int fReturnString
= 0;
993 if (scm_is_eq (destination
, SCM_BOOL_T
))
995 destination
= port
= scm_current_output_port ();
997 else if (scm_is_false (destination
))
1000 port
= scm_mkstrport (SCM_INUM0
,
1001 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
1002 SCM_OPN
| SCM_WRTNG
,
1008 SCM_VALIDATE_OPORT_VALUE (1, destination
);
1009 port
= SCM_COERCE_OUTPORT (destination
);
1011 SCM_VALIDATE_STRING (2, message
);
1012 SCM_VALIDATE_REST_ARGUMENT (args
);
1014 start
= scm_i_string_chars (message
);
1015 end
= start
+ scm_i_string_length (message
);
1016 for (p
= start
; p
!= end
; ++p
)
1031 scm_lfwrite (start
, p
- start
, port
);
1035 scm_lfwrite (start
, p
- start
- 1, port
);
1040 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
1041 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1046 if (!scm_is_pair (args
))
1047 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1048 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1050 scm_lfwrite (start
, p
- start
- 1, port
);
1051 /* we pass destination here */
1052 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1053 args
= SCM_CDR (args
);
1057 scm_lfwrite (start
, p
- start
, port
);
1058 if (!scm_is_eq (args
, SCM_EOL
))
1059 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1060 scm_list_1 (scm_length (args
)));
1063 answer
= scm_strport_to_string (destination
);
1065 return scm_return_first (answer
, message
);
1070 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1072 "Send a newline to @var{port}.\n"
1073 "If @var{port} is omitted, send to the current output port.")
1074 #define FUNC_NAME s_scm_newline
1076 if (SCM_UNBNDP (port
))
1077 port
= scm_current_output_port ();
1079 SCM_VALIDATE_OPORT_VALUE (1, port
);
1081 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1082 return SCM_UNSPECIFIED
;
1086 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1087 (SCM chr
, SCM port
),
1088 "Send character @var{chr} to @var{port}.")
1089 #define FUNC_NAME s_scm_write_char
1091 if (SCM_UNBNDP (port
))
1092 port
= scm_current_output_port ();
1094 SCM_VALIDATE_CHAR (1, chr
);
1095 SCM_VALIDATE_OPORT_VALUE (2, port
);
1097 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1102 scm_close_port (port
);
1106 return SCM_UNSPECIFIED
;
1112 /* Call back to Scheme code to do the printing of special objects
1113 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1114 * containing PORT and PSTATE. This object can be used as the port for
1115 * display/write etc to continue the current print chain. The REVEALED
1116 * field of PSTATE is set to true to indicate that the print state has
1117 * escaped to Scheme and thus has to be freed by the GC.
1120 scm_t_bits scm_tc16_port_with_ps
;
1122 /* Print exactly as the port itself would */
1125 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1127 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1128 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1132 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1134 pstate
->revealed
= 1;
1135 return scm_call_2 (proc
, exp
,
1136 scm_i_port_with_print_state (port
, pstate
->handle
));
1139 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 1, 1, 0,
1140 (SCM port
, SCM pstate
),
1141 "Create a new port which behaves like @var{port}, but with an\n"
1142 "included print state @var{pstate}. @var{pstate} is optional.\n"
1143 "If @var{pstate} isn't supplied and @var{port} already has\n"
1144 "a print state, the old print state is reused.")
1145 #define FUNC_NAME s_scm_port_with_print_state
1147 SCM_VALIDATE_OPORT_VALUE (1, port
);
1148 if (!SCM_UNBNDP (pstate
))
1149 SCM_VALIDATE_PRINTSTATE (2, pstate
);
1150 return scm_i_port_with_print_state (port
, pstate
);
1154 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1156 "Return the print state of the port @var{port}. If @var{port}\n"
1157 "has no associated print state, @code{#f} is returned.")
1158 #define FUNC_NAME s_scm_get_print_state
1160 if (SCM_PORT_WITH_PS_P (port
))
1161 return SCM_PORT_WITH_PS_PS (port
);
1162 if (SCM_OUTPUT_PORT_P (port
))
1164 SCM_WRONG_TYPE_ARG (1, port
);
1173 SCM vtable
, layout
, type
;
1175 scm_init_opts (scm_print_options
, scm_print_opts
);
1177 scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"),
1178 scm_from_locale_string ("{"),
1179 scm_from_locale_symbol ("highlight-suffix"),
1180 scm_from_locale_string ("}")));
1182 scm_gc_register_root (&print_state_pool
);
1183 scm_gc_register_root (&scm_print_state_vtable
);
1184 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1186 scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT
));
1187 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1188 scm_set_struct_vtable_name_x (type
, scm_from_locale_symbol ("print-state"));
1189 scm_print_state_vtable
= type
;
1191 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1192 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1193 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1194 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1196 #include "libguile/print.x"
1198 scm_print_opts
[SCM_PRINT_KEYWORD_STYLE_I
].val
= SCM_UNPACK (sym_reader
);