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