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