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