Commit | Line | Data |
---|---|---|
e6e2e95a | 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 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 | ||
e6e2e95a MD |
47 | #include <errno.h> |
48 | ||
a0599745 MD |
49 | #include "libguile/_scm.h" |
50 | #include "libguile/chars.h" | |
a002f1a2 | 51 | #include "libguile/continuations.h" |
a0599745 MD |
52 | #include "libguile/smob.h" |
53 | #include "libguile/eval.h" | |
54 | #include "libguile/macros.h" | |
55 | #include "libguile/procprop.h" | |
56 | #include "libguile/read.h" | |
57 | #include "libguile/weaks.h" | |
58 | #include "libguile/unif.h" | |
59 | #include "libguile/alist.h" | |
60 | #include "libguile/struct.h" | |
61 | #include "libguile/objects.h" | |
62 | #include "libguile/ports.h" | |
63 | #include "libguile/root.h" | |
64 | #include "libguile/strings.h" | |
65 | #include "libguile/strports.h" | |
66 | #include "libguile/vectors.h" | |
67 | ||
68 | #include "libguile/validate.h" | |
69 | #include "libguile/print.h" | |
0f2d19dd JB |
70 | \f |
71 | ||
72 | /* {Names of immediate symbols} | |
73 | * | |
74 | * This table must agree with the declarations in scm.h: {Immediate Symbols}. | |
75 | */ | |
76 | ||
77 | char *scm_isymnames[] = | |
78 | { | |
79 | /* This table must agree with the declarations */ | |
80 | "#@and", | |
81 | "#@begin", | |
82 | "#@case", | |
83 | "#@cond", | |
84 | "#@do", | |
85 | "#@if", | |
86 | "#@lambda", | |
87 | "#@let", | |
88 | "#@let*", | |
89 | "#@letrec", | |
90 | "#@or", | |
91 | "#@quote", | |
92 | "#@set!", | |
93 | "#@define", | |
94 | #if 0 | |
95 | "#@literal-variable-ref", | |
96 | "#@literal-variable-set!", | |
97 | #endif | |
98 | "#@apply", | |
99 | "#@call-with-current-continuation", | |
100 | ||
101 | /* user visible ISYMS */ | |
102 | /* other keywords */ | |
103 | /* Flags */ | |
104 | ||
105 | "#f", | |
106 | "#t", | |
107 | "#<undefined>", | |
108 | "#<eof>", | |
109 | "()", | |
4df91bd6 MD |
110 | "#<unspecified>", |
111 | "#@dispatch", | |
43c667e9 MD |
112 | "#@slot-ref", |
113 | "#@slot-set!", | |
159500fb | 114 | |
159500fb MD |
115 | /* Multi-language support */ |
116 | ||
117 | "#@nil-cond", | |
118 | "#@nil-ify", | |
119 | "#@t-ify", | |
120 | "#@0-cond", | |
121 | "#@0-ify", | |
122 | "#@1-ify", | |
ccd0e478 MD |
123 | "#@bind", |
124 | ||
5623a9b4 MD |
125 | "#@delay", |
126 | ||
127 | "#<unbound>" | |
0f2d19dd JB |
128 | }; |
129 | ||
e6e4c9af | 130 | scm_option scm_print_opts[] = { |
f1267706 | 131 | { SCM_OPTION_SCM, "closure-hook", SCM_UNPACK(SCM_BOOL_F), |
84f6a34a MD |
132 | "Hook for printing closures." }, |
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 | ||
bb35f315 | 189 | SCM scm_print_state_vtable; |
c4f37e80 | 190 | |
bb35f315 | 191 | static SCM print_state_pool; |
c4f37e80 | 192 | |
f843a84c | 193 | #ifdef GUILE_DEBUG /* Used for debugging purposes */ |
1cc91f1b | 194 | |
3b3b36dd | 195 | SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0, |
1bbd0b84 | 196 | (), |
b450f070 GB |
197 | "Return the current-pstate--the `cadr' of the print_state_pool.\n" |
198 | "`current-pstate' is only included in 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; | |
256 | SCM_SETCAR (handle, print_state); | |
257 | SCM_SETCDR (handle, SCM_CDR (print_state_pool)); | |
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 | { |
c62fbfe1 MD |
284 | register int i; |
285 | int self = pstate->top - 1; | |
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 | ||
c62fbfe1 | 307 | /* Print generally. Handles both write and display according to PSTATE. |
0f2d19dd | 308 | */ |
8b840115 MD |
309 | SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); |
310 | SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); | |
1cc91f1b | 311 | |
0f2d19dd | 312 | void |
1bbd0b84 | 313 | scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate) |
0f2d19dd | 314 | { |
0f2d19dd | 315 | taloop: |
54778cd3 | 316 | switch (SCM_ITAG3 (exp)) |
0f2d19dd | 317 | { |
e34f941a DH |
318 | case scm_tc3_closure: |
319 | case scm_tc3_tc7_1: | |
320 | case scm_tc3_tc7_2: | |
321 | /* These tc3 tags should never occur in an immediate value. They are | |
322 | * only used in cell types of non-immediates, i. e. the value returned | |
323 | * by SCM_CELL_TYPE (exp) can use these tags. | |
324 | */ | |
325 | scm_ipruk ("immediate", exp, port); | |
326 | break; | |
327 | case scm_tc3_int_1: | |
328 | case scm_tc3_int_2: | |
0f2d19dd JB |
329 | scm_intprint (SCM_INUM (exp), 10, port); |
330 | break; | |
e34f941a | 331 | case scm_tc3_imm24: |
7866a09b | 332 | if (SCM_CHARP (exp)) |
0f2d19dd | 333 | { |
e34f941a | 334 | long i = SCM_CHAR (exp); |
5ca6dc39 | 335 | |
b7f3516f TT |
336 | if (SCM_WRITINGP (pstate)) |
337 | { | |
338 | scm_puts ("#\\", port); | |
339 | if ((i >= 0) && (i <= ' ') && scm_charnames[i]) | |
340 | scm_puts (scm_charnames[i], port); | |
57b74422 GH |
341 | #ifndef EBCDIC |
342 | else if (i == '\177') | |
343 | scm_puts (scm_charnames[scm_n_charnames - 1], port); | |
344 | #endif | |
b7f3516f TT |
345 | else if (i < 0 || i > '\177') |
346 | scm_intprint (i, 8, port); | |
347 | else | |
348 | scm_putc (i, port); | |
349 | } | |
350 | else | |
351 | scm_putc (i, port); | |
0f2d19dd | 352 | } |
a51ea417 | 353 | else if (SCM_IFLAGP (exp) |
5ca6dc39 | 354 | && ((size_t) SCM_ISYMNUM (exp) < (sizeof scm_isymnames / sizeof (char *)))) |
b7f3516f | 355 | scm_puts (SCM_ISYMCHARS (exp), port); |
0f2d19dd JB |
356 | else if (SCM_ILOCP (exp)) |
357 | { | |
b7f3516f | 358 | scm_puts ("#@", port); |
230d095f | 359 | scm_intprint (SCM_IFRAME (exp), 10, port); |
b7f3516f | 360 | scm_putc (SCM_ICDRP (exp) ? '-' : '+', port); |
230d095f | 361 | scm_intprint (SCM_IDIST (exp), 10, port); |
0f2d19dd JB |
362 | } |
363 | else | |
e34f941a DH |
364 | { |
365 | /* unknown immediate value */ | |
366 | scm_ipruk ("immediate", exp, port); | |
367 | } | |
0f2d19dd | 368 | break; |
e34f941a | 369 | case scm_tc3_cons_gloc: |
0f2d19dd | 370 | /* gloc */ |
b7f3516f | 371 | scm_puts ("#@", port); |
998308d3 | 372 | exp = SCM_GLOC_SYM (exp); |
0f2d19dd | 373 | goto taloop; |
e34f941a | 374 | case scm_tc3_cons: |
0f2d19dd JB |
375 | switch (SCM_TYP7 (exp)) |
376 | { | |
377 | case scm_tcs_cons_gloc: | |
378 | ||
fee7ef83 | 379 | if (SCM_STRUCT_VTABLE_DATA (exp) [scm_vtable_index_vcell] == 0) |
0f2d19dd | 380 | { |
c4f37e80 | 381 | ENTER_NESTED_DATA (pstate, exp, circref); |
8b840115 MD |
382 | if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS) |
383 | { | |
384 | SCM pwps, print = pstate->writingp ? g_write : g_display; | |
385 | if (!print) | |
386 | goto print_struct; | |
387 | SCM_NEWSMOB (pwps, | |
388 | scm_tc16_port_with_ps, | |
54778cd3 | 389 | SCM_UNPACK (scm_cons (port, pstate->handle))); |
8b840115 MD |
390 | scm_call_generic_2 (print, exp, pwps); |
391 | } | |
392 | else | |
393 | { | |
394 | print_struct: | |
395 | scm_print_struct (exp, port, pstate); | |
396 | } | |
c4f37e80 | 397 | EXIT_NESTED_DATA (pstate); |
0f2d19dd JB |
398 | break; |
399 | } | |
400 | ||
401 | case scm_tcs_cons_imcar: | |
402 | case scm_tcs_cons_nimcar: | |
c62fbfe1 MD |
403 | ENTER_NESTED_DATA (pstate, exp, circref); |
404 | scm_iprlist ("(", exp, ')', port, pstate); | |
405 | EXIT_NESTED_DATA (pstate); | |
a51ea417 MD |
406 | break; |
407 | circref: | |
c62fbfe1 | 408 | print_circref (port, pstate, exp); |
0f2d19dd | 409 | break; |
7332df66 | 410 | macros: |
80ea260c | 411 | if (!SCM_CLOSUREP (SCM_CDR (exp))) |
7332df66 | 412 | goto prinmacro; |
0f2d19dd | 413 | case scm_tcs_closures: |
7332df66 MD |
414 | /* The user supplied print closure procedure must handle |
415 | macro closures as well. */ | |
bb35f315 MV |
416 | if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE)) |
417 | || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE, | |
77c25af7 | 418 | exp, port, pstate))) |
bb35f315 | 419 | { |
ba031394 | 420 | SCM name, code, env; |
bb35f315 MV |
421 | if (SCM_TYP16 (exp) == scm_tc16_macro) |
422 | { | |
423 | /* Printing a macro. */ | |
424 | prinmacro: | |
425 | name = scm_macro_name (exp); | |
426 | if (!SCM_CLOSUREP (SCM_CDR (exp))) | |
427 | { | |
9e882eec | 428 | code = env = SCM_UNDEFINED; |
b7f3516f | 429 | scm_puts ("#<primitive-", port); |
bb35f315 MV |
430 | } |
431 | else | |
432 | { | |
433 | code = SCM_CODE (SCM_CDR (exp)); | |
ba031394 | 434 | env = SCM_ENV (SCM_CDR (exp)); |
b7f3516f | 435 | scm_puts ("#<", port); |
bb35f315 | 436 | } |
206d3de3 | 437 | if (SCM_CELL_WORD_0 (exp) & (3L << 16)) |
b7f3516f | 438 | scm_puts ("macro", port); |
bb35f315 | 439 | else |
b7f3516f | 440 | scm_puts ("syntax", port); |
206d3de3 | 441 | if (SCM_CELL_WORD_0 (exp) & (2L << 16)) |
b7f3516f | 442 | scm_putc ('!', port); |
bb35f315 MV |
443 | } |
444 | else | |
445 | { | |
446 | /* Printing a closure. */ | |
447 | name = scm_procedure_name (exp); | |
448 | code = SCM_CODE (exp); | |
ba031394 | 449 | env = SCM_ENV (exp); |
b7f3516f | 450 | scm_puts ("#<procedure", port); |
bb35f315 | 451 | } |
b24b5e13 | 452 | if (SCM_SYMBOLP (name)) |
bb35f315 | 453 | { |
b7f3516f | 454 | scm_putc (' ', port); |
b24b5e13 DH |
455 | scm_lfwrite (SCM_SYMBOL_CHARS (name), SCM_SYMBOL_LENGTH (name), port); |
456 | } | |
457 | else if (SCM_STRINGP (name)) | |
458 | { | |
459 | scm_putc (' ', port); | |
34f0f2b8 | 460 | scm_lfwrite (SCM_STRING_CHARS (name), SCM_STRING_LENGTH (name), port); |
bb35f315 | 461 | } |
9e882eec | 462 | if (!SCM_UNBNDP (code)) |
bb35f315 | 463 | { |
ba031394 MD |
464 | if (SCM_PRINT_SOURCE_P) |
465 | { | |
466 | code = scm_unmemocopy (code, | |
467 | SCM_EXTEND_ENV (SCM_CAR (code), | |
468 | SCM_EOL, | |
469 | env)); | |
470 | ENTER_NESTED_DATA (pstate, exp, circref); | |
471 | scm_iprlist (" ", code, '>', port, pstate); | |
472 | EXIT_NESTED_DATA (pstate); | |
473 | } | |
474 | else | |
475 | { | |
476 | if (SCM_TYP16 (exp) != scm_tc16_macro) | |
477 | { | |
b7f3516f | 478 | scm_putc (' ', port); |
ba031394 MD |
479 | scm_iprin1 (SCM_CAR (code), port, pstate); |
480 | } | |
b7f3516f | 481 | scm_putc ('>', port); |
ba031394 | 482 | } |
bb35f315 MV |
483 | } |
484 | else | |
b7f3516f | 485 | scm_putc ('>', port); |
bb35f315 | 486 | } |
0f2d19dd | 487 | break; |
0f2d19dd JB |
488 | case scm_tc7_substring: |
489 | case scm_tc7_string: | |
c62fbfe1 | 490 | if (SCM_WRITINGP (pstate)) |
0f2d19dd | 491 | { |
5ca6dc39 JB |
492 | scm_sizet i; |
493 | ||
b7f3516f | 494 | scm_putc ('"', port); |
d1ca2c64 | 495 | for (i = 0; i < SCM_STRING_LENGTH (exp); ++i) |
34f0f2b8 | 496 | switch (SCM_STRING_CHARS (exp)[i]) |
0f2d19dd | 497 | { |
dbef8851 | 498 | case '"': |
0f2d19dd | 499 | case '\\': |
b7f3516f | 500 | scm_putc ('\\', port); |
0f2d19dd | 501 | default: |
34f0f2b8 | 502 | scm_putc (SCM_STRING_CHARS (exp)[i], port); |
0f2d19dd | 503 | } |
b7f3516f | 504 | scm_putc ('"', port); |
0f2d19dd JB |
505 | break; |
506 | } | |
507 | else | |
34f0f2b8 | 508 | scm_lfwrite (SCM_STRING_CHARS (exp), SCM_STRING_LENGTH (exp), port); |
0f2d19dd | 509 | break; |
28b06554 | 510 | case scm_tc7_symbol: |
0f2d19dd JB |
511 | { |
512 | int pos; | |
513 | int end; | |
514 | int len; | |
515 | char * str; | |
516 | int weird; | |
517 | int maybe_weird; | |
4dc2435a | 518 | int mw_pos = 0; |
0f2d19dd | 519 | |
b17004b8 | 520 | len = SCM_SYMBOL_LENGTH (exp); |
86c991c2 | 521 | str = SCM_SYMBOL_CHARS (exp); |
0f2d19dd JB |
522 | pos = 0; |
523 | weird = 0; | |
524 | maybe_weird = 0; | |
525 | ||
526 | if (len == 0) | |
b7f3516f | 527 | scm_lfwrite ("#{}#", 4, port); |
0f2d19dd JB |
528 | |
529 | for (end = pos; end < len; ++end) | |
530 | switch (str[end]) | |
531 | { | |
532 | #ifdef BRACKETS_AS_PARENS | |
533 | case '[': | |
534 | case ']': | |
535 | #endif | |
536 | case '(': | |
537 | case ')': | |
dbef8851 | 538 | case '"': |
0f2d19dd JB |
539 | case ';': |
540 | case SCM_WHITE_SPACES: | |
541 | case SCM_LINE_INCREMENTORS: | |
542 | weird_handler: | |
543 | if (maybe_weird) | |
544 | { | |
545 | end = mw_pos; | |
546 | maybe_weird = 0; | |
547 | } | |
548 | if (!weird) | |
549 | { | |
b7f3516f | 550 | scm_lfwrite ("#{", 2, port); |
0f2d19dd JB |
551 | weird = 1; |
552 | } | |
553 | if (pos < end) | |
554 | { | |
b7f3516f | 555 | scm_lfwrite (str + pos, end - pos, port); |
0f2d19dd JB |
556 | } |
557 | { | |
558 | char buf[2]; | |
559 | buf[0] = '\\'; | |
560 | buf[1] = str[end]; | |
b7f3516f | 561 | scm_lfwrite (buf, 2, port); |
0f2d19dd JB |
562 | } |
563 | pos = end + 1; | |
564 | break; | |
565 | case '\\': | |
566 | if (weird) | |
567 | goto weird_handler; | |
568 | if (!maybe_weird) | |
569 | { | |
570 | maybe_weird = 1; | |
571 | mw_pos = pos; | |
572 | } | |
573 | break; | |
574 | case '}': | |
575 | case '#': | |
576 | if (weird) | |
577 | goto weird_handler; | |
578 | break; | |
579 | default: | |
580 | break; | |
581 | } | |
582 | if (pos < end) | |
b7f3516f | 583 | scm_lfwrite (str + pos, end - pos, port); |
5d2b97cd | 584 | scm_remember_upto_here_1 (exp); |
0f2d19dd | 585 | if (weird) |
b7f3516f | 586 | scm_lfwrite ("}#", 2, port); |
0f2d19dd JB |
587 | break; |
588 | } | |
589 | case scm_tc7_wvect: | |
c62fbfe1 | 590 | ENTER_NESTED_DATA (pstate, exp, circref); |
0f2d19dd | 591 | if (SCM_IS_WHVEC (exp)) |
b7f3516f | 592 | scm_puts ("#wh(", port); |
0f2d19dd | 593 | else |
b7f3516f | 594 | scm_puts ("#w(", port); |
0f2d19dd JB |
595 | goto common_vector_printer; |
596 | ||
597 | case scm_tc7_vector: | |
c62fbfe1 | 598 | ENTER_NESTED_DATA (pstate, exp, circref); |
b7f3516f | 599 | scm_puts ("#(", port); |
0f2d19dd | 600 | common_vector_printer: |
9fbaf27c | 601 | { |
5ca6dc39 | 602 | register long i; |
b17004b8 | 603 | int last = SCM_VECTOR_LENGTH (exp) - 1; |
9fbaf27c | 604 | int cutp = 0; |
b17004b8 | 605 | if (pstate->fancyp && SCM_VECTOR_LENGTH (exp) > pstate->length) |
9fbaf27c MD |
606 | { |
607 | last = pstate->length - 1; | |
608 | cutp = 1; | |
609 | } | |
610 | for (i = 0; i < last; ++i) | |
611 | { | |
612 | /* CHECK_INTS; */ | |
613 | scm_iprin1 (SCM_VELTS (exp)[i], port, pstate); | |
b7f3516f | 614 | scm_putc (' ', port); |
9fbaf27c MD |
615 | } |
616 | if (i == last) | |
617 | { | |
618 | /* CHECK_INTS; */ | |
619 | scm_iprin1 (SCM_VELTS (exp)[i], port, pstate); | |
620 | } | |
621 | if (cutp) | |
b7f3516f TT |
622 | scm_puts (" ...", port); |
623 | scm_putc (')', port); | |
9fbaf27c | 624 | } |
c62fbfe1 | 625 | EXIT_NESTED_DATA (pstate); |
0f2d19dd | 626 | break; |
afe5177e | 627 | #ifdef HAVE_ARRAYS |
0f2d19dd JB |
628 | case scm_tc7_bvect: |
629 | case scm_tc7_byvect: | |
630 | case scm_tc7_svect: | |
631 | case scm_tc7_ivect: | |
632 | case scm_tc7_uvect: | |
633 | case scm_tc7_fvect: | |
634 | case scm_tc7_dvect: | |
635 | case scm_tc7_cvect: | |
5c11cc9d | 636 | #ifdef HAVE_LONG_LONGS |
0f2d19dd JB |
637 | case scm_tc7_llvect: |
638 | #endif | |
c62fbfe1 | 639 | scm_raprin1 (exp, port, pstate); |
0f2d19dd | 640 | break; |
afe5177e | 641 | #endif |
0f2d19dd | 642 | case scm_tcs_subrs: |
9de33deb MD |
643 | scm_puts (SCM_SUBR_GENERIC (exp) && *SCM_SUBR_GENERIC (exp) |
644 | ? "#<primitive-generic " | |
645 | : "#<primitive-procedure ", | |
646 | port); | |
a002f1a2 | 647 | scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp)), port); |
b7f3516f | 648 | scm_putc ('>', port); |
0f2d19dd JB |
649 | break; |
650 | #ifdef CCLO | |
651 | case scm_tc7_cclo: | |
56977059 MD |
652 | { |
653 | SCM proc = SCM_CCLO_SUBR (exp); | |
54778cd3 | 654 | if (SCM_EQ_P (proc, scm_f_gsubr_apply)) |
56977059 MD |
655 | { |
656 | /* Print gsubrs as primitives */ | |
c180dfd3 | 657 | SCM name = scm_procedure_name (exp); |
56977059 MD |
658 | scm_puts ("#<primitive-procedure", port); |
659 | if (SCM_NFALSEP (name)) | |
660 | { | |
661 | scm_putc (' ', port); | |
a002f1a2 | 662 | scm_puts (SCM_SYMBOL_CHARS (name), port); |
56977059 MD |
663 | } |
664 | } | |
665 | else | |
666 | { | |
667 | scm_puts ("#<compiled-closure ", port); | |
668 | scm_iprin1 (proc, port, pstate); | |
669 | } | |
670 | scm_putc ('>', port); | |
671 | } | |
0f2d19dd JB |
672 | break; |
673 | #endif | |
c180dfd3 MD |
674 | case scm_tc7_pws: |
675 | scm_puts ("#<procedure-with-setter", port); | |
676 | { | |
677 | SCM name = scm_procedure_name (exp); | |
678 | if (SCM_NFALSEP (name)) | |
679 | { | |
680 | scm_putc (' ', port); | |
b24b5e13 | 681 | scm_display (name, port); |
c180dfd3 MD |
682 | } |
683 | } | |
684 | scm_putc ('>', port); | |
685 | break; | |
0f2d19dd | 686 | case scm_tc7_port: |
5ca6dc39 JB |
687 | { |
688 | register long i = SCM_PTOBNUM (exp); | |
689 | if (i < scm_numptob | |
690 | && scm_ptobs[i].print | |
691 | && (scm_ptobs[i].print) (exp, port, pstate)) | |
a51ea417 | 692 | break; |
5ca6dc39 JB |
693 | goto punk; |
694 | } | |
695 | case scm_tc7_smob: | |
696 | { | |
697 | register long i; | |
698 | ENTER_NESTED_DATA (pstate, exp, circref); | |
699 | i = SCM_SMOBNUM (exp); | |
700 | if (i < scm_numsmob && scm_smobs[i].print | |
701 | && (scm_smobs[i].print) (exp, port, pstate)) | |
702 | { | |
703 | EXIT_NESTED_DATA (pstate); | |
704 | break; | |
705 | } | |
706 | EXIT_NESTED_DATA (pstate); | |
707 | /* Macros have their print field set to NULL. They are | |
708 | handled at the same place as closures in order to achieve | |
709 | non-redundancy. Placing the condition here won't slow | |
710 | down printing of other smobs. */ | |
711 | if (SCM_TYP16 (exp) == scm_tc16_macro) | |
712 | goto macros; | |
713 | } | |
0f2d19dd | 714 | default: |
a51ea417 MD |
715 | punk: |
716 | scm_ipruk ("type", exp, port); | |
0f2d19dd JB |
717 | } |
718 | } | |
719 | } | |
720 | ||
c62fbfe1 MD |
721 | /* Print states are necessary for circular reference safe printing. |
722 | * They are also expensive to allocate. Therefore print states are | |
723 | * kept in a pool so that they can be reused. | |
724 | */ | |
1cc91f1b | 725 | |
bb35f315 MV |
726 | /* The PORT argument can also be a print-state/port pair, which will |
727 | * then be used instead of allocating a new print state. This is | |
728 | * useful for continuing a chain of print calls from Scheme. */ | |
729 | ||
a51ea417 | 730 | void |
1bbd0b84 | 731 | scm_prin1 (SCM exp, SCM port, int writingp) |
a51ea417 | 732 | { |
c4f37e80 MV |
733 | SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */ |
734 | SCM pstate_scm; | |
c62fbfe1 MD |
735 | scm_print_state *pstate; |
736 | ||
bb35f315 MV |
737 | /* If PORT is a print-state/port pair, use that. Else create a new |
738 | print-state. */ | |
c4f37e80 | 739 | |
0c95b57d | 740 | if (SCM_PORT_WITH_PS_P (port)) |
bb35f315 | 741 | { |
52235e71 MD |
742 | pstate_scm = SCM_PORT_WITH_PS_PS (port); |
743 | port = SCM_PORT_WITH_PS_PORT (port); | |
bb35f315 MV |
744 | } |
745 | else | |
c62fbfe1 | 746 | { |
c4f37e80 MV |
747 | /* First try to allocate a print state from the pool */ |
748 | SCM_DEFER_INTS; | |
749 | if (SCM_NNULLP (SCM_CDR (print_state_pool))) | |
750 | { | |
751 | handle = SCM_CDR (print_state_pool); | |
752 | SCM_SETCDR (print_state_pool, SCM_CDDR (print_state_pool)); | |
753 | } | |
754 | SCM_ALLOW_INTS; | |
54778cd3 | 755 | if (SCM_FALSEP (handle)) |
c4f37e80 MV |
756 | handle = scm_cons (make_print_state (), SCM_EOL); |
757 | pstate_scm = SCM_CAR (handle); | |
c62fbfe1 | 758 | } |
c62fbfe1 | 759 | |
c4f37e80 | 760 | pstate = SCM_PRINT_STATE (pstate_scm); |
c62fbfe1 MD |
761 | pstate->writingp = writingp; |
762 | scm_iprin1 (exp, port, pstate); | |
763 | ||
bb35f315 MV |
764 | /* Return print state to pool if it has been created above and |
765 | hasn't escaped to Scheme. */ | |
766 | ||
54778cd3 | 767 | if (!SCM_FALSEP (handle) && !pstate->revealed) |
c4f37e80 MV |
768 | { |
769 | SCM_DEFER_INTS; | |
770 | SCM_SETCDR (handle, SCM_CDR (print_state_pool)); | |
771 | SCM_SETCDR (print_state_pool, handle); | |
772 | SCM_ALLOW_INTS; | |
773 | } | |
a51ea417 MD |
774 | } |
775 | ||
0f2d19dd JB |
776 | |
777 | /* Print an integer. | |
778 | */ | |
1cc91f1b | 779 | |
0f2d19dd | 780 | void |
1bbd0b84 | 781 | scm_intprint (long n, int radix, SCM port) |
0f2d19dd JB |
782 | { |
783 | char num_buf[SCM_INTBUFLEN]; | |
b7f3516f | 784 | scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port); |
0f2d19dd JB |
785 | } |
786 | ||
787 | /* Print an object of unrecognized type. | |
788 | */ | |
1cc91f1b | 789 | |
0f2d19dd | 790 | void |
1bbd0b84 | 791 | scm_ipruk (char *hdr, SCM ptr, SCM port) |
0f2d19dd | 792 | { |
b7f3516f TT |
793 | scm_puts ("#<unknown-", port); |
794 | scm_puts (hdr, port); | |
0f2d19dd JB |
795 | if (SCM_CELLP (ptr)) |
796 | { | |
b7f3516f | 797 | scm_puts (" (0x", port); |
54778cd3 | 798 | scm_intprint (SCM_CELL_WORD_0 (ptr), 16, port); |
b7f3516f | 799 | scm_puts (" . 0x", port); |
54778cd3 | 800 | scm_intprint (SCM_CELL_WORD_1 (ptr), 16, port); |
b7f3516f | 801 | scm_puts (") @", port); |
0f2d19dd | 802 | } |
b7f3516f | 803 | scm_puts (" 0x", port); |
54778cd3 | 804 | scm_intprint (SCM_UNPACK (ptr), 16, port); |
b7f3516f | 805 | scm_putc ('>', port); |
0f2d19dd JB |
806 | } |
807 | ||
808 | /* Print a list. | |
809 | */ | |
a51ea417 | 810 | |
1cc91f1b | 811 | |
0f2d19dd | 812 | void |
1bbd0b84 | 813 | scm_iprlist (char *hdr,SCM exp,int tlr,SCM port,scm_print_state *pstate) |
0f2d19dd | 814 | { |
c62fbfe1 MD |
815 | register SCM hare, tortoise; |
816 | int floor = pstate->top - 2; | |
b7f3516f | 817 | scm_puts (hdr, port); |
0f2d19dd | 818 | /* CHECK_INTS; */ |
c62fbfe1 MD |
819 | if (pstate->fancyp) |
820 | goto fancy_printing; | |
821 | ||
822 | /* Run a hare and tortoise so that total time complexity will be | |
823 | O(depth * N) instead of O(N^2). */ | |
824 | hare = SCM_CDR (exp); | |
825 | tortoise = exp; | |
0c95b57d | 826 | while (SCM_ECONSP (hare)) |
c62fbfe1 | 827 | { |
54778cd3 | 828 | if (SCM_EQ_P (hare, tortoise)) |
c62fbfe1 MD |
829 | goto fancy_printing; |
830 | hare = SCM_CDR (hare); | |
2fab3faa | 831 | if (SCM_IMP (hare) || SCM_NECONSP (hare)) |
c62fbfe1 MD |
832 | break; |
833 | hare = SCM_CDR (hare); | |
834 | tortoise = SCM_CDR (tortoise); | |
835 | } | |
836 | ||
837 | /* No cdr cycles intrinsic to this list */ | |
838 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
0f2d19dd JB |
839 | exp = SCM_CDR (exp); |
840 | for (; SCM_NIMP (exp); exp = SCM_CDR (exp)) | |
841 | { | |
5ca6dc39 JB |
842 | register int i; |
843 | ||
0f2d19dd JB |
844 | if (SCM_NECONSP (exp)) |
845 | break; | |
c62fbfe1 | 846 | for (i = floor; i >= 0; --i) |
230d095f | 847 | if (SCM_EQ_P (pstate->ref_stack[i], exp)) |
c62fbfe1 MD |
848 | goto circref; |
849 | PUSH_REF (pstate, exp); | |
b7f3516f | 850 | scm_putc (' ', port); |
0f2d19dd | 851 | /* CHECK_INTS; */ |
c62fbfe1 | 852 | scm_iprin1 (SCM_CAR (exp), port, pstate); |
0f2d19dd JB |
853 | } |
854 | if (SCM_NNULLP (exp)) | |
855 | { | |
b7f3516f | 856 | scm_puts (" . ", port); |
c62fbfe1 | 857 | scm_iprin1 (exp, port, pstate); |
0f2d19dd | 858 | } |
c62fbfe1 | 859 | |
a51ea417 | 860 | end: |
b7f3516f | 861 | scm_putc (tlr, port); |
c62fbfe1 | 862 | pstate->top = floor + 2; |
a51ea417 | 863 | return; |
c62fbfe1 MD |
864 | |
865 | fancy_printing: | |
866 | { | |
867 | int n = pstate->length; | |
868 | ||
869 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
870 | exp = SCM_CDR (exp); --n; | |
871 | for (; SCM_NIMP (exp); exp = SCM_CDR (exp)) | |
872 | { | |
5ca6dc39 JB |
873 | register unsigned long i; |
874 | ||
c62fbfe1 MD |
875 | if (SCM_NECONSP (exp)) |
876 | break; | |
877 | for (i = 0; i < pstate->top; ++i) | |
230d095f | 878 | if (SCM_EQ_P (pstate->ref_stack[i], exp)) |
c62fbfe1 MD |
879 | goto fancy_circref; |
880 | if (pstate->fancyp) | |
881 | { | |
882 | if (n == 0) | |
883 | { | |
b7f3516f | 884 | scm_puts (" ...", port); |
c62fbfe1 MD |
885 | goto skip_tail; |
886 | } | |
887 | else | |
888 | --n; | |
889 | } | |
890 | PUSH_REF(pstate, exp); | |
891 | ++pstate->list_offset; | |
b7f3516f | 892 | scm_putc (' ', port); |
c62fbfe1 MD |
893 | /* CHECK_INTS; */ |
894 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
895 | } | |
896 | } | |
897 | if (SCM_NNULLP (exp)) | |
898 | { | |
b7f3516f | 899 | scm_puts (" . ", port); |
c62fbfe1 MD |
900 | scm_iprin1 (exp, port, pstate); |
901 | } | |
902 | skip_tail: | |
903 | pstate->list_offset -= pstate->top - floor - 2; | |
a51ea417 | 904 | goto end; |
a51ea417 | 905 | |
c62fbfe1 MD |
906 | fancy_circref: |
907 | pstate->list_offset -= pstate->top - floor - 2; | |
908 | ||
909 | circref: | |
b7f3516f | 910 | scm_puts (" . ", port); |
c62fbfe1 MD |
911 | print_circref (port, pstate, exp); |
912 | goto end; | |
0f2d19dd JB |
913 | } |
914 | ||
915 | \f | |
916 | ||
bb35f315 MV |
917 | int |
918 | scm_valid_oport_value_p (SCM val) | |
919 | { | |
368cf54d GB |
920 | return (SCM_OPOUTPORTP (val) |
921 | || (SCM_PORT_WITH_PS_P (val) | |
922 | && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val)))); | |
bb35f315 MV |
923 | } |
924 | ||
8b840115 | 925 | /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */ |
1cc91f1b | 926 | |
0f2d19dd | 927 | SCM |
1bbd0b84 | 928 | scm_write (SCM obj, SCM port) |
0f2d19dd JB |
929 | { |
930 | if (SCM_UNBNDP (port)) | |
931 | port = scm_cur_outp; | |
3eb7e6ee JB |
932 | |
933 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write); | |
bb35f315 | 934 | |
a51ea417 | 935 | scm_prin1 (obj, port, 1); |
0f2d19dd JB |
936 | #ifdef HAVE_PIPE |
937 | # ifdef EPIPE | |
938 | if (EPIPE == errno) | |
939 | scm_close_port (port); | |
940 | # endif | |
941 | #endif | |
942 | return SCM_UNSPECIFIED; | |
943 | } | |
944 | ||
945 | ||
8b840115 | 946 | /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */ |
1cc91f1b | 947 | |
0f2d19dd | 948 | SCM |
1bbd0b84 | 949 | scm_display (SCM obj, SCM port) |
0f2d19dd JB |
950 | { |
951 | if (SCM_UNBNDP (port)) | |
952 | port = scm_cur_outp; | |
3eb7e6ee JB |
953 | |
954 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display); | |
bb35f315 | 955 | |
a51ea417 | 956 | scm_prin1 (obj, port, 0); |
0f2d19dd JB |
957 | #ifdef HAVE_PIPE |
958 | # ifdef EPIPE | |
959 | if (EPIPE == errno) | |
960 | scm_close_port (port); | |
961 | # endif | |
962 | #endif | |
963 | return SCM_UNSPECIFIED; | |
964 | } | |
965 | ||
70d63753 GB |
966 | |
967 | SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1, | |
968 | (SCM destination, SCM message, SCM args), | |
eca65e90 MG |
969 | "Write @var{message} to @var{destination}, defaulting to\n" |
970 | "the current output port.\n" | |
971 | "@var{message} can contain @code{~A} (was @code{%s}) and\n" | |
972 | "@code{~S} (was @code{%S}) escapes. When printed,\n" | |
973 | "the escapes are replaced with corresponding members of\n" | |
974 | "@var{ARGS}:\n" | |
975 | "@code{~A} formats using @code{display} and @code{~S} formats\n" | |
976 | "using @code{write}.\n" | |
977 | "If @var{destination} is @code{#t}, then use the current output\n" | |
978 | "port, if @var{destination} is @code{#f}, then return a string\n" | |
979 | "containing the formatted text. Does not add a trailing newline.") | |
70d63753 GB |
980 | #define FUNC_NAME s_scm_simple_format |
981 | { | |
982 | SCM answer = SCM_UNSPECIFIED; | |
983 | int fReturnString = 0; | |
984 | int writingp; | |
985 | char *start; | |
b24b5e13 | 986 | char *end; |
70d63753 GB |
987 | char *p; |
988 | ||
daba1a71 MD |
989 | if (SCM_EQ_P (destination, SCM_BOOL_T)) |
990 | { | |
991 | destination = scm_cur_outp; | |
992 | } | |
993 | else if (SCM_FALSEP (destination)) | |
994 | { | |
995 | fReturnString = 1; | |
996 | destination = scm_mkstrport (SCM_INUM0, | |
997 | scm_make_string (SCM_INUM0, SCM_UNDEFINED), | |
998 | SCM_OPN | SCM_WRTNG, | |
999 | FUNC_NAME); | |
1000 | } | |
1001 | else | |
1002 | { | |
1003 | SCM_VALIDATE_OPORT_VALUE (1, destination); | |
1004 | destination = SCM_COERCE_OUTPORT (destination); | |
1005 | } | |
1006 | SCM_VALIDATE_STRING (2, message); | |
af45e3b0 | 1007 | SCM_VALIDATE_REST_ARGUMENT (args); |
70d63753 | 1008 | |
34f0f2b8 | 1009 | start = SCM_STRING_CHARS (message); |
b24b5e13 DH |
1010 | end = start + SCM_STRING_LENGTH (message); |
1011 | for (p = start; p != end; ++p) | |
70d63753 GB |
1012 | if (*p == '~') |
1013 | { | |
b24b5e13 DH |
1014 | if (!SCM_CONSP (args)) |
1015 | continue; | |
1016 | ||
1017 | if (++p == end) | |
70d63753 GB |
1018 | continue; |
1019 | ||
cc6f0237 | 1020 | if (*p == 'A' || *p == 'a') |
70d63753 | 1021 | writingp = 0; |
cc6f0237 | 1022 | else if (*p == 'S' || *p == 's') |
70d63753 GB |
1023 | writingp = 1; |
1024 | else | |
1025 | continue; | |
1026 | ||
1027 | scm_lfwrite (start, p - start - 1, destination); | |
1028 | scm_prin1 (SCM_CAR (args), destination, writingp); | |
1029 | args = SCM_CDR (args); | |
1030 | start = p + 1; | |
1031 | } | |
1032 | scm_lfwrite (start, p - start, destination); | |
1033 | ||
1034 | if (fReturnString) | |
1035 | answer = scm_strport_to_string (destination); | |
1036 | ||
daba1a71 | 1037 | return scm_return_first (answer, message); |
70d63753 GB |
1038 | } |
1039 | #undef FUNC_NAME | |
1040 | ||
1041 | ||
3b3b36dd | 1042 | SCM_DEFINE (scm_newline, "newline", 0, 1, 0, |
b450f070 | 1043 | (SCM port), |
eca65e90 | 1044 | "Send a newline to @var{port}.") |
1bbd0b84 | 1045 | #define FUNC_NAME s_scm_newline |
0f2d19dd JB |
1046 | { |
1047 | if (SCM_UNBNDP (port)) | |
bb35f315 | 1048 | port = scm_cur_outp; |
3eb7e6ee | 1049 | |
3b3b36dd | 1050 | SCM_VALIDATE_OPORT_VALUE (1,port); |
bb35f315 | 1051 | |
0ef4ae82 | 1052 | scm_putc ('\n', SCM_COERCE_OUTPORT (port)); |
0f2d19dd JB |
1053 | return SCM_UNSPECIFIED; |
1054 | } | |
1bbd0b84 | 1055 | #undef FUNC_NAME |
0f2d19dd | 1056 | |
3b3b36dd | 1057 | SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0, |
b450f070 | 1058 | (SCM chr, SCM port), |
eca65e90 | 1059 | "Send character @var{chr} to @var{port}.") |
1bbd0b84 | 1060 | #define FUNC_NAME s_scm_write_char |
0f2d19dd JB |
1061 | { |
1062 | if (SCM_UNBNDP (port)) | |
bb35f315 | 1063 | port = scm_cur_outp; |
3eb7e6ee | 1064 | |
7866a09b | 1065 | SCM_VALIDATE_CHAR (1,chr); |
3b3b36dd | 1066 | SCM_VALIDATE_OPORT_VALUE (2,port); |
bb35f315 | 1067 | |
7866a09b | 1068 | scm_putc ((int) SCM_CHAR (chr), SCM_COERCE_OUTPORT (port)); |
0f2d19dd JB |
1069 | #ifdef HAVE_PIPE |
1070 | # ifdef EPIPE | |
1071 | if (EPIPE == errno) | |
1072 | scm_close_port (port); | |
1073 | # endif | |
1074 | #endif | |
1075 | return SCM_UNSPECIFIED; | |
1076 | } | |
1bbd0b84 | 1077 | #undef FUNC_NAME |
0f2d19dd | 1078 | |
0f2d19dd JB |
1079 | \f |
1080 | ||
bb35f315 | 1081 | /* Call back to Scheme code to do the printing of special objects |
c19bc088 MD |
1082 | * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob |
1083 | * containing PORT and PSTATE. This object can be used as the port for | |
1084 | * display/write etc to continue the current print chain. The REVEALED | |
1085 | * field of PSTATE is set to true to indicate that the print state has | |
1086 | * escaped to Scheme and thus has to be freed by the GC. | |
1087 | */ | |
1088 | ||
e841c3e0 | 1089 | scm_bits_t scm_tc16_port_with_ps; |
c19bc088 MD |
1090 | |
1091 | /* Print exactly as the port itself would */ | |
1092 | ||
1093 | static int | |
e841c3e0 | 1094 | port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate) |
c19bc088 MD |
1095 | { |
1096 | obj = SCM_PORT_WITH_PS_PORT (obj); | |
1097 | return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate); | |
1098 | } | |
c4f37e80 MV |
1099 | |
1100 | SCM | |
1bbd0b84 | 1101 | scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate) |
c4f37e80 | 1102 | { |
c19bc088 | 1103 | SCM pwps; |
bb35f315 | 1104 | SCM pair = scm_cons (port, pstate->handle); |
54778cd3 | 1105 | SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, SCM_UNPACK (pair)); |
bb35f315 | 1106 | pstate->revealed = 1; |
c19bc088 MD |
1107 | return scm_apply (proc, exp, scm_cons (pwps, scm_listofnull)); |
1108 | } | |
1109 | ||
a1ec6916 | 1110 | SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 2, 0, 0, |
1bbd0b84 | 1111 | (SCM port, SCM pstate), |
71331188 MG |
1112 | "Create a new port which behaves like @var{port}, but with an\n" |
1113 | "included print state @var{pstate}.") | |
1bbd0b84 | 1114 | #define FUNC_NAME s_scm_port_with_print_state |
c19bc088 MD |
1115 | { |
1116 | SCM pwps; | |
3b3b36dd GB |
1117 | SCM_VALIDATE_OPORT_VALUE (1,port); |
1118 | SCM_VALIDATE_PRINTSTATE (2,pstate); | |
c19bc088 | 1119 | port = SCM_COERCE_OUTPORT (port); |
54778cd3 | 1120 | SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, SCM_UNPACK (scm_cons (port, pstate))); |
c19bc088 MD |
1121 | return pwps; |
1122 | } | |
1bbd0b84 | 1123 | #undef FUNC_NAME |
c19bc088 | 1124 | |
a1ec6916 | 1125 | SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0, |
1bbd0b84 | 1126 | (SCM port), |
71331188 MG |
1127 | "Return the print state of the port @var{port}. If @var{port}\n" |
1128 | "has no associated print state, @code{#f} is returned.") | |
1bbd0b84 | 1129 | #define FUNC_NAME s_scm_get_print_state |
c19bc088 | 1130 | { |
368cf54d GB |
1131 | if (SCM_PORT_WITH_PS_P (port)) |
1132 | return SCM_PORT_WITH_PS_PS (port); | |
f5f2dcff | 1133 | if (SCM_OUTPUT_PORT_P (port)) |
368cf54d | 1134 | return SCM_BOOL_F; |
276dd677 | 1135 | SCM_WRONG_TYPE_ARG (1, port); |
c4f37e80 | 1136 | } |
1bbd0b84 | 1137 | #undef FUNC_NAME |
bb35f315 | 1138 | |
c4f37e80 | 1139 | \f |
1cc91f1b | 1140 | |
0f2d19dd JB |
1141 | void |
1142 | scm_init_print () | |
0f2d19dd | 1143 | { |
c19bc088 | 1144 | SCM vtable, layout, type; |
4bfdf158 | 1145 | |
b7ff98dd | 1146 | scm_init_opts (scm_print_options, scm_print_opts, SCM_N_PRINT_OPTIONS); |
3ce4544c | 1147 | vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL); |
4bfdf158 | 1148 | layout = scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT)); |
c19bc088 | 1149 | type = scm_make_struct (vtable, SCM_INUM0, SCM_LIST1 (layout)); |
38ae064c | 1150 | scm_set_struct_vtable_name_x (type, scm_str2symbol ("print-state")); |
c62fbfe1 | 1151 | print_state_pool = scm_permanent_object (scm_cons (type, SCM_EOL)); |
c4f37e80 | 1152 | |
bb35f315 | 1153 | scm_print_state_vtable = type; |
c4f37e80 | 1154 | |
c19bc088 MD |
1155 | /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */ |
1156 | scm_tc16_port_with_ps = scm_make_smob_type (0, 0); | |
1157 | scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr); | |
e841c3e0 | 1158 | scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print); |
c19bc088 | 1159 | |
8dc9439f | 1160 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 1161 | #include "libguile/print.x" |
8dc9439f | 1162 | #endif |
0f2d19dd | 1163 | } |
89e00824 ML |
1164 | |
1165 | /* | |
1166 | Local Variables: | |
1167 | c-file-style: "gnu" | |
1168 | End: | |
1169 | */ |