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