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