Commit | Line | Data |
---|---|---|
78a0461a | 1 | /* Copyright (C) 1995-1999 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program 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 | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
47 | #include <stdio.h> | |
48 | #include "_scm.h" | |
20e6290e JB |
49 | #include "chars.h" |
50 | #include "genio.h" | |
20e6290e JB |
51 | #include "smob.h" |
52 | #include "eval.h" | |
ddeae7ca | 53 | #include "macros.h" |
20e6290e JB |
54 | #include "procprop.h" |
55 | #include "read.h" | |
56 | #include "weaks.h" | |
57 | #include "unif.h" | |
dbef8851 | 58 | #include "alist.h" |
c62fbfe1 | 59 | #include "struct.h" |
8b840115 | 60 | #include "objects.h" |
70d63753 | 61 | #include "strports.h" |
0f2d19dd | 62 | |
1bbd0b84 | 63 | #include "scm_validate.h" |
20e6290e | 64 | #include "print.h" |
0f2d19dd JB |
65 | \f |
66 | ||
67 | /* {Names of immediate symbols} | |
68 | * | |
69 | * This table must agree with the declarations in scm.h: {Immediate Symbols}. | |
70 | */ | |
71 | ||
72 | char *scm_isymnames[] = | |
73 | { | |
74 | /* This table must agree with the declarations */ | |
75 | "#@and", | |
76 | "#@begin", | |
77 | "#@case", | |
78 | "#@cond", | |
79 | "#@do", | |
80 | "#@if", | |
81 | "#@lambda", | |
82 | "#@let", | |
83 | "#@let*", | |
84 | "#@letrec", | |
85 | "#@or", | |
86 | "#@quote", | |
87 | "#@set!", | |
88 | "#@define", | |
89 | #if 0 | |
90 | "#@literal-variable-ref", | |
91 | "#@literal-variable-set!", | |
92 | #endif | |
93 | "#@apply", | |
94 | "#@call-with-current-continuation", | |
95 | ||
96 | /* user visible ISYMS */ | |
97 | /* other keywords */ | |
98 | /* Flags */ | |
99 | ||
100 | "#f", | |
101 | "#t", | |
102 | "#<undefined>", | |
103 | "#<eof>", | |
104 | "()", | |
4df91bd6 MD |
105 | "#<unspecified>", |
106 | "#@dispatch", | |
43c667e9 MD |
107 | "#@slot-ref", |
108 | "#@slot-set!", | |
159500fb | 109 | |
159500fb MD |
110 | /* Multi-language support */ |
111 | ||
112 | "#@nil-cond", | |
113 | "#@nil-ify", | |
114 | "#@t-ify", | |
115 | "#@0-cond", | |
116 | "#@0-ify", | |
117 | "#@1-ify", | |
ccd0e478 MD |
118 | "#@bind", |
119 | ||
5623a9b4 MD |
120 | "#@delay", |
121 | ||
122 | "#<unbound>" | |
0f2d19dd JB |
123 | }; |
124 | ||
e6e4c9af | 125 | scm_option scm_print_opts[] = { |
b7ff98dd | 126 | { SCM_OPTION_SCM, "closure-hook", SCM_BOOL_F, |
84f6a34a MD |
127 | "Hook for printing closures." }, |
128 | { SCM_OPTION_BOOLEAN, "source", 0, | |
129 | "Print closures with source." } | |
e6e4c9af MD |
130 | }; |
131 | ||
a1ec6916 | 132 | SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0, |
1bbd0b84 GB |
133 | (SCM setting), |
134 | "") | |
135 | #define FUNC_NAME s_scm_print_options | |
e6e4c9af | 136 | { |
a51ea417 | 137 | SCM ans = scm_options (setting, |
b7ff98dd MD |
138 | scm_print_opts, |
139 | SCM_N_PRINT_OPTIONS, | |
1bbd0b84 | 140 | FUNC_NAME); |
e6e4c9af MD |
141 | return ans; |
142 | } | |
1bbd0b84 | 143 | #undef FUNC_NAME |
e6e4c9af | 144 | |
0f2d19dd JB |
145 | \f |
146 | /* {Printing of Scheme Objects} | |
147 | */ | |
148 | ||
a51ea417 | 149 | /* Detection of circular references. |
c62fbfe1 MD |
150 | * |
151 | * Due to other constraints in the implementation, this code has bad | |
152 | * time complexity (O (depth * N)), The printer code will be | |
153 | * completely rewritten before next release of Guile. The new code | |
154 | * will be O(N). | |
a51ea417 | 155 | */ |
c62fbfe1 | 156 | #define PUSH_REF(pstate, obj) \ |
1bbd0b84 | 157 | do { \ |
c62fbfe1 MD |
158 | pstate->ref_stack[pstate->top++] = (obj); \ |
159 | if (pstate->top == pstate->ceiling) \ | |
160 | grow_ref_stack (pstate); \ | |
1bbd0b84 | 161 | } while(0) |
a51ea417 | 162 | |
c62fbfe1 | 163 | #define ENTER_NESTED_DATA(pstate, obj, label) \ |
1bbd0b84 | 164 | do { \ |
5ca6dc39 | 165 | register unsigned long i; \ |
c62fbfe1 MD |
166 | for (i = 0; i < pstate->top; ++i) \ |
167 | if (pstate->ref_stack[i] == (obj)) \ | |
168 | goto label; \ | |
169 | if (pstate->fancyp) \ | |
170 | { \ | |
171 | if (pstate->top - pstate->list_offset >= pstate->level) \ | |
172 | { \ | |
b7f3516f | 173 | scm_putc ('#', port); \ |
c62fbfe1 MD |
174 | return; \ |
175 | } \ | |
176 | } \ | |
177 | PUSH_REF(pstate, obj); \ | |
1bbd0b84 | 178 | } while(0) |
a51ea417 | 179 | |
c62fbfe1 MD |
180 | #define EXIT_NESTED_DATA(pstate) { --pstate->top; } |
181 | ||
bb35f315 | 182 | SCM scm_print_state_vtable; |
c4f37e80 | 183 | |
bb35f315 | 184 | static SCM print_state_pool; |
c4f37e80 | 185 | |
f843a84c | 186 | #ifdef GUILE_DEBUG /* Used for debugging purposes */ |
1cc91f1b | 187 | |
3b3b36dd | 188 | SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0, |
1bbd0b84 | 189 | (), |
717050c8 | 190 | "") |
1bbd0b84 | 191 | #define FUNC_NAME s_scm_current_pstate |
c62fbfe1 MD |
192 | { |
193 | return SCM_CADR (print_state_pool); | |
194 | } | |
1bbd0b84 GB |
195 | #undef FUNC_NAME |
196 | ||
c62fbfe1 MD |
197 | #endif |
198 | ||
199 | #define PSTATE_SIZE 50L | |
200 | ||
698c0295 | 201 | static SCM |
1bbd0b84 | 202 | make_print_state (void) |
698c0295 MD |
203 | { |
204 | SCM print_state = scm_make_struct (SCM_CAR (print_state_pool), /* pstate type */ | |
bf685b6d | 205 | SCM_INUM0, |
698c0295 | 206 | SCM_EOL); |
bf685b6d MD |
207 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); |
208 | pstate->ref_vect = scm_make_vector (SCM_MAKINUM (PSTATE_SIZE), | |
bf685b6d MD |
209 | SCM_UNDEFINED); |
210 | pstate->ref_stack = SCM_VELTS (pstate->ref_vect); | |
211 | pstate->ceiling = SCM_LENGTH (pstate->ref_vect); | |
698c0295 MD |
212 | return print_state; |
213 | } | |
1cc91f1b | 214 | |
c62fbfe1 MD |
215 | SCM |
216 | scm_make_print_state () | |
c62fbfe1 | 217 | { |
698c0295 MD |
218 | SCM answer = 0; |
219 | ||
220 | /* First try to allocate a print state from the pool */ | |
221 | SCM_DEFER_INTS; | |
222 | if (SCM_NNULLP (SCM_CDR (print_state_pool))) | |
223 | { | |
224 | answer = SCM_CADR (print_state_pool); | |
225 | SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool)); | |
226 | } | |
227 | SCM_ALLOW_INTS; | |
228 | ||
229 | return answer ? answer : make_print_state (); | |
c62fbfe1 | 230 | } |
a51ea417 | 231 | |
698c0295 | 232 | void |
6e8d25a6 | 233 | scm_free_print_state (SCM print_state) |
698c0295 MD |
234 | { |
235 | SCM handle; | |
236 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); | |
237 | /* Cleanup before returning print state to pool. | |
238 | * It is better to do it here. Doing it in scm_prin1 | |
239 | * would cost more since that function is called much more | |
240 | * often. | |
241 | */ | |
242 | pstate->fancyp = 0; | |
bb35f315 | 243 | pstate->revealed = 0; |
698c0295 MD |
244 | SCM_NEWCELL (handle); |
245 | SCM_DEFER_INTS; | |
246 | SCM_SETCAR (handle, print_state); | |
247 | SCM_SETCDR (handle, SCM_CDR (print_state_pool)); | |
248 | SCM_SETCDR (print_state_pool, handle); | |
249 | SCM_ALLOW_INTS; | |
250 | } | |
1cc91f1b | 251 | |
a51ea417 | 252 | static void |
1bbd0b84 | 253 | grow_ref_stack (scm_print_state *pstate) |
a51ea417 | 254 | { |
bf685b6d MD |
255 | int new_size = 2 * pstate->ceiling; |
256 | scm_vector_set_length_x (pstate->ref_vect, SCM_MAKINUM (new_size)); | |
257 | pstate->ref_stack = SCM_VELTS (pstate->ref_vect); | |
258 | pstate->ceiling = new_size; | |
a51ea417 MD |
259 | } |
260 | ||
1cc91f1b | 261 | |
a51ea417 | 262 | static void |
1bbd0b84 | 263 | print_circref (SCM port,scm_print_state *pstate,SCM ref) |
a51ea417 | 264 | { |
c62fbfe1 MD |
265 | register int i; |
266 | int self = pstate->top - 1; | |
267 | i = pstate->top - 1; | |
268 | if (SCM_CONSP (pstate->ref_stack[i])) | |
269 | { | |
270 | while (i > 0) | |
271 | { | |
272 | if (SCM_NCONSP (pstate->ref_stack[i - 1]) | |
273 | || SCM_CDR (pstate->ref_stack[i - 1]) != pstate->ref_stack[i]) | |
274 | break; | |
275 | --i; | |
276 | } | |
277 | self = i; | |
278 | } | |
279 | for (i = pstate->top - 1; 1; --i) | |
280 | if (pstate->ref_stack[i] == ref) | |
281 | break; | |
b7f3516f | 282 | scm_putc ('#', port); |
c62fbfe1 | 283 | scm_intprint (i - self, 10, port); |
b7f3516f | 284 | scm_putc ('#', port); |
a51ea417 MD |
285 | } |
286 | ||
c62fbfe1 | 287 | /* Print generally. Handles both write and display according to PSTATE. |
0f2d19dd | 288 | */ |
8b840115 MD |
289 | SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); |
290 | SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); | |
1cc91f1b | 291 | |
0f2d19dd | 292 | void |
1bbd0b84 | 293 | scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate) |
0f2d19dd | 294 | { |
0f2d19dd JB |
295 | taloop: |
296 | switch (7 & (int) exp) | |
297 | { | |
298 | case 2: | |
299 | case 6: | |
300 | scm_intprint (SCM_INUM (exp), 10, port); | |
301 | break; | |
302 | case 4: | |
303 | if (SCM_ICHRP (exp)) | |
304 | { | |
5ca6dc39 JB |
305 | register long i; |
306 | ||
0f2d19dd | 307 | i = SCM_ICHR (exp); |
b7f3516f TT |
308 | if (SCM_WRITINGP (pstate)) |
309 | { | |
310 | scm_puts ("#\\", port); | |
311 | if ((i >= 0) && (i <= ' ') && scm_charnames[i]) | |
312 | scm_puts (scm_charnames[i], port); | |
313 | else if (i < 0 || i > '\177') | |
314 | scm_intprint (i, 8, port); | |
315 | else | |
316 | scm_putc (i, port); | |
317 | } | |
318 | else | |
319 | scm_putc (i, port); | |
0f2d19dd | 320 | } |
a51ea417 | 321 | else if (SCM_IFLAGP (exp) |
5ca6dc39 | 322 | && ((size_t) SCM_ISYMNUM (exp) < (sizeof scm_isymnames / sizeof (char *)))) |
b7f3516f | 323 | scm_puts (SCM_ISYMCHARS (exp), port); |
0f2d19dd JB |
324 | else if (SCM_ILOCP (exp)) |
325 | { | |
b7f3516f | 326 | scm_puts ("#@", port); |
0f2d19dd | 327 | scm_intprint ((long) SCM_IFRAME (exp), 10, port); |
b7f3516f | 328 | scm_putc (SCM_ICDRP (exp) ? '-' : '+', port); |
0f2d19dd JB |
329 | scm_intprint ((long) SCM_IDIST (exp), 10, port); |
330 | } | |
331 | else | |
332 | goto idef; | |
333 | break; | |
334 | case 1: | |
335 | /* gloc */ | |
b7f3516f | 336 | scm_puts ("#@", port); |
0f2d19dd JB |
337 | exp = SCM_CAR (exp - 1); |
338 | goto taloop; | |
339 | default: | |
340 | idef: | |
341 | scm_ipruk ("immediate", exp, port); | |
342 | break; | |
343 | case 0: | |
344 | switch (SCM_TYP7 (exp)) | |
345 | { | |
346 | case scm_tcs_cons_gloc: | |
347 | ||
348 | if (SCM_CDR (SCM_CAR (exp) - 1L) == 0) | |
349 | { | |
c4f37e80 | 350 | ENTER_NESTED_DATA (pstate, exp, circref); |
8b840115 MD |
351 | if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS) |
352 | { | |
353 | SCM pwps, print = pstate->writingp ? g_write : g_display; | |
354 | if (!print) | |
355 | goto print_struct; | |
356 | SCM_NEWSMOB (pwps, | |
357 | scm_tc16_port_with_ps, | |
358 | scm_cons (port, pstate->handle)); | |
359 | scm_call_generic_2 (print, exp, pwps); | |
360 | } | |
361 | else | |
362 | { | |
363 | print_struct: | |
364 | scm_print_struct (exp, port, pstate); | |
365 | } | |
c4f37e80 | 366 | EXIT_NESTED_DATA (pstate); |
0f2d19dd JB |
367 | break; |
368 | } | |
369 | ||
370 | case scm_tcs_cons_imcar: | |
371 | case scm_tcs_cons_nimcar: | |
c62fbfe1 MD |
372 | ENTER_NESTED_DATA (pstate, exp, circref); |
373 | scm_iprlist ("(", exp, ')', port, pstate); | |
374 | EXIT_NESTED_DATA (pstate); | |
a51ea417 MD |
375 | break; |
376 | circref: | |
c62fbfe1 | 377 | print_circref (port, pstate, exp); |
0f2d19dd | 378 | break; |
7332df66 | 379 | macros: |
80ea260c | 380 | if (!SCM_CLOSUREP (SCM_CDR (exp))) |
7332df66 | 381 | goto prinmacro; |
0f2d19dd | 382 | case scm_tcs_closures: |
7332df66 MD |
383 | /* The user supplied print closure procedure must handle |
384 | macro closures as well. */ | |
bb35f315 MV |
385 | if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE)) |
386 | || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE, | |
77c25af7 | 387 | exp, port, pstate))) |
bb35f315 | 388 | { |
ba031394 | 389 | SCM name, code, env; |
bb35f315 MV |
390 | if (SCM_TYP16 (exp) == scm_tc16_macro) |
391 | { | |
392 | /* Printing a macro. */ | |
393 | prinmacro: | |
394 | name = scm_macro_name (exp); | |
395 | if (!SCM_CLOSUREP (SCM_CDR (exp))) | |
396 | { | |
f44dd64b | 397 | code = env = 0; |
b7f3516f | 398 | scm_puts ("#<primitive-", port); |
bb35f315 MV |
399 | } |
400 | else | |
401 | { | |
402 | code = SCM_CODE (SCM_CDR (exp)); | |
ba031394 | 403 | env = SCM_ENV (SCM_CDR (exp)); |
b7f3516f | 404 | scm_puts ("#<", port); |
bb35f315 MV |
405 | } |
406 | if (SCM_CAR (exp) & (3L << 16)) | |
b7f3516f | 407 | scm_puts ("macro", port); |
bb35f315 | 408 | else |
b7f3516f | 409 | scm_puts ("syntax", port); |
bb35f315 | 410 | if (SCM_CAR (exp) & (2L << 16)) |
b7f3516f | 411 | scm_putc ('!', port); |
bb35f315 MV |
412 | } |
413 | else | |
414 | { | |
415 | /* Printing a closure. */ | |
416 | name = scm_procedure_name (exp); | |
417 | code = SCM_CODE (exp); | |
ba031394 | 418 | env = SCM_ENV (exp); |
b7f3516f | 419 | scm_puts ("#<procedure", port); |
bb35f315 | 420 | } |
0c95b57d | 421 | if (SCM_ROSTRINGP (name)) |
bb35f315 | 422 | { |
b7f3516f TT |
423 | scm_putc (' ', port); |
424 | scm_puts (SCM_ROCHARS (name), port); | |
bb35f315 MV |
425 | } |
426 | if (code) | |
427 | { | |
ba031394 MD |
428 | if (SCM_PRINT_SOURCE_P) |
429 | { | |
430 | code = scm_unmemocopy (code, | |
431 | SCM_EXTEND_ENV (SCM_CAR (code), | |
432 | SCM_EOL, | |
433 | env)); | |
434 | ENTER_NESTED_DATA (pstate, exp, circref); | |
435 | scm_iprlist (" ", code, '>', port, pstate); | |
436 | EXIT_NESTED_DATA (pstate); | |
437 | } | |
438 | else | |
439 | { | |
440 | if (SCM_TYP16 (exp) != scm_tc16_macro) | |
441 | { | |
b7f3516f | 442 | scm_putc (' ', port); |
ba031394 MD |
443 | scm_iprin1 (SCM_CAR (code), port, pstate); |
444 | } | |
b7f3516f | 445 | scm_putc ('>', port); |
ba031394 | 446 | } |
bb35f315 MV |
447 | } |
448 | else | |
b7f3516f | 449 | scm_putc ('>', port); |
bb35f315 | 450 | } |
0f2d19dd | 451 | break; |
0f2d19dd JB |
452 | case scm_tc7_substring: |
453 | case scm_tc7_string: | |
c62fbfe1 | 454 | if (SCM_WRITINGP (pstate)) |
0f2d19dd | 455 | { |
5ca6dc39 JB |
456 | scm_sizet i; |
457 | ||
b7f3516f | 458 | scm_putc ('"', port); |
0f2d19dd JB |
459 | for (i = 0; i < SCM_ROLENGTH (exp); ++i) |
460 | switch (SCM_ROCHARS (exp)[i]) | |
461 | { | |
dbef8851 | 462 | case '"': |
0f2d19dd | 463 | case '\\': |
b7f3516f | 464 | scm_putc ('\\', port); |
0f2d19dd | 465 | default: |
b7f3516f | 466 | scm_putc (SCM_ROCHARS (exp)[i], port); |
0f2d19dd | 467 | } |
b7f3516f | 468 | scm_putc ('"', port); |
0f2d19dd JB |
469 | break; |
470 | } | |
471 | else | |
b7f3516f TT |
472 | scm_lfwrite (SCM_ROCHARS (exp), (scm_sizet) SCM_ROLENGTH (exp), |
473 | port); | |
0f2d19dd JB |
474 | break; |
475 | case scm_tcs_symbols: | |
0f2d19dd JB |
476 | { |
477 | int pos; | |
478 | int end; | |
479 | int len; | |
480 | char * str; | |
481 | int weird; | |
482 | int maybe_weird; | |
4dc2435a | 483 | int mw_pos = 0; |
0f2d19dd JB |
484 | |
485 | len = SCM_LENGTH (exp); | |
486 | str = SCM_CHARS (exp); | |
487 | scm_remember (&exp); | |
488 | pos = 0; | |
489 | weird = 0; | |
490 | maybe_weird = 0; | |
491 | ||
492 | if (len == 0) | |
b7f3516f | 493 | scm_lfwrite ("#{}#", 4, port); |
0f2d19dd JB |
494 | |
495 | for (end = pos; end < len; ++end) | |
496 | switch (str[end]) | |
497 | { | |
498 | #ifdef BRACKETS_AS_PARENS | |
499 | case '[': | |
500 | case ']': | |
501 | #endif | |
502 | case '(': | |
503 | case ')': | |
dbef8851 | 504 | case '"': |
0f2d19dd JB |
505 | case ';': |
506 | case SCM_WHITE_SPACES: | |
507 | case SCM_LINE_INCREMENTORS: | |
508 | weird_handler: | |
509 | if (maybe_weird) | |
510 | { | |
511 | end = mw_pos; | |
512 | maybe_weird = 0; | |
513 | } | |
514 | if (!weird) | |
515 | { | |
b7f3516f | 516 | scm_lfwrite ("#{", 2, port); |
0f2d19dd JB |
517 | weird = 1; |
518 | } | |
519 | if (pos < end) | |
520 | { | |
b7f3516f | 521 | scm_lfwrite (str + pos, end - pos, port); |
0f2d19dd JB |
522 | } |
523 | { | |
524 | char buf[2]; | |
525 | buf[0] = '\\'; | |
526 | buf[1] = str[end]; | |
b7f3516f | 527 | scm_lfwrite (buf, 2, port); |
0f2d19dd JB |
528 | } |
529 | pos = end + 1; | |
530 | break; | |
531 | case '\\': | |
532 | if (weird) | |
533 | goto weird_handler; | |
534 | if (!maybe_weird) | |
535 | { | |
536 | maybe_weird = 1; | |
537 | mw_pos = pos; | |
538 | } | |
539 | break; | |
540 | case '}': | |
541 | case '#': | |
542 | if (weird) | |
543 | goto weird_handler; | |
544 | break; | |
545 | default: | |
546 | break; | |
547 | } | |
548 | if (pos < end) | |
b7f3516f | 549 | scm_lfwrite (str + pos, end - pos, port); |
0f2d19dd | 550 | if (weird) |
b7f3516f | 551 | scm_lfwrite ("}#", 2, port); |
0f2d19dd JB |
552 | break; |
553 | } | |
554 | case scm_tc7_wvect: | |
c62fbfe1 | 555 | ENTER_NESTED_DATA (pstate, exp, circref); |
0f2d19dd | 556 | if (SCM_IS_WHVEC (exp)) |
b7f3516f | 557 | scm_puts ("#wh(", port); |
0f2d19dd | 558 | else |
b7f3516f | 559 | scm_puts ("#w(", port); |
0f2d19dd JB |
560 | goto common_vector_printer; |
561 | ||
562 | case scm_tc7_vector: | |
c62fbfe1 | 563 | ENTER_NESTED_DATA (pstate, exp, circref); |
b7f3516f | 564 | scm_puts ("#(", port); |
0f2d19dd | 565 | common_vector_printer: |
9fbaf27c | 566 | { |
5ca6dc39 | 567 | register long i; |
9fbaf27c MD |
568 | int last = SCM_LENGTH (exp) - 1; |
569 | int cutp = 0; | |
570 | if (pstate->fancyp && SCM_LENGTH (exp) > pstate->length) | |
571 | { | |
572 | last = pstate->length - 1; | |
573 | cutp = 1; | |
574 | } | |
575 | for (i = 0; i < last; ++i) | |
576 | { | |
577 | /* CHECK_INTS; */ | |
578 | scm_iprin1 (SCM_VELTS (exp)[i], port, pstate); | |
b7f3516f | 579 | scm_putc (' ', port); |
9fbaf27c MD |
580 | } |
581 | if (i == last) | |
582 | { | |
583 | /* CHECK_INTS; */ | |
584 | scm_iprin1 (SCM_VELTS (exp)[i], port, pstate); | |
585 | } | |
586 | if (cutp) | |
b7f3516f TT |
587 | scm_puts (" ...", port); |
588 | scm_putc (')', port); | |
9fbaf27c | 589 | } |
c62fbfe1 | 590 | EXIT_NESTED_DATA (pstate); |
0f2d19dd | 591 | break; |
afe5177e | 592 | #ifdef HAVE_ARRAYS |
0f2d19dd JB |
593 | case scm_tc7_bvect: |
594 | case scm_tc7_byvect: | |
595 | case scm_tc7_svect: | |
596 | case scm_tc7_ivect: | |
597 | case scm_tc7_uvect: | |
598 | case scm_tc7_fvect: | |
599 | case scm_tc7_dvect: | |
600 | case scm_tc7_cvect: | |
5c11cc9d | 601 | #ifdef HAVE_LONG_LONGS |
0f2d19dd JB |
602 | case scm_tc7_llvect: |
603 | #endif | |
c62fbfe1 | 604 | scm_raprin1 (exp, port, pstate); |
0f2d19dd | 605 | break; |
afe5177e | 606 | #endif |
0f2d19dd | 607 | case scm_tcs_subrs: |
9de33deb MD |
608 | scm_puts (SCM_SUBR_GENERIC (exp) && *SCM_SUBR_GENERIC (exp) |
609 | ? "#<primitive-generic " | |
610 | : "#<primitive-procedure ", | |
611 | port); | |
b7f3516f TT |
612 | scm_puts (SCM_CHARS (SCM_SNAME (exp)), port); |
613 | scm_putc ('>', port); | |
0f2d19dd JB |
614 | break; |
615 | #ifdef CCLO | |
616 | case scm_tc7_cclo: | |
56977059 MD |
617 | { |
618 | SCM proc = SCM_CCLO_SUBR (exp); | |
619 | if (proc == scm_f_gsubr_apply) | |
620 | { | |
621 | /* Print gsubrs as primitives */ | |
c180dfd3 | 622 | SCM name = scm_procedure_name (exp); |
56977059 MD |
623 | scm_puts ("#<primitive-procedure", port); |
624 | if (SCM_NFALSEP (name)) | |
625 | { | |
626 | scm_putc (' ', port); | |
627 | scm_puts (SCM_CHARS (name), port); | |
628 | } | |
629 | } | |
630 | else | |
631 | { | |
632 | scm_puts ("#<compiled-closure ", port); | |
633 | scm_iprin1 (proc, port, pstate); | |
634 | } | |
635 | scm_putc ('>', port); | |
636 | } | |
0f2d19dd JB |
637 | break; |
638 | #endif | |
c180dfd3 MD |
639 | case scm_tc7_pws: |
640 | scm_puts ("#<procedure-with-setter", port); | |
641 | { | |
642 | SCM name = scm_procedure_name (exp); | |
643 | if (SCM_NFALSEP (name)) | |
644 | { | |
645 | scm_putc (' ', port); | |
646 | scm_puts (SCM_ROCHARS (name), port); | |
647 | } | |
648 | } | |
649 | scm_putc ('>', port); | |
650 | break; | |
0f2d19dd | 651 | case scm_tc7_contin: |
b7f3516f | 652 | scm_puts ("#<continuation ", port); |
0f2d19dd | 653 | scm_intprint (SCM_LENGTH (exp), 10, port); |
b7f3516f | 654 | scm_puts (" @ ", port); |
0f2d19dd | 655 | scm_intprint ((long) SCM_CHARS (exp), 16, port); |
b7f3516f | 656 | scm_putc ('>', port); |
0f2d19dd JB |
657 | break; |
658 | case scm_tc7_port: | |
5ca6dc39 JB |
659 | { |
660 | register long i = SCM_PTOBNUM (exp); | |
661 | if (i < scm_numptob | |
662 | && scm_ptobs[i].print | |
663 | && (scm_ptobs[i].print) (exp, port, pstate)) | |
a51ea417 | 664 | break; |
5ca6dc39 JB |
665 | goto punk; |
666 | } | |
667 | case scm_tc7_smob: | |
668 | { | |
669 | register long i; | |
670 | ENTER_NESTED_DATA (pstate, exp, circref); | |
671 | i = SCM_SMOBNUM (exp); | |
672 | if (i < scm_numsmob && scm_smobs[i].print | |
673 | && (scm_smobs[i].print) (exp, port, pstate)) | |
674 | { | |
675 | EXIT_NESTED_DATA (pstate); | |
676 | break; | |
677 | } | |
678 | EXIT_NESTED_DATA (pstate); | |
679 | /* Macros have their print field set to NULL. They are | |
680 | handled at the same place as closures in order to achieve | |
681 | non-redundancy. Placing the condition here won't slow | |
682 | down printing of other smobs. */ | |
683 | if (SCM_TYP16 (exp) == scm_tc16_macro) | |
684 | goto macros; | |
685 | } | |
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 | 702 | void |
1bbd0b84 | 703 | scm_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 MD |
707 | scm_print_state *pstate; |
708 | ||
bb35f315 MV |
709 | /* If PORT is a print-state/port pair, use that. Else create a new |
710 | print-state. */ | |
c4f37e80 | 711 | |
0c95b57d | 712 | if (SCM_PORT_WITH_PS_P (port)) |
bb35f315 | 713 | { |
52235e71 MD |
714 | pstate_scm = SCM_PORT_WITH_PS_PS (port); |
715 | port = SCM_PORT_WITH_PS_PORT (port); | |
bb35f315 MV |
716 | } |
717 | else | |
c62fbfe1 | 718 | { |
c4f37e80 MV |
719 | /* First try to allocate a print state from the pool */ |
720 | SCM_DEFER_INTS; | |
721 | if (SCM_NNULLP (SCM_CDR (print_state_pool))) | |
722 | { | |
723 | handle = SCM_CDR (print_state_pool); | |
724 | SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool)); | |
725 | } | |
726 | SCM_ALLOW_INTS; | |
727 | if (handle == SCM_BOOL_F) | |
728 | handle = scm_cons (make_print_state (), SCM_EOL); | |
729 | pstate_scm = SCM_CAR (handle); | |
c62fbfe1 | 730 | } |
c62fbfe1 | 731 | |
c4f37e80 | 732 | pstate = SCM_PRINT_STATE (pstate_scm); |
c62fbfe1 MD |
733 | pstate->writingp = writingp; |
734 | scm_iprin1 (exp, port, pstate); | |
735 | ||
bb35f315 MV |
736 | /* Return print state to pool if it has been created above and |
737 | hasn't escaped to Scheme. */ | |
738 | ||
739 | if (handle != SCM_BOOL_F && !pstate->revealed) | |
c4f37e80 MV |
740 | { |
741 | SCM_DEFER_INTS; | |
742 | SCM_SETCDR (handle, SCM_CDR (print_state_pool)); | |
743 | SCM_SETCDR (print_state_pool, handle); | |
744 | SCM_ALLOW_INTS; | |
745 | } | |
a51ea417 MD |
746 | } |
747 | ||
0f2d19dd JB |
748 | |
749 | /* Print an integer. | |
750 | */ | |
1cc91f1b | 751 | |
0f2d19dd | 752 | void |
1bbd0b84 | 753 | scm_intprint (long n, int radix, SCM port) |
0f2d19dd JB |
754 | { |
755 | char num_buf[SCM_INTBUFLEN]; | |
b7f3516f | 756 | scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port); |
0f2d19dd JB |
757 | } |
758 | ||
759 | /* Print an object of unrecognized type. | |
760 | */ | |
1cc91f1b | 761 | |
0f2d19dd | 762 | void |
1bbd0b84 | 763 | scm_ipruk (char *hdr, SCM ptr, SCM port) |
0f2d19dd | 764 | { |
b7f3516f TT |
765 | scm_puts ("#<unknown-", port); |
766 | scm_puts (hdr, port); | |
0f2d19dd JB |
767 | if (SCM_CELLP (ptr)) |
768 | { | |
b7f3516f | 769 | scm_puts (" (0x", port); |
0f2d19dd | 770 | scm_intprint (SCM_CAR (ptr), 16, port); |
b7f3516f | 771 | scm_puts (" . 0x", port); |
0f2d19dd | 772 | scm_intprint (SCM_CDR (ptr), 16, port); |
b7f3516f | 773 | scm_puts (") @", port); |
0f2d19dd | 774 | } |
b7f3516f | 775 | scm_puts (" 0x", port); |
0f2d19dd | 776 | scm_intprint (ptr, 16, port); |
b7f3516f | 777 | scm_putc ('>', port); |
0f2d19dd JB |
778 | } |
779 | ||
780 | /* Print a list. | |
781 | */ | |
a51ea417 | 782 | |
1cc91f1b | 783 | |
0f2d19dd | 784 | void |
1bbd0b84 | 785 | scm_iprlist (char *hdr,SCM exp,int tlr,SCM port,scm_print_state *pstate) |
0f2d19dd | 786 | { |
c62fbfe1 MD |
787 | register SCM hare, tortoise; |
788 | int floor = pstate->top - 2; | |
b7f3516f | 789 | scm_puts (hdr, port); |
0f2d19dd | 790 | /* CHECK_INTS; */ |
c62fbfe1 MD |
791 | if (pstate->fancyp) |
792 | goto fancy_printing; | |
793 | ||
794 | /* Run a hare and tortoise so that total time complexity will be | |
795 | O(depth * N) instead of O(N^2). */ | |
796 | hare = SCM_CDR (exp); | |
797 | tortoise = exp; | |
0c95b57d | 798 | while (SCM_ECONSP (hare)) |
c62fbfe1 MD |
799 | { |
800 | if (hare == tortoise) | |
801 | goto fancy_printing; | |
802 | hare = SCM_CDR (hare); | |
2fab3faa | 803 | if (SCM_IMP (hare) || SCM_NECONSP (hare)) |
c62fbfe1 MD |
804 | break; |
805 | hare = SCM_CDR (hare); | |
806 | tortoise = SCM_CDR (tortoise); | |
807 | } | |
808 | ||
809 | /* No cdr cycles intrinsic to this list */ | |
810 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
0f2d19dd JB |
811 | exp = SCM_CDR (exp); |
812 | for (; SCM_NIMP (exp); exp = SCM_CDR (exp)) | |
813 | { | |
5ca6dc39 JB |
814 | register int i; |
815 | ||
0f2d19dd JB |
816 | if (SCM_NECONSP (exp)) |
817 | break; | |
c62fbfe1 MD |
818 | for (i = floor; i >= 0; --i) |
819 | if (pstate->ref_stack[i] == exp) | |
820 | goto circref; | |
821 | PUSH_REF (pstate, exp); | |
b7f3516f | 822 | scm_putc (' ', port); |
0f2d19dd | 823 | /* CHECK_INTS; */ |
c62fbfe1 | 824 | scm_iprin1 (SCM_CAR (exp), port, pstate); |
0f2d19dd JB |
825 | } |
826 | if (SCM_NNULLP (exp)) | |
827 | { | |
b7f3516f | 828 | scm_puts (" . ", port); |
c62fbfe1 | 829 | scm_iprin1 (exp, port, pstate); |
0f2d19dd | 830 | } |
c62fbfe1 | 831 | |
a51ea417 | 832 | end: |
b7f3516f | 833 | scm_putc (tlr, port); |
c62fbfe1 | 834 | pstate->top = floor + 2; |
a51ea417 | 835 | return; |
c62fbfe1 MD |
836 | |
837 | fancy_printing: | |
838 | { | |
839 | int n = pstate->length; | |
840 | ||
841 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
842 | exp = SCM_CDR (exp); --n; | |
843 | for (; SCM_NIMP (exp); exp = SCM_CDR (exp)) | |
844 | { | |
5ca6dc39 JB |
845 | register unsigned long i; |
846 | ||
c62fbfe1 MD |
847 | if (SCM_NECONSP (exp)) |
848 | break; | |
849 | for (i = 0; i < pstate->top; ++i) | |
850 | if (pstate->ref_stack[i] == exp) | |
851 | goto fancy_circref; | |
852 | if (pstate->fancyp) | |
853 | { | |
854 | if (n == 0) | |
855 | { | |
b7f3516f | 856 | scm_puts (" ...", port); |
c62fbfe1 MD |
857 | goto skip_tail; |
858 | } | |
859 | else | |
860 | --n; | |
861 | } | |
862 | PUSH_REF(pstate, exp); | |
863 | ++pstate->list_offset; | |
b7f3516f | 864 | scm_putc (' ', port); |
c62fbfe1 MD |
865 | /* CHECK_INTS; */ |
866 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
867 | } | |
868 | } | |
869 | if (SCM_NNULLP (exp)) | |
870 | { | |
b7f3516f | 871 | scm_puts (" . ", port); |
c62fbfe1 MD |
872 | scm_iprin1 (exp, port, pstate); |
873 | } | |
874 | skip_tail: | |
875 | pstate->list_offset -= pstate->top - floor - 2; | |
a51ea417 | 876 | goto end; |
a51ea417 | 877 | |
c62fbfe1 MD |
878 | fancy_circref: |
879 | pstate->list_offset -= pstate->top - floor - 2; | |
880 | ||
881 | circref: | |
b7f3516f | 882 | scm_puts (" . ", port); |
c62fbfe1 MD |
883 | print_circref (port, pstate, exp); |
884 | goto end; | |
0f2d19dd JB |
885 | } |
886 | ||
887 | \f | |
888 | ||
bb35f315 MV |
889 | int |
890 | scm_valid_oport_value_p (SCM val) | |
891 | { | |
368cf54d GB |
892 | return (SCM_OPOUTPORTP (val) |
893 | || (SCM_PORT_WITH_PS_P (val) | |
894 | && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val)))); | |
bb35f315 MV |
895 | } |
896 | ||
8b840115 | 897 | /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */ |
1cc91f1b | 898 | |
0f2d19dd | 899 | SCM |
1bbd0b84 | 900 | scm_write (SCM obj, SCM port) |
0f2d19dd JB |
901 | { |
902 | if (SCM_UNBNDP (port)) | |
903 | port = scm_cur_outp; | |
3eb7e6ee JB |
904 | |
905 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write); | |
bb35f315 | 906 | |
a51ea417 | 907 | scm_prin1 (obj, port, 1); |
0f2d19dd JB |
908 | #ifdef HAVE_PIPE |
909 | # ifdef EPIPE | |
910 | if (EPIPE == errno) | |
911 | scm_close_port (port); | |
912 | # endif | |
913 | #endif | |
914 | return SCM_UNSPECIFIED; | |
915 | } | |
916 | ||
917 | ||
8b840115 | 918 | /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */ |
1cc91f1b | 919 | |
0f2d19dd | 920 | SCM |
1bbd0b84 | 921 | scm_display (SCM obj, SCM port) |
0f2d19dd JB |
922 | { |
923 | if (SCM_UNBNDP (port)) | |
924 | port = scm_cur_outp; | |
3eb7e6ee JB |
925 | |
926 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display); | |
bb35f315 | 927 | |
a51ea417 | 928 | scm_prin1 (obj, port, 0); |
0f2d19dd JB |
929 | #ifdef HAVE_PIPE |
930 | # ifdef EPIPE | |
931 | if (EPIPE == errno) | |
932 | scm_close_port (port); | |
933 | # endif | |
934 | #endif | |
935 | return SCM_UNSPECIFIED; | |
936 | } | |
937 | ||
70d63753 GB |
938 | |
939 | SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1, | |
940 | (SCM destination, SCM message, SCM args), | |
b380b885 MD |
941 | "Write MESSAGE to DESTINATION, defaulting to `current-output-port'.\n" |
942 | "MESSAGE can contain ~A (was %s) and ~S (was %S) escapes. When printed,\n" | |
943 | "the escapes are replaced with corresponding members of ARGS:\n" | |
944 | "~A formats using `display' and ~S formats using `write'.\n" | |
945 | "If DESTINATION is #t, then use the `current-output-port',\n" | |
946 | "if DESTINATION is #f, then return a string containing the formatted text.\n" | |
947 | "Does not add a trailing newline.") | |
70d63753 GB |
948 | #define FUNC_NAME s_scm_simple_format |
949 | { | |
950 | SCM answer = SCM_UNSPECIFIED; | |
951 | int fReturnString = 0; | |
952 | int writingp; | |
953 | char *start; | |
954 | char *p; | |
955 | ||
956 | if (SCM_BOOL_T == destination) { | |
957 | destination = scm_cur_outp; | |
958 | } else if (SCM_BOOL_F == destination) { | |
959 | fReturnString = 1; | |
960 | destination = scm_mkstrport (SCM_INUM0, | |
961 | scm_make_string (SCM_INUM0, SCM_UNDEFINED), | |
962 | SCM_OPN | SCM_WRTNG, | |
963 | FUNC_NAME); | |
964 | } else { | |
965 | SCM_VALIDATE_OPORT_VALUE (1,destination); | |
966 | } | |
967 | SCM_VALIDATE_STRING(2,message); | |
968 | SCM_VALIDATE_LIST(3,args); | |
969 | ||
70d63753 GB |
970 | start = SCM_ROCHARS (message); |
971 | for (p = start; *p != '\0'; ++p) | |
972 | if (*p == '~') | |
973 | { | |
974 | if (SCM_IMP (args) || SCM_NCONSP (args)) | |
975 | continue; | |
976 | ||
977 | ++p; | |
978 | if (*p == 'A') | |
979 | writingp = 0; | |
980 | else if (*p == 'S') | |
981 | writingp = 1; | |
982 | else | |
983 | continue; | |
984 | ||
985 | scm_lfwrite (start, p - start - 1, destination); | |
986 | scm_prin1 (SCM_CAR (args), destination, writingp); | |
987 | args = SCM_CDR (args); | |
988 | start = p + 1; | |
989 | } | |
990 | scm_lfwrite (start, p - start, destination); | |
991 | ||
992 | if (fReturnString) | |
993 | answer = scm_strport_to_string (destination); | |
994 | ||
5d2d2ffc | 995 | return scm_return_first(answer,message); |
70d63753 GB |
996 | } |
997 | #undef FUNC_NAME | |
998 | ||
999 | ||
3b3b36dd | 1000 | SCM_DEFINE (scm_newline, "newline", 0, 1, 0, |
1bbd0b84 | 1001 | (SCM port), |
b380b885 | 1002 | "") |
1bbd0b84 | 1003 | #define FUNC_NAME s_scm_newline |
0f2d19dd JB |
1004 | { |
1005 | if (SCM_UNBNDP (port)) | |
bb35f315 | 1006 | port = scm_cur_outp; |
3eb7e6ee | 1007 | |
3b3b36dd | 1008 | SCM_VALIDATE_OPORT_VALUE (1,port); |
bb35f315 | 1009 | |
0ef4ae82 | 1010 | scm_putc ('\n', SCM_COERCE_OUTPORT (port)); |
0f2d19dd JB |
1011 | return SCM_UNSPECIFIED; |
1012 | } | |
1bbd0b84 | 1013 | #undef FUNC_NAME |
0f2d19dd | 1014 | |
3b3b36dd | 1015 | SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0, |
1bbd0b84 | 1016 | (SCM chr, SCM port), |
b380b885 | 1017 | "") |
1bbd0b84 | 1018 | #define FUNC_NAME s_scm_write_char |
0f2d19dd JB |
1019 | { |
1020 | if (SCM_UNBNDP (port)) | |
bb35f315 | 1021 | port = scm_cur_outp; |
3eb7e6ee | 1022 | |
9a8351bc | 1023 | SCM_VALIDATE_ICHR (1,chr); |
3b3b36dd | 1024 | SCM_VALIDATE_OPORT_VALUE (2,port); |
bb35f315 | 1025 | |
0ef4ae82 | 1026 | scm_putc ((int) SCM_ICHR (chr), SCM_COERCE_OUTPORT (port)); |
0f2d19dd JB |
1027 | #ifdef HAVE_PIPE |
1028 | # ifdef EPIPE | |
1029 | if (EPIPE == errno) | |
1030 | scm_close_port (port); | |
1031 | # endif | |
1032 | #endif | |
1033 | return SCM_UNSPECIFIED; | |
1034 | } | |
1bbd0b84 | 1035 | #undef FUNC_NAME |
0f2d19dd | 1036 | |
0f2d19dd JB |
1037 | \f |
1038 | ||
bb35f315 | 1039 | /* Call back to Scheme code to do the printing of special objects |
c19bc088 MD |
1040 | * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob |
1041 | * containing PORT and PSTATE. This object can be used as the port for | |
1042 | * display/write etc to continue the current print chain. The REVEALED | |
1043 | * field of PSTATE is set to true to indicate that the print state has | |
1044 | * escaped to Scheme and thus has to be freed by the GC. | |
1045 | */ | |
1046 | ||
1047 | long scm_tc16_port_with_ps; | |
1048 | ||
1049 | /* Print exactly as the port itself would */ | |
1050 | ||
1051 | static int | |
1052 | print_port_with_ps (SCM obj, SCM port, scm_print_state *pstate) | |
1053 | { | |
1054 | obj = SCM_PORT_WITH_PS_PORT (obj); | |
1055 | return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate); | |
1056 | } | |
c4f37e80 MV |
1057 | |
1058 | SCM | |
1bbd0b84 | 1059 | scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate) |
c4f37e80 | 1060 | { |
c19bc088 | 1061 | SCM pwps; |
bb35f315 | 1062 | SCM pair = scm_cons (port, pstate->handle); |
c19bc088 | 1063 | SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, pair); |
bb35f315 | 1064 | pstate->revealed = 1; |
c19bc088 MD |
1065 | return scm_apply (proc, exp, scm_cons (pwps, scm_listofnull)); |
1066 | } | |
1067 | ||
a1ec6916 | 1068 | SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 2, 0, 0, |
1bbd0b84 | 1069 | (SCM port, SCM pstate), |
b380b885 | 1070 | "") |
1bbd0b84 | 1071 | #define FUNC_NAME s_scm_port_with_print_state |
c19bc088 MD |
1072 | { |
1073 | SCM pwps; | |
3b3b36dd GB |
1074 | SCM_VALIDATE_OPORT_VALUE (1,port); |
1075 | SCM_VALIDATE_PRINTSTATE (2,pstate); | |
c19bc088 MD |
1076 | port = SCM_COERCE_OUTPORT (port); |
1077 | SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, scm_cons (port, pstate)); | |
1078 | return pwps; | |
1079 | } | |
1bbd0b84 | 1080 | #undef FUNC_NAME |
c19bc088 | 1081 | |
a1ec6916 | 1082 | SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0, |
1bbd0b84 | 1083 | (SCM port), |
b380b885 | 1084 | "") |
1bbd0b84 | 1085 | #define FUNC_NAME s_scm_get_print_state |
c19bc088 | 1086 | { |
368cf54d GB |
1087 | if (SCM_PORT_WITH_PS_P (port)) |
1088 | return SCM_PORT_WITH_PS_PS (port); | |
1089 | if (SCM_OUTPORTP (port)) | |
1090 | return SCM_BOOL_F; | |
1bbd0b84 | 1091 | RETURN_SCM_WTA (1,port); |
c4f37e80 | 1092 | } |
1bbd0b84 | 1093 | #undef FUNC_NAME |
bb35f315 | 1094 | |
c4f37e80 | 1095 | \f |
1cc91f1b | 1096 | |
0f2d19dd JB |
1097 | void |
1098 | scm_init_print () | |
0f2d19dd | 1099 | { |
c19bc088 | 1100 | SCM vtable, layout, type; |
4bfdf158 | 1101 | |
b7ff98dd | 1102 | scm_init_opts (scm_print_options, scm_print_opts, SCM_N_PRINT_OPTIONS); |
4bfdf158 MD |
1103 | vtable = scm_make_vtable_vtable (scm_make_struct_layout (scm_nullstr), |
1104 | SCM_INUM0, | |
1105 | SCM_EOL); | |
1106 | layout = scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT)); | |
c19bc088 | 1107 | type = scm_make_struct (vtable, SCM_INUM0, SCM_LIST1 (layout)); |
e9cd0e47 | 1108 | scm_set_struct_vtable_name_x (type, SCM_CAR (scm_intern0 ("print-state"))); |
c62fbfe1 | 1109 | print_state_pool = scm_permanent_object (scm_cons (type, SCM_EOL)); |
c4f37e80 | 1110 | |
bb35f315 | 1111 | scm_print_state_vtable = type; |
c4f37e80 | 1112 | |
c19bc088 MD |
1113 | /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */ |
1114 | scm_tc16_port_with_ps = scm_make_smob_type (0, 0); | |
1115 | scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr); | |
1116 | scm_set_smob_print (scm_tc16_port_with_ps, print_port_with_ps); | |
1117 | ||
0f2d19dd JB |
1118 | #include "print.x" |
1119 | } |