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