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