1 /* Copyright (C) 1995-1999,2000,2001, 2002, 2003, 2004 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_t_option scm_print_opts
[] = {
72 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK (SCM_BOOL_F
),
73 "Hook for printing closures (should handle macros as well)." },
74 { SCM_OPTION_BOOLEAN
, "source", 0,
75 "Print closures with source." },
76 { SCM_OPTION_SCM
, "highlight-prefix", (unsigned long)SCM_BOOL_F
,
77 "The string to print before highlighted values." },
78 { SCM_OPTION_SCM
, "highlight-suffix", (unsigned long)SCM_BOOL_F
,
79 "The string to print after highlighted values." }
82 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
84 "Option interface for the print options. Instead of using\n"
85 "this procedure directly, use the procedures\n"
86 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
87 "and @code{print-options}.")
88 #define FUNC_NAME s_scm_print_options
90 SCM ans
= scm_options (setting
,
99 /* {Printing of Scheme Objects}
102 /* Detection of circular references.
104 * Due to other constraints in the implementation, this code has bad
105 * time complexity (O (depth * N)), The printer code can be
106 * rewritten to be O(N).
108 #define PUSH_REF(pstate, obj) \
110 PSTATE_STACK_SET (pstate, pstate->top++, obj); \
111 if (pstate->top == pstate->ceiling) \
112 grow_ref_stack (pstate); \
115 #define ENTER_NESTED_DATA(pstate, obj, label) \
117 register unsigned long i; \
118 for (i = 0; i < pstate->top; ++i) \
119 if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
121 if (pstate->fancyp) \
123 if (pstate->top - pstate->list_offset >= pstate->level) \
125 scm_putc ('#', port); \
129 PUSH_REF(pstate, obj); \
132 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
134 SCM scm_print_state_vtable
= SCM_BOOL_F
;
135 static SCM print_state_pool
= SCM_EOL
;
136 scm_i_pthread_mutex_t print_state_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
138 #ifdef GUILE_DEBUG /* Used for debugging purposes */
140 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
142 "Return the current-pstate -- the car of the\n"
143 "@code{print_state_pool}. @code{current-pstate} is only\n"
144 "included in @code{--enable-guile-debug} builds.")
145 #define FUNC_NAME s_scm_current_pstate
147 if (!scm_is_null (print_state_pool
))
148 return SCM_CAR (print_state_pool
);
156 #define PSTATE_SIZE 50L
159 make_print_state (void)
162 = scm_make_struct (scm_print_state_vtable
, SCM_INUM0
, SCM_EOL
);
163 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
164 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
165 pstate
->ceiling
= SCM_SIMPLE_VECTOR_LENGTH (pstate
->ref_vect
);
166 pstate
->highlight_objects
= SCM_EOL
;
171 scm_make_print_state ()
173 SCM answer
= SCM_BOOL_F
;
175 /* First try to allocate a print state from the pool */
176 scm_i_pthread_mutex_lock (&print_state_mutex
);
177 if (!scm_is_null (print_state_pool
))
179 answer
= SCM_CAR (print_state_pool
);
180 print_state_pool
= SCM_CDR (print_state_pool
);
182 scm_i_pthread_mutex_unlock (&print_state_mutex
);
184 return scm_is_false (answer
) ? make_print_state () : answer
;
188 scm_free_print_state (SCM print_state
)
191 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
192 /* Cleanup before returning print state to pool.
193 * It is better to do it here. Doing it in scm_prin1
194 * would cost more since that function is called much more
198 pstate
->revealed
= 0;
199 pstate
->highlight_objects
= SCM_EOL
;
200 scm_i_pthread_mutex_lock (&print_state_mutex
);
201 handle
= scm_cons (print_state
, print_state_pool
);
202 print_state_pool
= handle
;
203 scm_i_pthread_mutex_unlock (&print_state_mutex
);
207 scm_i_port_with_print_state (SCM port
, SCM print_state
)
209 if (SCM_UNBNDP (print_state
))
211 if (SCM_PORT_WITH_PS_P (port
))
214 print_state
= scm_make_print_state ();
215 /* port does not need to be coerced since it doesn't have ps */
218 port
= SCM_COERCE_OUTPORT (port
);
219 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps
,
220 SCM_UNPACK (scm_cons (port
, print_state
)));
224 grow_ref_stack (scm_print_state
*pstate
)
226 SCM old_vect
= pstate
->ref_vect
;
227 size_t old_size
= SCM_SIMPLE_VECTOR_LENGTH (old_vect
);
228 size_t new_size
= 2 * pstate
->ceiling
;
229 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
232 for (i
= 0; i
!= old_size
; ++i
)
233 SCM_SIMPLE_VECTOR_SET (new_vect
, i
, SCM_SIMPLE_VECTOR_REF (old_vect
, i
));
235 pstate
->ref_vect
= new_vect
;
236 pstate
->ceiling
= new_size
;
239 #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
240 #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
243 print_circref (SCM port
, scm_print_state
*pstate
, SCM ref
)
246 long self
= pstate
->top
- 1;
248 if (scm_is_pair (PSTATE_STACK_REF (pstate
, i
)))
252 if (!scm_is_pair (PSTATE_STACK_REF (pstate
, i
-1))
253 || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate
, i
-1)),
254 SCM_CDR (PSTATE_STACK_REF (pstate
, i
))))
260 for (i
= pstate
->top
- 1; 1; --i
)
261 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), ref
))
263 scm_putc ('#', port
);
264 scm_intprint (i
- self
, 10, port
);
265 scm_putc ('#', port
);
268 /* Print the name of a symbol. */
271 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
273 /* This points to the first character that has not yet been written to the
276 /* This points to the character we're currently looking at. */
278 /* If the name contains weird characters, we'll escape them with
279 * backslashes and set this flag; it indicates that we should surround the
280 * name with "#{" and "}#". */
282 /* Backslashes are not sufficient to make a name weird, but if a name is
283 * weird because of other characters, backslahes need to be escaped too.
284 * The first time we see a backslash, we set maybe_weird, and mw_pos points
285 * to the backslash. Then if the name turns out to be weird, we re-process
286 * everything starting from mw_pos.
287 * We could instead make backslashes always weird. This is not necessary
288 * to ensure that the output is (read)-able, but it would make this code
289 * simpler and faster. */
293 if (len
== 0 || str
[0] == '\'' || str
[0] == '`' || str
[0] == ',' ||
294 str
[0] == ':' || str
[len
-1] == ':' || (str
[0] == '.' && len
== 1) ||
295 scm_is_true (scm_i_mem2number(str
, len
, 10)))
297 scm_lfwrite ("#{", 2, port
);
301 for (end
= pos
; end
< len
; ++end
)
304 #ifdef BRACKETS_AS_PARENS
313 case SCM_WHITE_SPACES
:
314 case SCM_LINE_INCREMENTORS
:
323 scm_lfwrite ("#{", 2, port
);
327 scm_lfwrite (str
+ pos
, end
- pos
, port
);
332 scm_lfwrite (buf
, 2, port
);
349 scm_lfwrite (str
+ pos
, end
- pos
, port
);
351 scm_lfwrite ("}#", 2, port
);
354 /* Print generally. Handles both write and display according to PSTATE.
356 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
357 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
359 static void iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
);
362 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
365 && scm_is_true (scm_memq (exp
, pstate
->highlight_objects
)))
367 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX
, port
);
368 iprin1 (exp
, port
, pstate
);
369 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX
, port
);
372 iprin1 (exp
, port
, pstate
);
376 iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
378 switch (SCM_ITAG3 (exp
))
380 case scm_tc3_closure
:
383 /* These tc3 tags should never occur in an immediate value. They are
384 * only used in cell types of non-immediates, i. e. the value returned
385 * by SCM_CELL_TYPE (exp) can use these tags.
387 scm_ipruk ("immediate", exp
, port
);
391 scm_intprint (SCM_I_INUM (exp
), 10, port
);
396 long i
= SCM_CHAR (exp
);
398 if (SCM_WRITINGP (pstate
))
400 scm_puts ("#\\", port
);
401 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
402 scm_puts (scm_charnames
[i
], port
);
404 else if (i
== '\177')
405 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
407 else if (i
< 0 || i
> '\177')
408 scm_intprint (i
, 8, port
);
415 else if (SCM_IFLAGP (exp
)
416 && ((size_t) SCM_IFLAGNUM (exp
) < (sizeof iflagnames
/ sizeof (char *))))
418 scm_puts (iflagnames
[SCM_IFLAGNUM (exp
)], port
);
420 else if (SCM_ISYMP (exp
))
422 scm_i_print_isym (exp
, port
);
424 else if (SCM_ILOCP (exp
))
426 scm_i_print_iloc (exp
, port
);
430 /* unknown immediate value */
431 scm_ipruk ("immediate", exp
, port
);
435 switch (SCM_TYP7 (exp
))
439 ENTER_NESTED_DATA (pstate
, exp
, circref
);
440 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
442 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
445 pwps
= scm_i_port_with_print_state (port
, pstate
->handle
);
446 pstate
->revealed
= 1;
447 scm_call_generic_2 (print
, exp
, pwps
);
452 scm_print_struct (exp
, port
, pstate
);
454 EXIT_NESTED_DATA (pstate
);
457 case scm_tcs_cons_imcar
:
458 case scm_tcs_cons_nimcar
:
459 ENTER_NESTED_DATA (pstate
, exp
, circref
);
460 scm_iprlist ("(", exp
, ')', port
, pstate
);
461 EXIT_NESTED_DATA (pstate
);
464 print_circref (port
, pstate
, exp
);
466 case scm_tcs_closures
:
467 if (scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE
))
468 || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE
,
471 SCM formals
= SCM_CLOSURE_FORMALS (exp
);
472 scm_puts ("#<procedure", port
);
473 scm_putc (' ', port
);
474 scm_iprin1 (scm_procedure_name (exp
), port
, pstate
);
475 scm_putc (' ', port
);
476 if (SCM_PRINT_SOURCE_P
)
478 SCM env
= SCM_ENV (exp
);
479 SCM xenv
= SCM_EXTEND_ENV (formals
, SCM_EOL
, env
);
480 SCM src
= scm_i_unmemocopy_body (SCM_CODE (exp
), xenv
);
481 ENTER_NESTED_DATA (pstate
, exp
, circref
);
482 scm_iprin1 (src
, port
, pstate
);
483 EXIT_NESTED_DATA (pstate
);
486 scm_iprin1 (formals
, port
, pstate
);
487 scm_putc ('>', port
);
491 switch SCM_TYP16 (exp
) {
493 scm_bigprint (exp
, port
, pstate
);
496 scm_print_real (exp
, port
, pstate
);
498 case scm_tc16_complex
:
499 scm_print_complex (exp
, port
, pstate
);
501 case scm_tc16_fraction
:
502 scm_i_print_fraction (exp
, port
, pstate
);
507 if (SCM_WRITINGP (pstate
))
512 scm_putc ('"', port
);
513 len
= scm_i_string_length (exp
);
514 data
= scm_i_string_chars (exp
);
515 for (i
= 0, j
= 0; i
< len
; ++i
)
517 unsigned char ch
= data
[i
];
518 if ((ch
< 32 && ch
!= '\n') || (127 <= ch
&& ch
< 148))
520 static char const hex
[]="0123456789abcdef";
523 scm_lfwrite (data
+j
, i
-j
, port
);
526 buf
[2] = hex
[ch
/ 16];
527 buf
[3] = hex
[ch
% 16];
528 scm_lfwrite (buf
, 4, port
);
529 data
= scm_i_string_chars (exp
);
532 else if (ch
== '"' || ch
== '\\')
534 scm_lfwrite (data
+j
, i
-j
, port
);
535 scm_putc ('\\', port
);
536 data
= scm_i_string_chars (exp
);
540 scm_lfwrite (data
+j
, i
-j
, port
);
541 scm_putc ('"', port
);
542 scm_remember_upto_here_1 (exp
);
545 scm_lfwrite (scm_i_string_chars (exp
), scm_i_string_length (exp
),
547 scm_remember_upto_here_1 (exp
);
550 if (scm_i_symbol_is_interned (exp
))
552 scm_print_symbol_name (scm_i_symbol_chars (exp
),
553 scm_i_symbol_length (exp
),
555 scm_remember_upto_here_1 (exp
);
559 scm_puts ("#<uninterned-symbol ", port
);
560 scm_print_symbol_name (scm_i_symbol_chars (exp
),
561 scm_i_symbol_length (exp
),
563 scm_putc (' ', port
);
564 scm_uintprint (SCM_UNPACK (exp
), 16, port
);
565 scm_putc ('>', port
);
568 case scm_tc7_variable
:
569 scm_i_variable_print (exp
, port
, pstate
);
572 ENTER_NESTED_DATA (pstate
, exp
, circref
);
573 if (SCM_IS_WHVEC (exp
))
574 scm_puts ("#wh(", port
);
576 scm_puts ("#w(", port
);
577 goto common_vector_printer
;
580 ENTER_NESTED_DATA (pstate
, exp
, circref
);
581 scm_puts ("#(", port
);
582 common_vector_printer
:
585 long last
= SCM_SIMPLE_VECTOR_LENGTH (exp
) - 1;
588 && SCM_SIMPLE_VECTOR_LENGTH (exp
) > pstate
->length
)
590 last
= pstate
->length
- 1;
593 for (i
= 0; i
< last
; ++i
)
596 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
597 scm_putc (' ', port
);
602 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp
, i
), port
, pstate
);
605 scm_puts (" ...", port
);
606 scm_putc (')', port
);
608 EXIT_NESTED_DATA (pstate
);
611 scm_puts (SCM_SUBR_GENERIC (exp
)
612 ? "#<primitive-generic "
613 : "#<primitive-procedure ",
615 scm_puts (scm_i_symbol_chars (SCM_SNAME (exp
)), port
);
616 scm_putc ('>', port
);
621 SCM proc
= SCM_CCLO_SUBR (exp
);
622 if (scm_is_eq (proc
, scm_f_gsubr_apply
))
624 /* Print gsubrs as primitives */
625 SCM name
= scm_procedure_name (exp
);
626 scm_puts ("#<primitive-procedure", port
);
627 if (scm_is_true (name
))
629 scm_putc (' ', port
);
630 scm_puts (scm_i_symbol_chars (name
), port
);
635 scm_puts ("#<compiled-closure ", port
);
636 scm_iprin1 (proc
, port
, pstate
);
638 scm_putc ('>', port
);
643 scm_puts ("#<procedure-with-setter", port
);
645 SCM name
= scm_procedure_name (exp
);
646 if (scm_is_true (name
))
648 scm_putc (' ', port
);
649 scm_display (name
, port
);
652 scm_putc ('>', port
);
656 register long i
= SCM_PTOBNUM (exp
);
658 && scm_ptobs
[i
].print
659 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
664 ENTER_NESTED_DATA (pstate
, exp
, circref
);
665 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
666 EXIT_NESTED_DATA (pstate
);
670 scm_ipruk ("type", exp
, port
);
675 /* Print states are necessary for circular reference safe printing.
676 * They are also expensive to allocate. Therefore print states are
677 * kept in a pool so that they can be reused.
680 /* The PORT argument can also be a print-state/port pair, which will
681 * then be used instead of allocating a new print state. This is
682 * useful for continuing a chain of print calls from Scheme. */
685 scm_prin1 (SCM exp
, SCM port
, int writingp
)
687 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
689 scm_print_state
*pstate
;
692 /* If PORT is a print-state/port pair, use that. Else create a new
695 if (SCM_PORT_WITH_PS_P (port
))
697 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
698 port
= SCM_PORT_WITH_PS_PORT (port
);
702 /* First try to allocate a print state from the pool */
703 scm_i_pthread_mutex_lock (&print_state_mutex
);
704 if (!scm_is_null (print_state_pool
))
706 handle
= print_state_pool
;
707 print_state_pool
= SCM_CDR (print_state_pool
);
709 scm_i_pthread_mutex_unlock (&print_state_mutex
);
710 if (scm_is_false (handle
))
711 handle
= scm_list_1 (make_print_state ());
712 pstate_scm
= SCM_CAR (handle
);
715 pstate
= SCM_PRINT_STATE (pstate_scm
);
716 old_writingp
= pstate
->writingp
;
717 pstate
->writingp
= writingp
;
718 scm_iprin1 (exp
, port
, pstate
);
719 pstate
->writingp
= old_writingp
;
721 /* Return print state to pool if it has been created above and
722 hasn't escaped to Scheme. */
724 if (scm_is_true (handle
) && !pstate
->revealed
)
726 scm_i_pthread_mutex_lock (&print_state_mutex
);
727 SCM_SETCDR (handle
, print_state_pool
);
728 print_state_pool
= handle
;
729 scm_i_pthread_mutex_unlock (&print_state_mutex
);
738 scm_intprint (scm_t_intmax n
, int radix
, SCM port
)
740 char num_buf
[SCM_INTBUFLEN
];
741 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
745 scm_uintprint (scm_t_uintmax n
, int radix
, SCM port
)
747 char num_buf
[SCM_INTBUFLEN
];
748 scm_lfwrite (num_buf
, scm_iuint2str (n
, radix
, num_buf
), port
);
751 /* Print an object of unrecognized type.
755 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
757 scm_puts ("#<unknown-", port
);
758 scm_puts (hdr
, port
);
759 if (scm_in_heap_p (ptr
))
761 scm_puts (" (0x", port
);
762 scm_uintprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
763 scm_puts (" . 0x", port
);
764 scm_uintprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
765 scm_puts (") @", port
);
767 scm_puts (" 0x", port
);
768 scm_uintprint (SCM_UNPACK (ptr
), 16, port
);
769 scm_putc ('>', port
);
776 scm_iprlist (char *hdr
, SCM exp
, int tlr
, SCM port
, scm_print_state
*pstate
)
778 register SCM hare
, tortoise
;
779 long floor
= pstate
->top
- 2;
780 scm_puts (hdr
, port
);
785 /* Run a hare and tortoise so that total time complexity will be
786 O(depth * N) instead of O(N^2). */
787 hare
= SCM_CDR (exp
);
789 while (scm_is_pair (hare
))
791 if (scm_is_eq (hare
, tortoise
))
793 hare
= SCM_CDR (hare
);
794 if (!scm_is_pair (hare
))
796 hare
= SCM_CDR (hare
);
797 tortoise
= SCM_CDR (tortoise
);
800 /* No cdr cycles intrinsic to this list */
801 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
802 for (exp
= SCM_CDR (exp
); scm_is_pair (exp
); exp
= SCM_CDR (exp
))
806 for (i
= floor
; i
>= 0; --i
)
807 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
809 PUSH_REF (pstate
, exp
);
810 scm_putc (' ', port
);
812 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
814 if (!SCM_NULL_OR_NIL_P (exp
))
816 scm_puts (" . ", port
);
817 scm_iprin1 (exp
, port
, pstate
);
821 scm_putc (tlr
, port
);
822 pstate
->top
= floor
+ 2;
827 long n
= pstate
->length
;
829 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
830 exp
= SCM_CDR (exp
); --n
;
831 for (; scm_is_pair (exp
); exp
= SCM_CDR (exp
))
833 register unsigned long i
;
835 for (i
= 0; i
< pstate
->top
; ++i
)
836 if (scm_is_eq (PSTATE_STACK_REF(pstate
, i
), exp
))
842 scm_puts (" ...", port
);
848 PUSH_REF(pstate
, exp
);
849 ++pstate
->list_offset
;
850 scm_putc (' ', port
);
852 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
855 if (!SCM_NULL_OR_NIL_P (exp
))
857 scm_puts (" . ", port
);
858 scm_iprin1 (exp
, port
, pstate
);
861 pstate
->list_offset
-= pstate
->top
- floor
- 2;
865 pstate
->list_offset
-= pstate
->top
- floor
- 2;
868 scm_puts (" . ", port
);
869 print_circref (port
, pstate
, exp
);
876 scm_valid_oport_value_p (SCM val
)
878 return (SCM_OPOUTPORTP (val
)
879 || (SCM_PORT_WITH_PS_P (val
)
880 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
883 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
886 scm_write (SCM obj
, SCM port
)
888 if (SCM_UNBNDP (port
))
889 port
= scm_current_output_port ();
891 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
893 scm_prin1 (obj
, port
, 1);
897 scm_close_port (port
);
900 return SCM_UNSPECIFIED
;
904 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
907 scm_display (SCM obj
, SCM port
)
909 if (SCM_UNBNDP (port
))
910 port
= scm_current_output_port ();
912 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
914 scm_prin1 (obj
, port
, 0);
918 scm_close_port (port
);
921 return SCM_UNSPECIFIED
;
925 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
926 (SCM destination
, SCM message
, SCM args
),
927 "Write @var{message} to @var{destination}, defaulting to\n"
928 "the current output port.\n"
929 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
930 "@code{~S} (was @code{%S}) escapes. When printed,\n"
931 "the escapes are replaced with corresponding members of\n"
933 "@code{~A} formats using @code{display} and @code{~S} formats\n"
934 "using @code{write}.\n"
935 "If @var{destination} is @code{#t}, then use the current output\n"
936 "port, if @var{destination} is @code{#f}, then return a string\n"
937 "containing the formatted text. Does not add a trailing newline.")
938 #define FUNC_NAME s_scm_simple_format
940 SCM port
, answer
= SCM_UNSPECIFIED
;
941 int fReturnString
= 0;
947 if (scm_is_eq (destination
, SCM_BOOL_T
))
949 destination
= port
= scm_current_output_port ();
951 else if (scm_is_false (destination
))
954 port
= scm_mkstrport (SCM_INUM0
,
955 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
962 SCM_VALIDATE_OPORT_VALUE (1, destination
);
963 port
= SCM_COERCE_OUTPORT (destination
);
965 SCM_VALIDATE_STRING (2, message
);
966 SCM_VALIDATE_REST_ARGUMENT (args
);
968 start
= scm_i_string_chars (message
);
969 end
= start
+ scm_i_string_length (message
);
970 for (p
= start
; p
!= end
; ++p
)
985 scm_lfwrite (start
, p
- start
, port
);
989 scm_lfwrite (start
, p
- start
- 1, port
);
994 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
995 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1000 if (!scm_is_pair (args
))
1001 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1002 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1004 scm_lfwrite (start
, p
- start
- 1, port
);
1005 /* we pass destination here */
1006 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1007 args
= SCM_CDR (args
);
1011 scm_lfwrite (start
, p
- start
, port
);
1012 if (!scm_is_eq (args
, SCM_EOL
))
1013 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1014 scm_list_1 (scm_length (args
)));
1017 answer
= scm_strport_to_string (destination
);
1019 return scm_return_first (answer
, message
);
1024 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1026 "Send a newline to @var{port}.\n"
1027 "If @var{port} is omitted, send to the current output port.")
1028 #define FUNC_NAME s_scm_newline
1030 if (SCM_UNBNDP (port
))
1031 port
= scm_current_output_port ();
1033 SCM_VALIDATE_OPORT_VALUE (1, port
);
1035 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1036 return SCM_UNSPECIFIED
;
1040 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1041 (SCM chr
, SCM port
),
1042 "Send character @var{chr} to @var{port}.")
1043 #define FUNC_NAME s_scm_write_char
1045 if (SCM_UNBNDP (port
))
1046 port
= scm_current_output_port ();
1048 SCM_VALIDATE_CHAR (1, chr
);
1049 SCM_VALIDATE_OPORT_VALUE (2, port
);
1051 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1055 scm_close_port (port
);
1058 return SCM_UNSPECIFIED
;
1064 /* Call back to Scheme code to do the printing of special objects
1065 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1066 * containing PORT and PSTATE. This object can be used as the port for
1067 * display/write etc to continue the current print chain. The REVEALED
1068 * field of PSTATE is set to true to indicate that the print state has
1069 * escaped to Scheme and thus has to be freed by the GC.
1072 scm_t_bits scm_tc16_port_with_ps
;
1074 /* Print exactly as the port itself would */
1077 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1079 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1080 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1084 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1086 pstate
->revealed
= 1;
1087 return scm_call_2 (proc
, exp
,
1088 scm_i_port_with_print_state (port
, pstate
->handle
));
1091 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 1, 1, 0,
1092 (SCM port
, SCM pstate
),
1093 "Create a new port which behaves like @var{port}, but with an\n"
1094 "included print state @var{pstate}. @var{pstate} is optional.\n"
1095 "If @var{pstate} isn't supplied and @var{port} already has\n"
1096 "a print state, the old print state is reused.")
1097 #define FUNC_NAME s_scm_port_with_print_state
1099 SCM_VALIDATE_OPORT_VALUE (1, port
);
1100 if (!SCM_UNBNDP (pstate
))
1101 SCM_VALIDATE_PRINTSTATE (2, pstate
);
1102 return scm_i_port_with_print_state (port
, pstate
);
1106 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1108 "Return the print state of the port @var{port}. If @var{port}\n"
1109 "has no associated print state, @code{#f} is returned.")
1110 #define FUNC_NAME s_scm_get_print_state
1112 if (SCM_PORT_WITH_PS_P (port
))
1113 return SCM_PORT_WITH_PS_PS (port
);
1114 if (SCM_OUTPUT_PORT_P (port
))
1116 SCM_WRONG_TYPE_ARG (1, port
);
1125 SCM vtable
, layout
, type
;
1127 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1129 scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"),
1130 scm_from_locale_string ("{"),
1131 scm_from_locale_symbol ("highlight-suffix"),
1132 scm_from_locale_string ("}")));
1134 scm_gc_register_root (&print_state_pool
);
1135 scm_gc_register_root (&scm_print_state_vtable
);
1136 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1138 scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT
));
1139 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1140 scm_set_struct_vtable_name_x (type
, scm_from_locale_symbol ("print-state"));
1141 scm_print_state_vtable
= type
;
1143 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1144 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1145 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1146 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1148 #include "libguile/print.x"