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