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