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