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