Update `.gitignore'.
[bpt/guile.git] / libguile / print.c
... / ...
CommitLineData
1/* Copyright (C) 1995-1999,2000,2001, 2002, 2003, 2004, 2006, 2008, 2009 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19
20\f
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#include <errno.h>
26#include <uniconv.h>
27#include <unictype.h>
28
29#include "libguile/_scm.h"
30#include "libguile/chars.h"
31#include "libguile/continuations.h"
32#include "libguile/smob.h"
33#include "libguile/eval.h"
34#include "libguile/macros.h"
35#include "libguile/procprop.h"
36#include "libguile/read.h"
37#include "libguile/weaks.h"
38#include "libguile/programs.h"
39#include "libguile/alist.h"
40#include "libguile/struct.h"
41#include "libguile/ports.h"
42#include "libguile/root.h"
43#include "libguile/strings.h"
44#include "libguile/strports.h"
45#include "libguile/vectors.h"
46#include "libguile/lang.h"
47#include "libguile/numbers.h"
48
49#include "libguile/validate.h"
50#include "libguile/print.h"
51
52#include "libguile/private-options.h"
53
54\f
55
56/* {Names of immediate symbols}
57 *
58 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
59 */
60
61/* This table must agree with the list of flags in tags.h. */
62static const char *iflagnames[] =
63{
64 "#f",
65 "#nil", /* Elisp nil value. Should print from elisp as symbol `nil'. */
66 "#<XXX UNUSED LISP FALSE -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
67 "()",
68 "#t",
69 "#<XXX UNUSED BOOLEAN -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
70 "#<unspecified>",
71 "#<undefined>",
72 "#<eof>",
73
74 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
75 "#<unbound>",
76};
77
78SCM_SYMBOL (sym_reader, "reader");
79
80scm_t_option scm_print_opts[] = {
81 { SCM_OPTION_SCM, "closure-hook", (unsigned long) SCM_BOOL_F,
82 "Hook for printing closures (should handle macros as well)." },
83 { SCM_OPTION_BOOLEAN, "source", 0,
84 "Print closures with source." },
85 { SCM_OPTION_SCM, "highlight-prefix", (unsigned long)SCM_BOOL_F,
86 "The string to print before highlighted values." },
87 { SCM_OPTION_SCM, "highlight-suffix", (unsigned long)SCM_BOOL_F,
88 "The string to print after highlighted values." },
89 { SCM_OPTION_SCM, "quote-keywordish-symbols", (unsigned long)SCM_BOOL_F,
90 "How to print symbols that have a colon as their first or last character. "
91 "The value '#f' does not quote the colons; '#t' quotes them; "
92 "'reader' quotes them when the reader option 'keywords' is not '#f'."
93 },
94 { 0 },
95};
96
97SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0,
98 (SCM setting),
99 "Option interface for the print options. Instead of using\n"
100 "this procedure directly, use the procedures\n"
101 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
102 "and @code{print-options}.")
103#define FUNC_NAME s_scm_print_options
104{
105 SCM ans = scm_options (setting,
106 scm_print_opts,
107 FUNC_NAME);
108 return ans;
109}
110#undef FUNC_NAME
111
112\f
113/* {Printing of Scheme Objects}
114 */
115
116/* Detection of circular references.
117 *
118 * Due to other constraints in the implementation, this code has bad
119 * time complexity (O (depth * N)), The printer code can be
120 * rewritten to be O(N).
121 */
122#define PUSH_REF(pstate, obj) \
123do \
124{ \
125 PSTATE_STACK_SET (pstate, pstate->top, obj); \
126 pstate->top++; \
127 if (pstate->top == pstate->ceiling) \
128 grow_ref_stack (pstate); \
129} while(0)
130
131#define ENTER_NESTED_DATA(pstate, obj, label) \
132do \
133{ \
134 register unsigned long i; \
135 for (i = 0; i < pstate->top; ++i) \
136 if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
137 goto label; \
138 if (pstate->fancyp) \
139 { \
140 if (pstate->top - pstate->list_offset >= pstate->level) \
141 { \
142 scm_putc ('#', port); \
143 return; \
144 } \
145 } \
146 PUSH_REF(pstate, obj); \
147} while(0)
148
149#define EXIT_NESTED_DATA(pstate) \
150do \
151{ \
152 --pstate->top; \
153 PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
154} \
155while (0)
156
157SCM scm_print_state_vtable = SCM_BOOL_F;
158static SCM print_state_pool = SCM_EOL;
159scm_i_pthread_mutex_t print_state_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
160
161#ifdef GUILE_DEBUG /* Used for debugging purposes */
162
163SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0,
164 (),
165 "Return the current-pstate -- the car of the\n"
166 "@code{print_state_pool}. @code{current-pstate} is only\n"
167 "included in @code{--enable-guile-debug} builds.")
168#define FUNC_NAME s_scm_current_pstate
169{
170 if (!scm_is_null (print_state_pool))
171 return SCM_CAR (print_state_pool);
172 else
173 return SCM_BOOL_F;
174}
175#undef FUNC_NAME
176
177#endif
178
179#define PSTATE_SIZE 50L
180
181static SCM
182make_print_state (void)
183{
184 SCM print_state
185 = scm_make_struct (scm_print_state_vtable, SCM_INUM0, SCM_EOL);
186 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
187 pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED);
188 pstate->ceiling = SCM_SIMPLE_VECTOR_LENGTH (pstate->ref_vect);
189 pstate->highlight_objects = SCM_EOL;
190 return print_state;
191}
192
193SCM
194scm_make_print_state ()
195{
196 SCM answer = SCM_BOOL_F;
197
198 /* First try to allocate a print state from the pool */
199 scm_i_pthread_mutex_lock (&print_state_mutex);
200 if (!scm_is_null (print_state_pool))
201 {
202 answer = SCM_CAR (print_state_pool);
203 print_state_pool = SCM_CDR (print_state_pool);
204 }
205 scm_i_pthread_mutex_unlock (&print_state_mutex);
206
207 return scm_is_false (answer) ? make_print_state () : answer;
208}
209
210void
211scm_free_print_state (SCM print_state)
212{
213 SCM handle;
214 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
215 /* Cleanup before returning print state to pool.
216 * It is better to do it here. Doing it in scm_prin1
217 * would cost more since that function is called much more
218 * often.
219 */
220 pstate->fancyp = 0;
221 pstate->revealed = 0;
222 pstate->highlight_objects = SCM_EOL;
223 scm_i_pthread_mutex_lock (&print_state_mutex);
224 handle = scm_cons (print_state, print_state_pool);
225 print_state_pool = handle;
226 scm_i_pthread_mutex_unlock (&print_state_mutex);
227}
228
229SCM
230scm_i_port_with_print_state (SCM port, SCM print_state)
231{
232 if (SCM_UNBNDP (print_state))
233 {
234 if (SCM_PORT_WITH_PS_P (port))
235 return port;
236 else
237 print_state = scm_make_print_state ();
238 /* port does not need to be coerced since it doesn't have ps */
239 }
240 else
241 port = SCM_COERCE_OUTPORT (port);
242 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps,
243 SCM_UNPACK (scm_cons (port, print_state)));
244}
245
246static void
247grow_ref_stack (scm_print_state *pstate)
248{
249 SCM old_vect = pstate->ref_vect;
250 size_t old_size = SCM_SIMPLE_VECTOR_LENGTH (old_vect);
251 size_t new_size = 2 * pstate->ceiling;
252 SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED);
253 unsigned long int i;
254
255 for (i = 0; i != old_size; ++i)
256 SCM_SIMPLE_VECTOR_SET (new_vect, i, SCM_SIMPLE_VECTOR_REF (old_vect, i));
257
258 pstate->ref_vect = new_vect;
259 pstate->ceiling = new_size;
260}
261
262#define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
263#define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
264
265static void
266print_circref (SCM port, scm_print_state *pstate, SCM ref)
267{
268 register long i;
269 long self = pstate->top - 1;
270 i = pstate->top - 1;
271 if (scm_is_pair (PSTATE_STACK_REF (pstate, i)))
272 {
273 while (i > 0)
274 {
275 if (!scm_is_pair (PSTATE_STACK_REF (pstate, i-1))
276 || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate, i-1)),
277 SCM_CDR (PSTATE_STACK_REF (pstate, i))))
278 break;
279 --i;
280 }
281 self = i;
282 }
283 for (i = pstate->top - 1; 1; --i)
284 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), ref))
285 break;
286 scm_putc ('#', port);
287 scm_intprint (i - self, 10, port);
288 scm_putc ('#', port);
289}
290
291/* Print the name of a symbol. */
292
293static int
294quote_keywordish_symbol (SCM symbol)
295{
296 SCM option;
297
298 if (scm_i_symbol_ref (symbol, 0) != ':'
299 && scm_i_symbol_ref (symbol, scm_i_symbol_length (symbol) - 1) != ':')
300 return 0;
301
302 option = SCM_PRINT_KEYWORD_STYLE;
303 if (scm_is_false (option))
304 return 0;
305 if (scm_is_eq (option, sym_reader))
306 return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE));
307 return 1;
308}
309
310void
311scm_i_print_symbol_name (SCM str, SCM port)
312{
313 /* This points to the first character that has not yet been written to the
314 * port. */
315 size_t pos = 0;
316 /* This points to the character we're currently looking at. */
317 size_t end;
318 /* If the name contains weird characters, we'll escape them with
319 * backslashes and set this flag; it indicates that we should surround the
320 * name with "#{" and "}#". */
321 int weird = 0;
322 /* Backslashes are not sufficient to make a name weird, but if a name is
323 * weird because of other characters, backslahes need to be escaped too.
324 * The first time we see a backslash, we set maybe_weird, and mw_pos points
325 * to the backslash. Then if the name turns out to be weird, we re-process
326 * everything starting from mw_pos.
327 * We could instead make backslashes always weird. This is not necessary
328 * to ensure that the output is (read)-able, but it would make this code
329 * simpler and faster. */
330 int maybe_weird = 0;
331 size_t mw_pos = 0;
332 size_t len = scm_i_symbol_length (str);
333 scm_t_wchar str0 = scm_i_symbol_ref (str, 0);
334
335 if (len == 0 || str0 == '\'' || str0 == '`' || str0 == ','
336 || quote_keywordish_symbol (str)
337 || (str0 == '.' && len == 1)
338 || scm_is_true (scm_i_string_to_number (scm_symbol_to_string (str), 10)))
339 {
340 scm_lfwrite ("#{", 2, port);
341 weird = 1;
342 }
343
344 for (end = pos; end < len; ++end)
345 switch (scm_i_symbol_ref (str, end))
346 {
347#ifdef BRACKETS_AS_PARENS
348 case '[':
349 case ']':
350#endif
351 case '(':
352 case ')':
353 case '"':
354 case ';':
355 case '#':
356 case SCM_WHITE_SPACES:
357 case SCM_LINE_INCREMENTORS:
358 weird_handler:
359 if (maybe_weird)
360 {
361 end = mw_pos;
362 maybe_weird = 0;
363 }
364 if (!weird)
365 {
366 scm_lfwrite ("#{", 2, port);
367 weird = 1;
368 }
369 if (pos < end)
370 scm_lfwrite_substr (scm_symbol_to_string (str), pos, end, port);
371 {
372 char buf[2];
373 buf[0] = '\\';
374 buf[1] = (char) (unsigned char) scm_i_symbol_ref (str, end);
375 scm_lfwrite (buf, 2, port);
376 }
377 pos = end + 1;
378 break;
379 case '\\':
380 if (weird)
381 goto weird_handler;
382 if (!maybe_weird)
383 {
384 maybe_weird = 1;
385 mw_pos = pos;
386 }
387 break;
388 default:
389 break;
390 }
391 if (pos < end)
392 scm_lfwrite_substr (scm_symbol_to_string (str), pos, end, port);
393 if (weird)
394 scm_lfwrite ("}#", 2, port);
395}
396
397void
398scm_print_symbol_name (const char *str, size_t len, SCM port)
399{
400 SCM symbol = scm_from_locale_symboln (str, len);
401 scm_i_print_symbol_name (symbol, port);
402}
403
404/* Print generally. Handles both write and display according to PSTATE.
405 */
406SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write);
407SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
408
409static void iprin1 (SCM exp, SCM port, scm_print_state *pstate);
410
411void
412scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate)
413{
414 if (pstate->fancyp
415 && scm_is_true (scm_memq (exp, pstate->highlight_objects)))
416 {
417 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX, port);
418 iprin1 (exp, port, pstate);
419 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX, port);
420 }
421 else
422 iprin1 (exp, port, pstate);
423}
424
425static void
426iprin1 (SCM exp, SCM port, scm_print_state *pstate)
427{
428 switch (SCM_ITAG3 (exp))
429 {
430 case scm_tc3_tc7_1:
431 case scm_tc3_tc7_2:
432 /* These tc3 tags should never occur in an immediate value. They are
433 * only used in cell types of non-immediates, i. e. the value returned
434 * by SCM_CELL_TYPE (exp) can use these tags.
435 */
436 scm_ipruk ("immediate", exp, port);
437 break;
438 case scm_tc3_int_1:
439 case scm_tc3_int_2:
440 scm_intprint (SCM_I_INUM (exp), 10, port);
441 break;
442 case scm_tc3_imm24:
443 if (SCM_CHARP (exp))
444 {
445 scm_t_wchar i = SCM_CHAR (exp);
446 const char *name;
447
448 if (SCM_WRITINGP (pstate))
449 {
450 scm_puts ("#\\", port);
451 name = scm_i_charname (exp);
452 if (name != NULL)
453 scm_puts (name, port);
454 else if (uc_is_general_category_withtable (i, UC_CATEGORY_MASK_L
455 | UC_CATEGORY_MASK_M
456 | UC_CATEGORY_MASK_N
457 | UC_CATEGORY_MASK_P
458 | UC_CATEGORY_MASK_S))
459 /* Print the character if is graphic character. */
460 {
461 scm_t_wchar *wbuf;
462 SCM wstr;
463 char *buf;
464 size_t len;
465 const char *enc;
466
467 enc = scm_i_get_port_encoding (port);
468 if (uc_combining_class (i) == UC_CCC_NR)
469 {
470 wstr = scm_i_make_wide_string (1, &wbuf);
471 wbuf[0] = i;
472 }
473 else
474 {
475 /* Character is a combining character: print it connected
476 to a dotted circle instead of connecting it to the
477 backslash in '#\' */
478 wstr = scm_i_make_wide_string (2, &wbuf);
479 wbuf[0] = SCM_CODEPOINT_DOTTED_CIRCLE;
480 wbuf[1] = i;
481 }
482 if (enc == NULL)
483 {
484 if (i <= 0xFF)
485 /* Character is graphic and Latin-1. Print it */
486 scm_lfwrite_str (wstr, port);
487 else
488 /* Character is graphic but unrepresentable in
489 this port's encoding. */
490 scm_intprint (i, 8, port);
491 }
492 else
493 {
494 buf = u32_conv_to_encoding (enc,
495 iconveh_error,
496 (scm_t_uint32 *) wbuf,
497 1,
498 NULL,
499 NULL, &len);
500 if (buf != NULL)
501 {
502 /* Character is graphic. Print it. */
503 scm_lfwrite_str (wstr, port);
504 free (buf);
505 }
506 else
507 /* Character is graphic but unrepresentable in
508 this port's encoding. */
509 scm_intprint (i, 8, port);
510 }
511 }
512 else
513 /* Character is a non-graphical character. */
514 scm_intprint (i, 8, port);
515 }
516 else
517 scm_i_charprint (i, port);
518 }
519 else if (SCM_IFLAGP (exp)
520 && ((size_t) SCM_IFLAGNUM (exp) < (sizeof iflagnames / sizeof (char *))))
521 {
522 scm_puts (iflagnames [SCM_IFLAGNUM (exp)], port);
523 }
524 else
525 {
526 /* unknown immediate value */
527 scm_ipruk ("immediate", exp, port);
528 }
529 break;
530 case scm_tc3_cons:
531 switch (SCM_TYP7 (exp))
532 {
533 case scm_tcs_struct:
534 {
535 ENTER_NESTED_DATA (pstate, exp, circref);
536 if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS)
537 {
538 SCM pwps, print = pstate->writingp ? g_write : g_display;
539 if (!print)
540 goto print_struct;
541 pwps = scm_i_port_with_print_state (port, pstate->handle);
542 pstate->revealed = 1;
543 scm_call_generic_2 (print, exp, pwps);
544 }
545 else
546 {
547 print_struct:
548 scm_print_struct (exp, port, pstate);
549 }
550 EXIT_NESTED_DATA (pstate);
551 }
552 break;
553 case scm_tcs_cons_imcar:
554 case scm_tcs_cons_nimcar:
555 ENTER_NESTED_DATA (pstate, exp, circref);
556 scm_iprlist ("(", exp, ')', port, pstate);
557 EXIT_NESTED_DATA (pstate);
558 break;
559 circref:
560 print_circref (port, pstate, exp);
561 break;
562 case scm_tc7_number:
563 switch SCM_TYP16 (exp) {
564 case scm_tc16_big:
565 scm_bigprint (exp, port, pstate);
566 break;
567 case scm_tc16_real:
568 scm_print_real (exp, port, pstate);
569 break;
570 case scm_tc16_complex:
571 scm_print_complex (exp, port, pstate);
572 break;
573 case scm_tc16_fraction:
574 scm_i_print_fraction (exp, port, pstate);
575 break;
576 }
577 break;
578 case scm_tc7_string:
579 if (SCM_WRITINGP (pstate))
580 {
581 size_t i, j, len;
582 static char const hex[] = "0123456789abcdef";
583 char buf[8];
584
585
586 scm_putc ('"', port);
587 len = scm_i_string_length (exp);
588 for (i = 0; i < len; ++i)
589 {
590 scm_t_wchar ch = scm_i_string_ref (exp, i);
591 int printed = 0;
592
593 if (ch == ' ' || ch == '\n')
594 {
595 scm_putc (ch, port);
596 printed = 1;
597 }
598 else if (ch == '"' || ch == '\\')
599 {
600 scm_putc ('\\', port);
601 scm_i_charprint (ch, port);
602 printed = 1;
603 }
604 else
605 if (uc_is_general_category_withtable
606 (ch,
607 UC_CATEGORY_MASK_L | UC_CATEGORY_MASK_M |
608 UC_CATEGORY_MASK_N | UC_CATEGORY_MASK_P |
609 UC_CATEGORY_MASK_S))
610 {
611 /* Print the character since it is a graphic
612 character. */
613 scm_t_wchar *wbuf;
614 SCM wstr = scm_i_make_wide_string (1, &wbuf);
615 char *buf;
616 size_t len;
617
618 if (scm_i_get_port_encoding (port))
619 {
620 wstr = scm_i_make_wide_string (1, &wbuf);
621 wbuf[0] = ch;
622 buf = u32_conv_to_encoding (scm_i_get_port_encoding (port),
623 iconveh_error,
624 (scm_t_uint32 *) wbuf,
625 1 ,
626 NULL,
627 NULL, &len);
628 if (buf != NULL)
629 {
630 /* Character is graphic and representable in
631 this encoding. Print it. */
632 scm_lfwrite_str (wstr, port);
633 free (buf);
634 printed = 1;
635 }
636 }
637 else
638 if (ch <= 0xFF)
639 {
640 scm_putc (ch, port);
641 printed = 1;
642 }
643 }
644
645 if (!printed)
646 {
647 /* Character is graphic but unrepresentable in
648 this port's encoding or is not graphic. */
649 if (ch <= 0xFF)
650 {
651 buf[0] = '\\';
652 buf[1] = 'x';
653 buf[2] = hex[ch / 16];
654 buf[3] = hex[ch % 16];
655 scm_lfwrite (buf, 4, port);
656 }
657 else if (ch <= 0xFFFF)
658 {
659 buf[0] = '\\';
660 buf[1] = 'u';
661 buf[2] = hex[(ch & 0xF000) >> 12];
662 buf[3] = hex[(ch & 0xF00) >> 8];
663 buf[4] = hex[(ch & 0xF0) >> 4];
664 buf[5] = hex[(ch & 0xF)];
665 scm_lfwrite (buf, 6, port);
666 j = i + 1;
667 }
668 else if (ch > 0xFFFF)
669 {
670 buf[0] = '\\';
671 buf[1] = 'U';
672 buf[2] = hex[(ch & 0xF00000) >> 20];
673 buf[3] = hex[(ch & 0xF0000) >> 16];
674 buf[4] = hex[(ch & 0xF000) >> 12];
675 buf[5] = hex[(ch & 0xF00) >> 8];
676 buf[6] = hex[(ch & 0xF0) >> 4];
677 buf[7] = hex[(ch & 0xF)];
678 scm_lfwrite (buf, 8, port);
679 j = i + 1;
680 }
681 }
682 }
683 scm_putc ('"', port);
684 scm_remember_upto_here_1 (exp);
685 }
686 else
687 scm_lfwrite_str (exp, port);
688 scm_remember_upto_here_1 (exp);
689 break;
690 case scm_tc7_symbol:
691 if (scm_i_symbol_is_interned (exp))
692 {
693 scm_i_print_symbol_name (exp, port);
694 scm_remember_upto_here_1 (exp);
695 }
696 else
697 {
698 scm_puts ("#<uninterned-symbol ", port);
699 scm_i_print_symbol_name (exp, port);
700 scm_putc (' ', port);
701 scm_uintprint (SCM_UNPACK (exp), 16, port);
702 scm_putc ('>', port);
703 }
704 break;
705 case scm_tc7_variable:
706 scm_i_variable_print (exp, port, pstate);
707 break;
708 case scm_tc7_program:
709 scm_i_program_print (exp, port, pstate);
710 break;
711 case scm_tc7_hashtable:
712 scm_i_hashtable_print (exp, port, pstate);
713 break;
714 case scm_tc7_fluid:
715 scm_i_fluid_print (exp, port, pstate);
716 break;
717 case scm_tc7_dynamic_state:
718 scm_i_dynamic_state_print (exp, port, pstate);
719 break;
720 case scm_tc7_wvect:
721 ENTER_NESTED_DATA (pstate, exp, circref);
722 if (SCM_IS_WHVEC (exp))
723 scm_puts ("#wh(", port);
724 else
725 scm_puts ("#w(", port);
726 goto common_vector_printer;
727
728 case scm_tc7_bytevector:
729 scm_i_print_bytevector (exp, port, pstate);
730 break;
731 case scm_tc7_vector:
732 ENTER_NESTED_DATA (pstate, exp, circref);
733 scm_puts ("#(", port);
734 common_vector_printer:
735 {
736 register long i;
737 long last = SCM_SIMPLE_VECTOR_LENGTH (exp) - 1;
738 int cutp = 0;
739 if (pstate->fancyp
740 && SCM_SIMPLE_VECTOR_LENGTH (exp) > pstate->length)
741 {
742 last = pstate->length - 1;
743 cutp = 1;
744 }
745 if (SCM_I_WVECTP (exp))
746 {
747 /* Elements of weak vectors may not be accessed via the
748 `SIMPLE_VECTOR_REF ()' macro. */
749 for (i = 0; i < last; ++i)
750 {
751 scm_iprin1 (scm_c_vector_ref (exp, i),
752 port, pstate);
753 scm_putc (' ', port);
754 }
755 }
756 else
757 {
758 for (i = 0; i < last; ++i)
759 {
760 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp, i), port, pstate);
761 scm_putc (' ', port);
762 }
763 }
764
765 if (i == last)
766 {
767 /* CHECK_INTS; */
768 scm_iprin1 (scm_c_vector_ref (exp, i), port, pstate);
769 }
770 if (cutp)
771 scm_puts (" ...", port);
772 scm_putc (')', port);
773 }
774 EXIT_NESTED_DATA (pstate);
775 break;
776 case scm_tc7_gsubr:
777 {
778 SCM name = scm_symbol_to_string (SCM_SUBR_NAME (exp));
779 scm_puts (SCM_SUBR_GENERIC (exp)
780 ? "#<primitive-generic "
781 : "#<primitive-procedure ",
782 port);
783 scm_lfwrite_str (name, port);
784 scm_putc ('>', port);
785 break;
786 }
787 case scm_tc7_port:
788 {
789 register long i = SCM_PTOBNUM (exp);
790 if (i < scm_numptob
791 && scm_ptobs[i].print
792 && (scm_ptobs[i].print) (exp, port, pstate))
793 break;
794 goto punk;
795 }
796 case scm_tc7_smob:
797 ENTER_NESTED_DATA (pstate, exp, circref);
798 SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate);
799 EXIT_NESTED_DATA (pstate);
800 break;
801 default:
802 /* case scm_tcs_closures: */
803 punk:
804 scm_ipruk ("type", exp, port);
805 }
806 }
807}
808
809/* Print states are necessary for circular reference safe printing.
810 * They are also expensive to allocate. Therefore print states are
811 * kept in a pool so that they can be reused.
812 */
813
814/* The PORT argument can also be a print-state/port pair, which will
815 * then be used instead of allocating a new print state. This is
816 * useful for continuing a chain of print calls from Scheme. */
817
818void
819scm_prin1 (SCM exp, SCM port, int writingp)
820{
821 SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
822 SCM pstate_scm;
823 scm_print_state *pstate;
824 int old_writingp;
825
826 /* If PORT is a print-state/port pair, use that. Else create a new
827 print-state. */
828
829 if (SCM_PORT_WITH_PS_P (port))
830 {
831 pstate_scm = SCM_PORT_WITH_PS_PS (port);
832 port = SCM_PORT_WITH_PS_PORT (port);
833 }
834 else
835 {
836 /* First try to allocate a print state from the pool */
837 scm_i_pthread_mutex_lock (&print_state_mutex);
838 if (!scm_is_null (print_state_pool))
839 {
840 handle = print_state_pool;
841 print_state_pool = SCM_CDR (print_state_pool);
842 }
843 scm_i_pthread_mutex_unlock (&print_state_mutex);
844 if (scm_is_false (handle))
845 handle = scm_list_1 (make_print_state ());
846 pstate_scm = SCM_CAR (handle);
847 }
848
849 pstate = SCM_PRINT_STATE (pstate_scm);
850 old_writingp = pstate->writingp;
851 pstate->writingp = writingp;
852 scm_iprin1 (exp, port, pstate);
853 pstate->writingp = old_writingp;
854
855 /* Return print state to pool if it has been created above and
856 hasn't escaped to Scheme. */
857
858 if (scm_is_true (handle) && !pstate->revealed)
859 {
860 scm_i_pthread_mutex_lock (&print_state_mutex);
861 SCM_SETCDR (handle, print_state_pool);
862 print_state_pool = handle;
863 scm_i_pthread_mutex_unlock (&print_state_mutex);
864 }
865}
866
867/* Print a character.
868 */
869void
870scm_i_charprint (scm_t_wchar ch, SCM port)
871{
872 scm_t_wchar *wbuf;
873 SCM wstr = scm_i_make_wide_string (1, &wbuf);
874
875 wbuf[0] = ch;
876 scm_lfwrite_str (wstr, port);
877}
878
879/* Print an integer.
880 */
881
882void
883scm_intprint (scm_t_intmax n, int radix, SCM port)
884{
885 char num_buf[SCM_INTBUFLEN];
886 scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port);
887}
888
889void
890scm_uintprint (scm_t_uintmax n, int radix, SCM port)
891{
892 char num_buf[SCM_INTBUFLEN];
893 scm_lfwrite (num_buf, scm_iuint2str (n, radix, num_buf), port);
894}
895
896/* Print an object of unrecognized type.
897 */
898
899void
900scm_ipruk (char *hdr, SCM ptr, SCM port)
901{
902 scm_puts ("#<unknown-", port);
903 scm_puts (hdr, port);
904 if (1) /* (scm_in_heap_p (ptr)) */ /* FIXME */
905 {
906 scm_puts (" (0x", port);
907 scm_uintprint (SCM_CELL_WORD_0 (ptr), 16, port);
908 scm_puts (" . 0x", port);
909 scm_uintprint (SCM_CELL_WORD_1 (ptr), 16, port);
910 scm_puts (") @", port);
911 }
912 scm_puts (" 0x", port);
913 scm_uintprint (SCM_UNPACK (ptr), 16, port);
914 scm_putc ('>', port);
915}
916
917
918/* Print a list.
919 */
920void
921scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate)
922{
923 register SCM hare, tortoise;
924 long floor = pstate->top - 2;
925 scm_puts (hdr, port);
926 /* CHECK_INTS; */
927 if (pstate->fancyp)
928 goto fancy_printing;
929
930 /* Run a hare and tortoise so that total time complexity will be
931 O(depth * N) instead of O(N^2). */
932 hare = SCM_CDR (exp);
933 tortoise = exp;
934 while (scm_is_pair (hare))
935 {
936 if (scm_is_eq (hare, tortoise))
937 goto fancy_printing;
938 hare = SCM_CDR (hare);
939 if (!scm_is_pair (hare))
940 break;
941 hare = SCM_CDR (hare);
942 tortoise = SCM_CDR (tortoise);
943 }
944
945 /* No cdr cycles intrinsic to this list */
946 scm_iprin1 (SCM_CAR (exp), port, pstate);
947 for (exp = SCM_CDR (exp); scm_is_pair (exp); exp = SCM_CDR (exp))
948 {
949 register long i;
950
951 for (i = floor; i >= 0; --i)
952 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
953 goto circref;
954 PUSH_REF (pstate, exp);
955 scm_putc (' ', port);
956 /* CHECK_INTS; */
957 scm_iprin1 (SCM_CAR (exp), port, pstate);
958 }
959 if (!SCM_NULL_OR_NIL_P (exp))
960 {
961 scm_puts (" . ", port);
962 scm_iprin1 (exp, port, pstate);
963 }
964
965end:
966 scm_putc (tlr, port);
967 pstate->top = floor + 2;
968 return;
969
970fancy_printing:
971 {
972 long n = pstate->length;
973
974 scm_iprin1 (SCM_CAR (exp), port, pstate);
975 exp = SCM_CDR (exp); --n;
976 for (; scm_is_pair (exp); exp = SCM_CDR (exp))
977 {
978 register unsigned long i;
979
980 for (i = 0; i < pstate->top; ++i)
981 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
982 goto fancy_circref;
983 if (pstate->fancyp)
984 {
985 if (n == 0)
986 {
987 scm_puts (" ...", port);
988 goto skip_tail;
989 }
990 else
991 --n;
992 }
993 PUSH_REF(pstate, exp);
994 ++pstate->list_offset;
995 scm_putc (' ', port);
996 /* CHECK_INTS; */
997 scm_iprin1 (SCM_CAR (exp), port, pstate);
998 }
999 }
1000 if (!SCM_NULL_OR_NIL_P (exp))
1001 {
1002 scm_puts (" . ", port);
1003 scm_iprin1 (exp, port, pstate);
1004 }
1005skip_tail:
1006 pstate->list_offset -= pstate->top - floor - 2;
1007 goto end;
1008
1009fancy_circref:
1010 pstate->list_offset -= pstate->top - floor - 2;
1011
1012circref:
1013 scm_puts (" . ", port);
1014 print_circref (port, pstate, exp);
1015 goto end;
1016}
1017
1018\f
1019
1020int
1021scm_valid_oport_value_p (SCM val)
1022{
1023 return (SCM_OPOUTPORTP (val)
1024 || (SCM_PORT_WITH_PS_P (val)
1025 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val))));
1026}
1027
1028/* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
1029
1030SCM
1031scm_write (SCM obj, SCM port)
1032{
1033 if (SCM_UNBNDP (port))
1034 port = scm_current_output_port ();
1035
1036 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
1037
1038 scm_prin1 (obj, port, 1);
1039#if 0
1040#ifdef HAVE_PIPE
1041# ifdef EPIPE
1042 if (EPIPE == errno)
1043 scm_close_port (port);
1044# endif
1045#endif
1046#endif
1047 return SCM_UNSPECIFIED;
1048}
1049
1050
1051/* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
1052
1053SCM
1054scm_display (SCM obj, SCM port)
1055{
1056 if (SCM_UNBNDP (port))
1057 port = scm_current_output_port ();
1058
1059 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
1060
1061 scm_prin1 (obj, port, 0);
1062#if 0
1063#ifdef HAVE_PIPE
1064# ifdef EPIPE
1065 if (EPIPE == errno)
1066 scm_close_port (port);
1067# endif
1068#endif
1069#endif
1070 return SCM_UNSPECIFIED;
1071}
1072
1073
1074SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
1075 (SCM destination, SCM message, SCM args),
1076 "Write @var{message} to @var{destination}, defaulting to\n"
1077 "the current output port.\n"
1078 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
1079 "@code{~S} (was @code{%S}) escapes. When printed,\n"
1080 "the escapes are replaced with corresponding members of\n"
1081 "@var{ARGS}:\n"
1082 "@code{~A} formats using @code{display} and @code{~S} formats\n"
1083 "using @code{write}.\n"
1084 "If @var{destination} is @code{#t}, then use the current output\n"
1085 "port, if @var{destination} is @code{#f}, then return a string\n"
1086 "containing the formatted text. Does not add a trailing newline.")
1087#define FUNC_NAME s_scm_simple_format
1088{
1089 SCM port, answer = SCM_UNSPECIFIED;
1090 int fReturnString = 0;
1091 int writingp;
1092 size_t start, p, end;
1093
1094 if (scm_is_eq (destination, SCM_BOOL_T))
1095 {
1096 destination = port = scm_current_output_port ();
1097 }
1098 else if (scm_is_false (destination))
1099 {
1100 fReturnString = 1;
1101 port = scm_mkstrport (SCM_INUM0,
1102 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
1103 SCM_OPN | SCM_WRTNG,
1104 FUNC_NAME);
1105 destination = port;
1106 }
1107 else
1108 {
1109 SCM_VALIDATE_OPORT_VALUE (1, destination);
1110 port = SCM_COERCE_OUTPORT (destination);
1111 }
1112 SCM_VALIDATE_STRING (2, message);
1113 SCM_VALIDATE_REST_ARGUMENT (args);
1114
1115 p = 0;
1116 start = 0;
1117 end = scm_i_string_length (message);
1118 for (p = start; p != end; ++p)
1119 if (scm_i_string_ref (message, p) == '~')
1120 {
1121 if (++p == end)
1122 break;
1123
1124 switch (scm_i_string_ref (message, p))
1125 {
1126 case 'A': case 'a':
1127 writingp = 0;
1128 break;
1129 case 'S': case 's':
1130 writingp = 1;
1131 break;
1132 case '~':
1133 scm_lfwrite_substr (message, start, p, port);
1134 start = p + 1;
1135 continue;
1136 case '%':
1137 scm_lfwrite_substr (message, start, p - 1, port);
1138 scm_newline (port);
1139 start = p + 1;
1140 continue;
1141 default:
1142 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
1143 scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message, p))));
1144
1145 }
1146
1147
1148 if (!scm_is_pair (args))
1149 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1150 scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message, p))));
1151
1152 scm_lfwrite_substr (message, start, p - 1, port);
1153 /* we pass destination here */
1154 scm_prin1 (SCM_CAR (args), destination, writingp);
1155 args = SCM_CDR (args);
1156 start = p + 1;
1157 }
1158
1159 scm_lfwrite_substr (message, start, p, port);
1160 if (!scm_is_eq (args, SCM_EOL))
1161 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1162 scm_list_1 (scm_length (args)));
1163
1164 if (fReturnString)
1165 answer = scm_strport_to_string (destination);
1166
1167 return scm_return_first (answer, message);
1168}
1169#undef FUNC_NAME
1170
1171
1172SCM_DEFINE (scm_newline, "newline", 0, 1, 0,
1173 (SCM port),
1174 "Send a newline to @var{port}.\n"
1175 "If @var{port} is omitted, send to the current output port.")
1176#define FUNC_NAME s_scm_newline
1177{
1178 if (SCM_UNBNDP (port))
1179 port = scm_current_output_port ();
1180
1181 SCM_VALIDATE_OPORT_VALUE (1, port);
1182
1183 scm_putc ('\n', SCM_COERCE_OUTPORT (port));
1184 return SCM_UNSPECIFIED;
1185}
1186#undef FUNC_NAME
1187
1188SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
1189 (SCM chr, SCM port),
1190 "Send character @var{chr} to @var{port}.")
1191#define FUNC_NAME s_scm_write_char
1192{
1193 if (SCM_UNBNDP (port))
1194 port = scm_current_output_port ();
1195
1196 SCM_VALIDATE_CHAR (1, chr);
1197 SCM_VALIDATE_OPORT_VALUE (2, port);
1198
1199 scm_i_charprint (SCM_CHAR (chr), SCM_COERCE_OUTPORT (port));
1200#if 0
1201#ifdef HAVE_PIPE
1202# ifdef EPIPE
1203 if (EPIPE == errno)
1204 scm_close_port (port);
1205# endif
1206#endif
1207#endif
1208 return SCM_UNSPECIFIED;
1209}
1210#undef FUNC_NAME
1211
1212\f
1213
1214/* Call back to Scheme code to do the printing of special objects
1215 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1216 * containing PORT and PSTATE. This object can be used as the port for
1217 * display/write etc to continue the current print chain. The REVEALED
1218 * field of PSTATE is set to true to indicate that the print state has
1219 * escaped to Scheme and thus has to be freed by the GC.
1220 */
1221
1222scm_t_bits scm_tc16_port_with_ps;
1223
1224/* Print exactly as the port itself would */
1225
1226static int
1227port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate)
1228{
1229 obj = SCM_PORT_WITH_PS_PORT (obj);
1230 return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate);
1231}
1232
1233SCM
1234scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate)
1235{
1236 pstate->revealed = 1;
1237 return scm_call_2 (proc, exp,
1238 scm_i_port_with_print_state (port, pstate->handle));
1239}
1240
1241SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 1, 1, 0,
1242 (SCM port, SCM pstate),
1243 "Create a new port which behaves like @var{port}, but with an\n"
1244 "included print state @var{pstate}. @var{pstate} is optional.\n"
1245 "If @var{pstate} isn't supplied and @var{port} already has\n"
1246 "a print state, the old print state is reused.")
1247#define FUNC_NAME s_scm_port_with_print_state
1248{
1249 SCM_VALIDATE_OPORT_VALUE (1, port);
1250 if (!SCM_UNBNDP (pstate))
1251 SCM_VALIDATE_PRINTSTATE (2, pstate);
1252 return scm_i_port_with_print_state (port, pstate);
1253}
1254#undef FUNC_NAME
1255
1256SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0,
1257 (SCM port),
1258 "Return the print state of the port @var{port}. If @var{port}\n"
1259 "has no associated print state, @code{#f} is returned.")
1260#define FUNC_NAME s_scm_get_print_state
1261{
1262 if (SCM_PORT_WITH_PS_P (port))
1263 return SCM_PORT_WITH_PS_PS (port);
1264 if (SCM_OUTPUT_PORT_P (port))
1265 return SCM_BOOL_F;
1266 SCM_WRONG_TYPE_ARG (1, port);
1267}
1268#undef FUNC_NAME
1269
1270\f
1271
1272void
1273scm_init_print ()
1274{
1275 SCM vtable, layout, type;
1276
1277 scm_init_opts (scm_print_options, scm_print_opts);
1278
1279 scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"),
1280 scm_from_locale_string ("{"),
1281 scm_from_locale_symbol ("highlight-suffix"),
1282 scm_from_locale_string ("}")));
1283
1284 scm_gc_register_root (&print_state_pool);
1285 scm_gc_register_root (&scm_print_state_vtable);
1286 vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL);
1287 layout =
1288 scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT));
1289 type = scm_make_struct (vtable, SCM_INUM0, scm_list_1 (layout));
1290 scm_set_struct_vtable_name_x (type, scm_from_locale_symbol ("print-state"));
1291 scm_print_state_vtable = type;
1292
1293 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1294 scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
1295 scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print);
1296
1297#include "libguile/print.x"
1298
1299 scm_print_opts[SCM_PRINT_KEYWORD_STYLE_I].val = SCM_UNPACK (sym_reader);
1300}
1301
1302/*
1303 Local Variables:
1304 c-file-style: "gnu"
1305 End:
1306*/