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