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