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