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