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