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