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