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