Import 'c-strcase' from Gnulib.
[bpt/guile.git] / libguile / print.c
CommitLineData
f4bc4e59 1/* Copyright (C) 1995-1999, 2000, 2001, 2002, 2003, 2004, 2006, 2008,
e4598559 2 * 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
f4bc4e59 3 *
73be1d9e 4 * This library is free software; you can redistribute it and/or
53befeb7
NJ
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
0f2d19dd 8 *
53befeb7
NJ
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
0f2d19dd 13 *
73be1d9e
MV
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
53befeb7
NJ
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
73be1d9e 18 */
1bbd0b84 19
1bbd0b84 20
0f2d19dd 21\f
dbb605f5
LC
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
0f2d19dd 25
e6e2e95a 26#include <errno.h>
f4bc4e59
LC
27#include <iconv.h>
28#include <stdio.h>
29#include <assert.h>
30
eca29b02 31#include <uniconv.h>
904a78f1 32#include <unictype.h>
e6e2e95a 33
a0599745
MD
34#include "libguile/_scm.h"
35#include "libguile/chars.h"
a002f1a2 36#include "libguile/continuations.h"
a0599745 37#include "libguile/smob.h"
bbb2ecd1 38#include "libguile/control.h"
a0599745
MD
39#include "libguile/eval.h"
40#include "libguile/macros.h"
41#include "libguile/procprop.h"
42#include "libguile/read.h"
43#include "libguile/weaks.h"
2fb924f6 44#include "libguile/programs.h"
a0599745
MD
45#include "libguile/alist.h"
46#include "libguile/struct.h"
a0599745 47#include "libguile/ports.h"
e4598559 48#include "libguile/ports-internal.h"
a0599745
MD
49#include "libguile/root.h"
50#include "libguile/strings.h"
51#include "libguile/strports.h"
52#include "libguile/vectors.h"
327967ef 53#include "libguile/numbers.h"
6f3b0cc2 54#include "libguile/vm.h"
a0599745
MD
55
56#include "libguile/validate.h"
57#include "libguile/print.h"
22fc179a
HWN
58
59#include "libguile/private-options.h"
60
0f2d19dd
JB
61\f
62
07f49ac7
LC
63/* Character printers. */
64
478848cb
LC
65#define PORT_CONVERSION_HANDLER(port) \
66 SCM_PTAB_ENTRY (port)->ilseq_handler
67
f4bc4e59
LC
68static size_t display_string (const void *, int, size_t, SCM,
69 scm_t_string_failed_conversion_handler);
70
07f49ac7
LC
71static int display_character (scm_t_wchar, SCM,
72 scm_t_string_failed_conversion_handler);
f4bc4e59 73
07f49ac7
LC
74static void write_character (scm_t_wchar, SCM, int);
75
f4bc4e59
LC
76static void write_character_escaped (scm_t_wchar, int, SCM);
77
07f49ac7
LC
78\f
79
0f2d19dd
JB
80/* {Names of immediate symbols}
81 *
82 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
83 */
84
e17d318f
DH
85/* This table must agree with the list of flags in tags.h. */
86static const char *iflagnames[] =
87{
88 "#f",
45f4cbdf
MW
89 "#nil", /* Elisp nil value. Should print from elisp as symbol `nil'. */
90 "#<XXX UNUSED LISP FALSE -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
91 "()",
e17d318f 92 "#t",
f60c2c4e
MW
93 "#<XXX UNUSED BOOLEAN 0 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
94 "#<XXX UNUSED BOOLEAN 1 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
95 "#<XXX UNUSED BOOLEAN 2 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
45f4cbdf 96 "#<unspecified>",
e17d318f
DH
97 "#<undefined>",
98 "#<eof>",
e17d318f
DH
99
100 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
101 "#<unbound>",
e17d318f
DH
102};
103
475fa9a5
MV
104SCM_SYMBOL (sym_reader, "reader");
105
92c2555f 106scm_t_option scm_print_opts[] = {
210c0325 107 { SCM_OPTION_SCM, "highlight-prefix", (scm_t_bits)SCM_BOOL_F_BITS,
81ae25da 108 "The string to print before highlighted values." },
210c0325 109 { SCM_OPTION_SCM, "highlight-suffix", (scm_t_bits)SCM_BOOL_F_BITS,
475fa9a5 110 "The string to print after highlighted values." },
210c0325 111 { SCM_OPTION_SCM, "quote-keywordish-symbols", (scm_t_bits)SCM_BOOL_F_BITS,
475fa9a5
MV
112 "How to print symbols that have a colon as their first or last character. "
113 "The value '#f' does not quote the colons; '#t' quotes them; "
8500b186
AW
114 "'reader' quotes them when the reader option 'keywords' is not '#f'." },
115 { SCM_OPTION_BOOLEAN, "escape-newlines", 1,
116 "Render newlines as \\n when printing using `write'." },
62560650 117 { 0 },
e6e4c9af
MD
118};
119
a1ec6916 120SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0,
1bbd0b84 121 (SCM setting),
71331188 122 "Option interface for the print options. Instead of using\n"
1dd05fd8
MG
123 "this procedure directly, use the procedures\n"
124 "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
125 "and @code{print-options}.")
1bbd0b84 126#define FUNC_NAME s_scm_print_options
e6e4c9af 127{
a51ea417 128 SCM ans = scm_options (setting,
b7ff98dd 129 scm_print_opts,
1bbd0b84 130 FUNC_NAME);
e6e4c9af
MD
131 return ans;
132}
1bbd0b84 133#undef FUNC_NAME
e6e4c9af 134
0f2d19dd
JB
135\f
136/* {Printing of Scheme Objects}
137 */
138
a51ea417 139/* Detection of circular references.
c62fbfe1
MD
140 *
141 * Due to other constraints in the implementation, this code has bad
5d46ebe3
MD
142 * time complexity (O (depth * N)), The printer code can be
143 * rewritten to be O(N).
a51ea417 144 */
dbb5de29
NJ
145#define PUSH_REF(pstate, obj) \
146do \
147{ \
148 PSTATE_STACK_SET (pstate, pstate->top, obj); \
149 pstate->top++; \
150 if (pstate->top == pstate->ceiling) \
151 grow_ref_stack (pstate); \
1bbd0b84 152} while(0)
a51ea417 153
dbb5de29
NJ
154#define ENTER_NESTED_DATA(pstate, obj, label) \
155do \
156{ \
157 register unsigned long i; \
158 for (i = 0; i < pstate->top; ++i) \
159 if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
160 goto label; \
161 if (pstate->fancyp) \
162 { \
163 if (pstate->top - pstate->list_offset >= pstate->level) \
164 { \
165 scm_putc ('#', port); \
166 return; \
167 } \
168 } \
169 PUSH_REF(pstate, obj); \
1bbd0b84 170} while(0)
a51ea417 171
dbb5de29
NJ
172#define EXIT_NESTED_DATA(pstate) \
173do \
174{ \
175 --pstate->top; \
176 PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
177} \
178while (0)
c62fbfe1 179
d5cf5324
DH
180SCM scm_print_state_vtable = SCM_BOOL_F;
181static SCM print_state_pool = SCM_EOL;
9de87eea 182scm_i_pthread_mutex_t print_state_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
c4f37e80 183
f843a84c 184#ifdef GUILE_DEBUG /* Used for debugging purposes */
1cc91f1b 185
3b3b36dd 186SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0,
1bbd0b84 187 (),
d5cf5324 188 "Return the current-pstate -- the car of the\n"
5352393c
MG
189 "@code{print_state_pool}. @code{current-pstate} is only\n"
190 "included in @code{--enable-guile-debug} builds.")
1bbd0b84 191#define FUNC_NAME s_scm_current_pstate
c62fbfe1 192{
d2e53ed6 193 if (!scm_is_null (print_state_pool))
d5cf5324 194 return SCM_CAR (print_state_pool);
a0adfbf0 195 else
0a284a4e 196 return SCM_BOOL_F;
c62fbfe1 197}
1bbd0b84
GB
198#undef FUNC_NAME
199
c62fbfe1
MD
200#endif
201
202#define PSTATE_SIZE 50L
203
698c0295 204static SCM
1bbd0b84 205make_print_state (void)
698c0295 206{
d5cf5324
DH
207 SCM print_state
208 = scm_make_struct (scm_print_state_vtable, SCM_INUM0, SCM_EOL);
bf685b6d 209 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
00ffa0e7 210 pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED);
4057a3e0 211 pstate->ceiling = SCM_SIMPLE_VECTOR_LENGTH (pstate->ref_vect);
d232520a 212 pstate->highlight_objects = SCM_EOL;
698c0295
MD
213 return print_state;
214}
1cc91f1b 215
c62fbfe1
MD
216SCM
217scm_make_print_state ()
c62fbfe1 218{
230d095f 219 SCM answer = SCM_BOOL_F;
698c0295
MD
220
221 /* First try to allocate a print state from the pool */
9de87eea 222 scm_i_pthread_mutex_lock (&print_state_mutex);
d2e53ed6 223 if (!scm_is_null (print_state_pool))
698c0295 224 {
d5cf5324
DH
225 answer = SCM_CAR (print_state_pool);
226 print_state_pool = SCM_CDR (print_state_pool);
698c0295 227 }
9de87eea 228 scm_i_pthread_mutex_unlock (&print_state_mutex);
698c0295 229
7888309b 230 return scm_is_false (answer) ? make_print_state () : answer;
c62fbfe1 231}
a51ea417 232
698c0295 233void
6e8d25a6 234scm_free_print_state (SCM print_state)
698c0295
MD
235{
236 SCM handle;
237 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
238 /* Cleanup before returning print state to pool.
239 * It is better to do it here. Doing it in scm_prin1
240 * would cost more since that function is called much more
241 * often.
242 */
243 pstate->fancyp = 0;
bb35f315 244 pstate->revealed = 0;
d232520a 245 pstate->highlight_objects = SCM_EOL;
9de87eea 246 scm_i_pthread_mutex_lock (&print_state_mutex);
16d4699b 247 handle = scm_cons (print_state, print_state_pool);
d5cf5324 248 print_state_pool = handle;
9de87eea 249 scm_i_pthread_mutex_unlock (&print_state_mutex);
dfd03fb9
MD
250}
251
252SCM
253scm_i_port_with_print_state (SCM port, SCM print_state)
254{
255 if (SCM_UNBNDP (print_state))
256 {
257 if (SCM_PORT_WITH_PS_P (port))
258 return port;
259 else
260 print_state = scm_make_print_state ();
261 /* port does not need to be coerced since it doesn't have ps */
262 }
263 else
264 port = SCM_COERCE_OUTPORT (port);
265 SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps,
266 SCM_UNPACK (scm_cons (port, print_state)));
698c0295 267}
1cc91f1b 268
a51ea417 269static void
1bbd0b84 270grow_ref_stack (scm_print_state *pstate)
a51ea417 271{
4057a3e0
MV
272 SCM old_vect = pstate->ref_vect;
273 size_t old_size = SCM_SIMPLE_VECTOR_LENGTH (old_vect);
274 size_t new_size = 2 * pstate->ceiling;
00ffa0e7 275 SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED);
b17004b8
DH
276 unsigned long int i;
277
278 for (i = 0; i != old_size; ++i)
4057a3e0 279 SCM_SIMPLE_VECTOR_SET (new_vect, i, SCM_SIMPLE_VECTOR_REF (old_vect, i));
b17004b8
DH
280
281 pstate->ref_vect = new_vect;
bf685b6d 282 pstate->ceiling = new_size;
a51ea417
MD
283}
284
509759dd
MV
285#define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
286#define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
1cc91f1b 287
a51ea417 288static void
34d19ef6 289print_circref (SCM port, scm_print_state *pstate, SCM ref)
a51ea417 290{
c014a02e
ML
291 register long i;
292 long self = pstate->top - 1;
c62fbfe1 293 i = pstate->top - 1;
509759dd 294 if (scm_is_pair (PSTATE_STACK_REF (pstate, i)))
c62fbfe1
MD
295 {
296 while (i > 0)
297 {
509759dd
MV
298 if (!scm_is_pair (PSTATE_STACK_REF (pstate, i-1))
299 || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate, i-1)),
300 SCM_CDR (PSTATE_STACK_REF (pstate, i))))
c62fbfe1
MD
301 break;
302 --i;
303 }
304 self = i;
305 }
306 for (i = pstate->top - 1; 1; --i)
509759dd 307 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), ref))
c62fbfe1 308 break;
b7f3516f 309 scm_putc ('#', port);
c62fbfe1 310 scm_intprint (i - self, 10, port);
b7f3516f 311 scm_putc ('#', port);
a51ea417
MD
312}
313
6662998f
MV
314/* Print the name of a symbol. */
315
475fa9a5 316static int
15671c6e 317quote_keywordish_symbols (void)
475fa9a5 318{
15671c6e 319 SCM option = SCM_PRINT_KEYWORD_STYLE;
475fa9a5 320
475fa9a5
MV
321 if (scm_is_false (option))
322 return 0;
323 if (scm_is_eq (option, sym_reader))
324 return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE));
325 return 1;
326}
327
2e9fc9fc
AW
328#define INITIAL_IDENTIFIER_MASK \
329 (UC_CATEGORY_MASK_Lu | UC_CATEGORY_MASK_Ll | UC_CATEGORY_MASK_Lt \
330 | UC_CATEGORY_MASK_Lm | UC_CATEGORY_MASK_Lo | UC_CATEGORY_MASK_Mn \
331 | UC_CATEGORY_MASK_Nl | UC_CATEGORY_MASK_No | UC_CATEGORY_MASK_Pd \
332 | UC_CATEGORY_MASK_Pc | UC_CATEGORY_MASK_Po | UC_CATEGORY_MASK_Sc \
333 | UC_CATEGORY_MASK_Sm | UC_CATEGORY_MASK_Sk | UC_CATEGORY_MASK_So \
334 | UC_CATEGORY_MASK_Co)
335
336#define SUBSEQUENT_IDENTIFIER_MASK \
337 (INITIAL_IDENTIFIER_MASK \
338 | UC_CATEGORY_MASK_Nd | UC_CATEGORY_MASK_Mc | UC_CATEGORY_MASK_Me)
339
15671c6e
AW
340static int
341symbol_has_extended_read_syntax (SCM sym)
6662998f 342{
15671c6e
AW
343 size_t pos, len = scm_i_symbol_length (sym);
344 scm_t_wchar c;
345
346 /* The empty symbol. */
347 if (len == 0)
348 return 1;
349
350 c = scm_i_symbol_ref (sym, 0);
351
352 /* Single dot; conflicts with dotted-pair notation. */
353 if (len == 1 && c == '.')
354 return 1;
355
356 /* Other initial-character constraints. */
2e9fc9fc 357 if (c == '\'' || c == '`' || c == ',' || c == '"' || c == ';' || c == '#')
15671c6e
AW
358 return 1;
359
360 /* Keywords can be identified by trailing colons too. */
361 if (c == ':' || scm_i_symbol_ref (sym, len - 1) == ':')
362 return quote_keywordish_symbols ();
363
364 /* Number-ish symbols. */
365 if (scm_is_true (scm_i_string_to_number (scm_symbol_to_string (sym), 10)))
366 return 1;
367
2e9fc9fc
AW
368 /* Other disallowed first characters. */
369 if (!uc_is_general_category_withtable (c, INITIAL_IDENTIFIER_MASK))
370 return 1;
371
372 /* Otherwise, any character that's in the identifier category mask is
373 fine to pass through as-is, provided it's not one of the ASCII
374 delimiters like `;'. */
375 for (pos = 1; pos < len; pos++)
6662998f 376 {
2e9fc9fc
AW
377 c = scm_i_symbol_ref (sym, pos);
378 if (!uc_is_general_category_withtable (c, SUBSEQUENT_IDENTIFIER_MASK))
379 return 1;
380 else if (c == '"' || c == ';' || c == '#')
381 return 1;
6662998f 382 }
c6b49e89 383
15671c6e
AW
384 return 0;
385}
386
387static void
388print_normal_symbol (SCM sym, SCM port)
389{
390 scm_display (scm_symbol_to_string (sym), port);
391}
392
15671c6e
AW
393static void
394print_extended_symbol (SCM sym, SCM port)
395{
396 size_t pos, len;
397 scm_t_string_failed_conversion_handler strategy;
398
399 len = scm_i_symbol_length (sym);
478848cb 400 strategy = PORT_CONVERSION_HANDLER (port);
15671c6e
AW
401
402 scm_lfwrite ("#{", 2, port);
403
404 for (pos = 0; pos < len; pos++)
405 {
406 scm_t_wchar c = scm_i_symbol_ref (sym, pos);
407
2e9fc9fc
AW
408 if (uc_is_general_category_withtable (c,
409 SUBSEQUENT_IDENTIFIER_MASK
410 | UC_CATEGORY_MASK_Zs))
15671c6e 411 {
15671c6e
AW
412 if (!display_character (c, port, strategy))
413 scm_encoding_error ("print_extended_symbol", errno,
414 "cannot convert to output locale",
415 port, SCM_MAKE_CHAR (c));
2e9fc9fc
AW
416 }
417 else
418 {
419 display_string ("\\x", 1, 2, port, iconveh_question_mark);
420 scm_intprint (c, 16, port);
421 display_character (';', port, iconveh_question_mark);
15671c6e
AW
422 }
423 }
424
425 scm_lfwrite ("}#", 2, port);
426}
427
428/* FIXME: allow R6RS hex escapes instead of #{...}#. */
429void
430scm_i_print_symbol_name (SCM sym, SCM port)
431{
432 if (symbol_has_extended_read_syntax (sym))
433 print_extended_symbol (sym, port);
434 else
435 print_normal_symbol (sym, port);
6662998f
MV
436}
437
e23106d5
MG
438void
439scm_print_symbol_name (const char *str, size_t len, SCM port)
440{
441 SCM symbol = scm_from_locale_symboln (str, len);
ab33782c 442 scm_i_print_symbol_name (symbol, port);
e23106d5
MG
443}
444
c62fbfe1 445/* Print generally. Handles both write and display according to PSTATE.
0f2d19dd 446 */
8b840115
MD
447SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write);
448SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
1cc91f1b 449
d232520a
MV
450static void iprin1 (SCM exp, SCM port, scm_print_state *pstate);
451
dea901d6
MG
452
453/* Print a character as an octal or hex escape. */
454#define PRINT_CHAR_ESCAPE(i, port) \
455 do \
456 { \
457 if (!SCM_R6RS_ESCAPES_P) \
458 scm_intprint (i, 8, port); \
459 else \
460 { \
461 scm_puts ("x", port); \
462 scm_intprint (i, 16, port); \
463 } \
464 } \
465 while (0)
466
467
0f2d19dd 468void
1bbd0b84 469scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate)
d232520a
MV
470{
471 if (pstate->fancyp
472 && scm_is_true (scm_memq (exp, pstate->highlight_objects)))
473 {
81ae25da 474 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX, port);
d232520a 475 iprin1 (exp, port, pstate);
81ae25da 476 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX, port);
d232520a
MV
477 }
478 else
479 iprin1 (exp, port, pstate);
480}
481
482static void
483iprin1 (SCM exp, SCM port, scm_print_state *pstate)
0f2d19dd 484{
54778cd3 485 switch (SCM_ITAG3 (exp))
0f2d19dd 486 {
e34f941a
DH
487 case scm_tc3_tc7_1:
488 case scm_tc3_tc7_2:
489 /* These tc3 tags should never occur in an immediate value. They are
490 * only used in cell types of non-immediates, i. e. the value returned
491 * by SCM_CELL_TYPE (exp) can use these tags.
492 */
493 scm_ipruk ("immediate", exp, port);
494 break;
495 case scm_tc3_int_1:
496 case scm_tc3_int_2:
e11e83f3 497 scm_intprint (SCM_I_INUM (exp), 10, port);
0f2d19dd 498 break;
e34f941a 499 case scm_tc3_imm24:
7866a09b 500 if (SCM_CHARP (exp))
0f2d19dd 501 {
b7f3516f 502 if (SCM_WRITINGP (pstate))
07f49ac7
LC
503 write_character (SCM_CHAR (exp), port, 0);
504 else
b7f3516f 505 {
07f49ac7 506 if (!display_character (SCM_CHAR (exp), port,
478848cb 507 PORT_CONVERSION_HANDLER (port)))
07f49ac7
LC
508 scm_encoding_error (__func__, errno,
509 "cannot convert to output locale",
6851d3be 510 port, exp);
b7f3516f 511 }
0f2d19dd 512 }
a51ea417 513 else if (SCM_IFLAGP (exp)
e17d318f
DH
514 && ((size_t) SCM_IFLAGNUM (exp) < (sizeof iflagnames / sizeof (char *))))
515 {
516 scm_puts (iflagnames [SCM_IFLAGNUM (exp)], port);
517 }
0f2d19dd 518 else
e34f941a
DH
519 {
520 /* unknown immediate value */
521 scm_ipruk ("immediate", exp, port);
522 }
0f2d19dd 523 break;
e34f941a 524 case scm_tc3_cons:
0f2d19dd
JB
525 switch (SCM_TYP7 (exp))
526 {
904a077d
MV
527 case scm_tcs_struct:
528 {
529 ENTER_NESTED_DATA (pstate, exp, circref);
530 if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS)
531 {
532 SCM pwps, print = pstate->writingp ? g_write : g_display;
b2b33168 533 if (SCM_UNPACK (print) == 0)
904a077d 534 goto print_struct;
dfd03fb9 535 pwps = scm_i_port_with_print_state (port, pstate->handle);
7663c008 536 pstate->revealed = 1;
904a077d
MV
537 scm_call_generic_2 (print, exp, pwps);
538 }
539 else
540 {
541 print_struct:
542 scm_print_struct (exp, port, pstate);
543 }
544 EXIT_NESTED_DATA (pstate);
545 }
546 break;
0f2d19dd
JB
547 case scm_tcs_cons_imcar:
548 case scm_tcs_cons_nimcar:
c62fbfe1
MD
549 ENTER_NESTED_DATA (pstate, exp, circref);
550 scm_iprlist ("(", exp, ')', port, pstate);
551 EXIT_NESTED_DATA (pstate);
a51ea417
MD
552 break;
553 circref:
c62fbfe1 554 print_circref (port, pstate, exp);
0f2d19dd 555 break;
534c55a9
DH
556 case scm_tc7_number:
557 switch SCM_TYP16 (exp) {
558 case scm_tc16_big:
559 scm_bigprint (exp, port, pstate);
560 break;
561 case scm_tc16_real:
562 scm_print_real (exp, port, pstate);
563 break;
564 case scm_tc16_complex:
565 scm_print_complex (exp, port, pstate);
566 break;
f92e85f7
MV
567 case scm_tc16_fraction:
568 scm_i_print_fraction (exp, port, pstate);
569 break;
534c55a9
DH
570 }
571 break;
9c44cd45
MG
572 case scm_tc7_string:
573 if (SCM_WRITINGP (pstate))
574 {
07f49ac7 575 size_t len, i;
9c44cd45 576
f4bc4e59 577 display_character ('"', port, iconveh_question_mark);
9c44cd45
MG
578 len = scm_i_string_length (exp);
579 for (i = 0; i < len; ++i)
07f49ac7
LC
580 write_character (scm_i_string_ref (exp, i), port, 1);
581
f4bc4e59 582 display_character ('"', port, iconveh_question_mark);
9c44cd45
MG
583 scm_remember_upto_here_1 (exp);
584 }
585 else
f4bc4e59
LC
586 {
587 size_t len, printed;
588
589 len = scm_i_string_length (exp);
590 printed = display_string (scm_i_string_data (exp),
591 scm_i_is_narrow_string (exp),
592 len, port,
478848cb 593 PORT_CONVERSION_HANDLER (port));
f4bc4e59 594 if (SCM_UNLIKELY (printed < len))
f4bc4e59
LC
595 scm_encoding_error (__func__, errno,
596 "cannot convert to output locale",
6851d3be 597 port, scm_c_string_ref (exp, printed));
f4bc4e59
LC
598 }
599
9c44cd45
MG
600 scm_remember_upto_here_1 (exp);
601 break;
28b06554 602 case scm_tc7_symbol:
cc95e00a 603 if (scm_i_symbol_is_interned (exp))
9ff28a13 604 {
e23106d5 605 scm_i_print_symbol_name (exp, port);
9ff28a13
MV
606 scm_remember_upto_here_1 (exp);
607 }
608 else
609 {
610 scm_puts ("#<uninterned-symbol ", port);
e23106d5 611 scm_i_print_symbol_name (exp, port);
9ff28a13 612 scm_putc (' ', port);
0345e278 613 scm_uintprint (SCM_UNPACK (exp), 16, port);
9ff28a13
MV
614 scm_putc ('>', port);
615 }
6662998f 616 break;
e5aca4b5
MV
617 case scm_tc7_variable:
618 scm_i_variable_print (exp, port, pstate);
619 break;
2fb924f6
AW
620 case scm_tc7_program:
621 scm_i_program_print (exp, port, pstate);
622 break;
5b46a8c2
LC
623 case scm_tc7_pointer:
624 scm_i_pointer_print (exp, port, pstate);
e2c2a699 625 break;
c99de5aa
AW
626 case scm_tc7_hashtable:
627 scm_i_hashtable_print (exp, port, pstate);
628 break;
9ea31741
AW
629 case scm_tc7_fluid:
630 scm_i_fluid_print (exp, port, pstate);
631 break;
45cf2428
AW
632 case scm_tc7_dynamic_state:
633 scm_i_dynamic_state_print (exp, port, pstate);
634 break;
6f3b0cc2
AW
635 case scm_tc7_frame:
636 scm_i_frame_print (exp, port, pstate);
637 break;
638 case scm_tc7_objcode:
639 scm_i_objcode_print (exp, port, pstate);
640 break;
641 case scm_tc7_vm:
642 scm_i_vm_print (exp, port, pstate);
643 break;
644 case scm_tc7_vm_cont:
645 scm_i_vm_cont_print (exp, port, pstate);
646 break;
bbb2ecd1
AW
647 case scm_tc7_prompt:
648 scm_i_prompt_print (exp, port, pstate);
649 break;
650 case scm_tc7_with_fluids:
651 scm_i_with_fluids_print (exp, port, pstate);
652 break;
b2637c98
AW
653 case scm_tc7_array:
654 ENTER_NESTED_DATA (pstate, exp, circref);
655 scm_i_print_array (exp, port, pstate);
88c0a1d5 656 EXIT_NESTED_DATA (pstate);
b2637c98
AW
657 break;
658 case scm_tc7_bytevector:
659 scm_i_print_bytevector (exp, port, pstate);
660 break;
ff1feca9
AW
661 case scm_tc7_bitvector:
662 scm_i_print_bitvector (exp, port, pstate);
663 break;
0f2d19dd 664 case scm_tc7_wvect:
c62fbfe1 665 ENTER_NESTED_DATA (pstate, exp, circref);
0f2d19dd 666 if (SCM_IS_WHVEC (exp))
b7f3516f 667 scm_puts ("#wh(", port);
0f2d19dd 668 else
b7f3516f 669 scm_puts ("#w(", port);
0f2d19dd 670 goto common_vector_printer;
0f2d19dd 671 case scm_tc7_vector:
c62fbfe1 672 ENTER_NESTED_DATA (pstate, exp, circref);
b7f3516f 673 scm_puts ("#(", port);
0f2d19dd 674 common_vector_printer:
9fbaf27c 675 {
c014a02e 676 register long i;
4057a3e0 677 long last = SCM_SIMPLE_VECTOR_LENGTH (exp) - 1;
9fbaf27c 678 int cutp = 0;
4057a3e0
MV
679 if (pstate->fancyp
680 && SCM_SIMPLE_VECTOR_LENGTH (exp) > pstate->length)
9fbaf27c
MD
681 {
682 last = pstate->length - 1;
683 cutp = 1;
684 }
c367c4b4 685 if (SCM_I_WVECTP (exp))
9fbaf27c 686 {
c367c4b4
LC
687 /* Elements of weak vectors may not be accessed via the
688 `SIMPLE_VECTOR_REF ()' macro. */
689 for (i = 0; i < last; ++i)
690 {
691 scm_iprin1 (scm_c_vector_ref (exp, i),
692 port, pstate);
693 scm_putc (' ', port);
694 }
9fbaf27c 695 }
c367c4b4
LC
696 else
697 {
698 for (i = 0; i < last; ++i)
699 {
700 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp, i), port, pstate);
701 scm_putc (' ', port);
702 }
703 }
704
9fbaf27c
MD
705 if (i == last)
706 {
707 /* CHECK_INTS; */
c367c4b4 708 scm_iprin1 (scm_c_vector_ref (exp, i), port, pstate);
9fbaf27c
MD
709 }
710 if (cutp)
b7f3516f
TT
711 scm_puts (" ...", port);
712 scm_putc (')', port);
9fbaf27c 713 }
c62fbfe1 714 EXIT_NESTED_DATA (pstate);
0f2d19dd 715 break;
0f2d19dd 716 case scm_tc7_port:
5ca6dc39
JB
717 {
718 register long i = SCM_PTOBNUM (exp);
719 if (i < scm_numptob
720 && scm_ptobs[i].print
721 && (scm_ptobs[i].print) (exp, port, pstate))
a51ea417 722 break;
5ca6dc39
JB
723 goto punk;
724 }
725 case scm_tc7_smob:
7a7f7c53
DH
726 ENTER_NESTED_DATA (pstate, exp, circref);
727 SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate);
728 EXIT_NESTED_DATA (pstate);
729 break;
0f2d19dd 730 default:
314b8716 731 /* case scm_tcs_closures: */
a51ea417
MD
732 punk:
733 scm_ipruk ("type", exp, port);
0f2d19dd
JB
734 }
735 }
736}
737
c62fbfe1
MD
738/* Print states are necessary for circular reference safe printing.
739 * They are also expensive to allocate. Therefore print states are
740 * kept in a pool so that they can be reused.
741 */
1cc91f1b 742
bb35f315
MV
743/* The PORT argument can also be a print-state/port pair, which will
744 * then be used instead of allocating a new print state. This is
745 * useful for continuing a chain of print calls from Scheme. */
746
a51ea417 747void
1bbd0b84 748scm_prin1 (SCM exp, SCM port, int writingp)
a51ea417 749{
c4f37e80
MV
750 SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
751 SCM pstate_scm;
c62fbfe1 752 scm_print_state *pstate;
15635be5 753 int old_writingp;
c62fbfe1 754
bb35f315
MV
755 /* If PORT is a print-state/port pair, use that. Else create a new
756 print-state. */
c4f37e80 757
0c95b57d 758 if (SCM_PORT_WITH_PS_P (port))
bb35f315 759 {
52235e71
MD
760 pstate_scm = SCM_PORT_WITH_PS_PS (port);
761 port = SCM_PORT_WITH_PS_PORT (port);
bb35f315
MV
762 }
763 else
c62fbfe1 764 {
c4f37e80 765 /* First try to allocate a print state from the pool */
9de87eea 766 scm_i_pthread_mutex_lock (&print_state_mutex);
d2e53ed6 767 if (!scm_is_null (print_state_pool))
c4f37e80 768 {
d5cf5324
DH
769 handle = print_state_pool;
770 print_state_pool = SCM_CDR (print_state_pool);
c4f37e80 771 }
9de87eea 772 scm_i_pthread_mutex_unlock (&print_state_mutex);
7888309b 773 if (scm_is_false (handle))
d5cf5324 774 handle = scm_list_1 (make_print_state ());
c4f37e80 775 pstate_scm = SCM_CAR (handle);
c62fbfe1 776 }
c62fbfe1 777
c4f37e80 778 pstate = SCM_PRINT_STATE (pstate_scm);
15635be5 779 old_writingp = pstate->writingp;
c62fbfe1
MD
780 pstate->writingp = writingp;
781 scm_iprin1 (exp, port, pstate);
15635be5 782 pstate->writingp = old_writingp;
c62fbfe1 783
bb35f315
MV
784 /* Return print state to pool if it has been created above and
785 hasn't escaped to Scheme. */
786
7888309b 787 if (scm_is_true (handle) && !pstate->revealed)
c4f37e80 788 {
9de87eea 789 scm_i_pthread_mutex_lock (&print_state_mutex);
d5cf5324
DH
790 SCM_SETCDR (handle, print_state_pool);
791 print_state_pool = handle;
9de87eea 792 scm_i_pthread_mutex_unlock (&print_state_mutex);
c4f37e80 793 }
a51ea417
MD
794}
795
f4bc4e59
LC
796/* Convert codepoint CH to UTF-8 and store the result in UTF8. Return
797 the number of bytes of the UTF-8-encoded string. */
798static size_t
799codepoint_to_utf8 (scm_t_wchar ch, scm_t_uint8 utf8[4])
9c44cd45 800{
f4bc4e59
LC
801 size_t len;
802 scm_t_uint32 codepoint;
803
804 codepoint = (scm_t_uint32) ch;
07f49ac7 805
f4bc4e59 806 if (codepoint <= 0x7f)
07f49ac7 807 {
f4bc4e59
LC
808 len = 1;
809 utf8[0] = (scm_t_uint8) codepoint;
810 }
811 else if (codepoint <= 0x7ffUL)
812 {
813 len = 2;
814 utf8[0] = 0xc0 | (codepoint >> 6);
815 utf8[1] = 0x80 | (codepoint & 0x3f);
816 }
817 else if (codepoint <= 0xffffUL)
818 {
819 len = 3;
820 utf8[0] = 0xe0 | (codepoint >> 12);
821 utf8[1] = 0x80 | ((codepoint >> 6) & 0x3f);
822 utf8[2] = 0x80 | (codepoint & 0x3f);
07f49ac7
LC
823 }
824 else
825 {
f4bc4e59
LC
826 len = 4;
827 utf8[0] = 0xf0 | (codepoint >> 18);
828 utf8[1] = 0x80 | ((codepoint >> 12) & 0x3f);
829 utf8[2] = 0x80 | ((codepoint >> 6) & 0x3f);
830 utf8[3] = 0x80 | (codepoint & 0x3f);
831 }
832
833 return len;
834}
835
f4bc4e59
LC
836#define STR_REF(s, x) \
837 (narrow_p \
838 ? (scm_t_wchar) ((unsigned char *) (s))[x] \
839 : ((scm_t_wchar *) (s))[x])
840
7b292a9d
LC
841/* Write STR to PORT as UTF-8. STR is a LEN-codepoint string; it is
842 narrow if NARROW_P is true, wide otherwise. Return LEN. */
843static size_t
844display_string_as_utf8 (const void *str, int narrow_p, size_t len,
845 SCM port)
846{
847 size_t printed = 0;
848
849 while (len > printed)
850 {
851 size_t utf8_len, i;
852 char *input, utf8_buf[256];
853
854 /* Convert STR to UTF-8. */
855 for (i = printed, utf8_len = 0, input = utf8_buf;
856 i < len && utf8_len + 4 < sizeof (utf8_buf);
857 i++)
858 {
859 utf8_len += codepoint_to_utf8 (STR_REF (str, i),
860 (scm_t_uint8 *) input);
861 input = utf8_buf + utf8_len;
862 }
863
864 /* INPUT was successfully converted, entirely; print the
865 result. */
866 scm_lfwrite (utf8_buf, utf8_len, port);
867 printed += i - printed;
868 }
869
870 assert (printed == len);
871
872 return len;
873}
874
875/* Convert STR through PORT's output conversion descriptor and write the
876 output to PORT. Return the number of codepoints written. */
877static size_t
878display_string_using_iconv (const void *str, int narrow_p, size_t len,
879 SCM port,
880 scm_t_string_failed_conversion_handler strategy)
881{
f4bc4e59 882 size_t printed;
337edc59 883 scm_t_iconv_descriptors *id;
cdd3d6c9 884 scm_t_port_internal *pti = SCM_PORT_GET_INTERNAL (port);
f4bc4e59 885
cdd3d6c9
MW
886 id = scm_i_port_iconv_descriptors (port, SCM_PORT_WRITE);
887
888 if (SCM_UNLIKELY (pti->at_stream_start_for_bom_write && len > 0))
889 {
890 scm_t_port *pt = SCM_PTAB_ENTRY (port);
891
892 /* Record that we're no longer at stream start. */
893 pti->at_stream_start_for_bom_write = 0;
894 if (pt->rw_random)
895 pti->at_stream_start_for_bom_read = 0;
896
897 /* Write a BOM if appropriate. */
898 if (SCM_UNLIKELY (strcasecmp(pt->encoding, "UTF-16") == 0
899 || strcasecmp(pt->encoding, "UTF-32") == 0))
900 display_character (SCM_UNICODE_BOM, port, iconveh_error);
901 }
f4bc4e59 902
f4bc4e59
LC
903 printed = 0;
904
905 while (len > printed)
906 {
907 size_t done, utf8_len, input_left, output_left, i;
908 size_t codepoints_read, output_len;
909 char *input, *output;
910 char utf8_buf[256], encoded_output[256];
911 size_t offsets[256];
912
913 /* Convert STR to UTF-8. */
914 for (i = printed, utf8_len = 0, input = utf8_buf;
915 i < len && utf8_len + 4 < sizeof (utf8_buf);
916 i++)
07f49ac7 917 {
f4bc4e59
LC
918 offsets[utf8_len] = i;
919 utf8_len += codepoint_to_utf8 (STR_REF (str, i),
920 (scm_t_uint8 *) input);
921 input = utf8_buf + utf8_len;
922 }
923
924 input = utf8_buf;
925 input_left = utf8_len;
926
927 output = encoded_output;
928 output_left = sizeof (encoded_output);
929
337edc59 930 done = iconv (id->output_cd, &input, &input_left,
f4bc4e59 931 &output, &output_left);
07f49ac7 932
f4bc4e59
LC
933 output_len = sizeof (encoded_output) - output_left;
934
935 if (SCM_UNLIKELY (done == (size_t) -1))
936 {
b2548e23
AW
937 int errno_save = errno;
938
f4bc4e59 939 /* Reset the `iconv' state. */
337edc59 940 iconv (id->output_cd, NULL, NULL, NULL, NULL);
f4bc4e59 941
7174bc08
LC
942 /* Print the OUTPUT_LEN bytes successfully converted. */
943 scm_lfwrite (encoded_output, output_len, port);
944
945 /* See how many input codepoints these OUTPUT_LEN bytes
946 corresponds to. */
947 codepoints_read = offsets[input - utf8_buf] - printed;
948 printed += codepoints_read;
949
b2548e23 950 if (errno_save == EILSEQ &&
f4bc4e59 951 strategy != SCM_FAILED_CONVERSION_ERROR)
07f49ac7 952 {
f4bc4e59
LC
953 /* Conversion failed somewhere in INPUT and we want to
954 escape or substitute the offending input character. */
955
f4bc4e59 956 if (strategy == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE)
f1ee6d54 957 {
f4bc4e59
LC
958 scm_t_wchar ch;
959
960 /* Find CH, the offending codepoint, and escape it. */
961 ch = STR_REF (str, offsets[input - utf8_buf]);
962 write_character_escaped (ch, 1, port);
f1ee6d54 963 }
07f49ac7 964 else
f4bc4e59
LC
965 /* STRATEGY is `SCM_FAILED_CONVERSION_QUESTION_MARK'. */
966 display_string ("?", 1, 1, port, strategy);
9c44cd45 967
f4bc4e59
LC
968 printed++;
969 }
970 else
971 /* Something bad happened that we can't handle: bail out. */
972 break;
07f49ac7
LC
973 }
974 else
f4bc4e59
LC
975 {
976 /* INPUT was successfully converted, entirely; print the
977 result. */
978 scm_lfwrite (encoded_output, output_len, port);
979 codepoints_read = i - printed;
980 printed += codepoints_read;
981 }
07f49ac7
LC
982 }
983
984 return printed;
7b292a9d
LC
985}
986
f4bc4e59 987#undef STR_REF
7b292a9d
LC
988
989/* Display the LEN codepoints in STR to PORT according to STRATEGY;
990 return the number of codepoints successfully displayed. If NARROW_P,
991 then STR is interpreted as a sequence of `char', denoting a Latin-1
992 string; otherwise it's interpreted as a sequence of
993 `scm_t_wchar'. */
994static size_t
995display_string (const void *str, int narrow_p,
996 size_t len, SCM port,
997 scm_t_string_failed_conversion_handler strategy)
998
999{
e4598559 1000 scm_t_port_internal *pti;
7b292a9d 1001
e4598559 1002 pti = SCM_PORT_GET_INTERNAL (port);
7b292a9d 1003
337edc59 1004 if (pti->encoding_mode == SCM_PORT_ENCODING_MODE_UTF8)
7b292a9d
LC
1005 return display_string_as_utf8 (str, narrow_p, len, port);
1006 else
1007 return display_string_using_iconv (str, narrow_p, len,
1008 port, strategy);
f4bc4e59
LC
1009}
1010
1011/* Attempt to display CH to PORT according to STRATEGY. Return non-zero
1012 if CH was successfully displayed, zero otherwise (e.g., if it was not
1013 representable in PORT's encoding.) */
1014static int
1015display_character (scm_t_wchar ch, SCM port,
1016 scm_t_string_failed_conversion_handler strategy)
1017{
1018 return display_string (&ch, 0, 1, port, strategy) == 1;
07f49ac7
LC
1019}
1020
33d92fe6
LC
1021/* Attempt to pretty-print CH, a combining character, to PORT. Return
1022 zero upon failure, non-zero otherwise. The idea is to print CH above
1023 a dotted circle to make it more visible. */
1024static int
1025write_combining_character (scm_t_wchar ch, SCM port)
1026{
f4bc4e59
LC
1027 scm_t_wchar str[2];
1028
1029 str[0] = SCM_CODEPOINT_DOTTED_CIRCLE;
1030 str[1] = ch;
1031
1032 return display_string (str, 0, 2, port, iconveh_error) == 2;
1033}
33d92fe6 1034
f4bc4e59
LC
1035/* Write CH to PORT in its escaped form, using the string escape syntax
1036 if STRING_ESCAPES_P is non-zero. */
1037static void
1038write_character_escaped (scm_t_wchar ch, int string_escapes_p, SCM port)
1039{
1040 if (string_escapes_p)
33d92fe6 1041 {
f4bc4e59
LC
1042 /* Represent CH using the in-string escape syntax. */
1043
1044 static const char hex[] = "0123456789abcdef";
1045 static const char escapes[7] = "abtnvfr";
1046 char buf[9];
1047
1048 if (ch >= 0x07 && ch <= 0x0D && ch != 0x0A)
33d92fe6 1049 {
f4bc4e59
LC
1050 /* Use special escapes for some C0 controls. */
1051 buf[0] = '\\';
1052 buf[1] = escapes[ch - 0x07];
1053 scm_lfwrite (buf, 2, port);
1054 }
1055 else if (!SCM_R6RS_ESCAPES_P)
1056 {
1057 if (ch <= 0xFF)
1058 {
1059 buf[0] = '\\';
1060 buf[1] = 'x';
1061 buf[2] = hex[ch / 16];
1062 buf[3] = hex[ch % 16];
1063 scm_lfwrite (buf, 4, port);
1064 }
1065 else if (ch <= 0xFFFF)
1066 {
1067 buf[0] = '\\';
1068 buf[1] = 'u';
1069 buf[2] = hex[(ch & 0xF000) >> 12];
1070 buf[3] = hex[(ch & 0xF00) >> 8];
1071 buf[4] = hex[(ch & 0xF0) >> 4];
1072 buf[5] = hex[(ch & 0xF)];
1073 scm_lfwrite (buf, 6, port);
1074 }
1075 else if (ch > 0xFFFF)
1076 {
1077 buf[0] = '\\';
1078 buf[1] = 'U';
1079 buf[2] = hex[(ch & 0xF00000) >> 20];
1080 buf[3] = hex[(ch & 0xF0000) >> 16];
1081 buf[4] = hex[(ch & 0xF000) >> 12];
1082 buf[5] = hex[(ch & 0xF00) >> 8];
1083 buf[6] = hex[(ch & 0xF0) >> 4];
1084 buf[7] = hex[(ch & 0xF)];
1085 scm_lfwrite (buf, 8, port);
1086 }
33d92fe6
LC
1087 }
1088 else
f4bc4e59
LC
1089 {
1090 /* Print an R6RS variable-length hex escape: "\xNNNN;". */
1091 scm_t_wchar ch2 = ch;
1092
1093 int i = 8;
1094 buf[i] = ';';
1095 i --;
1096 if (ch == 0)
1097 buf[i--] = '0';
1098 else
1099 while (ch2 > 0)
1100 {
1101 buf[i] = hex[ch2 & 0xF];
1102 ch2 >>= 4;
1103 i --;
1104 }
1105 buf[i] = 'x';
1106 i --;
1107 buf[i] = '\\';
1108 scm_lfwrite (buf + i, 9 - i, port);
1109 }
33d92fe6
LC
1110 }
1111 else
f4bc4e59
LC
1112 {
1113 /* Represent CH using the character escape syntax. */
1114 const char *name;
33d92fe6 1115
f4bc4e59
LC
1116 name = scm_i_charname (SCM_MAKE_CHAR (ch));
1117 if (name != NULL)
1118 scm_puts (name, port);
1119 else
1120 PRINT_CHAR_ESCAPE (ch, port);
1121 }
33d92fe6
LC
1122}
1123
07f49ac7
LC
1124/* Write CH to PORT, escaping it if it's non-graphic or not
1125 representable in PORT's encoding. If STRING_ESCAPES_P is true and CH
1126 needs to be escaped, it is escaped using the in-string escape syntax;
1127 otherwise the character escape syntax is used. */
1128static void
1129write_character (scm_t_wchar ch, SCM port, int string_escapes_p)
1130{
1131 int printed = 0;
f4bc4e59
LC
1132 scm_t_string_failed_conversion_handler strategy;
1133
478848cb 1134 strategy = PORT_CONVERSION_HANDLER (port);
07f49ac7
LC
1135
1136 if (string_escapes_p)
1137 {
1138 /* Check if CH deserves special treatment. */
1139 if (ch == '"' || ch == '\\')
1140 {
f4bc4e59
LC
1141 display_character ('\\', port, iconveh_question_mark);
1142 display_character (ch, port, strategy);
07f49ac7
LC
1143 printed = 1;
1144 }
8500b186
AW
1145 else if (ch == '\n' && SCM_PRINT_ESCAPE_NEWLINES_P)
1146 {
1147 display_character ('\\', port, iconveh_question_mark);
1148 display_character ('n', port, strategy);
1149 printed = 1;
1150 }
07f49ac7
LC
1151 else if (ch == ' ' || ch == '\n')
1152 {
f4bc4e59 1153 display_character (ch, port, strategy);
07f49ac7
LC
1154 printed = 1;
1155 }
1156 }
1157 else
33d92fe6 1158 {
f4bc4e59 1159 display_string ("#\\", 1, 2, port, iconveh_question_mark);
33d92fe6
LC
1160
1161 if (uc_combining_class (ch) != UC_CCC_NR)
1162 /* Character is a combining character, so attempt to
1163 pretty-print it. */
1164 printed = write_combining_character (ch, port);
1165 }
07f49ac7
LC
1166
1167 if (!printed
1168 && uc_is_general_category_withtable (ch,
1169 UC_CATEGORY_MASK_L |
1170 UC_CATEGORY_MASK_M |
1171 UC_CATEGORY_MASK_N |
1172 UC_CATEGORY_MASK_P |
1173 UC_CATEGORY_MASK_S))
1174 /* CH is graphic; attempt to display it. */
1175 printed = display_character (ch, port, iconveh_error);
1176
1177 if (!printed)
f4bc4e59
LC
1178 /* CH isn't graphic or cannot be represented in PORT's encoding. */
1179 write_character_escaped (ch, string_escapes_p, port);
9c44cd45 1180}
0f2d19dd 1181
b908768a
LC
1182/* Display STR to PORT from START inclusive to END exclusive. */
1183void
1184scm_i_display_substring (SCM str, size_t start, size_t end, SCM port)
1185{
1186 int narrow_p;
1187 const char *buf;
1188 size_t len, printed;
1189
1190 buf = scm_i_string_data (str);
1191 len = end - start;
1192 narrow_p = scm_i_is_narrow_string (str);
1193 buf += start * (narrow_p ? sizeof (char) : sizeof (scm_t_wchar));
1194
1195 printed = display_string (buf, narrow_p, end - start, port,
1196 PORT_CONVERSION_HANDLER (port));
1197
1198 if (SCM_UNLIKELY (printed < len))
1199 scm_encoding_error (__func__, errno,
1200 "cannot convert to output locale",
1201 port, scm_c_string_ref (str, printed + start));
1202}
1203
1204\f
0f2d19dd
JB
1205/* Print an integer.
1206 */
1cc91f1b 1207
0f2d19dd 1208void
a406c9e9 1209scm_intprint (scm_t_intmax n, int radix, SCM port)
0f2d19dd
JB
1210{
1211 char num_buf[SCM_INTBUFLEN];
b7f3516f 1212 scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port);
0f2d19dd
JB
1213}
1214
a406c9e9
MV
1215void
1216scm_uintprint (scm_t_uintmax n, int radix, SCM port)
1217{
1218 char num_buf[SCM_INTBUFLEN];
1219 scm_lfwrite (num_buf, scm_iuint2str (n, radix, num_buf), port);
1220}
1221
0f2d19dd
JB
1222/* Print an object of unrecognized type.
1223 */
1cc91f1b 1224
0f2d19dd 1225void
1bbd0b84 1226scm_ipruk (char *hdr, SCM ptr, SCM port)
0f2d19dd 1227{
b7f3516f
TT
1228 scm_puts ("#<unknown-", port);
1229 scm_puts (hdr, port);
26224b3f 1230 if (1) /* (scm_in_heap_p (ptr)) */ /* FIXME */
0f2d19dd 1231 {
b7f3516f 1232 scm_puts (" (0x", port);
0345e278 1233 scm_uintprint (SCM_CELL_WORD_0 (ptr), 16, port);
b7f3516f 1234 scm_puts (" . 0x", port);
0345e278 1235 scm_uintprint (SCM_CELL_WORD_1 (ptr), 16, port);
b7f3516f 1236 scm_puts (") @", port);
0f2d19dd 1237 }
b7f3516f 1238 scm_puts (" 0x", port);
0345e278 1239 scm_uintprint (SCM_UNPACK (ptr), 16, port);
b7f3516f 1240 scm_putc ('>', port);
0f2d19dd
JB
1241}
1242
1cc91f1b 1243
904a077d 1244/* Print a list.
22a52da1 1245 */
0f2d19dd 1246void
34d19ef6 1247scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate)
0f2d19dd 1248{
c62fbfe1 1249 register SCM hare, tortoise;
c014a02e 1250 long floor = pstate->top - 2;
b7f3516f 1251 scm_puts (hdr, port);
0f2d19dd 1252 /* CHECK_INTS; */
c62fbfe1
MD
1253 if (pstate->fancyp)
1254 goto fancy_printing;
1255
1256 /* Run a hare and tortoise so that total time complexity will be
1257 O(depth * N) instead of O(N^2). */
1258 hare = SCM_CDR (exp);
1259 tortoise = exp;
d2e53ed6 1260 while (scm_is_pair (hare))
c62fbfe1 1261 {
bc36d050 1262 if (scm_is_eq (hare, tortoise))
c62fbfe1
MD
1263 goto fancy_printing;
1264 hare = SCM_CDR (hare);
d2e53ed6 1265 if (!scm_is_pair (hare))
c62fbfe1
MD
1266 break;
1267 hare = SCM_CDR (hare);
1268 tortoise = SCM_CDR (tortoise);
1269 }
1270
1271 /* No cdr cycles intrinsic to this list */
1272 scm_iprin1 (SCM_CAR (exp), port, pstate);
d2e53ed6 1273 for (exp = SCM_CDR (exp); scm_is_pair (exp); exp = SCM_CDR (exp))
0f2d19dd 1274 {
c014a02e 1275 register long i;
5ca6dc39 1276
c62fbfe1 1277 for (i = floor; i >= 0; --i)
509759dd 1278 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
c62fbfe1
MD
1279 goto circref;
1280 PUSH_REF (pstate, exp);
b7f3516f 1281 scm_putc (' ', port);
0f2d19dd 1282 /* CHECK_INTS; */
c62fbfe1 1283 scm_iprin1 (SCM_CAR (exp), port, pstate);
0f2d19dd 1284 }
c96d76b8 1285 if (!SCM_NULL_OR_NIL_P (exp))
0f2d19dd 1286 {
b7f3516f 1287 scm_puts (" . ", port);
c62fbfe1 1288 scm_iprin1 (exp, port, pstate);
0f2d19dd 1289 }
c62fbfe1 1290
a51ea417 1291end:
b7f3516f 1292 scm_putc (tlr, port);
c62fbfe1 1293 pstate->top = floor + 2;
a51ea417 1294 return;
c62fbfe1
MD
1295
1296fancy_printing:
1297 {
c014a02e 1298 long n = pstate->length;
c62fbfe1
MD
1299
1300 scm_iprin1 (SCM_CAR (exp), port, pstate);
1301 exp = SCM_CDR (exp); --n;
d2e53ed6 1302 for (; scm_is_pair (exp); exp = SCM_CDR (exp))
c62fbfe1 1303 {
c014a02e 1304 register unsigned long i;
5ca6dc39 1305
c62fbfe1 1306 for (i = 0; i < pstate->top; ++i)
509759dd 1307 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
c62fbfe1
MD
1308 goto fancy_circref;
1309 if (pstate->fancyp)
1310 {
1311 if (n == 0)
1312 {
b7f3516f 1313 scm_puts (" ...", port);
c62fbfe1
MD
1314 goto skip_tail;
1315 }
1316 else
1317 --n;
1318 }
1319 PUSH_REF(pstate, exp);
1320 ++pstate->list_offset;
b7f3516f 1321 scm_putc (' ', port);
c62fbfe1
MD
1322 /* CHECK_INTS; */
1323 scm_iprin1 (SCM_CAR (exp), port, pstate);
1324 }
1325 }
c96d76b8 1326 if (!SCM_NULL_OR_NIL_P (exp))
c62fbfe1 1327 {
b7f3516f 1328 scm_puts (" . ", port);
c62fbfe1
MD
1329 scm_iprin1 (exp, port, pstate);
1330 }
1331skip_tail:
1332 pstate->list_offset -= pstate->top - floor - 2;
a51ea417 1333 goto end;
a51ea417 1334
c62fbfe1
MD
1335fancy_circref:
1336 pstate->list_offset -= pstate->top - floor - 2;
1337
1338circref:
b7f3516f 1339 scm_puts (" . ", port);
c62fbfe1
MD
1340 print_circref (port, pstate, exp);
1341 goto end;
0f2d19dd
JB
1342}
1343
1344\f
1345
bb35f315
MV
1346int
1347scm_valid_oport_value_p (SCM val)
1348{
368cf54d
GB
1349 return (SCM_OPOUTPORTP (val)
1350 || (SCM_PORT_WITH_PS_P (val)
1351 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val))));
bb35f315
MV
1352}
1353
8b840115 1354/* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
1cc91f1b 1355
0f2d19dd 1356SCM
1bbd0b84 1357scm_write (SCM obj, SCM port)
0f2d19dd
JB
1358{
1359 if (SCM_UNBNDP (port))
9de87eea 1360 port = scm_current_output_port ();
3eb7e6ee
JB
1361
1362 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
bb35f315 1363
a51ea417 1364 scm_prin1 (obj, port, 1);
0f2d19dd
JB
1365 return SCM_UNSPECIFIED;
1366}
1367
1368
8b840115 1369/* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
1cc91f1b 1370
0f2d19dd 1371SCM
1bbd0b84 1372scm_display (SCM obj, SCM port)
0f2d19dd
JB
1373{
1374 if (SCM_UNBNDP (port))
9de87eea 1375 port = scm_current_output_port ();
3eb7e6ee
JB
1376
1377 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
bb35f315 1378
a51ea417 1379 scm_prin1 (obj, port, 0);
0f2d19dd
JB
1380 return SCM_UNSPECIFIED;
1381}
1382
70d63753
GB
1383
1384SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
1385 (SCM destination, SCM message, SCM args),
eca65e90
MG
1386 "Write @var{message} to @var{destination}, defaulting to\n"
1387 "the current output port.\n"
1388 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
1389 "@code{~S} (was @code{%S}) escapes. When printed,\n"
1390 "the escapes are replaced with corresponding members of\n"
b7e64f8b 1391 "@var{args}:\n"
eca65e90
MG
1392 "@code{~A} formats using @code{display} and @code{~S} formats\n"
1393 "using @code{write}.\n"
1394 "If @var{destination} is @code{#t}, then use the current output\n"
1395 "port, if @var{destination} is @code{#f}, then return a string\n"
1396 "containing the formatted text. Does not add a trailing newline.")
70d63753
GB
1397#define FUNC_NAME s_scm_simple_format
1398{
dfd03fb9 1399 SCM port, answer = SCM_UNSPECIFIED;
70d63753
GB
1400 int fReturnString = 0;
1401 int writingp;
889975e5 1402 size_t start, p, end;
70d63753 1403
bc36d050 1404 if (scm_is_eq (destination, SCM_BOOL_T))
daba1a71 1405 {
9de87eea 1406 destination = port = scm_current_output_port ();
daba1a71 1407 }
7888309b 1408 else if (scm_is_false (destination))
daba1a71
MD
1409 {
1410 fReturnString = 1;
0b2c2ba3 1411 port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
dfd03fb9
MD
1412 SCM_OPN | SCM_WRTNG,
1413 FUNC_NAME);
1414 destination = port;
daba1a71
MD
1415 }
1416 else
1417 {
1418 SCM_VALIDATE_OPORT_VALUE (1, destination);
dfd03fb9 1419 port = SCM_COERCE_OUTPORT (destination);
daba1a71
MD
1420 }
1421 SCM_VALIDATE_STRING (2, message);
af45e3b0 1422 SCM_VALIDATE_REST_ARGUMENT (args);
70d63753 1423
889975e5
MG
1424 p = 0;
1425 start = 0;
1426 end = scm_i_string_length (message);
b24b5e13 1427 for (p = start; p != end; ++p)
889975e5 1428 if (scm_i_string_ref (message, p) == '~')
70d63753 1429 {
b24b5e13 1430 if (++p == end)
6662998f
MV
1431 break;
1432
889975e5 1433 switch (scm_i_string_ref (message, p))
6662998f
MV
1434 {
1435 case 'A': case 'a':
1436 writingp = 0;
1437 break;
1438 case 'S': case 's':
1439 writingp = 1;
1440 break;
1441 case '~':
889975e5 1442 scm_lfwrite_substr (message, start, p, port);
6662998f
MV
1443 start = p + 1;
1444 continue;
1445 case '%':
889975e5 1446 scm_lfwrite_substr (message, start, p - 1, port);
dfd03fb9 1447 scm_newline (port);
6662998f
MV
1448 start = p + 1;
1449 continue;
1450 default:
1afff620 1451 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
889975e5 1452 scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message, p))));
6662998f
MV
1453
1454 }
70d63753 1455
6662998f 1456
d2e53ed6 1457 if (!scm_is_pair (args))
1afff620 1458 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
889975e5 1459 scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message, p))));
6662998f 1460
889975e5 1461 scm_lfwrite_substr (message, start, p - 1, port);
dfd03fb9 1462 /* we pass destination here */
70d63753
GB
1463 scm_prin1 (SCM_CAR (args), destination, writingp);
1464 args = SCM_CDR (args);
1465 start = p + 1;
1466 }
6662998f 1467
889975e5 1468 scm_lfwrite_substr (message, start, p, port);
bc36d050 1469 if (!scm_is_eq (args, SCM_EOL))
1afff620
KN
1470 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1471 scm_list_1 (scm_length (args)));
70d63753
GB
1472
1473 if (fReturnString)
1474 answer = scm_strport_to_string (destination);
1475
daba1a71 1476 return scm_return_first (answer, message);
70d63753
GB
1477}
1478#undef FUNC_NAME
1479
1480
3b3b36dd 1481SCM_DEFINE (scm_newline, "newline", 0, 1, 0,
b450f070 1482 (SCM port),
8f85c0c6
NJ
1483 "Send a newline to @var{port}.\n"
1484 "If @var{port} is omitted, send to the current output port.")
1bbd0b84 1485#define FUNC_NAME s_scm_newline
0f2d19dd
JB
1486{
1487 if (SCM_UNBNDP (port))
9de87eea 1488 port = scm_current_output_port ();
3eb7e6ee 1489
34d19ef6 1490 SCM_VALIDATE_OPORT_VALUE (1, port);
bb35f315 1491
0ef4ae82 1492 scm_putc ('\n', SCM_COERCE_OUTPORT (port));
0f2d19dd
JB
1493 return SCM_UNSPECIFIED;
1494}
1bbd0b84 1495#undef FUNC_NAME
0f2d19dd 1496
3b3b36dd 1497SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
b450f070 1498 (SCM chr, SCM port),
eca65e90 1499 "Send character @var{chr} to @var{port}.")
1bbd0b84 1500#define FUNC_NAME s_scm_write_char
0f2d19dd
JB
1501{
1502 if (SCM_UNBNDP (port))
9de87eea 1503 port = scm_current_output_port ();
3eb7e6ee 1504
34d19ef6
HWN
1505 SCM_VALIDATE_CHAR (1, chr);
1506 SCM_VALIDATE_OPORT_VALUE (2, port);
07f49ac7
LC
1507
1508 port = SCM_COERCE_OUTPORT (port);
1509 if (!display_character (SCM_CHAR (chr), port,
478848cb 1510 PORT_CONVERSION_HANDLER (port)))
07f49ac7
LC
1511 scm_encoding_error (__func__, errno,
1512 "cannot convert to output locale",
6851d3be 1513 port, chr);
07f49ac7 1514
0f2d19dd
JB
1515 return SCM_UNSPECIFIED;
1516}
1bbd0b84 1517#undef FUNC_NAME
0f2d19dd 1518
0f2d19dd
JB
1519\f
1520
bb35f315 1521/* Call back to Scheme code to do the printing of special objects
c19bc088
MD
1522 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1523 * containing PORT and PSTATE. This object can be used as the port for
1524 * display/write etc to continue the current print chain. The REVEALED
1525 * field of PSTATE is set to true to indicate that the print state has
1526 * escaped to Scheme and thus has to be freed by the GC.
1527 */
1528
92c2555f 1529scm_t_bits scm_tc16_port_with_ps;
c19bc088
MD
1530
1531/* Print exactly as the port itself would */
1532
1533static int
e841c3e0 1534port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate)
c19bc088
MD
1535{
1536 obj = SCM_PORT_WITH_PS_PORT (obj);
1537 return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate);
1538}
c4f37e80
MV
1539
1540SCM
1bbd0b84 1541scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate)
c4f37e80 1542{
bb35f315 1543 pstate->revealed = 1;
dfd03fb9
MD
1544 return scm_call_2 (proc, exp,
1545 scm_i_port_with_print_state (port, pstate->handle));
c19bc088
MD
1546}
1547
dfd03fb9 1548SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 1, 1, 0,
1bbd0b84 1549 (SCM port, SCM pstate),
71331188 1550 "Create a new port which behaves like @var{port}, but with an\n"
dfd03fb9
MD
1551 "included print state @var{pstate}. @var{pstate} is optional.\n"
1552 "If @var{pstate} isn't supplied and @var{port} already has\n"
1553 "a print state, the old print state is reused.")
1bbd0b84 1554#define FUNC_NAME s_scm_port_with_print_state
c19bc088 1555{
34d19ef6 1556 SCM_VALIDATE_OPORT_VALUE (1, port);
dfd03fb9
MD
1557 if (!SCM_UNBNDP (pstate))
1558 SCM_VALIDATE_PRINTSTATE (2, pstate);
1559 return scm_i_port_with_print_state (port, pstate);
c19bc088 1560}
1bbd0b84 1561#undef FUNC_NAME
c19bc088 1562
a1ec6916 1563SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0,
1bbd0b84 1564 (SCM port),
71331188
MG
1565 "Return the print state of the port @var{port}. If @var{port}\n"
1566 "has no associated print state, @code{#f} is returned.")
1bbd0b84 1567#define FUNC_NAME s_scm_get_print_state
c19bc088 1568{
368cf54d
GB
1569 if (SCM_PORT_WITH_PS_P (port))
1570 return SCM_PORT_WITH_PS_PS (port);
f5f2dcff 1571 if (SCM_OUTPUT_PORT_P (port))
368cf54d 1572 return SCM_BOOL_F;
276dd677 1573 SCM_WRONG_TYPE_ARG (1, port);
c4f37e80 1574}
1bbd0b84 1575#undef FUNC_NAME
bb35f315 1576
c4f37e80 1577\f
1cc91f1b 1578
0f2d19dd
JB
1579void
1580scm_init_print ()
0f2d19dd 1581{
66b1dbf6 1582 SCM type;
d5cf5324 1583
d5cf5324
DH
1584 scm_gc_register_root (&print_state_pool);
1585 scm_gc_register_root (&scm_print_state_vtable);
66b1dbf6
AW
1586 type = scm_make_vtable (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT),
1587 SCM_BOOL_F);
4a655e50 1588 scm_set_struct_vtable_name_x (type, scm_from_latin1_symbol ("print-state"));
bb35f315 1589 scm_print_state_vtable = type;
c4f37e80 1590
c19bc088
MD
1591 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1592 scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
e841c3e0 1593 scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print);
81ae25da 1594
a0599745 1595#include "libguile/print.x"
475fa9a5 1596
8500b186
AW
1597 scm_init_opts (scm_print_options, scm_print_opts);
1598 scm_print_opts[SCM_PRINT_HIGHLIGHT_PREFIX_I].val =
1599 SCM_UNPACK (scm_from_locale_string ("{"));
1600 scm_print_opts[SCM_PRINT_HIGHLIGHT_SUFFIX_I].val =
1601 SCM_UNPACK (scm_from_locale_string ("}"));
475fa9a5 1602 scm_print_opts[SCM_PRINT_KEYWORD_STYLE_I].val = SCM_UNPACK (sym_reader);
0f2d19dd 1603}
89e00824
ML
1604
1605/*
1606 Local Variables:
1607 c-file-style: "gnu"
1608 End:
1609*/