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