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