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