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