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