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