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