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