* list.c: Moved append docs to append! Thanks Dirk Hermann. Also,
[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 #include "strports.h"
62
63 #include "scm_validate.h"
64 #include "print.h"
65 \f
66
67 /* {Names of immediate symbols}
68 *
69 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
70 */
71
72 char *scm_isymnames[] =
73 {
74 /* This table must agree with the declarations */
75 "#@and",
76 "#@begin",
77 "#@case",
78 "#@cond",
79 "#@do",
80 "#@if",
81 "#@lambda",
82 "#@let",
83 "#@let*",
84 "#@letrec",
85 "#@or",
86 "#@quote",
87 "#@set!",
88 "#@define",
89 #if 0
90 "#@literal-variable-ref",
91 "#@literal-variable-set!",
92 #endif
93 "#@apply",
94 "#@call-with-current-continuation",
95
96 /* user visible ISYMS */
97 /* other keywords */
98 /* Flags */
99
100 "#f",
101 "#t",
102 "#<undefined>",
103 "#<eof>",
104 "()",
105 "#<unspecified>",
106 "#@dispatch",
107 "#@slot-ref",
108 "#@slot-set!",
109
110 /* Multi-language support */
111
112 "#@nil-cond",
113 "#@nil-ify",
114 "#@t-ify",
115 "#@0-cond",
116 "#@0-ify",
117 "#@1-ify",
118 "#@bind",
119
120 "#@delay",
121
122 "#<unbound>"
123 };
124
125 scm_option scm_print_opts[] = {
126 { SCM_OPTION_SCM, "closure-hook", SCM_BOOL_F,
127 "Hook for printing closures." },
128 { SCM_OPTION_BOOLEAN, "source", 0,
129 "Print closures with source." }
130 };
131
132 SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0,
133 (SCM setting),
134 "")
135 #define FUNC_NAME s_scm_print_options
136 {
137 SCM ans = scm_options (setting,
138 scm_print_opts,
139 SCM_N_PRINT_OPTIONS,
140 FUNC_NAME);
141 return ans;
142 }
143 #undef FUNC_NAME
144
145 \f
146 /* {Printing of Scheme Objects}
147 */
148
149 /* Detection of circular references.
150 *
151 * Due to other constraints in the implementation, this code has bad
152 * time complexity (O (depth * N)), The printer code will be
153 * completely rewritten before next release of Guile. The new code
154 * will be O(N).
155 */
156 #define PUSH_REF(pstate, obj) \
157 do { \
158 pstate->ref_stack[pstate->top++] = (obj); \
159 if (pstate->top == pstate->ceiling) \
160 grow_ref_stack (pstate); \
161 } while(0)
162
163 #define ENTER_NESTED_DATA(pstate, obj, label) \
164 do { \
165 register unsigned long i; \
166 for (i = 0; i < pstate->top; ++i) \
167 if (pstate->ref_stack[i] == (obj)) \
168 goto label; \
169 if (pstate->fancyp) \
170 { \
171 if (pstate->top - pstate->list_offset >= pstate->level) \
172 { \
173 scm_putc ('#', port); \
174 return; \
175 } \
176 } \
177 PUSH_REF(pstate, obj); \
178 } while(0)
179
180 #define EXIT_NESTED_DATA(pstate) { --pstate->top; }
181
182 SCM scm_print_state_vtable;
183
184 static SCM print_state_pool;
185
186 #ifdef GUILE_DEBUG /* Used for debugging purposes */
187
188 SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0,
189 (),
190 "Return the current-pstate--the `cadr' of the print_state_pool.\n"
191 "`current-pstate' is only included in GUILE_DEBUG builds.")
192 #define FUNC_NAME s_scm_current_pstate
193 {
194 return SCM_CADR (print_state_pool);
195 }
196 #undef FUNC_NAME
197
198 #endif
199
200 #define PSTATE_SIZE 50L
201
202 static SCM
203 make_print_state (void)
204 {
205 SCM print_state = scm_make_struct (SCM_CAR (print_state_pool), /* pstate type */
206 SCM_INUM0,
207 SCM_EOL);
208 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
209 pstate->ref_vect = scm_make_vector (SCM_MAKINUM (PSTATE_SIZE),
210 SCM_UNDEFINED);
211 pstate->ref_stack = SCM_VELTS (pstate->ref_vect);
212 pstate->ceiling = SCM_LENGTH (pstate->ref_vect);
213 return print_state;
214 }
215
216 SCM
217 scm_make_print_state ()
218 {
219 SCM answer = 0;
220
221 /* First try to allocate a print state from the pool */
222 SCM_DEFER_INTS;
223 if (SCM_NNULLP (SCM_CDR (print_state_pool)))
224 {
225 answer = SCM_CADR (print_state_pool);
226 SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool));
227 }
228 SCM_ALLOW_INTS;
229
230 return answer ? answer : make_print_state ();
231 }
232
233 void
234 scm_free_print_state (SCM print_state)
235 {
236 SCM handle;
237 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
238 /* Cleanup before returning print state to pool.
239 * It is better to do it here. Doing it in scm_prin1
240 * would cost more since that function is called much more
241 * often.
242 */
243 pstate->fancyp = 0;
244 pstate->revealed = 0;
245 SCM_NEWCELL (handle);
246 SCM_DEFER_INTS;
247 SCM_SETCAR (handle, print_state);
248 SCM_SETCDR (handle, SCM_CDR (print_state_pool));
249 SCM_SETCDR (print_state_pool, handle);
250 SCM_ALLOW_INTS;
251 }
252
253 static void
254 grow_ref_stack (scm_print_state *pstate)
255 {
256 int new_size = 2 * pstate->ceiling;
257 scm_vector_set_length_x (pstate->ref_vect, SCM_MAKINUM (new_size));
258 pstate->ref_stack = SCM_VELTS (pstate->ref_vect);
259 pstate->ceiling = new_size;
260 }
261
262
263 static void
264 print_circref (SCM port,scm_print_state *pstate,SCM ref)
265 {
266 register int i;
267 int self = pstate->top - 1;
268 i = pstate->top - 1;
269 if (SCM_CONSP (pstate->ref_stack[i]))
270 {
271 while (i > 0)
272 {
273 if (SCM_NCONSP (pstate->ref_stack[i - 1])
274 || SCM_CDR (pstate->ref_stack[i - 1]) != pstate->ref_stack[i])
275 break;
276 --i;
277 }
278 self = i;
279 }
280 for (i = pstate->top - 1; 1; --i)
281 if (pstate->ref_stack[i] == ref)
282 break;
283 scm_putc ('#', port);
284 scm_intprint (i - self, 10, port);
285 scm_putc ('#', port);
286 }
287
288 /* Print generally. Handles both write and display according to PSTATE.
289 */
290 SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write);
291 SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
292
293 void
294 scm_iprin1 (SCM exp, SCM port, 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_CHARP (exp))
305 {
306 register long i;
307
308 i = SCM_CHAR (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_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 #ifdef HAVE_ARRAYS
594 case scm_tc7_bvect:
595 case scm_tc7_byvect:
596 case scm_tc7_svect:
597 case scm_tc7_ivect:
598 case scm_tc7_uvect:
599 case scm_tc7_fvect:
600 case scm_tc7_dvect:
601 case scm_tc7_cvect:
602 #ifdef HAVE_LONG_LONGS
603 case scm_tc7_llvect:
604 #endif
605 scm_raprin1 (exp, port, pstate);
606 break;
607 #endif
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 (SCM exp, SCM port, int writingp)
705 {
706 SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
707 SCM pstate_scm;
708 scm_print_state *pstate;
709
710 /* If PORT is a print-state/port pair, use that. Else create a new
711 print-state. */
712
713 if (SCM_PORT_WITH_PS_P (port))
714 {
715 pstate_scm = SCM_PORT_WITH_PS_PS (port);
716 port = SCM_PORT_WITH_PS_PORT (port);
717 }
718 else
719 {
720 /* First try to allocate a print state from the pool */
721 SCM_DEFER_INTS;
722 if (SCM_NNULLP (SCM_CDR (print_state_pool)))
723 {
724 handle = SCM_CDR (print_state_pool);
725 SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool));
726 }
727 SCM_ALLOW_INTS;
728 if (handle == SCM_BOOL_F)
729 handle = scm_cons (make_print_state (), SCM_EOL);
730 pstate_scm = SCM_CAR (handle);
731 }
732
733 pstate = SCM_PRINT_STATE (pstate_scm);
734 pstate->writingp = writingp;
735 scm_iprin1 (exp, port, pstate);
736
737 /* Return print state to pool if it has been created above and
738 hasn't escaped to Scheme. */
739
740 if (handle != SCM_BOOL_F && !pstate->revealed)
741 {
742 SCM_DEFER_INTS;
743 SCM_SETCDR (handle, SCM_CDR (print_state_pool));
744 SCM_SETCDR (print_state_pool, handle);
745 SCM_ALLOW_INTS;
746 }
747 }
748
749
750 /* Print an integer.
751 */
752
753 void
754 scm_intprint (long n, int radix, SCM port)
755 {
756 char num_buf[SCM_INTBUFLEN];
757 scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port);
758 }
759
760 /* Print an object of unrecognized type.
761 */
762
763 void
764 scm_ipruk (char *hdr, SCM ptr, SCM port)
765 {
766 scm_puts ("#<unknown-", port);
767 scm_puts (hdr, port);
768 if (SCM_CELLP (ptr))
769 {
770 scm_puts (" (0x", port);
771 scm_intprint (SCM_CAR (ptr), 16, port);
772 scm_puts (" . 0x", port);
773 scm_intprint (SCM_CDR (ptr), 16, port);
774 scm_puts (") @", port);
775 }
776 scm_puts (" 0x", port);
777 scm_intprint (ptr, 16, port);
778 scm_putc ('>', port);
779 }
780
781 /* Print a list.
782 */
783
784
785 void
786 scm_iprlist (char *hdr,SCM exp,int tlr,SCM port,scm_print_state *pstate)
787 {
788 register SCM hare, tortoise;
789 int floor = pstate->top - 2;
790 scm_puts (hdr, port);
791 /* CHECK_INTS; */
792 if (pstate->fancyp)
793 goto fancy_printing;
794
795 /* Run a hare and tortoise so that total time complexity will be
796 O(depth * N) instead of O(N^2). */
797 hare = SCM_CDR (exp);
798 tortoise = exp;
799 while (SCM_ECONSP (hare))
800 {
801 if (hare == tortoise)
802 goto fancy_printing;
803 hare = SCM_CDR (hare);
804 if (SCM_IMP (hare) || SCM_NECONSP (hare))
805 break;
806 hare = SCM_CDR (hare);
807 tortoise = SCM_CDR (tortoise);
808 }
809
810 /* No cdr cycles intrinsic to this list */
811 scm_iprin1 (SCM_CAR (exp), port, pstate);
812 exp = SCM_CDR (exp);
813 for (; SCM_NIMP (exp); exp = SCM_CDR (exp))
814 {
815 register int i;
816
817 if (SCM_NECONSP (exp))
818 break;
819 for (i = floor; i >= 0; --i)
820 if (pstate->ref_stack[i] == exp)
821 goto circref;
822 PUSH_REF (pstate, exp);
823 scm_putc (' ', port);
824 /* CHECK_INTS; */
825 scm_iprin1 (SCM_CAR (exp), port, pstate);
826 }
827 if (SCM_NNULLP (exp))
828 {
829 scm_puts (" . ", port);
830 scm_iprin1 (exp, port, pstate);
831 }
832
833 end:
834 scm_putc (tlr, port);
835 pstate->top = floor + 2;
836 return;
837
838 fancy_printing:
839 {
840 int n = pstate->length;
841
842 scm_iprin1 (SCM_CAR (exp), port, pstate);
843 exp = SCM_CDR (exp); --n;
844 for (; SCM_NIMP (exp); exp = SCM_CDR (exp))
845 {
846 register unsigned long i;
847
848 if (SCM_NECONSP (exp))
849 break;
850 for (i = 0; i < pstate->top; ++i)
851 if (pstate->ref_stack[i] == exp)
852 goto fancy_circref;
853 if (pstate->fancyp)
854 {
855 if (n == 0)
856 {
857 scm_puts (" ...", port);
858 goto skip_tail;
859 }
860 else
861 --n;
862 }
863 PUSH_REF(pstate, exp);
864 ++pstate->list_offset;
865 scm_putc (' ', port);
866 /* CHECK_INTS; */
867 scm_iprin1 (SCM_CAR (exp), port, pstate);
868 }
869 }
870 if (SCM_NNULLP (exp))
871 {
872 scm_puts (" . ", port);
873 scm_iprin1 (exp, port, pstate);
874 }
875 skip_tail:
876 pstate->list_offset -= pstate->top - floor - 2;
877 goto end;
878
879 fancy_circref:
880 pstate->list_offset -= pstate->top - floor - 2;
881
882 circref:
883 scm_puts (" . ", port);
884 print_circref (port, pstate, exp);
885 goto end;
886 }
887
888 \f
889
890 int
891 scm_valid_oport_value_p (SCM val)
892 {
893 return (SCM_OPOUTPORTP (val)
894 || (SCM_PORT_WITH_PS_P (val)
895 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val))));
896 }
897
898 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
899
900 SCM
901 scm_write (SCM obj, SCM port)
902 {
903 if (SCM_UNBNDP (port))
904 port = scm_cur_outp;
905
906 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
907
908 scm_prin1 (obj, port, 1);
909 #ifdef HAVE_PIPE
910 # ifdef EPIPE
911 if (EPIPE == errno)
912 scm_close_port (port);
913 # endif
914 #endif
915 return SCM_UNSPECIFIED;
916 }
917
918
919 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
920
921 SCM
922 scm_display (SCM obj, SCM port)
923 {
924 if (SCM_UNBNDP (port))
925 port = scm_cur_outp;
926
927 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
928
929 scm_prin1 (obj, port, 0);
930 #ifdef HAVE_PIPE
931 # ifdef EPIPE
932 if (EPIPE == errno)
933 scm_close_port (port);
934 # endif
935 #endif
936 return SCM_UNSPECIFIED;
937 }
938
939
940 SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
941 (SCM destination, SCM message, SCM args),
942 "Write MESSAGE to DESTINATION, defaulting to `current-output-port'.\n"
943 "MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,\n"
944 "the escapes are replaced with corresponding members of ARGS:\n"
945 "~A formats using `display' and ~S formats using `write'.\n"
946 "If DESTINATION is #t, then use the `current-output-port',\n"
947 "if DESTINATION is #f, then return a string containing the formatted text.\n"
948 "Does not add a trailing newline.")
949 #define FUNC_NAME s_scm_simple_format
950 {
951 SCM answer = SCM_UNSPECIFIED;
952 int fReturnString = 0;
953 int writingp;
954 char *start;
955 char *p;
956
957 if (SCM_BOOL_T == destination) {
958 destination = scm_cur_outp;
959 } else if (SCM_BOOL_F == destination) {
960 fReturnString = 1;
961 destination = scm_mkstrport (SCM_INUM0,
962 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
963 SCM_OPN | SCM_WRTNG,
964 FUNC_NAME);
965 } else {
966 SCM_VALIDATE_OPORT_VALUE (1,destination);
967 }
968 SCM_VALIDATE_STRING(2,message);
969 SCM_VALIDATE_LIST(3,args);
970
971 start = SCM_ROCHARS (message);
972 for (p = start; *p != '\0'; ++p)
973 if (*p == '~')
974 {
975 if (SCM_IMP (args) || SCM_NCONSP (args))
976 continue;
977
978 ++p;
979 if (*p == 'A')
980 writingp = 0;
981 else if (*p == 'S')
982 writingp = 1;
983 else
984 continue;
985
986 scm_lfwrite (start, p - start - 1, destination);
987 scm_prin1 (SCM_CAR (args), destination, writingp);
988 args = SCM_CDR (args);
989 start = p + 1;
990 }
991 scm_lfwrite (start, p - start, destination);
992
993 if (fReturnString)
994 answer = scm_strport_to_string (destination);
995
996 return scm_return_first(answer,message);
997 }
998 #undef FUNC_NAME
999
1000
1001 SCM_DEFINE (scm_newline, "newline", 0, 1, 0,
1002 (SCM port),
1003 "Send a newline to PORT.")
1004 #define FUNC_NAME s_scm_newline
1005 {
1006 if (SCM_UNBNDP (port))
1007 port = scm_cur_outp;
1008
1009 SCM_VALIDATE_OPORT_VALUE (1,port);
1010
1011 scm_putc ('\n', SCM_COERCE_OUTPORT (port));
1012 return SCM_UNSPECIFIED;
1013 }
1014 #undef FUNC_NAME
1015
1016 SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
1017 (SCM chr, SCM port),
1018 "Send character CHR to PORT.")
1019 #define FUNC_NAME s_scm_write_char
1020 {
1021 if (SCM_UNBNDP (port))
1022 port = scm_cur_outp;
1023
1024 SCM_VALIDATE_CHAR (1,chr);
1025 SCM_VALIDATE_OPORT_VALUE (2,port);
1026
1027 scm_putc ((int) SCM_CHAR (chr), SCM_COERCE_OUTPORT (port));
1028 #ifdef HAVE_PIPE
1029 # ifdef EPIPE
1030 if (EPIPE == errno)
1031 scm_close_port (port);
1032 # endif
1033 #endif
1034 return SCM_UNSPECIFIED;
1035 }
1036 #undef FUNC_NAME
1037
1038 \f
1039
1040 /* Call back to Scheme code to do the printing of special objects
1041 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1042 * containing PORT and PSTATE. This object can be used as the port for
1043 * display/write etc to continue the current print chain. The REVEALED
1044 * field of PSTATE is set to true to indicate that the print state has
1045 * escaped to Scheme and thus has to be freed by the GC.
1046 */
1047
1048 long scm_tc16_port_with_ps;
1049
1050 /* Print exactly as the port itself would */
1051
1052 static int
1053 print_port_with_ps (SCM obj, SCM port, scm_print_state *pstate)
1054 {
1055 obj = SCM_PORT_WITH_PS_PORT (obj);
1056 return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate);
1057 }
1058
1059 SCM
1060 scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate)
1061 {
1062 SCM pwps;
1063 SCM pair = scm_cons (port, pstate->handle);
1064 SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, pair);
1065 pstate->revealed = 1;
1066 return scm_apply (proc, exp, scm_cons (pwps, scm_listofnull));
1067 }
1068
1069 SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 2, 0, 0,
1070 (SCM port, SCM pstate),
1071 "")
1072 #define FUNC_NAME s_scm_port_with_print_state
1073 {
1074 SCM pwps;
1075 SCM_VALIDATE_OPORT_VALUE (1,port);
1076 SCM_VALIDATE_PRINTSTATE (2,pstate);
1077 port = SCM_COERCE_OUTPORT (port);
1078 SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, scm_cons (port, pstate));
1079 return pwps;
1080 }
1081 #undef FUNC_NAME
1082
1083 SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0,
1084 (SCM port),
1085 "")
1086 #define FUNC_NAME s_scm_get_print_state
1087 {
1088 if (SCM_PORT_WITH_PS_P (port))
1089 return SCM_PORT_WITH_PS_PS (port);
1090 if (SCM_OUTPORTP (port))
1091 return SCM_BOOL_F;
1092 RETURN_SCM_WTA (1,port);
1093 }
1094 #undef FUNC_NAME
1095
1096 \f
1097
1098 void
1099 scm_init_print ()
1100 {
1101 SCM vtable, layout, type;
1102
1103 scm_init_opts (scm_print_options, scm_print_opts, SCM_N_PRINT_OPTIONS);
1104 vtable = scm_make_vtable_vtable (scm_make_struct_layout (scm_nullstr),
1105 SCM_INUM0,
1106 SCM_EOL);
1107 layout = scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT));
1108 type = scm_make_struct (vtable, SCM_INUM0, SCM_LIST1 (layout));
1109 scm_set_struct_vtable_name_x (type, SCM_CAR (scm_intern0 ("print-state")));
1110 print_state_pool = scm_permanent_object (scm_cons (type, SCM_EOL));
1111
1112 scm_print_state_vtable = type;
1113
1114 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1115 scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
1116 scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr);
1117 scm_set_smob_print (scm_tc16_port_with_ps, print_port_with_ps);
1118
1119 #include "print.x"
1120 }