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