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