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