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