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