1 /* Copyright (C) 1995-1999,2000,2001, 2002, 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
43 #include "libguile/validate.h"
44 #include "libguile/print.h"
47 /* {Names of immediate symbols}
49 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
52 /* This table must agree with the list of flags in tags.h. */
53 static const char *iflagnames
[] =
62 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
65 /* Elisp nil value. This is its Scheme name; whenever it's printed in
66 * Elisp, it should appear as the symbol `nil'. */
70 /* This table must agree with the list of SCM_IM_ constants in tags.h */
71 char *scm_isymnames
[] =
73 /* Short instructions */
90 /* Long instructions */
94 "#@call-with-current-continuation",
100 "#@call-with-values",
104 /* Multi-language support */
109 scm_t_option scm_print_opts
[] = {
110 { SCM_OPTION_SCM
, "closure-hook", SCM_UNPACK (SCM_BOOL_F
),
111 "Hook for printing closures (should handle macros as well)." },
112 { SCM_OPTION_BOOLEAN
, "source", 0,
113 "Print closures with source." }
116 SCM_DEFINE (scm_print_options
, "print-options-interface", 0, 1, 0,
118 "Option interface for the print options. Instead of using\n"
119 "this procedure directly, use the procedures\n"
120 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
121 "and @code{print-options}.")
122 #define FUNC_NAME s_scm_print_options
124 SCM ans
= scm_options (setting
,
133 /* {Printing of Scheme Objects}
136 /* Detection of circular references.
138 * Due to other constraints in the implementation, this code has bad
139 * time complexity (O (depth * N)), The printer code can be
140 * rewritten to be O(N).
142 #define PUSH_REF(pstate, obj) \
144 pstate->ref_stack[pstate->top++] = (obj); \
145 if (pstate->top == pstate->ceiling) \
146 grow_ref_stack (pstate); \
149 #define ENTER_NESTED_DATA(pstate, obj, label) \
151 register unsigned long i; \
152 for (i = 0; i < pstate->top; ++i) \
153 if (SCM_EQ_P (pstate->ref_stack[i], (obj))) \
155 if (pstate->fancyp) \
157 if (pstate->top - pstate->list_offset >= pstate->level) \
159 scm_putc ('#', port); \
163 PUSH_REF(pstate, obj); \
166 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
168 SCM scm_print_state_vtable
= SCM_BOOL_F
;
169 static SCM print_state_pool
= SCM_EOL
;
170 SCM_MUTEX (print_state_mutex
);
172 #ifdef GUILE_DEBUG /* Used for debugging purposes */
174 SCM_DEFINE (scm_current_pstate
, "current-pstate", 0, 0, 0,
176 "Return the current-pstate -- the car of the\n"
177 "@code{print_state_pool}. @code{current-pstate} is only\n"
178 "included in @code{--enable-guile-debug} builds.")
179 #define FUNC_NAME s_scm_current_pstate
181 if (!SCM_NULLP (print_state_pool
))
182 return SCM_CAR (print_state_pool
);
190 #define PSTATE_SIZE 50L
193 make_print_state (void)
196 = scm_make_struct (scm_print_state_vtable
, SCM_INUM0
, SCM_EOL
);
197 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
198 pstate
->ref_vect
= scm_c_make_vector (PSTATE_SIZE
, SCM_UNDEFINED
);
199 pstate
->ref_stack
= SCM_WRITABLE_VELTS (pstate
->ref_vect
);
200 pstate
->ceiling
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
205 scm_make_print_state ()
207 SCM answer
= SCM_BOOL_F
;
209 /* First try to allocate a print state from the pool */
210 scm_i_plugin_mutex_lock (&print_state_mutex
);
211 if (!SCM_NULLP (print_state_pool
))
213 answer
= SCM_CAR (print_state_pool
);
214 print_state_pool
= SCM_CDR (print_state_pool
);
216 scm_i_plugin_mutex_unlock (&print_state_mutex
);
218 return SCM_FALSEP (answer
) ? make_print_state () : answer
;
222 scm_free_print_state (SCM print_state
)
225 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
226 /* Cleanup before returning print state to pool.
227 * It is better to do it here. Doing it in scm_prin1
228 * would cost more since that function is called much more
232 pstate
->revealed
= 0;
233 scm_i_plugin_mutex_lock (&print_state_mutex
);
234 handle
= scm_cons (print_state
, print_state_pool
);
235 print_state_pool
= handle
;
236 scm_i_plugin_mutex_unlock (&print_state_mutex
);
240 scm_i_port_with_print_state (SCM port
, SCM print_state
)
242 if (SCM_UNBNDP (print_state
))
244 if (SCM_PORT_WITH_PS_P (port
))
247 print_state
= scm_make_print_state ();
248 /* port does not need to be coerced since it doesn't have ps */
251 port
= SCM_COERCE_OUTPORT (port
);
252 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps
,
253 SCM_UNPACK (scm_cons (port
, print_state
)));
257 grow_ref_stack (scm_print_state
*pstate
)
259 unsigned long int old_size
= SCM_VECTOR_LENGTH (pstate
->ref_vect
);
260 SCM
const *old_elts
= SCM_VELTS (pstate
->ref_vect
);
261 unsigned long int new_size
= 2 * pstate
->ceiling
;
262 SCM new_vect
= scm_c_make_vector (new_size
, SCM_UNDEFINED
);
265 for (i
= 0; i
!= old_size
; ++i
)
266 SCM_VECTOR_SET (new_vect
, i
, old_elts
[i
]);
268 pstate
->ref_vect
= new_vect
;
269 pstate
->ref_stack
= SCM_WRITABLE_VELTS(new_vect
);
270 pstate
->ceiling
= new_size
;
275 print_circref (SCM port
, scm_print_state
*pstate
, SCM ref
)
278 long self
= pstate
->top
- 1;
280 if (SCM_CONSP (pstate
->ref_stack
[i
]))
284 if (!SCM_CONSP (pstate
->ref_stack
[i
- 1])
285 || !SCM_EQ_P (SCM_CDR (pstate
->ref_stack
[i
- 1]),
286 pstate
->ref_stack
[i
]))
292 for (i
= pstate
->top
- 1; 1; --i
)
293 if (SCM_EQ_P (pstate
->ref_stack
[i
], ref
))
295 scm_putc ('#', port
);
296 scm_intprint (i
- self
, 10, port
);
297 scm_putc ('#', port
);
300 /* Print the name of a symbol. */
303 scm_print_symbol_name (const char *str
, size_t len
, SCM port
)
305 /* This points to the first character that has not yet been written to the
308 /* This points to the character we're currently looking at. */
310 /* If the name contains weird characters, we'll escape them with
311 * backslashes and set this flag; it indicates that we should surround the
312 * name with "#{" and "}#". */
314 /* Backslashes are not sufficient to make a name weird, but if a name is
315 * weird because of other characters, backslahes need to be escaped too.
316 * The first time we see a backslash, we set maybe_weird, and mw_pos points
317 * to the backslash. Then if the name turns out to be weird, we re-process
318 * everything starting from mw_pos. */
321 /* If the name is purely numeric, then it's weird as a whole, even though
322 * none of the individual characters is weird. But we won't know this
323 * until we reach the end of the name. This flag describes the part of the
324 * name we've looked at so far. */
327 if (len
== 0 || str
[0] == '\'' || str
[0] == ':' || str
[len
-1] == ':')
329 scm_lfwrite ("#{", 2, port
);
333 for (end
= pos
; end
< len
; ++end
)
336 #ifdef BRACKETS_AS_PARENS
345 case SCM_WHITE_SPACES
:
346 case SCM_LINE_INCREMENTORS
:
356 scm_lfwrite ("#{", 2, port
);
360 scm_lfwrite (str
+ pos
, end
- pos
, port
);
365 scm_lfwrite (buf
, 2, port
);
379 case '0': case '1': case '2': case '3': case '4':
380 case '5': case '6': case '7': case '8': case '9':
388 scm_lfwrite ("#{", 2, port
);
392 scm_lfwrite (str
+ pos
, end
- pos
, port
);
394 scm_lfwrite ("}#", 2, port
);
397 /* Print generally. Handles both write and display according to PSTATE.
399 SCM_GPROC(s_write
, "write", 1, 1, 0, scm_write
, g_write
);
400 SCM_GPROC(s_display
, "display", 1, 1, 0, scm_display
, g_display
);
403 scm_iprin1 (SCM exp
, SCM port
, scm_print_state
*pstate
)
405 switch (SCM_ITAG3 (exp
))
407 case scm_tc3_closure
:
410 /* These tc3 tags should never occur in an immediate value. They are
411 * only used in cell types of non-immediates, i. e. the value returned
412 * by SCM_CELL_TYPE (exp) can use these tags.
414 scm_ipruk ("immediate", exp
, port
);
418 scm_intprint (SCM_INUM (exp
), 10, port
);
423 long i
= SCM_CHAR (exp
);
425 if (SCM_WRITINGP (pstate
))
427 scm_puts ("#\\", port
);
428 if ((i
>= 0) && (i
<= ' ') && scm_charnames
[i
])
429 scm_puts (scm_charnames
[i
], port
);
431 else if (i
== '\177')
432 scm_puts (scm_charnames
[scm_n_charnames
- 1], port
);
434 else if (i
< 0 || i
> '\177')
435 scm_intprint (i
, 8, port
);
442 else if (SCM_IFLAGP (exp
)
443 && ((size_t) SCM_IFLAGNUM (exp
) < (sizeof iflagnames
/ sizeof (char *))))
445 scm_puts (iflagnames
[SCM_IFLAGNUM (exp
)], port
);
447 else if (SCM_ISYMP (exp
)
448 && ((size_t) SCM_ISYMNUM (exp
) < (sizeof scm_isymnames
/ sizeof (char *))))
450 scm_puts (SCM_ISYMCHARS (exp
), port
);
452 else if (SCM_ILOCP (exp
))
454 scm_puts ("#@", port
);
455 scm_intprint ((long) SCM_IFRAME (exp
), 10, port
);
456 scm_putc (SCM_ICDRP (exp
) ? '-' : '+', port
);
457 scm_intprint ((long) SCM_IDIST (exp
), 10, port
);
461 /* unknown immediate value */
462 scm_ipruk ("immediate", exp
, port
);
466 switch (SCM_TYP7 (exp
))
470 ENTER_NESTED_DATA (pstate
, exp
, circref
);
471 if (SCM_OBJ_CLASS_FLAGS (exp
) & SCM_CLASSF_GOOPS
)
473 SCM pwps
, print
= pstate
->writingp
? g_write
: g_display
;
476 pwps
= scm_i_port_with_print_state (port
, pstate
->handle
);
477 pstate
->revealed
= 1;
478 scm_call_generic_2 (print
, exp
, pwps
);
483 scm_print_struct (exp
, port
, pstate
);
485 EXIT_NESTED_DATA (pstate
);
488 case scm_tcs_cons_imcar
:
489 case scm_tcs_cons_nimcar
:
490 ENTER_NESTED_DATA (pstate
, exp
, circref
);
491 scm_iprlist ("(", exp
, ')', port
, pstate
);
492 EXIT_NESTED_DATA (pstate
);
495 print_circref (port
, pstate
, exp
);
497 case scm_tcs_closures
:
498 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE
))
499 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE
,
502 SCM formals
= SCM_CLOSURE_FORMALS (exp
);
503 scm_puts ("#<procedure", port
);
504 scm_putc (' ', port
);
505 scm_iprin1 (scm_procedure_name (exp
), port
, pstate
);
506 scm_putc (' ', port
);
507 if (SCM_PRINT_SOURCE_P
)
509 SCM env
= SCM_ENV (exp
);
510 SCM xenv
= SCM_EXTEND_ENV (formals
, SCM_EOL
, env
);
511 SCM src
= scm_unmemocopy (SCM_CODE (exp
), xenv
);
512 ENTER_NESTED_DATA (pstate
, exp
, circref
);
513 scm_iprin1 (src
, port
, pstate
);
514 EXIT_NESTED_DATA (pstate
);
517 scm_iprin1 (formals
, port
, pstate
);
518 scm_putc ('>', port
);
522 switch SCM_TYP16 (exp
) {
524 scm_bigprint (exp
, port
, pstate
);
527 scm_print_real (exp
, port
, pstate
);
529 case scm_tc16_complex
:
530 scm_print_complex (exp
, port
, pstate
);
532 case scm_tc16_fraction
:
533 scm_i_print_fraction (exp
, port
, pstate
);
538 if (SCM_WRITINGP (pstate
))
542 scm_putc ('"', port
);
543 for (i
= 0; i
< SCM_STRING_LENGTH (exp
); ++i
)
544 switch (SCM_STRING_CHARS (exp
)[i
])
548 scm_putc ('\\', port
);
550 scm_putc (SCM_STRING_CHARS (exp
)[i
], port
);
552 scm_putc ('"', port
);
556 scm_lfwrite (SCM_STRING_CHARS (exp
), SCM_STRING_LENGTH (exp
), port
);
559 if (SCM_SYMBOL_INTERNED_P (exp
))
561 scm_print_symbol_name (SCM_SYMBOL_CHARS (exp
),
562 SCM_SYMBOL_LENGTH (exp
),
564 scm_remember_upto_here_1 (exp
);
568 scm_puts ("#<uninterned-symbol ", port
);
569 scm_print_symbol_name (SCM_SYMBOL_CHARS (exp
),
570 SCM_SYMBOL_LENGTH (exp
),
572 scm_putc (' ', port
);
573 scm_intprint ((long)exp
, 16, port
);
574 scm_putc ('>', port
);
577 case scm_tc7_variable
:
578 scm_i_variable_print (exp
, port
, pstate
);
581 ENTER_NESTED_DATA (pstate
, exp
, circref
);
582 if (SCM_IS_WHVEC (exp
))
583 scm_puts ("#wh(", port
);
585 scm_puts ("#w(", port
);
586 goto common_vector_printer
;
589 ENTER_NESTED_DATA (pstate
, exp
, circref
);
590 scm_puts ("#(", port
);
591 common_vector_printer
:
594 long last
= SCM_VECTOR_LENGTH (exp
) - 1;
596 if (pstate
->fancyp
&& SCM_VECTOR_LENGTH (exp
) > pstate
->length
)
598 last
= pstate
->length
- 1;
601 for (i
= 0; i
< last
; ++i
)
604 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
605 scm_putc (' ', port
);
610 scm_iprin1 (SCM_VELTS (exp
)[i
], port
, pstate
);
613 scm_puts (" ...", port
);
614 scm_putc (')', port
);
616 EXIT_NESTED_DATA (pstate
);
627 #if SCM_SIZEOF_LONG_LONG != 0
630 scm_raprin1 (exp
, port
, pstate
);
634 scm_puts (SCM_SUBR_GENERIC (exp
)
635 ? "#<primitive-generic "
636 : "#<primitive-procedure ",
638 scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp
)), port
);
639 scm_putc ('>', port
);
644 SCM proc
= SCM_CCLO_SUBR (exp
);
645 if (SCM_EQ_P (proc
, scm_f_gsubr_apply
))
647 /* Print gsubrs as primitives */
648 SCM name
= scm_procedure_name (exp
);
649 scm_puts ("#<primitive-procedure", port
);
650 if (!SCM_FALSEP (name
))
652 scm_putc (' ', port
);
653 scm_puts (SCM_SYMBOL_CHARS (name
), port
);
658 scm_puts ("#<compiled-closure ", port
);
659 scm_iprin1 (proc
, port
, pstate
);
661 scm_putc ('>', port
);
666 scm_puts ("#<procedure-with-setter", port
);
668 SCM name
= scm_procedure_name (exp
);
669 if (!SCM_FALSEP (name
))
671 scm_putc (' ', port
);
672 scm_display (name
, port
);
675 scm_putc ('>', port
);
679 register long i
= SCM_PTOBNUM (exp
);
681 && scm_ptobs
[i
].print
682 && (scm_ptobs
[i
].print
) (exp
, port
, pstate
))
687 ENTER_NESTED_DATA (pstate
, exp
, circref
);
688 SCM_SMOB_DESCRIPTOR (exp
).print (exp
, port
, pstate
);
689 EXIT_NESTED_DATA (pstate
);
693 scm_ipruk ("type", exp
, port
);
698 /* Print states are necessary for circular reference safe printing.
699 * They are also expensive to allocate. Therefore print states are
700 * kept in a pool so that they can be reused.
703 /* The PORT argument can also be a print-state/port pair, which will
704 * then be used instead of allocating a new print state. This is
705 * useful for continuing a chain of print calls from Scheme. */
708 scm_prin1 (SCM exp
, SCM port
, int writingp
)
710 SCM handle
= SCM_BOOL_F
; /* Will GC protect the handle whilst unlinked */
712 scm_print_state
*pstate
;
715 /* If PORT is a print-state/port pair, use that. Else create a new
718 if (SCM_PORT_WITH_PS_P (port
))
720 pstate_scm
= SCM_PORT_WITH_PS_PS (port
);
721 port
= SCM_PORT_WITH_PS_PORT (port
);
725 /* First try to allocate a print state from the pool */
726 scm_i_plugin_mutex_lock (&print_state_mutex
);
727 if (!SCM_NULLP (print_state_pool
))
729 handle
= print_state_pool
;
730 print_state_pool
= SCM_CDR (print_state_pool
);
732 scm_i_plugin_mutex_unlock (&print_state_mutex
);
733 if (SCM_FALSEP (handle
))
734 handle
= scm_list_1 (make_print_state ());
735 pstate_scm
= SCM_CAR (handle
);
738 pstate
= SCM_PRINT_STATE (pstate_scm
);
739 old_writingp
= pstate
->writingp
;
740 pstate
->writingp
= writingp
;
741 scm_iprin1 (exp
, port
, pstate
);
742 pstate
->writingp
= old_writingp
;
744 /* Return print state to pool if it has been created above and
745 hasn't escaped to Scheme. */
747 if (!SCM_FALSEP (handle
) && !pstate
->revealed
)
749 scm_i_plugin_mutex_lock (&print_state_mutex
);
750 SCM_SETCDR (handle
, print_state_pool
);
751 print_state_pool
= handle
;
752 scm_i_plugin_mutex_unlock (&print_state_mutex
);
761 scm_intprint (long n
, int radix
, SCM port
)
763 char num_buf
[SCM_INTBUFLEN
];
764 scm_lfwrite (num_buf
, scm_iint2str (n
, radix
, num_buf
), port
);
767 /* Print an object of unrecognized type.
771 scm_ipruk (char *hdr
, SCM ptr
, SCM port
)
773 scm_puts ("#<unknown-", port
);
774 scm_puts (hdr
, port
);
775 if (scm_in_heap_p (ptr
))
777 scm_puts (" (0x", port
);
778 scm_intprint (SCM_CELL_WORD_0 (ptr
), 16, port
);
779 scm_puts (" . 0x", port
);
780 scm_intprint (SCM_CELL_WORD_1 (ptr
), 16, port
);
781 scm_puts (") @", port
);
783 scm_puts (" 0x", port
);
784 scm_intprint (SCM_UNPACK (ptr
), 16, port
);
785 scm_putc ('>', port
);
792 scm_iprlist (char *hdr
, SCM exp
, int tlr
, SCM port
, scm_print_state
*pstate
)
794 register SCM hare
, tortoise
;
795 long floor
= pstate
->top
- 2;
796 scm_puts (hdr
, port
);
801 /* Run a hare and tortoise so that total time complexity will be
802 O(depth * N) instead of O(N^2). */
803 hare
= SCM_CDR (exp
);
805 while (SCM_CONSP (hare
))
807 if (SCM_EQ_P (hare
, tortoise
))
809 hare
= SCM_CDR (hare
);
810 if (!SCM_CONSP (hare
))
812 hare
= SCM_CDR (hare
);
813 tortoise
= SCM_CDR (tortoise
);
816 /* No cdr cycles intrinsic to this list */
817 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
818 for (exp
= SCM_CDR (exp
); SCM_CONSP (exp
); exp
= SCM_CDR (exp
))
822 for (i
= floor
; i
>= 0; --i
)
823 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
825 PUSH_REF (pstate
, exp
);
826 scm_putc (' ', port
);
828 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
830 if (!SCM_NULL_OR_NIL_P (exp
))
832 scm_puts (" . ", port
);
833 scm_iprin1 (exp
, port
, pstate
);
837 scm_putc (tlr
, port
);
838 pstate
->top
= floor
+ 2;
843 long n
= pstate
->length
;
845 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
846 exp
= SCM_CDR (exp
); --n
;
847 for (; SCM_CONSP (exp
); exp
= SCM_CDR (exp
))
849 register unsigned long i
;
851 for (i
= 0; i
< pstate
->top
; ++i
)
852 if (SCM_EQ_P (pstate
->ref_stack
[i
], exp
))
858 scm_puts (" ...", port
);
864 PUSH_REF(pstate
, exp
);
865 ++pstate
->list_offset
;
866 scm_putc (' ', port
);
868 scm_iprin1 (SCM_CAR (exp
), port
, pstate
);
871 if (!SCM_NULL_OR_NIL_P (exp
))
873 scm_puts (" . ", port
);
874 scm_iprin1 (exp
, port
, pstate
);
877 pstate
->list_offset
-= pstate
->top
- floor
- 2;
881 pstate
->list_offset
-= pstate
->top
- floor
- 2;
884 scm_puts (" . ", port
);
885 print_circref (port
, pstate
, exp
);
892 scm_valid_oport_value_p (SCM val
)
894 return (SCM_OPOUTPORTP (val
)
895 || (SCM_PORT_WITH_PS_P (val
)
896 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val
))));
899 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
902 scm_write (SCM obj
, SCM port
)
904 if (SCM_UNBNDP (port
))
907 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_write
);
909 scm_prin1 (obj
, port
, 1);
913 scm_close_port (port
);
916 return SCM_UNSPECIFIED
;
920 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
923 scm_display (SCM obj
, SCM port
)
925 if (SCM_UNBNDP (port
))
928 SCM_ASSERT (scm_valid_oport_value_p (port
), port
, SCM_ARG2
, s_display
);
930 scm_prin1 (obj
, port
, 0);
934 scm_close_port (port
);
937 return SCM_UNSPECIFIED
;
941 SCM_DEFINE (scm_simple_format
, "simple-format", 2, 0, 1,
942 (SCM destination
, SCM message
, SCM args
),
943 "Write @var{message} to @var{destination}, defaulting to\n"
944 "the current output port.\n"
945 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
946 "@code{~S} (was @code{%S}) escapes. When printed,\n"
947 "the escapes are replaced with corresponding members of\n"
949 "@code{~A} formats using @code{display} and @code{~S} formats\n"
950 "using @code{write}.\n"
951 "If @var{destination} is @code{#t}, then use the current output\n"
952 "port, if @var{destination} is @code{#f}, then return a string\n"
953 "containing the formatted text. Does not add a trailing newline.")
954 #define FUNC_NAME s_scm_simple_format
956 SCM port
, answer
= SCM_UNSPECIFIED
;
957 int fReturnString
= 0;
963 if (SCM_EQ_P (destination
, SCM_BOOL_T
))
965 destination
= port
= scm_cur_outp
;
967 else if (SCM_FALSEP (destination
))
970 port
= scm_mkstrport (SCM_INUM0
,
971 scm_make_string (SCM_INUM0
, SCM_UNDEFINED
),
978 SCM_VALIDATE_OPORT_VALUE (1, destination
);
979 port
= SCM_COERCE_OUTPORT (destination
);
981 SCM_VALIDATE_STRING (2, message
);
982 SCM_VALIDATE_REST_ARGUMENT (args
);
984 start
= SCM_STRING_CHARS (message
);
985 end
= start
+ SCM_STRING_LENGTH (message
);
986 for (p
= start
; p
!= end
; ++p
)
1001 scm_lfwrite (start
, p
- start
, port
);
1005 scm_lfwrite (start
, p
- start
- 1, port
);
1010 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
1011 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1016 if (!SCM_CONSP (args
))
1017 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1018 scm_list_1 (SCM_MAKE_CHAR (*p
)));
1020 scm_lfwrite (start
, p
- start
- 1, port
);
1021 /* we pass destination here */
1022 scm_prin1 (SCM_CAR (args
), destination
, writingp
);
1023 args
= SCM_CDR (args
);
1027 scm_lfwrite (start
, p
- start
, port
);
1028 if (!SCM_EQ_P (args
, SCM_EOL
))
1029 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1030 scm_list_1 (scm_length (args
)));
1033 answer
= scm_strport_to_string (destination
);
1035 return scm_return_first (answer
, message
);
1040 SCM_DEFINE (scm_newline
, "newline", 0, 1, 0,
1042 "Send a newline to @var{port}.\n"
1043 "If @var{port} is omitted, send to the current output port.")
1044 #define FUNC_NAME s_scm_newline
1046 if (SCM_UNBNDP (port
))
1047 port
= scm_cur_outp
;
1049 SCM_VALIDATE_OPORT_VALUE (1, port
);
1051 scm_putc ('\n', SCM_COERCE_OUTPORT (port
));
1052 return SCM_UNSPECIFIED
;
1056 SCM_DEFINE (scm_write_char
, "write-char", 1, 1, 0,
1057 (SCM chr
, SCM port
),
1058 "Send character @var{chr} to @var{port}.")
1059 #define FUNC_NAME s_scm_write_char
1061 if (SCM_UNBNDP (port
))
1062 port
= scm_cur_outp
;
1064 SCM_VALIDATE_CHAR (1, chr
);
1065 SCM_VALIDATE_OPORT_VALUE (2, port
);
1067 scm_putc ((int) SCM_CHAR (chr
), SCM_COERCE_OUTPORT (port
));
1071 scm_close_port (port
);
1074 return SCM_UNSPECIFIED
;
1080 /* Call back to Scheme code to do the printing of special objects
1081 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1082 * containing PORT and PSTATE. This object can be used as the port for
1083 * display/write etc to continue the current print chain. The REVEALED
1084 * field of PSTATE is set to true to indicate that the print state has
1085 * escaped to Scheme and thus has to be freed by the GC.
1088 scm_t_bits scm_tc16_port_with_ps
;
1090 /* Print exactly as the port itself would */
1093 port_with_ps_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
1095 obj
= SCM_PORT_WITH_PS_PORT (obj
);
1096 return scm_ptobs
[SCM_PTOBNUM (obj
)].print (obj
, port
, pstate
);
1100 scm_printer_apply (SCM proc
, SCM exp
, SCM port
, scm_print_state
*pstate
)
1102 pstate
->revealed
= 1;
1103 return scm_call_2 (proc
, exp
,
1104 scm_i_port_with_print_state (port
, pstate
->handle
));
1107 SCM_DEFINE (scm_port_with_print_state
, "port-with-print-state", 1, 1, 0,
1108 (SCM port
, SCM pstate
),
1109 "Create a new port which behaves like @var{port}, but with an\n"
1110 "included print state @var{pstate}. @var{pstate} is optional.\n"
1111 "If @var{pstate} isn't supplied and @var{port} already has\n"
1112 "a print state, the old print state is reused.")
1113 #define FUNC_NAME s_scm_port_with_print_state
1115 SCM_VALIDATE_OPORT_VALUE (1, port
);
1116 if (!SCM_UNBNDP (pstate
))
1117 SCM_VALIDATE_PRINTSTATE (2, pstate
);
1118 return scm_i_port_with_print_state (port
, pstate
);
1122 SCM_DEFINE (scm_get_print_state
, "get-print-state", 1, 0, 0,
1124 "Return the print state of the port @var{port}. If @var{port}\n"
1125 "has no associated print state, @code{#f} is returned.")
1126 #define FUNC_NAME s_scm_get_print_state
1128 if (SCM_PORT_WITH_PS_P (port
))
1129 return SCM_PORT_WITH_PS_PS (port
);
1130 if (SCM_OUTPUT_PORT_P (port
))
1132 SCM_WRONG_TYPE_ARG (1, port
);
1141 SCM vtable
, layout
, type
;
1143 scm_init_opts (scm_print_options
, scm_print_opts
, SCM_N_PRINT_OPTIONS
);
1145 scm_gc_register_root (&print_state_pool
);
1146 scm_gc_register_root (&scm_print_state_vtable
);
1147 vtable
= scm_make_vtable_vtable (scm_nullstr
, SCM_INUM0
, SCM_EOL
);
1148 layout
= scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT
));
1149 type
= scm_make_struct (vtable
, SCM_INUM0
, scm_list_1 (layout
));
1150 scm_set_struct_vtable_name_x (type
, scm_str2symbol ("print-state"));
1151 scm_print_state_vtable
= type
;
1153 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1154 scm_tc16_port_with_ps
= scm_make_smob_type (0, 0);
1155 scm_set_smob_mark (scm_tc16_port_with_ps
, scm_markcdr
);
1156 scm_set_smob_print (scm_tc16_port_with_ps
, port_with_ps_print
);
1158 #include "libguile/print.x"