1 /* Copyright (C) 1995-1999,2000,2001, 2002, 2003, 2004, 2006 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
23 #include "libguile/_scm.h"
24 #include "libguile/chars.h"
25 #include "libguile/continuations.h"
26 #include "libguile/smob.h"
27 #include "libguile/eval.h"
28 #include "libguile/macros.h"
29 #include "libguile/procprop.h"
30 #include "libguile/read.h"
31 #include "libguile/weaks.h"
32 #include "libguile/unif.h"
33 #include "libguile/alist.h"
34 #include "libguile/struct.h"
35 #include "libguile/objects.h"
36 #include "libguile/ports.h"
37 #include "libguile/root.h"
38 #include "libguile/strings.h"
39 #include "libguile/strports.h"
40 #include "libguile/vectors.h"
41 #include "libguile/lang.h"
42 #include "libguile/numbers.h"
44 #include "libguile/validate.h"
45 #include "libguile/print.h"
48 /* {Names of immediate symbols}
50 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
53 /* This table must agree with the list of flags in tags.h. */
54 static const char *iflagnames
[] =
63 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
66 /* Elisp nil value. This is its Scheme name; whenever it's printed in
67 * Elisp, it should appear as the symbol `nil'. */
71 SCM_SYMBOL (sym_reader
, "reader");
73 scm_t_option scm_print_opts
[] = {
74 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK (SCM_BOOL_F
),
75 "Hook for printing closures (should handle macros as well)." },
76 { SCM_OPTION_BOOLEAN
, "source", 0,
77 "Print closures with source." },
78 { SCM_OPTION_SCM
, "highlight-prefix", (unsigned long)SCM_BOOL_F
,
79 "The string to print before highlighted values." },
80 { SCM_OPTION_SCM
, "highlight-suffix", (unsigned long)SCM_BOOL_F
,
81 "The string to print after highlighted values." },
82 { SCM_OPTION_SCM
, "quote-keywordish-symbols", (unsigned long)SCM_BOOL_F
,
83 "How to print symbols that have a colon as their first or last character. "
84 "The value '#f' does not quote the colons; '#t' quotes them; "
85 "'reader' quotes them when the reader option 'keywords' is not '#f'."
89 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
91 "Option interface for the print options. Instead of using\n"
92 "this procedure directly, use the procedures\n"
93 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
94 "and @code{print-options}.")
95 #define FUNC_NAME s_scm_print_options
97 SCM ans
= scm_options (setting
,
106 /* {Printing of Scheme Objects}
109 /* Detection of circular references.
111 * Due to other constraints in the implementation, this code has bad
112 * time complexity (O (depth * N)), The printer code can be
113 * rewritten to be O(N).
115 #define PUSH_REF(pstate, obj) \
118 PSTATE_STACK_SET (pstate, pstate->top, obj); \
120 if (pstate->top == pstate->ceiling) \
121 grow_ref_stack (pstate); \
124 #define ENTER_NESTED_DATA(pstate, obj, label) \
127 register unsigned long i; \
128 for (i = 0; i < pstate->top; ++i) \
129 if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
131 if (pstate->fancyp) \
133 if (pstate->top - pstate->list_offset >= pstate->level) \
135 scm_putc ('#', port); \
139 PUSH_REF(pstate, obj); \
142 #define EXIT_NESTED_DATA(pstate) \
146 PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
150 SCM scm_print_state_vtable
= SCM_BOOL_F
;
151 static SCM print_state_pool
= SCM_EOL
;
152 scm_i_pthread_mutex_t print_state_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
154 #ifdef GUILE_DEBUG /* Used for debugging purposes */
156 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
158 "Return the current-pstate -- the car of the\n"
159 "@code{print_state_pool}. @code{current-pstate} is only\n"
160 "included in @code{--enable-guile-debug} builds.")
161 #define FUNC_NAME s_scm_current_pstate
163 if (!scm_is_null (print_state_pool
))
164 return SCM_CAR (print_state_pool
);
172 #define PSTATE_SIZE 50L
175 make_print_state (void)
178 = scm_make_struct (scm_print_state_vtable
, SCM_INUM0
, SCM_EOL
);
179 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
180 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
181 pstate
->ceiling
= SCM_SIMPLE_VECTOR_LENGTH (pstate
->ref_vect
);
182 pstate
->highlight_objects
= SCM_EOL
;
187 scm_make_print_state ()
189 SCM answer
= SCM_BOOL_F
;
191 /* First try to allocate a print state from the pool */
192 scm_i_pthread_mutex_lock (&print_state_mutex
);
193 if (!scm_is_null (print_state_pool
))
195 answer
= SCM_CAR (print_state_pool
);
196 print_state_pool
= SCM_CDR (print_state_pool
);
198 scm_i_pthread_mutex_unlock (&print_state_mutex
);
200 return scm_is_false (answer
) ? make_print_state () : answer
;
204 scm_free_print_state (SCM print_state
)
207 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
208 /* Cleanup before returning print state to pool.
209 * It is better to do it here. Doing it in scm_prin1
210 * would cost more since that function is called much more
214 pstate
->revealed
= 0;
215 pstate
->highlight_objects
= SCM_EOL
;
216 scm_i_pthread_mutex_lock (&print_state_mutex
);
217 handle
= scm_cons (print_state
, print_state_pool
);
218 print_state_pool
= handle
;
219 scm_i_pthread_mutex_unlock (&print_state_mutex
);
223 scm_i_port_with_print_state (SCM port
, SCM print_state
)
225 if (SCM_UNBNDP (print_state
))
227 if (SCM_PORT_WITH_PS_P (port
))
230 print_state
= scm_make_print_state ();
231 /* port does not need to be coerced since it doesn't have ps */
234 port
= SCM_COERCE_OUTPORT (port
);
235 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps
,
236 SCM_UNPACK (scm_cons (port
, print_state
)));
240 grow_ref_stack (scm_print_state
*pstate
)
242 SCM old_vect
= pstate
->ref_vect
;
243 size_t old_size
= SCM_SIMPLE_VECTOR_LENGTH (old_vect
);
244 size_t new_size
= 2 * pstate
->ceiling
;
245 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
248 for (i
= 0; i
!= old_size
; ++i
)
249 SCM_SIMPLE_VECTOR_SET (new_vect
, i
, SCM_SIMPLE_VECTOR_REF (old_vect
, i
));
251 pstate
->ref_vect
= new_vect
;
252 pstate
->ceiling
= new_size
;
255 #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
256 #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
259 print_circref (SCM port
, scm_print_state
*pstate
, SCM ref
)
262 long self
= pstate
->top
- 1;
264 if (scm_is_pair (PSTATE_STACK_REF (pstate
, i
)))
268 if (!scm_is_pair (PSTATE_STACK_REF (pstate
, i
-1))
269 || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate
, i
-1)),
270 SCM_CDR (PSTATE_STACK_REF (pstate
, i
))))
276 for (i
= pstate
->top
- 1; 1; --i
)
277 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), ref
))
279 scm_putc ('#', port
);
280 scm_intprint (i
- self
, 10, port
);
281 scm_putc ('#', port
);
284 /* Print the name of a symbol. */
287 quote_keywordish_symbol (const char *str
, size_t len
)
291 /* LEN is guaranteed to be > 0.
293 if (str
[0] != ':' && str
[len
-1] != ':')
296 option
= SCM_PRINT_KEYWORD_STYLE
;
297 if (scm_is_false (option
))
299 if (scm_is_eq (option
, sym_reader
))
300 return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE
));
305 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
307 /* This points to the first character that has not yet been written to the
310 /* This points to the character we're currently looking at. */
312 /* If the name contains weird characters, we'll escape them with
313 * backslashes and set this flag; it indicates that we should surround the
314 * name with "#{" and "}#". */
316 /* Backslashes are not sufficient to make a name weird, but if a name is
317 * weird because of other characters, backslahes need to be escaped too.
318 * The first time we see a backslash, we set maybe_weird, and mw_pos points
319 * to the backslash. Then if the name turns out to be weird, we re-process
320 * everything starting from mw_pos.
321 * We could instead make backslashes always weird. This is not necessary
322 * to ensure that the output is (read)-able, but it would make this code
323 * simpler and faster. */
327 if (len
== 0 || str
[0] == '\'' || str
[0] == '`' || str
[0] == ','
328 || quote_keywordish_symbol (str
, len
)
329 || (str
[0] == '.' && len
== 1)
330 || scm_is_true (scm_c_locale_stringn_to_number (str
, len
, 10)))
332 scm_lfwrite ("#{", 2, port
);
336 for (end
= pos
; end
< len
; ++end
)
339 #ifdef BRACKETS_AS_PARENS
348 case SCM_WHITE_SPACES
:
349 case SCM_LINE_INCREMENTORS
:
358 scm_lfwrite ("#{", 2, port
);
362 scm_lfwrite (str
+ pos
, end
- pos
, port
);
367 scm_lfwrite (buf
, 2, port
);
384 scm_lfwrite (str
+ pos
, end
- pos
, port
);
386 scm_lfwrite ("}#", 2, port
);
389 /* Print generally. Handles both write and display according to PSTATE.
391 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
392 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
394 static void iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
);
397 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
400 && scm_is_true (scm_memq (exp
, pstate
->highlight_objects
)))
402 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX
, port
);
403 iprin1 (exp
, port
, pstate
);
404 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX
, port
);
407 iprin1 (exp
, port
, pstate
);
411 iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
413 switch (SCM_ITAG3 (exp
))
415 case scm_tc3_closure
:
418 /* These tc3 tags should never occur in an immediate value. They are
419 * only used in cell types of non-immediates, i. e. the value returned
420 * by SCM_CELL_TYPE (exp) can use these tags.
422 scm_ipruk ("immediate", exp
, port
);
426 scm_intprint (SCM_I_INUM (exp
), 10, port
);
431 long i
= SCM_CHAR (exp
);
433 if (SCM_WRITINGP (pstate
))
435 scm_puts ("#\\", port
);
436 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
437 scm_puts (scm_charnames
[i
], port
);
439 else if (i
== '\177')
440 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
442 else if (i
< 0 || i
> '\177')
443 scm_intprint (i
, 8, port
);
450 else if (SCM_IFLAGP (exp
)
451 && ((size_t) SCM_IFLAGNUM (exp
) < (sizeof iflagnames
/ sizeof (char *))))
453 scm_puts (iflagnames
[SCM_IFLAGNUM (exp
)], port
);
455 else if (SCM_ISYMP (exp
))
457 scm_i_print_isym (exp
, port
);
459 else if (SCM_ILOCP (exp
))
461 scm_i_print_iloc (exp
, port
);
465 /* unknown immediate value */
466 scm_ipruk ("immediate", exp
, port
);
470 switch (SCM_TYP7 (exp
))
474 ENTER_NESTED_DATA (pstate
, exp
, circref
);
475 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
477 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
480 pwps
= scm_i_port_with_print_state (port
, pstate
->handle
);
481 pstate
->revealed
= 1;
482 scm_call_generic_2 (print
, exp
, pwps
);
487 scm_print_struct (exp
, port
, pstate
);
489 EXIT_NESTED_DATA (pstate
);
492 case scm_tcs_cons_imcar
:
493 case scm_tcs_cons_nimcar
:
494 ENTER_NESTED_DATA (pstate
, exp
, circref
);
495 scm_iprlist ("(", exp
, ')', port
, pstate
);
496 EXIT_NESTED_DATA (pstate
);
499 print_circref (port
, pstate
, exp
);
501 case scm_tcs_closures
:
502 if (scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE
))
503 || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE
,
506 SCM formals
= SCM_CLOSURE_FORMALS (exp
);
507 scm_puts ("#<procedure", port
);
508 scm_putc (' ', port
);
509 scm_iprin1 (scm_procedure_name (exp
), port
, pstate
);
510 scm_putc (' ', port
);
511 if (SCM_PRINT_SOURCE_P
)
513 SCM env
= SCM_ENV (exp
);
514 SCM xenv
= SCM_EXTEND_ENV (formals
, SCM_EOL
, env
);
515 SCM src
= scm_i_unmemocopy_body (SCM_CODE (exp
), xenv
);
516 ENTER_NESTED_DATA (pstate
, exp
, circref
);
517 scm_iprin1 (src
, port
, pstate
);
518 EXIT_NESTED_DATA (pstate
);
521 scm_iprin1 (formals
, port
, pstate
);
522 scm_putc ('>', port
);
526 switch SCM_TYP16 (exp
) {
528 scm_bigprint (exp
, port
, pstate
);
531 scm_print_real (exp
, port
, pstate
);
533 case scm_tc16_complex
:
534 scm_print_complex (exp
, port
, pstate
);
536 case scm_tc16_fraction
:
537 scm_i_print_fraction (exp
, port
, pstate
);
542 if (SCM_WRITINGP (pstate
))
547 scm_putc ('"', port
);
548 len
= scm_i_string_length (exp
);
549 data
= scm_i_string_chars (exp
);
550 for (i
= 0, j
= 0; i
< len
; ++i
)
552 unsigned char ch
= data
[i
];
553 if ((ch
< 32 && ch
!= '\n') || (127 <= ch
&& ch
< 148))
555 static char const hex
[]="0123456789abcdef";
558 scm_lfwrite (data
+j
, i
-j
, port
);
561 buf
[2] = hex
[ch
/ 16];
562 buf
[3] = hex
[ch
% 16];
563 scm_lfwrite (buf
, 4, port
);
564 data
= scm_i_string_chars (exp
);
567 else if (ch
== '"' || ch
== '\\')
569 scm_lfwrite (data
+j
, i
-j
, port
);
570 scm_putc ('\\', port
);
571 data
= scm_i_string_chars (exp
);
575 scm_lfwrite (data
+j
, i
-j
, port
);
576 scm_putc ('"', port
);
577 scm_remember_upto_here_1 (exp
);
580 scm_lfwrite (scm_i_string_chars (exp
), scm_i_string_length (exp
),
582 scm_remember_upto_here_1 (exp
);
585 if (scm_i_symbol_is_interned (exp
))
587 scm_print_symbol_name (scm_i_symbol_chars (exp
),
588 scm_i_symbol_length (exp
),
590 scm_remember_upto_here_1 (exp
);
594 scm_puts ("#<uninterned-symbol ", port
);
595 scm_print_symbol_name (scm_i_symbol_chars (exp
),
596 scm_i_symbol_length (exp
),
598 scm_putc (' ', port
);
599 scm_uintprint (SCM_UNPACK (exp
), 16, port
);
600 scm_putc ('>', port
);
603 case scm_tc7_variable
:
604 scm_i_variable_print (exp
, port
, pstate
);
607 ENTER_NESTED_DATA (pstate
, exp
, circref
);
608 if (SCM_IS_WHVEC (exp
))
609 scm_puts ("#wh(", port
);
611 scm_puts ("#w(", port
);
612 goto common_vector_printer
;
615 ENTER_NESTED_DATA (pstate
, exp
, circref
);
616 scm_puts ("#(", port
);
617 common_vector_printer
:
620 long last
= SCM_SIMPLE_VECTOR_LENGTH (exp
) - 1;
623 && SCM_SIMPLE_VECTOR_LENGTH (exp
) > pstate
->length
)
625 last
= pstate
->length
- 1;
628 for (i
= 0; i
< last
; ++i
)
631 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
632 scm_putc (' ', port
);
637 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
640 scm_puts (" ...", port
);
641 scm_putc (')', port
);
643 EXIT_NESTED_DATA (pstate
);
646 scm_puts (SCM_SUBR_GENERIC (exp
)
647 ? "#<primitive-generic "
648 : "#<primitive-procedure ",
650 scm_puts (scm_i_symbol_chars (SCM_SNAME (exp
)), port
);
651 scm_putc ('>', port
);
656 SCM proc
= SCM_CCLO_SUBR (exp
);
657 if (scm_is_eq (proc
, scm_f_gsubr_apply
))
659 /* Print gsubrs as primitives */
660 SCM name
= scm_procedure_name (exp
);
661 scm_puts ("#<primitive-procedure", port
);
662 if (scm_is_true (name
))
664 scm_putc (' ', port
);
665 scm_puts (scm_i_symbol_chars (name
), port
);
670 scm_puts ("#<compiled-closure ", port
);
671 scm_iprin1 (proc
, port
, pstate
);
673 scm_putc ('>', port
);
678 scm_puts ("#<procedure-with-setter", port
);
680 SCM name
= scm_procedure_name (exp
);
681 if (scm_is_true (name
))
683 scm_putc (' ', port
);
684 scm_display (name
, port
);
687 scm_putc ('>', port
);
691 register long i
= SCM_PTOBNUM (exp
);
693 && scm_ptobs
[i
].print
694 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
699 ENTER_NESTED_DATA (pstate
, exp
, circref
);
700 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
701 EXIT_NESTED_DATA (pstate
);
705 scm_ipruk ("type", exp
, port
);
710 /* Print states are necessary for circular reference safe printing.
711 * They are also expensive to allocate. Therefore print states are
712 * kept in a pool so that they can be reused.
715 /* The PORT argument can also be a print-state/port pair, which will
716 * then be used instead of allocating a new print state. This is
717 * useful for continuing a chain of print calls from Scheme. */
720 scm_prin1 (SCM exp
, SCM port
, int writingp
)
722 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
724 scm_print_state
*pstate
;
727 /* If PORT is a print-state/port pair, use that. Else create a new
730 if (SCM_PORT_WITH_PS_P (port
))
732 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
733 port
= SCM_PORT_WITH_PS_PORT (port
);
737 /* First try to allocate a print state from the pool */
738 scm_i_pthread_mutex_lock (&print_state_mutex
);
739 if (!scm_is_null (print_state_pool
))
741 handle
= print_state_pool
;
742 print_state_pool
= SCM_CDR (print_state_pool
);
744 scm_i_pthread_mutex_unlock (&print_state_mutex
);
745 if (scm_is_false (handle
))
746 handle
= scm_list_1 (make_print_state ());
747 pstate_scm
= SCM_CAR (handle
);
750 pstate
= SCM_PRINT_STATE (pstate_scm
);
751 old_writingp
= pstate
->writingp
;
752 pstate
->writingp
= writingp
;
753 scm_iprin1 (exp
, port
, pstate
);
754 pstate
->writingp
= old_writingp
;
756 /* Return print state to pool if it has been created above and
757 hasn't escaped to Scheme. */
759 if (scm_is_true (handle
) && !pstate
->revealed
)
761 scm_i_pthread_mutex_lock (&print_state_mutex
);
762 SCM_SETCDR (handle
, print_state_pool
);
763 print_state_pool
= handle
;
764 scm_i_pthread_mutex_unlock (&print_state_mutex
);
773 scm_intprint (scm_t_intmax n
, int radix
, SCM port
)
775 char num_buf
[SCM_INTBUFLEN
];
776 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
780 scm_uintprint (scm_t_uintmax n
, int radix
, SCM port
)
782 char num_buf
[SCM_INTBUFLEN
];
783 scm_lfwrite (num_buf
, scm_iuint2str (n
, radix
, num_buf
), port
);
786 /* Print an object of unrecognized type.
790 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
792 scm_puts ("#<unknown-", port
);
793 scm_puts (hdr
, port
);
794 if (1) /* (scm_in_heap_p (ptr)) */ /* FIXME */
796 scm_puts (" (0x", port
);
797 scm_uintprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
798 scm_puts (" . 0x", port
);
799 scm_uintprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
800 scm_puts (") @", port
);
802 scm_puts (" 0x", port
);
803 scm_uintprint (SCM_UNPACK (ptr
), 16, port
);
804 scm_putc ('>', port
);
811 scm_iprlist (char *hdr
, SCM exp
, int tlr
, SCM port
, scm_print_state
*pstate
)
813 register SCM hare
, tortoise
;
814 long floor
= pstate
->top
- 2;
815 scm_puts (hdr
, port
);
820 /* Run a hare and tortoise so that total time complexity will be
821 O(depth * N) instead of O(N^2). */
822 hare
= SCM_CDR (exp
);
824 while (scm_is_pair (hare
))
826 if (scm_is_eq (hare
, tortoise
))
828 hare
= SCM_CDR (hare
);
829 if (!scm_is_pair (hare
))
831 hare
= SCM_CDR (hare
);
832 tortoise
= SCM_CDR (tortoise
);
835 /* No cdr cycles intrinsic to this list */
836 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
837 for (exp
= SCM_CDR (exp
); scm_is_pair (exp
); exp
= SCM_CDR (exp
))
841 for (i
= floor
; i
>= 0; --i
)
842 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
844 PUSH_REF (pstate
, exp
);
845 scm_putc (' ', port
);
847 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
849 if (!SCM_NULL_OR_NIL_P (exp
))
851 scm_puts (" . ", port
);
852 scm_iprin1 (exp
, port
, pstate
);
856 scm_putc (tlr
, port
);
857 pstate
->top
= floor
+ 2;
862 long n
= pstate
->length
;
864 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
865 exp
= SCM_CDR (exp
); --n
;
866 for (; scm_is_pair (exp
); exp
= SCM_CDR (exp
))
868 register unsigned long i
;
870 for (i
= 0; i
< pstate
->top
; ++i
)
871 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
877 scm_puts (" ...", port
);
883 PUSH_REF(pstate
, exp
);
884 ++pstate
->list_offset
;
885 scm_putc (' ', port
);
887 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
890 if (!SCM_NULL_OR_NIL_P (exp
))
892 scm_puts (" . ", port
);
893 scm_iprin1 (exp
, port
, pstate
);
896 pstate
->list_offset
-= pstate
->top
- floor
- 2;
900 pstate
->list_offset
-= pstate
->top
- floor
- 2;
903 scm_puts (" . ", port
);
904 print_circref (port
, pstate
, exp
);
911 scm_valid_oport_value_p (SCM val
)
913 return (SCM_OPOUTPORTP (val
)
914 || (SCM_PORT_WITH_PS_P (val
)
915 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
918 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
921 scm_write (SCM obj
, SCM port
)
923 if (SCM_UNBNDP (port
))
924 port
= scm_current_output_port ();
926 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
928 scm_prin1 (obj
, port
, 1);
932 scm_close_port (port
);
935 return SCM_UNSPECIFIED
;
939 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
942 scm_display (SCM obj
, SCM port
)
944 if (SCM_UNBNDP (port
))
945 port
= scm_current_output_port ();
947 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
949 scm_prin1 (obj
, port
, 0);
953 scm_close_port (port
);
956 return SCM_UNSPECIFIED
;
960 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
961 (SCM destination
, SCM message
, SCM args
),
962 "Write @var{message} to @var{destination}, defaulting to\n"
963 "the current output port.\n"
964 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
965 "@code{~S} (was @code{%S}) escapes. When printed,\n"
966 "the escapes are replaced with corresponding members of\n"
968 "@code{~A} formats using @code{display} and @code{~S} formats\n"
969 "using @code{write}.\n"
970 "If @var{destination} is @code{#t}, then use the current output\n"
971 "port, if @var{destination} is @code{#f}, then return a string\n"
972 "containing the formatted text. Does not add a trailing newline.")
973 #define FUNC_NAME s_scm_simple_format
975 SCM port
, answer
= SCM_UNSPECIFIED
;
976 int fReturnString
= 0;
982 if (scm_is_eq (destination
, SCM_BOOL_T
))
984 destination
= port
= scm_current_output_port ();
986 else if (scm_is_false (destination
))
989 port
= scm_mkstrport (SCM_INUM0
,
990 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
997 SCM_VALIDATE_OPORT_VALUE (1, destination
);
998 port
= SCM_COERCE_OUTPORT (destination
);
1000 SCM_VALIDATE_STRING (2, message
);
1001 SCM_VALIDATE_REST_ARGUMENT (args
);
1003 start
= scm_i_string_chars (message
);
1004 end
= start
+ scm_i_string_length (message
);
1005 for (p
= start
; p
!= end
; ++p
)
1020 scm_lfwrite (start
, p
- start
, port
);
1024 scm_lfwrite (start
, p
- start
- 1, port
);
1029 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
1030 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1035 if (!scm_is_pair (args
))
1036 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1037 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1039 scm_lfwrite (start
, p
- start
- 1, port
);
1040 /* we pass destination here */
1041 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1042 args
= SCM_CDR (args
);
1046 scm_lfwrite (start
, p
- start
, port
);
1047 if (!scm_is_eq (args
, SCM_EOL
))
1048 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1049 scm_list_1 (scm_length (args
)));
1052 answer
= scm_strport_to_string (destination
);
1054 return scm_return_first (answer
, message
);
1059 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1061 "Send a newline to @var{port}.\n"
1062 "If @var{port} is omitted, send to the current output port.")
1063 #define FUNC_NAME s_scm_newline
1065 if (SCM_UNBNDP (port
))
1066 port
= scm_current_output_port ();
1068 SCM_VALIDATE_OPORT_VALUE (1, port
);
1070 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1071 return SCM_UNSPECIFIED
;
1075 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1076 (SCM chr
, SCM port
),
1077 "Send character @var{chr} to @var{port}.")
1078 #define FUNC_NAME s_scm_write_char
1080 if (SCM_UNBNDP (port
))
1081 port
= scm_current_output_port ();
1083 SCM_VALIDATE_CHAR (1, chr
);
1084 SCM_VALIDATE_OPORT_VALUE (2, port
);
1086 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1090 scm_close_port (port
);
1093 return SCM_UNSPECIFIED
;
1099 /* Call back to Scheme code to do the printing of special objects
1100 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1101 * containing PORT and PSTATE. This object can be used as the port for
1102 * display/write etc to continue the current print chain. The REVEALED
1103 * field of PSTATE is set to true to indicate that the print state has
1104 * escaped to Scheme and thus has to be freed by the GC.
1107 scm_t_bits scm_tc16_port_with_ps
;
1109 /* Print exactly as the port itself would */
1112 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1114 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1115 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1119 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1121 pstate
->revealed
= 1;
1122 return scm_call_2 (proc
, exp
,
1123 scm_i_port_with_print_state (port
, pstate
->handle
));
1126 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 1, 1, 0,
1127 (SCM port
, SCM pstate
),
1128 "Create a new port which behaves like @var{port}, but with an\n"
1129 "included print state @var{pstate}. @var{pstate} is optional.\n"
1130 "If @var{pstate} isn't supplied and @var{port} already has\n"
1131 "a print state, the old print state is reused.")
1132 #define FUNC_NAME s_scm_port_with_print_state
1134 SCM_VALIDATE_OPORT_VALUE (1, port
);
1135 if (!SCM_UNBNDP (pstate
))
1136 SCM_VALIDATE_PRINTSTATE (2, pstate
);
1137 return scm_i_port_with_print_state (port
, pstate
);
1141 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1143 "Return the print state of the port @var{port}. If @var{port}\n"
1144 "has no associated print state, @code{#f} is returned.")
1145 #define FUNC_NAME s_scm_get_print_state
1147 if (SCM_PORT_WITH_PS_P (port
))
1148 return SCM_PORT_WITH_PS_PS (port
);
1149 if (SCM_OUTPUT_PORT_P (port
))
1151 SCM_WRONG_TYPE_ARG (1, port
);
1160 SCM vtable
, layout
, type
;
1162 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1164 scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"),
1165 scm_from_locale_string ("{"),
1166 scm_from_locale_symbol ("highlight-suffix"),
1167 scm_from_locale_string ("}")));
1169 scm_gc_register_root (&print_state_pool
);
1170 scm_gc_register_root (&scm_print_state_vtable
);
1171 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1173 scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT
));
1174 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1175 scm_set_struct_vtable_name_x (type
, scm_from_locale_symbol ("print-state"));
1176 scm_print_state_vtable
= type
;
1178 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1179 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1180 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1181 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1183 #include "libguile/print.x"
1185 scm_print_opts
[SCM_PRINT_KEYWORD_STYLE_I
].val
= SCM_UNPACK (sym_reader
);