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