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