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