* backtrace.c, debug.c, debug.h, deprecation.c, eq.c, eval.c
[bpt/guile.git] / libguile / print.c
CommitLineData
2b829bbb 1/* Copyright (C) 1995-1999,2000,2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
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.
0f2d19dd 7 *
73be1d9e
MV
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.
0f2d19dd 12 *
73be1d9e
MV
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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd
JB
19\f
20
e6e2e95a
MD
21#include <errno.h>
22
a0599745
MD
23#include "libguile/_scm.h"
24#include "libguile/chars.h"
a002f1a2 25#include "libguile/continuations.h"
a0599745
MD
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"
c96d76b8 41#include "libguile/lang.h"
327967ef 42#include "libguile/numbers.h"
a0599745
MD
43
44#include "libguile/validate.h"
45#include "libguile/print.h"
22fc179a
HWN
46
47#include "libguile/private-options.h"
48
0f2d19dd
JB
49\f
50
51/* {Names of immediate symbols}
52 *
53 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
54 */
55
e17d318f
DH
56/* This table must agree with the list of flags in tags.h. */
57static const char *iflagnames[] =
58{
59 "#f",
60 "#t",
61 "#<undefined>",
62 "#<eof>",
63 "()",
64 "#<unspecified>",
65
66 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
67 "#<unbound>",
68
69 /* Elisp nil value. This is its Scheme name; whenever it's printed in
70 * Elisp, it should appear as the symbol `nil'. */
71 "#nil"
72};
73
475fa9a5
MV
74SCM_SYMBOL (sym_reader, "reader");
75
92c2555f 76scm_t_option scm_print_opts[] = {
726d810a
DH
77 { SCM_OPTION_SCM, "closure-hook", SCM_UNPACK (SCM_BOOL_F),
78 "Hook for printing closures (should handle macros as well)." },
84f6a34a 79 { SCM_OPTION_BOOLEAN, "source", 0,
81ae25da
MV
80 "Print closures with source." },
81 { SCM_OPTION_SCM, "highlight-prefix", (unsigned long)SCM_BOOL_F,
82 "The string to print before highlighted values." },
83 { SCM_OPTION_SCM, "highlight-suffix", (unsigned long)SCM_BOOL_F,
475fa9a5
MV
84 "The string to print after highlighted values." },
85 { SCM_OPTION_SCM, "quote-keywordish-symbols", (unsigned long)SCM_BOOL_F,
86 "How to print symbols that have a colon as their first or last character. "
87 "The value '#f' does not quote the colons; '#t' quotes them; "
88 "'reader' quotes them when the reader option 'keywords' is not '#f'."
62560650
HWN
89 },
90 { 0 },
91
e6e4c9af
MD
92};
93
a1ec6916 94SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0,
1bbd0b84 95 (SCM setting),
71331188 96 "Option interface for the print options. Instead of using\n"
1dd05fd8
MG
97 "this procedure directly, use the procedures\n"
98 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
99 "and @code{print-options}.")
1bbd0b84 100#define FUNC_NAME s_scm_print_options
e6e4c9af 101{
a51ea417 102 SCM ans = scm_options (setting,
b7ff98dd 103 scm_print_opts,
1bbd0b84 104 FUNC_NAME);
e6e4c9af
MD
105 return ans;
106}
1bbd0b84 107#undef FUNC_NAME
e6e4c9af 108
0f2d19dd
JB
109\f
110/* {Printing of Scheme Objects}
111 */
112
a51ea417 113/* Detection of circular references.
c62fbfe1
MD
114 *
115 * Due to other constraints in the implementation, this code has bad
5d46ebe3
MD
116 * time complexity (O (depth * N)), The printer code can be
117 * rewritten to be O(N).
a51ea417 118 */
dbb5de29
NJ
119#define PUSH_REF(pstate, obj) \
120do \
121{ \
122 PSTATE_STACK_SET (pstate, pstate->top, obj); \
123 pstate->top++; \
124 if (pstate->top == pstate->ceiling) \
125 grow_ref_stack (pstate); \
1bbd0b84 126} while(0)
a51ea417 127
dbb5de29
NJ
128#define ENTER_NESTED_DATA(pstate, obj, label) \
129do \
130{ \
131 register unsigned long i; \
132 for (i = 0; i < pstate->top; ++i) \
133 if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
134 goto label; \
135 if (pstate->fancyp) \
136 { \
137 if (pstate->top - pstate->list_offset >= pstate->level) \
138 { \
139 scm_putc ('#', port); \
140 return; \
141 } \
142 } \
143 PUSH_REF(pstate, obj); \
1bbd0b84 144} while(0)
a51ea417 145
dbb5de29
NJ
146#define EXIT_NESTED_DATA(pstate) \
147do \
148{ \
149 --pstate->top; \
150 PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
151} \
152while (0)
c62fbfe1 153
d5cf5324
DH
154SCM scm_print_state_vtable = SCM_BOOL_F;
155static SCM print_state_pool = SCM_EOL;
9de87eea 156scm_i_pthread_mutex_t print_state_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
c4f37e80 157
f843a84c 158#ifdef GUILE_DEBUG /* Used for debugging purposes */
1cc91f1b 159
3b3b36dd 160SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0,
1bbd0b84 161 (),
d5cf5324 162 "Return the current-pstate -- the car of the\n"
5352393c
MG
163 "@code{print_state_pool}. @code{current-pstate} is only\n"
164 "included in @code{--enable-guile-debug} builds.")
1bbd0b84 165#define FUNC_NAME s_scm_current_pstate
c62fbfe1 166{
d2e53ed6 167 if (!scm_is_null (print_state_pool))
d5cf5324 168 return SCM_CAR (print_state_pool);
a0adfbf0 169 else
0a284a4e 170 return SCM_BOOL_F;
c62fbfe1 171}
1bbd0b84
GB
172#undef FUNC_NAME
173
c62fbfe1
MD
174#endif
175
176#define PSTATE_SIZE 50L
177
698c0295 178static SCM
1bbd0b84 179make_print_state (void)
698c0295 180{
d5cf5324
DH
181 SCM print_state
182 = scm_make_struct (scm_print_state_vtable, SCM_INUM0, SCM_EOL);
bf685b6d 183 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
00ffa0e7 184 pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED);
4057a3e0 185 pstate->ceiling = SCM_SIMPLE_VECTOR_LENGTH (pstate->ref_vect);
d232520a 186 pstate->highlight_objects = SCM_EOL;
698c0295
MD
187 return print_state;
188}
1cc91f1b 189
c62fbfe1
MD
190SCM
191scm_make_print_state ()
c62fbfe1 192{
230d095f 193 SCM answer = SCM_BOOL_F;
698c0295
MD
194
195 /* First try to allocate a print state from the pool */
9de87eea 196 scm_i_pthread_mutex_lock (&print_state_mutex);
d2e53ed6 197 if (!scm_is_null (print_state_pool))
698c0295 198 {
d5cf5324
DH
199 answer = SCM_CAR (print_state_pool);
200 print_state_pool = SCM_CDR (print_state_pool);
698c0295 201 }
9de87eea 202 scm_i_pthread_mutex_unlock (&print_state_mutex);
698c0295 203
7888309b 204 return scm_is_false (answer) ? make_print_state () : answer;
c62fbfe1 205}
a51ea417 206
698c0295 207void
6e8d25a6 208scm_free_print_state (SCM print_state)
698c0295
MD
209{
210 SCM handle;
211 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
212 /* Cleanup before returning print state to pool.
213 * It is better to do it here. Doing it in scm_prin1
214 * would cost more since that function is called much more
215 * often.
216 */
217 pstate->fancyp = 0;
bb35f315 218 pstate->revealed = 0;
d232520a 219 pstate->highlight_objects = SCM_EOL;
9de87eea 220 scm_i_pthread_mutex_lock (&print_state_mutex);
16d4699b 221 handle = scm_cons (print_state, print_state_pool);
d5cf5324 222 print_state_pool = handle;
9de87eea 223 scm_i_pthread_mutex_unlock (&print_state_mutex);
dfd03fb9
MD
224}
225
226SCM
227scm_i_port_with_print_state (SCM port, SCM print_state)
228{
229 if (SCM_UNBNDP (print_state))
230 {
231 if (SCM_PORT_WITH_PS_P (port))
232 return port;
233 else
234 print_state = scm_make_print_state ();
235 /* port does not need to be coerced since it doesn't have ps */
236 }
237 else
238 port = SCM_COERCE_OUTPORT (port);
239 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps,
240 SCM_UNPACK (scm_cons (port, print_state)));
698c0295 241}
1cc91f1b 242
a51ea417 243static void
1bbd0b84 244grow_ref_stack (scm_print_state *pstate)
a51ea417 245{
4057a3e0
MV
246 SCM old_vect = pstate->ref_vect;
247 size_t old_size = SCM_SIMPLE_VECTOR_LENGTH (old_vect);
248 size_t new_size = 2 * pstate->ceiling;
00ffa0e7 249 SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED);
b17004b8
DH
250 unsigned long int i;
251
252 for (i = 0; i != old_size; ++i)
4057a3e0 253 SCM_SIMPLE_VECTOR_SET (new_vect, i, SCM_SIMPLE_VECTOR_REF (old_vect, i));
b17004b8
DH
254
255 pstate->ref_vect = new_vect;
bf685b6d 256 pstate->ceiling = new_size;
a51ea417
MD
257}
258
509759dd
MV
259#define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
260#define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
1cc91f1b 261
a51ea417 262static void
34d19ef6 263print_circref (SCM port, scm_print_state *pstate, SCM ref)
a51ea417 264{
c014a02e
ML
265 register long i;
266 long self = pstate->top - 1;
c62fbfe1 267 i = pstate->top - 1;
509759dd 268 if (scm_is_pair (PSTATE_STACK_REF (pstate, i)))
c62fbfe1
MD
269 {
270 while (i > 0)
271 {
509759dd
MV
272 if (!scm_is_pair (PSTATE_STACK_REF (pstate, i-1))
273 || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate, i-1)),
274 SCM_CDR (PSTATE_STACK_REF (pstate, i))))
c62fbfe1
MD
275 break;
276 --i;
277 }
278 self = i;
279 }
280 for (i = pstate->top - 1; 1; --i)
509759dd 281 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), ref))
c62fbfe1 282 break;
b7f3516f 283 scm_putc ('#', port);
c62fbfe1 284 scm_intprint (i - self, 10, port);
b7f3516f 285 scm_putc ('#', port);
a51ea417
MD
286}
287
6662998f
MV
288/* Print the name of a symbol. */
289
475fa9a5
MV
290static int
291quote_keywordish_symbol (const char *str, size_t len)
292{
293 SCM option;
294
295 /* LEN is guaranteed to be > 0.
296 */
297 if (str[0] != ':' && str[len-1] != ':')
298 return 0;
299
300 option = SCM_PRINT_KEYWORD_STYLE;
301 if (scm_is_false (option))
302 return 0;
303 if (scm_is_eq (option, sym_reader))
304 return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE));
305 return 1;
306}
307
6662998f
MV
308void
309scm_print_symbol_name (const char *str, size_t len, SCM port)
310{
c6b49e89
MV
311 /* This points to the first character that has not yet been written to the
312 * port. */
313 size_t pos = 0;
314 /* This points to the character we're currently looking at. */
6662998f 315 size_t end;
c6b49e89
MV
316 /* If the name contains weird characters, we'll escape them with
317 * backslashes and set this flag; it indicates that we should surround the
318 * name with "#{" and "}#". */
319 int weird = 0;
320 /* Backslashes are not sufficient to make a name weird, but if a name is
321 * weird because of other characters, backslahes need to be escaped too.
322 * The first time we see a backslash, we set maybe_weird, and mw_pos points
323 * to the backslash. Then if the name turns out to be weird, we re-process
327967ef
MV
324 * everything starting from mw_pos.
325 * We could instead make backslashes always weird. This is not necessary
326 * to ensure that the output is (read)-able, but it would make this code
327 * simpler and faster. */
c6b49e89 328 int maybe_weird = 0;
6662998f 329 size_t mw_pos = 0;
6662998f 330
475fa9a5
MV
331 if (len == 0 || str[0] == '\'' || str[0] == '`' || str[0] == ','
332 || quote_keywordish_symbol (str, len)
333 || (str[0] == '.' && len == 1)
2b829bbb 334 || scm_is_true (scm_c_locale_stringn_to_number (str, len, 10)))
6662998f
MV
335 {
336 scm_lfwrite ("#{", 2, port);
337 weird = 1;
338 }
c6b49e89 339
6662998f
MV
340 for (end = pos; end < len; ++end)
341 switch (str[end])
342 {
343#ifdef BRACKETS_AS_PARENS
344 case '[':
345 case ']':
346#endif
347 case '(':
348 case ')':
349 case '"':
350 case ';':
c6b49e89 351 case '#':
6662998f
MV
352 case SCM_WHITE_SPACES:
353 case SCM_LINE_INCREMENTORS:
354 weird_handler:
355 if (maybe_weird)
356 {
357 end = mw_pos;
358 maybe_weird = 0;
359 }
360 if (!weird)
361 {
362 scm_lfwrite ("#{", 2, port);
363 weird = 1;
364 }
365 if (pos < end)
c6b49e89 366 scm_lfwrite (str + pos, end - pos, port);
6662998f
MV
367 {
368 char buf[2];
369 buf[0] = '\\';
370 buf[1] = str[end];
371 scm_lfwrite (buf, 2, port);
372 }
373 pos = end + 1;
374 break;
375 case '\\':
376 if (weird)
377 goto weird_handler;
378 if (!maybe_weird)
379 {
380 maybe_weird = 1;
381 mw_pos = pos;
382 }
383 break;
6662998f
MV
384 default:
385 break;
386 }
387 if (pos < end)
388 scm_lfwrite (str + pos, end - pos, port);
389 if (weird)
390 scm_lfwrite ("}#", 2, port);
391}
392
c62fbfe1 393/* Print generally. Handles both write and display according to PSTATE.
0f2d19dd 394 */
8b840115
MD
395SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write);
396SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
1cc91f1b 397
d232520a
MV
398static void iprin1 (SCM exp, SCM port, scm_print_state *pstate);
399
0f2d19dd 400void
1bbd0b84 401scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate)
d232520a
MV
402{
403 if (pstate->fancyp
404 && scm_is_true (scm_memq (exp, pstate->highlight_objects)))
405 {
81ae25da 406 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX, port);
d232520a 407 iprin1 (exp, port, pstate);
81ae25da 408 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX, port);
d232520a
MV
409 }
410 else
411 iprin1 (exp, port, pstate);
412}
413
414static void
415iprin1 (SCM exp, SCM port, scm_print_state *pstate)
0f2d19dd 416{
54778cd3 417 switch (SCM_ITAG3 (exp))
0f2d19dd 418 {
e34f941a
DH
419 case scm_tc3_closure:
420 case scm_tc3_tc7_1:
421 case scm_tc3_tc7_2:
422 /* These tc3 tags should never occur in an immediate value. They are
423 * only used in cell types of non-immediates, i. e. the value returned
424 * by SCM_CELL_TYPE (exp) can use these tags.
425 */
426 scm_ipruk ("immediate", exp, port);
427 break;
428 case scm_tc3_int_1:
429 case scm_tc3_int_2:
e11e83f3 430 scm_intprint (SCM_I_INUM (exp), 10, port);
0f2d19dd 431 break;
e34f941a 432 case scm_tc3_imm24:
7866a09b 433 if (SCM_CHARP (exp))
0f2d19dd 434 {
e34f941a 435 long i = SCM_CHAR (exp);
5ca6dc39 436
b7f3516f
TT
437 if (SCM_WRITINGP (pstate))
438 {
439 scm_puts ("#\\", port);
440 if ((i >= 0) && (i <= ' ') && scm_charnames[i])
441 scm_puts (scm_charnames[i], port);
57b74422
GH
442#ifndef EBCDIC
443 else if (i == '\177')
444 scm_puts (scm_charnames[scm_n_charnames - 1], port);
445#endif
b7f3516f
TT
446 else if (i < 0 || i > '\177')
447 scm_intprint (i, 8, port);
448 else
449 scm_putc (i, port);
450 }
451 else
452 scm_putc (i, port);
0f2d19dd 453 }
a51ea417 454 else if (SCM_IFLAGP (exp)
e17d318f
DH
455 && ((size_t) SCM_IFLAGNUM (exp) < (sizeof iflagnames / sizeof (char *))))
456 {
457 scm_puts (iflagnames [SCM_IFLAGNUM (exp)], port);
458 }
7e6e6b37 459 else if (SCM_ISYMP (exp))
e17d318f 460 {
7e6e6b37 461 scm_i_print_isym (exp, port);
e17d318f 462 }
0f2d19dd
JB
463 else if (SCM_ILOCP (exp))
464 {
7e6e6b37 465 scm_i_print_iloc (exp, port);
0f2d19dd
JB
466 }
467 else
e34f941a
DH
468 {
469 /* unknown immediate value */
470 scm_ipruk ("immediate", exp, port);
471 }
0f2d19dd 472 break;
e34f941a 473 case scm_tc3_cons:
0f2d19dd
JB
474 switch (SCM_TYP7 (exp))
475 {
904a077d
MV
476 case scm_tcs_struct:
477 {
478 ENTER_NESTED_DATA (pstate, exp, circref);
479 if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS)
480 {
481 SCM pwps, print = pstate->writingp ? g_write : g_display;
482 if (!print)
483 goto print_struct;
dfd03fb9 484 pwps = scm_i_port_with_print_state (port, pstate->handle);
7663c008 485 pstate->revealed = 1;
904a077d
MV
486 scm_call_generic_2 (print, exp, pwps);
487 }
488 else
489 {
490 print_struct:
491 scm_print_struct (exp, port, pstate);
492 }
493 EXIT_NESTED_DATA (pstate);
494 }
495 break;
0f2d19dd
JB
496 case scm_tcs_cons_imcar:
497 case scm_tcs_cons_nimcar:
c62fbfe1
MD
498 ENTER_NESTED_DATA (pstate, exp, circref);
499 scm_iprlist ("(", exp, ')', port, pstate);
500 EXIT_NESTED_DATA (pstate);
a51ea417
MD
501 break;
502 circref:
c62fbfe1 503 print_circref (port, pstate, exp);
0f2d19dd
JB
504 break;
505 case scm_tcs_closures:
7888309b
MV
506 if (scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE))
507 || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE,
77c25af7 508 exp, port, pstate)))
726d810a
DH
509 {
510 SCM formals = SCM_CLOSURE_FORMALS (exp);
511 scm_puts ("#<procedure", port);
512 scm_putc (' ', port);
513 scm_iprin1 (scm_procedure_name (exp), port, pstate);
514 scm_putc (' ', port);
515 if (SCM_PRINT_SOURCE_P)
516 {
517 SCM env = SCM_ENV (exp);
518 SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env);
9fcf3cbb 519 SCM src = scm_i_unmemocopy_body (SCM_CODE (exp), xenv);
726d810a
DH
520 ENTER_NESTED_DATA (pstate, exp, circref);
521 scm_iprin1 (src, port, pstate);
522 EXIT_NESTED_DATA (pstate);
523 }
524 else
525 scm_iprin1 (formals, port, pstate);
b7f3516f 526 scm_putc ('>', port);
726d810a 527 }
0f2d19dd 528 break;
534c55a9
DH
529 case scm_tc7_number:
530 switch SCM_TYP16 (exp) {
531 case scm_tc16_big:
532 scm_bigprint (exp, port, pstate);
533 break;
534 case scm_tc16_real:
535 scm_print_real (exp, port, pstate);
536 break;
537 case scm_tc16_complex:
538 scm_print_complex (exp, port, pstate);
539 break;
f92e85f7
MV
540 case scm_tc16_fraction:
541 scm_i_print_fraction (exp, port, pstate);
542 break;
534c55a9
DH
543 }
544 break;
0f2d19dd 545 case scm_tc7_string:
c62fbfe1 546 if (SCM_WRITINGP (pstate))
0f2d19dd 547 {
6d92f14f 548 size_t i, j, len;
cc95e00a 549 const char *data;
5ca6dc39 550
b7f3516f 551 scm_putc ('"', port);
cc95e00a
MV
552 len = scm_i_string_length (exp);
553 data = scm_i_string_chars (exp);
6d92f14f 554 for (i = 0, j = 0; i < len; ++i)
fea8e142 555 {
cc95e00a 556 unsigned char ch = data[i];
fea8e142
MV
557 if ((ch < 32 && ch != '\n') || (127 <= ch && ch < 148))
558 {
559 static char const hex[]="0123456789abcdef";
6d92f14f
MV
560 char buf[4];
561
562 scm_lfwrite (data+j, i-j, port);
563 buf[0] = '\\';
564 buf[1] = 'x';
565 buf[2] = hex [ch / 16];
566 buf[3] = hex [ch % 16];
567 scm_lfwrite (buf, 4, port);
568 data = scm_i_string_chars (exp);
569 j = i+1;
fea8e142 570 }
6d92f14f 571 else if (ch == '"' || ch == '\\')
fea8e142 572 {
6d92f14f
MV
573 scm_lfwrite (data+j, i-j, port);
574 scm_putc ('\\', port);
575 data = scm_i_string_chars (exp);
576 j = i;
fea8e142
MV
577 }
578 }
6d92f14f 579 scm_lfwrite (data+j, i-j, port);
b7f3516f 580 scm_putc ('"', port);
cc95e00a 581 scm_remember_upto_here_1 (exp);
0f2d19dd
JB
582 }
583 else
cc95e00a 584 scm_lfwrite (scm_i_string_chars (exp), scm_i_string_length (exp),
fea8e142 585 port);
8824ac88 586 scm_remember_upto_here_1 (exp);
0f2d19dd 587 break;
28b06554 588 case scm_tc7_symbol:
cc95e00a 589 if (scm_i_symbol_is_interned (exp))
9ff28a13 590 {
cc95e00a
MV
591 scm_print_symbol_name (scm_i_symbol_chars (exp),
592 scm_i_symbol_length (exp),
9ff28a13
MV
593 port);
594 scm_remember_upto_here_1 (exp);
595 }
596 else
597 {
598 scm_puts ("#<uninterned-symbol ", port);
cc95e00a
MV
599 scm_print_symbol_name (scm_i_symbol_chars (exp),
600 scm_i_symbol_length (exp),
9ff28a13
MV
601 port);
602 scm_putc (' ', port);
0345e278 603 scm_uintprint (SCM_UNPACK (exp), 16, port);
9ff28a13
MV
604 scm_putc ('>', port);
605 }
6662998f 606 break;
e5aca4b5
MV
607 case scm_tc7_variable:
608 scm_i_variable_print (exp, port, pstate);
609 break;
0f2d19dd 610 case scm_tc7_wvect:
c62fbfe1 611 ENTER_NESTED_DATA (pstate, exp, circref);
0f2d19dd 612 if (SCM_IS_WHVEC (exp))
b7f3516f 613 scm_puts ("#wh(", port);
0f2d19dd 614 else
b7f3516f 615 scm_puts ("#w(", port);
0f2d19dd
JB
616 goto common_vector_printer;
617
618 case scm_tc7_vector:
c62fbfe1 619 ENTER_NESTED_DATA (pstate, exp, circref);
b7f3516f 620 scm_puts ("#(", port);
0f2d19dd 621 common_vector_printer:
9fbaf27c 622 {
c014a02e 623 register long i;
4057a3e0 624 long last = SCM_SIMPLE_VECTOR_LENGTH (exp) - 1;
9fbaf27c 625 int cutp = 0;
4057a3e0
MV
626 if (pstate->fancyp
627 && SCM_SIMPLE_VECTOR_LENGTH (exp) > pstate->length)
9fbaf27c
MD
628 {
629 last = pstate->length - 1;
630 cutp = 1;
631 }
632 for (i = 0; i < last; ++i)
633 {
634 /* CHECK_INTS; */
4057a3e0 635 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp, i), port, pstate);
b7f3516f 636 scm_putc (' ', port);
9fbaf27c
MD
637 }
638 if (i == last)
639 {
640 /* CHECK_INTS; */
4057a3e0 641 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp, i), port, pstate);
9fbaf27c
MD
642 }
643 if (cutp)
b7f3516f
TT
644 scm_puts (" ...", port);
645 scm_putc (')', port);
9fbaf27c 646 }
c62fbfe1 647 EXIT_NESTED_DATA (pstate);
0f2d19dd 648 break;
0f2d19dd 649 case scm_tcs_subrs:
f76af603 650 scm_puts (SCM_SUBR_GENERIC (exp)
9de33deb
MD
651 ? "#<primitive-generic "
652 : "#<primitive-procedure ",
653 port);
cc95e00a 654 scm_puts (scm_i_symbol_chars (SCM_SNAME (exp)), port);
b7f3516f 655 scm_putc ('>', port);
0f2d19dd
JB
656 break;
657#ifdef CCLO
658 case scm_tc7_cclo:
56977059
MD
659 {
660 SCM proc = SCM_CCLO_SUBR (exp);
bc36d050 661 if (scm_is_eq (proc, scm_f_gsubr_apply))
56977059
MD
662 {
663 /* Print gsubrs as primitives */
c180dfd3 664 SCM name = scm_procedure_name (exp);
56977059 665 scm_puts ("#<primitive-procedure", port);
7888309b 666 if (scm_is_true (name))
56977059
MD
667 {
668 scm_putc (' ', port);
cc95e00a 669 scm_puts (scm_i_symbol_chars (name), port);
56977059
MD
670 }
671 }
672 else
673 {
674 scm_puts ("#<compiled-closure ", port);
675 scm_iprin1 (proc, port, pstate);
676 }
677 scm_putc ('>', port);
678 }
0f2d19dd
JB
679 break;
680#endif
c180dfd3
MD
681 case scm_tc7_pws:
682 scm_puts ("#<procedure-with-setter", port);
683 {
684 SCM name = scm_procedure_name (exp);
7888309b 685 if (scm_is_true (name))
c180dfd3
MD
686 {
687 scm_putc (' ', port);
b24b5e13 688 scm_display (name, port);
c180dfd3
MD
689 }
690 }
691 scm_putc ('>', port);
692 break;
0f2d19dd 693 case scm_tc7_port:
5ca6dc39
JB
694 {
695 register long i = SCM_PTOBNUM (exp);
696 if (i < scm_numptob
697 && scm_ptobs[i].print
698 && (scm_ptobs[i].print) (exp, port, pstate))
a51ea417 699 break;
5ca6dc39
JB
700 goto punk;
701 }
702 case scm_tc7_smob:
7a7f7c53
DH
703 ENTER_NESTED_DATA (pstate, exp, circref);
704 SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate);
705 EXIT_NESTED_DATA (pstate);
706 break;
0f2d19dd 707 default:
a51ea417
MD
708 punk:
709 scm_ipruk ("type", exp, port);
0f2d19dd
JB
710 }
711 }
712}
713
c62fbfe1
MD
714/* Print states are necessary for circular reference safe printing.
715 * They are also expensive to allocate. Therefore print states are
716 * kept in a pool so that they can be reused.
717 */
1cc91f1b 718
bb35f315
MV
719/* The PORT argument can also be a print-state/port pair, which will
720 * then be used instead of allocating a new print state. This is
721 * useful for continuing a chain of print calls from Scheme. */
722
a51ea417 723void
1bbd0b84 724scm_prin1 (SCM exp, SCM port, int writingp)
a51ea417 725{
c4f37e80
MV
726 SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
727 SCM pstate_scm;
c62fbfe1 728 scm_print_state *pstate;
15635be5 729 int old_writingp;
c62fbfe1 730
bb35f315
MV
731 /* If PORT is a print-state/port pair, use that. Else create a new
732 print-state. */
c4f37e80 733
0c95b57d 734 if (SCM_PORT_WITH_PS_P (port))
bb35f315 735 {
52235e71
MD
736 pstate_scm = SCM_PORT_WITH_PS_PS (port);
737 port = SCM_PORT_WITH_PS_PORT (port);
bb35f315
MV
738 }
739 else
c62fbfe1 740 {
c4f37e80 741 /* First try to allocate a print state from the pool */
9de87eea 742 scm_i_pthread_mutex_lock (&print_state_mutex);
d2e53ed6 743 if (!scm_is_null (print_state_pool))
c4f37e80 744 {
d5cf5324
DH
745 handle = print_state_pool;
746 print_state_pool = SCM_CDR (print_state_pool);
c4f37e80 747 }
9de87eea 748 scm_i_pthread_mutex_unlock (&print_state_mutex);
7888309b 749 if (scm_is_false (handle))
d5cf5324 750 handle = scm_list_1 (make_print_state ());
c4f37e80 751 pstate_scm = SCM_CAR (handle);
c62fbfe1 752 }
c62fbfe1 753
c4f37e80 754 pstate = SCM_PRINT_STATE (pstate_scm);
15635be5 755 old_writingp = pstate->writingp;
c62fbfe1
MD
756 pstate->writingp = writingp;
757 scm_iprin1 (exp, port, pstate);
15635be5 758 pstate->writingp = old_writingp;
c62fbfe1 759
bb35f315
MV
760 /* Return print state to pool if it has been created above and
761 hasn't escaped to Scheme. */
762
7888309b 763 if (scm_is_true (handle) && !pstate->revealed)
c4f37e80 764 {
9de87eea 765 scm_i_pthread_mutex_lock (&print_state_mutex);
d5cf5324
DH
766 SCM_SETCDR (handle, print_state_pool);
767 print_state_pool = handle;
9de87eea 768 scm_i_pthread_mutex_unlock (&print_state_mutex);
c4f37e80 769 }
a51ea417
MD
770}
771
0f2d19dd
JB
772
773/* Print an integer.
774 */
1cc91f1b 775
0f2d19dd 776void
a406c9e9 777scm_intprint (scm_t_intmax n, int radix, SCM port)
0f2d19dd
JB
778{
779 char num_buf[SCM_INTBUFLEN];
b7f3516f 780 scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port);
0f2d19dd
JB
781}
782
a406c9e9
MV
783void
784scm_uintprint (scm_t_uintmax n, int radix, SCM port)
785{
786 char num_buf[SCM_INTBUFLEN];
787 scm_lfwrite (num_buf, scm_iuint2str (n, radix, num_buf), port);
788}
789
0f2d19dd
JB
790/* Print an object of unrecognized type.
791 */
1cc91f1b 792
0f2d19dd 793void
1bbd0b84 794scm_ipruk (char *hdr, SCM ptr, SCM port)
0f2d19dd 795{
b7f3516f
TT
796 scm_puts ("#<unknown-", port);
797 scm_puts (hdr, port);
c8a1bdc4 798 if (scm_in_heap_p (ptr))
0f2d19dd 799 {
b7f3516f 800 scm_puts (" (0x", port);
0345e278 801 scm_uintprint (SCM_CELL_WORD_0 (ptr), 16, port);
b7f3516f 802 scm_puts (" . 0x", port);
0345e278 803 scm_uintprint (SCM_CELL_WORD_1 (ptr), 16, port);
b7f3516f 804 scm_puts (") @", port);
0f2d19dd 805 }
b7f3516f 806 scm_puts (" 0x", port);
0345e278 807 scm_uintprint (SCM_UNPACK (ptr), 16, port);
b7f3516f 808 scm_putc ('>', port);
0f2d19dd
JB
809}
810
1cc91f1b 811
904a077d 812/* Print a list.
22a52da1 813 */
0f2d19dd 814void
34d19ef6 815scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate)
0f2d19dd 816{
c62fbfe1 817 register SCM hare, tortoise;
c014a02e 818 long floor = pstate->top - 2;
b7f3516f 819 scm_puts (hdr, port);
0f2d19dd 820 /* CHECK_INTS; */
c62fbfe1
MD
821 if (pstate->fancyp)
822 goto fancy_printing;
823
824 /* Run a hare and tortoise so that total time complexity will be
825 O(depth * N) instead of O(N^2). */
826 hare = SCM_CDR (exp);
827 tortoise = exp;
d2e53ed6 828 while (scm_is_pair (hare))
c62fbfe1 829 {
bc36d050 830 if (scm_is_eq (hare, tortoise))
c62fbfe1
MD
831 goto fancy_printing;
832 hare = SCM_CDR (hare);
d2e53ed6 833 if (!scm_is_pair (hare))
c62fbfe1
MD
834 break;
835 hare = SCM_CDR (hare);
836 tortoise = SCM_CDR (tortoise);
837 }
838
839 /* No cdr cycles intrinsic to this list */
840 scm_iprin1 (SCM_CAR (exp), port, pstate);
d2e53ed6 841 for (exp = SCM_CDR (exp); scm_is_pair (exp); exp = SCM_CDR (exp))
0f2d19dd 842 {
c014a02e 843 register long i;
5ca6dc39 844
c62fbfe1 845 for (i = floor; i >= 0; --i)
509759dd 846 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
c62fbfe1
MD
847 goto circref;
848 PUSH_REF (pstate, exp);
b7f3516f 849 scm_putc (' ', port);
0f2d19dd 850 /* CHECK_INTS; */
c62fbfe1 851 scm_iprin1 (SCM_CAR (exp), port, pstate);
0f2d19dd 852 }
c96d76b8 853 if (!SCM_NULL_OR_NIL_P (exp))
0f2d19dd 854 {
b7f3516f 855 scm_puts (" . ", port);
c62fbfe1 856 scm_iprin1 (exp, port, pstate);
0f2d19dd 857 }
c62fbfe1 858
a51ea417 859end:
b7f3516f 860 scm_putc (tlr, port);
c62fbfe1 861 pstate->top = floor + 2;
a51ea417 862 return;
c62fbfe1
MD
863
864fancy_printing:
865 {
c014a02e 866 long n = pstate->length;
c62fbfe1
MD
867
868 scm_iprin1 (SCM_CAR (exp), port, pstate);
869 exp = SCM_CDR (exp); --n;
d2e53ed6 870 for (; scm_is_pair (exp); exp = SCM_CDR (exp))
c62fbfe1 871 {
c014a02e 872 register unsigned long i;
5ca6dc39 873
c62fbfe1 874 for (i = 0; i < pstate->top; ++i)
509759dd 875 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
c62fbfe1
MD
876 goto fancy_circref;
877 if (pstate->fancyp)
878 {
879 if (n == 0)
880 {
b7f3516f 881 scm_puts (" ...", port);
c62fbfe1
MD
882 goto skip_tail;
883 }
884 else
885 --n;
886 }
887 PUSH_REF(pstate, exp);
888 ++pstate->list_offset;
b7f3516f 889 scm_putc (' ', port);
c62fbfe1
MD
890 /* CHECK_INTS; */
891 scm_iprin1 (SCM_CAR (exp), port, pstate);
892 }
893 }
c96d76b8 894 if (!SCM_NULL_OR_NIL_P (exp))
c62fbfe1 895 {
b7f3516f 896 scm_puts (" . ", port);
c62fbfe1
MD
897 scm_iprin1 (exp, port, pstate);
898 }
899skip_tail:
900 pstate->list_offset -= pstate->top - floor - 2;
a51ea417 901 goto end;
a51ea417 902
c62fbfe1
MD
903fancy_circref:
904 pstate->list_offset -= pstate->top - floor - 2;
905
906circref:
b7f3516f 907 scm_puts (" . ", port);
c62fbfe1
MD
908 print_circref (port, pstate, exp);
909 goto end;
0f2d19dd
JB
910}
911
912\f
913
bb35f315
MV
914int
915scm_valid_oport_value_p (SCM val)
916{
368cf54d
GB
917 return (SCM_OPOUTPORTP (val)
918 || (SCM_PORT_WITH_PS_P (val)
919 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val))));
bb35f315
MV
920}
921
8b840115 922/* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
1cc91f1b 923
0f2d19dd 924SCM
1bbd0b84 925scm_write (SCM obj, SCM port)
0f2d19dd
JB
926{
927 if (SCM_UNBNDP (port))
9de87eea 928 port = scm_current_output_port ();
3eb7e6ee
JB
929
930 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
bb35f315 931
a51ea417 932 scm_prin1 (obj, port, 1);
23d72566 933#if 0
0f2d19dd
JB
934#ifdef HAVE_PIPE
935# ifdef EPIPE
936 if (EPIPE == errno)
937 scm_close_port (port);
938# endif
23d72566 939#endif
0f2d19dd
JB
940#endif
941 return SCM_UNSPECIFIED;
942}
943
944
8b840115 945/* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
1cc91f1b 946
0f2d19dd 947SCM
1bbd0b84 948scm_display (SCM obj, SCM port)
0f2d19dd
JB
949{
950 if (SCM_UNBNDP (port))
9de87eea 951 port = scm_current_output_port ();
3eb7e6ee
JB
952
953 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
bb35f315 954
a51ea417 955 scm_prin1 (obj, port, 0);
23d72566 956#if 0
0f2d19dd
JB
957#ifdef HAVE_PIPE
958# ifdef EPIPE
959 if (EPIPE == errno)
960 scm_close_port (port);
961# endif
23d72566 962#endif
0f2d19dd
JB
963#endif
964 return SCM_UNSPECIFIED;
965}
966
70d63753
GB
967
968SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
969 (SCM destination, SCM message, SCM args),
eca65e90
MG
970 "Write @var{message} to @var{destination}, defaulting to\n"
971 "the current output port.\n"
972 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
973 "@code{~S} (was @code{%S}) escapes. When printed,\n"
974 "the escapes are replaced with corresponding members of\n"
975 "@var{ARGS}:\n"
976 "@code{~A} formats using @code{display} and @code{~S} formats\n"
977 "using @code{write}.\n"
978 "If @var{destination} is @code{#t}, then use the current output\n"
979 "port, if @var{destination} is @code{#f}, then return a string\n"
980 "containing the formatted text. Does not add a trailing newline.")
70d63753
GB
981#define FUNC_NAME s_scm_simple_format
982{
dfd03fb9 983 SCM port, answer = SCM_UNSPECIFIED;
70d63753
GB
984 int fReturnString = 0;
985 int writingp;
cc95e00a
MV
986 const char *start;
987 const char *end;
988 const char *p;
70d63753 989
bc36d050 990 if (scm_is_eq (destination, SCM_BOOL_T))
daba1a71 991 {
9de87eea 992 destination = port = scm_current_output_port ();
daba1a71 993 }
7888309b 994 else if (scm_is_false (destination))
daba1a71
MD
995 {
996 fReturnString = 1;
dfd03fb9
MD
997 port = scm_mkstrport (SCM_INUM0,
998 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
999 SCM_OPN | SCM_WRTNG,
1000 FUNC_NAME);
1001 destination = port;
daba1a71
MD
1002 }
1003 else
1004 {
1005 SCM_VALIDATE_OPORT_VALUE (1, destination);
dfd03fb9 1006 port = SCM_COERCE_OUTPORT (destination);
daba1a71
MD
1007 }
1008 SCM_VALIDATE_STRING (2, message);
af45e3b0 1009 SCM_VALIDATE_REST_ARGUMENT (args);
70d63753 1010
cc95e00a
MV
1011 start = scm_i_string_chars (message);
1012 end = start + scm_i_string_length (message);
b24b5e13 1013 for (p = start; p != end; ++p)
70d63753
GB
1014 if (*p == '~')
1015 {
b24b5e13 1016 if (++p == end)
6662998f
MV
1017 break;
1018
1019 switch (*p)
1020 {
1021 case 'A': case 'a':
1022 writingp = 0;
1023 break;
1024 case 'S': case 's':
1025 writingp = 1;
1026 break;
1027 case '~':
dfd03fb9 1028 scm_lfwrite (start, p - start, port);
6662998f
MV
1029 start = p + 1;
1030 continue;
1031 case '%':
dfd03fb9
MD
1032 scm_lfwrite (start, p - start - 1, port);
1033 scm_newline (port);
6662998f
MV
1034 start = p + 1;
1035 continue;
1036 default:
1afff620
KN
1037 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
1038 scm_list_1 (SCM_MAKE_CHAR (*p)));
6662998f
MV
1039
1040 }
70d63753 1041
6662998f 1042
d2e53ed6 1043 if (!scm_is_pair (args))
1afff620
KN
1044 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1045 scm_list_1 (SCM_MAKE_CHAR (*p)));
6662998f 1046
dfd03fb9
MD
1047 scm_lfwrite (start, p - start - 1, port);
1048 /* we pass destination here */
70d63753
GB
1049 scm_prin1 (SCM_CAR (args), destination, writingp);
1050 args = SCM_CDR (args);
1051 start = p + 1;
1052 }
6662998f 1053
dfd03fb9 1054 scm_lfwrite (start, p - start, port);
bc36d050 1055 if (!scm_is_eq (args, SCM_EOL))
1afff620
KN
1056 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1057 scm_list_1 (scm_length (args)));
70d63753
GB
1058
1059 if (fReturnString)
1060 answer = scm_strport_to_string (destination);
1061
daba1a71 1062 return scm_return_first (answer, message);
70d63753
GB
1063}
1064#undef FUNC_NAME
1065
1066
3b3b36dd 1067SCM_DEFINE (scm_newline, "newline", 0, 1, 0,
b450f070 1068 (SCM port),
8f85c0c6
NJ
1069 "Send a newline to @var{port}.\n"
1070 "If @var{port} is omitted, send to the current output port.")
1bbd0b84 1071#define FUNC_NAME s_scm_newline
0f2d19dd
JB
1072{
1073 if (SCM_UNBNDP (port))
9de87eea 1074 port = scm_current_output_port ();
3eb7e6ee 1075
34d19ef6 1076 SCM_VALIDATE_OPORT_VALUE (1, port);
bb35f315 1077
0ef4ae82 1078 scm_putc ('\n', SCM_COERCE_OUTPORT (port));
0f2d19dd
JB
1079 return SCM_UNSPECIFIED;
1080}
1bbd0b84 1081#undef FUNC_NAME
0f2d19dd 1082
3b3b36dd 1083SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
b450f070 1084 (SCM chr, SCM port),
eca65e90 1085 "Send character @var{chr} to @var{port}.")
1bbd0b84 1086#define FUNC_NAME s_scm_write_char
0f2d19dd
JB
1087{
1088 if (SCM_UNBNDP (port))
9de87eea 1089 port = scm_current_output_port ();
3eb7e6ee 1090
34d19ef6
HWN
1091 SCM_VALIDATE_CHAR (1, chr);
1092 SCM_VALIDATE_OPORT_VALUE (2, port);
bb35f315 1093
7866a09b 1094 scm_putc ((int) SCM_CHAR (chr), SCM_COERCE_OUTPORT (port));
23d72566 1095#if 0
0f2d19dd
JB
1096#ifdef HAVE_PIPE
1097# ifdef EPIPE
1098 if (EPIPE == errno)
1099 scm_close_port (port);
1100# endif
23d72566 1101#endif
0f2d19dd
JB
1102#endif
1103 return SCM_UNSPECIFIED;
1104}
1bbd0b84 1105#undef FUNC_NAME
0f2d19dd 1106
0f2d19dd
JB
1107\f
1108
bb35f315 1109/* Call back to Scheme code to do the printing of special objects
c19bc088
MD
1110 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1111 * containing PORT and PSTATE. This object can be used as the port for
1112 * display/write etc to continue the current print chain. The REVEALED
1113 * field of PSTATE is set to true to indicate that the print state has
1114 * escaped to Scheme and thus has to be freed by the GC.
1115 */
1116
92c2555f 1117scm_t_bits scm_tc16_port_with_ps;
c19bc088
MD
1118
1119/* Print exactly as the port itself would */
1120
1121static int
e841c3e0 1122port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate)
c19bc088
MD
1123{
1124 obj = SCM_PORT_WITH_PS_PORT (obj);
1125 return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate);
1126}
c4f37e80
MV
1127
1128SCM
1bbd0b84 1129scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate)
c4f37e80 1130{
bb35f315 1131 pstate->revealed = 1;
dfd03fb9
MD
1132 return scm_call_2 (proc, exp,
1133 scm_i_port_with_print_state (port, pstate->handle));
c19bc088
MD
1134}
1135
dfd03fb9 1136SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 1, 1, 0,
1bbd0b84 1137 (SCM port, SCM pstate),
71331188 1138 "Create a new port which behaves like @var{port}, but with an\n"
dfd03fb9
MD
1139 "included print state @var{pstate}. @var{pstate} is optional.\n"
1140 "If @var{pstate} isn't supplied and @var{port} already has\n"
1141 "a print state, the old print state is reused.")
1bbd0b84 1142#define FUNC_NAME s_scm_port_with_print_state
c19bc088 1143{
34d19ef6 1144 SCM_VALIDATE_OPORT_VALUE (1, port);
dfd03fb9
MD
1145 if (!SCM_UNBNDP (pstate))
1146 SCM_VALIDATE_PRINTSTATE (2, pstate);
1147 return scm_i_port_with_print_state (port, pstate);
c19bc088 1148}
1bbd0b84 1149#undef FUNC_NAME
c19bc088 1150
a1ec6916 1151SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0,
1bbd0b84 1152 (SCM port),
71331188
MG
1153 "Return the print state of the port @var{port}. If @var{port}\n"
1154 "has no associated print state, @code{#f} is returned.")
1bbd0b84 1155#define FUNC_NAME s_scm_get_print_state
c19bc088 1156{
368cf54d
GB
1157 if (SCM_PORT_WITH_PS_P (port))
1158 return SCM_PORT_WITH_PS_PS (port);
f5f2dcff 1159 if (SCM_OUTPUT_PORT_P (port))
368cf54d 1160 return SCM_BOOL_F;
276dd677 1161 SCM_WRONG_TYPE_ARG (1, port);
c4f37e80 1162}
1bbd0b84 1163#undef FUNC_NAME
bb35f315 1164
c4f37e80 1165\f
1cc91f1b 1166
0f2d19dd
JB
1167void
1168scm_init_print ()
0f2d19dd 1169{
c19bc088 1170 SCM vtable, layout, type;
d5cf5324 1171
62560650 1172 scm_init_opts (scm_print_options, scm_print_opts);
d5cf5324 1173
81ae25da
MV
1174 scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"),
1175 scm_from_locale_string ("{"),
1176 scm_from_locale_symbol ("highlight-suffix"),
1177 scm_from_locale_string ("}")));
1178
d5cf5324
DH
1179 scm_gc_register_root (&print_state_pool);
1180 scm_gc_register_root (&scm_print_state_vtable);
3ce4544c 1181 vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL);
cc95e00a
MV
1182 layout =
1183 scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT));
1afff620 1184 type = scm_make_struct (vtable, SCM_INUM0, scm_list_1 (layout));
cc95e00a 1185 scm_set_struct_vtable_name_x (type, scm_from_locale_symbol ("print-state"));
bb35f315 1186 scm_print_state_vtable = type;
c4f37e80 1187
c19bc088
MD
1188 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1189 scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
1190 scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr);
e841c3e0 1191 scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print);
81ae25da 1192
a0599745 1193#include "libguile/print.x"
475fa9a5
MV
1194
1195 scm_print_opts[SCM_PRINT_KEYWORD_STYLE_I].val = SCM_UNPACK (sym_reader);
0f2d19dd 1196}
89e00824
ML
1197
1198/*
1199 Local Variables:
1200 c-file-style: "gnu"
1201 End:
1202*/