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