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