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