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