Commit | Line | Data |
---|---|---|
e2c2a699 | 1 | /* Copyright (C) 1995-1999,2000,2001, 2002, 2003, 2004, 2006, 2008, 2009, 2010 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
0f2d19dd | 20 | \f |
dbb605f5 LC |
21 | #ifdef HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
0f2d19dd | 24 | |
e6e2e95a | 25 | #include <errno.h> |
eca29b02 | 26 | #include <uniconv.h> |
904a78f1 | 27 | #include <unictype.h> |
e6e2e95a | 28 | |
a0599745 MD |
29 | #include "libguile/_scm.h" |
30 | #include "libguile/chars.h" | |
a002f1a2 | 31 | #include "libguile/continuations.h" |
a0599745 | 32 | #include "libguile/smob.h" |
bbb2ecd1 | 33 | #include "libguile/control.h" |
a0599745 MD |
34 | #include "libguile/eval.h" |
35 | #include "libguile/macros.h" | |
36 | #include "libguile/procprop.h" | |
37 | #include "libguile/read.h" | |
38 | #include "libguile/weaks.h" | |
2fb924f6 | 39 | #include "libguile/programs.h" |
a0599745 MD |
40 | #include "libguile/alist.h" |
41 | #include "libguile/struct.h" | |
a0599745 MD |
42 | #include "libguile/ports.h" |
43 | #include "libguile/root.h" | |
44 | #include "libguile/strings.h" | |
45 | #include "libguile/strports.h" | |
46 | #include "libguile/vectors.h" | |
327967ef | 47 | #include "libguile/numbers.h" |
6f3b0cc2 | 48 | #include "libguile/vm.h" |
a0599745 MD |
49 | |
50 | #include "libguile/validate.h" | |
51 | #include "libguile/print.h" | |
22fc179a HWN |
52 | |
53 | #include "libguile/private-options.h" | |
54 | ||
0f2d19dd JB |
55 | \f |
56 | ||
07f49ac7 LC |
57 | /* Character printers. */ |
58 | ||
59 | static int display_character (scm_t_wchar, SCM, | |
60 | scm_t_string_failed_conversion_handler); | |
61 | static void write_character (scm_t_wchar, SCM, int); | |
62 | ||
63 | \f | |
64 | ||
0f2d19dd JB |
65 | /* {Names of immediate symbols} |
66 | * | |
67 | * This table must agree with the declarations in scm.h: {Immediate Symbols}. | |
68 | */ | |
69 | ||
e17d318f DH |
70 | /* This table must agree with the list of flags in tags.h. */ |
71 | static const char *iflagnames[] = | |
72 | { | |
73 | "#f", | |
45f4cbdf MW |
74 | "#nil", /* Elisp nil value. Should print from elisp as symbol `nil'. */ |
75 | "#<XXX UNUSED LISP FALSE -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>", | |
76 | "()", | |
e17d318f | 77 | "#t", |
f60c2c4e MW |
78 | "#<XXX UNUSED BOOLEAN 0 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>", |
79 | "#<XXX UNUSED BOOLEAN 1 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>", | |
80 | "#<XXX UNUSED BOOLEAN 2 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>", | |
45f4cbdf | 81 | "#<unspecified>", |
e17d318f DH |
82 | "#<undefined>", |
83 | "#<eof>", | |
e17d318f DH |
84 | |
85 | /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */ | |
86 | "#<unbound>", | |
e17d318f DH |
87 | }; |
88 | ||
475fa9a5 MV |
89 | SCM_SYMBOL (sym_reader, "reader"); |
90 | ||
92c2555f | 91 | scm_t_option scm_print_opts[] = { |
62316c7f | 92 | { SCM_OPTION_SCM, "closure-hook", (unsigned long) SCM_BOOL_F, |
726d810a | 93 | "Hook for printing closures (should handle macros as well)." }, |
84f6a34a | 94 | { SCM_OPTION_BOOLEAN, "source", 0, |
81ae25da MV |
95 | "Print closures with source." }, |
96 | { SCM_OPTION_SCM, "highlight-prefix", (unsigned long)SCM_BOOL_F, | |
97 | "The string to print before highlighted values." }, | |
98 | { SCM_OPTION_SCM, "highlight-suffix", (unsigned long)SCM_BOOL_F, | |
475fa9a5 MV |
99 | "The string to print after highlighted values." }, |
100 | { SCM_OPTION_SCM, "quote-keywordish-symbols", (unsigned long)SCM_BOOL_F, | |
101 | "How to print symbols that have a colon as their first or last character. " | |
102 | "The value '#f' does not quote the colons; '#t' quotes them; " | |
103 | "'reader' quotes them when the reader option 'keywords' is not '#f'." | |
62560650 HWN |
104 | }, |
105 | { 0 }, | |
e6e4c9af MD |
106 | }; |
107 | ||
a1ec6916 | 108 | SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0, |
1bbd0b84 | 109 | (SCM setting), |
71331188 | 110 | "Option interface for the print options. Instead of using\n" |
1dd05fd8 MG |
111 | "this procedure directly, use the procedures\n" |
112 | "@code{print-enable}, @code{print-disable}, @code{print-set!}\n" | |
113 | "and @code{print-options}.") | |
1bbd0b84 | 114 | #define FUNC_NAME s_scm_print_options |
e6e4c9af | 115 | { |
a51ea417 | 116 | SCM ans = scm_options (setting, |
b7ff98dd | 117 | scm_print_opts, |
1bbd0b84 | 118 | FUNC_NAME); |
e6e4c9af MD |
119 | return ans; |
120 | } | |
1bbd0b84 | 121 | #undef FUNC_NAME |
e6e4c9af | 122 | |
0f2d19dd JB |
123 | \f |
124 | /* {Printing of Scheme Objects} | |
125 | */ | |
126 | ||
a51ea417 | 127 | /* Detection of circular references. |
c62fbfe1 MD |
128 | * |
129 | * Due to other constraints in the implementation, this code has bad | |
5d46ebe3 MD |
130 | * time complexity (O (depth * N)), The printer code can be |
131 | * rewritten to be O(N). | |
a51ea417 | 132 | */ |
dbb5de29 NJ |
133 | #define PUSH_REF(pstate, obj) \ |
134 | do \ | |
135 | { \ | |
136 | PSTATE_STACK_SET (pstate, pstate->top, obj); \ | |
137 | pstate->top++; \ | |
138 | if (pstate->top == pstate->ceiling) \ | |
139 | grow_ref_stack (pstate); \ | |
1bbd0b84 | 140 | } while(0) |
a51ea417 | 141 | |
dbb5de29 NJ |
142 | #define ENTER_NESTED_DATA(pstate, obj, label) \ |
143 | do \ | |
144 | { \ | |
145 | register unsigned long i; \ | |
146 | for (i = 0; i < pstate->top; ++i) \ | |
147 | if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \ | |
148 | goto label; \ | |
149 | if (pstate->fancyp) \ | |
150 | { \ | |
151 | if (pstate->top - pstate->list_offset >= pstate->level) \ | |
152 | { \ | |
153 | scm_putc ('#', port); \ | |
154 | return; \ | |
155 | } \ | |
156 | } \ | |
157 | PUSH_REF(pstate, obj); \ | |
1bbd0b84 | 158 | } while(0) |
a51ea417 | 159 | |
dbb5de29 NJ |
160 | #define EXIT_NESTED_DATA(pstate) \ |
161 | do \ | |
162 | { \ | |
163 | --pstate->top; \ | |
164 | PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \ | |
165 | } \ | |
166 | while (0) | |
c62fbfe1 | 167 | |
d5cf5324 DH |
168 | SCM scm_print_state_vtable = SCM_BOOL_F; |
169 | static SCM print_state_pool = SCM_EOL; | |
9de87eea | 170 | scm_i_pthread_mutex_t print_state_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER; |
c4f37e80 | 171 | |
f843a84c | 172 | #ifdef GUILE_DEBUG /* Used for debugging purposes */ |
1cc91f1b | 173 | |
3b3b36dd | 174 | SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0, |
1bbd0b84 | 175 | (), |
d5cf5324 | 176 | "Return the current-pstate -- the car of the\n" |
5352393c MG |
177 | "@code{print_state_pool}. @code{current-pstate} is only\n" |
178 | "included in @code{--enable-guile-debug} builds.") | |
1bbd0b84 | 179 | #define FUNC_NAME s_scm_current_pstate |
c62fbfe1 | 180 | { |
d2e53ed6 | 181 | if (!scm_is_null (print_state_pool)) |
d5cf5324 | 182 | return SCM_CAR (print_state_pool); |
a0adfbf0 | 183 | else |
0a284a4e | 184 | return SCM_BOOL_F; |
c62fbfe1 | 185 | } |
1bbd0b84 GB |
186 | #undef FUNC_NAME |
187 | ||
c62fbfe1 MD |
188 | #endif |
189 | ||
190 | #define PSTATE_SIZE 50L | |
191 | ||
698c0295 | 192 | static SCM |
1bbd0b84 | 193 | make_print_state (void) |
698c0295 | 194 | { |
d5cf5324 DH |
195 | SCM print_state |
196 | = scm_make_struct (scm_print_state_vtable, SCM_INUM0, SCM_EOL); | |
bf685b6d | 197 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); |
00ffa0e7 | 198 | pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED); |
4057a3e0 | 199 | pstate->ceiling = SCM_SIMPLE_VECTOR_LENGTH (pstate->ref_vect); |
d232520a | 200 | pstate->highlight_objects = SCM_EOL; |
698c0295 MD |
201 | return print_state; |
202 | } | |
1cc91f1b | 203 | |
c62fbfe1 MD |
204 | SCM |
205 | scm_make_print_state () | |
c62fbfe1 | 206 | { |
230d095f | 207 | SCM answer = SCM_BOOL_F; |
698c0295 MD |
208 | |
209 | /* First try to allocate a print state from the pool */ | |
9de87eea | 210 | scm_i_pthread_mutex_lock (&print_state_mutex); |
d2e53ed6 | 211 | if (!scm_is_null (print_state_pool)) |
698c0295 | 212 | { |
d5cf5324 DH |
213 | answer = SCM_CAR (print_state_pool); |
214 | print_state_pool = SCM_CDR (print_state_pool); | |
698c0295 | 215 | } |
9de87eea | 216 | scm_i_pthread_mutex_unlock (&print_state_mutex); |
698c0295 | 217 | |
7888309b | 218 | return scm_is_false (answer) ? make_print_state () : answer; |
c62fbfe1 | 219 | } |
a51ea417 | 220 | |
698c0295 | 221 | void |
6e8d25a6 | 222 | scm_free_print_state (SCM print_state) |
698c0295 MD |
223 | { |
224 | SCM handle; | |
225 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); | |
226 | /* Cleanup before returning print state to pool. | |
227 | * It is better to do it here. Doing it in scm_prin1 | |
228 | * would cost more since that function is called much more | |
229 | * often. | |
230 | */ | |
231 | pstate->fancyp = 0; | |
bb35f315 | 232 | pstate->revealed = 0; |
d232520a | 233 | pstate->highlight_objects = SCM_EOL; |
9de87eea | 234 | scm_i_pthread_mutex_lock (&print_state_mutex); |
16d4699b | 235 | handle = scm_cons (print_state, print_state_pool); |
d5cf5324 | 236 | print_state_pool = handle; |
9de87eea | 237 | scm_i_pthread_mutex_unlock (&print_state_mutex); |
dfd03fb9 MD |
238 | } |
239 | ||
240 | SCM | |
241 | scm_i_port_with_print_state (SCM port, SCM print_state) | |
242 | { | |
243 | if (SCM_UNBNDP (print_state)) | |
244 | { | |
245 | if (SCM_PORT_WITH_PS_P (port)) | |
246 | return port; | |
247 | else | |
248 | print_state = scm_make_print_state (); | |
249 | /* port does not need to be coerced since it doesn't have ps */ | |
250 | } | |
251 | else | |
252 | port = SCM_COERCE_OUTPORT (port); | |
253 | SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps, | |
254 | SCM_UNPACK (scm_cons (port, print_state))); | |
698c0295 | 255 | } |
1cc91f1b | 256 | |
a51ea417 | 257 | static void |
1bbd0b84 | 258 | grow_ref_stack (scm_print_state *pstate) |
a51ea417 | 259 | { |
4057a3e0 MV |
260 | SCM old_vect = pstate->ref_vect; |
261 | size_t old_size = SCM_SIMPLE_VECTOR_LENGTH (old_vect); | |
262 | size_t new_size = 2 * pstate->ceiling; | |
00ffa0e7 | 263 | SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED); |
b17004b8 DH |
264 | unsigned long int i; |
265 | ||
266 | for (i = 0; i != old_size; ++i) | |
4057a3e0 | 267 | SCM_SIMPLE_VECTOR_SET (new_vect, i, SCM_SIMPLE_VECTOR_REF (old_vect, i)); |
b17004b8 DH |
268 | |
269 | pstate->ref_vect = new_vect; | |
bf685b6d | 270 | pstate->ceiling = new_size; |
a51ea417 MD |
271 | } |
272 | ||
509759dd MV |
273 | #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i)) |
274 | #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v)) | |
1cc91f1b | 275 | |
a51ea417 | 276 | static void |
34d19ef6 | 277 | print_circref (SCM port, scm_print_state *pstate, SCM ref) |
a51ea417 | 278 | { |
c014a02e ML |
279 | register long i; |
280 | long self = pstate->top - 1; | |
c62fbfe1 | 281 | i = pstate->top - 1; |
509759dd | 282 | if (scm_is_pair (PSTATE_STACK_REF (pstate, i))) |
c62fbfe1 MD |
283 | { |
284 | while (i > 0) | |
285 | { | |
509759dd MV |
286 | if (!scm_is_pair (PSTATE_STACK_REF (pstate, i-1)) |
287 | || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate, i-1)), | |
288 | SCM_CDR (PSTATE_STACK_REF (pstate, i)))) | |
c62fbfe1 MD |
289 | break; |
290 | --i; | |
291 | } | |
292 | self = i; | |
293 | } | |
294 | for (i = pstate->top - 1; 1; --i) | |
509759dd | 295 | if (scm_is_eq (PSTATE_STACK_REF(pstate, i), ref)) |
c62fbfe1 | 296 | break; |
b7f3516f | 297 | scm_putc ('#', port); |
c62fbfe1 | 298 | scm_intprint (i - self, 10, port); |
b7f3516f | 299 | scm_putc ('#', port); |
a51ea417 MD |
300 | } |
301 | ||
6662998f MV |
302 | /* Print the name of a symbol. */ |
303 | ||
475fa9a5 | 304 | static int |
e23106d5 | 305 | quote_keywordish_symbol (SCM symbol) |
475fa9a5 MV |
306 | { |
307 | SCM option; | |
308 | ||
e23106d5 MG |
309 | if (scm_i_symbol_ref (symbol, 0) != ':' |
310 | && scm_i_symbol_ref (symbol, scm_i_symbol_length (symbol) - 1) != ':') | |
475fa9a5 MV |
311 | return 0; |
312 | ||
313 | option = SCM_PRINT_KEYWORD_STYLE; | |
314 | if (scm_is_false (option)) | |
315 | return 0; | |
316 | if (scm_is_eq (option, sym_reader)) | |
317 | return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE)); | |
318 | return 1; | |
319 | } | |
320 | ||
6662998f | 321 | void |
e23106d5 | 322 | scm_i_print_symbol_name (SCM str, SCM port) |
6662998f | 323 | { |
c6b49e89 MV |
324 | /* This points to the first character that has not yet been written to the |
325 | * port. */ | |
326 | size_t pos = 0; | |
327 | /* This points to the character we're currently looking at. */ | |
6662998f | 328 | size_t end; |
c6b49e89 MV |
329 | /* If the name contains weird characters, we'll escape them with |
330 | * backslashes and set this flag; it indicates that we should surround the | |
331 | * name with "#{" and "}#". */ | |
332 | int weird = 0; | |
333 | /* Backslashes are not sufficient to make a name weird, but if a name is | |
334 | * weird because of other characters, backslahes need to be escaped too. | |
335 | * The first time we see a backslash, we set maybe_weird, and mw_pos points | |
336 | * to the backslash. Then if the name turns out to be weird, we re-process | |
327967ef MV |
337 | * everything starting from mw_pos. |
338 | * We could instead make backslashes always weird. This is not necessary | |
339 | * to ensure that the output is (read)-able, but it would make this code | |
340 | * simpler and faster. */ | |
c6b49e89 | 341 | int maybe_weird = 0; |
6662998f | 342 | size_t mw_pos = 0; |
e23106d5 MG |
343 | size_t len = scm_i_symbol_length (str); |
344 | scm_t_wchar str0 = scm_i_symbol_ref (str, 0); | |
6662998f | 345 | |
e23106d5 MG |
346 | if (len == 0 || str0 == '\'' || str0 == '`' || str0 == ',' |
347 | || quote_keywordish_symbol (str) | |
348 | || (str0 == '.' && len == 1) | |
349 | || scm_is_true (scm_i_string_to_number (scm_symbol_to_string (str), 10))) | |
6662998f MV |
350 | { |
351 | scm_lfwrite ("#{", 2, port); | |
352 | weird = 1; | |
353 | } | |
c6b49e89 | 354 | |
6662998f | 355 | for (end = pos; end < len; ++end) |
e23106d5 | 356 | switch (scm_i_symbol_ref (str, end)) |
6662998f MV |
357 | { |
358 | #ifdef BRACKETS_AS_PARENS | |
359 | case '[': | |
360 | case ']': | |
361 | #endif | |
362 | case '(': | |
363 | case ')': | |
364 | case '"': | |
365 | case ';': | |
c6b49e89 | 366 | case '#': |
6662998f MV |
367 | case SCM_WHITE_SPACES: |
368 | case SCM_LINE_INCREMENTORS: | |
369 | weird_handler: | |
370 | if (maybe_weird) | |
371 | { | |
372 | end = mw_pos; | |
373 | maybe_weird = 0; | |
374 | } | |
375 | if (!weird) | |
376 | { | |
377 | scm_lfwrite ("#{", 2, port); | |
378 | weird = 1; | |
379 | } | |
380 | if (pos < end) | |
e23106d5 | 381 | scm_lfwrite_substr (scm_symbol_to_string (str), pos, end, port); |
6662998f MV |
382 | { |
383 | char buf[2]; | |
384 | buf[0] = '\\'; | |
e23106d5 | 385 | buf[1] = (char) (unsigned char) scm_i_symbol_ref (str, end); |
6662998f MV |
386 | scm_lfwrite (buf, 2, port); |
387 | } | |
388 | pos = end + 1; | |
389 | break; | |
390 | case '\\': | |
391 | if (weird) | |
392 | goto weird_handler; | |
393 | if (!maybe_weird) | |
394 | { | |
395 | maybe_weird = 1; | |
396 | mw_pos = pos; | |
397 | } | |
398 | break; | |
6662998f MV |
399 | default: |
400 | break; | |
401 | } | |
402 | if (pos < end) | |
e23106d5 | 403 | scm_lfwrite_substr (scm_symbol_to_string (str), pos, end, port); |
6662998f MV |
404 | if (weird) |
405 | scm_lfwrite ("}#", 2, port); | |
406 | } | |
407 | ||
e23106d5 MG |
408 | void |
409 | scm_print_symbol_name (const char *str, size_t len, SCM port) | |
410 | { | |
411 | SCM symbol = scm_from_locale_symboln (str, len); | |
ab33782c | 412 | scm_i_print_symbol_name (symbol, port); |
e23106d5 MG |
413 | } |
414 | ||
c62fbfe1 | 415 | /* Print generally. Handles both write and display according to PSTATE. |
0f2d19dd | 416 | */ |
8b840115 MD |
417 | SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); |
418 | SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); | |
1cc91f1b | 419 | |
d232520a MV |
420 | static void iprin1 (SCM exp, SCM port, scm_print_state *pstate); |
421 | ||
dea901d6 MG |
422 | |
423 | /* Print a character as an octal or hex escape. */ | |
424 | #define PRINT_CHAR_ESCAPE(i, port) \ | |
425 | do \ | |
426 | { \ | |
427 | if (!SCM_R6RS_ESCAPES_P) \ | |
428 | scm_intprint (i, 8, port); \ | |
429 | else \ | |
430 | { \ | |
431 | scm_puts ("x", port); \ | |
432 | scm_intprint (i, 16, port); \ | |
433 | } \ | |
434 | } \ | |
435 | while (0) | |
436 | ||
437 | ||
0f2d19dd | 438 | void |
1bbd0b84 | 439 | scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate) |
d232520a MV |
440 | { |
441 | if (pstate->fancyp | |
442 | && scm_is_true (scm_memq (exp, pstate->highlight_objects))) | |
443 | { | |
81ae25da | 444 | scm_display (SCM_PRINT_HIGHLIGHT_PREFIX, port); |
d232520a | 445 | iprin1 (exp, port, pstate); |
81ae25da | 446 | scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX, port); |
d232520a MV |
447 | } |
448 | else | |
449 | iprin1 (exp, port, pstate); | |
450 | } | |
451 | ||
452 | static void | |
453 | iprin1 (SCM exp, SCM port, scm_print_state *pstate) | |
0f2d19dd | 454 | { |
54778cd3 | 455 | switch (SCM_ITAG3 (exp)) |
0f2d19dd | 456 | { |
e34f941a DH |
457 | case scm_tc3_tc7_1: |
458 | case scm_tc3_tc7_2: | |
459 | /* These tc3 tags should never occur in an immediate value. They are | |
460 | * only used in cell types of non-immediates, i. e. the value returned | |
461 | * by SCM_CELL_TYPE (exp) can use these tags. | |
462 | */ | |
463 | scm_ipruk ("immediate", exp, port); | |
464 | break; | |
465 | case scm_tc3_int_1: | |
466 | case scm_tc3_int_2: | |
e11e83f3 | 467 | scm_intprint (SCM_I_INUM (exp), 10, port); |
0f2d19dd | 468 | break; |
e34f941a | 469 | case scm_tc3_imm24: |
7866a09b | 470 | if (SCM_CHARP (exp)) |
0f2d19dd | 471 | { |
b7f3516f | 472 | if (SCM_WRITINGP (pstate)) |
07f49ac7 LC |
473 | write_character (SCM_CHAR (exp), port, 0); |
474 | else | |
b7f3516f | 475 | { |
07f49ac7 LC |
476 | if (!display_character (SCM_CHAR (exp), port, |
477 | scm_i_get_conversion_strategy (port))) | |
478 | scm_encoding_error (__func__, errno, | |
479 | "cannot convert to output locale", | |
480 | "UTF-32", scm_i_get_port_encoding (port), | |
481 | scm_string (scm_list_1 (exp))); | |
b7f3516f | 482 | } |
0f2d19dd | 483 | } |
a51ea417 | 484 | else if (SCM_IFLAGP (exp) |
e17d318f DH |
485 | && ((size_t) SCM_IFLAGNUM (exp) < (sizeof iflagnames / sizeof (char *)))) |
486 | { | |
487 | scm_puts (iflagnames [SCM_IFLAGNUM (exp)], port); | |
488 | } | |
0f2d19dd | 489 | else |
e34f941a DH |
490 | { |
491 | /* unknown immediate value */ | |
492 | scm_ipruk ("immediate", exp, port); | |
493 | } | |
0f2d19dd | 494 | break; |
e34f941a | 495 | case scm_tc3_cons: |
0f2d19dd JB |
496 | switch (SCM_TYP7 (exp)) |
497 | { | |
904a077d MV |
498 | case scm_tcs_struct: |
499 | { | |
500 | ENTER_NESTED_DATA (pstate, exp, circref); | |
501 | if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS) | |
502 | { | |
503 | SCM pwps, print = pstate->writingp ? g_write : g_display; | |
504 | if (!print) | |
505 | goto print_struct; | |
dfd03fb9 | 506 | pwps = scm_i_port_with_print_state (port, pstate->handle); |
7663c008 | 507 | pstate->revealed = 1; |
904a077d MV |
508 | scm_call_generic_2 (print, exp, pwps); |
509 | } | |
510 | else | |
511 | { | |
512 | print_struct: | |
513 | scm_print_struct (exp, port, pstate); | |
514 | } | |
515 | EXIT_NESTED_DATA (pstate); | |
516 | } | |
517 | break; | |
0f2d19dd JB |
518 | case scm_tcs_cons_imcar: |
519 | case scm_tcs_cons_nimcar: | |
c62fbfe1 MD |
520 | ENTER_NESTED_DATA (pstate, exp, circref); |
521 | scm_iprlist ("(", exp, ')', port, pstate); | |
522 | EXIT_NESTED_DATA (pstate); | |
a51ea417 MD |
523 | break; |
524 | circref: | |
c62fbfe1 | 525 | print_circref (port, pstate, exp); |
0f2d19dd | 526 | break; |
534c55a9 DH |
527 | case scm_tc7_number: |
528 | switch SCM_TYP16 (exp) { | |
529 | case scm_tc16_big: | |
530 | scm_bigprint (exp, port, pstate); | |
531 | break; | |
532 | case scm_tc16_real: | |
533 | scm_print_real (exp, port, pstate); | |
534 | break; | |
535 | case scm_tc16_complex: | |
536 | scm_print_complex (exp, port, pstate); | |
537 | break; | |
f92e85f7 MV |
538 | case scm_tc16_fraction: |
539 | scm_i_print_fraction (exp, port, pstate); | |
540 | break; | |
534c55a9 DH |
541 | } |
542 | break; | |
9c44cd45 MG |
543 | case scm_tc7_string: |
544 | if (SCM_WRITINGP (pstate)) | |
545 | { | |
07f49ac7 | 546 | size_t len, i; |
9c44cd45 MG |
547 | |
548 | scm_putc ('"', port); | |
549 | len = scm_i_string_length (exp); | |
550 | for (i = 0; i < len; ++i) | |
07f49ac7 LC |
551 | write_character (scm_i_string_ref (exp, i), port, 1); |
552 | ||
9c44cd45 MG |
553 | scm_putc ('"', port); |
554 | scm_remember_upto_here_1 (exp); | |
555 | } | |
556 | else | |
6234ff20 | 557 | scm_lfwrite_str (exp, port); |
9c44cd45 MG |
558 | scm_remember_upto_here_1 (exp); |
559 | break; | |
28b06554 | 560 | case scm_tc7_symbol: |
cc95e00a | 561 | if (scm_i_symbol_is_interned (exp)) |
9ff28a13 | 562 | { |
e23106d5 | 563 | scm_i_print_symbol_name (exp, port); |
9ff28a13 MV |
564 | scm_remember_upto_here_1 (exp); |
565 | } | |
566 | else | |
567 | { | |
568 | scm_puts ("#<uninterned-symbol ", port); | |
e23106d5 | 569 | scm_i_print_symbol_name (exp, port); |
9ff28a13 | 570 | scm_putc (' ', port); |
0345e278 | 571 | scm_uintprint (SCM_UNPACK (exp), 16, port); |
9ff28a13 MV |
572 | scm_putc ('>', port); |
573 | } | |
6662998f | 574 | break; |
e5aca4b5 MV |
575 | case scm_tc7_variable: |
576 | scm_i_variable_print (exp, port, pstate); | |
577 | break; | |
2fb924f6 AW |
578 | case scm_tc7_program: |
579 | scm_i_program_print (exp, port, pstate); | |
580 | break; | |
5b46a8c2 LC |
581 | case scm_tc7_pointer: |
582 | scm_i_pointer_print (exp, port, pstate); | |
e2c2a699 | 583 | break; |
c99de5aa AW |
584 | case scm_tc7_hashtable: |
585 | scm_i_hashtable_print (exp, port, pstate); | |
586 | break; | |
9ea31741 AW |
587 | case scm_tc7_fluid: |
588 | scm_i_fluid_print (exp, port, pstate); | |
589 | break; | |
45cf2428 AW |
590 | case scm_tc7_dynamic_state: |
591 | scm_i_dynamic_state_print (exp, port, pstate); | |
592 | break; | |
6f3b0cc2 AW |
593 | case scm_tc7_frame: |
594 | scm_i_frame_print (exp, port, pstate); | |
595 | break; | |
596 | case scm_tc7_objcode: | |
597 | scm_i_objcode_print (exp, port, pstate); | |
598 | break; | |
599 | case scm_tc7_vm: | |
600 | scm_i_vm_print (exp, port, pstate); | |
601 | break; | |
602 | case scm_tc7_vm_cont: | |
603 | scm_i_vm_cont_print (exp, port, pstate); | |
604 | break; | |
bbb2ecd1 AW |
605 | case scm_tc7_prompt: |
606 | scm_i_prompt_print (exp, port, pstate); | |
607 | break; | |
608 | case scm_tc7_with_fluids: | |
609 | scm_i_with_fluids_print (exp, port, pstate); | |
610 | break; | |
0f2d19dd | 611 | case scm_tc7_wvect: |
c62fbfe1 | 612 | ENTER_NESTED_DATA (pstate, exp, circref); |
0f2d19dd | 613 | if (SCM_IS_WHVEC (exp)) |
b7f3516f | 614 | scm_puts ("#wh(", port); |
0f2d19dd | 615 | else |
b7f3516f | 616 | scm_puts ("#w(", port); |
0f2d19dd JB |
617 | goto common_vector_printer; |
618 | ||
807e5a66 LC |
619 | case scm_tc7_bytevector: |
620 | scm_i_print_bytevector (exp, port, pstate); | |
621 | break; | |
0f2d19dd | 622 | case scm_tc7_vector: |
c62fbfe1 | 623 | ENTER_NESTED_DATA (pstate, exp, circref); |
b7f3516f | 624 | scm_puts ("#(", port); |
0f2d19dd | 625 | common_vector_printer: |
9fbaf27c | 626 | { |
c014a02e | 627 | register long i; |
4057a3e0 | 628 | long last = SCM_SIMPLE_VECTOR_LENGTH (exp) - 1; |
9fbaf27c | 629 | int cutp = 0; |
4057a3e0 MV |
630 | if (pstate->fancyp |
631 | && SCM_SIMPLE_VECTOR_LENGTH (exp) > pstate->length) | |
9fbaf27c MD |
632 | { |
633 | last = pstate->length - 1; | |
634 | cutp = 1; | |
635 | } | |
c367c4b4 | 636 | if (SCM_I_WVECTP (exp)) |
9fbaf27c | 637 | { |
c367c4b4 LC |
638 | /* Elements of weak vectors may not be accessed via the |
639 | `SIMPLE_VECTOR_REF ()' macro. */ | |
640 | for (i = 0; i < last; ++i) | |
641 | { | |
642 | scm_iprin1 (scm_c_vector_ref (exp, i), | |
643 | port, pstate); | |
644 | scm_putc (' ', port); | |
645 | } | |
9fbaf27c | 646 | } |
c367c4b4 LC |
647 | else |
648 | { | |
649 | for (i = 0; i < last; ++i) | |
650 | { | |
651 | scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp, i), port, pstate); | |
652 | scm_putc (' ', port); | |
653 | } | |
654 | } | |
655 | ||
9fbaf27c MD |
656 | if (i == last) |
657 | { | |
658 | /* CHECK_INTS; */ | |
c367c4b4 | 659 | scm_iprin1 (scm_c_vector_ref (exp, i), port, pstate); |
9fbaf27c MD |
660 | } |
661 | if (cutp) | |
b7f3516f TT |
662 | scm_puts (" ...", port); |
663 | scm_putc (')', port); | |
9fbaf27c | 664 | } |
c62fbfe1 | 665 | EXIT_NESTED_DATA (pstate); |
0f2d19dd | 666 | break; |
0f2d19dd | 667 | case scm_tc7_port: |
5ca6dc39 JB |
668 | { |
669 | register long i = SCM_PTOBNUM (exp); | |
670 | if (i < scm_numptob | |
671 | && scm_ptobs[i].print | |
672 | && (scm_ptobs[i].print) (exp, port, pstate)) | |
a51ea417 | 673 | break; |
5ca6dc39 JB |
674 | goto punk; |
675 | } | |
676 | case scm_tc7_smob: | |
7a7f7c53 DH |
677 | ENTER_NESTED_DATA (pstate, exp, circref); |
678 | SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate); | |
679 | EXIT_NESTED_DATA (pstate); | |
680 | break; | |
0f2d19dd | 681 | default: |
314b8716 | 682 | /* case scm_tcs_closures: */ |
a51ea417 MD |
683 | punk: |
684 | scm_ipruk ("type", exp, port); | |
0f2d19dd JB |
685 | } |
686 | } | |
687 | } | |
688 | ||
c62fbfe1 MD |
689 | /* Print states are necessary for circular reference safe printing. |
690 | * They are also expensive to allocate. Therefore print states are | |
691 | * kept in a pool so that they can be reused. | |
692 | */ | |
1cc91f1b | 693 | |
bb35f315 MV |
694 | /* The PORT argument can also be a print-state/port pair, which will |
695 | * then be used instead of allocating a new print state. This is | |
696 | * useful for continuing a chain of print calls from Scheme. */ | |
697 | ||
a51ea417 | 698 | void |
1bbd0b84 | 699 | scm_prin1 (SCM exp, SCM port, int writingp) |
a51ea417 | 700 | { |
c4f37e80 MV |
701 | SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */ |
702 | SCM pstate_scm; | |
c62fbfe1 | 703 | scm_print_state *pstate; |
15635be5 | 704 | int old_writingp; |
c62fbfe1 | 705 | |
bb35f315 MV |
706 | /* If PORT is a print-state/port pair, use that. Else create a new |
707 | print-state. */ | |
c4f37e80 | 708 | |
0c95b57d | 709 | if (SCM_PORT_WITH_PS_P (port)) |
bb35f315 | 710 | { |
52235e71 MD |
711 | pstate_scm = SCM_PORT_WITH_PS_PS (port); |
712 | port = SCM_PORT_WITH_PS_PORT (port); | |
bb35f315 MV |
713 | } |
714 | else | |
c62fbfe1 | 715 | { |
c4f37e80 | 716 | /* First try to allocate a print state from the pool */ |
9de87eea | 717 | scm_i_pthread_mutex_lock (&print_state_mutex); |
d2e53ed6 | 718 | if (!scm_is_null (print_state_pool)) |
c4f37e80 | 719 | { |
d5cf5324 DH |
720 | handle = print_state_pool; |
721 | print_state_pool = SCM_CDR (print_state_pool); | |
c4f37e80 | 722 | } |
9de87eea | 723 | scm_i_pthread_mutex_unlock (&print_state_mutex); |
7888309b | 724 | if (scm_is_false (handle)) |
d5cf5324 | 725 | handle = scm_list_1 (make_print_state ()); |
c4f37e80 | 726 | pstate_scm = SCM_CAR (handle); |
c62fbfe1 | 727 | } |
c62fbfe1 | 728 | |
c4f37e80 | 729 | pstate = SCM_PRINT_STATE (pstate_scm); |
15635be5 | 730 | old_writingp = pstate->writingp; |
c62fbfe1 MD |
731 | pstate->writingp = writingp; |
732 | scm_iprin1 (exp, port, pstate); | |
15635be5 | 733 | pstate->writingp = old_writingp; |
c62fbfe1 | 734 | |
bb35f315 MV |
735 | /* Return print state to pool if it has been created above and |
736 | hasn't escaped to Scheme. */ | |
737 | ||
7888309b | 738 | if (scm_is_true (handle) && !pstate->revealed) |
c4f37e80 | 739 | { |
9de87eea | 740 | scm_i_pthread_mutex_lock (&print_state_mutex); |
d5cf5324 DH |
741 | SCM_SETCDR (handle, print_state_pool); |
742 | print_state_pool = handle; | |
9de87eea | 743 | scm_i_pthread_mutex_unlock (&print_state_mutex); |
c4f37e80 | 744 | } |
a51ea417 MD |
745 | } |
746 | ||
07f49ac7 LC |
747 | /* Attempt to display CH to PORT according to STRATEGY. Return non-zero |
748 | if CH was successfully displayed, zero otherwise (e.g., if it was not | |
749 | representable in PORT's encoding.) */ | |
750 | static int | |
751 | display_character (scm_t_wchar ch, SCM port, | |
752 | scm_t_string_failed_conversion_handler strategy) | |
9c44cd45 | 753 | { |
07f49ac7 LC |
754 | int printed; |
755 | const char *encoding; | |
756 | ||
757 | encoding = scm_i_get_port_encoding (port); | |
758 | if (encoding == NULL) | |
759 | { | |
760 | if (ch <= 0xff) | |
761 | { | |
762 | scm_putc (ch, port); | |
763 | printed = 1; | |
764 | } | |
765 | else | |
766 | printed = 0; | |
767 | } | |
768 | else | |
769 | { | |
770 | size_t len; | |
f1ee6d54 | 771 | char locale_encoded[8 * sizeof (ch)], *result; |
07f49ac7 LC |
772 | |
773 | len = sizeof (locale_encoded); | |
774 | result = u32_conv_to_encoding (encoding, strategy, | |
775 | (scm_t_uint32 *) &ch, 1, | |
776 | NULL, locale_encoded, &len); | |
777 | if (result != NULL) | |
778 | { | |
779 | /* CH is graphic; print it. */ | |
780 | ||
781 | if (strategy == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE) | |
782 | { | |
783 | /* Apply the same escaping syntax as in `write_character'. */ | |
784 | if (SCM_R6RS_ESCAPES_P) | |
f1ee6d54 LC |
785 | { |
786 | /* LOCALE_ENCODED is large enough to store an R6RS | |
787 | `\xNNNN;' escape sequence. However, libunistring | |
788 | up to 0.9.3 (included) always returns a | |
789 | heap-allocated RESULT. */ | |
790 | if (SCM_UNLIKELY (result != locale_encoded)) | |
791 | result = scm_realloc (result, len * 7); | |
792 | ||
793 | scm_i_unistring_escapes_to_r6rs_escapes (result, &len); | |
794 | } | |
07f49ac7 LC |
795 | else |
796 | scm_i_unistring_escapes_to_guile_escapes (result, &len); | |
797 | } | |
9c44cd45 | 798 | |
07f49ac7 LC |
799 | scm_lfwrite (result, len, port); |
800 | printed = 1; | |
801 | ||
802 | if (SCM_UNLIKELY (result != locale_encoded)) | |
803 | free (result); | |
804 | } | |
805 | else | |
806 | printed = 0; | |
807 | } | |
808 | ||
809 | return printed; | |
810 | } | |
811 | ||
33d92fe6 LC |
812 | /* Attempt to pretty-print CH, a combining character, to PORT. Return |
813 | zero upon failure, non-zero otherwise. The idea is to print CH above | |
814 | a dotted circle to make it more visible. */ | |
815 | static int | |
816 | write_combining_character (scm_t_wchar ch, SCM port) | |
817 | { | |
818 | int printed; | |
819 | const char *encoding; | |
820 | ||
821 | encoding = scm_i_get_port_encoding (port); | |
822 | if (encoding != NULL) | |
823 | { | |
824 | scm_t_wchar str[2]; | |
825 | char locale_encoded[sizeof (str)], *result; | |
826 | size_t len; | |
827 | ||
828 | str[0] = SCM_CODEPOINT_DOTTED_CIRCLE; | |
829 | str[1] = ch; | |
830 | ||
831 | len = sizeof (locale_encoded); | |
832 | result = u32_conv_to_encoding (encoding, iconveh_error, | |
833 | (scm_t_uint32 *) str, 2, | |
834 | NULL, locale_encoded, &len); | |
835 | if (result != NULL) | |
836 | { | |
837 | scm_lfwrite (result, len, port); | |
838 | printed = 1; | |
839 | if (SCM_UNLIKELY (result != locale_encoded)) | |
840 | free (result); | |
841 | } | |
842 | else | |
843 | /* Can't write the result to PORT. */ | |
844 | printed = 0; | |
845 | } | |
846 | else | |
847 | /* PORT is Latin-1-encoded and can't display the fancy things. */ | |
848 | printed = 0; | |
849 | ||
850 | return printed; | |
851 | } | |
852 | ||
07f49ac7 LC |
853 | /* Write CH to PORT, escaping it if it's non-graphic or not |
854 | representable in PORT's encoding. If STRING_ESCAPES_P is true and CH | |
855 | needs to be escaped, it is escaped using the in-string escape syntax; | |
856 | otherwise the character escape syntax is used. */ | |
857 | static void | |
858 | write_character (scm_t_wchar ch, SCM port, int string_escapes_p) | |
859 | { | |
860 | int printed = 0; | |
861 | ||
862 | if (string_escapes_p) | |
863 | { | |
864 | /* Check if CH deserves special treatment. */ | |
865 | if (ch == '"' || ch == '\\') | |
866 | { | |
867 | scm_putc ('\\', port); | |
868 | scm_putc (ch, port); | |
869 | printed = 1; | |
870 | } | |
871 | else if (ch == ' ' || ch == '\n') | |
872 | { | |
873 | scm_putc (ch, port); | |
874 | printed = 1; | |
875 | } | |
876 | } | |
877 | else | |
33d92fe6 LC |
878 | { |
879 | scm_puts ("#\\", port); | |
880 | ||
881 | if (uc_combining_class (ch) != UC_CCC_NR) | |
882 | /* Character is a combining character, so attempt to | |
883 | pretty-print it. */ | |
884 | printed = write_combining_character (ch, port); | |
885 | } | |
07f49ac7 LC |
886 | |
887 | if (!printed | |
888 | && uc_is_general_category_withtable (ch, | |
889 | UC_CATEGORY_MASK_L | | |
890 | UC_CATEGORY_MASK_M | | |
891 | UC_CATEGORY_MASK_N | | |
892 | UC_CATEGORY_MASK_P | | |
893 | UC_CATEGORY_MASK_S)) | |
894 | /* CH is graphic; attempt to display it. */ | |
895 | printed = display_character (ch, port, iconveh_error); | |
896 | ||
897 | if (!printed) | |
898 | { | |
899 | /* CH isn't graphic or cannot be represented in PORT's | |
900 | encoding. */ | |
901 | ||
902 | if (string_escapes_p) | |
903 | { | |
904 | /* Represent CH using the in-string escape syntax. */ | |
905 | ||
906 | static const char hex[] = "0123456789abcdef"; | |
907 | char buf[9]; | |
908 | ||
909 | if (!SCM_R6RS_ESCAPES_P) | |
910 | { | |
911 | if (ch <= 0xFF) | |
912 | { | |
913 | buf[0] = '\\'; | |
914 | buf[1] = 'x'; | |
915 | buf[2] = hex[ch / 16]; | |
916 | buf[3] = hex[ch % 16]; | |
917 | scm_lfwrite (buf, 4, port); | |
918 | } | |
919 | else if (ch <= 0xFFFF) | |
920 | { | |
921 | buf[0] = '\\'; | |
922 | buf[1] = 'u'; | |
923 | buf[2] = hex[(ch & 0xF000) >> 12]; | |
924 | buf[3] = hex[(ch & 0xF00) >> 8]; | |
925 | buf[4] = hex[(ch & 0xF0) >> 4]; | |
926 | buf[5] = hex[(ch & 0xF)]; | |
927 | scm_lfwrite (buf, 6, port); | |
928 | } | |
929 | else if (ch > 0xFFFF) | |
930 | { | |
931 | buf[0] = '\\'; | |
932 | buf[1] = 'U'; | |
933 | buf[2] = hex[(ch & 0xF00000) >> 20]; | |
934 | buf[3] = hex[(ch & 0xF0000) >> 16]; | |
935 | buf[4] = hex[(ch & 0xF000) >> 12]; | |
936 | buf[5] = hex[(ch & 0xF00) >> 8]; | |
937 | buf[6] = hex[(ch & 0xF0) >> 4]; | |
938 | buf[7] = hex[(ch & 0xF)]; | |
939 | scm_lfwrite (buf, 8, port); | |
940 | } | |
941 | } | |
942 | else | |
943 | { | |
944 | /* Print an R6RS variable-length hex escape: "\xNNNN;". */ | |
945 | scm_t_wchar ch2 = ch; | |
946 | ||
947 | int i = 8; | |
948 | buf[i] = ';'; | |
949 | i --; | |
950 | if (ch == 0) | |
951 | buf[i--] = '0'; | |
952 | else | |
953 | while (ch2 > 0) | |
954 | { | |
955 | buf[i] = hex[ch2 & 0xF]; | |
956 | ch2 >>= 4; | |
957 | i --; | |
958 | } | |
959 | buf[i] = 'x'; | |
960 | i --; | |
961 | buf[i] = '\\'; | |
962 | scm_lfwrite (buf + i, 9 - i, port); | |
963 | } | |
964 | } | |
965 | else | |
966 | { | |
967 | /* Represent CH using the character escape syntax. */ | |
968 | const char *name; | |
969 | ||
970 | name = scm_i_charname (SCM_MAKE_CHAR (ch)); | |
971 | if (name != NULL) | |
972 | scm_puts (name, port); | |
973 | else | |
974 | PRINT_CHAR_ESCAPE (ch, port); | |
975 | } | |
976 | } | |
9c44cd45 | 977 | } |
0f2d19dd JB |
978 | |
979 | /* Print an integer. | |
980 | */ | |
1cc91f1b | 981 | |
0f2d19dd | 982 | void |
a406c9e9 | 983 | scm_intprint (scm_t_intmax n, int radix, SCM port) |
0f2d19dd JB |
984 | { |
985 | char num_buf[SCM_INTBUFLEN]; | |
b7f3516f | 986 | scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port); |
0f2d19dd JB |
987 | } |
988 | ||
a406c9e9 MV |
989 | void |
990 | scm_uintprint (scm_t_uintmax n, int radix, SCM port) | |
991 | { | |
992 | char num_buf[SCM_INTBUFLEN]; | |
993 | scm_lfwrite (num_buf, scm_iuint2str (n, radix, num_buf), port); | |
994 | } | |
995 | ||
0f2d19dd JB |
996 | /* Print an object of unrecognized type. |
997 | */ | |
1cc91f1b | 998 | |
0f2d19dd | 999 | void |
1bbd0b84 | 1000 | scm_ipruk (char *hdr, SCM ptr, SCM port) |
0f2d19dd | 1001 | { |
b7f3516f TT |
1002 | scm_puts ("#<unknown-", port); |
1003 | scm_puts (hdr, port); | |
26224b3f | 1004 | if (1) /* (scm_in_heap_p (ptr)) */ /* FIXME */ |
0f2d19dd | 1005 | { |
b7f3516f | 1006 | scm_puts (" (0x", port); |
0345e278 | 1007 | scm_uintprint (SCM_CELL_WORD_0 (ptr), 16, port); |
b7f3516f | 1008 | scm_puts (" . 0x", port); |
0345e278 | 1009 | scm_uintprint (SCM_CELL_WORD_1 (ptr), 16, port); |
b7f3516f | 1010 | scm_puts (") @", port); |
0f2d19dd | 1011 | } |
b7f3516f | 1012 | scm_puts (" 0x", port); |
0345e278 | 1013 | scm_uintprint (SCM_UNPACK (ptr), 16, port); |
b7f3516f | 1014 | scm_putc ('>', port); |
0f2d19dd JB |
1015 | } |
1016 | ||
1cc91f1b | 1017 | |
904a077d | 1018 | /* Print a list. |
22a52da1 | 1019 | */ |
0f2d19dd | 1020 | void |
34d19ef6 | 1021 | scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate) |
0f2d19dd | 1022 | { |
c62fbfe1 | 1023 | register SCM hare, tortoise; |
c014a02e | 1024 | long floor = pstate->top - 2; |
b7f3516f | 1025 | scm_puts (hdr, port); |
0f2d19dd | 1026 | /* CHECK_INTS; */ |
c62fbfe1 MD |
1027 | if (pstate->fancyp) |
1028 | goto fancy_printing; | |
1029 | ||
1030 | /* Run a hare and tortoise so that total time complexity will be | |
1031 | O(depth * N) instead of O(N^2). */ | |
1032 | hare = SCM_CDR (exp); | |
1033 | tortoise = exp; | |
d2e53ed6 | 1034 | while (scm_is_pair (hare)) |
c62fbfe1 | 1035 | { |
bc36d050 | 1036 | if (scm_is_eq (hare, tortoise)) |
c62fbfe1 MD |
1037 | goto fancy_printing; |
1038 | hare = SCM_CDR (hare); | |
d2e53ed6 | 1039 | if (!scm_is_pair (hare)) |
c62fbfe1 MD |
1040 | break; |
1041 | hare = SCM_CDR (hare); | |
1042 | tortoise = SCM_CDR (tortoise); | |
1043 | } | |
1044 | ||
1045 | /* No cdr cycles intrinsic to this list */ | |
1046 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
d2e53ed6 | 1047 | for (exp = SCM_CDR (exp); scm_is_pair (exp); exp = SCM_CDR (exp)) |
0f2d19dd | 1048 | { |
c014a02e | 1049 | register long i; |
5ca6dc39 | 1050 | |
c62fbfe1 | 1051 | for (i = floor; i >= 0; --i) |
509759dd | 1052 | if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp)) |
c62fbfe1 MD |
1053 | goto circref; |
1054 | PUSH_REF (pstate, exp); | |
b7f3516f | 1055 | scm_putc (' ', port); |
0f2d19dd | 1056 | /* CHECK_INTS; */ |
c62fbfe1 | 1057 | scm_iprin1 (SCM_CAR (exp), port, pstate); |
0f2d19dd | 1058 | } |
c96d76b8 | 1059 | if (!SCM_NULL_OR_NIL_P (exp)) |
0f2d19dd | 1060 | { |
b7f3516f | 1061 | scm_puts (" . ", port); |
c62fbfe1 | 1062 | scm_iprin1 (exp, port, pstate); |
0f2d19dd | 1063 | } |
c62fbfe1 | 1064 | |
a51ea417 | 1065 | end: |
b7f3516f | 1066 | scm_putc (tlr, port); |
c62fbfe1 | 1067 | pstate->top = floor + 2; |
a51ea417 | 1068 | return; |
c62fbfe1 MD |
1069 | |
1070 | fancy_printing: | |
1071 | { | |
c014a02e | 1072 | long n = pstate->length; |
c62fbfe1 MD |
1073 | |
1074 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
1075 | exp = SCM_CDR (exp); --n; | |
d2e53ed6 | 1076 | for (; scm_is_pair (exp); exp = SCM_CDR (exp)) |
c62fbfe1 | 1077 | { |
c014a02e | 1078 | register unsigned long i; |
5ca6dc39 | 1079 | |
c62fbfe1 | 1080 | for (i = 0; i < pstate->top; ++i) |
509759dd | 1081 | if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp)) |
c62fbfe1 MD |
1082 | goto fancy_circref; |
1083 | if (pstate->fancyp) | |
1084 | { | |
1085 | if (n == 0) | |
1086 | { | |
b7f3516f | 1087 | scm_puts (" ...", port); |
c62fbfe1 MD |
1088 | goto skip_tail; |
1089 | } | |
1090 | else | |
1091 | --n; | |
1092 | } | |
1093 | PUSH_REF(pstate, exp); | |
1094 | ++pstate->list_offset; | |
b7f3516f | 1095 | scm_putc (' ', port); |
c62fbfe1 MD |
1096 | /* CHECK_INTS; */ |
1097 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
1098 | } | |
1099 | } | |
c96d76b8 | 1100 | if (!SCM_NULL_OR_NIL_P (exp)) |
c62fbfe1 | 1101 | { |
b7f3516f | 1102 | scm_puts (" . ", port); |
c62fbfe1 MD |
1103 | scm_iprin1 (exp, port, pstate); |
1104 | } | |
1105 | skip_tail: | |
1106 | pstate->list_offset -= pstate->top - floor - 2; | |
a51ea417 | 1107 | goto end; |
a51ea417 | 1108 | |
c62fbfe1 MD |
1109 | fancy_circref: |
1110 | pstate->list_offset -= pstate->top - floor - 2; | |
1111 | ||
1112 | circref: | |
b7f3516f | 1113 | scm_puts (" . ", port); |
c62fbfe1 MD |
1114 | print_circref (port, pstate, exp); |
1115 | goto end; | |
0f2d19dd JB |
1116 | } |
1117 | ||
1118 | \f | |
1119 | ||
bb35f315 MV |
1120 | int |
1121 | scm_valid_oport_value_p (SCM val) | |
1122 | { | |
368cf54d GB |
1123 | return (SCM_OPOUTPORTP (val) |
1124 | || (SCM_PORT_WITH_PS_P (val) | |
1125 | && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val)))); | |
bb35f315 MV |
1126 | } |
1127 | ||
8b840115 | 1128 | /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */ |
1cc91f1b | 1129 | |
0f2d19dd | 1130 | SCM |
1bbd0b84 | 1131 | scm_write (SCM obj, SCM port) |
0f2d19dd JB |
1132 | { |
1133 | if (SCM_UNBNDP (port)) | |
9de87eea | 1134 | port = scm_current_output_port (); |
3eb7e6ee JB |
1135 | |
1136 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write); | |
bb35f315 | 1137 | |
a51ea417 | 1138 | scm_prin1 (obj, port, 1); |
0f2d19dd JB |
1139 | return SCM_UNSPECIFIED; |
1140 | } | |
1141 | ||
1142 | ||
8b840115 | 1143 | /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */ |
1cc91f1b | 1144 | |
0f2d19dd | 1145 | SCM |
1bbd0b84 | 1146 | scm_display (SCM obj, SCM port) |
0f2d19dd JB |
1147 | { |
1148 | if (SCM_UNBNDP (port)) | |
9de87eea | 1149 | port = scm_current_output_port (); |
3eb7e6ee JB |
1150 | |
1151 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display); | |
bb35f315 | 1152 | |
a51ea417 | 1153 | scm_prin1 (obj, port, 0); |
0f2d19dd JB |
1154 | return SCM_UNSPECIFIED; |
1155 | } | |
1156 | ||
70d63753 GB |
1157 | |
1158 | SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1, | |
1159 | (SCM destination, SCM message, SCM args), | |
eca65e90 MG |
1160 | "Write @var{message} to @var{destination}, defaulting to\n" |
1161 | "the current output port.\n" | |
1162 | "@var{message} can contain @code{~A} (was @code{%s}) and\n" | |
1163 | "@code{~S} (was @code{%S}) escapes. When printed,\n" | |
1164 | "the escapes are replaced with corresponding members of\n" | |
1165 | "@var{ARGS}:\n" | |
1166 | "@code{~A} formats using @code{display} and @code{~S} formats\n" | |
1167 | "using @code{write}.\n" | |
1168 | "If @var{destination} is @code{#t}, then use the current output\n" | |
1169 | "port, if @var{destination} is @code{#f}, then return a string\n" | |
1170 | "containing the formatted text. Does not add a trailing newline.") | |
70d63753 GB |
1171 | #define FUNC_NAME s_scm_simple_format |
1172 | { | |
dfd03fb9 | 1173 | SCM port, answer = SCM_UNSPECIFIED; |
70d63753 GB |
1174 | int fReturnString = 0; |
1175 | int writingp; | |
889975e5 | 1176 | size_t start, p, end; |
70d63753 | 1177 | |
bc36d050 | 1178 | if (scm_is_eq (destination, SCM_BOOL_T)) |
daba1a71 | 1179 | { |
9de87eea | 1180 | destination = port = scm_current_output_port (); |
daba1a71 | 1181 | } |
7888309b | 1182 | else if (scm_is_false (destination)) |
daba1a71 MD |
1183 | { |
1184 | fReturnString = 1; | |
dfd03fb9 MD |
1185 | port = scm_mkstrport (SCM_INUM0, |
1186 | scm_make_string (SCM_INUM0, SCM_UNDEFINED), | |
1187 | SCM_OPN | SCM_WRTNG, | |
1188 | FUNC_NAME); | |
1189 | destination = port; | |
daba1a71 MD |
1190 | } |
1191 | else | |
1192 | { | |
1193 | SCM_VALIDATE_OPORT_VALUE (1, destination); | |
dfd03fb9 | 1194 | port = SCM_COERCE_OUTPORT (destination); |
daba1a71 MD |
1195 | } |
1196 | SCM_VALIDATE_STRING (2, message); | |
af45e3b0 | 1197 | SCM_VALIDATE_REST_ARGUMENT (args); |
70d63753 | 1198 | |
889975e5 MG |
1199 | p = 0; |
1200 | start = 0; | |
1201 | end = scm_i_string_length (message); | |
b24b5e13 | 1202 | for (p = start; p != end; ++p) |
889975e5 | 1203 | if (scm_i_string_ref (message, p) == '~') |
70d63753 | 1204 | { |
b24b5e13 | 1205 | if (++p == end) |
6662998f MV |
1206 | break; |
1207 | ||
889975e5 | 1208 | switch (scm_i_string_ref (message, p)) |
6662998f MV |
1209 | { |
1210 | case 'A': case 'a': | |
1211 | writingp = 0; | |
1212 | break; | |
1213 | case 'S': case 's': | |
1214 | writingp = 1; | |
1215 | break; | |
1216 | case '~': | |
889975e5 | 1217 | scm_lfwrite_substr (message, start, p, port); |
6662998f MV |
1218 | start = p + 1; |
1219 | continue; | |
1220 | case '%': | |
889975e5 | 1221 | scm_lfwrite_substr (message, start, p - 1, port); |
dfd03fb9 | 1222 | scm_newline (port); |
6662998f MV |
1223 | start = p + 1; |
1224 | continue; | |
1225 | default: | |
1afff620 | 1226 | SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead", |
889975e5 | 1227 | scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message, p)))); |
6662998f MV |
1228 | |
1229 | } | |
70d63753 | 1230 | |
6662998f | 1231 | |
d2e53ed6 | 1232 | if (!scm_is_pair (args)) |
1afff620 | 1233 | SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A", |
889975e5 | 1234 | scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message, p)))); |
6662998f | 1235 | |
889975e5 | 1236 | scm_lfwrite_substr (message, start, p - 1, port); |
dfd03fb9 | 1237 | /* we pass destination here */ |
70d63753 GB |
1238 | scm_prin1 (SCM_CAR (args), destination, writingp); |
1239 | args = SCM_CDR (args); | |
1240 | start = p + 1; | |
1241 | } | |
6662998f | 1242 | |
889975e5 | 1243 | scm_lfwrite_substr (message, start, p, port); |
bc36d050 | 1244 | if (!scm_is_eq (args, SCM_EOL)) |
1afff620 KN |
1245 | SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments", |
1246 | scm_list_1 (scm_length (args))); | |
70d63753 GB |
1247 | |
1248 | if (fReturnString) | |
1249 | answer = scm_strport_to_string (destination); | |
1250 | ||
daba1a71 | 1251 | return scm_return_first (answer, message); |
70d63753 GB |
1252 | } |
1253 | #undef FUNC_NAME | |
1254 | ||
1255 | ||
3b3b36dd | 1256 | SCM_DEFINE (scm_newline, "newline", 0, 1, 0, |
b450f070 | 1257 | (SCM port), |
8f85c0c6 NJ |
1258 | "Send a newline to @var{port}.\n" |
1259 | "If @var{port} is omitted, send to the current output port.") | |
1bbd0b84 | 1260 | #define FUNC_NAME s_scm_newline |
0f2d19dd JB |
1261 | { |
1262 | if (SCM_UNBNDP (port)) | |
9de87eea | 1263 | port = scm_current_output_port (); |
3eb7e6ee | 1264 | |
34d19ef6 | 1265 | SCM_VALIDATE_OPORT_VALUE (1, port); |
bb35f315 | 1266 | |
0ef4ae82 | 1267 | scm_putc ('\n', SCM_COERCE_OUTPORT (port)); |
0f2d19dd JB |
1268 | return SCM_UNSPECIFIED; |
1269 | } | |
1bbd0b84 | 1270 | #undef FUNC_NAME |
0f2d19dd | 1271 | |
3b3b36dd | 1272 | SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0, |
b450f070 | 1273 | (SCM chr, SCM port), |
eca65e90 | 1274 | "Send character @var{chr} to @var{port}.") |
1bbd0b84 | 1275 | #define FUNC_NAME s_scm_write_char |
0f2d19dd JB |
1276 | { |
1277 | if (SCM_UNBNDP (port)) | |
9de87eea | 1278 | port = scm_current_output_port (); |
3eb7e6ee | 1279 | |
34d19ef6 HWN |
1280 | SCM_VALIDATE_CHAR (1, chr); |
1281 | SCM_VALIDATE_OPORT_VALUE (2, port); | |
07f49ac7 LC |
1282 | |
1283 | port = SCM_COERCE_OUTPORT (port); | |
1284 | if (!display_character (SCM_CHAR (chr), port, | |
1285 | scm_i_get_conversion_strategy (port))) | |
1286 | scm_encoding_error (__func__, errno, | |
1287 | "cannot convert to output locale", | |
1288 | "UTF-32", scm_i_get_port_encoding (port), | |
1289 | scm_string (scm_list_1 (chr))); | |
1290 | ||
0f2d19dd JB |
1291 | return SCM_UNSPECIFIED; |
1292 | } | |
1bbd0b84 | 1293 | #undef FUNC_NAME |
0f2d19dd | 1294 | |
0f2d19dd JB |
1295 | \f |
1296 | ||
bb35f315 | 1297 | /* Call back to Scheme code to do the printing of special objects |
c19bc088 MD |
1298 | * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob |
1299 | * containing PORT and PSTATE. This object can be used as the port for | |
1300 | * display/write etc to continue the current print chain. The REVEALED | |
1301 | * field of PSTATE is set to true to indicate that the print state has | |
1302 | * escaped to Scheme and thus has to be freed by the GC. | |
1303 | */ | |
1304 | ||
92c2555f | 1305 | scm_t_bits scm_tc16_port_with_ps; |
c19bc088 MD |
1306 | |
1307 | /* Print exactly as the port itself would */ | |
1308 | ||
1309 | static int | |
e841c3e0 | 1310 | port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate) |
c19bc088 MD |
1311 | { |
1312 | obj = SCM_PORT_WITH_PS_PORT (obj); | |
1313 | return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate); | |
1314 | } | |
c4f37e80 MV |
1315 | |
1316 | SCM | |
1bbd0b84 | 1317 | scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate) |
c4f37e80 | 1318 | { |
bb35f315 | 1319 | pstate->revealed = 1; |
dfd03fb9 MD |
1320 | return scm_call_2 (proc, exp, |
1321 | scm_i_port_with_print_state (port, pstate->handle)); | |
c19bc088 MD |
1322 | } |
1323 | ||
dfd03fb9 | 1324 | SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 1, 1, 0, |
1bbd0b84 | 1325 | (SCM port, SCM pstate), |
71331188 | 1326 | "Create a new port which behaves like @var{port}, but with an\n" |
dfd03fb9 MD |
1327 | "included print state @var{pstate}. @var{pstate} is optional.\n" |
1328 | "If @var{pstate} isn't supplied and @var{port} already has\n" | |
1329 | "a print state, the old print state is reused.") | |
1bbd0b84 | 1330 | #define FUNC_NAME s_scm_port_with_print_state |
c19bc088 | 1331 | { |
34d19ef6 | 1332 | SCM_VALIDATE_OPORT_VALUE (1, port); |
dfd03fb9 MD |
1333 | if (!SCM_UNBNDP (pstate)) |
1334 | SCM_VALIDATE_PRINTSTATE (2, pstate); | |
1335 | return scm_i_port_with_print_state (port, pstate); | |
c19bc088 | 1336 | } |
1bbd0b84 | 1337 | #undef FUNC_NAME |
c19bc088 | 1338 | |
a1ec6916 | 1339 | SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0, |
1bbd0b84 | 1340 | (SCM port), |
71331188 MG |
1341 | "Return the print state of the port @var{port}. If @var{port}\n" |
1342 | "has no associated print state, @code{#f} is returned.") | |
1bbd0b84 | 1343 | #define FUNC_NAME s_scm_get_print_state |
c19bc088 | 1344 | { |
368cf54d GB |
1345 | if (SCM_PORT_WITH_PS_P (port)) |
1346 | return SCM_PORT_WITH_PS_PS (port); | |
f5f2dcff | 1347 | if (SCM_OUTPUT_PORT_P (port)) |
368cf54d | 1348 | return SCM_BOOL_F; |
276dd677 | 1349 | SCM_WRONG_TYPE_ARG (1, port); |
c4f37e80 | 1350 | } |
1bbd0b84 | 1351 | #undef FUNC_NAME |
bb35f315 | 1352 | |
c4f37e80 | 1353 | \f |
1cc91f1b | 1354 | |
0f2d19dd JB |
1355 | void |
1356 | scm_init_print () | |
0f2d19dd | 1357 | { |
c19bc088 | 1358 | SCM vtable, layout, type; |
d5cf5324 | 1359 | |
62560650 | 1360 | scm_init_opts (scm_print_options, scm_print_opts); |
d5cf5324 | 1361 | |
81ae25da MV |
1362 | scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"), |
1363 | scm_from_locale_string ("{"), | |
1364 | scm_from_locale_symbol ("highlight-suffix"), | |
1365 | scm_from_locale_string ("}"))); | |
1366 | ||
d5cf5324 DH |
1367 | scm_gc_register_root (&print_state_pool); |
1368 | scm_gc_register_root (&scm_print_state_vtable); | |
3ce4544c | 1369 | vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL); |
cc95e00a MV |
1370 | layout = |
1371 | scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT)); | |
1afff620 | 1372 | type = scm_make_struct (vtable, SCM_INUM0, scm_list_1 (layout)); |
cc95e00a | 1373 | scm_set_struct_vtable_name_x (type, scm_from_locale_symbol ("print-state")); |
bb35f315 | 1374 | scm_print_state_vtable = type; |
c4f37e80 | 1375 | |
c19bc088 MD |
1376 | /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */ |
1377 | scm_tc16_port_with_ps = scm_make_smob_type (0, 0); | |
e841c3e0 | 1378 | scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print); |
81ae25da | 1379 | |
a0599745 | 1380 | #include "libguile/print.x" |
475fa9a5 MV |
1381 | |
1382 | scm_print_opts[SCM_PRINT_KEYWORD_STYLE_I].val = SCM_UNPACK (sym_reader); | |
0f2d19dd | 1383 | } |
89e00824 ML |
1384 | |
1385 | /* | |
1386 | Local Variables: | |
1387 | c-file-style: "gnu" | |
1388 | End: | |
1389 | */ |