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