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