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