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