(scm_iprin1): Print primitives generics always as "primitive-generic"
[bpt/guile.git] / libguile / print.c
1 /* Copyright (C) 1995-1999,2000,2001, 2002 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 #include "libguile/lang.h"
66
67 #include "libguile/validate.h"
68 #include "libguile/print.h"
69 \f
70
71 /* {Names of immediate symbols}
72 *
73 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
74 */
75
76 char *scm_isymnames[] =
77 {
78 /* This table must agree with the list of SCM_IM_ constants in tags.h */
79 "#@and",
80 "#@begin",
81 "#@case",
82 "#@cond",
83 "#@do",
84 "#@if",
85 "#@lambda",
86 "#@let",
87 "#@let*",
88 "#@letrec",
89 "#@or",
90 "#@quote",
91 "#@set!",
92 "#@define",
93 #if 0
94 "#@literal-variable-ref",
95 "#@literal-variable-set!",
96 #endif
97 "#@apply",
98 "#@call-with-current-continuation",
99
100 /* user visible ISYMS */
101 /* other keywords */
102 /* Flags */
103
104 "#f",
105 "#t",
106 "#<undefined>",
107 "#<eof>",
108 "()",
109 "#<unspecified>",
110 "#@dispatch",
111 "#@slot-ref",
112 "#@slot-set!",
113
114 /* Multi-language support */
115
116 "#@nil-cond",
117 "#@bind",
118
119 "#@delay",
120 "#@call-with-values",
121
122 "#<unbound>",
123
124 /* Elisp nil value. This is its Scheme name; whenever it's printed
125 in Elisp, it should appear as the symbol `nil'. */
126
127 "#nil"
128 };
129
130 scm_t_option 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 = SCM_BOOL_F;
190 static SCM print_state_pool = SCM_EOL;
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 car 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_NULLP (print_state_pool))
202 return SCM_CAR (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
216 = scm_make_struct (scm_print_state_vtable, SCM_INUM0, SCM_EOL);
217 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
218 pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED);
219 pstate->ref_stack = SCM_WRITABLE_VELTS (pstate->ref_vect);
220 pstate->ceiling = SCM_VECTOR_LENGTH (pstate->ref_vect);
221 return print_state;
222 }
223
224 SCM
225 scm_make_print_state ()
226 {
227 SCM answer = SCM_BOOL_F;
228
229 /* First try to allocate a print state from the pool */
230 SCM_DEFER_INTS;
231 if (!SCM_NULLP (print_state_pool))
232 {
233 answer = SCM_CAR (print_state_pool);
234 print_state_pool = SCM_CDR (print_state_pool);
235 }
236 SCM_ALLOW_INTS;
237
238 return SCM_FALSEP (answer) ? make_print_state () : answer;
239 }
240
241 void
242 scm_free_print_state (SCM print_state)
243 {
244 SCM handle;
245 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
246 /* Cleanup before returning print state to pool.
247 * It is better to do it here. Doing it in scm_prin1
248 * would cost more since that function is called much more
249 * often.
250 */
251 pstate->fancyp = 0;
252 pstate->revealed = 0;
253 SCM_DEFER_INTS;
254 handle = scm_cons (print_state, print_state_pool);
255 print_state_pool = handle;
256 SCM_ALLOW_INTS;
257 }
258
259 static void
260 grow_ref_stack (scm_print_state *pstate)
261 {
262 unsigned long int old_size = SCM_VECTOR_LENGTH (pstate->ref_vect);
263 SCM const *old_elts = SCM_VELTS (pstate->ref_vect);
264 unsigned long int new_size = 2 * pstate->ceiling;
265 SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED);
266 unsigned long int i;
267
268 for (i = 0; i != old_size; ++i)
269 SCM_VECTOR_SET (new_vect, i, old_elts [i]);
270
271 pstate->ref_vect = new_vect;
272 pstate->ref_stack = SCM_WRITABLE_VELTS(new_vect);
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 if (SCM_SYMBOL_INTERNED_P (exp))
530 {
531 scm_print_symbol_name (SCM_SYMBOL_CHARS (exp),
532 SCM_SYMBOL_LENGTH (exp),
533 port);
534 scm_remember_upto_here_1 (exp);
535 }
536 else
537 {
538 scm_puts ("#<uninterned-symbol ", port);
539 scm_print_symbol_name (SCM_SYMBOL_CHARS (exp),
540 SCM_SYMBOL_LENGTH (exp),
541 port);
542 scm_putc (' ', port);
543 scm_intprint ((long)exp, 16, port);
544 scm_putc ('>', port);
545 }
546 break;
547 case scm_tc7_variable:
548 scm_i_variable_print (exp, port, pstate);
549 break;
550 case scm_tc7_wvect:
551 ENTER_NESTED_DATA (pstate, exp, circref);
552 if (SCM_IS_WHVEC (exp))
553 scm_puts ("#wh(", port);
554 else
555 scm_puts ("#w(", port);
556 goto common_vector_printer;
557
558 case scm_tc7_vector:
559 ENTER_NESTED_DATA (pstate, exp, circref);
560 scm_puts ("#(", port);
561 common_vector_printer:
562 {
563 register long i;
564 long last = SCM_VECTOR_LENGTH (exp) - 1;
565 int cutp = 0;
566 if (pstate->fancyp && SCM_VECTOR_LENGTH (exp) > pstate->length)
567 {
568 last = pstate->length - 1;
569 cutp = 1;
570 }
571 for (i = 0; i < last; ++i)
572 {
573 /* CHECK_INTS; */
574 scm_iprin1 (SCM_VELTS (exp)[i], port, pstate);
575 scm_putc (' ', port);
576 }
577 if (i == last)
578 {
579 /* CHECK_INTS; */
580 scm_iprin1 (SCM_VELTS (exp)[i], port, pstate);
581 }
582 if (cutp)
583 scm_puts (" ...", port);
584 scm_putc (')', port);
585 }
586 EXIT_NESTED_DATA (pstate);
587 break;
588 #ifdef HAVE_ARRAYS
589 case scm_tc7_bvect:
590 case scm_tc7_byvect:
591 case scm_tc7_svect:
592 case scm_tc7_ivect:
593 case scm_tc7_uvect:
594 case scm_tc7_fvect:
595 case scm_tc7_dvect:
596 case scm_tc7_cvect:
597 #ifdef HAVE_LONG_LONGS
598 case scm_tc7_llvect:
599 #endif
600 scm_raprin1 (exp, port, pstate);
601 break;
602 #endif
603 case scm_tcs_subrs:
604 scm_puts (SCM_SUBR_GENERIC (exp)
605 ? "#<primitive-generic "
606 : "#<primitive-procedure ",
607 port);
608 scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp)), port);
609 scm_putc ('>', port);
610 break;
611 #ifdef CCLO
612 case scm_tc7_cclo:
613 {
614 SCM proc = SCM_CCLO_SUBR (exp);
615 if (SCM_EQ_P (proc, scm_f_gsubr_apply))
616 {
617 /* Print gsubrs as primitives */
618 SCM name = scm_procedure_name (exp);
619 scm_puts ("#<primitive-procedure", port);
620 if (!SCM_FALSEP (name))
621 {
622 scm_putc (' ', port);
623 scm_puts (SCM_SYMBOL_CHARS (name), port);
624 }
625 }
626 else
627 {
628 scm_puts ("#<compiled-closure ", port);
629 scm_iprin1 (proc, port, pstate);
630 }
631 scm_putc ('>', port);
632 }
633 break;
634 #endif
635 case scm_tc7_pws:
636 scm_puts ("#<procedure-with-setter", port);
637 {
638 SCM name = scm_procedure_name (exp);
639 if (!SCM_FALSEP (name))
640 {
641 scm_putc (' ', port);
642 scm_display (name, port);
643 }
644 }
645 scm_putc ('>', port);
646 break;
647 case scm_tc7_port:
648 {
649 register long i = SCM_PTOBNUM (exp);
650 if (i < scm_numptob
651 && scm_ptobs[i].print
652 && (scm_ptobs[i].print) (exp, port, pstate))
653 break;
654 goto punk;
655 }
656 case scm_tc7_smob:
657 ENTER_NESTED_DATA (pstate, exp, circref);
658 SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate);
659 EXIT_NESTED_DATA (pstate);
660 break;
661 default:
662 punk:
663 scm_ipruk ("type", exp, port);
664 }
665 }
666 }
667
668 /* Print states are necessary for circular reference safe printing.
669 * They are also expensive to allocate. Therefore print states are
670 * kept in a pool so that they can be reused.
671 */
672
673 /* The PORT argument can also be a print-state/port pair, which will
674 * then be used instead of allocating a new print state. This is
675 * useful for continuing a chain of print calls from Scheme. */
676
677 void
678 scm_prin1 (SCM exp, SCM port, int writingp)
679 {
680 SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
681 SCM pstate_scm;
682 scm_print_state *pstate;
683
684 /* If PORT is a print-state/port pair, use that. Else create a new
685 print-state. */
686
687 if (SCM_PORT_WITH_PS_P (port))
688 {
689 pstate_scm = SCM_PORT_WITH_PS_PS (port);
690 port = SCM_PORT_WITH_PS_PORT (port);
691 }
692 else
693 {
694 /* First try to allocate a print state from the pool */
695 SCM_DEFER_INTS;
696 if (!SCM_NULLP (print_state_pool))
697 {
698 handle = print_state_pool;
699 print_state_pool = SCM_CDR (print_state_pool);
700 }
701 SCM_ALLOW_INTS;
702 if (SCM_FALSEP (handle))
703 handle = scm_list_1 (make_print_state ());
704 pstate_scm = SCM_CAR (handle);
705 }
706
707 pstate = SCM_PRINT_STATE (pstate_scm);
708 pstate->writingp = writingp;
709 scm_iprin1 (exp, port, pstate);
710
711 /* Return print state to pool if it has been created above and
712 hasn't escaped to Scheme. */
713
714 if (!SCM_FALSEP (handle) && !pstate->revealed)
715 {
716 SCM_DEFER_INTS;
717 SCM_SETCDR (handle, print_state_pool);
718 print_state_pool = handle;
719 SCM_ALLOW_INTS;
720 }
721 }
722
723
724 /* Print an integer.
725 */
726
727 void
728 scm_intprint (long n, int radix, SCM port)
729 {
730 char num_buf[SCM_INTBUFLEN];
731 scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port);
732 }
733
734 /* Print an object of unrecognized type.
735 */
736
737 void
738 scm_ipruk (char *hdr, SCM ptr, SCM port)
739 {
740 scm_puts ("#<unknown-", port);
741 scm_puts (hdr, port);
742 if (scm_in_heap_p (ptr))
743 {
744 scm_puts (" (0x", port);
745 scm_intprint (SCM_CELL_WORD_0 (ptr), 16, port);
746 scm_puts (" . 0x", port);
747 scm_intprint (SCM_CELL_WORD_1 (ptr), 16, port);
748 scm_puts (") @", port);
749 }
750 scm_puts (" 0x", port);
751 scm_intprint (SCM_UNPACK (ptr), 16, port);
752 scm_putc ('>', port);
753 }
754
755
756 /* Print a list.
757 */
758 void
759 scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate)
760 {
761 register SCM hare, tortoise;
762 long floor = pstate->top - 2;
763 scm_puts (hdr, port);
764 /* CHECK_INTS; */
765 if (pstate->fancyp)
766 goto fancy_printing;
767
768 /* Run a hare and tortoise so that total time complexity will be
769 O(depth * N) instead of O(N^2). */
770 hare = SCM_CDR (exp);
771 tortoise = exp;
772 while (SCM_CONSP (hare))
773 {
774 if (SCM_EQ_P (hare, tortoise))
775 goto fancy_printing;
776 hare = SCM_CDR (hare);
777 if (!SCM_CONSP (hare))
778 break;
779 hare = SCM_CDR (hare);
780 tortoise = SCM_CDR (tortoise);
781 }
782
783 /* No cdr cycles intrinsic to this list */
784 scm_iprin1 (SCM_CAR (exp), port, pstate);
785 for (exp = SCM_CDR (exp); SCM_CONSP (exp); exp = SCM_CDR (exp))
786 {
787 register long i;
788
789 for (i = floor; i >= 0; --i)
790 if (SCM_EQ_P (pstate->ref_stack[i], exp))
791 goto circref;
792 PUSH_REF (pstate, exp);
793 scm_putc (' ', port);
794 /* CHECK_INTS; */
795 scm_iprin1 (SCM_CAR (exp), port, pstate);
796 }
797 if (!SCM_NULL_OR_NIL_P (exp))
798 {
799 scm_puts (" . ", port);
800 scm_iprin1 (exp, port, pstate);
801 }
802
803 end:
804 scm_putc (tlr, port);
805 pstate->top = floor + 2;
806 return;
807
808 fancy_printing:
809 {
810 long n = pstate->length;
811
812 scm_iprin1 (SCM_CAR (exp), port, pstate);
813 exp = SCM_CDR (exp); --n;
814 for (; SCM_CONSP (exp); exp = SCM_CDR (exp))
815 {
816 register unsigned long i;
817
818 for (i = 0; i < pstate->top; ++i)
819 if (SCM_EQ_P (pstate->ref_stack[i], exp))
820 goto fancy_circref;
821 if (pstate->fancyp)
822 {
823 if (n == 0)
824 {
825 scm_puts (" ...", port);
826 goto skip_tail;
827 }
828 else
829 --n;
830 }
831 PUSH_REF(pstate, exp);
832 ++pstate->list_offset;
833 scm_putc (' ', port);
834 /* CHECK_INTS; */
835 scm_iprin1 (SCM_CAR (exp), port, pstate);
836 }
837 }
838 if (!SCM_NULL_OR_NIL_P (exp))
839 {
840 scm_puts (" . ", port);
841 scm_iprin1 (exp, port, pstate);
842 }
843 skip_tail:
844 pstate->list_offset -= pstate->top - floor - 2;
845 goto end;
846
847 fancy_circref:
848 pstate->list_offset -= pstate->top - floor - 2;
849
850 circref:
851 scm_puts (" . ", port);
852 print_circref (port, pstate, exp);
853 goto end;
854 }
855
856 \f
857
858 int
859 scm_valid_oport_value_p (SCM val)
860 {
861 return (SCM_OPOUTPORTP (val)
862 || (SCM_PORT_WITH_PS_P (val)
863 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val))));
864 }
865
866 /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
867
868 SCM
869 scm_write (SCM obj, SCM port)
870 {
871 if (SCM_UNBNDP (port))
872 port = scm_cur_outp;
873
874 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
875
876 scm_prin1 (obj, port, 1);
877 #ifdef HAVE_PIPE
878 # ifdef EPIPE
879 if (EPIPE == errno)
880 scm_close_port (port);
881 # endif
882 #endif
883 return SCM_UNSPECIFIED;
884 }
885
886
887 /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
888
889 SCM
890 scm_display (SCM obj, SCM port)
891 {
892 if (SCM_UNBNDP (port))
893 port = scm_cur_outp;
894
895 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
896
897 scm_prin1 (obj, port, 0);
898 #ifdef HAVE_PIPE
899 # ifdef EPIPE
900 if (EPIPE == errno)
901 scm_close_port (port);
902 # endif
903 #endif
904 return SCM_UNSPECIFIED;
905 }
906
907
908 SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
909 (SCM destination, SCM message, SCM args),
910 "Write @var{message} to @var{destination}, defaulting to\n"
911 "the current output port.\n"
912 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
913 "@code{~S} (was @code{%S}) escapes. When printed,\n"
914 "the escapes are replaced with corresponding members of\n"
915 "@var{ARGS}:\n"
916 "@code{~A} formats using @code{display} and @code{~S} formats\n"
917 "using @code{write}.\n"
918 "If @var{destination} is @code{#t}, then use the current output\n"
919 "port, if @var{destination} is @code{#f}, then return a string\n"
920 "containing the formatted text. Does not add a trailing newline.")
921 #define FUNC_NAME s_scm_simple_format
922 {
923 SCM answer = SCM_UNSPECIFIED;
924 int fReturnString = 0;
925 int writingp;
926 char *start;
927 char *end;
928 char *p;
929
930 if (SCM_EQ_P (destination, SCM_BOOL_T))
931 {
932 destination = scm_cur_outp;
933 }
934 else if (SCM_FALSEP (destination))
935 {
936 fReturnString = 1;
937 destination = scm_mkstrport (SCM_INUM0,
938 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
939 SCM_OPN | SCM_WRTNG,
940 FUNC_NAME);
941 }
942 else
943 {
944 SCM_VALIDATE_OPORT_VALUE (1, destination);
945 destination = SCM_COERCE_OUTPORT (destination);
946 }
947 SCM_VALIDATE_STRING (2, message);
948 SCM_VALIDATE_REST_ARGUMENT (args);
949
950 start = SCM_STRING_CHARS (message);
951 end = start + SCM_STRING_LENGTH (message);
952 for (p = start; p != end; ++p)
953 if (*p == '~')
954 {
955 if (++p == end)
956 break;
957
958 switch (*p)
959 {
960 case 'A': case 'a':
961 writingp = 0;
962 break;
963 case 'S': case 's':
964 writingp = 1;
965 break;
966 case '~':
967 scm_lfwrite (start, p - start, destination);
968 start = p + 1;
969 continue;
970 case '%':
971 scm_lfwrite (start, p - start - 1, destination);
972 scm_newline (destination);
973 start = p + 1;
974 continue;
975 default:
976 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
977 scm_list_1 (SCM_MAKE_CHAR (*p)));
978
979 }
980
981
982 if (!SCM_CONSP (args))
983 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
984 scm_list_1 (SCM_MAKE_CHAR (*p)));
985
986 scm_lfwrite (start, p - start - 1, destination);
987 scm_prin1 (SCM_CAR (args), destination, writingp);
988 args = SCM_CDR (args);
989 start = p + 1;
990 }
991
992 scm_lfwrite (start, p - start, destination);
993 if (!SCM_EQ_P (args, SCM_EOL))
994 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
995 scm_list_1 (scm_length (args)));
996
997 if (fReturnString)
998 answer = scm_strport_to_string (destination);
999
1000 return scm_return_first (answer, message);
1001 }
1002 #undef FUNC_NAME
1003
1004
1005 SCM_DEFINE (scm_newline, "newline", 0, 1, 0,
1006 (SCM port),
1007 "Send a newline to @var{port}.\n"
1008 "If @var{port} is omitted, send to the current output port.")
1009 #define FUNC_NAME s_scm_newline
1010 {
1011 if (SCM_UNBNDP (port))
1012 port = scm_cur_outp;
1013
1014 SCM_VALIDATE_OPORT_VALUE (1, port);
1015
1016 scm_putc ('\n', SCM_COERCE_OUTPORT (port));
1017 return SCM_UNSPECIFIED;
1018 }
1019 #undef FUNC_NAME
1020
1021 SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
1022 (SCM chr, SCM port),
1023 "Send character @var{chr} to @var{port}.")
1024 #define FUNC_NAME s_scm_write_char
1025 {
1026 if (SCM_UNBNDP (port))
1027 port = scm_cur_outp;
1028
1029 SCM_VALIDATE_CHAR (1, chr);
1030 SCM_VALIDATE_OPORT_VALUE (2, port);
1031
1032 scm_putc ((int) SCM_CHAR (chr), SCM_COERCE_OUTPORT (port));
1033 #ifdef HAVE_PIPE
1034 # ifdef EPIPE
1035 if (EPIPE == errno)
1036 scm_close_port (port);
1037 # endif
1038 #endif
1039 return SCM_UNSPECIFIED;
1040 }
1041 #undef FUNC_NAME
1042
1043 \f
1044
1045 /* Call back to Scheme code to do the printing of special objects
1046 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1047 * containing PORT and PSTATE. This object can be used as the port for
1048 * display/write etc to continue the current print chain. The REVEALED
1049 * field of PSTATE is set to true to indicate that the print state has
1050 * escaped to Scheme and thus has to be freed by the GC.
1051 */
1052
1053 scm_t_bits scm_tc16_port_with_ps;
1054
1055 /* Print exactly as the port itself would */
1056
1057 static int
1058 port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate)
1059 {
1060 obj = SCM_PORT_WITH_PS_PORT (obj);
1061 return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate);
1062 }
1063
1064 SCM
1065 scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate)
1066 {
1067 SCM pwps;
1068 SCM pair = scm_cons (port, pstate->handle);
1069 SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, SCM_UNPACK (pair));
1070 pstate->revealed = 1;
1071 return scm_call_2 (proc, exp, pwps);
1072 }
1073
1074 SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 2, 0, 0,
1075 (SCM port, SCM pstate),
1076 "Create a new port which behaves like @var{port}, but with an\n"
1077 "included print state @var{pstate}.")
1078 #define FUNC_NAME s_scm_port_with_print_state
1079 {
1080 SCM pwps;
1081 SCM_VALIDATE_OPORT_VALUE (1, port);
1082 SCM_VALIDATE_PRINTSTATE (2, pstate);
1083 port = SCM_COERCE_OUTPORT (port);
1084 SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, SCM_UNPACK (scm_cons (port, pstate)));
1085 return pwps;
1086 }
1087 #undef FUNC_NAME
1088
1089 SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0,
1090 (SCM port),
1091 "Return the print state of the port @var{port}. If @var{port}\n"
1092 "has no associated print state, @code{#f} is returned.")
1093 #define FUNC_NAME s_scm_get_print_state
1094 {
1095 if (SCM_PORT_WITH_PS_P (port))
1096 return SCM_PORT_WITH_PS_PS (port);
1097 if (SCM_OUTPUT_PORT_P (port))
1098 return SCM_BOOL_F;
1099 SCM_WRONG_TYPE_ARG (1, port);
1100 }
1101 #undef FUNC_NAME
1102
1103 \f
1104
1105 void
1106 scm_init_print ()
1107 {
1108 SCM vtable, layout, type;
1109
1110 scm_init_opts (scm_print_options, scm_print_opts, SCM_N_PRINT_OPTIONS);
1111
1112 scm_gc_register_root (&print_state_pool);
1113 scm_gc_register_root (&scm_print_state_vtable);
1114 vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL);
1115 layout = scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT));
1116 type = scm_make_struct (vtable, SCM_INUM0, scm_list_1 (layout));
1117 scm_set_struct_vtable_name_x (type, scm_str2symbol ("print-state"));
1118 scm_print_state_vtable = type;
1119
1120 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1121 scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
1122 scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr);
1123 scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print);
1124
1125 #include "libguile/print.x"
1126 }
1127
1128 /*
1129 Local Variables:
1130 c-file-style: "gnu"
1131 End:
1132 */