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