*** empty log message ***
[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. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
47#include <stdio.h>
48#include "_scm.h"
20e6290e
JB
49#include "chars.h"
50#include "genio.h"
20e6290e
JB
51#include "smob.h"
52#include "eval.h"
ddeae7ca 53#include "macros.h"
20e6290e
JB
54#include "procprop.h"
55#include "read.h"
56#include "weaks.h"
57#include "unif.h"
dbef8851 58#include "alist.h"
c62fbfe1 59#include "struct.h"
8b840115 60#include "objects.h"
70d63753 61#include "strports.h"
0f2d19dd 62
1bbd0b84 63#include "scm_validate.h"
20e6290e 64#include "print.h"
0f2d19dd
JB
65\f
66
67/* {Names of immediate symbols}
68 *
69 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
70 */
71
72char *scm_isymnames[] =
73{
74 /* This table must agree with the declarations */
75 "#@and",
76 "#@begin",
77 "#@case",
78 "#@cond",
79 "#@do",
80 "#@if",
81 "#@lambda",
82 "#@let",
83 "#@let*",
84 "#@letrec",
85 "#@or",
86 "#@quote",
87 "#@set!",
88 "#@define",
89#if 0
90 "#@literal-variable-ref",
91 "#@literal-variable-set!",
92#endif
93 "#@apply",
94 "#@call-with-current-continuation",
95
96 /* user visible ISYMS */
97 /* other keywords */
98 /* Flags */
99
100 "#f",
101 "#t",
102 "#<undefined>",
103 "#<eof>",
104 "()",
4df91bd6
MD
105 "#<unspecified>",
106 "#@dispatch",
43c667e9
MD
107 "#@slot-ref",
108 "#@slot-set!",
159500fb 109
159500fb
MD
110 /* Multi-language support */
111
112 "#@nil-cond",
113 "#@nil-ify",
114 "#@t-ify",
115 "#@0-cond",
116 "#@0-ify",
117 "#@1-ify",
ccd0e478
MD
118 "#@bind",
119
5623a9b4
MD
120 "#@delay",
121
122 "#<unbound>"
0f2d19dd
JB
123};
124
e6e4c9af 125scm_option scm_print_opts[] = {
b7ff98dd 126 { SCM_OPTION_SCM, "closure-hook", SCM_BOOL_F,
84f6a34a
MD
127 "Hook for printing closures." },
128 { SCM_OPTION_BOOLEAN, "source", 0,
129 "Print closures with source." }
e6e4c9af
MD
130};
131
a1ec6916 132SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0,
1bbd0b84
GB
133 (SCM setting),
134"")
135#define FUNC_NAME s_scm_print_options
e6e4c9af 136{
a51ea417 137 SCM ans = scm_options (setting,
b7ff98dd
MD
138 scm_print_opts,
139 SCM_N_PRINT_OPTIONS,
1bbd0b84 140 FUNC_NAME);
e6e4c9af
MD
141 return ans;
142}
1bbd0b84 143#undef FUNC_NAME
e6e4c9af 144
0f2d19dd
JB
145\f
146/* {Printing of Scheme Objects}
147 */
148
a51ea417 149/* Detection of circular references.
c62fbfe1
MD
150 *
151 * Due to other constraints in the implementation, this code has bad
152 * time complexity (O (depth * N)), The printer code will be
153 * completely rewritten before next release of Guile. The new code
154 * will be O(N).
a51ea417 155 */
c62fbfe1 156#define PUSH_REF(pstate, obj) \
1bbd0b84 157do { \
c62fbfe1
MD
158 pstate->ref_stack[pstate->top++] = (obj); \
159 if (pstate->top == pstate->ceiling) \
160 grow_ref_stack (pstate); \
1bbd0b84 161} while(0)
a51ea417 162
c62fbfe1 163#define ENTER_NESTED_DATA(pstate, obj, label) \
1bbd0b84 164do { \
5ca6dc39 165 register unsigned long i; \
c62fbfe1
MD
166 for (i = 0; i < pstate->top; ++i) \
167 if (pstate->ref_stack[i] == (obj)) \
168 goto label; \
169 if (pstate->fancyp) \
170 { \
171 if (pstate->top - pstate->list_offset >= pstate->level) \
172 { \
b7f3516f 173 scm_putc ('#', port); \
c62fbfe1
MD
174 return; \
175 } \
176 } \
177 PUSH_REF(pstate, obj); \
1bbd0b84 178} while(0)
a51ea417 179
c62fbfe1
MD
180#define EXIT_NESTED_DATA(pstate) { --pstate->top; }
181
bb35f315 182SCM scm_print_state_vtable;
c4f37e80 183
bb35f315 184static SCM print_state_pool;
c4f37e80 185
f843a84c 186#ifdef GUILE_DEBUG /* Used for debugging purposes */
1cc91f1b 187
3b3b36dd 188SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0,
1bbd0b84 189 (),
b450f070
GB
190 "Return the current-pstate--the `cadr' of the print_state_pool.\n"
191 "`current-pstate' is only included in GUILE_DEBUG builds.")
1bbd0b84 192#define FUNC_NAME s_scm_current_pstate
c62fbfe1
MD
193{
194 return SCM_CADR (print_state_pool);
195}
1bbd0b84
GB
196#undef FUNC_NAME
197
c62fbfe1
MD
198#endif
199
200#define PSTATE_SIZE 50L
201
698c0295 202static SCM
1bbd0b84 203make_print_state (void)
698c0295
MD
204{
205 SCM print_state = scm_make_struct (SCM_CAR (print_state_pool), /* pstate type */
bf685b6d 206 SCM_INUM0,
698c0295 207 SCM_EOL);
bf685b6d
MD
208 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
209 pstate->ref_vect = scm_make_vector (SCM_MAKINUM (PSTATE_SIZE),
bf685b6d
MD
210 SCM_UNDEFINED);
211 pstate->ref_stack = SCM_VELTS (pstate->ref_vect);
212 pstate->ceiling = SCM_LENGTH (pstate->ref_vect);
698c0295
MD
213 return print_state;
214}
1cc91f1b 215
c62fbfe1
MD
216SCM
217scm_make_print_state ()
c62fbfe1 218{
698c0295
MD
219 SCM answer = 0;
220
221 /* First try to allocate a print state from the pool */
222 SCM_DEFER_INTS;
223 if (SCM_NNULLP (SCM_CDR (print_state_pool)))
224 {
225 answer = SCM_CADR (print_state_pool);
226 SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool));
227 }
228 SCM_ALLOW_INTS;
229
230 return answer ? answer : make_print_state ();
c62fbfe1 231}
a51ea417 232
698c0295 233void
6e8d25a6 234scm_free_print_state (SCM print_state)
698c0295
MD
235{
236 SCM handle;
237 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
238 /* Cleanup before returning print state to pool.
239 * It is better to do it here. Doing it in scm_prin1
240 * would cost more since that function is called much more
241 * often.
242 */
243 pstate->fancyp = 0;
bb35f315 244 pstate->revealed = 0;
698c0295
MD
245 SCM_NEWCELL (handle);
246 SCM_DEFER_INTS;
247 SCM_SETCAR (handle, print_state);
248 SCM_SETCDR (handle, SCM_CDR (print_state_pool));
249 SCM_SETCDR (print_state_pool, handle);
250 SCM_ALLOW_INTS;
251}
1cc91f1b 252
a51ea417 253static void
1bbd0b84 254grow_ref_stack (scm_print_state *pstate)
a51ea417 255{
bf685b6d
MD
256 int new_size = 2 * pstate->ceiling;
257 scm_vector_set_length_x (pstate->ref_vect, SCM_MAKINUM (new_size));
258 pstate->ref_stack = SCM_VELTS (pstate->ref_vect);
259 pstate->ceiling = new_size;
a51ea417
MD
260}
261
1cc91f1b 262
a51ea417 263static void
1bbd0b84 264print_circref (SCM port,scm_print_state *pstate,SCM ref)
a51ea417 265{
c62fbfe1
MD
266 register int i;
267 int self = pstate->top - 1;
268 i = pstate->top - 1;
269 if (SCM_CONSP (pstate->ref_stack[i]))
270 {
271 while (i > 0)
272 {
273 if (SCM_NCONSP (pstate->ref_stack[i - 1])
274 || SCM_CDR (pstate->ref_stack[i - 1]) != pstate->ref_stack[i])
275 break;
276 --i;
277 }
278 self = i;
279 }
280 for (i = pstate->top - 1; 1; --i)
281 if (pstate->ref_stack[i] == ref)
282 break;
b7f3516f 283 scm_putc ('#', port);
c62fbfe1 284 scm_intprint (i - self, 10, port);
b7f3516f 285 scm_putc ('#', port);
a51ea417
MD
286}
287
c62fbfe1 288/* Print generally. Handles both write and display according to PSTATE.
0f2d19dd 289 */
8b840115
MD
290SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write);
291SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
1cc91f1b 292
0f2d19dd 293void
1bbd0b84 294scm_iprin1 (SCM exp, SCM port, 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 421 }
0c95b57d 422 if (SCM_ROSTRINGP (name))
bb35f315 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 592 break;
afe5177e 593#ifdef HAVE_ARRAYS
0f2d19dd
JB
594 case scm_tc7_bvect:
595 case scm_tc7_byvect:
596 case scm_tc7_svect:
597 case scm_tc7_ivect:
598 case scm_tc7_uvect:
599 case scm_tc7_fvect:
600 case scm_tc7_dvect:
601 case scm_tc7_cvect:
5c11cc9d 602#ifdef HAVE_LONG_LONGS
0f2d19dd
JB
603 case scm_tc7_llvect:
604#endif
c62fbfe1 605 scm_raprin1 (exp, port, pstate);
0f2d19dd 606 break;
afe5177e 607#endif
0f2d19dd 608 case scm_tcs_subrs:
9de33deb
MD
609 scm_puts (SCM_SUBR_GENERIC (exp) && *SCM_SUBR_GENERIC (exp)
610 ? "#<primitive-generic "
611 : "#<primitive-procedure ",
612 port);
b7f3516f
TT
613 scm_puts (SCM_CHARS (SCM_SNAME (exp)), port);
614 scm_putc ('>', port);
0f2d19dd
JB
615 break;
616#ifdef CCLO
617 case scm_tc7_cclo:
56977059
MD
618 {
619 SCM proc = SCM_CCLO_SUBR (exp);
620 if (proc == scm_f_gsubr_apply)
621 {
622 /* Print gsubrs as primitives */
c180dfd3 623 SCM name = scm_procedure_name (exp);
56977059
MD
624 scm_puts ("#<primitive-procedure", port);
625 if (SCM_NFALSEP (name))
626 {
627 scm_putc (' ', port);
628 scm_puts (SCM_CHARS (name), port);
629 }
630 }
631 else
632 {
633 scm_puts ("#<compiled-closure ", port);
634 scm_iprin1 (proc, port, pstate);
635 }
636 scm_putc ('>', port);
637 }
0f2d19dd
JB
638 break;
639#endif
c180dfd3
MD
640 case scm_tc7_pws:
641 scm_puts ("#<procedure-with-setter", port);
642 {
643 SCM name = scm_procedure_name (exp);
644 if (SCM_NFALSEP (name))
645 {
646 scm_putc (' ', port);
647 scm_puts (SCM_ROCHARS (name), port);
648 }
649 }
650 scm_putc ('>', port);
651 break;
0f2d19dd 652 case scm_tc7_contin:
b7f3516f 653 scm_puts ("#<continuation ", port);
0f2d19dd 654 scm_intprint (SCM_LENGTH (exp), 10, port);
b7f3516f 655 scm_puts (" @ ", port);
0f2d19dd 656 scm_intprint ((long) SCM_CHARS (exp), 16, port);
b7f3516f 657 scm_putc ('>', port);
0f2d19dd
JB
658 break;
659 case scm_tc7_port:
5ca6dc39
JB
660 {
661 register long i = SCM_PTOBNUM (exp);
662 if (i < scm_numptob
663 && scm_ptobs[i].print
664 && (scm_ptobs[i].print) (exp, port, pstate))
a51ea417 665 break;
5ca6dc39
JB
666 goto punk;
667 }
668 case scm_tc7_smob:
669 {
670 register long i;
671 ENTER_NESTED_DATA (pstate, exp, circref);
672 i = SCM_SMOBNUM (exp);
673 if (i < scm_numsmob && scm_smobs[i].print
674 && (scm_smobs[i].print) (exp, port, pstate))
675 {
676 EXIT_NESTED_DATA (pstate);
677 break;
678 }
679 EXIT_NESTED_DATA (pstate);
680 /* Macros have their print field set to NULL. They are
681 handled at the same place as closures in order to achieve
682 non-redundancy. Placing the condition here won't slow
683 down printing of other smobs. */
684 if (SCM_TYP16 (exp) == scm_tc16_macro)
685 goto macros;
686 }
0f2d19dd 687 default:
a51ea417
MD
688 punk:
689 scm_ipruk ("type", exp, port);
0f2d19dd
JB
690 }
691 }
692}
693
c62fbfe1
MD
694/* Print states are necessary for circular reference safe printing.
695 * They are also expensive to allocate. Therefore print states are
696 * kept in a pool so that they can be reused.
697 */
1cc91f1b 698
bb35f315
MV
699/* The PORT argument can also be a print-state/port pair, which will
700 * then be used instead of allocating a new print state. This is
701 * useful for continuing a chain of print calls from Scheme. */
702
a51ea417 703void
1bbd0b84 704scm_prin1 (SCM exp, SCM port, int writingp)
a51ea417 705{
c4f37e80
MV
706 SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
707 SCM pstate_scm;
c62fbfe1
MD
708 scm_print_state *pstate;
709
bb35f315
MV
710 /* If PORT is a print-state/port pair, use that. Else create a new
711 print-state. */
c4f37e80 712
0c95b57d 713 if (SCM_PORT_WITH_PS_P (port))
bb35f315 714 {
52235e71
MD
715 pstate_scm = SCM_PORT_WITH_PS_PS (port);
716 port = SCM_PORT_WITH_PS_PORT (port);
bb35f315
MV
717 }
718 else
c62fbfe1 719 {
c4f37e80
MV
720 /* First try to allocate a print state from the pool */
721 SCM_DEFER_INTS;
722 if (SCM_NNULLP (SCM_CDR (print_state_pool)))
723 {
724 handle = SCM_CDR (print_state_pool);
725 SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool));
726 }
727 SCM_ALLOW_INTS;
728 if (handle == SCM_BOOL_F)
729 handle = scm_cons (make_print_state (), SCM_EOL);
730 pstate_scm = SCM_CAR (handle);
c62fbfe1 731 }
c62fbfe1 732
c4f37e80 733 pstate = SCM_PRINT_STATE (pstate_scm);
c62fbfe1
MD
734 pstate->writingp = writingp;
735 scm_iprin1 (exp, port, pstate);
736
bb35f315
MV
737 /* Return print state to pool if it has been created above and
738 hasn't escaped to Scheme. */
739
740 if (handle != SCM_BOOL_F && !pstate->revealed)
c4f37e80
MV
741 {
742 SCM_DEFER_INTS;
743 SCM_SETCDR (handle, SCM_CDR (print_state_pool));
744 SCM_SETCDR (print_state_pool, handle);
745 SCM_ALLOW_INTS;
746 }
a51ea417
MD
747}
748
0f2d19dd
JB
749
750/* Print an integer.
751 */
1cc91f1b 752
0f2d19dd 753void
1bbd0b84 754scm_intprint (long n, int radix, SCM port)
0f2d19dd
JB
755{
756 char num_buf[SCM_INTBUFLEN];
b7f3516f 757 scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port);
0f2d19dd
JB
758}
759
760/* Print an object of unrecognized type.
761 */
1cc91f1b 762
0f2d19dd 763void
1bbd0b84 764scm_ipruk (char *hdr, SCM ptr, SCM port)
0f2d19dd 765{
b7f3516f
TT
766 scm_puts ("#<unknown-", port);
767 scm_puts (hdr, port);
0f2d19dd
JB
768 if (SCM_CELLP (ptr))
769 {
b7f3516f 770 scm_puts (" (0x", port);
0f2d19dd 771 scm_intprint (SCM_CAR (ptr), 16, port);
b7f3516f 772 scm_puts (" . 0x", port);
0f2d19dd 773 scm_intprint (SCM_CDR (ptr), 16, port);
b7f3516f 774 scm_puts (") @", port);
0f2d19dd 775 }
b7f3516f 776 scm_puts (" 0x", port);
0f2d19dd 777 scm_intprint (ptr, 16, port);
b7f3516f 778 scm_putc ('>', port);
0f2d19dd
JB
779}
780
781/* Print a list.
782 */
a51ea417 783
1cc91f1b 784
0f2d19dd 785void
1bbd0b84 786scm_iprlist (char *hdr,SCM exp,int tlr,SCM port,scm_print_state *pstate)
0f2d19dd 787{
c62fbfe1
MD
788 register SCM hare, tortoise;
789 int floor = pstate->top - 2;
b7f3516f 790 scm_puts (hdr, port);
0f2d19dd 791 /* CHECK_INTS; */
c62fbfe1
MD
792 if (pstate->fancyp)
793 goto fancy_printing;
794
795 /* Run a hare and tortoise so that total time complexity will be
796 O(depth * N) instead of O(N^2). */
797 hare = SCM_CDR (exp);
798 tortoise = exp;
0c95b57d 799 while (SCM_ECONSP (hare))
c62fbfe1
MD
800 {
801 if (hare == tortoise)
802 goto fancy_printing;
803 hare = SCM_CDR (hare);
2fab3faa 804 if (SCM_IMP (hare) || SCM_NECONSP (hare))
c62fbfe1
MD
805 break;
806 hare = SCM_CDR (hare);
807 tortoise = SCM_CDR (tortoise);
808 }
809
810 /* No cdr cycles intrinsic to this list */
811 scm_iprin1 (SCM_CAR (exp), port, pstate);
0f2d19dd
JB
812 exp = SCM_CDR (exp);
813 for (; SCM_NIMP (exp); exp = SCM_CDR (exp))
814 {
5ca6dc39
JB
815 register int i;
816
0f2d19dd
JB
817 if (SCM_NECONSP (exp))
818 break;
c62fbfe1
MD
819 for (i = floor; i >= 0; --i)
820 if (pstate->ref_stack[i] == exp)
821 goto circref;
822 PUSH_REF (pstate, exp);
b7f3516f 823 scm_putc (' ', port);
0f2d19dd 824 /* CHECK_INTS; */
c62fbfe1 825 scm_iprin1 (SCM_CAR (exp), port, pstate);
0f2d19dd
JB
826 }
827 if (SCM_NNULLP (exp))
828 {
b7f3516f 829 scm_puts (" . ", port);
c62fbfe1 830 scm_iprin1 (exp, port, pstate);
0f2d19dd 831 }
c62fbfe1 832
a51ea417 833end:
b7f3516f 834 scm_putc (tlr, port);
c62fbfe1 835 pstate->top = floor + 2;
a51ea417 836 return;
c62fbfe1
MD
837
838fancy_printing:
839 {
840 int n = pstate->length;
841
842 scm_iprin1 (SCM_CAR (exp), port, pstate);
843 exp = SCM_CDR (exp); --n;
844 for (; SCM_NIMP (exp); exp = SCM_CDR (exp))
845 {
5ca6dc39
JB
846 register unsigned long i;
847
c62fbfe1
MD
848 if (SCM_NECONSP (exp))
849 break;
850 for (i = 0; i < pstate->top; ++i)
851 if (pstate->ref_stack[i] == exp)
852 goto fancy_circref;
853 if (pstate->fancyp)
854 {
855 if (n == 0)
856 {
b7f3516f 857 scm_puts (" ...", port);
c62fbfe1
MD
858 goto skip_tail;
859 }
860 else
861 --n;
862 }
863 PUSH_REF(pstate, exp);
864 ++pstate->list_offset;
b7f3516f 865 scm_putc (' ', port);
c62fbfe1
MD
866 /* CHECK_INTS; */
867 scm_iprin1 (SCM_CAR (exp), port, pstate);
868 }
869 }
870 if (SCM_NNULLP (exp))
871 {
b7f3516f 872 scm_puts (" . ", port);
c62fbfe1
MD
873 scm_iprin1 (exp, port, pstate);
874 }
875skip_tail:
876 pstate->list_offset -= pstate->top - floor - 2;
a51ea417 877 goto end;
a51ea417 878
c62fbfe1
MD
879fancy_circref:
880 pstate->list_offset -= pstate->top - floor - 2;
881
882circref:
b7f3516f 883 scm_puts (" . ", port);
c62fbfe1
MD
884 print_circref (port, pstate, exp);
885 goto end;
0f2d19dd
JB
886}
887
888\f
889
bb35f315
MV
890int
891scm_valid_oport_value_p (SCM val)
892{
368cf54d
GB
893 return (SCM_OPOUTPORTP (val)
894 || (SCM_PORT_WITH_PS_P (val)
895 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val))));
bb35f315
MV
896}
897
8b840115 898/* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
1cc91f1b 899
0f2d19dd 900SCM
1bbd0b84 901scm_write (SCM obj, SCM port)
0f2d19dd
JB
902{
903 if (SCM_UNBNDP (port))
904 port = scm_cur_outp;
3eb7e6ee
JB
905
906 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
bb35f315 907
a51ea417 908 scm_prin1 (obj, port, 1);
0f2d19dd
JB
909#ifdef HAVE_PIPE
910# ifdef EPIPE
911 if (EPIPE == errno)
912 scm_close_port (port);
913# endif
914#endif
915 return SCM_UNSPECIFIED;
916}
917
918
8b840115 919/* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
1cc91f1b 920
0f2d19dd 921SCM
1bbd0b84 922scm_display (SCM obj, SCM port)
0f2d19dd
JB
923{
924 if (SCM_UNBNDP (port))
925 port = scm_cur_outp;
3eb7e6ee
JB
926
927 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
bb35f315 928
a51ea417 929 scm_prin1 (obj, port, 0);
0f2d19dd
JB
930#ifdef HAVE_PIPE
931# ifdef EPIPE
932 if (EPIPE == errno)
933 scm_close_port (port);
934# endif
935#endif
936 return SCM_UNSPECIFIED;
937}
938
70d63753
GB
939
940SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
941 (SCM destination, SCM message, SCM args),
b380b885
MD
942 "Write MESSAGE to DESTINATION, defaulting to `current-output-port'.\n"
943 "MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,\n"
944 "the escapes are replaced with corresponding members of ARGS:\n"
945 "~A formats using `display' and ~S formats using `write'.\n"
946 "If DESTINATION is #t, then use the `current-output-port',\n"
947 "if DESTINATION is #f, then return a string containing the formatted text.\n"
948 "Does not add a trailing newline.")
70d63753
GB
949#define FUNC_NAME s_scm_simple_format
950{
951 SCM answer = SCM_UNSPECIFIED;
952 int fReturnString = 0;
953 int writingp;
954 char *start;
955 char *p;
956
957 if (SCM_BOOL_T == destination) {
958 destination = scm_cur_outp;
959 } else if (SCM_BOOL_F == destination) {
960 fReturnString = 1;
961 destination = scm_mkstrport (SCM_INUM0,
962 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
963 SCM_OPN | SCM_WRTNG,
964 FUNC_NAME);
965 } else {
966 SCM_VALIDATE_OPORT_VALUE (1,destination);
967 }
968 SCM_VALIDATE_STRING(2,message);
969 SCM_VALIDATE_LIST(3,args);
970
70d63753
GB
971 start = SCM_ROCHARS (message);
972 for (p = start; *p != '\0'; ++p)
973 if (*p == '~')
974 {
975 if (SCM_IMP (args) || SCM_NCONSP (args))
976 continue;
977
978 ++p;
979 if (*p == 'A')
980 writingp = 0;
981 else if (*p == 'S')
982 writingp = 1;
983 else
984 continue;
985
986 scm_lfwrite (start, p - start - 1, destination);
987 scm_prin1 (SCM_CAR (args), destination, writingp);
988 args = SCM_CDR (args);
989 start = p + 1;
990 }
991 scm_lfwrite (start, p - start, destination);
992
993 if (fReturnString)
994 answer = scm_strport_to_string (destination);
995
5d2d2ffc 996 return scm_return_first(answer,message);
70d63753
GB
997}
998#undef FUNC_NAME
999
1000
3b3b36dd 1001SCM_DEFINE (scm_newline, "newline", 0, 1, 0,
b450f070
GB
1002 (SCM port),
1003 "Send a newline to PORT.")
1bbd0b84 1004#define FUNC_NAME s_scm_newline
0f2d19dd
JB
1005{
1006 if (SCM_UNBNDP (port))
bb35f315 1007 port = scm_cur_outp;
3eb7e6ee 1008
3b3b36dd 1009 SCM_VALIDATE_OPORT_VALUE (1,port);
bb35f315 1010
0ef4ae82 1011 scm_putc ('\n', SCM_COERCE_OUTPORT (port));
0f2d19dd
JB
1012 return SCM_UNSPECIFIED;
1013}
1bbd0b84 1014#undef FUNC_NAME
0f2d19dd 1015
3b3b36dd 1016SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
b450f070
GB
1017 (SCM chr, SCM port),
1018 "Send character CHR to PORT.")
1bbd0b84 1019#define FUNC_NAME s_scm_write_char
0f2d19dd
JB
1020{
1021 if (SCM_UNBNDP (port))
bb35f315 1022 port = scm_cur_outp;
3eb7e6ee 1023
9a8351bc 1024 SCM_VALIDATE_ICHR (1,chr);
3b3b36dd 1025 SCM_VALIDATE_OPORT_VALUE (2,port);
bb35f315 1026
0ef4ae82 1027 scm_putc ((int) SCM_ICHR (chr), SCM_COERCE_OUTPORT (port));
0f2d19dd
JB
1028#ifdef HAVE_PIPE
1029# ifdef EPIPE
1030 if (EPIPE == errno)
1031 scm_close_port (port);
1032# endif
1033#endif
1034 return SCM_UNSPECIFIED;
1035}
1bbd0b84 1036#undef FUNC_NAME
0f2d19dd 1037
0f2d19dd
JB
1038\f
1039
bb35f315 1040/* Call back to Scheme code to do the printing of special objects
c19bc088
MD
1041 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1042 * containing PORT and PSTATE. This object can be used as the port for
1043 * display/write etc to continue the current print chain. The REVEALED
1044 * field of PSTATE is set to true to indicate that the print state has
1045 * escaped to Scheme and thus has to be freed by the GC.
1046 */
1047
1048long scm_tc16_port_with_ps;
1049
1050/* Print exactly as the port itself would */
1051
1052static int
1053print_port_with_ps (SCM obj, SCM port, scm_print_state *pstate)
1054{
1055 obj = SCM_PORT_WITH_PS_PORT (obj);
1056 return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate);
1057}
c4f37e80
MV
1058
1059SCM
1bbd0b84 1060scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate)
c4f37e80 1061{
c19bc088 1062 SCM pwps;
bb35f315 1063 SCM pair = scm_cons (port, pstate->handle);
c19bc088 1064 SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, pair);
bb35f315 1065 pstate->revealed = 1;
c19bc088
MD
1066 return scm_apply (proc, exp, scm_cons (pwps, scm_listofnull));
1067}
1068
a1ec6916 1069SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 2, 0, 0,
1bbd0b84 1070 (SCM port, SCM pstate),
b380b885 1071 "")
1bbd0b84 1072#define FUNC_NAME s_scm_port_with_print_state
c19bc088
MD
1073{
1074 SCM pwps;
3b3b36dd
GB
1075 SCM_VALIDATE_OPORT_VALUE (1,port);
1076 SCM_VALIDATE_PRINTSTATE (2,pstate);
c19bc088
MD
1077 port = SCM_COERCE_OUTPORT (port);
1078 SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, scm_cons (port, pstate));
1079 return pwps;
1080}
1bbd0b84 1081#undef FUNC_NAME
c19bc088 1082
a1ec6916 1083SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0,
1bbd0b84 1084 (SCM port),
b380b885 1085 "")
1bbd0b84 1086#define FUNC_NAME s_scm_get_print_state
c19bc088 1087{
368cf54d
GB
1088 if (SCM_PORT_WITH_PS_P (port))
1089 return SCM_PORT_WITH_PS_PS (port);
1090 if (SCM_OUTPORTP (port))
1091 return SCM_BOOL_F;
1bbd0b84 1092 RETURN_SCM_WTA (1,port);
c4f37e80 1093}
1bbd0b84 1094#undef FUNC_NAME
bb35f315 1095
c4f37e80 1096\f
1cc91f1b 1097
0f2d19dd
JB
1098void
1099scm_init_print ()
0f2d19dd 1100{
c19bc088 1101 SCM vtable, layout, type;
4bfdf158 1102
b7ff98dd 1103 scm_init_opts (scm_print_options, scm_print_opts, SCM_N_PRINT_OPTIONS);
4bfdf158
MD
1104 vtable = scm_make_vtable_vtable (scm_make_struct_layout (scm_nullstr),
1105 SCM_INUM0,
1106 SCM_EOL);
1107 layout = scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT));
c19bc088 1108 type = scm_make_struct (vtable, SCM_INUM0, SCM_LIST1 (layout));
e9cd0e47 1109 scm_set_struct_vtable_name_x (type, SCM_CAR (scm_intern0 ("print-state")));
c62fbfe1 1110 print_state_pool = scm_permanent_object (scm_cons (type, SCM_EOL));
c4f37e80 1111
bb35f315 1112 scm_print_state_vtable = type;
c4f37e80 1113
c19bc088
MD
1114 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1115 scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
1116 scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr);
1117 scm_set_smob_print (scm_tc16_port_with_ps, print_port_with_ps);
1118
0f2d19dd
JB
1119#include "print.x"
1120}