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