*** empty log message ***
[bpt/guile.git] / libguile / print.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
0f2d19dd
JB
41\f
42
43#include <stdio.h>
44#include "_scm.h"
20e6290e
JB
45#include "chars.h"
46#include "genio.h"
20e6290e
JB
47#include "smob.h"
48#include "eval.h"
ddeae7ca 49#include "macros.h"
20e6290e
JB
50#include "procprop.h"
51#include "read.h"
52#include "weaks.h"
53#include "unif.h"
dbef8851 54#include "alist.h"
c62fbfe1 55#include "struct.h"
0f2d19dd 56
20e6290e 57#include "print.h"
0f2d19dd
JB
58\f
59
60/* {Names of immediate symbols}
61 *
62 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
63 */
64
65char *scm_isymnames[] =
66{
67 /* This table must agree with the declarations */
68 "#@and",
69 "#@begin",
70 "#@case",
71 "#@cond",
72 "#@do",
73 "#@if",
74 "#@lambda",
75 "#@let",
76 "#@let*",
77 "#@letrec",
78 "#@or",
79 "#@quote",
80 "#@set!",
81 "#@define",
82#if 0
83 "#@literal-variable-ref",
84 "#@literal-variable-set!",
85#endif
86 "#@apply",
87 "#@call-with-current-continuation",
88
89 /* user visible ISYMS */
90 /* other keywords */
91 /* Flags */
92
93 "#f",
94 "#t",
95 "#<undefined>",
96 "#<eof>",
97 "()",
4df91bd6
MD
98 "#<unspecified>",
99 "#@dispatch",
43c667e9
MD
100 "#@slot-ref",
101 "#@slot-set!",
159500fb 102
159500fb
MD
103 /* Multi-language support */
104
105 "#@nil-cond",
106 "#@nil-ify",
107 "#@t-ify",
108 "#@0-cond",
109 "#@0-ify",
110 "#@1-ify",
111 "#@bind"
0f2d19dd
JB
112};
113
e6e4c9af 114scm_option scm_print_opts[] = {
b7ff98dd 115 { SCM_OPTION_SCM, "closure-hook", SCM_BOOL_F,
84f6a34a
MD
116 "Hook for printing closures." },
117 { SCM_OPTION_BOOLEAN, "source", 0,
118 "Print closures with source." }
e6e4c9af
MD
119};
120
b7ff98dd 121SCM_PROC (s_print_options, "print-options-interface", 0, 1, 0, scm_print_options);
1cc91f1b 122
e6e4c9af 123SCM
a51ea417
MD
124scm_print_options (setting)
125 SCM setting;
e6e4c9af 126{
a51ea417 127 SCM ans = scm_options (setting,
b7ff98dd
MD
128 scm_print_opts,
129 SCM_N_PRINT_OPTIONS,
130 s_print_options);
e6e4c9af
MD
131 return ans;
132}
e6e4c9af 133
0f2d19dd
JB
134\f
135/* {Printing of Scheme Objects}
136 */
137
a51ea417 138/* Detection of circular references.
c62fbfe1
MD
139 *
140 * Due to other constraints in the implementation, this code has bad
141 * time complexity (O (depth * N)), The printer code will be
142 * completely rewritten before next release of Guile. The new code
143 * will be O(N).
a51ea417 144 */
c62fbfe1 145#define PUSH_REF(pstate, obj) \
a51ea417 146{ \
c62fbfe1
MD
147 pstate->ref_stack[pstate->top++] = (obj); \
148 if (pstate->top == pstate->ceiling) \
149 grow_ref_stack (pstate); \
150}
a51ea417 151
c62fbfe1 152#define ENTER_NESTED_DATA(pstate, obj, label) \
a51ea417 153{ \
5ca6dc39 154 register unsigned long i; \
c62fbfe1
MD
155 for (i = 0; i < pstate->top; ++i) \
156 if (pstate->ref_stack[i] == (obj)) \
157 goto label; \
158 if (pstate->fancyp) \
159 { \
160 if (pstate->top - pstate->list_offset >= pstate->level) \
161 { \
b7f3516f 162 scm_putc ('#', port); \
c62fbfe1
MD
163 return; \
164 } \
165 } \
166 PUSH_REF(pstate, obj); \
a51ea417
MD
167} \
168
c62fbfe1
MD
169#define EXIT_NESTED_DATA(pstate) { --pstate->top; }
170
bb35f315 171SCM scm_print_state_vtable;
c4f37e80 172
bb35f315 173static SCM print_state_pool;
c4f37e80 174
f843a84c 175#ifdef GUILE_DEBUG /* Used for debugging purposes */
c62fbfe1 176SCM_PROC(s_current_pstate, "current-pstate", 0, 0, 0, scm_current_pstate);
1cc91f1b 177
c62fbfe1
MD
178SCM
179scm_current_pstate ()
c62fbfe1
MD
180{
181 return SCM_CADR (print_state_pool);
182}
183#endif
184
185#define PSTATE_SIZE 50L
186
698c0295
MD
187static SCM make_print_state SCM_P ((void));
188
189static SCM
190make_print_state ()
191{
192 SCM print_state = scm_make_struct (SCM_CAR (print_state_pool), /* pstate type */
bf685b6d 193 SCM_INUM0,
698c0295 194 SCM_EOL);
bf685b6d
MD
195 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
196 pstate->ref_vect = scm_make_vector (SCM_MAKINUM (PSTATE_SIZE),
bf685b6d
MD
197 SCM_UNDEFINED);
198 pstate->ref_stack = SCM_VELTS (pstate->ref_vect);
199 pstate->ceiling = SCM_LENGTH (pstate->ref_vect);
698c0295
MD
200 return print_state;
201}
1cc91f1b 202
c62fbfe1
MD
203SCM
204scm_make_print_state ()
c62fbfe1 205{
698c0295
MD
206 SCM answer = 0;
207
208 /* First try to allocate a print state from the pool */
209 SCM_DEFER_INTS;
210 if (SCM_NNULLP (SCM_CDR (print_state_pool)))
211 {
212 answer = SCM_CADR (print_state_pool);
213 SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool));
214 }
215 SCM_ALLOW_INTS;
216
217 return answer ? answer : make_print_state ();
c62fbfe1 218}
a51ea417 219
698c0295
MD
220void
221scm_free_print_state (print_state)
222 SCM print_state;
223{
224 SCM handle;
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
229 * often.
230 */
231 pstate->fancyp = 0;
bb35f315 232 pstate->revealed = 0;
698c0295
MD
233 SCM_NEWCELL (handle);
234 SCM_DEFER_INTS;
235 SCM_SETCAR (handle, print_state);
236 SCM_SETCDR (handle, SCM_CDR (print_state_pool));
237 SCM_SETCDR (print_state_pool, handle);
238 SCM_ALLOW_INTS;
239}
1cc91f1b
JB
240
241static void grow_ref_stack SCM_P ((scm_print_state *pstate));
242
a51ea417 243static void
c62fbfe1
MD
244grow_ref_stack (pstate)
245 scm_print_state *pstate;
a51ea417 246{
bf685b6d
MD
247 int new_size = 2 * pstate->ceiling;
248 scm_vector_set_length_x (pstate->ref_vect, SCM_MAKINUM (new_size));
249 pstate->ref_stack = SCM_VELTS (pstate->ref_vect);
250 pstate->ceiling = new_size;
a51ea417
MD
251}
252
1cc91f1b
JB
253
254static void print_circref SCM_P ((SCM port, scm_print_state *pstate, SCM ref));
255
a51ea417 256static void
c62fbfe1
MD
257print_circref (port, pstate, ref)
258 SCM port;
259 scm_print_state *pstate;
260 SCM ref;
a51ea417 261{
c62fbfe1
MD
262 register int i;
263 int self = pstate->top - 1;
264 i = pstate->top - 1;
265 if (SCM_CONSP (pstate->ref_stack[i]))
266 {
267 while (i > 0)
268 {
269 if (SCM_NCONSP (pstate->ref_stack[i - 1])
270 || SCM_CDR (pstate->ref_stack[i - 1]) != pstate->ref_stack[i])
271 break;
272 --i;
273 }
274 self = i;
275 }
276 for (i = pstate->top - 1; 1; --i)
277 if (pstate->ref_stack[i] == ref)
278 break;
b7f3516f 279 scm_putc ('#', port);
c62fbfe1 280 scm_intprint (i - self, 10, port);
b7f3516f 281 scm_putc ('#', port);
a51ea417
MD
282}
283
c62fbfe1 284/* Print generally. Handles both write and display according to PSTATE.
0f2d19dd 285 */
a51ea417 286
1cc91f1b 287
0f2d19dd 288void
c62fbfe1 289scm_iprin1 (exp, port, pstate)
0f2d19dd
JB
290 SCM exp;
291 SCM port;
c62fbfe1 292 scm_print_state *pstate;
0f2d19dd 293{
0f2d19dd
JB
294taloop:
295 switch (7 & (int) exp)
296 {
297 case 2:
298 case 6:
299 scm_intprint (SCM_INUM (exp), 10, port);
300 break;
301 case 4:
302 if (SCM_ICHRP (exp))
303 {
5ca6dc39
JB
304 register long i;
305
0f2d19dd 306 i = SCM_ICHR (exp);
b7f3516f
TT
307 if (SCM_WRITINGP (pstate))
308 {
309 scm_puts ("#\\", port);
310 if ((i >= 0) && (i <= ' ') && scm_charnames[i])
311 scm_puts (scm_charnames[i], port);
312 else if (i < 0 || i > '\177')
313 scm_intprint (i, 8, port);
314 else
315 scm_putc (i, port);
316 }
317 else
318 scm_putc (i, port);
0f2d19dd 319 }
a51ea417 320 else if (SCM_IFLAGP (exp)
5ca6dc39 321 && ((size_t) SCM_ISYMNUM (exp) < (sizeof scm_isymnames / sizeof (char *))))
b7f3516f 322 scm_puts (SCM_ISYMCHARS (exp), port);
0f2d19dd
JB
323 else if (SCM_ILOCP (exp))
324 {
b7f3516f 325 scm_puts ("#@", port);
0f2d19dd 326 scm_intprint ((long) SCM_IFRAME (exp), 10, port);
b7f3516f 327 scm_putc (SCM_ICDRP (exp) ? '-' : '+', port);
0f2d19dd
JB
328 scm_intprint ((long) SCM_IDIST (exp), 10, port);
329 }
330 else
331 goto idef;
332 break;
333 case 1:
334 /* gloc */
b7f3516f 335 scm_puts ("#@", port);
0f2d19dd
JB
336 exp = SCM_CAR (exp - 1);
337 goto taloop;
338 default:
339 idef:
340 scm_ipruk ("immediate", exp, port);
341 break;
342 case 0:
343 switch (SCM_TYP7 (exp))
344 {
345 case scm_tcs_cons_gloc:
346
347 if (SCM_CDR (SCM_CAR (exp) - 1L) == 0)
348 {
c4f37e80 349 ENTER_NESTED_DATA (pstate, exp, circref);
bafcafb2 350 scm_print_struct (exp, port, pstate);
c4f37e80 351 EXIT_NESTED_DATA (pstate);
0f2d19dd
JB
352 break;
353 }
354
355 case scm_tcs_cons_imcar:
356 case scm_tcs_cons_nimcar:
c62fbfe1
MD
357 ENTER_NESTED_DATA (pstate, exp, circref);
358 scm_iprlist ("(", exp, ')', port, pstate);
359 EXIT_NESTED_DATA (pstate);
a51ea417
MD
360 break;
361 circref:
c62fbfe1 362 print_circref (port, pstate, exp);
0f2d19dd 363 break;
7332df66 364 macros:
80ea260c 365 if (!SCM_CLOSUREP (SCM_CDR (exp)))
7332df66 366 goto prinmacro;
0f2d19dd 367 case scm_tcs_closures:
7332df66
MD
368 /* The user supplied print closure procedure must handle
369 macro closures as well. */
bb35f315
MV
370 if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE))
371 || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE,
77c25af7 372 exp, port, pstate)))
bb35f315 373 {
ba031394 374 SCM name, code, env;
bb35f315
MV
375 if (SCM_TYP16 (exp) == scm_tc16_macro)
376 {
377 /* Printing a macro. */
378 prinmacro:
379 name = scm_macro_name (exp);
380 if (!SCM_CLOSUREP (SCM_CDR (exp)))
381 {
f44dd64b 382 code = env = 0;
b7f3516f 383 scm_puts ("#<primitive-", port);
bb35f315
MV
384 }
385 else
386 {
387 code = SCM_CODE (SCM_CDR (exp));
ba031394 388 env = SCM_ENV (SCM_CDR (exp));
b7f3516f 389 scm_puts ("#<", port);
bb35f315
MV
390 }
391 if (SCM_CAR (exp) & (3L << 16))
b7f3516f 392 scm_puts ("macro", port);
bb35f315 393 else
b7f3516f 394 scm_puts ("syntax", port);
bb35f315 395 if (SCM_CAR (exp) & (2L << 16))
b7f3516f 396 scm_putc ('!', port);
bb35f315
MV
397 }
398 else
399 {
400 /* Printing a closure. */
401 name = scm_procedure_name (exp);
402 code = SCM_CODE (exp);
ba031394 403 env = SCM_ENV (exp);
b7f3516f 404 scm_puts ("#<procedure", port);
bb35f315
MV
405 }
406 if (SCM_NIMP (name) && SCM_ROSTRINGP (name))
407 {
b7f3516f
TT
408 scm_putc (' ', port);
409 scm_puts (SCM_ROCHARS (name), port);
bb35f315
MV
410 }
411 if (code)
412 {
ba031394
MD
413 if (SCM_PRINT_SOURCE_P)
414 {
415 code = scm_unmemocopy (code,
416 SCM_EXTEND_ENV (SCM_CAR (code),
417 SCM_EOL,
418 env));
419 ENTER_NESTED_DATA (pstate, exp, circref);
420 scm_iprlist (" ", code, '>', port, pstate);
421 EXIT_NESTED_DATA (pstate);
422 }
423 else
424 {
425 if (SCM_TYP16 (exp) != scm_tc16_macro)
426 {
b7f3516f 427 scm_putc (' ', port);
ba031394
MD
428 scm_iprin1 (SCM_CAR (code), port, pstate);
429 }
b7f3516f 430 scm_putc ('>', port);
ba031394 431 }
bb35f315
MV
432 }
433 else
b7f3516f 434 scm_putc ('>', port);
bb35f315 435 }
0f2d19dd 436 break;
0f2d19dd
JB
437 case scm_tc7_substring:
438 case scm_tc7_string:
c62fbfe1 439 if (SCM_WRITINGP (pstate))
0f2d19dd 440 {
5ca6dc39
JB
441 scm_sizet i;
442
b7f3516f 443 scm_putc ('"', port);
0f2d19dd
JB
444 for (i = 0; i < SCM_ROLENGTH (exp); ++i)
445 switch (SCM_ROCHARS (exp)[i])
446 {
dbef8851 447 case '"':
0f2d19dd 448 case '\\':
b7f3516f 449 scm_putc ('\\', port);
0f2d19dd 450 default:
b7f3516f 451 scm_putc (SCM_ROCHARS (exp)[i], port);
0f2d19dd 452 }
b7f3516f 453 scm_putc ('"', port);
0f2d19dd
JB
454 break;
455 }
456 else
b7f3516f
TT
457 scm_lfwrite (SCM_ROCHARS (exp), (scm_sizet) SCM_ROLENGTH (exp),
458 port);
0f2d19dd
JB
459 break;
460 case scm_tcs_symbols:
0f2d19dd
JB
461 {
462 int pos;
463 int end;
464 int len;
465 char * str;
466 int weird;
467 int maybe_weird;
4dc2435a 468 int mw_pos = 0;
0f2d19dd
JB
469
470 len = SCM_LENGTH (exp);
471 str = SCM_CHARS (exp);
472 scm_remember (&exp);
473 pos = 0;
474 weird = 0;
475 maybe_weird = 0;
476
477 if (len == 0)
b7f3516f 478 scm_lfwrite ("#{}#", 4, port);
0f2d19dd
JB
479
480 for (end = pos; end < len; ++end)
481 switch (str[end])
482 {
483#ifdef BRACKETS_AS_PARENS
484 case '[':
485 case ']':
486#endif
487 case '(':
488 case ')':
dbef8851 489 case '"':
0f2d19dd
JB
490 case ';':
491 case SCM_WHITE_SPACES:
492 case SCM_LINE_INCREMENTORS:
493 weird_handler:
494 if (maybe_weird)
495 {
496 end = mw_pos;
497 maybe_weird = 0;
498 }
499 if (!weird)
500 {
b7f3516f 501 scm_lfwrite ("#{", 2, port);
0f2d19dd
JB
502 weird = 1;
503 }
504 if (pos < end)
505 {
b7f3516f 506 scm_lfwrite (str + pos, end - pos, port);
0f2d19dd
JB
507 }
508 {
509 char buf[2];
510 buf[0] = '\\';
511 buf[1] = str[end];
b7f3516f 512 scm_lfwrite (buf, 2, port);
0f2d19dd
JB
513 }
514 pos = end + 1;
515 break;
516 case '\\':
517 if (weird)
518 goto weird_handler;
519 if (!maybe_weird)
520 {
521 maybe_weird = 1;
522 mw_pos = pos;
523 }
524 break;
525 case '}':
526 case '#':
527 if (weird)
528 goto weird_handler;
529 break;
530 default:
531 break;
532 }
533 if (pos < end)
b7f3516f 534 scm_lfwrite (str + pos, end - pos, port);
0f2d19dd 535 if (weird)
b7f3516f 536 scm_lfwrite ("}#", 2, port);
0f2d19dd
JB
537 break;
538 }
539 case scm_tc7_wvect:
c62fbfe1 540 ENTER_NESTED_DATA (pstate, exp, circref);
0f2d19dd 541 if (SCM_IS_WHVEC (exp))
b7f3516f 542 scm_puts ("#wh(", port);
0f2d19dd 543 else
b7f3516f 544 scm_puts ("#w(", port);
0f2d19dd
JB
545 goto common_vector_printer;
546
547 case scm_tc7_vector:
c62fbfe1 548 ENTER_NESTED_DATA (pstate, exp, circref);
b7f3516f 549 scm_puts ("#(", port);
0f2d19dd 550 common_vector_printer:
9fbaf27c 551 {
5ca6dc39 552 register long i;
9fbaf27c
MD
553 int last = SCM_LENGTH (exp) - 1;
554 int cutp = 0;
555 if (pstate->fancyp && SCM_LENGTH (exp) > pstate->length)
556 {
557 last = pstate->length - 1;
558 cutp = 1;
559 }
560 for (i = 0; i < last; ++i)
561 {
562 /* CHECK_INTS; */
563 scm_iprin1 (SCM_VELTS (exp)[i], port, pstate);
b7f3516f 564 scm_putc (' ', port);
9fbaf27c
MD
565 }
566 if (i == last)
567 {
568 /* CHECK_INTS; */
569 scm_iprin1 (SCM_VELTS (exp)[i], port, pstate);
570 }
571 if (cutp)
b7f3516f
TT
572 scm_puts (" ...", port);
573 scm_putc (')', port);
9fbaf27c 574 }
c62fbfe1 575 EXIT_NESTED_DATA (pstate);
0f2d19dd
JB
576 break;
577 case scm_tc7_bvect:
578 case scm_tc7_byvect:
579 case scm_tc7_svect:
580 case scm_tc7_ivect:
581 case scm_tc7_uvect:
582 case scm_tc7_fvect:
583 case scm_tc7_dvect:
584 case scm_tc7_cvect:
585#ifdef LONGLONGS
586 case scm_tc7_llvect:
587#endif
c62fbfe1 588 scm_raprin1 (exp, port, pstate);
0f2d19dd
JB
589 break;
590 case scm_tcs_subrs:
9de33deb
MD
591 scm_puts (SCM_SUBR_GENERIC (exp) && *SCM_SUBR_GENERIC (exp)
592 ? "#<primitive-generic "
593 : "#<primitive-procedure ",
594 port);
b7f3516f
TT
595 scm_puts (SCM_CHARS (SCM_SNAME (exp)), port);
596 scm_putc ('>', port);
0f2d19dd
JB
597 break;
598#ifdef CCLO
599 case scm_tc7_cclo:
56977059
MD
600 {
601 SCM proc = SCM_CCLO_SUBR (exp);
602 if (proc == scm_f_gsubr_apply)
603 {
604 /* Print gsubrs as primitives */
c180dfd3 605 SCM name = scm_procedure_name (exp);
56977059
MD
606 scm_puts ("#<primitive-procedure", port);
607 if (SCM_NFALSEP (name))
608 {
609 scm_putc (' ', port);
610 scm_puts (SCM_CHARS (name), port);
611 }
612 }
613 else
614 {
615 scm_puts ("#<compiled-closure ", port);
616 scm_iprin1 (proc, port, pstate);
617 }
618 scm_putc ('>', port);
619 }
0f2d19dd
JB
620 break;
621#endif
c180dfd3
MD
622 case scm_tc7_pws:
623 scm_puts ("#<procedure-with-setter", port);
624 {
625 SCM name = scm_procedure_name (exp);
626 if (SCM_NFALSEP (name))
627 {
628 scm_putc (' ', port);
629 scm_puts (SCM_ROCHARS (name), port);
630 }
631 }
632 scm_putc ('>', port);
633 break;
0f2d19dd 634 case scm_tc7_contin:
b7f3516f 635 scm_puts ("#<continuation ", port);
0f2d19dd 636 scm_intprint (SCM_LENGTH (exp), 10, port);
b7f3516f 637 scm_puts (" @ ", port);
0f2d19dd 638 scm_intprint ((long) SCM_CHARS (exp), 16, port);
b7f3516f 639 scm_putc ('>', port);
0f2d19dd
JB
640 break;
641 case scm_tc7_port:
5ca6dc39
JB
642 {
643 register long i = SCM_PTOBNUM (exp);
644 if (i < scm_numptob
645 && scm_ptobs[i].print
646 && (scm_ptobs[i].print) (exp, port, pstate))
a51ea417 647 break;
5ca6dc39
JB
648 goto punk;
649 }
650 case scm_tc7_smob:
651 {
652 register long i;
653 ENTER_NESTED_DATA (pstate, exp, circref);
654 i = SCM_SMOBNUM (exp);
655 if (i < scm_numsmob && scm_smobs[i].print
656 && (scm_smobs[i].print) (exp, port, pstate))
657 {
658 EXIT_NESTED_DATA (pstate);
659 break;
660 }
661 EXIT_NESTED_DATA (pstate);
662 /* Macros have their print field set to NULL. They are
663 handled at the same place as closures in order to achieve
664 non-redundancy. Placing the condition here won't slow
665 down printing of other smobs. */
666 if (SCM_TYP16 (exp) == scm_tc16_macro)
667 goto macros;
668 }
0f2d19dd 669 default:
a51ea417
MD
670 punk:
671 scm_ipruk ("type", exp, port);
0f2d19dd
JB
672 }
673 }
674}
675
c62fbfe1
MD
676/* Print states are necessary for circular reference safe printing.
677 * They are also expensive to allocate. Therefore print states are
678 * kept in a pool so that they can be reused.
679 */
1cc91f1b 680
bb35f315
MV
681/* The PORT argument can also be a print-state/port pair, which will
682 * then be used instead of allocating a new print state. This is
683 * useful for continuing a chain of print calls from Scheme. */
684
a51ea417 685void
c62fbfe1 686scm_prin1 (exp, port, writingp)
a51ea417
MD
687 SCM exp;
688 SCM port;
c62fbfe1 689 int writingp;
a51ea417 690{
c4f37e80
MV
691 SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
692 SCM pstate_scm;
c62fbfe1
MD
693 scm_print_state *pstate;
694
bb35f315
MV
695 /* If PORT is a print-state/port pair, use that. Else create a new
696 print-state. */
c4f37e80 697
52235e71 698 if (SCM_NIMP (port) && SCM_PORT_WITH_PS_P (port))
bb35f315 699 {
52235e71
MD
700 pstate_scm = SCM_PORT_WITH_PS_PS (port);
701 port = SCM_PORT_WITH_PS_PORT (port);
bb35f315
MV
702 }
703 else
c62fbfe1 704 {
c4f37e80
MV
705 /* First try to allocate a print state from the pool */
706 SCM_DEFER_INTS;
707 if (SCM_NNULLP (SCM_CDR (print_state_pool)))
708 {
709 handle = SCM_CDR (print_state_pool);
710 SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool));
711 }
712 SCM_ALLOW_INTS;
713 if (handle == SCM_BOOL_F)
714 handle = scm_cons (make_print_state (), SCM_EOL);
715 pstate_scm = SCM_CAR (handle);
c62fbfe1 716 }
c62fbfe1 717
c4f37e80 718 pstate = SCM_PRINT_STATE (pstate_scm);
c62fbfe1
MD
719 pstate->writingp = writingp;
720 scm_iprin1 (exp, port, pstate);
721
bb35f315
MV
722 /* Return print state to pool if it has been created above and
723 hasn't escaped to Scheme. */
724
725 if (handle != SCM_BOOL_F && !pstate->revealed)
c4f37e80
MV
726 {
727 SCM_DEFER_INTS;
728 SCM_SETCDR (handle, SCM_CDR (print_state_pool));
729 SCM_SETCDR (print_state_pool, handle);
730 SCM_ALLOW_INTS;
731 }
a51ea417
MD
732}
733
0f2d19dd
JB
734
735/* Print an integer.
736 */
1cc91f1b 737
0f2d19dd
JB
738void
739scm_intprint (n, radix, port)
740 long n;
741 int radix;
742 SCM port;
0f2d19dd
JB
743{
744 char num_buf[SCM_INTBUFLEN];
b7f3516f 745 scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port);
0f2d19dd
JB
746}
747
748/* Print an object of unrecognized type.
749 */
1cc91f1b 750
0f2d19dd
JB
751void
752scm_ipruk (hdr, ptr, port)
753 char *hdr;
754 SCM ptr;
755 SCM port;
0f2d19dd 756{
b7f3516f
TT
757 scm_puts ("#<unknown-", port);
758 scm_puts (hdr, port);
0f2d19dd
JB
759 if (SCM_CELLP (ptr))
760 {
b7f3516f 761 scm_puts (" (0x", port);
0f2d19dd 762 scm_intprint (SCM_CAR (ptr), 16, port);
b7f3516f 763 scm_puts (" . 0x", port);
0f2d19dd 764 scm_intprint (SCM_CDR (ptr), 16, port);
b7f3516f 765 scm_puts (") @", port);
0f2d19dd 766 }
b7f3516f 767 scm_puts (" 0x", port);
0f2d19dd 768 scm_intprint (ptr, 16, port);
b7f3516f 769 scm_putc ('>', port);
0f2d19dd
JB
770}
771
772/* Print a list.
773 */
a51ea417 774
1cc91f1b 775
0f2d19dd 776void
c62fbfe1 777scm_iprlist (hdr, exp, tlr, port, pstate)
0f2d19dd
JB
778 char *hdr;
779 SCM exp;
805df3e8 780 int tlr;
0f2d19dd 781 SCM port;
c62fbfe1 782 scm_print_state *pstate;
0f2d19dd 783{
c62fbfe1
MD
784 register SCM hare, tortoise;
785 int floor = pstate->top - 2;
b7f3516f 786 scm_puts (hdr, port);
0f2d19dd 787 /* CHECK_INTS; */
c62fbfe1
MD
788 if (pstate->fancyp)
789 goto fancy_printing;
790
791 /* Run a hare and tortoise so that total time complexity will be
792 O(depth * N) instead of O(N^2). */
793 hare = SCM_CDR (exp);
794 tortoise = exp;
2fab3faa 795 while (SCM_NIMP (hare) && SCM_ECONSP (hare))
c62fbfe1
MD
796 {
797 if (hare == tortoise)
798 goto fancy_printing;
799 hare = SCM_CDR (hare);
2fab3faa 800 if (SCM_IMP (hare) || SCM_NECONSP (hare))
c62fbfe1
MD
801 break;
802 hare = SCM_CDR (hare);
803 tortoise = SCM_CDR (tortoise);
804 }
805
806 /* No cdr cycles intrinsic to this list */
807 scm_iprin1 (SCM_CAR (exp), port, pstate);
0f2d19dd
JB
808 exp = SCM_CDR (exp);
809 for (; SCM_NIMP (exp); exp = SCM_CDR (exp))
810 {
5ca6dc39
JB
811 register int i;
812
0f2d19dd
JB
813 if (SCM_NECONSP (exp))
814 break;
c62fbfe1
MD
815 for (i = floor; i >= 0; --i)
816 if (pstate->ref_stack[i] == exp)
817 goto circref;
818 PUSH_REF (pstate, exp);
b7f3516f 819 scm_putc (' ', port);
0f2d19dd 820 /* CHECK_INTS; */
c62fbfe1 821 scm_iprin1 (SCM_CAR (exp), port, pstate);
0f2d19dd
JB
822 }
823 if (SCM_NNULLP (exp))
824 {
b7f3516f 825 scm_puts (" . ", port);
c62fbfe1 826 scm_iprin1 (exp, port, pstate);
0f2d19dd 827 }
c62fbfe1 828
a51ea417 829end:
b7f3516f 830 scm_putc (tlr, port);
c62fbfe1 831 pstate->top = floor + 2;
a51ea417 832 return;
c62fbfe1
MD
833
834fancy_printing:
835 {
836 int n = pstate->length;
837
838 scm_iprin1 (SCM_CAR (exp), port, pstate);
839 exp = SCM_CDR (exp); --n;
840 for (; SCM_NIMP (exp); exp = SCM_CDR (exp))
841 {
5ca6dc39
JB
842 register unsigned long i;
843
c62fbfe1
MD
844 if (SCM_NECONSP (exp))
845 break;
846 for (i = 0; i < pstate->top; ++i)
847 if (pstate->ref_stack[i] == exp)
848 goto fancy_circref;
849 if (pstate->fancyp)
850 {
851 if (n == 0)
852 {
b7f3516f 853 scm_puts (" ...", port);
c62fbfe1
MD
854 goto skip_tail;
855 }
856 else
857 --n;
858 }
859 PUSH_REF(pstate, exp);
860 ++pstate->list_offset;
b7f3516f 861 scm_putc (' ', port);
c62fbfe1
MD
862 /* CHECK_INTS; */
863 scm_iprin1 (SCM_CAR (exp), port, pstate);
864 }
865 }
866 if (SCM_NNULLP (exp))
867 {
b7f3516f 868 scm_puts (" . ", port);
c62fbfe1
MD
869 scm_iprin1 (exp, port, pstate);
870 }
871skip_tail:
872 pstate->list_offset -= pstate->top - floor - 2;
a51ea417 873 goto end;
a51ea417 874
c62fbfe1
MD
875fancy_circref:
876 pstate->list_offset -= pstate->top - floor - 2;
877
878circref:
b7f3516f 879 scm_puts (" . ", port);
c62fbfe1
MD
880 print_circref (port, pstate, exp);
881 goto end;
0f2d19dd
JB
882}
883
884\f
885
bb35f315
MV
886int
887scm_valid_oport_value_p (SCM val)
888{
4bfdf158
MD
889 return (SCM_NIMP (val)
890 && (SCM_OPOUTPORTP (val)
c19bc088
MD
891 || (SCM_PORT_WITH_PS_P (val)
892 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val)))));
bb35f315
MV
893}
894
0f2d19dd 895SCM_PROC(s_write, "write", 1, 1, 0, scm_write);
1cc91f1b 896
0f2d19dd
JB
897SCM
898scm_write (obj, port)
899 SCM obj;
900 SCM port;
0f2d19dd
JB
901{
902 if (SCM_UNBNDP (port))
903 port = scm_cur_outp;
904 else
bb35f315
MV
905 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
906
a51ea417 907 scm_prin1 (obj, port, 1);
0f2d19dd
JB
908#ifdef HAVE_PIPE
909# ifdef EPIPE
910 if (EPIPE == errno)
911 scm_close_port (port);
912# endif
913#endif
914 return SCM_UNSPECIFIED;
915}
916
917
918SCM_PROC(s_display, "display", 1, 1, 0, scm_display);
1cc91f1b 919
0f2d19dd
JB
920SCM
921scm_display (obj, port)
922 SCM obj;
923 SCM port;
0f2d19dd
JB
924{
925 if (SCM_UNBNDP (port))
926 port = scm_cur_outp;
927 else
bb35f315
MV
928 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
929
a51ea417 930 scm_prin1 (obj, port, 0);
0f2d19dd
JB
931#ifdef HAVE_PIPE
932# ifdef EPIPE
933 if (EPIPE == errno)
934 scm_close_port (port);
935# endif
936#endif
937 return SCM_UNSPECIFIED;
938}
939
940SCM_PROC(s_newline, "newline", 0, 1, 0, scm_newline);
1cc91f1b 941
0f2d19dd
JB
942SCM
943scm_newline (port)
944 SCM port;
0f2d19dd
JB
945{
946 if (SCM_UNBNDP (port))
bb35f315 947 port = scm_cur_outp;
0f2d19dd 948 else
bb35f315
MV
949 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG1, s_newline);
950
0ef4ae82 951 scm_putc ('\n', SCM_COERCE_OUTPORT (port));
0f2d19dd
JB
952 return SCM_UNSPECIFIED;
953}
954
955SCM_PROC(s_write_char, "write-char", 1, 1, 0, scm_write_char);
1cc91f1b 956
0f2d19dd
JB
957SCM
958scm_write_char (chr, port)
959 SCM chr;
960 SCM port;
0f2d19dd
JB
961{
962 if (SCM_UNBNDP (port))
bb35f315 963 port = scm_cur_outp;
0f2d19dd 964 else
bb35f315
MV
965 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write_char);
966
0f2d19dd 967 SCM_ASSERT (SCM_ICHRP (chr), chr, SCM_ARG1, s_write_char);
0ef4ae82 968 scm_putc ((int) SCM_ICHR (chr), SCM_COERCE_OUTPORT (port));
0f2d19dd
JB
969#ifdef HAVE_PIPE
970# ifdef EPIPE
971 if (EPIPE == errno)
972 scm_close_port (port);
973# endif
974#endif
975 return SCM_UNSPECIFIED;
976}
977
0f2d19dd
JB
978\f
979
bb35f315 980/* Call back to Scheme code to do the printing of special objects
c19bc088
MD
981 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
982 * containing PORT and PSTATE. This object can be used as the port for
983 * display/write etc to continue the current print chain. The REVEALED
984 * field of PSTATE is set to true to indicate that the print state has
985 * escaped to Scheme and thus has to be freed by the GC.
986 */
987
988long scm_tc16_port_with_ps;
989
990/* Print exactly as the port itself would */
991
992static int
993print_port_with_ps (SCM obj, SCM port, scm_print_state *pstate)
994{
995 obj = SCM_PORT_WITH_PS_PORT (obj);
996 return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate);
997}
c4f37e80
MV
998
999SCM
1000scm_printer_apply (proc, exp, port, pstate)
1001 SCM proc, exp, port;
1002 scm_print_state *pstate;
1003{
c19bc088 1004 SCM pwps;
bb35f315 1005 SCM pair = scm_cons (port, pstate->handle);
c19bc088 1006 SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, pair);
bb35f315 1007 pstate->revealed = 1;
c19bc088
MD
1008 return scm_apply (proc, exp, scm_cons (pwps, scm_listofnull));
1009}
1010
1011SCM_PROC (s_port_with_print_state, "port-with-print-state", 2, 0, 0, scm_port_with_print_state);
1012
1013SCM
1014scm_port_with_print_state (SCM port, SCM pstate)
1015{
1016 SCM pwps;
1017 SCM_ASSERT (scm_valid_oport_value_p (port),
1018 port, SCM_ARG1, s_port_with_print_state);
1019 SCM_ASSERT (SCM_NIMP (pstate) && SCM_PRINT_STATE_P (pstate),
1020 pstate, SCM_ARG2, s_port_with_print_state);
1021 port = SCM_COERCE_OUTPORT (port);
1022 SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, scm_cons (port, pstate));
1023 return pwps;
1024}
1025
1026SCM_PROC (s_get_print_state, "get-print-state", 1, 0, 0, scm_get_print_state);
1027
1028SCM
1029scm_get_print_state (SCM port)
1030{
1031 if (SCM_NIMP (port))
1032 {
1033 if (SCM_PORT_WITH_PS_P (port))
1034 return SCM_PORT_WITH_PS_PS (port);
1035 if (SCM_OUTPORTP (port))
1036 return SCM_BOOL_F;
1037 }
1038 return scm_wta (port, (char *) SCM_ARG1, s_get_print_state);
c4f37e80 1039}
bb35f315 1040
c4f37e80 1041\f
1cc91f1b 1042
0f2d19dd
JB
1043void
1044scm_init_print ()
0f2d19dd 1045{
c19bc088 1046 SCM vtable, layout, type;
4bfdf158 1047
b7ff98dd 1048 scm_init_opts (scm_print_options, scm_print_opts, SCM_N_PRINT_OPTIONS);
4bfdf158
MD
1049 vtable = scm_make_vtable_vtable (scm_make_struct_layout (scm_nullstr),
1050 SCM_INUM0,
1051 SCM_EOL);
1052 layout = scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT));
c19bc088 1053 type = scm_make_struct (vtable, SCM_INUM0, SCM_LIST1 (layout));
e9cd0e47 1054 scm_set_struct_vtable_name_x (type, SCM_CAR (scm_intern0 ("print-state")));
c62fbfe1 1055 print_state_pool = scm_permanent_object (scm_cons (type, SCM_EOL));
c4f37e80 1056
bb35f315 1057 scm_print_state_vtable = type;
c4f37e80 1058
c19bc088
MD
1059 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1060 scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
1061 scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr);
1062 scm_set_smob_print (scm_tc16_port_with_ps, print_port_with_ps);
1063
0f2d19dd
JB
1064#include "print.x"
1065}