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