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