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