Replace global charnames variables with accessors
[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
MD
25#include <errno.h>
26
a0599745
MD
27#include "libguile/_scm.h"
28#include "libguile/chars.h"
a002f1a2 29#include "libguile/continuations.h"
a0599745
MD
30#include "libguile/smob.h"
31#include "libguile/eval.h"
32#include "libguile/macros.h"
33#include "libguile/procprop.h"
34#include "libguile/read.h"
35#include "libguile/weaks.h"
36#include "libguile/unif.h"
37#include "libguile/alist.h"
38#include "libguile/struct.h"
39#include "libguile/objects.h"
40#include "libguile/ports.h"
41#include "libguile/root.h"
42#include "libguile/strings.h"
43#include "libguile/strports.h"
44#include "libguile/vectors.h"
c96d76b8 45#include "libguile/lang.h"
327967ef 46#include "libguile/numbers.h"
a0599745
MD
47
48#include "libguile/validate.h"
49#include "libguile/print.h"
22fc179a
HWN
50
51#include "libguile/private-options.h"
52
0f2d19dd
JB
53\f
54
55/* {Names of immediate symbols}
56 *
57 * This table must agree with the declarations in scm.h: {Immediate Symbols}.
58 */
59
e17d318f
DH
60/* This table must agree with the list of flags in tags.h. */
61static const char *iflagnames[] =
62{
63 "#f",
64 "#t",
65 "#<undefined>",
66 "#<eof>",
67 "()",
68 "#<unspecified>",
69
70 /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
71 "#<unbound>",
72
73 /* Elisp nil value. This is its Scheme name; whenever it's printed in
74 * Elisp, it should appear as the symbol `nil'. */
75 "#nil"
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
MV
294static int
295quote_keywordish_symbol (const char *str, size_t len)
296{
297 SCM option;
298
299 /* LEN is guaranteed to be > 0.
300 */
301 if (str[0] != ':' && str[len-1] != ':')
302 return 0;
303
304 option = SCM_PRINT_KEYWORD_STYLE;
305 if (scm_is_false (option))
306 return 0;
307 if (scm_is_eq (option, sym_reader))
308 return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE));
309 return 1;
310}
311
6662998f
MV
312void
313scm_print_symbol_name (const char *str, size_t len, SCM port)
314{
c6b49e89
MV
315 /* This points to the first character that has not yet been written to the
316 * port. */
317 size_t pos = 0;
318 /* This points to the character we're currently looking at. */
6662998f 319 size_t end;
c6b49e89
MV
320 /* If the name contains weird characters, we'll escape them with
321 * backslashes and set this flag; it indicates that we should surround the
322 * name with "#{" and "}#". */
323 int weird = 0;
324 /* Backslashes are not sufficient to make a name weird, but if a name is
325 * weird because of other characters, backslahes need to be escaped too.
326 * The first time we see a backslash, we set maybe_weird, and mw_pos points
327 * to the backslash. Then if the name turns out to be weird, we re-process
327967ef
MV
328 * everything starting from mw_pos.
329 * We could instead make backslashes always weird. This is not necessary
330 * to ensure that the output is (read)-able, but it would make this code
331 * simpler and faster. */
c6b49e89 332 int maybe_weird = 0;
6662998f 333 size_t mw_pos = 0;
6662998f 334
475fa9a5
MV
335 if (len == 0 || str[0] == '\'' || str[0] == '`' || str[0] == ','
336 || quote_keywordish_symbol (str, len)
337 || (str[0] == '.' && len == 1)
2b829bbb 338 || scm_is_true (scm_c_locale_stringn_to_number (str, len, 10)))
6662998f
MV
339 {
340 scm_lfwrite ("#{", 2, port);
341 weird = 1;
342 }
c6b49e89 343
6662998f
MV
344 for (end = pos; end < len; ++end)
345 switch (str[end])
346 {
347#ifdef BRACKETS_AS_PARENS
348 case '[':
349 case ']':
350#endif
351 case '(':
352 case ')':
353 case '"':
354 case ';':
c6b49e89 355 case '#':
6662998f
MV
356 case SCM_WHITE_SPACES:
357 case SCM_LINE_INCREMENTORS:
358 weird_handler:
359 if (maybe_weird)
360 {
361 end = mw_pos;
362 maybe_weird = 0;
363 }
364 if (!weird)
365 {
366 scm_lfwrite ("#{", 2, port);
367 weird = 1;
368 }
369 if (pos < end)
c6b49e89 370 scm_lfwrite (str + pos, end - pos, port);
6662998f
MV
371 {
372 char buf[2];
373 buf[0] = '\\';
374 buf[1] = str[end];
375 scm_lfwrite (buf, 2, port);
376 }
377 pos = end + 1;
378 break;
379 case '\\':
380 if (weird)
381 goto weird_handler;
382 if (!maybe_weird)
383 {
384 maybe_weird = 1;
385 mw_pos = pos;
386 }
387 break;
6662998f
MV
388 default:
389 break;
390 }
391 if (pos < end)
392 scm_lfwrite (str + pos, end - pos, port);
393 if (weird)
394 scm_lfwrite ("}#", 2, port);
395}
396
c62fbfe1 397/* Print generally. Handles both write and display according to PSTATE.
0f2d19dd 398 */
8b840115
MD
399SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write);
400SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
1cc91f1b 401
d232520a
MV
402static void iprin1 (SCM exp, SCM port, scm_print_state *pstate);
403
0f2d19dd 404void
1bbd0b84 405scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate)
d232520a
MV
406{
407 if (pstate->fancyp
408 && scm_is_true (scm_memq (exp, pstate->highlight_objects)))
409 {
81ae25da 410 scm_display (SCM_PRINT_HIGHLIGHT_PREFIX, port);
d232520a 411 iprin1 (exp, port, pstate);
81ae25da 412 scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX, port);
d232520a
MV
413 }
414 else
415 iprin1 (exp, port, pstate);
416}
417
418static void
419iprin1 (SCM exp, SCM port, scm_print_state *pstate)
0f2d19dd 420{
54778cd3 421 switch (SCM_ITAG3 (exp))
0f2d19dd 422 {
e34f941a
DH
423 case scm_tc3_closure:
424 case scm_tc3_tc7_1:
425 case scm_tc3_tc7_2:
426 /* These tc3 tags should never occur in an immediate value. They are
427 * only used in cell types of non-immediates, i. e. the value returned
428 * by SCM_CELL_TYPE (exp) can use these tags.
429 */
430 scm_ipruk ("immediate", exp, port);
431 break;
432 case scm_tc3_int_1:
433 case scm_tc3_int_2:
e11e83f3 434 scm_intprint (SCM_I_INUM (exp), 10, port);
0f2d19dd 435 break;
e34f941a 436 case scm_tc3_imm24:
7866a09b 437 if (SCM_CHARP (exp))
0f2d19dd 438 {
e34f941a 439 long i = SCM_CHAR (exp);
77332b21 440 const char *name;
5ca6dc39 441
b7f3516f
TT
442 if (SCM_WRITINGP (pstate))
443 {
444 scm_puts ("#\\", port);
77332b21
MG
445 name = scm_i_charname (exp);
446 if (name != NULL)
447 scm_puts (name, port);
b7f3516f
TT
448 else if (i < 0 || i > '\177')
449 scm_intprint (i, 8, port);
450 else
451 scm_putc (i, port);
452 }
453 else
454 scm_putc (i, port);
0f2d19dd 455 }
a51ea417 456 else if (SCM_IFLAGP (exp)
e17d318f
DH
457 && ((size_t) SCM_IFLAGNUM (exp) < (sizeof iflagnames / sizeof (char *))))
458 {
459 scm_puts (iflagnames [SCM_IFLAGNUM (exp)], port);
460 }
7e6e6b37 461 else if (SCM_ISYMP (exp))
e17d318f 462 {
7e6e6b37 463 scm_i_print_isym (exp, port);
e17d318f 464 }
0f2d19dd
JB
465 else if (SCM_ILOCP (exp))
466 {
7e6e6b37 467 scm_i_print_iloc (exp, port);
0f2d19dd
JB
468 }
469 else
e34f941a
DH
470 {
471 /* unknown immediate value */
472 scm_ipruk ("immediate", exp, port);
473 }
0f2d19dd 474 break;
e34f941a 475 case scm_tc3_cons:
0f2d19dd
JB
476 switch (SCM_TYP7 (exp))
477 {
904a077d
MV
478 case scm_tcs_struct:
479 {
480 ENTER_NESTED_DATA (pstate, exp, circref);
481 if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS)
482 {
483 SCM pwps, print = pstate->writingp ? g_write : g_display;
484 if (!print)
485 goto print_struct;
dfd03fb9 486 pwps = scm_i_port_with_print_state (port, pstate->handle);
7663c008 487 pstate->revealed = 1;
904a077d
MV
488 scm_call_generic_2 (print, exp, pwps);
489 }
490 else
491 {
492 print_struct:
493 scm_print_struct (exp, port, pstate);
494 }
495 EXIT_NESTED_DATA (pstate);
496 }
497 break;
0f2d19dd
JB
498 case scm_tcs_cons_imcar:
499 case scm_tcs_cons_nimcar:
c62fbfe1
MD
500 ENTER_NESTED_DATA (pstate, exp, circref);
501 scm_iprlist ("(", exp, ')', port, pstate);
502 EXIT_NESTED_DATA (pstate);
a51ea417
MD
503 break;
504 circref:
c62fbfe1 505 print_circref (port, pstate, exp);
0f2d19dd
JB
506 break;
507 case scm_tcs_closures:
7888309b
MV
508 if (scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE))
509 || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE,
77c25af7 510 exp, port, pstate)))
726d810a
DH
511 {
512 SCM formals = SCM_CLOSURE_FORMALS (exp);
513 scm_puts ("#<procedure", port);
514 scm_putc (' ', port);
515 scm_iprin1 (scm_procedure_name (exp), port, pstate);
516 scm_putc (' ', port);
517 if (SCM_PRINT_SOURCE_P)
518 {
519 SCM env = SCM_ENV (exp);
520 SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env);
9fcf3cbb 521 SCM src = scm_i_unmemocopy_body (SCM_CODE (exp), xenv);
726d810a
DH
522 ENTER_NESTED_DATA (pstate, exp, circref);
523 scm_iprin1 (src, port, pstate);
524 EXIT_NESTED_DATA (pstate);
525 }
526 else
527 scm_iprin1 (formals, port, pstate);
b7f3516f 528 scm_putc ('>', port);
726d810a 529 }
0f2d19dd 530 break;
534c55a9
DH
531 case scm_tc7_number:
532 switch SCM_TYP16 (exp) {
533 case scm_tc16_big:
534 scm_bigprint (exp, port, pstate);
535 break;
536 case scm_tc16_real:
537 scm_print_real (exp, port, pstate);
538 break;
539 case scm_tc16_complex:
540 scm_print_complex (exp, port, pstate);
541 break;
f92e85f7
MV
542 case scm_tc16_fraction:
543 scm_i_print_fraction (exp, port, pstate);
544 break;
534c55a9
DH
545 }
546 break;
0f2d19dd 547 case scm_tc7_string:
c62fbfe1 548 if (SCM_WRITINGP (pstate))
0f2d19dd 549 {
6d92f14f 550 size_t i, j, len;
cc95e00a 551 const char *data;
5ca6dc39 552
b7f3516f 553 scm_putc ('"', port);
cc95e00a
MV
554 len = scm_i_string_length (exp);
555 data = scm_i_string_chars (exp);
6d92f14f 556 for (i = 0, j = 0; i < len; ++i)
fea8e142 557 {
cc95e00a 558 unsigned char ch = data[i];
fea8e142
MV
559 if ((ch < 32 && ch != '\n') || (127 <= ch && ch < 148))
560 {
561 static char const hex[]="0123456789abcdef";
6d92f14f
MV
562 char buf[4];
563
564 scm_lfwrite (data+j, i-j, port);
565 buf[0] = '\\';
566 buf[1] = 'x';
567 buf[2] = hex [ch / 16];
568 buf[3] = hex [ch % 16];
569 scm_lfwrite (buf, 4, port);
570 data = scm_i_string_chars (exp);
571 j = i+1;
fea8e142 572 }
6d92f14f 573 else if (ch == '"' || ch == '\\')
fea8e142 574 {
6d92f14f
MV
575 scm_lfwrite (data+j, i-j, port);
576 scm_putc ('\\', port);
577 data = scm_i_string_chars (exp);
578 j = i;
fea8e142
MV
579 }
580 }
6d92f14f 581 scm_lfwrite (data+j, i-j, port);
b7f3516f 582 scm_putc ('"', port);
cc95e00a 583 scm_remember_upto_here_1 (exp);
0f2d19dd
JB
584 }
585 else
cc95e00a 586 scm_lfwrite (scm_i_string_chars (exp), scm_i_string_length (exp),
fea8e142 587 port);
8824ac88 588 scm_remember_upto_here_1 (exp);
0f2d19dd 589 break;
28b06554 590 case scm_tc7_symbol:
cc95e00a 591 if (scm_i_symbol_is_interned (exp))
9ff28a13 592 {
cc95e00a
MV
593 scm_print_symbol_name (scm_i_symbol_chars (exp),
594 scm_i_symbol_length (exp),
9ff28a13
MV
595 port);
596 scm_remember_upto_here_1 (exp);
597 }
598 else
599 {
600 scm_puts ("#<uninterned-symbol ", port);
cc95e00a
MV
601 scm_print_symbol_name (scm_i_symbol_chars (exp),
602 scm_i_symbol_length (exp),
9ff28a13
MV
603 port);
604 scm_putc (' ', port);
0345e278 605 scm_uintprint (SCM_UNPACK (exp), 16, port);
9ff28a13
MV
606 scm_putc ('>', port);
607 }
6662998f 608 break;
e5aca4b5
MV
609 case scm_tc7_variable:
610 scm_i_variable_print (exp, port, pstate);
611 break;
0f2d19dd 612 case scm_tc7_wvect:
c62fbfe1 613 ENTER_NESTED_DATA (pstate, exp, circref);
0f2d19dd 614 if (SCM_IS_WHVEC (exp))
b7f3516f 615 scm_puts ("#wh(", port);
0f2d19dd 616 else
b7f3516f 617 scm_puts ("#w(", port);
0f2d19dd
JB
618 goto common_vector_printer;
619
620 case scm_tc7_vector:
c62fbfe1 621 ENTER_NESTED_DATA (pstate, exp, circref);
b7f3516f 622 scm_puts ("#(", port);
0f2d19dd 623 common_vector_printer:
9fbaf27c 624 {
c014a02e 625 register long i;
4057a3e0 626 long last = SCM_SIMPLE_VECTOR_LENGTH (exp) - 1;
9fbaf27c 627 int cutp = 0;
4057a3e0
MV
628 if (pstate->fancyp
629 && SCM_SIMPLE_VECTOR_LENGTH (exp) > pstate->length)
9fbaf27c
MD
630 {
631 last = pstate->length - 1;
632 cutp = 1;
633 }
634 for (i = 0; i < last; ++i)
635 {
636 /* CHECK_INTS; */
4057a3e0 637 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp, i), port, pstate);
b7f3516f 638 scm_putc (' ', port);
9fbaf27c
MD
639 }
640 if (i == last)
641 {
642 /* CHECK_INTS; */
4057a3e0 643 scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp, i), port, pstate);
9fbaf27c
MD
644 }
645 if (cutp)
b7f3516f
TT
646 scm_puts (" ...", port);
647 scm_putc (')', port);
9fbaf27c 648 }
c62fbfe1 649 EXIT_NESTED_DATA (pstate);
0f2d19dd 650 break;
0f2d19dd 651 case scm_tcs_subrs:
f76af603 652 scm_puts (SCM_SUBR_GENERIC (exp)
9de33deb
MD
653 ? "#<primitive-generic "
654 : "#<primitive-procedure ",
655 port);
ce471ab8 656 scm_puts (scm_i_symbol_chars (SCM_SUBR_NAME (exp)), port);
b7f3516f 657 scm_putc ('>', port);
0f2d19dd 658 break;
e20d7001 659
c180dfd3
MD
660 case scm_tc7_pws:
661 scm_puts ("#<procedure-with-setter", port);
662 {
663 SCM name = scm_procedure_name (exp);
7888309b 664 if (scm_is_true (name))
c180dfd3
MD
665 {
666 scm_putc (' ', port);
b24b5e13 667 scm_display (name, port);
c180dfd3
MD
668 }
669 }
670 scm_putc ('>', port);
671 break;
0f2d19dd 672 case scm_tc7_port:
5ca6dc39
JB
673 {
674 register long i = SCM_PTOBNUM (exp);
675 if (i < scm_numptob
676 && scm_ptobs[i].print
677 && (scm_ptobs[i].print) (exp, port, pstate))
a51ea417 678 break;
5ca6dc39
JB
679 goto punk;
680 }
681 case scm_tc7_smob:
7a7f7c53
DH
682 ENTER_NESTED_DATA (pstate, exp, circref);
683 SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate);
684 EXIT_NESTED_DATA (pstate);
685 break;
0f2d19dd 686 default:
a51ea417
MD
687 punk:
688 scm_ipruk ("type", exp, port);
0f2d19dd
JB
689 }
690 }
691}
692
c62fbfe1
MD
693/* Print states are necessary for circular reference safe printing.
694 * They are also expensive to allocate. Therefore print states are
695 * kept in a pool so that they can be reused.
696 */
1cc91f1b 697
bb35f315
MV
698/* The PORT argument can also be a print-state/port pair, which will
699 * then be used instead of allocating a new print state. This is
700 * useful for continuing a chain of print calls from Scheme. */
701
a51ea417 702void
1bbd0b84 703scm_prin1 (SCM exp, SCM port, int writingp)
a51ea417 704{
c4f37e80
MV
705 SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
706 SCM pstate_scm;
c62fbfe1 707 scm_print_state *pstate;
15635be5 708 int old_writingp;
c62fbfe1 709
bb35f315
MV
710 /* If PORT is a print-state/port pair, use that. Else create a new
711 print-state. */
c4f37e80 712
0c95b57d 713 if (SCM_PORT_WITH_PS_P (port))
bb35f315 714 {
52235e71
MD
715 pstate_scm = SCM_PORT_WITH_PS_PS (port);
716 port = SCM_PORT_WITH_PS_PORT (port);
bb35f315
MV
717 }
718 else
c62fbfe1 719 {
c4f37e80 720 /* First try to allocate a print state from the pool */
9de87eea 721 scm_i_pthread_mutex_lock (&print_state_mutex);
d2e53ed6 722 if (!scm_is_null (print_state_pool))
c4f37e80 723 {
d5cf5324
DH
724 handle = print_state_pool;
725 print_state_pool = SCM_CDR (print_state_pool);
c4f37e80 726 }
9de87eea 727 scm_i_pthread_mutex_unlock (&print_state_mutex);
7888309b 728 if (scm_is_false (handle))
d5cf5324 729 handle = scm_list_1 (make_print_state ());
c4f37e80 730 pstate_scm = SCM_CAR (handle);
c62fbfe1 731 }
c62fbfe1 732
c4f37e80 733 pstate = SCM_PRINT_STATE (pstate_scm);
15635be5 734 old_writingp = pstate->writingp;
c62fbfe1
MD
735 pstate->writingp = writingp;
736 scm_iprin1 (exp, port, pstate);
15635be5 737 pstate->writingp = old_writingp;
c62fbfe1 738
bb35f315
MV
739 /* Return print state to pool if it has been created above and
740 hasn't escaped to Scheme. */
741
7888309b 742 if (scm_is_true (handle) && !pstate->revealed)
c4f37e80 743 {
9de87eea 744 scm_i_pthread_mutex_lock (&print_state_mutex);
d5cf5324
DH
745 SCM_SETCDR (handle, print_state_pool);
746 print_state_pool = handle;
9de87eea 747 scm_i_pthread_mutex_unlock (&print_state_mutex);
c4f37e80 748 }
a51ea417
MD
749}
750
0f2d19dd
JB
751
752/* Print an integer.
753 */
1cc91f1b 754
0f2d19dd 755void
a406c9e9 756scm_intprint (scm_t_intmax n, int radix, SCM port)
0f2d19dd
JB
757{
758 char num_buf[SCM_INTBUFLEN];
b7f3516f 759 scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port);
0f2d19dd
JB
760}
761
a406c9e9
MV
762void
763scm_uintprint (scm_t_uintmax n, int radix, SCM port)
764{
765 char num_buf[SCM_INTBUFLEN];
766 scm_lfwrite (num_buf, scm_iuint2str (n, radix, num_buf), port);
767}
768
0f2d19dd
JB
769/* Print an object of unrecognized type.
770 */
1cc91f1b 771
0f2d19dd 772void
1bbd0b84 773scm_ipruk (char *hdr, SCM ptr, SCM port)
0f2d19dd 774{
b7f3516f
TT
775 scm_puts ("#<unknown-", port);
776 scm_puts (hdr, port);
c8a1bdc4 777 if (scm_in_heap_p (ptr))
0f2d19dd 778 {
b7f3516f 779 scm_puts (" (0x", port);
0345e278 780 scm_uintprint (SCM_CELL_WORD_0 (ptr), 16, port);
b7f3516f 781 scm_puts (" . 0x", port);
0345e278 782 scm_uintprint (SCM_CELL_WORD_1 (ptr), 16, port);
b7f3516f 783 scm_puts (") @", port);
0f2d19dd 784 }
b7f3516f 785 scm_puts (" 0x", port);
0345e278 786 scm_uintprint (SCM_UNPACK (ptr), 16, port);
b7f3516f 787 scm_putc ('>', port);
0f2d19dd
JB
788}
789
1cc91f1b 790
904a077d 791/* Print a list.
22a52da1 792 */
0f2d19dd 793void
34d19ef6 794scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate)
0f2d19dd 795{
c62fbfe1 796 register SCM hare, tortoise;
c014a02e 797 long floor = pstate->top - 2;
b7f3516f 798 scm_puts (hdr, port);
0f2d19dd 799 /* CHECK_INTS; */
c62fbfe1
MD
800 if (pstate->fancyp)
801 goto fancy_printing;
802
803 /* Run a hare and tortoise so that total time complexity will be
804 O(depth * N) instead of O(N^2). */
805 hare = SCM_CDR (exp);
806 tortoise = exp;
d2e53ed6 807 while (scm_is_pair (hare))
c62fbfe1 808 {
bc36d050 809 if (scm_is_eq (hare, tortoise))
c62fbfe1
MD
810 goto fancy_printing;
811 hare = SCM_CDR (hare);
d2e53ed6 812 if (!scm_is_pair (hare))
c62fbfe1
MD
813 break;
814 hare = SCM_CDR (hare);
815 tortoise = SCM_CDR (tortoise);
816 }
817
818 /* No cdr cycles intrinsic to this list */
819 scm_iprin1 (SCM_CAR (exp), port, pstate);
d2e53ed6 820 for (exp = SCM_CDR (exp); scm_is_pair (exp); exp = SCM_CDR (exp))
0f2d19dd 821 {
c014a02e 822 register long i;
5ca6dc39 823
c62fbfe1 824 for (i = floor; i >= 0; --i)
509759dd 825 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
c62fbfe1
MD
826 goto circref;
827 PUSH_REF (pstate, exp);
b7f3516f 828 scm_putc (' ', port);
0f2d19dd 829 /* CHECK_INTS; */
c62fbfe1 830 scm_iprin1 (SCM_CAR (exp), port, pstate);
0f2d19dd 831 }
c96d76b8 832 if (!SCM_NULL_OR_NIL_P (exp))
0f2d19dd 833 {
b7f3516f 834 scm_puts (" . ", port);
c62fbfe1 835 scm_iprin1 (exp, port, pstate);
0f2d19dd 836 }
c62fbfe1 837
a51ea417 838end:
b7f3516f 839 scm_putc (tlr, port);
c62fbfe1 840 pstate->top = floor + 2;
a51ea417 841 return;
c62fbfe1
MD
842
843fancy_printing:
844 {
c014a02e 845 long n = pstate->length;
c62fbfe1
MD
846
847 scm_iprin1 (SCM_CAR (exp), port, pstate);
848 exp = SCM_CDR (exp); --n;
d2e53ed6 849 for (; scm_is_pair (exp); exp = SCM_CDR (exp))
c62fbfe1 850 {
c014a02e 851 register unsigned long i;
5ca6dc39 852
c62fbfe1 853 for (i = 0; i < pstate->top; ++i)
509759dd 854 if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
c62fbfe1
MD
855 goto fancy_circref;
856 if (pstate->fancyp)
857 {
858 if (n == 0)
859 {
b7f3516f 860 scm_puts (" ...", port);
c62fbfe1
MD
861 goto skip_tail;
862 }
863 else
864 --n;
865 }
866 PUSH_REF(pstate, exp);
867 ++pstate->list_offset;
b7f3516f 868 scm_putc (' ', port);
c62fbfe1
MD
869 /* CHECK_INTS; */
870 scm_iprin1 (SCM_CAR (exp), port, pstate);
871 }
872 }
c96d76b8 873 if (!SCM_NULL_OR_NIL_P (exp))
c62fbfe1 874 {
b7f3516f 875 scm_puts (" . ", port);
c62fbfe1
MD
876 scm_iprin1 (exp, port, pstate);
877 }
878skip_tail:
879 pstate->list_offset -= pstate->top - floor - 2;
a51ea417 880 goto end;
a51ea417 881
c62fbfe1
MD
882fancy_circref:
883 pstate->list_offset -= pstate->top - floor - 2;
884
885circref:
b7f3516f 886 scm_puts (" . ", port);
c62fbfe1
MD
887 print_circref (port, pstate, exp);
888 goto end;
0f2d19dd
JB
889}
890
891\f
892
bb35f315
MV
893int
894scm_valid_oport_value_p (SCM val)
895{
368cf54d
GB
896 return (SCM_OPOUTPORTP (val)
897 || (SCM_PORT_WITH_PS_P (val)
898 && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val))));
bb35f315
MV
899}
900
8b840115 901/* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
1cc91f1b 902
0f2d19dd 903SCM
1bbd0b84 904scm_write (SCM obj, SCM port)
0f2d19dd
JB
905{
906 if (SCM_UNBNDP (port))
9de87eea 907 port = scm_current_output_port ();
3eb7e6ee
JB
908
909 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
bb35f315 910
a51ea417 911 scm_prin1 (obj, port, 1);
23d72566 912#if 0
0f2d19dd
JB
913#ifdef HAVE_PIPE
914# ifdef EPIPE
915 if (EPIPE == errno)
916 scm_close_port (port);
917# endif
23d72566 918#endif
0f2d19dd
JB
919#endif
920 return SCM_UNSPECIFIED;
921}
922
923
8b840115 924/* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
1cc91f1b 925
0f2d19dd 926SCM
1bbd0b84 927scm_display (SCM obj, SCM port)
0f2d19dd
JB
928{
929 if (SCM_UNBNDP (port))
9de87eea 930 port = scm_current_output_port ();
3eb7e6ee
JB
931
932 SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
bb35f315 933
a51ea417 934 scm_prin1 (obj, port, 0);
23d72566 935#if 0
0f2d19dd
JB
936#ifdef HAVE_PIPE
937# ifdef EPIPE
938 if (EPIPE == errno)
939 scm_close_port (port);
940# endif
23d72566 941#endif
0f2d19dd
JB
942#endif
943 return SCM_UNSPECIFIED;
944}
945
70d63753
GB
946
947SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
948 (SCM destination, SCM message, SCM args),
eca65e90
MG
949 "Write @var{message} to @var{destination}, defaulting to\n"
950 "the current output port.\n"
951 "@var{message} can contain @code{~A} (was @code{%s}) and\n"
952 "@code{~S} (was @code{%S}) escapes. When printed,\n"
953 "the escapes are replaced with corresponding members of\n"
954 "@var{ARGS}:\n"
955 "@code{~A} formats using @code{display} and @code{~S} formats\n"
956 "using @code{write}.\n"
957 "If @var{destination} is @code{#t}, then use the current output\n"
958 "port, if @var{destination} is @code{#f}, then return a string\n"
959 "containing the formatted text. Does not add a trailing newline.")
70d63753
GB
960#define FUNC_NAME s_scm_simple_format
961{
dfd03fb9 962 SCM port, answer = SCM_UNSPECIFIED;
70d63753
GB
963 int fReturnString = 0;
964 int writingp;
cc95e00a
MV
965 const char *start;
966 const char *end;
967 const char *p;
70d63753 968
bc36d050 969 if (scm_is_eq (destination, SCM_BOOL_T))
daba1a71 970 {
9de87eea 971 destination = port = scm_current_output_port ();
daba1a71 972 }
7888309b 973 else if (scm_is_false (destination))
daba1a71
MD
974 {
975 fReturnString = 1;
dfd03fb9
MD
976 port = scm_mkstrport (SCM_INUM0,
977 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
978 SCM_OPN | SCM_WRTNG,
979 FUNC_NAME);
980 destination = port;
daba1a71
MD
981 }
982 else
983 {
984 SCM_VALIDATE_OPORT_VALUE (1, destination);
dfd03fb9 985 port = SCM_COERCE_OUTPORT (destination);
daba1a71
MD
986 }
987 SCM_VALIDATE_STRING (2, message);
af45e3b0 988 SCM_VALIDATE_REST_ARGUMENT (args);
70d63753 989
cc95e00a
MV
990 start = scm_i_string_chars (message);
991 end = start + scm_i_string_length (message);
b24b5e13 992 for (p = start; p != end; ++p)
70d63753
GB
993 if (*p == '~')
994 {
b24b5e13 995 if (++p == end)
6662998f
MV
996 break;
997
998 switch (*p)
999 {
1000 case 'A': case 'a':
1001 writingp = 0;
1002 break;
1003 case 'S': case 's':
1004 writingp = 1;
1005 break;
1006 case '~':
dfd03fb9 1007 scm_lfwrite (start, p - start, port);
6662998f
MV
1008 start = p + 1;
1009 continue;
1010 case '%':
dfd03fb9
MD
1011 scm_lfwrite (start, p - start - 1, port);
1012 scm_newline (port);
6662998f
MV
1013 start = p + 1;
1014 continue;
1015 default:
1afff620
KN
1016 SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
1017 scm_list_1 (SCM_MAKE_CHAR (*p)));
6662998f
MV
1018
1019 }
70d63753 1020
6662998f 1021
d2e53ed6 1022 if (!scm_is_pair (args))
1afff620
KN
1023 SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
1024 scm_list_1 (SCM_MAKE_CHAR (*p)));
6662998f 1025
dfd03fb9
MD
1026 scm_lfwrite (start, p - start - 1, port);
1027 /* we pass destination here */
70d63753
GB
1028 scm_prin1 (SCM_CAR (args), destination, writingp);
1029 args = SCM_CDR (args);
1030 start = p + 1;
1031 }
6662998f 1032
dfd03fb9 1033 scm_lfwrite (start, p - start, port);
bc36d050 1034 if (!scm_is_eq (args, SCM_EOL))
1afff620
KN
1035 SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
1036 scm_list_1 (scm_length (args)));
70d63753
GB
1037
1038 if (fReturnString)
1039 answer = scm_strport_to_string (destination);
1040
daba1a71 1041 return scm_return_first (answer, message);
70d63753
GB
1042}
1043#undef FUNC_NAME
1044
1045
3b3b36dd 1046SCM_DEFINE (scm_newline, "newline", 0, 1, 0,
b450f070 1047 (SCM port),
8f85c0c6
NJ
1048 "Send a newline to @var{port}.\n"
1049 "If @var{port} is omitted, send to the current output port.")
1bbd0b84 1050#define FUNC_NAME s_scm_newline
0f2d19dd
JB
1051{
1052 if (SCM_UNBNDP (port))
9de87eea 1053 port = scm_current_output_port ();
3eb7e6ee 1054
34d19ef6 1055 SCM_VALIDATE_OPORT_VALUE (1, port);
bb35f315 1056
0ef4ae82 1057 scm_putc ('\n', SCM_COERCE_OUTPORT (port));
0f2d19dd
JB
1058 return SCM_UNSPECIFIED;
1059}
1bbd0b84 1060#undef FUNC_NAME
0f2d19dd 1061
3b3b36dd 1062SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
b450f070 1063 (SCM chr, SCM port),
eca65e90 1064 "Send character @var{chr} to @var{port}.")
1bbd0b84 1065#define FUNC_NAME s_scm_write_char
0f2d19dd
JB
1066{
1067 if (SCM_UNBNDP (port))
9de87eea 1068 port = scm_current_output_port ();
3eb7e6ee 1069
34d19ef6
HWN
1070 SCM_VALIDATE_CHAR (1, chr);
1071 SCM_VALIDATE_OPORT_VALUE (2, port);
bb35f315 1072
7866a09b 1073 scm_putc ((int) SCM_CHAR (chr), SCM_COERCE_OUTPORT (port));
23d72566 1074#if 0
0f2d19dd
JB
1075#ifdef HAVE_PIPE
1076# ifdef EPIPE
1077 if (EPIPE == errno)
1078 scm_close_port (port);
1079# endif
23d72566 1080#endif
0f2d19dd
JB
1081#endif
1082 return SCM_UNSPECIFIED;
1083}
1bbd0b84 1084#undef FUNC_NAME
0f2d19dd 1085
0f2d19dd
JB
1086\f
1087
bb35f315 1088/* Call back to Scheme code to do the printing of special objects
c19bc088
MD
1089 * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
1090 * containing PORT and PSTATE. This object can be used as the port for
1091 * display/write etc to continue the current print chain. The REVEALED
1092 * field of PSTATE is set to true to indicate that the print state has
1093 * escaped to Scheme and thus has to be freed by the GC.
1094 */
1095
92c2555f 1096scm_t_bits scm_tc16_port_with_ps;
c19bc088
MD
1097
1098/* Print exactly as the port itself would */
1099
1100static int
e841c3e0 1101port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate)
c19bc088
MD
1102{
1103 obj = SCM_PORT_WITH_PS_PORT (obj);
1104 return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate);
1105}
c4f37e80
MV
1106
1107SCM
1bbd0b84 1108scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate)
c4f37e80 1109{
bb35f315 1110 pstate->revealed = 1;
dfd03fb9
MD
1111 return scm_call_2 (proc, exp,
1112 scm_i_port_with_print_state (port, pstate->handle));
c19bc088
MD
1113}
1114
dfd03fb9 1115SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 1, 1, 0,
1bbd0b84 1116 (SCM port, SCM pstate),
71331188 1117 "Create a new port which behaves like @var{port}, but with an\n"
dfd03fb9
MD
1118 "included print state @var{pstate}. @var{pstate} is optional.\n"
1119 "If @var{pstate} isn't supplied and @var{port} already has\n"
1120 "a print state, the old print state is reused.")
1bbd0b84 1121#define FUNC_NAME s_scm_port_with_print_state
c19bc088 1122{
34d19ef6 1123 SCM_VALIDATE_OPORT_VALUE (1, port);
dfd03fb9
MD
1124 if (!SCM_UNBNDP (pstate))
1125 SCM_VALIDATE_PRINTSTATE (2, pstate);
1126 return scm_i_port_with_print_state (port, pstate);
c19bc088 1127}
1bbd0b84 1128#undef FUNC_NAME
c19bc088 1129
a1ec6916 1130SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0,
1bbd0b84 1131 (SCM port),
71331188
MG
1132 "Return the print state of the port @var{port}. If @var{port}\n"
1133 "has no associated print state, @code{#f} is returned.")
1bbd0b84 1134#define FUNC_NAME s_scm_get_print_state
c19bc088 1135{
368cf54d
GB
1136 if (SCM_PORT_WITH_PS_P (port))
1137 return SCM_PORT_WITH_PS_PS (port);
f5f2dcff 1138 if (SCM_OUTPUT_PORT_P (port))
368cf54d 1139 return SCM_BOOL_F;
276dd677 1140 SCM_WRONG_TYPE_ARG (1, port);
c4f37e80 1141}
1bbd0b84 1142#undef FUNC_NAME
bb35f315 1143
c4f37e80 1144\f
1cc91f1b 1145
0f2d19dd
JB
1146void
1147scm_init_print ()
0f2d19dd 1148{
c19bc088 1149 SCM vtable, layout, type;
d5cf5324 1150
62560650 1151 scm_init_opts (scm_print_options, scm_print_opts);
d5cf5324 1152
81ae25da
MV
1153 scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"),
1154 scm_from_locale_string ("{"),
1155 scm_from_locale_symbol ("highlight-suffix"),
1156 scm_from_locale_string ("}")));
1157
d5cf5324
DH
1158 scm_gc_register_root (&print_state_pool);
1159 scm_gc_register_root (&scm_print_state_vtable);
3ce4544c 1160 vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL);
cc95e00a
MV
1161 layout =
1162 scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT));
1afff620 1163 type = scm_make_struct (vtable, SCM_INUM0, scm_list_1 (layout));
cc95e00a 1164 scm_set_struct_vtable_name_x (type, scm_from_locale_symbol ("print-state"));
bb35f315 1165 scm_print_state_vtable = type;
c4f37e80 1166
c19bc088
MD
1167 /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
1168 scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
1169 scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr);
e841c3e0 1170 scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print);
81ae25da 1171
a0599745 1172#include "libguile/print.x"
475fa9a5
MV
1173
1174 scm_print_opts[SCM_PRINT_KEYWORD_STYLE_I].val = SCM_UNPACK (sym_reader);
0f2d19dd 1175}
89e00824
ML
1176
1177/*
1178 Local Variables:
1179 c-file-style: "gnu"
1180 End:
1181*/